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