Hello all,
[EDIT]
Link to repo
Commit: 6d9e380851d327844596bad644f2e5d5ab5e3ad5 for future reference.
Context
I’m trying to render a rectangle by following the Vulkan Guide - Mesh Buffers tutorial, and in the mesh buffer section there is a Vertex struct that I defined as:
pub const Vertex = struct {
position: @Vector(3, f32),
uv_x: f32,
normal: @Vector(3, f32),
uv_y: f32,
color: @Vector(4, f32),
};
Then I upload the following vertices and indices:
const rect_vertices: [4]Vertex = .{
.{
.position = .{ 0.5, -0.5, 0.0 },
.uv_x = 0.0,
.normal = .{ 0.0, 0.0, 0.0 },
.uv_y = 0.0,
.color = .{ 0.0, 0.0, 0.0, 1.0 },
},
.{
.position = .{ 0.5, 0.5, 0.0 },
.uv_x = 0.0,
.normal = .{ 0.0, 0.0, 0.0 },
.uv_y = 0.0,
.color = .{ 0.5, 0.5, 0.5, 1.0 },
},
.{
.position = .{ -0.5, -0.5, 0.0 },
.uv_x = 0.0,
.normal = .{ 0.0, 0.0, 0.0 },
.uv_y = 0.0,
.color = .{ 1.0, 0.0, 0.0, 1.0 },
},
.{
.position = .{ -0.5, 0.5, 0.0 },
.uv_x = 0.0,
.normal = .{ 0.0, 0.0, 0.0 },
.uv_y = 0.0,
.color = .{ 0.0, 1.0, 0.0, 1.0 },
},
};
const rect_indices: [6]u32 = .{ 0, 1, 2, 2, 1, 3 };
An then all data is copies to a staging buffer:
var data: ?*anyopaque = undefined;
try self.vk_ctx.vma.memoryMap(staging.allocation, @ptrCast(&data));
defer self.vk_ctx.vma.memoryUnmap(staging.allocation);
const aligned_data: [*]Vertex = @ptrCast(@alignCast(data));
// Copy vertex buffer.
@memcpy(aligned_data, vertices);
// Copy index buffer.
const aligned_data2: [*]u32 = @ptrCast(@alignCast(@as([*]u8, @ptrCast(@alignCast(data))) + vertex_buffer_size));
@memcpy(aligned_data2, indicies);
And then some offsets and vulkan commands later everithing is copied on the GPU where it’s needed.
Problem
When I run the example I get:
And if I check the vertex data in renderdoc the data is wrong:
After that I add the extern keyword to the Vertex struct (To preserve the memory layout) and get:
And now if I check the vertex data in
rednerdoc I get:Solution?
If I change the definition of Vertex to:
pub const Vertex = extern struct {
position: [3]f32,
uv_x: f32,
normal: [3]f32,
uv_y: f32,
color: [4]f32,
};
Then I get:
And the data in
renderdoc now looks ok:Question
Why is all this happening? Shouldn’t @Vector(3, f32) and [3]f32 be the same in this case?
I also have a push constant declared as such:
pub const GPUDrawPushConstants = extern struct {
world_matrix: [4]@Vector(4, f32),
vertex_buffer: vk.DeviceAddress,
};
And looks ok in renderdoc:






