knackered

11-08-2005, 08:30 AM

Hello,

I'm trying to implement a simple bilinear filter in an arbitary mipmap level in a vertex shader.

At the moment, I pass in as uniforms the dimensions of the texture and an individual texel in the texture. However, at different mipmap levels the texture and texel sizes are different, depending on the level I'm sampling.

Is there any (fast) way I can calculate the real dimensions of the texture (and texel) from an arbitary mipmap level, in a (GLSL) vertex shader? Or will I have to calculate all levels on the CPU and pass them as a uniform array of sizes?

Here's my function at the moment....

vec4 texture2DLod_bilinear(in sampler2D tex,

in vec2 t,

in float lod)

{

int l = int(lod); // ????

vec2 textureSize = uni_textureSize; // ????

vec2 texelSize = uni_texelSize; // ????

vec2 f = fract(t * textureSize);

vec4 t00 = texture2DLod(tex, t, lod);

vec4 t10 = texture2DLod(tex, t + vec2(texelSize.x, 0.0), lod);

vec4 tA = mix(t00, t10, f.x);

vec4 t01 = texture2DLod(tex, t + vec2(0.0, texelSize.y), lod);

vec4 t11 = texture2DLod(tex, t + vec2(texelSize.x, texelSize.y), lod);

vec4 tB = mix(t01, t11, f.x);

return mix(tA, tB, f.y);

}

I'm trying to implement a simple bilinear filter in an arbitary mipmap level in a vertex shader.

At the moment, I pass in as uniforms the dimensions of the texture and an individual texel in the texture. However, at different mipmap levels the texture and texel sizes are different, depending on the level I'm sampling.

Is there any (fast) way I can calculate the real dimensions of the texture (and texel) from an arbitary mipmap level, in a (GLSL) vertex shader? Or will I have to calculate all levels on the CPU and pass them as a uniform array of sizes?

Here's my function at the moment....

vec4 texture2DLod_bilinear(in sampler2D tex,

in vec2 t,

in float lod)

{

int l = int(lod); // ????

vec2 textureSize = uni_textureSize; // ????

vec2 texelSize = uni_texelSize; // ????

vec2 f = fract(t * textureSize);

vec4 t00 = texture2DLod(tex, t, lod);

vec4 t10 = texture2DLod(tex, t + vec2(texelSize.x, 0.0), lod);

vec4 tA = mix(t00, t10, f.x);

vec4 t01 = texture2DLod(tex, t + vec2(0.0, texelSize.y), lod);

vec4 t11 = texture2DLod(tex, t + vec2(texelSize.x, texelSize.y), lod);

vec4 tB = mix(t01, t11, f.x);

return mix(tA, tB, f.y);

}