Skip to content

Commit

Permalink
improve correctness of buffer handling
Browse files Browse the repository at this point in the history
* set buffer mapped to null when unmapping (ops)
* Wait for rendering to complete before manipulating vertex and index buffers
  • Loading branch information
Avokadoen committed Apr 9, 2022
1 parent b1e3628 commit 148ab7d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/modules/render/GpuBufferMemory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ pub fn map(self: *GpuBufferMemory, ctx: Context, size: vk.DeviceSize, offset: vk
self.mapped = (try ctx.vkd.mapMemory(ctx.logical_device, self.memory, offset, size, .{})) orelse return error.FailedToMapGPUMem;
}

pub fn unmap(self: GpuBufferMemory, ctx: Context) void {
pub fn unmap(self: *GpuBufferMemory, ctx: Context) void {
if (self.mapped != null) {
ctx.vkd.unmapMemory(ctx.logical_device, self.memory);
self.mapped = null;
}
}

Expand Down Expand Up @@ -199,9 +200,9 @@ pub fn batchTransfer(self: *GpuBufferMemory, ctx: Context, comptime T: type, off

/// destroy buffer and free memory
pub fn deinit(self: GpuBufferMemory, ctx: Context) void {
// unmap if needed
self.unmap(ctx);

if (self.mapped != null) {
ctx.vkd.unmapMemory(ctx.logical_device, self.memory);
}
if (self.buffer != .null_handle) {
ctx.vkd.destroyBuffer(ctx.logical_device, self.buffer, null);
}
Expand Down
9 changes: 6 additions & 3 deletions src/modules/voxel_rt/Pipeline.zig
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ pub fn deinit(self: Pipeline, ctx: Context) void {
pub fn draw(self: *Pipeline, ctx: Context) !void {
// wait for previous compute dispatch to complete
_ = try ctx.vkd.waitForFences(ctx.logical_device, 1, @ptrCast([*]const vk.Fence, &self.compute_complete_fence), vk.TRUE, std.math.maxInt(u64));
try ctx.vkd.resetFences(ctx.logical_device, 1, @ptrCast([*]const vk.Fence, &self.compute_complete_fence));

// perform the compute ray tracing, draw to target texture
const compute_submit_info = vk.SubmitInfo{
Expand All @@ -192,7 +193,6 @@ pub fn draw(self: *Pipeline, ctx: Context) !void {
.signal_semaphore_count = 1,
.p_signal_semaphores = @ptrCast([*]const vk.Semaphore, &self.compute_complete_semaphore),
};
try ctx.vkd.resetFences(ctx.logical_device, 1, @ptrCast([*]const vk.Fence, &self.compute_complete_fence));
try ctx.vkd.queueSubmit(ctx.compute_queue, 1, @ptrCast([*]const vk.SubmitInfo, &compute_submit_info), self.compute_complete_fence);

const aquire_result = try ctx.vkd.acquireNextImageKHR(
Expand All @@ -207,8 +207,11 @@ pub fn draw(self: *Pipeline, ctx: Context) !void {
}
const image_index = aquire_result.image_index;

try self.imgui_pipeline.prepareDraw(ctx);
// wait for previous texture draw before updating buffers and command buffers
_ = try ctx.vkd.waitForFences(ctx.logical_device, 1, @ptrCast([*]const vk.Fence, &self.render_complete_fence), vk.TRUE, std.math.maxInt(u64));
try ctx.vkd.resetFences(ctx.logical_device, 1, @ptrCast([*]const vk.Fence, &self.render_complete_fence));

try self.imgui_pipeline.prepareDraw(ctx);

// re-record command buffer to update any state
var begin_info = self.render_pass_begin_info;
Expand All @@ -225,7 +228,7 @@ pub fn draw(self: *Pipeline, ctx: Context) !void {
.signal_semaphore_count = 1,
.p_signal_semaphores = @ptrCast([*]const vk.Semaphore, &self.render_complete_semaphore),
};
try ctx.vkd.resetFences(ctx.logical_device, 1, @ptrCast([*]const vk.Fence, &self.render_complete_fence));

try ctx.vkd.queueSubmit(
ctx.graphics_queue,
1,
Expand Down

0 comments on commit 148ab7d

Please sign in to comment.