1 /*
2  * Copyright (c) 2021 The Khronos Group Inc.
3  * Copyright (c) 2021 Valve Corporation
4  * Copyright (c) 2021 LunarG, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and/or associated documentation files (the "Materials"), to
8  * deal in the Materials without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Materials, and to permit persons to whom the Materials are
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice(s) and this permission notice shall be included in
14  * all copies or substantial portions of the Materials.
15  *
16  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  *
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
23  * USE OR OTHER DEALINGS IN THE MATERIALS.
24  *
25  * Author: Charles Giessen <charles@lunarg.com>
26  */
27 
28 #pragma once
29 
30 #include "test_util.h"
31 
32 #include "layer/layer_util.h"
33 
34 #include "loader/generated/vk_layer_dispatch_table.h"
35 
36 /*
37 Interface Version 0
38 */
39 
40 /*
41 must export the following: -- always exported
42 vkEnumerateInstanceLayerProperties
43 vkEnumerateInstanceExtensionProperties
44 Must export the following but nothing -- always exported
45 vkEnumerateDeviceLayerProperties
46 vkEnumerateDeviceExtensionProperties
47 */
48 
49 // export test_layer_GetInstanceProcAddr(instance, pName)
50 // TEST_LAYER_EXPORT_LAYER_NAMED_GIPA
51 // or (single layer binary)
52 // export vkGetInstanceProcAddr
53 // TEST_LAYER_EXPORT_NO_NAME_GIPA
54 
55 // export test_layer_GetDeviceProcAddr(device, pName)
56 // TEST_LAYER_EXPORT_LAYER_NAMED_GDPA
57 // or (single layer binary)
58 // export vkGetDeviceProcAddr
59 // TEST_LAYER_EXPORT_NO_NAME_GDPA
60 
61 /*
62 Interface Version 1
63 */
64 // export GetInstanceProcAddr
65 // TEST_LAYER_EXPORT_NO_PREFIX_GIPA
66 
67 // export GetDeviceProcAddr
68 // TEST_LAYER_EXPORT_NO_PREFIX_GDPA
69 
70 // Layer Manifest can override the names of the GetInstanceProcAddr and GetDeviceProcAddrfunctions
71 
72 /*
73 Interface Version 2
74 */
75 // export vk_layerGetPhysicalDeviceProcAddr
76 // TEST_LAYER_EXPORT_GET_PHYSICAL_DEVICE_PROC_ADDR
77 
78 // export vkNegotiateLoaderLayerInterfaceVersion
79 // TEST_LAYER_EXPORT_NEGOTIATE_LOADER_LAYER_INTERFACE_VERSION
80 
81 // Added manifest version 1.1.0
82 
83 struct TestLayer;
84 
85 // Callbacks allow tests to implement custom functionality without modifying the layer binary
86 // TestLayer* layer - Access to the TestLayer object itself
87 // void* data - pointer to test specific thing, used to pass data from the test into the TestLayer
88 // Returns VkResult - This value will be used as the return value of the function
89 using FP_layer_callback = VkResult (*)(TestLayer& layer, void* data);
90 
91 struct TestLayer {
92     fs::path manifest_file_path;
93     uint32_t manifest_version = VK_MAKE_VERSION(1, 1, 2);
94 
95     BUILDER_VALUE(TestLayer, bool, is_meta_layer, false)
96 
97     BUILDER_VALUE(TestLayer, std::string, unique_name, {})
98     BUILDER_VALUE(TestLayer, uint32_t, api_version, VK_MAKE_VERSION(1, 0, 0))
99     BUILDER_VALUE(TestLayer, uint32_t, implementation_version, 2)
100     BUILDER_VALUE(TestLayer, uint32_t, min_implementation_version, 0)
101     BUILDER_VALUE(TestLayer, std::string, description, {})
102 
103     BUILDER_VECTOR(TestLayer, std::string, alternative_function_names, alternative_function_name)
104 
105     BUILDER_VECTOR(TestLayer, Extension, instance_extensions, instance_extension)
106     BUILDER_VECTOR(TestLayer, Extension, device_extensions, device_extension)
107 
108     BUILDER_VALUE(TestLayer, std::string, enable_environment, {});
109     BUILDER_VALUE(TestLayer, std::string, disable_environment, {});
110 
111     BUILDER_VECTOR(TestLayer, LayerDefinition, meta_component_layers, meta_component_layer);
112 
113     BUILDER_VALUE(TestLayer, bool, intercept_vkEnumerateInstanceExtensionProperties, false)
114     BUILDER_VALUE(TestLayer, bool, intercept_vkEnumerateInstanceLayerProperties, false)
115     // Called in vkCreateInstance after calling down the chain & returning
116     BUILDER_VALUE(TestLayer, std::function<VkResult(TestLayer& layer)>, create_instance_callback, {})
117     // Called in vkCreateDevice after calling down the chain & returning
118     BUILDER_VALUE(TestLayer, std::function<VkResult(TestLayer& layer)>, create_device_callback, {})
119 
120     PFN_vkGetInstanceProcAddr next_vkGetInstanceProcAddr = VK_NULL_HANDLE;
121     PFN_vkGetDeviceProcAddr next_vkGetDeviceProcAddr = VK_NULL_HANDLE;
122 
123     VkInstance instance_handle;
124     VkLayerInstanceDispatchTable instance_dispatch_table;
125 
126     struct Device {
127         VkDevice device_handle;
128         VkLayerDispatchTable dispatch_table;
129     };
130     std::vector<Device> created_devices;
131 };
132 
133 using GetTestLayerFunc = TestLayer* (*)();
134 #define GET_TEST_LAYER_FUNC_STR "get_test_layer_func"
135 
136 using GetNewTestLayerFunc = TestLayer* (*)();
137 #define RESET_LAYER_FUNC_STR "reset_layer_func"