• 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 "VkDescriptorSetLayout.hpp"
16 
17 #include "VkBuffer.hpp"
18 #include "VkBufferView.hpp"
19 #include "VkDescriptorSet.hpp"
20 #include "VkImageView.hpp"
21 #include "VkSampler.hpp"
22 
23 #include "Reactor/Reactor.hpp"
24 
25 #include <algorithm>
26 #include <cstddef>
27 #include <cstring>
28 
29 namespace vk {
30 
UsesImmutableSamplers(const VkDescriptorSetLayoutBinding & binding)31 static bool UsesImmutableSamplers(const VkDescriptorSetLayoutBinding &binding)
32 {
33 	return (((binding.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) ||
34 	         (binding.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)) &&
35 	        (binding.pImmutableSamplers != nullptr));
36 }
37 
DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo * pCreateInfo,void * mem)38 DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *pCreateInfo, void *mem)
39     : flags(pCreateInfo->flags)
40     , bindings(reinterpret_cast<Binding *>(mem))
41 {
42 	// The highest binding number determines the size of the direct-indexed array.
43 	bindingsArraySize = 0;
44 	for(uint32_t i = 0; i < pCreateInfo->bindingCount; i++)
45 	{
46 		bindingsArraySize = std::max(bindingsArraySize, pCreateInfo->pBindings[i].binding + 1);
47 	}
48 
49 	uint8_t *immutableSamplersStorage = static_cast<uint8_t *>(mem) + bindingsArraySize * sizeof(Binding);
50 
51 	// pCreateInfo->pBindings[] can have gaps in the binding numbers, so first initialize the entire bindings array.
52 	// "Bindings that are not specified have a descriptorCount and stageFlags of zero, and the value of descriptorType is undefined."
53 	for(uint32_t i = 0; i < bindingsArraySize; i++)
54 	{
55 		bindings[i].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER;
56 		bindings[i].descriptorCount = 0;
57 		bindings[i].immutableSamplers = nullptr;
58 	}
59 
60 	for(uint32_t i = 0; i < pCreateInfo->bindingCount; i++)
61 	{
62 		const VkDescriptorSetLayoutBinding &srcBinding = pCreateInfo->pBindings[i];
63 		vk::DescriptorSetLayout::Binding &dstBinding = bindings[srcBinding.binding];
64 
65 		dstBinding.descriptorType = srcBinding.descriptorType;
66 		dstBinding.descriptorCount = srcBinding.descriptorCount;
67 
68 		if(UsesImmutableSamplers(srcBinding))
69 		{
70 			size_t immutableSamplersSize = dstBinding.descriptorCount * sizeof(VkSampler);
71 			dstBinding.immutableSamplers = reinterpret_cast<const vk::Sampler **>(immutableSamplersStorage);
72 			immutableSamplersStorage += immutableSamplersSize;
73 
74 			for(uint32_t i = 0; i < dstBinding.descriptorCount; i++)
75 			{
76 				dstBinding.immutableSamplers[i] = vk::Cast(srcBinding.pImmutableSamplers[i]);
77 			}
78 		}
79 	}
80 
81 	uint32_t offset = 0;
82 	for(uint32_t i = 0; i < bindingsArraySize; i++)
83 	{
84 		bindings[i].offset = offset;
85 		offset += bindings[i].descriptorCount * GetDescriptorSize(bindings[i].descriptorType);
86 	}
87 
88 	ASSERT_MSG(offset == getDescriptorSetDataSize(), "offset: %d, size: %d", int(offset), int(getDescriptorSetDataSize()));
89 }
90 
destroy(const VkAllocationCallbacks * pAllocator)91 void DescriptorSetLayout::destroy(const VkAllocationCallbacks *pAllocator)
92 {
93 	vk::freeHostMemory(bindings, pAllocator);  // This allocation also contains pImmutableSamplers
94 }
95 
ComputeRequiredAllocationSize(const VkDescriptorSetLayoutCreateInfo * pCreateInfo)96 size_t DescriptorSetLayout::ComputeRequiredAllocationSize(const VkDescriptorSetLayoutCreateInfo *pCreateInfo)
97 {
98 	uint32_t bindingsArraySize = 0;
99 	uint32_t immutableSamplerCount = 0;
100 	for(uint32_t i = 0; i < pCreateInfo->bindingCount; i++)
101 	{
102 		bindingsArraySize = std::max(bindingsArraySize, pCreateInfo->pBindings[i].binding + 1);
103 
104 		if(UsesImmutableSamplers(pCreateInfo->pBindings[i]))
105 		{
106 			immutableSamplerCount += pCreateInfo->pBindings[i].descriptorCount;
107 		}
108 	}
109 
110 	return bindingsArraySize * sizeof(Binding) +
111 	       immutableSamplerCount * sizeof(VkSampler);
112 }
113 
GetDescriptorSize(VkDescriptorType type)114 uint32_t DescriptorSetLayout::GetDescriptorSize(VkDescriptorType type)
115 {
116 	switch(type)
117 	{
118 	case VK_DESCRIPTOR_TYPE_SAMPLER:
119 	case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
120 	case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
121 	case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
122 		return static_cast<uint32_t>(sizeof(SampledImageDescriptor));
123 	case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
124 	case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
125 	case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
126 		return static_cast<uint32_t>(sizeof(StorageImageDescriptor));
127 	case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
128 	case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
129 	case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
130 	case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
131 		return static_cast<uint32_t>(sizeof(BufferDescriptor));
132 	case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK:
133 		return 1;
134 	default:
135 		UNSUPPORTED("Unsupported Descriptor Type: %d", int(type));
136 		return 0;
137 	}
138 }
139 
IsDescriptorDynamic(VkDescriptorType type)140 bool DescriptorSetLayout::IsDescriptorDynamic(VkDescriptorType type)
141 {
142 	return type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
143 	       type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC;
144 }
145 
getDescriptorSetAllocationSize() const146 size_t DescriptorSetLayout::getDescriptorSetAllocationSize() const
147 {
148 	// vk::DescriptorSet has a header with a pointer to the layout.
149 	return sw::align<alignof(DescriptorSet)>(OFFSET(DescriptorSet, data) + getDescriptorSetDataSize());
150 }
151 
getDescriptorSetDataSize() const152 size_t DescriptorSetLayout::getDescriptorSetDataSize() const
153 {
154 	size_t size = 0;
155 	for(uint32_t i = 0; i < bindingsArraySize; i++)
156 	{
157 		size += bindings[i].descriptorCount * GetDescriptorSize(bindings[i].descriptorType);
158 	}
159 
160 	return size;
161 }
162 
initialize(DescriptorSet * descriptorSet)163 void DescriptorSetLayout::initialize(DescriptorSet *descriptorSet)
164 {
165 	ASSERT(descriptorSet->header.layout == nullptr);
166 
167 	// Use a pointer to this descriptor set layout as the descriptor set's header
168 	descriptorSet->header.layout = this;
169 	uint8_t *mem = descriptorSet->data;
170 
171 	for(uint32_t i = 0; i < bindingsArraySize; i++)
172 	{
173 		size_t descriptorSize = GetDescriptorSize(bindings[i].descriptorType);
174 
175 		if(bindings[i].immutableSamplers)
176 		{
177 			for(uint32_t j = 0; j < bindings[i].descriptorCount; j++)
178 			{
179 				SampledImageDescriptor *imageSamplerDescriptor = reinterpret_cast<SampledImageDescriptor *>(mem);
180 				imageSamplerDescriptor->samplerId = bindings[i].immutableSamplers[j]->id;
181 				imageSamplerDescriptor->memoryOwner = nullptr;
182 				mem += descriptorSize;
183 			}
184 		}
185 		else
186 		{
187 			switch(bindings[i].descriptorType)
188 			{
189 			case VK_DESCRIPTOR_TYPE_SAMPLER:
190 			case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
191 			case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
192 			case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
193 				for(uint32_t j = 0; j < bindings[i].descriptorCount; j++)
194 				{
195 					SampledImageDescriptor *imageSamplerDescriptor = reinterpret_cast<SampledImageDescriptor *>(mem);
196 					imageSamplerDescriptor->memoryOwner = nullptr;
197 					mem += descriptorSize;
198 				}
199 				break;
200 			case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
201 			case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
202 			case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
203 				for(uint32_t j = 0; j < bindings[i].descriptorCount; j++)
204 				{
205 					StorageImageDescriptor *storageImage = reinterpret_cast<StorageImageDescriptor *>(mem);
206 					storageImage->memoryOwner = nullptr;
207 					mem += descriptorSize;
208 				}
209 				break;
210 			case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
211 			case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
212 			case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
213 			case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
214 				mem += bindings[i].descriptorCount * descriptorSize;
215 				break;
216 			case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK:
217 				mem += bindings[i].descriptorCount;
218 				break;
219 			default:
220 				UNSUPPORTED("Unsupported Descriptor Type: %d", int(bindings[i].descriptorType));
221 			}
222 		}
223 	}
224 }
225 
getBindingOffset(uint32_t bindingNumber) const226 uint32_t DescriptorSetLayout::getBindingOffset(uint32_t bindingNumber) const
227 {
228 	ASSERT(bindingNumber < bindingsArraySize);
229 	return bindings[bindingNumber].offset;
230 }
231 
getDescriptorCount(uint32_t bindingNumber) const232 uint32_t DescriptorSetLayout::getDescriptorCount(uint32_t bindingNumber) const
233 {
234 	ASSERT(bindingNumber < bindingsArraySize);
235 	return bindings[bindingNumber].descriptorCount;
236 }
237 
getDynamicDescriptorCount() const238 uint32_t DescriptorSetLayout::getDynamicDescriptorCount() const
239 {
240 	uint32_t count = 0;
241 	for(size_t i = 0; i < bindingsArraySize; i++)
242 	{
243 		if(IsDescriptorDynamic(bindings[i].descriptorType))
244 		{
245 			count += bindings[i].descriptorCount;
246 		}
247 	}
248 
249 	return count;
250 }
251 
getDynamicOffsetIndex(uint32_t bindingNumber) const252 uint32_t DescriptorSetLayout::getDynamicOffsetIndex(uint32_t bindingNumber) const
253 {
254 	ASSERT(bindingNumber < bindingsArraySize);
255 	ASSERT(IsDescriptorDynamic(bindings[bindingNumber].descriptorType));
256 
257 	uint32_t index = 0;
258 	for(uint32_t i = 0; i < bindingNumber; i++)
259 	{
260 		if(IsDescriptorDynamic(bindings[i].descriptorType))
261 		{
262 			index += bindings[i].descriptorCount;
263 		}
264 	}
265 
266 	return index;
267 }
268 
getDescriptorType(uint32_t bindingNumber) const269 VkDescriptorType DescriptorSetLayout::getDescriptorType(uint32_t bindingNumber) const
270 {
271 	ASSERT(bindingNumber < bindingsArraySize);
272 	return bindings[bindingNumber].descriptorType;
273 }
274 
getDescriptorPointer(DescriptorSet * descriptorSet,uint32_t bindingNumber,uint32_t arrayElement,uint32_t count,size_t * typeSize) const275 uint8_t *DescriptorSetLayout::getDescriptorPointer(DescriptorSet *descriptorSet, uint32_t bindingNumber, uint32_t arrayElement, uint32_t count, size_t *typeSize) const
276 {
277 	ASSERT(bindingNumber < bindingsArraySize);
278 	*typeSize = GetDescriptorSize(bindings[bindingNumber].descriptorType);
279 	size_t byteOffset = bindings[bindingNumber].offset + (*typeSize * arrayElement);
280 	ASSERT(((*typeSize * count) + byteOffset) <= getDescriptorSetDataSize());  // Make sure the operation will not go out of bounds
281 
282 	return &descriptorSet->data[byteOffset];
283 }
284 
WriteTextureLevelInfo(sw::Texture * texture,uint32_t level,uint32_t width,uint32_t height,uint32_t depth,uint32_t pitchP,uint32_t sliceP,uint32_t samplePitchP,uint32_t sampleMax)285 static void WriteTextureLevelInfo(sw::Texture *texture, uint32_t level, uint32_t width, uint32_t height, uint32_t depth, uint32_t pitchP, uint32_t sliceP, uint32_t samplePitchP, uint32_t sampleMax)
286 {
287 	if(level == 0)
288 	{
289 		texture->widthWidthHeightHeight[0] = static_cast<float>(width);
290 		texture->widthWidthHeightHeight[1] = static_cast<float>(width);
291 		texture->widthWidthHeightHeight[2] = static_cast<float>(height);
292 		texture->widthWidthHeightHeight[3] = static_cast<float>(height);
293 
294 		texture->width = sw::float4(static_cast<float>(width));
295 		texture->height = sw::float4(static_cast<float>(height));
296 		texture->depth = sw::float4(static_cast<float>(depth));
297 	}
298 
299 	sw::Mipmap &mipmap = texture->mipmap[level];
300 
301 	uint16_t halfTexelU = 0x8000 / width;
302 	uint16_t halfTexelV = 0x8000 / height;
303 	uint16_t halfTexelW = 0x8000 / depth;
304 
305 	mipmap.uHalf = sw::ushort4(halfTexelU);
306 	mipmap.vHalf = sw::ushort4(halfTexelV);
307 	mipmap.wHalf = sw::ushort4(halfTexelW);
308 
309 	mipmap.width = sw::uint4(width);
310 	mipmap.height = sw::uint4(height);
311 	mipmap.depth = sw::uint4(depth);
312 
313 	mipmap.onePitchP[0] = 1;
314 	mipmap.onePitchP[1] = sw::assert_cast<short>(pitchP);
315 	mipmap.onePitchP[2] = 1;
316 	mipmap.onePitchP[3] = sw::assert_cast<short>(pitchP);
317 
318 	mipmap.pitchP = sw::uint4(pitchP);
319 	mipmap.sliceP = sw::uint4(sliceP);
320 	mipmap.samplePitchP = sw::uint4(samplePitchP);
321 	mipmap.sampleMax = sw::uint4(sampleMax);
322 }
323 
WriteDescriptorSet(Device * device,DescriptorSet * dstSet,VkDescriptorUpdateTemplateEntry const & entry,const char * src)324 void DescriptorSetLayout::WriteDescriptorSet(Device *device, DescriptorSet *dstSet, VkDescriptorUpdateTemplateEntry const &entry, const char *src)
325 {
326 	DescriptorSetLayout *dstLayout = dstSet->header.layout;
327 	const DescriptorSetLayout::Binding &binding = dstLayout->bindings[entry.dstBinding];
328 	ASSERT(dstLayout);
329 	ASSERT(binding.descriptorType == entry.descriptorType);
330 
331 	size_t typeSize = 0;
332 	uint8_t *memToWrite = dstLayout->getDescriptorPointer(dstSet, entry.dstBinding, entry.dstArrayElement, entry.descriptorCount, &typeSize);
333 
334 	ASSERT(reinterpret_cast<intptr_t>(memToWrite) % 16 == 0);  // Each descriptor must be 16-byte aligned.
335 
336 	if(entry.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER)
337 	{
338 		SampledImageDescriptor *sampledImage = reinterpret_cast<SampledImageDescriptor *>(memToWrite);
339 
340 		for(uint32_t i = 0; i < entry.descriptorCount; i++)
341 		{
342 			const VkDescriptorImageInfo *update = reinterpret_cast<const VkDescriptorImageInfo *>(src + entry.offset + entry.stride * i);
343 
344 			// "All consecutive bindings updated via a single VkWriteDescriptorSet structure, except those with a
345 			//  descriptorCount of zero, must all either use immutable samplers or must all not use immutable samplers."
346 			if(!binding.immutableSamplers)
347 			{
348 				sampledImage[i].samplerId = vk::Cast(update->sampler)->id;
349 			}
350 		}
351 	}
352 	else if(entry.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER)
353 	{
354 		SampledImageDescriptor *sampledImage = reinterpret_cast<SampledImageDescriptor *>(memToWrite);
355 
356 		for(uint32_t i = 0; i < entry.descriptorCount; i++)
357 		{
358 			const VkBufferView *update = reinterpret_cast<const VkBufferView *>(src + entry.offset + entry.stride * i);
359 			const vk::BufferView *bufferView = vk::Cast(*update);
360 
361 			sampledImage[i].imageViewId = bufferView->id;
362 
363 			uint32_t numElements = bufferView->getElementCount();
364 			sampledImage[i].width = numElements;
365 			sampledImage[i].height = 1;
366 			sampledImage[i].depth = 1;
367 			sampledImage[i].mipLevels = 1;
368 			sampledImage[i].sampleCount = 1;
369 			sampledImage[i].texture.widthWidthHeightHeight = sw::float4(static_cast<float>(numElements), static_cast<float>(numElements), 1, 1);
370 			sampledImage[i].texture.width = sw::float4(static_cast<float>(numElements));
371 			sampledImage[i].texture.height = sw::float4(1);
372 			sampledImage[i].texture.depth = sw::float4(1);
373 
374 			sw::Mipmap &mipmap = sampledImage[i].texture.mipmap[0];
375 			mipmap.buffer = bufferView->getPointer();
376 			mipmap.width[0] = mipmap.width[1] = mipmap.width[2] = mipmap.width[3] = numElements;
377 			mipmap.height[0] = mipmap.height[1] = mipmap.height[2] = mipmap.height[3] = 1;
378 			mipmap.depth[0] = mipmap.depth[1] = mipmap.depth[2] = mipmap.depth[3] = 1;
379 			mipmap.pitchP.x = mipmap.pitchP.y = mipmap.pitchP.z = mipmap.pitchP.w = numElements;
380 			mipmap.sliceP.x = mipmap.sliceP.y = mipmap.sliceP.z = mipmap.sliceP.w = 0;
381 			mipmap.onePitchP[0] = mipmap.onePitchP[2] = 1;
382 			mipmap.onePitchP[1] = mipmap.onePitchP[3] = 0;
383 		}
384 	}
385 	else if(entry.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
386 	        entry.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE)
387 	{
388 		SampledImageDescriptor *sampledImage = reinterpret_cast<SampledImageDescriptor *>(memToWrite);
389 
390 		for(uint32_t i = 0; i < entry.descriptorCount; i++)
391 		{
392 			const VkDescriptorImageInfo *update = reinterpret_cast<const VkDescriptorImageInfo *>(src + entry.offset + entry.stride * i);
393 
394 			vk::ImageView *imageView = vk::Cast(update->imageView);
395 			Format format = imageView->getFormat(ImageView::SAMPLING);
396 
397 			sw::Texture *texture = &sampledImage[i].texture;
398 
399 			if(entry.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
400 			{
401 				// "All consecutive bindings updated via a single VkWriteDescriptorSet structure, except those with a
402 				//  descriptorCount of zero, must all either use immutable samplers or must all not use immutable samplers."
403 				if(!binding.immutableSamplers)
404 				{
405 					sampledImage[i].samplerId = vk::Cast(update->sampler)->id;
406 				}
407 			}
408 
409 			const auto &extent = imageView->getMipLevelExtent(0);
410 
411 			sampledImage[i].imageViewId = imageView->id;
412 			sampledImage[i].width = extent.width;
413 			sampledImage[i].height = extent.height;
414 			sampledImage[i].depth = imageView->getDepthOrLayerCount(0);
415 			sampledImage[i].mipLevels = imageView->getSubresourceRange().levelCount;
416 			sampledImage[i].sampleCount = imageView->getSampleCount();
417 			sampledImage[i].memoryOwner = imageView;
418 
419 			auto &subresourceRange = imageView->getSubresourceRange();
420 
421 			if(format.isYcbcrFormat())
422 			{
423 				ASSERT(subresourceRange.levelCount == 1);
424 
425 				// YCbCr images can only have one level, so we can store parameters for the
426 				// different planes in the descriptor's mipmap levels instead.
427 
428 				const int level = 0;
429 				VkOffset3D offset = { 0, 0, 0 };
430 				texture->mipmap[0].buffer = imageView->getOffsetPointer(offset, VK_IMAGE_ASPECT_PLANE_0_BIT, level, 0, ImageView::SAMPLING);
431 				texture->mipmap[1].buffer = imageView->getOffsetPointer(offset, VK_IMAGE_ASPECT_PLANE_1_BIT, level, 0, ImageView::SAMPLING);
432 				if(format.getAspects() & VK_IMAGE_ASPECT_PLANE_2_BIT)
433 				{
434 					texture->mipmap[2].buffer = imageView->getOffsetPointer(offset, VK_IMAGE_ASPECT_PLANE_2_BIT, level, 0, ImageView::SAMPLING);
435 				}
436 
437 				VkExtent2D extent = imageView->getMipLevelExtent(0);
438 
439 				uint32_t width = extent.width;
440 				uint32_t height = extent.height;
441 				uint32_t pitchP0 = imageView->rowPitchBytes(VK_IMAGE_ASPECT_PLANE_0_BIT, level, ImageView::SAMPLING) /
442 				                   imageView->getFormat(VK_IMAGE_ASPECT_PLANE_0_BIT).bytes();
443 
444 				// Write plane 0 parameters to mipmap level 0.
445 				WriteTextureLevelInfo(texture, 0, width, height, 1, pitchP0, 0, 0, 0);
446 
447 				// Plane 2, if present, has equal parameters to plane 1, so we use mipmap level 1 for both.
448 				uint32_t pitchP1 = imageView->rowPitchBytes(VK_IMAGE_ASPECT_PLANE_1_BIT, level, ImageView::SAMPLING) /
449 				                   imageView->getFormat(VK_IMAGE_ASPECT_PLANE_1_BIT).bytes();
450 
451 				WriteTextureLevelInfo(texture, 1, width / 2, height / 2, 1, pitchP1, 0, 0, 0);
452 			}
453 			else
454 			{
455 				for(int mipmapLevel = 0; mipmapLevel < sw::MIPMAP_LEVELS; mipmapLevel++)
456 				{
457 					int level = sw::clamp(mipmapLevel, 0, (int)subresourceRange.levelCount - 1);  // Level within the image view
458 
459 					VkImageAspectFlagBits aspect = static_cast<VkImageAspectFlagBits>(imageView->getSubresourceRange().aspectMask);
460 					sw::Mipmap &mipmap = texture->mipmap[mipmapLevel];
461 
462 					if((imageView->getType() == VK_IMAGE_VIEW_TYPE_CUBE) ||
463 					   (imageView->getType() == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY))
464 					{
465 						// Obtain the pointer to the corner of the level including the border, for seamless sampling.
466 						// This is taken into account in the sampling routine, which can't handle negative texel coordinates.
467 						VkOffset3D offset = { -1, -1, 0 };
468 						mipmap.buffer = imageView->getOffsetPointer(offset, aspect, level, 0, ImageView::SAMPLING);
469 					}
470 					else
471 					{
472 						VkOffset3D offset = { 0, 0, 0 };
473 						mipmap.buffer = imageView->getOffsetPointer(offset, aspect, level, 0, ImageView::SAMPLING);
474 					}
475 
476 					VkExtent2D extent = imageView->getMipLevelExtent(level);
477 
478 					uint32_t width = extent.width;
479 					uint32_t height = extent.height;
480 					uint32_t layerCount = imageView->getSubresourceRange().layerCount;
481 					uint32_t depth = imageView->getDepthOrLayerCount(level);
482 					uint32_t bytes = format.bytes();
483 					uint32_t pitchP = imageView->rowPitchBytes(aspect, level, ImageView::SAMPLING) / bytes;
484 					uint32_t sliceP = (layerCount > 1 ? imageView->layerPitchBytes(aspect, ImageView::SAMPLING) : imageView->slicePitchBytes(aspect, level, ImageView::SAMPLING)) / bytes;
485 					uint32_t samplePitchP = imageView->getMipLevelSize(aspect, level, ImageView::SAMPLING) / bytes;
486 					uint32_t sampleMax = imageView->getSampleCount() - 1;
487 
488 					WriteTextureLevelInfo(texture, mipmapLevel, width, height, depth, pitchP, sliceP, samplePitchP, sampleMax);
489 				}
490 			}
491 		}
492 	}
493 	else if(entry.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE ||
494 	        entry.descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)
495 	{
496 		StorageImageDescriptor *storageImage = reinterpret_cast<StorageImageDescriptor *>(memToWrite);
497 
498 		for(uint32_t i = 0; i < entry.descriptorCount; i++)
499 		{
500 			const VkDescriptorImageInfo *update = reinterpret_cast<const VkDescriptorImageInfo *>(src + entry.offset + entry.stride * i);
501 			vk::ImageView *imageView = vk::Cast(update->imageView);
502 			const auto &extent = imageView->getMipLevelExtent(0);
503 			uint32_t layerCount = imageView->getSubresourceRange().layerCount;
504 
505 			storageImage[i].imageViewId = imageView->id;
506 			storageImage[i].ptr = imageView->getOffsetPointer({ 0, 0, 0 }, VK_IMAGE_ASPECT_COLOR_BIT, 0, 0);
507 			storageImage[i].width = extent.width;
508 			storageImage[i].height = extent.height;
509 			storageImage[i].depth = imageView->getDepthOrLayerCount(0);
510 			storageImage[i].rowPitchBytes = imageView->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
511 			storageImage[i].samplePitchBytes = imageView->slicePitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
512 			storageImage[i].slicePitchBytes = layerCount > 1
513 			                                      ? imageView->layerPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT)
514 			                                      : imageView->slicePitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
515 			storageImage[i].sampleCount = imageView->getSampleCount();
516 			storageImage[i].sizeInBytes = static_cast<int>(imageView->getSizeInBytes());
517 			storageImage[i].memoryOwner = imageView;
518 
519 			if(imageView->getFormat().isStencil())
520 			{
521 				storageImage[i].stencilPtr = imageView->getOffsetPointer({ 0, 0, 0 }, VK_IMAGE_ASPECT_STENCIL_BIT, 0, 0);
522 				storageImage[i].stencilRowPitchBytes = imageView->rowPitchBytes(VK_IMAGE_ASPECT_STENCIL_BIT, 0);
523 				storageImage[i].stencilSamplePitchBytes = imageView->slicePitchBytes(VK_IMAGE_ASPECT_STENCIL_BIT, 0);
524 				storageImage[i].stencilSlicePitchBytes = (imageView->getSubresourceRange().layerCount > 1)
525 				                                             ? imageView->layerPitchBytes(VK_IMAGE_ASPECT_STENCIL_BIT)
526 				                                             : imageView->slicePitchBytes(VK_IMAGE_ASPECT_STENCIL_BIT, 0);
527 			}
528 		}
529 	}
530 	else if(entry.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)
531 	{
532 		StorageImageDescriptor *storageImage = reinterpret_cast<StorageImageDescriptor *>(memToWrite);
533 
534 		for(uint32_t i = 0; i < entry.descriptorCount; i++)
535 		{
536 			const VkBufferView *update = reinterpret_cast<const VkBufferView *>(src + entry.offset + entry.stride * i);
537 			const vk::BufferView *bufferView = vk::Cast(*update);
538 
539 			storageImage[i].imageViewId = bufferView->id;
540 			storageImage[i].ptr = bufferView->getPointer();
541 			storageImage[i].width = bufferView->getElementCount();
542 			storageImage[i].height = 1;
543 			storageImage[i].depth = 1;
544 			storageImage[i].rowPitchBytes = 0;
545 			storageImage[i].slicePitchBytes = 0;
546 			storageImage[i].samplePitchBytes = 0;
547 			storageImage[i].sampleCount = 1;
548 			storageImage[i].sizeInBytes = bufferView->getRangeInBytes();
549 		}
550 	}
551 	else if(entry.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER ||
552 	        entry.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
553 	        entry.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
554 	        entry.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)
555 	{
556 		BufferDescriptor *bufferDescriptor = reinterpret_cast<BufferDescriptor *>(memToWrite);
557 
558 		for(uint32_t i = 0; i < entry.descriptorCount; i++)
559 		{
560 			const VkDescriptorBufferInfo *update = reinterpret_cast<const VkDescriptorBufferInfo *>(src + entry.offset + entry.stride * i);
561 			const vk::Buffer *buffer = vk::Cast(update->buffer);
562 			bufferDescriptor[i].ptr = buffer->getOffsetPointer(update->offset);
563 			bufferDescriptor[i].sizeInBytes = static_cast<int>((update->range == VK_WHOLE_SIZE) ? buffer->getSize() - update->offset : update->range);
564 
565 			// TODO(b/195684837): The spec states that "vertexBufferRangeSize is the byte size of the memory
566 			// range bound to the vertex buffer binding", while the code below uses the full size of the buffer.
567 			bufferDescriptor[i].robustnessSize = static_cast<int>(buffer->getSize() - update->offset);
568 		}
569 	}
570 	else if(entry.descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK)
571 	{
572 		memcpy(memToWrite, src + entry.offset, entry.descriptorCount);
573 	}
574 }
575 
WriteDescriptorSet(Device * device,const VkWriteDescriptorSet & writeDescriptorSet)576 void DescriptorSetLayout::WriteDescriptorSet(Device *device, const VkWriteDescriptorSet &writeDescriptorSet)
577 {
578 	DescriptorSet *dstSet = vk::Cast(writeDescriptorSet.dstSet);
579 	VkDescriptorUpdateTemplateEntry e;
580 	e.descriptorType = writeDescriptorSet.descriptorType;
581 	e.dstBinding = writeDescriptorSet.dstBinding;
582 	e.dstArrayElement = writeDescriptorSet.dstArrayElement;
583 	e.descriptorCount = writeDescriptorSet.descriptorCount;
584 	e.offset = 0;
585 	const void *ptr = nullptr;
586 
587 	switch(writeDescriptorSet.descriptorType)
588 	{
589 	case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
590 	case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
591 		ptr = writeDescriptorSet.pTexelBufferView;
592 		e.stride = sizeof(VkBufferView);
593 		break;
594 
595 	case VK_DESCRIPTOR_TYPE_SAMPLER:
596 	case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
597 	case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
598 	case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
599 	case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
600 		ptr = writeDescriptorSet.pImageInfo;
601 		e.stride = sizeof(VkDescriptorImageInfo);
602 		break;
603 
604 	case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
605 	case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
606 	case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
607 	case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
608 		ptr = writeDescriptorSet.pBufferInfo;
609 		e.stride = sizeof(VkDescriptorBufferInfo);
610 		break;
611 
612 	case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK:
613 		{
614 			auto extInfo = reinterpret_cast<VkBaseInStructure const *>(writeDescriptorSet.pNext);
615 			while(extInfo)
616 			{
617 				if(extInfo->sType == VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_INLINE_UNIFORM_BLOCK)
618 				{
619 					// "The descriptorCount of VkDescriptorSetLayoutBinding thus provides the total
620 					//  number of bytes a particular binding with an inline uniform block descriptor
621 					//  type can hold, while the srcArrayElement, dstArrayElement, and descriptorCount
622 					//  members of VkWriteDescriptorSet, VkCopyDescriptorSet, and
623 					//  VkDescriptorUpdateTemplateEntry (where applicable) specify the byte offset and
624 					//  number of bytes to write/copy to the binding's backing store. Additionally,
625 					//  the stride member of VkDescriptorUpdateTemplateEntry is ignored for inline
626 					//  uniform blocks and a default value of one is used, meaning that the data to
627 					//  update inline uniform block bindings with must be contiguous in memory."
628 					ptr = reinterpret_cast<const VkWriteDescriptorSetInlineUniformBlock *>(extInfo)->pData;
629 					e.stride = 1;
630 					break;
631 				}
632 				extInfo = extInfo->pNext;
633 			}
634 		}
635 		break;
636 
637 	default:
638 		UNSUPPORTED("descriptor type %u", writeDescriptorSet.descriptorType);
639 	}
640 
641 	WriteDescriptorSet(device, dstSet, e, reinterpret_cast<const char *>(ptr));
642 }
643 
CopyDescriptorSet(const VkCopyDescriptorSet & descriptorCopies)644 void DescriptorSetLayout::CopyDescriptorSet(const VkCopyDescriptorSet &descriptorCopies)
645 {
646 	DescriptorSet *srcSet = vk::Cast(descriptorCopies.srcSet);
647 	DescriptorSetLayout *srcLayout = srcSet->header.layout;
648 	ASSERT(srcLayout);
649 
650 	DescriptorSet *dstSet = vk::Cast(descriptorCopies.dstSet);
651 	DescriptorSetLayout *dstLayout = dstSet->header.layout;
652 	ASSERT(dstLayout);
653 
654 	size_t srcTypeSize = 0;
655 	uint8_t *memToRead = srcLayout->getDescriptorPointer(srcSet, descriptorCopies.srcBinding, descriptorCopies.srcArrayElement, descriptorCopies.descriptorCount, &srcTypeSize);
656 
657 	size_t dstTypeSize = 0;
658 	uint8_t *memToWrite = dstLayout->getDescriptorPointer(dstSet, descriptorCopies.dstBinding, descriptorCopies.dstArrayElement, descriptorCopies.descriptorCount, &dstTypeSize);
659 
660 	ASSERT(srcTypeSize == dstTypeSize);
661 	size_t writeSize = dstTypeSize * descriptorCopies.descriptorCount;
662 	memcpy(memToWrite, memToRead, writeSize);
663 }
664 
665 }  // namespace vk
666