vk: implement vbo
This commit is contained in:
parent
2038da787e
commit
9dbe031ce8
|
@ -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<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])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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, Error = Box<dyn Error>>,
|
||||
vulkan: impl TryInto<Vulkan, Error=Box<dyn Error>>,
|
||||
path: impl AsRef<Path>,
|
||||
options: Option<&FilterChainOptionsVulkan>,
|
||||
) -> error::Result<FilterChainVulkan> {
|
||||
|
@ -155,7 +156,7 @@ impl FilterChainVulkan {
|
|||
}
|
||||
|
||||
pub fn load_from_preset(
|
||||
vulkan: impl TryInto<Vulkan, Error = Box<dyn Error>>,
|
||||
vulkan: impl TryInto<Vulkan, Error=Box<dyn Error>>,
|
||||
preset: ShaderPreset,
|
||||
options: Option<&FilterChainOptionsVulkan>,
|
||||
) -> error::Result<FilterChainVulkan> {
|
||||
|
@ -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<dyn Error>>((shader, source, reflect))
|
||||
})
|
||||
.into_iter()
|
||||
.collect::<error::Result<Vec<(ShaderPassConfig, ShaderSource, CompilerBackend<_>)>>>(
|
||||
)?;
|
||||
.collect::<error::Result<Vec<(ShaderPassConfig, ShaderSource, CompilerBackend<_>)>>>()?;
|
||||
|
||||
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];
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<u32>,
|
||||
viewport_size: Size<u32>,
|
||||
descriptor_set: vk::DescriptorSet,
|
||||
descriptor_set: &vk::DescriptorSet,
|
||||
original: &Texture,
|
||||
source: &Texture,
|
||||
) {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -94,7 +94,7 @@ impl LutTexture {
|
|||
)?;
|
||||
unsafe {
|
||||
let mut handle = staging.map()?;
|
||||
handle.copy_from(&image.bytes)
|
||||
handle.copy_from(0, &image.bytes)
|
||||
}
|
||||
|
||||
unsafe {
|
||||
|
|
8
librashader-runtime-vk/src/rendertarget.rs
Normal file
8
librashader-runtime-vk/src/rendertarget.rs
Normal 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
|
||||
}
|
|
@ -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<u32>,
|
||||
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<u32>,
|
||||
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,
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue