打包数据类型与普通数据类型之间的区别 [英] Difference between packed vs normal data type
问题描述
在《金属》中,packed_float4
和float4
有什么区别?
In Metal what is the difference between a packed_float4
and a float4
?
推荐答案
此信息来自 float4
的对齐方式为16
个字节.这意味着这种类型的内存地址(例如0x12345670
)将被16
整除(又名最后一个十六进制数字为0
).
float4
has an alignment of 16
bytes. This means that the memory address of such a type (e.g. 0x12345670
) will be divisible by 16
(aka the last hexadecimal digit is 0
).
packed_float4
的对齐方式为4 bytes
.地址的最后一位是0
,4
,8
或c
packed_float4
on the other hand has an alignment of 4 bytes
. Last digit of the address will be 0
, 4
, 8
or c
这在创建自定义结构时很重要.假设您想要一个具有2个普通float
s和1个float4
/packed_float4
的结构:
This does matter when you create custom structs. Say you want a struct with 2 normal float
s and 1 float4
/packed_float4
:
struct A{
float x, y;
float4 z;
}
struct B{
float x, y;
packed_float4 z;
}
对于A
:float4
的对齐方式必须为16
,并且由于float4
必须在常规float
之后,因此8
字节之间的空白空间应为y
和z
.这是A
在内存中的样子:
For A
: The alignment of float4
has to be 16
and since float4
has to be after the normal float
s, there is going to be 8
bytes of empty space between y
and z
. Here is what A
looks like in memory:
Address | 0x200 | 0x204 | 0x208 | 0x20c | 0x210 | 0x214 | 0x218 | 0x21c |
Content | x | y | - | - | z1 | z2 | z3 | z4 |
^Has to be 16 byte aligned
对于B
:packed_float4
的对齐方式是4
,与float
相同,因此在任何情况下都可以紧随float
之后:
For B
: Alignment of packed_float4
is 4
, the same as float
, so it can follow right after the float
s in any case:
Address | 0x200 | 0x204 | 0x208 | 0x20c | 0x210 | 0x214 |
Content | x | y | z1 | z2 | z3 | z4 |
如您所见,A
占用32
个字节,而B
只使用24
个字节.当您拥有这些结构的数组时,A
将为每个元素占用8
个更多的字节.因此,为了传递大量数据,首选后者.
As you can see, A
takes up 32
bytes whereas B
only uses 24
bytes. When you have an array of those structs, A
will take up 8
more bytes for every element. So for passing around a lot of data, the latter is preferred.
根本需要float4
的原因是因为GPU无法处理4
字节对齐的packed_float4
s,因此您将无法在着色器中返回packed_float4
.我认为这是因为性能.
The reason you need float4
at all is because the GPU can't handle 4
byte aligned packed_float4
s, you won't be able to return packed_float4
in a shader. This is because of performance I assume.
最后一件事:当您声明结构的Swift版本时:
One last thing: When you declare the Swift version of a struct:
struct S {
let x, y: Float
let z : (Float, Float, Float, Float)
}
此结构在Metal中等于B
,而在不是 A
中等于.元组就像packed_floatN
.
This struct will be equal to B
in Metal and not A
. A tuple is like a packed_floatN
.
所有这些同样适用于其他向量类型,例如packed_float3
,packed_short2
等.
All of this also applies to other vector types such as packed_float3
, packed_short2
, ect.
这篇关于打包数据类型与普通数据类型之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!