vk: work on hooking

This commit is contained in:
chyyran 2022-12-29 00:50:48 -05:00
parent 88db9220c4
commit 3d57be2754
11 changed files with 128 additions and 32 deletions

View file

@ -122,6 +122,7 @@ impl TryFrom<(ash::Device, vk::Queue, vk::PhysicalDeviceMemoryProperties)> for V
pub struct FilterChainVulkan { pub struct FilterChainVulkan {
pub(crate) common: FilterCommon, pub(crate) common: FilterCommon,
pub(crate) passes: Box<[FilterPass]>, pub(crate) passes: Box<[FilterPass]>,
pub(crate) vulkan: Vulkan,
// pub(crate) output_framebuffers: Box<[OwnedFramebuffer]>, // pub(crate) output_framebuffers: Box<[OwnedFramebuffer]>,
// pub(crate) feedback_framebuffers: Box<[OwnedFramebuffer]>, // pub(crate) feedback_framebuffers: Box<[OwnedFramebuffer]>,
// pub(crate) history_framebuffers: VecDeque<OwnedFramebuffer>, // pub(crate) history_framebuffers: VecDeque<OwnedFramebuffer>,
@ -186,6 +187,7 @@ impl FilterChainVulkan {
}, },
}, },
passes: filters, passes: filters,
vulkan: device
}) })
} }
@ -371,10 +373,27 @@ impl FilterChainVulkan {
viewport: &vk::Viewport, viewport: &vk::Viewport,
input: &VulkanImage, input: &VulkanImage,
options: Option<()>, options: Option<()>,
wait: vk::Semaphore,
signal: vk::Semaphore,
) -> error::Result<()> { ) -> error::Result<()> {
// limit number of passes to those enabled. // limit number of passes to those enabled.
let passes = &mut self.passes[0..self.common.config.passes_enabled]; let passes = &mut self.passes[0..self.common.config.passes_enabled];
//
// for (index, pass) in passes.iter_mut().enumerate() {
// pass.draw(index, &self.common, count as u32, 0, viewport, &Default::default(), &Texture {}, &Texture {})
// }
unsafe {
self.vulkan.device.queue_submit(self.vulkan.queue, &[vk::SubmitInfo::builder()
.wait_semaphores(&[wait])
.wait_dst_stage_mask(&[vk::PipelineStageFlags::ALL_COMMANDS],)
.signal_semaphores(&[signal])
.command_buffers(&[])
.build()], vk::Fence::null())?
}
Ok(())
} }
} }

View file

@ -8,6 +8,7 @@ use librashader_runtime::uniforms::UniformStorage;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use librashader_common::Size; use librashader_common::Size;
use librashader_reflect::reflect::ShaderReflection; use librashader_reflect::reflect::ShaderReflection;
use crate::error;
use crate::filter_chain::FilterCommon; use crate::filter_chain::FilterCommon;
use crate::rendertarget::RenderTarget; use crate::rendertarget::RenderTarget;
use crate::texture::Texture; use crate::texture::Texture;
@ -61,24 +62,30 @@ impl FilterPass {
parent: &FilterCommon, parent: &FilterCommon,
frame_count: u32, frame_count: u32,
frame_direction: i32, frame_direction: i32,
descriptor: &vk::DescriptorSet,
viewport: &vk::Viewport, viewport: &vk::Viewport,
original: &Texture, original: &Texture,
source: &Texture, source: &Texture,
output: &RenderTarget, output: &RenderTarget,
) { ) -> error::Result<()> {
let descriptor = *&self.graphics_pipeline.layout.descriptor_sets[0];
self.build_semantics(pass_index, parent, &output.mvp, frame_count, frame_direction, Size::new(100, 100), self.build_semantics(pass_index, parent, &output.mvp, frame_count, frame_direction, Size::new(100, 100),
Size::new(100,100),descriptor, original, source); Size::new(100,100),&descriptor, original, source);
if let Some(ubo) = &self.reflection.ubo { if let Some(ubo) = &self.reflection.ubo {
// shader_vulkan: 2554 (ra uses uses one big buffer) // shader_vulkan: 2554 (ra uses uses one big buffer)
// itll be simpler for us if we just use a RingBuffer<vk::Buffer> tbh. // itll be simpler for us if we just use a RingBuffer<vk::Buffer> tbh.
self.ubo_ring.bind_for_frame(descriptor, ubo.binding, &self.uniform_storage)?;
} }
Ok(())
} }
fn bind_ubo(device: &vk::Device, descriptor: &vk::DescriptorSet, binding: u32, buffer: &vk::Buffer, offset: vk::DeviceSize, range: vk::DeviceSize) { //
// 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,
@ -137,7 +144,7 @@ impl FilterPass {
FilterPass::bind_texture( FilterPass::bind_texture(
&self.device, &self.device,
&parent.samplers, &parent.samplers,
descriptor_set, *descriptor_set,
binding, binding,
original, original,
); );
@ -149,7 +156,7 @@ impl FilterPass {
.get(&TextureSemantics::Original.semantics(0).into()) .get(&TextureSemantics::Original.semantics(0).into())
{ {
self.uniform_storage self.uniform_storage
.bind_vec4(*offset, original.size, None); .bind_vec4(*offset, original.image.size, None);
} }
// bind Source sampler // bind Source sampler
@ -163,7 +170,7 @@ impl FilterPass {
FilterPass::bind_texture( FilterPass::bind_texture(
&self.device, &self.device,
&parent.samplers, &parent.samplers,
descriptor_set, *descriptor_set,
binding, binding,
source source
); );
@ -175,7 +182,7 @@ impl FilterPass {
.get(&TextureSemantics::Source.semantics(0).into()) .get(&TextureSemantics::Source.semantics(0).into())
{ {
self.uniform_storage self.uniform_storage
.bind_vec4(*offset, source.size, None); .bind_vec4(*offset, source.image.size, None);
} }
if let Some(binding) = self if let Some(binding) = self
@ -187,7 +194,7 @@ impl FilterPass {
FilterPass::bind_texture( FilterPass::bind_texture(
&self.device, &self.device,
&parent.samplers, &parent.samplers,
descriptor_set, *descriptor_set,
binding, binding,
original, original,
); );
@ -198,7 +205,7 @@ impl FilterPass {
.get(&TextureSemantics::OriginalHistory.semantics(0).into()) .get(&TextureSemantics::OriginalHistory.semantics(0).into())
{ {
self.uniform_storage self.uniform_storage
.bind_vec4(*offset, original.size, None); .bind_vec4(*offset, original.image.size, None);
} }
// for (index, output) in parent.history_textures.iter().enumerate() { // for (index, output) in parent.history_textures.iter().enumerate() {
@ -327,7 +334,7 @@ impl FilterPass {
FilterPass::bind_texture( FilterPass::bind_texture(
&self.device, &self.device,
&parent.samplers, &parent.samplers,
descriptor_set, *descriptor_set,
binding, binding,
&lut.image, &lut.image,
); );
@ -338,7 +345,7 @@ impl FilterPass {
.get(&TextureSemantics::User.semantics(*index).into()) .get(&TextureSemantics::User.semantics(*index).into())
{ {
self.uniform_storage self.uniform_storage
.bind_vec4(*offset, lut.image.size, None); .bind_vec4(*offset, lut.image.image.size, None);
} }
} }

View file

@ -46,8 +46,8 @@ impl OwnedFramebuffer {
&vk::FramebufferCreateInfo::builder() &vk::FramebufferCreateInfo::builder()
.render_pass(render_pass.handle) .render_pass(render_pass.handle)
.attachments(&[image.image_view]) .attachments(&[image.image_view])
.width(image.size.width) .width(image.image.size.width)
.height(image.size.height) .height(image.image.size.height)
.layers(1) .layers(1)
.build(), .build(),
None, None,

View file

@ -9,6 +9,8 @@ use std::mem::align_of;
mod base; mod base;
pub use base::*; pub use base::*;
use crate::filter_chain::FilterChainVulkan;
use crate::texture::VulkanImage;
#[derive(Clone, Debug, Copy)] #[derive(Clone, Debug, Copy)]
struct Vertex { struct Vertex {
@ -16,7 +18,7 @@ struct Vertex {
color: [f32; 4], color: [f32; 4],
} }
pub(crate) fn main(base: ExampleBase) { pub(crate) fn main(base: ExampleBase, mut filter_chain: FilterChainVulkan) {
unsafe { unsafe {
let renderpass_attachments = [ let renderpass_attachments = [
vk::AttachmentDescription { vk::AttachmentDescription {
@ -429,8 +431,18 @@ pub(crate) fn main(base: ExampleBase) {
device.cmd_end_render_pass(draw_command_buffer); device.cmd_end_render_pass(draw_command_buffer);
}, },
); );
filter_chain.frame(0, &viewports[0], &VulkanImage {
size: Default::default(),
image: Default::default(),
format: Default::default(),
}, None, base.rendering_complete_semaphore, base.postprocess_complete_semaphore)
.unwrap();
//let mut present_info_err = mem::zeroed(); //let mut present_info_err = mem::zeroed();
let wait_semaphors = [base.rendering_complete_semaphore]; let wait_semaphors = [base.postprocess_complete_semaphore];
let swapchains = [base.swapchain]; let swapchains = [base.swapchain];
let image_indices = [present_index]; let image_indices = [present_index];
let present_info = vk::PresentInfoKHR::builder() let present_info = vk::PresentInfoKHR::builder()

View file

@ -181,13 +181,14 @@ pub struct ExampleBase {
pub present_complete_semaphore: vk::Semaphore, pub present_complete_semaphore: vk::Semaphore,
pub rendering_complete_semaphore: vk::Semaphore, pub rendering_complete_semaphore: vk::Semaphore,
pub postprocess_complete_semaphore: vk::Semaphore,
pub draw_commands_reuse_fence: vk::Fence, pub draw_commands_reuse_fence: vk::Fence,
pub setup_commands_reuse_fence: vk::Fence, pub setup_commands_reuse_fence: vk::Fence,
} }
impl ExampleBase { impl ExampleBase {
pub fn render_loop<F: Fn()>(&self, f: F) { pub fn render_loop<F: FnMut()>(&self, mut f: F) {
self.event_loop self.event_loop
.borrow_mut() .borrow_mut()
.run_return(|event, _, control_flow| { .run_return(|event, _, control_flow| {
@ -554,7 +555,9 @@ impl ExampleBase {
let rendering_complete_semaphore = device let rendering_complete_semaphore = device
.create_semaphore(&semaphore_create_info, None) .create_semaphore(&semaphore_create_info, None)
.unwrap(); .unwrap();
let postprocess_complete_semaphore = device
.create_semaphore(&semaphore_create_info, None)
.unwrap();
ExampleBase { ExampleBase {
event_loop: RefCell::new(event_loop), event_loop: RefCell::new(event_loop),
entry, entry,
@ -585,6 +588,7 @@ impl ExampleBase {
debug_call_back, debug_call_back,
debug_utils_loader, debug_utils_loader,
depth_image_memory, depth_image_memory,
postprocess_complete_semaphore
} }
} }
} }
@ -598,6 +602,8 @@ impl Drop for ExampleBase {
.destroy_semaphore(self.present_complete_semaphore, None); .destroy_semaphore(self.present_complete_semaphore, None);
self.device self.device
.destroy_semaphore(self.rendering_complete_semaphore, None); .destroy_semaphore(self.rendering_complete_semaphore, None);
self.device
.destroy_semaphore(self.postprocess_complete_semaphore, None);
self.device self.device
.destroy_fence(self.draw_commands_reuse_fence, None); .destroy_fence(self.draw_commands_reuse_fence, None);
self.device self.device

View file

@ -42,6 +42,6 @@ mod tests {
) )
// FilterChain::load_from_path("../test/slang-shaders/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp", None) // FilterChain::load_from_path("../test/slang-shaders/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp", None)
.unwrap(); .unwrap();
hello_triangle::main(base); hello_triangle::main(base, filter);
} }
} }

View file

@ -8,7 +8,7 @@ use librashader_common::{FilterMode, WrapMode};
use librashader_presets::TextureConfig; use librashader_presets::TextureConfig;
use librashader_runtime::image::{Image, BGRA8}; use librashader_runtime::image::{Image, BGRA8};
use librashader_runtime::scaling::MipmapSize; use librashader_runtime::scaling::MipmapSize;
use crate::texture::Texture; use crate::texture::{Texture, VulkanImage};
pub struct LutTexture { pub struct LutTexture {
pub memory: VulkanImageMemory, pub memory: VulkanImageMemory,
@ -237,13 +237,15 @@ impl LutTexture {
memory, memory,
staging, staging,
image: Texture { image: Texture {
size: image.size,
image_view: texture_view, image_view: texture_view,
image: texture, image: VulkanImage {
size: image.size,
image: texture,
format: vk::Format::B8G8R8A8_UNORM
},
filter_mode: config.filter_mode, filter_mode: config.filter_mode,
wrap_mode: config.wrap_mode, wrap_mode: config.wrap_mode,
mip_filter: config.filter_mode, mip_filter: config.filter_mode,
format: vk::Format::B8G8R8A8_UNORM
} }
}) })
} }

View file

@ -112,8 +112,8 @@ impl OwnedTexture {
let mut view_info = vk::ImageViewCreateInfo::builder() let mut view_info = vk::ImageViewCreateInfo::builder()
.view_type(ImageViewType::TYPE_2D) .view_type(ImageViewType::TYPE_2D)
.format(self.format) .format(self.image.format)
.image(self.image.clone()) .image(self.image.image.clone())
.subresource_range(image_subresource) .subresource_range(image_subresource)
.components(swizzle_components) .components(swizzle_components)
.build(); .build();
@ -129,7 +129,7 @@ impl Drop for OwnedTexture {
if self.image_view != vk::ImageView::null() { if self.image_view != vk::ImageView::null() {
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.image != vk::Image::null() {
self.device.destroy_image(self.image.image, None); self.device.destroy_image(self.image.image, None);
} }
} }

View file

@ -1,10 +1,12 @@
use ash::vk; use ash::vk;
use librashader_runtime::ringbuffer::{BoxRingBuffer, InlineRingBuffer}; use librashader_runtime::ringbuffer::{BoxRingBuffer, InlineRingBuffer, RingBuffer};
use librashader_runtime::uniforms::UniformStorageAccess;
use crate::error; use crate::error;
use crate::vulkan_primitives::VulkanBuffer; use crate::vulkan_primitives::VulkanBuffer;
pub struct VkUboRing { pub struct VkUboRing {
ring: BoxRingBuffer<VulkanBuffer>, ring: BoxRingBuffer<VulkanBuffer>,
device: ash::Device,
} }
impl VkUboRing { impl VkUboRing {
@ -15,7 +17,37 @@ impl VkUboRing {
} }
Ok(VkUboRing { Ok(VkUboRing {
ring: BoxRingBuffer::from_vec(ring) ring: BoxRingBuffer::from_vec(ring),
device: device.clone()
}) })
} }
pub fn bind_for_frame(&mut self, descriptor_set: vk::DescriptorSet, binding: u32, storage: &impl UniformStorageAccess) -> error::Result<()>{
let mut buffer = self.ring.current_mut();
// todo: write directly to allocated buffer.
unsafe {
let mut map = buffer.map()?;
map.copy_from(0, storage.ubo_slice())
}
unsafe {
let buffer_info = vk::DescriptorBufferInfo::builder()
.buffer(buffer.handle)
.offset(0)
.range(storage.ubo_slice().len() as vk::DeviceSize)
.build();
let write_info = vk::WriteDescriptorSet::builder()
.descriptor_type(vk::DescriptorType::UNIFORM_BUFFER)
.dst_set(descriptor_set)
.dst_binding(binding)
.dst_array_element(0)
.buffer_info(&[buffer_info])
.build();
self.device.update_descriptor_sets(&[write_info], &[])
}
self.ring.next();
Ok(())
}
} }

View file

@ -176,8 +176,8 @@ impl Drop for VulkanShaderModule {
} }
pub struct VulkanGraphicsPipeline { pub struct VulkanGraphicsPipeline {
layout: PipelineLayoutObjects, pub layout: PipelineLayoutObjects,
render_pass: VulkanRenderPass, pub render_pass: VulkanRenderPass,
pipeline: vk::Pipeline, pipeline: vk::Pipeline,
} }

View file

@ -23,19 +23,37 @@ pub trait UniformStorageAccess {
/// of the implementing struct. /// of the implementing struct.
fn ubo_pointer(&self) -> *const u8; fn ubo_pointer(&self) -> *const u8;
/// Get a pointer to the backing UBO storage. This pointer must be valid for the lifetime
/// of the implementing struct.
fn ubo_slice(&self) -> &[u8];
/// Get a pointer to the backing Push Constant buffer storage. /// Get a pointer to the backing Push Constant buffer storage.
/// This pointer must be valid for the lifetime of the implementing struct. /// This pointer must be valid for the lifetime of the implementing struct.
fn push_pointer(&self) -> *const u8; fn push_pointer(&self) -> *const u8;
/// Get a slice to the backing Push Constant buffer storage.
/// This pointer must be valid for the lifetime of the implementing struct.
fn push_slice(&self) -> &[u8];
} }
impl<T, H> UniformStorageAccess for UniformStorage<T, H> { impl<T, H> UniformStorageAccess for UniformStorage<T, H> {
fn ubo_pointer(&self) -> *const u8 { fn ubo_pointer(&self) -> *const u8 {
self.ubo.as_ptr() self.ubo.as_ptr()
} }
fn ubo_slice(&self) -> &[u8] {
&self.ubo
}
fn push_pointer(&self) -> *const u8 { fn push_pointer(&self) -> *const u8 {
self.push.as_ptr() self.push.as_ptr()
} }
fn push_slice(&self) -> &[u8] {
&self.push
}
} }
/// A uniform binder that always returns `None`, and does not do any binding of uniforms. /// A uniform binder that always returns `None`, and does not do any binding of uniforms.