金属结构中未使用的属性会损害性能(Do unused properties in a metal struct hurt performance)

编程入门 行业动态 更新时间:2024-10-20 08:26:12
金属结构中未使用的属性会损害性能(Do unused properties in a metal struct hurt performance)

假设我在.metal文件中有以下结构:

struct VertexOut{ float4 position [[position]]; float2 a; float2 b; };

此结构是我的片段着色器的[[ stage-in ]]输入。 现在我的一些着色器使用b而有些则不使用。 这是否会伤害性能,这意味着我应该为不需要它的着色器创建另一个没有b结构吗?

Suppose I have the following struct in a .metal file:

struct VertexOut{ float4 position [[position]]; float2 a; float2 b; };

This struct is the [[ stage-in ]] input for my fragment shaders. Now some of my shaders use b and some do not. Is this hurting performance at all, meaning should I create another struct without b for the shaders that do not need it?

最满意答案

答案取决于您的使用情况。 将会有内存带宽性能上升,但它会相当小。 如果使用此着色器输出全屏像素并且带宽有限,则删除不需要的float2 可能会有所帮助。

但是,在更改着色器时通常会出现性能损失,因此您可能无需更改片段着色器就可以获得性能优势,这很可能超过任何损失。

我要做的一个建议是将a和b改为float4 a_b并使用a_b.xy和a_b.zw访问它。 寄存器对齐到16个字节,因此这将节省空间。 以这个例子:

struct X { float4 position; float2 a; float2 b; }; struct Y { float4 position; float2 a; }; struct Z { float4 position; float4 a_b; };

在上面的例子中,X将使用48个字节(每个寄存器有3个16字节的寄存器),而Y或Z只有32个字节(有2个寄存器)。 所以使用Z可能会给你两全其美。

The answer depends on your usage. There will be a memory bandwidth performance hit but it will be fairly small. If you were outputting a full screen of pixels using this shader and were bandwidth limited, removing the unneeded float2 may help.

However, there is often a performance hit in changing shaders, so you are possibly getting a performance benefit by not needed to change fragment shaders which quite possibly outweighs any losses.

One suggestion I would make is to some change a and b to be float4 a_b and access it with a_b.xy and a_b.zw. Registers are aligned to 16 bytes, so this will save you space. Take this example:

struct X { float4 position; float2 a; float2 b; }; struct Y { float4 position; float2 a; }; struct Z { float4 position; float4 a_b; };

In the above case, X will use 48 bytes (there are 3 registers of 16 bytes each), whereas Y or Z is only 32 bytes (there are 2 registers). So using Z would potentially give you the best of both worlds.

更多推荐

本文发布于:2023-07-26 15:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1277505.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   中未   性能   金属结构   unused

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!