File: valid_flag_values_generator.py

package info (click to toggle)
vulkan-validationlayers 1.4.321.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,412 kB
  • sloc: cpp: 594,175; python: 11,321; sh: 24; makefile: 20; xml: 14
file content (205 lines) | stat: -rw-r--r-- 9,659 bytes parent folder | download | duplicates (5)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/python3 -i
#
# Copyright (c) 2025 The Khronos Group Inc.
# Copyright (c) 2025 Valve Corporation
# Copyright (c) 2025 LunarG, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
from collections import defaultdict
from base_generator import BaseGenerator
from generators.generator_utils import PlatformGuardHelper

class ValidFlagValuesOutputGenerator(BaseGenerator):
    def __init__(self):
        BaseGenerator.__init__(self)
        self.ignoreList = [
            'VkInstanceCreateFlagBits', # handled else where

            # TODO - will cause many tests to fail if any codec extension is not enabled
            'VkVideoCodecOperationFlagBitsKHR',
        ]

    def generate(self):
        self.write(f'''// *** THIS FILE IS GENERATED - DO NOT EDIT ***
            // See {os.path.basename(__file__)} for modifications

            /***************************************************************************
            *
            * Copyright (c) 2025 The Khronos Group Inc.
            * Copyright (c) 2025 Valve Corporation
            * Copyright (c) 2025 LunarG, Inc.
            *
            * Licensed under the Apache License, Version 2.0 (the "License");
            * you may not use this file except in compliance with the License.
            * You may obtain a copy of the License at
            *
            *     http://www.apache.org/licenses/LICENSE-2.0
            *
            * Unless required by applicable law or agreed to in writing, software
            * distributed under the License is distributed on an "AS IS" BASIS,
            * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
            * See the License for the specific language governing permissions and
            * limitations under the License.
            ****************************************************************************/\n''')
        self.write('// NOLINTBEGIN') # Wrap for clang-tidy to ignore

        if self.filename == 'valid_flag_values.cpp':
            self.generateSource()
        else:
            self.write(f'\nFile name {self.filename} has no code to generate\n')

        self.write('// NOLINTEND') # Wrap for clang-tidy to ignore

    def generateSource(self):
        guard_helper = PlatformGuardHelper()
        out = []
        out.append('''
            #include "stateless/stateless_validation.h"
            #include <vulkan/vk_enum_string_helper.h>

            // For flags, we can't use the VkFlag as it can't be templated (since it all resolves to a int).
            // It is simpler for the caller to already check for both
            //    - if zero is valid value or not
            //    - if the value is even found in the API
            // so the this file is only focused on checking for extensions being supported
            ''')

        out.append('''
            vvl::Extensions stateless::Context::IsValidFlagValue(vvl::FlagBitmask flag_bitmask, VkFlags value) const {
                switch(flag_bitmask) {
            ''')
        for bitmask in [x for x in self.vk.bitmasks.values() if x.name not in self.ignoreList and not x.returnedOnly and x.bitWidth == 32]:
            # Build up list of expressions so can check together
            expressionMap = defaultdict(list)
            for flag in [x for x in bitmask.flags if len(x.extensions) > 0]:
                # Ignore the base extensions needed to use the flag, only focus on the flag specific extensions
                extensions = [self.vk.extensions[x].name for x in flag.extensions if x not in bitmask.extensions]
                if (len(extensions) == 0):
                    continue
                expression = ",".join(extensions)
                expressionMap[expression].append(flag.name)
            if (len(expressionMap) == 0):
                continue

            out.extend(guard_helper.add_guard(bitmask.protect))
            out.append(f'case vvl::FlagBitmask::{bitmask.name}:\n')

            for flag in [x for x in bitmask.flags if x.multiBit]:
                out.append(f'if (value == {flag.name}) {{ return {{}}; }}\n')

            for expression, names in expressionMap.items():
                extensions = expression.split(',')
                checkExpression = []
                for extension in extensions:
                    checkExpression.append(f'!IsExtEnabled(extensions.{extension.lower()})')
                checkExpression = " && ".join(checkExpression)
                resultExpression = []
                for extension in extensions:
                    resultExpression.append(f'vvl::Extension::_{extension}')
                resultExpression = ", ".join(resultExpression)

                out.append(f'if (value & ({" | ".join(names)})) {{\n')
                out.append(f'   if ({checkExpression}) {{\n')
                out.append(f'       return {{{resultExpression}}};\n')
                out.append('    }')
                out.append('}')

            out.append('   return {};\n')
            # out.append('\n')
        out.extend(guard_helper.add_guard(None))
        out.append('''default: return {};
                }
            }
            ''')

        out.append('''
            vvl::Extensions stateless::Context::IsValidFlag64Value(vvl::FlagBitmask flag_bitmask, VkFlags64 value) const {
                switch(flag_bitmask) {
            ''')
        for bitmask in [x for x in self.vk.bitmasks.values() if x.name not in self.ignoreList and not x.returnedOnly and x.bitWidth == 64]:
            # Build up list of expressions so can check together
            expressionMap = defaultdict(list)
            for flag in [x for x in bitmask.flags if len(x.extensions) > 0]:
                # Ignore the base extensions needed to use the flag, only focus on the flag specific extensions
                extensions = [self.vk.extensions[x].name for x in flag.extensions if x not in bitmask.extensions]
                if (len(extensions) == 0):
                    continue
                expression = ",".join(extensions)
                expressionMap[expression].append(flag.name)
            if (len(expressionMap) == 0):
                continue

            out.extend(guard_helper.add_guard(bitmask.protect))
            out.append(f'case vvl::FlagBitmask::{bitmask.name}:\n')

            for flag in [x for x in bitmask.flags if x.multiBit]:
                out.append(f'if (value == {flag.name}) {{ return {{}}; }}\n')

            for expression, names in expressionMap.items():
                extensions = expression.split(',')
                checkExpression = []
                for extension in extensions:
                    checkExpression.append(f'!IsExtEnabled(extensions.{extension.lower()})')
                checkExpression = " && ".join(checkExpression)
                resultExpression = []
                for extension in extensions:
                    resultExpression.append(f'vvl::Extension::_{extension}')
                resultExpression = ", ".join(resultExpression)

                out.append(f'if (value & ({" | ".join(names)})) {{\n')
                out.append(f'   if ({checkExpression}) {{\n')
                out.append(f'       return {{{resultExpression}}};\n')
                out.append('    }')
                out.append('}')

            out.append('   return {};\n')
        out.extend(guard_helper.add_guard(None))
        out.append('''default: return {};
                }
            }

            std::string stateless::Context::DescribeFlagBitmaskValue(vvl::FlagBitmask flag_bitmask, VkFlags value) const {
                switch(flag_bitmask) {
            ''')
        for bitmask in [x for x in self.vk.bitmasks.values() if x.name not in self.ignoreList and not x.returnedOnly and len(x.flags) > 0 and x.bitWidth == 32]:
            out.extend(guard_helper.add_guard(bitmask.protect))
            out.append(f'case vvl::FlagBitmask::{bitmask.name}:\n')
            out.append(f'return string_{bitmask.flagName}(value);\n')
        out.extend(guard_helper.add_guard(None))
        out.append('''
                    default:
                        std::stringstream ss;
                        ss << "0x" << std::hex << value;
                        return ss.str();
                }
            }

            std::string stateless::Context::DescribeFlagBitmaskValue64(vvl::FlagBitmask flag_bitmask, VkFlags64 value) const {
                switch(flag_bitmask) {
            ''')
        for bitmask in [x for x in self.vk.bitmasks.values() if x.name not in self.ignoreList and not x.returnedOnly and len(x.flags) > 0 and x.bitWidth == 64]:
            out.extend(guard_helper.add_guard(bitmask.protect))
            out.append(f'case vvl::FlagBitmask::{bitmask.name}:\n')
            out.append(f'return string_{bitmask.flagName}(value);\n')
        out.extend(guard_helper.add_guard(None))
        out.append('''
                    default:
                        std::stringstream ss;
                        ss << "0x" << std::hex << value;
                        return ss.str();
                }
            }
        ''')
        self.write(''.join(out))