Skip to content

Commit

Permalink
refavtor
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatMG393 committed Aug 11, 2024
1 parent 4124431 commit a80d0a8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 53 deletions.
2 changes: 0 additions & 2 deletions src/main/java/com/thatmg393/vkapi/gpu/GPUManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ public void populateSupportedGPUs(VkInstance instance) {

public boolean isGPUSupported(VkPhysicalDevice device) {
PointerBuffer glfwReqExt = GLFWVulkan.glfwGetRequiredInstanceExtensions();


return true;
}
}
33 changes: 9 additions & 24 deletions src/main/java/com/thatmg393/vkapi/queue/CommandPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import org.lwjgl.vulkan.VkQueue;
import org.lwjgl.vulkan.VkSubmitInfo;

import com.thatmg393.vkapi.gpu.GPUManager;
import com.thatmg393.vkapi.utils.ResultChecker;

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import lombok.Getter;

public class CommandPool implements AutoCloseable {
private static final int DEFAULT_COMMAND_BUFFER_SIZE = 10;
Expand All @@ -33,11 +33,11 @@ public class CommandPool implements AutoCloseable {
private final VkDevice currentDevice;
private final long commandPoolPtr;

public CommandPool(VkDevice currentDevice, int queueFamIndex) {
public CommandPool(VkDevice currentDevice, int familyIndex) {
try (MemoryStack stack = MemoryStack.stackPush()) {
VkCommandPoolCreateInfo vcpci = VkCommandPoolCreateInfo.calloc(stack);
vcpci.sType(VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO);
vcpci.queueFamilyIndex(queueFamIndex);
vcpci.queueFamilyIndex(familyIndex);
vcpci.flags(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);

LongBuffer commandPoolPtr = stack.mallocLong(1);
Expand Down Expand Up @@ -113,7 +113,7 @@ public CommandBuffer beginCommands() {
public long submitCommands(CommandBuffer cb, VkQueue queue) {
try (MemoryStack stack = MemoryStack.stackPush()) {
vkEndCommandBuffer(cb.cmdBufHandle);
vkResetFences(GPUManager.getInstance().getSelectedGPU().asLogicalDevice(), cb.fenceHandle);
vkResetFences(currentDevice, cb.fenceHandle);

VkSubmitInfo vsi = VkSubmitInfo.calloc(stack);
vsi.sType(VK_STRUCTURE_TYPE_SUBMIT_INFO);
Expand All @@ -136,34 +136,19 @@ private void addToAvailableBuffers(CommandBuffer availCb) {
availableCommandBuffers.add(availCb);
}

@Getter
public class CommandBuffer {
final VkCommandBuffer cmdBufHandle;
final long fenceHandle;
private final VkCommandBuffer cmdBufHandle;
private final long fenceHandle;

boolean isSubmitted;
boolean isRecording;
private boolean isSubmitted;
private boolean isRecording;

CommandBuffer(VkCommandBuffer cmdBufHandle, long fenceHandle) {
this.cmdBufHandle = cmdBufHandle;
this.fenceHandle = fenceHandle;
}

public VkCommandBuffer getCmdBufHandle() {
return cmdBufHandle;
}

public long getFenceHandle() {
return fenceHandle;
}

public boolean isSubmitted() {
return isSubmitted;
}

public boolean isRecording() {
return isRecording;
}

public void reset() {
isSubmitted = false;
isRecording = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
public class QueueFamilyIndices {
@Getter
private static int graphicsFamily = VK_QUEUE_FAMILY_IGNORED, presentFamily = VK_QUEUE_FAMILY_IGNORED, transferFamily = VK_QUEUE_FAMILY_IGNORED;

@Getter
private static boolean hasDedicatedTransferQueue = false;

public static boolean findQueueFamilies(VkPhysicalDevice physicalDevice) {
Expand Down Expand Up @@ -89,8 +91,4 @@ public static boolean isFamilyComplete() {
public static int[] uniqueFamily() {
return IntStream.of(graphicsFamily, presentFamily, transferFamily).distinct().toArray();
}

public static boolean hasDedicatedTransferQueue() {
return hasDedicatedTransferQueue;
}
}
46 changes: 27 additions & 19 deletions src/main/java/com/thatmg393/vkapi/queue/VkQueues.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,40 @@

import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.VkBufferCopy;
import org.lwjgl.vulkan.VkCommandBuffer;
import org.lwjgl.vulkan.VkQueue;
import org.lwjgl.vulkan.*;

import com.thatmg393.vkapi.gpu.GPUManager;
import com.thatmg393.vkapi.Vulkan;
import com.thatmg393.vkapi.synchronization.SyncCommandBuffer;

import lombok.Getter;

public enum VkQueues implements AutoCloseable {
GraphicsQueue(QueueFamilyIndices.getGraphicsFamily(), false);
GraphicsQueue(QueueFamilyIndices.getGraphicsFamily(), false),
PresentQueue(QueueFamilyIndices.getPresentFamily(), false),
TransferQueue(QueueFamilyIndices.getTransferFamily(), false);

private final int familyIndex;
private final CommandPool commandPool;
private final VkQueue queue;
private final VkDevice currentDevice;
private final CommandPool commandPool;

@Getter
private final int familyIndex;

private CommandPool.CommandBuffer currentCmdBuf;

VkQueues(int familyIndex, boolean initCommandPool) {
this.currentDevice = Vulkan.getInstance().getCurrentGPU().asLogicalDevice();
this.familyIndex = familyIndex;
this.commandPool = initCommandPool ? new CommandPool(GPUManager.getInstance().getSelectedGPU().asLogicalDevice(), familyIndex) : null;
this.commandPool = initCommandPool ? new CommandPool(currentDevice, familyIndex) : null;

try (MemoryStack stack = MemoryStack.stackPush()) {
PointerBuffer queuePtr = stack.mallocPointer(1);
vkGetDeviceQueue(
GPUManager.getInstance().getSelectedGPU().asLogicalDevice(),
currentDevice,
familyIndex, 0, queuePtr
);

this.queue = new VkQueue(queuePtr.get(0), GPUManager.getInstance().getSelectedGPU().asLogicalDevice());
this.queue = new VkQueue(queuePtr.get(0), currentDevice);
}
}

Expand All @@ -41,19 +49,22 @@ public long submitCommands(CommandPool.CommandBuffer cb) {
return this.commandPool.submitCommands(cb, queue);
}

public void copyBufferCommand(long srcBuf, long srcOff, long dstBuf, long dstOff, int size) {
public long copyBufferCommand(long srcBuf, long srcOff, long dstBuf, long dstOff, int size) {
try (MemoryStack stack = MemoryStack.stackPush()) {
CommandPool.CommandBuffer cb = beginCommands();
uploadBufferCmd(cb.cmdBufHandle, srcBuf, srcOff, dstBuf, dstOff, size);
uploadBufferCmd(cb.getCmdBufHandle(), srcBuf, srcOff, dstBuf, dstOff, size);

submitCommands(cb);
SyncCommandBuffer.getInstance().add(cb);
return cb.getFenceHandle();
}
}

public void uploadCmdBufImmediate(long srcBuf, long srcOff, long dstBuf, long dstOff, int size) {
try (MemoryStack stack = MemoryStack.stackPush()) {
CommandPool.CommandBuffer cb = beginCommands();
uploadBufferCmd(cb.cmdBufHandle, srcBuf, srcOff, dstBuf, dstOff, size);
vkWaitForFences(GPUManager.getInstance().getSelectedGPU().asLogicalDevice(), cb.fenceHandle, true, Long.MAX_VALUE);
uploadBufferCmd(cb.getCmdBufHandle(), srcBuf, srcOff, dstBuf, dstOff, size);
vkWaitForFences(currentDevice, cb.getFenceHandle(), true, Long.MAX_VALUE);
cb.reset();
}
}
Expand All @@ -74,7 +85,8 @@ public void startRecording() {
}

public void endRecordingAndSubmit() {
long fence = submitCommands(currentCmdBuf);
submitCommands(currentCmdBuf);
SyncCommandBuffer.getInstance().add(currentCmdBuf);
currentCmdBuf = null;
}

Expand All @@ -90,8 +102,4 @@ public void waitIdle() {
public void close() {
if (commandPool != null) commandPool.close();
}

public int getFamilyIndex() {
return this.familyIndex;
}
}
6 changes: 2 additions & 4 deletions src/main/java/com/thatmg393/vkapi/vma/VMAManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.util.vma.VmaAllocationCreateInfo;
import org.lwjgl.util.vma.VmaAllocatorCreateInfo;
import org.lwjgl.vulkan.VkBufferCreateInfo;
import org.lwjgl.vulkan.VkImageCreateInfo;

Expand All @@ -19,7 +18,6 @@
import com.thatmg393.vkapi.image.base.BaseImage;
import com.thatmg393.vkapi.utils.ResultChecker;

import lombok.Builder;
import lombok.Getter;

public class VMAManager {
Expand All @@ -28,7 +26,7 @@ public class VMAManager {

private VMAManager() { }

private final HashMap<Long, BaseImage> images = new HashMap<>();
private final HashMap<Long, BaseImage<? extends BaseImage.Builder>> images = new HashMap<>();

public void createBuffer(long size, int usage, int memoryFlags, LongBuffer bufferPtr, PointerBuffer memoryAllocPtr) {
try (MemoryStack stack = MemoryStack.stackPush()) {
Expand Down Expand Up @@ -66,7 +64,7 @@ public void mapBuffer(long allocation, PointerBuffer data) {
vmaMapMemory(getVMA(), allocation, data);
}

public void addImage(BaseImage image) {
public void addImage(BaseImage<? extends BaseImage.Builder> image) {
images.putIfAbsent(image.getId(), image);
}

Expand Down

0 comments on commit a80d0a8

Please sign in to comment.