compute shaders and uniform blocks


Have just started working on compute shaders.
I have a uniform block in a compute shader and I am trying to get the uniform block index in my application , but it is always returning me index as -1. Whereas for normal uniforms (without blocks) it is returning proper location values (+ve values) . I am working on OPENGL 4.3 with GLSL 430 on Nvidia quadro 600.

Compute Shader

#version 430
#extension GL_EXT_shader_image_load_store : enable
layout(rgba32f) uniform image2D destTex;
layout(std140) uniform colors_UB{
	vec4 color1;
	vec4 color2;
	vec4 color3;
	vec4 color4; 

layout (local_size_x = 16, local_size_y = 16, local_size_z = 1) in;
void main() {
		vec4 add_color= vec4(color1  + color2 + color3 + color4); // just to make active
		ivec2 image_new = imageSize(destTex); // just to make active

The compute shader is compiling and linking properly. Also ‘destTex’ uniform is returning location value as 0.

But glGetUniformBlockIndex(compute_program, “colors_UB”); is always returning values as -1.

I am missing anything?


Maybe because of optimization. When the compiler realizes that the uniform is never used in the shader it will not be linked.

What do you mean by uniform never used here?

vec4 add_color= vec4(color1  + color2 + color3 + color4);

Here I am using the uniforms of the uniform block. So, I thought they are active.
Could you please elaborate more.

But ‘add_color’ is not used anywhere. The compiler removes all calculations and references to resources if the results of the calculations are not used. This also includes the ‘colorN’ uniforms.

yes, using ‘add_color’ worked. Thanks :slight_smile: