forked from mirror/vulkan-zig
zig fmt **.zig
This commit is contained in:
@@ -3,9 +3,7 @@ const vk = @import("vulkan");
|
||||
const c = @import("c.zig");
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const required_device_extensions = [_][]const u8{
|
||||
vk.extension_info.khr_swapchain.name
|
||||
};
|
||||
const required_device_extensions = [_][]const u8{vk.extension_info.khr_swapchain.name};
|
||||
|
||||
const BaseDispatch = struct {
|
||||
vkCreateInstance: vk.PfnCreateInstance,
|
||||
@@ -151,11 +149,11 @@ pub const GraphicsContext = struct {
|
||||
|
||||
pub fn deviceName(self: GraphicsContext) []const u8 {
|
||||
const len = std.mem.indexOfScalar(u8, &self.props.device_name, 0).?;
|
||||
return self.props.device_name[0 .. len];
|
||||
return self.props.device_name[0..len];
|
||||
}
|
||||
|
||||
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]) |mem_type, i| {
|
||||
for (self.mem_props.memory_types[0..self.mem_props.memory_type_count]) |mem_type, i| {
|
||||
if (memory_type_bits & (@as(u32, 1) << @truncate(u5, i)) != 0 and mem_type.property_flags.contains(flags)) {
|
||||
return @truncate(u32, i);
|
||||
}
|
||||
@@ -207,13 +205,13 @@ fn initializeCandidate(vki: InstanceDispatch, candidate: DeviceCandidate) !vk.De
|
||||
.queue_family_index = candidate.queues.present_family,
|
||||
.queue_count = 1,
|
||||
.p_queue_priorities = &priority,
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const queue_count: u32 = if (candidate.queues.graphics_family == candidate.queues.present_family)
|
||||
1
|
||||
else
|
||||
2;
|
||||
1
|
||||
else
|
||||
2;
|
||||
|
||||
return try vki.createDevice(candidate.pdev, .{
|
||||
.flags = .{},
|
||||
@@ -281,19 +279,14 @@ fn checkSuitable(
|
||||
return DeviceCandidate{
|
||||
.pdev = pdev,
|
||||
.props = props,
|
||||
.queues = allocation
|
||||
.queues = allocation,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
fn allocateQueues(
|
||||
vki: InstanceDispatch,
|
||||
pdev: vk.PhysicalDevice,
|
||||
allocator: *Allocator,
|
||||
surface: vk.SurfaceKHR
|
||||
) !?QueueAllocation {
|
||||
fn allocateQueues(vki: InstanceDispatch, pdev: vk.PhysicalDevice, allocator: *Allocator, surface: vk.SurfaceKHR) !?QueueAllocation {
|
||||
var family_count: u32 = undefined;
|
||||
vki.getPhysicalDeviceQueueFamilyProperties(pdev, &family_count, null);
|
||||
|
||||
@@ -307,7 +300,7 @@ fn allocateQueues(
|
||||
for (families) |properties, i| {
|
||||
const family = @intCast(u32, i);
|
||||
|
||||
if (graphics_family == null and properties.queue_flags.contains(.{.graphics_bit = true})) {
|
||||
if (graphics_family == null and properties.queue_flags.graphics_bit) {
|
||||
graphics_family = family;
|
||||
}
|
||||
|
||||
@@ -319,7 +312,7 @@ fn allocateQueues(
|
||||
if (graphics_family != null and present_family != null) {
|
||||
return QueueAllocation{
|
||||
.graphics_family = graphics_family.?,
|
||||
.present_family = present_family.?
|
||||
.present_family = present_family.?,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -352,7 +345,7 @@ fn checkExtensionSupport(
|
||||
for (required_device_extensions) |ext| {
|
||||
for (propsv) |props| {
|
||||
const len = std.mem.indexOfScalar(u8, &props.extension_name, 0).?;
|
||||
const prop_ext_name = props.extension_name[0 .. len];
|
||||
const prop_ext_name = props.extension_name[0..len];
|
||||
if (std.mem.eql(u8, ext, prop_ext_name)) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ pub const Swapchain = struct {
|
||||
}
|
||||
|
||||
const concurrent = gc.graphics_queue.family != gc.present_queue.family;
|
||||
const qfi = [_]u32{gc.graphics_queue.family, gc.present_queue.family};
|
||||
const qfi = [_]u32{ gc.graphics_queue.family, gc.present_queue.family };
|
||||
|
||||
const handle = try gc.vkd.createSwapchainKHR(gc.dev, .{
|
||||
.flags = .{},
|
||||
@@ -51,12 +51,12 @@ pub const Swapchain = struct {
|
||||
.image_color_space = surface_format.color_space,
|
||||
.image_extent = actual_extent,
|
||||
.image_array_layers = 1,
|
||||
.image_usage = .{.color_attachment_bit = true, .transfer_dst_bit = true},
|
||||
.image_usage = .{ .color_attachment_bit = true, .transfer_dst_bit = true },
|
||||
.image_sharing_mode = if (concurrent) .concurrent else .exclusive,
|
||||
.queue_family_index_count = qfi.len,
|
||||
.p_queue_family_indices = &qfi,
|
||||
.pre_transform = caps.current_transform,
|
||||
.composite_alpha = .{.opaque_bit_khr = true},
|
||||
.composite_alpha = .{ .opaque_bit_khr = true },
|
||||
.present_mode = present_mode,
|
||||
.clipped = vk.TRUE,
|
||||
.old_swapchain = old_handle,
|
||||
@@ -71,7 +71,7 @@ pub const Swapchain = struct {
|
||||
const swap_images = try initSwapchainImages(gc, handle, surface_format.format, allocator);
|
||||
errdefer for (swap_images) |si| si.deinit(gc);
|
||||
|
||||
var next_image_acquired = try gc.vkd.createSemaphore(gc.dev, .{.flags = .{}}, null);
|
||||
var next_image_acquired = try gc.vkd.createSemaphore(gc.dev, .{ .flags = .{} }, null);
|
||||
errdefer gc.vkd.destroySemaphore(gc.dev, next_image_acquired, null);
|
||||
|
||||
const result = try gc.vkd.acquireNextImageKHR(gc.dev, handle, std.math.maxInt(u64), next_image_acquired, .null_handle);
|
||||
@@ -147,7 +147,7 @@ pub const Swapchain = struct {
|
||||
try self.gc.vkd.resetFences(self.gc.dev, 1, @ptrCast([*]const vk.Fence, ¤t.frame_fence));
|
||||
|
||||
// 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{.{
|
||||
.wait_semaphore_count = 1,
|
||||
.p_wait_semaphores = @ptrCast([*]const vk.Semaphore, ¤t.image_acquired),
|
||||
@@ -201,9 +201,9 @@ const SwapImage = struct {
|
||||
.image = image,
|
||||
.view_type = .@"2d",
|
||||
.format = format,
|
||||
.components = .{.r = .identity, .g = .identity, .b = .identity, .a = .identity},
|
||||
.components = .{ .r = .identity, .g = .identity, .b = .identity, .a = .identity },
|
||||
.subresource_range = .{
|
||||
.aspect_mask = .{.color_bit = true},
|
||||
.aspect_mask = .{ .color_bit = true },
|
||||
.base_mip_level = 0,
|
||||
.level_count = 1,
|
||||
.base_array_layer = 0,
|
||||
@@ -212,13 +212,13 @@ const SwapImage = struct {
|
||||
}, null);
|
||||
errdefer gc.vkd.destroyImageView(gc.dev, view, null);
|
||||
|
||||
const image_acquired = try gc.vkd.createSemaphore(gc.dev, .{.flags = .{}}, null);
|
||||
const image_acquired = try gc.vkd.createSemaphore(gc.dev, .{ .flags = .{} }, null);
|
||||
errdefer gc.vkd.destroySemaphore(gc.dev, image_acquired, null);
|
||||
|
||||
const render_finished = try gc.vkd.createSemaphore(gc.dev, .{.flags = .{}}, null);
|
||||
const render_finished = try gc.vkd.createSemaphore(gc.dev, .{ .flags = .{} }, null);
|
||||
errdefer gc.vkd.destroySemaphore(gc.dev, image_acquired, null);
|
||||
|
||||
const frame_fence = try gc.vkd.createFence(gc.dev, .{.flags = .{.signaled_bit = true}}, null);
|
||||
const frame_fence = try gc.vkd.createFence(gc.dev, .{ .flags = .{ .signaled_bit = true } }, null);
|
||||
errdefer gc.vkd.destroyFence(gc.dev, frame_fence, null);
|
||||
|
||||
return SwapImage{
|
||||
@@ -254,7 +254,7 @@ fn initSwapchainImages(gc: *const GraphicsContext, swapchain: vk.SwapchainKHR, f
|
||||
errdefer allocator.free(images);
|
||||
|
||||
var i: usize = 0;
|
||||
errdefer for (swap_images[0 .. i]) |si| si.deinit(gc);
|
||||
errdefer for (swap_images[0..i]) |si| si.deinit(gc);
|
||||
|
||||
for (images) |image| {
|
||||
swap_images[i] = try SwapImage.init(gc, image, format);
|
||||
|
||||
@@ -35,16 +35,16 @@ const Vertex = struct {
|
||||
};
|
||||
|
||||
const vertices = [_]Vertex{
|
||||
.{.pos = .{0, -0.5}, .color = .{1, 0, 0}},
|
||||
.{.pos = .{0.5, 0.5}, .color = .{0, 1, 0}},
|
||||
.{.pos = .{-0.5, 0.5}, .color = .{0, 0, 1}},
|
||||
.{ .pos = .{ 0, -0.5 }, .color = .{ 1, 0, 0 } },
|
||||
.{ .pos = .{ 0.5, 0.5 }, .color = .{ 0, 1, 0 } },
|
||||
.{ .pos = .{ -0.5, 0.5 }, .color = .{ 0, 0, 1 } },
|
||||
};
|
||||
|
||||
pub fn main() !void {
|
||||
if (c.glfwInit() != c.GLFW_TRUE) return error.GlfwInitFailed;
|
||||
defer c.glfwTerminate();
|
||||
|
||||
var extent = vk.Extent2D{.width = 800, .height = 600};
|
||||
var extent = vk.Extent2D{ .width = 800, .height = 600 };
|
||||
|
||||
c.glfwWindowHint(c.GLFW_CLIENT_API, c.GLFW_NO_API);
|
||||
const window = c.glfwCreateWindow(
|
||||
@@ -52,7 +52,7 @@ pub fn main() !void {
|
||||
@intCast(c_int, extent.height),
|
||||
app_name,
|
||||
null,
|
||||
null
|
||||
null,
|
||||
) orelse return error.WindowInitFailed;
|
||||
defer c.glfwDestroyWindow(window);
|
||||
|
||||
@@ -61,7 +61,7 @@ pub fn main() !void {
|
||||
const gc = try GraphicsContext.init(allocator, app_name, window);
|
||||
defer gc.deinit();
|
||||
|
||||
std.debug.print("Using device: {s}\n", .{ gc.deviceName() });
|
||||
std.debug.print("Using device: {s}\n", .{gc.deviceName()});
|
||||
|
||||
var swapchain = try Swapchain.init(&gc, allocator, extent);
|
||||
defer swapchain.deinit();
|
||||
@@ -93,14 +93,14 @@ pub fn main() !void {
|
||||
const buffer = try gc.vkd.createBuffer(gc.dev, .{
|
||||
.flags = .{},
|
||||
.size = @sizeOf(@TypeOf(vertices)),
|
||||
.usage = .{.transfer_dst_bit = true, .vertex_buffer_bit = true},
|
||||
.usage = .{ .transfer_dst_bit = true, .vertex_buffer_bit = true },
|
||||
.sharing_mode = .exclusive,
|
||||
.queue_family_index_count = 0,
|
||||
.p_queue_family_indices = undefined,
|
||||
}, null);
|
||||
defer gc.vkd.destroyBuffer(gc.dev, buffer, null);
|
||||
const mem_reqs = gc.vkd.getBufferMemoryRequirements(gc.dev, buffer);
|
||||
const memory = try gc.allocate(mem_reqs, .{.device_local_bit = true});
|
||||
const memory = try gc.allocate(mem_reqs, .{ .device_local_bit = true });
|
||||
defer gc.vkd.freeMemory(gc.dev, memory, null);
|
||||
try gc.vkd.bindBufferMemory(gc.dev, buffer, memory, 0);
|
||||
|
||||
@@ -114,7 +114,7 @@ pub fn main() !void {
|
||||
swapchain.extent,
|
||||
render_pass,
|
||||
pipeline,
|
||||
framebuffers
|
||||
framebuffers,
|
||||
);
|
||||
defer destroyCommandBuffers(&gc, pool, allocator, cmdbufs);
|
||||
|
||||
@@ -146,13 +146,12 @@ pub fn main() !void {
|
||||
swapchain.extent,
|
||||
render_pass,
|
||||
pipeline,
|
||||
framebuffers
|
||||
framebuffers,
|
||||
);
|
||||
}
|
||||
|
||||
c.glfwSwapBuffers(window);
|
||||
c.glfwPollEvents();
|
||||
|
||||
}
|
||||
|
||||
try swapchain.waitForAllFences();
|
||||
@@ -162,14 +161,14 @@ fn uploadVertices(gc: *const GraphicsContext, pool: vk.CommandPool, buffer: vk.B
|
||||
const staging_buffer = try gc.vkd.createBuffer(gc.dev, .{
|
||||
.flags = .{},
|
||||
.size = @sizeOf(@TypeOf(vertices)),
|
||||
.usage = .{.transfer_src_bit = true},
|
||||
.usage = .{ .transfer_src_bit = true },
|
||||
.sharing_mode = .exclusive,
|
||||
.queue_family_index_count = 0,
|
||||
.p_queue_family_indices = undefined,
|
||||
}, null);
|
||||
defer gc.vkd.destroyBuffer(gc.dev, staging_buffer, null);
|
||||
const mem_reqs = gc.vkd.getBufferMemoryRequirements(gc.dev, staging_buffer);
|
||||
const staging_memory = try gc.allocate(mem_reqs, .{.host_visible_bit = true, .host_coherent_bit = true});
|
||||
const staging_memory = try gc.allocate(mem_reqs, .{ .host_visible_bit = true, .host_coherent_bit = true });
|
||||
defer gc.vkd.freeMemory(gc.dev, staging_memory, null);
|
||||
try gc.vkd.bindBufferMemory(gc.dev, staging_buffer, staging_memory, 0);
|
||||
|
||||
@@ -196,7 +195,7 @@ fn copyBuffer(gc: *const GraphicsContext, pool: vk.CommandPool, dst: vk.Buffer,
|
||||
defer gc.vkd.freeCommandBuffers(gc.dev, pool, 1, @ptrCast([*]const vk.CommandBuffer, &cmdbuf));
|
||||
|
||||
try gc.vkd.beginCommandBuffer(cmdbuf, .{
|
||||
.flags = .{.one_time_submit_bit = true},
|
||||
.flags = .{ .one_time_submit_bit = true },
|
||||
.p_inheritance_info = null,
|
||||
});
|
||||
|
||||
@@ -243,7 +242,7 @@ fn createCommandBuffers(
|
||||
errdefer gc.vkd.freeCommandBuffers(gc.dev, pool, @truncate(u32, cmdbufs.len), cmdbufs.ptr);
|
||||
|
||||
const clear = vk.ClearValue{
|
||||
.color = .{.float_32 = .{0, 0, 0, 1}},
|
||||
.color = .{ .float_32 = .{ 0, 0, 0, 1 } },
|
||||
};
|
||||
|
||||
const viewport = vk.Viewport{
|
||||
@@ -256,7 +255,7 @@ fn createCommandBuffers(
|
||||
};
|
||||
|
||||
const scissor = vk.Rect2D{
|
||||
.offset = .{.x = 0, .y = 0},
|
||||
.offset = .{ .x = 0, .y = 0 },
|
||||
.extent = extent,
|
||||
};
|
||||
|
||||
@@ -273,7 +272,7 @@ fn createCommandBuffers(
|
||||
.render_pass = render_pass,
|
||||
.framebuffer = framebuffers[i],
|
||||
.render_area = .{
|
||||
.offset = .{.x = 0, .y = 0},
|
||||
.offset = .{ .x = 0, .y = 0 },
|
||||
.extent = extent,
|
||||
},
|
||||
.clear_value_count = 1,
|
||||
@@ -297,17 +296,12 @@ fn destroyCommandBuffers(gc: *const GraphicsContext, pool: vk.CommandPool, alloc
|
||||
allocator.free(cmdbufs);
|
||||
}
|
||||
|
||||
fn createFramebuffers(
|
||||
gc: *const GraphicsContext,
|
||||
allocator: *Allocator,
|
||||
render_pass: vk.RenderPass,
|
||||
swapchain: Swapchain
|
||||
) ![]vk.Framebuffer {
|
||||
fn createFramebuffers(gc: *const GraphicsContext, allocator: *Allocator, render_pass: vk.RenderPass, swapchain: Swapchain) ![]vk.Framebuffer {
|
||||
const framebuffers = try allocator.alloc(vk.Framebuffer, swapchain.swap_images.len);
|
||||
errdefer allocator.free(framebuffers);
|
||||
|
||||
var i: usize = 0;
|
||||
errdefer for (framebuffers[0 .. i]) |fb| gc.vkd.destroyFramebuffer(gc.dev, fb, null);
|
||||
errdefer for (framebuffers[0..i]) |fb| gc.vkd.destroyFramebuffer(gc.dev, fb, null);
|
||||
|
||||
for (framebuffers) |*fb| {
|
||||
fb.* = try gc.vkd.createFramebuffer(gc.dev, .{
|
||||
@@ -334,7 +328,7 @@ fn createRenderPass(gc: *const GraphicsContext, swapchain: Swapchain) !vk.Render
|
||||
const color_attachment = vk.AttachmentDescription{
|
||||
.flags = .{},
|
||||
.format = swapchain.surface_format.format,
|
||||
.samples = .{.@"1_bit" = true},
|
||||
.samples = .{ .@"1_bit" = true },
|
||||
.load_op = .clear,
|
||||
.store_op = .store,
|
||||
.stencil_load_op = .dont_care,
|
||||
@@ -395,14 +389,14 @@ fn createPipeline(
|
||||
const pssci = [_]vk.PipelineShaderStageCreateInfo{
|
||||
.{
|
||||
.flags = .{},
|
||||
.stage = .{.vertex_bit = true},
|
||||
.stage = .{ .vertex_bit = true },
|
||||
.module = vert,
|
||||
.p_name = "main",
|
||||
.p_specialization_info = null,
|
||||
},
|
||||
.{
|
||||
.flags = .{},
|
||||
.stage = .{.fragment_bit = true},
|
||||
.stage = .{ .fragment_bit = true },
|
||||
.module = frag,
|
||||
.p_name = "main",
|
||||
.p_specialization_info = null,
|
||||
@@ -436,7 +430,7 @@ fn createPipeline(
|
||||
.depth_clamp_enable = vk.FALSE,
|
||||
.rasterizer_discard_enable = vk.FALSE,
|
||||
.polygon_mode = .fill,
|
||||
.cull_mode = .{.back_bit = true},
|
||||
.cull_mode = .{ .back_bit = true },
|
||||
.front_face = .clockwise,
|
||||
.depth_bias_enable = vk.FALSE,
|
||||
.depth_bias_constant_factor = 0,
|
||||
@@ -447,7 +441,7 @@ fn createPipeline(
|
||||
|
||||
const pmsci = vk.PipelineMultisampleStateCreateInfo{
|
||||
.flags = .{},
|
||||
.rasterization_samples = .{.@"1_bit" = true},
|
||||
.rasterization_samples = .{ .@"1_bit" = true },
|
||||
.sample_shading_enable = vk.FALSE,
|
||||
.min_sample_shading = 1,
|
||||
.p_sample_mask = null,
|
||||
@@ -463,7 +457,7 @@ fn createPipeline(
|
||||
.src_alpha_blend_factor = .one,
|
||||
.dst_alpha_blend_factor = .zero,
|
||||
.alpha_blend_op = .add,
|
||||
.color_write_mask = .{.r_bit = true, .g_bit = true, .b_bit = true, .a_bit = true},
|
||||
.color_write_mask = .{ .r_bit = true, .g_bit = true, .b_bit = true, .a_bit = true },
|
||||
};
|
||||
|
||||
const pcbsci = vk.PipelineColorBlendStateCreateInfo{
|
||||
@@ -472,10 +466,10 @@ fn createPipeline(
|
||||
.logic_op = .copy,
|
||||
.attachment_count = 1,
|
||||
.p_attachments = @ptrCast([*]const vk.PipelineColorBlendAttachmentState, &pcbas),
|
||||
.blend_constants = [_]f32{0, 0, 0, 0},
|
||||
.blend_constants = [_]f32{ 0, 0, 0, 0 },
|
||||
};
|
||||
|
||||
const dynstate = [_]vk.DynamicState{.viewport, .scissor};
|
||||
const dynstate = [_]vk.DynamicState{ .viewport, .scissor };
|
||||
const pdsci = vk.PipelineDynamicStateCreateInfo{
|
||||
.flags = .{},
|
||||
.dynamic_state_count = dynstate.len,
|
||||
@@ -506,7 +500,8 @@ fn createPipeline(
|
||||
_ = try gc.vkd.createGraphicsPipelines(
|
||||
gc.dev,
|
||||
.null_handle,
|
||||
1, @ptrCast([*]const vk.GraphicsPipelineCreateInfo, &gpci),
|
||||
1,
|
||||
@ptrCast([*]const vk.GraphicsPipelineCreateInfo, &gpci),
|
||||
null,
|
||||
@ptrCast([*]vk.Pipeline, &pipeline),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user