Vulkan枚举值的字符串化
Vulkan
中的枚举值做了很好的枚举类型管理,尽管还是是C类型的枚举,具有全局符号可见行;再尽管不似C++ enum class
有严格的命名空间访问机制,但是仍然具备相应的枚举类型作为命名空间管理和访问。这为Vulkan
枚举值的字符串转换提供了方便,可能这也是为何VulkanSDK
给出了官方实现vk_enum_string_helper.h
的原因。
以VkImageType
为例, 1
2
3
4
5
6// Provided by VK_VERSION_1_0
typedef enum VkImageType {
VK_IMAGE_TYPE_1D = 0,
VK_IMAGE_TYPE_2D = 1,
VK_IMAGE_TYPE_3D = 2,
} VkImageType;
其对应的字符串转化实现为,函数签名为string_
+
具体的枚举类型, 1
2
3
4
5
6
7
8
9
10
11
12static inline const char* string_VkImageType(VkImageType input_value) {
switch (input_value) {
case VK_IMAGE_TYPE_1D:
return "VK_IMAGE_TYPE_1D";
case VK_IMAGE_TYPE_2D:
return "VK_IMAGE_TYPE_2D";
case VK_IMAGE_TYPE_3D:
return "VK_IMAGE_TYPE_3D";
default:
return "Unhandled VkImageType";
}
}
OpenGL枚举值的字符串化
OpenGL
如果想做同样的工作,则面临大不相同的境遇。
different enum names may have the same hexadecimal values in OpenGL (especially if you consider the extensions) - a switch will refuse to compile because of ambiguities
以Khronos
官方给出的GL/glcorearb.h观察,对比Vulkan
的枚举值,OpenGL
的枚举值有几个问题:
1. 所有枚举值都是#define
定义的宏 2.
枚举值没有命名空间(枚举类型) 3.
即使不考虑扩展,都存在不同枚举值对应同一个十六进制常量 4. 类型不是统一的
1
2
3typedef unsigned int GLenum;
脚本生成gl_enum_string_helper.h
为了简单的生成OpenGL枚举值的字符串化字符串转换函数,做了一些简化: 1.
不考虑扩展枚举值 2.
合并数值相同的枚举值,在switch
实现中只保留一个枚举值 3.
对0xFFFFFFFF
、0xFFFFFFFFFFFFFFFF
这种,单独处理
最终,完成了自动化生成OpenGL
枚举值字符串化的头文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80exts = ["ARB", "KHR", "AMD", "NV", "OVR", "APPLE", "EXT", "INTEL", "MESA",
"VIEW",
"GL_CONTEXT_ROBUST_ACCESS"
]
def IsExts(line):
for ext in exts:
head = "GL_" + ext + "_"
tail = "_" + ext
tokens = line.split()
if tokens[1].endswith(tail) or tokens[1].startswith(head) or tokens[1] == ext:
return True
return False
def IsMacro(line):
tokens = line.split()
if len(tokens) != 3 or not tokens[2].startswith("0x"):
return True
else:
return False
enum2hex = {}
hex2enum = {}
def find_key(enum):
for el in hex2enum.values():
if enum in el:
return el
return []
header = """#pragma once
#ifdef __cplusplus
#include <string>
#endif
#include <glad/glad.h>
"""
if __name__ == '__main__':
f = open("glcorearb.h")
for line in f.readlines():
if line.startswith("#define"):
tokens = line.split()
if not IsMacro(line) and not IsExts(line):
print(line)
e = tokens[1]
h = "0x" + tokens[2][2:].lstrip("0")
if tokens[2] == "0xFFFFFFFFFFFFFFFFull" or tokens[2] == "0xFFFFFFFFu":
h = "0xFFFFFFFF"
enum2hex[e] = h
if h not in hex2enum.keys():
hex2enum[h] = [e]
else:
hex2enum[h].append(e)
with open("gl_enum_string_helper.h", "w") as outfile:
print(header, file=outfile)
print("static inline const char* string_gl_enum(GLenum input_value) {", file=outfile)
print(" switch (input_value) {", file=outfile)
for el in hex2enum.values():
# print('GL_ENUM(%s,%s)' % (enum, enums[enum]), file=outfile)
print(" case %s:" % el[0], file=outfile)
res = '|'.join(el)
print(" return \"%s\";" % res, file=outfile)
print(" default:", file=outfile)
print(" return \"Unhandled Enum\";", file=outfile)
print(" }", file=outfile)
print("}", file=outfile)