Now we have a spinning square

This commit is contained in:
Timothy Warren 2023-04-13 10:31:49 -04:00
parent fafbf868bf
commit afeb1b6fef

View File

@ -68,6 +68,8 @@ pub(crate) struct AppData {
index_buffer_memory: vk::DeviceMemory, index_buffer_memory: vk::DeviceMemory,
uniform_buffers: Vec<vk::Buffer>, uniform_buffers: Vec<vk::Buffer>,
pub(super) uniform_buffers_memory: Vec<vk::DeviceMemory>, pub(super) uniform_buffers_memory: Vec<vk::DeviceMemory>,
descriptor_pool: vk::DescriptorPool,
descriptor_sets: Vec<vk::DescriptorSet>,
// Command Buffers // Command Buffers
pub(super) command_buffers: Vec<vk::CommandBuffer>, pub(super) command_buffers: Vec<vk::CommandBuffer>,
// Sync Objects // Sync Objects
@ -103,6 +105,8 @@ impl AppData {
self.create_vertex_buffer(&instance, &device)?; self.create_vertex_buffer(&instance, &device)?;
self.create_index_buffer(&instance, &device)?; self.create_index_buffer(&instance, &device)?;
self.create_uniform_buffers(&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_command_buffers(&device)?;
self.create_sync_objects(&device)?; self.create_sync_objects(&device)?;
@ -194,6 +198,7 @@ impl AppData {
/// # Safety /// # Safety
/// Here be Dragons /// Here be Dragons
unsafe fn destroy_swapchain(&mut self, device: &Device) { unsafe fn destroy_swapchain(&mut self, device: &Device) {
device.destroy_descriptor_pool(self.descriptor_pool, None);
self.uniform_buffers self.uniform_buffers
.iter() .iter()
.for_each(|b| device.destroy_buffer(*b, None)); .for_each(|b| device.destroy_buffer(*b, None));
@ -423,6 +428,8 @@ impl AppData {
self.create_pipeline(device)?; self.create_pipeline(device)?;
self.create_framebuffers(device)?; self.create_framebuffers(device)?;
self.create_uniform_buffers(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_command_buffers(device)?;
self.images_in_flight self.images_in_flight
@ -573,7 +580,7 @@ impl AppData {
.polygon_mode(vk::PolygonMode::FILL) .polygon_mode(vk::PolygonMode::FILL)
.line_width(1.0) .line_width(1.0)
.cull_mode(vk::CullModeFlags::BACK) .cull_mode(vk::CullModeFlags::BACK)
.front_face(vk::FrontFace::CLOCKWISE) .front_face(vk::FrontFace::COUNTER_CLOCKWISE)
.depth_bias_enable(false); .depth_bias_enable(false);
let multisample_state = vk::PipelineMultisampleStateCreateInfo::builder() let multisample_state = vk::PipelineMultisampleStateCreateInfo::builder()
@ -826,6 +833,49 @@ impl AppData {
Ok(()) 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::<UniformBufferObject>() 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 // Command Buffers
//================================================ //================================================
@ -879,6 +929,14 @@ impl AppData {
0, 0,
vk::IndexType::UINT16, 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_draw_indexed(*command_buffer, INDICES.len() as u32, 1, 0, 0, 0);
device.cmd_end_render_pass(*command_buffer); device.cmd_end_render_pass(*command_buffer);