OPENGL 高低版本差异

194 篇文章 42 订阅

GLSL Versions · mattdesl/lwjgl-basics Wiki · GitHub

You can use the #version command as the first line of your shader to specify GLSL version:android

#version 120

void main() {
    gl_FragColor = vec4(1.0);
}

GLSL versions are released alongside GL versions. See the following charts to decide which version you would like to target.ios

GLSL Versions

OpenGL VersionGLSL Version
2.0110
2.1120
3.0130
3.1140
3.2150
3.3330
4.0400
4.1410
4.2420
4.3430

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.git

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.github

Differences at a Glance

Differences between (desktop) GLSL versions.web

Version 100

Vertex shader:app

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:less

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.ide

Vertex shader:webgl

#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:ui

#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 andvarying

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

再WEBGL和es上面片元着色器必须加上精度描述。在片元着色器代码最前面加上一下代码

'#ifdef GL_ES\n'+

'precision mediump float;\n' +

'#endif\n'+

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙行天下01

你的鼓励将是我的最大写作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值