Skip to content

Commit

Permalink
set up shaders at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
skyfloogle committed Aug 15, 2024
1 parent cfb2f31 commit 2dac578
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/zig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
29 changes: 28 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -32,13 +43,29 @@ 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" } });
gm82gex_run.step.dependOn(&gm82gex_file.step);
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(.{
Expand Down
18 changes: 18 additions & 0 deletions gm82gltf.gej
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
]
}
91 changes: 4 additions & 87 deletions gm82gltf.gml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions pixel.hlsl
Original file line number Diff line number Diff line change
@@ -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;
}
38 changes: 38 additions & 0 deletions vertex.hlsl
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 2dac578

Please sign in to comment.