getting close, something wrong with copying the buffer. Probably the format that I've selected

This commit is contained in:
2019-07-17 21:53:22 -07:00
parent 26410b78a2
commit 5928eb5dde
3 changed files with 213 additions and 169 deletions

View File

@@ -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<Surface<Window>>, mut frame_future: Box<dyn GpuFuture>) -> Box<dyn GpuFuture> {
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<Surface<Window>>, mut frame_future: Box<dyn GpuFuture>) -> Box<dyn GpuFuture> {
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<u8> {
@@ -683,3 +707,4 @@ impl<'a> VkProcessor<'a> {