You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.3 KiB

  1. #
  2. # Converts provided shader files into the gal namespaced shaders
  3. #
  4. # The goal here is to convert the contents of shaders in the source tree
  5. # into strings to embed into the binaries rather than loading from disk.
  6. #
  7. # We convert the shaders to binary form in the header due limitations of string literal
  8. # lengths and also because future development options such as moving to bgfx
  9. # results in precompiled binary shaders we need to store
  10. #
  11. # This file is structured to be invoked from add_custom_command in order
  12. # to have GENERATED marked output files that can be rebuilt on change.
  13. #
  14. # Required Arguments:
  15. # SOURCE_FILE - Path to source shader file
  16. # OUT_CPP_DIR - Destination path for cpp file
  17. # OUT_HEADER_DIR - Destination path for header file
  18. # OUT_CPP_FILENAME - cpp filename
  19. # OUT_HEADER_FILENAME - header filename
  20. # OUT_VAR_NAME - Name of variable containing shader to be created
  21. #
  22. # Parts taken from https://github.com/sivachandran/cmake-bin2h
  23. # Copyright 2020 Sivachandran Paramasivam
  24. #
  25. # Function to wrap a given string into multiple lines at the given column position.
  26. # Parameters:
  27. # VARIABLE - The name of the CMake variable holding the string.
  28. # AT_COLUMN - The column position at which string will be wrapped.
  29. function(WRAP_STRING)
  30. set(oneValueArgs VARIABLE AT_COLUMN)
  31. cmake_parse_arguments(WRAP_STRING "${options}" "${oneValueArgs}" "" ${ARGN})
  32. string(LENGTH ${${WRAP_STRING_VARIABLE}} stringLength)
  33. math(EXPR offset "0")
  34. while(stringLength GREATER 0)
  35. if(stringLength GREATER ${WRAP_STRING_AT_COLUMN})
  36. math(EXPR length "${WRAP_STRING_AT_COLUMN}")
  37. else()
  38. math(EXPR length "${stringLength}")
  39. endif()
  40. string(SUBSTRING ${${WRAP_STRING_VARIABLE}} ${offset} ${length} line)
  41. set(lines "${lines}\n${line}")
  42. math(EXPR stringLength "${stringLength} - ${length}")
  43. math(EXPR offset "${offset} + ${length}")
  44. endwhile()
  45. set(${WRAP_STRING_VARIABLE} "${lines}" PARENT_SCOPE)
  46. endfunction()
  47. file( READ ${SOURCE_FILE} _SOURCE_BINARY HEX )
  48. string(LENGTH ${_SOURCE_BINARY} _SOURCE_BINARY_LENGTH)
  49. set(SOURCE_BINARY "${_SOURCE_BINARY}00") # null terminate for the sake of it
  50. wrap_string(VARIABLE _SOURCE_BINARY AT_COLUMN 32)
  51. math(EXPR _ARRAY_SIZE "${_SOURCE_BINARY_LENGTH} / 2")
  52. # adds '0x' prefix and comma suffix before and after every byte respectively
  53. string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1, " _ARRAY_VALUES ${_SOURCE_BINARY})
  54. # removes trailing comma
  55. string(REGEX REPLACE ", $" "" _ARRAY_VALUES ${_ARRAY_VALUES})
  56. set( outCppTextByteArray "unsigned char ${OUT_VAR_NAME}_bytes[] = { ${_ARRAY_VALUES} };")
  57. set( outCppTextStdString "std::string ${OUT_VAR_NAME} = std::string( reinterpret_cast<char const*>(${OUT_VAR_NAME}_bytes), ${_ARRAY_SIZE} ) ;")
  58. set( outCppText
  59. "
  60. #include <string>
  61. #include <${OUT_HEADER_FILENAME}>
  62. namespace KIGFX {
  63. namespace BUILTIN_SHADERS {
  64. ${outCppTextByteArray}
  65. ${outCppTextStdString}
  66. }
  67. }
  68. " )
  69. file(
  70. WRITE ${OUT_CPP_DIR}/${OUT_CPP_FILENAME}
  71. "${outCppText}"
  72. )
  73. set( outHeaderText
  74. "namespace KIGFX {
  75. namespace BUILTIN_SHADERS {
  76. extern std::string ${OUT_VAR_NAME};
  77. }
  78. }"
  79. )
  80. file(
  81. WRITE ${OUT_HEADER_DIR}/${OUT_HEADER_FILENAME}
  82. "${outHeaderText}"
  83. )
  84. message(STATUS "Shader ${SOURCE_FILE} converted to ${OUT_CPP_DIR}/${OUT_CPP_FILENAME}")