AtlatestRepositorysigil-graphics
sigil-graphics / tree / src / csokol-graphics.c
1
/*2
* sokol-graphics.c - Sokol implementation for sigil-graphics3
*4
* Implements sokol_gfx and sokol_gp (and, on native, sokol_glue).5
*6
* NATIVE (default): SOKOL_GLCORE. sokol_glue bridges sokol_app (implemented in7
* sigil-app) to sokol_gfx; sokol_app.h is included for declarations only, and8
* slog_func comes from sigil-app.9
*10
* WEB (wasm32-wasi, __wasm__): SOKOL_GLES3 + SOKOL_EXTERNAL_GL_LOADER (set via11
* the web build's -D flags). sokol's gl* calls resolve to WASM imports in the12
* "gl" module via gl_shim.h (shipped by sigil-wasm-gles3). There is no13
* sokol_app / sokol_glue on web — the canvas, WebGL2 context, and swapchain come14
* from the sigil-wasm-gles3 JS bridge (see graphics.c's sig_gfx_* abstraction),15
* so those headers are excluded and slog_func is provided here since sigil-app16
* is not linked on web.17
*/19
/* Bump sgp's uniform-block buffer to 64 floats (256 bytes) so custom shaders20
* have room for the auto-uniforms (u_time, u_resolution) plus a reasonable21
* budget of caller-set uniforms. The default 8 floats (32 bytes) doesn't even22
* fit the auto-uniforms plus a single vec4. */23
#define SGP_UNIFORM_CONTENT_SLOTS 6425
#if defined(__wasm__)27
/* Web (wasm32-wasi): sokol_gfx's GLES3 backend, with GL entry points supplied28
* as WASM imports (SOKOL_EXTERNAL_GL_LOADER) rather than a native loader. These29
* are self-configured here from the wasm target so a first-class `sigil build30
* --config web` needs no -D flags; the #ifndef guards keep the older examples31
* that still pass -DSOKOL_GLES3/-DSOKOL_EXTERNAL_GL_LOADER conflict-free. */32
#ifndef SOKOL_GLES333
#define SOKOL_GLES334
#endif35
#ifndef SOKOL_EXTERNAL_GL_LOADER36
#define SOKOL_EXTERNAL_GL_LOADER37
#endif39
/* Web: bind every gl* sokol calls as a WASM import in the "gl" module. Must be40
* included before sokol_gfx.h so GL types/constants/prototypes are visible. */41
#include "gl_shim.h"43
#define SOKOL_IMPL44
/* Order matters: gfx before gp. No sokol_glue (no sokol_app on web). */45
#include "sokol_gfx.h"46
#include "sokol_gp.h"48
/* slog_func is provided by sigil-app on native; on web sigil-app is not linked,49
* so supply it here. Routes to stderr (wasi fd_write -> console) so sokol50
* validation failures stay visible without any import beyond gl.* + wasi. */51
#include <stdio.h>52
#include <stdint.h>53
void slog_func(const char* tag, uint32_t log_level, uint32_t log_item_id,54
const char* message_or_null, uint32_t line_nr,55
const char* filename_or_null, void* user_data) {56
(void)log_item_id; (void)user_data;57
fprintf(stderr, "sokol[level=%u] %s:%u [%s] %s\n",58
log_level,59
filename_or_null ? filename_or_null : "?",60
line_nr,61
tag ? tag : "sokol",62
message_or_null ? message_or_null : "");63
}65
#else67
/* Native: desktop GL core backend. Self-configured here (was previously set via68
* a -DSOKOL_GLCORE compiler flag; moved into the source so one build task serves69
* both native and web). #ifndef-guarded so a build/example still passing70
* -DSOKOL_GLCORE is conflict-free. Behaviour is identical: SOKOL_GLCORE ends up71
* defined either way. */72
#ifndef SOKOL_GLCORE73
#define SOKOL_GLCORE74
#endif76
/* Include sokol_app declarations WITHOUT implementation (implemented in sigil-app) */77
#include "sokol_app.h"79
/* Now define SOKOL_IMPL for the headers we implement */80
#define SOKOL_IMPL82
/* Order matters: gfx before glue, glue before gp */83
#include "sokol_gfx.h"84
#include "sokol_glue.h"85
#include "sokol_gp.h"86
/* slog_func is implemented in sigil-app's archive (sigil-app's87
* sokol-app.c picks up SOKOL_LOG_IMPL via SOKOL_IMPL). graphics.c88
* just declares it via sokol_log.h. */90
#endif