GLSL Versions和GLSL ES Versions 的之间的关系和区别

本文详细介绍了OpenGL和OpenGLES中GLSL和GLSLES版本之间的差异和对应关系,包括版本号、特性可用性及关键代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://github.com/mattdesl/lwjgl-basics/wiki/GLSL-Versions

GLSL Versions

OpenGL Version

GLSL Version

2.0

110

2.1

120

3.0

130

3.1

140

3.2

150

3.3

330

4.0

400

4.1

410

4.2

420

4.3

430

 

GLSL ES Versions (Android, iOS, WebGL)

OpenGL ES has its own Shading Language, and the versioning starts fresh. It is based on OpenGL Shading Language version 1.10.

OpenGL ES VersionGLSL ES Version
2.0100
3.0300

So, for example, if a feature is available in GLSL 120, it probably won't be available in GLSL ES 100 unless the ES compiler specifically allows it.

Differences at a Glance

Version 100

Vertex shader:

uniform mat4 projTrans;

attribute vec2 Position;
attribute vec2 TexCoord;

varying vec2 vTexCoord;

void main() {
	vTexCoord = TexCoord;
	gl_Position = u_projView * vec4(Position, 0.0, 1.0);
}

Fragment shader:

uniform sampler2D tex0;

varying vec2 vTexCoord;

void main() {
    vec4 color = texture2D(tex0, vTexCoord);
    gl_FragColor = color;
}

Version 330

As of GLSL 130+, in and out are used instead of attribute and varying. GLSL 330+ includes other features like layout qualifiers and changes texture2D to texture.

Vertex shader:

#version 330

uniform mat4 projTrans;

layout(location = 0) in vec2 Position;
layout(location = 1) in vec2 TexCoord;

out vec2 vTexCoord;

void main() {
	vTexCoord = TexCoord;
	gl_Position = u_projView * vec4(Position, 0, 1);
}

Fragment shader:

#version 330
uniform sampler2D tex0;

in vec2 vTexCoord;

//use your own output instead of gl_FragColor 
out vec4 fragColor;

void main() {
    //'texture' instead of 'texture2D'
    fragColor = texture(tex0, vTexCoord);
}

Other Significant Changes

GLSL 120 Additions

  • You can initialize arrays within a shader, like so:
    float a[5] = float[5](3.4, 4.2, 5.0, 5.2, 1.1);
    float b[5] = float[](3.4, 4.2, 5.0, 5.2, 1.1);
    
    However, the above is not supported on Mac OSX Snow Leopard, even with GLSL 120. (1)
  • You can initialize uniforms in a shader, and the value will be set at link time:
    uniform float val = 1.0;
    
  • You can use built-ins like sin() when setting a const value
  • Integers are implicitly converted to floats when necessary, for example:
    float f = 1.0; <-- valid
    float g = 1; <-- only supported in GLSL 120
    vec2 v = vec2(1, 2.0); <-- only supported in GLSL 120
    
  • You can use f to define a float: float f = 2.5f;

GLSL 130 Additions

  • int and uint support (and bitwise operations with them)
  • switch statement support
  • New built-ins: trunc(), round(), roundEven(), isnan(), isinf(), modf()
  • Fragment output can be user-defined
  • Input and output is declared with in and out syntax instead of attribute and varying

GLSL 150 Additions

  • texture() should now be used instead of texture2D()

GLSL 330 Additions

  • Layout qualifiers can declare the location of vertex shader inputs and fragment shader outputs, eg:
    layout(location = 2) in vec3 values[4];
    
    Formally this was only possible with ARB_explicit_attrib_location extension

 

 

### OpenGL Fixed Function Pipeline vs Programmable Pipeline Differences In traditional OpenGL versions prior to 2.0, the **fixed function pipeline** was utilized where developers had limited control over how vertices were transformed or fragments shaded; this process relied on predefined functions set by the API itself[^1]. The architecture included stages like transformation, lighting, clipping, rasterization, texture application, and finally pixel operations. With the advent of more advanced GPUs, OpenGL introduced the **programmable pipeline**, starting from version 2.0 onwards with specifications such as OpenGL ES 2.0 which emphasized creating shader programs using GLSL (OpenGL Shading Language). This shift allowed for greater flexibility in defining custom behavior at various points within the rendering pipeline through user-defined shaders—specifically vertex shaders that manipulate geometric data before it reaches later stages of processing, and fragment shaders responsible for determining final colors per-pixel after rasterization has occurred. The key distinctions between these two approaches are summarized below: #### Control Over Processing Stages - In the fixed-function approach, each stage operates according to pre-established rules without any direct intervention possible beyond setting parameters. - Conversely, under the programmable model, one writes code directly controlling what happens during both vertex transformations and fragment shading processes via shaders written in GLSL. #### Flexibility & Customizability - Limited customization options exist when working strictly inside a rigid framework provided solely by built-in functionalities offered by earlier APIs. - Enhanced capabilities allow artists/developers full access to modify nearly every aspect related to visual effects generation including but not limited to implementing non-standard algorithms unachievable otherwise due to constraints imposed previously upon them. #### Performance Considerations - While simpler applications may benefit slightly better performance-wise out-of-the-box because they do not require compilation time associated with writing new shaders, - Complex scenes requiring intricate interactions among multiple elements often see significant improvements once optimized specifically tailored towards target hardware characteristics thanks largely in part owing much credit given hereafter attributed primarily toward leveraging modern GPU architectures effectively. ```cpp // Example Vertex Shader Code Snippet #version 330 core layout(location = 0) in vec3 position; void main() { gl_Position = vec4(position, 1.0); } ``` ```cpp // Example Fragment Shader Code Snippet #version 330 core out vec4 FragColor; void main(){ FragColor = vec4(1.0f); // White color output } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值