![](/img/trans.png)
[英]OpenGL: Can't draw vertices generated by a compute shader stored in SSBO
[英]OpenGL Setting SSBO for Compute Shader
我一直無法將數據發送到 SSBO 以供計算着色器使用。 不幸的是, khronos 文檔說“TODO” ,我不能讓他們的示例代碼工作,而且人們似乎做的事情略有不同示例 1 示例 2 示例 3 - 任何人都可以幫忙嗎?
(我已經剪掉了我寫的其他我認為不相關的代碼部分 - 但整個代碼庫都在這里。這是我到目前為止所得到的:
帶有一些數據的 SSBO 初始化
std::vector<glm::vec4> data = { glm::vec4(1.0, 0.0, 0.0, 1.0), glm::vec4(1.0, 0.0, 0.0, 1.0) };
GLuint SSBO;
glGenBuffers(1, &SSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, data.size() * sizeof(glm::vec4), &data[0], GL_DYNAMIC_DRAW);
//the khronos docs put this line in
//glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, SSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
更新循環
s_Data.compute_shader.use();
-- snip: bind a texture --
int ssbo_binding = 1;
int block_index = glGetProgramResourceIndex(s_Data.compute_shader.ID, GL_SHADER_STORAGE_BLOCK, "bufferData");
glShaderStorageBlockBinding(s_Data.compute_shader.ID, block_index, ssbo_binding );
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ssbo_binding, SSBO);
glDispatchCompute( X workers, Y workers, 1);
//Synchronize all writes to the framebuffer image
glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
// Reset bindings
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ssbo_binding, 0);
glBindImageTexture(0, 0, 0, false, 0, GL_READ_WRITE, GL_RGBA32F);
glUseProgram(0);
-- snip: render output texture to screen --
計算着色器
#version 430 core
layout(binding = 0, rgba16f) uniform image2D outTexture;
layout(std430, binding = 1 ) readonly buffer bufferData
{
vec4 data[];
};
layout (local_size_x = 16, local_size_y = 8) in;
void main(void) {
ivec2 px = ivec2(gl_GlobalInvocationID.xy);
ivec2 size = imageSize(outTexture);
vec3 color;
if(data.length() > 0)
{
//green = data
color = vec3(0.2, 0.6, 0.2);
} else
{
//red = bad
color = vec3(0.6, 0.2, 0.2);
}
imageStore(outTexture, px, vec4(color, 1.0));
}
目前我的屏幕顯示紅色,表示沒有數據正在通過 SSBO 發送。
編輯:
發現問題。 計算着色器中的 .length() 方法不起作用。
我在計算着色器中發現了問題。
The.length() 返回錯誤的值。 我查詢了 data[0] 和 data[1],它們在計算着色器中返回了正確設置的值 - 所以這就是問題所在(但我不一定有解決方案)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.