Skip to content

Commit

Permalink
Fix sample cleanup. (#15)
Browse files Browse the repository at this point in the history
* build.zig: GLCORE33 => GLCORE, glsl330 => glsl430

* manual bindings update

* recompile shaders

* manual bindings update (add 'scope' to ref and pointer args)

* rebuild shaders

* cleanup and fix clear and triangle sample

* examples wip cleanup

* rename and cleanup debugtext sample

* build.zig: add triangle.glsl, rename debugtext sample

* fix missing debugtext_print => debugtext renames

* fix/cleanip saudio sample

* fix/cleanup sgl_points sample

* fix/cleanup sgl_context sample

* wip

* build.zig: remove the -cleanup-obj flag, this broke debugging via lldb on macOS

* rebuild shaders (default-zero-init)

* blend.d code cleanup wip

* manually updated bindings

* mrt.d: remove the scope on the event callback again

* math.d: fix matrix multiplication

* math.d brace style

* fix cube sample

* remove sgutil.asRange imports

* fix mrt sample

* remove the sgutil.asRange helper function

* remove utils.d, user_data.d coding style

* activate the out-commented dub samples in the CI build.yml

* add -preview=all -i flags to dub.sdl

* fix code-generated shaderDescFunc naming convention
  • Loading branch information
floooh authored May 12, 2024
1 parent c20da5b commit 9198de3
Show file tree
Hide file tree
Showing 40 changed files with 7,292 additions and 6,408 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- uses: dlang-community/setup-dlang@v1
with:
compiler: ldc-master

- name: prepare-linux
if: runner.os == 'Linux'
run: |
Expand All @@ -30,11 +30,11 @@ jobs:
dub build :clear
dub build :sgl_context
dub build :sgl_points
dub build :debugtext_print
# dub build :cube
# dub build :blend
dub build :debugtext
dub build :cube
dub build :blend
dub build :triangle
# dub build :mrt
dub build :mrt
dub build :user_data
- name: (Zig) Running Test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ zig build -Doptimize=ReleaseFast -Dshared
zig build run-blend -Doptimize=ReleaseFast
zig build run-clear -Doptimize=ReleaseFast
zig build run-cube -Doptimize=ReleaseFast
zig build run-debugtext_print -Doptimize=ReleaseFast
zig build run-debugtext -Doptimize=ReleaseFast
zig build run-mrt -Doptimize=ReleaseFast
zig build run-saudio -Doptimize=ReleaseFast
zig build run-sgl_context -Doptimize=ReleaseFast
Expand Down
11 changes: 4 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
const backend_cflags = switch (backend) {
.d3d11 => "-DSOKOL_D3D11",
.metal => "-DSOKOL_METAL",
.gl => "-DSOKOL_GLCORE33",
.gl => "-DSOKOL_GLCORE",
.gles3 => "-DSOKOL_GLES3",
.wgpu => "-DSOKOL_WGPU",
else => @panic("unknown sokol backend"),
Expand Down Expand Up @@ -218,7 +218,7 @@ pub fn build(b: *Build) !void {
"saudio",
"sgl_context",
"sgl_points",
"debugtext_print",
"debugtext",
"user_data", // Need GC for user data [associative array]
};

Expand Down Expand Up @@ -360,10 +360,6 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {
// name object files uniquely (so the files don't collide)
try cmds.append("-oq");

// remove object files after success build, and put them in a unique temp directory
if (options.kind != .obj)
try cmds.append("-cleanup-obj");

// disable LLVM-IR verifier
// https://llvm.org/docs/Passes.html#verify-module-verifier
try cmds.append("-disable-verify");
Expand Down Expand Up @@ -630,6 +626,7 @@ fn buildShaders(b: *Build) void {
const sokol_tools_bin_dir = "../sokol-tools-bin/bin/";
const shaders_dir = "src/shaders/";
const shaders = .{
"triangle.glsl",
"bufferoffsets.glsl",
"cube.glsl",
"instancing.glsl",
Expand Down Expand Up @@ -661,7 +658,7 @@ fn buildShaders(b: *Build) void {
"-o",
shaders_dir ++ shader[0 .. shader.len - 5] ++ ".d",
"-l",
"glsl330:metal_macos:hlsl4:glsl300es:wgsl",
"glsl430:metal_macos:hlsl4:glsl300es:wgsl",
"-f",
"sokol_d",
});
Expand Down
32 changes: 21 additions & 11 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ subPackage {
targetPath "build"
sourceFiles "src/examples/clear.d"
libs "sokol"
dflags "-preview=all" "-i"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/saudio.d" "src/examples/debugtext.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -45,9 +46,10 @@ subPackage {
targetPath "build"
sourceFiles "src/examples/sgl_context.d"
libs "sokol"
dflags "-preview=all" "-i"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/clear.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/clear.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/saudio.d" "src/examples/debugtext.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -56,17 +58,19 @@ subPackage {
targetPath "build"
sourceFiles "src/examples/sgl_points.d"
libs "sokol"
dflags "-preview=all" "-i"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/clear.d" "src/examples/triangle.d" "src/examples/sgl_context.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/clear.d" "src/examples/triangle.d" "src/examples/sgl_context.d" "src/examples/saudio.d" "src/examples/debugtext.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
name "debugtext_print"
name "debugtext"
targetType "executable"
targetPath "build"
sourceFiles "src/examples/debugtext_print.d"
sourceFiles "src/examples/debugtext.d"
libs "sokol"
dflags "-preview=all" "-i"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/saudio.d" "src/examples/clear.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
Expand All @@ -78,9 +82,10 @@ subPackage {
targetPath "build"
sourceFiles "src/examples/triangle.d"
libs "sokol"
dflags "-preview=all" "-i"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/sgl_points.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/sgl_points.d" "src/examples/saudio.d" "src/examples/debugtext.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -89,9 +94,10 @@ subPackage {
targetPath "build"
sourceFiles "src/examples/blend.d"
libs "sokol"
dflags "-preview=all" "-i"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -100,9 +106,10 @@ subPackage {
targetPath "build"
sourceFiles "src/examples/saudio.d"
libs "sokol"
dflags "-preview=all" "-i"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/debugtext_print.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/debugtext.d" "src/examples/mrt.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -111,9 +118,10 @@ subPackage {
targetPath "build"
sourceFiles "src/examples/mrt.d"
libs "sokol"
dflags "-preview=all" "-i"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/user_data.d" "src/examples/cube.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -122,9 +130,10 @@ subPackage {
targetPath "build"
sourceFiles "src/examples/cube.d"
libs "sokol"
dflags "-preview=all" "-i"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/user_data.d" "src/examples/mrt.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/user_data.d" "src/examples/mrt.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -133,8 +142,9 @@ subPackage {
targetPath "build"
sourceFiles "src/examples/user_data.d"
libs "sokol"
dflags "-preview=all" "-i"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext_print.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/cube.d" "src/examples/mrt.d" "src/examples/blend.d" "src/shaders/*.d"
excludedSourceFiles "src/examples/sgl_context.d" "src/examples/clear.d" "src/examples/saudio.d" "src/examples/debugtext.d" "src/examples/triangle.d" "src/examples/sgl_points.d" "src/examples/cube.d" "src/examples/mrt.d" "src/examples/blend.d" "src/shaders/*.d"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
103 changes: 50 additions & 53 deletions src/examples/blend.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,75 @@ import app = sokol.app;
import log = sokol.log;
import handmade.math : Mat4, Vec3;
import sglue = sokol.glue;
import sgutil = sokol.utils : asRange;
import shd = shaders.blend;

extern (C):
@safe:

immutable NUM_BLEND_FACTORS = 15;

struct State
{
struct State {
float r = 0.0f;
sg.Pipeline bg_pip;
sg.Pipeline[NUM_BLEND_FACTORS][NUM_BLEND_FACTORS] pips;
sg.Bindings bind;
sg.PassAction passAction = {
colors: [
{load_action: sg.LoadAction.Dontcare}
],
depth: {load_action: sg.LoadAction.Dontcare},
stencil: {load_action: sg.LoadAction.Dontcare}
colors: [ { load_action: sg.LoadAction.Dontcare } ],
depth: { load_action: sg.LoadAction.Dontcare },
stencil: { load_action: sg.LoadAction.Dontcare }
};
shd.QuadVsParams quad_vs_params;
shd.BgFsParams bg_fs_params;
}

static State state;

void init() @trusted
{
void init() {
sg.Desc gfx = {
pipeline_pool_size: NUM_BLEND_FACTORS * NUM_BLEND_FACTORS + 1,
environment: sglue.environment,
logger: {func: &log.func}
logger: { func: &log.func }
};
sg.setup(gfx);

float[28] vertices = [
// pos color
-1.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.5,
1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.5,
-1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5,
1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.5,
-1.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.5,
1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.5,
-1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5,
1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.5,
];

// Create vertex buffer
sg.BufferDesc vbuffer = {data: sgutil.asRange(&vertices[0])};
state.bind.vertex_buffers[0] = sg.makeBuffer(vbuffer);
// create vertex buffer
sg.BufferDesc vbufd = {
data: {
ptr: vertices.ptr,
size: vertices.sizeof,
}
};
state.bind.vertex_buffers[0] = sg.makeBuffer(vbufd);

sg.ShaderDesc bgShader = shd.bg_shader_desc(sg.queryBackend());
sg.PipelineDesc pld = {
layout: {buffers: [{stride: 28}]},
shader: sg.makeShader(shd.bgShaderDesc(sg.queryBackend())),
layout: {
buffers: [ { stride: 28 } ],
attrs: [
shd.ATTR_VS_BG_POSITION: { format: sg.VertexFormat.Float2 },
],
},
primitive_type: sg.PrimitiveType.Triangle_strip
};
pld.shader = sg.makeShader(bgShader);
pld.layout.attrs[shd.ATTR_VS_BG_POSITION].format = sg.VertexFormat.Float2;
state.bg_pip = sg.makePipeline(pld);

sg.ShaderDesc quadShader = shd.quad_shader_desc(sg.queryBackend());
sg.PipelineDesc pip_desc = {
layout: {buffers: [{stride: 28}]},
shader: sg.makeShader(shd.quadShaderDesc(sg.queryBackend())),
layout: {
attrs: [
shd.ATTR_VS_QUAD_POSITION: { format: sg.VertexFormat.Float3 },
shd.ATTR_VS_QUAD_COLOR0: { format: sg.VertexFormat.Float4 },
],
},
primitive_type: sg.PrimitiveType.Triangle_strip,
blend_color: {r: 1.0, g: 0.0, b: 0.0, a: 1.0},
blend_color: { r: 1.0, g: 0.0, b: 0.0, a: 1.0 },
colors: [
{
blend: {
Expand All @@ -82,22 +89,16 @@ void init() @trusted
}
]
};
pip_desc.shader = sg.makeShader(quadShader);
pip_desc.layout.attrs[shd.ATTR_VS_QUAD_POSITION].format = sg.VertexFormat.Float3;
pip_desc.layout.attrs[shd.ATTR_VS_QUAD_COLOR0].format = sg.VertexFormat.Float4;
foreach (src; 0 .. NUM_BLEND_FACTORS)
{
foreach (dst; 0 .. NUM_BLEND_FACTORS)
{
foreach (src; 0 .. NUM_BLEND_FACTORS) {
foreach (dst; 0 .. NUM_BLEND_FACTORS) {
pip_desc.colors[0].blend.src_factor_rgb = cast(sg.BlendFactor)(src + 1);
pip_desc.colors[0].blend.dst_factor_rgb = cast(sg.BlendFactor)(dst + 1);
state.pips[src][dst] = sg.makePipeline(pip_desc);
}
}
}

void frame()
{
void frame() {
immutable float t = cast(float)(app.frameDuration() * 60.0);

state.r += 0.6 * t;
Expand All @@ -108,36 +109,34 @@ void frame()
immutable view = Mat4.lookAt(Vec3(0.0, 0.0, 25.0), Vec3.zero(), Vec3.up());
immutable view_proj = Mat4.mul(proj, view);

sg.Pass pass = {action: state.passAction, swapchain: sglue.swapchain};
sg.Pass pass = { action: state.passAction, swapchain: sglue.swapchain() };
sg.beginPass(pass);

sg.Range r = sgutil.asRange(state.bg_fs_params);
sg.Range r = {
ptr: &state.bg_fs_params,
size: state.bg_fs_params.sizeof,
};
sg.applyPipeline(state.bg_pip);
sg.applyBindings(state.bind);
sg.applyUniforms(sg.ShaderStage.Fs, shd.SLOT_BG_FS_PARAMS, r);
sg.draw(0, 4, 1);

// draw the blended quads
float r0 = state.r;
foreach (src; 0 .. NUM_BLEND_FACTORS)
{
foreach (dst; 0 .. NUM_BLEND_FACTORS)
{
foreach (src; 0 .. NUM_BLEND_FACTORS) {
foreach (dst; 0 .. NUM_BLEND_FACTORS) {
// compute model-view-proj matrix
auto rm = Mat4.rotate(state.r, Vec3(0.0, 1.0, 0.0));
immutable x = (dst - (NUM_BLEND_FACTORS / 2)) * 3.0;
immutable y = (src - (NUM_BLEND_FACTORS / 2)) * 2.2;
immutable model = Mat4.mul(Mat4.translate(Vec3(x, y, 0.0)), rm);
state.quad_vs_params.mvp = Mat4.mul(view_proj, model);
sg.Range rg = sgutil.asRange(state.quad_vs_params);

sg.Range rg = {
ptr: &state.quad_vs_params,
size: state.quad_vs_params.sizeof,
};
sg.applyPipeline(state.pips[src][dst]);
sg.applyBindings(state.bind);
sg.applyUniforms(
sg.ShaderStage.Vs,
shd.SLOT_QUAD_VS_PARAMS,
rg,
);
sg.applyUniforms(sg.ShaderStage.Vs, shd.SLOT_QUAD_VS_PARAMS, rg);
sg.draw(0, 4, 1);
r0 += 0.6;
}
Expand All @@ -146,13 +145,11 @@ void frame()
sg.commit();
}

void cleanup()
{
void cleanup() {
sg.shutdown();
}

void main()
{
void main() {
app.Desc runner = {
window_title: "blend.d",
init_cb: &init,
Expand Down
Loading

0 comments on commit 9198de3

Please sign in to comment.