| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.renderpearl.api.device.BackendCreationException;
|
| 5 | import com.mojang.renderpearl.util.UncheckedAutoCloseable;
|
| 6 | import java.nio.IntBuffer;
|
| 7 | import java.util.ArrayList;
|
| 8 | import java.util.HashSet;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Set;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.minecraft.SharedConstants;
|
| 14 | import net.minecraft.util.Util;
|
| 15 | import net.minecraft.util.Util.OS;
|
| 16 | import org.lwjgl.PointerBuffer;
|
| 17 | import org.lwjgl.sdl.SDLVulkan;
|
| 18 | import org.lwjgl.system.MemoryStack;
|
| 19 | import org.lwjgl.system.MemoryUtil;
|
| 20 | import org.lwjgl.vulkan.VK12;
|
| 21 | import org.lwjgl.vulkan.VkApplicationInfo;
|
| 22 | import org.lwjgl.vulkan.VkExtensionProperties;
|
| 23 | import org.lwjgl.vulkan.VkInstance;
|
| 24 | import org.lwjgl.vulkan.VkInstanceCreateInfo;
|
| 25 | import org.lwjgl.vulkan.VkLayerProperties;
|
| 26 | import org.lwjgl.vulkan.VkExtensionProperties.Buffer;
|
| 27 | import org.slf4j.Logger;
|
| 28 |
|
| 29 | @Environment(EnvType.CLIENT)
|
| 30 | public class VulkanInstance implements UncheckedAutoCloseable {
|
| 31 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 32 | private static final String APPLICATION_NAME = "Minecraft Java Edition";
|
| 33 | private static final int APPLICATION_VERSION = SharedConstants.getCurrentVersion().dataVersion().version();
|
| 34 | private static final String ENGINE_NAME = "MinecraftJE";
|
| 35 | private static final int ENGINE_VERSION = 0;
|
| 36 | private final Set<String> enabledExtensions = new HashSet<>();
|
| 37 | private final VkInstance vkInstance;
|
| 38 | private final VulkanDebug debug;
|
| 39 |
|
| 40 | protected VulkanInstance(final int debugVerbosity, boolean wantsDebugLabels, final boolean validation) throws BackendCreationException {
|
| 41 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 42 | VkApplicationInfo appInfo = VkApplicationInfo.calloc(stack)
|
| 43 | .sType$Default()
|
| 44 | .pApplicationName(stack.UTF8("Minecraft Java Edition"))
|
| 45 | .applicationVersion(APPLICATION_VERSION)
|
| 46 | .pEngineName(stack.UTF8("MinecraftJE"))
|
| 47 | .engineVersion(0)
|
| 48 | .apiVersion(VK12.VK_API_VERSION_1_2);
|
| 49 | List<String> validationLayers = this.getSupportedValidationLayers();
|
| 50 | PointerBuffer requiredLayers = null;
|
| 51 | if (validation) {
|
| 52 | if (validationLayers.contains("VK_LAYER_KHRONOS_validation")) {
|
| 53 | requiredLayers = stack.callocPointer(1);
|
| 54 | requiredLayers.put(0, stack.ASCII("VK_LAYER_KHRONOS_validation"));
|
| 55 | LOGGER.warn("Enabling Vulkan validation layers");
|
| 56 | wantsDebugLabels = true;
|
| 57 | } else {
|
| 58 | LOGGER.warn("Vulkan validation layers requested but not found");
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | Set<String> availableExtensions = this.getSupportedInstanceExtensions();
|
| 63 | PointerBuffer sdlExtensions = SDLVulkan.SDL_Vulkan_GetInstanceExtensions();
|
| 64 | if (sdlExtensions == null) {
|
| 65 | throw new BackendCreationException("Failed to find the SDL platform surface extensions", BackendCreationException.Reason.PLATFORM_ERROR);
|
| 66 | }
|
| 67 |
|
| 68 | while (sdlExtensions.remaining() > 0) {
|
| 69 | String extensionName = MemoryUtil.memUTF8(sdlExtensions.get());
|
| 70 | if (!extensionName.equals("VK_KHR_portability_enumeration")) {
|
| 71 | this.enabledExtensions.add(extensionName);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | this.debug = VulkanDebug.create(debugVerbosity, wantsDebugLabels, availableExtensions, this.enabledExtensions);
|
| 76 | boolean usePortability = availableExtensions.contains("VK_KHR_portability_enumeration") && Util.getPlatform() == OS.OSX;
|
| 77 | if (usePortability) {
|
| 78 | this.enabledExtensions.add("VK_KHR_portability_enumeration");
|
| 79 | }
|
| 80 |
|
| 81 | PointerBuffer enabledExtensionsBuffer = stack.callocPointer(this.enabledExtensions.size());
|
| 82 |
|
| 83 | for (String name : this.enabledExtensions) {
|
| 84 | enabledExtensionsBuffer.put(stack.UTF8(name));
|
| 85 | }
|
| 86 |
|
| 87 | enabledExtensionsBuffer.flip();
|
| 88 | VkInstanceCreateInfo instanceInfo = VkInstanceCreateInfo.calloc(stack)
|
| 89 | .sType$Default()
|
| 90 | .pApplicationInfo(appInfo)
|
| 91 | .ppEnabledLayerNames(requiredLayers)
|
| 92 | .ppEnabledExtensionNames(enabledExtensionsBuffer);
|
| 93 | if (usePortability) {
|
| 94 | instanceInfo.flags(1);
|
| 95 | }
|
| 96 |
|
| 97 | this.debug.chainCreateInfo(instanceInfo, stack);
|
| 98 | PointerBuffer pInstance = stack.callocPointer(1);
|
| 99 | VulkanUtils.throwIfFailure(
|
| 100 | VK12.vkCreateInstance(instanceInfo, null, pInstance), "Error creating instance", BackendCreationException.Reason.VULKAN_INSTANCE_CREATION_FAILED
|
| 101 | );
|
| 102 | this.vkInstance = new VkInstance(pInstance.get(0), instanceInfo);
|
| 103 | this.debug.setup(this.vkInstance);
|
| 104 | }
|
| 105 | }
|
| 106 |
|
| 107 | public VkInstance vkInstance() {
|
| 108 | return this.vkInstance;
|
| 109 | }
|
| 110 |
|
| 111 | private Set<String> getSupportedInstanceExtensions() throws BackendCreationException {
|
| 112 | Set<String> instanceExtensions = new HashSet<>();
|
| 113 |
|
| 114 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 115 | IntBuffer numExtensionsBuf = stack.callocInt(1);
|
| 116 | VulkanUtils.throwIfFailure(
|
| 117 | VK12.vkEnumerateInstanceExtensionProperties((String)null, numExtensionsBuf, null),
|
| 118 | "Error enumerating instance extensions",
|
| 119 | BackendCreationException.Reason.VULKAN_INSTANCE_CREATION_FAILED
|
| 120 | );
|
| 121 | int numExtensions = numExtensionsBuf.get(0);
|
| 122 | Buffer instanceExtensionsProps = VkExtensionProperties.calloc(numExtensions, stack);
|
| 123 | VulkanUtils.throwIfFailure(
|
| 124 | VK12.vkEnumerateInstanceExtensionProperties((String)null, numExtensionsBuf, instanceExtensionsProps),
|
| 125 | "Error enumerating instance extensions",
|
| 126 | BackendCreationException.Reason.VULKAN_INSTANCE_CREATION_FAILED
|
| 127 | );
|
| 128 |
|
| 129 | for (int i = 0; i < numExtensions; i++) {
|
| 130 | VkExtensionProperties props = instanceExtensionsProps.get(i);
|
| 131 | String extensionName = props.extensionNameString();
|
| 132 | instanceExtensions.add(extensionName);
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | return instanceExtensions;
|
| 137 | }
|
| 138 |
|
| 139 | private List<String> getSupportedValidationLayers() throws BackendCreationException {
|
| 140 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 141 | IntBuffer numLayersArr = stack.callocInt(1);
|
| 142 | VulkanUtils.throwIfFailure(
|
| 143 | VK12.vkEnumerateInstanceLayerProperties(numLayersArr, null),
|
| 144 | "Error enumerating validation layers",
|
| 145 | BackendCreationException.Reason.VULKAN_INSTANCE_CREATION_FAILED
|
| 146 | );
|
| 147 | int numLayers = numLayersArr.get(0);
|
| 148 | org.lwjgl.vulkan.VkLayerProperties.Buffer propsBuf = VkLayerProperties.calloc(numLayers, stack);
|
| 149 | VulkanUtils.throwIfFailure(
|
| 150 | VK12.vkEnumerateInstanceLayerProperties(numLayersArr, propsBuf),
|
| 151 | "Error enumerating validation layers",
|
| 152 | BackendCreationException.Reason.VULKAN_INSTANCE_CREATION_FAILED
|
| 153 | );
|
| 154 | List<String> supportedLayers = new ArrayList<>();
|
| 155 |
|
| 156 | for (int i = 0; i < numLayers; i++) {
|
| 157 | VkLayerProperties props = propsBuf.get(i);
|
| 158 | String layerName = props.layerNameString();
|
| 159 | supportedLayers.add(layerName);
|
| 160 | }
|
| 161 |
|
| 162 | return supportedLayers;
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public void close() {
|
| 168 | this.debug.destroy(this.vkInstance);
|
| 169 | VK12.vkDestroyInstance(this.vkInstance, null);
|
| 170 | }
|
| 171 |
|
| 172 | public Set<String> getEnabledExtensions() {
|
| 173 | return this.enabledExtensions;
|
| 174 | }
|
| 175 |
|
| 176 | public VulkanDebug debug() {
|
| 177 | return this.debug;
|
| 178 | }
|
| 179 | }
|
| 180 |
|