AtlatestRepositorysigil-graphics

sigil-graphics / tree / src / csokol-graphics.c

1/*
2 * sokol-graphics.c - Sokol implementation for sigil-graphics
3 *
4 * Implements sokol_gfx and sokol_gp (and, on native, sokol_glue).
5 *
6 * NATIVE (default): SOKOL_GLCORE. sokol_glue bridges sokol_app (implemented in
7 * sigil-app) to sokol_gfx; sokol_app.h is included for declarations only, and
8 * slog_func comes from sigil-app.
9 *
10 * WEB (wasm32-wasi, __wasm__): SOKOL_GLES3 + SOKOL_EXTERNAL_GL_LOADER (set via
11 * the web build's -D flags). sokol's gl* calls resolve to WASM imports in the
12 * "gl" module via gl_shim.h (shipped by sigil-wasm-gles3). There is no
13 * sokol_app / sokol_glue on web — the canvas, WebGL2 context, and swapchain come
14 * 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-app
16 * is not linked on web.
17 */
19/* Bump sgp's uniform-block buffer to 64 floats (256 bytes) so custom shaders
20 * have room for the auto-uniforms (u_time, u_resolution) plus a reasonable
21 * budget of caller-set uniforms. The default 8 floats (32 bytes) doesn't even
22 * fit the auto-uniforms plus a single vec4. */
23#define SGP_UNIFORM_CONTENT_SLOTS 64
25#if defined(__wasm__)
27/* Web (wasm32-wasi): sokol_gfx's GLES3 backend, with GL entry points supplied
28 * as WASM imports (SOKOL_EXTERNAL_GL_LOADER) rather than a native loader. These
29 * are self-configured here from the wasm target so a first-class `sigil build
30 * --config web` needs no -D flags; the #ifndef guards keep the older examples
31 * that still pass -DSOKOL_GLES3/-DSOKOL_EXTERNAL_GL_LOADER conflict-free. */
32#ifndef SOKOL_GLES3
33#define SOKOL_GLES3
34#endif
35#ifndef SOKOL_EXTERNAL_GL_LOADER
36#define SOKOL_EXTERNAL_GL_LOADER
37#endif
39/* Web: bind every gl* sokol calls as a WASM import in the "gl" module. Must be
40 * included before sokol_gfx.h so GL types/constants/prototypes are visible. */
41#include "gl_shim.h"
43#define SOKOL_IMPL
44/* 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 sokol
50 * validation failures stay visible without any import beyond gl.* + wasi. */
51#include <stdio.h>
52#include <stdint.h>
53void 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 : "");
65#else
67/* Native: desktop GL core backend. Self-configured here (was previously set via
68 * a -DSOKOL_GLCORE compiler flag; moved into the source so one build task serves
69 * both native and web). #ifndef-guarded so a build/example still passing
70 * -DSOKOL_GLCORE is conflict-free. Behaviour is identical: SOKOL_GLCORE ends up
71 * defined either way. */
72#ifndef SOKOL_GLCORE
73#define SOKOL_GLCORE
74#endif
76/* 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_IMPL
82/* 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's
87 * sokol-app.c picks up SOKOL_LOG_IMPL via SOKOL_IMPL). graphics.c
88 * just declares it via sokol_log.h. */
90#endif