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 "Device/ASTC_Decoder.hpp"
21 #include "Device/BC_Decoder.hpp"
22 #include "Device/Blitter.hpp"
23 #include "Device/ETC_Decoder.hpp"
24
25 #ifdef __ANDROID__
26 # include "System/GrallocAndroid.hpp"
27 #endif
28
29 #include <cstring>
30
31 namespace {
32
GetInputType(const vk::Format & format)33 ETC_Decoder::InputType GetInputType(const vk::Format &format)
34 {
35 switch(format)
36 {
37 case VK_FORMAT_EAC_R11_UNORM_BLOCK:
38 return ETC_Decoder::ETC_R_UNSIGNED;
39 case VK_FORMAT_EAC_R11_SNORM_BLOCK:
40 return ETC_Decoder::ETC_R_SIGNED;
41 case VK_FORMAT_EAC_R11G11_UNORM_BLOCK:
42 return ETC_Decoder::ETC_RG_UNSIGNED;
43 case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
44 return ETC_Decoder::ETC_RG_SIGNED;
45 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
46 case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
47 return ETC_Decoder::ETC_RGB;
48 case VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
49 case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
50 return ETC_Decoder::ETC_RGB_PUNCHTHROUGH_ALPHA;
51 case VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
52 case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
53 return ETC_Decoder::ETC_RGBA;
54 default:
55 UNSUPPORTED("format: %d", int(format));
56 return ETC_Decoder::ETC_RGBA;
57 }
58 }
59
GetBCn(const vk::Format & format)60 int GetBCn(const vk::Format &format)
61 {
62 switch(format)
63 {
64 case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
65 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
66 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
67 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
68 return 1;
69 case VK_FORMAT_BC2_UNORM_BLOCK:
70 case VK_FORMAT_BC2_SRGB_BLOCK:
71 return 2;
72 case VK_FORMAT_BC3_UNORM_BLOCK:
73 case VK_FORMAT_BC3_SRGB_BLOCK:
74 return 3;
75 case VK_FORMAT_BC4_UNORM_BLOCK:
76 case VK_FORMAT_BC4_SNORM_BLOCK:
77 return 4;
78 case VK_FORMAT_BC5_UNORM_BLOCK:
79 case VK_FORMAT_BC5_SNORM_BLOCK:
80 return 5;
81 case VK_FORMAT_BC6H_UFLOAT_BLOCK:
82 case VK_FORMAT_BC6H_SFLOAT_BLOCK:
83 return 6;
84 case VK_FORMAT_BC7_UNORM_BLOCK:
85 case VK_FORMAT_BC7_SRGB_BLOCK:
86 return 7;
87 default:
88 UNSUPPORTED("format: %d", int(format));
89 return 0;
90 }
91 }
92
93 // Returns true for BC1 if we have an RGB format, false for RGBA
94 // Returns true for BC4 and BC5 if we have an unsigned format, false for signed
95 // Ignored by BC2, BC3, BC6 and BC7
GetNoAlphaOrUnsigned(const vk::Format & format)96 bool GetNoAlphaOrUnsigned(const vk::Format &format)
97 {
98 switch(format)
99 {
100 case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
101 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
102 case VK_FORMAT_BC4_UNORM_BLOCK:
103 case VK_FORMAT_BC5_UNORM_BLOCK:
104 return true;
105 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
106 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
107 case VK_FORMAT_BC2_UNORM_BLOCK:
108 case VK_FORMAT_BC2_SRGB_BLOCK:
109 case VK_FORMAT_BC3_UNORM_BLOCK:
110 case VK_FORMAT_BC3_SRGB_BLOCK:
111 case VK_FORMAT_BC4_SNORM_BLOCK:
112 case VK_FORMAT_BC5_SNORM_BLOCK:
113 case VK_FORMAT_BC6H_UFLOAT_BLOCK:
114 case VK_FORMAT_BC6H_SFLOAT_BLOCK:
115 case VK_FORMAT_BC7_SRGB_BLOCK:
116 case VK_FORMAT_BC7_UNORM_BLOCK:
117 return false;
118 default:
119 UNSUPPORTED("format: %d", int(format));
120 return false;
121 }
122 }
123
124 } // anonymous namespace
125
126 namespace vk {
127
Image(const VkImageCreateInfo * pCreateInfo,void * mem,Device * device)128 Image::Image(const VkImageCreateInfo *pCreateInfo, void *mem, Device *device)
129 : device(device)
130 , flags(pCreateInfo->flags)
131 , imageType(pCreateInfo->imageType)
132 , format(pCreateInfo->format)
133 , extent(pCreateInfo->extent)
134 , mipLevels(pCreateInfo->mipLevels)
135 , arrayLayers(pCreateInfo->arrayLayers)
136 , samples(pCreateInfo->samples)
137 , tiling(pCreateInfo->tiling)
138 , usage(pCreateInfo->usage)
139 {
140 if(format.isCompressed())
141 {
142 VkImageCreateInfo compressedImageCreateInfo = *pCreateInfo;
143 compressedImageCreateInfo.format = format.getDecompressedFormat();
144 decompressedImage = new(mem) Image(&compressedImageCreateInfo, nullptr, device);
145 }
146
147 const auto *nextInfo = reinterpret_cast<const VkBaseInStructure *>(pCreateInfo->pNext);
148 for(; nextInfo != nullptr; nextInfo = nextInfo->pNext)
149 {
150 if(nextInfo->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO)
151 {
152 const auto *externalInfo = reinterpret_cast<const VkExternalMemoryImageCreateInfo *>(nextInfo);
153 supportedExternalMemoryHandleTypes = externalInfo->handleTypes;
154 }
155 }
156 }
157
destroy(const VkAllocationCallbacks * pAllocator)158 void Image::destroy(const VkAllocationCallbacks *pAllocator)
159 {
160 if(decompressedImage)
161 {
162 vk::deallocate(decompressedImage, pAllocator);
163 }
164 }
165
ComputeRequiredAllocationSize(const VkImageCreateInfo * pCreateInfo)166 size_t Image::ComputeRequiredAllocationSize(const VkImageCreateInfo *pCreateInfo)
167 {
168 return Format(pCreateInfo->format).isCompressed() ? sizeof(Image) : 0;
169 }
170
getMemoryRequirements() const171 const VkMemoryRequirements Image::getMemoryRequirements() const
172 {
173 VkMemoryRequirements memoryRequirements;
174 memoryRequirements.alignment = vk::REQUIRED_MEMORY_ALIGNMENT;
175 memoryRequirements.memoryTypeBits = vk::MEMORY_TYPE_GENERIC_BIT;
176 memoryRequirements.size = getStorageSize(format.getAspects()) +
177 (decompressedImage ? decompressedImage->getStorageSize(decompressedImage->format.getAspects()) : 0);
178 return memoryRequirements;
179 }
180
getSizeInBytes(const VkImageSubresourceRange & subresourceRange) const181 size_t Image::getSizeInBytes(const VkImageSubresourceRange &subresourceRange) const
182 {
183 size_t size = 0;
184 uint32_t lastLayer = getLastLayerIndex(subresourceRange);
185 uint32_t lastMipLevel = getLastMipLevel(subresourceRange);
186 uint32_t layerCount = lastLayer - subresourceRange.baseArrayLayer + 1;
187 uint32_t mipLevelCount = lastMipLevel - subresourceRange.baseMipLevel + 1;
188
189 auto aspect = static_cast<VkImageAspectFlagBits>(subresourceRange.aspectMask);
190
191 if(layerCount > 1)
192 {
193 if(mipLevelCount < mipLevels) // Compute size for all layers except the last one, then add relevant mip level sizes only for last layer
194 {
195 size = (layerCount - 1) * getLayerSize(aspect);
196 for(uint32_t mipLevel = subresourceRange.baseMipLevel; mipLevel <= lastMipLevel; ++mipLevel)
197 {
198 size += getMultiSampledLevelSize(aspect, mipLevel);
199 }
200 }
201 else // All mip levels used, compute full layer sizes
202 {
203 size = layerCount * getLayerSize(aspect);
204 }
205 }
206 else // Single layer, add all mip levels in the subresource range
207 {
208 for(uint32_t mipLevel = subresourceRange.baseMipLevel; mipLevel <= lastMipLevel; ++mipLevel)
209 {
210 size += getMultiSampledLevelSize(aspect, mipLevel);
211 }
212 }
213
214 return size;
215 }
216
canBindToMemory(DeviceMemory * pDeviceMemory) const217 bool Image::canBindToMemory(DeviceMemory *pDeviceMemory) const
218 {
219 return pDeviceMemory->checkExternalMemoryHandleType(supportedExternalMemoryHandleTypes);
220 }
221
bind(DeviceMemory * pDeviceMemory,VkDeviceSize pMemoryOffset)222 void Image::bind(DeviceMemory *pDeviceMemory, VkDeviceSize pMemoryOffset)
223 {
224 deviceMemory = pDeviceMemory;
225 memoryOffset = pMemoryOffset;
226 if(decompressedImage)
227 {
228 decompressedImage->deviceMemory = deviceMemory;
229 decompressedImage->memoryOffset = memoryOffset + getStorageSize(format.getAspects());
230 }
231 }
232
233 #ifdef __ANDROID__
prepareForExternalUseANDROID() const234 VkResult Image::prepareForExternalUseANDROID() const
235 {
236 void *nativeBuffer = nullptr;
237 VkExtent3D extent = getMipLevelExtent(VK_IMAGE_ASPECT_COLOR_BIT, 0);
238
239 if(GrallocModule::getInstance()->lock(backingMemory.nativeHandle, GRALLOC_USAGE_SW_WRITE_OFTEN, 0, 0, extent.width, extent.height, &nativeBuffer) != 0)
240 {
241 return VK_ERROR_OUT_OF_DATE_KHR;
242 }
243
244 if(!nativeBuffer)
245 {
246 return VK_ERROR_OUT_OF_DATE_KHR;
247 }
248
249 int imageRowBytes = rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
250 int bufferRowBytes = backingMemory.stride * getFormat().bytes();
251 ASSERT(imageRowBytes <= bufferRowBytes);
252
253 uint8_t *srcBuffer = static_cast<uint8_t *>(deviceMemory->getOffsetPointer(0));
254 uint8_t *dstBuffer = static_cast<uint8_t *>(nativeBuffer);
255 for(uint32_t i = 0; i < extent.height; i++)
256 {
257 memcpy(dstBuffer + (i * bufferRowBytes), srcBuffer + (i * imageRowBytes), imageRowBytes);
258 }
259
260 if(GrallocModule::getInstance()->unlock(backingMemory.nativeHandle) != 0)
261 {
262 return VK_ERROR_OUT_OF_DATE_KHR;
263 }
264
265 return VK_SUCCESS;
266 }
267
getExternalMemory() const268 VkDeviceMemory Image::getExternalMemory() const
269 {
270 return backingMemory.externalMemory ? *deviceMemory : VkDeviceMemory{ VK_NULL_HANDLE };
271 }
272 #endif
273
getSubresourceLayout(const VkImageSubresource * pSubresource,VkSubresourceLayout * pLayout) const274 void Image::getSubresourceLayout(const VkImageSubresource *pSubresource, VkSubresourceLayout *pLayout) const
275 {
276 // By spec, aspectMask has a single bit set.
277 if(!((pSubresource->aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) ||
278 (pSubresource->aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT) ||
279 (pSubresource->aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT) ||
280 (pSubresource->aspectMask == VK_IMAGE_ASPECT_PLANE_0_BIT) ||
281 (pSubresource->aspectMask == VK_IMAGE_ASPECT_PLANE_1_BIT) ||
282 (pSubresource->aspectMask == VK_IMAGE_ASPECT_PLANE_2_BIT)))
283 {
284 UNSUPPORTED("aspectMask %X", pSubresource->aspectMask);
285 }
286
287 auto aspect = static_cast<VkImageAspectFlagBits>(pSubresource->aspectMask);
288 pLayout->offset = getMemoryOffset(aspect, pSubresource->mipLevel, pSubresource->arrayLayer);
289 pLayout->size = getMultiSampledLevelSize(aspect, pSubresource->mipLevel);
290 pLayout->rowPitch = rowPitchBytes(aspect, pSubresource->mipLevel);
291 pLayout->depthPitch = slicePitchBytes(aspect, pSubresource->mipLevel);
292 pLayout->arrayPitch = getLayerSize(aspect);
293 }
294
copyTo(Image * dstImage,const VkImageCopy & region) const295 void Image::copyTo(Image *dstImage, const VkImageCopy ®ion) const
296 {
297 // Image copy does not perform any conversion, it simply copies memory from
298 // an image to another image that has the same number of bytes per pixel.
299
300 if(!((region.srcSubresource.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) ||
301 (region.srcSubresource.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT) ||
302 (region.srcSubresource.aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT) ||
303 (region.srcSubresource.aspectMask == VK_IMAGE_ASPECT_PLANE_0_BIT) ||
304 (region.srcSubresource.aspectMask == VK_IMAGE_ASPECT_PLANE_1_BIT) ||
305 (region.srcSubresource.aspectMask == VK_IMAGE_ASPECT_PLANE_2_BIT)))
306 {
307 UNSUPPORTED("srcSubresource.aspectMask %X", region.srcSubresource.aspectMask);
308 }
309
310 if(!((region.dstSubresource.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) ||
311 (region.dstSubresource.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT) ||
312 (region.dstSubresource.aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT) ||
313 (region.dstSubresource.aspectMask == VK_IMAGE_ASPECT_PLANE_0_BIT) ||
314 (region.dstSubresource.aspectMask == VK_IMAGE_ASPECT_PLANE_1_BIT) ||
315 (region.dstSubresource.aspectMask == VK_IMAGE_ASPECT_PLANE_2_BIT)))
316 {
317 UNSUPPORTED("dstSubresource.aspectMask %X", region.dstSubresource.aspectMask);
318 }
319
320 VkImageAspectFlagBits srcAspect = static_cast<VkImageAspectFlagBits>(region.srcSubresource.aspectMask);
321 VkImageAspectFlagBits dstAspect = static_cast<VkImageAspectFlagBits>(region.dstSubresource.aspectMask);
322
323 Format srcFormat = getFormat(srcAspect);
324 Format dstFormat = dstImage->getFormat(dstAspect);
325
326 if((samples > VK_SAMPLE_COUNT_1_BIT) && (imageType == VK_IMAGE_TYPE_2D) && !format.isUnnormalizedInteger())
327 {
328 // Requires multisampling resolve
329 VkImageBlit blitRegion;
330 blitRegion.srcSubresource = region.srcSubresource;
331 blitRegion.srcOffsets[0] = region.srcOffset;
332 blitRegion.srcOffsets[1].x = blitRegion.srcOffsets[0].x + region.extent.width;
333 blitRegion.srcOffsets[1].y = blitRegion.srcOffsets[0].y + region.extent.height;
334 blitRegion.srcOffsets[1].z = blitRegion.srcOffsets[0].z + region.extent.depth;
335
336 blitRegion.dstSubresource = region.dstSubresource;
337 blitRegion.dstOffsets[0] = region.dstOffset;
338 blitRegion.dstOffsets[1].x = blitRegion.dstOffsets[0].x + region.extent.width;
339 blitRegion.dstOffsets[1].y = blitRegion.dstOffsets[0].y + region.extent.height;
340 blitRegion.dstOffsets[1].z = blitRegion.dstOffsets[0].z + region.extent.depth;
341
342 return device->getBlitter()->blit(this, dstImage, blitRegion, VK_FILTER_NEAREST);
343 }
344
345 int srcBytesPerBlock = srcFormat.bytesPerBlock();
346 ASSERT(srcBytesPerBlock == dstFormat.bytesPerBlock());
347
348 const uint8_t *srcMem = static_cast<const uint8_t *>(getTexelPointer(region.srcOffset, region.srcSubresource));
349 uint8_t *dstMem = static_cast<uint8_t *>(dstImage->getTexelPointer(region.dstOffset, region.dstSubresource));
350
351 int srcRowPitchBytes = rowPitchBytes(srcAspect, region.srcSubresource.mipLevel);
352 int srcSlicePitchBytes = slicePitchBytes(srcAspect, region.srcSubresource.mipLevel);
353 int dstRowPitchBytes = dstImage->rowPitchBytes(dstAspect, region.dstSubresource.mipLevel);
354 int dstSlicePitchBytes = dstImage->slicePitchBytes(dstAspect, region.dstSubresource.mipLevel);
355
356 VkExtent3D srcExtent = getMipLevelExtent(srcAspect, region.srcSubresource.mipLevel);
357 VkExtent3D dstExtent = dstImage->getMipLevelExtent(dstAspect, region.dstSubresource.mipLevel);
358 VkExtent3D copyExtent = imageExtentInBlocks(region.extent, srcAspect);
359
360 bool isSinglePlane = (copyExtent.depth == 1);
361 bool isSingleLine = (copyExtent.height == 1) && isSinglePlane;
362 // In order to copy multiple lines using a single memcpy call, we
363 // have to make sure that we need to copy the entire line and that
364 // both source and destination lines have the same length in bytes
365 bool isEntireLine = (region.extent.width == srcExtent.width) &&
366 (region.extent.width == dstExtent.width) &&
367 // For non compressed formats, blockWidth is 1. For compressed
368 // formats, rowPitchBytes returns the number of bytes for a row of
369 // blocks, so we have to divide by the block height, which means:
370 // srcRowPitchBytes / srcBlockWidth == dstRowPitchBytes / dstBlockWidth
371 // And, to avoid potential non exact integer division, for example if a
372 // block has 16 bytes and represents 5 lines, we change the equation to:
373 // srcRowPitchBytes * dstBlockWidth == dstRowPitchBytes * srcBlockWidth
374 ((srcRowPitchBytes * dstFormat.blockWidth()) ==
375 (dstRowPitchBytes * srcFormat.blockWidth()));
376 // In order to copy multiple planes using a single memcpy call, we
377 // have to make sure that we need to copy the entire plane and that
378 // both source and destination planes have the same length in bytes
379 bool isEntirePlane = isEntireLine &&
380 (copyExtent.height == srcExtent.height) &&
381 (copyExtent.height == dstExtent.height) &&
382 (srcSlicePitchBytes == dstSlicePitchBytes);
383
384 if(isSingleLine) // Copy one line
385 {
386 size_t copySize = copyExtent.width * srcBytesPerBlock;
387 ASSERT((srcMem + copySize) < end());
388 ASSERT((dstMem + copySize) < dstImage->end());
389 memcpy(dstMem, srcMem, copySize);
390 }
391 else if(isEntireLine && isSinglePlane) // Copy one plane
392 {
393 size_t copySize = copyExtent.height * srcRowPitchBytes;
394 ASSERT((srcMem + copySize) < end());
395 ASSERT((dstMem + copySize) < dstImage->end());
396 memcpy(dstMem, srcMem, copySize);
397 }
398 else if(isEntirePlane) // Copy multiple planes
399 {
400 size_t copySize = copyExtent.depth * srcSlicePitchBytes;
401 ASSERT((srcMem + copySize) < end());
402 ASSERT((dstMem + copySize) < dstImage->end());
403 memcpy(dstMem, srcMem, copySize);
404 }
405 else if(isEntireLine) // Copy plane by plane
406 {
407 size_t copySize = copyExtent.height * srcRowPitchBytes;
408
409 for(uint32_t z = 0; z < copyExtent.depth; z++, dstMem += dstSlicePitchBytes, srcMem += srcSlicePitchBytes)
410 {
411 ASSERT((srcMem + copySize) < end());
412 ASSERT((dstMem + copySize) < dstImage->end());
413 memcpy(dstMem, srcMem, copySize);
414 }
415 }
416 else // Copy line by line
417 {
418 size_t copySize = copyExtent.width * srcBytesPerBlock;
419
420 for(uint32_t z = 0; z < copyExtent.depth; z++, dstMem += dstSlicePitchBytes, srcMem += srcSlicePitchBytes)
421 {
422 const uint8_t *srcSlice = srcMem;
423 uint8_t *dstSlice = dstMem;
424 for(uint32_t y = 0; y < copyExtent.height; y++, dstSlice += dstRowPitchBytes, srcSlice += srcRowPitchBytes)
425 {
426 ASSERT((srcSlice + copySize) < end());
427 ASSERT((dstSlice + copySize) < dstImage->end());
428 memcpy(dstSlice, srcSlice, copySize);
429 }
430 }
431 }
432
433 dstImage->prepareForSampling({ region.dstSubresource.aspectMask, region.dstSubresource.mipLevel, 1,
434 region.dstSubresource.baseArrayLayer, region.dstSubresource.layerCount });
435 }
436
copy(Buffer * buffer,const VkBufferImageCopy & region,bool bufferIsSource)437 void Image::copy(Buffer *buffer, const VkBufferImageCopy ®ion, bool bufferIsSource)
438 {
439 switch(region.imageSubresource.aspectMask)
440 {
441 case VK_IMAGE_ASPECT_COLOR_BIT:
442 case VK_IMAGE_ASPECT_DEPTH_BIT:
443 case VK_IMAGE_ASPECT_STENCIL_BIT:
444 case VK_IMAGE_ASPECT_PLANE_0_BIT:
445 case VK_IMAGE_ASPECT_PLANE_1_BIT:
446 case VK_IMAGE_ASPECT_PLANE_2_BIT:
447 break;
448 default:
449 UNSUPPORTED("aspectMask %x", int(region.imageSubresource.aspectMask));
450 break;
451 }
452
453 auto aspect = static_cast<VkImageAspectFlagBits>(region.imageSubresource.aspectMask);
454 Format copyFormat = getFormat(aspect);
455
456 VkExtent3D imageExtent = imageExtentInBlocks(region.imageExtent, aspect);
457 VkExtent2D bufferExtent = bufferExtentInBlocks({ imageExtent.width, imageExtent.height }, region);
458 int bytesPerBlock = copyFormat.bytesPerBlock();
459 int bufferRowPitchBytes = bufferExtent.width * bytesPerBlock;
460 int bufferSlicePitchBytes = bufferExtent.height * bufferRowPitchBytes;
461
462 uint8_t *bufferMemory = static_cast<uint8_t *>(buffer->getOffsetPointer(region.bufferOffset));
463 uint8_t *imageMemory = static_cast<uint8_t *>(getTexelPointer(region.imageOffset, region.imageSubresource));
464 uint8_t *srcMemory = bufferIsSource ? bufferMemory : imageMemory;
465 uint8_t *dstMemory = bufferIsSource ? imageMemory : bufferMemory;
466 int imageRowPitchBytes = rowPitchBytes(aspect, region.imageSubresource.mipLevel);
467 int imageSlicePitchBytes = slicePitchBytes(aspect, region.imageSubresource.mipLevel);
468
469 int srcSlicePitchBytes = bufferIsSource ? bufferSlicePitchBytes : imageSlicePitchBytes;
470 int dstSlicePitchBytes = bufferIsSource ? imageSlicePitchBytes : bufferSlicePitchBytes;
471 int srcRowPitchBytes = bufferIsSource ? bufferRowPitchBytes : imageRowPitchBytes;
472 int dstRowPitchBytes = bufferIsSource ? imageRowPitchBytes : bufferRowPitchBytes;
473
474 VkExtent3D mipLevelExtent = getMipLevelExtent(aspect, region.imageSubresource.mipLevel);
475 bool isSinglePlane = (imageExtent.depth == 1);
476 bool isSingleLine = (imageExtent.height == 1) && isSinglePlane;
477 bool isEntireLine = (imageExtent.width == mipLevelExtent.width) &&
478 (imageRowPitchBytes == bufferRowPitchBytes);
479 bool isEntirePlane = isEntireLine && (imageExtent.height == mipLevelExtent.height) &&
480 (imageSlicePitchBytes == bufferSlicePitchBytes);
481
482 VkDeviceSize copySize = 0;
483 VkDeviceSize bufferLayerSize = 0;
484 if(isSingleLine)
485 {
486 copySize = imageExtent.width * bytesPerBlock;
487 bufferLayerSize = copySize;
488 }
489 else if(isEntireLine && isSinglePlane)
490 {
491 copySize = imageExtent.height * imageRowPitchBytes;
492 bufferLayerSize = copySize;
493 }
494 else if(isEntirePlane)
495 {
496 copySize = imageExtent.depth * imageSlicePitchBytes; // Copy multiple planes
497 bufferLayerSize = copySize;
498 }
499 else if(isEntireLine) // Copy plane by plane
500 {
501 copySize = imageExtent.height * imageRowPitchBytes;
502 bufferLayerSize = copySize * imageExtent.depth;
503 }
504 else // Copy line by line
505 {
506 copySize = imageExtent.width * bytesPerBlock;
507 bufferLayerSize = copySize * imageExtent.depth * imageExtent.height;
508 }
509
510 VkDeviceSize imageLayerSize = getLayerSize(aspect);
511 VkDeviceSize srcLayerSize = bufferIsSource ? bufferLayerSize : imageLayerSize;
512 VkDeviceSize dstLayerSize = bufferIsSource ? imageLayerSize : bufferLayerSize;
513
514 for(uint32_t i = 0; i < region.imageSubresource.layerCount; i++)
515 {
516 if(isSingleLine || (isEntireLine && isSinglePlane) || isEntirePlane)
517 {
518 ASSERT(((bufferIsSource ? dstMemory : srcMemory) + copySize) < end());
519 ASSERT(((bufferIsSource ? srcMemory : dstMemory) + copySize) < buffer->end());
520 memcpy(dstMemory, srcMemory, copySize);
521 }
522 else if(isEntireLine) // Copy plane by plane
523 {
524 uint8_t *srcPlaneMemory = srcMemory;
525 uint8_t *dstPlaneMemory = dstMemory;
526 for(uint32_t z = 0; z < imageExtent.depth; z++)
527 {
528 ASSERT(((bufferIsSource ? dstPlaneMemory : srcPlaneMemory) + copySize) < end());
529 ASSERT(((bufferIsSource ? srcPlaneMemory : dstPlaneMemory) + copySize) < buffer->end());
530 memcpy(dstPlaneMemory, srcPlaneMemory, copySize);
531 srcPlaneMemory += srcSlicePitchBytes;
532 dstPlaneMemory += dstSlicePitchBytes;
533 }
534 }
535 else // Copy line by line
536 {
537 uint8_t *srcLayerMemory = srcMemory;
538 uint8_t *dstLayerMemory = dstMemory;
539 for(uint32_t z = 0; z < imageExtent.depth; z++)
540 {
541 uint8_t *srcPlaneMemory = srcLayerMemory;
542 uint8_t *dstPlaneMemory = dstLayerMemory;
543 for(uint32_t y = 0; y < imageExtent.height; y++)
544 {
545 ASSERT(((bufferIsSource ? dstPlaneMemory : srcPlaneMemory) + copySize) < end());
546 ASSERT(((bufferIsSource ? srcPlaneMemory : dstPlaneMemory) + copySize) < buffer->end());
547 memcpy(dstPlaneMemory, srcPlaneMemory, copySize);
548 srcPlaneMemory += srcRowPitchBytes;
549 dstPlaneMemory += dstRowPitchBytes;
550 }
551 srcLayerMemory += srcSlicePitchBytes;
552 dstLayerMemory += dstSlicePitchBytes;
553 }
554 }
555
556 srcMemory += srcLayerSize;
557 dstMemory += dstLayerSize;
558 }
559
560 if(bufferIsSource)
561 {
562 prepareForSampling({ region.imageSubresource.aspectMask, region.imageSubresource.mipLevel, 1,
563 region.imageSubresource.baseArrayLayer, region.imageSubresource.layerCount });
564 }
565 }
566
copyTo(Buffer * dstBuffer,const VkBufferImageCopy & region)567 void Image::copyTo(Buffer *dstBuffer, const VkBufferImageCopy ®ion)
568 {
569 copy(dstBuffer, region, false);
570 }
571
copyFrom(Buffer * srcBuffer,const VkBufferImageCopy & region)572 void Image::copyFrom(Buffer *srcBuffer, const VkBufferImageCopy ®ion)
573 {
574 copy(srcBuffer, region, true);
575 }
576
getTexelPointer(const VkOffset3D & offset,const VkImageSubresourceLayers & subresource) const577 void *Image::getTexelPointer(const VkOffset3D &offset, const VkImageSubresourceLayers &subresource) const
578 {
579 VkImageAspectFlagBits aspect = static_cast<VkImageAspectFlagBits>(subresource.aspectMask);
580 return deviceMemory->getOffsetPointer(texelOffsetBytesInStorage(offset, subresource) +
581 getMemoryOffset(aspect, subresource.mipLevel, subresource.baseArrayLayer));
582 }
583
imageExtentInBlocks(const VkExtent3D & extent,VkImageAspectFlagBits aspect) const584 VkExtent3D Image::imageExtentInBlocks(const VkExtent3D &extent, VkImageAspectFlagBits aspect) const
585 {
586 VkExtent3D adjustedExtent = extent;
587 Format usedFormat = getFormat(aspect);
588 if(usedFormat.isCompressed())
589 {
590 // When using a compressed format, we use the block as the base unit, instead of the texel
591 int blockWidth = usedFormat.blockWidth();
592 int blockHeight = usedFormat.blockHeight();
593
594 // Mip level allocations will round up to the next block for compressed texture
595 adjustedExtent.width = ((adjustedExtent.width + blockWidth - 1) / blockWidth);
596 adjustedExtent.height = ((adjustedExtent.height + blockHeight - 1) / blockHeight);
597 }
598 return adjustedExtent;
599 }
600
imageOffsetInBlocks(const VkOffset3D & offset,VkImageAspectFlagBits aspect) const601 VkOffset3D Image::imageOffsetInBlocks(const VkOffset3D &offset, VkImageAspectFlagBits aspect) const
602 {
603 VkOffset3D adjustedOffset = offset;
604 Format usedFormat = getFormat(aspect);
605 if(usedFormat.isCompressed())
606 {
607 // When using a compressed format, we use the block as the base unit, instead of the texel
608 int blockWidth = usedFormat.blockWidth();
609 int blockHeight = usedFormat.blockHeight();
610
611 ASSERT(((offset.x % blockWidth) == 0) && ((offset.y % blockHeight) == 0)); // We can't offset within a block
612
613 adjustedOffset.x /= blockWidth;
614 adjustedOffset.y /= blockHeight;
615 }
616 return adjustedOffset;
617 }
618
bufferExtentInBlocks(const VkExtent2D & extent,const VkBufferImageCopy & region) const619 VkExtent2D Image::bufferExtentInBlocks(const VkExtent2D &extent, const VkBufferImageCopy ®ion) const
620 {
621 VkExtent2D adjustedExtent = extent;
622 VkImageAspectFlagBits aspect = static_cast<VkImageAspectFlagBits>(region.imageSubresource.aspectMask);
623 Format usedFormat = getFormat(aspect);
624 if(region.bufferRowLength != 0)
625 {
626 adjustedExtent.width = region.bufferRowLength;
627
628 if(usedFormat.isCompressed())
629 {
630 int blockWidth = usedFormat.blockWidth();
631 ASSERT((adjustedExtent.width % blockWidth) == 0);
632 adjustedExtent.width /= blockWidth;
633 }
634 }
635 if(region.bufferImageHeight != 0)
636 {
637 adjustedExtent.height = region.bufferImageHeight;
638
639 if(usedFormat.isCompressed())
640 {
641 int blockHeight = usedFormat.blockHeight();
642 ASSERT((adjustedExtent.height % blockHeight) == 0);
643 adjustedExtent.height /= blockHeight;
644 }
645 }
646 return adjustedExtent;
647 }
648
borderSize() const649 int Image::borderSize() const
650 {
651 // We won't add a border to compressed cube textures, we'll add it when we decompress the texture
652 return (isCube() && !format.isCompressed()) ? 1 : 0;
653 }
654
texelOffsetBytesInStorage(const VkOffset3D & offset,const VkImageSubresourceLayers & subresource) const655 VkDeviceSize Image::texelOffsetBytesInStorage(const VkOffset3D &offset, const VkImageSubresourceLayers &subresource) const
656 {
657 VkImageAspectFlagBits aspect = static_cast<VkImageAspectFlagBits>(subresource.aspectMask);
658 VkOffset3D adjustedOffset = imageOffsetInBlocks(offset, aspect);
659 int border = borderSize();
660 return adjustedOffset.z * slicePitchBytes(aspect, subresource.mipLevel) +
661 (adjustedOffset.y + border) * rowPitchBytes(aspect, subresource.mipLevel) +
662 (adjustedOffset.x + border) * getFormat(aspect).bytesPerBlock();
663 }
664
getMipLevelExtent(VkImageAspectFlagBits aspect,uint32_t mipLevel) const665 VkExtent3D Image::getMipLevelExtent(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
666 {
667 VkExtent3D mipLevelExtent;
668 mipLevelExtent.width = extent.width >> mipLevel;
669 mipLevelExtent.height = extent.height >> mipLevel;
670 mipLevelExtent.depth = extent.depth >> mipLevel;
671
672 if(mipLevelExtent.width == 0) { mipLevelExtent.width = 1; }
673 if(mipLevelExtent.height == 0) { mipLevelExtent.height = 1; }
674 if(mipLevelExtent.depth == 0) { mipLevelExtent.depth = 1; }
675
676 switch(aspect)
677 {
678 case VK_IMAGE_ASPECT_COLOR_BIT:
679 case VK_IMAGE_ASPECT_DEPTH_BIT:
680 case VK_IMAGE_ASPECT_STENCIL_BIT:
681 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.
682 break;
683 case VK_IMAGE_ASPECT_PLANE_1_BIT:
684 case VK_IMAGE_ASPECT_PLANE_2_BIT:
685 switch(format)
686 {
687 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
688 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
689 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."
690 // Vulkan 1.1 Table 31. Plane Format Compatibility Table:
691 // Half-resolution U and V planes.
692 mipLevelExtent.width /= 2;
693 mipLevelExtent.height /= 2;
694 break;
695 default:
696 UNSUPPORTED("format %d", int(format));
697 }
698 break;
699 default:
700 UNSUPPORTED("aspect %x", int(aspect));
701 }
702
703 return mipLevelExtent;
704 }
705
rowPitchBytes(VkImageAspectFlagBits aspect,uint32_t mipLevel) const706 int Image::rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
707 {
708 // Depth and Stencil pitch should be computed separately
709 ASSERT((aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) !=
710 (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
711
712 VkExtent3D mipLevelExtent = getMipLevelExtent(aspect, mipLevel);
713 Format usedFormat = getFormat(aspect);
714 if(usedFormat.isCompressed())
715 {
716 VkExtent3D extentInBlocks = imageExtentInBlocks(mipLevelExtent, aspect);
717 return extentInBlocks.width * usedFormat.bytesPerBlock();
718 }
719
720 return usedFormat.pitchB(mipLevelExtent.width, borderSize(), true);
721 }
722
slicePitchBytes(VkImageAspectFlagBits aspect,uint32_t mipLevel) const723 int Image::slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
724 {
725 // Depth and Stencil slice should be computed separately
726 ASSERT((aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) !=
727 (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
728
729 VkExtent3D mipLevelExtent = getMipLevelExtent(aspect, mipLevel);
730 Format usedFormat = getFormat(aspect);
731 if(usedFormat.isCompressed())
732 {
733 VkExtent3D extentInBlocks = imageExtentInBlocks(mipLevelExtent, aspect);
734 return extentInBlocks.height * extentInBlocks.width * usedFormat.bytesPerBlock();
735 }
736
737 return usedFormat.sliceB(mipLevelExtent.width, mipLevelExtent.height, borderSize(), true);
738 }
739
getFormat(VkImageAspectFlagBits aspect) const740 Format Image::getFormat(VkImageAspectFlagBits aspect) const
741 {
742 return format.getAspectFormat(aspect);
743 }
744
isCube() const745 bool Image::isCube() const
746 {
747 return (flags & VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) && (imageType == VK_IMAGE_TYPE_2D);
748 }
749
end() const750 uint8_t *Image::end() const
751 {
752 return reinterpret_cast<uint8_t *>(deviceMemory->getOffsetPointer(deviceMemory->getCommittedMemoryInBytes() + 1));
753 }
754
getMemoryOffset(VkImageAspectFlagBits aspect) const755 VkDeviceSize Image::getMemoryOffset(VkImageAspectFlagBits aspect) const
756 {
757 switch(format)
758 {
759 case VK_FORMAT_D16_UNORM_S8_UINT:
760 case VK_FORMAT_D24_UNORM_S8_UINT:
761 case VK_FORMAT_D32_SFLOAT_S8_UINT:
762 if(aspect == VK_IMAGE_ASPECT_STENCIL_BIT)
763 {
764 // Offset by depth buffer to get to stencil buffer
765 return memoryOffset + getStorageSize(VK_IMAGE_ASPECT_DEPTH_BIT);
766 }
767 break;
768
769 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
770 if(aspect == VK_IMAGE_ASPECT_PLANE_2_BIT)
771 {
772 return memoryOffset + getStorageSize(VK_IMAGE_ASPECT_PLANE_1_BIT) + getStorageSize(VK_IMAGE_ASPECT_PLANE_0_BIT);
773 }
774 // Fall through to 2PLANE case:
775 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
776 if(aspect == VK_IMAGE_ASPECT_PLANE_1_BIT)
777 {
778 return memoryOffset + getStorageSize(VK_IMAGE_ASPECT_PLANE_0_BIT);
779 }
780 else
781 {
782 ASSERT(aspect == VK_IMAGE_ASPECT_PLANE_0_BIT);
783
784 return memoryOffset;
785 }
786 break;
787
788 default:
789 break;
790 }
791
792 return memoryOffset;
793 }
794
getMemoryOffset(VkImageAspectFlagBits aspect,uint32_t mipLevel) const795 VkDeviceSize Image::getMemoryOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
796 {
797 VkDeviceSize offset = getMemoryOffset(aspect);
798 for(uint32_t i = 0; i < mipLevel; ++i)
799 {
800 offset += getMultiSampledLevelSize(aspect, i);
801 }
802 return offset;
803 }
804
getMemoryOffset(VkImageAspectFlagBits aspect,uint32_t mipLevel,uint32_t layer) const805 VkDeviceSize Image::getMemoryOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel, uint32_t layer) const
806 {
807 return layer * getLayerOffset(aspect, mipLevel) + getMemoryOffset(aspect, mipLevel);
808 }
809
getMipLevelSize(VkImageAspectFlagBits aspect,uint32_t mipLevel) const810 VkDeviceSize Image::getMipLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
811 {
812 return getMipLevelExtent(aspect, mipLevel).depth * slicePitchBytes(aspect, mipLevel);
813 }
814
getMultiSampledLevelSize(VkImageAspectFlagBits aspect,uint32_t mipLevel) const815 VkDeviceSize Image::getMultiSampledLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
816 {
817 return getMipLevelSize(aspect, mipLevel) * samples;
818 }
819
is3DSlice() const820 bool Image::is3DSlice() const
821 {
822 return ((imageType == VK_IMAGE_TYPE_3D) && (flags & VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT));
823 }
824
getLayerOffset(VkImageAspectFlagBits aspect,uint32_t mipLevel) const825 VkDeviceSize Image::getLayerOffset(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
826 {
827 if(is3DSlice())
828 {
829 // When the VkImageSubresourceRange structure is used to select a subset of the slices of a 3D
830 // image's mip level in order to create a 2D or 2D array image view of a 3D image created with
831 // VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT, baseArrayLayer and layerCount specify the first
832 // slice index and the number of slices to include in the created image view.
833 ASSERT(samples == VK_SAMPLE_COUNT_1_BIT);
834
835 // Offset to the proper slice of the 3D image's mip level
836 return slicePitchBytes(aspect, mipLevel);
837 }
838
839 return getLayerSize(aspect);
840 }
841
getLayerSize(VkImageAspectFlagBits aspect) const842 VkDeviceSize Image::getLayerSize(VkImageAspectFlagBits aspect) const
843 {
844 VkDeviceSize layerSize = 0;
845
846 for(uint32_t mipLevel = 0; mipLevel < mipLevels; ++mipLevel)
847 {
848 layerSize += getMultiSampledLevelSize(aspect, mipLevel);
849 }
850
851 return layerSize;
852 }
853
getStorageSize(VkImageAspectFlags aspectMask) const854 VkDeviceSize Image::getStorageSize(VkImageAspectFlags aspectMask) const
855 {
856 if((aspectMask & ~(VK_IMAGE_ASPECT_COLOR_BIT | VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT |
857 VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT | VK_IMAGE_ASPECT_PLANE_2_BIT)) != 0)
858 {
859 UNSUPPORTED("aspectMask %x", int(aspectMask));
860 }
861
862 VkDeviceSize storageSize = 0;
863
864 if(aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) storageSize += getLayerSize(VK_IMAGE_ASPECT_COLOR_BIT);
865 if(aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) storageSize += getLayerSize(VK_IMAGE_ASPECT_DEPTH_BIT);
866 if(aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) storageSize += getLayerSize(VK_IMAGE_ASPECT_STENCIL_BIT);
867 if(aspectMask & VK_IMAGE_ASPECT_PLANE_0_BIT) storageSize += getLayerSize(VK_IMAGE_ASPECT_PLANE_0_BIT);
868 if(aspectMask & VK_IMAGE_ASPECT_PLANE_1_BIT) storageSize += getLayerSize(VK_IMAGE_ASPECT_PLANE_1_BIT);
869 if(aspectMask & VK_IMAGE_ASPECT_PLANE_2_BIT) storageSize += getLayerSize(VK_IMAGE_ASPECT_PLANE_2_BIT);
870
871 return arrayLayers * storageSize;
872 }
873
getSampledImage(const vk::Format & imageViewFormat) const874 const Image *Image::getSampledImage(const vk::Format &imageViewFormat) const
875 {
876 bool isImageViewCompressed = imageViewFormat.isCompressed();
877 if(decompressedImage && !isImageViewCompressed)
878 {
879 ASSERT(flags & VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT);
880 ASSERT(format.bytesPerBlock() == imageViewFormat.bytesPerBlock());
881 }
882 // If the ImageView's format is compressed, then we do need to decompress the image so that
883 // it may be sampled properly by texture sampling functions, which don't support compressed
884 // textures. If the ImageView's format is NOT compressed, then we reinterpret cast the
885 // compressed image into the ImageView's format, so we must return the compressed image as is.
886 return (decompressedImage && isImageViewCompressed) ? decompressedImage : this;
887 }
888
blit(Image * dstImage,const VkImageBlit & region,VkFilter filter) const889 void Image::blit(Image *dstImage, const VkImageBlit ®ion, VkFilter filter) const
890 {
891 device->getBlitter()->blit(this, dstImage, region, filter);
892 }
893
blitToBuffer(VkImageSubresourceLayers subresource,VkOffset3D offset,VkExtent3D extent,uint8_t * dst,int bufferRowPitch,int bufferSlicePitch) const894 void Image::blitToBuffer(VkImageSubresourceLayers subresource, VkOffset3D offset, VkExtent3D extent, uint8_t *dst, int bufferRowPitch, int bufferSlicePitch) const
895 {
896 device->getBlitter()->blitToBuffer(this, subresource, offset, extent, dst, bufferRowPitch, bufferSlicePitch);
897 }
898
resolve(Image * dstImage,const VkImageResolve & region) const899 void Image::resolve(Image *dstImage, const VkImageResolve ®ion) const
900 {
901 VkImageBlit blitRegion;
902
903 blitRegion.srcOffsets[0] = blitRegion.srcOffsets[1] = region.srcOffset;
904 blitRegion.srcOffsets[1].x += region.extent.width;
905 blitRegion.srcOffsets[1].y += region.extent.height;
906 blitRegion.srcOffsets[1].z += region.extent.depth;
907
908 blitRegion.dstOffsets[0] = blitRegion.dstOffsets[1] = region.dstOffset;
909 blitRegion.dstOffsets[1].x += region.extent.width;
910 blitRegion.dstOffsets[1].y += region.extent.height;
911 blitRegion.dstOffsets[1].z += region.extent.depth;
912
913 blitRegion.srcSubresource = region.srcSubresource;
914 blitRegion.dstSubresource = region.dstSubresource;
915
916 device->getBlitter()->blit(this, dstImage, blitRegion, VK_FILTER_NEAREST);
917 }
918
getClearFormat() const919 VkFormat Image::getClearFormat() const
920 {
921 // Set the proper format for the clear value, as described here:
922 // https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#clears-values
923 if(format.isSignedUnnormalizedInteger())
924 {
925 return VK_FORMAT_R32G32B32A32_SINT;
926 }
927 else if(format.isUnsignedUnnormalizedInteger())
928 {
929 return VK_FORMAT_R32G32B32A32_UINT;
930 }
931
932 return VK_FORMAT_R32G32B32A32_SFLOAT;
933 }
934
getLastLayerIndex(const VkImageSubresourceRange & subresourceRange) const935 uint32_t Image::getLastLayerIndex(const VkImageSubresourceRange &subresourceRange) const
936 {
937 return ((subresourceRange.layerCount == VK_REMAINING_ARRAY_LAYERS) ? arrayLayers : (subresourceRange.baseArrayLayer + subresourceRange.layerCount)) - 1;
938 }
939
getLastMipLevel(const VkImageSubresourceRange & subresourceRange) const940 uint32_t Image::getLastMipLevel(const VkImageSubresourceRange &subresourceRange) const
941 {
942 return ((subresourceRange.levelCount == VK_REMAINING_MIP_LEVELS) ? mipLevels : (subresourceRange.baseMipLevel + subresourceRange.levelCount)) - 1;
943 }
944
clear(void * pixelData,VkFormat pixelFormat,const vk::Format & viewFormat,const VkImageSubresourceRange & subresourceRange,const VkRect2D & renderArea)945 void Image::clear(void *pixelData, VkFormat pixelFormat, const vk::Format &viewFormat, const VkImageSubresourceRange &subresourceRange, const VkRect2D &renderArea)
946 {
947 device->getBlitter()->clear(pixelData, pixelFormat, this, viewFormat, subresourceRange, &renderArea);
948 }
949
clear(const VkClearColorValue & color,const VkImageSubresourceRange & subresourceRange)950 void Image::clear(const VkClearColorValue &color, const VkImageSubresourceRange &subresourceRange)
951 {
952 ASSERT(subresourceRange.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT);
953
954 device->getBlitter()->clear((void *)color.float32, getClearFormat(), this, format, subresourceRange);
955 }
956
clear(const VkClearDepthStencilValue & color,const VkImageSubresourceRange & subresourceRange)957 void Image::clear(const VkClearDepthStencilValue &color, const VkImageSubresourceRange &subresourceRange)
958 {
959 ASSERT((subresourceRange.aspectMask & ~(VK_IMAGE_ASPECT_DEPTH_BIT |
960 VK_IMAGE_ASPECT_STENCIL_BIT)) == 0);
961
962 if(subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT)
963 {
964 VkImageSubresourceRange depthSubresourceRange = subresourceRange;
965 depthSubresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
966 device->getBlitter()->clear((void *)(&color.depth), VK_FORMAT_D32_SFLOAT, this, format, depthSubresourceRange);
967 }
968
969 if(subresourceRange.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)
970 {
971 VkImageSubresourceRange stencilSubresourceRange = subresourceRange;
972 stencilSubresourceRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
973 device->getBlitter()->clear((void *)(&color.stencil), VK_FORMAT_S8_UINT, this, format, stencilSubresourceRange);
974 }
975 }
976
clear(const VkClearValue & clearValue,const vk::Format & viewFormat,const VkRect2D & renderArea,const VkImageSubresourceRange & subresourceRange)977 void Image::clear(const VkClearValue &clearValue, const vk::Format &viewFormat, const VkRect2D &renderArea, const VkImageSubresourceRange &subresourceRange)
978 {
979 ASSERT((subresourceRange.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT) ||
980 (subresourceRange.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT |
981 VK_IMAGE_ASPECT_STENCIL_BIT)));
982
983 if(subresourceRange.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT)
984 {
985 clear((void *)(clearValue.color.float32), getClearFormat(), viewFormat, subresourceRange, renderArea);
986 }
987 else
988 {
989 if(subresourceRange.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT)
990 {
991 VkImageSubresourceRange depthSubresourceRange = subresourceRange;
992 depthSubresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
993 clear((void *)(&clearValue.depthStencil.depth), VK_FORMAT_D32_SFLOAT, viewFormat, depthSubresourceRange, renderArea);
994 }
995
996 if(subresourceRange.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)
997 {
998 VkImageSubresourceRange stencilSubresourceRange = subresourceRange;
999 stencilSubresourceRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
1000 clear((void *)(&clearValue.depthStencil.stencil), VK_FORMAT_S8_UINT, viewFormat, stencilSubresourceRange, renderArea);
1001 }
1002 }
1003 }
1004
prepareForSampling(const VkImageSubresourceRange & subresourceRange)1005 void Image::prepareForSampling(const VkImageSubresourceRange &subresourceRange)
1006 {
1007 if(decompressedImage)
1008 {
1009 switch(format)
1010 {
1011 case VK_FORMAT_EAC_R11_UNORM_BLOCK:
1012 case VK_FORMAT_EAC_R11_SNORM_BLOCK:
1013 case VK_FORMAT_EAC_R11G11_UNORM_BLOCK:
1014 case VK_FORMAT_EAC_R11G11_SNORM_BLOCK:
1015 case VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK:
1016 case VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK:
1017 case VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK:
1018 case VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK:
1019 case VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK:
1020 case VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK:
1021 decodeETC2(subresourceRange);
1022 break;
1023 case VK_FORMAT_BC1_RGB_UNORM_BLOCK:
1024 case VK_FORMAT_BC1_RGB_SRGB_BLOCK:
1025 case VK_FORMAT_BC1_RGBA_UNORM_BLOCK:
1026 case VK_FORMAT_BC1_RGBA_SRGB_BLOCK:
1027 case VK_FORMAT_BC2_UNORM_BLOCK:
1028 case VK_FORMAT_BC2_SRGB_BLOCK:
1029 case VK_FORMAT_BC3_UNORM_BLOCK:
1030 case VK_FORMAT_BC3_SRGB_BLOCK:
1031 case VK_FORMAT_BC4_UNORM_BLOCK:
1032 case VK_FORMAT_BC4_SNORM_BLOCK:
1033 case VK_FORMAT_BC5_UNORM_BLOCK:
1034 case VK_FORMAT_BC5_SNORM_BLOCK:
1035 case VK_FORMAT_BC6H_UFLOAT_BLOCK:
1036 case VK_FORMAT_BC6H_SFLOAT_BLOCK:
1037 case VK_FORMAT_BC7_UNORM_BLOCK:
1038 case VK_FORMAT_BC7_SRGB_BLOCK:
1039 decodeBC(subresourceRange);
1040 break;
1041 case VK_FORMAT_ASTC_4x4_UNORM_BLOCK:
1042 case VK_FORMAT_ASTC_5x4_UNORM_BLOCK:
1043 case VK_FORMAT_ASTC_5x5_UNORM_BLOCK:
1044 case VK_FORMAT_ASTC_6x5_UNORM_BLOCK:
1045 case VK_FORMAT_ASTC_6x6_UNORM_BLOCK:
1046 case VK_FORMAT_ASTC_8x5_UNORM_BLOCK:
1047 case VK_FORMAT_ASTC_8x6_UNORM_BLOCK:
1048 case VK_FORMAT_ASTC_8x8_UNORM_BLOCK:
1049 case VK_FORMAT_ASTC_10x5_UNORM_BLOCK:
1050 case VK_FORMAT_ASTC_10x6_UNORM_BLOCK:
1051 case VK_FORMAT_ASTC_10x8_UNORM_BLOCK:
1052 case VK_FORMAT_ASTC_10x10_UNORM_BLOCK:
1053 case VK_FORMAT_ASTC_12x10_UNORM_BLOCK:
1054 case VK_FORMAT_ASTC_12x12_UNORM_BLOCK:
1055 case VK_FORMAT_ASTC_4x4_SRGB_BLOCK:
1056 case VK_FORMAT_ASTC_5x4_SRGB_BLOCK:
1057 case VK_FORMAT_ASTC_5x5_SRGB_BLOCK:
1058 case VK_FORMAT_ASTC_6x5_SRGB_BLOCK:
1059 case VK_FORMAT_ASTC_6x6_SRGB_BLOCK:
1060 case VK_FORMAT_ASTC_8x5_SRGB_BLOCK:
1061 case VK_FORMAT_ASTC_8x6_SRGB_BLOCK:
1062 case VK_FORMAT_ASTC_8x8_SRGB_BLOCK:
1063 case VK_FORMAT_ASTC_10x5_SRGB_BLOCK:
1064 case VK_FORMAT_ASTC_10x6_SRGB_BLOCK:
1065 case VK_FORMAT_ASTC_10x8_SRGB_BLOCK:
1066 case VK_FORMAT_ASTC_10x10_SRGB_BLOCK:
1067 case VK_FORMAT_ASTC_12x10_SRGB_BLOCK:
1068 case VK_FORMAT_ASTC_12x12_SRGB_BLOCK:
1069 case VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT:
1070 case VK_FORMAT_ASTC_5x4_SFLOAT_BLOCK_EXT:
1071 case VK_FORMAT_ASTC_5x5_SFLOAT_BLOCK_EXT:
1072 case VK_FORMAT_ASTC_6x5_SFLOAT_BLOCK_EXT:
1073 case VK_FORMAT_ASTC_6x6_SFLOAT_BLOCK_EXT:
1074 case VK_FORMAT_ASTC_8x5_SFLOAT_BLOCK_EXT:
1075 case VK_FORMAT_ASTC_8x6_SFLOAT_BLOCK_EXT:
1076 case VK_FORMAT_ASTC_8x8_SFLOAT_BLOCK_EXT:
1077 case VK_FORMAT_ASTC_10x5_SFLOAT_BLOCK_EXT:
1078 case VK_FORMAT_ASTC_10x6_SFLOAT_BLOCK_EXT:
1079 case VK_FORMAT_ASTC_10x8_SFLOAT_BLOCK_EXT:
1080 case VK_FORMAT_ASTC_10x10_SFLOAT_BLOCK_EXT:
1081 case VK_FORMAT_ASTC_12x10_SFLOAT_BLOCK_EXT:
1082 case VK_FORMAT_ASTC_12x12_SFLOAT_BLOCK_EXT:
1083 decodeASTC(subresourceRange);
1084 break;
1085 default:
1086 break;
1087 }
1088 }
1089
1090 if(isCube() && (arrayLayers >= 6))
1091 {
1092 VkImageSubresourceLayers subresourceLayers = {
1093 subresourceRange.aspectMask,
1094 subresourceRange.baseMipLevel,
1095 subresourceRange.baseArrayLayer,
1096 6
1097 };
1098
1099 // Update the borders of all the groups of 6 layers that can be part of a cubemaps but don't
1100 // touch leftover layers that cannot be part of cubemaps.
1101 uint32_t lastMipLevel = getLastMipLevel(subresourceRange);
1102 for(; subresourceLayers.mipLevel <= lastMipLevel; subresourceLayers.mipLevel++)
1103 {
1104 for(subresourceLayers.baseArrayLayer = 0;
1105 subresourceLayers.baseArrayLayer < arrayLayers - 5;
1106 subresourceLayers.baseArrayLayer += 6)
1107 {
1108 device->getBlitter()->updateBorders(decompressedImage ? decompressedImage : this, subresourceLayers);
1109 }
1110 }
1111 }
1112 }
1113
decodeETC2(const VkImageSubresourceRange & subresourceRange) const1114 void Image::decodeETC2(const VkImageSubresourceRange &subresourceRange) const
1115 {
1116 ASSERT(decompressedImage);
1117
1118 ETC_Decoder::InputType inputType = GetInputType(format);
1119
1120 uint32_t lastLayer = getLastLayerIndex(subresourceRange);
1121 uint32_t lastMipLevel = getLastMipLevel(subresourceRange);
1122
1123 int bytes = decompressedImage->format.bytes();
1124 bool fakeAlpha = (format == VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK) || (format == VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK);
1125 size_t sizeToWrite = 0;
1126
1127 VkImageSubresourceLayers subresourceLayers = { subresourceRange.aspectMask, subresourceRange.baseMipLevel, subresourceRange.baseArrayLayer, 1 };
1128 for(; subresourceLayers.baseArrayLayer <= lastLayer; subresourceLayers.baseArrayLayer++)
1129 {
1130 for(; subresourceLayers.mipLevel <= lastMipLevel; subresourceLayers.mipLevel++)
1131 {
1132 VkExtent3D mipLevelExtent = getMipLevelExtent(static_cast<VkImageAspectFlagBits>(subresourceLayers.aspectMask), subresourceLayers.mipLevel);
1133
1134 int pitchB = decompressedImage->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, subresourceLayers.mipLevel);
1135
1136 if(fakeAlpha)
1137 {
1138 // To avoid overflow in case of cube textures, which are offset in memory to account for the border,
1139 // compute the size from the first pixel to the last pixel, excluding any padding or border before
1140 // the first pixel or after the last pixel.
1141 sizeToWrite = ((mipLevelExtent.height - 1) * pitchB) + (mipLevelExtent.width * bytes);
1142 }
1143
1144 for(int32_t depth = 0; depth < static_cast<int32_t>(mipLevelExtent.depth); depth++)
1145 {
1146 uint8_t *source = static_cast<uint8_t *>(getTexelPointer({ 0, 0, depth }, subresourceLayers));
1147 uint8_t *dest = static_cast<uint8_t *>(decompressedImage->getTexelPointer({ 0, 0, depth }, subresourceLayers));
1148
1149 if(fakeAlpha)
1150 {
1151 ASSERT((dest + sizeToWrite) < decompressedImage->end());
1152 memset(dest, 0xFF, sizeToWrite);
1153 }
1154
1155 ETC_Decoder::Decode(source, dest, mipLevelExtent.width, mipLevelExtent.height,
1156 pitchB, bytes, inputType);
1157 }
1158 }
1159 }
1160 }
1161
decodeBC(const VkImageSubresourceRange & subresourceRange) const1162 void Image::decodeBC(const VkImageSubresourceRange &subresourceRange) const
1163 {
1164 ASSERT(decompressedImage);
1165
1166 int n = GetBCn(format);
1167 int noAlphaU = GetNoAlphaOrUnsigned(format);
1168
1169 uint32_t lastLayer = getLastLayerIndex(subresourceRange);
1170 uint32_t lastMipLevel = getLastMipLevel(subresourceRange);
1171
1172 int bytes = decompressedImage->format.bytes();
1173
1174 VkImageSubresourceLayers subresourceLayers = { subresourceRange.aspectMask, subresourceRange.baseMipLevel, subresourceRange.baseArrayLayer, 1 };
1175 for(; subresourceLayers.baseArrayLayer <= lastLayer; subresourceLayers.baseArrayLayer++)
1176 {
1177 for(; subresourceLayers.mipLevel <= lastMipLevel; subresourceLayers.mipLevel++)
1178 {
1179 VkExtent3D mipLevelExtent = getMipLevelExtent(static_cast<VkImageAspectFlagBits>(subresourceLayers.aspectMask), subresourceLayers.mipLevel);
1180
1181 int pitchB = decompressedImage->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, subresourceLayers.mipLevel);
1182
1183 for(int32_t depth = 0; depth < static_cast<int32_t>(mipLevelExtent.depth); depth++)
1184 {
1185 uint8_t *source = static_cast<uint8_t *>(getTexelPointer({ 0, 0, depth }, subresourceLayers));
1186 uint8_t *dest = static_cast<uint8_t *>(decompressedImage->getTexelPointer({ 0, 0, depth }, subresourceLayers));
1187
1188 BC_Decoder::Decode(source, dest, mipLevelExtent.width, mipLevelExtent.height,
1189 pitchB, bytes, n, noAlphaU);
1190 }
1191 }
1192 }
1193 }
1194
decodeASTC(const VkImageSubresourceRange & subresourceRange) const1195 void Image::decodeASTC(const VkImageSubresourceRange &subresourceRange) const
1196 {
1197 ASSERT(decompressedImage);
1198
1199 int xBlockSize = format.blockWidth();
1200 int yBlockSize = format.blockHeight();
1201 int zBlockSize = 1;
1202 bool isUnsigned = format.isUnsignedComponent(0);
1203
1204 uint32_t lastLayer = getLastLayerIndex(subresourceRange);
1205 uint32_t lastMipLevel = getLastMipLevel(subresourceRange);
1206
1207 int bytes = decompressedImage->format.bytes();
1208
1209 VkImageSubresourceLayers subresourceLayers = { subresourceRange.aspectMask, subresourceRange.baseMipLevel, subresourceRange.baseArrayLayer, 1 };
1210 for(; subresourceLayers.baseArrayLayer <= lastLayer; subresourceLayers.baseArrayLayer++)
1211 {
1212 for(; subresourceLayers.mipLevel <= lastMipLevel; subresourceLayers.mipLevel++)
1213 {
1214 VkExtent3D mipLevelExtent = getMipLevelExtent(static_cast<VkImageAspectFlagBits>(subresourceLayers.aspectMask), subresourceLayers.mipLevel);
1215
1216 int xblocks = (mipLevelExtent.width + xBlockSize - 1) / xBlockSize;
1217 int yblocks = (mipLevelExtent.height + yBlockSize - 1) / yBlockSize;
1218 int zblocks = (zBlockSize > 1) ? (mipLevelExtent.depth + zBlockSize - 1) / zBlockSize : 1;
1219
1220 if(xblocks <= 0 || yblocks <= 0 || zblocks <= 0)
1221 {
1222 continue;
1223 }
1224
1225 int pitchB = decompressedImage->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, subresourceLayers.mipLevel);
1226 int sliceB = decompressedImage->slicePitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, subresourceLayers.mipLevel);
1227
1228 for(int32_t depth = 0; depth < static_cast<int32_t>(mipLevelExtent.depth); depth++)
1229 {
1230 uint8_t *source = static_cast<uint8_t *>(getTexelPointer({ 0, 0, depth }, subresourceLayers));
1231 uint8_t *dest = static_cast<uint8_t *>(decompressedImage->getTexelPointer({ 0, 0, depth }, subresourceLayers));
1232
1233 ASTC_Decoder::Decode(source, dest, mipLevelExtent.width, mipLevelExtent.height, mipLevelExtent.depth, bytes, pitchB, sliceB,
1234 xBlockSize, yBlockSize, zBlockSize, xblocks, yblocks, zblocks, isUnsigned);
1235 }
1236 }
1237 }
1238 }
1239
1240 } // namespace vk
1241