Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for sokol-gfx pass cleanup. #57

Merged
merged 8 commits into from Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,14 @@
to the example code or the supported Zig version. For actual Sokol header changes, see the
[sokol changelog](https://github.com/floooh/sokol/blob/master/CHANGELOG.md).

### 29-Feb-2024

**BREAKING CHANGES**

- The examples have been updated for the 'render pass cleanup' in sokol-gfx, please
see the [sokol changelog](https://github.com/floooh/sokol/blob/master/CHANGELOG.md)
for details!

### 17-Jan-2024

- Switched the master branch to support the Zig nightly versions, this is different from before
Expand Down
1 change: 1 addition & 0 deletions build.zig
Expand Up @@ -263,6 +263,7 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*Build.Step.Compile {
"sokol_gl.c",
"sokol_debugtext.c",
"sokol_shape.c",
"sokol_glue.c",
};
inline for (csources) |csrc| {
lib.addCSourceFile(.{
Expand Down
8 changes: 4 additions & 4 deletions src/examples/blend.zig
Expand Up @@ -6,7 +6,7 @@ const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const sglue = sokol.glue;
const vec3 = @import("math.zig").Vec3;
const mat4 = @import("math.zig").Mat4;
const shd = @import("shaders/blend.glsl.zig");
Expand All @@ -25,7 +25,7 @@ const state = struct {
export fn init() void {
sg.setup(.{
.pipeline_pool_size = NUM_BLEND_FACTORS * NUM_BLEND_FACTORS + 1,
.context = sgapp.context(),
.environment = sglue.environment(),
.logger = .{ .func = slog.func },
});

Expand All @@ -35,7 +35,7 @@ export fn init() void {

state.bind.vertex_buffers[0] = sg.makeBuffer(.{
.data = sg.asRange(&[_]f32{
// pos color
// 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,
Expand Down Expand Up @@ -79,7 +79,7 @@ export fn init() void {
export fn frame() void {
const time: f32 = @floatCast(sapp.frameDuration() * 60.0);

sg.beginDefaultPass(state.pass_action, sapp.width(), sapp.height());
sg.beginPass(.{ .action = state.pass_action, .swapchain = sglue.swapchain() });

// draw background
state.tick += 1.0 * time;
Expand Down
6 changes: 3 additions & 3 deletions src/examples/bufferoffsets.zig
Expand Up @@ -8,7 +8,7 @@ const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const sglue = sokol.glue;
const shd = @import("shaders/bufferoffsets.glsl.zig");

const state = struct {
Expand All @@ -21,7 +21,7 @@ const Vertex = extern struct { x: f32, y: f32, r: f32, g: f32, b: f32 };

export fn init() void {
sg.setup(.{
.context = sgapp.context(),
.environment = sglue.environment(),
.logger = .{ .func = slog.func },
});

Expand Down Expand Up @@ -65,7 +65,7 @@ export fn init() void {
}

export fn frame() void {
sg.beginDefaultPass(state.pass_action, sapp.width(), sapp.height());
sg.beginPass(.{ .action = state.pass_action, .swapchain = sglue.swapchain() });
sg.applyPipeline(state.pip);

// render the triangle
Expand Down
14 changes: 5 additions & 9 deletions src/examples/clear.zig
Expand Up @@ -7,14 +7,14 @@ const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const sglue = sokol.glue;
const print = @import("std").debug.print;

var pass_action: sg.PassAction = .{};

export fn init() void {
sg.setup(.{
.context = sgapp.context(),
.environment = sglue.environment(),
.logger = .{ .func = slog.func },
});
pass_action.colors[0] = .{
Expand All @@ -27,7 +27,7 @@ export fn init() void {
export fn frame() void {
const g = pass_action.colors[0].clear_value.g + 0.01;
pass_action.colors[0].clear_value.g = if (g > 1.0) 0.0 else g;
sg.beginDefaultPass(pass_action, sapp.width(), sapp.height());
sg.beginPass(.{ .action = pass_action, .swapchain = sglue.swapchain() });
sg.endPass();
sg.commit();
}
Expand All @@ -43,13 +43,9 @@ pub fn main() void {
.cleanup_cb = cleanup,
.width = 640,
.height = 480,
.icon = .{
.sokol_default = true,
},
.icon = .{ .sokol_default = true },
.window_title = "clear.zig",
.logger = .{
.func = slog.func,
},
.logger = .{ .func = slog.func },
.win32_console_attach = true,
});
}
6 changes: 3 additions & 3 deletions src/examples/cube.zig
Expand Up @@ -7,7 +7,7 @@ const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const sglue = sokol.glue;
const vec3 = @import("math.zig").Vec3;
const mat4 = @import("math.zig").Mat4;
const shd = @import("shaders/cube.glsl.zig");
Expand All @@ -23,7 +23,7 @@ const state = struct {

export fn init() void {
sg.setup(.{
.context = sgapp.context(),
.environment = sglue.environment(),
.logger = .{ .func = slog.func },
});

Expand Down Expand Up @@ -100,7 +100,7 @@ export fn frame() void {
state.ry += 2.0 * dt;
const vs_params = computeVsParams(state.rx, state.ry);

sg.beginDefaultPass(state.pass_action, sapp.width(), sapp.height());
sg.beginPass(.{ .action = state.pass_action, .swapchain = sglue.swapchain() });
sg.applyPipeline(state.pip);
sg.applyBindings(state.bind);
sg.applyUniforms(.VS, shd.SLOT_vs_params, sg.asRange(&vs_params));
Expand Down
6 changes: 3 additions & 3 deletions src/examples/debugtext-print.zig
Expand Up @@ -7,7 +7,7 @@ const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const sglue = sokol.glue;
const stm = sokol.time;
const sdtx = @import("sokol").debugtext;

Expand Down Expand Up @@ -36,7 +36,7 @@ export fn init() void {
// setup sokol.time and sokol.gfx
stm.setup();
sg.setup(.{
.context = sgapp.context(),
.environment = sglue.environment(),
.logger = .{ .func = slog.func },
});

Expand Down Expand Up @@ -76,7 +76,7 @@ export fn frame() void {
fmt.format(writer, "using std.fmt directly ({d})\n", .{state.frame_count}) catch unreachable;

// render the frame via sokol.gfx
sg.beginDefaultPass(state.pass_action, sapp.width(), sapp.height());
sg.beginPass(.{ .action = state.pass_action, .swapchain = sglue.swapchain() });
sdtx.draw();
sg.endPass();
sg.commit();
Expand Down
6 changes: 3 additions & 3 deletions src/examples/debugtext-userfont.zig
Expand Up @@ -7,7 +7,7 @@ const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const sglue = sokol.glue;
const sdtx = sokol.debugtext;

// use font-slot 1 for the user-defined font, there's no particular
Expand Down Expand Up @@ -45,7 +45,7 @@ const state = struct {

export fn init() void {
sg.setup(.{
.context = sgapp.context(),
.environment = sglue.environment(),
.logger = .{ .func = slog.func },
});

Expand Down Expand Up @@ -85,7 +85,7 @@ export fn frame() void {
sdtx.putc(c);
}

sg.beginDefaultPass(state.pass_action, sapp.width(), sapp.height());
sg.beginPass(.{ .action = state.pass_action, .swapchain = sglue.swapchain() });
sdtx.draw();
sg.endPass();
sg.commit();
Expand Down
6 changes: 3 additions & 3 deletions src/examples/debugtext.zig
Expand Up @@ -7,7 +7,7 @@ const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const sglue = sokol.glue;
const sdtx = sokol.debugtext;

// font indices
Expand All @@ -22,7 +22,7 @@ var pass_action: sg.PassAction = .{};

export fn init() void {
sg.setup(.{
.context = sgapp.context(),
.environment = sglue.environment(),
.logger = .{ .func = slog.func },
});

Expand Down Expand Up @@ -70,7 +70,7 @@ export fn frame() void {
printFont(ORIC, "Oric Atmos:\n", 0xff, 0x98, 0x00);

// do the actual rendering
sg.beginDefaultPass(pass_action, sapp.width(), sapp.height());
sg.beginPass(.{ .action = pass_action, .swapchain = sglue.swapchain() });
sdtx.draw();
sg.endPass();
sg.commit();
Expand Down
6 changes: 3 additions & 3 deletions src/examples/instancing.zig
Expand Up @@ -8,7 +8,7 @@ const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const sglue = sokol.glue;
const vec3 = @import("math.zig").Vec3;
const mat4 = @import("math.zig").Mat4;
const shd = @import("shaders/instancing.glsl.zig");
Expand All @@ -31,7 +31,7 @@ const state = struct {

export fn init() void {
sg.setup(.{
.context = sgapp.context(),
.environment = sglue.environment(),
.logger = .{ .func = slog.func },
});

Expand Down Expand Up @@ -133,7 +133,7 @@ export fn frame() void {
const vs_params = computeVsParams(1.0, state.ry);

// and finally draw...
sg.beginDefaultPass(state.pass_action, sapp.width(), sapp.height());
sg.beginPass(.{ .action = state.pass_action, .swapchain = sglue.swapchain() });
sg.applyPipeline(state.pip);
sg.applyBindings(state.bind);
sg.applyUniforms(.VS, shd.SLOT_vs_params, sg.asRange(&vs_params));
Expand Down
39 changes: 19 additions & 20 deletions src/examples/mrt.zig
Expand Up @@ -14,7 +14,7 @@ const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const sglue = sokol.glue;
const vec2 = @import("math.zig").Vec2;
const vec3 = @import("math.zig").Vec3;
const mat4 = @import("math.zig").Mat4;
Expand All @@ -25,8 +25,8 @@ const offscreen_sample_count = 1;
const state = struct {
const offscreen = struct {
var pass_action: sg.PassAction = .{};
var pass_desc: sg.PassDesc = .{};
var pass: sg.Pass = .{};
var attachments_desc: sg.AttachmentsDesc = .{};
var attachments: sg.Attachments = .{};
var pip: sg.Pipeline = .{};
var bind: sg.Bindings = .{};
};
Expand All @@ -48,7 +48,7 @@ const state = struct {

export fn init() void {
sg.setup(.{
.context = sgapp.context(),
.environment = sglue.environment(),
.logger = .{ .func = slog.func },
});

Expand All @@ -71,9 +71,8 @@ export fn init() void {
.clear_value = .{ .r = 0, .g = 0, .b = 0.25, .a = 1 },
};

// setup the offscreen render pass and render target images,
// this will also be called when the window resizes
createOffscreenPass(sapp.width(), sapp.height());
// setup the offscreen render pass resources this will also be called when the window resizes
createOffscreenAttachments(sapp.width(), sapp.height());

// create vertex buffer for a cube
const cube_vbuf = sg.makeBuffer(.{
Expand Down Expand Up @@ -171,7 +170,7 @@ export fn init() void {
// offscreen render target textures
state.fsq.bind.vertex_buffers[0] = quad_vbuf;
inline for (.{ 0, 1, 2 }) |i| {
state.fsq.bind.fs.images[i] = state.offscreen.pass_desc.color_attachments[i].image;
state.fsq.bind.fs.images[i] = state.offscreen.attachments_desc.colors[i].image;
}
state.fsq.bind.fs.samplers[0] = smp;

Expand Down Expand Up @@ -204,7 +203,7 @@ export fn frame() void {
};

// render cube into MRT offscreen render targets
sg.beginPass(state.offscreen.pass, state.offscreen.pass_action);
sg.beginPass(.{ .action = state.offscreen.pass_action, .attachments = state.offscreen.attachments });
sg.applyPipeline(state.offscreen.pip);
sg.applyBindings(state.offscreen.bind);
sg.applyUniforms(.VS, shd.SLOT_offscreen_params, sg.asRange(&offscreen_params));
Expand All @@ -213,15 +212,15 @@ export fn frame() void {

// render fullscreen quad with the composed offscreen-render images,
// 3 a small debug view quads at the bottom of the screen
sg.beginDefaultPass(state.default.pass_action, sapp.width(), sapp.height());
sg.beginPass(.{ .action = state.default.pass_action, .swapchain = sglue.swapchain() });
sg.applyPipeline(state.fsq.pip);
sg.applyBindings(state.fsq.bind);
sg.applyUniforms(.VS, shd.SLOT_fsq_params, sg.asRange(&fsq_params));
sg.draw(0, 4, 1);
sg.applyPipeline(state.dbg.pip);
inline for (.{ 0, 1, 2 }) |i| {
sg.applyViewport(i * 100, 0, 100, 100, false);
state.dbg.bind.fs.images[0] = state.offscreen.pass_desc.color_attachments[i].image;
state.dbg.bind.fs.images[0] = state.offscreen.attachments_desc.colors[i].image;
sg.applyBindings(state.dbg.bind);
sg.draw(0, 4, 1);
}
Expand All @@ -235,7 +234,7 @@ export fn cleanup() void {

export fn event(ev: [*c]const sapp.Event) void {
if (ev.*.type == .RESIZED) {
createOffscreenPass(ev.*.framebuffer_width, ev.*.framebuffer_height);
createOffscreenAttachments(ev.*.framebuffer_width, ev.*.framebuffer_height);
}
}

Expand Down Expand Up @@ -265,13 +264,13 @@ fn computeMVP(rx: f32, ry: f32) mat4 {
}

// helper function to create or re-create render target images and pass object for offscreen rendering
fn createOffscreenPass(width: i32, height: i32) void {
fn createOffscreenAttachments(width: i32, height: i32) void {
// destroy previous resources (can be called with invalid ids)
sg.destroyPass(state.offscreen.pass);
for (state.offscreen.pass_desc.color_attachments) |att| {
sg.destroyAttachments(state.offscreen.attachments);
for (state.offscreen.attachments_desc.colors) |att| {
sg.destroyImage(att.image);
}
sg.destroyImage(state.offscreen.pass_desc.depth_stencil_attachment.image);
sg.destroyImage(state.offscreen.attachments_desc.depth_stencil.image);

// create offscreen render target images and pass
const color_img_desc: sg.ImageDesc = .{
Expand All @@ -284,13 +283,13 @@ fn createOffscreenPass(width: i32, height: i32) void {
depth_img_desc.pixel_format = .DEPTH;

inline for (.{ 0, 1, 2 }) |i| {
state.offscreen.pass_desc.color_attachments[i].image = sg.makeImage(color_img_desc);
state.offscreen.attachments_desc.colors[i].image = sg.makeImage(color_img_desc);
}
state.offscreen.pass_desc.depth_stencil_attachment.image = sg.makeImage(depth_img_desc);
state.offscreen.pass = sg.makePass(state.offscreen.pass_desc);
state.offscreen.attachments_desc.depth_stencil.image = sg.makeImage(depth_img_desc);
state.offscreen.attachments = sg.makeAttachments(state.offscreen.attachments_desc);

// update the fullscreen-quad texture bindings
inline for (.{ 0, 1, 2 }) |i| {
state.fsq.bind.fs.images[i] = state.offscreen.pass_desc.color_attachments[i].image;
state.fsq.bind.fs.images[i] = state.offscreen.attachments_desc.colors[i].image;
}
}