├── README.md ├── renderer.cpp ├── renderer.h └── semaphore.h /README.md: -------------------------------------------------------------------------------- 1 | A simple wrapper for sokol_gfx (see https://github.com/floooh/sokol) enabling multithreaded rendering. 2 | 3 | Typically you would use it as follows: 4 | 5 | Render thread 6 | 7 | - create renderer instance, passing sg_desc to initialise sokol graphics 8 | - call renderer->set_default_pass_size() and again whenever back buffer size changes 9 | - call renderer->execute_commands() in render loop 10 | - call renderer->wait_for_flush() on termination 11 | - delete renderer instance 12 | 13 | Update thread 14 | 15 | - call renderer->add_command_xxx() commands in a similar manner to how you would call sg_xxx() commands 16 | - call renderer->commit_commands() when you're done for the frame 17 | - call renderer->flush_commands() on termination, before exiting the thread 18 | 19 | Resources 20 | 21 | When creating or updating resources (buffers, images etc) it is up to the caller to ensure that any pointers passed into an add_command_xxx() call made in the update thread remain valid until the underlying sg_xxx() command has been issued from the render thread. 22 | 23 | To help with this, you can optionally schedule clean-ups via the renderer->schedule_cleanup() function which allows user-provided callbacks to be called after all the commands to create the resources have been executed. 24 | 25 | These cleanup calls can be used to free up the memory or clean-up any objects that own the memory and avoids the need for the wrapper to make any copies of data. For convenience, these calls are also made from the update thread from inside a later commit_commands() call. 26 | -------------------------------------------------------------------------------- /renderer.cpp: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------------------------------- 2 | 3 | #include 4 | 5 | #define SOKOL_IMPL 6 | #define SOKOL_GFX_IMPL 7 | #include "sokol_gfx.h" 8 | #undef SOKOL_GFX_IMPL 9 | 10 | #include "renderer.h" 11 | 12 | // ---------------------------------------------------------------------------------------------------- 13 | 14 | constexpr int32_t INITIAL_NUMBER_OF_COMMANDS = 512; 15 | constexpr int32_t INITIAL_NUMBER_OF_CLEANUPS = 128; 16 | 17 | // ---------------------------------------------------------------------------------------------------- 18 | 19 | RENDERER::RENDERER(const sg_desc& desc) 20 | { 21 | // setup sokol graphics 22 | sg_setup(desc); 23 | 24 | // loop through commands 25 | for (int32_t i = 0; i < 2; i ++) 26 | { 27 | // reserve commands 28 | m_commands[i].reserve(INITIAL_NUMBER_OF_COMMANDS); 29 | } 30 | 31 | // reserve cleamups 32 | m_cleanups.reserve(INITIAL_NUMBER_OF_CLEANUPS); 33 | 34 | // release render semaphore 35 | m_render_semaphore.release(); 36 | } 37 | 38 | // ---------------------------------------------------------------------------------------------------- 39 | 40 | RENDERER::~RENDERER() 41 | { 42 | // process cleanups 43 | process_cleanups(-1); 44 | 45 | // shutdown sokol graphics 46 | sg_shutdown(); 47 | } 48 | 49 | // ---------------------------------------------------------------------------------------------------- 50 | 51 | void RENDERER::execute_commands(bool resource_only) 52 | { 53 | // not flushing? 54 | if (!m_flushing) 55 | { 56 | // acquire update semaphore 57 | m_update_semaphore.acquire(); 58 | } 59 | 60 | { 61 | // lock execute mutex 62 | std::scoped_lock lock(m_execute_mutex); 63 | 64 | // loop through commands 65 | for (const auto& command : m_commands[m_commit_commands_index]) 66 | { 67 | // ignore command? 68 | if (resource_only && !(command.type >= RENDER_COMMAND::TYPE::MAKE_BUFFER && command.type <= RENDER_COMMAND::TYPE::DESTROY_PASS)) 69 | { 70 | continue; 71 | } 72 | 73 | // execute command 74 | switch (command.type) 75 | { 76 | case RENDER_COMMAND::TYPE::PUSH_DEBUG_GROUP: 77 | sg_push_debug_group(command.push_debug_group.name); 78 | break; 79 | case RENDER_COMMAND::TYPE::POP_DEBUG_GROUP: 80 | sg_pop_debug_group(); 81 | break; 82 | case RENDER_COMMAND::TYPE::MAKE_BUFFER: 83 | sg_init_buffer(command.make_buffer.buffer, command.make_buffer.desc); 84 | break; 85 | case RENDER_COMMAND::TYPE::MAKE_IMAGE: 86 | sg_init_image(command.make_image.image, command.make_image.desc); 87 | break; 88 | case RENDER_COMMAND::TYPE::MAKE_SHADER: 89 | sg_init_shader(command.make_shader.shader, command.make_shader.desc); 90 | break; 91 | case RENDER_COMMAND::TYPE::MAKE_PIPELINE: 92 | sg_init_pipeline(command.make_pipeline.pipeline, command.make_pipeline.desc); 93 | break; 94 | case RENDER_COMMAND::TYPE::MAKE_PASS: 95 | sg_init_pass(command.make_pass.pass, command.make_pass.desc); 96 | break; 97 | case RENDER_COMMAND::TYPE::DESTROY_BUFFER: 98 | sg_uninit_buffer(command.destroy_buffer.buffer); 99 | break; 100 | case RENDER_COMMAND::TYPE::DESTROY_IMAGE: 101 | sg_uninit_image(command.destroy_image.image); 102 | break; 103 | case RENDER_COMMAND::TYPE::DESTROY_SHADER: 104 | sg_uninit_shader(command.destroy_shader.shader); 105 | break; 106 | case RENDER_COMMAND::TYPE::DESTROY_PIPELINE: 107 | sg_uninit_pipeline(command.destroy_pipeline.pipeline); 108 | break; 109 | case RENDER_COMMAND::TYPE::DESTROY_PASS: 110 | sg_uninit_pass(command.destroy_pass.pass); 111 | break; 112 | case RENDER_COMMAND::TYPE::UPDATE_BUFFER: 113 | sg_update_buffer(command.update_buffer.buffer, command.update_buffer.data); 114 | break; 115 | case RENDER_COMMAND::TYPE::APPEND_BUFFER: 116 | sg_append_buffer(command.append_buffer.buffer, command.append_buffer.data); 117 | break; 118 | case RENDER_COMMAND::TYPE::UPDATE_IMAGE: 119 | sg_update_image(command.update_image.image, command.update_image.data); 120 | break; 121 | case RENDER_COMMAND::TYPE::BEGIN_DEFAULT_PASS: 122 | sg_begin_default_pass(command.begin_default_pass.pass_action, m_default_pass_width, m_default_pass_height); 123 | break; 124 | case RENDER_COMMAND::TYPE::BEGIN_PASS: 125 | sg_begin_pass(command.begin_pass.pass, command.begin_pass.pass_action); 126 | break; 127 | case RENDER_COMMAND::TYPE::APPLY_VIEWPORT: 128 | sg_apply_viewport(command.apply_viewport.x, command.apply_viewport.y, command.apply_viewport.width, command.apply_viewport.height, command.apply_viewport.origin_top_left); 129 | break; 130 | case RENDER_COMMAND::TYPE::APPLY_SCISSOR_RECT: 131 | sg_apply_scissor_rect(command.apply_scissor_rect.x, command.apply_scissor_rect.y, command.apply_scissor_rect.width, command.apply_scissor_rect.height, command.apply_scissor_rect.origin_top_left); 132 | break; 133 | case RENDER_COMMAND::TYPE::APPLY_PIPELINE: 134 | sg_apply_pipeline(command.apply_pipeline.pipeline); 135 | break; 136 | case RENDER_COMMAND::TYPE::APPLY_BINDINGS: 137 | sg_apply_bindings(command.apply_bindings.bindings); 138 | break; 139 | case RENDER_COMMAND::TYPE::APPLY_UNIFORMS: 140 | sg_apply_uniforms(command.apply_uniforms.stage, command.apply_uniforms.ub_index, { command.apply_uniforms.buf, command.apply_uniforms.data_size }); 141 | break; 142 | case RENDER_COMMAND::TYPE::DRAW: 143 | sg_draw(command.draw.base_element, command.draw.number_of_elements, command.draw.number_of_instances); 144 | break; 145 | case RENDER_COMMAND::TYPE::END_PASS: 146 | sg_end_pass(); 147 | break; 148 | case RENDER_COMMAND::TYPE::COMMIT: 149 | sg_commit(); 150 | break; 151 | case RENDER_COMMAND::TYPE::CUSTOM: 152 | command.custom.custom_cb(command.custom.custom_data); 153 | break; 154 | case RENDER_COMMAND::TYPE::NOT_SET: 155 | break; 156 | } 157 | } 158 | } 159 | 160 | // release render semaphore 161 | m_render_semaphore.release(); 162 | } 163 | 164 | // ---------------------------------------------------------------------------------------------------- 165 | 166 | void RENDERER::wait_for_flush() 167 | { 168 | // initialise finished flushing 169 | bool finished_flushing = false; 170 | 171 | // wait for flush 172 | while (!finished_flushing) 173 | { 174 | // not flushing? 175 | if (!m_flushing) 176 | { 177 | // acquire update semaphore 178 | m_update_semaphore.acquire(); 179 | } 180 | 181 | { 182 | // lock execute mutex 183 | std::scoped_lock lock(m_execute_mutex); 184 | 185 | // loop through commands 186 | for (const auto& command : m_commands[m_commit_commands_index]) 187 | { 188 | // execute command 189 | switch (command.type) 190 | { 191 | case RENDER_COMMAND::TYPE::MAKE_BUFFER: 192 | sg_init_buffer(command.make_buffer.buffer, command.make_buffer.desc); 193 | break; 194 | case RENDER_COMMAND::TYPE::MAKE_IMAGE: 195 | sg_init_image(command.make_image.image, command.make_image.desc); 196 | break; 197 | case RENDER_COMMAND::TYPE::MAKE_SHADER: 198 | sg_init_shader(command.make_shader.shader, command.make_shader.desc); 199 | break; 200 | case RENDER_COMMAND::TYPE::MAKE_PIPELINE: 201 | sg_init_pipeline(command.make_pipeline.pipeline, command.make_pipeline.desc); 202 | break; 203 | case RENDER_COMMAND::TYPE::MAKE_PASS: 204 | sg_init_pass(command.make_pass.pass, command.make_pass.desc); 205 | break; 206 | case RENDER_COMMAND::TYPE::DESTROY_BUFFER: 207 | sg_uninit_buffer(command.destroy_buffer.buffer); 208 | break; 209 | case RENDER_COMMAND::TYPE::DESTROY_IMAGE: 210 | sg_uninit_image(command.destroy_image.image); 211 | break; 212 | case RENDER_COMMAND::TYPE::DESTROY_SHADER: 213 | sg_uninit_shader(command.destroy_shader.shader); 214 | break; 215 | case RENDER_COMMAND::TYPE::DESTROY_PIPELINE: 216 | sg_uninit_pipeline(command.destroy_pipeline.pipeline); 217 | break; 218 | case RENDER_COMMAND::TYPE::DESTROY_PASS: 219 | sg_uninit_pass(command.destroy_pass.pass); 220 | break; 221 | default: 222 | break; 223 | } 224 | } 225 | } 226 | 227 | // udpate finished flushing 228 | finished_flushing = m_flushing; 229 | 230 | // release render semaphore 231 | m_render_semaphore.release(); 232 | } 233 | } 234 | 235 | // ---------------------------------------------------------------------------------------------------- 236 | 237 | void RENDERER::add_command_push_debug_group(const char* name) 238 | { 239 | // add command 240 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::PUSH_DEBUG_GROUP); 241 | 242 | // copy args 243 | command.push_debug_group.name = name; 244 | } 245 | 246 | // ---------------------------------------------------------------------------------------------------- 247 | 248 | void RENDERER::add_command_pop_debug_group() 249 | { 250 | // add command 251 | /*RENDER_COMMAND& command = */m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::POP_DEBUG_GROUP); 252 | } 253 | 254 | // ---------------------------------------------------------------------------------------------------- 255 | 256 | sg_buffer RENDERER::add_command_make_buffer(const sg_buffer_desc& desc) 257 | { 258 | // add command 259 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::MAKE_BUFFER); 260 | 261 | // copy args 262 | command.make_buffer.desc = desc; 263 | 264 | // alloc buffer 265 | command.make_buffer.buffer = sg_alloc_buffer(); 266 | 267 | // return buffer 268 | return command.make_buffer.buffer; 269 | } 270 | 271 | // ---------------------------------------------------------------------------------------------------- 272 | 273 | sg_image RENDERER::add_command_make_image(const sg_image_desc& desc) 274 | { 275 | // add command 276 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::MAKE_IMAGE); 277 | 278 | // copy args 279 | command.make_image.desc = desc; 280 | 281 | // alloc image 282 | command.make_image.image = sg_alloc_image(); 283 | 284 | // return image 285 | return command.make_image.image; 286 | } 287 | 288 | // ---------------------------------------------------------------------------------------------------- 289 | 290 | sg_shader RENDERER::add_command_make_shader(const sg_shader_desc& desc) 291 | { 292 | // add command 293 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::MAKE_SHADER); 294 | 295 | // copy args 296 | command.make_shader.desc = desc; 297 | 298 | // alloc shader 299 | command.make_shader.shader = sg_alloc_shader(); 300 | 301 | // return shader 302 | return command.make_shader.shader; 303 | } 304 | 305 | // ---------------------------------------------------------------------------------------------------- 306 | 307 | sg_pipeline RENDERER::add_command_make_pipeline(const sg_pipeline_desc& desc) 308 | { 309 | // add command 310 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::MAKE_PIPELINE); 311 | 312 | // copy args 313 | command.make_pipeline.desc = desc; 314 | 315 | // alloc pipeline 316 | command.make_pipeline.pipeline = sg_alloc_pipeline(); 317 | 318 | // return pipeline 319 | return command.make_pipeline.pipeline; 320 | } 321 | 322 | // ---------------------------------------------------------------------------------------------------- 323 | 324 | sg_pass RENDERER::add_command_make_pass(const sg_pass_desc& desc) 325 | { 326 | // add command 327 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::MAKE_PASS); 328 | 329 | // copy args 330 | command.make_pass.desc = desc; 331 | 332 | // alloc pass 333 | command.make_pass.pass = sg_alloc_pass(); 334 | 335 | // return pass 336 | return command.make_pass.pass; 337 | } 338 | 339 | // ---------------------------------------------------------------------------------------------------- 340 | 341 | void RENDERER::add_command_destroy_buffer(sg_buffer buffer) 342 | { 343 | // add command 344 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::DESTROY_BUFFER); 345 | 346 | // copy args 347 | command.destroy_buffer.buffer = buffer; 348 | 349 | // schedule cleanup 350 | schedule_cleanup(dealloc_buffer_cb, (void*)(uintptr_t)command.destroy_buffer.buffer.id); 351 | } 352 | 353 | // ---------------------------------------------------------------------------------------------------- 354 | 355 | void RENDERER::add_command_destroy_image(sg_image image) 356 | { 357 | // add command 358 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::DESTROY_IMAGE); 359 | 360 | // copy args 361 | command.destroy_image.image = image; 362 | 363 | // schedule cleanup 364 | schedule_cleanup(dealloc_image_cb, (void*)(uintptr_t)command.destroy_image.image.id); 365 | } 366 | 367 | // ---------------------------------------------------------------------------------------------------- 368 | 369 | void RENDERER::add_command_destroy_shader(sg_shader shader) 370 | { 371 | // add command 372 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::DESTROY_SHADER); 373 | 374 | // copy args 375 | command.destroy_shader.shader = shader; 376 | 377 | // schedule cleanup 378 | schedule_cleanup(dealloc_shader_cb, (void*)(uintptr_t)command.destroy_shader.shader.id); 379 | } 380 | 381 | // ---------------------------------------------------------------------------------------------------- 382 | 383 | void RENDERER::add_command_destroy_pipeline(sg_pipeline pipeline) 384 | { 385 | // add command 386 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::DESTROY_PIPELINE); 387 | 388 | // copy args 389 | command.destroy_pipeline.pipeline = pipeline; 390 | 391 | // schedule cleanup 392 | schedule_cleanup(dealloc_pipeline_cb, (void*)(uintptr_t)command.destroy_pipeline.pipeline.id); 393 | } 394 | 395 | // ---------------------------------------------------------------------------------------------------- 396 | 397 | void RENDERER::add_command_destroy_pass(sg_pass pass) 398 | { 399 | // add command 400 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::DESTROY_PASS); 401 | 402 | // copy args 403 | command.destroy_pass.pass = pass; 404 | 405 | // schedule cleanup 406 | schedule_cleanup(dealloc_pass_cb, (void*)(uintptr_t)command.destroy_pass.pass.id); 407 | } 408 | 409 | // ---------------------------------------------------------------------------------------------------- 410 | 411 | void RENDERER::add_command_update_buffer(sg_buffer buffer, const sg_range& data) 412 | { 413 | // add command 414 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::UPDATE_BUFFER); 415 | 416 | // copy args 417 | command.update_buffer.buffer = buffer; 418 | command.update_buffer.data = data; 419 | } 420 | 421 | // ---------------------------------------------------------------------------------------------------- 422 | 423 | void RENDERER::add_command_append_buffer(sg_buffer buffer, const sg_range& data) 424 | { 425 | // add command 426 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::APPEND_BUFFER); 427 | 428 | // copy args 429 | command.append_buffer.buffer = buffer; 430 | command.append_buffer.data = data; 431 | } 432 | 433 | // ---------------------------------------------------------------------------------------------------- 434 | 435 | void RENDERER::add_command_update_image(sg_image image, const sg_image_data& data) 436 | { 437 | // add command 438 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::UPDATE_IMAGE); 439 | 440 | // copy args 441 | command.update_image.image = image; 442 | command.update_image.data = data; 443 | } 444 | 445 | // ---------------------------------------------------------------------------------------------------- 446 | 447 | void RENDERER::add_command_begin_default_pass(const sg_pass_action& pass_action) 448 | { 449 | // add command 450 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::BEGIN_DEFAULT_PASS); 451 | 452 | // copy args 453 | command.begin_default_pass.pass_action = pass_action; 454 | } 455 | 456 | // ---------------------------------------------------------------------------------------------------- 457 | 458 | void RENDERER::add_command_begin_pass(sg_pass pass, const sg_pass_action& pass_action) 459 | { 460 | // add command 461 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::BEGIN_PASS); 462 | 463 | // copy args 464 | command.begin_pass.pass = pass; 465 | command.begin_pass.pass_action = pass_action; 466 | } 467 | 468 | // ---------------------------------------------------------------------------------------------------- 469 | 470 | void RENDERER::add_command_apply_viewport(int x, int y, int width, int height, bool origin_top_left) 471 | { 472 | // add command 473 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::APPLY_VIEWPORT); 474 | 475 | // copy args 476 | command.apply_viewport.x = x; 477 | command.apply_viewport.y = y; 478 | command.apply_viewport.width = width; 479 | command.apply_viewport.height = height; 480 | command.apply_viewport.origin_top_left = origin_top_left; 481 | } 482 | 483 | // ---------------------------------------------------------------------------------------------------- 484 | 485 | void RENDERER::add_command_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left) 486 | { 487 | // add command 488 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::APPLY_SCISSOR_RECT); 489 | 490 | // copy args 491 | command.apply_scissor_rect.x = x; 492 | command.apply_scissor_rect.y = y; 493 | command.apply_scissor_rect.width = width; 494 | command.apply_scissor_rect.height = height; 495 | command.apply_scissor_rect.origin_top_left = origin_top_left; 496 | } 497 | 498 | // ---------------------------------------------------------------------------------------------------- 499 | 500 | void RENDERER::add_command_apply_pipeline(sg_pipeline pipeline) 501 | { 502 | // add command 503 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::APPLY_PIPELINE); 504 | 505 | // copy args 506 | command.apply_pipeline.pipeline = pipeline; 507 | } 508 | 509 | // ---------------------------------------------------------------------------------------------------- 510 | 511 | void RENDERER::add_command_apply_bindings(const sg_bindings& bindings) 512 | { 513 | // add command 514 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::APPLY_BINDINGS); 515 | 516 | // copy args 517 | command.apply_bindings.bindings = bindings; 518 | } 519 | 520 | // ---------------------------------------------------------------------------------------------------- 521 | 522 | void RENDERER::add_command_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range& data) 523 | { 524 | // data size too big? 525 | if ((size_t)data.size > sizeof(RENDER_COMMAND::apply_uniforms.buf)) 526 | { 527 | return; 528 | } 529 | 530 | // add command 531 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::APPLY_UNIFORMS); 532 | 533 | // copy args 534 | command.apply_uniforms.stage = stage; 535 | command.apply_uniforms.ub_index = ub_index; 536 | memcpy(command.apply_uniforms.buf, data.ptr, data.size); 537 | command.apply_uniforms.data_size = data.size; 538 | } 539 | 540 | // ---------------------------------------------------------------------------------------------------- 541 | 542 | void RENDERER::add_command_draw(int base_element, int number_of_elements, int number_of_instances) 543 | { 544 | // add command 545 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::DRAW); 546 | 547 | // copy args 548 | command.draw.base_element = base_element; 549 | command.draw.number_of_elements = number_of_elements; 550 | command.draw.number_of_instances = number_of_instances; 551 | } 552 | 553 | // ---------------------------------------------------------------------------------------------------- 554 | 555 | void RENDERER::add_command_end_pass() 556 | { 557 | // add command 558 | /*RENDER_COMMAND& command = */m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::END_PASS); 559 | } 560 | 561 | // ---------------------------------------------------------------------------------------------------- 562 | 563 | void RENDERER::add_command_commit() 564 | { 565 | // add command 566 | /*RENDER_COMMAND& command = */m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::COMMIT); 567 | } 568 | 569 | // ---------------------------------------------------------------------------------------------------- 570 | 571 | void RENDERER::add_command_custom(void (*custom_cb)(void* custom_data), void* custom_data) 572 | { 573 | // add command 574 | RENDER_COMMAND& command = m_commands[m_pending_commands_index].emplace_back(RENDER_COMMAND::TYPE::CUSTOM); 575 | 576 | // copy args 577 | command.custom.custom_cb = custom_cb; 578 | command.custom.custom_data = custom_data; 579 | } 580 | 581 | // ---------------------------------------------------------------------------------------------------- 582 | 583 | void RENDERER::schedule_cleanup(void (*cleanup_cb)(void* cleanup_data), void* cleanup_data, int32_t number_of_frames_to_defer) 584 | { 585 | // add cleanup 586 | RENDER_CLEANUP& cleanup = m_cleanups.emplace_back(cleanup_cb, cleanup_data); 587 | 588 | // set frame index 589 | cleanup.frame_index = m_frame_index + 1 + number_of_frames_to_defer; 590 | } 591 | 592 | // ---------------------------------------------------------------------------------------------------- 593 | 594 | void RENDERER::commit_commands() 595 | { 596 | // acquire render semaphore 597 | m_render_semaphore.acquire(); 598 | 599 | // clear commands 600 | m_commands[m_commit_commands_index].resize(0); 601 | 602 | // process cleanups 603 | process_cleanups(m_frame_index); 604 | 605 | // swap commands indexes 606 | std::swap(m_pending_commands_index, m_commit_commands_index); 607 | 608 | // increase frame index 609 | m_frame_index ++; 610 | 611 | // release update semaphore 612 | m_update_semaphore.release(); 613 | } 614 | 615 | // ---------------------------------------------------------------------------------------------------- 616 | 617 | void RENDERER::flush_commands() 618 | { 619 | // acquire render semaphore 620 | m_render_semaphore.acquire(); 621 | 622 | // clear commands 623 | m_commands[m_commit_commands_index].resize(0); 624 | 625 | // swap commands indexes 626 | std::swap(m_pending_commands_index, m_commit_commands_index); 627 | 628 | // set flushing 629 | m_flushing = true; 630 | 631 | // release update semaphore 632 | m_update_semaphore.release(); 633 | } 634 | 635 | // ---------------------------------------------------------------------------------------------------- 636 | 637 | const std::string RENDERER::get_name() const 638 | { 639 | // return name 640 | switch (sg_query_backend()) 641 | { 642 | case SG_BACKEND_GLCORE33: 643 | return "OpenGL 3.3"; 644 | case SG_BACKEND_GLES2: 645 | return "OpenGL ES 2.0"; 646 | case SG_BACKEND_GLES3: 647 | return "OpenGL ES 3.0"; 648 | case SG_BACKEND_D3D11: 649 | return "Direct3D 11"; 650 | case SG_BACKEND_METAL_IOS: 651 | case SG_BACKEND_METAL_MACOS: 652 | case SG_BACKEND_METAL_SIMULATOR: 653 | return "Metal"; 654 | case SG_BACKEND_WGPU: 655 | return "WebGPU"; 656 | default: 657 | return "Unknown"; 658 | } 659 | } 660 | 661 | // ---------------------------------------------------------------------------------------------------- 662 | 663 | void RENDERER::process_cleanups(int32_t frame_index) 664 | { 665 | // loop through cleanups 666 | for (auto& cleanup : m_cleanups) 667 | { 668 | // call cleanup cb? 669 | if ((cleanup.frame_index <= frame_index || frame_index < 0) && cleanup.cleanup_cb) 670 | { 671 | // call cleanup cb 672 | cleanup.cleanup_cb(cleanup.cleanup_data); 673 | 674 | // reset cleanup cb 675 | cleanup.cleanup_cb = nullptr; 676 | } 677 | } 678 | 679 | // erase invalid cleanups 680 | m_cleanups.erase(std::remove_if(m_cleanups.begin(), m_cleanups.end(), [](const auto& cleanup) { return !cleanup.cleanup_cb; }), m_cleanups.end()); 681 | } 682 | -------------------------------------------------------------------------------- /renderer.h: -------------------------------------------------------------------------------- 1 | #ifndef RENDERER_H 2 | #define RENDERER_H 3 | 4 | // ---------------------------------------------------------------------------------------------------- 5 | 6 | #include 7 | #include 8 | 9 | #include "sokol_gfx.h" 10 | 11 | #include "semaphore.h" 12 | 13 | // ---------------------------------------------------------------------------------------------------- 14 | 15 | struct RENDER_COMMAND 16 | { 17 | // types 18 | struct TYPE 19 | { 20 | enum ENUM 21 | { 22 | NOT_SET = 0, 23 | 24 | PUSH_DEBUG_GROUP, 25 | POP_DEBUG_GROUP, 26 | 27 | MAKE_BUFFER, 28 | MAKE_IMAGE, 29 | MAKE_SHADER, 30 | MAKE_PIPELINE, 31 | MAKE_PASS, 32 | DESTROY_BUFFER, 33 | DESTROY_IMAGE, 34 | DESTROY_SHADER, 35 | DESTROY_PIPELINE, 36 | DESTROY_PASS, 37 | UPDATE_BUFFER, 38 | APPEND_BUFFER, 39 | UPDATE_IMAGE, 40 | 41 | BEGIN_DEFAULT_PASS, 42 | BEGIN_PASS, 43 | APPLY_VIEWPORT, 44 | APPLY_SCISSOR_RECT, 45 | APPLY_PIPELINE, 46 | APPLY_BINDINGS, 47 | APPLY_UNIFORMS, 48 | DRAW, 49 | END_PASS, 50 | COMMIT, 51 | 52 | CUSTOM 53 | }; 54 | }; 55 | 56 | RENDER_COMMAND() {} 57 | RENDER_COMMAND(TYPE::ENUM _type) : type(_type) {} 58 | 59 | TYPE::ENUM type = TYPE::NOT_SET; 60 | 61 | union 62 | { 63 | struct 64 | { 65 | const char* name; 66 | } push_debug_group; 67 | 68 | struct 69 | { 70 | sg_buffer_desc desc; 71 | sg_buffer buffer; 72 | } make_buffer; 73 | 74 | struct 75 | { 76 | sg_image_desc desc; 77 | sg_image image; 78 | } make_image; 79 | 80 | struct 81 | { 82 | sg_shader_desc desc; 83 | sg_shader shader; 84 | } make_shader; 85 | 86 | struct 87 | { 88 | sg_pipeline_desc desc; 89 | sg_pipeline pipeline; 90 | } make_pipeline; 91 | 92 | struct 93 | { 94 | sg_pass_desc desc; 95 | sg_pass pass; 96 | } make_pass; 97 | 98 | struct 99 | { 100 | sg_buffer buffer; 101 | } destroy_buffer; 102 | 103 | struct 104 | { 105 | sg_image image; 106 | } destroy_image; 107 | 108 | struct 109 | { 110 | sg_shader shader; 111 | } destroy_shader; 112 | 113 | struct 114 | { 115 | sg_pipeline pipeline; 116 | } destroy_pipeline; 117 | 118 | struct 119 | { 120 | sg_pass pass; 121 | } destroy_pass; 122 | 123 | struct 124 | { 125 | sg_buffer buffer; 126 | sg_range data; 127 | } update_buffer; 128 | 129 | struct 130 | { 131 | sg_buffer buffer; 132 | sg_range data; 133 | } append_buffer; 134 | 135 | struct 136 | { 137 | sg_image image; 138 | sg_image_data data; 139 | } update_image; 140 | 141 | struct 142 | { 143 | void (*custom_cb)(void* custom_data); 144 | void* custom_data; 145 | } custom; 146 | 147 | struct 148 | { 149 | sg_pass_action pass_action; 150 | } begin_default_pass; 151 | 152 | struct 153 | { 154 | sg_pass pass; 155 | sg_pass_action pass_action; 156 | } begin_pass; 157 | 158 | struct 159 | { 160 | int x; 161 | int y; 162 | int width; 163 | int height; 164 | bool origin_top_left; 165 | } apply_viewport; 166 | 167 | struct 168 | { 169 | int x; 170 | int y; 171 | int width; 172 | int height; 173 | bool origin_top_left; 174 | } apply_scissor_rect; 175 | 176 | struct 177 | { 178 | sg_pipeline pipeline; 179 | } apply_pipeline; 180 | 181 | struct 182 | { 183 | sg_bindings bindings; 184 | } apply_bindings; 185 | 186 | struct 187 | { 188 | sg_shader_stage stage; 189 | int ub_index; 190 | size_t data_size; 191 | char buf[512]; 192 | } apply_uniforms; 193 | 194 | struct 195 | { 196 | int base_element; 197 | int number_of_elements; 198 | int number_of_instances; 199 | } draw; 200 | }; 201 | }; 202 | 203 | // ---------------------------------------------------------------------------------------------------- 204 | 205 | typedef std::vector RENDER_COMMAND_ARRAY; 206 | 207 | // ---------------------------------------------------------------------------------------------------- 208 | 209 | struct RENDER_CLEANUP 210 | { 211 | RENDER_CLEANUP() {} 212 | RENDER_CLEANUP(void (*_cleanup_cb)(void* cleanup_data), void* _cleanup_data) : cleanup_cb(_cleanup_cb), cleanup_data(_cleanup_data) {} 213 | 214 | void (*cleanup_cb)(void* cleanup_data) = nullptr; 215 | void* cleanup_data = nullptr; 216 | int32_t frame_index = 0; 217 | }; 218 | 219 | // ---------------------------------------------------------------------------------------------------- 220 | 221 | typedef std::vector RENDER_CLEANUP_ARRAY; 222 | 223 | // ---------------------------------------------------------------------------------------------------- 224 | 225 | class RENDERER 226 | { 227 | public: 228 | RENDERER(const sg_desc& desc); 229 | ~RENDERER(); 230 | 231 | // render thread functions 232 | void execute_commands(bool resource_only = false); 233 | void wait_for_flush(); 234 | 235 | void set_default_pass_size(int width, int height) { m_default_pass_width = width; m_default_pass_height = height; } 236 | 237 | // update thread functions 238 | void add_command_push_debug_group(const char* name); 239 | void add_command_pop_debug_group(); 240 | 241 | sg_buffer add_command_make_buffer(const sg_buffer_desc& desc); 242 | sg_image add_command_make_image(const sg_image_desc& desc); 243 | sg_shader add_command_make_shader(const sg_shader_desc& desc); 244 | sg_pipeline add_command_make_pipeline(const sg_pipeline_desc& desc); 245 | sg_pass add_command_make_pass(const sg_pass_desc& desc); 246 | 247 | void add_command_destroy_buffer(sg_buffer buffer); 248 | void add_command_destroy_image(sg_image image); 249 | void add_command_destroy_shader(sg_shader shader); 250 | void add_command_destroy_pipeline(sg_pipeline pipeline); 251 | void add_command_destroy_pass(sg_pass pass); 252 | 253 | void add_command_update_buffer(sg_buffer buffer, const sg_range& data); 254 | void add_command_append_buffer(sg_buffer buffer, const sg_range& data); 255 | void add_command_update_image(sg_image image, const sg_image_data& data); 256 | 257 | void add_command_begin_default_pass(const sg_pass_action& pass_action); 258 | void add_command_begin_pass(sg_pass pass, const sg_pass_action& pass_action); 259 | void add_command_apply_viewport(int x, int y, int width, int height, bool origin_top_left); 260 | void add_command_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left); 261 | void add_command_apply_pipeline(sg_pipeline pipeline); 262 | void add_command_apply_bindings(const sg_bindings& bindings); 263 | void add_command_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range& data); 264 | void add_command_draw(int base_element, int number_of_elements, int number_of_instances); 265 | void add_command_end_pass(); 266 | void add_command_commit(); 267 | 268 | void add_command_custom(void (*custom_cb)(void* custom_data), void* custom_data); 269 | 270 | void schedule_cleanup(void (*cleanup_cb)(void* cleanup_data), void* cleanup_data, int32_t number_of_frames_to_defer = 0); 271 | 272 | void commit_commands(); 273 | void flush_commands(); 274 | 275 | void lock_execute_mutex() { m_execute_mutex.lock(); } 276 | void unlock_execute_mutex() { m_execute_mutex.unlock(); } 277 | 278 | const std::string get_name() const; 279 | 280 | sg_pixel_format get_pixel_format() const { return sg_query_desc().context.color_format; } 281 | 282 | private: 283 | void process_cleanups(int32_t frame_index); 284 | 285 | static void dealloc_buffer_cb(void* cleanup_data) { sg_dealloc_buffer({(uint32_t)(uintptr_t)cleanup_data}); } 286 | static void dealloc_image_cb(void* cleanup_data) { sg_dealloc_image({(uint32_t)(uintptr_t)cleanup_data}); } 287 | static void dealloc_shader_cb(void* cleanup_data) { sg_dealloc_shader({(uint32_t)(uintptr_t)cleanup_data}); } 288 | static void dealloc_pipeline_cb(void* cleanup_data) { sg_dealloc_pipeline({(uint32_t)(uintptr_t)cleanup_data}); } 289 | static void dealloc_pass_cb(void* cleanup_data) { sg_dealloc_pass({(uint32_t)(uintptr_t)cleanup_data}); } 290 | 291 | RENDER_COMMAND_ARRAY m_commands[2]; 292 | int32_t m_pending_commands_index = 0; 293 | int32_t m_commit_commands_index = 1; 294 | RENDER_CLEANUP_ARRAY m_cleanups; 295 | 296 | SEMAPHORE m_update_semaphore; 297 | SEMAPHORE m_render_semaphore; 298 | std::atomic m_flushing = false; 299 | int m_default_pass_width = 0; 300 | int m_default_pass_height = 0; 301 | std::mutex m_execute_mutex; 302 | int32_t m_frame_index = 0; 303 | }; 304 | 305 | // ---------------------------------------------------------------------------------------------------- 306 | 307 | typedef std::shared_ptr RENDERER_REF; 308 | 309 | #endif 310 | -------------------------------------------------------------------------------- /semaphore.h: -------------------------------------------------------------------------------- 1 | #ifndef SEMAPHORE_H 2 | #define SEMAPHORE_H 3 | 4 | // ---------------------------------------------------------------------------------------------------- 5 | 6 | #include 7 | #include 8 | 9 | // ---------------------------------------------------------------------------------------------------- 10 | 11 | class SEMAPHORE 12 | { 13 | public: 14 | SEMAPHORE() {} 15 | ~SEMAPHORE() {} 16 | 17 | void release() 18 | { 19 | std::scoped_lock lock(m_mutex); 20 | m_count ++; 21 | m_cv.notify_one(); 22 | } 23 | 24 | void acquire() 25 | { 26 | std::unique_lock lock(m_mutex); 27 | while(!m_count) 28 | { 29 | m_cv.wait(lock); 30 | } 31 | m_count --; 32 | } 33 | 34 | private: 35 | uint32_t m_count = 0; 36 | std::mutex m_mutex; 37 | std::condition_variable m_cv; 38 | }; 39 | 40 | #endif 41 | --------------------------------------------------------------------------------