Vulkan SDK for Android 1.1.1 Mali Developer Center
platform.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 PLATFORM_HPP
22 #define PLATFORM_HPP
23 
24 #include <string>
25 #include <vector>
26 
27 #include "asset_manager.hpp"
28 #include "framework/common.hpp"
29 #include "framework/context.hpp"
30 
31 namespace MaliSDK
32 {
33 
38 class Platform
39 {
40 public:
43  static Platform &get();
44 
46  virtual ~Platform() = default;
47 
49  Platform(Platform &&) = delete;
51  void operator=(Platform &&) = delete;
52 
55  {
57  unsigned width;
59  unsigned height;
61  VkFormat format;
62  };
63 
65  enum Status
66  {
69 
72  };
73 
76  inline Context &getContext()
77  {
78  return *pContext;
79  }
80 
83  virtual Result initialize() = 0;
84 
87  inline void addExternalLayer(const char *pName)
88  {
89  externalLayers.push_back(pName);
90  }
91 
96  inline void setExternalDebugCallback(PFN_vkDebugReportCallbackEXT callback, void *pUserData)
97  {
98  externalDebugCallback = callback;
100  }
101 
104  inline PFN_vkDebugReportCallbackEXT getExternalDebugCallback() const
105  {
106  return externalDebugCallback;
107  }
108 
111  inline void *getExternalDebugCallbackUserData() const
112  {
114  }
115 
119 
126  virtual Result createWindow(const SwapchainDimensions &swapchain) = 0;
127 
131  virtual void getCurrentSwapchain(std::vector<VkImage> *images, SwapchainDimensions *swapchain) = 0;
132 
135  virtual unsigned getNumSwapchainImages() const = 0;
136 
143  virtual Result acquireNextImage(unsigned *index) = 0;
144 
149  virtual Result presentImage(unsigned index) = 0;
150 
153  virtual Status getWindowStatus() = 0;
154 
156  virtual void terminate() = 0;
157 
160  inline VkDevice getDevice() const
161  {
162  return device;
163  }
164 
167  inline VkPhysicalDevice getPhysicalDevice() const
168  {
169  return gpu;
170  }
171 
174  inline VkInstance getInstance() const
175  {
176  return instance;
177  }
178 
181  inline VkQueue getGraphicsQueue() const
182  {
183  return queue;
184  }
185 
188  inline unsigned getGraphicsQueueIndex() const
189  {
190  return graphicsQueueIndex;
191  }
192 
195  inline const VkPhysicalDeviceProperties &getGpuProperties() const
196  {
197  return gpuProperties;
198  }
199 
202  inline const VkPhysicalDeviceMemoryProperties &getMemoryProperties() const
203  {
204  return memoryProperties;
205  }
206 
207 protected:
210  Platform() = default;
211 
213  VkInstance instance = VK_NULL_HANDLE;
214 
216  VkPhysicalDevice gpu = VK_NULL_HANDLE;
217 
219  VkDevice device = VK_NULL_HANDLE;
220 
222  VkQueue queue = VK_NULL_HANDLE;
223 
225  Context *pContext = nullptr;
226 
228  VkPhysicalDeviceProperties gpuProperties;
229 
231  VkPhysicalDeviceMemoryProperties memoryProperties;
232 
234  std::vector<VkQueueFamilyProperties> queueProperties;
235 
238 
240  std::vector<std::string> externalLayers;
241 
243  PFN_vkDebugReportCallbackEXT externalDebugCallback = nullptr;
246 
250  inline void addExternalLayers(std::vector<const char *> &activeLayers,
251  const std::vector<VkLayerProperties> &supportedLayers)
252  {
253  for (auto &layer : externalLayers)
254  {
255  for (auto &supportedLayer : supportedLayers)
256  {
257  if (layer == supportedLayer.layerName)
258  {
259  activeLayers.push_back(supportedLayer.layerName);
260  LOGI("Found external layer: %s\n", supportedLayer.layerName);
261  break;
262  }
263  }
264  }
265  }
266 };
267 }
268 
269 #endif
void addExternalLayer(const char *pName)
Adds an additional layer to be loaded on startup, if it exists.
Definition: platform.hpp:87
unsigned getGraphicsQueueIndex() const
Gets the current Vulkan graphics queue family index.
Definition: platform.hpp:188
void setExternalDebugCallback(PFN_vkDebugReportCallbackEXT callback, void *pUserData)
Sets an external debug callback handler. The callback will be called if the platform receives debug r...
Definition: platform.hpp:96
void operator=(Platform &&)=delete
Disallow copies and moves.
VkPhysicalDevice getPhysicalDevice() const
Gets the current Vulkan physical device.
Definition: platform.hpp:167
virtual Result initialize()=0
Initializes the platform.
VkFormat format
Pixel format of the swapchain.
Definition: platform.hpp:61
virtual void getCurrentSwapchain(std::vector< VkImage > *images, SwapchainDimensions *swapchain)=0
Gets the current swapchain.
Platform()=default
Protected constructor. Only platform implementations can create this class.
std::vector< std::string > externalLayers
List of external layers to load.
Definition: platform.hpp:240
const VkPhysicalDeviceMemoryProperties & getMemoryProperties() const
Gets the current Vulkan GPU memory properties.
Definition: platform.hpp:202
The Context is the primary way for samples to interact with the swapchain and get rendered images to ...
Definition: context.hpp:36
unsigned height
Height of the swapchain.
Definition: platform.hpp:59
unsigned graphicsQueueIndex
The queue family index where graphics work will be submitted.
Definition: platform.hpp:237
VkPhysicalDeviceProperties gpuProperties
The Vulkan physical device properties.
Definition: platform.hpp:228
void addExternalLayers(std::vector< const char *> &activeLayers, const std::vector< VkLayerProperties > &supportedLayers)
Helper function to add external layers to a list of active ones.
Definition: platform.hpp:250
Describes the size and format of the swapchain.
Definition: platform.hpp:54
unsigned width
Width of the swapchain.
Definition: platform.hpp:57
Status
Describes the status of the application lifecycle.
Definition: platform.hpp:65
virtual Result createWindow(const SwapchainDimensions &swapchain)=0
Creates a window with desired swapchain dimensions.
Context & getContext()
Gets the context owned by the platform.
Definition: platform.hpp:76
VkDevice getDevice() const
Gets the current Vulkan device.
Definition: platform.hpp:160
PFN_vkDebugReportCallbackEXT externalDebugCallback
External debug callback.
Definition: platform.hpp:243
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
const VkPhysicalDeviceProperties & getGpuProperties() const
Gets the current Vulkan GPU properties.
Definition: platform.hpp:195
PFN_vkDebugReportCallbackEXT getExternalDebugCallback() const
Returns the currently set debug callback.
Definition: platform.hpp:104
virtual Status getWindowStatus()=0
Gets current window status.
std::vector< VkQueueFamilyProperties > queueProperties
The Vulkan physical device queue properties.
Definition: platform.hpp:234
VkQueue queue
The Vulkan device queue.
Definition: platform.hpp:222
VkPhysicalDeviceMemoryProperties memoryProperties
The Vulkan physical device memory properties.
Definition: platform.hpp:231
void * getExternalDebugCallbackUserData() const
Returns the currently set debug callback.
Definition: platform.hpp:111
VkDevice device
The Vulkan device.
Definition: platform.hpp:219
VkInstance instance
The Vulkan instance.
Definition: platform.hpp:213
virtual Result presentImage(unsigned index)=0
Presents an image to the swapchain.
The application should exit as the user has requested it.
Definition: platform.hpp:71
virtual unsigned getNumSwapchainImages() const =0
Gets number of swapchain images used.
virtual Result acquireNextImage(unsigned *index)=0
At start of a frame, acquire the next swapchain image to render into.
void * pExternalDebugCallbackUserData
User-data for external debug callback.
Definition: platform.hpp:245
virtual void terminate()=0
Terminates the platform.
VkQueue getGraphicsQueue() const
Gets the current Vulkan graphics queue.
Definition: platform.hpp:181
The application is running.
Definition: platform.hpp:68
VkPhysicalDevice gpu
The Vulkan physical device.
Definition: platform.hpp:216
VkInstance getInstance() const
Gets the current Vulkan instance.
Definition: platform.hpp:174
virtual ~Platform()=default
Destructor.
virtual SwapchainDimensions getPreferredSwapchain()=0
Gets the preferred swapchain size. Not relevant for all platforms.
Context * pContext
The Vulkan context.
Definition: platform.hpp:225