Vulkan SDK for Android 1.1.1 Mali Developer Center
context.hpp
1 /* Copyright (c) 2016-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #ifndef FRAMEWORK_CONTEXT_HPP
22 #define FRAMEWORK_CONTEXT_HPP
23 
24 #include "command_buffer_manager.hpp"
25 #include "fence_manager.hpp"
26 #include "framework/common.hpp"
27 #include <memory>
28 #include <vector>
29 
30 namespace MaliSDK
31 {
32 class Platform;
33 
36 class Context
37 {
38 public:
45  Result onPlatformUpdate(Platform *pPlatform);
46 
49  VkDevice getDevice()
50  {
51  return device;
52  }
53 
56  VkPhysicalDevice getPhysicalDevice() const;
57 
60  VkQueue getGraphicsQueue()
61  {
62  return queue;
63  }
64 
68  {
69  return *pPlatform;
70  }
71 
79  VkCommandBuffer requestPrimaryCommandBuffer()
80  {
81  return perFrame[swapchainIndex]->commandManager.requestCommandBuffer();
82  }
83 
100  VkCommandBuffer requestSecondaryCommandBuffer(unsigned threadIndex)
101  {
102  return perFrame[swapchainIndex]->secondaryCommandManagers[threadIndex]->requestCommandBuffer();
103  }
104 
107  void submit(VkCommandBuffer cmdBuffer);
108 
118  void submitSwapchain(VkCommandBuffer cmdBuffer);
119 
131  VkSemaphore beginFrame(unsigned index, VkSemaphore acquireSemaphore)
132  {
133  swapchainIndex = index;
134  perFrame[swapchainIndex]->beginFrame();
135  return perFrame[swapchainIndex]->setSwapchainAcquireSemaphore(acquireSemaphore);
136  }
137 
143  void setRenderingThreadCount(unsigned count)
144  {
145  vkQueueWaitIdle(queue);
146  for (auto &pFrame : perFrame)
147  pFrame->setSecondaryCommandManagersCount(count);
148  renderingThreadCount = count;
149  }
150 
155  {
156  return perFrame[swapchainIndex]->fenceManager;
157  }
158 
162  const VkSemaphore &getSwapchainAcquireSemaphore() const
163  {
164  return perFrame[swapchainIndex]->swapchainAcquireSemaphore;
165  }
166 
170  const VkSemaphore &getSwapchainReleaseSemaphore() const
171  {
172  return perFrame[swapchainIndex]->swapchainReleaseSemaphore;
173  }
174 
175 private:
176  Platform *pPlatform = nullptr;
177  VkDevice device = VK_NULL_HANDLE;
178  VkQueue queue = VK_NULL_HANDLE;
179  unsigned swapchainIndex = 0;
180  unsigned renderingThreadCount = 0;
181 
182  struct PerFrame
183  {
184  PerFrame(VkDevice device, unsigned graphicsQueueIndex);
185  ~PerFrame();
186 
187  void beginFrame();
188  VkSemaphore setSwapchainAcquireSemaphore(VkSemaphore acquireSemaphore);
189  void setSwapchainReleaseSemaphore(VkSemaphore releaseSemaphore);
190  void setSecondaryCommandManagersCount(unsigned count);
191 
192  VkDevice device = VK_NULL_HANDLE;
193  FenceManager fenceManager;
194  CommandBufferManager commandManager;
195  std::vector<std::unique_ptr<CommandBufferManager>> secondaryCommandManagers;
196  VkSemaphore swapchainAcquireSemaphore = VK_NULL_HANDLE;
197  VkSemaphore swapchainReleaseSemaphore = VK_NULL_HANDLE;
198  unsigned queueIndex;
199  };
200  std::vector<std::unique_ptr<PerFrame>> perFrame;
201 
202  void submitCommandBuffer(VkCommandBuffer, VkSemaphore acquireSemaphore, VkSemaphore releaseSemaphore);
203  void waitIdle();
204 };
205 }
206 
207 #endif
The command buffer allocates command buffers and recycles them for us. This gives us a convenient int...
The Context is the primary way for samples to interact with the swapchain and get rendered images to ...
Definition: context.hpp:36
void setRenderingThreadCount(unsigned count)
Sets the number of worker threads which can use secondary command buffers. This call is blocking and ...
Definition: context.hpp:143
VkDevice getDevice()
Get the Vulkan device assigned to the context.
Definition: context.hpp:49
Platform & getPlatform()
Gets the current platform.
Definition: context.hpp:67
void submit(VkCommandBuffer cmdBuffer)
Submit a command buffer to the queue.
Definition: context.cpp:105
The platform class is to abstract the Vulkan implementation of a particular platform. It is not used directly by applications, but by the mainloop implementation which is OS specific.
Definition: platform.hpp:38
VkCommandBuffer requestPrimaryCommandBuffer()
Requests a reset primary command buffer.
Definition: context.hpp:79
Result onPlatformUpdate(Platform *pPlatform)
Called by the platform internally when platform either initializes itself or the swapchain has been r...
Definition: context.cpp:84
The FenceManager keeps track of fences which in turn are used to keep track of GPU progress...
FenceManager & getFenceManager()
Gets the fence manager for the current swapchain image. Used by the platform internally.
Definition: context.hpp:154
void submitSwapchain(VkCommandBuffer cmdBuffer)
Submit a command buffer to the queue which renders to the swapchain image.
Definition: context.cpp:110
VkQueue getGraphicsQueue()
Get the Vulkan graphics queue assigned to the context.
Definition: context.hpp:60
VkSemaphore beginFrame(unsigned index, VkSemaphore acquireSemaphore)
Called by the platform, begins a frame.
Definition: context.hpp:131
VkCommandBuffer requestSecondaryCommandBuffer(unsigned threadIndex)
Requests a reset secondary command buffer, suitable for rendering multithreaded.
Definition: context.hpp:100
VkPhysicalDevice getPhysicalDevice() const
Gets the Vulkan physical device assigned to the context.
Definition: context.cpp:44
const VkSemaphore & getSwapchainAcquireSemaphore() const
Gets the acquire semaphore for the swapchain. Used by the platform internally.
Definition: context.hpp:162
const VkSemaphore & getSwapchainReleaseSemaphore() const
Gets the release semaphore for the swapchain. Used by the platform internally.
Definition: context.hpp:170