1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
|
# Sokol
**Sokol (Сокол)**: Russian for Falcon, a smaller and more nimble
bird of prey than the Eagle (Орёл, Oryol)
Minimalistic header-only cross-platform libs in C:
- **sokol\_gfx.h**: 3D-API wrapper (GL + Metal + D3D11)
- **sokol\_app.h**: app framework wrapper (entry + window + 3D-context + input)
- **sokol\_time.h**: time measurement
- **sokol\_audio.h**: minimal buffer-streaming audio playback
- **sokol\_args.h**: unified cmdline/URL arg parser for web and native apps
These are (mainly) the internal parts of the Oryol C++ framework
rewritten in pure C as standalone header-only libs.
WebAssembly is a 'first-class citizen', one important motivation for the
Sokol headers is to provide a collection of cross-platform APIs with a
minimal footprint on the web platform while still being useful.
All headers are standalone and can be used indepedendently from each other.
Sample code is in a separate repo: https://github.com/floooh/sokol-samples
asm.js/wasm live demos: https://floooh.github.io/sokol-html5/index.html
Tiny 8-bit emulators: https://floooh.github.io/tiny8bit/
Nim bindings: https://github.com/floooh/sokol-nim
Nim samples: https://github.com/floooh/sokol-nim-samples
### Why C:
- easier integration with other languages
- easier integration into other projects
- allows even smaller program binaries than Oryol
Sokol will be a bit less convenient to use than Oryol, but that's ok since
the Sokol headers are intended to be low-level building blocks.
Eventually Oryol will just be a thin C++ layer over Sokol.
A blog post with more background info: [A Tour of sokol_gfx.h](http://floooh.github.io/2017/07/29/sokol-gfx-tour.html)
# sokol_gfx.h:
- simple, modern wrapper around GLES2/WebGL, GLES3/WebGL2, GL3.3, D3D11 and Metal
- buffers, images, shaders, pipeline-state-objects and render-passes
- does *not* handle window creation or 3D API context initialization
- does *not* provide shader dialect cross-translation
A triangle in C99 with GLFW and FlextGL:
```c
#define GLFW_INCLUDE_NONE
#include "GLFW/glfw3.h"
#include "flextgl/flextGL.h"
#define SOKOL_IMPL
#define SOKOL_GLCORE33
#include "sokol_gfx.h"
int main() {
/* create window and GL context via GLFW */
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* w = glfwCreateWindow(640, 480, "Sokol Triangle GLFW", 0, 0);
glfwMakeContextCurrent(w);
glfwSwapInterval(1);
flextInit();
/* setup sokol_gfx */
sg_setup(&(sg_desc){0});
/* a vertex buffer */
const float vertices[] = {
// positions // colors
0.0f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f
};
sg_buffer vbuf = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(vertices),
.content = vertices,
});
/* a shader */
sg_shader shd = sg_make_shader(&(sg_shader_desc){
.vs.source =
"#version 330\n"
"in vec4 position;\n"
"in vec4 color0;\n"
"out vec4 color;\n"
"void main() {\n"
" gl_Position = position;\n"
" color = color0;\n"
"}\n",
.fs.source =
"#version 330\n"
"in vec4 color;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = color;\n"
"}\n"
});
/* a pipeline state object (default render states are fine for triangle) */
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.shader = shd,
.layout = {
.attrs = {
[0] = { .name="position", .format=SG_VERTEXFORMAT_FLOAT3 },
[1] = { .name="color0", .format=SG_VERTEXFORMAT_FLOAT4 }
}
}
});
/* resource bindings */
sg_bindings binds = {
.vertex_buffers[0] = vbuf
};
/* default pass action (clear to grey) */
sg_pass_action pass_action = {0};
/* draw loop */
while (!glfwWindowShouldClose(w)) {
int cur_width, cur_height;
glfwGetFramebufferSize(w, &cur_width, &cur_height);
sg_begin_default_pass(&pass_action, cur_width, cur_height);
sg_apply_pipeline(pip);
sg_apply_bindings(&binds);
sg_draw(0, 3, 1);
sg_end_pass();
sg_commit();
glfwSwapBuffers(w);
glfwPollEvents();
}
/* cleanup */
sg_shutdown();
glfwTerminate();
return 0;
}
```
# sokol_app.h
A minimal cross-platform application-wrapper library:
- unified application entry
- single window or canvas for 3D rendering
- 3D context initialization
- event-based keyboard, mouse and touch input
- supported platforms: Win32, MacOS, Linux (X11), iOS, WASM/asm.js, Android (planned: RaspberryPi)
- supported 3D-APIs: GL3.3 (GLX/WGL), Metal, D3D11, GLES2/WebGL, GLES3/WebGL2
A simple clear-loop sample using sokol_app.h and sokol_gfx.h (does not include
separate sokol.c/.m implementation file which is necessary
to split the Objective-C code from the C code of the sample):
```cpp
#include "sokol_gfx.h"
#include "sokol_app.h"
sg_pass_action pass_action;
void init(void) {
sg_setup(&(sg_desc){
.mtl_device = sapp_metal_get_device(),
.mtl_renderpass_descriptor_cb = sapp_metal_get_renderpass_descriptor,
.mtl_drawable_cb = sapp_metal_get_drawable,
.d3d11_device = sapp_d3d11_get_device(),
.d3d11_device_context = sapp_d3d11_get_device_context(),
.d3d11_render_target_view_cb = sapp_d3d11_get_render_target_view,
.d3d11_depth_stencil_view_cb = sapp_d3d11_get_depth_stencil_view
});
pass_action = (sg_pass_action) {
.colors[0] = { .action=SG_ACTION_CLEAR, .val={1.0f, 0.0f, 0.0f, 1.0f} }
};
}
void frame(void) {
float g = pass_action.colors[0].val[1] + 0.01f;
pass_action.colors[0].val[1] = (g > 1.0f) ? 0.0f : g;
sg_begin_default_pass(&pass_action, sapp_width(), sapp_height());
sg_end_pass();
sg_commit();
}
void cleanup(void) {
sg_shutdown();
}
sapp_desc sokol_main(int argc, char* argv[]) {
return (sapp_desc){
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.width = 400,
.height = 300,
.window_title = "Clear (sokol app)",
};
}
```
# sokol_audio.h
A minimal audio-streaming API:
- you provide a mono- or stereo-stream of 32-bit float samples which sokol_audio.h forwards into platform-specific backends
- two ways to provide the data:
1. directly fill backend audio buffer from your callback function running in the audio thread
2. alternatively push small packets of audio data from your main loop,
or a separate thread created by you
- platform backends:
- Windows: WASAPI
- macOS/iOS: CoreAudio
- Linux: ALSA
- emscripten: WebAudio + ScriptProcessorNode (doesn't use the emscripten-provided OpenAL or SDL Audio wrappers)
A simple mono square-wave generator using the callback model:
```cpp
// the sample callback, running in audio thread
static void stream_cb(float* buffer, int num_frames, int num_channels) {
assert(1 == num_channels);
static uint32_t count = 0;
for (int i = 0; i < num_frames; i++) {
buffer[i] = (count++ & (1<<3)) ? 0.5f : -0.5f;
}
}
int main() {
// init sokol-audio with default params
saudio_setup(&(saudio_desc){
.stream_cb = stream_cb
});
// run main loop
...
// shutdown sokol-audio
saudio_shutdown();
return 0;
```
The same code using the push-model
```cpp
#define BUF_SIZE (32)
int main() {
// init sokol-audio with default params, no callback
saudio_setup(&(saudio_desc){0});
assert(saudio_channels() == 1);
// a small intermediate buffer so we don't need to push
// individual samples, which would be quite inefficient
float buf[BUF_SIZE];
int buf_pos = 0;
uint32_t count = 0;
// push samples from main loop
bool done = false;
while (!done) {
// generate and push audio samples...
int num_frames = saudio_expect();
for (int i = 0; i < num_frames; i++) {
// simple square wave generator
buf[buf_pos++] = (count++ & (1<<3)) ? 0.5f : -0.5f;
if (buf_pos == BUF_SIZE) {
buf_pos = 0;
saudio_push(buf, BUF_SIZE);
}
}
// handle other per-frame stuff...
...
}
// shutdown sokol-audio
saudio_shutdown();
return 0;
}
```
# sokol_time.h:
Simple cross-platform time measurement:
```c
#include "sokol_time.h"
...
/* initialize sokol_time */
stm_setup();
/* take start timestamp */
uint64_t start = stm_now();
...some code to measure...
/* compute elapsed time */
uint64_t elapsed = stm_since(start);
/* convert to time units */
double seconds = stm_sec(elapsed);
double milliseconds = stm_ms(elapsed);
double microseconds = stm_us(elapsed);
double nanoseconds = stm_ns(elapsed);
/* difference between 2 time stamps */
uint64_t start = stm_now();
...
uint64_t end = stm_now();
uint64_t elapsed = stm_diff(end, start);
/* compute a 'lap time' (e.g. for fps) */
uint64_t last_time = 0;
while (!done) {
...render something...
double frame_time_ms = stm_ms(stm_laptime(&last_time));
}
```
# sokol_args.h
Unified argument parsing for web and native apps. Uses argc/argv on native
platforms and the URL query string on the web.
Example URL with one arg:
https://floooh.github.io/tiny8bit/kc85.html?type=kc85_4
The same as command line app:
> kc85 type=kc85_4
Parsed like this:
```c
#include "sokol_args.h"
int main(int argc, char* argv[]) {
sargs_setup(&(sargs_desc){ .argc=argc, .argv=argv });
if (sargs_exists("type")) {
if (sargs_equals("type", "kc85_4")) {
// start as KC85/4
}
else if (sargs_equals("type", "kc85_3")) {
// start as KC85/3
}
else {
// start as KC85/2
}
}
sargs_shutdown();
return 0;
}
```
See the sokol_args.h header for a more complete documentation, and the [Tiny
Emulators](https://floooh.github.io/tiny8bit/) for more interesting usage examples.
# Overview of planned features
A list of things I'd like to do next:
## sokol_gfx.h planned features:
- 2 small additions to the per-pool-slot generation counters in sokol_gfx.h:
- an sg_setup() option to disable a pool slot when it's generation
counter overflows, this makes the dangling-check for resource ids
watertight at the cost that the pool will run out of slots at some point
- instead of the 16/16-bit split in the resource ids for unique-tag vs
slot-index, only use as many bits as necessary for the slot-index
(based on the number of slots in the pool), and the remaining bits
for the unique-tag
## sokol_app.h planned features:
Mainly some "missing features" for desktop apps:
- allow 'programmatic quit' requested by the application
- allow to intercept the window close button, so that the app can show
a 'do you really want to quit?' dialog box
- define an application icon
- change the window title on existing window
- allow to programmatically activate and deactivate fullscreen
- pointer lock
- show/hide mouse cursor
- allow to change mouse cursor image (at first only switch between system-provided standard images)
## sokol_audio.h planned features:
- implement an alternative WebAudio backend using Audio Worklets and WASM threads
## Potential new sokol headers:
- system clipboard support
- query filesystem standard locations
- simple file access API (at least async file/URL loading)
- gamepad support
- simple cross-platform touch gesture recognition
# Updates
- **21-Feb-2019**: sokol_app.h and sokol_audio.h now have an alternative
set of callbacks with user_data arguments. This is useful if you don't
want or cannot store your own application state in global variables.
See the header documentation in sokol_app.h and sokol_audio.h for details,
and check out the samples *sapp/noentry-sapp.c* and *sapp/modplay-sapp.c*
in https://github.com/floooh/sokol-samples
- **19-Feb-2019**: sokol_app.h now has an alternative mode where it doesn't
"hijack" the platform's main() function. Search for SOKOL_NO_ENTRY in
sokol_app.h for details and documentation.
- **26-Jan-2019**: sokol_app.h now has an Android backend contributed by
[Gustav Olsson](https://github.com/gustavolsson)!
See the [sokol-samples readme](https://github.com/floooh/sokol-samples/blob/master/README.md)
for build instructions.
- **21-Jan-2019**: sokol_gfx.h - pool-slot-generation-counters and a dummy backend:
- Resource pool slots now have a generation-counter for the resource-id
unique-tag, instead of a single counter for the whole pool. This allows
to create many more unique handles.
- sokol_gfx.h now has a dummy backend, activated by defining SOKOL_DUMMY_BACKEND
(instead of SOKOL_METAL, SOKOL_D3D11, ...), this allows to write
'headless' tests (and there's now a sokol-gfx-test in the sokol-samples
repository which mainly tests the resource pool system)
- **12-Jan-2019**: sokol_gfx.h - setting the pipeline state and resource
bindings now happens in separate calls, specifically:
- *sg_apply_draw_state()* has been replaced with *sg_apply_pipeline()* and
*sg_apply_bindings()*
- the *sg_draw_state* struct has been replaced with *sg_bindings*
- *sg_apply_uniform_block()* has been renamed to *sg_apply_uniforms()*
All existing code will still work. See [this blog
post](https://floooh.github.io/2019/01/12/sokol-apply-pipeline.html) for
details.
- **29-Oct-2018**:
- sokol_gfx.h has a new function **sg_append_buffer()** which allows to
append new data to a buffer multiple times per frame and interleave this
with draw calls. This basically implements the
D3D11_MAP_WRITE_NO_OVERWRITE update strategy for buffer objects. For
example usage, see the updated Dear ImGui samples in the [sokol_samples
repo](https://github.com/floooh/sokol-samples)
- the GL state cache in sokol_gfx.h handles buffers bindings in a
more robust way, previously it might have happened that the
buffer binding gets confused when creating buffers or updating
buffer contents in the render loop
- **17-Oct-2018**: sokol_args.h added
- **26-Sep-2018**: sokol_audio.h ready for prime time :)
- **11-May-2018**: sokol_gfx.h now autodetects iOS vs MacOS in the Metal
backend during compilation using the standard define TARGET_OS_IPHONE defined
in the TargetConditionals.h system header, please replace the old
backend-selection defines SOKOL_METAL_MACOS and SOKOL_METAL_IOS with
**SOKOL_METAL**
- **20-Apr-2018**: 3 new context-switching functions have been added
to make it possible to use sokol together with applications that
use multiple GL contexts. On D3D11 and Metal, the functions are currently
empty. See the new section ```WORKING WITH CONTEXTS``` in the sokol_gfx.h
header documentation, and the new sample [multiwindow-glfw](https://github.com/floooh/sokol-samples/blob/master/glfw/multiwindow-glfw.c)
- **31-Jan-2018**: The vertex layout declaration in sg\_pipeline\_desc had
some fairly subtle flaws and has been changed to work like Metal or Vulkan.
The gist is that the vertex-buffer-layout properties (vertex stride,
vertex-step-rate and -step-function for instancing) is now defined in a
separate array from the vertex attributes. This removes some brittle backend
code which tries to guess the right vertex attribute slot if no attribute
names are given, and which was wrong for shader-code-generation pipelines
which reorder the vertex attributes (I stumbled over this when porting the
Oryol Gfx module over to sokol-gfx). Some code samples:
```c
// a complete vertex layout declaration with a single input buffer
// with two vertex attributes
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.buffers = {
[0] = {
.stride = 20,
.step_func = SG_VERTEXSTEP_PER_VERTEX,
.step_rate = 1
}
},
.attrs = {
[0] = {
.name = "pos",
.offset = 0,
.format = SG_VERTEXFORMAT_FLOAT3,
.buffer_index = 0
},
[1] = {
.name = "uv",
.offset = 12,
.format = SG_VERTEXFORMAT_FLOAT2,
.buffer_index = 0
}
}
},
...
});
// if the vertex layout has no gaps, we can get rid of the strides and offsets:
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.buffers = {
[0] = {
.step_func = SG_VERTEXSTEP_PER_VERTEX,
.step_rate=1
}
},
.attrs = {
[0] = {
.name = "pos",
.format = SG_VERTEXFORMAT_FLOAT3,
.buffer_index = 0
},
[1] = {
.name = "uv",
.format = SG_VERTEXFORMAT_FLOAT2,
.buffer_index = 0
}
}
},
...
});
// we can also get rid of the other default-values, which leaves buffers[0]
// as all-defaults, so it can disappear completely:
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.attrs = {
[0] = { .name = "pos", .format = SG_VERTEXFORMAT_FLOAT3 },
[1] = { .name = "uv", .format = SG_VERTEXFORMAT_FLOAT2 }
}
},
...
});
// and finally on GL3.3 and Metal and we don't need the attribute names
// (on D3D11, a semantic name and index must be provided though)
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.attrs = {
[0] = { .format = SG_VERTEXFORMAT_FLOAT3 },
[1] = { .format = SG_VERTEXFORMAT_FLOAT2 }
}
},
...
});
```
Please check the sample code in https://github.com/floooh/sokol-samples for
more examples!
Enjoy!
|