6

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?

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211

3 Answers3

5

You could use http://www.opengl.org/registry/specs/ARB/ES2_compatibility.txt

This extension adds support for features of OpenGL ES 2.0 that are missing from OpenGL 3.x. Enabling these features will ease the process of porting applications from OpenGL ES 2.0 to OpenGL.

StuGrey
  • 1,479
  • 9
  • 20
  • Does anyone know how this works? I have skimmed through the spec but could not find anything related to how an GLSL ES (version 100) and GLSL (version 150) would be compatible at all. – Justin Meiners Jun 23 '16 at 17:18
  • For future reference I discovered that under ES2_compatibility an OpenGL 3.2 > context can load ES 2.0 shaders with #version 100 at the top. – Justin Meiners Jun 25 '16 at 15:37
5

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.

Richard Viney
  • 957
  • 16
  • 31
2

I managed it by just defining the following at the begin of each shader when the shader runs on windows:

#define lowp
#define mediump
#define highp

Additional i give the shader the information which plattform is used ( WINDOWS, MOBILE, IOS ) which can be used this way:

#ifdef WINDOWS
precision mediump float;
#endif
Felix K.
  • 6,201
  • 2
  • 38
  • 71
  • Tempered upvote for the top part of the comment — I think it's much more valuable to have the precision modifiers in and discard them on irrelevant platforms than not to include them and be at a performance detriment elsewhere. But `#ifdef GL_ES` (and `#ifndef`) is the easiest way to conditionally compile, being built in. – Tommy Feb 24 '16 at 22:16