| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.renderpearl.api.GpuFormat;
|
| 5 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 6 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 7 | import com.mojang.renderpearl.api.commands.GpuQueryPool;
|
| 8 | import com.mojang.renderpearl.api.device.DeviceFeatures;
|
| 9 | import com.mojang.renderpearl.api.device.DeviceInfo;
|
| 10 | import com.mojang.renderpearl.api.device.DeviceLimits;
|
| 11 | import com.mojang.renderpearl.api.device.HintsAndWorkarounds;
|
| 12 | import com.mojang.renderpearl.api.textures.AddressMode;
|
| 13 | import com.mojang.renderpearl.api.textures.FilterMode;
|
| 14 | import com.mojang.renderpearl.api.textures.GpuSampler;
|
| 15 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 16 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 17 | import com.mojang.renderpearl.backend.api.BackendRenderPipeline;
|
| 18 | import com.mojang.renderpearl.backend.api.GpuDeviceBackend;
|
| 19 | import com.mojang.renderpearl.backend.api.GpuSurfaceBackend;
|
| 20 | import com.mojang.renderpearl.backend.vulkan.checkpoints.CheckpointExtension;
|
| 21 | import com.mojang.renderpearl.backend.vulkan.init.FeatureSet;
|
| 22 | import it.unimi.dsi.fastutil.ints.IntIntPair;
|
| 23 | import java.nio.ByteBuffer;
|
| 24 | import java.nio.LongBuffer;
|
| 25 | import java.util.Collections;
|
| 26 | import java.util.HashSet;
|
| 27 | import java.util.List;
|
| 28 | import java.util.OptionalDouble;
|
| 29 | import java.util.Set;
|
| 30 | import java.util.function.BooleanSupplier;
|
| 31 | import java.util.function.Supplier;
|
| 32 | import net.fabricmc.api.EnvType;
|
| 33 | import net.fabricmc.api.Environment;
|
| 34 | import net.minecraft.util.Util;
|
| 35 | import org.jspecify.annotations.Nullable;
|
| 36 | import org.lwjgl.system.MemoryStack;
|
| 37 | import org.lwjgl.util.vma.Vma;
|
| 38 | import org.lwjgl.vulkan.EXTCalibratedTimestamps;
|
| 39 | import org.lwjgl.vulkan.VK12;
|
| 40 | import org.lwjgl.vulkan.VkBufferCopy;
|
| 41 | import org.lwjgl.vulkan.VkCalibratedTimestampInfoEXT;
|
| 42 | import org.lwjgl.vulkan.VkDevice;
|
| 43 | import org.lwjgl.vulkan.VkPhysicalDeviceLimits;
|
| 44 | import org.lwjgl.vulkan.VkPhysicalDeviceVulkan11Properties;
|
| 45 | import org.lwjgl.vulkan.VkBufferCopy.Buffer;
|
| 46 | import org.slf4j.Logger;
|
| 47 |
|
| 48 | @Environment(EnvType.CLIENT)
|
| 49 | public class VulkanDevice implements GpuDeviceBackend {
|
| 50 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 51 | private final VulkanInstance instance;
|
| 52 | private final VkDevice vkDevice;
|
| 53 | private final long vma;
|
| 54 | private final DeviceInfo deviceInfo;
|
| 55 | private final VulkanQueue graphicsQueue;
|
| 56 | private final VulkanQueue computeQueue;
|
| 57 | private final VulkanQueue transferQueue;
|
| 58 | private final boolean isIntegratedIntelMoltenVK;
|
| 59 | private final FeatureSet enabledFeatures;
|
| 60 | private final VulkanCommandEncoder commandEncoder;
|
| 61 | private final CheckpointExtension checkpointExtension;
|
| 62 |
|
| 63 | public VulkanDevice(
|
| 64 | final VulkanInstance instance,
|
| 65 | final VulkanPhysicalDevice physicalDevice,
|
| 66 | final FeatureSet enabledFeatureSet,
|
| 67 | final VkDevice vkDevice,
|
| 68 | final long vma,
|
| 69 | final CheckpointExtension checkpointExtension
|
| 70 | ) {
|
| 71 | this.instance = instance;
|
| 72 | this.vkDevice = vkDevice;
|
| 73 | this.vma = vma;
|
| 74 | this.enabledFeatures = enabledFeatureSet;
|
| 75 | this.checkpointExtension = checkpointExtension;
|
| 76 | Set<String> extensionNames = new HashSet<>();
|
| 77 |
|
| 78 | for (String name : instance.getEnabledExtensions()) {
|
| 79 | extensionNames.add(name + " (I)");
|
| 80 | }
|
| 81 |
|
| 82 | for (String name : enabledFeatureSet.extensions()) {
|
| 83 | extensionNames.add(name + " (D)");
|
| 84 | }
|
| 85 |
|
| 86 | VkPhysicalDeviceLimits limits = physicalDevice.vkPhysicalDeviceProperties().limits();
|
| 87 | VkPhysicalDeviceVulkan11Properties vk11Properties = physicalDevice.vkPhysicalDeviceVulkan11Properties();
|
| 88 | int indirectDrawCount = Integer.compareUnsigned(limits.maxDrawIndirectCount(), Integer.MAX_VALUE) > 0 ? Integer.MAX_VALUE : limits.maxDrawIndirectCount();
|
| 89 | this.deviceInfo = new DeviceInfo(
|
| 90 | physicalDevice.deviceName(),
|
| 91 | physicalDevice.vendorName(),
|
| 92 | physicalDevice.driverInfo(),
|
| 93 | true,
|
| 94 | "Vulkan",
|
| 95 | limits.timestampPeriod(),
|
| 96 | new DeviceLimits(
|
| 97 | (int)limits.maxSamplerAnisotropy(),
|
| 98 | (int)limits.minUniformBufferOffsetAlignment(),
|
| 99 | limits.maxImageDimension2D(),
|
| 100 | vk11Properties.maxMemoryAllocationSize() < 0L ? Long.MAX_VALUE : vk11Properties.maxMemoryAllocationSize(),
|
| 101 | physicalDevice.vkPhysicalDeviceMultiDrawPropertiesEXT().maxMultiDrawCount() < 0
|
| 102 | ? Integer.MAX_VALUE
|
| 103 | : physicalDevice.vkPhysicalDeviceMultiDrawPropertiesEXT().maxMultiDrawCount(),
|
| 104 | limits.maxColorAttachments(),
|
| 105 | indirectDrawCount
|
| 106 | ),
|
| 107 | new DeviceFeatures(
|
| 108 | enabledFeatureSet.contains(VulkanFeatureSets.WIREFRAME_FEATURESET),
|
| 109 | true,
|
| 110 | enabledFeatureSet.contains(VulkanFeatureSets.MULTI_DRAW_FEATURESET),
|
| 111 | false,
|
| 112 | true,
|
| 113 | true,
|
| 114 | true,
|
| 115 | true
|
| 116 | ),
|
| 117 | Collections.unmodifiableSet(extensionNames),
|
| 118 | new HintsAndWorkarounds(false, false, Util.isAppleSiliconMac(physicalDevice.deviceName()), false),
|
| 119 | physicalDevice.deviceType()
|
| 120 | );
|
| 121 | IntIntPair graphicsQueueFamily = physicalDevice.graphicsQueueFamilyAndIndex();
|
| 122 | assert graphicsQueueFamily != null;
|
| 123 | IntIntPair computeQueueFamily = physicalDevice.computeQueueFamilyAndIndex();
|
| 124 | IntIntPair transferQueueFamily = physicalDevice.transferQueueFamilyAndIndex();
|
| 125 | this.graphicsQueue = new VulkanQueue(this, graphicsQueueFamily.leftInt(), graphicsQueueFamily.rightInt());
|
| 126 | if (computeQueueFamily != null) {
|
| 127 | this.computeQueue = new VulkanQueue(this, computeQueueFamily.leftInt(), computeQueueFamily.rightInt());
|
| 128 | } else {
|
| 129 | this.computeQueue = this.graphicsQueue;
|
| 130 | }
|
| 131 |
|
| 132 | if (transferQueueFamily != null) {
|
| 133 | this.transferQueue = new VulkanQueue(this, transferQueueFamily.leftInt(), transferQueueFamily.rightInt());
|
| 134 | } else {
|
| 135 | this.transferQueue = this.computeQueue;
|
| 136 | }
|
| 137 |
|
| 138 | this.isIntegratedIntelMoltenVK = physicalDevice.vkPhysicalDeviceProperties().deviceType() == 1
|
| 139 | && physicalDevice.vkPhysicalDeviceProperties().vendorID() == 32902
|
| 140 | && physicalDevice.vkPhysicalDeviceDriverProperties().driverID() == 14;
|
| 141 | physicalDevice.close();
|
| 142 | this.commandEncoder = new VulkanCommandEncoder(this);
|
| 143 | }
|
| 144 |
|
| 145 | @Override
|
| 146 | public void close() {
|
| 147 | this.checkpointExtension.close();
|
| 148 | this.commandEncoder.destroy();
|
| 149 | Vma.vmaDestroyAllocator(this.vma);
|
| 150 | VK12.vkDestroyDevice(this.vkDevice, null);
|
| 151 | this.instance.close();
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public DeviceInfo getDeviceInfo() {
|
| 156 | return this.deviceInfo;
|
| 157 | }
|
| 158 |
|
| 159 | public VulkanInstance instance() {
|
| 160 | return this.instance;
|
| 161 | }
|
| 162 |
|
| 163 | public VkDevice vkDevice() {
|
| 164 | return this.vkDevice;
|
| 165 | }
|
| 166 |
|
| 167 | public VulkanQueue graphicsQueue() {
|
| 168 | return this.graphicsQueue;
|
| 169 | }
|
| 170 |
|
| 171 | public VulkanQueue computeQueue() {
|
| 172 | return this.computeQueue;
|
| 173 | }
|
| 174 |
|
| 175 | public VulkanQueue transferQueue() {
|
| 176 | return this.transferQueue;
|
| 177 | }
|
| 178 |
|
| 179 | public long vma() {
|
| 180 | return this.vma;
|
| 181 | }
|
| 182 |
|
| 183 | @Override
|
| 184 | public GpuSurfaceBackend createSurface(final long windowHandle, final BooleanSupplier isIconified) {
|
| 185 | return new VulkanGpuSurface(this, windowHandle);
|
| 186 | }
|
| 187 |
|
| 188 | public VulkanCommandEncoder createCommandEncoder() {
|
| 189 | return this.commandEncoder;
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | public GpuSampler createSampler(
|
| 194 | final AddressMode addressModeU,
|
| 195 | final AddressMode addressModeV,
|
| 196 | final FilterMode minFilter,
|
| 197 | final FilterMode magFilter,
|
| 198 | final int maxAnisotropy,
|
| 199 | final OptionalDouble maxLod
|
| 200 | ) {
|
| 201 | return new VulkanGpuSampler(this, addressModeU, addressModeV, minFilter, magFilter, maxAnisotropy, maxLod);
|
| 202 | }
|
| 203 |
|
| 204 | @Override
|
| 205 | public GpuTexture createTexture(
|
| 206 | final @Nullable String label,
|
| 207 | final @GpuTexture.Usage int usage,
|
| 208 | final GpuFormat format,
|
| 209 | final int width,
|
| 210 | final int height,
|
| 211 | final int depthOrLayers,
|
| 212 | final int mipLevels
|
| 213 | ) {
|
| 214 | return new VulkanGpuTexture(this, usage, this.isDebuggingEnabled() && label != null ? label : "", format, width, height, depthOrLayers, mipLevels);
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public GpuTextureView createTextureView(final GpuTexture texture, final int baseMipLevel, final int mipLevels) {
|
| 219 | return new VulkanGpuTextureView(this, (VulkanGpuTexture)texture, baseMipLevel, mipLevels);
|
| 220 | }
|
| 221 |
|
| 222 | public VulkanGpuBuffer createBuffer(final @Nullable Supplier<String> label, final @GpuBuffer.Usage int usage, final long size) {
|
| 223 | return new VulkanGpuBuffer.Direct(this, label, usage, size, this.isIntegratedIntelMoltenVK);
|
| 224 | }
|
| 225 |
|
| 226 | @Override
|
| 227 | public GpuBuffer createBuffer(final @Nullable Supplier<String> label, final @GpuBuffer.Usage int usage, final ByteBuffer data) {
|
| 228 | VulkanGpuBuffer buffer = this.createBuffer(label, usage | 8, data.remaining());
|
| 229 | GpuBufferSlice stagingBuffer = this.commandEncoder.transientMemory().uploadStaging(data, 1L, 16);
|
| 230 |
|
| 231 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 232 | Buffer regions = VkBufferCopy.calloc(1, stack).srcOffset(stagingBuffer.offset()).dstOffset(0L).size(data.remaining());
|
| 233 | VK12.vkCmdCopyBuffer(this.commandEncoder.objectInitCommandBuffer(), ((VulkanGpuBuffer)stagingBuffer.buffer()).vkBuffer(), buffer.vkBuffer(), regions);
|
| 234 | }
|
| 235 |
|
| 236 | return buffer;
|
| 237 | }
|
| 238 |
|
| 239 | @Override
|
| 240 | public List<String> getLastDebugMessages() {
|
| 241 | return List.of();
|
| 242 | }
|
| 243 |
|
| 244 | @Override
|
| 245 | public boolean isDebuggingEnabled() {
|
| 246 | return this.instance.debug().enabled();
|
| 247 | }
|
| 248 |
|
| 249 | @Override
|
| 250 | public BackendRenderPipeline.Pending compilePipeline(final BackendRenderPipeline.CreateInfo pipelineCreateInfo) {
|
| 251 | VulkanRenderPipeline pipeline = VulkanRenderPipeline.compile(this, pipelineCreateInfo);
|
| 252 | return () -> pipeline;
|
| 253 | }
|
| 254 |
|
| 255 | @Override
|
| 256 | public GpuQueryPool createTimestampQueryPool(final int size) {
|
| 257 | return new VulkanQueryPool(this, size);
|
| 258 | }
|
| 259 |
|
| 260 | @Override
|
| 261 | public long getTimestampCalibrationOffset() {
|
| 262 | double timestampPeriod = this.deviceInfo.timestampPeriod();
|
| 263 | if (!this.enabledFeatures.contains(VulkanFeatureSets.CALIBRATED_TIMESTAMP_FEATURESET)) {
|
| 264 | long deviceTime = this.commandEncoder.getTimestampNow();
|
| 265 | long hostTime = System.nanoTime();
|
| 266 | long deviceTimeInNanos = timestampPeriod == 1.0 ? deviceTime : (long)(deviceTime * timestampPeriod);
|
| 267 | return hostTime - deviceTimeInNanos;
|
| 268 | }
|
| 269 |
|
| 270 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 271 | org.lwjgl.vulkan.VkCalibratedTimestampInfoEXT.Buffer infos = VkCalibratedTimestampInfoEXT.calloc(2, stack);
|
| 272 | infos.get(0).sType$Default().timeDomain(0);
|
| 273 | infos.get(1).sType$Default().timeDomain(1);
|
| 274 | LongBuffer timestampValues = stack.callocLong(infos.capacity());
|
| 275 | LongBuffer deviation = stack.callocLong(1);
|
| 276 | EXTCalibratedTimestamps.vkGetCalibratedTimestampsEXT(this.vkDevice, infos, timestampValues, deviation);
|
| 277 | long deviceTime = timestampValues.get(0);
|
| 278 | long hostTime = timestampValues.get(1);
|
| 279 | long deviceInNanos = timestampPeriod == 1.0 ? deviceTime : (long)(deviceTime * timestampPeriod);
|
| 280 | return hostTime - deviceInNanos;
|
| 281 | }
|
| 282 | }
|
| 283 |
|
| 284 | public CheckpointExtension checkpointExtension() {
|
| 285 | return this.checkpointExtension;
|
| 286 | }
|
| 287 | }
|
| 288 |
|