70b832d658
The page lists the packs in shaderpacks/: Horizon's own, marked (Horizon) and named by their manifest, then packs in the shaders/ format. One runs at a time: clicking a pack picks it, clicking the picked one turns it off, and Open Pack Folder opens the folder. The pick is saved in horizon-graphics.txt under a new key (pickedShaderPack), so the shaderPack value older versions saved stays ignored. A new pick drops the running pack and loads the new one in the next frame; HORIZON_SHADER_PACK still wins, for development. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
380 lines
16 KiB
Rust
380 lines
16 KiB
Rust
//! Shader packs: the one picked on Options > Horizon Settings > Shader Packs, or
|
||
//! `HORIZON_SHADER_PACK=<name>` (development; it wins over the pick), runs `shaderpacks/<name>`
|
||
//! (a folder or a zip beside `options.txt`) over the world; Horizon's own packs go to
|
||
//! `game/native_shaders.rs`. The programs of the current dimension's folder are loaded when the
|
||
//! dimension changes (spec 1.2).
|
||
|
||
use horizon_client::camera::CameraFluid;
|
||
use horizon_client::gfx::pack_uniforms::PackInputs;
|
||
use horizon_shaders::{BlockIds, Macros, Pack, Properties, dimension_folder, load_post};
|
||
|
||
use super::{Dim, Game};
|
||
use horizon_client::gfx::pack_draw::EntitySpan;
|
||
use std::cell::RefCell;
|
||
use std::collections::HashMap;
|
||
|
||
/// The ids a pack gives entities and block entities (4.1, 5.3), for the spans of the entity
|
||
/// geometry.
|
||
pub(super) struct PackIds {
|
||
entities: HashMap<String, i32>,
|
||
blocks: BlockIds,
|
||
/// `blockEntityId` by block state, filled as states are drawn.
|
||
block_states: RefCell<HashMap<u32, i32>>,
|
||
/// The id of a name the file lacks: 65535, or 0 without the file.
|
||
missing_entity: i32,
|
||
missing_block: i32,
|
||
/// `item.properties` (4.1) and the id of a held item the file lacks, or of an empty hand:
|
||
/// −1, or 0 without the file (5.3).
|
||
items: HashMap<String, i32>,
|
||
missing_item: i32,
|
||
/// `oldHandLight` (3.2): the first held item and light are those of the brighter hand.
|
||
old_hand_light: bool,
|
||
}
|
||
|
||
impl Game<'_> {
|
||
/// Loads the pack for the current dimension when it changed, and returns this frame's
|
||
/// inputs to the pack's uniforms.
|
||
pub(super) fn shader_pack_inputs(&mut self, camera: glam::DVec3, partial: f32) -> PackInputs {
|
||
static DEV_PACK: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
|
||
let dev = DEV_PACK.get_or_init(|| std::env::var("HORIZON_SHADER_PACK").ok());
|
||
let picked = dev.as_deref().unwrap_or(&self.settings.horizon.shader_pack);
|
||
if self.native_shaders.picked != picked {
|
||
// Another pack, or none: drop what ran.
|
||
self.native_shaders = super::native_shaders::NativeShaders::picking(picked);
|
||
self.pack_ids = None;
|
||
if let Some(renderer) = &mut self.renderer {
|
||
renderer.drop_shader_packs();
|
||
}
|
||
}
|
||
if picked.is_empty() {
|
||
return PackInputs::default();
|
||
}
|
||
let name = picked.to_owned();
|
||
let dimension = match self.dim {
|
||
Dim::Overworld => 0,
|
||
Dim::Nether => -1,
|
||
Dim::End => 1,
|
||
};
|
||
let path = Self::shader_packs_dir().join(&name);
|
||
if horizon_shader_api::package::is_pack(&path) {
|
||
// One of Horizon's own packs (game/native_shaders.rs).
|
||
self.tick_native_shader_pack(&path);
|
||
} else if let Some(renderer) = &mut self.renderer
|
||
&& renderer.shader_pack_dimension() != Some(dimension)
|
||
{
|
||
let name = name.as_str();
|
||
let started = std::time::Instant::now();
|
||
match Pack::open(&path) {
|
||
Ok(pack) => {
|
||
let macros = Macros::standard();
|
||
let text = pack.read_text("shaders.properties").unwrap_or_default();
|
||
let properties = Properties::parse(&text, ¯os);
|
||
for note in &properties.notes {
|
||
log::warn!(
|
||
"shader pack {name}: shaders.properties:{}: {}",
|
||
note.line,
|
||
note.message
|
||
);
|
||
}
|
||
let folder = dimension_folder(&pack, dimension);
|
||
let inputs = renderer.pack_inputs();
|
||
let mut post = load_post(&pack, &folder, &properties, ¯os, &inputs);
|
||
let biomes = self.res.biomes();
|
||
let names: Vec<&str> = biomes.list.iter().map(|b| b.name.as_str()).collect();
|
||
post.custom.set_biomes(&names);
|
||
let text = pack.read_text("block.properties").unwrap_or_default();
|
||
let ids = BlockIds::parse(&Properties::parse(&text, ¯os));
|
||
for note in &ids.notes {
|
||
log::warn!("shader pack {name}: block.properties: {note}");
|
||
}
|
||
let text = pack.read_text("entity.properties");
|
||
let entities = BlockIds::parse_kind(
|
||
&Properties::parse(text.as_deref().unwrap_or_default(), ¯os),
|
||
"entity",
|
||
);
|
||
let text = pack.read_text("item.properties");
|
||
let items = BlockIds::parse_kind(
|
||
&Properties::parse(text.as_deref().unwrap_or_default(), ¯os),
|
||
"item",
|
||
);
|
||
self.pack_ids = Some(PackIds {
|
||
items: items.plain(),
|
||
missing_item: if text.is_some() { -1 } else { 0 },
|
||
// OQ-21: on unless the pack turns it off.
|
||
old_hand_light: properties
|
||
.get("oldHandLight")
|
||
.is_none_or(|v| v.trim() != "false"),
|
||
entities: entities.plain(),
|
||
missing_entity: if text.is_some() { 65535 } else { 0 },
|
||
missing_block: if ids.is_empty() { 0 } else { 65535 },
|
||
blocks: ids.clone(),
|
||
block_states: RefCell::default(),
|
||
});
|
||
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}");
|
||
}
|
||
log::info!(
|
||
"shader pack {name}: folder `{folder}`, {} programs ({}) in {} ms",
|
||
programs.len(),
|
||
programs.join(" "),
|
||
started.elapsed().as_millis()
|
||
);
|
||
}
|
||
}
|
||
Err(e) => {
|
||
self.pack_ids = None;
|
||
log::warn!("shader pack {name}: {e}");
|
||
renderer.set_shader_pack(dimension, None, &[]);
|
||
}
|
||
}
|
||
}
|
||
let lerp = |(old, new): (f32, f32)| old + (new - old) * partial;
|
||
let at = camera.floor().as_ivec3();
|
||
let light = self.world.light(at.x, at.y, at.z);
|
||
let biome = self
|
||
.world
|
||
.chunks
|
||
.get(&(at.x >> 4, at.z >> 4))
|
||
.map(|c| c.biome(at.x, at.y, at.z));
|
||
let (held_items, held_light) = self.held_for_pack();
|
||
let info = biome.map(|b| self.res.biomes().get(b));
|
||
PackInputs {
|
||
day_time: self.world.day_time,
|
||
rain: lerp(self.world.weather.rain_level),
|
||
thunder: lerp(self.world.weather.thunder_level),
|
||
eye_in: match self.camera_fluid(camera.to_array()) {
|
||
CameraFluid::None => 0,
|
||
CameraFluid::Water => 1,
|
||
CameraFluid::Lava => 2,
|
||
CameraFluid::PowderSnow => 3,
|
||
},
|
||
blindness: f32::from(u8::from(self.potions.effects.get("blindness").is_some())),
|
||
eye_light: [i32::from(light & 15), i32::from(light >> 4)],
|
||
hide_gui: self.ui.hide_gui,
|
||
brightness: self.settings.gamma,
|
||
biome: biome.map_or(-1, i32::from),
|
||
biome_category: info.map_or(0, |b| horizon_shaders::biome_category(&b.name)),
|
||
biome_precipitation: info.map_or(0, |b| {
|
||
horizon_shaders::biome_precipitation(b.precipitation, b.temperature)
|
||
}),
|
||
temperature: info.map_or(0.0, |b| b.temperature),
|
||
rainfall: info.map_or(0.0, |b| b.downfall),
|
||
held_items,
|
||
held_light,
|
||
}
|
||
}
|
||
|
||
/// `heldItemId(2)` and `heldBlockLightValue(2)`: main hand, then off hand (5.2, 5.3).
|
||
fn held_for_pack(&mut self) -> ([i32; 2], [i32; 2]) {
|
||
if self.pack_ids.is_none() {
|
||
return ([0; 2], [0; 2]);
|
||
}
|
||
if self.luminance.is_none() {
|
||
self.luminance = Some(horizon_client::dynamic_light::Luminance::new(
|
||
self.res.facts(),
|
||
self.pack.assets.registry,
|
||
));
|
||
}
|
||
let (Some(ids), Some(lum)) = (&self.pack_ids, &self.luminance) else {
|
||
return ([0; 2], [0; 2]);
|
||
};
|
||
let hands = [self.selected, super::shield::OFFHAND_SLOT].map(|slot| {
|
||
let item = self.stack_in(slot).map(|s| &*s.item);
|
||
let id = item.and_then(|i| ids.items.get(i).copied());
|
||
(
|
||
id.unwrap_or(ids.missing_item),
|
||
item.map_or(0, |i| i32::from(lum.of(i))),
|
||
)
|
||
});
|
||
let first = if ids.old_hand_light && hands[1].1 > hands[0].1 {
|
||
hands[1]
|
||
} else {
|
||
hands[0]
|
||
};
|
||
([first.0, hands[1].0], [first.1, hands[1].1])
|
||
}
|
||
}
|
||
|
||
impl Game<'_> {
|
||
/// Starts a span of the entity geometry at vertex `start` for entity type `name`
|
||
/// (`minecraft:zombie`; "" for one the pack cannot name). Nothing without a pack.
|
||
pub(super) fn mark_entity(&self, start: usize, name: &str) {
|
||
self.mark_entity_part(start, name, false);
|
||
}
|
||
|
||
/// Starts a span for entity geometry that blends (name tags, shadows).
|
||
pub(super) fn mark_translucent_entity(&self, start: usize, name: &str) {
|
||
self.mark_entity_part(start, name, true);
|
||
}
|
||
|
||
fn mark_entity_part(&self, start: usize, name: &str, translucent: bool) {
|
||
if let Some(ids) = &self.pack_ids {
|
||
let id = ids.entities.get(name).copied();
|
||
self.push_span(start, "ENTITIES", id.unwrap_or(ids.missing_entity));
|
||
if let Some(last) = self.entity_spans.borrow_mut().last_mut() {
|
||
last.translucent = translucent;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Starts a span for the block entity of block state `state`.
|
||
pub(super) fn mark_block_entity(&self, start: usize, state: u32) {
|
||
let Some(ids) = &self.pack_ids else { return };
|
||
let id = *ids
|
||
.block_states
|
||
.borrow_mut()
|
||
.entry(state)
|
||
.or_insert_with(|| {
|
||
let registry = self.pack.assets.registry;
|
||
registry
|
||
.state(horizon_world::registry::BlockStateId(state))
|
||
.and_then(|s| {
|
||
ids.blocks
|
||
.id(&s.name, |k| s.properties.get(k).map(String::as_str))
|
||
})
|
||
.unwrap_or(ids.missing_block)
|
||
});
|
||
self.push_span(start, "BLOCK_ENTITIES", id);
|
||
let emission = self
|
||
.res
|
||
.facts()
|
||
.get(horizon_world::registry::BlockStateId(state))
|
||
.emission;
|
||
if let Some(last) = self.entity_spans.borrow_mut().last_mut() {
|
||
last.emissive = emission > 0;
|
||
}
|
||
}
|
||
|
||
/// Starts the span of the local player's model; `hidden` in first person, where only a
|
||
/// pack's shadow pass draws it.
|
||
pub(super) fn mark_player(&self, start: usize, hidden: bool) {
|
||
self.mark_entity(start, "minecraft:player");
|
||
if self.pack_ids.is_some()
|
||
&& let Some(last) = self.entity_spans.borrow_mut().last_mut()
|
||
{
|
||
last.player = true;
|
||
last.hidden = hidden;
|
||
}
|
||
}
|
||
|
||
/// Marks vertices `start..end` (one entity) as hidden from the camera, for an entity
|
||
/// Horizon's culling would drop but a pack's shadow pass still draws (`EntitySpan::hide`).
|
||
pub(super) fn hide_spans_from(&self, start: usize, end: usize) -> bool {
|
||
EntitySpan::hide(&mut self.entity_spans.borrow_mut(), start, end)
|
||
}
|
||
|
||
/// Starts a span of geometry that is not an entity (`PARTICLES`, `DESTROY`...).
|
||
pub(super) fn mark_stage(&self, start: usize, stage: &str) {
|
||
if self.pack_ids.is_some() {
|
||
self.push_span(start, stage, 0);
|
||
}
|
||
}
|
||
|
||
fn push_span(&self, start: usize, stage: &str, id: i32) {
|
||
let mut spans = self.entity_spans.borrow_mut();
|
||
let span = EntitySpan {
|
||
start: start as u32,
|
||
stage: horizon_shaders::render_stage(stage),
|
||
id,
|
||
..Default::default()
|
||
};
|
||
// A span that drew nothing gives way to the next.
|
||
match spans.last_mut() {
|
||
Some(last) if last.start == span.start => *last = span,
|
||
_ => spans.push(span),
|
||
}
|
||
}
|
||
}
|
||
|
||
/// `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
|
||
}
|