Introduce depth buffer finally
This commit is contained in:
@@ -5,6 +5,7 @@ const vk = @import("vulkan");
|
||||
|
||||
const Engine = @import("Engine.zig");
|
||||
const QSM = @import("QueueSharingMode.zig");
|
||||
const Texture = @import("Texture.zig");
|
||||
|
||||
params: Params,
|
||||
render_pass: vk.RenderPass,
|
||||
@@ -12,6 +13,7 @@ render_pass: vk.RenderPass,
|
||||
extent: vk.Extent2D = .{ .width = 0, .height = 0 },
|
||||
swapchain: vk.SwapchainKHR = .null_handle,
|
||||
swapchain_images: []SwapchainImage = &.{},
|
||||
depth_texture: ?Texture = null,
|
||||
image_index: u32 = 0,
|
||||
semaphore_image_acquired: vk.Semaphore = .null_handle,
|
||||
|
||||
@@ -35,6 +37,16 @@ pub fn init(engine: *Engine) !Swapchain {
|
||||
.initial_layout = .undefined,
|
||||
.final_layout = .present_src_khr,
|
||||
},
|
||||
.{
|
||||
.format = Texture.Usage.depth.vkFormat(),
|
||||
.samples = .{ .@"1_bit" = true },
|
||||
.load_op = .clear,
|
||||
.store_op = .dont_care,
|
||||
.stencil_load_op = .clear,
|
||||
.stencil_store_op = .dont_care,
|
||||
.initial_layout = .undefined,
|
||||
.final_layout = .depth_stencil_attachment_optimal,
|
||||
},
|
||||
};
|
||||
|
||||
const color_attachments = [_]vk.AttachmentReference{
|
||||
@@ -44,11 +56,39 @@ pub fn init(engine: *Engine) !Swapchain {
|
||||
},
|
||||
};
|
||||
|
||||
const depth_stencil_attachment: vk.AttachmentReference = .{
|
||||
.attachment = 1,
|
||||
.layout = .depth_stencil_attachment_optimal,
|
||||
};
|
||||
|
||||
const subpasses = [_]vk.SubpassDescription{
|
||||
.{
|
||||
.pipeline_bind_point = .graphics,
|
||||
.color_attachment_count = color_attachments.len,
|
||||
.p_color_attachments = &color_attachments,
|
||||
.p_depth_stencil_attachment = &depth_stencil_attachment,
|
||||
},
|
||||
};
|
||||
|
||||
const dependencies = [_]vk.SubpassDependency{
|
||||
.{
|
||||
.src_subpass = 0,
|
||||
.dst_subpass = 0,
|
||||
.src_stage_mask = .{
|
||||
.color_attachment_output_bit = true,
|
||||
.late_fragment_tests_bit = true,
|
||||
},
|
||||
.dst_stage_mask = .{
|
||||
.color_attachment_output_bit = true,
|
||||
.early_fragment_tests_bit = true,
|
||||
},
|
||||
.src_access_mask = .{
|
||||
.depth_stencil_attachment_write_bit = true,
|
||||
},
|
||||
.dst_access_mask = .{
|
||||
.color_attachment_write_bit = true,
|
||||
.depth_stencil_attachment_write_bit = true,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -57,6 +97,8 @@ pub fn init(engine: *Engine) !Swapchain {
|
||||
.p_attachments = &attachments,
|
||||
.subpass_count = subpasses.len,
|
||||
.p_subpasses = &subpasses,
|
||||
.dependency_count = dependencies.len,
|
||||
.p_dependencies = &dependencies,
|
||||
}, &engine.vk_allocator.interface);
|
||||
};
|
||||
errdefer engine.device.destroyRenderPass(render_pass, &engine.vk_allocator.interface);
|
||||
@@ -80,6 +122,10 @@ pub fn deinit(self: *Swapchain, engine: *Engine) void {
|
||||
}
|
||||
allocator.free(self.swapchain_images);
|
||||
|
||||
if (self.depth_texture) |*depth_texture| {
|
||||
depth_texture.deinit(engine);
|
||||
}
|
||||
|
||||
if (self.swapchain != .null_handle) {
|
||||
engine.device.destroySwapchainKHR(self.swapchain, &engine.vk_allocator.interface);
|
||||
}
|
||||
@@ -95,6 +141,7 @@ pub fn recreate(self: *Swapchain, engine: *Engine) !void {
|
||||
|
||||
const old_swapchain = self.swapchain;
|
||||
const old_swapchain_images = self.swapchain_images;
|
||||
var old_depth_texture = self.depth_texture;
|
||||
const old_semaphore_image_acquired = self.semaphore_image_acquired;
|
||||
|
||||
const extent = try getCurrentExtent(engine);
|
||||
@@ -137,6 +184,10 @@ pub fn recreate(self: *Swapchain, engine: *Engine) !void {
|
||||
allocator.free(self.swapchain_images);
|
||||
self.swapchain_images = &.{};
|
||||
|
||||
if (old_depth_texture) |*depth_texture| {
|
||||
depth_texture.deinit(engine);
|
||||
}
|
||||
|
||||
if (old_swapchain != .null_handle) {
|
||||
engine.device.destroySwapchainKHR(old_swapchain, &engine.vk_allocator.interface);
|
||||
self.swapchain = .null_handle;
|
||||
@@ -149,6 +200,16 @@ pub fn recreate(self: *Swapchain, engine: *Engine) !void {
|
||||
self.semaphore_image_acquired = .null_handle;
|
||||
}
|
||||
|
||||
// --- CREATE DEPTH TEXTURE ------------------------------------------------
|
||||
|
||||
var new_depth_texture = try Texture.init(engine, .{
|
||||
.width = extent.width,
|
||||
.height = extent.height,
|
||||
.target_queue = .graphics,
|
||||
.usage = .depth,
|
||||
});
|
||||
errdefer new_depth_texture.deinit(engine);
|
||||
|
||||
// --- CREATE NEW SWAPCHAIN IMAGES -----------------------------------------
|
||||
|
||||
const new_swapchain_images = blk: {
|
||||
@@ -166,6 +227,7 @@ pub fn recreate(self: *Swapchain, engine: *Engine) !void {
|
||||
.format = self.params.surface_format.format,
|
||||
.render_pass = self.render_pass,
|
||||
.extent = extent,
|
||||
.depth_stencil_image_view = new_depth_texture.image_view,
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -195,6 +257,7 @@ pub fn recreate(self: *Swapchain, engine: *Engine) !void {
|
||||
self.extent = extent;
|
||||
self.swapchain = new_swapchain;
|
||||
self.swapchain_images = new_swapchain_images;
|
||||
self.depth_texture = new_depth_texture;
|
||||
self.image_index = res.image_index;
|
||||
self.semaphore_image_acquired = semaphore_image_acquired;
|
||||
}
|
||||
@@ -359,6 +422,7 @@ const SwapchainImage = struct {
|
||||
format: vk.Format,
|
||||
render_pass: vk.RenderPass,
|
||||
extent: vk.Extent2D,
|
||||
depth_stencil_image_view: vk.ImageView,
|
||||
};
|
||||
|
||||
fn init(engine: *Engine, props: InitProps) !SwapchainImage {
|
||||
@@ -389,7 +453,7 @@ const SwapchainImage = struct {
|
||||
|
||||
const framebuffer = try engine.createFramebuffer(.{
|
||||
.render_pass = props.render_pass,
|
||||
.attachments = &.{image_view},
|
||||
.attachments = &.{ image_view, props.depth_stencil_image_view },
|
||||
.width = props.extent.width,
|
||||
.height = props.extent.height,
|
||||
.layers = 1,
|
||||
|
||||
Reference in New Issue
Block a user