#!/usr/bin/python
# Copyright 2016 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_vk_format_table.py:
#  Code generation for vk format map. See vk_format_map.json for data source.

from datetime import date
import json
import math
import pprint
import os
import re
import sys

sys.path.append('..')
import angle_format

template_table_autogen_cpp = """// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {input_file_name}
//
// Copyright {copyright_year} 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.
//
// {out_file_name}:
//   Queries for full Vulkan format information based on GL format.

#include "libANGLE/renderer/vulkan/vk_format_utils.h"

#include "image_util/copyimage.h"
#include "image_util/generatemip.h"
#include "image_util/loadimage.h"

using namespace angle;

namespace rx
{{

namespace vk
{{

void Format::initialize(VkPhysicalDevice physicalDevice, const angle::Format &angleFormat)
{{
    switch (angleFormat.id)
    {{
{format_case_data}
        default:
            UNREACHABLE();
            break;
    }}
}}

}}  // namespace vk

}}  // namespace rx
"""

empty_format_entry_template = """case angle::Format::ID::{format_id}:
// This format is not implemented in Vulkan.
break;
"""

format_entry_template = """case angle::Format::ID::{format_id}:
internalFormat = {internal_format};
{texture_template}
{buffer_template}
break;
"""

texture_basic_template = """textureFormatID = angle::Format::ID::{texture};
vkTextureFormat = {vk_texture_format};
textureInitializerFunction = {texture_initializer};"""

texture_fallback_template = """initTextureFallback(physicalDevice,
angle::Format::ID::{texture},
{vk_texture_format},
{texture_initializer},
angle::Format::ID::{texture_fallback},
{vk_texture_format_fallback},
{texture_initializer_fallback});"""

buffer_basic_template = """bufferFormatID = angle::Format::ID::{buffer};
vkBufferFormat = {vk_buffer_format};"""

buffer_fallback_template = """initBufferFallback(physicalDevice,
angle::Format::ID::{buffer},
{vk_buffer_format},
angle::Format::ID::{buffer_fallback},
{vk_buffer_format_fallback});"""

def gen_format_case(angle, internal_format, vk_json_data):
    vk_map = vk_json_data["map"]
    vk_overrides = vk_json_data["overrides"]
    vk_fallbacks = vk_json_data["fallbacks"]
    args = { "format_id" : angle }

    if ((angle not in vk_map) and (angle not in vk_overrides) and
        (angle not in vk_fallbacks)) or angle == 'NONE':
        return empty_format_entry_template.format(**args)

    def get_formats_and_template(format, type, basic_template, fallback_template):
        format = vk_overrides.get(format, {}).get(type, format)
        fallback = vk_fallbacks.get(format, {}).get(type, "NONE")
        if format not in vk_map:
            format = "NONE"
            template = ""
        elif fallback == "NONE":
            template = basic_template
        else:
            template = fallback_template
        return format, fallback, template

    texture_format, texture_fallback, texture_template = get_formats_and_template(
                angle, "texture", texture_basic_template, texture_fallback_template)
    buffer_format, buffer_fallback, buffer_template = get_formats_and_template(
                angle, "buffer", buffer_basic_template, buffer_fallback_template)

    args.update(
        internal_format=internal_format,
        texture_template=texture_template,
        texture=texture_format,
        vk_texture_format=vk_map[texture_format],
        texture_initializer=angle_format.get_internal_format_initializer(internal_format,
                                                                         texture_format),
        texture_fallback=texture_fallback,
        vk_texture_format_fallback=vk_map[texture_fallback],
        texture_initializer_fallback=angle_format.get_internal_format_initializer(internal_format,
                                                                                  texture_fallback),
        buffer_template=buffer_template,
        buffer=buffer_format,
        vk_buffer_format=vk_map[buffer_format],
        buffer_fallback=buffer_fallback,
        vk_buffer_format_fallback=vk_map[buffer_fallback],
    )

    return format_entry_template.format(**args).format(**args)

input_file_name = 'vk_format_map.json'
out_file_name = 'vk_format_table'

angle_to_gl = angle_format.load_inverse_table(os.path.join('..', 'angle_format_map.json'))
vk_json_data = angle_format.load_json(input_file_name)
vk_cases = [gen_format_case(angle, gl, vk_json_data)
             for angle, gl in sorted(angle_to_gl.iteritems())]

output_cpp = template_table_autogen_cpp.format(
    copyright_year = date.today().year,
    format_case_data = "\n".join(vk_cases),
    script_name = __file__,
    out_file_name = out_file_name,
    input_file_name = input_file_name)

with open(out_file_name + '_autogen.cpp', 'wt') as out_file:
    out_file.write(output_cpp)
    out_file.close()
