Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android OpenGL ES 2.0: Is "switch-case" syntax possible in GLSL fragment shader on Samsung Galaxy S2?

Does anybody know how to do switch case syntax in the fragment shader on the Samsung Galaxy S2? I get the compilation error: Expected literal or '(', got 'switch'.

My syntax is as follows:

switch(i){
    case 0: x = alphas[0]; break;
    case 1: //...etc.
}

This works fine on the Nexus 7, but on the Galaxy S2 I get the above error. Are switch case instructions simply impossible on the Galaxy S2? The reason why I want to use them is they appear to give a performance improvement over if else on the Nexus 7. If they are impossible on the Galaxy S2, is there a way to query the device and use switch case if available, and if else otherwise?

like image 347
Navigateur Avatar asked Apr 14 '13 17:04

Navigateur


People also ask

Does OpenGL use GLSL?

The OpenGL Shading Language (GLSL) is the principal shading language for OpenGL.

What is a fragment shader OpenGL?

A Fragment Shader is the Shader stage that will process a Fragment generated by the Rasterization into a set of colors and a single depth value. The fragment shader is the OpenGL pipeline stage after a primitive is rasterized. For each sample of the pixels covered by a primitive, a "fragment" is generated.

What language is GLSL written in?

OpenGL Shading Language (GLSL) is a high-level shading language with a syntax based on the C programming language.

How many times does a fragment shader run?

In this case, your fragment shader runs once for every fragment within that quad. For this program, that means 100 * 100 = 10000 times, once for every pixel on your screen. However, not every fragment rendered in the shader has to be displayed on the screen.


2 Answers

switch statement is not supported in OpenGL ES 2.0. From the OpenGL ES Shading Language 1.0.17 spec 3.7:

The following are the keywords reserved for future use. Using them will result in an error:

asm class union enum typedef template this packed goto switch default ...

like image 64
Kimi Avatar answered Oct 25 '22 18:10

Kimi


GLSL ES is based on version 1.10 of the desktop GLSL, but 'switch case' statements were added in vesrion 1.30, so you cannot assume device will support it.

Also I would recommend avoiding branching in fragment shader as it hits perfomanace badly.

like image 32
zombo Avatar answered Oct 25 '22 17:10

zombo