Imagination PowerVR SDK Blog

Question regarding lowp and swizzling


#1

Hi. I have a performance question about low precision variables and swizzling.


I read that you should avoid swizzling lowp variables since it’s expensive, but what exactly counts as bad swizzling?





for example, is this considered swizzling?





Code:

lowp vec4 texture = texture2D(tex0, uv0);
lowp vec3 color_rgb = texture.rgb;
lowp float color_alpha = texture.a;



Or is the "bad" swizzling only like this:

Code:

lowp vec4 texture = texture2D(tex0, uv0);
lowp vec3 color_bgr = texture.bgr; // most likely bad
lowp vec3 color_alpha = texture.aaa; // bad?, would lowp vec color_alpha = vec3(texture.a); be to prefer in this case?




#2

The second example may prove more expensive in shader cycles, but without a bit more context it’s difficult to know exactly how much difference it will make. This is why we recommend you make use of tools like PVRUniSCoEditor to gain some actual cycle count information from a real compiler.





Using a line with your first example like this:





Code:

     gl_FragColor = vec4(vec3(color_rgb* color_alpha),color_alpha);



Costs only a single cycle above directly outputting the texture read (probably the multiplication).



Whereas:



Code:

     gl_FragColor = vec4(color_rgb* color_alpha,color_alpha);



Costs 5 extra cycles with the compiler I'm using.



In short, in this example you're correct, but it might not always be that way - use tools and benchmark.



(Without some actual use of the variables here, the compiler will ignore both instances of swizzling)