summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLatias94 <[email protected]>2023-08-30 14:06:31 +0800
committerLatias94 <[email protected]>2023-08-30 14:06:31 +0800
commitdb699f189067513da3b591e9d9a1edeb59ade8b4 (patch)
tree60ef1fd5045c22a1ac7ea86cdaa9934f6973e518
parent44c70cdcb4f28e2499fb893f4d2e1d17e79bac22 (diff)
feat: add hlsl2spv
-rw-r--r--tests/projects/other/hlsl2spv/.gitignore8
-rw-r--r--tests/projects/other/hlsl2spv/src/main.c16
-rw-r--r--tests/projects/other/hlsl2spv/src/test.ps.hlsl10
-rw-r--r--tests/projects/other/hlsl2spv/src/test.vs.hlsl20
-rw-r--r--tests/projects/other/hlsl2spv/xmake.lua11
-rw-r--r--xmake/modules/detect/tools/find_dxc.lua59
-rw-r--r--xmake/rules/utils/hlsl2spv/xmake.lua108
7 files changed, 232 insertions, 0 deletions
diff --git a/tests/projects/other/hlsl2spv/.gitignore b/tests/projects/other/hlsl2spv/.gitignore
new file mode 100644
index 000000000..152105761
--- /dev/null
+++ b/tests/projects/other/hlsl2spv/.gitignore
@@ -0,0 +1,8 @@
+# Xmake cache
+.xmake/
+build/
+
+# MacOS Cache
+.DS_Store
+
+
diff --git a/tests/projects/other/hlsl2spv/src/main.c b/tests/projects/other/hlsl2spv/src/main.c
new file mode 100644
index 000000000..29a65edb1
--- /dev/null
+++ b/tests/projects/other/hlsl2spv/src/main.c
@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+static unsigned char g_test_vert_spv_data[] = {
+ #include "test.vs.spv.h"
+};
+
+static unsigned char g_test_frag_spv_data[] = {
+ #include "test.ps.spv.h"
+};
+
+int main(int argc, char** argv)
+{
+ printf("test.vert.spv: %s, size: %d\n", g_test_vert_spv_data, (int)sizeof(g_test_vert_spv_data));
+ printf("test.frag.spv: %s, size: %d\n", g_test_frag_spv_data, (int)sizeof(g_test_frag_spv_data));
+ return 0;
+}
diff --git a/tests/projects/other/hlsl2spv/src/test.ps.hlsl b/tests/projects/other/hlsl2spv/src/test.ps.hlsl
new file mode 100644
index 000000000..95c63d53c
--- /dev/null
+++ b/tests/projects/other/hlsl2spv/src/test.ps.hlsl
@@ -0,0 +1,10 @@
+struct PS_INPUT
+{
+ float4 pos : SV_POSITION;
+ float3 color : COLOR0;
+};
+
+float4 main(PS_INPUT input) : SV_Target
+{
+ return float4(input.color, 1.0);
+}
diff --git a/tests/projects/other/hlsl2spv/src/test.vs.hlsl b/tests/projects/other/hlsl2spv/src/test.vs.hlsl
new file mode 100644
index 000000000..ca2cf875d
--- /dev/null
+++ b/tests/projects/other/hlsl2spv/src/test.vs.hlsl
@@ -0,0 +1,20 @@
+struct VS_INPUT
+{
+ float2 pos : POSITION;
+ float3 color : COLOR0;
+};
+
+struct VS_OUTPUT
+{
+ float4 pos : SV_POSITION;
+ float3 color : COLOR0;
+};
+
+VS_OUTPUT main(VS_INPUT input)
+{
+ VS_OUTPUT output;
+ output.pos = float4(input.pos, 0.0, 1.0);
+ output.color = input.color;
+
+ return output;
+}
diff --git a/tests/projects/other/hlsl2spv/xmake.lua b/tests/projects/other/hlsl2spv/xmake.lua
new file mode 100644
index 000000000..0487fba5d
--- /dev/null
+++ b/tests/projects/other/hlsl2spv/xmake.lua
@@ -0,0 +1,11 @@
+add_rules("mode.debug", "mode.release")
+
+add_requires("glslang", {configs = {binaryonly = true}})
+
+target("test")
+ set_kind("binary")
+ add_rules("utils.hlsl2spv", {bin2c = true})
+ add_files("src/*.c")
+ add_files("src/*.hlsl", "src/*.hlsl")
+ add_packages("directxshadercompiler")
+
diff --git a/xmake/modules/detect/tools/find_dxc.lua b/xmake/modules/detect/tools/find_dxc.lua
new file mode 100644
index 000000000..075bbb6c3
--- /dev/null
+++ b/xmake/modules/detect/tools/find_dxc.lua
@@ -0,0 +1,59 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file find_dxc.lua
+--
+
+-- imports
+import("lib.detect.find_program")
+import("lib.detect.find_programver")
+
+-- find dxc
+--
+-- @param opt the argument options, e.g. {version = true, program = "c:\xxx\dxc.exe"}
+--
+-- @return program, version
+--
+-- @code
+--
+-- local dxc = find_dxc()
+-- local dxc, version = find_dxc({version = true})
+-- local dxc, version = find_dxc({version = true, program = "c:\xxx\dxc.exe"})
+--
+-- @endcode
+--
+function main(opt)
+
+ -- init options
+ opt = opt or {}
+
+ -- find program
+ opt.paths = opt.paths or
+ {
+ "$(env VULKAN_SDK)/Bin",
+ "$(env VK_SDK_PATH)/Bin",
+ "$(env PATH)"
+ }
+ local program = find_program(opt.program or "dxc.exe", opt)
+
+ -- find program version
+ local version = nil
+ if program and opt and opt.version then
+ version = find_programver(program, opt)
+ end
+ return program, version
+end
diff --git a/xmake/rules/utils/hlsl2spv/xmake.lua b/xmake/rules/utils/hlsl2spv/xmake.lua
new file mode 100644
index 000000000..505dc91df
--- /dev/null
+++ b/xmake/rules/utils/hlsl2spv/xmake.lua
@@ -0,0 +1,108 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file xmake.lua
+--
+
+-- compile glsl shader to spirv file, .spv
+--
+-- e.g.
+-- compile *.hlsl/*.hlsl to *.spv/*.spv files
+-- add_rules("utils.hlsl2spv", {outputdir = "build"})
+--
+-- compile *.vert/*.frag and generate binary c header files
+-- add_rules("utils.hlsl2spv", {bin2c = true})
+--
+-- in c code:
+-- static unsigned char g_test_frag_spv_data[] = {
+-- #include "test.spv.h"
+-- };
+--
+--
+rule("utils.hlsl2spv")
+ set_extensions(".hlsl")
+ on_load(function (target)
+ local is_bin2c = target:extraconf("rules", "utils.hlsl2spv", "bin2c")
+ if is_bin2c then
+ local headerdir = path.join(target:autogendir(), "rules", "utils", "hlsl2spv")
+ if not os.isdir(headerdir) then
+ os.mkdir(headerdir)
+ end
+ target:add("includedirs", headerdir)
+ end
+ end)
+
+ before_buildcmd_file(function (target, batchcmds, sourcefile_hlsl, opt)
+ import("lib.detect.find_tool")
+
+ local dxc = find_tool("dxc")
+ assert(dxc, "dxc not found!")
+
+ -- hlsl to spv
+ local basename_with_type = path.basename(sourcefile_hlsl)
+ local shadertype = string.sub(path.extension(basename_with_type), 2, -1)
+
+ if shadertype == "" then
+ -- if not specify shader type, considered it a header, skip
+ print("[WARN] hlsl2spv: shader type not specified, skip %s", sourcefile_hlsl)
+ return
+ end
+
+ local targetenv = target:extraconf("rules", "utils.hlsl2spv", "targetenv") or "vulkan1.0"
+ local outputdir = target:extraconf("rules", "utils.hlsl2spv", "outputdir") or path.join(target:autogendir(), "rules", "utils", "hlsl2spv")
+ local hlslversion = target:extraconf("rules", "utils.hlsl2spv", "hlslversion") or "2018"
+ local spvfilepath = path.join(outputdir, basename_with_type .. ".spv")
+
+ local shadermodel = target:extraconf("rules", "utils.hlsl2spv", "shadermodel") or "6.0"
+ local _sm = string.gsub(shadermodel, "%.", "_")
+
+ local dxc_profile = shadertype .. "_" .. _sm
+
+ batchcmds:show_progress(opt.progress, "${color.build.object}compiling.hlsl %s", sourcefile_hlsl)
+ batchcmds:mkdir(outputdir)
+
+ batchcmds:vrunv(dxc.program, {path(sourcefile_hlsl), "-spirv", "-HV", hlslversion, "-fspv-target-env=" .. targetenv, "-E", "main", "-T", dxc_profile, "-Fo", path(spvfilepath)})
+
+ -- bin2c
+ local outputfile = spvfilepath
+ local is_bin2c = target:extraconf("rules", "utils.hlsl2spv", "bin2c")
+
+ if is_bin2c then
+ -- get header file
+ local headerdir = outputdir
+ local headerfile = path.join(headerdir, path.filename(spvfilepath) .. ".h")
+
+ target:add("includedirs", headerdir)
+ outputfile = headerfile
+
+ -- add commands
+ local argv = {"lua", "private.utils.bin2c", "--nozeroend", "-i", path(spvfilepath), "-o", path(headerfile)}
+ batchcmds:vrunv(os.programfile(), argv, {envs = {XMAKE_SKIP_HISTORY = "y"}})
+ end
+
+ batchcmds:add_depfiles(sourcefile_hlsl)
+ batchcmds:set_depmtime(os.mtime(outputfile))
+ batchcmds:set_depcache(target:dependfile(outputfile))
+ end)
+
+ after_clean(function (target, batchcmds, sourcefile_hlsl)
+ import("private.action.clean.remove_files")
+
+ local outputdir = target:extraconf("rules", "utils.hlsl2spv", "outputdir") or path.join(target:targetdir(), "shader")
+ remove_files(path.join(outputdir, "*.spv"))
+ remove_files(path.join(outputdir, "*.spv.h"))
+ end)