着色器优化:三元运算符是否等效于分支? [英] Shader optimization: Is a ternary operator equivalent to branching?

查看:164
本文介绍了着色器优化:三元运算符是否等效于分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个顶点着色器,我想在其中有条件地放置一些顶点:

I'm working on a vertex shader in which I want to conditionally drop some vertices:

float visible = texture(VisibleTexture, index).x;
if (visible > threshold)
    gl_Vertex.z = 9999; // send out of frustum

我知道,当相邻数据之间几乎没有共同点时,分支会降低性能.在这种情况下,每个其他顶点可能会获得不同的可见"值,这对本地着色器核心群集的性能不利(根据我的理解).

I know that branches kill performance when there's little commonality between neighboring data. In this case, every other vertex may get a different 'visible' value, which would be bad for the performance of the local shader core cluster (from my understanding).

我的问题是:三元运算符更好(与可读性无关)吗?

To my question: Is a ternary operator better (irrespective of readability issues)?

float visible = texture(VisibleTexture, index).x;
gl_Vertex.z = (visible > threshold) ? 9999 : gl_Vertex.z;

如果没有,是否值得将其转换为计算结果?

If not, is converting it into a calculation worthwhile?

float visible = texture(VisibleTexture, index).x;
visible = sign(visible - threshold) * .5 + .5; // 1=visible, 0=invisible
gl_Vertex.z += 9999 * visible; // original value only for visible

是否有一种更好的方法可以在不依赖几何着色器的情况下放置顶点?

Is there an even better way to drop vertices without relying on a Geometry shader?

在此先感谢您的帮助!

推荐答案

三元运算符只是if语句的语法糖.他们是一样的.

A ternary operator is just syntactic sugar for an if statement. They are the same.

如果要在if语句中编写更多内容,则可以在此处进行一些优化,但是每个分支的内容都很少,因此实际上没有什么要优化的.

If you had more to write inside of your if statement, there might be some optimization that could be done here, but with so little inside of either branch, there is nothing to optimize really.

默认情况下,通常不使用分支.

Often branching is not used by default.

在您的情况下,三元运算符(或if语句)可能首先评估条件的两端,然后丢弃条件不满足的分支.

In your case, the ternary operator (or if statement) is probably evaluating both sides of the condition first and then discarding the branch that was not satisfied by the condition.

为了使用分支,您需要在着色器代码中设置分支编译器标志,以生成指示GPU实际尝试分支的程序集(如果GPU支持分支).在这种情况下,只有在分支预测器说某些预定义数量的内核将采用分支之一的情况下,GPU才会尝试分支.

In order to use branching, you need to set the branching compiler flag in your shader code, to generate assembly that instructs the GPU to actually attempt to branch (if the GPU supports branching). In that case, the GPU will try to branch only if the branch predictor says that some predefined number of cores will take one of the branches.

从一个编译器和一个GPU到另一个编译器,您的里程可能会有所不同.

Your mileage may vary from one compiler and GPU to another.

这篇关于着色器优化:三元运算符是否等效于分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆