Skip to main content
gamedev.net
🔒 Locked

Shader - texture issue

Started by razor1911 Jun 29, 2010 at 5:37 AM 2 replies 850+ views
Original Post
razor1911
razor1911
This is the shader i am using to satisfy the needs of a final scene colors,
tex1 - 1024x768 texture obtained from frame buffer of the whole scene
tex2 - 1024/2 x 768/2 texture obtained from frame buffer and dowsampled

uniform sampler2D tex2;uniform sampler2D tex1; vec4 finalColor;int KERNEL_SIZE_A=4;int KERNEL_SIZE_B=3;  void main(){ float POWER=0.35;   vec4 sum = vec4(0);   vec2 texcoord = vec2(gl_TexCoord[1]);   int j;   int i;   for( i= -KERNEL_SIZE_A ;i <KERNEL_SIZE_A; i++)   {        for (j = -KERNEL_SIZE_B; j < KERNEL_SIZE_B; j++)        {            sum += texture2D(tex1, texcoord + vec2(j, i)*0.004) * POWER;        }   }       if (texture2D(tex1, texcoord).r < 0.3)    {       finalColor = sum*sum*0.012 + texture2D(tex1, texcoord);    }    else    {        if (texture2D(tex1, texcoord).r < 0.5)        {            finalColor = sum*sum*0.009 + texture2D(tex1, texcoord);        }        else        {            finalColor = sum*sum*0.0075 + texture2D(tex1, texcoord);        }    }   gl_FragColor= texture2D(tex2, gl_TexCoord[2])* finalColor;}



The issue here is the FPS when combining two of those textures at the end.

I have managed to find out that when i pass

//set the 1024x768 normal texture without bloom appliedgl_FragColor= texture2D(tex2, gl_TexCoord[2]);


i get around 120FPS in a very dense and ultra detailed scene.
But when i combine both tex1 and tex2 my FPS is getting bottlenecked to ~70FPS.

The texture operation

float POWER=0.35;   vec4 sum = vec4(0);   vec2 texcoord = vec2(gl_TexCoord[1]);   int j;   int i;   for( i= -KERNEL_SIZE_A ;i <KERNEL_SIZE_A; i++)   {        for (j = -KERNEL_SIZE_B; j < KERNEL_SIZE_B; j++)        {            sum += texture2D(tex1, texcoord + vec2(j, i)*0.004) * POWER;        }   }       if (texture2D(tex1, texcoord).r < 0.3)    {       finalColor = sum*sum*0.012 + texture2D(tex1, texcoord);    }    else    {        if (texture2D(tex1, texcoord).r < 0.5)        {            finalColor = sum*sum*0.009 + texture2D(tex1, texcoord);        }        else        {            finalColor = sum*sum*0.0075 + texture2D(tex1, texcoord);        }    } 


Takes around 2FPS,
the problem is the last gl_FragColor operation,
why does it slow down so much when it comes up to combine those two textures?



Ashaman73
Ashaman73
You got tricked by the shader optimization. The problem with your shader is the filter which consists of 12 texture reads.

The driver will optimize the shader by removing unused code. When you change your code to
//set the 1024x768 normal texture without bloom appliedgl_FragColor= texture2D(tex2, gl_TexCoord[2]);

the finalColor variable is not used at all. So the complete code to calculate finalColor will be removed leaving only 1 texture read instead of 14.
razor1911
razor1911
Thank you for enlightening me,
how do i bloom the texture then if a 4x3 filter is too much for the shader?

Is there any other better method than the way i am doing it?
Ashaman73
Ashaman73
Quote:

how do i bloom the texture then if a 4x3 filter is too much for the shader?

I made a misstake, you have 8*6=48 texture accesses.


Quote:

Is there any other better method than the way i am doing it?

Yes, for bloom take a gaussian blur filter which is separable, this mean, that you can divide the shader into 2 passes which will work with only 6+8=14 instead of 48 accesses: clicky

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.