From 2dac5788ec5bbb94014327d20a0c4f96646032e3 Mon Sep 17 00:00:00 2001 From: Floogle <18466542+skyfloogle@users.noreply.github.com> Date: Thu, 15 Aug 2024 19:53:49 +0200 Subject: [PATCH] set up shaders at build time --- .github/workflows/zig.yml | 2 +- build.zig | 29 ++++++++++++- gm82gltf.gej | 18 ++++++++ gm82gltf.gml | 91 ++------------------------------------- pixel.hlsl | 21 +++++++++ vertex.hlsl | 38 ++++++++++++++++ 6 files changed, 110 insertions(+), 89 deletions(-) create mode 100644 pixel.hlsl create mode 100644 vertex.hlsl diff --git a/.github/workflows/zig.yml b/.github/workflows/zig.yml index 8f24a70..2e28f14 100644 --- a/.github/workflows/zig.yml +++ b/.github/workflows/zig.yml @@ -18,7 +18,7 @@ jobs: # This workflow contains a single job called "build" build: # The type of runner that the job will run on - runs-on: ubuntu-latest + runs-on: windows-latest # Steps represent a sequence of tasks that will be executed as part of the job steps: diff --git a/build.zig b/build.zig index 0992fed..f6f586d 100644 --- a/build.zig +++ b/build.zig @@ -1,10 +1,21 @@ const std = @import("std"); const builtin = @import("builtin"); +fn fxc(b: *std.Build, fxc_file: *std.Build.Step.InstallFile, target: []const u8, output: []const u8, input: []const u8) !*std.Build.Step.Run { + const fxc_path = b.getInstallPath(fxc_file.dir, fxc_file.dest_rel_path); + const run = b.addSystemCommand(if (builtin.os.tag != .windows) &.{ "wine", fxc_path } else &.{fxc_path}); + run.addArgs(&.{ "/nologo", target, "/Fo" }); + var allocator = std.heap.GeneralPurposeAllocator(.{}){}; + _ = run.addArg(try std.fs.path.relative(allocator.allocator(), ".", b.getInstallPath(.prefix, output))); + run.addFileInput(.{ .src_path = .{ .owner = b, .sub_path = input } }); + run.addArg(input); + return run; +} + // Although this function looks imperative, note that its job is to // declaratively construct a build graph that will be executed by an external // runner. -pub fn build(b: *std.Build) void { +pub fn build(b: *std.Build) !void { // Standard target options allows the person running `zig build` to choose // what target to build for. Here we do not override the defaults, which // means any target is allowed, and the default is native. Other options @@ -32,6 +43,15 @@ pub fn build(b: *std.Build) void { const gm82gex_output = gm82gex_download.captureStdOut(); const gm82gex_file = b.addInstallFileWithDir(gm82gex_output, .prefix, "gm82gex.py"); + const fxc_download = b.addSystemCommand(&.{ "curl", "https://raw.githubusercontent.com/gwihlidal/docker-fxc/master/fxc.exe" }); + const fxc_output = fxc_download.captureStdOut(); + const fxc_file = b.addInstallFileWithDir(fxc_output, .prefix, "fxc.exe"); + + const d3dcompiler_download = b.addSystemCommand(&.{ "curl", "https://raw.githubusercontent.com/gwihlidal/docker-fxc/master/d3dcompiler_47.dll" }); + const d3dcompiler_output = d3dcompiler_download.captureStdOut(); + const d3dcompiler_file = b.addInstallFileWithDir(d3dcompiler_output, .prefix, "d3dcompiler_47.dll"); + fxc_file.step.dependOn(&d3dcompiler_file.step); + const gm82gex_run = b.addSystemCommand(&.{if (builtin.os.tag == .windows) "py" else "python3"}); gm82gex_run.addArg(b.getInstallPath(gm82gex_file.dir, gm82gex_file.dest_rel_path)); gm82gex_run.addFileArg(.{ .src_path = .{ .owner = b, .sub_path = "gm82gltf.gej" } }); @@ -39,6 +59,13 @@ pub fn build(b: *std.Build) void { gm82gex_run.step.dependOn(&artifact_step.step); b.getInstallStep().dependOn(&gm82gex_run.step); + const vshader_compile_step = try fxc(b, fxc_file, "/Tvs_2_a", "vertex.vs2", "vertex.hlsl"); + vshader_compile_step.step.dependOn(&fxc_file.step); + gm82gex_run.step.dependOn(&vshader_compile_step.step); + const pshader_compile_step = try fxc(b, fxc_file, "/Tps_2_b", "pixel.ps2", "pixel.hlsl"); + pshader_compile_step.step.dependOn(&fxc_file.step); + gm82gex_run.step.dependOn(&pshader_compile_step.step); + // Creates a step for unit testing. This only builds the test executable // but does not run it. const lib_unit_tests = b.addTest(.{ diff --git a/gm82gltf.gej b/gm82gltf.gej index 2e342e3..21721fa 100644 --- a/gm82gltf.gej +++ b/gm82gltf.gej @@ -928,6 +928,24 @@ } ], "constants": [] + }, + { + "filename": "gltf_pixel.ps2", + "origname": "zig-out/pixel.ps2", + "kind": 4, + "init": "", + "final": "", + "functions": [], + "constants": [] + }, + { + "filename": "gltf_vertex.vs2", + "origname": "zig-out/vertex.vs2", + "kind": 4, + "init": "", + "final": "", + "functions": [], + "constants": [] } ] } \ No newline at end of file diff --git a/gm82gltf.gml b/gm82gltf.gml index da244e6..20b934f 100644 --- a/gm82gltf.gml +++ b/gm82gltf.gml @@ -14,94 +14,11 @@ // stuff on attributes in primitives globalvar __gm82gltf_primitivebuffers; - globalvar __gm82gltf_shader_vertex_default; __gm82gltf_shader_vertex_default=shader_vertex_create_base64(" - eJyFVU1rFEEQff0xsyOi8wcE5xgVFhLxEPCgBkEkATWOeguriWQxZmUTQo6t5gcM - mB+wB0+TSw7evPgbPHmMML8jLVVdvel1D9kc0l393uuq11W7Sp/7c38fKy8fProB - 4CcApc+9AUB7KOAHgDcANAAHYJviAA455mABHEnsWP4rAN9lPX46Gu7urw32x8ND - nJycGBhYWFTxPBy9fvUM0zMVz9Y/DHcf7w7e7mxtEpeUw1/4HOxtLG0MsDZ8Nx7t - jd7vVwsvblVPVtdXq/XtwebWuFoZffw03NkaV8v9peX+8r2l/t3FxUU8B7IjlBPA - PUDyuTkts2xonQFOyVoDTstaAc6gbBYooaI7ywADlE6ja46ASZ91ylPgj8sAq1A4 - AzSm8hMApyGmOIaZmOaYSmKaua1wa8FZxukEZxhXC66VGN1Ryx0xRnfUckeMkV4t - eu2pBoxC6RQ6Z9BxDZq1WtEKeWjWakUrxkirFa16qqXRsR5hwLl6ydUzD6zvRT/G - SN+LfoyRvhd9z/rkPUT/Cu8V74GuCX71xOu/7Jfi91ROoXW0Jp8UasnturxBk2C1 - A7xgaV3zmt4LaGd4eoZnE55NeHaOZ2Z4RcIrEl4x5VmuqZaaWuZlUM5KTTmv05ro - zZsEe1FTLjURtie6uuqm2BzW9QA+L+TczJwXruD5gDEouWdyfg+6h7ghp4xxLeO0 - 9EQmOCtehjVxQs2WObVLe9JKHwUPqCd/c69d7kErHkTsxZ3zHlAPdwk25HThAc1k - ej6fZ6wt+neZVzZ5k+BB9GPet+hBqIvmhurynIvl/g/+kR+kSdic86Z56hKsdrnk - Rn7k0l85rsmcHSdYy9jg0VWZ4e3kvOA609ooz+hfLrWRV+Tl/30QZpdmPswuYXuc - f/Chh5y9Sj3uTXmW98QL+4J1aX87/CbRd7IDvjV3ANB3NmmQ9md0E/piob0Dv6n0 - WYh94RjlHmJA+esrx8i3UB9gzhS6xnvgHw5sX8A= - ") + globalvar __gm82gltf_shader_vertex_default; + __gm82gltf_shader_vertex_default=shader_vertex_create_file(temp_directory+"\gm82\gltf_vertex.vs2") - /* - float4x4 rMatrixW, rMatrixWVP; - float4x4 rJointMatrix[32]; - bool rSkinEnabled; - - struct VS_INPUT { - float3 normal: NORMAL; - float3 tangent: TANGENT0; - float4 position: POSITION0; - float2 texcoord: TEXCOORD0; - int4 joints: BLENDINDICES0; - float4 weights: BLENDWEIGHT0; - }; - - struct VS_OUTPUT { - float4 position: POSITION0; - float2 texcoord: TEXCOORD0; - }; - - VS_OUTPUT main(VS_INPUT input) { - VS_OUTPUT output; - if (rSkinEnabled) { - float4x4 skin_mtx = - rJointMatrix[input.joints.x] * input.weights.x + - rJointMatrix[input.joints.y] * input.weights.y + - rJointMatrix[input.joints.z] * input.weights.z + - rJointMatrix[input.joints.w] * input.weights.w; - input.position = mul(skin_mtx, input.position); - input.normal = mul(skin_mtx, float4(input.normal, 0)).xyz; - } - - output.position = mul(rMatrixWVP, input.position); - output.texcoord = input.texcoord; - - return output; - } - */ - - globalvar __gm82gltf_shader_pixel_default; __gm82gltf_shader_pixel_default=shader_pixel_create_base64(" - eJxFj09qAjEUh78kIwxu5gKFztIWKoon6NgDdCG4rSWBiqIy/sFljjBHmP1sPFTO - 0UgysX1v8/2+x3skUnr/61+ZL96rJ8ACUnovgZAf9RE8IIDP5L4AldwhubpaHc18 - v93XdAKFIIvzv9nCXE/n2tB1XcYQEbuvy0bP9NvxZ6VNXU7H00k5Wq53pvw+7/TW - 6Jew89w/w4K6/XPRJG7Ii1ZELiy4JrBAWchugWXv2wqUoLACZ8ldO4hfSRlnHzmw - xNl4M++z93AHuY45Nw== - ") - - /* - SamplerState rBaseTexture: register(s0); - float4 rBaseColor; - - struct PS_INPUT { - float2 texcoord: TEXCOORD0; - float4 color: COLOR0; - }; - - struct PS_OUTPUT { - float4 color: COLOR0; - }; - - PS_OUTPUT main(PS_INPUT input) { - PS_OUTPUT output; - - float4 albedo = tex2D(rBaseTexture, input.texcoord); - - output.color = albedo * input.color * rBaseColor; - - return output; - } - */ + globalvar __gm82gltf_shader_pixel_default; + __gm82gltf_shader_pixel_default=shader_pixel_create_file(temp_directory+"\gm82\gltf_pixel.ps2") globalvar __gm82gltf_shader_vertex; __gm82gltf_shader_vertex=__gm82gltf_shader_vertex_default globalvar __gm82gltf_shader_pixel; __gm82gltf_shader_pixel=__gm82gltf_shader_pixel_default diff --git a/pixel.hlsl b/pixel.hlsl new file mode 100644 index 0000000..fcfc33e --- /dev/null +++ b/pixel.hlsl @@ -0,0 +1,21 @@ +SamplerState rBaseTexture: register(s0); +float4 rBaseColor; + +struct PS_INPUT { + float2 texcoord: TEXCOORD0; + float4 color: COLOR0; +}; + +struct PS_OUTPUT { + float4 color: COLOR0; +}; + +PS_OUTPUT main(PS_INPUT input) { + PS_OUTPUT output; + + float4 albedo = tex2D(rBaseTexture, input.texcoord); + + output.color = albedo * input.color * rBaseColor; + + return output; +} \ No newline at end of file diff --git a/vertex.hlsl b/vertex.hlsl new file mode 100644 index 0000000..4113f55 --- /dev/null +++ b/vertex.hlsl @@ -0,0 +1,38 @@ +float4x4 rMatrixW, rMatrixWVP; +float4x4 rJointMatrix[32]; +bool rSkinEnabled; + +struct VS_INPUT { + float3 normal: NORMAL; + float3 tangent: TANGENT0; + float4 position: POSITION0; + float2 texcoord: TEXCOORD0; + int4 joints: BLENDINDICES0; + float4 weights: BLENDWEIGHT0; +}; + +struct VS_OUTPUT { + float4 position: POSITION0; + float2 texcoord: TEXCOORD0; + //float4 light_col_front: COLOR0; + //float4 light_col_back: COLOR1; +}; + +VS_OUTPUT main(VS_INPUT input) { + VS_OUTPUT output; + + if (rSkinEnabled) { + float4x4 skin_mtx = + rJointMatrix[input.joints.x] * input.weights.x + + rJointMatrix[input.joints.y] * input.weights.y + + rJointMatrix[input.joints.z] * input.weights.z + + rJointMatrix[input.joints.w] * input.weights.w; + input.position = mul(skin_mtx, input.position); + input.normal = mul(skin_mtx, float4(input.normal, 0)).xyz; + } + + output.position = mul(rMatrixWVP, input.position); + output.texcoord = input.texcoord; + + return output; +} \ No newline at end of file