use crate::shaders::shader_source;
#[derive(Clone, Copy, Debug)]
pub enum Resource {
ConstantBuffer,
Texture,
FilterableTexture,
DepthTexture,
UintTexture,
Sampler { filtering: bool },
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DepthStencilStateDesc {
DisableDepth,
WriteAlways,
EnableDepthNoWrites,
}
pub struct Shader {
pub module: wgpu::ShaderModule,
pub entry_point: &'static str,
}
pub fn create_shader(
device: &wgpu::Device,
file_name: &str,
entry_point: &'static str,
macros: &[(&str, &str)],
) -> Shader {
Shader {
module: device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some(entry_point),
source: wgpu::ShaderSource::Wgsl(shader_source(file_name, macros).into()),
}),
entry_point,
}
}
#[derive(Default)]
pub struct PostFXRenderTechnique {
pub pso: Option<wgpu::RenderPipeline>,
pub layout: Option<wgpu::BindGroupLayout>,
}
impl PostFXRenderTechnique {
#[allow(clippy::too_many_arguments)]
pub fn initialize_pso(
&mut self,
device: &wgpu::Device,
pso_name: &str,
vertex_shader: &Shader,
pixel_shader: &Shader,
resource_layout: &[(u32, Resource)],
rtv_fmts: &[wgpu::TextureFormat],
dsv_fmt: Option<wgpu::TextureFormat>,
dss_desc: DepthStencilStateDesc,
) {
let entries: Vec<_> = resource_layout
.iter()
.map(|&(binding, resource)| wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: match resource {
Resource::ConstantBuffer => wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
Resource::Texture | Resource::FilterableTexture => wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float {
filterable: matches!(resource, Resource::FilterableTexture),
},
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
Resource::DepthTexture => wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Depth,
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
Resource::UintTexture => wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Uint,
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
Resource::Sampler { filtering } => wgpu::BindingType::Sampler(if filtering {
wgpu::SamplerBindingType::Filtering
} else {
wgpu::SamplerBindingType::NonFiltering
}),
},
count: None,
})
.collect();
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some(pso_name),
entries: &entries,
});
let targets: Vec<_> = rtv_fmts
.iter()
.map(|&format| {
Some(wgpu::ColorTargetState {
format,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})
})
.collect();
let pso = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some(pso_name),
layout: Some(
&device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some(pso_name),
bind_group_layouts: &[Some(&layout)],
immediate_size: 0,
}),
),
vertex: wgpu::VertexState {
module: &vertex_shader.module,
entry_point: Some(vertex_shader.entry_point),
compilation_options: Default::default(),
buffers: &[],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleStrip,
front_face: wgpu::FrontFace::Cw,
cull_mode: Some(wgpu::Face::Back),
..Default::default()
},
depth_stencil: dsv_fmt.map(|format| {
let (write, compare) = match dss_desc {
DepthStencilStateDesc::DisableDepth => (false, wgpu::CompareFunction::Always),
DepthStencilStateDesc::WriteAlways => (true, wgpu::CompareFunction::Always),
DepthStencilStateDesc::EnableDepthNoWrites => {
(false, wgpu::CompareFunction::Less)
}
};
wgpu::DepthStencilState {
format,
depth_write_enabled: Some(write),
depth_compare: Some(compare),
stencil: Default::default(),
bias: Default::default(),
}
}),
multisample: Default::default(),
fragment: Some(wgpu::FragmentState {
module: &pixel_shader.module,
entry_point: Some(pixel_shader.entry_point),
compilation_options: Default::default(),
targets: &targets,
}),
multiview_mask: None,
cache: None,
});
self.pso = Some(pso);
self.layout = Some(layout);
}
pub fn is_initialized_pso(&self) -> bool {
self.pso.is_some()
}
pub fn bind_group(
&self,
device: &wgpu::Device,
label: &str,
entries: &[wgpu::BindGroupEntry<'_>],
) -> wgpu::BindGroup {
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some(label),
layout: self.layout.as_ref().expect("initialized technique"),
entries,
})
}
}