Skip to content

Commit

Permalink
Merge branch 'main' of github.com:kassane/sokol-d into imgui-support
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Feb 25, 2024
2 parents 280c927 + e9865d4 commit de3ddd9
Show file tree
Hide file tree
Showing 21 changed files with 434 additions and 259 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
run: |
dub build :clear
dub build :sgl_context
dub build :sgl_points
dub build :debugtext_print
# dub build :cube
# dub build :blend
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ zig build run-debugtext_print -Doptimize=ReleaseFast
zig build run-mrt -Doptimize=ReleaseFast
zig build run-saudio -Doptimize=ReleaseFast
zig build run-sgl_context -Doptimize=ReleaseFast
zig build run-sgl_points -Doptimize=ReleaseFast
zig build run-user_data -Doptimize=ReleaseFast
zig build run-triangle -Doptimize=ReleaseFast

Expand Down
7 changes: 5 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ pub fn build(b: *Build) !void {
"imgui",
"saudio",
"sgl_context",
"sgl_points",
"debugtext_print",
"user_data", // Need GC for user data [associative array]
};
Expand Down Expand Up @@ -360,10 +361,10 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*RunStep {
var objpath: []const u8 = undefined; // needed for wasm build
if (b.cache_root.path) |path| {
// immutable state hash
objpath = b.pathJoin(&.{ path, "o", &b.cache.hash.peek() });
objpath = b.pathJoin(&.{ path, "o", &b.graph.cache.hash.peek() });
try cmds.append(b.fmt("-od={s}", .{objpath}));
// mutable state hash (ldc2 cache - llvm-ir2obj)
try cmds.append(b.fmt("-cache={s}", .{b.pathJoin(&.{ path, "o", &b.cache.hash.final() })}));
try cmds.append(b.fmt("-cache={s}", .{b.pathJoin(&.{ path, "o", &b.graph.cache.hash.final() })}));
}
// name object files uniquely (so the files don't collide)
try cmds.append("-oq");
Expand Down Expand Up @@ -722,6 +723,8 @@ pub fn emLinkStep(b: *Build, options: EmLinkOptions) !*Build.Step.Run {
try emcc_cmd.append(emcc_path);
if (options.optimize == .Debug) {
try emcc_cmd.append("-Og");
try emcc_cmd.append("-sSAFE_HEAP=1");
try emcc_cmd.append("-sSTACK_OVERFLOW_CHECK=1");
} else {
try emcc_cmd.append("-sASSERTIONS=0");
if (options.optimize == .ReleaseSmall) {
Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
.hash = "12202af0db41b79a4c7904950269b5e33b7d48f50aae042e334cb8d717a5a2caf48d",
},
.emsdk = .{
.url = "git+https://github.com/emscripten-core/emsdk#2aa74907151b2caa9da865fd0d36436fdce792f0",
.hash = "1220500fe26858fc95f2a1166df2ba1a419ab9af3219383b0e57026b0928739c8e57",
.url = "git+https://github.com/emscripten-core/emsdk#c18280c8f3f8f088b4337f0f6a5fdb2be0d67179",
.hash = "12203d61e8947f25993a86b4d73e2d91b4c1d96f1aa07c581b7dffba7cb5ca91bc35",
},
.wasmd = .{
.url = "git+https://github.com/kassane/wasmd#067e21e29aea24ec936d6c6b859f7c9a79894f78",
Expand Down
29 changes: 20 additions & 9 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ subPackage {
libs "sokol"
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/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_print.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 @@ -47,7 +47,18 @@ subPackage {
libs "sokol"
lflags "-Lzig-out/lib" platform="posix"
lflags "/LIBPATH:zig-out/lib" platform="windows"
excludedSourceFiles "src/examples/clear.d" "src/examples/triangle.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_print.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 "sgl_points"
targetType "executable"
targetPath "build"
sourceFiles "src/examples/sgl_points.d"
libs "sokol"
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"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -58,7 +69,7 @@ subPackage {
libs "sokol"
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/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"
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"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -69,7 +80,7 @@ subPackage {
libs "sokol"
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/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_print.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 @@ -80,7 +91,7 @@ subPackage {
libs "sokol"
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/shaders/*.d"
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"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -91,7 +102,7 @@ subPackage {
libs "sokol"
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/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_print.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 @@ -102,7 +113,7 @@ subPackage {
libs "sokol"
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/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_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"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -113,7 +124,7 @@ subPackage {
libs "sokol"
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/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_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"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
subPackage {
Expand All @@ -124,6 +135,6 @@ subPackage {
libs "sokol"
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/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_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"
preBuildCommands "zig build -Dshared -Doptimize=ReleaseFast"
}
18 changes: 11 additions & 7 deletions src/examples/clear.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ __gshared sg.PassAction pass_action;

void init()
{
sg.Desc gfx = {
context: sgapp.context(),
logger: {func: &log.slog_func}
};
sg.Desc gfx = {context: sgapp.context(),
logger: {func: &log.slog_func}};
sg.setup(gfx);

pass_action.colors[0].load_action = sg.LoadAction.Clear;
Expand All @@ -28,11 +26,17 @@ void init()
pass_action.colors[0].clear_value.b = 0;
pass_action.colors[0].clear_value.a = 1;

debug {
debug
{
import std.stdio : writeln;
try {

try
{
writeln("Backend: ", sg.queryBackend());
} catch (Exception) {}
}
catch (Exception)
{
}
}
}

Expand Down
88 changes: 42 additions & 46 deletions src/examples/cube.d
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import sgapp = sokol.glue;
import shd = shaders.cube;
import sgutil = sokol.utils : asRange;

extern(C):
extern (C):
@safe:

struct State
Expand Down Expand Up @@ -43,57 +43,53 @@ static State state;

void init() @trusted
{
sg.Desc gfx = {
context: sgapp.context(),
logger: {func: &log.func}
};
sg.Desc gfx = {context: sgapp.context(),
logger: {func: &log.func}};
sg.setup(gfx);

float[168] vertices = [
-1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
-1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0,

-1.0, -1.0, 1.0, 0.0, 1.0, 0.0, 1.0,
1.0, -1.0, 1.0, 0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1.0, 0.0, 0.0, 1.0, 1.0,
-1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0,

1.0, -1.0, -1.0, 1.0, 0.5, 0.0, 1.0,
1.0, 1.0, -1.0, 1.0, 0.5, 0.0, 1.0,
1.0, 1.0, 1.0, 1.0, 0.5, 0.0, 1.0,
1.0, -1.0, 1.0, 1.0, 0.5, 0.0, 1.0,

-1.0, -1.0, -1.0, 0.0, 0.5, 1.0, 1.0,
-1.0, -1.0, 1.0, 0.0, 0.5, 1.0, 1.0,
1.0, -1.0, 1.0, 0.0, 0.5, 1.0, 1.0,
1.0, -1.0, -1.0, 0.0, 0.5, 1.0, 1.0,

-1.0, 1.0, -1.0, 1.0, 0.0, 0.5, 1.0,
-1.0, 1.0, 1.0, 1.0, 0.0, 0.5, 1.0,
1.0, 1.0, 1.0, 1.0, 0.0, 0.5, 1.0,
1.0, 1.0, -1.0, 1.0, 0.0, 0.5, 1.0
];

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

double[36] indices = [
0, 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20,
0, 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20,
];

sg.BufferDesc ibuffer = {
Expand All @@ -117,7 +113,7 @@ void init() @trusted

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

state.rx += 1.0 * t;
state.ry += 2.0 * t;
Expand All @@ -140,7 +136,7 @@ void cleanup()
sg.shutdown();
}

Mat4 computeVsParams(float rx, float ry)
Mat4 computeVsParams(float rx, float ry)
{
immutable proj = Mat4.perspective(60.0, app.widthf() / app.heightf(), 0.01, 10.0);
immutable rxm = Mat4.rotate(rx, Vec3(1.0, 0.0, 0.0));
Expand All @@ -165,4 +161,4 @@ void main()
logger: {func: &log.func}
};
app.run(runner);
}
}
17 changes: 9 additions & 8 deletions src/examples/debugtext_print.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ struct State

void init()
{
sg.Desc gfx = {
context: sglue.context(),
logger: {func: &log.func}
};
sg.Desc gfx = {context: sglue.context(),
logger: {func: &log.func}};
sg.setup(gfx);

sdtx.Desc desc = {
Expand All @@ -59,14 +57,17 @@ void init()
sdtx.setup(desc);
}

void print_font(uint font_index, string title, ubyte r, ubyte g, ubyte b) {
void print_font(uint font_index, string title, ubyte r, ubyte g, ubyte b)
{
sdtx.font(font_index);
sdtx.color3b(r, g, b);
sdtx.puts(&title[0]);

foreach (c; 32 .. 255) {
sdtx.putc(cast(char)c);
if ( ((c + 1) & 63) == 0) {
foreach (c; 32 .. 255)
{
sdtx.putc(cast(char) c);
if (((c + 1) & 63) == 0)
{
sdtx.crlf();
}
}
Expand Down
Loading

0 comments on commit de3ddd9

Please sign in to comment.