Files
angle/include/platform/gen_features.py
T
Shahbaz Youssefi 797e627e64 Autogenerate list of features as enum
The WithX() and WithNoX() helpers are removed and replaced with enable()
and disable() member functions that take the name of the feature (as a
Feature::X enum constant).  This has two benefits:

- Adding tests that override a feature no longer requires additional
  helper functions to be written.
- There's no mistaking the feature name.

This change doesn't yet fix the main issue in anglebug.com/6435, but
does fix the following helpers using an old feature name (so they were
ineffective):

- WithMetalForcedBufferGPUStorage
- WithNoVulkanViewportFlip

A follow up would remove the old way of overriding features in tests and
replaces them with the new way.

Bug: angleproject:6435
Change-Id: Ida02b26ec72bc40d7a8938c76a93815bb903ca05
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3580982
Reviewed-by: Jamie Madill <[email protected]>
Reviewed-by: Yuly Novikov <[email protected]>
Commit-Queue: Shahbaz Youssefi <[email protected]>
2022-04-12 03:42:40 +00:00

213 lines
6.2 KiB
Python
Executable File

#! /usr/bin/python3
# Copyright 2022 The ANGLE Project Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# gen_features.py:
# Code generation for ANGLE features.
# NOTE: don't run this script directly. Run scripts/run_code_generation.py.
from collections import namedtuple
import json
import os
import sys
feature_files = {
'd3d_features.json': ('D3D', 'FeaturesD3D.h'),
'frontend_features.json': ('Frontend', 'FrontendFeatures.h'),
'gl_features.json': ('OpenGL', 'FeaturesGL.h'),
'mtl_features.json': ('Metal', 'FeaturesMtl.h'),
'vk_features.json': ('Vulkan', 'FeaturesVk.h'),
}
feature_list_header_file = '../../util/angle_features_autogen.h'
feature_list_source_file = '../../util/angle_features_autogen.cpp'
template_header = u"""// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {input_file_name}.
//
{description}
#ifndef ANGLE_PLATFORM_{NAME}_H_
#define ANGLE_PLATFORM_{NAME}_H_
#include "platform/Feature.h"
namespace angle
{{
struct {name} : FeatureSetBase
{{
{name}();
~{name}();
{features}
}};
inline {name}::{name}() = default;
inline {name}::~{name}() = default;
}} // namespace angle
#endif // ANGLE_PLATFORM_{NAME}_H_
"""
template_feature = u"""FeatureInfo {name} = {{
"{display_name}", FeatureCategory::{category},
{description},
&members, {issue}
}};
"""
template_feature_list_header = u"""// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {input_file_name}.
//
// Copyright 2022 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// angle_features_autogen.h: List of ANGLE features to help enable/disable them in tests.
#ifndef ANGLE_SRC_TESTS_TEST_UTILS_ANGLE_FEATURES_AUTOGEN_H_
#define ANGLE_SRC_TESTS_TEST_UTILS_ANGLE_FEATURES_AUTOGEN_H_
#include "util_export.h"
namespace angle
{{
enum class Feature
{{
{features}
InvalidEnum,
EnumCount = InvalidEnum,
}};
ANGLE_UTIL_EXPORT extern const char *GetFeatureName(Feature feature);
}} // namespace angle
#endif // ANGLE_SRC_TESTS_TEST_UTILS_ANGLE_FEATURES_AUTOGEN_H_
"""
template_feature_enum = u"""{Name},"""
template_feature_list_source = u"""// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {input_file_name}.
//
// Copyright 2022 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// angle_features_autogen.cpp: List of ANGLE features to help enable/disable them in tests.
#include "angle_features_autogen.h"
#include "common/PackedEnums.h"
namespace angle
{{
namespace
{{
constexpr PackedEnumMap<Feature, const char *> kFeatureNames = {{ {{
{features}
}} }};
}} // anonymous namespace
const char *GetFeatureName(Feature feature)
{{
return kFeatureNames[feature];
}}
}} // namespace angle
"""
template_feature_string = u"""{{Feature::{Name}, "{display_name}"}},"""
def main():
if len(sys.argv) == 2 and sys.argv[1] == 'inputs':
print(','.join(list(feature_files.keys())))
return
if len(sys.argv) == 2 and sys.argv[1] == 'outputs':
print(','.join([header for (_, header) in feature_files.values()]) + ',' +
feature_list_header_file + ',' + feature_list_source_file)
return
name_map = {}
for src_file, (category_prefix, header_file) in feature_files.items():
with open(src_file) as fin:
src = json.loads(fin.read())
features_json = src['features']
features = []
# Go over the list of features and write the header file that declares the features struct
for feature_json in features_json:
name = feature_json['name']
display_name = feature_json['display_name']
issue = feature_json.get('issue', None)
feature = template_feature.format(
name=name,
display_name=display_name,
category=category_prefix + feature_json['category'],
description='\n'.join('"' + line + '"' for line in feature_json['description']),
issue='' if issue is None else '"' + issue + '"')
features.append(feature)
# Keep track of the feature names. Sometimes the same feature name is present in
# multiple backends. That's ok for the purposes of feature overriding as long as the
# display names match.
if name in name_map:
assert (display_name == name_map[name])
else:
name_map[name] = display_name
description = '\n'.join(['// ' + line for line in src['description']])
name = header_file[:-2]
header = template_header.format(
script_name=os.path.basename(__file__),
input_file_name=src_file,
description=description.replace(src_file, header_file),
name=name,
NAME=name.upper(),
features='\n'.join(features))
with open(header_file, 'w') as fout:
fout.write(header)
fout.close()
# Generate helpers for use by tests to override a feature or not.
feature_enums = []
feature_strings = []
for name, display_name in sorted(name_map.items()):
Name = name[0].upper() + name[1:]
feature_enums.append(template_feature_enum.format(Name=Name))
feature_strings.append(
template_feature_string.format(Name=Name, display_name=display_name))
with open(feature_list_header_file, 'w') as fout:
fout.write(
template_feature_list_header.format(
script_name=os.path.basename(__file__),
input_file_name='*_features.json',
features='\n'.join(feature_enums)))
fout.close()
with open(feature_list_source_file, 'w') as fout:
fout.write(
template_feature_list_source.format(
script_name=os.path.basename(__file__),
input_file_name='*_features.json',
features='\n'.join(feature_strings)))
fout.close()
if __name__ == '__main__':
sys.exit(main())