Merge pull request #1887 from ludovic111/feat/shader-packs-shadow-buffers
🤖 Agent: shader packs clean room: main merged into the stack (merge commits), verify passed on this head, parity skipped by parity-scope.
This commit is contained in:
@@ -73,6 +73,7 @@ pub mod pack_draw;
|
||||
mod pack_entities;
|
||||
mod pack_hand;
|
||||
mod pack_post;
|
||||
mod pack_shadow;
|
||||
mod pack_sky;
|
||||
mod pack_timing;
|
||||
pub mod pack_uniforms;
|
||||
|
||||
@@ -37,6 +37,10 @@ enum Source {
|
||||
/// The block atlas (pack_atlas.rs), in gbuffers programs.
|
||||
Atlas,
|
||||
Lightmap,
|
||||
/// `shadowtex0`/`shadowtex1` (6.3), with depth comparison (`sampler2DShadow`) or not.
|
||||
ShadowDepth(usize, bool),
|
||||
/// `shadowcolor0/1` read outside the shadow programs (in them they are `Color`).
|
||||
ShadowColor(usize),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -63,13 +67,17 @@ enum SamplerKind {
|
||||
impl SamplerKind {
|
||||
fn of(source: Source, filtering: bool) -> Self {
|
||||
match source {
|
||||
Source::Color(_) | Source::Dummy if !filtering => Self::Nearest,
|
||||
Source::Depth | Source::Depth1 => Self::Nearest,
|
||||
Source::Color(_) | Source::ShadowColor(_) | Source::Dummy if !filtering => {
|
||||
Self::Nearest
|
||||
}
|
||||
Source::Depth | Source::Depth1 | Source::ShadowDepth(_, false) => Self::Nearest,
|
||||
Source::Noise => Self::Repeat,
|
||||
Source::Shadow => Self::Comparison,
|
||||
Source::Shadow | Source::ShadowDepth(_, true) => Self::Comparison,
|
||||
// Blocks look as the game draws them: nearest texels.
|
||||
Source::Atlas => Self::NearestRepeat,
|
||||
Source::Color(_) | Source::Dummy | Source::Lightmap => Self::Linear,
|
||||
Source::Color(_) | Source::ShadowColor(_) | Source::Dummy | Source::Lightmap => {
|
||||
Self::Linear
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,6 +166,8 @@ struct Pass {
|
||||
flip: u16,
|
||||
/// Buffers sampled, for the bind group cache key.
|
||||
reads: u16,
|
||||
/// shadowcolor buffers sampled by a program of the other buffers.
|
||||
shadow_reads: u16,
|
||||
/// Buffers written.
|
||||
written: u16,
|
||||
/// Buffers whose mip chain is regenerated right before it (6.1.7).
|
||||
@@ -168,9 +178,9 @@ struct Pass {
|
||||
atlas: Option<wgpu::TextureView>,
|
||||
/// Reads `mc_Entity`'s table (`PackPost::block_ids`).
|
||||
ids: bool,
|
||||
/// Bind groups by the sides read (low 16 bits) and the buffers whose mip chain is current
|
||||
/// (high 16 bits).
|
||||
groups: HashMap<u32, wgpu::BindGroup>,
|
||||
/// Bind groups by the sides read (bits 0–15), the buffers whose mip chain is current (bits
|
||||
/// 16–31) and the shadowcolor sides read (bits 32–33).
|
||||
groups: HashMap<u64, wgpu::BindGroup>,
|
||||
}
|
||||
|
||||
/// One side of a buffer with mipmaps.
|
||||
@@ -258,6 +268,9 @@ pub struct PackPost {
|
||||
hand: Option<(Pass, super::pack_hand::PackHand)>,
|
||||
/// A program after translucent terrain reads `depthtex1`: it is copied before water.
|
||||
depth1: bool,
|
||||
/// The shadow buffers, when the shadow pass runs (2.5), and the `shadowcomp` programs.
|
||||
shadow_targets: Option<super::pack_shadow::ShadowTargets>,
|
||||
shadow_comp: Vec<Pass>,
|
||||
/// Buffers with mipmaps (filterable ones a program asks them for).
|
||||
mipped: u16,
|
||||
/// Buffers whose mip chain matches their first level, this frame.
|
||||
@@ -342,7 +355,37 @@ fn texture_format(f: Format, features: wgpu::Features) -> wgpu::TextureFormat {
|
||||
}
|
||||
}
|
||||
|
||||
fn source(name: &str) -> Source {
|
||||
/// The texture behind sampler `name` in a program of the shadow buffers (`shadow_space`: buffer
|
||||
/// n is shadowcolor n) or of the others, when the shadow pass runs (`shadow_pass`). `shadow` is
|
||||
/// `shadowtex1` when the program also declares `watershadow` (6.3).
|
||||
fn source(name: &str, shadow_space: bool, shadow_pass: bool, watershadow: bool) -> Source {
|
||||
let shadow_color = match name {
|
||||
"shadowcolor" | "shadowcolor0" => Some(0),
|
||||
"shadowcolor1" => Some(1),
|
||||
_ => None,
|
||||
};
|
||||
let shadow_depth = match name {
|
||||
"shadowtex0" | "watershadow" | "shadowtex0HW" => Some(0),
|
||||
"shadowtex1" | "shadowtex1HW" => Some(1),
|
||||
"shadow" => Some(usize::from(watershadow)),
|
||||
_ => None,
|
||||
};
|
||||
if shadow_pass {
|
||||
if let Some(n) = shadow_depth {
|
||||
return Source::ShadowDepth(n, name.ends_with("HW"));
|
||||
}
|
||||
if let Some(n) = shadow_color {
|
||||
return if shadow_space {
|
||||
Source::Color(n)
|
||||
} else {
|
||||
Source::ShadowColor(n)
|
||||
};
|
||||
}
|
||||
}
|
||||
if shadow_space && horizon_shaders::buffer_index(name).is_some() {
|
||||
// colortex buffers are not this pass's.
|
||||
return Source::Dummy;
|
||||
}
|
||||
match name {
|
||||
"depthtex0" | "gdepthtex" => Source::Depth,
|
||||
// The hand is drawn after the pack, so `depthtex2` (without it) is `depthtex1`.
|
||||
@@ -606,11 +649,15 @@ pub(super) struct Geometry<'a> {
|
||||
}
|
||||
|
||||
/// The pass of program `p`: a quad, or `geometry` drawn by the game. `None` (with a note) when
|
||||
/// wgpu refuses it or it writes nothing.
|
||||
/// wgpu refuses it or it writes nothing. `formats` are its buffers' (for a program of the shadow
|
||||
/// buffers, buffer n is shadowcolor n); `shadow` the shadowcolor formats when the shadow pass
|
||||
/// runs.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn build_pass(
|
||||
device: &wgpu::Device,
|
||||
p: horizon_shaders::PostProgram,
|
||||
formats: &[wgpu::TextureFormat; BUFFERS],
|
||||
shadow: Option<&[wgpu::TextureFormat; 2]>,
|
||||
screen: wgpu::TextureFormat,
|
||||
notes: &mut Vec<String>,
|
||||
geometry: Option<&Geometry<'_>>,
|
||||
@@ -639,6 +686,8 @@ fn build_pass(
|
||||
let mut samplers = Vec::new();
|
||||
let (mut kinds, mut slots) = (HashMap::new(), HashMap::new());
|
||||
let mut reads = 0u16;
|
||||
let mut shadow_reads = 0u16;
|
||||
let watershadow = interface.samplers.iter().any(|s| s.name == "watershadow");
|
||||
// A gbuffers pass draws into its buffers and the depth buffer: reading them as well would
|
||||
// be a feedback loop (undefined in GL, refused by wgpu), so those read a blank texture.
|
||||
let in_place = match geometry {
|
||||
@@ -646,8 +695,10 @@ fn build_pass(
|
||||
None => 0,
|
||||
};
|
||||
for s in &interface.samplers {
|
||||
let mut src = source(&s.name);
|
||||
let mut src = source(&s.name, p.group.shadow(), shadow.is_some(), watershadow);
|
||||
let blank = match src {
|
||||
// The shadow pass writes the shadow map.
|
||||
Source::ShadowDepth(..) => p.group == Group::Shadow,
|
||||
Source::Atlas => geometry.is_none(),
|
||||
Source::Depth => geometry.is_some(),
|
||||
// Translucent terrain reads the copy; opaque terrain would read itself.
|
||||
@@ -660,14 +711,23 @@ fn build_pass(
|
||||
Source::Color(n) => formats[n]
|
||||
.sample_type(None, Some(features))
|
||||
.unwrap_or(float),
|
||||
Source::Depth | Source::Depth1 => wgpu::TextureSampleType::Float { filterable: false },
|
||||
Source::ShadowColor(n) => shadow
|
||||
.and_then(|f| f[n].sample_type(None, Some(features)))
|
||||
.unwrap_or(float),
|
||||
Source::Depth | Source::Depth1 | Source::ShadowDepth(..) => {
|
||||
wgpu::TextureSampleType::Float { filterable: false }
|
||||
}
|
||||
// Unfilterable on WebGPU-level devices; the lightmap is sampled at texel centres.
|
||||
Source::Lightmap => float,
|
||||
_ => float,
|
||||
};
|
||||
let int = !matches!(sample_type, wgpu::TextureSampleType::Float { .. });
|
||||
if s.ty == "sampler2DShadow" {
|
||||
(src, sample_type) = (Source::Shadow, wgpu::TextureSampleType::Depth);
|
||||
src = match src {
|
||||
Source::ShadowDepth(n, _) => Source::ShadowDepth(n, true),
|
||||
_ => Source::Shadow,
|
||||
};
|
||||
sample_type = wgpu::TextureSampleType::Depth;
|
||||
}
|
||||
let wanted = match s.ty.as_str() {
|
||||
"sampler2DShadow" => true,
|
||||
@@ -683,7 +743,9 @@ fn build_pass(
|
||||
));
|
||||
(src, sample_type) = (Source::Dummy, float);
|
||||
}
|
||||
if blank {
|
||||
if blank && sample_type == wgpu::TextureSampleType::Depth {
|
||||
src = Source::Shadow;
|
||||
} else if blank {
|
||||
// The blank texture is float: an integer sampler then fails the pipeline (a note).
|
||||
src = Source::Dummy;
|
||||
if int {
|
||||
@@ -691,8 +753,10 @@ fn build_pass(
|
||||
}
|
||||
}
|
||||
let filtering = sample_type == float;
|
||||
if let Source::Color(n) = src {
|
||||
reads |= 1 << n;
|
||||
match src {
|
||||
Source::Color(n) => reads |= 1 << n,
|
||||
Source::ShadowColor(n) => shadow_reads |= 1 << n,
|
||||
_ => {}
|
||||
}
|
||||
entries.push(texture_entry(s.binding, sample_type));
|
||||
let kind = SamplerKind::of(src, filtering);
|
||||
@@ -849,6 +913,7 @@ fn build_pass(
|
||||
writes,
|
||||
flip,
|
||||
reads,
|
||||
shadow_reads,
|
||||
written,
|
||||
mipmaps: p.mipmaps,
|
||||
draw,
|
||||
@@ -892,18 +957,32 @@ impl PackPost {
|
||||
|flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0);
|
||||
let mut used = 1u16;
|
||||
let draws = DrawUniforms::new(device);
|
||||
// The shadow buffers' formats when the shadow pass runs; their programs see them as
|
||||
// buffers 0 and 1.
|
||||
let shadow_formats: Option<[wgpu::TextureFormat; 2]> = post.shadow_pass.then(|| {
|
||||
std::array::from_fn(|n| texture_format(shadow_config.colors[n].format, features))
|
||||
});
|
||||
let shadow_space: [wgpu::TextureFormat; BUFFERS] = std::array::from_fn(|i| {
|
||||
shadow_formats
|
||||
.filter(|_| i < 2)
|
||||
.map_or(formats[i], |f| f[i])
|
||||
});
|
||||
let mut build = |p: horizon_shaders::PostProgram, geometry: Option<&Geometry<'_>>| {
|
||||
let in_shadow = p.group.shadow();
|
||||
let pass = build_pass(
|
||||
device,
|
||||
p,
|
||||
&formats,
|
||||
if in_shadow { &shadow_space } else { &formats },
|
||||
shadow_formats.as_ref(),
|
||||
screen,
|
||||
&mut notes,
|
||||
geometry,
|
||||
&custom_names,
|
||||
&draws.layout,
|
||||
)?;
|
||||
used |= pass.reads | pass.written | pass.flip;
|
||||
if !in_shadow {
|
||||
used |= pass.reads | pass.written | pass.flip;
|
||||
}
|
||||
Some(pass)
|
||||
};
|
||||
// Flat terrain reads `mc_Entity` from a table of the sprite layers.
|
||||
@@ -1036,6 +1115,12 @@ impl PackPost {
|
||||
.filter(|p| !skip.split(',').any(|s| s == p.name))
|
||||
.filter_map(|p| build(p, None))
|
||||
.collect();
|
||||
let shadow_comp: Vec<Pass> = post
|
||||
.shadow_comp
|
||||
.into_iter()
|
||||
.filter(|p| !skip.split(',').any(|s| s == p.name))
|
||||
.filter_map(|p| build(p, None))
|
||||
.collect();
|
||||
if let Some(t) = &terrain {
|
||||
notes.push(format!("terrain draws with {}", t.name));
|
||||
}
|
||||
@@ -1077,6 +1162,39 @@ impl PackPost {
|
||||
.chain(&water)
|
||||
.chain(weather.iter().take(1).flatten())
|
||||
.any(|p| p.samplers.iter().any(|s| s.source == Source::Depth1));
|
||||
let every_pass = || {
|
||||
(passes
|
||||
.iter()
|
||||
.chain(&shadow_comp)
|
||||
.chain(&terrain)
|
||||
.chain(&water))
|
||||
.chain(sky.iter().flat_map(|s| s.basic.iter().chain(&s.textured)))
|
||||
.chain(entities.iter().flat_map(|e| e.passes.iter().flatten()))
|
||||
.chain(&clouds)
|
||||
.chain(weather.iter().flatten())
|
||||
.chain(hand.iter().map(|h| &h.0))
|
||||
};
|
||||
let shadow_targets = shadow_formats.map(|f| {
|
||||
let colors = every_pass().fold(0u16, |m, p| {
|
||||
let own = if p.group.shadow() {
|
||||
p.reads | p.written | p.flip
|
||||
} else {
|
||||
0
|
||||
};
|
||||
m | own | p.shadow_reads
|
||||
});
|
||||
let depth1 = every_pass()
|
||||
.flat_map(|p| &p.samplers)
|
||||
.any(|s| matches!(s.source, Source::ShadowDepth(1, _)));
|
||||
let t =
|
||||
super::pack_shadow::ShadowTargets::new(device, &shadow_config, f, colors, depth1);
|
||||
notes.push(format!(
|
||||
"shadow map {0}x{0}, shadowcolor buffers {colors:#b}{1}",
|
||||
t.size,
|
||||
if depth1 { ", shadowtex1" } else { "" }
|
||||
));
|
||||
t
|
||||
});
|
||||
// A clear only matters when a program reads the buffer before one writes all of it
|
||||
// (composite passes cover their whole target, terrain does not); the others are skipped.
|
||||
let mut cleared = Vec::new();
|
||||
@@ -1215,6 +1333,8 @@ impl PackPost {
|
||||
weather_generation: 0,
|
||||
hand,
|
||||
depth1,
|
||||
shadow_targets,
|
||||
shadow_comp,
|
||||
mipped,
|
||||
mips_valid: 0,
|
||||
down,
|
||||
@@ -1319,17 +1439,43 @@ impl PackPost {
|
||||
queue.write_buffer(&pass.ubo, 0, &pass.block);
|
||||
}
|
||||
let targets = self.targets.as_ref().expect("resized");
|
||||
let shadow = self.shadow_targets.as_ref();
|
||||
let shadow_main = shadow.map_or(0, |s| s.main);
|
||||
let shadow_space = pass.group.shadow();
|
||||
// A chain is sampled only while it is current: a buffer written since its last chain
|
||||
// reads its first level, never stale levels (6.1.7).
|
||||
let current = self.mips_valid & self.mipped & pass.reads;
|
||||
let key = u32::from(self.main & pass.reads) | u32::from(current) << 16;
|
||||
// reads its first level, never stale levels (6.1.7). The shadow buffers have none.
|
||||
let (main, current) = if shadow_space {
|
||||
(shadow_main, 0)
|
||||
} else {
|
||||
(self.main, self.mips_valid & self.mipped & pass.reads)
|
||||
};
|
||||
let key = u64::from(main & pass.reads)
|
||||
| u64::from(current) << 16
|
||||
| u64::from(shadow_main & pass.shadow_reads) << 32;
|
||||
let group = pass.groups.entry(key).or_insert_with(|| {
|
||||
let mut entries = vec![wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: pass.ubo.as_entire_binding(),
|
||||
}];
|
||||
for s in &pass.samplers {
|
||||
let shadow_color = |i: usize, side: usize| {
|
||||
shadow.and_then(|t| t.color[i].as_ref()).map(|c| &c[side])
|
||||
};
|
||||
let texture = match s.source {
|
||||
Source::Color(i) if shadow_space => {
|
||||
shadow_color(i, usize::from(key & 1 << i != 0)).unwrap_or(&self.dummy)
|
||||
}
|
||||
Source::ShadowColor(i) => {
|
||||
shadow_color(i, usize::from(key & 1 << (32 + i) != 0))
|
||||
.unwrap_or(&self.dummy)
|
||||
}
|
||||
Source::ShadowDepth(i, _) => match shadow {
|
||||
Some(t) => match (i, &t.depth1) {
|
||||
(1, Some(d)) => &d.1,
|
||||
_ => &t.depth.1,
|
||||
},
|
||||
None => &self.shadow.0,
|
||||
},
|
||||
Source::Color(i) => {
|
||||
let side = usize::from(key & 1 << i != 0);
|
||||
match (&targets.mips[i], &targets.color[i]) {
|
||||
@@ -1366,7 +1512,7 @@ impl PackPost {
|
||||
SamplerKind::Nearest => &self.nearest,
|
||||
SamplerKind::Repeat => &self.repeat,
|
||||
SamplerKind::NearestRepeat => &self.nearest_repeat,
|
||||
SamplerKind::Comparison => &self.shadow.1,
|
||||
SamplerKind::Comparison => shadow.map_or(&self.shadow.1, |t| &t.comparison),
|
||||
};
|
||||
entries.push(wgpu::BindGroupEntry {
|
||||
binding: s.binding,
|
||||
@@ -1399,15 +1545,20 @@ impl PackPost {
|
||||
load: wgpu::LoadOp::Load,
|
||||
store: wgpu::StoreOp::Store,
|
||||
};
|
||||
// The shadow pass draws into the shadow buffers and the shadow map.
|
||||
let shadow = self.shadow_targets.as_ref().filter(|_| pass.group.shadow());
|
||||
let mut attachments: Vec<Option<wgpu::RenderPassColorAttachment>> = Vec::new();
|
||||
for &(location, buffer) in &pass.writes {
|
||||
let l = location as usize;
|
||||
if attachments.len() <= l {
|
||||
attachments.resize(l + 1, None);
|
||||
}
|
||||
attachments[l] = targets.color[buffer]
|
||||
.as_ref()
|
||||
.and_then(|t| attach(&t[usize::from(self.main & 1 << buffer != 0)], load));
|
||||
attachments[l] = match shadow {
|
||||
Some(s) => s.color(buffer, false).and_then(|v| attach(v, load)),
|
||||
None => targets.color[buffer]
|
||||
.as_ref()
|
||||
.and_then(|t| attach(&t[usize::from(self.main & 1 << buffer != 0)], load)),
|
||||
};
|
||||
}
|
||||
let timestamp_writes = self.timing.as_mut().and_then(|t| t.writes(&pass.name));
|
||||
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
@@ -1415,7 +1566,7 @@ impl PackPost {
|
||||
color_attachments: &attachments,
|
||||
timestamp_writes,
|
||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||
view: &targets.depth,
|
||||
view: shadow.map_or(&targets.depth, |s| &s.depth.1),
|
||||
depth_ops: Some(wgpu::Operations {
|
||||
load: wgpu::LoadOp::Load,
|
||||
store: wgpu::StoreOp::Store,
|
||||
@@ -1930,6 +2081,55 @@ impl PackPost {
|
||||
}
|
||||
}
|
||||
|
||||
/// The shadow pass (2.1, passes 2 and 3): the shadow buffers cleared, then the `shadowcomp`
|
||||
/// programs, each drawing into the "alt" side of what it writes and flipping it after.
|
||||
fn run_shadow(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
) {
|
||||
let Some(t) = &self.shadow_targets else {
|
||||
return;
|
||||
};
|
||||
t.clear(encoder);
|
||||
t.copy_depth1(encoder);
|
||||
let size = [t.size; 2];
|
||||
let mut comp = std::mem::take(&mut self.shadow_comp);
|
||||
for pass in &mut comp {
|
||||
let group = self.bind(device, queue, pass, size);
|
||||
let t = self.shadow_targets.as_ref().expect("shadow pass");
|
||||
let mut attachments: Vec<Option<wgpu::RenderPassColorAttachment>> = Vec::new();
|
||||
for &(location, buffer) in &pass.writes {
|
||||
let l = location as usize;
|
||||
if attachments.len() <= l {
|
||||
attachments.resize(l + 1, None);
|
||||
}
|
||||
// Overwritten whole.
|
||||
let ops = wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT),
|
||||
store: wgpu::StoreOp::Store,
|
||||
};
|
||||
attachments[l] = t.color(buffer, true).and_then(|v| attach(v, ops));
|
||||
}
|
||||
{
|
||||
let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some(&pass.name),
|
||||
color_attachments: &attachments,
|
||||
timestamp_writes: self.timing.as_mut().and_then(|t| t.writes(&pass.name)),
|
||||
..Default::default()
|
||||
});
|
||||
rp.set_pipeline(&pass.pipeline);
|
||||
rp.set_bind_group(0, &group, &[]);
|
||||
rp.draw(0..6, 0..1);
|
||||
}
|
||||
if let Some(t) = &mut self.shadow_targets {
|
||||
t.main ^= pass.flip;
|
||||
}
|
||||
}
|
||||
self.shadow_comp = comp;
|
||||
}
|
||||
|
||||
/// Runs the pack over the world drawn into `world` (colour) and `world_depth`, then draws
|
||||
/// the result onto `screen`.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
@@ -1978,6 +2178,10 @@ impl PackPost {
|
||||
}
|
||||
self.uniforms.update(frame, inputs, size);
|
||||
self.uniforms.eval_custom(&mut self.custom);
|
||||
if self.targets.is_none() {
|
||||
return;
|
||||
}
|
||||
self.run_shadow(device, queue, encoder);
|
||||
let Some(targets) = &self.targets else { return };
|
||||
let main = |m: u16, i: usize| usize::from(m & 1 << i != 0);
|
||||
let view = |m: u16, i: usize, alt: bool| {
|
||||
@@ -2249,10 +2453,22 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn sampler_names() {
|
||||
assert!(source("gaux1") == Source::Color(4));
|
||||
assert!(source("gdepthtex") == Source::Depth);
|
||||
assert!(source("depthtex2") == Source::Depth1);
|
||||
assert!(source("noisetex") == Source::Noise);
|
||||
assert!(source("shadowcolor0") == Source::Dummy);
|
||||
let main = |n| source(n, false, false, false);
|
||||
assert!(main("gaux1") == Source::Color(4));
|
||||
assert!(main("gdepthtex") == Source::Depth);
|
||||
assert!(main("depthtex2") == Source::Depth1);
|
||||
assert!(main("noisetex") == Source::Noise);
|
||||
// Without the shadow pass, the shadow buffers are stand-ins.
|
||||
assert!(main("shadowcolor0") == Source::Dummy);
|
||||
assert!(main("shadowtex0") == Source::Dummy);
|
||||
// With it: shadowcolor n is buffer n of the shadow programs, `shadow` is shadowtex1
|
||||
// beside `watershadow` (6.3), and colortex is not theirs.
|
||||
assert!(source("shadowcolor", false, true, false) == Source::ShadowColor(0));
|
||||
assert!(source("shadowcolor1", true, true, false) == Source::Color(1));
|
||||
assert!(source("colortex1", true, true, false) == Source::Dummy);
|
||||
assert!(source("shadow", false, true, false) == Source::ShadowDepth(0, false));
|
||||
assert!(source("shadow", false, true, true) == Source::ShadowDepth(1, false));
|
||||
assert!(source("watershadow", false, true, true) == Source::ShadowDepth(0, false));
|
||||
assert!(source("shadowtex1HW", false, true, false) == Source::ShadowDepth(1, true));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
//! A shader pack's shadow buffers (spec 6.3): the shadow map's depth (`shadowtex0`, and
|
||||
//! `shadowtex1` without translucent geometry), and the colour buffers `shadowcolor0/1`, each
|
||||
//! with two sides that `shadowcomp` programs flip as composite programs flip colortex (6.1.6).
|
||||
//! Square, `shadowMapResolution` pixels.
|
||||
|
||||
/// What the shadow pass writes and the programs read.
|
||||
pub(super) struct ShadowTargets {
|
||||
pub size: u32,
|
||||
/// `shadowtex0`, written by the shadow pass.
|
||||
pub depth: (wgpu::Texture, wgpu::TextureView),
|
||||
/// `shadowtex1`: a copy of `shadowtex0` taken before translucent terrain, when read.
|
||||
pub depth1: Option<(wgpu::Texture, wgpu::TextureView)>,
|
||||
/// Both sides of each shadowcolor buffer used.
|
||||
pub color: [Option<[wgpu::TextureView; 2]>; 2],
|
||||
/// The clear colour of each buffer cleared every frame.
|
||||
pub clears: [Option<[f32; 4]>; 2],
|
||||
/// Bit n set: B is shadowcolor n's "main".
|
||||
pub main: u16,
|
||||
/// Depth comparison for `sampler2DShadow` (hardware filtering): linear, as GL filters it.
|
||||
pub comparison: wgpu::Sampler,
|
||||
}
|
||||
|
||||
fn target(
|
||||
device: &wgpu::Device,
|
||||
label: &str,
|
||||
size: u32,
|
||||
format: wgpu::TextureFormat,
|
||||
copy: wgpu::TextureUsages,
|
||||
) -> (wgpu::Texture, wgpu::TextureView) {
|
||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some(label),
|
||||
size: wgpu::Extent3d {
|
||||
width: size,
|
||||
height: size,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format,
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT | copy,
|
||||
view_formats: &[],
|
||||
});
|
||||
let view = texture.create_view(&Default::default());
|
||||
(texture, view)
|
||||
}
|
||||
|
||||
impl ShadowTargets {
|
||||
/// The buffers of `shadow` the pack uses: `colors` (bit n: shadowcolor n), `depth1` when
|
||||
/// some program reads `shadowtex1`. The resolution is kept within the device's limit.
|
||||
pub fn new(
|
||||
device: &wgpu::Device,
|
||||
shadow: &horizon_shaders::Shadow,
|
||||
formats: [wgpu::TextureFormat; 2],
|
||||
colors: u16,
|
||||
depth1: bool,
|
||||
) -> Self {
|
||||
let size = shadow
|
||||
.resolution
|
||||
.clamp(1, device.limits().max_texture_dimension_2d);
|
||||
let copy = wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST;
|
||||
let depth = wgpu::TextureFormat::Depth32Float;
|
||||
let color = std::array::from_fn(|n| {
|
||||
(colors & 1 << n != 0).then(|| {
|
||||
[0, 1].map(|side| {
|
||||
let label = format!("Shader pack shadowcolor{n} {}", ["A", "B"][side]);
|
||||
target(
|
||||
device,
|
||||
&label,
|
||||
size,
|
||||
formats[n],
|
||||
wgpu::TextureUsages::empty(),
|
||||
)
|
||||
.1
|
||||
})
|
||||
})
|
||||
});
|
||||
Self {
|
||||
size,
|
||||
depth: target(device, "Shader pack shadowtex0", size, depth, copy),
|
||||
depth1: depth1.then(|| target(device, "Shader pack shadowtex1", size, depth, copy)),
|
||||
color,
|
||||
clears: std::array::from_fn(|n| {
|
||||
let b = &shadow.colors[n];
|
||||
b.clear.then(|| b.clear_color.unwrap_or([1.0; 4]))
|
||||
}),
|
||||
main: 0,
|
||||
comparison: device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
label: Some("Shader pack shadow comparison"),
|
||||
compare: Some(wgpu::CompareFunction::LessEqual),
|
||||
mag_filter: wgpu::FilterMode::Linear,
|
||||
min_filter: wgpu::FilterMode::Linear,
|
||||
..Default::default()
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Side `alt` (of "main" and "alt") of shadowcolor `n`.
|
||||
pub fn color(&self, n: usize, alt: bool) -> Option<&wgpu::TextureView> {
|
||||
let side = usize::from(self.main & 1 << n != 0) ^ usize::from(alt);
|
||||
self.color.get(n)?.as_ref().map(|c| &c[side])
|
||||
}
|
||||
|
||||
/// Clears the shadow map and the shadowcolor buffers cleared every frame (6.3).
|
||||
pub fn clear(&self, encoder: &mut wgpu::CommandEncoder) {
|
||||
let attachments: Vec<_> = (0..2)
|
||||
.filter_map(|n| {
|
||||
let c = self.clears[n]?;
|
||||
Some(Some(wgpu::RenderPassColorAttachment {
|
||||
view: self.color(n, false)?,
|
||||
depth_slice: None,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(wgpu::Color {
|
||||
r: c[0].into(),
|
||||
g: c[1].into(),
|
||||
b: c[2].into(),
|
||||
a: c[3].into(),
|
||||
}),
|
||||
store: wgpu::StoreOp::Store,
|
||||
},
|
||||
}))
|
||||
})
|
||||
.collect();
|
||||
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("Shader pack shadow clear"),
|
||||
color_attachments: &attachments,
|
||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||
view: &self.depth.1,
|
||||
depth_ops: Some(wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(1.0),
|
||||
store: wgpu::StoreOp::Store,
|
||||
}),
|
||||
stencil_ops: None,
|
||||
}),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
|
||||
/// `shadowtex1`: the shadow map as it is now (before translucent geometry).
|
||||
pub fn copy_depth1(&self, encoder: &mut wgpu::CommandEncoder) {
|
||||
if let Some((copy, _)) = &self.depth1 {
|
||||
encoder.copy_texture_to_texture(
|
||||
self.depth.0.as_image_copy(),
|
||||
copy.as_image_copy(),
|
||||
self.depth.0.size(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user