| 1 | package com.mojang.renderpearl.frontend;
|
| 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.commands.RenderPassDescriptor;
|
| 8 | import com.mojang.renderpearl.api.device.DeviceFeatures;
|
| 9 | import com.mojang.renderpearl.api.device.DeviceLimits;
|
| 10 | import com.mojang.renderpearl.api.pipeline.BindGroupLayout;
|
| 11 | import com.mojang.renderpearl.api.pipeline.ColorTargetState;
|
| 12 | import com.mojang.renderpearl.api.pipeline.CompiledRenderPipeline;
|
| 13 | import com.mojang.renderpearl.api.pipeline.IndexType;
|
| 14 | import com.mojang.renderpearl.api.textures.GpuSampler;
|
| 15 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 16 | import com.mojang.renderpearl.backend.api.GpuDeviceBackend;
|
| 17 | import com.mojang.renderpearl.backend.api.RenderPassBackend;
|
| 18 | import com.mojang.renderpearl.backend.common.BaseGpuBuffer;
|
| 19 | import com.mojang.renderpearl.util.TextureViewAndSampler;
|
| 20 | import java.nio.ByteBuffer;
|
| 21 | import java.nio.IntBuffer;
|
| 22 | import java.util.Collection;
|
| 23 | import java.util.HashMap;
|
| 24 | import java.util.List;
|
| 25 | import java.util.Locale;
|
| 26 | import java.util.Optional;
|
| 27 | import java.util.function.BiConsumer;
|
| 28 | import java.util.function.Supplier;
|
| 29 | import net.fabricmc.api.EnvType;
|
| 30 | import net.fabricmc.api.Environment;
|
| 31 | import org.joml.Vector4fc;
|
| 32 | import org.jspecify.annotations.Nullable;
|
| 33 | import org.lwjgl.PointerBuffer;
|
| 34 | import org.lwjgl.vulkan.VkDrawIndexedIndirectCommand;
|
| 35 | import org.lwjgl.vulkan.VkDrawIndirectCommand;
|
| 36 |
|
| 37 | @Environment(EnvType.CLIENT)
|
| 38 | public class FrontendRenderPass implements RenderPass, RenderPass.UniformUploader {
|
| 39 | private final RenderPassBackend backend;
|
| 40 | private final GpuDeviceBackend device;
|
| 41 | private final DeviceFeatures deviceFeatures;
|
| 42 | private final DeviceLimits deviceLimits;
|
| 43 | private final Runnable onFinish;
|
| 44 | private final RenderPass.@Nullable RenderArea renderArea;
|
| 45 | private final List<RenderPassDescriptor.@Nullable Attachment<Optional<Vector4fc>>> colorAttachments;
|
| 46 | private final boolean hasDepthAttachment;
|
| 47 | private boolean isClosed;
|
| 48 | private int pushedDebugGroups;
|
| 49 | private @Nullable FrontendRenderPipeline boundPipeline;
|
| 50 | private final @Nullable GpuBufferSlice[] vertexBuffers = new GpuBufferSlice[16];
|
| 51 | protected @Nullable GpuBuffer indexBuffer;
|
| 52 | protected final HashMap<String, Object> uniforms = new HashMap<>();
|
| 53 | private boolean constantsPushed = false;
|
| 54 |
|
| 55 | public FrontendRenderPass(
|
| 56 | final RenderPassBackend backend,
|
| 57 | final GpuDeviceBackend device,
|
| 58 | final List<RenderPassDescriptor.@Nullable Attachment<Optional<Vector4fc>>> colorAttachments,
|
| 59 | final boolean hasDepthAttachment,
|
| 60 | final Runnable onFinish,
|
| 61 | final RenderPass.@Nullable RenderArea renderArea
|
| 62 | ) {
|
| 63 | this.backend = backend;
|
| 64 | this.device = device;
|
| 65 | this.deviceFeatures = device.getDeviceInfo().features();
|
| 66 | this.deviceLimits = device.getDeviceInfo().limits();
|
| 67 | this.colorAttachments = colorAttachments;
|
| 68 | this.hasDepthAttachment = hasDepthAttachment;
|
| 69 | this.onFinish = onFinish;
|
| 70 | this.renderArea = renderArea;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public void pushDebugGroup(final Supplier<String> label) {
|
| 75 | if (this.isClosed) {
|
| 76 | throw new IllegalStateException("Can't use a closed render pass");
|
| 77 | }
|
| 78 |
|
| 79 | this.pushedDebugGroups++;
|
| 80 | this.backend.pushDebugGroup(label);
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void popDebugGroup() {
|
| 85 | if (this.isClosed) {
|
| 86 | throw new IllegalStateException("Can't use a closed render pass");
|
| 87 | }
|
| 88 |
|
| 89 | if (this.pushedDebugGroups == 0) {
|
| 90 | throw new IllegalStateException("Can't pop more debug groups than was pushed!");
|
| 91 | }
|
| 92 |
|
| 93 | this.pushedDebugGroups--;
|
| 94 | this.backend.popDebugGroup();
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public void writeTimestamp(final GpuQueryPool pool, final int index) {
|
| 99 | if (index >= 0 && index <= pool.size()) {
|
| 100 | this.backend.writeTimestamp(pool, index);
|
| 101 | } else {
|
| 102 | throw new IllegalStateException("Index " + index + " is out of range for query pool of size " + pool.size());
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public void setPipeline(final CompiledRenderPipeline pipeline) {
|
| 108 | if (!(pipeline instanceof FrontendRenderPipeline frontendPipeline)) {
|
| 109 | throw new IllegalArgumentException("Pipeline must be instance of FrontendCompiledRenderPipeline");
|
| 110 | } else {
|
| 111 | List<ColorTargetState> colorTargetStates = frontendPipeline.colorTargetStates();
|
| 112 | if (colorTargetStates.size() != this.colorAttachments.size()) {
|
| 113 | throw new IllegalStateException("Render pass color attachment count must match pipeline color target state count.");
|
| 114 | }
|
| 115 |
|
| 116 | for (int i = 0; i < this.colorAttachments.size(); i++) {
|
| 117 | RenderPassDescriptor.Attachment<Optional<Vector4fc>> attachment = this.colorAttachments.get(i);
|
| 118 | if (attachment != null) {
|
| 119 | ColorTargetState colorTargetState = colorTargetStates.get(i);
|
| 120 | if (colorTargetState == null || colorTargetState.format() != attachment.textureView().texture().getFormat()) {
|
| 121 | throw new IllegalStateException("Render pass color attachment " + i + " format doesn't match pipeline format.");
|
| 122 | }
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | this.boundPipeline = frontendPipeline;
|
| 127 | this.backend.setPipeline(frontendPipeline.backendRenderPipeline());
|
| 128 | this.uniforms.forEach(this::setUniform);
|
| 129 | this.constantsPushed = false;
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | public void setUniform(final String name, final @Nullable GpuTextureView textureView, final @Nullable GpuSampler sampler) {
|
| 135 | if (textureView != null && sampler != null) {
|
| 136 | TextureViewAndSampler pair = new TextureViewAndSampler(textureView, sampler);
|
| 137 | this.setUniform(name, pair);
|
| 138 | } else {
|
| 139 | if (textureView != null || sampler != null) {
|
| 140 | throw new IllegalArgumentException("textureView and sampler must both or neither be null");
|
| 141 | }
|
| 142 |
|
| 143 | this.setUniform(name, (Object)null);
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public void setUniform(final String name, final GpuBuffer value) {
|
| 149 | this.setUniform(name, value.slice());
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public void setUniform(final String name, final GpuBufferSlice value) {
|
| 154 | int alignment = this.device.getDeviceInfo().limits().minUniformOffsetAlignment();
|
| 155 | if (value.offset() % alignment > 0L) {
|
| 156 | throw new IllegalArgumentException("Uniform buffer offset must be aligned to " + alignment);
|
| 157 | }
|
| 158 |
|
| 159 | this.setUniform(name, (Object)value);
|
| 160 | }
|
| 161 |
|
| 162 | private void setUniform(final String name, final @Nullable Object value) {
|
| 163 | if (value == null) {
|
| 164 | this.uniforms.remove(name);
|
| 165 | } else {
|
| 166 | this.uniforms.put(name, value);
|
| 167 | }
|
| 168 |
|
| 169 | if (this.boundPipeline != null) {
|
| 170 | int uniformIndex = this.boundPipeline.uniformIndices().getOrDefault(name, -1);
|
| 171 | if (uniformIndex != -1) {
|
| 172 | this.backend.setUniform(uniformIndex, value);
|
| 173 | }
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | public void pushConstants(final ByteBuffer value) {
|
| 179 | if (this.boundPipeline == null) {
|
| 180 | throw new IllegalStateException("Must bind pipeline before pushing constants");
|
| 181 | }
|
| 182 |
|
| 183 | if (value.remaining() < this.boundPipeline.pushConstantSize()) {
|
| 184 | throw new IllegalArgumentException("Not enough values for push constant range");
|
| 185 | }
|
| 186 |
|
| 187 | this.constantsPushed = true;
|
| 188 | this.backend.pushConstants(value);
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | public void enableScissor(final int x, final int y, final int width, final int height) {
|
| 193 | if (width > 0 && height > 0) {
|
| 194 | if (x >= this.renderArea.x()
|
| 195 | && y >= this.renderArea.y()
|
| 196 | && x + width <= this.renderArea.x() + this.renderArea.width()
|
| 197 | && y + height <= this.renderArea.y() + this.renderArea.height()) {
|
| 198 | this.backend.enableScissor(x, y, width, height);
|
| 199 | } else {
|
| 200 | throw new IllegalArgumentException(
|
| 201 | "Scissor at " + x + ", " + y + " with size " + width + "x" + height + " is out of bounds for render area " + this.renderArea
|
| 202 | );
|
| 203 | }
|
| 204 | } else {
|
| 205 | throw new IllegalArgumentException("Scissor size must be >0, was " + width + "x" + height);
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | @Override
|
| 210 | public void disableScissor() {
|
| 211 | this.backend.disableScissor();
|
| 212 | }
|
| 213 |
|
| 214 | @Override
|
| 215 | public void setVertexBuffer(final int slot, final @Nullable GpuBufferSlice vertexBuffer) {
|
| 216 | if (slot < 0 || slot >= 16) {
|
| 217 | throw new IllegalArgumentException("Vertex buffer slot is out of range: " + slot);
|
| 218 | }
|
| 219 |
|
| 220 | if (vertexBuffer != null && vertexBuffer.buffer().isClosed()) {
|
| 221 | throw new IllegalStateException("Vertex buffer at slot " + slot + " has been closed!");
|
| 222 | }
|
| 223 |
|
| 224 | if (vertexBuffer != null && (vertexBuffer.buffer().usage() & 32) == 0) {
|
| 225 | throw new IllegalStateException("Vertex buffer at slot " + slot + " doesn't have GpuBuffer.USAGE_VERTEX flag!");
|
| 226 | }
|
| 227 |
|
| 228 | this.vertexBuffers[slot] = vertexBuffer;
|
| 229 | this.backend.setVertexBuffer(slot, vertexBuffer);
|
| 230 | }
|
| 231 |
|
| 232 | @Override
|
| 233 | public void setIndexBuffer(final GpuBuffer indexBuffer, final IndexType indexType) {
|
| 234 | this.indexBuffer = indexBuffer;
|
| 235 | this.backend.setIndexBuffer(indexBuffer, indexType);
|
| 236 | }
|
| 237 |
|
| 238 | @Override
|
| 239 | public void drawIndexed(final int indexCount, final int instanceCount, final int firstIndex, final int vertexOffset, final int firstInstance) {
|
| 240 | if (this.isClosed) {
|
| 241 | throw new IllegalStateException("Can't use a closed render pass");
|
| 242 | }
|
| 243 |
|
| 244 | if (firstInstance != 0 && !this.deviceFeatures.nonZeroFirstInstance()) {
|
| 245 | throw new UnsupportedOperationException("firstInstance must be zero on when device does not support nonZeroFirstInstance");
|
| 246 | }
|
| 247 |
|
| 248 | this.validateDraw(List.of(), false, true);
|
| 249 | this.backend.drawIndexed(indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
|
| 250 | }
|
| 251 |
|
| 252 | @Override
|
| 253 | public void multiDrawIndexed(final IntBuffer drawParameters, final int instanceCount, final int firstInstance, final int drawCount) {
|
| 254 | if (this.isClosed) {
|
| 255 | throw new IllegalStateException("Can't use a closed render pass");
|
| 256 | }
|
| 257 |
|
| 258 | if (!this.deviceFeatures.multiDrawDirectInterleaved()) {
|
| 259 | throw new UnsupportedOperationException("device does not support multiDrawDirectInterleaved");
|
| 260 | }
|
| 261 |
|
| 262 | if (firstInstance != 0 && !this.deviceFeatures.nonZeroFirstInstance()) {
|
| 263 | throw new UnsupportedOperationException("firstInstance must be zero on when device does not support nonZeroFirstInstance");
|
| 264 | }
|
| 265 |
|
| 266 | if (drawCount > this.deviceLimits.maxMultiDrawDirectInterleavedDrawCount()) {
|
| 267 | throw new IllegalArgumentException("May not exceed maxMultiDrawDirectInterleavedDrawCount draws in a single multiDrawDirectInterleaved call");
|
| 268 | }
|
| 269 |
|
| 270 | if (drawParameters.remaining() < drawCount * 3) {
|
| 271 | throw new IllegalArgumentException("Not enough elements in drawParameters for drawCount draws");
|
| 272 | }
|
| 273 |
|
| 274 | this.validateDraw(List.of(), false, true);
|
| 275 | this.backend.multiDrawIndexed(drawParameters, instanceCount, firstInstance, drawCount);
|
| 276 | }
|
| 277 |
|
| 278 | @Override
|
| 279 | public void multiDrawIndexed(final PointerBuffer firstIndexOffsets, final IntBuffer indexCounts, final IntBuffer vertexOffsets, final int drawCount) {
|
| 280 | if (this.isClosed) {
|
| 281 | throw new IllegalStateException("Can't use a closed render pass");
|
| 282 | }
|
| 283 |
|
| 284 | if (!this.deviceFeatures.multiDrawDirectSeparate()) {
|
| 285 | throw new UnsupportedOperationException("device does not support multiDrawDirectSeparate");
|
| 286 | }
|
| 287 |
|
| 288 | if (firstIndexOffsets.remaining() < drawCount) {
|
| 289 | throw new IllegalArgumentException("firstIndexOffsets does not contain enough elements for drawCount draws");
|
| 290 | }
|
| 291 |
|
| 292 | if (indexCounts.remaining() < drawCount) {
|
| 293 | throw new IllegalArgumentException("indexCounts does not contain enough elements for drawCount draws");
|
| 294 | }
|
| 295 |
|
| 296 | if (vertexOffsets.remaining() < drawCount) {
|
| 297 | throw new IllegalArgumentException("vertexOffsets does not contain enough elements for drawCount draws");
|
| 298 | }
|
| 299 |
|
| 300 | this.validateDraw(List.of(), false, true);
|
| 301 | this.backend.multiDrawIndexed(firstIndexOffsets, indexCounts, vertexOffsets, drawCount);
|
| 302 | }
|
| 303 |
|
| 304 | @Override
|
| 305 | public void drawIndexedIndirect(final GpuBufferSlice commands, final int drawCount) {
|
| 306 | if (this.isClosed) {
|
| 307 | throw new IllegalStateException("Can't use a closed render pass");
|
| 308 | }
|
| 309 |
|
| 310 | if (!this.deviceFeatures.drawIndirect()) {
|
| 311 | throw new UnsupportedOperationException("device does not support drawIndirect");
|
| 312 | }
|
| 313 |
|
| 314 | if (drawCount > 1 && !this.deviceFeatures.multiDrawIndirect()) {
|
| 315 | throw new UnsupportedOperationException("drawCount must be one when device does not support multiDrawIndirect");
|
| 316 | }
|
| 317 |
|
| 318 | if ((commands.buffer().usage() & 512) == 0) {
|
| 319 | throw new IllegalArgumentException("Indirect commands buffer must have GpuBuffer.USAGE_INDIRECT_PARAMETERS flag");
|
| 320 | }
|
| 321 |
|
| 322 | if (commands.length() < (long)drawCount * VkDrawIndexedIndirectCommand.SIZEOF) {
|
| 323 | throw new IllegalArgumentException("Commands buffer is not large enough to hold requested draw count at the given offset");
|
| 324 | }
|
| 325 |
|
| 326 | if (commands.offset() % 4L != 0L) {
|
| 327 | throw new IllegalArgumentException("Commands offset must be multiple of 4");
|
| 328 | }
|
| 329 |
|
| 330 | this.validateDraw(List.of(), false, true);
|
| 331 | this.backend.drawIndexedIndirect(commands, drawCount);
|
| 332 | }
|
| 333 |
|
| 334 | @Override
|
| 335 | public <T> void drawMultipleIndexed(
|
| 336 | final Collection<RenderPass.Draw<T>> draws,
|
| 337 | final @Nullable GpuBuffer defaultIndexBuffer,
|
| 338 | final @Nullable IndexType defaultIndexType,
|
| 339 | final Collection<String> dynamicUniforms,
|
| 340 | final T uniformArgument
|
| 341 | ) {
|
| 342 | if (this.isClosed) {
|
| 343 | throw new IllegalStateException("Can't use a closed render pass");
|
| 344 | }
|
| 345 |
|
| 346 | this.validateDraw(dynamicUniforms, true, false);
|
| 347 | GpuBuffer lastIndexBuffer = null;
|
| 348 | IndexType lastIndexType = null;
|
| 349 |
|
| 350 | for (RenderPass.Draw<T> draw : draws) {
|
| 351 | BiConsumer<T, RenderPass.UniformUploader> uniformUploaderConsumer = draw.uniformUploaderConsumer();
|
| 352 | if (uniformUploaderConsumer != null) {
|
| 353 | uniformUploaderConsumer.accept(uniformArgument, this);
|
| 354 | }
|
| 355 |
|
| 356 | IndexType indexType = draw.indexType() == null ? defaultIndexType : draw.indexType();
|
| 357 | GpuBuffer indexBuffer = draw.indexBuffer() == null ? defaultIndexBuffer : draw.indexBuffer();
|
| 358 | assert indexBuffer != null;
|
| 359 | assert indexType != null;
|
| 360 | this.setVertexBuffer(draw.slot(), draw.vertexBuffer().slice());
|
| 361 | if (FrontendGpuDevice.STRICT_VALIDATION) {
|
| 362 | if (indexBuffer == null) {
|
| 363 | throw new IllegalStateException("Missing index buffer");
|
| 364 | }
|
| 365 |
|
| 366 | ((BaseGpuBuffer)indexBuffer).checkCanBeUsed();
|
| 367 | if (indexBuffer.isClosed()) {
|
| 368 | throw new IllegalStateException("Index buffer has been closed!");
|
| 369 | }
|
| 370 |
|
| 371 | if (draw.slot() < 0 || draw.slot() >= 16) {
|
| 372 | throw new IllegalStateException("Vertex buffer slot must be between 0 and 16");
|
| 373 | }
|
| 374 |
|
| 375 | if (this.vertexBuffers[draw.slot()] != null) {
|
| 376 | ((BaseGpuBuffer)this.vertexBuffers[draw.slot()].buffer()).checkCanBeUsed();
|
| 377 | }
|
| 378 |
|
| 379 | if (this.vertexBuffers[draw.slot()] == null) {
|
| 380 | throw new IllegalStateException("Missing vertex buffer at slot " + draw.slot());
|
| 381 | }
|
| 382 |
|
| 383 | if (this.vertexBuffers[draw.slot()].buffer().isClosed()) {
|
| 384 | throw new IllegalStateException("Vertex buffer at slot " + draw.slot() + " has been closed!");
|
| 385 | }
|
| 386 | }
|
| 387 |
|
| 388 | if (indexBuffer != lastIndexBuffer || indexType != lastIndexType) {
|
| 389 | this.setIndexBuffer(indexBuffer, indexType);
|
| 390 | lastIndexBuffer = indexBuffer;
|
| 391 | lastIndexType = indexType;
|
| 392 | }
|
| 393 |
|
| 394 | assert this.boundPipeline != null;
|
| 395 | if (this.boundPipeline.pushConstantSize() > 0 && !this.constantsPushed) {
|
| 396 | throw new IllegalStateException("Missing push constants");
|
| 397 | }
|
| 398 |
|
| 399 | this.backend.drawIndexed(draw.indexCount(), 1, draw.firstIndex(), draw.baseVertex(), 0);
|
| 400 | }
|
| 401 | }
|
| 402 |
|
| 403 | @Override
|
| 404 | public void draw(final int vertexCount, final int instanceCount, final int firstVertex, final int firstInstance) {
|
| 405 | if (this.isClosed) {
|
| 406 | throw new IllegalStateException("Can't use a closed render pass");
|
| 407 | }
|
| 408 |
|
| 409 | if (firstInstance != 0 && !this.deviceFeatures.nonZeroFirstInstance()) {
|
| 410 | throw new UnsupportedOperationException("firstInstance must be zero on when device does not support nonZeroFirstInstance");
|
| 411 | }
|
| 412 |
|
| 413 | this.validateDraw(List.of(), false, false);
|
| 414 | this.backend.draw(vertexCount, instanceCount, firstVertex, firstInstance);
|
| 415 | }
|
| 416 |
|
| 417 | @Override
|
| 418 | public void multiDraw(final IntBuffer drawParameters, final int instanceCount, final int firstInstance, final int drawCount) {
|
| 419 | if (this.isClosed) {
|
| 420 | throw new IllegalStateException("Can't use a closed render pass");
|
| 421 | }
|
| 422 |
|
| 423 | if (!this.deviceFeatures.multiDrawDirectInterleaved()) {
|
| 424 | throw new UnsupportedOperationException("device does not support multiDrawDirectInterleaved");
|
| 425 | }
|
| 426 |
|
| 427 | if (firstInstance != 0 && !this.deviceFeatures.nonZeroFirstInstance()) {
|
| 428 | throw new UnsupportedOperationException("firstInstance must be zero on when device does not support nonZeroFirstInstance");
|
| 429 | }
|
| 430 |
|
| 431 | if (drawCount > this.deviceLimits.maxMultiDrawDirectInterleavedDrawCount()) {
|
| 432 | throw new IllegalArgumentException("May not exceed maxMultiDrawDirectInterleavedDrawCount draws in a single multiDrawDirectInterleaved call");
|
| 433 | }
|
| 434 |
|
| 435 | if (drawParameters.remaining() < drawCount * 2) {
|
| 436 | throw new IllegalArgumentException("Not enough elements in drawParameters for drawCount draws");
|
| 437 | }
|
| 438 |
|
| 439 | this.validateDraw(List.of(), false, false);
|
| 440 | this.backend.multiDraw(drawParameters, instanceCount, firstInstance, drawCount);
|
| 441 | }
|
| 442 |
|
| 443 | @Override
|
| 444 | public void multiDraw(final IntBuffer firstVertices, final IntBuffer vertexCounts, final int drawCount) {
|
| 445 | if (this.isClosed) {
|
| 446 | throw new IllegalStateException("Can't use a closed render pass");
|
| 447 | }
|
| 448 |
|
| 449 | if (!this.deviceFeatures.multiDrawDirectSeparate()) {
|
| 450 | throw new UnsupportedOperationException("device does not support multiDrawDirectSeparate");
|
| 451 | }
|
| 452 |
|
| 453 | if (firstVertices.remaining() < drawCount) {
|
| 454 | throw new IllegalArgumentException("firstVertices does not contain enough elements for drawCount draws");
|
| 455 | }
|
| 456 |
|
| 457 | if (vertexCounts.remaining() < drawCount) {
|
| 458 | throw new IllegalArgumentException("vertexCounts does not contain enough elements for drawCount draws");
|
| 459 | }
|
| 460 |
|
| 461 | this.validateDraw(List.of(), false, false);
|
| 462 | this.backend.multiDraw(firstVertices, vertexCounts, drawCount);
|
| 463 | }
|
| 464 |
|
| 465 | @Override
|
| 466 | public void drawIndirect(final GpuBufferSlice commands, final int drawCount) {
|
| 467 | if (this.isClosed) {
|
| 468 | throw new IllegalStateException("Can't use a closed render pass");
|
| 469 | }
|
| 470 |
|
| 471 | if (!this.deviceFeatures.drawIndirect()) {
|
| 472 | throw new UnsupportedOperationException("device does not support drawIndirect");
|
| 473 | }
|
| 474 |
|
| 475 | if (drawCount > 1 && !this.deviceFeatures.multiDrawIndirect()) {
|
| 476 | throw new UnsupportedOperationException("drawCount must be one when device does not support multiDrawIndirect");
|
| 477 | }
|
| 478 |
|
| 479 | if ((commands.buffer().usage() & 512) == 0) {
|
| 480 | throw new IllegalArgumentException("Indirect commands buffer must have GpuBuffer.USAGE_INDIRECT_PARAMETERS flag");
|
| 481 | }
|
| 482 |
|
| 483 | if (commands.length() < (long)drawCount * VkDrawIndirectCommand.SIZEOF) {
|
| 484 | throw new IllegalArgumentException("Commands buffer is not large enough to hold requested draw count at the given offset");
|
| 485 | }
|
| 486 |
|
| 487 | if (commands.offset() % 4L != 0L) {
|
| 488 | throw new IllegalArgumentException("Commands offset must be multiple of 4");
|
| 489 | }
|
| 490 |
|
| 491 | this.validateDraw(List.of(), false, false);
|
| 492 | this.backend.drawIndirect(commands, drawCount);
|
| 493 | }
|
| 494 |
|
| 495 | @Override
|
| 496 | public void close() {
|
| 497 | if (!this.isClosed) {
|
| 498 | this.isClosed = true;
|
| 499 | if (this.pushedDebugGroups > 0) {
|
| 500 | throw new IllegalStateException("Render pass had debug groups left open!");
|
| 501 | }
|
| 502 |
|
| 503 | this.onFinish.run();
|
| 504 | }
|
| 505 | }
|
| 506 |
|
| 507 | private void validateDraw(final Collection<String> dynamicUniforms, final boolean dynamicVertexBuffer, final boolean indexed) {
|
| 508 | if (this.boundPipeline == null) {
|
| 509 | throw new IllegalStateException("Can't draw without a render pipeline");
|
| 510 | }
|
| 511 |
|
| 512 | if (FrontendGpuDevice.STRICT_VALIDATION) {
|
| 513 | if (this.boundPipeline.pushConstantSize() > 0 && !this.constantsPushed) {
|
| 514 | throw new IllegalStateException("Missing push constants");
|
| 515 | }
|
| 516 |
|
| 517 | if (indexed) {
|
| 518 | if (this.indexBuffer == null) {
|
| 519 | throw new IllegalStateException("Missing index buffer");
|
| 520 | }
|
| 521 |
|
| 522 | ((BaseGpuBuffer)this.indexBuffer).checkCanBeUsed();
|
| 523 | if (this.indexBuffer.isClosed()) {
|
| 524 | throw new IllegalStateException("Index buffer has been closed!");
|
| 525 | }
|
| 526 |
|
| 527 | if ((this.indexBuffer.usage() & 64) == 0) {
|
| 528 | throw new IllegalStateException("Index buffer must have GpuBuffer.USAGE_INDEX!");
|
| 529 | }
|
| 530 | }
|
| 531 |
|
| 532 | if (!dynamicVertexBuffer) {
|
| 533 | for (int i = 0; i < 16; i++) {
|
| 534 | if (this.vertexBuffers[i] == null && this.boundPipeline.vertexFormats().get(i) != null) {
|
| 535 | throw new IllegalStateException("Vertex format contains elements but vertex buffer at slot " + i + " is null");
|
| 536 | }
|
| 537 |
|
| 538 | if (this.vertexBuffers[i] != null) {
|
| 539 | ((BaseGpuBuffer)this.vertexBuffers[i].buffer()).checkCanBeUsed();
|
| 540 | }
|
| 541 | }
|
| 542 | }
|
| 543 |
|
| 544 | for (BindGroupLayout.UniformDescription uniform : this.boundPipeline.uniforms()) {
|
| 545 | Object value = this.uniforms.get(uniform.name());
|
| 546 | if (!dynamicUniforms.contains(uniform.name())) {
|
| 547 | if (value == null) {
|
| 548 | throw new IllegalStateException("Missing uniform " + uniform.name() + " (should be " + uniform.type() + ")");
|
| 549 | }
|
| 550 |
|
| 551 | switch (uniform.type()) {
|
| 552 | case UNIFORM_BUFFER:
|
| 553 | if (!(value instanceof GpuBufferSlice valueSlice)) {
|
| 554 | throw new IllegalArgumentException("UBO value must be GpuBufferSlice");
|
| 555 | }
|
| 556 |
|
| 557 | ((BaseGpuBuffer)valueSlice.buffer()).checkCanBeUsed();
|
| 558 | if (valueSlice.buffer().isClosed()) {
|
| 559 | throw new IllegalStateException("Uniform buffer " + uniform.name() + " is already closed");
|
| 560 | }
|
| 561 |
|
| 562 | if ((valueSlice.buffer().usage() & 128) == 0) {
|
| 563 | throw new IllegalStateException("Uniform buffer " + uniform.name() + " must have GpuBuffer.USAGE_UNIFORM");
|
| 564 | }
|
| 565 | break;
|
| 566 | case TEXEL_BUFFER:
|
| 567 | if (value instanceof GpuBufferSlice(GpuBuffer buffer, long offset, long length)) {
|
| 568 | if (offset == 0L && length == buffer.size()) {
|
| 569 | if ((buffer.usage() & 256) == 0) {
|
| 570 | throw new IllegalStateException("Uniform texel buffer " + uniform.name() + " must have GpuBuffer.USAGE_UNIFORM_TEXEL_BUFFER");
|
| 571 | }
|
| 572 |
|
| 573 | if (uniform.gpuFormat() == null) {
|
| 574 | throw new IllegalStateException("Invalid uniform texel buffer " + uniform.name() + " (missing a texture format)");
|
| 575 | }
|
| 576 | break;
|
| 577 | }
|
| 578 |
|
| 579 | throw new IllegalStateException("Uniform texel buffers do not support a slice of a buffer, must be entire buffer");
|
| 580 | }
|
| 581 |
|
| 582 | throw new IllegalArgumentException("UTB value must be GpuBufferSlice");
|
| 583 | case COMBINED_IMAGE_SAMPLER:
|
| 584 | if (!(value instanceof TextureViewAndSampler var9)) {
|
| 585 | throw new IllegalArgumentException("Sampler value must be TextureViewAndSampler");
|
| 586 | }
|
| 587 |
|
| 588 | TextureViewAndSampler var10000 = var9;
|
| 589 |
|
| 590 | try {
|
| 591 | var30 = var10000.view();
|
| 592 | } catch (Throwable var18) {
|
| 593 | throw new MatchException(var18.toString(), var18);
|
| 594 | }
|
| 595 |
|
| 596 | GpuTextureView length = var30;
|
| 597 | GpuTextureView textureView = length;
|
| 598 | var10000 = var9;
|
| 599 |
|
| 600 | try {
|
| 601 | var32 = var10000.sampler();
|
| 602 | } catch (Throwable var17) {
|
| 603 | throw new MatchException(var17.toString(), var17);
|
| 604 | }
|
| 605 |
|
| 606 | GpuSampler var26 = var32;
|
| 607 | GpuSampler sampler = var26;
|
| 608 | if (textureView.isClosed()) {
|
| 609 | throw new IllegalStateException("Texture view " + uniform.name() + " (" + textureView.texture().getLabel() + ") has been closed!");
|
| 610 | }
|
| 611 |
|
| 612 | if ((textureView.texture().usage() & 4) == 0) {
|
| 613 | throw new IllegalStateException("Texture view " + uniform.name() + " (" + textureView.texture().getLabel() + ") must have USAGE_TEXTURE_BINDING!");
|
| 614 | }
|
| 615 |
|
| 616 | if (sampler.isClosed()) {
|
| 617 | throw new IllegalStateException("Sampler for " + uniform.name() + " (" + textureView.texture().getLabel() + ") has been closed!");
|
| 618 | }
|
| 619 | }
|
| 620 | }
|
| 621 | }
|
| 622 |
|
| 623 | if (this.boundPipeline.wantsDepthTexture() && !this.hasDepthAttachment) {
|
| 624 | throw new IllegalStateException(String.format(Locale.ROOT, "Render pipeline %s wants a depth texture but none was provided", this.boundPipeline.name()));
|
| 625 | }
|
| 626 | }
|
| 627 | }
|
| 628 | }
|
| 629 |
|