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" |
Alexis Hetu | 8c1e8f1 | 2019-02-15 16:41:12 -0500 | [diff] [blame] | 16 | #include <cstring> |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 17 | |
| 18 | namespace vk |
| 19 | { |
| 20 | |
| 21 | PipelineLayout::PipelineLayout(const VkPipelineLayoutCreateInfo* pCreateInfo, void* mem) |
Alexis Hetu | 8c1e8f1 | 2019-02-15 16:41:12 -0500 | [diff] [blame] | 22 | : setLayoutCount(pCreateInfo->setLayoutCount), pushConstantRangeCount(pCreateInfo->pushConstantRangeCount) |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 23 | { |
Alexis Hetu | 8c1e8f1 | 2019-02-15 16:41:12 -0500 | [diff] [blame] | 24 | char* hostMem = reinterpret_cast<char*>(mem); |
| 25 | |
| 26 | size_t setLayoutsSize = pCreateInfo->setLayoutCount * sizeof(DescriptorSetLayout*); |
| 27 | setLayouts = reinterpret_cast<DescriptorSetLayout**>(hostMem); |
| 28 | memcpy(setLayouts, pCreateInfo->pSetLayouts, setLayoutsSize); // Assumes Cast(VkDescriptorSetLayout) is nothing but a cast |
| 29 | hostMem += setLayoutsSize; |
| 30 | |
| 31 | size_t pushConstantRangesSize = pCreateInfo->pushConstantRangeCount * sizeof(VkPushConstantRange); |
| 32 | pushConstantRanges = reinterpret_cast<VkPushConstantRange*>(hostMem); |
| 33 | memcpy(pushConstantRanges, pCreateInfo->pPushConstantRanges, pushConstantRangesSize); |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 34 | hostMem += pushConstantRangesSize; |
| 35 | |
| 36 | dynamicOffsetBases = reinterpret_cast<uint32_t*>(hostMem); |
| 37 | uint32_t dynamicOffsetBase = 0; |
| 38 | for (uint32_t i = 0; i < setLayoutCount; i++) |
| 39 | { |
| 40 | ASSERT_OR_RETURN(dynamicOffsetBase < MAX_DESCRIPTOR_SET_COMBINED_BUFFERS_DYNAMIC); |
| 41 | dynamicOffsetBases[i] = dynamicOffsetBase; |
| 42 | dynamicOffsetBase += setLayouts[i]->getDynamicDescriptorCount(); |
| 43 | } |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void PipelineLayout::destroy(const VkAllocationCallbacks* pAllocator) |
| 47 | { |
Alexis Hetu | 8c1e8f1 | 2019-02-15 16:41:12 -0500 | [diff] [blame] | 48 | vk::deallocate(setLayouts, pAllocator); // pushConstantRanges are in the same allocation |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | size_t PipelineLayout::ComputeRequiredAllocationSize(const VkPipelineLayoutCreateInfo* pCreateInfo) |
| 52 | { |
Alexis Hetu | 8c1e8f1 | 2019-02-15 16:41:12 -0500 | [diff] [blame] | 53 | return (pCreateInfo->setLayoutCount * sizeof(DescriptorSetLayout*)) + |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 54 | (pCreateInfo->pushConstantRangeCount * sizeof(VkPushConstantRange)) + |
| 55 | (pCreateInfo->setLayoutCount * sizeof(uint32_t)); // dynamicOffsetBases |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 58 | size_t PipelineLayout::getNumDescriptorSets() const |
| 59 | { |
| 60 | return setLayoutCount; |
| 61 | } |
| 62 | |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 63 | DescriptorSetLayout const* PipelineLayout::getDescriptorSetLayout(size_t descriptorSet) const |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 64 | { |
| 65 | ASSERT(descriptorSet < setLayoutCount); |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 66 | return setLayouts[descriptorSet]; |
| 67 | } |
| 68 | |
| 69 | uint32_t PipelineLayout::getDynamicOffsetBase(size_t descriptorSet) const |
| 70 | { |
| 71 | ASSERT(descriptorSet < setLayoutCount); |
| 72 | return dynamicOffsetBases[descriptorSet]; |
Ben Clayton | 76e9bc0 | 2019-02-26 15:02:18 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Alexis Hetu | 000df8b | 2018-10-24 15:22:41 -0400 | [diff] [blame] | 75 | } // namespace vk |