File: generate_settings.py

package info (click to toggle)
vulkan-validationlayers 1.4.335.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 51,728 kB
  • sloc: cpp: 645,254; python: 12,203; sh: 24; makefile: 24; xml: 14
file content (191 lines) | stat: -rw-r--r-- 7,482 bytes parent folder | download
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
#!/usr/bin/env python3
#
# 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.
#
# Take the JSON with our settings and generates the other setting related files
# See docs/settings.md

import common_ci
import json
import os

def IsDebugSetting(setting):
    if setting['key'] == 'debug_action':
        return False
    return 'debug' in setting['key']

def GenerateTextFile(all_settings):
    out = []
    out.append(f'''# The settings in this file can be used to configure the behavior of layers in this repository.
# This file doesn't display the dependencies on the various settings
#
# This file is generated by {os.path.basename(__file__)}

# VK_LAYER_KHRONOS_validation
''')

    for setting in all_settings:
        if IsDebugSetting(setting):
            continue
        if 'view' in setting and setting['view'] == 'HIDDEN':
            continue

        default = setting['default']
        type = setting['type']
        if type == 'BOOL':
            default = 'true' if setting['default'] is True else 'false'
        if type == 'FLAGS' or type == 'LIST':
            default = ','.join(default)
        default = str(default)
        if default:
            default = " " + default
        out.append(f'# {setting["label"]}')
        out.append('# =====================')
        out.append(f'# {setting["description"]}')
        out.append(f'khronos_validation.{setting["key"]} ={default}\n')

    text_output = common_ci.RepoRelative('layers/vk_layer_settings.txt')
    with open(text_output, 'w') as file:
        file.write('\n'.join(out))

def GenerateValidation(all_settings):
    out = []
    out.append(f'''// *** THIS FILE IS GENERATED - DO NOT EDIT ***
// See {os.path.basename(__file__)} for modifications

/***************************************************************************
 *
 * 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.
 */''')

    out.append('''
#pragma once
// clang-format off

// This function is generated from scripts/generate_settings.py
static void ValidateLayerSettingsProvided(const VkLayerSettingsCreateInfoEXT &layer_setting_create_info,
                                          std::vector<std::string> &setting_warnings) {
    // Found that a set of <const char*> doesn't detect duplicates on all compilers
    vvl::unordered_set<std::string> used_settings;

    for (uint32_t i = 0; i < layer_setting_create_info.settingCount; i++) {
        const VkLayerSettingEXT &setting = layer_setting_create_info.pSettings[i];
        if (strcmp(OBJECT_LAYER_NAME, setting.pLayerName) != 0) {
            continue;
        }

        // used as a backup for settings not listed below
        VkLayerSettingTypeEXT required_type = VK_LAYER_SETTING_TYPE_MAX_ENUM_EXT;

        // Debugging settings are not added here as those are for internal development
        // and not designed for an app to use via VkLayerSettings API
        const char* name = setting.pSettingName;
        if (strcmp(VK_LAYER_ENABLES, name) == 0) { required_type = VK_LAYER_SETTING_TYPE_STRING_EXT; }
        else if (strcmp(VK_LAYER_DISABLES, name) == 0) { required_type = VK_LAYER_SETTING_TYPE_STRING_EXT; }''')

    for setting in all_settings:
        if IsDebugSetting(setting):
            continue
        if 'view' in setting and setting['view'] == 'HIDDEN':
            continue

        type = setting['type']
        layer_type = None
        if type == 'BOOL':
            layer_type = 'VK_LAYER_SETTING_TYPE_BOOL32_EXT'
        elif type == 'INT':
            layer_type = 'VK_LAYER_SETTING_TYPE_UINT32_EXT'
        elif type in ['SAVE_FILE', 'LIST', 'FLAGS']:
            layer_type = 'VK_LAYER_SETTING_TYPE_STRING_EXT'
        else:
            print(f'Warning, not type handled for {type} ({setting["key"]})')
            continue

        out.append(f'        else if (strcmp(VK_LAYER_{setting["key"].upper()}, name) == 0) {{ required_type = {layer_type}; }}')

    out.append('''        else {
            setting_warnings.emplace_back("The setting \\"" + std::string(name) +
                                          "\\" in VkLayerSettingsCreateInfoEXT was not recognized by the Validation Layers. Please "
                                          "view the VkLayer_khronos_validation.json for a list of all settings.");
        }

        if (required_type != VK_LAYER_SETTING_TYPE_MAX_ENUM_EXT && setting.type != required_type) {
            setting_warnings.emplace_back(
                "The setting \\"" + std::string(name) + "\\" in VkLayerSettingsCreateInfoEXT was set to type " +
                std::string(string_VkLayerSettingTypeEXT(setting.type)) + " but requires type " +
                std::string(string_VkLayerSettingTypeEXT(required_type)) + " and the value may be parsed incorrectly.");
        }

        if (used_settings.count(name)) {
            setting_warnings.emplace_back(
                "The setting \\"" + std::string(name) +
                "\\" in VkLayerSettingsCreateInfoEXT was listed twice and only the first one listed will be recognized.");
        }
        used_settings.insert(name);
    }
}
// clang-format on
''')

    text_output = common_ci.RepoRelative('layers/layer_options_validation.h')
    with open(text_output, 'w') as file:
        file.write('\n'.join(out))

if __name__ == '__main__':
    json_input = common_ci.RepoRelative('layers/VkLayer_khronos_validation.json.in')
    with open(json_input, 'r') as file:
        json_settings = json.load(file)['layer']['features']['settings']

    # Build up the list all at once into a flat list
    all_settings = []
    def Parse(setting):
        type = setting['type']
        if type == 'GROUP':
            for s in setting['settings']:
                Parse(s)
            return

        if type == 'FLAGS':
            for flags in setting['flags']:
                if 'settings' in flags:
                    for s in flags['settings']:
                        Parse(s)

        if 'settings' in setting:
            for s in setting['settings']:
                Parse(s)
        all_settings.append(setting)
    for setting in json_settings:
        Parse(setting)

    # Same on all OS (and easy to find setting in large generated list)
    all_settings = sorted(all_settings, key=lambda obj: obj['key'])

    GenerateTextFile(all_settings)
    GenerateValidation(all_settings)