Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL ES and OpenGL compatible shaders

I want to have same shader source for OpenGL ES and OpenGL (Windows). To do this I want to define custom data types and use only OpenGL ES functions.

One approach is to define:

#define highp
#define mediump
#define lowp

for Windows shader and write the shader as it is for OpenGL ES.

Other approach is to define custom data types like this for OpenGL ES:

#define hvec2 highp vec2

and like this for Windows

#define hvec2 vec2

What do you think is better? Do you have other solutions for this problem?

like image 386
Mircea Ispas Avatar asked Jul 11 '12 14:07

Mircea Ispas


1 Answers

My solution has been to write shader code that is compatible with both APIs, and then when uploading the fragment shader source code to OpenGL ES just prepend the following line:

precision mediump float;

That sets the default floating point precision to medium. In OpenGL ES fragment shaders there is no default precision level for floating point values, which means you either need to specify a precision on every floating point declaration, or explicitly set a default precision. You could use lowp or highp instead if that makes more sense for your application.

For more details see section 4.5.3 of the OpenGL ES Shading Language specification: http://www.khronos.org/files/opengles_shading_language.pdf

You could also mix this approach with your #define approach if you need finer control over precision in your shaders.

like image 119
Richard Viney Avatar answered Sep 19 '22 12:09

Richard Viney