Skip to content

Commit

Permalink
HDR part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Sep 19, 2024
1 parent dfd9ae4 commit 1fa508a
Show file tree
Hide file tree
Showing 16 changed files with 371 additions and 61 deletions.
8 changes: 6 additions & 2 deletions common/src/main/java/net/irisshaders/iris/GLFWAccess.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.client.Minecraft;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.system.JNI;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.system.SharedLibrary;

import java.lang.reflect.Field;
Expand All @@ -27,10 +28,13 @@ public class GLFWAccess {
private static boolean done;

public static void tryHDRConf() {
if (done) return;
conf = GLFWHDRConfig.create(MemoryUtil.nmemAlloc(GLFWHDRConfig.SIZEOF));
conf.setMaxLuminance(1000);
conf.setSDRWhite(100);
if (true) return;
done = true;
long addr = JNI.invokePP(Minecraft.getInstance().getWindow().getWindow(), lib.getFunctionAddress("glfwGetHDRConfig"));
conf = GLFWHDRConfig.create(addr);
System.out.println("Testing red primary X: " + conf.getPrimaryRedX());
System.out.println("Testing SDR white: " + conf.getSDRWhite());
}
}
20 changes: 20 additions & 0 deletions common/src/main/java/net/irisshaders/iris/GLFWHDRConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.ByteBuffer;

import static org.lwjgl.system.MemoryUtil.memGetFloat;
import static org.lwjgl.system.MemoryUtil.memPutFloat;

public class GLFWHDRConfig extends Struct<GLFWHDRConfig> {
public static final int SIZEOF, ALIGNOF;
Expand All @@ -24,6 +25,7 @@ public class GLFWHDRConfig extends Struct<GLFWHDRConfig> {
private static final int OFFSET_MAX_LUMINANCE;
private static final int OFFSET_MIN_LUMINANCE;
private static final int OFFSET_MAX_FRAME_LUMINANCE;
private static final int OFFSET_SDR_WHITE;

static {
var layout = __struct(
Expand All @@ -38,6 +40,7 @@ public class GLFWHDRConfig extends Struct<GLFWHDRConfig> {
__member(Float.BYTES),
__member(Float.BYTES),
__member(Float.BYTES),
__member(Float.BYTES),
__member(Float.BYTES)
);

Expand All @@ -56,6 +59,7 @@ public class GLFWHDRConfig extends Struct<GLFWHDRConfig> {
OFFSET_MAX_LUMINANCE = layout.offsetof(9);
OFFSET_MIN_LUMINANCE = layout.offsetof(10);
OFFSET_MAX_FRAME_LUMINANCE = layout.offsetof(11);
OFFSET_SDR_WHITE = layout.offsetof(12);
}

GLFWHDRConfig(long address, ByteBuffer container) {
Expand All @@ -79,6 +83,10 @@ public float getTransferFunction() {
return memGetFloat(this.address + OFFSET_TRANSFER_FUNCTION);
}

public void setTransferFunction(float value) {
memPutFloat(this.address + OFFSET_TRANSFER_FUNCTION, value);
}

public float getPrimaryRedX() {
return memGetFloat(this.address + OFFSET_PRIMARY_RED_X);
}
Expand Down Expand Up @@ -123,6 +131,18 @@ public float getMinLuminance() {
return memGetFloat(this.address + OFFSET_MIN_LUMINANCE);
}

public void setMaxLuminance(float value) {
memPutFloat(this.address + OFFSET_MAX_LUMINANCE, value);
}

public float getSDRWhite() {
return memGetFloat(this.address + OFFSET_SDR_WHITE);
}

public void setSDRWhite(float value) {
memPutFloat(this.address + OFFSET_SDR_WHITE, value);
}

@Override
public int sizeof() {
return SIZEOF;
Expand Down
1 change: 0 additions & 1 deletion common/src/main/java/net/irisshaders/iris/Iris.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public class Iris {
private static boolean loadShaderPackWhenPossible;

static {
Configuration.GLFW_LIBRARY_NAME.set(IrisPlatformHelpers.getInstance().getGameDir().resolve("glfw3.dll").toAbsolutePath().toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,73 @@
package net.irisshaders.iris.compat.dh;

import com.sun.jna.NativeLibrary;
import net.irisshaders.iris.platform.IrisPlatformHelpers;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.system.Configuration;
import org.lwjgl.system.JNI;
import org.lwjgl.system.Library;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.system.Platform;
import org.lwjgl.system.SharedLibrary;
import org.objectweb.asm.tree.ClassNode;
import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin;
import org.spongepowered.asm.mixin.extensibility.IMixinInfo;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.util.List;
import java.util.Set;

public class DHMixinConfigPlugin implements IMixinConfigPlugin {

private static SharedLibrary GLFW;

static {
Class<?> clas = GLFW.class;

try {
Field field = clas.getDeclaredField("GLFW");
field.setAccessible(true);
GLFW = (SharedLibrary) field.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) {

}
@Override
public void onLoad(String mixinPackage) {
if (true) return;
System.out.println("[Iris] Hijacking GLFW.");
long base = GLFW.address();
long maxSize = 0x100000;
if (GLFW.getPath() != null) {
try {
maxSize = Files.size(new File(GLFW.getPath()).toPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
long result = -1;
for (long offset = 0; offset < maxSize; offset++) {
if (MemoryUtil.memGetInt(base + offset) == 0x0000202b) {
result = offset;
break;
}
}

try(var kernel = NativeLibrary.getInstance("Kernel32")) {
long VirtualProtect = com.sun.jna.Pointer.nativeValue(kernel.getFunction("VirtualProtect"));
long aa = MemoryUtil.nmemAlloc(4);
JNI.invokePPPPP(base+result,4,0x40,aa, VirtualProtect);
MemoryUtil.memPutInt(base+result, 0x000021A0);
JNI.invokePPPPP(base+result,4,MemoryUtil.memGetInt(aa), aa, VirtualProtect);
MemoryUtil.nmemFree(aa);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package net.irisshaders.iris.mixin;

import com.google.common.collect.Lists;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.platform.GlUtil;
import com.mojang.blaze3d.shaders.Program;
import com.mojang.blaze3d.vertex.PoseStack;
import net.irisshaders.iris.Iris;
import net.irisshaders.iris.api.v0.IrisApi;
import net.irisshaders.iris.gl.program.IrisProgramTypes;
import net.irisshaders.iris.pathways.BlurAccess;
import net.irisshaders.iris.pathways.HandRenderer;
import net.irisshaders.iris.pipeline.IrisRenderingPipeline;
import net.irisshaders.iris.pipeline.ShaderRenderingPipeline;
import net.irisshaders.iris.pipeline.WorldRenderingPhase;
import net.irisshaders.iris.pipeline.WorldRenderingPipeline;
Expand All @@ -25,6 +28,8 @@
import net.minecraft.client.renderer.RenderBuffers;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.server.packs.resources.ResourceManager;
import org.lwjgl.opengl.GL46C;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -37,10 +42,33 @@
import java.util.ArrayList;

@Mixin(GameRenderer.class)
public class MixinGameRenderer {
public abstract class MixinGameRenderer implements BlurAccess {
@Shadow
private boolean renderHand;

@Shadow
@Final
private Minecraft minecraft;

@Unique
private boolean allowBlur;
private RenderTarget newTarget = new RenderTarget(false) {
@Override
public void resize(int width, int height, boolean clearError) {
super.resize(width, height, clearError);
}

@Override
public int getColorTextureId() {
if (!(Iris.getPipelineManager().getPipelineNullable() instanceof IrisRenderingPipeline)) return Minecraft.getInstance().getMainRenderTarget().getColorTextureId();

return ((IrisRenderingPipeline) Iris.getPipelineManager().getPipelineNullable()).getMainTarget();
}
};

@Shadow
public abstract void processBlurEffect(float partialTick);

@Inject(method = "getPositionShader", at = @At("HEAD"), cancellable = true)
private static void iris$overridePositionShader(CallbackInfoReturnable<ShaderInstance> cir) {
if (isSky()) {
Expand Down Expand Up @@ -466,12 +494,40 @@ private static void override(ShaderKey key, CallbackInfoReturnable<ShaderInstanc
}
}



@Inject(method = "processBlurEffect", at = @At("HEAD"), cancellable = true)
private void lol(float partialTick, CallbackInfo ci) {
if (!allowBlur) {
ci.cancel();
}
}

@Redirect(method = "loadBlurEffect", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;getMainRenderTarget()Lcom/mojang/blaze3d/pipeline/RenderTarget;"))
private RenderTarget lol2(Minecraft instance) {
return newTarget;
}

@Inject(method = "render", at = @At("HEAD"))
private void iris$startFrame(DeltaTracker deltaTracker, boolean bl, CallbackInfo ci) {
// This allows certain functions like float smoothing to function outside a world.
CapturedRenderingState.INSTANCE.setRealTickDelta(deltaTracker.getGameTimeDeltaPartialTick(true));
SystemTimeUniforms.COUNTER.beginFrame();
SystemTimeUniforms.TIMER.beginFrame(Util.getNanos());

minecraft.getMainRenderTarget().bindWrite(true);
GlStateManager._clearColor(0.0f, 0.0f, 0.0f, 1.0f);
GlStateManager._clear(GL46C.GL_COLOR_BUFFER_BIT, Minecraft.ON_OSX);
}

@Inject(method = "resize", at = @At("HEAD"))
private void res(int width, int height, CallbackInfo ci) {
newTarget.resize(width, height, false);
}

@Inject(method = "<init>", at = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/GameRenderer;minecraft:Lnet/minecraft/client/Minecraft;"))
private void iris$set(Minecraft arg, ItemInHandRenderer arg2, ResourceManager arg3, RenderBuffers arg4, CallbackInfo ci) {
newTarget.resize(arg.getWindow().getWidth(), arg.getWindow().getHeight(), false);
}

@Inject(method = "<init>", at = @At("TAIL"))
Expand All @@ -491,8 +547,8 @@ private static void override(ShaderKey key, CallbackInfoReturnable<ShaderInstanc
itemInHandRenderer.renderHandsWithItems(tickDelta, poseStack, bufferSource, localPlayer, light);
}

@Inject(method = "renderLevel", at = @At("TAIL"))
private void iris$runColorSpace(DeltaTracker deltaTracker, CallbackInfo ci) {
@Inject(method = "render", at = @At("TAIL"))
private void iris$runColorSpace(DeltaTracker deltaTracker, boolean renderLevel, CallbackInfo ci) {
Iris.getPipelineManager().getPipeline().ifPresent(WorldRenderingPipeline::finalizeGameRendering);
}

Expand All @@ -504,4 +560,11 @@ private static void override(ShaderKey key, CallbackInfoReturnable<ShaderInstanc
programs.addAll(IrisProgramTypes.TESS_EVAL.getPrograms().values());
return programs;
}

@Override
public void processBlurIris(int inTarget, int outFb, float f) {
allowBlur = true;
processBlurEffect(f);
allowBlur = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private Frustum changeFrustum(Frustum frustum) {
private void iris$endLevelRender(DeltaTracker deltaTracker, boolean bl, Camera camera, GameRenderer gameRenderer, LightTexture lightTexture, Matrix4f modelMatrix, Matrix4f matrix4f2, CallbackInfo ci) {
HandRenderer.INSTANCE.renderTranslucent(modelMatrix, deltaTracker.getGameTimeDeltaPartialTick(true), camera, gameRenderer, pipeline);
Minecraft.getInstance().getProfiler().popPush("iris_final");
pipeline.finalizeLevelRendering();
pipeline.finalizeLevelRendering(deltaTracker.getGameTimeDeltaPartialTick(true));
pipeline = null;

if (!warned) {
Expand Down
Loading

0 comments on commit 1fa508a

Please sign in to comment.