opengl 4.3计算着色器不将值写入缓冲区

时间:2014-04-19 22:56:31

标签: opengl opengl-4 compute-shader

我对计算着色器有一个奇怪的问题,因为我改变了传入着色器的缓冲区的结构大小。

struct Particle
{
    vec3 position;
    vec2 uv;
    vec3 accumulated_normal;
    int id;
    int flattened_id;
    int movable;
    // can the particle move or not ? used to pin parts of the cloth
    float mass;
    // the mass of the particle (is always 1 in this example)
    vec3 old_pos;
    // the position of the particle in the previous time step, used as part of the verlet numerical integration scheme
    vec3 acceleration;
    // a vector representing the current acceleration of the particle
};

定义如下。我在尝试使用flattened id时遇到了问题,所以我想将id写回我传入的缓冲区。着色器看起来像这样。

layout (local_size_x = 16, local_size_y = 1, local_size_z = 1) in;

void main()
{
    unsigned int flattened_id = gl_LocalInvocationIndex;

    particleBuffer.particles[0].id = 16;
    particleBuffer.particles[1].id = 17;
    particleBuffer.particles[2].id = 18;
    particleBuffer.particles[3].id = 19;
    //particleBuffer.particles[4].id = 20;
}

所以直到这一点它很好但是当我取消注释最后一行是particleBuffer.particles [4]时,网格从屏幕上消失了。我之前设法改变了这个网格中的位置数据,但这看起来很奇怪。我确认我确实传递了16个缓冲区元素,所以它不应该超出界限(即cloth1.particles.size()= 16)。有任何想法吗 ?? 整个代码都在这里.. https://github.com/ssarangi/OpenGL_Cloth/tree/master/cloth_4_3_compute

glUseProgram(computeShader);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, cloth1.vertex_vbo_storage);
glBufferData(GL_SHADER_STORAGE_BUFFER, cloth1.particles.size() * sizeof(Particle), &(cloth1.particles[0]), GL_DYNAMIC_COPY);

glDispatchCompute(1, 1, 1);

{
    GLenum err = gl3wGetError();
}

glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, cloth1.vertex_vbo_storage);
Particle * ptr = reinterpret_cast<Particle *>(glMapBufferRange(GL_ARRAY_BUFFER, 0, cloth1.particles.size() * sizeof(Particle), GL_MAP_READ_BIT));

memcpy(&cloth1.particles[0], ptr, cloth1.particles.size()*sizeof(Particle));
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);

*********************** 编辑Andon的评论 ********** ******************** 这是C ++方面的新布局。

struct Particle
{
    vec4 position;
    vec2 uv;
    vec4 accumulated_normal;
    vec4 old_pos; // the position of the particle in the previous time step, used as part of the verlet numerical integration scheme
    vec4 acceleration; // a vector representing the current acceleration of the particle
    int id;
    int flattened_id;
    int movable; // can the particle move or not ? used to pin parts of the cloth
    float mass; // the mass of the particle (is always 1 in this example)

GLSL方面的定义。我不确定的是,填充元素是否需要包含在glsl结构中。它仍然不会更新ID。

struct Particle
{
    vec4 position;
    vec2 uv;
    vec4 accumulated_normal;
    vec4 old_pos; // the position of the particle in the previous time step, used as part of the verlet numerical integration scheme
    vec4 acceleration; // a vector representing the current acceleration of the particle
    int id;
    int flattened_id;
    int movable; // can the particle move or not ? used to pin parts of the cloth
    float mass; // the mass of the particle (is always 1 in this example)
};

0 个答案:

没有答案