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

master
mitchellhansen 6 years ago
parent 26410b78a2
commit 5928eb5dde

@ -36,34 +36,42 @@ void main() {
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)]);
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);
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);
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);
// 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 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;
// }
// //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);
// read_buffer.buf[idx] = (read_buffer.buf[idx] & (~0x000000FF) ) | (p.x);
// read_buffer.buf[idx] = (read_buffer.buf[idx] & (~0x0000FF00) ) | (p.y << 8);

@ -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 );
}

@ -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,6 +393,150 @@ impl<'a> VkProcessor<'a> {
self.images = Some(new_images);
}
pub fn load_buffers(&mut self, image_filename: String)
{
let project_root =
std::env::current_dir()
.expect("failed to get root directory");
let mut compute_path = project_root.clone();
compute_path.push(PathBuf::from("resources/images/"));
compute_path.push(PathBuf::from(image_filename));
let img = image::open(compute_path).expect("Couldn't find image");
self.xy = img.dimensions();
let data_length = self.xy.0 * self.xy.1 * 4;
let pixel_count = img.raw_pixels().len();
println!("Pixel count {}", pixel_count);
if pixel_count != data_length as usize {
println!("Creating apha channel...");
for i in img.raw_pixels().iter() {
if (self.image_buffer.len() + 1) % 4 == 0 {
self.image_buffer.push(255);
}
self.image_buffer.push(*i);
}
self.image_buffer.push(255);
} else {
self.image_buffer = img.raw_pixels();
}
println!("Buffer length {}", self.image_buffer.len());
println!("Size {:?}", self.xy);
println!("Allocating Buffers...");
// Pull out the image data and place it in a buffer for the kernel to write to and for us to read from
let write_buffer = {
let mut buff = self.image_buffer.iter();
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), data_iter).unwrap()
};
// Pull out the image data and place it in a buffer for the kernel to read from
let read_buffer = {
let mut buff = self.image_buffer.iter();
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), data_iter).unwrap()
};
// A buffer to hold many i32 values to use as settings
let settings_buffer = {
let vec = vec![self.xy.0, self.xy.1];
let mut buff = vec.iter();
let data_iter =
(0..2).map(|n| *(buff.next().unwrap()));
CpuAccessibleBuffer::from_iter(self.device.clone(),
BufferUsage::all(),
data_iter).unwrap()
};
println!("Done");
// Create the data descriptor set for our previously created shader pipeline
let mut set =
PersistentDescriptorSet::start(self.compute_pipeline.clone().unwrap().clone(), 0)
.add_buffer(write_buffer.clone()).unwrap()
.add_buffer(read_buffer.clone()).unwrap()
.add_buffer(settings_buffer.clone()).unwrap();
self.compute_set = Some(Arc::new(set.build().unwrap()));
self.img_buffers.push(write_buffer);
self.img_buffers.push(read_buffer);
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);
CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), [
tVertex { position: [-1.0, -1.0 ] },
tVertex { position: [-1.0, 1.0 ] },
tVertex { position: [ 1.0, 1.0 ] },
tVertex { position: [ 1.0, -1.0 ] },
].iter().cloned()).unwrap()
};
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> {
@ -490,7 +597,8 @@ impl<'a> VkProcessor<'a> {
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()
.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()
@ -504,6 +612,16 @@ impl<'a> VkProcessor<'a> {
.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()
@ -526,100 +644,6 @@ impl<'a> VkProcessor<'a> {
}
}
pub fn load_buffers(&mut self, image_filename: String)
{
let project_root =
std::env::current_dir()
.expect("failed to get root directory");
let mut compute_path = project_root.clone();
compute_path.push(PathBuf::from("resources/images/"));
compute_path.push(PathBuf::from(image_filename));
let img = image::open(compute_path).expect("Couldn't find image");
self.xy = img.dimensions();
let data_length = self.xy.0 * self.xy.1 * 4;
let pixel_count = img.raw_pixels().len();
println!("Pixel count {}", pixel_count);
if pixel_count != data_length as usize {
println!("Creating apha channel...");
for i in img.raw_pixels().iter() {
if (self.image_buffer.len() + 1) % 4 == 0 {
self.image_buffer.push(255);
}
self.image_buffer.push(*i);
}
self.image_buffer.push(255);
} else {
self.image_buffer = img.raw_pixels();
}
println!("Buffer length {}", self.image_buffer.len());
println!("Size {:?}", self.xy);
println!("Allocating Buffers...");
// Pull out the image data and place it in a buffer for the kernel to write to and for us to read from
let write_buffer = {
let mut buff = self.image_buffer.iter();
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), data_iter).unwrap()
};
// Pull out the image data and place it in a buffer for the kernel to read from
let read_buffer = {
let mut buff = self.image_buffer.iter();
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), data_iter).unwrap()
};
// A buffer to hold many i32 values to use as settings
let settings_buffer = {
let vec = vec![self.xy.0, self.xy.1];
let mut buff = vec.iter();
let data_iter =
(0..2).map(|n| *(buff.next().unwrap()));
CpuAccessibleBuffer::from_iter(self.device.clone(),
BufferUsage::all(),
data_iter).unwrap()
};
println!("Done");
// Create the data descriptor set for our previously created shader pipeline
let mut set =
PersistentDescriptorSet::start(self.compute_pipeline.clone().unwrap().clone(), 0)
.add_buffer(write_buffer.clone()).unwrap()
.add_buffer(read_buffer.clone()).unwrap()
.add_buffer(settings_buffer.clone()).unwrap();
self.compute_set = Some(Arc::new(set.build().unwrap()));
self.img_buffers.push(write_buffer);
self.img_buffers.push(read_buffer);
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);
CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), [
tVertex { position: [-1.0, -1.0 ] },
tVertex { position: [-1.0, 1.0 ] },
tVertex { position: [ 1.0, 1.0 ] },
tVertex { position: [ 1.0, -1.0 ] },
].iter().cloned()).unwrap()
};
self.vertex_buffer = Some(vertex_buffer);
}
// pub fn read_image(&self) -> Vec<u8> {
//
// // The buffer is sync'd so we can just read straight from the handle
@ -683,3 +707,4 @@ impl<'a> VkProcessor<'a> {

Loading…
Cancel
Save