Skip to content

Commit

Permalink
Split leftover platform specific code out of common, fix remaining wa…
Browse files Browse the repository at this point in the history
…rnings
  • Loading branch information
mrmangohands committed Aug 17, 2024
1 parent 83111dd commit 6bb29b5
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import net.caffeinemc.mods.sodium.client.render.SodiumWorldRenderer;
import net.caffeinemc.mods.sodium.client.render.chunk.ChunkRenderMatrices;
import net.caffeinemc.mods.sodium.client.render.viewport.ViewportProvider;
import net.caffeinemc.mods.sodium.client.services.PlatformLevelAccess;
import net.caffeinemc.mods.sodium.client.services.PlatformLevelRenderHooks;
import net.caffeinemc.mods.sodium.client.util.FlawlessFrames;
import net.caffeinemc.mods.sodium.client.world.LevelRendererExtension;
Expand All @@ -25,8 +24,6 @@
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.BlockDestructionProgress;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -35,7 +32,6 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.SortedSet;
import java.util.function.Consumer;

@Mixin(LevelRenderer.class)
public abstract class LevelRendererMixin implements LevelRendererExtension {
Expand All @@ -47,10 +43,6 @@ public abstract class LevelRendererMixin implements LevelRendererExtension {
@Final
private Long2ObjectMap<SortedSet<BlockDestructionProgress>> destructionProgress;

@Shadow
@Nullable
private ClientLevel level;

@Shadow
private int ticks;

Expand Down Expand Up @@ -211,15 +203,6 @@ private void onRenderBlockEntities(DeltaTracker deltaTracker, boolean bl, Camera
this.renderer.renderBlockEntities(new PoseStack(), this.renderBuffers, this.destructionProgress, camera, deltaTracker.getGameTimeDeltaPartialTick(false));
}

// Exclusive to NeoForge, allow to fail.
@SuppressWarnings("all")
@Inject(method = "iterateVisibleBlockEntities", at = @At("HEAD"), cancellable = true, require = 0)
public void replaceBlockEntityIteration(Consumer<BlockEntity> blockEntityConsumer, CallbackInfo ci) {
ci.cancel();

this.renderer.iterateVisibleBlockEntities(blockEntityConsumer);
}

/**
* @reason Replace the debug string
* @author JellySquid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class ItemBlockRenderTypesMixin {
// getRenderLayers is a NeoForge only function required for the leaves to properly work.
@SuppressWarnings("all")
@Redirect(
method = { "getChunkRenderType", "getMovingBlockRenderType", "getRenderLayers" },
at = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/ItemBlockRenderTypes;renderCutout:Z"), require = 2)
method = { "getChunkRenderType", "getMovingBlockRenderType" },
at = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/ItemBlockRenderTypes;renderCutout:Z"))
private static boolean redirectLeavesShouldBeFancy() {
return leavesFancy;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package net.caffeinemc.mods.sodium.mixin.workarounds.context_creation;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mojang.blaze3d.platform.DisplayData;
import com.mojang.blaze3d.platform.ScreenManager;
import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.platform.WindowEventHandler;
import net.caffeinemc.mods.sodium.client.compatibility.checks.PostLaunchChecks;
import net.caffeinemc.mods.sodium.client.compatibility.checks.ModuleScanner;
import net.caffeinemc.mods.sodium.client.compatibility.environment.GLContextInfo;
import net.caffeinemc.mods.sodium.client.compatibility.workarounds.Workarounds;
import net.caffeinemc.mods.sodium.client.compatibility.workarounds.nvidia.NvidiaWorkarounds;
import net.caffeinemc.mods.sodium.client.services.PlatformRuntimeInformation;
import net.minecraft.Util;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.WGL;
import org.lwjgl.system.MemoryUtil;
import org.slf4j.Logger;
Expand All @@ -23,14 +17,8 @@
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.function.IntSupplier;
import java.util.function.LongSupplier;
import java.util.function.Supplier;


@Mixin(Window.class)
public class WindowMixin {
@Shadow
Expand All @@ -40,42 +28,7 @@ public class WindowMixin {
@Unique
private long wglPrevContext = MemoryUtil.NULL;

@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Lorg/lwjgl/glfw/GLFW;glfwCreateWindow(IILjava/lang/CharSequence;JJ)J"), expect = 0, require = 0)
private long wrapGlfwCreateWindow(int width, int height, CharSequence title, long monitor, long share) {
final boolean applyNvidiaWorkarounds = Workarounds.isWorkaroundEnabled(Workarounds.Reference.NVIDIA_THREADED_OPTIMIZATIONS);

if (applyNvidiaWorkarounds) {
NvidiaWorkarounds.install();
}

try {
return GLFW.glfwCreateWindow(width, height, title, monitor, share);
} finally {
if (applyNvidiaWorkarounds) {
NvidiaWorkarounds.uninstall();
}
}
}

@SuppressWarnings("all")
@WrapOperation(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/neoforged/fml/loading/ImmediateWindowHandler;setupMinecraftWindow(Ljava/util/function/IntSupplier;Ljava/util/function/IntSupplier;Ljava/util/function/Supplier;Ljava/util/function/LongSupplier;)J"), expect = 0, require = 0)
private long wrapGlfwCreateWindowForge(final IntSupplier width, final IntSupplier height, final Supplier<String> title, final LongSupplier monitor, Operation<Long> op) {
final boolean applyNvidiaWorkarounds = Workarounds.isWorkaroundEnabled(Workarounds.Reference.NVIDIA_THREADED_OPTIMIZATIONS);

if (applyNvidiaWorkarounds && !PlatformRuntimeInformation.getInstance().platformHasEarlyLoadingScreen()) {
NvidiaWorkarounds.install();
}

try {
return op.call(width, height, title, monitor);
} finally {
if (applyNvidiaWorkarounds) {
NvidiaWorkarounds.uninstall();
}
}
}

@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL;createCapabilities()Lorg/lwjgl/opengl/GLCapabilities;", shift = At.Shift.AFTER))
@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lorg/lwjgl/opengl/GL;createCapabilities()Lorg/lwjgl/opengl/GLCapabilities;", shift = At.Shift.AFTER, remap = false))
private void postContextReady(WindowEventHandler eventHandler, ScreenManager monitorTracker, DisplayData settings, String videoMode, String title, CallbackInfo ci) {
GLContextInfo driver = GLContextInfo.create();

Expand All @@ -98,7 +51,7 @@ private void postContextReady(WindowEventHandler eventHandler, ScreenManager mon
ModuleScanner.checkModules();
}

@Inject(method = "updateDisplay", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/systems/RenderSystem;flipFrame(J)V", shift = At.Shift.AFTER))
@Inject(method = "updateDisplay", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/systems/RenderSystem;flipFrame(J)V", shift = At.Shift.AFTER, remap = false))
private void preSwapBuffers(CallbackInfo ci) {
if (this.wglPrevContext == MemoryUtil.NULL) {
// There is no prior recorded context.
Expand Down
2 changes: 0 additions & 2 deletions common/src/main/resources/sodium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
},
"client": [
"core.MinecraftMixin",
"core.WindowMixin",
"core.gui.LevelLoadStatusManagerMixin",
"core.model.colors.BlockColorsMixin",
"core.model.colors.ItemColorsMixin",
Expand All @@ -35,7 +34,6 @@
"core.world.map.ClientLevelMixin",
"features.gui.hooks.console.GameRendererMixin",
"features.gui.hooks.debug.DebugScreenOverlayMixin",
"features.gui.hooks.settings.OptionsScreenMixin",
"features.gui.screen.LevelLoadingScreenMixin",
"features.options.overlays.GuiMixin",
"features.options.render_layers.LeavesBlockMixin",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package net.caffeinemc.mods.sodium.mixin.core;
package net.caffeinemc.mods.sodium.mixin.fabric.core;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mojang.blaze3d.platform.Window;
import net.caffeinemc.mods.sodium.client.SodiumClientMod;
import net.caffeinemc.mods.sodium.client.compatibility.workarounds.Workarounds;
import net.caffeinemc.mods.sodium.client.services.PlatformRuntimeInformation;
import org.lwjgl.glfw.GLFW;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(Window.class)
public class WindowMixin {
@WrapOperation(method = "<init>", at = @At(value = "INVOKE", target = "Lorg/lwjgl/glfw/GLFW;glfwCreateWindow(IILjava/lang/CharSequence;JJ)J"), require = 0)
@WrapOperation(method = "<init>", at = @At(value = "INVOKE", target = "Lorg/lwjgl/glfw/GLFW;glfwCreateWindow(IILjava/lang/CharSequence;JJ)J"), remap = false)
public long setAdditionalWindowHints(int titleEncoded, int width, CharSequence height, long title, long monitor, Operation<Long> original) {
if (!PlatformRuntimeInformation.getInstance().platformHasEarlyLoadingScreen() && SodiumClientMod.options().performance.useNoErrorGLContext &&
if (SodiumClientMod.options().performance.useNoErrorGLContext &&
!Workarounds.isWorkaroundEnabled(Workarounds.Reference.NO_ERROR_CONTEXT_UNSUPPORTED)) {
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_NO_ERROR, GLFW.GLFW_TRUE);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.caffeinemc.mods.sodium.mixin.features.gui.hooks.settings;
package net.caffeinemc.mods.sodium.mixin.fabric.features.gui.hooks.settings;

import net.caffeinemc.mods.sodium.client.gui.SodiumOptionsGUI;
import net.minecraft.client.gui.screens.Screen;
Expand All @@ -17,10 +17,7 @@ protected OptionsScreenMixin(Component title) {
}

@Dynamic
@Inject(method = {
"method_19828",
"lambda$init$2"
}, require = 1, at = @At("HEAD"), cancellable = true)
@Inject(method = "method_19828", at = @At("HEAD"), cancellable = true)
private void open(CallbackInfoReturnable<Screen> ci) {
ci.setReturnValue(SodiumOptionsGUI.createScreen(this));
}
Expand Down
2 changes: 2 additions & 0 deletions fabric/src/main/resources/sodium-fabric.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"conformVisibility": true
},
"client": [
"core.WindowMixin",
"core.model.quad.BakedQuadMixin",
"features.gui.hooks.settings.OptionsScreenMixin",
"features.model.MultiPartBakedModelMixin",
"features.model.WeightedBakedModelMixin",
"features.render.model.block.ModelBlockRendererMixin"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.caffeinemc.mods.sodium.neoforge.mixin.core.render.world;

import net.caffeinemc.mods.sodium.client.render.SodiumWorldRenderer;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.world.level.block.entity.BlockEntity;
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 java.util.function.Consumer;

@Mixin(LevelRenderer.class)
public class LevelRendererMixin {
@Inject(method = "iterateVisibleBlockEntities", at = @At("HEAD"), cancellable = true)
public void replaceBlockEntityIteration(Consumer<BlockEntity> blockEntityConsumer, CallbackInfo ci) {
ci.cancel();

SodiumWorldRenderer.instance().iterateVisibleBlockEntities(blockEntityConsumer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.caffeinemc.mods.sodium.neoforge.mixin.features.gui.hooks.settings;

import net.caffeinemc.mods.sodium.client.gui.SodiumOptionsGUI;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.options.OptionsScreen;
import net.minecraft.network.chat.Component;
import org.spongepowered.asm.mixin.Dynamic;
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.CallbackInfoReturnable;

@Mixin(OptionsScreen.class)
public class OptionsScreenMixin extends Screen {
protected OptionsScreenMixin(Component title) {
super(title);
}

@Dynamic
@Inject(method = "lambda$init$2", at = @At("HEAD"), cancellable = true)
private void open(CallbackInfoReturnable<Screen> ci) {
ci.setReturnValue(SodiumOptionsGUI.createScreen(this));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.caffeinemc.mods.sodium.neoforge.mixin.features.options.render_layers;

import net.caffeinemc.mods.sodium.client.SodiumClientMod;
import net.minecraft.client.GraphicsStatus;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import org.spongepowered.asm.mixin.Mixin;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ItemBlockRenderTypes.class)
public class ItemBlockRenderTypesMixin {
@Unique
private static boolean leavesFancy;

// getRenderLayers is a NeoForge only function required for the leaves to properly work.
@Redirect(
method = { "getRenderLayers" },
at = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/ItemBlockRenderTypes;renderCutout:Z"))
private static boolean redirectLeavesShouldBeFancy() {
return leavesFancy;
}

@Inject(method = "setFancy", at = @At("RETURN"))
private static void onSetFancyGraphicsOrBetter(boolean fancyGraphicsOrBetter, CallbackInfo ci) {
leavesFancy = SodiumClientMod.options().quality.leavesQuality.isFancy(fancyGraphicsOrBetter ? GraphicsStatus.FANCY : GraphicsStatus.FAST);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.caffeinemc.mods.sodium.neoforge.mixin.workarounds.context_creation;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mojang.blaze3d.platform.Window;
import net.caffeinemc.mods.sodium.client.compatibility.workarounds.Workarounds;
import net.caffeinemc.mods.sodium.client.compatibility.workarounds.nvidia.NvidiaWorkarounds;
import net.caffeinemc.mods.sodium.client.services.PlatformRuntimeInformation;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.util.function.IntSupplier;
import java.util.function.LongSupplier;
import java.util.function.Supplier;

@Mixin(Window.class)
public class WindowMixin {
@WrapOperation(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/neoforged/fml/loading/ImmediateWindowHandler;setupMinecraftWindow(Ljava/util/function/IntSupplier;Ljava/util/function/IntSupplier;Ljava/util/function/Supplier;Ljava/util/function/LongSupplier;)J"), remap = false)
private long wrapGlfwCreateWindowForge(final IntSupplier width, final IntSupplier height, final Supplier<String> title, final LongSupplier monitor, Operation<Long> op) {
final boolean applyNvidiaWorkarounds = Workarounds.isWorkaroundEnabled(Workarounds.Reference.NVIDIA_THREADED_OPTIMIZATIONS);

if (applyNvidiaWorkarounds && !PlatformRuntimeInformation.getInstance().platformHasEarlyLoadingScreen()) {
NvidiaWorkarounds.install();
}

try {
return op.call(width, height, title, monitor);
} finally {
if (applyNvidiaWorkarounds) {
NvidiaWorkarounds.uninstall();
}
}
}
}
4 changes: 4 additions & 0 deletions neoforge/src/main/resources/sodium-forge.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
},
"client" : [
"core.model.quad.BakedQuadMixin",
"core.render.world.LevelRendererMixin",
"features.gui.hooks.settings.OptionsScreenMixin",
"features.model.MultiPartBakedModelMixin",
"features.model.WeightedBakedModelMixin",
"features.options.render_layers.ItemBlockRenderTypesMixin",
"features.render.model.block.ModelBlockRendererMixin",
"workarounds.context_creation.WindowMixin",
"AbstractBlockRenderContextMixin",
"AuxiliaryLightManagerMixin",
"ChunkRenderTypeSetAccessor",
Expand Down

0 comments on commit 6bb29b5

Please sign in to comment.