Skip to content
0.2.0-dev Download ZIP
471 lines ·22.5 KB Raw
1// Copyright (c) 2017-2026 The Khronos Group Inc.
2// Copyright (c) 2017-2019 Valve Corporation
3// Copyright (c) 2017-2019 LunarG, Inc.
4//
5// SPDX-License-Identifier: Apache-2.0 OR MIT
6//
7// Initial Author: Mark Young <marky@lunarg.com>
8//
9
10#include "api_layer_interface.hpp"
11
12#include "loader_init_data.hpp"
13#include "loader_logger.hpp"
14#include "loader_properties.hpp"
15#include "loader_platform.hpp"
16#include "manifest_file.hpp"
17#include "platform_utils.hpp"
18
19#include <openxr/openxr.h>
20#include <openxr/openxr_loader_negotiation.h>
21
22#include <algorithm>
23#include <cstring>
24#include <iterator>
25#include <memory>
26#include <sstream>
27#include <string>
28#include <utility>
29#include <vector>
30
31#define OPENXR_ENABLE_LAYERS_ENV_VAR "XR_ENABLE_API_LAYERS"
32
33// Add any layers defined in the loader layer environment variable.
34static void AddEnvironmentApiLayers(std::vector<std::string>& enabled_layers) {
35 std::string layers = LoaderProperty::Get(OPENXR_ENABLE_LAYERS_ENV_VAR);
36
37 std::size_t last_found = 0;
38 std::size_t found = layers.find_first_of(PATH_SEPARATOR);
39 std::string cur_search;
40
41 // Handle any path listings in the string (separated by the appropriate path separator)
42 while (found != std::string::npos) {
43 cur_search = layers.substr(last_found, found - last_found);
44 enabled_layers.push_back(cur_search);
45 last_found = found + 1;
46 found = layers.find_first_of(PATH_SEPARATOR, last_found);
47 }
48
49 // If there's something remaining in the string, copy it over
50 if (last_found < layers.size()) {
51 cur_search = layers.substr(last_found);
52 enabled_layers.push_back(cur_search);
53 }
54}
55
56XrResult ApiLayerInterface::GetApiLayerProperties(const std::string& openxr_command, uint32_t incoming_count,
57 uint32_t* outgoing_count, XrApiLayerProperties* api_layer_properties) {
58 std::vector<std::unique_ptr<ApiLayerManifestFile>> manifest_files;
59 uint32_t manifest_count = 0;
60 // Validate props struct before proceeding
61 if (0 < incoming_count && nullptr != api_layer_properties) {
62 for (uint32_t i = 0; i < incoming_count; i++) {
63 if (XR_TYPE_API_LAYER_PROPERTIES != api_layer_properties[i].type) {
64 LoaderLogger::LogErrorMessage(openxr_command,
65 "VUID-XrApiLayerProperties-type-type: unknown type in api_layer_properties");
66 return XR_ERROR_VALIDATION_FAILURE;
67 }
68 }
69 }
70
71 // "Independent of elementCapacityInput or elements parameters, elementCountOutput must be a valid pointer,
72 // and the function sets elementCountOutput." - 2.11
73 if (nullptr == outgoing_count) {
74 LoaderLogger::LogErrorMessage(openxr_command,
75 "VUID-xrEnumerateApiLayerProperties-propertyCountOutput-parameter: null propertyCountOutput");
76 return XR_ERROR_VALIDATION_FAILURE;
77 }
78
79 // Find any implicit layers which we may need to report information for.
80 XrResult result = ApiLayerManifestFile::FindManifestFiles(openxr_command, MANIFEST_TYPE_IMPLICIT_API_LAYER, manifest_files);
81 if (XR_SUCCEEDED(result)) {
82 // Find any explicit layers which we may need to report information for.
83 result = ApiLayerManifestFile::FindManifestFiles(openxr_command, MANIFEST_TYPE_EXPLICIT_API_LAYER, manifest_files);
84 }
85 if (XR_FAILED(result)) {
86 LoaderLogger::LogErrorMessage(openxr_command,
87 "ApiLayerInterface::GetApiLayerProperties - failed searching for API layer manifest files");
88 return result;
89 }
90
91 // check for potential overflow before static_cast<uint32_t>
92 if (manifest_files.size() >= UINT32_MAX) {
93 LoaderLogger::LogErrorMessage(openxr_command, "ApiLayerInterface::GetApiLayerProperties - too many API layers found");
94 return XR_ERROR_RUNTIME_FAILURE;
95 }
96
97 manifest_count = static_cast<uint32_t>(manifest_files.size());
98
99 *outgoing_count = manifest_count;
100 if (0 == incoming_count) {
101 // capacity check only
102 return XR_SUCCESS;
103 }
104 if (nullptr == api_layer_properties) {
105 // incoming_count is not 0 BUT the api_layer_properties is NULL
106 LoaderLogger::LogErrorMessage(openxr_command,
107 "VUID-xrEnumerateApiLayerProperties-properties-parameter: non-zero capacity but null array");
108 return XR_ERROR_VALIDATION_FAILURE;
109 }
110 if (incoming_count < manifest_count) {
111 LoaderLogger::LogErrorMessage(
112 openxr_command, "VUID-xrEnumerateApiLayerProperties-propertyCapacityInput-parameter: insufficient space in array");
113 return XR_ERROR_SIZE_INSUFFICIENT;
114 }
115
116 for (uint32_t prop = 0; prop < incoming_count && prop < manifest_count; ++prop) {
117 manifest_files[prop]->PopulateApiLayerProperties(api_layer_properties[prop]);
118 }
119 return XR_SUCCESS;
120}
121
122XrResult ApiLayerInterface::GetInstanceExtensionProperties(const std::string& openxr_command, const char* layer_name,
123 std::vector<XrExtensionProperties>& extension_properties) {
124 std::vector<std::unique_ptr<ApiLayerManifestFile>> manifest_files;
125
126 // If a layer name is supplied, only use the information out of that one layer
127 if (nullptr != layer_name && 0 != strlen(layer_name)) {
128 XrResult result = ApiLayerManifestFile::FindManifestFiles(openxr_command, MANIFEST_TYPE_IMPLICIT_API_LAYER, manifest_files);
129 if (XR_SUCCEEDED(result)) {
130 // Find any explicit layers which we may need to report information for.
131 result = ApiLayerManifestFile::FindManifestFiles(openxr_command, MANIFEST_TYPE_EXPLICIT_API_LAYER, manifest_files);
132 if (XR_FAILED(result)) {
133 LoaderLogger::LogErrorMessage(
134 openxr_command,
135 "ApiLayerInterface::GetInstanceExtensionProperties - failed searching for API layer manifest files");
136 return result;
137 }
138
139 bool found = false;
140 size_t num_files = manifest_files.size();
141 for (size_t man_file = 0; man_file < num_files; ++man_file) {
142 // If a layer with the provided name exists, get it's instance extension information.
143 if (manifest_files[man_file]->LayerName() == layer_name) {
144 manifest_files[man_file]->GetInstanceExtensionProperties(extension_properties);
145 found = true;
146 break;
147 }
148 }
149
150 // If nothing found, report 0
151 if (!found) {
152 return XR_ERROR_API_LAYER_NOT_PRESENT;
153 }
154 }
155 // Otherwise, we want to add only implicit API layers and explicit API layers enabled using the environment variables
156 } else {
157 XrResult result = ApiLayerManifestFile::FindManifestFiles(openxr_command, MANIFEST_TYPE_IMPLICIT_API_LAYER, manifest_files);
158 if (XR_SUCCEEDED(result)) {
159 // Find any environmentally enabled explicit layers. If they're present, treat them like implicit layers
160 // since we know that they're going to be enabled.
161 std::vector<std::string> env_enabled_layers;
162 AddEnvironmentApiLayers(env_enabled_layers);
163 if (!env_enabled_layers.empty()) {
164 std::vector<std::unique_ptr<ApiLayerManifestFile>> exp_layer_man_files = {};
165 result =
166 ApiLayerManifestFile::FindManifestFiles(openxr_command, MANIFEST_TYPE_EXPLICIT_API_LAYER, exp_layer_man_files);
167 if (XR_SUCCEEDED(result)) {
168 for (auto& exp_layer_man_file : exp_layer_man_files) {
169 for (std::string& enabled_layer : env_enabled_layers) {
170 // If this is an enabled layer, transfer it over to the manifest list.
171 if (enabled_layer == exp_layer_man_file->LayerName()) {
172 manifest_files.push_back(std::move(exp_layer_man_file));
173 break;
174 }
175 }
176 }
177 }
178 }
179 }
180
181 // Grab the layer instance extensions information
182 size_t num_files = manifest_files.size();
183 for (size_t man_file = 0; man_file < num_files; ++man_file) {
184 manifest_files[man_file]->GetInstanceExtensionProperties(extension_properties);
185 }
186 }
187 return XR_SUCCESS;
188}
189
190XrResult ApiLayerInterface::LoadApiLayers(const std::string& openxr_command, uint32_t enabled_api_layer_count,
191 const char* const* enabled_api_layer_names,
192 std::vector<std::unique_ptr<ApiLayerInterface>>& api_layer_interfaces) {
193 XrResult last_error = XR_SUCCESS;
194 std::unordered_set<std::string> layers_already_found;
195
196 bool any_loaded = false;
197 std::vector<std::unique_ptr<ApiLayerManifestFile>> enabled_layer_manifest_files_in_init_order = {};
198
199 // Find any implicit layers.
200 XrResult result = ApiLayerManifestFile::FindManifestFiles(openxr_command, MANIFEST_TYPE_IMPLICIT_API_LAYER,
201 enabled_layer_manifest_files_in_init_order);
202
203 for (const auto& enabled_layer_manifest_file : enabled_layer_manifest_files_in_init_order) {
204 layers_already_found.insert(enabled_layer_manifest_file->LayerName());
205 }
206
207 // Find any explicit layers.
208 std::vector<std::unique_ptr<ApiLayerManifestFile>> explicit_layer_manifest_files = {};
209
210 if (XR_SUCCEEDED(result)) {
211 result = ApiLayerManifestFile::FindManifestFiles(openxr_command, MANIFEST_TYPE_EXPLICIT_API_LAYER,
212 explicit_layer_manifest_files);
213 }
214
215 bool found_all_layers = true;
216
217 if (XR_SUCCEEDED(result)) {
218 // Put all explicit and then xrCreateInstance enabled layers into a string vector
219
220 std::vector<std::string> enabled_explicit_api_layer_names = {};
221
222 AddEnvironmentApiLayers(enabled_explicit_api_layer_names);
223
224 if (enabled_api_layer_count > 0) {
225 if (nullptr == enabled_api_layer_names) {
226 LoaderLogger::LogErrorMessage(
227 "xrCreateInstance",
228 "VUID-XrInstanceCreateInfo-enabledApiLayerNames-parameter: enabledApiLayerCount is non-0 but array is NULL");
229 LoaderLogger::LogErrorMessage(
230 "xrCreateInstance", "VUID-xrCreateInstance-info-parameter: something wrong with XrInstanceCreateInfo contents");
231 return XR_ERROR_VALIDATION_FAILURE;
232 }
233
234 std::copy(enabled_api_layer_names, enabled_api_layer_names + enabled_api_layer_count,
235 std::back_inserter(enabled_explicit_api_layer_names));
236 }
237
238 // add explicit layers to list of layers to enable
239 for (const auto& layer_name : enabled_explicit_api_layer_names) {
240 bool found_this_layer = false;
241
242 if (layers_already_found.count(layer_name) > 0) {
243 found_this_layer = true;
244 } else {
245 for (auto it = explicit_layer_manifest_files.begin(); it != explicit_layer_manifest_files.end();) {
246 bool erased_layer_manifest_file = false;
247
248 if (layer_name == (*it)->LayerName()) {
249 found_this_layer = true;
250 layers_already_found.insert(layer_name);
251 enabled_layer_manifest_files_in_init_order.push_back(std::move(*it));
252 it = explicit_layer_manifest_files.erase(it);
253 erased_layer_manifest_file = true;
254 }
255
256 if (!erased_layer_manifest_file) {
257 it++;
258 }
259 }
260 }
261
262 // If even one of the layers wasn't found, we want to return an error
263 if (!found_this_layer) {
264 found_all_layers = false;
265 std::string error_message = "ApiLayerInterface::LoadApiLayers - failed to find layer ";
266 error_message += layer_name;
267 LoaderLogger::LogErrorMessage(openxr_command, error_message);
268 }
269 }
270 }
271
272 for (std::unique_ptr<ApiLayerManifestFile>& manifest_file : enabled_layer_manifest_files_in_init_order) {
273 LoaderPlatformLibraryHandle layer_library = LoaderPlatformLibraryOpen(manifest_file->LibraryPath());
274 if (nullptr == layer_library) {
275 if (!any_loaded) {
276 last_error = XR_ERROR_FILE_ACCESS_ERROR;
277 }
278 std::string library_message = LoaderPlatformLibraryOpenError(manifest_file->LibraryPath());
279 std::string warning_message = "ApiLayerInterface::LoadApiLayers skipping layer ";
280 warning_message += manifest_file->LayerName();
281 warning_message += ", failed to load with message \"";
282 warning_message += library_message;
283 warning_message += "\"";
284 LoaderLogger::LogWarningMessage(openxr_command, warning_message);
285 continue;
286 }
287#if defined(XR_HAS_REQUIRED_PLATFORM_LOADER_INIT_STRUCT) // Cannot proceed without mandatory xrInitializeLoaderKHR call.
288 if (!LoaderInitData::instance().initialized()) {
289 LoaderLogger::LogErrorMessage(openxr_command, "ApiLayerInterface::LoadApiLayers skipping manifest file " +
290 manifest_file->Filename() +
291 " because xrInitializeLoaderKHR was not yet called.");
292
293 LoaderPlatformLibraryClose(layer_library);
294 return XR_ERROR_VALIDATION_FAILURE;
295 }
296#endif
297
298 bool forwardedInitLoader = false;
299 if (LoaderInitData::instance().getPlatformParam() != nullptr) {
300 // If we have xrInitializeLoaderKHR exposed as an export, forward call to it.
301 const auto function_name = manifest_file->GetFunctionName("xrInitializeLoaderKHR");
302 auto initLoader =
303 reinterpret_cast<PFN_xrInitializeLoaderKHR>(LoaderPlatformLibraryGetProcAddr(layer_library, function_name));
304 if (initLoader != nullptr) {
305 // we found the entry point one way or another.
306 LoaderLogger::LogInfoMessage(openxr_command,
307 "ApiLayerInterface::LoadApiLayers forwarding xrInitializeLoaderKHR call to API layer "
308 "before calling xrNegotiateLoaderApiLayerInterface.");
309 XrResult res = initLoader(LoaderInitData::instance().getPlatformParam());
310 if (!XR_SUCCEEDED(res)) {
311 LoaderLogger::LogErrorMessage(
312 openxr_command, "ApiLayerInterface::LoadApiLayers forwarded call to xrInitializeLoaderKHR failed.");
313
314 LoaderPlatformLibraryClose(layer_library);
315 return res;
316 }
317 forwardedInitLoader = true;
318 }
319 }
320
321 // Get and settle on an layer interface version (using any provided name if required).
322 std::string function_name = manifest_file->GetFunctionName("xrNegotiateLoaderApiLayerInterface");
323 auto negotiate = reinterpret_cast<PFN_xrNegotiateLoaderApiLayerInterface>(
324 LoaderPlatformLibraryGetProcAddr(layer_library, function_name));
325
326 if (nullptr == negotiate) {
327 std::ostringstream oss;
328 oss << "ApiLayerInterface::LoadApiLayers skipping layer " << manifest_file->LayerName()
329 << " because negotiation function " << function_name << " was not found";
330 LoaderLogger::LogErrorMessage(openxr_command, oss.str());
331 LoaderPlatformLibraryClose(layer_library);
332 last_error = XR_ERROR_API_LAYER_NOT_PRESENT;
333 continue;
334 }
335
336 // Loader info for negotiation
337 XrNegotiateLoaderInfo loader_info = {};
338 loader_info.structType = XR_LOADER_INTERFACE_STRUCT_LOADER_INFO;
339 loader_info.structVersion = XR_LOADER_INFO_STRUCT_VERSION;
340 loader_info.structSize = sizeof(XrNegotiateLoaderInfo);
341 loader_info.minInterfaceVersion = 1;
342 loader_info.maxInterfaceVersion = XR_CURRENT_LOADER_API_LAYER_VERSION;
343 loader_info.minApiVersion = XR_MAKE_VERSION(1, 0, 0);
344 loader_info.maxApiVersion = XR_MAKE_VERSION(1, 0x3ff, 0xfff); // Maximum allowed version for this major version.
345
346 // Set up the layer return structure
347 XrNegotiateApiLayerRequest api_layer_info = {};
348 api_layer_info.structType = XR_LOADER_INTERFACE_STRUCT_API_LAYER_REQUEST;
349 api_layer_info.structVersion = XR_API_LAYER_INFO_STRUCT_VERSION;
350 api_layer_info.structSize = sizeof(XrNegotiateApiLayerRequest);
351
352 XrResult res = negotiate(&loader_info, manifest_file->LayerName().c_str(), &api_layer_info);
353 // If we supposedly succeeded, but got a nullptr for getInstanceProcAddr
354 // then something still went wrong, so return with an error.
355 if (XR_SUCCEEDED(res) && nullptr == api_layer_info.getInstanceProcAddr) {
356 std::string warning_message = "ApiLayerInterface::LoadApiLayers skipping layer ";
357 warning_message += manifest_file->LayerName();
358 warning_message += ", negotiation did not return a valid getInstanceProcAddr";
359 LoaderLogger::LogWarningMessage(openxr_command, warning_message);
360 res = XR_ERROR_FILE_CONTENTS_INVALID;
361 }
362
363 if (XR_SUCCEEDED(res) && !forwardedInitLoader && LoaderInitData::instance().getPlatformParam() != nullptr) {
364 // Forward initialize loader call, where possible and if we did not do so before.
365 PFN_xrVoidFunction initializeVoid = nullptr;
366 PFN_xrInitializeLoaderKHR initialize = nullptr;
367
368 // Now we may try asking xrGetInstanceProcAddr on the API layer
369 if (XR_SUCCEEDED(api_layer_info.getInstanceProcAddr(XR_NULL_HANDLE, "xrInitializeLoaderKHR", &initializeVoid))) {
370 if (initializeVoid == nullptr) {
371 LoaderLogger::LogErrorMessage(openxr_command,
372 "ApiLayerInterface::LoadApiLayers got success from xrGetInstanceProcAddr "
373 "for xrInitializeLoaderKHR, but output a null pointer.");
374 res = XR_ERROR_RUNTIME_FAILURE;
375 } else {
376 initialize = reinterpret_cast<PFN_xrInitializeLoaderKHR>(initializeVoid);
377 }
378 }
379 if (initialize != nullptr) {
380 // we found the entry point one way or another.
381 LoaderLogger::LogInfoMessage(openxr_command,
382 "ApiLayerInterface::LoadApiLayers forwarding xrInitializeLoaderKHR call to API layer "
383 "after calling xrNegotiateLoaderApiLayerInterface.");
384 res = initialize(LoaderInitData::instance().getPlatformParam());
385 if (!XR_SUCCEEDED(res)) {
386 LoaderLogger::LogErrorMessage(
387 openxr_command, "ApiLayerInterface::LoadApiLayers forwarded call to xrInitializeLoaderKHR failed.");
388 }
389 }
390 }
391
392 if (XR_FAILED(res)) {
393 if (!any_loaded) {
394 last_error = res;
395 }
396 std::ostringstream oss;
397 oss << "ApiLayerInterface::LoadApiLayers skipping layer " << manifest_file->LayerName()
398 << " due to failed negotiation with error " << res;
399 LoaderLogger::LogWarningMessage(openxr_command, oss.str());
400 LoaderPlatformLibraryClose(layer_library);
401 continue;
402 }
403
404 {
405 std::ostringstream oss;
406 oss << "ApiLayerInterface::LoadApiLayers succeeded loading layer " << manifest_file->LayerName()
407 << " using interface version " << api_layer_info.layerInterfaceVersion << " and OpenXR API version "
408 << XR_VERSION_MAJOR(api_layer_info.layerApiVersion) << "." << XR_VERSION_MINOR(api_layer_info.layerApiVersion);
409 LoaderLogger::LogInfoMessage(openxr_command, oss.str());
410 }
411
412 // Grab the list of extensions this layer supports for easy filtering after the
413 // xrCreateInstance call
414 std::vector<std::string> supported_extensions;
415 std::vector<XrExtensionProperties> extension_properties;
416 manifest_file->GetInstanceExtensionProperties(extension_properties);
417 supported_extensions.reserve(extension_properties.size());
418 for (XrExtensionProperties& ext_prop : extension_properties) {
419 supported_extensions.emplace_back(ext_prop.extensionName);
420 }
421
422 // Add this API layer to the vector
423 auto iface = std::make_unique<ApiLayerInterface>(manifest_file->LayerName(), layer_library, supported_extensions,
424 api_layer_info.getInstanceProcAddr, api_layer_info.createApiLayerInstance);
425 api_layer_interfaces.emplace_back(std::move(iface));
426
427 // If we load one, clear all errors.
428 any_loaded = true;
429 last_error = XR_SUCCESS;
430 }
431
432 // Set error here to preserve prior error behavior
433 if (!found_all_layers) {
434 last_error = XR_ERROR_API_LAYER_NOT_PRESENT;
435 }
436
437 // If we failed catastrophically for some reason, clean up everything.
438 if (XR_FAILED(last_error)) {
439 api_layer_interfaces.clear();
440 }
441
442 return last_error;
443}
444
445ApiLayerInterface::ApiLayerInterface(const std::string& layer_name, LoaderPlatformLibraryHandle layer_library,
446 std::vector<std::string>& supported_extensions,
447 PFN_xrGetInstanceProcAddr get_instance_proc_addr,
448 PFN_xrCreateApiLayerInstance create_api_layer_instance)
449 : _layer_name(layer_name),
450 _layer_library(layer_library),
451 _get_instance_proc_addr(get_instance_proc_addr),
452 _create_api_layer_instance(create_api_layer_instance),
453 _supported_extensions(supported_extensions) {}
454
455ApiLayerInterface::~ApiLayerInterface() {
456 std::string info_message = "ApiLayerInterface being destroyed for layer ";
457 info_message += _layer_name;
458 LoaderLogger::LogInfoMessage("", info_message);
459 LoaderPlatformLibraryClose(_layer_library);
460}
461
462bool ApiLayerInterface::SupportsExtension(const std::string& extension_name) const {
463 bool found_prop = false;
464 for (const std::string& supported_extension : _supported_extensions) {
465 if (supported_extension == extension_name) {
466 found_prop = true;
467 break;
468 }
469 }
470 return found_prop;
471}