Shader packs, clean room 24: mc_Entity from block.properties

block.properties (spec 4.1) is read: `block.<id>=` names with optional
namespaces and state filters; the most specific matching entry wins.
Terrain vertices carry their sprite layer, not their block, so the game
builds a table of the block sprite layers: each takes the id of the block
named like its sprite, else the id most blocks drawing it share; y is 1
on the water and lava sprites. Flat terrain reads it from a storage
buffer at set 3; other draws and packs without the file read (-1, -1).
Building the table takes 14 ms for BSL, 39 ms for Complementary.

Clean-room: implemented from cleanroom/shader-packs-spec.md revision 1,
4.1, 5.1, 5.3; no other loader's code read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-25 04:59:39 +01:00
parent b940754555
commit 441f749c25
10 changed files with 314 additions and 8 deletions
+98 -3
View File
@@ -4,7 +4,7 @@
use horizon_client::camera::CameraFluid;
use horizon_client::gfx::pack_uniforms::PackInputs;
use horizon_shaders::{Macros, Pack, Properties, dimension_folder, load_post};
use horizon_shaders::{BlockIds, Macros, Pack, Properties, dimension_folder, load_post};
use super::{Dim, Game};
@@ -44,7 +44,21 @@ impl Game<'_> {
let folder = dimension_folder(&pack, dimension);
let inputs = renderer.pack_inputs();
let post = load_post(&pack, &folder, &properties, &macros, &inputs);
if let Some((programs, notes)) = renderer.set_shader_pack(dimension, Some(post))
let text = pack.read_text("block.properties").unwrap_or_default();
let ids = BlockIds::parse(&Properties::parse(&text, &macros));
for note in &ids.notes {
log::warn!("shader pack {name}: block.properties: {note}");
}
let started_ids = std::time::Instant::now();
let table = block_id_table(&self.pack, &ids);
log::info!(
"shader pack {name}: mc_Entity for {} of {} block sprites in {} ms",
table.chunks(2).filter(|p| p[0] != -1).count(),
table.len() / 2,
started_ids.elapsed().as_millis()
);
if let Some((programs, notes)) =
renderer.set_shader_pack(dimension, Some(post), &table)
{
for note in notes {
log::warn!("shader pack {name}: {note}");
@@ -59,7 +73,7 @@ impl Game<'_> {
}
Err(e) => {
log::warn!("shader pack {name}: {e}");
renderer.set_shader_pack(dimension, None);
renderer.set_shader_pack(dimension, None, &[]);
}
}
}
@@ -83,3 +97,84 @@ impl Game<'_> {
}
}
}
/// `mc_Entity` (x, y) of each block sprite layer, as (id, fluid) pairs: terrain vertices carry
/// their sprite layer, not their block. A layer takes the id of the block named like its sprite
/// (`block/oak_leaves` for `oak_leaves`), else the id most blocks drawing it share (ties: the
/// lowest); -1 when no block drawing it has one. y is 1 for the fluid sprites, -1 otherwise.
fn block_id_table(pack: &super::PackResources, ids: &BlockIds) -> Vec<i32> {
use std::collections::{BTreeMap, BTreeSet};
let layers = pack
.render
.sprite_layers
.values()
.max()
.map_or(0, |l| l + 1) as usize;
let mut table = vec![-1; layers * 2];
if ids.is_empty() {
return table;
}
let registry = pack.assets.registry;
let catalog = &pack.render.model_catalog;
let sprite = |name: &str| pack.render.sprite_layers.get(name).copied();
// (layer, id) -> (a block named like the sprite, the blocks).
let mut votes: BTreeMap<(u32, i32), (bool, BTreeSet<&str>)> = BTreeMap::new();
for i in 0..registry.len() {
let state_id = horizon_world::registry::BlockStateId(i as u32);
let Some(state) = registry.state(state_id) else {
continue;
};
let Some(id) = ids.id(&state.name, |k| state.properties.get(k).map(String::as_str)) else {
continue;
};
let block = state.name.strip_prefix("minecraft:").unwrap_or(&state.name);
let mut vote = |layer: u32| {
let named = pack
.layer_names
.get(&layer)
.and_then(|n| n.strip_prefix("block/"))
== Some(block);
let v = votes.entry((layer, id)).or_default();
v.0 |= named;
v.1.insert(&state.name);
};
// Fluids draw from their own sprites.
let fluid = match block {
"water" => ["water_still", "water_flow"],
"lava" => ["lava_still", "lava_flow"],
_ => ["", ""],
};
for f in fluid.iter().filter(|f| !f.is_empty()) {
if let Some(l) = sprite(&format!("minecraft:block/{f}")) {
vote(l);
}
}
let origin = horizon_core::position::BlockPos::new(0, 0, 0);
for (_, model) in catalog.collect(state_id, origin).unwrap_or_default() {
for face in &model.faces {
vote(face.layer);
}
}
}
let mut best: BTreeMap<u32, (bool, usize, i32)> = BTreeMap::new();
for ((layer, id), (named, blocks)) in votes {
let candidate = (named, blocks.len(), -id);
let b = best.entry(layer).or_insert(candidate);
if candidate > *b {
*b = candidate;
}
}
for (layer, (_, _, id)) in best {
if let Some(slot) = table.get_mut(layer as usize * 2) {
*slot = -id;
}
}
for f in ["water_still", "water_flow", "lava_still", "lava_flow"] {
if let Some(l) = sprite(&format!("minecraft:block/{f}"))
&& let Some(slot) = table.get_mut(l as usize * 2 + 1)
{
*slot = 1;
}
}
table
}
+5 -1
View File
@@ -2764,12 +2764,14 @@ impl Renderer {
self.world_post.scale = scale.clamp(0.25, 2.0);
self.world_post.antialias = antialias;
}
/// Runs `post` over the world from now on (`None`: no pack), for the dimension `dimension`.
/// Runs `post` over the world from now on (`None`: no pack), for the dimension `dimension`;
/// `block_ids` gives each block sprite layer's `mc_Entity` (id, fluid) pair.
/// Returns the programs that run and the notes of loading them.
pub fn set_shader_pack(
&mut self,
dimension: i32,
pack: Option<horizon_shaders::Post>,
block_ids: &[i32],
) -> Option<(Vec<String>, Vec<String>)> {
if pack.is_none() {
self.pack_atlas = None;
@@ -2796,7 +2798,9 @@ impl Renderer {
table: self.flat.as_ref().filter(|_| flat).map(|f| &f.draw_layout),
depth_write: true,
atlas: None,
ids: None,
}),
block_ids,
celestial: self
.hud
.atlas_texture
+10 -1
View File
@@ -127,6 +127,10 @@ impl PackAtlas {
"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",
);
// `mc_Entity` of each sprite layer: id, then 1 for fluids or -1 (the game's table).
declarations.push_str(
"layout(set = 3, binding = 0) readonly buffer HzBlockIds { int hz_block_ids[]; };\n",
);
"hz_slot_origins[hz_block_slots[uint(gl_VertexIndex) >> 8u]]"
} else {
declarations.push_str("layout(location = 5) in vec4 hz_v_origin;\n");
@@ -153,8 +157,13 @@ impl PackAtlas {
scale[0], scale[1],
);
declarations.push_str("vec2 hz_cell; vec2 hz_scale;\n");
let entity = if flat {
"vec4(float(hz_block_ids[2u * hz_layer]), float(hz_block_ids[2u * hz_layer + 1u]), 0.0, 0.0)"
} else {
"vec4(-1.0, -1.0, 0.0, 0.0)"
};
let attributes = [
("mc_Entity", "vec4(-1.0, -1.0, 0.0, 0.0)"),
("mc_Entity", entity),
(
"mc_midTexCoord",
"vec4((hz_cell + 0.5) * hz_scale, 0.0, 1.0)",
+69 -2
View File
@@ -154,6 +154,8 @@ struct Pass {
draw: bool,
/// What `texture` reads instead of the block atlas.
atlas: Option<wgpu::TextureView>,
/// Reads `mc_Entity`'s table (`PackPost::block_ids`).
ids: bool,
groups: HashMap<u16, wgpu::BindGroup>,
}
@@ -184,6 +186,8 @@ pub(super) struct World<'a> {
pub lightmap: wgpu::TextureView,
/// Terrain's geometry, when the pack draws it.
pub terrain: Option<Geometry<'a>>,
/// `mc_Entity` of each block sprite layer: (id, 1 for fluids or -1) pairs.
pub block_ids: &'a [i32],
/// The atlas of the sun and moon's sprites.
pub celestial: Option<wgpu::TextureView>,
}
@@ -207,6 +211,8 @@ pub struct PackPost {
down: HashMap<wgpu::TextureFormat, (wgpu::RenderPipeline, wgpu::BindGroupLayout)>,
/// The per-draw uniforms of gbuffers programs.
draws: DrawUniforms,
/// `mc_Entity`'s table of flat terrain, and its bind group.
block_ids: Option<(wgpu::BindGroupLayout, wgpu::BindGroup)>,
/// `HORIZON_SHADER_PACK_TIMING`: GPU time of each pass, in the log.
timing: Option<super::pack_timing::PassTiming>,
/// `shaders.properties`' custom uniforms, after the frame's in `uniforms.values`.
@@ -492,6 +498,42 @@ fn attach(
})
}
/// `mc_Entity`'s table as a storage buffer (vertex stage) at set 3; -1 pairs when empty.
fn block_ids(device: &wgpu::Device, table: &[i32]) -> (wgpu::BindGroupLayout, wgpu::BindGroup) {
let empty = [-1, -1];
let table = if table.is_empty() { &empty[..] } else { table };
let buffer = wgpu::util::DeviceExt::create_buffer_init(
device,
&wgpu::util::BufferInitDescriptor {
label: Some("Shader pack block ids"),
contents: bytemuck::cast_slice(table),
usage: wgpu::BufferUsages::STORAGE,
},
);
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("Shader pack block ids"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
});
let group = device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("Shader pack block ids"),
layout: &layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: buffer.as_entire_binding(),
}],
});
(layout, group)
}
/// 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> {
@@ -501,6 +543,8 @@ pub(super) struct Geometry<'a> {
pub depth_write: bool,
/// What `texture` reads instead of the block atlas.
pub atlas: Option<wgpu::TextureView>,
/// `mc_Entity`'s table at set 3 (flat terrain).
pub ids: Option<&'a wgpu::BindGroupLayout>,
}
/// The pass of program `p`: a quad, or `geometry` drawn by the game. `None` (with a note) when
@@ -663,6 +707,7 @@ fn build_pass(
Some(&layout),
geometry.and_then(|g| g.table),
draw.then_some(draws),
geometry.and_then(|g| g.ids),
],
..Default::default()
});
@@ -745,6 +790,7 @@ fn build_pass(
mipmaps: p.mipmaps,
draw,
atlas: geometry.and_then(|g| g.atlas.clone()),
ids: geometry.is_some_and(|g| g.ids.is_some()),
groups: HashMap::new(),
})
}
@@ -796,13 +842,26 @@ impl PackPost {
used |= pass.reads | pass.written | pass.flip;
Some(pass)
};
// Flat terrain reads `mc_Entity` from a table of the sprite layers.
let block_ids = world
.terrain
.as_ref()
.filter(|g| g.table.is_some())
.map(|_| block_ids(device, world.block_ids));
let terrain_geometry = world.terrain.as_ref().map(|g| Geometry {
buffers: g.buffers,
table: g.table,
depth_write: g.depth_write,
atlas: g.atlas.clone(),
ids: block_ids.as_ref().map(|i| &i.0),
});
let terrain = post
.terrain
.zip(world.terrain.as_ref())
.zip(terrain_geometry.as_ref())
.and_then(|(p, g)| build(p, Some(g)));
let water = post
.water
.zip(world.terrain.as_ref())
.zip(terrain_geometry.as_ref())
.and_then(|(p, g)| build(p, Some(g)));
let sky = (post.sky.is_some() || post.sky_textured.is_some()).then(|| {
let geometry = PackSky::new(device);
@@ -811,6 +870,7 @@ impl PackPost {
table: Some(&geometry.layout),
depth_write: false,
atlas: None,
ids: None,
};
let basic = post.sky.and_then(|p| build(p, Some(&basic)));
let textured = Geometry {
@@ -818,6 +878,7 @@ impl PackPost {
table: None,
depth_write: false,
atlas: world.celestial.clone(),
ids: None,
};
let textured = post.sky_textured.and_then(|p| build(p, Some(&textured)));
Sky {
@@ -981,6 +1042,7 @@ impl PackPost {
mips_valid: 0,
down,
draws,
block_ids,
timing: super::pack_timing::PassTiming::new(device, queue),
custom,
atlas: world.atlas.as_ref().map(|a| a.0.clone()),
@@ -1216,6 +1278,11 @@ impl PackPost {
let mut rp = self.begin(encoder, &pass);
rp.set_pipeline(&pass.pipeline);
rp.set_bind_group(0, &group, &[]);
if pass.ids
&& let Some((_, ids)) = &self.block_ids
{
rp.set_bind_group(3, ids, &[]);
}
if pass.draw {
let stage = if translucent {
"TERRAIN_TRANSLUCENT"
+120
View File
@@ -0,0 +1,120 @@
//! `block.properties` (spec 4.1): `block.<id>=<block> <block> ...`, each block
//! `[namespace:]name` with optional state filters `:<property>=<v1>,<v2>`. Values in one filter
//! are alternatives; separate filters must all match. Names the game does not know are simply
//! never asked for. Block tags (`%tag`, tier E) are not read yet.
use crate::Properties;
#[derive(Clone, Debug, PartialEq)]
struct Rule {
id: i32,
/// `minecraft:stone`.
name: String,
filters: Vec<(String, Vec<String>)>,
}
/// The ids `block.properties` gives block states.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct BlockIds {
rules: Vec<Rule>,
/// Lines skipped and why.
pub notes: Vec<String>,
}
impl BlockIds {
pub fn parse(properties: &Properties) -> Self {
let mut out = Self::default();
for e in &properties.entries {
let Some(id) = e.key.strip_prefix("block.") else {
continue;
};
let Ok(id) = id.parse::<i32>() else {
// `layer.*` (4.2) and other keys are not ids.
continue;
};
for word in e.value.split_whitespace() {
if word.starts_with('%') {
out.notes.push(format!(
"line {}: block tags are not read yet: {word}",
e.line
));
continue;
}
let (names, filters): (Vec<&str>, Vec<&str>) =
word.split(':').partition(|p| !p.contains('='));
let name = match names[..] {
[name] => format!("minecraft:{name}"),
[namespace, name] => format!("{namespace}:{name}"),
_ => {
out.notes
.push(format!("line {}: not a block name: {word}", e.line));
continue;
}
};
let filters = filters
.iter()
.filter_map(|f| f.split_once('='))
.map(|(k, v)| (k.to_owned(), v.split(',').map(str::to_owned).collect()))
.collect();
out.rules.push(Rule { id, name, filters });
}
}
out
}
/// The id of block `name` (`minecraft:stone`) in the state with `properties`. The rule with
/// the most filters that all match wins, then the first in the file (one name belongs to
/// one id; a filtered entry is the more specific one).
pub fn id<'a>(&self, name: &str, properties: impl Fn(&str) -> Option<&'a str>) -> Option<i32> {
self.rules
.iter()
.filter(|r| r.name == name)
.filter(|r| {
r.filters
.iter()
.all(|(k, vs)| properties(k).is_some_and(|v| vs.iter().any(|x| x == v)))
})
.min_by_key(|r| std::cmp::Reverse(r.filters.len()))
.map(|r| r.id)
}
pub fn is_empty(&self) -> bool {
self.rules.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Macros;
#[test]
fn names_states_and_precedence() {
let text = "block.10=stone minecraft:dirt mod:thing \\\n grass_block:snowy=false\n\
block.11=grass_block\nblock.12=oak_leaves:persistent=true:distance=1,2 %leaves\n\
layer.translucent=glass\n";
let ids = BlockIds::parse(&Properties::parse(text, &Macros::standard()));
let none = |_: &str| None;
assert_eq!(ids.id("minecraft:stone", none), Some(10));
assert_eq!(ids.id("mod:thing", none), Some(10));
assert_eq!(ids.id("minecraft:glass", none), None);
let snowy = |s: &'static str| move |k: &str| (k == "snowy").then_some(s);
// The filtered entry is the more specific one.
assert_eq!(ids.id("minecraft:grass_block", snowy("false")), Some(10));
assert_eq!(ids.id("minecraft:grass_block", snowy("true")), Some(11));
let leaves = |p: &'static str, d: &'static str| {
move |k: &str| match k {
"persistent" => Some(p),
"distance" => Some(d),
_ => None,
}
};
assert_eq!(
ids.id("minecraft:oak_leaves", leaves("true", "2")),
Some(12)
);
assert_eq!(ids.id("minecraft:oak_leaves", leaves("true", "3")), None);
assert_eq!(ids.id("minecraft:oak_leaves", leaves("false", "1")), None);
assert_eq!(ids.notes.len(), 1, "{:?}", ids.notes);
}
}
+2
View File
@@ -12,6 +12,7 @@
//! - [`load_post`]: a dimension's `prepare`, `deferred`, `composite` and `final` programs, in order (2.1).
//! - [`Macros`] and [`eval_condition`]: the macros packs test, and the `#if` arithmetic (1.5).
mod block_ids;
mod condition;
mod config;
mod custom;
@@ -25,6 +26,7 @@ mod preprocess;
mod properties;
mod translate;
pub use block_ids::BlockIds;
pub use condition::eval_condition;
pub use config::{BUFFERS, Buffer, Config, Format, Kind, buffer_index, draw_buffers};
pub use custom::{Custom, Val};
@@ -3,7 +3,7 @@
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, and any stage but solid terrain (translucent terrain falls back to it)
face's centre with a dot; leaves (id 1 in `block.properties`) are yellow, and any stage but solid terrain (translucent terrain falls back to it)
is red. `gbuffers_skybasic` draws the sky in its vertex colour, a little greener, with magenta
stars and a dark red void; `gbuffers_textured` (the fallback of `gbuffers_skytextured`) the sun,
and the moon tinted green. `composite` puts the depth buffer's view-space distance into the blue
@@ -0,0 +1,3 @@
# Leaves draw yellow in gbuffers_terrain.
block.1=oak_leaves spruce_leaves birch_leaves jungle_leaves acacia_leaves dark_oak_leaves \
mangrove_leaves cherry_leaves azalea_leaves flowering_azalea_leaves pale_oak_leaves
@@ -9,6 +9,7 @@ varying vec2 texcoord;
varying vec2 lmcoord;
varying vec2 mid;
varying vec4 glcolor;
varying float blockId;
void main() {
vec4 color = texture2D(texture, texcoord) * glcolor;
@@ -16,6 +17,8 @@ void main() {
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);
// Leaves (block.properties' id 1) yellow.
if (abs(blockId - 1.0) < 0.5) color.rgb = vec3(color.g, color.g, 0.0);
// 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);
// Any other stage than solid terrain (translucent terrain falls back here): red.
@@ -1,11 +1,13 @@
#version 120
attribute vec4 mc_midTexCoord;
attribute vec4 mc_Entity;
varying vec2 texcoord;
varying vec2 lmcoord;
varying vec2 mid;
varying vec4 glcolor;
varying float blockId;
void main() {
gl_Position = ftransform();
@@ -13,4 +15,5 @@ void main() {
lmcoord = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;
mid = mc_midTexCoord.xy;
glcolor = gl_Color;
blockId = mc_Entity.x;
}