我想创建一个在Open GL ES中使用多个向量并返回一个向量的程序。
我已经使用example编写了自己的代码。
如果我有以下代码:
static const char COMPUTE_SHADER[] =
"#version 310 es\n"
"layout(local_size_x = 128) in;\n"
"layout(std430) buffer;\n"
"layout(binding = 0) buffer Output {\n"
"uint elements[];\n"
"} output_data;\n"
"void main()\n"
"{\n"
" uint ident = gl_GlobalInvocationID.x;\n"
"output_data.elements[ident] = output_data.elements[ident] * "
"output_data.elements[ident];\n"
"}";
...
GLuint data_buffer;
glGenBuffers(1, &data_buffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, data_buffer);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, data_buffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLuint) * 10, (void*)data,
GL_DYNAMIC_READ);
GLuint program = glCreateProgram();
GLuint shader = LoadShader(COMPUTE_SHADER);
glAttachShader(program, shader);
glLinkProgram(program);
glUseProgram(program);
glDispatchCompute(10, 1, 1);
std::vector<uint32_t> ready_data(10);
GLuint* ptr = (GLuint*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0,
sizeof(GLuint) * 10, GL_MAP_READ_BIT);
std::copy(ptr, ptr + 10, ready_data.begin());
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
它工作得很好,并将我需要的全部存储在输出向量中。
但是,我想遵循该示例并进行单独的输入和输出。而且不起作用:
static const char COMPUTE_SHADER[] =
" #version 310 es"
"layout(local_size_x = 128) in;"
"layout(std430) buffer;"
"layout(binding = 0) buffer Output {"
" uint elements[];"
"} output_data;"
"layout(binding = 1) buffer Input0 {"
" uint elements[];"
"} input_data0;"
"layout(binding = 2) readonly buffer Input1 {"
"uint elements[];"
"} input_data1;"
"void main()"
"{"
" uint ident = gl_GlobalInvocationID.x;"
" output_data.elements[ident] = input_data0.elements[ident] * input_data1.elements[ident];"
"}";
uint32_t data1[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
uint32_t data2[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
uint32_t data3[10] = {11, 21, 31, 41, 51, 61, 71, 81, 91, 101};
glGenBuffers(1, &input_buffer1);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, input_buffer1);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, input_buffer1);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLuint) * 10, (void*)data2,
GL_STREAM_COPY);
glGenBuffers(1, &input_buffer2);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, input_buffer2);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, input_buffer2);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLuint) * 10, (void*)data3,
GL_STREAM_COPY);
glGenBuffers(1, &data_buffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, data_buffer);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, data_buffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GLuint) * 10, (void*)data1,
GL_DYNAMIC_READ);
GLuint program = glCreateProgram();
GLuint shader = LoadShader(COMPUTE_SHADER);
glAttachShader(program, shader);
glLinkProgram(program);
glUseProgram(program);
glDispatchCompute(10, 1, 1);
GLuint* ptr = (GLuint*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0,
sizeof(GLuint) * 10, GL_MAP_READ_BIT);
std::copy(ptr, ptr + 10, ready_data.begin());
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
它只输出数字1 2 3 .. 10
,而我希望它们乘以元素。
答案 0 :(得分:0)
由于缺少换行符,第二个计算着色器甚至无法编译。
计算着色器的前两行
static const char COMPUTE_SHADER[] =
" #version 310 es"
"layout(local_size_x = 128) in;"
.....
导致
#version 310 eslayout(local_size_x = 128) in;
在第一行末尾添加换行符以解决此问题:
static const char COMPUTE_SHADER[] =
"#version 310 es\n"
"layout(local_size_x = 128) in;"
.....
但是我建议使用Raw string literals(C ++ 11):
static const char COMPUTE_SHADER[] = R"(
#version 310 es
layout(local_size_x = 128) in;
layout(std430) buffer;
layout(binding = 0) buffer Output {
uint elements[];
} output_data;
layout(binding = 1) buffer Input0 {
uint elements[];
} input_data0;
layout(binding = 2) readonly buffer Input1 {
uint elements[];
} input_data1;
void main()
{
uint ident = gl_GlobalInvocationID.x;
output_data.elements[ident] = input_data0.elements[ident] * input_data1.elements[ident];
}
)";