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-resource-cache.h>
20 #include <dali/graphics/vulkan/vulkan-buffer.h>
21 #include <dali/graphics/vulkan/vulkan-image.h>
22 #include <dali/graphics/vulkan/vulkan-pipeline.h>
23 #include <dali/graphics/vulkan/vulkan-shader.h>
24 #include <dali/graphics/vulkan/vulkan-descriptor-set.h>
25 #include <dali/graphics/vulkan/vulkan-framebuffer.h>
26 #include <dali/graphics/vulkan/vulkan-command-pool.h>
27 #include <dali/graphics/vulkan/vulkan-sampler.h>
38 ResourceCache& ResourceCache::AddBuffer( BufferRef buffer )
40 mBuffers.push_back( buffer );
44 ResourceCache& ResourceCache::AddImage( ImageRef image )
46 mImages.push_back( image );
50 ResourceCache& ResourceCache::AddPipeline( PipelineRef pipeline )
52 mPipelines.push_back( pipeline );
56 ResourceCache& ResourceCache::AddShader( ShaderRef shader )
58 mShaders.push_back( shader );
62 ResourceCache& ResourceCache::AddCommandPool( CommandPoolRef pool )
64 mCommandPools.push_back( pool );
68 ResourceCache& ResourceCache::AddDescriptorPool( DescriptorPoolRef pool )
70 mDescriptorPools.push_back( pool );
74 ResourceCache& ResourceCache::AddFramebuffer( FramebufferRef framebuffer )
76 mFramebuffers.push_back( framebuffer );
80 ResourceCache& ResourceCache::AddSampler( SamplerRef sampler )
82 mSamplers.push_back(sampler);
86 PipelineRef ResourceCache::FindPipeline( vk::Pipeline pipeline )
88 auto iterator = std::find_if(mPipelines.begin(),
90 [&](const PipelineRef entry) { return entry->GetVkHandle() == pipeline; });
92 return iterator == mPipelines.end() ? PipelineRef() : PipelineRef(&**iterator);
95 ShaderRef ResourceCache::FindShader( vk::ShaderModule shaderModule )
97 auto iterator = std::find_if(mShaders.begin(),
99 [&](const ShaderRef entry) { return entry->GetVkHandle() == shaderModule; });
101 return iterator == mShaders.end() ? ShaderRef() : ShaderRef(&**iterator);
104 CommandPoolRef ResourceCache::FindCommandPool( vk::CommandPool commandPool )
106 auto iterator = std::find_if(mCommandPools.begin(),
108 [&](const CommandPoolRef entry) { return entry->GetVkHandle() == commandPool; });
110 return iterator == mCommandPools.end() ? CommandPoolRef() : CommandPoolRef(&**iterator);
113 DescriptorPoolRef ResourceCache::FindDescriptorPool( vk::DescriptorPool descriptorPool )
115 auto iterator = std::find_if(mDescriptorPools.begin(),
116 mDescriptorPools.end(),
117 [&](const DescriptorPoolRef entry) { return entry->GetVkHandle() == descriptorPool; });
119 return iterator == mDescriptorPools.end() ? DescriptorPoolRef() : DescriptorPoolRef(&**iterator);
122 FramebufferRef ResourceCache::FindFramebuffer( vk::Framebuffer framebuffer )
124 auto iterator = std::find_if(mFramebuffers.begin(),
126 [&](const FramebufferRef entry) { return entry->GetVkHandle() == framebuffer; });
128 return iterator == mFramebuffers.end() ? FramebufferRef() : FramebufferRef(&**iterator);
131 SamplerRef ResourceCache::FindSampler( vk::Sampler sampler )
133 auto iterator = std::find_if(mSamplers.begin(),
135 [&](const SamplerRef entry) { return entry->GetVkHandle() == sampler; });
137 return iterator == mSamplers.end() ? SamplerRef() : SamplerRef(&**iterator);
140 BufferRef ResourceCache::FindBuffer( vk::Buffer buffer )
142 auto iterator = std::find_if(mBuffers.begin(),
144 [&](const BufferRef entry) { return entry->GetVkHandle() == buffer; });
146 return iterator == mBuffers.end() ? BufferRef() : BufferRef(&**iterator);
149 ImageRef ResourceCache::FindImage( vk::Image image )
151 auto iterator = std::find_if(mImages.begin(),
153 [&](const ImageRef entry) { return entry->GetVkHandle() == image; });
155 return iterator == mImages.end() ? ImageRef() : ImageRef(&**iterator);
158 ResourceCache& ResourceCache::RemoveBuffer( Buffer& buffer )
160 if( !mBuffers.empty() )
162 auto found = std::find_if(mBuffers.begin(),
164 [&](const BufferRef entry) { return &(*entry) == &buffer; });
166 std::iter_swap(found, std::prev(mBuffers.end()));
167 mBuffers.back().Reset();
173 ResourceCache& ResourceCache::RemoveImage( Image& image )
175 if( !mImages.empty() )
177 auto found = std::find_if(mImages.begin(),
179 [&](const ImageRef entry) { return &(*entry) == ℑ });
181 std::iter_swap(found, std::prev(mImages.end()));
182 mImages.back().Reset();
188 ResourceCache& ResourceCache::RemovePipeline( Pipeline &pipeline )
190 if( !mPipelines.empty() )
192 auto found = std::find_if(mPipelines.begin(),
194 [&](const PipelineRef entry) { return &(*entry) == &pipeline; });
196 std::iter_swap(found, std::prev(mPipelines.end()));
197 mPipelines.back().Reset();
198 mPipelines.pop_back();
203 ResourceCache& ResourceCache::RemoveShader( Shader& shader )
205 if( !mShaders.empty() )
207 auto iterator = std::find_if(mShaders.begin(),
209 [&](const ShaderRef entry) { return &*entry == &shader; });
211 std::iter_swap(iterator, std::prev(mShaders.end()));
212 mShaders.back().Reset();
218 ResourceCache& ResourceCache::RemoveCommandPool( CommandPool& commandPool )
220 if( !mCommandPools.empty() )
222 auto iterator = std::find_if(mCommandPools.begin(),
224 [&](const CommandPoolRef entry) { return &*entry == &commandPool; });
226 std::iter_swap(iterator, std::prev(mCommandPools.end()));
227 mCommandPools.back().Reset();
228 mCommandPools.pop_back();
233 ResourceCache& ResourceCache::RemoveDescriptorPool( DescriptorPool& descriptorPool )
235 if( !mDescriptorPools.empty() )
237 auto iterator = std::find_if(mDescriptorPools.begin(),
238 mDescriptorPools.end(),
239 [&](const DescriptorPoolRef entry) { return &*entry == &descriptorPool; });
241 std::iter_swap(iterator, std::prev(mDescriptorPools.end()));
242 mDescriptorPools.back().Reset();
243 mDescriptorPools.pop_back();
248 ResourceCache& ResourceCache::RemoveFramebuffer( Framebuffer &framebuffer )
250 if( !mFramebuffers.empty() )
252 auto iterator = std::find_if(mFramebuffers.begin(),
254 [&](const FramebufferRef entry) { return &*entry == &framebuffer; });
256 std::iter_swap(iterator, std::prev(mFramebuffers.end()));
257 mFramebuffers.back().Reset();
258 mFramebuffers.pop_back();
263 ResourceCache& ResourceCache::RemoveSampler( Sampler &sampler )
265 if( !mSamplers.empty() )
267 auto iterator = std::find_if(mSamplers.begin(),
269 [&](const SamplerRef entry) { return &*entry == &sampler; });
271 std::iter_swap(iterator, std::prev(mSamplers.end()));
272 mSamplers.back().Reset();
273 mSamplers.pop_back();
279 } //namespace Graphics