compile_shaders.py 3.2 KB

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