shaders.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. -- Compile bgfx shader files. Substitution for scripts/shader.mk.
  2. --
  3. -- Usage:
  4. --
  5. -- add_rules("@bgfx/shaders")
  6. -- add_files("shader.vert", {type = "vertex", output_dir = "shaders", output_name = "shader.vert.bin", profiles = {glsl = "330"}})
  7. rule("shaders")
  8. set_extensions(".vert", ".frag", ".comp")
  9. on_buildcmd_file(function (target, batchcmds, shaderfile, opt)
  10. import("lib.detect.find_program")
  11. import("core.base.option")
  12. batchcmds:show_progress(opt.progress, "${color.build.object}compiling.shaderc %s", shaderfile)
  13. -- get bgfx shaderc
  14. local shaderc = find_program("shadercRelease") or find_program("shadercDebug")
  15. assert(shaderc, "bgfx shaderc not found! please check your bgfx installation.")
  16. -- determine arguments for shaderc from fileconfig
  17. local fileconfig = target:fileconfig(shaderfile)
  18. local output_filename
  19. if fileconfig and fileconfig.output_name then
  20. output_filename = fileconfig.output_name
  21. else
  22. output_filename = path.filename(shaderfile) .. ".bin"
  23. end
  24. local output_dir
  25. if fileconfig and fileconfig.output_dir then
  26. output_dir = fileconfig.output_dir
  27. else
  28. output_dir = "shaders"
  29. end
  30. local vardef_filename
  31. if fileconfig and fileconfig.vardef then
  32. vardef_filename = fileconfig.vardef
  33. else
  34. vardef_filename = path.join(
  35. path.directory(shaderfile),
  36. path.basename(shaderfile) .. ".varying.def.sc")
  37. end
  38. local shader_type
  39. if fileconfig and fileconfig.type then
  40. if table.contains(bgfx_types, fileconfig.type) then
  41. shader_type = fileconfig.type
  42. else
  43. raise("unsupported shader type " .. fileconfig.type)
  44. end
  45. elseif shaderfile:match("%.vert$") then
  46. shader_type = "vertex"
  47. elseif shaderfile:match("%.frag$") then
  48. shader_type = "fragment"
  49. elseif shaderfile:match("%.comp$") then
  50. shader_type = "compute"
  51. else
  52. raise("cannot determine shader type from file name " .. path.filename(shaderfile))
  53. end
  54. -- determine platform-specific shaderc arguments
  55. local bgfx_platforms = {
  56. windows = "windows",
  57. macosx = "osx",
  58. linux = "linux"
  59. }
  60. local bgfx_types = {
  61. "vertex",
  62. "fragment",
  63. "compute"
  64. }
  65. local bgfx_default_profiles = {
  66. windows = {
  67. vertex = {dx9 = "s_3_0", dx11 = "s_5_0", glsl = "120"},
  68. fragment = {dx9 = "s_3_0", dx11 = "s_5_0", glsl = "120"},
  69. compute = {dx11 = "s_5_0", glsl = "430"},
  70. },
  71. macosx = {
  72. vertex = {metal = "metal", glsl = "120"},
  73. fragment = {metal = "metal", glsl = "120"},
  74. compute = {metal = "metal", glsl = "430"}
  75. },
  76. linux = {
  77. vertex = {glsl = "120", spirv = "spirv"},
  78. fragment = {glsl = "120", spirv = "spirv"},
  79. compute = {glsl = "430", spirv = "spirv"}
  80. }
  81. }
  82. -- build command args
  83. local args = {
  84. "-f", shaderfile,
  85. "--type", shader_type,
  86. "--varyingdef", vardef_filename,
  87. "--platform", bgfx_platforms[target:plat()],
  88. }
  89. for _, includedir in ipairs(target:get("includedirs")) do
  90. table.insert(args, "-i")
  91. table.insert(args, includedir)
  92. end
  93. local mtime = 0
  94. local shader_profiles
  95. if fileconfig and fileconfig.profiles then
  96. shader_profiles = fileconfig.profiles
  97. else
  98. shader_profiles = bgfx_default_profiles[target:plat()][shader_type]
  99. end
  100. for folder, profile in pairs(shader_profiles) do
  101. -- set output dir
  102. local outputdir = path.join(target:targetdir(), output_dir, folder)
  103. batchcmds:mkdir(outputdir)
  104. local binary = path.join(outputdir, output_filename)
  105. -- compiling
  106. local real_args = {}
  107. table.join2(real_args, args)
  108. table.insert(real_args, "-o")
  109. table.insert(real_args, binary)
  110. table.insert(real_args, "--profile")
  111. table.insert(real_args, profile)
  112. if option.get("verbose") then
  113. batchcmds:show(shaderc .. " " .. os.args(real_args))
  114. end
  115. batchcmds:vrunv(shaderc, real_args)
  116. if (mtime == 0) then mtime = os.mtime(binary) end
  117. end
  118. -- add deps
  119. batchcmds:add_depfiles(shaderfile)
  120. batchcmds:set_depmtime(mtime)
  121. end)