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_log), with GL5
* entry points supplied by an external loader on both targets.6
*7
* NATIVE (default): SOKOL_GLCORE + SOKOL_EXTERNAL_GL_LOADER. The window and8
* context are sigil-desktop's (GLFW); gl_native_loader.h fills sokol's GL9
* function pointers through sigil_desktop_gl_get_proc_address. No system GL10
* header, no libGL link.11
*12
* WEB (wasm32-wasi, __wasm__): SOKOL_GLES3 + SOKOL_EXTERNAL_GL_LOADER. sokol's13
* gl* calls resolve to WASM imports in the "gl" module via gl_shim.h (shipped14
* by sigil-wasm-gles3). There is no window package on web: the canvas, WebGL215
* context, and swapchain come from the sigil-wasm-gles3 JS bridge (see16
* 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 equal20
* SIGIL_GFX_UNIFORM_BUFFER_SIZE / sizeof(float) in graphics.c, which21
* 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 the23
* 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 as25
* `vec4 palette[8]` beside them. Cost: sokol_gp keeps max_commands26
* (16384) copies of the sgp_uniform struct in its pool, so the pool goes27
* from 4.3 MB to 8.4 MB, allocated and zeroed once in sgp_setup; per draw28
* under a custom shader it memcmps one 512 B struct. Default-pipeline29
* draws (no shader bound) skip the uniform path entirely. */30
#define SGP_UNIFORM_CONTENT_SLOTS 12832
#if defined(__wasm__)34
/* Web (wasm32-wasi): sokol_gfx's GLES3 backend, with GL entry points supplied35
* as WASM imports (SOKOL_EXTERNAL_GL_LOADER) rather than a native loader. These36
* are self-configured here from the wasm target so a first-class `sigil build37
* --config web` needs no -D flags; the #ifndef guards keep the older examples38
* that still pass -DSOKOL_GLES3/-DSOKOL_EXTERNAL_GL_LOADER conflict-free. */39
#ifndef SOKOL_GLES340
#define SOKOL_GLES341
#endif42
#ifndef SOKOL_EXTERNAL_GL_LOADER43
#define SOKOL_EXTERNAL_GL_LOADER44
#endif46
/* Web: bind every gl* sokol calls as a WASM import in the "gl" module. Must be47
* included before sokol_gfx.h so GL types/constants/prototypes are visible. */48
#include "gl_shim.h"50
#define SOKOL_IMPL51
/* 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 web56
* that header is not compiled, so supply it here. Routes to stderr (wasi57
* fd_write -> console) so sokol58
* validation failures stay visible without any import beyond gl.* + wasi. */59
#include <stdio.h>60
#include <stdint.h>61
void 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 : "");71
}73
#else75
/* Native: desktop GL core backend over sigil-desktop's window. GL comes in76
* through the generated loader (gl_native_loader.h: sokol's own list of77
* entry points, filled from sigil_desktop_gl_get_proc_address, which is78
* GLFW's glfwGetProcAddress), so this file needs no system GL header and79
* the package links no libGL; GLFW loads libGL/libEGL at run time. The80
* loader is the same shape as the web build's gl_shim.h, hence81
* SOKOL_EXTERNAL_GL_LOADER on both. */82
#ifndef SOKOL_GLCORE83
#define SOKOL_GLCORE84
#endif85
#ifndef SOKOL_EXTERNAL_GL_LOADER86
#define SOKOL_EXTERNAL_GL_LOADER87
#endif89
#include "gl_native_loader.h"90
#include "sigil-desktop.h"92
/* sokol_log's implementation lives here now (it was in sigil-app when the93
* window was sokol_app); graphics.c declares slog_func through sokol_log.h. */94
#define SOKOL_IMPL95
#include "sokol_log.h"97
/* Order matters: gfx before gp. No sokol_glue: graphics.c builds the98
* 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 of104
* missing ones (and sokol cannot run). */105
int sigil_graphics_load_gl(void)106
{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;110
}112
#endif