blob: 1c80ca76f9d8461242f2c71c71c8ff8d43e689a6 [file] [log] [blame]
Alexis Hetu000df8b2018-10-24 15:22:41 -04001// 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 Hetu8c1e8f12019-02-15 16:41:12 -050016#include <cstring>
Alexis Hetu000df8b2018-10-24 15:22:41 -040017
18namespace vk
19{
20
21PipelineLayout::PipelineLayout(const VkPipelineLayoutCreateInfo* pCreateInfo, void* mem)
Alexis Hetu8c1e8f12019-02-15 16:41:12 -050022 : setLayoutCount(pCreateInfo->setLayoutCount), pushConstantRangeCount(pCreateInfo->pushConstantRangeCount)
Alexis Hetu000df8b2018-10-24 15:22:41 -040023{
Alexis Hetu8c1e8f12019-02-15 16:41:12 -050024 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 Clayton225a1302019-04-02 12:28:22 +010034 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 Hetu000df8b2018-10-24 15:22:41 -040044}
45
46void PipelineLayout::destroy(const VkAllocationCallbacks* pAllocator)
47{
Alexis Hetu8c1e8f12019-02-15 16:41:12 -050048 vk::deallocate(setLayouts, pAllocator); // pushConstantRanges are in the same allocation
Alexis Hetu000df8b2018-10-24 15:22:41 -040049}
50
51size_t PipelineLayout::ComputeRequiredAllocationSize(const VkPipelineLayoutCreateInfo* pCreateInfo)
52{
Alexis Hetu8c1e8f12019-02-15 16:41:12 -050053 return (pCreateInfo->setLayoutCount * sizeof(DescriptorSetLayout*)) +
Ben Clayton225a1302019-04-02 12:28:22 +010054 (pCreateInfo->pushConstantRangeCount * sizeof(VkPushConstantRange)) +
55 (pCreateInfo->setLayoutCount * sizeof(uint32_t)); // dynamicOffsetBases
Alexis Hetu000df8b2018-10-24 15:22:41 -040056}
57
Ben Clayton76e9bc02019-02-26 15:02:18 +000058size_t PipelineLayout::getNumDescriptorSets() const
59{
60 return setLayoutCount;
61}
62
Ben Clayton225a1302019-04-02 12:28:22 +010063DescriptorSetLayout const* PipelineLayout::getDescriptorSetLayout(size_t descriptorSet) const
Ben Clayton76e9bc02019-02-26 15:02:18 +000064{
65 ASSERT(descriptorSet < setLayoutCount);
Ben Clayton225a1302019-04-02 12:28:22 +010066 return setLayouts[descriptorSet];
67}
68
69uint32_t PipelineLayout::getDynamicOffsetBase(size_t descriptorSet) const
70{
71 ASSERT(descriptorSet < setLayoutCount);
72 return dynamicOffsetBases[descriptorSet];
Ben Clayton76e9bc02019-02-26 15:02:18 +000073}
74
Alexis Hetu000df8b2018-10-24 15:22:41 -040075} // namespace vk