Merge feat/java-packs-photon-chocapic into feat/java-gbuffers-terrain
Brings main's shader api 2 (#1414) up the stack. One path for blending: the terrain programs' blend.* now go through Pass.blend/buffer_blends like composite and final (java_pack::pass_blends). depthtex1/depthtex2 are one source, DepthSolid: Horizon's depth before translucent geometry, then the pack's solid terrain's. Horizon's world goes to the pack's first terrain target (colortex1 for MakeUp). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,9 +7,9 @@
|
||||
//! terrain (`gfx/java_terrain.rs`); the rest of the world is Horizon's.
|
||||
use horizon_shaderpack::{
|
||||
JavaPack,
|
||||
pipeline::{Directives, GeometryProgram, Program, TargetSettings},
|
||||
pipeline::{Directives, GeometryProgram, Pass, Program, TargetSettings},
|
||||
preprocess::Environment,
|
||||
properties::SkyDirectives,
|
||||
properties::{Blend, SkyDirectives},
|
||||
uniforms::CustomUniforms,
|
||||
};
|
||||
use std::{
|
||||
@@ -24,14 +24,18 @@ pub struct JavaFolder {
|
||||
/// `begin*`, `prepare*`, `deferred*`, `composite*`, in order.
|
||||
pub passes: Vec<Program>,
|
||||
/// How many of `passes` run before the world (`begin*`, `prepare*`): the
|
||||
/// world's colour and depth enter `colortex0` and `depthtex*` after them.
|
||||
/// world's colour and depth enter `world_target` and `depthtex*` after them.
|
||||
pub world_at: usize,
|
||||
/// The colour target the world goes into, where the pack's terrain
|
||||
/// program draws (MakeUp: colortex1).
|
||||
pub world_target: u32,
|
||||
/// The targets whose alternate side the world is written to.
|
||||
pub flipped_after_prepare: BTreeSet<u32>,
|
||||
pub final_pass: Option<Program>,
|
||||
/// The targets whose alternate side holds the frame's last write.
|
||||
pub flipped_at_end: BTreeSet<u32>,
|
||||
/// `sunPathRotation` and `eyeBrightnessHalflife`.
|
||||
/// `sunPathRotation`, `eyeBrightnessHalflife`, `wetnessHalflife` and
|
||||
/// `drynessHalflife`.
|
||||
pub directives: Directives,
|
||||
/// The programs Horizon's solid and translucent terrain are drawn with,
|
||||
/// each translated for instanced and flat draws (`variants[0]`).
|
||||
@@ -46,6 +50,21 @@ pub struct JavaFolder {
|
||||
pub water_blends: Vec<Option<wgpu::BlendState>>,
|
||||
}
|
||||
|
||||
/// The key of `texture.noise` (`noisetex`) in [`JavaPasses::custom_textures`].
|
||||
pub const NOISE_KEY: &str = "noise";
|
||||
|
||||
/// The key of `texture.<stage>.<sampler>` in [`JavaPasses::custom_textures`]
|
||||
/// for a pass of `program` (`world0/composite2`): Iris's texture stages, where
|
||||
/// `final` reads the `composite` ones.
|
||||
pub fn texture_key(program: &str, sampler: &str) -> String {
|
||||
let name = program.rsplit('/').next().unwrap_or(program);
|
||||
let stage = match name.trim_end_matches(|c: char| c.is_ascii_digit()) {
|
||||
"final" => "composite",
|
||||
stage => stage,
|
||||
};
|
||||
format!("{stage}.{sampler}")
|
||||
}
|
||||
|
||||
/// A loaded Java pack.
|
||||
pub struct JavaPasses {
|
||||
pub folders: Vec<JavaFolder>,
|
||||
@@ -55,6 +74,9 @@ pub struct JavaPasses {
|
||||
pub sky: SkyDirectives,
|
||||
/// `block.properties`: block name → `mc_Entity` id.
|
||||
pub block_ids: BTreeMap<String, i32>,
|
||||
/// `texture.<stage>.<sampler>` and `texture.noise` images, by
|
||||
/// `"<stage>.<sampler>"` and [`NOISE_KEY`].
|
||||
pub custom_textures: BTreeMap<String, crate::shader_pack::Rgba>,
|
||||
}
|
||||
|
||||
impl JavaPasses {
|
||||
@@ -155,12 +177,7 @@ pub fn load(
|
||||
let translucent_at = early.clone().count() + plan.deferred.len();
|
||||
let blends = |g: &Option<GeometryProgram>, default| {
|
||||
g.as_ref().map_or_else(Vec::new, |g| {
|
||||
geometry_blends(
|
||||
&properties,
|
||||
&g.program.pass.program,
|
||||
&g.program.draw_buffers,
|
||||
default,
|
||||
)
|
||||
pass_blends(&g.program.pass, &g.program.draw_buffers, default)
|
||||
})
|
||||
};
|
||||
let terrain_blends = blends(&terrain, None);
|
||||
@@ -184,6 +201,7 @@ pub fn load(
|
||||
folder: name.to_owned(),
|
||||
targets: plan.targets,
|
||||
world_at: early.count(),
|
||||
world_target: plan.world_target,
|
||||
passes,
|
||||
flipped_after_prepare: plan.flipped_after_prepare,
|
||||
final_pass,
|
||||
@@ -200,6 +218,27 @@ pub fn load(
|
||||
if folders.is_empty() {
|
||||
return Err("no deferred, composite or final programs".into());
|
||||
}
|
||||
let mut custom_textures = BTreeMap::new();
|
||||
for tex in properties.custom_textures() {
|
||||
let key = match tex.stage {
|
||||
Some(stage) => format!("{stage}.{}", tex.sampler),
|
||||
None if tex.sampler == NOISE_KEY => NOISE_KEY.to_owned(),
|
||||
None => continue,
|
||||
};
|
||||
// Only PNGs for now: resource locations and raw `.dat` textures are
|
||||
// skipped and the sampler reads what it would without them.
|
||||
let image = pack
|
||||
.files
|
||||
.read_bytes(&tex.path)
|
||||
.ok_or_else(|| "not in the pack".to_owned())
|
||||
.and_then(|bytes| crate::shader_pack::decode_png(&bytes));
|
||||
match image {
|
||||
Ok(image) => {
|
||||
custom_textures.insert(key, image);
|
||||
}
|
||||
Err(e) => log::warn!("Horizon: shader pack texture {}: {e}", tex.path),
|
||||
}
|
||||
}
|
||||
let uniforms = properties.custom_uniforms();
|
||||
for problem in &uniforms.problems {
|
||||
log::warn!("Horizon: shader pack custom uniform {problem}");
|
||||
@@ -212,76 +251,61 @@ pub fn load(
|
||||
folders,
|
||||
uniforms,
|
||||
sky: properties.sky(),
|
||||
custom_textures,
|
||||
block_ids,
|
||||
})
|
||||
}
|
||||
|
||||
/// A geometry program's blending per draw buffer: `default`, or the
|
||||
/// pack's `blend.<program>` and `blend.<program>.<buffer>` (Iris's
|
||||
/// `BlendModeOverride`; the folder isn't part of the name).
|
||||
fn geometry_blends(
|
||||
properties: &horizon_shaderpack::properties::ShaderProperties,
|
||||
program: &str,
|
||||
/// Each draw buffer's blending, as Iris's `BlendModeOverride`:
|
||||
/// `blend.<program>.<buffer>`, else `blend.<program>`, else `default`
|
||||
/// (none for full-screen passes, vanilla's for the geometry programs).
|
||||
pub(crate) fn pass_blends(
|
||||
pass: &Pass,
|
||||
draw_buffers: &[u32],
|
||||
default: Option<wgpu::BlendState>,
|
||||
) -> Vec<Option<wgpu::BlendState>> {
|
||||
use horizon_shaderpack::properties::Blend;
|
||||
let name = program.rsplit('/').next().unwrap_or(program);
|
||||
let state = |blend: &Result<Blend, String>| match blend {
|
||||
Ok(Blend::Off) => Some(None),
|
||||
Ok(Blend::Factors(f)) => {
|
||||
let [a, b, c, d] = f.each_ref().map(|f| blend_factor(f));
|
||||
Some(Some(wgpu::BlendState {
|
||||
color: wgpu::BlendComponent {
|
||||
src_factor: a?,
|
||||
dst_factor: b?,
|
||||
operation: wgpu::BlendOperation::Add,
|
||||
},
|
||||
alpha: wgpu::BlendComponent {
|
||||
src_factor: c?,
|
||||
dst_factor: d?,
|
||||
operation: wgpu::BlendOperation::Add,
|
||||
},
|
||||
}))
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("Horizon: shader pack blend.{name}: {e}");
|
||||
None
|
||||
}
|
||||
};
|
||||
let all = properties
|
||||
.blend(name)
|
||||
.and_then(|b| state(&b))
|
||||
.unwrap_or(default);
|
||||
let buffers = properties.buffer_blends(name);
|
||||
let all = pass.blend.as_ref().map_or(default, blend_state);
|
||||
draw_buffers
|
||||
.iter()
|
||||
.map(|i| {
|
||||
buffers
|
||||
pass.buffer_blends
|
||||
.iter()
|
||||
.find(|(b, _)| b == i)
|
||||
.and_then(|(_, b)| state(b))
|
||||
.unwrap_or(all)
|
||||
.map_or(all, |(_, b)| blend_state(b))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A GL blend factor's wgpu one.
|
||||
fn blend_factor(name: &str) -> Option<wgpu::BlendFactor> {
|
||||
use wgpu::BlendFactor as F;
|
||||
Some(match name {
|
||||
"ZERO" => F::Zero,
|
||||
"ONE" => F::One,
|
||||
"SRC_COLOR" => F::Src,
|
||||
"ONE_MINUS_SRC_COLOR" => F::OneMinusSrc,
|
||||
"DST_COLOR" => F::Dst,
|
||||
"ONE_MINUS_DST_COLOR" => F::OneMinusDst,
|
||||
"SRC_ALPHA" => F::SrcAlpha,
|
||||
"ONE_MINUS_SRC_ALPHA" => F::OneMinusSrcAlpha,
|
||||
"DST_ALPHA" => F::DstAlpha,
|
||||
"ONE_MINUS_DST_ALPHA" => F::OneMinusDstAlpha,
|
||||
"SRC_ALPHA_SATURATE" => F::SrcAlphaSaturated,
|
||||
_ => return None,
|
||||
/// A `blend.*` value as wgpu's blending (`off`: none). The factors are the
|
||||
/// GL ones `ShaderProperties` accepts.
|
||||
pub(crate) fn blend_state(blend: &Blend) -> Option<wgpu::BlendState> {
|
||||
let Blend::Factors(factors) = blend else {
|
||||
return None;
|
||||
};
|
||||
let factor = |name: &str| {
|
||||
use wgpu::BlendFactor as F;
|
||||
match name {
|
||||
"ZERO" => F::Zero,
|
||||
"SRC_COLOR" => F::Src,
|
||||
"ONE_MINUS_SRC_COLOR" => F::OneMinusSrc,
|
||||
"DST_COLOR" => F::Dst,
|
||||
"ONE_MINUS_DST_COLOR" => F::OneMinusDst,
|
||||
"SRC_ALPHA" => F::SrcAlpha,
|
||||
"ONE_MINUS_SRC_ALPHA" => F::OneMinusSrcAlpha,
|
||||
"DST_ALPHA" => F::DstAlpha,
|
||||
"ONE_MINUS_DST_ALPHA" => F::OneMinusDstAlpha,
|
||||
"SRC_ALPHA_SATURATE" => F::SrcAlphaSaturated,
|
||||
_ => F::One,
|
||||
}
|
||||
};
|
||||
let component = |src: &str, dst: &str| wgpu::BlendComponent {
|
||||
src_factor: factor(src),
|
||||
dst_factor: factor(dst),
|
||||
operation: wgpu::BlendOperation::Add,
|
||||
};
|
||||
Some(wgpu::BlendState {
|
||||
color: component(&factors[0], &factors[1]),
|
||||
alpha: component(&factors[2], &factors[3]),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -289,6 +313,17 @@ fn blend_factor(name: &str) -> Option<wgpu::BlendFactor> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn custom_textures_belong_to_iris_s_stages() {
|
||||
assert_eq!(texture_key("world0/composite2", "gaux1"), "composite.gaux1");
|
||||
assert_eq!(texture_key("final", "colortex4"), "composite.colortex4");
|
||||
assert_eq!(
|
||||
texture_key("world-1/deferred10", "noisetex"),
|
||||
"deferred.noisetex"
|
||||
);
|
||||
assert_eq!(texture_key("prepare", "depthtex2"), "prepare.depthtex2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn loads_the_full_screen_passes_of_a_java_pack() {
|
||||
let dir = Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
|
||||
Reference in New Issue
Block a user