Skip to content

Commit

Permalink
Change bloom layer to use mixin instead of enumHelper (GregTechCEu#2583)
Browse files Browse the repository at this point in the history
  • Loading branch information
MCTian-mi authored Nov 24, 2024
1 parent 0160791 commit 7509825
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package meldexun.nothirium.api.renderer.chunk;

/**
* Adapted and minimized from <a
* href="https://github.com/Meldexun/Nothirium/blob/main/src/main/java/meldexun/nothirium/api/renderer/chunk/ChunkRenderPass.java">ChunkRenderPass.java</a>
*/
public enum ChunkRenderPass {
;

public static final ChunkRenderPass[] ALL = ChunkRenderPass.values();

}
21 changes: 1 addition & 20 deletions src/main/java/gregtech/client/utils/BloomEffectUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.shader.Framebuffer;
import net.minecraft.entity.Entity;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.world.World;
import net.minecraftforge.common.util.EnumHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import com.github.bsideup.jabel.Desugar;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.GL11;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -336,24 +332,9 @@ public boolean test(BloomRenderTicket bloomRenderTicket) {
}, validityChecker);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void init() {
bloom = EnumHelper.addEnum(BlockRenderLayer.class, "BLOOM", new Class[] { String.class }, "Bloom");
bloom = BlockRenderLayer.valueOf("BLOOM");
BLOOM = bloom;
if (Mods.Nothirium.isModLoaded()) {
try {
// Nothirium hard copies the BlockRenderLayer enum into a ChunkRenderPass enum. Add our BLOOM layer to
// that too.
Class crp = Class.forName("meldexun.nothirium.api.renderer.chunk.ChunkRenderPass", false,
Launch.classLoader);
EnumHelper.addEnum(crp, "BLOOM", new Class[] {});
Field all = FieldUtils.getField(crp, "ALL", false);
FieldUtils.removeFinalModifier(all);
FieldUtils.writeStaticField(all, crp.getEnumConstants());
} catch (ClassNotFoundException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}

// Calls injected via ASM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
@SideOnly(Side.CLIENT)
public class BloomEffectVintagiumUtil {

public static BlockRenderPass bloom;

/**
* @return {@link BlockRenderPass} instance for the bloom render layer.
*/
@NotNull
@SuppressWarnings("unused")
public static BlockRenderPass getBloomPass() {
return Objects.requireNonNull(bloom, "Bloom effect is not initialized yet");
return Objects.requireNonNull(BlockRenderPass.valueOf("BLOOM"), "Bloom effect is not initialized yet");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public List<String> getMixinConfigs() {
configs.add("mixins.gregtech.ccl.json");
configs.add("mixins.gregtech.littletiles.json");
configs.add("mixins.gregtech.vintagium.json");
configs.add("mixins.gregtech.nothirium.json");

return configs;
}
Expand All @@ -31,6 +32,7 @@ public boolean shouldMixinConfigQueue(String mixinConfig) {
case "mixin.gregtech.ctm.json" -> Mods.CTM.isModLoaded();
case "mixins.gregtech.littletiles.json" -> Mods.LittleTiles.isModLoaded();
case "mixins.gregtech.vintagium.json" -> Mods.Vintagium.isModLoaded();
case "mixins.gregtech.nothirium.json" -> Mods.Nothirium.isModLoaded();
default -> true;
};
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/gregtech/mixins/minecraft/BlockRenderLayerMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gregtech.mixins.minecraft;

import net.minecraft.util.BlockRenderLayer;

import org.apache.commons.lang3.ArrayUtils;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(BlockRenderLayer.class)
public class BlockRenderLayerMixin {

@Final
@Shadow
@Mutable
private static BlockRenderLayer[] $VALUES;

@SuppressWarnings("all")
@Invoker("<init>")
private static BlockRenderLayer create(String name, int ordinal, String directoryName) {
throw new IllegalStateException("Unreachable");
}

static {
BlockRenderLayer bloom = create("BLOOM", $VALUES.length, "Bloom");

$VALUES = ArrayUtils.add($VALUES, bloom);
}
}
35 changes: 35 additions & 0 deletions src/main/java/gregtech/mixins/nothirium/ChunkRenderPassMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package gregtech.mixins.nothirium;

import meldexun.nothirium.api.renderer.chunk.ChunkRenderPass;
import org.apache.commons.lang3.ArrayUtils;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(ChunkRenderPass.class)
public class ChunkRenderPassMixin {

@Final
@Mutable
@Shadow(remap = false)
public static ChunkRenderPass[] $VALUES;

@Final
@Mutable
@Shadow(remap = false)
public static ChunkRenderPass[] ALL;

@SuppressWarnings("all")
@Invoker(value = "<init>")
private static ChunkRenderPass create(String name, int ordinal) {
throw new IllegalStateException("Unreachable");
}

static {
ChunkRenderPass bloom = create("BLOOM", $VALUES.length);
$VALUES = ArrayUtils.add($VALUES, bloom);
ALL = ChunkRenderPass.values();
}
}
31 changes: 16 additions & 15 deletions src/main/java/gregtech/mixins/vintagium/BlockRenderPassMixin.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package gregtech.mixins.vintagium;

import gregtech.client.utils.BloomEffectUtil;
import gregtech.client.utils.BloomEffectVintagiumUtil;

import net.minecraft.util.BlockRenderLayer;
import net.minecraftforge.common.util.EnumHelper;

import me.jellysquid.mods.sodium.client.render.chunk.passes.BlockRenderPass;
import me.jellysquid.mods.sodium.client.util.BufferSizeUtil;
import me.jellysquid.mods.sodium.client.util.EnumUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(BlockRenderPass.class)
public abstract class BlockRenderPassMixin {

@Final
@Mutable
@Shadow(remap = false)
public static BlockRenderPass[] $VALUES;

@Final
@Mutable
@Shadow(remap = false)
Expand All @@ -30,17 +31,17 @@ public abstract class BlockRenderPassMixin {
@Shadow(remap = false)
public static int COUNT;

@SuppressWarnings("UnresolvedMixinReference")
@Inject(method = "<clinit>",
at = @At(value = "TAIL"),
remap = false)
private static void init(CallbackInfo ci) {
EnumUtil.LAYERS = BlockRenderLayer.values();
@SuppressWarnings("all")
@Invoker("<init>")
private static BlockRenderPass create(String name, int ordinal, BlockRenderLayer layer, boolean translucent) {
throw new IllegalStateException("Unreachable");
}

static {
BufferSizeUtil.BUFFER_SIZES.put(BloomEffectUtil.getBloomLayer(), 131072);

var params = new Class[] { BlockRenderLayer.class, boolean.class };
var values = new Object[] { BloomEffectUtil.getBloomLayer(), true };
BloomEffectVintagiumUtil.bloom = EnumHelper.addEnum(BlockRenderPass.class, "BLOOM", params, values);
BlockRenderPass bloom = create("BLOOM", $VALUES.length, BloomEffectUtil.getBloomLayer(), true);
$VALUES = ArrayUtils.add($VALUES, bloom);
VALUES = BlockRenderPass.values();
COUNT = VALUES.length;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mixins.gregtech.minecraft.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"mixins": [
"BlockConcretePowderMixin",
"BlockRenderLayerMixin",
"DamageSourceMixin",
"EnchantmentCanApplyMixin",
"MinecraftMixin"
Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/mixins.gregtech.nothirium.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"package": "gregtech.mixins.nothirium",
"refmap": "mixins.gregtech.refmap.json",
"target": "@env(DEFAULT)",
"minVersion": "0.8",
"compatibilityLevel": "JAVA_8",
"mixins": [
"ChunkRenderPassMixin"
],
"client": [],
"server": []
}

0 comments on commit 7509825

Please sign in to comment.