Difference between packed vs normal data type

↘锁芯ラ 提交于 2020-01-12 07:33:14

问题


In Metal what is the difference between a packed_float4 and a float4?


回答1:


This information is from here

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 on the other hand has an alignment of 4 bytes. Last digit of the address will be 0, 4, 8 or c

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;
}

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

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  |

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.

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.

One last thing: When you declare the Swift version of a struct:

struct S {
    let x, y: Float
    let z : (Float, Float, Float, Float)
}

This struct will be equal to B in Metal and not A. A tuple is like a packed_floatN.

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




回答2:


From what I gather from the documentation:

Packed datatypes are arrays, whereas their unpacked counter parts are structs.

Here's their usage:

packed_float4 packedFloat4;
packedFloat4[0] = 0.0f;
packedFloat4[1] = 1.0f;
packedFloat4[2] = 2.0f;
packedFloat4[3] = 3.0f;

packed_float4 anotherPackedFloat4 = [0.0f, 1.0f, 2.0f, 3.0f] //array initalizer

//vs.

float4 f;
f.x = 0; //equivalent to f.r = 0;
f.y = 1; //equivalent to f.g = 1;
f.z = 2; //equivalent to f.b = 2;
f.w = 3; //equivalent to f.a = 3;

float4 anotherFloat4 = float4(0.0f, 1.0f, 2.0f, 3.0f) //constructor call


来源:https://stackoverflow.com/questions/38773807/difference-between-packed-vs-normal-data-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!