| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.lang.StackWalker.Option;
|
| 5 | import java.nio.ByteBuffer;
|
| 6 | import java.nio.LongBuffer;
|
| 7 | import java.util.Set;
|
| 8 | import java.util.function.Supplier;
|
| 9 | import java.util.stream.Collectors;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import org.lwjgl.system.MemoryStack;
|
| 13 | import org.lwjgl.vulkan.EXTDebugUtils;
|
| 14 | import org.lwjgl.vulkan.VkCommandBuffer;
|
| 15 | import org.lwjgl.vulkan.VkDebugUtilsLabelEXT;
|
| 16 | import org.lwjgl.vulkan.VkDebugUtilsMessengerCallbackDataEXT;
|
| 17 | import org.lwjgl.vulkan.VkDebugUtilsMessengerCreateInfoEXT;
|
| 18 | import org.lwjgl.vulkan.VkDebugUtilsObjectNameInfoEXT;
|
| 19 | import org.lwjgl.vulkan.VkDevice;
|
| 20 | import org.lwjgl.vulkan.VkInstance;
|
| 21 | import org.lwjgl.vulkan.VkInstanceCreateInfo;
|
| 22 | import org.slf4j.Logger;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public interface VulkanDebug {
|
| 26 | static VulkanDebug create(final int verbosity, final boolean wantsDebugLabels, final Set<String> availableExtensions, final Set<String> enabledExtensions) {
|
| 27 | if ((verbosity > 0 || wantsDebugLabels) && availableExtensions.contains("VK_EXT_debug_utils")) {
|
| 28 | enabledExtensions.add("VK_EXT_debug_utils");
|
| 29 | return new VulkanDebug.Enabled(verbosity, wantsDebugLabels);
|
| 30 | } else {
|
| 31 | return new VulkanDebug.Disabled();
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 | void chainCreateInfo(VkInstanceCreateInfo instanceCreateInfo, MemoryStack stack);
|
| 36 |
|
| 37 | void setup(VkInstance vkInstance);
|
| 38 |
|
| 39 | void setObjectName(VkDevice device, int objectType, long objectHandle, String label);
|
| 40 |
|
| 41 | void setObjectName(VkDevice device, int objectType, long objectHandle, Supplier<String> label);
|
| 42 |
|
| 43 | void beginDebugGroup(VkCommandBuffer buffer, Supplier<String> label);
|
| 44 |
|
| 45 | void endDebugGroup(VkCommandBuffer buffer);
|
| 46 |
|
| 47 | void destroy(VkInstance instance);
|
| 48 |
|
| 49 | boolean enabled();
|
| 50 |
|
| 51 | @Environment(EnvType.CLIENT)
|
| 52 | class Disabled implements VulkanDebug {
|
| 53 | @Override
|
| 54 | public void chainCreateInfo(final VkInstanceCreateInfo instanceCreateInfo, final MemoryStack stack) {
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public void setup(final VkInstance vkInstance) {
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public void setObjectName(final VkDevice device, final int objectType, final long objectHandle, final String label) {
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public void setObjectName(final VkDevice device, final int objectType, final long objectHandle, final Supplier<String> label) {
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public void beginDebugGroup(final VkCommandBuffer buffer, final Supplier<String> label) {
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public void endDebugGroup(final VkCommandBuffer buffer) {
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public void destroy(final VkInstance instance) {
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public boolean enabled() {
|
| 83 | return false;
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | @Environment(EnvType.CLIENT)
|
| 88 | class Enabled implements VulkanDebug {
|
| 89 | private static final StackWalker STACK_WALKER = StackWalker.getInstance(Set.of(Option.RETAIN_CLASS_REFERENCE), 3);
|
| 90 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 91 | public static final int MESSAGE_TYPE_BITMASK = 7;
|
| 92 | private static final int[] DEBUG_LEVELS = new int[]{4096, 256, 16, 1};
|
| 93 | private final boolean wantsDebugLabels;
|
| 94 | private long messenger;
|
| 95 | private final int severityBitmask;
|
| 96 |
|
| 97 | public Enabled(final int verbosity, final boolean wantsDebugLabels) {
|
| 98 | this.wantsDebugLabels = wantsDebugLabels;
|
| 99 | int severityBitmask = 0;
|
| 100 | if (verbosity > 0) {
|
| 101 | for (int i = 0; i < Math.min(verbosity, DEBUG_LEVELS.length); i++) {
|
| 102 | severityBitmask |= DEBUG_LEVELS[i];
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | this.severityBitmask = severityBitmask;
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public void chainCreateInfo(final VkInstanceCreateInfo instanceCreateInfo, final MemoryStack stack) {
|
| 111 | if (this.severityBitmask > 0) {
|
| 112 | instanceCreateInfo.pNext(
|
| 113 | VkDebugUtilsMessengerCreateInfoEXT.calloc(stack)
|
| 114 | .sType$Default()
|
| 115 | .messageSeverity(this.severityBitmask)
|
| 116 | .messageType(7)
|
| 117 | .pfnUserCallback(this::onDebugMessage)
|
| 118 | );
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public void setup(final VkInstance vkInstance) {
|
| 124 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 125 | LongBuffer pointer = stack.callocLong(1);
|
| 126 | VkDebugUtilsMessengerCreateInfoEXT createInfo = VkDebugUtilsMessengerCreateInfoEXT.calloc(stack)
|
| 127 | .sType$Default()
|
| 128 | .messageSeverity(this.severityBitmask)
|
| 129 | .messageType(7)
|
| 130 | .pfnUserCallback(this::onDebugMessage);
|
| 131 | int result = EXTDebugUtils.vkCreateDebugUtilsMessengerEXT(vkInstance, createInfo, null, pointer);
|
| 132 | if (result != 0) {
|
| 133 | LOGGER.error("Error creating debug utils messenger: {}", VulkanUtils.resultToString(result));
|
| 134 | return;
|
| 135 | }
|
| 136 |
|
| 137 | this.messenger = pointer.get(0);
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | @Override
|
| 142 | public void setObjectName(final VkDevice device, final int objectType, final long objectHandle, final String label) {
|
| 143 | if (this.wantsDebugLabels) {
|
| 144 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 145 | ByteBuffer name = stack.UTF8(label);
|
| 146 | VkDebugUtilsObjectNameInfoEXT nameInfo = VkDebugUtilsObjectNameInfoEXT.calloc(stack)
|
| 147 | .sType$Default()
|
| 148 | .pObjectName(name)
|
| 149 | .objectType(objectType)
|
| 150 | .objectHandle(objectHandle);
|
| 151 | EXTDebugUtils.vkSetDebugUtilsObjectNameEXT(device, nameInfo);
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | public void setObjectName(final VkDevice device, final int objectType, final long objectHandle, final Supplier<String> label) {
|
| 158 | if (this.wantsDebugLabels) {
|
| 159 | this.setObjectName(device, objectType, objectHandle, label.get());
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | private int onDebugMessage(final int messageSeverity, final int messageTypes, final long pCallbackData, final long pUserData) {
|
| 164 | VkDebugUtilsMessengerCallbackDataEXT callbackData = VkDebugUtilsMessengerCallbackDataEXT.create(pCallbackData);
|
| 165 | String message = callbackData.pMessageString();
|
| 166 | if ((messageSeverity & 16) != 0) {
|
| 167 | LOGGER.info("{}", message);
|
| 168 | } else if ((messageSeverity & 256) != 0) {
|
| 169 | LOGGER.warn("{}", message);
|
| 170 | } else {
|
| 171 | if ((messageSeverity & 4096) != 0) {
|
| 172 | if (message == null || !message.contains("vkDestroyInstance") && !message.contains("vkDestroyDevice")) {
|
| 173 | String callStack = STACK_WALKER.walk(
|
| 174 | s -> s.filter(frame -> frame.getDeclaringClass() != VulkanDebug.Enabled.class && !frame.getDeclaringClass().getPackageName().startsWith("org.lwjgl"))
|
| 175 | .limit(5L)
|
| 176 | .map(frame -> "\t" + frame)
|
| 177 | .collect(Collectors.joining("\n"))
|
| 178 | );
|
| 179 | LOGGER.error("{}\n{}", message, callStack);
|
| 180 | } else {
|
| 181 | LOGGER.error("{}", message);
|
| 182 | }
|
| 183 |
|
| 184 | return 1;
|
| 185 | }
|
| 186 |
|
| 187 | LOGGER.debug("{}", message);
|
| 188 | }
|
| 189 |
|
| 190 | return 0;
|
| 191 | }
|
| 192 |
|
| 193 | @Override
|
| 194 | public void beginDebugGroup(final VkCommandBuffer buffer, final Supplier<String> label) {
|
| 195 | if (this.wantsDebugLabels) {
|
| 196 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 197 | ByteBuffer name = stack.UTF8(label.get());
|
| 198 | VkDebugUtilsLabelEXT nameInfo = VkDebugUtilsLabelEXT.calloc(stack).sType$Default().pLabelName(name);
|
| 199 | EXTDebugUtils.vkCmdBeginDebugUtilsLabelEXT(buffer, nameInfo);
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | @Override
|
| 205 | public void endDebugGroup(final VkCommandBuffer buffer) {
|
| 206 | if (this.wantsDebugLabels) {
|
| 207 | EXTDebugUtils.vkCmdEndDebugUtilsLabelEXT(buffer);
|
| 208 | }
|
| 209 | }
|
| 210 |
|
| 211 | @Override
|
| 212 | public void destroy(final VkInstance instance) {
|
| 213 | if (this.messenger != 0L) {
|
| 214 | EXTDebugUtils.vkDestroyDebugUtilsMessengerEXT(instance, this.messenger, null);
|
| 215 | }
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | public boolean enabled() {
|
| 220 | return true;
|
| 221 | }
|
| 222 | }
|
| 223 | }
|
| 224 |
|