1# -*- coding: utf-8 -*- 2 3#------------------------------------------------------------------------- 4# Vulkan CTS 5# ---------- 6# 7# Copyright (c) 2015 Google Inc. 8# 9# Licensed under the Apache License, Version 2.0 (the "License"); 10# you may not use this file except in compliance with the License. 11# You may obtain a copy of the License at 12# 13# http://www.apache.org/licenses/LICENSE-2.0 14# 15# Unless required by applicable law or agreed to in writing, software 16# distributed under the License is distributed on an "AS IS" BASIS, 17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18# See the License for the specific language governing permissions and 19# limitations under the License. 20# 21#------------------------------------------------------------------------- 22 23import os 24import sys 25import argparse 26import re 27 28scriptPath = os.path.join(os.path.dirname(__file__), "..", "..", "..", "scripts") 29sys.path.insert(0, scriptPath) 30 31from ctsbuild.common import * 32from khr_util.format import writeInlFile 33 34VULKAN_HEADERS_INCLUDE_DIR = os.path.join(os.path.dirname(__file__), "..", "..", "vulkan-docs", "src", "include") 35VULKAN_H = { "" : [ os.path.join(VULKAN_HEADERS_INCLUDE_DIR, "vk_video", "vulkan_video_codecs_common.h"), 36 os.path.join(VULKAN_HEADERS_INCLUDE_DIR, "vk_video", "vulkan_video_codec_h264std.h"), 37 os.path.join(VULKAN_HEADERS_INCLUDE_DIR, "vk_video", "vulkan_video_codec_h264std_decode.h"), 38 os.path.join(VULKAN_HEADERS_INCLUDE_DIR, "vk_video", "vulkan_video_codec_h264std_encode.h"), 39 os.path.join(VULKAN_HEADERS_INCLUDE_DIR, "vk_video", "vulkan_video_codec_h265std.h"), 40 os.path.join(VULKAN_HEADERS_INCLUDE_DIR, "vk_video", "vulkan_video_codec_h265std_decode.h"), 41 os.path.join(VULKAN_HEADERS_INCLUDE_DIR, "vk_video", "vulkan_video_codec_h265std_encode.h"), 42 os.path.join(VULKAN_HEADERS_INCLUDE_DIR, "vk_video", "vulkan_video_codec_av1std.h"), 43 os.path.join(VULKAN_HEADERS_INCLUDE_DIR, "vk_video", "vulkan_video_codec_av1std_decode.h"), 44 os.path.join(VULKAN_HEADERS_INCLUDE_DIR, "vulkan", "vulkan_core.h") ], 45 "SC" : [ os.path.join(os.path.dirname(__file__), "src", "vulkan_sc_core.h") ] } 46DEFAULT_OUTPUT_DIR = { "" : os.path.join(os.path.dirname(__file__), "..", "framework", "vulkan", "generated", "vulkan"), 47 "SC" : os.path.join(os.path.dirname(__file__), "..", "framework", "vulkan", "generated", "vulkansc") } 48 49INL_HEADER = """\ 50/* WARNING: This is auto-generated file. Do not modify, since changes will 51 * be lost! Modify the generating script instead. 52 * This file was generated by /scripts/gen_framework_c.py 53 */\ 54 55""" 56 57TYPE_SUBSTITUTIONS = [ 58 ("uint8_t", "uint8_t"), 59 ("uint16_t", "uint16_t"), 60 ("uint32_t", "uint32_t"), 61 ("uint64_t", "uint64_t"), 62 ("int8_t", "int8_t"), 63 ("int16_t", "int16_t"), 64 ("int32_t", "int32_t"), 65 ("int64_t", "int64_t"), 66 ("bool32_t", "uint32_t"), 67 ("size_t", "uintptr_t"), 68] 69 70def readFile (filename): 71 with open(filename, 'rt') as f: 72 return f.read() 73 74def writeVulkanCHeader (src, filename): 75 def gen (): 76 dst = re.sub(r'(#include "[^\s,\n}]+")', '', src) 77 78 for old_type, new_type in TYPE_SUBSTITUTIONS: 79 dst = dst.replace(old_type, new_type) 80 yield dst 81 writeInlFile(filename, INL_HEADER, gen()) 82 83def parseCmdLineArgs(): 84 parser = argparse.ArgumentParser(description = "Generate Vulkan INL files", 85 formatter_class=argparse.ArgumentDefaultsHelpFormatter) 86 parser.add_argument("-a", 87 "--api", 88 dest="api", 89 default="", 90 help="Choose between Vulkan and Vulkan SC") 91 parser.add_argument("-o", 92 "--outdir", 93 dest="outdir", 94 default="", 95 help="Choose output directory") 96 parser.add_argument("-v", "--verbose", 97 dest="verbose", 98 action="store_true", 99 help="Enable verbose logging") 100 return parser.parse_args() 101 102def getApiName (args): 103 if len(args)<2: 104 return '' 105 return args[1] 106 107if __name__ == "__main__": 108 args = parseCmdLineArgs() 109 initializeLogger(args.verbose) 110 111 outputPath = DEFAULT_OUTPUT_DIR[args.api] 112 # if argument was specified it is interpreted as a path to which .inl file will be written 113 if args.outdir != '': 114 outputPath = args.outdir 115 src = "" 116 117 # Generate vulkan headers from vk.xml 118 currentDir = os.getcwd() 119 pythonExecutable = sys.executable or "python" 120 os.chdir(os.path.join(VULKAN_HEADERS_INCLUDE_DIR, "..", "xml")) 121 vkTargets = ["vulkan_core.h"] 122 for target in vkTargets: 123 execute([pythonExecutable, "../scripts/genvk.py", "-o", "../include/vulkan", target]) 124 125 videoDir = "../include/vk_video" 126 if (not os.path.isdir(videoDir)): 127 os.mkdir(videoDir) 128 129 videoTargets = [ 130 'vulkan_video_codecs_common.h', 131 'vulkan_video_codec_h264std.h', 132 'vulkan_video_codec_h264std_decode.h', 133 'vulkan_video_codec_h264std_encode.h', 134 'vulkan_video_codec_h265std.h', 135 'vulkan_video_codec_h265std_decode.h', 136 'vulkan_video_codec_h265std_encode.h', 137 'vulkan_video_codec_av1std.h', 138 'vulkan_video_codec_av1std_decode.h', 139 ] 140 for target in videoTargets: 141 execute([pythonExecutable, "../scripts/genvk.py", "-registry", "video.xml", "-o", videoDir, target]) 142 143 os.chdir(currentDir) 144 145 for file in VULKAN_H[args.api]: 146 src += readFile(file) 147 148 writeVulkanCHeader (src, os.path.join(outputPath, "vkVulkan_c.inl")) 149