# Issues with PowerVR SGX 540 fragment shaders

**URL:** <https://forums.imgtec.com/t/issues-with-powervr-sgx-540-fragment-shaders/1180>\
**Category:** PowerVR Insider\
**Created:** [September 9, 2011, 7:16pm UTC](https://forums.imgtec.com/t/issues-with-powervr-sgx-540-fragment-shaders/1180 "2011-09-09T19:16:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![madruga](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@madruga](https://forums.imgtec.com/u/madruga)\
**Post date:** [September 9, 2011, 7:16pm UTC](https://forums.imgtec.com/t/issues-with-powervr-sgx-540-fragment-shaders/1180/1 "2011-09-09T19:16:46Z")

</div>

Hello,  
  
I'm an Android developer, our App applies real-time effects to the camera preview, using some fairly simple GLSL shaders.  
  
We are getting some issues with devices that use the PowerVR SGX 540 (Galaxy S and Droid 3 among others).  
  
The errors don't affect all variants of the devices equally, our Galaxy S works fine, but the ones from the American carriers don't: some effects don't show up, we have some error reporting features in the App, and the typical error we get is a GL\_INVALID\_OPERATION after either glDrawArrays or glGetUniformLocation (and the shader doesn't work)  
  
We've tried all sorts of things with no luck: packing uniforms into vec4s, removing uniforms altogether, rearrange client code, etc...  
  
My questions are:  
- Do all devices use the same drivers? Different variants of the same phone seem to behave differently.  
  
- Are there any known bugs with these devices? should I be avoiding any particular features?  
  
As an example of our shaders, this is one that creates a cartoon effect:  
  
precision mediump float;&nbsp;&nbsp;&nbsp;   
  
varying vec4 v\_yTexCoord;  
varying vec4 v\_effectTexCoord;  
  
uniform sampler2D effectTexture;  
uniform sampler2D effectTexture2;  
uniform sampler2D yTexture;&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;   
uniform sampler2D uvTexture;&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; &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;   
  
uniform vec4 pvalues;  
uniform vec4 textureSize;  
  
float yDecode(vec2 texCoord)  
{  
&nbsp;&nbsp;&nbsp; return 1.164 \* ( texture2D(yTexture, texCoord).r - 0.0627 );&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
}  
  
vec4 yuvDecode(vec2 texCoord)  
{  
&nbsp;&nbsp;&nbsp; vec4 y = 1.164 \* ( texture2D(yTexture, texCoord) - 0.0627 );&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; vec4 uv = texture2D(uvTexture, texCoord) - 0.5;&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; return y + vec4( 1.596 \* uv.r, - 0.813 \* uv.r - 0.391 \* uv.a, 2.018 \* uv.a, 1.0);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;   
}  
  
float edgeDetection(vec2 texCoords)  
{&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; vec2 xdisp = vec2(1.0/textureSize.x, 0.0);  
&nbsp;&nbsp;&nbsp; vec2 ydisp = vec2(0.0, 1.0/textureSize.y);  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; //grab surrounding px colors  
&nbsp;&nbsp;&nbsp; float tl = yDecode(texCoords - xdisp - ydisp);  
&nbsp;&nbsp;&nbsp; float l =&nbsp; yDecode(texCoords - xdisp);  
&nbsp;&nbsp;&nbsp; float bl = yDecode(texCoords - xdisp + ydisp);  
&nbsp;&nbsp;&nbsp; float t =&nbsp; yDecode(texCoords - xdisp);  
&nbsp;&nbsp;&nbsp; float b =&nbsp; yDecode(texCoords + ydisp);  
&nbsp;&nbsp;&nbsp; float tr = yDecode(texCoords + xdisp - ydisp);  
&nbsp;&nbsp;&nbsp; float r =&nbsp; yDecode(texCoords + xdisp);  
&nbsp;&nbsp;&nbsp; float br = yDecode(texCoords + xdisp + ydisp);  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; float dX = -tl - 2.0\*l - bl + tr + 2.0\*r + br;  
&nbsp;&nbsp;&nbsp; float dY = -tl - 2.0\*t - tr + bl + 2.0\*b + br;  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; //compute the magnitude  
&nbsp;&nbsp;&nbsp; float grad = abs(dX) + abs(dY);  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; return clamp(grad\*pvalues.z, 0.0, 1.0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
}  
  
void main()  
{&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; float grad = edgeDetection(v\_yTexCoord.xy);  
&nbsp;&nbsp;&nbsp; highp vec4 color = yuvDecode(v\_yTexCoord.xy);  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; vec3 yuv;  
&nbsp;&nbsp;&nbsp; yuv.r = 0.299 \* color.r + 0.587 \* color.g + 0.114 \* color.b;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; yuv.g = (color.b-yuv.r)\*0.565;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; yuv.b = (color.r-yuv.r)\*0.713;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; float grayscale = yuv.r;  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; float chalk = floor((yuv.r \* 5.0 ) + 0.5);  
&nbsp;&nbsp;&nbsp; float howmuch = 48.0;  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; yuv.r = ceil((yuv.r \* 256.0 / howmuch)-0.5);  
&nbsp;&nbsp;&nbsp; yuv.r \*= howmuch;  
&nbsp;&nbsp;&nbsp; yuv.r /= 256.0;  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; vec3 colorC;  
&nbsp;&nbsp;&nbsp; colorC.r = yuv.r + 1.403 \* yuv.b;  
&nbsp;&nbsp;&nbsp; colorC.g = yuv.r - 0.344 \* yuv.g - 0.714 \* yuv.b;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; colorC.b = yuv.r + 1.770 \* yuv.g;  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; grad = mix(grad, 0.5, grad);  
&nbsp;&nbsp;&nbsp; color.rgb = vec3(1.0 - grad);  
  
&nbsp;&nbsp;&nbsp; vec2 halfCoord = v\_effectTexCoord.xy \* 0.5;  
&nbsp;&nbsp;&nbsp; vec2 topLeft = vec2(0.0, 0.0);  
&nbsp;&nbsp;&nbsp; vec2 topRight = vec2(0.5, 0.0);  
&nbsp;&nbsp;&nbsp; vec2 bottomLeft = vec2(0.0, 0.5);  
&nbsp;&nbsp;&nbsp; vec2 bottomRight = vec2(0.5, 0.5);  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; if ( chalk \< 1.0 )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; color.rgb -= vec3(1.0 - texture2D( effectTexture, halfCoord.xy + bottomRight&nbsp; ).rgb);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; }   
&nbsp;&nbsp;&nbsp; if ( (chalk \>= 1.0&nbsp; ) && (chalk \< 2.0) )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; color.rgb -= vec3(1.0 - texture2D( effectTexture, halfCoord.xy + bottomLeft ).rgb);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; }   
&nbsp;&nbsp;&nbsp; if ( (chalk \>= 2.0 ) && (chalk \< 3.0) )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; color.rgb -= vec3(1.0 - texture2D( effectTexture, halfCoord.xy + topRight ).rgb);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; }   
&nbsp;&nbsp;&nbsp; if ( (chalk \>= 3.0) )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; color.rgb -= vec3(1.0 - texture2D( effectTexture, halfCoord.xy + topLeft ).rgb);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; color.rgb = mix(colorC&nbsp; \* vec3(229.0/255.0, 170.0/255.0, 110.0/255.0), color.rgb, 0.65);  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; // Contrast 2\*(Src\*t + 0.25 - 0.5\*t)&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; color.xyz = ((color.rgb - 0.5) \* max(pvalues.x, 0.0)) + 0.5;  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; // Brightness&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; color.xyz \*= vec3(pvalues.y, pvalues.y, pvalues.y);  
&nbsp;&nbsp;&nbsp;   
&nbsp;&nbsp;&nbsp; gl\_FragColor = color;&nbsp;&nbsp;&nbsp;   
}  
  
  
  
  
  
  
madruga2011-09-09 21:19:23
