优化片段着色器中的GLSL代码(iOS 5 OpenGL ES 2.0)

优化片段着色器中的GLSL代码(iOS 5 OpenGL ES 2.0),第1张

概述我的片段着色器函数中有一些计算(下面),这被称为很多次.我想知道是否可以优化代码.我看了一下 OpenGL.org glsl optimisation page,并进行了一些修改,但是有可能使这段代码更快吗? uniform int mn;highp float Nx;highp float Ny; highp float Nz;highp float invXTMax;highp 我的片段着色器函数中有一些计算(下面),这被称为很多次.我想知道是否可以优化此代码.我看了一下 OpenGL.org glsl optimisation page,并进行了一些修改,但是有可能使这段代码更快吗?

uniform int mn;highp float Nx;highp float Ny;   highp float Nz;highp float invXTMax;highp float invYTMax;int m;    int n;    highp vec4 func(in highp vec3 texCoords3D){            // tile index    int Ti = int(texCoords3D.z * Nz);    // (r,c) position of tile withn texture unit    int r = Ti / n; // integer division    int c = Ti - r * n;    // x/y offsets in pixels of tile origin with the texture unit    highp float xOff = float(c) * Nx;    highp float yOff = float(r) * Ny;    // 2D texture coordinates    highp vec2 texCoords2D;    texCoords2D.x = (Nx * texCoords3D.x + xOff)*invXTMax;    texCoords2D.y = (Ny * texCoords3D.y + yOff)*invYTMax;    return texture2D(uSamplerTex0,texCoords2D); }

编辑:

为了给出一些上下文,func()用作光线投射设置的一部分.它被召集起来
每个片段从main()300次.

解决方法 矢量化代码非常容易,如下所示:

highp vec3 N;highp vec2 invTMax;highp vec4 func(in highp vec3 texCoords3D){            // tile index    int Ti = int(texCoords3D.z * N.z);    // (r,c) position of tile within texture unit    int r = Ti / n;    int c = Ti - r * n;    // x/y offsets in pixels of tile origin with the texture unit    highp vec2 Off = vec2( float(c),float(r) ) * N;    // 2D texture coordinates    highp vec2 texCoords2D = ( N * texCoords3D.xy + Off ) * invTMax;    return texture2D(uSamplerTex0,texCoords2D); }

确保类似的计算并行运行.

总结

以上是内存溢出为你收集整理的优化片段着色器中的GLSL代码(iOS 5 OpenGL ES 2.0)全部内容,希望文章能够帮你解决优化片段着色器中的GLSL代码(iOS 5 OpenGL ES 2.0)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1035756.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存