cleaning up the shader hoohaw

This commit is contained in:
2019-08-05 22:40:28 -07:00
parent bb1f782168
commit 56455774bc
4 changed files with 128 additions and 216 deletions

View File

@@ -36,27 +36,21 @@ use vulkano::image::{Dimensions, ImageUsage};
use vulkano::format::Format;
use vulkano::sampler::{Sampler, Filter, MipmapMode, SamplerAddressMode};
use image::flat::NormalForm::ColumnMajorPacked;
use crate::vkprocessor::SimpleSpecializationConstants;
use crate::vertex_2d::ColoredVertex2D;
struct EntryPoint<'a> {
compiled_shaders: CompiledShaders,
frag_entry_point: Option<GraphicsEntryPoint<'a, SimpleSpecializationConstants, FragInput, FragOutput, FragLayout>>,
vertex_entry_point: Option<GraphicsEntryPoint<'a, SimpleSpecializationConstants, VertInput, VertOutput, VertLayout>>,
vertex_shader_module: Arc<ShaderModule>,
fragment_shader_module: Arc<ShaderModule>,
}
/*
Shaderkernel holds the pipeline and render pass for the inputted shader source
*/
#[derive(Clone)]
pub struct ShaderKernels {
// pub swapchain : Arc<Swapchain<Window>>,
// pub swapchain_images: Vec<Arc<SwapchainImage<Window>>>, // Surface which is drawn to
pub render_pass: Arc<RenderPassAbstract + Send + Sync>,
pub graphics_pipeline: Option<Arc<GraphicsPipelineAbstract + Sync + Send>>,
graphics_pipeline: Option<Arc<GraphicsPipelineAbstract + Sync + Send>>,
device: Arc<Device>,
}
impl ShaderKernels {
@@ -82,74 +76,15 @@ impl ShaderKernels {
}
pub fn get_pipeline(&mut self) -> Arc<GraphicsPipelineAbstract + Sync + Send> {
match self.graphics_pipeline.clone() {
Some(t) => t,
None => {
// TODO: Create new graphics pipeline
self.graphics_pipeline.clone().unwrap()
}
}
self.graphics_pipeline.clone().unwrap()
}
// On resizes we have to recreate the swapchain
// pub fn recreate_swapchain(mut self, surface: &Arc<Surface<Window>>) -> Self {
// 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 self;
// };
//
// let (new_swapchain, new_images) = match self.swapchain.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 = new_swapchain;
// self.swapchain_images = new_images;
// self
// }
pub fn new(filename: String,
surface: &Arc<Surface<Window>>,
queue: Arc<Queue>,
physical: PhysicalDevice,
device: Arc<Device>) -> ShaderKernels {
// 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 format = capabilities.supported_formats[0].0;
@@ -225,33 +160,23 @@ impl ShaderKernels {
).unwrap());
ShaderKernels {
// swapchain: swapchain,
// swapchain_images: images,
//physical: physical,
//options: CompileOptions::new().ok_or(CompileError::CreateCompiler).unwrap(),
graphics_pipeline: Some(Arc::new(GraphicsPipeline::start()
// We need to indicate the layout of the vertices.
// 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::<ColoredVertex2D>()
// 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.
.vertex_shader(vertex_entry_point.clone().unwrap(), SimpleSpecializationConstants {
.vertex_shader(vertex_entry_point.clone().unwrap(), ShaderSpecializationConstants {
first_constant: 0,
second_constant: 0,
third_constant: 0.0,
})
// The content of the vertex buffer describes a list of triangles.
.triangle_fan()
// Use a resizable viewport set to draw over the entire window
.viewports_dynamic_scissors_irrelevant(1)
// See `vertex_shader`.
.fragment_shader(frag_entry_point.clone().unwrap(), SimpleSpecializationConstants {
.fragment_shader(frag_entry_point.clone().unwrap(), ShaderSpecializationConstants {
first_constant: 0,
second_constant: 0,
third_constant: 0.0,
@@ -259,7 +184,7 @@ impl ShaderKernels {
// We have to indicate which subpass of which render pass this pipeline is going to be used
// in. The pipeline will only be usable from this particular subpass.
.render_pass(Subpass::from(render_pass.clone(), 0).unwrap())
// Now that our builder is filled, we call `build()` to obtain an actual pipeline.
.build(device.clone())
.unwrap())),
@@ -268,4 +193,37 @@ impl ShaderKernels {
}
}
}
#[repr(C)]
#[derive(Default, Debug, Clone)]
// TODO: This needs to be duplicated and moved into their respective containers shaderkenrels copute
struct ShaderSpecializationConstants {
first_constant: i32,
second_constant: u32,
third_constant: f32,
}
unsafe impl SpecializationConstants for ShaderSpecializationConstants {
fn descriptors() -> &'static [SpecializationMapEntry] {
static DESCRIPTORS: [SpecializationMapEntry; 3] = [
SpecializationMapEntry {
constant_id: 0,
offset: 0,
size: 4,
},
SpecializationMapEntry {
constant_id: 1,
offset: 4,
size: 4,
},
SpecializationMapEntry {
constant_id: 2,
offset: 8,
size: 4,
},
];
&DESCRIPTORS
}
}