Created
September 21, 2021 23:01
-
-
Save greggman/ac79e0e9d5a1569843e18519625bd128 to your computer and use it in GitHub Desktop.
ubo of mat2 array (no twgl)
This file contains 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 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 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
import * as twgl from 'https://twgljs.org/dist/4.x/twgl-full.module.js'; | |
const vs = `#version 300 es | |
void main() { | |
gl_Position = vec4(0, 0, 0, 1); | |
gl_PointSize = 1.0; | |
} | |
`; | |
const fs = `#version 300 es | |
precision highp float; | |
out vec4 outColor; | |
layout(std140, column_major) uniform Ubo | |
{ | |
mat2 m2[3]; | |
} ubo; | |
uniform int arrayNdx; | |
uniform int majorNdx; | |
void main() | |
{ | |
outColor = vec4(ubo.m2[arrayNdx][majorNdx], 0, 1) / 255.0; | |
}`; | |
const canvas = document.createElement('canvas'); | |
canvas.width = 1; | |
canvas.height = 1; | |
const gl = canvas.getContext('webgl2'); | |
const programInfo = twgl.createProgramInfo(gl, [vs, fs]); | |
const shaders = gl.getAttachedShaders(programInfo.program); | |
/* | |
const ext = gl.getExtension('WEBGL_debug_shaders'); | |
if (ext) shaders.forEach(s => { | |
console.log(`----------\n${ext.getTranslatedShaderSource(s)}`); | |
}); | |
*/ | |
const program = programInfo.program; | |
gl.useProgram(program); | |
const numUniformBlocks = gl.getProgramParameter(program, gl.ACTIVE_UNIFORM_BLOCKS); | |
for (let ii = 0; ii < numUniformBlocks; ++ii) { | |
const name = gl.getActiveUniformBlockName(program, ii); | |
const blockSpec = { | |
index: gl.getUniformBlockIndex(program, name), | |
usedByVertexShader: gl.getActiveUniformBlockParameter(program, ii, gl.UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER), | |
usedByFragmentShader: gl.getActiveUniformBlockParameter(program, ii, gl.UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER), | |
size: gl.getActiveUniformBlockParameter(program, ii, gl.UNIFORM_BLOCK_DATA_SIZE), | |
uniformIndices: gl.getActiveUniformBlockParameter(program, ii, gl.UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES), | |
}; | |
blockSpec.used = blockSpec.usedByVertexShader || blockSpec.usedByFragmentShader; | |
console.log(ii, JSON.stringify(blockSpec, null, 2)); | |
} | |
const buf = gl.createBuffer(); | |
gl.bindBuffer(gl.UNIFORM_BUFFER, buf); | |
gl.bufferData(gl.UNIFORM_BUFFER, new Float32Array([ | |
1, 2, 3, 201, | |
4, 5, 6, 202, | |
7, 8, 9, 203, | |
10, 11, 12, 204, | |
13, 14, 15, 205, | |
16, 17, 18, | |
19, 20, 21, | |
22, 23, 24, | |
]), gl.STATIC_DRAW); | |
const ndx = gl.getUniformBlockIndex(programInfo.program, "Ubo"); | |
gl.uniformBlockBinding(programInfo.program, ndx, 0); | |
gl.bindBufferBase(gl.UNIFORM_BUFFER, 0, buf); | |
for (let arrayNdx = 0; arrayNdx < 3; ++arrayNdx) { | |
for (let majorNdx = 0; majorNdx < 2; ++majorNdx) { | |
twgl.setUniforms(programInfo, { | |
arrayNdx, | |
majorNdx, | |
}); | |
gl.drawArrays(gl.POINTS, 0, 1); | |
const p = new Uint8Array(4); | |
gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, p); | |
console.log(`m2[${arrayNdx}][${majorNdx}] = ${p}`); | |
} | |
} | |
console.log(gl.getError()); |
This file contains 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":"ubo of mat2 array (no twgl)","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