#[solved] Embedding u32 slice from file

1 messages ยท Page 1 of 1 (latest)

still nest
#
// 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? ๐Ÿค”
dreamy musk
#

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?).

still nest
#
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_
dreamy musk
still nest
# dreamy musk Yeah, this is the way I'd do it; I can't see a reason to use the dereference and...
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

still nest
#
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
stiff canyon
#

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

still nest
stiff canyon
#

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

still nest
stiff canyon
#

the original thing you sent

still nest
#

That is not a slice?

stiff canyon
#

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;
still nest
stiff canyon
#

what doesnt it answer

still nest
#

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

stiff canyon
#

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

still nest
#

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}

still nest
stiff canyon
#

its an array of u8s, the length is however big the file is

stiff canyon
#
const array align(4) = @embedFile("foo").*;
const ptr: []const u32 = @ptrCast(&array);

something like this

still nest
#

That seemed to work so far. ty โœ๏ธ๐Ÿ™