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