2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali/graphics/vulkan/vulkan-descriptor-set.h>
19 #include <dali/graphics/vulkan/vulkan-graphics.h>
20 #include <dali/graphics/vulkan/vulkan-buffer.h>
21 #include <dali/graphics/vulkan/vulkan-image.h>
22 #include <dali/graphics/vulkan/vulkan-sampler.h>
30 struct DescriptorPool::Impl
32 Impl( DescriptorPool& owner, Graphics& graphics, vk::DescriptorPoolCreateInfo createInfo )
33 : mGraphics( graphics ),
35 mCreateInfo( createInfo )
46 * Allocates an array of descriptor sets
50 std::vector<Handle<DescriptorSet>> AllocateDescriptorSets( vk::DescriptorSetAllocateInfo allocateInfo )
52 // all other fields must be set correct
53 allocateInfo.setDescriptorPool( mVkDescriptorPool );
54 auto result = VkAssert( mGraphics.GetDevice().allocateDescriptorSets( allocateInfo ) );
56 std::vector<Handle<DescriptorSet>> retval;
57 retval.reserve( result.size() );
58 for( auto&& item : result )
60 Handle<DescriptorSet> handle( new DescriptorSet(mGraphics, mOwner, item, allocateInfo) );
61 retval.emplace_back( handle );
62 mDescriptorSetCache.emplace_back( handle );
70 mGraphics.GetDevice().resetDescriptorPool( mVkDescriptorPool );
71 mDescriptorSetCache.clear();
76 mVkDescriptorPool = VkAssert( mGraphics.GetDevice().createDescriptorPool( mCreateInfo, mGraphics.GetAllocator() ) );
81 DescriptorPool& mOwner;
82 vk::DescriptorPoolCreateInfo mCreateInfo;
84 vk::DescriptorPool mVkDescriptorPool;
87 std::vector<Handle<DescriptorSet>> mDescriptorSetCache;
90 Handle<DescriptorPool> DescriptorPool::New( Graphics& graphics, const vk::DescriptorPoolCreateInfo& createInfo )
92 auto pool = Handle<DescriptorPool>( new DescriptorPool( graphics, createInfo ) );
93 if(pool->mImpl->Initialise())
95 graphics.AddDescriptorPool(pool);
100 DescriptorPool::DescriptorPool( Graphics& graphics, const vk::DescriptorPoolCreateInfo& createInfo )
102 mImpl = MakeUnique<Impl>( *this, graphics, createInfo );
105 DescriptorPool::~DescriptorPool() = default;
107 vk::DescriptorPool DescriptorPool::GetVkDescriptorPool() const
109 return mImpl->mVkDescriptorPool;
112 std::vector<DescriptorSetHandle> DescriptorPool::AllocateDescriptorSets( vk::DescriptorSetAllocateInfo allocateInfo )
114 return mImpl->AllocateDescriptorSets( allocateInfo );
117 void DescriptorPool::Reset()
122 /****************************************************************************************
123 * Class DescriptorSet::Impl
126 struct DescriptorSet::Impl
128 Impl( Graphics& graphics, DescriptorPool& pool, vk::DescriptorSet set, vk::DescriptorSetAllocateInfo allocateInfo )
129 : mGraphics( graphics ),
131 mAllocateInfo( allocateInfo ),
132 mVkDescriptorSet( set )
141 // TODO: @todo non freeable!!!
142 //mGraphics.GetDevice().freeDescriptorSets( mPool.GetVkDescriptorPool(), 1, &mVkDescriptorSet );
146 void WriteUniformBuffer( uint32_t binding, Handle<Buffer> buffer, uint32_t offset, uint32_t size )
148 // add resource to the list
149 mResources.emplace_back( buffer.StaticCast<VkManaged>() );
151 auto bufferInfo = vk::DescriptorBufferInfo{}
152 .setOffset( U32(offset) )
153 .setRange( U32(size) )
154 .setBuffer( buffer->GetVkBuffer() );
156 auto write = vk::WriteDescriptorSet{}.setPBufferInfo( &bufferInfo )
157 .setDescriptorType( vk::DescriptorType::eUniformBuffer )
158 .setDescriptorCount( 1 )
159 .setDstSet( mVkDescriptorSet )
160 .setDstBinding( binding )
161 .setDstArrayElement( 0 );
163 // write descriptor set
164 mGraphics.GetDevice().updateDescriptorSets( 1, &write, 0, nullptr );
167 void WriteCombinedImageSampler( uint32_t binding, SamplerRef sampler, ImageViewRef imageView )
169 // add resource to the list
170 mResources.emplace_back( sampler.StaticCast<VkManaged>() );
171 mResources.emplace_back( imageView.StaticCast<VkManaged>() );
173 auto imageViewInfo = vk::DescriptorImageInfo{}
174 .setImageLayout( vk::ImageLayout::eShaderReadOnlyOptimal )
175 .setImageView( imageView->GetVkImageView() )
176 .setSampler( sampler->GetVkSampler() );
178 auto write = vk::WriteDescriptorSet{}.setPImageInfo( &imageViewInfo )
179 .setDescriptorType( vk::DescriptorType::eCombinedImageSampler )
180 .setDescriptorCount( 1 )
181 .setDstSet( mVkDescriptorSet )
182 .setDstBinding( binding )
183 .setDstArrayElement( 0 );
185 // write descriptor set
186 mGraphics.GetDevice().updateDescriptorSets( 1, &write, 0, nullptr );
189 void WriteStorageBuffer( Handle<Buffer> buffer, uint32_t offset, uint32_t size )
191 mResources.emplace_back( buffer.StaticCast<VkManaged>() );
195 DescriptorPool& mPool;
196 vk::DescriptorSetAllocateInfo mAllocateInfo;
197 vk::DescriptorSet mVkDescriptorSet;
199 // attached resources
200 std::vector<Handle<VkManaged>> mResources;
204 * Called by DescriptorPool only!
206 DescriptorSet::DescriptorSet( Graphics& graphics, DescriptorPool& pool, vk::DescriptorSet descriptorSet, vk::DescriptorSetAllocateInfo allocateInfo )
208 mImpl = MakeUnique<Impl>( graphics, pool, descriptorSet, allocateInfo );
211 DescriptorSet::~DescriptorSet() = default;
213 void DescriptorSet::WriteUniformBuffer( uint32_t binding, Handle<Buffer> buffer, uint32_t offset, uint32_t size )
215 mImpl->WriteUniformBuffer( binding, buffer, offset, size );
218 vk::DescriptorSet DescriptorSet::GetVkDescriptorSet() const
220 return mImpl->mVkDescriptorSet;
223 void DescriptorSet::WriteCombinedImageSampler( uint32_t binding, SamplerRef sampler, ImageViewRef imageView )
225 mImpl->WriteCombinedImageSampler( binding, sampler, imageView );
229 struct DescriptorSetLayout::Impl
231 Impl( Graphics& graphics, const vk::DescriptorSetLayoutCreateInfo& createInfo ) :
232 mGraphics( graphics ),
233 mCreateInfo( createInfo )
241 mGraphics.GetDevice().destroyDescriptorSetLayout( mLayout, mGraphics.GetAllocator() );
247 mLayout = VkAssert( mGraphics.GetDevice().createDescriptorSetLayout( mCreateInfo, mGraphics.GetAllocator() ));
256 vk::DescriptorSetLayout mLayout;
257 vk::DescriptorSetLayoutCreateInfo mCreateInfo;
261 * Class: DescriptorSetLayout
264 std::unique_ptr<DescriptorSetLayout> DescriptorSetLayout::New( Graphics& graphics, const vk::DescriptorSetLayoutCreateInfo& createInfo )
266 auto retval = std::unique_ptr<DescriptorSetLayout>( new DescriptorSetLayout(graphics, createInfo ) );
267 if( retval->mImpl->Initialise() )
274 DescriptorSetLayout::DescriptorSetLayout( Graphics& graphics, const vk::DescriptorSetLayoutCreateInfo& createInfo )
276 mImpl = MakeUnique<DescriptorSetLayout::Impl>( graphics, createInfo );
279 } // Namespace Vulkan
281 } // Namespace Graphics