Skip to content

Commit 1df688c

Browse files
Generate VkResult operator<< overload
1 parent 77ccbe4 commit 1df688c

File tree

5 files changed

+221
-107
lines changed

5 files changed

+221
-107
lines changed

scripts/generate_source.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def RunGenerators(api: str, registry: str, directory: str, styleFile: str, targe
6666
from generators.dispatch_table_helper_generator import DispatchTableHelperGenerator
6767
from generators.helper_file_generator import HelperFileGenerator
6868
from generators.loader_extension_generator import LoaderExtensionGenerator
69+
from generators.vk_result_to_string_generator import VkResultToStringGenerator
6970

7071
# These set fields that are needed by both OutputGenerator and BaseGenerator,
7172
# but are uniform and don't need to be set at a per-generated file level
@@ -75,6 +76,7 @@ def RunGenerators(api: str, registry: str, directory: str, styleFile: str, targe
7576
# Generated directory and dispatch table helper file name may be API specific (e.g. Vulkan SC)
7677
generated_directory = 'loader/generated'
7778
dispatch_table_helper_filename = 'vk_dispatch_table_helper.h'
79+
result_to_string_filename = 'vk_result_to_string_helper.h'
7880

7981
generators.update({
8082
'vk_layer_dispatch_table.h': {
@@ -101,6 +103,11 @@ def RunGenerators(api: str, registry: str, directory: str, styleFile: str, targe
101103
'generator' : DispatchTableHelperGenerator,
102104
'genCombined': False,
103105
'directory' : 'tests/framework/layer/generated',
106+
},
107+
f'{result_to_string_filename}': {
108+
'generator' : VkResultToStringGenerator,
109+
'genCombined': False,
110+
'directory' : 'tests/framework/generated',
104111
}
105112
})
106113

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/python3 -i
2+
#
3+
# Copyright (c) 2015-2021 The Khronos Group Inc.
4+
# Copyright (c) 2015-2021 Valve Corporation
5+
# Copyright (c) 2015-2021 LunarG, Inc.
6+
# Copyright (c) 2015-2021 Google Inc.
7+
#
8+
# Licensed under the Apache License, Version 2.0 (the "License");
9+
# you may not use this file except in compliance with the License.
10+
# You may obtain a copy of the License at
11+
#
12+
# http://www.apache.org/licenses/LICENSE-2.0
13+
#
14+
# Unless required by applicable law or agreed to in writing, software
15+
# distributed under the License is distributed on an "AS IS" BASIS,
16+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
# See the License for the specific language governing permissions and
18+
# limitations under the License.
19+
#
20+
# Author: Mark Lobodzinski <[email protected]>
21+
# Author: Charles Giessen <[email protected]>
22+
23+
import os
24+
from base_generator import BaseGenerator
25+
26+
class VkResultToStringGenerator(BaseGenerator):
27+
def __init__(self):
28+
BaseGenerator.__init__(self)
29+
30+
def generate(self):
31+
out = []
32+
33+
out.append(f'''#pragma once
34+
// *** THIS FILE IS GENERATED - DO NOT EDIT ***
35+
// See {os.path.basename(__file__)} for modifications
36+
37+
/*
38+
* Copyright (c) 2025 The Khronos Group Inc.
39+
* Copyright (c) 2025 Valve Corporation
40+
* Copyright (c) 2025 LunarG, Inc.
41+
*
42+
* Licensed under the Apache License, Version 2.0 (the "License");
43+
* you may not use this file except in compliance with the License.
44+
* You may obtain a copy of the License at
45+
*
46+
* http://www.apache.org/licenses/LICENSE-2.0
47+
*
48+
* Unless required by applicable law or agreed to in writing, software
49+
* distributed under the License is distributed on an "AS IS" BASIS,
50+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
51+
* See the License for the specific language governing permissions and
52+
* limitations under the License.
53+
*
54+
55+
*/
56+
57+
#include <ostream>
58+
#include <string>
59+
60+
#include <vulkan/vulkan.h>
61+
62+
''')
63+
64+
self.OutputVkResultToStringHelper(out)
65+
out.append('\n')
66+
67+
self.write(''.join(out))
68+
69+
# Create a dispatch table from the corresponding table_type and append it to out
70+
def OutputVkResultToStringHelper(self, out: list):
71+
out.append('inline std::ostream& operator<<(std::ostream& os, const VkResult& result) {\n')
72+
out.append(' switch (result) {\n')
73+
for field in self.vk.enums['VkResult'].fields:
74+
out.append(f' case({field.name}):\n')
75+
out.append(f' return os << "{field.name}";\n')
76+
out.append(' default:\n')
77+
out.append(' return os << static_cast<int32_t>(result);\n')
78+
out.append(' }\n')
79+
out.append(' }\n')
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#pragma once
2+
// *** THIS FILE IS GENERATED - DO NOT EDIT ***
3+
// See vk_result_to_string_generator.py for modifications
4+
5+
/*
6+
* Copyright (c) 2025 The Khronos Group Inc.
7+
* Copyright (c) 2025 Valve Corporation
8+
* Copyright (c) 2025 LunarG, Inc.
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*
22+
23+
*/
24+
25+
#include <ostream>
26+
#include <string>
27+
28+
#include <vulkan/vulkan.h>
29+
30+
inline std::ostream& operator<<(std::ostream& os, const VkResult& result) {
31+
switch (result) {
32+
case (VK_SUCCESS):
33+
return os << "VK_SUCCESS";
34+
case (VK_NOT_READY):
35+
return os << "VK_NOT_READY";
36+
case (VK_TIMEOUT):
37+
return os << "VK_TIMEOUT";
38+
case (VK_EVENT_SET):
39+
return os << "VK_EVENT_SET";
40+
case (VK_EVENT_RESET):
41+
return os << "VK_EVENT_RESET";
42+
case (VK_INCOMPLETE):
43+
return os << "VK_INCOMPLETE";
44+
case (VK_ERROR_OUT_OF_HOST_MEMORY):
45+
return os << "VK_ERROR_OUT_OF_HOST_MEMORY";
46+
case (VK_ERROR_OUT_OF_DEVICE_MEMORY):
47+
return os << "VK_ERROR_OUT_OF_DEVICE_MEMORY";
48+
case (VK_ERROR_INITIALIZATION_FAILED):
49+
return os << "VK_ERROR_INITIALIZATION_FAILED";
50+
case (VK_ERROR_DEVICE_LOST):
51+
return os << "VK_ERROR_DEVICE_LOST";
52+
case (VK_ERROR_MEMORY_MAP_FAILED):
53+
return os << "VK_ERROR_MEMORY_MAP_FAILED";
54+
case (VK_ERROR_LAYER_NOT_PRESENT):
55+
return os << "VK_ERROR_LAYER_NOT_PRESENT";
56+
case (VK_ERROR_EXTENSION_NOT_PRESENT):
57+
return os << "VK_ERROR_EXTENSION_NOT_PRESENT";
58+
case (VK_ERROR_FEATURE_NOT_PRESENT):
59+
return os << "VK_ERROR_FEATURE_NOT_PRESENT";
60+
case (VK_ERROR_INCOMPATIBLE_DRIVER):
61+
return os << "VK_ERROR_INCOMPATIBLE_DRIVER";
62+
case (VK_ERROR_TOO_MANY_OBJECTS):
63+
return os << "VK_ERROR_TOO_MANY_OBJECTS";
64+
case (VK_ERROR_FORMAT_NOT_SUPPORTED):
65+
return os << "VK_ERROR_FORMAT_NOT_SUPPORTED";
66+
case (VK_ERROR_FRAGMENTED_POOL):
67+
return os << "VK_ERROR_FRAGMENTED_POOL";
68+
case (VK_ERROR_UNKNOWN):
69+
return os << "VK_ERROR_UNKNOWN";
70+
case (VK_ERROR_VALIDATION_FAILED):
71+
return os << "VK_ERROR_VALIDATION_FAILED";
72+
case (VK_ERROR_OUT_OF_POOL_MEMORY):
73+
return os << "VK_ERROR_OUT_OF_POOL_MEMORY";
74+
case (VK_ERROR_INVALID_EXTERNAL_HANDLE):
75+
return os << "VK_ERROR_INVALID_EXTERNAL_HANDLE";
76+
case (VK_ERROR_FRAGMENTATION):
77+
return os << "VK_ERROR_FRAGMENTATION";
78+
case (VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS):
79+
return os << "VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS";
80+
case (VK_PIPELINE_COMPILE_REQUIRED):
81+
return os << "VK_PIPELINE_COMPILE_REQUIRED";
82+
case (VK_ERROR_NOT_PERMITTED):
83+
return os << "VK_ERROR_NOT_PERMITTED";
84+
case (VK_ERROR_SURFACE_LOST_KHR):
85+
return os << "VK_ERROR_SURFACE_LOST_KHR";
86+
case (VK_ERROR_NATIVE_WINDOW_IN_USE_KHR):
87+
return os << "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
88+
case (VK_SUBOPTIMAL_KHR):
89+
return os << "VK_SUBOPTIMAL_KHR";
90+
case (VK_ERROR_OUT_OF_DATE_KHR):
91+
return os << "VK_ERROR_OUT_OF_DATE_KHR";
92+
case (VK_ERROR_INCOMPATIBLE_DISPLAY_KHR):
93+
return os << "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR";
94+
case (VK_ERROR_INVALID_SHADER_NV):
95+
return os << "VK_ERROR_INVALID_SHADER_NV";
96+
case (VK_ERROR_IMAGE_USAGE_NOT_SUPPORTED_KHR):
97+
return os << "VK_ERROR_IMAGE_USAGE_NOT_SUPPORTED_KHR";
98+
case (VK_ERROR_VIDEO_PICTURE_LAYOUT_NOT_SUPPORTED_KHR):
99+
return os << "VK_ERROR_VIDEO_PICTURE_LAYOUT_NOT_SUPPORTED_KHR";
100+
case (VK_ERROR_VIDEO_PROFILE_OPERATION_NOT_SUPPORTED_KHR):
101+
return os << "VK_ERROR_VIDEO_PROFILE_OPERATION_NOT_SUPPORTED_KHR";
102+
case (VK_ERROR_VIDEO_PROFILE_FORMAT_NOT_SUPPORTED_KHR):
103+
return os << "VK_ERROR_VIDEO_PROFILE_FORMAT_NOT_SUPPORTED_KHR";
104+
case (VK_ERROR_VIDEO_PROFILE_CODEC_NOT_SUPPORTED_KHR):
105+
return os << "VK_ERROR_VIDEO_PROFILE_CODEC_NOT_SUPPORTED_KHR";
106+
case (VK_ERROR_VIDEO_STD_VERSION_NOT_SUPPORTED_KHR):
107+
return os << "VK_ERROR_VIDEO_STD_VERSION_NOT_SUPPORTED_KHR";
108+
case (VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT):
109+
return os << "VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT";
110+
case (VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT):
111+
return os << "VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT";
112+
case (VK_THREAD_IDLE_KHR):
113+
return os << "VK_THREAD_IDLE_KHR";
114+
case (VK_THREAD_DONE_KHR):
115+
return os << "VK_THREAD_DONE_KHR";
116+
case (VK_OPERATION_DEFERRED_KHR):
117+
return os << "VK_OPERATION_DEFERRED_KHR";
118+
case (VK_OPERATION_NOT_DEFERRED_KHR):
119+
return os << "VK_OPERATION_NOT_DEFERRED_KHR";
120+
case (VK_ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR):
121+
return os << "VK_ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR";
122+
case (VK_ERROR_COMPRESSION_EXHAUSTED_EXT):
123+
return os << "VK_ERROR_COMPRESSION_EXHAUSTED_EXT";
124+
case (VK_INCOMPATIBLE_SHADER_BINARY_EXT):
125+
return os << "VK_INCOMPATIBLE_SHADER_BINARY_EXT";
126+
case (VK_PIPELINE_BINARY_MISSING_KHR):
127+
return os << "VK_PIPELINE_BINARY_MISSING_KHR";
128+
case (VK_ERROR_NOT_ENOUGH_SPACE_KHR):
129+
return os << "VK_ERROR_NOT_ENOUGH_SPACE_KHR";
130+
default:
131+
return os << static_cast<int32_t>(result);
132+
}
133+
}

tests/framework/test_environment.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757

5858
#include "layer/test_layer.h"
5959

60+
#include "generated/vk_result_to_string_helper.h"
61+
6062
#include FRAMEWORK_CONFIG_HEADER
6163

6264
// Useful defines

tests/framework/test_util.h

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -345,113 +345,6 @@ struct FRAMEWORK_EXPORT DispatchableHandle {
345345
T handle = nullptr;
346346
};
347347

348-
// Stream operator for VkResult so GTEST will print out error codes as strings (automatically)
349-
inline std::ostream& operator<<(std::ostream& os, const VkResult& result) {
350-
switch (result) {
351-
case (VK_SUCCESS):
352-
return os << "VK_SUCCESS";
353-
case (VK_NOT_READY):
354-
return os << "VK_NOT_READY";
355-
case (VK_TIMEOUT):
356-
return os << "VK_TIMEOUT";
357-
case (VK_EVENT_SET):
358-
return os << "VK_EVENT_SET";
359-
case (VK_EVENT_RESET):
360-
return os << "VK_EVENT_RESET";
361-
case (VK_INCOMPLETE):
362-
return os << "VK_INCOMPLETE";
363-
case (VK_ERROR_OUT_OF_HOST_MEMORY):
364-
return os << "VK_ERROR_OUT_OF_HOST_MEMORY";
365-
case (VK_ERROR_OUT_OF_DEVICE_MEMORY):
366-
return os << "VK_ERROR_OUT_OF_DEVICE_MEMORY";
367-
case (VK_ERROR_INITIALIZATION_FAILED):
368-
return os << "VK_ERROR_INITIALIZATION_FAILED";
369-
case (VK_ERROR_DEVICE_LOST):
370-
return os << "VK_ERROR_DEVICE_LOST";
371-
case (VK_ERROR_MEMORY_MAP_FAILED):
372-
return os << "VK_ERROR_MEMORY_MAP_FAILED";
373-
case (VK_ERROR_LAYER_NOT_PRESENT):
374-
return os << "VK_ERROR_LAYER_NOT_PRESENT";
375-
case (VK_ERROR_EXTENSION_NOT_PRESENT):
376-
return os << "VK_ERROR_EXTENSION_NOT_PRESENT";
377-
case (VK_ERROR_FEATURE_NOT_PRESENT):
378-
return os << "VK_ERROR_FEATURE_NOT_PRESENT";
379-
case (VK_ERROR_INCOMPATIBLE_DRIVER):
380-
return os << "VK_ERROR_INCOMPATIBLE_DRIVER";
381-
case (VK_ERROR_TOO_MANY_OBJECTS):
382-
return os << "VK_ERROR_TOO_MANY_OBJECTS";
383-
case (VK_ERROR_FORMAT_NOT_SUPPORTED):
384-
return os << "VK_ERROR_FORMAT_NOT_SUPPORTED";
385-
case (VK_ERROR_FRAGMENTED_POOL):
386-
return os << "VK_ERROR_FRAGMENTED_POOL";
387-
case (VK_ERROR_UNKNOWN):
388-
return os << "VK_ERROR_UNKNOWN";
389-
case (VK_ERROR_OUT_OF_POOL_MEMORY):
390-
return os << "VK_ERROR_OUT_OF_POOL_MEMORY";
391-
case (VK_ERROR_INVALID_EXTERNAL_HANDLE):
392-
return os << "VK_ERROR_INVALID_EXTERNAL_HANDLE";
393-
case (VK_ERROR_FRAGMENTATION):
394-
return os << "VK_ERROR_FRAGMENTATION";
395-
case (VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS):
396-
return os << "VK_ERROR_INVALID_OPAQUE_CAPTURE_ADDRESS";
397-
case (VK_ERROR_SURFACE_LOST_KHR):
398-
return os << "VK_ERROR_SURFACE_LOST_KHR";
399-
case (VK_ERROR_NATIVE_WINDOW_IN_USE_KHR):
400-
return os << "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
401-
case (VK_SUBOPTIMAL_KHR):
402-
return os << "VK_SUBOPTIMAL_KHR";
403-
case (VK_ERROR_OUT_OF_DATE_KHR):
404-
return os << "VK_ERROR_OUT_OF_DATE_KHR";
405-
case (VK_ERROR_INCOMPATIBLE_DISPLAY_KHR):
406-
return os << "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR";
407-
case (VK_ERROR_VALIDATION_FAILED_EXT):
408-
return os << "VK_ERROR_VALIDATION_FAILED_EXT";
409-
case (VK_ERROR_INVALID_SHADER_NV):
410-
return os << "VK_ERROR_INVALID_SHADER_NV";
411-
case (VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT):
412-
return os << "VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT";
413-
case (VK_ERROR_NOT_PERMITTED_EXT):
414-
return os << "VK_ERROR_NOT_PERMITTED_EXT";
415-
case (VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT):
416-
return os << "VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT";
417-
case (VK_THREAD_IDLE_KHR):
418-
return os << "VK_THREAD_IDLE_KHR";
419-
case (VK_THREAD_DONE_KHR):
420-
return os << "VK_THREAD_DONE_KHR";
421-
case (VK_OPERATION_DEFERRED_KHR):
422-
return os << "VK_OPERATION_DEFERRED_KHR";
423-
case (VK_OPERATION_NOT_DEFERRED_KHR):
424-
return os << "VK_OPERATION_NOT_DEFERRED_KHR";
425-
case (VK_PIPELINE_COMPILE_REQUIRED_EXT):
426-
return os << "VK_PIPELINE_COMPILE_REQUIRED_EXT";
427-
case (VK_RESULT_MAX_ENUM):
428-
return os << "VK_RESULT_MAX_ENUM";
429-
case (VK_ERROR_COMPRESSION_EXHAUSTED_EXT):
430-
return os << "VK_ERROR_COMPRESSION_EXHAUSTED_EXT";
431-
case (VK_ERROR_IMAGE_USAGE_NOT_SUPPORTED_KHR):
432-
return os << "VK_ERROR_IMAGE_USAGE_NOT_SUPPORTED_KHR";
433-
case (VK_ERROR_VIDEO_PICTURE_LAYOUT_NOT_SUPPORTED_KHR):
434-
return os << "VK_ERROR_VIDEO_PICTURE_LAYOUT_NOT_SUPPORTED_KHR";
435-
case (VK_ERROR_VIDEO_PROFILE_OPERATION_NOT_SUPPORTED_KHR):
436-
return os << "VK_ERROR_VIDEO_PROFILE_OPERATION_NOT_SUPPORTED_KHR";
437-
case (VK_ERROR_VIDEO_PROFILE_FORMAT_NOT_SUPPORTED_KHR):
438-
return os << "VK_ERROR_VIDEO_PROFILE_FORMAT_NOT_SUPPORTED_KHR";
439-
case (VK_ERROR_VIDEO_PROFILE_CODEC_NOT_SUPPORTED_KHR):
440-
return os << "VK_ERROR_VIDEO_PROFILE_CODEC_NOT_SUPPORTED_KHR";
441-
case (VK_ERROR_VIDEO_STD_VERSION_NOT_SUPPORTED_KHR):
442-
return os << "VK_ERROR_VIDEO_STD_VERSION_NOT_SUPPORTED_KHR";
443-
case (VK_ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR):
444-
return os << "VK_ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR";
445-
case (VK_ERROR_INCOMPATIBLE_SHADER_BINARY_EXT):
446-
return os << "VK_ERROR_INCOMPATIBLE_SHADER_BINARY_EXT";
447-
case (VK_PIPELINE_BINARY_MISSING_KHR):
448-
return os << "VK_PIPELINE_BINARY_MISSING_KHR";
449-
case (VK_ERROR_NOT_ENOUGH_SPACE_KHR):
450-
return os << "VK_ERROR_NOT_ENOUGH_SPACE_KHR";
451-
}
452-
return os << static_cast<int32_t>(result);
453-
}
454-
455348
const char* get_platform_wsi_extension([[maybe_unused]] const char* api_selection);
456349

457350
bool string_eq(const char* a, const char* b) noexcept;

0 commit comments

Comments
 (0)