compile_shaders.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. -- Compile shaders to includables headers
  2. rule("compile.shaders")
  3. set_extensions(".nzsl", ".nzslb")
  4. add_deps("@nzsl/find_nzsl")
  5. on_config(function (target)
  6. local archives = {}
  7. for _, sourcebatch in pairs(target:sourcebatches()) do
  8. local rulename = sourcebatch.rulename
  9. if rulename == "@nzsl/compile.shaders" then
  10. for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
  11. local fileconfig = target:fileconfig(sourcefile)
  12. if fileconfig and fileconfig.archive then
  13. local archivefiles = archives[fileconfig.archive]
  14. if not archivefiles then
  15. archivefiles = {}
  16. archives[fileconfig.archive] = archivefiles
  17. end
  18. table.insert(archivefiles, path.join(path.directory(sourcefile), path.basename(sourcefile) .. ".nzslb"))
  19. end
  20. end
  21. end
  22. end
  23. if not table.empty(archives) then
  24. assert(target:rule("@nzsl/archive.shaders"), "you must add the @nzsl/archive.shaders rule to the target")
  25. for archive, archivefiles in table.orderpairs(archives) do
  26. local args = { rule = "@nzsl/archive.shaders", always_added = true, compress = true, files = archivefiles }
  27. if archive:endswith(".nzsla.h") or archive:endswith(".nzsla.hpp") then
  28. args.header = path.extension(archive)
  29. archive = archive:sub(1, -#args.header - 1) -- foo.nzsla.h => foo.nzsla
  30. end
  31. target:add("files", archive, args)
  32. end
  33. end
  34. end)
  35. before_buildcmd_file(function (target, batchcmds, shaderfile, opt)
  36. local outputdir = target:data("nzsl_includedirs")
  37. local nzslc = target:data("nzslc")
  38. local runenvs = target:data("nzsl_runenv")
  39. assert(nzslc, "nzslc not found! please install nzsl package with nzslc enabled")
  40. local fileconfig = target:fileconfig(shaderfile) or {}
  41. local header = fileconfig.archive == nil
  42. -- add commands
  43. batchcmds:show_progress(opt.progress, "${color.build.object}compiling.shader %s", shaderfile)
  44. local argv = { "--compile=nzslb" .. (header and "-header" or ""), "--partial", "--optimize" }
  45. if outputdir then
  46. batchcmds:mkdir(outputdir)
  47. table.insert(argv, "--output=" .. outputdir)
  48. end
  49. -- handle --log-format
  50. local kind = target:data("plugin.project.kind") or ""
  51. if kind:match("vs") then
  52. table.insert(argv, "--log-format=vs")
  53. end
  54. table.insert(argv, shaderfile)
  55. batchcmds:vrunv(nzslc.program, argv, { curdir = ".", envs = runenvs })
  56. local outputfile = path.join(outputdir or path.directory(shaderfile), path.basename(shaderfile) .. ".nzslb" .. (header and ".h" or ""))
  57. -- add deps
  58. batchcmds:add_depfiles(shaderfile)
  59. batchcmds:add_depvalues(nzslc.version)
  60. batchcmds:set_depmtime(os.mtime(outputfile))
  61. batchcmds:set_depcache(target:dependfile(outputfile))
  62. end)