merge-stonecutter #16

Merged
embeddedt merged 9 commits from mitchej123/celeritas:merge-stonecutter into shader-refactor 2025-07-07 21:28:48 -04:00
Showing only changes of commit 3e3edc0a59 - Show all commits
@@ -32,158 +32,10 @@ import net.irisshaders.iris.uniforms.custom.CustomUniforms;
import java.util.*; import java.util.*;
import java.util.function.IntFunction; import java.util.function.IntFunction;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.embeddedt.embeddium.compat.mc.MinecraftVersionShimService.MINECRAFT_SHIM; import static org.embeddedt.embeddium.compat.mc.MinecraftVersionShimService.MINECRAFT_SHIM;
public class SodiumTerrainPipeline { public class SodiumTerrainPipeline {
private static final String defaultVertex = """
#version 330 core
in ivec2 a_LightCoord;
in vec4 a_Color;
in vec2 a_TexCoord;
in uvec4 a_PosId;
uniform mat4 iris_ProjectionMatrix;
uniform int fogShape;
uniform mat4 iris_ModelViewMatrix;
uniform vec3 u_RegionOffset;
vec3 _vert_position;
vec2 _vert_tex_diffuse_coord;
ivec2 _vert_tex_light_coord;
vec4 _vert_color;
uint _draw_id;
uint _material_params;
out float v_FragDistance;
const int FOG_SHAPE_SPHERICAL = 0;
const int FOG_SHAPE_CYLINDRICAL = 1;
vec4 _linearFog(vec4 fragColor, float fragDistance, vec4 fogColor, float fogStart, float fogEnd) {
float factor = smoothstep(fogStart, fogEnd, fragDistance * fogColor.a); // alpha value of fog is used as a weight
vec3 blended = mix(fragColor.rgb, fogColor.rgb, factor);
return vec4(blended, fragColor.a); // alpha value of fragment cannot be modified
}
float getFragDistance(int fogShape, vec3 position) {
// Use the maximum of the horizontal and vertical distance to get cylindrical fog if fog shape is cylindrical
switch (fogShape) {
case FOG_SHAPE_SPHERICAL: return length(position);
case FOG_SHAPE_CYLINDRICAL: return max(length(position.xz), abs(position.y));
default: return length(position); // This shouldn't be possible to get, but return a sane value just in case
}
}
out vec4 v_ColorModulator;
out vec2 v_TexCoord;
out float v_MaterialMipBias;
out float v_MaterialAlphaCutoff;
const uint MATERIAL_USE_MIP_OFFSET = 0u;
const uint MATERIAL_ALPHA_CUTOFF_OFFSET = 1u;
float _material_mip_bias(uint material) {
return ((material >> MATERIAL_USE_MIP_OFFSET) & 1u) != 0u ? 0.0f : -4.0f;
}
const float[4] ALPHA_CUTOFF = float[4](0.0, 0.1, 0.5, 1.0);
float _material_alpha_cutoff(uint material) {
return ALPHA_CUTOFF[(material >> MATERIAL_ALPHA_CUTOFF_OFFSET) & 3u];
}
void _vert_init() {
_vert_position = (vec3(a_PosId.xyz) * 4.8828125E-4f + -8.0f);
""" +
"_vert_tex_diffuse_coord = (a_TexCoord * " + (1.0f / 32768.0f) + ");" +
"""
_vert_tex_light_coord = a_LightCoord;
_vert_color = a_Color;
_draw_id = (a_PosId.w >> 8u) & 0xffu;
_material_params = (a_PosId.w >> 0u) & 0xFFu;
}
uvec3 _get_relative_chunk_coord(uint pos) {
return uvec3(pos) >> uvec3(5u, 0u, 2u) & uvec3(7u, 3u, 7u);
}
vec3 _get_draw_translation(uint pos) {
return _get_relative_chunk_coord(pos) * vec3(16.0f);
}
vec4 getVertexPosition() {
return vec4(_vert_position + u_RegionOffset + _get_draw_translation(_draw_id), 1.0f);
}
uniform sampler2D lightmap; // The light map texture
vec3 _sample_lightmap(ivec2 uv) {
return texture(lightmap, clamp(uv / 256.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0))).rgb;
}
void main() {
_vert_init();
// Transform the chunk-local vertex position into world model space
vec3 translation = u_RegionOffset + _get_draw_translation(_draw_id);
vec3 position = _vert_position + translation;
v_FragDistance = getFragDistance(fogShape, position);
// Transform the vertex position into model-view-projection space
gl_Position = iris_ProjectionMatrix * iris_ModelViewMatrix * vec4(position, 1.0);
v_ColorModulator = vec4((_vert_color.rgb * _vert_color.a), 1) * vec4(_sample_lightmap(_vert_tex_light_coord), 1.0);
v_TexCoord = _vert_tex_diffuse_coord;
v_MaterialMipBias = _material_mip_bias(_material_params);
v_MaterialAlphaCutoff = _material_alpha_cutoff(_material_params);
}
""";
private static final String defaultFragment = """
#version 330 core
const int FOG_SHAPE_SPHERICAL = 0;
const int FOG_SHAPE_CYLINDRICAL = 1;
vec4 _linearFog(vec4 fragColor, float fragDistance, vec4 fogColor, float fogStart, float fogEnd) {
float factor = smoothstep(fogStart, fogEnd, fragDistance * fogColor.a); // alpha value of fog is used as a weight
vec3 blended = mix(fragColor.rgb, fogColor.rgb, factor);
return vec4(blended, fragColor.a); // alpha value of fragment cannot be modified
}
in vec4 v_ColorModulator; // The interpolated vertex color
in vec2 v_TexCoord; // The interpolated block texture coordinates
in float v_FragDistance; // The fragment's distance from the camera
in float v_MaterialMipBias;
in float v_MaterialAlphaCutoff;
uniform sampler2D gtexture; // The block atlas texture
uniform vec4 iris_FogColor; // The color of the shader fog
uniform float iris_FogStart; // The starting position of the shader fog
uniform float iris_FogEnd; // The ending position of the shader fog
out vec4 out_FragColor; // The output fragment for the color framebuffer
void main() {
vec4 diffuseColor = texture(gtexture, v_TexCoord, v_MaterialMipBias);
if (diffuseColor.a < 0.1) {
discard;
}
// Modulate the color (used by ambient occlusion and per-vertex colouring)
diffuseColor.rgb *= v_ColorModulator.rgb;
out_FragColor = _linearFog(diffuseColor, v_FragDistance, iris_FogColor, iris_FogStart, iris_FogEnd);
}
""";
private final WorldRenderingPipeline parent; private final WorldRenderingPipeline parent;
private final CustomUniforms customUniforms; private final CustomUniforms customUniforms;
private final IntFunction<ProgramSamplers> createTerrainSamplers; private final IntFunction<ProgramSamplers> createTerrainSamplers;
@@ -265,22 +117,7 @@ public class SodiumTerrainPipeline {
return Optional.empty(); return Optional.empty();
} }
public static String parseSodiumImport(String shader) { public void patchShaders(RenderPassConfiguration<?> configuration) {
Pattern IMPORT_PATTERN = Pattern.compile("#import <(?<namespace>.*):(?<path>.*)>");
Matcher matcher = IMPORT_PATTERN.matcher(shader);
if (!matcher.matches()) {
throw new IllegalArgumentException("Malformed import statement (expected format: " + IMPORT_PATTERN + ")");
}
String namespace = matcher.group("namespace");
String path = matcher.group("path");
MCResourceLocation identifier = MINECRAFT_SHIM.makeResourceLocation(namespace, path);
return "";
}
public void patchShaders(RenderPassConfiguration configuration) {
ShaderAttributeInputs inputs = new ShaderAttributeInputs(true, true, false, true, true); ShaderAttributeInputs inputs = new ShaderAttributeInputs(true, true, false, true, true);
for (var pass : IrisTerrainPass.values()) { for (var pass : IrisTerrainPass.values()) {
@@ -331,8 +168,7 @@ public class SodiumTerrainPipeline {
passInfo.blendModeOverride = null; passInfo.blendModeOverride = null;
passInfo.bufferOverrides = Collections.emptyList(); passInfo.bufferOverrides = Collections.emptyList();
if (pass != IrisTerrainPass.SHADOW && pass != IrisTerrainPass.SHADOW_CUTOUT) { if (pass != IrisTerrainPass.SHADOW && pass != IrisTerrainPass.SHADOW_CUTOUT) {
passInfo.sources.put(ShaderType.VERTEX, Optional.of(defaultVertex)); throw new UnsupportedOperationException("Fallback vertex/fragment shaders are no longer implemented, but are needed by pass " + pass.name());
passInfo.sources.put(ShaderType.FRAGMENT, Optional.of(defaultFragment));
} }
}); });
} }