| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.device.BackendCreationException;
|
| 4 | import com.mojang.renderpearl.api.device.DeviceType;
|
| 5 | import com.mojang.renderpearl.util.UncheckedAutoCloseable;
|
| 6 | import it.unimi.dsi.fastutil.ints.Int2IntArrayMap;
|
| 7 | import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
| 8 | import it.unimi.dsi.fastutil.ints.Int2IntMaps;
|
| 9 | import it.unimi.dsi.fastutil.ints.IntIntImmutablePair;
|
| 10 | import it.unimi.dsi.fastutil.ints.IntIntPair;
|
| 11 | import java.nio.IntBuffer;
|
| 12 | import java.util.Collection;
|
| 13 | import java.util.HashSet;
|
| 14 | import java.util.Locale;
|
| 15 | import java.util.Set;
|
| 16 | import net.fabricmc.api.EnvType;
|
| 17 | import net.fabricmc.api.Environment;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 | import org.lwjgl.sdl.SDLVulkan;
|
| 20 | import org.lwjgl.system.MemoryStack;
|
| 21 | import org.lwjgl.vulkan.VK12;
|
| 22 | import org.lwjgl.vulkan.VkExtensionProperties;
|
| 23 | import org.lwjgl.vulkan.VkPhysicalDevice;
|
| 24 | import org.lwjgl.vulkan.VkPhysicalDeviceDriverProperties;
|
| 25 | import org.lwjgl.vulkan.VkPhysicalDeviceFeatures2;
|
| 26 | import org.lwjgl.vulkan.VkPhysicalDeviceMultiDrawPropertiesEXT;
|
| 27 | import org.lwjgl.vulkan.VkPhysicalDeviceProperties;
|
| 28 | import org.lwjgl.vulkan.VkPhysicalDeviceProperties2;
|
| 29 | import org.lwjgl.vulkan.VkPhysicalDeviceVulkan11Properties;
|
| 30 | import org.lwjgl.vulkan.VkQueueFamilyProperties;
|
| 31 | import org.lwjgl.vulkan.VkExtensionProperties.Buffer;
|
| 32 |
|
| 33 | @Environment(EnvType.CLIENT)
|
| 34 | public class VulkanPhysicalDevice implements UncheckedAutoCloseable {
|
| 35 | private final VkPhysicalDevice vkPhysicalDevice;
|
| 36 | private final Buffer vkDeviceExtensions;
|
| 37 | private final VkPhysicalDeviceFeatures2 vkPhysicalDeviceFeatures;
|
| 38 | private final VkPhysicalDeviceProperties2 vkPhysicalDeviceProperties;
|
| 39 | private final VkPhysicalDeviceVulkan11Properties vkPhysicalDeviceVulkan11Properties;
|
| 40 | private final VkPhysicalDeviceDriverProperties vkPhysicalDeviceDriverProperties;
|
| 41 | private final VkPhysicalDeviceMultiDrawPropertiesEXT vkPhysicalDeviceMultiDrawPropertiesEXT;
|
| 42 | private final Int2IntMap queueFamilyCreateInfoMap;
|
| 43 | private final @Nullable IntIntPair graphicsQueueFamilyAndIndex;
|
| 44 | private final @Nullable IntIntPair computeQueueFamilyAndIndex;
|
| 45 | private final @Nullable IntIntPair transferQueueFamilyAndIndex;
|
| 46 |
|
| 47 | public VulkanPhysicalDevice(final VkPhysicalDevice vkPhysicalDevice) throws BackendCreationException {
|
| 48 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 49 | this.vkPhysicalDevice = vkPhysicalDevice;
|
| 50 | IntBuffer intBuffer = stack.callocInt(1);
|
| 51 | VulkanUtils.throwIfFailure(
|
| 52 | VK12.vkEnumerateDeviceExtensionProperties(vkPhysicalDevice, (String)null, intBuffer, null),
|
| 53 | "Failed to get number of device extension properties",
|
| 54 | BackendCreationException.Reason.VULKAN_NO_DEVICE
|
| 55 | );
|
| 56 | this.vkDeviceExtensions = VkExtensionProperties.calloc(intBuffer.get(0));
|
| 57 | VulkanUtils.throwIfFailure(
|
| 58 | VK12.vkEnumerateDeviceExtensionProperties(vkPhysicalDevice, (String)null, intBuffer, this.vkDeviceExtensions),
|
| 59 | "Failed to get extension properties",
|
| 60 | BackendCreationException.Reason.VULKAN_NO_DEVICE
|
| 61 | );
|
| 62 | this.vkPhysicalDeviceProperties = VkPhysicalDeviceProperties2.calloc().sType$Default();
|
| 63 | this.vkPhysicalDeviceVulkan11Properties = VkPhysicalDeviceVulkan11Properties.calloc().sType$Default();
|
| 64 | this.vkPhysicalDeviceDriverProperties = VkPhysicalDeviceDriverProperties.calloc().sType$Default();
|
| 65 | this.vkPhysicalDeviceMultiDrawPropertiesEXT = VkPhysicalDeviceMultiDrawPropertiesEXT.calloc().sType$Default();
|
| 66 | this.vkPhysicalDeviceProperties.pNext(this.vkPhysicalDeviceDriverProperties);
|
| 67 | this.vkPhysicalDeviceProperties.pNext(this.vkPhysicalDeviceVulkan11Properties);
|
| 68 | if (this.hasDeviceExtension("VK_EXT_multi_draw")) {
|
| 69 | this.vkPhysicalDeviceProperties.pNext(this.vkPhysicalDeviceMultiDrawPropertiesEXT);
|
| 70 | }
|
| 71 |
|
| 72 | VK12.vkGetPhysicalDeviceProperties2(vkPhysicalDevice, this.vkPhysicalDeviceProperties);
|
| 73 | VK12.vkGetPhysicalDeviceQueueFamilyProperties(vkPhysicalDevice, intBuffer, null);
|
| 74 | org.lwjgl.vulkan.VkQueueFamilyProperties.Buffer vkQueueFamilyProps = VkQueueFamilyProperties.calloc(intBuffer.get(0), stack);
|
| 75 | VK12.vkGetPhysicalDeviceQueueFamilyProperties(vkPhysicalDevice, intBuffer, vkQueueFamilyProps);
|
| 76 | this.vkPhysicalDeviceFeatures = VkPhysicalDeviceFeatures2.calloc().sType$Default();
|
| 77 | VK12.vkGetPhysicalDeviceFeatures2(vkPhysicalDevice, this.vkPhysicalDeviceFeatures);
|
| 78 | int graphicsQueueFamily = -1;
|
| 79 | int computeQueueFamily = -1;
|
| 80 | int transferQueueFamily = -1;
|
| 81 | int computeQueueFamilyBits = -1;
|
| 82 | int transferQueueFamilyBits = -1;
|
| 83 | int numQueueFamilies = vkQueueFamilyProps.capacity();
|
| 84 |
|
| 85 | for (int i = 0; i < numQueueFamilies; i++) {
|
| 86 | int familyUsedQueues = 0;
|
| 87 | VkQueueFamilyProperties queueFamilyProperties = vkQueueFamilyProps.get(i);
|
| 88 | if (graphicsQueueFamily == -1
|
| 89 | && VulkanUtils.hasAllBits(queueFamilyProperties.queueFlags(), 3)
|
| 90 | && SDLVulkan.SDL_Vulkan_GetPresentationSupport(vkPhysicalDevice.getInstance(), vkPhysicalDevice, i)) {
|
| 91 | graphicsQueueFamily = i;
|
| 92 | familyUsedQueues++;
|
| 93 | }
|
| 94 |
|
| 95 | if (queueFamilyProperties.queueCount() > familyUsedQueues) {
|
| 96 | if (VulkanUtils.hasAllBits(queueFamilyProperties.queueFlags(), 2)
|
| 97 | && (computeQueueFamily == -1 || Integer.bitCount(queueFamilyProperties.queueFlags()) <= Integer.bitCount(computeQueueFamilyBits))) {
|
| 98 | computeQueueFamily = i;
|
| 99 | computeQueueFamilyBits = queueFamilyProperties.queueFlags();
|
| 100 | familyUsedQueues++;
|
| 101 | }
|
| 102 |
|
| 103 | if (queueFamilyProperties.queueCount() > familyUsedQueues
|
| 104 | && VulkanUtils.hasAnyBit(queueFamilyProperties.queueFlags(), 7)
|
| 105 | && (transferQueueFamily == -1 || Integer.bitCount(queueFamilyProperties.queueFlags()) <= Integer.bitCount(transferQueueFamilyBits))) {
|
| 106 | transferQueueFamily = i;
|
| 107 | transferQueueFamilyBits = queueFamilyProperties.queueFlags();
|
| 108 | familyUsedQueues++;
|
| 109 | }
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | Int2IntMap familyMap = new Int2IntArrayMap();
|
| 114 | int graphicsQueueIndex = familyMap.put(graphicsQueueFamily, familyMap.get(graphicsQueueFamily) + 1);
|
| 115 | int computeQueueIndex = familyMap.put(computeQueueFamily, familyMap.get(computeQueueFamily) + 1);
|
| 116 | int transferQueueIndex = familyMap.put(transferQueueFamily, familyMap.get(transferQueueFamily) + 1);
|
| 117 | familyMap.remove(-1);
|
| 118 | this.queueFamilyCreateInfoMap = Int2IntMaps.unmodifiable(familyMap);
|
| 119 | this.graphicsQueueFamilyAndIndex = graphicsQueueFamily == -1 ? null : new IntIntImmutablePair(graphicsQueueFamily, graphicsQueueIndex);
|
| 120 | this.computeQueueFamilyAndIndex = computeQueueFamily == -1 ? null : new IntIntImmutablePair(computeQueueFamily, computeQueueIndex);
|
| 121 | this.transferQueueFamilyAndIndex = transferQueueFamily == -1 ? null : new IntIntImmutablePair(transferQueueFamily, transferQueueIndex);
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public void close() {
|
| 127 | this.vkPhysicalDeviceFeatures.free();
|
| 128 | this.vkDeviceExtensions.free();
|
| 129 | this.vkPhysicalDeviceMultiDrawPropertiesEXT.free();
|
| 130 | this.vkPhysicalDeviceVulkan11Properties.free();
|
| 131 | this.vkPhysicalDeviceDriverProperties.free();
|
| 132 | this.vkPhysicalDeviceProperties.free();
|
| 133 | }
|
| 134 |
|
| 135 | public String deviceName() {
|
| 136 | return this.vkPhysicalDeviceProperties.properties().deviceNameString();
|
| 137 | }
|
| 138 |
|
| 139 | public String vendorName() {
|
| 140 | int vendorId = this.vkPhysicalDeviceProperties.properties().vendorID();
|
| 141 |
|
| 142 | return switch (vendorId) {
|
| 143 | case 4098, 4130 -> "AMD";
|
| 144 | case 4112 -> "IMGTEC";
|
| 145 | case 4203 -> "APPLE";
|
| 146 | case 4318, 4818 -> "NVIDIA";
|
| 147 | case 5045 -> "ARM";
|
| 148 | case 5140 -> "MICROSOFT";
|
| 149 | case 5348 -> "BROADCOM";
|
| 150 | case 5772, 6091, 6505, 20803 -> "QUALCOMM";
|
| 151 | case 32902 -> "INTEL";
|
| 152 | default -> String.format(Locale.ROOT, "0x%x", vendorId);
|
| 153 | };
|
| 154 | }
|
| 155 |
|
| 156 | public VkPhysicalDevice vkPhysicalDevice() {
|
| 157 | return this.vkPhysicalDevice;
|
| 158 | }
|
| 159 |
|
| 160 | public VkPhysicalDeviceProperties vkPhysicalDeviceProperties() {
|
| 161 | return this.vkPhysicalDeviceProperties.properties();
|
| 162 | }
|
| 163 |
|
| 164 | public VkPhysicalDeviceVulkan11Properties vkPhysicalDeviceVulkan11Properties() {
|
| 165 | return this.vkPhysicalDeviceVulkan11Properties;
|
| 166 | }
|
| 167 |
|
| 168 | public VkPhysicalDeviceDriverProperties vkPhysicalDeviceDriverProperties() {
|
| 169 | return this.vkPhysicalDeviceDriverProperties;
|
| 170 | }
|
| 171 |
|
| 172 | public VkPhysicalDeviceMultiDrawPropertiesEXT vkPhysicalDeviceMultiDrawPropertiesEXT() {
|
| 173 | return this.vkPhysicalDeviceMultiDrawPropertiesEXT;
|
| 174 | }
|
| 175 |
|
| 176 | public boolean hasDeviceExtension(final String name) {
|
| 177 | return this.vkDeviceExtensions.stream().anyMatch(e -> e.extensionNameString().equals(name));
|
| 178 | }
|
| 179 |
|
| 180 | public Set<String> getMissingExtensions(final Collection<String> required) {
|
| 181 | Set<String> remaining = new HashSet<>(required);
|
| 182 |
|
| 183 | for (VkExtensionProperties extension : this.vkDeviceExtensions) {
|
| 184 | remaining.remove(extension.extensionNameString());
|
| 185 | }
|
| 186 |
|
| 187 | return remaining;
|
| 188 | }
|
| 189 |
|
| 190 | public Int2IntMap queueFamilyCreateInfoMap() {
|
| 191 | return this.queueFamilyCreateInfoMap;
|
| 192 | }
|
| 193 |
|
| 194 | public @Nullable IntIntPair graphicsQueueFamilyAndIndex() {
|
| 195 | return this.graphicsQueueFamilyAndIndex;
|
| 196 | }
|
| 197 |
|
| 198 | public @Nullable IntIntPair computeQueueFamilyAndIndex() {
|
| 199 | return this.computeQueueFamilyAndIndex;
|
| 200 | }
|
| 201 |
|
| 202 | public @Nullable IntIntPair transferQueueFamilyAndIndex() {
|
| 203 | return this.transferQueueFamilyAndIndex;
|
| 204 | }
|
| 205 |
|
| 206 | private static String getStandardEncodingVersion(final int version) {
|
| 207 | int major = version >>> 22 & 127;
|
| 208 | int minor = version >>> 12 & 1023;
|
| 209 | int patch = version & 4095;
|
| 210 | return String.format(Locale.ROOT, "%d.%d.%d", major, minor, patch);
|
| 211 | }
|
| 212 |
|
| 213 | public String driverInfo() {
|
| 214 | int apiVersion = this.vkPhysicalDeviceProperties.properties().apiVersion();
|
| 215 | String versionString = getStandardEncodingVersion(apiVersion);
|
| 216 | return String.format(
|
| 217 | Locale.ROOT, "%s %s %s", versionString, this.vkPhysicalDeviceDriverProperties.driverNameString(), this.vkPhysicalDeviceDriverProperties.driverInfoString()
|
| 218 | );
|
| 219 | }
|
| 220 |
|
| 221 | public DeviceType deviceType() {
|
| 222 | return switch (this.vkPhysicalDeviceProperties.properties().deviceType()) {
|
| 223 | case 1 -> DeviceType.INTEGRATED;
|
| 224 | case 2 -> DeviceType.DISCRETE;
|
| 225 | case 3 -> DeviceType.VIRTUAL;
|
| 226 | case 4 -> DeviceType.CPU;
|
| 227 | default -> DeviceType.OTHER;
|
| 228 | };
|
| 229 | }
|
| 230 | }
|
| 231 |
|