1#!/usr/bin/python
2# Copyright 2016 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_copy_conversion_table.py:
7#  Code generation for ES3 valid copy conversions table format map.
8
9from datetime import date
10import sys
11
12sys.path.append('renderer')
13import angle_format
14
15template_cpp = """// GENERATED FILE - DO NOT EDIT.
16// Generated by {script_name} using data from {data_source_name}.
17//
18// Copyright {copyright_year} The ANGLE Project Authors. All rights reserved.
19// Use of this source code is governed by a BSD-style license that can be
20// found in the LICENSE file.
21//
22// format_map:
23//   Determining the sized internal format from a (format,type) pair.
24//   Also check es3 format combinations for validity.
25
26#include "angle_gl.h"
27#include "common/debug.h"
28
29namespace gl
30{{
31
32bool ValidES3CopyConversion(GLenum textureFormat, GLenum framebufferFormat)
33{{
34    switch (textureFormat)
35    {{
36{texture_format_cases}        default:
37            break;
38    }}
39
40    return false;
41}}
42
43}}  // namespace gl
44"""
45
46template_format_case = """        case {texture_format}:
47            switch (framebufferFormat)
48            {{
49{framebuffer_format_cases}                    return true;
50                default:
51                    break;
52            }}
53            break;
54
55"""
56
57template_simple_case = """                case {key}:
58"""
59
60def parse_texture_format_case(texture_format, framebuffer_formats):
61    framebuffer_format_cases = ""
62    for framebuffer_format in sorted(framebuffer_formats):
63        framebuffer_format_cases += template_simple_case.format(key = framebuffer_format)
64    return template_format_case.format(
65        texture_format = texture_format, framebuffer_format_cases = framebuffer_format_cases)
66
67data_source_name = 'es3_copy_conversion_formats.json'
68
69json_data = angle_format.load_json(data_source_name)
70
71format_map = {}
72
73for description, data in json_data.iteritems():
74    for texture_format, framebuffer_format in data:
75        if texture_format not in format_map:
76            format_map[texture_format] = []
77        format_map[texture_format] += [ framebuffer_format ]
78
79texture_format_cases = ""
80
81for texture_format, framebuffer_formats in sorted(format_map.iteritems()):
82    texture_format_cases += parse_texture_format_case(texture_format, framebuffer_formats)
83
84with open('es3_copy_conversion_table_autogen.cpp', 'wt') as out_file:
85    output_cpp = template_cpp.format(
86        script_name = sys.argv[0],
87        data_source_name = data_source_name,
88        copyright_year = date.today().year,
89        texture_format_cases = texture_format_cases)
90    out_file.write(output_cpp)
91    out_file.close()
92