Update to 1.21.5

This commit is contained in:
Jonas Herzig
2025-04-27 13:04:28 +02:00
parent 7aad4dcfd9
commit a8c1d19240
26 changed files with 372 additions and 37 deletions

View File

@@ -23,6 +23,13 @@ import static com.replaymod.core.versions.MCVer.resizeMainWindow;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
//#if MC>=12105
//$$ import com.mojang.blaze3d.buffers.BufferType;
//$$ import com.mojang.blaze3d.buffers.BufferUsage;
//$$ import com.mojang.blaze3d.buffers.GpuBuffer;
//$$ import com.mojang.blaze3d.systems.GpuDevice;
//#endif
public abstract class OpenGlFrameCapturer<F extends Frame, D extends CaptureData> implements FrameCapturer<F> {
protected final WorldRenderer worldRenderer;
protected final RenderInfo renderInfo;
@@ -81,20 +88,30 @@ public abstract class OpenGlFrameCapturer<F extends Frame, D extends CaptureData
resizeMainWindow(mc, getFrameWidth(), getFrameHeight());
pushMatrix();
//#if MC<12105
frameBuffer().beginWrite(true);
//#endif
//#if MC>=12105
//$$ RenderSystem.getDevice()
//$$ .createCommandEncoder()
//$$ .clearColorAndDepthTextures(mc.getFramebuffer().getColorAttachment(), 0, mc.getFramebuffer().getDepthAttachment(), 1);
//#else
GlStateManager.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
//#if MC>=11400 && MC<12102
, false
//#endif
);
//#endif
//#if MC<11904
GlStateManager.enableTexture();
//#endif
worldRenderer.renderWorld(partialTicks, captureData);
//#if MC<12105
frameBuffer().endWrite();
//#endif
popMatrix();
return captureFrame(frameId, captureData);
@@ -102,9 +119,19 @@ public abstract class OpenGlFrameCapturer<F extends Frame, D extends CaptureData
protected OpenGlFrame captureFrame(int frameId, D captureData) {
ByteBuffer buffer = ByteBufferPool.allocate(getFrameWidth() * getFrameHeight() * 4);
//#if MC>=12105
//$$ GpuDevice device = RenderSystem.getDevice();
//$$ try (GpuBuffer gpuBuffer = device.createBuffer(null, BufferType.PIXEL_PACK, BufferUsage.STATIC_READ, getFrameWidth() * getFrameHeight() * 4)) {
//$$ device.createCommandEncoder().copyTextureToBuffer(frameBuffer().getColorAttachment(), gpuBuffer, 0, () -> {}, 0);
//$$ try (GpuBuffer.ReadView view = device.createCommandEncoder().readBuffer(gpuBuffer)) {
//$$ buffer.put(view.data());
//$$ }
//$$ }
//#else
frameBuffer().beginWrite(true);
GL11.glReadPixels(0, 0, getFrameWidth(), getFrameHeight(), GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE, buffer);
frameBuffer().endWrite();
//#endif
buffer.rewind();
return new OpenGlFrame(frameId, new Dimension(getFrameWidth(), getFrameHeight()), 4, buffer);

View File

@@ -1,6 +1,5 @@
package com.replaymod.render.capturer;
import com.mojang.blaze3d.platform.GlStateManager;
import com.replaymod.render.frame.OpenGlFrame;
import com.replaymod.render.rendering.Channel;
import com.replaymod.render.rendering.Frame;
@@ -14,11 +13,24 @@ import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
//#if MC>=12105
//$$ import com.mojang.blaze3d.buffers.BufferType;
//$$ import com.mojang.blaze3d.buffers.BufferUsage;
//$$ import com.mojang.blaze3d.buffers.GpuBuffer;
//$$ import com.mojang.blaze3d.systems.CommandEncoder;
//$$ import com.mojang.blaze3d.systems.GpuDevice;
//$$ import com.mojang.blaze3d.systems.RenderSystem;
//#endif
public abstract class PboOpenGlFrameCapturer<F extends Frame, D extends Enum<D> & CaptureData>
extends OpenGlFrameCapturer<F, D> {
private final boolean withDepth;
private final D[] data;
//#if MC>=12105
//$$ private GpuBuffer pbo, otherPBO;
//#else
private PixelBufferObject pbo, otherPBO;
//#endif
public PboOpenGlFrameCapturer(WorldRenderer worldRenderer, RenderInfo renderInfo, Class<D> type, int framePixels) {
super(worldRenderer, renderInfo);
@@ -26,14 +38,23 @@ public abstract class PboOpenGlFrameCapturer<F extends Frame, D extends Enum<D>
withDepth = renderInfo.getRenderSettings().isDepthMap();
data = type.getEnumConstants();
int bufferSize = framePixels * (4 /* bgra */ + (withDepth ? 4 /* float */ : 0)) * data.length;
//#if MC>=12105
//$$ pbo = RenderSystem.getDevice().createBuffer(null, BufferType.PIXEL_PACK, BufferUsage.STREAM_READ, bufferSize);
//$$ otherPBO = RenderSystem.getDevice().createBuffer(null, BufferType.PIXEL_PACK, BufferUsage.STREAM_READ, bufferSize);
//#else
pbo = new PixelBufferObject(bufferSize, PixelBufferObject.Usage.READ);
otherPBO = new PixelBufferObject(bufferSize, PixelBufferObject.Usage.READ);
//#endif
}
protected abstract F create(OpenGlFrame[] from);
private void swapPBOs() {
//#if MC>=12105
//$$ GpuBuffer old = pbo;
//#else
PixelBufferObject old = pbo;
//#endif
pbo = otherPBO;
otherPBO = old;
}
@@ -43,13 +64,26 @@ public abstract class PboOpenGlFrameCapturer<F extends Frame, D extends Enum<D>
return framesDone >= renderInfo.getTotalFrames() + 2;
}
private F readFromPbo(ByteBuffer pboBuffer, int bytesPerPixel) {
private F readFromPbo(ByteBuffer pboBuffer, int bytesPerPixel, boolean swapRB) {
OpenGlFrame[] frames = new OpenGlFrame[data.length];
int frameBufferSize = getFrameWidth() * getFrameHeight() * bytesPerPixel;
for (int i = 0; i < frames.length; i++) {
ByteBuffer frameBuffer = ByteBufferPool.allocate(frameBufferSize);
pboBuffer.limit(pboBuffer.position() + frameBufferSize);
frameBuffer.put(pboBuffer);
if (swapRB) {
for (int j = 0; j < frameBufferSize; j += 4) {
byte r = pboBuffer.get();
byte g = pboBuffer.get();
byte b = pboBuffer.get();
byte a = pboBuffer.get();
frameBuffer.put(b);
frameBuffer.put(g);
frameBuffer.put(r);
frameBuffer.put(a);
}
} else {
frameBuffer.put(pboBuffer);
}
frameBuffer.rewind();
frames[i] = new OpenGlFrame(framesDone - 2, frameSize, bytesPerPixel, frameBuffer);
}
@@ -62,17 +96,27 @@ public abstract class PboOpenGlFrameCapturer<F extends Frame, D extends Enum<D>
if (framesDone > 1) {
// Read pbo to memory
//#if MC>=12105
//$$ try (GpuBuffer.ReadView view = RenderSystem.getDevice().createCommandEncoder().readBuffer(pbo)) {
//$$ channels = new HashMap<>();
//$$ channels.put(Channel.BRGA, readFromPbo(view.data(), 4, true));
//$$ if (withDepth) {
//$$ channels.put(Channel.DEPTH, readFromPbo(view.data(), 4, false));
//$$ }
//$$ }
//#else
pbo.bind();
ByteBuffer pboBuffer = pbo.mapReadOnly();
channels = new HashMap<>();
channels.put(Channel.BRGA, readFromPbo(pboBuffer, 4));
channels.put(Channel.BRGA, readFromPbo(pboBuffer, 4, false));
if (withDepth) {
channels.put(Channel.DEPTH, readFromPbo(pboBuffer, 4));
channels.put(Channel.DEPTH, readFromPbo(pboBuffer, 4, false));
}
pbo.unmap();
pbo.unbind();
//#endif
}
if (framesDone < renderInfo.getTotalFrames()) {
@@ -90,6 +134,15 @@ public abstract class PboOpenGlFrameCapturer<F extends Frame, D extends Enum<D>
@Override
protected OpenGlFrame captureFrame(int frameId, D captureData) {
//#if MC>=12105
//$$ int offset = captureData.ordinal() * getFrameWidth() * getFrameHeight() * 4;
//$$ CommandEncoder cmd = RenderSystem.getDevice().createCommandEncoder();
//$$ cmd.copyTextureToBuffer(frameBuffer().getColorAttachment(), pbo, offset, () -> {}, 0);
//$$ if (withDepth) {
//$$ offset += data.length * getFrameWidth() * getFrameHeight() * 4;
//$$ cmd.copyTextureToBuffer(frameBuffer().getDepthAttachment(), pbo, offset, () -> {}, 0);
//$$ }
//#else
pbo.bind();
int offset = captureData.ordinal() * getFrameWidth() * getFrameHeight() * 4;
@@ -102,13 +155,14 @@ public abstract class PboOpenGlFrameCapturer<F extends Frame, D extends Enum<D>
frameBuffer().endWrite();
pbo.unbind();
//#endif
return null;
}
@Override
public void close() throws IOException {
super.close();
pbo.delete();
otherPBO.delete();
pbo.close();
otherPBO.close();
}
}

View File

@@ -240,7 +240,9 @@ public class GuiVideoRenderer extends GuiScreen implements Tickable {
final int videoHeight = videoSize.getHeight();
if (previewTexture == null) {
//#if MC>=11400
//#if MC>=12105
//$$ previewTexture = new NativeImageBackedTexture((String) null, videoWidth, videoHeight, true);
//#elseif MC>=11400
previewTexture = new NativeImageBackedTexture(videoWidth, videoHeight, true);
//#else
//$$ previewTexture = new DynamicTexture(videoWidth, videoHeight);
@@ -252,7 +254,11 @@ public class GuiVideoRenderer extends GuiScreen implements Tickable {
previewTextureDirty = false;
}
//#if MC>=12105
//$$ guiRenderer.bindTexture(previewTexture.getGlTexture());
//#else
guiRenderer.bindTexture(previewTexture.getGlId());
//#endif
renderPreviewTexture(guiRenderer, size, videoWidth, videoHeight);
}

View File

@@ -69,16 +69,24 @@ public class VirtualWindow implements Closeable {
public void beginWrite() {
MinecraftClientExt.get(mc).setFramebufferDelegate(guiFramebuffer);
//#if MC<12105
guiFramebuffer.beginWrite(true);
//#endif
}
public void endWrite() {
//#if MC<12105
guiFramebuffer.endWrite();
//#endif
MinecraftClientExt.get(mc).setFramebufferDelegate(null);
}
public void flip() {
//#if MC>=12105
//$$ guiFramebuffer.blitToScreen();
//#else
guiFramebuffer.draw(framebufferWidth, framebufferHeight);
//#endif
//#if MC>=12102
//$$ window.swapBuffers(null);

View File

@@ -21,6 +21,12 @@ import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//#if MC>=12105
//$$ import org.spongepowered.asm.mixin.Mutable;
//$$ import java.util.AbstractQueue;
//$$ import java.util.Iterator;
//#endif
//#if MC>=12102
//$$ import net.minecraft.util.thread.SimpleConsecutiveExecutor;
//#endif
@@ -68,6 +74,9 @@ public abstract class Mixin_BlockOnChunkRebuilds implements ForceChunkLoadingHoo
@Shadow protected abstract void scheduleRunTasks();
//#if MC>=12105
//$$ @Mutable
//#endif
@Shadow @Final private Queue<Runnable> uploadQueue;
private final Lock waitingForWorkLock = new ReentrantLock();
private final Condition newWork = waitingForWorkLock.newCondition();
@@ -96,6 +105,29 @@ public abstract class Mixin_BlockOnChunkRebuilds implements ForceChunkLoadingHoo
}
}
//#if MC>=12105
//$$ @Inject(method = "<init>", at = @At("RETURN"))
//$$ private void notifyMainThreadOfNewUpload(CallbackInfo ci) {
//$$ Queue<Runnable> inner = this.uploadQueue;
//$$ this.uploadQueue = new AbstractQueue<>() {
//$$ @Override
//$$ public boolean offer(Runnable runnable) {
//$$ boolean result = inner.offer(runnable);
//$$ waitingForWorkLock.lock();
//$$ try {
//$$ newWork.signal();
//$$ } finally {
//$$ waitingForWorkLock.unlock();
//$$ }
//$$ return result;
//$$ }
//$$ @Override public Runnable poll() { return inner.poll(); }
//$$ @Override public Runnable peek() { return inner.peek(); }
//$$ @Override public int size() { return inner.size(); }
//$$ @Override public Iterator<Runnable> iterator() { return inner.iterator(); }
//$$ };
//$$ }
//#else
@Inject(method = "scheduleUpload", at = @At("RETURN"))
private void notifyMainThreadOfNewUpload(CallbackInfoReturnable<CompletableFuture<Void>> ci) {
this.waitingForWorkLock.lock();
@@ -105,6 +137,7 @@ public abstract class Mixin_BlockOnChunkRebuilds implements ForceChunkLoadingHoo
this.waitingForWorkLock.unlock();
}
}
//#endif
private boolean waitForMainThreadWork() {
//#if MC>=12102
@@ -125,6 +158,7 @@ public abstract class Mixin_BlockOnChunkRebuilds implements ForceChunkLoadingHoo
this.waitingForWorkLock.lock();
try {
while (true) {
//#if MC<11900
// Now, what is this call doing here you might be wondering. Well, from a quick look over everything
// it does not look like it would be required but have a **very** close look at [scheduleUpload]:
// It is not actually guaranteed to run the upload on the main thread, it just looks like it (and
@@ -138,6 +172,7 @@ public abstract class Mixin_BlockOnChunkRebuilds implements ForceChunkLoadingHoo
// dead-lock ourselves here (since the upload queue is already empty), if we did never do this call
// to run the upload scheduled via this particular path of code execution.
RenderSystem.replayQueue();
//#endif
if (this.allDone) {
return true;

View File

@@ -49,12 +49,19 @@ public abstract class Mixin_ChromaKeyColorSky {
if (handler != null) {
ReadableColor color = handler.getSettings().getChromaKeyingColor();
if (color != null) {
//#if MC>=12105
//$$ RenderSystem.getDevice().createCommandEncoder().clearColorTexture(
//$$ this.client.getFramebuffer().getColorAttachment(),
//$$ (0xff << 24) | (color.getRed() << 16) | (color.getGreen() << 8) | color.getBlue()
//$$ );
//#else
GlStateManager.clearColor(color.getRed() / 255f, color.getGreen() / 255f, color.getBlue() / 255f, 1);
GlStateManager.clear(GL11.GL_COLOR_BUFFER_BIT
//#if MC>=11400 && MC<12102
, false
//#endif
);
//#endif
ci.cancel();
}
}

View File

@@ -495,22 +495,34 @@ public class VideoRenderer implements RenderInfo {
}
pushMatrix();
//#if MC>=12105
//$$ RenderSystem.getDevice()
//$$ .createCommandEncoder()
//$$ .clearColorAndDepthTextures(mc.getFramebuffer().getColorAttachment(), 0, mc.getFramebuffer().getDepthAttachment(), 1);
//#else
GlStateManager.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
//#if MC>=11400 && MC<12102
, false
//#endif
);
//#endif
//#if MC<11904
GlStateManager.enableTexture();
//#endif
guiWindow.beginWrite();
//#if MC>=11500
//#if MC>=12105
//$$ RenderSystem.getDevice()
//$$ .createCommandEncoder()
//$$ .clearColorAndDepthTextures(mc.getFramebuffer().getColorAttachment(), 0, mc.getFramebuffer().getDepthAttachment(), 1);
//#else
//#if MC>=12102
//$$ RenderSystem.clear(256);
//#else
RenderSystem.clear(256, MinecraftClient.IS_SYSTEM_MAC);
//#endif
//#endif
//#if MC>=11700
//$$ RenderSystem.setProjectionMatrix(Matrix4f.projectionMatrix(
//$$ 0,

View File

@@ -23,7 +23,7 @@ import static org.lwjgl.opengl.GL21.GL_PIXEL_PACK_BUFFER;
import static org.lwjgl.opengl.ARBVertexBufferObject.*;
//#endif
public class PixelBufferObject {
public class PixelBufferObject implements AutoCloseable {
public enum Usage {
COPY(GL_STREAM_COPY_ARB, GL_STREAM_COPY),
DRAW(GL_STREAM_DRAW_ARB, GL_STREAM_DRAW),
@@ -207,7 +207,8 @@ public class PixelBufferObject {
mapped.set(0);
}
public void delete() {
@Override
public void close() {
if (handle != -1) {
if (arb) {
//#if MC>=11400
@@ -227,7 +228,7 @@ public class PixelBufferObject {
super.finalize();
if (handle != -1) {
LogManager.getLogger().warn("PBO garbage collected before deleted!");
ReplayMod.instance.runLater(this::delete);
ReplayMod.instance.runLater(this::close);
}
}
}