Skip to content

Commit 32bbac2

Browse files
committed
fix: avoid macOS reduced resolution over-scaling
1 parent afc1766 commit 32bbac2

3 files changed

Lines changed: 43 additions & 3 deletions

File tree

common/src/main/java/me/flashyreese/mods/sodiumextra/client/util/MacReducedResolution.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package me.flashyreese.mods.sodiumextra.client.util;
22

3+
import com.mojang.blaze3d.systems.GpuSurface;
34
import me.flashyreese.mods.sodiumextra.client.SodiumExtraClientMod;
45
import net.minecraft.util.Util;
56

67
public final class MacReducedResolution {
8+
private static int windowWidth = -1;
9+
private static int windowHeight = -1;
10+
711
public static boolean isEnabled() {
812
return Util.getPlatform() == Util.OS.OSX && SodiumExtraClientMod.options().extraSettings.reduceResolutionOnMac;
913
}
@@ -12,7 +16,28 @@ public static int reduce(int value) {
1216
return Math.max(1, value / 2);
1317
}
1418

19+
public static void rememberWindowSize(int width, int height) {
20+
windowWidth = width;
21+
windowHeight = height;
22+
}
23+
24+
public static boolean shouldReduceFramebuffer(int framebufferWidth, int framebufferHeight, int windowWidth, int windowHeight) {
25+
return isEnabled() && isHighDpiFramebuffer(framebufferWidth, framebufferHeight, windowWidth, windowHeight);
26+
}
27+
28+
public static GpuSurface.Configuration reduceSurfaceConfiguration(GpuSurface.Configuration config) {
29+
if (!shouldReduceFramebuffer(config.width(), config.height(), windowWidth, windowHeight)) {
30+
return config;
31+
}
32+
33+
return new GpuSurface.Configuration(reduce(config.width()), reduce(config.height()), config.presentMode());
34+
}
35+
1536
public static boolean shouldScalePresentation(int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) {
1637
return isEnabled() && (sourceWidth < targetWidth || sourceHeight < targetHeight);
1738
}
39+
40+
private static boolean isHighDpiFramebuffer(int framebufferWidth, int framebufferHeight, int windowWidth, int windowHeight) {
41+
return windowWidth > 0 && windowHeight > 0 && (framebufferWidth > windowWidth || framebufferHeight > windowHeight);
42+
}
1843
}

common/src/main/java/me/flashyreese/mods/sodiumextra/mixin/reduce_resolution_on_mac/MixinVulkanGpuSurface.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.llamalad7.mixinextras.sugar.Local;
44
import com.mojang.blaze3d.systems.CommandEncoderBackend;
5+
import com.mojang.blaze3d.systems.GpuSurface;
56
import com.mojang.blaze3d.textures.GpuTextureView;
67
import com.mojang.blaze3d.vulkan.VulkanGpuSurface;
78
import me.flashyreese.mods.sodiumextra.client.util.MacReducedResolution;
@@ -11,6 +12,7 @@
1112
import org.spongepowered.asm.mixin.Shadow;
1213
import org.spongepowered.asm.mixin.injection.At;
1314
import org.spongepowered.asm.mixin.injection.Inject;
15+
import org.spongepowered.asm.mixin.injection.ModifyVariable;
1416
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1517

1618
@Mixin(VulkanGpuSurface.class)
@@ -21,6 +23,11 @@ public class MixinVulkanGpuSurface {
2123
@Shadow
2224
private int swapchainHeight;
2325

26+
@ModifyVariable(method = "configure", at = @At("HEAD"), argsOnly = true)
27+
private GpuSurface.Configuration reduceSwapchainConfiguration(GpuSurface.Configuration config) {
28+
return MacReducedResolution.reduceSurfaceConfig uration(config);
29+
}
30+
2431
@Inject(method = "blitFromTexture", at = @At(value = "INVOKE", target = "Lorg/lwjgl/vulkan/VK12;vkCmdBlitImage(Lorg/lwjgl/vulkan/VkCommandBuffer;JIJILorg/lwjgl/vulkan/VkImageBlit$Buffer;I)V", remap = false))
2532
private void scalePresentedTexture(CommandEncoderBackend commandEncoder, GpuTextureView textureView, CallbackInfo ci, @Local(ordinal = 0) VkImageBlit.Buffer blitRegion) {
2633
if (!MacReducedResolution.shouldScalePresentation(textureView.getWidth(0), textureView.getHeight(0), this.swapchainWidth, this.swapchainHeight)) {

common/src/main/java/me/flashyreese/mods/sodiumextra/mixin/reduce_resolution_on_mac/MixinWindow.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
*/
2222
@Mixin(Window.class)
2323
public class MixinWindow {
24+
@Shadow
25+
private int width;
26+
27+
@Shadow
28+
private int height;
29+
2430
@Shadow
2531
private int framebufferWidth;
2632

@@ -39,11 +45,13 @@ private void afterFramebufferResize(long handle, int newWidth, int newHeight, Ca
3945

4046
@Unique
4147
private void scaleFramebufferSize() {
42-
if (!MacReducedResolution.isEnabled()) {
48+
MacReducedResolution.rememberWindowSize(this.width, this.height);
49+
50+
if (!MacReducedResolution.shouldReduceFramebuffer(this.framebufferWidth, this.framebufferHeight, this.width, this.height)) {
4351
return;
4452
}
4553

46-
framebufferWidth = MacReducedResolution.reduce(framebufferWidth);
47-
framebufferHeight = MacReducedResolution.reduce(framebufferHeight);
54+
this.framebufferWidth = MacReducedResolution.reduce(this.framebufferWidth);
55+
this.framebufferHeight = MacReducedResolution.reduce(this.framebufferHeight);
4856
}
4957
}

0 commit comments

Comments
 (0)