From 9dbe031ce8ac760e7529f35c163945d2b3a0ce53 Mon Sep 17 00:00:00 2001 From: chyyran Date: Sun, 25 Dec 2022 01:18:11 -0500 Subject: [PATCH] vk: implement vbo --- librashader-runtime-vk/src/draw_quad.rs | 47 ++++++++++++++++++- librashader-runtime-vk/src/filter_chain.rs | 29 ++++++++++-- librashader-runtime-vk/src/filter_pass.rs | 27 ++++++++++- librashader-runtime-vk/src/lib.rs | 2 + librashader-runtime-vk/src/luts.rs | 2 +- librashader-runtime-vk/src/rendertarget.rs | 8 ++++ librashader-runtime-vk/src/texture.rs | 22 +++++---- .../src/vulkan_primitives.rs | 5 +- 8 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 librashader-runtime-vk/src/rendertarget.rs diff --git a/librashader-runtime-vk/src/draw_quad.rs b/librashader-runtime-vk/src/draw_quad.rs index 2d24351..a31f7d4 100644 --- a/librashader-runtime-vk/src/draw_quad.rs +++ b/librashader-runtime-vk/src/draw_quad.rs @@ -1,5 +1,9 @@ +use ash::vk; +use crate::error; +use crate::vulkan_primitives::VulkanBuffer; + #[rustfmt::skip] -static VBO_OFFSCREEN_MVP: &[f32; 16] = &[ +static VBO_OFFSCREEN: &[f32; 16] = &[ // Offscreen -1.0, -1.0, 0.0, 0.0, -1.0, 1.0, 0.0, 1.0, @@ -8,10 +12,49 @@ static VBO_OFFSCREEN_MVP: &[f32; 16] = &[ ]; #[rustfmt::skip] -static VBO_DEFAULT_FINAL_MVP: &[f32; 16] = &[ +static VBO_DEFAULT_FINAL: &[f32; 16] = &[ // Final 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, ]; + +pub enum VboType { + Offscreen, + Final +} + +pub struct DrawQuad { + buffer: VulkanBuffer, + device: ash::Device +} + +impl DrawQuad { + pub fn new(device: &ash::Device, mem_props: &vk::PhysicalDeviceMemoryProperties) -> error::Result { + let mut buffer = VulkanBuffer::new(device, mem_props, vk::BufferUsageFlags::VERTEX_BUFFER, 2 * std::mem::size_of::<[f32; 16]>())?; + + { + let mut map = buffer.map()?; + unsafe { + map.copy_from(0, bytemuck::cast_slice(VBO_OFFSCREEN)); + map.copy_from(std::mem::size_of::<[f32; 16]>(), bytemuck::cast_slice(VBO_DEFAULT_FINAL)); + } + } + Ok(DrawQuad { + buffer, + device: device.clone() + }) + } + + pub fn bind_vbo(&self, cmd: &vk::CommandBuffer, vbo: VboType) { + let offset = match vbo { + VboType::Offscreen => 0, + VboType::Final => std::mem::size_of::<[f32; 16]>() + }; + + unsafe { + self.device.cmd_bind_vertex_buffers(*cmd, 0, &[self.buffer.handle], &[offset as vk::DeviceSize]) + } + } +} diff --git a/librashader-runtime-vk/src/filter_chain.rs b/librashader-runtime-vk/src/filter_chain.rs index db685ae..66ea9a9 100644 --- a/librashader-runtime-vk/src/filter_chain.rs +++ b/librashader-runtime-vk/src/filter_chain.rs @@ -20,6 +20,7 @@ use rustc_hash::FxHashMap; use std::error::Error; use std::path::Path; use crate::samplers::SamplerSet; +use crate::texture::VulkanImage; pub struct Vulkan { // physical_device: vk::PhysicalDevice, @@ -145,7 +146,7 @@ pub type FilterChainOptionsVulkan = (); impl FilterChainVulkan { /// Load the shader preset at the given path into a filter chain. pub fn load_from_path( - vulkan: impl TryInto>, + vulkan: impl TryInto>, path: impl AsRef, options: Option<&FilterChainOptionsVulkan>, ) -> error::Result { @@ -155,7 +156,7 @@ impl FilterChainVulkan { } pub fn load_from_preset( - vulkan: impl TryInto>, + vulkan: impl TryInto>, preset: ShaderPreset, options: Option<&FilterChainOptionsVulkan>, ) -> error::Result { @@ -167,9 +168,12 @@ impl FilterChainVulkan { let luts = FilterChainVulkan::load_luts(&device, &preset.textures)?; let samplers = SamplerSet::new(&device.device)?; + + eprintln!("filters initialized ok."); Ok(FilterChainVulkan { - common: FilterCommon { luts, + common: FilterCommon { + luts, samplers, config: FilterMutable { passes_enabled: preset.shader_count as usize, @@ -213,8 +217,7 @@ impl FilterChainVulkan { Ok::<_, Box>((shader, source, reflect)) }) .into_iter() - .collect::)>>>( - )?; + .collect::)>>>()?; for details in &passes { librashader_runtime::semantics::insert_pass_semantics( @@ -355,4 +358,20 @@ impl FilterChainVulkan { } Ok(luts) } + + /// Process a frame with the input image. + /// + /// When this frame returns, GL_FRAMEBUFFER is bound to 0. + pub fn frame( + &mut self, + count: usize, + viewport: &vk::Viewport, + input: &VulkanImage, + options: Option<()>, + ) -> error::Result<()> { + // limit number of passes to those enabled. + let passes = &mut self.passes[0..self.common.config.passes_enabled]; + + + } } diff --git a/librashader-runtime-vk/src/filter_pass.rs b/librashader-runtime-vk/src/filter_pass.rs index af964b7..daea0cc 100644 --- a/librashader-runtime-vk/src/filter_pass.rs +++ b/librashader-runtime-vk/src/filter_pass.rs @@ -9,6 +9,7 @@ use rustc_hash::FxHashMap; use librashader_common::Size; use librashader_reflect::reflect::ShaderReflection; use crate::filter_chain::FilterCommon; +use crate::rendertarget::RenderTarget; use crate::texture::Texture; use crate::samplers::{SamplerSet, VulkanSampler}; @@ -52,6 +53,30 @@ impl FilterPass { } + pub(crate) fn draw( + &mut self, + pass_index: usize, + parent: &FilterCommon, + frame_count: u32, + frame_direction: i32, + descriptor: &vk::DescriptorSet, + viewport: &vk::Viewport, + original: &Texture, + source: &Texture, + output: &RenderTarget, + ) { + self.build_semantics(pass_index, parent, &output.mvp, frame_count, frame_direction, Size::new(100, 100), + Size::new(100,100),descriptor, original, source); + + if let Some(ubo) = &self.reflection.ubo { + // shader_vulkan: 2554 (ra uses uses one big buffer) + } + } + + fn bind_ubo(device: &vk::Device, descriptor: &vk::DescriptorSet, binding: u32, buffer: &vk::Buffer, offset: vk::DeviceSize, range: vk::DeviceSize) { + + } + fn build_semantics( &mut self, pass_index: usize, @@ -61,7 +86,7 @@ impl FilterPass { frame_direction: i32, fb_size: Size, viewport_size: Size, - descriptor_set: vk::DescriptorSet, + descriptor_set: &vk::DescriptorSet, original: &Texture, source: &Texture, ) { diff --git a/librashader-runtime-vk/src/lib.rs b/librashader-runtime-vk/src/lib.rs index 42f0638..5de32b3 100644 --- a/librashader-runtime-vk/src/lib.rs +++ b/librashader-runtime-vk/src/lib.rs @@ -1,5 +1,6 @@ #![feature(type_alias_impl_trait)] #![feature(let_chains)] +#![feature(strict_provenance)] mod draw_quad; mod error; @@ -14,6 +15,7 @@ mod vulkan_primitives; mod vulkan_state; mod samplers; mod texture; +mod rendertarget; #[cfg(test)] mod tests { diff --git a/librashader-runtime-vk/src/luts.rs b/librashader-runtime-vk/src/luts.rs index 8f90321..1a593d8 100644 --- a/librashader-runtime-vk/src/luts.rs +++ b/librashader-runtime-vk/src/luts.rs @@ -94,7 +94,7 @@ impl LutTexture { )?; unsafe { let mut handle = staging.map()?; - handle.copy_from(&image.bytes) + handle.copy_from(0, &image.bytes) } unsafe { diff --git a/librashader-runtime-vk/src/rendertarget.rs b/librashader-runtime-vk/src/rendertarget.rs new file mode 100644 index 0000000..8fd826a --- /dev/null +++ b/librashader-runtime-vk/src/rendertarget.rs @@ -0,0 +1,8 @@ +use ash::vk; +use crate::framebuffer::VulkanFramebuffer; + +pub struct RenderTarget { + pub mvp: [f32; 16], + pub image: vk::Image, + pub framebuffer: VulkanFramebuffer +} \ No newline at end of file diff --git a/librashader-runtime-vk/src/texture.rs b/librashader-runtime-vk/src/texture.rs index 89d5dc1..a0a47fa 100644 --- a/librashader-runtime-vk/src/texture.rs +++ b/librashader-runtime-vk/src/texture.rs @@ -10,10 +10,8 @@ use crate::vulkan_primitives::VulkanImageMemory; pub struct OwnedTexture { pub device: ash::Device, pub image_view: vk::ImageView, - pub image: vk::Image, - pub format: vk::Format, + pub image: VulkanImage, pub memory: VulkanImageMemory, - pub size: Size, pub max_miplevels: u32, } @@ -85,9 +83,11 @@ impl OwnedTexture { Ok(OwnedTexture { device: vulkan.device.clone(), image_view, - image, - size, - format: format.into(), + image: VulkanImage { + image, + size, + format: format.into() + }, memory, max_miplevels, }) @@ -130,17 +130,21 @@ impl Drop for OwnedTexture { self.device.destroy_image_view(self.image_view, None); } if self.image != vk::Image::null() { - self.device.destroy_image(self.image, None); + self.device.destroy_image(self.image.image, None); } } } } -pub struct Texture { +pub struct VulkanImage { pub size: Size, - pub image_view: vk::ImageView, pub image: vk::Image, pub format: vk::Format, +} + +pub struct Texture { + pub image: VulkanImage, + pub image_view: vk::ImageView, pub wrap_mode: WrapMode, pub filter_mode: FilterMode, pub mip_filter: FilterMode, diff --git a/librashader-runtime-vk/src/vulkan_primitives.rs b/librashader-runtime-vk/src/vulkan_primitives.rs index 63022dd..8a2febe 100644 --- a/librashader-runtime-vk/src/vulkan_primitives.rs +++ b/librashader-runtime-vk/src/vulkan_primitives.rs @@ -123,8 +123,9 @@ impl Drop for VulkanBuffer { } impl<'a> VulkanBufferMapHandle<'a> { - pub unsafe fn copy_from(&mut self, src: &[u8]) { - std::ptr::copy_nonoverlapping(src.as_ptr(), self.ptr.cast(), src.len()); + pub unsafe fn copy_from(&mut self, offset: usize, src: &[u8]) { + std::ptr::copy_nonoverlapping(src.as_ptr(), + self.ptr.map_addr(|original| original.wrapping_add(offset)).cast(), src.len()); } }