Skip to content

Commit 1c3eab6

Browse files
committed
ported createSyncObjects function
1 parent 5e04389 commit 1c3eab6

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/main.zig

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const c = @import("vulkan.zig");
77
const WIDTH = 800;
88
const HEIGHT = 600;
99

10+
const MAX_FRAMES_IN_FLIGHT = 2;
11+
1012
const enableValidationLayers = std.debug.runtime_safety;
1113
const validationLayers = [][*]const u8{c"VK_LAYER_LUNARG_standard_validation"};
1214
const deviceExtensions = [][*]const u8{c.VK_KHR_SWAPCHAIN_EXTENSION_NAME};
1315

14-
var imageAvailableSemaphores: std.ArrayList(c.VkSemaphore) = undefined;
15-
var renderFinishedSemaphores: std.ArrayList(c.VkSemaphore) = undefined;
1616
var inflightFences: std.ArrayList(c.VkFence) = undefined;
1717
var currentFrame: usize = 0;
1818
var instance: c.VkInstance = undefined;
@@ -34,6 +34,10 @@ var swapChainFramebuffers: []c.VkFramebuffer = undefined;
3434
var commandPool: c.VkCommandPool = undefined;
3535
var commandBuffers: []c.VkCommandBuffer = undefined;
3636

37+
var imageAvailableSemaphores: [MAX_FRAMES_IN_FLIGHT]c.VkSemaphore = undefined;
38+
var renderFinishedSemaphores: [MAX_FRAMES_IN_FLIGHT]c.VkSemaphore = undefined;
39+
var inFlightFences: [MAX_FRAMES_IN_FLIGHT]c.VkFence = undefined;
40+
3741
const QueueFamilyIndices = struct {
3842
graphicsFamily: ?u32,
3943
presentFamily: ?u32,
@@ -107,8 +111,7 @@ fn initVulkan(allocator: *Allocator, window: *c.GLFWwindow) !void {
107111
try createFramebuffers(allocator);
108112
try createCommandPool(allocator);
109113
try createCommandBuffers(allocator);
110-
// TODO
111-
//createSyncObjects();
114+
try createSyncObjects();
112115
}
113116

114117
fn createCommandBuffers(allocator: *Allocator) !void {
@@ -161,6 +164,27 @@ fn createCommandBuffers(allocator: *Allocator) !void {
161164
}
162165
}
163166

167+
fn createSyncObjects() !void {
168+
const semaphoreInfo = c.VkSemaphoreCreateInfo{
169+
.sType = c.VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
170+
.pNext = null,
171+
.flags = 0,
172+
};
173+
174+
const fenceInfo = c.VkFenceCreateInfo{
175+
.sType = c.VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
176+
.flags = c.VK_FENCE_CREATE_SIGNALED_BIT,
177+
.pNext = null,
178+
};
179+
180+
var i: usize = 0;
181+
while (i < MAX_FRAMES_IN_FLIGHT) : (i += 1) {
182+
try checkSuccess(c.vkCreateSemaphore(global_device, &semaphoreInfo, null, &imageAvailableSemaphores[i]));
183+
try checkSuccess(c.vkCreateSemaphore(global_device, &semaphoreInfo, null, &renderFinishedSemaphores[i]));
184+
try checkSuccess(c.vkCreateFence(global_device, &fenceInfo, null, &inFlightFences[i]));
185+
}
186+
}
187+
164188
fn createCommandPool(allocator: *Allocator) !void {
165189
const queueFamilyIndices = try findQueueFamilies(allocator, physicalDevice);
166190

@@ -945,6 +969,7 @@ fn checkValidationLayerSupport(allocator: *Allocator) !bool {
945969
}
946970

947971
fn drawFrame() void {
972+
// TODO
948973
//c.vkWaitForFences(device, 1, &inFlightFences[currentFrame], c.VK_TRUE, @maxValue(u64));
949974
//c.vkResetFences(device, 1, &inFlightFences[currentFrame]);
950975

src/vulkan.zig

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,12 +2400,8 @@ pub const enum_VkSparseMemoryBindFlagBits = extern enum {
24002400
};
24012401
pub const VkSparseMemoryBindFlagBits = enum_VkSparseMemoryBindFlagBits;
24022402
pub const VkSparseMemoryBindFlags = VkFlags;
2403-
pub const VK_FENCE_CREATE_SIGNALED_BIT = enum_VkFenceCreateFlagBits.VK_FENCE_CREATE_SIGNALED_BIT;
2404-
pub const VK_FENCE_CREATE_FLAG_BITS_MAX_ENUM = enum_VkFenceCreateFlagBits.VK_FENCE_CREATE_FLAG_BITS_MAX_ENUM;
2405-
pub const enum_VkFenceCreateFlagBits = extern enum {
2406-
VK_FENCE_CREATE_SIGNALED_BIT = 1,
2407-
VK_FENCE_CREATE_FLAG_BITS_MAX_ENUM = 2147483647,
2408-
};
2403+
pub const VK_FENCE_CREATE_SIGNALED_BIT = 1;
2404+
pub const enum_VkFenceCreateFlagBits = c_int;
24092405
pub const VkFenceCreateFlagBits = enum_VkFenceCreateFlagBits;
24102406
pub const VkFenceCreateFlags = VkFlags;
24112407
pub const VkSemaphoreCreateFlags = VkFlags;
@@ -3976,12 +3972,12 @@ pub extern fn vkGetImageMemoryRequirements(device: VkDevice, image: VkImage, pMe
39763972
pub extern fn vkGetImageSparseMemoryRequirements(device: VkDevice, image: VkImage, pSparseMemoryRequirementCount: ?[*]u32, pSparseMemoryRequirements: ?[*]VkSparseImageMemoryRequirements) void;
39773973
pub extern fn vkGetPhysicalDeviceSparseImageFormatProperties(physicalDevice: VkPhysicalDevice, format: VkFormat, type_0: VkImageType, samples: VkSampleCountFlagBits, usage: VkImageUsageFlags, tiling: VkImageTiling, pPropertyCount: ?[*]u32, pProperties: ?[*]VkSparseImageFormatProperties) void;
39783974
pub extern fn vkQueueBindSparse(queue: VkQueue, bindInfoCount: u32, pBindInfo: ?[*]const VkBindSparseInfo, fence: VkFence) VkResult;
3979-
pub extern fn vkCreateFence(device: VkDevice, pCreateInfo: ?[*]const VkFenceCreateInfo, pAllocator: ?[*]const VkAllocationCallbacks, pFence: ?[*]VkFence) VkResult;
3975+
pub extern fn vkCreateFence(device: VkDevice, pCreateInfo: *const VkFenceCreateInfo, pAllocator: ?[*]const VkAllocationCallbacks, pFence: *VkFence) VkResult;
39803976
pub extern fn vkDestroyFence(device: VkDevice, fence: VkFence, pAllocator: ?[*]const VkAllocationCallbacks) void;
39813977
pub extern fn vkResetFences(device: VkDevice, fenceCount: u32, pFences: ?[*]const VkFence) VkResult;
39823978
pub extern fn vkGetFenceStatus(device: VkDevice, fence: VkFence) VkResult;
39833979
pub extern fn vkWaitForFences(device: VkDevice, fenceCount: u32, pFences: ?[*]const VkFence, waitAll: VkBool32, timeout: u64) VkResult;
3984-
pub extern fn vkCreateSemaphore(device: VkDevice, pCreateInfo: ?[*]const VkSemaphoreCreateInfo, pAllocator: ?[*]const VkAllocationCallbacks, pSemaphore: ?[*]VkSemaphore) VkResult;
3980+
pub extern fn vkCreateSemaphore(device: VkDevice, pCreateInfo: *const VkSemaphoreCreateInfo, pAllocator: ?[*]const VkAllocationCallbacks, pSemaphore: *VkSemaphore) VkResult;
39853981
pub extern fn vkDestroySemaphore(device: VkDevice, semaphore: VkSemaphore, pAllocator: ?[*]const VkAllocationCallbacks) void;
39863982
pub extern fn vkCreateEvent(device: VkDevice, pCreateInfo: ?[*]const VkEventCreateInfo, pAllocator: ?[*]const VkAllocationCallbacks, pEvent: ?[*]VkEvent) VkResult;
39873983
pub extern fn vkDestroyEvent(device: VkDevice, event: VkEvent, pAllocator: ?[*]const VkAllocationCallbacks) void;

0 commit comments

Comments
 (0)