OpenGL ES and OpenGL compatible shaders

心不动则不痛 提交于 2020-04-13 02:49:11

问题


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?


回答1:


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.




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/11435105/opengl-es-and-opengl-compatible-shaders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!