Hello. In one code example I found next code in two files.
In first (One.swift)
private struct Vertex {
var position: SIMD4<Float>
var color: SIMD4<Float>
}
In second (Two.metal)
struct Vertex {
float4 position [[position]];
float4 color;
};
vertex Vertex main_vertex(device Vertex* vertices [[buffer(0)]], uint vid [[vertex_id]]) {
return vertices[vid];
}
So is it true that in memory these structures have same layout, and if we declare properties with same types and order it will work guaranteed?
cukr
December 5, 2019, 11:35am
3
C structs don't have to have the same layout as swift structs, for example Compiler Explorer
If you want to have a struct with a C memory layout, the solution is to write it in C and import it using a bridging header
Thanks. I assemble an app via SPM and there is no way to mix Swift and Obj-C in one package as I know. So I will research a MTLBuffer and hope that data will match.
Lantua
December 5, 2019, 12:43pm
5
I have a simple struct (nested struct of primitive types/enum) and want to load from/save to large file in chunks, sometimes in time-critical region. Right now I'm exploring my options.
I could use Codable, but JSON doesn't seem to support random access (you have to parse the entire file). I don't want to load the entire file into memory. I could separate each chunk and encode/decode them independently, but then I don't know the upper limit of the encoded data size.
I could implement Binar…
Also similar topic about the layout of data. Essentially there’s little one can say about the memory layout.
2 Likes