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" |
16 | #include <cstring> |
17 | |
18 | namespace vk |
19 | { |
20 | |
21 | PipelineLayout::PipelineLayout(const VkPipelineLayoutCreateInfo* pCreateInfo, void* mem) |
22 | : setLayoutCount(pCreateInfo->setLayoutCount), pushConstantRangeCount(pCreateInfo->pushConstantRangeCount) |
23 | { |
24 | char* hostMem = reinterpret_cast<char*>(mem); |
25 | |
26 | size_t setLayoutsSize = pCreateInfo->setLayoutCount * sizeof(DescriptorSetLayout*); |
27 | setLayouts = reinterpret_cast<DescriptorSetLayout**>(hostMem); |
28 | for(uint32_t i = 0; i < pCreateInfo->setLayoutCount; i++) |
29 | { |
30 | setLayouts[i] = vk::Cast(pCreateInfo->pSetLayouts[i]); |
31 | } |
32 | hostMem += setLayoutsSize; |
33 | |
34 | size_t pushConstantRangesSize = pCreateInfo->pushConstantRangeCount * sizeof(VkPushConstantRange); |
35 | pushConstantRanges = reinterpret_cast<VkPushConstantRange*>(hostMem); |
36 | memcpy(pushConstantRanges, pCreateInfo->pPushConstantRanges, pushConstantRangesSize); |
37 | hostMem += pushConstantRangesSize; |
38 | |
39 | dynamicOffsetBases = reinterpret_cast<uint32_t*>(hostMem); |
40 | uint32_t dynamicOffsetBase = 0; |
41 | for (uint32_t i = 0; i < setLayoutCount; i++) |
42 | { |
43 | uint32_t dynamicDescriptorCount = setLayouts[i]->getDynamicDescriptorCount(); |
44 | ASSERT_OR_RETURN((dynamicOffsetBase + dynamicDescriptorCount) <= MAX_DESCRIPTOR_SET_COMBINED_BUFFERS_DYNAMIC); |
45 | dynamicOffsetBases[i] = dynamicOffsetBase; |
46 | dynamicOffsetBase += dynamicDescriptorCount; |
47 | } |
48 | } |
49 | |
50 | void PipelineLayout::destroy(const VkAllocationCallbacks* pAllocator) |
51 | { |
52 | vk::deallocate(setLayouts, pAllocator); // pushConstantRanges are in the same allocation |
53 | } |
54 | |
55 | size_t PipelineLayout::ComputeRequiredAllocationSize(const VkPipelineLayoutCreateInfo* pCreateInfo) |
56 | { |
57 | return (pCreateInfo->setLayoutCount * sizeof(DescriptorSetLayout*)) + |
58 | (pCreateInfo->pushConstantRangeCount * sizeof(VkPushConstantRange)) + |
59 | (pCreateInfo->setLayoutCount * sizeof(uint32_t)); // dynamicOffsetBases |
60 | } |
61 | |
62 | size_t PipelineLayout::getNumDescriptorSets() const |
63 | { |
64 | return setLayoutCount; |
65 | } |
66 | |
67 | DescriptorSetLayout const* PipelineLayout::getDescriptorSetLayout(size_t descriptorSet) const |
68 | { |
69 | ASSERT(descriptorSet < setLayoutCount); |
70 | return setLayouts[descriptorSet]; |
71 | } |
72 | |
73 | uint32_t PipelineLayout::getDynamicOffsetBase(size_t descriptorSet) const |
74 | { |
75 | ASSERT(descriptorSet < setLayoutCount); |
76 | return dynamicOffsetBases[descriptorSet]; |
77 | } |
78 | |
79 | } // namespace vk |
80 |