• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <vulkan/vulkan_core.h>
16 
17 // Driver is used to load a Vulkan graphics driver and expose its functions.
18 // It is used by the unit tests to test the SwiftShader driver and optionally
19 // load a system vulkan driver to test against.
20 class Driver
21 {
22 public:
23 	Driver();
24 	~Driver();
25 
26 	// loadSwiftShader attempts to load the SwiftShader vulkan driver.
27 	// returns true on success, false on failure.
28 	bool loadSwiftShader();
29 
30 	// loadSystem attempts to load the system's vulkan driver.
31 	// returns true on success, false on failure.
32 	bool loadSystem();
33 
34 	// load attempts to load the vulkan driver from the given path.
35 	// returns true on success, false on failure.
36 	bool load(const char *path);
37 
38 	// unloads the currently loaded driver.
39 	// No-op if no driver is currently loaded.
40 	void unload();
41 
42 	// isLoaded returns true if the driver is currently loaded.
43 	bool isLoaded() const;
44 
45 	// resolve all the functions for the given VkInstance.
46 	bool resolve(VkInstance);
47 
48 	VKAPI_ATTR PFN_vkVoidFunction(VKAPI_CALL *vk_icdGetInstanceProcAddr)(VkInstance instance, const char *pName);
49 
50 	// Global vulkan function pointers.
51 #define VK_GLOBAL(N, R, ...) VKAPI_ATTR R(VKAPI_CALL *N)(__VA_ARGS__)
52 #include "VkGlobalFuncs.hpp"
53 #undef VK_GLOBAL
54 
55 	// Per-instance vulkan function pointers.
56 #define VK_INSTANCE(N, R, ...) VKAPI_ATTR R(VKAPI_CALL *N)(__VA_ARGS__)
57 #include "VkInstanceFuncs.hpp"
58 #undef VK_INSTANCE
59 
60 private:
61 	Driver(const Driver &) = delete;
62 	Driver(Driver &&) = delete;
63 	Driver &operator=(const Driver &) = delete;
64 
65 	// lookup searches the loaded driver for a symbol with the given name,
66 	// returning the address of this symbol if found, otherwise nullptr.
67 	void *lookup(const char *name);
68 
69 	// Helper function to lookup a symbol and cast it to the appropriate type.
70 	// Returns true if the symbol was found and assigned to ptr, otherwise
71 	// returns false.
72 	template<typename T>
73 	inline bool lookup(T *ptr, const char *name);
74 
75 	void *dll;
76 };
77 
78 template<typename T>
lookup(T * ptr,const char * name)79 bool Driver::lookup(T *ptr, const char *name)
80 {
81 	void *sym = lookup(name);
82 	if(sym == nullptr)
83 	{
84 		return false;
85 	}
86 	*ptr = reinterpret_cast<T>(sym);
87 	return true;
88 }