2025-11-24 22:52:51 +03:00

126 lines
4.8 KiB
Java

/*
* Decompiled with CFR 0.152.
*
* Could not load the following classes:
* com.mojang.jtracy.TracyClient
*/
package com.mojang.blaze3d;
import com.mojang.blaze3d.buffers.GpuBuffer;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.systems.CommandEncoder;
import com.mojang.blaze3d.systems.GpuDevice;
import com.mojang.blaze3d.systems.RenderPass;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.textures.FilterMode;
import com.mojang.blaze3d.textures.GpuTexture;
import com.mojang.blaze3d.textures.GpuTextureView;
import com.mojang.blaze3d.textures.TextureFormat;
import com.mojang.jtracy.TracyClient;
import java.nio.ByteBuffer;
import java.util.OptionalInt;
import net.minecraft.client.renderer.RenderPipelines;
public class TracyFrameCapture
implements AutoCloseable {
private static final int MAX_WIDTH = 320;
private static final int MAX_HEIGHT = 180;
private static final int BYTES_PER_PIXEL = 4;
private int targetWidth;
private int targetHeight;
private int width = 320;
private int height = 180;
private GpuTexture frameBuffer;
private GpuTextureView frameBufferView;
private GpuBuffer pixelbuffer;
private int lastCaptureDelay;
private boolean capturedThisFrame;
private Status status = Status.WAITING_FOR_CAPTURE;
public TracyFrameCapture() {
GpuDevice device = RenderSystem.getDevice();
this.frameBuffer = device.createTexture("Tracy Frame Capture", 10, TextureFormat.RGBA8, this.width, this.height, 1, 1);
this.frameBufferView = device.createTextureView(this.frameBuffer);
this.pixelbuffer = device.createBuffer(() -> "Tracy Frame Capture buffer", 9, this.width * this.height * 4);
}
private void resize(int width, int height) {
float aspectRatio = (float)width / (float)height;
if (width > 320) {
width = 320;
height = (int)(320.0f / aspectRatio);
}
if (height > 180) {
width = (int)(180.0f * aspectRatio);
height = 180;
}
width = width / 4 * 4;
height = height / 4 * 4;
if (this.width != width || this.height != height) {
this.width = width;
this.height = height;
GpuDevice device = RenderSystem.getDevice();
this.frameBuffer.close();
this.frameBuffer = device.createTexture("Tracy Frame Capture", 10, TextureFormat.RGBA8, width, height, 1, 1);
this.frameBufferView.close();
this.frameBufferView = device.createTextureView(this.frameBuffer);
this.pixelbuffer.close();
this.pixelbuffer = device.createBuffer(() -> "Tracy Frame Capture buffer", 9, width * height * 4);
}
}
public void capture(RenderTarget captureTarget) {
if (this.status != Status.WAITING_FOR_CAPTURE || this.capturedThisFrame || captureTarget.getColorTexture() == null) {
return;
}
this.capturedThisFrame = true;
if (captureTarget.width != this.targetWidth || captureTarget.height != this.targetHeight) {
this.targetWidth = captureTarget.width;
this.targetHeight = captureTarget.height;
this.resize(this.targetWidth, this.targetHeight);
}
this.status = Status.WAITING_FOR_COPY;
CommandEncoder commandEncoder = RenderSystem.getDevice().createCommandEncoder();
try (RenderPass renderPass = RenderSystem.getDevice().createCommandEncoder().createRenderPass(() -> "Tracy blit", this.frameBufferView, OptionalInt.empty());){
renderPass.setPipeline(RenderPipelines.TRACY_BLIT);
renderPass.bindTexture("InSampler", captureTarget.getColorTextureView(), RenderSystem.getSamplerCache().getClampToEdge(FilterMode.LINEAR));
renderPass.draw(0, 3);
}
commandEncoder.copyTextureToBuffer(this.frameBuffer, this.pixelbuffer, 0, () -> {
this.status = Status.WAITING_FOR_UPLOAD;
}, 0);
this.lastCaptureDelay = 0;
}
public void upload() {
if (this.status != Status.WAITING_FOR_UPLOAD) {
return;
}
this.status = Status.WAITING_FOR_CAPTURE;
try (GpuBuffer.MappedView view = RenderSystem.getDevice().createCommandEncoder().mapBuffer(this.pixelbuffer, true, false);){
TracyClient.frameImage((ByteBuffer)view.data(), (int)this.width, (int)this.height, (int)this.lastCaptureDelay, (boolean)true);
}
}
public void endFrame() {
++this.lastCaptureDelay;
this.capturedThisFrame = false;
TracyClient.markFrame();
}
@Override
public void close() {
this.frameBuffer.close();
this.frameBufferView.close();
this.pixelbuffer.close();
}
static enum Status {
WAITING_FOR_CAPTURE,
WAITING_FOR_COPY,
WAITING_FOR_UPLOAD;
}
}