1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "VkImage.hpp"
16
17 #include "VkBuffer.hpp"
18 #include "VkDevice.hpp"
19 #include "VkDeviceMemory.hpp"
20 #include "VkImageView.hpp"
21 #include "VkStringify.hpp"
22 #include "Device/ASTC_Decoder.hpp"
23 #include "Device/BC_Decoder.hpp"
24 #include "Device/Blitter.hpp"
25 #include "Device/ETC_Decoder.hpp"
26
27 #ifdef __ANDROID__
28 # include "System/GrallocAndroid.hpp"
29 # include "VkDeviceMemoryExternalAndroid.hpp"
30 #endif
31
32 #include <cstring>
33
34 namespace {
35
GetInputType(const vk::Format & format)36 ETC_Decoder::InputType GetInputType(const vk::Format &format)
37 {
38 switch(format)
39 {
40 case VK_FORMAT_EAC_R11_UNORM_BLOCK:
41 return ETC_Decoder::ETC_R_UNSIGNED;
42 case VK_FORMAT_EAC_R11_SNORM_BLOCK:
43 return ETC_Decoder::ETC_R_SIGNED;
44 case VK_FORMAT_EAC_R11G11_UNORM_BLOCK:
45 return ETC_Decoder::ETC_RG_UNSIGNED;
46 case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
47 return ETC_Decoder::ETC_RG_SIGNED;
48 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
49 case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
50 return ETC_Decoder::ETC_RGB;
51 case VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
52 case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
53 return ETC_Decoder::ETC_RGB_PUNCHTHROUGH_ALPHA;
54 case VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
55 case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
56 return ETC_Decoder::ETC_RGBA;
57 default:
58 UNSUPPORTED("format: %d", int(format));
59 return ETC_Decoder::ETC_RGBA;
60 }
61 }
62
GetBCn(const vk::Format & format)63 int GetBCn(const vk::Format &format)
64 {
65 switch(format)
66 {
67 case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
68 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
69 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
70 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
71 return 1;
72 case VK_FORMAT_BC2_UNORM_BLOCK:
73 case VK_FORMAT_BC2_SRGB_BLOCK:
74 return 2;
75 case VK_FORMAT_BC3_UNORM_BLOCK:
76 case VK_FORMAT_BC3_SRGB_BLOCK:
77 return 3;
78 case VK_FORMAT_BC4_UNORM_BLOCK:
79 case VK_FORMAT_BC4_SNORM_BLOCK:
80 return 4;
81 case VK_FORMAT_BC5_UNORM_BLOCK:
82 case VK_FORMAT_BC5_SNORM_BLOCK:
83 return 5;
84 case VK_FORMAT_BC6H_UFLOAT_BLOCK:
85 case VK_FORMAT_BC6H_SFLOAT_BLOCK:
86 return 6;
87 case VK_FORMAT_BC7_UNORM_BLOCK:
88 case VK_FORMAT_BC7_SRGB_BLOCK:
89 return 7;
90 default:
91 UNSUPPORTED("format: %d", int(format));
92 return 0;
93 }
94 }
95
96 // Returns true for BC1 if we have an RGB format, false for RGBA
97 // Returns true for BC4, BC5, BC6H if we have an unsigned format, false for signed
98 // Ignored by BC2, BC3, and BC7
GetNoAlphaOrUnsigned(const vk::Format & format)99 bool GetNoAlphaOrUnsigned(const vk::Format &format)
100 {
101 switch(format)
102 {
103 case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
104 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
105 case VK_FORMAT_BC4_UNORM_BLOCK:
106 case VK_FORMAT_BC5_UNORM_BLOCK:
107 case VK_FORMAT_BC6H_UFLOAT_BLOCK:
108 return true;
109 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
110 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
111 case VK_FORMAT_BC2_UNORM_BLOCK:
112 case VK_FORMAT_BC2_SRGB_BLOCK:
113 case VK_FORMAT_BC3_UNORM_BLOCK:
114 case VK_FORMAT_BC3_SRGB_BLOCK:
115 case VK_FORMAT_BC4_SNORM_BLOCK:
116 case VK_FORMAT_BC5_SNORM_BLOCK:
117 case VK_FORMAT_BC6H_SFLOAT_BLOCK:
118 case VK_FORMAT_BC7_SRGB_BLOCK:
119 case VK_FORMAT_BC7_UNORM_BLOCK:
120 return false;
121 default:
122 UNSUPPORTED("format: %d", int(format));
123 return false;
124 }
125 }
126
GetImageFormat(const VkImageCreateInfo * pCreateInfo)127 VkFormat GetImageFormat(const VkImageCreateInfo *pCreateInfo)
128 {
129 auto nextInfo = reinterpret_cast<VkBaseInStructure const *>(pCreateInfo->pNext);
130 while(nextInfo)
131 {
132 switch(nextInfo->sType)
133 {
134 #ifdef __ANDROID__
135 case VK_STRUCTURE_TYPE_EXTERNAL_FORMAT_ANDROID:
136 {
137 const VkExternalFormatANDROID *externalFormatAndroid = reinterpret_cast<const VkExternalFormatANDROID *>(nextInfo);
138
139 // VkExternalFormatANDROID: "If externalFormat is zero, the effect is as if the VkExternalFormatANDROID structure was not present."
140 if(externalFormatAndroid->externalFormat == 0)
141 {
142 break;
143 }
144
145 const VkFormat correspondingVkFormat = AHardwareBufferExternalMemory::GetVkFormatFromAHBFormat(externalFormatAndroid->externalFormat);
146 ASSERT(pCreateInfo->format == VK_FORMAT_UNDEFINED || pCreateInfo->format == correspondingVkFormat);
147 return correspondingVkFormat;
148 }
149 break;
150 #endif
151 default:
152 LOG_TRAP("pCreateInfo->pNext->sType = %s", vk::Stringify(nextInfo->sType).c_str());
153 break;
154 }
155
156 nextInfo = nextInfo->pNext;
157 }
158
159 return pCreateInfo->format;
160 }
161
162 } // anonymous namespace
163
164 namespace vk {
165
Image(const VkImageCreateInfo * pCreateInfo,void * mem,Device * device)166 Image::Image(const VkImageCreateInfo *pCreateInfo, void *mem, Device *device)
167 : device(device)
168 , flags(pCreateInfo->flags)
169 , imageType(pCreateInfo->imageType)
170 , format(GetImageFormat(pCreateInfo))
171 , extent(pCreateInfo->extent)
172 , mipLevels(pCreateInfo->mipLevels)
173 , arrayLayers(pCreateInfo->arrayLayers)
174 , samples(pCreateInfo->samples)
175 , tiling(pCreateInfo->tiling)
176 , usage(pCreateInfo->usage)
177 {
178 if(format.isCompressed())
179 {
180 VkImageCreateInfo compressedImageCreateInfo = *pCreateInfo;
181 compressedImageCreateInfo.format = format.getDecompressedFormat();
182 decompressedImage = new(mem) Image(&compressedImageCreateInfo, nullptr, device);
183 }
184
185 const auto *nextInfo = reinterpret_cast<const VkBaseInStructure *>(pCreateInfo->pNext);
186 for(; nextInfo != nullptr; nextInfo = nextInfo->pNext)
187 {
188 if(nextInfo->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO)
189 {
190 const auto *externalInfo = reinterpret_cast<const VkExternalMemoryImageCreateInfo *>(nextInfo);
191 supportedExternalMemoryHandleTypes = externalInfo->handleTypes;
192 }
193 }
194 }
195
destroy(const VkAllocationCallbacks * pAllocator)196 void Image::destroy(const VkAllocationCallbacks *pAllocator)
197 {
198 if(decompressedImage)
199 {
200 vk::deallocate(decompressedImage, pAllocator);
201 }
202 }
203
ComputeRequiredAllocationSize(const VkImageCreateInfo * pCreateInfo)204 size_t Image::ComputeRequiredAllocationSize(const VkImageCreateInfo *pCreateInfo)
205 {
206 return Format(pCreateInfo->format).isCompressed() ? sizeof(Image) : 0;
207 }
208
getMemoryRequirements() const209 const VkMemoryRequirements Image::getMemoryRequirements() const
210 {
211 VkMemoryRequirements memoryRequirements;
212 memoryRequirements.alignment = vk::REQUIRED_MEMORY_ALIGNMENT;
213 memoryRequirements.memoryTypeBits = vk::MEMORY_TYPE_GENERIC_BIT;
214 memoryRequirements.size = getStorageSize(format.getAspects()) +
215 (decompressedImage ? decompressedImage->getStorageSize(decompressedImage->format.getAspects()) : 0);
216 return memoryRequirements;
217 }
218
getSizeInBytes(const VkImageSubresourceRange & subresourceRange) const219 size_t Image::getSizeInBytes(const VkImageSubresourceRange &subresourceRange) const
220 {
221 size_t size = 0;
222 uint32_t lastLayer = getLastLayerIndex(subresourceRange);
223 uint32_t lastMipLevel = getLastMipLevel(subresourceRange);
224 uint32_t layerCount = lastLayer - subresourceRange.baseArrayLayer + 1;
225 uint32_t mipLevelCount = lastMipLevel - subresourceRange.baseMipLevel + 1;
226
227 auto aspect = static_cast<VkImageAspectFlagBits>(subresourceRange.aspectMask);
228
229 if(layerCount > 1)
230 {
231 if(mipLevelCount < mipLevels) // Compute size for all layers except the last one, then add relevant mip level sizes only for last layer
232 {
233 size = (layerCount - 1) * getLayerSize(aspect);
234 for(uint32_t mipLevel = subresourceRange.baseMipLevel; mipLevel <= lastMipLevel; ++mipLevel)
235 {
236 size += getMultiSampledLevelSize(aspect, mipLevel);
237 }
238 }
239 else // All mip levels used, compute full layer sizes
240 {
241 size = layerCount * getLayerSize(aspect);
242 }
243 }
244 else // Single layer, add all mip levels in the subresource range
245 {
246 for(uint32_t mipLevel = subresourceRange.baseMipLevel; mipLevel <= lastMipLevel; ++mipLevel)
247 {
248 size += getMultiSampledLevelSize(aspect, mipLevel);
249 }
250 }
251
252 return size;
253 }
254
canBindToMemory(DeviceMemory * pDeviceMemory) const255 bool Image::canBindToMemory(DeviceMemory *pDeviceMemory) const
256 {
257 return pDeviceMemory->checkExternalMemoryHandleType(supportedExternalMemoryHandleTypes);
258 }
259
bind(DeviceMemory * pDeviceMemory,VkDeviceSize pMemoryOffset)260 void Image::bind(DeviceMemory *pDeviceMemory, VkDeviceSize pMemoryOffset)
261 {
262 deviceMemory = pDeviceMemory;
263 memoryOffset = pMemoryOffset;
264 if(decompressedImage)
265 {
266 decompressedImage->deviceMemory = deviceMemory;
267 decompressedImage->memoryOffset = memoryOffset + getStorageSize(format.getAspects());
268 }
269 }
270
271 #ifdef __ANDROID__
prepareForExternalUseANDROID() const272 VkResult Image::prepareForExternalUseANDROID() const
273 {
274 void *nativeBuffer = nullptr;
275 VkExtent3D extent = getMipLevelExtent(VK_IMAGE_ASPECT_COLOR_BIT, 0);
276
277 buffer_handle_t importedBufferHandle = nullptr;
278 if(GrallocModule::getInstance()->import(backingMemory.nativeHandle, &importedBufferHandle) != 0)
279 {
280 return VK_ERROR_OUT_OF_DATE_KHR;
281 }
282 if(!importedBufferHandle)
283 {
284 return VK_ERROR_OUT_OF_DATE_KHR;
285 }
286
287 if(GrallocModule::getInstance()->lock(importedBufferHandle, GRALLOC_USAGE_SW_WRITE_OFTEN, 0, 0, extent.width, extent.height, &nativeBuffer) != 0)
288 {
289 return VK_ERROR_OUT_OF_DATE_KHR;
290 }
291
292 if(!nativeBuffer)
293 {
294 return VK_ERROR_OUT_OF_DATE_KHR;
295 }
296
297 int imageRowBytes = rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
298 int bufferRowBytes = backingMemory.stride * getFormat().bytes();
299 ASSERT(imageRowBytes <= bufferRowBytes);
300
301 uint8_t *srcBuffer = static_cast<uint8_t *>(deviceMemory->getOffsetPointer(0));
302 uint8_t *dstBuffer = static_cast<uint8_t *>(nativeBuffer);
303 for(uint32_t i = 0; i < extent.height; i++)
304 {
305 memcpy(dstBuffer + (i * bufferRowBytes), srcBuffer + (i * imageRowBytes), imageRowBytes);
306 }
307
308 if(GrallocModule::getInstance()->unlock(importedBufferHandle) != 0)
309 {
310 return VK_ERROR_OUT_OF_DATE_KHR;
311 }
312
313 if(GrallocModule::getInstance()->release(importedBufferHandle) != 0)
314 {
315 return VK_ERROR_OUT_OF_DATE_KHR;
316 }
317
318 return VK_SUCCESS;
319 }
320
getExternalMemory() const321 VkDeviceMemory Image::getExternalMemory() const
322 {
323 return backingMemory.externalMemory ? *deviceMemory : VkDeviceMemory{ VK_NULL_HANDLE };
324 }
325 #endif
326
getSubresourceLayout(const VkImageSubresource * pSubresource,VkSubresourceLayout * pLayout) const327 void Image::getSubresourceLayout(const VkImageSubresource *pSubresource, VkSubresourceLayout *pLayout) const
328 {
329 // By spec, aspectMask has a single bit set.
330 if(!((pSubresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) ||
331 (pSubresource->aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT) ||
332 (pSubresource->aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT) ||
333 (pSubresource->aspectMask == VK_IMAGE_ASPECT_PLANE_0_BIT) ||
334 (pSubresource->aspectMask == VK_IMAGE_ASPECT_PLANE_1_BIT) ||
335 (pSubresource->aspectMask == VK_IMAGE_ASPECT_PLANE_2_BIT)))
336 {
337 UNSUPPORTED("aspectMask %X", pSubresource->aspectMask);
338 }
339
340 auto aspect = static_cast<VkImageAspectFlagBits>(pSubresource->aspectMask);
341 pLayout->offset = getMemoryOffset(aspect, pSubresource->mipLevel, pSubresource->arrayLayer);
342 pLayout->size = getMultiSampledLevelSize(aspect, pSubresource->mipLevel);
343 pLayout->rowPitch = rowPitchBytes(aspect, pSubresource->mipLevel);
344 pLayout->depthPitch = slicePitchBytes(aspect, pSubresource->mipLevel);
345 pLayout->arrayPitch = getLayerSize(aspect);
346 }
347
copyTo(Image * dstImage,const VkImageCopy & region) const348 void Image::copyTo(Image *dstImage, const VkImageCopy ®ion) const
349 {
350 // Image copy does not perform any conversion, it simply copies memory from
351 // an image to another image that has the same number of bytes per pixel.
352
353 if(!((region.srcSubresource.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) ||
354 (region.srcSubresource.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT) ||
355 (region.srcSubresource.aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT) ||
356 (region.srcSubresource.aspectMask == VK_IMAGE_ASPECT_PLANE_0_BIT) ||
357 (region.srcSubresource.aspectMask == VK_IMAGE_ASPECT_PLANE_1_BIT) ||
358 (region.srcSubresource.aspectMask == VK_IMAGE_ASPECT_PLANE_2_BIT)))
359 {
360 UNSUPPORTED("srcSubresource.aspectMask %X", region.srcSubresource.aspectMask);
361 }
362
363 if(!((region.dstSubresource.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) ||
364 (region.dstSubresource.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT) ||
365 (region.dstSubresource.aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT) ||
366 (region.dstSubresource.aspectMask == VK_IMAGE_ASPECT_PLANE_0_BIT) ||
367 (region.dstSubresource.aspectMask == VK_IMAGE_ASPECT_PLANE_1_BIT) ||
368 (region.dstSubresource.aspectMask == VK_IMAGE_ASPECT_PLANE_2_BIT)))
369 {
370 UNSUPPORTED("dstSubresource.aspectMask %X", region.dstSubresource.aspectMask);
371 }
372
373 VkImageAspectFlagBits srcAspect = static_cast<VkImageAspectFlagBits>(region.srcSubresource.aspectMask);
374 VkImageAspectFlagBits dstAspect = static_cast<VkImageAspectFlagBits>(region.dstSubresource.aspectMask);
375
376 Format srcFormat = getFormat(srcAspect);
377 Format dstFormat = dstImage->getFormat(dstAspect);
378 int bytesPerBlock = srcFormat.bytesPerBlock();
379 ASSERT(bytesPerBlock == dstFormat.bytesPerBlock());
380 ASSERT(samples == dstImage->samples);
381
382 VkExtent3D srcExtent = getMipLevelExtent(srcAspect, region.srcSubresource.mipLevel);
383 VkExtent3D dstExtent = dstImage->getMipLevelExtent(dstAspect, region.dstSubresource.mipLevel);
384 VkExtent3D copyExtent = imageExtentInBlocks(region.extent, srcAspect);
385
386 VkImageType srcImageType = imageType;
387 VkImageType dstImageType = dstImage->getImageType();
388 bool one3D = (srcImageType == VK_IMAGE_TYPE_3D) != (dstImageType == VK_IMAGE_TYPE_3D);
389 bool both3D = (srcImageType == VK_IMAGE_TYPE_3D) && (dstImageType == VK_IMAGE_TYPE_3D);
390
391 // Texel layout pitches, using the VkSubresourceLayout nomenclature.
392 int srcRowPitch = rowPitchBytes(srcAspect, region.srcSubresource.mipLevel);
393 int srcDepthPitch = slicePitchBytes(srcAspect, region.srcSubresource.mipLevel);
394 int dstRowPitch = dstImage->rowPitchBytes(dstAspect, region.dstSubresource.mipLevel);
395 int dstDepthPitch = dstImage->slicePitchBytes(dstAspect, region.dstSubresource.mipLevel);
396 VkDeviceSize srcArrayPitch = getLayerSize(srcAspect);
397 VkDeviceSize dstArrayPitch = dstImage->getLayerSize(dstAspect);
398
399 // These are the pitches used when iterating over the layers that are being copied by the
400 // vkCmdCopyImage command. They can differ from the above array piches because the spec states that:
401 // "If one image is VK_IMAGE_TYPE_3D and the other image is VK_IMAGE_TYPE_2D with multiple
402 // layers, then each slice is copied to or from a different layer."
403 VkDeviceSize srcLayerPitch = (srcImageType == VK_IMAGE_TYPE_3D) ? srcDepthPitch : srcArrayPitch;
404 VkDeviceSize dstLayerPitch = (dstImageType == VK_IMAGE_TYPE_3D) ? dstDepthPitch : dstArrayPitch;
405
406 // If one image is 3D, extent.depth must match the layer count. If both images are 2D,
407 // depth is 1 but the source and destination subresource layer count must match.
408 uint32_t layerCount = one3D ? copyExtent.depth : region.srcSubresource.layerCount;
409
410 // Copies between 2D and 3D images are treated as layers, so only use depth as the slice count when
411 // both images are 3D.
412 // Multisample images are currently implemented similar to 3D images by storing one sample per slice.
413 // TODO(b/160600347): Store samples consecutively.
414 uint32_t sliceCount = both3D ? copyExtent.depth : samples;
415
416 bool isSingleSlice = (sliceCount == 1);
417 bool isSingleRow = (copyExtent.height == 1) && isSingleSlice;
418 // In order to copy multiple rows using a single memcpy call, we
419 // have to make sure that we need to copy the entire row and that
420 // both source and destination rows have the same size in bytes
421 bool isEntireRow = (region.extent.width == srcExtent.width) &&
422 (region.extent.width == dstExtent.width) &&
423 // For non-compressed formats, blockWidth is 1. For compressed
424 // formats, rowPitchBytes returns the number of bytes for a row of
425 // blocks, so we have to divide by the block height, which means:
426 // srcRowPitchBytes / srcBlockWidth == dstRowPitchBytes / dstBlockWidth
427 // And, to avoid potential non exact integer division, for example if a
428 // block has 16 bytes and represents 5 rows, we change the equation to:
429 // srcRowPitchBytes * dstBlockWidth == dstRowPitchBytes * srcBlockWidth
430 ((srcRowPitch * dstFormat.blockWidth()) ==
431 (dstRowPitch * srcFormat.blockWidth()));
432 // In order to copy multiple slices using a single memcpy call, we
433 // have to make sure that we need to copy the entire slice and that
434 // both source and destination slices have the same size in bytes
435 bool isEntireSlice = isEntireRow &&
436 (copyExtent.height == srcExtent.height) &&
437 (copyExtent.height == dstExtent.height) &&
438 (srcDepthPitch == dstDepthPitch);
439
440 const uint8_t *srcLayer = static_cast<const uint8_t *>(getTexelPointer(region.srcOffset, { region.srcSubresource.aspectMask, region.srcSubresource.mipLevel, region.srcSubresource.baseArrayLayer }));
441 uint8_t *dstLayer = static_cast<uint8_t *>(dstImage->getTexelPointer(region.dstOffset, { region.dstSubresource.aspectMask, region.dstSubresource.mipLevel, region.dstSubresource.baseArrayLayer }));
442
443 for(uint32_t layer = 0; layer < layerCount; layer++)
444 {
445 if(isSingleRow) // Copy one row
446 {
447 size_t copySize = copyExtent.width * bytesPerBlock;
448 ASSERT((srcLayer + copySize) < end());
449 ASSERT((dstLayer + copySize) < dstImage->end());
450 memcpy(dstLayer, srcLayer, copySize);
451 }
452 else if(isEntireRow && isSingleSlice) // Copy one slice
453 {
454 size_t copySize = copyExtent.height * srcRowPitch;
455 ASSERT((srcLayer + copySize) < end());
456 ASSERT((dstLayer + copySize) < dstImage->end());
457 memcpy(dstLayer, srcLayer, copySize);
458 }
459 else if(isEntireSlice) // Copy multiple slices
460 {
461 size_t copySize = sliceCount * srcDepthPitch;
462 ASSERT((srcLayer + copySize) < end());
463 ASSERT((dstLayer + copySize) < dstImage->end());
464 memcpy(dstLayer, srcLayer, copySize);
465 }
466 else if(isEntireRow) // Copy slice by slice
467 {
468 size_t sliceSize = copyExtent.height * srcRowPitch;
469 const uint8_t *srcSlice = srcLayer;
470 uint8_t *dstSlice = dstLayer;
471
472 for(uint32_t z = 0; z < sliceCount; z++)
473 {
474 ASSERT((srcSlice + sliceSize) < end());
475 ASSERT((dstSlice + sliceSize) < dstImage->end());
476
477 memcpy(dstSlice, srcSlice, sliceSize);
478
479 dstSlice += dstDepthPitch;
480 srcSlice += srcDepthPitch;
481 }
482 }
483 else // Copy row by row
484 {
485 size_t rowSize = copyExtent.width * bytesPerBlock;
486 const uint8_t *srcSlice = srcLayer;
487 uint8_t *dstSlice = dstLayer;
488
489 for(uint32_t z = 0; z < sliceCount; z++)
490 {
491 const uint8_t *srcRow = srcSlice;
492 uint8_t *dstRow = dstSlice;
493
494 for(uint32_t y = 0; y < copyExtent.height; y++)
495 {
496 ASSERT((srcRow + rowSize) < end());
497 ASSERT((dstRow + rowSize) < dstImage->end());
498
499 memcpy(dstRow, srcRow, rowSize);
500
501 srcRow += srcRowPitch;
502 dstRow += dstRowPitch;
503 }
504
505 srcSlice += srcDepthPitch;
506 dstSlice += dstDepthPitch;
507 }
508 }
509
510 srcLayer += srcLayerPitch;
511 dstLayer += dstLayerPitch;
512 }
513
514 dstImage->contentsChanged({ region.dstSubresource.aspectMask, region.dstSubresource.mipLevel, 1,
515 region.dstSubresource.baseArrayLayer, region.dstSubresource.layerCount });
516 }
517
copy(Buffer * buffer,const VkBufferImageCopy & region,bool bufferIsSource)518 void Image::copy(Buffer *buffer, const VkBufferImageCopy ®ion, bool bufferIsSource)
519 {
520 switch(region.imageSubresource.aspectMask)
521 {
522 case VK_IMAGE_ASPECT_COLOR_BIT:
523 case VK_IMAGE_ASPECT_DEPTH_BIT:
524 case VK_IMAGE_ASPECT_STENCIL_BIT:
525 case VK_IMAGE_ASPECT_PLANE_0_BIT:
526 case VK_IMAGE_ASPECT_PLANE_1_BIT:
527 case VK_IMAGE_ASPECT_PLANE_2_BIT:
528 break;
529 default:
530 UNSUPPORTED("aspectMask %x", int(region.imageSubresource.aspectMask));
531 break;
532 }
533
534 auto aspect = static_cast<VkImageAspectFlagBits>(region.imageSubresource.aspectMask);
535 Format copyFormat = getFormat(aspect);
536
537 VkExtent3D imageExtent = imageExtentInBlocks(region.imageExtent, aspect);
538
539 if(imageExtent.width == 0 || imageExtent.height == 0 || imageExtent.depth == 0)
540 {
541 return;
542 }
543
544 VkExtent2D bufferExtent = bufferExtentInBlocks({ imageExtent.width, imageExtent.height }, region);
545 int bytesPerBlock = copyFormat.bytesPerBlock();
546 int bufferRowPitchBytes = bufferExtent.width * bytesPerBlock;
547 int bufferSlicePitchBytes = bufferExtent.height * bufferRowPitchBytes;
548 ASSERT(samples == 1);
549
550 uint8_t *bufferMemory = static_cast<uint8_t *>(buffer->getOffsetPointer(region.bufferOffset));
551 uint8_t *imageMemory = static_cast<uint8_t *>(getTexelPointer(region.imageOffset, { region.imageSubresource.aspectMask, region.imageSubresource.mipLevel, region.imageSubresource.baseArrayLayer }));
552 uint8_t *srcMemory = bufferIsSource ? bufferMemory : imageMemory;
553 uint8_t *dstMemory = bufferIsSource ? imageMemory : bufferMemory;
554 int imageRowPitchBytes = rowPitchBytes(aspect, region.imageSubresource.mipLevel);
555 int imageSlicePitchBytes = slicePitchBytes(aspect, region.imageSubresource.mipLevel);
556
557 int srcSlicePitchBytes = bufferIsSource ? bufferSlicePitchBytes : imageSlicePitchBytes;
558 int dstSlicePitchBytes = bufferIsSource ? imageSlicePitchBytes : bufferSlicePitchBytes;
559 int srcRowPitchBytes = bufferIsSource ? bufferRowPitchBytes : imageRowPitchBytes;
560 int dstRowPitchBytes = bufferIsSource ? imageRowPitchBytes : bufferRowPitchBytes;
561
562 VkExtent3D mipLevelExtent = getMipLevelExtent(aspect, region.imageSubresource.mipLevel);
563 bool isSingleSlice = (imageExtent.depth == 1);
564 bool isSingleRow = (imageExtent.height == 1) && isSingleSlice;
565 bool isEntireRow = (imageExtent.width == mipLevelExtent.width) &&
566 (imageRowPitchBytes == bufferRowPitchBytes);
567 bool isEntireSlice = isEntireRow && (imageExtent.height == mipLevelExtent.height) &&
568 (imageSlicePitchBytes == bufferSlicePitchBytes);
569
570 VkDeviceSize copySize = 0;
571 if(isSingleRow)
572 {
573 copySize = imageExtent.width * bytesPerBlock;
574 }
575 else if(isEntireRow && isSingleSlice)
576 {
577 copySize = (imageExtent.height - 1) * imageRowPitchBytes + imageExtent.width * bytesPerBlock;
578 }
579 else if(isEntireSlice)
580 {
581 copySize = (imageExtent.depth - 1) * imageSlicePitchBytes + (imageExtent.height - 1) * imageRowPitchBytes + imageExtent.width * bytesPerBlock; // Copy multiple slices
582 }
583 else if(isEntireRow) // Copy slice by slice
584 {
585 copySize = (imageExtent.height - 1) * imageRowPitchBytes + imageExtent.width * bytesPerBlock;
586 }
587 else // Copy row by row
588 {
589 copySize = imageExtent.width * bytesPerBlock;
590 }
591
592 VkDeviceSize imageLayerSize = getLayerSize(aspect);
593 VkDeviceSize srcLayerSize = bufferIsSource ? bufferSlicePitchBytes : imageLayerSize;
594 VkDeviceSize dstLayerSize = bufferIsSource ? imageLayerSize : bufferSlicePitchBytes;
595
596 for(uint32_t i = 0; i < region.imageSubresource.layerCount; i++)
597 {
598 if(isSingleRow || (isEntireRow && isSingleSlice) || isEntireSlice)
599 {
600 ASSERT(((bufferIsSource ? dstMemory : srcMemory) + copySize) < end());
601 ASSERT(((bufferIsSource ? srcMemory : dstMemory) + copySize) < buffer->end());
602 memcpy(dstMemory, srcMemory, copySize);
603 }
604 else if(isEntireRow) // Copy slice by slice
605 {
606 uint8_t *srcSliceMemory = srcMemory;
607 uint8_t *dstSliceMemory = dstMemory;
608 for(uint32_t z = 0; z < imageExtent.depth; z++)
609 {
610 ASSERT(((bufferIsSource ? dstSliceMemory : srcSliceMemory) + copySize) < end());
611 ASSERT(((bufferIsSource ? srcSliceMemory : dstSliceMemory) + copySize) < buffer->end());
612 memcpy(dstSliceMemory, srcSliceMemory, copySize);
613 srcSliceMemory += srcSlicePitchBytes;
614 dstSliceMemory += dstSlicePitchBytes;
615 }
616 }
617 else // Copy row by row
618 {
619 uint8_t *srcLayerMemory = srcMemory;
620 uint8_t *dstLayerMemory = dstMemory;
621 for(uint32_t z = 0; z < imageExtent.depth; z++)
622 {
623 uint8_t *srcSliceMemory = srcLayerMemory;
624 uint8_t *dstSliceMemory = dstLayerMemory;
625 for(uint32_t y = 0; y < imageExtent.height; y++)
626 {
627 ASSERT(((bufferIsSource ? dstSliceMemory : srcSliceMemory) + copySize) < end());
628 ASSERT(((bufferIsSource ? srcSliceMemory : dstSliceMemory) + copySize) < buffer->end());
629 memcpy(dstSliceMemory, srcSliceMemory, copySize);
630 srcSliceMemory += srcRowPitchBytes;
631 dstSliceMemory += dstRowPitchBytes;
632 }
633 srcLayerMemory += srcSlicePitchBytes;
634 dstLayerMemory += dstSlicePitchBytes;
635 }
636 }
637
638 srcMemory += srcLayerSize;
639 dstMemory += dstLayerSize;
640 }
641
642 if(bufferIsSource)
643 {
644 contentsChanged({ region.imageSubresource.aspectMask, region.imageSubresource.mipLevel, 1,
645 region.imageSubresource.baseArrayLayer, region.imageSubresource.layerCount });
646 }
647 }
648
copyTo(Buffer * dstBuffer,const VkBufferImageCopy & region)649 void Image::copyTo(Buffer *dstBuffer, const VkBufferImageCopy ®ion)
650 {
651 copy(dstBuffer, region, false);
652 }
653
copyFrom(Buffer * srcBuffer,const VkBufferImageCopy & region)654 void Image::copyFrom(Buffer *srcBuffer, const VkBufferImageCopy ®ion)
655 {
656 copy(srcBuffer, region, true);
657 }
658
getTexelPointer(const VkOffset3D & offset,const VkImageSubresource & subresource) const659 void *Image::getTexelPointer(const VkOffset3D &offset, const VkImageSubresource &subresource) const
660 {
661 VkImageAspectFlagBits aspect = static_cast<VkImageAspectFlagBits>(subresource.aspectMask);
662 return deviceMemory->getOffsetPointer(texelOffsetBytesInStorage(offset, subresource) +
663 getMemoryOffset(aspect, subresource.mipLevel, subresource.arrayLayer));
664 }
665
imageExtentInBlocks(const VkExtent3D & extent,VkImageAspectFlagBits aspect) const666 VkExtent3D Image::imageExtentInBlocks(const VkExtent3D &extent, VkImageAspectFlagBits aspect) const
667 {
668 VkExtent3D adjustedExtent = extent;
669 Format usedFormat = getFormat(aspect);
670 if(usedFormat.isCompressed())
671 {
672 // When using a compressed format, we use the block as the base unit, instead of the texel
673 int blockWidth = usedFormat.blockWidth();
674 int blockHeight = usedFormat.blockHeight();
675
676 // Mip level allocations will round up to the next block for compressed texture
677 adjustedExtent.width = ((adjustedExtent.width + blockWidth - 1) / blockWidth);
678 adjustedExtent.height = ((adjustedExtent.height + blockHeight - 1) / blockHeight);
679 }
680 return adjustedExtent;
681 }
682
imageOffsetInBlocks(const VkOffset3D & offset,VkImageAspectFlagBits aspect) const683 VkOffset3D Image::imageOffsetInBlocks(const VkOffset3D &offset, VkImageAspectFlagBits aspect) const
684 {
685 VkOffset3D adjustedOffset = offset;
686 Format usedFormat = getFormat(aspect);
687 if(usedFormat.isCompressed())
688 {
689 // When using a compressed format, we use the block as the base unit, instead of the texel
690 int blockWidth = usedFormat.blockWidth();
691 int blockHeight = usedFormat.blockHeight();
692
693 ASSERT(((offset.x % blockWidth) == 0) && ((offset.y % blockHeight) == 0)); // We can't offset within a block
694
695 adjustedOffset.x /= blockWidth;
696 adjustedOffset.y /= blockHeight;
697 }
698 return adjustedOffset;
699 }
700
bufferExtentInBlocks(const VkExtent2D & extent,const VkBufferImageCopy & region) const701 VkExtent2D Image::bufferExtentInBlocks(const VkExtent2D &extent, const VkBufferImageCopy ®ion) const
702 {
703 VkExtent2D adjustedExtent = extent;
704 VkImageAspectFlagBits aspect = static_cast<VkImageAspectFlagBits>(region.imageSubresource.aspectMask);
705 Format usedFormat = getFormat(aspect);
706 if(region.bufferRowLength != 0)
707 {
708 adjustedExtent.width = region.bufferRowLength;
709
710 if(usedFormat.isCompressed())
711 {
712 int blockWidth = usedFormat.blockWidth();
713 ASSERT((adjustedExtent.width % blockWidth) == 0);
714 adjustedExtent.width /= blockWidth;
715 }
716 }
717 if(region.bufferImageHeight != 0)
718 {
719 adjustedExtent.height = region.bufferImageHeight;
720
721 if(usedFormat.isCompressed())
722 {
723 int blockHeight = usedFormat.blockHeight();
724 ASSERT((adjustedExtent.height % blockHeight) == 0);
725 adjustedExtent.height /= blockHeight;
726 }
727 }
728 return adjustedExtent;
729 }
730
borderSize() const731 int Image::borderSize() const
732 {
733 // We won't add a border to compressed cube textures, we'll add it when we decompress the texture
734 return (isCube() && !format.isCompressed()) ? 1 : 0;
735 }
736
texelOffsetBytesInStorage(const VkOffset3D & offset,const VkImageSubresource & subresource) const737 VkDeviceSize Image::texelOffsetBytesInStorage(const VkOffset3D &offset, const VkImageSubresource &subresource) const
738 {
739 VkImageAspectFlagBits aspect = static_cast<VkImageAspectFlagBits>(subresource.aspectMask);
740 VkOffset3D adjustedOffset = imageOffsetInBlocks(offset, aspect);
741 int border = borderSize();
742 return adjustedOffset.z * slicePitchBytes(aspect, subresource.mipLevel) +
743 (adjustedOffset.y + border) * rowPitchBytes(aspect, subresource.mipLevel) +
744 (adjustedOffset.x + border) * getFormat(aspect).bytesPerBlock();
745 }
746
getMipLevelExtent(VkImageAspectFlagBits aspect,uint32_t mipLevel) const747 VkExtent3D Image::getMipLevelExtent(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
748 {
749 VkExtent3D mipLevelExtent;
750 mipLevelExtent.width = extent.width >> mipLevel;
751 mipLevelExtent.height = extent.height >> mipLevel;
752 mipLevelExtent.depth = extent.depth >> mipLevel;
753
754 if(mipLevelExtent.width == 0) { mipLevelExtent.width = 1; }
755 if(mipLevelExtent.height == 0) { mipLevelExtent.height = 1; }
756 if(mipLevelExtent.depth == 0) { mipLevelExtent.depth = 1; }
757
758 switch(aspect)
759 {
760 case VK_IMAGE_ASPECT_COLOR_BIT:
761 case VK_IMAGE_ASPECT_DEPTH_BIT:
762 case VK_IMAGE_ASPECT_STENCIL_BIT:
763 case VK_IMAGE_ASPECT_PLANE_0_BIT: // Vulkan 1.1 Table 31. Plane Format Compatibility Table: plane 0 of all defined formats is full resolution.
764 break;
765 case VK_IMAGE_ASPECT_PLANE_1_BIT:
766 case VK_IMAGE_ASPECT_PLANE_2_BIT:
767 switch(format)
768 {
769 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
770 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
771 ASSERT(mipLevelExtent.width % 2 == 0 && mipLevelExtent.height % 2 == 0); // Vulkan 1.1: "Images in this format must be defined with a width and height that is a multiple of two."
772 // Vulkan 1.1 Table 31. Plane Format Compatibility Table:
773 // Half-resolution U and V planes.
774 mipLevelExtent.width /= 2;
775 mipLevelExtent.height /= 2;
776 break;
777 default:
778 UNSUPPORTED("format %d", int(format));
779 }
780 break;
781 default:
782 UNSUPPORTED("aspect %x", int(aspect));
783 }
784
785 return mipLevelExtent;
786 }
787
rowPitchBytes(VkImageAspectFlagBits aspect,uint32_t mipLevel) const788 int Image::rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
789 {
790 if(deviceMemory && deviceMemory->hasExternalImageProperties())
791 {
792 return deviceMemory->externalImageRowPitchBytes(aspect);
793 }
794
795 // Depth and Stencil pitch should be computed separately
796 ASSERT((aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) !=
797 (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
798
799 VkExtent3D mipLevelExtent = getMipLevelExtent(aspect, mipLevel);
800 Format usedFormat = getFormat(aspect);
801 if(usedFormat.isCompressed())
802 {
803 VkExtent3D extentInBlocks = imageExtentInBlocks(mipLevelExtent, aspect);
804 return extentInBlocks.width * usedFormat.bytesPerBlock();
805 }
806
807 return usedFormat.pitchB(mipLevelExtent.width, borderSize(), true);
808 }
809
slicePitchBytes(VkImageAspectFlagBits aspect,uint32_t mipLevel) const810 int Image::slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
811 {
812 // Depth and Stencil slice should be computed separately
813 ASSERT((aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) !=
814 (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
815
816 VkExtent3D mipLevelExtent = getMipLevelExtent(aspect, mipLevel);
817 Format usedFormat = getFormat(aspect);
818 if(usedFormat.isCompressed())
819 {
820 VkExtent3D extentInBlocks = imageExtentInBlocks(mipLevelExtent, aspect);
821 return extentInBlocks.height * extentInBlocks.width * usedFormat.bytesPerBlock();
822 }
823
824 return usedFormat.sliceB(mipLevelExtent.width, mipLevelExtent.height, borderSize(), true);
825 }
826
getFormat(VkImageAspectFlagBits aspect) const827 Format Image::getFormat(VkImageAspectFlagBits aspect) const
828 {
829 return format.getAspectFormat(aspect);
830 }
831
isCube() const832 bool Image::isCube() const
833 {
834 return (flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) && (imageType == VK_IMAGE_TYPE_2D);
835 }
836
end() const837 uint8_t *Image::end() const
838 {
839 return reinterpret_cast<uint8_t *>(deviceMemory->getOffsetPointer(deviceMemory->getCommittedMemoryInBytes() + 1));
840 }
841
getMemoryOffset(VkImageAspectFlagBits aspect) const842 VkDeviceSize Image::getMemoryOffset(VkImageAspectFlagBits aspect) const
843 {
844 if(deviceMemory && deviceMemory->hasExternalImageProperties())
845 {
846 return deviceMemory->externalImageMemoryOffset(aspect);
847 }
848
849 switch(format)
850 {
851 case VK_FORMAT_D16_UNORM_S8_UINT:
852 case VK_FORMAT_D24_UNORM_S8_UINT:
853 case VK_FORMAT_D32_SFLOAT_S8_UINT:
854 if(aspect == VK_IMAGE_ASPECT_STENCIL_BIT)
855 {
856 // Offset by depth buffer to get to stencil buffer
857 return memoryOffset + getStorageSize(VK_IMAGE_ASPECT_DEPTH_BIT);
858 }
859 break;
860
861 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
862 if(aspect == VK_IMAGE_ASPECT_PLANE_2_BIT)
863 {
864 return memoryOffset + getStorageSize(VK_IMAGE_ASPECT_PLANE_1_BIT) + getStorageSize(VK_IMAGE_ASPECT_PLANE_0_BIT);
865 }
866 // Fall through to 2PLANE case:
867 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
868 if(aspect == VK_IMAGE_ASPECT_PLANE_1_BIT)
869 {
870 return memoryOffset + getStorageSize(VK_IMAGE_ASPECT_PLANE_0_BIT);
871 }
872 else
873 {
874 ASSERT(aspect == VK_IMAGE_ASPECT_PLANE_0_BIT);
875
876 return memoryOffset;
877 }
878 break;
879
880 default:
881 break;
882 }
883
884 return memoryOffset;
885 }
886
getMemoryOffset(VkImageAspectFlagBits aspect,uint32_t mipLevel) const887 VkDeviceSize Image::getMemoryOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
888 {
889 VkDeviceSize offset = getMemoryOffset(aspect);
890 for(uint32_t i = 0; i < mipLevel; ++i)
891 {
892 offset += getMultiSampledLevelSize(aspect, i);
893 }
894 return offset;
895 }
896
getMemoryOffset(VkImageAspectFlagBits aspect,uint32_t mipLevel,uint32_t layer) const897 VkDeviceSize Image::getMemoryOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel, uint32_t layer) const
898 {
899 return layer * getLayerOffset(aspect, mipLevel) + getMemoryOffset(aspect, mipLevel);
900 }
901
getMipLevelSize(VkImageAspectFlagBits aspect,uint32_t mipLevel) const902 VkDeviceSize Image::getMipLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
903 {
904 return getMipLevelExtent(aspect, mipLevel).depth * slicePitchBytes(aspect, mipLevel);
905 }
906
getMultiSampledLevelSize(VkImageAspectFlagBits aspect,uint32_t mipLevel) const907 VkDeviceSize Image::getMultiSampledLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
908 {
909 return getMipLevelSize(aspect, mipLevel) * samples;
910 }
911
is3DSlice() const912 bool Image::is3DSlice() const
913 {
914 return ((imageType == VK_IMAGE_TYPE_3D) && (flags & VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT));
915 }
916
getLayerOffset(VkImageAspectFlagBits aspect,uint32_t mipLevel) const917 VkDeviceSize Image::getLayerOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
918 {
919 if(is3DSlice())
920 {
921 // When the VkImageSubresourceRange structure is used to select a subset of the slices of a 3D
922 // image's mip level in order to create a 2D or 2D array image view of a 3D image created with
923 // VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT, baseArrayLayer and layerCount specify the first
924 // slice index and the number of slices to include in the created image view.
925 ASSERT(samples == VK_SAMPLE_COUNT_1_BIT);
926
927 // Offset to the proper slice of the 3D image's mip level
928 return slicePitchBytes(aspect, mipLevel);
929 }
930
931 return getLayerSize(aspect);
932 }
933
getLayerSize(VkImageAspectFlagBits aspect) const934 VkDeviceSize Image::getLayerSize(VkImageAspectFlagBits aspect) const
935 {
936 VkDeviceSize layerSize = 0;
937
938 for(uint32_t mipLevel = 0; mipLevel < mipLevels; ++mipLevel)
939 {
940 layerSize += getMultiSampledLevelSize(aspect, mipLevel);
941 }
942
943 return layerSize;
944 }
945
getStorageSize(VkImageAspectFlags aspectMask) const946 VkDeviceSize Image::getStorageSize(VkImageAspectFlags aspectMask) const
947 {
948 if((aspectMask & ~(VK_IMAGE_ASPECT_COLOR_BIT | VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT |
949 VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT | VK_IMAGE_ASPECT_PLANE_2_BIT)) != 0)
950 {
951 UNSUPPORTED("aspectMask %x", int(aspectMask));
952 }
953
954 VkDeviceSize storageSize = 0;
955
956 if(aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) storageSize += getLayerSize(VK_IMAGE_ASPECT_COLOR_BIT);
957 if(aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) storageSize += getLayerSize(VK_IMAGE_ASPECT_DEPTH_BIT);
958 if(aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) storageSize += getLayerSize(VK_IMAGE_ASPECT_STENCIL_BIT);
959 if(aspectMask & VK_IMAGE_ASPECT_PLANE_0_BIT) storageSize += getLayerSize(VK_IMAGE_ASPECT_PLANE_0_BIT);
960 if(aspectMask & VK_IMAGE_ASPECT_PLANE_1_BIT) storageSize += getLayerSize(VK_IMAGE_ASPECT_PLANE_1_BIT);
961 if(aspectMask & VK_IMAGE_ASPECT_PLANE_2_BIT) storageSize += getLayerSize(VK_IMAGE_ASPECT_PLANE_2_BIT);
962
963 return arrayLayers * storageSize;
964 }
965
getSampledImage(const vk::Format & imageViewFormat) const966 const Image *Image::getSampledImage(const vk::Format &imageViewFormat) const
967 {
968 bool isImageViewCompressed = imageViewFormat.isCompressed();
969 if(decompressedImage && !isImageViewCompressed)
970 {
971 ASSERT(flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT);
972 ASSERT(format.bytesPerBlock() == imageViewFormat.bytesPerBlock());
973 }
974 // If the ImageView's format is compressed, then we do need to decompress the image so that
975 // it may be sampled properly by texture sampling functions, which don't support compressed
976 // textures. If the ImageView's format is NOT compressed, then we reinterpret cast the
977 // compressed image into the ImageView's format, so we must return the compressed image as is.
978 return (decompressedImage && isImageViewCompressed) ? decompressedImage : this;
979 }
980
blitTo(Image * dstImage,const VkImageBlit & region,VkFilter filter) const981 void Image::blitTo(Image *dstImage, const VkImageBlit ®ion, VkFilter filter) const
982 {
983 device->getBlitter()->blit(this, dstImage, region, filter);
984 }
985
copyTo(uint8_t * dst,unsigned int dstPitch) const986 void Image::copyTo(uint8_t *dst, unsigned int dstPitch) const
987 {
988 device->getBlitter()->copy(this, dst, dstPitch);
989 }
990
resolveTo(Image * dstImage,const VkImageResolve & region) const991 void Image::resolveTo(Image *dstImage, const VkImageResolve ®ion) const
992 {
993 device->getBlitter()->resolve(this, dstImage, region);
994 }
995
resolveDepthStencilTo(const ImageView * src,ImageView * dst,const VkSubpassDescriptionDepthStencilResolve & dsResolve) const996 void Image::resolveDepthStencilTo(const ImageView *src, ImageView *dst, const VkSubpassDescriptionDepthStencilResolve &dsResolve) const
997 {
998 device->getBlitter()->resolveDepthStencil(src, dst, dsResolve);
999 }
1000
getClearFormat() const1001 VkFormat Image::getClearFormat() const
1002 {
1003 // Set the proper format for the clear value, as described here:
1004 // https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#clears-values
1005 if(format.isSignedUnnormalizedInteger())
1006 {
1007 return VK_FORMAT_R32G32B32A32_SINT;
1008 }
1009 else if(format.isUnsignedUnnormalizedInteger())
1010 {
1011 return VK_FORMAT_R32G32B32A32_UINT;
1012 }
1013
1014 return VK_FORMAT_R32G32B32A32_SFLOAT;
1015 }
1016
getLastLayerIndex(const VkImageSubresourceRange & subresourceRange) const1017 uint32_t Image::getLastLayerIndex(const VkImageSubresourceRange &subresourceRange) const
1018 {
1019 return ((subresourceRange.layerCount == VK_REMAINING_ARRAY_LAYERS) ? arrayLayers : (subresourceRange.baseArrayLayer + subresourceRange.layerCount)) - 1;
1020 }
1021
getLastMipLevel(const VkImageSubresourceRange & subresourceRange) const1022 uint32_t Image::getLastMipLevel(const VkImageSubresourceRange &subresourceRange) const
1023 {
1024 return ((subresourceRange.levelCount == VK_REMAINING_MIP_LEVELS) ? mipLevels : (subresourceRange.baseMipLevel + subresourceRange.levelCount)) - 1;
1025 }
1026
clear(void * pixelData,VkFormat pixelFormat,const vk::Format & viewFormat,const VkImageSubresourceRange & subresourceRange,const VkRect2D & renderArea)1027 void Image::clear(void *pixelData, VkFormat pixelFormat, const vk::Format &viewFormat, const VkImageSubresourceRange &subresourceRange, const VkRect2D &renderArea)
1028 {
1029 device->getBlitter()->clear(pixelData, pixelFormat, this, viewFormat, subresourceRange, &renderArea);
1030 }
1031
clear(const VkClearColorValue & color,const VkImageSubresourceRange & subresourceRange)1032 void Image::clear(const VkClearColorValue &color, const VkImageSubresourceRange &subresourceRange)
1033 {
1034 ASSERT(subresourceRange.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
1035
1036 device->getBlitter()->clear((void *)color.float32, getClearFormat(), this, format, subresourceRange);
1037 }
1038
clear(const VkClearDepthStencilValue & color,const VkImageSubresourceRange & subresourceRange)1039 void Image::clear(const VkClearDepthStencilValue &color, const VkImageSubresourceRange &subresourceRange)
1040 {
1041 ASSERT((subresourceRange.aspectMask & ~(VK_IMAGE_ASPECT_DEPTH_BIT |
1042 VK_IMAGE_ASPECT_STENCIL_BIT)) == 0);
1043
1044 if(subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT)
1045 {
1046 VkImageSubresourceRange depthSubresourceRange = subresourceRange;
1047 depthSubresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1048 device->getBlitter()->clear((void *)(&color.depth), VK_FORMAT_D32_SFLOAT, this, format, depthSubresourceRange);
1049 }
1050
1051 if(subresourceRange.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)
1052 {
1053 VkImageSubresourceRange stencilSubresourceRange = subresourceRange;
1054 stencilSubresourceRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1055 device->getBlitter()->clear((void *)(&color.stencil), VK_FORMAT_S8_UINT, this, format, stencilSubresourceRange);
1056 }
1057 }
1058
clear(const VkClearValue & clearValue,const vk::Format & viewFormat,const VkRect2D & renderArea,const VkImageSubresourceRange & subresourceRange)1059 void Image::clear(const VkClearValue &clearValue, const vk::Format &viewFormat, const VkRect2D &renderArea, const VkImageSubresourceRange &subresourceRange)
1060 {
1061 ASSERT((subresourceRange.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) ||
1062 (subresourceRange.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT |
1063 VK_IMAGE_ASPECT_STENCIL_BIT)));
1064
1065 if(subresourceRange.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT)
1066 {
1067 clear((void *)(clearValue.color.float32), getClearFormat(), viewFormat, subresourceRange, renderArea);
1068 }
1069 else
1070 {
1071 if(subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT)
1072 {
1073 VkImageSubresourceRange depthSubresourceRange = subresourceRange;
1074 depthSubresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
1075 clear((void *)(&clearValue.depthStencil.depth), VK_FORMAT_D32_SFLOAT, viewFormat, depthSubresourceRange, renderArea);
1076 }
1077
1078 if(subresourceRange.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)
1079 {
1080 VkImageSubresourceRange stencilSubresourceRange = subresourceRange;
1081 stencilSubresourceRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1082 clear((void *)(&clearValue.depthStencil.stencil), VK_FORMAT_S8_UINT, viewFormat, stencilSubresourceRange, renderArea);
1083 }
1084 }
1085 }
1086
requiresPreprocessing() const1087 bool Image::requiresPreprocessing() const
1088 {
1089 return (isCube() && (arrayLayers >= 6)) || decompressedImage;
1090 }
1091
contentsChanged(const VkImageSubresourceRange & subresourceRange,ContentsChangedContext contentsChangedContext)1092 void Image::contentsChanged(const VkImageSubresourceRange &subresourceRange, ContentsChangedContext contentsChangedContext)
1093 {
1094 // If this function is called after (possibly) writing to this image from a shader,
1095 // this must have the VK_IMAGE_USAGE_STORAGE_BIT set for the write operation to be
1096 // valid. Otherwise, we can't have legally written to this image, so we know we can
1097 // skip updating dirtyResources.
1098 if((contentsChangedContext == USING_STORAGE) && !(usage & VK_IMAGE_USAGE_STORAGE_BIT))
1099 {
1100 return;
1101 }
1102
1103 // If this isn't a cube or a compressed image, we'll never need dirtyResources,
1104 // so we can skip updating dirtyResources
1105 if(!requiresPreprocessing())
1106 {
1107 return;
1108 }
1109
1110 uint32_t lastLayer = getLastLayerIndex(subresourceRange);
1111 uint32_t lastMipLevel = getLastMipLevel(subresourceRange);
1112
1113 VkImageSubresource subresource = {
1114 subresourceRange.aspectMask,
1115 subresourceRange.baseMipLevel,
1116 subresourceRange.baseArrayLayer
1117 };
1118
1119 marl::lock lock(mutex);
1120 for(subresource.arrayLayer = subresourceRange.baseArrayLayer;
1121 subresource.arrayLayer <= lastLayer;
1122 subresource.arrayLayer++)
1123 {
1124 for(subresource.mipLevel = subresourceRange.baseMipLevel;
1125 subresource.mipLevel <= lastMipLevel;
1126 subresource.mipLevel++)
1127 {
1128 dirtySubresources.insert(subresource);
1129 }
1130 }
1131 }
1132
prepareForSampling(const VkImageSubresourceRange & subresourceRange)1133 void Image::prepareForSampling(const VkImageSubresourceRange &subresourceRange)
1134 {
1135 // If this isn't a cube or a compressed image, there's nothing to do
1136 if(!requiresPreprocessing())
1137 {
1138 return;
1139 }
1140
1141 uint32_t lastLayer = getLastLayerIndex(subresourceRange);
1142 uint32_t lastMipLevel = getLastMipLevel(subresourceRange);
1143
1144 VkImageSubresource subresource = {
1145 subresourceRange.aspectMask,
1146 subresourceRange.baseMipLevel,
1147 subresourceRange.baseArrayLayer
1148 };
1149
1150 marl::lock lock(mutex);
1151
1152 if(dirtySubresources.empty())
1153 {
1154 return;
1155 }
1156
1157 // First, decompress all relevant dirty subregions
1158 for(subresource.arrayLayer = subresourceRange.baseArrayLayer;
1159 subresource.arrayLayer <= lastLayer;
1160 subresource.arrayLayer++)
1161 {
1162 for(subresource.mipLevel = subresourceRange.baseMipLevel;
1163 subresource.mipLevel <= lastMipLevel;
1164 subresource.mipLevel++)
1165 {
1166 auto it = dirtySubresources.find(subresource);
1167 if(it != dirtySubresources.end())
1168 {
1169 decompress(subresource);
1170 }
1171 }
1172 }
1173
1174 // Second, update cubemap borders
1175 for(subresource.arrayLayer = subresourceRange.baseArrayLayer;
1176 subresource.arrayLayer <= lastLayer;
1177 subresource.arrayLayer++)
1178 {
1179 for(subresource.mipLevel = subresourceRange.baseMipLevel;
1180 subresource.mipLevel <= lastMipLevel;
1181 subresource.mipLevel++)
1182 {
1183 auto it = dirtySubresources.find(subresource);
1184 if(it != dirtySubresources.end())
1185 {
1186 if(updateCube(subresource))
1187 {
1188 // updateCube() updates all layers of all cubemaps at once, so remove entries to avoid duplicating effort
1189 VkImageSubresource cleanSubresource = subresource;
1190 for(cleanSubresource.arrayLayer = 0; cleanSubresource.arrayLayer < arrayLayers - 5;)
1191 {
1192 // Delete one cube's worth of dirty subregions
1193 for(uint32_t i = 0; i < 6; i++, cleanSubresource.arrayLayer++)
1194 {
1195 auto it = dirtySubresources.find(cleanSubresource);
1196 if(it != dirtySubresources.end())
1197 {
1198 dirtySubresources.erase(it);
1199 }
1200 }
1201 }
1202 }
1203 }
1204 }
1205 }
1206
1207 // Finally, mark all updated subregions clean
1208 for(subresource.arrayLayer = subresourceRange.baseArrayLayer;
1209 subresource.arrayLayer <= lastLayer;
1210 subresource.arrayLayer++)
1211 {
1212 for(subresource.mipLevel = subresourceRange.baseMipLevel;
1213 subresource.mipLevel <= lastMipLevel;
1214 subresource.mipLevel++)
1215 {
1216 auto it = dirtySubresources.find(subresource);
1217 if(it != dirtySubresources.end())
1218 {
1219 dirtySubresources.erase(it);
1220 }
1221 }
1222 }
1223 }
1224
decompress(const VkImageSubresource & subresource)1225 void Image::decompress(const VkImageSubresource &subresource)
1226 {
1227 if(decompressedImage)
1228 {
1229 switch(format)
1230 {
1231 case VK_FORMAT_EAC_R11_UNORM_BLOCK:
1232 case VK_FORMAT_EAC_R11_SNORM_BLOCK:
1233 case VK_FORMAT_EAC_R11G11_UNORM_BLOCK:
1234 case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
1235 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
1236 case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
1237 case VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
1238 case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
1239 case VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
1240 case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
1241 decodeETC2(subresource);
1242 break;
1243 case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
1244 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
1245 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
1246 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
1247 case VK_FORMAT_BC2_UNORM_BLOCK:
1248 case VK_FORMAT_BC2_SRGB_BLOCK:
1249 case VK_FORMAT_BC3_UNORM_BLOCK:
1250 case VK_FORMAT_BC3_SRGB_BLOCK:
1251 case VK_FORMAT_BC4_UNORM_BLOCK:
1252 case VK_FORMAT_BC4_SNORM_BLOCK:
1253 case VK_FORMAT_BC5_UNORM_BLOCK:
1254 case VK_FORMAT_BC5_SNORM_BLOCK:
1255 case VK_FORMAT_BC6H_UFLOAT_BLOCK:
1256 case VK_FORMAT_BC6H_SFLOAT_BLOCK:
1257 case VK_FORMAT_BC7_UNORM_BLOCK:
1258 case VK_FORMAT_BC7_SRGB_BLOCK:
1259 decodeBC(subresource);
1260 break;
1261 case VK_FORMAT_ASTC_4x4_UNORM_BLOCK:
1262 case VK_FORMAT_ASTC_5x4_UNORM_BLOCK:
1263 case VK_FORMAT_ASTC_5x5_UNORM_BLOCK:
1264 case VK_FORMAT_ASTC_6x5_UNORM_BLOCK:
1265 case VK_FORMAT_ASTC_6x6_UNORM_BLOCK:
1266 case VK_FORMAT_ASTC_8x5_UNORM_BLOCK:
1267 case VK_FORMAT_ASTC_8x6_UNORM_BLOCK:
1268 case VK_FORMAT_ASTC_8x8_UNORM_BLOCK:
1269 case VK_FORMAT_ASTC_10x5_UNORM_BLOCK:
1270 case VK_FORMAT_ASTC_10x6_UNORM_BLOCK:
1271 case VK_FORMAT_ASTC_10x8_UNORM_BLOCK:
1272 case VK_FORMAT_ASTC_10x10_UNORM_BLOCK:
1273 case VK_FORMAT_ASTC_12x10_UNORM_BLOCK:
1274 case VK_FORMAT_ASTC_12x12_UNORM_BLOCK:
1275 case VK_FORMAT_ASTC_4x4_SRGB_BLOCK:
1276 case VK_FORMAT_ASTC_5x4_SRGB_BLOCK:
1277 case VK_FORMAT_ASTC_5x5_SRGB_BLOCK:
1278 case VK_FORMAT_ASTC_6x5_SRGB_BLOCK:
1279 case VK_FORMAT_ASTC_6x6_SRGB_BLOCK:
1280 case VK_FORMAT_ASTC_8x5_SRGB_BLOCK:
1281 case VK_FORMAT_ASTC_8x6_SRGB_BLOCK:
1282 case VK_FORMAT_ASTC_8x8_SRGB_BLOCK:
1283 case VK_FORMAT_ASTC_10x5_SRGB_BLOCK:
1284 case VK_FORMAT_ASTC_10x6_SRGB_BLOCK:
1285 case VK_FORMAT_ASTC_10x8_SRGB_BLOCK:
1286 case VK_FORMAT_ASTC_10x10_SRGB_BLOCK:
1287 case VK_FORMAT_ASTC_12x10_SRGB_BLOCK:
1288 case VK_FORMAT_ASTC_12x12_SRGB_BLOCK:
1289 decodeASTC(subresource);
1290 break;
1291 default:
1292 break;
1293 }
1294 }
1295 }
1296
updateCube(const VkImageSubresource & subres)1297 bool Image::updateCube(const VkImageSubresource &subres)
1298 {
1299 if(isCube() && (arrayLayers >= 6))
1300 {
1301 VkImageSubresource subresource = subres;
1302
1303 // Update the borders of all the groups of 6 layers that can be part of a cubemaps but don't
1304 // touch leftover layers that cannot be part of cubemaps.
1305 for(subresource.arrayLayer = 0; subresource.arrayLayer < arrayLayers - 5; subresource.arrayLayer += 6)
1306 {
1307 device->getBlitter()->updateBorders(decompressedImage ? decompressedImage : this, subresource);
1308 }
1309
1310 return true;
1311 }
1312
1313 return false;
1314 }
1315
decodeETC2(const VkImageSubresource & subresource)1316 void Image::decodeETC2(const VkImageSubresource &subresource)
1317 {
1318 ASSERT(decompressedImage);
1319
1320 ETC_Decoder::InputType inputType = GetInputType(format);
1321
1322 int bytes = decompressedImage->format.bytes();
1323 bool fakeAlpha = (format == VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK) || (format == VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK);
1324 size_t sizeToWrite = 0;
1325
1326 VkExtent3D mipLevelExtent = getMipLevelExtent(static_cast<VkImageAspectFlagBits>(subresource.aspectMask), subresource.mipLevel);
1327
1328 int pitchB = decompressedImage->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, subresource.mipLevel);
1329
1330 if(fakeAlpha)
1331 {
1332 // To avoid overflow in case of cube textures, which are offset in memory to account for the border,
1333 // compute the size from the first pixel to the last pixel, excluding any padding or border before
1334 // the first pixel or after the last pixel.
1335 sizeToWrite = ((mipLevelExtent.height - 1) * pitchB) + (mipLevelExtent.width * bytes);
1336 }
1337
1338 for(int32_t depth = 0; depth < static_cast<int32_t>(mipLevelExtent.depth); depth++)
1339 {
1340 uint8_t *source = static_cast<uint8_t *>(getTexelPointer({ 0, 0, depth }, subresource));
1341 uint8_t *dest = static_cast<uint8_t *>(decompressedImage->getTexelPointer({ 0, 0, depth }, subresource));
1342
1343 if(fakeAlpha)
1344 {
1345 ASSERT((dest + sizeToWrite) < decompressedImage->end());
1346 memset(dest, 0xFF, sizeToWrite);
1347 }
1348
1349 ETC_Decoder::Decode(source, dest, mipLevelExtent.width, mipLevelExtent.height,
1350 pitchB, bytes, inputType);
1351 }
1352 }
1353
decodeBC(const VkImageSubresource & subresource)1354 void Image::decodeBC(const VkImageSubresource &subresource)
1355 {
1356 ASSERT(decompressedImage);
1357
1358 int n = GetBCn(format);
1359 int noAlphaU = GetNoAlphaOrUnsigned(format);
1360
1361 int bytes = decompressedImage->format.bytes();
1362
1363 VkExtent3D mipLevelExtent = getMipLevelExtent(static_cast<VkImageAspectFlagBits>(subresource.aspectMask), subresource.mipLevel);
1364
1365 int pitchB = decompressedImage->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, subresource.mipLevel);
1366
1367 for(int32_t depth = 0; depth < static_cast<int32_t>(mipLevelExtent.depth); depth++)
1368 {
1369 uint8_t *source = static_cast<uint8_t *>(getTexelPointer({ 0, 0, depth }, subresource));
1370 uint8_t *dest = static_cast<uint8_t *>(decompressedImage->getTexelPointer({ 0, 0, depth }, subresource));
1371
1372 BC_Decoder::Decode(source, dest, mipLevelExtent.width, mipLevelExtent.height,
1373 pitchB, bytes, n, noAlphaU);
1374 }
1375 }
1376
decodeASTC(const VkImageSubresource & subresource)1377 void Image::decodeASTC(const VkImageSubresource &subresource)
1378 {
1379 ASSERT(decompressedImage);
1380
1381 int xBlockSize = format.blockWidth();
1382 int yBlockSize = format.blockHeight();
1383 int zBlockSize = 1;
1384 bool isUnsigned = format.isUnsignedComponent(0);
1385
1386 int bytes = decompressedImage->format.bytes();
1387
1388 VkExtent3D mipLevelExtent = getMipLevelExtent(static_cast<VkImageAspectFlagBits>(subresource.aspectMask), subresource.mipLevel);
1389
1390 int xblocks = (mipLevelExtent.width + xBlockSize - 1) / xBlockSize;
1391 int yblocks = (mipLevelExtent.height + yBlockSize - 1) / yBlockSize;
1392 int zblocks = (zBlockSize > 1) ? (mipLevelExtent.depth + zBlockSize - 1) / zBlockSize : 1;
1393
1394 if(xblocks <= 0 || yblocks <= 0 || zblocks <= 0)
1395 {
1396 return;
1397 }
1398
1399 int pitchB = decompressedImage->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, subresource.mipLevel);
1400 int sliceB = decompressedImage->slicePitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, subresource.mipLevel);
1401
1402 for(int32_t depth = 0; depth < static_cast<int32_t>(mipLevelExtent.depth); depth++)
1403 {
1404 uint8_t *source = static_cast<uint8_t *>(getTexelPointer({ 0, 0, depth }, subresource));
1405 uint8_t *dest = static_cast<uint8_t *>(decompressedImage->getTexelPointer({ 0, 0, depth }, subresource));
1406
1407 ASTC_Decoder::Decode(source, dest, mipLevelExtent.width, mipLevelExtent.height, mipLevelExtent.depth, bytes, pitchB, sliceB,
1408 xBlockSize, yBlockSize, zBlockSize, xblocks, yblocks, zblocks, isUnsigned);
1409 }
1410 }
1411
1412 } // namespace vk
1413