Skip to content

Instantly share code, notes, and snippets.

@greggman
Created June 12, 2024 18:08
Show Gist options
  • Save greggman/b0dea77391e1c3d3c545349313aed9aa to your computer and use it in GitHub Desktop.
Save greggman/b0dea77391e1c3d3c545349313aed9aa to your computer and use it in GitHub Desktop.
WebGL: test trying to render to layer of 2D texture while reading it (fails as expected)

WebGL: test trying to render to layer of 2D texture while reading it (fails as expected)

view on jsgist

/*bug-in-github-api-content-can-not-be-empty*/
/*bug-in-github-api-content-can-not-be-empty*/
import 'https://greggman.github.io/webgl-lint/webgl-lint.js';
import * as twgl from 'https://twgljs.org/dist/5.x/twgl-full.module.js';
/** @type WebGL2RenderingContext */
const gl = new OffscreenCanvas(1, 1).getContext('webgl2');
const tex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D_ARRAY, tex);
gl.texStorage3D(gl.TEXTURE_2D_ARRAY, 1, gl.RGBA8, 1, 1, 2);
gl.texSubImage3D(gl.TEXTURE_2D_ARRAY, 0, 0, 0, 0, 1, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([64, 128, 255, 192]));
const vs = `#version 300 es
void main() {
gl_PointSize = 1.0;
gl_Position = vec4(0, 0, 0, 1);
}
`;
const fs = `#version 300 es
precision highp float;
uniform highp sampler2DArray tex;
out vec4 fragColor;
void main() {
fragColor = texture(tex, vec3(0,0,0)).bgra;
}
`;
const prg = twgl.createProgram(gl, [vs, fs]);
gl.useProgram(prg);
const fb = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
gl.framebufferTextureLayer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, tex, 0, 1);
gl.drawArrays(gl.POINTS, 0, 1);
const pixel = new Uint8Array(4);
gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
console.log(pixel);
{"name":"WebGL: test trying to render to layer of 2D texture while reading it (fails as expected)","settings":{},"filenames":["index.html","index.css","index.js"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment