Shader packs, clean room 14: terrain drawn by the pack's program
With a pack whose gbuffers_terrain (or a fallback) loads, solid and cutout terrain is drawn by it into the pack's buffers, in place, between prepare and deferred, and Horizon's own solid terrain draw is skipped. - depthtex0 is now a Depth32Float attachment: the blit writes the world's depth there (GL window depth, GL rows), terrain tests and writes against it, so entities and terrain occlude each other; later programs sample it as before. - The vertex glue reads Horizon's TerrainVertex on both draw paths (instanced, and flat with the page tables in bind group 1) and gives atlas coordinates, lightmap coordinates in 0-240, the vertex colour, mc_midTexCoord at the sprite's centre and mc_Entity = -1. Normals, tangents and block ids come later. - texture/gtexture/tex sample the block atlas, lightmap the lightmap; atlasSize is set. - Occlusion culling is off while a pack draws terrain (it reads the depth of Horizon's terrain draw), and flat draws stay on. - Uniforms a program reads that are not provided yet are reported. - Program building is shared by quads and terrain (build_pass, bind). - terrain-test: Horizon's own pack for this slice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -42,7 +42,8 @@ impl Game<'_> {
|
||||
);
|
||||
}
|
||||
let folder = dimension_folder(&pack, dimension);
|
||||
let post = load_post(&pack, &folder, &properties, ¯os, None);
|
||||
let terrain = renderer.pack_terrain_inputs();
|
||||
let post = load_post(&pack, &folder, &properties, ¯os, terrain.as_ref());
|
||||
if let Some((programs, notes)) = renderer.set_shader_pack(dimension, Some(post))
|
||||
{
|
||||
for note in notes {
|
||||
|
||||
@@ -471,6 +471,8 @@ pub struct Renderer {
|
||||
pack_post: Option<(i32, Option<pack_post::PackPost>)>,
|
||||
/// The block sprites as one atlas, while a shader pack is loaded.
|
||||
pack_atlas: Option<pack_atlas::PackAtlas>,
|
||||
/// The pack's terrain program reads flat draws' vertices (`pack_terrain_inputs`).
|
||||
pack_terrain_flat: bool,
|
||||
pub hud: HudRenderer,
|
||||
/// Vanilla's menu background blur, made on first use.
|
||||
menu_blur: Option<crate::menu_blur::MenuBlur>,
|
||||
@@ -2319,6 +2321,7 @@ impl Renderer {
|
||||
world_post,
|
||||
pack_post: None,
|
||||
pack_atlas: None,
|
||||
pack_terrain_flat: false,
|
||||
hud,
|
||||
menu_blur: None,
|
||||
info,
|
||||
@@ -2744,17 +2747,50 @@ impl Renderer {
|
||||
dimension: i32,
|
||||
pack: Option<horizon_shaders::Post>,
|
||||
) -> Option<(Vec<String>, Vec<String>)> {
|
||||
if pack.is_none() {
|
||||
self.pack_atlas = None;
|
||||
}
|
||||
let instance = wgpu::vertex_attr_array![5=>Float32x4];
|
||||
let vertex = wgpu::VertexBufferLayout {
|
||||
array_stride: std::mem::size_of::<TerrainVertex>() as u64,
|
||||
step_mode: wgpu::VertexStepMode::Vertex,
|
||||
attributes: &TERRAIN_ATTRIBUTES,
|
||||
};
|
||||
let instances = wgpu::VertexBufferLayout {
|
||||
array_stride: 16,
|
||||
step_mode: wgpu::VertexStepMode::Instance,
|
||||
attributes: &instance,
|
||||
};
|
||||
let buffers = [Some(vertex), Some(instances)];
|
||||
let flat = self.pack_terrain_flat;
|
||||
let post = pack.map(|post| {
|
||||
let world = pack_post::World {
|
||||
atlas: self.pack_atlas.as_ref().map(|a| (a.view.clone(), a.size())),
|
||||
lightmap: self.lightmap_view.clone(),
|
||||
terrain: self.pack_atlas.as_ref().map(|_| pack_post::Geometry {
|
||||
buffers: if flat { &buffers[..1] } else { &buffers },
|
||||
table: self.flat.as_ref().filter(|_| flat).map(|f| &f.draw_layout),
|
||||
}),
|
||||
};
|
||||
pack_post::PackPost::new(
|
||||
&self.device,
|
||||
&self.queue,
|
||||
post,
|
||||
self.config.format.remove_srgb_suffix(),
|
||||
world,
|
||||
)
|
||||
});
|
||||
if post.is_none() {
|
||||
self.pack_atlas = None;
|
||||
} else if self.pack_atlas.is_none() {
|
||||
let report = post.as_ref().map(|p| {
|
||||
let programs = p.programs().into_iter().map(str::to_owned).collect();
|
||||
(programs, p.notes.clone())
|
||||
});
|
||||
self.pack_post = Some((dimension, post));
|
||||
report
|
||||
}
|
||||
/// What a shader pack's terrain program reads from Horizon's terrain vertices, in the
|
||||
/// pack's block atlas (made here when missing); `None` when the atlas does not fit.
|
||||
pub fn pack_terrain_inputs(&mut self) -> Option<horizon_shaders::GbuffersInputs> {
|
||||
if self.pack_atlas.is_none() {
|
||||
let mut encoder = self.device.create_command_encoder(&Default::default());
|
||||
let layers = self
|
||||
.sprite_textures
|
||||
@@ -2771,12 +2807,13 @@ impl Renderer {
|
||||
);
|
||||
self.queue.submit([encoder.finish()]);
|
||||
}
|
||||
let report = post.as_ref().map(|p| {
|
||||
let programs = p.programs().into_iter().map(str::to_owned).collect();
|
||||
(programs, p.notes.clone())
|
||||
});
|
||||
self.pack_post = Some((dimension, post));
|
||||
report
|
||||
// Flat draws whenever the device has them (`render` keeps them on with a pack).
|
||||
self.pack_terrain_flat = self.flat.is_some() && self.flat_on;
|
||||
Some(
|
||||
self.pack_atlas
|
||||
.as_ref()?
|
||||
.terrain_inputs(self.pack_terrain_flat),
|
||||
)
|
||||
}
|
||||
/// The dimension the shader pack was last set for.
|
||||
pub fn shader_pack_dimension(&self) -> Option<i32> {
|
||||
@@ -3184,11 +3221,23 @@ impl Renderer {
|
||||
// Occlusion culling leaves the sections its last result did not see
|
||||
// to phase 2, after the others, with instance counts of 0. Whether
|
||||
// the frame has draws enough goes by the last one's.
|
||||
// A shader pack drawing terrain draws all of it: occlusion culling reads the depth of
|
||||
// Horizon's own terrain draw.
|
||||
let pack_terrain = !frame.title
|
||||
&& self
|
||||
.pack_post
|
||||
.as_ref()
|
||||
.and_then(|p| p.1.as_ref())
|
||||
.is_some_and(|p| p.has_terrain());
|
||||
let mut cull = self
|
||||
.cull
|
||||
.as_mut()
|
||||
.filter(|c| {
|
||||
self.cull_on && !frame.title && self.multi_draw && self.section_draws >= c.min_draws
|
||||
self.cull_on
|
||||
&& !frame.title
|
||||
&& !pack_terrain
|
||||
&& self.multi_draw
|
||||
&& self.section_draws >= c.min_draws
|
||||
})
|
||||
.and_then(|cull| cull.begin(&self.device).then_some(cull));
|
||||
self.phase2_draws.clear();
|
||||
@@ -3591,7 +3640,9 @@ impl Renderer {
|
||||
}
|
||||
let flat_frame = match &mut self.flat {
|
||||
Some(flat)
|
||||
if self.flat_on && !self.draws.is_empty() && self.draws.len() >= flat.min_draws =>
|
||||
if self.flat_on
|
||||
&& !self.draws.is_empty()
|
||||
&& (self.draws.len() >= flat.min_draws || pack_terrain) =>
|
||||
{
|
||||
let sections = &self.sections;
|
||||
let lods = &self.lods;
|
||||
@@ -3651,6 +3702,8 @@ impl Renderer {
|
||||
}
|
||||
// The world's targets: the screen, or the scaled target of `post`.
|
||||
let pack_post = !frame.title && self.pack_post.as_ref().is_some_and(|p| p.1.is_some());
|
||||
// The pack's program reads the vertices this frame's draws bind.
|
||||
let pack_draws_terrain = pack_terrain && flat_frame == self.pack_terrain_flat;
|
||||
let scaled = self.world_post.prepare(
|
||||
&self.device,
|
||||
[self.config.width, self.config.height],
|
||||
@@ -3739,7 +3792,14 @@ impl Renderer {
|
||||
pass.draw(0..3, 0..1);
|
||||
}
|
||||
}
|
||||
match self.flat.as_ref().filter(|_| flat_frame) {
|
||||
match self
|
||||
.flat
|
||||
.as_ref()
|
||||
.filter(|_| flat_frame && !pack_draws_terrain)
|
||||
{
|
||||
_ if pack_draws_terrain => {
|
||||
pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
|
||||
}
|
||||
Some(flat) => {
|
||||
pass.set_pipeline(&flat.solid);
|
||||
let vertices = |p| self.arena.buffer(p);
|
||||
@@ -3947,9 +4007,27 @@ impl Renderer {
|
||||
}
|
||||
if let Some((color, depth)) = &scaled
|
||||
&& pack_post
|
||||
&& let Some((_, Some(post))) = &mut self.pack_post
|
||||
&& let Some((dimension, Some(mut post))) = self.pack_post.take()
|
||||
{
|
||||
let size = self.world_post.size().unwrap_or([1, 1]);
|
||||
let flat = self.flat.as_ref().filter(|_| flat_frame);
|
||||
let mut terrain = |rp: &mut wgpu::RenderPass<'_>| {
|
||||
if !pack_draws_terrain {
|
||||
return;
|
||||
}
|
||||
match flat {
|
||||
Some(flat) => {
|
||||
flat.draw(rp, &flat.calls[..flat.solid_calls], |p| {
|
||||
self.arena.buffer(p)
|
||||
});
|
||||
}
|
||||
None => {
|
||||
rp.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32);
|
||||
rp.set_vertex_buffer(1, self.instances.slice(..));
|
||||
self.draw_sections(rp, &self.indirect, 0, 0, solid_draws);
|
||||
}
|
||||
}
|
||||
};
|
||||
post.run(
|
||||
&self.device,
|
||||
&self.queue,
|
||||
@@ -3961,7 +4039,9 @@ impl Renderer {
|
||||
&frame.pack,
|
||||
size,
|
||||
[self.config.width, self.config.height],
|
||||
&mut terrain,
|
||||
);
|
||||
self.pack_post = Some((dimension, Some(post)));
|
||||
} else if scaled.is_some() {
|
||||
self.world_post.finish(&mut encoder, &view_target);
|
||||
}
|
||||
|
||||
@@ -73,7 +73,8 @@ pub struct Flat {
|
||||
phase2_call_data: Vec<[u32; 5]>,
|
||||
/// The copy of phase 2's draws, and its ranges.
|
||||
phase2_group: Option<(wgpu::BindGroup, u32)>,
|
||||
draw_layout: wgpu::BindGroupLayout,
|
||||
/// The layout of a page's bind group 1 (shader packs' terrain pipelines share it).
|
||||
pub(super) draw_layout: wgpu::BindGroupLayout,
|
||||
blocks_per_page: u64,
|
||||
pages: Vec<Option<PageTable>>,
|
||||
free_ids: Vec<u32>,
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
//! layer is copied into a grid, row by row from the top left, and an animated layer again
|
||||
//! whenever its frame changes.
|
||||
|
||||
#[expect(dead_code, reason = "the gbuffers pass binds it (next slice)")]
|
||||
pub(super) struct PackAtlas {
|
||||
texture: wgpu::Texture,
|
||||
pub view: wgpu::TextureView,
|
||||
@@ -64,10 +63,6 @@ impl PackAtlas {
|
||||
}
|
||||
|
||||
/// The atlas's size in pixels (`atlasSize`).
|
||||
#[cfg_attr(
|
||||
not(test),
|
||||
expect(dead_code, reason = "the gbuffers pass reads it (next slice)")
|
||||
)]
|
||||
pub fn size(&self) -> [u32; 2] {
|
||||
[self.texture.width(), self.texture.height()]
|
||||
}
|
||||
@@ -118,6 +113,70 @@ impl PackAtlas {
|
||||
}
|
||||
}
|
||||
|
||||
impl PackAtlas {
|
||||
/// The vertex inputs of a gbuffers program drawing terrain (`TerrainVertex`, terrain.wgsl's
|
||||
/// `vs_terrain` or, for `flat` draws, `vs_terrain_flat`), in atlas coordinates.
|
||||
pub fn terrain_inputs(&self, flat: bool) -> horizon_shaders::GbuffersInputs {
|
||||
let mut declarations = String::from(
|
||||
"layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in uvec2 hz_v_uv;\n\
|
||||
layout(location = 2) in uint hz_v_layer;\nlayout(location = 3) in vec4 hz_v_color;\n\
|
||||
layout(location = 4) in uvec2 hz_v_light;\n",
|
||||
);
|
||||
let origin = if flat {
|
||||
declarations.push_str(
|
||||
"layout(set = 1, binding = 0) readonly buffer HzBlockSlots { uint hz_block_slots[]; };\n\
|
||||
layout(set = 1, binding = 1) readonly buffer HzSlotOrigins { vec4 hz_slot_origins[]; };\n",
|
||||
);
|
||||
"hz_slot_origins[hz_block_slots[uint(gl_VertexIndex) >> 8u]]"
|
||||
} else {
|
||||
declarations.push_str("layout(location = 5) in vec4 hz_v_origin;\n");
|
||||
"hz_v_origin"
|
||||
};
|
||||
let [width, height] = self.size();
|
||||
let scale = [
|
||||
self.sprite as f32 / width as f32,
|
||||
self.sprite as f32 / height as f32,
|
||||
];
|
||||
let columns = self.columns;
|
||||
// UVs past a sprite (models up to 2, distant tiles) cannot repeat in an atlas: clamped.
|
||||
let body = format!(
|
||||
" vec4 hz_origin = {origin};\n \
|
||||
uint hz_layer = hz_v_layer & 32767u;\n \
|
||||
hz_cell = vec2(float(hz_layer % {columns}u), float(hz_layer / {columns}u));\n \
|
||||
hz_scale = vec2({:?}, {:?});\n \
|
||||
hz_Vertex = vec4(hz_v_pos + hz_origin.xyz, 1.0);\n \
|
||||
hz_MultiTexCoord0 = vec4((hz_cell + clamp(vec2(hz_v_uv) * hz_origin.w, 0.0, 1.0)) * hz_scale, 0.0, 1.0);\n \
|
||||
hz_MultiTexCoord1 = vec4(float(hz_v_light.y), float(hz_v_light.x), 0.0, 1.0);\n \
|
||||
hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
|
||||
hz_Color = hz_v_color;\n \
|
||||
hz_Normal = vec3(0.0, 1.0, 0.0);\n",
|
||||
scale[0], scale[1],
|
||||
);
|
||||
declarations.push_str("vec2 hz_cell; vec2 hz_scale;\n");
|
||||
let attributes = [
|
||||
("mc_Entity", "vec4(-1.0, -1.0, 0.0, 0.0)"),
|
||||
(
|
||||
"mc_midTexCoord",
|
||||
"vec4((hz_cell + 0.5) * hz_scale, 0.0, 1.0)",
|
||||
),
|
||||
("at_tangent", "vec4(1.0, 0.0, 0.0, 1.0)"),
|
||||
("vaPosition", "hz_Vertex.xyz"),
|
||||
("vaUV0", "hz_MultiTexCoord0.xy"),
|
||||
("vaUV2", "hz_MultiTexCoord1.xy"),
|
||||
("vaColor", "hz_Color"),
|
||||
("vaNormal", "hz_Normal"),
|
||||
];
|
||||
horizon_shaders::GbuffersInputs {
|
||||
declarations,
|
||||
body,
|
||||
attributes: attributes
|
||||
.iter()
|
||||
.map(|&(n, v)| (n.to_owned(), v.to_owned()))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -29,6 +29,9 @@ enum Source {
|
||||
Dummy,
|
||||
/// A 1×1 depth of 1.0 behind a comparison sampler, until shadow maps exist: no shadow.
|
||||
Shadow,
|
||||
/// The block atlas (pack_atlas.rs), in gbuffers programs.
|
||||
Atlas,
|
||||
Lightmap,
|
||||
}
|
||||
|
||||
struct Binding {
|
||||
@@ -66,9 +69,22 @@ struct Targets {
|
||||
depth: wgpu::TextureView,
|
||||
}
|
||||
|
||||
/// What a pack's gbuffers programs read and draw from the game's world.
|
||||
pub(super) struct World<'a> {
|
||||
/// The block atlas and its size.
|
||||
pub atlas: Option<(wgpu::TextureView, [u32; 2])>,
|
||||
pub lightmap: wgpu::TextureView,
|
||||
/// Terrain's geometry, when the pack draws it.
|
||||
pub terrain: Option<Geometry<'a>>,
|
||||
}
|
||||
|
||||
pub struct PackPost {
|
||||
pub notes: Vec<String>,
|
||||
passes: Vec<Pass>,
|
||||
/// What draws terrain (`gbuffers_terrain` or its fallback), between `prepare` and `deferred`.
|
||||
terrain: Option<Pass>,
|
||||
atlas: Option<wgpu::TextureView>,
|
||||
lightmap: wgpu::TextureView,
|
||||
deferred_pre: u16,
|
||||
composite_pre: u16,
|
||||
formats: [wgpu::TextureFormat; BUFFERS],
|
||||
@@ -85,6 +101,7 @@ pub struct PackPost {
|
||||
linear: wgpu::Sampler,
|
||||
nearest: wgpu::Sampler,
|
||||
repeat: wgpu::Sampler,
|
||||
nearest_repeat: wgpu::Sampler,
|
||||
blit: (wgpu::RenderPipeline, wgpu::BindGroupLayout),
|
||||
/// The blit also writes depthtex0 (some program reads it) and clears these buffers.
|
||||
blit_depth: bool,
|
||||
@@ -133,6 +150,8 @@ fn source(name: &str) -> Source {
|
||||
match name {
|
||||
"depthtex0" | "gdepthtex" | "depthtex1" | "depthtex2" => Source::Depth,
|
||||
"noisetex" => Source::Noise,
|
||||
"texture" | "gtexture" | "tex" => Source::Atlas,
|
||||
"lightmap" => Source::Lightmap,
|
||||
n => horizon_shaders::buffer_index(n).map_or(Source::Dummy, Source::Color),
|
||||
}
|
||||
}
|
||||
@@ -228,6 +247,7 @@ fn helper(
|
||||
entry: &str,
|
||||
entries: &[wgpu::BindGroupLayoutEntry],
|
||||
targets: &[Option<wgpu::ColorTargetState>],
|
||||
depth: Option<wgpu::DepthStencilState>,
|
||||
) -> (wgpu::RenderPipeline, wgpu::BindGroupLayout) {
|
||||
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some("Shader pack helpers"),
|
||||
@@ -252,7 +272,7 @@ fn helper(
|
||||
buffers: &[],
|
||||
},
|
||||
primitive: Default::default(),
|
||||
depth_stencil: None,
|
||||
depth_stencil: depth,
|
||||
multisample: Default::default(),
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &module,
|
||||
@@ -306,6 +326,211 @@ fn attach(
|
||||
})
|
||||
}
|
||||
|
||||
/// The game's geometry a gbuffers program draws: its vertex buffers and, for flat draws
|
||||
/// (flat.rs), the layout of their bind group 1.
|
||||
pub(super) struct Geometry<'a> {
|
||||
pub buffers: &'a [Option<wgpu::VertexBufferLayout<'a>>],
|
||||
pub table: Option<&'a wgpu::BindGroupLayout>,
|
||||
}
|
||||
|
||||
/// 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.
|
||||
fn build_pass(
|
||||
device: &wgpu::Device,
|
||||
p: horizon_shaders::PostProgram,
|
||||
formats: &[wgpu::TextureFormat; BUFFERS],
|
||||
screen: wgpu::TextureFormat,
|
||||
notes: &mut Vec<String>,
|
||||
geometry: Option<&Geometry<'_>>,
|
||||
) -> Option<Pass> {
|
||||
let features = device.features();
|
||||
let mask = |flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0);
|
||||
let interface = &p.program.interface;
|
||||
let mut entries = vec![entry(
|
||||
0,
|
||||
wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Uniform,
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
)];
|
||||
let mut samplers = Vec::new();
|
||||
let mut reads = 0u16;
|
||||
for s in &interface.samplers {
|
||||
let mut src = source(&s.name);
|
||||
if src == Source::Atlas && geometry.is_none() {
|
||||
src = Source::Dummy;
|
||||
}
|
||||
let float = wgpu::TextureSampleType::Float { filterable: true };
|
||||
let mut sample_type = match src {
|
||||
Source::Color(n) => formats[n]
|
||||
.sample_type(None, Some(features))
|
||||
.unwrap_or(float),
|
||||
Source::Depth => 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);
|
||||
}
|
||||
let wanted = match s.ty.as_str() {
|
||||
"sampler2DShadow" => true,
|
||||
"sampler2D" => !int,
|
||||
"isampler2D" => sample_type == wgpu::TextureSampleType::Sint,
|
||||
"usampler2D" => sample_type == wgpu::TextureSampleType::Uint,
|
||||
_ => false,
|
||||
};
|
||||
if !wanted {
|
||||
notes.push(format!(
|
||||
"{}: `{} {}` is not provided yet",
|
||||
p.name, s.ty, s.name
|
||||
));
|
||||
(src, sample_type) = (Source::Dummy, float);
|
||||
}
|
||||
let filtering = sample_type == float;
|
||||
if let Source::Color(n) = src {
|
||||
reads |= 1 << n;
|
||||
}
|
||||
entries.push(texture_entry(s.binding, sample_type));
|
||||
entries.push(entry(
|
||||
s.binding + 1,
|
||||
wgpu::BindingType::Sampler(if src == Source::Shadow {
|
||||
wgpu::SamplerBindingType::Comparison
|
||||
} else if filtering {
|
||||
wgpu::SamplerBindingType::Filtering
|
||||
} else {
|
||||
wgpu::SamplerBindingType::NonFiltering
|
||||
}),
|
||||
));
|
||||
samplers.push(Binding {
|
||||
source: src,
|
||||
binding: s.binding,
|
||||
filtering,
|
||||
});
|
||||
}
|
||||
let final_pass = p.group == Group::Final;
|
||||
let mut targets = Vec::new();
|
||||
let mut writes = Vec::new();
|
||||
for &location in &interface.outputs {
|
||||
let l = location as usize;
|
||||
if targets.len() <= l {
|
||||
targets.resize(l + 1, None);
|
||||
}
|
||||
if final_pass {
|
||||
if location == 0 {
|
||||
targets[0] = target(screen);
|
||||
}
|
||||
} else if let Some(&buffer) = p.buffers.get(l) {
|
||||
targets[l] = target(formats[buffer as usize]);
|
||||
writes.push((location, buffer as usize));
|
||||
}
|
||||
}
|
||||
if !final_pass && writes.is_empty() {
|
||||
notes.push(format!("{}: writes no buffer; skipped", p.name));
|
||||
return None;
|
||||
}
|
||||
let written = writes.iter().fold(0u16, |m, w| m | 1 << w.1);
|
||||
let kept = p
|
||||
.flips
|
||||
.iter()
|
||||
.filter(|f| !f.1)
|
||||
.fold(0u16, |m, f| m | 1 << f.0);
|
||||
// Gbuffers programs write in place (6.1.6).
|
||||
let flip = if final_pass || geometry.is_some() {
|
||||
0
|
||||
} else {
|
||||
(written & !kept) | mask(&p.flips)
|
||||
};
|
||||
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some(&p.name),
|
||||
entries: &entries,
|
||||
});
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some(&p.name),
|
||||
bind_group_layouts: &[Some(&layout), geometry.and_then(|g| g.table)],
|
||||
..Default::default()
|
||||
});
|
||||
let created = checked(device, || {
|
||||
let module = |m: naga::Module| {
|
||||
device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some(&p.name),
|
||||
source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(m)),
|
||||
})
|
||||
};
|
||||
let (vs, fs) = (module(p.program.vertex), module(p.program.fragment));
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some(&p.name),
|
||||
layout: Some(&pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &vs,
|
||||
entry_point: Some("main"),
|
||||
compilation_options: Default::default(),
|
||||
buffers: geometry.map_or(&[], |g| g.buffers),
|
||||
},
|
||||
primitive: Default::default(),
|
||||
depth_stencil: geometry.map(|_| wgpu::DepthStencilState {
|
||||
format: wgpu::TextureFormat::Depth32Float,
|
||||
depth_write_enabled: Some(true),
|
||||
depth_compare: Some(wgpu::CompareFunction::LessEqual),
|
||||
stencil: Default::default(),
|
||||
bias: Default::default(),
|
||||
}),
|
||||
multisample: Default::default(),
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &fs,
|
||||
entry_point: Some("main"),
|
||||
compilation_options: Default::default(),
|
||||
targets: &targets,
|
||||
}),
|
||||
multiview_mask: None,
|
||||
cache: None,
|
||||
})
|
||||
});
|
||||
let pipeline = match created {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
notes.push(format!("{}: skipped: {e}", p.name));
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let interface = &p.program.interface;
|
||||
let missing: Vec<&str> = interface
|
||||
.uniforms
|
||||
.iter()
|
||||
.filter(|u| !u.name.starts_with("hz_") && pack_uniforms::index(&u.name).is_none())
|
||||
.map(|u| u.name.as_str())
|
||||
.collect();
|
||||
if !missing.is_empty() {
|
||||
notes.push(format!("{}: zero for now: {}", p.name, missing.join(" ")));
|
||||
}
|
||||
Some(Pass {
|
||||
ubo: device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some(&p.name),
|
||||
size: u64::from(interface.uniform_size.max(16)),
|
||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
}),
|
||||
block: vec![0; interface.uniform_size as usize],
|
||||
uniforms: interface
|
||||
.uniforms
|
||||
.iter()
|
||||
.map(|u| (pack_uniforms::index(&u.name), u.ty.clone(), u.offset))
|
||||
.collect(),
|
||||
name: p.name,
|
||||
group: p.group,
|
||||
pipeline,
|
||||
layout,
|
||||
samplers,
|
||||
writes,
|
||||
flip,
|
||||
reads,
|
||||
written,
|
||||
groups: HashMap::new(),
|
||||
})
|
||||
}
|
||||
|
||||
impl PackPost {
|
||||
/// Builds the pipelines of `post` for a screen of `screen` format.
|
||||
pub fn new(
|
||||
@@ -313,6 +538,7 @@ impl PackPost {
|
||||
queue: &wgpu::Queue,
|
||||
post: Post,
|
||||
screen: wgpu::TextureFormat,
|
||||
world: World<'_>,
|
||||
) -> Self {
|
||||
let features = device.features();
|
||||
let formats: [wgpu::TextureFormat; BUFFERS] =
|
||||
@@ -329,205 +555,56 @@ impl PackPost {
|
||||
let mask =
|
||||
|flips: &[(u32, bool)]| flips.iter().filter(|f| f.1).fold(0u16, |m, f| m | 1 << f.0);
|
||||
let mut used = 1u16;
|
||||
let mut passes = Vec::new();
|
||||
for p in post.programs {
|
||||
let interface = &p.program.interface;
|
||||
let mut entries = vec![entry(
|
||||
0,
|
||||
wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Uniform,
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
)];
|
||||
let mut samplers = Vec::new();
|
||||
let mut reads = 0u16;
|
||||
for s in &interface.samplers {
|
||||
let mut src = source(&s.name);
|
||||
let float = wgpu::TextureSampleType::Float { filterable: true };
|
||||
let mut sample_type = match src {
|
||||
Source::Color(n) => formats[n]
|
||||
.sample_type(None, Some(features))
|
||||
.unwrap_or(float),
|
||||
Source::Depth => wgpu::TextureSampleType::Float { filterable: false },
|
||||
_ => float,
|
||||
};
|
||||
let int = !matches!(sample_type, wgpu::TextureSampleType::Float { .. });
|
||||
if s.ty == "sampler2DShadow" {
|
||||
(src, sample_type) = (Source::Shadow, wgpu::TextureSampleType::Depth);
|
||||
}
|
||||
let wanted = match s.ty.as_str() {
|
||||
"sampler2DShadow" => true,
|
||||
"sampler2D" => !int,
|
||||
"isampler2D" => sample_type == wgpu::TextureSampleType::Sint,
|
||||
"usampler2D" => sample_type == wgpu::TextureSampleType::Uint,
|
||||
_ => false,
|
||||
};
|
||||
if !wanted {
|
||||
notes.push(format!(
|
||||
"{}: `{} {}` is not provided yet",
|
||||
p.name, s.ty, s.name
|
||||
));
|
||||
(src, sample_type) = (Source::Dummy, float);
|
||||
}
|
||||
let filtering = sample_type == float;
|
||||
if let Source::Color(n) = src {
|
||||
reads |= 1 << n;
|
||||
}
|
||||
entries.push(texture_entry(s.binding, sample_type));
|
||||
entries.push(entry(
|
||||
s.binding + 1,
|
||||
wgpu::BindingType::Sampler(if src == Source::Shadow {
|
||||
wgpu::SamplerBindingType::Comparison
|
||||
} else if filtering {
|
||||
wgpu::SamplerBindingType::Filtering
|
||||
} else {
|
||||
wgpu::SamplerBindingType::NonFiltering
|
||||
}),
|
||||
));
|
||||
samplers.push(Binding {
|
||||
source: src,
|
||||
binding: s.binding,
|
||||
filtering,
|
||||
});
|
||||
}
|
||||
let final_pass = p.group == Group::Final;
|
||||
let mut targets = Vec::new();
|
||||
let mut writes = Vec::new();
|
||||
for &location in &interface.outputs {
|
||||
let l = location as usize;
|
||||
if targets.len() <= l {
|
||||
targets.resize(l + 1, None);
|
||||
}
|
||||
if final_pass {
|
||||
if location == 0 {
|
||||
targets[0] = target(screen);
|
||||
}
|
||||
} else if let Some(&buffer) = p.buffers.get(l) {
|
||||
targets[l] = target(formats[buffer as usize]);
|
||||
writes.push((location, buffer as usize));
|
||||
}
|
||||
}
|
||||
if !final_pass && writes.is_empty() {
|
||||
notes.push(format!("{}: writes no buffer; skipped", p.name));
|
||||
continue;
|
||||
}
|
||||
let written = writes.iter().fold(0u16, |m, w| m | 1 << w.1);
|
||||
let kept = p
|
||||
.flips
|
||||
.iter()
|
||||
.filter(|f| !f.1)
|
||||
.fold(0u16, |m, f| m | 1 << f.0);
|
||||
let flip = if final_pass {
|
||||
0
|
||||
} else {
|
||||
(written & !kept) | mask(&p.flips)
|
||||
};
|
||||
used |= reads | written | flip;
|
||||
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some(&p.name),
|
||||
entries: &entries,
|
||||
});
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some(&p.name),
|
||||
bind_group_layouts: &[Some(&layout)],
|
||||
..Default::default()
|
||||
});
|
||||
let created = checked(device, || {
|
||||
let module = |m: naga::Module| {
|
||||
device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||
label: Some(&p.name),
|
||||
source: wgpu::ShaderSource::Naga(std::borrow::Cow::Owned(m)),
|
||||
})
|
||||
};
|
||||
let (vs, fs) = (module(p.program.vertex), module(p.program.fragment));
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some(&p.name),
|
||||
layout: Some(&pipeline_layout),
|
||||
vertex: wgpu::VertexState {
|
||||
module: &vs,
|
||||
entry_point: Some("main"),
|
||||
compilation_options: Default::default(),
|
||||
buffers: &[],
|
||||
},
|
||||
primitive: Default::default(),
|
||||
depth_stencil: None,
|
||||
multisample: Default::default(),
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &fs,
|
||||
entry_point: Some("main"),
|
||||
compilation_options: Default::default(),
|
||||
targets: &targets,
|
||||
}),
|
||||
multiview_mask: None,
|
||||
cache: None,
|
||||
})
|
||||
});
|
||||
let pipeline = match created {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
notes.push(format!("{}: skipped: {e}", p.name));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let interface = &p.program.interface;
|
||||
passes.push(Pass {
|
||||
ubo: device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some(&p.name),
|
||||
size: u64::from(interface.uniform_size.max(16)),
|
||||
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
}),
|
||||
block: vec![0; interface.uniform_size as usize],
|
||||
uniforms: interface
|
||||
.uniforms
|
||||
.iter()
|
||||
.map(|u| (pack_uniforms::index(&u.name), u.ty.clone(), u.offset))
|
||||
.collect(),
|
||||
name: p.name,
|
||||
group: p.group,
|
||||
pipeline,
|
||||
layout,
|
||||
samplers,
|
||||
writes,
|
||||
flip,
|
||||
reads,
|
||||
written,
|
||||
groups: HashMap::new(),
|
||||
});
|
||||
let mut build = |p: horizon_shaders::PostProgram, geometry: Option<&Geometry<'_>>| {
|
||||
let pass = build_pass(device, p, &formats, screen, &mut notes, geometry)?;
|
||||
used |= pass.reads | pass.written | pass.flip;
|
||||
Some(pass)
|
||||
};
|
||||
let terrain = post
|
||||
.terrain
|
||||
.zip(world.terrain.as_ref())
|
||||
.and_then(|(p, g)| build(p, Some(g)));
|
||||
let passes: Vec<Pass> = post
|
||||
.programs
|
||||
.into_iter()
|
||||
.filter_map(|p| build(p, None))
|
||||
.collect();
|
||||
if let Some(t) = &terrain {
|
||||
notes.push(format!("terrain draws with {}", t.name));
|
||||
}
|
||||
// A clear only matters when a program reads the buffer before one writes all of it
|
||||
// (composite passes cover their whole target); the others are skipped.
|
||||
// (composite passes cover their whole target, terrain does not); the others are skipped.
|
||||
let mut cleared = Vec::new();
|
||||
for b in 1..BUFFERS {
|
||||
let bit = 1u16 << b;
|
||||
let first = passes
|
||||
.iter()
|
||||
.find(|p| (p.reads | p.flip | p.written) & bit != 0);
|
||||
if clears[b].is_some()
|
||||
&& first.is_some_and(|p| p.reads & bit != 0 || p.written & bit == 0)
|
||||
{
|
||||
let touched = |p: &&Pass| (p.reads | p.flip | p.written) & bit != 0;
|
||||
let first = terrain.iter().find(touched).map(|_| true).or_else(|| {
|
||||
let p = passes.iter().find(touched)?;
|
||||
Some(p.reads & bit != 0 || p.written & bit == 0)
|
||||
});
|
||||
if clears[b].is_some() && first == Some(true) {
|
||||
cleared.push(b);
|
||||
}
|
||||
}
|
||||
let blit_depth = passes
|
||||
.iter()
|
||||
.any(|p| p.samplers.iter().any(|s| s.source == Source::Depth));
|
||||
let room = 8 - 1 - usize::from(blit_depth);
|
||||
// Terrain tests against the world's depth (entities...), so the blit copies it then.
|
||||
let blit_depth = terrain.is_some()
|
||||
|| passes
|
||||
.iter()
|
||||
.any(|p| p.samplers.iter().any(|s| s.source == Source::Depth));
|
||||
let room = 8 - 1;
|
||||
let other_clears = cleared.split_off(room.min(cleared.len()));
|
||||
let float = wgpu::TextureSampleType::Float { filterable: true };
|
||||
let mut blit_targets = vec![target(formats[0])];
|
||||
if blit_depth {
|
||||
blit_targets.push(target(wgpu::TextureFormat::R32Float));
|
||||
}
|
||||
let blit_targets = std::iter::once(target(formats[0]));
|
||||
// Cleared by the pass's load operation; the shader writes nothing there.
|
||||
blit_targets.extend(cleared.iter().map(|&b| {
|
||||
Some(wgpu::ColorTargetState {
|
||||
format: formats[b],
|
||||
blend: None,
|
||||
write_mask: wgpu::ColorWrites::empty(),
|
||||
})
|
||||
}));
|
||||
let blit_targets: Vec<_> = blit_targets
|
||||
.chain(cleared.iter().map(|&b| {
|
||||
Some(wgpu::ColorTargetState {
|
||||
format: formats[b],
|
||||
blend: None,
|
||||
write_mask: wgpu::ColorWrites::empty(),
|
||||
})
|
||||
}))
|
||||
.collect();
|
||||
let blit = helper(
|
||||
device,
|
||||
"vs_full",
|
||||
@@ -541,6 +618,14 @@ impl PackPost {
|
||||
texture_entry(1, wgpu::TextureSampleType::Depth),
|
||||
],
|
||||
&blit_targets,
|
||||
// GL's window depth, written for depthtex0 and the terrain's depth test.
|
||||
blit_depth.then_some(wgpu::DepthStencilState {
|
||||
format: wgpu::TextureFormat::Depth32Float,
|
||||
depth_write_enabled: Some(true),
|
||||
depth_compare: Some(wgpu::CompareFunction::Always),
|
||||
stencil: Default::default(),
|
||||
bias: Default::default(),
|
||||
}),
|
||||
);
|
||||
let copy = helper(
|
||||
device,
|
||||
@@ -554,11 +639,15 @@ impl PackPost {
|
||||
),
|
||||
],
|
||||
&[target(screen)],
|
||||
None,
|
||||
);
|
||||
let white = [255u8; 4];
|
||||
Self {
|
||||
notes,
|
||||
passes,
|
||||
terrain,
|
||||
atlas: world.atlas.as_ref().map(|a| a.0.clone()),
|
||||
lightmap: world.lightmap,
|
||||
deferred_pre: mask(&post.deferred_pre),
|
||||
composite_pre: mask(&post.composite_pre),
|
||||
formats,
|
||||
@@ -608,12 +697,153 @@ impl PackPost {
|
||||
wgpu::AddressMode::ClampToEdge,
|
||||
),
|
||||
repeat: sampler(device, wgpu::FilterMode::Linear, wgpu::AddressMode::Repeat),
|
||||
nearest_repeat: sampler(device, wgpu::FilterMode::Nearest, wgpu::AddressMode::Repeat),
|
||||
blit,
|
||||
copy,
|
||||
uniforms: FrameUniforms::default(),
|
||||
uniforms: {
|
||||
let mut u = FrameUniforms::default();
|
||||
u.atlas = world.atlas.map_or([0; 2], |a| a.1);
|
||||
u
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes `pass`'s uniforms for a target of `out` pixels; its bind group for the buffers'
|
||||
/// current mapping.
|
||||
fn bind(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
pass: &mut Pass,
|
||||
out: [u32; 2],
|
||||
) -> wgpu::BindGroup {
|
||||
let fog = self.uniforms.fog();
|
||||
// Our block members first (`hz_target`, `hz_fog_color`, `hz_fog`), then the pack's.
|
||||
let ours = [
|
||||
[
|
||||
out[0] as f32,
|
||||
out[1] as f32,
|
||||
1.0 / out[0] as f32,
|
||||
1.0 / out[1] as f32,
|
||||
],
|
||||
fog[0].to_array(),
|
||||
fog[1].to_array(),
|
||||
];
|
||||
let head: &[u8] = bytemuck::cast_slice(&ours);
|
||||
let n = head.len().min(pass.block.len());
|
||||
pass.block[..n].copy_from_slice(&head[..n]);
|
||||
for (index, ty, offset) in &pass.uniforms {
|
||||
if let Some(v) = index.and_then(|i| self.uniforms.values.get(i)) {
|
||||
v.write(ty, &mut pass.block, *offset as usize);
|
||||
}
|
||||
}
|
||||
if !pass.block.is_empty() {
|
||||
queue.write_buffer(&pass.ubo, 0, &pass.block);
|
||||
}
|
||||
let targets = self.targets.as_ref().expect("resized");
|
||||
let key = self.main & pass.reads;
|
||||
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 filter = if s.filtering {
|
||||
&self.linear
|
||||
} else {
|
||||
&self.nearest
|
||||
};
|
||||
let (texture, sampler) = match s.source {
|
||||
Source::Color(i) => (
|
||||
targets.color[i]
|
||||
.as_ref()
|
||||
.map_or(&self.dummy, |t| &t[usize::from(key & 1 << i != 0)]),
|
||||
filter,
|
||||
),
|
||||
Source::Depth => (&targets.depth, &self.nearest),
|
||||
Source::Noise => (&self.noise, &self.repeat),
|
||||
Source::Dummy => (&self.dummy, &self.linear),
|
||||
Source::Shadow => (&self.shadow.0, &self.shadow.1),
|
||||
// Blocks look as the game draws them: nearest texels.
|
||||
Source::Atlas => (
|
||||
self.atlas.as_ref().unwrap_or(&self.dummy),
|
||||
&self.nearest_repeat,
|
||||
),
|
||||
Source::Lightmap => (&self.lightmap, &self.linear),
|
||||
};
|
||||
entries.push(wgpu::BindGroupEntry {
|
||||
binding: s.binding,
|
||||
resource: wgpu::BindingResource::TextureView(texture),
|
||||
});
|
||||
entries.push(wgpu::BindGroupEntry {
|
||||
binding: s.binding + 1,
|
||||
resource: wgpu::BindingResource::Sampler(sampler),
|
||||
});
|
||||
}
|
||||
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some(&pass.name),
|
||||
layout: &pass.layout,
|
||||
entries: &entries,
|
||||
})
|
||||
});
|
||||
group.clone()
|
||||
}
|
||||
|
||||
/// Terrain with the pack's program into the buffers it writes, in place, tested against
|
||||
/// the world's depth; `draw` issues the game's draws.
|
||||
fn draw_terrain(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
size: [u32; 2],
|
||||
draw: &mut dyn FnMut(&mut wgpu::RenderPass<'_>),
|
||||
) {
|
||||
let Some(mut pass) = self.terrain.take() else {
|
||||
return;
|
||||
};
|
||||
let group = self.bind(device, queue, &mut pass, size);
|
||||
let targets = self.targets.as_ref().expect("resized");
|
||||
let load = wgpu::Operations {
|
||||
load: wgpu::LoadOp::Load,
|
||||
store: wgpu::StoreOp::Store,
|
||||
};
|
||||
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));
|
||||
}
|
||||
{
|
||||
let mut rp = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some(&pass.name),
|
||||
color_attachments: &attachments,
|
||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||
view: &targets.depth,
|
||||
depth_ops: Some(wgpu::Operations {
|
||||
load: wgpu::LoadOp::Load,
|
||||
store: wgpu::StoreOp::Store,
|
||||
}),
|
||||
stencil_ops: None,
|
||||
}),
|
||||
..Default::default()
|
||||
});
|
||||
rp.set_pipeline(&pass.pipeline);
|
||||
rp.set_bind_group(0, &group, &[]);
|
||||
draw(&mut rp);
|
||||
}
|
||||
self.terrain = Some(pass);
|
||||
}
|
||||
|
||||
/// Whether the pack draws terrain itself.
|
||||
pub fn has_terrain(&self) -> bool {
|
||||
self.terrain.is_some()
|
||||
}
|
||||
|
||||
/// The names of the programs that run, in order.
|
||||
pub fn programs(&self) -> Vec<&str> {
|
||||
self.passes.iter().map(|p| p.name.as_str()).collect()
|
||||
@@ -651,9 +881,9 @@ impl PackPost {
|
||||
self.targets = Some(Targets {
|
||||
size,
|
||||
color,
|
||||
depth: make("depthtex0", wgpu::TextureFormat::R32Float),
|
||||
depth: make("depthtex0", wgpu::TextureFormat::Depth32Float),
|
||||
});
|
||||
for pass in &mut self.passes {
|
||||
for pass in self.passes.iter_mut().chain(&mut self.terrain) {
|
||||
pass.groups.clear();
|
||||
}
|
||||
}
|
||||
@@ -673,6 +903,7 @@ impl PackPost {
|
||||
inputs: &PackInputs,
|
||||
size: [u32; 2],
|
||||
window: [u32; 2],
|
||||
terrain: &mut dyn FnMut(&mut wgpu::RenderPass<'_>),
|
||||
) {
|
||||
self.resize(device, size);
|
||||
self.uniforms.update(frame, inputs, size);
|
||||
@@ -751,9 +982,6 @@ impl PackPost {
|
||||
view(self.main, 0, false).expect("colortex0"),
|
||||
overwrite,
|
||||
)];
|
||||
if self.blit_depth {
|
||||
attachments.push(attach(&targets.depth, overwrite));
|
||||
}
|
||||
for &b in &self.blit_clears {
|
||||
attachments.push(attach(
|
||||
view(self.main, b, false).expect("used buffer"),
|
||||
@@ -763,6 +991,16 @@ impl PackPost {
|
||||
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("Shader pack world"),
|
||||
color_attachments: &attachments,
|
||||
depth_stencil_attachment: self.blit_depth.then_some(
|
||||
wgpu::RenderPassDepthStencilAttachment {
|
||||
view: &targets.depth,
|
||||
depth_ops: Some(wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(1.0),
|
||||
store: wgpu::StoreOp::Store,
|
||||
}),
|
||||
stencil_ops: None,
|
||||
},
|
||||
),
|
||||
..Default::default()
|
||||
});
|
||||
pass.set_pipeline(&self.blit.0);
|
||||
@@ -770,10 +1008,15 @@ impl PackPost {
|
||||
pass.draw(0..3, 0..1);
|
||||
}
|
||||
|
||||
let fog = self.uniforms.fog();
|
||||
let mut passes = std::mem::take(&mut self.passes);
|
||||
let mut seen = (false, false);
|
||||
let mut shown = false;
|
||||
for pass in &mut self.passes {
|
||||
let mut terrain_drawn = self.terrain.is_none();
|
||||
for pass in &mut passes {
|
||||
if !terrain_drawn && pass.group != Group::Prepare {
|
||||
terrain_drawn = true;
|
||||
self.draw_terrain(device, queue, encoder, size, terrain);
|
||||
}
|
||||
if pass.group == Group::Deferred && !seen.0 {
|
||||
seen.0 = true;
|
||||
self.main ^= self.deferred_pre;
|
||||
@@ -783,63 +1026,13 @@ impl PackPost {
|
||||
self.main ^= self.composite_pre;
|
||||
}
|
||||
let final_pass = pass.group == Group::Final;
|
||||
let out = if final_pass { window } else { size };
|
||||
// Our block members first (`hz_target`, `hz_fog_color`, `hz_fog`), then the pack's.
|
||||
let ours = [
|
||||
[
|
||||
out[0] as f32,
|
||||
out[1] as f32,
|
||||
1.0 / out[0] as f32,
|
||||
1.0 / out[1] as f32,
|
||||
],
|
||||
fog[0].to_array(),
|
||||
fog[1].to_array(),
|
||||
];
|
||||
let head: &[u8] = bytemuck::cast_slice(&ours);
|
||||
let n = head.len().min(pass.block.len());
|
||||
pass.block[..n].copy_from_slice(&head[..n]);
|
||||
for (index, ty, offset) in &pass.uniforms {
|
||||
if let Some(v) = index.and_then(|i| self.uniforms.values.get(i)) {
|
||||
v.write(ty, &mut pass.block, *offset as usize);
|
||||
}
|
||||
}
|
||||
if !pass.block.is_empty() {
|
||||
queue.write_buffer(&pass.ubo, 0, &pass.block);
|
||||
}
|
||||
let key = self.main & pass.reads;
|
||||
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 filter = if s.filtering {
|
||||
&self.linear
|
||||
} else {
|
||||
&self.nearest
|
||||
};
|
||||
let (texture, sampler) = match s.source {
|
||||
Source::Color(i) => (view(key, i, false).unwrap_or(&self.dummy), filter),
|
||||
Source::Depth => (&targets.depth, &self.nearest),
|
||||
Source::Noise => (&self.noise, &self.repeat),
|
||||
Source::Dummy => (&self.dummy, &self.linear),
|
||||
Source::Shadow => (&self.shadow.0, &self.shadow.1),
|
||||
};
|
||||
entries.push(wgpu::BindGroupEntry {
|
||||
binding: s.binding,
|
||||
resource: wgpu::BindingResource::TextureView(texture),
|
||||
});
|
||||
entries.push(wgpu::BindGroupEntry {
|
||||
binding: s.binding + 1,
|
||||
resource: wgpu::BindingResource::Sampler(sampler),
|
||||
});
|
||||
}
|
||||
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some(&pass.name),
|
||||
layout: &pass.layout,
|
||||
entries: &entries,
|
||||
})
|
||||
});
|
||||
let group = self.bind(device, queue, pass, if final_pass { window } else { size });
|
||||
let targets = self.targets.as_ref().expect("resized");
|
||||
let view = |i: usize| {
|
||||
targets.color[i]
|
||||
.as_ref()
|
||||
.map(|t| &t[usize::from(self.main & 1 << i == 0)])
|
||||
};
|
||||
let mut attachments: Vec<Option<wgpu::RenderPassColorAttachment>> = Vec::new();
|
||||
if final_pass {
|
||||
attachments.push(attach(screen, overwrite));
|
||||
@@ -850,8 +1043,7 @@ impl PackPost {
|
||||
if attachments.len() <= l {
|
||||
attachments.resize(l + 1, None);
|
||||
}
|
||||
attachments[l] =
|
||||
view(self.main, buffer, true).and_then(|v| attach(v, overwrite));
|
||||
attachments[l] = view(buffer).and_then(|v| attach(v, overwrite));
|
||||
}
|
||||
}
|
||||
{
|
||||
@@ -861,11 +1053,21 @@ impl PackPost {
|
||||
..Default::default()
|
||||
});
|
||||
rp.set_pipeline(&pass.pipeline);
|
||||
rp.set_bind_group(0, &*group, &[]);
|
||||
rp.set_bind_group(0, &group, &[]);
|
||||
rp.draw(0..6, 0..1);
|
||||
}
|
||||
self.main ^= pass.flip;
|
||||
}
|
||||
self.passes = passes;
|
||||
if !terrain_drawn {
|
||||
self.draw_terrain(device, queue, encoder, size, terrain);
|
||||
}
|
||||
let targets = self.targets.as_ref().expect("resized");
|
||||
let view = |m: u16, i: usize, alt: bool| {
|
||||
targets.color[i]
|
||||
.as_ref()
|
||||
.map(|t| &t[main(m, i) ^ usize::from(alt)])
|
||||
};
|
||||
if !shown {
|
||||
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("Shader pack colortex0"),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// Helpers of a shader pack's post-processing (pack_post.rs): the world into colortex0 and
|
||||
// depthtex0, and colortex0 onto the screen for packs without `final`.
|
||||
// depthtex0 (a depth attachment, for terrain's depth test), and colortex0 onto the screen for
|
||||
// packs without `final`.
|
||||
|
||||
@vertex
|
||||
fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> {
|
||||
@@ -12,7 +13,7 @@ fn vs_full(@builtin(vertex_index) i: u32) -> @builtin(position) vec4<f32> {
|
||||
|
||||
struct World {
|
||||
@location(0) color: vec4<f32>,
|
||||
@location(1) depth: vec4<f32>,
|
||||
@builtin(frag_depth) depth: f32,
|
||||
}
|
||||
|
||||
// Pack buffers keep GL's rows (row 0 at the bottom), and GL's window depth: Horizon's
|
||||
@@ -23,7 +24,7 @@ fn fs_blit(@builtin(position) p: vec4<f32>) -> World {
|
||||
let at = vec2<i32>(i32(p.x), size.y - 1 - i32(p.y));
|
||||
var out: World;
|
||||
out.color = textureLoad(source, at, 0);
|
||||
out.depth = vec4<f32>(1.0 - textureLoad(world_depth, at, 0), 0.0, 0.0, 1.0);
|
||||
out.depth = 1.0 - textureLoad(world_depth, at, 0);
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
@@ -140,11 +140,14 @@ pub const NAMES: &[&str] = &[
|
||||
"gbufferProjectionInverse",
|
||||
"gbufferPreviousModelView",
|
||||
"gbufferPreviousProjection",
|
||||
"atlasSize",
|
||||
];
|
||||
|
||||
/// Values carried from frame to frame.
|
||||
pub struct FrameUniforms {
|
||||
pub values: Vec<Value>,
|
||||
/// The block atlas's size (`atlasSize`), 0 without one.
|
||||
pub atlas: [u32; 2],
|
||||
start: std::time::Instant,
|
||||
last: Option<std::time::Instant>,
|
||||
frame: u32,
|
||||
@@ -157,6 +160,7 @@ impl Default for FrameUniforms {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
values: Vec::new(),
|
||||
atlas: [0; 2],
|
||||
start: std::time::Instant::now(),
|
||||
last: None,
|
||||
frame: 0,
|
||||
@@ -243,6 +247,7 @@ impl FrameUniforms {
|
||||
"worldDay" => Value::i(&[world_day as i32]),
|
||||
"moonPhase" => Value::i(&[world_day.rem_euclid(8) as i32]),
|
||||
"frameCounter" => Value::i(&[self.frame as i32]),
|
||||
"atlasSize" => Value::i(&[self.atlas[0] as i32, self.atlas[1] as i32]),
|
||||
"frameTime" => Value::f(&[dt]),
|
||||
"frameTimeCounter" => Value::f(&[seconds]),
|
||||
"viewWidth" => Value::f(&[size[0] as f32]),
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
# terrain-test
|
||||
|
||||
Horizon's own shader pack for testing gbuffers terrain (written for Horizon, no other pack's code).
|
||||
`gbuffers_terrain` draws blocks from the atlas with their vertex colour and lightmap, tinted by
|
||||
the lightmap coordinate's sky light (cooler where the sky is hidden); `mc_midTexCoord` marks each
|
||||
face's centre with a dot. `composite` puts the depth buffer's view-space distance into the blue
|
||||
channel of the far half. Copy the folder into `shaderpacks/` and run the game with
|
||||
`HORIZON_SHADER_PACK=terrain-test`.
|
||||
@@ -0,0 +1,19 @@
|
||||
#version 120
|
||||
|
||||
uniform sampler2D colortex0;
|
||||
uniform sampler2D depthtex0;
|
||||
uniform mat4 gbufferProjectionInverse;
|
||||
uniform float far;
|
||||
|
||||
varying vec2 texcoord;
|
||||
|
||||
void main() {
|
||||
vec3 color = texture2D(colortex0, texcoord).rgb;
|
||||
if (texcoord.x > 0.5) {
|
||||
float depth = texture2D(depthtex0, texcoord).r;
|
||||
vec4 view = gbufferProjectionInverse * vec4(vec3(texcoord, depth) * 2.0 - 1.0, 1.0);
|
||||
color.b = clamp(length(view.xyz / view.w) / far, 0.0, 1.0);
|
||||
}
|
||||
/* DRAWBUFFERS:0 */
|
||||
gl_FragData[0] = vec4(color, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#version 120
|
||||
|
||||
varying vec2 texcoord;
|
||||
|
||||
void main() {
|
||||
gl_Position = ftransform();
|
||||
texcoord = gl_MultiTexCoord0.xy;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#version 120
|
||||
|
||||
uniform sampler2D texture;
|
||||
uniform sampler2D lightmap;
|
||||
uniform ivec2 atlasSize;
|
||||
|
||||
varying vec2 texcoord;
|
||||
varying vec2 lmcoord;
|
||||
varying vec2 mid;
|
||||
varying vec4 glcolor;
|
||||
|
||||
void main() {
|
||||
vec4 color = texture2D(texture, texcoord) * glcolor;
|
||||
if (color.a < 0.1) discard;
|
||||
color.rgb *= texture2D(lightmap, lmcoord).rgb;
|
||||
// Less sky light: bluer.
|
||||
color.rgb = mix(color.rgb * vec3(0.6, 0.7, 1.2), color.rgb, lmcoord.y);
|
||||
// A dot at the face's centre, one texel wide.
|
||||
if (length((texcoord - mid) * vec2(atlasSize)) < 1.0) color.rgb = vec3(1.0, 0.0, 1.0);
|
||||
/* DRAWBUFFERS:0 */
|
||||
gl_FragData[0] = color;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#version 120
|
||||
|
||||
attribute vec4 mc_midTexCoord;
|
||||
|
||||
varying vec2 texcoord;
|
||||
varying vec2 lmcoord;
|
||||
varying vec2 mid;
|
||||
varying vec4 glcolor;
|
||||
|
||||
void main() {
|
||||
gl_Position = ftransform();
|
||||
texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
|
||||
lmcoord = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;
|
||||
mid = mc_midTexCoord.xy;
|
||||
glcolor = gl_Color;
|
||||
}
|
||||
Reference in New Issue
Block a user