1 #ifndef DALI_GRAPHICS_VULKAN_RESOURCE_CACHE
2 #define DALI_GRAPHICS_VULKAN_RESOURCE_CACHE
5 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 #ifndef VULKAN_HPP_NO_EXCEPTIONS
22 #define VULKAN_HPP_NO_EXCEPTIONS
26 #include <dali/graphics/vulkan/vulkan-types.h>
37 * Stores and manages Vulkan resources
39 class ResourceCache final
43 * Adds the provided buffer object to the buffer cache
44 * @param buffer The buffer object to be added to the cache
45 * @return A reference to the ResourceCache
47 ResourceCache& AddBuffer( RefCountedBuffer buffer );
50 * Adds the provided image object to the image cache
51 * @param image The image object to be added to the cache
52 * @return A reference to the ResourceCache
54 ResourceCache& AddImage( RefCountedImage image );
57 * Adds the provided image view object to the image view cache
58 * @param imageView The image view object to be added to the cache
59 * @return A reference to the ResourceCache
61 ResourceCache& AddImageView( RefCountedImageView imageView );
64 * Adds the provided shader object to the pipeline cache
65 * @param shader The shader object to be added to the cache
66 * @return A reference to the ResourceCache
68 ResourceCache& AddShader( RefCountedShader shader );
71 * Adds the provided command pool object to the command pool cache
72 * @param pool The command pool object to be added to the cache
73 * @return A reference to the ResourceCache
75 ResourceCache& AddCommandPool( RefCountedCommandPool pool );
78 * Adds the provided descriptor pool object to the descriptor pool cache
79 * @param pool The descriptor pool object to be added to the cache
80 * @return A reference to the ResourceCache
82 ResourceCache& AddDescriptorPool( RefCountedDescriptorPool pool );
85 * Adds the provided framebuffer object to the framebuffer cache
86 * @param framebuffer The framebuffer object to be added to the cache
87 * @return A reference to the ResourceCache
89 ResourceCache& AddFramebuffer( RefCountedFramebuffer framebuffer );
92 * Adds the provided sampler object to the sampler cache
93 * @param sampler The sampler object to be added to the cache
94 * @return A reference to the ResourceCache
96 ResourceCache& AddSampler( RefCountedSampler sampler );
99 * Adds the provided fence object to the fence cache
100 * @param fence The fence object to be added to the cache
101 * @return A reference to the ResourceCache
103 ResourceCache& AddFence( RefCountedFence fence );
106 * Finds the buffer object using the specified Vulkan handle in the cache
107 * @param buffer The Vulkan handle of the buffer object to be found
108 * @return A Handle to the buffer object if found. An empty Handle otherwise
110 RefCountedBuffer FindBuffer( vk::Buffer buffer );
113 * Finds the image object using the specified Vulkan handle in the cache
114 * @param image The Vulkan handle of the image object to be found
115 * @return A handle to the Image object if found. An empty Handle otherwise
117 RefCountedImage FindImage( vk::Image image);
120 * Finds the image view object using the specified Vulkan handle
121 * @param imageView The Vulkan handle of the image view object to be found
122 * @return A handle to the ImageView object if found. An empty Handle otherwise
124 RefCountedImageView FindImageView( vk::ImageView imageView );
127 * Finds the shader module using the specified Vulkan handle
128 * @param shaderModule The Vulkan handle of the shader module to be found
129 * @return A Handle to the Shader module if found. An empty Handle otherwise
131 RefCountedShader FindShader( vk::ShaderModule shaderModule );
134 * Finds the CommandPool object using the specified Vulkan handle
135 * @param commandPool The Vulkan handle of the CommandPool object to be found
136 * @return A Handle to the CommandPool object if found. An empty Handle otherwise
138 RefCountedCommandPool FindCommandPool( vk::CommandPool commandPool );
141 * Finds the DescriptorPool object using the specified Vulkan handle
142 * @param descriptorPool The Vulkan handle of the DescriptorPool object to be found
143 * @return A Handle to the DescriptorPool object if found. An empty Handle otherwise
145 RefCountedDescriptorPool FindDescriptorPool( vk::DescriptorPool descriptorPool );
148 * Finds the Framebuffer object using the specified Vulkan handle
149 * @param framebuffer The Vulkan handle of the Framebuffer object to be found
150 * @return A Handle to the Framebuffer object if found. An empty Handle otherwise
152 RefCountedFramebuffer FindFramebuffer( vk::Framebuffer framebuffer );
155 * Finds the Sampler object using the specified Vulkan handle
156 * @param sampler The Vulkan handle of the Sampler object to be found
157 * @return A Handle to the Sampler object if found. An empty Handle otherwise
159 RefCountedSampler FindSampler( vk::Sampler sampler );
162 * Finds the Fence object using the specified Vulkan handle
163 * @param fence The Vulkan handle of the Fence object to be found
164 * @return A Handle to the Sampler object if found. An empty Handle otherwise
166 RefCountedFence FindFence( vk::Fence fence );
169 * Removes the specified Buffer from the cache
170 * @param buffer The Buffer to be removed
171 * @return A reference to the ResourceCache
173 ResourceCache& RemoveBuffer( Buffer& buffer );
176 * Removes the specified Image from the cache
177 * @param image The Image to be removed
178 * @return A reference to the ResourceCache
180 ResourceCache& RemoveImage( Image& image );
183 * Removes the specified ImageView from the cache
184 * @param imageView The ImageView to be removed
185 * @return A reference to the ResourceCache
187 ResourceCache& RemoveImageView( ImageView& imageView );
190 * Removes the specified Shader from the cache
191 * @param shader The Shader to be removed
192 * @return A reference to the ResourceCache
194 ResourceCache& RemoveShader( Shader& shader );
197 * Removes the specified CommandPool from the cache
198 * @param commandPool The CommandPool to be removed
199 * @return A reference to the ResourceCache
201 ResourceCache& RemoveCommandPool( CommandPool& commandPool );
204 * Removes the specified DescriptorPool from the cache
205 * @param descriptorPool The DescriptorPool to be removed
206 * @return A reference to the ResourceCache
208 ResourceCache& RemoveDescriptorPool( DescriptorPool& descriptorPool );
211 * Removes the specified Framebuffer from the cache
212 * @param framebuffer The DescriptorPool to be removed
213 * @return A reference to the ResourceCache
215 ResourceCache& RemoveFramebuffer( Framebuffer& framebuffer );
218 * Removes the specified Sampler from the cache
219 * @param sampler The Sampler to be removed
220 * @return A reference to the ResourceCache
222 ResourceCache& RemoveSampler( Sampler& sampler );
225 * Removes the specified Fence from the cache
226 * @param fence The Fence to be removed
227 * @return A reference to the ResourceCache
229 ResourceCache& RemoveFence( Fence& fence );
231 void CollectGarbage();
233 void EnqueueDiscardOperation( std::function<void()> deleter );
235 ResourceCache() = default;
237 // The cache should not be copyable
238 ResourceCache( const ResourceCache& other ) = delete;
240 ResourceCache& operator=( const ResourceCache& other ) = delete;
242 // The cache should not be movable
243 ResourceCache( ResourceCache&& other ) = delete;
245 ResourceCache&& operator=( ResourceCache&& other ) = delete;
248 std::vector< RefCountedBuffer > mBuffers;
249 std::vector< RefCountedImage > mImages;
250 std::vector< RefCountedImageView > mImageViews;
251 std::vector< RefCountedShader > mShaders;
252 std::vector< RefCountedCommandPool > mCommandPools;
253 std::vector< RefCountedDescriptorPool > mDescriptorPools;
254 std::vector< RefCountedFramebuffer > mFramebuffers;
255 std::vector< RefCountedSampler > mSamplers;
256 std::vector< RefCountedFence > mFences;
258 std::vector< std::function< void() > > mDiscardQueue;
262 } //namespace Graphics
265 #endif //DALI_GRAPHICS_VULKAN_RESOURCE_CACHE