taontech / githublog

一个基于github issues的博客系统,实时呈现,零依赖,零代码部署,不用打包不用上线。
4 stars 1 forks source link

shader 安卓隐藏优化 #72

Open taontech opened 1 year ago

taontech commented 1 year ago

shader代码隐藏优化

看下面的代码:

void main(){
    vec3 pos;
    pos = aPosition;
    pos.x = 3.0 * sin(float(gl_VertexID)/400.0);
    pos.y = 3.0 * cos(float(gl_VertexID)/400.0);
    pos.z = float(gl_VertexID)/39000.0;
    pos.x = float(int(pos.x) % 20) + 3.0 * sin(float(gl_VertexID)/400.0);
    pos.y = float(int(pos.y) % 20) + 3.0 * cos(float(gl_VertexID)/400.0);
    pos.z = float(int(pos.z) % 20) + float(gl_VertexID)/39000.0;
    gl_Position = uProjectMatrix * uViewMatrix*uModelMatrix * vec4(pos.x, pos.y, pos.z, 1);

    gl_PointSize = uPointSize;
    vec3 hsl = vec3(float(gl_VertexID)/60000.0, 0.8, 0.8);
    vColor.rgb = hsb_rgb(hsl);
    vColor.a = 0.8;
}

其中:

    pos.x = 3.0 * sin(float(gl_VertexID)/400.0);
    pos.y = 3.0 * cos(float(gl_VertexID)/400.0);
    pos.z = float(gl_VertexID)/39000.0;

这段非常关键,如果有这三行,则上面pos = aposition就会被优化掉,导致aPosition没有被使用,运行时报错。 而

    pos.x = float(int(pos.x) % 20) + 3.0 * sin(float(gl_VertexID)/400.0);
    pos.y = float(int(pos.y) % 20) + 3.0 * cos(float(gl_VertexID)/400.0);
    pos.z = float(int(pos.z) % 20) + float(gl_VertexID)/39000.0;

则不会触发优化,因为在赋值的同时访问了pos的值,也就是aPosition的值。

taontech commented 1 year ago
void main(){
    vec3 pos;
    pos = aPosition;
    pos.x = 3.0 * sin(float(gl_VertexID)/400.0);
    pos.y = 3.0 * cos(float(gl_VertexID)/400.0);
    pos.z = float(gl_VertexID)/39000.0;
    pos.x = float(int(pos.x) % 20) + 3.0 * sin(float(gl_VertexID)/400.0);
    pos.y = float(int(pos.y) % 20) + 3.0 * cos(float(gl_VertexID)/400.0);
    pos.z = float(int(pos.z) % 20) + float(gl_VertexID)/39000.0;
    gl_Position = uProjectMatrix * uViewMatrix*uModelMatrix * vec4(pos.x, pos.y, pos.z, 1);

    gl_PointSize = uPointSize;
    vec3 hsl = vec3(float(gl_VertexID)/60000.0, 0.8, 0.8);
    vColor.rgb = hsb_rgb(hsl);
    vColor.a = 0.8;
}