4

我的片段着色器函数中有一些计算(如下),被调用了很多次。我想知道是否可以优化此代码。我看了一下OpenGL.org glsl optimization 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 次。

4

2 回答 2

2

将代码向量化如下:

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); 
}

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

于 2012-11-09T13:02:44.667 回答
0

修改纹理坐标而不是使用传递到片段着色器的坐标会创建动态纹理读取,并在早期硬件上产生最大的性能损失。

检查动态纹理查找的最后一部分

https://developer.apple.com/library/ios/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/BestPracticesforShaders/BestPracticesforShaders.html

他们建议将纹理坐标向上移动到片段着色器中。如果我正确理解代码的意图,看起来你可以没有太多问题。您为 UV(以及纹理)上的微调、缩放和动画添加了偏移和平铺支持?认为这样。用这个。

//
// Vertex Shader
//


attribute vec4 position;
attribute vec2 texture;


uniform mat4 modelViewProjectionMatrix;


// tiling parameters:
// -- x and y components of the Tiling (x,y)
// -- x and y components of the Offset (w,z)
// a value of vec4(1.0, 1.0, 0.0, 0.0) means no adjustment

uniform vec4 texture_ST;  

// UV calculated in the vertex shader, GL will interpolate over the pixels
// and prefetch the texel to avoid dynamic texture read on pre ES 3.0 hw.
// This should be highp in the fragment shader.

varying vec2 uv;


void main()
{
    uv = ((texture.xy * texture_ST.xy) + texture_ST.zw);

    gl_Position = modelViewProjectionMatrix * position;
}
于 2013-10-11T05:54:53.653 回答