• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python2
2# Copyright 2018 The ANGLE Project Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5#
6# gen_vk_mandatory_format_support_table.py:
7#  Code generation for mandatory formats supported by Vulkan.
8#  NOTE: don't run this script directly. Run scripts/run_code_generation.py.
9
10import sys
11
12sys.path.append('..')
13import angle_format
14import xml.etree.ElementTree as etree
15import sys, os
16
17TEMPLATE_TABLE_AUTOGEN_CPP = """// GENERATED FILE - DO NOT EDIT.
18// Generated by {script_name} using data from {input_file_name} and
19// the vk.xml file situated at
20// /third_party/vulkan-validation-layers/src/scripts/vk.xml
21//
22// Copyright 2020 The ANGLE Project Authors. All rights reserved.
23// Use of this source code is governed by a BSD-style license that can be
24// found in the LICENSE file.
25//
26// {out_file_name}:
27//   Queries for full Vulkan mandatory format support information based on VK format.
28
29#include "libANGLE/renderer/vulkan/vk_format_utils.h"
30
31using namespace angle;
32
33namespace rx
34{{
35namespace vk
36{{
37namespace
38{{
39constexpr VkFormatFeatureFlagBits BLIT_DST = VK_FORMAT_FEATURE_BLIT_DST_BIT;
40constexpr VkFormatFeatureFlagBits BLIT_SRC = VK_FORMAT_FEATURE_BLIT_SRC_BIT;
41constexpr VkFormatFeatureFlagBits COLOR_ATTACHMENT = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT;
42constexpr VkFormatFeatureFlagBits COLOR_ATTACHMENT_BLEND = VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT;
43constexpr VkFormatFeatureFlagBits DEPTH_STENCIL_ATTACHMENT = VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT;
44constexpr VkFormatFeatureFlagBits SAMPLED_IMAGE = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
45constexpr VkFormatFeatureFlagBits SAMPLED_IMAGE_FILTER_LINEAR = VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT;
46constexpr VkFormatFeatureFlagBits STORAGE_IMAGE = VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT;
47constexpr VkFormatFeatureFlagBits STORAGE_IMAGE_ATOMIC = VK_FORMAT_FEATURE_STORAGE_IMAGE_ATOMIC_BIT;
48constexpr VkFormatFeatureFlagBits STORAGE_TEXEL_BUFFER = VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT;
49constexpr VkFormatFeatureFlagBits STORAGE_TEXEL_BUFFER_ATOMIC = VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_ATOMIC_BIT;
50constexpr VkFormatFeatureFlagBits UNIFORM_TEXEL_BUFFER = VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT;
51constexpr VkFormatFeatureFlagBits VERTEX_BUFFER = VK_FORMAT_FEATURE_VERTEX_BUFFER_BIT;
52
53using namespace angle;
54
55constexpr FormatMap<VkFormatProperties> kFormatProperties = {{
56    {format_case_data}
57}};
58}}  // anonymous namespace
59
60const VkFormatProperties& GetMandatoryFormatSupport(FormatID formatID)
61{{
62    return kFormatProperties[formatID];
63}}
64}}  // namespace vk
65}}  // namespace rx
66"""
67
68TEMPLATE_FORMAT_PROPERTY = """{{FormatID::{format_id}, {{0, {optimal_features}, {buffer_features}}}}}"""
69
70
71def script_relative(path):
72    return os.path.join(os.path.dirname(sys.argv[0]), path)
73
74
75def gen_format_case(format_id, vk_format, vk_map):
76
77    def de(str):
78        return str.replace("VK_FORMAT_FEATURE_", "").replace("_BIT", "")
79
80    if vk_format in vk_map and len(vk_map[vk_format]) > 0:
81        # Check which feature is a buffer feature or not.
82        buffer_features = [de(x) for x in vk_map[vk_format] if x.find("_BUFFER_") != -1]
83        optimal_features = [de(x) for x in vk_map[vk_format] if x.find("_BUFFER_") == -1]
84        optimal_features_str = "|".join(sorted(optimal_features)) if len(optimal_features) else "0"
85        buffer_features_str = "|".join(sorted(buffer_features)) if len(buffer_features) else "0"
86    else:
87        optimal_features_str = "0"
88        buffer_features_str = "0"
89
90    return TEMPLATE_FORMAT_PROPERTY.format(
91        format_id=format_id,
92        vk_format=vk_format,
93        optimal_features=optimal_features_str,
94        buffer_features=buffer_features_str)
95
96
97def main():
98
99    input_file_name = 'vk_mandatory_format_support_data.json'
100    vk_format_map_path = 'vk_format_map.json'
101    out_file_name = 'vk_mandatory_format_support_table_autogen.cpp'
102    vk_xml_file = '../../../../third_party/vulkan-deps/vulkan-headers/src/registry/vk.xml'
103
104    # auto_script parameters.
105    if len(sys.argv) > 1:
106        inputs = [
107            '../angle_format.py',
108            input_file_name,
109            vk_format_map_path,
110            vk_xml_file,
111        ]
112        outputs = [out_file_name]
113
114        if sys.argv[1] == 'inputs':
115            print ','.join(inputs)
116        elif sys.argv[1] == 'outputs':
117            print ','.join(outputs)
118        else:
119            print('Invalid script parameters')
120            return 1
121        return 0
122
123    tree = etree.parse(script_relative(vk_xml_file))
124    root = tree.getroot()
125    vk_format_enums = root.findall(".//enums[@name='VkFormat']/enum")
126    vk_map = angle_format.load_json(input_file_name)
127    vk_format_map = angle_format.load_json(vk_format_map_path)
128    vk_cases = [
129        gen_format_case(format_id, vk_format, vk_map)
130        for format_id, vk_format in vk_format_map["map"].iteritems()
131    ]
132
133    output_cpp = TEMPLATE_TABLE_AUTOGEN_CPP.format(
134        format_case_data=",\n".join(vk_cases),
135        script_name=__file__,
136        out_file_name=out_file_name,
137        input_file_name=input_file_name)
138
139    with open(out_file_name, 'wt') as out_file:
140        out_file.write(output_cpp)
141        out_file.close()
142    return 0
143
144
145if __name__ == '__main__':
146    sys.exit(main())
147