Horizon shader packs: one frame uniform per pack, apart from its post passes

`NativePack` holds what a running pack needs: the `frame` every stage
reads (`NativeFrame`, written once a frame before the world draws), its
post passes and the timer of its passes. `NativePost` no longer owns the
frame buffer or the timer, so the sky stage can read the same frame and
be timed by the same timer. No change in what draws.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-26 08:42:46 +01:00
parent 70b832d658
commit a68c2f587d
3 changed files with 180 additions and 102 deletions
+41 -32
View File
@@ -67,6 +67,7 @@ mod end_sky;
mod flat;
pub mod lightning;
mod lod;
mod native_pack;
mod native_post;
mod occlusion;
mod pack_atlas;
@@ -501,7 +502,7 @@ pub struct Renderer {
/// The shader pack's post-processing, and the dimension (legacy id) it was loaded for.
pack_post: Option<(i32, Option<pack_post::PackPost>)>,
/// The post passes of one of Horizon's own shader packs; never beside `pack_post`'s.
native_post: Option<native_post::NativePost>,
native: Option<native_pack::NativePack>,
/// Time the shader pack's passes on the GPU (F3, or `HORIZON_SHADER_PACK_TIMING`).
pass_timing: bool,
/// The entity vertices Horizon draws itself while a shader pack draws the others.
@@ -2414,7 +2415,7 @@ impl Renderer {
occlusion: occlusion::Occlusion::new(crate::chunk::MIN_Y, crate::chunk::HEIGHT / 16),
world_post,
pack_post: None,
native_post: None,
native: None,
pass_timing: pack_timing::PassTiming::logging(),
pack_entity_rest: Vec::new(),
cloud_bindings,
@@ -2924,41 +2925,35 @@ impl Renderer {
});
self.pack_post = Some((dimension, post));
if self.pack_post.as_ref().is_some_and(|p| p.1.is_some()) {
self.native_post = None;
self.native = None;
}
report
}
/// Drops the running shader pack, of either kind.
pub fn drop_shader_packs(&mut self) {
self.pack_post = None;
self.native_post = None;
self.native = None;
self.pack_atlas = None;
}
/// Runs the post passes of one of Horizon's own shader packs over the world from now on
/// (`None`: no pack), with its options' values; replaces a `shaders/` pack. Returns the
/// passes' labels; on an error the passes that ran keep running.
/// Runs the post stage of one of Horizon's own shader packs from now on (`None`: no
/// pack), with its options' values; replaces a `shaders/` pack. Returns the passes'
/// labels; on an error the pack that ran keeps running.
pub fn set_native_shader_pack(
&mut self,
pack: Option<(horizon_shader_api::Report, Vec<(String, f64)>)>,
) -> std::result::Result<Vec<String>, String> {
let stages = pack.map(|(report, constants)| {
let post: Vec<_> = report
.stages
.into_iter()
.filter(|s| s.kind == horizon_shader_api::StageKind::Post)
.collect();
(post, constants)
});
let Some((post, constants)) = stages.filter(|(post, _)| !post.is_empty()) else {
self.native_post = None;
let Some((report, constants)) = pack else {
self.native = None;
return Ok(Vec::new());
};
let format = self.config.format.remove_srgb_suffix();
let post = native_post::NativePost::new(&self.device, post, &constants, format)?;
let labels = post.labels().into_iter().map(str::to_owned).collect();
self.pack_post = None;
self.pack_atlas = None;
self.native_post = Some(post);
let (pack, labels) =
native_pack::NativePack::new(&self.device, report, &constants, format)?;
self.native = pack.draws().then_some(pack);
if self.native.is_some() {
self.pack_post = None;
self.pack_atlas = None;
}
Ok(labels)
}
/// What a shader pack's gbuffers programs read from Horizon's geometry: the sky's, and
@@ -3009,20 +3004,20 @@ impl Renderer {
/// The running shader pack's timer, of either kind.
fn pass_timer<'a>(
pack: &'a mut Option<(i32, Option<pack_post::PackPost>)>,
native: &'a mut Option<native_post::NativePost>,
native: &'a mut Option<native_pack::NativePack>,
) -> Option<&'a mut Option<pack_timing::PassTiming>> {
match (pack, native) {
(Some((_, Some(post))), _) => Some(&mut post.timing),
(_, Some(post)) => Some(&mut post.timing),
(_, Some(native)) => Some(&mut native.timing),
_ => None,
}
}
/// Each pass of the running shader pack with its median GPU time in milliseconds, while
/// they are timed (`set_pass_timing`).
pub fn shader_pass_times(&self) -> Vec<(String, f32)> {
let timer = match (&self.pack_post, &self.native_post) {
let timer = match (&self.pack_post, &self.native) {
(Some((_, Some(post))), _) => post.timing.as_ref(),
(_, Some(post)) => post.timing.as_ref(),
(_, Some(native)) => native.timing.as_ref(),
_ => None,
};
timer
@@ -4052,8 +4047,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());
// One of Horizon's own packs reads the same world target.
let native_post = !frame.title && self.native_post.is_some();
if let Some(timer) = Self::pass_timer(&mut self.pack_post, &mut self.native_post)
let native_post = !frame.title && self.native.as_ref().is_some_and(|n| n.post.is_some());
if let Some(timer) = Self::pass_timer(&mut self.pack_post, &mut self.native)
&& timer.is_some() != self.pass_timing
{
*timer = self
@@ -4115,6 +4110,20 @@ impl Renderer {
cull.bind_depth(&self.device, world_depth);
}
let cull = self.cull.as_ref().filter(|_| culling && solid_draws > 0);
// One of Horizon's own packs: this frame's inputs.
if !frame.title
&& let Some(native) = &mut self.native
{
let size = self
.world_post
.size()
.filter(|_| scaled.is_some())
.unwrap_or([self.config.width, self.config.height]);
native.frame.write(&self.queue, frame, size);
if let Some(t) = &mut native.timing {
t.begin(&self.device, &self.queue);
}
}
{
let attachments = [Some(wgpu::RenderPassColorAttachment {
view: world_view,
@@ -4511,17 +4520,17 @@ impl Renderer {
self.pack_post = Some((dimension, Some(post)));
} else if let Some((color, depth)) = &scaled
&& native_post
&& let Some(post) = &mut self.native_post
&& let Some(native) = &mut self.native
&& let Some(post) = &mut native.post
{
let size = self.world_post.size().unwrap_or([1, 1]);
post.run(
&self.device,
&self.queue,
&mut encoder,
(color, depth),
&view_target,
frame,
size,
native.timing.as_mut(),
);
} else if scaled.is_some() {
self.world_post.finish(&mut encoder, &view_target);
@@ -4694,7 +4703,7 @@ impl Renderer {
timer.submitted(&self.queue);
timer.collect(&self.device, &mut self.gpu_times);
}
if let Some(Some(timer)) = Self::pass_timer(&mut self.pack_post, &mut self.native_post) {
if let Some(Some(timer)) = Self::pass_timer(&mut self.pack_post, &mut self.native) {
timer.submitted(&self.queue);
}
if let Some((path, buffer)) = readback {
@@ -0,0 +1,52 @@
//! One of Horizon's own shader packs while it runs (docs/shaders.md): the frame its stages
//! read, its post stage, and the timer of its passes.
use horizon_shader_api::{Report, StageKind};
use super::native_post::{NativeFrame, NativePost};
use super::pack_timing::PassTiming;
pub struct NativePack {
pub frame: NativeFrame,
pub post: Option<NativePost>,
/// GPU time of each pass, while the renderer asks for it (`pack_timing.rs`).
pub timing: Option<PassTiming>,
}
impl NativePack {
/// The pack of the checked stages `report` with the option values `constants`, drawing
/// onto targets of `format`. Also returns its stages' labels, in the order they draw.
/// Errors name the stage wgpu refused.
pub fn new(
device: &wgpu::Device,
report: Report,
constants: &[(String, f64)],
format: wgpu::TextureFormat,
) -> Result<(Self, Vec<String>), String> {
let frame = NativeFrame::new(device);
let post: Vec<_> = report
.stages
.into_iter()
.filter(|s| s.kind == StageKind::Post)
.collect();
let post = (!post.is_empty())
.then(|| NativePost::new(device, post, constants, format, &frame))
.transpose()?;
let labels = post
.iter()
.flat_map(|p| p.labels())
.map(str::to_owned)
.collect();
let pack = Self {
frame,
post,
timing: None,
};
Ok((pack, labels))
}
/// Whether the pack draws anything the game runs today.
pub fn draws(&self) -> bool {
self.post.is_some()
}
}
+87 -70
View File
@@ -3,7 +3,8 @@
//! the pack's options as pipeline constants. A pass reads bind group 3: the frame, the world's
//! colour and depth, the previous pass's output and two samplers. Passes before the last write
//! half-float targets of the world's size, so values over 1 carry to the next; the last draws
//! onto the screen.
//! onto the screen. Every stage of a pack reads the same `frame`, written once a frame
//! (`NativeFrame`).
use glam::{Mat4, Vec3};
use horizon_shader_api::{Frame as FrameInputs, check::CheckedStage, stages::GROUP};
@@ -25,12 +26,70 @@ pub struct NativePost {
targets: Option<([u32; 2], [wgpu::TextureView; 2])>,
/// One bind group per pass, for these world views.
groups: Option<(WorldViews, Vec<wgpu::BindGroup>)>,
start: std::time::Instant,
frames: u32,
/// The screen's colour format, the world target's too.
format: wgpu::TextureFormat,
/// GPU time of each pass, while the renderer asks for it (`pack_timing.rs`).
pub timing: Option<super::pack_timing::PassTiming>,
}
/// The `frame` every stage of a pack reads (group 3, binding 0), written once a frame.
pub struct NativeFrame {
pub(super) buffer: wgpu::Buffer,
start: std::time::Instant,
frames: u32,
}
/// The uniform `frame` at binding 0, read by `visibility`.
fn frame_entry(visibility: wgpu::ShaderStages) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(size_of::<FrameInputs>() as u64),
},
count: None,
}
}
impl NativeFrame {
pub fn new(device: &wgpu::Device) -> Self {
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Native shader pack frame"),
size: size_of::<FrameInputs>() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
Self {
buffer,
start: std::time::Instant::now(),
frames: 0,
}
}
/// Writes this frame's inputs, for a world of `size`.
pub fn write(&mut self, queue: &wgpu::Queue, frame: &Frame, size: [u32; 2]) {
let seconds = self.start.elapsed().as_secs_f32() % 3600.0;
let inputs = frame_inputs(frame, size, seconds, self.frames);
self.frames = self.frames.wrapping_add(1);
queue.write_buffer(&self.buffer, 0, bytemuck::bytes_of(&inputs));
}
}
/// The option values of `constants` that `module` declares as overrides (wgpu refuses others).
pub fn present_constants<'a>(
module: &naga::Module,
constants: &'a [(String, f64)],
) -> Vec<(&'a str, f64)> {
constants
.iter()
.filter(|(id, _)| {
module
.overrides
.iter()
.any(|(_, o)| o.name.as_ref() == Some(id))
})
.map(|(id, v)| (id.as_str(), *v))
.collect()
}
fn texture_entry(binding: u32, sample_type: wgpu::TextureSampleType) -> wgpu::BindGroupLayoutEntry {
@@ -63,21 +122,13 @@ impl NativePost {
stages: Vec<CheckedStage>,
constants: &[(String, f64)],
format: wgpu::TextureFormat,
frame: &NativeFrame,
) -> Result<Self, String> {
let float = wgpu::TextureSampleType::Float { filterable: true };
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Native shader pack post"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: wgpu::BufferSize::new(size_of::<FrameInputs>() as u64),
},
count: None,
},
frame_entry(wgpu::ShaderStages::FRAGMENT),
texture_entry(1, float),
texture_entry(2, wgpu::TextureSampleType::Depth),
texture_entry(3, float),
@@ -95,17 +146,7 @@ impl NativePost {
let count = stages.len();
let mut passes = Vec::with_capacity(count);
for (i, stage) in stages.into_iter().enumerate() {
let present: Vec<(&str, f64)> = constants
.iter()
.filter(|(id, _)| {
stage
.module
.overrides
.iter()
.any(|(_, o)| o.name.as_ref() == Some(id))
})
.map(|(id, v)| (id.as_str(), *v))
.collect();
let present = present_constants(&stage.module, constants);
let scope = device.push_error_scope(wgpu::ErrorFilter::Validation);
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some(&stage.label),
@@ -145,12 +186,6 @@ impl NativePost {
}
passes.push((stage.label, pipeline));
}
let frame = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Native shader pack frame"),
size: size_of::<FrameInputs>() as u64,
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
mapped_at_creation: false,
});
let sampler = |label, filter| {
device.create_sampler(&wgpu::SamplerDescriptor {
label: Some(label),
@@ -162,15 +197,12 @@ impl NativePost {
Ok(Self {
passes,
layout,
frame,
frame: frame.buffer.clone(),
linear: sampler("Native shader pack linear", wgpu::FilterMode::Linear),
nearest: sampler("Native shader pack nearest", wgpu::FilterMode::Nearest),
targets: None,
groups: None,
start: std::time::Instant::now(),
frames: 0,
format,
timing: None,
})
}
@@ -264,37 +296,18 @@ impl NativePost {
}
/// Runs the passes over the world target `world` (colour, depth) of `size`, the last onto
/// `screen`.
/// `screen`, timed by `timing` when it times this frame.
pub fn run(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
world: (&wgpu::TextureView, &wgpu::TextureView),
screen: &wgpu::TextureView,
frame: &Frame,
size: [u32; 2],
) {
let seconds = self.start.elapsed().as_secs_f32() % 3600.0;
let inputs = frame_inputs(frame, size, seconds, self.frames);
self.frames = self.frames.wrapping_add(1);
self.draw(device, queue, encoder, world, screen, &inputs, size);
}
fn draw(
&mut self,
device: &wgpu::Device,
queue: &wgpu::Queue,
encoder: &mut wgpu::CommandEncoder,
world: (&wgpu::TextureView, &wgpu::TextureView),
screen: &wgpu::TextureView,
inputs: &FrameInputs,
size: [u32; 2],
mut timing: Option<&mut super::pack_timing::PassTiming>,
) {
self.prepare(device, world, size);
queue.write_buffer(&self.frame, 0, bytemuck::bytes_of(inputs));
if let Some(t) = &mut self.timing {
t.begin(device, queue);
if let Some(t) = timing.as_deref_mut() {
t.mark(device, encoder, (world.0, self.format));
}
let Some((_, groups)) = &self.groups else {
@@ -317,7 +330,7 @@ impl NativePost {
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: Some(label),
color_attachments: &attachments,
timestamp_writes: self.timing.as_mut().and_then(|t| t.writes(label)),
timestamp_writes: timing.as_deref_mut().and_then(|t| t.writes(label)),
..Default::default()
});
pass.set_pipeline(pipeline);
@@ -437,8 +450,20 @@ mod tests {
.into();
let report = check(&Pack::from_files(files).unwrap()).unwrap();
let format = wgpu::TextureFormat::Rgba8Unorm;
let mut post =
NativePost::new(&device, report.stages, &[("gain".into(), 1.0)], format).unwrap();
let frame = NativeFrame::new(&device);
queue.write_buffer(
&frame.buffer,
0,
bytemuck::bytes_of(&FrameInputs::default()),
);
let mut post = NativePost::new(
&device,
report.stages,
&[("gain".into(), 1.0)],
format,
&frame,
)
.unwrap();
assert_eq!(post.labels(), ["a.wgsl", "b.wgsl"]);
let usage = wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING;
@@ -478,15 +503,7 @@ mod tests {
..Default::default()
});
let view = screen.create_view(&Default::default());
post.draw(
&device,
&queue,
&mut encoder,
(&color, &depth),
&view,
&FrameInputs::default(),
[4, 4],
);
post.run(&device, &mut encoder, (&color, &depth), &view, [4, 4], None);
let buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: None,
size: u64::from(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT * 4),