Files
src
build.gradle
gradle.properties
manifest.kod
README.md
settings.gradle
Code
16 703
src/client/java/dev/kodari/resolution/client/ResolutionController.java
package dev.kodari.resolution.client;
import com.mojang.blaze3d.systems.RenderPass;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.textures.FilterMode;
import dev.kodari.resolution.ResolutionCalculator;
import java.util.OptionalInt;
import net.fabricmc.api.ClientModInitializer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.client.gl.RenderPipelines;
import net.minecraft.client.gl.SimpleFramebuffer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class ResolutionController implements ClientModInitializer {
private static final Logger LOGGER = LoggerFactory.getLogger("ultimate_resolution_controller");
private static Framebuffer target;
private static boolean active;
private static boolean failed;
private static String lastError = "";
private static ResolutionSettings.Snapshot lastWorking = ResolutionSettings.snapshot();
private static ResolutionSettings.Snapshot candidate = lastWorking;
@Override
public void onInitializeClient() {
ResolutionSettings.load();
candidate = ResolutionSettings.snapshot();
}
public static Framebuffer current(Framebuffer original) { return active && target != null ? target : original; }
public static boolean begin(MinecraftClient client) {
if (!ResolutionSettings.enabled || failed) {
if (target != null && !active) cleanup();
if (!failed) lastWorking = candidate;
return false;
}
if (active) return false;
Framebuffer display = client.getFramebuffer();
try {
int dw = display.textureWidth, dh = display.textureHeight;
int gpuLimit = RenderSystem.getDevice().getMaxTextureSize();
int limit = Math.min(gpuLimit, ResolutionSettings.maxDimension);
ResolutionCalculator.Limits limits = new ResolutionCalculator.Limits(limit, limit, gpuLimit);
ResolutionCalculator.Resolution nativeSize = new ResolutionCalculator.Resolution(dw, dh);
ResolutionCalculator.Resolution desired = calculate(nativeSize, limits);
int w = desired.width(), h = desired.height();
if (w == dw && h == dh) {
cleanup();
lastWorking = candidate;
return false;
}
if (target == null || target.textureWidth != w || target.textureHeight != h) {
Framebuffer replacement = new SimpleFramebuffer("Resolution Controller world", w, h, true);
Framebuffer old = target;
target = replacement;
if (old != null) old.delete();
}
RenderSystem.getDevice().createCommandEncoder().clearColorAndDepthTextures(target.getColorAttachment(), 0, target.getDepthAttachment(), 1.0);
active = true;
return true;
} catch (RuntimeException e) {
disable(e);
return false;
}
}
public static ResolutionCalculator.Resolution calculate(ResolutionCalculator.Resolution display, ResolutionCalculator.Limits limits) {
ResolutionSettings.Preset preset = ResolutionSettings.preset;
if (preset == ResolutionSettings.Preset.SUPER_SAMPLE_2K || preset == ResolutionSettings.Preset.SUPER_SAMPLE_4K) {
return ResolutionCalculator.widthPreservingAspect(display, preset == ResolutionSettings.Preset.SUPER_SAMPLE_2K ? 2560 : 3840, limits);
}
int percent = switch (preset) {
case ULTRA_PERFORMANCE -> 10;
case EXTREME_PERFORMANCE -> 15;
case PERFORMANCE -> 25;
case BALANCED -> 50;
case STANDARD -> 100;
case HIGH_QUALITY -> 125;
default -> ResolutionSettings.percent;
};
ResolutionCalculator.Resolution scaled = ResolutionCalculator.percentage(display, percent,
new ResolutionCalculator.Limits(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE));
int width = preset == ResolutionSettings.Preset.CUSTOM ? ResolutionSettings.width : 0;
int height = preset == ResolutionSettings.Preset.CUSTOM ? ResolutionSettings.height : 0;
int w = width > 0 ? width : scaled.width();
int h = height > 0 ? height : scaled.height();
if (width > 0 && height == 0) h = Math.max(1, (int) Math.round((double) w * display.height() / display.width()));
if (height > 0 && width == 0) w = Math.max(1, (int) Math.round((double) h * display.width() / display.height()));
return ResolutionCalculator.custom(w, h, limits);
}
public static void finish(MinecraftClient client, boolean rendered) {
active = false;
if (!rendered || target == null) return;
try {
Framebuffer display = client.getFramebuffer();
boolean up = target.textureWidth < display.textureWidth || target.textureHeight < display.textureHeight;
ResolutionSettings.Filter filter = up ? ResolutionSettings.upscale : ResolutionSettings.downscale;
try (RenderPass pass = RenderSystem.getDevice().createCommandEncoder().createRenderPass(() -> "Resolution Controller composite", display.getColorAttachmentView(), OptionalInt.empty())) {
pass.setPipeline(RenderPipelines.TRACY_BLIT);
RenderSystem.bindDefaultUniforms(pass);
pass.bindTexture("InSampler", target.getColorAttachmentView(), RenderSystem.getSamplerCache().get(filter == ResolutionSettings.Filter.LINEAR ? FilterMode.LINEAR : FilterMode.NEAREST));
pass.draw(0, 3);
}
lastWorking = candidate;
} catch (RuntimeException e) { disable(e); }
}
private static void disable(RuntimeException e) {
active = false;
failed = true;
lastError = e.toString();
LOGGER.error("Resolution rendering failed; restoring the last working settings and using native rendering until retried", e);
cleanup();
ResolutionSettings.restore(lastWorking);
candidate = lastWorking;
ResolutionSettings.save();
}
public static String lastError() { return lastError; }
public static void retry() {
candidate = ResolutionSettings.snapshot();
failed = false;
lastError = "";
}
public static void cleanup() {
active = false;
if (target != null) { target.delete(); target = null; }
}
}