CG->GLSL, FilterManager: texpad_x

I’m attempting to port a CG shader of mine to GLSL, and have hit a problem: I’m not sure of how to use the “texpad_x” variable in GLSL.

Specifically, I’m using FilterManager to set up some post-processing effects. Naturally, I want to adjust the texture coordinates to account for the (presumed) padding to a power-of-two texture, as indicated in the manual. In CG this is accomplished via the “texpad_x” variable (where “x” is the name of the texture involved, as given via “setShaderInput”), I believe–and indeed, in CG this seems to work. In GLSL, however, it seems that either I’m doing something incorrectly, or the variable doesn’t exist.

Where am I going wrong?

Here is the vertex shader in which I’m using “texpad”:

#version 120

attribute vec4 p3d_Vertex;
attribute vec4 p3d_MultiTexCoord0;

uniform vec4 texpad_colourTex;

void main()
{
    gl_Position = ftransform();
    gl_TexCoord[0] = p3d_MultiTexCoord0*texpad_colourTex + texpad_colourTex;
}

Note “texpad_colourTex” on the sixth line.

The actual texture, “colourTex”, is declared and used in the fragment shader, which does not use (nor declare) “texpad_colourTex”.

As far as I remember texpad is just “aspect ratio” of texture? How it can help in adjusting? If I understand texture should be itself power-of-two, i.e. it should have U2 x W2 resolution and it’s not related to the texcoords.

If you have a 800x600 window and you render that to a buffer with power-o-2 textures on you will get a 1024x1024 texture with a 800x600 image and some empty space. Texpad helps to crop this to only the used pixels. I think you should be able to supply a texpad value from python vec4(win_y/tex_y, win_x/tex_x, 1.0, 1.0)… or x and y the other way, and make sure you get floats not ints on python 2.x

Or You can assume that all gpu support non-power-of-two textures and don’t mess with texpad.

Ah, you right, I forget about this feature. I’ve tried to implement it in GLSL, and in my case formula looked like this:
Panad

quad.setShaderInput('texpad_tex', Vec2(screen_w/tex_w*0.5, screen_h/tex_h*0.5))

Shader

uniform vec2 texpad_tex;
.....
gl_TexCoord[0].xy = gl_Position.xy * texpad_tex + texpad_tex;

And you should to update texpad when window is changed

Aah, thank you both–that looks about right; I’ll hopefully try it out tomorrow.

It’s a pity that the automatic version doesn’t seem to work in GLSL as it does in CG, but as you’ve pointed out, it’s not too difficult to provide the value oneself.

Just FYI, there’s not a lot of merit to the texture padding system nowadays. It’s pretty safe to just set “textures-power-2 none” in Config.prc if you have a shader-based application.

Really? That should make life easier–thank you! :slight_smile:

According to the well-known 0AD study, 96% of users support non-power-of-two textures:
feedback.wildfiregames.com/repor … wer_of_two

That’s good to know–thank you again. :slight_smile:

(And it’s a mild relief to see that my own card does indeed seem to be on that list–it’s a little old these days, so I wasn’t entirely confident. ^^; )