diff --git a/src/rendering/render_system.rs b/src/rendering/render_system.rs index a006f7d3..17e83b40 100644 --- a/src/rendering/render_system.rs +++ b/src/rendering/render_system.rs @@ -82,11 +82,14 @@ pub struct MeshHandle(pub(super) u64); pub struct SynchronizerHandle(pub(super) u64); #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] -pub struct DescriptorSetLayoutHandle(pub(super) u64); +pub struct DescriptorSetTemplateHandle(pub(super) u64); #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub struct DescriptorSetHandle(pub(super) u64); +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] +pub struct DescriptorSetBindingHandle(pub(super) u64); + /// Handle to a Pipeline Layout #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub struct PipelineLayoutHandle(pub(super) u64); @@ -116,7 +119,7 @@ pub enum Handle { Image(ImageHandle), Mesh(MeshHandle), Synchronizer(SynchronizerHandle), - DescriptorSetLayout(DescriptorSetLayoutHandle), + DescriptorSetLayout(DescriptorSetTemplateHandle), DescriptorSet(DescriptorSetHandle), PipelineLayout(PipelineLayoutHandle), Sampler(SamplerHandle), @@ -317,13 +320,15 @@ pub trait RenderSystem: orchestrator::System { /// Creates a shader. fn create_shader(&mut self, shader_source_type: ShaderSourceType, stage: ShaderTypes, shader: &[u8]) -> ShaderHandle; - fn create_descriptor_set_layout(&mut self, name: Option<&str>, bindings: &[DescriptorSetLayoutBinding]) -> DescriptorSetLayoutHandle; + fn create_descriptor_set_template(&mut self, name: Option<&str>, binding_templates: &[DescriptorSetBindingTemplate]) -> DescriptorSetTemplateHandle; + + fn create_descriptor_set(&mut self, name: Option<&str>, descriptor_set_template_handle: &DescriptorSetTemplateHandle) -> DescriptorSetHandle; - fn create_descriptor_set(&mut self, name: Option<&str>, descriptor_set_layout_handle: &DescriptorSetLayoutHandle, bindings: &[DescriptorSetLayoutBinding]) -> DescriptorSetHandle; + fn create_descriptor_binding(&mut self, descriptor_set: DescriptorSetHandle, binding_template: &DescriptorSetBindingTemplate) -> DescriptorSetBindingHandle; fn write(&self, descriptor_set_writes: &[DescriptorWrite]); - fn create_pipeline_layout(&mut self, descriptor_set_layout_handles: &[DescriptorSetLayoutHandle], push_constant_ranges: &[PushConstantRange]) -> PipelineLayoutHandle; + fn create_pipeline_layout(&mut self, descriptor_set_template_handles: &[DescriptorSetTemplateHandle], push_constant_ranges: &[PushConstantRange]) -> PipelineLayoutHandle; fn create_raster_pipeline(&mut self, pipeline_blocks: &[PipelineConfigurationBlocks]) -> PipelineHandle; @@ -709,7 +714,7 @@ AccelerationStructure, } /// Stores the information of a descriptor set layout binding. -pub struct DescriptorSetLayoutBinding { +pub struct DescriptorSetBindingTemplate { pub name: &'static str, /// The binding of the descriptor set layout binding. pub binding: u32, @@ -752,10 +757,7 @@ pub enum DescriptorInfo { /// Stores the information of a descriptor set write. pub struct DescriptorWrite { - /// The descriptor set to write to. - pub descriptor_set: DescriptorSetHandle, - /// The binding to write to. - pub binding: u32, + pub binding_handle: DescriptorSetBindingHandle, /// The index of the array element to write to in the binding(if the binding is an array). pub array_element: u32, /// Information describing the descriptor. @@ -960,12 +962,16 @@ impl RenderSystem for RenderSystemImplementation { self.pointer.create_command_buffer_recording(command_buffer_handle, frame) } - fn create_descriptor_set(&mut self, name: Option<&str>, descriptor_set_layout: &DescriptorSetLayoutHandle, bindings: &[DescriptorSetLayoutBinding]) -> DescriptorSetHandle { - self.pointer.create_descriptor_set(name, descriptor_set_layout, bindings) + fn create_descriptor_binding(&mut self, descriptor_set: DescriptorSetHandle, binding: &DescriptorSetBindingTemplate) -> DescriptorSetBindingHandle { + self.pointer.create_descriptor_binding(descriptor_set, binding) + } + + fn create_descriptor_set(&mut self, name: Option<&str>, descriptor_set_layout_handle: &DescriptorSetTemplateHandle) -> DescriptorSetHandle { + self.pointer.create_descriptor_set(name, descriptor_set_layout_handle) } - fn create_descriptor_set_layout(&mut self, name: Option<&str>, bindings: &[DescriptorSetLayoutBinding]) -> DescriptorSetLayoutHandle { - self.pointer.create_descriptor_set_layout(name, bindings) + fn create_descriptor_set_template(&mut self, name: Option<&str>, bindings: &[DescriptorSetBindingTemplate]) -> DescriptorSetTemplateHandle { + self.pointer.create_descriptor_set_template(name, bindings) } fn create_raster_pipeline(&mut self, pipeline_blocks: &[PipelineConfigurationBlocks]) -> PipelineHandle { @@ -980,7 +986,7 @@ impl RenderSystem for RenderSystemImplementation { self.pointer.create_ray_tracing_pipeline(pipeline_layout_handle, shaders) } - fn create_pipeline_layout(&mut self, descriptor_set_layout_handles: &[DescriptorSetLayoutHandle], push_constant_ranges: &[PushConstantRange]) -> PipelineLayoutHandle { + fn create_pipeline_layout(&mut self, descriptor_set_layout_handles: &[DescriptorSetTemplateHandle], push_constant_ranges: &[PushConstantRange]) -> PipelineLayoutHandle { self.pointer.create_pipeline_layout(descriptor_set_layout_handles, push_constant_ranges) } @@ -1795,8 +1801,8 @@ pub(super) mod tests { let sampler = renderer.create_sampler(); - let bindings = [ - DescriptorSetLayoutBinding { + let descriptor_set_layout_handle = renderer.create_descriptor_set_template(None, &[ + DescriptorSetBindingTemplate { name: "sampler", descriptor_count: 1, descriptor_type: DescriptorType::Sampler, @@ -1804,7 +1810,7 @@ pub(super) mod tests { stages: Stages::FRAGMENT, immutable_samplers: Some(vec![sampler]), }, - DescriptorSetLayoutBinding { + DescriptorSetBindingTemplate { name: "ubo", descriptor_count: 1, descriptor_type: DescriptorType::StorageBuffer, @@ -1812,7 +1818,7 @@ pub(super) mod tests { stages: Stages::VERTEX, immutable_samplers: None, }, - DescriptorSetLayoutBinding { + DescriptorSetBindingTemplate { name: "tex", descriptor_count: 1, descriptor_type: DescriptorType::SampledImage, @@ -1820,16 +1826,46 @@ pub(super) mod tests { stages: Stages::FRAGMENT, immutable_samplers: None, }, - ]; + ]); - let descriptor_set_layout_handle = renderer.create_descriptor_set_layout(None, &bindings); + let descriptor_set = renderer.create_descriptor_set(None, &descriptor_set_layout_handle,); - let descriptor_set = renderer.create_descriptor_set(None, &descriptor_set_layout_handle, &bindings); + let sampler_binding = renderer.create_descriptor_binding(descriptor_set, + &DescriptorSetBindingTemplate { + name: "sampler", + descriptor_count: 1, + descriptor_type: DescriptorType::Sampler, + binding: 0, + stages: Stages::FRAGMENT, + immutable_samplers: Some(vec![sampler]), + },); + + let ubo_binding = renderer.create_descriptor_binding(descriptor_set, + &DescriptorSetBindingTemplate { + name: "ubo", + descriptor_count: 1, + descriptor_type: DescriptorType::StorageBuffer, + binding: 1, + stages: Stages::VERTEX, + immutable_samplers: None, + }, + ); + + let tex_binding = renderer.create_descriptor_binding(descriptor_set, + &DescriptorSetBindingTemplate { + name: "tex", + descriptor_count: 1, + descriptor_type: DescriptorType::SampledImage, + binding: 2, + stages: Stages::FRAGMENT, + immutable_samplers: None, + }, + ); renderer.write(&[ - DescriptorWrite { descriptor_set: descriptor_set, binding: 0, array_element: 0, descriptor: Descriptor::Sampler(sampler) }, - DescriptorWrite { descriptor_set: descriptor_set, binding: 1, array_element: 0, descriptor: Descriptor::Buffer{ handle: buffer, size: Ranges::Size(64) } }, - DescriptorWrite { descriptor_set: descriptor_set, binding: 2, array_element: 0, descriptor: Descriptor::Image{ handle: sampled_texture, layout: Layouts::Read } }, + DescriptorWrite { binding_handle: sampler_binding, array_element: 0, descriptor: Descriptor::Sampler(sampler) }, + DescriptorWrite { binding_handle: ubo_binding, array_element: 0, descriptor: Descriptor::Buffer{ handle: buffer, size: Ranges::Size(64) } }, + DescriptorWrite { binding_handle: tex_binding, array_element: 0, descriptor: Descriptor::Image{ handle: sampled_texture, layout: Layouts::Read } }, ]); assert!(!renderer.has_errors()); @@ -2045,7 +2081,7 @@ void main() { }); let bindings = [ - DescriptorSetLayoutBinding { + DescriptorSetBindingTemplate { name: "acceleration structure", descriptor_count: 1, descriptor_type: DescriptorType::AccelerationStructure, @@ -2053,7 +2089,7 @@ void main() { stages: Stages::RAYGEN, immutable_samplers: None, }, - DescriptorSetLayoutBinding { + DescriptorSetBindingTemplate { name: "render target", descriptor_count: 1, descriptor_type: DescriptorType::StorageImage, @@ -2061,7 +2097,7 @@ void main() { stages: Stages::RAYGEN, immutable_samplers: None, }, - DescriptorSetLayoutBinding { + DescriptorSetBindingTemplate { name: "vertex positions", descriptor_count: 1, descriptor_type: DescriptorType::StorageBuffer, @@ -2069,7 +2105,7 @@ void main() { stages: Stages::CLOSEST_HIT, immutable_samplers: None, }, - DescriptorSetLayoutBinding { + DescriptorSetBindingTemplate { name: "vertex colors", descriptor_count: 1, descriptor_type: DescriptorType::StorageBuffer, @@ -2077,7 +2113,7 @@ void main() { stages: Stages::CLOSEST_HIT, immutable_samplers: None, }, - DescriptorSetLayoutBinding { + DescriptorSetBindingTemplate { name: "indices", descriptor_count: 1, descriptor_type: DescriptorType::StorageBuffer, @@ -2087,18 +2123,73 @@ void main() { }, ]; - let descriptor_set_layout_handle = renderer.create_descriptor_set_layout(None, &bindings); + let descriptor_set_layout_handle = renderer.create_descriptor_set_template(None, &bindings); + + let descriptor_set = renderer.create_descriptor_set(None, &descriptor_set_layout_handle); + + let acceleration_structure_binding = renderer.create_descriptor_binding(descriptor_set, + &DescriptorSetBindingTemplate { + name: "acceleration structure", + descriptor_count: 1, + descriptor_type: DescriptorType::AccelerationStructure, + binding: 0, + stages: Stages::RAYGEN, + immutable_samplers: None, + }, + ); + + let render_target_binding = renderer.create_descriptor_binding(descriptor_set, + &DescriptorSetBindingTemplate { + name: "render target", + descriptor_count: 1, + descriptor_type: DescriptorType::StorageImage, + binding: 1, + stages: Stages::RAYGEN, + immutable_samplers: None, + }, + ); - let descriptor_set = renderer.create_descriptor_set(None, &descriptor_set_layout_handle, &bindings); + let vertex_positions_binding = renderer.create_descriptor_binding(descriptor_set, + &DescriptorSetBindingTemplate { + name: "vertex positions", + descriptor_count: 1, + descriptor_type: DescriptorType::StorageBuffer, + binding: 2, + stages: Stages::CLOSEST_HIT, + immutable_samplers: None, + }, + ); + + let vertex_colors_binding = renderer.create_descriptor_binding(descriptor_set, + &DescriptorSetBindingTemplate { + name: "vertex colors", + descriptor_count: 1, + descriptor_type: DescriptorType::StorageBuffer, + binding: 3, + stages: Stages::CLOSEST_HIT, + immutable_samplers: None, + }, + ); + + let indices_binding = renderer.create_descriptor_binding(descriptor_set, + &DescriptorSetBindingTemplate { + name: "indices", + descriptor_count: 1, + descriptor_type: DescriptorType::StorageBuffer, + binding: 4, + stages: Stages::CLOSEST_HIT, + immutable_samplers: None, + }, + ); let render_target = renderer.create_image(None, extent, Formats::RGBAu8, None, Uses::Storage, DeviceAccesses::CpuRead | DeviceAccesses::GpuWrite, UseCases::DYNAMIC); renderer.write(&[ - DescriptorWrite { descriptor_set: descriptor_set, binding: 0, array_element: 0, descriptor: Descriptor::AccelerationStructure{ handle: top_level_acceleration_structure } }, - DescriptorWrite { descriptor_set: descriptor_set, binding: 1, array_element: 0, descriptor: Descriptor::Image { handle: render_target, layout: Layouts::General } }, - DescriptorWrite { descriptor_set: descriptor_set, binding: 2, array_element: 0, descriptor: Descriptor::Buffer { handle: vertex_positions_buffer, size: Ranges::Size(12 * 3) } }, - DescriptorWrite { descriptor_set: descriptor_set, binding: 3, array_element: 0, descriptor: Descriptor::Buffer { handle: vertex_colors_buffer, size: Ranges::Size(16 * 3) } }, - DescriptorWrite { descriptor_set: descriptor_set, binding: 4, array_element: 0, descriptor: Descriptor::Buffer { handle: index_buffer, size: Ranges::Size(2 * 3) } }, + DescriptorWrite { binding_handle: acceleration_structure_binding, array_element: 0, descriptor: Descriptor::AccelerationStructure{ handle: top_level_acceleration_structure } }, + DescriptorWrite { binding_handle: render_target_binding, array_element: 0, descriptor: Descriptor::Image { handle: render_target, layout: Layouts::General } }, + DescriptorWrite { binding_handle: vertex_positions_binding, array_element: 0, descriptor: Descriptor::Buffer { handle: vertex_positions_buffer, size: Ranges::Size(12 * 3) } }, + DescriptorWrite { binding_handle: vertex_colors_binding, array_element: 0, descriptor: Descriptor::Buffer { handle: vertex_colors_buffer, size: Ranges::Size(16 * 3) } }, + DescriptorWrite { binding_handle: indices_binding, array_element: 0, descriptor: Descriptor::Buffer { handle: index_buffer, size: Ranges::Size(2 * 3) } }, ]); let pipeline_layout = renderer.create_pipeline_layout(&[descriptor_set_layout_handle], &[]); diff --git a/src/rendering/visibility_model/render_domain.rs b/src/rendering/visibility_model/render_domain.rs index 7fbaffa6..64956ad0 100644 --- a/src/rendering/visibility_model/render_domain.rs +++ b/src/rendering/visibility_model/render_domain.rs @@ -17,7 +17,7 @@ struct VisibilityInfo { struct ToneMapPass { pipeline_layout: render_system::PipelineLayoutHandle, pipeline: render_system::PipelineHandle, - descriptor_set_layout: render_system::DescriptorSetLayoutHandle, + descriptor_set_layout: render_system::DescriptorSetTemplateHandle, descriptor_set: render_system::DescriptorSetHandle, } @@ -52,9 +52,11 @@ pub struct VisibilityWorldRenderDomain { camera_data_buffer_handle: render_system::BaseBufferHandle, materials_data_buffer_handle: render_system::BaseBufferHandle, - descriptor_set_layout: render_system::DescriptorSetLayoutHandle, + descriptor_set_layout: render_system::DescriptorSetTemplateHandle, descriptor_set: render_system::DescriptorSetHandle, + textures_binding: render_system::DescriptorSetBindingHandle, + transfer_synchronizer: render_system::SynchronizerHandle, transfer_command_buffer: render_system::CommandBufferHandle, @@ -92,7 +94,7 @@ pub struct VisibilityWorldRenderDomain { material_evaluation_dispatches: render_system::BaseBufferHandle, material_xy: render_system::BaseBufferHandle, - material_evaluation_descriptor_set_layout: render_system::DescriptorSetLayoutHandle, + material_evaluation_descriptor_set_layout: render_system::DescriptorSetTemplateHandle, material_evaluation_descriptor_set: render_system::DescriptorSetHandle, material_evaluation_pipeline_layout: render_system::PipelineLayoutHandle, @@ -418,7 +420,7 @@ impl VisibilityWorldRenderDomain { let render_system: &mut render_system::RenderSystemImplementation = render_system.downcast_mut().unwrap(); let bindings = [ - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "CameraData", binding: 0, descriptor_type: render_system::DescriptorType::StorageBuffer, @@ -426,7 +428,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::MESH | render_system::Stages::FRAGMENT | render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "MeshData", binding: 1, descriptor_type: render_system::DescriptorType::StorageBuffer, @@ -434,7 +436,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::MESH | render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "vertex positions", binding: 2, descriptor_type: render_system::DescriptorType::StorageBuffer, @@ -442,7 +444,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::MESH | render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "vertex normals", binding: 3, descriptor_type: render_system::DescriptorType::StorageBuffer, @@ -450,7 +452,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::MESH | render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "vertex indices", binding: 4, descriptor_type: render_system::DescriptorType::StorageBuffer, @@ -458,7 +460,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::MESH | render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "primitive indices", binding: 5, descriptor_type: render_system::DescriptorType::StorageBuffer, @@ -466,7 +468,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::MESH | render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "meshlet data", binding: 6, descriptor_type: render_system::DescriptorType::StorageBuffer, @@ -474,7 +476,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::MESH | render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "textures", binding: 7, descriptor_type: render_system::DescriptorType::CombinedImageSampler, @@ -484,9 +486,18 @@ impl VisibilityWorldRenderDomain { }, ]; - let descriptor_set_layout = render_system.create_descriptor_set_layout(Some("Base Set Layout"), &bindings); + let descriptor_set_layout = render_system.create_descriptor_set_template(Some("Base Set Layout"), &bindings); + + let descriptor_set = render_system.create_descriptor_set(Some("Base Descriptor Set"), &descriptor_set_layout); - let descriptor_set = render_system.create_descriptor_set(Some("Base Descriptor Set"), &descriptor_set_layout, &bindings); + let camera_data_binding = render_system.create_descriptor_binding(descriptor_set, &bindings[0]); + let meshes_data_binding = render_system.create_descriptor_binding(descriptor_set, &bindings[1]); + let vertex_positions_binding = render_system.create_descriptor_binding(descriptor_set, &bindings[2]); + let vertex_normals_binding = render_system.create_descriptor_binding(descriptor_set, &bindings[3]); + let vertex_indices_binding = render_system.create_descriptor_binding(descriptor_set, &bindings[4]); + let primitive_indices_binding = render_system.create_descriptor_binding(descriptor_set, &bindings[5]); + let meshlets_data_binding = render_system.create_descriptor_binding(descriptor_set, &bindings[6]); + let textures_binding = render_system.create_descriptor_binding(descriptor_set, &bindings[7]); let pipeline_layout_handle = render_system.create_pipeline_layout(&[descriptor_set_layout], &[]); @@ -516,44 +527,37 @@ impl VisibilityWorldRenderDomain { render_system.write(&[ render_system::DescriptorWrite { - descriptor_set, - binding: 0, + binding_handle: camera_data_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: camera_data_buffer_handle, size: render_system::Ranges::Whole }, }, render_system::DescriptorWrite { - descriptor_set, - binding: 1, + binding_handle: meshes_data_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: meshes_data_buffer, size: render_system::Ranges::Whole }, }, render_system::DescriptorWrite { - descriptor_set, - binding: 2, + binding_handle: vertex_positions_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: vertex_positions_buffer_handle, size: render_system::Ranges::Whole }, }, render_system::DescriptorWrite { - descriptor_set, - binding: 3, + binding_handle: vertex_normals_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: vertex_normals_buffer_handle, size: render_system::Ranges::Whole }, }, render_system::DescriptorWrite { - descriptor_set, - binding: 4, + binding_handle: vertex_indices_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: vertex_indices_buffer_handle, size: render_system::Ranges::Whole }, }, render_system::DescriptorWrite { - descriptor_set, - binding: 5, + binding_handle: primitive_indices_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: primitive_indices_buffer_handle, size: render_system::Ranges::Whole }, }, render_system::DescriptorWrite { - descriptor_set, - binding: 6, + binding_handle: meshlets_data_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer { handle: meshlets_data_buffer, size: render_system::Ranges::Whole }, }, @@ -616,7 +620,7 @@ impl VisibilityWorldRenderDomain { let material_xy = render_system.create_buffer(Some("Material XY"), 1920 * 1080 * 2 * 2, render_system::Uses::Storage | render_system::Uses::TransferDestination, render_system::DeviceAccesses::GpuWrite | render_system::DeviceAccesses::GpuRead, render_system::UseCases::STATIC); let bindings = [ - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "MaterialCount", binding: 0, descriptor_type: render_system::DescriptorType::StorageBuffer, @@ -624,7 +628,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "MaterialOffset", binding: 1, descriptor_type: render_system::DescriptorType::StorageBuffer, @@ -632,7 +636,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "MaterialOffsetScratch", binding: 2, descriptor_type: render_system::DescriptorType::StorageBuffer, @@ -640,7 +644,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "MaterialEvaluationDispatches", binding: 3, descriptor_type: render_system::DescriptorType::StorageBuffer, @@ -648,7 +652,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "MaterialXY", binding: 4, descriptor_type: render_system::DescriptorType::StorageBuffer, @@ -656,7 +660,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "MaterialId", binding: 5, descriptor_type: render_system::DescriptorType::StorageImage, @@ -664,7 +668,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "VertexId", binding: 6, descriptor_type: render_system::DescriptorType::StorageImage, @@ -672,7 +676,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "InstanceId", binding: 7, descriptor_type: render_system::DescriptorType::StorageImage, @@ -682,50 +686,52 @@ impl VisibilityWorldRenderDomain { }, ]; - let visibility_descriptor_set_layout = render_system.create_descriptor_set_layout(Some("Visibility Set Layout"), &bindings); + let visibility_descriptor_set_layout = render_system.create_descriptor_set_template(Some("Visibility Set Layout"), &bindings); let visibility_pass_pipeline_layout = render_system.create_pipeline_layout(&[descriptor_set_layout, visibility_descriptor_set_layout], &[]); - let visibility_passes_descriptor_set = render_system.create_descriptor_set(Some("Visibility Descriptor Set"), &visibility_descriptor_set_layout, &bindings); + let visibility_passes_descriptor_set = render_system.create_descriptor_set(Some("Visibility Descriptor Set"), &visibility_descriptor_set_layout); + + let material_count_binding = render_system.create_descriptor_binding(visibility_passes_descriptor_set, &bindings[0]); + let material_offset_binding = render_system.create_descriptor_binding(visibility_passes_descriptor_set, &bindings[1]); + let material_offset_scratch_binding = render_system.create_descriptor_binding(visibility_passes_descriptor_set, &bindings[2]); + let material_evaluation_dispatches_binding = render_system.create_descriptor_binding(visibility_passes_descriptor_set, &bindings[3]); + let material_xy_binding = render_system.create_descriptor_binding(visibility_passes_descriptor_set, &bindings[4]); + let material_id_binding = render_system.create_descriptor_binding(visibility_passes_descriptor_set, &bindings[5]); + let vertex_id_binding = render_system.create_descriptor_binding(visibility_passes_descriptor_set, &bindings[6]); + let instance_id_binding = render_system.create_descriptor_binding(visibility_passes_descriptor_set, &bindings[7]); render_system.write(&[ render_system::DescriptorWrite { // MaterialCount - descriptor_set: visibility_passes_descriptor_set, - binding: 0, + binding_handle: material_count_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: material_count, size: render_system::Ranges::Whole }, }, render_system::DescriptorWrite { // MaterialOffset - descriptor_set: visibility_passes_descriptor_set, - binding: 1, + binding_handle: material_offset_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: material_offset, size: render_system::Ranges::Whole }, }, render_system::DescriptorWrite { // MaterialOffsetScratch - descriptor_set: visibility_passes_descriptor_set, - binding: 2, + binding_handle: material_offset_scratch_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: material_offset_scratch, size: render_system::Ranges::Whole }, }, render_system::DescriptorWrite { // MaterialEvaluationDispatches - descriptor_set: visibility_passes_descriptor_set, - binding: 3, + binding_handle: material_evaluation_dispatches_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: material_evaluation_dispatches, size: render_system::Ranges::Whole }, }, render_system::DescriptorWrite { // MaterialXY - descriptor_set: visibility_passes_descriptor_set, - binding: 4, + binding_handle: material_xy_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: material_xy, size: render_system::Ranges::Whole }, }, render_system::DescriptorWrite { // Primitive Index - descriptor_set: visibility_passes_descriptor_set, - binding: 6, + binding_handle: vertex_id_binding, array_element: 0, descriptor: render_system::Descriptor::Image{ handle: primitive_index, layout: render_system::Layouts::General }, }, render_system::DescriptorWrite { // InstanceId - descriptor_set: visibility_passes_descriptor_set, - binding: 7, + binding_handle: instance_id_binding, array_element: 0, descriptor: render_system::Descriptor::Image{ handle: instance_id, layout: render_system::Layouts::General }, }, @@ -749,7 +755,7 @@ impl VisibilityWorldRenderDomain { let materials_data_buffer_handle = render_system.create_buffer(Some("Materials Data"), MAX_MATERIALS * std::mem::size_of::(), render_system::Uses::Storage | render_system::Uses::TransferDestination, render_system::DeviceAccesses::CpuWrite | render_system::DeviceAccesses::GpuRead, render_system::UseCases::DYNAMIC); let bindings = [ - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "albedo", binding: 0, descriptor_type: render_system::DescriptorType::StorageImage, @@ -757,81 +763,41 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { - name: "MeshBuffer", - binding: 1, - descriptor_type: render_system::DescriptorType::StorageBuffer, - descriptor_count: 1, - stages: render_system::Stages::COMPUTE, - immutable_samplers: None, - }, - render_system::DescriptorSetLayoutBinding { - name: "Positions", - binding: 2, - descriptor_type: render_system::DescriptorType::StorageBuffer, - descriptor_count: 1, - stages: render_system::Stages::COMPUTE, - immutable_samplers: None, - }, - render_system::DescriptorSetLayoutBinding { - name: "Normals", - binding: 3, - descriptor_type: render_system::DescriptorType::StorageBuffer, - descriptor_count: 1, - stages: render_system::Stages::COMPUTE, - immutable_samplers: None, - }, - render_system::DescriptorSetLayoutBinding { - name: "Indeces", - binding: 4, - descriptor_type: render_system::DescriptorType::StorageBuffer, - descriptor_count: 1, - stages: render_system::Stages::COMPUTE, - immutable_samplers: None, - }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "CameraData", - binding: 5, - descriptor_type: render_system::DescriptorType::StorageBuffer, - descriptor_count: 1, - stages: render_system::Stages::COMPUTE, - immutable_samplers: None, - }, - render_system::DescriptorSetLayoutBinding { - name: "MeshData", - binding: 6, + binding: 1, descriptor_type: render_system::DescriptorType::StorageBuffer, descriptor_count: 1, stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "debug_position", - binding: 7, + binding: 2, descriptor_type: render_system::DescriptorType::StorageImage, descriptor_count: 1, stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "debug_normals", - binding: 8, + binding: 3, descriptor_type: render_system::DescriptorType::StorageImage, descriptor_count: 1, stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "LightData", - binding: 9, + binding: 4, descriptor_type: render_system::DescriptorType::StorageBuffer, descriptor_count: 1, stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "MaterialsData", - binding: 10, + binding: 5, descriptor_type: render_system::DescriptorType::StorageBuffer, descriptor_count: 1, stages: render_system::Stages::COMPUTE, @@ -839,73 +805,44 @@ impl VisibilityWorldRenderDomain { }, ]; - let material_evaluation_descriptor_set_layout = render_system.create_descriptor_set_layout(Some("Material Evaluation Set Layout"), &bindings); - let material_evaluation_descriptor_set = render_system.create_descriptor_set(Some("Material Evaluation Descriptor Set"), &material_evaluation_descriptor_set_layout, &bindings); + let material_evaluation_descriptor_set_layout = render_system.create_descriptor_set_template(Some("Material Evaluation Set Layout"), &bindings); + let material_evaluation_descriptor_set = render_system.create_descriptor_set(Some("Material Evaluation Descriptor Set"), &material_evaluation_descriptor_set_layout); + + let albedo_binding = render_system.create_descriptor_binding(material_evaluation_descriptor_set, &bindings[0]); + let camera_data_binding = render_system.create_descriptor_binding(material_evaluation_descriptor_set, &bindings[1]); + let debug_position_binding = render_system.create_descriptor_binding(material_evaluation_descriptor_set, &bindings[2]); + let debug_normals_binding = render_system.create_descriptor_binding(material_evaluation_descriptor_set, &bindings[3]); + let light_data_binding = render_system.create_descriptor_binding(material_evaluation_descriptor_set, &bindings[4]); + let materials_data_binding = render_system.create_descriptor_binding(material_evaluation_descriptor_set, &bindings[5]); render_system.write(&[ render_system::DescriptorWrite { // albedo - descriptor_set: material_evaluation_descriptor_set, - binding: 0, + binding_handle: albedo_binding, array_element: 0, descriptor: render_system::Descriptor::Image{ handle: albedo, layout: render_system::Layouts::General }, }, - render_system::DescriptorWrite { // MeshBuffer - descriptor_set: material_evaluation_descriptor_set, - binding: 1, - array_element: 0, - descriptor: render_system::Descriptor::Buffer{ handle: meshes_data_buffer, size: render_system::Ranges::Whole }, - }, - render_system::DescriptorWrite { // Positions - descriptor_set: material_evaluation_descriptor_set, - binding: 2, - array_element: 0, - descriptor: render_system::Descriptor::Buffer{ handle: vertex_positions_buffer_handle, size: render_system::Ranges::Whole }, - }, - render_system::DescriptorWrite { // Normals - descriptor_set: material_evaluation_descriptor_set, - binding: 3, - array_element: 0, - descriptor: render_system::Descriptor::Buffer{ handle: vertex_normals_buffer_handle, size: render_system::Ranges::Whole }, - }, - render_system::DescriptorWrite { // Indeces - descriptor_set: material_evaluation_descriptor_set, - binding: 4, - array_element: 0, - descriptor: render_system::Descriptor::Buffer{ handle: vertex_indices_buffer_handle, size: render_system::Ranges::Whole }, - }, render_system::DescriptorWrite { // CameraData - descriptor_set: material_evaluation_descriptor_set, - binding: 5, + binding_handle: camera_data_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: camera_data_buffer_handle, size: render_system::Ranges::Whole }, }, - render_system::DescriptorWrite { // MeshData - descriptor_set: material_evaluation_descriptor_set, - binding: 6, - array_element: 0, - descriptor: render_system::Descriptor::Buffer{ handle: meshes_data_buffer, size: render_system::Ranges::Whole }, - }, render_system::DescriptorWrite { // debug_position - descriptor_set: material_evaluation_descriptor_set, - binding: 7, + binding_handle: debug_position_binding, array_element: 0, descriptor: render_system::Descriptor::Image{ handle: debug_position, layout: render_system::Layouts::General } }, render_system::DescriptorWrite { // debug_normals - descriptor_set: material_evaluation_descriptor_set, - binding: 8, + binding_handle: debug_normals_binding, array_element: 0, descriptor: render_system::Descriptor::Image{ handle: debug_normals, layout: render_system::Layouts::General } }, render_system::DescriptorWrite { // LightData - descriptor_set: material_evaluation_descriptor_set, - binding: 9, + binding_handle: light_data_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: light_data_buffer, size: render_system::Ranges::Whole }, }, render_system::DescriptorWrite { // MaterialsData - descriptor_set: material_evaluation_descriptor_set, - binding: 10, + binding_handle: materials_data_binding, array_element: 0, descriptor: render_system::Descriptor::Buffer{ handle: materials_data_buffer_handle, size: render_system::Ranges::Whole }, }, @@ -916,8 +853,8 @@ impl VisibilityWorldRenderDomain { let result = render_system.create_image(Some("result"), Extent::new(1920, 1080, 1), render_system::Formats::RGBAu8, None, render_system::Uses::Storage | render_system::Uses::TransferDestination, render_system::DeviceAccesses::GpuWrite | render_system::DeviceAccesses::GpuRead, render_system::UseCases::DYNAMIC); let tone_map_pass = { - let descriptor_set_layout = render_system.create_descriptor_set_layout(Some("Tonemap Pass Set Layout"), &[ - render_system::DescriptorSetLayoutBinding { + let bindings = [ + render_system::DescriptorSetBindingTemplate { name: "source", binding: 0, descriptor_type: render_system::DescriptorType::StorageImage, @@ -925,7 +862,7 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - render_system::DescriptorSetLayoutBinding { + render_system::DescriptorSetBindingTemplate { name: "result", binding: 1, descriptor_type: render_system::DescriptorType::StorageImage, @@ -933,39 +870,25 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::COMPUTE, immutable_samplers: None, }, - ]); + ]; + + let descriptor_set_layout = render_system.create_descriptor_set_template(Some("Tonemap Pass Set Layout"), &bindings); let pipeline_layout = render_system.create_pipeline_layout(&[descriptor_set_layout], &[]); - let descriptor_set = render_system.create_descriptor_set(Some("Tonemap Pass Descriptor Set"), &descriptor_set_layout, &[ - render_system::DescriptorSetLayoutBinding { - name: "source", - binding: 0, - descriptor_type: render_system::DescriptorType::StorageImage, - descriptor_count: 1, - stages: render_system::Stages::COMPUTE, - immutable_samplers: None, - }, - render_system::DescriptorSetLayoutBinding { - name: "result", - binding: 1, - descriptor_type: render_system::DescriptorType::StorageImage, - descriptor_count: 1, - stages: render_system::Stages::COMPUTE, - immutable_samplers: None, - }, - ]); + let descriptor_set = render_system.create_descriptor_set(Some("Tonemap Pass Descriptor Set"), &descriptor_set_layout); + + let albedo_binding = render_system.create_descriptor_binding(descriptor_set, &bindings[0]); + let result_binding = render_system.create_descriptor_binding(descriptor_set, &bindings[1]); render_system.write(&[ render_system::DescriptorWrite { - descriptor_set, - binding: 0, + binding_handle: albedo_binding, array_element: 0, descriptor: render_system::Descriptor::Image{ handle: albedo, layout: render_system::Layouts::General }, }, render_system::DescriptorWrite { - descriptor_set, - binding: 1, + binding_handle: result_binding, array_element: 0, descriptor: render_system::Descriptor::Image{ handle: result, layout: render_system::Layouts::General }, }, @@ -986,8 +909,8 @@ impl VisibilityWorldRenderDomain { let top_level_acceleration_structure = render_system.create_top_level_acceleration_structure(Some("Top Level Acceleration Structure")); - let rt_pass_descriptor_set_layout = render_system.create_descriptor_set_layout(Some("RT Pass Set Layout"), &[ - render_system::DescriptorSetLayoutBinding { + let bindings = [ + render_system::DescriptorSetBindingTemplate { name: "top level acc str", binding: 0, descriptor_type: render_system::DescriptorType::AccelerationStructure, @@ -995,25 +918,19 @@ impl VisibilityWorldRenderDomain { stages: render_system::Stages::RAYGEN, immutable_samplers: None, }, - ]); + ]; + + let rt_pass_descriptor_set_layout = render_system.create_descriptor_set_template(Some("RT Pass Set Layout"), &bindings); let _rt_pass_pipeline_layout = render_system.create_pipeline_layout(&[descriptor_set_layout, rt_pass_descriptor_set_layout], &[]); + + let rt_pass_descriptor_set = render_system.create_descriptor_set(Some("RT Pass Descriptor Set"), &rt_pass_descriptor_set_layout); - let rt_pass_descriptor_set = render_system.create_descriptor_set(Some("RT Pass Descriptor Set"), &rt_pass_descriptor_set_layout, &[ - render_system::DescriptorSetLayoutBinding { - name: "top level acc str", - binding: 0, - descriptor_type: render_system::DescriptorType::AccelerationStructure, - descriptor_count: 1, - stages: render_system::Stages::RAYGEN, - immutable_samplers: None, - }, - ]); + let top_level_binding = render_system.create_descriptor_binding(rt_pass_descriptor_set, &bindings[0]); render_system.write(&[ render_system::DescriptorWrite { - descriptor_set: rt_pass_descriptor_set, - binding: 0, + binding_handle: top_level_binding, array_element: 0, descriptor: render_system::Descriptor::AccelerationStructure{ handle: top_level_acceleration_structure }, }, @@ -1048,6 +965,8 @@ void main() { descriptor_set_layout, descriptor_set, + textures_binding, + albedo, depth_target, result, @@ -1148,8 +1067,7 @@ void main() { render_system.write(&[ render_system::DescriptorWrite { - descriptor_set: self.descriptor_set, - binding: 5, + binding_handle: self.textures_binding, array_element: 0, descriptor: render_system::Descriptor::CombinedImageSampler { image_handle: new_texture, sampler_handle: sampler, layout: render_system::Layouts::Read }, }, diff --git a/src/rendering/visibility_shader_generator.rs b/src/rendering/visibility_shader_generator.rs index 261095e7..7f8d4547 100644 --- a/src/rendering/visibility_shader_generator.rs +++ b/src/rendering/visibility_shader_generator.rs @@ -158,8 +158,8 @@ layout(set=1,binding=4,scalar) buffer PixelMapping { layout(set=1, binding=6, r32ui) uniform readonly uimage2D triangle_index; layout(set=1, binding=7, r32ui) uniform readonly uimage2D instance_id; layout(set=2, binding=0, rgba16) uniform image2D out_albedo; -layout(set=2, binding=7, rgba16) uniform image2D out_position; -layout(set=2, binding=8, rgba16) uniform image2D out_normals; +layout(set=2, binding=2, rgba16) uniform image2D out_position; +layout(set=2, binding=3, rgba16) uniform image2D out_normals; struct BarycentricDeriv { vec3 lambda; @@ -295,7 +295,7 @@ struct Camera { mat4 view_projection; }; -layout(set=2,binding=5,scalar) buffer CameraBuffer { +layout(set=2,binding=1,scalar) buffer CameraBuffer { Camera camera; }; @@ -313,11 +313,11 @@ struct Material { uint textures[16]; }; -layout(set=2,binding=9,scalar) buffer readonly LightingBuffer { +layout(set=2,binding=4,scalar) buffer readonly LightingBuffer { LightingData lighting_data; }; -layout(set=2,binding=10,scalar) buffer readonly MaterialsBuffer { +layout(set=2,binding=5,scalar) buffer readonly MaterialsBuffer { Material materials[]; }; diff --git a/src/rendering/vulkan_render_system.rs b/src/rendering/vulkan_render_system.rs index 176be34c..f68b29df 100644 --- a/src/rendering/vulkan_render_system.rs +++ b/src/rendering/vulkan_render_system.rs @@ -41,6 +41,7 @@ pub struct VulkanRenderSystem { textures: Vec, allocations: Vec, descriptor_sets_layouts: Vec, + bindings: Vec, descriptor_sets: Vec, meshes: Vec, acceleration_structures: Vec, @@ -147,10 +148,8 @@ impl render_system::RenderSystem for VulkanRenderSystem { } } - fn create_descriptor_set_layout(&mut self, name: Option<&str>, bindings: &[render_system::DescriptorSetLayoutBinding]) -> render_system::DescriptorSetLayoutHandle { - let mut map: HashMap = HashMap::new(); - - fn m(rs: &mut VulkanRenderSystem, bindings: &[render_system::DescriptorSetLayoutBinding], layout_bindings: &mut Vec, map: &mut HashMap) -> vk::DescriptorSetLayout { + fn create_descriptor_set_template(&mut self, name: Option<&str>, bindings: &[render_system::DescriptorSetBindingTemplate]) -> render_system::DescriptorSetTemplateHandle { + fn m(rs: &mut VulkanRenderSystem, bindings: &[render_system::DescriptorSetBindingTemplate], layout_bindings: &mut Vec, map: &mut Vec<(vk::DescriptorType, u32)>) -> vk::DescriptorSetLayout { if let Some(binding) = bindings.get(0) { let b = vk::DescriptorSetLayoutBinding::default() .binding(binding.binding) @@ -174,7 +173,7 @@ impl render_system::RenderSystem for VulkanRenderSystem { b.immutable_samplers(&x); - map.insert(DSLB { binding: binding.binding, }, b.descriptor_type); + map.push((b.descriptor_type, b.descriptor_count)); layout_bindings.push(b); @@ -188,7 +187,9 @@ impl render_system::RenderSystem for VulkanRenderSystem { } } - let descriptor_set_layout = m(self, bindings, &mut Vec::new(), &mut map); + let mut bindings_list = Vec::with_capacity(8); + + let descriptor_set_layout = m(self, bindings, &mut Vec::new(), &mut bindings_list); unsafe{ if let Some(name) = name { @@ -204,29 +205,46 @@ impl render_system::RenderSystem for VulkanRenderSystem { } } - let handle = render_system::DescriptorSetLayoutHandle(self.descriptor_sets_layouts.len() as u64); + let handle = render_system::DescriptorSetTemplateHandle(self.descriptor_sets_layouts.len() as u64); self.descriptor_sets_layouts.push(DescriptorSetLayout { - map, + bindings: bindings_list, descriptor_set_layout, }); handle } - fn create_descriptor_set(&mut self, name: Option<&str>, descriptor_set_layout_handle: &render_system::DescriptorSetLayoutHandle, bindings: &[render_system::DescriptorSetLayoutBinding]) -> render_system::DescriptorSetHandle { - let pool_sizes = bindings.iter().map(|binding| { + fn create_descriptor_binding(&mut self, descriptor_set: render_system::DescriptorSetHandle, binding: &render_system::DescriptorSetBindingTemplate) -> render_system::DescriptorSetBindingHandle { + let descriptor_type = match binding.descriptor_type { + render_system::DescriptorType::UniformBuffer => vk::DescriptorType::UNIFORM_BUFFER, + render_system::DescriptorType::StorageBuffer => vk::DescriptorType::STORAGE_BUFFER, + render_system::DescriptorType::SampledImage => vk::DescriptorType::SAMPLED_IMAGE, + render_system::DescriptorType::CombinedImageSampler => vk::DescriptorType::COMBINED_IMAGE_SAMPLER, + render_system::DescriptorType::StorageImage => vk::DescriptorType::STORAGE_IMAGE, + render_system::DescriptorType::Sampler => vk::DescriptorType::SAMPLER, + render_system::DescriptorType::AccelerationStructure => vk::DescriptorType::ACCELERATION_STRUCTURE_KHR, + }; + + let created_binding = Binding { + descriptor_set_handle: descriptor_set, + descriptor_type, + count: binding.descriptor_count, + index: binding.binding, + }; + + let binding_handle = render_system::DescriptorSetBindingHandle(self.bindings.len() as u64); + + self.bindings.push(created_binding); + + binding_handle + } + + fn create_descriptor_set(&mut self, name: Option<&str>, descriptor_set_layout_handle: &render_system::DescriptorSetTemplateHandle) -> render_system::DescriptorSetHandle { + let pool_sizes = self.descriptor_sets_layouts[descriptor_set_layout_handle.0 as usize].bindings.iter().map(|(descriptor_type, descriptor_count)| { vk::DescriptorPoolSize::default() - .ty(match binding.descriptor_type { - render_system::DescriptorType::UniformBuffer => vk::DescriptorType::UNIFORM_BUFFER, - render_system::DescriptorType::StorageBuffer => vk::DescriptorType::STORAGE_BUFFER, - render_system::DescriptorType::SampledImage => vk::DescriptorType::SAMPLED_IMAGE, - render_system::DescriptorType::CombinedImageSampler => vk::DescriptorType::COMBINED_IMAGE_SAMPLER, - render_system::DescriptorType::StorageImage => vk::DescriptorType::STORAGE_IMAGE, - render_system::DescriptorType::Sampler => vk::DescriptorType::SAMPLER, - render_system::DescriptorType::AccelerationStructure => vk::DescriptorType::ACCELERATION_STRUCTURE_KHR, - }) - .descriptor_count(binding.descriptor_count * self.frames as u32) + .ty(*descriptor_type) + .descriptor_count(descriptor_count * self.frames as u32) /* .build() */ }) .collect::>(); @@ -290,17 +308,19 @@ impl render_system::RenderSystem for VulkanRenderSystem { fn write(&self, descriptor_set_writes: &[render_system::DescriptorWrite]) { for descriptor_set_write in descriptor_set_writes { - let descriptor_set = &self.descriptor_sets[descriptor_set_write.descriptor_set.0 as usize]; + let binding = &self.bindings[descriptor_set_write.binding_handle.0 as usize]; + let descriptor_set = &self.descriptor_sets[binding.descriptor_set_handle.0 as usize]; let layout = descriptor_set.descriptor_set_layout; let descriptor_set_layout = &self.descriptor_sets_layouts[layout.0 as usize]; - let descriptor_type = *descriptor_set_layout.map.get(&DSLB { binding: descriptor_set_write.binding }).unwrap(); + let descriptor_type = binding.descriptor_type; + let binding_index = binding.index; match descriptor_set_write.descriptor { render_system::Descriptor::Buffer { handle, size } => { - let mut descriptor_set_handle_option = Some(descriptor_set_write.descriptor_set); + let mut descriptor_set_handle_option = Some(binding.descriptor_set_handle); let mut _buffer_handle_option = Some(handle); while let Some(descriptor_set_handle) = descriptor_set_handle_option { @@ -311,7 +331,7 @@ impl render_system::RenderSystem for VulkanRenderSystem { let write_info = vk::WriteDescriptorSet::default() .dst_set(descriptor_set.descriptor_set) - .dst_binding(descriptor_set_write.binding) + .dst_binding(binding_index) .dst_array_element(descriptor_set_write.array_element) .descriptor_type(descriptor_type) .buffer_info(&buffers); @@ -326,7 +346,7 @@ impl render_system::RenderSystem for VulkanRenderSystem { } }, render_system::Descriptor::Image{ handle, layout } => { - let mut descriptor_set_handle_option = Some(descriptor_set_write.descriptor_set); + let mut descriptor_set_handle_option = Some(binding.descriptor_set_handle); let mut texture_handle_option = Some(handle); while let (Some(descriptor_set_handle), Some(texture_handle)) = (descriptor_set_handle_option, texture_handle_option) { @@ -341,7 +361,7 @@ impl render_system::RenderSystem for VulkanRenderSystem { let write_info = vk::WriteDescriptorSet::default() .dst_set(descriptor_set.descriptor_set) - .dst_binding(descriptor_set_write.binding) + .dst_binding(binding_index) .dst_array_element(descriptor_set_write.array_element) .descriptor_type(descriptor_type) .image_info(&images); @@ -356,7 +376,7 @@ impl render_system::RenderSystem for VulkanRenderSystem { } }, render_system::Descriptor::CombinedImageSampler{ image_handle, sampler_handle, layout } => { - let mut descriptor_set_handle_option = Some(descriptor_set_write.descriptor_set); + let mut descriptor_set_handle_option = Some(binding.descriptor_set_handle); let mut texture_handle_option = Some(image_handle); while let (Some(descriptor_set_handle), Some(texture_handle)) = (descriptor_set_handle_option, texture_handle_option) { @@ -372,7 +392,7 @@ impl render_system::RenderSystem for VulkanRenderSystem { let write_info = vk::WriteDescriptorSet::default() .dst_set(descriptor_set.descriptor_set) - .dst_binding(descriptor_set_write.binding) + .dst_binding(binding_index) .dst_array_element(descriptor_set_write.array_element) .descriptor_type(descriptor_type) .image_info(&images); @@ -387,7 +407,7 @@ impl render_system::RenderSystem for VulkanRenderSystem { } }, render_system::Descriptor::Sampler(handle) => { - let mut descriptor_set_handle_option = Some(descriptor_set_write.descriptor_set); + let mut descriptor_set_handle_option = Some(binding.descriptor_set_handle); let sampler_handle_option = Some(handle); while let (Some(descriptor_set_handle), Some(sampler_handle)) = (descriptor_set_handle_option, sampler_handle_option) { @@ -397,7 +417,7 @@ impl render_system::RenderSystem for VulkanRenderSystem { let write_info = vk::WriteDescriptorSet::default() .dst_set(descriptor_set.descriptor_set) - .dst_binding(descriptor_set_write.binding) + .dst_binding(binding_index) .dst_array_element(descriptor_set_write.array_element) .descriptor_type(descriptor_type) .image_info(&images); @@ -412,7 +432,7 @@ impl render_system::RenderSystem for VulkanRenderSystem { unimplemented!() } render_system::Descriptor::AccelerationStructure { handle } => { - let mut descriptor_set_handle_option = Some(descriptor_set_write.descriptor_set); + let mut descriptor_set_handle_option = Some(binding.descriptor_set_handle); let mut acceleration_structure_handle_option = Some(handle); while let (Some(descriptor_set_handle), Some(acceleration_structure_handle)) = (descriptor_set_handle_option, acceleration_structure_handle_option) { @@ -427,7 +447,7 @@ impl render_system::RenderSystem for VulkanRenderSystem { let write_info = vk::WriteDescriptorSet{ descriptor_count: 1, ..vk::WriteDescriptorSet::default() } .push_next(&mut acc_str_descriptor_info) .dst_set(descriptor_set.descriptor_set) - .dst_binding(descriptor_set_write.binding) + .dst_binding(binding_index) .dst_array_element(descriptor_set_write.array_element) .descriptor_type(descriptor_type); @@ -444,7 +464,7 @@ impl render_system::RenderSystem for VulkanRenderSystem { } } - fn create_pipeline_layout(&mut self, descriptor_set_layout_handles: &[render_system::DescriptorSetLayoutHandle], push_constant_ranges: &[render_system::PushConstantRange]) -> render_system::PipelineLayoutHandle { + fn create_pipeline_layout(&mut self, descriptor_set_layout_handles: &[render_system::DescriptorSetTemplateHandle], push_constant_ranges: &[render_system::PushConstantRange]) -> render_system::PipelineLayoutHandle { let push_constant_ranges = push_constant_ranges.iter().map(|push_constant_range| vk::PushConstantRange::default().size(push_constant_range.size).offset(push_constant_range.offset).stage_flags(vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::MESH_EXT | vk::ShaderStageFlags::FRAGMENT | vk::ShaderStageFlags::COMPUTE)).collect::>(); let set_layouts = descriptor_set_layout_handles.iter().map(|set_layout| self.descriptor_sets_layouts[set_layout.0 as usize].descriptor_set_layout).collect::>(); @@ -1196,15 +1216,9 @@ pub(crate) struct Swapchain { swapchain: vk::SwapchainKHR, } -#[derive(Clone, Hash, PartialEq, Eq)] -struct DSLB { - binding: u32, - // position: u32, -} - #[derive(Clone)] pub(crate) struct DescriptorSetLayout { - map: std::collections::HashMap, + bindings: Vec<(vk::DescriptorType, u32)>, descriptor_set_layout: vk::DescriptorSetLayout, } @@ -1212,7 +1226,7 @@ pub(crate) struct DescriptorSetLayout { pub(crate) struct DescriptorSet { next: Option, descriptor_set: vk::DescriptorSet, - descriptor_set_layout: render_system::DescriptorSetLayoutHandle, + descriptor_set_layout: render_system::DescriptorSetTemplateHandle, } #[derive(Clone)] @@ -1227,6 +1241,14 @@ pub(crate) struct CommandBufferInternal { command_buffer: vk::CommandBuffer, } +#[derive(Clone)] +pub(crate) struct Binding { + descriptor_set_handle: render_system::DescriptorSetHandle, + descriptor_type: vk::DescriptorType, + index: u32, + count: u32, +} + #[derive(Clone)] pub(crate) struct CommandBuffer { frames: Vec, @@ -1895,6 +1917,7 @@ impl VulkanRenderSystem { buffers: Vec::new(), textures: Vec::new(), descriptor_sets_layouts: Vec::new(), + bindings: Vec::new(), descriptor_sets: Vec::new(), acceleration_structures: Vec::new(), pipelines: Vec::new(), diff --git a/tests/gi.rs b/tests/gi.rs index 7ec90944..802e0e63 100644 --- a/tests/gi.rs +++ b/tests/gi.rs @@ -21,7 +21,7 @@ fn gi() { }); let _floor: EntityHandle = orchestrator.spawn(mesh::Mesh{ resource_id: "Box", material_id: "white_solid", transform: maths_rs::Mat4f::from_translation(Vec3f::new(0.0, -0.5, 0.0)) * maths_rs::Mat4f::from_scale(Vec3f::new(5.0, 1.0, 2.5)), }); - let _a: EntityHandle = orchestrator.spawn(mesh::Mesh{ resource_id: "Suzanne", material_id: "white_solid", transform: maths_rs::Mat4f::from_translation(Vec3f::new(0.0, 0.5, 0.0)) * maths_rs::Mat4f::from_scale(Vec3f::new(0.4, 0.4, 0.4)), }); + let _a: EntityHandle = orchestrator.spawn(mesh::Mesh{ resource_id: "Sphere", material_id: "white_solid", transform: maths_rs::Mat4f::from_translation(Vec3f::new(0.0, 0.5, 0.0)) * maths_rs::Mat4f::from_scale(Vec3f::new(0.4, 0.4, 0.4)), }); let _b: EntityHandle = orchestrator.spawn(mesh::Mesh{ resource_id: "Box", material_id: "red_solid", transform: maths_rs::Mat4f::from_translation(Vec3f::new(-0.6, 0.17, -0.1)) * maths_rs::Mat4f::from_scale(Vec3f::new(0.34, 0.34, 0.34)), }); let _c: EntityHandle = orchestrator.spawn(mesh::Mesh{ resource_id: "Box", material_id: "green_solid", transform: maths_rs::Mat4f::from_translation(Vec3f::new(0.5, 0.13, -0.3)) * maths_rs::Mat4f::from_scale(Vec3f::new(0.26, 0.26, 0.26)), });