Last active
July 24, 2025 20:38
-
-
Save greggman/ffffd6663767a4b4e2048b9a274b9759 to your computer and use it in GitHub Desktop.
WebGL2 EXT_disjoint_timer_query_webgl2 limits test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*bug-in-github-api-content-can-not-be-empty*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*bug-in-github-api-content-can-not-be-empty*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function main() { | |
const gl = new OffscreenCanvas(300, 100).getContext('webgl2');; | |
const prg = createProgram(gl, ` | |
void main() { | |
gl_Position = vec4(0, 0, 0, 1); | |
gl_PointSize = 128.0; | |
} | |
`, ` | |
precision mediump float; | |
void main() { | |
gl_FragColor = vec4(1, 0, 0, 1); | |
} | |
`); | |
gl.useProgram(prg); | |
// Example (1) -- uses beginQuery/endQuery. | |
let ext = gl.getExtension('EXT_disjoint_timer_query_webgl2'); | |
if (!ext) { | |
console.error('need EXT_disjoint_timer_query_webgl2'); | |
} | |
async function makeAndUseQuery() { | |
let query = gl.createQuery(); | |
gl.beginQuery(ext.TIME_ELAPSED_EXT, query); | |
gl.clear(gl.COLOR_BUFFER_BIT); | |
gl.drawArrays(gl.POINTS, 0, 1); | |
gl.endQuery(ext.TIME_ELAPSED_EXT); | |
// ...at some point in the future, after returning control to the browser and being called again: | |
// (Note that this code might be called multiple times) | |
for (;;) { | |
await new Promise(resolve => setTimeout(resolve, 100)); | |
gl.flush(); | |
const available = gl.getQueryParameter(query, gl.QUERY_RESULT_AVAILABLE); | |
const disjoint = gl.getParameter(ext.GPU_DISJOINT_EXT); | |
if (available || disjoint) { | |
break; | |
} | |
} | |
return query; | |
}; | |
const queries = []; | |
for (;;) { | |
const query = await makeAndUseQuery(); | |
const err = gl.getError(); | |
if (err !== 0) { | |
console.log(err); | |
break; | |
} | |
queries.push(query); | |
if (queries.length % 100 == 0) { | |
console.log('queries so far:', queries.length); | |
await new Promise(resolve => setTimeout(resolve, 100)); | |
} | |
} | |
console.log('num queries:', queries.length); | |
} | |
function createShader(gl, type, src) { | |
const shader = gl.createShader(type); | |
gl.shaderSource(shader, src); | |
gl.compileShader(shader); | |
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { | |
throw new Error(gl.getShaderInfoLog(shader)); | |
} | |
return shader; | |
} | |
function createProgram(gl, vs, fs, tf) { | |
const program = gl.createProgram(); | |
gl.attachShader(program, createShader(gl, gl.VERTEX_SHADER, vs)); | |
gl.attachShader(program, createShader(gl, gl.FRAGMENT_SHADER, fs)); | |
if (tf) { | |
gl.transformFeedbackVaryings(program, tf, gl.INTERLEAVED_ATTRIBS); // gl.SEPARATE_ATTRIBS); | |
} | |
gl.linkProgram(program); | |
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) { | |
throw new Error(gl.getProgramInfoLog(program)); | |
} | |
return program; | |
} | |
main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"name":"WebGL2 EXT_disjoint_timer_query_webgl2 limits test","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