OpenGL ES和OpenGL兼容着色器 [英] OpenGL ES and OpenGL compatible shaders
问题描述
我想为OpenGL ES和OpenGL(Windows)使用相同的着色器源.为此,我想定义自定义数据类型并仅使用OpenGL ES函数.
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.
一种方法是定义:
#define highp
#define mediump
#define lowp
对于Windows着色器,并按原样为OpenGL ES编写着色器.
for Windows shader and write the shader as it is for OpenGL ES.
其他方法是为OpenGL ES定义这样的自定义数据类型:
Other approach is to define custom data types like this for OpenGL ES:
#define hvec2 highp vec2
,对于Windows,则是这样
and like this for Windows
#define hvec2 vec2
您认为哪个更好?您是否有其他解决此问题的方法?
What do you think is better? Do you have other solutions for this problem?
推荐答案
我的解决方案是编写与两个API兼容的着色器代码,然后将片段着色器源代码上传到OpenGL ES时,只需在以下行添加前缀:
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;
将默认浮点精度设置为中.在OpenGL ES片段着色器中,没有浮点值的默认精度级别,这意味着您要么需要在每个浮点声明中指定精度,要么显式设置默认精度.如果对您的应用程序更有意义,则可以使用lowp
或highp
.
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.
有关更多详细信息,请参见OpenGL ES着色语言规范的第4.5.3节: http://www.khronos.org/files/opengles_shading_language.pdf
For more details see section 4.5.3 of the OpenGL ES Shading Language specification: http://www.khronos.org/files/opengles_shading_language.pdf
如果需要更好地控制着色器的精度,也可以将此方法与#define
方法混合使用.
You could also mix this approach with your #define
approach if you need finer control over precision in your shaders.
这篇关于OpenGL ES和OpenGL兼容着色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!