| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.device.GpuSurface;
|
| 4 | import com.mojang.renderpearl.api.device.SurfaceException;
|
| 5 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 6 | import com.mojang.renderpearl.backend.api.CommandEncoderBackend;
|
| 7 | import com.mojang.renderpearl.backend.api.GpuSurfaceBackend;
|
| 8 | import com.mojang.renderpearl.backend.vulkan.checkpoints.CheckpointExtension;
|
| 9 | import it.unimi.dsi.fastutil.longs.LongArrayList;
|
| 10 | import it.unimi.dsi.fastutil.longs.LongArrays;
|
| 11 | import it.unimi.dsi.fastutil.longs.LongList;
|
| 12 | import java.nio.IntBuffer;
|
| 13 | import java.nio.LongBuffer;
|
| 14 | import java.util.Collection;
|
| 15 | import java.util.Collections;
|
| 16 | import java.util.EnumSet;
|
| 17 | import java.util.List;
|
| 18 | import java.util.Locale;
|
| 19 | import java.util.Set;
|
| 20 | import net.fabricmc.api.EnvType;
|
| 21 | import net.fabricmc.api.Environment;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 | import org.lwjgl.sdl.SDLError;
|
| 24 | import org.lwjgl.sdl.SDLVulkan;
|
| 25 | import org.lwjgl.system.MemoryStack;
|
| 26 | import org.lwjgl.system.MemoryUtil;
|
| 27 | import org.lwjgl.vulkan.KHRSurface;
|
| 28 | import org.lwjgl.vulkan.KHRSwapchain;
|
| 29 | import org.lwjgl.vulkan.KHRSynchronization2;
|
| 30 | import org.lwjgl.vulkan.VK12;
|
| 31 | import org.lwjgl.vulkan.VkCommandBuffer;
|
| 32 | import org.lwjgl.vulkan.VkDependencyInfo;
|
| 33 | import org.lwjgl.vulkan.VkExtent2D;
|
| 34 | import org.lwjgl.vulkan.VkImageBlit;
|
| 35 | import org.lwjgl.vulkan.VkImageMemoryBarrier2;
|
| 36 | import org.lwjgl.vulkan.VkImageSubresourceLayers;
|
| 37 | import org.lwjgl.vulkan.VkImageSubresourceRange;
|
| 38 | import org.lwjgl.vulkan.VkMemoryBarrier2;
|
| 39 | import org.lwjgl.vulkan.VkOffset3D;
|
| 40 | import org.lwjgl.vulkan.VkPresentInfoKHR;
|
| 41 | import org.lwjgl.vulkan.VkQueue;
|
| 42 | import org.lwjgl.vulkan.VkSemaphoreCreateInfo;
|
| 43 | import org.lwjgl.vulkan.VkSurfaceCapabilitiesKHR;
|
| 44 | import org.lwjgl.vulkan.VkSurfaceFormatKHR;
|
| 45 | import org.lwjgl.vulkan.VkSwapchainCreateInfoKHR;
|
| 46 | import org.lwjgl.vulkan.VkSurfaceFormatKHR.Buffer;
|
| 47 |
|
| 48 | @Environment(EnvType.CLIENT)
|
| 49 | public class VulkanGpuSurface implements GpuSurfaceBackend {
|
| 50 | private static final int NO_CURRENT_IMAGE = -1;
|
| 51 | private final VulkanDevice device;
|
| 52 | private final VkQueue presentQueue;
|
| 53 | private final long surface;
|
| 54 | private final int swapchainImageFormat;
|
| 55 | private long swapchain;
|
| 56 | private int swapchainWidth;
|
| 57 | private int swapchainHeight;
|
| 58 | private final LongList swapchainImages = new LongArrayList();
|
| 59 | private final long[] acquireSemaphores = new long[2];
|
| 60 | private int currentAcquireSemaphore = 0;
|
| 61 | private long[] presentSemaphores = LongArrays.EMPTY_ARRAY;
|
| 62 | private int currentImageIndex = -1;
|
| 63 | private @Nullable SurfaceException eatenException = null;
|
| 64 | private boolean swapchainSuboptimal;
|
| 65 | private boolean swapchainOutOfDate;
|
| 66 | private final Set<GpuSurface.PresentMode> supportedPresentModes;
|
| 67 |
|
| 68 | public VulkanGpuSurface(final VulkanDevice device, final long windowHandle) {
|
| 69 | this.device = device;
|
| 70 | this.presentQueue = device.graphicsQueue().vkQueue();
|
| 71 |
|
| 72 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 73 | LongBuffer handlePtr = stack.longs(0L);
|
| 74 | if (!SDLVulkan.SDL_Vulkan_CreateSurface(windowHandle, device.vkDevice().getPhysicalDevice().getInstance(), null, handlePtr)) {
|
| 75 | throw new IllegalStateException("Failed to create window surface: " + SDLError.SDL_GetError());
|
| 76 | }
|
| 77 |
|
| 78 | this.surface = handlePtr.get(0);
|
| 79 | IntBuffer countPtr = stack.callocInt(1);
|
| 80 | VulkanUtils.crashIfFailure(
|
| 81 | device,
|
| 82 | KHRSurface.vkGetPhysicalDeviceSurfacePresentModesKHR(device.vkDevice().getPhysicalDevice(), this.surface, countPtr, null),
|
| 83 | "Failed to enumerate surface present modes"
|
| 84 | );
|
| 85 | int presentModeCount = countPtr.get(0);
|
| 86 | IntBuffer presentModes = stack.callocInt(presentModeCount);
|
| 87 | VulkanUtils.crashIfFailure(
|
| 88 | device,
|
| 89 | KHRSurface.vkGetPhysicalDeviceSurfacePresentModesKHR(device.vkDevice().getPhysicalDevice(), this.surface, countPtr, presentModes),
|
| 90 | "Failed to enumerate surface present modes"
|
| 91 | );
|
| 92 | this.supportedPresentModes = Collections.unmodifiableSet(this.convertPresentModes(presentModes));
|
| 93 | IntBuffer formatCount = stack.callocInt(1);
|
| 94 | KHRSurface.vkGetPhysicalDeviceSurfaceFormatsKHR(device.vkDevice().getPhysicalDevice(), this.surface, formatCount, null);
|
| 95 | Buffer formatsBuffer = VkSurfaceFormatKHR.calloc(formatCount.get(0));
|
| 96 | KHRSurface.vkGetPhysicalDeviceSurfaceFormatsKHR(device.vkDevice().getPhysicalDevice(), this.surface, formatCount, formatsBuffer);
|
| 97 | this.swapchainImageFormat = this.pickSwapchainSurfaceFormat(formatsBuffer).format();
|
| 98 | MemoryUtil.memFree(formatsBuffer);
|
| 99 | VkSemaphoreCreateInfo semaphoreCreateInfo = VkSemaphoreCreateInfo.calloc(stack).sType$Default();
|
| 100 | LongBuffer semaphoreHandlePtr = stack.callocLong(1);
|
| 101 |
|
| 102 | for (int i = 0; i < this.acquireSemaphores.length; i++) {
|
| 103 | VulkanUtils.crashIfFailure(
|
| 104 | device, VK12.vkCreateSemaphore(device.vkDevice(), semaphoreCreateInfo, null, semaphoreHandlePtr), "Failed to create VkSemaphore(binary)"
|
| 105 | );
|
| 106 | this.acquireSemaphores[i] = semaphoreHandlePtr.get(0);
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | private Set<GpuSurface.PresentMode> convertPresentModes(final IntBuffer presentModes) {
|
| 112 | Set<GpuSurface.PresentMode> result = EnumSet.noneOf(GpuSurface.PresentMode.class);
|
| 113 |
|
| 114 | for (int i = 0; i < presentModes.limit(); i++) {
|
| 115 | int mode = presentModes.get(i);
|
| 116 | switch (mode) {
|
| 117 | case 0:
|
| 118 | result.add(GpuSurface.PresentMode.IMMEDIATE);
|
| 119 | break;
|
| 120 | case 1:
|
| 121 | result.add(GpuSurface.PresentMode.MAILBOX);
|
| 122 | break;
|
| 123 | case 2:
|
| 124 | result.add(GpuSurface.PresentMode.FIFO);
|
| 125 | break;
|
| 126 | case 3:
|
| 127 | result.add(GpuSurface.PresentMode.FIFO_RELAXED);
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | return result;
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public Collection<GpuSurface.PresentMode> supportedPresentModes() {
|
| 136 | return this.supportedPresentModes;
|
| 137 | }
|
| 138 |
|
| 139 | public VkSurfaceFormatKHR pickSwapchainSurfaceFormat(final Buffer formats) {
|
| 140 | for (VkSurfaceFormatKHR format : formats) {
|
| 141 | if (format.colorSpace() == 0 && (format.format() == 37 || format.format() == 44)) {
|
| 142 | return format;
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | throw new IllegalStateException("Could not find compatible swapchain format");
|
| 147 | }
|
| 148 |
|
| 149 | public static void throwIfFailure(final int result, final String message) throws SurfaceException {
|
| 150 | if (result < 0) {
|
| 151 | throw new SurfaceException(VulkanUtils.resultToString(result) + ": " + message);
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public void close() {
|
| 157 | this.destroySwapchain();
|
| 158 |
|
| 159 | for (int i = 0; i < this.acquireSemaphores.length; i++) {
|
| 160 | VK12.vkDestroySemaphore(this.device.vkDevice(), this.acquireSemaphores[i], null);
|
| 161 | }
|
| 162 |
|
| 163 | KHRSurface.vkDestroySurfaceKHR(this.device.instance().vkInstance(), this.surface, null);
|
| 164 | }
|
| 165 |
|
| 166 | private void destroySwapchain() {
|
| 167 | if (this.swapchain != 0L) {
|
| 168 | this.device.graphicsQueue().waitIdle();
|
| 169 | KHRSwapchain.vkDestroySwapchainKHR(this.device.vkDevice(), this.swapchain, null);
|
| 170 |
|
| 171 | for (int i = 0; i < this.presentSemaphores.length; i++) {
|
| 172 | VK12.vkDestroySemaphore(this.device.vkDevice(), this.presentSemaphores[i], null);
|
| 173 | }
|
| 174 |
|
| 175 | this.presentSemaphores = LongArrays.EMPTY_ARRAY;
|
| 176 | this.swapchain = 0L;
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public void configure(final GpuSurface.Configuration config) throws SurfaceException {
|
| 182 | this.destroySwapchain();
|
| 183 |
|
| 184 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 185 | VkSurfaceCapabilitiesKHR surfaceCapabilities = VkSurfaceCapabilitiesKHR.calloc(stack);
|
| 186 | throwIfFailure(
|
| 187 | KHRSurface.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this.device.vkDevice().getPhysicalDevice(), this.surface, surfaceCapabilities),
|
| 188 | "Failed to get surface capabilities"
|
| 189 | );
|
| 190 | VkExtent2D minExtent = surfaceCapabilities.minImageExtent();
|
| 191 | VkExtent2D maxExtent = surfaceCapabilities.maxImageExtent();
|
| 192 | if (config.width() < minExtent.width() || maxExtent.width() < config.width() || config.height() < minExtent.height() || maxExtent.height() < config.height()
|
| 193 | )
|
| 194 | {
|
| 195 | throw new SurfaceException(
|
| 196 | String.format(
|
| 197 | Locale.ROOT,
|
| 198 | "Requested swapchain extent (%d x %d) not within allowed extent: min(%d x %d) max(%d x %d)",
|
| 199 | config.width(),
|
| 200 | config.height(),
|
| 201 | minExtent.width(),
|
| 202 | minExtent.height(),
|
| 203 | maxExtent.width(),
|
| 204 | maxExtent.height()
|
| 205 | )
|
| 206 | );
|
| 207 | }
|
| 208 |
|
| 209 | VkSwapchainCreateInfoKHR swapchainCreateInfo = VkSwapchainCreateInfoKHR.calloc(stack).sType$Default();
|
| 210 | swapchainCreateInfo.surface(this.surface);
|
| 211 | int currentPresentMode = VulkanConst.toVk(config.presentMode());
|
| 212 | int requestedImageCount = 3;
|
| 213 | swapchainCreateInfo.minImageCount(Math.max(3, surfaceCapabilities.minImageCount()));
|
| 214 | swapchainCreateInfo.imageFormat(this.swapchainImageFormat);
|
| 215 | swapchainCreateInfo.imageColorSpace(0);
|
| 216 | swapchainCreateInfo.imageExtent(VkExtent2D.calloc(stack).set(config.width(), config.height()));
|
| 217 | swapchainCreateInfo.imageArrayLayers(1);
|
| 218 | swapchainCreateInfo.imageUsage(2);
|
| 219 | swapchainCreateInfo.imageSharingMode(0);
|
| 220 | swapchainCreateInfo.preTransform(surfaceCapabilities.currentTransform());
|
| 221 | swapchainCreateInfo.compositeAlpha(1);
|
| 222 | swapchainCreateInfo.presentMode(currentPresentMode);
|
| 223 | swapchainCreateInfo.clipped(true);
|
| 224 | LongBuffer swapchainPtr = stack.callocLong(1);
|
| 225 | throwIfFailure(KHRSwapchain.vkCreateSwapchainKHR(this.device.vkDevice(), swapchainCreateInfo, null, swapchainPtr), "Failed to create Swapchain");
|
| 226 | this.swapchain = swapchainPtr.get(0);
|
| 227 | IntBuffer imageCountPtr = stack.callocInt(1);
|
| 228 | throwIfFailure(KHRSwapchain.vkGetSwapchainImagesKHR(this.device.vkDevice(), this.swapchain, imageCountPtr, null), "Failed to get swapchain image count");
|
| 229 | int swapchainImageCount = imageCountPtr.get(0);
|
| 230 | LongBuffer swapchainImagesPtr = stack.callocLong(swapchainImageCount);
|
| 231 | throwIfFailure(
|
| 232 | KHRSwapchain.vkGetSwapchainImagesKHR(this.device.vkDevice(), this.swapchain, imageCountPtr, swapchainImagesPtr), "Failed to get swapchain images"
|
| 233 | );
|
| 234 | this.swapchainImages.clear();
|
| 235 |
|
| 236 | for (int i = 0; i < swapchainImageCount; i++) {
|
| 237 | this.swapchainImages.add(swapchainImagesPtr.get(i));
|
| 238 | }
|
| 239 |
|
| 240 | this.presentSemaphores = new long[swapchainImageCount];
|
| 241 | VkSemaphoreCreateInfo semaphoreCreateInfo = VkSemaphoreCreateInfo.calloc(stack).sType$Default();
|
| 242 | LongBuffer semaphoreHandlePtr = stack.callocLong(1);
|
| 243 |
|
| 244 | for (int i = 0; i < this.presentSemaphores.length; i++) {
|
| 245 | throwIfFailure(VK12.vkCreateSemaphore(this.device.vkDevice(), semaphoreCreateInfo, null, semaphoreHandlePtr), "Failed to create VkSemaphore(binary)");
|
| 246 | this.presentSemaphores[i] = semaphoreHandlePtr.get(0);
|
| 247 | }
|
| 248 |
|
| 249 | this.swapchainSuboptimal = false;
|
| 250 | this.swapchainOutOfDate = false;
|
| 251 | this.swapchainWidth = config.width();
|
| 252 | this.swapchainHeight = config.height();
|
| 253 | } catch (SurfaceException e) {
|
| 254 | this.swapchainSuboptimal = true;
|
| 255 | this.swapchainOutOfDate = true;
|
| 256 | throw e;
|
| 257 | }
|
| 258 | }
|
| 259 |
|
| 260 | @Override
|
| 261 | public boolean isSuboptimal() {
|
| 262 | return this.swapchainSuboptimal;
|
| 263 | }
|
| 264 |
|
| 265 | @Override
|
| 266 | public void acquireNextTexture() throws SurfaceException {
|
| 267 | if (this.eatenException != null) {
|
| 268 | SurfaceException toThrow = this.eatenException;
|
| 269 | this.eatenException = null;
|
| 270 | throw new SurfaceException(toThrow);
|
| 271 | }
|
| 272 |
|
| 273 | if (this.swapchainOutOfDate) {
|
| 274 | throw new IllegalStateException("Attempt to use out of date swapchain");
|
| 275 | }
|
| 276 |
|
| 277 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 278 | IntBuffer frameIndexPtr = stack.callocInt(1);
|
| 279 | frameIndexPtr.put(0, -1);
|
| 280 | this.currentAcquireSemaphore++;
|
| 281 | this.currentAcquireSemaphore = this.currentAcquireSemaphore % this.acquireSemaphores.length;
|
| 282 | long acquireSemaphore = this.acquireSemaphores[this.currentAcquireSemaphore];
|
| 283 | int result = KHRSwapchain.vkAcquireNextImageKHR(this.device.vkDevice(), this.swapchain, 5000000000L, acquireSemaphore, 0L, frameIndexPtr);
|
| 284 | if (result == 2) {
|
| 285 | List<CheckpointExtension.QueueCheckpoints> checkpoints = this.device.checkpointExtension().retrieveCheckpoints(false);
|
| 286 | throw new IllegalStateException("GPU timeout attempting to acquire next frame: " + VulkanUtils.formatCheckpoints(checkpoints));
|
| 287 | }
|
| 288 |
|
| 289 | this.currentImageIndex = frameIndexPtr.get(0);
|
| 290 | if (result == -1000001004) {
|
| 291 | this.swapchainSuboptimal = true;
|
| 292 | this.swapchainOutOfDate = true;
|
| 293 | throw new SurfaceException("Failed to acquire image, swapchain out of date");
|
| 294 | }
|
| 295 |
|
| 296 | if (result == 1000001003) {
|
| 297 | this.swapchainSuboptimal = true;
|
| 298 | } else {
|
| 299 | VulkanUtils.crashIfFailure(this.device, result, "Failed to acquire image");
|
| 300 | }
|
| 301 | }
|
| 302 | }
|
| 303 |
|
| 304 | @Override
|
| 305 | public void blitFromTexture(final CommandEncoderBackend commandEncoder, final GpuTextureView textureView) {
|
| 306 | if (this.swapchainOutOfDate) {
|
| 307 | throw new IllegalStateException("Attempt to use out of date swapchain");
|
| 308 | }
|
| 309 |
|
| 310 | assert this.currentImageIndex != -1;
|
| 311 | VulkanCommandEncoder vulkanCommandEncoder = (VulkanCommandEncoder)commandEncoder;
|
| 312 | VkCommandBuffer blitCommandBuffer = vulkanCommandEncoder.allocateAndBeginTransientCommandBuffer();
|
| 313 | long swapchainImage = this.swapchainImages.getLong(this.currentImageIndex);
|
| 314 | MemoryStack stack = MemoryStack.stackGet();
|
| 315 |
|
| 316 | try (MemoryStack var8 = stack.push()) {
|
| 317 | org.lwjgl.vulkan.VkImageMemoryBarrier2.Buffer imageBarrier = VkImageMemoryBarrier2.calloc(1, stack).sType$Default();
|
| 318 | imageBarrier.srcStageMask(0L);
|
| 319 | imageBarrier.srcAccessMask(0L);
|
| 320 | imageBarrier.dstStageMask(4096L);
|
| 321 | imageBarrier.dstAccessMask(4096L);
|
| 322 | imageBarrier.oldLayout(0);
|
| 323 | imageBarrier.newLayout(7);
|
| 324 | imageBarrier.srcQueueFamilyIndex(-1);
|
| 325 | imageBarrier.dstQueueFamilyIndex(-1);
|
| 326 | imageBarrier.image(swapchainImage);
|
| 327 | VkImageSubresourceRange subresourceRange = imageBarrier.subresourceRange();
|
| 328 | subresourceRange.aspectMask(1);
|
| 329 | subresourceRange.baseMipLevel(0);
|
| 330 | subresourceRange.levelCount(1);
|
| 331 | subresourceRange.baseArrayLayer(0);
|
| 332 | subresourceRange.layerCount(1);
|
| 333 | VkDependencyInfo depinfo = VkDependencyInfo.calloc(stack).sType$Default();
|
| 334 | depinfo.pImageMemoryBarriers(imageBarrier);
|
| 335 | KHRSynchronization2.vkCmdPipelineBarrier2KHR(blitCommandBuffer, depinfo);
|
| 336 | }
|
| 337 |
|
| 338 | try (MemoryStack var22 = stack.push()) {
|
| 339 | int copyWidth = Math.min(this.swapchainWidth, textureView.getWidth(0));
|
| 340 | int copyHeight = Math.min(this.swapchainHeight, textureView.getHeight(0));
|
| 341 | org.lwjgl.vulkan.VkOffset3D.Buffer srcOffsets = VkOffset3D.calloc(2, stack);
|
| 342 | srcOffsets.x(0).y(0).z(0);
|
| 343 | srcOffsets.position(1);
|
| 344 | srcOffsets.x(copyWidth).y(copyHeight).z(1);
|
| 345 | srcOffsets.position(0);
|
| 346 | org.lwjgl.vulkan.VkOffset3D.Buffer dstOffsets = VkOffset3D.calloc(2, stack);
|
| 347 | dstOffsets.x(0).y(copyHeight).z(0);
|
| 348 | dstOffsets.position(1);
|
| 349 | dstOffsets.x(copyWidth).y(0).z(1);
|
| 350 | dstOffsets.position(0);
|
| 351 | VkImageSubresourceLayers srcSubresource = VkImageSubresourceLayers.calloc(stack);
|
| 352 | srcSubresource.aspectMask(1);
|
| 353 | srcSubresource.mipLevel(textureView.baseMipLevel());
|
| 354 | srcSubresource.baseArrayLayer(0);
|
| 355 | srcSubresource.layerCount(1);
|
| 356 | VkImageSubresourceLayers dstSubresource = VkImageSubresourceLayers.calloc(stack);
|
| 357 | dstSubresource.aspectMask(1);
|
| 358 | dstSubresource.mipLevel(0);
|
| 359 | dstSubresource.baseArrayLayer(0);
|
| 360 | dstSubresource.layerCount(1);
|
| 361 | org.lwjgl.vulkan.VkImageBlit.Buffer blitRegion = VkImageBlit.calloc(1, stack);
|
| 362 | blitRegion.srcSubresource(srcSubresource);
|
| 363 | blitRegion.srcOffsets(srcOffsets);
|
| 364 | blitRegion.dstSubresource(dstSubresource);
|
| 365 | blitRegion.dstOffsets(dstOffsets);
|
| 366 | VK12.vkCmdBlitImage(blitCommandBuffer, ((VulkanGpuTexture)textureView.texture()).vkImage(), 1, swapchainImage, 7, blitRegion, 0);
|
| 367 | }
|
| 368 |
|
| 369 | try (MemoryStack var23 = stack.push()) {
|
| 370 | org.lwjgl.vulkan.VkImageMemoryBarrier2.Buffer imageBarrier = VkImageMemoryBarrier2.calloc(1, stack).sType$Default();
|
| 371 | imageBarrier.srcStageMask(4096L);
|
| 372 | imageBarrier.srcAccessMask(4096L);
|
| 373 | imageBarrier.dstStageMask(65536L);
|
| 374 | imageBarrier.dstAccessMask(0L);
|
| 375 | imageBarrier.oldLayout(7);
|
| 376 | imageBarrier.newLayout(1000001002);
|
| 377 | imageBarrier.srcQueueFamilyIndex(-1);
|
| 378 | imageBarrier.dstQueueFamilyIndex(-1);
|
| 379 | imageBarrier.image(swapchainImage);
|
| 380 | VkImageSubresourceRange subresourceRange = imageBarrier.subresourceRange();
|
| 381 | subresourceRange.aspectMask(1);
|
| 382 | subresourceRange.baseMipLevel(0);
|
| 383 | subresourceRange.levelCount(1);
|
| 384 | subresourceRange.baseArrayLayer(0);
|
| 385 | subresourceRange.layerCount(1);
|
| 386 | org.lwjgl.vulkan.VkMemoryBarrier2.Buffer memoryBarrier = VkMemoryBarrier2.calloc(1, stack).sType$Default();
|
| 387 | memoryBarrier.srcStageMask(4096L);
|
| 388 | memoryBarrier.srcAccessMask(2048L);
|
| 389 | memoryBarrier.dstStageMask(65536L);
|
| 390 | memoryBarrier.dstAccessMask(98304L);
|
| 391 | VkDependencyInfo depinfo = VkDependencyInfo.calloc(stack).sType$Default();
|
| 392 | depinfo.pMemoryBarriers(memoryBarrier);
|
| 393 | depinfo.pImageMemoryBarriers(imageBarrier);
|
| 394 | KHRSynchronization2.vkCmdPipelineBarrier2KHR(blitCommandBuffer, depinfo);
|
| 395 | }
|
| 396 |
|
| 397 | VulkanUtils.crashIfFailure(this.device, VK12.vkEndCommandBuffer(blitCommandBuffer), "Failed to end VkCommandBuffer");
|
| 398 | vulkanCommandEncoder.waitSemaphore(this.acquireSemaphores[this.currentAcquireSemaphore], 0L, 65536L);
|
| 399 | vulkanCommandEncoder.execute(blitCommandBuffer);
|
| 400 | vulkanCommandEncoder.signalSemaphore(this.presentSemaphores[this.currentImageIndex], 0L, 4096L);
|
| 401 | }
|
| 402 |
|
| 403 | @Override
|
| 404 | public void present() {
|
| 405 | if (this.swapchainOutOfDate) {
|
| 406 | throw new IllegalStateException("Attempt to use out of date swapchain");
|
| 407 | }
|
| 408 |
|
| 409 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 410 | VkPresentInfoKHR presentInfo = VkPresentInfoKHR.calloc(stack).sType$Default();
|
| 411 | presentInfo.pWaitSemaphores(stack.longs(this.presentSemaphores[this.currentImageIndex]));
|
| 412 | presentInfo.swapchainCount(1);
|
| 413 | presentInfo.pSwapchains(stack.longs(this.swapchain));
|
| 414 | presentInfo.pImageIndices(stack.ints(this.currentImageIndex));
|
| 415 | this.currentImageIndex = -1;
|
| 416 | int result = KHRSwapchain.vkQueuePresentKHR(this.presentQueue, presentInfo);
|
| 417 | if (result == -1000001004) {
|
| 418 | this.swapchainSuboptimal = true;
|
| 419 | this.swapchainOutOfDate = true;
|
| 420 | this.eatenException = new SurfaceException("Failed to present image, swapchain out of date");
|
| 421 | } else if (result == 1000001003) {
|
| 422 | this.swapchainSuboptimal = true;
|
| 423 | } else {
|
| 424 | VulkanUtils.crashIfFailure(this.device, result, "Failed to present image");
|
| 425 | }
|
| 426 | }
|
| 427 | }
|
| 428 | }
|
| 429 |
|