# webgl2.0 how I can get all uniforms except uniformBuffer item

when I write uniformBuffer in shader like this.

``````uniform Material {
uniform vec4 u_DiffuseColor;
uniform vec4 u_TilingOffset;
uniform vec3 u_MaterialSpecular;
uniform float u_AlphaTestValue;
uniform float u_Shininess;
};
``````

gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS) and gl.getActiveUniform(program, i) still contaion uniformBuffer item( for example u_DiffuseColor,it's redundant!), the only one thing I found is gl.getUniformLocation(program, uniName) will return null.

is there another better way i can get uniforms not include uniformBuffer item,because the i should dispose them with two different way.

How can I find a list of all the uniforms in OpenGL es 2.0 vertex shader pro Maybe this is not best way when have uniformBuffer block in shader with opengles3.0

• thank you for your reply. it is webgl2.0. I fixed the question – gl3336563163com Mar 30 at 16:28

Given some list of active uniform indices, you can query the value of a property for each item in that list with `getActiveUniforms`. Among the queryable properties is `UNIFORM_BLOCK_INDEX`, which is the index of the uniform block the uniform is a member of. If it's not a member of any block, the value will be -1.

So basically, you build a list of all the uniform indices (0 to `ACTIVE_UNIFORMS` - 1), query their block index, and then query the properties only for the indices whose block index is -1.

Alternatively, you could query, for each active block (0 to `ACTIVE_UNIFORM_BLOCKS` - 1), the list of uniform indices that are a member of that block with `getActiveUniformBlockParameter`, passing `UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES` as the property to be queried. Any uniform index not in one of these lists is therefore not a member of a block.

You can get all uniforms and which blocks they are in by calling `gl.getActiveUniform` and `gl.getActiveUniforms`. `gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS)` returns the number of uniforms. You then create an array of indices to pass to `gl.getActiveUniforms` and pass it `gl.UNIFORM_BLOCK_INDEX`. -1 = not in a block.

``````const vs = `#version 300 es

// NOTE: We need to mark these as mediump to match
// the fragment shader (or of course we could mark
// the fragment shader's uniform block to highp)
//
layout(std140) uniform u_testBlock
{
mediump vec4 foo;
mediump vec3 bar;
};

void main() {
gl_Position = foo;
}
`;
const fs = `#version 300 es
precision mediump float;

layout(std140) uniform u_testBlock
{
vec4 foo;
vec3 bar;
};

uniform vec2 blat;

out vec4 theColor;

void main() {
theColor = vec4(bar + vec3(blat, 0), 1);
}
`;

function main() {
const gl = document.createElement("canvas").getContext("webgl2");
if (!gl) {
return alert("ERROR: need WebGL 2 support");
}
const program = twgl.createProgram(gl, [vs, fs]);

const numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
const indices = [...Array(numUniforms).keys()];
const blockIndices = gl.getActiveUniforms(program, indices, gl.UNIFORM_BLOCK_INDEX);
const offsets = gl.getActiveUniforms(program, indices, gl.UNIFORM_OFFSET);

for (let ii = 0; ii < numUniforms; ++ii) {
const uniformInfo = gl.getActiveUniform(program, ii);
if (isBuiltIn(uniformInfo)) {
continue;
}
const {name, type, size} = uniformInfo;
const blockIndex = blockIndices[ii];
const offset = offsets[ii];
console.log(
name, size, glEnumToString(gl, type),
blockIndex, offset);
}
}

function isBuiltIn(info) {
const name = info.name;
return name.startsWith("gl_") || name.startsWith("webgl_");
}

function glEnumToString(gl, value) {
const keys = [];
for (const key in gl) {
if (gl[key] === value) {
keys.push(key);
}
}
return keys.length ? keys.join(' | ') : `0x\${value.toString(16)}`;
}

main();``````
``<script src="https://twgljs.org/dist/4.x/twgl.min.js"></script>``

Other questions related to using uniform blocks
{"ft_lang_label":"__label__en","ft_lang_prob":0.67775404,"math_prob":0.84835464,"size":772,"snap":"2020-24-2020-29","text_gpt3_token_len":178,"char_repetition_ratio":0.16015625,"word_repetition_ratio":0.0,"special_character_ratio":0.19559586,"punctuation_ratio":0.16176471,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96102184,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-07-06T10:27:20Z\",\"WARC-Record-ID\":\"<urn:uuid:a1ed42ff-b5c2-45aa-ada9-86dbd2c55415>\",\"Content-Length\":\"160428\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fa1fed77-648d-4295-b395-db70d49a9d95>\",\"WARC-Concurrent-To\":\"<urn:uuid:1f0193e0-25f6-4d26-a09f-1a51ce6ecd12>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/60933510/webgl2-0-how-i-can-get-all-uniforms-except-uniformbuffer-item\",\"WARC-Payload-Digest\":\"sha1:6R52P5MDOQ7QV2BRCX6BOB364JU5LPCL\",\"WARC-Block-Digest\":\"sha1:TLIOR6V4NCAFPWZDBPW5BDZDMMTPWJAU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-29/CC-MAIN-2020-29_segments_1593655890157.10_warc_CC-MAIN-20200706073443-20200706103443-00030.warc.gz\"}"}