VulkanRenderPass .java

com.mojang.renderpearl.backend.vulkan.VulkanRenderPass (class): members: anyDescriptorDirty, outputHeight, outputWidth, checkpointStorage, commandBuffer.

com.mojang.renderpearl.backend.vulkanclass317 linesclient12.4 KB

Implements

API Surface (19)

void pushDebugGroup(final Supplier<String> label)
void popDebugGroup()
void setPipeline(final BackendRenderPipeline pipeline)
void setUniform(final int index, final @Nullable Object value)
void pushConstants(final ByteBuffer value)
void enableScissor(final int x, final int y, final int width, final int height)
void disableScissor()
void setVertexBuffer(final int slot, final @Nullable GpuBufferSlice vertexBuffer)
void setIndexBuffer(final GpuBuffer indexBuffer, final IndexType indexType)
void drawIndexed(final int indexCount, final int instanceCount, final int firstIndex, final int vertexOffset, final int firstInstance)
void multiDrawIndexed(final IntBuffer drawParameters, final int instanceCount, final int firstInstance, final int drawCount)
void multiDrawIndexed(final PointerBuffer firstIndexOffsets, final IntBuffer indexCounts, final IntBuffer vertexOffsets, final int drawCount)
void drawIndexedIndirect(final GpuBufferSlice commands, final int drawCount)
void draw(final int vertexCount, final int instanceCount, final int firstVertex, final int firstInstance)
void multiDraw(final IntBuffer drawParameters, final int instanceCount, final int firstInstance, final int drawCount)
void multiDraw(final IntBuffer firstVertices, final IntBuffer vertexCounts, final int drawCount)
void drawIndirect(final GpuBufferSlice commands, final int drawCount)
void writeTimestamp(final GpuQueryPool pool, final int index)
Supplier<String> getLabel()

Fields & Constants (13)

device
encoder
checkpointStorage
renderArea
outputWidth
outputHeight
hasDepth
label
pushedDebugGroups
commandBuffer
pipeline
anyDescriptorDirty
uniforms

String literals (5)

"Can't pop more debug groups than was pushed!"
"Missing uniform "
" (should be "
"Missing sampler "
"Couldn't create buffer view for texel buffer"

Full Source

VulkanRenderPass.java317 lines · Minecraft 26.3 · verbatim
1
package com.mojang.renderpearl.backend.vulkan;
2
3
import com.mojang.renderpearl.api.buffers.GpuBuffer;
4
import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
5
import com.mojang.renderpearl.api.commands.GpuQueryPool;
6
import com.mojang.renderpearl.api.commands.RenderPass;
7
import com.mojang.renderpearl.api.pipeline.BindGroupLayout;
8
import com.mojang.renderpearl.api.pipeline.IndexType;
9
import com.mojang.renderpearl.api.pipeline.UniformType;
10
import com.mojang.renderpearl.backend.api.BackendRenderPipeline;
11
import com.mojang.renderpearl.backend.api.RenderPassBackend;
12
import com.mojang.renderpearl.backend.vulkan.checkpoints.CheckpointExtension;
13
import com.mojang.renderpearl.util.TextureViewAndSampler;
14
import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
15
import it.unimi.dsi.fastutil.objects.ReferenceList;
16
import java.nio.ByteBuffer;
17
import java.nio.IntBuffer;
18
import java.nio.LongBuffer;
19
import java.util.List;
20
import java.util.function.Supplier;
21
import net.fabricmc.api.EnvType;
22
import net.fabricmc.api.Environment;
23
import org.jspecify.annotations.Nullable;
24
import org.lwjgl.PointerBuffer;
25
import org.lwjgl.system.MemoryStack;
26
import org.lwjgl.system.MemoryUtil;
27
import org.lwjgl.vulkan.EXTMultiDraw;
28
import org.lwjgl.vulkan.KHRPushDescriptor;
29
import org.lwjgl.vulkan.KHRSynchronization2;
30
import org.lwjgl.vulkan.VK12;
31
import org.lwjgl.vulkan.VkBufferViewCreateInfo;
32
import org.lwjgl.vulkan.VkCommandBuffer;
33
import org.lwjgl.vulkan.VkDescriptorBufferInfo;
34
import org.lwjgl.vulkan.VkDescriptorImageInfo;
35
import org.lwjgl.vulkan.VkDrawIndexedIndirectCommand;
36
import org.lwjgl.vulkan.VkDrawIndirectCommand;
37
import org.lwjgl.vulkan.VkMultiDrawIndexedInfoEXT;
38
import org.lwjgl.vulkan.VkMultiDrawInfoEXT;
39
import org.lwjgl.vulkan.VkRect2D;
40
import org.lwjgl.vulkan.VkViewport;
41
import org.lwjgl.vulkan.VkWriteDescriptorSet;
42
import org.lwjgl.vulkan.VkViewport.Buffer;
43
44
@Environment(EnvType.CLIENT)
45
public class VulkanRenderPass implements RenderPassBackend {
46
	private final VulkanDevice device;
47
	private final VulkanCommandEncoder encoder;
48
	private final CheckpointExtension.CheckpointStorage checkpointStorage;
49
	private final RenderPass.@Nullable RenderArea renderArea;
50
	private final int outputWidth;
51
	private final int outputHeight;
52
	private final boolean hasDepth;
53
	private final Supplier<String> label;
54
	protected int pushedDebugGroups = 0;
55
	private final VkCommandBuffer commandBuffer;
56
	protected @Nullable VulkanRenderPipeline pipeline;
57
	private boolean anyDescriptorDirty = false;
58
	protected final ReferenceList<@Nullable Object> uniforms = new ReferenceArrayList<>();
59
60
	public VulkanRenderPass(
61
		final VulkanDevice device,
62
		final VulkanCommandEncoder encoder,
63
		final VkCommandBuffer commandBuffer,
64
		final CheckpointExtension.CheckpointStorage checkpointStorage,
65
		final RenderPass.RenderArea renderArea,
66
		final int outputWidth,
67
		final int outputHeight,
68
		final boolean hasDepth,
69
		final Supplier<String> label
70
	) {
71
		this.device = device;
72
		this.encoder = encoder;
73
		this.commandBuffer = commandBuffer;
74
		this.checkpointStorage = checkpointStorage;
75
		this.renderArea = renderArea;
76
		this.outputWidth = outputWidth;
77
		this.outputHeight = outputHeight;
78
		this.hasDepth = hasDepth;
79
		this.label = label;
80
81
		try (MemoryStack stack = MemoryStack.stackPush()) {
82
			Buffer viewport = VkViewport.calloc(1, stack);
83
			viewport.x(0.0F);
84
			viewport.y(0.0F);
85
			viewport.width(outputWidth);
86
			viewport.height(outputHeight);
87
			viewport.minDepth(0.0F);
88
			viewport.maxDepth(1.0F);
89
			VK12.vkCmdSetViewport(this.commandBuffer(), 0, viewport);
90
			setScissor(stack, this.commandBuffer(), renderArea.x(), renderArea.y(), renderArea.width(), renderArea.height());
91
		}
92
	}
93
94
	private VkCommandBuffer commandBuffer() {
95
		return this.commandBuffer;
96
	}
97
98
	@Override
99
	public void pushDebugGroup(final Supplier<String> label) {
100
		this.pushedDebugGroups++;
101
		this.device.instance().debug().beginDebugGroup(this.commandBuffer(), label);
102
	}
103
104
	@Override
105
	public void popDebugGroup() {
106
		if (this.pushedDebugGroups == 0) {
107
			throw new IllegalStateException("Can't pop more debug groups than was pushed!");
108
		}
109
110
		this.pushedDebugGroups--;
111
		this.device.instance().debug().endDebugGroup(this.commandBuffer());
112
	}
113
114
	@Override
115
	public void setPipeline(final BackendRenderPipeline pipeline) {
116
		if (pipeline instanceof VulkanRenderPipeline vulkanRenderPipeline) {
117
			this.pipeline = vulkanRenderPipeline;
118
			this.anyDescriptorDirty = true;
119
			this.uniforms.clear();
120
			this.uniforms.size(vulkanRenderPipeline.uniforms().size());
121
			VK12.vkCmdBindPipeline(this.commandBuffer(), 0, this.hasDepth ? this.pipeline.withDepthPipeline() : this.pipeline.withoutDepthPipeline());
122
		} else {
123
			throw new IllegalArgumentException("Pipeline must be instance of VulkanRenderPipeline");
124
		}
125
	}
126
127
	@Override
128
	public void setUniform(final int index, final @Nullable Object value) {
129
		this.uniforms.set(index, value);
130
		this.anyDescriptorDirty = true;
131
	}
132
133
	@Override
134
	public void pushConstants(final ByteBuffer value) {
135
		assert this.pipeline != null;
136
		VK12.vkCmdPushConstants(this.commandBuffer(), this.pipeline.pipelineLayout(), Integer.MAX_VALUE, 0, value);
137
	}
138
139
	@Override
140
	public void enableScissor(final int x, final int y, final int width, final int height) {
141
		try (MemoryStack stack = MemoryStack.stackPush()) {
142
			setScissor(stack, this.commandBuffer(), x, y, width, height);
143
		}
144
	}
145
146
	private static void setScissor(final MemoryStack stack, final VkCommandBuffer commandBuffer, final int x, final int y, final int width, final int height) {
147
		org.lwjgl.vulkan.VkRect2D.Buffer scissor = VkRect2D.calloc(1, stack);
148
		scissor.offset().set(x, y);
149
		scissor.extent().set(width, height);
150
		VK12.vkCmdSetScissor(commandBuffer, 0, scissor);
151
	}
152
153
	@Override
154
	public void disableScissor() {
155
		if (this.renderArea != null) {
156
			this.enableScissor(this.renderArea.x(), this.renderArea.y(), this.renderArea.width(), this.renderArea.height());
157
		} else {
158
			this.enableScissor(0, 0, this.outputWidth, this.outputHeight);
159
		}
160
	}
161
162
	@Override
163
	public void setVertexBuffer(final int slot, final @Nullable GpuBufferSlice vertexBuffer) {
164
		if (vertexBuffer != null) {
165
			try (MemoryStack stack = MemoryStack.stackPush()) {
166
				long buffer = ((VulkanGpuBuffer)vertexBuffer.buffer()).vkBuffer();
167
				long offset = vertexBuffer.offset();
168
				VK12.vkCmdBindVertexBuffers(this.commandBuffer(), slot, stack.longs(buffer), stack.longs(offset));
169
			}
170
		}
171
	}
172
173
	@Override
174
	public void setIndexBuffer(final GpuBuffer indexBuffer, final IndexType indexType) {
175
		int type = switch (indexType) {
176
			case SHORT -> 0;
177
			case INT -> 1;
178
		};
179
		VK12.vkCmdBindIndexBuffer(this.commandBuffer(), ((VulkanGpuBuffer)indexBuffer).vkBuffer(), 0L, type);
180
	}
181
182
	@Override
183
	public void drawIndexed(final int indexCount, final int instanceCount, final int firstIndex, final int vertexOffset, final int firstInstance) {
184
		this.pushDescriptors();
185
		VK12.vkCmdDrawIndexed(this.commandBuffer(), indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
186
	}
187
188
	@Override
189
	public void multiDrawIndexed(final IntBuffer drawParameters, final int instanceCount, final int firstInstance, final int drawCount) {
190
		this.pushDescriptors();
191
		EXTMultiDraw.nvkCmdDrawMultiIndexedEXT(
192
			this.commandBuffer(), drawCount, MemoryUtil.memAddress(drawParameters), instanceCount, firstInstance, VkMultiDrawIndexedInfoEXT.SIZEOF, 0L
193
		);
194
	}
195
196
	@Override
197
	public void multiDrawIndexed(final PointerBuffer firstIndexOffsets, final IntBuffer indexCounts, final IntBuffer vertexOffsets, final int drawCount) {
198
		throw new UnsupportedOperationException("Vulkan does not support the multiDrawDirectSeparate device feature");
199
	}
200
201
	@Override
202
	public void drawIndexedIndirect(final GpuBufferSlice commands, final int drawCount) {
203
		this.pushDescriptors();
204
		VK12.vkCmdDrawIndexedIndirect(
205
			this.commandBuffer(), ((VulkanGpuBuffer)commands.buffer()).vkBuffer(), commands.offset(), drawCount, VkDrawIndexedIndirectCommand.SIZEOF
206
		);
207
	}
208
209
	@Override
210
	public void draw(final int vertexCount, final int instanceCount, final int firstVertex, final int firstInstance) {
211
		this.pushDescriptors();
212
		VK12.vkCmdDraw(this.commandBuffer(), vertexCount, instanceCount, firstVertex, firstInstance);
213
	}
214
215
	@Override
216
	public void multiDraw(final IntBuffer drawParameters, final int instanceCount, final int firstInstance, final int drawCount) {
217
		this.pushDescriptors();
218
		EXTMultiDraw.nvkCmdDrawMultiEXT(
219
			this.commandBuffer(), drawCount, MemoryUtil.memAddress(drawParameters), instanceCount, firstInstance, VkMultiDrawInfoEXT.SIZEOF
220
		);
221
	}
222
223
	@Override
224
	public void multiDraw(final IntBuffer firstVertices, final IntBuffer vertexCounts, final int drawCount) {
225
		throw new UnsupportedOperationException("Vulkan does not support the multiDrawDirectSeparate device feature");
226
	}
227
228
	@Override
229
	public void drawIndirect(final GpuBufferSlice commands, final int drawCount) {
230
		this.pushDescriptors();
231
		VK12.vkCmdDrawIndirect(this.commandBuffer(), ((VulkanGpuBuffer)commands.buffer()).vkBuffer(), commands.offset(), drawCount, VkDrawIndirectCommand.SIZEOF);
232
	}
233
234
	private void pushDescriptors() {
235
		if (this.anyDescriptorDirty) {
236
			assert this.pipeline != null;
237
			List<BindGroupLayout.UniformDescription> uniforms = this.pipeline.uniforms();
238
239
			try (MemoryStack stack = MemoryStack.stackPush()) {
240
				org.lwjgl.vulkan.VkWriteDescriptorSet.Buffer writes = VkWriteDescriptorSet.calloc(uniforms.size(), stack);
241
242
				for (int i = 0; i < uniforms.size(); i++) {
243
					BindGroupLayout.UniformDescription uniform = uniforms.get(i);
244
					VkWriteDescriptorSet set = writes.get().sType$Default();
245
					set.dstBinding(i);
246
					set.dstArrayElement(0);
247
					set.descriptorCount(1);
248
					if (uniform.type() == UniformType.UNIFORM_BUFFER) {
249
						GpuBufferSlice buffer = (GpuBufferSlice)this.uniforms.get(i);
250
						if (buffer == null) {
251
							throw new IllegalStateException("Missing uniform " + uniform.name() + " (should be " + uniform.type() + ")");
252
						}
253
254
						org.lwjgl.vulkan.VkDescriptorBufferInfo.Buffer bufferInfo = VkDescriptorBufferInfo.calloc(1, stack);
255
						bufferInfo.buffer(((VulkanGpuBuffer)buffer.buffer()).vkBuffer());
256
						bufferInfo.offset(buffer.offset());
257
						bufferInfo.range(buffer.length());
258
						set.descriptorType(6);
259
						set.pBufferInfo(bufferInfo);
260
					} else if (uniform.type() == UniformType.COMBINED_IMAGE_SAMPLER) {
261
						TextureViewAndSampler value = (TextureViewAndSampler)this.uniforms.get(i);
262
						if (value == null) {
263
							throw new IllegalStateException("Missing sampler " + uniform.name());
264
						}
265
266
						org.lwjgl.vulkan.VkDescriptorImageInfo.Buffer imageInfo = VkDescriptorImageInfo.calloc(1, stack);
267
						imageInfo.sampler(((VulkanGpuSampler)value.sampler()).vkSampler());
268
						imageInfo.imageView(((VulkanGpuTextureView)value.view()).vkImageView());
269
						imageInfo.imageLayout(1);
270
						set.descriptorType(1);
271
						set.pImageInfo(imageInfo);
272
					} else if (uniform.type() == UniformType.TEXEL_BUFFER) {
273
						GpuBufferSlice value = (GpuBufferSlice)this.uniforms.get(i);
274
						if (value == null) {
275
							throw new IllegalStateException("Missing uniform " + uniform.name() + " (should be " + uniform.type() + ")");
276
						}
277
278
						LongBuffer bufferViewPtr = stack.callocLong(1);
279
280
						try (MemoryStack var9 = stack.push()) {
281
							assert uniform.gpuFormat() != null;
282
							VkBufferViewCreateInfo viewCreateInfo = VkBufferViewCreateInfo.calloc(stack).sType$Default();
283
							viewCreateInfo.buffer(((VulkanGpuBuffer)value.buffer()).vkBuffer());
284
							viewCreateInfo.offset(value.offset());
285
							viewCreateInfo.range(value.length());
286
							viewCreateInfo.format(VulkanConst.toVk(uniform.gpuFormat()));
287
							VulkanUtils.crashIfFailure(
288
								this.device, VK12.vkCreateBufferView(this.device.vkDevice(), viewCreateInfo, null, bufferViewPtr), "Couldn't create buffer view for texel buffer"
289
							);
290
							long bufferViewHandle = bufferViewPtr.get(0);
291
							this.encoder.queueForDestroy(() -> VK12.vkDestroyBufferView(this.device.vkDevice(), bufferViewHandle, null));
292
						}
293
294
						set.descriptorType(4);
295
						set.pTexelBufferView(bufferViewPtr);
296
					}
297
				}
298
299
				KHRPushDescriptor.vkCmdPushDescriptorSetKHR(this.commandBuffer(), 0, this.pipeline.pipelineLayout(), 0, writes.flip());
300
			}
301
302
			this.anyDescriptorDirty = false;
303
		}
304
	}
305
306
	@Override
307
	public void writeTimestamp(final GpuQueryPool pool, final int index) {
308
		long queryPool = ((VulkanQueryPool)pool).vkQueryPool();
309
		VK12.vkResetQueryPool(this.device.vkDevice(), queryPool, index, 1);
310
		KHRSynchronization2.vkCmdWriteTimestamp2KHR(this.commandBuffer(), 65536L, queryPool, index);
311
	}
312
313
	public Supplier<String> getLabel() {
314
		return this.label;
315
	}
316
}
317

Decompiled Minecraft 26.3 source of VulkanRenderPass (com/mojang/renderpearl/backend/vulkan/VulkanRenderPass.java) with Mojang official mappings, stored verbatim on the site.