1 // Copyright 2020 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 "VkDescriptorSet.hpp"
16
17 #include "VkDevice.hpp"
18 #include "VkImageView.hpp"
19 #include "VkPipelineLayout.hpp"
20
21 namespace vk {
22
ParseDescriptors(const Array & descriptorSets,const PipelineLayout * layout,Device * device,NotificationType notificationType)23 void DescriptorSet::ParseDescriptors(const Array &descriptorSets, const PipelineLayout *layout, Device *device, NotificationType notificationType)
24 {
25 if(layout)
26 {
27 uint32_t descriptorSetCount = layout->getDescriptorSetCount();
28 ASSERT(descriptorSetCount <= MAX_BOUND_DESCRIPTOR_SETS);
29
30 for(uint32_t i = 0; i < descriptorSetCount; ++i)
31 {
32 DescriptorSet *descriptorSet = descriptorSets[i];
33 if(!descriptorSet)
34 {
35 continue;
36 }
37
38 marl::lock lock(descriptorSet->header.mutex);
39 uint32_t bindingCount = layout->getBindingCount(i);
40 for(uint32_t j = 0; j < bindingCount; ++j)
41 {
42 VkDescriptorType type = layout->getDescriptorType(i, j);
43 uint32_t descriptorCount = layout->getDescriptorCount(i, j);
44 uint32_t descriptorSize = layout->getDescriptorSize(i, j);
45 uint8_t *descriptorMemory = descriptorSet->getDataAddress() + layout->getBindingOffset(i, j);
46
47 for(uint32_t k = 0; k < descriptorCount; k++)
48 {
49 ImageView *memoryOwner = nullptr;
50 switch(type)
51 {
52 case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
53 case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
54 memoryOwner = reinterpret_cast<SampledImageDescriptor *>(descriptorMemory)->memoryOwner;
55 break;
56 case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
57 case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
58 memoryOwner = reinterpret_cast<StorageImageDescriptor *>(descriptorMemory)->memoryOwner;
59 break;
60 default:
61 break;
62 }
63 if(memoryOwner)
64 {
65 if(notificationType == PREPARE_FOR_SAMPLING)
66 {
67 device->prepareForSampling(memoryOwner);
68 }
69 else if((notificationType == CONTENTS_CHANGED) && (type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE))
70 {
71 device->contentsChanged(memoryOwner, Image::USING_STORAGE);
72 }
73 }
74 descriptorMemory += descriptorSize;
75 }
76 }
77 }
78 }
79 }
80
ContentsChanged(const Array & descriptorSets,const PipelineLayout * layout,Device * device)81 void DescriptorSet::ContentsChanged(const Array &descriptorSets, const PipelineLayout *layout, Device *device)
82 {
83 ParseDescriptors(descriptorSets, layout, device, CONTENTS_CHANGED);
84 }
85
PrepareForSampling(const Array & descriptorSets,const PipelineLayout * layout,Device * device)86 void DescriptorSet::PrepareForSampling(const Array &descriptorSets, const PipelineLayout *layout, Device *device)
87 {
88 ParseDescriptors(descriptorSets, layout, device, PREPARE_FOR_SAMPLING);
89 }
90
getDataAddress()91 uint8_t *DescriptorSet::getDataAddress()
92 {
93 // Descriptor sets consist of a header followed by a variable amount of descriptor data, depending
94 // on the descriptor set layout. Therefore the size of this class must match the size of the header.
95 static_assert(sizeof(DescriptorSet) == sizeof(DescriptorSetHeader));
96
97 // Return a pointer to the first byte after the header.
98 return reinterpret_cast<uint8_t *>(this) + sizeof(DescriptorSetHeader);
99 }
100
101 } // namespace vk