mirror of
https://gitlab.com/veloren/veloren.git
synced 2025-07-25 12:52:26 +00:00
255 lines
8.5 KiB
Rust
255 lines
8.5 KiB
Rust
use super::super::{
|
|
AaMode, ColLightInfo, FigureLayout, GlobalsLayouts, Renderer, TerrainLayout, TerrainVertex,
|
|
Texture,
|
|
};
|
|
use bytemuck::Pod;
|
|
use vek::*;
|
|
|
|
#[repr(C)]
|
|
#[derive(Copy, Clone, Debug, Pod)]
|
|
pub struct Locals {
|
|
shadow_matrices: [[f32; 4]; 4],
|
|
texture_mats: [[f32; 4]; 4],
|
|
}
|
|
|
|
impl Locals {
|
|
pub fn new(shadow_mat: Mat4<f32>, texture_mat: Mat4<f32>) -> Self {
|
|
Self {
|
|
shadow_matrices: shadow_mat.into_col_arrays(),
|
|
texture_mats: texture_mat.into_col_arrays(),
|
|
}
|
|
}
|
|
|
|
pub fn default() -> Self { Self::new(Mat4::identity(), Mat4::identity()) }
|
|
|
|
fn layout(device: &wgpu::Device) -> wgpu::BindGroupLayout {
|
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
label: None,
|
|
entries: &[wgpu::BindGroupLayoutEntry {
|
|
binding: 0,
|
|
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
|
|
ty: wgpu::BindingType::UniformBuffer {
|
|
dynamic: false,
|
|
min_binding_size: None,
|
|
},
|
|
count: None,
|
|
}],
|
|
})
|
|
}
|
|
}
|
|
|
|
pub struct ShadowLayout {
|
|
pub locals: wgpu::BindGroupLayout,
|
|
}
|
|
|
|
impl ShadowLayout {
|
|
pub fn new(device: &wgpu::Device) -> Self {
|
|
Self {
|
|
locals: Locals::layout(device),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn create_col_lights(
|
|
renderer: &mut Renderer,
|
|
(col_lights, col_lights_size): &ColLightInfo,
|
|
) -> Texture {
|
|
let mut texture_info = wgpu::TextureDescriptor {
|
|
label: None,
|
|
size: wgpu::Extent3d {
|
|
width: col_lights_size.x,
|
|
height: col_lights_size.y,
|
|
depth: 1,
|
|
},
|
|
mip_level_count: 1,
|
|
sample_count: 1,
|
|
dimension: wgpu::TextureDimension::D2,
|
|
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
|
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
|
|
};
|
|
|
|
let sampler_info = wgpu::SamplerDescriptor {
|
|
label: None,
|
|
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
|
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
|
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
|
mag_filter: wgpu::FilterMode::Linear,
|
|
min_filter: wgpu::FilterMode::Linear,
|
|
mipmap_filter: wgpu::FilterMode::Nearest,
|
|
border_color: Some(wgpu::SamplerBorderColor::TransparentBlack),
|
|
..Default::default()
|
|
};
|
|
|
|
renderer.create_texture_with_data_raw(
|
|
&texture_info,
|
|
&sampler_info,
|
|
col_lights_size.x * 4,
|
|
[col_lights_size.x, col_lights_size.y],
|
|
col_lights.as_bytes(),
|
|
)
|
|
}
|
|
|
|
pub struct ShadowFigurePipeline {
|
|
pub pipeline: wgpu::RenderPipeline,
|
|
}
|
|
|
|
impl ShadowFigurePipeline {
|
|
pub fn new(
|
|
device: &wgpu::Device,
|
|
vs_module: &wgpu::ShaderModule,
|
|
fs_module: &wgpu::ShaderModule,
|
|
sc_desc: &wgpu::SwapChainDescriptor,
|
|
global_layout: &GlobalsLayouts,
|
|
figure_layout: &FigureLayout,
|
|
layout: &ShadowLayout,
|
|
aa_mode: AaMode,
|
|
) -> Self {
|
|
let render_pipeline_layout =
|
|
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
label: Some("Shadow figure pipeline layout"),
|
|
push_constant_ranges: &[],
|
|
bind_group_layouts: &[
|
|
&global_layout.globals,
|
|
&figure_layout.locals,
|
|
&layout.locals,
|
|
],
|
|
});
|
|
|
|
let samples = match aa_mode {
|
|
AaMode::None | AaMode::Fxaa => 1,
|
|
// TODO: Ensure sampling in the shader is exactly between the 4 texels
|
|
AaMode::SsaaX4 => 1,
|
|
AaMode::MsaaX4 => 4,
|
|
AaMode::MsaaX8 => 8,
|
|
AaMode::MsaaX16 => 16,
|
|
};
|
|
|
|
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
label: Some("Shadow figure pipeline"),
|
|
layout: Some(&render_pipeline_layout),
|
|
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
|
module: vs_module,
|
|
entry_point: "main",
|
|
},
|
|
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
|
|
module: fs_module,
|
|
entry_point: "main",
|
|
}),
|
|
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
|
|
front_face: wgpu::FrontFace::Ccw,
|
|
cull_mode: wgpu::CullMode::Back,
|
|
clamp_depth: false,
|
|
depth_bias: 0,
|
|
depth_bias_slope_scale: 0.0,
|
|
depth_bias_clamp: 0.0,
|
|
}),
|
|
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
|
color_states: &[],
|
|
depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor {
|
|
format: wgpu::TextureFormat::Depth24Plus,
|
|
depth_write_enabled: true,
|
|
depth_compare: wgpu::CompareFunction::Less,
|
|
stencil: wgpu::StencilStateDescriptor {
|
|
front: wgpu::StencilStateFaceDescriptor::IGNORE,
|
|
back: wgpu::StencilStateFaceDescriptor::IGNORE,
|
|
read_mask: !0,
|
|
write_mask: !0,
|
|
},
|
|
}),
|
|
vertex_state: wgpu::VertexStateDescriptor {
|
|
index_format: wgpu::IndexFormat::Uint16,
|
|
vertex_buffers: &[TerrainVertex::desc()],
|
|
},
|
|
sample_count: samples,
|
|
sample_mask: !0,
|
|
alpha_to_coverage_enabled: false,
|
|
});
|
|
|
|
Self {
|
|
pipeline: render_pipeline,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct ShadowPipeline {
|
|
pub pipeline: wgpu::RenderPipeline,
|
|
}
|
|
|
|
impl ShadowPipeline {
|
|
pub fn new(
|
|
device: &wgpu::Device,
|
|
vs_module: &wgpu::ShaderModule,
|
|
fs_module: &wgpu::ShaderModule,
|
|
sc_desc: &wgpu::SwapChainDescriptor,
|
|
global_layout: &GlobalsLayouts,
|
|
terrain_layout: &TerrainLayout,
|
|
layout: &ShadowLayout,
|
|
aa_mode: AaMode,
|
|
) -> Self {
|
|
let render_pipeline_layout =
|
|
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
|
label: Some("Shadow pipeline layout"),
|
|
push_constant_ranges: &[],
|
|
bind_group_layouts: &[
|
|
&global_layout.globals,
|
|
&terrain_layout.locals,
|
|
&layout.locals,
|
|
],
|
|
});
|
|
|
|
let samples = match aa_mode {
|
|
AaMode::None | AaMode::Fxaa => 1,
|
|
// TODO: Ensure sampling in the shader is exactly between the 4 texels
|
|
AaMode::SsaaX4 => 1,
|
|
AaMode::MsaaX4 => 4,
|
|
AaMode::MsaaX8 => 8,
|
|
AaMode::MsaaX16 => 16,
|
|
};
|
|
|
|
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
|
label: Some("Shadow pipeline"),
|
|
layout: Some(&render_pipeline_layout),
|
|
vertex_stage: wgpu::ProgrammableStageDescriptor {
|
|
module: vs_module,
|
|
entry_point: "main",
|
|
},
|
|
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
|
|
module: fs_module,
|
|
entry_point: "main",
|
|
}),
|
|
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
|
|
front_face: wgpu::FrontFace::Ccw,
|
|
cull_mode: wgpu::CullMode::Back,
|
|
clamp_depth: false,
|
|
depth_bias: 0,
|
|
depth_bias_slope_scale: 0.0,
|
|
depth_bias_clamp: 0.0,
|
|
}),
|
|
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
|
color_states: &[],
|
|
depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor {
|
|
format: wgpu::TextureFormat::Depth24Plus,
|
|
depth_write_enabled: true,
|
|
depth_compare: wgpu::CompareFunction::Less,
|
|
stencil: wgpu::StencilStateDescriptor {
|
|
front: wgpu::StencilStateFaceDescriptor::IGNORE,
|
|
back: wgpu::StencilStateFaceDescriptor::IGNORE,
|
|
read_mask: !0,
|
|
write_mask: !0,
|
|
},
|
|
}),
|
|
vertex_state: wgpu::VertexStateDescriptor {
|
|
index_format: wgpu::IndexFormat::Uint16,
|
|
vertex_buffers: &[TerrainVertex::desc()],
|
|
},
|
|
sample_count: samples,
|
|
sample_mask: !0,
|
|
alpha_to_coverage_enabled: false,
|
|
});
|
|
|
|
Self {
|
|
pipeline: render_pipeline,
|
|
}
|
|
}
|
|
}
|