| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.GpuFormat;
|
| 4 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 5 | import com.mojang.renderpearl.backend.common.BaseGpuTexture;
|
| 6 | import java.nio.LongBuffer;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import org.lwjgl.PointerBuffer;
|
| 10 | import org.lwjgl.system.MemoryStack;
|
| 11 | import org.lwjgl.util.vma.Vma;
|
| 12 | import org.lwjgl.util.vma.VmaAllocationCreateInfo;
|
| 13 | import org.lwjgl.vulkan.VK12;
|
| 14 | import org.lwjgl.vulkan.VkImageCreateInfo;
|
| 15 | import org.lwjgl.vulkan.VkImageMemoryBarrier;
|
| 16 | import org.lwjgl.vulkan.VkImageSubresourceRange;
|
| 17 | import org.lwjgl.vulkan.VkImageMemoryBarrier.Buffer;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class VulkanGpuTexture extends BaseGpuTexture implements Destroyable {
|
| 21 | private final VulkanDevice device;
|
| 22 | private final long vkImage;
|
| 23 | private final long vmaAllocation;
|
| 24 | private boolean closed = false;
|
| 25 | private int views = 0;
|
| 26 |
|
| 27 | public VulkanGpuTexture(
|
| 28 | final VulkanDevice device,
|
| 29 | final @GpuTexture.Usage int usage,
|
| 30 | final String label,
|
| 31 | final GpuFormat format,
|
| 32 | final int width,
|
| 33 | final int height,
|
| 34 | final int depthOrLayers,
|
| 35 | final int mipLevels
|
| 36 | ) {
|
| 37 | super(usage, label, format, width, height, depthOrLayers, mipLevels);
|
| 38 | this.device = device;
|
| 39 |
|
| 40 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 41 | VkImageCreateInfo imageCreateInfo = VkImageCreateInfo.calloc(stack).sType$Default();
|
| 42 | imageCreateInfo.imageType(1);
|
| 43 | imageCreateInfo.extent().set(width, height, 1);
|
| 44 | imageCreateInfo.mipLevels(mipLevels);
|
| 45 | imageCreateInfo.arrayLayers(depthOrLayers);
|
| 46 | imageCreateInfo.format(VulkanConst.toVk(format));
|
| 47 | imageCreateInfo.tiling(0);
|
| 48 | imageCreateInfo.initialLayout(0);
|
| 49 | imageCreateInfo.usage(VulkanConst.textureUsageToVk(usage, format));
|
| 50 | imageCreateInfo.sharingMode(0);
|
| 51 | imageCreateInfo.samples(1);
|
| 52 | imageCreateInfo.flags(VulkanUtils.hasAnyBit(usage, 16) ? 16 : 0);
|
| 53 | VmaAllocationCreateInfo allocationCreateInfo = VmaAllocationCreateInfo.calloc(stack);
|
| 54 | allocationCreateInfo.usage(8);
|
| 55 | LongBuffer imageHandlePtr = stack.callocLong(1);
|
| 56 | PointerBuffer allocationHandlePtr = stack.callocPointer(1);
|
| 57 | VulkanUtils.crashIfFailure(
|
| 58 | device, Vma.vmaCreateImage(device.vma(), imageCreateInfo, allocationCreateInfo, imageHandlePtr, allocationHandlePtr, null), "Failed to create image"
|
| 59 | );
|
| 60 | this.vkImage = imageHandlePtr.get(0);
|
| 61 | this.vmaAllocation = allocationHandlePtr.get(0);
|
| 62 | Buffer barrier = VkImageMemoryBarrier.calloc(1, stack).sType$Default();
|
| 63 | barrier.oldLayout(0);
|
| 64 | barrier.newLayout(1);
|
| 65 | barrier.srcAccessMask(0);
|
| 66 | barrier.dstAccessMask(98304);
|
| 67 | barrier.srcQueueFamilyIndex(-1);
|
| 68 | barrier.dstQueueFamilyIndex(-1);
|
| 69 | barrier.image(this.vkImage);
|
| 70 | VkImageSubresourceRange subresourceRange = barrier.subresourceRange();
|
| 71 | subresourceRange.aspectMask(this.getFormat().hasColorAspect() ? 1 : 2);
|
| 72 | subresourceRange.baseMipLevel(0);
|
| 73 | subresourceRange.levelCount(this.getMipLevels());
|
| 74 | subresourceRange.baseArrayLayer(0);
|
| 75 | subresourceRange.layerCount(depthOrLayers);
|
| 76 | VK12.vkCmdPipelineBarrier(device.createCommandEncoder().objectInitCommandBuffer(), 1, 8192, 0, null, null, barrier);
|
| 77 | device.instance().debug().setObjectName(device.vkDevice(), 10, this.vkImage, label);
|
| 78 | }
|
| 79 |
|
| 80 | this.addViews();
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void destroy() {
|
| 85 | Vma.vmaDestroyImage(this.device.vma(), this.vkImage, this.vmaAllocation);
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public void close() {
|
| 90 | if (!this.closed) {
|
| 91 | this.closed = true;
|
| 92 | this.removeViews();
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | public boolean isClosed() {
|
| 98 | return this.closed;
|
| 99 | }
|
| 100 |
|
| 101 | public void addViews() {
|
| 102 | this.views++;
|
| 103 | }
|
| 104 |
|
| 105 | public void removeViews() {
|
| 106 | this.views--;
|
| 107 | if (this.views < 0) {
|
| 108 | throw new IllegalStateException("Too many views removed from texture");
|
| 109 | }
|
| 110 |
|
| 111 | if (this.closed && this.views == 0) {
|
| 112 | this.device.createCommandEncoder().queueForDestroy(this);
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | public long vkImage() {
|
| 117 | return this.vkImage;
|
| 118 | }
|
| 119 | }
|
| 120 |
|