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_log), with GL
5 * entry points supplied by an external loader on both targets.
6 *
7 * NATIVE (default): SOKOL_GLCORE + SOKOL_EXTERNAL_GL_LOADER. The window and
8 * context are sigil-desktop's (GLFW); gl_native_loader.h fills sokol's GL
9 * function pointers through sigil_desktop_gl_get_proc_address. No system GL
10 * header, no libGL link.
11 *
12 * WEB (wasm32-wasi, __wasm__): SOKOL_GLES3 + SOKOL_EXTERNAL_GL_LOADER. sokol's
13 * gl* calls resolve to WASM imports in the "gl" module via gl_shim.h (shipped
14 * by sigil-wasm-gles3). There is no window package on web: the canvas, WebGL2
15 * context, and swapchain come from the sigil-wasm-gles3 JS bridge (see
16 * graphics.c's sig_gfx_* abstraction), and slog_func is provided here.
17 */
19/* sgp's per-draw uniform buffer: 128 floats (512 B). Must equal
20 * SIGIL_GFX_UNIFORM_BUFFER_SIZE / sizeof(float) in graphics.c, which
21 * mirrors the block on the CPU side and asserts nothing larger is pushed.
22 * sokol_gp's default is 8 floats (32 bytes), too small for even the
23 * auto-uniforms (u_time, u_resolution) plus one vec4; 64 floats (v0.10)
24 * held a handful of vec4s; 128 floats holds uniform arrays such as
25 * `vec4 palette[8]` beside them. Cost: sokol_gp keeps max_commands
26 * (16384) copies of the sgp_uniform struct in its pool, so the pool goes
27 * from 4.3 MB to 8.4 MB, allocated and zeroed once in sgp_setup; per draw
28 * under a custom shader it memcmps one 512 B struct. Default-pipeline
29 * draws (no shader bound) skip the uniform path entirely. */
30#define SGP_UNIFORM_CONTENT_SLOTS 128
32#if defined(__wasm__)
34/* Web (wasm32-wasi): sokol_gfx's GLES3 backend, with GL entry points supplied
35 * as WASM imports (SOKOL_EXTERNAL_GL_LOADER) rather than a native loader. These
36 * are self-configured here from the wasm target so a first-class `sigil build
37 * --config web` needs no -D flags; the #ifndef guards keep the older examples
38 * that still pass -DSOKOL_GLES3/-DSOKOL_EXTERNAL_GL_LOADER conflict-free. */
39#ifndef SOKOL_GLES3
40#define SOKOL_GLES3
41#endif
42#ifndef SOKOL_EXTERNAL_GL_LOADER
43#define SOKOL_EXTERNAL_GL_LOADER
44#endif
46/* Web: bind every gl* sokol calls as a WASM import in the "gl" module. Must be
47 * included before sokol_gfx.h so GL types/constants/prototypes are visible. */
48#include "gl_shim.h"
50#define SOKOL_IMPL
51/* Order matters: gfx before gp. No sokol_glue (no sokol_app on web). */
52#include "sokol_gfx.h"
53#include "sokol_gp.h"
55/* slog_func is compiled from sokol_log.h in the native branch below; on web
56 * that header is not compiled, so supply it here. Routes to stderr (wasi
57 * fd_write -> console) so sokol
58 * validation failures stay visible without any import beyond gl.* + wasi. */
59#include <stdio.h>
60#include <stdint.h>
61void slog_func(const char* tag, uint32_t log_level, uint32_t log_item_id,
62 const char* message_or_null, uint32_t line_nr,
63 const char* filename_or_null, void* user_data) {
64 (void)log_item_id; (void)user_data;
65 fprintf(stderr, "sokol[level=%u] %s:%u [%s] %s\n",
66 log_level,
67 filename_or_null ? filename_or_null : "?",
68 line_nr,
69 tag ? tag : "sokol",
70 message_or_null ? message_or_null : "");
73#else
75/* Native: desktop GL core backend over sigil-desktop's window. GL comes in
76 * through the generated loader (gl_native_loader.h: sokol's own list of
77 * entry points, filled from sigil_desktop_gl_get_proc_address, which is
78 * GLFW's glfwGetProcAddress), so this file needs no system GL header and
79 * the package links no libGL; GLFW loads libGL/libEGL at run time. The
80 * loader is the same shape as the web build's gl_shim.h, hence
81 * SOKOL_EXTERNAL_GL_LOADER on both. */
82#ifndef SOKOL_GLCORE
83#define SOKOL_GLCORE
84#endif
85#ifndef SOKOL_EXTERNAL_GL_LOADER
86#define SOKOL_EXTERNAL_GL_LOADER
87#endif
89#include "gl_native_loader.h"
90#include "sigil-desktop.h"
92/* sokol_log's implementation lives here now (it was in sigil-app when the
93 * window was sokol_app); graphics.c declares slog_func through sokol_log.h. */
94#define SOKOL_IMPL
95#include "sokol_log.h"
97/* Order matters: gfx before gp. No sokol_glue: graphics.c builds the
98 * environment and swapchain from sigil-desktop's framebuffer size. */
99#include "sokol_gfx.h"
100#include "sokol_gp.h"
102/* Called by graphics.c before sg_setup, with the window's context current.
103 * Returns 0 when the core entry points are all present, else the count of
104 * missing ones (and sokol cannot run). */
105int sigil_graphics_load_gl(void)
107 int missing = sigil_gl_load(sigil_desktop_gl_get_proc_address);
108 if (!sigil_gl_ready()) return missing > 0 ? missing : 1;
109 return 0;
112#endif