为什么计算着色器不进行计算?
Why doesn't the compute shader do the calculation?
我正在试验计算着色器。我想要做的是将 arr1 的数据发送到计算着色器变量 shader_arr1[],将其所有元素设为 1,然后将该结果读回 arr2[] 变量中的 CPU 端。然而,运行下面的程序我在arr2[]中得到了相同的arr1[]{1,2,3,4,5,6,7}初始值,没有任何改变。你能告诉我哪里做错了吗?
GLuint program = glCreateProgram();
GLuint computeShader = glCreateShader(GL_COMPUTE_SHADER);
const GLchar* const shaderSrc = {
"#version 310 es\n"
"\n"
"layout (local_size_x = 1) in;\n"
"layout(std430, binding = 0) writeonly buffer SSBO {\n"
" int shader_arr1[];\n"
"};\n"
"void main(void)\n"
"{\n"
" shader_arr1[gl_GlobalInvocationID.x] = 1;\n"
"}\n"
};
glShaderSource(computeShader, 1, &shaderSrc, NULL);
glCompileShader(computeShader);
int result;
glGetShaderiv(computeShader, GL_COMPILE_STATUS, &result);
if(result == GL_FALSE){
int length;
glGetShaderiv(computeShader, GL_INFO_LOG_LENGTH, &length);
char* message = static_cast<char*>(malloc(length));
glGetShaderInfoLog(computeShader, length, &length, message);
__android_log_print(ANDROID_LOG_INFO, "MyLog", "Shader Compile Error: %s", message);
free(message);
}
GL_CALL(glAttachShader(program, computeShader));
GL_CALL(glLinkProgram(program));
GL_CALL(glValidateProgram(program));
GL_CALL(glUseProgram(program));
GLuint buff1Id, buff2Id;
glGenBuffers(1, &buff1Id);
glGenBuffers(1, &buff2Id);
GLint arr1[7] = {1, 2, 3, 4, 5, 6, 7};
GLint arr2[7];
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff1Id);
//glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff2Id);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER,0, buff1Id);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(arr1), arr1, GL_DYNAMIC_DRAW);
//glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(arr2), arr2, GL_DYNAMIC_DRAW);
GL_CALL(glDispatchCompute(1, 1, 1));
GL_CALL(glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT));
// GL_CALL(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buff2Id));
// GL_CALL(glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff2Id));
GLint* data = static_cast<GLint*>(glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, 7,GL_MAP_READ_BIT));
for(int i = 0; i < 7; ++i) {
arr2[i] = data[i];
__android_log_print(ANDROID_LOG_INFO, "MyLog", "Num %d", arr2[i]);
}
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
我修改了代码。这是程序的工作版本
GLuint program = glCreateProgram();
GLuint computeShader = glCreateShader(GL_COMPUTE_SHADER);
const GLchar* const shaderSrc = {
"#version 310 es\n"
"\n"
"layout (local_size_x = 1) in;\n"
"layout(std430, binding = 0) writeonly buffer SSBO1 {\n"
" int shader_arr1[];\n"
"};\n"
"void main(void)\n"
"{\n"
" shader_arr1[gl_GlobalInvocationID.x] = 1;\n"
"}\n"
};
glShaderSource(computeShader, 1, &shaderSrc, NULL);
glCompileShader(computeShader);
int result;
glGetShaderiv(computeShader, GL_COMPILE_STATUS, &result);
if(result == GL_FALSE){
int length;
glGetShaderiv(computeShader, GL_INFO_LOG_LENGTH, &length);
char* message = static_cast<char*>(malloc(length));
glGetShaderInfoLog(computeShader, length, &length, message);
__android_log_print(ANDROID_LOG_INFO, "MyLog", "Shader Compile Error: %s", message);
free(message);
}
GL_CALL(glAttachShader(program, computeShader));
GL_CALL(glLinkProgram(program));
GL_CALL(glValidateProgram(program));
GL_CALL(glUseProgram(program));
GLuint buff1Id, buff2Id;
GL_CALL(glGenBuffers(1, &buff1Id));
GL_CALL(glGenBuffers(1, &buff2Id));
GLint arr1[7] = {1, 2, 3, 4, 5, 6, 7};
GLint arr2[7];
GL_CALL(glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff1Id));
GL_CALL(glBindBufferBase(GL_SHADER_STORAGE_BUFFER,0, buff1Id));
GL_CALL(glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(arr1), arr1, GL_DYNAMIC_DRAW));
GL_CALL(glDispatchCompute(7, 1, 1));
GL_CALL(glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT));
GL_CALL(glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0));
GL_CALL(glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff1Id));
GL_CALL(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buff1Id));
GLint* data = static_cast<GLint*>(glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(GLint) * 7,GL_MAP_READ_BIT));
for(int i = 0; i < 7; ++i) {
arr2[i] = data[i];
__android_log_print(ANDROID_LOG_INFO, "MyLog", "Num %d", arr2[i]);
}
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER
我正在试验计算着色器。我想要做的是将 arr1 的数据发送到计算着色器变量 shader_arr1[],将其所有元素设为 1,然后将该结果读回 arr2[] 变量中的 CPU 端。然而,运行下面的程序我在arr2[]中得到了相同的arr1[]{1,2,3,4,5,6,7}初始值,没有任何改变。你能告诉我哪里做错了吗?
GLuint program = glCreateProgram();
GLuint computeShader = glCreateShader(GL_COMPUTE_SHADER);
const GLchar* const shaderSrc = {
"#version 310 es\n"
"\n"
"layout (local_size_x = 1) in;\n"
"layout(std430, binding = 0) writeonly buffer SSBO {\n"
" int shader_arr1[];\n"
"};\n"
"void main(void)\n"
"{\n"
" shader_arr1[gl_GlobalInvocationID.x] = 1;\n"
"}\n"
};
glShaderSource(computeShader, 1, &shaderSrc, NULL);
glCompileShader(computeShader);
int result;
glGetShaderiv(computeShader, GL_COMPILE_STATUS, &result);
if(result == GL_FALSE){
int length;
glGetShaderiv(computeShader, GL_INFO_LOG_LENGTH, &length);
char* message = static_cast<char*>(malloc(length));
glGetShaderInfoLog(computeShader, length, &length, message);
__android_log_print(ANDROID_LOG_INFO, "MyLog", "Shader Compile Error: %s", message);
free(message);
}
GL_CALL(glAttachShader(program, computeShader));
GL_CALL(glLinkProgram(program));
GL_CALL(glValidateProgram(program));
GL_CALL(glUseProgram(program));
GLuint buff1Id, buff2Id;
glGenBuffers(1, &buff1Id);
glGenBuffers(1, &buff2Id);
GLint arr1[7] = {1, 2, 3, 4, 5, 6, 7};
GLint arr2[7];
glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff1Id);
//glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff2Id);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER,0, buff1Id);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(arr1), arr1, GL_DYNAMIC_DRAW);
//glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(arr2), arr2, GL_DYNAMIC_DRAW);
GL_CALL(glDispatchCompute(1, 1, 1));
GL_CALL(glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT));
// GL_CALL(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buff2Id));
// GL_CALL(glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff2Id));
GLint* data = static_cast<GLint*>(glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, 7,GL_MAP_READ_BIT));
for(int i = 0; i < 7; ++i) {
arr2[i] = data[i];
__android_log_print(ANDROID_LOG_INFO, "MyLog", "Num %d", arr2[i]);
}
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
我修改了代码。这是程序的工作版本
GLuint program = glCreateProgram();
GLuint computeShader = glCreateShader(GL_COMPUTE_SHADER);
const GLchar* const shaderSrc = {
"#version 310 es\n"
"\n"
"layout (local_size_x = 1) in;\n"
"layout(std430, binding = 0) writeonly buffer SSBO1 {\n"
" int shader_arr1[];\n"
"};\n"
"void main(void)\n"
"{\n"
" shader_arr1[gl_GlobalInvocationID.x] = 1;\n"
"}\n"
};
glShaderSource(computeShader, 1, &shaderSrc, NULL);
glCompileShader(computeShader);
int result;
glGetShaderiv(computeShader, GL_COMPILE_STATUS, &result);
if(result == GL_FALSE){
int length;
glGetShaderiv(computeShader, GL_INFO_LOG_LENGTH, &length);
char* message = static_cast<char*>(malloc(length));
glGetShaderInfoLog(computeShader, length, &length, message);
__android_log_print(ANDROID_LOG_INFO, "MyLog", "Shader Compile Error: %s", message);
free(message);
}
GL_CALL(glAttachShader(program, computeShader));
GL_CALL(glLinkProgram(program));
GL_CALL(glValidateProgram(program));
GL_CALL(glUseProgram(program));
GLuint buff1Id, buff2Id;
GL_CALL(glGenBuffers(1, &buff1Id));
GL_CALL(glGenBuffers(1, &buff2Id));
GLint arr1[7] = {1, 2, 3, 4, 5, 6, 7};
GLint arr2[7];
GL_CALL(glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff1Id));
GL_CALL(glBindBufferBase(GL_SHADER_STORAGE_BUFFER,0, buff1Id));
GL_CALL(glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(arr1), arr1, GL_DYNAMIC_DRAW));
GL_CALL(glDispatchCompute(7, 1, 1));
GL_CALL(glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT));
GL_CALL(glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0));
GL_CALL(glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff1Id));
GL_CALL(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buff1Id));
GLint* data = static_cast<GLint*>(glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(GLint) * 7,GL_MAP_READ_BIT));
for(int i = 0; i < 7; ++i) {
arr2[i] = data[i];
__android_log_print(ANDROID_LOG_INFO, "MyLog", "Num %d", arr2[i]);
}
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER