• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
PipelineLayout(const VkPipelineLayoutCreateInfo * pCreateInfo,void * mem)20 PipelineLayout::PipelineLayout(const VkPipelineLayoutCreateInfo *pCreateInfo, void *mem)
21     : setLayoutCount(pCreateInfo->setLayoutCount)
22     , 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 
destroy(const VkAllocationCallbacks * pAllocator)50 void PipelineLayout::destroy(const VkAllocationCallbacks *pAllocator)
51 {
52 	vk::deallocate(setLayouts, pAllocator);  // pushConstantRanges are in the same allocation
53 }
54 
ComputeRequiredAllocationSize(const VkPipelineLayoutCreateInfo * pCreateInfo)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 
getNumDescriptorSets() const62 size_t PipelineLayout::getNumDescriptorSets() const
63 {
64 	return setLayoutCount;
65 }
66 
getDescriptorSetLayout(size_t descriptorSet) const67 DescriptorSetLayout const *PipelineLayout::getDescriptorSetLayout(size_t descriptorSet) const
68 {
69 	ASSERT(descriptorSet < setLayoutCount);
70 	return setLayouts[descriptorSet];
71 }
72 
getDynamicOffsetBase(size_t descriptorSet) const73 uint32_t PipelineLayout::getDynamicOffsetBase(size_t descriptorSet) const
74 {
75 	ASSERT(descriptorSet < setLayoutCount);
76 	return dynamicOffsetBases[descriptorSet];
77 }
78 
79 }  // namespace vk
80