From 982e2ad5179b6d68196ac9fdfcb502afe7f03de4 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sun, 28 Jul 2019 00:29:52 -0700 Subject: [PATCH] initial impl of get image set --- resources/shaders/simple-edge.compute | 2 +- resources/shaders/simple_texture.fragment | 2 - src/vkprocessor.rs | 46 +++++++++++++++++------ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/resources/shaders/simple-edge.compute b/resources/shaders/simple-edge.compute index 47ddc43d..add83ad2 100644 --- a/resources/shaders/simple-edge.compute +++ b/resources/shaders/simple-edge.compute @@ -52,7 +52,7 @@ void main() { ivec3 m = max(max(max(d0, d1), d2), d3); - if ((m.x + m.y + m.z) > 975){ + if ((m.x + m.y + m.z) > 200){ p.x = 0; p.y = 0; p.z = 255; diff --git a/resources/shaders/simple_texture.fragment b/resources/shaders/simple_texture.fragment index 69b562ef..63fa5753 100644 --- a/resources/shaders/simple_texture.fragment +++ b/resources/shaders/simple_texture.fragment @@ -8,8 +8,6 @@ void main() { ivec2 size = ivec2(gl_FragCoord.x, gl_FragCoord.y); f_color = imageLoad(img, size) / (255.0); - // f_color.a += 100; - // f_color = texture(tex, tex_coords); float gamma = 0.5; f_color.rgb = pow(f_color.rgb, vec3(1.0/gamma)); diff --git a/src/vkprocessor.rs b/src/vkprocessor.rs index 3f05f507..46b4fef6 100644 --- a/src/vkprocessor.rs +++ b/src/vkprocessor.rs @@ -1,6 +1,6 @@ use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer, DeviceLocalBuffer, ImmutableBuffer, BufferAccess}; use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState}; -use vulkano::descriptor::descriptor_set::{PersistentDescriptorSet, StdDescriptorPoolAlloc}; +use vulkano::descriptor::descriptor_set::{PersistentDescriptorSet, StdDescriptorPoolAlloc, PersistentDescriptorSetBuilder}; use vulkano::device::{Device, DeviceExtensions, QueuesIter, Queue}; use vulkano::instance::{Instance, InstanceExtensions, PhysicalDevice, QueueFamily}; use vulkano::pipeline::{ComputePipeline, GraphicsPipeline, GraphicsPipelineAbstract}; @@ -26,7 +26,7 @@ use vulkano::image::swapchain::SwapchainImage; use winit::{EventsLoop, WindowBuilder, Window, Event, WindowEvent}; use vulkano_win::VkSurfaceBuild; use vulkano::pipeline::vertex::{SingleBufferDefinition, Vertex}; -use vulkano::descriptor::PipelineLayoutAbstract; +use vulkano::descriptor::{PipelineLayoutAbstract, DescriptorSet}; use std::alloc::Layout; use vulkano::pipeline::viewport::Viewport; use image::ImageFormat; @@ -277,7 +277,6 @@ impl<'a> VkProcessor<'a> { self.settings_buffer = Some(settings_buffer); - // We now create a buffer that will store the shape of our triangle. let vertex_buffer = { vulkano::impl_vertex!(tVertex, position); @@ -301,7 +300,7 @@ impl<'a> VkProcessor<'a> { ).unwrap() }; - let attachment_image = { + let compute_transfer_image = { let mut usage = ImageUsage::none(); usage.transfer_destination = true; @@ -318,19 +317,41 @@ impl<'a> VkProcessor<'a> { MipmapMode::Nearest, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap(); - self.img_set = Some(Arc::new(PersistentDescriptorSet::start(self.graphics_pipeline.clone().unwrap().clone(), 0) + // img_set is only used in the run()'s command buffer construction + // So, adding a get image set function, I can just build lazily + + self.img_set = Some(Arc::new(PersistentDescriptorSet::start( + self.shader_kernels.clone().unwrap().graphics_pipeline.clone().unwrap().clone(), 0) .add_sampled_image(texture.clone(), sampler.clone()).unwrap() - .add_image(attachment_image.clone().unwrap().clone()).unwrap() + .add_image(compute_transfer_image.clone().unwrap().clone()).unwrap() .build().unwrap())); self.graphics_image_buffer = Some(texture.clone()); - self.graphics_image_swap_buffer = Some(attachment_image.clone().unwrap()); + self.graphics_image_swap_buffer = Some(compute_transfer_image.clone().unwrap()); + } + + // The image set is the containing object for all texture and image hooks. + fn get_image_set(&mut self) -> Box { + + let sampler = Sampler::new(self.device.clone(), Filter::Linear, Filter::Linear, + MipmapMode::Nearest, SamplerAddressMode::Repeat, SamplerAddressMode::Repeat, + SamplerAddressMode::Repeat, 0.0, 1.0, 0.0, 0.0).unwrap(); + + let o : Box = Box::new( + PersistentDescriptorSet::start( + self.shader_kernels.clone().unwrap().graphics_pipeline.clone().unwrap().clone(), 0 + ) + .add_sampled_image(self.graphics_image_buffer.clone().unwrap().clone(), sampler.clone()).unwrap() + .add_image(self.graphics_image_swap_buffer.clone().unwrap().clone()).unwrap() + .build().unwrap()); + + o } pub fn run(&mut self, surface: &'a Arc>, mut frame_future: Box) -> Box { let mut framebuffers = window_size_dependent_setup(&self.shader_kernels.clone().unwrap().swapchain_images.clone(), - self.render_pass.clone().unwrap().clone(), + self.shader_kernels.clone().unwrap().render_pass.clone(), &mut self.dynamic_state); let mut recreate_swapchain = false; @@ -341,7 +362,7 @@ impl<'a> VkProcessor<'a> { // Whenever the window resizes we need to recreate everything dependent on the window size. // In this example that includes the swapchain, the framebuffers and the dynamic state viewport. if recreate_swapchain { - //self.shader_kernels.unwrap().recreate_swapchain(surface); + self.shader_kernels = Some(self.shader_kernels.clone().unwrap().recreate_swapchain(surface)); framebuffers = window_size_dependent_setup(&self.shader_kernels.clone().unwrap().swapchain_images.clone(), self.render_pass.clone().unwrap().clone(), &mut self.dynamic_state); @@ -391,9 +412,10 @@ impl<'a> VkProcessor<'a> { .begin_render_pass(framebuffers[image_num].clone(), false, clear_values) .unwrap() - .draw(self.graphics_pipeline.clone().unwrap().clone(), - &self.dynamic_state, v, - self.img_set.clone().unwrap().clone(), ()) + .draw(self.shader_kernels.clone().unwrap().graphics_pipeline.clone().unwrap().clone(), + &self.dynamic_state.clone(), v, + //self.img_set.clone().unwrap().clone(), ()) + self.get_image_set(), ()) .unwrap() .end_render_pass()