| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.device.BackendCreationException;
|
| 4 | import com.mojang.renderpearl.api.device.GpuDeviceLossException;
|
| 5 | import com.mojang.renderpearl.backend.vulkan.checkpoints.CheckpointExtension;
|
| 6 | import com.mojang.renderpearl.backend.vulkan.init.VulkanFeature;
|
| 7 | import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
| 8 | import java.nio.IntBuffer;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Set;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import org.joml.Vector4fc;
|
| 14 | import org.lwjgl.system.MemoryStack;
|
| 15 | import org.lwjgl.vulkan.VK12;
|
| 16 | import org.lwjgl.vulkan.VkClearColorValue;
|
| 17 | import org.lwjgl.vulkan.VkExtensionProperties;
|
| 18 | import org.lwjgl.vulkan.VkPhysicalDevice;
|
| 19 | import org.lwjgl.vulkan.VkPhysicalDeviceFeatures2;
|
| 20 | import org.lwjgl.vulkan.VkExtensionProperties.Buffer;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class VulkanUtils {
|
| 24 | public static final Set<VulkanUtils.DeviceUUID> KNOWN_PROBLEMATIC_DEVICES = Set.of(
|
| 25 | new VulkanUtils.DeviceUUID(14, 32902, 338),
|
| 26 | new VulkanUtils.DeviceUUID(14, 32902, 341),
|
| 27 | new VulkanUtils.DeviceUUID(14, 32902, 343),
|
| 28 | new VulkanUtils.DeviceUUID(14, 32902, 346),
|
| 29 | new VulkanUtils.DeviceUUID(14, 32902, 354),
|
| 30 | new VulkanUtils.DeviceUUID(14, 32902, 358),
|
| 31 | new VulkanUtils.DeviceUUID(14, 32902, 362),
|
| 32 | new VulkanUtils.DeviceUUID(14, 32902, 1026),
|
| 33 | new VulkanUtils.DeviceUUID(14, 32902, 1030),
|
| 34 | new VulkanUtils.DeviceUUID(14, 32902, 1034),
|
| 35 | new VulkanUtils.DeviceUUID(14, 32902, 1035),
|
| 36 | new VulkanUtils.DeviceUUID(14, 32902, 1038),
|
| 37 | new VulkanUtils.DeviceUUID(14, 32902, 1042),
|
| 38 | new VulkanUtils.DeviceUUID(14, 32902, 1046),
|
| 39 | new VulkanUtils.DeviceUUID(14, 32902, 1050),
|
| 40 | new VulkanUtils.DeviceUUID(14, 32902, 1051),
|
| 41 | new VulkanUtils.DeviceUUID(14, 32902, 1054),
|
| 42 | new VulkanUtils.DeviceUUID(14, 32902, 1058),
|
| 43 | new VulkanUtils.DeviceUUID(14, 32902, 1062),
|
| 44 | new VulkanUtils.DeviceUUID(14, 32902, 1066),
|
| 45 | new VulkanUtils.DeviceUUID(14, 32902, 1067),
|
| 46 | new VulkanUtils.DeviceUUID(14, 32902, 1070),
|
| 47 | new VulkanUtils.DeviceUUID(14, 32902, 2562),
|
| 48 | new VulkanUtils.DeviceUUID(14, 32902, 2566),
|
| 49 | new VulkanUtils.DeviceUUID(14, 32902, 2570),
|
| 50 | new VulkanUtils.DeviceUUID(14, 32902, 2571),
|
| 51 | new VulkanUtils.DeviceUUID(14, 32902, 2574),
|
| 52 | new VulkanUtils.DeviceUUID(14, 32902, 2578),
|
| 53 | new VulkanUtils.DeviceUUID(14, 32902, 2582),
|
| 54 | new VulkanUtils.DeviceUUID(14, 32902, 2586),
|
| 55 | new VulkanUtils.DeviceUUID(14, 32902, 2587),
|
| 56 | new VulkanUtils.DeviceUUID(14, 32902, 2590),
|
| 57 | new VulkanUtils.DeviceUUID(14, 32902, 2594),
|
| 58 | new VulkanUtils.DeviceUUID(14, 32902, 2598),
|
| 59 | new VulkanUtils.DeviceUUID(14, 32902, 2602),
|
| 60 | new VulkanUtils.DeviceUUID(14, 32902, 2603),
|
| 61 | new VulkanUtils.DeviceUUID(14, 32902, 2606),
|
| 62 | new VulkanUtils.DeviceUUID(14, 32902, 3362),
|
| 63 | new VulkanUtils.DeviceUUID(14, 32902, 3366),
|
| 64 | new VulkanUtils.DeviceUUID(14, 32902, 3370),
|
| 65 | new VulkanUtils.DeviceUUID(14, 32902, 3371),
|
| 66 | new VulkanUtils.DeviceUUID(14, 32902, 3374),
|
| 67 | new VulkanUtils.DeviceUUID(14, 32902, 3888),
|
| 68 | new VulkanUtils.DeviceUUID(14, 32902, 3889),
|
| 69 | new VulkanUtils.DeviceUUID(14, 32902, 3890),
|
| 70 | new VulkanUtils.DeviceUUID(14, 32902, 3891),
|
| 71 | new VulkanUtils.DeviceUUID(14, 32902, 5638),
|
| 72 | new VulkanUtils.DeviceUUID(14, 32902, 5650),
|
| 73 | new VulkanUtils.DeviceUUID(14, 32902, 5654),
|
| 74 | new VulkanUtils.DeviceUUID(14, 32902, 5662),
|
| 75 | new VulkanUtils.DeviceUUID(14, 32902, 5666),
|
| 76 | new VulkanUtils.DeviceUUID(14, 32902, 5670),
|
| 77 | new VulkanUtils.DeviceUUID(14, 32902, 5674),
|
| 78 | new VulkanUtils.DeviceUUID(14, 32902, 5675),
|
| 79 | new VulkanUtils.DeviceUUID(14, 32902, 8880),
|
| 80 | new VulkanUtils.DeviceUUID(14, 32902, 8881),
|
| 81 | new VulkanUtils.DeviceUUID(14, 32902, 8882),
|
| 82 | new VulkanUtils.DeviceUUID(14, 32902, 8883)
|
| 83 | );
|
| 84 |
|
| 85 | public static void throwIfFailure(final int result, final String message, final BackendCreationException.Reason reason) throws BackendCreationException {
|
| 86 | if (result < 0) {
|
| 87 | throw new BackendCreationException(resultToString(result) + ": " + message, reason);
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | public static void crashIfFailure(final VulkanDevice device, final int result, final String message) {
|
| 92 | if (result < 0) {
|
| 93 | String error = resultToString(result) + ": " + message;
|
| 94 | if (result == -4) {
|
| 95 | List<CheckpointExtension.QueueCheckpoints> checkpoints = device.checkpointExtension().retrieveCheckpoints(true);
|
| 96 | throw new GpuDeviceLossException(error + "\n" + formatCheckpoints(checkpoints));
|
| 97 | } else {
|
| 98 | throw new IllegalStateException(error);
|
| 99 | }
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | public static String formatCheckpoints(final List<CheckpointExtension.QueueCheckpoints> queueCheckpoints) {
|
| 104 | StringBuilder result = new StringBuilder();
|
| 105 |
|
| 106 | for (CheckpointExtension.QueueCheckpoints queue : queueCheckpoints) {
|
| 107 | result.append("Queue 0x").append(Long.toHexString(queue.queue())).append('\n');
|
| 108 |
|
| 109 | for (CheckpointExtension.StageCheckpoint checkpoint : queue.checkpoints()) {
|
| 110 | result.append(' ')
|
| 111 | .append(pipelineStageToString(checkpoint.stage()))
|
| 112 | .append(" = ")
|
| 113 | .append(checkpoint.type())
|
| 114 | .append(' ')
|
| 115 | .append(checkpoint.label())
|
| 116 | .append('\n');
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | return result.toString();
|
| 121 | }
|
| 122 |
|
| 123 | public static String pipelineStageToString(final long pipelineStage) {
|
| 124 | if (pipelineStage == 0L) {
|
| 125 | return "NONE";
|
| 126 | } else if (pipelineStage == 1L) {
|
| 127 | return "TOP_OF_PIPE";
|
| 128 | } else if (pipelineStage == 2L) {
|
| 129 | return "DRAW_INDIRECT";
|
| 130 | } else if (pipelineStage == 4L) {
|
| 131 | return "VERTEX_INPUT";
|
| 132 | } else if (pipelineStage == 8L) {
|
| 133 | return "VERTEX_SHADER";
|
| 134 | } else if (pipelineStage == 16L) {
|
| 135 | return "TESSELLATION_CONTROL_SHADER";
|
| 136 | } else if (pipelineStage == 32L) {
|
| 137 | return "TESSELLATION_EVALUATION_SHADER";
|
| 138 | } else if (pipelineStage == 64L) {
|
| 139 | return "GEOMETRY_SHADER";
|
| 140 | } else if (pipelineStage == 128L) {
|
| 141 | return "FRAGMENT_SHADER";
|
| 142 | } else if (pipelineStage == 256L) {
|
| 143 | return "EARLY_FRAGMENT_TESTS";
|
| 144 | } else if (pipelineStage == 512L) {
|
| 145 | return "LATE_FRAGMENT_TESTS";
|
| 146 | } else if (pipelineStage == 1024L) {
|
| 147 | return "COLOR_ATTACHMENT_OUTPUT";
|
| 148 | } else if (pipelineStage == 2048L) {
|
| 149 | return "COMPUTE_SHADER";
|
| 150 | } else if (pipelineStage == 4096L) {
|
| 151 | return "TRANSFER";
|
| 152 | } else if (pipelineStage == 8192L) {
|
| 153 | return "BOTTOM_OF_PIPE";
|
| 154 | } else if (pipelineStage == 16384L) {
|
| 155 | return "HOST";
|
| 156 | } else if (pipelineStage == 32768L) {
|
| 157 | return "ALL_GRAPHICS";
|
| 158 | } else if (pipelineStage == 65536L) {
|
| 159 | return "ALL_COMMANDS";
|
| 160 | } else if (pipelineStage == 131072L) {
|
| 161 | return "COMMAND_PREPROCESS";
|
| 162 | } else if (pipelineStage == 262144L) {
|
| 163 | return "CONDITIONAL_RENDERING";
|
| 164 | } else if (pipelineStage == 524288L) {
|
| 165 | return "TASK_SHADER";
|
| 166 | } else if (pipelineStage == 1048576L) {
|
| 167 | return "MESH_SHADER";
|
| 168 | } else if (pipelineStage == 2097152L) {
|
| 169 | return "RAY_TRACING_SHADER";
|
| 170 | } else if (pipelineStage == 4194304L) {
|
| 171 | return "FRAGMENT_SHADING_RATE_ATTACHMENT";
|
| 172 | } else if (pipelineStage == 8388608L) {
|
| 173 | return "FRAGMENT_DENSITY_PROCESS";
|
| 174 | } else if (pipelineStage == 16777216L) {
|
| 175 | return "TRANSFORM_FEEDBACK";
|
| 176 | } else if (pipelineStage == 33554432L) {
|
| 177 | return "ACCELERATION_STRUCTURE_BUILD";
|
| 178 | } else if (pipelineStage == 4294967296L) {
|
| 179 | return "COPY";
|
| 180 | } else if (pipelineStage == 8589934592L) {
|
| 181 | return "RESOLVE";
|
| 182 | } else if (pipelineStage == 17179869184L) {
|
| 183 | return "BLIT";
|
| 184 | } else if (pipelineStage == 34359738368L) {
|
| 185 | return "CLEAR";
|
| 186 | } else if (pipelineStage == 68719476736L) {
|
| 187 | return "INDEX_INPUT";
|
| 188 | } else if (pipelineStage == 137438953472L) {
|
| 189 | return "VERTEX_ATTRIBUTE_INPUT";
|
| 190 | } else {
|
| 191 | return pipelineStage == 274877906944L ? "PRE_RASTERIZATION_SHADERS" : "0x" + Long.toHexString(pipelineStage);
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | public static String resultToString(final int error) {
|
| 196 | return switch (error) {
|
| 197 | case -1000257000 -> "VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS";
|
| 198 | case -1000161000 -> "VK_ERROR_FRAGMENTATION";
|
| 199 | case -1000072003 -> "VK_ERROR_INVALID_EXTERNAL_HANDLE";
|
| 200 | case -1000069000 -> "VK_ERROR_OUT_OF_POOL_MEMORY";
|
| 201 | case -1000001004 -> "VK_ERROR_OUT_OF_DATE_KHR";
|
| 202 | case -1000000001 -> "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
|
| 203 | case -1000000000 -> "VK_ERROR_SURFACE_LOST_KHR";
|
| 204 | case -13 -> "VK_ERROR_UNKNOWN";
|
| 205 | case -12 -> "VK_ERROR_FRAGMENTED_POOL";
|
| 206 | case -11 -> "VK_ERROR_FORMAT_NOT_SUPPORTED";
|
| 207 | case -10 -> "VK_ERROR_TOO_MANY_OBJECTS";
|
| 208 | case -9 -> "VK_ERROR_INCOMPATIBLE_DRIVER";
|
| 209 | case -8 -> "VK_ERROR_FEATURE_NOT_PRESENT";
|
| 210 | case -7 -> "VK_ERROR_EXTENSION_NOT_PRESENT";
|
| 211 | case -6 -> "VK_ERROR_LAYER_NOT_PRESENT";
|
| 212 | case -5 -> "VK_ERROR_MEMORY_MAP_FAILED";
|
| 213 | case -4 -> "VK_ERROR_DEVICE_LOST";
|
| 214 | case -3 -> "VK_ERROR_INITIALIZATION_FAILED";
|
| 215 | case -2 -> "VK_ERROR_OUT_OF_DEVICE_MEMORY";
|
| 216 | case -1 -> "VK_ERROR_OUT_OF_HOST_MEMORY";
|
| 217 | case 0 -> "VK_SUCCESS";
|
| 218 | case 1 -> "VK_NOT_READY";
|
| 219 | case 2 -> "VK_TIMEOUT";
|
| 220 | case 3 -> "VK_EVENT_SET";
|
| 221 | case 4 -> "VK_EVENT_RESET";
|
| 222 | case 5 -> "VK_INCOMPLETE";
|
| 223 | case 1000001003 -> "VK_SUBOPTIMAL_KHR";
|
| 224 | default -> "0x" + Integer.toHexString(error);
|
| 225 | };
|
| 226 | }
|
| 227 |
|
| 228 | public static boolean hasAllBits(final int bitfield, final int bitmask) {
|
| 229 | return (bitfield & bitmask) == bitmask;
|
| 230 | }
|
| 231 |
|
| 232 | public static boolean hasAllBits(final long bitfield, final long bitmask) {
|
| 233 | return (bitfield & bitmask) == bitmask;
|
| 234 | }
|
| 235 |
|
| 236 | public static boolean hasAnyBit(final int bitfield, final int bitmask) {
|
| 237 | return (bitfield & bitmask) != 0;
|
| 238 | }
|
| 239 |
|
| 240 | public static boolean hasAnyBit(final long bitfield, final long bitmask) {
|
| 241 | return (bitfield & bitmask) != 0L;
|
| 242 | }
|
| 243 |
|
| 244 | public static boolean hasNoBit(final int bitfield, final int bitmask) {
|
| 245 | return (bitfield & bitmask) == 0;
|
| 246 | }
|
| 247 |
|
| 248 | public static boolean hasNoBit(final long bitfield, final long bitmask) {
|
| 249 | return (bitfield & bitmask) == 0L;
|
| 250 | }
|
| 251 |
|
| 252 | public static VkClearColorValue putArgb(final VkClearColorValue vkClearColor, final Vector4fc argb) {
|
| 253 | vkClearColor.float32(0, argb.x());
|
| 254 | vkClearColor.float32(1, argb.y());
|
| 255 | vkClearColor.float32(2, argb.z());
|
| 256 | vkClearColor.float32(3, argb.w());
|
| 257 | return vkClearColor;
|
| 258 | }
|
| 259 |
|
| 260 | public static Set<String> enumerateExtensions(final VkPhysicalDevice vkPhysicalDevice) throws BackendCreationException {
|
| 261 | Set<String> extensions = new ObjectOpenHashSet<>();
|
| 262 |
|
| 263 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 264 | IntBuffer extensionCount = stack.callocInt(1);
|
| 265 | throwIfFailure(
|
| 266 | VK12.vkEnumerateDeviceExtensionProperties(vkPhysicalDevice, (String)null, extensionCount, null),
|
| 267 | "Failed to get number of device extension properties",
|
| 268 | BackendCreationException.Reason.VULKAN_NO_DEVICE
|
| 269 | );
|
| 270 |
|
| 271 | try (Buffer vkDeviceExtensions = VkExtensionProperties.calloc(extensionCount.get(0))) {
|
| 272 | throwIfFailure(
|
| 273 | VK12.vkEnumerateDeviceExtensionProperties(vkPhysicalDevice, (String)null, extensionCount, vkDeviceExtensions),
|
| 274 | "Failed to get extension properties",
|
| 275 | BackendCreationException.Reason.VULKAN_NO_DEVICE
|
| 276 | );
|
| 277 | vkDeviceExtensions.limit(extensionCount.get(0));
|
| 278 |
|
| 279 | for (int i = 0; i < vkDeviceExtensions.limit(); i++) {
|
| 280 | vkDeviceExtensions.position(i);
|
| 281 | extensions.add(vkDeviceExtensions.extensionNameString());
|
| 282 | }
|
| 283 | }
|
| 284 | }
|
| 285 |
|
| 286 | return extensions;
|
| 287 | }
|
| 288 |
|
| 289 | public static VkPhysicalDeviceFeatures2 enumerateFeatures(final VkPhysicalDevice vkPhysicalDevice, final Set<VulkanFeature> features, final MemoryStack stack) {
|
| 290 | VkPhysicalDeviceFeatures2 deviceFeatures = VkPhysicalDeviceFeatures2.calloc(stack).sType$Default();
|
| 291 |
|
| 292 | for (VulkanFeature requiredDeviceFeature : features) {
|
| 293 | requiredDeviceFeature.struct().findOrCreateStructInPNextChain(deviceFeatures, stack);
|
| 294 | }
|
| 295 |
|
| 296 | VK12.vkGetPhysicalDeviceFeatures2(vkPhysicalDevice, deviceFeatures);
|
| 297 | return deviceFeatures;
|
| 298 | }
|
| 299 |
|
| 300 | @Environment(EnvType.CLIENT)
|
| 301 | public record DeviceUUID(int driverID, int vendorID, int deviceID) {
|
| 302 | }
|
| 303 | }
|
| 304 |
|