Skip to content

Commit

Permalink
Experimental HDR (only for my PC)
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed May 22, 2024
1 parent 2b8dd4f commit fe17c6d
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.irisshaders.batchedentityrendering.mixin;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.irisshaders.batchedentityrendering.impl.BlendingStateHolder;
import net.irisshaders.batchedentityrendering.impl.TransparencyType;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.entity.layers.HorseMarkingLayer;
import net.minecraft.resources.ResourceLocation;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(HorseMarkingLayer.class)
public class MixinHorseMarkingLayer {
@WrapOperation(method = "render(Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource;ILnet/minecraft/world/entity/animal/horse/Horse;FFFFFF)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/RenderType;entityTranslucent(Lnet/minecraft/resources/ResourceLocation;)Lnet/minecraft/client/renderer/RenderType;"))
private RenderType redirect(ResourceLocation resourceLocation, Operation<RenderType> original) {
RenderType renderType = original.call(resourceLocation);

((BlendingStateHolder) renderType).setTransparencyType(TransparencyType.OPAQUE);

return renderType;
}
}
5 changes: 5 additions & 0 deletions src/main/java/net/irisshaders/iris/Iris.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.irisshaders.iris.gl.shader.StandardMacros;
import net.irisshaders.iris.gui.debug.DebugLoadFailedGridScreen;
import net.irisshaders.iris.gui.screen.ShaderPackScreen;
import net.irisshaders.iris.hdr.GLFWHDRConfig;
import net.irisshaders.iris.helpers.OptionalBoolean;
import net.irisshaders.iris.pipeline.IrisRenderingPipeline;
import net.irisshaders.iris.pipeline.PipelineManager;
Expand Down Expand Up @@ -73,6 +74,7 @@ public class Iris {
private static final Map<String, String> shaderPackOptionQueue = new HashMap<>();
// Change this for snapshots!
private static final String backupVersionNumber = "1.20.3";
public static GLFWHDRConfig HDRCONFIG;
public static NamespacedId lastDimension = null;
public static boolean testing = false;
private static Path shaderpacksDirectory;
Expand All @@ -98,6 +100,7 @@ public class Iris {

static {
if (!BuildConfig.ACTIVATE_RENDERDOC && FabricLoader.getInstance().isDevelopmentEnvironment() && System.getProperty("user.name").contains("ims") && Util.getPlatform() == Util.OS.LINUX) {
Configuration.GLFW_LIBRARY_NAME.set("/home/ims/glfw/build/src/libglfw.so.3.5");

This comment has been minimized.

Copy link
@douira

douira May 22, 2024

Member

lmao

}
}

Expand Down Expand Up @@ -131,6 +134,8 @@ public static void onLoadingComplete() {
return;
}

HDRCONFIG = GLFWHDRConfig.glfwGetHDRConfig(Minecraft.getInstance().getWindow().getWindow());
Iris.logger.warn(String.valueOf(HDRCONFIG));
// Initialize the pipeline now so that we don't increase world loading time. Just going to guess that
// the player is in the overworld.
// See: https://github.com/IrisShaders/Iris/issues/323
Expand Down
155 changes: 155 additions & 0 deletions src/main/java/net/irisshaders/iris/hdr/GLFWHDRConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package net.irisshaders.iris.hdr;

import net.irisshaders.iris.Iris;
import org.jetbrains.annotations.Nullable;
import org.joml.Vector2f;
import org.joml.Vector2i;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWGammaRamp;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.system.NativeType;
import org.lwjgl.system.Struct;

import java.nio.ByteBuffer;
import java.text.NumberFormat;

import static org.lwjgl.system.APIUtil.apiGetFunctionAddress;
import static org.lwjgl.system.Checks.CHECKS;
import static org.lwjgl.system.Checks.check;
import static org.lwjgl.system.JNI.invokePP;
import static org.lwjgl.system.MemoryUtil.NULL;

public class GLFWHDRConfig extends Struct<GLFWHDRConfig> {
private static final long getHDRConfig = GLFW.getLibrary().getFunctionAddress("glfwGetHDRConfig");
public static final int SIZEOF;

/** The struct alignment in bytes. */
public static final int ALIGNOF;

/** The struct member offsets. */
public static final int
transfer_function,
output_display_primary_red_x,
output_display_primary_red_y,
output_display_primary_green_x,
output_display_primary_green_y,
output_display_primary_blue_x,
output_display_primary_blue_y,
output_white_point_x,
output_white_point_y,
max_luminance,
min_luminance,
max_full_frame_luminance;

static {
Layout layout = __struct(
__member(Integer.BYTES),
__member(Integer.BYTES),
__member(Integer.BYTES),
__member(Integer.BYTES),
__member(Integer.BYTES),
__member(Integer.BYTES),
__member(Integer.BYTES),
__member(Integer.BYTES),
__member(Integer.BYTES),
__member(Integer.BYTES),
__member(Integer.BYTES),
__member(Integer.BYTES)
);

SIZEOF = layout.getSize();
ALIGNOF = layout.getAlignment();

transfer_function = layout.offsetof(0);
output_display_primary_red_x = layout.offsetof(1);
output_display_primary_red_y = layout.offsetof(2);
output_display_primary_green_x = layout.offsetof(3);
output_display_primary_green_y = layout.offsetof(4);
output_display_primary_blue_x = layout.offsetof(5);
output_display_primary_blue_y = layout.offsetof(6);
output_white_point_x = layout.offsetof(7);
output_white_point_y = layout.offsetof(8);
max_luminance = layout.offsetof(9);
min_luminance = layout.offsetof(10);
max_full_frame_luminance = layout.offsetof(11);
}

protected GLFWHDRConfig(long address, @Nullable ByteBuffer container) {
super(address, container);
}

@Override
protected GLFWHDRConfig create(long address, @Nullable ByteBuffer container) {
return new GLFWHDRConfig(address, container);
}

public int getTransferFunction() {
return mgi(0);
}

public Vector2f getPrimaryRedCoord() {
return new Vector2f(mgi(1) / 50000.0f, mgi(2) / 50000.0f);
}

public Vector2f getPrimaryGreenCoord() {
return new Vector2f(mgi(3) / 50000.0f, mgi(4) / 50000.0f);
}

public Vector2f getPrimaryBlueCoord() {
return new Vector2f(mgi(5) / 50000.0f, mgi(6) / 50000.0f);
}

public Vector2f getWhitePoint() {
return new Vector2f(mgi(7) / 50000.0f, mgi(8) / 50000.0f);
}

public int getMaxLuminance() {
return mgi(9);
}

public int getMinLuminance() {
return mgi(10);
}

public int getMaxFullFrameLuminance() {
return mgi(11);
}

@Override
public String toString() {
return "Max: " + getMaxLuminance() + " Min: " + getMinLuminance() + " White point: " + getWhitePoint().toString(NumberFormat.getNumberInstance());
}

private int mgi(int i) {
return MemoryUtil.memGetInt(address() + (i * 4));
}

@Nullable
@NativeType("GLFWhdrconfig const *")
public static GLFWHDRConfig glfwGetHDRConfig(@NativeType("GLFWwindow *") long window) {
if (getHDRConfig == 0L) {
Iris.logger.fatal("Can't do this, no HDR in GLFW!");
return null;
}
long __result = nglfwGetHDRConfig(window);
return GLFWHDRConfig.createSafe(__result);
}

public static long nglfwGetHDRConfig(long window) {
long __functionAddress = getHDRConfig;
if (CHECKS) {
check(window);
}
return invokePP(window, __functionAddress);
}

@Nullable
public static GLFWHDRConfig createSafe(long address) {
return address == NULL ? null : new GLFWHDRConfig(address, null);
}

@Override
public int sizeof() {
return SIZEOF;
}
}
11 changes: 11 additions & 0 deletions src/main/java/net/irisshaders/iris/mixin/MixinRenderTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.mojang.blaze3d.pipeline.RenderTarget;
import net.irisshaders.iris.targets.Blaze3dRenderTargetExt;
import org.lwjgl.opengl.GL30;
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.ModifyArgs;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;

/**
* Allows Iris to detect when the depth texture was re-created, so we can re-attach it
Expand All @@ -29,6 +32,14 @@ public class MixinRenderTarget implements Blaze3dRenderTargetExt {
iris$colorBufferVersion++;
}

@ModifyArgs(method = "createBuffers",
at = @At(value = "INVOKE",
target = "Lcom/mojang/blaze3d/platform/GlStateManager;_texImage2D(IIIIIIIILjava/nio/IntBuffer;)V",
ordinal = 1))
public void init(Args args) {
args.set(2, GL30.GL_RGBA16F);
}

@Override
public int iris$getDepthBufferVersion() {
return iris$depthBufferVersion;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.irisshaders.iris.uniforms;

import net.irisshaders.iris.Iris;
import net.irisshaders.iris.gl.uniform.UniformHolder;
import net.irisshaders.iris.gl.uniform.UniformUpdateFrequency;
import net.irisshaders.iris.gui.option.IrisVideoSettings;
Expand All @@ -24,6 +25,7 @@
import java.util.Objects;
import java.util.stream.StreamSupport;

import static net.irisshaders.iris.gl.uniform.UniformUpdateFrequency.ONCE;
import static net.irisshaders.iris.gl.uniform.UniformUpdateFrequency.PER_FRAME;

public class IrisExclusiveUniforms {
Expand Down Expand Up @@ -71,6 +73,15 @@ public static void addIrisExclusiveUniforms(UniformHolder uniforms) {
return zero;
}
});

// HDR!!!

uniforms.uniform1i(ONCE, "maxLuminance", () -> Iris.HDRCONFIG.getMaxLuminance());
uniforms.uniform1i(ONCE, "minLuminance", () -> Iris.HDRCONFIG.getMinLuminance());
uniforms.uniform2f(ONCE, "redPrimary", () -> Iris.HDRCONFIG.getPrimaryRedCoord());
uniforms.uniform2f(ONCE, "bluePrimary", () -> Iris.HDRCONFIG.getPrimaryBlueCoord());
uniforms.uniform2f(ONCE, "greenPrimary", () -> Iris.HDRCONFIG.getPrimaryGreenCoord());
uniforms.uniform2f(ONCE, "whitePoint", () -> Iris.HDRCONFIG.getWhitePoint());
}

private static int getCurrentSelectedBlockId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"MixinCompositeRenderType",
"MixinDebugScreenOverlay",
"MixinFishingHookRenderer",
"MixinHorseMarkingLayer",
"MixinLevelRenderer",
"MixinLevelRenderer_EntityListSorting",
"MixinRenderBuffers",
Expand Down

0 comments on commit fe17c6d

Please sign in to comment.