compile_shaders.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # This source file is part of RmlUi, the HTML/CSS Interface Middleware
  2. #
  3. # For the latest information, see http://github.com/mikke89/RmlUi
  4. #
  5. # Copyright (c) 2008-2014 CodePoint Ltd, Shift Technology Ltd, and contributors
  6. # Copyright (c) 2019 The RmlUi Team, and contributors
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining a copy
  9. # of this software and associated documentation files (the "Software"), to deal
  10. # in the Software without restriction, including without limitation the rights
  11. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. # copies of the Software, and to permit persons to whom the Software is
  13. # furnished to do so, subject to the following conditions:
  14. #
  15. # The above copyright notice and this permission notice shall be included in all
  16. # copies or substantial portions of the Software.
  17. #
  18. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. # SOFTWARE.
  25. import sys
  26. import os
  27. import subprocess
  28. # Compiles all .frag and .vert files in this directory to SPIR-V binary C character arrays. Requires 'glslc' installed and available system-wide.
  29. out_file = "ShadersCompiledSPV.h"
  30. current_dir = os.path.dirname(os.path.realpath(__file__));
  31. temp_spirv_path = os.path.join(current_dir, ".temp.spv")
  32. out_path = os.path.join(current_dir, out_file)
  33. with open(out_path,'w') as result_file:
  34. result_file.write('// RmlUi SPIR-V Vulkan shaders compiled using command: \'python compile_shaders.py\'. Do not edit manually.\n\n#include <stdint.h>\n')
  35. for file in os.listdir(current_dir):
  36. if file.endswith(".vert") or file.endswith(".frag"):
  37. shader_path = os.path.join(current_dir, file)
  38. variable_name = os.path.splitext(file)[0]
  39. print("Compiling '{}' to SPIR-V using glslc.".format(file))
  40. subprocess.run(["glslc", shader_path, "-o", temp_spirv_path], check = True)
  41. print("Success, writing output to variable '{}' in {}".format(variable_name, out_file))
  42. i = 0
  43. result_file.write('\nalignas(uint32_t) static const unsigned char {}[] = {{'.format(variable_name))
  44. for b in open(temp_spirv_path, 'rb').read():
  45. if i % 20 == 0:
  46. result_file.write('\n\t')
  47. result_file.write('0x%02X,' % b)
  48. i += 1
  49. result_file.write('\n};\n')
  50. os.remove(temp_spirv_path)