diff --git a/resources/shaders/simple-edge.compute b/resources/shaders/simple-edge.compute index fd90041e..e2c7d2d8 100644 --- a/resources/shaders/simple-edge.compute +++ b/resources/shaders/simple-edge.compute @@ -33,37 +33,45 @@ uint get_idx(int offset_x, int offset_y){ void main() { - uint idx = get_idx(0,0); + uint idx = get_idx(0,0); - ivec4 p = separate(read_buffer.buf[get_idx(0 , 0)]); - ivec4 p0 = separate(read_buffer.buf[get_idx(0 , 1)]); - ivec4 p1 = separate(read_buffer.buf[get_idx(0 ,-1)]); - ivec4 p2 = separate(read_buffer.buf[get_idx(1 , 1)]); - ivec4 p3 = separate(read_buffer.buf[get_idx(-1,-1)]); - ivec4 p4 = separate(read_buffer.buf[get_idx(1 , 0)]); - ivec4 p5 = separate(read_buffer.buf[get_idx(-1, 0)]); - ivec4 p6 = separate(read_buffer.buf[get_idx(1 ,-1)]); - ivec4 p7 = separate(read_buffer.buf[get_idx(-1, 1)]); + ivec4 p = separate(read_buffer.buf[get_idx(0 , 0)]); +// ivec4 p0 = separate(read_buffer.buf[get_idx(0 , 1)]); +// ivec4 p1 = separate(read_buffer.buf[get_idx(0 ,-1)]); +// ivec4 p2 = separate(read_buffer.buf[get_idx(1 , 1)]); +// ivec4 p3 = separate(read_buffer.buf[get_idx(-1,-1)]); +// ivec4 p4 = separate(read_buffer.buf[get_idx(1 , 0)]); +// ivec4 p5 = separate(read_buffer.buf[get_idx(-1, 0)]); +// ivec4 p6 = separate(read_buffer.buf[get_idx(1 ,-1)]); +// ivec4 p7 = separate(read_buffer.buf[get_idx(-1, 1)]); - ivec3 d0 = abs(p0.xyz - p1.xyz); - ivec3 d1 = abs(p2.xyz - p3.xyz); - ivec3 d2 = abs(p4.xyz - p5.xyz); - ivec3 d3 = abs(p6.xyz - p7.xyz); +// ivec3 d0 = abs(p0.xyz - p1.xyz); +// ivec3 d1 = abs(p2.xyz - p3.xyz); +// ivec3 d2 = abs(p4.xyz - p5.xyz); +// ivec3 d3 = abs(p6.xyz - p7.xyz); - ivec3 m = max(max(max(d0, d1), d2), d3); +// ivec3 m = max(max(max(d0, d1), d2), d3); - if ((m.x + m.y + m.z) > 275){ - p.x = 0; - p.y = 0; - p.z = 255; - } +// if ((m.x + m.y + m.z) > 275){ +// p.x = 0; +// p.y = 0; +// p.z = 255; +// } - //p.z = max(p.z - (d0.x + d0.y + d0.z + d1.x + d1.y + d1.z)/5, 0); +// //p.z = max(p.z - (d0.x + d0.y + d0.z + d1.x + d1.y + d1.z)/5, 0); + +// write_buffer.buf[idx] = (write_buffer.buf[idx] & (~0x000000FF) ) | (p.x); +// write_buffer.buf[idx] = (write_buffer.buf[idx] & (~0x0000FF00) ) | (p.y << 8); +// write_buffer.buf[idx] = (write_buffer.buf[idx] & (~0x00FF0000) ) | (p.z << 16); +// write_buffer.buf[idx] = (write_buffer.buf[idx] & (~0xFF000000) ) | (p.w << 24); + +// p.x = 70; + +// write_buffer.buf[idx] = (write_buffer.buf[idx] & (~0x000000FF) ) | (p.x); +// write_buffer.buf[idx] = (write_buffer.buf[idx] & (~0x0000FF00) ) | (p.y << 8); +// write_buffer.buf[idx] = (write_buffer.buf[idx] & (~0x00FF0000) ) | (p.z << 16); +// write_buffer.buf[idx] = (write_buffer.buf[idx] & (~0xFF000000) ) | (p.w << 24); - write_buffer.buf[idx] = (write_buffer.buf[idx] & (~0x000000FF) ) | (p.x); - write_buffer.buf[idx] = (write_buffer.buf[idx] & (~0x0000FF00) ) | (p.y << 8); - write_buffer.buf[idx] = (write_buffer.buf[idx] & (~0x00FF0000) ) | (p.z << 16); - write_buffer.buf[idx] = (write_buffer.buf[idx] & (~0xFF000000) ) | (p.w << 24); // read_buffer.buf[idx] = (read_buffer.buf[idx] & (~0x000000FF) ) | (p.x); // read_buffer.buf[idx] = (read_buffer.buf[idx] & (~0x0000FF00) ) | (p.y << 8); diff --git a/resources/shaders/simple_texture.fragment b/resources/shaders/simple_texture.fragment index 3a91974e..95dbbb13 100644 --- a/resources/shaders/simple_texture.fragment +++ b/resources/shaders/simple_texture.fragment @@ -4,5 +4,16 @@ layout(location = 0) out vec4 f_color; layout(set = 0, binding = 0) uniform sampler2D tex; layout(set = 0, binding = 1, rgba8ui) readonly uniform uimage2D img; void main() { - f_color = texture(tex, tex_coords); + + vec2 onePixel = vec2(1.0, 1.0) / (720.0, 756.0); + vec2 pos = tex_coords + onePixel * vec2(0, 0); + ivec2 ipos = ivec2(pos); + vec4 colorSum = imageLoad(img, ipos); + f_color = colorSum; + + + // f_color = texture(tex, tex_coords); + // ivec2 t = ivec2(tex_coords.x, tex_coords.y ); + + } \ No newline at end of file diff --git a/src/vkprocessor.rs b/src/vkprocessor.rs index a984e66d..8bbbfbe1 100644 --- a/src/vkprocessor.rs +++ b/src/vkprocessor.rs @@ -262,9 +262,16 @@ impl<'a> VkProcessor<'a> { options.add_macro_definition("SETTING_BUCKETS_START", Some("2")); options.add_macro_definition("SETTING_BUCKETS_LEN", Some("2")); - let shader = - sr::load(vertex_shader_path, fragment_shader_path) - .expect("Failed to compile"); + let shader = sr::load(vertex_shader_path, fragment_shader_path).expect(""); +// let shader = match sr::load(vertex_shader_path, fragment_shader_path) { +// Ok(t) => t, +// Err(e) => { +// +// panic!(e); +// } +// }; + + let vulkano_entry = sr::parse(&shader) @@ -327,42 +334,6 @@ impl<'a> VkProcessor<'a> { self.render_pass = Some(render_pass); - let (texture, tex_future) = { - let image = image::load_from_memory_with_format(include_bytes!("../resources/images/funky-bird.jpg"), - ImageFormat::JPEG).unwrap().to_rgba(); - let dimensions = image.dimensions(); - let image_data = image.into_raw().clone(); - - ImmutableImage::from_iter( - image_data.iter().cloned(), - Dimensions::Dim2d { width: dimensions.0, height: dimensions.1 }, - Format::R8G8B8A8Srgb, - self.queue.clone() - ).unwrap() - }; - - let attachment_image = { - let image = image::load_from_memory_with_format(include_bytes!("../resources/images/funky-bird.jpg"), - ImageFormat::JPEG).unwrap().to_rgba(); - let dimensions = image.dimensions(); - let image_data = image.into_raw().clone(); - - let mut usage = ImageUsage::none(); - usage.transfer_destination = true; - usage.storage = true; - - AttachmentImage::with_usage( - self.device.clone(), - [dimensions.0, dimensions.1], - Format::R8G8B8A8Uint, - usage) - }; - - 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(); - - // Before we draw we have to create what is called a pipeline. This is similar to an OpenGL // program, but much more specific. let pipeline = GraphicsPipeline::start() @@ -396,16 +367,8 @@ impl<'a> VkProcessor<'a> { .build(self.device.clone()) .unwrap(); - self.graphics_pipeline = Some(Arc::new(pipeline)); - self.img_set = Some(Arc::new(PersistentDescriptorSet::start(self.graphics_pipeline.clone().unwrap().clone(), 0) - .add_sampled_image(texture.clone(), sampler.clone()).unwrap() - .add_image(attachment_image.clone().unwrap().clone()).unwrap() - .build().unwrap())); - - self.graphics_image_buffer = Some(texture.clone()); - self.graphics_iamge_swap_buffer = Some(attachment_image.clone().unwrap()); } @@ -430,102 +393,6 @@ impl<'a> VkProcessor<'a> { self.images = Some(new_images); } - - pub fn run(&mut self, surface: &'a Arc>, mut frame_future: Box) -> Box { - - let mut framebuffers = window_size_dependent_setup(&self.images.clone().unwrap().clone(), - self.render_pass.clone().unwrap().clone(), - &mut self.dynamic_state); - - let mut recreate_swapchain = false; - - // The docs said to call this on each loop. - frame_future.cleanup_finished(); - - // 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.recreate_swapchain(surface); - framebuffers = window_size_dependent_setup(&self.images.clone().unwrap().clone(), - self.render_pass.clone().unwrap().clone(), - &mut self.dynamic_state); - recreate_swapchain = false; - } - - - // This function can block if no image is available. The parameter is an optional timeout - // after which the function call will return an error. - let (image_num, acquire_future) = match vulkano::swapchain::acquire_next_image(self.swapchain.clone().unwrap().clone(), None) { - Ok(r) => r, - Err(AcquireError::OutOfDate) => { - recreate_swapchain = true; - //continue; - panic!("Weird thing"); - } - Err(err) => panic!("{:?}", err) - }; - - // Specify the color to clear the framebuffer with i.e. blue - let clear_values = vec!([0.0, 0.0, 1.0, 1.0].into()); - - - { - // In order to draw, we have to build a *command buffer*. The command buffer object holds - // the list of commands that are going to be executed. - // - // Building a command buffer is an expensive operation (usually a few hundred - // microseconds), but it is known to be a hot path in the driver and is expected to be - // optimized. - // - // Note that we have to pass a queue family when we create the command buffer. The command - // buffer will only be executable on that given queue family. - let mut v = Vec::new(); - v.push(self.vertex_buffer.clone().unwrap().clone()); - - let command_buffer = - AutoCommandBufferBuilder::primary_one_time_submit(self.device.clone(), self.queue.family()) - .unwrap() - - .dispatch([self.xy.0, self.xy.1, 1], - self.compute_pipeline.clone().unwrap().clone(), - self.compute_set.clone().unwrap().clone(), ()).unwrap() - - //.copy_buffer_to_image(self.img_buffers.get(0).unwrap().clone(), self.graphics_image_buffer.clone().unwrap()).unwrap() - .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(), ()) - .unwrap() - - .end_render_pass() - .unwrap() - - .build().unwrap(); - - // Wait on the previous frame, then execute the command buffer and present the image - let future = frame_future.join(acquire_future) - .then_execute(self.queue.clone(), command_buffer).unwrap() - .then_swapchain_present(self.queue.clone(), self.swapchain.clone().unwrap().clone(), image_num) - .then_signal_fence_and_flush(); - - match future { - Ok(future) => { - (Box::new(future) as Box<_>) - } - Err(FlushError::OutOfDate) => { - recreate_swapchain = true; - (Box::new(sync::now(self.device.clone())) as Box<_>) - } - Err(e) => { - println!("{:?}", e); - (Box::new(sync::now(self.device.clone())) as Box<_>) - } - } - } - } - pub fn load_buffers(&mut self, image_filename: String) { let project_root = @@ -618,6 +485,163 @@ impl<'a> VkProcessor<'a> { }; self.vertex_buffer = Some(vertex_buffer); + + let (texture, tex_future) = { + let image = image::load_from_memory_with_format(include_bytes!("../resources/images/funky-bird.jpg"), + ImageFormat::JPEG).unwrap().to_rgba(); + + println!("{}", image.len()); + println!("{}", self.image_buffer.len()); + + let dimensions = image.dimensions(); + let image_data = image.into_raw().clone(); + + ImmutableImage::from_iter( + image_data.iter().cloned(), + Dimensions::Dim2d { width: dimensions.0, height: dimensions.1 }, + Format::R8G8B8A8Srgb, + self.queue.clone() +// self.image_buffer.iter().cloned(), +// Format::R8G8B8A8Uint, + + ).unwrap() + }; + + let attachment_image = { + let image = image::load_from_memory_with_format(include_bytes!("../resources/images/funky-bird.jpg"), + ImageFormat::JPEG).unwrap().to_rgba(); + let dimensions = image.dimensions(); + let image_data = image.into_raw().clone(); + + let mut usage = ImageUsage::none(); + usage.transfer_destination = true; + usage.storage = true; + + AttachmentImage::with_usage( + self.device.clone(), + [dimensions.0, dimensions.1], + Format::R8G8B8A8Uint, + usage) + }; + + 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(); + + + self.img_set = Some(Arc::new(PersistentDescriptorSet::start(self.graphics_pipeline.clone().unwrap().clone(), 0) + .add_sampled_image(texture.clone(), sampler.clone()).unwrap() + .add_image(attachment_image.clone().unwrap().clone()).unwrap() + .build().unwrap())); + + self.graphics_image_buffer = Some(texture.clone()); + self.graphics_iamge_swap_buffer = Some(attachment_image.clone().unwrap()); + } + + pub fn run(&mut self, surface: &'a Arc>, mut frame_future: Box) -> Box { + + let mut framebuffers = window_size_dependent_setup(&self.images.clone().unwrap().clone(), + self.render_pass.clone().unwrap().clone(), + &mut self.dynamic_state); + + let mut recreate_swapchain = false; + + // The docs said to call this on each loop. + frame_future.cleanup_finished(); + + // 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.recreate_swapchain(surface); + framebuffers = window_size_dependent_setup(&self.images.clone().unwrap().clone(), + self.render_pass.clone().unwrap().clone(), + &mut self.dynamic_state); + recreate_swapchain = false; + } + + + // This function can block if no image is available. The parameter is an optional timeout + // after which the function call will return an error. + let (image_num, acquire_future) = match vulkano::swapchain::acquire_next_image(self.swapchain.clone().unwrap().clone(), None) { + Ok(r) => r, + Err(AcquireError::OutOfDate) => { + recreate_swapchain = true; + //continue; + panic!("Weird thing"); + } + Err(err) => panic!("{:?}", err) + }; + + // Specify the color to clear the framebuffer with i.e. blue + let clear_values = vec!([0.0, 0.0, 1.0, 1.0].into()); + + + { + // In order to draw, we have to build a *command buffer*. The command buffer object holds + // the list of commands that are going to be executed. + // + // Building a command buffer is an expensive operation (usually a few hundred + // microseconds), but it is known to be a hot path in the driver and is expected to be + // optimized. + // + // Note that we have to pass a queue family when we create the command buffer. The command + // buffer will only be executable on that given queue family. + let mut v = Vec::new(); + v.push(self.vertex_buffer.clone().unwrap().clone()); + + let command_buffer = + AutoCommandBufferBuilder::primary_one_time_submit(self.device.clone(), self.queue.family()) + .unwrap() + + .dispatch([self.xy.0, self.xy.1, 1], + self.compute_pipeline.clone().unwrap().clone(), + self.compute_set.clone().unwrap().clone(), ()).unwrap() + + .copy_buffer_to_image(self.img_buffers.get(0).unwrap().clone(), + self.graphics_iamge_swap_buffer.clone().unwrap()).unwrap() + .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(), ()) + .unwrap() + + .end_render_pass() + .unwrap() + + .build().unwrap(); + + let mut data_buffer_content = self.img_buffers.get(0).unwrap().read().unwrap(); + let img = ImageBuffer::from_fn(self.xy.0, self.xy.1, |x, y| { + let r = data_buffer_content[((self.xy.0 * y + x) * 4 + 0) as usize] as u8; + let g = data_buffer_content[((self.xy.0 * y + x) * 4 + 1) as usize] as u8; + let b = data_buffer_content[((self.xy.0 * y + x) * 4 + 2) as usize] as u8; + let a = data_buffer_content[((self.xy.0 * y + x) * 4 + 3) as usize] as u8; + + image::Rgba([r, g, b, a]) + }); + + // Wait on the previous frame, then execute the command buffer and present the image + let future = frame_future.join(acquire_future) + .then_execute(self.queue.clone(), command_buffer).unwrap() + .then_swapchain_present(self.queue.clone(), self.swapchain.clone().unwrap().clone(), image_num) + .then_signal_fence_and_flush(); + + match future { + Ok(future) => { + (Box::new(future) as Box<_>) + } + Err(FlushError::OutOfDate) => { + recreate_swapchain = true; + (Box::new(sync::now(self.device.clone())) as Box<_>) + } + Err(e) => { + println!("{:?}", e); + (Box::new(sync::now(self.device.clone())) as Box<_>) + } + } + } } // pub fn read_image(&self) -> Vec { @@ -683,3 +707,4 @@ impl<'a> VkProcessor<'a> { +