start working on render_graph v2

This commit is contained in:
Carter Anderson 2020-01-15 10:28:27 -08:00
parent 724ad16c95
commit 435357ee86
5 changed files with 137 additions and 7 deletions

View file

@ -34,12 +34,6 @@ impl Material {
}
}
#[repr(C)]
#[derive(Clone, Copy, AsBytes, FromBytes)]
pub struct RenderedUniforms {
pub transform: [[f32; 4]; 4],
}
#[repr(C)]
#[derive(Clone, Copy, AsBytes, FromBytes)]
pub struct MaterialUniforms {

View file

@ -7,6 +7,7 @@ pub mod shader;
mod light;
mod material;
mod render_graph;
mod render_graph_2;
mod vertex;
pub use camera::*;

View file

@ -0,0 +1,3 @@
mod pipeline;
pub use pipeline::*;

View file

@ -0,0 +1,91 @@
use crate::render::shader::ShaderStages;
pub struct VertexBufferDefinition {
pub stride: wgpu::BufferAddress,
pub step_mode: wgpu::InputStepMode,
pub attributes: Vec<wgpu::VertexAttributeDescriptor>,
}
impl<'a> Into<wgpu::VertexBufferDescriptor<'a>> for &'a VertexBufferDefinition {
fn into(self) -> wgpu::VertexBufferDescriptor<'a> {
wgpu::VertexBufferDescriptor {
step_mode: self.step_mode,
stride: self.stride,
attributes: &self.attributes,
}
}
}
pub struct PipelineDefinition {
pub shader_stages: ShaderStages,
pub rasterization_state: Option<wgpu::RasterizationStateDescriptor>,
/// The primitive topology used to interpret vertices.
pub primitive_topology: wgpu::PrimitiveTopology,
/// The effect of draw calls on the color aspect of the output target.
pub color_states: Vec<wgpu::ColorStateDescriptor>,
/// The effect of draw calls on the depth and stencil aspects of the output target, if any.
pub depth_stencil_state: Option<wgpu::DepthStencilStateDescriptor>,
/// The format of any index buffers used with this pipeline.
pub index_format: wgpu::IndexFormat,
/// The format of any vertex buffers used with this pipeline.
pub vertex_buffer_definitions: Vec<VertexBufferDefinition>,
/// The number of samples calculated per pixel (for MSAA).
pub sample_count: u32,
/// Bitmask that restricts the samples of a pixel modified by this pipeline.
pub sample_mask: u32,
/// When enabled, produces another sample mask per pixel based on the alpha output value, that
/// is ANDed with the sample_mask and the primitive coverage to restrict the set of samples
/// affected by a primitive.
/// The implicit mask produced for alpha of zero is guaranteed to be zero, and for alpha of one
/// is guaranteed to be all 1-s.
pub alpha_to_coverage_enabled: bool,
}
impl PipelineDefinition {
pub fn create_render_pipeline(&self, device: &wgpu::Device) -> wgpu::RenderPipeline {
let vertex_shader_module = self.shader_stages.vertex.create_shader_module(device);
let fragment_shader_module = match self.shader_stages.fragment {
Some(ref fragment_shader) => Some(fragment_shader.create_shader_module(device)),
None => None,
};
let pipeline_layout =
device
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
bind_group_layouts: &[],
});
let render_pipeline_descriptor = wgpu::RenderPipelineDescriptor {
layout: &pipeline_layout,
vertex_stage: wgpu::ProgrammableStageDescriptor {
module: &vertex_shader_module,
entry_point: &self.shader_stages.vertex.entry_point,
},
fragment_stage: match self.shader_stages.fragment {
Some(ref fragment_shader) => Some(wgpu::ProgrammableStageDescriptor {
entry_point: &fragment_shader.entry_point,
module: fragment_shader_module.as_ref().unwrap(),
}),
None => None,
},
rasterization_state: self.rasterization_state.clone(),
primitive_topology: self.primitive_topology,
color_states: &self.color_states,
depth_stencil_state: self.depth_stencil_state.clone(),
index_format: self.index_format,
vertex_buffers: &self.vertex_buffer_definitions.iter().map(|v| v.into()).collect::<Vec<wgpu::VertexBufferDescriptor>>(),
sample_count: self.sample_count,
sample_mask: self.sample_mask,
alpha_to_coverage_enabled: self.alpha_to_coverage_enabled,
};
device.create_render_pipeline(&render_pipeline_descriptor)
}
}

View file

@ -1,4 +1,6 @@
#[allow(dead_code)]
use std::marker::Copy;
#[derive(Copy, Clone)]
pub enum ShaderStage {
Vertex,
Fragment,
@ -24,4 +26,43 @@ pub fn glsl_to_spirv(glsl_source: &str, stage: ShaderStage) -> Vec<u32> {
"shader.glsl", "main", Some(&options)).unwrap();
binary_result.as_binary().into()
}
pub enum ShaderSource {
Spirv(Vec<u32>),
Glsl(String),
}
pub struct Shader {
pub source: ShaderSource,
pub stage: ShaderStage,
pub entry_point: String,
pub macros: Option<Vec<String>>,
}
impl Shader {
pub fn from_glsl(glsl: &str, stage: ShaderStage) -> Shader {
Shader {
source: ShaderSource::Glsl(glsl.to_string()),
entry_point: "main".to_string(),
macros: None,
stage,
}
}
pub fn get_spirv(&self) -> Vec<u32> {
match self.source {
ShaderSource::Spirv(ref bytes) => bytes.clone(),
ShaderSource::Glsl(ref source) => glsl_to_spirv(&source, self.stage),
}
}
pub fn create_shader_module(&self, device: &wgpu::Device) -> wgpu::ShaderModule {
device.create_shader_module(&self.get_spirv())
}
}
pub struct ShaderStages {
pub vertex: Shader,
pub fragment: Option<Shader>,
}