| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.backend.common.BaseGpuTextureView;
|
| 4 | import java.nio.LongBuffer;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import org.lwjgl.system.MemoryStack;
|
| 8 | import org.lwjgl.vulkan.VK12;
|
| 9 | import org.lwjgl.vulkan.VkImageSubresourceRange;
|
| 10 | import org.lwjgl.vulkan.VkImageViewCreateInfo;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public class VulkanGpuTextureView extends BaseGpuTextureView implements Destroyable {
|
| 14 | private final VulkanDevice device;
|
| 15 | private final long vkImageView;
|
| 16 | private boolean closed;
|
| 17 |
|
| 18 | protected VulkanGpuTextureView(final VulkanDevice device, final VulkanGpuTexture texture, final int baseMipLevel, final int mipLevels) {
|
| 19 | super(texture, baseMipLevel, mipLevels);
|
| 20 | this.device = device;
|
| 21 |
|
| 22 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 23 | boolean isCubemap = (texture.usage() & 16) != 0;
|
| 24 | VkImageViewCreateInfo imageViewCreateInfo = VkImageViewCreateInfo.calloc(stack).sType$Default();
|
| 25 | imageViewCreateInfo.image(texture.vkImage());
|
| 26 | imageViewCreateInfo.viewType(isCubemap ? 3 : 1);
|
| 27 | imageViewCreateInfo.format(VulkanConst.toVk(texture.getFormat()));
|
| 28 | VkImageSubresourceRange subresourceRange = imageViewCreateInfo.subresourceRange();
|
| 29 | subresourceRange.aspectMask(texture.getFormat().hasColorAspect() ? 1 : 2);
|
| 30 | subresourceRange.baseMipLevel(baseMipLevel);
|
| 31 | subresourceRange.levelCount(mipLevels);
|
| 32 | subresourceRange.baseArrayLayer(0);
|
| 33 | subresourceRange.layerCount(isCubemap ? 6 : 1);
|
| 34 | LongBuffer handlePtr = stack.callocLong(1);
|
| 35 | VulkanUtils.crashIfFailure(device, VK12.vkCreateImageView(device.vkDevice(), imageViewCreateInfo, null, handlePtr), "Failed to create VkImageView");
|
| 36 | this.vkImageView = handlePtr.get(0);
|
| 37 | device.instance().debug().setObjectName(device.vkDevice(), 14, this.vkImageView, texture.getLabel());
|
| 38 | }
|
| 39 |
|
| 40 | texture.addViews();
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public void destroy() {
|
| 45 | VK12.vkDestroyImageView(this.device.vkDevice(), this.vkImageView, null);
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public void close() {
|
| 50 | if (!this.closed) {
|
| 51 | this.closed = true;
|
| 52 | this.device.createCommandEncoder().queueForDestroy(this);
|
| 53 | this.texture().removeViews();
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public boolean isClosed() {
|
| 59 | return this.closed;
|
| 60 | }
|
| 61 |
|
| 62 | public VulkanGpuTexture texture() {
|
| 63 | return (VulkanGpuTexture)super.texture();
|
| 64 | }
|
| 65 |
|
| 66 | public long vkImageView() {
|
| 67 | return this.vkImageView;
|
| 68 | }
|
| 69 | }
|
| 70 |
|