打包数据类型与普通数据类型之间的区别 [英] Difference between packed vs normal data type

查看:108
本文介绍了打包数据类型与普通数据类型之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在《金属》中,packed_float4float4有什么区别?

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.地址的最后一位是048c

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 floats 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字节之间的空白空间应为yz.这是A在内存中的样子:

For A: The alignment of float4 has to be 16 and since float4 has to be after the normal floats, 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 floats 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_float4s, 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_float3packed_short2等.

All of this also applies to other vector types such as packed_float3, packed_short2, ect.

这篇关于打包数据类型与普通数据类型之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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