From 0b3e23456f38bac359112d82d6aad1b5430c04d1 Mon Sep 17 00:00:00 2001 From: rbasamoyai Date: Wed, 31 Jul 2024 16:40:50 -0400 Subject: [PATCH] improvement, bug: Registered custom shaders properly so that Create Big Cannons shaders don't cause logspam on crash --- changelog.txt | 1 + .../index/CBCRenderTypes.java | 23 +++++++++++--- .../mixin/client/GameRendererMixin.java | 31 ------------------- .../shaders/core/cannon_smoke_particle.fsh} | 0 .../shaders/core/cannon_smoke_particle.json} | 4 +-- .../shaders/core/cannon_smoke_particle.vsh} | 0 .../shaders/core/splinter_particle.fsh} | 0 .../shaders/core/splinter_particle.json} | 4 +-- .../shaders/core/splinter_particle.vsh} | 0 .../createbigcannons-common.mixins.json | 1 - .../fabric/CBCClientFabric.java | 14 +++++++++ .../forge/CBCClientForge.java | 14 +++++++++ 12 files changed, 52 insertions(+), 40 deletions(-) delete mode 100644 common/src/main/java/rbasamoyai/createbigcannons/mixin/client/GameRendererMixin.java rename common/src/main/resources/assets/{minecraft/shaders/core/createbigcannons_cannon_smoke_particle.fsh => createbigcannons/shaders/core/cannon_smoke_particle.fsh} (100%) rename common/src/main/resources/assets/{minecraft/shaders/core/createbigcannons_cannon_smoke_particle.json => createbigcannons/shaders/core/cannon_smoke_particle.json} (90%) rename common/src/main/resources/assets/{minecraft/shaders/core/createbigcannons_cannon_smoke_particle.vsh => createbigcannons/shaders/core/cannon_smoke_particle.vsh} (100%) rename common/src/main/resources/assets/{minecraft/shaders/core/createbigcannons_splinter_particle.fsh => createbigcannons/shaders/core/splinter_particle.fsh} (100%) rename common/src/main/resources/assets/{minecraft/shaders/core/createbigcannons_splinter_particle.json => createbigcannons/shaders/core/splinter_particle.json} (92%) rename common/src/main/resources/assets/{minecraft/shaders/core/createbigcannons_splinter_particle.vsh => createbigcannons/shaders/core/splinter_particle.vsh} (100%) diff --git a/changelog.txt b/changelog.txt index b865e3911..8d530d9a2 100644 --- a/changelog.txt +++ b/changelog.txt @@ -12,6 +12,7 @@ Changed: - Changed impact explosion queueing to better allow compatibility with Valkyrien Skies - All Autocannon Recoil Springs on an autocannon now play the firing animation - Format for storing Autocannon Recoil Spring positions has changed, reassemble autocannons to fix any issues +- Registered custom shaders properly so that Create Big Cannons shaders don't cause logspam on crash Fixed: - Fixed Trinkets compat crashing - Fixed gas mask overlay rendering as solid color when holding Just Enough Guns firearm diff --git a/common/src/main/java/rbasamoyai/createbigcannons/index/CBCRenderTypes.java b/common/src/main/java/rbasamoyai/createbigcannons/index/CBCRenderTypes.java index 54a586e14..bf7889cb8 100644 --- a/common/src/main/java/rbasamoyai/createbigcannons/index/CBCRenderTypes.java +++ b/common/src/main/java/rbasamoyai/createbigcannons/index/CBCRenderTypes.java @@ -1,6 +1,9 @@ package rbasamoyai.createbigcannons.index; +import java.io.IOException; import java.util.Locale; +import java.util.function.BiConsumer; +import java.util.function.Consumer; import java.util.function.Function; import javax.annotation.Nullable; @@ -11,14 +14,16 @@ import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.ShaderInstance; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.packs.resources.ResourceManager; import rbasamoyai.createbigcannons.CreateBigCannons; public enum CBCRenderTypes { CANNON_SMOKE_PARTICLE(CBCRenderingParts.CANNON_SMOKE_PARTICLE_INPUT, VertexFormat.Mode.QUADS, CBCRenderingParts.CANNON_SMOKE_PARTICLE_STATE), - SPLINTER_PARTICLE(type -> RenderType.create(type.id, CBCRenderingParts.SPLINTER_PARTICLE_INPUT, VertexFormat.Mode.QUADS, + SPLINTER_PARTICLE(type -> RenderType.create(type.id.toString(), CBCRenderingParts.SPLINTER_PARTICLE_INPUT, VertexFormat.Mode.QUADS, 512, false, false, CBCRenderingParts.SPLINTER_PARTICLE_STATE.apply(type))); - private final String id = CreateBigCannons.MOD_ID + "_" + this.name().toLowerCase(Locale.ROOT); + private final ResourceLocation id = CreateBigCannons.resource(this.name().toLowerCase(Locale.ROOT)); private final RenderType renderType; @Nullable private ShaderInstance shaderInstance; @@ -27,10 +32,10 @@ public enum CBCRenderTypes { } CBCRenderTypes(VertexFormat format, VertexFormat.Mode mode, Function state) { - this(type -> RenderType.create(type.id, format, mode, 256, false, false, state.apply(type))); + this(type -> RenderType.create(type.id.toString(), format, mode, 256, false, false, state.apply(type))); } - public String id() { return this.id; } + public ResourceLocation id() { return this.id; } public RenderType renderType() { return this.renderType; } @Nullable public ShaderInstance getShaderInstance() { return this.shaderInstance; } public void setShaderInstance(ShaderInstance shaderInstance) { this.shaderInstance = shaderInstance; } @@ -40,4 +45,14 @@ public void setRenderTypeForBuilder(BufferBuilder builder) { builder.begin(this.renderType.mode(), this.renderType.format()); } + public static void registerAllShaders(BiConsumer> cons, + CreateShaderInstance shaderCreator, ResourceManager resourceManager) throws IOException { + for (CBCRenderTypes renderType : values()) + cons.accept(shaderCreator.create(resourceManager, renderType.id, renderType.renderType().format()), renderType::setShaderInstance); + } + + public interface CreateShaderInstance { + ShaderInstance create(ResourceManager manager, ResourceLocation location, VertexFormat format) throws IOException; + } + } diff --git a/common/src/main/java/rbasamoyai/createbigcannons/mixin/client/GameRendererMixin.java b/common/src/main/java/rbasamoyai/createbigcannons/mixin/client/GameRendererMixin.java deleted file mode 100644 index b1e99c136..000000000 --- a/common/src/main/java/rbasamoyai/createbigcannons/mixin/client/GameRendererMixin.java +++ /dev/null @@ -1,31 +0,0 @@ -package rbasamoyai.createbigcannons.mixin.client; - -import java.io.IOException; -import java.util.List; -import java.util.function.Consumer; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import com.llamalad7.mixinextras.sugar.Local; -import com.mojang.datafixers.util.Pair; - -import net.minecraft.client.renderer.GameRenderer; -import net.minecraft.client.renderer.ShaderInstance; -import net.minecraft.server.packs.resources.ResourceManager; -import rbasamoyai.createbigcannons.index.CBCRenderTypes; - -@Mixin(GameRenderer.class) -public class GameRendererMixin { - - @Inject(method = "reloadShaders", at = @At(value = "INVOKE", target = "Ljava/util/List;add(Ljava/lang/Object;)Z", ordinal = 53)) - private void reloadShaders(ResourceManager resourceManager, CallbackInfo ci, - @Local(ordinal = 1) List>> shaderInstances) throws IOException { - for (CBCRenderTypes renderType : CBCRenderTypes.values()) - shaderInstances.add(Pair.of(new ShaderInstance(resourceManager, renderType.id(), renderType.renderType().format()), - renderType::setShaderInstance)); - } - -} diff --git a/common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_cannon_smoke_particle.fsh b/common/src/main/resources/assets/createbigcannons/shaders/core/cannon_smoke_particle.fsh similarity index 100% rename from common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_cannon_smoke_particle.fsh rename to common/src/main/resources/assets/createbigcannons/shaders/core/cannon_smoke_particle.fsh diff --git a/common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_cannon_smoke_particle.json b/common/src/main/resources/assets/createbigcannons/shaders/core/cannon_smoke_particle.json similarity index 90% rename from common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_cannon_smoke_particle.json rename to common/src/main/resources/assets/createbigcannons/shaders/core/cannon_smoke_particle.json index 31c9888c1..4cd5eefd1 100644 --- a/common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_cannon_smoke_particle.json +++ b/common/src/main/resources/assets/createbigcannons/shaders/core/cannon_smoke_particle.json @@ -4,8 +4,8 @@ "srcrgb": "srcalpha", "dstrgb": "1-srcalpha" }, - "vertex": "createbigcannons_cannon_smoke_particle", - "fragment": "createbigcannons_cannon_smoke_particle", + "vertex": "createbigcannons:cannon_smoke_particle", + "fragment": "createbigcannons:cannon_smoke_particle", "attributes": [ "Position", "UV0", diff --git a/common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_cannon_smoke_particle.vsh b/common/src/main/resources/assets/createbigcannons/shaders/core/cannon_smoke_particle.vsh similarity index 100% rename from common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_cannon_smoke_particle.vsh rename to common/src/main/resources/assets/createbigcannons/shaders/core/cannon_smoke_particle.vsh diff --git a/common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_splinter_particle.fsh b/common/src/main/resources/assets/createbigcannons/shaders/core/splinter_particle.fsh similarity index 100% rename from common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_splinter_particle.fsh rename to common/src/main/resources/assets/createbigcannons/shaders/core/splinter_particle.fsh diff --git a/common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_splinter_particle.json b/common/src/main/resources/assets/createbigcannons/shaders/core/splinter_particle.json similarity index 92% rename from common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_splinter_particle.json rename to common/src/main/resources/assets/createbigcannons/shaders/core/splinter_particle.json index 0623e988d..187bef6e9 100644 --- a/common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_splinter_particle.json +++ b/common/src/main/resources/assets/createbigcannons/shaders/core/splinter_particle.json @@ -4,8 +4,8 @@ "srcrgb": "srcalpha", "dstrgb": "1-srcalpha" }, - "vertex": "createbigcannons_splinter_particle", - "fragment": "createbigcannons_splinter_particle", + "vertex": "createbigcannons:splinter_particle", + "fragment": "createbigcannons:splinter_particle", "attributes": [ "Position", "UV0", diff --git a/common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_splinter_particle.vsh b/common/src/main/resources/assets/createbigcannons/shaders/core/splinter_particle.vsh similarity index 100% rename from common/src/main/resources/assets/minecraft/shaders/core/createbigcannons_splinter_particle.vsh rename to common/src/main/resources/assets/createbigcannons/shaders/core/splinter_particle.vsh diff --git a/common/src/main/resources/createbigcannons-common.mixins.json b/common/src/main/resources/createbigcannons-common.mixins.json index cf6074997..255fadd27 100644 --- a/common/src/main/resources/createbigcannons-common.mixins.json +++ b/common/src/main/resources/createbigcannons-common.mixins.json @@ -11,7 +11,6 @@ "client.ClientLevelAccessor", "client.EntityClientMixin", "client.EntityRenderDispatcherMixin", - "client.GameRendererMixin", "client.HumanoidPoseMixin", "client.LevelRendererMixin", "client.LightingMixin", diff --git a/fabric/src/main/java/rbasamoyai/createbigcannons/fabric/CBCClientFabric.java b/fabric/src/main/java/rbasamoyai/createbigcannons/fabric/CBCClientFabric.java index e238983df..a958c1103 100644 --- a/fabric/src/main/java/rbasamoyai/createbigcannons/fabric/CBCClientFabric.java +++ b/fabric/src/main/java/rbasamoyai/createbigcannons/fabric/CBCClientFabric.java @@ -1,11 +1,13 @@ package rbasamoyai.createbigcannons.fabric; +import java.io.IOException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; import java.util.function.Consumer; import com.mojang.blaze3d.platform.Window; import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexFormat; import io.github.fabricators_of_create.porting_lib.event.client.CameraSetupCallback; import io.github.fabricators_of_create.porting_lib.event.client.ClientWorldEvents; @@ -15,6 +17,7 @@ import io.github.fabricators_of_create.porting_lib.event.client.MouseButtonCallback; import io.github.fabricators_of_create.porting_lib.event.client.MouseScrolledCallback; import io.github.fabricators_of_create.porting_lib.event.client.ParticleManagerRegistrationCallback; +import io.github.fabricators_of_create.porting_lib.event.client.RegisterShadersCallback; import io.github.fabricators_of_create.porting_lib.event.client.TextureStitchCallback; import io.github.fabricators_of_create.porting_lib.event.common.ModsLoadedCallback; import net.fabricmc.api.ClientModInitializer; @@ -36,6 +39,7 @@ import net.minecraft.client.multiplayer.ClientPacketListener; import net.minecraft.client.player.AbstractClientPlayer; import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.client.renderer.ShaderInstance; import net.minecraft.client.renderer.entity.LivingEntityRenderer; import net.minecraft.client.renderer.entity.player.PlayerRenderer; import net.minecraft.client.renderer.texture.TextureAtlas; @@ -51,6 +55,7 @@ import rbasamoyai.createbigcannons.compat.trinkets.CBCTrinketsClient; import rbasamoyai.createbigcannons.fabric.mixin.client.KeyMappingAccessor; import rbasamoyai.createbigcannons.fabric.network.CBCNetworkFabric; +import rbasamoyai.createbigcannons.index.CBCRenderTypes; public class CBCClientFabric implements ClientModInitializer { @Override @@ -77,6 +82,7 @@ public void onInitializeClient() { ClientWorldEvents.LOAD.register(CBCClientFabric::onLoadClientLevel); ClientLoginConnectionEvents.INIT.register(CBCClientFabric::onPlayerLogIn); ModsLoadedCallback.EVENT.register(CBCClientFabric::onModsLoaded); + RegisterShadersCallback.EVENT.register(CBCClientFabric::onShaderReload); } private static void wrapOverlay(String id, CBCClientCommon.CBCGuiOverlay overlay) { @@ -203,4 +209,12 @@ public CompletableFuture reload(PreparationBarrier preparationBarrier, Res ResourceManagerHelper.get(PackType.CLIENT_RESOURCES).registerReloadListener(listener); } + public static void onShaderReload(ResourceManager resourceManager, RegisterShadersCallback.ShaderRegistry registry) throws IOException { + CBCRenderTypes.registerAllShaders(registry::registerShader, CBCClientFabric::createShader, resourceManager); + } + + private static ShaderInstance createShader(ResourceManager manager, ResourceLocation loc, VertexFormat format) throws IOException { + return new ShaderInstance(manager, loc.toString(), format); + } + } diff --git a/forge/src/main/java/rbasamoyai/createbigcannons/forge/CBCClientForge.java b/forge/src/main/java/rbasamoyai/createbigcannons/forge/CBCClientForge.java index caa43436e..e00eed976 100644 --- a/forge/src/main/java/rbasamoyai/createbigcannons/forge/CBCClientForge.java +++ b/forge/src/main/java/rbasamoyai/createbigcannons/forge/CBCClientForge.java @@ -1,7 +1,10 @@ package rbasamoyai.createbigcannons.forge; +import java.io.IOException; + import net.minecraft.client.Minecraft; import net.minecraft.client.player.AbstractClientPlayer; +import net.minecraft.client.renderer.ShaderInstance; import net.minecraft.world.level.LevelAccessor; import net.minecraftforge.client.ClientRegistry; import net.minecraftforge.client.ConfigGuiHandler.ConfigGuiFactory; @@ -11,6 +14,7 @@ import net.minecraftforge.client.event.InputEvent; import net.minecraftforge.client.event.ParticleFactoryRegisterEvent; import net.minecraftforge.client.event.RegisterClientReloadListenersEvent; +import net.minecraftforge.client.event.RegisterShadersEvent; import net.minecraftforge.client.event.RenderPlayerEvent; import net.minecraftforge.client.event.TextureStitchEvent; import net.minecraftforge.client.gui.ForgeIngameGui; @@ -29,6 +33,7 @@ import rbasamoyai.createbigcannons.compat.curios.CBCCuriosRenderers; import rbasamoyai.createbigcannons.config.CBCConfigs; import rbasamoyai.createbigcannons.index.CBCBlockPartials; +import rbasamoyai.createbigcannons.index.CBCRenderTypes; public class CBCClientForge { @@ -43,6 +48,7 @@ public static void prepareClient(IEventBus modEventBus, IEventBus forgeEventBus) modEventBus.addListener(CBCClientForge::onTextureStitchAtlasPre); modEventBus.addListener(CBCClientForge::onLoadComplete); modEventBus.addListener(CBCClientForge::onRegisterClientReloadListeners); + modEventBus.addListener(CBCClientForge::onRegisterShaders); forgeEventBus.addListener(CBCClientForge::getFogColor); forgeEventBus.addListener(CBCClientForge::getFogDensity); @@ -160,4 +166,12 @@ public static void onRegisterClientReloadListeners(RegisterClientReloadListeners CBCClientCommon.registerClientReloadListeners((listener, id) -> evt.registerReloadListener(listener)); } + public static void onRegisterShaders(RegisterShadersEvent evt) { + try { + CBCRenderTypes.registerAllShaders(evt::registerShader, ShaderInstance::new, evt.getResourceManager()); + } catch (IOException e) { + throw new RuntimeException("Failed to reload Create Big Cannons shaders: ", e); + } + } + }