HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了优化片段着色器中的GLSL代码(iOS 5 OpenGL ES 2.0)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_618_4@ 我的片段着色器函数中有一些计算(下面),这被称为很多次.我想知道是否可以优化此代码.我看了一下 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)所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。