From 0c55e883986d38e69342ff70f7708394fa05369d Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Wed, 1 Jun 2022 18:27:55 +0300 Subject: [PATCH] Moving from core shaders to Satin Managed Core Shaders (#25) --- build.gradle | 2 + gradle.properties | 1 + src/main/java/dev/kir/sync/Sync.java | 2 + .../client/render/CustomGameRenderer.java | 38 ++++++++++----- .../sync/client/render/CustomRenderLayer.java | 15 ++---- .../dev/kir/sync/mixin/GameRendererMixin.java | 47 ------------------- ..._entity_translucent_partially_textured.fsh | 0 ...entity_translucent_partially_textured.json | 4 +- ..._entity_translucent_partially_textured.vsh | 0 .../shaders/core/rendertype_voxel.fsh | 0 .../shaders/core/rendertype_voxel.json | 4 +- .../shaders/core/rendertype_voxel.vsh | 0 src/main/resources/fabric.mod.json | 1 + src/main/resources/sync.mixins.json | 1 - 14 files changed, 40 insertions(+), 75 deletions(-) delete mode 100644 src/main/java/dev/kir/sync/mixin/GameRendererMixin.java rename src/main/resources/assets/{minecraft => sync}/shaders/core/rendertype_entity_translucent_partially_textured.fsh (100%) rename src/main/resources/assets/{minecraft => sync}/shaders/core/rendertype_entity_translucent_partially_textured.json (90%) rename src/main/resources/assets/{minecraft => sync}/shaders/core/rendertype_entity_translucent_partially_textured.vsh (100%) rename src/main/resources/assets/{minecraft => sync}/shaders/core/rendertype_voxel.fsh (100%) rename src/main/resources/assets/{minecraft => sync}/shaders/core/rendertype_voxel.json (93%) rename src/main/resources/assets/{minecraft => sync}/shaders/core/rendertype_voxel.vsh (100%) diff --git a/build.gradle b/build.gradle index 05ebb14..5ae5208 100644 --- a/build.gradle +++ b/build.gradle @@ -96,6 +96,7 @@ dependencies { modImplementation(include("de.guntram.mcmod:crowdin-translate:${project.crowdin_translate_version}")) modImplementation(include("teamreborn:energy:${project.tr_energy_version}")) + modImplementation(include("io.github.ladysnake:satin:${project.satin_version}")) modImplementation(include("maven.modrinth:smart-recipes:${project.smartrecipes_version}")) modImplementation(include("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_version}"), { exclude group: "net.fabricmc.fabric-api" @@ -114,6 +115,7 @@ processResources { "minecraft": ">=${minecraftDependencyVersion}", "smart_recipes": project.smartrecipes_version, "cloth_config": project.cloth_version, + "satin_api": project.satin_version, "crowdin_translate": project.crowdin_translate_version, "team_reborn_energy": project.tr_energy_version, "modmenu": ">=${project.modmenu_version}", diff --git a/gradle.properties b/gradle.properties index 618754d..1be16fa 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,6 +15,7 @@ archives_base_name=sync # Dependencies fabric_version=0.47.8+1.18.2 tr_energy_version=2.2.0 +satin_version=1.7.2 cloth_version=6.2.57 modmenu_version=3.1.1 trinkets_version=3.3.0 diff --git a/src/main/java/dev/kir/sync/Sync.java b/src/main/java/dev/kir/sync/Sync.java index eab29be..b5e4a8f 100644 --- a/src/main/java/dev/kir/sync/Sync.java +++ b/src/main/java/dev/kir/sync/Sync.java @@ -1,6 +1,7 @@ package dev.kir.sync; import de.guntram.mcmod.crowdintranslate.CrowdinTranslate; +import dev.kir.sync.client.render.CustomGameRenderer; import dev.kir.sync.networking.SyncPackets; import dev.kir.sync.block.SyncBlocks; import dev.kir.sync.block.entity.SyncBlockEntities; @@ -41,6 +42,7 @@ public void onInitialize() { @Override @Environment(EnvType.CLIENT) public void onInitializeClient() { + CustomGameRenderer.initClient(); SyncRenderers.initClient(); SyncPackets.initClient(); diff --git a/src/main/java/dev/kir/sync/client/render/CustomGameRenderer.java b/src/main/java/dev/kir/sync/client/render/CustomGameRenderer.java index 3bf67c7..0a68d94 100644 --- a/src/main/java/dev/kir/sync/client/render/CustomGameRenderer.java +++ b/src/main/java/dev/kir/sync/client/render/CustomGameRenderer.java @@ -1,35 +1,51 @@ package dev.kir.sync.client.render; +import dev.kir.sync.Sync; +import ladysnake.satin.api.managed.ManagedCoreShader; +import ladysnake.satin.api.managed.ShaderEffectManager; +import ladysnake.satin.api.managed.uniform.Uniform1f; +import ladysnake.satin.api.managed.uniform.UniformMat4; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.MinecraftClient; import net.minecraft.client.render.BufferBuilderStorage; import net.minecraft.client.render.GameRenderer; -import net.minecraft.client.render.Shader; +import net.minecraft.client.render.VertexFormats; import net.minecraft.resource.ResourceManager; +import net.minecraft.util.math.Matrix4f; @Environment(EnvType.CLIENT) public final class CustomGameRenderer extends GameRenderer { - private static Shader renderTypeEntityTranslucentPartiallyTexturedShader = null; - private static Shader renderTypeVoxelShader = null; + private static final ManagedCoreShader RENDER_TYPE_ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER; + private static final Uniform1f RENDER_TYPE_ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER_UNIFORM_CUTOUT_Y; + private static final UniformMat4 RENDER_TYPE_ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER_UNIFORM_MODEL_MAT; + + private static final ManagedCoreShader RENDER_TYPE_VOXEL_SHADER; private CustomGameRenderer(MinecraftClient client, ResourceManager resourceManager, BufferBuilderStorage buffers) { super(client, resourceManager, buffers); } - public static void setRenderTypeEntityTranslucentPartiallyTexturedShader(Shader shader) { - renderTypeEntityTranslucentPartiallyTexturedShader = shader; + public static void initRenderTypeEntityTranslucentPartiallyTexturedShader(float cutoutY, Matrix4f modelMatrix) { + RENDER_TYPE_ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER_UNIFORM_CUTOUT_Y.set(cutoutY); + RENDER_TYPE_ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER_UNIFORM_MODEL_MAT.set(modelMatrix); } - public static Shader getRenderTypeEntityTranslucentPartiallyTexturedShader() { - return renderTypeEntityTranslucentPartiallyTexturedShader; + public static ManagedCoreShader getRenderTypeEntityTranslucentPartiallyTexturedShader() { + return RENDER_TYPE_ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER; } - public static void setRenderTypeVoxelShader(Shader shader) { - renderTypeVoxelShader = shader; + public static ManagedCoreShader getRenderTypeVoxelShader() { + return RENDER_TYPE_VOXEL_SHADER; } - public static Shader getRenderTypeVoxelShader() { - return renderTypeVoxelShader; + public static void initClient() { } + + static { + RENDER_TYPE_ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER = ShaderEffectManager.getInstance().manageCoreShader(Sync.locate("rendertype_entity_translucent_partially_textured"), VertexFormats.POSITION_COLOR_TEXTURE_OVERLAY_LIGHT_NORMAL); + RENDER_TYPE_ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER_UNIFORM_CUTOUT_Y = RENDER_TYPE_ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER.findUniform1f("CutoutY"); + RENDER_TYPE_ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER_UNIFORM_MODEL_MAT = RENDER_TYPE_ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER.findUniformMat4("ModelMat"); + + RENDER_TYPE_VOXEL_SHADER = ShaderEffectManager.getInstance().manageCoreShader(Sync.locate("rendertype_voxel"), CustomVertexFormats.POSITION_COLOR_OVERLAY_LIGHT_NORMAL); } } \ No newline at end of file diff --git a/src/main/java/dev/kir/sync/client/render/CustomRenderLayer.java b/src/main/java/dev/kir/sync/client/render/CustomRenderLayer.java index cdc7e20..30b8ad8 100644 --- a/src/main/java/dev/kir/sync/client/render/CustomRenderLayer.java +++ b/src/main/java/dev/kir/sync/client/render/CustomRenderLayer.java @@ -5,7 +5,6 @@ import net.minecraft.client.render.RenderLayer; import net.minecraft.client.render.RenderPhase; import net.minecraft.client.render.VertexFormat; -import net.minecraft.client.render.VertexFormats; import net.minecraft.util.Identifier; import net.minecraft.util.Util; @@ -13,9 +12,6 @@ @Environment(EnvType.CLIENT) public final class CustomRenderLayer extends RenderLayer { - private static final Shader ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER; - private static final Shader VOXEL_SHADER; - private static final RenderLayer VOXELS; private static final BiFunction ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED; @@ -32,17 +28,12 @@ public static RenderLayer getEntityTranslucentPartiallyTextured(Identifier textu } public static RenderLayer getEntityTranslucentPartiallyTextured(Identifier textureId, float cutoutY, boolean affectsOutline) { - net.minecraft.client.render.Shader shader = CustomGameRenderer.getRenderTypeEntityTranslucentPartiallyTexturedShader(); - shader.getUniformOrDefault("CutoutY").set(cutoutY); - shader.getUniformOrDefault("ModelMat").set(MatrixStackStorage.getModelMatrixStack().peek().getPositionMatrix()); + CustomGameRenderer.initRenderTypeEntityTranslucentPartiallyTexturedShader(cutoutY, MatrixStackStorage.getModelMatrixStack().peek().getPositionMatrix()); return ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED.apply(textureId, affectsOutline); } static { - ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER = new Shader(CustomGameRenderer::getRenderTypeEntityTranslucentPartiallyTexturedShader); - VOXEL_SHADER = new Shader(CustomGameRenderer::getRenderTypeVoxelShader); - - VOXELS = of("voxels", CustomVertexFormats.POSITION_COLOR_OVERLAY_LIGHT_NORMAL, VertexFormat.DrawMode.QUADS, 256, false, false, RenderLayer.MultiPhaseParameters.builder().shader(VOXEL_SHADER).transparency(NO_TRANSPARENCY).cull(DISABLE_CULLING).lightmap(ENABLE_LIGHTMAP).overlay(ENABLE_OVERLAY_COLOR).build(true)); - ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED = Util.memoize((id, outline) -> RenderLayer.of("entity_translucent_partially_textured", VertexFormats.POSITION_COLOR_TEXTURE_OVERLAY_LIGHT_NORMAL, VertexFormat.DrawMode.QUADS, 256, true, true, RenderLayer.MultiPhaseParameters.builder().shader(ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED_SHADER).texture(new RenderPhase.Texture(id, false, false)).transparency(RenderLayer.TRANSLUCENT_TRANSPARENCY).cull(RenderLayer.DISABLE_CULLING).lightmap(RenderLayer.ENABLE_LIGHTMAP).overlay(RenderLayer.ENABLE_OVERLAY_COLOR).build(outline))); + VOXELS = CustomGameRenderer.getRenderTypeVoxelShader().getRenderLayer(of("voxels", CustomVertexFormats.POSITION_COLOR_OVERLAY_LIGHT_NORMAL, VertexFormat.DrawMode.QUADS, 256, false, false, RenderLayer.MultiPhaseParameters.builder().shader(RenderPhase.SOLID_SHADER).transparency(NO_TRANSPARENCY).cull(DISABLE_CULLING).lightmap(ENABLE_LIGHTMAP).overlay(ENABLE_OVERLAY_COLOR).build(true))); + ENTITY_TRANSLUCENT_PARTIALLY_TEXTURED = Util.memoize((id, outline) -> CustomGameRenderer.getRenderTypeEntityTranslucentPartiallyTexturedShader().getRenderLayer(RenderLayer.getEntityTranslucent(id, outline))); } } \ No newline at end of file diff --git a/src/main/java/dev/kir/sync/mixin/GameRendererMixin.java b/src/main/java/dev/kir/sync/mixin/GameRendererMixin.java deleted file mode 100644 index ef24f68..0000000 --- a/src/main/java/dev/kir/sync/mixin/GameRendererMixin.java +++ /dev/null @@ -1,47 +0,0 @@ -package dev.kir.sync.mixin; - -import com.google.common.collect.ImmutableList; -import dev.kir.sync.client.render.CustomGameRenderer; -import dev.kir.sync.client.render.CustomVertexFormats; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.client.render.GameRenderer; -import net.minecraft.client.render.Shader; -import net.minecraft.client.render.VertexFormat; -import net.minecraft.client.render.VertexFormats; -import net.minecraft.resource.ResourceManager; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import oshi.util.tuples.Triplet; - -import java.io.IOException; -import java.util.Map; -import java.util.function.Consumer; - -@Environment(EnvType.CLIENT) -@Mixin(GameRenderer.class) -public class GameRendererMixin { - @Unique - private static final ImmutableList>> CUSTOM_SHADERS = ImmutableList.of( - new Triplet>("rendertype_entity_translucent_partially_textured", VertexFormats.POSITION_COLOR_TEXTURE_OVERLAY_LIGHT_NORMAL, CustomGameRenderer::setRenderTypeEntityTranslucentPartiallyTexturedShader), - new Triplet>("rendertype_voxel", CustomVertexFormats.POSITION_COLOR_OVERLAY_LIGHT_NORMAL, CustomGameRenderer::setRenderTypeVoxelShader) - ); - - @Final - @Shadow - private Map shaders; - - @Inject(method = "loadShaders", at = @At("TAIL")) - private void loadCustomShaders(ResourceManager manager, CallbackInfo ci) throws IOException { - for (Triplet> entry : CUSTOM_SHADERS) { - Shader shader = new Shader(manager, entry.getA(), entry.getB()); - this.shaders.put(shader.getName(), shader); - entry.getC().accept(shader); - } - } -} \ No newline at end of file diff --git a/src/main/resources/assets/minecraft/shaders/core/rendertype_entity_translucent_partially_textured.fsh b/src/main/resources/assets/sync/shaders/core/rendertype_entity_translucent_partially_textured.fsh similarity index 100% rename from src/main/resources/assets/minecraft/shaders/core/rendertype_entity_translucent_partially_textured.fsh rename to src/main/resources/assets/sync/shaders/core/rendertype_entity_translucent_partially_textured.fsh diff --git a/src/main/resources/assets/minecraft/shaders/core/rendertype_entity_translucent_partially_textured.json b/src/main/resources/assets/sync/shaders/core/rendertype_entity_translucent_partially_textured.json similarity index 90% rename from src/main/resources/assets/minecraft/shaders/core/rendertype_entity_translucent_partially_textured.json rename to src/main/resources/assets/sync/shaders/core/rendertype_entity_translucent_partially_textured.json index cb7e1fd..32856c4 100644 --- a/src/main/resources/assets/minecraft/shaders/core/rendertype_entity_translucent_partially_textured.json +++ b/src/main/resources/assets/sync/shaders/core/rendertype_entity_translucent_partially_textured.json @@ -4,8 +4,8 @@ "srcrgb": "srcalpha", "dstrgb": "1-srcalpha" }, - "vertex": "rendertype_entity_translucent_partially_textured", - "fragment": "rendertype_entity_translucent_partially_textured", + "vertex": "sync:rendertype_entity_translucent_partially_textured", + "fragment": "sync:rendertype_entity_translucent_partially_textured", "attributes": [ "Position", "Color", diff --git a/src/main/resources/assets/minecraft/shaders/core/rendertype_entity_translucent_partially_textured.vsh b/src/main/resources/assets/sync/shaders/core/rendertype_entity_translucent_partially_textured.vsh similarity index 100% rename from src/main/resources/assets/minecraft/shaders/core/rendertype_entity_translucent_partially_textured.vsh rename to src/main/resources/assets/sync/shaders/core/rendertype_entity_translucent_partially_textured.vsh diff --git a/src/main/resources/assets/minecraft/shaders/core/rendertype_voxel.fsh b/src/main/resources/assets/sync/shaders/core/rendertype_voxel.fsh similarity index 100% rename from src/main/resources/assets/minecraft/shaders/core/rendertype_voxel.fsh rename to src/main/resources/assets/sync/shaders/core/rendertype_voxel.fsh diff --git a/src/main/resources/assets/minecraft/shaders/core/rendertype_voxel.json b/src/main/resources/assets/sync/shaders/core/rendertype_voxel.json similarity index 93% rename from src/main/resources/assets/minecraft/shaders/core/rendertype_voxel.json rename to src/main/resources/assets/sync/shaders/core/rendertype_voxel.json index 440d63c..b1fb064 100644 --- a/src/main/resources/assets/minecraft/shaders/core/rendertype_voxel.json +++ b/src/main/resources/assets/sync/shaders/core/rendertype_voxel.json @@ -4,8 +4,8 @@ "srcrgb": "srcalpha", "dstrgb": "1-srcalpha" }, - "vertex": "rendertype_voxel", - "fragment": "rendertype_voxel", + "vertex": "sync:rendertype_voxel", + "fragment": "sync:rendertype_voxel", "attributes": [ "Position", "Color", diff --git a/src/main/resources/assets/minecraft/shaders/core/rendertype_voxel.vsh b/src/main/resources/assets/sync/shaders/core/rendertype_voxel.vsh similarity index 100% rename from src/main/resources/assets/minecraft/shaders/core/rendertype_voxel.vsh rename to src/main/resources/assets/sync/shaders/core/rendertype_voxel.vsh diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index d578045..79e541e 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -50,6 +50,7 @@ "includes": { "smart-recipes": "${smart_recipes}", "cloth-config": "${cloth_config}", + "satin-api": "${satin_api}", "crowdin-translate": "${crowdin_translate}", "team_reborn_energy": "${team_reborn_energy}" }, diff --git a/src/main/resources/sync.mixins.json b/src/main/resources/sync.mixins.json index 6f2c9ae..522eabe 100644 --- a/src/main/resources/sync.mixins.json +++ b/src/main/resources/sync.mixins.json @@ -13,7 +13,6 @@ "ClientPlayerEntityMixin", "ClientPlayerInteractionManagerMixin", "EntityRenderDispatcherMixin", - "GameRendererMixin", "MinecraftClientMixin", "PlayerEntityModelMixin", "WorldRendererMixin"