Skip to content

Commit 521c8a7

Browse files
committed
move pool management to logical device
1 parent 4a1970f commit 521c8a7

File tree

10 files changed

+74
-47
lines changed

10 files changed

+74
-47
lines changed

jme3-examples/src/main/java/jme3test/vulkan/VulkanHelperTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void simpleInitApp() {
165165
new PoolSize(Descriptor.StorageBuffer, 4),
166166
new PoolSize(Descriptor.CombinedImageSampler, 2));
167167

168-
CommandPool transferPool = new CommandPool(device, physDevice.getGraphics(), true, false);
168+
CommandPool transferPool = device.getShortTermPool(physDevice.getGraphics());
169169

170170
// depth texture
171171
depthView = createDepthAttachment(transferPool);
@@ -215,7 +215,7 @@ public void simpleInitApp() {
215215
p.getColorBlend().addAttachment(new ColorBlendAttachment());
216216
p.getDynamicState().addTypes(DynamicState.Type.ViewPort, DynamicState.Type.Scissor);
217217
}
218-
graphicsPool = new CommandPool(device, physDevice.getGraphics(), false, true);
218+
graphicsPool = device.getLongTermPool(physDevice.getGraphics());
219219

220220
// vertex buffers
221221
try (MemoryStack stack = MemoryStack.stackPush()) {
@@ -266,7 +266,7 @@ public boolean swapchainOutOfDate(Swapchain swapchain, int imageAcquireCode) {
266266
s.selectExtentByWindow();
267267
s.selectImageCount(2);
268268
}
269-
depthView = createDepthAttachment(new CommandPool(device, device.getPhysicalDevice().getGraphics(), true, false));
269+
depthView = createDepthAttachment(device.getShortTermPool(device.getPhysicalDevice().getGraphics()));
270270
swapchain.createFrameBuffers(renderPass, depthView);
271271
return true;
272272
}

jme3-lwjgl3/src/main/java/com/jme3/vulkan/commands/CommandAllocator.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

jme3-lwjgl3/src/main/java/com/jme3/vulkan/commands/CommandPool.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,34 @@
99
import java.nio.LongBuffer;
1010

1111
import static com.jme3.renderer.vulkan.VulkanUtils.*;
12-
import static org.lwjgl.vulkan.VK10.*;
12+
import static org.lwjgl.vulkan.VK11.*;
1313

1414
public class CommandPool implements Native<Long> {
1515

16-
private final LogicalDevice<?> device;
1716
private final Queue queue;
1817
private final NativeReference ref;
18+
private final boolean shortLived, reusable, protect;
1919
private long id;
2020

21-
public CommandPool(LogicalDevice<?> device, Queue queue, boolean isTransient, boolean reset) {
22-
this.device = device;
21+
public CommandPool(Queue queue, boolean shortLived, boolean reusable, boolean protect) {
2322
this.queue = queue;
23+
this.shortLived = shortLived;
24+
this.reusable = reusable;
25+
this.protect = protect;
2426
try (MemoryStack stack = MemoryStack.stackPush()) {
2527
VkCommandPoolCreateInfo create = VkCommandPoolCreateInfo.calloc(stack)
2628
.sType(VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO)
27-
.flags((isTransient ? VK_COMMAND_POOL_CREATE_TRANSIENT_BIT : 0)
28-
| (reset ? VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT : 0))
29+
.flags((shortLived ? VK_COMMAND_POOL_CREATE_TRANSIENT_BIT : 0)
30+
| (reusable ? VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT : 0)
31+
| (protect ? VK_COMMAND_POOL_CREATE_PROTECTED_BIT : 0))
2932
.queueFamilyIndex(queue.getFamilyIndex());
3033
LongBuffer idBuf = stack.mallocLong(1);
31-
check(vkCreateCommandPool(device.getNativeObject(), create, null, idBuf), "Failed to create command pool.");
34+
check(vkCreateCommandPool(queue.getDevice().getNativeObject(), create, null, idBuf),
35+
"Failed to create command pool.");
3236
id = idBuf.get(0);
3337
}
3438
ref = Native.get().register(this);
35-
device.getNativeReference().addDependent(ref);
39+
queue.getDevice().getNativeReference().addDependent(ref);
3640
}
3741

3842
@Override
@@ -43,7 +47,7 @@ public Long getNativeObject() {
4347
@Override
4448
public Runnable createNativeDestroyer() {
4549
return () -> {
46-
vkDestroyCommandPool(device.getNativeObject(), id, null);
50+
vkDestroyCommandPool(queue.getDevice().getNativeObject(), id, null);
4751
};
4852
}
4953

@@ -66,11 +70,23 @@ public OneTimeCommandBuffer allocateOneTimeCommandBuffer() {
6670
}
6771

6872
public LogicalDevice<?> getDevice() {
69-
return device;
73+
return queue.getDevice();
7074
}
7175

7276
public Queue getQueue() {
7377
return queue;
7478
}
7579

80+
public boolean isShortLived() {
81+
return shortLived;
82+
}
83+
84+
public boolean isReusable() {
85+
return reusable;
86+
}
87+
88+
public boolean isProtected() {
89+
return protect;
90+
}
91+
7692
}

jme3-lwjgl3/src/main/java/com/jme3/vulkan/commands/Queue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void waitIdle() {
4040
vkQueueWaitIdle(queue);
4141
}
4242

43-
public LogicalDevice getDevice() {
43+
public LogicalDevice<?> getDevice() {
4444
return device;
4545
}
4646

jme3-lwjgl3/src/main/java/com/jme3/vulkan/devices/AbstractPhysicalDevice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public VulkanInstance getInstance() {
2828
}
2929

3030
@Override
31-
public VkPhysicalDevice getPhysicalDevice() {
31+
public VkPhysicalDevice getDeviceHandle() {
3232
return physicalDevice;
3333
}
3434

jme3-lwjgl3/src/main/java/com/jme3/vulkan/devices/GeneralPhysicalDevice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public boolean populateQueueFamilyIndices() {
3535
if (graphicsIndex == null && (flags & VK_QUEUE_GRAPHICS_BIT) > 0) {
3636
graphicsIndex = i;
3737
} else if (presentIndex == null) {
38-
KHRSurface.vkGetPhysicalDeviceSurfaceSupportKHR(getPhysicalDevice(),
38+
KHRSurface.vkGetPhysicalDeviceSurfaceSupportKHR(getDeviceHandle(),
3939
i, surface.getNativeObject(), ibuf);
4040
if (ibuf.get(0) == VK_TRUE) {
4141
presentIndex = i;

jme3-lwjgl3/src/main/java/com/jme3/vulkan/devices/LogicalDevice.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import com.jme3.util.natives.Native;
44
import com.jme3.vulkan.VulkanInstance;
55
import com.jme3.vulkan.VulkanObject;
6+
import com.jme3.vulkan.commands.CommandPool;
7+
import com.jme3.vulkan.commands.Queue;
68
import org.lwjgl.PointerBuffer;
79
import org.lwjgl.system.MemoryStack;
810
import org.lwjgl.vulkan.*;
911

1012
import java.util.*;
13+
import java.util.concurrent.ConcurrentHashMap;
1114
import java.util.function.Function;
1215

1316
import static com.jme3.renderer.vulkan.VulkanUtils.*;
@@ -18,6 +21,7 @@ public class LogicalDevice <T extends PhysicalDevice> extends VulkanObject<VkDev
1821
private final VulkanInstance instance;
1922
private final Set<String> enabledExtensions = new HashSet<>();
2023
private final VkPhysicalDeviceFeatures enabledFeatures = VkPhysicalDeviceFeatures.calloc();
24+
private final Map<Thread, Collection<CommandPool>> pools = new ConcurrentHashMap<>();
2125
private T physical;
2226

2327
public LogicalDevice(VulkanInstance instance) {
@@ -48,6 +52,35 @@ public VkPhysicalDeviceFeatures getEnabledFeatures() {
4852
return enabledFeatures;
4953
}
5054

55+
public CommandPool getShortTermPool(Queue queue) {
56+
return getPool(queue, true, false);
57+
}
58+
59+
public CommandPool getLongTermPool(Queue queue) {
60+
return getPool(queue, false, true);
61+
}
62+
63+
public CommandPool getPool(Queue queue, boolean shortLived, boolean reusable) {
64+
return getPool(queue, shortLived, reusable, false);
65+
}
66+
67+
public CommandPool getPool(Queue queue, boolean shortLived, boolean reusable, boolean protect) {
68+
if (queue.getDevice() != this) {
69+
throw new IllegalArgumentException("Queue must belong to this device.");
70+
}
71+
Collection<CommandPool> p = pools.computeIfAbsent(Thread.currentThread(),
72+
t -> new ArrayList<>());
73+
for (CommandPool pool : p) {
74+
if (pool.getQueue() == queue && pool.isShortLived() == shortLived
75+
&& pool.isReusable() == reusable && pool.isProtected() == protect) {
76+
return pool;
77+
}
78+
}
79+
CommandPool pool = new CommandPool(queue, shortLived, reusable, protect);
80+
p.add(pool);
81+
return pool;
82+
}
83+
5184
public Builder build(Function<Long, T> deviceFactory) {
5285
return new Builder(deviceFactory);
5386
}
@@ -96,9 +129,9 @@ protected void build() {
96129
create.ppEnabledLayerNames(lyrs.flip());
97130
}
98131
PointerBuffer ptr = stack.mallocPointer(1);
99-
check(vkCreateDevice(physical.getPhysicalDevice(), create, null, ptr),
132+
check(vkCreateDevice(physical.getDeviceHandle(), create, null, ptr),
100133
"Failed to create logical device.");
101-
object = new VkDevice(ptr.get(0), physical.getPhysicalDevice(), create);
134+
object = new VkDevice(ptr.get(0), physical.getDeviceHandle(), create);
102135
ref = Native.get().register(LogicalDevice.this);
103136
physical.getInstance().getNativeReference().addDependent(ref);
104137
physical.createQueues(LogicalDevice.this);

jme3-lwjgl3/src/main/java/com/jme3/vulkan/devices/PhysicalDevice.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
import org.lwjgl.system.MemoryStack;
77
import org.lwjgl.vulkan.*;
88

9-
import java.nio.ByteBuffer;
10-
import java.nio.IntBuffer;
11-
12-
import static com.jme3.renderer.vulkan.VulkanUtils.*;
13-
import static org.lwjgl.vulkan.VK10.*;
14-
159
public interface PhysicalDevice {
1610

1711
boolean populateQueueFamilyIndices();
@@ -22,7 +16,7 @@ public interface PhysicalDevice {
2216

2317
VulkanInstance getInstance();
2418

25-
VkPhysicalDevice getPhysicalDevice();
19+
VkPhysicalDevice getDeviceHandle();
2620

2721
VkQueueFamilyProperties.Buffer getQueueFamilyProperties(MemoryStack stack);
2822

jme3-lwjgl3/src/main/java/com/jme3/vulkan/surface/Surface.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public Surface(VulkanInstance instance, long window) {
3737
public Float evaluateDevice(PhysicalDevice device) {
3838
try (MemoryStack stack = MemoryStack.stackPush()) {
3939
IntBuffer count = stack.mallocInt(1);
40-
KHRSurface.vkGetPhysicalDeviceSurfaceFormatsKHR(device.getPhysicalDevice(), id, count, null);
40+
KHRSurface.vkGetPhysicalDeviceSurfaceFormatsKHR(device.getDeviceHandle(), id, count, null);
4141
if (count.get(0) == 0) {
4242
System.out.println("Reject device by surface support (formats)");
4343
return null;
4444
}
45-
KHRSurface.vkGetPhysicalDeviceSurfacePresentModesKHR(device.getPhysicalDevice(), id, count, null);
45+
KHRSurface.vkGetPhysicalDeviceSurfacePresentModesKHR(device.getDeviceHandle(), id, count, null);
4646
if (count.get(0) == 0) {
4747
System.out.println("Reject device by surface support (present modes)");
4848
return null;

jme3-lwjgl3/src/main/java/com/jme3/vulkan/surface/Swapchain.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,12 @@ public class Builder extends VulkanObject.Builder<Swapchain> {
221221

222222
public Builder() {
223223
caps = VkSurfaceCapabilitiesKHR.malloc(stack);
224-
KHRSurface.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device.getPhysicalDevice().getPhysicalDevice(), surface.getNativeObject(), caps);
224+
KHRSurface.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device.getPhysicalDevice().getDeviceHandle(), surface.getNativeObject(), caps);
225225
formats = enumerateBuffer(stack, n -> VkSurfaceFormatKHR.malloc(n, stack), (count, buffer)
226-
-> KHRSurface.vkGetPhysicalDeviceSurfaceFormatsKHR(device.getPhysicalDevice().getPhysicalDevice(),
226+
-> KHRSurface.vkGetPhysicalDeviceSurfaceFormatsKHR(device.getPhysicalDevice().getDeviceHandle(),
227227
surface.getNativeObject(), count, buffer));
228228
modes = enumerateBuffer(stack, stack::mallocInt, (count, buffer) ->
229-
KHRSurface.vkGetPhysicalDeviceSurfacePresentModesKHR(device.getPhysicalDevice().getPhysicalDevice(),
229+
KHRSurface.vkGetPhysicalDeviceSurfacePresentModesKHR(device.getPhysicalDevice().getDeviceHandle(),
230230
surface.getNativeObject(), count, buffer));
231231
if (formats == null || modes == null) {
232232
throw new UnsupportedOperationException("Swapchains are not supported by the device.");
@@ -253,7 +253,7 @@ protected void build() {
253253
}
254254
VkSurfaceCapabilitiesKHR caps = VkSurfaceCapabilitiesKHR.calloc(stack);
255255
KHRSurface.vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
256-
device.getPhysicalDevice().getPhysicalDevice(), surface.getNativeObject(), caps);
256+
device.getPhysicalDevice().getDeviceHandle(), surface.getNativeObject(), caps);
257257
format = Image.Format.vkEnum(selectedFormat.format());
258258
extent = new Extent2(selectedExtent);
259259
VkSwapchainCreateInfoKHR create = VkSwapchainCreateInfoKHR.calloc(stack)

0 commit comments

Comments
 (0)