Skip to content

Commit 5f7d1ec

Browse files
committed
migrated instance, device, surface, and swapchain to new builder architecture
1 parent 4e31f0d commit 5f7d1ec

File tree

7 files changed

+290
-0
lines changed

7 files changed

+290
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.jme3.vulkan;
2+
3+
import com.jme3.vulkan.devices.LogicalDevice;
4+
import com.jme3.vulkan.devices.PhysicalDevice;
5+
import org.lwjgl.system.MemoryStack;
6+
import org.lwjgl.vulkan.KHRSurface;
7+
import org.lwjgl.vulkan.VK13;
8+
import org.lwjgl.vulkan.VkDeviceQueueCreateInfo;
9+
import org.lwjgl.vulkan.VkQueueFamilyProperties;
10+
11+
import java.nio.IntBuffer;
12+
13+
public class SimplePhysicalDevice extends PhysicalDevice {
14+
15+
private final Surface surface;
16+
private Integer graphicsIndex, presentIndex;
17+
private Queue graphics, present;
18+
19+
public SimplePhysicalDevice(VulkanInstance instance, Surface surface, long id) {
20+
super(instance, id);
21+
this.surface = surface;
22+
}
23+
24+
@Override
25+
protected boolean populateQueueFamilyIndices() {
26+
try (MemoryStack stack = MemoryStack.stackPush()) {
27+
VkQueueFamilyProperties.Buffer properties = getQueueFamilyProperties(stack);
28+
IntBuffer ibuf = stack.callocInt(1);
29+
for (int i = 0; i < properties.limit(); i++) {
30+
VkQueueFamilyProperties props = properties.get(i);
31+
if (graphicsIndex == null && (props.queueFlags() & VK13.VK_QUEUE_GRAPHICS_BIT) > 0) {
32+
graphicsIndex = i;
33+
} else if (presentIndex == null) {
34+
KHRSurface.vkGetPhysicalDeviceSurfaceSupportKHR(
35+
getPhysicalDevice(), i, surface.getNativeObject(), ibuf);
36+
if (ibuf.get(0) == VK13.VK_TRUE) {
37+
presentIndex = i;
38+
}
39+
}
40+
if (graphicsIndex != null && presentIndex != null) {
41+
return true;
42+
}
43+
}
44+
}
45+
return false;
46+
}
47+
48+
@Override
49+
protected VkDeviceQueueCreateInfo.Buffer createQueueFamilyInfo(MemoryStack stack) {
50+
VkDeviceQueueCreateInfo.Buffer create = VkDeviceQueueCreateInfo.calloc(2, stack); // one element for each queue
51+
create.get(0).sType(VK13.VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO)
52+
.queueFamilyIndex(graphicsIndex)
53+
.pQueuePriorities(stack.floats(1f));
54+
create.get(1).sType(VK13.VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO)
55+
.queueFamilyIndex(presentIndex)
56+
.pQueuePriorities(stack.floats(1f));
57+
return create;
58+
}
59+
60+
@Override
61+
protected void createQueues(LogicalDevice device) {
62+
graphics = new Queue(device, graphicsIndex, 0);
63+
present = new Queue(device, presentIndex, 0);
64+
}
65+
66+
public Integer getGraphicsIndex() {
67+
return graphicsIndex;
68+
}
69+
70+
public Integer getPresentIndex() {
71+
return presentIndex;
72+
}
73+
74+
public Queue getGraphics() {
75+
return graphics;
76+
}
77+
78+
public Queue getPresent() {
79+
return present;
80+
}
81+
82+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.jme3.vulkan;
2+
3+
import com.jme3.util.natives.Native;
4+
import com.jme3.util.natives.NativeReference;
5+
import org.lwjgl.system.MemoryStack;
6+
7+
public abstract class VulkanObject <T> implements Native<T> {
8+
9+
protected T object;
10+
protected NativeReference ref;
11+
12+
@Override
13+
public T getNativeObject() {
14+
return object;
15+
}
16+
17+
@Override
18+
public void prematureNativeDestruction() {}
19+
20+
@Override
21+
public NativeReference getNativeReference() {
22+
return ref;
23+
}
24+
25+
public static abstract class Builder <T extends VulkanObject> implements AutoCloseable {
26+
27+
protected final MemoryStack stack = MemoryStack.stackPush();
28+
29+
@Override
30+
public void close() {
31+
build();
32+
stack.pop();
33+
}
34+
35+
protected abstract void build();
36+
37+
}
38+
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.jme3.vulkan.app;
2+
3+
import com.jme3.app.SimpleApplication;
4+
import com.jme3.vulkan.VulkanInstance;
5+
import org.lwjgl.system.MemoryStack;
6+
7+
public class VulkanApplication extends SimpleApplication {
8+
9+
protected VulkanInstance instance;
10+
11+
@Override
12+
public void simpleInitApp() {
13+
try (MemoryStack stack = MemoryStack.stackPush()) {
14+
15+
}
16+
}
17+
18+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.jme3.vulkan.devices;
2+
3+
import java.util.Objects;
4+
import java.util.Set;
5+
6+
public class DeviceExtension {
7+
8+
private final String name;
9+
private final Float success, fail;
10+
11+
public DeviceExtension(String name) {
12+
this(name, 1f, null);
13+
}
14+
15+
public DeviceExtension(String name, Float success) {
16+
this(name, success, null);
17+
}
18+
19+
public DeviceExtension(String name, Float success, Float fail) {
20+
this.name = name;
21+
this.success = success;
22+
this.fail = fail;
23+
}
24+
25+
public Float evaluate(Set<String> extensions) {
26+
return extensions.contains(name) ? success : fail;
27+
}
28+
29+
public String getName() {
30+
return name;
31+
}
32+
33+
public Float getSuccessWeight() {
34+
return success;
35+
}
36+
37+
public Float getFailWeight() {
38+
return fail;
39+
}
40+
41+
public boolean isRejectOnFailure() {
42+
return fail == null;
43+
}
44+
45+
@Override
46+
public boolean equals(Object o) {
47+
if (o == null || getClass() != o.getClass()) return false;
48+
DeviceExtension that = (DeviceExtension) o;
49+
return Objects.equals(name, that.name);
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
return Objects.hashCode(name);
55+
}
56+
57+
public static DeviceExtension critical(String name) {
58+
return new DeviceExtension(name, 1f, null);
59+
}
60+
61+
public static DeviceExtension optional(String name, float successWeight) {
62+
return new DeviceExtension(name, successWeight);
63+
}
64+
65+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.jme3.vulkan.devices;
2+
3+
import org.lwjgl.vulkan.VkPhysicalDeviceFeatures;
4+
5+
public interface DeviceFeature {
6+
7+
void enableFeature(VkPhysicalDeviceFeatures features);
8+
9+
Float evaluateFeatureSupport(VkPhysicalDeviceFeatures features);
10+
11+
static DeviceFeature anisotropy(Float pass, boolean rejectOnFail) {
12+
return new DeviceFeature() {
13+
@Override
14+
public void enableFeature(VkPhysicalDeviceFeatures features) {
15+
features.samplerAnisotropy(true);
16+
}
17+
@Override
18+
public Float evaluateFeatureSupport(VkPhysicalDeviceFeatures features) {
19+
return features.samplerAnisotropy() ? pass : (rejectOnFail ? null : 0f);
20+
}
21+
};
22+
}
23+
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.jme3.vulkan.devices;
2+
3+
import org.lwjgl.vulkan.VkPhysicalDeviceProperties;
4+
5+
public interface DeviceProperty {
6+
7+
Float evaluateDeviceProperties(VkPhysicalDeviceProperties properties);
8+
9+
static DeviceProperty maxAnisotropy(float threshold) {
10+
return p -> p.limits().maxSamplerAnisotropy() >= threshold ? 0f : null;
11+
}
12+
13+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.jme3.vulkan.devices;
2+
3+
public class DeviceWeights {
4+
5+
public static final Float SUCCESS = 1f;
6+
public static final Float FAILURE = -1f;
7+
public static final Float REJECTION = null;
8+
9+
public static Float success(float weight) {
10+
return weight;
11+
}
12+
13+
public static Float failure(float weight) {
14+
return -weight;
15+
}
16+
17+
public static Float successOrReject(boolean success) {
18+
return successOrReject(success, SUCCESS);
19+
}
20+
21+
public static Float successOrReject(boolean success, float weight) {
22+
return success ? Float.valueOf(weight) : REJECTION;
23+
}
24+
25+
public static Float successOrFail(boolean success) {
26+
return successOrFail(success, SUCCESS, FAILURE);
27+
}
28+
29+
public static Float successOrFail(boolean success, float weight) {
30+
return success ? weight : -weight;
31+
}
32+
33+
public static Float successOrFail(boolean success, float successWeight, float failWeight) {
34+
return success ? successWeight : -failWeight;
35+
}
36+
37+
public static boolean isSuccess(Float weight) {
38+
return weight != null && weight >= 0f;
39+
}
40+
41+
public static boolean isFailure(Float weight) {
42+
return weight == null || weight < 0f;
43+
}
44+
45+
public static boolean isRejection(Float weight) {
46+
return weight == null;
47+
}
48+
49+
}

0 commit comments

Comments
 (0)