Skip to content

Commit

Permalink
SwapChain resources cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjglgamedev committed Sep 8, 2023
1 parent 620d7b5 commit 6009104
Show file tree
Hide file tree
Showing 18 changed files with 32 additions and 19 deletions.
6 changes: 4 additions & 2 deletions bookcontents/chapter-04/chapter-04.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class SwapChain {
private final Device device;
private final ImageView[] imageViews;
private final SurfaceFormat surfaceFormat;
private final VkExtent2D swapChainExtent;
private final long vkSwapChain;

public SwapChain(Device device, Surface surface, Window window, int requestedImages, boolean vsync) {
Expand Down Expand Up @@ -158,7 +159,7 @@ public class SwapChain {
...
public SwapChain(Device device, Surface surface, Window window, int requestedImages, boolean vsync) {
...
VkExtent2D swapChainExtent = calcSwapChainExtent(stack, window, surfCapabilities);
swapChainExtent = calcSwapChainExtent(window, surfCapabilities);
...
}
...
Expand All @@ -170,7 +171,7 @@ The `calcSwapChainExtent` method is defined like this:
```java
public class SwapChain {
...
public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down Expand Up @@ -429,6 +430,7 @@ public class SwapChain {
...
public void cleanup() {
Logger.debug("Destroying Vulkan SwapChain");
swapChainExtent.free();
Arrays.asList(imageViews).forEach(ImageView::cleanup);
KHRSwapchain.vkDestroySwapchainKHR(device.getVkDevice(), vkSwapChain, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SwapChain {
private final Device device;
private final ImageView[] imageViews;
private final SurfaceFormat surfaceFormat;
private final VkExtent2D swapChainExtent;
private final long vkSwapChain;

public SwapChain(Device device, Surface surface, Window window, int requestedImages, boolean vsync) {
Expand All @@ -34,7 +35,7 @@ public SwapChain(Device device, Surface surface, Window window, int requestedIma

surfaceFormat = calcSurfaceFormat(physicalDevice, surface);

VkExtent2D swapChainExtent = calcSwapChainExtent(window, surfCapabilities);
swapChainExtent = calcSwapChainExtent(window, surfCapabilities);

VkSwapchainCreateInfoKHR vkSwapchainCreateInfo = VkSwapchainCreateInfoKHR.calloc(stack)
.sType(KHRSwapchain.VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR)
Expand Down Expand Up @@ -109,7 +110,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand All @@ -130,6 +131,7 @@ public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR su

public void cleanup() {
Logger.debug("Destroying Vulkan SwapChain");
swapChainExtent.free();
Arrays.asList(imageViews).forEach(ImageView::cleanup);
KHRSwapchain.vkDestroySwapchainKHR(device.getVkDevice(), vkSwapChain, null);
}
Expand Down Expand Up @@ -167,6 +169,10 @@ public SurfaceFormat getSurfaceFormat() {
return surfaceFormat;
}

public VkExtent2D getSwapChainExtent() {
return swapChainExtent;
}

public long getVkSwapChain() {
return vkSwapChain;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.vulkanb.eng.graph.vk;

import org.lwjgl.system.MemoryUtil;
import org.lwjgl.vulkan.VkMemoryBarrier;

import static org.lwjgl.vulkan.VK11.VK_STRUCTURE_TYPE_MEMORY_BARRIER;
Expand All @@ -15,6 +16,10 @@ public MemoryBarrier(int srcAccessMask, int dstAccessMask) {
.dstAccessMask(dstAccessMask);
}

public void cleanup() {
MemoryUtil.memFree(vkMemoryBarrier);
}

public VkMemoryBarrier.Buffer getVkMemoryBarrier() {
return vkMemoryBarrier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private SurfaceFormat calcSurfaceFormat(PhysicalDevice physicalDevice, Surface s
return new SurfaceFormat(imageFormat, colorSpace);
}

public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
private VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR surfCapabilities) {
VkExtent2D result = VkExtent2D.calloc();
if (surfCapabilities.currentExtent().width() == 0xFFFFFFFF) {
// Surface size undefined. Set to the window size if within bounds
Expand Down

0 comments on commit 6009104

Please sign in to comment.