Implemented the ResourceCache class.
[platform/core/uifw/dali-core.git] / dali / graphics / vulkan / vulkan-graphics.cpp
index d1ea784..a898475 100644 (file)
@@ -29,7 +29,8 @@
 #include <dali/graphics/vulkan/vulkan-shader.h>
 #include <dali/graphics/vulkan/vulkan-descriptor-set.h>
 #include <dali/graphics/vulkan/vulkan-framebuffer.h>
-#include <dali/graphics/vulkan/vulkan-graphics-controller.h>
+#include <dali/graphics/vulkan/api/vulkan-api-controller.h>
+#include <dali/graphics/vulkan/vulkan-pipeline-cache.h>
 
 #include <dali/graphics-api/graphics-api-controller.h>
 
@@ -45,8 +46,6 @@
 #define VK_KHR_XCB_SURFACE_EXTENSION_NAME "VK_KHR_xcb_surface"
 #endif
 
-#include <iostream>
-
 namespace Dali
 {
 namespace Graphics
@@ -58,24 +57,22 @@ namespace Vulkan
 const auto VALIDATION_LAYERS = std::vector< const char* >{
 
   //"VK_LAYER_LUNARG_screenshot",           // screenshot
-  "VK_LAYER_RENDERDOC_Capture",
-  "VK_LAYER_LUNARG_parameter_validation", // parameter
+  //"VK_LAYER_RENDERDOC_Capture",
+  //"VK_LAYER_LUNARG_parameter_validation", // parameter
   //"VK_LAYER_LUNARG_vktrace",              // vktrace ( requires vktrace connection )
-  "VK_LAYER_LUNARG_monitor",             // monitor
+  //"VK_LAYER_LUNARG_monitor",             // monitor
   "VK_LAYER_LUNARG_swapchain",           // swapchain
   "VK_LAYER_GOOGLE_threading",           // threading
   "VK_LAYER_LUNARG_api_dump",            // api
   "VK_LAYER_LUNARG_object_tracker",      // objects
   "VK_LAYER_LUNARG_core_validation",     // core
   "VK_LAYER_GOOGLE_unique_objects",      // unique objects
+  "VK_LAYER_GOOGLE_unique_objects",      // unique objects
   "VK_LAYER_LUNARG_standard_validation", // standard
 };
 
 Graphics::Graphics() = default;
-
 Graphics::~Graphics() = default;
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wframe-larger-than="
 
 Platform Graphics::GetDefaultPlatform() const
 {
@@ -94,10 +91,10 @@ Dali::Graphics::API::Controller& Graphics::GetController()
 {
   if(!mGfxController)
   {
-    mGfxController = Dali::Graphics::Vulkan::Controller::New(*this);
+    mGfxController = Dali::Graphics::VulkanAPI::Controller::New(*this);
   }
 
-  return *mGfxController.get();
+  return *mGfxController;
 }
 
 std::vector<const char*> Graphics::PrepareDefaultInstanceExtensions()
@@ -189,7 +186,7 @@ void Graphics::Create()
   {
     for( auto&& prop : layers.value )
     {
-      //std::cout << prop.layerName << std::endl;
+      std::cout << prop.layerName << std::endl;
       if( std::string(prop.layerName) == reqLayer )
       {
         validationLayers.push_back(reqLayer);
@@ -207,8 +204,8 @@ void Graphics::CreateInstance( const std::vector<const char*>& extensions, const
 
   info.setEnabledExtensionCount(U32(extensions.size()))
       .setPpEnabledExtensionNames(extensions.data())
-      //.setEnabledLayerCount(U32(validationLayers.size()))
-      .setEnabledLayerCount(0)
+      .setEnabledLayerCount(U32(validationLayers.size()))
+      //.setEnabledLayerCount(0)
       .setPpEnabledLayerNames(validationLayers.data());
 
   mInstance = VkAssert(vk::createInstance(info, *mAllocator));
@@ -257,10 +254,7 @@ void Graphics::PreparePhysicalDevice()
 
   mDeviceMemoryManager = GpuMemoryManager::New( *this );
 }
-#pragma GCC diagnostic pop
 
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wframe-larger-than="
 void Graphics::GetPhysicalDeviceProperties()
 {
   // store data on heap to keep object smaller
@@ -271,31 +265,69 @@ void Graphics::GetPhysicalDeviceProperties()
   mPhysicalDeviceFeatures =
     MakeUnique<vk::PhysicalDeviceFeatures>(mPhysicalDevice.getFeatures());
 }
-#pragma GCC diagnostic pop
 
 void Graphics::GetQueueFamilyProperties()
 {
-  mQueueFamilyProperties = std::move(mPhysicalDevice.getQueueFamilyProperties());
+  mQueueFamilyProperties = mPhysicalDevice.getQueueFamilyProperties();
 }
 
 FBID Graphics::CreateSurface(std::unique_ptr< SurfaceFactory > surfaceFactory)
 {
-  const auto vkFactory = dynamic_cast< const VkSurfaceFactory* >(surfaceFactory.get());
-  if(!vkFactory)
+  // create surface from the factory
+  auto surfaceRef = Surface::New( *this, std::move(surfaceFactory) );
+
+  if( surfaceRef->Create() )
   {
-    return FBID{0u};
+
+    // map surface to FBID
+    auto fbid = ++mBaseFBID;
+    mSurfaceFBIDMap[fbid] = SwapchainSurfacePair{ SwapchainRef{}, surfaceRef };
+    return fbid;
   }
+  return -1;
+}
 
-  auto surface = vkFactory->Create(mInstance, mAllocator.get(), mPhysicalDevice);
+SwapchainRef Graphics::CreateSwapchainForSurface( SurfaceRef surface )
+{
+  auto swapchain = Swapchain::New( *this,
+                                   GetGraphicsQueue(0u),
+                                   surface, 4, 0 );
 
-  // map surface to FBID
-  auto fbid             = ++mBaseFBID;
-  mSurfaceFBIDMap[fbid] = MakeUnique<Surface>(*this, surface, 3u);
-  return fbid;
+  // store swapchain in the correct pair
+  for( auto&& val : mSurfaceFBIDMap )
+  {
+    if( val.second.surface == surface )
+    {
+      val.second.swapchain = swapchain;
+      break;
+    }
+  }
+
+  return swapchain;
+}
+
+SwapchainRef Graphics::GetSwapchainForSurface( SurfaceRef surface )
+{
+  for( auto&& val : mSurfaceFBIDMap )
+  {
+    if( val.second.surface == surface )
+    {
+      return val.second
+                .swapchain;
+    }
+  }
+  return SwapchainRef();
+}
+
+SwapchainRef Graphics::GetSwapchainForFBID( FBID surfaceId )
+{
+  if(surfaceId == 0)
+  {
+    return mSurfaceFBIDMap.begin()->second.swapchain;
+  }
+  return mSurfaceFBIDMap[surfaceId].swapchain;
 }
 
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wframe-larger-than="
 std::vector< vk::DeviceQueueCreateInfo > Graphics::GetQueueCreateInfos()
 {
   // surface is needed in order to find a family that supports presentation to this surface
@@ -330,7 +362,7 @@ std::vector< vk::DeviceQueueCreateInfo > Graphics::GetQueueCreateInfos()
     {
       transferFamily = queueFamilyIndex;
     }
-    if(mPhysicalDevice.getSurfaceSupportKHR(queueFamilyIndex, mSurfaceFBIDMap.begin()->second.get()->GetSurfaceKHR())
+    if(mPhysicalDevice.getSurfaceSupportKHR(queueFamilyIndex, mSurfaceFBIDMap.begin()->second.surface->GetSurfaceKHR())
            .value &&
        presentFamily == -1u)
     {
@@ -375,10 +407,7 @@ std::vector< vk::DeviceQueueCreateInfo > Graphics::GetQueueCreateInfos()
 
   return queueInfos;
 }
-#pragma GCC diagnostic pop
 
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wframe-larger-than="
 void Graphics::CreateDevice()
 {
   auto queueInfos = GetQueueCreateInfos();
@@ -437,8 +466,9 @@ void Graphics::CreateDevice()
       // todo: present queue
     }
   }
+
+  mPipelineDatabase = std::make_unique<PipelineCache>( *this );
 }
-#pragma GCC diagnostic pop
 
 vk::Device Graphics::GetDevice() const
 {
@@ -489,7 +519,8 @@ Queue& Graphics::GetComputeQueue(uint32_t index) const
 
 Queue& Graphics::GetPresentQueue() const
 {
-  return *mPresentQueue.get();
+  // fixme: should be a dedicated presentation queue
+  return GetGraphicsQueue(0);
 }
 
 Handle< CommandPool > Graphics::CreateCommandPool(const vk::CommandPoolCreateInfo& info)
@@ -500,15 +531,15 @@ Handle< CommandPool > Graphics::CreateCommandPool(const vk::CommandPoolCreateInf
   return cmdpool;
 }
 
-Surface& Graphics::GetSurface( FBID surfaceId )
+SurfaceRef Graphics::GetSurface( FBID surfaceId )
 {
   // TODO: FBID == 0 means default framebuffer, but there should be no
   // such thing as default framebuffer.
   if( surfaceId == 0 )
   {
-    return *mSurfaceFBIDMap.begin()->second.get();
+    return mSurfaceFBIDMap.begin()->second.surface;
   }
-  return *mSurfaceFBIDMap[surfaceId].get();
+  return mSurfaceFBIDMap[surfaceId].surface;
 }
 
 // TODO: all this stuff should go into some vulkan cache
@@ -587,7 +618,7 @@ void Graphics::RemoveDescriptorPool( std::unique_ptr<DescriptorPool> pool )
   NotImplemented();
 }
 
-Handle<Shader> Graphics::FindShader( vk::ShaderModule shaderModule )
+ShaderRef Graphics::FindShader( vk::ShaderModule shaderModule )
 {
   for( auto&& iter : mShaderCache )
   {
@@ -599,6 +630,19 @@ Handle<Shader> Graphics::FindShader( vk::ShaderModule shaderModule )
   return Handle<Shader>();
 }
 
+ImageRef Graphics::FindImage( vk::Image image )
+{
+  for( auto&& iter : mImageCache )
+  {
+    if( iter->GetVkImage() == image )
+    {
+      return ImageRef(&*iter);
+    }
+  }
+  return ImageRef();
+}
+
+
 
 } // namespace Vulkan
 } // namespace Graphics