How to get texelFetch to read and return ints?

Опубликовано: 01 Июль 2026
на канале: Emrah KAYA
19
0

Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram:   / ky.emrah  

Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!How to get texelFetch to read and return ints?

The function texelFetch is working fine with floats, but not with ints.
texelFetch
This is the working example with floats. Host code:
floats
float Test[5][2] = { 2.0f, 2.0f };

GLuint tex;
GLuint tbo;
glGenBuffers(1, &tbo);
glBindBuffer(GL_TEXTURE_BUFFER, tbo);
glBufferData(GL_TEXTURE_BUFFER, sizeof(Test), Test, GL_STATIC_DRAW);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_BUFFER, tex);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, tbo);

float Test[5][2] = { 2.0f, 2.0f };

GLuint tex;
GLuint tbo;
glGenBuffers(1, &tbo);
glBindBuffer(GL_TEXTURE_BUFFER, tbo);
glBufferData(GL_TEXTURE_BUFFER, sizeof(Test), Test, GL_STATIC_DRAW);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_BUFFER, tex);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, tbo);

Fragment Shader code:
uniform samplerBuffer textureData;

void main()
{
vec2 TexBegin = texelFetch(textureData, 0).xy;

if (TexBegin.x == 2)
{
color = vec4(1.0, 0.0, 0.0, 1.0);
}
else
{
color = vec4(0.0, 0.0, 0.0, 1.0);
}
}

uniform samplerBuffer textureData;

void main()
{
vec2 TexBegin = texelFetch(textureData, 0).xy;

if (TexBegin.x == 2)
{
color = vec4(1.0, 0.0, 0.0, 1.0);
}
else
{
color = vec4(0.0, 0.0, 0.0, 1.0);
}
}

This is with int format, which only returns 0. Host code:
int
int Test[5][2] = { 2, 2 };

GLuint tex;
GLuint tbo;
glGenBuffers(1, &tbo);
glBindBuffer(GL_TEXTURE_BUFFER, tbo);
glBufferData(GL_TEXTURE_BUFFER, sizeof(Test), Test, GL_STATIC_DRAW);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_BUFFER, tex);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32I, tbo);

int Test[5][2] = { 2, 2 };

GLuint tex;
GLuint tbo;
glGenBuffers(1, &tbo);
glBindBuffer(GL_TEXTURE_BUFFER, tbo);
glBufferData(GL_TEXTURE_BUFFER, sizeof(Test), Test, GL_STATIC_DRAW);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_BUFFER, tex);
glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32I, tbo);

Fragment Shader code:
uniform samplerBuffer textureData;

void main()
{
vec2 TexBegin = texelFetch(textureData, 0).xy;

if (TexBegin.x == 2)
{
color = vec4(1.0, 0.0, 0.0, 1.0);
}
else
{
color = vec4(0.0, 0.0, 0.0, 1.0);
}
}

uniform samplerBuffer textureData;

void main()
{
vec2 TexBegin = texelFetch(textureData, 0).xy;

if (TexBegin.x == 2)
{
color = vec4(1.0, 0.0, 0.0, 1.0);
}
else
{
color = vec4(0.0, 0.0, 0.0, 1.0);
}
}

Changing vec2 to ivec2 errors with: "cannot convert float to int".
vec2
ivec2


Tags: c++,opengl,glslSource of the question:
https://stackoverflow.com/questions/7...

Question and source license information:
https://meta.stackexchange.com/help/l...
https://stackoverflow.com/