#how can i safely cast a `*const [N:0]u8` to `[]const u32`?
1 messages · Page 1 of 1 (latest)
actually, it might not need to be null terminated since you pass in the size of the code too
This is not really possible, since the alignment of u8 is smaller than the alignment of u32. How do you get the u8 pointer variable
from @embedFile
the question should probably be []const u8 to [*:0]const u32, the shader code doesnt need to be null terminated (and cannot be)
how can i safely cast a []const u8 to [*:0]const u32?
you can do
const aligned_data align(@alignOf(u32)) = @embedFile("file").*;
const u32_ptr: *const [@divExact(aligned_data.len, 4)]u32 = @ptrCast(&aligned_data);
Other way around: @embedFile always returns a null terminated ptr, but for vulkan the shader does not need to be null terminated.
what would be the type of aligned_data?
[N]u8, where N is the comptime known len of your shader file
oh so then could i just pass in a slice for a function?
afaik your function needs a u32 slice and not a aligned u8 slice
I don't know the complete vulkan function interface, but maybe @ptrCast(&aligned_data); is possible
Here is an example: https://github.com/Snektron/vulkan-zig/blob/master/examples/triangle.zig. See the vert_spv and frag_spv vars and how they are used.
src\main.zig:524:62: error: TODO: implement @ptrCast between slices changing the length
const vertex_shader_module = self.createShaderModule(@ptrCast(&vertex_shader_code));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
interesting error message
that vulkan binding might be doing other stuff
then do @as(*const [@divExact(vertex_shader_code.len, 4)]u32, @ptrCast(&vertex_shader_code))
did that, results in seg fault
which os?
windows
how can i safely cast a *const [N:0]u8 to []const u32?
bump
does this work?
const bytes: []const u8 = @embedFile("my_file");
const data: [@divExact(bytes.len, 4)]u32 = @bitCast(bytes[0..bytes.len].*);
_ = &data; // <- your `[]const u32`
src\main.zig:524:83: error: @bitCast size mismatch: destination type '[376]u32' has 12032 bits but source type '[1504:0]u8' has 12040 bits
const vertex_shader_bytecode: [@divExact(vertex_shader_code.len, 4)]u32 = @bitCast(vertex_shader_code[0..vertex_shader_code.len].*);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
oh thats 8 bits off
so kinda close?
oh i did see that
we need to rid of the sentinel terminator
compiles, but is UB
UB how?
seg fault
that's in a differnet part of your program then.
the whole calculation is done at compile time, no UB comes from there.
If pCode is a pointer to SPIR-V code, pCode must adhere to the validation rules described by the Validation Rules within a Module section of the SPIR-V Environment appendix
hmm yeah this is what the validation layer says