gloo gloo - 4 months ago 29
Swift Question

Difference between packed vs normal data type

In Metal what is the difference between a

packed_float4
and a
float4
?

Answer

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