compile_shaders.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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-2023 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, MSL, and DXIL binary C character arrays
  29. out_file = "ShadersCompiledSPV.h"
  30. current_dir = os.path.dirname(os.path.realpath(__file__));
  31. # The SDL_shadercross binaries can be found here: https://github.com/libsdl-org/SDL_shadercross/actions
  32. # Click on the latest workflow and download them for the platform of your choice.
  33. # Set the path to the shadercross executable (shadercross.exe) below.
  34. shadercross_path = ""
  35. out_path = os.path.join(current_dir, out_file)
  36. with open(out_path,'w') as result_file:
  37. result_file.write('// RmlUi SDL GPU shaders compiled using command: \'python compile_shaders.py\'. Do not edit manually.\n\n#include <stdint.h>\n')
  38. for file in os.listdir(current_dir):
  39. if file.endswith(".vert") or file.endswith(".frag"):
  40. shader_path = os.path.join(current_dir, file)
  41. def compile(target):
  42. print("Compiling '{}' to '{}' using SDL_shadercross.".format(file, target))
  43. variable_name = "{}_{}".format(os.path.splitext(file)[0], target)
  44. temp_path = os.path.join(current_dir, ".temp.{}".format(target))
  45. print(temp_path)
  46. subprocess.run([shadercross_path, "-s", "hlsl", shader_path, "-o", temp_path, "-d", target], check = True)
  47. print("Success, writing output to variable '{}' in {}".format(variable_name, out_file))
  48. i = 0
  49. result_file.write('\nalignas(uint32_t) static const unsigned char {}[] = {{'.format(variable_name))
  50. for b in open(temp_path, 'rb').read():
  51. if i % 20 == 0:
  52. result_file.write('\n\t')
  53. result_file.write('0x%02X,' % b)
  54. i += 1
  55. result_file.write('\n};\n')
  56. os.remove(temp_path)
  57. compile("spirv")
  58. compile("msl")
  59. compile("dxil")