Pyrogenesis  trunk
Texture.h
Go to the documentation of this file.
1 /* Copyright (C) 2023 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 
24 
25 #include <glad/vulkan.h>
26 #include <memory>
27 
28 namespace Renderer
29 {
30 
31 namespace Backend
32 {
33 
34 namespace Vulkan
35 {
36 
37 class CDevice;
38 
39 class CTexture final : public ITexture
40 {
41 public:
42  ~CTexture() override;
43 
44  IDevice* GetDevice() override;
45 
46  Type GetType() const override { return m_Type; }
47  uint32_t GetUsage() const override { return m_Usage; }
48  Format GetFormat() const override { return m_Format; }
49 
50  uint32_t GetWidth() const override { return m_Width; }
51  uint32_t GetHeight() const override { return m_Height; }
52  uint32_t GetMIPLevelCount() const override { return m_MIPLevelCount; }
54  uint32_t GetLayerCount() const { return m_LayerCount; }
55 
56  VkImage GetImage() { return m_Image; }
58  VkImageView GetSamplerImageView() { return m_SamplerImageView; }
59  VkSampler GetSampler() { return m_Sampler; }
61  VkFormat GetVkFormat() const { return m_VkFormat; }
62 
64  VkImageAspectFlags GetSamplerImageAspectMask() { return m_SamplerImageAspectMask; }
65 
66  bool IsInitialized() const { return m_Initialized; }
67  void SetInitialized() { m_Initialized = true; }
68 
69  /**
70  * @return UID of the texture. It's unique along all textures during a whole
71  * application run. We assume that 32bits should be enough, else we'd have
72  * a too big texture flow.
73  */
74  using UID = uint32_t;
75  UID GetUID() const { return m_UID; }
76 
77 private:
78  friend class CDevice;
79  friend class CSwapChain;
80 
81  CTexture();
82 
83  static std::unique_ptr<CTexture> Create(
84  CDevice* device, const char* name, const Type type, const uint32_t usage,
85  const Format format, const uint32_t width, const uint32_t height,
86  const Sampler::Desc& defaultSamplerDesc,
87  const uint32_t MIPLevelCount, const uint32_t sampleCount);
88 
89  static std::unique_ptr<CTexture> WrapBackbufferImage(
90  CDevice* device, const char* name, const VkImage image, const VkFormat format,
91  const VkImageUsageFlags usage, const uint32_t width, const uint32_t height);
92 
96  VkFormat m_VkFormat = VK_FORMAT_UNDEFINED;
102 
103  CDevice* m_Device = nullptr;
104 
105  VkImage m_Image = VK_NULL_HANDLE;
106  VkImageView m_AttachmentImageView = VK_NULL_HANDLE;
107  VkImageView m_SamplerImageView = VK_NULL_HANDLE;
108  VkSampler m_Sampler = VK_NULL_HANDLE;
109  bool m_IsCompareEnabled = false;
111 
112  UID m_UID = 0;
113 
114  // Sampler image aspect mask is submask of the attachment one. As we can't
115  // have both VK_IMAGE_ASPECT_DEPTH_BIT and VK_IMAGE_ASPECT_STENCIL_BIT for
116  // VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL.
117  VkImageAspectFlags m_AttachmentImageAspectMask = 0;
118  VkImageAspectFlags m_SamplerImageAspectMask = 0;
119 
120  // We store a flag of all subresources, we don't have to handle them separately.
121  // It's safe to store the current state while we use a single device command
122  // context.
123  bool m_Initialized = false;
124 };
125 
126 } // namespace Vulkan
127 
128 } // namespace Backend
129 
130 } // namespace Renderer
131 
132 #endif // INCLUDED_RENDERER_BACKEND_VULKAN_TEXTURE
uint32_t m_Usage
Definition: Texture.h:94
~CTexture() override
Definition: Texture.cpp:273
Type
Definition: ITexture.h:36
VkImageAspectFlags m_AttachmentImageAspectMask
Definition: Texture.h:117
Type m_Type
Definition: Texture.h:93
Definition: ITexture.h:33
uint32_t m_MIPLevelCount
Definition: Texture.h:99
VkImageView GetSamplerImageView()
Definition: Texture.h:58
void SetInitialized()
Definition: Texture.h:67
Format m_Format
Definition: Texture.h:95
uint32_t GetSampleCount() const
Definition: Texture.h:53
Definition: Texture.h:39
Definition: SwapChain.h:43
Definition: Device.h:59
static std::unique_ptr< CTexture > WrapBackbufferImage(CDevice *device, const char *name, const VkImage image, const VkFormat format, const VkImageUsageFlags usage, const uint32_t width, const uint32_t height)
Definition: Texture.cpp:219
uint32_t GetLayerCount() const
Definition: Texture.h:54
VkImageView m_AttachmentImageView
Definition: Texture.h:106
VkImageAspectFlags m_SamplerImageAspectMask
Definition: Texture.h:118
bool m_IsCompareEnabled
Definition: Texture.h:109
Format
Definition: Format.h:27
uint32_t m_LayerCount
Definition: Texture.h:101
uint32_t UID
Definition: Texture.h:74
VkImage m_Image
Definition: Texture.h:105
VkImageAspectFlags GetSamplerImageAspectMask()
Definition: Texture.h:64
VmaAllocation m_Allocation
Definition: Texture.h:110
IDevice * GetDevice() override
Definition: Texture.cpp:290
uint32_t GetUsage() const override
Definition: Texture.h:47
VkSampler m_Sampler
Definition: Texture.h:108
static std::unique_ptr< CTexture > Create(CDevice *device, const char *name, const Type type, const uint32_t usage, const Format format, const uint32_t width, const uint32_t height, const Sampler::Desc &defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount)
Definition: Texture.cpp:37
Definition: IDevice.h:47
VkImageView GetAttachmentImageView()
Definition: Texture.h:57
uint32_t GetWidth() const override
Definition: Texture.h:50
Represents single memory allocation.
bool IsInitialized() const
Definition: Texture.h:66
Backend
Definition: Backend.h:27
Format GetFormat() const override
Definition: Texture.h:48
uint32_t GetHeight() const override
Definition: Texture.h:51
Type GetType() const override
Definition: Texture.h:46
bool m_Initialized
Definition: Texture.h:123
VkFormat m_VkFormat
Definition: Texture.h:96
UID m_UID
Definition: Texture.h:112
VkImageAspectFlags GetAttachmentImageAspectMask()
Definition: Texture.h:63
unsigned int uint32_t
Definition: wposix_types.h:53
Definition: VideoMode.h:28
uint32_t GetMIPLevelCount() const override
Definition: Texture.h:52
uint32_t m_SampleCount
Definition: Texture.h:100
CTexture()
Definition: Texture.cpp:267
bool IsCompareEnabled()
Definition: Texture.h:60
CDevice * m_Device
Definition: Texture.h:103
uint32_t m_Height
Definition: Texture.h:98
VkImageView m_SamplerImageView
Definition: Texture.h:107
Definition: Sampler.h:56
uint32_t m_Width
Definition: Texture.h:97
UID GetUID() const
Definition: Texture.h:75
VkFormat GetVkFormat() const
Definition: Texture.h:61
VkSampler GetSampler()
Definition: Texture.h:59
VkImage GetImage()
Definition: Texture.h:56