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