pulled swapchain out of shaderkernels

master
mitchellhansen 5 years ago
parent 28878dc345
commit bb1f782168

@ -8,14 +8,5 @@ layout(set = 0, binding = 0) uniform sampler2D tex;
layout(set = 0, binding = 1, rgba32ui) readonly uniform uimage2D img; layout(set = 0, binding = 1, rgba32ui) readonly uniform uimage2D img;
void main() { void main() {
f_color = out_color;
ivec2 pos = ivec2(gl_FragCoord.x, gl_FragCoord.y);
f_color = imageLoad(img, ivec2(pos)) / (255.0);
float gamma = 0.5;
f_color.rgb = pow(f_color.rgb, vec3(1.0/gamma));
//f_color = out_color;
// f_color = texture(tex, tex_coords);
} }

@ -7,7 +7,5 @@ layout(location = 1) out vec4 out_color;
void main() { void main() {
out_color = color; out_color = color;
gl_Position = vec4(position, 0.0, 1.0); gl_Position = vec4(position, 0.0, 1.0);
tex_coords = position;
} }

@ -0,0 +1,45 @@
// Canvas is the accumulator of Sprites for drawing
// Needs to know:
// textured?
// colored?
// vertices
/*
If it is textured. It needs to be rendered with the texture shader which requires a separate
graphics pipeline. Might as well have a new render pass as well.
Need to pull recreate swapchain out of shader_kernels.rs
I need a second version of shaderkernels
*/
trait Drawable {
fn draw() {
}
}
pub struct Canvas {
}
impl Canvas {
pub fn new() -> Canvas {
Canvas {
}
}
pub fn draw() ->
}

@ -42,7 +42,6 @@ use vulkano::descriptor::pipeline_layout::PipelineLayoutAbstract;
use vulkano::sync::GpuFuture; use vulkano::sync::GpuFuture;
use shaderc::CompileOptions; use shaderc::CompileOptions;
use shade_runner::CompileError; use shade_runner::CompileError;
use crate::workpiece::{WorkpieceLoader, Workpiece};
use winit::{EventsLoop, WindowBuilder, WindowEvent, Event, DeviceEvent, VirtualKeyCode, ElementState}; use winit::{EventsLoop, WindowBuilder, WindowEvent, Event, DeviceEvent, VirtualKeyCode, ElementState};
use winit::dpi::LogicalSize; use winit::dpi::LogicalSize;
use vulkano_win::VkSurfaceBuild; use vulkano_win::VkSurfaceBuild;
@ -54,7 +53,6 @@ mod input;
mod vkprocessor; mod vkprocessor;
mod util; mod util;
mod button; mod button;
mod workpiece;
mod vertex_2d; mod vertex_2d;
mod vertex_3d; mod vertex_3d;
mod sprite; mod sprite;
@ -76,6 +74,7 @@ fn main() {
processor.compile_kernel(String::from("simple-edge.compute")); processor.compile_kernel(String::from("simple-edge.compute"));
processor.compile_shaders(String::from("simple_texture"), &surface); processor.compile_shaders(String::from("simple_texture"), &surface);
processor.load_buffers(String::from("background.jpg")); processor.load_buffers(String::from("background.jpg"));
processor.create_swapchain(&surface);
let mut timer = Timer::new(); let mut timer = Timer::new();
let mut frame_future = Box::new(sync::now(processor.device.clone())) as Box<dyn GpuFuture>; let mut frame_future = Box::new(sync::now(processor.device.clone())) as Box<dyn GpuFuture>;

@ -125,10 +125,8 @@ pub struct VkProcessor<'a> {
pub image_buffer: Vec<u8>, pub image_buffer: Vec<u8>,
pub compute_image_buffers: Vec<Arc<CpuAccessibleBuffer<[u8]>>>, pub compute_image_buffers: Vec<Arc<CpuAccessibleBuffer<[u8]>>>,
pub settings_buffer: Option<Arc<CpuAccessibleBuffer<[u32]>>>, pub settings_buffer: Option<Arc<CpuAccessibleBuffer<[u32]>>>,
// pub swapchain: Option<Arc<Swapchain<Window>>>,
// pub images: Option<Vec<Arc<SwapchainImage<Window>>>>,
pub xy: (u32, u32), pub xy: (u32, u32),
pub render_pass: Option<Arc<RenderPassAbstract + Send + Sync>>,
pub vertex_buffer: Option<Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync + 'static)>>, pub vertex_buffer: Option<Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync + 'static)>>,
pub vertex_buffer2: Option<Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync + 'static)>>, pub vertex_buffer2: Option<Arc<(dyn BufferAccess + std::marker::Send + std::marker::Sync + 'static)>>,
pub dynamic_state: DynamicState, pub dynamic_state: DynamicState,
@ -139,8 +137,9 @@ pub struct VkProcessor<'a> {
pub image_buffer_store : Vec<Box<ImageAccess + Send + Sync>>, pub image_buffer_store : Vec<Box<ImageAccess + Send + Sync>>,
pub compute_image: Option<ComputeImage>, pub compute_image: Option<ComputeImage>,
pub swapchain: Option<Arc<Swapchain<Window>>>,
pub swapchain_images: Option<Vec<Arc<SwapchainImage<Window>>>>,
// pub image_buffers_obj : ImageBuffers,
} }
@ -183,7 +182,6 @@ impl<'a> VkProcessor<'a> {
compute_image_buffers: Vec::new(), compute_image_buffers: Vec::new(),
settings_buffer: Option::None, settings_buffer: Option::None,
xy: (0, 0), xy: (0, 0),
render_pass: Option::None,
vertex_buffer: Option::None, vertex_buffer: Option::None,
vertex_buffer2: None, vertex_buffer2: None,
@ -193,7 +191,10 @@ impl<'a> VkProcessor<'a> {
textures: vec![], textures: vec![],
image_buffer_store: vec![], image_buffer_store: vec![],
compute_image: None compute_image: None,
swapchain: None,
swapchain_images: None
} }
} }
@ -212,9 +213,64 @@ impl<'a> VkProcessor<'a> {
); );
} }
pub fn create_swapchain(&mut self, surface: &'a Arc<Surface<Window>>) {
let (mut swapchain, images) = {
let capabilities = surface.capabilities(self.physical).unwrap();
let usage = capabilities.supported_usage_flags;
let alpha = capabilities.supported_composite_alpha.iter().next().unwrap();
// Choosing the internal format that the images will have.
let format = capabilities.supported_formats[0].0;
// Set the swapchains window dimensions
let initial_dimensions = if let Some(dimensions) = surface.window().get_inner_size() {
// convert to physical pixels
let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into();
[dimensions.0, dimensions.1]
} else {
// The window no longer exists so exit the application.
panic!("window closed");
};
Swapchain::new(self.device.clone(),
surface.clone(),
capabilities.min_image_count,
format,
initial_dimensions,
1, // Layers
usage,
&self.queue,
SurfaceTransform::Identity,
alpha,
PresentMode::Fifo, true, None).unwrap()
};
self.swapchain = Some(swapchain);
self.swapchain_images = Some(images);
}
// On resizes we have to recreate the swapchain // On resizes we have to recreate the swapchain
pub fn recreate_swapchain(&mut self, surface: &'a Arc<Surface<Window>>) { pub fn recreate_swapchain(&mut self, surface: &'a Arc<Surface<Window>>) {
self.shader_kernels = Some(self.shader_kernels.take().unwrap().recreate_swapchain(surface));
let dimensions = if let Some(dimensions) = surface.window().get_inner_size() {
let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into();
[dimensions.0, dimensions.1]
} else {
return;
};
let (new_swapchain, new_images) = match self.swapchain.clone().unwrap().clone().recreate_with_dimension(dimensions) {
Ok(r) => r,
// This error tends to happen when the user is manually resizing the window.
// Simply restarting the loop is the easiest way to fix this issue.
Err(SwapchainCreationError::UnsupportedDimensions) => panic!("Uh oh"),
Err(err) => panic!("{:?}", err)
};
self.swapchain = Some(new_swapchain);
self.swapchain_images = Some(new_images);
} }
fn get_texture_from_file(image_filename: String, queue: Arc<Queue>) -> Arc<ImmutableImage<Format>> { fn get_texture_from_file(image_filename: String, queue: Arc<Queue>) -> Arc<ImmutableImage<Format>> {
@ -376,7 +432,7 @@ impl<'a> VkProcessor<'a> {
pub fn run(&mut self, surface: &'a Arc<Surface<Window>>, mut frame_future: Box<dyn GpuFuture>) -> Box<dyn GpuFuture> { 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.shader_kernels.clone().unwrap().swapchain_images.clone(), let mut framebuffers = window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone(),
self.shader_kernels.clone().unwrap().render_pass.clone(), self.shader_kernels.clone().unwrap().render_pass.clone(),
&mut self.dynamic_state); &mut self.dynamic_state);
@ -388,8 +444,8 @@ impl<'a> VkProcessor<'a> {
// Whenever the window resizes we need to recreate everything dependent on the window size. // 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. // In this example that includes the swapchain, the framebuffers and the dynamic state viewport.
if recreate_swapchain { if recreate_swapchain {
self.shader_kernels = Some(self.shader_kernels.clone().unwrap().recreate_swapchain(surface)); self.recreate_swapchain(surface);
framebuffers = window_size_dependent_setup(&self.shader_kernels.clone().unwrap().swapchain_images.clone(), framebuffers = window_size_dependent_setup(&self.swapchain_images.clone().unwrap().clone(),
self.shader_kernels.clone().unwrap().render_pass.clone(), self.shader_kernels.clone().unwrap().render_pass.clone(),
//self.render_pass.clone().unwrap().clone(), //self.render_pass.clone().unwrap().clone(),
&mut self.dynamic_state); &mut self.dynamic_state);
@ -398,7 +454,7 @@ impl<'a> VkProcessor<'a> {
// This function can block if no image is available. The parameter is an optional timeout // This function can block if no image is available. The parameter is an optional timeout
// after which the function call will return an error. // after which the function call will return an error.
let (image_num, acquire_future) = match vulkano::swapchain::acquire_next_image(self.shader_kernels.clone().unwrap().swapchain.clone(), None) { let (image_num, acquire_future) = match vulkano::swapchain::acquire_next_image(self.swapchain.clone().unwrap().clone(), None) {
Ok(r) => r, Ok(r) => r,
Err(AcquireError::OutOfDate) => { Err(AcquireError::OutOfDate) => {
recreate_swapchain = true; recreate_swapchain = true;
@ -450,7 +506,7 @@ impl<'a> VkProcessor<'a> {
// Wait on the previous frame, then execute the command buffer and present the image // Wait on the previous frame, then execute the command buffer and present the image
let future = frame_future.join(acquire_future) let future = frame_future.join(acquire_future)
.then_execute(self.queue.clone(), command_buffer).unwrap() .then_execute(self.queue.clone(), command_buffer).unwrap()
.then_swapchain_present(self.queue.clone(), self.shader_kernels.clone().unwrap().swapchain.clone(), image_num) .then_swapchain_present(self.queue.clone(), self.swapchain.clone().unwrap().clone(), image_num)
.then_signal_fence_and_flush(); .then_signal_fence_and_flush();
match future { match future {

@ -49,8 +49,8 @@ struct EntryPoint<'a> {
#[derive(Clone)] #[derive(Clone)]
pub struct ShaderKernels { pub struct ShaderKernels {
pub swapchain : Arc<Swapchain<Window>>, // pub swapchain : Arc<Swapchain<Window>>,
pub swapchain_images: Vec<Arc<SwapchainImage<Window>>>, // Surface which is drawn to // pub swapchain_images: Vec<Arc<SwapchainImage<Window>>>, // Surface which is drawn to
pub render_pass: Arc<RenderPassAbstract + Send + Sync>, pub render_pass: Arc<RenderPassAbstract + Send + Sync>,
pub graphics_pipeline: Option<Arc<GraphicsPipelineAbstract + Sync + Send>>, pub graphics_pipeline: Option<Arc<GraphicsPipelineAbstract + Sync + Send>>,
@ -93,26 +93,26 @@ impl ShaderKernels {
} }
// On resizes we have to recreate the swapchain // On resizes we have to recreate the swapchain
pub fn recreate_swapchain(mut self, surface: &Arc<Surface<Window>>) -> Self { // pub fn recreate_swapchain(mut self, surface: &Arc<Surface<Window>>) -> Self {
let dimensions = if let Some(dimensions) = surface.window().get_inner_size() { // let dimensions = if let Some(dimensions) = surface.window().get_inner_size() {
let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into(); // let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into();
[dimensions.0, dimensions.1] // [dimensions.0, dimensions.1]
} else { // } else {
return self; // return self;
}; // };
//
let (new_swapchain, new_images) = match self.swapchain.clone().recreate_with_dimension(dimensions) { // let (new_swapchain, new_images) = match self.swapchain.clone().recreate_with_dimension(dimensions) {
Ok(r) => r, // Ok(r) => r,
// This error tends to happen when the user is manually resizing the window. // // This error tends to happen when the user is manually resizing the window.
// Simply restarting the loop is the easiest way to fix this issue. // // Simply restarting the loop is the easiest way to fix this issue.
Err(SwapchainCreationError::UnsupportedDimensions) => panic!("Uh oh"), // Err(SwapchainCreationError::UnsupportedDimensions) => panic!("Uh oh"),
Err(err) => panic!("{:?}", err) // Err(err) => panic!("{:?}", err)
}; // };
//
self.swapchain = new_swapchain; // self.swapchain = new_swapchain;
self.swapchain_images = new_images; // self.swapchain_images = new_images;
self // self
} // }
pub fn new(filename: String, pub fn new(filename: String,
surface: &Arc<Surface<Window>>, surface: &Arc<Surface<Window>>,
@ -120,36 +120,39 @@ impl ShaderKernels {
physical: PhysicalDevice, physical: PhysicalDevice,
device: Arc<Device>) -> ShaderKernels { device: Arc<Device>) -> ShaderKernels {
let (mut swapchain, images) = { // let (mut swapchain, images) = {
// let capabilities = surface.capabilities(physical).unwrap();
// let usage = capabilities.supported_usage_flags;
// let alpha = capabilities.supported_composite_alpha.iter().next().unwrap();
// // Choosing the internal format that the images will have.
// let format = capabilities.supported_formats[0].0;
//
// // Set the swapchains window dimensions
// let initial_dimensions = if let Some(dimensions) = surface.window().get_inner_size() {
// // convert to physical pixels
// let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into();
// [dimensions.0, dimensions.1]
// } else {
// // The window no longer exists so exit the application.
// panic!("window closed");
// };
//
// Swapchain::new(device.clone(),
// surface.clone(),
// capabilities.min_image_count,
// format,
// initial_dimensions,
// 1, // Layers
// usage,
// &queue,
// SurfaceTransform::Identity,
// alpha,
// PresentMode::Fifo, true, None).unwrap()
// };
let capabilities = surface.capabilities(physical).unwrap(); let capabilities = surface.capabilities(physical).unwrap();
let usage = capabilities.supported_usage_flags;
let alpha = capabilities.supported_composite_alpha.iter().next().unwrap();
// Choosing the internal format that the images will have.
let format = capabilities.supported_formats[0].0; let format = capabilities.supported_formats[0].0;
// Set the swapchains window dimensions
let initial_dimensions = if let Some(dimensions) = surface.window().get_inner_size() {
// convert to physical pixels
let dimensions: (u32, u32) = dimensions.to_physical(surface.window().get_hidpi_factor()).into();
[dimensions.0, dimensions.1]
} else {
// The window no longer exists so exit the application.
panic!("window closed");
};
Swapchain::new(device.clone(),
surface.clone(),
capabilities.min_image_count,
format,
initial_dimensions,
1, // Layers
usage,
&queue,
SurfaceTransform::Identity,
alpha,
PresentMode::Fifo, true, None).unwrap()
};
let filenames = ShaderKernels::get_path(filename.clone()); let filenames = ShaderKernels::get_path(filename.clone());
// TODO: better compile message, run til successful compile // TODO: better compile message, run til successful compile
@ -208,7 +211,7 @@ impl ShaderKernels {
// be one of the types of the `vulkano::format` module (or alternatively one // be one of the types of the `vulkano::format` module (or alternatively one
// of your structs that implements the `FormatDesc` trait). Here we use the // of your structs that implements the `FormatDesc` trait). Here we use the
// same format as the swapchain. // same format as the swapchain.
format: swapchain.clone().format(), format: format,
// TODO: // TODO:
samples: 1, samples: 1,
} }
@ -224,8 +227,8 @@ impl ShaderKernels {
ShaderKernels { ShaderKernels {
swapchain: swapchain, // swapchain: swapchain,
swapchain_images: images, // swapchain_images: images,
//physical: physical, //physical: physical,
//options: CompileOptions::new().ok_or(CompileError::CreateCompiler).unwrap(), //options: CompileOptions::new().ok_or(CompileError::CreateCompiler).unwrap(),

Loading…
Cancel
Save