From afeb1b6fef1ffadd1e966a82f89c02829565e2f1 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 13 Apr 2023 10:31:49 -0400 Subject: [PATCH] Now we have a spinning square --- src/app/data.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/app/data.rs b/src/app/data.rs index cc229c9..4ad25ce 100644 --- a/src/app/data.rs +++ b/src/app/data.rs @@ -68,6 +68,8 @@ pub(crate) struct AppData { index_buffer_memory: vk::DeviceMemory, uniform_buffers: Vec, pub(super) uniform_buffers_memory: Vec, + descriptor_pool: vk::DescriptorPool, + descriptor_sets: Vec, // Command Buffers pub(super) command_buffers: Vec, // Sync Objects @@ -103,6 +105,8 @@ impl AppData { self.create_vertex_buffer(&instance, &device)?; self.create_index_buffer(&instance, &device)?; self.create_uniform_buffers(&instance, &device)?; + self.create_descriptor_pool(&device)?; + self.create_descriptor_sets(&device)?; self.create_command_buffers(&device)?; self.create_sync_objects(&device)?; @@ -194,6 +198,7 @@ impl AppData { /// # Safety /// Here be Dragons unsafe fn destroy_swapchain(&mut self, device: &Device) { + device.destroy_descriptor_pool(self.descriptor_pool, None); self.uniform_buffers .iter() .for_each(|b| device.destroy_buffer(*b, None)); @@ -423,6 +428,8 @@ impl AppData { self.create_pipeline(device)?; self.create_framebuffers(device)?; self.create_uniform_buffers(instance, device)?; + self.create_descriptor_pool(device)?; + self.create_descriptor_sets(device)?; self.create_command_buffers(device)?; self.images_in_flight @@ -573,7 +580,7 @@ impl AppData { .polygon_mode(vk::PolygonMode::FILL) .line_width(1.0) .cull_mode(vk::CullModeFlags::BACK) - .front_face(vk::FrontFace::CLOCKWISE) + .front_face(vk::FrontFace::COUNTER_CLOCKWISE) .depth_bias_enable(false); let multisample_state = vk::PipelineMultisampleStateCreateInfo::builder() @@ -826,6 +833,49 @@ impl AppData { Ok(()) } + unsafe fn create_descriptor_pool(&mut self, device: &Device) -> Result<()> { + let ubo_size = vk::DescriptorPoolSize::builder() + .type_(vk::DescriptorType::UNIFORM_BUFFER) + .descriptor_count(self.swapchain_images.len() as u32); + + let pool_sizes = &[ubo_size]; + let info = vk::DescriptorPoolCreateInfo::builder() + .pool_sizes(pool_sizes) + .max_sets(self.swapchain_images.len() as u32); + + self.descriptor_pool = device.create_descriptor_pool(&info, None)?; + + Ok(()) + } + + unsafe fn create_descriptor_sets(&mut self, device: &Device) -> Result<()> { + let layouts = vec![self.descriptor_set_layout; self.swapchain_images.len()]; + let info = vk::DescriptorSetAllocateInfo::builder() + .descriptor_pool(self.descriptor_pool) + .set_layouts(&layouts); + + self.descriptor_sets = device.allocate_descriptor_sets(&info)?; + + for i in 0..self.swapchain_images.len() { + let info = vk::DescriptorBufferInfo::builder() + .buffer(self.uniform_buffers[i]) + .offset(0) + .range(size_of::() as u64); + + let buffer_info = &[info]; + let ubo_write = vk::WriteDescriptorSet::builder() + .dst_set(self.descriptor_sets[i]) + .dst_binding(0) + .dst_array_element(0) + .descriptor_type(vk::DescriptorType::UNIFORM_BUFFER) + .buffer_info(buffer_info); + + device.update_descriptor_sets(&[ubo_write], &[] as &[vk::CopyDescriptorSet]); + } + + Ok(()) + } + //================================================ // Command Buffers //================================================ @@ -879,6 +929,14 @@ impl AppData { 0, vk::IndexType::UINT16, ); + device.cmd_bind_descriptor_sets( + *command_buffer, + vk::PipelineBindPoint::GRAPHICS, + self.pipeline_layout, + 0, + &[self.descriptor_sets[i]], + &[], + ); device.cmd_draw_indexed(*command_buffer, INDICES.len() as u32, 1, 0, 0, 0); device.cmd_end_render_pass(*command_buffer);