Skip to content

Commit

Permalink
Merge branch 'PerfPatches2' of https://github.com/thr3343/VulkanMod i…
Browse files Browse the repository at this point in the history
…nto PerfPatches2
  • Loading branch information
thr3343 committed Oct 6, 2024
2 parents 2903e54 + 261abf8 commit e00baed
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void copyBuffer(Buffer src, int srcOffset, Buffer dst, int dstOffset, int
public void syncUploads() {
submitUploads();

Synchronization.INSTANCE.waitFences();
Synchronization.INSTANCE.waitFences(false);
}

private void beginCommands() {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/vulkanmod/vulkan/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.vulkanmod.vulkan.memory.MemoryManager;
import net.vulkanmod.vulkan.pass.DefaultMainPass;
import net.vulkanmod.vulkan.pass.MainPass;
import net.vulkanmod.vulkan.queue.Queue;
import net.vulkanmod.vulkan.shader.GraphicsPipeline;
import net.vulkanmod.vulkan.shader.Pipeline;
import net.vulkanmod.vulkan.shader.PipelineState;
Expand Down Expand Up @@ -306,7 +307,7 @@ private void submitFrame() {

vkResetFences(device, inFlightFences.get(currentFrame));

Synchronization.INSTANCE.waitFences();
Synchronization.INSTANCE.waitFences(true);

if ((vkResult = vkQueueSubmit(DeviceManager.getGraphicsQueue().queue(), submitInfo, inFlightFences.get(currentFrame))) != VK_SUCCESS) {
vkResetFences(device, inFlightFences.get(currentFrame));
Expand Down Expand Up @@ -356,7 +357,7 @@ public void flushCmds() {

vkResetFences(device, inFlightFences.get(currentFrame));

Synchronization.INSTANCE.waitFences();
Synchronization.INSTANCE.waitFences(false);

if ((vkResult = vkQueueSubmit(DeviceManager.getGraphicsQueue().queue(), submitInfo, inFlightFences.get(currentFrame))) != VK_SUCCESS) {
vkResetFences(device, inFlightFences.get(currentFrame));
Expand Down Expand Up @@ -413,7 +414,7 @@ public void preInitFrame() {
// runTick might be called recursively,
// this check forces sync to avoid upload corruption
if (lastReset == currentFrame) {
Synchronization.INSTANCE.waitFences();
Synchronization.INSTANCE.waitFences(false);
}
lastReset = currentFrame;

Expand Down Expand Up @@ -461,11 +462,10 @@ void waitForSwapChain() {

@SuppressWarnings("UnreachableCode")
private void recreateSwapChain() {
Synchronization.INSTANCE.waitFences();
Synchronization.INSTANCE.waitFences(true);
Vulkan.waitIdle();

commandBuffers.forEach(commandBuffer -> vkResetCommandBuffer(commandBuffer, 0));

vkResetCommandPool(Vulkan.getVkDevice(), Vulkan.getCommandPool(), 0);
Vulkan.getSwapChain().recreate();

//Semaphores need to be recreated in order to make them unsignaled
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/net/vulkanmod/vulkan/Synchronization.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.vulkanmod.vulkan.queue.CommandPool;
import net.vulkanmod.vulkan.queue.Queue;
import net.vulkanmod.vulkan.util.VUtil;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.vulkan.VkDevice;
Expand Down Expand Up @@ -31,13 +32,13 @@ public synchronized void addCommandBuffer(CommandPool.CommandBuffer commandBuffe

public synchronized void addFence(long fence) {
if (idx == ALLOCATION_SIZE)
waitFences();
waitFences(false);

fences.put(idx, fence);
idx++;
}

public synchronized void waitFences() {
public synchronized void waitFences(boolean allowReset) {
if (idx == 0)
return;

Expand All @@ -47,8 +48,13 @@ public synchronized void waitFences() {

vkWaitForFences(device, fences, true, VUtil.UINT64_MAX);

this.commandBuffers.forEach(CommandPool.CommandBuffer::reset);
this.commandBuffers.clear();
if (allowReset) {
this.commandBuffers.forEach(CommandPool.CommandBuffer::reset);
this.commandBuffers.clear();

Queue.GraphicsQueue.resetPool();
Queue.TransferQueue.resetPool();
}

fences.limit(ALLOCATION_SIZE);
idx = 0;
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/net/vulkanmod/vulkan/queue/CommandPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CommandPool {
VkCommandPoolCreateInfo poolInfo = VkCommandPoolCreateInfo.calloc(stack);
poolInfo.sType(VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO);
poolInfo.queueFamilyIndex(queueFamilyIndex);
poolInfo.flags(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);
//Not using VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT enables additional driver and/or optimization benefits apparently

LongBuffer pCommandPool = stack.mallocLong(1);

Expand Down Expand Up @@ -114,6 +114,15 @@ public void cleanUp() {
vkDestroyCommandPool(Vulkan.getVkDevice(), id, null);
}

//Implicitly reset all CommandBuffers from this pool
public void resetAll() {
vkResetCommandPool(Vulkan.getVkDevice(), id, 0);
}

public void trim() {
VK11.vkTrimCommandPool(Vulkan.getVkDevice(), id, 0);
}

public class CommandBuffer {
final VkCommandBuffer handle;
final long fence;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/net/vulkanmod/vulkan/queue/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,10 @@ public void updateBuffer(CommandPool.CommandBuffer commandBuffer, long id, int b
nvkCmdUpdateBuffer(commandBuffer.getHandle(), id, baseOffset, sizeT, bufferPtr);

}

public void resetPool() {
if (this.commandPool != null)
this.commandPool.resetAll();
}
}

0 comments on commit e00baed

Please sign in to comment.