blob: 98eb010ed9f00a378521f372b7a03db455700a31 [file] [log] [blame]
Alexis Hetu4f438a52020-06-15 16:13:51 -04001// 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"
Nicolas Capens06aaffa2021-11-10 10:19:50 -050016
Alexis Hetu72ea2ee2020-08-06 10:08:37 -040017#include "VkDevice.hpp"
Alexis Hetu4f438a52020-06-15 16:13:51 -040018#include "VkImageView.hpp"
19#include "VkPipelineLayout.hpp"
20
21namespace vk {
22
Alexis Hetu72ea2ee2020-08-06 10:08:37 -040023void DescriptorSet::ParseDescriptors(const Array &descriptorSets, const PipelineLayout *layout, Device *device, NotificationType notificationType)
Alexis Hetu4f438a52020-06-15 16:13:51 -040024{
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);
Nicolas Capens616af332022-04-29 14:01:32 -040045 uint8_t *descriptorMemory = descriptorSet->getDataAddress() + layout->getBindingOffset(i, j);
Alexis Hetu4f438a52020-06-15 16:13:51 -040046
47 for(uint32_t k = 0; k < descriptorCount; k++)
48 {
49 ImageView *memoryOwner = nullptr;
50 switch(type)
51 {
Nicolas Capens112faf42019-12-13 17:32:26 -050052 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;
Alexis Hetu4f438a52020-06-15 16:13:51 -040062 }
63 if(memoryOwner)
64 {
65 if(notificationType == PREPARE_FOR_SAMPLING)
66 {
Alexis Hetu72ea2ee2020-08-06 10:08:37 -040067 device->prepareForSampling(memoryOwner);
Alexis Hetu4f438a52020-06-15 16:13:51 -040068 }
69 else if((notificationType == CONTENTS_CHANGED) && (type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE))
70 {
Alexis Hetuca2ada82021-10-07 14:20:43 -040071 device->contentsChanged(memoryOwner, Image::USING_STORAGE);
Alexis Hetu4f438a52020-06-15 16:13:51 -040072 }
73 }
74 descriptorMemory += descriptorSize;
75 }
76 }
77 }
78 }
79}
80
Alexis Hetu72ea2ee2020-08-06 10:08:37 -040081void DescriptorSet::ContentsChanged(const Array &descriptorSets, const PipelineLayout *layout, Device *device)
Alexis Hetu4f438a52020-06-15 16:13:51 -040082{
Alexis Hetu72ea2ee2020-08-06 10:08:37 -040083 ParseDescriptors(descriptorSets, layout, device, CONTENTS_CHANGED);
Alexis Hetu4f438a52020-06-15 16:13:51 -040084}
85
Alexis Hetu72ea2ee2020-08-06 10:08:37 -040086void DescriptorSet::PrepareForSampling(const Array &descriptorSets, const PipelineLayout *layout, Device *device)
Alexis Hetu4f438a52020-06-15 16:13:51 -040087{
Alexis Hetu72ea2ee2020-08-06 10:08:37 -040088 ParseDescriptors(descriptorSets, layout, device, PREPARE_FOR_SAMPLING);
Alexis Hetu4f438a52020-06-15 16:13:51 -040089}
90
Nicolas Capens616af332022-04-29 14:01:32 -040091uint8_t *DescriptorSet::getDataAddress()
Ari Suonpääe6172192022-04-28 15:07:30 +030092{
Nicolas Capens616af332022-04-29 14:01:32 -040093 // 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.
Ari Suonpääe6172192022-04-28 15:07:30 +030098 return reinterpret_cast<uint8_t *>(this) + sizeof(DescriptorSetHeader);
99}
100
Alexis Hetu4f438a52020-06-15 16:13:51 -0400101} // namespace vk