Skip to content

Commit ab0de0e

Browse files
committed
add Flag interface to handle bit flags
1 parent d0340ab commit ab0de0e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+529
-294
lines changed

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@
1717
import com.jme3.vulkan.commands.CommandPool;
1818
import com.jme3.vulkan.descriptors.*;
1919
import com.jme3.vulkan.devices.*;
20-
import com.jme3.vulkan.flags.ImageUsageFlags;
21-
import com.jme3.vulkan.flags.MemoryFlags;
22-
import com.jme3.vulkan.flags.BufferUsageFlags;
2320
import com.jme3.vulkan.images.*;
2421
import com.jme3.vulkan.material.TestMaterial;
22+
import com.jme3.vulkan.material.uniforms.BufferUniform;
23+
import com.jme3.vulkan.memory.MemoryFlag;
24+
import com.jme3.vulkan.memory.MemorySize;
2525
import com.jme3.vulkan.pass.Attachment;
2626
import com.jme3.vulkan.pass.Subpass;
27-
import com.jme3.vulkan.pipelines.GraphicsPipeline;
27+
import com.jme3.vulkan.pipelines.*;
2828
import com.jme3.vulkan.pass.RenderPass;
29-
import com.jme3.vulkan.pipelines.PipelineBindPoint;
30-
import com.jme3.vulkan.pipelines.PipelineLayout;
3129
import com.jme3.vulkan.pipelines.states.ColorBlendAttachment;
3230
import com.jme3.vulkan.pipelines.states.DynamicState;
3331
import com.jme3.vulkan.shader.ShaderModule;
32+
import com.jme3.vulkan.shader.ShaderStage;
3433
import com.jme3.vulkan.surface.Surface;
3534
import com.jme3.vulkan.surface.Swapchain;
3635
import com.jme3.vulkan.surface.SwapchainUpdater;
3736
import com.jme3.vulkan.sync.Fence;
3837
import com.jme3.vulkan.sync.Semaphore;
3938
import com.jme3.vulkan.sync.SyncGroup;
39+
import com.jme3.vulkan.util.Flag;
4040
import org.lwjgl.system.MemoryStack;
4141
import org.lwjgl.vulkan.*;
4242

@@ -158,8 +158,8 @@ public void simpleInitApp() {
158158
// requiring 1 descriptor, and an image sampler at binding 1
159159
// requiring 1 descriptor.
160160
descriptorLayout = new DescriptorSetLayout(device,
161-
new SetLayoutBinding(Descriptor.UniformBuffer, 0, 1, VK_SHADER_STAGE_VERTEX_BIT),
162-
new SetLayoutBinding(Descriptor.CombinedImageSampler, 1, 1, VK_SHADER_STAGE_FRAGMENT_BIT));
161+
new SetLayoutBinding(Descriptor.UniformBuffer, 0, 1, ShaderStage.Vertex),
162+
new SetLayoutBinding(Descriptor.CombinedImageSampler, 1, 1, ShaderStage.Fragment));
163163
descriptorPool = new DescriptorPool(device, 3,
164164
new PoolSize(Descriptor.UniformBuffer, 3),
165165
new PoolSize(Descriptor.StorageBuffer, 4),
@@ -173,9 +173,9 @@ public void simpleInitApp() {
173173
// pipeline
174174
pipelineLayout = new PipelineLayout(device, descriptorLayout);
175175
vertModule = new ShaderModule(device, assetManager.loadAsset(ShadercLoader.key(
176-
"Shaders/VulkanVertTest.glsl", ShaderType.Vertex)), "main");
176+
"Shaders/VulkanVertTest.glsl", ShaderType.Vertex)));
177177
fragModule = new ShaderModule(device, assetManager.loadAsset(ShadercLoader.key(
178-
"Shaders/VulkanFragTest.glsl", ShaderType.Fragment)), "main");
178+
"Shaders/VulkanFragTest.glsl", ShaderType.Fragment)));
179179
renderPass = new RenderPass(device);
180180
try (RenderPass.Builder p = renderPass.build()) {
181181
Attachment color = p.createAttachment(swapchain.getFormat(), VK_SAMPLE_COUNT_1_BIT, a -> {
@@ -199,17 +199,17 @@ public void simpleInitApp() {
199199
s.setDepthStencilAttachment(depth.createReference(Image.Layout.DepthStencilAttachmentOptimal));
200200
});
201201
p.createDependency(null, subpass, d -> {
202-
d.setSrcStageMask(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT);
203-
d.setSrcAccessMask(subpass.getPosition());
204-
d.setDstStageMask(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT);
205-
d.setDstAccessMask(VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT);
202+
d.setSrcStageMask(Flag.of(PipelineStage.ColorAttachmentOutput, PipelineStage.EarlyFragmentTests));
203+
d.setSrcAccessMask(Flag.of(subpass.getPosition()));
204+
d.setDstStageMask(Flag.of(PipelineStage.ColorAttachmentOutput, PipelineStage.EarlyFragmentTests));
205+
d.setDstAccessMask(Flag.of(Access.ColorAttachmentWrite, Access.DepthStencilAttachmentWrite));
206206
});
207207
}
208208
swapchain.createFrameBuffers(renderPass, depthView);
209209
pipeline = new GraphicsPipeline(device, pipelineLayout, renderPass, 0, new TestCaseMeshDescription());
210210
try (GraphicsPipeline.Builder p = pipeline.build()) {
211-
p.addStage(vertModule, VK_SHADER_STAGE_VERTEX_BIT);
212-
p.addStage(fragModule, VK_SHADER_STAGE_FRAGMENT_BIT);
211+
p.addShader(vertModule, ShaderStage.Vertex, "main");
212+
p.addShader(fragModule, ShaderStage.Fragment, "main");
213213
p.getViewportState().addViewport();
214214
p.getViewportState().addScissor();
215215
p.getColorBlend().addAttachment(new ColorBlendAttachment());
@@ -222,12 +222,12 @@ public void simpleInitApp() {
222222
// cpu-accessible memory is not usually fast for the gpu to access, but
223223
// the cpu cannot directly access fast gpu memory. The solution is to
224224
// copy cpu-side data to a mutual staging buffer, then have the gpu copy
225-
// that data to faster memory. Hence, why we use a StageableBuffer here.
225+
// that data to faster memory.
226226
vertexBuffer = new StaticBuffer(device, transferPool, MemorySize.floats(vertexData.capacity()),
227-
new BufferUsageFlags().vertexBuffer(), new MemoryFlags().deviceLocal(), false);
227+
BufferUsage.Vertex, MemoryFlag.DeviceLocal, false);
228228
vertexBuffer.copy(stack, vertexData);
229229
indexBuffer = new StaticBuffer(device, transferPool, MemorySize.ints(indexData.capacity()),
230-
new BufferUsageFlags().indexBuffer(), new MemoryFlags().deviceLocal(), false);
230+
BufferUsage.Index, MemoryFlag.DeviceLocal, false);
231231
indexBuffer.copy(stack, indexData);
232232
}
233233

@@ -283,11 +283,11 @@ public void simpleUpdate(float tpf) {
283283

284284
private ImageView createDepthAttachment(CommandPool pool) {
285285
Image.Format depthFormat = device.getPhysicalDevice().findSupportedFormat(
286-
VK_IMAGE_TILING_OPTIMAL,
286+
Image.Tiling.Optimal,
287287
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT,
288288
Image.Format.Depth32SFloat, Image.Format.Depth32SFloat_Stencil8UInt, Image.Format.Depth24UNorm_Stencil8UInt);
289289
GpuImage image = new GpuImage(device, swapchain.getExtent().x, swapchain.getExtent().y, depthFormat,
290-
Image.Tiling.Optimal, new ImageUsageFlags().depthStencilAttachment(), new MemoryFlags().deviceLocal());
290+
Image.Tiling.Optimal, ImageUsage.DepthStencilAttachment, MemoryFlag.DeviceLocal);
291291
ImageView view = image.createView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1, 0, 1);
292292
CommandBuffer commands = pool.allocateOneTimeCommandBuffer();
293293
commands.begin();
@@ -301,7 +301,7 @@ private class Frame implements Consumer<Float> {
301301

302302
// render manager
303303
private final CommandBuffer graphicsCommands = graphicsPool.allocateCommandBuffer();
304-
private final Semaphore imageAvailable = new Semaphore(device, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
304+
private final Semaphore imageAvailable = new Semaphore(device, PipelineStage.ColorAttachmentOutput);
305305
private final Semaphore renderFinished = new Semaphore(device);
306306
private final Fence inFlight = new Fence(device, true);
307307

@@ -321,8 +321,9 @@ public Frame() {
321321
// new ImageSetWriter(Descriptor.CombinedImageSampler, 1, 0, new ImageDescriptor(texture, Image.Layout.ShaderReadOnlyOptimal)));
322322
material.getMatrices().setValue(new PersistentBuffer(device,
323323
MemorySize.floats(16),
324-
new BufferUsageFlags().uniformBuffer(),
325-
new MemoryFlags().hostVisible().hostCoherent(), false));
324+
BufferUsage.Uniform,
325+
Flag.of(MemoryFlag.HostVisible, MemoryFlag.HostCoherent),
326+
false));
326327
material.getBaseColorMap().setValue(texture);
327328
}
328329

@@ -353,7 +354,8 @@ public void accept(Float tpf) {
353354
.fillFloatBuffer(
354355

355356
// material
356-
material.getMatrices().getValue().mapFloats(stack, 0, 16, 0), true);
357+
material.getMatrices().getValue().mapFloats(stack, 0,
358+
material.getMatrices().getValue().size().getElements(), 0), true);
357359
material.getMatrices().getValue().unmap();
358360
material.bind(graphicsCommands, pipeline);
359361
// vkCmdBindDescriptorSets(graphicsCommands.getBuffer(), pipeline.getBindPoint().getVkEnum(),
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.jme3.vulkan.buffers;
2+
3+
import com.jme3.vulkan.util.Flag;
4+
5+
import static org.lwjgl.vulkan.VK10.*;
6+
7+
public enum BufferUsage implements Flag<BufferUsage> {
8+
9+
Uniform(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT),
10+
Index(VK_BUFFER_USAGE_INDEX_BUFFER_BIT),
11+
Storage(VK_BUFFER_USAGE_STORAGE_BUFFER_BIT),
12+
StorageTexel(VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT),
13+
Indirect(VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT),
14+
TransferDst(VK_BUFFER_USAGE_TRANSFER_DST_BIT),
15+
TransferSrc(VK_BUFFER_USAGE_TRANSFER_SRC_BIT),
16+
UniformTexel(VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT),
17+
Vertex(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
18+
19+
private final int vkEnum;
20+
21+
BufferUsage(int vkEnum) {
22+
this.vkEnum = vkEnum;
23+
}
24+
25+
@Override
26+
public int bits() {
27+
return vkEnum;
28+
}
29+
30+
}

jme3-lwjgl3/src/main/java/com/jme3/vulkan/buffers/GpuBuffer.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import com.jme3.util.natives.NativeReference;
66
import com.jme3.vulkan.commands.CommandBuffer;
77
import com.jme3.vulkan.devices.LogicalDevice;
8-
import com.jme3.vulkan.flags.MemoryFlags;
9-
import com.jme3.vulkan.flags.BufferUsageFlags;
8+
import com.jme3.vulkan.memory.MemoryFlag;
9+
import com.jme3.vulkan.memory.MemoryRegion;
10+
import com.jme3.vulkan.memory.MemorySize;
11+
import com.jme3.vulkan.util.Flag;
1012
import org.lwjgl.PointerBuffer;
1113
import org.lwjgl.system.MemoryStack;
1214
import org.lwjgl.system.MemoryUtil;
@@ -18,7 +20,6 @@
1820

1921
import java.nio.*;
2022
import java.util.function.Function;
21-
import java.util.function.LongFunction;
2223

2324
import static com.jme3.renderer.vulkan.VulkanUtils.*;
2425
import static org.lwjgl.vulkan.VK10.*;
@@ -31,14 +32,14 @@ public class GpuBuffer implements Native<Long> {
3132
private final long id;
3233
protected final MemoryRegion memory;
3334

34-
public GpuBuffer(LogicalDevice<?> device, MemorySize size, BufferUsageFlags usage, MemoryFlags mem, boolean concurrent) {
35+
public GpuBuffer(LogicalDevice<?> device, MemorySize size, Flag<BufferUsage> usage, Flag<MemoryFlag> mem, boolean concurrent) {
3536
this.device = device;
3637
this.size = size;
3738
try (MemoryStack stack = MemoryStack.stackPush()) {
3839
VkBufferCreateInfo create = VkBufferCreateInfo.calloc(stack)
3940
.sType(VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO)
4041
.size(size.getBytes())
41-
.usage(usage.getUsageFlags())
42+
.usage(usage.bits())
4243
.sharingMode(VulkanUtils.sharingMode(concurrent));
4344
LongBuffer idBuf = stack.mallocLong(1);
4445
check(vkCreateBuffer(device.getNativeObject(), create, null, idBuf),
@@ -47,7 +48,7 @@ public GpuBuffer(LogicalDevice<?> device, MemorySize size, BufferUsageFlags usag
4748
VkMemoryRequirements bufferMem = VkMemoryRequirements.malloc(stack);
4849
vkGetBufferMemoryRequirements(device.getNativeObject(), id, bufferMem);
4950
memory = new MemoryRegion(device, bufferMem.size(), device.getPhysicalDevice().findSupportedMemoryType(
50-
stack, bufferMem.memoryTypeBits(), mem.getMemoryFlags()));
51+
stack, bufferMem.memoryTypeBits(), mem));
5152
memory.bind(this, 0);
5253
}
5354
ref = Native.get().register(this);

jme3-lwjgl3/src/main/java/com/jme3/vulkan/buffers/PersistentBuffer.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package com.jme3.vulkan.buffers;
22

33
import com.jme3.vulkan.devices.LogicalDevice;
4-
import com.jme3.vulkan.flags.MemoryFlags;
5-
import com.jme3.vulkan.flags.BufferUsageFlags;
4+
import com.jme3.vulkan.memory.MemoryFlag;
5+
import com.jme3.vulkan.memory.MemorySize;
6+
import com.jme3.vulkan.util.Flag;
67
import org.lwjgl.PointerBuffer;
78
import org.lwjgl.system.MemoryStack;
89

910
public class PersistentBuffer extends GpuBuffer {
1011

1112
private final long address;
1213

13-
public PersistentBuffer(LogicalDevice device, MemorySize size, BufferUsageFlags usage, MemoryFlags mem, boolean concurrent) {
14+
public PersistentBuffer(LogicalDevice device, MemorySize size, Flag<BufferUsage> usage, Flag<MemoryFlag> mem, boolean concurrent) {
1415
super(device, size, usage, mem, concurrent);
1516
try (MemoryStack stack = MemoryStack.stackPush()) {
1617
address = memory.map(stack, 0, size.getBytes(), 0).get(0);

jme3-lwjgl3/src/main/java/com/jme3/vulkan/buffers/StageableBuffer.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import com.jme3.vulkan.commands.CommandBuffer;
44
import com.jme3.vulkan.commands.CommandPool;
55
import com.jme3.vulkan.devices.LogicalDevice;
6-
import com.jme3.vulkan.flags.MemoryFlags;
7-
import com.jme3.vulkan.flags.BufferUsageFlags;
8-
import com.jme3.vulkan.sync.Fence;
9-
import com.jme3.vulkan.sync.Semaphore;
6+
import com.jme3.vulkan.memory.MemoryFlag;
7+
import com.jme3.vulkan.memory.MemorySize;
108
import com.jme3.vulkan.sync.SyncGroup;
9+
import com.jme3.vulkan.util.Flag;
1110
import org.lwjgl.PointerBuffer;
1211
import org.lwjgl.system.MemoryStack;
1312

@@ -16,10 +15,10 @@ public class StageableBuffer extends GpuBuffer {
1615
private final GpuBuffer stage;
1716

1817
public StageableBuffer(LogicalDevice device, MemorySize size,
19-
BufferUsageFlags usage, MemoryFlags mem, boolean concurrent) {
20-
super(device, size, usage.transferDst(), mem, concurrent);
21-
this.stage = new GpuBuffer(device, size, new BufferUsageFlags().transferSrc(),
22-
new MemoryFlags().hostVisible().hostCoherent(), concurrent);
18+
Flag<BufferUsage> usage, Flag<MemoryFlag> mem, boolean concurrent) {
19+
super(device, size, usage.add(BufferUsage.TransferDst), mem, concurrent);
20+
this.stage = new GpuBuffer(device, size, BufferUsage.TransferSrc,
21+
Flag.of(MemoryFlag.HostVisible, MemoryFlag.HostCoherent), concurrent);
2322
}
2423

2524
@Override

jme3-lwjgl3/src/main/java/com/jme3/vulkan/buffers/StaticBuffer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
import com.jme3.vulkan.commands.CommandPool;
44
import com.jme3.vulkan.devices.LogicalDevice;
5-
import com.jme3.vulkan.flags.BufferUsageFlags;
6-
import com.jme3.vulkan.flags.MemoryFlags;
5+
import com.jme3.vulkan.material.uniforms.BufferUniform;
6+
import com.jme3.vulkan.memory.MemoryFlag;
7+
import com.jme3.vulkan.memory.MemorySize;
78
import com.jme3.vulkan.sync.Fence;
89
import com.jme3.vulkan.sync.SyncGroup;
910
import com.jme3.vulkan.sync.TaskQueue;
11+
import com.jme3.vulkan.util.Flag;
1012

1113
import java.util.concurrent.*;
1214

1315
public class StaticBuffer extends StageableBuffer {
1416

1517
private final CommandPool transferPool;
1618

17-
public StaticBuffer(LogicalDevice device, CommandPool transferPool, MemorySize size, BufferUsageFlags usage, MemoryFlags mem, boolean concurrent) {
19+
public StaticBuffer(LogicalDevice device, CommandPool transferPool, MemorySize size, Flag<BufferUsage> usage, Flag<MemoryFlag> mem, boolean concurrent) {
1820
super(device, size, usage, mem, concurrent);
1921
this.transferPool = transferPool;
2022
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,4 @@ public void reset() {
7575
vkResetDescriptorPool(device.getNativeObject(), id, 0);
7676
}
7777

78-
79-
8078
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public DescriptorSet(LogicalDevice<?> device, DescriptorPool pool, DescriptorSet
2727
public Runnable createNativeDestroyer() {
2828
return () -> {
2929
try (MemoryStack stack = MemoryStack.stackPush()) {
30-
vkFreeDescriptorSets(device.getNativeObject(),
31-
pool.getNativeObject(), stack.longs(object));
30+
vkFreeDescriptorSets(device.getNativeObject(), pool.getNativeObject(), stack.longs(object));
3231
}
3332
};
3433
}
@@ -37,7 +36,6 @@ public void write(DescriptorSetWriter... writers) {
3736
try (MemoryStack stack = MemoryStack.stackPush()) {
3837
VkWriteDescriptorSet.Buffer write = VkWriteDescriptorSet.calloc(writers.length, stack);
3938
populateWriteBuffer(stack, write, writers);
40-
write.flip();
4139
vkUpdateDescriptorSets(device.getNativeObject(), write, null);
4240
}
4341
}
@@ -48,8 +46,10 @@ public void populateWriteBuffer(MemoryStack stack, VkWriteDescriptorSet.Buffer b
4846
.sType(VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET)
4947
.dstSet(object));
5048
}
49+
buffer.flip();
5150
}
5251

52+
@Deprecated
5353
public long getId() {
5454
return object;
5555
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package com.jme3.vulkan.descriptors;
22

3+
import com.jme3.vulkan.shader.ShaderStage;
4+
import com.jme3.vulkan.util.Flag;
35
import org.lwjgl.vulkan.VkDescriptorSetLayoutBinding;
46

5-
import static org.lwjgl.vulkan.VK10.*;
6-
77
public class SetLayoutBinding {
88

99
private final Descriptor type;
10-
private final int binding, descriptors, stages;
10+
private final int binding, descriptors;
11+
private final Flag<ShaderStage> stages;
12+
13+
public SetLayoutBinding(Descriptor type, int binding, int descriptors) {
14+
this(type, binding, descriptors, ShaderStage.All);
15+
}
1116

12-
public SetLayoutBinding(Descriptor type, int binding, int descriptors, int stages) {
17+
public SetLayoutBinding(Descriptor type, int binding, int descriptors, Flag<ShaderStage> stages) {
1318
this.type = type;
1419
this.binding = binding;
1520
this.descriptors = descriptors;
@@ -21,7 +26,7 @@ public void fillLayoutBinding(VkDescriptorSetLayoutBinding layoutBinding) {
2126
layoutBinding.descriptorType(type.getVkEnum())
2227
.binding(binding)
2328
.descriptorCount(descriptors)
24-
.stageFlags(stages)
29+
.stageFlags(stages.bits())
2530
.pImmutableSamplers(null);
2631
}
2732

@@ -37,7 +42,7 @@ public int getDescriptors() {
3742
return descriptors;
3843
}
3944

40-
public int getStages() {
45+
public Flag<ShaderStage> getStages() {
4146
return stages;
4247
}
4348

0 commit comments

Comments
 (0)