只有第一个计算着色器数组元素显示更新 [英] Only first Compute Shader array element appears updated

查看:308
本文介绍了只有第一个计算着色器数组元素显示更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试向计算着色器发送一个整数数组,为每个整数设置一个任意值,然后读回CPU / HOST。问题是,只有我的数组的第一个元素得到更新。我的数组在CPU中所有元素= 5初始化,然后我尝试在Compute着色器中将所有值设置为2:

Trying to send an array of integer to a compute shader, sets an arbitrary value to each integer and then reads back on CPU/HOST. The problem is that only the first element of my array gets updated. My array is initialized with all elements = 5 in the CPU, then I try to sets all the values to 2 in the Compute Shader:

C ++代码:

   this->numOfElements = std::vector<int> numOfElements; //num of elements for each voxel

   //Set the reset grid program as current program
    glUseProgram(this->resetGridProgHandle);

    //Binds and fill the buffer 
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, this->counterBufferHandle);
    glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(int) * numOfVoxels, this->numOfElements.data(), GL_DYNAMIC_DRAW);
    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, this->counterBufferHandle);

    //Flag used in the buffer map function
    GLint bufMask = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT;

    //calc maximum size for workgroups
    //glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_SIZE, &result);

    //Executes the compute shader
    glDispatchCompute(32, 1, 1); // 
    glMemoryBarrier(GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT);

    //Gets a pointer to the returned data
    int* returnArray = (int *)glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_WRITE);

    //Free the buffer mapping
    glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);

着色器:

#version 430
layout (local_size_x = 32) in;
layout(binding = 0) buffer SSBO{
    int counter[];
};

void main(){
    counter[gl_WorkGroupID.x * gl_WorkGroupSize.x + gl_LocalInvocationID.x] = 2;
}



如果我打印returnArray [0]它是2 > 0给我5,这是在主机中初始化的初始值。

If I print returnArray[0] it's 2 (correct), but any index > 0 gives me 5, which is the initial value initialized in host.

推荐答案

glMemoryBarrier(GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT);

//Gets a pointer to the returned data
int* returnArray = (int *)glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_WRITE);

您用于 glMemoryBarrier 表示您希望读取着色器写入的数据的方式。 GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT 说我要使用顶点属性数组的缓冲区读取这个写入的数据。实际上,你要通过映射读取缓冲区。

The bit you use for glMemoryBarrier represents the way you want to read the data written by the shader. GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT says "I'm going to read this written data by using the buffer for vertex attribute arrays". In reality, you are going to read the buffer by mapping it.

因此,你应该使用正确的屏障位:

So you should use the proper barrier bit:

glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);

这篇关于只有第一个计算着色器数组元素显示更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆