简体   繁体   English

OpenGL Compute Shader 渲染到 3D 纹理似乎没有做任何事情

[英]OpenGL Compute Shader rendering to 3D texture does not seem to do anything

I have written a small shader to set the color values of a 3D volume.我编写了一个小的着色器来设置 3D 体积的颜色值。

#version 430

layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout (r8, binding = 0) uniform image3D destTex;

void main() 
{
    imageStore(destTex, ivec3(gl_GlobalInvocationID.xyz), vec4(0.33, 0, 1.0, 0.5));
}

It compiles and links without errors.它编译和链接没有错误。

The volume is created and the compute shader is dispatched like this创建卷并像这样调度计算着色器

    generateShader->use();

    glEnable(GL_TEXTURE_3D);
    glGenTextures(1, &densityTex);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_3D, densityTex);

    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

    glTexImage3D(GL_TEXTURE_3D, 0, GL_R8, chunksize + 1, chunksize + 1, chunksize + 1, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
    glBindImageTexture(0, densityTex, 0, GL_TRUE, 0, GL_READ_WRITE, GL_R8);

    glDispatchCompute(chunksize + 1, chunksize + 1, chunksize + 1);
    // make sure writing to image has finished before read
    glMemoryBarrier(GL_ALL_BARRIER_BITS);

asdf(0); //Here I print the content of my texture. 
         //Strangely it contains the values 205 everywhere and independent of what I set in the shader: vec4(0.33, 0, 1.0, 0.5)

    glBindTexture(GL_TEXTURE_3D, 0);

The method for printing the image data looks like this:打印图像数据的方法如下所示:

void Chunk::asdf(int slice) {

    GLubyte* image;
    image = new GLubyte[(chunksize + 1) * (chunksize + 1) * (chunksize + 1)];

    glGetTexImage(GL_TEXTURE_3D, 0, GL_R8, GL_UNSIGNED_BYTE, image);
    std::cout << "Reading texels" << std::endl;
    for (int i = 0; i < (chunksize + 1); i++) {
        for (int j = 0; j < (chunksize + 1); j++) {

            int start = (((chunksize + 1) * (chunksize + 1) * slice) + (i * (chunksize + 1)) + j);
            std::cout << "Texel at " << i << " " << j << " " << slice << " has color " << (float)image[start] << std::endl;
        }
    }

    delete[] image;
}

What is wrong in this code?这段代码有什么问题? Do I need some special method to retrieve the data after setting it on the GPU?在 GPU 上设置数据后,我是否需要一些特殊的方法来检索数据? Also, I dont quite understand how the value is set in the GPU because when using imageStore one has to set a vec4 value, but the texture only contains one channel for red (it does not make a difference if I use a RGBA format)另外,我不太明白如何在 GPU 中设置该值,因为在使用 imageStore 时,必须设置一个 vec4 值,但纹理仅包含一个红色通道(如果我使用 RGBA 格式,则没有区别)

edit: It turns out I didn't get the problem right.编辑:事实证明我没有解决问题。 I wrote myself a shader to render the content of my 3D texture to the screen.我自己写了一个着色器来将我的 3D 纹理的内容渲染到屏幕上。 It seems my print-function (asdf) does not get the right values of the texture because if rendered to the screen the color values are different似乎我的打印功能(asdf)没有得到正确的纹理值,因为如果渲染到屏幕上,颜色值是不同的渲染到屏幕的 1 片纹理

as you can see it still doesn't look quite right, but I assume this is a topic fo another issue?如您所见,它看起来仍然不太正确,但我认为这是另一个问题的主题?

glGetTexImage(GL_TEXTURE_3D, 0, GL_R8, GL_UNSIGNED_BYTE, image);

That will not copy any data to image , but will only generate a GL_INVALID_ENUM error.这不会将任何数据复制到image ,而只会生成GL_INVALID_ENUM错误。 GL_R8 is just not a vaild format parameter. GL_R8只是不是一个有效的format参数。 You most likely mean GL_RED here (note this is the same you used for glTexImage3D 's format parameter. GL_R8 is only an enum for internalFormat , and it is never relating to anything you see on OpenGL client side, see also this question ).您很可能在这里指的是GL_RED (请注意,这与您用于glTexImage3Dformat参数GL_R8只是internalFormat的枚举,它永远不会与您在 OpenGL 客户端看到的任何内容相关,另请参阅此问题)。

As a more general recommendation, I really advice everyone to use OpenGL's Debug Output feature during development, so that no OpenGL error will go unnoticed.作为更一般的建议,我真的建议大家在开发过程中使用OpenGL 的调试输出功能,这样就不会忽视 OpenGL 错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM