Skip to content

Commit

Permalink
fix metal samples
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Feb 13, 2024
1 parent 5689985 commit 1cf0a0e
Show file tree
Hide file tree
Showing 21 changed files with 109 additions and 91 deletions.
2 changes: 1 addition & 1 deletion metal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fips_begin_app(blend-metal windowed)
fips_end_app()

fips_begin_app(inject-metal windowed)
fips_files(inject-metal.mm)
fips_files(inject-metal.m)
fips_deps(osxentry)
fips_end_app()

Expand Down
4 changes: 2 additions & 2 deletions metal/arraytex-metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef struct {
static void init(void) {
// setup sokol
sg_setup(&(sg_desc){
.context = osx_get_context(),
.environment = osx_get_environment(),
.logger.func = slog_func,
});

Expand Down Expand Up @@ -219,7 +219,7 @@ static void frame() {
state.frame_index++;

// render the frame
sg_begin_default_pass(&state.pass_action, osx_width(), osx_height());
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = osx_get_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params));
Expand Down
4 changes: 2 additions & 2 deletions metal/binshader-metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ typedef struct {

static void init(void) {
sg_setup(&(sg_desc){
.context = osx_get_context(),
.environment = osx_get_environment(),
.logger.func = slog_func,
});

Expand Down Expand Up @@ -129,7 +129,7 @@ static void frame() {
hmm_mat4 model = HMM_MultiplyMat4(rxm, rym);
vs_params.mvp = HMM_MultiplyMat4(state.view_proj, model);

sg_begin_default_pass(&state.pass_action, osx_width(), osx_height());
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = osx_get_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params));
Expand Down
4 changes: 2 additions & 2 deletions metal/blend-metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct {
static void init(void) {
sg_setup(&(sg_desc){
.pipeline_pool_size = NUM_BLEND_FACTORS * NUM_BLEND_FACTORS + 1,
.context = osx_get_context(),
.environment = osx_get_environment(),
.logger.func = slog_func,
});

Expand Down Expand Up @@ -183,7 +183,7 @@ static void init(void) {
}

static void frame(void) {
sg_begin_default_pass(&(sg_pass_action){0}, osx_width(), osx_height());
sg_begin_pass(&(sg_pass){ .swapchain = osx_get_swapchain() });

/* draw a background quad */
sg_apply_pipeline(state.bg_pip);
Expand Down
4 changes: 2 additions & 2 deletions metal/bufferoffsets-metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static struct {

static void init(void) {
sg_setup(&(sg_desc){
.context = osx_get_context(),
.environment = osx_get_environment(),
.logger.func = slog_func,
});

Expand Down Expand Up @@ -94,7 +94,7 @@ static void init(void) {
}

static void frame(void) {
sg_begin_default_pass(&state.pass_action, osx_width(), osx_height());
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = osx_get_swapchain() });
sg_apply_pipeline(state.pip);
// render the triangle
state.bind.vertex_buffer_offsets[0] = 0;
Expand Down
4 changes: 2 additions & 2 deletions metal/clear-metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static sg_pass_action pass_action;

static void init(void) {
sg_setup(&(sg_desc) {
.context = osx_get_context(),
.environment = osx_get_environment(),
.logger.func = slog_func,
});

Expand All @@ -26,7 +26,7 @@ static void frame(void) {
pass_action.colors[0].clear_value.g = g;

// draw one frame
sg_begin_default_pass(&pass_action, osx_width(), osx_height());
sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = osx_get_swapchain() });
sg_end_pass();
sg_commit();
}
Expand Down
4 changes: 2 additions & 2 deletions metal/cube-metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typedef struct {
static void init(void) {
// setup sokol
sg_setup(&(sg_desc){
.context = osx_get_context(),
.environment = osx_get_environment(),
.logger.func = slog_func,
});

Expand Down Expand Up @@ -146,7 +146,7 @@ static void frame(void) {
hmm_mat4 model = HMM_MultiplyMat4(rxm, rym);
vs_params.mvp = HMM_MultiplyMat4(state.view_proj, model);

sg_begin_default_pass(&state.pass_action, osx_width(), osx_height());
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = osx_get_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params));
Expand Down
4 changes: 2 additions & 2 deletions metal/dyntex-metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static void game_of_life_update();
static void init(void) {
// setup sokol_gfx
sg_setup(&(sg_desc){
.context = osx_get_context(),
.environment = osx_get_environment(),
.logger.func = slog_func,
});

Expand Down Expand Up @@ -190,7 +190,7 @@ static void frame(void) {
});

// render the frame
sg_begin_default_pass(&state.pass_action, osx_width(), osx_height());
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = osx_get_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params));
Expand Down
11 changes: 6 additions & 5 deletions metal/imgui-metal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ static sg_pass_action pass_action;

void init() {
// setup sokol_gfx and sokol_time
sg_desc desc = {
.context = osx_get_context(),
const sg_desc desc = {
.environment = osx_get_environment(),
.logger = {
.func = slog_func,
}
};
sg_setup(&desc);
stm_setup();
simgui_desc_t simgui_desc = { };
simgui_desc.logger.func = slog_func;
const simgui_desc_t simgui_desc = {
.logger.func = slog_func,
};
simgui_setup(&simgui_desc);

// setup the imgui environment
Expand Down Expand Up @@ -104,7 +105,7 @@ void frame() {
}

// the sokol draw pass
sg_begin_default_pass(&pass_action, width, height);
sg_begin_pass({ .action = pass_action, .swapchain = osx_get_swapchain() });
simgui_render();
sg_end_pass();
sg_commit();
Expand Down
24 changes: 11 additions & 13 deletions metal/inject-metal.mm → metal/inject-metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@

static void init(void) {
// setup sokol_gfx
sg_desc desc = {
.context = osx_get_context(),
sg_setup(&(sg_desc){
.environment = osx_get_environment(),
.logger = {
.func = slog_func,
}
};
sg_setup(&desc);
});

// create native Metal vertex- and index-buffer
float vertices[] = {
Expand Down Expand Up @@ -94,14 +93,14 @@ static void init(void) {
sg_reset_state_cache();

// create sokol_gfx buffers with injected Metal buffer objects
sg_buffer_desc vbuf_desc = {
const sg_buffer_desc vbuf_desc = {
.size = sizeof(vertices),
.mtl_buffers[0] = (__bridge const void*) mtl_vbuf
};
state.bind.vertex_buffers[0] = sg_make_buffer(&vbuf_desc);
assert(((__bridge id<MTLBuffer>) sg_mtl_query_buffer_info(state.bind.vertex_buffers[0]).buf[0]) == mtl_vbuf);
assert(((__bridge id<MTLBuffer>) sg_mtl_query_buffer_info(state.bind.vertex_buffers[0]).buf[1]) == nil);
sg_buffer_desc ibuf_desc = {
const sg_buffer_desc ibuf_desc = {
.type = SG_BUFFERTYPE_INDEXBUFFER,
.size = sizeof(indices),
.mtl_buffers[0] = (__bridge const void*) mtl_ibuf
Expand Down Expand Up @@ -146,20 +145,19 @@ static void init(void) {
mtl_smp_desc.sAddressMode = MTLSamplerAddressModeRepeat;
id<MTLSamplerState> mtl_smp = [osx_mtl_device() newSamplerStateWithDescriptor:mtl_smp_desc];

sg_sampler_desc smp_desc = {
const sg_sampler_desc smp_desc = {
.min_filter = SG_FILTER_LINEAR,
.mag_filter = SG_FILTER_LINEAR,
.wrap_u = SG_WRAP_REPEAT,
.wrap_v = SG_WRAP_REPEAT,
.mtl_sampler = mtl_smp,
};

sg_reset_state_cache();
state.bind.fs.samplers[0] = sg_make_sampler(&smp_desc);
assert(((__bridge id<MTLSamplerState>) sg_mtl_query_sampler_info(state.bind.fs.samplers[0]).smp) == mtl_smp);

// a shader
sg_shader_desc shader_desc = {
const sg_shader_desc shader_desc = {
.vs = {
.uniform_blocks[0].size = sizeof(vs_params_t),
.entry = "vs_main",
Expand Down Expand Up @@ -210,7 +208,7 @@ static void init(void) {
assert(((__bridge id<MTLFunction>) sg_mtl_query_shader_info(shd).fs_func) != nil);

// a pipeline state object
sg_pipeline_desc pip_desc = {
const sg_pipeline_desc pip_desc = {
.layout = {
.attrs = {
[0] = { .format=SG_VERTEXFORMAT_FLOAT3 },
Expand Down Expand Up @@ -255,12 +253,12 @@ static void frame(void) {
}
}
state.counter++;
sg_update_image(state.bind.fs.images[0], { .subimage[0][0] = SG_RANGE(state.pixels) });
sg_update_image(state.bind.fs.images[0], &(sg_image_data){ .subimage[0][0] = SG_RANGE(state.pixels) });

sg_begin_default_pass(&state.pass_action, osx_width(), osx_height());
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = osx_get_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE(vs_params));
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params));
sg_draw(0, 36, 1);
sg_end_pass();
sg_commit();
Expand Down
4 changes: 2 additions & 2 deletions metal/instancing-metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef struct {
static void init(void) {
// setup sokol_gfx
sg_setup(&(sg_desc){
.context = osx_get_context(),
.environment = osx_get_environment(),
.logger.func = slog_func,
});

Expand Down Expand Up @@ -179,7 +179,7 @@ static void frame(void) {
vs_params.mvp = HMM_MultiplyMat4(state.view_proj, HMM_Rotate(state.ry, HMM_Vec3(0.0f, 1.0f, 0.0f)));;

// ...and draw
sg_begin_default_pass(&state.pass_action, osx_width(), osx_height());
sg_begin_pass(&(sg_pass){ .action = state.pass_action, .swapchain = osx_get_swapchain() });
sg_apply_pipeline(state.pip);
sg_apply_bindings(&state.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(vs_params));
Expand Down
8 changes: 4 additions & 4 deletions metal/mipmap-metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef struct {
static void init(void) {
// setup sokol
sg_setup(&(sg_desc){
.context = osx_get_context(),
.environment = osx_get_environment(),
.logger.func = slog_func,
});

Expand Down Expand Up @@ -131,8 +131,8 @@ static void init(void) {
// the last 4 samplers use different anistropy levels
smp_desc.min_lod = 0.0f;
smp_desc.max_lod = 0.0f; // for max_lod, zero-initialized means "FLT_MAX"
smp_desc.min_filter = SG_FILTER_NEAREST;
smp_desc.mag_filter = SG_FILTER_NEAREST;
smp_desc.min_filter = SG_FILTER_LINEAR;
smp_desc.mag_filter = SG_FILTER_LINEAR;
smp_desc.mipmap_filter = SG_FILTER_LINEAR;
for (int i = 0; i < 4; i++) {
smp_desc.max_anisotropy = 1<<i;
Expand Down Expand Up @@ -213,7 +213,7 @@ static void frame(void) {
.vertex_buffers[0] = state.vbuf,
.fs.images[0] = state.img,
};
sg_begin_default_pass(&(sg_pass_action){ }, osx_width(), osx_height());
sg_begin_pass(&(sg_pass){ .swapchain = osx_get_swapchain() });
sg_apply_pipeline(state.pip);
for (int i = 0; i < 12; i++) {
const float x = ((float)(i & 3) - 1.5f) * 2.0f;
Expand Down
34 changes: 20 additions & 14 deletions metal/mrt-metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#define SAMPLE_COUNT (4)

static struct {
sg_pass_desc offscreen_pass_desc;
sg_attachments_desc offscreen_attachments_desc;
sg_pass_action offscreen_pass_action;
sg_pass_action default_pass_action;
sg_pass offscreen_pass;
sg_attachments offscreen_attachments;
sg_pipeline offscreen_pip;
sg_bindings offscreen_bind;
sg_pipeline fsq_pip;
Expand All @@ -44,7 +44,7 @@ typedef struct {
static void init(void) {
/* setup sokol */
sg_setup(&(sg_desc){
.context = osx_get_context(),
.environment = osx_get_environment(),
.logger.func = slog_func,
});

Expand All @@ -59,20 +59,20 @@ static void init(void) {
resolve_img_desc.sample_count = 1;
sg_image_desc depth_img_desc = color_img_desc;
depth_img_desc.pixel_format = SG_PIXELFORMAT_DEPTH;
state.offscreen_pass_desc = (sg_pass_desc){
.color_attachments = {
state.offscreen_attachments_desc = (sg_attachments_desc){
.colors = {
[0].image = sg_make_image(&color_img_desc),
[1].image = sg_make_image(&color_img_desc),
[2].image = sg_make_image(&color_img_desc)
},
.resolve_attachments = {
.resolves = {
[0].image = sg_make_image(&resolve_img_desc),
[1].image = sg_make_image(&resolve_img_desc),
[2].image = sg_make_image(&resolve_img_desc)
},
.depth_stencil_attachment.image = sg_make_image(&depth_img_desc)
.depth_stencil.image = sg_make_image(&depth_img_desc)
};
state.offscreen_pass = sg_make_pass(&state.offscreen_pass_desc);
state.offscreen_attachments = sg_make_attachments(&state.offscreen_attachments_desc);

// a matching pass action with clear colors
state.offscreen_pass_action = (sg_pass_action){
Expand Down Expand Up @@ -287,9 +287,9 @@ static void init(void) {
state.fsq_bind = (sg_bindings) {
.vertex_buffers[0] = quad_vbuf,
.fs = {
.images[0] = state.offscreen_pass_desc.resolve_attachments[0].image,
.images[1] = state.offscreen_pass_desc.resolve_attachments[1].image,
.images[2] = state.offscreen_pass_desc.resolve_attachments[2].image,
.images[0] = state.offscreen_attachments_desc.resolves[0].image,
.images[1] = state.offscreen_attachments_desc.resolves[1].image,
.images[2] = state.offscreen_attachments_desc.resolves[2].image,
.samplers[0] = smp
}
};
Expand Down Expand Up @@ -372,15 +372,21 @@ static void frame(void) {
params.offset = HMM_Vec2(HMM_SinF(state.rx*0.01f)*0.1f, HMM_SinF(state.ry*0.01f)*0.1f);

// render cube into MRT offscreen render targets
sg_begin_pass(state.offscreen_pass, &state.offscreen_pass_action);
sg_begin_pass(&(sg_pass){
.action = state.offscreen_pass_action,
.attachments = state.offscreen_attachments
});
sg_apply_pipeline(state.offscreen_pip);
sg_apply_bindings(&state.offscreen_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(offscreen_params));
sg_draw(0, 36, 1);
sg_end_pass();

// render fullscreen quad with the 'composed image'
sg_begin_default_pass(&state.default_pass_action, osx_width(), osx_height());
sg_begin_pass(&(sg_pass){
.action = state.default_pass_action,
.swapchain = osx_get_swapchain()
});
sg_apply_pipeline(state.fsq_pip);
sg_apply_bindings(&state.fsq_bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(params));
Expand All @@ -390,7 +396,7 @@ static void frame(void) {
sg_apply_pipeline(state.dbg_pip);
for (int i = 0; i < 3; i++) {
sg_apply_viewport(i*100, 0, 100, 100, false);
state.dbg_bind.fs.images[0] = state.offscreen_pass_desc.resolve_attachments[i].image;
state.dbg_bind.fs.images[0] = state.offscreen_attachments_desc.resolves[i].image;
sg_apply_bindings(&state.dbg_bind);
sg_draw(0, 4, 1);
}
Expand Down
Loading

0 comments on commit 1cf0a0e

Please sign in to comment.