added texture. looks like copying the one from the compute output isn't going to totally work out of the box

This commit is contained in:
2019-07-16 22:33:05 -07:00
parent 5751965ce3
commit 937b4fc8ca
5 changed files with 361 additions and 39 deletions

View File

@@ -16,7 +16,7 @@ use image::GenericImageView;
use vulkano::descriptor::pipeline_layout::PipelineLayout;
use image::GenericImage;
use shade_runner::{ComputeLayout, CompileError, FragLayout, FragInput, FragOutput, VertInput, VertOutput, VertLayout};
use vulkano::descriptor::descriptor_set::PersistentDescriptorSetBuf;
use vulkano::descriptor::descriptor_set::{PersistentDescriptorSetBuf, PersistentDescriptorSetImg, PersistentDescriptorSetSampler};
use shaderc::CompileOptions;
use vulkano::framebuffer::{Subpass, RenderPass, RenderPassAbstract, Framebuffer, FramebufferAbstract};
use vulkano::pipeline::shader::{GraphicsShaderType, ShaderModule, GraphicsEntryPoint, SpecializationConstants, SpecializationMapEntry};
@@ -29,7 +29,11 @@ use vulkano::pipeline::vertex::{SingleBufferDefinition, Vertex};
use vulkano::descriptor::PipelineLayoutAbstract;
use std::alloc::Layout;
use vulkano::pipeline::viewport::Viewport;
use image::ImageFormat;
use vulkano::image::immutable::ImmutableImage;
use vulkano::image::Dimensions;
use vulkano::format::Format;
use vulkano::sampler::{Sampler, Filter, MipmapMode, SamplerAddressMode};
#[derive(Default, Debug, Clone)]
struct tVertex { position: [f32; 2] }
@@ -93,12 +97,14 @@ unsafe impl SpecializationConstants for MySpecConstants {
pub struct VkProcessor<'a> {
pub instance: Arc<Instance>,
pub physical: PhysicalDevice<'a>,
pub pipeline: Option<Arc<GraphicsPipelineAbstract + Sync + Send>>,
pub graphics_pipeline: Option<Arc<GraphicsPipelineAbstract + Sync + Send>>,
pub compute_pipeline: Option<std::sync::Arc<ComputePipeline<PipelineLayout<shade_runner::layouts::ComputeLayout>>>>,
pub device: Arc<Device>,
pub queues: QueuesIter,
pub queue: Arc<Queue>,
pub set: Option<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]>>>)>>>,
pub compute_set: Option<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]>>>)>>>,
pub img_set: Option<Arc<PersistentDescriptorSet<Arc<dyn GraphicsPipelineAbstract + Send + Sync>, (((), PersistentDescriptorSetImg<Arc<ImmutableImage<Format>>>), PersistentDescriptorSetSampler)>>>,
pub graphics_image_buffer: Option<Arc<ImmutableImage<Format>>>,
pub image_buffer: Vec<u8>,
pub img_buffers: Vec<Arc<CpuAccessibleBuffer<[u8]>>>,
pub settings_buffer: Option<Arc<CpuAccessibleBuffer<[u32]>>>,
@@ -108,10 +114,11 @@ pub struct VkProcessor<'a> {
pub render_pass: Option<Arc<RenderPassAbstract + Send + Sync>>,
pub vertex_buffer: Option<Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync + 'static)>>,
pub dynamic_state: DynamicState,
pub previous_frame: Box<dyn GpuFuture>,
}
impl<'a> VkProcessor<'a> {
pub fn new(instance: &'a Arc<Instance>, surface: &'a Arc<Surface<Window>>) -> VkProcessor<'a> {
let physical = PhysicalDevice::enumerate(instance).next().unwrap();
@@ -133,12 +140,14 @@ impl<'a> VkProcessor<'a> {
VkProcessor {
instance: instance.clone(),
physical: physical.clone(),
pipeline: Option::None,
graphics_pipeline: Option::None,
compute_pipeline: Option::None,
device: device.clone(),
queue: queue,
queues: queues,
set: Option::None,
compute_set: Option::None,
img_set: Option::None,
graphics_image_buffer: None,
image_buffer: Vec::new(),
img_buffers: Vec::new(),
settings_buffer: Option::None,
@@ -148,7 +157,6 @@ impl<'a> VkProcessor<'a> {
render_pass: Option::None,
vertex_buffer: Option::None,
dynamic_state: DynamicState { line_width: None, viewports: None, scissors: None },
previous_frame: Box::new(sync::now(device.clone())) as Box<dyn GpuFuture>,
}
}
@@ -316,6 +324,25 @@ 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 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()
@@ -323,6 +350,7 @@ impl<'a> VkProcessor<'a> {
// The type `SingleBufferDefinition` actually contains a template parameter corresponding
// to the type of each vertex. But in this code it is automatically inferred.
.vertex_input_single_buffer::<tVertex>()
// A Vulkan shader can in theory contain multiple entry points, so we have to specify
// which one. The `main` word of `main_entry_point` actually corresponds to the name of
// the entry point.
@@ -332,7 +360,7 @@ impl<'a> VkProcessor<'a> {
floating_point: 0.0,
})
// The content of the vertex buffer describes a list of triangles.
.triangle_list()
.triangle_fan()
// Use a resizable viewport set to draw over the entire window
.viewports_dynamic_scissors_irrelevant(1)
// See `vertex_shader`.
@@ -349,7 +377,13 @@ impl<'a> VkProcessor<'a> {
.unwrap();
self.pipeline = Option::Some(Arc::new(pipeline));
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()
.build().unwrap()));
self.graphics_image_buffer = Some(texture.clone());
}
@@ -432,12 +466,15 @@ impl<'a> VkProcessor<'a> {
.dispatch([self.xy.0, self.xy.1, 1],
self.compute_pipeline.clone().unwrap().clone(),
self.set.clone().unwrap().clone(), ()).unwrap()
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.pipeline.clone().unwrap().clone(), &self.dynamic_state, v, (), ())
.draw(self.graphics_pipeline.clone().unwrap().clone(),
&self.dynamic_state, v,
self.img_set.clone().unwrap().clone(), ())
.unwrap()
.end_render_pass()
@@ -539,7 +576,7 @@ impl<'a> VkProcessor<'a> {
.add_buffer(read_buffer.clone()).unwrap()
.add_buffer(settings_buffer.clone()).unwrap();
self.set = Some(Arc::new(set.build().unwrap()));
self.compute_set = Some(Arc::new(set.build().unwrap()));
self.img_buffers.push(write_buffer);
self.img_buffers.push(read_buffer);
@@ -551,37 +588,16 @@ impl<'a> VkProcessor<'a> {
vulkano::impl_vertex!(tVertex, position);
CpuAccessibleBuffer::from_iter(self.device.clone(), BufferUsage::all(), [
tVertex { position: [-0.5, -0.25] },
tVertex { position: [0.0, 0.5] },
tVertex { position: [0.25, -0.1] }
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 run_kernel(&mut self) {
//
// println!("Running Kernel...");
//
// // The command buffer I think pretty much serves to define what runs where for how many times
// 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.set.clone().unwrap().clone(), ()).unwrap()
// .build().unwrap();
//
// // Create a future for running the command buffer and then just fence it
// let future = sync::now(self.device.clone())
// .then_execute(self.queue.clone(), command_buffer).unwrap()
// .then_signal_fence_and_flush().unwrap();
//
// // I think this is redundant and returns immediately
// future.wait(None).unwrap();
// println!("Done running kernel");
// }
// pub fn read_image(&self) -> Vec<u8> {
//
// // The buffer is sync'd so we can just read straight from the handle