Skip to content

Commit 6be9f67

Browse files
authored
Merge pull request #4 from marijnfs/master
Fix an early deinit that causes crash, update to [_] syntax
2 parents 1406663 + a9605ea commit 6be9f67

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

build.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const std = @import("std");
2-
const path = std.os.path;
2+
const path = std.fs.path;
33
const Builder = std.build.Builder;
44

55
pub fn build(b: *Builder) !void {
@@ -25,10 +25,10 @@ fn addShader(b: *Builder, exe: var, in_file: []const u8, out_file: []const u8) !
2525
// example:
2626
// glslc -o shaders/vert.spv shaders/shader.vert
2727
const dirname = "shaders";
28-
const full_in = try path.join(b.allocator, [][]const u8{ dirname, in_file });
29-
const full_out = try path.join(b.allocator, [][]const u8{ dirname, out_file });
28+
const full_in = try path.join(b.allocator, [_][]const u8{ dirname, in_file });
29+
const full_out = try path.join(b.allocator, [_][]const u8{ dirname, out_file });
3030

31-
const run_cmd = b.addSystemCommand([][]const u8{
31+
const run_cmd = b.addSystemCommand([_][]const u8{
3232
"glslc",
3333
"-o",
3434
full_out,

src/main.zig

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const HEIGHT = 600;
1111
const MAX_FRAMES_IN_FLIGHT = 2;
1212

1313
const enableValidationLayers = std.debug.runtime_safety;
14-
const validationLayers = [][*]const u8{c"VK_LAYER_LUNARG_standard_validation"};
15-
const deviceExtensions = [][*]const u8{c.VK_KHR_SWAPCHAIN_EXTENSION_NAME};
14+
const validationLayers = [_][*]const u8{c"VK_LAYER_LUNARG_standard_validation"};
15+
const deviceExtensions = [_][*]const u8{c.VK_KHR_SWAPCHAIN_EXTENSION_NAME};
1616

1717
var currentFrame: usize = 0;
1818
var instance: c.VkInstance = undefined;
@@ -170,7 +170,7 @@ fn createCommandBuffers(allocator: *Allocator) !void {
170170

171171
try checkSuccess(c.vkBeginCommandBuffer(commandBuffers[i], &beginInfo));
172172

173-
const clearColor = c.VkClearValue{ .color = c.VkClearColorValue{ .float32 = []f32{ 0.0, 0.0, 0.0, 1.0 } } };
173+
const clearColor = c.VkClearValue{ .color = c.VkClearColorValue{ .float32 = [_]f32{ 0.0, 0.0, 0.0, 1.0 } } };
174174

175175
const renderPassInfo = c.VkRenderPassBeginInfo{
176176
.sType = c.VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
@@ -236,7 +236,7 @@ fn createFramebuffers(allocator: *Allocator) !void {
236236
swapChainFramebuffers = try allocator.alloc(c.VkFramebuffer, swapChainImageViews.len);
237237

238238
for (swapChainImageViews) |swap_chain_image_view, i| {
239-
const attachments = []c.VkImageView{swap_chain_image_view};
239+
const attachments = [_]c.VkImageView{swap_chain_image_view};
240240

241241
const framebufferInfo = c.VkFramebufferCreateInfo{
242242
.sType = c.VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
@@ -303,7 +303,7 @@ fn createGraphicsPipeline(allocator: *Allocator) !void {
303303
.pSpecializationInfo = null,
304304
};
305305

306-
const shaderStages = []c.VkPipelineShaderStageCreateInfo{ vertShaderStageInfo, fragShaderStageInfo };
306+
const shaderStages = [_]c.VkPipelineShaderStageCreateInfo{ vertShaderStageInfo, fragShaderStageInfo };
307307

308308
const vertexInputInfo = c.VkPipelineVertexInputStateCreateInfo{
309309
.sType = c.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
@@ -324,7 +324,7 @@ fn createGraphicsPipeline(allocator: *Allocator) !void {
324324
.flags = 0,
325325
};
326326

327-
const viewport = []c.VkViewport{c.VkViewport{
327+
const viewport = [_]c.VkViewport{c.VkViewport{
328328
.x = 0.0,
329329
.y = 0.0,
330330
.width = @intToFloat(f32, swapChainExtent.width),
@@ -333,7 +333,7 @@ fn createGraphicsPipeline(allocator: *Allocator) !void {
333333
.maxDepth = 1.0,
334334
}};
335335

336-
const scissor = []c.VkRect2D{c.VkRect2D{
336+
const scissor = [_]c.VkRect2D{c.VkRect2D{
337337
.offset = c.VkOffset2D{ .x = 0, .y = 0 },
338338
.extent = swapChainExtent,
339339
}};
@@ -396,7 +396,7 @@ fn createGraphicsPipeline(allocator: *Allocator) !void {
396396
.logicOp = c.VK_LOGIC_OP_COPY,
397397
.attachmentCount = 1,
398398
.pAttachments = &colorBlendAttachment,
399-
.blendConstants = []f32{ 0, 0, 0, 0 },
399+
.blendConstants = [_]f32{ 0, 0, 0, 0 },
400400

401401
.pNext = null,
402402
.flags = 0,
@@ -414,7 +414,7 @@ fn createGraphicsPipeline(allocator: *Allocator) !void {
414414

415415
try checkSuccess(c.vkCreatePipelineLayout(global_device, &pipelineLayoutInfo, null, &pipelineLayout));
416416

417-
const pipelineInfo = []c.VkGraphicsPipelineCreateInfo{c.VkGraphicsPipelineCreateInfo{
417+
const pipelineInfo = [_]c.VkGraphicsPipelineCreateInfo{c.VkGraphicsPipelineCreateInfo{
418418
.sType = c.VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
419419
.stageCount = @intCast(u32, shaderStages.len),
420420
.pStages = &shaderStages,
@@ -468,7 +468,7 @@ fn createRenderPass() !void {
468468
.layout = c.VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
469469
};
470470

471-
const subpass = []c.VkSubpassDescription{c.VkSubpassDescription{
471+
const subpass = [_]c.VkSubpassDescription{c.VkSubpassDescription{
472472
.pipelineBindPoint = c.VK_PIPELINE_BIND_POINT_GRAPHICS,
473473
.colorAttachmentCount = 1,
474474
.pColorAttachments = (*const [1]c.VkAttachmentReference)(&colorAttachmentRef),
@@ -482,7 +482,7 @@ fn createRenderPass() !void {
482482
.pPreserveAttachments = null,
483483
}};
484484

485-
const dependency = []c.VkSubpassDependency{c.VkSubpassDependency{
485+
const dependency = [_]c.VkSubpassDependency{c.VkSubpassDependency{
486486
.srcSubpass = c.VK_SUBPASS_EXTERNAL,
487487
.dstSubpass = 0,
488488
.srcStageMask = c.VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
@@ -591,8 +591,9 @@ fn chooseSwapExtent(capabilities: c.VkSurfaceCapabilitiesKHR) c.VkExtent2D {
591591
}
592592

593593
fn createSwapChain(allocator: *Allocator) !void {
594-
const swapChainSupport = try querySwapChainSupport(allocator, physicalDevice);
595-
594+
var swapChainSupport = try querySwapChainSupport(allocator, physicalDevice);
595+
defer swapChainSupport.deinit();
596+
596597
const surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats.toSlice());
597598
const presentMode = chooseSwapPresentMode(swapChainSupport.presentModes.toSlice());
598599
const extent = chooseSwapExtent(swapChainSupport.capabilities);
@@ -605,7 +606,7 @@ fn createSwapChain(allocator: *Allocator) !void {
605606
}
606607

607608
const indices = try findQueueFamilies(allocator, physicalDevice);
608-
const queueFamilyIndices = []u32{ indices.graphicsFamily.?, indices.presentFamily.? };
609+
const queueFamilyIndices = [_]u32{ indices.graphicsFamily.?, indices.presentFamily.? };
609610

610611
const different_families = indices.graphicsFamily.? != indices.presentFamily.?;
611612

@@ -622,7 +623,7 @@ fn createSwapChain(allocator: *Allocator) !void {
622623

623624
.imageSharingMode = if (different_families) c.VK_SHARING_MODE_CONCURRENT else c.VK_SHARING_MODE_EXCLUSIVE,
624625
.queueFamilyIndexCount = if (different_families) u32(2) else u32(0),
625-
.pQueueFamilyIndices = if (different_families) &queueFamilyIndices else &([]u32{ 0, 0 }),
626+
.pQueueFamilyIndices = if (different_families) &queueFamilyIndices else &([_]u32{ 0, 0 }),
626627

627628
.preTransform = swapChainSupport.capabilities.currentTransform,
628629
.compositeAlpha = c.VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
@@ -650,7 +651,7 @@ fn createLogicalDevice(allocator: *Allocator) !void {
650651

651652
var queueCreateInfos = std.ArrayList(c.VkDeviceQueueCreateInfo).init(allocator);
652653
defer queueCreateInfos.deinit();
653-
const all_queue_families = []u32{ indices.graphicsFamily.?, indices.presentFamily.? };
654+
const all_queue_families = [_]u32{ indices.graphicsFamily.?, indices.presentFamily.? };
654655
const uniqueQueueFamilies = if (indices.graphicsFamily.? == indices.presentFamily.?)
655656
all_queue_families[0..1]
656657
else
@@ -811,7 +812,8 @@ fn isDeviceSuitable(allocator: *Allocator, device: c.VkPhysicalDevice) !bool {
811812

812813
var swapChainAdequate = false;
813814
if (extensionsSupported) {
814-
const swapChainSupport = try querySwapChainSupport(allocator, device);
815+
var swapChainSupport = try querySwapChainSupport(allocator, device);
816+
defer swapChainSupport.deinit();
815817
swapChainAdequate = swapChainSupport.formats.len != 0 and swapChainSupport.presentModes.len != 0;
816818
}
817819

@@ -820,7 +822,6 @@ fn isDeviceSuitable(allocator: *Allocator, device: c.VkPhysicalDevice) !bool {
820822

821823
fn querySwapChainSupport(allocator: *Allocator, device: c.VkPhysicalDevice) !SwapChainSupportDetails {
822824
var details = SwapChainSupportDetails.init(allocator);
823-
defer details.deinit();
824825

825826
try checkSuccess(c.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities));
826827

@@ -1018,12 +1019,12 @@ fn drawFrame() !void {
10181019
var imageIndex: u32 = undefined;
10191020
try checkSuccess(c.vkAcquireNextImageKHR(global_device, swapChain, maxInt(u64), imageAvailableSemaphores[currentFrame], null, &imageIndex));
10201021

1021-
var waitSemaphores = []c.VkSemaphore{imageAvailableSemaphores[currentFrame]};
1022-
var waitStages = []c.VkPipelineStageFlags{c.VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
1022+
var waitSemaphores = [_]c.VkSemaphore{imageAvailableSemaphores[currentFrame]};
1023+
var waitStages = [_]c.VkPipelineStageFlags{c.VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
10231024

1024-
const signalSemaphores = []c.VkSemaphore{renderFinishedSemaphores[currentFrame]};
1025+
const signalSemaphores = [_]c.VkSemaphore{renderFinishedSemaphores[currentFrame]};
10251026

1026-
var submitInfo = []c.VkSubmitInfo{c.VkSubmitInfo{
1027+
var submitInfo = [_]c.VkSubmitInfo{c.VkSubmitInfo{
10271028
.sType = c.VK_STRUCTURE_TYPE_SUBMIT_INFO,
10281029
.waitSemaphoreCount = 1,
10291030
.pWaitSemaphores = &waitSemaphores,
@@ -1038,7 +1039,7 @@ fn drawFrame() !void {
10381039

10391040
try checkSuccess(c.vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]));
10401041

1041-
const swapChains = []c.VkSwapchainKHR{swapChain};
1042+
const swapChains = [_]c.VkSwapchainKHR{swapChain};
10421043
const presentInfo = c.VkPresentInfoKHR{
10431044
.sType = c.VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
10441045

0 commit comments

Comments
 (0)