Refactor writing texture level descriptor info

This will make it easier to write YCbCr plane data as mipmap levels.

Also removes the unused fWidth, fHeight, and fDepth from sw::Mipmap,
and ensured the other members are aligned to their vector size.

Bug: b/132437008
Tests: dEQP-VK.*ycbcr*
Change-Id: Ib2b0cbebf9e4fd640fcc104281097ff3c4c73b5d
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/31616
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
diff --git a/src/Device/Sampler.hpp b/src/Device/Sampler.hpp
index 65313ba..48ca936 100644
--- a/src/Device/Sampler.hpp
+++ b/src/Device/Sampler.hpp
@@ -31,13 +31,13 @@
 	{
 		const void *buffer[6];
 
-		short uHalf[4];
-		short vHalf[4];
-		short wHalf[4];
+		short4 uHalf;
+		short4 vHalf;
+		short4 wHalf;
 		int4 width;
 		int4 height;
 		int4 depth;
-		short onePitchP[4];
+		short4 onePitchP;
 		int4 pitchP;
 		int4 sliceP;
 	};
diff --git a/src/Vulkan/VkDescriptorSetLayout.cpp b/src/Vulkan/VkDescriptorSetLayout.cpp
index d0b906a..afd621c 100644
--- a/src/Vulkan/VkDescriptorSetLayout.cpp
+++ b/src/Vulkan/VkDescriptorSetLayout.cpp
@@ -333,14 +333,17 @@
 		}
 	}
 	else if (entry.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
-	   		 entry.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE)
+	         entry.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE)
 	{
 		SampledImageDescriptor *imageSampler = reinterpret_cast<SampledImageDescriptor*>(memToWrite);
 
 		for(uint32_t i = 0; i < entry.descriptorCount; i++)
 		{
 			auto update = reinterpret_cast<VkDescriptorImageInfo const *>(src + entry.offset + entry.stride * i);
+
 			vk::ImageView *imageView = vk::Cast(update->imageView);
+			Format format = imageView->getFormat(ImageView::SAMPLING);
+
 			sw::Texture *texture = &imageSampler[i].texture;
 
 			// "All consecutive bindings updated via a single VkWriteDescriptorSet structure, except those with a
@@ -357,7 +360,7 @@
 			imageSampler[i].sampleCount = imageView->getSampleCount();
 			imageSampler[i].type = imageView->getType();
 			imageSampler[i].swizzle = imageView->getComponentMapping();
-			imageSampler[i].format = imageView->getFormat(ImageView::SAMPLING);
+			imageSampler[i].format = format;
 
 			auto &subresourceRange = imageView->getSubresourceRange();
 
@@ -387,113 +390,15 @@
 				}
 
 				VkExtent3D extent = imageView->getMipLevelExtent(level);
-				Format format = imageView->getFormat(ImageView::SAMPLING);
-				int layers = imageView->getSubresourceRange().layerCount;
-				// TODO(b/129523279): Untangle depth vs layers throughout the sampler
+
 				int width = extent.width;
 				int height = extent.height;
+				int layers = imageView->getSubresourceRange().layerCount;  // TODO(b/129523279): Untangle depth vs layers throughout the sampler
 				int depth = layers > 1 ? layers : extent.depth;
 				int pitchP = imageView->rowPitchBytes(aspect, level, ImageView::SAMPLING) / format.bytes();
 				int sliceP = (layers > 1 ? imageView->layerPitchBytes(aspect, ImageView::SAMPLING) : imageView->slicePitchBytes(aspect, level, ImageView::SAMPLING)) / format.bytes();
 
-				if(mipmapLevel == 0)
-				{
-					texture->widthWidthHeightHeight[0] = width;
-					texture->widthWidthHeightHeight[1] = width;
-					texture->widthWidthHeightHeight[2] = height;
-					texture->widthWidthHeightHeight[3] = height;
-
-					texture->width[0] = width;
-					texture->width[1] = width;
-					texture->width[2] = width;
-					texture->width[3] = width;
-
-					texture->height[0] = height;
-					texture->height[1] = height;
-					texture->height[2] = height;
-					texture->height[3] = height;
-
-					texture->depth[0] = depth;
-					texture->depth[1] = depth;
-					texture->depth[2] = depth;
-					texture->depth[3] = depth;
-				}
-
-				short halfTexelU = 0x8000 / width;
-				short halfTexelV = 0x8000 / height;
-				short halfTexelW = 0x8000 / depth;
-
-				mipmap.uHalf[0] = halfTexelU;
-				mipmap.uHalf[1] = halfTexelU;
-				mipmap.uHalf[2] = halfTexelU;
-				mipmap.uHalf[3] = halfTexelU;
-
-				mipmap.vHalf[0] = halfTexelV;
-				mipmap.vHalf[1] = halfTexelV;
-				mipmap.vHalf[2] = halfTexelV;
-				mipmap.vHalf[3] = halfTexelV;
-
-				mipmap.wHalf[0] = halfTexelW;
-				mipmap.wHalf[1] = halfTexelW;
-				mipmap.wHalf[2] = halfTexelW;
-				mipmap.wHalf[3] = halfTexelW;
-
-				mipmap.width[0] = width;
-				mipmap.width[1] = width;
-				mipmap.width[2] = width;
-				mipmap.width[3] = width;
-
-				mipmap.height[0] = height;
-				mipmap.height[1] = height;
-				mipmap.height[2] = height;
-				mipmap.height[3] = height;
-
-				mipmap.depth[0] = depth;
-				mipmap.depth[1] = depth;
-				mipmap.depth[2] = depth;
-				mipmap.depth[3] = depth;
-
-				mipmap.onePitchP[0] = 1;
-				mipmap.onePitchP[1] = pitchP;
-				mipmap.onePitchP[2] = 1;
-				mipmap.onePitchP[3] = pitchP;
-
-				mipmap.pitchP[0] = pitchP;
-				mipmap.pitchP[1] = pitchP;
-				mipmap.pitchP[2] = pitchP;
-				mipmap.pitchP[3] = pitchP;
-
-				mipmap.sliceP[0] = sliceP;
-				mipmap.sliceP[1] = sliceP;
-				mipmap.sliceP[2] = sliceP;
-				mipmap.sliceP[3] = sliceP;
-
-				// TODO(b/129523279)
-				if(false/*format == FORMAT_YV12_BT601 ||
-				   format == FORMAT_YV12_BT709 ||
-				   format == FORMAT_YV12_JFIF*/)
-				{
-					unsigned int YStride = pitchP;
-					unsigned int YSize = YStride * height;
-					unsigned int CStride = sw::align<16>(YStride / 2);
-					unsigned int CSize = CStride * height / 2;
-
-					mipmap.buffer[1] = (sw::byte*)mipmap.buffer[0] + YSize;
-					mipmap.buffer[2] = (sw::byte*)mipmap.buffer[1] + CSize;
-
-					texture->mipmap[1].width[0] = width / 2;
-					texture->mipmap[1].width[1] = width / 2;
-					texture->mipmap[1].width[2] = width / 2;
-					texture->mipmap[1].width[3] = width / 2;
-					texture->mipmap[1].height[0] = height / 2;
-					texture->mipmap[1].height[1] = height / 2;
-					texture->mipmap[1].height[2] = height / 2;
-					texture->mipmap[1].height[3] = height / 2;
-					texture->mipmap[1].onePitchP[0] = 1;
-					texture->mipmap[1].onePitchP[1] = CStride;
-					texture->mipmap[1].onePitchP[2] = 1;
-					texture->mipmap[1].onePitchP[3] = CStride;
-				}
+				WriteTextureLevelInfo(texture, mipmapLevel, width, height, depth, pitchP, sliceP);
 			}
 		}
 	}
@@ -561,6 +466,83 @@
 	}
 }
 
+void DescriptorSetLayout::WriteTextureLevelInfo(sw::Texture *texture, int level, int width, int height, int depth, int pitchP, int sliceP)
+{
+	if(level == 0)
+	{
+		texture->widthWidthHeightHeight[0] = width;
+		texture->widthWidthHeightHeight[1] = width;
+		texture->widthWidthHeightHeight[2] = height;
+		texture->widthWidthHeightHeight[3] = height;
+
+		texture->width[0] = width;
+		texture->width[1] = width;
+		texture->width[2] = width;
+		texture->width[3] = width;
+
+		texture->height[0] = height;
+		texture->height[1] = height;
+		texture->height[2] = height;
+		texture->height[3] = height;
+
+		texture->depth[0] = depth;
+		texture->depth[1] = depth;
+		texture->depth[2] = depth;
+		texture->depth[3] = depth;
+	}
+
+	sw::Mipmap &mipmap = texture->mipmap[level];
+
+	short halfTexelU = 0x8000 / width;
+	short halfTexelV = 0x8000 / height;
+	short halfTexelW = 0x8000 / depth;
+
+	mipmap.uHalf[0] = halfTexelU;
+	mipmap.uHalf[1] = halfTexelU;
+	mipmap.uHalf[2] = halfTexelU;
+	mipmap.uHalf[3] = halfTexelU;
+
+	mipmap.vHalf[0] = halfTexelV;
+	mipmap.vHalf[1] = halfTexelV;
+	mipmap.vHalf[2] = halfTexelV;
+	mipmap.vHalf[3] = halfTexelV;
+
+	mipmap.wHalf[0] = halfTexelW;
+	mipmap.wHalf[1] = halfTexelW;
+	mipmap.wHalf[2] = halfTexelW;
+	mipmap.wHalf[3] = halfTexelW;
+
+	mipmap.width[0] = width;
+	mipmap.width[1] = width;
+	mipmap.width[2] = width;
+	mipmap.width[3] = width;
+
+	mipmap.height[0] = height;
+	mipmap.height[1] = height;
+	mipmap.height[2] = height;
+	mipmap.height[3] = height;
+
+	mipmap.depth[0] = depth;
+	mipmap.depth[1] = depth;
+	mipmap.depth[2] = depth;
+	mipmap.depth[3] = depth;
+
+	mipmap.onePitchP[0] = 1;
+	mipmap.onePitchP[1] = pitchP;
+	mipmap.onePitchP[2] = 1;
+	mipmap.onePitchP[3] = pitchP;
+
+	mipmap.pitchP[0] = pitchP;
+	mipmap.pitchP[1] = pitchP;
+	mipmap.pitchP[2] = pitchP;
+	mipmap.pitchP[3] = pitchP;
+
+	mipmap.sliceP[0] = sliceP;
+	mipmap.sliceP[1] = sliceP;
+	mipmap.sliceP[2] = sliceP;
+	mipmap.sliceP[3] = sliceP;
+}
+
 void DescriptorSetLayout::WriteDescriptorSet(const VkWriteDescriptorSet& writeDescriptorSet)
 {
 	DescriptorSet* dstSet = vk::Cast(writeDescriptorSet.dstSet);
diff --git a/src/Vulkan/VkDescriptorSetLayout.hpp b/src/Vulkan/VkDescriptorSetLayout.hpp
index a67b261..08408f2 100644
--- a/src/Vulkan/VkDescriptorSetLayout.hpp
+++ b/src/Vulkan/VkDescriptorSetLayout.hpp
@@ -82,6 +82,7 @@
 	static void CopyDescriptorSet(const VkCopyDescriptorSet& descriptorCopies);
 
 	static void WriteDescriptorSet(DescriptorSet *dstSet, VkDescriptorUpdateTemplateEntry const &entry, char const *src);
+	static void WriteTextureLevelInfo(sw::Texture *texture, int level, int width, int height, int depth, int pitchP, int sliceP);
 
 	void initialize(VkDescriptorSet descriptorSet);