parent
878b37c3e0
commit
1737319fc5
@ -0,0 +1,62 @@
|
|||||||
|
use crate::vertex_2d::{ColoredVertex2D, Vertex2D};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use crate::canvas::Drawable;
|
||||||
|
|
||||||
|
pub struct CanvasFrame {
|
||||||
|
pub colored_drawables: Vec<ColoredVertex2D>,
|
||||||
|
pub textured_drawables: HashMap<Arc<u32>, Vec<Vec<Vertex2D>>>,
|
||||||
|
pub image_drawables: HashMap<Arc<u32>, Vec<Vec<Vertex2D>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CanvasFrame {
|
||||||
|
pub fn new() -> CanvasFrame {
|
||||||
|
CanvasFrame {
|
||||||
|
colored_drawables: vec![],
|
||||||
|
textured_drawables: Default::default(),
|
||||||
|
image_drawables: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accumulates the drawables vertices and colors
|
||||||
|
pub fn draw(&mut self, drawable: &dyn Drawable) {
|
||||||
|
match drawable.get_texture_handle() {
|
||||||
|
Some(handle) => {
|
||||||
|
self.textured_drawables
|
||||||
|
.entry(handle.clone())
|
||||||
|
.or_insert(Vec::new())
|
||||||
|
.push(drawable.get_vertices().iter().map(|n|
|
||||||
|
Vertex2D {
|
||||||
|
position: [n.0, n.1],
|
||||||
|
}
|
||||||
|
).collect::<Vec<Vertex2D>>());
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
match drawable.get_image_handle() {
|
||||||
|
Some(handle) => {
|
||||||
|
self.image_drawables
|
||||||
|
.entry(handle.clone())
|
||||||
|
.or_insert(Vec::new())
|
||||||
|
.push(drawable.get_vertices().iter().map(|n|
|
||||||
|
Vertex2D {
|
||||||
|
position: [n.0, n.1],
|
||||||
|
}
|
||||||
|
).collect());
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
let colors = drawable.get_color();
|
||||||
|
|
||||||
|
self.colored_drawables.extend(
|
||||||
|
drawable.get_vertices().iter().map(|n|
|
||||||
|
ColoredVertex2D {
|
||||||
|
position: [n.0, n.1],
|
||||||
|
color: [colors.0, colors.1, colors.2, colors.3],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,99 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
use vulkano::device::Device;
|
||||||
|
use vulkano::buffer::{CpuAccessibleBuffer, BufferUsage};
|
||||||
|
use vulkano::pipeline::ComputePipeline;
|
||||||
|
use vulkano::descriptor::pipeline_layout::PipelineLayout;
|
||||||
|
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
|
||||||
|
use image::ImageBuffer;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct CompuBuffers {
|
||||||
|
dimensions: (u32, u32),
|
||||||
|
device: Arc<Device>,
|
||||||
|
handle: Arc<CompuBufferHandle>,
|
||||||
|
|
||||||
|
io_buffers: Vec<Arc<CpuAccessibleBuffer<[u8]>>>,
|
||||||
|
settings_buffer: Arc<CpuAccessibleBuffer<[u32]>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CompuBuffers {
|
||||||
|
pub fn new(device: Arc<Device>, data: Vec<u8>,
|
||||||
|
dimensions: (u32, u32), stride: u32,
|
||||||
|
handle: Arc<CompuBufferHandle>) -> CompuBuffers {
|
||||||
|
|
||||||
|
let data_length = dimensions.0 * dimensions.1 * stride;
|
||||||
|
|
||||||
|
let input_buffer = {
|
||||||
|
let mut buff = data.iter();
|
||||||
|
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
|
||||||
|
CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
|
let output_buffer = {
|
||||||
|
let mut buff = data.iter();
|
||||||
|
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
|
||||||
|
CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Settings buffer which holds i32's
|
||||||
|
// Compile macros into the kernel eventually to index them
|
||||||
|
let settings_buffer = {
|
||||||
|
let vec = vec![dimensions.0, dimensions.1];
|
||||||
|
let mut buff = vec.iter();
|
||||||
|
let data_iter =
|
||||||
|
(0..2).map(|n| *(buff.next().unwrap()));
|
||||||
|
CpuAccessibleBuffer::from_iter(device.clone(),
|
||||||
|
BufferUsage::all(),
|
||||||
|
data_iter).unwrap()
|
||||||
|
};
|
||||||
|
|
||||||
|
CompuBuffers {
|
||||||
|
dimensions: dimensions,
|
||||||
|
device: device.clone(),
|
||||||
|
|
||||||
|
handle: handle,
|
||||||
|
io_buffers: vec![input_buffer, output_buffer],
|
||||||
|
settings_buffer: settings_buffer,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_size(&self) -> (u32, u32) {
|
||||||
|
self.dimensions
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_descriptor_set(&self, compute_pipeline: std::sync::Arc<ComputePipeline<PipelineLayout<shade_runner::layouts::ComputeLayout>>>)
|
||||||
|
-> Arc<PersistentDescriptorSet<std::sync::Arc<ComputePipeline<PipelineLayout<shade_runner::layouts::ComputeLayout>>>, ((((),
|
||||||
|
PersistentDescriptorSetBuf<std::sync::Arc<vulkano::buffer::cpu_access::CpuAccessibleBuffer<[u8]>>>),
|
||||||
|
PersistentDescriptorSetBuf<std::sync::Arc<vulkano::buffer::cpu_access::CpuAccessibleBuffer<[u8]>>>),
|
||||||
|
PersistentDescriptorSetBuf<std::sync::Arc<vulkano::buffer::cpu_access::CpuAccessibleBuffer<[u32]>>>)>> {
|
||||||
|
Arc::new(PersistentDescriptorSet::start(compute_pipeline.clone(), 0)
|
||||||
|
.add_buffer(self.io_buffers.get(0).unwrap().clone()).unwrap()
|
||||||
|
.add_buffer(self.io_buffers.get(1).unwrap().clone()).unwrap()
|
||||||
|
.add_buffer(self.settings_buffer.clone()).unwrap()
|
||||||
|
.build().unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_output_buffer(&self) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
|
||||||
|
let xy = self.get_size();
|
||||||
|
|
||||||
|
self.io_buffers.get(1).unwrap().write().unwrap().map(|x| x);
|
||||||
|
let data_buffer_content = self.io_buffers.get(1).unwrap().read().unwrap();
|
||||||
|
ImageBuffer::from_fn(xy.0, xy.1, |x, y| {
|
||||||
|
let r = data_buffer_content[((xy.0 * y + x) * 4 + 0) as usize] as u8;
|
||||||
|
let g = data_buffer_content[((xy.0 * y + x) * 4 + 1) as usize] as u8;
|
||||||
|
let b = data_buffer_content[((xy.0 * y + x) * 4 + 2) as usize] as u8;
|
||||||
|
let a = data_buffer_content[((xy.0 * y + x) * 4 + 3) as usize] as u8;
|
||||||
|
|
||||||
|
image::Rgba([r, g, b, a])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct CompuBufferHandle {
|
||||||
|
handle: u32,
|
||||||
|
}
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct CompuKernelHandle {
|
||||||
|
handle: u32,
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
use crate::canvas::ImageHandle;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use crate::compu_sprite::CompuSprite;
|
||||||
|
use crate::compu_buffer::{CompuBufferHandle, CompuKernelHandle};
|
||||||
|
|
||||||
|
pub struct CompuFrame {
|
||||||
|
// Vec<(Buffer, Kernel)>
|
||||||
|
pure_compute: Vec<(
|
||||||
|
Arc<CompuBufferHandle>,
|
||||||
|
Arc<CompuKernelHandle>)>,
|
||||||
|
|
||||||
|
// Vec<(Buffer, Image, Kernel)>
|
||||||
|
swapped_to_image: Vec<(
|
||||||
|
Arc<CompuBufferHandle>,
|
||||||
|
Arc<ImageHandle>,
|
||||||
|
Arc<CompuKernelHandle>)>,
|
||||||
|
|
||||||
|
// Vec<(Input Buffer, Output Buffer, Kernel)>
|
||||||
|
swapped_to_buffer: Vec<(
|
||||||
|
Arc<CompuBufferHandle>,
|
||||||
|
Arc<CompuBufferHandle>,
|
||||||
|
Arc<CompuKernelHandle>)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CompuFrame {
|
||||||
|
pub fn new() -> CompuFrame {
|
||||||
|
CompuFrame {
|
||||||
|
pure_compute: vec![],
|
||||||
|
swapped_to_image: vec![],
|
||||||
|
swapped_to_buffer: vec![],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add(&mut self, buffer: Arc<CompuBufferHandle>, kernel: Arc<CompuKernelHandle>) {
|
||||||
|
self.pure_compute.push((buffer, kernel));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
INPUT_BUFFER -> input -> kernel -> output
|
||||||
|
v------------------^
|
||||||
|
OUTPUT_BUFFER -> input X kernel X output
|
||||||
|
*/
|
||||||
|
pub fn add_chained(&mut self,
|
||||||
|
input_buffer: Arc<CompuBufferHandle>,
|
||||||
|
output_buffer: Arc<CompuBufferHandle>,
|
||||||
|
kernel: Arc<CompuKernelHandle>) {
|
||||||
|
self.swapped_to_buffer.push((input_buffer, output_buffer, kernel));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_with_image_swap(&mut self,
|
||||||
|
buffer: Arc<CompuBufferHandle>,
|
||||||
|
kernel: Arc<CompuKernelHandle>,
|
||||||
|
sprite: &CompuSprite) {
|
||||||
|
self.swapped_to_image.push((buffer, sprite.get_image_handle().clone(), kernel))
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
use crate::canvas::{ImageHandle, Drawable, TextureHandle};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub struct CompuSprite {
|
||||||
|
vertices: [(f32, f32); 6],
|
||||||
|
position: (f32, f32),
|
||||||
|
size: (f32, f32),
|
||||||
|
color: (f32, f32, f32, f32),
|
||||||
|
image_handle: Arc<ImageHandle>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drawable for CompuSprite {
|
||||||
|
fn get_vertices(&self) -> Vec<(f32, f32)> {
|
||||||
|
self.vertices.to_vec()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_color(&self) -> (f32, f32, f32, f32) {
|
||||||
|
self.color
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_texture_handle(&self) -> Option<Arc<TextureHandle>> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_image_handle(&self) -> Option<Arc<ImageHandle>> {
|
||||||
|
Some(self.image_handle.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CompuSprite {
|
||||||
|
pub fn new(position: (f32, f32),
|
||||||
|
size: (f32, f32),
|
||||||
|
image_handle: Arc<ImageHandle>) -> CompuSprite {
|
||||||
|
let fsize = (size.0 as f32, size.1 as f32);
|
||||||
|
|
||||||
|
CompuSprite {
|
||||||
|
vertices: [
|
||||||
|
(position.0, position.1), // top left
|
||||||
|
(position.0, position.1 + fsize.1), // bottom left
|
||||||
|
(position.0 + fsize.0, position.1 + fsize.1), // bottom right
|
||||||
|
(position.0, position.1), // top left
|
||||||
|
(position.0 + fsize.0, position.1 + fsize.1), // bottom right
|
||||||
|
(position.0 + fsize.0, position.1), // top right
|
||||||
|
],
|
||||||
|
|
||||||
|
position: position,
|
||||||
|
size: size,
|
||||||
|
color: (0.0, 0.0, 0.0, 0.0),
|
||||||
|
image_handle: image_handle.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_vertices(&self) -> Vec<(f32, f32)> {
|
||||||
|
self.vertices.to_vec()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_color(&self) -> (f32, f32, f32, f32) {
|
||||||
|
self.color.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_image_handle(&self) -> Arc<ImageHandle> {
|
||||||
|
self.image_handle.clone()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,141 @@
|
|||||||
|
use std::ffi::CStr;
|
||||||
|
use vulkano::buffer::{CpuAccessibleBuffer, BufferUsage};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use crate::canvas::{Drawable, CanvasState, ImageHandle, CanvasImage, TextureHandle};
|
||||||
|
use vulkano::framebuffer::RenderPassAbstract;
|
||||||
|
use vulkano::pipeline::{GraphicsPipelineAbstract, ComputePipeline};
|
||||||
|
use vulkano::device::Device;
|
||||||
|
use image::ImageBuffer;
|
||||||
|
use image::GenericImageView;
|
||||||
|
use vulkano::image::{ImageUsage, AttachmentImage};
|
||||||
|
use vulkano::descriptor::descriptor_set::{PersistentDescriptorSetBuf, PersistentDescriptorSet};
|
||||||
|
use vulkano::format::Format;
|
||||||
|
use vulkano::descriptor::pipeline_layout::PipelineLayout;
|
||||||
|
use std::borrow::Borrow;
|
||||||
|
use image::Rgba;
|
||||||
|
use vulkano::command_buffer::AutoCommandBufferBuilder;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use shade_runner::{CompiledShaders, Entry, CompileError};
|
||||||
|
use vulkano::pipeline::shader::ShaderModule;
|
||||||
|
use shaderc::CompileOptions;
|
||||||
|
use crate::compu_kernel::CompuKernel;
|
||||||
|
use crate::compu_buffer::{CompuBuffers, CompuBufferHandle, CompuKernelHandle};
|
||||||
|
use crate::compu_frame::CompuFrame;
|
||||||
|
|
||||||
|
|
||||||
|
// Canvas analog
|
||||||
|
pub struct CompuState {
|
||||||
|
compute_buffers: Vec<CompuBuffers>,
|
||||||
|
kernels: Vec<CompuKernel>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CompuState {
|
||||||
|
pub fn new() -> CompuState {
|
||||||
|
CompuState {
|
||||||
|
compute_buffers: vec![],
|
||||||
|
kernels: vec![],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_compute_buffer(&mut self,
|
||||||
|
data: Vec<u8>,
|
||||||
|
dimensions: (u32, u32),
|
||||||
|
stride: u32,
|
||||||
|
device: Arc<Device>) -> Arc<CompuBufferHandle> {
|
||||||
|
|
||||||
|
let handle = Arc::new(CompuBufferHandle {
|
||||||
|
handle: self.compute_buffers.len() as u32 + 1
|
||||||
|
});
|
||||||
|
|
||||||
|
self.compute_buffers.push(
|
||||||
|
CompuBuffers::new(device.clone(), data, dimensions, stride, handle.clone()));
|
||||||
|
|
||||||
|
handle
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_compute_buffer(&mut self, handle: Arc<CompuBufferHandle>) -> Vec<u8> {
|
||||||
|
// This is way more difficult than it should be
|
||||||
|
//let compute_buffer : CompuBuffers = self.compute_buffers.get(handle.into()).unwrap();
|
||||||
|
//compute_buffer.read_output_buffer().to_vec()
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
pub fn write_compute_buffer(&self, handle: Arc<CompuBufferHandle>, data: Vec<u8>) {}
|
||||||
|
|
||||||
|
pub fn new_kernel(&mut self,
|
||||||
|
filename: String,
|
||||||
|
device: &Arc<Device>) -> Arc<CompuKernelHandle> {
|
||||||
|
|
||||||
|
let handle = Arc::new(CompuKernelHandle {
|
||||||
|
handle: self.kernels.len() as u32 + 1
|
||||||
|
});
|
||||||
|
|
||||||
|
self.kernels.push(CompuKernel::new(filename, device.clone(), handle.clone()));
|
||||||
|
|
||||||
|
handle
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn compute_commands(&mut self,
|
||||||
|
compute_frame: CompuFrame,
|
||||||
|
mut command_buffer: AutoCommandBufferBuilder,
|
||||||
|
canvas: &CanvasState)
|
||||||
|
-> AutoCommandBufferBuilder {
|
||||||
|
|
||||||
|
// i = (Buffer, Kernel)
|
||||||
|
for i in compute_frame.pure_compute {
|
||||||
|
let buffer_id = (*i.0).clone() as usize;
|
||||||
|
let kernel_id = (*i.1).clone() as usize;
|
||||||
|
|
||||||
|
let buffer = self.compute_buffers.get(buffer_id).unwrap();
|
||||||
|
let kernel = self.kernels.get(buffer_id).unwrap();
|
||||||
|
|
||||||
|
let p = kernel.clone().get_pipeline();
|
||||||
|
let d = buffer.get_descriptor_set(kernel.clone().get_pipeline());
|
||||||
|
|
||||||
|
command_buffer = command_buffer
|
||||||
|
.dispatch([100,100,1], p, d, ()).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
// i = (Buffer, Image, Kernel)
|
||||||
|
for i in compute_frame.swapped_to_image {
|
||||||
|
let buffer_id = (*i.0).clone() as usize;
|
||||||
|
let image_id = i.1.clone();
|
||||||
|
let kernel_id = (*i.2).clone() as usize;
|
||||||
|
|
||||||
|
let buffer = self.compute_buffers.get(buffer_id).unwrap();
|
||||||
|
let image = canvas.get_image(image_id);
|
||||||
|
let kernel = self.kernels.get(buffer_id).unwrap();
|
||||||
|
|
||||||
|
let p = kernel.clone().get_pipeline();
|
||||||
|
let d = buffer.get_descriptor_set(kernel.clone().get_pipeline());
|
||||||
|
|
||||||
|
command_buffer = command_buffer
|
||||||
|
.dispatch([100,100,1], p, d, ()).unwrap()
|
||||||
|
.copy_buffer_to_image(buffer.io_buffers.get(0).unwrap().clone(), image).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// i = (Input Buffer, Output Buffer, Kernel)
|
||||||
|
// Input buffer -> Kernel -> Output buffer
|
||||||
|
for i in compute_frame.swapped_to_buffer {
|
||||||
|
let input_buffer_id = (*i.0).clone() as usize;
|
||||||
|
let output_buffer_id = (*i.1).clone() as usize;
|
||||||
|
let kernel_id = (*i.2).clone() as usize;
|
||||||
|
|
||||||
|
let input_buffer = self.compute_buffers.get(input_buffer_id).unwrap();
|
||||||
|
let output_buffer = self.compute_buffers.get(output_buffer_id).unwrap();
|
||||||
|
let kernel = self.kernels.get(kernel_id).unwrap();
|
||||||
|
|
||||||
|
let pipeline = kernel.clone().get_pipeline();
|
||||||
|
let descriptor_set = input_buffer.get_descriptor_set(kernel.clone().get_pipeline());
|
||||||
|
|
||||||
|
command_buffer = command_buffer
|
||||||
|
.dispatch([100,100,1], pipeline, descriptor_set, ()).unwrap()
|
||||||
|
.copy_buffer(
|
||||||
|
input_buffer.io_buffers.get(1).unwrap().clone(),
|
||||||
|
output_buffer.io_buffers.get(0).unwrap().clone()).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
command_buffer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,315 +0,0 @@
|
|||||||
use vulkano::buffer::{CpuAccessibleBuffer, BufferUsage};
|
|
||||||
use std::sync::Arc;
|
|
||||||
use crate::canvas::{Drawable, Canvas};
|
|
||||||
use vulkano::framebuffer::RenderPassAbstract;
|
|
||||||
use vulkano::pipeline::{GraphicsPipelineAbstract, ComputePipeline};
|
|
||||||
use vulkano::device::Device;
|
|
||||||
use crate::util::compute_kernel::ComputeKernel;
|
|
||||||
use image::ImageBuffer;
|
|
||||||
use image::GenericImageView;
|
|
||||||
use crate::util::compute_image::ComputeImage;
|
|
||||||
use vulkano::image::{ImageUsage, AttachmentImage};
|
|
||||||
use vulkano::descriptor::descriptor_set::{PersistentDescriptorSetBuf, PersistentDescriptorSet};
|
|
||||||
use vulkano::format::Format;
|
|
||||||
use vulkano::descriptor::pipeline_layout::PipelineLayout;
|
|
||||||
use std::borrow::Borrow;
|
|
||||||
use image::Rgba;
|
|
||||||
use vulkano::command_buffer::AutoCommandBufferBuilder;
|
|
||||||
|
|
||||||
pub struct CompuSprite {
|
|
||||||
vertices: [(f32, f32); 6],
|
|
||||||
position: (f32, f32),
|
|
||||||
size: (f32, f32),
|
|
||||||
color: (f32, f32, f32, f32),
|
|
||||||
image_handle: Arc<u32>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drawable for CompuSprite {
|
|
||||||
fn get_vertices(&self) -> Vec<(f32, f32)> {
|
|
||||||
self.vertices.to_vec()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_color(&self) -> (f32, f32, f32, f32) {
|
|
||||||
self.color
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_texture_handle(&self) -> Option<Arc<u32>> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_image_handle(&self) -> Option<Arc<u32>> {
|
|
||||||
Some(self.image_handle.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CompuSprite {
|
|
||||||
pub fn new(position: (f32, f32),
|
|
||||||
size: (f32, f32),
|
|
||||||
image_handle: Arc<u32>) -> CompuSprite {
|
|
||||||
let fsize = (size.0 as f32, size.1 as f32);
|
|
||||||
|
|
||||||
CompuSprite {
|
|
||||||
vertices: [
|
|
||||||
(position.0, position.1), // top left
|
|
||||||
(position.0, position.1 + fsize.1), // bottom left
|
|
||||||
(position.0 + fsize.0, position.1 + fsize.1), // bottom right
|
|
||||||
(position.0, position.1), // top left
|
|
||||||
(position.0 + fsize.0, position.1 + fsize.1), // bottom right
|
|
||||||
(position.0 + fsize.0, position.1), // top right
|
|
||||||
],
|
|
||||||
|
|
||||||
position: position,
|
|
||||||
size: size,
|
|
||||||
color: (0.0, 0.0, 0.0, 0.0),
|
|
||||||
image_handle: image_handle.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_vertices(&self) -> Vec<(f32, f32)> {
|
|
||||||
self.vertices.to_vec()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_color(&self) -> (f32, f32, f32, f32) {
|
|
||||||
self.color.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_image_handle(&self) -> Arc<u32> {
|
|
||||||
self.image_handle.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct CompuBuffers {
|
|
||||||
dimensions: (u32, u32),
|
|
||||||
device: Arc<Device>,
|
|
||||||
|
|
||||||
io_buffers: Vec<Arc<CpuAccessibleBuffer<[u8]>>>,
|
|
||||||
settings_buffer: Arc<CpuAccessibleBuffer<[u32]>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CompuBuffers {
|
|
||||||
pub fn new(device: Arc<Device>, data: Vec<u8>, dimensions: (u32, u32), stride: u32) -> CompuBuffers {
|
|
||||||
let data_length = dimensions.0 * dimensions.1 * stride;
|
|
||||||
|
|
||||||
let input_buffer = {
|
|
||||||
let mut buff = data.iter();
|
|
||||||
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
|
|
||||||
CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
let output_buffer = {
|
|
||||||
let mut buff = data.iter();
|
|
||||||
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
|
|
||||||
CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
// Settings buffer which holds i32's
|
|
||||||
// Compile macros into the kernel eventually to index them
|
|
||||||
let settings_buffer = {
|
|
||||||
let vec = vec![dimensions.0, dimensions.1];
|
|
||||||
let mut buff = vec.iter();
|
|
||||||
let data_iter =
|
|
||||||
(0..2).map(|n| *(buff.next().unwrap()));
|
|
||||||
CpuAccessibleBuffer::from_iter(device.clone(),
|
|
||||||
BufferUsage::all(),
|
|
||||||
data_iter).unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
CompuBuffers {
|
|
||||||
dimensions: dimensions,
|
|
||||||
device: device.clone(),
|
|
||||||
|
|
||||||
io_buffers: vec![input_buffer, output_buffer],
|
|
||||||
settings_buffer: settings_buffer,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_size(&self) -> (u32, u32) {
|
|
||||||
self.dimensions
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_descriptor_set(&self, compute_pipeline: std::sync::Arc<ComputePipeline<PipelineLayout<shade_runner::layouts::ComputeLayout>>>)
|
|
||||||
-> Arc<PersistentDescriptorSet<std::sync::Arc<ComputePipeline<PipelineLayout<shade_runner::layouts::ComputeLayout>>>, ((((),
|
|
||||||
PersistentDescriptorSetBuf<std::sync::Arc<vulkano::buffer::cpu_access::CpuAccessibleBuffer<[u8]>>>),
|
|
||||||
PersistentDescriptorSetBuf<std::sync::Arc<vulkano::buffer::cpu_access::CpuAccessibleBuffer<[u8]>>>),
|
|
||||||
PersistentDescriptorSetBuf<std::sync::Arc<vulkano::buffer::cpu_access::CpuAccessibleBuffer<[u32]>>>)>> {
|
|
||||||
Arc::new(PersistentDescriptorSet::start(compute_pipeline.clone(), 0)
|
|
||||||
.add_buffer(self.io_buffers.get(0).unwrap().clone()).unwrap()
|
|
||||||
.add_buffer(self.io_buffers.get(1).unwrap().clone()).unwrap()
|
|
||||||
.add_buffer(self.settings_buffer.clone()).unwrap()
|
|
||||||
.build().unwrap())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_output_buffer(&self) -> ImageBuffer<Rgba<u8>, Vec<u8>> {
|
|
||||||
let xy = self.get_size();
|
|
||||||
|
|
||||||
self.io_buffers.get(1).unwrap().write().unwrap().map(|x| x);
|
|
||||||
let data_buffer_content = self.io_buffers.get(1).unwrap().read().unwrap();
|
|
||||||
ImageBuffer::from_fn(xy.0, xy.1, |x, y| {
|
|
||||||
let r = data_buffer_content[((xy.0 * y + x) * 4 + 0) as usize] as u8;
|
|
||||||
let g = data_buffer_content[((xy.0 * y + x) * 4 + 1) as usize] as u8;
|
|
||||||
let b = data_buffer_content[((xy.0 * y + x) * 4 + 2) as usize] as u8;
|
|
||||||
let a = data_buffer_content[((xy.0 * y + x) * 4 + 3) as usize] as u8;
|
|
||||||
|
|
||||||
image::Rgba([r, g, b, a])
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Canvas analog
|
|
||||||
pub struct CompuState {
|
|
||||||
compute_buffers: Vec<CompuBuffers>,
|
|
||||||
compute_buffer_handles: Vec<Arc<u32>>,
|
|
||||||
|
|
||||||
kernels: Vec<ComputeKernel>,
|
|
||||||
kernel_handles: Vec<Arc<u32>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CompuState {
|
|
||||||
pub fn new() -> CompuState {
|
|
||||||
CompuState {
|
|
||||||
compute_buffers: vec![],
|
|
||||||
compute_buffer_handles: vec![],
|
|
||||||
kernels: vec![],
|
|
||||||
kernel_handles: vec![],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_compute_buffer(&mut self,
|
|
||||||
data: Vec<u8>,
|
|
||||||
dimensions: (u32, u32),
|
|
||||||
stride: u32,
|
|
||||||
device: Arc<Device>) -> Arc<u32> {
|
|
||||||
self.compute_buffers.push(
|
|
||||||
CompuBuffers::new(device.clone(), data, dimensions, stride));
|
|
||||||
|
|
||||||
let id = Arc::new(self.compute_buffers.len() as u32);
|
|
||||||
self.compute_buffer_handles.push(id.clone());
|
|
||||||
id
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_compute_buffer(&mut self, handle: Arc<u32>) -> Vec<u8> {
|
|
||||||
// This is way more difficult than it should be
|
|
||||||
//let compute_buffer : CompuBuffers = self.compute_buffers.get(handle.into()).unwrap();
|
|
||||||
//compute_buffer.read_output_buffer().to_vec()
|
|
||||||
Vec::new()
|
|
||||||
}
|
|
||||||
pub fn write_compute_buffer(&self, handle: Arc<u32>, data: Vec<u8>) {}
|
|
||||||
|
|
||||||
pub fn new_kernel(&mut self,
|
|
||||||
filename: String,
|
|
||||||
device: &Arc<Device>) -> Arc<u32> {
|
|
||||||
let kernel = ComputeKernel::new(filename, device.clone());
|
|
||||||
|
|
||||||
self.kernels.push(kernel);
|
|
||||||
|
|
||||||
let id = Arc::new(self.kernels.len() as u32);
|
|
||||||
|
|
||||||
self.kernel_handles.push(id.clone());
|
|
||||||
|
|
||||||
id
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn compute_commands(&mut self,
|
|
||||||
compute_frame: ComputeFrame,
|
|
||||||
mut command_buffer: AutoCommandBufferBuilder,
|
|
||||||
canvas: &Canvas)
|
|
||||||
-> AutoCommandBufferBuilder {
|
|
||||||
|
|
||||||
// i = (Buffer, Kernel)
|
|
||||||
for i in compute_frame.pure_compute {
|
|
||||||
let buffer_id = (*i.0).clone() as usize;
|
|
||||||
let kernel_id = (*i.1).clone() as usize;
|
|
||||||
|
|
||||||
let buffer = self.compute_buffers.get(buffer_id).unwrap();
|
|
||||||
let kernel = self.kernels.get(buffer_id).unwrap();
|
|
||||||
|
|
||||||
let p = kernel.clone().get_pipeline();
|
|
||||||
let d = buffer.get_descriptor_set(kernel.clone().get_pipeline());
|
|
||||||
|
|
||||||
command_buffer = command_buffer
|
|
||||||
.dispatch([100,100,1], p, d, ()).unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
// i = (Buffer, Image, Kernel)
|
|
||||||
for i in compute_frame.swapped_to_image {
|
|
||||||
let buffer_id = (*i.0).clone() as usize;
|
|
||||||
let image_id = i.1.clone();
|
|
||||||
let kernel_id = (*i.2).clone() as usize;
|
|
||||||
|
|
||||||
let buffer = self.compute_buffers.get(buffer_id).unwrap();
|
|
||||||
let image = canvas.get_image(image_id);
|
|
||||||
let kernel = self.kernels.get(buffer_id).unwrap();
|
|
||||||
|
|
||||||
let p = kernel.clone().get_pipeline();
|
|
||||||
let d = buffer.get_descriptor_set(kernel.clone().get_pipeline());
|
|
||||||
|
|
||||||
command_buffer = command_buffer
|
|
||||||
.dispatch([100,100,1], p, d, ()).unwrap()
|
|
||||||
.copy_buffer_to_image(buffer.io_buffers.get(0).unwrap().clone(), image).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// i = (Input Buffer, Output Buffer, Kernel)
|
|
||||||
// Input buffer -> Kernel -> Output buffer
|
|
||||||
for i in compute_frame.swapped_to_buffer {
|
|
||||||
let input_buffer_id = (*i.0).clone() as usize;
|
|
||||||
let output_buffer_id = (*i.1).clone() as usize;
|
|
||||||
let kernel_id = (*i.2).clone() as usize;
|
|
||||||
|
|
||||||
let input_buffer = self.compute_buffers.get(input_buffer_id).unwrap();
|
|
||||||
let output_buffer = self.compute_buffers.get(output_buffer_id).unwrap();
|
|
||||||
let kernel = self.kernels.get(kernel_id).unwrap();
|
|
||||||
|
|
||||||
let pipeline = kernel.clone().get_pipeline();
|
|
||||||
let descriptor_set = input_buffer.get_descriptor_set(kernel.clone().get_pipeline());
|
|
||||||
|
|
||||||
command_buffer = command_buffer
|
|
||||||
.dispatch([100,100,1], pipeline, descriptor_set, ()).unwrap()
|
|
||||||
.copy_buffer(
|
|
||||||
input_buffer.io_buffers.get(1).unwrap().clone(),
|
|
||||||
output_buffer.io_buffers.get(0).unwrap().clone()).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
command_buffer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub struct ComputeFrame {
|
|
||||||
// Vec<(Buffer, Kernel)>
|
|
||||||
pure_compute: Vec<(Arc<u32>, Arc<u32>)>,
|
|
||||||
|
|
||||||
// Vec<(Buffer, Image, Kernel)>
|
|
||||||
swapped_to_image: Vec<(Arc<u32>, Arc<u32>, Arc<u32>)>,
|
|
||||||
|
|
||||||
// Vec<(Input Buffer, Output Buffer, Kernel)>
|
|
||||||
swapped_to_buffer: Vec<(Arc<u32>, Arc<u32>, Arc<u32>)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ComputeFrame {
|
|
||||||
pub fn new() -> ComputeFrame {
|
|
||||||
ComputeFrame {
|
|
||||||
pure_compute: vec![],
|
|
||||||
swapped_to_image: vec![],
|
|
||||||
swapped_to_buffer: vec![],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add(&mut self, buffer: Arc<u32>, kernel: Arc<u32>) {
|
|
||||||
self.pure_compute.push((buffer, kernel));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
INPUT_BUFFER -> input -> kernel -> output
|
|
||||||
v------------------^
|
|
||||||
OUTPUT_BUFFER -> input X kernel X output
|
|
||||||
*/
|
|
||||||
pub fn add_chained(&mut self, input_buffer: Arc<u32>, output_buffer: Arc<u32>, kernel: Arc<u32>) {
|
|
||||||
self.swapped_to_buffer.push((input_buffer, output_buffer, kernel));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_with_image_swap(&mut self, buffer: Arc<u32>, kernel: Arc<u32>, sprite: &CompuSprite) {
|
|
||||||
self.swapped_to_image.push((buffer, sprite.get_image_handle().clone(), kernel))
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,172 +0,0 @@
|
|||||||
use vulkano::buffer::{BufferUsage, CpuAccessibleBuffer};
|
|
||||||
use vulkano::descriptor::descriptor_set::{PersistentDescriptorSet};
|
|
||||||
use vulkano::device::{Device};
|
|
||||||
use vulkano::pipeline::{ComputePipeline};
|
|
||||||
use std::time::SystemTime;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use std::path::PathBuf;
|
|
||||||
use image::{ImageBuffer};
|
|
||||||
use image::GenericImageView;
|
|
||||||
use vulkano::descriptor::pipeline_layout::PipelineLayout;
|
|
||||||
use vulkano::descriptor::descriptor_set::{PersistentDescriptorSetBuf};
|
|
||||||
use vulkano::image::attachment::AttachmentImage;
|
|
||||||
use vulkano::image::{ImageUsage};
|
|
||||||
use vulkano::format::Format;
|
|
||||||
use image::Rgba;
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
Compute Image holds read write swap and settings buffers for the kernel
|
|
||||||
|
|
||||||
This is a pretty specific use case. One in for settings. One in for data, two for the transfer.
|
|
||||||
|
|
||||||
multiple data inputs might be nice?
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct ComputeImage {
|
|
||||||
|
|
||||||
device: Arc<Device>,
|
|
||||||
|
|
||||||
compute_graphics_swap_buffer: std::sync::Arc<vulkano::image::attachment::AttachmentImage>,
|
|
||||||
|
|
||||||
pub rw_buffers: Vec<Arc<CpuAccessibleBuffer<[u8]>>>,
|
|
||||||
pub settings_buffer: Arc<CpuAccessibleBuffer<[u32]>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl ComputeImage {
|
|
||||||
|
|
||||||
pub fn load_raw(filename: String) -> (Vec<u8>, (u32,u32)) {
|
|
||||||
|
|
||||||
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(filename.clone()));
|
|
||||||
|
|
||||||
let img = image::open(compute_path).expect("Couldn't find image");
|
|
||||||
|
|
||||||
let xy = img.dimensions();
|
|
||||||
|
|
||||||
let data_length = xy.0 * xy.1 * 4;
|
|
||||||
let pixel_count = img.raw_pixels().len();
|
|
||||||
|
|
||||||
let mut image_buffer = Vec::new();
|
|
||||||
|
|
||||||
if pixel_count != data_length as usize {
|
|
||||||
println!("Creating apha channel...");
|
|
||||||
for i in img.raw_pixels().iter() {
|
|
||||||
if (image_buffer.len() + 1) % 4 == 0 {
|
|
||||||
image_buffer.push(255);
|
|
||||||
}
|
|
||||||
image_buffer.push(*i);
|
|
||||||
}
|
|
||||||
image_buffer.push(255);
|
|
||||||
} else {
|
|
||||||
image_buffer = img.raw_pixels();
|
|
||||||
}
|
|
||||||
|
|
||||||
(image_buffer, xy)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new(device: Arc<Device>, image_filename: String) -> ComputeImage {
|
|
||||||
|
|
||||||
let (image_buffer, xy) = ComputeImage::load_raw(image_filename);
|
|
||||||
|
|
||||||
let compute_graphics_swap_buffer = {
|
|
||||||
|
|
||||||
let mut usage = ImageUsage::none();
|
|
||||||
usage.transfer_destination = true;
|
|
||||||
usage.storage = true;
|
|
||||||
|
|
||||||
AttachmentImage::with_usage(
|
|
||||||
device.clone(),
|
|
||||||
[xy.0, xy.1],
|
|
||||||
Format::R8G8B8A8Uint,
|
|
||||||
usage)
|
|
||||||
};
|
|
||||||
|
|
||||||
let data_length = xy.0 * xy.1 * 4;
|
|
||||||
|
|
||||||
// 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 = image_buffer.iter();
|
|
||||||
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
|
|
||||||
CpuAccessibleBuffer::from_iter(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 = image_buffer.iter();
|
|
||||||
let data_iter = (0..data_length).map(|n| *(buff.next().unwrap()));
|
|
||||||
CpuAccessibleBuffer::from_iter(device.clone(), BufferUsage::all(), data_iter).unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// A buffer to hold many i32 values to use as settings
|
|
||||||
let settings_buffer = {
|
|
||||||
let vec = vec![xy.0, xy.1];
|
|
||||||
let mut buff = vec.iter();
|
|
||||||
let data_iter =
|
|
||||||
(0..2).map(|n| *(buff.next().unwrap()));
|
|
||||||
CpuAccessibleBuffer::from_iter(device.clone(),
|
|
||||||
BufferUsage::all(),
|
|
||||||
data_iter).unwrap()
|
|
||||||
};
|
|
||||||
|
|
||||||
ComputeImage{
|
|
||||||
device: device.clone(),
|
|
||||||
compute_graphics_swap_buffer: compute_graphics_swap_buffer.unwrap(),
|
|
||||||
|
|
||||||
rw_buffers: vec![write_buffer, read_buffer],
|
|
||||||
settings_buffer: settings_buffer
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_swap_buffer(&mut self) -> Arc<AttachmentImage> {
|
|
||||||
self.compute_graphics_swap_buffer.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_size(&self) -> (u32, u32) {
|
|
||||||
let xy = self.compute_graphics_swap_buffer.dimensions();
|
|
||||||
(xy[0], xy[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_read_buffer(&self) -> ImageBuffer<Rgba<u8>, Vec<u8>>{
|
|
||||||
|
|
||||||
let xy = self.get_size();
|
|
||||||
|
|
||||||
self.rw_buffers.get(0).unwrap().write().unwrap().map(|x| x);
|
|
||||||
let data_buffer_content = self.rw_buffers.get(0).unwrap().read().unwrap();
|
|
||||||
ImageBuffer::from_fn(xy.0, xy.1, |x, y| {
|
|
||||||
let r = data_buffer_content[((xy.0 * y + x) * 4 + 0) as usize] as u8;
|
|
||||||
let g = data_buffer_content[((xy.0 * y + x) * 4 + 1) as usize] as u8;
|
|
||||||
let b = data_buffer_content[((xy.0 * y + x) * 4 + 2) as usize] as u8;
|
|
||||||
let a = data_buffer_content[((xy.0 * y + x) * 4 + 3) as usize] as u8;
|
|
||||||
|
|
||||||
image::Rgba([r, g, b, a])
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn save_image(&self) {
|
|
||||||
self.read_read_buffer().save(format!("output/{}.jpg", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_descriptor_set(&self, compute_pipeline: std::sync::Arc<ComputePipeline<PipelineLayout<shade_runner::layouts::ComputeLayout>>>)
|
|
||||||
-> Arc<PersistentDescriptorSet<std::sync::Arc<ComputePipeline<PipelineLayout<shade_runner::layouts::ComputeLayout>>>, ((((),
|
|
||||||
PersistentDescriptorSetBuf<std::sync::Arc<vulkano::buffer::cpu_access::CpuAccessibleBuffer<[u8]>>>),
|
|
||||||
PersistentDescriptorSetBuf<std::sync::Arc<vulkano::buffer::cpu_access::CpuAccessibleBuffer<[u8]>>>),
|
|
||||||
PersistentDescriptorSetBuf<std::sync::Arc<vulkano::buffer::cpu_access::CpuAccessibleBuffer<[u32]>>>)>> {
|
|
||||||
|
|
||||||
Arc::new(PersistentDescriptorSet::start(compute_pipeline.clone(), 0)
|
|
||||||
.add_buffer(self.rw_buffers.get(0).unwrap().clone()).unwrap()
|
|
||||||
.add_buffer(self.rw_buffers.get(1).unwrap().clone()).unwrap()
|
|
||||||
.add_buffer(self.settings_buffer.clone()).unwrap()
|
|
||||||
.build().unwrap())
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,3 +1,43 @@
|
|||||||
pub mod compute_image;
|
use image::GenericImageView;
|
||||||
pub mod compute_kernel;
|
use std::sync::Arc;
|
||||||
|
use std::ffi::CStr;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub mod shader_kernels;
|
pub mod shader_kernels;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pub fn load_raw(filename: String) -> (Vec<u8>, (u32,u32)) {
|
||||||
|
|
||||||
|
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(filename.clone()));
|
||||||
|
|
||||||
|
let img = image::open(compute_path).expect("Couldn't find image");
|
||||||
|
|
||||||
|
let xy = img.dimensions();
|
||||||
|
|
||||||
|
let data_length = xy.0 * xy.1 * 4;
|
||||||
|
let pixel_count = img.raw_pixels().len();
|
||||||
|
|
||||||
|
let mut image_buffer = Vec::new();
|
||||||
|
|
||||||
|
if pixel_count != data_length as usize {
|
||||||
|
println!("Creating apha channel...");
|
||||||
|
for i in img.raw_pixels().iter() {
|
||||||
|
if (image_buffer.len() + 1) % 4 == 0 {
|
||||||
|
image_buffer.push(255);
|
||||||
|
}
|
||||||
|
image_buffer.push(*i);
|
||||||
|
}
|
||||||
|
image_buffer.push(255);
|
||||||
|
} else {
|
||||||
|
image_buffer = img.raw_pixels();
|
||||||
|
}
|
||||||
|
|
||||||
|
(image_buffer, xy)
|
||||||
|
}
|
Loading…
Reference in new issue