简体   繁体   中英

Compute Shader not writing to buffer

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 .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM