Hello, developers,
I'm implementing slice rendering of 3d volume.
And then, I have a simple question...
I use a simple vertex buffer type both in swift code and in metal code. Firstly, I defined uv to float2 but it's not working. It has weird texture coordinates when I use float2...
public struct VertexIn: sizeable {
var position = float3()
var normal = float3()
var uv = float3()
}
struct VertexIn {
float3 position [[ attribute(0) ]];
float3 normal [[ attribute(1) ]];
float3 uv [[ attribute(2) ]];
};
like this
float2.
float3.
It has just difference at the uv type. And I have same issue at passing uniform to shader. When I pass uniform that includes float or short types it doesn't work. So I change type to float3... So I inquire that metal data type is so difference compared with swift type??? Or what types are same and supported from metal.
Hello,
It is possible that you have a type mismatch (that is producing a memory layout mismatch), but in any case, you should not be binding Swift structs to your Metal shader.
It is not safe to bind Swift structs to Metal shaders because Swift struct memory layout is not guaranteed, and so even if your Swift struct happens to match the layout of a C struct today (which won't always be the case even today), it could break in the future. Therefore, the only correct way to do this is to define the struct in C, and bridge it into Swift (which is also advantageous in its own right, since you only need to manage a single struct representing your data instead of two.)