GLSL和GLSL ES 2之间的区别

问题描述:

两个问题……

  1. GLSL ES 2是完全独立的语言还是GLSL的特殊版本?
  2. 就标准库"功能,语法和功能而言,它们之间有什么区别?

我正在为面向Windows,Mac和iPad的应用程序编写着色器,我希望不必为每个着色器添加更多版本-无论如何,都是更简单的着色器.

I am writing shaders for an application targeted at Windows, Mac and iPad and I would prefer not to have to add more versions of each shader - well simpler shaders anyway.

GLSL ES 2是完全独立的语言还是GLSL的特殊版本?

Is GLSL ES 2 a totally separate language, or a special version of GLSL?

每个版本的GLSL最终都是完全独立的语言";有些甚至不能与以前的版本向后兼容.但是,GLSL的ES变异更是如此.

Every version of GLSL is ultimately a "totally separate language;" some aren't even backwards compatible with previous versions. However, the ES variation of GLSL is even moreso.

将其视为桌面GLSL的分支.请注意,六个版本之前的版本的GLSL分支.

Think of it as a fork of desktop GLSL. A fork of GLSL from six versions ago, mind you.

就标准库"功能,语法和功能而言,它们之间有什么区别?

What are the differences between them, in terms of "standard library" functions, syntax and capabilities?

哪个版本之间?

您必须尝试在这些平台之间共享着色器的唯一可能原因是,如果您试图在它们之间共享OpenGL代码.因此,您必须限制自己使用桌面OpenGL 2.1功能.

The only possible reason you could have to try to share shaders between these platforms is if you're trying to share OpenGL code between them. So you'd have to be limiting yourself to desktop OpenGL 2.1 functionality.

这意味着您将使用桌面GLSL 1.20版.这类似于GLSL ES版本1.00(GLSL版本的编号与匹配的GL版本号不同.嗯,直到台式机GL 3.3/4.0和GLSL 3.30/4.00为止),但仍然有所不同.在ES中,您有很多台式GLSL 1.20无法处理的精度限定符.

That means you'd be using desktop GLSL version 1.20. This is similar to GLSL ES version 1.00 (the version of GLSL is not given the same number as the matching GL version. Well, until desktop GL 3.3/4.0 and GLSL 3.30/4.00), but still different. In ES, you have a lot of precision qualifiers that desktop GLSL 1.20 doesn't handle.

处理此问题的最佳方法是使用前导"着色器字符串.请参见, glShaderSource 将多个着色器字符串拍打在一起并编译为一个.您可以使用主着色器字符串(main及其各种代码,attribute定义,uniforms等),并在其前面粘贴一个"preamble"着色器字符串.序言会有所不同,具体取决于您要编译的内容.

The best way to handle that is to use a "preamble" shader string. See, glShaderSource takes multiple shader strings, which it slaps together and compiles as one. You can take your main shader string (main and its various code, attribute definitions, uniforms, etc) and stick a "preamble" shader string in front of that. The preamble would be different depending on what you're compiling for.

您的ES GLSL 1.00前言如下:

Your ES GLSL 1.00 preamble would look like this:

#version 100  //Must start with a version specifier.

您的桌面GLSL 1.20前言看起来像这样:

Your desktop GLSL 1.20 preamble would look like this:

#version 120
#define lowp
#define mediump
#define highp

也许还有其他一些明智的#defines.这样,您的着色器可以设置那些精度限定符,并且在使用桌面GLSL时将被#define删除.

And maybe a few other judicious #defines. This way, your shader can have those precision qualifiers set, and they'll just be #defined away when using desktop GLSL.

这与C/C ++中与平台无关的编码非常相似,在该平台中,您通常具有一些特定于平台的标头,这些标头会更改定义和/或具有#define您可以根据其调节代码.

This is a lot like platform-neutral coding in C/C++, where you often have some platform-specific header that changes the definition and/or has #defines that you can condition code based on.