Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_RENDERER_BACKEND_VULKAN_TEXTURE 19 : #define INCLUDED_RENDERER_BACKEND_VULKAN_TEXTURE 20 : 21 : #include "renderer/backend/Format.h" 22 : #include "renderer/backend/ITexture.h" 23 : #include "renderer/backend/vulkan/DeviceObjectUID.h" 24 : #include "renderer/backend/vulkan/VMA.h" 25 : 26 : #include <cstdint> 27 : #include <glad/vulkan.h> 28 : #include <memory> 29 : 30 : namespace Renderer::Backend::Sampler { struct Desc; } 31 : namespace Renderer::Backend::Vulkan { class CDevice; } 32 : 33 : namespace Renderer 34 : { 35 : 36 : namespace Backend 37 : { 38 : 39 : namespace Vulkan 40 : { 41 : 42 : class CTexture final : public ITexture 43 : { 44 : public: 45 : ~CTexture() override; 46 : 47 : IDevice* GetDevice() override; 48 : 49 0 : Type GetType() const override { return m_Type; } 50 0 : uint32_t GetUsage() const override { return m_Usage; } 51 0 : Format GetFormat() const override { return m_Format; } 52 : 53 0 : uint32_t GetWidth() const override { return m_Width; } 54 0 : uint32_t GetHeight() const override { return m_Height; } 55 0 : uint32_t GetMIPLevelCount() const override { return m_MIPLevelCount; } 56 0 : uint32_t GetSampleCount() const { return m_SampleCount; } 57 0 : uint32_t GetLayerCount() const { return m_LayerCount; } 58 : 59 0 : VkImage GetImage() { return m_Image; } 60 0 : VkImageView GetAttachmentImageView() { return m_AttachmentImageView; } 61 0 : VkImageView GetSamplerImageView() { return m_SamplerImageView; } 62 0 : VkSampler GetSampler() { return m_Sampler; } 63 0 : bool IsCompareEnabled() { return m_IsCompareEnabled; } 64 0 : VkFormat GetVkFormat() const { return m_VkFormat; } 65 : 66 0 : VkImageAspectFlags GetAttachmentImageAspectMask() { return m_AttachmentImageAspectMask; } 67 : VkImageAspectFlags GetSamplerImageAspectMask() { return m_SamplerImageAspectMask; } 68 : 69 : /** 70 : * @return mapped data for readback textures else returns nullptr. 71 : */ 72 0 : void* GetMappedData() { return m_AllocationInfo.pMappedData; } 73 : 74 : VkDeviceMemory GetDeviceMemory() { return m_AllocationInfo.deviceMemory; } 75 : 76 0 : bool IsInitialized() const { return m_Initialized; } 77 0 : void SetInitialized() { m_Initialized = true; } 78 : 79 0 : DeviceObjectUID GetUID() const { return m_UID; } 80 : 81 : private: 82 : friend class CDevice; 83 : friend class CSwapChain; 84 : 85 : CTexture(CDevice* device); 86 : 87 : static std::unique_ptr<CTexture> Create( 88 : CDevice* device, const char* name, const Type type, const uint32_t usage, 89 : const Format format, const uint32_t width, const uint32_t height, 90 : const Sampler::Desc& defaultSamplerDesc, 91 : const uint32_t MIPLevelCount, const uint32_t sampleCount); 92 : 93 : static std::unique_ptr<CTexture> WrapBackbufferImage( 94 : CDevice* device, const char* name, const VkImage image, const VkFormat format, 95 : const VkImageUsageFlags usage, const uint32_t width, const uint32_t height); 96 : 97 : static std::unique_ptr<CTexture> CreateReadback( 98 : CDevice* device, const char* name, const Format format, 99 : const uint32_t width, const uint32_t height); 100 : 101 : Type m_Type = Type::TEXTURE_2D; 102 : uint32_t m_Usage = 0; 103 : Format m_Format = Format::UNDEFINED; 104 : VkFormat m_VkFormat = VK_FORMAT_UNDEFINED; 105 : uint32_t m_Width = 0; 106 : uint32_t m_Height = 0; 107 : uint32_t m_MIPLevelCount = 0; 108 : uint32_t m_SampleCount = 0; 109 : uint32_t m_LayerCount = 0; 110 : 111 : CDevice* m_Device = nullptr; 112 : 113 : VkImage m_Image = VK_NULL_HANDLE; 114 : VkImageView m_AttachmentImageView = VK_NULL_HANDLE; 115 : VkImageView m_SamplerImageView = VK_NULL_HANDLE; 116 : VkSampler m_Sampler = VK_NULL_HANDLE; 117 : bool m_IsCompareEnabled = false; 118 : VmaAllocation m_Allocation{}; 119 : VmaAllocationInfo m_AllocationInfo{}; 120 : 121 : DeviceObjectUID m_UID{INVALID_DEVICE_OBJECT_UID}; 122 : 123 : // Sampler image aspect mask is submask of the attachment one. As we can't 124 : // have both VK_IMAGE_ASPECT_DEPTH_BIT and VK_IMAGE_ASPECT_STENCIL_BIT for 125 : // VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL. 126 : VkImageAspectFlags m_AttachmentImageAspectMask = 0; 127 : VkImageAspectFlags m_SamplerImageAspectMask = 0; 128 : 129 : // We store a flag of all subresources, we don't have to handle them separately. 130 : // It's safe to store the current state while we use a single device command 131 : // context. 132 : bool m_Initialized = false; 133 : }; 134 : 135 : } // namespace Vulkan 136 : 137 : } // namespace Backend 138 : 139 : } // namespace Renderer 140 : 141 : #endif // INCLUDED_RENDERER_BACKEND_VULKAN_TEXTURE