GLSL Texture Size
Asked Answered
S

1

8

I have a problem with my fragment shader. I want to get the size of a texture (which is loaded from an image).

I know that it is possible to use textureSize(sampler) to get an ivec2 which contains the texture size. But i don't know why this isn't working (it doesn't compile):

#version 120

uniform sampler2D tex;

float textureSize;
float texelSize;


void main()
{
    textureSize = textureSize(tex).x;//first line
    //textureSize = 512.0;//if i set the above line as comment and use this one the shader compiles.
    texelSize = 1.0 / textureSize;

    vec4 color = texture2D(tex,gl_TexCoord[0].st);
    gl_FragColor = color * gl_Color;
}
Sivan answered 12/9, 2014 at 8:28 Comment(6)
it doesn't compile - what error are you getting? Come on.Shultz
after compiling glGetShader(handle, GL_COMPILE_STATUS) == GL_FALSE returns true. Is there anyway to get more specific info about the error?Sivan
It would be rather hard if there wasn't, won't you agree? The function was called ShaderInfoLog or something like that, and if you have a fairly modern (4.x) system, you should have some version of debug_output extension (which should be preferred, as it's much nicer to use, configurable and gives overall better information).Shultz
The error is: ERROR: 0:11: 'textureSize' : no matching overloaded function found (using implicit conversion) In which version was textureSize() implemented?Sivan
This shows that it was implemented in Version 130: opengl.org/sdk/docs/man/html/textureSize.xhtml But if i change the version it still doesn't work.Sivan
Because the error states that there's no overload, not that there's no function. In this case, you apparently can't ask for a size of sampler2D.Shultz
S
9

Th problem was that my GLSL version was to low (implemented in 1.30) and that i was missing a parameter.

Here the working version:

#version 130

uniform sampler2D tex;

float textureSize;
float texelSize;


void main()
{
    ivec2 textureSize2d = textureSize(tex,0);
    textureSize = float(textureSize2d.x);
    texelSize = 1.0 / textureSize;

    vec4 color = texture2D(tex,gl_TexCoord[0].st);
    gl_FragColor = color * gl_Color;
}
Sivan answered 12/9, 2014 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.