我已经实现了一个CPU代码,将一个投影纹理复制到3D对象上的一个更大的纹理中,如果您愿意,可以称之为“贴花烘焙”,但现在我需要在GPU上实现它。为此,我希望使用计算着色器,因为在我的当前设置中添加FBO相当困难。
我当前实现的示例图像
这个问题更多地涉及如何使用计算着色器,但对于任何感兴趣的人,这个想法基于用户jozxyqk的答案,可以在这里看到:https://stackoverflow.com/a/27124029/2579996
在我的代码中,被写入的纹理称为
然后在渲染时我看到了这个:
_texture
,而被投影的纹理称为_textureProj
简单的计算着色器
const char *csSrc[] = {
"#version 440\n",
"layout (binding = 0, rgba32f) uniform image2D destTex;\
layout (local_size_x = 16, local_size_y = 16) in;\
void main() {\
ivec2 storePos = ivec2(gl_GlobalInvocationID.xy);\
imageStore(destTex, storePos, vec4(0.0,0.0,1.0,1.0));\
}"
};
如您所见,我目前只想将纹理更新为一些任意的(蓝色)颜色。
更新函数
void updateTex(){
glUseProgram(_computeShader);
const GLint location = glGetUniformLocation(_computeShader, "destTex");
if (location == -1){
printf("Could not locate uniform location for texture in CS");
}
// bind texture
glUniform1i(location, 0);
glBindImageTexture(0, *_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
// ^second param returns the GLint id - that im sure of.
glDispatchCompute(_texture->width() / 16, _texture->height() / 16, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
glUseProgram(0);
printOpenGLError(); // reports no errors.
}
问题
如果我在主程序对象之外调用updateTex()
,我看不到任何效果,但是如果我在其作用域内调用它,就像这样:
glUseProgram(_id); // vert, frag shader pipe
updateTex();
// Pass uniforms to shader
// bind _textureProj & _texture (latter is the one im trying to update)
glUseProgram(0);
然后在渲染时我看到了这个:
问题: 我意识到在主程序对象范围内设置更新方法并不是正确的方法,但这是获得任何视觉结果的唯一方法。我觉得发生的事情是它基本上消除了片段着色器并绘制到屏幕空间中...
我该怎么做才能使其正常工作?(我的主要关注点是能够写入任何内容到纹理并进行更新)
如果需要发布更多代码,请告诉我。
_programObject->activate();
是做什么的? - GuyRT