小编Cra*_*nog的帖子

我应该在 GLSL 中使用条件分支来避免可能的纹理查找吗?

我有一系列关于 GLSL 中不统一的流量控制及其在现代桌面 GPU 上的性能成本的问题。首先,我想指出,我已经阅读了手册,但仍然没有找到答案。让我们开始吧。

  1. Alpha 检查和零乘优化。哪个片段着色器运行得更快?(两者的标题相同)

    in vec2 textureCoordIn; //interpolated texture coords from vertex shader
    out vec4 outputColor; //resulted color should be here
    uniform sampler2D alphaMask; // splat alpha mask for textures1-4;
    uniform sampler2D mainTexture1;
    uniform sampler2D mainTexture2;
    uniform sampler2D mainTexture3;
    uniform sampler2D mainTexture4;
    
    void main(){
        vec4 maskValues = texture(alphaMask,textureCoordIn);
        if (maskValues.r>0){
            outputColor += maskValues.r * texture(mainTexture1,textureCoordIn);
        }
        if (maskValues.g>0){
            outputColor += maskValues.g * texture(mainTexture2,textureCoordIn);
        }
        if (maskValues.b>0){
            outputColor += maskValues.b * texture(mainTexture3,textureCoordIn);
        }
        if (maskValues.w>0){
            outputColor …
    Run Code Online (Sandbox Code Playgroud)

opengl textures glsl

7
推荐指数
0
解决办法
1822
查看次数

着色器中的点积与直接向量分量总和性能

我正在编写 CG 着色器,用于基于 Unity 的游戏的高级光照计算。有时需要对所有向量分量求和。有两种方法可以做到这一点:

  1. 只需编写如下内容: float sum = vx + vy + vz;
  2. 或者做类似的事情: float sum = dot(v,float3(1,1,1));

我真的很好奇什么更快并且代码风格看起来更好。

显然,如果我们对CPU计算有同样的问题,第一种简单的方法要好得多。因为:

a) 不需要再分配一个float(1,1,1)向量

b) 无需将每个原始向量“v”分量乘以 1。

但由于我们在 GPU 上运行的着色器代码中执行此操作,因此我相信点积函数有一些很好的硬件优化,并且可能会在根本不分配的情况下转换 float3(1,1,1) 的分配。

float4 _someVector;

void surf (Input IN, inout SurfaceOutputStandard o){
   float sum = _someVector.x + _someVector.y + _someVector.z + _someVector.w;
    // VS
   float sum2 = dot(_someVector, float4(1,1,1,1));
}
Run Code Online (Sandbox Code Playgroud)

performance shader cg hlsl

6
推荐指数
2
解决办法
4609
查看次数

标签 统计

cg ×1

glsl ×1

hlsl ×1

opengl ×1

performance ×1

shader ×1

textures ×1