use std::{
cell::{Cell, OnceCell},
sync::{Arc, Weak},
};
use bytemuck::{Pod, Zeroable};
use cranpose_ui_graphics::{
Rect,
space::{Light, Mat4, Material, Mesh, Vec3, Volume},
};
use crate::{
collect::SceneDraw,
fixed_pipeline::FixedPipeline,
frame_graph::{FrameCommandRecorder, FrameCommandStats, UploadAllocatorSpec},
idle_pool::{IDLE_FRAMES, IdlePool},
lazy_resource::LazyGpuResource,
pipeline_compiler::{CompileLane, CompilerSend, PipelineCompiler},
pipeline_recorder::PipelineRecorder,
render::{DEPTH_FORMAT, create_render_pipeline_logged},
shader_cache::create_volume_shader_module,
shaders,
shared_shader::SharedShader,
};
const KEPT_MESHES: usize = 64;
pub(crate) const SCENE_DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth24PlusStencil8;
const SHADOW_MAP_SIZE: u32 = 1024;
const SHADOW_DEPTH_BIAS: f32 = 0.003;
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
struct SceneVertex {
position: [f32; 3],
normal: [f32; 3],
uv: [f32; 2],
}
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
pub(crate) struct SceneSurfaceUniforms {
clip_from_model: [[f32; 4]; 4],
scene_from_model: [[f32; 4]; 4],
light_clip_from_model: [[f32; 4]; 4],
clip_from_scene: [[f32; 4]; 4],
normal_rows: [[f32; 4]; 3],
eye: [f32; 4],
light: [f32; 4],
source_region: [f32; 4],
params: [f32; 4],
shadow: [f32; 4],
shadow_soft: [f32; 4],
glass: [f32; 4],
glass_tint: [f32; 4],
backdrop_region: [f32; 4],
clip: [f32; 4],
}
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
pub(crate) struct SceneVolumeUniforms {
clip_from_model: [[f32; 4]; 4],
eye: [f32; 4],
light: [f32; 4],
footprint: [f32; 4],
low: [f32; 4],
high: [f32; 4],
params: [[f32; 4]; Volume::PARAMS / 4],
clip: [f32; 4],
source_region: [f32; 4],
}
#[derive(Clone)]
pub(crate) struct SceneMeshBuffers {
pub(crate) vertices: wgpu::Buffer,
pub(crate) indices: wgpu::Buffer,
pub(crate) index_count: u32,
}
struct KeptMesh {
id: u64,
mesh: Weak<Mesh>,
buffers: SceneMeshBuffers,
}
pub(crate) struct ScenePipelineParts {
shader: SharedShader,
pub(crate) uniform_layout: wgpu::BindGroupLayout,
shadow_layout: wgpu::BindGroupLayout,
shadow_samplers: ShadowSamplers,
pub(crate) no_shadow: wgpu::BindGroup,
volume_layout: wgpu::PipelineLayout,
}
pub(crate) struct VolumeShader {
source: Weak<str>,
module: LazyGpuResource<Option<wgpu::ShaderModule>>,
pipelines: [VolumePipeline; 4],
}
impl VolumeShader {
pub(crate) fn pipeline(&self, variant: VolumeVariant) -> &VolumePipeline {
&self.pipelines[variant as usize]
}
}
pub(crate) struct VolumePipeline {
resource: LazyGpuResource<Option<wgpu::RenderPipeline>>,
queued: Cell<bool>,
}
impl VolumePipeline {
fn new(label: &'static str) -> Self {
Self {
resource: LazyGpuResource::new(label),
queued: Cell::new(false),
}
}
pub(crate) fn when_ready<J>(
&self,
(recorder, compiler, backend): (&PipelineRecorder, &PipelineCompiler, wgpu::Backend),
job: impl FnOnce() -> J,
) -> Option<&Option<wgpu::RenderPipeline>>
where
J: FnOnce() -> Option<wgpu::RenderPipeline> + CompilerSend + 'static,
{
if let Some(built) = self.resource.get() {
return Some(built);
}
if !compiler.is_active() {
return Some(self.resource.for_draw(recorder, backend, job()));
}
if !self.queued.replace(true) {
self.resource
.queue(compiler, CompileLane::Demanded, backend, job());
}
None
}
}
struct ShadowSamplers {
compare: wgpu::Sampler,
depth: wgpu::Sampler,
}
pub(crate) struct ShadowMap {
pub(crate) view: wgpu::TextureView,
pub(crate) group: wgpu::BindGroup,
}
pub(crate) struct ScenePass {
parts: OnceCell<ScenePipelineParts>,
pipelines: [FixedPipeline; ScenePipeline::ALL.len()],
window: OnceCell<Arc<Mesh>>,
meshes: IdlePool<KeptMesh>,
volumes: Vec<VolumeShader>,
pub(crate) shadow_map: Option<ShadowMap>,
shadow_map_idle: u64,
}
impl Default for ScenePass {
fn default() -> Self {
Self {
parts: OnceCell::new(),
pipelines: ScenePipeline::ALL.map(|kind| FixedPipeline::new(kind.record_label())),
window: OnceCell::new(),
meshes: IdlePool::default(),
volumes: Vec::new(),
shadow_map: None,
shadow_map_idle: 0,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum ScenePipeline {
Plain,
Shadowed,
Shadow,
Glass,
Glow,
Depth,
WindowMark,
WindowClear,
WindowRestore,
Portal,
PortalGlow,
PortalDepth,
PortalGlass,
}
impl ScenePipeline {
pub(crate) const ALL: [Self; 13] = [
Self::Plain,
Self::Shadowed,
Self::Shadow,
Self::Glass,
Self::Glow,
Self::Depth,
Self::WindowMark,
Self::WindowClear,
Self::WindowRestore,
Self::Portal,
Self::PortalGlow,
Self::PortalDepth,
Self::PortalGlass,
];
fn record_label(self) -> &'static str {
match self {
Self::Plain => "scene/mesh-surface",
Self::Shadowed => "scene/mesh-surface-shadowed",
Self::Shadow => "scene/mesh-shadow",
Self::Glass => "scene/mesh-glass",
Self::Glow => "scene/mesh-glow",
Self::Depth => "scene/mesh-depth",
Self::WindowMark => "scene/window-mark",
Self::WindowClear => "scene/window-clear",
Self::WindowRestore => "scene/window-restore",
Self::Portal => "scene/mesh-portal",
Self::PortalGlow => "scene/mesh-portal-glow",
Self::PortalDepth => "scene/mesh-portal-depth",
Self::PortalGlass => "scene/mesh-portal-glass",
}
}
fn spec(self) -> PipelineSpec {
let (label, entry_points) = self.shader();
let (color, depth, stencil) = self.targets();
PipelineSpec {
label,
entry_points,
color,
depth,
stencil,
}
}
fn shader(self) -> (&'static str, (&'static str, &'static str)) {
match self {
Self::Plain => (
"Scene Surface Pipeline",
("mesh_surface_vs", "mesh_surface_fs"),
),
Self::Shadowed => (
"Scene Shadowed Surface Pipeline",
("mesh_surface_vs", "mesh_surface_shadowed_fs"),
),
Self::Shadow => (
"Scene Shadow Pipeline",
("mesh_shadow_vs", "mesh_shadow_fs"),
),
Self::Glass => ("Scene Glass Pipeline", ("mesh_surface_vs", "mesh_glass_fs")),
Self::Glow => (
"Scene Glow Pipeline",
("mesh_surface_vs", "mesh_surface_fs"),
),
Self::Depth => ("Scene Depth Pipeline", ("mesh_surface_vs", "mesh_depth_fs")),
Self::WindowMark => (
"Scene Window Mark Pipeline",
("mesh_surface_vs", "mesh_depth_fs"),
),
Self::WindowClear => (
"Scene Window Clear Pipeline",
("mesh_surface_vs", "mesh_window_far_fs"),
),
Self::WindowRestore => (
"Scene Window Restore Pipeline",
("mesh_surface_vs", "mesh_depth_fs"),
),
Self::Portal => (
"Scene Portal Surface Pipeline",
("mesh_surface_vs", "mesh_portal_surface_fs"),
),
Self::PortalGlow => (
"Scene Portal Glow Pipeline",
("mesh_surface_vs", "mesh_portal_surface_fs"),
),
Self::PortalDepth => (
"Scene Portal Depth Pipeline",
("mesh_surface_vs", "mesh_portal_depth_fs"),
),
Self::PortalGlass => (
"Scene Portal Glass Pipeline",
("mesh_surface_vs", "mesh_portal_glass_fs"),
),
}
}
fn targets(self) -> (Option<Blend>, Depth, Stencil) {
match self {
Self::Plain | Self::Shadowed | Self::Glass => {
(Some(Blend::Over), Depth::Tested, Stencil::Ignored)
}
Self::Shadow => (None, Depth::Tested, Stencil::Ignored),
Self::Glow => (Some(Blend::Add), Depth::Read, Stencil::Ignored),
Self::Depth => (None, Depth::Tested, Stencil::Ignored),
Self::WindowMark => (None, Depth::Read, Stencil::Mark),
Self::WindowClear | Self::WindowRestore => (
None,
Depth::Overwrite,
if self == Self::WindowClear {
Stencil::Inside
} else {
Stencil::Release
},
),
Self::Portal | Self::PortalGlass => (Some(Blend::Over), Depth::Tested, Stencil::Inside),
Self::PortalGlow => (Some(Blend::Add), Depth::Read, Stencil::Inside),
Self::PortalDepth => (None, Depth::Tested, Stencil::Inside),
}
}
}
struct PipelineSpec {
label: &'static str,
entry_points: (&'static str, &'static str),
color: Option<Blend>,
depth: Depth,
stencil: Stencil,
}
#[derive(Clone, Copy, PartialEq)]
enum Blend {
Over,
Add,
}
#[derive(Clone, Copy, PartialEq)]
enum Depth {
Tested,
Read,
Overwrite,
}
#[derive(Clone, Copy, PartialEq)]
pub(crate) enum Stencil {
Ignored,
Mark,
Inside,
Release,
}
impl Stencil {
fn state(self) -> wgpu::StencilState {
let pass_op = match self {
Self::Ignored => return wgpu::StencilState::default(),
Self::Mark => wgpu::StencilOperation::IncrementClamp,
Self::Inside => wgpu::StencilOperation::Keep,
Self::Release => wgpu::StencilOperation::DecrementClamp,
};
let face = wgpu::StencilFaceState {
compare: wgpu::CompareFunction::Equal,
fail_op: wgpu::StencilOperation::Keep,
depth_fail_op: wgpu::StencilOperation::Keep,
pass_op,
};
wgpu::StencilState {
front: face,
back: face,
read_mask: 0xff,
write_mask: if self == Self::Inside { 0 } else { 0xff },
}
}
}
#[derive(Clone, Copy)]
pub(crate) struct ScenePage {
pub(crate) scale: f32,
pub(crate) offset: [f32; 2],
pub(crate) size: (u32, u32),
}
pub(crate) struct SurfaceShading<'m> {
pub(crate) transform: Mat4,
pub(crate) material: &'m Material,
pub(crate) alpha: f32,
pub(crate) source_region: [f32; 4],
pub(crate) light_projection: Option<Mat4>,
pub(crate) backdrop_region: [f32; 4],
pub(crate) space: SpaceShading,
}
#[derive(Clone, Copy)]
pub(crate) struct SpaceShading {
pub(crate) light: Option<Light>,
pub(crate) clip: [f32; 4],
}
impl ScenePass {
pub(crate) fn pipeline(&self, kind: ScenePipeline) -> &FixedPipeline {
&self.pipelines[kind as usize]
}
pub(crate) fn window_buffers(
&mut self,
gpu: (&wgpu::Device, &mut impl FrameCommandRecorder),
) -> (SceneMeshBuffers, FrameCommandStats) {
let mesh = self.window.get_or_init(|| Arc::new(Mesh::plane()));
kept_mesh_buffers(&mut self.meshes, gpu, mesh)
}
pub(crate) fn parts(
&self,
device: &wgpu::Device,
backend: wgpu::Backend,
texture_layout: &wgpu::BindGroupLayout,
) -> &ScenePipelineParts {
self.parts
.get_or_init(|| pipeline_parts(device, backend, texture_layout))
}
pub(crate) fn shadow_map(&mut self, device: &wgpu::Device) -> Option<&ShadowMap> {
self.shadow_map_idle = 0;
if self.shadow_map.is_none() {
let parts = self.parts.get()?;
self.shadow_map = Some(create_shadow_map(device, parts));
}
self.shadow_map.as_ref()
}
pub(crate) fn mesh_buffers(
&mut self,
gpu: (&wgpu::Device, &mut impl FrameCommandRecorder),
mesh: &Arc<Mesh>,
) -> (SceneMeshBuffers, FrameCommandStats) {
kept_mesh_buffers(&mut self.meshes, gpu, mesh)
}
pub(crate) fn note_volume(&mut self, shader: &Arc<str>) {
if self.volume(shader).is_none() {
self.volumes.push(VolumeShader {
source: Arc::downgrade(shader),
module: LazyGpuResource::new("scene/volume-module"),
pipelines: [
VolumePipeline::new("scene/volume"),
VolumePipeline::new("scene/volume-depth"),
VolumePipeline::new("scene/volume-portal"),
VolumePipeline::new("scene/volume-portal-depth"),
],
});
}
}
pub(crate) fn volume(&self, shader: &Arc<str>) -> Option<&VolumeShader> {
self.volumes
.iter()
.find(|kept| std::ptr::addr_eq(kept.source.as_ptr(), Arc::as_ptr(shader)))
}
pub(crate) fn end_frame(&mut self) {
self.meshes.end_frame();
self.volumes.retain(|kept| kept.source.strong_count() > 0);
self.shadow_map_idle += 1;
if self.shadow_map_idle > IDLE_FRAMES {
self.shadow_map = None;
}
}
}
fn kept_mesh_buffers(
meshes: &mut IdlePool<KeptMesh>,
(device, recorder): (&wgpu::Device, &mut impl FrameCommandRecorder),
mesh: &Arc<Mesh>,
) -> (SceneMeshBuffers, FrameCommandStats) {
let id = mesh.id();
let mut stats = FrameCommandStats::default();
let buffers = match meshes.take(|kept| kept.id == id) {
Some(kept) => kept.buffers,
None => {
let vertices = scene_vertices(mesh);
let vertices: &[u8] = bytemuck::cast_slice(&vertices);
let indices: &[u8] = bytemuck::cast_slice(mesh.indices());
let freed = meshes.take(|kept| {
kept.mesh.strong_count() == 0
&& kept.buffers.vertices.size() >= vertices.len() as u64
&& kept.buffers.indices.size() >= indices.len() as u64
});
let (vertex_buffer, index_buffer) = match freed {
Some(kept) => (kept.buffers.vertices, kept.buffers.indices),
None => (
mesh_buffer(
device,
"Scene Mesh Vertices",
wgpu::BufferUsages::VERTEX,
vertices,
),
mesh_buffer(
device,
"Scene Mesh Indices",
wgpu::BufferUsages::INDEX,
indices,
),
),
};
stats += recorder.stage_buffer_copy(device, &vertex_buffer, 0, vertices);
stats += recorder.stage_buffer_copy(device, &index_buffer, 0, indices);
SceneMeshBuffers {
vertices: vertex_buffer,
indices: index_buffer,
index_count: mesh.indices().len() as u32,
}
}
};
meshes.put(
KeptMesh {
id,
mesh: Arc::downgrade(mesh),
buffers: buffers.clone(),
},
KEPT_MESHES,
u64::MAX,
|_| 0,
);
(buffers, stats)
}
fn pipeline_parts(
device: &wgpu::Device,
backend: wgpu::Backend,
texture_layout: &wgpu::BindGroupLayout,
) -> ScenePipelineParts {
let uniform_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Scene Surface Uniform Bind Group Layout"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: None,
},
count: None,
}],
});
let shadow_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Scene Shadow Map Bind Group Layout"),
entries: &[
shadow_map_entry(0, wgpu::TextureSampleType::Depth),
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Comparison),
count: None,
},
shadow_map_entry(2, wgpu::TextureSampleType::Float { filterable: false }),
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::NonFiltering),
count: None,
},
],
});
let shadow_samplers = ShadowSamplers {
compare: device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("Scene Shadow Sampler"),
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
compare: Some(wgpu::CompareFunction::LessEqual),
..wgpu::SamplerDescriptor::default()
}),
depth: device.create_sampler(&wgpu::SamplerDescriptor {
label: Some("Scene Shadow Depth Sampler"),
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
..wgpu::SamplerDescriptor::default()
}),
};
let shader = SharedShader::new(
device,
backend,
"Scene Surface Shader",
shaders::MESH_SURFACE,
&[
Some(texture_layout),
Some(&uniform_layout),
Some(&shadow_layout),
Some(texture_layout),
],
);
let no_shadow_view = depth_texture(device, "Scene No Shadow", 1);
let no_shadow = shadow_group(device, (&shadow_layout, &shadow_samplers), &no_shadow_view);
let volume_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Scene Volume Pipeline Layout"),
bind_group_layouts: &[Some(texture_layout), Some(&uniform_layout)],
immediate_size: 0,
});
ScenePipelineParts {
shader,
uniform_layout,
shadow_layout,
shadow_samplers,
no_shadow,
volume_layout,
}
}
fn shadow_map_entry(
binding: u32,
sample_type: wgpu::TextureSampleType,
) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type,
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
}
}
fn depth_texture(device: &wgpu::Device, label: &'static str, side: u32) -> wgpu::TextureView {
device
.create_texture(&wgpu::TextureDescriptor {
label: Some(label),
size: wgpu::Extent3d {
width: side,
height: side,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: DEPTH_FORMAT,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
})
.create_view(&wgpu::TextureViewDescriptor::default())
}
fn shadow_group(
device: &wgpu::Device,
(layout, samplers): (&wgpu::BindGroupLayout, &ShadowSamplers),
view: &wgpu::TextureView,
) -> wgpu::BindGroup {
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Scene Shadow Map Bind Group"),
layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&samplers.compare),
},
wgpu::BindGroupEntry {
binding: 2,
resource: wgpu::BindingResource::TextureView(view),
},
wgpu::BindGroupEntry {
binding: 3,
resource: wgpu::BindingResource::Sampler(&samplers.depth),
},
],
})
}
fn create_shadow_map(device: &wgpu::Device, parts: &ScenePipelineParts) -> ShadowMap {
let view = depth_texture(device, "Scene Shadow Map", SHADOW_MAP_SIZE);
let group = shadow_group(
device,
(&parts.shadow_layout, &parts.shadow_samplers),
&view,
);
ShadowMap { view, group }
}
fn scene_vertices(mesh: &Mesh) -> Vec<SceneVertex> {
mesh.positions()
.iter()
.zip(mesh.normals())
.zip(mesh.uvs())
.map(|((&position, &normal), &uv)| SceneVertex {
position,
normal,
uv,
})
.collect()
}
fn mesh_buffer(
device: &wgpu::Device,
label: &'static str,
usage: wgpu::BufferUsages,
bytes: &[u8],
) -> wgpu::Buffer {
device.create_buffer(&wgpu::BufferDescriptor {
label: Some(label),
size: (bytes.len() as u64).next_power_of_two().max(256),
usage: usage | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
})
}
pub(crate) fn scene_surface_uniform_spec() -> UploadAllocatorSpec {
UploadAllocatorSpec::uniform(
"Scene Surface Uniform Buffer",
"Scene Surface Uniform Bind Group",
std::mem::size_of::<SceneSurfaceUniforms>() as u64,
)
}
pub(crate) fn scene_volume_uniform_spec() -> UploadAllocatorSpec {
UploadAllocatorSpec::uniform(
"Scene Volume Uniform Buffer",
"Scene Volume Uniform Bind Group",
std::mem::size_of::<SceneVolumeUniforms>() as u64,
)
}
fn light_uniform(light: Option<Light>, into: impl Fn(Vec3) -> Vec3) -> [f32; 4] {
match light {
Some(light) => {
let direction = into(light.direction).normalized().unwrap_or(Vec3::ZERO);
[
direction.x,
direction.y,
direction.z,
light.ambient.clamp(0.0, 1.0),
]
}
None => [0.0, 0.0, 0.0, -1.0],
}
}
pub(crate) fn volume_uniforms(
scene: &SceneDraw,
(transform, mesh, volume): (&Mat4, &Mesh, &Volume),
(alpha, source_region): (f32, [f32; 4]),
(page, eye, space): (ScenePage, Vec3, SpaceShading),
) -> Option<SceneVolumeUniforms> {
let model_from_scene = transform.inverse()?;
let point = |point: Vec3| [point.x, point.y, point.z, 1.0];
let (low, high) = mesh.bounds();
Some(SceneVolumeUniforms {
clip_from_model: transform
.then(&clip_from_scene(scene, page))
.transpose()
.rows(),
eye: point(model_from_scene.transform_point(eye)),
light: light_uniform(space.light, |direction| {
model_from_scene.transform_vector(direction)
}),
footprint: [
1.0 / (scene.camera.focal_length * page.scale).max(f32::MIN_POSITIVE),
alpha.clamp(0.0, 1.0),
0.0,
0.0,
],
low: point(low),
high: point(high),
params: bytemuck::cast(volume.params),
clip: plane_in_model(space.clip, transform),
source_region,
})
}
fn plane_in_model(plane: [f32; 4], transform: &Mat4) -> [f32; 4] {
let rows = transform.rows();
std::array::from_fn(|column| (0..4).map(|row| plane[row] * rows[row][column]).sum())
}
pub(crate) fn surface_uniforms(
scene: &SceneDraw,
surface: &SurfaceShading,
(page, eye): (ScenePage, Vec3),
) -> Option<SceneSurfaceUniforms> {
let inverse = surface.transform.inverse()?.rows();
let normal_row = |row: usize| [inverse[0][row], inverse[1][row], inverse[2][row], 0.0];
let light = light_uniform(surface.space.light, |direction| direction);
let columns = |matrix: Mat4| matrix.transpose().rows();
let clip_from_scene = clip_from_scene(scene, page);
let (mode, glass) = match surface.material {
Material::Unlit => (0.0, None),
Material::Lit => (1.0, None),
Material::Emissive | Material::Glow => (2.0, None),
Material::Glass(glass) => (0.0, Some(*glass)),
Material::Volume(_) => (0.0, None),
};
let glass_tint = glass.map_or([1.0; 4], |glass| {
[glass.tint.0, glass.tint.1, glass.tint.2, 1.0]
});
Some(SceneSurfaceUniforms {
clip_from_model: columns(surface.transform.then(&clip_from_scene)),
scene_from_model: columns(surface.transform),
light_clip_from_model: columns(
surface
.light_projection
.map_or(Mat4::IDENTITY, |projection| {
surface.transform.then(&projection)
}),
),
clip_from_scene: columns(clip_from_scene),
normal_rows: [normal_row(0), normal_row(1), normal_row(2)],
eye: [eye.x, eye.y, eye.z, 1.0],
light,
source_region: surface.source_region,
params: [surface.alpha.clamp(0.0, 1.0), mode, 0.0, 0.0],
shadow: [1.0 / SHADOW_MAP_SIZE as f32, SHADOW_DEPTH_BIAS, 0.0, 0.0],
shadow_soft: penumbra_scale(scene, surface.light_projection),
glass: [
glass.map_or(1.0, |glass| 1.0 / glass.index.max(1.0)),
glass.map_or(0.0, |glass| glass.thickness.max(0.0)),
page.size.0 as f32,
page.size.1 as f32,
],
glass_tint,
backdrop_region: surface.backdrop_region,
clip: surface.space.clip,
})
}
fn penumbra_scale(scene: &SceneDraw, light_projection: Option<Mat4>) -> [f32; 4] {
let (Some(light), Some(projection)) = (scene.light, light_projection) else {
return [0.0; 4];
};
if light.size <= 0.0 {
return [0.0; 4];
}
let rows = projection.rows();
let length = |row: [f32; 4]| Vec3::new(row[0], row[1], row[2]).length();
let depth = length(rows[2]).max(f32::MIN_POSITIVE);
let spread = light.size.tan();
[
spread * length(rows[0]) * 0.5 / depth,
spread * length(rows[1]) * 0.5 / depth,
1.0,
0.0,
]
}
fn clip_from_scene(scene: &SceneDraw, page: ScenePage) -> Mat4 {
let camera = scene.camera;
let (scale, focal_length) = (page.scale, camera.focal_length);
let (width, height) = (page.size.0 as f32, page.size.1 as f32);
let center_x = scale * (scene.rect.x + scene.rect.width * 0.5) - page.offset[0];
let center_y = scale * (scene.rect.y + scene.rect.height * 0.5) - page.offset[1];
let projection = Mat4::from_rows([
[
2.0 * scale * focal_length / width,
0.0,
2.0 * center_x / width - 1.0,
0.0,
],
[
0.0,
-2.0 * scale * focal_length / height,
1.0 - 2.0 * center_y / height,
0.0,
],
[0.0, 0.0, 1.0, -camera.near()],
[0.0, 0.0, 1.0, 0.0],
]);
camera.view.then(&projection)
}
pub(crate) fn surface_page_bounds(
scene: &SceneDraw,
transform: &Mat4,
mesh: &Mesh,
page: ScenePage,
) -> Option<[f32; 4]> {
let rows = transform.then(&clip_from_scene(scene, page)).rows();
let (low, high) = mesh.bounds();
let mut bounds = [
f32::INFINITY,
f32::INFINITY,
f32::NEG_INFINITY,
f32::NEG_INFINITY,
];
for corner in 0..8 {
let pick = |bit: usize, low: f32, high: f32| if corner & bit == 0 { low } else { high };
let point = [
pick(1, low.x, high.x),
pick(2, low.y, high.y),
pick(4, low.z, high.z),
1.0,
];
let [x, y, _, w] = rows.map(|row| row.iter().zip(point).map(|(a, b)| a * b).sum::<f32>());
if w <= 0.0 {
return None;
}
let pixel = [
(x / w * 0.5 + 0.5) * page.size.0 as f32,
(0.5 - y / w * 0.5) * page.size.1 as f32,
];
bounds = [
bounds[0].min(pixel[0]),
bounds[1].min(pixel[1]),
bounds[2].max(pixel[0]),
bounds[3].max(pixel[1]),
];
}
Some(bounds)
}
pub(crate) fn view_depth(scene: &SceneDraw, transform: &Mat4) -> f32 {
transform
.then(&scene.camera.view)
.transform_point(Vec3::ZERO)
.z
}
pub(crate) fn scene_scissor(
scene: &SceneDraw,
clip: Option<Rect>,
page: ScenePage,
) -> Option<(u32, u32, u32, u32)> {
let mut rect = scene.rect;
for cut in [scene.clip, clip].into_iter().flatten() {
rect = rect.intersect(cut)?;
}
let left = (rect.x * page.scale - page.offset[0]).floor().max(0.0);
let top = (rect.y * page.scale - page.offset[1]).floor().max(0.0);
let right = ((rect.x + rect.width) * page.scale - page.offset[0])
.ceil()
.min(page.size.0 as f32);
let bottom = ((rect.y + rect.height) * page.scale - page.offset[1])
.ceil()
.min(page.size.1 as f32);
(right > left && bottom > top).then_some((
left as u32,
top as u32,
(right - left) as u32,
(bottom - top) as u32,
))
}
pub(crate) fn scene_pipeline_job(
device: &Arc<wgpu::Device>,
cache: Option<wgpu::PipelineCache>,
(format, kind): (wgpu::TextureFormat, ScenePipeline),
parts: &ScenePipelineParts,
) -> impl FnOnce() -> wgpu::RenderPipeline + CompilerSend + 'static {
let device = Arc::clone(device);
let shader = parts.shader.clone();
move || {
let module = shader.module();
let spec = kind.spec();
let color_target = color_target(format, spec.color);
let shadow = kind == ScenePipeline::Shadow;
let targets: &[Option<wgpu::ColorTargetState>] = if shadow { &[] } else { &color_target };
create_scene_pipeline(
&device,
cache.as_ref(),
spec.label,
(shader.layout(), module, spec.entry_points),
(targets, depth_state(&spec, shadow)),
)
}
}
fn depth_state(spec: &PipelineSpec, shadow: bool) -> wgpu::DepthStencilState {
let (compare, write) = match spec.depth {
Depth::Tested => (wgpu::CompareFunction::Less, true),
Depth::Read => (wgpu::CompareFunction::LessEqual, false),
Depth::Overwrite => (wgpu::CompareFunction::Always, true),
};
wgpu::DepthStencilState {
format: if shadow {
DEPTH_FORMAT
} else {
SCENE_DEPTH_FORMAT
},
depth_write_enabled: Some(write),
depth_compare: Some(compare),
stencil: spec.stencil.state(),
bias: if shadow {
wgpu::DepthBiasState {
constant: 2,
slope_scale: 2.0,
clamp: 0.0,
}
} else {
wgpu::DepthBiasState::default()
},
}
}
pub(crate) fn volume_pipeline_job(
device: &Arc<wgpu::Device>,
cache: Option<wgpu::PipelineCache>,
(format, backend, variant): (wgpu::TextureFormat, wgpu::Backend, VolumeVariant),
(parts, kept): (&ScenePipelineParts, &VolumeShader),
) -> impl FnOnce() -> Option<wgpu::RenderPipeline> + CompilerSend + 'static {
let device = Arc::clone(device);
let layout = parts.volume_layout.clone();
let module = kept.module.clone();
let source = kept.source.upgrade();
move || {
let source = source?;
let module = module
.get_or_init(backend, || {
create_volume_shader_module(&device, &source, backend)
})
.as_ref()?;
let spec = variant.spec();
Some(create_scene_pipeline(
&device,
cache.as_ref(),
spec.label,
(&layout, module, spec.entry_points),
(&color_target(format, spec.color), depth_state(&spec, false)),
))
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum VolumeVariant {
Color,
Depth,
Portal,
PortalDepth,
}
impl VolumeVariant {
fn spec(self) -> PipelineSpec {
let (label, fragment, color, stencil) = match self {
Self::Color => (
"Scene Volume Pipeline",
"mesh_volume_fs",
Some(Blend::Over),
Stencil::Ignored,
),
Self::Depth => (
"Scene Volume Depth Pipeline",
"mesh_volume_fs",
None,
Stencil::Ignored,
),
Self::Portal => (
"Scene Portal Volume Pipeline",
"mesh_volume_portal_fs",
Some(Blend::Over),
Stencil::Inside,
),
Self::PortalDepth => (
"Scene Portal Volume Depth Pipeline",
"mesh_volume_portal_fs",
None,
Stencil::Inside,
),
};
PipelineSpec {
label,
entry_points: ("mesh_volume_vs", fragment),
color,
depth: Depth::Tested,
stencil,
}
}
}
fn color_target(
format: wgpu::TextureFormat,
blend: Option<Blend>,
) -> [Option<wgpu::ColorTargetState>; 1] {
let added = wgpu::BlendState {
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::One,
operation: wgpu::BlendOperation::Add,
},
alpha: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::Zero,
dst_factor: wgpu::BlendFactor::One,
operation: wgpu::BlendOperation::Add,
},
};
[Some(wgpu::ColorTargetState {
format,
blend: Some(match blend {
Some(Blend::Add) => added,
_ => wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING,
}),
write_mask: if blend.is_some() {
wgpu::ColorWrites::ALL
} else {
wgpu::ColorWrites::empty()
},
})]
}
fn create_scene_pipeline(
device: &wgpu::Device,
cache: Option<&wgpu::PipelineCache>,
label: &'static str,
(layout, module, (vertex, fragment)): (
&wgpu::PipelineLayout,
&wgpu::ShaderModule,
(&'static str, &'static str),
),
(targets, depth_stencil): (&[Option<wgpu::ColorTargetState>], wgpu::DepthStencilState),
) -> wgpu::RenderPipeline {
create_render_pipeline_logged(
device,
cache,
label,
wgpu::RenderPipelineDescriptor {
label: Some(label),
layout: Some(layout),
vertex: wgpu::VertexState {
module,
entry_point: Some(vertex),
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[Some(wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<SceneVertex>() as u64,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![
0 => Float32x3,
1 => Float32x3,
2 => Float32x2
],
})],
},
fragment: Some(wgpu::FragmentState {
module,
entry_point: Some(fragment),
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets,
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: Some(depth_stencil),
multisample: wgpu::MultisampleState::default(),
multiview_mask: None,
cache: None,
},
)
}