Refactor obtaining format aspect info

Aspect info is part of the format, so shouldn't be queried from an Image
utility function.

Bug: b/132437008
Change-Id: I87d632231a810cece3d1ea579e9df63b6b80a714
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/31611
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
diff --git a/src/Device/Blitter.cpp b/src/Device/Blitter.cpp
index 2230243..a97fc3d 100644
--- a/src/Device/Blitter.cpp
+++ b/src/Device/Blitter.cpp
@@ -40,7 +40,7 @@
 	void Blitter::clear(void *pixel, vk::Format format, vk::Image *dest, const vk::Format& viewFormat, const VkImageSubresourceRange& subresourceRange, const VkRect2D* renderArea)
 	{
 		VkImageAspectFlagBits aspect = static_cast<VkImageAspectFlagBits>(subresourceRange.aspectMask);
-		vk::Format dstFormat = vk::Image::GetFormat(viewFormat, aspect);
+		vk::Format dstFormat = viewFormat.getAspectFormat(aspect);
 		if(dstFormat == VK_FORMAT_UNDEFINED)
 		{
 			return;
diff --git a/src/Pipeline/SamplerCore.cpp b/src/Pipeline/SamplerCore.cpp
index e4ba3e1..cf11440 100644
--- a/src/Pipeline/SamplerCore.cpp
+++ b/src/Pipeline/SamplerCore.cpp
@@ -147,7 +147,7 @@
 			}
 		}
 
-		bool force32BitFiltering = state.highPrecisionFiltering && !hasYuvFormat() && (state.textureFilter != FILTER_POINT);
+		bool force32BitFiltering = state.highPrecisionFiltering && !isYcbcrFormat() && (state.textureFilter != FILTER_POINT);
 		bool seamlessCube = (state.addressingModeU == ADDRESSING_SEAMLESS);
 		bool use32BitFiltering = hasFloatTexture() || hasUnnormalizedIntegerTexture() || force32BitFiltering ||
 		                         seamlessCube || state.unnormalizedCoordinates || state.compareEnable || state.largeTexture ||
@@ -1601,7 +1601,7 @@
 		UInt index[4];
 		computeIndices(index, uuuu, vvvv, wwww, offset, mipmap, function);
 
-		if(hasYuvFormat())
+		if(isYcbcrFormat())
 		{
 			// Generic YPbPr to RGB transformation
 			// R = Y                               +           2 * (1 - Kr) * Pr
@@ -1844,7 +1844,7 @@
 		}
 		else
 		{
-			ASSERT(!hasYuvFormat());
+			ASSERT(!isYcbcrFormat());
 
 			Vector4s cs = sampleTexel(index, buffer);
 
@@ -1989,7 +1989,7 @@
 		{
 			buffer[0] = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap,buffer[0]));
 
-			if(hasYuvFormat())
+			if(isYcbcrFormat())
 			{
 				buffer[1] = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap,buffer[1]));
 				buffer[2] = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap,buffer[2]));
@@ -2356,9 +2356,9 @@
 		return state.textureFormat.has32bitIntegerTextureComponents();
 	}
 
-	bool SamplerCore::hasYuvFormat() const
+	bool SamplerCore::isYcbcrFormat() const
 	{
-		return state.textureFormat.hasYuvFormat();
+		return state.textureFormat.isYcbcrFormat();
 	}
 
 	bool SamplerCore::isRGBComponent(int component) const
diff --git a/src/Pipeline/SamplerCore.hpp b/src/Pipeline/SamplerCore.hpp
index 068139b..0b75bf1 100644
--- a/src/Pipeline/SamplerCore.hpp
+++ b/src/Pipeline/SamplerCore.hpp
@@ -107,7 +107,7 @@
 		bool has8bitTextureComponents() const;
 		bool has16bitTextureComponents() const;
 		bool has32bitIntegerTextureComponents() const;
-		bool hasYuvFormat() const;
+		bool isYcbcrFormat() const;
 		bool isRGBComponent(int component) const;
 		bool borderModeActive() const;
 		VkComponentSwizzle gatherSwizzle() const;
diff --git a/src/Vulkan/VkFormat.cpp b/src/Vulkan/VkFormat.cpp
index 152afdd..1d5ed01 100644
--- a/src/Vulkan/VkFormat.cpp
+++ b/src/Vulkan/VkFormat.cpp
@@ -86,6 +86,57 @@
 	return isSignedNonNormalizedInteger() || isUnsignedNonNormalizedInteger();
 }
 
+VkImageAspectFlags Format::getAspects() const
+{
+	// TODO: probably just flatten this out to a full format list, and alter
+	// isDepth / isStencil etc to check for their aspect
+
+	VkImageAspectFlags aspects = 0;
+	if (isDepth()) aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
+	if (isStencil()) aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
+
+	// TODO: YCbCr planar formats have different aspects
+
+	// Anything else is "color".
+	if (!aspects) aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
+	return aspects;
+}
+
+Format Format::getAspectFormat(VkImageAspectFlags aspect) const
+{
+	switch(aspect)
+	{
+	case VK_IMAGE_ASPECT_DEPTH_BIT:
+		switch(format)
+		{
+		case VK_FORMAT_D16_UNORM_S8_UINT:
+			return VK_FORMAT_D16_UNORM;
+		case VK_FORMAT_D24_UNORM_S8_UINT:
+			return VK_FORMAT_X8_D24_UNORM_PACK32; // FIXME: This will allocate an extra byte per pixel
+		case VK_FORMAT_D32_SFLOAT_S8_UINT:
+			return VK_FORMAT_D32_SFLOAT;
+		default:
+			break;
+		}
+		break;
+	case VK_IMAGE_ASPECT_STENCIL_BIT:
+		switch(format)
+		{
+		case VK_FORMAT_D16_UNORM_S8_UINT:
+		case VK_FORMAT_D24_UNORM_S8_UINT:
+		case VK_FORMAT_D32_SFLOAT_S8_UINT:
+			return VK_FORMAT_S8_UINT;
+		default:
+			break;
+		}
+		break;
+	default:
+		break;
+	}
+
+	return format;
+}
+
 bool Format::isStencil() const
 {
 	switch(format)
@@ -306,6 +357,18 @@
 	return false;
 }
 
+bool Format::isYcbcrFormat() const
+{
+	switch(format)
+	{
+	case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
+	case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
+		return true;
+	default:
+		return false;
+	}
+}
+
 bool Format::isCompressed() const
 {
 	switch(format)
@@ -1983,69 +2046,6 @@
 	return false;
 }
 
-bool Format::hasYuvFormat() const
-{
-	switch(format)
-	{
-	case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
-		return true;
-	case VK_FORMAT_A1R5G5B5_UNORM_PACK16:
-	case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
-	case VK_FORMAT_R5G6B5_UNORM_PACK16:
-	case VK_FORMAT_R8_SNORM:
-	case VK_FORMAT_R8G8_SNORM:
-	case VK_FORMAT_R8G8B8A8_SNORM:
-	case VK_FORMAT_R8_SINT:
-	case VK_FORMAT_R8_UINT:
-	case VK_FORMAT_R8G8_SINT:
-	case VK_FORMAT_R8G8_UINT:
-	case VK_FORMAT_R8G8B8A8_SINT:
-	case VK_FORMAT_R8G8B8A8_UINT:
-	case VK_FORMAT_R32_SINT:
-	case VK_FORMAT_R32_UINT:
-	case VK_FORMAT_R32G32_SINT:
-	case VK_FORMAT_R32G32_UINT:
-	case VK_FORMAT_R32G32B32A32_SINT:
-	case VK_FORMAT_R32G32B32A32_UINT:
-	case VK_FORMAT_R8G8_UNORM:
-	case VK_FORMAT_B8G8R8_UNORM:
-	case VK_FORMAT_B8G8R8A8_UNORM:
-	case VK_FORMAT_R8G8B8A8_UNORM:
-	case VK_FORMAT_B8G8R8_SRGB:
-	case VK_FORMAT_R8G8B8A8_SRGB:
-	case VK_FORMAT_B8G8R8A8_SRGB:
-	case VK_FORMAT_R32_SFLOAT:
-	case VK_FORMAT_R32G32_SFLOAT:
-	case VK_FORMAT_R32G32B32A32_SFLOAT:
-	case VK_FORMAT_R8_UNORM:
-	case VK_FORMAT_R16_UNORM:
-	case VK_FORMAT_R16_SNORM:
-	case VK_FORMAT_R16G16_UNORM:
-	case VK_FORMAT_R16G16_SNORM:
-	case VK_FORMAT_R16G16B16A16_UNORM:
-	case VK_FORMAT_R16_SINT:
-	case VK_FORMAT_R16_UINT:
-	case VK_FORMAT_R16_SFLOAT:
-	case VK_FORMAT_R16G16_SINT:
-	case VK_FORMAT_R16G16_UINT:
-	case VK_FORMAT_R16G16_SFLOAT:
-	case VK_FORMAT_R16G16B16A16_SINT:
-	case VK_FORMAT_R16G16B16A16_UINT:
-	case VK_FORMAT_R16G16B16A16_SFLOAT:
-	case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
-	case VK_FORMAT_A2B10G10R10_UINT_PACK32:
-	case VK_FORMAT_E5B9G9R9_UFLOAT_PACK32:
-	case VK_FORMAT_D32_SFLOAT:
-	case VK_FORMAT_D16_UNORM:
-	case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
-		return false;
-	default:
-		UNIMPLEMENTED("Format: %d", int(format));
-	}
-
-	return false;
-}
-
 bool Format::isRGBComponent(int component) const
 {
 	switch(format)
diff --git a/src/Vulkan/VkFormat.h b/src/Vulkan/VkFormat.h
index 7184c34..15385f4 100644
--- a/src/Vulkan/VkFormat.h
+++ b/src/Vulkan/VkFormat.h
@@ -36,13 +36,15 @@
 	bool isUnsignedNonNormalizedInteger() const;
 	bool isNonNormalizedInteger() const;
 
+	VkImageAspectFlags getAspects() const;
+	Format getAspectFormat(VkImageAspectFlags aspect) const;
 	bool isStencil() const;
 	bool isDepth() const;
 	bool hasQuadLayout() const;
 	VkFormat getNonQuadLayoutFormat() const;
-
 	bool isSRGBformat() const;
 	bool isFloatFormat() const;
+	bool isYcbcrFormat() const;
 
 	bool isCompatible(const Format& other) const;
 	bool isCompressed() const;
@@ -65,7 +67,6 @@
 	bool has8bitTextureComponents() const;
 	bool has16bitTextureComponents() const;
 	bool has32bitIntegerTextureComponents() const;
-	bool hasYuvFormat() const;
 	bool isRGBComponent(int component) const;
 
 private:
diff --git a/src/Vulkan/VkImage.cpp b/src/Vulkan/VkImage.cpp
index cf44d68..d56070b 100644
--- a/src/Vulkan/VkImage.cpp
+++ b/src/Vulkan/VkImage.cpp
@@ -22,22 +22,6 @@
 
 namespace
 {
-	VkImageAspectFlags GetAspects(vk::Format format)
-	{
-		// TODO: probably just flatten this out to a full format list, and alter
-		// isDepth / isStencil etc to check for their aspect
-
-		VkImageAspectFlags aspects = 0;
-		if (format.isDepth()) aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
-		if (format.isStencil()) aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
-
-		// TODO: YCbCr planar formats have different aspects
-
-		// Anything else is "color".
-		if (!aspects) aspects |= VK_IMAGE_ASPECT_COLOR_BIT;
-		return aspects;
-	}
-
 	ETC_Decoder::InputType GetInputType(const vk::Format& format)
 	{
 		switch(format)
@@ -108,8 +92,8 @@
 	VkMemoryRequirements memoryRequirements;
 	memoryRequirements.alignment = vk::REQUIRED_MEMORY_ALIGNMENT;
 	memoryRequirements.memoryTypeBits = vk::MEMORY_TYPE_GENERIC_BIT;
-	memoryRequirements.size = getStorageSize(GetAspects(format)) +
-	                          (decompressedImage ? decompressedImage->getStorageSize(GetAspects(decompressedImage->format)) : 0);
+	memoryRequirements.size = getStorageSize(format.getAspects()) +
+	                          (decompressedImage ? decompressedImage->getStorageSize(decompressedImage->format.getAspects()) : 0);
 	return memoryRequirements;
 }
 
@@ -120,7 +104,7 @@
 	if(decompressedImage)
 	{
 		decompressedImage->deviceMemory = deviceMemory;
-		decompressedImage->memoryOffset = memoryOffset + getStorageSize(GetAspects(format));
+		decompressedImage->memoryOffset = memoryOffset + getStorageSize(format.getAspects());
 	}
 }
 
@@ -569,42 +553,7 @@
 
 Format Image::getFormat(VkImageAspectFlagBits aspect) const
 {
-	return GetFormat(format, aspect);
-}
-
-Format Image::GetFormat(const vk::Format& format, VkImageAspectFlagBits aspect)
-{
-	switch(aspect)
-	{
-	case VK_IMAGE_ASPECT_DEPTH_BIT:
-		switch(format)
-		{
-		case VK_FORMAT_D16_UNORM_S8_UINT:
-			return VK_FORMAT_D16_UNORM;
-		case VK_FORMAT_D24_UNORM_S8_UINT:
-			return VK_FORMAT_X8_D24_UNORM_PACK32; // FIXME: This will allocate an extra byte per pixel
-		case VK_FORMAT_D32_SFLOAT_S8_UINT:
-			return VK_FORMAT_D32_SFLOAT;
-		default:
-			break;
-		}
-		break;
-	case VK_IMAGE_ASPECT_STENCIL_BIT:
-		switch(format)
-		{
-		case VK_FORMAT_D16_UNORM_S8_UINT:
-		case VK_FORMAT_D24_UNORM_S8_UINT:
-		case VK_FORMAT_D32_SFLOAT_S8_UINT:
-			return VK_FORMAT_S8_UINT;
-		default:
-			break;
-		}
-		break;
-	default:
-		break;
-	}
-
-	return format;
+	return format.getAspectFormat(aspect);
 }
 
 bool Image::isCube() const
diff --git a/src/Vulkan/VkImage.hpp b/src/Vulkan/VkImage.hpp
index ac37f1e..24c48fe 100644
--- a/src/Vulkan/VkImage.hpp
+++ b/src/Vulkan/VkImage.hpp
@@ -72,8 +72,6 @@
 	void                     prepareForSampling(const VkImageSubresourceRange& subresourceRange);
 	const Image*             getSampledImage(const vk::Format& imageViewFormat) const;
 
-	static Format            GetFormat(const vk::Format& format, VkImageAspectFlagBits aspect);
-
 private:
 	void copy(VkBuffer buffer, const VkBufferImageCopy& region, bool bufferIsSource);
 	VkDeviceSize getStorageSize(VkImageAspectFlags flags) const;