PictureInPictureRenderer .java

net.minecraft.client.gui.render.pip.PictureInPictureRenderer (class, 152 lines): members: textureIsReadyToBlit, blitTexture, submitNodeStorage, projection.

net.minecraft.client.gui.render.pipclass152 linesclientextends PictureInPictureRenderState>5.9 KB

Implements

API Surface (5)

void prepare(final T renderState, final GuiRenderState guiRenderState, final FeatureRenderDispatcher featureRenderDispatcher, final int guiScale)
void blitTexture(final T renderState, final GuiRenderState guiRenderState)
boolean textureIsReadyToBlit(final T renderState)
float getTranslateY(final int height, final int guiScale)
void close()

Fields & Constants (7)

texture
textureView
depthTexture
depthTextureView
projection
projectionMatrixBuffer
submitNodeStorage

String literals (4)

"PIP - "
"Picture in picture"
" texture"
" depth texture"

Full Source

PictureInPictureRenderer.java152 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.render.pip;
2
3
import com.mojang.blaze3d.ProjectionType;
4
import com.mojang.blaze3d.systems.RenderSystem;
5
import com.mojang.blaze3d.vertex.PoseStack;
6
import com.mojang.renderpearl.api.GpuFormat;
7
import com.mojang.renderpearl.api.commands.RenderPass;
8
import com.mojang.renderpearl.api.device.GpuDevice;
9
import com.mojang.renderpearl.api.textures.FilterMode;
10
import com.mojang.renderpearl.api.textures.GpuTexture;
11
import com.mojang.renderpearl.api.textures.GpuTextureView;
12
import java.util.Optional;
13
import java.util.OptionalDouble;
14
import net.fabricmc.api.EnvType;
15
import net.fabricmc.api.Environment;
16
import net.minecraft.client.gui.render.GuiRenderer;
17
import net.minecraft.client.gui.render.TextureSetup;
18
import net.minecraft.client.renderer.Projection;
19
import net.minecraft.client.renderer.ProjectionMatrixBuffer;
20
import net.minecraft.client.renderer.RenderPipelines;
21
import net.minecraft.client.renderer.SubmitNodeCollector;
22
import net.minecraft.client.renderer.SubmitNodeStorage;
23
import net.minecraft.client.renderer.feature.FeatureRenderDispatcher;
24
import net.minecraft.client.renderer.state.gui.BlitRenderState;
25
import net.minecraft.client.renderer.state.gui.GuiRenderState;
26
import net.minecraft.client.renderer.state.gui.pip.PictureInPictureRenderState;
27
import org.joml.Matrix4fStack;
28
import org.jspecify.annotations.Nullable;
29
30
@Environment(EnvType.CLIENT)
31
public abstract class PictureInPictureRenderer<T extends PictureInPictureRenderState> implements AutoCloseable {
32
	private @Nullable GpuTexture texture;
33
	private @Nullable GpuTextureView textureView;
34
	private @Nullable GpuTexture depthTexture;
35
	private @Nullable GpuTextureView depthTextureView;
36
	private final Projection projection = new Projection();
37
	private final ProjectionMatrixBuffer projectionMatrixBuffer = new ProjectionMatrixBuffer("PIP - " + this.getClass().getSimpleName());
38
	private final SubmitNodeStorage submitNodeStorage = new SubmitNodeStorage();
39
40
	public void prepare(final T renderState, final GuiRenderState guiRenderState, final FeatureRenderDispatcher featureRenderDispatcher, final int guiScale) {
41
		int width = (renderState.x1() - renderState.x0()) * guiScale;
42
		int height = (renderState.y1() - renderState.y0()) * guiScale;
43
		boolean needsAResize = this.texture == null || this.texture.getWidth(0) != width || this.texture.getHeight(0) != height;
44
		if (!needsAResize && this.textureIsReadyToBlit(renderState)) {
45
			this.blitTexture(renderState, guiRenderState);
46
		} else {
47
			this.prepareTexturesAndProjection(needsAResize, width, height);
48
			Matrix4fStack modelViewStack = RenderSystem.getModelViewStack();
49
			modelViewStack.pushMatrix();
50
			PoseStack poseStack = new PoseStack();
51
			poseStack.translate(width / 2.0F, this.getTranslateY(height, guiScale), 0.0F);
52
			float scale = guiScale * renderState.scale();
53
			poseStack.scale(scale, scale, -scale);
54
			this.renderToTexture(renderState, poseStack, this.submitNodeStorage);
55
56
			try (
57
				FeatureRenderDispatcher.PreparedFrame frame = featureRenderDispatcher.prepareFrame(this.submitNodeStorage);
58
				RenderPass renderPass = RenderSystem.getDevice()
59
					.createCommandEncoder()
60
					.createRenderPass(() -> "Picture in picture", this.textureView, Optional.empty(), this.depthTextureView, OptionalDouble.empty());
61
			) {
62
				RenderSystem.bindDefaultUniforms(renderPass);
63
				FeatureRenderDispatcher.renderAllFeatures(renderPass, frame);
64
			}
65
66
			modelViewStack.popMatrix();
67
			this.blitTexture(renderState, guiRenderState);
68
		}
69
	}
70
71
	protected void blitTexture(final T renderState, final GuiRenderState guiRenderState) {
72
		guiRenderState.addBlitToCurrentLayer(
73
			new BlitRenderState(
74
				RenderPipelines.GUI_TEXTURED_PREMULTIPLIED_ALPHA,
75
				TextureSetup.singleTexture(this.textureView, RenderSystem.getSamplerCache().getRepeat(FilterMode.NEAREST)),
76
				renderState.pose(),
77
				renderState.x0(),
78
				renderState.y0(),
79
				renderState.x1(),
80
				renderState.y1(),
81
				0.0F,
82
				1.0F,
83
				1.0F,
84
				0.0F,
85
				-1,
86
				renderState.scissorArea(),
87
				null
88
			)
89
		);
90
	}
91
92
	private void prepareTexturesAndProjection(final boolean needsAResize, final int width, final int height) {
93
		if (this.texture != null && needsAResize) {
94
			this.texture.close();
95
			this.texture = null;
96
			this.textureView.close();
97
			this.textureView = null;
98
			this.depthTexture.close();
99
			this.depthTexture = null;
100
			this.depthTextureView.close();
101
			this.depthTextureView = null;
102
		}
103
104
		GpuDevice device = RenderSystem.getDevice();
105
		if (this.texture == null) {
106
			this.texture = device.createTexture(() -> "UI " + this.getTextureLabel() + " texture", 13, GpuFormat.RGBA8_UNORM, width, height, 1, 1);
107
			this.textureView = device.createTextureView(this.texture);
108
			this.depthTexture = device.createTexture(() -> "UI " + this.getTextureLabel() + " depth texture", 9, GpuFormat.D32_FLOAT, width, height, 1, 1);
109
			this.depthTextureView = device.createTextureView(this.depthTexture);
110
		}
111
112
		device.createCommandEncoder().clearColorAndDepthTextures(this.texture, GuiRenderer.CLEAR_COLOR, this.depthTexture, 0.0);
113
		this.projection.setupOrtho(-1000.0F, 1000.0F, width, height, true);
114
		RenderSystem.setProjectionMatrix(this.projectionMatrixBuffer.getBuffer(this.projection), ProjectionType.ORTHOGRAPHIC);
115
	}
116
117
	protected boolean textureIsReadyToBlit(final T renderState) {
118
		return false;
119
	}
120
121
	protected float getTranslateY(final int height, final int guiScale) {
122
		return height;
123
	}
124
125
	@Override
126
	public void close() {
127
		if (this.texture != null) {
128
			this.texture.close();
129
		}
130
131
		if (this.textureView != null) {
132
			this.textureView.close();
133
		}
134
135
		if (this.depthTexture != null) {
136
			this.depthTexture.close();
137
		}
138
139
		if (this.depthTextureView != null) {
140
			this.depthTextureView.close();
141
		}
142
143
		this.projectionMatrixBuffer.close();
144
	}
145
146
	public abstract Class<T> getRenderStateClass();
147
148
	protected abstract void renderToTexture(T renderState, PoseStack poseStack, SubmitNodeCollector submitNodeCollector);
149
150
	protected abstract String getTextureLabel();
151
}
152

Decompiled Minecraft 26.3 source of PictureInPictureRenderer (net/minecraft/client/gui/render/pip/PictureInPictureRenderer.java) with Mojang official mappings, stored verbatim on the site.