TracyFrameCapture .java

com.mojang.blaze3d.TracyFrameCapture (class): members: capturedThisFrame, lastCaptureDelay, BYTES_PER_PIXEL, frameBufferView, targetHeight, frameBuffer.

com.mojang.blaze3dclass126 linesclient4.5 KB

Implements

API Surface (4)

void capture(final RenderTarget captureTarget)
void upload()
void endFrame()
void close()

Fields & Constants (13)

MAX_WIDTH
MAX_HEIGHT
BYTES_PER_PIXEL
targetWidth
targetHeight
width
height
frameBuffer
frameBufferView
pixelbuffer
lastCaptureDelay
capturedThisFrame
status

String literals (4)

"Tracy Frame Capture"
"Tracy Frame Capture buffer"
"Tracy blit"
"InSampler"

Full Source

TracyFrameCapture.java126 lines · Minecraft 26.3 · verbatim
1
package com.mojang.blaze3d;
2
3
import com.mojang.blaze3d.pipeline.RenderTarget;
4
import com.mojang.blaze3d.systems.RenderSystem;
5
import com.mojang.jtracy.TracyClient;
6
import com.mojang.renderpearl.api.GpuFormat;
7
import com.mojang.renderpearl.api.buffers.GpuBuffer;
8
import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
9
import com.mojang.renderpearl.api.commands.CommandEncoder;
10
import com.mojang.renderpearl.api.commands.RenderPass;
11
import com.mojang.renderpearl.api.device.GpuDevice;
12
import com.mojang.renderpearl.api.textures.FilterMode;
13
import com.mojang.renderpearl.api.textures.GpuTexture;
14
import com.mojang.renderpearl.api.textures.GpuTextureView;
15
import java.util.Optional;
16
import net.fabricmc.api.EnvType;
17
import net.fabricmc.api.Environment;
18
import net.minecraft.client.renderer.RenderPipelines;
19
20
@Environment(EnvType.CLIENT)
21
public class TracyFrameCapture implements AutoCloseable {
22
	private static final int MAX_WIDTH = 320;
23
	private static final int MAX_HEIGHT = 180;
24
	private static final long BYTES_PER_PIXEL = 4L;
25
	private int targetWidth;
26
	private int targetHeight;
27
	private int width;
28
	private int height;
29
	private GpuTexture frameBuffer;
30
	private GpuTextureView frameBufferView;
31
	private GpuBuffer pixelbuffer;
32
	private int lastCaptureDelay;
33
	private boolean capturedThisFrame;
34
	private TracyFrameCapture.Status status = TracyFrameCapture.Status.WAITING_FOR_CAPTURE;
35
36
	public TracyFrameCapture() {
37
		this.width = 320;
38
		this.height = 180;
39
		GpuDevice device = RenderSystem.getDevice();
40
		this.frameBuffer = device.createTexture("Tracy Frame Capture", 10, GpuFormat.RGBA8_UNORM, this.width, this.height, 1, 1);
41
		this.frameBufferView = device.createTextureView(this.frameBuffer);
42
		this.pixelbuffer = device.createBuffer(() -> "Tracy Frame Capture buffer", 9, this.width * this.height * 4L);
43
	}
44
45
	private void resize(int width, int height) {
46
		float aspectRatio = (float)width / height;
47
		if (width > 320) {
48
			width = 320;
49
			height = (int)(320.0F / aspectRatio);
50
		}
51
52
		if (height > 180) {
53
			width = (int)(180.0F * aspectRatio);
54
			height = 180;
55
		}
56
57
		width = width / 4 * 4;
58
		height = height / 4 * 4;
59
		if (this.width != width || this.height != height) {
60
			this.width = width;
61
			this.height = height;
62
			GpuDevice device = RenderSystem.getDevice();
63
			this.frameBuffer.close();
64
			this.frameBuffer = device.createTexture("Tracy Frame Capture", 10, GpuFormat.RGBA8_UNORM, width, height, 1, 1);
65
			this.frameBufferView.close();
66
			this.frameBufferView = device.createTextureView(this.frameBuffer);
67
			this.pixelbuffer.close();
68
			this.pixelbuffer = device.createBuffer(() -> "Tracy Frame Capture buffer", 9, width * height * 4L);
69
		}
70
	}
71
72
	public void capture(final RenderTarget captureTarget) {
73
		if (this.status == TracyFrameCapture.Status.WAITING_FOR_CAPTURE && !this.capturedThisFrame && captureTarget.getColorTexture() != null) {
74
			this.capturedThisFrame = true;
75
			if (captureTarget.width != this.targetWidth || captureTarget.height != this.targetHeight) {
76
				this.targetWidth = captureTarget.width;
77
				this.targetHeight = captureTarget.height;
78
				this.resize(this.targetWidth, this.targetHeight);
79
			}
80
81
			this.status = TracyFrameCapture.Status.WAITING_FOR_COPY;
82
			CommandEncoder commandEncoder = RenderSystem.getDevice().createCommandEncoder();
83
84
			try (RenderPass renderPass = RenderSystem.getDevice().createCommandEncoder().createRenderPass(() -> "Tracy blit", this.frameBufferView, Optional.empty())) {
85
				RenderSystem.bindDefaultUniforms(renderPass);
86
				renderPass.setPipeline(RenderSystem.getCompiledPipeline(RenderPipelines.TRACY_BLIT));
87
				renderPass.setUniform("InSampler", captureTarget.getColorTextureView(), RenderSystem.getSamplerCache().getClampToEdge(FilterMode.LINEAR));
88
				renderPass.draw(3, 1, 0, 0);
89
			}
90
91
			commandEncoder.copyTextureToBuffer(this.frameBuffer, this.pixelbuffer, 0L, () -> this.status = TracyFrameCapture.Status.WAITING_FOR_UPLOAD, 0);
92
			this.lastCaptureDelay = 0;
93
		}
94
	}
95
96
	public void upload() {
97
		if (this.status == TracyFrameCapture.Status.WAITING_FOR_UPLOAD) {
98
			this.status = TracyFrameCapture.Status.WAITING_FOR_CAPTURE;
99
100
			try (GpuBufferSlice.MappedView view = this.pixelbuffer.map(true, false)) {
101
				TracyClient.frameImage(view.data(), this.width, this.height, this.lastCaptureDelay, true);
102
			}
103
		}
104
	}
105
106
	public void endFrame() {
107
		this.lastCaptureDelay++;
108
		this.capturedThisFrame = false;
109
		TracyClient.markFrame();
110
	}
111
112
	@Override
113
	public void close() {
114
		this.frameBuffer.close();
115
		this.frameBufferView.close();
116
		this.pixelbuffer.close();
117
	}
118
119
	@Environment(EnvType.CLIENT)
120
	private enum Status {
121
		WAITING_FOR_CAPTURE,
122
		WAITING_FOR_COPY,
123
		WAITING_FOR_UPLOAD;
124
	}
125
}
126

Decompiled Minecraft 26.3 source of TracyFrameCapture (com/mojang/blaze3d/TracyFrameCapture.java) with Mojang official mappings, stored verbatim on the site.