diff options
| author | ruki <[email protected]> | 2021-11-30 23:09:45 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-11-30 23:09:45 +0800 |
| commit | 03ba395bd4e5057853907ba0d536707d710379a4 (patch) | |
| tree | 35fe9c94ac98609614d33fa0f382183531a864a0 | |
| parent | b5af4b27137ab12de8a53a7e62cf004eb488ba5f (diff) | |
add utils.glsl2spc
| -rw-r--r-- | tests/projects/other/glsl2spv/.gitignore | 8 | ||||
| -rw-r--r-- | tests/projects/other/glsl2spv/src/main.c | 16 | ||||
| -rw-r--r-- | tests/projects/other/glsl2spv/src/test.frag | 6 | ||||
| -rw-r--r-- | tests/projects/other/glsl2spv/src/test.vert | 5 | ||||
| -rw-r--r-- | tests/projects/other/glsl2spv/xmake.lua | 9 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/find_glslangValidator.lua | 52 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/find_glslc.lua | 61 | ||||
| -rw-r--r-- | xmake/rules/utils/bin2c/xmake.lua | 4 | ||||
| -rw-r--r-- | xmake/rules/utils/glsl2spv/xmake.lua | 90 |
9 files changed, 249 insertions, 2 deletions
diff --git a/tests/projects/other/glsl2spv/.gitignore b/tests/projects/other/glsl2spv/.gitignore new file mode 100644 index 000000000..152105761 --- /dev/null +++ b/tests/projects/other/glsl2spv/.gitignore @@ -0,0 +1,8 @@ +# Xmake cache +.xmake/ +build/ + +# MacOS Cache +.DS_Store + + diff --git a/tests/projects/other/glsl2spv/src/main.c b/tests/projects/other/glsl2spv/src/main.c new file mode 100644 index 000000000..56e54d771 --- /dev/null +++ b/tests/projects/other/glsl2spv/src/main.c @@ -0,0 +1,16 @@ +#include <stdio.h> + +static unsigned char g_test_vert_spv_data[] = { + #include "test.vert.spv.h" +}; + +static unsigned char g_test_frag_spv_data[] = { + #include "test.frag.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/glsl2spv/src/test.frag b/tests/projects/other/glsl2spv/src/test.frag new file mode 100644 index 000000000..9aeec4257 --- /dev/null +++ b/tests/projects/other/glsl2spv/src/test.frag @@ -0,0 +1,6 @@ +#version 330 +precision mediump float; + +void main() { +} + diff --git a/tests/projects/other/glsl2spv/src/test.vert b/tests/projects/other/glsl2spv/src/test.vert new file mode 100644 index 000000000..31dedf3e7 --- /dev/null +++ b/tests/projects/other/glsl2spv/src/test.vert @@ -0,0 +1,5 @@ +#version 330 +precision mediump float; + +void main() { +} diff --git a/tests/projects/other/glsl2spv/xmake.lua b/tests/projects/other/glsl2spv/xmake.lua new file mode 100644 index 000000000..3dfce3ed4 --- /dev/null +++ b/tests/projects/other/glsl2spv/xmake.lua @@ -0,0 +1,9 @@ +add_rules("mode.debug", "mode.release") + +target("test") + set_kind("binary") + add_rules("utils.glsl2spv", {bin2c = true}) + add_files("src/*.c") + add_files("src/*.vert", "src/*.frag") + + diff --git a/xmake/modules/detect/tools/find_glslangValidator.lua b/xmake/modules/detect/tools/find_glslangValidator.lua new file mode 100644 index 000000000..93bc94670 --- /dev/null +++ b/xmake/modules/detect/tools/find_glslangValidator.lua @@ -0,0 +1,52 @@ +--!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_glslangValidator.lua +-- + +-- imports +import("lib.detect.find_program") +import("lib.detect.find_programver") + +-- find glslangValidator +-- +-- @param opt the argument options, e.g. {version = true} +-- +-- @return program, version +-- +-- @code +-- +-- local glslangValidator = find_glslangValidator() +-- local glslangValidator, version = find_glslangValidator({program = "glslangValidator", version = true}) +-- +-- @endcode +-- +function main(opt) + + -- init options + opt = opt or {} + + -- find program + local program = find_program(opt.program or "glslangValidator", opt) + + -- find program version + local version = nil + if program and opt.version then + version = find_programver(program, opt) + end + return program, version +end diff --git a/xmake/modules/detect/tools/find_glslc.lua b/xmake/modules/detect/tools/find_glslc.lua new file mode 100644 index 000000000..ada89e672 --- /dev/null +++ b/xmake/modules/detect/tools/find_glslc.lua @@ -0,0 +1,61 @@ +--!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_glslc.lua +-- + +-- imports +import("lib.detect.find_program") +import("lib.detect.find_programver") +import("core.tool.toolchain") + +-- find glslc +-- +-- @param opt the argument options, e.g. {version = true} +-- +-- @return program, version +-- +-- @code +-- +-- local glslc = find_glslc() +-- local glslc, version = find_glslc({program = "glslc", version = true}) +-- +-- @endcode +-- +function main(opt) + + -- init options + opt = opt or {} + + -- find program + local program = find_program(opt.program or "glslc", opt) + if not program and is_plat("android") then + local ndk = toolchain.load("ndk"):config("ndk") + if ndk then + local prebuilt = (is_host("macosx") and "darwin" or os.host()) .. "-x86_64" + opt.paths = path.join(ndk, "shader-tools", prebuilt) + program = find_program(opt.program or "glslc", opt) + end + end + + -- find program version + local version = nil + if program and opt.version then + version = find_programver(program, opt) + end + return program, version +end diff --git a/xmake/rules/utils/bin2c/xmake.lua b/xmake/rules/utils/bin2c/xmake.lua index 03d27a534..77f81125c 100644 --- a/xmake/rules/utils/bin2c/xmake.lua +++ b/xmake/rules/utils/bin2c/xmake.lua @@ -21,7 +21,7 @@ rule("utils.bin2c") set_extensions(".bin") on_load(function (target) - local headerdir = path.join(target:autogendir(), "rules", "c++", "bin2c") + local headerdir = path.join(target:autogendir(), "rules", "utils", "bin2c") if not os.isdir(headerdir) then os.mkdir(headerdir) end @@ -30,7 +30,7 @@ rule("utils.bin2c") before_buildcmd_file(function (target, batchcmds, sourcefile_bin, opt) -- get header file - local headerdir = path.join(target:autogendir(), "rules", "c++", "bin2c") + local headerdir = path.join(target:autogendir(), "rules", "utils", "bin2c") local headerfile = path.join(headerdir, path.filename(sourcefile_bin) .. ".h") target:add("includedirs", headerdir) diff --git a/xmake/rules/utils/glsl2spv/xmake.lua b/xmake/rules/utils/glsl2spv/xmake.lua new file mode 100644 index 000000000..d34c670ec --- /dev/null +++ b/xmake/rules/utils/glsl2spv/xmake.lua @@ -0,0 +1,90 @@ +--!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 *.vert/*.frag to *.vert.spv/*.frag.spv files +-- add_rules("utils.glsl2spv", {outputdir = "build"}) +-- +-- compile *.vert/*.frag and generate binary c header files +-- add_rules("utils.glsl2spv", {bin2c = true}) +-- +-- in c code: +-- static unsigned char g_test_frag_spv_data[] = { +-- #include "test.frag.spv.h" +-- }; +-- +-- +rule("utils.glsl2spv") + set_extensions(".vert", ".frag") + on_load(function (target) + local is_bin2c = target:extraconf("rules", "utils.glsl2spv", "bin2c") + if is_bin2c then + local headerdir = path.join(target:autogendir(), "rules", "utils", "glsl2spv") + if not os.isdir(headerdir) then + os.mkdir(headerdir) + end + target:add("includedirs", headerdir) + end + end) + before_buildcmd_file(function (target, batchcmds, sourcefile_glsl, opt) + import("lib.detect.find_tool") + + -- get glslangValidator + local glslc + local glslangValidator = find_tool("glslangValidator") + if not glslangValidator then + glslc = find_tool("glslc") + end + assert(glslangValidator or glslc, "glslangValidator or glslc not found!") + + -- glsl to spv + local outputdir = target:extraconf("rules", "utils.glsl2spv", "outputdir") or path.join(target:autogendir(), "rules", "utils", "glsl2spv") + local spvfilepath = path.join(outputdir, path.filename(sourcefile_glsl) .. ".spv") + batchcmds:show_progress(opt.progress, "${color.build.object}generating.glsl2spv %s", sourcefile_glsl) + batchcmds:mkdir(outputdir) + if glslangValidator then + batchcmds:vrunv(glslangValidator.program, {"-V", "-o", spvfilepath, sourcefile_glsl}) + else + batchcmds:vrunv(glslc.program, {"-o", spvfilepath, sourcefile_glsl}) + end + + -- do bin2c + local outputfile = spvfilepath + local is_bin2c = target:extraconf("rules", "utils.glsl2spv", "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", "-i", spvfilepath, "-o", headerfile} + batchcmds:vrunv(os.programfile(), argv, {envs = {XMAKE_SKIP_HISTORY = "y"}}) + end + + -- add deps + batchcmds:add_depfiles(sourcefile_glsl) + batchcmds:set_depmtime(os.mtime(outputfile)) + batchcmds:set_depcache(target:dependfile(outputfile)) + end) + |
