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