-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVulkanApplicationPerFrameThreadObject.java
More file actions
109 lines (81 loc) · 4.23 KB
/
VulkanApplicationPerFrameThreadObject.java
File metadata and controls
109 lines (81 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import static org.lwjgl.system.MemoryUtil.memAllocLong;
import static org.lwjgl.system.MemoryUtil.memAllocPointer;
import static org.lwjgl.system.MemoryUtil.memFree;
import static org.lwjgl.vulkan.VK10.VK_COMMAND_BUFFER_LEVEL_PRIMARY;
import static org.lwjgl.vulkan.VK10.VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
import static org.lwjgl.vulkan.VK10.VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
import static org.lwjgl.vulkan.VK10.VK_SUCCESS;
import static org.lwjgl.vulkan.VK10.vkAllocateCommandBuffers;
import static org.lwjgl.vulkan.VK10.vkCreateCommandPool;
import java.nio.LongBuffer;
import org.lwjgl.PointerBuffer;
import org.lwjgl.vulkan.VkCommandBuffer;
import org.lwjgl.vulkan.VkCommandBufferAllocateInfo;
import org.lwjgl.vulkan.VkCommandPoolCreateInfo;
public abstract class VulkanApplicationPerFrameThreadObject<U extends VulkanApplication<? extends VulkanApplicationPerFrameThreadObject<U>>> extends Thread{
protected final LongBuffer graphicsCommandPools = memAllocLong(3);
protected final LongBuffer transferCommandPools = memAllocLong(1);
protected final U parentProcess;
protected VkCommandBuffer[] commandBuffers;
public VulkanApplicationPerFrameThreadObject(U parentProcess) {
synchronized(this) {
this.parentProcess = parentProcess;
}
}
protected void createCommandPools() {
VkCommandPoolCreateInfo graphicsCommandPoolCreateInfo = VkCommandPoolCreateInfo.calloc();
graphicsCommandPoolCreateInfo.sType(VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO);
graphicsCommandPoolCreateInfo.queueFamilyIndex(parentProcess.queueFamilyIndices[0]);
graphicsCommandPoolCreateInfo.flags(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT);
LongBuffer currentCommanPool = memAllocLong(1);
int err;
for(int i=0;i<graphicsCommandPools.limit();++i) {
err = vkCreateCommandPool(parentProcess.device, graphicsCommandPoolCreateInfo, null, currentCommanPool);
if (err != VK_SUCCESS) {
throw new AssertionError("Failed to create graphicsCommandPool: " + err);
}
graphicsCommandPools.put(i, currentCommanPool.get(0));
}
VkCommandPoolCreateInfo transferCommandPoolCreateInfo = VkCommandPoolCreateInfo.calloc();
transferCommandPoolCreateInfo.sType(VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO);
transferCommandPoolCreateInfo.queueFamilyIndex(parentProcess.queueFamilyIndices[2]);
transferCommandPoolCreateInfo.flags(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT);
for(int i=0;i<transferCommandPools.limit();++i) {
err = vkCreateCommandPool(parentProcess.device, transferCommandPoolCreateInfo, null, currentCommanPool);
if (err != VK_SUCCESS) {
throw new AssertionError("Failed to create transferCommandPool: " + err);
}
transferCommandPools.put(i, currentCommanPool.get(0));
}
graphicsCommandPoolCreateInfo.free();
transferCommandPoolCreateInfo.free();
memFree(currentCommanPool);
}
protected long[] allocateCommandBuffers() {
VkCommandBufferAllocateInfo commandBufferAllocateInfo = VkCommandBufferAllocateInfo.calloc();
commandBufferAllocateInfo.sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO);
commandBufferAllocateInfo.level(VK_COMMAND_BUFFER_LEVEL_PRIMARY);
commandBufferAllocateInfo.commandBufferCount(1);
long[] ret = new long[graphicsCommandPools.limit()];
PointerBuffer currentCommanBufferPointer = memAllocPointer(1);
int err;
for(int i=0;i<graphicsCommandPools.limit();++i) {
commandBufferAllocateInfo.commandPool(graphicsCommandPools.get(i));
err = vkAllocateCommandBuffers(parentProcess.device, commandBufferAllocateInfo, currentCommanBufferPointer);
if (err != VK_SUCCESS) {
throw new AssertionError("Failed to allocate CommandBuffers: " + err);
}
ret[i] = currentCommanBufferPointer.get(0);
}
commandBufferAllocateInfo.free();
memFree(currentCommanBufferPointer);
return ret;
}
protected abstract void recordCommandBuffer();
@Override
public abstract void run();
protected abstract void cleanup();
protected abstract void cleanupBuffers();
}