From 0187b34737611b7b194761ef1a241dd4ac6eee05 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 23:00:26 +0800 Subject: add bin2obj support for glsl/hlsl2spv --- tests/projects/other/glsl2spv/src/main.c | 3 +- tests/projects/other/glsl2spv_bin2obj/src/main.c | 36 +++++++ .../projects/other/glsl2spv_bin2obj/src/test.frag | 6 ++ .../projects/other/glsl2spv_bin2obj/src/test.vert | 6 ++ tests/projects/other/glsl2spv_bin2obj/xmake.lua | 11 ++ tests/projects/other/hlsl2spv/src/main.c | 3 +- tests/projects/other/hlsl2spv_bin2obj/src/main.c | 36 +++++++ .../other/hlsl2spv_bin2obj/src/test.ps.hlsl | 9 ++ .../other/hlsl2spv_bin2obj/src/test.vs.hlsl | 17 +++ tests/projects/other/hlsl2spv_bin2obj/xmake.lua | 11 ++ xmake/rules/utils/bin2obj/utils.lua | 115 +++++++++++++++++++++ xmake/rules/utils/bin2obj/xmake.lua | 66 +----------- xmake/rules/utils/glsl2spv/xmake.lua | 21 +++- xmake/rules/utils/hlsl2spv/xmake.lua | 32 ++++-- 14 files changed, 298 insertions(+), 74 deletions(-) create mode 100644 tests/projects/other/glsl2spv_bin2obj/src/main.c create mode 100644 tests/projects/other/glsl2spv_bin2obj/src/test.frag create mode 100644 tests/projects/other/glsl2spv_bin2obj/src/test.vert create mode 100644 tests/projects/other/glsl2spv_bin2obj/xmake.lua create mode 100644 tests/projects/other/hlsl2spv_bin2obj/src/main.c create mode 100644 tests/projects/other/hlsl2spv_bin2obj/src/test.ps.hlsl create mode 100644 tests/projects/other/hlsl2spv_bin2obj/src/test.vs.hlsl create mode 100644 tests/projects/other/hlsl2spv_bin2obj/xmake.lua create mode 100644 xmake/rules/utils/bin2obj/utils.lua diff --git a/tests/projects/other/glsl2spv/src/main.c b/tests/projects/other/glsl2spv/src/main.c index 56e54d771..eba8286ae 100644 --- a/tests/projects/other/glsl2spv/src/main.c +++ b/tests/projects/other/glsl2spv/src/main.c @@ -8,8 +8,7 @@ static unsigned char g_test_frag_spv_data[] = { #include "test.frag.spv.h" }; -int main(int argc, char** argv) -{ +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_bin2obj/src/main.c b/tests/projects/other/glsl2spv_bin2obj/src/main.c new file mode 100644 index 000000000..64d62ce8b --- /dev/null +++ b/tests/projects/other/glsl2spv_bin2obj/src/main.c @@ -0,0 +1,36 @@ +#include +#include + +extern const uint8_t _binary_test_vert_spv_start[]; +extern const uint8_t _binary_test_vert_spv_end[]; + +extern const uint8_t _binary_test_frag_spv_start[]; +extern const uint8_t _binary_test_frag_spv_end[]; + +int main(int argc, char** argv) { + const uint32_t vert_size = (uint32_t)(_binary_test_vert_spv_end - _binary_test_vert_spv_start); + const uint32_t frag_size = (uint32_t)(_binary_test_frag_spv_end - _binary_test_frag_spv_start); + + printf("test.vert.spv: size: %u bytes\n", vert_size); + printf("test.frag.spv: size: %u bytes\n", frag_size); + + // Print first few bytes + if (vert_size > 0) { + printf("test.vert.spv first 4 bytes: "); + for (uint32_t i = 0; i < 4 && i < vert_size; i++) { + printf("%02x ", _binary_test_vert_spv_start[i]); + } + printf("\n"); + } + + if (frag_size > 0) { + printf("test.frag.spv first 4 bytes: "); + for (uint32_t i = 0; i < 4 && i < frag_size; i++) { + printf("%02x ", _binary_test_frag_spv_start[i]); + } + printf("\n"); + } + + return 0; +} + diff --git a/tests/projects/other/glsl2spv_bin2obj/src/test.frag b/tests/projects/other/glsl2spv_bin2obj/src/test.frag new file mode 100644 index 000000000..9aeec4257 --- /dev/null +++ b/tests/projects/other/glsl2spv_bin2obj/src/test.frag @@ -0,0 +1,6 @@ +#version 330 +precision mediump float; + +void main() { +} + diff --git a/tests/projects/other/glsl2spv_bin2obj/src/test.vert b/tests/projects/other/glsl2spv_bin2obj/src/test.vert new file mode 100644 index 000000000..9aeec4257 --- /dev/null +++ b/tests/projects/other/glsl2spv_bin2obj/src/test.vert @@ -0,0 +1,6 @@ +#version 330 +precision mediump float; + +void main() { +} + diff --git a/tests/projects/other/glsl2spv_bin2obj/xmake.lua b/tests/projects/other/glsl2spv_bin2obj/xmake.lua new file mode 100644 index 000000000..36382be66 --- /dev/null +++ b/tests/projects/other/glsl2spv_bin2obj/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.glsl2spv", {bin2obj = true}) + add_files("src/*.c") + add_files("src/*.vert", "src/*.frag") + add_packages("glslang") + diff --git a/tests/projects/other/hlsl2spv/src/main.c b/tests/projects/other/hlsl2spv/src/main.c index 29a65edb1..30c475abe 100644 --- a/tests/projects/other/hlsl2spv/src/main.c +++ b/tests/projects/other/hlsl2spv/src/main.c @@ -8,8 +8,7 @@ static unsigned char g_test_frag_spv_data[] = { #include "test.ps.spv.h" }; -int main(int argc, char** argv) -{ +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_bin2obj/src/main.c b/tests/projects/other/hlsl2spv_bin2obj/src/main.c new file mode 100644 index 000000000..343e5ac2f --- /dev/null +++ b/tests/projects/other/hlsl2spv_bin2obj/src/main.c @@ -0,0 +1,36 @@ +#include +#include + +extern const uint8_t _binary_test_vs_spv_start[]; +extern const uint8_t _binary_test_vs_spv_end[]; + +extern const uint8_t _binary_test_ps_spv_start[]; +extern const uint8_t _binary_test_ps_spv_end[]; + +int main(int argc, char** argv) { + const uint32_t vs_size = (uint32_t)(_binary_test_vs_spv_end - _binary_test_vs_spv_start); + const uint32_t ps_size = (uint32_t)(_binary_test_ps_spv_end - _binary_test_ps_spv_start); + + printf("test.vs.spv: size: %u bytes\n", vs_size); + printf("test.ps.spv: size: %u bytes\n", ps_size); + + // Print first few bytes + if (vs_size > 0) { + printf("test.vs.spv first 4 bytes: "); + for (uint32_t i = 0; i < 4 && i < vs_size; i++) { + printf("%02x ", _binary_test_vs_spv_start[i]); + } + printf("\n"); + } + + if (ps_size > 0) { + printf("test.ps.spv first 4 bytes: "); + for (uint32_t i = 0; i < 4 && i < ps_size; i++) { + printf("%02x ", _binary_test_ps_spv_start[i]); + } + printf("\n"); + } + + return 0; +} + diff --git a/tests/projects/other/hlsl2spv_bin2obj/src/test.ps.hlsl b/tests/projects/other/hlsl2spv_bin2obj/src/test.ps.hlsl new file mode 100644 index 000000000..5c534d30b --- /dev/null +++ b/tests/projects/other/hlsl2spv_bin2obj/src/test.ps.hlsl @@ -0,0 +1,9 @@ +struct PSInput { + float4 position : SV_POSITION; + float2 texcoord : TEXCOORD0; +}; + +float4 main(PSInput input) : SV_TARGET { + return float4(input.texcoord, 0.0, 1.0); +} + diff --git a/tests/projects/other/hlsl2spv_bin2obj/src/test.vs.hlsl b/tests/projects/other/hlsl2spv_bin2obj/src/test.vs.hlsl new file mode 100644 index 000000000..e86b3b263 --- /dev/null +++ b/tests/projects/other/hlsl2spv_bin2obj/src/test.vs.hlsl @@ -0,0 +1,17 @@ +struct VSInput { + float3 position : POSITION; + float2 texcoord : TEXCOORD0; +}; + +struct VSOutput { + float4 position : SV_POSITION; + float2 texcoord : TEXCOORD0; +}; + +VSOutput main(VSInput input) { + VSOutput output; + output.position = float4(input.position, 1.0); + output.texcoord = input.texcoord; + return output; +} + diff --git a/tests/projects/other/hlsl2spv_bin2obj/xmake.lua b/tests/projects/other/hlsl2spv_bin2obj/xmake.lua new file mode 100644 index 000000000..2184d24cc --- /dev/null +++ b/tests/projects/other/hlsl2spv_bin2obj/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", {bin2obj = true}) + add_files("src/*.c") + add_files("src/*.hlsl", "src/*.hlsl") + add_packages("directxshadercompiler") + diff --git a/xmake/rules/utils/bin2obj/utils.lua b/xmake/rules/utils/bin2obj/utils.lua new file mode 100644 index 000000000..b658928a1 --- /dev/null +++ b/xmake/rules/utils/bin2obj/utils.lua @@ -0,0 +1,115 @@ +--!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, Xmake Open Source Community. +-- +-- @author ruki +-- @file utils.lua +-- + +-- generate object file from binary file +-- +-- @param target the target +-- @param batchcmds the batch commands +-- @param binaryfile the binary file path +-- @param opt the options +-- - progress: the progress callback +-- - format: the object file format (coff, elf, macho), auto-detected if not provided +-- - symbol_prefix: the symbol prefix (default: _binary_) +-- - zeroend: append null terminator (default: false) +-- - rulename: the rule name for getting extra config (default: utils.bin2obj) +-- - objectfile: the output object file path (optional, auto-generated if not provided) +-- +function generate_objectfile(target, batchcmds, binaryfile, opt) + opt = opt or {} + local rulename = opt.rulename or "utils.bin2obj" + local progress = opt.progress + + -- get format (default: auto-detect from platform) + local format = opt.format or target:extraconf("rules", rulename, "format") + if not format then + if target:is_plat("windows", "mingw", "msys", "cygwin") then + format = "coff" + elseif target:is_plat("macosx", "iphoneos", "watchos", "appletvos") then + format = "macho" + else + format = "elf" + end + end + + -- get object file + local objectfile = opt.objectfile + if not objectfile then + objectfile = target:objectfile(binaryfile) + -- adjust extension based on format (use .obj for COFF, .o for others) + local objext = (format == "coff") and ".obj" or ".o" + objectfile = objectfile:gsub("%.o$", objext):gsub("%.obj$", objext) + end + table.insert(target:objectfiles(), objectfile) + + -- add commands + if progress then + batchcmds:show_progress(progress, "${color.build.object}generating.bin2obj %s", binaryfile) + end + batchcmds:mkdir(path.directory(objectfile)) + + -- get symbol prefix (default: _binary_) + local symbol_prefix = opt.symbol_prefix or target:extraconf("rules", rulename, "symbol_prefix") or "_binary_" + + -- get zeroend (default: false, but can be overridden) + local zeroend = opt.zeroend + if zeroend == nil then + zeroend = target:extraconf("rules", rulename, "zeroend") or false + end + + -- get architecture and platform + local arch = target:arch() + local plat = target:plat() + + -- get target_minver and xcode_sdkver from xcode toolchain (if available) + local target_minver = nil + local xcode_sdkver = nil + if format == "macho" then + local toolchain = target:toolchain("xcode") + if toolchain then + target_minver = toolchain:config("target_minver") + xcode_sdkver = toolchain:config("xcode_sdkver") + end + end + + -- convert binary file to object file + local argv = { + "-i", path(binaryfile), + "-o", path(objectfile), + "-f", format, + "-a", arch, + "-p", plat + } + if symbol_prefix ~= "_binary_" then + table.insert(argv, "--symbol_prefix=" .. symbol_prefix) + end + if target_minver then + table.insert(argv, "--target_minver=" .. target_minver) + end + if xcode_sdkver then + table.insert(argv, "--xcode_sdkver=" .. xcode_sdkver) + end + if zeroend then + table.insert(argv, "--zeroend") + end + batchcmds:vlua("utils.binary.bin2obj", argv) + + return objectfile +end + diff --git a/xmake/rules/utils/bin2obj/xmake.lua b/xmake/rules/utils/bin2obj/xmake.lua index 69fd9e717..760b9d536 100644 --- a/xmake/rules/utils/bin2obj/xmake.lua +++ b/xmake/rules/utils/bin2obj/xmake.lua @@ -22,74 +22,18 @@ rule("utils.bin2obj") set_extensions(".bin") add_orders("utils.bin2obj", "c++.build.modules.builder") on_buildcmd_file(function (target, batchcmds, sourcefile_bin, opt) - - -- get format (default: auto-detect from platform) - local format = target:extraconf("rules", "utils.bin2obj", "format") - if not format then - if target:is_plat("windows", "mingw", "msys", "cygwin") then - format = "coff" - elseif target:is_plat("macosx", "iphoneos", "watchos", "appletvos") then - format = "macho" - else - format = "elf" - end - end - - -- get object file (use .obj for COFF, .o for others) - local objext = (format == "coff") and ".obj" or ".o" - local objectfile = path.join(target:objectdir(), path.basename(sourcefile_bin) .. objext) - table.insert(target:objectfiles(), objectfile) - - -- add commands - batchcmds:show_progress(opt.progress, "${color.build.object}generating.bin2obj %s", sourcefile_bin) - batchcmds:mkdir(path.directory(objectfile)) - - -- get symbol prefix (default: _binary_) - local symbol_prefix = target:extraconf("rules", "utils.bin2obj", "symbol_prefix") or "_binary_" + import("rules.utils.bin2obj.utils", {alias = "bin2obj_utils", rootdir = os.programdir()}) -- get zeroend (default: false) -- check file-level config first, then rule-level config local fileconfig = target:fileconfig(sourcefile_bin) local zeroend = (fileconfig and fileconfig.zeroend) or target:extraconf("rules", "utils.bin2obj", "zeroend") or false - -- get architecture - local arch = target:arch() - - -- get platform - local plat = target:plat() - - -- get target_minver and xcode_sdkver from xcode toolchain (if available) - local target_minver = nil - local xcode_sdkver = nil - if format == "macho" then - local toolchain = target:toolchain("xcode") - if toolchain then - target_minver = toolchain:config("target_minver") - xcode_sdkver = toolchain:config("xcode_sdkver") - end - end - -- convert binary file to object file - local argv = { - "-i", path(sourcefile_bin), - "-o", path(objectfile), - "-f", format, - "-a", arch, - "-p", plat - } - if symbol_prefix ~= "_binary_" then - table.insert(argv, "--symbol_prefix=" .. symbol_prefix) - end - if target_minver then - table.insert(argv, "--target_minver=" .. target_minver) - end - if xcode_sdkver then - table.insert(argv, "--xcode_sdkver=" .. xcode_sdkver) - end - if zeroend then - table.insert(argv, "--zeroend") - end - batchcmds:vlua("utils.binary.bin2obj", argv) + local objectfile = bin2obj_utils.generate_objectfile(target, batchcmds, sourcefile_bin, { + progress = opt.progress, + zeroend = zeroend + }) -- add deps batchcmds:add_depfiles(sourcefile_bin) diff --git a/xmake/rules/utils/glsl2spv/xmake.lua b/xmake/rules/utils/glsl2spv/xmake.lua index a6c4a2aef..7fc456c80 100644 --- a/xmake/rules/utils/glsl2spv/xmake.lua +++ b/xmake/rules/utils/glsl2spv/xmake.lua @@ -27,11 +27,19 @@ -- compile *.vert/*.frag and generate binary c header files -- add_rules("utils.glsl2spv", {bin2c = true}) -- --- in c code: +-- compile *.vert/*.frag and generate object files for direct linking +-- add_rules("utils.glsl2spv", {bin2obj = true}) +-- +-- in c code (bin2c): -- static unsigned char g_test_frag_spv_data[] = { -- #include "test.frag.spv.h" -- }; -- +-- in c code (bin2obj): +-- extern const uint8_t _binary_test_frag_spv_start[]; +-- extern const uint8_t _binary_test_frag_spv_end[]; +-- const uint32_t size = (uint32_t)(_binary_test_frag_spv_end - _binary_test_frag_spv_start); +-- -- rule("utils.glsl2spv") set_extensions(".vert", ".tesc", ".tese", ".geom", ".comp", ".frag", ".comp", ".mesh", ".task", ".rgen", ".rint", ".rahit", ".rchit", ".rmiss", ".rcall", ".glsl") @@ -47,6 +55,7 @@ rule("utils.glsl2spv") end) before_buildcmd_file(function (target, batchcmds, sourcefile_glsl, opt) import("lib.detect.find_tool") + import("rules.utils.bin2obj.utils", {alias = "bin2obj_utils", rootdir = os.programdir()}) -- get glslangValidator local glslc @@ -74,9 +83,10 @@ rule("utils.glsl2spv") batchcmds:vrunv(glslc.program, {"--target-env", targetenv, "-o", path(spvfilepath), path(sourcefile_glsl)}) end - -- do bin2c + -- do bin2c or bin2obj local outputfile = spvfilepath local is_bin2c = target:extraconf("rules", "utils.glsl2spv", "bin2c") + local is_bin2obj = target:extraconf("rules", "utils.glsl2spv", "bin2obj") if is_bin2c then -- get header file local headerdir = outputdir @@ -87,6 +97,13 @@ rule("utils.glsl2spv") -- add commands local argv = {"--nozeroend", "-i", path(spvfilepath), "-o", path(headerfile)} batchcmds:vlua("utils.binary.bin2c", argv) + elseif is_bin2obj then + -- convert to object file using bin2obj + local objectfile = bin2obj_utils.generate_objectfile(target, batchcmds, spvfilepath, { + progress = opt.progress, + zeroend = true -- default enable zeroend for shader data + }) + outputfile = objectfile end -- add deps diff --git a/xmake/rules/utils/hlsl2spv/xmake.lua b/xmake/rules/utils/hlsl2spv/xmake.lua index 4af520420..de84aa3ab 100644 --- a/xmake/rules/utils/hlsl2spv/xmake.lua +++ b/xmake/rules/utils/hlsl2spv/xmake.lua @@ -18,20 +18,28 @@ -- @file xmake.lua -- --- compile glsl shader to spirv file, .spv +-- compile hlsl shader to spirv file, .spv -- -- e.g. --- compile *.hlsl/*.hlsl to *.spv/*.spv files +-- compile *.hlsl to *.spv files -- add_rules("utils.hlsl2spv", {outputdir = "build"}) -- --- compile *.vert/*.frag and generate binary c header files +-- compile *.hlsl 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" +-- compile *.hlsl and generate object files for direct linking +-- add_rules("utils.hlsl2spv", {bin2obj = true}) +-- +-- in c code (bin2c): +-- static unsigned char g_test_ps_spv_data[] = { +-- #include "test.ps.spv.h" -- }; -- +-- in c code (bin2obj): +-- extern const uint8_t _binary_test_ps_spv_start[]; +-- extern const uint8_t _binary_test_ps_spv_end[]; +-- const uint32_t size = (uint32_t)(_binary_test_ps_spv_end - _binary_test_ps_spv_start); +-- -- rule("utils.hlsl2spv") set_extensions(".hlsl") @@ -48,6 +56,7 @@ rule("utils.hlsl2spv") before_buildcmd_file(function (target, batchcmds, sourcefile_hlsl, opt) import("lib.detect.find_tool") + import("rules.utils.bin2obj.utils", {alias = "bin2obj_utils", rootdir = os.programdir()}) local dxc = assert(find_tool("dxc"), "dxc not found!") @@ -73,9 +82,10 @@ rule("utils.hlsl2spv") 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 + -- bin2c or bin2obj local outputfile = spvfilepath local is_bin2c = target:extraconf("rules", "utils.hlsl2spv", "bin2c") + local is_bin2obj = target:extraconf("rules", "utils.hlsl2spv", "bin2obj") if is_bin2c then -- get header file local headerdir = outputdir @@ -87,6 +97,13 @@ rule("utils.hlsl2spv") -- add commands local argv = {"--nozeroend", "-i", path(spvfilepath), "-o", path(headerfile)} batchcmds:vlua("utils.binary.bin2c", argv) + elseif is_bin2obj then + -- convert to object file using bin2obj + local objectfile = bin2obj_utils.generate_objectfile(target, batchcmds, spvfilepath, { + progress = opt.progress, + zeroend = true -- default enable zeroend for shader data + }) + outputfile = objectfile end batchcmds:add_depfiles(sourcefile_hlsl) @@ -100,4 +117,5 @@ rule("utils.hlsl2spv") 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")) + -- object files are cleaned automatically by xmake end) -- cgit v1.3.1 From eecb6774a518fbe5df5c5820fa35bd3d8def8dd9 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 23:16:00 +0800 Subject: improve bin2c --- tests/projects/other/glsl2spv/.gitignore | 8 -- tests/projects/other/glsl2spv/src/main.c | 15 ---- tests/projects/other/glsl2spv/src/test.frag | 6 -- tests/projects/other/glsl2spv/src/test.vert | 5 -- tests/projects/other/glsl2spv/xmake.lua | 11 --- tests/projects/other/glsl2spv_bin2c/src/main.c | 99 ++++++++++++++++++++++ tests/projects/other/glsl2spv_bin2c/src/test.frag | 6 ++ tests/projects/other/glsl2spv_bin2c/src/test.vert | 6 ++ tests/projects/other/glsl2spv_bin2c/xmake.lua | 11 +++ tests/projects/other/hlsl2spv/.gitignore | 8 -- tests/projects/other/hlsl2spv/src/main.c | 15 ---- tests/projects/other/hlsl2spv/src/test.ps.hlsl | 10 --- tests/projects/other/hlsl2spv/src/test.vs.hlsl | 19 ----- tests/projects/other/hlsl2spv/xmake.lua | 11 --- tests/projects/other/hlsl2spv_bin2c/src/main.c | 99 ++++++++++++++++++++++ .../projects/other/hlsl2spv_bin2c/src/test.ps.hlsl | 9 ++ .../projects/other/hlsl2spv_bin2c/src/test.vs.hlsl | 17 ++++ tests/projects/other/hlsl2spv_bin2c/xmake.lua | 11 +++ xmake/rules/utils/bin2c/utils.lua | 98 +++++++++++++++++++++ xmake/rules/utils/bin2c/xmake.lua | 30 ++----- xmake/rules/utils/glsl2spv/xmake.lua | 15 ++-- xmake/rules/utils/hlsl2spv/xmake.lua | 16 ++-- 22 files changed, 375 insertions(+), 150 deletions(-) delete mode 100644 tests/projects/other/glsl2spv/.gitignore delete mode 100644 tests/projects/other/glsl2spv/src/main.c delete mode 100644 tests/projects/other/glsl2spv/src/test.frag delete mode 100644 tests/projects/other/glsl2spv/src/test.vert delete mode 100644 tests/projects/other/glsl2spv/xmake.lua create mode 100644 tests/projects/other/glsl2spv_bin2c/src/main.c create mode 100644 tests/projects/other/glsl2spv_bin2c/src/test.frag create mode 100644 tests/projects/other/glsl2spv_bin2c/src/test.vert create mode 100644 tests/projects/other/glsl2spv_bin2c/xmake.lua delete mode 100644 tests/projects/other/hlsl2spv/.gitignore delete mode 100644 tests/projects/other/hlsl2spv/src/main.c delete mode 100644 tests/projects/other/hlsl2spv/src/test.ps.hlsl delete mode 100644 tests/projects/other/hlsl2spv/src/test.vs.hlsl delete mode 100644 tests/projects/other/hlsl2spv/xmake.lua create mode 100644 tests/projects/other/hlsl2spv_bin2c/src/main.c create mode 100644 tests/projects/other/hlsl2spv_bin2c/src/test.ps.hlsl create mode 100644 tests/projects/other/hlsl2spv_bin2c/src/test.vs.hlsl create mode 100644 tests/projects/other/hlsl2spv_bin2c/xmake.lua create mode 100644 xmake/rules/utils/bin2c/utils.lua diff --git a/tests/projects/other/glsl2spv/.gitignore b/tests/projects/other/glsl2spv/.gitignore deleted file mode 100644 index 152105761..000000000 --- a/tests/projects/other/glsl2spv/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# 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 deleted file mode 100644 index eba8286ae..000000000 --- a/tests/projects/other/glsl2spv/src/main.c +++ /dev/null @@ -1,15 +0,0 @@ -#include - -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 deleted file mode 100644 index 9aeec4257..000000000 --- a/tests/projects/other/glsl2spv/src/test.frag +++ /dev/null @@ -1,6 +0,0 @@ -#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 deleted file mode 100644 index 31dedf3e7..000000000 --- a/tests/projects/other/glsl2spv/src/test.vert +++ /dev/null @@ -1,5 +0,0 @@ -#version 330 -precision mediump float; - -void main() { -} diff --git a/tests/projects/other/glsl2spv/xmake.lua b/tests/projects/other/glsl2spv/xmake.lua deleted file mode 100644 index 111927e56..000000000 --- a/tests/projects/other/glsl2spv/xmake.lua +++ /dev/null @@ -1,11 +0,0 @@ -add_rules("mode.debug", "mode.release") - -add_requires("glslang", {configs = {binaryonly = true}}) - -target("test") - set_kind("binary") - add_rules("utils.glsl2spv", {bin2c = true}) - add_files("src/*.c") - add_files("src/*.vert", "src/*.frag") - add_packages("glslang") - diff --git a/tests/projects/other/glsl2spv_bin2c/src/main.c b/tests/projects/other/glsl2spv_bin2c/src/main.c new file mode 100644 index 000000000..3dfe7b351 --- /dev/null +++ b/tests/projects/other/glsl2spv_bin2c/src/main.c @@ -0,0 +1,99 @@ +#include +#include +#include + +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" +}; + +static void hexdump(const char* name, const uint8_t* data, uint32_t size) { + printf("%s: size: %u bytes\n", name, (unsigned int)size); + if (size == 0) { + return; + } + + // if file is larger than 128 bytes, only dump first 64 and last 64 bytes + uint32_t dump_size = size; + uint32_t start_offset = 0; + uint32_t end_offset = 0; + uint32_t skip_size = 0; + + if (size > 128) { + dump_size = 64; + start_offset = 0; + end_offset = size - 64; + skip_size = end_offset - start_offset - dump_size; + } + + // dump start + for (uint32_t offset = start_offset; offset < start_offset + dump_size; offset += 16) { + // print offset + printf("%08x ", (unsigned int)offset); + + // print hex bytes (8 bytes, space, 8 bytes) + for (uint32_t i = 0; i < 16; i++) { + if (offset + i < size) { + printf("%02x ", data[offset + i]); + } else { + printf(" "); + } + if (i == 7) { + printf(" "); + } + } + + // print ASCII representation + printf(" |"); + for (uint32_t i = 0; i < 16 && offset + i < size; i++) { + uint8_t c = data[offset + i]; + printf("%c", isprint(c) ? c : '.'); + } + printf("|\n"); + } + + // print skip indicator if needed + if (skip_size > 0) { + printf(" ... (skipped %u bytes) ...\n", (unsigned int)skip_size); + } + + // dump end if needed + if (size > 128) { + for (uint32_t offset = end_offset; offset < size; offset += 16) { + // print offset + printf("%08x ", (unsigned int)offset); + + // print hex bytes (8 bytes, space, 8 bytes) + for (uint32_t i = 0; i < 16; i++) { + if (offset + i < size) { + printf("%02x ", data[offset + i]); + } else { + printf(" "); + } + if (i == 7) { + printf(" "); + } + } + + // print ASCII representation + printf(" |"); + for (uint32_t i = 0; i < 16 && offset + i < size; i++) { + uint8_t c = data[offset + i]; + printf("%c", isprint(c) ? c : '.'); + } + printf("|\n"); + } + } +} + +int main(int argc, char** argv) +{ + hexdump("test.vert.spv", g_test_vert_spv_data, (uint32_t)sizeof(g_test_vert_spv_data)); + printf("\n"); + hexdump("test.frag.spv", g_test_frag_spv_data, (uint32_t)sizeof(g_test_frag_spv_data)); + return 0; +} + diff --git a/tests/projects/other/glsl2spv_bin2c/src/test.frag b/tests/projects/other/glsl2spv_bin2c/src/test.frag new file mode 100644 index 000000000..9aeec4257 --- /dev/null +++ b/tests/projects/other/glsl2spv_bin2c/src/test.frag @@ -0,0 +1,6 @@ +#version 330 +precision mediump float; + +void main() { +} + diff --git a/tests/projects/other/glsl2spv_bin2c/src/test.vert b/tests/projects/other/glsl2spv_bin2c/src/test.vert new file mode 100644 index 000000000..9aeec4257 --- /dev/null +++ b/tests/projects/other/glsl2spv_bin2c/src/test.vert @@ -0,0 +1,6 @@ +#version 330 +precision mediump float; + +void main() { +} + diff --git a/tests/projects/other/glsl2spv_bin2c/xmake.lua b/tests/projects/other/glsl2spv_bin2c/xmake.lua new file mode 100644 index 000000000..111927e56 --- /dev/null +++ b/tests/projects/other/glsl2spv_bin2c/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.glsl2spv", {bin2c = true}) + add_files("src/*.c") + add_files("src/*.vert", "src/*.frag") + add_packages("glslang") + diff --git a/tests/projects/other/hlsl2spv/.gitignore b/tests/projects/other/hlsl2spv/.gitignore deleted file mode 100644 index 152105761..000000000 --- a/tests/projects/other/hlsl2spv/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# 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 deleted file mode 100644 index 30c475abe..000000000 --- a/tests/projects/other/hlsl2spv/src/main.c +++ /dev/null @@ -1,15 +0,0 @@ -#include - -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 deleted file mode 100644 index 95c63d53c..000000000 --- a/tests/projects/other/hlsl2spv/src/test.ps.hlsl +++ /dev/null @@ -1,10 +0,0 @@ -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 deleted file mode 100644 index 97fab0408..000000000 --- a/tests/projects/other/hlsl2spv/src/test.vs.hlsl +++ /dev/null @@ -1,19 +0,0 @@ -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 deleted file mode 100644 index 0487fba5d..000000000 --- a/tests/projects/other/hlsl2spv/xmake.lua +++ /dev/null @@ -1,11 +0,0 @@ -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/tests/projects/other/hlsl2spv_bin2c/src/main.c b/tests/projects/other/hlsl2spv_bin2c/src/main.c new file mode 100644 index 000000000..0a05e7506 --- /dev/null +++ b/tests/projects/other/hlsl2spv_bin2c/src/main.c @@ -0,0 +1,99 @@ +#include +#include +#include + +static unsigned char g_test_vs_spv_data[] = { + #include "test.vs.spv.h" +}; + +static unsigned char g_test_ps_spv_data[] = { + #include "test.ps.spv.h" +}; + +static void hexdump(const char* name, const uint8_t* data, uint32_t size) { + printf("%s: size: %u bytes\n", name, (unsigned int)size); + if (size == 0) { + return; + } + + // if file is larger than 128 bytes, only dump first 64 and last 64 bytes + uint32_t dump_size = size; + uint32_t start_offset = 0; + uint32_t end_offset = 0; + uint32_t skip_size = 0; + + if (size > 128) { + dump_size = 64; + start_offset = 0; + end_offset = size - 64; + skip_size = end_offset - start_offset - dump_size; + } + + // dump start + for (uint32_t offset = start_offset; offset < start_offset + dump_size; offset += 16) { + // print offset + printf("%08x ", (unsigned int)offset); + + // print hex bytes (8 bytes, space, 8 bytes) + for (uint32_t i = 0; i < 16; i++) { + if (offset + i < size) { + printf("%02x ", data[offset + i]); + } else { + printf(" "); + } + if (i == 7) { + printf(" "); + } + } + + // print ASCII representation + printf(" |"); + for (uint32_t i = 0; i < 16 && offset + i < size; i++) { + uint8_t c = data[offset + i]; + printf("%c", isprint(c) ? c : '.'); + } + printf("|\n"); + } + + // print skip indicator if needed + if (skip_size > 0) { + printf(" ... (skipped %u bytes) ...\n", (unsigned int)skip_size); + } + + // dump end if needed + if (size > 128) { + for (uint32_t offset = end_offset; offset < size; offset += 16) { + // print offset + printf("%08x ", (unsigned int)offset); + + // print hex bytes (8 bytes, space, 8 bytes) + for (uint32_t i = 0; i < 16; i++) { + if (offset + i < size) { + printf("%02x ", data[offset + i]); + } else { + printf(" "); + } + if (i == 7) { + printf(" "); + } + } + + // print ASCII representation + printf(" |"); + for (uint32_t i = 0; i < 16 && offset + i < size; i++) { + uint8_t c = data[offset + i]; + printf("%c", isprint(c) ? c : '.'); + } + printf("|\n"); + } + } +} + +int main(int argc, char** argv) +{ + hexdump("test.vs.spv", g_test_vs_spv_data, (uint32_t)sizeof(g_test_vs_spv_data)); + printf("\n"); + hexdump("test.ps.spv", g_test_ps_spv_data, (uint32_t)sizeof(g_test_ps_spv_data)); + return 0; +} + diff --git a/tests/projects/other/hlsl2spv_bin2c/src/test.ps.hlsl b/tests/projects/other/hlsl2spv_bin2c/src/test.ps.hlsl new file mode 100644 index 000000000..5c534d30b --- /dev/null +++ b/tests/projects/other/hlsl2spv_bin2c/src/test.ps.hlsl @@ -0,0 +1,9 @@ +struct PSInput { + float4 position : SV_POSITION; + float2 texcoord : TEXCOORD0; +}; + +float4 main(PSInput input) : SV_TARGET { + return float4(input.texcoord, 0.0, 1.0); +} + diff --git a/tests/projects/other/hlsl2spv_bin2c/src/test.vs.hlsl b/tests/projects/other/hlsl2spv_bin2c/src/test.vs.hlsl new file mode 100644 index 000000000..e86b3b263 --- /dev/null +++ b/tests/projects/other/hlsl2spv_bin2c/src/test.vs.hlsl @@ -0,0 +1,17 @@ +struct VSInput { + float3 position : POSITION; + float2 texcoord : TEXCOORD0; +}; + +struct VSOutput { + float4 position : SV_POSITION; + float2 texcoord : TEXCOORD0; +}; + +VSOutput main(VSInput input) { + VSOutput output; + output.position = float4(input.position, 1.0); + output.texcoord = input.texcoord; + return output; +} + diff --git a/tests/projects/other/hlsl2spv_bin2c/xmake.lua b/tests/projects/other/hlsl2spv_bin2c/xmake.lua new file mode 100644 index 000000000..944747cba --- /dev/null +++ b/tests/projects/other/hlsl2spv_bin2c/xmake.lua @@ -0,0 +1,11 @@ +add_rules("mode.debug", "mode.release") + +add_requires("directxshadercompiler", {configs = {binaryonly = true}}) + +target("test") + set_kind("binary") + add_rules("utils.hlsl2spv", {bin2c = true}) + add_files("src/*.c") + add_files("src/*.vs.hlsl", "src/*.ps.hlsl") + add_packages("directxshadercompiler") + diff --git a/xmake/rules/utils/bin2c/utils.lua b/xmake/rules/utils/bin2c/utils.lua new file mode 100644 index 000000000..cf2f074f2 --- /dev/null +++ b/xmake/rules/utils/bin2c/utils.lua @@ -0,0 +1,98 @@ +--!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, Xmake Open Source Community. +-- +-- @author ruki +-- @file utils.lua +-- + +-- generate header file from binary file +-- +-- @param target the target +-- @param batchcmds the batch commands +-- @param binaryfile the binary file path +-- @param opt the options +-- - progress: the progress callback +-- - linewidth: the line width for output (optional) +-- - nozeroend: disable null terminator (deprecated, for backward compatibility only) +-- - zeroend: enable null terminator (default: true, bin2c enables zeroend by default for compatibility) +-- - rulename: the rule name for getting extra config (default: utils.bin2c) +-- - headerfile: the output header file path (optional, auto-generated if not provided) +-- - headerdir: the header directory (optional, auto-generated if not provided) +-- +function generate_headerfile(target, batchcmds, binaryfile, opt) + opt = opt or {} + local rulename = opt.rulename or "utils.bin2c" + local progress = opt.progress + + -- get header directory and file + local headerdir = opt.headerdir + local headerfile = opt.headerfile + if not headerfile then + if not headerdir then + headerdir = path.join(target:autogendir(), "rules", "utils", "bin2c") + end + headerfile = path.join(headerdir, path.filename(binaryfile) .. ".h") + else + headerdir = headerdir or path.directory(headerfile) + end + + -- add includedirs + target:add("includedirs", headerdir) + + -- add commands + if progress then + batchcmds:show_progress(progress, "${color.build.object}generating.bin2c %s", binaryfile) + end + batchcmds:mkdir(headerdir) + + -- build argv + local argv = {"-i", path(binaryfile), "-o", path(headerfile)} + + -- get linewidth + local linewidth = opt.linewidth or target:extraconf("rules", rulename, "linewidth") + if linewidth then + table.insert(argv, "-w") + table.insert(argv, tostring(linewidth)) + end + + -- get nozeroend/zeroend (check file-level config first, then rule-level config, then opt) + local fileconfig = target:fileconfig(binaryfile) + local nozeroend = nil + if fileconfig then + if fileconfig.nozeroend ~= nil then + nozeroend = fileconfig.nozeroend + elseif fileconfig.zeroend ~= nil then + nozeroend = not fileconfig.zeroend + end + end + if nozeroend == nil then + nozeroend = target:extraconf("rules", rulename, "nozeroend") or false + end + -- also support zeroend in opt (inverse of nozeroend) + if opt.zeroend ~= nil then + nozeroend = not opt.zeroend + elseif opt.nozeroend ~= nil then + nozeroend = opt.nozeroend + end + if nozeroend then + table.insert(argv, "--nozeroend") + end + + batchcmds:vlua("utils.binary.bin2c", argv) + + return headerfile +end + diff --git a/xmake/rules/utils/bin2c/xmake.lua b/xmake/rules/utils/bin2c/xmake.lua index 7be711af1..9f6338758 100644 --- a/xmake/rules/utils/bin2c/xmake.lua +++ b/xmake/rules/utils/bin2c/xmake.lua @@ -29,32 +29,12 @@ rule("utils.bin2c") target:add("includedirs", headerdir) end) on_preparecmd_file(function (target, batchcmds, sourcefile_bin, opt) + import("rules.utils.bin2c.utils", {alias = "bin2c_utils", rootdir = os.programdir()}) - -- get header file - local headerdir = path.join(target:autogendir(), "rules", "utils", "bin2c") - local headerfile = path.join(headerdir, path.filename(sourcefile_bin) .. ".h") - target:add("includedirs", headerdir) - - -- add commands - batchcmds:show_progress(opt.progress, "${color.build.object}generating.bin2c %s", sourcefile_bin) - batchcmds:mkdir(headerdir) - local argv = {"-i", path(sourcefile_bin), "-o", path(headerfile)} - local linewidth = target:extraconf("rules", "utils.bin2c", "linewidth") - if linewidth then - table.insert(argv, "-w") - table.insert(argv, tostring(linewidth)) - end - -- get nozeroend (check file-level config first, then rule-level config) - local fileconfig = target:fileconfig(sourcefile_bin) - local nozeroend = (fileconfig and fileconfig.nozeroend) or target:extraconf("rules", "utils.bin2c", "nozeroend") or false - -- also support zeroend (inverse of nozeroend) - if fileconfig and fileconfig.zeroend ~= nil then - nozeroend = not fileconfig.zeroend - end - if nozeroend then - table.insert(argv, "--nozeroend") - end - batchcmds:vlua("utils.binary.bin2c", argv) + -- generate header file + local headerfile = bin2c_utils.generate_headerfile(target, batchcmds, sourcefile_bin, { + progress = opt.progress + }) -- add deps batchcmds:add_depfiles(sourcefile_bin) diff --git a/xmake/rules/utils/glsl2spv/xmake.lua b/xmake/rules/utils/glsl2spv/xmake.lua index 7fc456c80..a534b43ad 100644 --- a/xmake/rules/utils/glsl2spv/xmake.lua +++ b/xmake/rules/utils/glsl2spv/xmake.lua @@ -56,6 +56,7 @@ rule("utils.glsl2spv") before_buildcmd_file(function (target, batchcmds, sourcefile_glsl, opt) import("lib.detect.find_tool") import("rules.utils.bin2obj.utils", {alias = "bin2obj_utils", rootdir = os.programdir()}) + import("rules.utils.bin2c.utils", {alias = "bin2c_utils", rootdir = os.programdir()}) -- get glslangValidator local glslc @@ -88,15 +89,13 @@ rule("utils.glsl2spv") local is_bin2c = target:extraconf("rules", "utils.glsl2spv", "bin2c") local is_bin2obj = target:extraconf("rules", "utils.glsl2spv", "bin2obj") if is_bin2c then - -- get header file - local headerdir = outputdir - local headerfile = path.join(headerdir, path.filename(spvfilepath) .. ".h") - target:add("includedirs", headerdir) + -- generate header file + local headerfile = bin2c_utils.generate_headerfile(target, batchcmds, spvfilepath, { + progress = opt.progress, + headerdir = outputdir, + zeroend = true -- default enable zeroend for shader data + }) outputfile = headerfile - - -- add commands - local argv = {"--nozeroend", "-i", path(spvfilepath), "-o", path(headerfile)} - batchcmds:vlua("utils.binary.bin2c", argv) elseif is_bin2obj then -- convert to object file using bin2obj local objectfile = bin2obj_utils.generate_objectfile(target, batchcmds, spvfilepath, { diff --git a/xmake/rules/utils/hlsl2spv/xmake.lua b/xmake/rules/utils/hlsl2spv/xmake.lua index de84aa3ab..a1326b97f 100644 --- a/xmake/rules/utils/hlsl2spv/xmake.lua +++ b/xmake/rules/utils/hlsl2spv/xmake.lua @@ -57,6 +57,7 @@ rule("utils.hlsl2spv") before_buildcmd_file(function (target, batchcmds, sourcefile_hlsl, opt) import("lib.detect.find_tool") import("rules.utils.bin2obj.utils", {alias = "bin2obj_utils", rootdir = os.programdir()}) + import("rules.utils.bin2c.utils", {alias = "bin2c_utils", rootdir = os.programdir()}) local dxc = assert(find_tool("dxc"), "dxc not found!") @@ -87,16 +88,13 @@ rule("utils.hlsl2spv") local is_bin2c = target:extraconf("rules", "utils.hlsl2spv", "bin2c") local is_bin2obj = target:extraconf("rules", "utils.hlsl2spv", "bin2obj") if is_bin2c then - -- get header file - local headerdir = outputdir - local headerfile = path.join(headerdir, path.filename(spvfilepath) .. ".h") - - target:add("includedirs", headerdir) + -- generate header file + local headerfile = bin2c_utils.generate_headerfile(target, batchcmds, spvfilepath, { + progress = opt.progress, + headerdir = outputdir, + zeroend = true -- default enable zeroend for shader data + }) outputfile = headerfile - - -- add commands - local argv = {"--nozeroend", "-i", path(spvfilepath), "-o", path(headerfile)} - batchcmds:vlua("utils.binary.bin2c", argv) elseif is_bin2obj then -- convert to object file using bin2obj local objectfile = bin2obj_utils.generate_objectfile(target, batchcmds, spvfilepath, { -- cgit v1.3.1 From d51cb68331dae5c27f7af8515ec83e9764a832a7 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 23:22:31 +0800 Subject: improve tests --- tests/projects/other/glsl2spv_bin2obj/src/main.c | 95 ++++++++++++++++++++---- tests/projects/other/hlsl2spv_bin2obj/src/main.c | 95 ++++++++++++++++++++---- 2 files changed, 158 insertions(+), 32 deletions(-) diff --git a/tests/projects/other/glsl2spv_bin2obj/src/main.c b/tests/projects/other/glsl2spv_bin2obj/src/main.c index 64d62ce8b..6e5449fac 100644 --- a/tests/projects/other/glsl2spv_bin2obj/src/main.c +++ b/tests/projects/other/glsl2spv_bin2obj/src/main.c @@ -1,5 +1,6 @@ #include #include +#include extern const uint8_t _binary_test_vert_spv_start[]; extern const uint8_t _binary_test_vert_spv_end[]; @@ -7,30 +8,92 @@ extern const uint8_t _binary_test_vert_spv_end[]; extern const uint8_t _binary_test_frag_spv_start[]; extern const uint8_t _binary_test_frag_spv_end[]; -int main(int argc, char** argv) { - const uint32_t vert_size = (uint32_t)(_binary_test_vert_spv_end - _binary_test_vert_spv_start); - const uint32_t frag_size = (uint32_t)(_binary_test_frag_spv_end - _binary_test_frag_spv_start); +static void hexdump(const char* name, const uint8_t* data, uint32_t size) { + printf("%s: size: %u bytes\n", name, (unsigned int)size); + if (size == 0) { + return; + } - printf("test.vert.spv: size: %u bytes\n", vert_size); - printf("test.frag.spv: size: %u bytes\n", frag_size); + // if file is larger than 128 bytes, only dump first 64 and last 64 bytes + uint32_t dump_size = size; + uint32_t start_offset = 0; + uint32_t end_offset = 0; + uint32_t skip_size = 0; - // Print first few bytes - if (vert_size > 0) { - printf("test.vert.spv first 4 bytes: "); - for (uint32_t i = 0; i < 4 && i < vert_size; i++) { - printf("%02x ", _binary_test_vert_spv_start[i]); + if (size > 128) { + dump_size = 64; + start_offset = 0; + end_offset = size - 64; + skip_size = end_offset - start_offset - dump_size; + } + + // dump start + for (uint32_t offset = start_offset; offset < start_offset + dump_size; offset += 16) { + // print offset + printf("%08x ", (unsigned int)offset); + + // print hex bytes (8 bytes, space, 8 bytes) + for (uint32_t i = 0; i < 16; i++) { + if (offset + i < size) { + printf("%02x ", data[offset + i]); + } else { + printf(" "); + } + if (i == 7) { + printf(" "); + } + } + + // print ASCII representation + printf(" |"); + for (uint32_t i = 0; i < 16 && offset + i < size; i++) { + uint8_t c = data[offset + i]; + printf("%c", isprint(c) ? c : '.'); } - printf("\n"); + printf("|\n"); } - if (frag_size > 0) { - printf("test.frag.spv first 4 bytes: "); - for (uint32_t i = 0; i < 4 && i < frag_size; i++) { - printf("%02x ", _binary_test_frag_spv_start[i]); + // print skip indicator if needed + if (skip_size > 0) { + printf(" ... (skipped %u bytes) ...\n", (unsigned int)skip_size); + } + + // dump end if needed + if (size > 128) { + for (uint32_t offset = end_offset; offset < size; offset += 16) { + // print offset + printf("%08x ", (unsigned int)offset); + + // print hex bytes (8 bytes, space, 8 bytes) + for (uint32_t i = 0; i < 16; i++) { + if (offset + i < size) { + printf("%02x ", data[offset + i]); + } else { + printf(" "); + } + if (i == 7) { + printf(" "); + } + } + + // print ASCII representation + printf(" |"); + for (uint32_t i = 0; i < 16 && offset + i < size; i++) { + uint8_t c = data[offset + i]; + printf("%c", isprint(c) ? c : '.'); + } + printf("|\n"); } - printf("\n"); } +} + +int main(int argc, char** argv) { + const uint32_t vert_size = (uint32_t)(_binary_test_vert_spv_end - _binary_test_vert_spv_start); + const uint32_t frag_size = (uint32_t)(_binary_test_frag_spv_end - _binary_test_frag_spv_start); + hexdump("test.vert.spv", _binary_test_vert_spv_start, vert_size); + printf("\n"); + hexdump("test.frag.spv", _binary_test_frag_spv_start, frag_size); return 0; } diff --git a/tests/projects/other/hlsl2spv_bin2obj/src/main.c b/tests/projects/other/hlsl2spv_bin2obj/src/main.c index 343e5ac2f..4d163f576 100644 --- a/tests/projects/other/hlsl2spv_bin2obj/src/main.c +++ b/tests/projects/other/hlsl2spv_bin2obj/src/main.c @@ -1,5 +1,6 @@ #include #include +#include extern const uint8_t _binary_test_vs_spv_start[]; extern const uint8_t _binary_test_vs_spv_end[]; @@ -7,30 +8,92 @@ extern const uint8_t _binary_test_vs_spv_end[]; extern const uint8_t _binary_test_ps_spv_start[]; extern const uint8_t _binary_test_ps_spv_end[]; -int main(int argc, char** argv) { - const uint32_t vs_size = (uint32_t)(_binary_test_vs_spv_end - _binary_test_vs_spv_start); - const uint32_t ps_size = (uint32_t)(_binary_test_ps_spv_end - _binary_test_ps_spv_start); +static void hexdump(const char* name, const uint8_t* data, uint32_t size) { + printf("%s: size: %u bytes\n", name, (unsigned int)size); + if (size == 0) { + return; + } - printf("test.vs.spv: size: %u bytes\n", vs_size); - printf("test.ps.spv: size: %u bytes\n", ps_size); + // if file is larger than 128 bytes, only dump first 64 and last 64 bytes + uint32_t dump_size = size; + uint32_t start_offset = 0; + uint32_t end_offset = 0; + uint32_t skip_size = 0; - // Print first few bytes - if (vs_size > 0) { - printf("test.vs.spv first 4 bytes: "); - for (uint32_t i = 0; i < 4 && i < vs_size; i++) { - printf("%02x ", _binary_test_vs_spv_start[i]); + if (size > 128) { + dump_size = 64; + start_offset = 0; + end_offset = size - 64; + skip_size = end_offset - start_offset - dump_size; + } + + // dump start + for (uint32_t offset = start_offset; offset < start_offset + dump_size; offset += 16) { + // print offset + printf("%08x ", (unsigned int)offset); + + // print hex bytes (8 bytes, space, 8 bytes) + for (uint32_t i = 0; i < 16; i++) { + if (offset + i < size) { + printf("%02x ", data[offset + i]); + } else { + printf(" "); + } + if (i == 7) { + printf(" "); + } + } + + // print ASCII representation + printf(" |"); + for (uint32_t i = 0; i < 16 && offset + i < size; i++) { + uint8_t c = data[offset + i]; + printf("%c", isprint(c) ? c : '.'); } - printf("\n"); + printf("|\n"); } - if (ps_size > 0) { - printf("test.ps.spv first 4 bytes: "); - for (uint32_t i = 0; i < 4 && i < ps_size; i++) { - printf("%02x ", _binary_test_ps_spv_start[i]); + // print skip indicator if needed + if (skip_size > 0) { + printf(" ... (skipped %u bytes) ...\n", (unsigned int)skip_size); + } + + // dump end if needed + if (size > 128) { + for (uint32_t offset = end_offset; offset < size; offset += 16) { + // print offset + printf("%08x ", (unsigned int)offset); + + // print hex bytes (8 bytes, space, 8 bytes) + for (uint32_t i = 0; i < 16; i++) { + if (offset + i < size) { + printf("%02x ", data[offset + i]); + } else { + printf(" "); + } + if (i == 7) { + printf(" "); + } + } + + // print ASCII representation + printf(" |"); + for (uint32_t i = 0; i < 16 && offset + i < size; i++) { + uint8_t c = data[offset + i]; + printf("%c", isprint(c) ? c : '.'); + } + printf("|\n"); } - printf("\n"); } +} + +int main(int argc, char** argv) { + const uint32_t vs_size = (uint32_t)(_binary_test_vs_spv_end - _binary_test_vs_spv_start); + const uint32_t ps_size = (uint32_t)(_binary_test_ps_spv_end - _binary_test_ps_spv_start); + hexdump("test.vs.spv", _binary_test_vs_spv_start, vs_size); + printf("\n"); + hexdump("test.ps.spv", _binary_test_ps_spv_start, ps_size); return 0; } -- cgit v1.3.1 From c4f48bbfda999f7b6dfb575059f4c5b676643294 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 7 Dec 2025 23:31:06 +0800 Subject: fix zeroend --- tests/projects/other/hlsl2spv_bin2obj/xmake.lua | 2 +- xmake/rules/utils/glsl2spv/xmake.lua | 6 ++++-- xmake/rules/utils/hlsl2spv/xmake.lua | 6 ++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/projects/other/hlsl2spv_bin2obj/xmake.lua b/tests/projects/other/hlsl2spv_bin2obj/xmake.lua index 2184d24cc..4d09e5036 100644 --- a/tests/projects/other/hlsl2spv_bin2obj/xmake.lua +++ b/tests/projects/other/hlsl2spv_bin2obj/xmake.lua @@ -6,6 +6,6 @@ target("test") set_kind("binary") add_rules("utils.hlsl2spv", {bin2obj = true}) add_files("src/*.c") - add_files("src/*.hlsl", "src/*.hlsl") + add_files("src/*.hlsl") add_packages("directxshadercompiler") diff --git a/xmake/rules/utils/glsl2spv/xmake.lua b/xmake/rules/utils/glsl2spv/xmake.lua index a534b43ad..01abc561a 100644 --- a/xmake/rules/utils/glsl2spv/xmake.lua +++ b/xmake/rules/utils/glsl2spv/xmake.lua @@ -90,17 +90,19 @@ rule("utils.glsl2spv") local is_bin2obj = target:extraconf("rules", "utils.glsl2spv", "bin2obj") if is_bin2c then -- generate header file + -- note: explicitly disable zeroend (SPIR-V is binary format, not null-terminated string) local headerfile = bin2c_utils.generate_headerfile(target, batchcmds, spvfilepath, { progress = opt.progress, headerdir = outputdir, - zeroend = true -- default enable zeroend for shader data + zeroend = false -- SPIR-V is binary format, not null-terminated string }) outputfile = headerfile elseif is_bin2obj then -- convert to object file using bin2obj + -- note: zeroend is false by default (SPIR-V is binary format, not null-terminated string) local objectfile = bin2obj_utils.generate_objectfile(target, batchcmds, spvfilepath, { progress = opt.progress, - zeroend = true -- default enable zeroend for shader data + rulename = "utils.glsl2spv" -- pass current rule name for config lookup }) outputfile = objectfile end diff --git a/xmake/rules/utils/hlsl2spv/xmake.lua b/xmake/rules/utils/hlsl2spv/xmake.lua index a1326b97f..750574eac 100644 --- a/xmake/rules/utils/hlsl2spv/xmake.lua +++ b/xmake/rules/utils/hlsl2spv/xmake.lua @@ -89,17 +89,19 @@ rule("utils.hlsl2spv") local is_bin2obj = target:extraconf("rules", "utils.hlsl2spv", "bin2obj") if is_bin2c then -- generate header file + -- note: explicitly disable zeroend (SPIR-V is binary format, not null-terminated string) local headerfile = bin2c_utils.generate_headerfile(target, batchcmds, spvfilepath, { progress = opt.progress, headerdir = outputdir, - zeroend = true -- default enable zeroend for shader data + zeroend = false -- SPIR-V is binary format, not null-terminated string }) outputfile = headerfile elseif is_bin2obj then -- convert to object file using bin2obj + -- note: zeroend is false by default (SPIR-V is binary format, not null-terminated string) local objectfile = bin2obj_utils.generate_objectfile(target, batchcmds, spvfilepath, { progress = opt.progress, - zeroend = true -- default enable zeroend for shader data + rulename = "utils.hlsl2spv" -- pass current rule name for config lookup }) outputfile = objectfile end -- cgit v1.3.1