Last active
May 21, 2021 07:03
-
-
Save hjm1fb/20c82a1e3cfa797a9645f9842dc1a077 to your computer and use it in GitHub Desktop.
glsl优化操作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
常用关系运算符优化 | |
//relation operator | |
vec4 when_eq(vec4 x, vec4 y) { | |
return 1.0 - abs(step(x - y)); | |
} | |
vec4 when_neq(vec4 x, vec4 y) { | |
return abs(step(x - y)); | |
} | |
vec4 when_gt(vec4 x, vec4 y) { | |
return max(step(x - y), 0.0); | |
} | |
vec4 when_lt(vec4 x, vec4 y) { | |
return max(step(y - x), 0.0); | |
} | |
vec4 when_ge(vec4 x, vec4 y) { | |
return 1.0 - when_lt(x, y); | |
} | |
vec4 when_le(vec4 x, vec4 y) { | |
return 1.0 - when_gt(x, y); | |
} | |
常用逻辑运算符优化 | |
//logical operator; | |
vec4 and(vec4 a, vec4 b) { | |
return a * b; | |
} | |
vec4 or(vec4 a, vec4 b) { | |
return min(a + b, 1.0); | |
// or | |
// return max(a, b); | |
} | |
vec4 xor(vec4 a, vec4 b) { | |
return (a + b) % 2.0; | |
} | |
vec4 not(vec4 a) { | |
return 1.0 - a; | |
} | |
例子: | |
if (x == 0) { | |
y += 5; | |
} | |
=》 | |
y += 5 * when_eq(x, 0); | |
if (x == 0) { | |
b = a1; | |
} else { | |
b = a2; | |
} | |
=》 | |
b = mix(a1, a2, when_neq(x, 0)); | |
备注: 对于OpenGL ES 2.0,基本上大都只能优化静态分支;对于OpenGL ES 3.0,通常可以优化uniform数据分支即静态分支,部分机型可能可以优化动态分支 | |
现代GPU编译器已经能够很好的优化代码,不用像 GPU Gem介绍的Shader优化 需要手动整理成硬件友好的 mad、rcp 等形式,而且大量的 swizzle 技巧也是编译器的常规操作。手动调整一方面会影响代码可读性,另一方面带来的收益性价比不高。当然在 ALU 成为绝对瓶颈的情况下,可以进行尝试。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment