Line data Source code
1 : /* Copyright (C) 2026 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 : #include "precompiled.h"
19 :
20 : #include "Texture.h"
21 :
22 : #include "lib/debug.h"
23 : #include "ps/CLogger.h"
24 : #include "renderer/backend/Sampler.h"
25 : #include "renderer/backend/vulkan/Device.h"
26 : #include "renderer/backend/vulkan/DeviceSelection.h"
27 : #include "renderer/backend/vulkan/Mapping.h"
28 : #include "renderer/backend/vulkan/SamplerManager.h"
29 : #include "renderer/backend/vulkan/Utilities.h"
30 :
31 : namespace Renderer
32 : {
33 :
34 : namespace Backend
35 : {
36 :
37 : namespace Vulkan
38 : {
39 :
40 : // static
41 0 : std::unique_ptr<CTexture> CTexture::Create(
42 : CDevice* device, const char* name, const Type type, const uint32_t usage,
43 : const Format format, const uint32_t width, const uint32_t height,
44 : const Sampler::Desc& defaultSamplerDesc,
45 : const uint32_t MIPLevelCount, const uint32_t sampleCount)
46 : {
47 0 : std::unique_ptr<CTexture> texture(new CTexture(device));
48 :
49 0 : texture->m_Format = format;
50 0 : texture->m_Type = type;
51 0 : texture->m_Usage = usage;
52 0 : texture->m_Width = width;
53 0 : texture->m_Height = height;
54 0 : texture->m_MIPLevelCount = MIPLevelCount;
55 0 : texture->m_SampleCount = sampleCount;
56 0 : texture->m_LayerCount = type == ITexture::Type::TEXTURE_CUBE ? 6 : 1;
57 :
58 0 : if (type == Type::TEXTURE_2D_MULTISAMPLE)
59 0 : ENSURE(sampleCount > 1);
60 :
61 0 : VkFormat imageFormat = VK_FORMAT_UNDEFINED;
62 : // A8 and L8 are special cases for GL2.1, because it doesn't have a proper
63 : // channel swizzling.
64 0 : if (format == Format::A8_UNORM || format == Format::L8_UNORM)
65 : imageFormat = VK_FORMAT_R8_UNORM;
66 : else
67 0 : imageFormat = Mapping::FromFormat(format);
68 0 : texture->m_VkFormat = imageFormat;
69 :
70 0 : VkImageType imageType = VK_IMAGE_TYPE_2D;
71 0 : VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
72 :
73 0 : const VkPhysicalDevice physicalDevice =
74 0 : device->GetChoosenPhysicalDevice().device;
75 :
76 0 : VkFormatProperties formatProperties{};
77 0 : vkGetPhysicalDeviceFormatProperties(
78 : physicalDevice, imageFormat, &formatProperties);
79 :
80 0 : if (!(usage & Usage::SAMPLED))
81 : {
82 : // A texture can't be *_ATTACHMENT and STORAGE at the same time without
83 : // to be SAMPLED.
84 0 : const bool isAttachment = (usage & Usage::COLOR_ATTACHMENT) || (usage & Usage::DEPTH_STENCIL_ATTACHMENT);
85 0 : const bool isStorage = usage & Usage::STORAGE;
86 0 : ENSURE(!(isAttachment && isStorage));
87 : }
88 :
89 0 : VkImageUsageFlags usageFlags = 0;
90 : // Vulkan 1.0 implies that TRANSFER_SRC and TRANSFER_DST are supported.
91 : // TODO: account Vulkan 1.1.
92 0 : if (usage & Usage::TRANSFER_SRC)
93 0 : usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
94 0 : if (usage & Usage::TRANSFER_DST)
95 0 : usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
96 0 : if (usage & Usage::SAMPLED)
97 : {
98 0 : ENSURE(type != Type::TEXTURE_2D_MULTISAMPLE);
99 0 : if (!(formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT))
100 : {
101 0 : LOGERROR("Format {} doesn't support sampling for optimal tiling.",
102 : static_cast<int>(imageFormat));
103 0 : return nullptr;
104 : }
105 0 : usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT;
106 : }
107 0 : if (usage & Usage::STORAGE)
108 : {
109 0 : ENSURE(type != Type::TEXTURE_2D_MULTISAMPLE);
110 0 : if (!(formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT))
111 : {
112 0 : LOGERROR("Format {} doesn't support storage for optimal tiling.",
113 : static_cast<int>(imageFormat));
114 0 : return nullptr;
115 : }
116 0 : usageFlags |= VK_IMAGE_USAGE_STORAGE_BIT;
117 : }
118 0 : if (usage & Usage::COLOR_ATTACHMENT)
119 : {
120 0 : ENSURE(device->IsFramebufferFormatSupported(format));
121 0 : if (!(formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT))
122 : {
123 0 : LOGERROR("Format {} doesn't support color attachment for optimal tiling.",
124 : static_cast<int>(imageFormat));
125 0 : return nullptr;
126 : }
127 0 : usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
128 : }
129 0 : if (usage & Usage::DEPTH_STENCIL_ATTACHMENT)
130 : {
131 0 : ENSURE(IsDepthFormat(format));
132 0 : if (!(formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))
133 : {
134 0 : LOGERROR("Format {} doesn't support depth stencil attachment for optimal tiling.",
135 : static_cast<int>(imageFormat));
136 0 : return nullptr;
137 : }
138 0 : usageFlags |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
139 : }
140 :
141 0 : if (IsDepthFormat(format))
142 : {
143 0 : texture->m_AttachmentImageAspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
144 0 : texture->m_SamplerImageAspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
145 0 : if (format == Format::D24_UNORM_S8_UINT || format == Format::D32_SFLOAT_S8_UINT)
146 0 : texture->m_AttachmentImageAspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
147 : }
148 : else
149 : {
150 0 : texture->m_AttachmentImageAspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
151 0 : texture->m_SamplerImageAspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
152 : }
153 :
154 0 : VkImageCreateInfo imageCreateInfo{};
155 0 : imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
156 0 : imageCreateInfo.imageType = imageType;
157 0 : imageCreateInfo.extent.width = width;
158 0 : imageCreateInfo.extent.height = height;
159 0 : imageCreateInfo.extent.depth = 1;
160 0 : imageCreateInfo.mipLevels = MIPLevelCount;
161 0 : imageCreateInfo.arrayLayers = type == Type::TEXTURE_CUBE ? 6 : 1;
162 0 : imageCreateInfo.format = imageFormat;
163 0 : imageCreateInfo.samples = Mapping::FromSampleCount(sampleCount);
164 0 : imageCreateInfo.tiling = tiling;
165 0 : imageCreateInfo.usage = usageFlags;
166 0 : imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
167 0 : imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
168 :
169 0 : if (type == Type::TEXTURE_CUBE)
170 0 : imageCreateInfo.flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
171 :
172 0 : VmaAllocationCreateInfo allocationCreateInfo{};
173 0 : if ((usage & Usage::COLOR_ATTACHMENT) || (usage & Usage::DEPTH_STENCIL_ATTACHMENT))
174 0 : allocationCreateInfo.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
175 : #ifndef NDEBUG
176 : allocationCreateInfo.flags |= VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT;
177 : allocationCreateInfo.pUserData = const_cast<char*>(name);
178 : #endif
179 0 : allocationCreateInfo.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
180 0 : allocationCreateInfo.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE;
181 0 : const VkResult createImageResult = vmaCreateImage(
182 : device->GetVMAAllocator(), &imageCreateInfo, &allocationCreateInfo,
183 0 : &texture->m_Image, &texture->m_Allocation, nullptr);
184 0 : if (createImageResult != VK_SUCCESS)
185 : {
186 0 : LOGERROR("Failed to create VkImage: {} ({})",
187 : static_cast<int>(createImageResult), Utilities::GetVkResultName(createImageResult));
188 0 : return nullptr;
189 : }
190 :
191 0 : VkImageViewCreateInfo imageViewCreateInfo{};
192 0 : imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
193 0 : imageViewCreateInfo.image = texture->m_Image;
194 0 : imageViewCreateInfo.viewType = type == Type::TEXTURE_CUBE ? VK_IMAGE_VIEW_TYPE_CUBE : VK_IMAGE_VIEW_TYPE_2D;
195 0 : imageViewCreateInfo.format = imageFormat;
196 0 : imageViewCreateInfo.subresourceRange.baseMipLevel = 0;
197 0 : imageViewCreateInfo.subresourceRange.levelCount = MIPLevelCount;
198 0 : imageViewCreateInfo.subresourceRange.baseArrayLayer = 0;
199 0 : imageViewCreateInfo.subresourceRange.layerCount = type == Type::TEXTURE_CUBE ? 6 : 1;
200 0 : if (format == Format::A8_UNORM)
201 : {
202 0 : imageViewCreateInfo.components.r = VK_COMPONENT_SWIZZLE_ZERO;
203 0 : imageViewCreateInfo.components.g = VK_COMPONENT_SWIZZLE_ZERO;
204 0 : imageViewCreateInfo.components.b = VK_COMPONENT_SWIZZLE_ZERO;
205 0 : imageViewCreateInfo.components.a = VK_COMPONENT_SWIZZLE_R;
206 : }
207 0 : else if (format == Format::L8_UNORM)
208 : {
209 0 : imageViewCreateInfo.components.r = VK_COMPONENT_SWIZZLE_R;
210 0 : imageViewCreateInfo.components.g = VK_COMPONENT_SWIZZLE_R;
211 0 : imageViewCreateInfo.components.b = VK_COMPONENT_SWIZZLE_R;
212 0 : imageViewCreateInfo.components.a = VK_COMPONENT_SWIZZLE_ONE;
213 : }
214 : else
215 : {
216 : imageViewCreateInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
217 : imageViewCreateInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
218 : imageViewCreateInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
219 : imageViewCreateInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
220 : }
221 :
222 0 : if ((usage & Usage::COLOR_ATTACHMENT) || (usage & Usage::DEPTH_STENCIL_ATTACHMENT))
223 : {
224 0 : imageViewCreateInfo.subresourceRange.aspectMask = texture->m_AttachmentImageAspectMask;
225 0 : ENSURE_VK_SUCCESS(vkCreateImageView(
226 : device->GetVkDevice(), &imageViewCreateInfo, nullptr, &texture->m_AttachmentImageView));
227 : }
228 :
229 0 : if (usage & Usage::SAMPLED)
230 : {
231 0 : imageViewCreateInfo.subresourceRange.aspectMask = texture->m_SamplerImageAspectMask;
232 0 : ENSURE_VK_SUCCESS(vkCreateImageView(
233 : device->GetVkDevice(), &imageViewCreateInfo, nullptr, &texture->m_SamplerImageView));
234 :
235 0 : texture->m_Sampler = device->GetSamplerManager().GetOrCreateSampler(
236 : defaultSamplerDesc);
237 0 : texture->m_IsCompareEnabled = defaultSamplerDesc.compareEnabled;
238 : }
239 :
240 0 : device->SetObjectName(VK_OBJECT_TYPE_IMAGE, texture->m_Image, name);
241 0 : if (texture->m_AttachmentImageView != VK_NULL_HANDLE)
242 0 : device->SetObjectName(VK_OBJECT_TYPE_IMAGE_VIEW, texture->m_AttachmentImageView, name);
243 0 : if (texture->m_SamplerImageView != VK_NULL_HANDLE)
244 0 : device->SetObjectName(VK_OBJECT_TYPE_IMAGE_VIEW, texture->m_SamplerImageView, name);
245 :
246 0 : return texture;
247 0 : }
248 :
249 : // static
250 0 : std::unique_ptr<CTexture> CTexture::WrapBackbufferImage(
251 : CDevice* device, const char* name, const VkImage image, const VkFormat format,
252 : const VkImageUsageFlags usage, const uint32_t width, const uint32_t height)
253 : {
254 0 : std::unique_ptr<CTexture> texture(new CTexture(device));
255 :
256 0 : if (format == VK_FORMAT_R8G8B8A8_UNORM)
257 0 : texture->m_Format = Format::R8G8B8A8_UNORM;
258 0 : else if (format == VK_FORMAT_B8G8R8A8_UNORM)
259 0 : texture->m_Format = Format::B8G8R8A8_UNORM;
260 : else
261 0 : texture->m_Format = Format::UNDEFINED;
262 0 : texture->m_Type = Type::TEXTURE_2D;
263 0 : if (usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
264 0 : texture->m_Usage |= Usage::COLOR_ATTACHMENT;
265 0 : if (usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT)
266 0 : texture->m_Usage |= Usage::TRANSFER_SRC;
267 0 : if (usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)
268 0 : texture->m_Usage |= Usage::TRANSFER_DST;
269 0 : texture->m_Width = width;
270 0 : texture->m_Height = height;
271 0 : texture->m_MIPLevelCount = 1;
272 0 : texture->m_SampleCount = 1;
273 0 : texture->m_LayerCount = 1;
274 0 : texture->m_VkFormat = format;
275 : // The image is owned by its swapchain, but we don't set a special flag
276 : // because the ownership is detected by m_Allocation presence.
277 0 : texture->m_Image = image;
278 0 : texture->m_AttachmentImageAspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
279 0 : texture->m_SamplerImageAspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
280 :
281 0 : VkImageViewCreateInfo imageViewCreateInfo{};
282 0 : imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
283 0 : imageViewCreateInfo.image = image;
284 0 : imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
285 0 : imageViewCreateInfo.format = format;
286 0 : imageViewCreateInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
287 0 : imageViewCreateInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
288 0 : imageViewCreateInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
289 0 : imageViewCreateInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
290 0 : imageViewCreateInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
291 0 : imageViewCreateInfo.subresourceRange.baseMipLevel = 0;
292 0 : imageViewCreateInfo.subresourceRange.levelCount = 1;
293 0 : imageViewCreateInfo.subresourceRange.baseArrayLayer = 0;
294 0 : imageViewCreateInfo.subresourceRange.layerCount = 1;
295 0 : ENSURE_VK_SUCCESS(vkCreateImageView(
296 : device->GetVkDevice(), &imageViewCreateInfo, nullptr, &texture->m_AttachmentImageView));
297 0 : device->SetObjectName(VK_OBJECT_TYPE_IMAGE_VIEW, texture->m_AttachmentImageView, name);
298 :
299 0 : return texture;
300 0 : }
301 :
302 : // static
303 0 : std::unique_ptr<CTexture> CTexture::CreateReadback(
304 : CDevice* device, const char* name, const Format format,
305 : const uint32_t width, const uint32_t height)
306 : {
307 0 : std::unique_ptr<CTexture> texture(new CTexture(device));
308 :
309 0 : texture->m_Format = format;
310 0 : texture->m_Type = Type::TEXTURE_2D;
311 0 : texture->m_Usage = Usage::TRANSFER_DST;
312 0 : texture->m_Width = width;
313 0 : texture->m_Height = height;
314 0 : texture->m_MIPLevelCount = 1;
315 0 : texture->m_SampleCount = 1;
316 0 : texture->m_LayerCount = 1;
317 0 : texture->m_VkFormat = Mapping::FromFormat(texture->m_Format);
318 0 : texture->m_AttachmentImageAspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
319 0 : texture->m_SamplerImageAspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
320 :
321 0 : VkImageCreateInfo imageCreateInfo{};
322 0 : imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
323 0 : imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
324 0 : imageCreateInfo.extent.width = width;
325 0 : imageCreateInfo.extent.height = height;
326 0 : imageCreateInfo.extent.depth = 1;
327 0 : imageCreateInfo.mipLevels = 1;
328 0 : imageCreateInfo.arrayLayers = 1;
329 0 : imageCreateInfo.format = texture->m_VkFormat;
330 0 : imageCreateInfo.samples = Mapping::FromSampleCount(1);
331 0 : imageCreateInfo.tiling = VK_IMAGE_TILING_LINEAR;
332 0 : imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT;
333 0 : imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
334 0 : imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
335 :
336 0 : VmaAllocationCreateInfo allocationCreateInfo{};
337 0 : allocationCreateInfo.flags |= VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT;
338 : #ifndef NDEBUG
339 : allocationCreateInfo.flags |= VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT;
340 : allocationCreateInfo.pUserData = const_cast<char*>(name);
341 : #endif
342 0 : allocationCreateInfo.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
343 0 : allocationCreateInfo.usage = VMA_MEMORY_USAGE_AUTO_PREFER_HOST;
344 0 : const VkResult createImageResult = vmaCreateImage(
345 : device->GetVMAAllocator(), &imageCreateInfo, &allocationCreateInfo,
346 0 : &texture->m_Image, &texture->m_Allocation, &texture->m_AllocationInfo);
347 0 : if (createImageResult != VK_SUCCESS)
348 : {
349 0 : LOGERROR("Failed to create VkImage: {} ({})",
350 : static_cast<int>(createImageResult), Utilities::GetVkResultName(createImageResult));
351 0 : return nullptr;
352 : }
353 :
354 0 : if (!texture->m_AllocationInfo.pMappedData)
355 : {
356 0 : LOGERROR("Failed to map readback image.");
357 0 : return nullptr;
358 : }
359 :
360 0 : device->SetObjectName(VK_OBJECT_TYPE_IMAGE, texture->m_Image, name);
361 :
362 0 : return texture;
363 0 : }
364 :
365 0 : CTexture::CTexture(CDevice* device)
366 0 : : m_Device(device), m_UID(device->GenerateNextDeviceObjectUID())
367 : {
368 0 : }
369 :
370 0 : CTexture::~CTexture()
371 : {
372 0 : if (m_AttachmentImageView != VK_NULL_HANDLE)
373 0 : m_Device->ScheduleObjectToDestroy(
374 0 : VK_OBJECT_TYPE_IMAGE_VIEW, m_AttachmentImageView, VK_NULL_HANDLE);
375 :
376 0 : if (m_SamplerImageView != VK_NULL_HANDLE)
377 0 : m_Device->ScheduleObjectToDestroy(
378 0 : VK_OBJECT_TYPE_IMAGE_VIEW, m_SamplerImageView, VK_NULL_HANDLE);
379 :
380 0 : if (m_Allocation != VK_NULL_HANDLE)
381 0 : m_Device->ScheduleObjectToDestroy(
382 0 : VK_OBJECT_TYPE_IMAGE, m_Image, m_Allocation);
383 :
384 0 : m_Device->ScheduleTextureToDestroy(m_UID);
385 0 : }
386 :
387 0 : IDevice* CTexture::GetDevice()
388 : {
389 0 : return m_Device;
390 : }
391 :
392 : } // namespace Vulkan
393 :
394 : } // namespace Backend
395 :
396 : } // namespace Renderer
|