| 1 | package com.mojang.renderpearl.backend.vulkan.init;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.device.BackendCreationException;
|
| 4 | import com.mojang.renderpearl.backend.vulkan.VulkanUtils;
|
| 5 | import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
| 6 | import java.util.Collection;
|
| 7 | import java.util.Collections;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Set;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import org.lwjgl.system.MemoryStack;
|
| 13 | import org.lwjgl.vulkan.VkPhysicalDevice;
|
| 14 | import org.lwjgl.vulkan.VkPhysicalDeviceFeatures2;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public record FeatureSet(String name, Set<String> extensions, Set<VulkanFeature> features, FeatureSet.Condition condition) {
|
| 18 | public FeatureSet(final String name, final Set<String> extensions, final Set<VulkanFeature> features) {
|
| 19 | this(name, extensions, features, FeatureSet.Condition.IDENTITY_CONDITION);
|
| 20 | }
|
| 21 |
|
| 22 | public FeatureSet(final String name, final Collection<FeatureSet> others) {
|
| 23 | FeatureSet.Condition condition = others.stream().map(FeatureSet::condition).reduce(FeatureSet.Condition.IDENTITY_CONDITION, FeatureSet.Condition::reduce);
|
| 24 | Set<String> extensions = new ObjectOpenHashSet<>();
|
| 25 | Set<VulkanFeature> features = new ObjectOpenHashSet<>();
|
| 26 |
|
| 27 | for (FeatureSet other : others) {
|
| 28 | extensions.addAll(other.extensions);
|
| 29 | features.addAll(other.features);
|
| 30 | }
|
| 31 |
|
| 32 | this(name, extensions, features, condition);
|
| 33 | }
|
| 34 |
|
| 35 | public FeatureSet composite(final FeatureSet other) {
|
| 36 | return new FeatureSet(this.name, List.of(this, other));
|
| 37 | }
|
| 38 |
|
| 39 | public boolean contains(final FeatureSet other) {
|
| 40 | return this.extensions.containsAll(other.extensions) && this.features.containsAll(other.features);
|
| 41 | }
|
| 42 |
|
| 43 | public boolean checkCondition(final VkPhysicalDevice device) throws BackendCreationException {
|
| 44 | return this.condition.test(device);
|
| 45 | }
|
| 46 |
|
| 47 | public boolean isSupported(final VkPhysicalDevice vkPhysicalDevice) throws BackendCreationException {
|
| 48 | return this.isSupported(vkPhysicalDevice, VulkanUtils.enumerateExtensions(vkPhysicalDevice));
|
| 49 | }
|
| 50 |
|
| 51 | public boolean isSupported(final VkPhysicalDevice vkPhysicalDevice, final Set<String> deviceSupportedExtensions) throws BackendCreationException {
|
| 52 | return !deviceSupportedExtensions.containsAll(this.extensions) ? false : this.allFeaturesSupported(vkPhysicalDevice);
|
| 53 | }
|
| 54 |
|
| 55 | public boolean allFeaturesSupported(final VkPhysicalDevice vkPhysicalDevice) {
|
| 56 | if (!this.features.isEmpty()) {
|
| 57 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 58 | VkPhysicalDeviceFeatures2 supportedFeatures = VulkanUtils.enumerateFeatures(vkPhysicalDevice, this.features, stack);
|
| 59 |
|
| 60 | for (VulkanFeature feature : this.features) {
|
| 61 | if (!feature.get(supportedFeatures)) {
|
| 62 | return false;
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | return true;
|
| 69 | }
|
| 70 |
|
| 71 | public Set<String> unsupportedExtensions(final Set<String> deviceSupportedExtensions) {
|
| 72 | if (this.extensions.isEmpty()) {
|
| 73 | return Set.of();
|
| 74 | }
|
| 75 |
|
| 76 | Set<String> supportedExtensions = new ObjectOpenHashSet<>(this.extensions);
|
| 77 | supportedExtensions.removeAll(deviceSupportedExtensions);
|
| 78 | return supportedExtensions.isEmpty() ? Set.of() : Collections.unmodifiableSet(supportedExtensions);
|
| 79 | }
|
| 80 |
|
| 81 | public Set<String> supportedExtensions(final Set<String> deviceSupportedExtensions) {
|
| 82 | if (this.extensions.isEmpty()) {
|
| 83 | return Set.of();
|
| 84 | }
|
| 85 |
|
| 86 | Set<String> supportedExtensions = new ObjectOpenHashSet<>();
|
| 87 |
|
| 88 | for (String extension : this.extensions) {
|
| 89 | if (deviceSupportedExtensions.contains(extension)) {
|
| 90 | supportedExtensions.add(extension);
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | return supportedExtensions.isEmpty() ? Set.of() : Collections.unmodifiableSet(supportedExtensions);
|
| 95 | }
|
| 96 |
|
| 97 | public Set<VulkanFeature> unsupportedFeatures(final VkPhysicalDevice vkPhysicalDevice) {
|
| 98 | Set<VulkanFeature> unsupportedFeatures = new ObjectOpenHashSet<>();
|
| 99 |
|
| 100 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 101 | VkPhysicalDeviceFeatures2 featuresChain = VulkanUtils.enumerateFeatures(vkPhysicalDevice, this.features, stack);
|
| 102 |
|
| 103 | for (VulkanFeature feature : this.features) {
|
| 104 | if (!feature.get(featuresChain)) {
|
| 105 | unsupportedFeatures.add(feature);
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | return unsupportedFeatures.isEmpty() ? Set.of() : Collections.unmodifiableSet(unsupportedFeatures);
|
| 111 | }
|
| 112 |
|
| 113 | public Set<VulkanFeature> supportedFeatures(final VkPhysicalDevice vkPhysicalDevice) {
|
| 114 | Set<VulkanFeature> supportedFeatures = new ObjectOpenHashSet<>();
|
| 115 |
|
| 116 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 117 | VkPhysicalDeviceFeatures2 featuresChain = VulkanUtils.enumerateFeatures(vkPhysicalDevice, this.features, stack);
|
| 118 |
|
| 119 | for (VulkanFeature feature : this.features) {
|
| 120 | if (feature.get(featuresChain)) {
|
| 121 | supportedFeatures.add(feature);
|
| 122 | }
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | return supportedFeatures.isEmpty() ? Set.of() : Collections.unmodifiableSet(supportedFeatures);
|
| 127 | }
|
| 128 |
|
| 129 | @Environment(EnvType.CLIENT)
|
| 130 | public interface Condition {
|
| 131 | FeatureSet.Condition IDENTITY_CONDITION = var0 -> true;
|
| 132 |
|
| 133 | static FeatureSet.Condition reduce(final FeatureSet.Condition a, final FeatureSet.Condition b) {
|
| 134 | if (a == b) {
|
| 135 | return a;
|
| 136 | } else if (a == IDENTITY_CONDITION) {
|
| 137 | return b;
|
| 138 | } else {
|
| 139 | return b == IDENTITY_CONDITION ? a : device -> a.test(device) && b.test(device);
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | boolean test(VkPhysicalDevice device) throws BackendCreationException;
|
| 144 | }
|
| 145 | }
|
| 146 |
|