| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.NativeLibrariesBootstrap;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.renderpearl.api.device.BackendCreationException;
|
| 6 | import com.mojang.renderpearl.api.device.GpuBackend;
|
| 7 | import com.mojang.renderpearl.api.device.GpuDebugOptions;
|
| 8 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 9 | import com.mojang.renderpearl.backend.vulkan.checkpoints.AmdCheckpointExtension;
|
| 10 | import com.mojang.renderpearl.backend.vulkan.checkpoints.CheckpointExtension;
|
| 11 | import com.mojang.renderpearl.backend.vulkan.checkpoints.NoopCheckpointExtension;
|
| 12 | import com.mojang.renderpearl.backend.vulkan.checkpoints.NvidiaCheckpointExtension;
|
| 13 | import com.mojang.renderpearl.backend.vulkan.init.FeatureSet;
|
| 14 | import com.mojang.renderpearl.backend.vulkan.init.VulkanFeature;
|
| 15 | import com.mojang.renderpearl.frontend.FrontendGpuDevice;
|
| 16 | import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
| 17 | import it.unimi.dsi.fastutil.ints.Int2IntMap.Entry;
|
| 18 | import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
| 19 | import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
|
| 20 | import java.nio.IntBuffer;
|
| 21 | import java.util.List;
|
| 22 | import java.util.Objects;
|
| 23 | import java.util.Set;
|
| 24 | import net.fabricmc.api.EnvType;
|
| 25 | import net.fabricmc.api.Environment;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 | import org.lwjgl.PointerBuffer;
|
| 28 | import org.lwjgl.sdl.SDLError;
|
| 29 | import org.lwjgl.sdl.SDLVideo;
|
| 30 | import org.lwjgl.sdl.SDLVulkan;
|
| 31 | import org.lwjgl.system.MemoryStack;
|
| 32 | import org.lwjgl.system.SharedLibrary;
|
| 33 | import org.lwjgl.util.vma.Vma;
|
| 34 | import org.lwjgl.util.vma.VmaAllocatorCreateInfo;
|
| 35 | import org.lwjgl.util.vma.VmaVulkanFunctions;
|
| 36 | import org.lwjgl.vulkan.VK;
|
| 37 | import org.lwjgl.vulkan.VK12;
|
| 38 | import org.lwjgl.vulkan.VkDevice;
|
| 39 | import org.lwjgl.vulkan.VkDeviceCreateInfo;
|
| 40 | import org.lwjgl.vulkan.VkDeviceQueueCreateInfo;
|
| 41 | import org.lwjgl.vulkan.VkPhysicalDevice;
|
| 42 | import org.lwjgl.vulkan.VkPhysicalDeviceFeatures2;
|
| 43 | import org.lwjgl.vulkan.VkPhysicalDeviceProperties;
|
| 44 | import org.lwjgl.vulkan.VkPhysicalDeviceProperties2;
|
| 45 | import org.lwjgl.vulkan.VkDeviceQueueCreateInfo.Buffer;
|
| 46 | import org.slf4j.Logger;
|
| 47 |
|
| 48 | @Environment(EnvType.CLIENT)
|
| 49 | public class VulkanBackend implements GpuBackend {
|
| 50 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 51 | private boolean libraryLoaded;
|
| 52 | private @Nullable BackendCreationException libraryLoadFailure;
|
| 53 |
|
| 54 | @Override
|
| 55 | public String getName() {
|
| 56 | return "Vulkan";
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public void loadLibrary() throws BackendCreationException {
|
| 61 | if (!this.libraryLoaded) {
|
| 62 | if (this.libraryLoadFailure != null) {
|
| 63 | throw this.libraryLoadFailure;
|
| 64 | }
|
| 65 |
|
| 66 | if (!NativeLibrariesBootstrap.isVulkanLoaderAvailable()) {
|
| 67 | this.libraryLoadFailure = new BackendCreationException("Vulkan loader library is missing", BackendCreationException.Reason.VULKAN_LOADER_MISSING);
|
| 68 | throw this.libraryLoadFailure;
|
| 69 | }
|
| 70 |
|
| 71 | if (!SDLVulkan.SDL_Vulkan_LoadLibrary(((SharedLibrary)VK.getFunctionProvider()).getPath())) {
|
| 72 | this.libraryLoadFailure = new BackendCreationException(
|
| 73 | "Vulkan is not supported: " + Objects.requireNonNullElse(SDLError.SDL_GetError(), "<no error>"), BackendCreationException.Reason.PLATFORM_ERROR
|
| 74 | );
|
| 75 | throw this.libraryLoadFailure;
|
| 76 | }
|
| 77 |
|
| 78 | if (VK.getFunctionProvider().getFunctionAddress("vkGetInstanceProcAddr") != SDLVulkan.SDL_Vulkan_GetVkGetInstanceProcAddr()) {
|
| 79 | this.libraryLoadFailure = new BackendCreationException("vkGetInstanceProcAddr mismatch", BackendCreationException.Reason.PLATFORM_ERROR);
|
| 80 | SDLVulkan.SDL_Vulkan_UnloadLibrary();
|
| 81 | throw this.libraryLoadFailure;
|
| 82 | }
|
| 83 |
|
| 84 | this.libraryLoaded = true;
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public void unloadLibrary() {
|
| 90 | if (this.libraryLoaded) {
|
| 91 | SDLVulkan.SDL_Vulkan_UnloadLibrary();
|
| 92 | this.libraryLoaded = false;
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | public static @Nullable BackendCreationException checkBackendAvailable() {
|
| 97 | VulkanBackend probe = new VulkanBackend();
|
| 98 |
|
| 99 | try {
|
| 100 | probe.loadLibrary();
|
| 101 | } catch (BackendCreationException e) {
|
| 102 | return e;
|
| 103 | }
|
| 104 |
|
| 105 | try {
|
| 106 | return probe.checkBackendAvailableWithLoadedLibrary();
|
| 107 | } finally {
|
| 108 | probe.unloadLibrary();
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | private @Nullable BackendCreationException checkBackendAvailableWithLoadedLibrary() {
|
| 113 | Set<FeatureSet> requiredFeatureSets = VulkanFeatureSets.requiredFeatureSets();
|
| 114 | Set<FeatureSet> requiredIfExtensionsAvailableFeatureSets = VulkanFeatureSets.requiredIfExtensionsAvailableFeatureSets();
|
| 115 |
|
| 116 | try (
|
| 117 | VulkanInstance instance = new VulkanInstance(0, false, false);
|
| 118 | VulkanPhysicalDevice physicalDevice = findPhysicalDevice(instance, requiredFeatureSets, requiredIfExtensionsAvailableFeatureSets);
|
| 119 | ) {
|
| 120 | return null;
|
| 121 | } catch (BackendCreationException e) {
|
| 122 | return e;
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public long createWindow(final @Nullable String title, final int width, final int height, final long flags) {
|
| 128 | return SDLVideo.SDL_CreateWindow(title, width, height, 268435456L | flags);
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public GpuDevice createDevice(final GpuDebugOptions debugOptions) throws BackendCreationException {
|
| 133 | if (!NativeLibrariesBootstrap.isVulkanLoaderAvailable()) {
|
| 134 | throw new BackendCreationException("Vulkan loader library is missing", BackendCreationException.Reason.VULKAN_LOADER_MISSING);
|
| 135 | }
|
| 136 |
|
| 137 | Set<FeatureSet> requiredFeatureSets = VulkanFeatureSets.requiredFeatureSets();
|
| 138 | Set<FeatureSet> requiredIfExtensionsAvailableFeatureSets = VulkanFeatureSets.requiredIfExtensionsAvailableFeatureSets();
|
| 139 | Set<FeatureSet> optionalFeatureSets = VulkanFeatureSets.optionalFeatureSets();
|
| 140 | VulkanInstance instance = null;
|
| 141 | VulkanPhysicalDevice physicalDevice = null;
|
| 142 | VkDevice device = null;
|
| 143 | long vma = 0L;
|
| 144 | CheckpointExtension checkpointExtension = NoopCheckpointExtension.INSTANCE;
|
| 145 |
|
| 146 | FeatureSet enabledFeatures;
|
| 147 | try {
|
| 148 | boolean renderdocAttached = "1".equals(System.getenv("ENABLE_VULKAN_RENDERDOC_CAPTURE"));
|
| 149 | instance = new VulkanInstance(debugOptions.logLevel(), debugOptions.useLabels() || renderdocAttached, debugOptions.useValidationLayers());
|
| 150 | physicalDevice = findPhysicalDevice(instance, requiredFeatureSets, requiredIfExtensionsAvailableFeatureSets);
|
| 151 | Set<String> deviceExtensions = VulkanUtils.enumerateExtensions(physicalDevice.vkPhysicalDevice());
|
| 152 | Set<FeatureSet> enabledFeatureSets = new ObjectOpenHashSet<>(requiredFeatureSets);
|
| 153 |
|
| 154 | for (FeatureSet featureSet : requiredIfExtensionsAvailableFeatureSets) {
|
| 155 | if (featureSet.isSupported(physicalDevice.vkPhysicalDevice(), deviceExtensions)) {
|
| 156 | enabledFeatureSets.add(featureSet);
|
| 157 | LOGGER.info("Enabling required for device FeatureSet [{}]", featureSet.name());
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | for (FeatureSet featureSet : optionalFeatureSets) {
|
| 162 | if (featureSet.isSupported(physicalDevice.vkPhysicalDevice(), deviceExtensions)) {
|
| 163 | if (featureSet.checkCondition(physicalDevice.vkPhysicalDevice())) {
|
| 164 | enabledFeatureSets.add(featureSet);
|
| 165 | LOGGER.info("Enabling optional FeatureSet [{}]", featureSet.name());
|
| 166 | } else {
|
| 167 | LOGGER.info("Optional FeatureSet [{}] supported but device condition failed and will not be enabled", featureSet.name());
|
| 168 | }
|
| 169 | } else {
|
| 170 | LOGGER.info("Optional FeatureSet [{}] not supported", featureSet.name());
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | if (enabledFeatureSets.contains(VulkanFeatureSets.AMD_BUFFER_MARKER_FEATURESET)) {
|
| 175 | checkpointExtension = new AmdCheckpointExtension();
|
| 176 | } else if (enabledFeatureSets.contains(VulkanFeatureSets.NV_DIAGNOSTIC_CHECKPOINT_FEATURESET)) {
|
| 177 | checkpointExtension = new NvidiaCheckpointExtension();
|
| 178 | }
|
| 179 |
|
| 180 | enabledFeatures = new FeatureSet("Enabled", enabledFeatureSets);
|
| 181 | device = createDevice(enabledFeatures, physicalDevice);
|
| 182 | vma = createVma(device);
|
| 183 | } catch (BackendCreationException e) {
|
| 184 | if (vma != 0L) {
|
| 185 | Vma.vmaDestroyAllocator(vma);
|
| 186 | }
|
| 187 |
|
| 188 | if (device != null) {
|
| 189 | VK12.vkDestroyDevice(device, null);
|
| 190 | }
|
| 191 |
|
| 192 | if (physicalDevice != null) {
|
| 193 | physicalDevice.close();
|
| 194 | }
|
| 195 |
|
| 196 | if (instance != null) {
|
| 197 | instance.close();
|
| 198 | }
|
| 199 |
|
| 200 | throw e;
|
| 201 | }
|
| 202 |
|
| 203 | return new FrontendGpuDevice(new VulkanDevice(instance, physicalDevice, enabledFeatures, device, vma, checkpointExtension));
|
| 204 | }
|
| 205 |
|
| 206 | private static long createVma(final VkDevice vkDevice) throws BackendCreationException {
|
| 207 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 208 | VmaVulkanFunctions vmaVulkanFunctions = VmaVulkanFunctions.calloc(stack).set(vkDevice.getPhysicalDevice().getInstance(), vkDevice);
|
| 209 | VmaAllocatorCreateInfo createInfo = VmaAllocatorCreateInfo.calloc(stack)
|
| 210 | .instance(vkDevice.getPhysicalDevice().getInstance())
|
| 211 | .vulkanApiVersion(VK12.VK_API_VERSION_1_2)
|
| 212 | .device(vkDevice)
|
| 213 | .physicalDevice(vkDevice.getPhysicalDevice())
|
| 214 | .pVulkanFunctions(vmaVulkanFunctions);
|
| 215 | PointerBuffer pointer = stack.callocPointer(1);
|
| 216 | VulkanUtils.throwIfFailure(Vma.vmaCreateAllocator(createInfo, pointer), "Failed to create VMA allocator", BackendCreationException.Reason.OTHER);
|
| 217 | return pointer.get(0);
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | private static VulkanPhysicalDevice findPhysicalDevice(
|
| 222 | final VulkanInstance instance, final Set<FeatureSet> requiredFeatureSets, final Set<FeatureSet> requiredIfExtensionsAvailable
|
| 223 | ) throws BackendCreationException {
|
| 224 | BackendCreationException deviceFailureReason = null;
|
| 225 | VkPhysicalDevice selectedDevice = null;
|
| 226 |
|
| 227 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 228 | IntBuffer intBuffer = stack.callocInt(1);
|
| 229 | VulkanUtils.throwIfFailure(
|
| 230 | VK12.vkEnumeratePhysicalDevices(instance.vkInstance(), intBuffer, null),
|
| 231 | "Failed to get number of physical devices",
|
| 232 | BackendCreationException.Reason.VULKAN_NO_DEVICE
|
| 233 | );
|
| 234 | if (intBuffer.get(0) == 0) {
|
| 235 | throw new BackendCreationException("No Vulkan capable devices", BackendCreationException.Reason.VULKAN_NO_DEVICE);
|
| 236 | }
|
| 237 |
|
| 238 | PointerBuffer pPhysicalDevices = stack.callocPointer(intBuffer.get(0));
|
| 239 | VulkanUtils.throwIfFailure(
|
| 240 | VK12.vkEnumeratePhysicalDevices(instance.vkInstance(), intBuffer, pPhysicalDevices),
|
| 241 | "Failed to get physical devices",
|
| 242 | BackendCreationException.Reason.VULKAN_NO_DEVICE
|
| 243 | );
|
| 244 | int numDevices = intBuffer.get(0);
|
| 245 | if (numDevices == 0) {
|
| 246 | throw new BackendCreationException("No Vulkan capable devices", BackendCreationException.Reason.VULKAN_NO_DEVICE);
|
| 247 | }
|
| 248 |
|
| 249 | for (int i = 0; i < numDevices; i++) {
|
| 250 | if (pPhysicalDevices.get(i) != 0L) {
|
| 251 | VkPhysicalDevice currentDevice = new VkPhysicalDevice(pPhysicalDevices.get(i), instance.vkInstance());
|
| 252 | BackendCreationException failureReason = checkDeviceSuitability(currentDevice, requiredFeatureSets, requiredIfExtensionsAvailable);
|
| 253 | if (failureReason != null) {
|
| 254 | if (deviceFailureReason == null) {
|
| 255 | deviceFailureReason = failureReason;
|
| 256 | }
|
| 257 | } else if (selectedDevice == null) {
|
| 258 | selectedDevice = currentDevice;
|
| 259 | } else if (isDeviceDiscrete(currentDevice) && !isDeviceDiscrete(selectedDevice)) {
|
| 260 | LOGGER.info("Preferring discrete GPU: {}", getDeviceName(currentDevice));
|
| 261 | selectedDevice = currentDevice;
|
| 262 | break;
|
| 263 | }
|
| 264 | }
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | if (selectedDevice == null) {
|
| 269 | if (deviceFailureReason == null) {
|
| 270 | throw new BackendCreationException("No Vulkan capable devices", BackendCreationException.Reason.VULKAN_NO_DEVICE);
|
| 271 | } else {
|
| 272 | throw deviceFailureReason;
|
| 273 | }
|
| 274 | } else {
|
| 275 | return new VulkanPhysicalDevice(selectedDevice);
|
| 276 | }
|
| 277 | }
|
| 278 |
|
| 279 | private static boolean deviceMeetsFeatureQueryRequirements(final VkPhysicalDevice vkPhysicalDevice) {
|
| 280 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 281 | VkPhysicalDeviceProperties properties = VkPhysicalDeviceProperties.calloc(stack);
|
| 282 | VK12.vkGetPhysicalDeviceProperties(vkPhysicalDevice, properties);
|
| 283 | return properties.apiVersion() >= VK12.VK_API_VERSION_1_1;
|
| 284 | }
|
| 285 | }
|
| 286 |
|
| 287 | private static boolean isDeviceDiscrete(final VkPhysicalDevice vkPhysicalDevice) {
|
| 288 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 289 | VkPhysicalDeviceProperties2 deviceProperties = VkPhysicalDeviceProperties2.calloc(stack).sType$Default();
|
| 290 | VK12.vkGetPhysicalDeviceProperties2(vkPhysicalDevice, deviceProperties);
|
| 291 | return deviceProperties.properties().deviceType() == 2;
|
| 292 | }
|
| 293 | }
|
| 294 |
|
| 295 | private static String getDeviceName(final VkPhysicalDevice vkPhysicalDevice) {
|
| 296 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 297 | VkPhysicalDeviceProperties2 deviceProperties = VkPhysicalDeviceProperties2.calloc(stack).sType$Default();
|
| 298 | VK12.vkGetPhysicalDeviceProperties2(vkPhysicalDevice, deviceProperties);
|
| 299 | return deviceProperties.properties().deviceNameString();
|
| 300 | }
|
| 301 | }
|
| 302 |
|
| 303 | private static BackendCreationException checkDeviceSuitability(
|
| 304 | final VkPhysicalDevice vkPhysicalDevice, final Set<FeatureSet> requiredFeatureSets, final Set<FeatureSet> requiredIfExtensionsAvailable
|
| 305 | ) throws BackendCreationException {
|
| 306 | try (VulkanPhysicalDevice physicalDevice = new VulkanPhysicalDevice(vkPhysicalDevice)) {
|
| 307 | String deviceName = physicalDevice.deviceName();
|
| 308 | if (!deviceMeetsFeatureQueryRequirements(vkPhysicalDevice)) {
|
| 309 | LOGGER.warn("Device [{}] does not support Vulkan 1.1, skipping further capability checks", deviceName);
|
| 310 | return new BackendCreationException(
|
| 311 | "Device missing capabilities", BackendCreationException.Reason.VULKAN_DEVICE_VERSION_TOO_LOW, List.of("VULKAN_CORE_1_1")
|
| 312 | );
|
| 313 | }
|
| 314 |
|
| 315 | VulkanUtils.DeviceUUID deviceUUID = new VulkanUtils.DeviceUUID(
|
| 316 | physicalDevice.vkPhysicalDeviceDriverProperties().driverID(),
|
| 317 | physicalDevice.vkPhysicalDeviceProperties().vendorID(),
|
| 318 | physicalDevice.vkPhysicalDeviceProperties().deviceID()
|
| 319 | );
|
| 320 | if (VulkanUtils.KNOWN_PROBLEMATIC_DEVICES.contains(deviceUUID)) {
|
| 321 | LOGGER.warn("Device [{}] is known to be problematic, skipping", deviceName);
|
| 322 | return new BackendCreationException("Device known problematic", BackendCreationException.Reason.VULKAN_KNOWN_PROBLEMATIC, List.of());
|
| 323 | }
|
| 324 |
|
| 325 | Set<FeatureSet> deviceRequiredFeatureSets = new ObjectOpenHashSet<>(requiredFeatureSets);
|
| 326 | Set<String> deviceExtensions = VulkanUtils.enumerateExtensions(vkPhysicalDevice);
|
| 327 |
|
| 328 | for (FeatureSet featureSet : requiredIfExtensionsAvailable) {
|
| 329 | if (deviceExtensions.containsAll(featureSet.extensions())) {
|
| 330 | LOGGER.warn("Device [{}] supports all extensions from FeatureSet [{}], making required", deviceName, featureSet.name());
|
| 331 | deviceRequiredFeatureSets.add(featureSet);
|
| 332 | }
|
| 333 | }
|
| 334 |
|
| 335 | List<String> missingCapabilities = new ReferenceArrayList<>();
|
| 336 | BackendCreationException.Reason mostProminentReason = null;
|
| 337 |
|
| 338 | for (FeatureSet featureSet : deviceRequiredFeatureSets) {
|
| 339 | Set<VulkanFeature> missingFeatures = featureSet.unsupportedFeatures(vkPhysicalDevice);
|
| 340 | if (!missingFeatures.isEmpty()) {
|
| 341 | LOGGER.warn("Device [{}] does not support required features from FeatureSet [{}], missing: {}", deviceName, featureSet.name(), missingFeatures);
|
| 342 | mostProminentReason = BackendCreationException.Reason.VULKAN_MISSING_FEATURE;
|
| 343 |
|
| 344 | for (VulkanFeature missingFeature : missingFeatures) {
|
| 345 | missingCapabilities.add(missingFeature.name());
|
| 346 | }
|
| 347 | }
|
| 348 | }
|
| 349 |
|
| 350 | for (FeatureSet featureSet : deviceRequiredFeatureSets) {
|
| 351 | Set<String> missingExtensions = featureSet.unsupportedExtensions(deviceExtensions);
|
| 352 | if (!missingExtensions.isEmpty()) {
|
| 353 | LOGGER.warn("Device [{}] does not support required extensions from FeatureSet [{}], missing: {}", deviceName, featureSet.name(), missingExtensions);
|
| 354 | mostProminentReason = BackendCreationException.Reason.VULKAN_MISSING_EXTENSION;
|
| 355 | missingCapabilities.addAll(missingExtensions);
|
| 356 | }
|
| 357 | }
|
| 358 |
|
| 359 | if (physicalDevice.graphicsQueueFamilyAndIndex() == null) {
|
| 360 | LOGGER.warn("Device [{}] does not have a graphics queue", deviceName);
|
| 361 | mostProminentReason = BackendCreationException.Reason.VULKAN_NO_GRAPHICS_QUEUE;
|
| 362 | missingCapabilities.add("COMBINED_GRAPHICS_COMPUTE_PRESENT_QUEUE");
|
| 363 | }
|
| 364 |
|
| 365 | if (physicalDevice.vkPhysicalDeviceProperties().apiVersion() < VK12.VK_API_VERSION_1_2) {
|
| 366 | LOGGER.warn("Device [{}] does not support Vulkan 1.2", deviceName);
|
| 367 | mostProminentReason = BackendCreationException.Reason.VULKAN_DEVICE_VERSION_TOO_LOW;
|
| 368 | missingCapabilities.add("VULKAN_CORE_1_2");
|
| 369 | }
|
| 370 |
|
| 371 | if (mostProminentReason != null) {
|
| 372 | LOGGER.debug("Device [{}] is not suitable", deviceName);
|
| 373 | return new BackendCreationException("Device missing capabilities", mostProminentReason, missingCapabilities);
|
| 374 | } else {
|
| 375 | assert missingCapabilities.isEmpty();
|
| 376 | LOGGER.debug("Device [{}] is suitable", deviceName);
|
| 377 | return null;
|
| 378 | }
|
| 379 | }
|
| 380 | }
|
| 381 |
|
| 382 | private static VkDevice createDevice(final FeatureSet featureSet, final VulkanPhysicalDevice physicalDevice) throws BackendCreationException {
|
| 383 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 384 | VkPhysicalDeviceFeatures2 deviceFeatures = VkPhysicalDeviceFeatures2.calloc(stack).sType$Default();
|
| 385 |
|
| 386 | for (VulkanFeature requiredDeviceFeature : featureSet.features()) {
|
| 387 | requiredDeviceFeature.set(deviceFeatures, true, stack);
|
| 388 | }
|
| 389 |
|
| 390 | Int2IntMap queuesToCreate = physicalDevice.queueFamilyCreateInfoMap();
|
| 391 | Buffer queueCreationInfo = VkDeviceQueueCreateInfo.calloc(queuesToCreate.size(), stack);
|
| 392 |
|
| 393 | for (Entry familyCount : queuesToCreate.int2IntEntrySet()) {
|
| 394 | queueCreationInfo.sType$Default();
|
| 395 | queueCreationInfo.queueFamilyIndex(familyCount.getIntKey());
|
| 396 | queueCreationInfo.pQueuePriorities(stack.callocFloat(familyCount.getIntValue()));
|
| 397 | queueCreationInfo.position(queueCreationInfo.position() + 1);
|
| 398 | }
|
| 399 |
|
| 400 | queueCreationInfo.position(0);
|
| 401 | PointerBuffer enabledExtensionsBuffer = stack.callocPointer(featureSet.extensions().size());
|
| 402 |
|
| 403 | for (String name : featureSet.extensions()) {
|
| 404 | enabledExtensionsBuffer.put(stack.UTF8(name));
|
| 405 | }
|
| 406 |
|
| 407 | enabledExtensionsBuffer.flip();
|
| 408 | VkDeviceCreateInfo deviceCreateInfo = VkDeviceCreateInfo.calloc(stack).sType$Default();
|
| 409 | deviceCreateInfo.pNext(deviceFeatures.pNext());
|
| 410 | deviceCreateInfo.pQueueCreateInfos(queueCreationInfo);
|
| 411 | deviceCreateInfo.ppEnabledExtensionNames(enabledExtensionsBuffer);
|
| 412 | deviceCreateInfo.pEnabledFeatures(deviceFeatures.features());
|
| 413 | PointerBuffer pointer = stack.callocPointer(1);
|
| 414 | VulkanUtils.throwIfFailure(
|
| 415 | VK12.vkCreateDevice(physicalDevice.vkPhysicalDevice(), deviceCreateInfo, null, pointer),
|
| 416 | "Failed to create device",
|
| 417 | BackendCreationException.Reason.VULKAN_NO_DEVICE
|
| 418 | );
|
| 419 | return new VkDevice(pointer.get(0), physicalDevice.vkPhysicalDevice(), deviceCreateInfo);
|
| 420 | }
|
| 421 | }
|
| 422 | }
|
| 423 |
|