webgl2 1.1.7 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/coverage.md CHANGED
@@ -3,34 +3,35 @@
3
3
  > **Legend:** 🟢 ≥80% | 🟡 ≥50% (or ≤6 lines) | 🟠 ≥20% | 🔴 <20%
4
4
 
5
5
  | File | Lines Covered | Lines Missed | Total Lines | Coverage |
6
- |---|---|---|---|---|
7
- | src/lib.rs | 110 | 31 | 141 | 78.01% 🟡 |
8
- | src/naga_wasm_backend/backend.rs | 45 | 1 | 46 | 97.83% 🟢 |
9
- | src/naga_wasm_backend/control_flow.rs | 24 | 11 | 35 | 68.57% 🟡 |
6
+ |---|---|---|---|---:|
7
+ | src/lib.rs | 110 | 29 | 139 | 79.14% 🟡 |
8
+ | src/naga_wasm_backend/backend.rs | 43 | 1 | 44 | 97.73% 🟢 |
9
+ | src/naga_wasm_backend/control_flow.rs | 30 | 12 | 42 | 71.43% 🟡 |
10
10
  | src/naga_wasm_backend/debug/stub.rs | 20 | 0 | 20 | 100.00% 🟢 |
11
- | src/naga_wasm_backend/expressions.rs | 32 | 14 | 46 | 69.57% 🟡 |
11
+ | src/naga_wasm_backend/expressions.rs | 40 | 19 | 59 | 67.80% 🟡 |
12
+ | src/naga_wasm_backend/output_layout.rs | 1 | 1 | 2 | 50.00% 🟡 |
12
13
  | src/naga_wasm_backend/types.rs | 6 | 0 | 6 | 100.00% 🟢 |
13
14
  | src/wasm_gl_emu/framebuffer.rs | 1 | 0 | 1 | 100.00% 🟢 |
14
- | src/wasm_gl_emu/rasterizer.rs | 15 | 0 | 15 | 100.00% 🟢 |
15
+ | src/wasm_gl_emu/rasterizer.rs | 20 | 2 | 22 | 90.91% 🟢 |
15
16
  | src/webgl2_context/buffers.rs | 15 | 1 | 16 | 93.75% 🟢 |
16
- | src/webgl2_context/drawing.rs | 29 | 4 | 33 | 87.88% 🟢 |
17
- | src/webgl2_context/framebuffers.rs | 7 | 0 | 7 | 100.00% 🟢 |
17
+ | src/webgl2_context/drawing.rs | 34 | 7 | 41 | 82.93% 🟢 |
18
+ | src/webgl2_context/framebuffers.rs | 8 | 0 | 8 | 100.00% 🟢 |
18
19
  | src/webgl2_context/registry.rs | 5 | 0 | 5 | 100.00% 🟢 |
19
- | src/webgl2_context/renderbuffers.rs | 10 | 0 | 10 | 100.00% 🟢 |
20
- | src/webgl2_context/shaders.rs | 70 | 3 | 73 | 95.89% 🟢 |
21
- | src/webgl2_context/state.rs | 14 | 3 | 17 | 82.35% 🟢 |
20
+ | src/webgl2_context/renderbuffers.rs | 11 | 0 | 11 | 100.00% 🟢 |
21
+ | src/webgl2_context/shaders.rs | 80 | 13 | 93 | 86.02% 🟢 |
22
+ | src/webgl2_context/state.rs | 14 | 2 | 16 | 87.50% 🟢 |
22
23
  | src/webgl2_context/textures.rs | 10 | 0 | 10 | 100.00% 🟢 |
23
- | src/webgl2_context/types.rs | 9 | 1 | 10 | 90.00% 🟢 |
24
+ | src/webgl2_context/types.rs | 10 | 2 | 12 | 83.33% 🟢 |
24
25
  | src/webgl2_context/vaos.rs | 36 | 0 | 36 | 100.00% 🟢 |
25
- | src/webgpu/adapter.rs | 2 | 0 | 2 | 100.00% 🟢 |
26
+ | src/webgpu/adapter.rs | 3 | 0 | 3 | 100.00% 🟢 |
26
27
  | src/webgpu/backend.rs | 24 | 26 | 50 | 48.00% 🟠 |
27
- | **Total** | **484** | **95** | **579** | **83.59% 🟢** |
28
+ | **Total** | **521** | **115** | **636** | **81.92% 🟢** |
28
29
 
29
30
  ## Top Missed Files
30
31
 
31
32
  | File | Lines Missed | Illustrative Line | Coverage |
32
- |---|---|---|---|
33
- | src/lib.rs | 31/141 | [516] `/// Set uniform 3f.` | 78.01% 🟡 |
33
+ |---|---|---|---:|
34
+ | src/lib.rs | 29/139 | [1011] `#[no_mangle]` | 79.14% 🟡 |
34
35
  | src/webgpu/backend.rs | 26/50 | [919] `}` | 48.00% 🟠 |
35
- | src/naga_wasm_backend/expressions.rs | 14/46 | [11] `pub fn is_integer_type(type_inner: &TypeInner) -> bool {` | 69.57% 🟡 |
36
- | src/naga_wasm_backend/control_flow.rs | 11/35 | [171] `for i in (0..types.len()).rev() {` | 68.57% 🟡 |
36
+ | src/naga_wasm_backend/expressions.rs | 19/59 | [11] `pub fn is_integer_type(type_inner: &TypeInner) -> bool {` | 67.80% 🟡 |
37
+ | src/webgl2_context/shaders.rs | 13/93 | [575] `if !varying_locations.values().any(|&v| v == *loc) {` | 86.02% 🟢 |
package/index.js CHANGED
@@ -45,7 +45,7 @@ const isNode =
45
45
  * This function:
46
46
  * 1. Auto-loads webgl2.wasm (expects it next to index2.js)
47
47
  * 2. Instantiates the WASM module with memory
48
- * 3. Creates a Rust-owned context via wasm_create_context()
48
+ * 3. Creates a Rust-owned context via wasm_create_context_with_flags(flags)
49
49
  * 4. Returns a WasmWebGL2RenderingContext JS wrapper
50
50
  *
51
51
  * @param {{
@@ -83,21 +83,25 @@ export async function webGL2({ debug = (typeof process !== 'undefined' ? process
83
83
  ex.wasm_init_coverage(numEntries);
84
84
  }
85
85
 
86
- // Create a context in WASM
87
- const ctxHandle = ex.wasm_create_context();
86
+ // Determine debug flags for creation
87
+ const debugShaders = debug === true || debug === 'shaders' || debug === 'all';
88
+ const debugRust = debug === true || debug === 'rust' || debug === 'all';
89
+ const flags = (debugShaders ? 1 : 0); // only shader debug encoded in flags
90
+
91
+ // Default size to 640x480 if not provided
92
+ const width = size?.width ?? 640;
93
+ const height = size?.height ?? 480;
94
+
95
+ // Create a context in WASM using the flags-aware API (mandatory)
96
+ const ctxHandle = ex.wasm_create_context_with_flags(flags, width, height);
97
+
88
98
  if (ctxHandle === 0) {
89
99
  const msg = readErrorMessage(instance);
90
100
  throw new Error(`Failed to create context: ${msg}`);
91
101
  }
92
102
 
93
- // Wrap and return
94
- const gl = new WasmWebGL2RenderingContext(instance, ctxHandle);
95
-
96
- // Set debug mode if requested
97
- if (debug) {
98
- gl.setDebugMode(debug);
99
- }
100
-
103
+ // Wrap and return, pass debug booleans to the JS wrapper
104
+ const gl = new WasmWebGL2RenderingContext({ instance, ctxHandle, width, height, debugShaders: !!debugShaders });
101
105
 
102
106
  if (size && typeof size.width === 'number' && typeof size.height === 'number') {
103
107
  gl.resize(size.width, size.height);
@@ -185,8 +189,8 @@ async function initWASM({ debug } = {}) {
185
189
 
186
190
  // Verify required exports
187
191
  const ex = instance.exports;
188
- if (typeof ex.wasm_create_context !== 'function') {
189
- throw new Error('WASM module missing wasm_create_context export');
192
+ if (typeof ex.wasm_create_context_with_flags !== 'function') {
193
+ throw new Error('WASM module missing wasm_create_context_with_flags export');
190
194
  }
191
195
  if (!(ex.memory instanceof WebAssembly.Memory)) {
192
196
  throw new Error('WASM module missing memory export');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webgl2",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
4
4
  "description": "WebGL2 tools to derisk large GPU projects on the web beyond toys and demos.",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -96,37 +96,22 @@ export class WasmWebGL2RenderingContext {
96
96
  /**
97
97
  * @param {WebAssembly.Instance} instance
98
98
  * @param {u32} ctxHandle
99
+ * @param {number} width
100
+ * @param {number} height
101
+ * @param {boolean} [debugShaders]
99
102
  */
100
- constructor(instance, ctxHandle) {
103
+ constructor({ instance, ctxHandle, width, height, debugShaders = false }) {
101
104
  this._instance = instance;
102
105
  this._ctxHandle = ctxHandle;
103
106
  this._destroyed = false;
104
107
  /** @type {import('./webgl2_resources.js').WasmWebGLProgram | null} */
105
108
  this._currentProgram = null;
106
- this._debugMode = 0; // 0=None, 1=Shaders, 2=Rust, 3=All
107
- this._drawingBufferWidth = 640;
108
- this._drawingBufferHeight = 480;
109
-
110
- WasmWebGL2RenderingContext._contexts.set(ctxHandle, this);
111
- }
112
-
113
- setDebugMode(mode) {
114
- this._assertNotDestroyed();
115
- const ex = this._instance.exports;
116
- if (!ex || typeof ex.wasm_ctx_set_debug_mode !== 'function') {
117
- console.warn('wasm_ctx_set_debug_mode not found');
118
- return;
119
- }
120
-
121
- let modeVal = 0;
122
- if (mode === true || mode === 'all') modeVal = 3;
123
- else if (mode === 'shaders') modeVal = 1;
124
- else if (mode === 'rust') modeVal = 2;
125
- else modeVal = 0;
109
+ // Explicit booleans for clarity
110
+ this._debugShaders = !!debugShaders;
111
+ this._drawingBufferWidth = width;
112
+ this._drawingBufferHeight = height;
126
113
 
127
- this._debugMode = modeVal;
128
- const code = ex.wasm_ctx_set_debug_mode(this._ctxHandle, modeVal);
129
- _checkErr(code, this._instance);
114
+ WasmWebGL2RenderingContext._contexts.set(this._ctxHandle, this);
130
115
  }
131
116
 
132
117
  get drawingBufferWidth() {
@@ -177,8 +162,6 @@ export class WasmWebGL2RenderingContext {
177
162
  console.error(` attrPtr: ${attrPtr}, uniformPtr: ${uniformPtr}, varyingPtr: ${varyingPtr}, privatePtr: ${privatePtr}, texturePtr: ${texturePtr}`);
178
163
  throw e;
179
164
  }
180
- } else {
181
- // console.log(`DEBUG: Shader instance or main missing for type ${type}. Instance: ${!!shaderInstance}`);
182
165
  }
183
166
  }
184
167
 
@@ -533,9 +516,15 @@ export class WasmWebGL2RenderingContext {
533
516
  const code = ex.wasm_ctx_link_program(this._ctxHandle, programHandle);
534
517
  _checkErr(code, this._instance);
535
518
 
536
- // After linking, we need to instantiate the WASM modules on the host
519
+ // After linking, we need to instantiate the WASM modules on the host.
520
+ // According to the WebGL 2.0 Spec (https://registry.khronos.org/webgl/specs/latest/2.0/#3.7.6),
521
+ // linkProgram sets the LINK_STATUS parameter. If linking fails, no executable is generated.
522
+ // We must check LINK_STATUS before attempting to instantiate the WASM, as getProgramWasm will return null.
537
523
  if (program && typeof program === 'object') {
538
- this._instantiateProgramShaders(program);
524
+ const linkStatus = this.getProgramParameter(program, this.LINK_STATUS);
525
+ if (linkStatus) {
526
+ this._instantiateProgramShaders(program);
527
+ }
539
528
  }
540
529
  }
541
530
 
@@ -544,7 +533,7 @@ export class WasmWebGL2RenderingContext {
544
533
  const fsWasm = this.getProgramWasm(program, this.FRAGMENT_SHADER);
545
534
 
546
535
  const createDebugEnv = (type, instanceRef) => {
547
- if (this._debugMode !== 1 && this._debugMode !== 3) return {};
536
+ if (!this._debugShaders) return {};
548
537
 
549
538
  const stubCode = this.getProgramDebugStub(program, type);
550
539
  if (!stubCode) return {};
@@ -564,6 +553,10 @@ export class WasmWebGL2RenderingContext {
564
553
 
565
554
  return {
566
555
  debug_step: (line, funcIdx, resultPtr) => {
556
+ if (line === 999999) {
557
+ console.log(`DEBUG LOG: val=${funcIdx} (0x${(funcIdx >>> 0).toString(16)})`);
558
+ return;
559
+ }
567
560
  const func = stubFuncs[line - 1];
568
561
  if (func) {
569
562
  const ctx = {
@@ -582,40 +575,29 @@ export class WasmWebGL2RenderingContext {
582
575
  };
583
576
  };
584
577
 
585
- if (vsWasm) {
586
- try {
587
- const vsModule = new WebAssembly.Module(vsWasm);
588
- const instanceRef = { current: null };
589
- const debugEnv = createDebugEnv(this.VERTEX_SHADER, instanceRef);
590
-
591
- program._vsInstance = new WebAssembly.Instance(vsModule, {
592
- env: {
593
- memory: this._instance.exports.memory,
594
- ...debugEnv
595
- }
596
- });
597
- instanceRef.current = program._vsInstance;
598
- } catch (e) {
599
- // console.log(`DEBUG: VS Instance creation failed: ${e}`);
578
+ const vsModule = new WebAssembly.Module(vsWasm);
579
+ const vsInstanceRef = { current: null };
580
+ const vsDebugEnv = createDebugEnv(this.VERTEX_SHADER, vsInstanceRef);
581
+
582
+ program._vsInstance = new WebAssembly.Instance(vsModule, {
583
+ env: {
584
+ memory: this._instance.exports.memory,
585
+ ...vsDebugEnv
600
586
  }
601
- }
602
- if (fsWasm) {
603
- try {
604
- const fsModule = new WebAssembly.Module(fsWasm);
605
- const instanceRef = { current: null };
606
- const debugEnv = createDebugEnv(this.FRAGMENT_SHADER, instanceRef);
607
-
608
- program._fsInstance = new WebAssembly.Instance(fsModule, {
609
- env: {
610
- memory: this._instance.exports.memory,
611
- ...debugEnv
612
- }
613
- });
614
- instanceRef.current = program._fsInstance;
615
- } catch (e) {
616
- // console.log(`DEBUG: FS Instance creation failed: ${e}`);
587
+ });
588
+ vsInstanceRef.current = program._vsInstance;
589
+
590
+ const fsModule = new WebAssembly.Module(fsWasm);
591
+ const fsInstanceRef = { current: null };
592
+ const fsDebugEnv = createDebugEnv(this.FRAGMENT_SHADER, fsInstanceRef);
593
+
594
+ program._fsInstance = new WebAssembly.Instance(fsModule, {
595
+ env: {
596
+ memory: this._instance.exports.memory,
597
+ ...fsDebugEnv
617
598
  }
618
- }
599
+ });
600
+ fsInstanceRef.current = program._fsInstance;
619
601
  }
620
602
 
621
603
  getProgramDebugStub(program, shaderType) {
@@ -1363,7 +1345,8 @@ export class WasmWebGL2RenderingContext {
1363
1345
  try {
1364
1346
  const mem = new Uint8Array(ex.memory.buffer);
1365
1347
  mem.set(bytes, ptr);
1366
- const code = ex.wasm_ctx_uniform_matrix_4fv(this._ctxHandle, locHandle, transpose ? 1 : 0, ptr, len);
1348
+ const count = len / 4;
1349
+ const code = ex.wasm_ctx_uniform_matrix_4fv(this._ctxHandle, locHandle, transpose ? 1 : 0, ptr, count);
1367
1350
  _checkErr(code, this._instance);
1368
1351
  } finally {
1369
1352
  ex.wasm_free(ptr);
package/webgl2.debug.wasm CHANGED
Binary file
package/webgl2.wasm CHANGED
Binary file