PDA

View Full Version : How can i access a GL_RGBA8 texture buffer object in fragement shader?



vbtang
09-29-2015, 07:56 PM
hi everyone:
i applied a Texture2d with the code below:
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,4096, 4096, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
and then i used it as TBO:
glBindTexture(GL_TEXTURE_2D, 0);
glBindImageTexture(0, textureID, 0, GL_FALSE,0, GL_READ_WRITE, GL_RGBA8UI);
i want to update my this texture in my fragement, every pixels in my texture is updataed by it's former color and the new color. i want to save memory, so i apply the texture in GL_RGBA8.
here is my Fragement shader:

#version 420

in vec4 color_from_vshader;
in vec2 text_coord;
out vec4 out_color;
uniform sampler2D SpriteTex;
uniform float bDraw;
uniform vec3 oldcolor;

layout (binding=0, rgba8) uniform uimage2D out_texture;

void main() {
if(bDraw > 0.0)
{
//out_color = color_from_vshader;
ivec4 orgColor = imageLoad(out_texture, ivec2(gl_FragCoord.xy));
if(color_from_vshader.w>0)
{
imageStore(out_texture, ivec2(gl_FragCoord.xy), ivec4(255*oldcolor,color_from_vshader.w));
}
else if(orgColor.w < 0.1)
{
imageStore(out_texture, ivec2(gl_FragCoord.xy), ivec4(0, 0, 0,0));
}
else
{
orgColor = orgColor - 0.1 * vec4(oldcolor, orgColor.w);
imageStore(out_texture, ivec2(gl_FragCoord.xy), orgColor);
}
}
else
{
out_color = texture(SpriteTex, text_coord);
}
}

in the shader, when i want to update the texture, i will set the "bDraw" to 10.0, and call "glDrawArrays(GL_POINTS, 0, numPoints);".
but when i compile the shader,i got the below error log:

0(10) : error C1318: can't apply layout(rgba8) to image type "uimage2D"
0(10) : error C1315: can't apply layout(rgba8) to non-image
0(10) : error C7596: OpenGL requires image variables declared without using the 'writeonly' qualifier to have a format layout qualifier
0(16) : error C1115: unable to find compatible overloaded function "imageLoad(struct uimage2D_bindless, ivec2)"
0(27) : error C7011: implicit cast from "vec4" to "ivec4"

i know this "0(27) : error C7011: implicit cast from "vec4" to "ivec4"",but i can't understand others. i found some sample code in 《OpenGL Programming Guide》,but the texture is GL_RGBA32F. i canít access a texture in GL_RGBA8.is there anyone can give me some suggestions?

Alfonse Reinheart
09-29-2015, 09:08 PM
0(10) : error C1318: can't apply layout(rgba8) to image type "uimage2D"

This means exactly what it says. The `rgba8` image format qualifier (https://www.opengl.org/wiki/Image_Load_Store#Format_qualifiers) says that it is an unsigned normalized integer texture, with 8-bits-per-channel. `uimage2D` says that the image is a non-normalized unsigned integer image. You cannot have a normalized format applied to a non-normalized image type.

Indeed, you seem to be decidedly indecisive about whether you want the texture to be normalized or unnormalized. You create the texture with the GL_RGBA8 image format (https://www.opengl.org/wiki/Image_Format), which is normalized. But you bind the image of it as GL_RGBA8UI, which is not normalized. Note that this is not technically illegal (https://www.opengl.org/wiki/Image_Load_Store#Format_conversion); it's just odd. The illegal part is what you did in your shader, where you were again indecisive about whether you were accessing the texture with normalized values or not.

Also, it should be noted that read/modify/write operations like this only work if you're very careful with them.

vbtang
09-30-2015, 12:25 AM
This means exactly what it says. The `rgba8` image format qualifier (https://www.opengl.org/wiki/Image_Load_Store#Format_qualifiers) says that it is an unsigned normalized integer texture, with 8-bits-per-channel. `uimage2D` says that the image is a non-normalized unsigned integer image. You cannot have a normalized format applied to a non-normalized image type.

Indeed, you seem to be decidedly indecisive about whether you want the texture to be normalized or unnormalized. You create the texture with the GL_RGBA8 image format (https://www.opengl.org/wiki/Image_Format), which is normalized. But you bind the image of it as GL_RGBA8UI, which is not normalized. Note that this is not technically illegal (https://www.opengl.org/wiki/Image_Load_Store#Format_conversion); it's just odd. The illegal part is what you did in your shader, where you were again indecisive about whether you were accessing the texture with normalized values or not.

Also, it should be noted that read/modify/write operations like this only work if you're very careful with them.

thank you for your answering, i modified my fragement shader and it's complied successfully.But my texture keeps black(it means i didn't draw anything to it?).
here is the fragment shader:
#version 420

in float color_from_vshader;
in vec2 text_coord;
out vec4 out_color;
uniform sampler2D SpriteTex;
uniform float bDraw;
uniform vec3 color;

layout (binding=0, rgba8ui) uniform uimage2D out_texture;

void main() {
if(bDraw > 0.0)
{
uvec4 orgColor = imageLoad(out_texture, ivec2(gl_FragCoord.xy));
if(color_from_vshader>0)
{
imageStore(out_texture, ivec2(gl_FragCoord.xy), uvec4(color_from_vshader * color, color_from_vshader));
}
else if(orgColor.w < 10)
{
imageStore(out_texture, ivec2(gl_FragCoord.xy), uvec4(0, 0, 0, 0));
}
else
{
imageStore(out_texture, ivec2(gl_FragCoord.xy), uvec4(0, 0, 0, 0));
}
}
else
{
out_color = texture(SpriteTex, text_coord);
}
}

vbtang
09-30-2015, 12:28 AM
This means exactly what it says. The `rgba8` image format qualifier (https://www.opengl.org/wiki/Image_Load_Store#Format_qualifiers) says that it is an unsigned normalized integer texture, with 8-bits-per-channel. `uimage2D` says that the image is a non-normalized unsigned integer image. You cannot have a normalized format applied to a non-normalized image type.

Indeed, you seem to be decidedly indecisive about whether you want the texture to be normalized or unnormalized. You create the texture with the GL_RGBA8 image format (https://www.opengl.org/wiki/Image_Format), which is normalized. But you bind the image of it as GL_RGBA8UI, which is not normalized. Note that this is not technically illegal (https://www.opengl.org/wiki/Image_Load_Store#Format_conversion); it's just odd. The illegal part is what you did in your shader, where you were again indecisive about whether you were accessing the texture with normalized values or not.

Also, it should be noted that read/modify/write operations like this only work if you're very careful with them.
i have 4096*4096 points,so every pixel on the texture represents a point