Merge pull request #98 from antlilja/builtin-rework

Remove type param from cast builtins (Update to master)
This commit is contained in:
Robin Voetter
2023-06-27 14:36:57 +02:00
committed by GitHub
3 changed files with 43 additions and 43 deletions

View File

@@ -112,7 +112,7 @@ pub const GraphicsContext = struct {
self.instance = try self.vkb.createInstance(&.{ self.instance = try self.vkb.createInstance(&.{
.p_application_info = &app_info, .p_application_info = &app_info,
.enabled_extension_count = glfw_exts_count, .enabled_extension_count = glfw_exts_count,
.pp_enabled_extension_names = @ptrCast([*]const [*:0]const u8, glfw_exts), .pp_enabled_extension_names = @as([*]const [*:0]const u8, @ptrCast(glfw_exts)),
}, null); }, null);
self.vki = try InstanceDispatch.load(self.instance, self.vkb.dispatch.vkGetInstanceProcAddr); self.vki = try InstanceDispatch.load(self.instance, self.vkb.dispatch.vkGetInstanceProcAddr);
@@ -148,8 +148,8 @@ pub const GraphicsContext = struct {
pub fn findMemoryTypeIndex(self: GraphicsContext, memory_type_bits: u32, flags: vk.MemoryPropertyFlags) !u32 { pub fn findMemoryTypeIndex(self: GraphicsContext, memory_type_bits: u32, flags: vk.MemoryPropertyFlags) !u32 {
for (self.mem_props.memory_types[0..self.mem_props.memory_type_count], 0..) |mem_type, i| { for (self.mem_props.memory_types[0..self.mem_props.memory_type_count], 0..) |mem_type, i| {
if (memory_type_bits & (@as(u32, 1) << @truncate(u5, i)) != 0 and mem_type.property_flags.contains(flags)) { if (memory_type_bits & (@as(u32, 1) << @truncate(i)) != 0 and mem_type.property_flags.contains(flags)) {
return @truncate(u32, i); return @truncate(i);
} }
} }
@@ -209,7 +209,7 @@ fn initializeCandidate(vki: InstanceDispatch, candidate: DeviceCandidate) !vk.De
.queue_create_info_count = queue_count, .queue_create_info_count = queue_count,
.p_queue_create_infos = &qci, .p_queue_create_infos = &qci,
.enabled_extension_count = required_device_extensions.len, .enabled_extension_count = required_device_extensions.len,
.pp_enabled_extension_names = @ptrCast([*]const [*:0]const u8, &required_device_extensions), .pp_enabled_extension_names = @as([*]const [*:0]const u8, @ptrCast(&required_device_extensions)),
}, null); }, null);
} }
@@ -286,7 +286,7 @@ fn allocateQueues(vki: InstanceDispatch, pdev: vk.PhysicalDevice, allocator: All
var present_family: ?u32 = null; var present_family: ?u32 = null;
for (families, 0..) |properties, i| { for (families, 0..) |properties, i| {
const family = @intCast(u32, i); const family: u32 = @intCast(i);
if (graphics_family == null and properties.queue_flags.graphics_bit) { if (graphics_family == null and properties.queue_flags.graphics_bit) {
graphics_family = family; graphics_family = family;

View File

@@ -150,27 +150,27 @@ pub const Swapchain = struct {
// Step 1: Make sure the current frame has finished rendering // Step 1: Make sure the current frame has finished rendering
const current = self.currentSwapImage(); const current = self.currentSwapImage();
try current.waitForFence(self.gc); try current.waitForFence(self.gc);
try self.gc.vkd.resetFences(self.gc.dev, 1, @ptrCast([*]const vk.Fence, &current.frame_fence)); try self.gc.vkd.resetFences(self.gc.dev, 1, @ptrCast(&current.frame_fence));
// Step 2: Submit the command buffer // Step 2: Submit the command buffer
const wait_stage = [_]vk.PipelineStageFlags{.{ .top_of_pipe_bit = true }}; const wait_stage = [_]vk.PipelineStageFlags{.{ .top_of_pipe_bit = true }};
try self.gc.vkd.queueSubmit(self.gc.graphics_queue.handle, 1, &[_]vk.SubmitInfo{.{ try self.gc.vkd.queueSubmit(self.gc.graphics_queue.handle, 1, &[_]vk.SubmitInfo{.{
.wait_semaphore_count = 1, .wait_semaphore_count = 1,
.p_wait_semaphores = @ptrCast([*]const vk.Semaphore, &current.image_acquired), .p_wait_semaphores = @ptrCast(&current.image_acquired),
.p_wait_dst_stage_mask = &wait_stage, .p_wait_dst_stage_mask = &wait_stage,
.command_buffer_count = 1, .command_buffer_count = 1,
.p_command_buffers = @ptrCast([*]const vk.CommandBuffer, &cmdbuf), .p_command_buffers = @ptrCast(&cmdbuf),
.signal_semaphore_count = 1, .signal_semaphore_count = 1,
.p_signal_semaphores = @ptrCast([*]const vk.Semaphore, &current.render_finished), .p_signal_semaphores = @ptrCast(&current.render_finished),
}}, current.frame_fence); }}, current.frame_fence);
// Step 3: Present the current frame // Step 3: Present the current frame
_ = try self.gc.vkd.queuePresentKHR(self.gc.present_queue.handle, &.{ _ = try self.gc.vkd.queuePresentKHR(self.gc.present_queue.handle, &.{
.wait_semaphore_count = 1, .wait_semaphore_count = 1,
.p_wait_semaphores = @ptrCast([*]const vk.Semaphore, &current.render_finished), .p_wait_semaphores = @as([*]const vk.Semaphore, @ptrCast(&current.render_finished)),
.swapchain_count = 1, .swapchain_count = 1,
.p_swapchains = @ptrCast([*]const vk.SwapchainKHR, &self.handle), .p_swapchains = @as([*]const vk.SwapchainKHR, @ptrCast(&self.handle)),
.p_image_indices = @ptrCast([*]const u32, &self.image_index), .p_image_indices = @as([*]const u32, @ptrCast(&self.image_index)),
}); });
// Step 4: Acquire next frame // Step 4: Acquire next frame
@@ -243,7 +243,7 @@ const SwapImage = struct {
} }
fn waitForFence(self: SwapImage, gc: *const GraphicsContext) !void { fn waitForFence(self: SwapImage, gc: *const GraphicsContext) !void {
_ = try gc.vkd.waitForFences(gc.dev, 1, @ptrCast([*]const vk.Fence, &self.frame_fence), vk.TRUE, std.math.maxInt(u64)); _ = try gc.vkd.waitForFences(gc.dev, 1, @ptrCast(&self.frame_fence), vk.TRUE, std.math.maxInt(u64));
} }
}; };

View File

@@ -53,8 +53,8 @@ pub fn main() !void {
c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API); c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API);
const window = c.glfwCreateWindow( const window = c.glfwCreateWindow(
@intCast(c_int, extent.width), @intCast(extent.width),
@intCast(c_int, extent.height), @intCast(extent.height),
app_name, app_name,
null, null,
null, null,
@@ -133,9 +133,9 @@ pub fn main() !void {
var h: c_int = undefined; var h: c_int = undefined;
c.glfwGetWindowSize(window, &w, &h); c.glfwGetWindowSize(window, &w, &h);
if (state == .suboptimal or extent.width != @intCast(u32, w) or extent.height != @intCast(u32, h)) { if (state == .suboptimal or extent.width != @as(u32, @intCast(w)) or extent.height != @as(u32, @intCast(h))) {
extent.width = @intCast(u32, w); extent.width = @intCast(w);
extent.height = @intCast(u32, h); extent.height = @intCast(h);
try swapchain.recreate(extent); try swapchain.recreate(extent);
destroyFramebuffers(&gc, allocator, framebuffers); destroyFramebuffers(&gc, allocator, framebuffers);
@@ -176,7 +176,7 @@ fn uploadVertices(gc: *const GraphicsContext, pool: vk.CommandPool, buffer: vk.B
const data = try gc.vkd.mapMemory(gc.dev, staging_memory, 0, vk.WHOLE_SIZE, .{}); const data = try gc.vkd.mapMemory(gc.dev, staging_memory, 0, vk.WHOLE_SIZE, .{});
defer gc.vkd.unmapMemory(gc.dev, staging_memory); defer gc.vkd.unmapMemory(gc.dev, staging_memory);
const gpu_vertices = @ptrCast([*]Vertex, @alignCast(@alignOf(Vertex), data)); const gpu_vertices: [*]Vertex = @ptrCast(@alignCast(data));
for (vertices, 0..) |vertex, i| { for (vertices, 0..) |vertex, i| {
gpu_vertices[i] = vertex; gpu_vertices[i] = vertex;
} }
@@ -191,8 +191,8 @@ fn copyBuffer(gc: *const GraphicsContext, pool: vk.CommandPool, dst: vk.Buffer,
.command_pool = pool, .command_pool = pool,
.level = .primary, .level = .primary,
.command_buffer_count = 1, .command_buffer_count = 1,
}, @ptrCast([*]vk.CommandBuffer, &cmdbuf)); }, @ptrCast(&cmdbuf));
defer gc.vkd.freeCommandBuffers(gc.dev, pool, 1, @ptrCast([*]const vk.CommandBuffer, &cmdbuf)); defer gc.vkd.freeCommandBuffers(gc.dev, pool, 1, @ptrCast(&cmdbuf));
try gc.vkd.beginCommandBuffer(cmdbuf, &.{ try gc.vkd.beginCommandBuffer(cmdbuf, &.{
.flags = .{ .one_time_submit_bit = true }, .flags = .{ .one_time_submit_bit = true },
@@ -203,16 +203,16 @@ fn copyBuffer(gc: *const GraphicsContext, pool: vk.CommandPool, dst: vk.Buffer,
.dst_offset = 0, .dst_offset = 0,
.size = size, .size = size,
}; };
gc.vkd.cmdCopyBuffer(cmdbuf, src, dst, 1, @ptrCast([*]const vk.BufferCopy, &region)); gc.vkd.cmdCopyBuffer(cmdbuf, src, dst, 1, @ptrCast(&region));
try gc.vkd.endCommandBuffer(cmdbuf); try gc.vkd.endCommandBuffer(cmdbuf);
const si = vk.SubmitInfo{ const si = vk.SubmitInfo{
.command_buffer_count = 1, .command_buffer_count = 1,
.p_command_buffers = @ptrCast([*]const vk.CommandBuffer, &cmdbuf), .p_command_buffers = @ptrCast(&cmdbuf),
.p_wait_dst_stage_mask = undefined, .p_wait_dst_stage_mask = undefined,
}; };
try gc.vkd.queueSubmit(gc.graphics_queue.handle, 1, @ptrCast([*]const vk.SubmitInfo, &si), .null_handle); try gc.vkd.queueSubmit(gc.graphics_queue.handle, 1, @ptrCast(&si), .null_handle);
try gc.vkd.queueWaitIdle(gc.graphics_queue.handle); try gc.vkd.queueWaitIdle(gc.graphics_queue.handle);
} }
@@ -232,9 +232,9 @@ fn createCommandBuffers(
try gc.vkd.allocateCommandBuffers(gc.dev, &.{ try gc.vkd.allocateCommandBuffers(gc.dev, &.{
.command_pool = pool, .command_pool = pool,
.level = .primary, .level = .primary,
.command_buffer_count = @truncate(u32, cmdbufs.len), .command_buffer_count = @as(u32, @truncate(cmdbufs.len)),
}, cmdbufs.ptr); }, cmdbufs.ptr);
errdefer gc.vkd.freeCommandBuffers(gc.dev, pool, @truncate(u32, cmdbufs.len), cmdbufs.ptr); errdefer gc.vkd.freeCommandBuffers(gc.dev, pool, @truncate(cmdbufs.len), cmdbufs.ptr);
const clear = vk.ClearValue{ const clear = vk.ClearValue{
.color = .{ .float_32 = .{ 0, 0, 0, 1 } }, .color = .{ .float_32 = .{ 0, 0, 0, 1 } },
@@ -243,8 +243,8 @@ fn createCommandBuffers(
const viewport = vk.Viewport{ const viewport = vk.Viewport{
.x = 0, .x = 0,
.y = 0, .y = 0,
.width = @floatFromInt(f32, extent.width), .width = @as(f32, @floatFromInt(extent.width)),
.height = @floatFromInt(f32, extent.height), .height = @as(f32, @floatFromInt(extent.height)),
.min_depth = 0, .min_depth = 0,
.max_depth = 1, .max_depth = 1,
}; };
@@ -257,8 +257,8 @@ fn createCommandBuffers(
for (cmdbufs, framebuffers) |cmdbuf, framebuffer| { for (cmdbufs, framebuffers) |cmdbuf, framebuffer| {
try gc.vkd.beginCommandBuffer(cmdbuf, &.{}); try gc.vkd.beginCommandBuffer(cmdbuf, &.{});
gc.vkd.cmdSetViewport(cmdbuf, 0, 1, @ptrCast([*]const vk.Viewport, &viewport)); gc.vkd.cmdSetViewport(cmdbuf, 0, 1, @ptrCast(&viewport));
gc.vkd.cmdSetScissor(cmdbuf, 0, 1, @ptrCast([*]const vk.Rect2D, &scissor)); gc.vkd.cmdSetScissor(cmdbuf, 0, 1, @ptrCast(&scissor));
// This needs to be a separate definition - see https://github.com/ziglang/zig/issues/7627. // This needs to be a separate definition - see https://github.com/ziglang/zig/issues/7627.
const render_area = vk.Rect2D{ const render_area = vk.Rect2D{
@@ -271,12 +271,12 @@ fn createCommandBuffers(
.framebuffer = framebuffer, .framebuffer = framebuffer,
.render_area = render_area, .render_area = render_area,
.clear_value_count = 1, .clear_value_count = 1,
.p_clear_values = @ptrCast([*]const vk.ClearValue, &clear), .p_clear_values = @as([*]const vk.ClearValue, @ptrCast(&clear)),
}, .@"inline"); }, .@"inline");
gc.vkd.cmdBindPipeline(cmdbuf, .graphics, pipeline); gc.vkd.cmdBindPipeline(cmdbuf, .graphics, pipeline);
const offset = [_]vk.DeviceSize{0}; const offset = [_]vk.DeviceSize{0};
gc.vkd.cmdBindVertexBuffers(cmdbuf, 0, 1, @ptrCast([*]const vk.Buffer, &buffer), &offset); gc.vkd.cmdBindVertexBuffers(cmdbuf, 0, 1, @ptrCast(&buffer), &offset);
gc.vkd.cmdDraw(cmdbuf, vertices.len, 1, 0, 0); gc.vkd.cmdDraw(cmdbuf, vertices.len, 1, 0, 0);
gc.vkd.cmdEndRenderPass(cmdbuf); gc.vkd.cmdEndRenderPass(cmdbuf);
@@ -287,7 +287,7 @@ fn createCommandBuffers(
} }
fn destroyCommandBuffers(gc: *const GraphicsContext, pool: vk.CommandPool, allocator: Allocator, cmdbufs: []vk.CommandBuffer) void { fn destroyCommandBuffers(gc: *const GraphicsContext, pool: vk.CommandPool, allocator: Allocator, cmdbufs: []vk.CommandBuffer) void {
gc.vkd.freeCommandBuffers(gc.dev, pool, @truncate(u32, cmdbufs.len), cmdbufs.ptr); gc.vkd.freeCommandBuffers(gc.dev, pool, @truncate(cmdbufs.len), cmdbufs.ptr);
allocator.free(cmdbufs); allocator.free(cmdbufs);
} }
@@ -302,7 +302,7 @@ fn createFramebuffers(gc: *const GraphicsContext, allocator: Allocator, render_p
fb.* = try gc.vkd.createFramebuffer(gc.dev, &.{ fb.* = try gc.vkd.createFramebuffer(gc.dev, &.{
.render_pass = render_pass, .render_pass = render_pass,
.attachment_count = 1, .attachment_count = 1,
.p_attachments = @ptrCast([*]const vk.ImageView, &swapchain.swap_images[i].view), .p_attachments = @as([*]const vk.ImageView, @ptrCast(&swapchain.swap_images[i].view)),
.width = swapchain.extent.width, .width = swapchain.extent.width,
.height = swapchain.extent.height, .height = swapchain.extent.height,
.layers = 1, .layers = 1,
@@ -338,14 +338,14 @@ fn createRenderPass(gc: *const GraphicsContext, swapchain: Swapchain) !vk.Render
const subpass = vk.SubpassDescription{ const subpass = vk.SubpassDescription{
.pipeline_bind_point = .graphics, .pipeline_bind_point = .graphics,
.color_attachment_count = 1, .color_attachment_count = 1,
.p_color_attachments = @ptrCast([*]const vk.AttachmentReference, &color_attachment_ref), .p_color_attachments = @ptrCast(&color_attachment_ref),
}; };
return try gc.vkd.createRenderPass(gc.dev, &.{ return try gc.vkd.createRenderPass(gc.dev, &.{
.attachment_count = 1, .attachment_count = 1,
.p_attachments = @ptrCast([*]const vk.AttachmentDescription, &color_attachment), .p_attachments = @as([*]const vk.AttachmentDescription, @ptrCast(&color_attachment)),
.subpass_count = 1, .subpass_count = 1,
.p_subpasses = @ptrCast([*]const vk.SubpassDescription, &subpass), .p_subpasses = @as([*]const vk.SubpassDescription, @ptrCast(&subpass)),
}, null); }, null);
} }
@@ -356,13 +356,13 @@ fn createPipeline(
) !vk.Pipeline { ) !vk.Pipeline {
const vert = try gc.vkd.createShaderModule(gc.dev, &.{ const vert = try gc.vkd.createShaderModule(gc.dev, &.{
.code_size = shaders.triangle_vert.len, .code_size = shaders.triangle_vert.len,
.p_code = @ptrCast([*]const u32, &shaders.triangle_vert), .p_code = @as([*]const u32, @ptrCast(&shaders.triangle_vert)),
}, null); }, null);
defer gc.vkd.destroyShaderModule(gc.dev, vert, null); defer gc.vkd.destroyShaderModule(gc.dev, vert, null);
const frag = try gc.vkd.createShaderModule(gc.dev, &.{ const frag = try gc.vkd.createShaderModule(gc.dev, &.{
.code_size = shaders.triangle_frag.len, .code_size = shaders.triangle_frag.len,
.p_code = @ptrCast([*]const u32, &shaders.triangle_frag), .p_code = @as([*]const u32, @ptrCast(&shaders.triangle_frag)),
}, null); }, null);
defer gc.vkd.destroyShaderModule(gc.dev, frag, null); defer gc.vkd.destroyShaderModule(gc.dev, frag, null);
@@ -381,7 +381,7 @@ fn createPipeline(
const pvisci = vk.PipelineVertexInputStateCreateInfo{ const pvisci = vk.PipelineVertexInputStateCreateInfo{
.vertex_binding_description_count = 1, .vertex_binding_description_count = 1,
.p_vertex_binding_descriptions = @ptrCast([*]const vk.VertexInputBindingDescription, &Vertex.binding_description), .p_vertex_binding_descriptions = @ptrCast(&Vertex.binding_description),
.vertex_attribute_description_count = Vertex.attribute_description.len, .vertex_attribute_description_count = Vertex.attribute_description.len,
.p_vertex_attribute_descriptions = &Vertex.attribute_description, .p_vertex_attribute_descriptions = &Vertex.attribute_description,
}; };
@@ -434,7 +434,7 @@ fn createPipeline(
.logic_op_enable = vk.FALSE, .logic_op_enable = vk.FALSE,
.logic_op = .copy, .logic_op = .copy,
.attachment_count = 1, .attachment_count = 1,
.p_attachments = @ptrCast([*]const vk.PipelineColorBlendAttachmentState, &pcbas), .p_attachments = @ptrCast(&pcbas),
.blend_constants = [_]f32{ 0, 0, 0, 0 }, .blend_constants = [_]f32{ 0, 0, 0, 0 },
}; };
@@ -470,9 +470,9 @@ fn createPipeline(
gc.dev, gc.dev,
.null_handle, .null_handle,
1, 1,
@ptrCast([*]const vk.GraphicsPipelineCreateInfo, &gpci), @ptrCast(&gpci),
null, null,
@ptrCast([*]vk.Pipeline, &pipeline), @ptrCast(&pipeline),
); );
return pipeline; return pipeline;
} }