1
votes

I want to render a unsigned integer texture with fragment shader using following code:

glTexImage2D(GL_TEXTURE_2D, 0, GL_R8UI, width, height, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, data);

and part of the fragment shader code:

#version 330
uniform usampler2D tex;
void main(void){
    vec3 vec_tex;
    vec_tex = (texture(tex), TexCoordOut).r
}

It is written in OpenGL Programming Guide, that if I want to receive integers in shader, then I should use an integer sampler type, an integer internal format, and an integer external format and type. Here is us GL_R8UI as internal format, GL_RED_INTEGER as external format, GL_UNSIGNED_BYTE as data type. I also use usampler2D in shader. But when the program starts to render the file, always got error implicit cast from "int" to "uint". It seems that the texture data is stored as int, and the unsigned sampler can not convert that. But I did use GL_R8UI as internal format, so the texture data should be stored as unsigned. why the unsigned sampler only get signed int? How can I solve this problem?

1
You know that vec3 is floating-point, right? If you want to store the results of fetching from a usampler2D, you should probably use uvec3. Even then, if this is literally your shader it should be generating a more serious error than that. texture (...) requires texture coordinates and TexCoordOut is not even defined.Andon M. Coleman

1 Answers

0
votes

The texture function call is not correct, secondly the texture function returns float values which needs to handled in shader by dividing the RGBA components by 255.0 (as you use GL_R8UI) and return and fragment color output.

uniform usampler2D tex;
out uvec3 OutColor;
void main(void){
    uvec3 vec_tex;
    vec_tex = texture(tex, TexCoordOut)
    OutColor = vec3(float(vec_tex.r)/255, float(vec_tex.g)/255, float(vec_tex.b)/255)
}