# Help with speeding up a pixel shader please...

**URL:** <https://forums.imgtec.com/t/help-with-speeding-up-a-pixel-shader-please/288>\
**Category:** PowerVR Insider\
**Created:** [March 19, 2009, 3:39pm UTC](https://forums.imgtec.com/t/help-with-speeding-up-a-pixel-shader-please/288 "2009-03-19T15:39:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![richarduk](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@richarduk](https://forums.imgtec.com/u/richarduk)\
**Post date:** [March 19, 2009, 3:39pm UTC](https://forums.imgtec.com/t/help-with-speeding-up-a-pixel-shader-please/288/1 "2009-03-19T15:39:15Z")

</div>

This is a shader I use to add extra detail to a low resolution texture. You can think of it as colour keying. Running this shader is costing me about 6fps and I think it’s because of the way I use the dynamic branching. Any idea’s on speeding it up?  
  
I did a test were the if’s were always true and it ran quicker so it’s not the extra texture sampling, I think…  
  
My best bet is to use lessThan function cast the result to a vec4 and do a mix.  
  
For example  
  
vec3 diff = texel.xyz - detail\_mapping[0].xyz;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
vec3 replace = lessThan(dot(diff,diff),detail\_mapping[0].www);  
  
  
texel.xyz = mix(texel.xyz, texture2D(tex\_sampler1,v\_texcoord\_detail),replace);  
  
  
  
But for just doing a simple “replace colour with another colour if colour == A\_value” seems a lot of work.  
  
precision lowp float;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
uniform bvec4 flags;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
uniform sampler2D tex\_sampler0;&nbsp;&nbsp;&nbsp;   
uniform vec4 fog\_colour;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
uniform vec4 detail\_mapping[2];&nbsp;&nbsp;&nbsp;   
uniform sampler2D tex\_sampler1;&nbsp;&nbsp;&nbsp;   
uniform sampler2D tex\_sampler2;&nbsp;&nbsp;&nbsp;   
varying mediump vec2 v\_texcoord;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
varying mediump vec2 v\_texcoord\_detail;&nbsp;&nbsp;&nbsp;   
varying vec4 v\_colour;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
varying float v\_fog\_factor;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
void main()  
{  
&nbsp; &nbsp;&nbsp;&nbsp; vec4 texel = texture2D(tex\_sampler0,v\_texcoord);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
&nbsp; &nbsp;&nbsp;&nbsp; vec3 diff = texel.xyz - detail\_mapping[0].xyz;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
  
&nbsp; &nbsp;&nbsp;&nbsp; if( dot(diff,diff) \< detail\_mapping[0].w&nbsp; )&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
&nbsp; &nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; texel = texture2D(tex\_sampler1,v\_texcoord\_detail);&nbsp;&nbsp;&nbsp;   
&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
&nbsp; &nbsp;&nbsp;&nbsp; diff = texel.xyz - detail\_mapping[1].xyz;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
&nbsp; &nbsp;&nbsp;&nbsp; if( dot(diff,diff) \< detail\_mapping[1].w&nbsp; )&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
&nbsp; &nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; texel = texture2D(tex\_sampler2,v\_texcoord\_detail);&nbsp;&nbsp;&nbsp;   
&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
&nbsp; &nbsp;&nbsp;&nbsp; gl\_FragColor = texel \* v\_colour;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
  
&nbsp;&nbsp;&nbsp; if( flags[2] )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; gl\_FragColor = mix(fog\_colour,gl\_FragColor,v\_fog\_factor);  
&nbsp;&nbsp;&nbsp; }  
}

---

<div class="post-metadata">

**Author:** ![richarduk](https://avatars.discourse-cdn.com/v4/letter/r/6de8d8/32.png) [@richarduk](https://forums.imgtec.com/u/richarduk)\
**Post date:** [March 20, 2009, 9:30am UTC](https://forums.imgtec.com/t/help-with-speeding-up-a-pixel-shader-please/288/2 "2009-03-20T09:30:03Z")

</div>

I’ve gained an extra 4 to 5 fps using step to do the test and mix to do the replacement.  
  
vec3 diff = texel.xyz - detail\_mapping[0].xyz;  
float replace = step(dot(diff,diff),detail\_mapping[0].w);  
texel = mix(texel,texture2D(tex\_sampler1,v\_texcoord\_detail),replace);  
  
I would have thought the compiler would have done the same???
