Skip to content

Commit

Permalink
cleanup (ref: floooh/sokol#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Feb 25, 2024
1 parent e9865d4 commit 7eed2a9
Show file tree
Hide file tree
Showing 20 changed files with 2,160 additions and 2,055 deletions.
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
csrc_root ++ "sokol_gl.c",
csrc_root ++ "sokol_debugtext.c",
csrc_root ++ "sokol_shape.c",
csrc_root ++ "sokol_glue.c",
};
for (csources) |csrc| {
lib.addCSourceFile(.{
Expand Down
9 changes: 5 additions & 4 deletions src/examples/blend.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import sg = sokol.gfx;
import app = sokol.app;
import log = sokol.log;
import handmade.math : Mat4, Vec3;
import sgapp = sokol.glue;
import sglue = sokol.glue;
import sgutil = sokol.utils : asRange;
import shd = shaders.blend;

Expand All @@ -20,7 +20,7 @@ immutable NUM_BLEND_FACTORS = 15;

struct State
{
float r;
float r = 0.0f;
sg.Pipeline bg_pip;
sg.Pipeline[NUM_BLEND_FACTORS][NUM_BLEND_FACTORS] pips;
sg.Bindings bind;
Expand All @@ -41,7 +41,7 @@ void init() @trusted
{
sg.Desc gfx = {
pipeline_pool_size: NUM_BLEND_FACTORS * NUM_BLEND_FACTORS + 1,
context: sgapp.context(),
environment: sglue.environment,
logger: {func: &log.func}
};
sg.setup(gfx);
Expand Down Expand Up @@ -108,7 +108,8 @@ 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.beginDefaultPass(state.passAction, app.width(), app.height());
sg.Pass pass = {action: state.passAction, swapchain: sglue.swapchain};
sg.beginPass(pass);

sg.Range r = sgutil.asRange(state.bg_fs_params);
sg.applyPipeline(state.bg_pip);
Expand Down
11 changes: 7 additions & 4 deletions src/examples/clear.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module examples.clear;

import sg = sokol.gfx;
import sgapp = sokol.glue;
import sglue = sokol.glue;
import sapp = sokol.app;
import log = sokol.log;

Expand All @@ -16,8 +16,10 @@ __gshared sg.PassAction pass_action;

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

pass_action.colors[0].load_action = sg.LoadAction.Clear;
Expand All @@ -44,7 +46,8 @@ void frame()
{
const g = pass_action.colors[0].clear_value.g + 0.01;
pass_action.colors[0].clear_value.g = g > 1.0 ? 0.0 : g;
sg.beginDefaultPass(pass_action, sapp.width(), sapp.height());
sg.Pass pass = {action: pass_action, swapchain: sglue.swapchain};
sg.beginPass(pass);
sg.endPass();
sg.commit();
}
Expand Down
7 changes: 4 additions & 3 deletions src/examples/cube.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import sg = sokol.gfx;
import app = sokol.app;
import log = sokol.log;
import handmade.math : Mat4, Vec3;
import sgapp = sokol.glue;
import sglue = sokol.glue;
import shd = shaders.cube;
import sgutil = sokol.utils : asRange;

Expand Down Expand Up @@ -43,7 +43,7 @@ static State state;

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

Expand Down Expand Up @@ -120,7 +120,8 @@ void frame()

shd.VsParams vsParams = {mvp: computeVsParams(state.rx, state.ry)};

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

sg.Range r = sgutil.asRange(vsParams);
sg.applyPipeline(state.pip);
Expand Down
5 changes: 3 additions & 2 deletions src/examples/debugtext_print.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct State

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

Expand Down Expand Up @@ -88,7 +88,8 @@ void frame()
print_font(FONT_C64, "C64:\n", 0x79, 0x86, 0xcb);
print_font(FONT_ORIC, "Oric Atmos:\n", 0xff, 0x98, 0x00);

sg.beginDefaultPass(state.passAction, sapp.width(), sapp.height());
sg.Pass pass = {action: state.passAction, swapchain: sglue.swapchain};
sg.beginPass(pass);
sdtx.draw();
sg.endPass();
sg.commit();
Expand Down
38 changes: 20 additions & 18 deletions src/examples/mrt.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import sg = sokol.gfx;
import app = sokol.app;
import log = sokol.log;
import handmade.math : Mat4, Vec3, Vec2, sin, cos;
import sgapp = sokol.glue;
import sglue = sokol.glue;
import shd = shaders.mrt;
import sgutil = sokol.utils : asRange;

Expand All @@ -27,8 +27,8 @@ enum OFFSCREEN_SAMPLE_COUNT = 1;
struct Offscreen
{
sg.PassAction pass_action;
sg.PassDesc pass_desc;
sg.Pass pass;
sg.AttachmentsDesc atts_desc;
sg.Attachments atts;
sg.Pipeline pip;
sg.Bindings bind;
}
Expand Down Expand Up @@ -65,7 +65,7 @@ static State state;

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

Expand Down Expand Up @@ -95,7 +95,7 @@ void init() @trusted

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

float[96] VERTICES = [
// positions brightness
Expand Down Expand Up @@ -201,7 +201,7 @@ void init() @trusted
state.fsq.bind.vertex_buffers[0] = quad_vbuf;
foreach (i; [0, 1, 2])
{
state.fsq.bind.fs.images[i] = state.offscreen.pass_desc.color_attachments[i].image;
state.fsq.bind.fs.images[i] = state.offscreen.atts_desc.colors[i].image;
}
state.fsq.bind.fs.samplers[0] = smp;

Expand Down Expand Up @@ -233,7 +233,8 @@ void frame()
};

// render cube into MRT offscreen render targets
sg.beginPass(state.offscreen.pass, state.offscreen.pass_action);
sg.Pass pass_atts = {action: state.offscreen.pass_action, attachments: state.offscreen.atts};
sg.beginPass(pass_atts);
sg.applyPipeline(state.offscreen.pip);
sg.applyBindings(state.offscreen.bind);
auto offs_rg = sgutil.asRange(offscreen_params);
Expand All @@ -247,7 +248,8 @@ void frame()

// render fullscreen quad with the composed offscreen-render images,
// 3 a small debug view quads at the bottom of the screen
sg.beginDefaultPass(state.dflt.pass_action, app.width(), app.height());
sg.Pass pass_swap = {action: state.dflt.pass_action, swapchain: sglue.swapchain};
sg.beginPass(pass_swap);
sg.applyPipeline(state.fsq.pip);
sg.applyBindings(state.fsq.bind);
auto fsq_rg = sgutil.asRange(fsq_params);
Expand All @@ -257,7 +259,7 @@ void frame()
foreach (i; [0, 1, 2])
{
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.atts_desc.colors[i].image;
sg.applyBindings(state.dbg.bind);
sg.draw(0, 4, 1);
}
Expand All @@ -269,7 +271,7 @@ void event(const app.Event* ev)
{
if (ev.type == app.EventType.Resized)
{
createOffscreenPass(ev.framebuffer_width, ev.framebuffer_height);
createOffscreenAttachments(ev.framebuffer_width, ev.framebuffer_height);
}
}

Expand All @@ -295,15 +297,15 @@ void main()
app.run(runner);
}

void createOffscreenPass(int width, int height)
void createOffscreenAttachments(int width, int height)
{
// destroy previous resources (can be called with invalid ids)
sg.destroyPass(state.offscreen.pass);
foreach (att; state.offscreen.pass_desc.color_attachments)
sg.destroyAttachments(state.offscreen.atts);
foreach (att; state.offscreen.atts_desc.colors)
{
sg.destroyImage(att.image);
}
sg.destroyImage(state.offscreen.pass_desc.depth_stencil_attachment.image);
sg.destroyImage(state.offscreen.atts_desc.depth_stencil.image);

// create offscreen render target images and pass
sg.ImageDesc color_img_desc = {
Expand All @@ -317,15 +319,15 @@ void createOffscreenPass(int width, int height)

foreach (i; [0, 1, 2])
{
state.offscreen.pass_desc.color_attachments[i].image = sg.makeImage(color_img_desc);
state.offscreen.atts_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.atts_desc.depth_stencil.image = sg.makeImage(depth_img_desc);
state.offscreen.atts = sg.makeAttachments(state.offscreen.atts_desc);

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

}
Expand Down
11 changes: 7 additions & 4 deletions src/examples/saudio.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module examples.saudio;
import sg = sokol.gfx;
import app = sokol.app;
import log = sokol.log;
import sgapp = sokol.glue;
import sglue = sokol.glue;
import saudio = sokol.audio;

extern (C):
Expand All @@ -34,8 +34,10 @@ static State state;

void init()
{
sg.Desc gfx = {context: sgapp.context(),
logger: {func: &log.slog_func}};
sg.Desc gfx = {
environment: sglue.environment,
logger: {func: &log.slog_func}
};
sg.setup(gfx);
saudio.Desc audio = {logger: {func: &log.slog_func}};
saudio.setup(audio);
Expand All @@ -59,7 +61,8 @@ void frame()
state.samples[state.sample_pos] = (0 != (state.even_odd & 0x20)) ? 0.1 : -0.1;
}

sg.beginDefaultPass(state.pass_action, app.width(), app.height());
sg.Pass pass = {action: state.pass_action, swapchain: sglue.swapchain};
sg.beginPass(pass);
sg.endPass();
sg.commit();
}
Expand Down
24 changes: 15 additions & 9 deletions src/examples/sgl_context.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module examples.sgl_context;

import sg = sokol.gfx;
import sgapp = sokol.glue;
import sglue = sokol.glue;
import sapp = sokol.app;
import slog = sokol.log;
import sgl = sokol.gl;
Expand All @@ -19,7 +19,7 @@ extern (C):
struct Offscreen
{
sg.PassAction pass_action;
sg.Pass pass;
sg.Attachments attachments;
sg.Image img;
sgl.Context sgl_ctx;
}
Expand All @@ -44,7 +44,7 @@ enum offscreen_height = 32;

void init()
{
sg.Desc gfxd = {context: sgapp.context(),
sg.Desc gfxd = {environment: sglue.environment,
logger: {func: &slog.func}};
sg.setup(gfxd);

Expand Down Expand Up @@ -85,7 +85,7 @@ void init()
};
state.offscreen.sgl_ctx = sgl.makeContext(ctd);

// create an offscreen render target texture, pass and pass-action
// create an offscreen render target texture, pass-attachments object and pass-action
sg.ImageDesc imgd = {
render_target: true,
width: offscreen_width,
Expand All @@ -95,9 +95,9 @@ void init()
};
state.offscreen.img = sg.makeImage(imgd);

sg.PassDesc pass_desc;
pass_desc.color_attachments[0].image = state.offscreen.img;
state.offscreen.pass = sg.makePass(pass_desc);
sg.AttachmentsDesc attachmentDesc;
attachmentDesc.colors[0].image = state.offscreen.img;
state.offscreen.attachments = sg.makeAttachments(attachmentDesc);

state.offscreen.pass_action.colors[0].load_action = sg.LoadAction.Clear;
state.offscreen.pass_action.colors[0].clear_value.r = 0.0;
Expand Down Expand Up @@ -141,10 +141,16 @@ void frame()
draw_cube();

// do the actual offscreen and display rendering in sokol-gfx passes
sg.beginPass(state.offscreen.pass, state.offscreen.pass_action);
sg.Pass pass1 = {
action: state.offscreen.pass_action, attachments: state.offscreen.attachments
};
sg.beginPass(pass1);
sgl.contextDraw(state.offscreen.sgl_ctx);
sg.endPass();
sg.beginDefaultPass(state.display.pass_action, sapp.width(), sapp.height());
sg.Pass pass2 = {
action: state.display.pass_action, swapchain: sglue.swapchain
};
sg.beginPass(pass2);
sgl.contextDraw(sgl.defaultContext());
sg.endPass();
sg.commit();
Expand Down
7 changes: 4 additions & 3 deletions src/examples/sgl_points.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
module examples.sgl_points;

import sg = sokol.gfx;
import sgapp = sokol.glue;
import sglue = sokol.glue;
import sapp = sokol.app;
import slog = sokol.log;
import sgl = sokol.gl;
Expand Down Expand Up @@ -56,7 +56,7 @@ immutable float[3][16] pallete = [

void init()
{
sg.Desc gfxd = {context: sgapp.context(),
sg.Desc gfxd = {environment: sglue.environment,
logger: {func: &slog.func}};
sg.setup(gfxd);

Expand Down Expand Up @@ -88,7 +88,8 @@ void frame()
}
sgl.end();

sg.beginDefaultPass(state.pass_action, sapp.width, sapp.height);
sg.Pass pass = {action: state.pass_action, swapchain: sglue.swapchain};
sg.beginPass(pass);
sgl.draw();
sg.endPass();
sg.commit();
Expand Down
Loading

0 comments on commit 7eed2a9

Please sign in to comment.