Skip to content

Commit

Permalink
Updated Sokol to the current version. Some issues on macOS though
Browse files Browse the repository at this point in the history
  • Loading branch information
tanis2000 committed Oct 11, 2024
1 parent 8397d46 commit e7da6ec
Show file tree
Hide file tree
Showing 8 changed files with 3,447 additions and 2,141 deletions.
37 changes: 26 additions & 11 deletions src/binocle/core/binocle_gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,15 @@ void binocle_gd_destroy(binocle_gd *gd) {
void binocle_gd_init(binocle_gd *gd, binocle_window *win) {
#if defined(BINOCLE_GL)
sg_desc desc = {
.environment = binocle_window_get_environment(win),
.logger.func = slog_func,
};
#elif defined(BINOCLE_METAL)
binocle_metal_init(win->mtl_view);
sg_environment environment = binocle_window_get_environment(win);
environment.metal.device = binocle_metal_get_device();
sg_desc desc = {
.context = binocle_metal_get_context(),
.environment = environment,
.logger.func = slog_func,
};
#endif
Expand Down Expand Up @@ -182,8 +185,8 @@ void binocle_gd_setup_default_pipeline(binocle_gd *gd, uint32_t offscreen_width,
gd->offscreen.action = offscreen_action;

// Render pass that renders to the offscreen render target
gd->offscreen.pass = sg_make_pass(&(sg_pass_desc){
.color_attachments[0].image = gd->offscreen.render_target,
gd->offscreen.attachments = sg_make_attachments(&(sg_attachments_desc){
.colors[0].image = gd->offscreen.render_target,
.label = "offscreen-pass",
});

Expand Down Expand Up @@ -250,7 +253,7 @@ void binocle_gd_setup_default_pipeline(binocle_gd *gd, uint32_t offscreen_width,
.label = "offscreen-sampler",
.min_filter = SG_FILTER_LINEAR,
.mag_filter = SG_FILTER_LINEAR,
.mipmap_filter = SG_FILTER_NONE,
.mipmap_filter = SG_FILTER_LINEAR,
});

// Render to screen pipeline
Expand Down Expand Up @@ -347,7 +350,7 @@ void binocle_gd_setup_default_pipeline(binocle_gd *gd, uint32_t offscreen_width,
.label = "display-sampler",
.min_filter = SG_FILTER_LINEAR,
.mag_filter = SG_FILTER_LINEAR,
.mipmap_filter = SG_FILTER_NONE,
.mipmap_filter = SG_FILTER_LINEAR,
});
}

Expand Down Expand Up @@ -409,7 +412,10 @@ void binocle_gd_render_offscreen(binocle_gd *gd) {
}
sg_update_buffer(gd->offscreen.vbuf, &(sg_range){ .ptr=gd->vertices, .size=gd->num_vertices * sizeof(binocle_vpct) });

sg_begin_pass(gd->offscreen.pass, &gd->offscreen.action);
sg_begin_pass(&(sg_pass){
.action = gd->offscreen.action,
.attachments = gd->offscreen.attachments,
});

ks_mergesort(binocle_gd_sort_commands, gd->num_commands, gd->commands, 0);

Expand Down Expand Up @@ -465,7 +471,10 @@ void binocle_gd_render_screen(binocle_gd *gd, struct binocle_window *window, flo

gd->display.bind.fs.images[0] = gd->offscreen.render_target;

sg_begin_default_pass(&gd->display.action, window->width, window->height);
sg_begin_pass(&(sg_pass){
.action = gd->display.action,
.swapchain = binocle_window_get_swapchain(window),
});
sg_apply_pipeline(gd->display.pip);
sg_apply_bindings(&gd->display.bind);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, &SG_RANGE(screen_vs_params));
Expand Down Expand Up @@ -540,8 +549,8 @@ void binocle_gd_setup_flat_pipeline(binocle_gd *gd) {
};
gd->flat.action = action;

gd->flat.pass = sg_make_pass(&(sg_pass_desc){
.color_attachments[0].image = gd->offscreen.render_target,
gd->flat.attachments = sg_make_attachments(&(sg_attachments_desc){
.colors[0].image = gd->offscreen.render_target,
});

binocle_log_info("Setting up flat pipeline");
Expand Down Expand Up @@ -788,7 +797,10 @@ void binocle_gd_draw_quad_to_screen(binocle_gd *gd, sg_shader shader, sg_image r

/* default pass action */
sg_pass_action pass_action = { 0 };
sg_begin_pass(gd->display.pass, &pass_action);
sg_begin_pass(&(sg_pass){
.action = pass_action,
.attachments = gd->display.attachments,
});
sg_apply_pipeline(pip);
sg_apply_bindings(&bind);
sg_draw(0, 6, 1);
Expand Down Expand Up @@ -874,7 +886,10 @@ void binocle_gd_render_flat(binocle_gd *gd) {
}
sg_update_buffer(gd->flat.vbuf, &(sg_range){ .ptr=gd->flat_vertices, .size=gd->flat_num_vertices * sizeof(binocle_vpct) });

sg_begin_pass(gd->flat.pass, &gd->flat.action);
sg_begin_pass(&(sg_pass){
.action = gd->flat.action,
.attachments = gd->flat.attachments,
});

for (uint32_t i = 0 ; i < gd->flat_num_commands ; i++) {
binocle_gd_command_t *cmd = &gd->flat_commands[i];
Expand Down
2 changes: 1 addition & 1 deletion src/binocle/core/binocle_gd.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ enum sg_blend_factor;
enum sg_blend_op;

typedef struct binocle_gd_gfx_t {
sg_pass pass;
sg_attachments attachments;
sg_pipeline pip;
sg_bindings bind;
sg_pass_action action;
Expand Down
2 changes: 1 addition & 1 deletion src/binocle/core/binocle_sokol.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#elif defined(__IPHONEOS__) || defined(__ANDROID__)
#define SOKOL_GLES3
#else
#define SOKOL_GLCORE33
#define SOKOL_GLCORE
#endif
#elif defined(BINOCLE_METAL)
#define SOKOL_METAL
Expand Down
3 changes: 2 additions & 1 deletion src/binocle/core/binocle_sokol.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "sokol_gfx.h"
#include "sokol_log.h"
void binocle_metal_init(void *mtl_view);
sg_context_desc binocle_metal_get_context(void);
// sg_context_desc binocle_metal_get_context(void);
const void* binocle_metal_get_device(void);
GLuint binocle_sokol_tex_id(sg_image img_id);
#endif
26 changes: 15 additions & 11 deletions src/binocle/core/binocle_sokol.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#if defined(__IPHONEOS__) || defined(__ANDROID__)
#define SOKOL_GLES3
#else
#define SOKOL_GLCORE33
#define SOKOL_GLCORE
#endif
#elif defined(BINOCLE_METAL)
#define SOKOL_METAL
Expand Down Expand Up @@ -44,16 +44,20 @@ void binocle_metal_init(void *mtl_view) {
return cached_drawable_hack;
}

sg_context_desc binocle_metal_get_context(void) {
return (sg_context_desc) {
.sample_count = 1,
.metal = {
.device = (__bridge const void*) device,
.renderpass_descriptor_userdata_cb = binocle_sokol_mtk_get_render_pass_descriptor,
.drawable_userdata_cb = binocle_sokol_mtk_get_drawable,
.user_data = (void*)metal_layer,
}
};
//sg_context_desc binocle_metal_get_context(void) {
// return (sg_context_desc) {
// .sample_count = 1,
// .metal = {
// .device = (__bridge const void*) device,
// .renderpass_descriptor_userdata_cb = binocle_sokol_mtk_get_render_pass_descriptor,
// .drawable_userdata_cb = binocle_sokol_mtk_get_drawable,
// .user_data = (void*)metal_layer,
// }
// };
//}

const void* binocle_metal_get_device(void) {
return (__bridge const void*) device;
}

GLuint binocle_sokol_tex_id(sg_image img_id) {
Expand Down
25 changes: 25 additions & 0 deletions src/binocle/core/binocle_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ binocle_window *binocle_window_new(uint32_t width, uint32_t height, char *title)
res->is_fullscreen = false;
res->current_frame_delta = 0;
res->framerate_timer = binocle_timer_new();
res->sample_count = 0;
res->has_depth_buffer = true;

// Default to 60 FPS
binocle_window_set_target_fps(res, 60);
Expand Down Expand Up @@ -349,4 +351,27 @@ bool binocle_window_get_display_size(uint32_t *w, uint32_t *h) {
}
}
return false;
}

sg_environment binocle_window_get_environment(binocle_window *window) {
return (sg_environment) {
.defaults = {
.color_format = SG_PIXELFORMAT_RGBA8,
.depth_format = window->has_depth_buffer ? SG_PIXELFORMAT_NONE : SG_PIXELFORMAT_DEPTH_STENCIL,
.sample_count = window->sample_count,
},
};
}
sg_swapchain binocle_window_get_swapchain(binocle_window *window) {
return (sg_swapchain) {
.width = window->width,
.height = window->height,
.sample_count = window->sample_count,
.color_format = SG_PIXELFORMAT_RGBA8,
.depth_format = window->has_depth_buffer ? SG_PIXELFORMAT_NONE : SG_PIXELFORMAT_DEPTH_STENCIL,
.gl = {
// we just assume here that the GL framebuffer is always 0
.framebuffer = 0,
}
};
}
17 changes: 17 additions & 0 deletions src/binocle/core/binocle_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ typedef struct binocle_window {


SDL_Window *window;

bool has_depth_buffer;
uint32_t sample_count;
} binocle_window;

/**
Expand Down Expand Up @@ -190,4 +193,18 @@ void binocle_window_get_real_size(binocle_window *win, uint32_t *w, uint32_t *h)
*/
bool binocle_window_get_display_size(uint32_t *w, uint32_t *h);

/**
* \brief Build the environment to pass to Sokol
* @param window a pointer to the window
* @return the environment
*/
sg_environment binocle_window_get_environment(binocle_window *window);

/**
* \brief Build the swapchain data to pass to Sokol
* @param window a pointer to the window
* @return the swapchain data
*/
sg_swapchain binocle_window_get_swapchain(binocle_window *window);

#endif //BINOCLE_WINDOW_H
Loading

0 comments on commit e7da6ec

Please sign in to comment.