Shader packs, clean room 33: clouds
A pack with gbuffers_clouds draws Horizon's cloud geometry itself (spec
2.3, OQ-3: no fallback), blended, after the opaque world. Horizon's own
cloud draw is skipped then, also when the pack's program writes nothing
(BSL and Complementary hide vanilla clouds that way). Without a pack the
cloud draw is unchanged.
The translator now rewrites a bare `discard;` or `return;` to
`if (true) discard;` / `if (true) return;`, so code a pack leaves after
it stays reachable for naga ("Expression not visited"). This also fixes
Complementary Unbound's gbuffers_skytextured.
Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1;
no other loader's code read.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -490,6 +490,8 @@ pub struct Renderer {
|
||||
pack_post: Option<(i32, Option<pack_post::PackPost>)>,
|
||||
/// The entity vertices Horizon draws itself while a shader pack draws the others.
|
||||
pack_entity_rest: Vec<Vertex>,
|
||||
/// Set 1 of the cloud pipeline, for a shader pack's `gbuffers_clouds`.
|
||||
cloud_bindings: wgpu::BindGroupLayout,
|
||||
/// 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`).
|
||||
@@ -2350,6 +2352,7 @@ impl Renderer {
|
||||
world_post,
|
||||
pack_post: None,
|
||||
pack_entity_rest: Vec::new(),
|
||||
cloud_bindings,
|
||||
pack_atlas: None,
|
||||
pack_terrain_flat: false,
|
||||
hud,
|
||||
@@ -2813,6 +2816,7 @@ impl Renderer {
|
||||
.atlas_texture
|
||||
.as_ref()
|
||||
.map(|t| t.create_view(&Default::default())),
|
||||
clouds: Some(&self.cloud_bindings),
|
||||
};
|
||||
pack_post::PackPost::new(
|
||||
&self.device,
|
||||
@@ -2837,6 +2841,7 @@ impl Renderer {
|
||||
sky: Some(pack_sky::inputs()),
|
||||
celestial: Some(pack_sky::celestial_inputs()),
|
||||
entities: Some(pack_entities::inputs()),
|
||||
clouds: Some(pack_entities::cloud_inputs()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3786,6 +3791,12 @@ impl Renderer {
|
||||
.as_ref()
|
||||
.and_then(|p| p.1.as_ref())
|
||||
.is_some_and(|p| p.has_water());
|
||||
let pack_draws_clouds = pack_post
|
||||
&& self
|
||||
.pack_post
|
||||
.as_ref()
|
||||
.and_then(|p| p.1.as_ref())
|
||||
.is_some_and(|p| p.has_clouds());
|
||||
let scaled = self.world_post.prepare(
|
||||
&self.device,
|
||||
[self.config.width, self.config.height],
|
||||
@@ -4066,7 +4077,7 @@ impl Renderer {
|
||||
0..1,
|
||||
);
|
||||
}
|
||||
if cloud_quads > 0 {
|
||||
if cloud_quads > 0 && !pack_draws_clouds {
|
||||
pass.set_pipeline(&self.cloud_pipeline);
|
||||
pass.set_bind_group(0, &self.entity_group, &[]);
|
||||
pass.set_bind_group(1, &self.cloud_group, &[]);
|
||||
@@ -4127,6 +4138,12 @@ impl Renderer {
|
||||
}
|
||||
}
|
||||
};
|
||||
post.set_clouds(pack_draws_clouds.then(|| pack_post::Clouds {
|
||||
vertices: self.cloud_buffer.clone(),
|
||||
group: self.cloud_group.clone(),
|
||||
indices: self.indices.clone(),
|
||||
quads: cloud_quads,
|
||||
}));
|
||||
post.run(
|
||||
&self.device,
|
||||
&self.queue,
|
||||
|
||||
@@ -104,6 +104,41 @@ pub fn inputs() -> horizon_shaders::GbuffersInputs {
|
||||
}
|
||||
}
|
||||
|
||||
/// What `gbuffers_clouds` reads from the clouds' mesh (entity vertices, `vs_cloud`): the
|
||||
/// position moved by the clouds' offset, the colour of the face's shade from their palette,
|
||||
/// full sky light. Set 1 holds the clouds' uniform (`clouds::Uniform`).
|
||||
pub fn cloud_inputs() -> horizon_shaders::GbuffersInputs {
|
||||
let declarations = "layout(location = 0) in vec3 hz_v_pos;\nlayout(location = 1) in vec2 hz_v_uv;\n\
|
||||
layout(location = 2) in uint hz_v_overlay;\nlayout(location = 3) in vec4 hz_v_color;\n\
|
||||
layout(location = 4) in uvec4 hz_v_light;\nlayout(location = 5) in vec4 hz_v_normal;\n\
|
||||
layout(set = 1, binding = 0) uniform HzCloud { vec4 hz_cloud_offset; vec4 hz_cloud_colors[4]; };\n"
|
||||
.to_owned();
|
||||
let body = " hz_Vertex = vec4(hz_v_pos + hz_cloud_offset.xyz, 1.0);\n \
|
||||
hz_MultiTexCoord0 = vec4(hz_v_uv, 0.0, 1.0);\n \
|
||||
hz_MultiTexCoord1 = vec4(0.0, 240.0, 0.0, 1.0);\n \
|
||||
hz_MultiTexCoord2 = hz_MultiTexCoord1;\n \
|
||||
hz_Color = hz_cloud_colors[min(hz_v_light.x, 3u)];\n \
|
||||
hz_Normal = dot(hz_v_normal.xyz, hz_v_normal.xyz) > 0.01 ? normalize(hz_v_normal.xyz) : vec3(0.0, 1.0, 0.0);\n"
|
||||
.to_owned();
|
||||
let attributes = [
|
||||
("vaPosition", "hz_Vertex.xyz"),
|
||||
("vaUV0", "hz_v_uv"),
|
||||
("vaUV2", "ivec2(0, 240)"),
|
||||
("vaColor", "hz_Color"),
|
||||
("vaNormal", "hz_Normal"),
|
||||
];
|
||||
horizon_shaders::GbuffersInputs {
|
||||
declarations,
|
||||
body,
|
||||
attributes: attributes
|
||||
.iter()
|
||||
.map(|&(n, v)| (n.to_owned(), v.to_owned()))
|
||||
.collect(),
|
||||
after: String::new(),
|
||||
alpha_test: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// `entityColor` for a run whose first quad carries overlay code `overlay`: the hurt red or a
|
||||
/// white flash, its alpha how much of that colour shows over the entity (vanilla mixes the
|
||||
/// overlay texel's colour in by one minus the texel's alpha).
|
||||
|
||||
@@ -202,6 +202,17 @@ pub(super) struct World<'a> {
|
||||
pub block_ids: &'a [i32],
|
||||
/// The atlas of the sun and moon's sprites.
|
||||
pub celestial: Option<wgpu::TextureView>,
|
||||
/// Set 1 of the clouds' draw: their offset and palette.
|
||||
pub clouds: Option<&'a wgpu::BindGroupLayout>,
|
||||
}
|
||||
|
||||
/// This frame's clouds, when the pack draws them: their vertices, uniform, quad indices and
|
||||
/// quad count.
|
||||
pub(super) struct Clouds {
|
||||
pub vertices: wgpu::Buffer,
|
||||
pub group: wgpu::BindGroup,
|
||||
pub indices: wgpu::Buffer,
|
||||
pub quads: u32,
|
||||
}
|
||||
|
||||
pub struct PackPost {
|
||||
@@ -215,6 +226,12 @@ pub struct PackPost {
|
||||
sky: Option<Sky>,
|
||||
/// What draws entities and block entities, after terrain, and their vertices.
|
||||
entities: Option<Entities>,
|
||||
/// What draws the clouds, after entities, and this frame's clouds.
|
||||
clouds: Option<Pass>,
|
||||
cloud_draw: Option<Clouds>,
|
||||
/// The pack has `gbuffers_clouds`, even one that writes nothing (a pack drawing its own
|
||||
/// clouds discards vanilla's): the game's clouds are not drawn.
|
||||
hides_clouds: bool,
|
||||
/// A program after translucent terrain reads `depthtex1`: it is copied before water.
|
||||
depth1: bool,
|
||||
/// Buffers with mipmaps (filterable ones a program asks them for).
|
||||
@@ -944,6 +961,19 @@ impl PackPost {
|
||||
passes: entity_passes,
|
||||
late,
|
||||
});
|
||||
let cloud_geometry = world.clouds.map(|layout| Geometry {
|
||||
buffers: &entity_buffers,
|
||||
table: Some(layout),
|
||||
depth_write: true,
|
||||
atlas: world.celestial.clone(),
|
||||
ids: None,
|
||||
cull: false,
|
||||
});
|
||||
let hides_clouds = post.clouds.is_some() && cloud_geometry.is_some();
|
||||
let clouds = post
|
||||
.clouds
|
||||
.zip(cloud_geometry.as_ref())
|
||||
.and_then(|(p, g)| build(p, Some(g)));
|
||||
// Developers' aid: `HORIZON_SHADER_PACK_SKIP=composite,final` leaves programs out.
|
||||
let skip = std::env::var("HORIZON_SHADER_PACK_SKIP").unwrap_or_default();
|
||||
let passes: Vec<Pass> = post
|
||||
@@ -972,6 +1002,9 @@ impl PackPost {
|
||||
"translucent entities",
|
||||
"particles",
|
||||
];
|
||||
if let Some(c) = &clouds {
|
||||
notes.push(format!("clouds draw with {}", c.name));
|
||||
}
|
||||
for (part, pass) in entities.iter().flat_map(|e| parts.iter().zip(&e.passes)) {
|
||||
if let Some(p) = pass {
|
||||
notes.push(format!("{part} draw with {}", p.name));
|
||||
@@ -995,6 +1028,7 @@ impl PackPost {
|
||||
.flat_map(|s| s.basic.iter().chain(&s.textured))
|
||||
.chain(&terrain)
|
||||
.chain(entities.iter().flat_map(|e| e.passes.iter().flatten()))
|
||||
.chain(&clouds)
|
||||
.chain(&water)
|
||||
.find(touched)
|
||||
.map(|_| true)
|
||||
@@ -1011,6 +1045,7 @@ impl PackPost {
|
||||
|| water.is_some()
|
||||
|| sky.is_some()
|
||||
|| entities.is_some()
|
||||
|| clouds.is_some()
|
||||
|| passes.iter().any(|p| {
|
||||
p.samplers
|
||||
.iter()
|
||||
@@ -1107,6 +1142,9 @@ impl PackPost {
|
||||
water,
|
||||
sky,
|
||||
entities,
|
||||
clouds,
|
||||
cloud_draw: None,
|
||||
hides_clouds,
|
||||
depth1,
|
||||
mipped,
|
||||
mips_valid: 0,
|
||||
@@ -1523,6 +1561,45 @@ impl PackPost {
|
||||
}
|
||||
self.draw_terrain(device, queue, encoder, size, false, terrain);
|
||||
self.draw_entities(device, queue, encoder, size, false);
|
||||
self.draw_clouds(device, queue, encoder, size);
|
||||
}
|
||||
|
||||
/// The clouds `set_clouds` gave for this frame, with `gbuffers_clouds`.
|
||||
fn draw_clouds(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
size: [u32; 2],
|
||||
) {
|
||||
let (Some(mut pass), Some(clouds)) = (self.clouds.take(), self.cloud_draw.take()) else {
|
||||
return;
|
||||
};
|
||||
let group = self.bind(device, queue, &mut pass, size);
|
||||
{
|
||||
let mut rp = self.begin(encoder, &pass);
|
||||
rp.set_pipeline(&pass.pipeline);
|
||||
rp.set_bind_group(0, &group, &[]);
|
||||
rp.set_bind_group(1, &clouds.group, &[]);
|
||||
if pass.draw {
|
||||
self.draws.bind(&mut rp, "CLOUDS");
|
||||
}
|
||||
rp.set_vertex_buffer(0, clouds.vertices.slice(..));
|
||||
rp.set_index_buffer(clouds.indices.slice(..), wgpu::IndexFormat::Uint32);
|
||||
rp.draw_indexed(0..clouds.quads * 6, 0, 0..1);
|
||||
}
|
||||
self.mips_valid &= !pass.written;
|
||||
self.clouds = Some(pass);
|
||||
}
|
||||
|
||||
/// Whether the pack draws the clouds itself (or hides them).
|
||||
pub fn has_clouds(&self) -> bool {
|
||||
self.hides_clouds
|
||||
}
|
||||
|
||||
/// The clouds to draw this frame (`None`: none).
|
||||
pub fn set_clouds(&mut self, clouds: Option<Clouds>) {
|
||||
self.cloud_draw = clouds.filter(|c| c.quads > 0);
|
||||
}
|
||||
|
||||
/// Translucent terrain, then the entity parts drawn after deferred.
|
||||
@@ -1842,8 +1919,10 @@ impl PackPost {
|
||||
let mut passes = std::mem::take(&mut self.passes);
|
||||
let mut seen = (false, false);
|
||||
let mut shown = false;
|
||||
let mut terrain_drawn =
|
||||
self.terrain.is_none() && self.sky.is_none() && self.entities.is_none();
|
||||
let mut terrain_drawn = self.terrain.is_none()
|
||||
&& self.sky.is_none()
|
||||
&& self.entities.is_none()
|
||||
&& self.clouds.is_none();
|
||||
let late_entities = self.entities.as_ref().is_some_and(|e| {
|
||||
e.passes
|
||||
.iter()
|
||||
|
||||
@@ -120,6 +120,9 @@ const BLOCK: [&str; 5] = [
|
||||
"gbuffers_basic",
|
||||
];
|
||||
|
||||
/// `gbuffers_clouds`: no fallback, the game draws its clouds otherwise (OQ-3).
|
||||
const CLOUDS: [&str; 1] = ["gbuffers_clouds"];
|
||||
|
||||
/// `gbuffers_entities_translucent` and its fallbacks (2.3).
|
||||
const ENTITIES_TRANSLUCENT: [&str; 5] = [
|
||||
"gbuffers_entities_translucent",
|
||||
@@ -191,6 +194,8 @@ pub struct Post {
|
||||
pub entities_translucent: Option<PostProgram>,
|
||||
/// What draws particles.
|
||||
pub particles: Option<PostProgram>,
|
||||
/// What draws the game's clouds.
|
||||
pub clouds: Option<PostProgram>,
|
||||
/// Translucent entities draw after deferred (`separateEntityDraws`, 3.2).
|
||||
pub translucent_late: bool,
|
||||
/// Particles draw after deferred (`particles.ordering`, `particles.before.deferred`, 3.2).
|
||||
@@ -222,6 +227,8 @@ pub struct GameInputs {
|
||||
pub celestial: Option<GbuffersInputs>,
|
||||
/// Entities and block entities (one vertex format).
|
||||
pub entities: Option<GbuffersInputs>,
|
||||
/// The clouds' mesh.
|
||||
pub clouds: Option<GbuffersInputs>,
|
||||
}
|
||||
|
||||
/// Loads the post-processing programs of `folder` and, for the geometry `inputs` describes, the
|
||||
@@ -246,6 +253,7 @@ pub fn load_post(
|
||||
block: None,
|
||||
entities_translucent: None,
|
||||
particles: None,
|
||||
clouds: None,
|
||||
translucent_late: false,
|
||||
particles_late: false,
|
||||
custom: Custom::parse(properties),
|
||||
@@ -342,6 +350,15 @@ pub fn load_post(
|
||||
let mut gbuffers_dyn = |names: &[&str], i: &GbuffersInputs| gbuffers(names, Group::Gbuffers, i);
|
||||
let entities_translucent = translucent(&ENTITIES_TRANSLUCENT, 0.0, &mut gbuffers_dyn);
|
||||
let particles = translucent(&PARTICLES, 0.1, &mut gbuffers_dyn);
|
||||
// Clouds blend, as vanilla's do (OQ-23).
|
||||
let clouds = inputs
|
||||
.clouds
|
||||
.as_ref()
|
||||
.and_then(|i| gbuffers_dyn(&CLOUDS, i));
|
||||
let clouds = clouds.map(|mut p| {
|
||||
p.blend = blend(properties, &p.name).unwrap_or(Some(TRANSLUCENT_BLEND));
|
||||
p
|
||||
});
|
||||
if let Some(inputs) = &inputs.terrain {
|
||||
let terrain = gbuffers(&TERRAIN, Group::Gbuffers, inputs);
|
||||
// gbuffers_water falls back to gbuffers_terrain's chain (2.3).
|
||||
@@ -361,6 +378,7 @@ pub fn load_post(
|
||||
loader.post.block = block;
|
||||
loader.post.entities_translucent = entities_translucent;
|
||||
loader.post.particles = particles;
|
||||
loader.post.clouds = clouds;
|
||||
let flag = |key: &str| properties.get(key).map(|v| v.trim() == "true");
|
||||
loader.post.translucent_late = flag("separateEntityDraws").unwrap_or(false);
|
||||
// `after` when the pack has a deferred program, else `mixed`; ours are all translucent, so
|
||||
@@ -648,7 +666,8 @@ mod tests {
|
||||
terrain: Some(inputs.clone()),
|
||||
sky: Some(inputs.clone()),
|
||||
celestial: Some(inputs.clone()),
|
||||
entities: Some(inputs),
|
||||
entities: Some(inputs.clone()),
|
||||
clouds: Some(inputs),
|
||||
};
|
||||
let post = load_post(&pack, "", &Properties::default(), ¯os, &inputs);
|
||||
let (terrain, water) = (post.terrain.unwrap(), post.water.unwrap());
|
||||
@@ -696,6 +715,8 @@ mod tests {
|
||||
);
|
||||
assert_eq!(particles.blend, Some(TRANSLUCENT_BLEND));
|
||||
assert!(post.particles_late && !post.translucent_late);
|
||||
// gbuffers_clouds has no fallback (OQ-3): without it the game draws its clouds.
|
||||
assert!(post.clouds.is_none());
|
||||
let ordering = Properties::parse(
|
||||
"particles.ordering = before\nseparateEntityDraws = true\n",
|
||||
¯os,
|
||||
|
||||
@@ -702,6 +702,35 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Code after an unconditional `discard` (BSL's `gbuffers_clouds` with its own clouds on)
|
||||
/// or `return;` still makes a valid module.
|
||||
#[test]
|
||||
fn code_after_discard_or_return_translates() {
|
||||
let vsh = "#version 120\nvoid main() { gl_Position = ftransform(); }\n";
|
||||
for fsh in [
|
||||
"#version 120\nuniform sampler2D texture;\nvoid main() {\n discard;\n vec4 c = texture2D(texture, vec2(0.5));\n gl_FragData[0] = c;\n}\n",
|
||||
"#version 120\nvoid main() {\n gl_FragData[0] = vec4(1.0);\n return;\n gl_FragData[0] = vec4(0.5);\n}\n",
|
||||
] {
|
||||
let files = [
|
||||
("shaders/gbuffers_clouds.vsh", vsh),
|
||||
("shaders/gbuffers_clouds.fsh", fsh),
|
||||
];
|
||||
let pack = Pack::from_zip_bytes("P.zip", zip(&files)).unwrap();
|
||||
let m = Macros::standard();
|
||||
let inputs = GbuffersInputs {
|
||||
body: " hz_Vertex = vec4(0.0, 0.0, 0.0, 1.0);\n".into(),
|
||||
..Default::default()
|
||||
};
|
||||
translate_gbuffers(
|
||||
&preprocess(&pack, "gbuffers_clouds.vsh", &m),
|
||||
&preprocess(&pack, "gbuffers_clouds.fsh", &m),
|
||||
Target::Buffers,
|
||||
&inputs,
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("{fsh}: {e:?}"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_alpha_test_discards_below_its_reference() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -190,6 +190,13 @@ impl Stage<'_> {
|
||||
}
|
||||
let replaced: Option<String> = match t {
|
||||
"main" if depth == 0 && called => Some("hz_main".into()),
|
||||
// naga's frontend leaves the code after an unconditional `discard` or `return;`
|
||||
// unreachable but half-built (a module that fails validation): behind an `if`
|
||||
// it stays reachable as far as naga knows.
|
||||
"discard" => Some("if (true) discard".into()),
|
||||
"return" if next(i).is_some_and(|j| self.tokens[j] == ";") => {
|
||||
Some("if (true) return".into())
|
||||
}
|
||||
// GLSL's `modf`, which naga's frontend lacks.
|
||||
"modf" if called => Some("hz_modf".into()),
|
||||
"texture2D" | "texture1D" | "texture3D" | "textureCube" if called => {
|
||||
|
||||
@@ -173,7 +173,8 @@ fn real_packs_read() {
|
||||
terrain: Some(terrain.clone()),
|
||||
sky: Some(terrain.clone()),
|
||||
celestial: Some(terrain.clone()),
|
||||
entities: Some(terrain),
|
||||
entities: Some(terrain.clone()),
|
||||
clouds: Some(terrain),
|
||||
};
|
||||
let post = load_post(&pack, &folder, &shaders, &Macros::standard(), &inputs);
|
||||
println!(
|
||||
|
||||
Reference in New Issue
Block a user