Merge pull request #1669 from ludovic111/feat/shader-packs-gbuffers-translate
Shader packs, clean room 11: translating gbuffers programs
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
//! - [`Properties`]: `shaders.properties` and the id files, with their conditional directives (3.1, 1.5).
|
||||
//! - [`Lang`]: the pack's translated names (8.4).
|
||||
//! - [`preprocess`]: a stage file through `#include` and the GLSL preprocessor, lines mapped back (1.4, 1.5).
|
||||
//! - [`translate_composite`]: a composite-style program into naga modules and its interface (1.6, 2.2).
|
||||
//! - [`translate_composite`], [`translate_gbuffers`]: a program into naga modules and its interface (1.6, 2.2).
|
||||
//! - [`Config`] and [`draw_buffers`]: the buffer settings and output lists stage files carry (1.7, 6.1).
|
||||
//! - [`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).
|
||||
@@ -32,4 +32,7 @@ pub use pack::{Pack, PackError};
|
||||
pub use post::{Group, Post, PostProgram, dimension_folder, load_post};
|
||||
pub use preprocess::{Diagnostic, Origin, Preprocessed, preprocess};
|
||||
pub use properties::{Entry, Note, Properties};
|
||||
pub use translate::{Error, Interface, Program, Sampler, Target, Uniform, translate_composite};
|
||||
pub use translate::{
|
||||
Error, GbuffersInputs, Interface, Program, Sampler, Target, Uniform, translate_composite,
|
||||
translate_gbuffers,
|
||||
};
|
||||
|
||||
@@ -86,18 +86,74 @@ pub enum Target {
|
||||
Window,
|
||||
}
|
||||
|
||||
/// What a gbuffers-style program's vertex stage reads. The game writes it: it knows its vertex
|
||||
/// formats, the translator only places it.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct GbuffersInputs {
|
||||
/// GLSL declarations: the vertex inputs and any bindings of set 1.
|
||||
pub declarations: String,
|
||||
/// GLSL statements that set `hz_Vertex`, `hz_Color`, `hz_MultiTexCoord0`,
|
||||
/// `hz_MultiTexCoord1` and `hz_Normal` (the compatibility inputs), run first in `main`.
|
||||
pub body: String,
|
||||
/// The value of each attribute a pack may declare (`mc_Entity`, `vaPosition`...), as a GLSL
|
||||
/// expression of the declared type's size; an attribute not listed reads zero.
|
||||
pub attributes: Vec<(String, String)>,
|
||||
}
|
||||
|
||||
/// Translates a composite-style program (`composite`, `deferred`, `final`...).
|
||||
pub fn translate_composite(
|
||||
vertex: &Preprocessed,
|
||||
fragment: &Preprocessed,
|
||||
target: Target,
|
||||
) -> Result<Program, Vec<Error>> {
|
||||
translate(vertex, fragment, target, None)
|
||||
}
|
||||
|
||||
/// Translates a gbuffers-style program (`gbuffers_terrain`...) drawing the game's geometry
|
||||
/// described by `inputs`, into `target` (the pack's buffers).
|
||||
pub fn translate_gbuffers(
|
||||
vertex: &Preprocessed,
|
||||
fragment: &Preprocessed,
|
||||
target: Target,
|
||||
inputs: &GbuffersInputs,
|
||||
) -> Result<Program, Vec<Error>> {
|
||||
translate(vertex, fragment, target, Some(inputs))
|
||||
}
|
||||
|
||||
/// The matrices of the compatibility profile in a gbuffers-style program, from the frame's.
|
||||
const GBUFFERS_MATRICES: &str = " hz_ModelViewMatrix = gbufferModelView;\n \
|
||||
hz_ProjectionMatrix = gbufferProjection;\n \
|
||||
hz_ModelViewProjectionMatrix = gbufferProjection * gbufferModelView;\n \
|
||||
hz_ModelViewMatrixInverse = gbufferModelViewInverse;\n \
|
||||
hz_ProjectionMatrixInverse = gbufferProjectionInverse;\n \
|
||||
hz_NormalMatrix = mat3(gbufferModelView);\n \
|
||||
mat4 hz_light = mat4(vec4(1.0 / 256.0, 0.0, 0.0, 0.0), vec4(0.0, 1.0 / 256.0, 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(1.0 / 32.0, 1.0 / 32.0, 0.0, 1.0));\n \
|
||||
hz_TextureMatrix = mat4[8](mat4(1.0), hz_light, hz_light, mat4(1.0), mat4(1.0), mat4(1.0), mat4(1.0), mat4(1.0));\n";
|
||||
|
||||
fn translate(
|
||||
vertex: &Preprocessed,
|
||||
fragment: &Preprocessed,
|
||||
target: Target,
|
||||
gbuffers: Option<&GbuffersInputs>,
|
||||
) -> Result<Program, Vec<Error>> {
|
||||
let mut v = Stage::scan(vertex, ShaderStage::Vertex);
|
||||
let mut f = Stage::scan(fragment, ShaderStage::Fragment);
|
||||
v.gbuffers = gbuffers.is_some();
|
||||
f.gbuffers = gbuffers.is_some();
|
||||
let mut errors: Vec<Error> = v.errors.drain(..).chain(f.errors.drain(..)).collect();
|
||||
|
||||
// One uniform block and one set of samplers for both stages.
|
||||
let mut uniforms: BTreeMap<String, String> = BTreeMap::new();
|
||||
if gbuffers.is_some() {
|
||||
for name in [
|
||||
"gbufferModelView",
|
||||
"gbufferModelViewInverse",
|
||||
"gbufferProjection",
|
||||
"gbufferProjectionInverse",
|
||||
] {
|
||||
uniforms.insert(name.to_owned(), "mat4".to_owned());
|
||||
}
|
||||
}
|
||||
let mut samplers: BTreeMap<String, String> = BTreeMap::new();
|
||||
for stage in [&v, &f] {
|
||||
for (name, ty, at) in &stage.uniforms {
|
||||
@@ -169,17 +225,37 @@ pub fn translate_composite(
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
if let (Some(inputs), ShaderStage::Vertex) = (gbuffers, stage.kind) {
|
||||
h.push_str(&inputs.declarations);
|
||||
h.push('\n');
|
||||
}
|
||||
h.push_str(&stage.prelude());
|
||||
h
|
||||
};
|
||||
let flip = if target == Target::Buffers { "-" } else { "" };
|
||||
let fog =
|
||||
" hz_Fog = hz_FogParameters(hz_fog_color, hz_fog.x, hz_fog.y, hz_fog.z, hz_fog.w);\n";
|
||||
let mut vertex_main = String::from(
|
||||
"\nvoid main() {\n vec2 hz_p = hz_quad[gl_VertexIndex];\n hz_Vertex = vec4(hz_p, 0.0, 1.0);\n \
|
||||
hz_MultiTexCoord0 = hz_Vertex;\n hz_Color = vec4(1.0);\n hz_Normal = vec3(0.0, 0.0, 1.0);\n",
|
||||
);
|
||||
for (name, value) in &v.attributes {
|
||||
let mut vertex_main = String::from("\nvoid main() {\n");
|
||||
match gbuffers {
|
||||
None => vertex_main.push_str(
|
||||
" vec2 hz_p = hz_quad[gl_VertexIndex];\n hz_Vertex = vec4(hz_p, 0.0, 1.0);\n \
|
||||
hz_MultiTexCoord0 = hz_Vertex;\n hz_Color = vec4(1.0);\n hz_Normal = vec3(0.0, 0.0, 1.0);\n",
|
||||
),
|
||||
Some(inputs) => {
|
||||
vertex_main.push_str(GBUFFERS_MATRICES);
|
||||
vertex_main.push_str(&inputs.body);
|
||||
}
|
||||
}
|
||||
for (name, value, ty) in &v.attributes {
|
||||
let value = match gbuffers {
|
||||
// Our quad's values for a composite.
|
||||
None => value.clone(),
|
||||
// The game's, or zero for anything it doesn't provide.
|
||||
Some(inputs) => match inputs.attributes.iter().find(|a| &a.0 == name) {
|
||||
Some((_, given)) => format!("{ty}({given})"),
|
||||
None => format!("{ty}(0)"),
|
||||
},
|
||||
};
|
||||
writeln!(vertex_main, " {name} = {value};").unwrap();
|
||||
}
|
||||
vertex_main.push_str(fog);
|
||||
@@ -192,6 +268,9 @@ pub fn translate_composite(
|
||||
)
|
||||
.unwrap();
|
||||
let mut fragment_main = String::from("\nvoid main() {\n");
|
||||
if gbuffers.is_some() {
|
||||
fragment_main.push_str(GBUFFERS_MATRICES);
|
||||
}
|
||||
fragment_main.push_str(fog);
|
||||
if f.frag_coord {
|
||||
fragment_main.push_str(match target {
|
||||
@@ -444,6 +523,66 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gbuffers_programs_read_the_games_vertices() {
|
||||
let vsh = "#version 120\nattribute vec4 mc_Entity;\nattribute vec2 mc_midTexCoord;\nvarying vec2 uv;\nvarying vec2 light;\n\
|
||||
varying vec4 color;\nvarying vec3 normal;\nvarying float id;\nvoid main() {\n \
|
||||
gl_Position = ftransform();\n uv = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;\n \
|
||||
light = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;\n color = gl_Color;\n \
|
||||
normal = normalize(gl_NormalMatrix * gl_Normal);\n id = mc_Entity.x + mc_midTexCoord.x;\n}\n";
|
||||
let fsh = "#version 120\nuniform sampler2D texture;\nuniform sampler2D lightmap;\nuniform mat4 gbufferModelView;\n\
|
||||
varying vec2 uv;\nvarying vec2 light;\nvarying vec4 color;\nvarying vec3 normal;\nvarying float id;\n\
|
||||
void main() {\n vec4 c = texture2D(texture, uv) * texture2D(lightmap, light) * color;\n \
|
||||
/* DRAWBUFFERS:02 */\n gl_FragData[0] = c * gl_FragCoord.z;\n \
|
||||
gl_FragData[1] = vec4(normal * 0.5 + 0.5, id);\n}\n";
|
||||
let files: Vec<(String, &str)> =
|
||||
[("gbuffers_terrain.vsh", vsh), ("gbuffers_terrain.fsh", fsh)]
|
||||
.iter()
|
||||
.map(|(p, t)| (format!("shaders/{p}"), *t))
|
||||
.collect();
|
||||
let files: Vec<(&str, &str)> = files.iter().map(|(p, t)| (p.as_str(), *t)).collect();
|
||||
let pack = Pack::from_zip_bytes("P.zip", zip(&files)).unwrap();
|
||||
let m = Macros::standard();
|
||||
let inputs = GbuffersInputs {
|
||||
declarations: "layout(location = 0) in vec3 game_pos;\nlayout(location = 1) in uvec2 game_uv;".into(),
|
||||
body: " hz_Vertex = vec4(game_pos, 1.0);\n hz_MultiTexCoord0 = vec4(vec2(game_uv) / 32768.0, 0.0, 1.0);\n \
|
||||
hz_MultiTexCoord1 = vec4(240.0, 240.0, 0.0, 1.0);\n hz_Color = vec4(1.0);\n hz_Normal = vec3(0.0, 1.0, 0.0);\n"
|
||||
.into(),
|
||||
attributes: vec![("mc_midTexCoord".into(), "vec2(game_uv) / 32768.0".into())],
|
||||
};
|
||||
let p = translate_gbuffers(
|
||||
&preprocess(&pack, "gbuffers_terrain.vsh", &m),
|
||||
&preprocess(&pack, "gbuffers_terrain.fsh", &m),
|
||||
Target::Buffers,
|
||||
&inputs,
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("{e:?}"));
|
||||
let names: Vec<&str> = p
|
||||
.interface
|
||||
.uniforms
|
||||
.iter()
|
||||
.map(|u| u.name.as_str())
|
||||
.collect();
|
||||
for name in [
|
||||
"gbufferModelView",
|
||||
"gbufferProjection",
|
||||
"gbufferModelViewInverse",
|
||||
] {
|
||||
assert!(names.contains(&name), "{name} missing from {names:?}");
|
||||
}
|
||||
assert_eq!(p.interface.outputs, vec![0, 1]);
|
||||
let inputs: Vec<&str> = p.vertex.entry_points[0]
|
||||
.function
|
||||
.arguments
|
||||
.iter()
|
||||
.filter_map(|a| a.name.as_deref())
|
||||
.collect();
|
||||
assert!(
|
||||
inputs.contains(&"game_pos") && inputs.contains(&"game_uv"),
|
||||
"{inputs:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_point_at_the_pack_file_and_line() {
|
||||
let fsh =
|
||||
|
||||
@@ -18,12 +18,22 @@ impl Stage<'_> {
|
||||
vec3 hz_modf(vec3 x, out vec3 i) { i = trunc(x); return x - i; }\n\
|
||||
vec4 hz_modf(vec4 x, out vec4 i) { i = trunc(x); return x - i; }\n\
|
||||
struct hz_FogParameters { vec4 color; float density; float start; float end; float scale; };\n\
|
||||
hz_FogParameters hz_Fog;\n\
|
||||
const mat4 hz_ModelViewMatrix = mat4(1.0); const mat4 hz_ProjectionMatrix = mat4(2.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, -1.0, 0.0, 1.0);\n\
|
||||
hz_FogParameters hz_Fog;\n",
|
||||
);
|
||||
if self.gbuffers {
|
||||
// Set by our `main` from the frame's matrices (gbuffers.rs).
|
||||
p.push_str(
|
||||
"mat4 hz_ModelViewMatrix; mat4 hz_ProjectionMatrix; mat4 hz_ModelViewProjectionMatrix;\n\
|
||||
mat4 hz_ModelViewMatrixInverse; mat4 hz_ProjectionMatrixInverse; mat3 hz_NormalMatrix; mat4 hz_TextureMatrix[8];\n",
|
||||
);
|
||||
} else {
|
||||
p.push_str(
|
||||
"const mat4 hz_ModelViewMatrix = mat4(1.0); const mat4 hz_ProjectionMatrix = mat4(2.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -1.0, -1.0, 0.0, 1.0);\n\
|
||||
const mat4 hz_ModelViewProjectionMatrix = hz_ProjectionMatrix; const mat4 hz_ModelViewMatrixInverse = mat4(1.0); const mat3 hz_NormalMatrix = mat3(1.0);\n\
|
||||
const mat4 hz_ProjectionMatrixInverse = mat4(0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.5, 0.5, 0.0, 1.0);\n\
|
||||
const mat4 hz_TextureMatrix[8] = mat4[8](mat4(1.0), mat4(1.0), mat4(1.0), mat4(1.0), mat4(1.0), mat4(1.0), mat4(1.0), mat4(1.0));\n",
|
||||
);
|
||||
);
|
||||
}
|
||||
if self.kind == ShaderStage::Vertex {
|
||||
p.push_str(
|
||||
"const vec2 hz_quad[6] = vec2[6](vec2(0.0, 0.0), vec2(1.0, 0.0), vec2(1.0, 1.0), vec2(0.0, 0.0), vec2(1.0, 1.0), vec2(0.0, 1.0));\n\
|
||||
|
||||
@@ -29,7 +29,10 @@ pub(super) struct Stage<'p> {
|
||||
pub(super) ins: Vec<Var>,
|
||||
pub(super) outs: Vec<Var>,
|
||||
/// Vertex inputs, now private globals, and the value our `main` gives each.
|
||||
pub(super) attributes: Vec<(String, String)>,
|
||||
/// Vertex inputs: name, our composite value, type.
|
||||
pub(super) attributes: Vec<(String, String, String)>,
|
||||
/// A gbuffers-style program: the matrices are the frame's, not the quad's.
|
||||
pub(super) gbuffers: bool,
|
||||
/// The pack's fragment outputs, declared again with their locations.
|
||||
pub(super) frag_outs: Vec<(u32, String)>,
|
||||
/// The pack's functions that take samplers: which parameters are samplers.
|
||||
@@ -119,6 +122,7 @@ impl<'p> Stage<'p> {
|
||||
sampler_functions: HashMap::new(),
|
||||
outputs: BTreeSet::new(),
|
||||
frag_coord: false,
|
||||
gbuffers: false,
|
||||
replaced: HashMap::new(),
|
||||
inits: Vec::new(),
|
||||
errors: Vec::new(),
|
||||
@@ -315,7 +319,7 @@ impl<'p> Stage<'p> {
|
||||
"uniform" => self.uniforms.push((name, format!("{ty}{array}"), ty_at)),
|
||||
_ if input && !fragment => {
|
||||
let value = attribute_value(&name, &ty);
|
||||
self.attributes.push((name, value));
|
||||
self.attributes.push((name, value, ty.clone()));
|
||||
self.removed.pop();
|
||||
// A private global of ours now: `in`/`attribute` and qualifiers go.
|
||||
for &w in &words[..k] {
|
||||
|
||||
Reference in New Issue
Block a user