Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 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" |
Nicolas Capens | 06aaffa | 2021-11-10 10:19:50 -0500 | [diff] [blame] | 16 | |
Alexis Hetu | 72ea2ee | 2020-08-06 10:08:37 -0400 | [diff] [blame] | 17 | #include "VkDevice.hpp" |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 18 | #include "VkImageView.hpp" |
| 19 | #include "VkPipelineLayout.hpp" |
| 20 | |
| 21 | namespace vk { |
| 22 | |
Alexis Hetu | 72ea2ee | 2020-08-06 10:08:37 -0400 | [diff] [blame] | 23 | void DescriptorSet::ParseDescriptors(const Array &descriptorSets, const PipelineLayout *layout, Device *device, NotificationType notificationType) |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 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); |
Nicolas Capens | 616af33 | 2022-04-29 14:01:32 -0400 | [diff] [blame] | 45 | uint8_t *descriptorMemory = descriptorSet->getDataAddress() + layout->getBindingOffset(i, j); |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 46 | |
| 47 | for(uint32_t k = 0; k < descriptorCount; k++) |
| 48 | { |
| 49 | ImageView *memoryOwner = nullptr; |
| 50 | switch(type) |
| 51 | { |
Nicolas Capens | 112faf4 | 2019-12-13 17:32:26 -0500 | [diff] [blame] | 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; |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 62 | } |
| 63 | if(memoryOwner) |
| 64 | { |
| 65 | if(notificationType == PREPARE_FOR_SAMPLING) |
| 66 | { |
Alexis Hetu | 72ea2ee | 2020-08-06 10:08:37 -0400 | [diff] [blame] | 67 | device->prepareForSampling(memoryOwner); |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 68 | } |
| 69 | else if((notificationType == CONTENTS_CHANGED) && (type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE)) |
| 70 | { |
Alexis Hetu | ca2ada8 | 2021-10-07 14:20:43 -0400 | [diff] [blame] | 71 | device->contentsChanged(memoryOwner, Image::USING_STORAGE); |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | descriptorMemory += descriptorSize; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
Alexis Hetu | 72ea2ee | 2020-08-06 10:08:37 -0400 | [diff] [blame] | 81 | void DescriptorSet::ContentsChanged(const Array &descriptorSets, const PipelineLayout *layout, Device *device) |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 82 | { |
Alexis Hetu | 72ea2ee | 2020-08-06 10:08:37 -0400 | [diff] [blame] | 83 | ParseDescriptors(descriptorSets, layout, device, CONTENTS_CHANGED); |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Alexis Hetu | 72ea2ee | 2020-08-06 10:08:37 -0400 | [diff] [blame] | 86 | void DescriptorSet::PrepareForSampling(const Array &descriptorSets, const PipelineLayout *layout, Device *device) |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 87 | { |
Alexis Hetu | 72ea2ee | 2020-08-06 10:08:37 -0400 | [diff] [blame] | 88 | ParseDescriptors(descriptorSets, layout, device, PREPARE_FOR_SAMPLING); |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Nicolas Capens | 616af33 | 2022-04-29 14:01:32 -0400 | [diff] [blame] | 91 | uint8_t *DescriptorSet::getDataAddress() |
Ari Suonpää | e617219 | 2022-04-28 15:07:30 +0300 | [diff] [blame] | 92 | { |
Nicolas Capens | 616af33 | 2022-04-29 14:01:32 -0400 | [diff] [blame] | 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. |
Ari Suonpää | e617219 | 2022-04-28 15:07:30 +0300 | [diff] [blame] | 98 | return reinterpret_cast<uint8_t *>(this) + sizeof(DescriptorSetHeader); |
| 99 | } |
| 100 | |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 101 | } // namespace vk |