问题
I'm looking for help calling a compute shader from Qt using QOpenGLFunctions_4_3_Core
OpenGL functions.
Specifically, my call to glDispatchCompute(1024, 1, 1);
does not seem to have any effect on the buffer bound to it. How do you bind a buffer to a compute shader in QT such that the results of the shader can be read back to the C++?
I create my program and bind it with (Squircle.cpp):
computeProgram_ = new QOpenGLShaderProgram();
computeProgram_->addShaderFromSourceFile(QOpenGLShader::Compute, "../app/shaders/pointcloud.comp");
computeProgram_->bindAttributeLocation("Particles", 0);
m_ParticlesLoc = 0;
computeProgram_->link();
And then bind my QOpenGLBuffer
with (Squircle.cpp):
// Setup our vertex buffer object.
pointOpenGLBuffer_.create();
pointOpenGLBuffer_.bind();
pointOpenGLBuffer_.allocate(pointBuffer_.data(), pointBuffer_.vertexCount() * pointBuffer_.stride_);
Then I invoke the compute shader with (Squircle.cpp):
computeProgram_->bind();
// ...
pointOpenGLBuffer_.bind();
glDispatchCompute(1024, 1, 1);
But when I read my buffer, either with read()
or by map()
'ing, the values are never changed, they're just what I originally inserted.
From the compute shader perspective, I accept my input with (pointcloud.comp):
#version 430
layout(local_size_x = 1024) in;
struct ParticleData
{
vec4 position;
};
// Particles from previous frame
layout (std430, binding = 0) coherent buffer Particles
{
ParticleData particles[];
} data;
Am I not binding my buffer properly maybe? Or is there another OpenGL command to call to actually dispatch the compute? I've tried different usages, etc.
I've posted all the relevant code here.
回答1:
It seems that problem is in wrong buffer binding understanding.
pointOpenGLBuffer_.bind();
only binds your buffer to your OGL context, not to your shader buffer, calling it twice won't do the trick.
Second time instead of just bind you need to call
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, pointOpenGLBuffer_.bufferId());
where 0 comes from your layout (std430, binding = 0)
来源:https://stackoverflow.com/questions/42444493/compute-shader-not-writing-to-buffer