gpt4 book ai didi

opengl - 计算着色器写入纹理

转载 作者:行者123 更新时间:2023-12-04 15:43:42 25 4
gpt4 key购买 nike

我已经实现了将投影纹理复制到 3d 对象上的更大纹理的 CPU 代码,如果你愿意的话,“贴花烘焙”,但现在我需要在 GPU 上实现它。为此,我希望使用计算着色器,因为在我当前的设置中添加 FBO 非常困难。

Example image from my current implementation

这个问题更多的是关于如何使用计算着色器,但对于任何感兴趣的人,这个想法是基于我从用户 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);

然后在渲染我看到这个:



问题:
我意识到在主程序对象范围内设置更新方法不是正确的方法,但它是获得任何视觉结果的唯一方法。在我看来,发生的事情是它几乎消除了片段着色器并绘制到屏幕空间......

我该怎么做才能使它正常工作? (我的主要重点是能够将任何内容写入纹理和更新)

如果需要发布更多代码,请告诉我。

最佳答案

在这种情况下,我相信 FBO会更容易和更快,并会建议改为。但是这个问题本身还是很有效的。

我很惊讶看到一个球体,因为您正在向整个纹理写入蓝色(如果纹理大小不是 16 的倍数,则减去任何边缘位)。我想这是来自其他地方的代码。

无论如何,您的主要问题似乎是能够从设置代码之外的计算着色器写入纹理以进行常规渲染。 我怀疑这与您绑定(bind) destTex 的方式有关。图片 .我不确定你的 TexUnitactivate方法可以,但是要将 GL 纹理绑定(bind)到图像单元,请执行以下操作:

int imageUnitIndex = 0; //something unique
int uniformLocation = glGetUniformLocation(...);
glUniform1i(uniformLocation, imageUnitIndex); //program must be active
glBindImageTexture(imageUnitIndex, textureHandle, ...);

看:
  • https://www.opengl.org/sdk/docs/man/html/glBindImageTexture.xhtml
  • https://www.opengl.org/wiki/Image_Load_Store#Images_in_the_context

  • 最后,当您使用 image2D所以 GL_SHADER_IMAGE_ACCESS_BARRIER_BIT是使用的障碍。 GL_SHADER_STORAGE_BARRIER_BIT适用于 storage buffer objects .

    关于opengl - 计算着色器写入纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494768/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com