Skip to content

Commit a00411b

Browse files
committed
add stage mask to semaphores
1 parent 521c8a7 commit a00411b

File tree

6 files changed

+49
-14
lines changed

6 files changed

+49
-14
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ private ImageView createDepthAttachment(CommandPool pool) {
297297
CommandBuffer commands = pool.allocateOneTimeCommandBuffer();
298298
commands.begin();
299299
image.transitionLayout(commands, Image.Layout.Undefined, Image.Layout.DepthStencilAttachmentOptimal);
300-
commands.end();
301-
commands.submit(new SyncGroup());
300+
commands.endAndSubmit(new SyncGroup());
302301
commands.getPool().getQueue().waitIdle();
303302
return view;
304303
}
@@ -307,7 +306,7 @@ private class Frame implements Consumer<Float> {
307306

308307
// render manager
309308
private final CommandBuffer graphicsCommands = graphicsPool.allocateCommandBuffer();
310-
private final Semaphore imageAvailable = new Semaphore(device);
309+
private final Semaphore imageAvailable = new Semaphore(device, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
311310
private final Semaphore renderFinished = new Semaphore(device);
312311
private final Fence inFlight = new Fence(device, true);
313312

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void submit(SyncGroup sync) {
6363
if (sync.containsWaits()) {
6464
submit.waitSemaphoreCount(sync.getWaits().length)
6565
.pWaitSemaphores(sync.toWaitBuffer(stack))
66-
.pWaitDstStageMask(stack.ints(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT));
66+
.pWaitDstStageMask(sync.toDstStageBuffer(stack));
6767
}
6868
if (sync.containsSignals()) {
6969
submit.pSignalSemaphores(sync.toSignalBuffer(stack));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public CommandPool(Queue queue, boolean shortLived, boolean reusable, boolean pr
2727
VkCommandPoolCreateInfo create = VkCommandPoolCreateInfo.calloc(stack)
2828
.sType(VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO)
2929
.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))
30+
| (reusable ? VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT : 0)
31+
| (protect ? VK_COMMAND_POOL_CREATE_PROTECTED_BIT : 0))
3232
.queueFamilyIndex(queue.getFamilyIndex());
3333
LongBuffer idBuf = stack.mallocLong(1);
3434
check(vkCreateCommandPool(queue.getDevice().getNativeObject(), create, null, idBuf),

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.jme3.vulkan.sync.Fence;
44
import com.jme3.vulkan.sync.Semaphore;
5+
import com.jme3.vulkan.sync.SyncGroup;
56
import org.lwjgl.system.MemoryStack;
67
import org.lwjgl.vulkan.VkCommandBufferBeginInfo;
78
import org.lwjgl.vulkan.VkSubmitInfo;
@@ -22,7 +23,7 @@ public void begin() {
2223
throw new IllegalStateException("Command buffer already recording.");
2324
}
2425
if (active) {
25-
throw new IllegalStateException("Buffer already freed.");
26+
throw new IllegalStateException("One-time command buffer has already been used.");
2627
}
2728
try (MemoryStack stack = MemoryStack.stackPush()) {
2829
VkCommandBufferBeginInfo begin = VkCommandBufferBeginInfo.calloc(stack)
@@ -34,22 +35,23 @@ public void begin() {
3435
}
3536

3637
@Override
37-
public void submit(Semaphore wait, Semaphore signal, Fence fence) {
38+
public void submit(SyncGroup sync) {
3839
if (recording) {
3940
throw new IllegalStateException("Command buffer is still recording.");
4041
}
4142
try (MemoryStack stack = MemoryStack.stackPush()) {
4243
VkSubmitInfo.Buffer submit = VkSubmitInfo.calloc(1, stack)
4344
.sType(VK_STRUCTURE_TYPE_SUBMIT_INFO)
4445
.pCommandBuffers(stack.pointers(buffer));
45-
if (wait != null) {
46-
submit.waitSemaphoreCount(1).pWaitSemaphores(stack.longs(wait.getNativeObject()))
47-
.pWaitDstStageMask(stack.ints(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT));
46+
if (sync.containsWaits()) {
47+
submit.waitSemaphoreCount(sync.getWaits().length)
48+
.pWaitSemaphores(sync.toWaitBuffer(stack))
49+
.pWaitDstStageMask(sync.toDstStageBuffer(stack));
4850
}
49-
if (signal != null) {
50-
submit.pSignalSemaphores(stack.longs(signal.getNativeObject()));
51+
if (sync.containsSignals()) {
52+
submit.pSignalSemaphores(sync.toSignalBuffer(stack));
5153
}
52-
pool.getQueue().submit(submit, fence);
54+
pool.getQueue().submit(submit, sync.getFence());
5355
pool.getQueue().waitIdle();
5456
vkFreeCommandBuffers(pool.getDevice().getNativeObject(), pool.getNativeObject(), buffer);
5557
}

jme3-lwjgl3/src/main/java/com/jme3/vulkan/sync/Semaphore.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ public class Semaphore implements Native<Long> {
1717
private final LogicalDevice<?> device;
1818
private final NativeReference ref;
1919
private long id;
20+
private int dstStageMask;
2021

2122
public Semaphore(LogicalDevice<?> device) {
23+
this(device, 0);
24+
}
25+
26+
public Semaphore(LogicalDevice<?> device, int dstStageMask) {
2227
this.device = device;
28+
this.dstStageMask = dstStageMask;
2329
try (MemoryStack stack = MemoryStack.stackPush()) {
2430
VkSemaphoreCreateInfo create = VkSemaphoreCreateInfo.calloc(stack)
2531
.sType(VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO);
@@ -50,10 +56,28 @@ public NativeReference getNativeReference() {
5056
return ref;
5157
}
5258

59+
public void setDstStageMask(int dstStageMask) {
60+
this.dstStageMask = dstStageMask;
61+
}
62+
63+
public void addDstStageBit(int stageBit) {
64+
this.dstStageMask |= stageBit;
65+
}
66+
67+
public void removeDstStageBit(int stageBit) {
68+
this.dstStageMask &= ~stageBit;
69+
}
70+
71+
public int getDstStageMask() {
72+
return dstStageMask;
73+
}
74+
75+
@Deprecated
5376
public long getId() {
5477
return id;
5578
}
5679

80+
@Deprecated
5781
public LongBuffer toBuffer(MemoryStack stack) {
5882
return stack.longs(id);
5983
}

jme3-lwjgl3/src/main/java/com/jme3/vulkan/sync/SyncGroup.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.lwjgl.system.MemoryStack;
44
import org.lwjgl.vulkan.VK10;
55

6+
import java.nio.IntBuffer;
67
import java.nio.LongBuffer;
78

89
public class SyncGroup {
@@ -88,6 +89,15 @@ public LongBuffer toWaitBuffer(MemoryStack stack) {
8889
return buf;
8990
}
9091

92+
public IntBuffer toDstStageBuffer(MemoryStack stack) {
93+
IntBuffer buf = stack.mallocInt(waits.length);
94+
for (Semaphore s : signals) {
95+
buf.put(s.getDstStageMask());
96+
}
97+
buf.flip();
98+
return buf;
99+
}
100+
91101
public LongBuffer toSignalBuffer(MemoryStack stack) {
92102
LongBuffer buf = stack.mallocLong(signals.length);
93103
for (Semaphore s : signals) {

0 commit comments

Comments
 (0)