| 1 | package com.mojang.renderpearl.frontend;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.NativeImage;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.renderpearl.api.GpuFormat;
|
| 6 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 7 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 8 | import com.mojang.renderpearl.api.buffers.TransientMemory;
|
| 9 | import com.mojang.renderpearl.api.commands.CommandEncoder;
|
| 10 | import com.mojang.renderpearl.api.commands.GpuFence;
|
| 11 | import com.mojang.renderpearl.api.commands.GpuQueryPool;
|
| 12 | import com.mojang.renderpearl.api.commands.RenderPass;
|
| 13 | import com.mojang.renderpearl.api.commands.RenderPassDescriptor;
|
| 14 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 15 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 16 | import com.mojang.renderpearl.backend.api.CommandEncoderBackend;
|
| 17 | import com.mojang.renderpearl.backend.api.GpuDeviceBackend;
|
| 18 | import java.nio.ByteBuffer;
|
| 19 | import java.util.Optional;
|
| 20 | import java.util.OptionalDouble;
|
| 21 | import java.util.function.Supplier;
|
| 22 | import net.fabricmc.api.EnvType;
|
| 23 | import net.fabricmc.api.Environment;
|
| 24 | import org.jetbrains.annotations.ApiStatus.Internal;
|
| 25 | import org.joml.Vector4fc;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 | import org.slf4j.Logger;
|
| 28 |
|
| 29 | @Environment(EnvType.CLIENT)
|
| 30 | public class FrontendCommandEncoder implements CommandEncoder {
|
| 31 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 32 | private final GpuDeviceBackend device;
|
| 33 | private final CommandEncoderBackend backend;
|
| 34 | private boolean isInRenderPass;
|
| 35 | private final @Nullable TracyGpuProfiler profiler;
|
| 36 |
|
| 37 | public FrontendCommandEncoder(final @Nullable TracyGpuProfiler profiler, final GpuDeviceBackend device, final CommandEncoderBackend backend) {
|
| 38 | this.profiler = profiler;
|
| 39 | this.device = device;
|
| 40 | this.backend = backend;
|
| 41 | }
|
| 42 |
|
| 43 | @Internal
|
| 44 | public CommandEncoderBackend backend() {
|
| 45 | return this.backend;
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public void submit() {
|
| 50 | this.backend.submit();
|
| 51 | if (this.profiler != null) {
|
| 52 | this.profiler.endFrame();
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public TransientMemory transientMemory() {
|
| 58 | return this.backend.transientMemory();
|
| 59 | }
|
| 60 |
|
| 61 | @Internal
|
| 62 | public boolean isInRenderPass() {
|
| 63 | return this.isInRenderPass;
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public RenderPass createRenderPass(final Supplier<String> label, final GpuTextureView colorTexture, final Optional<Vector4fc> clearColor) {
|
| 68 | return this.createRenderPass(label, colorTexture, clearColor, null, OptionalDouble.empty());
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public RenderPass createRenderPass(
|
| 73 | final Supplier<String> label,
|
| 74 | final GpuTextureView colorTexture,
|
| 75 | final Optional<Vector4fc> clearColor,
|
| 76 | final @Nullable GpuTextureView depthTexture,
|
| 77 | final OptionalDouble clearDepth
|
| 78 | ) {
|
| 79 | return this.createRenderPass(
|
| 80 | label, colorTexture, clearColor, depthTexture, clearDepth, new RenderPass.RenderArea(0, 0, colorTexture.getWidth(0), colorTexture.getHeight(0))
|
| 81 | );
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public RenderPass createRenderPass(
|
| 86 | final Supplier<String> label,
|
| 87 | final GpuTextureView colorTexture,
|
| 88 | final Optional<Vector4fc> clearColor,
|
| 89 | final @Nullable GpuTextureView depthTexture,
|
| 90 | final OptionalDouble clearDepth,
|
| 91 | final RenderPass.RenderArea renderArea
|
| 92 | ) {
|
| 93 | RenderPassDescriptor.Builder descriptor = RenderPassDescriptor.builder(label).withColorAttachment(colorTexture, clearColor);
|
| 94 | if (depthTexture != null) {
|
| 95 | descriptor.withDepthAttachment(depthTexture, clearDepth);
|
| 96 | }
|
| 97 |
|
| 98 | descriptor.withRenderArea(renderArea);
|
| 99 | return this.createRenderPass(descriptor.build());
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public RenderPass createRenderPass(final RenderPassDescriptor descriptor) {
|
| 104 | if (this.isInRenderPass) {
|
| 105 | throw new IllegalStateException("Close the existing render pass before creating a new one!");
|
| 106 | }
|
| 107 |
|
| 108 | int maxColorAttachments = this.device.getDeviceInfo().limits().maxColorAttachments();
|
| 109 | int colorAttachmentCount = descriptor.colorAttachments().size();
|
| 110 | if (colorAttachmentCount > maxColorAttachments) {
|
| 111 | throw new IllegalStateException("Render pass created with " + colorAttachmentCount + " color attachments but device only supports " + maxColorAttachments);
|
| 112 | }
|
| 113 |
|
| 114 | int totalAttachments = colorAttachmentCount + (descriptor.depthAttachment() != null ? 1 : 0);
|
| 115 | if (totalAttachments == 0) {
|
| 116 | throw new IllegalArgumentException("At least one attachment (depth or color) must be specified");
|
| 117 | }
|
| 118 |
|
| 119 | int attachmentWidth;
|
| 120 | int attachmentHeight;
|
| 121 | if (colorAttachmentCount != 0) {
|
| 122 | RenderPassDescriptor.Attachment<Optional<Vector4fc>> firstAttachment = descriptor.colorAttachments().getFirst();
|
| 123 | assert firstAttachment != null;
|
| 124 | attachmentWidth = firstAttachment.textureView().getWidth(0);
|
| 125 | attachmentHeight = firstAttachment.textureView().getHeight(0);
|
| 126 | } else {
|
| 127 | attachmentWidth = descriptor.depthAttachment().textureView().getWidth(0);
|
| 128 | attachmentHeight = descriptor.depthAttachment().textureView().getHeight(0);
|
| 129 | }
|
| 130 |
|
| 131 | RenderPass.RenderArea renderArea = descriptor.renderArea();
|
| 132 | if (renderArea.x() >= 0
|
| 133 | && renderArea.y() >= 0
|
| 134 | && renderArea.x() + renderArea.width() <= attachmentWidth
|
| 135 | && renderArea.y() + renderArea.height() <= attachmentHeight) {
|
| 136 | for (int i = 0; i < colorAttachmentCount; i++) {
|
| 137 | RenderPassDescriptor.Attachment<Optional<Vector4fc>> colorAttachment = descriptor.colorAttachments().get(i);
|
| 138 | if (colorAttachment != null) {
|
| 139 | GpuTextureView colorTexture = colorAttachment.textureView();
|
| 140 | if (colorTexture.isClosed()) {
|
| 141 | throw new IllegalStateException("Color texture " + i + " is closed");
|
| 142 | }
|
| 143 |
|
| 144 | if ((colorTexture.texture().usage() & 8) == 0) {
|
| 145 | throw new IllegalStateException("Color texture " + i + " must have USAGE_RENDER_ATTACHMENT");
|
| 146 | }
|
| 147 |
|
| 148 | if (colorTexture.texture().getDepthOrLayers() > 1) {
|
| 149 | throw new UnsupportedOperationException("Color texture " + i + ": Textures with multiple depths or layers are not yet supported as an attachment");
|
| 150 | }
|
| 151 |
|
| 152 | if (colorTexture.getWidth(0) != attachmentWidth || colorTexture.getHeight(0) != attachmentHeight) {
|
| 153 | throw new IllegalArgumentException(
|
| 154 | "Color texture "
|
| 155 | + i
|
| 156 | + ": size does not match expected attachment size. Is "
|
| 157 | + colorTexture.getWidth(0)
|
| 158 | + "x"
|
| 159 | + colorTexture.getHeight(0)
|
| 160 | + " expected "
|
| 161 | + attachmentWidth
|
| 162 | + "x"
|
| 163 | + attachmentHeight
|
| 164 | );
|
| 165 | }
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | if (descriptor.depthAttachment() != null) {
|
| 170 | GpuTextureView depthTexture = descriptor.depthAttachment().textureView();
|
| 171 | if (depthTexture.isClosed()) {
|
| 172 | throw new IllegalStateException("Depth texture is closed");
|
| 173 | }
|
| 174 |
|
| 175 | if ((depthTexture.texture().usage() & 8) == 0) {
|
| 176 | throw new IllegalStateException("Depth texture must have USAGE_RENDER_ATTACHMENT");
|
| 177 | }
|
| 178 |
|
| 179 | if (depthTexture.texture().getDepthOrLayers() > 1) {
|
| 180 | throw new UnsupportedOperationException("Depth texture: Textures with multiple depths or layers are not yet supported as an attachment");
|
| 181 | }
|
| 182 |
|
| 183 | if (depthTexture.getWidth(0) != attachmentWidth || depthTexture.getHeight(0) != attachmentHeight) {
|
| 184 | throw new IllegalArgumentException(
|
| 185 | "Depth texture: size does not match expected attachment size. Is "
|
| 186 | + depthTexture.getWidth(0)
|
| 187 | + "x"
|
| 188 | + depthTexture.getHeight(0)
|
| 189 | + " expected "
|
| 190 | + attachmentWidth
|
| 191 | + "x"
|
| 192 | + attachmentHeight
|
| 193 | );
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | this.isInRenderPass = true;
|
| 198 | if (this.profiler != null) {
|
| 199 | this.profiler.pushZone(this, descriptor.label().get());
|
| 200 | }
|
| 201 |
|
| 202 | return new FrontendRenderPass(
|
| 203 | this.backend.createRenderPass(descriptor),
|
| 204 | this.device,
|
| 205 | descriptor.colorAttachments(),
|
| 206 | descriptor.depthAttachment() != null,
|
| 207 | this::submitRenderPass,
|
| 208 | renderArea
|
| 209 | );
|
| 210 | } else {
|
| 211 | throw new IllegalArgumentException("RenderPass render area " + renderArea + " is out of bounds for texture of " + attachmentWidth + "x" + attachmentHeight);
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | protected void submitRenderPass() {
|
| 216 | if (!this.isInRenderPass) {
|
| 217 | throw new IllegalStateException("Can't submit a render pass if one isn't open");
|
| 218 | }
|
| 219 |
|
| 220 | this.isInRenderPass = false;
|
| 221 | this.backend.submitRenderPass();
|
| 222 | if (this.profiler != null) {
|
| 223 | this.profiler.popZone(this);
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | @Override
|
| 228 | public void clearColorTexture(final GpuTexture colorTexture, final Vector4fc clearColor) {
|
| 229 | if (this.isInRenderPass) {
|
| 230 | throw new IllegalStateException("Close the existing render pass before creating a new one!");
|
| 231 | }
|
| 232 |
|
| 233 | this.verifyColorTexture(colorTexture);
|
| 234 | this.backend.clearColorTexture(colorTexture, clearColor);
|
| 235 | }
|
| 236 |
|
| 237 | @Override
|
| 238 | public void clearColorAndDepthTextures(final GpuTexture colorTexture, final Vector4fc clearColor, final GpuTexture depthTexture, final double clearDepth) {
|
| 239 | if (this.isInRenderPass) {
|
| 240 | throw new IllegalStateException("Close the existing render pass before creating a new one!");
|
| 241 | }
|
| 242 |
|
| 243 | this.verifyColorTexture(colorTexture);
|
| 244 | this.verifyDepthTexture(depthTexture);
|
| 245 | if (colorTexture.getMipLevels() != depthTexture.getMipLevels()) {
|
| 246 | throw new IllegalArgumentException("Both textures must have the same mip count");
|
| 247 | }
|
| 248 |
|
| 249 | this.backend.clearColorAndDepthTextures(colorTexture, clearColor, depthTexture, clearDepth);
|
| 250 | }
|
| 251 |
|
| 252 | @Override
|
| 253 | public void clearColorAndDepthTextures(
|
| 254 | final GpuTexture colorTexture,
|
| 255 | final Vector4fc clearColor,
|
| 256 | final GpuTexture depthTexture,
|
| 257 | final double clearDepth,
|
| 258 | final int regionX,
|
| 259 | final int regionY,
|
| 260 | final int regionWidth,
|
| 261 | final int regionHeight,
|
| 262 | final int mipLevel
|
| 263 | ) {
|
| 264 | if (this.isInRenderPass) {
|
| 265 | throw new IllegalStateException("Close the existing render pass before creating a new one!");
|
| 266 | }
|
| 267 |
|
| 268 | this.verifyColorTexture(colorTexture);
|
| 269 | this.verifyDepthTexture(depthTexture);
|
| 270 | if (colorTexture.getMipLevels() != depthTexture.getMipLevels()) {
|
| 271 | throw new IllegalArgumentException("Both textures must have the same mip count");
|
| 272 | }
|
| 273 |
|
| 274 | this.verifyRegion(colorTexture, regionX, regionY, regionWidth, regionHeight, mipLevel);
|
| 275 | this.backend.clearColorAndDepthTextures(colorTexture, clearColor, depthTexture, clearDepth, regionX, regionY, regionWidth, regionHeight, mipLevel);
|
| 276 | }
|
| 277 |
|
| 278 | @Override
|
| 279 | public void clearDepthTexture(final GpuTexture depthTexture, final double clearDepth) {
|
| 280 | if (this.isInRenderPass) {
|
| 281 | throw new IllegalStateException("Close the existing render pass before creating a new one!");
|
| 282 | }
|
| 283 |
|
| 284 | this.verifyDepthTexture(depthTexture);
|
| 285 | this.backend.clearDepthTexture(depthTexture, clearDepth);
|
| 286 | }
|
| 287 |
|
| 288 | @Override
|
| 289 | public void writeToBuffer(final GpuBufferSlice destination, final ByteBuffer data) {
|
| 290 | if (this.isInRenderPass) {
|
| 291 | throw new IllegalStateException("Close the existing render pass before performing additional commands");
|
| 292 | }
|
| 293 |
|
| 294 | GpuBuffer buffer = destination.buffer();
|
| 295 | if (buffer.isClosed()) {
|
| 296 | throw new IllegalStateException("Buffer already closed");
|
| 297 | }
|
| 298 |
|
| 299 | if ((buffer.usage() & 8) == 0) {
|
| 300 | throw new IllegalStateException("Buffer needs USAGE_COPY_DST to be a destination for a copy");
|
| 301 | }
|
| 302 |
|
| 303 | int length = data.remaining();
|
| 304 | if (length > destination.length()) {
|
| 305 | throw new IllegalArgumentException(
|
| 306 | "Cannot write more data than the slice allows (attempting to write " + length + " bytes into a slice of length " + destination.length() + ")"
|
| 307 | );
|
| 308 | }
|
| 309 |
|
| 310 | if (destination.length() + destination.offset() > buffer.size()) {
|
| 311 | throw new IllegalArgumentException(
|
| 312 | "Cannot write more data than this buffer can hold (attempting to write "
|
| 313 | + length
|
| 314 | + " bytes at offset "
|
| 315 | + destination.offset()
|
| 316 | + " to "
|
| 317 | + buffer.size()
|
| 318 | + " size buffer)"
|
| 319 | );
|
| 320 | }
|
| 321 |
|
| 322 | this.backend.writeToBuffer(destination, data);
|
| 323 | }
|
| 324 |
|
| 325 | @Override
|
| 326 | public void copyToBuffer(final GpuBufferSlice source, final GpuBufferSlice target) {
|
| 327 | if (this.isInRenderPass) {
|
| 328 | throw new IllegalStateException("Close the existing render pass before performing additional commands");
|
| 329 | }
|
| 330 |
|
| 331 | GpuBuffer sourceBuffer = source.buffer();
|
| 332 | if (sourceBuffer.isClosed()) {
|
| 333 | throw new IllegalStateException("Source buffer already closed");
|
| 334 | }
|
| 335 |
|
| 336 | if ((sourceBuffer.usage() & 16) == 0) {
|
| 337 | throw new IllegalStateException("Source buffer needs USAGE_COPY_SRC to be a source for a copy");
|
| 338 | }
|
| 339 |
|
| 340 | GpuBuffer targetBuffer = target.buffer();
|
| 341 | if (targetBuffer.isClosed()) {
|
| 342 | throw new IllegalStateException("Target buffer already closed");
|
| 343 | }
|
| 344 |
|
| 345 | if ((targetBuffer.usage() & 8) == 0) {
|
| 346 | throw new IllegalStateException("Target buffer needs USAGE_COPY_DST to be a destination for a copy");
|
| 347 | }
|
| 348 |
|
| 349 | if (source.length() != target.length()) {
|
| 350 | throw new IllegalArgumentException("Cannot copy from slice of size " + source.length() + " to slice of size " + target.length() + ", they must be equal");
|
| 351 | }
|
| 352 |
|
| 353 | if (source.offset() + source.length() > sourceBuffer.size()) {
|
| 354 | throw new IllegalArgumentException(
|
| 355 | "Cannot copy more data than the source buffer holds (attempting to copy "
|
| 356 | + source.length()
|
| 357 | + " bytes at offset "
|
| 358 | + source.offset()
|
| 359 | + " from "
|
| 360 | + sourceBuffer.size()
|
| 361 | + " size buffer)"
|
| 362 | );
|
| 363 | }
|
| 364 |
|
| 365 | if (target.offset() + target.length() > targetBuffer.size()) {
|
| 366 | throw new IllegalArgumentException(
|
| 367 | "Cannot copy more data than the target buffer can hold (attempting to copy "
|
| 368 | + target.length()
|
| 369 | + " bytes at offset "
|
| 370 | + target.offset()
|
| 371 | + " to "
|
| 372 | + targetBuffer.size()
|
| 373 | + " size buffer)"
|
| 374 | );
|
| 375 | }
|
| 376 |
|
| 377 | this.backend.copyToBuffer(source, target);
|
| 378 | }
|
| 379 |
|
| 380 | @Override
|
| 381 | public void writeToTexture(final GpuTexture destination, final NativeImage source) {
|
| 382 | int width = destination.getWidth(0);
|
| 383 | int height = destination.getHeight(0);
|
| 384 | if (source.getWidth() == width && source.getHeight() == height) {
|
| 385 | this.writeToTexture(destination, source, 0, 0, 0, 0);
|
| 386 | } else {
|
| 387 | throw new IllegalArgumentException(
|
| 388 | "Cannot replace texture of size " + width + "x" + height + " with image of size " + source.getWidth() + "x" + source.getHeight()
|
| 389 | );
|
| 390 | }
|
| 391 | }
|
| 392 |
|
| 393 | @Override
|
| 394 | public void writeToTexture(
|
| 395 | final GpuTexture destination, final NativeImage source, final int mipLevel, final int depthOrLayer, final int destX, final int destY
|
| 396 | ) {
|
| 397 | if (this.isInRenderPass) {
|
| 398 | throw new IllegalStateException("Close the existing render pass before performing additional commands");
|
| 399 | }
|
| 400 |
|
| 401 | if (destination.getFormat().componentType() != GpuFormat.ComponentType.UNORM_8) {
|
| 402 | throw new IllegalArgumentException("Destination texture for NativeImage writes must have component type of UNORM_8");
|
| 403 | }
|
| 404 |
|
| 405 | if (destination.getFormat().componentCount() != source.format().components()) {
|
| 406 | throw new IllegalArgumentException(
|
| 407 | "Destination(" + destination.getFormat() + ") texture for NativeImage(" + source.format() + ") write must have channel count matching source"
|
| 408 | );
|
| 409 | }
|
| 410 |
|
| 411 | if (mipLevel >= 0 && mipLevel < destination.getMipLevels()) {
|
| 412 | if (destX + source.getWidth() > destination.getWidth(mipLevel) || destY + source.getHeight() > destination.getHeight(mipLevel)) {
|
| 413 | throw new IllegalArgumentException(
|
| 414 | "Dest texture ("
|
| 415 | + source.getWidth()
|
| 416 | + "x"
|
| 417 | + source.getHeight()
|
| 418 | + ") is not large enough to write a rectangle of "
|
| 419 | + source.getWidth()
|
| 420 | + "x"
|
| 421 | + source.getHeight()
|
| 422 | + " at "
|
| 423 | + destX
|
| 424 | + "x"
|
| 425 | + destY
|
| 426 | + " (at mip level "
|
| 427 | + mipLevel
|
| 428 | + ")"
|
| 429 | );
|
| 430 | }
|
| 431 |
|
| 432 | if (destination.isClosed()) {
|
| 433 | throw new IllegalStateException("Destination texture is closed");
|
| 434 | }
|
| 435 |
|
| 436 | if ((destination.usage() & 1) == 0) {
|
| 437 | throw new IllegalStateException("Color texture must have USAGE_COPY_DST to be a destination for a write");
|
| 438 | }
|
| 439 |
|
| 440 | if (depthOrLayer >= destination.getDepthOrLayers()) {
|
| 441 | throw new UnsupportedOperationException("Depth or layer is out of range, must be >= 0 and < " + destination.getDepthOrLayers());
|
| 442 | }
|
| 443 |
|
| 444 | this.writeToTexture(destination, source.getPixelBytes(), mipLevel, depthOrLayer, destX, destY, source.getWidth(), source.getHeight());
|
| 445 | } else {
|
| 446 | throw new IllegalArgumentException("Invalid mipLevel " + mipLevel + ", must be >= 0 and < " + destination.getMipLevels());
|
| 447 | }
|
| 448 | }
|
| 449 |
|
| 450 | @Override
|
| 451 | public void writeToTexture(
|
| 452 | final GpuTexture destination,
|
| 453 | final ByteBuffer source,
|
| 454 | final int mipLevel,
|
| 455 | final int depthOrLayer,
|
| 456 | final int destX,
|
| 457 | final int destY,
|
| 458 | final int width,
|
| 459 | final int height
|
| 460 | ) {
|
| 461 | if (this.isInRenderPass) {
|
| 462 | throw new IllegalStateException("Close the existing render pass before performing additional commands");
|
| 463 | }
|
| 464 |
|
| 465 | if (mipLevel >= 0 && mipLevel < destination.getMipLevels()) {
|
| 466 | if (width * height * destination.getFormat().blockSize() > source.remaining()) {
|
| 467 | throw new IllegalArgumentException(
|
| 468 | "Copy would overrun the source buffer (remaining length of "
|
| 469 | + source.remaining()
|
| 470 | + ", but copy is "
|
| 471 | + width
|
| 472 | + "x"
|
| 473 | + height
|
| 474 | + " of format "
|
| 475 | + destination.getFormat()
|
| 476 | + ")"
|
| 477 | );
|
| 478 | }
|
| 479 |
|
| 480 | if (destX + width > destination.getWidth(mipLevel) || destY + height > destination.getHeight(mipLevel)) {
|
| 481 | throw new IllegalArgumentException(
|
| 482 | "Dest texture ("
|
| 483 | + destination.getWidth(mipLevel)
|
| 484 | + "x"
|
| 485 | + destination.getHeight(mipLevel)
|
| 486 | + ") is not large enough to write a rectangle of "
|
| 487 | + width
|
| 488 | + "x"
|
| 489 | + height
|
| 490 | + " at "
|
| 491 | + destX
|
| 492 | + "x"
|
| 493 | + destY
|
| 494 | );
|
| 495 | }
|
| 496 |
|
| 497 | if (destination.isClosed()) {
|
| 498 | throw new IllegalStateException("Destination texture is closed");
|
| 499 | }
|
| 500 |
|
| 501 | if ((destination.usage() & 1) == 0) {
|
| 502 | throw new IllegalStateException("Color texture must have USAGE_COPY_DST to be a destination for a write");
|
| 503 | }
|
| 504 |
|
| 505 | if (depthOrLayer >= destination.getDepthOrLayers()) {
|
| 506 | throw new UnsupportedOperationException("Depth or layer is out of range, must be >= 0 and < " + destination.getDepthOrLayers());
|
| 507 | }
|
| 508 |
|
| 509 | this.backend.writeToTexture(destination, source, mipLevel, depthOrLayer, destX, destY, width, height);
|
| 510 | } else {
|
| 511 | throw new IllegalArgumentException("Invalid mipLevel, must be >= 0 and < " + destination.getMipLevels());
|
| 512 | }
|
| 513 | }
|
| 514 |
|
| 515 | @Override
|
| 516 | public void copyBufferToTexture(
|
| 517 | final GpuBufferSlice source,
|
| 518 | final int sourceX,
|
| 519 | final int sourceY,
|
| 520 | final int sourceWidth,
|
| 521 | final int sourceHeight,
|
| 522 | final GpuTexture destination,
|
| 523 | final int destinationX,
|
| 524 | final int destinationY,
|
| 525 | final int copyWidth,
|
| 526 | final int copyHeight,
|
| 527 | final int mipLevel,
|
| 528 | final int arrayLayer
|
| 529 | ) {
|
| 530 | if (this.isInRenderPass) {
|
| 531 | throw new IllegalStateException("Close the existing render pass before performing additional commands");
|
| 532 | }
|
| 533 |
|
| 534 | if (mipLevel >= 0 && mipLevel < destination.getMipLevels()) {
|
| 535 | if (sourceX + copyWidth > sourceWidth || sourceY + copyHeight > sourceHeight) {
|
| 536 | throw new IllegalArgumentException(
|
| 537 | "Copy source ("
|
| 538 | + sourceWidth
|
| 539 | + "x"
|
| 540 | + sourceHeight
|
| 541 | + ") is not large enough to read a rectangle of "
|
| 542 | + copyWidth
|
| 543 | + "x"
|
| 544 | + copyHeight
|
| 545 | + " from "
|
| 546 | + sourceX
|
| 547 | + "x"
|
| 548 | + sourceY
|
| 549 | );
|
| 550 | }
|
| 551 |
|
| 552 | if ((long)sourceWidth * copyHeight * destination.getFormat().blockSize() > source.length()) {
|
| 553 | throw new IllegalArgumentException(
|
| 554 | "Copy would overrun the source buffer (remaining length of "
|
| 555 | + source.length()
|
| 556 | + ", but copy is "
|
| 557 | + copyWidth
|
| 558 | + "x"
|
| 559 | + copyHeight
|
| 560 | + " of format "
|
| 561 | + destination.getFormat()
|
| 562 | + ")"
|
| 563 | );
|
| 564 | }
|
| 565 |
|
| 566 | if (destinationX + copyWidth > destination.getWidth(mipLevel) || destinationY + copyHeight > destination.getHeight(mipLevel)) {
|
| 567 | throw new IllegalArgumentException(
|
| 568 | "Dest texture ("
|
| 569 | + destination.getWidth(mipLevel)
|
| 570 | + "x"
|
| 571 | + destination.getHeight(mipLevel)
|
| 572 | + ") is not large enough to write a rectangle of "
|
| 573 | + copyWidth
|
| 574 | + "x"
|
| 575 | + copyHeight
|
| 576 | + " at "
|
| 577 | + destinationX
|
| 578 | + "x"
|
| 579 | + destinationY
|
| 580 | );
|
| 581 | }
|
| 582 |
|
| 583 | if (source.buffer().isClosed()) {
|
| 584 | throw new IllegalStateException("Source buffer is closed");
|
| 585 | }
|
| 586 |
|
| 587 | if ((source.buffer().usage() & 16) == 0) {
|
| 588 | throw new IllegalStateException("Source buffer must have USAGE_COPY_SRC to be a source for a read");
|
| 589 | }
|
| 590 |
|
| 591 | if (destination.isClosed()) {
|
| 592 | throw new IllegalStateException("Destination texture is closed");
|
| 593 | }
|
| 594 |
|
| 595 | if ((destination.usage() & 1) == 0) {
|
| 596 | throw new IllegalStateException("Color texture must have USAGE_COPY_DST to be a destination for a write");
|
| 597 | }
|
| 598 |
|
| 599 | if (arrayLayer >= destination.getDepthOrLayers()) {
|
| 600 | throw new UnsupportedOperationException("Depth or layer is out of range, must be >= 0 and < " + destination.getDepthOrLayers());
|
| 601 | }
|
| 602 |
|
| 603 | this.backend
|
| 604 | .copyBufferToTexture(
|
| 605 | source, sourceX, sourceY, sourceWidth, sourceHeight, destination, destinationX, destinationY, copyWidth, copyHeight, mipLevel, arrayLayer
|
| 606 | );
|
| 607 | } else {
|
| 608 | throw new IllegalArgumentException("Invalid mipLevel, must be >= 0 and < " + destination.getMipLevels());
|
| 609 | }
|
| 610 | }
|
| 611 |
|
| 612 | @Override
|
| 613 | public void copyTextureToBuffer(final GpuTexture source, final GpuBuffer destination, final long offset, final Runnable callback, final int mipLevel) {
|
| 614 | if (this.isInRenderPass) {
|
| 615 | throw new IllegalStateException("Close the existing render pass before performing additional commands");
|
| 616 | }
|
| 617 |
|
| 618 | this.backend.copyTextureToBuffer(source, destination, offset, callback, mipLevel);
|
| 619 | }
|
| 620 |
|
| 621 | @Override
|
| 622 | public void copyTextureToBuffer(
|
| 623 | final GpuTexture source,
|
| 624 | final GpuBuffer destination,
|
| 625 | final long offset,
|
| 626 | final Runnable callback,
|
| 627 | final int mipLevel,
|
| 628 | final int x,
|
| 629 | final int y,
|
| 630 | final int width,
|
| 631 | final int height
|
| 632 | ) {
|
| 633 | if (this.isInRenderPass) {
|
| 634 | throw new IllegalStateException("Close the existing render pass before performing additional commands");
|
| 635 | }
|
| 636 |
|
| 637 | if (mipLevel >= 0 && mipLevel < source.getMipLevels()) {
|
| 638 | if ((long)width * height * source.getFormat().blockSize() + offset > destination.size()) {
|
| 639 | throw new IllegalArgumentException(
|
| 640 | "Buffer of size "
|
| 641 | + destination.size()
|
| 642 | + " is not large enough to hold "
|
| 643 | + width
|
| 644 | + "x"
|
| 645 | + height
|
| 646 | + " pixels ("
|
| 647 | + source.getFormat().blockSize()
|
| 648 | + " bytes each) starting from offset "
|
| 649 | + offset
|
| 650 | );
|
| 651 | }
|
| 652 |
|
| 653 | if ((source.usage() & 2) == 0) {
|
| 654 | throw new IllegalArgumentException("Texture needs USAGE_COPY_SRC to be a source for a copy");
|
| 655 | }
|
| 656 |
|
| 657 | if ((destination.usage() & 8) == 0) {
|
| 658 | throw new IllegalArgumentException("Buffer needs USAGE_COPY_DST to be a destination for a copy");
|
| 659 | }
|
| 660 |
|
| 661 | if (x + width > source.getWidth(mipLevel) || y + height > source.getHeight(mipLevel)) {
|
| 662 | throw new IllegalArgumentException(
|
| 663 | "Copy source texture ("
|
| 664 | + source.getWidth(mipLevel)
|
| 665 | + "x"
|
| 666 | + source.getHeight(mipLevel)
|
| 667 | + ") is not large enough to read a rectangle of "
|
| 668 | + width
|
| 669 | + "x"
|
| 670 | + height
|
| 671 | + " from "
|
| 672 | + x
|
| 673 | + ","
|
| 674 | + y
|
| 675 | );
|
| 676 | }
|
| 677 |
|
| 678 | if (source.isClosed()) {
|
| 679 | throw new IllegalStateException("Source texture is closed");
|
| 680 | }
|
| 681 |
|
| 682 | if (destination.isClosed()) {
|
| 683 | throw new IllegalStateException("Destination buffer is closed");
|
| 684 | }
|
| 685 |
|
| 686 | if (source.getDepthOrLayers() > 1) {
|
| 687 | throw new UnsupportedOperationException("Textures with multiple depths or layers are not yet supported for copying");
|
| 688 | }
|
| 689 |
|
| 690 | this.backend.copyTextureToBuffer(source, destination, offset, callback, mipLevel, x, y, width, height);
|
| 691 | } else {
|
| 692 | throw new IllegalArgumentException("Invalid mipLevel " + mipLevel + ", must be >= 0 and < " + source.getMipLevels());
|
| 693 | }
|
| 694 | }
|
| 695 |
|
| 696 | @Override
|
| 697 | public void copyTextureToTexture(
|
| 698 | final GpuTexture source,
|
| 699 | final GpuTexture destination,
|
| 700 | final int mipLevel,
|
| 701 | final int destX,
|
| 702 | final int destY,
|
| 703 | final int sourceX,
|
| 704 | final int sourceY,
|
| 705 | final int width,
|
| 706 | final int height
|
| 707 | ) {
|
| 708 | if (this.isInRenderPass) {
|
| 709 | throw new IllegalStateException("Close the existing render pass before performing additional commands");
|
| 710 | }
|
| 711 |
|
| 712 | if (mipLevel >= 0 && mipLevel < source.getMipLevels() && mipLevel < destination.getMipLevels()) {
|
| 713 | if (destX + width > destination.getWidth(mipLevel) || destY + height > destination.getHeight(mipLevel)) {
|
| 714 | throw new IllegalArgumentException(
|
| 715 | "Dest texture ("
|
| 716 | + destination.getWidth(mipLevel)
|
| 717 | + "x"
|
| 718 | + destination.getHeight(mipLevel)
|
| 719 | + ") is not large enough to write a rectangle of "
|
| 720 | + width
|
| 721 | + "x"
|
| 722 | + height
|
| 723 | + " at "
|
| 724 | + destX
|
| 725 | + "x"
|
| 726 | + destY
|
| 727 | );
|
| 728 | }
|
| 729 |
|
| 730 | if (sourceX + width > source.getWidth(mipLevel) || sourceY + height > source.getHeight(mipLevel)) {
|
| 731 | throw new IllegalArgumentException(
|
| 732 | "Source texture ("
|
| 733 | + source.getWidth(mipLevel)
|
| 734 | + "x"
|
| 735 | + source.getHeight(mipLevel)
|
| 736 | + ") is not large enough to read a rectangle of "
|
| 737 | + width
|
| 738 | + "x"
|
| 739 | + height
|
| 740 | + " at "
|
| 741 | + sourceX
|
| 742 | + "x"
|
| 743 | + sourceY
|
| 744 | );
|
| 745 | }
|
| 746 |
|
| 747 | if (source.isClosed()) {
|
| 748 | throw new IllegalStateException("Source texture is closed");
|
| 749 | }
|
| 750 |
|
| 751 | if (destination.isClosed()) {
|
| 752 | throw new IllegalStateException("Destination texture is closed");
|
| 753 | }
|
| 754 |
|
| 755 | if ((source.usage() & 2) == 0) {
|
| 756 | throw new IllegalArgumentException("Texture needs USAGE_COPY_SRC to be a source for a copy");
|
| 757 | }
|
| 758 |
|
| 759 | if ((destination.usage() & 1) == 0) {
|
| 760 | throw new IllegalArgumentException("Texture needs USAGE_COPY_DST to be a destination for a copy");
|
| 761 | }
|
| 762 |
|
| 763 | if (source.getDepthOrLayers() > 1) {
|
| 764 | throw new UnsupportedOperationException("Textures with multiple depths or layers are not yet supported for copying");
|
| 765 | }
|
| 766 |
|
| 767 | if (destination.getDepthOrLayers() > 1) {
|
| 768 | throw new UnsupportedOperationException("Textures with multiple depths or layers are not yet supported for copying");
|
| 769 | }
|
| 770 |
|
| 771 | this.backend.copyTextureToTexture(source, destination, mipLevel, destX, destY, sourceX, sourceY, width, height);
|
| 772 | } else {
|
| 773 | throw new IllegalArgumentException("Invalid mipLevel " + mipLevel + ", must be >= 0 and < " + source.getMipLevels() + " and < " + destination.getMipLevels());
|
| 774 | }
|
| 775 | }
|
| 776 |
|
| 777 | @Override
|
| 778 | public GpuFence createFence() {
|
| 779 | if (this.isInRenderPass) {
|
| 780 | throw new IllegalStateException("Close the existing render pass before performing additional commands");
|
| 781 | } else {
|
| 782 | return this.backend.createFence();
|
| 783 | }
|
| 784 | }
|
| 785 |
|
| 786 | @Override
|
| 787 | public void writeTimestamp(final GpuQueryPool pool, final int index) {
|
| 788 | if (index >= 0 && index <= pool.size()) {
|
| 789 | this.backend.writeTimestamp(pool, index);
|
| 790 | } else {
|
| 791 | throw new IllegalStateException("Index " + index + " is out of range for query pool of size " + pool.size());
|
| 792 | }
|
| 793 | }
|
| 794 |
|
| 795 | private void verifyColorTexture(final GpuTexture colorTexture) {
|
| 796 | if (!colorTexture.getFormat().hasColorAspect()) {
|
| 797 | throw new IllegalStateException("Trying to clear a non-color texture as color");
|
| 798 | }
|
| 799 |
|
| 800 | if (colorTexture.isClosed()) {
|
| 801 | throw new IllegalStateException("Color texture is closed");
|
| 802 | }
|
| 803 |
|
| 804 | if ((colorTexture.usage() & 8) == 0) {
|
| 805 | throw new IllegalStateException("Color texture must have USAGE_RENDER_ATTACHMENT");
|
| 806 | }
|
| 807 |
|
| 808 | if ((colorTexture.usage() & 1) == 0) {
|
| 809 | throw new IllegalStateException("Color texture must have USAGE_COPY_DST");
|
| 810 | }
|
| 811 |
|
| 812 | if (colorTexture.getDepthOrLayers() > 1) {
|
| 813 | throw new UnsupportedOperationException("Clearing a texture with multiple layers or depths is not yet supported");
|
| 814 | }
|
| 815 | }
|
| 816 |
|
| 817 | private void verifyDepthTexture(final GpuTexture depthTexture) {
|
| 818 | if (!depthTexture.getFormat().hasDepthAspect()) {
|
| 819 | throw new IllegalStateException("Trying to clear a non-depth texture as depth");
|
| 820 | }
|
| 821 |
|
| 822 | if (depthTexture.isClosed()) {
|
| 823 | throw new IllegalStateException("Depth texture is closed");
|
| 824 | }
|
| 825 |
|
| 826 | if ((depthTexture.usage() & 8) == 0) {
|
| 827 | throw new IllegalStateException("Depth texture must have USAGE_RENDER_ATTACHMENT");
|
| 828 | }
|
| 829 |
|
| 830 | if ((depthTexture.usage() & 1) == 0) {
|
| 831 | throw new IllegalStateException("Depth texture must have USAGE_COPY_DST");
|
| 832 | }
|
| 833 |
|
| 834 | if (depthTexture.getDepthOrLayers() > 1) {
|
| 835 | throw new UnsupportedOperationException("Clearing a texture with multiple layers or depths is not yet supported");
|
| 836 | }
|
| 837 | }
|
| 838 |
|
| 839 | private void verifyRegion(
|
| 840 | final GpuTexture colorTexture, final int regionX, final int regionY, final int regionWidth, final int regionHeight, final int mipLevel
|
| 841 | ) {
|
| 842 | if (mipLevel >= colorTexture.getMipLevels()) {
|
| 843 | throw new IllegalArgumentException("Mip level " + mipLevel + " out of range for texture with " + colorTexture.getMipLevels() + " mips");
|
| 844 | }
|
| 845 |
|
| 846 | if (regionX < 0 || regionX >= colorTexture.getWidth(mipLevel)) {
|
| 847 | throw new IllegalArgumentException("regionX should not be outside of the texture");
|
| 848 | }
|
| 849 |
|
| 850 | if (regionY < 0 || regionY >= colorTexture.getHeight(mipLevel)) {
|
| 851 | throw new IllegalArgumentException("regionY should not be outside of the texture");
|
| 852 | }
|
| 853 |
|
| 854 | if (regionWidth <= 0) {
|
| 855 | throw new IllegalArgumentException("regionWidth should be greater than 0");
|
| 856 | }
|
| 857 |
|
| 858 | if (regionX + regionWidth > colorTexture.getWidth(mipLevel)) {
|
| 859 | throw new IllegalArgumentException("regionWidth + regionX should be less than the texture width");
|
| 860 | }
|
| 861 |
|
| 862 | if (regionHeight <= 0) {
|
| 863 | throw new IllegalArgumentException("regionHeight should be greater than 0");
|
| 864 | }
|
| 865 |
|
| 866 | if (regionY + regionHeight > colorTexture.getHeight(mipLevel)) {
|
| 867 | throw new IllegalArgumentException("regionWidth + regionX should be less than the texture height");
|
| 868 | }
|
| 869 | }
|
| 870 | }
|
| 871 |
|