The difference between packed and normal data type

In Metal, what is the difference between a packed_float4and a float4?

+4
source share
2 answers

This information is from here.

float4has 16byte alignment . This means that a memory address of this type (for example, 0x12345670) will be divided by 16(as well as the last hexadecimal digit 0).

packed_float4on the other hand has alignment 4 bytes. The last digit of the address will be 0, 4, 8orc

. , 2 float 1 float4/packed_float4:

struct A{
    float x, y;
    float4 z;
}

struct B{
    float x, y;
    packed_float4 z;
}

A: float4 16, float4 float s, 8 y z. A :

 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 :

 Address | 0x200 | 0x204 | 0x208 | 0x20c | 0x210 | 0x214 |
 Content |   x   |   y   |   z1  |   z2  |   z3  |   z4  |

, A 32 , B 24 . , A 8 . .

, float4 , , GPU 4 , packed_float4 s, packed_float4 . - , .

: Swift :

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

B , A. packed_floatN.

, packed_float3, packed_short2, ect.

+11

, :

, .

:

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
-1

Source: https://habr.com/ru/post/1650209/


All Articles