Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

performance - 是否应避免在着色器中使用范围和光源类型的条件?(Should conditionals for range and light type in shaders be avoided?)

Similar questions:

(类似问题:)
Is this shader conditional bad for performance and can it be optimized?

(此着色器是否有条件损害性能,是否可以对其进行优化?)
Do conditional statements in shaders come at a higher cost?

(着色器中的条件语句会带来更高的成本吗?)
Do conditional statements slow down shaders?

(条件语句会减慢着色器的速度吗?)

My question is more specific.

(我的问题更具体。)

I have 3 types of lights: directional, pointlight and spotlight.

(我有3种类型的灯:定向灯,聚光灯和聚光灯。)

The latter 2 are attenuated and are limited by range.

(后者2被衰减并受范围限制。)

The first isn't.

(第一个不是。)

Pseudocode for the fragment/pixel shader:

(片段/像素着色器的伪代码:)

for each light {
    if (type == direcional) {
        color += lightColor * angleFactor;
    else {
        if (light.range < rangeToPixel) {
            // out of range - do nothing
        } else {
            atn = 1.0 / (const + lin * dist + quad * dist * dist);
            if (type == spotlight) {
                spotlightFactor = ... // a number from 0 to 1 signifying the decrease in intensity
                color += lightColor * spotlightFactor * atn * angleFactor * dot(n,l);
            } else { // pointlight
                color += lightColor * atn * dot(n,l);
            }
        }
    }
}

This looks like a lot of branching for a shader.

(对于着色器,这看起来像是很多分支。)

My thoughts are:

(我的想法是:)

  1. Move the directional light computation to the vertex shader since it contributes to all pixels/fragments in the vertex equally.

    (将定向光计算移动到顶点着色器,因为它对顶点中的所有像素/片段的贡献均等。)

    This will still need an if check on the light type.

    (这将仍然需要一个if在光类型检查。)

  2. Pointlight computation can be done on the spotlight branch by giving it a 360 degrees inner angle by default (then the direction of the spotlight is not important), so spotlightFactor == 1 .

    (可以通过默认为聚光灯分支设置360度内角来进行聚光灯计算(此时聚光灯的方向并不重要),因此spotlightFactor == 1 。)

    This gets rid of the branching on that computation but means that more math is being done when it is not needed, so more work.

    (这摆脱了该计算的分支,但意味着在不需要时可以完成更多的数学运算,因此需要更多的工作。)

Because I have only one system to test on, it won't be indicative, so I require help with the approach/design.

(因为我只有一个要测试的系统,所以它不是指示性的,因此我需要有关方法/设计的帮助。)

How should I modify the code?

(我应该如何修改代码?)

Using D3D9 shader model 3 and OpenGL version 2.1.

(使用D3D9着色器模型3和OpenGL版本2.1。)

  ask by user1803551 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...