Skip to content

Commit

Permalink
Complete proper resource cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lwjglgamedev committed Sep 8, 2023
1 parent f0c241e commit 455d4ab
Show file tree
Hide file tree
Showing 26 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions bookcontents/chapter-13/chapter-13.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ public class ShadowRenderActivity {
GeometryAttachments.NUMBER_COLOR_ATTACHMENTS, true, true, GraphConstants.MAT4X4_SIZE,
new VertexBufferStructure(), descriptorSetLayouts);
pipeLine = new Pipeline(pipelineCache, pipeLineCreationInfo);
pipeLineCreationInfo.cleanup();
}
...
}
Expand Down
19 changes: 19 additions & 0 deletions bookcontents/chapter-14/chapter-14.md
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,10 @@ public class MemoryBarrier {
.dstAccessMask(dstAccessMask);
}

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

public VkMemoryBarrier.Buffer getVkMemoryBarrier() {
return vkMemoryBarrier;
}
Expand Down Expand Up @@ -856,6 +860,7 @@ public class AnimationComputeActivity {
for (Map.Entry<String, List<EntityAnimationBuffer>> entry : entityAnimationsBuffers.entrySet()) {
entry.getValue().forEach(EntityAnimationBuffer::cleanup);
}
memoryBarrier.cleanup();
}
...
}
Expand Down Expand Up @@ -1281,6 +1286,8 @@ public class GeometryRenderActivity {

You can see that the `recordCommandBuffer` has a new reference to the buffers that hold the animated vertices for each of the entities. This will be used in the `recordEntities` method. In this method, we just need to check if the entity is related to a model that has animations or not. If so, instead of using the data associated to the meshes of the model, we use the buffer associated to the animation for that entity.
```java
public class GeometryRenderActivity {
...
private void recordEntities(MemoryStack stack, VkCommandBuffer cmdHandle, LongBuffer descriptorSets,
List<VulkanModel> vulkanModelList,
Map<String, List<AnimationComputeActivity.EntityAnimationBuffer>> entityAnimationsBuffers) {
Expand Down Expand Up @@ -1324,6 +1331,18 @@ You can see that the `recordCommandBuffer` has a new reference to the buffers th
}
```

Finally, we need to free the memory barrier in the `cleanup` method:
```java
public class GeometryRenderActivity {
...
public void cleanup() {
...
memoryBarrier.cleanup();
}
...
}
```

Note that we do not need to modify the geometry shaders. Animated models are rendered exactly the same way as non animated ways. The only thing that we need to do is select the appropriate buffer.

## Updates on shadow rendering
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void cleanup() {
descriptorPool.cleanup();
storageDescriptorSetLayout.cleanup();
fence.cleanup();
memoryBarrier.cleanup();
}

private void createCommandBuffers(CommandPool commandPool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void cleanup() {
descriptorPool.cleanup();
shaderProgram.cleanup();
geometryFrameBuffer.cleanup();
memoryBarrier.cleanup();
}

private void createDescriptorPool() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private void createPipeline(PipelineCache pipelineCache) {
GeometryAttachments.NUMBER_COLOR_ATTACHMENTS, true, true, 0,
new InstancedVertexBufferStructure(), descriptorSetLayouts);
pipeLine = new Pipeline(pipelineCache, pipeLineCreationInfo);
pipeLineCreationInfo.cleanup();
}

private void createShaders() {
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 @@ -55,6 +55,7 @@ public void cleanup() {
descriptorPool.cleanup();
storageDescriptorSetLayout.cleanup();
fence.cleanup();
memoryBarrier.cleanup();
}

private void createCommandBuffers(CommandPool commandPool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void cleanup() {
descriptorPool.cleanup();
shaderProgram.cleanup();
geometryFrameBuffer.cleanup();
memoryBarrier.cleanup();
}

private void createDescriptorPool() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private void createPipeline(PipelineCache pipelineCache) {
GeometryAttachments.NUMBER_COLOR_ATTACHMENTS, true, true, 0,
new InstancedVertexBufferStructure(), descriptorSetLayouts);
pipeLine = new Pipeline(pipelineCache, pipeLineCreationInfo);
pipeLineCreationInfo.cleanup();
}

private void createShaders() {
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 @@ -102,6 +102,7 @@ private void createPipeline(PipelineCache pipelineCache) {
GeometryAttachments.NUMBER_COLOR_ATTACHMENTS, true, true, GraphConstants.MAT4X4_SIZE,
new VertexBufferStructure(), descriptorSetLayouts);
pipeLine = new Pipeline(pipelineCache, pipeLineCreationInfo);
pipeLineCreationInfo.cleanup();
}

private void createShaders() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void cleanup() {
for (Map.Entry<String, List<EntityAnimationBuffer>> entry : entityAnimationsBuffers.entrySet()) {
entry.getValue().forEach(EntityAnimationBuffer::cleanup);
}
memoryBarrier.cleanup();
}

private void createCommandBuffers(CommandPool commandPool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public void cleanup() {
geometryFrameBuffer.cleanup();
Arrays.asList(commandBuffers).forEach(CommandBuffer::cleanup);
Arrays.asList(fences).forEach(Fence::cleanup);
memoryBarrier.cleanup();
}

private void createCommandBuffers(CommandPool commandPool, int numImages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private void createPipeline(PipelineCache pipelineCache) {
GeometryAttachments.NUMBER_COLOR_ATTACHMENTS, true, true, GraphConstants.MAT4X4_SIZE,
new VertexBufferStructure(), descriptorSetLayouts);
pipeLine = new Pipeline(pipelineCache, pipeLineCreationInfo);
pipeLineCreationInfo.cleanup();
}

private void createShaders() {
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 @@ -62,6 +62,7 @@ public void cleanup() {
for (Map.Entry<String, List<EntityAnimationBuffer>> entry : entityAnimationsBuffers.entrySet()) {
entry.getValue().forEach(EntityAnimationBuffer::cleanup);
}
memoryBarrier.cleanup();
}

private void createCommandBuffers(CommandPool commandPool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public void cleanup() {
geometryFrameBuffer.cleanup();
Arrays.asList(commandBuffers).forEach(CommandBuffer::cleanup);
Arrays.asList(fences).forEach(Fence::cleanup);
memoryBarrier.cleanup();
}

private void createCommandBuffers(CommandPool commandPool, int numImages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ private void createPipeline(PipelineCache pipelineCache) {
GeometryAttachments.NUMBER_COLOR_ATTACHMENTS, true, true, GraphConstants.MAT4X4_SIZE,
new VertexBufferStructure(), descriptorSetLayouts);
pipeLine = new Pipeline(pipelineCache, pipeLineCreationInfo);
pipeLineCreationInfo.cleanup();
}

private void createShaders() {
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 @@ -55,6 +55,7 @@ public void cleanup() {
descriptorPool.cleanup();
storageDescriptorSetLayout.cleanup();
fence.cleanup();
memoryBarrier.cleanup();
}

private void createCommandBuffers(CommandPool commandPool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void cleanup() {
descriptorPool.cleanup();
shaderProgram.cleanup();
geometryFrameBuffer.cleanup();
memoryBarrier.cleanup();
}

private void createDescriptorPool() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private void createPipeline(PipelineCache pipelineCache) {
GeometryAttachments.NUMBER_COLOR_ATTACHMENTS, true, true, 0,
new InstancedVertexBufferStructure(), descriptorSetLayouts);
pipeLine = new Pipeline(pipelineCache, pipeLineCreationInfo);
pipeLineCreationInfo.cleanup();
}

private void createShaders() {
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 @@ -55,6 +55,7 @@ public void cleanup() {
descriptorPool.cleanup();
storageDescriptorSetLayout.cleanup();
fence.cleanup();
memoryBarrier.cleanup();
}

private void createCommandBuffers(CommandPool commandPool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void cleanup() {
descriptorPool.cleanup();
shaderProgram.cleanup();
geometryFrameBuffer.cleanup();
memoryBarrier.cleanup();
}

private void createDescriptorPool() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private void createPipeline(PipelineCache pipelineCache) {
GeometryAttachments.NUMBER_COLOR_ATTACHMENTS, true, true, 0,
new InstancedVertexBufferStructure(), descriptorSetLayouts);
pipeLine = new Pipeline(pipelineCache, pipeLineCreationInfo);
pipeLineCreationInfo.cleanup();
}

private void createShaders() {
Expand Down

0 comments on commit 455d4ab

Please sign in to comment.