| 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.buffers.TransientMemory;
|
| 6 | import com.mojang.renderpearl.api.commands.GpuFence;
|
| 7 | import com.mojang.renderpearl.api.commands.GpuQueryPool;
|
| 8 | import com.mojang.renderpearl.api.commands.RenderPass;
|
| 9 | import com.mojang.renderpearl.api.commands.RenderPassDescriptor;
|
| 10 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 11 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 12 | import com.mojang.renderpearl.backend.api.CommandEncoderBackend;
|
| 13 | import com.mojang.renderpearl.backend.api.RenderPassBackend;
|
| 14 | import com.mojang.renderpearl.backend.vulkan.checkpoints.CheckpointExtension;
|
| 15 | import java.nio.ByteBuffer;
|
| 16 | import java.nio.LongBuffer;
|
| 17 | import java.util.List;
|
| 18 | import java.util.Optional;
|
| 19 | import java.util.OptionalDouble;
|
| 20 | import net.fabricmc.api.EnvType;
|
| 21 | import net.fabricmc.api.Environment;
|
| 22 | import org.joml.Vector4fc;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 | import org.lwjgl.system.MemoryStack;
|
| 25 | import org.lwjgl.vulkan.KHRDynamicRendering;
|
| 26 | import org.lwjgl.vulkan.KHRSynchronization2;
|
| 27 | import org.lwjgl.vulkan.VK12;
|
| 28 | import org.lwjgl.vulkan.VkBufferCopy;
|
| 29 | import org.lwjgl.vulkan.VkBufferImageCopy;
|
| 30 | import org.lwjgl.vulkan.VkClearAttachment;
|
| 31 | import org.lwjgl.vulkan.VkClearColorValue;
|
| 32 | import org.lwjgl.vulkan.VkClearDepthStencilValue;
|
| 33 | import org.lwjgl.vulkan.VkClearRect;
|
| 34 | import org.lwjgl.vulkan.VkClearValue;
|
| 35 | import org.lwjgl.vulkan.VkCommandBuffer;
|
| 36 | import org.lwjgl.vulkan.VkCommandBufferBeginInfo;
|
| 37 | import org.lwjgl.vulkan.VkDependencyInfo;
|
| 38 | import org.lwjgl.vulkan.VkImageCopy;
|
| 39 | import org.lwjgl.vulkan.VkImageSubresourceLayers;
|
| 40 | import org.lwjgl.vulkan.VkImageSubresourceRange;
|
| 41 | import org.lwjgl.vulkan.VkMemoryBarrier2;
|
| 42 | import org.lwjgl.vulkan.VkRect2D;
|
| 43 | import org.lwjgl.vulkan.VkRenderingAttachmentInfo;
|
| 44 | import org.lwjgl.vulkan.VkRenderingInfo;
|
| 45 | import org.lwjgl.vulkan.VkSemaphoreCreateInfo;
|
| 46 | import org.lwjgl.vulkan.VkSemaphoreTypeCreateInfo;
|
| 47 | import org.lwjgl.vulkan.VkSemaphoreWaitInfo;
|
| 48 | import org.lwjgl.vulkan.VkMemoryBarrier2.Buffer;
|
| 49 |
|
| 50 | @Environment(EnvType.CLIENT)
|
| 51 | public class VulkanCommandEncoder implements CommandEncoderBackend, Destroyable {
|
| 52 | public static final int MAX_SUBMITS_IN_FLIGHT = 2;
|
| 53 | private final VulkanDevice device;
|
| 54 | private final VulkanTransientMemory transientMemory;
|
| 55 | private final long submitSemaphore;
|
| 56 | private long currentSubmitIndex = 2L;
|
| 57 | private long completedSubmitIndex = 0L;
|
| 58 | private final CheckpointExtension.CheckpointStorage checkpointStorage;
|
| 59 | private VulkanQueue.Submission submissionBuilder;
|
| 60 | private final DestructionQueue<Destroyable> destroyQueue = new DestructionQueue<>(2, Destroyable::destroy);
|
| 61 | private final VulkanCommandPool[] commandPools = new VulkanCommandPool[2];
|
| 62 | private @Nullable VkCommandBuffer currentInitCommandBuffer;
|
| 63 | private @Nullable VkCommandBuffer currentCommandBuffer;
|
| 64 | private @Nullable VulkanRenderPass currentRenderPass;
|
| 65 |
|
| 66 | public VulkanCommandEncoder(final VulkanDevice device) {
|
| 67 | this.device = device;
|
| 68 | this.transientMemory = new VulkanTransientMemory(device, this);
|
| 69 | MemoryStack baseStack = MemoryStack.stackGet();
|
| 70 |
|
| 71 | try (MemoryStack stack = baseStack.push()) {
|
| 72 | VkSemaphoreTypeCreateInfo semaphoreTypeCreateInfo = VkSemaphoreTypeCreateInfo.calloc(stack).sType$Default();
|
| 73 | semaphoreTypeCreateInfo.semaphoreType(1);
|
| 74 | semaphoreTypeCreateInfo.initialValue(this.currentSubmitIndex - 1L);
|
| 75 | VkSemaphoreCreateInfo semaphoreCreateInfo = VkSemaphoreCreateInfo.calloc(stack).sType$Default();
|
| 76 | semaphoreCreateInfo.pNext(semaphoreTypeCreateInfo);
|
| 77 | LongBuffer semaphoreHandlePtr = stack.callocLong(1);
|
| 78 | VulkanUtils.crashIfFailure(
|
| 79 | device, VK12.vkCreateSemaphore(device.vkDevice(), semaphoreCreateInfo, null, semaphoreHandlePtr), "Failed to create submit VkSemaphore"
|
| 80 | );
|
| 81 | this.submitSemaphore = semaphoreHandlePtr.get(0);
|
| 82 | }
|
| 83 |
|
| 84 | for (int i = 0; i < 2; i++) {
|
| 85 | this.commandPools[i] = new VulkanCommandPool(device, device.graphicsQueue());
|
| 86 | }
|
| 87 |
|
| 88 | this.checkpointStorage = device.checkpointExtension().createStorage(device, device.graphicsQueue(), 2);
|
| 89 | this.submissionBuilder = device.graphicsQueue().beginSubmit();
|
| 90 | this.transientMemory.beginSubmit();
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public void destroy() {
|
| 95 | this.transientMemory.endSubmit();
|
| 96 | this.submissionBuilder.close();
|
| 97 | this.device.graphicsQueue().waitIdle();
|
| 98 | this.destroyQueue.close();
|
| 99 | this.transientMemory.destroy();
|
| 100 | this.destroyQueue.close();
|
| 101 |
|
| 102 | for (int i = 0; i < 2; i++) {
|
| 103 | this.commandPools[i].destroy();
|
| 104 | }
|
| 105 |
|
| 106 | VK12.vkDestroySemaphore(this.device.vkDevice(), this.submitSemaphore, null);
|
| 107 | }
|
| 108 |
|
| 109 | public void queueForDestroy(final Destroyable destroyable) {
|
| 110 | this.destroyQueue.add(destroyable);
|
| 111 | }
|
| 112 |
|
| 113 | private VulkanCommandPool currentCommandPool() {
|
| 114 | return this.commandPools[(int)(this.currentSubmitIndex % 2L)];
|
| 115 | }
|
| 116 |
|
| 117 | public VkCommandBuffer allocateAndBeginTransientCommandBuffer() {
|
| 118 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 119 | VkCommandBuffer commandBuffer = this.currentCommandPool().allocateBuffer();
|
| 120 | VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo.calloc(stack).sType$Default();
|
| 121 | beginInfo.flags(1);
|
| 122 | VulkanUtils.crashIfFailure(this.device, VK12.vkBeginCommandBuffer(commandBuffer, beginInfo), "Failed to begin VkCommandBuffer");
|
| 123 | return commandBuffer;
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | private VkCommandBuffer commandBuffer() {
|
| 128 | if (this.currentCommandBuffer != null) {
|
| 129 | return this.currentCommandBuffer;
|
| 130 | }
|
| 131 |
|
| 132 | if (this.currentRenderPass != null) {
|
| 133 | throw new IllegalStateException("Cannot start command buffer while inside RenderPass");
|
| 134 | }
|
| 135 |
|
| 136 | this.currentCommandBuffer = this.allocateAndBeginTransientCommandBuffer();
|
| 137 | return this.currentCommandBuffer;
|
| 138 | }
|
| 139 |
|
| 140 | VkCommandBuffer objectInitCommandBuffer() {
|
| 141 | if (this.currentInitCommandBuffer != null) {
|
| 142 | return this.currentInitCommandBuffer;
|
| 143 | }
|
| 144 |
|
| 145 | this.currentInitCommandBuffer = this.allocateAndBeginTransientCommandBuffer();
|
| 146 | return this.currentInitCommandBuffer;
|
| 147 | }
|
| 148 |
|
| 149 | private void endCommandBuffer() {
|
| 150 | if (this.currentCommandBuffer != null || this.currentInitCommandBuffer != null) {
|
| 151 | if (this.currentRenderPass != null) {
|
| 152 | throw new IllegalStateException("Cannot end command buffer while inside RenderPass");
|
| 153 | }
|
| 154 |
|
| 155 | if (this.currentInitCommandBuffer != null) {
|
| 156 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 157 | memoryBarrier(this.currentInitCommandBuffer, stack);
|
| 158 | }
|
| 159 |
|
| 160 | VulkanUtils.crashIfFailure(this.device, VK12.vkEndCommandBuffer(this.currentInitCommandBuffer), "Failed to end VkCommandBuffer");
|
| 161 | this.submissionBuilder.executeCommands(this.currentInitCommandBuffer);
|
| 162 | this.currentInitCommandBuffer = null;
|
| 163 | }
|
| 164 |
|
| 165 | if (this.currentCommandBuffer != null) {
|
| 166 | VulkanUtils.crashIfFailure(this.device, VK12.vkEndCommandBuffer(this.currentCommandBuffer), "Failed to end VkCommandBuffer");
|
| 167 | this.submissionBuilder.executeCommands(this.currentCommandBuffer);
|
| 168 | this.currentCommandBuffer = null;
|
| 169 | }
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | public void waitSemaphore(final long vkSemaphore, final long value, final long stageMask) {
|
| 174 | if (this.currentRenderPass != null) {
|
| 175 | throw new IllegalStateException("Cannot add semaphore operation while inside RenderPass");
|
| 176 | }
|
| 177 |
|
| 178 | this.endCommandBuffer();
|
| 179 | this.submissionBuilder.waitSemaphore(vkSemaphore, value, stageMask);
|
| 180 | }
|
| 181 |
|
| 182 | public void execute(final VkCommandBuffer commandBuffer) {
|
| 183 | if (this.currentRenderPass != null) {
|
| 184 | throw new IllegalStateException("Cannot execute command buffer while inside RenderPass");
|
| 185 | }
|
| 186 |
|
| 187 | this.endCommandBuffer();
|
| 188 | this.submissionBuilder.executeCommands(commandBuffer);
|
| 189 | }
|
| 190 |
|
| 191 | public void signalSemaphore(final long vkSemaphore, final long value, final long stageMask) {
|
| 192 | if (this.currentRenderPass != null) {
|
| 193 | throw new IllegalStateException("Cannot add semaphore operation while inside RenderPass");
|
| 194 | }
|
| 195 |
|
| 196 | this.endCommandBuffer();
|
| 197 | this.submissionBuilder.signalSemaphore(vkSemaphore, value, stageMask);
|
| 198 | }
|
| 199 |
|
| 200 | private void memoryBarrier(final MemoryStack stack) {
|
| 201 | memoryBarrier(this.commandBuffer(), stack);
|
| 202 | }
|
| 203 |
|
| 204 | public static void memoryBarrier(final VkCommandBuffer commandBuffer, final MemoryStack stack) {
|
| 205 | Buffer memoryBarrier = VkMemoryBarrier2.calloc(1, stack).sType$Default();
|
| 206 | memoryBarrier.srcStageMask(65536L);
|
| 207 | memoryBarrier.srcAccessMask(98304L);
|
| 208 | memoryBarrier.dstStageMask(65536L);
|
| 209 | memoryBarrier.dstAccessMask(98304L);
|
| 210 | VkDependencyInfo depInfo = VkDependencyInfo.calloc(stack).sType$Default();
|
| 211 | depInfo.pMemoryBarriers(memoryBarrier);
|
| 212 | KHRSynchronization2.vkCmdPipelineBarrier2KHR(commandBuffer, depInfo);
|
| 213 | }
|
| 214 |
|
| 215 | @Override
|
| 216 | public void submit() {
|
| 217 | this.endCommandBuffer();
|
| 218 | this.transientMemory.endSubmit();
|
| 219 | this.signalSemaphore(this.submitSemaphore, this.currentSubmitIndex, 65536L);
|
| 220 | this.submissionBuilder.close();
|
| 221 | this.submissionBuilder = this.device.graphicsQueue().beginSubmit();
|
| 222 | this.currentSubmitIndex++;
|
| 223 | if (!this.awaitSubmitCompletion(this.currentSubmitIndex - 2L, 5000000000L)) {
|
| 224 | List<CheckpointExtension.QueueCheckpoints> checkpoints = this.device.checkpointExtension().retrieveCheckpoints(false);
|
| 225 | throw new IllegalStateException("5s timeout reached when waiting for VK semaphore: " + VulkanUtils.formatCheckpoints(checkpoints));
|
| 226 | }
|
| 227 |
|
| 228 | this.currentCommandPool().reset();
|
| 229 | this.destroyQueue.rotate();
|
| 230 | this.checkpointStorage.rotate();
|
| 231 | this.transientMemory.beginSubmit();
|
| 232 | }
|
| 233 |
|
| 234 | @Override
|
| 235 | public TransientMemory transientMemory() {
|
| 236 | return this.transientMemory;
|
| 237 | }
|
| 238 |
|
| 239 | @Override
|
| 240 | public RenderPassBackend createRenderPass(final RenderPassDescriptor descriptor) {
|
| 241 | List<RenderPassDescriptor.Attachment<Optional<Vector4fc>>> colorAttachments = descriptor.colorAttachments();
|
| 242 | VulkanGpuTextureView[] colorTextures = new VulkanGpuTextureView[colorAttachments.size()];
|
| 243 |
|
| 244 | for (int i = 0; i < colorAttachments.size(); i++) {
|
| 245 | RenderPassDescriptor.Attachment<Optional<Vector4fc>> attachment = colorAttachments.get(i);
|
| 246 | colorTextures[i] = attachment != null ? (VulkanGpuTextureView)attachment.textureView() : null;
|
| 247 | }
|
| 248 |
|
| 249 | RenderPassDescriptor.Attachment<OptionalDouble> depthAttachment = descriptor.depthAttachment();
|
| 250 | this.device.instance().debug().beginDebugGroup(this.commandBuffer(), descriptor.label());
|
| 251 | this.checkpointStorage.recordCheckpoint(this.commandBuffer(), CheckpointExtension.CheckpointType.BEGIN_RENDER_PASS, descriptor.label());
|
| 252 |
|
| 253 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 254 | int width = 0;
|
| 255 | int height = 0;
|
| 256 | if (!colorAttachments.isEmpty()) {
|
| 257 | for (RenderPassDescriptor.Attachment<Optional<Vector4fc>> colorAttachment : colorAttachments) {
|
| 258 | if (colorAttachment != null) {
|
| 259 | GpuTextureView colorTexture = colorAttachment.textureView();
|
| 260 | width = colorTexture.getWidth(0);
|
| 261 | height = colorTexture.getHeight(0);
|
| 262 | }
|
| 263 | }
|
| 264 | } else if (depthAttachment != null) {
|
| 265 | width = depthAttachment.textureView().getWidth(0);
|
| 266 | height = depthAttachment.textureView().getHeight(0);
|
| 267 | }
|
| 268 |
|
| 269 | VkRect2D vkRenderArea = VkRect2D.calloc(stack);
|
| 270 | RenderPass.RenderArea renderArea = descriptor.renderArea();
|
| 271 | vkRenderArea.extent().set(renderArea.width(), renderArea.height());
|
| 272 | vkRenderArea.offset().set(renderArea.x(), renderArea.y());
|
| 273 | org.lwjgl.vulkan.VkRenderingAttachmentInfo.Buffer colorAttachmentInfo = VkRenderingAttachmentInfo.calloc(colorAttachments.size(), stack);
|
| 274 |
|
| 275 | for (int i = 0; i < colorAttachments.size(); i++) {
|
| 276 | colorAttachmentInfo.position(i).sType$Default();
|
| 277 | VulkanGpuTextureView colorTexture = colorTextures[i];
|
| 278 | if (colorTexture != null) {
|
| 279 | colorAttachmentInfo.imageView(colorTexture.vkImageView());
|
| 280 | colorAttachmentInfo.imageLayout(1);
|
| 281 | colorAttachmentInfo.storeOp(0);
|
| 282 | RenderPassDescriptor.Attachment<Optional<Vector4fc>> attachment = colorAttachments.get(i);
|
| 283 | Optional<Vector4fc> clearValue = attachment.clearValue();
|
| 284 | if (clearValue.isPresent()) {
|
| 285 | Vector4fc color = clearValue.get();
|
| 286 | VkClearColorValue vkClearColor = VulkanUtils.putArgb(VkClearColorValue.calloc(stack), color);
|
| 287 | colorAttachmentInfo.loadOp(1);
|
| 288 | colorAttachmentInfo.clearValue(VkClearValue.calloc(stack).color(vkClearColor));
|
| 289 | } else {
|
| 290 | colorAttachmentInfo.loadOp(0);
|
| 291 | }
|
| 292 | } else {
|
| 293 | colorAttachmentInfo.imageView(0L);
|
| 294 | colorAttachmentInfo.imageLayout(0);
|
| 295 | colorAttachmentInfo.storeOp(1);
|
| 296 | colorAttachmentInfo.loadOp(2);
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | colorAttachmentInfo.position(0);
|
| 301 | VkRenderingInfo renderingInfo = VkRenderingInfo.calloc(stack).sType$Default();
|
| 302 | renderingInfo.renderArea(vkRenderArea);
|
| 303 | renderingInfo.layerCount(1);
|
| 304 | renderingInfo.viewMask(0);
|
| 305 | renderingInfo.pColorAttachments(colorAttachmentInfo);
|
| 306 | if (depthAttachment != null) {
|
| 307 | VkRenderingAttachmentInfo depthAttachmentInfo = VkRenderingAttachmentInfo.calloc(stack).sType$Default();
|
| 308 | VulkanGpuTextureView vulkanDepthAttachment = (VulkanGpuTextureView)depthAttachment.textureView();
|
| 309 | depthAttachmentInfo.imageView(vulkanDepthAttachment.vkImageView());
|
| 310 | depthAttachmentInfo.imageLayout(1);
|
| 311 | depthAttachmentInfo.storeOp(0);
|
| 312 | OptionalDouble clearValue = depthAttachment.clearValue();
|
| 313 | if (clearValue.isPresent()) {
|
| 314 | double color = clearValue.getAsDouble();
|
| 315 | VkClearDepthStencilValue vkClearColor = VkClearDepthStencilValue.calloc(stack).depth((float)color);
|
| 316 | depthAttachmentInfo.loadOp(1);
|
| 317 | depthAttachmentInfo.clearValue(VkClearValue.calloc(stack).depthStencil(vkClearColor));
|
| 318 | } else {
|
| 319 | depthAttachmentInfo.loadOp(0);
|
| 320 | }
|
| 321 |
|
| 322 | renderingInfo.pDepthAttachment(depthAttachmentInfo);
|
| 323 | }
|
| 324 |
|
| 325 | KHRDynamicRendering.vkCmdBeginRenderingKHR(this.commandBuffer(), renderingInfo);
|
| 326 | this.currentRenderPass = new VulkanRenderPass(
|
| 327 | this.device, this, this.commandBuffer(), this.checkpointStorage, renderArea, width, height, depthAttachment != null, descriptor.label()
|
| 328 | );
|
| 329 | }
|
| 330 |
|
| 331 | return this.currentRenderPass;
|
| 332 | }
|
| 333 |
|
| 334 | @Override
|
| 335 | public void submitRenderPass() {
|
| 336 | if (this.currentRenderPass == null) {
|
| 337 | throw new IllegalStateException("Cannot submit a renderpass if one hasn't been started!");
|
| 338 | }
|
| 339 |
|
| 340 | KHRDynamicRendering.vkCmdEndRenderingKHR(this.commandBuffer());
|
| 341 | this.device.instance().debug().endDebugGroup(this.commandBuffer());
|
| 342 | this.checkpointStorage.recordCheckpoint(this.commandBuffer(), CheckpointExtension.CheckpointType.END_RENDER_PASS, this.currentRenderPass.getLabel());
|
| 343 | this.currentRenderPass = null;
|
| 344 |
|
| 345 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 346 | this.memoryBarrier(stack);
|
| 347 | }
|
| 348 | }
|
| 349 |
|
| 350 | private void clearColorTextureUnsynced(final MemoryStack stack, final GpuTexture colorTexture, final Vector4fc clearColor) {
|
| 351 | VkClearColorValue vkClearColor = VulkanUtils.putArgb(VkClearColorValue.calloc(stack), clearColor);
|
| 352 | VkImageSubresourceRange subresourceRange = VkImageSubresourceRange.calloc(stack);
|
| 353 | subresourceRange.baseMipLevel(0);
|
| 354 | subresourceRange.levelCount(colorTexture.getMipLevels());
|
| 355 | subresourceRange.baseArrayLayer(0);
|
| 356 | subresourceRange.layerCount(1);
|
| 357 | subresourceRange.aspectMask(1);
|
| 358 | VK12.vkCmdClearColorImage(this.commandBuffer(), ((VulkanGpuTexture)colorTexture).vkImage(), 1, vkClearColor, subresourceRange);
|
| 359 | }
|
| 360 |
|
| 361 | public void clearDepthTextureUnsynced(final MemoryStack stack, final GpuTexture depthTexture, final double clearDepth) {
|
| 362 | VkClearDepthStencilValue vkClearDepth = VkClearDepthStencilValue.calloc(stack).depth((float)clearDepth);
|
| 363 | VkImageSubresourceRange subresourceRange = VkImageSubresourceRange.calloc(stack);
|
| 364 | subresourceRange.baseMipLevel(0);
|
| 365 | subresourceRange.levelCount(depthTexture.getMipLevels());
|
| 366 | subresourceRange.baseArrayLayer(0);
|
| 367 | subresourceRange.layerCount(1);
|
| 368 | subresourceRange.aspectMask(2);
|
| 369 | VK12.vkCmdClearDepthStencilImage(this.commandBuffer(), ((VulkanGpuTexture)depthTexture).vkImage(), 1, vkClearDepth, subresourceRange);
|
| 370 | }
|
| 371 |
|
| 372 | @Override
|
| 373 | public void clearColorTexture(final GpuTexture colorTexture, final Vector4fc clearColor) {
|
| 374 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 375 | this.clearColorTextureUnsynced(stack, colorTexture, clearColor);
|
| 376 | this.memoryBarrier(stack);
|
| 377 | }
|
| 378 | }
|
| 379 |
|
| 380 | @Override
|
| 381 | public void clearColorAndDepthTextures(final GpuTexture colorTexture, final Vector4fc clearColor, final GpuTexture depthTexture, final double clearDepth) {
|
| 382 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 383 | this.clearColorTextureUnsynced(stack, colorTexture, clearColor);
|
| 384 | this.clearDepthTextureUnsynced(stack, depthTexture, clearDepth);
|
| 385 | this.memoryBarrier(stack);
|
| 386 | }
|
| 387 | }
|
| 388 |
|
| 389 | @Override
|
| 390 | public void clearColorAndDepthTextures(
|
| 391 | final GpuTexture colorTexture,
|
| 392 | final Vector4fc clearColor,
|
| 393 | final GpuTexture depthTexture,
|
| 394 | final double clearDepth,
|
| 395 | final int regionX,
|
| 396 | final int regionY,
|
| 397 | final int regionWidth,
|
| 398 | final int regionHeight,
|
| 399 | final int mipLevel
|
| 400 | ) {
|
| 401 | try (
|
| 402 | GpuTextureView colorTextureView = this.device.createTextureView(colorTexture, mipLevel, 1);
|
| 403 | GpuTextureView depthTextureView = this.device.createTextureView(depthTexture, mipLevel, 1);
|
| 404 | MemoryStack stack = MemoryStack.stackPush();
|
| 405 | ) {
|
| 406 | this.createRenderPass(
|
| 407 | RenderPassDescriptor.builder(() -> "ClearColorDepthTextures")
|
| 408 | .withColorAttachment(colorTextureView)
|
| 409 | .withDepthAttachment(depthTextureView)
|
| 410 | .withRenderArea(new RenderPass.RenderArea(0, 0, colorTexture.getWidth(mipLevel), colorTexture.getHeight(mipLevel)))
|
| 411 | .build()
|
| 412 | );
|
| 413 | assert this.currentRenderPass != null;
|
| 414 | org.lwjgl.vulkan.VkClearRect.Buffer rects = VkClearRect.calloc(1, stack);
|
| 415 | rects.baseArrayLayer(0);
|
| 416 | rects.layerCount(1);
|
| 417 | rects.rect().offset().set(regionX, regionY);
|
| 418 | rects.rect().extent().set(regionWidth, regionHeight);
|
| 419 | org.lwjgl.vulkan.VkClearAttachment.Buffer attachments = VkClearAttachment.calloc(2, stack);
|
| 420 | VkClearValue colorClearValue = VkClearValue.calloc(stack);
|
| 421 | VulkanUtils.putArgb(colorClearValue.color(), clearColor);
|
| 422 | attachments.aspectMask(1);
|
| 423 | attachments.clearValue(colorClearValue);
|
| 424 | VkClearValue depthClearValue = VkClearValue.calloc(stack);
|
| 425 | VkClearDepthStencilValue clearValue = depthClearValue.depthStencil();
|
| 426 | clearValue.depth((float)clearDepth);
|
| 427 | attachments.position(1);
|
| 428 | attachments.aspectMask(2);
|
| 429 | attachments.clearValue(depthClearValue);
|
| 430 | attachments.position(0);
|
| 431 | VK12.vkCmdClearAttachments(this.commandBuffer(), attachments, rects);
|
| 432 | this.submitRenderPass();
|
| 433 | }
|
| 434 | }
|
| 435 |
|
| 436 | @Override
|
| 437 | public void clearDepthTexture(final GpuTexture depthTexture, final double clearDepth) {
|
| 438 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 439 | this.clearDepthTextureUnsynced(stack, depthTexture, clearDepth);
|
| 440 | this.memoryBarrier(stack);
|
| 441 | }
|
| 442 | }
|
| 443 |
|
| 444 | @Override
|
| 445 | public void writeToBuffer(final GpuBufferSlice destination, final ByteBuffer data) {
|
| 446 | VulkanGpuBuffer destBuffer = (VulkanGpuBuffer)destination.buffer();
|
| 447 | GpuBufferSlice stagingBuffer = this.transientMemory.uploadStaging(data, 1L, 16);
|
| 448 |
|
| 449 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 450 | org.lwjgl.vulkan.VkBufferCopy.Buffer regions = VkBufferCopy.calloc(1, stack)
|
| 451 | .srcOffset(stagingBuffer.offset())
|
| 452 | .dstOffset(destination.offset())
|
| 453 | .size(data.remaining());
|
| 454 | VK12.vkCmdCopyBuffer(this.commandBuffer(), ((VulkanGpuBuffer)stagingBuffer.buffer()).vkBuffer(), destBuffer.vkBuffer(), regions);
|
| 455 | this.memoryBarrier(stack);
|
| 456 | }
|
| 457 | }
|
| 458 |
|
| 459 | @Override
|
| 460 | public void copyToBuffer(final GpuBufferSlice source, final GpuBufferSlice target) {
|
| 461 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 462 | org.lwjgl.vulkan.VkBufferCopy.Buffer copyInfo = VkBufferCopy.calloc(1, stack);
|
| 463 | copyInfo.srcOffset(source.offset());
|
| 464 | copyInfo.dstOffset(target.offset());
|
| 465 | copyInfo.size(source.length());
|
| 466 | VK12.vkCmdCopyBuffer(this.commandBuffer(), ((VulkanGpuBuffer)source.buffer()).vkBuffer(), ((VulkanGpuBuffer)target.buffer()).vkBuffer(), copyInfo);
|
| 467 | this.memoryBarrier(stack);
|
| 468 | }
|
| 469 | }
|
| 470 |
|
| 471 | @Override
|
| 472 | public void writeToTexture(
|
| 473 | final GpuTexture destination,
|
| 474 | final ByteBuffer source,
|
| 475 | final int mipLevel,
|
| 476 | final int depthOrLayer,
|
| 477 | final int destX,
|
| 478 | final int destY,
|
| 479 | final int width,
|
| 480 | final int height
|
| 481 | ) {
|
| 482 | GpuBufferSlice stagingBuffer = this.transientMemory.uploadStaging(source, 1L, 16);
|
| 483 |
|
| 484 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 485 | org.lwjgl.vulkan.VkBufferImageCopy.Buffer region = VkBufferImageCopy.calloc(1, stack);
|
| 486 | region.bufferOffset(stagingBuffer.offset());
|
| 487 | region.bufferRowLength(width);
|
| 488 | region.bufferImageHeight(height);
|
| 489 | VkImageSubresourceLayers imageSubresource = region.imageSubresource();
|
| 490 | imageSubresource.aspectMask(1);
|
| 491 | imageSubresource.mipLevel(mipLevel);
|
| 492 | imageSubresource.baseArrayLayer(depthOrLayer);
|
| 493 | imageSubresource.layerCount(1);
|
| 494 | region.imageOffset().set(destX, destY, 0);
|
| 495 | region.imageExtent().set(width, height, 1);
|
| 496 | VK12.vkCmdCopyBufferToImage(this.commandBuffer(), ((VulkanGpuBuffer)stagingBuffer.buffer()).vkBuffer(), ((VulkanGpuTexture)destination).vkImage(), 1, region);
|
| 497 | this.memoryBarrier(stack);
|
| 498 | }
|
| 499 | }
|
| 500 |
|
| 501 | @Override
|
| 502 | public void copyBufferToTexture(
|
| 503 | final GpuBufferSlice source,
|
| 504 | final int sourceX,
|
| 505 | final int sourceY,
|
| 506 | final int sourceWidth,
|
| 507 | final int sourceHeight,
|
| 508 | final GpuTexture destination,
|
| 509 | final int destinationX,
|
| 510 | final int destinationY,
|
| 511 | final int copyWidth,
|
| 512 | final int copyHeight,
|
| 513 | final int mipLevel,
|
| 514 | final int arrayLayer
|
| 515 | ) {
|
| 516 | int texelSize = destination.getFormat().blockSize();
|
| 517 | long skipTexels = sourceX + (long)sourceY * sourceWidth;
|
| 518 | long skipBytes = skipTexels * texelSize;
|
| 519 |
|
| 520 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 521 | org.lwjgl.vulkan.VkBufferImageCopy.Buffer region = VkBufferImageCopy.calloc(1, stack);
|
| 522 | region.bufferOffset(source.offset() + skipBytes);
|
| 523 | region.bufferRowLength(sourceWidth);
|
| 524 | region.bufferImageHeight(sourceHeight);
|
| 525 | VkImageSubresourceLayers imageSubresource = region.imageSubresource();
|
| 526 | imageSubresource.aspectMask(1);
|
| 527 | imageSubresource.mipLevel(mipLevel);
|
| 528 | imageSubresource.baseArrayLayer(arrayLayer);
|
| 529 | imageSubresource.layerCount(1);
|
| 530 | region.imageOffset().set(destinationX, destinationY, 0);
|
| 531 | region.imageExtent().set(copyWidth, copyHeight, 1);
|
| 532 | VK12.vkCmdCopyBufferToImage(this.commandBuffer(), ((VulkanGpuBuffer)source.buffer()).vkBuffer(), ((VulkanGpuTexture)destination).vkImage(), 1, region);
|
| 533 | this.memoryBarrier(stack);
|
| 534 | }
|
| 535 | }
|
| 536 |
|
| 537 | @Override
|
| 538 | public void copyTextureToBuffer(final GpuTexture source, final GpuBuffer destination, final long offset, final Runnable callback, final int mipLevel) {
|
| 539 | this.copyTextureToBuffer(source, destination, offset, callback, mipLevel, 0, 0, source.getWidth(mipLevel), source.getHeight(mipLevel));
|
| 540 | }
|
| 541 |
|
| 542 | @Override
|
| 543 | public void copyTextureToBuffer(
|
| 544 | final GpuTexture source,
|
| 545 | final GpuBuffer destination,
|
| 546 | final long offset,
|
| 547 | final Runnable callback,
|
| 548 | final int mipLevel,
|
| 549 | final int x,
|
| 550 | final int y,
|
| 551 | final int width,
|
| 552 | final int height
|
| 553 | ) {
|
| 554 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 555 | org.lwjgl.vulkan.VkBufferImageCopy.Buffer copy = VkBufferImageCopy.calloc(1, stack);
|
| 556 | copy.bufferOffset(offset);
|
| 557 | VkImageSubresourceLayers subresource = copy.imageSubresource();
|
| 558 | subresource.aspectMask(VulkanConst.formatAspectMask(source.getFormat()));
|
| 559 | subresource.mipLevel(mipLevel);
|
| 560 | subresource.baseArrayLayer(0);
|
| 561 | subresource.layerCount(1);
|
| 562 | copy.imageOffset().set(x, y, 0);
|
| 563 | copy.imageExtent().set(width, height, 1);
|
| 564 | copy.bufferRowLength(width);
|
| 565 | copy.bufferImageHeight(height);
|
| 566 | VK12.vkCmdCopyImageToBuffer(this.commandBuffer(), ((VulkanGpuTexture)source).vkImage(), 1, ((VulkanGpuBuffer)destination).vkBuffer(), copy);
|
| 567 | this.memoryBarrier(stack);
|
| 568 | }
|
| 569 |
|
| 570 | this.queueForDestroy(callback::run);
|
| 571 | }
|
| 572 |
|
| 573 | @Override
|
| 574 | public void copyTextureToTexture(
|
| 575 | final GpuTexture source,
|
| 576 | final GpuTexture destination,
|
| 577 | final int mipLevel,
|
| 578 | final int destX,
|
| 579 | final int destY,
|
| 580 | final int sourceX,
|
| 581 | final int sourceY,
|
| 582 | final int width,
|
| 583 | final int height
|
| 584 | ) {
|
| 585 | VulkanGpuTexture vulkanSrc = (VulkanGpuTexture)source;
|
| 586 | VulkanGpuTexture vulkanDst = (VulkanGpuTexture)destination;
|
| 587 |
|
| 588 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 589 | VkImageSubresourceLayers subresourceLayers = VkImageSubresourceLayers.calloc(stack);
|
| 590 | subresourceLayers.mipLevel(mipLevel);
|
| 591 | subresourceLayers.baseArrayLayer(0);
|
| 592 | subresourceLayers.layerCount(1);
|
| 593 | subresourceLayers.aspectMask(VulkanConst.formatAspectMask(source.getFormat()));
|
| 594 | org.lwjgl.vulkan.VkImageCopy.Buffer regions = VkImageCopy.calloc(1, stack);
|
| 595 | regions.srcOffset().set(sourceX, sourceY, 0);
|
| 596 | regions.dstOffset().set(destX, destY, 0);
|
| 597 | regions.extent().set(width, height, 1);
|
| 598 | regions.srcSubresource(subresourceLayers);
|
| 599 | regions.dstSubresource(subresourceLayers);
|
| 600 | VK12.vkCmdCopyImage(this.commandBuffer(), vulkanSrc.vkImage(), 1, vulkanDst.vkImage(), 1, regions);
|
| 601 | this.memoryBarrier(stack);
|
| 602 | }
|
| 603 | }
|
| 604 |
|
| 605 | private boolean awaitSubmitCompletion(final long submitIndex, final long timeoutNS) {
|
| 606 | if (this.completedSubmitIndex >= submitIndex) {
|
| 607 | return true;
|
| 608 | }
|
| 609 |
|
| 610 | if (submitIndex == this.currentSubmitIndex) {
|
| 611 | if (timeoutNS == 0L) {
|
| 612 | return false;
|
| 613 | } else {
|
| 614 | throw new IllegalStateException("Cannot wait on a fence for the current submit");
|
| 615 | }
|
| 616 | } else {
|
| 617 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 618 | VkSemaphoreWaitInfo waitInfo = VkSemaphoreWaitInfo.calloc(stack).sType$Default();
|
| 619 | waitInfo.pSemaphores(stack.longs(this.submitSemaphore));
|
| 620 | waitInfo.pValues(stack.longs(submitIndex));
|
| 621 | waitInfo.semaphoreCount(1);
|
| 622 | int result = VK12.vkWaitSemaphores(this.device.vkDevice(), waitInfo, timeoutNS);
|
| 623 | VulkanUtils.crashIfFailure(this.device, result, "Failed to wait for semaphore");
|
| 624 | boolean completed = result == 0;
|
| 625 | if (completed) {
|
| 626 | this.completedSubmitIndex = submitIndex;
|
| 627 | }
|
| 628 |
|
| 629 | return completed;
|
| 630 | }
|
| 631 | }
|
| 632 | }
|
| 633 |
|
| 634 | @Override
|
| 635 | public GpuFence createFence() {
|
| 636 | return new GpuFence() {
|
| 637 | private final long submitIndex = VulkanCommandEncoder.this.currentSubmitIndex;
|
| 638 | private boolean completed = false;
|
| 639 |
|
| 640 | @Override
|
| 641 | public boolean awaitCompletion(final long timeoutMs) {
|
| 642 | if (!this.completed) {
|
| 643 | this.completed = VulkanCommandEncoder.this.awaitSubmitCompletion(this.submitIndex, timeoutMs);
|
| 644 | }
|
| 645 |
|
| 646 | return this.completed;
|
| 647 | }
|
| 648 |
|
| 649 | @Override
|
| 650 | public void close() {
|
| 651 | this.completed = true;
|
| 652 | }
|
| 653 | };
|
| 654 | }
|
| 655 |
|
| 656 | @Override
|
| 657 | public void writeTimestamp(final GpuQueryPool pool, final int index) {
|
| 658 | long queryPool = ((VulkanQueryPool)pool).vkQueryPool();
|
| 659 | VK12.vkResetQueryPool(this.device.vkDevice(), queryPool, index, 1);
|
| 660 | KHRSynchronization2.vkCmdWriteTimestamp2KHR(this.commandBuffer(), 65536L, queryPool, index);
|
| 661 | }
|
| 662 |
|
| 663 | public long getTimestampNow() {
|
| 664 | try (
|
| 665 | MemoryStack stack = MemoryStack.stackPush();
|
| 666 | VulkanQueryPool queryPool = (VulkanQueryPool)this.device.createTimestampQueryPool(1);
|
| 667 | ) {
|
| 668 | VkCommandBuffer commandBuffer = this.allocateAndBeginTransientCommandBuffer();
|
| 669 | KHRSynchronization2.vkCmdWriteTimestamp2KHR(commandBuffer, 0L, queryPool.vkQueryPool(), 0);
|
| 670 | VulkanUtils.crashIfFailure(this.device, VK12.vkEndCommandBuffer(commandBuffer), "Failed to end VkCommandBuffer");
|
| 671 |
|
| 672 | try (VulkanQueue.Submission submit = this.device.graphicsQueue().beginSubmit()) {
|
| 673 | submit.executeCommands(commandBuffer);
|
| 674 | }
|
| 675 |
|
| 676 | LongBuffer timestampPtr = stack.callocLong(1);
|
| 677 | VulkanUtils.crashIfFailure(
|
| 678 | this.device, VK12.vkGetQueryPoolResults(this.device.vkDevice(), queryPool.vkQueryPool(), 0, 1, timestampPtr, 0L, 3), "Cannot fetch current timestamp"
|
| 679 | );
|
| 680 | return timestampPtr.get(0);
|
| 681 | }
|
| 682 | }
|
| 683 | }
|
| 684 |
|