import subprocess
import sys
import tempfile
+from textwrap import dedent
def print_usage(err):
- print """\
-glsl_scraper.py [options] file
+ print(dedent("""\
+ glsl_scraper.py [options] file
-This program scrapes a C file for any instance of the GLSL_VK_SHADER macro,
-grabs the GLSL source code, compiles it to SPIR-V. The resulting SPIR-V
-code is written to another C file as an array of 32-bit words.
+ This program scrapes a C file for any instance of the GLSL_VK_SHADER macro,
+ grabs the GLSL source code, compiles it to SPIR-V. The resulting SPIR-V
+ code is written to another C file as an array of 32-bit words.
-If '-' is passed as the input file or output file, stdin or stdout will be
-used instead of a file on disc.
+ If '-' is passed as the input file or output file, stdin or stdout will be
+ used instead of a file on disc.
-Options:
- -o outfile Output to the given file (default: stdout)
- --with-glslang=PATH Full path to the glslangValidator program"""
+ Options:
+ -o outfile Output to the given file (default: stdout)
+ --with-glslang=PATH Full path to the glslangValidator program"""))
exit(err)
class Shader:
shutil.rmtree(tmpdir)
with open_file(outfname, 'w') as outfile:
- outfile.write("""\
-/* =========================== DO NOT EDIT! ===========================
- * This file is autogenerated by glsl_scraper.py.
- */
-
-#include <stdint.h>
-
-#define _ANV_SPIRV_MAGIC "\\x03\\x02\\x23\\x07\\0\\0\\0\\0"
-
-#define _ANV_SPIRV_VERTEX _ANV_SPIRV_MAGIC "\\0\\0\\0\\0"
-#define _ANV_SPIRV_TESS_CONTROL _ANV_SPIRV_MAGIC "\\1\\0\\0\\0"
-#define _ANV_SPIRV_TESS_EVALUATION _ANV_SPIRV_MAGIC "\\2\\0\\0\\0"
-#define _ANV_SPIRV_GEOMETRY _ANV_SPIRV_MAGIC "\\3\\0\\0\\0"
-#define _ANV_SPIRV_FRAGMENT _ANV_SPIRV_MAGIC "\\4\\0\\0\\0"
-#define _ANV_SPIRV_COMPUTE _ANV_SPIRV_MAGIC "\\5\\0\\0\\0"
-
-#define _ANV_GLSL_SRC_VAR2(_line) _glsl_helpers_shader ## _line ## _glsl_src
-#define _ANV_GLSL_SRC_VAR(_line) _ANV_GLSL_SRC_VAR2(_line)
-
-#define GLSL_VK_SHADER(device, stage, ...) ({ \\
- VkShader __shader; \\
- VkShaderCreateInfo __shader_create_info = { \\
- .sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO, \\
- .codeSize = sizeof(_ANV_GLSL_SRC_VAR(__LINE__)), \\
- .pCode = _ANV_GLSL_SRC_VAR(__LINE__), \\
- }; \\
- vkCreateShader((VkDevice) device, &__shader_create_info, &__shader); \\
- __shader; \\
-})
-""")
+ outfile.write(dedent("""\
+ /* =========================== DO NOT EDIT! ===========================
+ * This file is autogenerated by glsl_scraper.py.
+ */
+
+ #include <stdint.h>
+
+ #define _ANV_SPIRV_MAGIC "\\x03\\x02\\x23\\x07\\0\\0\\0\\0"
+
+ #define _ANV_SPIRV_VERTEX _ANV_SPIRV_MAGIC "\\0\\0\\0\\0"
+ #define _ANV_SPIRV_TESS_CONTROL _ANV_SPIRV_MAGIC "\\1\\0\\0\\0"
+ #define _ANV_SPIRV_TESS_EVALUATION _ANV_SPIRV_MAGIC "\\2\\0\\0\\0"
+ #define _ANV_SPIRV_GEOMETRY _ANV_SPIRV_MAGIC "\\3\\0\\0\\0"
+ #define _ANV_SPIRV_FRAGMENT _ANV_SPIRV_MAGIC "\\4\\0\\0\\0"
+ #define _ANV_SPIRV_COMPUTE _ANV_SPIRV_MAGIC "\\5\\0\\0\\0"
+
+ #define _ANV_GLSL_SRC_VAR2(_line) _glsl_helpers_shader ## _line ## _glsl_src
+ #define _ANV_GLSL_SRC_VAR(_line) _ANV_GLSL_SRC_VAR2(_line)
+
+ #define GLSL_VK_SHADER(device, stage, ...) ({ \\
+ VkShader __shader; \\
+ VkShaderCreateInfo __shader_create_info = { \\
+ .sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO, \\
+ .codeSize = sizeof(_ANV_GLSL_SRC_VAR(__LINE__)), \\
+ .pCode = _ANV_GLSL_SRC_VAR(__LINE__), \\
+ }; \\
+ vkCreateShader((VkDevice) device, &__shader_create_info, &__shader); \\
+ __shader; \\
+ })
+ """))
for shader in parser.shaders:
shader.dump_c_code(outfile, glsl_only)