// from:
// https://github.com/Snektron/vulkan-zig/blob/f69af7a9248982cf7536aaf32111843f1a4fa4a9/examples/triangle.zig#L8-L9
const vert_spv align(@alignOf(u32)) = @embedFile("vertex_shader").*;
const frag_spv align(@alignOf(u32)) = @embedFile("fragment_shader").*;
```I'm trying to understand these two lines ๐
The end goal is to pass a [`{*u32, len}` to a C library](https://codeberg.org/heysokam/cvulkan/src/commit/a910a8c7f03cc289be1aab44d0580fb702f52d8b/src/cvulkan/types.h#L410-L413),
which I was assuming I could achieve by `@embedFile` directly into a `[]u32` variable
But, apparently embedFile gives you `[:0]u8`, not `u32`...
So, I remembered that the vulkan-zig bindings do the above.
But, I'm completely clueless as to what those two lines above even do
_(note: My end goal is similar to those bindings above, but not the same._
_I have my own vulkan library in C, and also a Zig wrapper for my C library)_
The main issue I have is that the type is not explicitly listed
All I know is that it is somehow dereferencing the u8 slice.... but..... what about everything else?
What is the resulting type of this even? ๐ค
#[solved] Embedding u32 slice from file
1 messages ยท Page 1 of 1 (latest)
According to the docs, @embedFile returns a pointer to an array and not a slice, so dereferencing it "copies" the array (I don't know why @embedFile does not simply return a slice).
I'm not sure about the align thing though (why not just cast to a u32 array/slice?).
const vertex :[]const u32= std.mem.bytesAsSlice(u32, @embedFile("./vert.spv"));
const fragment :[]const u32= std.mem.bytesAsSlice(u32, @embedFile("./frag.spv"));
```_I just found this reading the documentation, but I havent tested it and I don't know if it will work at comptime_
Yeah, this is the way I'd do it; I can't see a reason to use the dereference and align syntax instead.
mem.zig:4255:49: error: exact division produced remainder
return @as(cast_target, @ptrCast(bytes))[0..@divExact(bytes.len, @sizeOf(T))];
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tech.zig:59:52: note: called at comptime here
const vertex :[]const u32= std.mem.bytesAsSlice(u32, @embedFile("./vert.spv")) ;
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```~~Do you know anything about this?~~ ๐ค
Oh, nvm, I think my temp file was broken. Removed one character and it went past that error
I may just found an explanation of why the align/alignof
tech.zig:59:52: error: expected type '[]const u32', found '[]align(1) const u32'
const vertex :[]const u32= std.mem.bytesAsSlice(u32, @embedFile("./mess.vert.spv")[0..]) ;
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tech.zig:59:52: note: pointer alignment '1' cannot cast into pointer alignment '4'
const vertex :[]const u32= @alignCast(std.mem.bytesAsSlice(u32, @embedFile("./vert.spv")));
```This seems to get rid of that error. No idea if it works as intended, though
if you need something aligned then the snippet in the OP is the correct way to do it, anything else is wrong or will only sometimes work
what is wrong with @alignCast(std.mem.bytesAsSlice(u32, @embedFile("./vert.spv")));? ๐ค
alignCast is just an assertion it doesnt actually change anything about the alignment, if the pointer returned by @embedFile isnt already aligned to 4 youll get an error
So, how do I @embedFile a u32 binary file into a []u32?
I don't want a [*]u32, I need a slice ๐ค
the original thing you sent
That is not a slice?
itll be an array, just pass a pointer to it
or you could make another variable that is the pointer
const vertex_array align(4) = @embedFile(...).*;
const vertex = &vertex_array;
If we go back to the original question, this doesn't address it
what doesnt it answer
The main issue I have is that the type is not explicitly listed
All I know is that it is somehow dereferencing the u8 slice.... but..... what about everything else?
What is the resulting type of this even? ๐ค
It is infering a type, and I need to use it, not just have it blindly sitting there in the file
therefore I need to understand what it contains
it contains the file, im not sure i understand what you mean
its the same idea as a string literal
@TypeOf("foo") == *const [3:0]u8
But I don't want a string literal. I'm passing this to my C library, not to vulkan
therefore I need a {*u32, len}
const vertex_array align(4) :????????= @embedFile(...).*;
its an array of u8s, the length is however big the file is
i forgot you needed u32 so this exact code wont work, youll need to cast it, but the dereferencing is needed if you need it to be aligned properly
const array align(4) = @embedFile("foo").*;
const ptr: []const u32 = @ptrCast(&array);
something like this
That seemed to work so far. ty โ๏ธ๐