Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion common/src/main/java/xyz/rrtt217/config/HDRModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,45 @@
@Config(name = "hdr_mod")
public class HDRModConfig implements ConfigData {
@ConfigEntry.Gui.Tooltip
@ConfigEntry.Category("general")
public boolean enableHDR = true;

@ConfigEntry.Gui.Tooltip
@ConfigEntry.Category("general")
public float uiBrightness = -1.0f;
@ConfigEntry.Gui.Tooltip
@ConfigEntry.Category("general")
public float customGamePaperWhiteBrightness = -1.0f;
@ConfigEntry.Gui.Tooltip
@ConfigEntry.Category("general")
public float customGamePeakBrightness = 1000.0f;
@ConfigEntry.Gui.Tooltip
@ConfigEntry.Category("general")
public float customGameMinimumBrightness = 0.0f;
@ConfigEntry.Gui.Tooltip
@ConfigEntry.Category("general")
public float customEotfEmulate = System.getProperty("os.name").startsWith("Windows") ? -1.0f : 0.0f;

@ConfigEntry.Gui.Tooltip
@ConfigEntry.Category("advanced")
public boolean onlyUpgradeNecessaryTexture = false;
@ConfigEntry.Gui.Tooltip
@ConfigEntry.Category("advanced")
public boolean writeBeforeBlitToMainTarget = false;
@ConfigEntry.Gui.Tooltip
@ConfigEntry.Category("advanced")
public boolean useRGBA16UNORM = System.getProperty("os.name").toLowerCase().contains("linux");

@ConfigEntry.Category("debug")
public boolean autoSetPrimaries = true;
@ConfigEntry.Category("debug")
public Primaries customPrimaries = Primaries.SRGB;
@ConfigEntry.Category("debug")
public boolean autoSetTransferFunction = true;
@ConfigEntry.Category("debug")
public TransferFunction customTransferFunction = TransferFunction.SRGB;

@ConfigEntry.Category("debug")
public boolean forceDisableGlfwWorkound = false;
@ConfigEntry.Category("debug")
public boolean forceDisableBeforeBlitPipeline = false;
}
41 changes: 25 additions & 16 deletions common/src/main/java/xyz/rrtt217/core/PngjHDRScreenshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.function.Consumer;

public class PngjHDRScreenshot {
Expand Down Expand Up @@ -93,22 +92,32 @@ public static void grab(File baseDirectory, @Nullable String string, GpuTexture
for (int x = 0; x < width; x++) {
// Interpret data.
int basePos = ((y * width + x) * 4) * 2;
for (int c = 0; c < 4; c++) {
bits = mappedView.data().getShort(basePos + c * 2);
datas[c] = Float.float16ToFloat(bits);
// RGBA16 UNORM.
if(config.useRGBA16UNORM) {
for (int c = 0; c < png.imgInfo.channels; c++) {
bits = mappedView.data().getShort(basePos + c * 2);
scanline[x * png.imgInfo.channels + c] = bits;
}
}
// Do transform.
if (doPrimariesTransform) {
System.arraycopy(datas, 0, rgb, 0, 3);
System.arraycopy(ColorTransforms.linearColorspaceTransform(rgb, ColorTransforms.linear709to2020Matrix), 0, datas, 0, 3);
}
if (doTransferTransform) {
System.arraycopy(datas, 0, rgb, 0, 3);
System.arraycopy(ColorTransforms.scRGBtoPQ(rgb, config.customGamePaperWhiteBrightness < 0 ? GLFWColorManagement.glfwGetWindowSdrWhiteLevel(Minecraft.getInstance().getWindow().handle()) : config.customGamePaperWhiteBrightness), 0, datas, 0, 3);
}
// Write to line.
for (int c = 0; c < png.imgInfo.channels; c++) {
scanline[x * png.imgInfo.channels + c] = (int) (datas[c] * 65535);
else{
// RGBA16F.
for (int c = 0; c < 4; c++) {
bits = mappedView.data().getShort(basePos + c * 2);
datas[c] = Float.float16ToFloat(bits);
}
// Do transform.
if (doPrimariesTransform) {
System.arraycopy(datas, 0, rgb, 0, 3);
System.arraycopy(ColorTransforms.linearColorspaceTransform(rgb, ColorTransforms.linear709to2020Matrix), 0, datas, 0, 3);
}
if (doTransferTransform) {
System.arraycopy(datas, 0, rgb, 0, 3);
System.arraycopy(ColorTransforms.scRGBtoPQ(rgb, config.customGamePaperWhiteBrightness < 0 ? GLFWColorManagement.glfwGetWindowSdrWhiteLevel(Minecraft.getInstance().getWindow().handle()) : config.customGamePaperWhiteBrightness), 0, datas, 0, 3);
}
// Write to line.
for (int c = 0; c < png.imgInfo.channels; c++) {
scanline[x * png.imgInfo.channels + c] = (int) (datas[c] * 65535);
}
}
}
// Write to file.
Expand Down
16 changes: 11 additions & 5 deletions common/src/main/java/xyz/rrtt217/mixin/MixinGlCommandEncoder.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package xyz.rrtt217.mixin;

import com.mojang.blaze3d.opengl.GlCommandEncoder;
import me.shedaniel.autoconfig.AutoConfig;
import org.lwjgl.opengl.GL30;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArgs;
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import xyz.rrtt217.config.HDRModConfig;
import xyz.rrtt217.util.HDRModInjectHooks;

@Mixin(GlCommandEncoder.class)
public class MixinGlCommandEncoder {
@ModifyArgs(method = "copyTextureToBuffer(Lcom/mojang/blaze3d/textures/GpuTexture;Lcom/mojang/blaze3d/buffers/GpuBuffer;JLjava/lang/Runnable;IIIII)V", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/opengl/GlStateManager;_readPixels(IIIIIIJ)V"))
private void modifyReadPixelFormat(Args args){
if(HDRModInjectHooks.isInjectEnabled()) args.set(5, GL30.GL_HALF_FLOAT);
@ModifyArg(method = "copyTextureToBuffer(Lcom/mojang/blaze3d/textures/GpuTexture;Lcom/mojang/blaze3d/buffers/GpuBuffer;JLjava/lang/Runnable;IIIII)V", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/opengl/GlStateManager;_readPixels(IIIIIIJ)V"), index = 5)
private int modifyReadPixelFormat(int i){
HDRModConfig config = AutoConfig.getConfigHolder(HDRModConfig.class).getConfig();
if(HDRModInjectHooks.isInjectEnabled()){
if(config.useRGBA16UNORM) return GL30.GL_UNSIGNED_SHORT;
return GL30.GL_HALF_FLOAT;
}
return i;
}
}
22 changes: 13 additions & 9 deletions common/src/main/java/xyz/rrtt217/mixin/MixinGlDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@
import me.shedaniel.autoconfig.AutoConfig;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.lwjgl.opengl.GL30;
import xyz.rrtt217.HDRMod;
import org.spongepowered.asm.mixin.injection.ModifyArgs;
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;
import xyz.rrtt217.config.HDRModConfig;
import xyz.rrtt217.util.HDRModInjectHooks;

@Mixin(GlDevice.class)
public class MixinGlDevice {
//FIXME: The mixin upgrades all RGBA8 color texture created by createTexture, which may cause performance issues.
//TODO: Only upgrade RGBA8 color texture belonging to a RenderTarget, or the main RenderTarget.
@ModifyArg(method = "createTexture(Ljava/lang/String;ILcom/mojang/blaze3d/textures/TextureFormat;IIII)Lcom/mojang/blaze3d/textures/GpuTexture;", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/opengl/GlStateManager;_texImage2D(IIIIIIIILjava/nio/ByteBuffer;)V", ordinal = 1),index = 2 )
private int hdr_mod$upgradeColorBufferFormat$0(int i)
@ModifyArgs(method = "createTexture(Ljava/lang/String;ILcom/mojang/blaze3d/textures/TextureFormat;IIII)Lcom/mojang/blaze3d/textures/GpuTexture;", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/opengl/GlStateManager;_texImage2D(IIIIIIIILjava/nio/ByteBuffer;)V", ordinal = 1))
private void hdr_mod$upgradeColorBufferFormat$0(Args args)
{
HDRModConfig config = AutoConfig.getConfigHolder(HDRModConfig.class).getConfig();
if(config.enableHDR && i == GlConst.toGlInternalId(TextureFormat.RGBA8) && (!config.onlyUpgradeNecessaryTexture || HDRModInjectHooks.isInjectEnabled())) {
return GL30.GL_RGBA16F;
if(config.enableHDR && args.get(2).equals(GlConst.toGlInternalId(TextureFormat.RGBA8)) && (!config.onlyUpgradeNecessaryTexture || HDRModInjectHooks.isInjectEnabled())) {
if(HDRModInjectHooks.isInject2Enabled() && config.useRGBA16UNORM) {
args.set(2,GL30.GL_RGBA16);
args.set(7,GL30.GL_UNSIGNED_SHORT);
}
else {
args.set(2, GL30.GL_RGBA16F);
args.set(7, GL30.GL_HALF_FLOAT);
}
}
return i;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public class MixinRenderTarget {
// If texture / textureView have not been created yet, create them.
if (BeforeBlitRenderer.beforeBlitTexture == null) {
HDRModInjectHooks.enableInject();
HDRModInjectHooks.enableInject2();
BeforeBlitRenderer.beforeBlitTexture = RenderSystem.getDevice().createTexture(() -> "Before Blit Ping-pong Texture", 15, TextureFormat.RGBA8, this.width, this.height, 1, 1);
HDRModInjectHooks.disableInject2();
HDRModInjectHooks.disableInject();
}

Expand All @@ -82,8 +84,10 @@ public class MixinRenderTarget {
BeforeBlitRenderer.beforeBlitTextureView.close();
BeforeBlitRenderer.beforeBlitTexture.close();
HDRModInjectHooks.enableInject();
HDRModInjectHooks.enableInject2();
BeforeBlitRenderer.beforeBlitTexture = RenderSystem.getDevice().createTexture(() -> "Before Blit Ping-pong Texture", 15, TextureFormat.RGBA8, this.width, this.height, 1, 1);
BeforeBlitRenderer.beforeBlitTextureView = RenderSystem.getDevice().createTextureView(BeforeBlitRenderer.beforeBlitTexture);
HDRModInjectHooks.disableInject2();
HDRModInjectHooks.disableInject();
}

Expand Down Expand Up @@ -124,7 +128,7 @@ public class MixinRenderTarget {
}
}
}
@ModifyArg(method = "blitToScreen", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/systems/CommandEncoder;presentTexture(Lcom/mojang/blaze3d/textures/GpuTextureView;)V"), index = 0)
@ModifyArg(method = "blitToScreen", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/systems/CommandEncoder;presentTexture(Lcom/mojang/blaze3d/textures/GpuTextureView;)V"), index = 0)
private GpuTextureView hdr_mod$modifyTextureToBePresented(GpuTextureView gpuTextureView){
HDRModConfig config = AutoConfig.getConfigHolder(HDRModConfig.class).getConfig();
if(config.writeBeforeBlitToMainTarget || config.forceDisableBeforeBlitPipeline) return gpuTextureView;
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/xyz/rrtt217/mixin/MixinWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public abstract class MixinWindow {
GLFW.glfwWindowHint(GLFW.GLFW_GREEN_BITS, 16);
GLFW.glfwWindowHint(GLFW.GLFW_BLUE_BITS, 16);
// For float buffer. Note: Because Intel on Windows do not support float buffer (WGL_TYPE_RGBA_FLOAT_ARB), Intel users can't use this mod natively.
if(!applyWorkaround) {
if(!applyWorkaround && !config.useRGBA16UNORM) {
GLFW.glfwWindowHint(0x00021011,GLFW.GLFW_TRUE);
}
else {
else if(applyWorkaround) {
HDRMod.LOGGER.warn("A workaround has been applied for your platform and hardware. HDR Mod may or may not work.");
}
}
Expand Down
10 changes: 10 additions & 0 deletions common/src/main/java/xyz/rrtt217/util/HDRModInjectHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

public class HDRModInjectHooks {
private static final ThreadLocal<Boolean> shouldInject = ThreadLocal.withInitial(() -> false);
private static final ThreadLocal<Boolean> shouldInject2 = ThreadLocal.withInitial(() -> false);

public static void enableInject() {
shouldInject.set(true);
}
public static void enableInject2() {
shouldInject2.set(true);
}

public static void disableInject() {
shouldInject.remove();
}
public static void disableInject2() {
shouldInject2.remove();
}

public static boolean isInjectEnabled() {
return shouldInject.get();
}
public static boolean isInject2Enabled() {
return shouldInject2.get();
}
}
20 changes: 14 additions & 6 deletions common/src/main/resources/assets/hdr_mod/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@
"text.autoconfig.hdr_mod.option.customGamePeakBrightness.@Tooltip": "In nits, the maximum the display can output. Set it negative to use display reported value.",
"text.autoconfig.hdr_mod.option.customEotfEmulate.@Tooltip": "Correct raised shadows to match SDR. Use if OS or display doesn't do so already. Set it negative to use display reported SDR paper white.",

"text.autoconfig.hdr_mod.option.onlyUpgradeNecessaryTexture": "(Advanced) Only Upgrade Necessary Texture",
"text.autoconfig.hdr_mod.option.onlyUpgradeNecessaryTexture": "Only Upgrade Necessary Texture",
"text.autoconfig.hdr_mod.option.onlyUpgradeNecessaryTexture.@Tooltip": "Only upgrade necessary texture from RGBA8 to RGBA16F. When enabled, only color texture of a render target or the BEFORE_BLIT ping-pong texture will be upgraded. When disabled, all color textures created by vanilla will be upgraded.",
"text.autoconfig.hdr_mod.option.writeBeforeBlitToMainTarget": "(Advanced) Write BEFORE_BLIT Render Target To Main Render Target",
"text.autoconfig.hdr_mod.option.writeBeforeBlitToMainTarget": "Write BEFORE_BLIT Render Target To Main Render Target",
"text.autoconfig.hdr_mod.option.writeBeforeBlitToMainTarget.@Tooltip": "If on, ingame screenshot and ReplayMod/Flashback will be severely broken.\n If off, some mods that rewrite blitToScreen may break the game. Keep 'off' if unsure.",
"text.autoconfig.hdr_mod.option.useRGBA16UNORM": "Use RGBA16 UNORM Instead Of RGBA16F For BEFORE_BLIT Render Target",
"text.autoconfig.hdr_mod.option.useRGBA16UNORM.@Tooltip": "Improve precision when using BT.2100 PQ transfer function, but completely breaks scRGB linear. Keep default if unsure.",

"text.autoconfig.hdr_mod.option.autoSetPrimaries": "Auto Set Primaries",
"text.autoconfig.hdr_mod.option.customPrimaries": "(Debug) Custom Primaries ",
"text.autoconfig.hdr_mod.option.customPrimaries": "Custom Primaries ",
"text.autoconfig.hdr_mod.option.autoSetTransferFunction": "Auto Set Transfer Function",
"text.autoconfig.hdr_mod.option.customTransferFunction": "(Debug) Custom Transfer Function ",
"text.autoconfig.hdr_mod.option.forceDisableGlfwWorkound": "(Debug) Force Disable GLFW Workaround",
"text.autoconfig.hdr_mod.option.forceDisableBeforeBlitPipeline": "(Debug) Force Disable the BEFORE_BLIT Pipeline",
"text.autoconfig.hdr_mod.option.customTransferFunction": "Custom Transfer Function ",
"text.autoconfig.hdr_mod.option.forceDisableGlfwWorkound": "Force Disable GLFW Workaround",
"text.autoconfig.hdr_mod.option.forceDisableBeforeBlitPipeline": "Force Disable the BEFORE_BLIT Pipeline",

"text.autoconfig.hdr_mod.title": "HDR Mod Config",

"text.autoconfig.hdr_mod.category.general": "General",
"text.autoconfig.hdr_mod.category.advanced": "Advanced",
"text.autoconfig.hdr_mod.category.debug": "Debug",

"key.hdr_mod.open_config_menu": "Open Config Menu",
"key.hdr_mod.take_hdr_screenshot": "Take HDR Screenshot",
"key.category.hdr_mod.main": "HDR Mod"
Expand Down
20 changes: 14 additions & 6 deletions common/src/main/resources/assets/hdr_mod/lang/zh_cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@
"text.autoconfig.hdr_mod.option.customGamePeakBrightness.@Tooltip": "以尼特为单位,设置显示器可输出的最大亮度。设置为负数以使用显示器报告的值。",
"text.autoconfig.hdr_mod.option.customEotfEmulate.@Tooltip": "校正提升的黑色区域以匹配 SDR。如果操作系统或显示器尚未执行此操作,请使用此项。设置为负数以使用显示器报告的 SDR 参考白亮度。",

"text.autoconfig.hdr_mod.option.onlyUpgradeNecessaryTexture": "(高级) 仅升级必要纹理",
"text.autoconfig.hdr_mod.option.onlyUpgradeNecessaryTexture": "仅升级必要纹理",
"text.autoconfig.hdr_mod.option.onlyUpgradeNecessaryTexture.@Tooltip": "仅将必要的纹理从 RGBA8 升级到 RGBA16F。启用时,只有渲染目标的颜色纹理和 BEFORE_BLIT Ping-pong纹理会被升级。禁用时,所有由原版创建的彩色纹理都将被升级。",
"text.autoconfig.hdr_mod.option.writeBeforeBlitToMainTarget": "(高级) 将BEFORE_BLIT 渲染目标纹理复制到主渲染目标纹理",
"text.autoconfig.hdr_mod.option.writeBeforeBlitToMainTarget": "将BEFORE_BLIT 渲染目标纹理复制到主渲染目标纹理",
"text.autoconfig.hdr_mod.option.writeBeforeBlitToMainTarget.@Tooltip": "如果开启,原版游戏截图和ReplayMod/Flashback模组录像会严重损坏。\n 如果关闭,某些重写blitToScreen的模组可能会破坏游戏. 如果不确定,请保持关闭。",
"text.autoconfig.hdr_mod.option.useRGBA16UNORM": "为BEFORE_BLIT 渲染目标纹理使用归一化定点RGBA16而非浮点RGBA16格式",
"text.autoconfig.hdr_mod.option.useRGBA16UNORM.@Tooltip": "提高使用BT.2100 PQ转换函数时的精度,但会完全破坏scRGB。 如果不确定,请保持默认值。",

"text.autoconfig.hdr_mod.option.autoSetPrimaries": "自动设置基色",
"text.autoconfig.hdr_mod.option.customPrimaries": "(调试) 自定义基色",
"text.autoconfig.hdr_mod.option.customPrimaries": "自定义基色",
"text.autoconfig.hdr_mod.option.autoSetTransferFunction": "自动设置传递函数",
"text.autoconfig.hdr_mod.option.customTransferFunction": "(调试) 自定义传递函数",
"text.autoconfig.hdr_mod.option.forceDisableGlfwWorkound": "(调试) 强制禁用 GLFW 兼容性调整",
"text.autoconfig.hdr_mod.option.forceDisableBeforeBlitPipeline": "(调试) 强制禁用 BEFORE_BLIT 管线",
"text.autoconfig.hdr_mod.option.customTransferFunction": "自定义传递函数",
"text.autoconfig.hdr_mod.option.forceDisableGlfwWorkound": "强制禁用 GLFW 兼容性调整",
"text.autoconfig.hdr_mod.option.forceDisableBeforeBlitPipeline": "强制禁用 BEFORE_BLIT 管线",

"text.autoconfig.hdr_mod.title": "HDR 模组配置",

"text.autoconfig.hdr_mod.category.general": "通用",
"text.autoconfig.hdr_mod.category.advanced": "高级",
"text.autoconfig.hdr_mod.category.debug": "调试",

"key.hdr_mod.open_config_menu": "打开配置菜单",
"key.hdr_mod.take_hdr_screenshot": "拍摄HDR截图",
"key.category.hdr_mod.main": "HDR 模组"
Expand Down