#[derive(Default)]
pub struct WgpuShader {
pub vertex_module: Option<wgpu::ShaderModule>,
pub fragment_module: Option<wgpu::ShaderModule>,
pub pipeline_layout: Option<wgpu::PipelineLayout>,
pub bind_group_layout: Option<wgpu::BindGroupLayout>,
}
impl WgpuShader {
pub fn new() -> Self {
Self::default()
}
pub fn compile(
device: &wgpu::Device,
source: &str,
label: Option<&str>,
) -> wgpu::ShaderModule {
device.create_shader_module(wgpu::ShaderModuleDescriptor {
label,
source: wgpu::ShaderSource::Wgsl(source.into()),
})
}
pub fn create_pipeline(
device: &wgpu::Device,
vertex_module: &wgpu::ShaderModule,
fragment_module: &wgpu::ShaderModule,
vertex_layout: &wgpu::VertexBufferLayout,
bind_group_layouts: &[&wgpu::BindGroupLayout],
surface_format: wgpu::TextureFormat,
) -> wgpu::RenderPipeline {
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("RustPixel Pipeline Layout"),
bind_group_layouts,
push_constant_ranges: &[],
});
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("RustPixel Render Pipeline"),
layout: Some(&pipeline_layout),
cache: None,
vertex: wgpu::VertexState {
module: vertex_module,
entry_point: Some("vs_main"),
buffers: std::slice::from_ref(vertex_layout),
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: Some(wgpu::FragmentState {
module: fragment_module,
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: surface_format,
blend: Some(wgpu::BlendState {
color: wgpu::BlendComponent::REPLACE,
alpha: wgpu::BlendComponent::REPLACE,
}),
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: wgpu::PipelineCompilationOptions::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: Some(wgpu::Face::Back),
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: 1,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview: None,
})
}
}