From 26ce7884fe627e23fa615fe062c3055340e43111 Mon Sep 17 00:00:00 2001 From: Luis Montes Date: Tue, 23 Jun 2026 08:22:46 -0700 Subject: [PATCH 1/2] v1.2.0: texSubImage2D Image/Canvas source form + release pending fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - texSubImage2D now accepts the (target, level, xoffset, yoffset, format, type, source) Image/Canvas form, mirroring texImage2D. three.js uses this path, so without it callers had to patch three. Closes #1. - Include the already-committed texImage2D Image/Canvas support and the transformFeedbackVaryings arg-count fix (drop stray varyings.length) in a published release — npm 1.1.0 predates both. - Bump version 1.1.0 -> 1.2.0 so these reach npm. - Bump example deps: three ^0.184.0, @kmamal/sdl ^0.11.13, sharp ^0.35.2. - Document the Image/Canvas source form in the README. --- README.md | 1 + examples/threejs-ocean/index.mjs | 4 ++-- examples/threejs-ocean/package.json | 8 ++++---- examples/threejs-simple/package.json | 6 +++--- lib/webgl2-context.mjs | 30 +++++++++++++++++++++++++++- package.json | 2 +- 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2673b4f..6afb8f2 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,7 @@ See [`examples/`](examples/) for complete demos using three.js with SDL. - WebGL-specific pixel store params (`UNPACK_FLIP_Y_WEBGL`, `UNPACK_PREMULTIPLY_ALPHA_WEBGL`) are tracked in JS - `getParameter` returns proper WebGL wrapper objects for binding queries - `getUniform` introspects the uniform type to return the correct typed array +- `texImage2D` and `texSubImage2D` accept the Image/Canvas source form; pixels are read back via the source's `getContext('2d')` (or a temp canvas for an `Image`), so the source must expose a 2D context ## License diff --git a/examples/threejs-ocean/index.mjs b/examples/threejs-ocean/index.mjs index 42fc368..6f67fb6 100644 --- a/examples/threejs-ocean/index.mjs +++ b/examples/threejs-ocean/index.mjs @@ -203,8 +203,8 @@ scene.add(crate) const torusGeo = new THREE.TorusKnotGeometry(12, 3.5, 128, 32) const torusMat = new THREE.MeshPhongMaterial({ - color: 0xcc8844, - specular: 0xffffcc, + color: 0x8833cc, + specular: 0xeeccff, shininess: 300, }) const torusKnot = new THREE.Mesh(torusGeo, torusMat) diff --git a/examples/threejs-ocean/package.json b/examples/threejs-ocean/package.json index ccd449e..37114ba 100644 --- a/examples/threejs-ocean/package.json +++ b/examples/threejs-ocean/package.json @@ -9,9 +9,9 @@ "start": "node index.mjs" }, "dependencies": { - "@kmamal/sdl": "^0.11.0", - "sharp": "^0.33.0", - "three": "^0.170.0", - "webgl-node": "^1.1.0" + "@kmamal/sdl": "^0.11.13", + "sharp": "^0.35.2", + "three": "^0.184.0", + "webgl-node": "^1.2.0" } } diff --git a/examples/threejs-simple/package.json b/examples/threejs-simple/package.json index 7d6a9f6..61e20f1 100644 --- a/examples/threejs-simple/package.json +++ b/examples/threejs-simple/package.json @@ -9,8 +9,8 @@ "start": "node index.mjs" }, "dependencies": { - "@kmamal/sdl": "^0.11.0", - "three": "^0.170.0", - "webgl-node": "^1.1.0" + "@kmamal/sdl": "^0.11.13", + "three": "^0.184.0", + "webgl-node": "^1.2.0" } } diff --git a/lib/webgl2-context.mjs b/lib/webgl2-context.mjs index be61538..bb78390 100644 --- a/lib/webgl2-context.mjs +++ b/lib/webgl2-context.mjs @@ -501,6 +501,9 @@ export class WebGL2RenderingContext { } texSubImage2D(target, level, xoffset, yoffset, ...rest) { + // 7-arg: texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels/pboOffset) + // 5-arg: texSubImage2D(target, level, xoffset, yoffset, format, type, source) — source is Image/Canvas + // 8-arg: texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels, srcOffset) if (rest.length >= 4) { const [width, height, format, type, pixels, srcOffset] = rest if (typeof pixels === 'number') { @@ -513,6 +516,31 @@ export class WebGL2RenderingContext { } this._gl.glTexSubImage2D(target, level, xoffset, yoffset, width, height, format, type, toUint8(data)) } + } else if (rest.length === 3) { + // 5-arg form: (target, level, xoffset, yoffset, format, type, source) + const [format, type, source] = rest + if (source && typeof source === 'object' && source.width && source.height) { + // Extract pixel data from Image or Canvas via a temp 2D canvas + const w = source.width + const h = source.height + let pixels + if (source.getContext) { + // It's a canvas — get pixels directly + const ctx = source.getContext('2d') + pixels = new Uint8Array(ctx.getImageData(0, 0, w, h).data) + } else if (typeof globalThis.document !== 'undefined' && globalThis.document.createElement) { + // It's an Image — draw to a temp canvas + const tmp = globalThis.document.createElement('canvas') + tmp.width = w + tmp.height = h + const ctx = tmp.getContext('2d') + ctx.drawImage(source, 0, 0) + pixels = new Uint8Array(ctx.getImageData(0, 0, w, h).data) + } + if (pixels) { + this._gl.glTexSubImage2D(target, level, xoffset, yoffset, w, h, format, type, pixels) + } + } } } @@ -827,7 +855,7 @@ export class WebGL2RenderingContext { isTransformFeedback(tf) { return tf ? this._gl.glIsTransformFeedback(tf._id) : false } transformFeedbackVaryings(prog, varyings, bufferMode) { - this._gl.glTransformFeedbackVaryings(prog._id, varyings.length, varyings, bufferMode) + this._gl.glTransformFeedbackVaryings(prog._id, varyings, bufferMode) } getTransformFeedbackVarying(prog, index) { diff --git a/package.json b/package.json index 7900d45..6b898c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webgl-node", - "version": "1.1.0", + "version": "1.2.0", "description": "WebGL2 implementation for Node.js on top of native-gles", "type": "module", "main": "index.mjs", From f026ea592c4f161368e9d4dee6ea6e7e7f436895 Mon Sep 17 00:00:00 2001 From: Luis Montes Date: Tue, 23 Jun 2026 08:37:39 -0700 Subject: [PATCH 2/2] additional test --- test/test-image-source.mjs | 119 +++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 test/test-image-source.mjs diff --git a/test/test-image-source.mjs b/test/test-image-source.mjs new file mode 100644 index 0000000..4fac3e6 --- /dev/null +++ b/test/test-image-source.mjs @@ -0,0 +1,119 @@ +// Demonstrates the GH issue #1 fix: texImage2D AND texSubImage2D accept the +// Image/Canvas "source" form. We upload a 2x2 canvas, overwrite one texel via +// texSubImage2D(source), then render the texture full-screen and read it back +// so the result is visible and verifiable end-to-end on the real GL driver. +import { createWebGL2Context } from '../index.mjs' + +const W = 64, H = 64 +const { gl } = createWebGL2Context(W, H) + +let pass = 0, fail = 0 +const check = (cond, msg) => { + if (cond) { pass++; console.log(' PASS:', msg) } + else { fail++; console.error(' FAIL:', msg) } +} + +// A minimal "canvas" source: width/height + getContext('2d').getImageData(). +// This is exactly the shape three.js / browser code passes to texImage2D. +function canvasSource(w, h, rgbaPerTexel) { + const data = new Uint8ClampedArray(w * h * 4) + for (let i = 0; i < w * h; i++) data.set(rgbaPerTexel[i], i * 4) + return { + width: w, + height: h, + getContext: () => ({ getImageData: () => ({ data, width: w, height: h }) }), + } +} + +const R = [255, 0, 0, 255] +const G = [0, 255, 0, 255] +const B = [0, 0, 255, 255] +const Y = [255, 255, 0, 255] + +console.log('-- texImage2D(canvas source) --') +const tex = gl.createTexture() +gl.bindTexture(gl.TEXTURE_2D, tex) +gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1) +gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) +gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) +gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) +gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE) + +// 2x2 texture, bottom row [R, G], top row [B, B], uploaded from a "canvas". +gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, + canvasSource(2, 2, [R, G, B, B])) +check(gl.getError() === gl.NO_ERROR, 'texImage2D(source) uploaded with no GL error') + +console.log('-- texSubImage2D(canvas source) --') +// Overwrite the top-left texel (0,1) with yellow, from a 1x1 "canvas". +// Pre-fix this threw / no-op'd because the source form was unsupported. +gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 1, gl.RGBA, gl.UNSIGNED_BYTE, + canvasSource(1, 1, [Y])) +check(gl.getError() === gl.NO_ERROR, 'texSubImage2D(source) sub-uploaded with no GL error') + +// --- Render the texture full-screen so we can read back actual texels --- +const vs = `#version 300 es +in vec2 a_pos; +out vec2 v_uv; +void main() { v_uv = a_pos * 0.5 + 0.5; gl_Position = vec4(a_pos, 0.0, 1.0); }` +const fs = `#version 300 es +precision highp float; +in vec2 v_uv; +uniform sampler2D u_tex; +out vec4 fragColor; +void main() { fragColor = texture(u_tex, v_uv); }` + +function compile(type, src) { + const s = gl.createShader(type) + gl.shaderSource(s, src); gl.compileShader(s) + if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(s)) + return s +} +const prog = gl.createProgram() +gl.attachShader(prog, compile(gl.VERTEX_SHADER, vs)) +gl.attachShader(prog, compile(gl.FRAGMENT_SHADER, fs)) +gl.linkProgram(prog) +if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(prog)) +gl.useProgram(prog) + +const quad = new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]) +const vbo = gl.createBuffer() +gl.bindBuffer(gl.ARRAY_BUFFER, vbo) +gl.bufferData(gl.ARRAY_BUFFER, quad, gl.STATIC_DRAW) +const loc = gl.getAttribLocation(prog, 'a_pos') +gl.enableVertexAttribArray(loc) +gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0) +gl.uniform1i(gl.getUniformLocation(prog, 'u_tex'), 0) + +gl.viewport(0, 0, W, H) +gl.clearColor(0, 0, 0, 1) +gl.clear(gl.COLOR_BUFFER_BIT) +gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4) + +const px = new Uint8Array(W * H * 4) +gl.readPixels(0, 0, W, H, gl.RGBA, gl.UNSIGNED_BYTE, px) + +// Sample the four quadrants (readPixels origin is bottom-left). +const sample = (fx, fy) => { + const x = Math.floor(fx * W), y = Math.floor(fy * H) + const o = (y * W + x) * 4 + return [px[o], px[o + 1], px[o + 2], px[o + 3]] +} +const eq = (a, b) => a.every((v, i) => Math.abs(v - b[i]) <= 1) + +console.log('-- readback (texture rendered to framebuffer) --') +const bl = sample(0.25, 0.25) // bottom-left -> texel (0,0) = R +const br = sample(0.75, 0.25) // bottom-right -> texel (1,0) = G +const tl = sample(0.25, 0.75) // top-left -> texel (0,1), overwritten to Y +const tr = sample(0.75, 0.75) // top-right -> texel (1,1) = B +console.log(` bottom-left rgba(${bl}) expected red ${R}`) +console.log(` bottom-right rgba(${br}) expected green ${G}`) +console.log(` top-left rgba(${tl}) expected yellow ${Y} (from texSubImage2D source)`) +console.log(` top-right rgba(${tr}) expected blue ${B}`) +check(eq(bl, R), 'bottom-left is red (texImage2D source, texel 0,0)') +check(eq(br, G), 'bottom-right is green (texImage2D source, texel 1,0)') +check(eq(tl, Y), 'top-left is yellow (texSubImage2D source overwrote texel 0,1)') +check(eq(tr, B), 'top-right is blue (texImage2D source, texel 1,1)') + +console.log(`\n=== Results: ${pass} passed, ${fail} failed ===`) +process.exit(fail ? 1 : 0)