Skip to content

Commit ba4ea7f

Browse files
committed
use Flag for CommandPool creation instead of booleans
1 parent 42a845a commit ba4ea7f

File tree

4 files changed

+42
-32
lines changed

4 files changed

+42
-32
lines changed

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

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.jme3.util.natives.Native;
44
import com.jme3.util.natives.NativeReference;
55
import com.jme3.vulkan.devices.LogicalDevice;
6+
import com.jme3.vulkan.util.Flag;
67
import org.lwjgl.system.MemoryStack;
78
import org.lwjgl.vulkan.VkCommandPoolCreateInfo;
89

@@ -13,22 +14,41 @@
1314

1415
public class CommandPool implements Native<Long> {
1516

17+
public enum Create implements Flag<Create> {
18+
19+
Transient(VK_COMMAND_POOL_CREATE_TRANSIENT_BIT),
20+
ResetCommandBuffer(VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT),
21+
Protected(VK_COMMAND_POOL_CREATE_PROTECTED_BIT);
22+
23+
private final int vkEnum;
24+
25+
Create(int vkEnum) {
26+
this.vkEnum = vkEnum;
27+
}
28+
29+
@Override
30+
public int bits() {
31+
return vkEnum;
32+
}
33+
34+
}
35+
1636
private final Queue queue;
1737
private final NativeReference ref;
18-
private final boolean shortLived, reusable, protect;
38+
private final Flag<Create> flags;
1939
private long id;
2040

21-
public CommandPool(Queue queue, boolean shortLived, boolean reusable, boolean protect) {
41+
public CommandPool(Queue queue) {
42+
this(queue, Create.ResetCommandBuffer);
43+
}
44+
45+
public CommandPool(Queue queue, Flag<Create> flags) {
2246
this.queue = queue;
23-
this.shortLived = shortLived;
24-
this.reusable = reusable;
25-
this.protect = protect;
47+
this.flags = flags;
2648
try (MemoryStack stack = MemoryStack.stackPush()) {
2749
VkCommandPoolCreateInfo create = VkCommandPoolCreateInfo.calloc(stack)
2850
.sType(VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO)
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))
51+
.flags(this.flags.bits())
3252
.queueFamilyIndex(queue.getFamilyIndex());
3353
LongBuffer idBuf = stack.mallocLong(1);
3454
check(vkCreateCommandPool(queue.getDevice().getNativeObject(), create, null, idBuf),
@@ -77,16 +97,8 @@ public Queue getQueue() {
7797
return queue;
7898
}
7999

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;
100+
public Flag<Create> getFlags() {
101+
return flags;
90102
}
91103

92104
}

jme3-lwjgl3/src/main/java/com/jme3/vulkan/descriptors/DescriptorPool.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ public void reset() {
106106
vkResetDescriptorPool(device.getNativeObject(), id, 0);
107107
}
108108

109-
public boolean isFreeSetsEnabled() {
110-
return flags.contains(Create.FreeDescriptorSets);
109+
public Flag<Create> getFlags() {
110+
return flags;
111111
}
112112

113113
}

jme3-lwjgl3/src/main/java/com/jme3/vulkan/descriptors/DescriptorSet.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ public DescriptorSet(LogicalDevice<?> device, DescriptorPool pool, DescriptorSet
2626
@Override
2727
public Runnable createNativeDestroyer() {
2828
return () -> {
29-
if (pool.isFreeSetsEnabled()) try (MemoryStack stack = MemoryStack.stackPush()) {
30-
vkFreeDescriptorSets(device.getNativeObject(), pool.getNativeObject(), stack.longs(object));
29+
if (pool.getFlags().contains(DescriptorPool.Create.FreeDescriptorSets)) {
30+
try (MemoryStack stack = MemoryStack.stackPush()) {
31+
vkFreeDescriptorSets(device.getNativeObject(), pool.getNativeObject(), stack.longs(object));
32+
}
3133
}
3234
};
3335
}

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.jme3.vulkan.AbstractNative;
66
import com.jme3.vulkan.commands.CommandPool;
77
import com.jme3.vulkan.commands.Queue;
8+
import com.jme3.vulkan.util.Flag;
89
import org.lwjgl.PointerBuffer;
910
import org.lwjgl.system.MemoryStack;
1011
import org.lwjgl.vulkan.*;
@@ -53,30 +54,25 @@ public VkPhysicalDeviceFeatures getEnabledFeatures() {
5354
}
5455

5556
public CommandPool getShortTermPool(Queue queue) {
56-
return getPool(queue, true, false);
57+
return getPool(queue, CommandPool.Create.Transient);
5758
}
5859

5960
public CommandPool getLongTermPool(Queue queue) {
60-
return getPool(queue, false, true);
61+
return getPool(queue, CommandPool.Create.ResetCommandBuffer);
6162
}
6263

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) {
64+
public CommandPool getPool(Queue queue, Flag<CommandPool.Create> flags) {
6865
if (queue.getDevice() != this) {
6966
throw new IllegalArgumentException("Queue must belong to this device.");
7067
}
7168
Collection<CommandPool> p = pools.computeIfAbsent(Thread.currentThread(),
7269
t -> new ArrayList<>());
7370
for (CommandPool pool : p) {
74-
if (pool.getQueue() == queue && pool.isShortLived() == shortLived
75-
&& pool.isReusable() == reusable && pool.isProtected() == protect) {
71+
if (pool.getQueue() == queue && pool.getFlags().contains(flags)) {
7672
return pool;
7773
}
7874
}
79-
CommandPool pool = new CommandPool(queue, shortLived, reusable, protect);
75+
CommandPool pool = new CommandPool(queue, flags);
8076
p.add(pool);
8177
return pool;
8278
}

0 commit comments

Comments
 (0)