Skip to content

Commit

Permalink
Merge branch 'master' into sgfx-samplers-bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Jul 17, 2023
2 parents 7e72312 + f93db89 commit c2f89ec
Show file tree
Hide file tree
Showing 33 changed files with 740 additions and 874 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v3
- uses: goto-bus-stop/setup-zig@v1
with:
version: 0.10.1
version: 0.11.0-dev.4004+a57608217
- name: prepare-linux
if: runner.os == 'Linux'
run: |
Expand Down
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@

Auto-generated Zig bindings for the [sokol headers](https://github.com/floooh/sokol).

For Zig version 0.10.0
For Zig version 0.11.0-dev

If you're on the current dev version, check the branch:

- zig-0.11.0

If you're on an older Zig version, check the following branches (note that these
also contain older versions of the Sokol C headers):

- zig-0.9.1
- zig-0.8.1
- zig-0.8.0
- zig-0.7.1
> NOTE: the zig-0.11.0 branch has been merged before Zig 0.11.0 is actually released because of
> a problem in the latest sokol headers involving `if (@available(...))`, which the sokol
> headers have started to use to conditionally support more recent Metal features.
Related projects:

Expand Down
63 changes: 34 additions & 29 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const CrossTarget = std.zig.CrossTarget;
const Mode = std.builtin.Mode;

pub const Backend = enum {
auto, // Windows: D3D11, macOS/iOS: Metal, otherwise: GL
auto, // Windows: D3D11, macOS/iOS: Metal, otherwise: GL
d3d11,
metal,
gl,
Expand All @@ -18,20 +18,20 @@ pub const Backend = enum {
pub const Config = struct {
backend: Backend = .auto,
force_egl: bool = false,

enable_x11: bool = true,
enable_wayland: bool = false
};

// build sokol into a static library
pub fn buildSokol(b: *Builder, target: CrossTarget, mode: Mode, config: Config, comptime prefix_path: []const u8) *LibExeObjStep {
const lib = b.addStaticLibrary("sokol", null);
lib.setBuildMode(mode);
lib.setTarget(target);

pub fn buildSokol(b: *Builder, target: CrossTarget, optimize: Mode, config: Config, comptime prefix_path: []const u8) *LibExeObjStep {
const lib = b.addStaticLibrary(.{
.name = "sokol",
.target = target,
.optimize = optimize,
});
lib.linkLibC();
const sokol_path = prefix_path ++ "src/sokol/c/";
const csources = [_][]const u8 {
const csources = [_][]const u8{
"sokol_log.c",
"sokol_app.c",
"sokol_gfx.c",
Expand All @@ -43,9 +43,13 @@ pub fn buildSokol(b: *Builder, target: CrossTarget, mode: Mode, config: Config,
};
var _backend = config.backend;
if (_backend == .auto) {
if (lib.target.isDarwin()) { _backend = .metal; }
else if (lib.target.isWindows()) { _backend = .d3d11; }
else { _backend = .gl; }
if (lib.target.isDarwin()) {
_backend = .metal;
} else if (lib.target.isWindows()) {
_backend = .d3d11;
} else {
_backend = .gl;
}
}
const backend_option = switch (_backend) {
.d3d11 => "-DSOKOL_D3D11",
Expand All @@ -59,16 +63,15 @@ pub fn buildSokol(b: *Builder, target: CrossTarget, mode: Mode, config: Config,

if (lib.target.isDarwin()) {
inline for (csources) |csrc| {
lib.addCSourceFile(sokol_path ++ csrc, &[_][]const u8{"-ObjC", "-DIMPL", backend_option});
lib.addCSourceFile(sokol_path ++ csrc, &[_][]const u8{ "-ObjC", "-DIMPL", backend_option });
}
lib.linkFramework("Cocoa");
lib.linkFramework("QuartzCore");
lib.linkFramework("AudioToolbox");
if (.metal == _backend) {
lib.linkFramework("MetalKit");
lib.linkFramework("Metal");
}
else {
} else {
lib.linkFramework("OpenGL");
}
} else {
Expand All @@ -77,7 +80,7 @@ pub fn buildSokol(b: *Builder, target: CrossTarget, mode: Mode, config: Config,
var wayland_flag = if (!config.enable_wayland) "-DSOKOL_DISABLE_WAYLAND" else "";

inline for (csources) |csrc| {
lib.addCSourceFile(sokol_path ++ csrc, &[_][]const u8{"-DIMPL", backend_option, egl_flag, x11_flag, wayland_flag});
lib.addCSourceFile(sokol_path ++ csrc, &[_][]const u8{ "-DIMPL", backend_option, egl_flag, x11_flag, wayland_flag });
}

if (lib.target.isLinux()) {
Expand Down Expand Up @@ -108,8 +111,7 @@ pub fn buildSokol(b: *Builder, target: CrossTarget, mode: Mode, config: Config,
if (link_egl) {
lib.linkSystemLibrary("egl");
}
}
else if (lib.target.isWindows()) {
} else if (lib.target.isWindows()) {
lib.linkSystemLibraryName("kernel32");
lib.linkSystemLibraryName("user32");
lib.linkSystemLibraryName("gdi32");
Expand All @@ -124,14 +126,18 @@ pub fn buildSokol(b: *Builder, target: CrossTarget, mode: Mode, config: Config,
}

// build one of the example exes
fn buildExample(b: *Builder, target: CrossTarget, mode: Mode, sokol: *LibExeObjStep, comptime name: []const u8) void {
const e = b.addExecutable(name, "src/examples/" ++ name ++ ".zig");
e.setBuildMode(mode);
e.setTarget(target);
fn buildExample(b: *Builder, target: CrossTarget, optimize: Mode, sokol: *LibExeObjStep, comptime name: []const u8) void {
const e = b.addExecutable(.{
.name = name,
.root_source_file = .{ .path = "src/examples/" ++ name ++ ".zig" },
.target = target,
.optimize = optimize,
});
e.linkLibrary(sokol);
e.addPackagePath("sokol", "src/sokol/sokol.zig");
e.install();
b.step("run-" ++ name, "Run " ++ name).dependOn(&e.run().step);
e.addAnonymousModule("sokol", .{ .source_file = .{ .path = "src/sokol/sokol.zig" } });
b.installArtifact(e);
const run = b.addRunArtifact(e);
b.step("run-" ++ name, "Run " ++ name).dependOn(&run.step);
}

pub fn build(b: *Builder) void {
Expand All @@ -147,17 +153,16 @@ pub fn build(b: *Builder) void {
config.force_egl = b.option(bool, "egl", "Use EGL instead of GLX if possible (default: false)") orelse false;

const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
const sokol = buildSokol(b, target, mode, config, "");
const optimize = b.standardOptimizeOption(.{});
const sokol = buildSokol(b, target, optimize, config, "");
const examples = .{
"clear",
"triangle",
"quad",
"bufferoffsets",
"cube",
"noninterleaved",
"texcube",
"blend",
"texcube", "blend",
"offscreen",
"instancing",
"mrt",
Expand All @@ -171,7 +176,7 @@ pub fn build(b: *Builder) void {
"shapes"
};
inline for (examples) |example| {
buildExample(b, target, mode, sokol, example);
buildExample(b, target, optimize, sokol, example);
}
buildShaders(b);
}
Expand Down
54 changes: 29 additions & 25 deletions src/examples/blend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// Test/demonstrate blend modes.
//------------------------------------------------------------------------------
const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const vec3 = @import("math.zig").Vec3;
const mat4 = @import("math.zig").Mat4;
const shd = @import("shaders/blend.glsl.zig");
const vec3 = @import("math.zig").Vec3;
const mat4 = @import("math.zig").Mat4;
const shd = @import("shaders/blend.glsl.zig");

const NUM_BLEND_FACTORS = 15;

Expand All @@ -34,13 +34,13 @@ export fn init() void {
state.pass_action.stencil.load_action = .DONTCARE;

state.bind.vertex_buffers[0] = sg.makeBuffer(.{
.data = sg.asRange(&[_]f32 {
// 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
})
.data = sg.asRange(&[_]f32{
// 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,
}),
});

// pipeline object for rendering the background
Expand All @@ -56,7 +56,7 @@ export fn init() void {
pip_desc = .{
.shader = sg.makeShader(shd.quadShaderDesc(sg.queryBackend())),
.primitive_type = .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 },
};
pip_desc.layout.attrs[shd.ATTR_vs_quad_position].format = .FLOAT3;
pip_desc.layout.attrs[shd.ATTR_vs_quad_color0].format = .FLOAT4;
Expand All @@ -65,17 +65,19 @@ export fn init() void {
.src_factor_alpha = .ONE,
.dst_factor_alpha = .ZERO,
};
var src: usize = 0; while (src < NUM_BLEND_FACTORS): (src += 1) {
var dst: usize = 0; while (dst < NUM_BLEND_FACTORS): (dst += 1) {
pip_desc.colors[0].blend.src_factor_rgb = @intToEnum(sg.BlendFactor, src + 1);
pip_desc.colors[0].blend.dst_factor_rgb = @intToEnum(sg.BlendFactor, dst + 1);
var src: usize = 0;
while (src < NUM_BLEND_FACTORS) : (src += 1) {
var dst: usize = 0;
while (dst < NUM_BLEND_FACTORS) : (dst += 1) {
pip_desc.colors[0].blend.src_factor_rgb = @as(sg.BlendFactor, @enumFromInt(src + 1));
pip_desc.colors[0].blend.dst_factor_rgb = @as(sg.BlendFactor, @enumFromInt(dst + 1));
state.pip[src][dst] = sg.makePipeline(pip_desc);
}
}
}

export fn frame() void {
const time = @floatCast(f32, sapp.frameDuration()) * 60.0;
const time = @as(f32, @floatCast(sapp.frameDuration())) * 60.0;

sg.beginDefaultPass(state.pass_action, sapp.width(), sapp.height());

Expand All @@ -94,18 +96,20 @@ export fn frame() void {

state.r += 0.6 * time;
var r0 = state.r;
var src: usize = 0; while (src < NUM_BLEND_FACTORS): (src += 1) {
var dst: usize = 0; while (dst < NUM_BLEND_FACTORS): (dst += 1) {
var src: usize = 0;
while (src < NUM_BLEND_FACTORS) : (src += 1) {
var dst: usize = 0;
while (dst < NUM_BLEND_FACTORS) : (dst += 1) {
// compute model-view-proj matrix
const shift = NUM_BLEND_FACTORS / 2;
const t: vec3 = .{
.x = (@intToFloat(f32, dst) - shift) * 3.0,
.y = (@intToFloat(f32, src) - shift) * 2.2,
.z = 0.0
.x = (@as(f32, @floatFromInt(dst)) - shift) * 3.0,
.y = (@as(f32, @floatFromInt(src)) - shift) * 2.2,
.z = 0.0,
};
const model = mat4.mul(mat4.translate(t), mat4.rotate(r0, vec3.up()));
const quad_vs_params: shd.QuadVsParams = .{
.mvp = mat4.mul(view_proj, model)
.mvp = mat4.mul(view_proj, model),
};
sg.applyPipeline(state.pip[src][dst]);
sg.applyBindings(state.bind);
Expand Down
41 changes: 18 additions & 23 deletions src/examples/bufferoffsets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@
// buffer offsets.
//------------------------------------------------------------------------------
const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const shd = @import("shaders/bufferoffsets.glsl.zig");
const shd = @import("shaders/bufferoffsets.glsl.zig");

const state = struct {
var pass_action: sg.PassAction = .{};
var pip: sg.Pipeline = .{};
var bind: sg.Bindings = .{};
};

const Vertex = extern struct {
x: f32, y: f32,
r: f32, g: f32, b: f32
};
const Vertex = extern struct { x: f32, y: f32, r: f32, g: f32, b: f32 };

export fn init() void {
sg.setup(.{
Expand All @@ -29,38 +26,36 @@ export fn init() void {
});

// clear to a blue-ish color
state.pass_action.colors[0] = .{ .load_action = .CLEAR, .clear_value = .{ .r=0.5, .g=0.5, .b=1, .a=1 } };
state.pass_action.colors[0] = .{ .load_action = .CLEAR, .clear_value = .{ .r = 0.5, .g = 0.5, .b = 1, .a = 1 } };

// a 2D triangle and quad in 1 vertex buffer and 1 index buffer
state.bind.vertex_buffers[0] = sg.makeBuffer(.{
.data = sg.asRange(&[_]Vertex{
// triangle vertices
.{ .x= 0.0, .y= 0.55, .r=1.0, .g=0.0, .b=0.0 },
.{ .x= 0.25, .y= 0.05, .r=0.0, .g=1.0, .b=0.0 },
.{ .x=-0.25, .y= 0.05, .r=0.0, .g=0.0, .b=1.0 },
.{ .x = 0.0, .y = 0.55, .r = 1.0, .g = 0.0, .b = 0.0 },
.{ .x = 0.25, .y = 0.05, .r = 0.0, .g = 1.0, .b = 0.0 },
.{ .x = -0.25, .y = 0.05, .r = 0.0, .g = 0.0, .b = 1.0 },

// quad vertices
.{ .x=-0.25, .y=-0.05, .r=0.0, .g=0.0, .b=1.0 },
.{ .x= 0.25, .y=-0.05, .r=0.0, .g=1.0, .b=0.0 },
.{ .x= 0.25, .y=-0.55, .r=1.0, .g=0.0, .b=0.0 },
.{ .x=-0.25, .y=-0.55, .r=1.0, .g=1.0, .b=0.0 }
})
.{ .x = -0.25, .y = -0.05, .r = 0.0, .g = 0.0, .b = 1.0 },
.{ .x = 0.25, .y = -0.05, .r = 0.0, .g = 1.0, .b = 0.0 },
.{ .x = 0.25, .y = -0.55, .r = 1.0, .g = 0.0, .b = 0.0 },
.{ .x = -0.25, .y = -0.55, .r = 1.0, .g = 1.0, .b = 0.0 },
}),
});
state.bind.index_buffer = sg.makeBuffer(.{
.type = .INDEXBUFFER,
.data = sg.asRange(&[_]u16{
// triangle indices
0, 1, 2,
// quad indices
0, 1, 2, 0, 2, 3
})
0, 1, 2,
0, 2, 3,
}),
});

// a shader and pipeline object
var pip_desc: sg.PipelineDesc = .{
.shader = sg.makeShader(shd.bufferoffsetsShaderDesc(sg.queryBackend())),
.index_type = .UINT16
};
var pip_desc: sg.PipelineDesc = .{ .shader = sg.makeShader(shd.bufferoffsetsShaderDesc(sg.queryBackend())), .index_type = .UINT16 };
pip_desc.layout.attrs[shd.ATTR_vs_position].format = .FLOAT2;
pip_desc.layout.attrs[shd.ATTR_vs_color0].format = .FLOAT3;
state.pip = sg.makePipeline(pip_desc);
Expand Down
15 changes: 6 additions & 9 deletions src/examples/clear.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
// Just clear the framebuffer with an animated color.
//------------------------------------------------------------------------------
const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const print = @import("std").debug.print;

var pass_action: sg.PassAction = .{};

export fn init() void {
sg.setup(.{
.context = sgapp.context(),
.logger = .{ .func = slog.func }
});
pass_action.colors[0] = .{ .load_action=.CLEAR, .clear_value=.{ .r=1, .g=1, .b=0, .a=1 } };
print("Backend: {}\n", .{ sg.queryBackend()});
sg.setup(.{ .context = sgapp.context(), .logger = .{ .func = slog.func } });
pass_action.colors[0] = .{ .load_action = .CLEAR, .clear_value = .{ .r = 1, .g = 1, .b = 0, .a = 1 } };
print("Backend: {}\n", .{sg.queryBackend()});
}

export fn frame() void {
Expand Down
Loading

0 comments on commit c2f89ec

Please sign in to comment.