Home | History | Annotate | Download | only in smoke
      1 #!/usr/bin/env python3
      2 #
      3 # Copyright (C) 2016 Google, Inc.
      4 #
      5 # Permission is hereby granted, free of charge, to any person obtaining a
      6 # copy of this software and associated documentation files (the "Software"),
      7 # to deal in the Software without restriction, including without limitation
      8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
      9 # and/or sell copies of the Software, and to permit persons to whom the
     10 # Software is furnished to do so, subject to the following conditions:
     11 #
     12 # The above copyright notice and this permission notice shall be included
     13 # in all copies or substantial portions of the Software.
     14 #
     15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     21 # DEALINGS IN THE SOFTWARE.
     22 
     23 """Compile GLSL to SPIR-V.
     24 
     25 Depends on glslangValidator.
     26 """
     27 
     28 import os
     29 import sys
     30 import subprocess
     31 import struct
     32 import re
     33 
     34 SPIRV_MAGIC = 0x07230203
     35 COLUMNS = 4
     36 INDENT = 4
     37 
     38 in_filename = sys.argv[1]
     39 out_filename = sys.argv[2] if len(sys.argv) > 2 else None
     40 validator = sys.argv[3] if len(sys.argv) > 3 else \
     41         "../../../glslang/build/install/bin/glslangValidator"
     42 
     43 def identifierize(s):
     44     # translate invalid chars
     45     s = re.sub("[^0-9a-zA-Z_]", "_", s)
     46     # translate leading digits
     47     return re.sub("^[^a-zA-Z_]+", "_", s)
     48 
     49 def compile_glsl(filename, tmpfile):
     50     # invoke glslangValidator
     51     try:
     52         args = [validator, "-V", "-H", "-o", tmpfile, filename]
     53         output = subprocess.check_output(args, universal_newlines=True)
     54     except subprocess.CalledProcessError as e:
     55         print(e.output, file=sys.stderr)
     56         exit(1)
     57 
     58     # read the temp file into a list of SPIR-V words
     59     words = []
     60     with open(tmpfile, "rb") as f:
     61         data = f.read()
     62         assert(len(data) and len(data) % 4 == 0)
     63 
     64         # determine endianness
     65         fmt = ("<" if data[0] == (SPIRV_MAGIC & 0xff) else ">") + "I"
     66         for i in range(0, len(data), 4):
     67             words.append(struct.unpack(fmt, data[i:(i + 4)])[0])
     68 
     69         assert(words[0] == SPIRV_MAGIC)
     70 
     71 
     72     # remove temp file
     73     os.remove(tmpfile)
     74 
     75     return (words, output.rstrip())
     76 
     77 base = os.path.basename(in_filename)
     78 words, comments = compile_glsl(in_filename, base + ".tmp")
     79 
     80 literals = []
     81 for i in range(0, len(words), COLUMNS):
     82     columns = ["0x%08x" % word for word in words[i:(i + COLUMNS)]]
     83     literals.append(" " * INDENT + ", ".join(columns) + ",")
     84 
     85 header = """#include <stdint.h>
     86 
     87 #if 0
     88 %s
     89 #endif
     90 
     91 static const uint32_t %s[%d] = {
     92 %s
     93 };
     94 """ % (comments, identifierize(base), len(words), "\n".join(literals))
     95 
     96 if out_filename:
     97     with open(out_filename, "w") as f:
     98         print(header, end="", file=f)
     99 else:
    100         print(header, end="")
    101