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 #include "VkDevice.hpp"
17 #include "VkImageView.hpp"
18 #include "VkPipelineLayout.hpp"
19 
20 namespace vk {
21 
ParseDescriptors(const Array & descriptorSets,const PipelineLayout * layout,Device * device,NotificationType notificationType)22 void DescriptorSet::ParseDescriptors(const Array &descriptorSets, const PipelineLayout *layout, Device *device, NotificationType notificationType)
23 {
24 	if(layout)
25 	{
26 		uint32_t descriptorSetCount = layout->getDescriptorSetCount();
27 		ASSERT(descriptorSetCount <= MAX_BOUND_DESCRIPTOR_SETS);
28 
29 		for(uint32_t i = 0; i < descriptorSetCount; ++i)
30 		{
31 			DescriptorSet *descriptorSet = descriptorSets[i];
32 			if(!descriptorSet)
33 			{
34 				continue;
35 			}
36 
37 			marl::lock lock(descriptorSet->header.mutex);
38 			uint32_t bindingCount = layout->getBindingCount(i);
39 			for(uint32_t j = 0; j < bindingCount; ++j)
40 			{
41 				VkDescriptorType type = layout->getDescriptorType(i, j);
42 				uint32_t descriptorCount = layout->getDescriptorCount(i, j);
43 				uint32_t descriptorSize = layout->getDescriptorSize(i, j);
44 				uint8_t *descriptorMemory = descriptorSet->data + layout->getBindingOffset(i, j);
45 
46 				for(uint32_t k = 0; k < descriptorCount; k++)
47 				{
48 					ImageView *memoryOwner = nullptr;
49 					switch(type)
50 					{
51 						case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
52 						case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
53 							memoryOwner = reinterpret_cast<SampledImageDescriptor *>(descriptorMemory)->memoryOwner;
54 							break;
55 						case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
56 						case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
57 							memoryOwner = reinterpret_cast<StorageImageDescriptor *>(descriptorMemory)->memoryOwner;
58 							break;
59 						default:
60 							break;
61 					}
62 					if(memoryOwner)
63 					{
64 						if(notificationType == PREPARE_FOR_SAMPLING)
65 						{
66 							device->prepareForSampling(memoryOwner);
67 						}
68 						else if((notificationType == CONTENTS_CHANGED) && (type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE))
69 						{
70 							device->contentsChanged(memoryOwner);
71 						}
72 					}
73 					descriptorMemory += descriptorSize;
74 				}
75 			}
76 		}
77 	}
78 }
79 
ContentsChanged(const Array & descriptorSets,const PipelineLayout * layout,Device * device)80 void DescriptorSet::ContentsChanged(const Array &descriptorSets, const PipelineLayout *layout, Device *device)
81 {
82 	ParseDescriptors(descriptorSets, layout, device, CONTENTS_CHANGED);
83 }
84 
PrepareForSampling(const Array & descriptorSets,const PipelineLayout * layout,Device * device)85 void DescriptorSet::PrepareForSampling(const Array &descriptorSets, const PipelineLayout *layout, Device *device)
86 {
87 	ParseDescriptors(descriptorSets, layout, device, PREPARE_FOR_SAMPLING);
88 }
89 
90 }  // namespace vk