Hal Gee
Hal Gee

Reputation: 105

OpenGL compute shader not putting value in uniform buffer

I'm trying to make a compute shader for computing texture samples. For now I just want it to sample a single point in a texture, and later on I will try to have it do so for an array of vectors. While testing this, I found that the shader doesn't seem to be setting the value I'm using as the output.

Shader:

#version 430

layout(local_size_x = 1) in;
layout(std430, binding = 1) buffer samplePoints {
    vec2 pos;
};
layout(std430, binding = 2) buffer samples {
    float samp;
};

uniform sampler2D tex;

void main() {
    //samp = texture(tex, pos).r;
    samp = 7.0f;
}

Setting samp to 7 is a test. I run the shader with PyOpenGL, the relevant part being:

shader = GL.glCreateShader(GL.GL_COMPUTE_SHADER)
GL.glShaderSource(shader, open("test.glsl").read())
GL.glCompileShader(shader)

program = GL.glCreateProgram()
GL.glAttachShader(program, shader)
GL.glLinkProgram(program)

points, samples = GL.glGenBuffers(2)
GL.glBindBuffer(GL.GL_UNIFORM_BUFFER, points)
GL.glBufferData(GL.GL_UNIFORM_BUFFER, 8, b"\0\0\0\0\0\0\0\0", GL.GL_STATIC_DRAW)
GL.glBindBuffer(GL.GL_UNIFORM_BUFFER, samples)
GL.glBufferData(GL.GL_UNIFORM_BUFFER, 4, b"\0\0\0\0", GL.GL_STATIC_DRAW)

GL.glUseProgram(program)
GL.glBindBufferBase(GL.GL_UNIFORM_BUFFER, 1, points)
GL.glBindBufferBase(GL.GL_UNIFORM_BUFFER, 2, samples)
GL.glDispatchCompute(1, 1, 1)

GL.glBindBuffer(GL.GL_UNIFORM_BUFFER, samples)
a = GL.glGetBufferSubData(GL.GL_UNIFORM_BUFFER, 0, 4).view("<f4")

print(a)

This results in just printing the float made from the 4 bytes I placed in the samples buffer earlier. 0 in this case. I've omitted various bits of error-checking, none of which report any errors along the way.

Where am I going wrong?

Upvotes: 0

Views: 915

Answers (1)

mystery
mystery

Reputation: 19513

That looks like a storage buffer, not a uniform buffer, so wouldn't you need to use GL_SHADER_STORAGE_BUFFER? Also you need to use glMemoryBarrier before accessing the contents.

Upvotes: 0

Related Questions