Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 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 "VkPipelineLayout.hpp" |
Nicolas Capens | 5ab1f36 | 2020-04-22 01:39:36 -0400 | [diff] [blame] | 16 | |
| 17 | #include <atomic> |
Alexis Hetu | 8c1e8f1 | 2019-02-15 16:41:12 -0500 | [diff] [blame] | 18 | #include <cstring> |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 19 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 20 | namespace vk { |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 21 | |
Nicolas Capens | 5ab1f36 | 2020-04-22 01:39:36 -0400 | [diff] [blame] | 22 | static std::atomic<uint32_t> layoutIdentifierSerial = { 1 }; // Start at 1. 0 is invalid/void layout. |
| 23 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 24 | PipelineLayout::PipelineLayout(const VkPipelineLayoutCreateInfo *pCreateInfo, void *mem) |
Nicolas Capens | 5ab1f36 | 2020-04-22 01:39:36 -0400 | [diff] [blame] | 25 | : identifier(layoutIdentifierSerial++) |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 26 | , descriptorSetCount(pCreateInfo->setLayoutCount) |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 27 | , pushConstantRangeCount(pCreateInfo->pushConstantRangeCount) |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 28 | { |
Nicolas Capens | 026f7d0 | 2020-04-23 23:44:57 -0400 | [diff] [blame] | 29 | Binding *bindingStorage = reinterpret_cast<Binding *>(mem); |
| 30 | uint32_t dynamicOffsetIndex = 0; |
Nicolas Capens | 6f5ff49 | 2020-06-01 13:06:52 -0400 | [diff] [blame] | 31 | |
| 32 | descriptorSets[0].bindings = bindingStorage; // Used in destroy() for deallocation. |
| 33 | |
Alexis Hetu | f630b25 | 2019-05-30 09:47:38 -0400 | [diff] [blame] | 34 | for(uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) |
| 35 | { |
Nicolas Capens | 026f7d0 | 2020-04-23 23:44:57 -0400 | [diff] [blame] | 36 | const vk::DescriptorSetLayout *setLayout = vk::Cast(pCreateInfo->pSetLayouts[i]); |
| 37 | uint32_t bindingsArraySize = setLayout->getBindingsArraySize(); |
| 38 | descriptorSets[i].bindings = bindingStorage; |
| 39 | bindingStorage += bindingsArraySize; |
Nicolas Capens | 026f7d0 | 2020-04-23 23:44:57 -0400 | [diff] [blame] | 40 | descriptorSets[i].bindingCount = bindingsArraySize; |
| 41 | |
| 42 | for(uint32_t j = 0; j < bindingsArraySize; j++) |
| 43 | { |
| 44 | descriptorSets[i].bindings[j].descriptorType = setLayout->getDescriptorType(j); |
| 45 | descriptorSets[i].bindings[j].offset = setLayout->getBindingOffset(j); |
| 46 | descriptorSets[i].bindings[j].dynamicOffsetIndex = dynamicOffsetIndex; |
Nicolas Capens | eb68244 | 2020-06-01 15:24:52 -0400 | [diff] [blame] | 47 | descriptorSets[i].bindings[j].descriptorCount = setLayout->getDescriptorCount(j); |
Nicolas Capens | 026f7d0 | 2020-04-23 23:44:57 -0400 | [diff] [blame] | 48 | |
| 49 | if(DescriptorSetLayout::IsDescriptorDynamic(descriptorSets[i].bindings[j].descriptorType)) |
| 50 | { |
| 51 | dynamicOffsetIndex += setLayout->getDescriptorCount(j); |
| 52 | } |
| 53 | } |
Alexis Hetu | f630b25 | 2019-05-30 09:47:38 -0400 | [diff] [blame] | 54 | } |
Alexis Hetu | 8c1e8f1 | 2019-02-15 16:41:12 -0500 | [diff] [blame] | 55 | |
| 56 | size_t pushConstantRangesSize = pCreateInfo->pushConstantRangeCount * sizeof(VkPushConstantRange); |
Nicolas Capens | 026f7d0 | 2020-04-23 23:44:57 -0400 | [diff] [blame] | 57 | pushConstantRanges = reinterpret_cast<VkPushConstantRange *>(bindingStorage); |
Alexis Hetu | 8c1e8f1 | 2019-02-15 16:41:12 -0500 | [diff] [blame] | 58 | memcpy(pushConstantRanges, pCreateInfo->pPushConstantRanges, pushConstantRangesSize); |
Trevor David Black | 3ad285a | 2020-05-26 19:28:05 +0000 | [diff] [blame] | 59 | |
| 60 | incRefCount(); |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 63 | void PipelineLayout::destroy(const VkAllocationCallbacks *pAllocator) |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 64 | { |
Nicolas Capens | 026f7d0 | 2020-04-23 23:44:57 -0400 | [diff] [blame] | 65 | vk::deallocate(descriptorSets[0].bindings, pAllocator); // pushConstantRanges are in the same allocation |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Trevor David Black | 3ad285a | 2020-05-26 19:28:05 +0000 | [diff] [blame] | 68 | bool PipelineLayout::release(const VkAllocationCallbacks *pAllocator) |
| 69 | { |
| 70 | if(decRefCount() == 0) |
| 71 | { |
| 72 | vk::deallocate(descriptorSets[0].bindings, pAllocator); // pushConstantRanges are in the same allocation |
| 73 | return true; |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
Ben Clayton | 2ed93ab | 2019-12-17 20:38:03 +0000 | [diff] [blame] | 78 | size_t PipelineLayout::ComputeRequiredAllocationSize(const VkPipelineLayoutCreateInfo *pCreateInfo) |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 79 | { |
Nicolas Capens | 026f7d0 | 2020-04-23 23:44:57 -0400 | [diff] [blame] | 80 | uint32_t bindingsCount = 0; |
| 81 | for(uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) |
| 82 | { |
| 83 | bindingsCount += vk::Cast(pCreateInfo->pSetLayouts[i])->getBindingsArraySize(); |
| 84 | } |
| 85 | |
| 86 | return bindingsCount * sizeof(Binding) + // descriptorSets[] |
| 87 | pCreateInfo->pushConstantRangeCount * sizeof(VkPushConstantRange); // pushConstantRanges[] |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 88 | } |
| 89 | |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 90 | size_t PipelineLayout::getDescriptorSetCount() const |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 91 | { |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 92 | return descriptorSetCount; |
| 93 | } |
| 94 | |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 95 | uint32_t PipelineLayout::getBindingCount(uint32_t setNumber) const |
| 96 | { |
| 97 | return descriptorSets[setNumber].bindingCount; |
| 98 | } |
| 99 | |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 100 | uint32_t PipelineLayout::getDynamicOffsetIndex(uint32_t setNumber, uint32_t bindingNumber) const |
| 101 | { |
Nicolas Capens | 026f7d0 | 2020-04-23 23:44:57 -0400 | [diff] [blame] | 102 | ASSERT(setNumber < descriptorSetCount && bindingNumber < descriptorSets[setNumber].bindingCount); |
| 103 | return descriptorSets[setNumber].bindings[bindingNumber].dynamicOffsetIndex; |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 104 | } |
| 105 | |
Nicolas Capens | eb68244 | 2020-06-01 15:24:52 -0400 | [diff] [blame] | 106 | uint32_t PipelineLayout::getDescriptorCount(uint32_t setNumber, uint32_t bindingNumber) const |
| 107 | { |
| 108 | ASSERT(setNumber < descriptorSetCount && bindingNumber < descriptorSets[setNumber].bindingCount); |
| 109 | return descriptorSets[setNumber].bindings[bindingNumber].descriptorCount; |
| 110 | } |
| 111 | |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 112 | uint32_t PipelineLayout::getBindingOffset(uint32_t setNumber, uint32_t bindingNumber) const |
| 113 | { |
Nicolas Capens | 026f7d0 | 2020-04-23 23:44:57 -0400 | [diff] [blame] | 114 | ASSERT(setNumber < descriptorSetCount && bindingNumber < descriptorSets[setNumber].bindingCount); |
| 115 | return descriptorSets[setNumber].bindings[bindingNumber].offset; |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | VkDescriptorType PipelineLayout::getDescriptorType(uint32_t setNumber, uint32_t bindingNumber) const |
| 119 | { |
Nicolas Capens | 026f7d0 | 2020-04-23 23:44:57 -0400 | [diff] [blame] | 120 | ASSERT(setNumber < descriptorSetCount && bindingNumber < descriptorSets[setNumber].bindingCount); |
| 121 | return descriptorSets[setNumber].bindings[bindingNumber].descriptorType; |
Nicolas Capens | c7d5ec3 | 2020-04-22 01:11:37 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | uint32_t PipelineLayout::getDescriptorSize(uint32_t setNumber, uint32_t bindingNumber) const |
| 125 | { |
| 126 | return DescriptorSetLayout::GetDescriptorSize(getDescriptorType(setNumber, bindingNumber)); |
| 127 | } |
| 128 | |
| 129 | bool PipelineLayout::isDescriptorDynamic(uint32_t setNumber, uint32_t bindingNumber) const |
| 130 | { |
| 131 | return DescriptorSetLayout::IsDescriptorDynamic(getDescriptorType(setNumber, bindingNumber)); |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Trevor David Black | 3ad285a | 2020-05-26 19:28:05 +0000 | [diff] [blame] | 134 | uint32_t PipelineLayout::incRefCount() |
| 135 | { |
| 136 | return ++refCount; |
| 137 | } |
| 138 | |
| 139 | uint32_t PipelineLayout::decRefCount() |
| 140 | { |
| 141 | return --refCount; |
| 142 | } |
| 143 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 144 | } // namespace vk |