| OLD | NEW | 
|    1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |    1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 
|    2 // Use of this source code is governed by a BSD-style license that can be |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 #include "gpu/vulkan/vulkan_swap_chain.h" |    5 #include "gpu/vulkan/vulkan_swap_chain.h" | 
|    6  |    6  | 
|    7 #include "gpu/vulkan/vulkan_command_buffer.h" |    7 #include "gpu/vulkan/vulkan_command_buffer.h" | 
|    8 #include "gpu/vulkan/vulkan_command_pool.h" |    8 #include "gpu/vulkan/vulkan_command_pool.h" | 
 |    9 #include "gpu/vulkan/vulkan_device_queue.h" | 
|    9 #include "gpu/vulkan/vulkan_image_view.h" |   10 #include "gpu/vulkan/vulkan_image_view.h" | 
|   10 #include "gpu/vulkan/vulkan_implementation.h" |   11 #include "gpu/vulkan/vulkan_implementation.h" | 
|   11  |   12  | 
|   12 namespace gpu { |   13 namespace gpu { | 
|   13  |   14  | 
|   14 VulkanSwapChain::VulkanSwapChain() {} |   15 VulkanSwapChain::VulkanSwapChain() {} | 
|   15  |   16  | 
|   16 VulkanSwapChain::~VulkanSwapChain() { |   17 VulkanSwapChain::~VulkanSwapChain() { | 
|   17   DCHECK(images_.empty()); |   18   DCHECK(images_.empty()); | 
|   18   DCHECK_EQ(static_cast<VkSwapchainKHR>(VK_NULL_HANDLE), swap_chain_); |   19   DCHECK_EQ(static_cast<VkSwapchainKHR>(VK_NULL_HANDLE), swap_chain_); | 
|   19   DCHECK_EQ(static_cast<VkSemaphore>(VK_NULL_HANDLE), next_present_semaphore_); |   20   DCHECK_EQ(static_cast<VkSemaphore>(VK_NULL_HANDLE), next_present_semaphore_); | 
|   20 } |   21 } | 
|   21  |   22  | 
|   22 bool VulkanSwapChain::Initialize(VkSurfaceKHR surface, |   23 bool VulkanSwapChain::Initialize(VulkanDeviceQueue* device_queue, | 
 |   24                                  VkSurfaceKHR surface, | 
|   23                                  const VkSurfaceCapabilitiesKHR& surface_caps, |   25                                  const VkSurfaceCapabilitiesKHR& surface_caps, | 
|   24                                  const VkSurfaceFormatKHR& surface_format) { |   26                                  const VkSurfaceFormatKHR& surface_format) { | 
 |   27   DCHECK(device_queue); | 
 |   28   device_queue_ = device_queue; | 
|   25   return InitializeSwapChain(surface, surface_caps, surface_format) && |   29   return InitializeSwapChain(surface, surface_caps, surface_format) && | 
|   26          InitializeSwapImages(surface_caps, surface_format); |   30          InitializeSwapImages(surface_caps, surface_format); | 
|   27 } |   31 } | 
|   28  |   32  | 
|   29 void VulkanSwapChain::Destroy() { |   33 void VulkanSwapChain::Destroy() { | 
|   30   DestroySwapImages(); |   34   DestroySwapImages(); | 
|   31   DestroySwapChain(); |   35   DestroySwapChain(); | 
|   32 } |   36 } | 
|   33  |   37  | 
|   34 gfx::SwapResult VulkanSwapChain::SwapBuffers() { |   38 gfx::SwapResult VulkanSwapChain::SwapBuffers() { | 
|   35   VkResult result = VK_SUCCESS; |   39   VkResult result = VK_SUCCESS; | 
|   36  |   40  | 
|   37   VkDevice device = GetVulkanDevice(); |   41   VkDevice device = device_queue_->GetVulkanDevice(); | 
|   38   VkQueue queue = GetVulkanQueue(); |   42   VkQueue queue = device_queue_->GetVulkanQueue(); | 
|   39  |   43  | 
|   40   scoped_ptr<ImageData>& current_image_data = images_[current_image_]; |   44   scoped_ptr<ImageData>& current_image_data = images_[current_image_]; | 
|   41  |   45  | 
|   42   // Default image subresource range. |   46   // Default image subresource range. | 
|   43   VkImageSubresourceRange image_subresource_range = {}; |   47   VkImageSubresourceRange image_subresource_range = {}; | 
|   44   image_subresource_range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |   48   image_subresource_range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; | 
|   45   image_subresource_range.baseMipLevel = 0; |   49   image_subresource_range.baseMipLevel = 0; | 
|   46   image_subresource_range.levelCount = 1; |   50   image_subresource_range.levelCount = 1; | 
|   47   image_subresource_range.baseArrayLayer = 0; |   51   image_subresource_range.baseArrayLayer = 0; | 
|   48   image_subresource_range.layerCount = 1; |   52   image_subresource_range.layerCount = 1; | 
|   49  |   53  | 
|   50   // Submit our command buffer for the current buffer. |   54   // Submit our command buffer for the current buffer. | 
|   51   if (!current_image_data->command_buffer->Submit( |   55   if (!current_image_data->command_buffer->Submit( | 
|   52           queue, 1, ¤t_image_data->present_semaphore, 1, |   56           1, ¤t_image_data->present_semaphore, 1, | 
|   53           ¤t_image_data->render_semaphore)) { |   57           ¤t_image_data->render_semaphore)) { | 
|   54     return gfx::SwapResult::SWAP_FAILED; |   58     return gfx::SwapResult::SWAP_FAILED; | 
|   55   } |   59   } | 
|   56  |   60  | 
|   57   // Queue the present. |   61   // Queue the present. | 
|   58   VkPresentInfoKHR present_info = {}; |   62   VkPresentInfoKHR present_info = {}; | 
|   59   present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; |   63   present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; | 
|   60   present_info.waitSemaphoreCount = 1; |   64   present_info.waitSemaphoreCount = 1; | 
|   61   present_info.pWaitSemaphores = ¤t_image_data->render_semaphore; |   65   present_info.pWaitSemaphores = ¤t_image_data->render_semaphore; | 
|   62   present_info.swapchainCount = 1; |   66   present_info.swapchainCount = 1; | 
|   63   present_info.pSwapchains = &swap_chain_; |   67   present_info.pSwapchains = &swap_chain_; | 
|   64   present_info.pImageIndices = ¤t_image_; |   68   present_info.pImageIndices = ¤t_image_; | 
|   65  |   69  | 
|   66   result = vkQueuePresentKHR(queue, &present_info); |   70   result = vkQueuePresentKHR(queue, &present_info); | 
|   67   if (VK_SUCCESS != result) { |   71   if (VK_SUCCESS != result) { | 
|   68     return gfx::SwapResult::SWAP_FAILED; |   72     return gfx::SwapResult::SWAP_FAILED; | 
|   69   } |   73   } | 
|   70  |   74  | 
|   71   // Acquire then next image. |   75   // Acquire then next image. | 
|   72   result = vkAcquireNextImageKHR(device, swap_chain_, UINT64_MAX, |   76   result = vkAcquireNextImageKHR(device, swap_chain_, UINT64_MAX, | 
|   73                                  next_present_semaphore_, VK_NULL_HANDLE, |   77                                  next_present_semaphore_, VK_NULL_HANDLE, | 
|   74                                  ¤t_image_); |   78                                  ¤t_image_); | 
|   75   if (VK_SUCCESS != result) { |   79   if (VK_SUCCESS != result) { | 
 |   80     DLOG(ERROR) << "vkAcquireNextImageKHR() failed: " << result; | 
|   76     return gfx::SwapResult::SWAP_FAILED; |   81     return gfx::SwapResult::SWAP_FAILED; | 
|   77   } |   82   } | 
|   78  |   83  | 
|   79   // Swap in the "next_present_semaphore" into the newly acquired image. The |   84   // Swap in the "next_present_semaphore" into the newly acquired image. The | 
|   80   // old "present_semaphore" for the image becomes the place holder for the next |   85   // old "present_semaphore" for the image becomes the place holder for the next | 
|   81   // present semaphore for the next image. |   86   // present semaphore for the next image. | 
|   82   std::swap(images_[current_image_]->present_semaphore, |   87   std::swap(images_[current_image_]->present_semaphore, | 
|   83             next_present_semaphore_); |   88             next_present_semaphore_); | 
|   84  |   89  | 
|   85   return gfx::SwapResult::SWAP_ACK; |   90   return gfx::SwapResult::SWAP_ACK; | 
|   86 } |   91 } | 
|   87  |   92  | 
|   88 bool VulkanSwapChain::InitializeSwapChain( |   93 bool VulkanSwapChain::InitializeSwapChain( | 
|   89     VkSurfaceKHR surface, |   94     VkSurfaceKHR surface, | 
|   90     const VkSurfaceCapabilitiesKHR& surface_caps, |   95     const VkSurfaceCapabilitiesKHR& surface_caps, | 
|   91     const VkSurfaceFormatKHR& surface_format) { |   96     const VkSurfaceFormatKHR& surface_format) { | 
|   92   VkDevice device = GetVulkanDevice(); |   97   VkDevice device = device_queue_->GetVulkanDevice(); | 
|   93   VkResult result = VK_SUCCESS; |   98   VkResult result = VK_SUCCESS; | 
|   94  |   99  | 
|   95   VkSwapchainCreateInfoKHR swap_chain_create_info = {}; |  100   VkSwapchainCreateInfoKHR swap_chain_create_info = {}; | 
|   96   swap_chain_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; |  101   swap_chain_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; | 
|   97   swap_chain_create_info.surface = surface; |  102   swap_chain_create_info.surface = surface; | 
|   98   swap_chain_create_info.minImageCount = |  103   swap_chain_create_info.minImageCount = | 
|   99       std::max(2u, surface_caps.minImageCount); |  104       std::max(2u, surface_caps.minImageCount); | 
|  100   swap_chain_create_info.imageFormat = surface_format.format; |  105   swap_chain_create_info.imageFormat = surface_format.format; | 
|  101   swap_chain_create_info.imageColorSpace = surface_format.colorSpace; |  106   swap_chain_create_info.imageColorSpace = surface_format.colorSpace; | 
|  102   swap_chain_create_info.imageExtent = surface_caps.currentExtent; |  107   swap_chain_create_info.imageExtent = surface_caps.currentExtent; | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
|  120   Destroy(); |  125   Destroy(); | 
|  121  |  126  | 
|  122   swap_chain_ = new_swap_chain; |  127   swap_chain_ = new_swap_chain; | 
|  123   size_ = gfx::Size(swap_chain_create_info.imageExtent.width, |  128   size_ = gfx::Size(swap_chain_create_info.imageExtent.width, | 
|  124                     swap_chain_create_info.imageExtent.height); |  129                     swap_chain_create_info.imageExtent.height); | 
|  125  |  130  | 
|  126   return true; |  131   return true; | 
|  127 } |  132 } | 
|  128  |  133  | 
|  129 void VulkanSwapChain::DestroySwapChain() { |  134 void VulkanSwapChain::DestroySwapChain() { | 
|  130   VkDevice device = GetVulkanDevice(); |  135   VkDevice device = device_queue_->GetVulkanDevice(); | 
|  131  |  136  | 
|  132   if (swap_chain_ != VK_NULL_HANDLE) { |  137   if (swap_chain_ != VK_NULL_HANDLE) { | 
|  133     vkDestroySwapchainKHR(device, swap_chain_, nullptr); |  138     vkDestroySwapchainKHR(device, swap_chain_, nullptr); | 
|  134     swap_chain_ = VK_NULL_HANDLE; |  139     swap_chain_ = VK_NULL_HANDLE; | 
|  135   } |  140   } | 
|  136 } |  141 } | 
|  137  |  142  | 
|  138 bool VulkanSwapChain::InitializeSwapImages( |  143 bool VulkanSwapChain::InitializeSwapImages( | 
|  139     const VkSurfaceCapabilitiesKHR& surface_caps, |  144     const VkSurfaceCapabilitiesKHR& surface_caps, | 
|  140     const VkSurfaceFormatKHR& surface_format) { |  145     const VkSurfaceFormatKHR& surface_format) { | 
|  141   VkDevice device = GetVulkanDevice(); |  146   VkDevice device = device_queue_->GetVulkanDevice(); | 
|  142   VkResult result = VK_SUCCESS; |  147   VkResult result = VK_SUCCESS; | 
|  143  |  148  | 
|  144   uint32_t image_count = 0; |  149   uint32_t image_count = 0; | 
|  145   result = vkGetSwapchainImagesKHR(device, swap_chain_, &image_count, nullptr); |  150   result = vkGetSwapchainImagesKHR(device, swap_chain_, &image_count, nullptr); | 
|  146   if (VK_SUCCESS != result) { |  151   if (VK_SUCCESS != result) { | 
|  147     DLOG(ERROR) << "vkGetSwapchainImagesKHR(NULL) failed: " << result; |  152     DLOG(ERROR) << "vkGetSwapchainImagesKHR(NULL) failed: " << result; | 
|  148     return false; |  153     return false; | 
|  149   } |  154   } | 
|  150  |  155  | 
|  151   std::vector<VkImage> images(image_count); |  156   std::vector<VkImage> images(image_count); | 
| (...skipping 19 matching lines...) Expand all  Loading... | 
|  171   // The image memory barrier is used to setup the image layout. |  176   // The image memory barrier is used to setup the image layout. | 
|  172   VkImageMemoryBarrier image_memory_barrier = {}; |  177   VkImageMemoryBarrier image_memory_barrier = {}; | 
|  173   image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; |  178   image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; | 
|  174   image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; |  179   image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; | 
|  175   image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; |  180   image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; | 
|  176   image_memory_barrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; |  181   image_memory_barrier.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; | 
|  177   image_memory_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |  182   image_memory_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | 
|  178   image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; |  183   image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; | 
|  179   image_memory_barrier.subresourceRange = image_subresource_range; |  184   image_memory_barrier.subresourceRange = image_subresource_range; | 
|  180  |  185  | 
|  181   command_pool_ = CreateCommandPool(); |  186   command_pool_ = device_queue_->CreateCommandPool(); | 
|  182   if (!command_pool_) |  187   if (!command_pool_) | 
|  183     return false; |  188     return false; | 
|  184  |  189  | 
|  185   images_.resize(image_count); |  190   images_.resize(image_count); | 
|  186   for (uint32_t i = 0; i < image_count; ++i) { |  191   for (uint32_t i = 0; i < image_count; ++i) { | 
|  187     images_[i].reset(new ImageData); |  192     images_[i].reset(new ImageData); | 
|  188     scoped_ptr<ImageData>& image_data = images_[i]; |  193     scoped_ptr<ImageData>& image_data = images_[i]; | 
|  189     image_data->image = images[i]; |  194     image_data->image = images[i]; | 
|  190  |  195  | 
|  191     // Setup semaphores. |  196     // Setup semaphores. | 
| (...skipping 16 matching lines...) Expand all  Loading... | 
|  208  |  213  | 
|  209     // Setup the Image Layout as the first command that gets issued in each |  214     // Setup the Image Layout as the first command that gets issued in each | 
|  210     // command buffer. |  215     // command buffer. | 
|  211     ScopedSingleUseCommandBufferRecorder recorder(*image_data->command_buffer); |  216     ScopedSingleUseCommandBufferRecorder recorder(*image_data->command_buffer); | 
|  212     image_memory_barrier.image = images[i]; |  217     image_memory_barrier.image = images[i]; | 
|  213     vkCmdPipelineBarrier(recorder.handle(), VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |  218     vkCmdPipelineBarrier(recorder.handle(), VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, | 
|  214                          VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, 0, nullptr, 0, |  219                          VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, 0, nullptr, 0, | 
|  215                          nullptr, 1, &image_memory_barrier); |  220                          nullptr, 1, &image_memory_barrier); | 
|  216  |  221  | 
|  217     // Create the image view. |  222     // Create the image view. | 
|  218     image_data->image_view.reset(new VulkanImageView); |  223     image_data->image_view.reset(new VulkanImageView(device_queue_)); | 
|  219     if (!image_data->image_view->Initialize( |  224     if (!image_data->image_view->Initialize( | 
|  220             images[i], VK_IMAGE_VIEW_TYPE_2D, VulkanImageView::IMAGE_TYPE_COLOR, |  225             images[i], VK_IMAGE_VIEW_TYPE_2D, VulkanImageView::IMAGE_TYPE_COLOR, | 
|  221             surface_format.format, size_.width(), size_.height(), 0, 1, 0, 1)) { |  226             surface_format.format, size_.width(), size_.height(), 0, 1, 0, 1)) { | 
|  222       return false; |  227       return false; | 
|  223     } |  228     } | 
|  224   } |  229   } | 
|  225  |  230  | 
|  226   result = vkCreateSemaphore(device, &semaphore_create_info, nullptr, |  231   result = vkCreateSemaphore(device, &semaphore_create_info, nullptr, | 
|  227                              &next_present_semaphore_); |  232                              &next_present_semaphore_); | 
|  228   if (VK_SUCCESS != result) { |  233   if (VK_SUCCESS != result) { | 
|  229     DLOG(ERROR) << "vkCreateSemaphore(next_present) failed: " << result; |  234     DLOG(ERROR) << "vkCreateSemaphore(next_present) failed: " << result; | 
|  230     return false; |  235     return false; | 
|  231   } |  236   } | 
|  232  |  237  | 
|  233   // Acquire the initial buffer. |  238   // Acquire the initial buffer. | 
|  234   result = vkAcquireNextImageKHR(GetVulkanDevice(), swap_chain_, UINT64_MAX, |  239   result = vkAcquireNextImageKHR(device, swap_chain_, UINT64_MAX, | 
|  235                                  next_present_semaphore_, VK_NULL_HANDLE, |  240                                  next_present_semaphore_, VK_NULL_HANDLE, | 
|  236                                  ¤t_image_); |  241                                  ¤t_image_); | 
|  237   if (VK_SUCCESS != result) { |  242   if (VK_SUCCESS != result) { | 
|  238     DLOG(ERROR) << "vkAcquireNextImageKHR() failed: " << result; |  243     DLOG(ERROR) << "vkAcquireNextImageKHR() failed: " << result; | 
|  239     return false; |  244     return false; | 
|  240   } |  245   } | 
|  241  |  246  | 
|  242   std::swap(images_[current_image_]->present_semaphore, |  247   std::swap(images_[current_image_]->present_semaphore, | 
|  243             next_present_semaphore_); |  248             next_present_semaphore_); | 
|  244  |  249  | 
|  245   return true; |  250   return true; | 
|  246 } |  251 } | 
|  247  |  252  | 
|  248 void VulkanSwapChain::DestroySwapImages() { |  253 void VulkanSwapChain::DestroySwapImages() { | 
|  249   VkDevice device = GetVulkanDevice(); |  254   VkDevice device = device_queue_->GetVulkanDevice(); | 
|  250  |  255  | 
|  251   if (VK_NULL_HANDLE != next_present_semaphore_) { |  256   if (VK_NULL_HANDLE != next_present_semaphore_) { | 
|  252     vkDestroySemaphore(device, next_present_semaphore_, nullptr); |  257     vkDestroySemaphore(device, next_present_semaphore_, nullptr); | 
|  253     next_present_semaphore_ = VK_NULL_HANDLE; |  258     next_present_semaphore_ = VK_NULL_HANDLE; | 
|  254   } |  259   } | 
|  255  |  260  | 
|  256   for (const scoped_ptr<ImageData>& image_data : images_) { |  261   for (const scoped_ptr<ImageData>& image_data : images_) { | 
|  257     if (image_data->command_buffer) { |  262     if (image_data->command_buffer) { | 
|  258       // Make sure command buffer is done processing. |  263       // Make sure command buffer is done processing. | 
|  259       image_data->command_buffer->Wait(UINT64_MAX); |  264       image_data->command_buffer->Wait(UINT64_MAX); | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
|  287     command_pool_->Destroy(); |  292     command_pool_->Destroy(); | 
|  288     command_pool_.reset(); |  293     command_pool_.reset(); | 
|  289   } |  294   } | 
|  290 } |  295 } | 
|  291  |  296  | 
|  292 VulkanSwapChain::ImageData::ImageData() {} |  297 VulkanSwapChain::ImageData::ImageData() {} | 
|  293  |  298  | 
|  294 VulkanSwapChain::ImageData::~ImageData() {} |  299 VulkanSwapChain::ImageData::~ImageData() {} | 
|  295  |  300  | 
|  296 }  // namespace gpu |  301 }  // namespace gpu | 
| OLD | NEW |