vk: implement vbo

This commit is contained in:
chyyran 2022-12-25 01:18:11 -05:00
parent 2038da787e
commit 9dbe031ce8
8 changed files with 122 additions and 20 deletions

View file

@ -1,5 +1,9 @@
use ash::vk;
use crate::error;
use crate::vulkan_primitives::VulkanBuffer;
#[rustfmt::skip] #[rustfmt::skip]
static VBO_OFFSCREEN_MVP: &[f32; 16] = &[ static VBO_OFFSCREEN: &[f32; 16] = &[
// Offscreen // Offscreen
-1.0, -1.0, 0.0, 0.0, -1.0, -1.0, 0.0, 0.0,
-1.0, 1.0, 0.0, 1.0, -1.0, 1.0, 0.0, 1.0,
@ -8,10 +12,49 @@ static VBO_OFFSCREEN_MVP: &[f32; 16] = &[
]; ];
#[rustfmt::skip] #[rustfmt::skip]
static VBO_DEFAULT_FINAL_MVP: &[f32; 16] = &[ static VBO_DEFAULT_FINAL: &[f32; 16] = &[
// Final // Final
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0,
1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0,
1.0, 1.0, 1.0, 1.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<DrawQuad> {
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])
}
}
}

View file

@ -20,6 +20,7 @@ use rustc_hash::FxHashMap;
use std::error::Error; use std::error::Error;
use std::path::Path; use std::path::Path;
use crate::samplers::SamplerSet; use crate::samplers::SamplerSet;
use crate::texture::VulkanImage;
pub struct Vulkan { pub struct Vulkan {
// physical_device: vk::PhysicalDevice, // physical_device: vk::PhysicalDevice,
@ -145,7 +146,7 @@ pub type FilterChainOptionsVulkan = ();
impl FilterChainVulkan { impl FilterChainVulkan {
/// Load the shader preset at the given path into a filter chain. /// Load the shader preset at the given path into a filter chain.
pub fn load_from_path( pub fn load_from_path(
vulkan: impl TryInto<Vulkan, Error = Box<dyn Error>>, vulkan: impl TryInto<Vulkan, Error=Box<dyn Error>>,
path: impl AsRef<Path>, path: impl AsRef<Path>,
options: Option<&FilterChainOptionsVulkan>, options: Option<&FilterChainOptionsVulkan>,
) -> error::Result<FilterChainVulkan> { ) -> error::Result<FilterChainVulkan> {
@ -155,7 +156,7 @@ impl FilterChainVulkan {
} }
pub fn load_from_preset( pub fn load_from_preset(
vulkan: impl TryInto<Vulkan, Error = Box<dyn Error>>, vulkan: impl TryInto<Vulkan, Error=Box<dyn Error>>,
preset: ShaderPreset, preset: ShaderPreset,
options: Option<&FilterChainOptionsVulkan>, options: Option<&FilterChainOptionsVulkan>,
) -> error::Result<FilterChainVulkan> { ) -> error::Result<FilterChainVulkan> {
@ -167,9 +168,12 @@ impl FilterChainVulkan {
let luts = FilterChainVulkan::load_luts(&device, &preset.textures)?; let luts = FilterChainVulkan::load_luts(&device, &preset.textures)?;
let samplers = SamplerSet::new(&device.device)?; let samplers = SamplerSet::new(&device.device)?;
eprintln!("filters initialized ok."); eprintln!("filters initialized ok.");
Ok(FilterChainVulkan { Ok(FilterChainVulkan {
common: FilterCommon { luts, common: FilterCommon {
luts,
samplers, samplers,
config: FilterMutable { config: FilterMutable {
passes_enabled: preset.shader_count as usize, passes_enabled: preset.shader_count as usize,
@ -213,8 +217,7 @@ impl FilterChainVulkan {
Ok::<_, Box<dyn Error>>((shader, source, reflect)) Ok::<_, Box<dyn Error>>((shader, source, reflect))
}) })
.into_iter() .into_iter()
.collect::<error::Result<Vec<(ShaderPassConfig, ShaderSource, CompilerBackend<_>)>>>( .collect::<error::Result<Vec<(ShaderPassConfig, ShaderSource, CompilerBackend<_>)>>>()?;
)?;
for details in &passes { for details in &passes {
librashader_runtime::semantics::insert_pass_semantics( librashader_runtime::semantics::insert_pass_semantics(
@ -355,4 +358,20 @@ impl FilterChainVulkan {
} }
Ok(luts) 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];
}
} }

View file

@ -9,6 +9,7 @@ use rustc_hash::FxHashMap;
use librashader_common::Size; use librashader_common::Size;
use librashader_reflect::reflect::ShaderReflection; use librashader_reflect::reflect::ShaderReflection;
use crate::filter_chain::FilterCommon; use crate::filter_chain::FilterCommon;
use crate::rendertarget::RenderTarget;
use crate::texture::Texture; use crate::texture::Texture;
use crate::samplers::{SamplerSet, VulkanSampler}; 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( fn build_semantics(
&mut self, &mut self,
pass_index: usize, pass_index: usize,
@ -61,7 +86,7 @@ impl FilterPass {
frame_direction: i32, frame_direction: i32,
fb_size: Size<u32>, fb_size: Size<u32>,
viewport_size: Size<u32>, viewport_size: Size<u32>,
descriptor_set: vk::DescriptorSet, descriptor_set: &vk::DescriptorSet,
original: &Texture, original: &Texture,
source: &Texture, source: &Texture,
) { ) {

View file

@ -1,5 +1,6 @@
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
#![feature(let_chains)] #![feature(let_chains)]
#![feature(strict_provenance)]
mod draw_quad; mod draw_quad;
mod error; mod error;
@ -14,6 +15,7 @@ mod vulkan_primitives;
mod vulkan_state; mod vulkan_state;
mod samplers; mod samplers;
mod texture; mod texture;
mod rendertarget;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

View file

@ -94,7 +94,7 @@ impl LutTexture {
)?; )?;
unsafe { unsafe {
let mut handle = staging.map()?; let mut handle = staging.map()?;
handle.copy_from(&image.bytes) handle.copy_from(0, &image.bytes)
} }
unsafe { unsafe {

View file

@ -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
}

View file

@ -10,10 +10,8 @@ use crate::vulkan_primitives::VulkanImageMemory;
pub struct OwnedTexture { pub struct OwnedTexture {
pub device: ash::Device, pub device: ash::Device,
pub image_view: vk::ImageView, pub image_view: vk::ImageView,
pub image: vk::Image, pub image: VulkanImage,
pub format: vk::Format,
pub memory: VulkanImageMemory, pub memory: VulkanImageMemory,
pub size: Size<u32>,
pub max_miplevels: u32, pub max_miplevels: u32,
} }
@ -85,9 +83,11 @@ impl OwnedTexture {
Ok(OwnedTexture { Ok(OwnedTexture {
device: vulkan.device.clone(), device: vulkan.device.clone(),
image_view, image_view,
image, image: VulkanImage {
size, image,
format: format.into(), size,
format: format.into()
},
memory, memory,
max_miplevels, max_miplevels,
}) })
@ -130,17 +130,21 @@ impl Drop for OwnedTexture {
self.device.destroy_image_view(self.image_view, None); self.device.destroy_image_view(self.image_view, None);
} }
if self.image != vk::Image::null() { 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<u32>, pub size: Size<u32>,
pub image_view: vk::ImageView,
pub image: vk::Image, pub image: vk::Image,
pub format: vk::Format, pub format: vk::Format,
}
pub struct Texture {
pub image: VulkanImage,
pub image_view: vk::ImageView,
pub wrap_mode: WrapMode, pub wrap_mode: WrapMode,
pub filter_mode: FilterMode, pub filter_mode: FilterMode,
pub mip_filter: FilterMode, pub mip_filter: FilterMode,

View file

@ -123,8 +123,9 @@ impl Drop for VulkanBuffer {
} }
impl<'a> VulkanBufferMapHandle<'a> { impl<'a> VulkanBufferMapHandle<'a> {
pub unsafe fn copy_from(&mut self, src: &[u8]) { pub unsafe fn copy_from(&mut self, offset: usize, src: &[u8]) {
std::ptr::copy_nonoverlapping(src.as_ptr(), self.ptr.cast(), src.len()); std::ptr::copy_nonoverlapping(src.as_ptr(),
self.ptr.map_addr(|original| original.wrapping_add(offset)).cast(), src.len());
} }
} }