From 2baeae494d0a1f54616f6a5fb6f88a2361393ebc Mon Sep 17 00:00:00 2001 From: chyyran Date: Tue, 6 Dec 2022 01:38:21 -0500 Subject: [PATCH] vk: pipeline layout stuff also ringbuffer fixes --- librashader-reflect/src/reflect/cross.rs | 1 + librashader-runtime-vk/src/filter_chain.rs | 8 ++- librashader-runtime-vk/src/filter_pass.rs | 70 +++++++++++++++++++++- librashader-runtime-vk/src/lib.rs | 1 + librashader-runtime/src/ringbuffer.rs | 14 +---- 5 files changed, 81 insertions(+), 13 deletions(-) diff --git a/librashader-reflect/src/reflect/cross.rs b/librashader-reflect/src/reflect/cross.rs index d7267c8..0f0e88d 100644 --- a/librashader-reflect/src/reflect/cross.rs +++ b/librashader-reflect/src/reflect/cross.rs @@ -531,6 +531,7 @@ where SemanticsErrorKind::InvalidBinding(binding), )); } + Ok(TextureData { id: texture.id, name: &texture.name, diff --git a/librashader-runtime-vk/src/filter_chain.rs b/librashader-runtime-vk/src/filter_chain.rs index 5b2a592..d362aff 100644 --- a/librashader-runtime-vk/src/filter_chain.rs +++ b/librashader-runtime-vk/src/filter_chain.rs @@ -11,7 +11,7 @@ use librashader_reflect::reflect::ReflectShader; use librashader_reflect::reflect::semantics::{Semantic, ShaderSemantics, TextureSemantics, UniformBinding, UniformSemantic, UniqueSemantics}; use librashader_runtime::uniforms::UniformStorage; use crate::error; -use crate::filter_pass::FilterPass; +use crate::filter_pass::{FilterPass, PipelineDescriptors}; pub struct Vulkan { physical_device: vk::PhysicalDevice, @@ -134,6 +134,7 @@ impl FilterChainVulkan { fn init_passes( passes: Vec, semantics: &ShaderSemantics, + images: u32, ) -> error::Result> { let mut filters = Vec::new(); @@ -156,6 +157,7 @@ impl FilterChainVulkan { ); let mut uniform_bindings = FxHashMap::default(); + for param in reflection.meta.parameter_meta.values() { uniform_bindings.insert(UniformBinding::Parameter(param.id.clone()), param.offset); } @@ -168,6 +170,10 @@ impl FilterChainVulkan { uniform_bindings.insert(UniformBinding::TextureSize(*semantics), param.offset); } + let mut pipeline = PipelineDescriptors::new(images); + pipeline.add_ubo_binding(reflection.ubo.as_ref()); + pipeline.add_texture_bindings(reflection.meta.texture_meta.values()); + // shader_vulkan 1927 (pipeline_layout) filters.push(FilterPass { compiled: spirv_words, uniform_storage, diff --git a/librashader-runtime-vk/src/filter_pass.rs b/librashader-runtime-vk/src/filter_pass.rs index a47d443..eebf312 100644 --- a/librashader-runtime-vk/src/filter_pass.rs +++ b/librashader-runtime-vk/src/filter_pass.rs @@ -2,8 +2,9 @@ use rustc_hash::FxHashMap; use librashader_preprocess::ShaderSource; use librashader_presets::ShaderPassConfig; use librashader_reflect::back::ShaderCompilerOutput; -use librashader_reflect::reflect::semantics::{MemberOffset, UniformBinding}; +use librashader_reflect::reflect::semantics::{BindingStage, MemberOffset, TextureBinding, UboReflection, UniformBinding}; use librashader_runtime::uniforms::UniformStorage; +use ash::vk; pub struct FilterPass { pub(crate) compiled: ShaderCompilerOutput>, @@ -11,4 +12,71 @@ pub struct FilterPass { pub uniform_bindings: FxHashMap, pub source: ShaderSource, pub config: ShaderPassConfig, +} + +pub struct PipelineDescriptors { + pub replicas: u32, + pub layout_bindings: Vec, + pub pool_sizes: Vec +} + +impl PipelineDescriptors { + pub fn new(duplicates: u32) -> Self { + Self { + replicas: duplicates, + layout_bindings: vec![], + pool_sizes: vec![], + } + } + + pub fn add_ubo_binding(&mut self, ubo_meta: Option<&UboReflection>) { + if let Some(ubo_meta) = ubo_meta && !ubo_meta.stage_mask.is_empty() { + let mut ubo_mask = vk::ShaderStageFlags::default(); + if ubo_meta.stage_mask.contains(BindingStage::VERTEX) { + ubo_mask |= vk::ShaderStageFlags::VERTEX; + } + if ubo_meta.stage_mask.contains(BindingStage::FRAGMENT) { + ubo_mask |= vk::ShaderStageFlags::FRAGMENT; + } + + self.layout_bindings.push(vk::DescriptorSetLayoutBinding { + binding: ubo_meta.binding, + descriptor_type: vk::DescriptorType::UNIFORM_BUFFER, + descriptor_count: 1, + stage_flags: ubo_mask, + p_immutable_samplers: std::ptr::null(), + }); + + self.pool_sizes.push(vk::DescriptorPoolSize { + ty: vk::DescriptorType::UNIFORM_BUFFER, + descriptor_count: self.replicas, + }) + } + } + + pub fn add_texture_bindings<'a>(&mut self, textures: impl Iterator) { + let mut texture_mask = vk::ShaderStageFlags::FRAGMENT; + for texture in textures { + self.layout_bindings.push(vk::DescriptorSetLayoutBinding { + binding: texture.binding, + descriptor_type: vk::DescriptorType::COMBINED_IMAGE_SAMPLER, + descriptor_count: 1, + stage_flags: texture_mask, + p_immutable_samplers: std::ptr::null(), + }); + + self.pool_sizes.push(vk::DescriptorPoolSize { + ty: vk::DescriptorType::COMBINED_IMAGE_SAMPLER, + descriptor_count: self.replicas, + }) + } + } + + pub fn binding_count(&self) -> usize { + self.layout_bindings.len() + } + + pub fn bindings(&self) -> &[vk::DescriptorSetLayoutBinding] { + self.layout_bindings.as_ref() + } } \ No newline at end of file diff --git a/librashader-runtime-vk/src/lib.rs b/librashader-runtime-vk/src/lib.rs index 86919fc..6b2128a 100644 --- a/librashader-runtime-vk/src/lib.rs +++ b/librashader-runtime-vk/src/lib.rs @@ -1,4 +1,5 @@ #![feature(type_alias_impl_trait)] +#![feature(let_chains)] mod hello_triangle; mod filter_chain; diff --git a/librashader-runtime/src/ringbuffer.rs b/librashader-runtime/src/ringbuffer.rs index 5d19c7e..e45a71c 100644 --- a/librashader-runtime/src/ringbuffer.rs +++ b/librashader-runtime/src/ringbuffer.rs @@ -79,16 +79,6 @@ impl BoxRingBuffer index: 0, } } - - /// Get a borrow to all the items in this ring buffer. - pub fn items(&self) -> &[T; SIZE] { - &self.items - } - - /// Get a mutable borrow to all the items in this ring buffer. - pub fn items_mut(&mut self) -> &mut [T; SIZE] { - &mut self.items - } } @@ -100,10 +90,12 @@ impl BoxRingBuffer { } } + /// Get a borrow to all the items in this ring buffer. pub fn items(&self) -> &[T] { &self.items } + /// Get a mutable borrow to all the items in this ring buffer. pub fn items_mut(&mut self) -> &mut [T] { &mut self.items } @@ -126,7 +118,7 @@ impl RingBuffer for BoxRingBuffer { fn next(&mut self) { self.index += 1; - if self.index >= SIZE { + if self.index >= self.items.len() { self.index = 0 } }