Implemented the ResourceCache class.
authorAngelos Gkountis <a.gkountis@samsung.com>
Thu, 17 May 2018 13:11:49 +0000 (14:11 +0100)
committeradam.b <jsr184@gmail.com>
Fri, 25 May 2018 15:51:13 +0000 (16:51 +0100)
Change-Id: I5485a83ef15e9c54050e4964bf5fccd13182decd

dali/graphics-api/graphics-api-sampler.h
dali/graphics/vulkan/vulkan-graphics.cpp
dali/graphics/vulkan/vulkan-resource-cache.cpp [new file with mode: 0644]
dali/graphics/vulkan/vulkan-resource-cache.h [new file with mode: 0644]

index c270a4c..d66d106 100644 (file)
@@ -38,7 +38,7 @@ public:
   virtual ~Sampler() = default;
 
 protected:
-  // derived types should not be moved direcly to prevent slicing
+  // derived types should not be moved directly to prevent slicing
   Sampler(Sampler&&) = default;
   Sampler& operator=(Sampler&&) = default;
 
index fbcb697..a898475 100644 (file)
@@ -94,7 +94,7 @@ Dali::Graphics::API::Controller& Graphics::GetController()
     mGfxController = Dali::Graphics::VulkanAPI::Controller::New(*this);
   }
 
-  return *mGfxController.get();
+  return *mGfxController;
 }
 
 std::vector<const char*> Graphics::PrepareDefaultInstanceExtensions()
diff --git a/dali/graphics/vulkan/vulkan-resource-cache.cpp b/dali/graphics/vulkan/vulkan-resource-cache.cpp
new file mode 100644 (file)
index 0000000..378b009
--- /dev/null
@@ -0,0 +1,278 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <dali/graphics/vulkan/vulkan-resource-cache.h>
+
+#include <dali/graphics/vulkan/vulkan-buffer.h>
+#include <dali/graphics/vulkan/vulkan-image.h>
+#include <dali/graphics/vulkan/vulkan-pipeline.h>
+#include <dali/graphics/vulkan/vulkan-shader.h>
+#include <dali/graphics/vulkan/vulkan-descriptor-set.h>
+#include <dali/graphics/vulkan/vulkan-framebuffer.h>
+
+#include <algorithm>
+
+namespace Dali
+{
+namespace Graphics
+{
+namespace Vulkan
+{
+
+ResourceCache& ResourceCache::AddBuffer( BufferRef buffer )
+{
+  mBuffers.push_back( buffer );
+  return *this;
+}
+
+ResourceCache& ResourceCache::AddImage( ImageRef image )
+{
+  mImages.push_back( image );
+  return *this;
+}
+
+ResourceCache& ResourceCache::AddPipeline( PipelineRef pipeline )
+{
+  mPipelines.push_back( pipeline );
+  return *this;
+}
+
+ResourceCache& ResourceCache::AddShader( ShaderRef shader )
+{
+  mShaders.push_back( shader );
+  return *this;
+}
+
+ResourceCache& ResourceCache::AddCommandPool( CommandPoolRef pool )
+{
+  mCommandPools.push_back( pool );
+  return *this;
+}
+
+ResourceCache& ResourceCache::AddDescriptorPool( DescriptorPoolRef pool )
+{
+  mDescriptorPools.push_back( pool );
+  return *this;
+}
+
+ResourceCache& ResourceCache::AddFramebuffer( FramebufferRef framebuffer )
+{
+  mFramebuffers.push_back( framebuffer );
+  return *this;
+}
+
+ResourceCache& ResourceCache::AddSampler( SamplerRef sampler )
+{
+  mSamplers.push_back(sampler);
+  return *this;
+}
+
+PipelineRef ResourceCache::FindPipeline( vk::Pipeline pipeline )
+{
+  auto iterator = std::find_if(mPipelines.begin(),
+                               mPipelines.end(),
+                               [&](const PipelineRef entry) { return &*entry == pipeline; });
+
+  return iterator == mPipelines.end() ? PipelineRef() : PipelineRef(&**iterator);
+}
+
+ShaderRef ResourceCache::FindShader( vk::ShaderModule shaderModule )
+{
+  auto iterator = std::find_if(mShaders.begin(),
+                               mShaders.end(),
+                               [&](const ShaderRef entry) { return &*entry == shaderModule; });
+
+  return iterator == mShaders.end() ? ShaderRef() : ShaderRef(&**iterator);
+}
+
+CommandPoolRef ResourceCache::FindCommandPool( vk::CommandPool commandPool )
+{
+  auto iterator = std::find_if(mCommandPools.begin(),
+                               mCommandPools.end(),
+                               [&](const CommandPoolRef entry) { return &*entry == commandPool; });
+
+  return iterator == mCommandPools.end() ? CommandPoolRef() : CommandPoolRef(&**iterator);
+}
+
+DescriptorPoolRef ResourceCache::FindDescriptorPool( vk::DescriptorPool descriptorPool )
+{
+  auto iterator = std::find_if(mDescriptorPools.begin(),
+                               mDescriptorPools.end(),
+                               [&](const DescriptorPoolRef entry) { return &*entry == descriptorPool; });
+
+  return iterator == mDescriptorPools.end() ? DescriptorPoolRef() : DescriptorPoolRef(&**iterator);
+}
+
+FramebufferRef ResourceCache::FindFramebuffer( vk::Framebuffer framebuffer )
+{
+  auto iterator = std::find_if(mFramebuffers.begin(),
+                               mFramebuffers.end(),
+                               [&](const FramebufferRef entry) { return &*entry == framebuffer; });
+
+  return iterator == mFramebuffers.end() ? FramebufferRef() : FramebufferRef(&**iterator);
+}
+
+SamplerRef ResourceCache::FindSampler( vk::Sampler sampler )
+{
+  auto iterator = std::find_if(mSamplers.begin(),
+                               mSamplers.end(),
+                               [&](const SamplerRef entry) { return &*entry == sampler; });
+
+  return iterator == mSamplers.end() ? SamplerRef() : SamplerRef(&**iterator);
+}
+
+BufferRef ResourceCache::FindBuffer( vk::Buffer buffer )
+{
+  auto iterator = std::find_if(mBuffers.begin(),
+                               mBuffers.end(),
+                               [&](const BufferRef entry) { return &*entry == buffer; });
+
+  return iterator == mBuffers.end() ? BufferRef() : BufferRef(&**iterator);
+}
+
+ImageRef ResourceCache::FindImage( vk::Image image )
+{
+  auto iterator = std::find_if(mImages.begin(),
+                               mImages.end(),
+                               [&](const ImageRef entry) { return &*entry == image; });
+
+  return iterator == mImages.end() ? ImageRef() : ImageRef(&**iterator);
+}
+
+ResourceCache& ResourceCache::RemoveBuffer( Buffer& buffer )
+{
+  if( !mBuffers.empty() )
+  {
+    auto found = std::find_if(mBuffers.begin(),
+                              mBuffers.end(),
+                              [&](const BufferRef entry) { return &(*entry) == &buffer; });
+
+    std::iter_swap(found, std::prev(mBuffers.end()));
+    mBuffers.back().Reset();
+    mBuffers.pop_back();
+  }
+  return *this;
+}
+
+ResourceCache& ResourceCache::RemoveImage( Image& image )
+{
+  if( !mImages.empty() )
+  {
+    auto found = std::find_if(mImages.begin(),
+                              mImages.end(),
+                              [&](const ImageRef entry) { return &(*entry) == &image; });
+
+    std::iter_swap(found, std::prev(mImages.end()));
+    mImages.back().Reset();
+    mImages.pop_back();
+  }
+  return *this;
+}
+
+ResourceCache& ResourceCache::RemovePipeline( Pipeline &pipeline )
+{
+  if( !mPipelines.empty() )
+  {
+    auto found = std::find_if(mPipelines.begin(),
+                              mPipelines.end(),
+                              [&](const PipelineRef entry) { return &(*entry) == &pipeline; });
+
+    std::iter_swap(found, std::prev(mPipelines.end()));
+    mPipelines.back().Reset();
+    mPipelines.pop_back();
+  }
+  return *this;
+}
+
+ResourceCache& ResourceCache::RemoveShader( Shader& shader )
+{
+  if( !mShaders.empty() )
+  {
+    auto iterator = std::find_if(mShaders.begin(),
+                                 mShaders.end(),
+                                 [&](const ShaderRef entry) { return &*entry == &shader; });
+
+    std::iter_swap(iterator, std::prev(mShaders.end());
+    mShaders.back().Reset();
+    mShaders.pop_back();
+  }
+  return *this;
+}
+
+ResourceCache& ResourceCache::RemoveCommandPool( CommandPool& commandPool )
+{
+  if( !mCommandPools.empty() )
+  {
+    auto iterator = std::find_if(mCommandPools.begin(),
+                                 mCommandPools.end(),
+                                 [&](const CommandPoolRef entry) { return &*entry == &commandPool; });
+
+    std::iter_swap(iterator, std::prev(mCommandPools.end()));
+    mCommandPools.back().Reset();
+    mCommandPools.pop_back();
+  }
+  return *this;
+}
+
+ResourceCache& ResourceCache::RemoveDescriptorPool( std::unique_ptr<DescriptorPool> descriptorPool )
+{
+  if( !mDescriptorPools.empty() )
+  {
+    auto iterator = std::find_if(mDescriptorPools.begin(),
+                                 mDescriptorPools.end(),
+                                 [&](const DescriptorPoolRef entry) { return &*entry == &*descriptorPool; });
+
+    std::iter_swap(iterator, std::prev(mDescriptorPools.end()));
+    mDescriptorPools.back().Reset();
+    mDescriptorPools.pop_back();
+  }
+  return *this;
+}
+
+ResourceCache& ResourceCache::RemoveFramebuffer( Framebuffer &framebuffer )
+{
+  if( !mFramebuffers.empty() )
+  {
+    auto iterator = std::find_if(mFramebuffers.begin(),
+                                 mFramebuffers.end(),
+                                 [&](const FramebufferRef entry) { return &*entry == &framebuffer; });
+
+    std::iter_swap(iterator, std::prev(mFramebuffers.end()));
+    mFramebuffers.back().Reset();
+    mFramebuffers.pop_back();
+  }
+  return *this;
+}
+
+ResourceCache& ResourceCache::RemoveSampler( Sampler &sampler )
+{
+  if( !mSamplers.empty() )
+  {
+    auto iterator = std::find_if(mSamplers.begin(),
+                                 mSamplers.end(),
+                                 [&](const SamplerRef entry) { return &*entry == &sampler; });
+
+    std::iter_swap(iterator, std::prev(mSamplers.end()));
+    mSamplers.back().Reset();
+    mSamplers.pop_back();
+  }
+  return *this;
+}
+
+} //namespace Vulkan
+} //namespace Graphics
+} //namespace Dali
diff --git a/dali/graphics/vulkan/vulkan-resource-cache.h b/dali/graphics/vulkan/vulkan-resource-cache.h
new file mode 100644 (file)
index 0000000..a23c024
--- /dev/null
@@ -0,0 +1,236 @@
+#ifndef DALI_GRAPHICS_VULKAN_RESOURCE_CACHE
+#define DALI_GRAPHICS_VULKAN_RESOURCE_CACHE
+
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef VULKAN_HPP_NO_EXCEPTIONS
+#define VULKAN_HPP_NO_EXCEPTIONS
+#endif
+
+// INTERNAL INCLUDES
+#include <dali/graphics/vulkan/vulkan-types.h>
+
+namespace Dali
+{
+namespace Graphics
+{
+namespace Vulkan
+{
+
+/**
+ * Stores and manages Vulkan resources
+ */
+class ResourceCache final
+{
+public:
+  /**
+   * Adds the provided buffer object to the buffer cache
+   * @param buffer The buffer object to be added to the cache
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& AddBuffer( BufferRef buffer );
+
+  /**
+   * Adds the provided image object to the image cache
+   * @param image The image object to be added to the cache
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& AddImage( ImageRef image );
+
+  /**
+   * Adds the provided pipeline object to the pipeline cache
+   * @param pipeline The pipeline object to be added to the cache
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& AddPipeline( PipelineRef pipeline );
+
+  /**
+   * Adds the provided shader object to the pipeline cache
+   * @param shader The shader object to be added to the cache
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& AddShader( ShaderRef shader );
+
+  /**
+   * Adds the provided command pool object to the command pool cache
+   * @param pool The command pool object to be added to the cache
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& AddCommandPool( CommandPoolRef pool );
+
+  /**
+   * Adds the provided descriptor pool object to the descriptor pool cache
+   * @param pool The descriptor pool object to be added to the cache
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& AddDescriptorPool( DescriptorPoolRef pool );
+
+  /**
+   * Adds the provided framebuffer object to the framebuffer cache
+   * @param framebuffer The framebuffer object to be added to the cache
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& AddFramebuffer( FramebufferRef framebuffer );
+
+  /**
+   * Adds the provided sampler object to the sampler cache
+   * @param sampler The sampler object to be added to the cache
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& AddSampler( SamplerRef sampler );
+
+
+  /**
+   * Finds the buffer object using the specified Vulkan handle in the cache
+   * @param buffer The Vulkan handle of the buffer object to be found
+   * @return A Handle to the buffer object if found. An empty Handle otherwise
+   */
+  BufferRef FindBuffer( vk::Buffer buffer );
+
+  /**
+   * Finds the image object using the specified Vulkan handle in the cache
+   * @param image The Vulkan handle of the image object to be found
+   * @return A handle to the Image object if found. An empty Handle otherwise
+   */
+  ImageRef FindImage( vk::Image image);
+
+  /**
+   * Finds the Pipeline object using the specified Vulkan handle in the cache
+   * @param pipeline The Vulkan handle of the Pipeline object to be found
+   * @return A Handle to the Pipeline object if found. An empty Handle otherwise
+   */
+  PipelineRef FindPipeline( vk::Pipeline pipeline );
+
+  /**
+   * Finds the shader module using the specified Vulkan handle in the cache
+   * @param shaderModule The Vulkan handle of the shader module to be found
+   * @return A Handle to the Shader module if found. An empty Handle otherwise
+   */
+  ShaderRef FindShader( vk::ShaderModule shaderModule );
+
+  /**
+   * Finds the CommandPool object using the specified Vulkan handle in the cache
+   * @param commandPool The Vulkan handle of the CommandPool object to be found
+   * @return A Handle to the CommandPool object if found. An empty Handle otherwise
+   */
+  CommandPoolRef FindCommandPool( vk::CommandPool commandPool );
+
+  /**
+   * Finds the DescriptorPool object using the specified Vulkan handle in the cache
+   * @param descriptorPool The Vulkan handle of the DescriptorPool object to be found
+   * @return A Handle to the DescriptorPool object if found. An empty Handle otherwise
+   */
+  DescriptorPoolRef FindDescriptorPool( vk::DescriptorPool descriptorPool );
+
+  /**
+   * Finds the Framebuffer object using the specified Vulkan handle in the cache
+   * @param framebuffer The Vulkan handle of the Framebuffer object to be found
+   * @return A Handle to the Framebuffer object if found. An empty Handle otherwise
+   */
+  FramebufferRef FindFramebuffer( vk::Framebuffer framebuffer );
+
+  /**
+   * Finds the Sampler object using the specified Vulkan handle in the cache.
+   * @param sampler The Vulkan handle of the Sampler object to be found
+   * @return A Handle to the Sampler object if found. An empty Handle otherwise
+   */
+  SamplerRef FindSampler( vk::Sampler sampler );
+
+  /**
+   * Removes the specified Buffer from the cache
+   * @param buffer The Buffer to be removed
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& RemoveBuffer( Buffer& buffer );
+
+  /**
+   * Removes the specified Image from the cache
+   * @param image The Image to be removed
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& RemoveImage( Image& image );
+
+  /**
+   * Removes the specified Pipeline from the cache
+   * @param pipeline The Pipeline to be removed
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& RemovePipeline( Pipeline& pipeline );
+
+  /**
+   * Removes the specified Shader from the cache
+   * @param shader The Shader to be removed
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& RemoveShader( Shader& shader );
+
+  /**
+   * Removes the specified CommandPool from the cache
+   * @param commandPool The CommandPool to be removed
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& RemoveCommandPool( CommandPool& commandPool );
+
+  /**
+   * Removes the specified DescriptorPool from the cache
+   * @param descriptorPool The DescriptorPool to be removed
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& RemoveDescriptorPool( std::unique_ptr<DescriptorPool> descriptorPool );
+
+  /**
+   * Removes the specified Framebuffer from the cache
+   * @param framebuffer The DescriptorPool to be removed
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& RemoveFramebuffer( Framebuffer& framebuffer );
+
+  /**
+   * Removes the specified Sampler from the cache
+   * @param sampler The Sampler to be removed
+   * @return A reference to the ResourceCache
+   */
+  ResourceCache& RemoveSampler( Sampler& sampler );
+
+
+  // The cache should not be copyable
+  ResourceCache( const ResourceCache& other ) = delete;
+
+  ResourceCache& operator=( const ResourceCache& other ) = delete;
+
+  // The cache should not be movable
+  ResourceCache( ResourceCache&& other ) = delete;
+
+  ResourceCache&& operator=( ResourceCache&& other ) = delete;
+
+private:
+  std::vector<BufferRef> mBuffers;
+  std::vector<ImageRef> mImages;
+  std::vector<PipelineRef> mPipelines;
+  std::vector<ShaderRef> mShaders;
+  std::vector<CommandPoolRef> mCommandPools;
+  std::vector<DescriptorPoolRef> mDescriptorPools;
+  std::vector<FramebufferRef> mFramebuffers;
+  std::vector<SamplerRef> mSamplers;
+};
+
+} //namespace Vulkan
+} //namespace Graphics
+} //namespace Dali
+
+#endif //DALI_GRAPHICS_VULKAN_RESOURCE_CACHE