diff --git a/lib/webgl2-context.mjs b/lib/webgl2-context.mjs index dd23c74..4208083 100644 --- a/lib/webgl2-context.mjs +++ b/lib/webgl2-context.mjs @@ -7,6 +7,13 @@ import { unwrap, toUint8, } from './webgl-objects.mjs' +// Headroom for glGetIntegerv on array pnames that are not special-cased. +// The driver writes as many values as the pname defines and cannot know our +// buffer size, so a scalar-sized buffer corrupts the heap. 64 ints covers +// every fixed-size GL pname (the largest are COMPRESSED_TEXTURE_FORMATS, +// bounded by NUM_COMPRESSED_TEXTURE_FORMATS, and the 4-element rect queries). +const GET_INTEGERV_SCRATCH_INTS = 64 + const _tmp1 = new Uint32Array(1) export class WebGL2RenderingContext { @@ -461,7 +468,19 @@ export class WebGL2RenderingContext { // 6-arg: texImage2D(target, level, internalformat, format, type, source) — source is Image/Canvas // 10-arg: texImage2D(target, level, internalformat, width, height, border, format, type, pixels, srcOffset) if (rest.length >= 5) { - const [width, height, border, format, type, pixels, srcOffset] = rest + let [width, height, border, format, type, pixels, srcOffset] = rest + /* + * WebIDL coerces the numeric arguments, so a browser turns an undefined or + * null width/height into 0 and allocates an empty texture. The native + * binding requires real numbers and throws "A number was expected" + * instead. Allocating an empty texture this way is common: twgl's + * setEmptyTexture() calls exactly like this whenever a texture is created + * with no src and no explicit size. Without the coercion a perfectly + * legal call fails in a way that reads as a texture bug. + */ + width = width | 0 + height = height | 0 + border = border | 0 if (typeof pixels === 'number') { // PBO offset this._gl.glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels) @@ -480,7 +499,12 @@ export class WebGL2RenderingContext { const w = source.width const h = source.height let pixels - if (source.getContext) { + if (source.data && ArrayBuffer.isView(source.data)) { + // ImageData is a valid texImage2D source per the WebGL spec. Its + // pixels are already RGBA in the expected layout, so upload them + // directly; the temp-canvas path below rejects ImageData outright. + pixels = new Uint8Array(source.data.buffer, source.data.byteOffset, source.data.byteLength) + } else 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) @@ -899,13 +923,17 @@ export class WebGL2RenderingContext { getUniformIndices(prog, uniformNames) { const indices = new Uint32Array(uniformNames.length) - this._gl.glGetUniformIndices(prog._id, uniformNames.length, uniformNames, indices) + // native takes (program, namesArray, outIndices) and derives the count from + // the array itself. Passing an extra count shifted every argument along and + // made the addon reject the call with "Invalid argument". + this._gl.glGetUniformIndices(prog._id, uniformNames, indices) return Array.from(indices) } getActiveUniforms(prog, uniformIndices, pname) { const result = new Int32Array(uniformIndices.length) - this._gl.glGetActiveUniformsiv(prog._id, uniformIndices.length, new Uint32Array(uniformIndices), pname, result) + // native takes (program, indices, pname, outParams), again no count arg. + this._gl.glGetActiveUniformsiv(prog._id, new Uint32Array(uniformIndices), pname, result) if (pname === GL.UNIFORM_IS_ROW_MAJOR) { return Array.from(result, v => !!v) } @@ -1070,12 +1098,36 @@ export class WebGL2RenderingContext { return v } - // Default: integer scalar - const buf = new Int32Array(1) + // Default: integer scalar. + // + // glGetIntegerv writes however many values the pname has, and the driver + // does not know how big our buffer is. A one-element buffer is therefore + // a heap overflow for any ARRAY pname we have not special-cased above -- + // GL_COMPRESSED_TEXTURE_FORMATS reports 51 entries on a desktop Mesa + // stack, which is a ~200 byte write past the end. The corruption lands in + // whatever allocated next (V8, typically), so it surfaces as an unrelated + // crash much later and is extremely hard to trace back to here. + // + // Over-allocate and return only the first element. Scalars are unchanged; + // an unlisted array pname costs a slightly larger temporary instead of + // corrupting the heap. Genuinely unbounded queries are special-cased + // above (and COMPRESSED_TEXTURE_FORMATS is bounded by its own + // NUM_COMPRESSED_TEXTURE_FORMATS, which no driver reports above a few + // dozen). + const buf = new Int32Array(GET_INTEGERV_SCRATCH_INTS) this._gl.glGetIntegerv(pname, buf) return buf[0] } + // Returns every value an integer pname writes, for callers that need the + // whole array rather than the first element. + getParameterArray(pname, count) { + const n = Math.max(1, Math.min(count | 0, GET_INTEGERV_SCRATCH_INTS)) + const buf = new Int32Array(GET_INTEGERV_SCRATCH_INTS) + this._gl.glGetIntegerv(pname, buf) + return buf.subarray(0, n) + } + getBufferParameter(target, pname) { if (pname === GL.BUFFER_SIZE) { // Use 64-bit query for large buffers