| 1 | #pragma once |
| 2 | /* |
| 3 | sokol_gfx.h -- simple 3D API wrapper |
| 4 | |
| 5 | Project URL: https://github.com/floooh/sokol |
| 6 | |
| 7 | Do this: |
| 8 | #define SOKOL_IMPL |
| 9 | before you include this file in *one* C or C++ file to create the |
| 10 | implementation. |
| 11 | |
| 12 | In the same place define one of the following to select the rendering |
| 13 | backend: |
| 14 | #define SOKOL_GLCORE33 |
| 15 | #define SOKOL_GLES2 |
| 16 | #define SOKOL_GLES3 |
| 17 | #define SOKOL_D3D11 |
| 18 | #define SOKOL_METAL |
| 19 | #define SOKOL_DUMMY_BACKEND |
| 20 | |
| 21 | I.e. for the GL 3.3 Core Profile it should look like this: |
| 22 | |
| 23 | #include ... |
| 24 | #include ... |
| 25 | #define SOKOL_IMPL |
| 26 | #define SOKOL_GLCORE33 |
| 27 | #include "sokol_gfx.h" |
| 28 | |
| 29 | The dummy backend replaces the platform-specific backend code with empty |
| 30 | stub functions. This is useful for writing tests that need to run on the |
| 31 | command line. |
| 32 | |
| 33 | Optionally provide the following defines with your own implementations: |
| 34 | |
| 35 | SOKOL_ASSERT(c) - your own assert macro (default: assert(c)) |
| 36 | SOKOL_MALLOC(s) - your own malloc function (default: malloc(s)) |
| 37 | SOKOL_FREE(p) - your own free function (default: free(p)) |
| 38 | SOKOL_LOG(msg) - your own logging function (default: puts(msg)) |
| 39 | SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false)) |
| 40 | SOKOL_API_DECL - public function declaration prefix (default: extern) |
| 41 | SOKOL_API_IMPL - public function implementation prefix (default: -) |
| 42 | SOKOL_TRACE_HOOKS - enable trace hook callbacks (search below for TRACE HOOKS) |
| 43 | |
| 44 | If sokol_gfx.h is compiled as a DLL, define the following before |
| 45 | including the declaration or implementation: |
| 46 | |
| 47 | SOKOL_DLL |
| 48 | |
| 49 | On Windows, SOKOL_DLL will define SOKOL_API_DECL as __declspec(dllexport) |
| 50 | or __declspec(dllimport) as needed. |
| 51 | |
| 52 | If you want to compile without deprecated structs and functions, |
| 53 | define: |
| 54 | |
| 55 | SOKOL_NO_DEPRECATED |
| 56 | |
| 57 | API usage validation macros: |
| 58 | |
| 59 | SOKOL_VALIDATE_BEGIN() - begin a validation block (default:_sg_validate_begin()) |
| 60 | SOKOL_VALIDATE(cond, err) - like assert but for API validation (default: _sg_validate(cond, err)) |
| 61 | SOKOL_VALIDATE_END() - end a validation block, return true if all checks in block passed (default: bool _sg_validate()) |
| 62 | |
| 63 | If you don't want validation errors to be fatal, define SOKOL_VALIDATE_NON_FATAL, |
| 64 | be aware though that this may spam SOKOL_LOG messages. |
| 65 | |
| 66 | Optionally define the following to force debug checks and validations |
| 67 | even in release mode: |
| 68 | |
| 69 | SOKOL_DEBUG - by default this is defined if _DEBUG is defined |
| 70 | |
| 71 | |
| 72 | sokol_gfx DOES NOT: |
| 73 | =================== |
| 74 | - create a window or the 3D-API context/device, you must do this |
| 75 | before sokol_gfx is initialized, and pass any required information |
| 76 | (like 3D device pointers) to the sokol_gfx initialization call |
| 77 | |
| 78 | - present the rendered frame, how this is done exactly usually depends |
| 79 | on how the window and 3D-API context/device was created |
| 80 | |
| 81 | - provide a unified shader language, instead 3D-API-specific shader |
| 82 | source-code or shader-bytecode must be provided |
| 83 | |
| 84 | For complete code examples using the various backend 3D-APIs, see: |
| 85 | |
| 86 | https://github.com/floooh/sokol-samples |
| 87 | |
| 88 | For an optional shader-cross-compile solution, see: |
| 89 | |
| 90 | https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md |
| 91 | |
| 92 | |
| 93 | STEP BY STEP |
| 94 | ============ |
| 95 | --- to initialize sokol_gfx, after creating a window and a 3D-API |
| 96 | context/device, call: |
| 97 | |
| 98 | sg_setup(const sg_desc*) |
| 99 | |
| 100 | --- create resource objects (at least buffers, shaders and pipelines, |
| 101 | and optionally images and passes): |
| 102 | |
| 103 | sg_buffer sg_make_buffer(const sg_buffer_desc*) |
| 104 | sg_image sg_make_image(const sg_image_desc*) |
| 105 | sg_shader sg_make_shader(const sg_shader_desc*) |
| 106 | sg_pipeline sg_make_pipeline(const sg_pipeline_desc*) |
| 107 | sg_pass sg_make_pass(const sg_pass_desc*) |
| 108 | |
| 109 | --- start rendering to the default frame buffer with: |
| 110 | |
| 111 | sg_begin_default_pass(const sg_pass_action* actions, int width, int height) |
| 112 | |
| 113 | --- or start rendering to an offscreen framebuffer with: |
| 114 | |
| 115 | sg_begin_pass(sg_pass pass, const sg_pass_action* actions) |
| 116 | |
| 117 | --- set the pipeline state for the next draw call with: |
| 118 | |
| 119 | sg_apply_pipeline(sg_pipeline pip) |
| 120 | |
| 121 | --- fill an sg_bindings struct with the resource bindings for the next |
| 122 | draw call (1..N vertex buffers, 0 or 1 index buffer, 0..N image objects |
| 123 | to use as textures each on the vertex-shader- and fragment-shader-stage |
| 124 | and then call |
| 125 | |
| 126 | sg_apply_bindings(const sg_bindings* bindings) |
| 127 | |
| 128 | to update the resource bindings |
| 129 | |
| 130 | --- optionally update shader uniform data with: |
| 131 | |
| 132 | sg_apply_uniforms(sg_shader_stage stage, int ub_index, const void* data, int num_bytes) |
| 133 | |
| 134 | --- kick off a draw call with: |
| 135 | |
| 136 | sg_draw(int base_element, int num_elements, int num_instances) |
| 137 | |
| 138 | --- finish the current rendering pass with: |
| 139 | |
| 140 | sg_end_pass() |
| 141 | |
| 142 | --- when done with the current frame, call |
| 143 | |
| 144 | sg_commit() |
| 145 | |
| 146 | --- at the end of your program, shutdown sokol_gfx with: |
| 147 | |
| 148 | sg_shutdown() |
| 149 | |
| 150 | --- if you need to destroy resources before sg_shutdown(), call: |
| 151 | |
| 152 | sg_destroy_buffer(sg_buffer buf) |
| 153 | sg_destroy_image(sg_image img) |
| 154 | sg_destroy_shader(sg_shader shd) |
| 155 | sg_destroy_pipeline(sg_pipeline pip) |
| 156 | sg_destroy_pass(sg_pass pass) |
| 157 | |
| 158 | --- to set a new viewport rectangle, call |
| 159 | |
| 160 | sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left) |
| 161 | |
| 162 | --- to set a new scissor rect, call: |
| 163 | |
| 164 | sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left) |
| 165 | |
| 166 | both sg_apply_viewport() and sg_apply_scissor_rect() must be called |
| 167 | inside a rendering pass |
| 168 | |
| 169 | beginning a pass will reset the viewport to the size of the framebuffer used |
| 170 | in the new pass, |
| 171 | |
| 172 | --- to update (overwrite) the content of buffer and image resources, call: |
| 173 | |
| 174 | sg_update_buffer(sg_buffer buf, const void* ptr, int num_bytes) |
| 175 | sg_update_image(sg_image img, const sg_image_content* content) |
| 176 | |
| 177 | Buffers and images to be updated must have been created with |
| 178 | SG_USAGE_DYNAMIC or SG_USAGE_STREAM |
| 179 | |
| 180 | Only one update per frame is allowed for buffer and image resources. |
| 181 | The rationale is to have a simple countermeasure to avoid the CPU |
| 182 | scribbling over data the GPU is currently using, or the CPU having to |
| 183 | wait for the GPU |
| 184 | |
| 185 | Buffer and image updates can be partial, as long as a rendering |
| 186 | operation only references the valid (updated) data in the |
| 187 | buffer or image. |
| 188 | |
| 189 | --- to append a chunk of data to a buffer resource, call: |
| 190 | |
| 191 | int sg_append_buffer(sg_buffer buf, const void* ptr, int num_bytes) |
| 192 | |
| 193 | The difference to sg_update_buffer() is that sg_append_buffer() |
| 194 | can be called multiple times per frame to append new data to the |
| 195 | buffer piece by piece, optionally interleaved with draw calls referencing |
| 196 | the previously written data. |
| 197 | |
| 198 | sg_append_buffer() returns a byte offset to the start of the |
| 199 | written data, this offset can be assigned to |
| 200 | sg_bindings.vertex_buffer_offsets[n] or |
| 201 | sg_bindings.index_buffer_offset |
| 202 | |
| 203 | Code example: |
| 204 | |
| 205 | for (...) { |
| 206 | const void* data = ...; |
| 207 | const int num_bytes = ...; |
| 208 | int offset = sg_append_buffer(buf, data, num_bytes); |
| 209 | bindings.vertex_buffer_offsets[0] = offset; |
| 210 | sg_apply_pipeline(pip); |
| 211 | sg_apply_bindings(&bindings); |
| 212 | sg_apply_uniforms(...); |
| 213 | sg_draw(...); |
| 214 | } |
| 215 | |
| 216 | A buffer to be used with sg_append_buffer() must have been created |
| 217 | with SG_USAGE_DYNAMIC or SG_USAGE_STREAM. |
| 218 | |
| 219 | If the application appends more data to the buffer then fits into |
| 220 | the buffer, the buffer will go into the "overflow" state for the |
| 221 | rest of the frame. |
| 222 | |
| 223 | Any draw calls attempting to render an overflown buffer will be |
| 224 | silently dropped (in debug mode this will also result in a |
| 225 | validation error). |
| 226 | |
| 227 | You can also check manually if a buffer is in overflow-state by calling |
| 228 | |
| 229 | bool sg_query_buffer_overflow(sg_buffer buf) |
| 230 | |
| 231 | --- to check for support of optional features: |
| 232 | |
| 233 | bool sg_query_feature(sg_feature feature) |
| 234 | |
| 235 | --- if you need to call into the underlying 3D-API directly, you must call: |
| 236 | |
| 237 | sg_reset_state_cache() |
| 238 | |
| 239 | ...before calling sokol_gfx functions again |
| 240 | |
| 241 | --- you can inspect the original sg_desc structure handed to sg_setup() |
| 242 | by calling sg_query_desc(). This will return an sg_desc struct with |
| 243 | the default values patched in instead of any zero-initialized values |
| 244 | |
| 245 | --- you can inspect various internal resource attributes via: |
| 246 | |
| 247 | sg_buffer_info sg_query_buffer_info(sg_buffer buf) |
| 248 | sg_image_info sg_query_image_info(sg_image img) |
| 249 | sg_shader_info sg_query_shader_info(sg_shader shd) |
| 250 | sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip) |
| 251 | sg_pass_info sg_query_pass_info(sg_pass pass) |
| 252 | |
| 253 | ...please note that the returned info-structs are tied quite closely |
| 254 | to sokol_gfx.h internals, and may change more often than other |
| 255 | public API functions and structs. |
| 256 | |
| 257 | --- you can ask at runtime what backend sokol_gfx.h has been compiled |
| 258 | for, or whether the GLES3 backend had to fall back to GLES2 with: |
| 259 | |
| 260 | sg_backend sg_query_backend(void) |
| 261 | |
| 262 | BACKEND-SPECIFIC TOPICS: |
| 263 | ======================== |
| 264 | --- the GL backends need to know about the internal structure of uniform |
| 265 | blocks, and the texture sampler-name and -type: |
| 266 | |
| 267 | typedef struct { |
| 268 | float mvp[16]; // model-view-projection matrix |
| 269 | float offset0[2]; // some 2D vectors |
| 270 | float offset1[2]; |
| 271 | float offset2[2]; |
| 272 | } params_t; |
| 273 | |
| 274 | // uniform block structure and texture image definition in sg_shader_desc: |
| 275 | sg_shader_desc desc = { |
| 276 | // uniform block description (size and internal structure) |
| 277 | .vs.uniform_blocks[0] = { |
| 278 | .size = sizeof(params_t), |
| 279 | .uniforms = { |
| 280 | [0] = { .name="mvp", .type=SG_UNIFORMTYPE_MAT4 }, |
| 281 | [1] = { .name="offset0", .type=SG_UNIFORMTYPE_VEC2 }, |
| 282 | ... |
| 283 | } |
| 284 | }, |
| 285 | // one texture on the fragment-shader-stage, GLES2/WebGL needs name and image type |
| 286 | .fs.images[0] = { .name="tex", .type=SG_IMAGETYPE_ARRAY } |
| 287 | ... |
| 288 | }; |
| 289 | |
| 290 | --- the Metal and D3D11 backends only need to know the size of uniform blocks, |
| 291 | not their internal member structure, and they only need to know |
| 292 | the type of a texture sampler, not its name: |
| 293 | |
| 294 | sg_shader_desc desc = { |
| 295 | .vs.uniform_blocks[0].size = sizeof(params_t), |
| 296 | .fs.images[0].type = SG_IMAGETYPE_ARRAY, |
| 297 | ... |
| 298 | }; |
| 299 | |
| 300 | --- when creating a shader object, GLES2/WebGL need to know the vertex |
| 301 | attribute names as used in the vertex shader: |
| 302 | |
| 303 | sg_shader_desc desc = { |
| 304 | .attrs = { |
| 305 | [0] = { .name="position" }, |
| 306 | [1] = { .name="color1" } |
| 307 | } |
| 308 | }; |
| 309 | |
| 310 | The vertex attribute names provided when creating a shader will be |
| 311 | used later in sg_create_pipeline() for matching the vertex layout |
| 312 | to vertex shader inputs. |
| 313 | |
| 314 | --- on D3D11 you need to provide a semantic name and semantic index in the |
| 315 | shader description struct instead (see the D3D11 documentation on |
| 316 | D3D11_INPUT_ELEMENT_DESC for details): |
| 317 | |
| 318 | sg_shader_desc desc = { |
| 319 | .attrs = { |
| 320 | [0] = { .sem_name="POSITION", .sem_index=0 } |
| 321 | [1] = { .sem_name="COLOR", .sem_index=1 } |
| 322 | } |
| 323 | }; |
| 324 | |
| 325 | The provided semantic information will be used later in sg_create_pipeline() |
| 326 | to match the vertex layout to vertex shader inputs. |
| 327 | |
| 328 | --- on Metal, GL 3.3 or GLES3/WebGL2, you don't need to provide an attribute |
| 329 | name or semantic name, since vertex attributes can be bound by their slot index |
| 330 | (this is mandatory in Metal, and optional in GL): |
| 331 | |
| 332 | sg_pipeline_desc desc = { |
| 333 | .layout = { |
| 334 | .attrs = { |
| 335 | [0] = { .format=SG_VERTEXFORMAT_FLOAT3 }, |
| 336 | [1] = { .format=SG_VERTEXFORMAT_FLOAT4 } |
| 337 | } |
| 338 | } |
| 339 | }; |
| 340 | |
| 341 | WORKING WITH CONTEXTS |
| 342 | ===================== |
| 343 | sokol-gfx allows to switch between different rendering contexts and |
| 344 | associate resource objects with contexts. This is useful to |
| 345 | create GL applications that render into multiple windows. |
| 346 | |
| 347 | A rendering context keeps track of all resources created while |
| 348 | the context is active. When the context is destroyed, all resources |
| 349 | "belonging to the context" are destroyed as well. |
| 350 | |
| 351 | A default context will be created and activated implicitly in |
| 352 | sg_setup(), and destroyed in sg_shutdown(). So for a typical application |
| 353 | which *doesn't* use multiple contexts, nothing changes, and calling |
| 354 | the context functions isn't necessary. |
| 355 | |
| 356 | Three functions have been added to work with contexts: |
| 357 | |
| 358 | --- sg_context sg_setup_context(): |
| 359 | This must be called once after a GL context has been created and |
| 360 | made active. |
| 361 | |
| 362 | --- void sg_activate_context(sg_context ctx) |
| 363 | This must be called after making a different GL context active. |
| 364 | Apart from 3D-API-specific actions, the call to sg_activate_context() |
| 365 | will internally call sg_reset_state_cache(). |
| 366 | |
| 367 | --- void sg_discard_context(sg_context ctx) |
| 368 | This must be called right before a GL context is destroyed and |
| 369 | will destroy all resources associated with the context (that |
| 370 | have been created while the context was active) The GL context must be |
| 371 | active at the time sg_discard_context(sg_context ctx) is called. |
| 372 | |
| 373 | Also note that resources (buffers, images, shaders and pipelines) must |
| 374 | only be used or destroyed while the same GL context is active that |
| 375 | was also active while the resource was created (an exception is |
| 376 | resource sharing on GL, such resources can be used while |
| 377 | another context is active, but must still be destroyed under |
| 378 | the same context that was active during creation). |
| 379 | |
| 380 | For more information, check out the multiwindow-glfw sample: |
| 381 | |
| 382 | https://github.com/floooh/sokol-samples/blob/master/glfw/multiwindow-glfw.c |
| 383 | |
| 384 | TRACE HOOKS: |
| 385 | ============ |
| 386 | sokol_gfx.h optionally allows to install "trace hook" callbacks for |
| 387 | each public API functions. When a public API function is called, and |
| 388 | a trace hook callback has been installed for this function, the |
| 389 | callback will be invoked with the parameters and result of the function. |
| 390 | This is useful for things like debugging- and profiling-tools, or |
| 391 | keeping track of resource creation and destruction. |
| 392 | |
| 393 | To use the trace hook feature: |
| 394 | |
| 395 | --- Define SOKOL_TRACE_HOOKS before including the implementation. |
| 396 | |
| 397 | --- Setup an sg_trace_hooks structure with your callback function |
| 398 | pointers (keep all function pointers you're not interested |
| 399 | in zero-initialized), optionally set the user_data member |
| 400 | in the sg_trace_hooks struct. |
| 401 | |
| 402 | --- Install the trace hooks by calling sg_install_trace_hooks(), |
| 403 | the return value of this function is another sg_trace_hooks |
| 404 | struct which contains the previously set of trace hooks. |
| 405 | You should keep this struct around, and call those previous |
| 406 | functions pointers from your own trace callbacks for proper |
| 407 | chaining. |
| 408 | |
| 409 | As an example of how trace hooks are used, have a look at the |
| 410 | imgui/sokol_gfx_imgui.h header which implements a realtime |
| 411 | debugging UI for sokol_gfx.h on top of Dear ImGui. |
| 412 | |
| 413 | A NOTE ON PORTABLE PACKED VERTEX FORMATS: |
| 414 | ========================================= |
| 415 | There are two things to consider when using packed |
| 416 | vertex formats like UBYTE4, SHORT2, etc which need to work |
| 417 | across all backends: |
| 418 | |
| 419 | - D3D11 can only convert *normalized* vertex formats to |
| 420 | floating point during vertex fetch, normalized formats |
| 421 | have a trailing 'N', and are "normalized" to a range |
| 422 | -1.0..+1.0 (for the signed formats) or 0.0..1.0 (for the |
| 423 | unsigned formats): |
| 424 | |
| 425 | - SG_VERTEXFORMAT_BYTE4N |
| 426 | - SG_VERTEXFORMAT_UBYTE4N |
| 427 | - SG_VERTEXFORMAT_SHORT2N |
| 428 | - SG_VERTEXFORMAT_SHORT4N |
| 429 | |
| 430 | D3D11 will not convert *non-normalized* vertex formats |
| 431 | to floating point vertex shader inputs, those can |
| 432 | only use the ivecn formats when D3D11 is used |
| 433 | as backend (GL and should Metal can use both formats) |
| 434 | |
| 435 | - SG_VERTEXFORMAT_BYTE4, |
| 436 | - SG_VERTEXFORMAT_UBYTE4 |
| 437 | - SG_VERTEXFORMAT_SHORT2 |
| 438 | - SG_VERTEXFORMAT_SHORT4 |
| 439 | |
| 440 | - WebGL/GLES2 cannot use integer vertex shader inputs (int or ivecn) |
| 441 | |
| 442 | - SG_VERTEXFORMAT_UINT10_N2 is not supported on WebGL/GLES2 |
| 443 | |
| 444 | So for a vertex input layout which works on all platforms, only use the following |
| 445 | vertex formats, and if needed "expand" the normalized vertex shader |
| 446 | inputs in the vertex shader by multiplying with 127.0, 255.0, 32767.0 or |
| 447 | 65535.0: |
| 448 | |
| 449 | - SG_VERTEXFORMAT_FLOAT, |
| 450 | - SG_VERTEXFORMAT_FLOAT2, |
| 451 | - SG_VERTEXFORMAT_FLOAT3, |
| 452 | - SG_VERTEXFORMAT_FLOAT4, |
| 453 | - SG_VERTEXFORMAT_BYTE4N, |
| 454 | - SG_VERTEXFORMAT_UBYTE4N, |
| 455 | - SG_VERTEXFORMAT_SHORT2N, |
| 456 | - SG_VERTEXFORMAT_SHORT4N, |
| 457 | |
| 458 | TODO: |
| 459 | ==== |
| 460 | - talk about asynchronous resource creation |
| 461 | |
| 462 | zlib/libpng license |
| 463 | |
| 464 | Copyright (c) 2018 Andre Weissflog |
| 465 | |
| 466 | This software is provided 'as-is', without any express or implied warranty. |
| 467 | In no event will the authors be held liable for any damages arising from the |
| 468 | use of this software. |
| 469 | |
| 470 | Permission is granted to anyone to use this software for any purpose, |
| 471 | including commercial applications, and to alter it and redistribute it |
| 472 | freely, subject to the following restrictions: |
| 473 | |
| 474 | 1. The origin of this software must not be misrepresented; you must not |
| 475 | claim that you wrote the original software. If you use this software in a |
| 476 | product, an acknowledgment in the product documentation would be |
| 477 | appreciated but is not required. |
| 478 | |
| 479 | 2. Altered source versions must be plainly marked as such, and must not |
| 480 | be misrepresented as being the original software. |
| 481 | |
| 482 | 3. This notice may not be removed or altered from any source |
| 483 | distribution. |
| 484 | */ |
| 485 | #define SOKOL_GFX_INCLUDED (1) |
| 486 | #include <stdint.h> |
| 487 | #include <stdbool.h> |
| 488 | |
| 489 | #ifndef SOKOL_API_DECL |
| 490 | #if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_IMPL) |
| 491 | #define SOKOL_API_DECL __declspec(dllexport) |
| 492 | #elif defined(_WIN32) && defined(SOKOL_DLL) |
| 493 | #define SOKOL_API_DECL __declspec(dllimport) |
| 494 | #else |
| 495 | #define SOKOL_API_DECL extern |
| 496 | #endif |
| 497 | #endif |
| 498 | |
| 499 | #ifdef __cplusplus |
| 500 | extern "C" { |
| 501 | #endif |
| 502 | |
| 503 | #ifdef _MSC_VER |
| 504 | #pragma warning(push) |
| 505 | #pragma warning(disable:4201) /* nonstandard extension used: nameless struct/union */ |
| 506 | #endif |
| 507 | |
| 508 | /* |
| 509 | Resource id typedefs: |
| 510 | |
| 511 | sg_buffer: vertex- and index-buffers |
| 512 | sg_image: textures and render targets |
| 513 | sg_shader: vertex- and fragment-shaders, uniform blocks |
| 514 | sg_pipeline: associated shader and vertex-layouts, and render states |
| 515 | sg_pass: a bundle of render targets and actions on them |
| 516 | sg_context: a 'context handle' for switching between 3D-API contexts |
| 517 | |
| 518 | Instead of pointers, resource creation functions return a 32-bit |
| 519 | number which uniquely identifies the resource object. |
| 520 | |
| 521 | The 32-bit resource id is split into a 16-bit pool index in the lower bits, |
| 522 | and a 16-bit 'unique counter' in the upper bits. The index allows fast |
| 523 | pool lookups, and combined with the unique-mask it allows to detect |
| 524 | 'dangling accesses' (trying to use an object which no longer exists, and |
| 525 | its pool slot has been reused for a new object) |
| 526 | |
| 527 | The resource ids are wrapped into a struct so that the compiler |
| 528 | can complain when the wrong resource type is used. |
| 529 | */ |
| 530 | typedef struct sg_buffer { uint32_t id; } sg_buffer; |
| 531 | typedef struct sg_image { uint32_t id; } sg_image; |
| 532 | typedef struct sg_shader { uint32_t id; } sg_shader; |
| 533 | typedef struct sg_pipeline { uint32_t id; } sg_pipeline; |
| 534 | typedef struct sg_pass { uint32_t id; } sg_pass; |
| 535 | typedef struct sg_context { uint32_t id; } sg_context; |
| 536 | |
| 537 | /* |
| 538 | various compile-time constants |
| 539 | |
| 540 | FIXME: it may make sense to convert some of those into defines so |
| 541 | that the user code can override them. |
| 542 | */ |
| 543 | enum { |
| 544 | SG_INVALID_ID = 0, |
| 545 | SG_NUM_SHADER_STAGES = 2, |
| 546 | SG_NUM_INFLIGHT_FRAMES = 2, |
| 547 | SG_MAX_COLOR_ATTACHMENTS = 4, |
| 548 | SG_MAX_SHADERSTAGE_BUFFERS = 4, |
| 549 | SG_MAX_SHADERSTAGE_IMAGES = 12, |
| 550 | SG_MAX_SHADERSTAGE_UBS = 4, |
| 551 | SG_MAX_UB_MEMBERS = 16, |
| 552 | SG_MAX_VERTEX_ATTRIBUTES = 16, |
| 553 | SG_MAX_MIPMAPS = 16, |
| 554 | SG_MAX_TEXTUREARRAY_LAYERS = 128 |
| 555 | }; |
| 556 | |
| 557 | /* |
| 558 | sg_backend |
| 559 | |
| 560 | The active 3D-API backend, use the function sg_query_backend() |
| 561 | to get the currently active backend. |
| 562 | |
| 563 | For returned value corresponds with the compile-time define to select |
| 564 | a backend, with the only exception of SOKOL_GLES3: this may |
| 565 | return SG_BACKEND_GLES2 if the backend has to fallback to GLES2 mode |
| 566 | because GLES3 isn't supported. |
| 567 | */ |
| 568 | typedef enum sg_backend { |
| 569 | SG_BACKEND_GLCORE33, |
| 570 | SG_BACKEND_GLES2, |
| 571 | SG_BACKEND_GLES3, |
| 572 | SG_BACKEND_D3D11, |
| 573 | SG_BACKEND_METAL_IOS, |
| 574 | SG_BACKEND_METAL_MACOS, |
| 575 | SG_BACKEND_METAL_SIMULATOR, |
| 576 | SG_BACKEND_DUMMY, |
| 577 | } sg_backend; |
| 578 | |
| 579 | /* |
| 580 | sg_feature |
| 581 | |
| 582 | These are optional features, use the function |
| 583 | sg_query_feature() to check whether the feature is supported. |
| 584 | */ |
| 585 | typedef enum sg_feature { |
| 586 | SG_FEATURE_INSTANCING, |
| 587 | SG_FEATURE_TEXTURE_COMPRESSION_DXT, |
| 588 | SG_FEATURE_TEXTURE_COMPRESSION_PVRTC, |
| 589 | SG_FEATURE_TEXTURE_COMPRESSION_ATC, |
| 590 | SG_FEATURE_TEXTURE_COMPRESSION_ETC2, |
| 591 | SG_FEATURE_TEXTURE_FLOAT, |
| 592 | SG_FEATURE_TEXTURE_HALF_FLOAT, |
| 593 | SG_FEATURE_ORIGIN_BOTTOM_LEFT, |
| 594 | SG_FEATURE_ORIGIN_TOP_LEFT, |
| 595 | SG_FEATURE_MSAA_RENDER_TARGETS, |
| 596 | SG_FEATURE_PACKED_VERTEX_FORMAT_10_2, |
| 597 | SG_FEATURE_MULTIPLE_RENDER_TARGET, |
| 598 | SG_FEATURE_IMAGETYPE_3D, |
| 599 | SG_FEATURE_IMAGETYPE_ARRAY, |
| 600 | |
| 601 | SG_NUM_FEATURES |
| 602 | } sg_feature; |
| 603 | |
| 604 | /* |
| 605 | sg_resource_state |
| 606 | |
| 607 | The current state of a resource in its resource pool. |
| 608 | Resources start in the INITIAL state, which means the |
| 609 | pool slot is unoccupied and can be allocated. When a resource is |
| 610 | created, first an id is allocated, and the resource pool slot |
| 611 | is set to state ALLOC. After allocation, the resource is |
| 612 | initialized, which may result in the VALID or FAILED state. The |
| 613 | reason why allocation and initialization are separate is because |
| 614 | some resource types (e.g. buffers and images) might be asynchronously |
| 615 | initialized by the user application. If a resource which is not |
| 616 | in the VALID state is attempted to be used for rendering, rendering |
| 617 | operations will silently be dropped. |
| 618 | |
| 619 | The special INVALID state is returned in sg_query_xxx_state() if no |
| 620 | resource object exists for the provided resource id. |
| 621 | */ |
| 622 | typedef enum sg_resource_state { |
| 623 | SG_RESOURCESTATE_INITIAL, |
| 624 | SG_RESOURCESTATE_ALLOC, |
| 625 | SG_RESOURCESTATE_VALID, |
| 626 | SG_RESOURCESTATE_FAILED, |
| 627 | SG_RESOURCESTATE_INVALID, |
| 628 | _SG_RESOURCESTATE_FORCE_U32 = 0x7FFFFFFF |
| 629 | } sg_resource_state; |
| 630 | |
| 631 | /* |
| 632 | sg_usage |
| 633 | |
| 634 | A resource usage hint describing the update strategy of |
| 635 | buffers and images. This is used in the sg_buffer_desc.usage |
| 636 | and sg_image_desc.usage members when creating buffers |
| 637 | and images: |
| 638 | |
| 639 | SG_USAGE_IMMUTABLE: the resource will never be updated with |
| 640 | new data, instead the data content of the |
| 641 | resource must be provided on creation |
| 642 | SG_USAGE_DYNAMIC: the resource will be updated infrequently |
| 643 | with new data (this could range from "once |
| 644 | after creation", to "quite often but not |
| 645 | every frame") |
| 646 | SG_USAGE_STREAM: the resource will be updated each frame |
| 647 | with new content |
| 648 | |
| 649 | The rendering backends use this hint to prevent that the |
| 650 | CPU needs to wait for the GPU when attempting to update |
| 651 | a resource that might be currently accessed by the GPU. |
| 652 | |
| 653 | Resource content is updated with the function sg_update_buffer() for |
| 654 | buffer objects, and sg_update_image() for image objects. Only |
| 655 | one update is allowed per frame and resource object. The |
| 656 | application must update all data required for rendering (this |
| 657 | means that the update data can be smaller than the resource size, |
| 658 | if only a part of the overall resource size is used for rendering, |
| 659 | you only need to make sure that the data that *is* used is valid. |
| 660 | |
| 661 | The default usage is SG_USAGE_IMMUTABLE. |
| 662 | */ |
| 663 | typedef enum sg_usage { |
| 664 | _SG_USAGE_DEFAULT, /* value 0 reserved for default-init */ |
| 665 | SG_USAGE_IMMUTABLE, |
| 666 | SG_USAGE_DYNAMIC, |
| 667 | SG_USAGE_STREAM, |
| 668 | _SG_USAGE_NUM, |
| 669 | _SG_USAGE_FORCE_U32 = 0x7FFFFFFF |
| 670 | } sg_usage; |
| 671 | |
| 672 | /* |
| 673 | sg_buffer_type |
| 674 | |
| 675 | This indicates whether a buffer contains vertex- or index-data, |
| 676 | used in the sg_buffer_desc.type member when creating a buffer. |
| 677 | |
| 678 | The default value is SG_BUFFERTYPE_VERTEXBUFFER. |
| 679 | */ |
| 680 | typedef enum sg_buffer_type { |
| 681 | _SG_BUFFERTYPE_DEFAULT, /* value 0 reserved for default-init */ |
| 682 | SG_BUFFERTYPE_VERTEXBUFFER, |
| 683 | SG_BUFFERTYPE_INDEXBUFFER, |
| 684 | _SG_BUFFERTYPE_NUM, |
| 685 | _SG_BUFFERTYPE_FORCE_U32 = 0x7FFFFFFF |
| 686 | } sg_buffer_type; |
| 687 | |
| 688 | /* |
| 689 | sg_index_type |
| 690 | |
| 691 | Indicates whether indexed rendering (fetching vertex-indices from an |
| 692 | index buffer) is used, and if yes, the index data type (16- or 32-bits). |
| 693 | This is used in the sg_pipeline_desc.index_type member when creating a |
| 694 | pipeline object. |
| 695 | |
| 696 | The default index type is SG_INDEXTYPE_NONE. |
| 697 | */ |
| 698 | typedef enum sg_index_type { |
| 699 | _SG_INDEXTYPE_DEFAULT, /* value 0 reserved for default-init */ |
| 700 | SG_INDEXTYPE_NONE, |
| 701 | SG_INDEXTYPE_UINT16, |
| 702 | SG_INDEXTYPE_UINT32, |
| 703 | _SG_INDEXTYPE_NUM, |
| 704 | _SG_INDEXTYPE_FORCE_U32 = 0x7FFFFFFF |
| 705 | } sg_index_type; |
| 706 | |
| 707 | /* |
| 708 | sg_image_type |
| 709 | |
| 710 | Indicates the basic image type (2D-texture, cubemap, 3D-texture |
| 711 | or 2D-array-texture). 3D- and array-textures are not supported |
| 712 | on the GLES2/WebGL backend. The image type is used in the |
| 713 | sg_image_desc.type member when creating an image. |
| 714 | |
| 715 | The default image type when creating an image is SG_IMAGETYPE_2D. |
| 716 | */ |
| 717 | typedef enum sg_image_type { |
| 718 | _SG_IMAGETYPE_DEFAULT, /* value 0 reserved for default-init */ |
| 719 | SG_IMAGETYPE_2D, |
| 720 | SG_IMAGETYPE_CUBE, |
| 721 | SG_IMAGETYPE_3D, |
| 722 | SG_IMAGETYPE_ARRAY, |
| 723 | _SG_IMAGETYPE_NUM, |
| 724 | _SG_IMAGETYPE_FORCE_U32 = 0x7FFFFFFF |
| 725 | } sg_image_type; |
| 726 | |
| 727 | /* |
| 728 | sg_cube_face |
| 729 | |
| 730 | The cubemap faces. Use these as indices in the sg_image_desc.content |
| 731 | array. |
| 732 | */ |
| 733 | typedef enum sg_cube_face { |
| 734 | SG_CUBEFACE_POS_X, |
| 735 | SG_CUBEFACE_NEG_X, |
| 736 | SG_CUBEFACE_POS_Y, |
| 737 | SG_CUBEFACE_NEG_Y, |
| 738 | SG_CUBEFACE_POS_Z, |
| 739 | SG_CUBEFACE_NEG_Z, |
| 740 | SG_CUBEFACE_NUM, |
| 741 | _SG_CUBEFACE_FORCE_U32 = 0x7FFFFFFF |
| 742 | } sg_cube_face; |
| 743 | |
| 744 | /* |
| 745 | sg_shader_stage |
| 746 | |
| 747 | There are 2 shader stages: vertex- and fragment-shader-stage. |
| 748 | Each shader stage consists of: |
| 749 | |
| 750 | - one slot for a shader function (provided as source- or byte-code) |
| 751 | - SG_MAX_SHADERSTAGE_UBS slots for uniform blocks |
| 752 | - SG_MAX_SHADERSTAGE_IMAGES slots for images used as textures by |
| 753 | the shader function |
| 754 | */ |
| 755 | typedef enum sg_shader_stage { |
| 756 | SG_SHADERSTAGE_VS, |
| 757 | SG_SHADERSTAGE_FS, |
| 758 | _SG_SHADERSTAGE_FORCE_U32 = 0x7FFFFFFF |
| 759 | } sg_shader_stage; |
| 760 | |
| 761 | /* |
| 762 | sg_pixel_format |
| 763 | |
| 764 | This is a common subset of useful and widely supported pixel formats. The |
| 765 | pixel format enum is mainly used when creating an image object in the |
| 766 | sg_image_desc.pixel_format member. |
| 767 | |
| 768 | The default pixel format when creating an image is SG_PIXELFORMAT_RGBA8. |
| 769 | */ |
| 770 | typedef enum sg_pixel_format { |
| 771 | _SG_PIXELFORMAT_DEFAULT, /* value 0 reserved for default-init */ |
| 772 | SG_PIXELFORMAT_NONE, |
| 773 | SG_PIXELFORMAT_RGBA8, |
| 774 | SG_PIXELFORMAT_RGB8, |
| 775 | SG_PIXELFORMAT_RGBA4, |
| 776 | SG_PIXELFORMAT_R5G6B5, |
| 777 | SG_PIXELFORMAT_R5G5B5A1, |
| 778 | SG_PIXELFORMAT_R10G10B10A2, |
| 779 | SG_PIXELFORMAT_RGBA32F, |
| 780 | SG_PIXELFORMAT_RGBA16F, |
| 781 | SG_PIXELFORMAT_R32F, |
| 782 | SG_PIXELFORMAT_R16F, |
| 783 | SG_PIXELFORMAT_L8, |
| 784 | SG_PIXELFORMAT_DXT1, |
| 785 | SG_PIXELFORMAT_DXT3, |
| 786 | SG_PIXELFORMAT_DXT5, |
| 787 | SG_PIXELFORMAT_DEPTH, |
| 788 | SG_PIXELFORMAT_DEPTHSTENCIL, |
| 789 | SG_PIXELFORMAT_PVRTC2_RGB, |
| 790 | SG_PIXELFORMAT_PVRTC4_RGB, |
| 791 | SG_PIXELFORMAT_PVRTC2_RGBA, |
| 792 | SG_PIXELFORMAT_PVRTC4_RGBA, |
| 793 | SG_PIXELFORMAT_ETC2_RGB8, |
| 794 | SG_PIXELFORMAT_ETC2_SRGB8, |
| 795 | _SG_PIXELFORMAT_NUM, |
| 796 | _SG_PIXELFORMAT_FORCE_U32 = 0x7FFFFFFF |
| 797 | } sg_pixel_format; |
| 798 | |
| 799 | /* |
| 800 | sg_primitive_type |
| 801 | |
| 802 | This is the common subset of 3D primitive types supported across all 3D |
| 803 | APIs. This is used in the sg_pipeline_desc.primitive_type member when |
| 804 | creating a pipeline object. |
| 805 | |
| 806 | The default primitive type is SG_PRIMITIVETYPE_TRIANGLES. |
| 807 | */ |
| 808 | typedef enum sg_primitive_type { |
| 809 | _SG_PRIMITIVETYPE_DEFAULT, /* value 0 reserved for default-init */ |
| 810 | SG_PRIMITIVETYPE_POINTS, |
| 811 | SG_PRIMITIVETYPE_LINES, |
| 812 | SG_PRIMITIVETYPE_LINE_STRIP, |
| 813 | SG_PRIMITIVETYPE_TRIANGLES, |
| 814 | SG_PRIMITIVETYPE_TRIANGLE_STRIP, |
| 815 | _SG_PRIMITIVETYPE_NUM, |
| 816 | _SG_PRIMITIVETYPE_FORCE_U32 = 0x7FFFFFFF |
| 817 | } sg_primitive_type; |
| 818 | |
| 819 | /* |
| 820 | sg_filter |
| 821 | |
| 822 | The filtering mode when sampling a texture image. This is |
| 823 | used in the sg_image_desc.min_filter and sg_image_desc.mag_filter |
| 824 | members when creating an image object. |
| 825 | |
| 826 | The default filter mode is SG_FILTER_NEAREST. |
| 827 | */ |
| 828 | typedef enum sg_filter { |
| 829 | _SG_FILTER_DEFAULT, /* value 0 reserved for default-init */ |
| 830 | SG_FILTER_NEAREST, |
| 831 | SG_FILTER_LINEAR, |
| 832 | SG_FILTER_NEAREST_MIPMAP_NEAREST, |
| 833 | SG_FILTER_NEAREST_MIPMAP_LINEAR, |
| 834 | SG_FILTER_LINEAR_MIPMAP_NEAREST, |
| 835 | SG_FILTER_LINEAR_MIPMAP_LINEAR, |
| 836 | _SG_FILTER_NUM, |
| 837 | _SG_FILTER_FORCE_U32 = 0x7FFFFFFF |
| 838 | } sg_filter; |
| 839 | |
| 840 | /* |
| 841 | sg_wrap |
| 842 | |
| 843 | The texture coordinates wrapping mode when sampling a texture |
| 844 | image. This is used in the sg_image_desc.wrap_u, .wrap_v |
| 845 | and .wrap_w members when creating an image. |
| 846 | |
| 847 | The default wrap mode is SG_WRAP_REPEAT. |
| 848 | */ |
| 849 | typedef enum sg_wrap { |
| 850 | _SG_WRAP_DEFAULT, /* value 0 reserved for default-init */ |
| 851 | SG_WRAP_REPEAT, |
| 852 | SG_WRAP_CLAMP_TO_EDGE, |
| 853 | SG_WRAP_MIRRORED_REPEAT, |
| 854 | _SG_WRAP_NUM, |
| 855 | _SG_WRAP_FORCE_U32 = 0x7FFFFFFF |
| 856 | } sg_wrap; |
| 857 | |
| 858 | /* |
| 859 | sg_vertex_format |
| 860 | |
| 861 | The data type of a vertex component. This is used to describe |
| 862 | the layout of vertex data when creating a pipeline object. |
| 863 | */ |
| 864 | typedef enum sg_vertex_format { |
| 865 | SG_VERTEXFORMAT_INVALID, |
| 866 | SG_VERTEXFORMAT_FLOAT, |
| 867 | SG_VERTEXFORMAT_FLOAT2, |
| 868 | SG_VERTEXFORMAT_FLOAT3, |
| 869 | SG_VERTEXFORMAT_FLOAT4, |
| 870 | SG_VERTEXFORMAT_BYTE4, |
| 871 | SG_VERTEXFORMAT_BYTE4N, |
| 872 | SG_VERTEXFORMAT_UBYTE4, |
| 873 | SG_VERTEXFORMAT_UBYTE4N, |
| 874 | SG_VERTEXFORMAT_SHORT2, |
| 875 | SG_VERTEXFORMAT_SHORT2N, |
| 876 | SG_VERTEXFORMAT_SHORT4, |
| 877 | SG_VERTEXFORMAT_SHORT4N, |
| 878 | SG_VERTEXFORMAT_UINT10_N2, |
| 879 | _SG_VERTEXFORMAT_NUM, |
| 880 | _SG_VERTEXFORMAT_FORCE_U32 = 0x7FFFFFFF |
| 881 | } sg_vertex_format; |
| 882 | |
| 883 | /* |
| 884 | sg_vertex_step |
| 885 | |
| 886 | Defines whether the input pointer of a vertex input stream is advanced |
| 887 | 'per vertex' or 'per instance'. The default step-func is |
| 888 | SG_VERTEXSTEP_PER_VERTEX. SG_VERTEXSTEP_PER_INSTANCE is used with |
| 889 | instanced-rendering. |
| 890 | |
| 891 | The vertex-step is part of the vertex-layout definition |
| 892 | when creating pipeline objects. |
| 893 | */ |
| 894 | typedef enum sg_vertex_step { |
| 895 | _SG_VERTEXSTEP_DEFAULT, /* value 0 reserved for default-init */ |
| 896 | SG_VERTEXSTEP_PER_VERTEX, |
| 897 | SG_VERTEXSTEP_PER_INSTANCE, |
| 898 | _SG_VERTEXSTEP_NUM, |
| 899 | _SG_VERTEXSTEP_FORCE_U32 = 0x7FFFFFFF |
| 900 | } sg_vertex_step; |
| 901 | |
| 902 | /* |
| 903 | sg_uniform_type |
| 904 | |
| 905 | The data type of a uniform block member. This is used to |
| 906 | describe the internal layout of uniform blocks when creating |
| 907 | a shader object. |
| 908 | */ |
| 909 | typedef enum sg_uniform_type { |
| 910 | SG_UNIFORMTYPE_INVALID, |
| 911 | SG_UNIFORMTYPE_FLOAT, |
| 912 | SG_UNIFORMTYPE_FLOAT2, |
| 913 | SG_UNIFORMTYPE_FLOAT3, |
| 914 | SG_UNIFORMTYPE_FLOAT4, |
| 915 | SG_UNIFORMTYPE_MAT4, |
| 916 | _SG_UNIFORMTYPE_NUM, |
| 917 | _SG_UNIFORMTYPE_FORCE_U32 = 0x7FFFFFFF |
| 918 | } sg_uniform_type; |
| 919 | |
| 920 | /* |
| 921 | sg_cull_mode |
| 922 | |
| 923 | The face-culling mode, this is used in the |
| 924 | sg_pipeline_desc.rasterizer.cull_mode member when creating a |
| 925 | pipeline object. |
| 926 | |
| 927 | The default cull mode is SG_CULLMODE_NONE |
| 928 | */ |
| 929 | typedef enum sg_cull_mode { |
| 930 | _SG_CULLMODE_DEFAULT, /* value 0 reserved for default-init */ |
| 931 | SG_CULLMODE_NONE, |
| 932 | SG_CULLMODE_FRONT, |
| 933 | SG_CULLMODE_BACK, |
| 934 | _SG_CULLMODE_NUM, |
| 935 | _SG_CULLMODE_FORCE_U32 = 0x7FFFFFFF |
| 936 | } sg_cull_mode; |
| 937 | |
| 938 | /* |
| 939 | sg_face_winding |
| 940 | |
| 941 | The vertex-winding rule that determines a front-facing primitive. This |
| 942 | is used in the member sg_pipeline_desc.rasterizer.face_winding |
| 943 | when creating a pipeline object. |
| 944 | |
| 945 | The default winding is SG_FACEWINDING_CW (clockwise) |
| 946 | */ |
| 947 | typedef enum sg_face_winding { |
| 948 | _SG_FACEWINDING_DEFAULT, /* value 0 reserved for default-init */ |
| 949 | SG_FACEWINDING_CCW, |
| 950 | SG_FACEWINDING_CW, |
| 951 | _SG_FACEWINDING_NUM, |
| 952 | _SG_FACEWINDING_FORCE_U32 = 0x7FFFFFFF |
| 953 | } sg_face_winding; |
| 954 | |
| 955 | /* |
| 956 | sg_compare_func |
| 957 | |
| 958 | The compare-function for depth- and stencil-ref tests. |
| 959 | This is used when creating pipeline objects in the members: |
| 960 | |
| 961 | sg_pipeline_desc |
| 962 | .depth_stencil |
| 963 | .depth_compare_func |
| 964 | .stencil_front.compare_func |
| 965 | .stencil_back.compare_func |
| 966 | |
| 967 | The default compare func for depth- and stencil-tests is |
| 968 | SG_COMPAREFUNC_ALWAYS. |
| 969 | */ |
| 970 | typedef enum sg_compare_func { |
| 971 | _SG_COMPAREFUNC_DEFAULT, /* value 0 reserved for default-init */ |
| 972 | SG_COMPAREFUNC_NEVER, |
| 973 | SG_COMPAREFUNC_LESS, |
| 974 | SG_COMPAREFUNC_EQUAL, |
| 975 | SG_COMPAREFUNC_LESS_EQUAL, |
| 976 | SG_COMPAREFUNC_GREATER, |
| 977 | SG_COMPAREFUNC_NOT_EQUAL, |
| 978 | SG_COMPAREFUNC_GREATER_EQUAL, |
| 979 | SG_COMPAREFUNC_ALWAYS, |
| 980 | _SG_COMPAREFUNC_NUM, |
| 981 | _SG_COMPAREFUNC_FORCE_U32 = 0x7FFFFFFF |
| 982 | } sg_compare_func; |
| 983 | |
| 984 | /* |
| 985 | sg_stencil_op |
| 986 | |
| 987 | The operation performed on a currently stored stencil-value when a |
| 988 | comparison test passes or fails. This is used when creating a pipeline |
| 989 | object in the members: |
| 990 | |
| 991 | sg_pipeline_desc |
| 992 | .depth_stencil |
| 993 | .stencil_front |
| 994 | .fail_op |
| 995 | .depth_fail_op |
| 996 | .pass_op |
| 997 | .stencil_back |
| 998 | .fail_op |
| 999 | .depth_fail_op |
| 1000 | .pass_op |
| 1001 | |
| 1002 | The default value is SG_STENCILOP_KEEP. |
| 1003 | */ |
| 1004 | typedef enum sg_stencil_op { |
| 1005 | _SG_STENCILOP_DEFAULT, /* value 0 reserved for default-init */ |
| 1006 | SG_STENCILOP_KEEP, |
| 1007 | SG_STENCILOP_ZERO, |
| 1008 | SG_STENCILOP_REPLACE, |
| 1009 | SG_STENCILOP_INCR_CLAMP, |
| 1010 | SG_STENCILOP_DECR_CLAMP, |
| 1011 | SG_STENCILOP_INVERT, |
| 1012 | SG_STENCILOP_INCR_WRAP, |
| 1013 | SG_STENCILOP_DECR_WRAP, |
| 1014 | _SG_STENCILOP_NUM, |
| 1015 | _SG_STENCILOP_FORCE_U32 = 0x7FFFFFFF |
| 1016 | } sg_stencil_op; |
| 1017 | |
| 1018 | /* |
| 1019 | sg_blend_factor |
| 1020 | |
| 1021 | The source and destination factors in blending operations. |
| 1022 | This is used in the following members when creating a pipeline object: |
| 1023 | |
| 1024 | sg_pipeline_desc |
| 1025 | .blend |
| 1026 | .src_factor_rgb |
| 1027 | .dst_factor_rgb |
| 1028 | .src_factor_alpha |
| 1029 | .dst_factor_alpha |
| 1030 | |
| 1031 | The default value is SG_BLENDFACTOR_ONE for source |
| 1032 | factors, and SG_BLENDFACTOR_ZERO for destination factors. |
| 1033 | */ |
| 1034 | typedef enum sg_blend_factor { |
| 1035 | _SG_BLENDFACTOR_DEFAULT, /* value 0 reserved for default-init */ |
| 1036 | SG_BLENDFACTOR_ZERO, |
| 1037 | SG_BLENDFACTOR_ONE, |
| 1038 | SG_BLENDFACTOR_SRC_COLOR, |
| 1039 | SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR, |
| 1040 | SG_BLENDFACTOR_SRC_ALPHA, |
| 1041 | SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, |
| 1042 | SG_BLENDFACTOR_DST_COLOR, |
| 1043 | SG_BLENDFACTOR_ONE_MINUS_DST_COLOR, |
| 1044 | SG_BLENDFACTOR_DST_ALPHA, |
| 1045 | SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA, |
| 1046 | SG_BLENDFACTOR_SRC_ALPHA_SATURATED, |
| 1047 | SG_BLENDFACTOR_BLEND_COLOR, |
| 1048 | SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR, |
| 1049 | SG_BLENDFACTOR_BLEND_ALPHA, |
| 1050 | SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA, |
| 1051 | _SG_BLENDFACTOR_NUM, |
| 1052 | _SG_BLENDFACTOR_FORCE_U32 = 0x7FFFFFFF |
| 1053 | } sg_blend_factor; |
| 1054 | |
| 1055 | /* |
| 1056 | sg_blend_op |
| 1057 | |
| 1058 | Describes how the source and destination values are combined in the |
| 1059 | fragment blending operation. It is used in the following members when |
| 1060 | creating a pipeline object: |
| 1061 | |
| 1062 | sg_pipeline_desc |
| 1063 | .blend |
| 1064 | .op_rgb |
| 1065 | .op_alpha |
| 1066 | |
| 1067 | The default value is SG_BLENDOP_ADD. |
| 1068 | */ |
| 1069 | typedef enum sg_blend_op { |
| 1070 | _SG_BLENDOP_DEFAULT, /* value 0 reserved for default-init */ |
| 1071 | SG_BLENDOP_ADD, |
| 1072 | SG_BLENDOP_SUBTRACT, |
| 1073 | SG_BLENDOP_REVERSE_SUBTRACT, |
| 1074 | _SG_BLENDOP_NUM, |
| 1075 | _SG_BLENDOP_FORCE_U32 = 0x7FFFFFFF |
| 1076 | } sg_blend_op; |
| 1077 | |
| 1078 | /* |
| 1079 | sg_color_mask |
| 1080 | |
| 1081 | Selects the color channels when writing a fragment color to the |
| 1082 | framebuffer. This is used in the members |
| 1083 | sg_pipeline_desc.blend.color_write_mask when creating a pipeline object. |
| 1084 | |
| 1085 | The default colormask is SG_COLORMASK_RGBA (write all colors channels) |
| 1086 | */ |
| 1087 | typedef enum sg_color_mask { |
| 1088 | _SG_COLORMASK_DEFAULT = 0, /* value 0 reserved for default-init */ |
| 1089 | SG_COLORMASK_NONE = (0x10), /* special value for 'all channels disabled */ |
| 1090 | SG_COLORMASK_R = (1<<0), |
| 1091 | SG_COLORMASK_G = (1<<1), |
| 1092 | SG_COLORMASK_B = (1<<2), |
| 1093 | SG_COLORMASK_A = (1<<3), |
| 1094 | SG_COLORMASK_RGB = 0x7, |
| 1095 | SG_COLORMASK_RGBA = 0xF, |
| 1096 | _SG_COLORMASK_FORCE_U32 = 0x7FFFFFFF |
| 1097 | } sg_color_mask; |
| 1098 | |
| 1099 | /* |
| 1100 | sg_action |
| 1101 | |
| 1102 | Defines what action should be performed at the start of a render pass: |
| 1103 | |
| 1104 | SG_ACTION_CLEAR: clear the render target image |
| 1105 | SG_ACTION_LOAD: load the previous content of the render target image |
| 1106 | SG_ACTION_DONTCARE: leave the render target image content undefined |
| 1107 | |
| 1108 | This is used in the sg_pass_action structure. |
| 1109 | |
| 1110 | The default action for all pass attachments is SG_ACTION_CLEAR, with the |
| 1111 | clear color rgba = {0.5f, 0.5f, 0.5f, 1.0f], depth=1.0 and stencil=0. |
| 1112 | |
| 1113 | If you want to override the default behaviour, it is important to not |
| 1114 | only set the clear color, but the 'action' field as well (as long as this |
| 1115 | is in its _SG_ACTION_DEFAULT, the value fields will be ignored). |
| 1116 | */ |
| 1117 | typedef enum sg_action { |
| 1118 | _SG_ACTION_DEFAULT, |
| 1119 | SG_ACTION_CLEAR, |
| 1120 | SG_ACTION_LOAD, |
| 1121 | SG_ACTION_DONTCARE, |
| 1122 | _SG_ACTION_NUM, |
| 1123 | _SG_ACTION_FORCE_U32 = 0x7FFFFFFF |
| 1124 | } sg_action; |
| 1125 | |
| 1126 | /* |
| 1127 | sg_pass_action |
| 1128 | |
| 1129 | The sg_pass_action struct defines the actions to be performed |
| 1130 | at the start of a rendering pass in the functions sg_begin_pass() |
| 1131 | and sg_begin_default_pass(). |
| 1132 | |
| 1133 | A separate action and clear values can be defined for each |
| 1134 | color attachment, and for the depth-stencil attachment. |
| 1135 | |
| 1136 | The default clear values are defined by the macros: |
| 1137 | |
| 1138 | - SG_DEFAULT_CLEAR_RED: 0.5f |
| 1139 | - SG_DEFAULT_CLEAR_GREEN: 0.5f |
| 1140 | - SG_DEFAULT_CLEAR_BLUE: 0.5f |
| 1141 | - SG_DEFAULT_CLEAR_ALPHA: 1.0f |
| 1142 | - SG_DEFAULT_CLEAR_DEPTH: 1.0f |
| 1143 | - SG_DEFAULT_CLEAR_STENCIL: 0 |
| 1144 | */ |
| 1145 | typedef struct sg_color_attachment_action { |
| 1146 | sg_action action; |
| 1147 | float val[4]; |
| 1148 | } sg_color_attachment_action; |
| 1149 | |
| 1150 | typedef struct sg_depth_attachment_action { |
| 1151 | sg_action action; |
| 1152 | float val; |
| 1153 | } sg_depth_attachment_action; |
| 1154 | |
| 1155 | typedef struct sg_stencil_attachment_action { |
| 1156 | sg_action action; |
| 1157 | uint8_t val; |
| 1158 | } sg_stencil_attachment_action; |
| 1159 | |
| 1160 | typedef struct sg_pass_action { |
| 1161 | uint32_t _start_canary; |
| 1162 | sg_color_attachment_action colors[SG_MAX_COLOR_ATTACHMENTS]; |
| 1163 | sg_depth_attachment_action depth; |
| 1164 | sg_stencil_attachment_action stencil; |
| 1165 | uint32_t _end_canary; |
| 1166 | } sg_pass_action; |
| 1167 | |
| 1168 | /* |
| 1169 | sg_bindings |
| 1170 | |
| 1171 | The sg_bindings structure defines the resource binding slots |
| 1172 | of the sokol_gfx render pipeline, used as argument to the |
| 1173 | sg_apply_bindings() function. |
| 1174 | |
| 1175 | A resource binding struct contains: |
| 1176 | |
| 1177 | - 1..N vertex buffers |
| 1178 | - 0..N vertex buffer offsets |
| 1179 | - 0..1 index buffers |
| 1180 | - 0..1 index buffer offsets |
| 1181 | - 0..N vertex shader stage images |
| 1182 | - 0..N fragment shader stage images |
| 1183 | |
| 1184 | The max number of vertex buffer and shader stage images |
| 1185 | are defined by the SG_MAX_SHADERSTAGE_BUFFERS and |
| 1186 | SG_MAX_SHADERSTAGE_IMAGES configuration constants. |
| 1187 | |
| 1188 | The optional buffer offsets can be used to group different chunks |
| 1189 | of vertex- and/or index-data into the same buffer objects. |
| 1190 | */ |
| 1191 | typedef struct sg_bindings { |
| 1192 | uint32_t _start_canary; |
| 1193 | sg_buffer vertex_buffers[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 1194 | int vertex_buffer_offsets[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 1195 | sg_buffer index_buffer; |
| 1196 | int index_buffer_offset; |
| 1197 | sg_image vs_images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 1198 | sg_image fs_images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 1199 | uint32_t _end_canary; |
| 1200 | } sg_bindings; |
| 1201 | |
| 1202 | /* |
| 1203 | sg_buffer_desc |
| 1204 | |
| 1205 | Creation parameters for sg_buffer objects, used in the |
| 1206 | sg_make_buffer() call. |
| 1207 | |
| 1208 | The default configuration is: |
| 1209 | |
| 1210 | .size: 0 (this *must* be set to a valid size in bytes) |
| 1211 | .type: SG_BUFFERTYPE_VERTEXBUFFER |
| 1212 | .usage: SG_USAGE_IMMUTABLE |
| 1213 | .content 0 |
| 1214 | .label 0 (optional string label for trace hooks) |
| 1215 | |
| 1216 | The dbg_label will be ignored by sokol_gfx.h, it is only useful |
| 1217 | when hooking into sg_make_buffer() or sg_init_buffer() via |
| 1218 | the sg_install_trace_hook |
| 1219 | |
| 1220 | ADVANCED TOPIC: Injecting native 3D-API buffers: |
| 1221 | |
| 1222 | The following struct members allow to inject your own GL, Metal |
| 1223 | or D3D11 buffers into sokol_gfx: |
| 1224 | |
| 1225 | .gl_buffers[SG_NUM_INFLIGHT_FRAMES] |
| 1226 | .mtl_buffers[SG_NUM_INFLIGHT_FRAMES] |
| 1227 | .d3d11_buffer |
| 1228 | |
| 1229 | You must still provide all other members except the .content member, and |
| 1230 | these must match the creation parameters of the native buffers you |
| 1231 | provide. For SG_USAGE_IMMUTABLE, only provide a single native 3D-API |
| 1232 | buffer, otherwise you need to provide SG_NUM_INFLIGHT_FRAMES buffers |
| 1233 | (only for GL and Metal, not D3D11). Providing multiple buffers for GL and |
| 1234 | Metal is necessary because sokol_gfx will rotate through them when |
| 1235 | calling sg_update_buffer() to prevent lock-stalls. |
| 1236 | |
| 1237 | Note that it is expected that immutable injected buffer have already been |
| 1238 | initialized with content, and the .content member must be 0! |
| 1239 | |
| 1240 | Also you need to call sg_reset_state_cache() after calling native 3D-API |
| 1241 | functions, and before calling any sokol_gfx function. |
| 1242 | */ |
| 1243 | typedef struct sg_buffer_desc { |
| 1244 | uint32_t _start_canary; |
| 1245 | int size; |
| 1246 | sg_buffer_type type; |
| 1247 | sg_usage usage; |
| 1248 | const void* content; |
| 1249 | const char* label; |
| 1250 | /* GL specific */ |
| 1251 | uint32_t gl_buffers[SG_NUM_INFLIGHT_FRAMES]; |
| 1252 | /* Metal specific */ |
| 1253 | const void* mtl_buffers[SG_NUM_INFLIGHT_FRAMES]; |
| 1254 | /* D3D11 specific */ |
| 1255 | const void* d3d11_buffer; |
| 1256 | uint32_t _end_canary; |
| 1257 | } sg_buffer_desc; |
| 1258 | |
| 1259 | /* |
| 1260 | sg_subimage_content |
| 1261 | |
| 1262 | Pointer to and size of a subimage-surface data, this is |
| 1263 | used to describe the initial content of immutable-usage images, |
| 1264 | or for updating a dynamic- or stream-usage images. |
| 1265 | |
| 1266 | For 3D- or array-textures, one sg_subimage_content item |
| 1267 | describes an entire mipmap level consisting of all array- or |
| 1268 | 3D-slices of the mipmap level. It is only possible to update |
| 1269 | an entire mipmap level, not parts of it. |
| 1270 | */ |
| 1271 | typedef struct sg_subimage_content { |
| 1272 | const void* ptr; /* pointer to subimage data */ |
| 1273 | int size; /* size in bytes of pointed-to subimage data */ |
| 1274 | } sg_subimage_content; |
| 1275 | |
| 1276 | /* |
| 1277 | sg_image_content |
| 1278 | |
| 1279 | Defines the content of an image through a 2D array |
| 1280 | of sg_subimage_content structs. The first array dimension |
| 1281 | is the cubemap face, and the second array dimension the |
| 1282 | mipmap level. |
| 1283 | */ |
| 1284 | typedef struct sg_image_content { |
| 1285 | sg_subimage_content subimage[SG_CUBEFACE_NUM][SG_MAX_MIPMAPS]; |
| 1286 | } sg_image_content; |
| 1287 | |
| 1288 | /* |
| 1289 | sg_image_desc |
| 1290 | |
| 1291 | Creation parameters for sg_image objects, used in the |
| 1292 | sg_make_image() call. |
| 1293 | |
| 1294 | The default configuration is: |
| 1295 | |
| 1296 | .type: SG_IMAGETYPE_2D |
| 1297 | .render_target: false |
| 1298 | .width 0 (must be set to >0) |
| 1299 | .height 0 (must be set to >0) |
| 1300 | .depth/.layers: 1 |
| 1301 | .num_mipmaps: 1 |
| 1302 | .usage: SG_USAGE_IMMUTABLE |
| 1303 | .pixel_format: SG_PIXELFORMAT_RGBA8 |
| 1304 | .sample_count: 1 (only used in render_targets) |
| 1305 | .min_filter: SG_FILTER_NEAREST |
| 1306 | .mag_filter: SG_FILTER_NEAREST |
| 1307 | .wrap_u: SG_WRAP_REPEAT |
| 1308 | .wrap_v: SG_WRAP_REPEAT |
| 1309 | .wrap_w: SG_WRAP_REPEAT (only SG_IMAGETYPE_3D) |
| 1310 | .max_anisotropy 1 (must be 1..16) |
| 1311 | .min_lod 0.0f |
| 1312 | .max_lod FLT_MAX |
| 1313 | .content an sg_image_content struct to define the initial content |
| 1314 | .label 0 (optional string label for trace hooks) |
| 1315 | |
| 1316 | SG_IMAGETYPE_ARRAY and SG_IMAGETYPE_3D are not supported on |
| 1317 | WebGL/GLES2, use sg_query_feature(SG_FEATURE_IMAGETYPE_ARRAY) and |
| 1318 | sg_query_feature(SG_FEATURE_IMAGETYPE_3D) at runtime to check |
| 1319 | if array- and 3D-textures are supported. |
| 1320 | |
| 1321 | Images with usage SG_USAGE_IMMUTABLE must be fully initialized by |
| 1322 | providing a valid .content member which points to |
| 1323 | initialization data. |
| 1324 | |
| 1325 | ADVANCED TOPIC: Injecting native 3D-API textures: |
| 1326 | |
| 1327 | The following struct members allow to inject your own GL, Metal |
| 1328 | or D3D11 textures into sokol_gfx: |
| 1329 | |
| 1330 | .gl_textures[SG_NUM_INFLIGHT_FRAMES] |
| 1331 | .mtl_textures[SG_NUM_INFLIGHT_FRAMES] |
| 1332 | .d3d11_texture |
| 1333 | |
| 1334 | The same rules apply as for injecting native buffers |
| 1335 | (see sg_buffer_desc documentation for more details). |
| 1336 | */ |
| 1337 | typedef struct sg_image_desc { |
| 1338 | uint32_t _start_canary; |
| 1339 | sg_image_type type; |
| 1340 | bool render_target; |
| 1341 | int width; |
| 1342 | int height; |
| 1343 | union { |
| 1344 | int depth; |
| 1345 | int layers; |
| 1346 | }; |
| 1347 | int num_mipmaps; |
| 1348 | sg_usage usage; |
| 1349 | sg_pixel_format pixel_format; |
| 1350 | int sample_count; |
| 1351 | sg_filter min_filter; |
| 1352 | sg_filter mag_filter; |
| 1353 | sg_wrap wrap_u; |
| 1354 | sg_wrap wrap_v; |
| 1355 | sg_wrap wrap_w; |
| 1356 | uint32_t max_anisotropy; |
| 1357 | float min_lod; |
| 1358 | float max_lod; |
| 1359 | sg_image_content content; |
| 1360 | const char* label; |
| 1361 | /* GL specific */ |
| 1362 | uint32_t gl_textures[SG_NUM_INFLIGHT_FRAMES]; |
| 1363 | /* Metal specific */ |
| 1364 | const void* mtl_textures[SG_NUM_INFLIGHT_FRAMES]; |
| 1365 | /* D3D11 specific */ |
| 1366 | const void* d3d11_texture; |
| 1367 | uint32_t _end_canary; |
| 1368 | } sg_image_desc; |
| 1369 | |
| 1370 | /* |
| 1371 | sg_shader_desc |
| 1372 | |
| 1373 | The structure sg_shader_desc defines all creation parameters |
| 1374 | for shader programs, used as input to the sg_make_shader() function: |
| 1375 | |
| 1376 | - reflection information for vertex attributes (vertex shader inputs): |
| 1377 | - vertex attribute name (required for GLES2, optional for GLES3 and GL) |
| 1378 | - a semantic name and index (required for D3D11) |
| 1379 | - for each vertex- and fragment-shader-stage: |
| 1380 | - the shader source or bytecode |
| 1381 | - an optional entry function name |
| 1382 | - reflection info for each uniform block used by the shader stage: |
| 1383 | - the size of the uniform block in bytes |
| 1384 | - reflection info for each uniform block member (only required for GL backends): |
| 1385 | - member name |
| 1386 | - member type (SG_UNIFORMTYPE_xxx) |
| 1387 | - if the member is an array, the number of array items |
| 1388 | - reflection info for the texture images used by the shader stage: |
| 1389 | - the image type (SG_IMAGETYPE_xxx) |
| 1390 | - the name of the texture sampler (required for GLES2, optional everywhere else) |
| 1391 | |
| 1392 | For all GL backends, shader source-code must be provided. For D3D11 and Metal, |
| 1393 | either shader source-code or byte-code can be provided. |
| 1394 | |
| 1395 | For D3D11, if source code is provided, the d3dcompiler_47.dll will be loaded |
| 1396 | on demand. If this fails, shader creation will fail. |
| 1397 | */ |
| 1398 | typedef struct sg_shader_attr_desc { |
| 1399 | const char* name; /* GLSL vertex attribute name (only required for GLES2) */ |
| 1400 | const char* sem_name; /* HLSL semantic name */ |
| 1401 | int sem_index; /* HLSL semantic index */ |
| 1402 | } sg_shader_attr_desc; |
| 1403 | |
| 1404 | typedef struct sg_shader_uniform_desc { |
| 1405 | const char* name; |
| 1406 | sg_uniform_type type; |
| 1407 | int array_count; |
| 1408 | } sg_shader_uniform_desc; |
| 1409 | |
| 1410 | typedef struct sg_shader_uniform_block_desc { |
| 1411 | int size; |
| 1412 | sg_shader_uniform_desc uniforms[SG_MAX_UB_MEMBERS]; |
| 1413 | } sg_shader_uniform_block_desc; |
| 1414 | |
| 1415 | typedef struct sg_shader_image_desc { |
| 1416 | const char* name; |
| 1417 | sg_image_type type; |
| 1418 | } sg_shader_image_desc; |
| 1419 | |
| 1420 | typedef struct sg_shader_stage_desc { |
| 1421 | const char* source; |
| 1422 | const uint8_t* byte_code; |
| 1423 | int byte_code_size; |
| 1424 | const char* entry; |
| 1425 | sg_shader_uniform_block_desc uniform_blocks[SG_MAX_SHADERSTAGE_UBS]; |
| 1426 | sg_shader_image_desc images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 1427 | } sg_shader_stage_desc; |
| 1428 | |
| 1429 | typedef struct sg_shader_desc { |
| 1430 | uint32_t _start_canary; |
| 1431 | sg_shader_attr_desc attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 1432 | sg_shader_stage_desc vs; |
| 1433 | sg_shader_stage_desc fs; |
| 1434 | const char* label; |
| 1435 | uint32_t _end_canary; |
| 1436 | } sg_shader_desc; |
| 1437 | |
| 1438 | /* |
| 1439 | sg_pipeline_desc |
| 1440 | |
| 1441 | The sg_pipeline_desc struct defines all creation parameters |
| 1442 | for an sg_pipeline object, used as argument to the |
| 1443 | sg_make_pipeline() function: |
| 1444 | |
| 1445 | - the vertex layout for all input vertex buffers |
| 1446 | - a shader object |
| 1447 | - the 3D primitive type (points, lines, triangles, ...) |
| 1448 | - the index type (none, 16- or 32-bit) |
| 1449 | - depth-stencil state |
| 1450 | - alpha-blending state |
| 1451 | - rasterizer state |
| 1452 | |
| 1453 | If the vertex data has no gaps between vertex components, you can omit |
| 1454 | the .layout.buffers[].stride and layout.attrs[].offset items (leave them |
| 1455 | default-initialized to 0), sokol will then compute the offsets and strides |
| 1456 | from the vertex component formats (.layout.attrs[].offset). Please note |
| 1457 | that ALL vertex attribute offsets must be 0 in order for the the |
| 1458 | automatic offset computation to kick in. |
| 1459 | |
| 1460 | The default configuration is as follows: |
| 1461 | |
| 1462 | .layout: |
| 1463 | .buffers[]: vertex buffer layouts |
| 1464 | .stride: 0 (if no stride is given it will be computed) |
| 1465 | .step_func SG_VERTEXSTEP_PER_VERTEX |
| 1466 | .step_rate 1 |
| 1467 | .attrs[]: vertex attribute declarations |
| 1468 | .buffer_index 0 the vertex buffer bind slot |
| 1469 | .offset 0 (offsets can be omitted if the vertex layout has no gaps) |
| 1470 | .format SG_VERTEXFORMAT_INVALID (must be initialized!) |
| 1471 | .shader: 0 (must be intilized with a valid sg_shader id!) |
| 1472 | .primitive_type: SG_PRIMITIVETYPE_TRIANGLES |
| 1473 | .index_type: SG_INDEXTYPE_NONE |
| 1474 | .depth_stencil: |
| 1475 | .stencil_front, .stencil_back: |
| 1476 | .fail_op: SG_STENCILOP_KEEP |
| 1477 | .depth_fail_op: SG_STENCILOP_KEEP |
| 1478 | .pass_op: SG_STENCILOP_KEEP |
| 1479 | .compare_func SG_COMPAREFUNC_ALWAYS |
| 1480 | .depth_compare_func: SG_COMPAREFUNC_ALWAYS |
| 1481 | .depth_write_enabled: false |
| 1482 | .stencil_enabled: false |
| 1483 | .stencil_read_mask: 0 |
| 1484 | .stencil_write_mask: 0 |
| 1485 | .stencil_ref: 0 |
| 1486 | .blend: |
| 1487 | .enabled: false |
| 1488 | .src_factor_rgb: SG_BLENDFACTOR_ONE |
| 1489 | .dst_factor_rgb: SG_BLENDFACTOR_ZERO |
| 1490 | .op_rgb: SG_BLENDOP_ADD |
| 1491 | .src_factor_alpha: SG_BLENDFACTOR_ONE |
| 1492 | .dst_factor_alpha: SG_BLENDFACTOR_ZERO |
| 1493 | .op_alpha: SG_BLENDOP_ADD |
| 1494 | .color_write_mask: SG_COLORMASK_RGBA |
| 1495 | .color_attachment_count 1 |
| 1496 | .color_format SG_PIXELFORMAT_RGBA8 |
| 1497 | .depth_format SG_PIXELFORMAT_DEPTHSTENCIL |
| 1498 | .blend_color: { 0.0f, 0.0f, 0.0f, 0.0f } |
| 1499 | .rasterizer: |
| 1500 | .alpha_to_coverage_enabled: false |
| 1501 | .cull_mode: SG_CULLMODE_NONE |
| 1502 | .face_winding: SG_FACEWINDING_CW |
| 1503 | .sample_count: 1 |
| 1504 | .depth_bias: 0.0f |
| 1505 | .depth_bias_slope_scale: 0.0f |
| 1506 | .depth_bias_clamp: 0.0f |
| 1507 | .label 0 (optional string label for trace hooks) |
| 1508 | */ |
| 1509 | typedef struct sg_buffer_layout_desc { |
| 1510 | int stride; |
| 1511 | sg_vertex_step step_func; |
| 1512 | int step_rate; |
| 1513 | } sg_buffer_layout_desc; |
| 1514 | |
| 1515 | typedef struct sg_vertex_attr_desc { |
| 1516 | int buffer_index; |
| 1517 | int offset; |
| 1518 | sg_vertex_format format; |
| 1519 | } sg_vertex_attr_desc; |
| 1520 | |
| 1521 | typedef struct sg_layout_desc { |
| 1522 | sg_buffer_layout_desc buffers[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 1523 | sg_vertex_attr_desc attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 1524 | } sg_layout_desc; |
| 1525 | |
| 1526 | typedef struct sg_stencil_state { |
| 1527 | sg_stencil_op fail_op; |
| 1528 | sg_stencil_op depth_fail_op; |
| 1529 | sg_stencil_op pass_op; |
| 1530 | sg_compare_func compare_func; |
| 1531 | } sg_stencil_state; |
| 1532 | |
| 1533 | typedef struct sg_depth_stencil_state { |
| 1534 | sg_stencil_state stencil_front; |
| 1535 | sg_stencil_state stencil_back; |
| 1536 | sg_compare_func depth_compare_func; |
| 1537 | bool depth_write_enabled; |
| 1538 | bool stencil_enabled; |
| 1539 | uint8_t stencil_read_mask; |
| 1540 | uint8_t stencil_write_mask; |
| 1541 | uint8_t stencil_ref; |
| 1542 | } sg_depth_stencil_state; |
| 1543 | |
| 1544 | typedef struct sg_blend_state { |
| 1545 | bool enabled; |
| 1546 | sg_blend_factor src_factor_rgb; |
| 1547 | sg_blend_factor dst_factor_rgb; |
| 1548 | sg_blend_op op_rgb; |
| 1549 | sg_blend_factor src_factor_alpha; |
| 1550 | sg_blend_factor dst_factor_alpha; |
| 1551 | sg_blend_op op_alpha; |
| 1552 | uint8_t color_write_mask; |
| 1553 | int color_attachment_count; |
| 1554 | sg_pixel_format color_format; |
| 1555 | sg_pixel_format depth_format; |
| 1556 | float blend_color[4]; |
| 1557 | } sg_blend_state; |
| 1558 | |
| 1559 | typedef struct sg_rasterizer_state { |
| 1560 | bool alpha_to_coverage_enabled; |
| 1561 | sg_cull_mode cull_mode; |
| 1562 | sg_face_winding face_winding; |
| 1563 | int sample_count; |
| 1564 | float depth_bias; |
| 1565 | float depth_bias_slope_scale; |
| 1566 | float depth_bias_clamp; |
| 1567 | } sg_rasterizer_state; |
| 1568 | |
| 1569 | typedef struct sg_pipeline_desc { |
| 1570 | uint32_t _start_canary; |
| 1571 | sg_layout_desc layout; |
| 1572 | sg_shader shader; |
| 1573 | sg_primitive_type primitive_type; |
| 1574 | sg_index_type index_type; |
| 1575 | sg_depth_stencil_state depth_stencil; |
| 1576 | sg_blend_state blend; |
| 1577 | sg_rasterizer_state rasterizer; |
| 1578 | const char* label; |
| 1579 | uint32_t _end_canary; |
| 1580 | } sg_pipeline_desc; |
| 1581 | |
| 1582 | /* |
| 1583 | sg_pass_desc |
| 1584 | |
| 1585 | Creation parameters for an sg_pass object, used as argument |
| 1586 | to the sg_make_pass() function. |
| 1587 | |
| 1588 | A pass object contains 1..4 color-attachments and none, or one, |
| 1589 | depth-stencil-attachment. Each attachment consists of |
| 1590 | an image, and two additional indices describing |
| 1591 | which subimage the pass will render: one mipmap index, and |
| 1592 | if the image is a cubemap, array-texture or 3D-texture, the |
| 1593 | face-index, array-layer or depth-slice. |
| 1594 | |
| 1595 | Pass images must fulfill the following requirements: |
| 1596 | |
| 1597 | All images must have: |
| 1598 | - been created as render target (sg_image_desc.render_target = true) |
| 1599 | - the same size |
| 1600 | - the same sample count |
| 1601 | |
| 1602 | In addition, all color-attachment images must have the same |
| 1603 | pixel format. |
| 1604 | */ |
| 1605 | typedef struct sg_attachment_desc { |
| 1606 | sg_image image; |
| 1607 | int mip_level; |
| 1608 | union { |
| 1609 | int face; |
| 1610 | int layer; |
| 1611 | int slice; |
| 1612 | }; |
| 1613 | } sg_attachment_desc; |
| 1614 | |
| 1615 | typedef struct sg_pass_desc { |
| 1616 | uint32_t _start_canary; |
| 1617 | sg_attachment_desc color_attachments[SG_MAX_COLOR_ATTACHMENTS]; |
| 1618 | sg_attachment_desc depth_stencil_attachment; |
| 1619 | const char* label; |
| 1620 | uint32_t _end_canary; |
| 1621 | } sg_pass_desc; |
| 1622 | |
| 1623 | /* |
| 1624 | sg_trace_hooks |
| 1625 | |
| 1626 | Installable callback function to keep track of the sokol_gfx calls, |
| 1627 | this is useful for debugging, or keeping track of resource creation |
| 1628 | and destruction. |
| 1629 | |
| 1630 | Trace hooks are installed with sg_install_trace_hooks(), this returns |
| 1631 | another sg_trace_hooks functions with the previous set of |
| 1632 | trace hook function pointers. These should be invoked by the |
| 1633 | new trace hooks to form a proper call chain. |
| 1634 | */ |
| 1635 | typedef struct sg_trace_hooks { |
| 1636 | void* user_data; |
| 1637 | void (*query_feature)(sg_feature feature, bool result, void* user_data); |
| 1638 | void (*reset_state_cache)(void* user_data); |
| 1639 | void (*make_buffer)(const sg_buffer_desc* desc, sg_buffer result, void* user_data); |
| 1640 | void (*make_image)(const sg_image_desc* desc, sg_image result, void* user_data); |
| 1641 | void (*make_shader)(const sg_shader_desc* desc, sg_shader result, void* user_data); |
| 1642 | void (*make_pipeline)(const sg_pipeline_desc* desc, sg_pipeline result, void* user_data); |
| 1643 | void (*make_pass)(const sg_pass_desc* desc, sg_pass result, void* user_data); |
| 1644 | void (*destroy_buffer)(sg_buffer buf, void* user_data); |
| 1645 | void (*destroy_image)(sg_image img, void* user_data); |
| 1646 | void (*destroy_shader)(sg_shader shd, void* user_data); |
| 1647 | void (*destroy_pipeline)(sg_pipeline pip, void* user_data); |
| 1648 | void (*destroy_pass)(sg_pass pass, void* user_data); |
| 1649 | void (*update_buffer)(sg_buffer buf, const void* data_ptr, int data_size, void* user_data); |
| 1650 | void (*update_image)(sg_image img, const sg_image_content* data, void* user_data); |
| 1651 | void (*append_buffer)(sg_buffer buf, const void* data_ptr, int data_size, int result, void* user_data); |
| 1652 | void (*begin_default_pass)(const sg_pass_action* pass_action, int width, int height, void* user_data); |
| 1653 | void (*begin_pass)(sg_pass pass, const sg_pass_action* pass_action, void* user_data); |
| 1654 | void (*apply_viewport)(int x, int y, int width, int height, bool origin_top_left, void* user_data); |
| 1655 | void (*apply_scissor_rect)(int x, int y, int width, int height, bool origin_top_left, void* user_data); |
| 1656 | void (*apply_pipeline)(sg_pipeline pip, void* user_data); |
| 1657 | void (*apply_bindings)(const sg_bindings* bindings, void* user_data); |
| 1658 | void (*apply_uniforms)(sg_shader_stage stage, int ub_index, const void* data, int num_bytes, void* user_data); |
| 1659 | void (*draw)(int base_element, int num_elements, int num_instances, void* user_data); |
| 1660 | void (*end_pass)(void* user_data); |
| 1661 | void (*commit)(void* user_data); |
| 1662 | void (*alloc_buffer)(sg_buffer result, void* user_data); |
| 1663 | void (*alloc_image)(sg_image result, void* user_data); |
| 1664 | void (*alloc_shader)(sg_shader result, void* user_data); |
| 1665 | void (*alloc_pipeline)(sg_pipeline result, void* user_data); |
| 1666 | void (*alloc_pass)(sg_pass result, void* user_data); |
| 1667 | void (*init_buffer)(sg_buffer buf_id, const sg_buffer_desc* desc, void* user_data); |
| 1668 | void (*init_image)(sg_image img_id, const sg_image_desc* desc, void* user_data); |
| 1669 | void (*init_shader)(sg_shader shd_id, const sg_shader_desc* desc, void* user_data); |
| 1670 | void (*init_pipeline)(sg_pipeline pip_id, const sg_pipeline_desc* desc, void* user_data); |
| 1671 | void (*init_pass)(sg_pass pass_id, const sg_pass_desc* desc, void* user_data); |
| 1672 | void (*fail_buffer)(sg_buffer buf_id, void* user_data); |
| 1673 | void (*fail_image)(sg_image img_id, void* user_data); |
| 1674 | void (*fail_shader)(sg_shader shd_id, void* user_data); |
| 1675 | void (*fail_pipeline)(sg_pipeline pip_id, void* user_data); |
| 1676 | void (*fail_pass)(sg_pass pass_id, void* user_data); |
| 1677 | void (*push_debug_group)(const char* name, void* user_data); |
| 1678 | void (*pop_debug_group)(void* user_data); |
| 1679 | void (*err_buffer_pool_exhausted)(void* user_data); |
| 1680 | void (*err_image_pool_exhausted)(void* user_data); |
| 1681 | void (*err_shader_pool_exhausted)(void* user_data); |
| 1682 | void (*err_pipeline_pool_exhausted)(void* user_data); |
| 1683 | void (*err_pass_pool_exhausted)(void* user_data); |
| 1684 | void (*err_context_mismatch)(void* user_data); |
| 1685 | void (*err_pass_invalid)(void* user_data); |
| 1686 | void (*err_draw_invalid)(void* user_data); |
| 1687 | void (*err_bindings_invalid)(void* user_data); |
| 1688 | } sg_trace_hooks; |
| 1689 | |
| 1690 | /* |
| 1691 | sg_buffer_info |
| 1692 | sg_image_info |
| 1693 | sg_shader_info |
| 1694 | sg_pipeline_info |
| 1695 | sg_pass_info |
| 1696 | |
| 1697 | These structs contain various internal resource attributes which |
| 1698 | might be useful for debug-inspection. Please don't rely on the |
| 1699 | actual content of those structs too much, as they are quite closely |
| 1700 | tied to sokol_gfx.h internals and may change more frequently than |
| 1701 | the other public API elements. |
| 1702 | |
| 1703 | The *_info structs are used as the return values of the following functions: |
| 1704 | |
| 1705 | sg_query_buffer_info() |
| 1706 | sg_query_image_info() |
| 1707 | sg_query_shader_info() |
| 1708 | sg_query_pipeline_info() |
| 1709 | sg_query_pass_info() |
| 1710 | */ |
| 1711 | typedef struct sg_slot_info { |
| 1712 | sg_resource_state state; /* the current state of this resource slot */ |
| 1713 | uint32_t res_id; /* type-neutral resource if (e.g. sg_buffer.id) */ |
| 1714 | uint32_t ctx_id; /* the context this resource belongs to */ |
| 1715 | } sg_slot_info; |
| 1716 | |
| 1717 | typedef struct sg_buffer_info { |
| 1718 | sg_slot_info slot; /* resource pool slot info */ |
| 1719 | uint32_t update_frame_index; /* frame index of last sg_update_buffer() */ |
| 1720 | uint32_t append_frame_index; /* frame index of last sg_append_buffer() */ |
| 1721 | int append_pos; /* current position in buffer for sg_append_buffer() */ |
| 1722 | bool append_overflow; /* is buffer in overflow state (due to sg_append_buffer) */ |
| 1723 | int num_slots; /* number of renaming-slots for dynamically updated buffers */ |
| 1724 | int active_slot; /* currently active write-slot for dynamically updated buffers */ |
| 1725 | } sg_buffer_info; |
| 1726 | |
| 1727 | typedef struct sg_image_info { |
| 1728 | sg_slot_info slot; /* resource pool slot info */ |
| 1729 | uint32_t upd_frame_index; /* frame index of last sg_update_image() */ |
| 1730 | int num_slots; /* number of renaming-slots for dynamically updated images */ |
| 1731 | int active_slot; /* currently active write-slot for dynamically updated images */ |
| 1732 | } sg_image_info; |
| 1733 | |
| 1734 | typedef struct sg_shader_info { |
| 1735 | sg_slot_info slot; /* resoure pool slot info */ |
| 1736 | } sg_shader_info; |
| 1737 | |
| 1738 | typedef struct sg_pipeline_info { |
| 1739 | sg_slot_info slot; /* resource pool slot info */ |
| 1740 | } sg_pipeline_info; |
| 1741 | |
| 1742 | typedef struct sg_pass_info { |
| 1743 | sg_slot_info slot; /* resource pool slot info */ |
| 1744 | } sg_pass_info; |
| 1745 | |
| 1746 | /* |
| 1747 | sg_desc |
| 1748 | |
| 1749 | The sg_desc struct contains configuration values for sokol_gfx, |
| 1750 | it is used as parameter to the sg_setup() call. |
| 1751 | |
| 1752 | The default configuration is: |
| 1753 | |
| 1754 | .buffer_pool_size: 128 |
| 1755 | .image_pool_size: 128 |
| 1756 | .shader_pool_size: 32 |
| 1757 | .pipeline_pool_size: 64 |
| 1758 | .pass_pool_size: 16 |
| 1759 | .context_pool_size: 16 |
| 1760 | |
| 1761 | GL specific: |
| 1762 | .gl_force_gles2 |
| 1763 | if this is true the GL backend will act in "GLES2 fallback mode" even |
| 1764 | when compiled with SOKOL_GLES3, this is useful to fall back |
| 1765 | to traditional WebGL if a browser doesn't support a WebGL2 context |
| 1766 | |
| 1767 | Metal specific: |
| 1768 | (NOTE: All Objective-C object references are transferred through |
| 1769 | a bridged (const void*) to sokol_gfx, which will use a unretained |
| 1770 | bridged cast (__bridged id<xxx>) to retrieve the Objective-C |
| 1771 | references back. Since the bridge cast is unretained, the caller |
| 1772 | must hold a strong reference to the Objective-C object for the |
| 1773 | duration of the sokol_gfx call! |
| 1774 | |
| 1775 | .mtl_device |
| 1776 | a pointer to the MTLDevice object |
| 1777 | .mtl_renderpass_descriptor_cb |
| 1778 | a C callback function to obtain the MTLRenderPassDescriptor for the |
| 1779 | current frame when rendering to the default framebuffer, will be called |
| 1780 | in sg_begin_default_pass() |
| 1781 | .mtl_drawable_cb |
| 1782 | a C callback function to obtain a MTLDrawable for the current |
| 1783 | frame when rendering to the default framebuffer, will be called in |
| 1784 | sg_end_pass() of the default pass |
| 1785 | .mtl_global_uniform_buffer_size |
| 1786 | the size of the global uniform buffer in bytes, this must be big |
| 1787 | enough to hold all uniform block updates for a single frame, |
| 1788 | the default value is 4 MByte (4 * 1024 * 1024) |
| 1789 | .mtl_sampler_cache_size |
| 1790 | the number of slots in the sampler cache, the Metal backend |
| 1791 | will share texture samplers with the same state in this |
| 1792 | cache, the default value is 64 |
| 1793 | |
| 1794 | D3D11 specific: |
| 1795 | .d3d11_device |
| 1796 | a pointer to the ID3D11Device object, this must have been created |
| 1797 | before sg_setup() is called |
| 1798 | .d3d11_device_context |
| 1799 | a pointer to the ID3D11DeviceContext object |
| 1800 | .d3d11_render_target_view_cb |
| 1801 | a C callback function to obtain a pointer to the current |
| 1802 | ID3D11RenderTargetView object of the default framebuffer, |
| 1803 | this function will be called in sg_begin_pass() when rendering |
| 1804 | to the default framebuffer |
| 1805 | .d3d11_depth_stencil_view_cb |
| 1806 | a C callback function to obtain a pointer to the current |
| 1807 | ID3D11DepthStencilView object of the default framebuffer, |
| 1808 | this function will be called in sg_begin_pass() when rendering |
| 1809 | to the default framebuffer |
| 1810 | */ |
| 1811 | typedef struct sg_desc { |
| 1812 | uint32_t _start_canary; |
| 1813 | int buffer_pool_size; |
| 1814 | int image_pool_size; |
| 1815 | int shader_pool_size; |
| 1816 | int pipeline_pool_size; |
| 1817 | int pass_pool_size; |
| 1818 | int context_pool_size; |
| 1819 | /* GL specific */ |
| 1820 | bool gl_force_gles2; |
| 1821 | /* Metal-specific */ |
| 1822 | const void* mtl_device; |
| 1823 | const void* (*mtl_renderpass_descriptor_cb)(void); |
| 1824 | const void* (*mtl_drawable_cb)(void); |
| 1825 | int mtl_global_uniform_buffer_size; |
| 1826 | int mtl_sampler_cache_size; |
| 1827 | /* D3D11-specific */ |
| 1828 | const void* d3d11_device; |
| 1829 | const void* d3d11_device_context; |
| 1830 | const void* (*d3d11_render_target_view_cb)(void); |
| 1831 | const void* (*d3d11_depth_stencil_view_cb)(void); |
| 1832 | uint32_t _end_canary; |
| 1833 | } sg_desc; |
| 1834 | |
| 1835 | /* setup and misc functions */ |
| 1836 | SOKOL_API_DECL void sg_setup(const sg_desc* desc); |
| 1837 | SOKOL_API_DECL void sg_shutdown(void); |
| 1838 | SOKOL_API_DECL bool sg_isvalid(void); |
| 1839 | SOKOL_API_DECL sg_desc sg_query_desc(void); |
| 1840 | SOKOL_API_DECL sg_backend sg_query_backend(void); |
| 1841 | SOKOL_API_DECL bool sg_query_feature(sg_feature feature); |
| 1842 | SOKOL_API_DECL void sg_reset_state_cache(void); |
| 1843 | SOKOL_API_DECL sg_trace_hooks sg_install_trace_hooks(const sg_trace_hooks* trace_hooks); |
| 1844 | SOKOL_API_DECL void sg_push_debug_group(const char* name); |
| 1845 | SOKOL_API_DECL void sg_pop_debug_group(void); |
| 1846 | |
| 1847 | /* resource creation, destruction and updating */ |
| 1848 | SOKOL_API_DECL sg_buffer sg_make_buffer(const sg_buffer_desc* desc); |
| 1849 | SOKOL_API_DECL sg_image sg_make_image(const sg_image_desc* desc); |
| 1850 | SOKOL_API_DECL sg_shader sg_make_shader(const sg_shader_desc* desc); |
| 1851 | SOKOL_API_DECL sg_pipeline sg_make_pipeline(const sg_pipeline_desc* desc); |
| 1852 | SOKOL_API_DECL sg_pass sg_make_pass(const sg_pass_desc* desc); |
| 1853 | SOKOL_API_DECL void sg_destroy_buffer(sg_buffer buf); |
| 1854 | SOKOL_API_DECL void sg_destroy_image(sg_image img); |
| 1855 | SOKOL_API_DECL void sg_destroy_shader(sg_shader shd); |
| 1856 | SOKOL_API_DECL void sg_destroy_pipeline(sg_pipeline pip); |
| 1857 | SOKOL_API_DECL void sg_destroy_pass(sg_pass pass); |
| 1858 | SOKOL_API_DECL void sg_update_buffer(sg_buffer buf, const void* data_ptr, int data_size); |
| 1859 | SOKOL_API_DECL void sg_update_image(sg_image img, const sg_image_content* data); |
| 1860 | SOKOL_API_DECL int sg_append_buffer(sg_buffer buf, const void* data_ptr, int data_size); |
| 1861 | SOKOL_API_DECL bool sg_query_buffer_overflow(sg_buffer buf); |
| 1862 | |
| 1863 | /* get resource state (initial, alloc, valid, failed) */ |
| 1864 | SOKOL_API_DECL sg_resource_state sg_query_buffer_state(sg_buffer buf); |
| 1865 | SOKOL_API_DECL sg_resource_state sg_query_image_state(sg_image img); |
| 1866 | SOKOL_API_DECL sg_resource_state sg_query_shader_state(sg_shader shd); |
| 1867 | SOKOL_API_DECL sg_resource_state sg_query_pipeline_state(sg_pipeline pip); |
| 1868 | SOKOL_API_DECL sg_resource_state sg_query_pass_state(sg_pass pass); |
| 1869 | |
| 1870 | /* rendering functions */ |
| 1871 | SOKOL_API_DECL void sg_begin_default_pass(const sg_pass_action* pass_action, int width, int height); |
| 1872 | SOKOL_API_DECL void sg_begin_pass(sg_pass pass, const sg_pass_action* pass_action); |
| 1873 | SOKOL_API_DECL void sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left); |
| 1874 | SOKOL_API_DECL void sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left); |
| 1875 | SOKOL_API_DECL void sg_apply_pipeline(sg_pipeline pip); |
| 1876 | SOKOL_API_DECL void sg_apply_bindings(const sg_bindings* bindings); |
| 1877 | SOKOL_API_DECL void sg_apply_uniforms(sg_shader_stage stage, int ub_index, const void* data, int num_bytes); |
| 1878 | SOKOL_API_DECL void sg_draw(int base_element, int num_elements, int num_instances); |
| 1879 | SOKOL_API_DECL void sg_end_pass(void); |
| 1880 | SOKOL_API_DECL void sg_commit(void); |
| 1881 | |
| 1882 | /* separate resource allocation and initialization (for async setup) */ |
| 1883 | SOKOL_API_DECL sg_buffer sg_alloc_buffer(void); |
| 1884 | SOKOL_API_DECL sg_image sg_alloc_image(void); |
| 1885 | SOKOL_API_DECL sg_shader sg_alloc_shader(void); |
| 1886 | SOKOL_API_DECL sg_pipeline sg_alloc_pipeline(void); |
| 1887 | SOKOL_API_DECL sg_pass sg_alloc_pass(void); |
| 1888 | SOKOL_API_DECL void sg_init_buffer(sg_buffer buf_id, const sg_buffer_desc* desc); |
| 1889 | SOKOL_API_DECL void sg_init_image(sg_image img_id, const sg_image_desc* desc); |
| 1890 | SOKOL_API_DECL void sg_init_shader(sg_shader shd_id, const sg_shader_desc* desc); |
| 1891 | SOKOL_API_DECL void sg_init_pipeline(sg_pipeline pip_id, const sg_pipeline_desc* desc); |
| 1892 | SOKOL_API_DECL void sg_init_pass(sg_pass pass_id, const sg_pass_desc* desc); |
| 1893 | SOKOL_API_DECL void sg_fail_buffer(sg_buffer buf_id); |
| 1894 | SOKOL_API_DECL void sg_fail_image(sg_image img_id); |
| 1895 | SOKOL_API_DECL void sg_fail_shader(sg_shader shd_id); |
| 1896 | SOKOL_API_DECL void sg_fail_pipeline(sg_pipeline pip_id); |
| 1897 | SOKOL_API_DECL void sg_fail_pass(sg_pass pass_id); |
| 1898 | |
| 1899 | /* get internal resource attributes */ |
| 1900 | SOKOL_API_DECL sg_buffer_info sg_query_buffer_info(sg_buffer buf); |
| 1901 | SOKOL_API_DECL sg_image_info sg_query_image_info(sg_image img); |
| 1902 | SOKOL_API_DECL sg_shader_info sg_query_shader_info(sg_shader shd); |
| 1903 | SOKOL_API_DECL sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip); |
| 1904 | SOKOL_API_DECL sg_pass_info sg_query_pass_info(sg_pass pass); |
| 1905 | |
| 1906 | /* rendering contexts (optional) */ |
| 1907 | SOKOL_API_DECL sg_context sg_setup_context(void); |
| 1908 | SOKOL_API_DECL void sg_activate_context(sg_context ctx_id); |
| 1909 | SOKOL_API_DECL void sg_discard_context(sg_context ctx_id); |
| 1910 | |
| 1911 | /* deprecated structs and functions */ |
| 1912 | #ifndef SOKOL_NO_DEPRECATED |
| 1913 | typedef struct sg_draw_state { |
| 1914 | uint32_t _start_canary; |
| 1915 | sg_pipeline pipeline; |
| 1916 | sg_buffer vertex_buffers[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 1917 | int vertex_buffer_offsets[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 1918 | sg_buffer index_buffer; |
| 1919 | int index_buffer_offset; |
| 1920 | sg_image vs_images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 1921 | sg_image fs_images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 1922 | uint32_t _end_canary; |
| 1923 | } sg_draw_state; |
| 1924 | SOKOL_API_DECL void sg_apply_draw_state(const sg_draw_state* ds); |
| 1925 | SOKOL_API_DECL void sg_apply_uniform_block(sg_shader_stage stage, int ub_index, const void* data, int num_bytes); |
| 1926 | #endif |
| 1927 | |
| 1928 | #ifdef _MSC_VER |
| 1929 | #pragma warning(pop) |
| 1930 | #endif |
| 1931 | #ifdef __cplusplus |
| 1932 | } /* extern "C" */ |
| 1933 | #endif |
| 1934 | |
| 1935 | /*--- IMPLEMENTATION ---------------------------------------------------------*/ |
| 1936 | #ifdef SOKOL_IMPL |
| 1937 | #define SOKOL_GFX_IMPL_INCLUDED (1) |
| 1938 | |
| 1939 | #if !(defined(SOKOL_GLCORE33)||defined(SOKOL_GLES2)||defined(SOKOL_GLES3)||defined(SOKOL_D3D11)||defined(SOKOL_METAL)||defined(SOKOL_DUMMY_BACKEND)) |
| 1940 | #error "Please select a backend with SOKOL_GLCORE33, SOKOL_GLES2, SOKOL_GLES3, SOKOL_D3D11, SOKOL_METAL or SOKOL_DUMMY_BACKEND" |
| 1941 | #endif |
| 1942 | #include <string.h> /* memset */ |
| 1943 | #include <float.h> /* FLT_MAX */ |
| 1944 | |
| 1945 | #ifndef SOKOL_API_IMPL |
| 1946 | #define SOKOL_API_IMPL |
| 1947 | #endif |
| 1948 | #ifndef SOKOL_DEBUG |
| 1949 | #ifndef NDEBUG |
| 1950 | #define SOKOL_DEBUG (1) |
| 1951 | #endif |
| 1952 | #endif |
| 1953 | #ifndef SOKOL_ASSERT |
| 1954 | #include <assert.h> |
| 1955 | #define SOKOL_ASSERT(c) assert(c) |
| 1956 | #endif |
| 1957 | #ifndef SOKOL_VALIDATE_BEGIN |
| 1958 | #define SOKOL_VALIDATE_BEGIN() _sg_validate_begin() |
| 1959 | #endif |
| 1960 | #ifndef SOKOL_VALIDATE |
| 1961 | #define SOKOL_VALIDATE(cond, err) _sg_validate((cond), err) |
| 1962 | #endif |
| 1963 | #ifndef SOKOL_VALIDATE_END |
| 1964 | #define SOKOL_VALIDATE_END() _sg_validate_end() |
| 1965 | #endif |
| 1966 | #ifndef SOKOL_UNREACHABLE |
| 1967 | #define SOKOL_UNREACHABLE SOKOL_ASSERT(false) |
| 1968 | #endif |
| 1969 | #ifndef SOKOL_MALLOC |
| 1970 | #include <stdlib.h> |
| 1971 | #define SOKOL_MALLOC(s) malloc(s) |
| 1972 | #define SOKOL_FREE(p) free(p) |
| 1973 | #endif |
| 1974 | #ifndef SOKOL_LOG |
| 1975 | #ifdef SOKOL_DEBUG |
| 1976 | #include <stdio.h> |
| 1977 | #define SOKOL_LOG(s) { SOKOL_ASSERT(s); puts(s); } |
| 1978 | #else |
| 1979 | #define SOKOL_LOG(s) |
| 1980 | #endif |
| 1981 | #endif |
| 1982 | |
| 1983 | #ifndef _SOKOL_PRIVATE |
| 1984 | #if defined(__GNUC__) |
| 1985 | #define _SOKOL_PRIVATE __attribute__((unused)) static |
| 1986 | #else |
| 1987 | #define _SOKOL_PRIVATE static |
| 1988 | #endif |
| 1989 | #endif |
| 1990 | |
| 1991 | #ifndef _SOKOL_UNUSED |
| 1992 | #define _SOKOL_UNUSED(x) (void)(x) |
| 1993 | #endif |
| 1994 | |
| 1995 | #if defined(SOKOL_TRACE_HOOKS) |
| 1996 | #define _SG_TRACE_ARGS(fn, ...) if (_sg.hooks.fn) { _sg.hooks.fn(__VA_ARGS__, _sg.hooks.user_data); } |
| 1997 | #define _SG_TRACE_NOARGS(fn) if (_sg.hooks.fn) { _sg.hooks.fn(_sg.hooks.user_data); } |
| 1998 | #else |
| 1999 | #define _SG_TRACE_ARGS(fn, ...) |
| 2000 | #define _SG_TRACE_NOARGS(fn) |
| 2001 | #endif |
| 2002 | |
| 2003 | /* default clear values */ |
| 2004 | #ifndef SG_DEFAULT_CLEAR_RED |
| 2005 | #define SG_DEFAULT_CLEAR_RED (0.5f) |
| 2006 | #endif |
| 2007 | #ifndef SG_DEFAULT_CLEAR_GREEN |
| 2008 | #define SG_DEFAULT_CLEAR_GREEN (0.5f) |
| 2009 | #endif |
| 2010 | #ifndef SG_DEFAULT_CLEAR_BLUE |
| 2011 | #define SG_DEFAULT_CLEAR_BLUE (0.5f) |
| 2012 | #endif |
| 2013 | #ifndef SG_DEFAULT_CLEAR_ALPHA |
| 2014 | #define SG_DEFAULT_CLEAR_ALPHA (1.0f) |
| 2015 | #endif |
| 2016 | #ifndef SG_DEFAULT_CLEAR_DEPTH |
| 2017 | #define SG_DEFAULT_CLEAR_DEPTH (1.0f) |
| 2018 | #endif |
| 2019 | #ifndef SG_DEFAULT_CLEAR_STENCIL |
| 2020 | #define SG_DEFAULT_CLEAR_STENCIL (0) |
| 2021 | #endif |
| 2022 | |
| 2023 | #if defined(SOKOL_GLCORE33) || defined(SOKOL_GLES2) || defined(SOKOL_GLES3) |
| 2024 | #ifndef GL_UNSIGNED_INT_2_10_10_10_REV |
| 2025 | #define GL_UNSIGNED_INT_2_10_10_10_REV 0x8368 |
| 2026 | #endif |
| 2027 | #ifndef GL_UNSIGNED_INT_24_8 |
| 2028 | #define GL_UNSIGNED_INT_24_8 0x84FA |
| 2029 | #endif |
| 2030 | #ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT |
| 2031 | #define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE |
| 2032 | #endif |
| 2033 | #ifndef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT |
| 2034 | #define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF |
| 2035 | #endif |
| 2036 | #ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT |
| 2037 | #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1 |
| 2038 | #endif |
| 2039 | #ifndef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT |
| 2040 | #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2 |
| 2041 | #endif |
| 2042 | #ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT |
| 2043 | #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3 |
| 2044 | #endif |
| 2045 | #ifndef GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG |
| 2046 | #define GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01 |
| 2047 | #endif |
| 2048 | #ifndef GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG |
| 2049 | #define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00 |
| 2050 | #endif |
| 2051 | #ifndef GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG |
| 2052 | #define GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03 |
| 2053 | #endif |
| 2054 | #ifndef GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG |
| 2055 | #define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02 |
| 2056 | #endif |
| 2057 | #ifndef GL_COMPRESSED_RGB8_ETC2 |
| 2058 | #define GL_COMPRESSED_RGB8_ETC2 0x9274 |
| 2059 | #endif |
| 2060 | #ifndef GL_COMPRESSED_SRGB8_ETC2 |
| 2061 | #define GL_COMPRESSED_SRGB8_ETC2 0x9275 |
| 2062 | #endif |
| 2063 | #ifndef GL_DEPTH24_STENCIL8 |
| 2064 | #define GL_DEPTH24_STENCIL8 0x88F0 |
| 2065 | #endif |
| 2066 | #ifndef GL_HALF_FLOAT |
| 2067 | #define GL_HALF_FLOAT 0x140B |
| 2068 | #endif |
| 2069 | #ifndef GL_DEPTH_STENCIL |
| 2070 | #define GL_DEPTH_STENCIL 0x84F9 |
| 2071 | #endif |
| 2072 | #ifndef GL_LUMINANCE |
| 2073 | #define GL_LUMINANCE 0x1909 |
| 2074 | #endif |
| 2075 | #ifdef SOKOL_GLES2 |
| 2076 | # ifdef GL_ANGLE_instanced_arrays |
| 2077 | # define SOKOL_INSTANCING_ENABLED |
| 2078 | # define glDrawArraysInstanced(mode, first, count, instancecount) glDrawArraysInstancedANGLE(mode, first, count, instancecount) |
| 2079 | # define glDrawElementsInstanced(mode, count, type, indices, instancecount) glDrawElementsInstancedANGLE(mode, count, type, indices, instancecount) |
| 2080 | # define glVertexAttribDivisor(index, divisor) glVertexAttribDivisorANGLE(index, divisor) |
| 2081 | # elif defined(GL_EXT_draw_instanced) && defined(GL_EXT_instanced_arrays) |
| 2082 | # define SOKOL_INSTANCING_ENABLED |
| 2083 | # define glDrawArraysInstanced(mode, first, count, instancecount) glDrawArraysInstancedEXT(mode, first, count, instancecount) |
| 2084 | # define glDrawElementsInstanced(mode, count, type, indices, instancecount) glDrawElementsInstancedEXT(mode, count, type, indices, instancecount) |
| 2085 | # define glVertexAttribDivisor(index, divisor) glVertexAttribDivisorEXT(index, divisor) |
| 2086 | # else |
| 2087 | # define SOKOL_GLES2_INSTANCING_ERROR "Select GL_ANGLE_instanced_arrays or (GL_EXT_draw_instanced & GL_EXT_instanced_arrays) to enable instancing in GLES2" |
| 2088 | # define glDrawArraysInstanced(mode, first, count, instancecount) SOKOL_ASSERT(0 && SOKOL_GLES2_INSTANCING_ERROR) |
| 2089 | # define glDrawElementsInstanced(mode, count, type, indices, instancecount) SOKOL_ASSERT(0 && SOKOL_GLES2_INSTANCING_ERROR) |
| 2090 | # define glVertexAttribDivisor(index, divisor) SOKOL_ASSERT(0 && SOKOL_GLES2_INSTANCING_ERROR) |
| 2091 | # endif |
| 2092 | #else |
| 2093 | # define SOKOL_INSTANCING_ENABLED |
| 2094 | #endif |
| 2095 | #define _SG_GL_CHECK_ERROR() { SOKOL_ASSERT(glGetError() == GL_NO_ERROR); } |
| 2096 | |
| 2097 | #elif defined(SOKOL_D3D11) |
| 2098 | #ifndef D3D11_NO_HELPERS |
| 2099 | #define D3D11_NO_HELPERS |
| 2100 | #endif |
| 2101 | #ifndef CINTERFACE |
| 2102 | #define CINTERFACE |
| 2103 | #endif |
| 2104 | #ifndef COBJMACROS |
| 2105 | #define COBJMACROS |
| 2106 | #endif |
| 2107 | #ifndef WIN32_LEAN_AND_MEAN |
| 2108 | #define WIN32_LEAN_AND_MEAN |
| 2109 | #endif |
| 2110 | #include <windows.h> |
| 2111 | #include <d3d11.h> |
| 2112 | #include <d3dcompiler.h> |
| 2113 | #if (defined(WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) |
| 2114 | #pragma comment (lib, "WindowsApp.lib") |
| 2115 | #else |
| 2116 | #pragma comment (lib, "user32.lib") |
| 2117 | #pragma comment (lib, "dxgi.lib") |
| 2118 | #pragma comment (lib, "d3d11.lib") |
| 2119 | #pragma comment (lib, "dxguid.lib") |
| 2120 | #endif |
| 2121 | #elif defined(SOKOL_METAL) |
| 2122 | #if !__has_feature(objc_arc) |
| 2123 | #error "Please enable ARC when using the Metal backend" |
| 2124 | #endif |
| 2125 | #include <TargetConditionals.h> |
| 2126 | #import <Metal/Metal.h> |
| 2127 | #endif |
| 2128 | |
| 2129 | #ifdef _MSC_VER |
| 2130 | #pragma warning(push) |
| 2131 | #pragma warning(disable:4201) /* nonstandard extension used: nameless struct/union */ |
| 2132 | #pragma warning(disable:4115) /* named type definition in parentheses */ |
| 2133 | #pragma warning(disable:4505) /* unreferenced local function has been removed */ |
| 2134 | #endif |
| 2135 | |
| 2136 | /*=== PRIVATE DECLS ==========================================================*/ |
| 2137 | |
| 2138 | /* resource pool slots */ |
| 2139 | typedef struct { |
| 2140 | uint32_t id; |
| 2141 | uint32_t ctx_id; |
| 2142 | sg_resource_state state; |
| 2143 | } _sg_slot_t; |
| 2144 | |
| 2145 | /* constants */ |
| 2146 | enum { |
| 2147 | _SG_STRING_SIZE = 16, |
| 2148 | _SG_SLOT_SHIFT = 16, |
| 2149 | _SG_SLOT_MASK = (1<<_SG_SLOT_SHIFT)-1, |
| 2150 | _SG_MAX_POOL_SIZE = (1<<_SG_SLOT_SHIFT), |
| 2151 | _SG_DEFAULT_BUFFER_POOL_SIZE = 128, |
| 2152 | _SG_DEFAULT_IMAGE_POOL_SIZE = 128, |
| 2153 | _SG_DEFAULT_SHADER_POOL_SIZE = 32, |
| 2154 | _SG_DEFAULT_PIPELINE_POOL_SIZE = 64, |
| 2155 | _SG_DEFAULT_PASS_POOL_SIZE = 16, |
| 2156 | _SG_DEFAULT_CONTEXT_POOL_SIZE = 16, |
| 2157 | _SG_MTL_DEFAULT_UB_SIZE = 4 * 1024 * 1024, |
| 2158 | _SG_MTL_DEFAULT_SAMPLER_CACHE_CAPACITY = 64, |
| 2159 | }; |
| 2160 | |
| 2161 | /* fixed-size string */ |
| 2162 | typedef struct { |
| 2163 | char buf[_SG_STRING_SIZE]; |
| 2164 | } _sg_str_t; |
| 2165 | |
| 2166 | /* helper macros */ |
| 2167 | #define _sg_def(val, def) (((val) == 0) ? (def) : (val)) |
| 2168 | #define _sg_def_flt(val, def) (((val) == 0.0f) ? (def) : (val)) |
| 2169 | #define _sg_min(a,b) ((a<b)?a:b) |
| 2170 | #define _sg_max(a,b) ((a>b)?a:b) |
| 2171 | #define _sg_clamp(v,v0,v1) ((v<v0)?(v0):((v>v1)?(v1):(v))) |
| 2172 | #define _sg_fequal(val,cmp,delta) (((val-cmp)> -delta)&&((val-cmp)<delta)) |
| 2173 | |
| 2174 | /*=== DUMMY BACKEND DECLARATIONS =============================================*/ |
| 2175 | #if defined(SOKOL_DUMMY_BACKEND) |
| 2176 | typedef struct { |
| 2177 | _sg_slot_t slot; |
| 2178 | int size; |
| 2179 | int append_pos; |
| 2180 | bool append_overflow; |
| 2181 | sg_buffer_type type; |
| 2182 | sg_usage usage; |
| 2183 | uint32_t update_frame_index; |
| 2184 | uint32_t append_frame_index; |
| 2185 | int num_slots; |
| 2186 | int active_slot; |
| 2187 | } _sg_buffer_t; |
| 2188 | |
| 2189 | typedef struct { |
| 2190 | _sg_slot_t slot; |
| 2191 | sg_image_type type; |
| 2192 | bool render_target; |
| 2193 | int width; |
| 2194 | int height; |
| 2195 | int depth; |
| 2196 | int num_mipmaps; |
| 2197 | sg_usage usage; |
| 2198 | sg_pixel_format pixel_format; |
| 2199 | int sample_count; |
| 2200 | sg_filter min_filter; |
| 2201 | sg_filter mag_filter; |
| 2202 | sg_wrap wrap_u; |
| 2203 | sg_wrap wrap_v; |
| 2204 | sg_wrap wrap_w; |
| 2205 | uint32_t max_anisotropy; |
| 2206 | uint32_t upd_frame_index; |
| 2207 | int num_slots; |
| 2208 | int active_slot; |
| 2209 | } _sg_image_t; |
| 2210 | |
| 2211 | typedef struct { |
| 2212 | int size; |
| 2213 | } _sg_uniform_block_t; |
| 2214 | |
| 2215 | typedef struct { |
| 2216 | sg_image_type type; |
| 2217 | } _sg_shader_image_t; |
| 2218 | |
| 2219 | typedef struct { |
| 2220 | int num_uniform_blocks; |
| 2221 | int num_images; |
| 2222 | _sg_uniform_block_t uniform_blocks[SG_MAX_SHADERSTAGE_UBS]; |
| 2223 | _sg_shader_image_t images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 2224 | } _sg_shader_stage_t; |
| 2225 | |
| 2226 | typedef struct { |
| 2227 | _sg_slot_t slot; |
| 2228 | _sg_shader_stage_t stage[SG_NUM_SHADER_STAGES]; |
| 2229 | } _sg_shader_t; |
| 2230 | |
| 2231 | typedef struct { |
| 2232 | _sg_slot_t slot; |
| 2233 | _sg_shader_t* shader; |
| 2234 | sg_shader shader_id; |
| 2235 | bool vertex_layout_valid[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 2236 | int color_attachment_count; |
| 2237 | sg_pixel_format color_format; |
| 2238 | sg_pixel_format depth_format; |
| 2239 | int sample_count; |
| 2240 | float depth_bias; |
| 2241 | float depth_bias_slope_scale; |
| 2242 | float depth_bias_clamp; |
| 2243 | sg_index_type index_type; |
| 2244 | float blend_color[4]; |
| 2245 | } _sg_pipeline_t; |
| 2246 | |
| 2247 | typedef struct { |
| 2248 | _sg_image_t* image; |
| 2249 | sg_image image_id; |
| 2250 | int mip_level; |
| 2251 | int slice; |
| 2252 | } _sg_attachment_t; |
| 2253 | |
| 2254 | typedef struct { |
| 2255 | _sg_slot_t slot; |
| 2256 | int num_color_atts; |
| 2257 | _sg_attachment_t color_atts[SG_MAX_COLOR_ATTACHMENTS]; |
| 2258 | _sg_attachment_t ds_att; |
| 2259 | } _sg_pass_t; |
| 2260 | |
| 2261 | typedef struct { |
| 2262 | _sg_slot_t slot; |
| 2263 | } _sg_context_t; |
| 2264 | |
| 2265 | /*== GL BACKEND DECLARATIONS =================================================*/ |
| 2266 | #elif defined(SOKOL_GLCORE33) || defined(SOKOL_GLES2) || defined(SOKOL_GLES3) |
| 2267 | typedef struct { |
| 2268 | _sg_slot_t slot; |
| 2269 | int size; |
| 2270 | int append_pos; |
| 2271 | bool append_overflow; |
| 2272 | sg_buffer_type type; |
| 2273 | sg_usage usage; |
| 2274 | uint32_t update_frame_index; |
| 2275 | uint32_t append_frame_index; |
| 2276 | int num_slots; |
| 2277 | int active_slot; |
| 2278 | GLuint gl_buf[SG_NUM_INFLIGHT_FRAMES]; |
| 2279 | bool ext_buffers; /* if true, external buffers were injected with sg_buffer_desc.gl_buffers */ |
| 2280 | } _sg_buffer_t; |
| 2281 | |
| 2282 | typedef struct { |
| 2283 | _sg_slot_t slot; |
| 2284 | sg_image_type type; |
| 2285 | bool render_target; |
| 2286 | int width; |
| 2287 | int height; |
| 2288 | int depth; |
| 2289 | int num_mipmaps; |
| 2290 | sg_usage usage; |
| 2291 | sg_pixel_format pixel_format; |
| 2292 | int sample_count; |
| 2293 | sg_filter min_filter; |
| 2294 | sg_filter mag_filter; |
| 2295 | sg_wrap wrap_u; |
| 2296 | sg_wrap wrap_v; |
| 2297 | sg_wrap wrap_w; |
| 2298 | uint32_t max_anisotropy; |
| 2299 | GLenum gl_target; |
| 2300 | GLuint gl_depth_render_buffer; |
| 2301 | GLuint gl_msaa_render_buffer; |
| 2302 | uint32_t upd_frame_index; |
| 2303 | int num_slots; |
| 2304 | int active_slot; |
| 2305 | GLuint gl_tex[SG_NUM_INFLIGHT_FRAMES]; |
| 2306 | bool ext_textures; /* if true, external textures were injected with sg_image_desc.gl_textures */ |
| 2307 | } _sg_image_t; |
| 2308 | |
| 2309 | typedef struct { |
| 2310 | GLint gl_loc; |
| 2311 | sg_uniform_type type; |
| 2312 | uint8_t count; |
| 2313 | uint16_t offset; |
| 2314 | } _sg_uniform_t; |
| 2315 | |
| 2316 | typedef struct { |
| 2317 | int size; |
| 2318 | int num_uniforms; |
| 2319 | _sg_uniform_t uniforms[SG_MAX_UB_MEMBERS]; |
| 2320 | } _sg_uniform_block_t; |
| 2321 | |
| 2322 | typedef struct { |
| 2323 | sg_image_type type; |
| 2324 | GLint gl_loc; |
| 2325 | int gl_tex_slot; |
| 2326 | } _sg_shader_image_t; |
| 2327 | |
| 2328 | typedef struct { |
| 2329 | _sg_str_t name; |
| 2330 | } _sg_shader_attr_t; |
| 2331 | |
| 2332 | typedef struct { |
| 2333 | int num_uniform_blocks; |
| 2334 | int num_images; |
| 2335 | _sg_uniform_block_t uniform_blocks[SG_MAX_SHADERSTAGE_UBS]; |
| 2336 | _sg_shader_image_t images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 2337 | } _sg_shader_stage_t; |
| 2338 | |
| 2339 | typedef struct { |
| 2340 | _sg_slot_t slot; |
| 2341 | GLuint gl_prog; |
| 2342 | _sg_shader_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 2343 | _sg_shader_stage_t stage[SG_NUM_SHADER_STAGES]; |
| 2344 | } _sg_shader_t; |
| 2345 | |
| 2346 | typedef struct { |
| 2347 | int8_t vb_index; /* -1 if attr is not enabled */ |
| 2348 | int8_t divisor; /* -1 if not initialized */ |
| 2349 | uint8_t stride; |
| 2350 | uint8_t size; |
| 2351 | uint8_t normalized; |
| 2352 | int offset; |
| 2353 | GLenum type; |
| 2354 | } _sg_gl_attr_t; |
| 2355 | |
| 2356 | typedef struct { |
| 2357 | _sg_slot_t slot; |
| 2358 | _sg_shader_t* shader; |
| 2359 | sg_shader shader_id; |
| 2360 | sg_primitive_type primitive_type; |
| 2361 | sg_index_type index_type; |
| 2362 | bool vertex_layout_valid[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 2363 | int color_attachment_count; |
| 2364 | sg_pixel_format color_format; |
| 2365 | sg_pixel_format depth_format; |
| 2366 | int sample_count; |
| 2367 | _sg_gl_attr_t gl_attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 2368 | sg_depth_stencil_state depth_stencil; |
| 2369 | sg_blend_state blend; |
| 2370 | sg_rasterizer_state rast; |
| 2371 | } _sg_pipeline_t; |
| 2372 | |
| 2373 | typedef struct { |
| 2374 | _sg_image_t* image; |
| 2375 | sg_image image_id; |
| 2376 | int mip_level; |
| 2377 | int slice; |
| 2378 | GLuint gl_msaa_resolve_buffer; |
| 2379 | } _sg_attachment_t; |
| 2380 | |
| 2381 | typedef struct { |
| 2382 | _sg_slot_t slot; |
| 2383 | GLuint gl_fb; |
| 2384 | int num_color_atts; |
| 2385 | _sg_attachment_t color_atts[SG_MAX_COLOR_ATTACHMENTS]; |
| 2386 | _sg_attachment_t ds_att; |
| 2387 | } _sg_pass_t; |
| 2388 | |
| 2389 | typedef struct { |
| 2390 | _sg_slot_t slot; |
| 2391 | #if !defined(SOKOL_GLES2) |
| 2392 | GLuint vao; |
| 2393 | #endif |
| 2394 | GLuint default_framebuffer; |
| 2395 | } _sg_context_t; |
| 2396 | |
| 2397 | typedef struct { |
| 2398 | _sg_gl_attr_t gl_attr; |
| 2399 | GLuint gl_vbuf; |
| 2400 | } _sg_gl_cache_attr_t; |
| 2401 | |
| 2402 | typedef struct { |
| 2403 | GLenum target; |
| 2404 | GLuint texture; |
| 2405 | } _sg_gl_texture_bind_slot; |
| 2406 | |
| 2407 | typedef struct { |
| 2408 | sg_depth_stencil_state ds; |
| 2409 | sg_blend_state blend; |
| 2410 | sg_rasterizer_state rast; |
| 2411 | bool polygon_offset_enabled; |
| 2412 | _sg_gl_cache_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 2413 | GLuint vertex_buffer; |
| 2414 | GLuint index_buffer; |
| 2415 | GLuint stored_vertex_buffer; |
| 2416 | GLuint stored_index_buffer; |
| 2417 | _sg_gl_texture_bind_slot textures[SG_MAX_SHADERSTAGE_IMAGES]; |
| 2418 | _sg_gl_texture_bind_slot stored_texture; |
| 2419 | int cur_ib_offset; |
| 2420 | GLenum cur_primitive_type; |
| 2421 | GLenum cur_index_type; |
| 2422 | _sg_pipeline_t* cur_pipeline; |
| 2423 | sg_pipeline cur_pipeline_id; |
| 2424 | } _sg_gl_state_cache_t; |
| 2425 | |
| 2426 | typedef struct { |
| 2427 | bool valid; |
| 2428 | bool gles2; |
| 2429 | bool in_pass; |
| 2430 | int cur_pass_width; |
| 2431 | int cur_pass_height; |
| 2432 | _sg_context_t* cur_context; |
| 2433 | _sg_pass_t* cur_pass; |
| 2434 | sg_pass cur_pass_id; |
| 2435 | _sg_gl_state_cache_t cache; |
| 2436 | bool features[SG_NUM_FEATURES]; |
| 2437 | bool ext_anisotropic; |
| 2438 | GLint max_anisotropy; |
| 2439 | GLint max_combined_texture_image_units; |
| 2440 | } _sg_gl_backend_t; |
| 2441 | |
| 2442 | /*== D3D11 BACKEND DECLARATIONS ==============================================*/ |
| 2443 | #elif defined(SOKOL_D3D11) |
| 2444 | |
| 2445 | typedef struct { |
| 2446 | _sg_slot_t slot; |
| 2447 | int size; |
| 2448 | int append_pos; |
| 2449 | bool append_overflow; |
| 2450 | sg_buffer_type type; |
| 2451 | sg_usage usage; |
| 2452 | uint32_t update_frame_index; |
| 2453 | uint32_t append_frame_index; |
| 2454 | ID3D11Buffer* d3d11_buf; |
| 2455 | } _sg_buffer_t; |
| 2456 | |
| 2457 | typedef struct { |
| 2458 | _sg_slot_t slot; |
| 2459 | sg_image_type type; |
| 2460 | bool render_target; |
| 2461 | int width; |
| 2462 | int height; |
| 2463 | int depth; |
| 2464 | int num_mipmaps; |
| 2465 | sg_usage usage; |
| 2466 | sg_pixel_format pixel_format; |
| 2467 | int sample_count; |
| 2468 | sg_filter min_filter; |
| 2469 | sg_filter mag_filter; |
| 2470 | sg_wrap wrap_u; |
| 2471 | sg_wrap wrap_v; |
| 2472 | sg_wrap wrap_w; |
| 2473 | uint32_t max_anisotropy; |
| 2474 | uint32_t upd_frame_index; |
| 2475 | DXGI_FORMAT d3d11_format; |
| 2476 | ID3D11Texture2D* d3d11_tex2d; |
| 2477 | ID3D11Texture3D* d3d11_tex3d; |
| 2478 | ID3D11Texture2D* d3d11_texds; |
| 2479 | ID3D11Texture2D* d3d11_texmsaa; |
| 2480 | ID3D11ShaderResourceView* d3d11_srv; |
| 2481 | ID3D11SamplerState* d3d11_smp; |
| 2482 | } _sg_image_t; |
| 2483 | |
| 2484 | typedef struct { |
| 2485 | int size; |
| 2486 | } _sg_uniform_block_t; |
| 2487 | |
| 2488 | typedef struct { |
| 2489 | sg_image_type type; |
| 2490 | } _sg_shader_image_t; |
| 2491 | |
| 2492 | typedef struct { |
| 2493 | _sg_str_t sem_name; |
| 2494 | int sem_index; |
| 2495 | } _sg_shader_attr_t; |
| 2496 | |
| 2497 | typedef struct { |
| 2498 | int num_uniform_blocks; |
| 2499 | int num_images; |
| 2500 | _sg_shader_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 2501 | _sg_uniform_block_t uniform_blocks[SG_MAX_SHADERSTAGE_UBS]; |
| 2502 | _sg_shader_image_t images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 2503 | ID3D11Buffer* d3d11_cbs[SG_MAX_SHADERSTAGE_UBS]; |
| 2504 | } _sg_shader_stage_t; |
| 2505 | |
| 2506 | typedef struct { |
| 2507 | _sg_slot_t slot; |
| 2508 | _sg_shader_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 2509 | _sg_shader_stage_t stage[SG_NUM_SHADER_STAGES]; |
| 2510 | ID3D11VertexShader* d3d11_vs; |
| 2511 | ID3D11PixelShader* d3d11_fs; |
| 2512 | void* d3d11_vs_blob; |
| 2513 | int d3d11_vs_blob_length; |
| 2514 | } _sg_shader_t; |
| 2515 | |
| 2516 | typedef struct { |
| 2517 | _sg_slot_t slot; |
| 2518 | _sg_shader_t* shader; |
| 2519 | sg_shader shader_id; |
| 2520 | sg_index_type index_type; |
| 2521 | bool vertex_layout_valid[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 2522 | int color_attachment_count; |
| 2523 | sg_pixel_format color_format; |
| 2524 | sg_pixel_format depth_format; |
| 2525 | int sample_count; |
| 2526 | float blend_color[4]; |
| 2527 | UINT d3d11_stencil_ref; |
| 2528 | UINT d3d11_vb_strides[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 2529 | D3D_PRIMITIVE_TOPOLOGY d3d11_topology; |
| 2530 | DXGI_FORMAT d3d11_index_format; |
| 2531 | ID3D11InputLayout* d3d11_il; |
| 2532 | ID3D11RasterizerState* d3d11_rs; |
| 2533 | ID3D11DepthStencilState* d3d11_dss; |
| 2534 | ID3D11BlendState* d3d11_bs; |
| 2535 | } _sg_pipeline_t; |
| 2536 | |
| 2537 | typedef struct { |
| 2538 | _sg_image_t* image; |
| 2539 | sg_image image_id; |
| 2540 | int mip_level; |
| 2541 | int slice; |
| 2542 | } _sg_attachment_t; |
| 2543 | |
| 2544 | typedef struct { |
| 2545 | _sg_slot_t slot; |
| 2546 | int num_color_atts; |
| 2547 | _sg_attachment_t color_atts[SG_MAX_COLOR_ATTACHMENTS]; |
| 2548 | _sg_attachment_t ds_att; |
| 2549 | ID3D11RenderTargetView* d3d11_rtvs[SG_MAX_COLOR_ATTACHMENTS]; |
| 2550 | ID3D11DepthStencilView* d3d11_dsv; |
| 2551 | } _sg_pass_t; |
| 2552 | |
| 2553 | typedef struct { |
| 2554 | _sg_slot_t slot; |
| 2555 | } _sg_context_t; |
| 2556 | |
| 2557 | typedef struct { |
| 2558 | bool valid; |
| 2559 | ID3D11Device* dev; |
| 2560 | ID3D11DeviceContext* ctx; |
| 2561 | const void* (*rtv_cb)(void); |
| 2562 | const void* (*dsv_cb)(void); |
| 2563 | bool in_pass; |
| 2564 | bool use_indexed_draw; |
| 2565 | int cur_width; |
| 2566 | int cur_height; |
| 2567 | int num_rtvs; |
| 2568 | _sg_pass_t* cur_pass; |
| 2569 | sg_pass cur_pass_id; |
| 2570 | _sg_pipeline_t* cur_pipeline; |
| 2571 | sg_pipeline cur_pipeline_id; |
| 2572 | ID3D11RenderTargetView* cur_rtvs[SG_MAX_COLOR_ATTACHMENTS]; |
| 2573 | ID3D11DepthStencilView* cur_dsv; |
| 2574 | /* on-demand loaded d3dcompiler_47.dll handles */ |
| 2575 | HINSTANCE d3dcompiler_dll; |
| 2576 | bool d3dcompiler_dll_load_failed; |
| 2577 | pD3DCompile D3DCompile_func; |
| 2578 | /* the following arrays are used for unbinding resources, they will always contain zeroes */ |
| 2579 | ID3D11RenderTargetView* zero_rtvs[SG_MAX_COLOR_ATTACHMENTS]; |
| 2580 | ID3D11Buffer* zero_vbs[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 2581 | UINT zero_vb_offsets[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 2582 | UINT zero_vb_strides[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 2583 | ID3D11Buffer* zero_cbs[SG_MAX_SHADERSTAGE_UBS]; |
| 2584 | ID3D11ShaderResourceView* zero_srvs[SG_MAX_SHADERSTAGE_IMAGES]; |
| 2585 | ID3D11SamplerState* zero_smps[SG_MAX_SHADERSTAGE_IMAGES]; |
| 2586 | /* global subresourcedata array for texture updates */ |
| 2587 | D3D11_SUBRESOURCE_DATA subres_data[SG_MAX_MIPMAPS * SG_MAX_TEXTUREARRAY_LAYERS]; |
| 2588 | } _sg_d3d11_backend_t; |
| 2589 | |
| 2590 | /*=== METAL BACKEND DECLARATIONS =============================================*/ |
| 2591 | #elif defined(SOKOL_METAL) |
| 2592 | |
| 2593 | enum { |
| 2594 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 2595 | _SG_MTL_UB_ALIGN = 256, |
| 2596 | #else |
| 2597 | _SG_MTL_UB_ALIGN = 16, |
| 2598 | #endif |
| 2599 | _SG_MTL_INVALID_SLOT_INDEX = 0 |
| 2600 | }; |
| 2601 | |
| 2602 | /* note that there's a free-standing _sg_mtl_idpool NSMutableArray, |
| 2603 | this can't be part of a C struct before Xcode10.x |
| 2604 | */ |
| 2605 | typedef struct { |
| 2606 | uint32_t frame_index; /* frame index at which it is safe to release this resource */ |
| 2607 | uint32_t slot_index; |
| 2608 | } _sg_mtl_release_item_t; |
| 2609 | |
| 2610 | typedef struct { |
| 2611 | uint32_t num_slots; |
| 2612 | uint32_t free_queue_top; |
| 2613 | uint32_t* free_queue; |
| 2614 | uint32_t release_queue_front; |
| 2615 | uint32_t release_queue_back; |
| 2616 | _sg_mtl_release_item_t* release_queue; |
| 2617 | } _sg_mtl_idpool_t; |
| 2618 | |
| 2619 | /* Metal sampler cache */ |
| 2620 | typedef struct { |
| 2621 | sg_filter min_filter; |
| 2622 | sg_filter mag_filter; |
| 2623 | sg_wrap wrap_u; |
| 2624 | sg_wrap wrap_v; |
| 2625 | sg_wrap wrap_w; |
| 2626 | uint32_t max_anisotropy; |
| 2627 | int min_lod; /* orig min/max_lod is float, this is int(min/max_lod*1000.0) */ |
| 2628 | int max_lod; |
| 2629 | uint32_t mtl_sampler_state; |
| 2630 | } _sg_mtl_sampler_cache_item_t; |
| 2631 | |
| 2632 | typedef struct { |
| 2633 | int capacity; |
| 2634 | int num_items; |
| 2635 | _sg_mtl_sampler_cache_item_t* items; |
| 2636 | } _sg_mtl_sampler_cache_t; |
| 2637 | |
| 2638 | typedef struct { |
| 2639 | _sg_slot_t slot; |
| 2640 | int size; |
| 2641 | int append_pos; |
| 2642 | bool append_overflow; |
| 2643 | sg_buffer_type type; |
| 2644 | sg_usage usage; |
| 2645 | uint32_t update_frame_index; |
| 2646 | uint32_t append_frame_index; |
| 2647 | int num_slots; |
| 2648 | int active_slot; |
| 2649 | uint32_t mtl_buf[SG_NUM_INFLIGHT_FRAMES]; /* index intp _sg_mtl_pool */ |
| 2650 | } _sg_buffer_t; |
| 2651 | |
| 2652 | typedef struct { |
| 2653 | _sg_slot_t slot; |
| 2654 | sg_image_type type; |
| 2655 | bool render_target; |
| 2656 | int width; |
| 2657 | int height; |
| 2658 | int depth; |
| 2659 | int num_mipmaps; |
| 2660 | sg_usage usage; |
| 2661 | sg_pixel_format pixel_format; |
| 2662 | int sample_count; |
| 2663 | sg_filter min_filter; |
| 2664 | sg_filter mag_filter; |
| 2665 | sg_wrap wrap_u; |
| 2666 | sg_wrap wrap_v; |
| 2667 | sg_wrap wrap_w; |
| 2668 | uint32_t max_anisotropy; |
| 2669 | uint32_t upd_frame_index; |
| 2670 | int num_slots; |
| 2671 | int active_slot; |
| 2672 | uint32_t mtl_tex[SG_NUM_INFLIGHT_FRAMES]; |
| 2673 | uint32_t mtl_depth_tex; |
| 2674 | uint32_t mtl_msaa_tex; |
| 2675 | uint32_t mtl_sampler_state; |
| 2676 | } _sg_image_t; |
| 2677 | |
| 2678 | typedef struct { |
| 2679 | int size; |
| 2680 | } _sg_uniform_block_t; |
| 2681 | |
| 2682 | typedef struct { |
| 2683 | sg_image_type type; |
| 2684 | } _sg_shader_image_t; |
| 2685 | |
| 2686 | typedef struct { |
| 2687 | int num_uniform_blocks; |
| 2688 | int num_images; |
| 2689 | _sg_uniform_block_t uniform_blocks[SG_MAX_SHADERSTAGE_UBS]; |
| 2690 | _sg_shader_image_t images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 2691 | uint32_t mtl_lib; |
| 2692 | uint32_t mtl_func; |
| 2693 | } _sg_shader_stage_t; |
| 2694 | |
| 2695 | typedef struct { |
| 2696 | _sg_slot_t slot; |
| 2697 | _sg_shader_stage_t stage[SG_NUM_SHADER_STAGES]; |
| 2698 | } _sg_shader_t; |
| 2699 | |
| 2700 | typedef struct { |
| 2701 | _sg_slot_t slot; |
| 2702 | _sg_shader_t* shader; |
| 2703 | sg_shader shader_id; |
| 2704 | bool vertex_layout_valid[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 2705 | int color_attachment_count; |
| 2706 | sg_pixel_format color_format; |
| 2707 | sg_pixel_format depth_format; |
| 2708 | int sample_count; |
| 2709 | float depth_bias; |
| 2710 | float depth_bias_slope_scale; |
| 2711 | float depth_bias_clamp; |
| 2712 | MTLPrimitiveType mtl_prim_type; |
| 2713 | sg_index_type index_type; |
| 2714 | NSUInteger mtl_index_size; |
| 2715 | MTLIndexType mtl_index_type; |
| 2716 | MTLCullMode mtl_cull_mode; |
| 2717 | MTLWinding mtl_winding; |
| 2718 | float blend_color[4]; |
| 2719 | uint32_t mtl_stencil_ref; |
| 2720 | uint32_t mtl_rps; |
| 2721 | uint32_t mtl_dss; |
| 2722 | } _sg_pipeline_t; |
| 2723 | |
| 2724 | typedef struct { |
| 2725 | _sg_image_t* image; |
| 2726 | sg_image image_id; |
| 2727 | int mip_level; |
| 2728 | int slice; |
| 2729 | } _sg_attachment_t; |
| 2730 | |
| 2731 | typedef struct { |
| 2732 | _sg_slot_t slot; |
| 2733 | int num_color_atts; |
| 2734 | _sg_attachment_t color_atts[SG_MAX_COLOR_ATTACHMENTS]; |
| 2735 | _sg_attachment_t ds_att; |
| 2736 | } _sg_pass_t; |
| 2737 | |
| 2738 | typedef struct { |
| 2739 | _sg_slot_t slot; |
| 2740 | } _sg_context_t; |
| 2741 | |
| 2742 | /* resouce binding state cache */ |
| 2743 | typedef struct { |
| 2744 | const _sg_pipeline_t* cur_pipeline; |
| 2745 | sg_pipeline cur_pipeline_id; |
| 2746 | const _sg_buffer_t* cur_indexbuffer; |
| 2747 | int cur_indexbuffer_offset; |
| 2748 | sg_buffer cur_indexbuffer_id; |
| 2749 | const _sg_buffer_t* cur_vertexbuffers[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 2750 | int cur_vertexbuffer_offsets[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 2751 | sg_buffer cur_vertexbuffer_ids[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 2752 | const _sg_image_t* cur_vs_images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 2753 | sg_image cur_vs_image_ids[SG_MAX_SHADERSTAGE_IMAGES]; |
| 2754 | const _sg_image_t* cur_fs_images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 2755 | sg_image cur_fs_image_ids[SG_MAX_SHADERSTAGE_IMAGES]; |
| 2756 | } _sg_mtl_state_cache_t; |
| 2757 | |
| 2758 | typedef struct { |
| 2759 | bool valid; |
| 2760 | const void*(*renderpass_descriptor_cb)(void); |
| 2761 | const void*(*drawable_cb)(void); |
| 2762 | uint32_t frame_index; |
| 2763 | uint32_t cur_frame_rotate_index; |
| 2764 | uint32_t ub_size; |
| 2765 | uint32_t cur_ub_offset; |
| 2766 | uint8_t* cur_ub_base_ptr; |
| 2767 | bool in_pass; |
| 2768 | bool pass_valid; |
| 2769 | int cur_width; |
| 2770 | int cur_height; |
| 2771 | _sg_mtl_state_cache_t state_cache; |
| 2772 | _sg_mtl_sampler_cache_t sampler_cache; |
| 2773 | _sg_mtl_idpool_t idpool; |
| 2774 | } _sg_mtl_backend_t; |
| 2775 | |
| 2776 | /* keep Objective-C 'smart data' in a separate static objects, these can't be in a C struct until Xcode10 or so */ |
| 2777 | static NSMutableArray* _sg_mtl_idpool; |
| 2778 | static id<MTLDevice> _sg_mtl_device; |
| 2779 | static id<MTLCommandQueue> _sg_mtl_cmd_queue; |
| 2780 | static id<MTLCommandBuffer> _sg_mtl_cmd_buffer; |
| 2781 | static id<MTLBuffer> _sg_mtl_uniform_buffers[SG_NUM_INFLIGHT_FRAMES]; |
| 2782 | static id<MTLRenderCommandEncoder> _sg_mtl_cmd_encoder; |
| 2783 | static dispatch_semaphore_t _sg_mtl_sem; |
| 2784 | |
| 2785 | #endif /* SOKOL_METAL */ |
| 2786 | |
| 2787 | /*=== RESOURCE POOL DECLARATIONS =============================================*/ |
| 2788 | |
| 2789 | /* this *MUST* remain 0 */ |
| 2790 | #define _SG_INVALID_SLOT_INDEX (0) |
| 2791 | |
| 2792 | typedef struct { |
| 2793 | int size; |
| 2794 | int queue_top; |
| 2795 | uint32_t* gen_ctrs; |
| 2796 | int* free_queue; |
| 2797 | } _sg_pool_t; |
| 2798 | |
| 2799 | typedef struct { |
| 2800 | _sg_pool_t buffer_pool; |
| 2801 | _sg_pool_t image_pool; |
| 2802 | _sg_pool_t shader_pool; |
| 2803 | _sg_pool_t pipeline_pool; |
| 2804 | _sg_pool_t pass_pool; |
| 2805 | _sg_pool_t context_pool; |
| 2806 | _sg_buffer_t* buffers; |
| 2807 | _sg_image_t* images; |
| 2808 | _sg_shader_t* shaders; |
| 2809 | _sg_pipeline_t* pipelines; |
| 2810 | _sg_pass_t* passes; |
| 2811 | _sg_context_t* contexts; |
| 2812 | } _sg_pools_t; |
| 2813 | |
| 2814 | /*=== VALIDATION LAYER DECLARATIONS ==========================================*/ |
| 2815 | typedef enum { |
| 2816 | /* special case 'validation was successful' */ |
| 2817 | _SG_VALIDATE_SUCCESS, |
| 2818 | |
| 2819 | /* buffer creation */ |
| 2820 | _SG_VALIDATE_BUFFERDESC_CANARY, |
| 2821 | _SG_VALIDATE_BUFFERDESC_SIZE, |
| 2822 | _SG_VALIDATE_BUFFERDESC_CONTENT, |
| 2823 | _SG_VALIDATE_BUFFERDESC_NO_CONTENT, |
| 2824 | |
| 2825 | /* image creation */ |
| 2826 | _SG_VALIDATE_IMAGEDESC_CANARY, |
| 2827 | _SG_VALIDATE_IMAGEDESC_WIDTH, |
| 2828 | _SG_VALIDATE_IMAGEDESC_HEIGHT, |
| 2829 | _SG_VALIDATE_IMAGEDESC_RT_PIXELFORMAT, |
| 2830 | _SG_VALIDATE_IMAGEDESC_NONRT_PIXELFORMAT, |
| 2831 | _SG_VALIDATE_IMAGEDESC_MSAA_BUT_NO_RT, |
| 2832 | _SG_VALIDATE_IMAGEDESC_NO_MSAA_RT_SUPPORT, |
| 2833 | _SG_VALIDATE_IMAGEDESC_RT_IMMUTABLE, |
| 2834 | _SG_VALIDATE_IMAGEDESC_RT_NO_CONTENT, |
| 2835 | _SG_VALIDATE_IMAGEDESC_CONTENT, |
| 2836 | _SG_VALIDATE_IMAGEDESC_NO_CONTENT, |
| 2837 | |
| 2838 | /* shader creation */ |
| 2839 | _SG_VALIDATE_SHADERDESC_CANARY, |
| 2840 | _SG_VALIDATE_SHADERDESC_SOURCE, |
| 2841 | _SG_VALIDATE_SHADERDESC_BYTECODE, |
| 2842 | _SG_VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE, |
| 2843 | _SG_VALIDATE_SHADERDESC_NO_BYTECODE_SIZE, |
| 2844 | _SG_VALIDATE_SHADERDESC_NO_CONT_UBS, |
| 2845 | _SG_VALIDATE_SHADERDESC_NO_CONT_IMGS, |
| 2846 | _SG_VALIDATE_SHADERDESC_NO_CONT_UB_MEMBERS, |
| 2847 | _SG_VALIDATE_SHADERDESC_NO_UB_MEMBERS, |
| 2848 | _SG_VALIDATE_SHADERDESC_UB_MEMBER_NAME, |
| 2849 | _SG_VALIDATE_SHADERDESC_UB_SIZE_MISMATCH, |
| 2850 | _SG_VALIDATE_SHADERDESC_IMG_NAME, |
| 2851 | _SG_VALIDATE_SHADERDESC_ATTR_NAMES, |
| 2852 | _SG_VALIDATE_SHADERDESC_ATTR_SEMANTICS, |
| 2853 | _SG_VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG, |
| 2854 | |
| 2855 | /* pipeline creation */ |
| 2856 | _SG_VALIDATE_PIPELINEDESC_CANARY, |
| 2857 | _SG_VALIDATE_PIPELINEDESC_SHADER, |
| 2858 | _SG_VALIDATE_PIPELINEDESC_NO_ATTRS, |
| 2859 | _SG_VALIDATE_PIPELINEDESC_LAYOUT_STRIDE4, |
| 2860 | _SG_VALIDATE_PIPELINEDESC_ATTR_NAME, |
| 2861 | _SG_VALIDATE_PIPELINEDESC_ATTR_SEMANTICS, |
| 2862 | |
| 2863 | /* pass creation */ |
| 2864 | _SG_VALIDATE_PASSDESC_CANARY, |
| 2865 | _SG_VALIDATE_PASSDESC_NO_COLOR_ATTS, |
| 2866 | _SG_VALIDATE_PASSDESC_NO_CONT_COLOR_ATTS, |
| 2867 | _SG_VALIDATE_PASSDESC_IMAGE, |
| 2868 | _SG_VALIDATE_PASSDESC_MIPLEVEL, |
| 2869 | _SG_VALIDATE_PASSDESC_FACE, |
| 2870 | _SG_VALIDATE_PASSDESC_LAYER, |
| 2871 | _SG_VALIDATE_PASSDESC_SLICE, |
| 2872 | _SG_VALIDATE_PASSDESC_IMAGE_NO_RT, |
| 2873 | _SG_VALIDATE_PASSDESC_COLOR_PIXELFORMATS, |
| 2874 | _SG_VALIDATE_PASSDESC_COLOR_INV_PIXELFORMAT, |
| 2875 | _SG_VALIDATE_PASSDESC_DEPTH_INV_PIXELFORMAT, |
| 2876 | _SG_VALIDATE_PASSDESC_IMAGE_SIZES, |
| 2877 | _SG_VALIDATE_PASSDESC_IMAGE_SAMPLE_COUNTS, |
| 2878 | |
| 2879 | /* sg_begin_pass validation */ |
| 2880 | _SG_VALIDATE_BEGINPASS_PASS, |
| 2881 | _SG_VALIDATE_BEGINPASS_IMAGE, |
| 2882 | |
| 2883 | /* sg_apply_pipeline validation */ |
| 2884 | _SG_VALIDATE_APIP_PIPELINE_VALID_ID, |
| 2885 | _SG_VALIDATE_APIP_PIPELINE_EXISTS, |
| 2886 | _SG_VALIDATE_APIP_PIPELINE_VALID, |
| 2887 | _SG_VALIDATE_APIP_SHADER_EXISTS, |
| 2888 | _SG_VALIDATE_APIP_SHADER_VALID, |
| 2889 | _SG_VALIDATE_APIP_ATT_COUNT, |
| 2890 | _SG_VALIDATE_APIP_COLOR_FORMAT, |
| 2891 | _SG_VALIDATE_APIP_DEPTH_FORMAT, |
| 2892 | _SG_VALIDATE_APIP_SAMPLE_COUNT, |
| 2893 | |
| 2894 | /* sg_apply_bindings validation */ |
| 2895 | _SG_VALIDATE_ABND_PIPELINE, |
| 2896 | _SG_VALIDATE_ABND_PIPELINE_EXISTS, |
| 2897 | _SG_VALIDATE_ABND_PIPELINE_VALID, |
| 2898 | _SG_VALIDATE_ABND_VBS, |
| 2899 | _SG_VALIDATE_ABND_VB_EXISTS, |
| 2900 | _SG_VALIDATE_ABND_VB_TYPE, |
| 2901 | _SG_VALIDATE_ABND_VB_OVERFLOW, |
| 2902 | _SG_VALIDATE_ABND_NO_IB, |
| 2903 | _SG_VALIDATE_ABND_IB, |
| 2904 | _SG_VALIDATE_ABND_IB_EXISTS, |
| 2905 | _SG_VALIDATE_ABND_IB_TYPE, |
| 2906 | _SG_VALIDATE_ABND_IB_OVERFLOW, |
| 2907 | _SG_VALIDATE_ABND_VS_IMGS, |
| 2908 | _SG_VALIDATE_ABND_VS_IMG_EXISTS, |
| 2909 | _SG_VALIDATE_ABND_VS_IMG_TYPES, |
| 2910 | _SG_VALIDATE_ABND_FS_IMGS, |
| 2911 | _SG_VALIDATE_ABND_FS_IMG_EXISTS, |
| 2912 | _SG_VALIDATE_ABND_FS_IMG_TYPES, |
| 2913 | |
| 2914 | /* sg_apply_uniforms validation */ |
| 2915 | _SG_VALIDATE_AUB_NO_PIPELINE, |
| 2916 | _SG_VALIDATE_AUB_NO_UB_AT_SLOT, |
| 2917 | _SG_VALIDATE_AUB_SIZE, |
| 2918 | |
| 2919 | /* sg_update_buffer validation */ |
| 2920 | _SG_VALIDATE_UPDATEBUF_USAGE, |
| 2921 | _SG_VALIDATE_UPDATEBUF_SIZE, |
| 2922 | _SG_VALIDATE_UPDATEBUF_ONCE, |
| 2923 | _SG_VALIDATE_UPDATEBUF_APPEND, |
| 2924 | |
| 2925 | /* sg_append_buffer validation */ |
| 2926 | _SG_VALIDATE_APPENDBUF_USAGE, |
| 2927 | _SG_VALIDATE_APPENDBUF_SIZE, |
| 2928 | _SG_VALIDATE_APPENDBUF_UPDATE, |
| 2929 | |
| 2930 | /* sg_update_image validation */ |
| 2931 | _SG_VALIDATE_UPDIMG_USAGE, |
| 2932 | _SG_VALIDATE_UPDIMG_NOTENOUGHDATA, |
| 2933 | _SG_VALIDATE_UPDIMG_SIZE, |
| 2934 | _SG_VALIDATE_UPDIMG_COMPRESSED, |
| 2935 | _SG_VALIDATE_UPDIMG_ONCE |
| 2936 | } _sg_validate_error_t; |
| 2937 | |
| 2938 | /*=== GENERIC BACKEND STATE ==================================================*/ |
| 2939 | |
| 2940 | typedef struct { |
| 2941 | bool valid; |
| 2942 | sg_desc desc; /* original desc with default values patched in */ |
| 2943 | uint32_t frame_index; |
| 2944 | sg_context active_context; |
| 2945 | sg_pass cur_pass; |
| 2946 | sg_pipeline cur_pipeline; |
| 2947 | bool pass_valid; |
| 2948 | bool bindings_valid; |
| 2949 | bool next_draw_valid; |
| 2950 | #if defined(SOKOL_DEBUG) |
| 2951 | _sg_validate_error_t validate_error; |
| 2952 | #endif |
| 2953 | _sg_pools_t pools; |
| 2954 | #if defined(SOKOL_GLCORE33) || defined(SOKOL_GLES2) || defined(SOKOL_GLES3) |
| 2955 | _sg_gl_backend_t gl; |
| 2956 | #elif defined(SOKOL_METAL) |
| 2957 | _sg_mtl_backend_t mtl; |
| 2958 | #elif defined(SOKOL_D3D11) |
| 2959 | _sg_d3d11_backend_t d3d11; |
| 2960 | #endif |
| 2961 | #if defined(SOKOL_TRACE_HOOKS) |
| 2962 | sg_trace_hooks hooks; |
| 2963 | #endif |
| 2964 | } _sg_state_t; |
| 2965 | static _sg_state_t _sg; |
| 2966 | |
| 2967 | /*-- helper functions --------------------------------------------------------*/ |
| 2968 | |
| 2969 | _SOKOL_PRIVATE bool _sg_strempty(const _sg_str_t* str) { |
| 2970 | return 0 == str->buf[0]; |
| 2971 | } |
| 2972 | |
| 2973 | _SOKOL_PRIVATE const char* _sg_strptr(const _sg_str_t* str) { |
| 2974 | return &str->buf[0]; |
| 2975 | } |
| 2976 | |
| 2977 | _SOKOL_PRIVATE void _sg_strcpy(_sg_str_t* dst, const char* src) { |
| 2978 | SOKOL_ASSERT(dst); |
| 2979 | if (src) { |
| 2980 | #if defined(_MSC_VER) |
| 2981 | strncpy_s(dst->buf, _SG_STRING_SIZE, src, (_SG_STRING_SIZE-1)); |
| 2982 | #else |
| 2983 | strncpy(dst->buf, src, _SG_STRING_SIZE); |
| 2984 | #endif |
| 2985 | dst->buf[_SG_STRING_SIZE-1] = 0; |
| 2986 | } |
| 2987 | else { |
| 2988 | memset(dst->buf, 0, _SG_STRING_SIZE); |
| 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | /* return byte size of a vertex format */ |
| 2993 | _SOKOL_PRIVATE int _sg_vertexformat_bytesize(sg_vertex_format fmt) { |
| 2994 | switch (fmt) { |
| 2995 | case SG_VERTEXFORMAT_FLOAT: return 4; |
| 2996 | case SG_VERTEXFORMAT_FLOAT2: return 8; |
| 2997 | case SG_VERTEXFORMAT_FLOAT3: return 12; |
| 2998 | case SG_VERTEXFORMAT_FLOAT4: return 16; |
| 2999 | case SG_VERTEXFORMAT_BYTE4: return 4; |
| 3000 | case SG_VERTEXFORMAT_BYTE4N: return 4; |
| 3001 | case SG_VERTEXFORMAT_UBYTE4: return 4; |
| 3002 | case SG_VERTEXFORMAT_UBYTE4N: return 4; |
| 3003 | case SG_VERTEXFORMAT_SHORT2: return 4; |
| 3004 | case SG_VERTEXFORMAT_SHORT2N: return 4; |
| 3005 | case SG_VERTEXFORMAT_SHORT4: return 8; |
| 3006 | case SG_VERTEXFORMAT_SHORT4N: return 8; |
| 3007 | case SG_VERTEXFORMAT_UINT10_N2: return 4; |
| 3008 | case SG_VERTEXFORMAT_INVALID: return 0; |
| 3009 | default: |
| 3010 | SOKOL_UNREACHABLE; |
| 3011 | return -1; |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | /* return the byte size of a shader uniform */ |
| 3016 | _SOKOL_PRIVATE int _sg_uniform_size(sg_uniform_type type, int count) { |
| 3017 | switch (type) { |
| 3018 | case SG_UNIFORMTYPE_INVALID: return 0; |
| 3019 | case SG_UNIFORMTYPE_FLOAT: return 4 * count; |
| 3020 | case SG_UNIFORMTYPE_FLOAT2: return 8 * count; |
| 3021 | case SG_UNIFORMTYPE_FLOAT3: return 12 * count; /* FIXME: std140??? */ |
| 3022 | case SG_UNIFORMTYPE_FLOAT4: return 16 * count; |
| 3023 | case SG_UNIFORMTYPE_MAT4: return 64 * count; |
| 3024 | default: |
| 3025 | SOKOL_UNREACHABLE; |
| 3026 | return -1; |
| 3027 | } |
| 3028 | } |
| 3029 | |
| 3030 | /* return true if pixel format is a compressed format */ |
| 3031 | _SOKOL_PRIVATE bool _sg_is_compressed_pixel_format(sg_pixel_format fmt) { |
| 3032 | switch (fmt) { |
| 3033 | case SG_PIXELFORMAT_DXT1: |
| 3034 | case SG_PIXELFORMAT_DXT3: |
| 3035 | case SG_PIXELFORMAT_DXT5: |
| 3036 | case SG_PIXELFORMAT_PVRTC2_RGB: |
| 3037 | case SG_PIXELFORMAT_PVRTC4_RGB: |
| 3038 | case SG_PIXELFORMAT_PVRTC2_RGBA: |
| 3039 | case SG_PIXELFORMAT_PVRTC4_RGBA: |
| 3040 | case SG_PIXELFORMAT_ETC2_RGB8: |
| 3041 | case SG_PIXELFORMAT_ETC2_SRGB8: |
| 3042 | return true; |
| 3043 | default: |
| 3044 | return false; |
| 3045 | } |
| 3046 | } |
| 3047 | |
| 3048 | /* return true if pixel format is a valid render target format */ |
| 3049 | _SOKOL_PRIVATE bool _sg_is_valid_rendertarget_color_format(sg_pixel_format fmt) { |
| 3050 | switch (fmt) { |
| 3051 | case SG_PIXELFORMAT_RGBA8: |
| 3052 | case SG_PIXELFORMAT_R10G10B10A2: |
| 3053 | case SG_PIXELFORMAT_RGBA32F: |
| 3054 | case SG_PIXELFORMAT_RGBA16F: |
| 3055 | return true; |
| 3056 | default: |
| 3057 | return false; |
| 3058 | } |
| 3059 | } |
| 3060 | |
| 3061 | /* return true if pixel format is a valid depth format */ |
| 3062 | _SOKOL_PRIVATE bool _sg_is_valid_rendertarget_depth_format(sg_pixel_format fmt) { |
| 3063 | switch (fmt) { |
| 3064 | case SG_PIXELFORMAT_DEPTH: |
| 3065 | case SG_PIXELFORMAT_DEPTHSTENCIL: |
| 3066 | return true; |
| 3067 | default: |
| 3068 | return false; |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | /* return true if pixel format is a depth-stencil format */ |
| 3073 | _SOKOL_PRIVATE bool _sg_is_depth_stencil_format(sg_pixel_format fmt) { |
| 3074 | /* FIXME: more depth stencil formats? */ |
| 3075 | return (SG_PIXELFORMAT_DEPTHSTENCIL == fmt); |
| 3076 | } |
| 3077 | |
| 3078 | /* return the bytes-per-pixel for a pixel format */ |
| 3079 | _SOKOL_PRIVATE int _sg_pixelformat_bytesize(sg_pixel_format fmt) { |
| 3080 | switch (fmt) { |
| 3081 | case SG_PIXELFORMAT_RGBA32F: |
| 3082 | return 16; |
| 3083 | case SG_PIXELFORMAT_RGBA16F: |
| 3084 | return 8; |
| 3085 | case SG_PIXELFORMAT_RGBA8: |
| 3086 | case SG_PIXELFORMAT_R10G10B10A2: |
| 3087 | case SG_PIXELFORMAT_R32F: |
| 3088 | return 4; |
| 3089 | case SG_PIXELFORMAT_RGB8: |
| 3090 | return 3; |
| 3091 | case SG_PIXELFORMAT_R5G5B5A1: |
| 3092 | case SG_PIXELFORMAT_R5G6B5: |
| 3093 | case SG_PIXELFORMAT_RGBA4: |
| 3094 | case SG_PIXELFORMAT_R16F: |
| 3095 | return 2; |
| 3096 | case SG_PIXELFORMAT_L8: |
| 3097 | return 1; |
| 3098 | default: |
| 3099 | SOKOL_UNREACHABLE; |
| 3100 | return 0; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | /* return row pitch for an image */ |
| 3105 | _SOKOL_PRIVATE int _sg_row_pitch(sg_pixel_format fmt, int width) { |
| 3106 | int pitch; |
| 3107 | switch (fmt) { |
| 3108 | case SG_PIXELFORMAT_DXT1: |
| 3109 | case SG_PIXELFORMAT_ETC2_RGB8: |
| 3110 | case SG_PIXELFORMAT_ETC2_SRGB8: |
| 3111 | pitch = ((width + 3) / 4) * 8; |
| 3112 | pitch = pitch < 8 ? 8 : pitch; |
| 3113 | break; |
| 3114 | case SG_PIXELFORMAT_DXT3: |
| 3115 | case SG_PIXELFORMAT_DXT5: |
| 3116 | pitch = ((width + 3) / 4) * 16; |
| 3117 | pitch = pitch < 16 ? 16 : pitch; |
| 3118 | break; |
| 3119 | case SG_PIXELFORMAT_PVRTC4_RGB: |
| 3120 | case SG_PIXELFORMAT_PVRTC4_RGBA: |
| 3121 | { |
| 3122 | const int block_size = 4*4; |
| 3123 | const int bpp = 4; |
| 3124 | int width_blocks = width / 4; |
| 3125 | width_blocks = width_blocks < 2 ? 2 : width_blocks; |
| 3126 | pitch = width_blocks * ((block_size * bpp) / 8); |
| 3127 | } |
| 3128 | break; |
| 3129 | case SG_PIXELFORMAT_PVRTC2_RGB: |
| 3130 | case SG_PIXELFORMAT_PVRTC2_RGBA: |
| 3131 | { |
| 3132 | const int block_size = 8*4; |
| 3133 | const int bpp = 2; |
| 3134 | int width_blocks = width / 4; |
| 3135 | width_blocks = width_blocks < 2 ? 2 : width_blocks; |
| 3136 | pitch = width_blocks * ((block_size * bpp) / 8); |
| 3137 | } |
| 3138 | break; |
| 3139 | default: |
| 3140 | pitch = width * _sg_pixelformat_bytesize(fmt); |
| 3141 | break; |
| 3142 | } |
| 3143 | return pitch; |
| 3144 | } |
| 3145 | |
| 3146 | /* return pitch of a 2D subimage / texture slice */ |
| 3147 | _SOKOL_PRIVATE int _sg_surface_pitch(sg_pixel_format fmt, int width, int height) { |
| 3148 | int num_rows = 0; |
| 3149 | switch (fmt) { |
| 3150 | case SG_PIXELFORMAT_DXT1: |
| 3151 | case SG_PIXELFORMAT_DXT3: |
| 3152 | case SG_PIXELFORMAT_DXT5: |
| 3153 | case SG_PIXELFORMAT_ETC2_RGB8: |
| 3154 | case SG_PIXELFORMAT_ETC2_SRGB8: |
| 3155 | case SG_PIXELFORMAT_PVRTC2_RGB: |
| 3156 | case SG_PIXELFORMAT_PVRTC2_RGBA: |
| 3157 | case SG_PIXELFORMAT_PVRTC4_RGB: |
| 3158 | case SG_PIXELFORMAT_PVRTC4_RGBA: |
| 3159 | num_rows = ((height + 3) / 4); |
| 3160 | break; |
| 3161 | default: |
| 3162 | num_rows = height; |
| 3163 | break; |
| 3164 | } |
| 3165 | if (num_rows < 1) { |
| 3166 | num_rows = 1; |
| 3167 | } |
| 3168 | return num_rows * _sg_row_pitch(fmt, width); |
| 3169 | } |
| 3170 | |
| 3171 | /* resolve pass action defaults into a new pass action struct */ |
| 3172 | _SOKOL_PRIVATE void _sg_resolve_default_pass_action(const sg_pass_action* from, sg_pass_action* to) { |
| 3173 | SOKOL_ASSERT(from && to); |
| 3174 | *to = *from; |
| 3175 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 3176 | if (to->colors[i].action == _SG_ACTION_DEFAULT) { |
| 3177 | to->colors[i].action = SG_ACTION_CLEAR; |
| 3178 | to->colors[i].val[0] = SG_DEFAULT_CLEAR_RED; |
| 3179 | to->colors[i].val[1] = SG_DEFAULT_CLEAR_GREEN; |
| 3180 | to->colors[i].val[2] = SG_DEFAULT_CLEAR_BLUE; |
| 3181 | to->colors[i].val[3] = SG_DEFAULT_CLEAR_ALPHA; |
| 3182 | } |
| 3183 | } |
| 3184 | if (to->depth.action == _SG_ACTION_DEFAULT) { |
| 3185 | to->depth.action = SG_ACTION_CLEAR; |
| 3186 | to->depth.val = SG_DEFAULT_CLEAR_DEPTH; |
| 3187 | } |
| 3188 | if (to->stencil.action == _SG_ACTION_DEFAULT) { |
| 3189 | to->stencil.action = SG_ACTION_CLEAR; |
| 3190 | to->stencil.val = SG_DEFAULT_CLEAR_STENCIL; |
| 3191 | } |
| 3192 | } |
| 3193 | |
| 3194 | /*== DUMMY BACKEND IMPL ======================================================*/ |
| 3195 | #if defined(SOKOL_DUMMY_BACKEND) |
| 3196 | |
| 3197 | _SOKOL_PRIVATE void _sg_setup_backend(const sg_desc* desc) { |
| 3198 | SOKOL_ASSERT(desc); |
| 3199 | _SOKOL_UNUSED(desc); |
| 3200 | } |
| 3201 | |
| 3202 | _SOKOL_PRIVATE void _sg_discard_backend(void) { |
| 3203 | /* empty */ |
| 3204 | } |
| 3205 | |
| 3206 | _SOKOL_PRIVATE bool _sg_query_feature(sg_feature f) { |
| 3207 | _SOKOL_UNUSED(f); |
| 3208 | return true; |
| 3209 | } |
| 3210 | |
| 3211 | _SOKOL_PRIVATE void _sg_reset_state_cache(void) { |
| 3212 | /* empty*/ |
| 3213 | } |
| 3214 | |
| 3215 | _SOKOL_PRIVATE sg_resource_state _sg_create_context(_sg_context_t* ctx) { |
| 3216 | SOKOL_ASSERT(ctx); |
| 3217 | _SOKOL_UNUSED(ctx); |
| 3218 | return SG_RESOURCESTATE_VALID; |
| 3219 | } |
| 3220 | |
| 3221 | _SOKOL_PRIVATE void _sg_destroy_context(_sg_context_t* ctx) { |
| 3222 | SOKOL_ASSERT(ctx); |
| 3223 | _SOKOL_UNUSED(ctx); |
| 3224 | } |
| 3225 | |
| 3226 | _SOKOL_PRIVATE void _sg_activate_context(_sg_context_t* ctx) { |
| 3227 | SOKOL_ASSERT(ctx); |
| 3228 | _SOKOL_UNUSED(ctx); |
| 3229 | } |
| 3230 | |
| 3231 | _SOKOL_PRIVATE sg_resource_state _sg_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) { |
| 3232 | SOKOL_ASSERT(buf && desc); |
| 3233 | buf->size = desc->size; |
| 3234 | buf->append_pos = 0; |
| 3235 | buf->append_overflow = false; |
| 3236 | buf->type = desc->type; |
| 3237 | buf->usage = desc->usage; |
| 3238 | buf->update_frame_index = 0; |
| 3239 | buf->append_frame_index = 0; |
| 3240 | buf->num_slots = (buf->usage == SG_USAGE_IMMUTABLE) ? 1 : SG_NUM_INFLIGHT_FRAMES; |
| 3241 | buf->active_slot = 0; |
| 3242 | return SG_RESOURCESTATE_VALID; |
| 3243 | } |
| 3244 | |
| 3245 | _SOKOL_PRIVATE void _sg_destroy_buffer(_sg_buffer_t* buf) { |
| 3246 | SOKOL_ASSERT(buf); |
| 3247 | _SOKOL_UNUSED(buf); |
| 3248 | } |
| 3249 | |
| 3250 | _SOKOL_PRIVATE sg_resource_state _sg_create_image(_sg_image_t* img, const sg_image_desc* desc) { |
| 3251 | SOKOL_ASSERT(img && desc); |
| 3252 | img->type = desc->type; |
| 3253 | img->render_target = desc->render_target; |
| 3254 | img->width = desc->width; |
| 3255 | img->height = desc->height; |
| 3256 | img->depth = desc->depth; |
| 3257 | img->num_mipmaps = desc->num_mipmaps; |
| 3258 | img->usage = desc->usage; |
| 3259 | img->pixel_format = desc->pixel_format; |
| 3260 | img->sample_count = desc->sample_count; |
| 3261 | img->min_filter = desc->min_filter; |
| 3262 | img->mag_filter = desc->mag_filter; |
| 3263 | img->wrap_u = desc->wrap_u; |
| 3264 | img->wrap_v = desc->wrap_v; |
| 3265 | img->wrap_w = desc->wrap_w; |
| 3266 | img->max_anisotropy = desc->max_anisotropy; |
| 3267 | img->upd_frame_index = 0; |
| 3268 | img->num_slots = (img->usage == SG_USAGE_IMMUTABLE) ? 1 :SG_NUM_INFLIGHT_FRAMES; |
| 3269 | img->active_slot = 0; |
| 3270 | return SG_RESOURCESTATE_VALID; |
| 3271 | } |
| 3272 | |
| 3273 | _SOKOL_PRIVATE void _sg_destroy_image(_sg_image_t* img) { |
| 3274 | SOKOL_ASSERT(img); |
| 3275 | _SOKOL_UNUSED(img); |
| 3276 | } |
| 3277 | |
| 3278 | _SOKOL_PRIVATE sg_resource_state _sg_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) { |
| 3279 | SOKOL_ASSERT(shd && desc); |
| 3280 | /* uniform block sizes and image types */ |
| 3281 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 3282 | const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS) ? &desc->vs : &desc->fs; |
| 3283 | _sg_shader_stage_t* stage = &shd->stage[stage_index]; |
| 3284 | SOKOL_ASSERT(stage->num_uniform_blocks == 0); |
| 3285 | for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) { |
| 3286 | const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index]; |
| 3287 | if (0 == ub_desc->size) { |
| 3288 | break; |
| 3289 | } |
| 3290 | _sg_uniform_block_t* ub = &stage->uniform_blocks[ub_index]; |
| 3291 | ub->size = ub_desc->size; |
| 3292 | stage->num_uniform_blocks++; |
| 3293 | } |
| 3294 | SOKOL_ASSERT(stage->num_images == 0); |
| 3295 | for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) { |
| 3296 | const sg_shader_image_desc* img_desc = &stage_desc->images[img_index]; |
| 3297 | if (img_desc->type == _SG_IMAGETYPE_DEFAULT) { |
| 3298 | break; |
| 3299 | } |
| 3300 | stage->images[img_index].type = img_desc->type; |
| 3301 | stage->num_images++; |
| 3302 | } |
| 3303 | } |
| 3304 | return SG_RESOURCESTATE_VALID; |
| 3305 | } |
| 3306 | |
| 3307 | _SOKOL_PRIVATE void _sg_destroy_shader(_sg_shader_t* shd) { |
| 3308 | SOKOL_ASSERT(shd); |
| 3309 | _SOKOL_UNUSED(shd); |
| 3310 | } |
| 3311 | |
| 3312 | _SOKOL_PRIVATE sg_resource_state _sg_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) { |
| 3313 | SOKOL_ASSERT(pip && desc); |
| 3314 | pip->shader = shd; |
| 3315 | pip->shader_id = desc->shader; |
| 3316 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 3317 | const sg_vertex_attr_desc* a_desc = &desc->layout.attrs[attr_index]; |
| 3318 | if (a_desc->format == SG_VERTEXFORMAT_INVALID) { |
| 3319 | break; |
| 3320 | } |
| 3321 | SOKOL_ASSERT((a_desc->buffer_index >= 0) && (a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS)); |
| 3322 | pip->vertex_layout_valid[a_desc->buffer_index] = true; |
| 3323 | } |
| 3324 | pip->color_attachment_count = desc->blend.color_attachment_count; |
| 3325 | pip->color_format = desc->blend.color_format; |
| 3326 | pip->depth_format = desc->blend.depth_format; |
| 3327 | pip->sample_count = desc->rasterizer.sample_count; |
| 3328 | pip->depth_bias = desc->rasterizer.depth_bias; |
| 3329 | pip->depth_bias_slope_scale = desc->rasterizer.depth_bias_slope_scale; |
| 3330 | pip->depth_bias_clamp = desc->rasterizer.depth_bias_clamp; |
| 3331 | pip->index_type = desc->index_type; |
| 3332 | for (int i = 0; i < 4; i++) { |
| 3333 | pip->blend_color[i] = desc->blend.blend_color[i]; |
| 3334 | } |
| 3335 | return SG_RESOURCESTATE_VALID; |
| 3336 | } |
| 3337 | |
| 3338 | _SOKOL_PRIVATE void _sg_destroy_pipeline(_sg_pipeline_t* pip) { |
| 3339 | SOKOL_ASSERT(pip); |
| 3340 | _SOKOL_UNUSED(pip); |
| 3341 | } |
| 3342 | |
| 3343 | _SOKOL_PRIVATE sg_resource_state _sg_create_pass(_sg_pass_t* pass, _sg_image_t** att_images, const sg_pass_desc* desc) { |
| 3344 | SOKOL_ASSERT(pass && desc); |
| 3345 | SOKOL_ASSERT(att_images && att_images[0]); |
| 3346 | /* copy image pointers and desc attributes */ |
| 3347 | const sg_attachment_desc* att_desc; |
| 3348 | _sg_attachment_t* att; |
| 3349 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 3350 | SOKOL_ASSERT(0 == pass->color_atts[i].image); |
| 3351 | att_desc = &desc->color_attachments[i]; |
| 3352 | if (att_desc->image.id != SG_INVALID_ID) { |
| 3353 | pass->num_color_atts++; |
| 3354 | SOKOL_ASSERT(att_images[i] && (att_images[i]->slot.id == att_desc->image.id)); |
| 3355 | SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(att_images[i]->pixel_format)); |
| 3356 | att = &pass->color_atts[i]; |
| 3357 | SOKOL_ASSERT((att->image == 0) && (att->image_id.id == SG_INVALID_ID)); |
| 3358 | att->image = att_images[i]; |
| 3359 | att->image_id = att_desc->image; |
| 3360 | att->mip_level = att_desc->mip_level; |
| 3361 | att->slice = att_desc->slice; |
| 3362 | } |
| 3363 | } |
| 3364 | SOKOL_ASSERT(0 == pass->ds_att.image); |
| 3365 | att_desc = &desc->depth_stencil_attachment; |
| 3366 | const int ds_img_index = SG_MAX_COLOR_ATTACHMENTS; |
| 3367 | if (att_desc->image.id != SG_INVALID_ID) { |
| 3368 | SOKOL_ASSERT(att_images[ds_img_index] && (att_images[ds_img_index]->slot.id == att_desc->image.id)); |
| 3369 | SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(att_images[ds_img_index]->pixel_format)); |
| 3370 | att = &pass->ds_att; |
| 3371 | SOKOL_ASSERT((att->image == 0) && (att->image_id.id == SG_INVALID_ID)); |
| 3372 | att->image = att_images[ds_img_index]; |
| 3373 | att->image_id = att_desc->image; |
| 3374 | att->mip_level = att_desc->mip_level; |
| 3375 | att->slice = att_desc->slice; |
| 3376 | } |
| 3377 | return SG_RESOURCESTATE_VALID; |
| 3378 | } |
| 3379 | |
| 3380 | _SOKOL_PRIVATE void _sg_destroy_pass(_sg_pass_t* pass) { |
| 3381 | SOKOL_ASSERT(pass); |
| 3382 | _SOKOL_UNUSED(pass); |
| 3383 | } |
| 3384 | |
| 3385 | _SOKOL_PRIVATE void _sg_begin_pass(_sg_pass_t* pass, const sg_pass_action* action, int w, int h) { |
| 3386 | SOKOL_ASSERT(action); |
| 3387 | _SOKOL_UNUSED(pass); |
| 3388 | _SOKOL_UNUSED(action); |
| 3389 | _SOKOL_UNUSED(w); |
| 3390 | _SOKOL_UNUSED(h); |
| 3391 | } |
| 3392 | |
| 3393 | _SOKOL_PRIVATE void _sg_end_pass(void) { |
| 3394 | /* empty */ |
| 3395 | } |
| 3396 | |
| 3397 | _SOKOL_PRIVATE void _sg_commit(void) { |
| 3398 | /* empty */ |
| 3399 | } |
| 3400 | |
| 3401 | _SOKOL_PRIVATE void _sg_apply_viewport(int x, int y, int w, int h, bool origin_top_left) { |
| 3402 | _SOKOL_UNUSED(x); |
| 3403 | _SOKOL_UNUSED(y); |
| 3404 | _SOKOL_UNUSED(w); |
| 3405 | _SOKOL_UNUSED(h); |
| 3406 | _SOKOL_UNUSED(origin_top_left); |
| 3407 | } |
| 3408 | |
| 3409 | _SOKOL_PRIVATE void _sg_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) { |
| 3410 | _SOKOL_UNUSED(x); |
| 3411 | _SOKOL_UNUSED(y); |
| 3412 | _SOKOL_UNUSED(w); |
| 3413 | _SOKOL_UNUSED(h); |
| 3414 | _SOKOL_UNUSED(origin_top_left); |
| 3415 | } |
| 3416 | |
| 3417 | _SOKOL_PRIVATE void _sg_apply_pipeline(_sg_pipeline_t* pip) { |
| 3418 | SOKOL_ASSERT(pip); |
| 3419 | _SOKOL_UNUSED(pip); |
| 3420 | } |
| 3421 | |
| 3422 | _SOKOL_PRIVATE void _sg_apply_bindings( |
| 3423 | _sg_pipeline_t* pip, |
| 3424 | _sg_buffer_t** vbs, const int* vb_offsets, int num_vbs, |
| 3425 | _sg_buffer_t* ib, int ib_offset, |
| 3426 | _sg_image_t** vs_imgs, int num_vs_imgs, |
| 3427 | _sg_image_t** fs_imgs, int num_fs_imgs) |
| 3428 | { |
| 3429 | SOKOL_ASSERT(pip); |
| 3430 | SOKOL_ASSERT(vbs && vb_offsets); |
| 3431 | SOKOL_ASSERT(vs_imgs); |
| 3432 | SOKOL_ASSERT(fs_imgs); |
| 3433 | _SOKOL_UNUSED(pip); |
| 3434 | _SOKOL_UNUSED(vbs); _SOKOL_UNUSED(vb_offsets); _SOKOL_UNUSED(num_vbs); |
| 3435 | _SOKOL_UNUSED(ib); _SOKOL_UNUSED(ib_offset); |
| 3436 | _SOKOL_UNUSED(vs_imgs); _SOKOL_UNUSED(num_vs_imgs); |
| 3437 | _SOKOL_UNUSED(fs_imgs); _SOKOL_UNUSED(num_fs_imgs); |
| 3438 | } |
| 3439 | |
| 3440 | _SOKOL_PRIVATE void _sg_apply_uniforms(sg_shader_stage stage_index, int ub_index, const void* data, int num_bytes) { |
| 3441 | SOKOL_ASSERT(data && (num_bytes > 0)); |
| 3442 | SOKOL_ASSERT((stage_index >= 0) && ((int)stage_index < SG_NUM_SHADER_STAGES)); |
| 3443 | SOKOL_ASSERT((ub_index >= 0) && (ub_index < SG_MAX_SHADERSTAGE_UBS)); |
| 3444 | _SOKOL_UNUSED(stage_index); |
| 3445 | _SOKOL_UNUSED(ub_index); |
| 3446 | _SOKOL_UNUSED(data); |
| 3447 | _SOKOL_UNUSED(num_bytes); |
| 3448 | } |
| 3449 | |
| 3450 | _SOKOL_PRIVATE void _sg_draw(int base_element, int num_elements, int num_instances) { |
| 3451 | _SOKOL_UNUSED(base_element); |
| 3452 | _SOKOL_UNUSED(num_elements); |
| 3453 | _SOKOL_UNUSED(num_instances); |
| 3454 | } |
| 3455 | |
| 3456 | _SOKOL_PRIVATE void _sg_update_buffer(_sg_buffer_t* buf, const void* data, int data_size) { |
| 3457 | SOKOL_ASSERT(buf && data && (data_size > 0)); |
| 3458 | _SOKOL_UNUSED(data); |
| 3459 | _SOKOL_UNUSED(data_size); |
| 3460 | if (++buf->active_slot >= buf->num_slots) { |
| 3461 | buf->active_slot = 0; |
| 3462 | } |
| 3463 | } |
| 3464 | |
| 3465 | _SOKOL_PRIVATE void _sg_append_buffer(_sg_buffer_t* buf, const void* data, int data_size, bool new_frame) { |
| 3466 | SOKOL_ASSERT(buf && data && (data_size > 0)); |
| 3467 | _SOKOL_UNUSED(data); |
| 3468 | _SOKOL_UNUSED(data_size); |
| 3469 | if (new_frame) { |
| 3470 | if (++buf->active_slot >= buf->num_slots) { |
| 3471 | buf->active_slot = 0; |
| 3472 | } |
| 3473 | } |
| 3474 | } |
| 3475 | |
| 3476 | _SOKOL_PRIVATE void _sg_update_image(_sg_image_t* img, const sg_image_content* data) { |
| 3477 | SOKOL_ASSERT(img && data); |
| 3478 | _SOKOL_UNUSED(data); |
| 3479 | if (++img->active_slot >= img->num_slots) { |
| 3480 | img->active_slot = 0; |
| 3481 | } |
| 3482 | } |
| 3483 | |
| 3484 | /*== GL BACKEND ==============================================================*/ |
| 3485 | #elif defined(SOKOL_GLCORE33) || defined(SOKOL_GLES2) || defined(SOKOL_GLES3) |
| 3486 | |
| 3487 | /*-- type translation --------------------------------------------------------*/ |
| 3488 | _SOKOL_PRIVATE GLenum _sg_gl_buffer_target(sg_buffer_type t) { |
| 3489 | switch (t) { |
| 3490 | case SG_BUFFERTYPE_VERTEXBUFFER: return GL_ARRAY_BUFFER; |
| 3491 | case SG_BUFFERTYPE_INDEXBUFFER: return GL_ELEMENT_ARRAY_BUFFER; |
| 3492 | default: SOKOL_UNREACHABLE; return 0; |
| 3493 | } |
| 3494 | } |
| 3495 | |
| 3496 | _SOKOL_PRIVATE GLenum _sg_gl_texture_target(sg_image_type t) { |
| 3497 | switch (t) { |
| 3498 | case SG_IMAGETYPE_2D: return GL_TEXTURE_2D; |
| 3499 | case SG_IMAGETYPE_CUBE: return GL_TEXTURE_CUBE_MAP; |
| 3500 | #if !defined(SOKOL_GLES2) |
| 3501 | case SG_IMAGETYPE_3D: return GL_TEXTURE_3D; |
| 3502 | case SG_IMAGETYPE_ARRAY: return GL_TEXTURE_2D_ARRAY; |
| 3503 | #endif |
| 3504 | default: SOKOL_UNREACHABLE; return 0; |
| 3505 | } |
| 3506 | } |
| 3507 | |
| 3508 | _SOKOL_PRIVATE GLenum _sg_gl_usage(sg_usage u) { |
| 3509 | switch (u) { |
| 3510 | case SG_USAGE_IMMUTABLE: return GL_STATIC_DRAW; |
| 3511 | case SG_USAGE_DYNAMIC: return GL_DYNAMIC_DRAW; |
| 3512 | case SG_USAGE_STREAM: return GL_STREAM_DRAW; |
| 3513 | default: SOKOL_UNREACHABLE; return 0; |
| 3514 | } |
| 3515 | } |
| 3516 | |
| 3517 | _SOKOL_PRIVATE GLenum _sg_gl_shader_stage(sg_shader_stage stage) { |
| 3518 | switch (stage) { |
| 3519 | case SG_SHADERSTAGE_VS: return GL_VERTEX_SHADER; |
| 3520 | case SG_SHADERSTAGE_FS: return GL_FRAGMENT_SHADER; |
| 3521 | default: SOKOL_UNREACHABLE; return 0; |
| 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | _SOKOL_PRIVATE GLint _sg_gl_vertexformat_size(sg_vertex_format fmt) { |
| 3526 | switch (fmt) { |
| 3527 | case SG_VERTEXFORMAT_FLOAT: return 1; |
| 3528 | case SG_VERTEXFORMAT_FLOAT2: return 2; |
| 3529 | case SG_VERTEXFORMAT_FLOAT3: return 3; |
| 3530 | case SG_VERTEXFORMAT_FLOAT4: return 4; |
| 3531 | case SG_VERTEXFORMAT_BYTE4: return 4; |
| 3532 | case SG_VERTEXFORMAT_BYTE4N: return 4; |
| 3533 | case SG_VERTEXFORMAT_UBYTE4: return 4; |
| 3534 | case SG_VERTEXFORMAT_UBYTE4N: return 4; |
| 3535 | case SG_VERTEXFORMAT_SHORT2: return 2; |
| 3536 | case SG_VERTEXFORMAT_SHORT2N: return 2; |
| 3537 | case SG_VERTEXFORMAT_SHORT4: return 4; |
| 3538 | case SG_VERTEXFORMAT_SHORT4N: return 4; |
| 3539 | case SG_VERTEXFORMAT_UINT10_N2: return 4; |
| 3540 | default: SOKOL_UNREACHABLE; return 0; |
| 3541 | } |
| 3542 | } |
| 3543 | |
| 3544 | _SOKOL_PRIVATE GLenum _sg_gl_vertexformat_type(sg_vertex_format fmt) { |
| 3545 | switch (fmt) { |
| 3546 | case SG_VERTEXFORMAT_FLOAT: |
| 3547 | case SG_VERTEXFORMAT_FLOAT2: |
| 3548 | case SG_VERTEXFORMAT_FLOAT3: |
| 3549 | case SG_VERTEXFORMAT_FLOAT4: |
| 3550 | return GL_FLOAT; |
| 3551 | case SG_VERTEXFORMAT_BYTE4: |
| 3552 | case SG_VERTEXFORMAT_BYTE4N: |
| 3553 | return GL_BYTE; |
| 3554 | case SG_VERTEXFORMAT_UBYTE4: |
| 3555 | case SG_VERTEXFORMAT_UBYTE4N: |
| 3556 | return GL_UNSIGNED_BYTE; |
| 3557 | case SG_VERTEXFORMAT_SHORT2: |
| 3558 | case SG_VERTEXFORMAT_SHORT2N: |
| 3559 | case SG_VERTEXFORMAT_SHORT4: |
| 3560 | case SG_VERTEXFORMAT_SHORT4N: |
| 3561 | return GL_SHORT; |
| 3562 | case SG_VERTEXFORMAT_UINT10_N2: |
| 3563 | return GL_UNSIGNED_INT_2_10_10_10_REV; |
| 3564 | default: |
| 3565 | SOKOL_UNREACHABLE; return 0; |
| 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | _SOKOL_PRIVATE GLboolean _sg_gl_vertexformat_normalized(sg_vertex_format fmt) { |
| 3570 | switch (fmt) { |
| 3571 | case SG_VERTEXFORMAT_BYTE4N: |
| 3572 | case SG_VERTEXFORMAT_UBYTE4N: |
| 3573 | case SG_VERTEXFORMAT_SHORT2N: |
| 3574 | case SG_VERTEXFORMAT_SHORT4N: |
| 3575 | case SG_VERTEXFORMAT_UINT10_N2: |
| 3576 | return GL_TRUE; |
| 3577 | default: |
| 3578 | return GL_FALSE; |
| 3579 | } |
| 3580 | } |
| 3581 | |
| 3582 | _SOKOL_PRIVATE GLenum _sg_gl_primitive_type(sg_primitive_type t) { |
| 3583 | switch (t) { |
| 3584 | case SG_PRIMITIVETYPE_POINTS: return GL_POINTS; |
| 3585 | case SG_PRIMITIVETYPE_LINES: return GL_LINES; |
| 3586 | case SG_PRIMITIVETYPE_LINE_STRIP: return GL_LINE_STRIP; |
| 3587 | case SG_PRIMITIVETYPE_TRIANGLES: return GL_TRIANGLES; |
| 3588 | case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return GL_TRIANGLE_STRIP; |
| 3589 | default: SOKOL_UNREACHABLE; return 0; |
| 3590 | } |
| 3591 | } |
| 3592 | |
| 3593 | _SOKOL_PRIVATE GLenum _sg_gl_index_type(sg_index_type t) { |
| 3594 | switch (t) { |
| 3595 | case SG_INDEXTYPE_NONE: return 0; |
| 3596 | case SG_INDEXTYPE_UINT16: return GL_UNSIGNED_SHORT; |
| 3597 | case SG_INDEXTYPE_UINT32: return GL_UNSIGNED_INT; |
| 3598 | default: SOKOL_UNREACHABLE; return 0; |
| 3599 | } |
| 3600 | } |
| 3601 | |
| 3602 | _SOKOL_PRIVATE GLenum _sg_gl_compare_func(sg_compare_func cmp) { |
| 3603 | switch (cmp) { |
| 3604 | case SG_COMPAREFUNC_NEVER: return GL_NEVER; |
| 3605 | case SG_COMPAREFUNC_LESS: return GL_LESS; |
| 3606 | case SG_COMPAREFUNC_EQUAL: return GL_EQUAL; |
| 3607 | case SG_COMPAREFUNC_LESS_EQUAL: return GL_LEQUAL; |
| 3608 | case SG_COMPAREFUNC_GREATER: return GL_GREATER; |
| 3609 | case SG_COMPAREFUNC_NOT_EQUAL: return GL_NOTEQUAL; |
| 3610 | case SG_COMPAREFUNC_GREATER_EQUAL: return GL_GEQUAL; |
| 3611 | case SG_COMPAREFUNC_ALWAYS: return GL_ALWAYS; |
| 3612 | default: SOKOL_UNREACHABLE; return 0; |
| 3613 | } |
| 3614 | } |
| 3615 | |
| 3616 | _SOKOL_PRIVATE GLenum _sg_gl_stencil_op(sg_stencil_op op) { |
| 3617 | switch (op) { |
| 3618 | case SG_STENCILOP_KEEP: return GL_KEEP; |
| 3619 | case SG_STENCILOP_ZERO: return GL_ZERO; |
| 3620 | case SG_STENCILOP_REPLACE: return GL_REPLACE; |
| 3621 | case SG_STENCILOP_INCR_CLAMP: return GL_INCR; |
| 3622 | case SG_STENCILOP_DECR_CLAMP: return GL_DECR; |
| 3623 | case SG_STENCILOP_INVERT: return GL_INVERT; |
| 3624 | case SG_STENCILOP_INCR_WRAP: return GL_INCR_WRAP; |
| 3625 | case SG_STENCILOP_DECR_WRAP: return GL_DECR_WRAP; |
| 3626 | default: SOKOL_UNREACHABLE; return 0; |
| 3627 | } |
| 3628 | } |
| 3629 | |
| 3630 | _SOKOL_PRIVATE GLenum _sg_gl_blend_factor(sg_blend_factor f) { |
| 3631 | switch (f) { |
| 3632 | case SG_BLENDFACTOR_ZERO: return GL_ZERO; |
| 3633 | case SG_BLENDFACTOR_ONE: return GL_ONE; |
| 3634 | case SG_BLENDFACTOR_SRC_COLOR: return GL_SRC_COLOR; |
| 3635 | case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return GL_ONE_MINUS_SRC_COLOR; |
| 3636 | case SG_BLENDFACTOR_SRC_ALPHA: return GL_SRC_ALPHA; |
| 3637 | case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return GL_ONE_MINUS_SRC_ALPHA; |
| 3638 | case SG_BLENDFACTOR_DST_COLOR: return GL_DST_COLOR; |
| 3639 | case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return GL_ONE_MINUS_DST_COLOR; |
| 3640 | case SG_BLENDFACTOR_DST_ALPHA: return GL_DST_ALPHA; |
| 3641 | case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return GL_ONE_MINUS_DST_ALPHA; |
| 3642 | case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return GL_SRC_ALPHA_SATURATE; |
| 3643 | case SG_BLENDFACTOR_BLEND_COLOR: return GL_CONSTANT_COLOR; |
| 3644 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return GL_ONE_MINUS_CONSTANT_COLOR; |
| 3645 | case SG_BLENDFACTOR_BLEND_ALPHA: return GL_CONSTANT_ALPHA; |
| 3646 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return GL_ONE_MINUS_CONSTANT_ALPHA; |
| 3647 | default: SOKOL_UNREACHABLE; return 0; |
| 3648 | } |
| 3649 | } |
| 3650 | |
| 3651 | _SOKOL_PRIVATE GLenum _sg_gl_blend_op(sg_blend_op op) { |
| 3652 | switch (op) { |
| 3653 | case SG_BLENDOP_ADD: return GL_FUNC_ADD; |
| 3654 | case SG_BLENDOP_SUBTRACT: return GL_FUNC_SUBTRACT; |
| 3655 | case SG_BLENDOP_REVERSE_SUBTRACT: return GL_FUNC_REVERSE_SUBTRACT; |
| 3656 | default: SOKOL_UNREACHABLE; return 0; |
| 3657 | } |
| 3658 | } |
| 3659 | |
| 3660 | _SOKOL_PRIVATE GLenum _sg_gl_filter(sg_filter f) { |
| 3661 | switch (f) { |
| 3662 | case SG_FILTER_NEAREST: return GL_NEAREST; |
| 3663 | case SG_FILTER_LINEAR: return GL_LINEAR; |
| 3664 | case SG_FILTER_NEAREST_MIPMAP_NEAREST: return GL_NEAREST_MIPMAP_NEAREST; |
| 3665 | case SG_FILTER_NEAREST_MIPMAP_LINEAR: return GL_NEAREST_MIPMAP_LINEAR; |
| 3666 | case SG_FILTER_LINEAR_MIPMAP_NEAREST: return GL_LINEAR_MIPMAP_NEAREST; |
| 3667 | case SG_FILTER_LINEAR_MIPMAP_LINEAR: return GL_LINEAR_MIPMAP_LINEAR; |
| 3668 | default: SOKOL_UNREACHABLE; return 0; |
| 3669 | } |
| 3670 | } |
| 3671 | |
| 3672 | _SOKOL_PRIVATE GLenum _sg_gl_wrap(sg_wrap w) { |
| 3673 | switch (w) { |
| 3674 | case SG_WRAP_CLAMP_TO_EDGE: return GL_CLAMP_TO_EDGE; |
| 3675 | case SG_WRAP_REPEAT: return GL_REPEAT; |
| 3676 | case SG_WRAP_MIRRORED_REPEAT: return GL_MIRRORED_REPEAT; |
| 3677 | default: SOKOL_UNREACHABLE; return 0; |
| 3678 | } |
| 3679 | } |
| 3680 | |
| 3681 | _SOKOL_PRIVATE GLenum _sg_gl_teximage_type(sg_pixel_format fmt) { |
| 3682 | switch (fmt) { |
| 3683 | case SG_PIXELFORMAT_RGBA32F: |
| 3684 | case SG_PIXELFORMAT_R32F: |
| 3685 | return GL_FLOAT; |
| 3686 | case SG_PIXELFORMAT_RGBA16F: |
| 3687 | case SG_PIXELFORMAT_R16F: |
| 3688 | return GL_HALF_FLOAT; |
| 3689 | case SG_PIXELFORMAT_RGBA8: |
| 3690 | case SG_PIXELFORMAT_RGB8: |
| 3691 | case SG_PIXELFORMAT_L8: |
| 3692 | return GL_UNSIGNED_BYTE; |
| 3693 | case SG_PIXELFORMAT_R10G10B10A2: |
| 3694 | return GL_UNSIGNED_INT_2_10_10_10_REV; |
| 3695 | case SG_PIXELFORMAT_R5G5B5A1: |
| 3696 | return GL_UNSIGNED_SHORT_5_5_5_1; |
| 3697 | case SG_PIXELFORMAT_R5G6B5: |
| 3698 | return GL_UNSIGNED_SHORT_5_6_5; |
| 3699 | case SG_PIXELFORMAT_RGBA4: |
| 3700 | return GL_UNSIGNED_SHORT_4_4_4_4; |
| 3701 | case SG_PIXELFORMAT_DEPTH: |
| 3702 | /* FIXME */ |
| 3703 | return GL_UNSIGNED_SHORT; |
| 3704 | case SG_PIXELFORMAT_DEPTHSTENCIL: |
| 3705 | /* FIXME */ |
| 3706 | return GL_UNSIGNED_INT_24_8; |
| 3707 | default: |
| 3708 | SOKOL_UNREACHABLE; return 0; |
| 3709 | } |
| 3710 | } |
| 3711 | |
| 3712 | _SOKOL_PRIVATE GLenum _sg_gl_teximage_format(sg_pixel_format fmt) { |
| 3713 | switch (fmt) { |
| 3714 | case SG_PIXELFORMAT_NONE: |
| 3715 | return 0; |
| 3716 | case SG_PIXELFORMAT_RGBA8: |
| 3717 | case SG_PIXELFORMAT_R5G5B5A1: |
| 3718 | case SG_PIXELFORMAT_RGBA4: |
| 3719 | case SG_PIXELFORMAT_RGBA32F: |
| 3720 | case SG_PIXELFORMAT_RGBA16F: |
| 3721 | case SG_PIXELFORMAT_R10G10B10A2: |
| 3722 | return GL_RGBA; |
| 3723 | case SG_PIXELFORMAT_RGB8: |
| 3724 | case SG_PIXELFORMAT_R5G6B5: |
| 3725 | return GL_RGB; |
| 3726 | case SG_PIXELFORMAT_L8: |
| 3727 | case SG_PIXELFORMAT_R32F: |
| 3728 | case SG_PIXELFORMAT_R16F: |
| 3729 | #if defined(SOKOL_GLES2) |
| 3730 | return GL_LUMINANCE; |
| 3731 | #else |
| 3732 | if (_sg.gl.gles2) { |
| 3733 | return GL_LUMINANCE; |
| 3734 | } |
| 3735 | else { |
| 3736 | return GL_RED; |
| 3737 | } |
| 3738 | #endif |
| 3739 | case SG_PIXELFORMAT_DEPTH: |
| 3740 | return GL_DEPTH_COMPONENT; |
| 3741 | case SG_PIXELFORMAT_DEPTHSTENCIL: |
| 3742 | return GL_DEPTH_STENCIL; |
| 3743 | case SG_PIXELFORMAT_DXT1: |
| 3744 | return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
| 3745 | case SG_PIXELFORMAT_DXT3: |
| 3746 | return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; |
| 3747 | case SG_PIXELFORMAT_DXT5: |
| 3748 | return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; |
| 3749 | case SG_PIXELFORMAT_PVRTC2_RGB: |
| 3750 | return GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG; |
| 3751 | case SG_PIXELFORMAT_PVRTC4_RGB: |
| 3752 | return GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG; |
| 3753 | case SG_PIXELFORMAT_PVRTC2_RGBA: |
| 3754 | return GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; |
| 3755 | case SG_PIXELFORMAT_PVRTC4_RGBA: |
| 3756 | return GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; |
| 3757 | case SG_PIXELFORMAT_ETC2_RGB8: |
| 3758 | return GL_COMPRESSED_RGB8_ETC2; |
| 3759 | case SG_PIXELFORMAT_ETC2_SRGB8: |
| 3760 | return GL_COMPRESSED_SRGB8_ETC2; |
| 3761 | default: |
| 3762 | SOKOL_UNREACHABLE; return 0; |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | _SOKOL_PRIVATE GLenum _sg_gl_teximage_internal_format(sg_pixel_format fmt) { |
| 3767 | #if defined(SOKOL_GLES2) |
| 3768 | return _sg_gl_teximage_format(fmt); |
| 3769 | #else |
| 3770 | if (_sg.gl.gles2) { |
| 3771 | return _sg_gl_teximage_format(fmt); |
| 3772 | } |
| 3773 | else { |
| 3774 | switch (fmt) { |
| 3775 | case SG_PIXELFORMAT_NONE: |
| 3776 | return 0; |
| 3777 | case SG_PIXELFORMAT_RGBA8: |
| 3778 | return GL_RGBA8; |
| 3779 | case SG_PIXELFORMAT_RGB8: |
| 3780 | return GL_RGB8; |
| 3781 | case SG_PIXELFORMAT_RGBA4: |
| 3782 | return GL_RGBA4; |
| 3783 | case SG_PIXELFORMAT_R5G6B5: |
| 3784 | #if defined(SOKOL_GLES3) |
| 3785 | return GL_RGB565; |
| 3786 | #else |
| 3787 | return GL_RGB5; |
| 3788 | #endif |
| 3789 | case SG_PIXELFORMAT_R5G5B5A1: |
| 3790 | return GL_RGB5_A1; |
| 3791 | case SG_PIXELFORMAT_R10G10B10A2: |
| 3792 | return GL_RGB10_A2; |
| 3793 | case SG_PIXELFORMAT_RGBA32F: |
| 3794 | return GL_RGBA32F; |
| 3795 | case SG_PIXELFORMAT_RGBA16F: |
| 3796 | return GL_RGBA16F; |
| 3797 | case SG_PIXELFORMAT_R32F: |
| 3798 | return GL_R32F; |
| 3799 | case SG_PIXELFORMAT_R16F: |
| 3800 | return GL_R16F; |
| 3801 | case SG_PIXELFORMAT_L8: |
| 3802 | return GL_R8; |
| 3803 | case SG_PIXELFORMAT_DEPTH: |
| 3804 | /* FIXME */ |
| 3805 | return GL_DEPTH_COMPONENT16; |
| 3806 | case SG_PIXELFORMAT_DEPTHSTENCIL: |
| 3807 | return GL_DEPTH24_STENCIL8; |
| 3808 | case SG_PIXELFORMAT_DXT1: |
| 3809 | return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
| 3810 | case SG_PIXELFORMAT_DXT3: |
| 3811 | return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; |
| 3812 | case SG_PIXELFORMAT_DXT5: |
| 3813 | return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; |
| 3814 | case SG_PIXELFORMAT_PVRTC2_RGB: |
| 3815 | return GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG; |
| 3816 | case SG_PIXELFORMAT_PVRTC4_RGB: |
| 3817 | return GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG; |
| 3818 | case SG_PIXELFORMAT_PVRTC2_RGBA: |
| 3819 | return GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; |
| 3820 | case SG_PIXELFORMAT_PVRTC4_RGBA: |
| 3821 | return GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; |
| 3822 | case SG_PIXELFORMAT_ETC2_RGB8: |
| 3823 | return GL_COMPRESSED_RGB8_ETC2; |
| 3824 | case SG_PIXELFORMAT_ETC2_SRGB8: |
| 3825 | return GL_COMPRESSED_SRGB8_ETC2; |
| 3826 | default: |
| 3827 | SOKOL_UNREACHABLE; return 0; |
| 3828 | } |
| 3829 | } |
| 3830 | #endif |
| 3831 | } |
| 3832 | |
| 3833 | _SOKOL_PRIVATE GLenum _sg_gl_cubeface_target(int face_index) { |
| 3834 | switch (face_index) { |
| 3835 | case 0: return GL_TEXTURE_CUBE_MAP_POSITIVE_X; |
| 3836 | case 1: return GL_TEXTURE_CUBE_MAP_NEGATIVE_X; |
| 3837 | case 2: return GL_TEXTURE_CUBE_MAP_POSITIVE_Y; |
| 3838 | case 3: return GL_TEXTURE_CUBE_MAP_NEGATIVE_Y; |
| 3839 | case 4: return GL_TEXTURE_CUBE_MAP_POSITIVE_Z; |
| 3840 | case 5: return GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; |
| 3841 | default: SOKOL_UNREACHABLE; return 0; |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | _SOKOL_PRIVATE GLenum _sg_gl_depth_attachment_format(sg_pixel_format fmt) { |
| 3846 | switch (fmt) { |
| 3847 | case SG_PIXELFORMAT_DEPTH: return GL_DEPTH_COMPONENT16; |
| 3848 | case SG_PIXELFORMAT_DEPTHSTENCIL: return GL_DEPTH24_STENCIL8; |
| 3849 | default: SOKOL_UNREACHABLE; return 0; |
| 3850 | } |
| 3851 | } |
| 3852 | |
| 3853 | _SOKOL_PRIVATE void _sg_gl_init_attr(_sg_gl_attr_t* attr) { |
| 3854 | attr->vb_index = -1; |
| 3855 | attr->divisor = -1; |
| 3856 | } |
| 3857 | |
| 3858 | _SOKOL_PRIVATE void _sg_gl_init_stencil_state(sg_stencil_state* s) { |
| 3859 | SOKOL_ASSERT(s); |
| 3860 | s->fail_op = SG_STENCILOP_KEEP; |
| 3861 | s->depth_fail_op = SG_STENCILOP_KEEP; |
| 3862 | s->pass_op = SG_STENCILOP_KEEP; |
| 3863 | s->compare_func = SG_COMPAREFUNC_ALWAYS; |
| 3864 | } |
| 3865 | |
| 3866 | _SOKOL_PRIVATE void _sg_gl_init_depth_stencil_state(sg_depth_stencil_state* s) { |
| 3867 | SOKOL_ASSERT(s); |
| 3868 | _sg_gl_init_stencil_state(&s->stencil_front); |
| 3869 | _sg_gl_init_stencil_state(&s->stencil_back); |
| 3870 | s->depth_compare_func = SG_COMPAREFUNC_ALWAYS; |
| 3871 | } |
| 3872 | |
| 3873 | _SOKOL_PRIVATE void _sg_gl_init_blend_state(sg_blend_state* s) { |
| 3874 | SOKOL_ASSERT(s); |
| 3875 | s->src_factor_rgb = SG_BLENDFACTOR_ONE; |
| 3876 | s->dst_factor_rgb = SG_BLENDFACTOR_ZERO; |
| 3877 | s->op_rgb = SG_BLENDOP_ADD; |
| 3878 | s->src_factor_alpha = SG_BLENDFACTOR_ONE; |
| 3879 | s->dst_factor_alpha = SG_BLENDFACTOR_ZERO; |
| 3880 | s->op_alpha = SG_BLENDOP_ADD; |
| 3881 | s->color_write_mask = SG_COLORMASK_RGBA; |
| 3882 | } |
| 3883 | |
| 3884 | _SOKOL_PRIVATE void _sg_gl_init_rasterizer_state(sg_rasterizer_state* s) { |
| 3885 | SOKOL_ASSERT(s); |
| 3886 | s->cull_mode = SG_CULLMODE_NONE; |
| 3887 | s->face_winding = SG_FACEWINDING_CW; |
| 3888 | s->sample_count = 1; |
| 3889 | } |
| 3890 | |
| 3891 | /*-- state cache implementation ----------------------------------------------*/ |
| 3892 | _SOKOL_PRIVATE void _sg_gl_clear_buffer_bindings(bool force) { |
| 3893 | if (force || (_sg.gl.cache.vertex_buffer != 0)) { |
| 3894 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 3895 | _sg.gl.cache.vertex_buffer = 0; |
| 3896 | } |
| 3897 | if (force || (_sg.gl.cache.index_buffer != 0)) { |
| 3898 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 3899 | _sg.gl.cache.index_buffer = 0; |
| 3900 | } |
| 3901 | } |
| 3902 | |
| 3903 | _SOKOL_PRIVATE void _sg_gl_bind_buffer(GLenum target, GLuint buffer) { |
| 3904 | SOKOL_ASSERT((GL_ARRAY_BUFFER == target) || (GL_ELEMENT_ARRAY_BUFFER == target)); |
| 3905 | if (target == GL_ARRAY_BUFFER) { |
| 3906 | if (_sg.gl.cache.vertex_buffer != buffer) { |
| 3907 | _sg.gl.cache.vertex_buffer = buffer; |
| 3908 | glBindBuffer(target, buffer); |
| 3909 | } |
| 3910 | } |
| 3911 | else { |
| 3912 | if (_sg.gl.cache.index_buffer != buffer) { |
| 3913 | _sg.gl.cache.index_buffer = buffer; |
| 3914 | glBindBuffer(target, buffer); |
| 3915 | } |
| 3916 | } |
| 3917 | } |
| 3918 | |
| 3919 | _SOKOL_PRIVATE void _sg_gl_store_buffer_binding(GLenum target) { |
| 3920 | if (target == GL_ARRAY_BUFFER) { |
| 3921 | _sg.gl.cache.stored_vertex_buffer = _sg.gl.cache.vertex_buffer; |
| 3922 | } |
| 3923 | else { |
| 3924 | _sg.gl.cache.stored_index_buffer = _sg.gl.cache.index_buffer; |
| 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | _SOKOL_PRIVATE void _sg_gl_restore_buffer_binding(GLenum target) { |
| 3929 | if (target == GL_ARRAY_BUFFER) { |
| 3930 | _sg_gl_bind_buffer(target, _sg.gl.cache.stored_vertex_buffer); |
| 3931 | } |
| 3932 | else { |
| 3933 | _sg_gl_bind_buffer(target, _sg.gl.cache.stored_index_buffer); |
| 3934 | } |
| 3935 | } |
| 3936 | |
| 3937 | _SOKOL_PRIVATE void _sg_gl_clear_texture_bindings(bool force) { |
| 3938 | for (int i = 0; (i < SG_MAX_SHADERSTAGE_IMAGES) && (i < _sg.gl.max_combined_texture_image_units); i++) { |
| 3939 | if (force || (_sg.gl.cache.textures[i].texture != 0)) { |
| 3940 | glActiveTexture(GL_TEXTURE0 + i); |
| 3941 | glBindTexture(GL_TEXTURE_2D, 0); |
| 3942 | glBindTexture(GL_TEXTURE_CUBE_MAP, 0); |
| 3943 | #if !defined(SOKOL_GLES2) |
| 3944 | if (!_sg.gl.gles2) { |
| 3945 | glBindTexture(GL_TEXTURE_3D, 0); |
| 3946 | glBindTexture(GL_TEXTURE_2D_ARRAY, 0); |
| 3947 | } |
| 3948 | #endif |
| 3949 | _sg.gl.cache.textures[i].target = 0; |
| 3950 | _sg.gl.cache.textures[i].texture = 0; |
| 3951 | } |
| 3952 | } |
| 3953 | } |
| 3954 | |
| 3955 | _SOKOL_PRIVATE void _sg_gl_bind_texture(int slot_index, GLenum target, GLuint texture) { |
| 3956 | /* it's valid to call this function with target=0 and/or texture=0 |
| 3957 | target=0 will unbind the previous binding, texture=0 will clear |
| 3958 | the new binding |
| 3959 | */ |
| 3960 | SOKOL_ASSERT(slot_index < SG_MAX_SHADERSTAGE_IMAGES); |
| 3961 | if (slot_index >= _sg.gl.max_combined_texture_image_units) { |
| 3962 | return; |
| 3963 | } |
| 3964 | _sg_gl_texture_bind_slot* slot = &_sg.gl.cache.textures[slot_index]; |
| 3965 | if ((slot->target != target) || (slot->texture != texture)) { |
| 3966 | glActiveTexture(GL_TEXTURE0 + slot_index); |
| 3967 | /* if the target has changed, clear the previous binding on that target */ |
| 3968 | if ((target != slot->target) && (slot->target != 0)) { |
| 3969 | glBindTexture(slot->target, 0); |
| 3970 | } |
| 3971 | /* apply new binding (texture can be 0 to unbind) */ |
| 3972 | if (target != 0) { |
| 3973 | glBindTexture(target, texture); |
| 3974 | } |
| 3975 | slot->target = target; |
| 3976 | slot->texture = texture; |
| 3977 | } |
| 3978 | } |
| 3979 | |
| 3980 | _SOKOL_PRIVATE void _sg_gl_store_texture_binding(int slot_index) { |
| 3981 | SOKOL_ASSERT(slot_index < SG_MAX_SHADERSTAGE_IMAGES); |
| 3982 | _sg.gl.cache.stored_texture = _sg.gl.cache.textures[slot_index]; |
| 3983 | } |
| 3984 | |
| 3985 | _SOKOL_PRIVATE void _sg_gl_restore_texture_binding(int slot_index) { |
| 3986 | SOKOL_ASSERT(slot_index < SG_MAX_SHADERSTAGE_IMAGES); |
| 3987 | const _sg_gl_texture_bind_slot* slot = &_sg.gl.cache.stored_texture; |
| 3988 | _sg_gl_bind_texture(slot_index, slot->target, slot->texture); |
| 3989 | } |
| 3990 | |
| 3991 | _SOKOL_PRIVATE void _sg_gl_reset_state_cache(void) { |
| 3992 | _SG_GL_CHECK_ERROR(); |
| 3993 | memset(&_sg.gl.cache, 0, sizeof(_sg.gl.cache)); |
| 3994 | _sg_gl_clear_buffer_bindings(true); |
| 3995 | _SG_GL_CHECK_ERROR(); |
| 3996 | _sg_gl_clear_texture_bindings(true); |
| 3997 | _SG_GL_CHECK_ERROR(); |
| 3998 | for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) { |
| 3999 | _sg_gl_init_attr(&_sg.gl.cache.attrs[i].gl_attr); |
| 4000 | glDisableVertexAttribArray(i); |
| 4001 | _SG_GL_CHECK_ERROR(); |
| 4002 | } |
| 4003 | _sg.gl.cache.cur_primitive_type = GL_TRIANGLES; |
| 4004 | |
| 4005 | /* depth-stencil state */ |
| 4006 | _sg_gl_init_depth_stencil_state(&_sg.gl.cache.ds); |
| 4007 | glEnable(GL_DEPTH_TEST); |
| 4008 | glDepthFunc(GL_ALWAYS); |
| 4009 | glDepthMask(GL_FALSE); |
| 4010 | glDisable(GL_STENCIL_TEST); |
| 4011 | glStencilFunc(GL_ALWAYS, 0, 0); |
| 4012 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
| 4013 | glStencilMask(0); |
| 4014 | |
| 4015 | /* blend state */ |
| 4016 | _sg_gl_init_blend_state(&_sg.gl.cache.blend); |
| 4017 | glDisable(GL_BLEND); |
| 4018 | glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO); |
| 4019 | glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD); |
| 4020 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 4021 | glBlendColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 4022 | |
| 4023 | /* rasterizer state */ |
| 4024 | _sg_gl_init_rasterizer_state(&_sg.gl.cache.rast); |
| 4025 | glPolygonOffset(0.0f, 0.0f); |
| 4026 | glDisable(GL_POLYGON_OFFSET_FILL); |
| 4027 | glDisable(GL_CULL_FACE); |
| 4028 | glFrontFace(GL_CW); |
| 4029 | glCullFace(GL_BACK); |
| 4030 | glEnable(GL_SCISSOR_TEST); |
| 4031 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE); |
| 4032 | glEnable(GL_DITHER); |
| 4033 | glDisable(GL_POLYGON_OFFSET_FILL); |
| 4034 | #if defined(SOKOL_GLCORE33) |
| 4035 | glEnable(GL_MULTISAMPLE); |
| 4036 | glEnable(GL_PROGRAM_POINT_SIZE); |
| 4037 | #endif |
| 4038 | } |
| 4039 | |
| 4040 | /*-- main GL backend state and functions -------------------------------------*/ |
| 4041 | |
| 4042 | _SOKOL_PRIVATE void _sg_setup_backend(const sg_desc* desc) { |
| 4043 | /* assumes that _sg.gl is already zero-initialized */ |
| 4044 | _sg.gl.valid = true; |
| 4045 | #if defined(SOKOL_GLES2) || defined(SOKOL_GLES3) |
| 4046 | _sg.gl.gles2 = desc->gl_force_gles2; |
| 4047 | #else |
| 4048 | _SOKOL_UNUSED(desc); |
| 4049 | _sg.gl.gles2 = false; |
| 4050 | #endif |
| 4051 | |
| 4052 | /* clear initial GL error state */ |
| 4053 | #if defined(SOKOL_DEBUG) |
| 4054 | while (glGetError() != GL_NO_ERROR); |
| 4055 | #endif |
| 4056 | |
| 4057 | /* initialize feature flags */ |
| 4058 | _sg.gl.features[SG_FEATURE_ORIGIN_BOTTOM_LEFT] = true; |
| 4059 | #if defined(SOKOL_GLCORE33) |
| 4060 | _sg.gl.features[SG_FEATURE_INSTANCING] = true; |
| 4061 | _sg.gl.features[SG_FEATURE_TEXTURE_FLOAT] = true; |
| 4062 | _sg.gl.features[SG_FEATURE_TEXTURE_HALF_FLOAT] = true; |
| 4063 | _sg.gl.features[SG_FEATURE_MSAA_RENDER_TARGETS] = true; |
| 4064 | _sg.gl.features[SG_FEATURE_PACKED_VERTEX_FORMAT_10_2] = true; |
| 4065 | _sg.gl.features[SG_FEATURE_MULTIPLE_RENDER_TARGET] = true; |
| 4066 | _sg.gl.features[SG_FEATURE_IMAGETYPE_3D] = true; |
| 4067 | _sg.gl.features[SG_FEATURE_IMAGETYPE_ARRAY] = true; |
| 4068 | GLint num_ext = 0; |
| 4069 | glGetIntegerv(GL_NUM_EXTENSIONS, &num_ext); |
| 4070 | for (int i = 0; i < num_ext; i++) { |
| 4071 | const char* ext = (const char*) glGetStringi(GL_EXTENSIONS, i); |
| 4072 | if (ext) { |
| 4073 | if (strstr(ext, "_texture_compression_s3tc")) { |
| 4074 | _sg.gl.features[SG_FEATURE_TEXTURE_COMPRESSION_DXT] = true; |
| 4075 | continue; |
| 4076 | } |
| 4077 | else if (strstr(ext, "_texture_filter_anisotropic")) { |
| 4078 | _sg.gl.ext_anisotropic = true; |
| 4079 | continue; |
| 4080 | } |
| 4081 | } |
| 4082 | } |
| 4083 | #elif defined(SOKOL_GLES3) |
| 4084 | const char* ext = (const char*) glGetString(GL_EXTENSIONS); |
| 4085 | if (ext) { |
| 4086 | if (!_sg.gl.gles2) { |
| 4087 | _sg.gl.features[SG_FEATURE_INSTANCING] = true; |
| 4088 | _sg.gl.features[SG_FEATURE_TEXTURE_FLOAT] = true; |
| 4089 | _sg.gl.features[SG_FEATURE_TEXTURE_HALF_FLOAT] = true; |
| 4090 | _sg.gl.features[SG_FEATURE_IMAGETYPE_3D] = true; |
| 4091 | _sg.gl.features[SG_FEATURE_IMAGETYPE_ARRAY] = true; |
| 4092 | _sg.gl.features[SG_FEATURE_MSAA_RENDER_TARGETS] = true; |
| 4093 | _sg.gl.features[SG_FEATURE_PACKED_VERTEX_FORMAT_10_2] = true; |
| 4094 | _sg.gl.features[SG_FEATURE_MULTIPLE_RENDER_TARGET] = true; |
| 4095 | } |
| 4096 | else { |
| 4097 | _sg.gl.features[SG_FEATURE_INSTANCING] = strstr(ext, "_instanced_arrays"); |
| 4098 | _sg.gl.features[SG_FEATURE_TEXTURE_FLOAT] = strstr(ext, "_texture_float"); |
| 4099 | _sg.gl.features[SG_FEATURE_TEXTURE_HALF_FLOAT] = strstr(ext, "_texture_half_float"); |
| 4100 | } |
| 4101 | _sg.gl.features[SG_FEATURE_TEXTURE_COMPRESSION_DXT] = |
| 4102 | strstr(ext, "_texture_compression_s3tc") || |
| 4103 | strstr(ext, "_compressed_texture_s3tc") || |
| 4104 | strstr(ext, "texture_compression_dxt1"); |
| 4105 | _sg.gl.features[SG_FEATURE_TEXTURE_COMPRESSION_PVRTC] = |
| 4106 | strstr(ext, "_texture_compression_pvrtc") || |
| 4107 | strstr(ext, "_compressed_texture_pvrtc"); |
| 4108 | _sg.gl.features[SG_FEATURE_TEXTURE_COMPRESSION_ATC] = |
| 4109 | strstr(ext, "_compressed_texture_atc"); |
| 4110 | _sg.gl.ext_anisotropic = |
| 4111 | strstr(ext, "_texture_filter_anisotropic"); |
| 4112 | } |
| 4113 | #elif defined(SOKOL_GLES2) |
| 4114 | const char* ext = (const char*) glGetString(GL_EXTENSIONS); |
| 4115 | if (ext) { |
| 4116 | _sg.gl.features[SG_FEATURE_INSTANCING] = |
| 4117 | strstr(ext, "_instanced_arrays"); |
| 4118 | _sg.gl.features[SG_FEATURE_TEXTURE_FLOAT] = |
| 4119 | strstr(ext, "_texture_float"); |
| 4120 | _sg.gl.features[SG_FEATURE_TEXTURE_HALF_FLOAT] = |
| 4121 | strstr(ext, "_texture_half_float"); |
| 4122 | _sg.gl.features[SG_FEATURE_TEXTURE_COMPRESSION_DXT] = |
| 4123 | strstr(ext, "_texture_compression_s3tc") || |
| 4124 | strstr(ext, "_compressed_texture_s3tc") || |
| 4125 | strstr(ext, "texture_compression_dxt1"); |
| 4126 | _sg.gl.features[SG_FEATURE_TEXTURE_COMPRESSION_PVRTC] = |
| 4127 | strstr(ext, "_texture_compression_pvrtc") || |
| 4128 | strstr(ext, "_compressed_texture_pvrtc"); |
| 4129 | _sg.gl.features[SG_FEATURE_TEXTURE_COMPRESSION_ATC] = |
| 4130 | strstr(ext, "_compressed_texture_atc"); |
| 4131 | _sg.gl.ext_anisotropic = |
| 4132 | strstr(ext, "_texture_filter_anisotropic"); |
| 4133 | } |
| 4134 | #endif |
| 4135 | _sg.gl.max_anisotropy = 1; |
| 4136 | if (_sg.gl.ext_anisotropic) { |
| 4137 | glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &_sg.gl.max_anisotropy); |
| 4138 | } |
| 4139 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &_sg.gl.max_combined_texture_image_units); |
| 4140 | } |
| 4141 | |
| 4142 | _SOKOL_PRIVATE void _sg_discard_backend(void) { |
| 4143 | SOKOL_ASSERT(_sg.gl.valid); |
| 4144 | _sg.gl.valid = false; |
| 4145 | } |
| 4146 | |
| 4147 | _SOKOL_PRIVATE void _sg_reset_state_cache(void) { |
| 4148 | if (_sg.gl.cur_context) { |
| 4149 | #if !defined(SOKOL_GLES2) |
| 4150 | if (!_sg.gl.gles2) { |
| 4151 | _SG_GL_CHECK_ERROR(); |
| 4152 | glBindVertexArray(_sg.gl.cur_context->vao); |
| 4153 | _SG_GL_CHECK_ERROR(); |
| 4154 | } |
| 4155 | #endif |
| 4156 | _sg_gl_reset_state_cache(); |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | _SOKOL_PRIVATE bool _sg_query_feature(sg_feature f) { |
| 4161 | SOKOL_ASSERT((f>=0) && (f<SG_NUM_FEATURES)); |
| 4162 | return _sg.gl.features[f]; |
| 4163 | } |
| 4164 | |
| 4165 | _SOKOL_PRIVATE void _sg_activate_context(_sg_context_t* ctx) { |
| 4166 | SOKOL_ASSERT(_sg.gl.valid); |
| 4167 | /* NOTE: ctx can be 0 to unset the current context */ |
| 4168 | _sg.gl.cur_context = ctx; |
| 4169 | _sg_reset_state_cache(); |
| 4170 | } |
| 4171 | |
| 4172 | /*-- GL backend resource creation and destruction ----------------------------*/ |
| 4173 | _SOKOL_PRIVATE sg_resource_state _sg_create_context(_sg_context_t* ctx) { |
| 4174 | SOKOL_ASSERT(ctx); |
| 4175 | SOKOL_ASSERT(0 == ctx->default_framebuffer); |
| 4176 | _SG_GL_CHECK_ERROR(); |
| 4177 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&ctx->default_framebuffer); |
| 4178 | _SG_GL_CHECK_ERROR(); |
| 4179 | #if !defined(SOKOL_GLES2) |
| 4180 | if (!_sg.gl.gles2) { |
| 4181 | SOKOL_ASSERT(0 == ctx->vao); |
| 4182 | glGenVertexArrays(1, &ctx->vao); |
| 4183 | glBindVertexArray(ctx->vao); |
| 4184 | _SG_GL_CHECK_ERROR(); |
| 4185 | } |
| 4186 | #endif |
| 4187 | return SG_RESOURCESTATE_VALID; |
| 4188 | } |
| 4189 | |
| 4190 | _SOKOL_PRIVATE void _sg_destroy_context(_sg_context_t* ctx) { |
| 4191 | SOKOL_ASSERT(ctx); |
| 4192 | #if !defined(SOKOL_GLES2) |
| 4193 | if (!_sg.gl.gles2) { |
| 4194 | if (ctx->vao) { |
| 4195 | glDeleteVertexArrays(1, &ctx->vao); |
| 4196 | } |
| 4197 | _SG_GL_CHECK_ERROR(); |
| 4198 | } |
| 4199 | #endif |
| 4200 | } |
| 4201 | |
| 4202 | _SOKOL_PRIVATE sg_resource_state _sg_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) { |
| 4203 | SOKOL_ASSERT(buf && desc); |
| 4204 | _SG_GL_CHECK_ERROR(); |
| 4205 | buf->size = desc->size; |
| 4206 | buf->append_pos = 0; |
| 4207 | buf->append_overflow = false; |
| 4208 | buf->type = desc->type; |
| 4209 | buf->usage = desc->usage; |
| 4210 | buf->update_frame_index = 0; |
| 4211 | buf->append_frame_index = 0; |
| 4212 | buf->num_slots = (buf->usage == SG_USAGE_IMMUTABLE) ? 1 : SG_NUM_INFLIGHT_FRAMES; |
| 4213 | buf->active_slot = 0; |
| 4214 | buf->ext_buffers = (0 != desc->gl_buffers[0]); |
| 4215 | GLenum gl_target = _sg_gl_buffer_target(buf->type); |
| 4216 | GLenum gl_usage = _sg_gl_usage(buf->usage); |
| 4217 | for (int slot = 0; slot < buf->num_slots; slot++) { |
| 4218 | GLuint gl_buf = 0; |
| 4219 | if (buf->ext_buffers) { |
| 4220 | SOKOL_ASSERT(desc->gl_buffers[slot]); |
| 4221 | gl_buf = desc->gl_buffers[slot]; |
| 4222 | } |
| 4223 | else { |
| 4224 | glGenBuffers(1, &gl_buf); |
| 4225 | _sg_gl_store_buffer_binding(gl_target); |
| 4226 | _sg_gl_bind_buffer(gl_target, gl_buf); |
| 4227 | glBufferData(gl_target, buf->size, 0, gl_usage); |
| 4228 | if (buf->usage == SG_USAGE_IMMUTABLE) { |
| 4229 | SOKOL_ASSERT(desc->content); |
| 4230 | glBufferSubData(gl_target, 0, buf->size, desc->content); |
| 4231 | } |
| 4232 | _sg_gl_restore_buffer_binding(gl_target); |
| 4233 | } |
| 4234 | buf->gl_buf[slot] = gl_buf; |
| 4235 | } |
| 4236 | _SG_GL_CHECK_ERROR(); |
| 4237 | return SG_RESOURCESTATE_VALID; |
| 4238 | } |
| 4239 | |
| 4240 | _SOKOL_PRIVATE void _sg_destroy_buffer(_sg_buffer_t* buf) { |
| 4241 | SOKOL_ASSERT(buf); |
| 4242 | _SG_GL_CHECK_ERROR(); |
| 4243 | if (!buf->ext_buffers) { |
| 4244 | for (int slot = 0; slot < buf->num_slots; slot++) { |
| 4245 | if (buf->gl_buf[slot]) { |
| 4246 | glDeleteBuffers(1, &buf->gl_buf[slot]); |
| 4247 | } |
| 4248 | } |
| 4249 | _SG_GL_CHECK_ERROR(); |
| 4250 | } |
| 4251 | } |
| 4252 | |
| 4253 | _SOKOL_PRIVATE bool _sg_gl_supported_texture_format(sg_pixel_format fmt) { |
| 4254 | switch (fmt) { |
| 4255 | case SG_PIXELFORMAT_DXT1: |
| 4256 | case SG_PIXELFORMAT_DXT3: |
| 4257 | case SG_PIXELFORMAT_DXT5: |
| 4258 | return _sg.gl.features[SG_FEATURE_TEXTURE_COMPRESSION_DXT]; |
| 4259 | case SG_PIXELFORMAT_PVRTC2_RGB: |
| 4260 | case SG_PIXELFORMAT_PVRTC4_RGB: |
| 4261 | case SG_PIXELFORMAT_PVRTC2_RGBA: |
| 4262 | case SG_PIXELFORMAT_PVRTC4_RGBA: |
| 4263 | return _sg.gl.features[SG_FEATURE_TEXTURE_COMPRESSION_PVRTC]; |
| 4264 | case SG_PIXELFORMAT_ETC2_RGB8: |
| 4265 | case SG_PIXELFORMAT_ETC2_SRGB8: |
| 4266 | return _sg.gl.features[SG_FEATURE_TEXTURE_COMPRESSION_ETC2]; |
| 4267 | default: |
| 4268 | return true; |
| 4269 | } |
| 4270 | } |
| 4271 | |
| 4272 | _SOKOL_PRIVATE sg_resource_state _sg_create_image(_sg_image_t* img, const sg_image_desc* desc) { |
| 4273 | SOKOL_ASSERT(img && desc); |
| 4274 | _SG_GL_CHECK_ERROR(); |
| 4275 | img->type = desc->type; |
| 4276 | img->render_target = desc->render_target; |
| 4277 | img->width = desc->width; |
| 4278 | img->height = desc->height; |
| 4279 | img->depth = desc->depth; |
| 4280 | img->num_mipmaps = desc->num_mipmaps; |
| 4281 | img->usage = desc->usage; |
| 4282 | img->pixel_format = desc->pixel_format; |
| 4283 | img->sample_count = desc->sample_count; |
| 4284 | img->min_filter = desc->min_filter; |
| 4285 | img->mag_filter = desc->mag_filter; |
| 4286 | img->wrap_u = desc->wrap_u; |
| 4287 | img->wrap_v = desc->wrap_v; |
| 4288 | img->wrap_w = desc->wrap_w; |
| 4289 | img->max_anisotropy = desc->max_anisotropy; |
| 4290 | img->upd_frame_index = 0; |
| 4291 | |
| 4292 | /* check if texture format is support */ |
| 4293 | if (!_sg_gl_supported_texture_format(img->pixel_format)) { |
| 4294 | SOKOL_LOG("compressed texture format not supported by GL context\n"); |
| 4295 | return SG_RESOURCESTATE_FAILED; |
| 4296 | } |
| 4297 | /* check for optional texture types */ |
| 4298 | if ((img->type == SG_IMAGETYPE_3D) && !_sg.gl.features[SG_FEATURE_IMAGETYPE_3D]) { |
| 4299 | SOKOL_LOG("3D textures not supported by GL context\n"); |
| 4300 | return SG_RESOURCESTATE_FAILED; |
| 4301 | } |
| 4302 | if ((img->type == SG_IMAGETYPE_ARRAY) && !_sg.gl.features[SG_FEATURE_IMAGETYPE_ARRAY]) { |
| 4303 | SOKOL_LOG("array textures not supported by GL context\n"); |
| 4304 | return SG_RESOURCESTATE_FAILED; |
| 4305 | } |
| 4306 | |
| 4307 | /* create 1 or 2 GL textures, depending on requested update strategy */ |
| 4308 | img->num_slots = (img->usage == SG_USAGE_IMMUTABLE) ? 1 : SG_NUM_INFLIGHT_FRAMES; |
| 4309 | img->active_slot = 0; |
| 4310 | img->ext_textures = (0 != desc->gl_textures[0]); |
| 4311 | |
| 4312 | #if !defined(SOKOL_GLES2) |
| 4313 | bool msaa = false; |
| 4314 | if (!_sg.gl.gles2) { |
| 4315 | msaa = (img->sample_count > 1) && (_sg.gl.features[SG_FEATURE_MSAA_RENDER_TARGETS]); |
| 4316 | } |
| 4317 | #endif |
| 4318 | |
| 4319 | if (_sg_is_valid_rendertarget_depth_format(img->pixel_format)) { |
| 4320 | /* special case depth-stencil-buffer? */ |
| 4321 | SOKOL_ASSERT((img->usage == SG_USAGE_IMMUTABLE) && (img->num_slots == 1)); |
| 4322 | SOKOL_ASSERT(!img->ext_textures); /* cannot provide external texture for depth images */ |
| 4323 | glGenRenderbuffers(1, &img->gl_depth_render_buffer); |
| 4324 | glBindRenderbuffer(GL_RENDERBUFFER, img->gl_depth_render_buffer); |
| 4325 | GLenum gl_depth_format = _sg_gl_depth_attachment_format(img->pixel_format); |
| 4326 | #if !defined(SOKOL_GLES2) |
| 4327 | if (!_sg.gl.gles2 && msaa) { |
| 4328 | glRenderbufferStorageMultisample(GL_RENDERBUFFER, img->sample_count, gl_depth_format, img->width, img->height); |
| 4329 | } |
| 4330 | else |
| 4331 | #endif |
| 4332 | { |
| 4333 | glRenderbufferStorage(GL_RENDERBUFFER, gl_depth_format, img->width, img->height); |
| 4334 | } |
| 4335 | } |
| 4336 | else { |
| 4337 | /* regular color texture */ |
| 4338 | img->gl_target = _sg_gl_texture_target(img->type); |
| 4339 | const GLenum gl_internal_format = _sg_gl_teximage_internal_format(img->pixel_format); |
| 4340 | |
| 4341 | /* if this is a MSAA render target, need to create a separate render buffer */ |
| 4342 | #if !defined(SOKOL_GLES2) |
| 4343 | if (!_sg.gl.gles2 && img->render_target && msaa) { |
| 4344 | glGenRenderbuffers(1, &img->gl_msaa_render_buffer); |
| 4345 | glBindRenderbuffer(GL_RENDERBUFFER, img->gl_msaa_render_buffer); |
| 4346 | glRenderbufferStorageMultisample(GL_RENDERBUFFER, img->sample_count, gl_internal_format, img->width, img->height); |
| 4347 | } |
| 4348 | #endif |
| 4349 | |
| 4350 | if (img->ext_textures) { |
| 4351 | /* inject externally GL textures */ |
| 4352 | for (int slot = 0; slot < img->num_slots; slot++) { |
| 4353 | SOKOL_ASSERT(desc->gl_textures[slot]); |
| 4354 | img->gl_tex[slot] = desc->gl_textures[slot]; |
| 4355 | } |
| 4356 | } |
| 4357 | else { |
| 4358 | /* create our own GL texture(s) */ |
| 4359 | const GLenum gl_format = _sg_gl_teximage_format(img->pixel_format); |
| 4360 | const bool is_compressed = _sg_is_compressed_pixel_format(img->pixel_format); |
| 4361 | for (int slot = 0; slot < img->num_slots; slot++) { |
| 4362 | glGenTextures(1, &img->gl_tex[slot]); |
| 4363 | _sg_gl_store_texture_binding(0); |
| 4364 | _sg_gl_bind_texture(0, img->gl_target, img->gl_tex[slot]); |
| 4365 | GLenum gl_min_filter = _sg_gl_filter(img->min_filter); |
| 4366 | GLenum gl_mag_filter = _sg_gl_filter(img->mag_filter); |
| 4367 | glTexParameteri(img->gl_target, GL_TEXTURE_MIN_FILTER, gl_min_filter); |
| 4368 | glTexParameteri(img->gl_target, GL_TEXTURE_MAG_FILTER, gl_mag_filter); |
| 4369 | if (_sg.gl.ext_anisotropic && (img->max_anisotropy > 1)) { |
| 4370 | GLint max_aniso = (GLint) img->max_anisotropy; |
| 4371 | if (max_aniso > _sg.gl.max_anisotropy) { |
| 4372 | max_aniso = _sg.gl.max_anisotropy; |
| 4373 | } |
| 4374 | glTexParameteri(img->gl_target, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_aniso); |
| 4375 | } |
| 4376 | if (img->type == SG_IMAGETYPE_CUBE) { |
| 4377 | glTexParameteri(img->gl_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 4378 | glTexParameteri(img->gl_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 4379 | } |
| 4380 | else { |
| 4381 | glTexParameteri(img->gl_target, GL_TEXTURE_WRAP_S, _sg_gl_wrap(img->wrap_u)); |
| 4382 | glTexParameteri(img->gl_target, GL_TEXTURE_WRAP_T, _sg_gl_wrap(img->wrap_v)); |
| 4383 | #if !defined(SOKOL_GLES2) |
| 4384 | if (!_sg.gl.gles2 && (img->type == SG_IMAGETYPE_3D)) { |
| 4385 | glTexParameteri(img->gl_target, GL_TEXTURE_WRAP_R, _sg_gl_wrap(img->wrap_w)); |
| 4386 | } |
| 4387 | #endif |
| 4388 | } |
| 4389 | #if !defined(SOKOL_GLES2) |
| 4390 | if (!_sg.gl.gles2) { |
| 4391 | /* GL spec has strange defaults for mipmap min/max lod: -1000 to +1000 */ |
| 4392 | const float min_lod = _sg_clamp(desc->min_lod, 0.0f, 1000.0f); |
| 4393 | const float max_lod = _sg_clamp(desc->max_lod, 0.0f, 1000.0f); |
| 4394 | glTexParameterf(img->gl_target, GL_TEXTURE_MIN_LOD, min_lod); |
| 4395 | glTexParameterf(img->gl_target, GL_TEXTURE_MAX_LOD, max_lod); |
| 4396 | } |
| 4397 | #endif |
| 4398 | const int num_faces = img->type == SG_IMAGETYPE_CUBE ? 6 : 1; |
| 4399 | int data_index = 0; |
| 4400 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 4401 | for (int mip_index = 0; mip_index < img->num_mipmaps; mip_index++, data_index++) { |
| 4402 | GLenum gl_img_target = img->gl_target; |
| 4403 | if (SG_IMAGETYPE_CUBE == img->type) { |
| 4404 | gl_img_target = _sg_gl_cubeface_target(face_index); |
| 4405 | } |
| 4406 | const GLvoid* data_ptr = desc->content.subimage[face_index][mip_index].ptr; |
| 4407 | const int data_size = desc->content.subimage[face_index][mip_index].size; |
| 4408 | int mip_width = img->width >> mip_index; |
| 4409 | if (mip_width == 0) { |
| 4410 | mip_width = 1; |
| 4411 | } |
| 4412 | int mip_height = img->height >> mip_index; |
| 4413 | if (mip_height == 0) { |
| 4414 | mip_height = 1; |
| 4415 | } |
| 4416 | if ((SG_IMAGETYPE_2D == img->type) || (SG_IMAGETYPE_CUBE == img->type)) { |
| 4417 | if (is_compressed) { |
| 4418 | glCompressedTexImage2D(gl_img_target, mip_index, gl_internal_format, |
| 4419 | mip_width, mip_height, 0, data_size, data_ptr); |
| 4420 | } |
| 4421 | else { |
| 4422 | const GLenum gl_type = _sg_gl_teximage_type(img->pixel_format); |
| 4423 | glTexImage2D(gl_img_target, mip_index, gl_internal_format, |
| 4424 | mip_width, mip_height, 0, gl_format, gl_type, data_ptr); |
| 4425 | } |
| 4426 | } |
| 4427 | #if !defined(SOKOL_GLES2) |
| 4428 | else if (!_sg.gl.gles2 && ((SG_IMAGETYPE_3D == img->type) || (SG_IMAGETYPE_ARRAY == img->type))) { |
| 4429 | int mip_depth = img->depth; |
| 4430 | if (SG_IMAGETYPE_3D == img->type) { |
| 4431 | mip_depth >>= mip_index; |
| 4432 | } |
| 4433 | if (mip_depth == 0) { |
| 4434 | mip_depth = 1; |
| 4435 | } |
| 4436 | if (is_compressed) { |
| 4437 | glCompressedTexImage3D(gl_img_target, mip_index, gl_internal_format, |
| 4438 | mip_width, mip_height, mip_depth, 0, data_size, data_ptr); |
| 4439 | } |
| 4440 | else { |
| 4441 | const GLenum gl_type = _sg_gl_teximage_type(img->pixel_format); |
| 4442 | glTexImage3D(gl_img_target, mip_index, gl_internal_format, |
| 4443 | mip_width, mip_height, mip_depth, 0, gl_format, gl_type, data_ptr); |
| 4444 | } |
| 4445 | } |
| 4446 | #endif |
| 4447 | } |
| 4448 | } |
| 4449 | _sg_gl_restore_texture_binding(0); |
| 4450 | } |
| 4451 | } |
| 4452 | } |
| 4453 | _SG_GL_CHECK_ERROR(); |
| 4454 | return SG_RESOURCESTATE_VALID; |
| 4455 | } |
| 4456 | |
| 4457 | _SOKOL_PRIVATE void _sg_destroy_image(_sg_image_t* img) { |
| 4458 | SOKOL_ASSERT(img); |
| 4459 | _SG_GL_CHECK_ERROR(); |
| 4460 | if (!img->ext_textures) { |
| 4461 | for (int slot = 0; slot < img->num_slots; slot++) { |
| 4462 | if (img->gl_tex[slot]) { |
| 4463 | glDeleteTextures(1, &img->gl_tex[slot]); |
| 4464 | } |
| 4465 | } |
| 4466 | } |
| 4467 | if (img->gl_depth_render_buffer) { |
| 4468 | glDeleteRenderbuffers(1, &img->gl_depth_render_buffer); |
| 4469 | } |
| 4470 | if (img->gl_msaa_render_buffer) { |
| 4471 | glDeleteRenderbuffers(1, &img->gl_msaa_render_buffer); |
| 4472 | } |
| 4473 | _SG_GL_CHECK_ERROR(); |
| 4474 | } |
| 4475 | |
| 4476 | _SOKOL_PRIVATE GLuint _sg_gl_compile_shader(sg_shader_stage stage, const char* src) { |
| 4477 | SOKOL_ASSERT(src); |
| 4478 | _SG_GL_CHECK_ERROR(); |
| 4479 | GLuint gl_shd = glCreateShader(_sg_gl_shader_stage(stage)); |
| 4480 | glShaderSource(gl_shd, 1, &src, 0); |
| 4481 | glCompileShader(gl_shd); |
| 4482 | GLint compile_status = 0; |
| 4483 | glGetShaderiv(gl_shd, GL_COMPILE_STATUS, &compile_status); |
| 4484 | if (!compile_status) { |
| 4485 | /* compilation failed, log error and delete shader */ |
| 4486 | GLint log_len = 0; |
| 4487 | glGetShaderiv(gl_shd, GL_INFO_LOG_LENGTH, &log_len); |
| 4488 | if (log_len > 0) { |
| 4489 | GLchar* log_buf = (GLchar*) SOKOL_MALLOC(log_len); |
| 4490 | glGetShaderInfoLog(gl_shd, log_len, &log_len, log_buf); |
| 4491 | SOKOL_LOG(log_buf); |
| 4492 | SOKOL_FREE(log_buf); |
| 4493 | } |
| 4494 | glDeleteShader(gl_shd); |
| 4495 | gl_shd = 0; |
| 4496 | } |
| 4497 | _SG_GL_CHECK_ERROR(); |
| 4498 | return gl_shd; |
| 4499 | } |
| 4500 | |
| 4501 | _SOKOL_PRIVATE sg_resource_state _sg_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) { |
| 4502 | SOKOL_ASSERT(shd && desc); |
| 4503 | SOKOL_ASSERT(!shd->gl_prog); |
| 4504 | _SG_GL_CHECK_ERROR(); |
| 4505 | |
| 4506 | /* copy vertex attribute names over, these are required for GLES2, and optional for GLES3 and GL3.x */ |
| 4507 | for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) { |
| 4508 | _sg_strcpy(&shd->attrs[i].name, desc->attrs[i].name); |
| 4509 | } |
| 4510 | |
| 4511 | GLuint gl_vs = _sg_gl_compile_shader(SG_SHADERSTAGE_VS, desc->vs.source); |
| 4512 | GLuint gl_fs = _sg_gl_compile_shader(SG_SHADERSTAGE_FS, desc->fs.source); |
| 4513 | if (!(gl_vs && gl_fs)) { |
| 4514 | return SG_RESOURCESTATE_FAILED; |
| 4515 | } |
| 4516 | GLuint gl_prog = glCreateProgram(); |
| 4517 | glAttachShader(gl_prog, gl_vs); |
| 4518 | glAttachShader(gl_prog, gl_fs); |
| 4519 | glLinkProgram(gl_prog); |
| 4520 | glDeleteShader(gl_vs); |
| 4521 | glDeleteShader(gl_fs); |
| 4522 | _SG_GL_CHECK_ERROR(); |
| 4523 | |
| 4524 | GLint link_status; |
| 4525 | glGetProgramiv(gl_prog, GL_LINK_STATUS, &link_status); |
| 4526 | if (!link_status) { |
| 4527 | GLint log_len = 0; |
| 4528 | glGetProgramiv(gl_prog, GL_INFO_LOG_LENGTH, &log_len); |
| 4529 | if (log_len > 0) { |
| 4530 | GLchar* log_buf = (GLchar*) SOKOL_MALLOC(log_len); |
| 4531 | glGetProgramInfoLog(gl_prog, log_len, &log_len, log_buf); |
| 4532 | SOKOL_LOG(log_buf); |
| 4533 | SOKOL_FREE(log_buf); |
| 4534 | } |
| 4535 | glDeleteProgram(gl_prog); |
| 4536 | return SG_RESOURCESTATE_FAILED; |
| 4537 | } |
| 4538 | shd->gl_prog = gl_prog; |
| 4539 | |
| 4540 | /* resolve uniforms */ |
| 4541 | _SG_GL_CHECK_ERROR(); |
| 4542 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 4543 | const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS)? &desc->vs : &desc->fs; |
| 4544 | _sg_shader_stage_t* stage = &shd->stage[stage_index]; |
| 4545 | SOKOL_ASSERT(stage->num_uniform_blocks == 0); |
| 4546 | for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) { |
| 4547 | const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index]; |
| 4548 | if (0 == ub_desc->size) { |
| 4549 | break; |
| 4550 | } |
| 4551 | _sg_uniform_block_t* ub = &stage->uniform_blocks[ub_index]; |
| 4552 | ub->size = ub_desc->size; |
| 4553 | SOKOL_ASSERT(ub->num_uniforms == 0); |
| 4554 | int cur_uniform_offset = 0; |
| 4555 | for (int u_index = 0; u_index < SG_MAX_UB_MEMBERS; u_index++) { |
| 4556 | const sg_shader_uniform_desc* u_desc = &ub_desc->uniforms[u_index]; |
| 4557 | if (u_desc->type == SG_UNIFORMTYPE_INVALID) { |
| 4558 | break; |
| 4559 | } |
| 4560 | _sg_uniform_t* u = &ub->uniforms[u_index]; |
| 4561 | u->type = u_desc->type; |
| 4562 | u->count = (uint8_t) u_desc->array_count; |
| 4563 | u->offset = (uint16_t) cur_uniform_offset; |
| 4564 | cur_uniform_offset += _sg_uniform_size(u->type, u->count); |
| 4565 | if (u_desc->name) { |
| 4566 | u->gl_loc = glGetUniformLocation(gl_prog, u_desc->name); |
| 4567 | } |
| 4568 | else { |
| 4569 | u->gl_loc = u_index; |
| 4570 | } |
| 4571 | ub->num_uniforms++; |
| 4572 | } |
| 4573 | SOKOL_ASSERT(ub_desc->size == cur_uniform_offset); |
| 4574 | stage->num_uniform_blocks++; |
| 4575 | } |
| 4576 | } |
| 4577 | |
| 4578 | /* resolve image locations */ |
| 4579 | _SG_GL_CHECK_ERROR(); |
| 4580 | int gl_tex_slot = 0; |
| 4581 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 4582 | const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS)? &desc->vs : &desc->fs; |
| 4583 | _sg_shader_stage_t* stage = &shd->stage[stage_index]; |
| 4584 | SOKOL_ASSERT(stage->num_images == 0); |
| 4585 | for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) { |
| 4586 | const sg_shader_image_desc* img_desc = &stage_desc->images[img_index]; |
| 4587 | if (img_desc->type == _SG_IMAGETYPE_DEFAULT) { |
| 4588 | break; |
| 4589 | } |
| 4590 | _sg_shader_image_t* img = &stage->images[img_index]; |
| 4591 | img->type = img_desc->type; |
| 4592 | img->gl_loc = img_index; |
| 4593 | if (img_desc->name) { |
| 4594 | img->gl_loc = glGetUniformLocation(gl_prog, img_desc->name); |
| 4595 | } |
| 4596 | if (img->gl_loc != -1) { |
| 4597 | img->gl_tex_slot = gl_tex_slot++; |
| 4598 | } |
| 4599 | else { |
| 4600 | img->gl_tex_slot = -1; |
| 4601 | } |
| 4602 | stage->num_images++; |
| 4603 | } |
| 4604 | } |
| 4605 | _SG_GL_CHECK_ERROR(); |
| 4606 | return SG_RESOURCESTATE_VALID; |
| 4607 | } |
| 4608 | |
| 4609 | _SOKOL_PRIVATE void _sg_destroy_shader(_sg_shader_t* shd) { |
| 4610 | SOKOL_ASSERT(shd); |
| 4611 | _SG_GL_CHECK_ERROR(); |
| 4612 | if (shd->gl_prog) { |
| 4613 | glDeleteProgram(shd->gl_prog); |
| 4614 | } |
| 4615 | _SG_GL_CHECK_ERROR(); |
| 4616 | } |
| 4617 | |
| 4618 | _SOKOL_PRIVATE sg_resource_state _sg_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) { |
| 4619 | SOKOL_ASSERT(pip && shd && desc); |
| 4620 | SOKOL_ASSERT(!pip->shader && pip->shader_id.id == SG_INVALID_ID); |
| 4621 | SOKOL_ASSERT(desc->shader.id == shd->slot.id); |
| 4622 | SOKOL_ASSERT(shd->gl_prog); |
| 4623 | pip->shader = shd; |
| 4624 | pip->shader_id = desc->shader; |
| 4625 | pip->primitive_type = desc->primitive_type; |
| 4626 | pip->index_type = desc->index_type; |
| 4627 | pip->color_attachment_count = desc->blend.color_attachment_count; |
| 4628 | pip->color_format = desc->blend.color_format; |
| 4629 | pip->depth_format = desc->blend.depth_format; |
| 4630 | pip->sample_count = desc->rasterizer.sample_count; |
| 4631 | pip->depth_stencil = desc->depth_stencil; |
| 4632 | pip->blend = desc->blend; |
| 4633 | pip->rast = desc->rasterizer; |
| 4634 | |
| 4635 | /* resolve vertex attributes */ |
| 4636 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 4637 | pip->gl_attrs[attr_index].vb_index = -1; |
| 4638 | } |
| 4639 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 4640 | const sg_vertex_attr_desc* a_desc = &desc->layout.attrs[attr_index]; |
| 4641 | if (a_desc->format == SG_VERTEXFORMAT_INVALID) { |
| 4642 | break; |
| 4643 | } |
| 4644 | SOKOL_ASSERT((a_desc->buffer_index >= 0) && (a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS)); |
| 4645 | const sg_buffer_layout_desc* l_desc = &desc->layout.buffers[a_desc->buffer_index]; |
| 4646 | const sg_vertex_step step_func = l_desc->step_func; |
| 4647 | const int step_rate = l_desc->step_rate; |
| 4648 | GLint attr_loc = attr_index; |
| 4649 | if (!_sg_strempty(&shd->attrs[attr_index].name)) { |
| 4650 | attr_loc = glGetAttribLocation(pip->shader->gl_prog, _sg_strptr(&shd->attrs[attr_index].name)); |
| 4651 | } |
| 4652 | SOKOL_ASSERT(attr_loc < SG_MAX_VERTEX_ATTRIBUTES); |
| 4653 | if (attr_loc != -1) { |
| 4654 | _sg_gl_attr_t* gl_attr = &pip->gl_attrs[attr_loc]; |
| 4655 | SOKOL_ASSERT(gl_attr->vb_index == -1); |
| 4656 | gl_attr->vb_index = (int8_t) a_desc->buffer_index; |
| 4657 | if (step_func == SG_VERTEXSTEP_PER_VERTEX) { |
| 4658 | gl_attr->divisor = 0; |
| 4659 | } |
| 4660 | else { |
| 4661 | gl_attr->divisor = (int8_t) step_rate; |
| 4662 | } |
| 4663 | SOKOL_ASSERT(l_desc->stride > 0); |
| 4664 | gl_attr->stride = (uint8_t) l_desc->stride; |
| 4665 | gl_attr->offset = a_desc->offset; |
| 4666 | gl_attr->size = (uint8_t) _sg_gl_vertexformat_size(a_desc->format); |
| 4667 | gl_attr->type = _sg_gl_vertexformat_type(a_desc->format); |
| 4668 | gl_attr->normalized = _sg_gl_vertexformat_normalized(a_desc->format); |
| 4669 | pip->vertex_layout_valid[a_desc->buffer_index] = true; |
| 4670 | } |
| 4671 | else { |
| 4672 | SOKOL_LOG("Vertex attribute not found in shader: "); |
| 4673 | SOKOL_LOG(_sg_strptr(&shd->attrs[attr_index].name)); |
| 4674 | } |
| 4675 | } |
| 4676 | return SG_RESOURCESTATE_VALID; |
| 4677 | } |
| 4678 | |
| 4679 | _SOKOL_PRIVATE void _sg_destroy_pipeline(_sg_pipeline_t* pip) { |
| 4680 | SOKOL_ASSERT(pip); |
| 4681 | /* empty */ |
| 4682 | } |
| 4683 | |
| 4684 | /* |
| 4685 | _sg_create_pass |
| 4686 | |
| 4687 | att_imgs must point to a _sg_image* att_imgs[SG_MAX_COLOR_ATTACHMENTS+1] array, |
| 4688 | first entries are the color attachment images (or nullptr), last entry |
| 4689 | is the depth-stencil image (or nullptr). |
| 4690 | */ |
| 4691 | _SOKOL_PRIVATE sg_resource_state _sg_create_pass(_sg_pass_t* pass, _sg_image_t** att_images, const sg_pass_desc* desc) { |
| 4692 | SOKOL_ASSERT(pass && att_images && desc); |
| 4693 | SOKOL_ASSERT(att_images && att_images[0]); |
| 4694 | _SG_GL_CHECK_ERROR(); |
| 4695 | |
| 4696 | /* copy image pointers and desc attributes */ |
| 4697 | const sg_attachment_desc* att_desc; |
| 4698 | _sg_attachment_t* att; |
| 4699 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 4700 | SOKOL_ASSERT(0 == pass->color_atts[i].image); |
| 4701 | att_desc = &desc->color_attachments[i]; |
| 4702 | if (att_desc->image.id != SG_INVALID_ID) { |
| 4703 | pass->num_color_atts++; |
| 4704 | SOKOL_ASSERT(att_images[i] && (att_images[i]->slot.id == att_desc->image.id)); |
| 4705 | SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(att_images[i]->pixel_format)); |
| 4706 | att = &pass->color_atts[i]; |
| 4707 | SOKOL_ASSERT((att->image == 0) && (att->image_id.id == SG_INVALID_ID)); |
| 4708 | att->image = att_images[i]; |
| 4709 | att->image_id = att_desc->image; |
| 4710 | att->mip_level = att_desc->mip_level; |
| 4711 | att->slice = att_desc->slice; |
| 4712 | } |
| 4713 | } |
| 4714 | SOKOL_ASSERT(0 == pass->ds_att.image); |
| 4715 | att_desc = &desc->depth_stencil_attachment; |
| 4716 | const int ds_img_index = SG_MAX_COLOR_ATTACHMENTS; |
| 4717 | if (att_desc->image.id != SG_INVALID_ID) { |
| 4718 | SOKOL_ASSERT(att_images[ds_img_index] && (att_images[ds_img_index]->slot.id == att_desc->image.id)); |
| 4719 | SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(att_images[ds_img_index]->pixel_format)); |
| 4720 | att = &pass->ds_att; |
| 4721 | SOKOL_ASSERT((att->image == 0) && (att->image_id.id == SG_INVALID_ID)); |
| 4722 | att->image = att_images[ds_img_index]; |
| 4723 | att->image_id = att_desc->image; |
| 4724 | att->mip_level = att_desc->mip_level; |
| 4725 | att->slice = att_desc->slice; |
| 4726 | } |
| 4727 | |
| 4728 | /* store current framebuffer binding (restored at end of function) */ |
| 4729 | GLuint gl_orig_fb; |
| 4730 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&gl_orig_fb); |
| 4731 | |
| 4732 | /* create a framebuffer object */ |
| 4733 | glGenFramebuffers(1, &pass->gl_fb); |
| 4734 | glBindFramebuffer(GL_FRAMEBUFFER, pass->gl_fb); |
| 4735 | |
| 4736 | /* attach msaa render buffer or textures */ |
| 4737 | const bool is_msaa = (0 != att_images[0]->gl_msaa_render_buffer); |
| 4738 | if (is_msaa) { |
| 4739 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 4740 | const _sg_image_t* att_img = pass->color_atts[i].image; |
| 4741 | if (att_img) { |
| 4742 | const GLuint gl_render_buffer = att_img->gl_msaa_render_buffer; |
| 4743 | SOKOL_ASSERT(gl_render_buffer); |
| 4744 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+i, GL_RENDERBUFFER, gl_render_buffer); |
| 4745 | } |
| 4746 | } |
| 4747 | } |
| 4748 | else { |
| 4749 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 4750 | const _sg_image_t* att_img = pass->color_atts[i].image; |
| 4751 | const int mip_level = pass->color_atts[i].mip_level; |
| 4752 | const int slice = pass->color_atts[i].slice; |
| 4753 | if (att_img) { |
| 4754 | const GLuint gl_tex = att_img->gl_tex[0]; |
| 4755 | SOKOL_ASSERT(gl_tex); |
| 4756 | const GLenum gl_att = GL_COLOR_ATTACHMENT0 + i; |
| 4757 | switch (att_img->type) { |
| 4758 | case SG_IMAGETYPE_2D: |
| 4759 | glFramebufferTexture2D(GL_FRAMEBUFFER, gl_att, GL_TEXTURE_2D, gl_tex, mip_level); |
| 4760 | break; |
| 4761 | case SG_IMAGETYPE_CUBE: |
| 4762 | glFramebufferTexture2D(GL_FRAMEBUFFER, gl_att, _sg_gl_cubeface_target(slice), gl_tex, mip_level); |
| 4763 | break; |
| 4764 | default: |
| 4765 | /* 3D- or array-texture */ |
| 4766 | #if !defined(SOKOL_GLES2) |
| 4767 | if (!_sg.gl.gles2) { |
| 4768 | glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_att, gl_tex, mip_level, slice); |
| 4769 | } |
| 4770 | #endif |
| 4771 | break; |
| 4772 | } |
| 4773 | } |
| 4774 | } |
| 4775 | } |
| 4776 | |
| 4777 | /* attach depth-stencil buffer to framebuffer */ |
| 4778 | if (pass->ds_att.image) { |
| 4779 | const GLuint gl_render_buffer = pass->ds_att.image->gl_depth_render_buffer; |
| 4780 | SOKOL_ASSERT(gl_render_buffer); |
| 4781 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, gl_render_buffer); |
| 4782 | if (_sg_is_depth_stencil_format(pass->ds_att.image->pixel_format)) { |
| 4783 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, gl_render_buffer); |
| 4784 | } |
| 4785 | } |
| 4786 | |
| 4787 | /* check if framebuffer is complete */ |
| 4788 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { |
| 4789 | SOKOL_LOG("Framebuffer completeness check failed!\n"); |
| 4790 | return SG_RESOURCESTATE_FAILED; |
| 4791 | } |
| 4792 | |
| 4793 | /* create MSAA resolve framebuffers if necessary */ |
| 4794 | if (is_msaa) { |
| 4795 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 4796 | att = &pass->color_atts[i]; |
| 4797 | if (att->image) { |
| 4798 | SOKOL_ASSERT(0 == att->gl_msaa_resolve_buffer); |
| 4799 | glGenFramebuffers(1, &att->gl_msaa_resolve_buffer); |
| 4800 | glBindFramebuffer(GL_FRAMEBUFFER, att->gl_msaa_resolve_buffer); |
| 4801 | const GLuint gl_tex = att->image->gl_tex[0]; |
| 4802 | SOKOL_ASSERT(gl_tex); |
| 4803 | switch (att->image->type) { |
| 4804 | case SG_IMAGETYPE_2D: |
| 4805 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 4806 | GL_TEXTURE_2D, gl_tex, att->mip_level); |
| 4807 | break; |
| 4808 | case SG_IMAGETYPE_CUBE: |
| 4809 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 4810 | _sg_gl_cubeface_target(att->slice), gl_tex, att->mip_level); |
| 4811 | break; |
| 4812 | default: |
| 4813 | #if !defined(SOKOL_GLES2) |
| 4814 | if (!_sg.gl.gles2) { |
| 4815 | glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, gl_tex, att->mip_level, att->slice); |
| 4816 | } |
| 4817 | #endif |
| 4818 | break; |
| 4819 | } |
| 4820 | /* check if framebuffer is complete */ |
| 4821 | if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { |
| 4822 | SOKOL_LOG("Framebuffer completeness check failed (msaa resolve buffer)!\n"); |
| 4823 | return SG_RESOURCESTATE_FAILED; |
| 4824 | } |
| 4825 | } |
| 4826 | } |
| 4827 | } |
| 4828 | |
| 4829 | /* restore original framebuffer binding */ |
| 4830 | glBindFramebuffer(GL_FRAMEBUFFER, gl_orig_fb); |
| 4831 | _SG_GL_CHECK_ERROR(); |
| 4832 | return SG_RESOURCESTATE_VALID; |
| 4833 | } |
| 4834 | |
| 4835 | _SOKOL_PRIVATE void _sg_destroy_pass(_sg_pass_t* pass) { |
| 4836 | SOKOL_ASSERT(pass); |
| 4837 | _SG_GL_CHECK_ERROR(); |
| 4838 | if (0 != pass->gl_fb) { |
| 4839 | glDeleteFramebuffers(1, &pass->gl_fb); |
| 4840 | } |
| 4841 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 4842 | if (pass->color_atts[i].gl_msaa_resolve_buffer) { |
| 4843 | glDeleteFramebuffers(1, &pass->color_atts[i].gl_msaa_resolve_buffer); |
| 4844 | } |
| 4845 | } |
| 4846 | if (pass->ds_att.gl_msaa_resolve_buffer) { |
| 4847 | glDeleteFramebuffers(1, &pass->ds_att.gl_msaa_resolve_buffer); |
| 4848 | } |
| 4849 | _SG_GL_CHECK_ERROR(); |
| 4850 | } |
| 4851 | |
| 4852 | /*-- GL backend rendering functions ------------------------------------------*/ |
| 4853 | _SOKOL_PRIVATE void _sg_begin_pass(_sg_pass_t* pass, const sg_pass_action* action, int w, int h) { |
| 4854 | /* FIXME: what if a texture used as render target is still bound, should we |
| 4855 | unbind all currently bound textures in begin pass? */ |
| 4856 | SOKOL_ASSERT(action); |
| 4857 | SOKOL_ASSERT(!_sg.gl.in_pass); |
| 4858 | _SG_GL_CHECK_ERROR(); |
| 4859 | _sg.gl.in_pass = true; |
| 4860 | _sg.gl.cur_pass = pass; /* can be 0 */ |
| 4861 | if (pass) { |
| 4862 | _sg.gl.cur_pass_id.id = pass->slot.id; |
| 4863 | } |
| 4864 | else { |
| 4865 | _sg.gl.cur_pass_id.id = SG_INVALID_ID; |
| 4866 | } |
| 4867 | _sg.gl.cur_pass_width = w; |
| 4868 | _sg.gl.cur_pass_height = h; |
| 4869 | if (pass) { |
| 4870 | /* offscreen pass */ |
| 4871 | SOKOL_ASSERT(pass->gl_fb); |
| 4872 | glBindFramebuffer(GL_FRAMEBUFFER, pass->gl_fb); |
| 4873 | #if !defined(SOKOL_GLES2) |
| 4874 | if (!_sg.gl.gles2) { |
| 4875 | GLenum att[SG_MAX_COLOR_ATTACHMENTS] = { |
| 4876 | GL_COLOR_ATTACHMENT0, |
| 4877 | GL_COLOR_ATTACHMENT1, |
| 4878 | GL_COLOR_ATTACHMENT2, |
| 4879 | GL_COLOR_ATTACHMENT3 |
| 4880 | }; |
| 4881 | int num_attrs = 0; |
| 4882 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 4883 | if (pass->color_atts[num_attrs].image) { |
| 4884 | num_attrs++; |
| 4885 | } |
| 4886 | else { |
| 4887 | break; |
| 4888 | } |
| 4889 | } |
| 4890 | glDrawBuffers(num_attrs, att); |
| 4891 | } |
| 4892 | #endif |
| 4893 | } |
| 4894 | else { |
| 4895 | /* default pass */ |
| 4896 | SOKOL_ASSERT(_sg.gl.cur_context); |
| 4897 | glBindFramebuffer(GL_FRAMEBUFFER, _sg.gl.cur_context->default_framebuffer); |
| 4898 | } |
| 4899 | glViewport(0, 0, w, h); |
| 4900 | glScissor(0, 0, w, h); |
| 4901 | bool need_pip_cache_flush = false; |
| 4902 | if (_sg.gl.cache.blend.color_write_mask != SG_COLORMASK_RGBA) { |
| 4903 | need_pip_cache_flush = true; |
| 4904 | _sg.gl.cache.blend.color_write_mask = SG_COLORMASK_RGBA; |
| 4905 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 4906 | } |
| 4907 | if (!_sg.gl.cache.ds.depth_write_enabled) { |
| 4908 | need_pip_cache_flush = true; |
| 4909 | _sg.gl.cache.ds.depth_write_enabled = true; |
| 4910 | glDepthMask(GL_TRUE); |
| 4911 | } |
| 4912 | if (_sg.gl.cache.ds.depth_compare_func != SG_COMPAREFUNC_ALWAYS) { |
| 4913 | need_pip_cache_flush = true; |
| 4914 | _sg.gl.cache.ds.depth_compare_func = SG_COMPAREFUNC_ALWAYS; |
| 4915 | glDepthFunc(GL_ALWAYS); |
| 4916 | } |
| 4917 | if (_sg.gl.cache.ds.stencil_write_mask != 0xFF) { |
| 4918 | need_pip_cache_flush = true; |
| 4919 | _sg.gl.cache.ds.stencil_write_mask = 0xFF; |
| 4920 | glStencilMask(0xFF); |
| 4921 | } |
| 4922 | if (need_pip_cache_flush) { |
| 4923 | /* we messed with the state cache directly, need to clear cached |
| 4924 | pipeline to force re-evaluation in next sg_apply_pipeline() */ |
| 4925 | _sg.gl.cache.cur_pipeline = 0; |
| 4926 | _sg.gl.cache.cur_pipeline_id.id = SG_INVALID_ID; |
| 4927 | } |
| 4928 | bool use_mrt_clear = (0 != pass); |
| 4929 | #if defined(SOKOL_GLES2) |
| 4930 | use_mrt_clear = false; |
| 4931 | #else |
| 4932 | if (_sg.gl.gles2) { |
| 4933 | use_mrt_clear = false; |
| 4934 | } |
| 4935 | #endif |
| 4936 | if (!use_mrt_clear) { |
| 4937 | GLbitfield clear_mask = 0; |
| 4938 | if (action->colors[0].action == SG_ACTION_CLEAR) { |
| 4939 | clear_mask |= GL_COLOR_BUFFER_BIT; |
| 4940 | const float* c = action->colors[0].val; |
| 4941 | glClearColor(c[0], c[1], c[2], c[3]); |
| 4942 | } |
| 4943 | if (action->depth.action == SG_ACTION_CLEAR) { |
| 4944 | clear_mask |= GL_DEPTH_BUFFER_BIT; |
| 4945 | #ifdef SOKOL_GLCORE33 |
| 4946 | glClearDepth(action->depth.val); |
| 4947 | #else |
| 4948 | glClearDepthf(action->depth.val); |
| 4949 | #endif |
| 4950 | } |
| 4951 | if (action->stencil.action == SG_ACTION_CLEAR) { |
| 4952 | clear_mask |= GL_STENCIL_BUFFER_BIT; |
| 4953 | glClearStencil(action->stencil.val); |
| 4954 | } |
| 4955 | if (0 != clear_mask) { |
| 4956 | glClear(clear_mask); |
| 4957 | } |
| 4958 | } |
| 4959 | #if !defined SOKOL_GLES2 |
| 4960 | else { |
| 4961 | SOKOL_ASSERT(pass); |
| 4962 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 4963 | if (pass->color_atts[i].image) { |
| 4964 | if (action->colors[i].action == SG_ACTION_CLEAR) { |
| 4965 | glClearBufferfv(GL_COLOR, i, action->colors[i].val); |
| 4966 | } |
| 4967 | } |
| 4968 | else { |
| 4969 | break; |
| 4970 | } |
| 4971 | } |
| 4972 | if (pass->ds_att.image) { |
| 4973 | if ((action->depth.action == SG_ACTION_CLEAR) && (action->stencil.action == SG_ACTION_CLEAR)) { |
| 4974 | glClearBufferfi(GL_DEPTH_STENCIL, 0, action->depth.val, action->stencil.val); |
| 4975 | } |
| 4976 | else if (action->depth.action == SG_ACTION_CLEAR) { |
| 4977 | glClearBufferfv(GL_DEPTH, 0, &action->depth.val); |
| 4978 | } |
| 4979 | else if (action->stencil.action == SG_ACTION_CLEAR) { |
| 4980 | GLuint val = action->stencil.val; |
| 4981 | glClearBufferuiv(GL_STENCIL, 0, &val); |
| 4982 | } |
| 4983 | } |
| 4984 | } |
| 4985 | #endif |
| 4986 | _SG_GL_CHECK_ERROR(); |
| 4987 | } |
| 4988 | |
| 4989 | _SOKOL_PRIVATE void _sg_end_pass(void) { |
| 4990 | SOKOL_ASSERT(_sg.gl.in_pass); |
| 4991 | _SG_GL_CHECK_ERROR(); |
| 4992 | |
| 4993 | /* if this was an offscreen pass, and MSAA rendering was used, need |
| 4994 | to resolve into the pass images */ |
| 4995 | #if !defined(SOKOL_GLES2) |
| 4996 | if (!_sg.gl.gles2 && _sg.gl.cur_pass) { |
| 4997 | /* check if the pass object is still valid */ |
| 4998 | const _sg_pass_t* pass = _sg.gl.cur_pass; |
| 4999 | SOKOL_ASSERT(pass->slot.id == _sg.gl.cur_pass_id.id); |
| 5000 | bool is_msaa = (0 != _sg.gl.cur_pass->color_atts[0].gl_msaa_resolve_buffer); |
| 5001 | if (is_msaa) { |
| 5002 | SOKOL_ASSERT(pass->gl_fb); |
| 5003 | glBindFramebuffer(GL_READ_FRAMEBUFFER, pass->gl_fb); |
| 5004 | SOKOL_ASSERT(pass->color_atts[0].image); |
| 5005 | const int w = pass->color_atts[0].image->width; |
| 5006 | const int h = pass->color_atts[0].image->height; |
| 5007 | for (int att_index = 0; att_index < SG_MAX_COLOR_ATTACHMENTS; att_index++) { |
| 5008 | const _sg_attachment_t* att = &pass->color_atts[att_index]; |
| 5009 | if (att->image) { |
| 5010 | SOKOL_ASSERT(att->gl_msaa_resolve_buffer); |
| 5011 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, att->gl_msaa_resolve_buffer); |
| 5012 | glReadBuffer(GL_COLOR_ATTACHMENT0 + att_index); |
| 5013 | const GLenum gl_att = GL_COLOR_ATTACHMENT0; |
| 5014 | glDrawBuffers(1, &gl_att); |
| 5015 | glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST); |
| 5016 | } |
| 5017 | else { |
| 5018 | break; |
| 5019 | } |
| 5020 | } |
| 5021 | } |
| 5022 | } |
| 5023 | #endif |
| 5024 | _sg.gl.cur_pass = 0; |
| 5025 | _sg.gl.cur_pass_id.id = SG_INVALID_ID; |
| 5026 | _sg.gl.cur_pass_width = 0; |
| 5027 | _sg.gl.cur_pass_height = 0; |
| 5028 | |
| 5029 | SOKOL_ASSERT(_sg.gl.cur_context); |
| 5030 | glBindFramebuffer(GL_FRAMEBUFFER, _sg.gl.cur_context->default_framebuffer); |
| 5031 | _sg.gl.in_pass = false; |
| 5032 | _SG_GL_CHECK_ERROR(); |
| 5033 | } |
| 5034 | |
| 5035 | _SOKOL_PRIVATE void _sg_apply_viewport(int x, int y, int w, int h, bool origin_top_left) { |
| 5036 | SOKOL_ASSERT(_sg.gl.in_pass); |
| 5037 | y = origin_top_left ? (_sg.gl.cur_pass_height - (y+h)) : y; |
| 5038 | glViewport(x, y, w, h); |
| 5039 | } |
| 5040 | |
| 5041 | _SOKOL_PRIVATE void _sg_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) { |
| 5042 | SOKOL_ASSERT(_sg.gl.in_pass); |
| 5043 | y = origin_top_left ? (_sg.gl.cur_pass_height - (y+h)) : y; |
| 5044 | glScissor(x, y, w, h); |
| 5045 | } |
| 5046 | |
| 5047 | _SOKOL_PRIVATE void _sg_apply_pipeline(_sg_pipeline_t* pip) { |
| 5048 | SOKOL_ASSERT(pip); |
| 5049 | SOKOL_ASSERT(pip->shader); |
| 5050 | _SG_GL_CHECK_ERROR(); |
| 5051 | if ((_sg.gl.cache.cur_pipeline != pip) || (_sg.gl.cache.cur_pipeline_id.id != pip->slot.id)) { |
| 5052 | _sg.gl.cache.cur_pipeline = pip; |
| 5053 | _sg.gl.cache.cur_pipeline_id.id = pip->slot.id; |
| 5054 | _sg.gl.cache.cur_primitive_type = _sg_gl_primitive_type(pip->primitive_type); |
| 5055 | _sg.gl.cache.cur_index_type = _sg_gl_index_type(pip->index_type); |
| 5056 | |
| 5057 | /* update depth-stencil state */ |
| 5058 | const sg_depth_stencil_state* new_ds = &pip->depth_stencil; |
| 5059 | sg_depth_stencil_state* cache_ds = &_sg.gl.cache.ds; |
| 5060 | if (new_ds->depth_compare_func != cache_ds->depth_compare_func) { |
| 5061 | cache_ds->depth_compare_func = new_ds->depth_compare_func; |
| 5062 | glDepthFunc(_sg_gl_compare_func(new_ds->depth_compare_func)); |
| 5063 | } |
| 5064 | if (new_ds->depth_write_enabled != cache_ds->depth_write_enabled) { |
| 5065 | cache_ds->depth_write_enabled = new_ds->depth_write_enabled; |
| 5066 | glDepthMask(new_ds->depth_write_enabled); |
| 5067 | } |
| 5068 | if (new_ds->stencil_enabled != cache_ds->stencil_enabled) { |
| 5069 | cache_ds->stencil_enabled = new_ds->stencil_enabled; |
| 5070 | if (new_ds->stencil_enabled) glEnable(GL_STENCIL_TEST); |
| 5071 | else glDisable(GL_STENCIL_TEST); |
| 5072 | } |
| 5073 | if (new_ds->stencil_write_mask != cache_ds->stencil_write_mask) { |
| 5074 | cache_ds->stencil_write_mask = new_ds->stencil_write_mask; |
| 5075 | glStencilMask(new_ds->stencil_write_mask); |
| 5076 | } |
| 5077 | for (int i = 0; i < 2; i++) { |
| 5078 | const sg_stencil_state* new_ss = (i==0)? &new_ds->stencil_front : &new_ds->stencil_back; |
| 5079 | sg_stencil_state* cache_ss = (i==0)? &cache_ds->stencil_front : &cache_ds->stencil_back; |
| 5080 | GLenum gl_face = (i==0)? GL_FRONT : GL_BACK; |
| 5081 | if ((new_ss->compare_func != cache_ss->compare_func) || |
| 5082 | (new_ds->stencil_read_mask != cache_ds->stencil_read_mask) || |
| 5083 | (new_ds->stencil_ref != cache_ds->stencil_ref)) |
| 5084 | { |
| 5085 | cache_ss->compare_func = new_ss->compare_func; |
| 5086 | glStencilFuncSeparate(gl_face, |
| 5087 | _sg_gl_compare_func(new_ss->compare_func), |
| 5088 | new_ds->stencil_ref, |
| 5089 | new_ds->stencil_read_mask); |
| 5090 | } |
| 5091 | if ((new_ss->fail_op != cache_ss->fail_op) || |
| 5092 | (new_ss->depth_fail_op != cache_ss->depth_fail_op) || |
| 5093 | (new_ss->pass_op != cache_ss->pass_op)) |
| 5094 | { |
| 5095 | cache_ss->fail_op = new_ss->fail_op; |
| 5096 | cache_ss->depth_fail_op = new_ss->depth_fail_op; |
| 5097 | cache_ss->pass_op = new_ss->pass_op; |
| 5098 | glStencilOpSeparate(gl_face, |
| 5099 | _sg_gl_stencil_op(new_ss->fail_op), |
| 5100 | _sg_gl_stencil_op(new_ss->depth_fail_op), |
| 5101 | _sg_gl_stencil_op(new_ss->pass_op)); |
| 5102 | } |
| 5103 | } |
| 5104 | cache_ds->stencil_read_mask = new_ds->stencil_read_mask; |
| 5105 | cache_ds->stencil_ref = new_ds->stencil_ref; |
| 5106 | |
| 5107 | /* update blend state */ |
| 5108 | const sg_blend_state* new_b = &pip->blend; |
| 5109 | sg_blend_state* cache_b = &_sg.gl.cache.blend; |
| 5110 | if (new_b->enabled != cache_b->enabled) { |
| 5111 | cache_b->enabled = new_b->enabled; |
| 5112 | if (new_b->enabled) glEnable(GL_BLEND); |
| 5113 | else glDisable(GL_BLEND); |
| 5114 | } |
| 5115 | if ((new_b->src_factor_rgb != cache_b->src_factor_rgb) || |
| 5116 | (new_b->dst_factor_rgb != cache_b->dst_factor_rgb) || |
| 5117 | (new_b->src_factor_alpha != cache_b->src_factor_alpha) || |
| 5118 | (new_b->dst_factor_alpha != cache_b->dst_factor_alpha)) |
| 5119 | { |
| 5120 | cache_b->src_factor_rgb = new_b->src_factor_rgb; |
| 5121 | cache_b->dst_factor_rgb = new_b->dst_factor_rgb; |
| 5122 | cache_b->src_factor_alpha = new_b->src_factor_alpha; |
| 5123 | cache_b->dst_factor_alpha = new_b->dst_factor_alpha; |
| 5124 | glBlendFuncSeparate(_sg_gl_blend_factor(new_b->src_factor_rgb), |
| 5125 | _sg_gl_blend_factor(new_b->dst_factor_rgb), |
| 5126 | _sg_gl_blend_factor(new_b->src_factor_alpha), |
| 5127 | _sg_gl_blend_factor(new_b->dst_factor_alpha)); |
| 5128 | } |
| 5129 | if ((new_b->op_rgb != cache_b->op_rgb) || (new_b->op_alpha != cache_b->op_alpha)) { |
| 5130 | cache_b->op_rgb = new_b->op_rgb; |
| 5131 | cache_b->op_alpha = new_b->op_alpha; |
| 5132 | glBlendEquationSeparate(_sg_gl_blend_op(new_b->op_rgb), _sg_gl_blend_op(new_b->op_alpha)); |
| 5133 | } |
| 5134 | if (new_b->color_write_mask != cache_b->color_write_mask) { |
| 5135 | cache_b->color_write_mask = new_b->color_write_mask; |
| 5136 | glColorMask((new_b->color_write_mask & SG_COLORMASK_R) != 0, |
| 5137 | (new_b->color_write_mask & SG_COLORMASK_G) != 0, |
| 5138 | (new_b->color_write_mask & SG_COLORMASK_B) != 0, |
| 5139 | (new_b->color_write_mask & SG_COLORMASK_A) != 0); |
| 5140 | } |
| 5141 | if (!_sg_fequal(new_b->blend_color[0], cache_b->blend_color[0], 0.0001f) || |
| 5142 | !_sg_fequal(new_b->blend_color[1], cache_b->blend_color[1], 0.0001f) || |
| 5143 | !_sg_fequal(new_b->blend_color[2], cache_b->blend_color[2], 0.0001f) || |
| 5144 | !_sg_fequal(new_b->blend_color[3], cache_b->blend_color[3], 0.0001f)) |
| 5145 | { |
| 5146 | const float* bc = new_b->blend_color; |
| 5147 | for (int i=0; i<4; i++) { |
| 5148 | cache_b->blend_color[i] = bc[i]; |
| 5149 | } |
| 5150 | glBlendColor(bc[0], bc[1], bc[2], bc[3]); |
| 5151 | } |
| 5152 | |
| 5153 | /* update rasterizer state */ |
| 5154 | const sg_rasterizer_state* new_r = &pip->rast; |
| 5155 | sg_rasterizer_state* cache_r = &_sg.gl.cache.rast; |
| 5156 | if (new_r->cull_mode != cache_r->cull_mode) { |
| 5157 | cache_r->cull_mode = new_r->cull_mode; |
| 5158 | if (SG_CULLMODE_NONE == new_r->cull_mode) { |
| 5159 | glDisable(GL_CULL_FACE); |
| 5160 | } |
| 5161 | else { |
| 5162 | glEnable(GL_CULL_FACE); |
| 5163 | GLenum gl_mode = (SG_CULLMODE_FRONT == new_r->cull_mode) ? GL_FRONT : GL_BACK; |
| 5164 | glCullFace(gl_mode); |
| 5165 | } |
| 5166 | } |
| 5167 | if (new_r->face_winding != cache_r->face_winding) { |
| 5168 | cache_r->face_winding = new_r->face_winding; |
| 5169 | GLenum gl_winding = (SG_FACEWINDING_CW == new_r->face_winding) ? GL_CW : GL_CCW; |
| 5170 | glFrontFace(gl_winding); |
| 5171 | } |
| 5172 | if (new_r->alpha_to_coverage_enabled != cache_r->alpha_to_coverage_enabled) { |
| 5173 | cache_r->alpha_to_coverage_enabled = new_r->alpha_to_coverage_enabled; |
| 5174 | if (new_r->alpha_to_coverage_enabled) glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE); |
| 5175 | else glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE); |
| 5176 | } |
| 5177 | #ifdef SOKOL_GLCORE33 |
| 5178 | if (new_r->sample_count != cache_r->sample_count) { |
| 5179 | cache_r->sample_count = new_r->sample_count; |
| 5180 | if (new_r->sample_count > 1) glEnable(GL_MULTISAMPLE); |
| 5181 | else glDisable(GL_MULTISAMPLE); |
| 5182 | } |
| 5183 | #endif |
| 5184 | if (!_sg_fequal(new_r->depth_bias, cache_r->depth_bias, 0.000001f) || |
| 5185 | !_sg_fequal(new_r->depth_bias_slope_scale, cache_r->depth_bias_slope_scale, 0.000001f)) |
| 5186 | { |
| 5187 | /* according to ANGLE's D3D11 backend: |
| 5188 | D3D11 SlopeScaledDepthBias ==> GL polygonOffsetFactor |
| 5189 | D3D11 DepthBias ==> GL polygonOffsetUnits |
| 5190 | DepthBiasClamp has no meaning on GL |
| 5191 | */ |
| 5192 | cache_r->depth_bias = new_r->depth_bias; |
| 5193 | cache_r->depth_bias_slope_scale = new_r->depth_bias_slope_scale; |
| 5194 | glPolygonOffset(new_r->depth_bias_slope_scale, new_r->depth_bias); |
| 5195 | bool po_enabled = true; |
| 5196 | if (_sg_fequal(new_r->depth_bias, 0.0f, 0.000001f) && |
| 5197 | _sg_fequal(new_r->depth_bias_slope_scale, 0.0f, 0.000001f)) |
| 5198 | { |
| 5199 | po_enabled = false; |
| 5200 | } |
| 5201 | if (po_enabled != _sg.gl.cache.polygon_offset_enabled) { |
| 5202 | _sg.gl.cache.polygon_offset_enabled = po_enabled; |
| 5203 | if (po_enabled) glEnable(GL_POLYGON_OFFSET_FILL); |
| 5204 | else glDisable(GL_POLYGON_OFFSET_FILL); |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | /* bind shader program */ |
| 5209 | glUseProgram(pip->shader->gl_prog); |
| 5210 | } |
| 5211 | } |
| 5212 | |
| 5213 | _SOKOL_PRIVATE void _sg_apply_bindings( |
| 5214 | _sg_pipeline_t* pip, |
| 5215 | _sg_buffer_t** vbs, const int* vb_offsets, int num_vbs, |
| 5216 | _sg_buffer_t* ib, int ib_offset, |
| 5217 | _sg_image_t** vs_imgs, int num_vs_imgs, |
| 5218 | _sg_image_t** fs_imgs, int num_fs_imgs) |
| 5219 | { |
| 5220 | SOKOL_ASSERT(pip); |
| 5221 | _SOKOL_UNUSED(num_fs_imgs); |
| 5222 | _SOKOL_UNUSED(num_vs_imgs); |
| 5223 | _SOKOL_UNUSED(num_vbs); |
| 5224 | _SG_GL_CHECK_ERROR(); |
| 5225 | |
| 5226 | /* bind textures */ |
| 5227 | _SG_GL_CHECK_ERROR(); |
| 5228 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 5229 | const _sg_shader_stage_t* stage = &pip->shader->stage[stage_index]; |
| 5230 | _sg_image_t** imgs = (stage_index == SG_SHADERSTAGE_VS)? vs_imgs : fs_imgs; |
| 5231 | SOKOL_ASSERT(((stage_index == SG_SHADERSTAGE_VS)? num_vs_imgs : num_fs_imgs) == stage->num_images); |
| 5232 | for (int img_index = 0; img_index < stage->num_images; img_index++) { |
| 5233 | const _sg_shader_image_t* shd_img = &stage->images[img_index]; |
| 5234 | if (shd_img->gl_loc != -1) { |
| 5235 | _sg_image_t* img = imgs[img_index]; |
| 5236 | const GLuint gl_tex = img->gl_tex[img->active_slot]; |
| 5237 | SOKOL_ASSERT(img && img->gl_target); |
| 5238 | SOKOL_ASSERT((shd_img->gl_tex_slot != -1) && gl_tex); |
| 5239 | glUniform1i(shd_img->gl_loc, shd_img->gl_tex_slot); |
| 5240 | _sg_gl_bind_texture(shd_img->gl_tex_slot, img->gl_target, gl_tex); |
| 5241 | } |
| 5242 | } |
| 5243 | } |
| 5244 | _SG_GL_CHECK_ERROR(); |
| 5245 | |
| 5246 | /* index buffer (can be 0) */ |
| 5247 | const GLuint gl_ib = ib ? ib->gl_buf[ib->active_slot] : 0; |
| 5248 | _sg_gl_bind_buffer(GL_ELEMENT_ARRAY_BUFFER, gl_ib); |
| 5249 | _sg.gl.cache.cur_ib_offset = ib_offset; |
| 5250 | |
| 5251 | /* vertex attributes */ |
| 5252 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 5253 | _sg_gl_attr_t* attr = &pip->gl_attrs[attr_index]; |
| 5254 | _sg_gl_cache_attr_t* cache_attr = &_sg.gl.cache.attrs[attr_index]; |
| 5255 | bool cache_attr_dirty = false; |
| 5256 | int vb_offset = 0; |
| 5257 | GLuint gl_vb = 0; |
| 5258 | if (attr->vb_index >= 0) { |
| 5259 | /* attribute is enabled */ |
| 5260 | SOKOL_ASSERT(attr->vb_index < num_vbs); |
| 5261 | _sg_buffer_t* vb = vbs[attr->vb_index]; |
| 5262 | SOKOL_ASSERT(vb); |
| 5263 | gl_vb = vb->gl_buf[vb->active_slot]; |
| 5264 | vb_offset = vb_offsets[attr->vb_index] + attr->offset; |
| 5265 | if ((gl_vb != cache_attr->gl_vbuf) || |
| 5266 | (attr->size != cache_attr->gl_attr.size) || |
| 5267 | (attr->type != cache_attr->gl_attr.type) || |
| 5268 | (attr->normalized != cache_attr->gl_attr.normalized) || |
| 5269 | (attr->stride != cache_attr->gl_attr.stride) || |
| 5270 | (vb_offset != cache_attr->gl_attr.offset) || |
| 5271 | (cache_attr->gl_attr.divisor != attr->divisor)) |
| 5272 | { |
| 5273 | _sg_gl_bind_buffer(GL_ARRAY_BUFFER, gl_vb); |
| 5274 | glVertexAttribPointer(attr_index, attr->size, attr->type, |
| 5275 | attr->normalized, attr->stride, |
| 5276 | (const GLvoid*)(GLintptr)vb_offset); |
| 5277 | #ifdef SOKOL_INSTANCING_ENABLED |
| 5278 | if (_sg.gl.features[SG_FEATURE_INSTANCING]) { |
| 5279 | glVertexAttribDivisor(attr_index, attr->divisor); |
| 5280 | } |
| 5281 | #endif |
| 5282 | cache_attr_dirty = true; |
| 5283 | } |
| 5284 | if (cache_attr->gl_attr.vb_index == -1) { |
| 5285 | glEnableVertexAttribArray(attr_index); |
| 5286 | cache_attr_dirty = true; |
| 5287 | } |
| 5288 | } |
| 5289 | else { |
| 5290 | /* attribute is disabled */ |
| 5291 | if (cache_attr->gl_attr.vb_index != -1) { |
| 5292 | glDisableVertexAttribArray(attr_index); |
| 5293 | cache_attr_dirty = true; |
| 5294 | } |
| 5295 | } |
| 5296 | if (cache_attr_dirty) { |
| 5297 | cache_attr->gl_attr = *attr; |
| 5298 | cache_attr->gl_attr.offset = vb_offset; |
| 5299 | cache_attr->gl_vbuf = gl_vb; |
| 5300 | } |
| 5301 | } |
| 5302 | _SG_GL_CHECK_ERROR(); |
| 5303 | } |
| 5304 | |
| 5305 | _SOKOL_PRIVATE void _sg_apply_uniforms(sg_shader_stage stage_index, int ub_index, const void* data, int num_bytes) { |
| 5306 | _SOKOL_UNUSED(num_bytes); |
| 5307 | SOKOL_ASSERT(data && (num_bytes > 0)); |
| 5308 | SOKOL_ASSERT((stage_index >= 0) && ((int)stage_index < SG_NUM_SHADER_STAGES)); |
| 5309 | SOKOL_ASSERT(_sg.gl.cache.cur_pipeline); |
| 5310 | SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->slot.id == _sg.gl.cache.cur_pipeline_id.id); |
| 5311 | SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->shader->slot.id == _sg.gl.cache.cur_pipeline->shader_id.id); |
| 5312 | _sg_shader_stage_t* stage = &_sg.gl.cache.cur_pipeline->shader->stage[stage_index]; |
| 5313 | SOKOL_ASSERT(ub_index < stage->num_uniform_blocks); |
| 5314 | _sg_uniform_block_t* ub = &stage->uniform_blocks[ub_index]; |
| 5315 | SOKOL_ASSERT(ub->size == num_bytes); |
| 5316 | for (int u_index = 0; u_index < ub->num_uniforms; u_index++) { |
| 5317 | _sg_uniform_t* u = &ub->uniforms[u_index]; |
| 5318 | SOKOL_ASSERT(u->type != SG_UNIFORMTYPE_INVALID); |
| 5319 | if (u->gl_loc == -1) { |
| 5320 | continue; |
| 5321 | } |
| 5322 | GLfloat* ptr = (GLfloat*) (((uint8_t*)data) + u->offset); |
| 5323 | switch (u->type) { |
| 5324 | case SG_UNIFORMTYPE_INVALID: |
| 5325 | break; |
| 5326 | case SG_UNIFORMTYPE_FLOAT: |
| 5327 | glUniform1fv(u->gl_loc, u->count, ptr); |
| 5328 | break; |
| 5329 | case SG_UNIFORMTYPE_FLOAT2: |
| 5330 | glUniform2fv(u->gl_loc, u->count, ptr); |
| 5331 | break; |
| 5332 | case SG_UNIFORMTYPE_FLOAT3: |
| 5333 | glUniform3fv(u->gl_loc, u->count, ptr); |
| 5334 | break; |
| 5335 | case SG_UNIFORMTYPE_FLOAT4: |
| 5336 | glUniform4fv(u->gl_loc, u->count, ptr); |
| 5337 | break; |
| 5338 | case SG_UNIFORMTYPE_MAT4: |
| 5339 | glUniformMatrix4fv(u->gl_loc, u->count, GL_FALSE, ptr); |
| 5340 | break; |
| 5341 | default: |
| 5342 | SOKOL_UNREACHABLE; |
| 5343 | break; |
| 5344 | } |
| 5345 | } |
| 5346 | } |
| 5347 | |
| 5348 | _SOKOL_PRIVATE void _sg_draw(int base_element, int num_elements, int num_instances) { |
| 5349 | const GLenum i_type = _sg.gl.cache.cur_index_type; |
| 5350 | const GLenum p_type = _sg.gl.cache.cur_primitive_type; |
| 5351 | if (0 != i_type) { |
| 5352 | /* indexed rendering */ |
| 5353 | const int i_size = (i_type == GL_UNSIGNED_SHORT) ? 2 : 4; |
| 5354 | const int ib_offset = _sg.gl.cache.cur_ib_offset; |
| 5355 | const GLvoid* indices = (const GLvoid*)(GLintptr)(base_element*i_size+ib_offset); |
| 5356 | if (num_instances == 1) { |
| 5357 | glDrawElements(p_type, num_elements, i_type, indices); |
| 5358 | } |
| 5359 | else { |
| 5360 | if (_sg.gl.features[SG_FEATURE_INSTANCING]) { |
| 5361 | glDrawElementsInstanced(p_type, num_elements, i_type, indices, num_instances); |
| 5362 | } |
| 5363 | } |
| 5364 | } |
| 5365 | else { |
| 5366 | /* non-indexed rendering */ |
| 5367 | if (num_instances == 1) { |
| 5368 | glDrawArrays(p_type, base_element, num_elements); |
| 5369 | } |
| 5370 | else { |
| 5371 | if (_sg.gl.features[SG_FEATURE_INSTANCING]) { |
| 5372 | glDrawArraysInstanced(p_type, base_element, num_elements, num_instances); |
| 5373 | } |
| 5374 | } |
| 5375 | } |
| 5376 | } |
| 5377 | |
| 5378 | _SOKOL_PRIVATE void _sg_commit(void) { |
| 5379 | SOKOL_ASSERT(!_sg.gl.in_pass); |
| 5380 | /* "soft" clear bindings (only those that are actually bound) */ |
| 5381 | _sg_gl_clear_buffer_bindings(false); |
| 5382 | _sg_gl_clear_texture_bindings(false); |
| 5383 | } |
| 5384 | |
| 5385 | _SOKOL_PRIVATE void _sg_update_buffer(_sg_buffer_t* buf, const void* data_ptr, int data_size) { |
| 5386 | SOKOL_ASSERT(buf && data_ptr && (data_size > 0)); |
| 5387 | /* only one update per buffer per frame allowed */ |
| 5388 | if (++buf->active_slot >= buf->num_slots) { |
| 5389 | buf->active_slot = 0; |
| 5390 | } |
| 5391 | GLenum gl_tgt = _sg_gl_buffer_target(buf->type); |
| 5392 | SOKOL_ASSERT(buf->active_slot < SG_NUM_INFLIGHT_FRAMES); |
| 5393 | GLuint gl_buf = buf->gl_buf[buf->active_slot]; |
| 5394 | SOKOL_ASSERT(gl_buf); |
| 5395 | _SG_GL_CHECK_ERROR(); |
| 5396 | _sg_gl_store_buffer_binding(gl_tgt); |
| 5397 | _sg_gl_bind_buffer(gl_tgt, gl_buf); |
| 5398 | glBufferSubData(gl_tgt, 0, data_size, data_ptr); |
| 5399 | _sg_gl_restore_buffer_binding(gl_tgt); |
| 5400 | _SG_GL_CHECK_ERROR(); |
| 5401 | } |
| 5402 | |
| 5403 | _SOKOL_PRIVATE void _sg_append_buffer(_sg_buffer_t* buf, const void* data_ptr, int data_size, bool new_frame) { |
| 5404 | SOKOL_ASSERT(buf && data_ptr && (data_size > 0)); |
| 5405 | if (new_frame) { |
| 5406 | if (++buf->active_slot >= buf->num_slots) { |
| 5407 | buf->active_slot = 0; |
| 5408 | } |
| 5409 | } |
| 5410 | GLenum gl_tgt = _sg_gl_buffer_target(buf->type); |
| 5411 | SOKOL_ASSERT(buf->active_slot < SG_NUM_INFLIGHT_FRAMES); |
| 5412 | GLuint gl_buf = buf->gl_buf[buf->active_slot]; |
| 5413 | SOKOL_ASSERT(gl_buf); |
| 5414 | _SG_GL_CHECK_ERROR(); |
| 5415 | _sg_gl_store_buffer_binding(gl_tgt); |
| 5416 | _sg_gl_bind_buffer(gl_tgt, gl_buf); |
| 5417 | glBufferSubData(gl_tgt, buf->append_pos, data_size, data_ptr); |
| 5418 | _sg_gl_restore_buffer_binding(gl_tgt); |
| 5419 | _SG_GL_CHECK_ERROR(); |
| 5420 | } |
| 5421 | |
| 5422 | _SOKOL_PRIVATE void _sg_update_image(_sg_image_t* img, const sg_image_content* data) { |
| 5423 | SOKOL_ASSERT(img && data); |
| 5424 | /* only one update per image per frame allowed */ |
| 5425 | if (++img->active_slot >= img->num_slots) { |
| 5426 | img->active_slot = 0; |
| 5427 | } |
| 5428 | SOKOL_ASSERT(img->active_slot < SG_NUM_INFLIGHT_FRAMES); |
| 5429 | SOKOL_ASSERT(0 != img->gl_tex[img->active_slot]); |
| 5430 | _sg_gl_store_texture_binding(0); |
| 5431 | _sg_gl_bind_texture(0, img->gl_target, img->gl_tex[img->active_slot]); |
| 5432 | const GLenum gl_img_format = _sg_gl_teximage_format(img->pixel_format); |
| 5433 | const GLenum gl_img_type = _sg_gl_teximage_type(img->pixel_format); |
| 5434 | const int num_faces = img->type == SG_IMAGETYPE_CUBE ? 6 : 1; |
| 5435 | const int num_mips = img->num_mipmaps; |
| 5436 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 5437 | for (int mip_index = 0; mip_index < num_mips; mip_index++) { |
| 5438 | GLenum gl_img_target = img->gl_target; |
| 5439 | if (SG_IMAGETYPE_CUBE == img->type) { |
| 5440 | gl_img_target = _sg_gl_cubeface_target(face_index); |
| 5441 | } |
| 5442 | const GLvoid* data_ptr = data->subimage[face_index][mip_index].ptr; |
| 5443 | int mip_width = img->width >> mip_index; |
| 5444 | if (mip_width == 0) { |
| 5445 | mip_width = 1; |
| 5446 | } |
| 5447 | int mip_height = img->height >> mip_index; |
| 5448 | if (mip_height == 0) { |
| 5449 | mip_height = 1; |
| 5450 | } |
| 5451 | if ((SG_IMAGETYPE_2D == img->type) || (SG_IMAGETYPE_CUBE == img->type)) { |
| 5452 | glTexSubImage2D(gl_img_target, mip_index, |
| 5453 | 0, 0, |
| 5454 | mip_width, mip_height, |
| 5455 | gl_img_format, gl_img_type, |
| 5456 | data_ptr); |
| 5457 | } |
| 5458 | #if !defined(SOKOL_GLES2) |
| 5459 | else if (!_sg.gl.gles2 && ((SG_IMAGETYPE_3D == img->type) || (SG_IMAGETYPE_ARRAY == img->type))) { |
| 5460 | int mip_depth = img->depth >> mip_index; |
| 5461 | if (mip_depth == 0) { |
| 5462 | mip_depth = 1; |
| 5463 | } |
| 5464 | glTexSubImage3D(gl_img_target, mip_index, |
| 5465 | 0, 0, 0, |
| 5466 | mip_width, mip_height, mip_depth, |
| 5467 | gl_img_format, gl_img_type, |
| 5468 | data_ptr); |
| 5469 | |
| 5470 | } |
| 5471 | #endif |
| 5472 | } |
| 5473 | } |
| 5474 | _sg_gl_restore_texture_binding(0); |
| 5475 | } |
| 5476 | |
| 5477 | /*== D3D11 BACKEND IMPLEMENTATION ============================================*/ |
| 5478 | #elif defined(SOKOL_D3D11) |
| 5479 | |
| 5480 | /*-- enum translation functions ----------------------------------------------*/ |
| 5481 | _SOKOL_PRIVATE D3D11_USAGE _sg_d3d11_usage(sg_usage usg) { |
| 5482 | switch (usg) { |
| 5483 | case SG_USAGE_IMMUTABLE: |
| 5484 | return D3D11_USAGE_IMMUTABLE; |
| 5485 | case SG_USAGE_DYNAMIC: |
| 5486 | case SG_USAGE_STREAM: |
| 5487 | return D3D11_USAGE_DYNAMIC; |
| 5488 | default: |
| 5489 | SOKOL_UNREACHABLE; |
| 5490 | return (D3D11_USAGE) 0; |
| 5491 | } |
| 5492 | } |
| 5493 | |
| 5494 | _SOKOL_PRIVATE UINT _sg_d3d11_cpu_access_flags(sg_usage usg) { |
| 5495 | switch (usg) { |
| 5496 | case SG_USAGE_IMMUTABLE: |
| 5497 | return 0; |
| 5498 | case SG_USAGE_DYNAMIC: |
| 5499 | case SG_USAGE_STREAM: |
| 5500 | return D3D11_CPU_ACCESS_WRITE; |
| 5501 | default: |
| 5502 | SOKOL_UNREACHABLE; |
| 5503 | return 0; |
| 5504 | } |
| 5505 | } |
| 5506 | |
| 5507 | _SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_texture_format(sg_pixel_format fmt) { |
| 5508 | /* |
| 5509 | NOTE: the following pixel formats are only supported on D3D11.1 |
| 5510 | (we're running on D3D11.0): |
| 5511 | DXGI_FORMAT_B4G4R4A4_UNORM |
| 5512 | DXGI_FORMAT_B5G6R5_UNORM |
| 5513 | DXGI_FORMAT_B5G5R5A1_UNORM |
| 5514 | */ |
| 5515 | switch (fmt) { |
| 5516 | case SG_PIXELFORMAT_RGBA8: return DXGI_FORMAT_R8G8B8A8_UNORM; |
| 5517 | case SG_PIXELFORMAT_R10G10B10A2: return DXGI_FORMAT_R10G10B10A2_UNORM; |
| 5518 | case SG_PIXELFORMAT_RGBA32F: return DXGI_FORMAT_R32G32B32A32_FLOAT; |
| 5519 | case SG_PIXELFORMAT_RGBA16F: return DXGI_FORMAT_R16G16B16A16_FLOAT; |
| 5520 | case SG_PIXELFORMAT_R32F: return DXGI_FORMAT_R32_FLOAT; |
| 5521 | case SG_PIXELFORMAT_R16F: return DXGI_FORMAT_R16_FLOAT; |
| 5522 | case SG_PIXELFORMAT_L8: return DXGI_FORMAT_R8_UNORM; |
| 5523 | case SG_PIXELFORMAT_DXT1: return DXGI_FORMAT_BC1_UNORM; |
| 5524 | case SG_PIXELFORMAT_DXT3: return DXGI_FORMAT_BC2_UNORM; |
| 5525 | case SG_PIXELFORMAT_DXT5: return DXGI_FORMAT_BC3_UNORM; |
| 5526 | default: return DXGI_FORMAT_UNKNOWN; |
| 5527 | }; |
| 5528 | } |
| 5529 | |
| 5530 | _SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_rendertarget_color_format(sg_pixel_format fmt) { |
| 5531 | switch (fmt) { |
| 5532 | case SG_PIXELFORMAT_RGBA8: return DXGI_FORMAT_R8G8B8A8_UNORM; |
| 5533 | case SG_PIXELFORMAT_RGBA32F: return DXGI_FORMAT_R32G32B32A32_FLOAT; |
| 5534 | case SG_PIXELFORMAT_RGBA16F: return DXGI_FORMAT_R16G16B16A16_FLOAT; |
| 5535 | case SG_PIXELFORMAT_R32F: return DXGI_FORMAT_R32_FLOAT; |
| 5536 | case SG_PIXELFORMAT_R16F: return DXGI_FORMAT_R16_FLOAT; |
| 5537 | case SG_PIXELFORMAT_L8: return DXGI_FORMAT_R8_UNORM; |
| 5538 | default: return DXGI_FORMAT_UNKNOWN; |
| 5539 | } |
| 5540 | } |
| 5541 | |
| 5542 | _SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_rendertarget_depth_format(sg_pixel_format fmt) { |
| 5543 | switch (fmt) { |
| 5544 | case SG_PIXELFORMAT_DEPTH: return DXGI_FORMAT_D16_UNORM; |
| 5545 | case SG_PIXELFORMAT_DEPTHSTENCIL: return DXGI_FORMAT_D24_UNORM_S8_UINT; |
| 5546 | default: return DXGI_FORMAT_UNKNOWN; |
| 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | _SOKOL_PRIVATE D3D11_PRIMITIVE_TOPOLOGY _sg_d3d11_primitive_topology(sg_primitive_type prim_type) { |
| 5551 | switch (prim_type) { |
| 5552 | case SG_PRIMITIVETYPE_POINTS: return D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; |
| 5553 | case SG_PRIMITIVETYPE_LINES: return D3D11_PRIMITIVE_TOPOLOGY_LINELIST; |
| 5554 | case SG_PRIMITIVETYPE_LINE_STRIP: return D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP; |
| 5555 | case SG_PRIMITIVETYPE_TRIANGLES: return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST; |
| 5556 | case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; |
| 5557 | default: SOKOL_UNREACHABLE; return (D3D11_PRIMITIVE_TOPOLOGY) 0; |
| 5558 | } |
| 5559 | } |
| 5560 | |
| 5561 | _SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_index_format(sg_index_type index_type) { |
| 5562 | switch (index_type) { |
| 5563 | case SG_INDEXTYPE_NONE: return DXGI_FORMAT_UNKNOWN; |
| 5564 | case SG_INDEXTYPE_UINT16: return DXGI_FORMAT_R16_UINT; |
| 5565 | case SG_INDEXTYPE_UINT32: return DXGI_FORMAT_R32_UINT; |
| 5566 | default: SOKOL_UNREACHABLE; return (DXGI_FORMAT) 0; |
| 5567 | } |
| 5568 | } |
| 5569 | |
| 5570 | _SOKOL_PRIVATE D3D11_FILTER _sg_d3d11_filter(sg_filter min_f, sg_filter mag_f, uint32_t max_anisotropy) { |
| 5571 | if (max_anisotropy > 1) { |
| 5572 | return D3D11_FILTER_ANISOTROPIC; |
| 5573 | } |
| 5574 | else if (mag_f == SG_FILTER_NEAREST) { |
| 5575 | switch (min_f) { |
| 5576 | case SG_FILTER_NEAREST: |
| 5577 | case SG_FILTER_NEAREST_MIPMAP_NEAREST: |
| 5578 | return D3D11_FILTER_MIN_MAG_MIP_POINT; |
| 5579 | case SG_FILTER_LINEAR: |
| 5580 | case SG_FILTER_LINEAR_MIPMAP_NEAREST: |
| 5581 | return D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT; |
| 5582 | case SG_FILTER_NEAREST_MIPMAP_LINEAR: |
| 5583 | return D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR; |
| 5584 | case SG_FILTER_LINEAR_MIPMAP_LINEAR: |
| 5585 | return D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR; |
| 5586 | default: |
| 5587 | SOKOL_UNREACHABLE; break; |
| 5588 | } |
| 5589 | } |
| 5590 | else if (mag_f == SG_FILTER_LINEAR) { |
| 5591 | switch (min_f) { |
| 5592 | case SG_FILTER_NEAREST: |
| 5593 | case SG_FILTER_NEAREST_MIPMAP_NEAREST: |
| 5594 | return D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT; |
| 5595 | case SG_FILTER_LINEAR: |
| 5596 | case SG_FILTER_LINEAR_MIPMAP_NEAREST: |
| 5597 | return D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT; |
| 5598 | case SG_FILTER_NEAREST_MIPMAP_LINEAR: |
| 5599 | return D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR; |
| 5600 | case SG_FILTER_LINEAR_MIPMAP_LINEAR: |
| 5601 | return D3D11_FILTER_MIN_MAG_MIP_LINEAR; |
| 5602 | default: |
| 5603 | SOKOL_UNREACHABLE; break; |
| 5604 | } |
| 5605 | } |
| 5606 | /* invalid value for mag filter */ |
| 5607 | SOKOL_UNREACHABLE; |
| 5608 | return D3D11_FILTER_MIN_MAG_MIP_POINT; |
| 5609 | } |
| 5610 | |
| 5611 | _SOKOL_PRIVATE D3D11_TEXTURE_ADDRESS_MODE _sg_d3d11_address_mode(sg_wrap m) { |
| 5612 | switch (m) { |
| 5613 | case SG_WRAP_REPEAT: return D3D11_TEXTURE_ADDRESS_WRAP; |
| 5614 | case SG_WRAP_CLAMP_TO_EDGE: return D3D11_TEXTURE_ADDRESS_CLAMP; |
| 5615 | case SG_WRAP_MIRRORED_REPEAT: return D3D11_TEXTURE_ADDRESS_MIRROR; |
| 5616 | default: SOKOL_UNREACHABLE; return (D3D11_TEXTURE_ADDRESS_MODE) 0; |
| 5617 | } |
| 5618 | } |
| 5619 | |
| 5620 | _SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_vertex_format(sg_vertex_format fmt) { |
| 5621 | switch (fmt) { |
| 5622 | case SG_VERTEXFORMAT_FLOAT: return DXGI_FORMAT_R32_FLOAT; |
| 5623 | case SG_VERTEXFORMAT_FLOAT2: return DXGI_FORMAT_R32G32_FLOAT; |
| 5624 | case SG_VERTEXFORMAT_FLOAT3: return DXGI_FORMAT_R32G32B32_FLOAT; |
| 5625 | case SG_VERTEXFORMAT_FLOAT4: return DXGI_FORMAT_R32G32B32A32_FLOAT; |
| 5626 | case SG_VERTEXFORMAT_BYTE4: return DXGI_FORMAT_R8G8B8A8_SINT; |
| 5627 | case SG_VERTEXFORMAT_BYTE4N: return DXGI_FORMAT_R8G8B8A8_SNORM; |
| 5628 | case SG_VERTEXFORMAT_UBYTE4: return DXGI_FORMAT_R8G8B8A8_UINT; |
| 5629 | case SG_VERTEXFORMAT_UBYTE4N: return DXGI_FORMAT_R8G8B8A8_UNORM; |
| 5630 | case SG_VERTEXFORMAT_SHORT2: return DXGI_FORMAT_R16G16_SINT; |
| 5631 | case SG_VERTEXFORMAT_SHORT2N: return DXGI_FORMAT_R16G16_SNORM; |
| 5632 | case SG_VERTEXFORMAT_SHORT4: return DXGI_FORMAT_R16G16B16A16_SINT; |
| 5633 | case SG_VERTEXFORMAT_SHORT4N: return DXGI_FORMAT_R16G16B16A16_SNORM; |
| 5634 | case SG_VERTEXFORMAT_UINT10_N2: return DXGI_FORMAT_R10G10B10A2_UNORM; |
| 5635 | default: SOKOL_UNREACHABLE; return (DXGI_FORMAT) 0; |
| 5636 | } |
| 5637 | } |
| 5638 | |
| 5639 | _SOKOL_PRIVATE D3D11_INPUT_CLASSIFICATION _sg_d3d11_input_classification(sg_vertex_step step) { |
| 5640 | switch (step) { |
| 5641 | case SG_VERTEXSTEP_PER_VERTEX: return D3D11_INPUT_PER_VERTEX_DATA; |
| 5642 | case SG_VERTEXSTEP_PER_INSTANCE: return D3D11_INPUT_PER_INSTANCE_DATA; |
| 5643 | default: SOKOL_UNREACHABLE; return (D3D11_INPUT_CLASSIFICATION) 0; |
| 5644 | } |
| 5645 | } |
| 5646 | |
| 5647 | _SOKOL_PRIVATE D3D11_CULL_MODE _sg_d3d11_cull_mode(sg_cull_mode m) { |
| 5648 | switch (m) { |
| 5649 | case SG_CULLMODE_NONE: return D3D11_CULL_NONE; |
| 5650 | case SG_CULLMODE_FRONT: return D3D11_CULL_FRONT; |
| 5651 | case SG_CULLMODE_BACK: return D3D11_CULL_BACK; |
| 5652 | default: SOKOL_UNREACHABLE; return (D3D11_CULL_MODE) 0; |
| 5653 | } |
| 5654 | } |
| 5655 | |
| 5656 | _SOKOL_PRIVATE D3D11_COMPARISON_FUNC _sg_d3d11_compare_func(sg_compare_func f) { |
| 5657 | switch (f) { |
| 5658 | case SG_COMPAREFUNC_NEVER: return D3D11_COMPARISON_NEVER; |
| 5659 | case SG_COMPAREFUNC_LESS: return D3D11_COMPARISON_LESS; |
| 5660 | case SG_COMPAREFUNC_EQUAL: return D3D11_COMPARISON_EQUAL; |
| 5661 | case SG_COMPAREFUNC_LESS_EQUAL: return D3D11_COMPARISON_LESS_EQUAL; |
| 5662 | case SG_COMPAREFUNC_GREATER: return D3D11_COMPARISON_GREATER; |
| 5663 | case SG_COMPAREFUNC_NOT_EQUAL: return D3D11_COMPARISON_NOT_EQUAL; |
| 5664 | case SG_COMPAREFUNC_GREATER_EQUAL: return D3D11_COMPARISON_GREATER_EQUAL; |
| 5665 | case SG_COMPAREFUNC_ALWAYS: return D3D11_COMPARISON_ALWAYS; |
| 5666 | default: SOKOL_UNREACHABLE; return (D3D11_COMPARISON_FUNC) 0; |
| 5667 | } |
| 5668 | } |
| 5669 | |
| 5670 | _SOKOL_PRIVATE D3D11_STENCIL_OP _sg_d3d11_stencil_op(sg_stencil_op op) { |
| 5671 | switch (op) { |
| 5672 | case SG_STENCILOP_KEEP: return D3D11_STENCIL_OP_KEEP; |
| 5673 | case SG_STENCILOP_ZERO: return D3D11_STENCIL_OP_ZERO; |
| 5674 | case SG_STENCILOP_REPLACE: return D3D11_STENCIL_OP_REPLACE; |
| 5675 | case SG_STENCILOP_INCR_CLAMP: return D3D11_STENCIL_OP_INCR_SAT; |
| 5676 | case SG_STENCILOP_DECR_CLAMP: return D3D11_STENCIL_OP_DECR_SAT; |
| 5677 | case SG_STENCILOP_INVERT: return D3D11_STENCIL_OP_INVERT; |
| 5678 | case SG_STENCILOP_INCR_WRAP: return D3D11_STENCIL_OP_INCR; |
| 5679 | case SG_STENCILOP_DECR_WRAP: return D3D11_STENCIL_OP_DECR; |
| 5680 | default: SOKOL_UNREACHABLE; return (D3D11_STENCIL_OP) 0; |
| 5681 | } |
| 5682 | } |
| 5683 | |
| 5684 | _SOKOL_PRIVATE D3D11_BLEND _sg_d3d11_blend_factor(sg_blend_factor f) { |
| 5685 | switch (f) { |
| 5686 | case SG_BLENDFACTOR_ZERO: return D3D11_BLEND_ZERO; |
| 5687 | case SG_BLENDFACTOR_ONE: return D3D11_BLEND_ONE; |
| 5688 | case SG_BLENDFACTOR_SRC_COLOR: return D3D11_BLEND_SRC_COLOR; |
| 5689 | case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return D3D11_BLEND_INV_SRC_COLOR; |
| 5690 | case SG_BLENDFACTOR_SRC_ALPHA: return D3D11_BLEND_SRC_ALPHA; |
| 5691 | case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return D3D11_BLEND_INV_SRC_ALPHA; |
| 5692 | case SG_BLENDFACTOR_DST_COLOR: return D3D11_BLEND_DEST_COLOR; |
| 5693 | case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return D3D11_BLEND_INV_DEST_COLOR; |
| 5694 | case SG_BLENDFACTOR_DST_ALPHA: return D3D11_BLEND_DEST_ALPHA; |
| 5695 | case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return D3D11_BLEND_INV_DEST_ALPHA; |
| 5696 | case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return D3D11_BLEND_SRC_ALPHA_SAT; |
| 5697 | case SG_BLENDFACTOR_BLEND_COLOR: return D3D11_BLEND_BLEND_FACTOR; |
| 5698 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return D3D11_BLEND_INV_BLEND_FACTOR; |
| 5699 | case SG_BLENDFACTOR_BLEND_ALPHA: return D3D11_BLEND_BLEND_FACTOR; |
| 5700 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return D3D11_BLEND_INV_BLEND_FACTOR; |
| 5701 | default: SOKOL_UNREACHABLE; return (D3D11_BLEND) 0; |
| 5702 | } |
| 5703 | } |
| 5704 | |
| 5705 | _SOKOL_PRIVATE D3D11_BLEND_OP _sg_d3d11_blend_op(sg_blend_op op) { |
| 5706 | switch (op) { |
| 5707 | case SG_BLENDOP_ADD: return D3D11_BLEND_OP_ADD; |
| 5708 | case SG_BLENDOP_SUBTRACT: return D3D11_BLEND_OP_SUBTRACT; |
| 5709 | case SG_BLENDOP_REVERSE_SUBTRACT: return D3D11_BLEND_OP_REV_SUBTRACT; |
| 5710 | default: SOKOL_UNREACHABLE; return (D3D11_BLEND_OP) 0; |
| 5711 | } |
| 5712 | } |
| 5713 | |
| 5714 | _SOKOL_PRIVATE UINT8 _sg_d3d11_color_write_mask(sg_color_mask m) { |
| 5715 | UINT8 res = 0; |
| 5716 | if (m & SG_COLORMASK_R) { |
| 5717 | res |= D3D11_COLOR_WRITE_ENABLE_RED; |
| 5718 | } |
| 5719 | if (m & SG_COLORMASK_G) { |
| 5720 | res |= D3D11_COLOR_WRITE_ENABLE_GREEN; |
| 5721 | } |
| 5722 | if (m & SG_COLORMASK_B) { |
| 5723 | res |= D3D11_COLOR_WRITE_ENABLE_BLUE; |
| 5724 | } |
| 5725 | if (m & SG_COLORMASK_A) { |
| 5726 | res |= D3D11_COLOR_WRITE_ENABLE_ALPHA; |
| 5727 | } |
| 5728 | return res; |
| 5729 | } |
| 5730 | |
| 5731 | _SOKOL_PRIVATE void _sg_setup_backend(const sg_desc* desc) { |
| 5732 | /* assume _sg.d3d11 already is zero-initialized */ |
| 5733 | SOKOL_ASSERT(desc); |
| 5734 | SOKOL_ASSERT(desc->d3d11_device); |
| 5735 | SOKOL_ASSERT(desc->d3d11_device_context); |
| 5736 | SOKOL_ASSERT(desc->d3d11_render_target_view_cb); |
| 5737 | SOKOL_ASSERT(desc->d3d11_depth_stencil_view_cb); |
| 5738 | SOKOL_ASSERT(desc->d3d11_render_target_view_cb != desc->d3d11_depth_stencil_view_cb); |
| 5739 | _sg.d3d11.valid = true; |
| 5740 | _sg.d3d11.dev = (ID3D11Device*) desc->d3d11_device; |
| 5741 | _sg.d3d11.ctx = (ID3D11DeviceContext*) desc->d3d11_device_context; |
| 5742 | _sg.d3d11.rtv_cb = desc->d3d11_render_target_view_cb; |
| 5743 | _sg.d3d11.dsv_cb = desc->d3d11_depth_stencil_view_cb; |
| 5744 | } |
| 5745 | |
| 5746 | _SOKOL_PRIVATE void _sg_discard_backend(void) { |
| 5747 | SOKOL_ASSERT(_sg.d3d11.valid); |
| 5748 | _sg.d3d11.valid = false; |
| 5749 | } |
| 5750 | |
| 5751 | _SOKOL_PRIVATE bool _sg_query_feature(sg_feature f) { |
| 5752 | switch (f) { |
| 5753 | case SG_FEATURE_INSTANCING: |
| 5754 | case SG_FEATURE_TEXTURE_COMPRESSION_DXT: |
| 5755 | case SG_FEATURE_TEXTURE_FLOAT: |
| 5756 | case SG_FEATURE_TEXTURE_HALF_FLOAT: |
| 5757 | case SG_FEATURE_ORIGIN_TOP_LEFT: |
| 5758 | case SG_FEATURE_MSAA_RENDER_TARGETS: |
| 5759 | case SG_FEATURE_MULTIPLE_RENDER_TARGET: |
| 5760 | case SG_FEATURE_IMAGETYPE_3D: |
| 5761 | case SG_FEATURE_IMAGETYPE_ARRAY: |
| 5762 | case SG_FEATURE_PACKED_VERTEX_FORMAT_10_2: |
| 5763 | return true; |
| 5764 | default: |
| 5765 | return false; |
| 5766 | } |
| 5767 | } |
| 5768 | |
| 5769 | _SOKOL_PRIVATE void _sg_d3d11_clear_state(void) { |
| 5770 | /* clear all the device context state, so that resource refs don't keep stuck in the d3d device context */ |
| 5771 | ID3D11DeviceContext_OMSetRenderTargets(_sg.d3d11.ctx, SG_MAX_COLOR_ATTACHMENTS, _sg.d3d11.zero_rtvs, NULL); |
| 5772 | ID3D11DeviceContext_RSSetState(_sg.d3d11.ctx, NULL); |
| 5773 | ID3D11DeviceContext_OMSetDepthStencilState(_sg.d3d11.ctx, NULL, 0); |
| 5774 | ID3D11DeviceContext_OMSetBlendState(_sg.d3d11.ctx, NULL, NULL, 0xFFFFFFFF); |
| 5775 | ID3D11DeviceContext_IASetVertexBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_BUFFERS, _sg.d3d11.zero_vbs, _sg.d3d11.zero_vb_strides, _sg.d3d11.zero_vb_offsets); |
| 5776 | ID3D11DeviceContext_IASetIndexBuffer(_sg.d3d11.ctx, NULL, DXGI_FORMAT_UNKNOWN, 0); |
| 5777 | ID3D11DeviceContext_IASetInputLayout(_sg.d3d11.ctx, NULL); |
| 5778 | ID3D11DeviceContext_VSSetShader(_sg.d3d11.ctx, NULL, NULL, 0); |
| 5779 | ID3D11DeviceContext_PSSetShader(_sg.d3d11.ctx, NULL, NULL, 0); |
| 5780 | ID3D11DeviceContext_VSSetConstantBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_UBS, _sg.d3d11.zero_cbs); |
| 5781 | ID3D11DeviceContext_PSSetConstantBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_UBS, _sg.d3d11.zero_cbs); |
| 5782 | ID3D11DeviceContext_VSSetShaderResources(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, _sg.d3d11.zero_srvs); |
| 5783 | ID3D11DeviceContext_PSSetShaderResources(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, _sg.d3d11.zero_srvs); |
| 5784 | ID3D11DeviceContext_VSSetSamplers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, _sg.d3d11.zero_smps); |
| 5785 | ID3D11DeviceContext_PSSetSamplers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, _sg.d3d11.zero_smps); |
| 5786 | } |
| 5787 | |
| 5788 | _SOKOL_PRIVATE void _sg_reset_state_cache(void) { |
| 5789 | /* just clear the d3d11 device context state */ |
| 5790 | _sg_d3d11_clear_state(); |
| 5791 | } |
| 5792 | |
| 5793 | _SOKOL_PRIVATE void _sg_activate_context(_sg_context_t* ctx) { |
| 5794 | _SOKOL_UNUSED(ctx); |
| 5795 | _sg_reset_state_cache(); |
| 5796 | } |
| 5797 | |
| 5798 | _SOKOL_PRIVATE sg_resource_state _sg_create_context(_sg_context_t* ctx) { |
| 5799 | SOKOL_ASSERT(ctx); |
| 5800 | _SOKOL_UNUSED(ctx); |
| 5801 | return SG_RESOURCESTATE_VALID; |
| 5802 | } |
| 5803 | |
| 5804 | _SOKOL_PRIVATE void _sg_destroy_context(_sg_context_t* ctx) { |
| 5805 | SOKOL_ASSERT(ctx); |
| 5806 | _SOKOL_UNUSED(ctx); |
| 5807 | /* empty */ |
| 5808 | } |
| 5809 | |
| 5810 | _SOKOL_PRIVATE sg_resource_state _sg_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) { |
| 5811 | SOKOL_ASSERT(buf && desc); |
| 5812 | SOKOL_ASSERT(!buf->d3d11_buf); |
| 5813 | buf->size = desc->size; |
| 5814 | buf->append_pos = 0; |
| 5815 | buf->append_overflow = false; |
| 5816 | buf->type = desc->type; |
| 5817 | buf->usage = desc->usage; |
| 5818 | buf->update_frame_index = 0; |
| 5819 | buf->append_frame_index = 0; |
| 5820 | const bool injected = (0 != desc->d3d11_buffer); |
| 5821 | if (injected) { |
| 5822 | buf->d3d11_buf = (ID3D11Buffer*) desc->d3d11_buffer; |
| 5823 | ID3D11Buffer_AddRef(buf->d3d11_buf); |
| 5824 | } |
| 5825 | else { |
| 5826 | D3D11_BUFFER_DESC d3d11_desc; |
| 5827 | memset(&d3d11_desc, 0, sizeof(d3d11_desc)); |
| 5828 | d3d11_desc.ByteWidth = buf->size; |
| 5829 | d3d11_desc.Usage = _sg_d3d11_usage(buf->usage); |
| 5830 | d3d11_desc.BindFlags = buf->type == SG_BUFFERTYPE_VERTEXBUFFER ? D3D11_BIND_VERTEX_BUFFER : D3D11_BIND_INDEX_BUFFER; |
| 5831 | d3d11_desc.CPUAccessFlags = _sg_d3d11_cpu_access_flags(buf->usage); |
| 5832 | D3D11_SUBRESOURCE_DATA* init_data_ptr = 0; |
| 5833 | D3D11_SUBRESOURCE_DATA init_data; |
| 5834 | memset(&init_data, 0, sizeof(init_data)); |
| 5835 | if (buf->usage == SG_USAGE_IMMUTABLE) { |
| 5836 | SOKOL_ASSERT(desc->content); |
| 5837 | init_data.pSysMem = desc->content; |
| 5838 | init_data_ptr = &init_data; |
| 5839 | } |
| 5840 | HRESULT hr = ID3D11Device_CreateBuffer(_sg.d3d11.dev, &d3d11_desc, init_data_ptr, &buf->d3d11_buf); |
| 5841 | _SOKOL_UNUSED(hr); |
| 5842 | SOKOL_ASSERT(SUCCEEDED(hr) && buf->d3d11_buf); |
| 5843 | } |
| 5844 | return SG_RESOURCESTATE_VALID; |
| 5845 | } |
| 5846 | |
| 5847 | _SOKOL_PRIVATE void _sg_destroy_buffer(_sg_buffer_t* buf) { |
| 5848 | SOKOL_ASSERT(buf); |
| 5849 | if (buf->d3d11_buf) { |
| 5850 | ID3D11Buffer_Release(buf->d3d11_buf); |
| 5851 | } |
| 5852 | } |
| 5853 | |
| 5854 | _SOKOL_PRIVATE void _sg_d3d11_fill_subres_data(const _sg_image_t* img, const sg_image_content* content) { |
| 5855 | const int num_faces = (img->type == SG_IMAGETYPE_CUBE) ? 6:1; |
| 5856 | const int num_slices = (img->type == SG_IMAGETYPE_ARRAY) ? img->depth:1; |
| 5857 | int subres_index = 0; |
| 5858 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 5859 | for (int slice_index = 0; slice_index < num_slices; slice_index++) { |
| 5860 | for (int mip_index = 0; mip_index < img->num_mipmaps; mip_index++, subres_index++) { |
| 5861 | SOKOL_ASSERT(subres_index < (SG_MAX_MIPMAPS * SG_MAX_TEXTUREARRAY_LAYERS)); |
| 5862 | D3D11_SUBRESOURCE_DATA* subres_data = &_sg.d3d11.subres_data[subres_index]; |
| 5863 | const int mip_width = ((img->width>>mip_index)>0) ? img->width>>mip_index : 1; |
| 5864 | const int mip_height = ((img->height>>mip_index)>0) ? img->height>>mip_index : 1; |
| 5865 | const sg_subimage_content* subimg_content = &(content->subimage[face_index][mip_index]); |
| 5866 | const int slice_size = subimg_content->size / num_slices; |
| 5867 | const int slice_offset = slice_size * slice_index; |
| 5868 | const uint8_t* ptr = (const uint8_t*) subimg_content->ptr; |
| 5869 | subres_data->pSysMem = ptr + slice_offset; |
| 5870 | subres_data->SysMemPitch = _sg_row_pitch(img->pixel_format, mip_width); |
| 5871 | if (img->type == SG_IMAGETYPE_3D) { |
| 5872 | /* FIXME? const int mip_depth = ((img->depth>>mip_index)>0) ? img->depth>>mip_index : 1; */ |
| 5873 | subres_data->SysMemSlicePitch = _sg_surface_pitch(img->pixel_format, mip_width, mip_height); |
| 5874 | } |
| 5875 | else { |
| 5876 | subres_data->SysMemSlicePitch = 0; |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | } |
| 5881 | } |
| 5882 | |
| 5883 | _SOKOL_PRIVATE sg_resource_state _sg_create_image(_sg_image_t* img, const sg_image_desc* desc) { |
| 5884 | SOKOL_ASSERT(img && desc); |
| 5885 | SOKOL_ASSERT(!img->d3d11_tex2d && !img->d3d11_tex3d && !img->d3d11_texds && !img->d3d11_texmsaa); |
| 5886 | SOKOL_ASSERT(!img->d3d11_srv && !img->d3d11_smp); |
| 5887 | HRESULT hr; |
| 5888 | |
| 5889 | img->type = desc->type; |
| 5890 | img->render_target = desc->render_target; |
| 5891 | img->width = desc->width; |
| 5892 | img->height = desc->height; |
| 5893 | img->depth = desc->depth; |
| 5894 | img->num_mipmaps = desc->num_mipmaps; |
| 5895 | img->usage = desc->usage; |
| 5896 | img->pixel_format = desc->pixel_format; |
| 5897 | img->sample_count = desc->sample_count; |
| 5898 | img->min_filter = desc->min_filter; |
| 5899 | img->mag_filter = desc->mag_filter; |
| 5900 | img->wrap_u = desc->wrap_u; |
| 5901 | img->wrap_v = desc->wrap_v; |
| 5902 | img->wrap_w = desc->wrap_w; |
| 5903 | img->max_anisotropy = desc->max_anisotropy; |
| 5904 | img->upd_frame_index = 0; |
| 5905 | const bool injected = (0 != desc->d3d11_texture); |
| 5906 | const bool msaa = (img->sample_count > 1); |
| 5907 | |
| 5908 | /* special case depth-stencil buffer? */ |
| 5909 | if (_sg_is_valid_rendertarget_depth_format(img->pixel_format)) { |
| 5910 | /* create only a depth-texture */ |
| 5911 | SOKOL_ASSERT(!injected); |
| 5912 | img->d3d11_format = _sg_d3d11_rendertarget_depth_format(img->pixel_format); |
| 5913 | if (img->d3d11_format == DXGI_FORMAT_UNKNOWN) { |
| 5914 | /* trying to create a texture format that's not supported by D3D */ |
| 5915 | SOKOL_LOG("trying to create a D3D11 depth-texture with unsupported pixel format\n"); |
| 5916 | return SG_RESOURCESTATE_FAILED; |
| 5917 | } |
| 5918 | D3D11_TEXTURE2D_DESC d3d11_desc; |
| 5919 | memset(&d3d11_desc, 0, sizeof(d3d11_desc)); |
| 5920 | d3d11_desc.Width = img->width; |
| 5921 | d3d11_desc.Height = img->height; |
| 5922 | d3d11_desc.MipLevels = 1; |
| 5923 | d3d11_desc.ArraySize = 1; |
| 5924 | d3d11_desc.Format = img->d3d11_format; |
| 5925 | d3d11_desc.Usage = D3D11_USAGE_DEFAULT; |
| 5926 | d3d11_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL; |
| 5927 | d3d11_desc.SampleDesc.Count = img->sample_count; |
| 5928 | d3d11_desc.SampleDesc.Quality = msaa ? D3D11_STANDARD_MULTISAMPLE_PATTERN : 0; |
| 5929 | hr = ID3D11Device_CreateTexture2D(_sg.d3d11.dev, &d3d11_desc, NULL, &img->d3d11_texds); |
| 5930 | SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11_texds); |
| 5931 | } |
| 5932 | else { |
| 5933 | /* create (or inject) color texture */ |
| 5934 | |
| 5935 | /* prepare initial content pointers */ |
| 5936 | D3D11_SUBRESOURCE_DATA* init_data = 0; |
| 5937 | if (!injected && (img->usage == SG_USAGE_IMMUTABLE) && !img->render_target) { |
| 5938 | _sg_d3d11_fill_subres_data(img, &desc->content); |
| 5939 | init_data = _sg.d3d11.subres_data; |
| 5940 | } |
| 5941 | if (img->type != SG_IMAGETYPE_3D) { |
| 5942 | /* 2D-, cube- or array-texture */ |
| 5943 | /* if this is an MSAA render target, the following texture will be the 'resolve-texture' */ |
| 5944 | D3D11_TEXTURE2D_DESC d3d11_tex_desc; |
| 5945 | memset(&d3d11_tex_desc, 0, sizeof(d3d11_tex_desc)); |
| 5946 | d3d11_tex_desc.Width = img->width; |
| 5947 | d3d11_tex_desc.Height = img->height; |
| 5948 | d3d11_tex_desc.MipLevels = img->num_mipmaps; |
| 5949 | switch (img->type) { |
| 5950 | case SG_IMAGETYPE_ARRAY: d3d11_tex_desc.ArraySize = img->depth; break; |
| 5951 | case SG_IMAGETYPE_CUBE: d3d11_tex_desc.ArraySize = 6; break; |
| 5952 | default: d3d11_tex_desc.ArraySize = 1; break; |
| 5953 | } |
| 5954 | d3d11_tex_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; |
| 5955 | if (img->render_target) { |
| 5956 | img->d3d11_format = _sg_d3d11_rendertarget_color_format(img->pixel_format); |
| 5957 | d3d11_tex_desc.Format = img->d3d11_format; |
| 5958 | d3d11_tex_desc.Usage = D3D11_USAGE_DEFAULT; |
| 5959 | if (!msaa) { |
| 5960 | d3d11_tex_desc.BindFlags |= D3D11_BIND_RENDER_TARGET; |
| 5961 | } |
| 5962 | d3d11_tex_desc.CPUAccessFlags = 0; |
| 5963 | } |
| 5964 | else { |
| 5965 | img->d3d11_format = _sg_d3d11_texture_format(img->pixel_format); |
| 5966 | d3d11_tex_desc.Format = img->d3d11_format; |
| 5967 | d3d11_tex_desc.Usage = _sg_d3d11_usage(img->usage); |
| 5968 | d3d11_tex_desc.CPUAccessFlags = _sg_d3d11_cpu_access_flags(img->usage); |
| 5969 | } |
| 5970 | if (img->d3d11_format == DXGI_FORMAT_UNKNOWN) { |
| 5971 | /* trying to create a texture format that's not supported by D3D */ |
| 5972 | SOKOL_LOG("trying to create a D3D11 texture with unsupported pixel format\n"); |
| 5973 | return SG_RESOURCESTATE_FAILED; |
| 5974 | } |
| 5975 | d3d11_tex_desc.SampleDesc.Count = 1; |
| 5976 | d3d11_tex_desc.SampleDesc.Quality = 0; |
| 5977 | d3d11_tex_desc.MiscFlags = (img->type == SG_IMAGETYPE_CUBE) ? D3D11_RESOURCE_MISC_TEXTURECUBE : 0; |
| 5978 | if (injected) { |
| 5979 | img->d3d11_tex2d = (ID3D11Texture2D*) desc->d3d11_texture; |
| 5980 | ID3D11Texture2D_AddRef(img->d3d11_tex2d); |
| 5981 | } |
| 5982 | else { |
| 5983 | hr = ID3D11Device_CreateTexture2D(_sg.d3d11.dev, &d3d11_tex_desc, init_data, &img->d3d11_tex2d); |
| 5984 | SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11_tex2d); |
| 5985 | } |
| 5986 | |
| 5987 | /* shader-resource-view */ |
| 5988 | D3D11_SHADER_RESOURCE_VIEW_DESC d3d11_srv_desc; |
| 5989 | memset(&d3d11_srv_desc, 0, sizeof(d3d11_srv_desc)); |
| 5990 | d3d11_srv_desc.Format = d3d11_tex_desc.Format; |
| 5991 | switch (img->type) { |
| 5992 | case SG_IMAGETYPE_2D: |
| 5993 | d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; |
| 5994 | d3d11_srv_desc.Texture2D.MipLevels = img->num_mipmaps; |
| 5995 | break; |
| 5996 | case SG_IMAGETYPE_CUBE: |
| 5997 | d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; |
| 5998 | d3d11_srv_desc.TextureCube.MipLevels = img->num_mipmaps; |
| 5999 | break; |
| 6000 | case SG_IMAGETYPE_ARRAY: |
| 6001 | d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY; |
| 6002 | d3d11_srv_desc.Texture2DArray.MipLevels = img->num_mipmaps; |
| 6003 | d3d11_srv_desc.Texture2DArray.ArraySize = img->depth; |
| 6004 | break; |
| 6005 | default: |
| 6006 | SOKOL_UNREACHABLE; break; |
| 6007 | } |
| 6008 | hr = ID3D11Device_CreateShaderResourceView(_sg.d3d11.dev, (ID3D11Resource*)img->d3d11_tex2d, &d3d11_srv_desc, &img->d3d11_srv); |
| 6009 | SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11_srv); |
| 6010 | } |
| 6011 | else { |
| 6012 | /* 3D texture */ |
| 6013 | D3D11_TEXTURE3D_DESC d3d11_tex_desc; |
| 6014 | memset(&d3d11_tex_desc, 0, sizeof(d3d11_tex_desc)); |
| 6015 | d3d11_tex_desc.Width = img->width; |
| 6016 | d3d11_tex_desc.Height = img->height; |
| 6017 | d3d11_tex_desc.Depth = img->depth; |
| 6018 | d3d11_tex_desc.MipLevels = img->num_mipmaps; |
| 6019 | d3d11_tex_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; |
| 6020 | if (img->render_target) { |
| 6021 | img->d3d11_format = _sg_d3d11_rendertarget_color_format(img->pixel_format); |
| 6022 | d3d11_tex_desc.Format = img->d3d11_format; |
| 6023 | d3d11_tex_desc.Usage = D3D11_USAGE_DEFAULT; |
| 6024 | if (!msaa) { |
| 6025 | d3d11_tex_desc.BindFlags |= D3D11_BIND_RENDER_TARGET; |
| 6026 | } |
| 6027 | d3d11_tex_desc.CPUAccessFlags = 0; |
| 6028 | } |
| 6029 | else { |
| 6030 | img->d3d11_format = _sg_d3d11_texture_format(img->pixel_format); |
| 6031 | d3d11_tex_desc.Format = img->d3d11_format; |
| 6032 | d3d11_tex_desc.Usage = _sg_d3d11_usage(img->usage); |
| 6033 | d3d11_tex_desc.CPUAccessFlags = _sg_d3d11_cpu_access_flags(img->usage); |
| 6034 | } |
| 6035 | if (img->d3d11_format == DXGI_FORMAT_UNKNOWN) { |
| 6036 | /* trying to create a texture format that's not supported by D3D */ |
| 6037 | SOKOL_LOG("trying to create a D3D11 texture with unsupported pixel format\n"); |
| 6038 | return SG_RESOURCESTATE_FAILED; |
| 6039 | } |
| 6040 | if (injected) { |
| 6041 | img->d3d11_tex3d = (ID3D11Texture3D*) desc->d3d11_texture; |
| 6042 | ID3D11Texture3D_AddRef(img->d3d11_tex3d); |
| 6043 | } |
| 6044 | else { |
| 6045 | hr = ID3D11Device_CreateTexture3D(_sg.d3d11.dev, &d3d11_tex_desc, init_data, &img->d3d11_tex3d); |
| 6046 | SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11_tex3d); |
| 6047 | } |
| 6048 | |
| 6049 | /* shader resource view for 3d texture */ |
| 6050 | D3D11_SHADER_RESOURCE_VIEW_DESC d3d11_srv_desc; |
| 6051 | memset(&d3d11_srv_desc, 0, sizeof(d3d11_srv_desc)); |
| 6052 | d3d11_srv_desc.Format = d3d11_tex_desc.Format; |
| 6053 | d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D; |
| 6054 | d3d11_srv_desc.Texture3D.MipLevels = img->num_mipmaps; |
| 6055 | hr = ID3D11Device_CreateShaderResourceView(_sg.d3d11.dev, (ID3D11Resource*)img->d3d11_tex3d, &d3d11_srv_desc, &img->d3d11_srv); |
| 6056 | SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11_srv); |
| 6057 | } |
| 6058 | |
| 6059 | /* also need to create a separate MSAA render target texture? */ |
| 6060 | if (msaa) { |
| 6061 | D3D11_TEXTURE2D_DESC d3d11_tex_desc; |
| 6062 | memset(&d3d11_tex_desc, 0, sizeof(d3d11_tex_desc)); |
| 6063 | d3d11_tex_desc.Width = img->width; |
| 6064 | d3d11_tex_desc.Height = img->height; |
| 6065 | d3d11_tex_desc.MipLevels = 1; |
| 6066 | d3d11_tex_desc.ArraySize = 1; |
| 6067 | d3d11_tex_desc.Format = img->d3d11_format; |
| 6068 | d3d11_tex_desc.Usage = D3D11_USAGE_DEFAULT; |
| 6069 | d3d11_tex_desc.BindFlags = D3D11_BIND_RENDER_TARGET; |
| 6070 | d3d11_tex_desc.CPUAccessFlags = 0; |
| 6071 | d3d11_tex_desc.SampleDesc.Count = img->sample_count; |
| 6072 | d3d11_tex_desc.SampleDesc.Quality = (UINT)D3D11_STANDARD_MULTISAMPLE_PATTERN; |
| 6073 | hr = ID3D11Device_CreateTexture2D(_sg.d3d11.dev, &d3d11_tex_desc, NULL, &img->d3d11_texmsaa); |
| 6074 | SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11_texmsaa); |
| 6075 | } |
| 6076 | |
| 6077 | /* sampler state object, note D3D11 implements an internal shared-pool for sampler objects */ |
| 6078 | D3D11_SAMPLER_DESC d3d11_smp_desc; |
| 6079 | memset(&d3d11_smp_desc, 0, sizeof(d3d11_smp_desc)); |
| 6080 | d3d11_smp_desc.Filter = _sg_d3d11_filter(img->min_filter, img->mag_filter, img->max_anisotropy); |
| 6081 | d3d11_smp_desc.AddressU = _sg_d3d11_address_mode(img->wrap_u); |
| 6082 | d3d11_smp_desc.AddressV = _sg_d3d11_address_mode(img->wrap_v); |
| 6083 | d3d11_smp_desc.AddressW = _sg_d3d11_address_mode(img->wrap_w); |
| 6084 | d3d11_smp_desc.MaxAnisotropy = img->max_anisotropy; |
| 6085 | d3d11_smp_desc.ComparisonFunc = D3D11_COMPARISON_NEVER; |
| 6086 | d3d11_smp_desc.MinLOD = desc->min_lod; |
| 6087 | d3d11_smp_desc.MaxLOD = desc->max_lod; |
| 6088 | hr = ID3D11Device_CreateSamplerState(_sg.d3d11.dev, &d3d11_smp_desc, &img->d3d11_smp); |
| 6089 | SOKOL_ASSERT(SUCCEEDED(hr) && img->d3d11_smp); |
| 6090 | } |
| 6091 | return SG_RESOURCESTATE_VALID; |
| 6092 | } |
| 6093 | |
| 6094 | _SOKOL_PRIVATE void _sg_destroy_image(_sg_image_t* img) { |
| 6095 | SOKOL_ASSERT(img); |
| 6096 | if (img->d3d11_tex2d) { |
| 6097 | ID3D11Texture2D_Release(img->d3d11_tex2d); |
| 6098 | } |
| 6099 | if (img->d3d11_tex3d) { |
| 6100 | ID3D11Texture3D_Release(img->d3d11_tex3d); |
| 6101 | } |
| 6102 | if (img->d3d11_texds) { |
| 6103 | ID3D11Texture2D_Release(img->d3d11_texds); |
| 6104 | } |
| 6105 | if (img->d3d11_texmsaa) { |
| 6106 | ID3D11Texture2D_Release(img->d3d11_texmsaa); |
| 6107 | } |
| 6108 | if (img->d3d11_srv) { |
| 6109 | ID3D11ShaderResourceView_Release(img->d3d11_srv); |
| 6110 | } |
| 6111 | if (img->d3d11_smp) { |
| 6112 | ID3D11SamplerState_Release(img->d3d11_smp); |
| 6113 | } |
| 6114 | } |
| 6115 | |
| 6116 | _SOKOL_PRIVATE bool _sg_d3d11_load_d3dcompiler_dll(void) { |
| 6117 | /* on UWP, don't do anything (not tested) */ |
| 6118 | #if (defined(WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) |
| 6119 | return true; |
| 6120 | #else |
| 6121 | /* load DLL on demand */ |
| 6122 | if ((0 == _sg.d3d11.d3dcompiler_dll) && !_sg.d3d11.d3dcompiler_dll_load_failed) { |
| 6123 | _sg.d3d11.d3dcompiler_dll = LoadLibraryA("d3dcompiler_47.dll"); |
| 6124 | if (0 == _sg.d3d11.d3dcompiler_dll) { |
| 6125 | /* don't attempt to load missing DLL in the future */ |
| 6126 | SOKOL_LOG("failed to load d3dcompiler_47.dll!\n"); |
| 6127 | _sg.d3d11.d3dcompiler_dll_load_failed = true; |
| 6128 | return false; |
| 6129 | } |
| 6130 | /* look up function pointers */ |
| 6131 | _sg.d3d11.D3DCompile_func = (pD3DCompile) GetProcAddress(_sg.d3d11.d3dcompiler_dll, "D3DCompile"); |
| 6132 | SOKOL_ASSERT(_sg.d3d11.D3DCompile_func); |
| 6133 | } |
| 6134 | return 0 != _sg.d3d11.d3dcompiler_dll; |
| 6135 | #endif |
| 6136 | } |
| 6137 | |
| 6138 | #if (defined(WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)) |
| 6139 | #define _sg_d3d11_D3DCompile D3DCompile |
| 6140 | #else |
| 6141 | #define _sg_d3d11_D3DCompile _sg.d3d11.D3DCompile_func |
| 6142 | #endif |
| 6143 | |
| 6144 | _SOKOL_PRIVATE ID3DBlob* _sg_d3d11_compile_shader(const sg_shader_stage_desc* stage_desc, const char* target) { |
| 6145 | if (!_sg_d3d11_load_d3dcompiler_dll()) { |
| 6146 | return NULL; |
| 6147 | } |
| 6148 | ID3DBlob* output = NULL; |
| 6149 | ID3DBlob* errors = NULL; |
| 6150 | _sg_d3d11_D3DCompile( |
| 6151 | stage_desc->source, /* pSrcData */ |
| 6152 | strlen(stage_desc->source), /* SrcDataSize */ |
| 6153 | NULL, /* pSourceName */ |
| 6154 | NULL, /* pDefines */ |
| 6155 | NULL, /* pInclude */ |
| 6156 | stage_desc->entry ? stage_desc->entry : "main", /* pEntryPoint */ |
| 6157 | target, /* pTarget (vs_5_0 or ps_5_0) */ |
| 6158 | D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR | D3DCOMPILE_OPTIMIZATION_LEVEL3, /* Flags1 */ |
| 6159 | 0, /* Flags2 */ |
| 6160 | &output, /* ppCode */ |
| 6161 | &errors); /* ppErrorMsgs */ |
| 6162 | if (errors) { |
| 6163 | SOKOL_LOG((LPCSTR)ID3D10Blob_GetBufferPointer(errors)); |
| 6164 | ID3D10Blob_Release(errors); errors = NULL; |
| 6165 | return NULL; |
| 6166 | } |
| 6167 | return output; |
| 6168 | } |
| 6169 | |
| 6170 | #define _sg_d3d11_roundup(val, round_to) (((val)+((round_to)-1))&~((round_to)-1)) |
| 6171 | |
| 6172 | _SOKOL_PRIVATE sg_resource_state _sg_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) { |
| 6173 | SOKOL_ASSERT(shd && desc); |
| 6174 | SOKOL_ASSERT(!shd->d3d11_vs && !shd->d3d11_fs && !shd->d3d11_vs_blob); |
| 6175 | HRESULT hr; |
| 6176 | sg_resource_state result = SG_RESOURCESTATE_FAILED; |
| 6177 | |
| 6178 | /* copy vertex attribute semantic names and indices */ |
| 6179 | for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) { |
| 6180 | _sg_strcpy(&shd->attrs[i].sem_name, desc->attrs[i].sem_name); |
| 6181 | shd->attrs[i].sem_index = desc->attrs[i].sem_index; |
| 6182 | } |
| 6183 | |
| 6184 | /* shader stage uniform blocks and image slots */ |
| 6185 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 6186 | const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS) ? &desc->vs : &desc->fs; |
| 6187 | _sg_shader_stage_t* stage = &shd->stage[stage_index]; |
| 6188 | SOKOL_ASSERT(stage->num_uniform_blocks == 0); |
| 6189 | for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) { |
| 6190 | const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index]; |
| 6191 | if (0 == ub_desc->size) { |
| 6192 | break; |
| 6193 | } |
| 6194 | _sg_uniform_block_t* ub = &stage->uniform_blocks[ub_index]; |
| 6195 | ub->size = ub_desc->size; |
| 6196 | |
| 6197 | /* create a D3D constant buffer */ |
| 6198 | SOKOL_ASSERT(!stage->d3d11_cbs[ub_index]); |
| 6199 | D3D11_BUFFER_DESC cb_desc; |
| 6200 | memset(&cb_desc, 0, sizeof(cb_desc)); |
| 6201 | cb_desc.ByteWidth = _sg_d3d11_roundup(ub->size, 16); |
| 6202 | cb_desc.Usage = D3D11_USAGE_DEFAULT; |
| 6203 | cb_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; |
| 6204 | hr = ID3D11Device_CreateBuffer(_sg.d3d11.dev, &cb_desc, NULL, &stage->d3d11_cbs[ub_index]); |
| 6205 | SOKOL_ASSERT(SUCCEEDED(hr) && stage->d3d11_cbs[ub_index]); |
| 6206 | |
| 6207 | stage->num_uniform_blocks++; |
| 6208 | } |
| 6209 | SOKOL_ASSERT(stage->num_images == 0); |
| 6210 | for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) { |
| 6211 | const sg_shader_image_desc* img_desc = &stage_desc->images[img_index]; |
| 6212 | if (img_desc->type == _SG_IMAGETYPE_DEFAULT) { |
| 6213 | break; |
| 6214 | } |
| 6215 | stage->images[img_index].type = img_desc->type; |
| 6216 | stage->num_images++; |
| 6217 | } |
| 6218 | } |
| 6219 | |
| 6220 | const void* vs_ptr = 0, *fs_ptr = 0; |
| 6221 | SIZE_T vs_length = 0, fs_length = 0; |
| 6222 | ID3DBlob* vs_blob = 0, *fs_blob = 0; |
| 6223 | if (desc->vs.byte_code && desc->fs.byte_code) { |
| 6224 | /* create from byte code */ |
| 6225 | vs_ptr = desc->vs.byte_code; |
| 6226 | fs_ptr = desc->fs.byte_code; |
| 6227 | vs_length = desc->vs.byte_code_size; |
| 6228 | fs_length = desc->fs.byte_code_size; |
| 6229 | } |
| 6230 | else { |
| 6231 | /* compile shader code */ |
| 6232 | vs_blob = _sg_d3d11_compile_shader(&desc->vs, "vs_5_0"); |
| 6233 | fs_blob = _sg_d3d11_compile_shader(&desc->fs, "ps_5_0"); |
| 6234 | if (vs_blob && fs_blob) { |
| 6235 | vs_ptr = ID3D10Blob_GetBufferPointer(vs_blob); |
| 6236 | vs_length = ID3D10Blob_GetBufferSize(vs_blob); |
| 6237 | fs_ptr = ID3D10Blob_GetBufferPointer(fs_blob); |
| 6238 | fs_length = ID3D10Blob_GetBufferSize(fs_blob); |
| 6239 | } |
| 6240 | } |
| 6241 | if (vs_ptr && fs_ptr && (vs_length > 0) && (fs_length > 0)) { |
| 6242 | /* create the D3D vertex- and pixel-shader objects */ |
| 6243 | hr = ID3D11Device_CreateVertexShader(_sg.d3d11.dev, vs_ptr, vs_length, NULL, &shd->d3d11_vs); |
| 6244 | SOKOL_ASSERT(SUCCEEDED(hr) && shd->d3d11_vs); |
| 6245 | hr = ID3D11Device_CreatePixelShader(_sg.d3d11.dev, fs_ptr, fs_length, NULL, &shd->d3d11_fs); |
| 6246 | SOKOL_ASSERT(SUCCEEDED(hr) && shd->d3d11_fs); |
| 6247 | |
| 6248 | /* need to store the vertex shader byte code, this is needed later in sg_create_pipeline */ |
| 6249 | shd->d3d11_vs_blob_length = (int)vs_length; |
| 6250 | shd->d3d11_vs_blob = SOKOL_MALLOC((int)vs_length); |
| 6251 | SOKOL_ASSERT(shd->d3d11_vs_blob); |
| 6252 | memcpy(shd->d3d11_vs_blob, vs_ptr, vs_length); |
| 6253 | |
| 6254 | result = SG_RESOURCESTATE_VALID; |
| 6255 | } |
| 6256 | if (vs_blob) { |
| 6257 | ID3D10Blob_Release(vs_blob); vs_blob = 0; |
| 6258 | } |
| 6259 | if (fs_blob) { |
| 6260 | ID3D10Blob_Release(fs_blob); fs_blob = 0; |
| 6261 | } |
| 6262 | return result; |
| 6263 | } |
| 6264 | |
| 6265 | _SOKOL_PRIVATE void _sg_destroy_shader(_sg_shader_t* shd) { |
| 6266 | SOKOL_ASSERT(shd); |
| 6267 | if (shd->d3d11_vs) { |
| 6268 | ID3D11VertexShader_Release(shd->d3d11_vs); |
| 6269 | } |
| 6270 | if (shd->d3d11_fs) { |
| 6271 | ID3D11PixelShader_Release(shd->d3d11_fs); |
| 6272 | } |
| 6273 | if (shd->d3d11_vs_blob) { |
| 6274 | SOKOL_FREE(shd->d3d11_vs_blob); |
| 6275 | } |
| 6276 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 6277 | _sg_shader_stage_t* stage = &shd->stage[stage_index]; |
| 6278 | for (int ub_index = 0; ub_index < stage->num_uniform_blocks; ub_index++) { |
| 6279 | if (stage->d3d11_cbs[ub_index]) { |
| 6280 | ID3D11Buffer_Release(stage->d3d11_cbs[ub_index]); |
| 6281 | } |
| 6282 | } |
| 6283 | } |
| 6284 | } |
| 6285 | |
| 6286 | _SOKOL_PRIVATE sg_resource_state _sg_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) { |
| 6287 | SOKOL_ASSERT(pip && shd && desc); |
| 6288 | SOKOL_ASSERT(desc->shader.id == shd->slot.id); |
| 6289 | SOKOL_ASSERT(shd->slot.state == SG_RESOURCESTATE_VALID); |
| 6290 | SOKOL_ASSERT(shd->d3d11_vs_blob && shd->d3d11_vs_blob_length > 0); |
| 6291 | SOKOL_ASSERT(!pip->d3d11_il && !pip->d3d11_rs && !pip->d3d11_dss && !pip->d3d11_bs); |
| 6292 | HRESULT hr; |
| 6293 | |
| 6294 | pip->shader = shd; |
| 6295 | pip->shader_id = desc->shader; |
| 6296 | pip->index_type = desc->index_type; |
| 6297 | pip->color_attachment_count = desc->blend.color_attachment_count; |
| 6298 | pip->color_format = desc->blend.color_format; |
| 6299 | pip->depth_format = desc->blend.depth_format; |
| 6300 | pip->sample_count = desc->rasterizer.sample_count; |
| 6301 | pip->d3d11_index_format = _sg_d3d11_index_format(pip->index_type); |
| 6302 | pip->d3d11_topology = _sg_d3d11_primitive_topology(desc->primitive_type); |
| 6303 | for (int i = 0; i < 4; i++) { |
| 6304 | pip->blend_color[i] = desc->blend.blend_color[i]; |
| 6305 | } |
| 6306 | pip->d3d11_stencil_ref = desc->depth_stencil.stencil_ref; |
| 6307 | |
| 6308 | /* create input layout object */ |
| 6309 | D3D11_INPUT_ELEMENT_DESC d3d11_comps[SG_MAX_VERTEX_ATTRIBUTES]; |
| 6310 | memset(d3d11_comps, 0, sizeof(d3d11_comps)); |
| 6311 | int attr_index = 0; |
| 6312 | for (; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 6313 | const sg_vertex_attr_desc* a_desc = &desc->layout.attrs[attr_index]; |
| 6314 | if (a_desc->format == SG_VERTEXFORMAT_INVALID) { |
| 6315 | break; |
| 6316 | } |
| 6317 | SOKOL_ASSERT((a_desc->buffer_index >= 0) && (a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS)); |
| 6318 | const sg_buffer_layout_desc* l_desc = &desc->layout.buffers[a_desc->buffer_index]; |
| 6319 | const sg_vertex_step step_func = l_desc->step_func; |
| 6320 | const int step_rate = l_desc->step_rate; |
| 6321 | D3D11_INPUT_ELEMENT_DESC* d3d11_comp = &d3d11_comps[attr_index]; |
| 6322 | d3d11_comp->SemanticName = _sg_strptr(&shd->attrs[attr_index].sem_name); |
| 6323 | d3d11_comp->SemanticIndex = shd->attrs[attr_index].sem_index; |
| 6324 | d3d11_comp->Format = _sg_d3d11_vertex_format(a_desc->format); |
| 6325 | d3d11_comp->InputSlot = a_desc->buffer_index; |
| 6326 | d3d11_comp->AlignedByteOffset = a_desc->offset; |
| 6327 | d3d11_comp->InputSlotClass = _sg_d3d11_input_classification(step_func); |
| 6328 | if (SG_VERTEXSTEP_PER_INSTANCE == step_func) { |
| 6329 | d3d11_comp->InstanceDataStepRate = step_rate; |
| 6330 | } |
| 6331 | pip->vertex_layout_valid[a_desc->buffer_index] = true; |
| 6332 | } |
| 6333 | for (int layout_index = 0; layout_index < SG_MAX_SHADERSTAGE_BUFFERS; layout_index++) { |
| 6334 | if (pip->vertex_layout_valid[layout_index]) { |
| 6335 | const sg_buffer_layout_desc* l_desc = &desc->layout.buffers[layout_index]; |
| 6336 | SOKOL_ASSERT(l_desc->stride > 0); |
| 6337 | pip->d3d11_vb_strides[layout_index] = l_desc->stride; |
| 6338 | } |
| 6339 | else { |
| 6340 | pip->d3d11_vb_strides[layout_index] = 0; |
| 6341 | } |
| 6342 | } |
| 6343 | hr = ID3D11Device_CreateInputLayout(_sg.d3d11.dev, |
| 6344 | d3d11_comps, /* pInputElementDesc */ |
| 6345 | attr_index, /* NumElements */ |
| 6346 | shd->d3d11_vs_blob, /* pShaderByteCodeWithInputSignature */ |
| 6347 | shd->d3d11_vs_blob_length, /* BytecodeLength */ |
| 6348 | &pip->d3d11_il); |
| 6349 | SOKOL_ASSERT(SUCCEEDED(hr) && pip->d3d11_il); |
| 6350 | |
| 6351 | /* create rasterizer state */ |
| 6352 | D3D11_RASTERIZER_DESC rs_desc; |
| 6353 | memset(&rs_desc, 0, sizeof(rs_desc)); |
| 6354 | rs_desc.FillMode = D3D11_FILL_SOLID; |
| 6355 | rs_desc.CullMode = _sg_d3d11_cull_mode(desc->rasterizer.cull_mode); |
| 6356 | rs_desc.FrontCounterClockwise = desc->rasterizer.face_winding == SG_FACEWINDING_CCW; |
| 6357 | rs_desc.DepthBias = (INT) desc->rasterizer.depth_bias; |
| 6358 | rs_desc.DepthBiasClamp = desc->rasterizer.depth_bias_clamp; |
| 6359 | rs_desc.SlopeScaledDepthBias = desc->rasterizer.depth_bias_slope_scale; |
| 6360 | rs_desc.DepthClipEnable = TRUE; |
| 6361 | rs_desc.ScissorEnable = TRUE; |
| 6362 | rs_desc.MultisampleEnable = desc->rasterizer.sample_count > 1; |
| 6363 | rs_desc.AntialiasedLineEnable = FALSE; |
| 6364 | hr = ID3D11Device_CreateRasterizerState(_sg.d3d11.dev, &rs_desc, &pip->d3d11_rs); |
| 6365 | SOKOL_ASSERT(SUCCEEDED(hr) && pip->d3d11_rs); |
| 6366 | |
| 6367 | /* create depth-stencil state */ |
| 6368 | D3D11_DEPTH_STENCIL_DESC dss_desc; |
| 6369 | memset(&dss_desc, 0, sizeof(dss_desc)); |
| 6370 | dss_desc.DepthEnable = TRUE; |
| 6371 | dss_desc.DepthWriteMask = desc->depth_stencil.depth_write_enabled ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO; |
| 6372 | dss_desc.DepthFunc = _sg_d3d11_compare_func(desc->depth_stencil.depth_compare_func); |
| 6373 | dss_desc.StencilEnable = desc->depth_stencil.stencil_enabled; |
| 6374 | dss_desc.StencilReadMask = desc->depth_stencil.stencil_read_mask; |
| 6375 | dss_desc.StencilWriteMask = desc->depth_stencil.stencil_write_mask; |
| 6376 | const sg_stencil_state* sf = &desc->depth_stencil.stencil_front; |
| 6377 | dss_desc.FrontFace.StencilFailOp = _sg_d3d11_stencil_op(sf->fail_op); |
| 6378 | dss_desc.FrontFace.StencilDepthFailOp = _sg_d3d11_stencil_op(sf->depth_fail_op); |
| 6379 | dss_desc.FrontFace.StencilPassOp = _sg_d3d11_stencil_op(sf->pass_op); |
| 6380 | dss_desc.FrontFace.StencilFunc = _sg_d3d11_compare_func(sf->compare_func); |
| 6381 | const sg_stencil_state* sb = &desc->depth_stencil.stencil_back; |
| 6382 | dss_desc.BackFace.StencilFailOp = _sg_d3d11_stencil_op(sb->fail_op); |
| 6383 | dss_desc.BackFace.StencilDepthFailOp = _sg_d3d11_stencil_op(sb->depth_fail_op); |
| 6384 | dss_desc.BackFace.StencilPassOp = _sg_d3d11_stencil_op(sb->pass_op); |
| 6385 | dss_desc.BackFace.StencilFunc = _sg_d3d11_compare_func(sb->compare_func); |
| 6386 | hr = ID3D11Device_CreateDepthStencilState(_sg.d3d11.dev, &dss_desc, &pip->d3d11_dss); |
| 6387 | SOKOL_ASSERT(SUCCEEDED(hr) && pip->d3d11_dss); |
| 6388 | |
| 6389 | /* create blend state */ |
| 6390 | D3D11_BLEND_DESC bs_desc; |
| 6391 | memset(&bs_desc, 0, sizeof(bs_desc)); |
| 6392 | bs_desc.AlphaToCoverageEnable = desc->rasterizer.alpha_to_coverage_enabled; |
| 6393 | bs_desc.IndependentBlendEnable = FALSE; |
| 6394 | bs_desc.RenderTarget[0].BlendEnable = desc->blend.enabled; |
| 6395 | bs_desc.RenderTarget[0].SrcBlend = _sg_d3d11_blend_factor(desc->blend.src_factor_rgb); |
| 6396 | bs_desc.RenderTarget[0].DestBlend = _sg_d3d11_blend_factor(desc->blend.dst_factor_rgb); |
| 6397 | bs_desc.RenderTarget[0].BlendOp = _sg_d3d11_blend_op(desc->blend.op_rgb); |
| 6398 | bs_desc.RenderTarget[0].SrcBlendAlpha = _sg_d3d11_blend_factor(desc->blend.src_factor_alpha); |
| 6399 | bs_desc.RenderTarget[0].DestBlendAlpha = _sg_d3d11_blend_factor(desc->blend.dst_factor_alpha); |
| 6400 | bs_desc.RenderTarget[0].BlendOpAlpha = _sg_d3d11_blend_op(desc->blend.op_alpha); |
| 6401 | bs_desc.RenderTarget[0].RenderTargetWriteMask = _sg_d3d11_color_write_mask((sg_color_mask)desc->blend.color_write_mask); |
| 6402 | hr = ID3D11Device_CreateBlendState(_sg.d3d11.dev, &bs_desc, &pip->d3d11_bs); |
| 6403 | SOKOL_ASSERT(SUCCEEDED(hr) && pip->d3d11_bs); |
| 6404 | |
| 6405 | return SG_RESOURCESTATE_VALID; |
| 6406 | } |
| 6407 | |
| 6408 | _SOKOL_PRIVATE void _sg_destroy_pipeline(_sg_pipeline_t* pip) { |
| 6409 | SOKOL_ASSERT(pip); |
| 6410 | if (pip->d3d11_il) { |
| 6411 | ID3D11InputLayout_Release(pip->d3d11_il); |
| 6412 | } |
| 6413 | if (pip->d3d11_rs) { |
| 6414 | ID3D11RasterizerState_Release(pip->d3d11_rs); |
| 6415 | } |
| 6416 | if (pip->d3d11_dss) { |
| 6417 | ID3D11DepthStencilState_Release(pip->d3d11_dss); |
| 6418 | } |
| 6419 | if (pip->d3d11_bs) { |
| 6420 | ID3D11BlendState_Release(pip->d3d11_bs); |
| 6421 | } |
| 6422 | } |
| 6423 | |
| 6424 | _SOKOL_PRIVATE sg_resource_state _sg_create_pass(_sg_pass_t* pass, _sg_image_t** att_images, const sg_pass_desc* desc) { |
| 6425 | SOKOL_ASSERT(pass && desc); |
| 6426 | SOKOL_ASSERT(att_images && att_images[0]); |
| 6427 | SOKOL_ASSERT(_sg.d3d11.dev); |
| 6428 | |
| 6429 | const sg_attachment_desc* att_desc; |
| 6430 | _sg_attachment_t* att; |
| 6431 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 6432 | SOKOL_ASSERT(0 == pass->color_atts[i].image); |
| 6433 | SOKOL_ASSERT(pass->d3d11_rtvs[i] == 0); |
| 6434 | att_desc = &desc->color_attachments[i]; |
| 6435 | if (att_desc->image.id != SG_INVALID_ID) { |
| 6436 | pass->num_color_atts++; |
| 6437 | SOKOL_ASSERT(att_images[i] && (att_images[i]->slot.id == att_desc->image.id)); |
| 6438 | SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(att_images[i]->pixel_format)); |
| 6439 | att = &pass->color_atts[i]; |
| 6440 | SOKOL_ASSERT((att->image == 0) && (att->image_id.id == SG_INVALID_ID)); |
| 6441 | att->image = att_images[i]; |
| 6442 | att->image_id = att_desc->image; |
| 6443 | att->mip_level = att_desc->mip_level; |
| 6444 | att->slice = att_desc->slice; |
| 6445 | |
| 6446 | /* create D3D11 render-target-view */ |
| 6447 | ID3D11Resource* d3d11_res = 0; |
| 6448 | const bool is_msaa = att->image->sample_count > 1; |
| 6449 | D3D11_RENDER_TARGET_VIEW_DESC d3d11_rtv_desc; |
| 6450 | memset(&d3d11_rtv_desc, 0, sizeof(d3d11_rtv_desc)); |
| 6451 | d3d11_rtv_desc.Format = att->image->d3d11_format; |
| 6452 | if ((att->image->type == SG_IMAGETYPE_2D) || is_msaa) { |
| 6453 | if (is_msaa) { |
| 6454 | d3d11_res = (ID3D11Resource*) att->image->d3d11_texmsaa; |
| 6455 | d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS; |
| 6456 | } |
| 6457 | else { |
| 6458 | d3d11_res = (ID3D11Resource*) att->image->d3d11_tex2d; |
| 6459 | d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; |
| 6460 | d3d11_rtv_desc.Texture2D.MipSlice = att->mip_level; |
| 6461 | } |
| 6462 | } |
| 6463 | else if ((att->image->type == SG_IMAGETYPE_CUBE) || (att->image->type == SG_IMAGETYPE_ARRAY)) { |
| 6464 | d3d11_res = (ID3D11Resource*) att->image->d3d11_tex2d; |
| 6465 | d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY; |
| 6466 | d3d11_rtv_desc.Texture2DArray.MipSlice = att->mip_level; |
| 6467 | d3d11_rtv_desc.Texture2DArray.FirstArraySlice = att->slice; |
| 6468 | d3d11_rtv_desc.Texture2DArray.ArraySize = 1; |
| 6469 | } |
| 6470 | else { |
| 6471 | SOKOL_ASSERT(att->image->type == SG_IMAGETYPE_3D); |
| 6472 | d3d11_res = (ID3D11Resource*) att->image->d3d11_tex3d; |
| 6473 | d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE3D; |
| 6474 | d3d11_rtv_desc.Texture3D.MipSlice = att->mip_level; |
| 6475 | d3d11_rtv_desc.Texture3D.FirstWSlice = att->slice; |
| 6476 | d3d11_rtv_desc.Texture3D.WSize = 1; |
| 6477 | } |
| 6478 | SOKOL_ASSERT(d3d11_res); |
| 6479 | HRESULT hr = ID3D11Device_CreateRenderTargetView(_sg.d3d11.dev, d3d11_res, &d3d11_rtv_desc, &pass->d3d11_rtvs[i]); |
| 6480 | _SOKOL_UNUSED(hr); |
| 6481 | SOKOL_ASSERT(SUCCEEDED(hr) && pass->d3d11_rtvs[i]); |
| 6482 | } |
| 6483 | } |
| 6484 | |
| 6485 | /* optional depth-stencil image */ |
| 6486 | SOKOL_ASSERT(0 == pass->ds_att.image); |
| 6487 | SOKOL_ASSERT(pass->d3d11_dsv == 0); |
| 6488 | att_desc = &desc->depth_stencil_attachment; |
| 6489 | const int ds_img_index = SG_MAX_COLOR_ATTACHMENTS; |
| 6490 | if (att_desc->image.id != SG_INVALID_ID) { |
| 6491 | SOKOL_ASSERT(att_images[ds_img_index] && (att_images[ds_img_index]->slot.id == att_desc->image.id)); |
| 6492 | SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(att_images[ds_img_index]->pixel_format)); |
| 6493 | att = &pass->ds_att; |
| 6494 | SOKOL_ASSERT((att->image == 0) && (att->image_id.id == SG_INVALID_ID)); |
| 6495 | att->image = att_images[ds_img_index]; |
| 6496 | att->image_id = att_desc->image; |
| 6497 | att->mip_level = att_desc->mip_level; |
| 6498 | att->slice = att_desc->slice; |
| 6499 | |
| 6500 | /* create D3D11 depth-stencil-view */ |
| 6501 | D3D11_DEPTH_STENCIL_VIEW_DESC d3d11_dsv_desc; |
| 6502 | memset(&d3d11_dsv_desc, 0, sizeof(d3d11_dsv_desc)); |
| 6503 | d3d11_dsv_desc.Format = att->image->d3d11_format; |
| 6504 | const bool is_msaa = att->image->sample_count > 1; |
| 6505 | if (is_msaa) { |
| 6506 | d3d11_dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS; |
| 6507 | } |
| 6508 | else { |
| 6509 | d3d11_dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; |
| 6510 | } |
| 6511 | ID3D11Resource* d3d11_res = (ID3D11Resource*) att->image->d3d11_texds; |
| 6512 | SOKOL_ASSERT(d3d11_res); |
| 6513 | HRESULT hr = ID3D11Device_CreateDepthStencilView(_sg.d3d11.dev, d3d11_res, &d3d11_dsv_desc, &pass->d3d11_dsv); |
| 6514 | _SOKOL_UNUSED(hr); |
| 6515 | SOKOL_ASSERT(SUCCEEDED(hr) && pass->d3d11_dsv); |
| 6516 | } |
| 6517 | return SG_RESOURCESTATE_VALID; |
| 6518 | } |
| 6519 | |
| 6520 | _SOKOL_PRIVATE void _sg_destroy_pass(_sg_pass_t* pass) { |
| 6521 | SOKOL_ASSERT(pass); |
| 6522 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 6523 | if (pass->d3d11_rtvs[i]) { |
| 6524 | ID3D11RenderTargetView_Release(pass->d3d11_rtvs[i]); |
| 6525 | } |
| 6526 | } |
| 6527 | if (pass->d3d11_dsv) { |
| 6528 | ID3D11DepthStencilView_Release(pass->d3d11_dsv); |
| 6529 | } |
| 6530 | } |
| 6531 | |
| 6532 | _SOKOL_PRIVATE void _sg_begin_pass(_sg_pass_t* pass, const sg_pass_action* action, int w, int h) { |
| 6533 | SOKOL_ASSERT(action); |
| 6534 | SOKOL_ASSERT(!_sg.d3d11.in_pass); |
| 6535 | _sg.d3d11.in_pass = true; |
| 6536 | _sg.d3d11.cur_width = w; |
| 6537 | _sg.d3d11.cur_height = h; |
| 6538 | if (pass) { |
| 6539 | _sg.d3d11.cur_pass = pass; |
| 6540 | _sg.d3d11.cur_pass_id.id = pass->slot.id; |
| 6541 | _sg.d3d11.num_rtvs = 0; |
| 6542 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 6543 | _sg.d3d11.cur_rtvs[i] = pass->d3d11_rtvs[i]; |
| 6544 | if (_sg.d3d11.cur_rtvs[i]) { |
| 6545 | _sg.d3d11.num_rtvs++; |
| 6546 | } |
| 6547 | } |
| 6548 | _sg.d3d11.cur_dsv = pass->d3d11_dsv; |
| 6549 | } |
| 6550 | else { |
| 6551 | /* render to default frame buffer */ |
| 6552 | _sg.d3d11.cur_pass = 0; |
| 6553 | _sg.d3d11.cur_pass_id.id = SG_INVALID_ID; |
| 6554 | _sg.d3d11.num_rtvs = 1; |
| 6555 | _sg.d3d11.cur_rtvs[0] = (ID3D11RenderTargetView*) _sg.d3d11.rtv_cb(); |
| 6556 | for (int i = 1; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 6557 | _sg.d3d11.cur_rtvs[i] = 0; |
| 6558 | } |
| 6559 | _sg.d3d11.cur_dsv = (ID3D11DepthStencilView*) _sg.d3d11.dsv_cb(); |
| 6560 | SOKOL_ASSERT(_sg.d3d11.cur_rtvs[0] && _sg.d3d11.cur_dsv); |
| 6561 | } |
| 6562 | /* apply the render-target- and depth-stencil-views */ |
| 6563 | ID3D11DeviceContext_OMSetRenderTargets(_sg.d3d11.ctx, SG_MAX_COLOR_ATTACHMENTS, _sg.d3d11.cur_rtvs, _sg.d3d11.cur_dsv); |
| 6564 | |
| 6565 | /* set viewport and scissor rect to cover whole screen */ |
| 6566 | D3D11_VIEWPORT vp; |
| 6567 | memset(&vp, 0, sizeof(vp)); |
| 6568 | vp.Width = (FLOAT) w; |
| 6569 | vp.Height = (FLOAT) h; |
| 6570 | vp.MaxDepth = 1.0f; |
| 6571 | ID3D11DeviceContext_RSSetViewports(_sg.d3d11.ctx, 1, &vp); |
| 6572 | D3D11_RECT rect; |
| 6573 | rect.left = 0; |
| 6574 | rect.top = 0; |
| 6575 | rect.right = w; |
| 6576 | rect.bottom = h; |
| 6577 | ID3D11DeviceContext_RSSetScissorRects(_sg.d3d11.ctx, 1, &rect); |
| 6578 | |
| 6579 | /* perform clear action */ |
| 6580 | for (int i = 0; i < _sg.d3d11.num_rtvs; i++) { |
| 6581 | if (action->colors[i].action == SG_ACTION_CLEAR) { |
| 6582 | ID3D11DeviceContext_ClearRenderTargetView(_sg.d3d11.ctx, _sg.d3d11.cur_rtvs[i], action->colors[i].val); |
| 6583 | } |
| 6584 | } |
| 6585 | UINT ds_flags = 0; |
| 6586 | if (action->depth.action == SG_ACTION_CLEAR) { |
| 6587 | ds_flags |= D3D11_CLEAR_DEPTH; |
| 6588 | } |
| 6589 | if (action->stencil.action == SG_ACTION_CLEAR) { |
| 6590 | ds_flags |= D3D11_CLEAR_STENCIL; |
| 6591 | } |
| 6592 | if ((0 != ds_flags) && _sg.d3d11.cur_dsv) { |
| 6593 | ID3D11DeviceContext_ClearDepthStencilView(_sg.d3d11.ctx, _sg.d3d11.cur_dsv, ds_flags, action->depth.val, action->stencil.val); |
| 6594 | } |
| 6595 | } |
| 6596 | |
| 6597 | /* D3D11CalcSubresource only exists for C++ */ |
| 6598 | _SOKOL_PRIVATE UINT _sg_d3d11_calcsubresource(UINT mip_slice, UINT array_slice, UINT mip_levels) { |
| 6599 | return mip_slice + array_slice * mip_levels; |
| 6600 | } |
| 6601 | |
| 6602 | _SOKOL_PRIVATE void _sg_end_pass(void) { |
| 6603 | SOKOL_ASSERT(_sg.d3d11.in_pass && _sg.d3d11.ctx); |
| 6604 | _sg.d3d11.in_pass = false; |
| 6605 | |
| 6606 | /* need to resolve MSAA render target into texture? */ |
| 6607 | if (_sg.d3d11.cur_pass) { |
| 6608 | SOKOL_ASSERT(_sg.d3d11.cur_pass->slot.id == _sg.d3d11.cur_pass_id.id); |
| 6609 | for (int i = 0; i < _sg.d3d11.num_rtvs; i++) { |
| 6610 | _sg_attachment_t* att = &_sg.d3d11.cur_pass->color_atts[i]; |
| 6611 | SOKOL_ASSERT(att->image && (att->image->slot.id == att->image_id.id)); |
| 6612 | if (att->image->sample_count > 1) { |
| 6613 | /* FIXME: support MSAA resolve into 3D texture */ |
| 6614 | SOKOL_ASSERT(att->image->d3d11_tex2d && att->image->d3d11_texmsaa && !att->image->d3d11_tex3d); |
| 6615 | SOKOL_ASSERT(DXGI_FORMAT_UNKNOWN != att->image->d3d11_format); |
| 6616 | const _sg_image_t* img = att->image; |
| 6617 | UINT dst_subres = _sg_d3d11_calcsubresource(att->mip_level, att->slice, img->num_mipmaps); |
| 6618 | ID3D11DeviceContext_ResolveSubresource(_sg.d3d11.ctx, |
| 6619 | (ID3D11Resource*) img->d3d11_tex2d, /* pDstResource */ |
| 6620 | dst_subres, /* DstSubresource */ |
| 6621 | (ID3D11Resource*) img->d3d11_texmsaa, /* pSrcResource */ |
| 6622 | 0, /* SrcSubresource */ |
| 6623 | img->d3d11_format); |
| 6624 | } |
| 6625 | } |
| 6626 | } |
| 6627 | _sg.d3d11.cur_pass = 0; |
| 6628 | _sg.d3d11.cur_pass_id.id = SG_INVALID_ID; |
| 6629 | _sg.d3d11.cur_pipeline = 0; |
| 6630 | _sg.d3d11.cur_pipeline_id.id = SG_INVALID_ID; |
| 6631 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 6632 | _sg.d3d11.cur_rtvs[i] = 0; |
| 6633 | } |
| 6634 | _sg.d3d11.cur_dsv = 0; |
| 6635 | _sg_d3d11_clear_state(); |
| 6636 | } |
| 6637 | |
| 6638 | _SOKOL_PRIVATE void _sg_apply_viewport(int x, int y, int w, int h, bool origin_top_left) { |
| 6639 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 6640 | SOKOL_ASSERT(_sg.d3d11.in_pass); |
| 6641 | D3D11_VIEWPORT vp; |
| 6642 | vp.TopLeftX = (FLOAT) x; |
| 6643 | vp.TopLeftY = (FLOAT) (origin_top_left ? y : (_sg.d3d11.cur_height - (y + h))); |
| 6644 | vp.Width = (FLOAT) w; |
| 6645 | vp.Height = (FLOAT) h; |
| 6646 | vp.MinDepth = 0.0f; |
| 6647 | vp.MaxDepth = 1.0f; |
| 6648 | ID3D11DeviceContext_RSSetViewports(_sg.d3d11.ctx, 1, &vp); |
| 6649 | } |
| 6650 | |
| 6651 | _SOKOL_PRIVATE void _sg_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) { |
| 6652 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 6653 | SOKOL_ASSERT(_sg.d3d11.in_pass); |
| 6654 | D3D11_RECT rect; |
| 6655 | rect.left = x; |
| 6656 | rect.top = (origin_top_left ? y : (_sg.d3d11.cur_height - (y + h))); |
| 6657 | rect.right = x + w; |
| 6658 | rect.bottom = origin_top_left ? (y + h) : (_sg.d3d11.cur_height - y); |
| 6659 | ID3D11DeviceContext_RSSetScissorRects(_sg.d3d11.ctx, 1, &rect); |
| 6660 | } |
| 6661 | |
| 6662 | _SOKOL_PRIVATE void _sg_apply_pipeline(_sg_pipeline_t* pip) { |
| 6663 | SOKOL_ASSERT(pip); |
| 6664 | SOKOL_ASSERT(pip->shader); |
| 6665 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 6666 | SOKOL_ASSERT(_sg.d3d11.in_pass); |
| 6667 | SOKOL_ASSERT(pip->d3d11_rs && pip->d3d11_bs && pip->d3d11_dss && pip->d3d11_il); |
| 6668 | |
| 6669 | _sg.d3d11.cur_pipeline = pip; |
| 6670 | _sg.d3d11.cur_pipeline_id.id = pip->slot.id; |
| 6671 | _sg.d3d11.use_indexed_draw = (pip->d3d11_index_format != DXGI_FORMAT_UNKNOWN); |
| 6672 | |
| 6673 | ID3D11DeviceContext_RSSetState(_sg.d3d11.ctx, pip->d3d11_rs); |
| 6674 | ID3D11DeviceContext_OMSetDepthStencilState(_sg.d3d11.ctx, pip->d3d11_dss, pip->d3d11_stencil_ref); |
| 6675 | ID3D11DeviceContext_OMSetBlendState(_sg.d3d11.ctx, pip->d3d11_bs, pip->blend_color, 0xFFFFFFFF); |
| 6676 | ID3D11DeviceContext_IASetPrimitiveTopology(_sg.d3d11.ctx, pip->d3d11_topology); |
| 6677 | ID3D11DeviceContext_IASetInputLayout(_sg.d3d11.ctx, pip->d3d11_il); |
| 6678 | ID3D11DeviceContext_VSSetShader(_sg.d3d11.ctx, pip->shader->d3d11_vs, NULL, 0); |
| 6679 | ID3D11DeviceContext_VSSetConstantBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_UBS, pip->shader->stage[SG_SHADERSTAGE_VS].d3d11_cbs); |
| 6680 | ID3D11DeviceContext_PSSetShader(_sg.d3d11.ctx, pip->shader->d3d11_fs, NULL, 0); |
| 6681 | ID3D11DeviceContext_PSSetConstantBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_UBS, pip->shader->stage[SG_SHADERSTAGE_FS].d3d11_cbs); |
| 6682 | } |
| 6683 | |
| 6684 | _SOKOL_PRIVATE void _sg_apply_bindings( |
| 6685 | _sg_pipeline_t* pip, |
| 6686 | _sg_buffer_t** vbs, const int* vb_offsets, int num_vbs, |
| 6687 | _sg_buffer_t* ib, int ib_offset, |
| 6688 | _sg_image_t** vs_imgs, int num_vs_imgs, |
| 6689 | _sg_image_t** fs_imgs, int num_fs_imgs) |
| 6690 | { |
| 6691 | SOKOL_ASSERT(pip); |
| 6692 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 6693 | SOKOL_ASSERT(_sg.d3d11.in_pass); |
| 6694 | |
| 6695 | /* gather all the D3D11 resources into arrays */ |
| 6696 | ID3D11Buffer* d3d11_ib = ib ? ib->d3d11_buf : 0; |
| 6697 | ID3D11Buffer* d3d11_vbs[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 6698 | UINT d3d11_vb_offsets[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 6699 | ID3D11ShaderResourceView* d3d11_vs_srvs[SG_MAX_SHADERSTAGE_IMAGES]; |
| 6700 | ID3D11SamplerState* d3d11_vs_smps[SG_MAX_SHADERSTAGE_IMAGES]; |
| 6701 | ID3D11ShaderResourceView* d3d11_fs_srvs[SG_MAX_SHADERSTAGE_IMAGES]; |
| 6702 | ID3D11SamplerState* d3d11_fs_smps[SG_MAX_SHADERSTAGE_IMAGES]; |
| 6703 | int i; |
| 6704 | for (i = 0; i < num_vbs; i++) { |
| 6705 | SOKOL_ASSERT(vbs[i]->d3d11_buf); |
| 6706 | d3d11_vbs[i] = vbs[i]->d3d11_buf; |
| 6707 | d3d11_vb_offsets[i] = vb_offsets[i]; |
| 6708 | } |
| 6709 | for (; i < SG_MAX_SHADERSTAGE_BUFFERS; i++) { |
| 6710 | d3d11_vbs[i] = 0; |
| 6711 | d3d11_vb_offsets[i] = 0; |
| 6712 | } |
| 6713 | for (i = 0; i < num_vs_imgs; i++) { |
| 6714 | SOKOL_ASSERT(vs_imgs[i]->d3d11_srv); |
| 6715 | SOKOL_ASSERT(vs_imgs[i]->d3d11_smp); |
| 6716 | d3d11_vs_srvs[i] = vs_imgs[i]->d3d11_srv; |
| 6717 | d3d11_vs_smps[i] = vs_imgs[i]->d3d11_smp; |
| 6718 | } |
| 6719 | for (; i < SG_MAX_SHADERSTAGE_IMAGES; i++) { |
| 6720 | d3d11_vs_srvs[i] = 0; |
| 6721 | d3d11_vs_smps[i] = 0; |
| 6722 | } |
| 6723 | for (i = 0; i < num_fs_imgs; i++) { |
| 6724 | SOKOL_ASSERT(fs_imgs[i]->d3d11_srv); |
| 6725 | SOKOL_ASSERT(fs_imgs[i]->d3d11_smp); |
| 6726 | d3d11_fs_srvs[i] = fs_imgs[i]->d3d11_srv; |
| 6727 | d3d11_fs_smps[i] = fs_imgs[i]->d3d11_smp; |
| 6728 | } |
| 6729 | for (; i < SG_MAX_SHADERSTAGE_IMAGES; i++) { |
| 6730 | d3d11_fs_srvs[i] = 0; |
| 6731 | d3d11_fs_smps[i] = 0; |
| 6732 | } |
| 6733 | |
| 6734 | ID3D11DeviceContext_IASetVertexBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_BUFFERS, d3d11_vbs, pip->d3d11_vb_strides, d3d11_vb_offsets); |
| 6735 | ID3D11DeviceContext_IASetIndexBuffer(_sg.d3d11.ctx, d3d11_ib, pip->d3d11_index_format, ib_offset); |
| 6736 | ID3D11DeviceContext_VSSetShaderResources(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, d3d11_vs_srvs); |
| 6737 | ID3D11DeviceContext_VSSetSamplers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, d3d11_vs_smps); |
| 6738 | ID3D11DeviceContext_PSSetShaderResources(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, d3d11_fs_srvs); |
| 6739 | ID3D11DeviceContext_PSSetSamplers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_IMAGES, d3d11_fs_smps); |
| 6740 | } |
| 6741 | |
| 6742 | _SOKOL_PRIVATE void _sg_apply_uniforms(sg_shader_stage stage_index, int ub_index, const void* data, int num_bytes) { |
| 6743 | _SOKOL_UNUSED(num_bytes); |
| 6744 | SOKOL_ASSERT(_sg.d3d11.ctx && _sg.d3d11.in_pass); |
| 6745 | SOKOL_ASSERT(data && (num_bytes > 0)); |
| 6746 | SOKOL_ASSERT((stage_index >= 0) && (stage_index < SG_NUM_SHADER_STAGES)); |
| 6747 | SOKOL_ASSERT((ub_index >= 0) && (ub_index < SG_MAX_SHADERSTAGE_UBS)); |
| 6748 | SOKOL_ASSERT(_sg.d3d11.cur_pipeline && _sg.d3d11.cur_pipeline->slot.id == _sg.d3d11.cur_pipeline_id.id); |
| 6749 | SOKOL_ASSERT(_sg.d3d11.cur_pipeline->shader && _sg.d3d11.cur_pipeline->shader->slot.id == _sg.d3d11.cur_pipeline->shader_id.id); |
| 6750 | SOKOL_ASSERT(ub_index < _sg.d3d11.cur_pipeline->shader->stage[stage_index].num_uniform_blocks); |
| 6751 | SOKOL_ASSERT(num_bytes == _sg.d3d11.cur_pipeline->shader->stage[stage_index].uniform_blocks[ub_index].size); |
| 6752 | ID3D11Buffer* cb = _sg.d3d11.cur_pipeline->shader->stage[stage_index].d3d11_cbs[ub_index]; |
| 6753 | SOKOL_ASSERT(cb); |
| 6754 | ID3D11DeviceContext_UpdateSubresource(_sg.d3d11.ctx, (ID3D11Resource*)cb, 0, NULL, data, 0, 0); |
| 6755 | } |
| 6756 | |
| 6757 | _SOKOL_PRIVATE void _sg_draw(int base_element, int num_elements, int num_instances) { |
| 6758 | SOKOL_ASSERT(_sg.d3d11.in_pass); |
| 6759 | if (_sg.d3d11.use_indexed_draw) { |
| 6760 | if (1 == num_instances) { |
| 6761 | ID3D11DeviceContext_DrawIndexed(_sg.d3d11.ctx, num_elements, base_element, 0); |
| 6762 | } |
| 6763 | else { |
| 6764 | ID3D11DeviceContext_DrawIndexedInstanced(_sg.d3d11.ctx, num_elements, num_instances, base_element, 0, 0); |
| 6765 | } |
| 6766 | } |
| 6767 | else { |
| 6768 | if (1 == num_instances) { |
| 6769 | ID3D11DeviceContext_Draw(_sg.d3d11.ctx, num_elements, base_element); |
| 6770 | } |
| 6771 | else { |
| 6772 | ID3D11DeviceContext_DrawInstanced(_sg.d3d11.ctx, num_elements, num_instances, base_element, 0); |
| 6773 | } |
| 6774 | } |
| 6775 | } |
| 6776 | |
| 6777 | _SOKOL_PRIVATE void _sg_commit(void) { |
| 6778 | SOKOL_ASSERT(!_sg.d3d11.in_pass); |
| 6779 | } |
| 6780 | |
| 6781 | _SOKOL_PRIVATE void _sg_update_buffer(_sg_buffer_t* buf, const void* data_ptr, int data_size) { |
| 6782 | SOKOL_ASSERT(buf && data_ptr && (data_size > 0)); |
| 6783 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 6784 | SOKOL_ASSERT(buf->d3d11_buf); |
| 6785 | D3D11_MAPPED_SUBRESOURCE d3d11_msr; |
| 6786 | HRESULT hr = ID3D11DeviceContext_Map(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11_buf, 0, D3D11_MAP_WRITE_DISCARD, 0, &d3d11_msr); |
| 6787 | _SOKOL_UNUSED(hr); |
| 6788 | SOKOL_ASSERT(SUCCEEDED(hr)); |
| 6789 | memcpy(d3d11_msr.pData, data_ptr, data_size); |
| 6790 | ID3D11DeviceContext_Unmap(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11_buf, 0); |
| 6791 | } |
| 6792 | |
| 6793 | _SOKOL_PRIVATE void _sg_append_buffer(_sg_buffer_t* buf, const void* data_ptr, int data_size, bool new_frame) { |
| 6794 | SOKOL_ASSERT(buf && data_ptr && (data_size > 0)); |
| 6795 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 6796 | SOKOL_ASSERT(buf->d3d11_buf); |
| 6797 | D3D11_MAP map_type = new_frame ? D3D11_MAP_WRITE_DISCARD : D3D11_MAP_WRITE_NO_OVERWRITE; |
| 6798 | D3D11_MAPPED_SUBRESOURCE d3d11_msr; |
| 6799 | HRESULT hr = ID3D11DeviceContext_Map(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11_buf, 0, map_type, 0, &d3d11_msr); |
| 6800 | _SOKOL_UNUSED(hr); |
| 6801 | SOKOL_ASSERT(SUCCEEDED(hr)); |
| 6802 | uint8_t* dst_ptr = (uint8_t*)d3d11_msr.pData + buf->append_pos; |
| 6803 | memcpy(dst_ptr, data_ptr, data_size); |
| 6804 | ID3D11DeviceContext_Unmap(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11_buf, 0); |
| 6805 | } |
| 6806 | |
| 6807 | _SOKOL_PRIVATE void _sg_update_image(_sg_image_t* img, const sg_image_content* data) { |
| 6808 | SOKOL_ASSERT(img && data); |
| 6809 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 6810 | SOKOL_ASSERT(img->d3d11_tex2d || img->d3d11_tex3d); |
| 6811 | ID3D11Resource* d3d11_res = 0; |
| 6812 | if (img->d3d11_tex3d) { |
| 6813 | d3d11_res = (ID3D11Resource*) img->d3d11_tex3d; |
| 6814 | } |
| 6815 | else { |
| 6816 | d3d11_res = (ID3D11Resource*) img->d3d11_tex2d; |
| 6817 | } |
| 6818 | SOKOL_ASSERT(d3d11_res); |
| 6819 | const int num_faces = (img->type == SG_IMAGETYPE_CUBE) ? 6:1; |
| 6820 | const int num_slices = (img->type == SG_IMAGETYPE_ARRAY) ? img->depth:1; |
| 6821 | int subres_index = 0; |
| 6822 | HRESULT hr; |
| 6823 | D3D11_MAPPED_SUBRESOURCE d3d11_msr; |
| 6824 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 6825 | for (int slice_index = 0; slice_index < num_slices; slice_index++) { |
| 6826 | for (int mip_index = 0; mip_index < img->num_mipmaps; mip_index++, subres_index++) { |
| 6827 | SOKOL_ASSERT(subres_index < (SG_MAX_MIPMAPS * SG_MAX_TEXTUREARRAY_LAYERS)); |
| 6828 | const int mip_width = ((img->width>>mip_index)>0) ? img->width>>mip_index : 1; |
| 6829 | const int mip_height = ((img->height>>mip_index)>0) ? img->height>>mip_index : 1; |
| 6830 | const int src_pitch = _sg_row_pitch(img->pixel_format, mip_width); |
| 6831 | const sg_subimage_content* subimg_content = &(data->subimage[face_index][mip_index]); |
| 6832 | const int slice_size = subimg_content->size / num_slices; |
| 6833 | const int slice_offset = slice_size * slice_index; |
| 6834 | const uint8_t* slice_ptr = ((const uint8_t*)subimg_content->ptr) + slice_offset; |
| 6835 | hr = ID3D11DeviceContext_Map(_sg.d3d11.ctx, d3d11_res, subres_index, D3D11_MAP_WRITE_DISCARD, 0, &d3d11_msr); |
| 6836 | SOKOL_ASSERT(SUCCEEDED(hr)); |
| 6837 | /* FIXME: need to handle difference in depth-pitch for 3D textures as well! */ |
| 6838 | if (src_pitch == (int)d3d11_msr.RowPitch) { |
| 6839 | memcpy(d3d11_msr.pData, slice_ptr, slice_size); |
| 6840 | } |
| 6841 | else { |
| 6842 | SOKOL_ASSERT(src_pitch < (int)d3d11_msr.RowPitch); |
| 6843 | const uint8_t* src_ptr = slice_ptr; |
| 6844 | uint8_t* dst_ptr = (uint8_t*) d3d11_msr.pData; |
| 6845 | for (int row_index = 0; row_index < mip_height; row_index++) { |
| 6846 | memcpy(dst_ptr, src_ptr, src_pitch); |
| 6847 | src_ptr += src_pitch; |
| 6848 | dst_ptr += d3d11_msr.RowPitch; |
| 6849 | } |
| 6850 | } |
| 6851 | ID3D11DeviceContext_Unmap(_sg.d3d11.ctx, d3d11_res, subres_index); |
| 6852 | } |
| 6853 | } |
| 6854 | } |
| 6855 | } |
| 6856 | |
| 6857 | /*== METAL BACKEND IMPLEMENTATION ============================================*/ |
| 6858 | #elif defined(SOKOL_METAL) |
| 6859 | |
| 6860 | /*-- enum translation functions ----------------------------------------------*/ |
| 6861 | _SOKOL_PRIVATE MTLLoadAction _sg_mtl_load_action(sg_action a) { |
| 6862 | switch (a) { |
| 6863 | case SG_ACTION_CLEAR: return MTLLoadActionClear; |
| 6864 | case SG_ACTION_LOAD: return MTLLoadActionLoad; |
| 6865 | case SG_ACTION_DONTCARE: return MTLLoadActionDontCare; |
| 6866 | default: SOKOL_UNREACHABLE; return (MTLLoadAction)0; |
| 6867 | } |
| 6868 | } |
| 6869 | |
| 6870 | _SOKOL_PRIVATE MTLResourceOptions _sg_mtl_buffer_resource_options(sg_usage usg) { |
| 6871 | switch (usg) { |
| 6872 | case SG_USAGE_IMMUTABLE: |
| 6873 | return MTLResourceStorageModeShared; |
| 6874 | case SG_USAGE_DYNAMIC: |
| 6875 | case SG_USAGE_STREAM: |
| 6876 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 6877 | return MTLCPUCacheModeWriteCombined|MTLResourceStorageModeManaged; |
| 6878 | #else |
| 6879 | return MTLCPUCacheModeWriteCombined; |
| 6880 | #endif |
| 6881 | default: |
| 6882 | SOKOL_UNREACHABLE; |
| 6883 | return 0; |
| 6884 | } |
| 6885 | } |
| 6886 | |
| 6887 | _SOKOL_PRIVATE MTLVertexStepFunction _sg_mtl_step_function(sg_vertex_step step) { |
| 6888 | switch (step) { |
| 6889 | case SG_VERTEXSTEP_PER_VERTEX: return MTLVertexStepFunctionPerVertex; |
| 6890 | case SG_VERTEXSTEP_PER_INSTANCE: return MTLVertexStepFunctionPerInstance; |
| 6891 | default: SOKOL_UNREACHABLE; return (MTLVertexStepFunction)0; |
| 6892 | } |
| 6893 | } |
| 6894 | |
| 6895 | _SOKOL_PRIVATE MTLVertexFormat _sg_mtl_vertex_format(sg_vertex_format fmt) { |
| 6896 | switch (fmt) { |
| 6897 | case SG_VERTEXFORMAT_FLOAT: return MTLVertexFormatFloat; |
| 6898 | case SG_VERTEXFORMAT_FLOAT2: return MTLVertexFormatFloat2; |
| 6899 | case SG_VERTEXFORMAT_FLOAT3: return MTLVertexFormatFloat3; |
| 6900 | case SG_VERTEXFORMAT_FLOAT4: return MTLVertexFormatFloat4; |
| 6901 | case SG_VERTEXFORMAT_BYTE4: return MTLVertexFormatChar4; |
| 6902 | case SG_VERTEXFORMAT_BYTE4N: return MTLVertexFormatChar4Normalized; |
| 6903 | case SG_VERTEXFORMAT_UBYTE4: return MTLVertexFormatUChar4; |
| 6904 | case SG_VERTEXFORMAT_UBYTE4N: return MTLVertexFormatUChar4Normalized; |
| 6905 | case SG_VERTEXFORMAT_SHORT2: return MTLVertexFormatShort2; |
| 6906 | case SG_VERTEXFORMAT_SHORT2N: return MTLVertexFormatShort2Normalized; |
| 6907 | case SG_VERTEXFORMAT_SHORT4: return MTLVertexFormatShort4; |
| 6908 | case SG_VERTEXFORMAT_SHORT4N: return MTLVertexFormatShort4Normalized; |
| 6909 | case SG_VERTEXFORMAT_UINT10_N2: return MTLVertexFormatUInt1010102Normalized; |
| 6910 | default: SOKOL_UNREACHABLE; return (MTLVertexFormat)0; |
| 6911 | } |
| 6912 | } |
| 6913 | |
| 6914 | _SOKOL_PRIVATE MTLPrimitiveType _sg_mtl_primitive_type(sg_primitive_type t) { |
| 6915 | switch (t) { |
| 6916 | case SG_PRIMITIVETYPE_POINTS: return MTLPrimitiveTypePoint; |
| 6917 | case SG_PRIMITIVETYPE_LINES: return MTLPrimitiveTypeLine; |
| 6918 | case SG_PRIMITIVETYPE_LINE_STRIP: return MTLPrimitiveTypeLineStrip; |
| 6919 | case SG_PRIMITIVETYPE_TRIANGLES: return MTLPrimitiveTypeTriangle; |
| 6920 | case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return MTLPrimitiveTypeTriangleStrip; |
| 6921 | default: SOKOL_UNREACHABLE; return (MTLPrimitiveType)0; |
| 6922 | } |
| 6923 | } |
| 6924 | |
| 6925 | _SOKOL_PRIVATE MTLPixelFormat _sg_mtl_texture_format(sg_pixel_format fmt) { |
| 6926 | switch (fmt) { |
| 6927 | case SG_PIXELFORMAT_RGBA8: return MTLPixelFormatRGBA8Unorm; |
| 6928 | case SG_PIXELFORMAT_R10G10B10A2: return MTLPixelFormatRGB10A2Unorm; |
| 6929 | case SG_PIXELFORMAT_RGBA32F: return MTLPixelFormatRGBA32Float; |
| 6930 | case SG_PIXELFORMAT_RGBA16F: return MTLPixelFormatRGBA16Float; |
| 6931 | case SG_PIXELFORMAT_R32F: return MTLPixelFormatR32Float; |
| 6932 | case SG_PIXELFORMAT_R16F: return MTLPixelFormatR16Float; |
| 6933 | case SG_PIXELFORMAT_L8: return MTLPixelFormatR8Unorm; |
| 6934 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 6935 | case SG_PIXELFORMAT_DXT1: return MTLPixelFormatBC1_RGBA; |
| 6936 | case SG_PIXELFORMAT_DXT3: return MTLPixelFormatBC2_RGBA; |
| 6937 | case SG_PIXELFORMAT_DXT5: return MTLPixelFormatBC3_RGBA; |
| 6938 | #else |
| 6939 | case SG_PIXELFORMAT_PVRTC2_RGB: return MTLPixelFormatPVRTC_RGB_2BPP; |
| 6940 | case SG_PIXELFORMAT_PVRTC4_RGB: return MTLPixelFormatPVRTC_RGB_4BPP; |
| 6941 | case SG_PIXELFORMAT_PVRTC2_RGBA: return MTLPixelFormatPVRTC_RGBA_2BPP; |
| 6942 | case SG_PIXELFORMAT_PVRTC4_RGBA: return MTLPixelFormatPVRTC_RGBA_4BPP; |
| 6943 | case SG_PIXELFORMAT_ETC2_RGB8: return MTLPixelFormatETC2_RGB8; |
| 6944 | case SG_PIXELFORMAT_ETC2_SRGB8: return MTLPixelFormatETC2_RGB8_sRGB; |
| 6945 | #endif |
| 6946 | default: return MTLPixelFormatInvalid; |
| 6947 | } |
| 6948 | } |
| 6949 | |
| 6950 | _SOKOL_PRIVATE MTLPixelFormat _sg_mtl_rendertarget_color_format(sg_pixel_format fmt) { |
| 6951 | switch (fmt) { |
| 6952 | case SG_PIXELFORMAT_RGBA8: return MTLPixelFormatBGRA8Unorm; /* not a bug */ |
| 6953 | case SG_PIXELFORMAT_RGBA32F: return MTLPixelFormatRGBA32Float; |
| 6954 | case SG_PIXELFORMAT_RGBA16F: return MTLPixelFormatRGBA16Float; |
| 6955 | case SG_PIXELFORMAT_R10G10B10A2: return MTLPixelFormatRGB10A2Unorm; |
| 6956 | default: return MTLPixelFormatInvalid; |
| 6957 | } |
| 6958 | } |
| 6959 | |
| 6960 | _SOKOL_PRIVATE MTLPixelFormat _sg_mtl_rendertarget_depth_format(sg_pixel_format fmt) { |
| 6961 | switch (fmt) { |
| 6962 | case SG_PIXELFORMAT_DEPTH: |
| 6963 | return MTLPixelFormatDepth32Float; |
| 6964 | case SG_PIXELFORMAT_DEPTHSTENCIL: |
| 6965 | /* NOTE: Depth24_Stencil8 isn't universally supported! */ |
| 6966 | return MTLPixelFormatDepth32Float_Stencil8; |
| 6967 | default: |
| 6968 | return MTLPixelFormatInvalid; |
| 6969 | } |
| 6970 | } |
| 6971 | |
| 6972 | _SOKOL_PRIVATE MTLPixelFormat _sg_mtl_rendertarget_stencil_format(sg_pixel_format fmt) { |
| 6973 | switch (fmt) { |
| 6974 | case SG_PIXELFORMAT_DEPTHSTENCIL: |
| 6975 | return MTLPixelFormatDepth32Float_Stencil8; |
| 6976 | default: |
| 6977 | return MTLPixelFormatInvalid; |
| 6978 | } |
| 6979 | } |
| 6980 | |
| 6981 | _SOKOL_PRIVATE MTLColorWriteMask _sg_mtl_color_write_mask(sg_color_mask m) { |
| 6982 | MTLColorWriteMask mtl_mask = MTLColorWriteMaskNone; |
| 6983 | if (m & SG_COLORMASK_R) { |
| 6984 | mtl_mask |= MTLColorWriteMaskRed; |
| 6985 | } |
| 6986 | if (m & SG_COLORMASK_G) { |
| 6987 | mtl_mask |= MTLColorWriteMaskGreen; |
| 6988 | } |
| 6989 | if (m & SG_COLORMASK_B) { |
| 6990 | mtl_mask |= MTLColorWriteMaskBlue; |
| 6991 | } |
| 6992 | if (m & SG_COLORMASK_A) { |
| 6993 | mtl_mask |= MTLColorWriteMaskAlpha; |
| 6994 | } |
| 6995 | return mtl_mask; |
| 6996 | } |
| 6997 | |
| 6998 | _SOKOL_PRIVATE MTLBlendOperation _sg_mtl_blend_op(sg_blend_op op) { |
| 6999 | switch (op) { |
| 7000 | case SG_BLENDOP_ADD: return MTLBlendOperationAdd; |
| 7001 | case SG_BLENDOP_SUBTRACT: return MTLBlendOperationSubtract; |
| 7002 | case SG_BLENDOP_REVERSE_SUBTRACT: return MTLBlendOperationReverseSubtract; |
| 7003 | default: SOKOL_UNREACHABLE; return (MTLBlendOperation)0; |
| 7004 | } |
| 7005 | } |
| 7006 | |
| 7007 | _SOKOL_PRIVATE MTLBlendFactor _sg_mtl_blend_factor(sg_blend_factor f) { |
| 7008 | switch (f) { |
| 7009 | case SG_BLENDFACTOR_ZERO: return MTLBlendFactorZero; |
| 7010 | case SG_BLENDFACTOR_ONE: return MTLBlendFactorOne; |
| 7011 | case SG_BLENDFACTOR_SRC_COLOR: return MTLBlendFactorSourceColor; |
| 7012 | case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return MTLBlendFactorOneMinusSourceColor; |
| 7013 | case SG_BLENDFACTOR_SRC_ALPHA: return MTLBlendFactorSourceAlpha; |
| 7014 | case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return MTLBlendFactorOneMinusSourceAlpha; |
| 7015 | case SG_BLENDFACTOR_DST_COLOR: return MTLBlendFactorDestinationColor; |
| 7016 | case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return MTLBlendFactorOneMinusDestinationColor; |
| 7017 | case SG_BLENDFACTOR_DST_ALPHA: return MTLBlendFactorDestinationAlpha; |
| 7018 | case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return MTLBlendFactorOneMinusDestinationAlpha; |
| 7019 | case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return MTLBlendFactorSourceAlphaSaturated; |
| 7020 | case SG_BLENDFACTOR_BLEND_COLOR: return MTLBlendFactorBlendColor; |
| 7021 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return MTLBlendFactorOneMinusBlendColor; |
| 7022 | case SG_BLENDFACTOR_BLEND_ALPHA: return MTLBlendFactorBlendAlpha; |
| 7023 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return MTLBlendFactorOneMinusBlendAlpha; |
| 7024 | default: SOKOL_UNREACHABLE; return (MTLBlendFactor)0; |
| 7025 | } |
| 7026 | } |
| 7027 | |
| 7028 | _SOKOL_PRIVATE MTLCompareFunction _sg_mtl_compare_func(sg_compare_func f) { |
| 7029 | switch (f) { |
| 7030 | case SG_COMPAREFUNC_NEVER: return MTLCompareFunctionNever; |
| 7031 | case SG_COMPAREFUNC_LESS: return MTLCompareFunctionLess; |
| 7032 | case SG_COMPAREFUNC_EQUAL: return MTLCompareFunctionEqual; |
| 7033 | case SG_COMPAREFUNC_LESS_EQUAL: return MTLCompareFunctionLessEqual; |
| 7034 | case SG_COMPAREFUNC_GREATER: return MTLCompareFunctionGreater; |
| 7035 | case SG_COMPAREFUNC_NOT_EQUAL: return MTLCompareFunctionNotEqual; |
| 7036 | case SG_COMPAREFUNC_GREATER_EQUAL: return MTLCompareFunctionGreaterEqual; |
| 7037 | case SG_COMPAREFUNC_ALWAYS: return MTLCompareFunctionAlways; |
| 7038 | default: SOKOL_UNREACHABLE; return (MTLCompareFunction)0; |
| 7039 | } |
| 7040 | } |
| 7041 | |
| 7042 | _SOKOL_PRIVATE MTLStencilOperation _sg_mtl_stencil_op(sg_stencil_op op) { |
| 7043 | switch (op) { |
| 7044 | case SG_STENCILOP_KEEP: return MTLStencilOperationKeep; |
| 7045 | case SG_STENCILOP_ZERO: return MTLStencilOperationZero; |
| 7046 | case SG_STENCILOP_REPLACE: return MTLStencilOperationReplace; |
| 7047 | case SG_STENCILOP_INCR_CLAMP: return MTLStencilOperationIncrementClamp; |
| 7048 | case SG_STENCILOP_DECR_CLAMP: return MTLStencilOperationDecrementClamp; |
| 7049 | case SG_STENCILOP_INVERT: return MTLStencilOperationInvert; |
| 7050 | case SG_STENCILOP_INCR_WRAP: return MTLStencilOperationIncrementWrap; |
| 7051 | case SG_STENCILOP_DECR_WRAP: return MTLStencilOperationDecrementWrap; |
| 7052 | default: SOKOL_UNREACHABLE; return (MTLStencilOperation)0; |
| 7053 | } |
| 7054 | } |
| 7055 | |
| 7056 | _SOKOL_PRIVATE MTLCullMode _sg_mtl_cull_mode(sg_cull_mode m) { |
| 7057 | switch (m) { |
| 7058 | case SG_CULLMODE_NONE: return MTLCullModeNone; |
| 7059 | case SG_CULLMODE_FRONT: return MTLCullModeFront; |
| 7060 | case SG_CULLMODE_BACK: return MTLCullModeBack; |
| 7061 | default: SOKOL_UNREACHABLE; return (MTLCullMode)0; |
| 7062 | } |
| 7063 | } |
| 7064 | |
| 7065 | _SOKOL_PRIVATE MTLWinding _sg_mtl_winding(sg_face_winding w) { |
| 7066 | switch (w) { |
| 7067 | case SG_FACEWINDING_CW: return MTLWindingClockwise; |
| 7068 | case SG_FACEWINDING_CCW: return MTLWindingCounterClockwise; |
| 7069 | default: SOKOL_UNREACHABLE; return (MTLWinding)0; |
| 7070 | } |
| 7071 | } |
| 7072 | |
| 7073 | _SOKOL_PRIVATE MTLIndexType _sg_mtl_index_type(sg_index_type t) { |
| 7074 | switch (t) { |
| 7075 | case SG_INDEXTYPE_UINT16: return MTLIndexTypeUInt16; |
| 7076 | case SG_INDEXTYPE_UINT32: return MTLIndexTypeUInt32; |
| 7077 | default: SOKOL_UNREACHABLE; return (MTLIndexType)0; |
| 7078 | } |
| 7079 | } |
| 7080 | |
| 7081 | _SOKOL_PRIVATE NSUInteger _sg_mtl_index_size(sg_index_type t) { |
| 7082 | switch (t) { |
| 7083 | case SG_INDEXTYPE_NONE: return 0; |
| 7084 | case SG_INDEXTYPE_UINT16: return 2; |
| 7085 | case SG_INDEXTYPE_UINT32: return 4; |
| 7086 | default: SOKOL_UNREACHABLE; return 0; |
| 7087 | } |
| 7088 | } |
| 7089 | |
| 7090 | _SOKOL_PRIVATE MTLTextureType _sg_mtl_texture_type(sg_image_type t) { |
| 7091 | switch (t) { |
| 7092 | case SG_IMAGETYPE_2D: return MTLTextureType2D; |
| 7093 | case SG_IMAGETYPE_CUBE: return MTLTextureTypeCube; |
| 7094 | case SG_IMAGETYPE_3D: return MTLTextureType3D; |
| 7095 | case SG_IMAGETYPE_ARRAY: return MTLTextureType2DArray; |
| 7096 | default: SOKOL_UNREACHABLE; return (MTLTextureType)0; |
| 7097 | } |
| 7098 | } |
| 7099 | |
| 7100 | _SOKOL_PRIVATE bool _sg_mtl_is_pvrtc(sg_pixel_format fmt) { |
| 7101 | switch (fmt) { |
| 7102 | case SG_PIXELFORMAT_PVRTC2_RGB: |
| 7103 | case SG_PIXELFORMAT_PVRTC2_RGBA: |
| 7104 | case SG_PIXELFORMAT_PVRTC4_RGB: |
| 7105 | case SG_PIXELFORMAT_PVRTC4_RGBA: |
| 7106 | return true; |
| 7107 | default: |
| 7108 | return false; |
| 7109 | } |
| 7110 | } |
| 7111 | |
| 7112 | _SOKOL_PRIVATE MTLSamplerAddressMode _sg_mtl_address_mode(sg_wrap w) { |
| 7113 | switch (w) { |
| 7114 | case SG_WRAP_REPEAT: return MTLSamplerAddressModeRepeat; |
| 7115 | case SG_WRAP_CLAMP_TO_EDGE: return MTLSamplerAddressModeClampToEdge; |
| 7116 | case SG_WRAP_MIRRORED_REPEAT: return MTLSamplerAddressModeMirrorRepeat; |
| 7117 | default: SOKOL_UNREACHABLE; return (MTLSamplerAddressMode)0; |
| 7118 | } |
| 7119 | } |
| 7120 | |
| 7121 | _SOKOL_PRIVATE MTLSamplerMinMagFilter _sg_mtl_minmag_filter(sg_filter f) { |
| 7122 | switch (f) { |
| 7123 | case SG_FILTER_NEAREST: |
| 7124 | case SG_FILTER_NEAREST_MIPMAP_NEAREST: |
| 7125 | case SG_FILTER_NEAREST_MIPMAP_LINEAR: |
| 7126 | return MTLSamplerMinMagFilterNearest; |
| 7127 | case SG_FILTER_LINEAR: |
| 7128 | case SG_FILTER_LINEAR_MIPMAP_NEAREST: |
| 7129 | case SG_FILTER_LINEAR_MIPMAP_LINEAR: |
| 7130 | return MTLSamplerMinMagFilterLinear; |
| 7131 | default: |
| 7132 | SOKOL_UNREACHABLE; return (MTLSamplerMinMagFilter)0; |
| 7133 | } |
| 7134 | } |
| 7135 | |
| 7136 | _SOKOL_PRIVATE MTLSamplerMipFilter _sg_mtl_mip_filter(sg_filter f) { |
| 7137 | switch (f) { |
| 7138 | case SG_FILTER_NEAREST: |
| 7139 | case SG_FILTER_LINEAR: |
| 7140 | return MTLSamplerMipFilterNotMipmapped; |
| 7141 | case SG_FILTER_NEAREST_MIPMAP_NEAREST: |
| 7142 | case SG_FILTER_LINEAR_MIPMAP_NEAREST: |
| 7143 | return MTLSamplerMipFilterNearest; |
| 7144 | case SG_FILTER_NEAREST_MIPMAP_LINEAR: |
| 7145 | case SG_FILTER_LINEAR_MIPMAP_LINEAR: |
| 7146 | return MTLSamplerMipFilterLinear; |
| 7147 | default: |
| 7148 | SOKOL_UNREACHABLE; return (MTLSamplerMipFilter)0; |
| 7149 | } |
| 7150 | } |
| 7151 | |
| 7152 | /*-- a pool for all Metal resource objects, with deferred release queue -------*/ |
| 7153 | |
| 7154 | _SOKOL_PRIVATE void _sg_mtl_init_pool(const sg_desc* desc) { |
| 7155 | _sg.mtl.idpool.num_slots = 2 * |
| 7156 | ( |
| 7157 | 2 * desc->buffer_pool_size + |
| 7158 | 5 * desc->image_pool_size + |
| 7159 | 4 * desc->shader_pool_size + |
| 7160 | 2 * desc->pipeline_pool_size + |
| 7161 | desc->pass_pool_size |
| 7162 | ); |
| 7163 | _sg_mtl_idpool = [NSMutableArray arrayWithCapacity:_sg.mtl.idpool.num_slots]; |
| 7164 | NSNull* null = [NSNull null]; |
| 7165 | for (uint32_t i = 0; i < _sg.mtl.idpool.num_slots; i++) { |
| 7166 | [_sg_mtl_idpool addObject:null]; |
| 7167 | } |
| 7168 | SOKOL_ASSERT([_sg_mtl_idpool count] == _sg.mtl.idpool.num_slots); |
| 7169 | /* a queue of currently free slot indices */ |
| 7170 | _sg.mtl.idpool.free_queue_top = 0; |
| 7171 | _sg.mtl.idpool.free_queue = (uint32_t*)SOKOL_MALLOC(_sg.mtl.idpool.num_slots * sizeof(uint32_t)); |
| 7172 | /* pool slot 0 is reserved! */ |
| 7173 | for (int i = _sg.mtl.idpool.num_slots-1; i >= 1; i--) { |
| 7174 | _sg.mtl.idpool.free_queue[_sg.mtl.idpool.free_queue_top++] = (uint32_t)i; |
| 7175 | } |
| 7176 | /* a circular queue which holds release items (frame index |
| 7177 | when a resource is to be released, and the resource's |
| 7178 | pool index |
| 7179 | */ |
| 7180 | _sg.mtl.idpool.release_queue_front = 0; |
| 7181 | _sg.mtl.idpool.release_queue_back = 0; |
| 7182 | _sg.mtl.idpool.release_queue = (_sg_mtl_release_item_t*)SOKOL_MALLOC(_sg.mtl.idpool.num_slots * sizeof(_sg_mtl_release_item_t)); |
| 7183 | for (uint32_t i = 0; i < _sg.mtl.idpool.num_slots; i++) { |
| 7184 | _sg.mtl.idpool.release_queue[i].frame_index = 0; |
| 7185 | _sg.mtl.idpool.release_queue[i].slot_index = _SG_MTL_INVALID_SLOT_INDEX; |
| 7186 | } |
| 7187 | } |
| 7188 | |
| 7189 | _SOKOL_PRIVATE void _sg_mtl_destroy_pool(void) { |
| 7190 | SOKOL_FREE(_sg.mtl.idpool.release_queue); _sg.mtl.idpool.release_queue = 0; |
| 7191 | SOKOL_FREE(_sg.mtl.idpool.free_queue); _sg.mtl.idpool.free_queue = 0; |
| 7192 | _sg_mtl_idpool = nil; |
| 7193 | } |
| 7194 | |
| 7195 | /* get a new free resource pool slot */ |
| 7196 | _SOKOL_PRIVATE uint32_t _sg_mtl_alloc_pool_slot(void) { |
| 7197 | SOKOL_ASSERT(_sg.mtl.idpool.free_queue_top > 0); |
| 7198 | const uint32_t slot_index = _sg.mtl.idpool.free_queue[--_sg.mtl.idpool.free_queue_top]; |
| 7199 | SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots)); |
| 7200 | return slot_index; |
| 7201 | } |
| 7202 | |
| 7203 | /* put a free resource pool slot back into the free-queue */ |
| 7204 | _SOKOL_PRIVATE void _sg_mtl_free_pool_slot(uint32_t slot_index) { |
| 7205 | SOKOL_ASSERT(_sg.mtl.idpool.free_queue_top < _sg.mtl.idpool.num_slots); |
| 7206 | SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots)); |
| 7207 | _sg.mtl.idpool.free_queue[_sg.mtl.idpool.free_queue_top++] = slot_index; |
| 7208 | } |
| 7209 | |
| 7210 | /* add an MTLResource to the pool, return pool index or 0 if input was 'nil' */ |
| 7211 | _SOKOL_PRIVATE uint32_t _sg_mtl_add_resource(id res) { |
| 7212 | if (nil == res) { |
| 7213 | return _SG_MTL_INVALID_SLOT_INDEX; |
| 7214 | } |
| 7215 | const uint32_t slot_index = _sg_mtl_alloc_pool_slot(); |
| 7216 | SOKOL_ASSERT([NSNull null] == _sg_mtl_idpool[slot_index]); |
| 7217 | _sg_mtl_idpool[slot_index] = res; |
| 7218 | return slot_index; |
| 7219 | } |
| 7220 | |
| 7221 | /* mark an MTLResource for release, this will put the resource into the |
| 7222 | deferred-release queue, and the resource will then be released N frames later, |
| 7223 | the special pool index 0 will be ignored (this means that a nil |
| 7224 | value was provided to _sg_mtl_add_resource() |
| 7225 | */ |
| 7226 | _SOKOL_PRIVATE void _sg_mtl_release_resource(uint32_t frame_index, uint32_t slot_index) { |
| 7227 | if (slot_index == _SG_MTL_INVALID_SLOT_INDEX) { |
| 7228 | return; |
| 7229 | } |
| 7230 | SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots)); |
| 7231 | SOKOL_ASSERT([NSNull null] != _sg_mtl_idpool[slot_index]); |
| 7232 | int release_index = _sg.mtl.idpool.release_queue_front++; |
| 7233 | if (_sg.mtl.idpool.release_queue_front >= _sg.mtl.idpool.num_slots) { |
| 7234 | /* wrap-around */ |
| 7235 | _sg.mtl.idpool.release_queue_front = 0; |
| 7236 | } |
| 7237 | /* release queue full? */ |
| 7238 | SOKOL_ASSERT(_sg.mtl.idpool.release_queue_front != _sg.mtl.idpool.release_queue_back); |
| 7239 | SOKOL_ASSERT(0 == _sg.mtl.idpool.release_queue[release_index].frame_index); |
| 7240 | const uint32_t safe_to_release_frame_index = frame_index + SG_NUM_INFLIGHT_FRAMES + 1; |
| 7241 | _sg.mtl.idpool.release_queue[release_index].frame_index = safe_to_release_frame_index; |
| 7242 | _sg.mtl.idpool.release_queue[release_index].slot_index = slot_index; |
| 7243 | } |
| 7244 | |
| 7245 | /* run garbage-collection pass on all resources in the release-queue */ |
| 7246 | _SOKOL_PRIVATE void _sg_mtl_garbage_collect(uint32_t frame_index) { |
| 7247 | while (_sg.mtl.idpool.release_queue_back != _sg.mtl.idpool.release_queue_front) { |
| 7248 | if (frame_index < _sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].frame_index) { |
| 7249 | /* don't need to check further, release-items past this are too young */ |
| 7250 | break; |
| 7251 | } |
| 7252 | /* safe to release this resource */ |
| 7253 | const uint32_t slot_index = _sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].slot_index; |
| 7254 | SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots)); |
| 7255 | SOKOL_ASSERT(_sg_mtl_idpool[slot_index] != [NSNull null]); |
| 7256 | _sg_mtl_idpool[slot_index] = [NSNull null]; |
| 7257 | /* put the now free pool index back on the free queue */ |
| 7258 | _sg_mtl_free_pool_slot(slot_index); |
| 7259 | /* reset the release queue slot and advance the back index */ |
| 7260 | _sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].frame_index = 0; |
| 7261 | _sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].slot_index = _SG_MTL_INVALID_SLOT_INDEX; |
| 7262 | _sg.mtl.idpool.release_queue_back++; |
| 7263 | if (_sg.mtl.idpool.release_queue_back >= _sg.mtl.idpool.num_slots) { |
| 7264 | /* wrap-around */ |
| 7265 | _sg.mtl.idpool.release_queue_back = 0; |
| 7266 | } |
| 7267 | } |
| 7268 | } |
| 7269 | |
| 7270 | /*-- a very simple sampler cache ----------------------------------------------- |
| 7271 | |
| 7272 | since there's only a small number of different samplers, sampler objects |
| 7273 | will never be deleted (except on shutdown), and searching an identical |
| 7274 | sampler is a simple linear search |
| 7275 | */ |
| 7276 | /* initialize the sampler cache */ |
| 7277 | _SOKOL_PRIVATE void _sg_mtl_init_sampler_cache(const sg_desc* desc) { |
| 7278 | SOKOL_ASSERT(desc->mtl_sampler_cache_size > 0); |
| 7279 | _sg.mtl.sampler_cache.capacity = desc->mtl_sampler_cache_size; |
| 7280 | _sg.mtl.sampler_cache.num_items = 0; |
| 7281 | const int size = _sg.mtl.sampler_cache.capacity * sizeof(_sg_mtl_sampler_cache_item_t); |
| 7282 | _sg.mtl.sampler_cache.items = (_sg_mtl_sampler_cache_item_t*)SOKOL_MALLOC(size); |
| 7283 | memset(_sg.mtl.sampler_cache.items, 0, size); |
| 7284 | } |
| 7285 | |
| 7286 | /* destroy the sampler cache, and release all sampler objects */ |
| 7287 | _SOKOL_PRIVATE void _sg_mtl_destroy_sampler_cache(uint32_t frame_index) { |
| 7288 | SOKOL_ASSERT(_sg.mtl.sampler_cache.items); |
| 7289 | SOKOL_ASSERT(_sg.mtl.sampler_cache.num_items <= _sg.mtl.sampler_cache.capacity); |
| 7290 | for (int i = 0; i < _sg.mtl.sampler_cache.num_items; i++) { |
| 7291 | _sg_mtl_release_resource(frame_index, _sg.mtl.sampler_cache.items[i].mtl_sampler_state); |
| 7292 | } |
| 7293 | SOKOL_FREE(_sg.mtl.sampler_cache.items); _sg.mtl.sampler_cache.items = 0; |
| 7294 | _sg.mtl.sampler_cache.num_items = 0; |
| 7295 | _sg.mtl.sampler_cache.capacity = 0; |
| 7296 | } |
| 7297 | |
| 7298 | /* |
| 7299 | create and add an MTLSamplerStateObject and return its resource pool index, |
| 7300 | reuse identical sampler state if one exists |
| 7301 | */ |
| 7302 | _SOKOL_PRIVATE uint32_t _sg_mtl_create_sampler(id<MTLDevice> mtl_device, const sg_image_desc* img_desc) { |
| 7303 | SOKOL_ASSERT(img_desc); |
| 7304 | SOKOL_ASSERT(_sg.mtl.sampler_cache.items); |
| 7305 | /* sampler state cache is full */ |
| 7306 | const sg_filter min_filter = img_desc->min_filter; |
| 7307 | const sg_filter mag_filter = img_desc->mag_filter; |
| 7308 | const sg_wrap wrap_u = img_desc->wrap_u; |
| 7309 | const sg_wrap wrap_v = img_desc->wrap_v; |
| 7310 | const sg_wrap wrap_w = img_desc->wrap_w; |
| 7311 | const uint32_t max_anisotropy = img_desc->max_anisotropy; |
| 7312 | /* convert floats to valid int for proper comparison */ |
| 7313 | const int min_lod = (int)(img_desc->min_lod * 1000.0f); |
| 7314 | const int max_lod = (int)(_sg_clamp(img_desc->max_lod, 0.0f, 1000.0f) * 1000.0f); |
| 7315 | /* first try to find identical sampler, number of samplers will be small, so linear search is ok */ |
| 7316 | for (int i = 0; i < _sg.mtl.sampler_cache.num_items; i++) { |
| 7317 | _sg_mtl_sampler_cache_item_t* item = &_sg.mtl.sampler_cache.items[i]; |
| 7318 | if ((min_filter == item->min_filter) && |
| 7319 | (mag_filter == item->mag_filter) && |
| 7320 | (wrap_u == item->wrap_u) && |
| 7321 | (wrap_v == item->wrap_v) && |
| 7322 | (wrap_w == item->wrap_w) && |
| 7323 | (max_anisotropy == item->max_anisotropy) && |
| 7324 | (min_lod == item->min_lod) && |
| 7325 | (max_lod == item->max_lod)) |
| 7326 | { |
| 7327 | return item->mtl_sampler_state; |
| 7328 | } |
| 7329 | } |
| 7330 | /* fallthrough: need to create a new MTLSamplerState object */ |
| 7331 | SOKOL_ASSERT(_sg.mtl.sampler_cache.num_items < _sg.mtl.sampler_cache.capacity); |
| 7332 | _sg_mtl_sampler_cache_item_t* new_item = &_sg.mtl.sampler_cache.items[_sg.mtl.sampler_cache.num_items++]; |
| 7333 | new_item->min_filter = min_filter; |
| 7334 | new_item->mag_filter = mag_filter; |
| 7335 | new_item->wrap_u = wrap_u; |
| 7336 | new_item->wrap_v = wrap_v; |
| 7337 | new_item->wrap_w = wrap_w; |
| 7338 | new_item->min_lod = min_lod; |
| 7339 | new_item->max_lod = max_lod; |
| 7340 | new_item->max_anisotropy = max_anisotropy; |
| 7341 | MTLSamplerDescriptor* mtl_desc = [[MTLSamplerDescriptor alloc] init]; |
| 7342 | mtl_desc.sAddressMode = _sg_mtl_address_mode(wrap_u); |
| 7343 | mtl_desc.tAddressMode = _sg_mtl_address_mode(wrap_v); |
| 7344 | if (SG_IMAGETYPE_3D == img_desc->type) { |
| 7345 | mtl_desc.rAddressMode = _sg_mtl_address_mode(wrap_w); |
| 7346 | } |
| 7347 | mtl_desc.minFilter = _sg_mtl_minmag_filter(min_filter); |
| 7348 | mtl_desc.magFilter = _sg_mtl_minmag_filter(mag_filter); |
| 7349 | mtl_desc.mipFilter = _sg_mtl_mip_filter(min_filter); |
| 7350 | mtl_desc.lodMinClamp = img_desc->min_lod; |
| 7351 | mtl_desc.lodMaxClamp = img_desc->max_lod; |
| 7352 | mtl_desc.maxAnisotropy = max_anisotropy; |
| 7353 | mtl_desc.normalizedCoordinates = YES; |
| 7354 | id<MTLSamplerState> mtl_sampler = [mtl_device newSamplerStateWithDescriptor:mtl_desc]; |
| 7355 | new_item->mtl_sampler_state = _sg_mtl_add_resource(mtl_sampler); |
| 7356 | return new_item->mtl_sampler_state; |
| 7357 | } |
| 7358 | |
| 7359 | /*-- a simple state cache for the resource bindings --------------------------*/ |
| 7360 | _SOKOL_PRIVATE void _sg_mtl_clear_state_cache(void) { |
| 7361 | memset(&_sg.mtl.state_cache, 0, sizeof(_sg.mtl.state_cache)); |
| 7362 | } |
| 7363 | |
| 7364 | /*-- main Metal backend state and functions ----------------------------------*/ |
| 7365 | _SOKOL_PRIVATE void _sg_setup_backend(const sg_desc* desc) { |
| 7366 | /* assume already zero-initialized */ |
| 7367 | SOKOL_ASSERT(desc); |
| 7368 | SOKOL_ASSERT(desc->mtl_device); |
| 7369 | SOKOL_ASSERT(desc->mtl_renderpass_descriptor_cb); |
| 7370 | SOKOL_ASSERT(desc->mtl_drawable_cb); |
| 7371 | SOKOL_ASSERT(desc->mtl_global_uniform_buffer_size > 0); |
| 7372 | _sg_mtl_init_pool(desc); |
| 7373 | _sg_mtl_init_sampler_cache(desc); |
| 7374 | _sg_mtl_clear_state_cache(); |
| 7375 | _sg.mtl.valid = true; |
| 7376 | _sg.mtl.renderpass_descriptor_cb = desc->mtl_renderpass_descriptor_cb; |
| 7377 | _sg.mtl.drawable_cb = desc->mtl_drawable_cb; |
| 7378 | _sg.mtl.frame_index = 1; |
| 7379 | _sg.mtl.ub_size = desc->mtl_global_uniform_buffer_size; |
| 7380 | _sg_mtl_sem = dispatch_semaphore_create(SG_NUM_INFLIGHT_FRAMES); |
| 7381 | _sg_mtl_device = (__bridge id<MTLDevice>) desc->mtl_device; |
| 7382 | _sg_mtl_cmd_queue = [_sg_mtl_device newCommandQueue]; |
| 7383 | MTLResourceOptions res_opts = MTLResourceCPUCacheModeWriteCombined; |
| 7384 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 7385 | res_opts |= MTLResourceStorageModeManaged; |
| 7386 | #endif |
| 7387 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 7388 | _sg_mtl_uniform_buffers[i] = [_sg_mtl_device |
| 7389 | newBufferWithLength:_sg.mtl.ub_size |
| 7390 | options:res_opts |
| 7391 | ]; |
| 7392 | } |
| 7393 | } |
| 7394 | |
| 7395 | _SOKOL_PRIVATE void _sg_discard_backend(void) { |
| 7396 | SOKOL_ASSERT(_sg.mtl.valid); |
| 7397 | /* wait for the last frame to finish */ |
| 7398 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 7399 | dispatch_semaphore_wait(_sg_mtl_sem, DISPATCH_TIME_FOREVER); |
| 7400 | } |
| 7401 | _sg_mtl_destroy_sampler_cache(_sg.mtl.frame_index); |
| 7402 | _sg_mtl_garbage_collect(_sg.mtl.frame_index + SG_NUM_INFLIGHT_FRAMES + 2); |
| 7403 | _sg_mtl_destroy_pool(); |
| 7404 | _sg.mtl.valid = false; |
| 7405 | _sg_mtl_cmd_encoder = nil; |
| 7406 | _sg_mtl_cmd_buffer = nil; |
| 7407 | _sg_mtl_cmd_queue = nil; |
| 7408 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 7409 | _sg_mtl_uniform_buffers[i] = nil; |
| 7410 | } |
| 7411 | _sg_mtl_device = nil; |
| 7412 | } |
| 7413 | |
| 7414 | _SOKOL_PRIVATE bool _sg_query_feature(sg_feature f) { |
| 7415 | switch (f) { |
| 7416 | case SG_FEATURE_INSTANCING: |
| 7417 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 7418 | case SG_FEATURE_TEXTURE_COMPRESSION_DXT: |
| 7419 | #else |
| 7420 | case SG_FEATURE_TEXTURE_COMPRESSION_PVRTC: |
| 7421 | case SG_FEATURE_TEXTURE_COMPRESSION_ETC2: |
| 7422 | #endif |
| 7423 | case SG_FEATURE_TEXTURE_FLOAT: |
| 7424 | case SG_FEATURE_ORIGIN_TOP_LEFT: |
| 7425 | case SG_FEATURE_MSAA_RENDER_TARGETS: |
| 7426 | case SG_FEATURE_PACKED_VERTEX_FORMAT_10_2: |
| 7427 | case SG_FEATURE_MULTIPLE_RENDER_TARGET: |
| 7428 | case SG_FEATURE_IMAGETYPE_3D: |
| 7429 | case SG_FEATURE_IMAGETYPE_ARRAY: |
| 7430 | return true; |
| 7431 | default: |
| 7432 | return false; |
| 7433 | } |
| 7434 | } |
| 7435 | |
| 7436 | _SOKOL_PRIVATE void _sg_reset_state_cache(void) { |
| 7437 | _sg_mtl_clear_state_cache(); |
| 7438 | } |
| 7439 | |
| 7440 | _SOKOL_PRIVATE sg_resource_state _sg_create_context(_sg_context_t* ctx) { |
| 7441 | SOKOL_ASSERT(ctx); |
| 7442 | _SOKOL_UNUSED(ctx); |
| 7443 | return SG_RESOURCESTATE_VALID; |
| 7444 | } |
| 7445 | |
| 7446 | _SOKOL_PRIVATE void _sg_destroy_context(_sg_context_t* ctx) { |
| 7447 | SOKOL_ASSERT(ctx); |
| 7448 | _SOKOL_UNUSED(ctx); |
| 7449 | /* empty */ |
| 7450 | } |
| 7451 | |
| 7452 | _SOKOL_PRIVATE void _sg_activate_context(_sg_context_t* ctx) { |
| 7453 | _sg_reset_state_cache(); |
| 7454 | } |
| 7455 | |
| 7456 | _SOKOL_PRIVATE sg_resource_state _sg_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) { |
| 7457 | SOKOL_ASSERT(buf && desc); |
| 7458 | buf->size = desc->size; |
| 7459 | buf->append_pos = 0; |
| 7460 | buf->append_overflow = false; |
| 7461 | buf->type = desc->type; |
| 7462 | buf->usage = desc->usage; |
| 7463 | buf->update_frame_index = 0; |
| 7464 | buf->append_frame_index = 0; |
| 7465 | buf->num_slots = (buf->usage == SG_USAGE_IMMUTABLE) ? 1 : SG_NUM_INFLIGHT_FRAMES; |
| 7466 | buf->active_slot = 0; |
| 7467 | const bool injected = (0 != desc->mtl_buffers[0]); |
| 7468 | MTLResourceOptions mtl_options = _sg_mtl_buffer_resource_options(buf->usage); |
| 7469 | for (int slot = 0; slot < buf->num_slots; slot++) { |
| 7470 | id<MTLBuffer> mtl_buf; |
| 7471 | if (injected) { |
| 7472 | SOKOL_ASSERT(desc->mtl_buffers[slot]); |
| 7473 | mtl_buf = (__bridge id<MTLBuffer>) desc->mtl_buffers[slot]; |
| 7474 | } |
| 7475 | else { |
| 7476 | if (buf->usage == SG_USAGE_IMMUTABLE) { |
| 7477 | SOKOL_ASSERT(desc->content); |
| 7478 | mtl_buf = [_sg_mtl_device newBufferWithBytes:desc->content length:buf->size options:mtl_options]; |
| 7479 | } |
| 7480 | else { |
| 7481 | mtl_buf = [_sg_mtl_device newBufferWithLength:buf->size options:mtl_options]; |
| 7482 | } |
| 7483 | } |
| 7484 | buf->mtl_buf[slot] = _sg_mtl_add_resource(mtl_buf); |
| 7485 | } |
| 7486 | return SG_RESOURCESTATE_VALID; |
| 7487 | } |
| 7488 | |
| 7489 | _SOKOL_PRIVATE void _sg_destroy_buffer(_sg_buffer_t* buf) { |
| 7490 | SOKOL_ASSERT(buf); |
| 7491 | for (int slot = 0; slot < buf->num_slots; slot++) { |
| 7492 | /* it's valid to call release resource with '0' */ |
| 7493 | _sg_mtl_release_resource(_sg.mtl.frame_index, buf->mtl_buf[slot]); |
| 7494 | } |
| 7495 | } |
| 7496 | |
| 7497 | _SOKOL_PRIVATE void _sg_mtl_copy_image_content(const _sg_image_t* img, __unsafe_unretained id<MTLTexture> mtl_tex, const sg_image_content* content) { |
| 7498 | const int num_faces = (img->type == SG_IMAGETYPE_CUBE) ? 6:1; |
| 7499 | const int num_slices = (img->type == SG_IMAGETYPE_ARRAY) ? img->depth : 1; |
| 7500 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 7501 | for (int mip_index = 0; mip_index < img->num_mipmaps; mip_index++) { |
| 7502 | SOKOL_ASSERT(content->subimage[face_index][mip_index].ptr); |
| 7503 | SOKOL_ASSERT(content->subimage[face_index][mip_index].size > 0); |
| 7504 | const uint8_t* data_ptr = (const uint8_t*)content->subimage[face_index][mip_index].ptr; |
| 7505 | const int mip_width = _sg_max(img->width >> mip_index, 1); |
| 7506 | const int mip_height = _sg_max(img->height >> mip_index, 1); |
| 7507 | /* special case PVRTC formats: bytePerRow must be 0 */ |
| 7508 | int bytes_per_row = 0; |
| 7509 | int bytes_per_slice = _sg_surface_pitch(img->pixel_format, mip_width, mip_height); |
| 7510 | if (!_sg_mtl_is_pvrtc(img->pixel_format)) { |
| 7511 | bytes_per_row = _sg_row_pitch(img->pixel_format, mip_width); |
| 7512 | } |
| 7513 | MTLRegion region; |
| 7514 | if (img->type == SG_IMAGETYPE_3D) { |
| 7515 | const int mip_depth = _sg_max(img->depth >> mip_index, 1); |
| 7516 | region = MTLRegionMake3D(0, 0, 0, mip_width, mip_height, mip_depth); |
| 7517 | /* FIXME: apparently the minimal bytes_per_image size for 3D texture |
| 7518 | is 4 KByte... somehow need to handle this */ |
| 7519 | } |
| 7520 | else { |
| 7521 | region = MTLRegionMake2D(0, 0, mip_width, mip_height); |
| 7522 | } |
| 7523 | for (int slice_index = 0; slice_index < num_slices; slice_index++) { |
| 7524 | const int mtl_slice_index = (img->type == SG_IMAGETYPE_CUBE) ? face_index : slice_index; |
| 7525 | const int slice_offset = slice_index * bytes_per_slice; |
| 7526 | SOKOL_ASSERT((slice_offset + bytes_per_slice) <= (int)content->subimage[face_index][mip_index].size); |
| 7527 | [mtl_tex replaceRegion:region |
| 7528 | mipmapLevel:mip_index |
| 7529 | slice:mtl_slice_index |
| 7530 | withBytes:data_ptr + slice_offset |
| 7531 | bytesPerRow:bytes_per_row |
| 7532 | bytesPerImage:bytes_per_slice]; |
| 7533 | } |
| 7534 | } |
| 7535 | } |
| 7536 | } |
| 7537 | |
| 7538 | /* |
| 7539 | FIXME: METAL RESOURCE STORAGE MODE FOR macOS AND iOS |
| 7540 | |
| 7541 | For immutable textures on macOS, the recommended procedure is to create |
| 7542 | a MTLStorageModeManaged texture with the immutable content first, |
| 7543 | and then use the GPU to blit the content into a MTLStorageModePrivate |
| 7544 | texture before the first use. |
| 7545 | |
| 7546 | On iOS use the same one-time-blit procedure, but from a |
| 7547 | MTLStorageModeShared to a MTLStorageModePrivate texture. |
| 7548 | |
| 7549 | It probably makes sense to handle this in a separate 'resource manager' |
| 7550 | with a recycable pool of blit-source-textures? |
| 7551 | */ |
| 7552 | |
| 7553 | /* initialize MTLTextureDescritor with common attributes */ |
| 7554 | _SOKOL_PRIVATE bool _sg_mtl_init_texdesc_common(MTLTextureDescriptor* mtl_desc, _sg_image_t* img) { |
| 7555 | mtl_desc.textureType = _sg_mtl_texture_type(img->type); |
| 7556 | if (img->render_target) { |
| 7557 | if (_sg_is_valid_rendertarget_color_format(img->pixel_format)) { |
| 7558 | mtl_desc.pixelFormat = _sg_mtl_rendertarget_color_format(img->pixel_format); |
| 7559 | } |
| 7560 | else { |
| 7561 | mtl_desc.pixelFormat = _sg_mtl_rendertarget_depth_format(img->pixel_format); |
| 7562 | } |
| 7563 | } |
| 7564 | else { |
| 7565 | mtl_desc.pixelFormat = _sg_mtl_texture_format(img->pixel_format); |
| 7566 | } |
| 7567 | if (MTLPixelFormatInvalid == mtl_desc.pixelFormat) { |
| 7568 | SOKOL_LOG("Unsupported texture pixel format!\n"); |
| 7569 | return false; |
| 7570 | } |
| 7571 | mtl_desc.width = img->width; |
| 7572 | mtl_desc.height = img->height; |
| 7573 | if (SG_IMAGETYPE_3D == img->type) { |
| 7574 | mtl_desc.depth = img->depth; |
| 7575 | } |
| 7576 | else { |
| 7577 | mtl_desc.depth = 1; |
| 7578 | } |
| 7579 | mtl_desc.mipmapLevelCount = img->num_mipmaps; |
| 7580 | if (SG_IMAGETYPE_ARRAY == img->type) { |
| 7581 | mtl_desc.arrayLength = img->depth; |
| 7582 | } |
| 7583 | else { |
| 7584 | mtl_desc.arrayLength = 1; |
| 7585 | } |
| 7586 | mtl_desc.usage = MTLTextureUsageShaderRead; |
| 7587 | if (img->usage != SG_USAGE_IMMUTABLE) { |
| 7588 | mtl_desc.cpuCacheMode = MTLCPUCacheModeWriteCombined; |
| 7589 | } |
| 7590 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 7591 | /* macOS: use managed textures */ |
| 7592 | mtl_desc.resourceOptions = MTLResourceStorageModeManaged; |
| 7593 | mtl_desc.storageMode = MTLStorageModeManaged; |
| 7594 | #else |
| 7595 | /* iOS: use CPU/GPU shared memory */ |
| 7596 | mtl_desc.resourceOptions = MTLResourceStorageModeShared; |
| 7597 | mtl_desc.storageMode = MTLStorageModeShared; |
| 7598 | #endif |
| 7599 | return true; |
| 7600 | } |
| 7601 | |
| 7602 | /* initialize MTLTextureDescritor with rendertarget attributes */ |
| 7603 | _SOKOL_PRIVATE void _sg_mtl_init_texdesc_rt(MTLTextureDescriptor* mtl_desc, _sg_image_t* img) { |
| 7604 | SOKOL_ASSERT(img->render_target); |
| 7605 | /* reset the cpuCacheMode to 'default' */ |
| 7606 | mtl_desc.cpuCacheMode = MTLCPUCacheModeDefaultCache; |
| 7607 | /* render targets are only visible to the GPU */ |
| 7608 | mtl_desc.resourceOptions = MTLResourceStorageModePrivate; |
| 7609 | mtl_desc.storageMode = MTLStorageModePrivate; |
| 7610 | /* non-MSAA render targets are shader-readable */ |
| 7611 | mtl_desc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget; |
| 7612 | } |
| 7613 | |
| 7614 | /* initialize MTLTextureDescritor with MSAA attributes */ |
| 7615 | _SOKOL_PRIVATE void _sg_mtl_init_texdesc_rt_msaa(MTLTextureDescriptor* mtl_desc, _sg_image_t* img) { |
| 7616 | SOKOL_ASSERT(img->sample_count > 1); |
| 7617 | /* reset the cpuCacheMode to 'default' */ |
| 7618 | mtl_desc.cpuCacheMode = MTLCPUCacheModeDefaultCache; |
| 7619 | /* render targets are only visible to the GPU */ |
| 7620 | mtl_desc.resourceOptions = MTLResourceStorageModePrivate; |
| 7621 | mtl_desc.storageMode = MTLStorageModePrivate; |
| 7622 | /* MSAA render targets are not shader-readable (instead they are resolved) */ |
| 7623 | mtl_desc.usage = MTLTextureUsageRenderTarget; |
| 7624 | mtl_desc.textureType = MTLTextureType2DMultisample; |
| 7625 | mtl_desc.depth = 1; |
| 7626 | mtl_desc.arrayLength = 1; |
| 7627 | mtl_desc.mipmapLevelCount = 1; |
| 7628 | mtl_desc.sampleCount = img->sample_count; |
| 7629 | } |
| 7630 | |
| 7631 | _SOKOL_PRIVATE sg_resource_state _sg_create_image(_sg_image_t* img, const sg_image_desc* desc) { |
| 7632 | SOKOL_ASSERT(img && desc); |
| 7633 | img->type = desc->type; |
| 7634 | img->render_target = desc->render_target; |
| 7635 | img->width = desc->width; |
| 7636 | img->height = desc->height; |
| 7637 | img->depth = desc->depth; |
| 7638 | img->num_mipmaps = desc->num_mipmaps; |
| 7639 | img->usage = desc->usage; |
| 7640 | img->pixel_format = desc->pixel_format; |
| 7641 | img->sample_count = desc->sample_count; |
| 7642 | img->min_filter = desc->min_filter; |
| 7643 | img->mag_filter = desc->mag_filter; |
| 7644 | img->wrap_u = desc->wrap_u; |
| 7645 | img->wrap_v = desc->wrap_v; |
| 7646 | img->wrap_w = desc->wrap_w; |
| 7647 | img->max_anisotropy = desc->max_anisotropy; |
| 7648 | img->upd_frame_index = 0; |
| 7649 | img->num_slots = (img->usage == SG_USAGE_IMMUTABLE) ? 1 :SG_NUM_INFLIGHT_FRAMES; |
| 7650 | img->active_slot = 0; |
| 7651 | const bool injected = (0 != desc->mtl_textures[0]); |
| 7652 | const bool msaa = (img->sample_count > 1); |
| 7653 | |
| 7654 | /* first initialize all Metal resource pool slots to 'empty' */ |
| 7655 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 7656 | img->mtl_tex[i] = _sg_mtl_add_resource(nil); |
| 7657 | } |
| 7658 | img->mtl_sampler_state = _sg_mtl_add_resource(nil); |
| 7659 | img->mtl_depth_tex = _sg_mtl_add_resource(nil); |
| 7660 | img->mtl_msaa_tex = _sg_mtl_add_resource(nil); |
| 7661 | |
| 7662 | /* initialize a Metal texture descriptor with common attributes */ |
| 7663 | MTLTextureDescriptor* mtl_desc = [[MTLTextureDescriptor alloc] init]; |
| 7664 | if (!_sg_mtl_init_texdesc_common(mtl_desc, img)) { |
| 7665 | return SG_RESOURCESTATE_FAILED; |
| 7666 | } |
| 7667 | |
| 7668 | /* special case depth-stencil-buffer? */ |
| 7669 | if (_sg_is_valid_rendertarget_depth_format(img->pixel_format)) { |
| 7670 | /* depth-stencil buffer texture must always be a render target */ |
| 7671 | SOKOL_ASSERT(img->render_target); |
| 7672 | SOKOL_ASSERT(img->type == SG_IMAGETYPE_2D); |
| 7673 | SOKOL_ASSERT(img->num_mipmaps == 1); |
| 7674 | SOKOL_ASSERT(!injected); |
| 7675 | if (msaa) { |
| 7676 | _sg_mtl_init_texdesc_rt_msaa(mtl_desc, img); |
| 7677 | } |
| 7678 | else { |
| 7679 | _sg_mtl_init_texdesc_rt(mtl_desc, img); |
| 7680 | } |
| 7681 | id<MTLTexture> tex = [_sg_mtl_device newTextureWithDescriptor:mtl_desc]; |
| 7682 | SOKOL_ASSERT(nil != tex); |
| 7683 | img->mtl_depth_tex = _sg_mtl_add_resource(tex); |
| 7684 | } |
| 7685 | else { |
| 7686 | /* create the color texture |
| 7687 | In case this is a render target without MSAA, add the relevant |
| 7688 | render-target descriptor attributes. |
| 7689 | In case this is a render target *with* MSAA, the color texture |
| 7690 | will serve as MSAA-resolve target (not as render target), and rendering |
| 7691 | will go into a separate render target texture of type |
| 7692 | MTLTextureType2DMultisample. |
| 7693 | */ |
| 7694 | if (img->render_target && !msaa) { |
| 7695 | _sg_mtl_init_texdesc_rt(mtl_desc, img); |
| 7696 | } |
| 7697 | for (int slot = 0; slot < img->num_slots; slot++) { |
| 7698 | id<MTLTexture> tex; |
| 7699 | if (injected) { |
| 7700 | SOKOL_ASSERT(desc->mtl_textures[slot]); |
| 7701 | tex = (__bridge id<MTLTexture>) desc->mtl_textures[slot]; |
| 7702 | } |
| 7703 | else { |
| 7704 | tex = [_sg_mtl_device newTextureWithDescriptor:mtl_desc]; |
| 7705 | if ((img->usage == SG_USAGE_IMMUTABLE) && !img->render_target) { |
| 7706 | _sg_mtl_copy_image_content(img, tex, &desc->content); |
| 7707 | } |
| 7708 | } |
| 7709 | img->mtl_tex[slot] = _sg_mtl_add_resource(tex); |
| 7710 | } |
| 7711 | |
| 7712 | /* if MSAA color render target, create an additional MSAA render-surface texture */ |
| 7713 | if (img->render_target && msaa) { |
| 7714 | _sg_mtl_init_texdesc_rt_msaa(mtl_desc, img); |
| 7715 | id<MTLTexture> tex = [_sg_mtl_device newTextureWithDescriptor:mtl_desc]; |
| 7716 | img->mtl_msaa_tex = _sg_mtl_add_resource(tex); |
| 7717 | } |
| 7718 | |
| 7719 | /* create (possibly shared) sampler state */ |
| 7720 | img->mtl_sampler_state = _sg_mtl_create_sampler(_sg_mtl_device, desc); |
| 7721 | } |
| 7722 | return SG_RESOURCESTATE_VALID; |
| 7723 | } |
| 7724 | |
| 7725 | _SOKOL_PRIVATE void _sg_destroy_image(_sg_image_t* img) { |
| 7726 | SOKOL_ASSERT(img); |
| 7727 | /* it's valid to call release resource with a 'null resource' */ |
| 7728 | for (int slot = 0; slot < img->num_slots; slot++) { |
| 7729 | _sg_mtl_release_resource(_sg.mtl.frame_index, img->mtl_tex[slot]); |
| 7730 | } |
| 7731 | _sg_mtl_release_resource(_sg.mtl.frame_index, img->mtl_depth_tex); |
| 7732 | _sg_mtl_release_resource(_sg.mtl.frame_index, img->mtl_msaa_tex); |
| 7733 | /* NOTE: sampler state objects are shared and not released until shutdown */ |
| 7734 | } |
| 7735 | |
| 7736 | _SOKOL_PRIVATE id<MTLLibrary> _sg_mtl_compile_library(const char* src) { |
| 7737 | NSError* err = NULL; |
| 7738 | id<MTLLibrary> lib = [_sg_mtl_device |
| 7739 | newLibraryWithSource:[NSString stringWithUTF8String:src] |
| 7740 | options:nil |
| 7741 | error:&err |
| 7742 | ]; |
| 7743 | if (err) { |
| 7744 | SOKOL_LOG([err.localizedDescription UTF8String]); |
| 7745 | } |
| 7746 | return lib; |
| 7747 | } |
| 7748 | |
| 7749 | _SOKOL_PRIVATE id<MTLLibrary> _sg_mtl_library_from_bytecode(const uint8_t* ptr, int num_bytes) { |
| 7750 | NSError* err = NULL; |
| 7751 | dispatch_data_t lib_data = dispatch_data_create(ptr, num_bytes, NULL, DISPATCH_DATA_DESTRUCTOR_DEFAULT); |
| 7752 | id<MTLLibrary> lib = [_sg_mtl_device newLibraryWithData:lib_data error:&err]; |
| 7753 | if (err) { |
| 7754 | SOKOL_LOG([err.localizedDescription UTF8String]); |
| 7755 | } |
| 7756 | return lib; |
| 7757 | } |
| 7758 | |
| 7759 | _SOKOL_PRIVATE sg_resource_state _sg_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) { |
| 7760 | SOKOL_ASSERT(shd && desc); |
| 7761 | |
| 7762 | /* uniform block sizes and image types */ |
| 7763 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 7764 | const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS) ? &desc->vs : &desc->fs; |
| 7765 | _sg_shader_stage_t* stage = &shd->stage[stage_index]; |
| 7766 | SOKOL_ASSERT(stage->num_uniform_blocks == 0); |
| 7767 | for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) { |
| 7768 | const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index]; |
| 7769 | if (0 == ub_desc->size) { |
| 7770 | break; |
| 7771 | } |
| 7772 | _sg_uniform_block_t* ub = &stage->uniform_blocks[ub_index]; |
| 7773 | ub->size = ub_desc->size; |
| 7774 | stage->num_uniform_blocks++; |
| 7775 | } |
| 7776 | SOKOL_ASSERT(stage->num_images == 0); |
| 7777 | for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) { |
| 7778 | const sg_shader_image_desc* img_desc = &stage_desc->images[img_index]; |
| 7779 | if (img_desc->type == _SG_IMAGETYPE_DEFAULT) { |
| 7780 | break; |
| 7781 | } |
| 7782 | stage->images[img_index].type = img_desc->type; |
| 7783 | stage->num_images++; |
| 7784 | } |
| 7785 | } |
| 7786 | |
| 7787 | /* create metal libray objects and lookup entry functions */ |
| 7788 | id<MTLLibrary> vs_lib; |
| 7789 | id<MTLLibrary> fs_lib; |
| 7790 | id<MTLFunction> vs_func; |
| 7791 | id<MTLFunction> fs_func; |
| 7792 | const char* vs_entry = desc->vs.entry; |
| 7793 | const char* fs_entry = desc->fs.entry; |
| 7794 | if (desc->vs.byte_code && desc->fs.byte_code) { |
| 7795 | /* separate byte code provided */ |
| 7796 | vs_lib = _sg_mtl_library_from_bytecode(desc->vs.byte_code, desc->vs.byte_code_size); |
| 7797 | fs_lib = _sg_mtl_library_from_bytecode(desc->fs.byte_code, desc->fs.byte_code_size); |
| 7798 | if (nil == vs_lib || nil == fs_lib) { |
| 7799 | return SG_RESOURCESTATE_FAILED; |
| 7800 | } |
| 7801 | vs_func = [vs_lib newFunctionWithName:[NSString stringWithUTF8String:vs_entry]]; |
| 7802 | fs_func = [fs_lib newFunctionWithName:[NSString stringWithUTF8String:fs_entry]]; |
| 7803 | } |
| 7804 | else if (desc->vs.source && desc->fs.source) { |
| 7805 | /* separate sources provided */ |
| 7806 | vs_lib = _sg_mtl_compile_library(desc->vs.source); |
| 7807 | fs_lib = _sg_mtl_compile_library(desc->fs.source); |
| 7808 | if (nil == vs_lib || nil == fs_lib) { |
| 7809 | return SG_RESOURCESTATE_FAILED; |
| 7810 | } |
| 7811 | vs_func = [vs_lib newFunctionWithName:[NSString stringWithUTF8String:vs_entry]]; |
| 7812 | fs_func = [fs_lib newFunctionWithName:[NSString stringWithUTF8String:fs_entry]]; |
| 7813 | } |
| 7814 | else { |
| 7815 | return SG_RESOURCESTATE_FAILED; |
| 7816 | } |
| 7817 | if (nil == vs_func) { |
| 7818 | SOKOL_LOG("vertex shader entry function not found\n"); |
| 7819 | return SG_RESOURCESTATE_FAILED; |
| 7820 | } |
| 7821 | if (nil == fs_func) { |
| 7822 | SOKOL_LOG("fragment shader entry function not found\n"); |
| 7823 | return SG_RESOURCESTATE_FAILED; |
| 7824 | } |
| 7825 | /* it is legal to call _sg_mtl_add_resource with a nil value, this will return a special 0xFFFFFFFF index */ |
| 7826 | shd->stage[SG_SHADERSTAGE_VS].mtl_lib = _sg_mtl_add_resource(vs_lib); |
| 7827 | shd->stage[SG_SHADERSTAGE_FS].mtl_lib = _sg_mtl_add_resource(fs_lib); |
| 7828 | shd->stage[SG_SHADERSTAGE_VS].mtl_func = _sg_mtl_add_resource(vs_func); |
| 7829 | shd->stage[SG_SHADERSTAGE_FS].mtl_func = _sg_mtl_add_resource(fs_func); |
| 7830 | return SG_RESOURCESTATE_VALID; |
| 7831 | } |
| 7832 | |
| 7833 | _SOKOL_PRIVATE void _sg_destroy_shader(_sg_shader_t* shd) { |
| 7834 | SOKOL_ASSERT(shd); |
| 7835 | /* it is valid to call _sg_mtl_release_resource with a 'null resource' */ |
| 7836 | _sg_mtl_release_resource(_sg.mtl.frame_index, shd->stage[SG_SHADERSTAGE_VS].mtl_func); |
| 7837 | _sg_mtl_release_resource(_sg.mtl.frame_index, shd->stage[SG_SHADERSTAGE_VS].mtl_lib); |
| 7838 | _sg_mtl_release_resource(_sg.mtl.frame_index, shd->stage[SG_SHADERSTAGE_FS].mtl_func); |
| 7839 | _sg_mtl_release_resource(_sg.mtl.frame_index, shd->stage[SG_SHADERSTAGE_FS].mtl_lib); |
| 7840 | } |
| 7841 | |
| 7842 | _SOKOL_PRIVATE sg_resource_state _sg_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) { |
| 7843 | SOKOL_ASSERT(pip && shd && desc); |
| 7844 | SOKOL_ASSERT(desc->shader.id == shd->slot.id); |
| 7845 | |
| 7846 | pip->shader = shd; |
| 7847 | pip->shader_id = desc->shader; |
| 7848 | pip->color_attachment_count = desc->blend.color_attachment_count; |
| 7849 | pip->color_format = desc->blend.color_format; |
| 7850 | pip->depth_format = desc->blend.depth_format; |
| 7851 | pip->sample_count = desc->rasterizer.sample_count; |
| 7852 | pip->depth_bias = desc->rasterizer.depth_bias; |
| 7853 | pip->depth_bias_slope_scale = desc->rasterizer.depth_bias_slope_scale; |
| 7854 | pip->depth_bias_clamp = desc->rasterizer.depth_bias_clamp; |
| 7855 | sg_primitive_type prim_type = desc->primitive_type; |
| 7856 | pip->mtl_prim_type = _sg_mtl_primitive_type(prim_type); |
| 7857 | pip->index_type = desc->index_type; |
| 7858 | pip->mtl_index_size = _sg_mtl_index_size(pip->index_type); |
| 7859 | if (SG_INDEXTYPE_NONE != pip->index_type) { |
| 7860 | pip->mtl_index_type = _sg_mtl_index_type(pip->index_type); |
| 7861 | } |
| 7862 | pip->mtl_cull_mode = _sg_mtl_cull_mode(desc->rasterizer.cull_mode); |
| 7863 | pip->mtl_winding = _sg_mtl_winding(desc->rasterizer.face_winding); |
| 7864 | pip->mtl_stencil_ref = desc->depth_stencil.stencil_ref; |
| 7865 | for (int i = 0; i < 4; i++) { |
| 7866 | pip->blend_color[i] = desc->blend.blend_color[i]; |
| 7867 | } |
| 7868 | |
| 7869 | /* create vertex-descriptor */ |
| 7870 | MTLVertexDescriptor* vtx_desc = [MTLVertexDescriptor vertexDescriptor]; |
| 7871 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 7872 | const sg_vertex_attr_desc* a_desc = &desc->layout.attrs[attr_index]; |
| 7873 | if (a_desc->format == SG_VERTEXFORMAT_INVALID) { |
| 7874 | break; |
| 7875 | } |
| 7876 | SOKOL_ASSERT((a_desc->buffer_index >= 0) && (a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS)); |
| 7877 | vtx_desc.attributes[attr_index].format = _sg_mtl_vertex_format(a_desc->format); |
| 7878 | vtx_desc.attributes[attr_index].offset = a_desc->offset; |
| 7879 | vtx_desc.attributes[attr_index].bufferIndex = a_desc->buffer_index + SG_MAX_SHADERSTAGE_UBS; |
| 7880 | pip->vertex_layout_valid[a_desc->buffer_index] = true; |
| 7881 | } |
| 7882 | for (int layout_index = 0; layout_index < SG_MAX_SHADERSTAGE_BUFFERS; layout_index++) { |
| 7883 | if (pip->vertex_layout_valid[layout_index]) { |
| 7884 | const sg_buffer_layout_desc* l_desc = &desc->layout.buffers[layout_index]; |
| 7885 | const int mtl_vb_slot = layout_index + SG_MAX_SHADERSTAGE_UBS; |
| 7886 | SOKOL_ASSERT(l_desc->stride > 0); |
| 7887 | vtx_desc.layouts[mtl_vb_slot].stride = l_desc->stride; |
| 7888 | vtx_desc.layouts[mtl_vb_slot].stepFunction = _sg_mtl_step_function(l_desc->step_func); |
| 7889 | vtx_desc.layouts[mtl_vb_slot].stepRate = l_desc->step_rate; |
| 7890 | } |
| 7891 | } |
| 7892 | |
| 7893 | /* render-pipeline descriptor */ |
| 7894 | MTLRenderPipelineDescriptor* rp_desc = [[MTLRenderPipelineDescriptor alloc] init]; |
| 7895 | rp_desc.vertexDescriptor = vtx_desc; |
| 7896 | SOKOL_ASSERT(shd->stage[SG_SHADERSTAGE_VS].mtl_func != _SG_MTL_INVALID_SLOT_INDEX); |
| 7897 | rp_desc.vertexFunction = _sg_mtl_idpool[shd->stage[SG_SHADERSTAGE_VS].mtl_func]; |
| 7898 | SOKOL_ASSERT(shd->stage[SG_SHADERSTAGE_FS].mtl_func != _SG_MTL_INVALID_SLOT_INDEX); |
| 7899 | rp_desc.fragmentFunction = _sg_mtl_idpool[shd->stage[SG_SHADERSTAGE_FS].mtl_func]; |
| 7900 | rp_desc.sampleCount = desc->rasterizer.sample_count; |
| 7901 | rp_desc.alphaToCoverageEnabled = desc->rasterizer.alpha_to_coverage_enabled; |
| 7902 | rp_desc.alphaToOneEnabled = NO; |
| 7903 | rp_desc.rasterizationEnabled = YES; |
| 7904 | rp_desc.depthAttachmentPixelFormat = _sg_mtl_rendertarget_depth_format(desc->blend.depth_format); |
| 7905 | rp_desc.stencilAttachmentPixelFormat = _sg_mtl_rendertarget_stencil_format(desc->blend.depth_format); |
| 7906 | /* FIXME: this only works on macOS 10.13! |
| 7907 | for (int i = 0; i < (SG_MAX_SHADERSTAGE_UBS+SG_MAX_SHADERSTAGE_BUFFERS); i++) { |
| 7908 | rp_desc.vertexBuffers[i].mutability = MTLMutabilityImmutable; |
| 7909 | } |
| 7910 | for (int i = 0; i < SG_MAX_SHADERSTAGE_UBS; i++) { |
| 7911 | rp_desc.fragmentBuffers[i].mutability = MTLMutabilityImmutable; |
| 7912 | } |
| 7913 | */ |
| 7914 | const int att_count = desc->blend.color_attachment_count; |
| 7915 | for (int i = 0; i < att_count; i++) { |
| 7916 | rp_desc.colorAttachments[i].pixelFormat = _sg_mtl_rendertarget_color_format(desc->blend.color_format); |
| 7917 | rp_desc.colorAttachments[i].writeMask = _sg_mtl_color_write_mask((sg_color_mask)desc->blend.color_write_mask); |
| 7918 | rp_desc.colorAttachments[i].blendingEnabled = desc->blend.enabled; |
| 7919 | rp_desc.colorAttachments[i].alphaBlendOperation = _sg_mtl_blend_op(desc->blend.op_alpha); |
| 7920 | rp_desc.colorAttachments[i].rgbBlendOperation = _sg_mtl_blend_op(desc->blend.op_rgb); |
| 7921 | rp_desc.colorAttachments[i].destinationAlphaBlendFactor = _sg_mtl_blend_factor(desc->blend.dst_factor_alpha); |
| 7922 | rp_desc.colorAttachments[i].destinationRGBBlendFactor = _sg_mtl_blend_factor(desc->blend.dst_factor_rgb); |
| 7923 | rp_desc.colorAttachments[i].sourceAlphaBlendFactor = _sg_mtl_blend_factor(desc->blend.src_factor_alpha); |
| 7924 | rp_desc.colorAttachments[i].sourceRGBBlendFactor = _sg_mtl_blend_factor(desc->blend.src_factor_rgb); |
| 7925 | } |
| 7926 | NSError* err = NULL; |
| 7927 | id<MTLRenderPipelineState> mtl_rps = [_sg_mtl_device newRenderPipelineStateWithDescriptor:rp_desc error:&err]; |
| 7928 | if (nil == mtl_rps) { |
| 7929 | SOKOL_ASSERT(err); |
| 7930 | SOKOL_LOG([err.localizedDescription UTF8String]); |
| 7931 | return SG_RESOURCESTATE_FAILED; |
| 7932 | } |
| 7933 | |
| 7934 | /* depth-stencil-state */ |
| 7935 | MTLDepthStencilDescriptor* ds_desc = [[MTLDepthStencilDescriptor alloc] init]; |
| 7936 | ds_desc.depthCompareFunction = _sg_mtl_compare_func(desc->depth_stencil.depth_compare_func); |
| 7937 | ds_desc.depthWriteEnabled = desc->depth_stencil.depth_write_enabled; |
| 7938 | if (desc->depth_stencil.stencil_enabled) { |
| 7939 | const sg_stencil_state* sb = &desc->depth_stencil.stencil_back; |
| 7940 | ds_desc.backFaceStencil = [[MTLStencilDescriptor alloc] init]; |
| 7941 | ds_desc.backFaceStencil.stencilFailureOperation = _sg_mtl_stencil_op(sb->fail_op); |
| 7942 | ds_desc.backFaceStencil.depthFailureOperation = _sg_mtl_stencil_op(sb->depth_fail_op); |
| 7943 | ds_desc.backFaceStencil.depthStencilPassOperation = _sg_mtl_stencil_op(sb->pass_op); |
| 7944 | ds_desc.backFaceStencil.stencilCompareFunction = _sg_mtl_compare_func(sb->compare_func); |
| 7945 | ds_desc.backFaceStencil.readMask = desc->depth_stencil.stencil_read_mask; |
| 7946 | ds_desc.backFaceStencil.writeMask = desc->depth_stencil.stencil_write_mask; |
| 7947 | const sg_stencil_state* sf = &desc->depth_stencil.stencil_front; |
| 7948 | ds_desc.frontFaceStencil = [[MTLStencilDescriptor alloc] init]; |
| 7949 | ds_desc.frontFaceStencil.stencilFailureOperation = _sg_mtl_stencil_op(sf->fail_op); |
| 7950 | ds_desc.frontFaceStencil.depthFailureOperation = _sg_mtl_stencil_op(sf->depth_fail_op); |
| 7951 | ds_desc.frontFaceStencil.depthStencilPassOperation = _sg_mtl_stencil_op(sf->pass_op); |
| 7952 | ds_desc.frontFaceStencil.stencilCompareFunction = _sg_mtl_compare_func(sf->compare_func); |
| 7953 | ds_desc.frontFaceStencil.readMask = desc->depth_stencil.stencil_read_mask; |
| 7954 | ds_desc.frontFaceStencil.writeMask = desc->depth_stencil.stencil_write_mask; |
| 7955 | } |
| 7956 | id<MTLDepthStencilState> mtl_dss = [_sg_mtl_device newDepthStencilStateWithDescriptor:ds_desc]; |
| 7957 | |
| 7958 | pip->mtl_rps = _sg_mtl_add_resource(mtl_rps); |
| 7959 | pip->mtl_dss = _sg_mtl_add_resource(mtl_dss); |
| 7960 | return SG_RESOURCESTATE_VALID; |
| 7961 | } |
| 7962 | |
| 7963 | _SOKOL_PRIVATE void _sg_destroy_pipeline(_sg_pipeline_t* pip) { |
| 7964 | SOKOL_ASSERT(pip); |
| 7965 | /* it's valid to call release resource with a 'null resource' */ |
| 7966 | _sg_mtl_release_resource(_sg.mtl.frame_index, pip->mtl_rps); |
| 7967 | _sg_mtl_release_resource(_sg.mtl.frame_index, pip->mtl_dss); |
| 7968 | } |
| 7969 | |
| 7970 | _SOKOL_PRIVATE sg_resource_state _sg_create_pass(_sg_pass_t* pass, _sg_image_t** att_images, const sg_pass_desc* desc) { |
| 7971 | SOKOL_ASSERT(pass && desc); |
| 7972 | SOKOL_ASSERT(att_images && att_images[0]); |
| 7973 | |
| 7974 | /* copy image pointers and desc attributes */ |
| 7975 | const sg_attachment_desc* att_desc; |
| 7976 | _sg_attachment_t* att; |
| 7977 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 7978 | SOKOL_ASSERT(0 == pass->color_atts[i].image); |
| 7979 | att_desc = &desc->color_attachments[i]; |
| 7980 | if (att_desc->image.id != SG_INVALID_ID) { |
| 7981 | pass->num_color_atts++; |
| 7982 | SOKOL_ASSERT(att_images[i] && (att_images[i]->slot.id == att_desc->image.id)); |
| 7983 | SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(att_images[i]->pixel_format)); |
| 7984 | att = &pass->color_atts[i]; |
| 7985 | SOKOL_ASSERT((att->image == 0) && (att->image_id.id == SG_INVALID_ID)); |
| 7986 | att->image = att_images[i]; |
| 7987 | att->image_id = att_desc->image; |
| 7988 | att->mip_level = att_desc->mip_level; |
| 7989 | att->slice = att_desc->slice; |
| 7990 | } |
| 7991 | } |
| 7992 | SOKOL_ASSERT(0 == pass->ds_att.image); |
| 7993 | att_desc = &desc->depth_stencil_attachment; |
| 7994 | const int ds_img_index = SG_MAX_COLOR_ATTACHMENTS; |
| 7995 | if (att_desc->image.id != SG_INVALID_ID) { |
| 7996 | SOKOL_ASSERT(att_images[ds_img_index] && (att_images[ds_img_index]->slot.id == att_desc->image.id)); |
| 7997 | SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(att_images[ds_img_index]->pixel_format)); |
| 7998 | att = &pass->ds_att; |
| 7999 | SOKOL_ASSERT((att->image == 0) && (att->image_id.id == SG_INVALID_ID)); |
| 8000 | att->image = att_images[ds_img_index]; |
| 8001 | att->image_id = att_desc->image; |
| 8002 | att->mip_level = att_desc->mip_level; |
| 8003 | att->slice = att_desc->slice; |
| 8004 | } |
| 8005 | return SG_RESOURCESTATE_VALID; |
| 8006 | } |
| 8007 | |
| 8008 | _SOKOL_PRIVATE void _sg_destroy_pass(_sg_pass_t* pass) { |
| 8009 | SOKOL_ASSERT(pass); |
| 8010 | _SOKOL_UNUSED(pass); |
| 8011 | } |
| 8012 | |
| 8013 | _SOKOL_PRIVATE void _sg_begin_pass(_sg_pass_t* pass, const sg_pass_action* action, int w, int h) { |
| 8014 | SOKOL_ASSERT(action); |
| 8015 | SOKOL_ASSERT(!_sg.mtl.in_pass); |
| 8016 | SOKOL_ASSERT(_sg_mtl_cmd_queue); |
| 8017 | SOKOL_ASSERT(!_sg_mtl_cmd_encoder); |
| 8018 | SOKOL_ASSERT(_sg.mtl.renderpass_descriptor_cb); |
| 8019 | _sg.mtl.in_pass = true; |
| 8020 | _sg.mtl.cur_width = w; |
| 8021 | _sg.mtl.cur_height = h; |
| 8022 | _sg_mtl_clear_state_cache(); |
| 8023 | |
| 8024 | /* if this is the first pass in the frame, create a command buffer */ |
| 8025 | if (nil == _sg_mtl_cmd_buffer) { |
| 8026 | /* block until the oldest frame in flight has finished */ |
| 8027 | dispatch_semaphore_wait(_sg_mtl_sem, DISPATCH_TIME_FOREVER); |
| 8028 | _sg_mtl_cmd_buffer = [_sg_mtl_cmd_queue commandBufferWithUnretainedReferences]; |
| 8029 | } |
| 8030 | |
| 8031 | /* if this is first pass in frame, get uniform buffer base pointer */ |
| 8032 | if (0 == _sg.mtl.cur_ub_base_ptr) { |
| 8033 | _sg.mtl.cur_ub_base_ptr = (uint8_t*)[_sg_mtl_uniform_buffers[_sg.mtl.cur_frame_rotate_index] contents]; |
| 8034 | } |
| 8035 | |
| 8036 | /* initialize a render pass descriptor */ |
| 8037 | MTLRenderPassDescriptor* pass_desc = nil; |
| 8038 | if (pass) { |
| 8039 | /* offscreen render pass */ |
| 8040 | pass_desc = [MTLRenderPassDescriptor renderPassDescriptor]; |
| 8041 | } |
| 8042 | else { |
| 8043 | /* default render pass, call user-provided callback to provide render pass descriptor */ |
| 8044 | pass_desc = (__bridge MTLRenderPassDescriptor*) _sg.mtl.renderpass_descriptor_cb(); |
| 8045 | |
| 8046 | } |
| 8047 | if (pass_desc) { |
| 8048 | _sg.mtl.pass_valid = true; |
| 8049 | } |
| 8050 | else { |
| 8051 | /* default pass descriptor will not be valid if window is minimized, |
| 8052 | don't do any rendering in this case */ |
| 8053 | _sg.mtl.pass_valid = false; |
| 8054 | return; |
| 8055 | } |
| 8056 | if (pass) { |
| 8057 | /* setup pass descriptor for offscreen rendering */ |
| 8058 | SOKOL_ASSERT(pass->slot.state == SG_RESOURCESTATE_VALID); |
| 8059 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 8060 | const _sg_attachment_t* att = &pass->color_atts[i]; |
| 8061 | if (0 == att->image) { |
| 8062 | break; |
| 8063 | } |
| 8064 | SOKOL_ASSERT(att->image->slot.state == SG_RESOURCESTATE_VALID); |
| 8065 | SOKOL_ASSERT(att->image->slot.id == att->image_id.id); |
| 8066 | const bool is_msaa = (att->image->sample_count > 1); |
| 8067 | pass_desc.colorAttachments[i].loadAction = _sg_mtl_load_action(action->colors[i].action); |
| 8068 | pass_desc.colorAttachments[i].storeAction = is_msaa ? MTLStoreActionMultisampleResolve : MTLStoreActionStore; |
| 8069 | const float* c = &(action->colors[i].val[0]); |
| 8070 | pass_desc.colorAttachments[i].clearColor = MTLClearColorMake(c[0], c[1], c[2], c[3]); |
| 8071 | if (is_msaa) { |
| 8072 | SOKOL_ASSERT(att->image->mtl_msaa_tex != _SG_MTL_INVALID_SLOT_INDEX); |
| 8073 | SOKOL_ASSERT(att->image->mtl_tex[att->image->active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 8074 | pass_desc.colorAttachments[i].texture = _sg_mtl_idpool[att->image->mtl_msaa_tex]; |
| 8075 | pass_desc.colorAttachments[i].resolveTexture = _sg_mtl_idpool[att->image->mtl_tex[att->image->active_slot]]; |
| 8076 | pass_desc.colorAttachments[i].resolveLevel = att->mip_level; |
| 8077 | switch (att->image->type) { |
| 8078 | case SG_IMAGETYPE_CUBE: |
| 8079 | case SG_IMAGETYPE_ARRAY: |
| 8080 | pass_desc.colorAttachments[i].resolveSlice = att->slice; |
| 8081 | break; |
| 8082 | case SG_IMAGETYPE_3D: |
| 8083 | pass_desc.colorAttachments[i].resolveDepthPlane = att->slice; |
| 8084 | break; |
| 8085 | default: break; |
| 8086 | } |
| 8087 | } |
| 8088 | else { |
| 8089 | SOKOL_ASSERT(att->image->mtl_tex[att->image->active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 8090 | pass_desc.colorAttachments[i].texture = _sg_mtl_idpool[att->image->mtl_tex[att->image->active_slot]]; |
| 8091 | pass_desc.colorAttachments[i].level = att->mip_level; |
| 8092 | switch (att->image->type) { |
| 8093 | case SG_IMAGETYPE_CUBE: |
| 8094 | case SG_IMAGETYPE_ARRAY: |
| 8095 | pass_desc.colorAttachments[i].slice = att->slice; |
| 8096 | break; |
| 8097 | case SG_IMAGETYPE_3D: |
| 8098 | pass_desc.colorAttachments[i].depthPlane = att->slice; |
| 8099 | break; |
| 8100 | default: break; |
| 8101 | } |
| 8102 | } |
| 8103 | } |
| 8104 | if (0 != pass->ds_att.image) { |
| 8105 | const _sg_attachment_t* att = &pass->ds_att; |
| 8106 | SOKOL_ASSERT(att->image->slot.state == SG_RESOURCESTATE_VALID); |
| 8107 | SOKOL_ASSERT(att->image->slot.id == att->image_id.id); |
| 8108 | SOKOL_ASSERT(att->image->mtl_depth_tex != _SG_MTL_INVALID_SLOT_INDEX); |
| 8109 | pass_desc.depthAttachment.texture = _sg_mtl_idpool[att->image->mtl_depth_tex]; |
| 8110 | pass_desc.depthAttachment.loadAction = _sg_mtl_load_action(action->depth.action); |
| 8111 | pass_desc.depthAttachment.clearDepth = action->depth.val; |
| 8112 | if (_sg_is_depth_stencil_format(att->image->pixel_format)) { |
| 8113 | pass_desc.stencilAttachment.texture = _sg_mtl_idpool[att->image->mtl_depth_tex]; |
| 8114 | pass_desc.stencilAttachment.loadAction = _sg_mtl_load_action(action->stencil.action); |
| 8115 | pass_desc.stencilAttachment.clearStencil = action->stencil.val; |
| 8116 | } |
| 8117 | } |
| 8118 | } |
| 8119 | else { |
| 8120 | /* setup pass descriptor for default rendering */ |
| 8121 | pass_desc.colorAttachments[0].loadAction = _sg_mtl_load_action(action->colors[0].action); |
| 8122 | const float* c = &(action->colors[0].val[0]); |
| 8123 | pass_desc.colorAttachments[0].clearColor = MTLClearColorMake(c[0], c[1], c[2], c[3]); |
| 8124 | pass_desc.depthAttachment.loadAction = _sg_mtl_load_action(action->depth.action); |
| 8125 | pass_desc.depthAttachment.clearDepth = action->depth.val; |
| 8126 | pass_desc.stencilAttachment.loadAction = _sg_mtl_load_action(action->stencil.action); |
| 8127 | pass_desc.stencilAttachment.clearStencil = action->stencil.val; |
| 8128 | } |
| 8129 | |
| 8130 | /* create a render command encoder, this might return nil if window is minimized */ |
| 8131 | _sg_mtl_cmd_encoder = [_sg_mtl_cmd_buffer renderCommandEncoderWithDescriptor:pass_desc]; |
| 8132 | if (_sg_mtl_cmd_encoder == nil) { |
| 8133 | _sg.mtl.pass_valid = false; |
| 8134 | return; |
| 8135 | } |
| 8136 | |
| 8137 | /* bind the global uniform buffer, this only happens once per pass */ |
| 8138 | for (int slot = 0; slot < SG_MAX_SHADERSTAGE_UBS; slot++) { |
| 8139 | [_sg_mtl_cmd_encoder |
| 8140 | setVertexBuffer:_sg_mtl_uniform_buffers[_sg.mtl.cur_frame_rotate_index] |
| 8141 | offset:0 |
| 8142 | atIndex:slot]; |
| 8143 | [_sg_mtl_cmd_encoder |
| 8144 | setFragmentBuffer:_sg_mtl_uniform_buffers[_sg.mtl.cur_frame_rotate_index] |
| 8145 | offset:0 |
| 8146 | atIndex:slot]; |
| 8147 | } |
| 8148 | } |
| 8149 | |
| 8150 | _SOKOL_PRIVATE void _sg_end_pass(void) { |
| 8151 | SOKOL_ASSERT(_sg.mtl.in_pass); |
| 8152 | _sg.mtl.in_pass = false; |
| 8153 | _sg.mtl.pass_valid = false; |
| 8154 | if (nil != _sg_mtl_cmd_encoder) { |
| 8155 | [_sg_mtl_cmd_encoder endEncoding]; |
| 8156 | _sg_mtl_cmd_encoder = nil; |
| 8157 | } |
| 8158 | } |
| 8159 | |
| 8160 | _SOKOL_PRIVATE void _sg_commit(void) { |
| 8161 | SOKOL_ASSERT(!_sg.mtl.in_pass); |
| 8162 | SOKOL_ASSERT(!_sg.mtl.pass_valid); |
| 8163 | SOKOL_ASSERT(_sg.mtl.drawable_cb); |
| 8164 | SOKOL_ASSERT(nil == _sg_mtl_cmd_encoder); |
| 8165 | SOKOL_ASSERT(nil != _sg_mtl_cmd_buffer); |
| 8166 | |
| 8167 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 8168 | [_sg_mtl_uniform_buffers[_sg.mtl.cur_frame_rotate_index] didModifyRange:NSMakeRange(0, _sg.mtl.cur_ub_offset)]; |
| 8169 | #endif |
| 8170 | |
| 8171 | /* present, commit and signal semaphore when done */ |
| 8172 | id<MTLDrawable> cur_drawable = (__bridge id<MTLDrawable>) _sg.mtl.drawable_cb(); |
| 8173 | [_sg_mtl_cmd_buffer presentDrawable:cur_drawable]; |
| 8174 | __block dispatch_semaphore_t sem = _sg_mtl_sem; |
| 8175 | [_sg_mtl_cmd_buffer addCompletedHandler:^(id<MTLCommandBuffer> cmd_buffer) { |
| 8176 | dispatch_semaphore_signal(sem); |
| 8177 | }]; |
| 8178 | [_sg_mtl_cmd_buffer commit]; |
| 8179 | |
| 8180 | /* garbage-collect resources pending for release */ |
| 8181 | _sg_mtl_garbage_collect(_sg.mtl.frame_index); |
| 8182 | |
| 8183 | /* rotate uniform buffer slot */ |
| 8184 | if (++_sg.mtl.cur_frame_rotate_index >= SG_NUM_INFLIGHT_FRAMES) { |
| 8185 | _sg.mtl.cur_frame_rotate_index = 0; |
| 8186 | } |
| 8187 | _sg.mtl.frame_index++; |
| 8188 | _sg.mtl.cur_ub_offset = 0; |
| 8189 | _sg.mtl.cur_ub_base_ptr = 0; |
| 8190 | _sg_mtl_cmd_buffer = nil; |
| 8191 | } |
| 8192 | |
| 8193 | _SOKOL_PRIVATE void _sg_apply_viewport(int x, int y, int w, int h, bool origin_top_left) { |
| 8194 | SOKOL_ASSERT(_sg.mtl.in_pass); |
| 8195 | if (!_sg.mtl.pass_valid) { |
| 8196 | return; |
| 8197 | } |
| 8198 | SOKOL_ASSERT(_sg_mtl_cmd_encoder); |
| 8199 | MTLViewport vp; |
| 8200 | vp.originX = (double) x; |
| 8201 | vp.originY = (double) (origin_top_left ? y : (_sg.mtl.cur_height - (y + h))); |
| 8202 | vp.width = (double) w; |
| 8203 | vp.height = (double) h; |
| 8204 | vp.znear = 0.0; |
| 8205 | vp.zfar = 1.0; |
| 8206 | [_sg_mtl_cmd_encoder setViewport:vp]; |
| 8207 | } |
| 8208 | |
| 8209 | _SOKOL_PRIVATE void _sg_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) { |
| 8210 | SOKOL_ASSERT(_sg.mtl.in_pass); |
| 8211 | if (!_sg.mtl.pass_valid) { |
| 8212 | return; |
| 8213 | } |
| 8214 | SOKOL_ASSERT(_sg_mtl_cmd_encoder); |
| 8215 | /* clip against framebuffer rect */ |
| 8216 | x = _sg_min(_sg_max(0, x), _sg.mtl.cur_width-1); |
| 8217 | y = _sg_min(_sg_max(0, y), _sg.mtl.cur_height-1); |
| 8218 | if ((x + w) > _sg.mtl.cur_width) { |
| 8219 | w = _sg.mtl.cur_width - x; |
| 8220 | } |
| 8221 | if ((y + h) > _sg.mtl.cur_height) { |
| 8222 | h = _sg.mtl.cur_height - y; |
| 8223 | } |
| 8224 | w = _sg_max(w, 1); |
| 8225 | h = _sg_max(h, 1); |
| 8226 | |
| 8227 | MTLScissorRect r; |
| 8228 | r.x = x; |
| 8229 | r.y = origin_top_left ? y : (_sg.mtl.cur_height - (y + h)); |
| 8230 | r.width = w; |
| 8231 | r.height = h; |
| 8232 | [_sg_mtl_cmd_encoder setScissorRect:r]; |
| 8233 | } |
| 8234 | |
| 8235 | _SOKOL_PRIVATE void _sg_apply_pipeline(_sg_pipeline_t* pip) { |
| 8236 | SOKOL_ASSERT(pip); |
| 8237 | SOKOL_ASSERT(pip->shader); |
| 8238 | SOKOL_ASSERT(_sg.mtl.in_pass); |
| 8239 | if (!_sg.mtl.pass_valid) { |
| 8240 | return; |
| 8241 | } |
| 8242 | SOKOL_ASSERT(_sg_mtl_cmd_encoder); |
| 8243 | |
| 8244 | if ((_sg.mtl.state_cache.cur_pipeline != pip) || (_sg.mtl.state_cache.cur_pipeline_id.id != pip->slot.id)) { |
| 8245 | _sg.mtl.state_cache.cur_pipeline = pip; |
| 8246 | _sg.mtl.state_cache.cur_pipeline_id.id = pip->slot.id; |
| 8247 | const float* c = pip->blend_color; |
| 8248 | [_sg_mtl_cmd_encoder setBlendColorRed:c[0] green:c[1] blue:c[2] alpha:c[3]]; |
| 8249 | [_sg_mtl_cmd_encoder setCullMode:pip->mtl_cull_mode]; |
| 8250 | [_sg_mtl_cmd_encoder setFrontFacingWinding:pip->mtl_winding]; |
| 8251 | [_sg_mtl_cmd_encoder setStencilReferenceValue:pip->mtl_stencil_ref]; |
| 8252 | [_sg_mtl_cmd_encoder setDepthBias:pip->depth_bias slopeScale:pip->depth_bias_slope_scale clamp:pip->depth_bias_clamp]; |
| 8253 | SOKOL_ASSERT(pip->mtl_rps != _SG_MTL_INVALID_SLOT_INDEX); |
| 8254 | [_sg_mtl_cmd_encoder setRenderPipelineState:_sg_mtl_idpool[pip->mtl_rps]]; |
| 8255 | SOKOL_ASSERT(pip->mtl_dss != _SG_MTL_INVALID_SLOT_INDEX); |
| 8256 | [_sg_mtl_cmd_encoder setDepthStencilState:_sg_mtl_idpool[pip->mtl_dss]]; |
| 8257 | } |
| 8258 | } |
| 8259 | |
| 8260 | _SOKOL_PRIVATE void _sg_apply_bindings( |
| 8261 | _sg_pipeline_t* pip, |
| 8262 | _sg_buffer_t** vbs, const int* vb_offsets, int num_vbs, |
| 8263 | _sg_buffer_t* ib, int ib_offset, |
| 8264 | _sg_image_t** vs_imgs, int num_vs_imgs, |
| 8265 | _sg_image_t** fs_imgs, int num_fs_imgs) |
| 8266 | { |
| 8267 | SOKOL_ASSERT(_sg.mtl.in_pass); |
| 8268 | if (!_sg.mtl.pass_valid) { |
| 8269 | return; |
| 8270 | } |
| 8271 | SOKOL_ASSERT(_sg_mtl_cmd_encoder); |
| 8272 | |
| 8273 | /* store index buffer binding, this will be needed later in sg_draw() */ |
| 8274 | _sg.mtl.state_cache.cur_indexbuffer = ib; |
| 8275 | _sg.mtl.state_cache.cur_indexbuffer_offset = ib_offset; |
| 8276 | if (ib) { |
| 8277 | SOKOL_ASSERT(pip->index_type != SG_INDEXTYPE_NONE); |
| 8278 | _sg.mtl.state_cache.cur_indexbuffer_id.id = ib->slot.id; |
| 8279 | } |
| 8280 | else { |
| 8281 | SOKOL_ASSERT(pip->index_type == SG_INDEXTYPE_NONE); |
| 8282 | _sg.mtl.state_cache.cur_indexbuffer_id.id = SG_INVALID_ID; |
| 8283 | } |
| 8284 | |
| 8285 | /* apply vertex buffers */ |
| 8286 | int slot; |
| 8287 | for (slot = 0; slot < num_vbs; slot++) { |
| 8288 | const _sg_buffer_t* vb = vbs[slot]; |
| 8289 | if ((_sg.mtl.state_cache.cur_vertexbuffers[slot] != vb) || |
| 8290 | (_sg.mtl.state_cache.cur_vertexbuffer_offsets[slot] != vb_offsets[slot]) || |
| 8291 | (_sg.mtl.state_cache.cur_vertexbuffer_ids[slot].id != vb->slot.id)) |
| 8292 | { |
| 8293 | _sg.mtl.state_cache.cur_vertexbuffers[slot] = vb; |
| 8294 | _sg.mtl.state_cache.cur_vertexbuffer_offsets[slot] = vb_offsets[slot]; |
| 8295 | _sg.mtl.state_cache.cur_vertexbuffer_ids[slot].id = vb->slot.id; |
| 8296 | const NSUInteger mtl_slot = SG_MAX_SHADERSTAGE_UBS + slot; |
| 8297 | SOKOL_ASSERT(vb->mtl_buf[vb->active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 8298 | [_sg_mtl_cmd_encoder setVertexBuffer:_sg_mtl_idpool[vb->mtl_buf[vb->active_slot]] |
| 8299 | offset:vb_offsets[slot] |
| 8300 | atIndex:mtl_slot]; |
| 8301 | } |
| 8302 | } |
| 8303 | |
| 8304 | /* apply vertex shader images */ |
| 8305 | for (slot = 0; slot < num_vs_imgs; slot++) { |
| 8306 | const _sg_image_t* img = vs_imgs[slot]; |
| 8307 | if ((_sg.mtl.state_cache.cur_vs_images[slot] != img) || (_sg.mtl.state_cache.cur_vs_image_ids[slot].id != img->slot.id)) { |
| 8308 | _sg.mtl.state_cache.cur_vs_images[slot] = img; |
| 8309 | _sg.mtl.state_cache.cur_vs_image_ids[slot].id = img->slot.id; |
| 8310 | SOKOL_ASSERT(img->mtl_tex[img->active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 8311 | [_sg_mtl_cmd_encoder setVertexTexture:_sg_mtl_idpool[img->mtl_tex[img->active_slot]] atIndex:slot]; |
| 8312 | SOKOL_ASSERT(img->mtl_sampler_state != _SG_MTL_INVALID_SLOT_INDEX); |
| 8313 | [_sg_mtl_cmd_encoder setVertexSamplerState:_sg_mtl_idpool[img->mtl_sampler_state] atIndex:slot]; |
| 8314 | } |
| 8315 | } |
| 8316 | |
| 8317 | /* apply fragment shader images */ |
| 8318 | for (slot = 0; slot < num_fs_imgs; slot++) { |
| 8319 | const _sg_image_t* img = fs_imgs[slot]; |
| 8320 | if ((_sg.mtl.state_cache.cur_fs_images[slot] != img) || (_sg.mtl.state_cache.cur_fs_image_ids[slot].id != img->slot.id)) { |
| 8321 | _sg.mtl.state_cache.cur_fs_images[slot] = img; |
| 8322 | _sg.mtl.state_cache.cur_fs_image_ids[slot].id = img->slot.id; |
| 8323 | SOKOL_ASSERT(img->mtl_tex[img->active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 8324 | [_sg_mtl_cmd_encoder setFragmentTexture:_sg_mtl_idpool[img->mtl_tex[img->active_slot]] atIndex:slot]; |
| 8325 | SOKOL_ASSERT(img->mtl_sampler_state != _SG_MTL_INVALID_SLOT_INDEX); |
| 8326 | [_sg_mtl_cmd_encoder setFragmentSamplerState:_sg_mtl_idpool[img->mtl_sampler_state] atIndex:slot]; |
| 8327 | } |
| 8328 | } |
| 8329 | } |
| 8330 | |
| 8331 | #define _sg_mtl_roundup(val, round_to) (((val)+((round_to)-1))&~((round_to)-1)) |
| 8332 | |
| 8333 | _SOKOL_PRIVATE void _sg_apply_uniforms(sg_shader_stage stage_index, int ub_index, const void* data, int num_bytes) { |
| 8334 | SOKOL_ASSERT(_sg.mtl.in_pass); |
| 8335 | if (!_sg.mtl.pass_valid) { |
| 8336 | return; |
| 8337 | } |
| 8338 | SOKOL_ASSERT(_sg_mtl_cmd_encoder); |
| 8339 | SOKOL_ASSERT(data && (num_bytes > 0)); |
| 8340 | SOKOL_ASSERT((stage_index >= 0) && ((int)stage_index < SG_NUM_SHADER_STAGES)); |
| 8341 | SOKOL_ASSERT((ub_index >= 0) && (ub_index < SG_MAX_SHADERSTAGE_UBS)); |
| 8342 | SOKOL_ASSERT((_sg.mtl.cur_ub_offset + num_bytes) <= _sg.mtl.ub_size); |
| 8343 | SOKOL_ASSERT((_sg.mtl.cur_ub_offset & (_SG_MTL_UB_ALIGN-1)) == 0); |
| 8344 | SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline && _sg.mtl.state_cache.cur_pipeline->shader); |
| 8345 | SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline->slot.id == _sg.mtl.state_cache.cur_pipeline_id.id); |
| 8346 | SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline->shader->slot.id == _sg.mtl.state_cache.cur_pipeline->shader_id.id); |
| 8347 | SOKOL_ASSERT(ub_index < _sg.mtl.state_cache.cur_pipeline->shader->stage[stage_index].num_uniform_blocks); |
| 8348 | SOKOL_ASSERT(num_bytes <= _sg.mtl.state_cache.cur_pipeline->shader->stage[stage_index].uniform_blocks[ub_index].size); |
| 8349 | |
| 8350 | /* copy to global uniform buffer, record offset into cmd encoder, and advance offset */ |
| 8351 | uint8_t* dst = &_sg.mtl.cur_ub_base_ptr[_sg.mtl.cur_ub_offset]; |
| 8352 | memcpy(dst, data, num_bytes); |
| 8353 | if (stage_index == SG_SHADERSTAGE_VS) { |
| 8354 | [_sg_mtl_cmd_encoder setVertexBufferOffset:_sg.mtl.cur_ub_offset atIndex:ub_index]; |
| 8355 | } |
| 8356 | else { |
| 8357 | [_sg_mtl_cmd_encoder setFragmentBufferOffset:_sg.mtl.cur_ub_offset atIndex:ub_index]; |
| 8358 | } |
| 8359 | _sg.mtl.cur_ub_offset = _sg_mtl_roundup(_sg.mtl.cur_ub_offset + num_bytes, _SG_MTL_UB_ALIGN); |
| 8360 | } |
| 8361 | |
| 8362 | _SOKOL_PRIVATE void _sg_draw(int base_element, int num_elements, int num_instances) { |
| 8363 | SOKOL_ASSERT(_sg.mtl.in_pass); |
| 8364 | if (!_sg.mtl.pass_valid) { |
| 8365 | return; |
| 8366 | } |
| 8367 | SOKOL_ASSERT(_sg_mtl_cmd_encoder); |
| 8368 | SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline && (_sg.mtl.state_cache.cur_pipeline->slot.id == _sg.mtl.state_cache.cur_pipeline_id.id)); |
| 8369 | if (SG_INDEXTYPE_NONE != _sg.mtl.state_cache.cur_pipeline->index_type) { |
| 8370 | /* indexed rendering */ |
| 8371 | SOKOL_ASSERT(_sg.mtl.state_cache.cur_indexbuffer && (_sg.mtl.state_cache.cur_indexbuffer->slot.id == _sg.mtl.state_cache.cur_indexbuffer_id.id)); |
| 8372 | const _sg_buffer_t* ib = _sg.mtl.state_cache.cur_indexbuffer; |
| 8373 | SOKOL_ASSERT(ib->mtl_buf[ib->active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 8374 | const NSUInteger index_buffer_offset = _sg.mtl.state_cache.cur_indexbuffer_offset + |
| 8375 | base_element * _sg.mtl.state_cache.cur_pipeline->mtl_index_size; |
| 8376 | [_sg_mtl_cmd_encoder drawIndexedPrimitives:_sg.mtl.state_cache.cur_pipeline->mtl_prim_type |
| 8377 | indexCount:num_elements |
| 8378 | indexType:_sg.mtl.state_cache.cur_pipeline->mtl_index_type |
| 8379 | indexBuffer:_sg_mtl_idpool[ib->mtl_buf[ib->active_slot]] |
| 8380 | indexBufferOffset:index_buffer_offset |
| 8381 | instanceCount:num_instances]; |
| 8382 | } |
| 8383 | else { |
| 8384 | /* non-indexed rendering */ |
| 8385 | [_sg_mtl_cmd_encoder drawPrimitives:_sg.mtl.state_cache.cur_pipeline->mtl_prim_type |
| 8386 | vertexStart:base_element |
| 8387 | vertexCount:num_elements |
| 8388 | instanceCount:num_instances]; |
| 8389 | } |
| 8390 | } |
| 8391 | |
| 8392 | _SOKOL_PRIVATE void _sg_update_buffer(_sg_buffer_t* buf, const void* data, int data_size) { |
| 8393 | SOKOL_ASSERT(buf && data && (data_size > 0)); |
| 8394 | if (++buf->active_slot >= buf->num_slots) { |
| 8395 | buf->active_slot = 0; |
| 8396 | } |
| 8397 | __unsafe_unretained id<MTLBuffer> mtl_buf = _sg_mtl_idpool[buf->mtl_buf[buf->active_slot]]; |
| 8398 | void* dst_ptr = [mtl_buf contents]; |
| 8399 | memcpy(dst_ptr, data, data_size); |
| 8400 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 8401 | [mtl_buf didModifyRange:NSMakeRange(0, data_size)]; |
| 8402 | #endif |
| 8403 | } |
| 8404 | |
| 8405 | _SOKOL_PRIVATE void _sg_append_buffer(_sg_buffer_t* buf, const void* data, int data_size, bool new_frame) { |
| 8406 | SOKOL_ASSERT(buf && data && (data_size > 0)); |
| 8407 | if (new_frame) { |
| 8408 | if (++buf->active_slot >= buf->num_slots) { |
| 8409 | buf->active_slot = 0; |
| 8410 | } |
| 8411 | } |
| 8412 | __unsafe_unretained id<MTLBuffer> mtl_buf = _sg_mtl_idpool[buf->mtl_buf[buf->active_slot]]; |
| 8413 | uint8_t* dst_ptr = (uint8_t*) [mtl_buf contents]; |
| 8414 | dst_ptr += buf->append_pos; |
| 8415 | memcpy(dst_ptr, data, data_size); |
| 8416 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 8417 | [mtl_buf didModifyRange:NSMakeRange(buf->append_pos, data_size)]; |
| 8418 | #endif |
| 8419 | } |
| 8420 | |
| 8421 | _SOKOL_PRIVATE void _sg_update_image(_sg_image_t* img, const sg_image_content* data) { |
| 8422 | SOKOL_ASSERT(img && data); |
| 8423 | if (++img->active_slot >= img->num_slots) { |
| 8424 | img->active_slot = 0; |
| 8425 | } |
| 8426 | __unsafe_unretained id<MTLTexture> mtl_tex = _sg_mtl_idpool[img->mtl_tex[img->active_slot]]; |
| 8427 | _sg_mtl_copy_image_content(img, mtl_tex, data); |
| 8428 | } |
| 8429 | |
| 8430 | #endif |
| 8431 | |
| 8432 | /*== RESOURCE POOLS ==========================================================*/ |
| 8433 | |
| 8434 | _SOKOL_PRIVATE void _sg_init_pool(_sg_pool_t* pool, int num) { |
| 8435 | SOKOL_ASSERT(pool && (num >= 1)); |
| 8436 | /* slot 0 is reserved for the 'invalid id', so bump the pool size by 1 */ |
| 8437 | pool->size = num + 1; |
| 8438 | pool->queue_top = 0; |
| 8439 | /* generation counters indexable by pool slot index, slot 0 is reserved */ |
| 8440 | size_t gen_ctrs_size = sizeof(uint32_t) * pool->size; |
| 8441 | pool->gen_ctrs = (uint32_t*) SOKOL_MALLOC(gen_ctrs_size); |
| 8442 | SOKOL_ASSERT(pool->gen_ctrs); |
| 8443 | memset(pool->gen_ctrs, 0, gen_ctrs_size); |
| 8444 | /* it's not a bug to only reserve 'num' here */ |
| 8445 | pool->free_queue = (int*) SOKOL_MALLOC(sizeof(int)*num); |
| 8446 | SOKOL_ASSERT(pool->free_queue); |
| 8447 | /* never allocate the zero-th pool item since the invalid id is 0 */ |
| 8448 | for (int i = pool->size-1; i >= 1; i--) { |
| 8449 | pool->free_queue[pool->queue_top++] = i; |
| 8450 | } |
| 8451 | } |
| 8452 | |
| 8453 | _SOKOL_PRIVATE void _sg_discard_pool(_sg_pool_t* pool) { |
| 8454 | SOKOL_ASSERT(pool); |
| 8455 | SOKOL_ASSERT(pool->free_queue); |
| 8456 | SOKOL_FREE(pool->free_queue); |
| 8457 | pool->free_queue = 0; |
| 8458 | SOKOL_ASSERT(pool->gen_ctrs); |
| 8459 | SOKOL_FREE(pool->gen_ctrs); |
| 8460 | pool->gen_ctrs = 0; |
| 8461 | pool->size = 0; |
| 8462 | pool->queue_top = 0; |
| 8463 | } |
| 8464 | |
| 8465 | _SOKOL_PRIVATE int _sg_pool_alloc_index(_sg_pool_t* pool) { |
| 8466 | SOKOL_ASSERT(pool); |
| 8467 | SOKOL_ASSERT(pool->free_queue); |
| 8468 | if (pool->queue_top > 0) { |
| 8469 | int slot_index = pool->free_queue[--pool->queue_top]; |
| 8470 | SOKOL_ASSERT((slot_index > 0) && (slot_index < pool->size)); |
| 8471 | return slot_index; |
| 8472 | } |
| 8473 | else { |
| 8474 | /* pool exhausted */ |
| 8475 | return _SG_INVALID_SLOT_INDEX; |
| 8476 | } |
| 8477 | } |
| 8478 | |
| 8479 | _SOKOL_PRIVATE void _sg_pool_free_index(_sg_pool_t* pool, int slot_index) { |
| 8480 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < pool->size)); |
| 8481 | SOKOL_ASSERT(pool); |
| 8482 | SOKOL_ASSERT(pool->free_queue); |
| 8483 | SOKOL_ASSERT(pool->queue_top < pool->size); |
| 8484 | #ifdef SOKOL_DEBUG |
| 8485 | /* debug check against double-free */ |
| 8486 | for (int i = 0; i < pool->queue_top; i++) { |
| 8487 | SOKOL_ASSERT(pool->free_queue[i] != slot_index); |
| 8488 | } |
| 8489 | #endif |
| 8490 | pool->free_queue[pool->queue_top++] = slot_index; |
| 8491 | SOKOL_ASSERT(pool->queue_top <= (pool->size-1)); |
| 8492 | } |
| 8493 | |
| 8494 | _SOKOL_PRIVATE void _sg_reset_buffer(_sg_buffer_t* buf) { |
| 8495 | SOKOL_ASSERT(buf); |
| 8496 | memset(buf, 0, sizeof(_sg_buffer_t)); |
| 8497 | } |
| 8498 | |
| 8499 | _SOKOL_PRIVATE void _sg_reset_image(_sg_image_t* img) { |
| 8500 | SOKOL_ASSERT(img); |
| 8501 | memset(img, 0, sizeof(_sg_image_t)); |
| 8502 | } |
| 8503 | |
| 8504 | _SOKOL_PRIVATE void _sg_reset_shader(_sg_shader_t* shd) { |
| 8505 | SOKOL_ASSERT(shd); |
| 8506 | memset(shd, 0, sizeof(_sg_shader_t)); |
| 8507 | } |
| 8508 | |
| 8509 | _SOKOL_PRIVATE void _sg_reset_pipeline(_sg_pipeline_t* pip) { |
| 8510 | SOKOL_ASSERT(pip); |
| 8511 | memset(pip, 0, sizeof(_sg_pipeline_t)); |
| 8512 | } |
| 8513 | |
| 8514 | _SOKOL_PRIVATE void _sg_reset_pass(_sg_pass_t* pass) { |
| 8515 | SOKOL_ASSERT(pass); |
| 8516 | memset(pass, 0, sizeof(_sg_pass_t)); |
| 8517 | } |
| 8518 | |
| 8519 | _SOKOL_PRIVATE void _sg_reset_context(_sg_context_t* ctx) { |
| 8520 | SOKOL_ASSERT(ctx); |
| 8521 | memset(ctx, 0, sizeof(_sg_context_t)); |
| 8522 | } |
| 8523 | |
| 8524 | _SOKOL_PRIVATE void _sg_setup_pools(_sg_pools_t* p, const sg_desc* desc) { |
| 8525 | SOKOL_ASSERT(p); |
| 8526 | SOKOL_ASSERT(desc); |
| 8527 | /* note: the pools here will have an additional item, since slot 0 is reserved */ |
| 8528 | SOKOL_ASSERT((desc->buffer_pool_size > 0) && (desc->buffer_pool_size < _SG_MAX_POOL_SIZE)); |
| 8529 | _sg_init_pool(&p->buffer_pool, desc->buffer_pool_size); |
| 8530 | size_t buffer_pool_byte_size = sizeof(_sg_buffer_t) * p->buffer_pool.size; |
| 8531 | p->buffers = (_sg_buffer_t*) SOKOL_MALLOC(buffer_pool_byte_size); |
| 8532 | SOKOL_ASSERT(p->buffers); |
| 8533 | memset(p->buffers, 0, buffer_pool_byte_size); |
| 8534 | |
| 8535 | SOKOL_ASSERT((desc->image_pool_size > 0) && (desc->image_pool_size < _SG_MAX_POOL_SIZE)); |
| 8536 | _sg_init_pool(&p->image_pool, desc->image_pool_size); |
| 8537 | size_t image_pool_byte_size = sizeof(_sg_image_t) * p->image_pool.size; |
| 8538 | p->images = (_sg_image_t*) SOKOL_MALLOC(image_pool_byte_size); |
| 8539 | SOKOL_ASSERT(p->images); |
| 8540 | memset(p->images, 0, image_pool_byte_size); |
| 8541 | |
| 8542 | SOKOL_ASSERT((desc->shader_pool_size > 0) && (desc->shader_pool_size < _SG_MAX_POOL_SIZE)); |
| 8543 | _sg_init_pool(&p->shader_pool, desc->shader_pool_size); |
| 8544 | size_t shader_pool_byte_size = sizeof(_sg_shader_t) * p->shader_pool.size; |
| 8545 | p->shaders = (_sg_shader_t*) SOKOL_MALLOC(shader_pool_byte_size); |
| 8546 | SOKOL_ASSERT(p->shaders); |
| 8547 | memset(p->shaders, 0, shader_pool_byte_size); |
| 8548 | |
| 8549 | SOKOL_ASSERT((desc->pipeline_pool_size > 0) && (desc->pipeline_pool_size < _SG_MAX_POOL_SIZE)); |
| 8550 | _sg_init_pool(&p->pipeline_pool, desc->pipeline_pool_size); |
| 8551 | size_t pipeline_pool_byte_size = sizeof(_sg_pipeline_t) * p->pipeline_pool.size; |
| 8552 | p->pipelines = (_sg_pipeline_t*) SOKOL_MALLOC(pipeline_pool_byte_size); |
| 8553 | SOKOL_ASSERT(p->pipelines); |
| 8554 | memset(p->pipelines, 0, pipeline_pool_byte_size); |
| 8555 | |
| 8556 | SOKOL_ASSERT((desc->pass_pool_size > 0) && (desc->pass_pool_size < _SG_MAX_POOL_SIZE)); |
| 8557 | _sg_init_pool(&p->pass_pool, desc->pass_pool_size); |
| 8558 | size_t pass_pool_byte_size = sizeof(_sg_pass_t) * p->pass_pool.size; |
| 8559 | p->passes = (_sg_pass_t*) SOKOL_MALLOC(pass_pool_byte_size); |
| 8560 | SOKOL_ASSERT(p->passes); |
| 8561 | memset(p->passes, 0, pass_pool_byte_size); |
| 8562 | |
| 8563 | SOKOL_ASSERT((desc->context_pool_size > 0) && (desc->context_pool_size < _SG_MAX_POOL_SIZE)); |
| 8564 | _sg_init_pool(&p->context_pool, desc->context_pool_size); |
| 8565 | size_t context_pool_byte_size = sizeof(_sg_context_t) * p->context_pool.size; |
| 8566 | p->contexts = (_sg_context_t*) SOKOL_MALLOC(context_pool_byte_size); |
| 8567 | SOKOL_ASSERT(p->contexts); |
| 8568 | memset(p->contexts, 0, context_pool_byte_size); |
| 8569 | } |
| 8570 | |
| 8571 | _SOKOL_PRIVATE void _sg_discard_pools(_sg_pools_t* p) { |
| 8572 | SOKOL_ASSERT(p); |
| 8573 | SOKOL_FREE(p->contexts); p->contexts = 0; |
| 8574 | SOKOL_FREE(p->passes); p->passes = 0; |
| 8575 | SOKOL_FREE(p->pipelines); p->pipelines = 0; |
| 8576 | SOKOL_FREE(p->shaders); p->shaders = 0; |
| 8577 | SOKOL_FREE(p->images); p->images = 0; |
| 8578 | SOKOL_FREE(p->buffers); p->buffers = 0; |
| 8579 | _sg_discard_pool(&p->context_pool); |
| 8580 | _sg_discard_pool(&p->pass_pool); |
| 8581 | _sg_discard_pool(&p->pipeline_pool); |
| 8582 | _sg_discard_pool(&p->shader_pool); |
| 8583 | _sg_discard_pool(&p->image_pool); |
| 8584 | _sg_discard_pool(&p->buffer_pool); |
| 8585 | } |
| 8586 | |
| 8587 | /* allocate the slot at slot_index: |
| 8588 | - bump the slot's generation counter |
| 8589 | - create a resource id from the generation counter and slot index |
| 8590 | - set the slot's id to this id |
| 8591 | - set the slot's state to ALLOC |
| 8592 | - return the resource id |
| 8593 | */ |
| 8594 | _SOKOL_PRIVATE uint32_t _sg_slot_alloc(_sg_pool_t* pool, _sg_slot_t* slot, int slot_index) { |
| 8595 | /* FIXME: add handling for an overflowing generation counter, |
| 8596 | for now, just overflow (another option is to disable |
| 8597 | the slot) |
| 8598 | */ |
| 8599 | SOKOL_ASSERT(pool && pool->gen_ctrs); |
| 8600 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < pool->size)); |
| 8601 | SOKOL_ASSERT((slot->state == SG_RESOURCESTATE_INITIAL) && (slot->id == SG_INVALID_ID)); |
| 8602 | uint32_t ctr = ++pool->gen_ctrs[slot_index]; |
| 8603 | slot->id = (ctr<<_SG_SLOT_SHIFT)|(slot_index & _SG_SLOT_MASK); |
| 8604 | slot->state = SG_RESOURCESTATE_ALLOC; |
| 8605 | return slot->id; |
| 8606 | } |
| 8607 | |
| 8608 | /* extract slot index from id */ |
| 8609 | _SOKOL_PRIVATE int _sg_slot_index(uint32_t id) { |
| 8610 | int slot_index = (int) (id & _SG_SLOT_MASK); |
| 8611 | SOKOL_ASSERT(_SG_INVALID_SLOT_INDEX != slot_index); |
| 8612 | return slot_index; |
| 8613 | } |
| 8614 | |
| 8615 | /* returns pointer to resource by id without matching id check */ |
| 8616 | _SOKOL_PRIVATE _sg_buffer_t* _sg_buffer_at(const _sg_pools_t* p, uint32_t buf_id) { |
| 8617 | SOKOL_ASSERT(p && (SG_INVALID_ID != buf_id)); |
| 8618 | int slot_index = _sg_slot_index(buf_id); |
| 8619 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->buffer_pool.size)); |
| 8620 | return &p->buffers[slot_index]; |
| 8621 | } |
| 8622 | |
| 8623 | _SOKOL_PRIVATE _sg_image_t* _sg_image_at(const _sg_pools_t* p, uint32_t img_id) { |
| 8624 | SOKOL_ASSERT(p && (SG_INVALID_ID != img_id)); |
| 8625 | int slot_index = _sg_slot_index(img_id); |
| 8626 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->image_pool.size)); |
| 8627 | return &p->images[slot_index]; |
| 8628 | } |
| 8629 | |
| 8630 | _SOKOL_PRIVATE _sg_shader_t* _sg_shader_at(const _sg_pools_t* p, uint32_t shd_id) { |
| 8631 | SOKOL_ASSERT(p && (SG_INVALID_ID != shd_id)); |
| 8632 | int slot_index = _sg_slot_index(shd_id); |
| 8633 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->shader_pool.size)); |
| 8634 | return &p->shaders[slot_index]; |
| 8635 | } |
| 8636 | |
| 8637 | _SOKOL_PRIVATE _sg_pipeline_t* _sg_pipeline_at(const _sg_pools_t* p, uint32_t pip_id) { |
| 8638 | SOKOL_ASSERT(p && (SG_INVALID_ID != pip_id)); |
| 8639 | int slot_index = _sg_slot_index(pip_id); |
| 8640 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->pipeline_pool.size)); |
| 8641 | return &p->pipelines[slot_index]; |
| 8642 | } |
| 8643 | |
| 8644 | _SOKOL_PRIVATE _sg_pass_t* _sg_pass_at(const _sg_pools_t* p, uint32_t pass_id) { |
| 8645 | SOKOL_ASSERT(p && (SG_INVALID_ID != pass_id)); |
| 8646 | int slot_index = _sg_slot_index(pass_id); |
| 8647 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->pass_pool.size)); |
| 8648 | return &p->passes[slot_index]; |
| 8649 | } |
| 8650 | |
| 8651 | _SOKOL_PRIVATE _sg_context_t* _sg_context_at(const _sg_pools_t* p, uint32_t context_id) { |
| 8652 | SOKOL_ASSERT(p && (SG_INVALID_ID != context_id)); |
| 8653 | int slot_index = _sg_slot_index(context_id); |
| 8654 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->context_pool.size)); |
| 8655 | return &p->contexts[slot_index]; |
| 8656 | } |
| 8657 | |
| 8658 | /* returns pointer to resource with matching id check, may return 0 */ |
| 8659 | _SOKOL_PRIVATE _sg_buffer_t* _sg_lookup_buffer(const _sg_pools_t* p, uint32_t buf_id) { |
| 8660 | if (SG_INVALID_ID != buf_id) { |
| 8661 | _sg_buffer_t* buf = _sg_buffer_at(p, buf_id); |
| 8662 | if (buf->slot.id == buf_id) { |
| 8663 | return buf; |
| 8664 | } |
| 8665 | } |
| 8666 | return 0; |
| 8667 | } |
| 8668 | |
| 8669 | _SOKOL_PRIVATE _sg_image_t* _sg_lookup_image(const _sg_pools_t* p, uint32_t img_id) { |
| 8670 | if (SG_INVALID_ID != img_id) { |
| 8671 | _sg_image_t* img = _sg_image_at(p, img_id); |
| 8672 | if (img->slot.id == img_id) { |
| 8673 | return img; |
| 8674 | } |
| 8675 | } |
| 8676 | return 0; |
| 8677 | } |
| 8678 | |
| 8679 | _SOKOL_PRIVATE _sg_shader_t* _sg_lookup_shader(const _sg_pools_t* p, uint32_t shd_id) { |
| 8680 | SOKOL_ASSERT(p); |
| 8681 | if (SG_INVALID_ID != shd_id) { |
| 8682 | _sg_shader_t* shd = _sg_shader_at(p, shd_id); |
| 8683 | if (shd->slot.id == shd_id) { |
| 8684 | return shd; |
| 8685 | } |
| 8686 | } |
| 8687 | return 0; |
| 8688 | } |
| 8689 | |
| 8690 | _SOKOL_PRIVATE _sg_pipeline_t* _sg_lookup_pipeline(const _sg_pools_t* p, uint32_t pip_id) { |
| 8691 | SOKOL_ASSERT(p); |
| 8692 | if (SG_INVALID_ID != pip_id) { |
| 8693 | _sg_pipeline_t* pip = _sg_pipeline_at(p, pip_id); |
| 8694 | if (pip->slot.id == pip_id) { |
| 8695 | return pip; |
| 8696 | } |
| 8697 | } |
| 8698 | return 0; |
| 8699 | } |
| 8700 | |
| 8701 | _SOKOL_PRIVATE _sg_pass_t* _sg_lookup_pass(const _sg_pools_t* p, uint32_t pass_id) { |
| 8702 | SOKOL_ASSERT(p); |
| 8703 | if (SG_INVALID_ID != pass_id) { |
| 8704 | _sg_pass_t* pass = _sg_pass_at(p, pass_id); |
| 8705 | if (pass->slot.id == pass_id) { |
| 8706 | return pass; |
| 8707 | } |
| 8708 | } |
| 8709 | return 0; |
| 8710 | } |
| 8711 | |
| 8712 | _SOKOL_PRIVATE _sg_context_t* _sg_lookup_context(const _sg_pools_t* p, uint32_t ctx_id) { |
| 8713 | SOKOL_ASSERT(p); |
| 8714 | if (SG_INVALID_ID != ctx_id) { |
| 8715 | _sg_context_t* ctx = _sg_context_at(p, ctx_id); |
| 8716 | if (ctx->slot.id == ctx_id) { |
| 8717 | return ctx; |
| 8718 | } |
| 8719 | } |
| 8720 | return 0; |
| 8721 | } |
| 8722 | |
| 8723 | _SOKOL_PRIVATE void _sg_destroy_all_resources(_sg_pools_t* p, uint32_t ctx_id) { |
| 8724 | /* this is a bit dumb since it loops over all pool slots to |
| 8725 | find the occupied slots, on the other hand it is only ever |
| 8726 | executed at shutdown |
| 8727 | NOTE: ONLY EXECUTE THIS AT SHUTDOWN |
| 8728 | ...because the free queues will not be reset |
| 8729 | and the resource slots not be cleared! |
| 8730 | */ |
| 8731 | for (int i = 1; i < p->buffer_pool.size; i++) { |
| 8732 | if (p->buffers[i].slot.ctx_id == ctx_id) { |
| 8733 | sg_resource_state state = p->buffers[i].slot.state; |
| 8734 | if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) { |
| 8735 | _sg_destroy_buffer(&p->buffers[i]); |
| 8736 | } |
| 8737 | } |
| 8738 | } |
| 8739 | for (int i = 1; i < p->image_pool.size; i++) { |
| 8740 | if (p->images[i].slot.ctx_id == ctx_id) { |
| 8741 | sg_resource_state state = p->images[i].slot.state; |
| 8742 | if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) { |
| 8743 | _sg_destroy_image(&p->images[i]); |
| 8744 | } |
| 8745 | } |
| 8746 | } |
| 8747 | for (int i = 1; i < p->shader_pool.size; i++) { |
| 8748 | if (p->shaders[i].slot.ctx_id == ctx_id) { |
| 8749 | sg_resource_state state = p->shaders[i].slot.state; |
| 8750 | if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) { |
| 8751 | _sg_destroy_shader(&p->shaders[i]); |
| 8752 | } |
| 8753 | } |
| 8754 | } |
| 8755 | for (int i = 1; i < p->pipeline_pool.size; i++) { |
| 8756 | if (p->pipelines[i].slot.ctx_id == ctx_id) { |
| 8757 | sg_resource_state state = p->pipelines[i].slot.state; |
| 8758 | if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) { |
| 8759 | _sg_destroy_pipeline(&p->pipelines[i]); |
| 8760 | } |
| 8761 | } |
| 8762 | } |
| 8763 | for (int i = 1; i < p->pass_pool.size; i++) { |
| 8764 | if (p->passes[i].slot.ctx_id == ctx_id) { |
| 8765 | sg_resource_state state = p->passes[i].slot.state; |
| 8766 | if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) { |
| 8767 | _sg_destroy_pass(&p->passes[i]); |
| 8768 | } |
| 8769 | } |
| 8770 | } |
| 8771 | } |
| 8772 | |
| 8773 | /*== VALIDATION LAYER ========================================================*/ |
| 8774 | #if defined(SOKOL_DEBUG) |
| 8775 | /* return a human readable string for an _sg_validate_error */ |
| 8776 | _SOKOL_PRIVATE const char* _sg_validate_string(_sg_validate_error_t err) { |
| 8777 | switch (err) { |
| 8778 | /* buffer creation validation errors */ |
| 8779 | case _SG_VALIDATE_BUFFERDESC_CANARY: return "sg_buffer_desc not initialized"; |
| 8780 | case _SG_VALIDATE_BUFFERDESC_SIZE: return "sg_buffer_desc.size cannot be 0"; |
| 8781 | case _SG_VALIDATE_BUFFERDESC_CONTENT: return "immutable buffers must be initialized with content (sg_buffer_desc.content)"; |
| 8782 | case _SG_VALIDATE_BUFFERDESC_NO_CONTENT: return "dynamic/stream usage buffers cannot be initialized with content"; |
| 8783 | |
| 8784 | /* image creation validation errros */ |
| 8785 | case _SG_VALIDATE_IMAGEDESC_CANARY: return "sg_image_desc not initialized"; |
| 8786 | case _SG_VALIDATE_IMAGEDESC_WIDTH: return "sg_image_desc.width must be > 0"; |
| 8787 | case _SG_VALIDATE_IMAGEDESC_HEIGHT: return "sg_image_desc.height must be > 0"; |
| 8788 | case _SG_VALIDATE_IMAGEDESC_RT_PIXELFORMAT: return "invalid pixel format for render-target image"; |
| 8789 | case _SG_VALIDATE_IMAGEDESC_NONRT_PIXELFORMAT: return "invalid pixel format for non-render-target image"; |
| 8790 | case _SG_VALIDATE_IMAGEDESC_MSAA_BUT_NO_RT: return "non-render-target images cannot be multisampled"; |
| 8791 | case _SG_VALIDATE_IMAGEDESC_NO_MSAA_RT_SUPPORT: return "MSAA render targets not supported (SG_FEATURE_MSAA_RENDER_TARGETS)"; |
| 8792 | case _SG_VALIDATE_IMAGEDESC_RT_IMMUTABLE: return "render target images must be SG_USAGE_IMMUTABLE"; |
| 8793 | case _SG_VALIDATE_IMAGEDESC_RT_NO_CONTENT: return "render target images cannot be initialized with content"; |
| 8794 | case _SG_VALIDATE_IMAGEDESC_CONTENT: return "missing or invalid content for immutable image"; |
| 8795 | case _SG_VALIDATE_IMAGEDESC_NO_CONTENT: return "dynamic/stream usage images cannot be initialized with content"; |
| 8796 | |
| 8797 | /* shader creation */ |
| 8798 | case _SG_VALIDATE_SHADERDESC_CANARY: return "sg_shader_desc not initialized"; |
| 8799 | case _SG_VALIDATE_SHADERDESC_SOURCE: return "shader source code required"; |
| 8800 | case _SG_VALIDATE_SHADERDESC_BYTECODE: return "shader byte code required"; |
| 8801 | case _SG_VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE: return "shader source or byte code required"; |
| 8802 | case _SG_VALIDATE_SHADERDESC_NO_BYTECODE_SIZE: return "shader byte code length (in bytes) required"; |
| 8803 | case _SG_VALIDATE_SHADERDESC_NO_CONT_UBS: return "shader uniform blocks must occupy continuous slots"; |
| 8804 | case _SG_VALIDATE_SHADERDESC_NO_CONT_UB_MEMBERS: return "uniform block members must occupy continuous slots"; |
| 8805 | case _SG_VALIDATE_SHADERDESC_NO_UB_MEMBERS: return "GL backend requires uniform block member declarations"; |
| 8806 | case _SG_VALIDATE_SHADERDESC_UB_MEMBER_NAME: return "uniform block member name missing"; |
| 8807 | case _SG_VALIDATE_SHADERDESC_UB_SIZE_MISMATCH: return "size of uniform block members doesn't match uniform block size"; |
| 8808 | case _SG_VALIDATE_SHADERDESC_NO_CONT_IMGS: return "shader images must occupy continuous slots"; |
| 8809 | case _SG_VALIDATE_SHADERDESC_IMG_NAME: return "GL backend requires uniform block member names"; |
| 8810 | case _SG_VALIDATE_SHADERDESC_ATTR_NAMES: return "GLES2 backend requires vertex attribute names"; |
| 8811 | case _SG_VALIDATE_SHADERDESC_ATTR_SEMANTICS: return "D3D11 backend requires vertex attribute semantics"; |
| 8812 | case _SG_VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG: return "vertex attribute name/semantic string too long (max len 16)"; |
| 8813 | |
| 8814 | /* pipeline creation */ |
| 8815 | case _SG_VALIDATE_PIPELINEDESC_CANARY: return "sg_pipeline_desc not initialized"; |
| 8816 | case _SG_VALIDATE_PIPELINEDESC_SHADER: return "sg_pipeline_desc.shader missing or invalid"; |
| 8817 | case _SG_VALIDATE_PIPELINEDESC_NO_ATTRS: return "sg_pipeline_desc.layout.attrs is empty or not continuous"; |
| 8818 | case _SG_VALIDATE_PIPELINEDESC_LAYOUT_STRIDE4: return "sg_pipeline_desc.layout.buffers[].stride must be multiple of 4"; |
| 8819 | case _SG_VALIDATE_PIPELINEDESC_ATTR_NAME: return "GLES2/WebGL missing vertex attribute name in shader"; |
| 8820 | case _SG_VALIDATE_PIPELINEDESC_ATTR_SEMANTICS: return "D3D11 missing vertex attribute semantics in shader"; |
| 8821 | |
| 8822 | /* pass creation */ |
| 8823 | case _SG_VALIDATE_PASSDESC_CANARY: return "sg_pass_desc not initialized"; |
| 8824 | case _SG_VALIDATE_PASSDESC_NO_COLOR_ATTS: return "sg_pass_desc.color_attachments[0] must be valid"; |
| 8825 | case _SG_VALIDATE_PASSDESC_NO_CONT_COLOR_ATTS: return "color attachments must occupy continuous slots"; |
| 8826 | case _SG_VALIDATE_PASSDESC_IMAGE: return "pass attachment image is not valid"; |
| 8827 | case _SG_VALIDATE_PASSDESC_MIPLEVEL: return "pass attachment mip level is bigger than image has mipmaps"; |
| 8828 | case _SG_VALIDATE_PASSDESC_FACE: return "pass attachment image is cubemap, but face index is too big"; |
| 8829 | case _SG_VALIDATE_PASSDESC_LAYER: return "pass attachment image is array texture, but layer index is too big"; |
| 8830 | case _SG_VALIDATE_PASSDESC_SLICE: return "pass attachment image is 3d texture, but slice value is too big"; |
| 8831 | case _SG_VALIDATE_PASSDESC_IMAGE_NO_RT: return "pass attachment image must be render targets"; |
| 8832 | case _SG_VALIDATE_PASSDESC_COLOR_PIXELFORMATS: return "all pass color attachment images must have the same pixel format"; |
| 8833 | case _SG_VALIDATE_PASSDESC_COLOR_INV_PIXELFORMAT: return "pass color-attachment images must have a renderable pixel format"; |
| 8834 | case _SG_VALIDATE_PASSDESC_DEPTH_INV_PIXELFORMAT: return "pass depth-attachment image must have depth pixel format"; |
| 8835 | case _SG_VALIDATE_PASSDESC_IMAGE_SIZES: return "all pass attachments must have the same size"; |
| 8836 | case _SG_VALIDATE_PASSDESC_IMAGE_SAMPLE_COUNTS: return "all pass attachments must have the same sample count"; |
| 8837 | |
| 8838 | /* sg_begin_pass */ |
| 8839 | case _SG_VALIDATE_BEGINPASS_PASS: return "sg_begin_pass: pass must be valid"; |
| 8840 | case _SG_VALIDATE_BEGINPASS_IMAGE: return "sg_begin_pass: one or more attachment images are not valid"; |
| 8841 | |
| 8842 | /* sg_apply_pipeline */ |
| 8843 | case _SG_VALIDATE_APIP_PIPELINE_VALID_ID: return "sg_apply_pipeline: invalid pipeline id provided"; |
| 8844 | case _SG_VALIDATE_APIP_PIPELINE_EXISTS: return "sg_apply_pipeline: pipeline object no longer alive"; |
| 8845 | case _SG_VALIDATE_APIP_PIPELINE_VALID: return "sg_apply_pipeline: pipeline object not in valid state"; |
| 8846 | case _SG_VALIDATE_APIP_SHADER_EXISTS: return "sg_apply_pipeline: shader object no longer alive"; |
| 8847 | case _SG_VALIDATE_APIP_SHADER_VALID: return "sg_apply_pipeline: shader object not in valid state"; |
| 8848 | case _SG_VALIDATE_APIP_ATT_COUNT: return "sg_apply_pipeline: color_attachment_count in pipeline doesn't match number of pass color attachments"; |
| 8849 | case _SG_VALIDATE_APIP_COLOR_FORMAT: return "sg_apply_pipeline: color_format in pipeline doesn't match pass color attachment pixel format"; |
| 8850 | case _SG_VALIDATE_APIP_DEPTH_FORMAT: return "sg_apply_pipeline: depth_format in pipeline doesn't match pass depth attachment pixel format"; |
| 8851 | case _SG_VALIDATE_APIP_SAMPLE_COUNT: return "sg_apply_pipeline: MSAA sample count in pipeline doesn't match render pass attachment sample count"; |
| 8852 | |
| 8853 | /* sg_apply_bindings */ |
| 8854 | case _SG_VALIDATE_ABND_PIPELINE: return "sg_apply_bindings: must be called after sg_apply_pipeline"; |
| 8855 | case _SG_VALIDATE_ABND_PIPELINE_EXISTS: return "sg_apply_bindings: currently applied pipeline object no longer alive"; |
| 8856 | case _SG_VALIDATE_ABND_PIPELINE_VALID: return "sg_apply_bindings: currently applied pipeline object not in valid state"; |
| 8857 | case _SG_VALIDATE_ABND_VBS: return "sg_apply_bindings: number of vertex buffers doesn't match number of pipeline vertex layouts"; |
| 8858 | case _SG_VALIDATE_ABND_VB_EXISTS: return "sg_apply_bindings: vertex buffer no longer alive"; |
| 8859 | case _SG_VALIDATE_ABND_VB_TYPE: return "sg_apply_bindings: buffer in vertex buffer slot is not a SG_BUFFERTYPE_VERTEXBUFFER"; |
| 8860 | case _SG_VALIDATE_ABND_VB_OVERFLOW: return "sg_apply_bindings: buffer in vertex buffer slot is overflown"; |
| 8861 | case _SG_VALIDATE_ABND_NO_IB: return "sg_apply_bindings: pipeline object defines indexed rendering, but no index buffer provided"; |
| 8862 | case _SG_VALIDATE_ABND_IB: return "sg_apply_bindings: pipeline object defines non-indexed rendering, but index buffer provided"; |
| 8863 | case _SG_VALIDATE_ABND_IB_EXISTS: return "sg_apply_bindings: index buffer no longer alive"; |
| 8864 | case _SG_VALIDATE_ABND_IB_TYPE: return "sg_apply_bindings: buffer in index buffer slot is not a SG_BUFFERTYPE_INDEXBUFFER"; |
| 8865 | case _SG_VALIDATE_ABND_IB_OVERFLOW: return "sg_apply_bindings: buffer in index buffer slot is overflown"; |
| 8866 | case _SG_VALIDATE_ABND_VS_IMGS: return "sg_apply_bindings: vertex shader image count doesn't match sg_shader_desc"; |
| 8867 | case _SG_VALIDATE_ABND_VS_IMG_EXISTS: return "sg_apply_bindings: vertex shader image no longer alive"; |
| 8868 | case _SG_VALIDATE_ABND_VS_IMG_TYPES: return "sg_apply_bindings: one or more vertex shader image types don't match sg_shader_desc"; |
| 8869 | case _SG_VALIDATE_ABND_FS_IMGS: return "sg_apply_bindings: fragment shader image count doesn't match sg_shader_desc"; |
| 8870 | case _SG_VALIDATE_ABND_FS_IMG_EXISTS: return "sg_apply_bindings: fragment shader image no longer alive"; |
| 8871 | case _SG_VALIDATE_ABND_FS_IMG_TYPES: return "sg_apply_bindings: one or more fragment shader image types don't match sg_shader_desc"; |
| 8872 | |
| 8873 | /* sg_apply_uniforms */ |
| 8874 | case _SG_VALIDATE_AUB_NO_PIPELINE: return "sg_apply_uniforms: must be called after sg_apply_pipeline()"; |
| 8875 | case _SG_VALIDATE_AUB_NO_UB_AT_SLOT: return "sg_apply_uniforms: no uniform block declaration at this shader stage UB slot"; |
| 8876 | case _SG_VALIDATE_AUB_SIZE: return "sg_apply_uniforms: data size exceeds declared uniform block size"; |
| 8877 | |
| 8878 | /* sg_update_buffer */ |
| 8879 | case _SG_VALIDATE_UPDATEBUF_USAGE: return "sg_update_buffer: cannot update immutable buffer"; |
| 8880 | case _SG_VALIDATE_UPDATEBUF_SIZE: return "sg_update_buffer: update size is bigger than buffer size"; |
| 8881 | case _SG_VALIDATE_UPDATEBUF_ONCE: return "sg_update_buffer: only one update allowed per buffer and frame"; |
| 8882 | case _SG_VALIDATE_UPDATEBUF_APPEND: return "sg_update_buffer: cannot call sg_update_buffer and sg_append_buffer in same frame"; |
| 8883 | |
| 8884 | /* sg_append_buffer */ |
| 8885 | case _SG_VALIDATE_APPENDBUF_USAGE: return "sg_append_buffer: cannot append to immutable buffer"; |
| 8886 | case _SG_VALIDATE_APPENDBUF_SIZE: return "sg_append_buffer: overall appended size is bigger than buffer size"; |
| 8887 | case _SG_VALIDATE_APPENDBUF_UPDATE: return "sg_append_buffer: cannot call sg_append_buffer and sg_update_buffer in same frame"; |
| 8888 | |
| 8889 | /* sg_update_image */ |
| 8890 | case _SG_VALIDATE_UPDIMG_USAGE: return "sg_update_image: cannot update immutable image"; |
| 8891 | case _SG_VALIDATE_UPDIMG_NOTENOUGHDATA: return "sg_update_image: not enough subimage data provided"; |
| 8892 | case _SG_VALIDATE_UPDIMG_SIZE: return "sg_update_image: provided subimage data size too big"; |
| 8893 | case _SG_VALIDATE_UPDIMG_COMPRESSED: return "sg_update_image: cannot update images with compressed format"; |
| 8894 | case _SG_VALIDATE_UPDIMG_ONCE: return "sg_update_image: only one update allowed per image and frame"; |
| 8895 | |
| 8896 | default: return "unknown validation error"; |
| 8897 | } |
| 8898 | } |
| 8899 | #endif /* defined(SOKOL_DEBUG) */ |
| 8900 | |
| 8901 | /*-- validation checks -------------------------------------------------------*/ |
| 8902 | #if defined(SOKOL_DEBUG) |
| 8903 | _SOKOL_PRIVATE void _sg_validate_begin(void) { |
| 8904 | _sg.validate_error = _SG_VALIDATE_SUCCESS; |
| 8905 | } |
| 8906 | |
| 8907 | _SOKOL_PRIVATE void _sg_validate(bool cond, _sg_validate_error_t err) { |
| 8908 | if (!cond) { |
| 8909 | _sg.validate_error = err; |
| 8910 | SOKOL_LOG(_sg_validate_string(err)); |
| 8911 | } |
| 8912 | } |
| 8913 | |
| 8914 | _SOKOL_PRIVATE bool _sg_validate_end(void) { |
| 8915 | if (_sg.validate_error != _SG_VALIDATE_SUCCESS) { |
| 8916 | #if !defined(SOKOL_VALIDATE_NON_FATAL) |
| 8917 | SOKOL_LOG("^^^^ VALIDATION FAILED, TERMINATING ^^^^"); |
| 8918 | SOKOL_ASSERT(false); |
| 8919 | #endif |
| 8920 | return false; |
| 8921 | } |
| 8922 | else { |
| 8923 | return true; |
| 8924 | } |
| 8925 | } |
| 8926 | #endif |
| 8927 | |
| 8928 | _SOKOL_PRIVATE bool _sg_validate_buffer_desc(const sg_buffer_desc* desc) { |
| 8929 | #if !defined(SOKOL_DEBUG) |
| 8930 | _SOKOL_UNUSED(desc); |
| 8931 | return true; |
| 8932 | #else |
| 8933 | SOKOL_ASSERT(desc); |
| 8934 | SOKOL_VALIDATE_BEGIN(); |
| 8935 | SOKOL_VALIDATE(desc->_start_canary == 0, _SG_VALIDATE_BUFFERDESC_CANARY); |
| 8936 | SOKOL_VALIDATE(desc->_end_canary == 0, _SG_VALIDATE_BUFFERDESC_CANARY); |
| 8937 | SOKOL_VALIDATE(desc->size > 0, _SG_VALIDATE_BUFFERDESC_SIZE); |
| 8938 | bool ext = (0 != desc->gl_buffers[0]) || (0 != desc->mtl_buffers[0]) || (0 != desc->d3d11_buffer); |
| 8939 | if (!ext && (desc->usage == SG_USAGE_IMMUTABLE)) { |
| 8940 | SOKOL_VALIDATE(0 != desc->content, _SG_VALIDATE_BUFFERDESC_CONTENT); |
| 8941 | } |
| 8942 | else { |
| 8943 | SOKOL_VALIDATE(0 == desc->content, _SG_VALIDATE_BUFFERDESC_NO_CONTENT); |
| 8944 | } |
| 8945 | return SOKOL_VALIDATE_END(); |
| 8946 | #endif |
| 8947 | } |
| 8948 | |
| 8949 | _SOKOL_PRIVATE bool _sg_validate_image_desc(const sg_image_desc* desc) { |
| 8950 | #if !defined(SOKOL_DEBUG) |
| 8951 | _SOKOL_UNUSED(desc); |
| 8952 | return true; |
| 8953 | #else |
| 8954 | SOKOL_ASSERT(desc); |
| 8955 | SOKOL_VALIDATE_BEGIN(); |
| 8956 | SOKOL_VALIDATE(desc->_start_canary == 0, _SG_VALIDATE_IMAGEDESC_CANARY); |
| 8957 | SOKOL_VALIDATE(desc->_end_canary == 0, _SG_VALIDATE_IMAGEDESC_CANARY); |
| 8958 | SOKOL_VALIDATE(desc->width > 0, _SG_VALIDATE_IMAGEDESC_WIDTH); |
| 8959 | SOKOL_VALIDATE(desc->height > 0, _SG_VALIDATE_IMAGEDESC_HEIGHT); |
| 8960 | const sg_pixel_format fmt = desc->pixel_format; |
| 8961 | const sg_usage usage = desc->usage; |
| 8962 | const bool ext = (0 != desc->gl_textures[0]) || (0 != desc->mtl_textures[0]) || (0 != desc->d3d11_texture); |
| 8963 | if (desc->render_target) { |
| 8964 | if (desc->sample_count > 1) { |
| 8965 | SOKOL_VALIDATE(_sg_query_feature(SG_FEATURE_MSAA_RENDER_TARGETS), _SG_VALIDATE_IMAGEDESC_NO_MSAA_RT_SUPPORT); |
| 8966 | } |
| 8967 | const bool valid_color_fmt = _sg_is_valid_rendertarget_color_format(fmt); |
| 8968 | const bool valid_depth_fmt = _sg_is_valid_rendertarget_depth_format(fmt); |
| 8969 | SOKOL_VALIDATE(valid_color_fmt || valid_depth_fmt, _SG_VALIDATE_IMAGEDESC_RT_PIXELFORMAT); |
| 8970 | SOKOL_VALIDATE(usage == SG_USAGE_IMMUTABLE, _SG_VALIDATE_IMAGEDESC_RT_IMMUTABLE); |
| 8971 | SOKOL_VALIDATE(desc->content.subimage[0][0].ptr==0, _SG_VALIDATE_IMAGEDESC_RT_NO_CONTENT); |
| 8972 | } |
| 8973 | else { |
| 8974 | SOKOL_VALIDATE(desc->sample_count <= 1, _SG_VALIDATE_IMAGEDESC_MSAA_BUT_NO_RT); |
| 8975 | const bool valid_nonrt_fmt = !_sg_is_valid_rendertarget_depth_format(fmt); |
| 8976 | SOKOL_VALIDATE(valid_nonrt_fmt, _SG_VALIDATE_IMAGEDESC_NONRT_PIXELFORMAT); |
| 8977 | /* FIXME: should use the same "expected size" computation as in _sg_validate_update_image() here */ |
| 8978 | if (!ext && (usage == SG_USAGE_IMMUTABLE)) { |
| 8979 | const int num_faces = desc->type == SG_IMAGETYPE_CUBE ? 6:1; |
| 8980 | const int num_mips = desc->num_mipmaps; |
| 8981 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 8982 | for (int mip_index = 0; mip_index < num_mips; mip_index++) { |
| 8983 | const bool has_data = desc->content.subimage[face_index][mip_index].ptr != 0; |
| 8984 | const bool has_size = desc->content.subimage[face_index][mip_index].size > 0; |
| 8985 | SOKOL_VALIDATE(has_data && has_size, _SG_VALIDATE_IMAGEDESC_CONTENT); |
| 8986 | } |
| 8987 | } |
| 8988 | } |
| 8989 | else { |
| 8990 | for (int face_index = 0; face_index < SG_CUBEFACE_NUM; face_index++) { |
| 8991 | for (int mip_index = 0; mip_index < SG_MAX_MIPMAPS; mip_index++) { |
| 8992 | const bool no_data = 0 == desc->content.subimage[face_index][mip_index].ptr; |
| 8993 | const bool no_size = 0 == desc->content.subimage[face_index][mip_index].size; |
| 8994 | SOKOL_VALIDATE(no_data && no_size, _SG_VALIDATE_IMAGEDESC_NO_CONTENT); |
| 8995 | } |
| 8996 | } |
| 8997 | } |
| 8998 | } |
| 8999 | return SOKOL_VALIDATE_END(); |
| 9000 | #endif |
| 9001 | } |
| 9002 | |
| 9003 | _SOKOL_PRIVATE bool _sg_validate_shader_desc(const sg_shader_desc* desc) { |
| 9004 | #if !defined(SOKOL_DEBUG) |
| 9005 | _SOKOL_UNUSED(desc); |
| 9006 | return true; |
| 9007 | #else |
| 9008 | SOKOL_ASSERT(desc); |
| 9009 | SOKOL_VALIDATE_BEGIN(); |
| 9010 | SOKOL_VALIDATE(desc->_start_canary == 0, _SG_VALIDATE_SHADERDESC_CANARY); |
| 9011 | SOKOL_VALIDATE(desc->_end_canary == 0, _SG_VALIDATE_SHADERDESC_CANARY); |
| 9012 | #if defined(SOKOL_GLES2) |
| 9013 | SOKOL_VALIDATE(0 != desc->attrs[0].name, _SG_VALIDATE_SHADERDESC_ATTR_NAMES); |
| 9014 | #elif defined(SOKOL_D3D11) |
| 9015 | SOKOL_VALIDATE(0 != desc->attrs[0].sem_name, _SG_VALIDATE_SHADERDESC_ATTR_SEMANTICS); |
| 9016 | #endif |
| 9017 | #if defined(SOKOL_GLCORE33) || defined(SOKOL_GLES2) || defined(SOKOL_GLES3) |
| 9018 | /* on GL, must provide shader source code */ |
| 9019 | SOKOL_VALIDATE(0 != desc->vs.source, _SG_VALIDATE_SHADERDESC_SOURCE); |
| 9020 | SOKOL_VALIDATE(0 != desc->fs.source, _SG_VALIDATE_SHADERDESC_SOURCE); |
| 9021 | #elif defined(SOKOL_METAL) || defined(SOKOL_D3D11) |
| 9022 | /* on Metal or D3D11, must provide shader source code or byte code */ |
| 9023 | SOKOL_VALIDATE((0 != desc->vs.source)||(0 != desc->vs.byte_code), _SG_VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE); |
| 9024 | SOKOL_VALIDATE((0 != desc->fs.source)||(0 != desc->fs.byte_code), _SG_VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE); |
| 9025 | #else |
| 9026 | /* Dummy Backend, don't require source or bytecode */ |
| 9027 | #endif |
| 9028 | for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) { |
| 9029 | if (desc->attrs[i].name) { |
| 9030 | SOKOL_VALIDATE(strlen(desc->attrs[i].name) < _SG_STRING_SIZE, _SG_VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG); |
| 9031 | } |
| 9032 | if (desc->attrs[i].sem_name) { |
| 9033 | SOKOL_VALIDATE(strlen(desc->attrs[i].sem_name) < _SG_STRING_SIZE, _SG_VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG); |
| 9034 | } |
| 9035 | } |
| 9036 | /* if shader byte code, the size must also be provided */ |
| 9037 | if (0 != desc->vs.byte_code) { |
| 9038 | SOKOL_VALIDATE(desc->vs.byte_code_size > 0, _SG_VALIDATE_SHADERDESC_NO_BYTECODE_SIZE); |
| 9039 | } |
| 9040 | if (0 != desc->fs.byte_code) { |
| 9041 | SOKOL_VALIDATE(desc->fs.byte_code_size > 0, _SG_VALIDATE_SHADERDESC_NO_BYTECODE_SIZE); |
| 9042 | } |
| 9043 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 9044 | const sg_shader_stage_desc* stage_desc = (stage_index == 0)? &desc->vs : &desc->fs; |
| 9045 | bool uniform_blocks_continuous = true; |
| 9046 | for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) { |
| 9047 | const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index]; |
| 9048 | if (ub_desc->size > 0) { |
| 9049 | SOKOL_VALIDATE(uniform_blocks_continuous, _SG_VALIDATE_SHADERDESC_NO_CONT_UBS); |
| 9050 | bool uniforms_continuous = true; |
| 9051 | int uniform_offset = 0; |
| 9052 | int num_uniforms = 0; |
| 9053 | for (int u_index = 0; u_index < SG_MAX_UB_MEMBERS; u_index++) { |
| 9054 | const sg_shader_uniform_desc* u_desc = &ub_desc->uniforms[u_index]; |
| 9055 | if (u_desc->type != SG_UNIFORMTYPE_INVALID) { |
| 9056 | SOKOL_VALIDATE(uniforms_continuous, _SG_VALIDATE_SHADERDESC_NO_CONT_UB_MEMBERS); |
| 9057 | #if defined(SOKOL_GLES2) || defined(SOKOL_GLES3) |
| 9058 | SOKOL_VALIDATE(u_desc->name, _SG_VALIDATE_SHADERDESC_UB_MEMBER_NAME); |
| 9059 | #endif |
| 9060 | const int array_count = u_desc->array_count; |
| 9061 | uniform_offset += _sg_uniform_size(u_desc->type, array_count); |
| 9062 | num_uniforms++; |
| 9063 | } |
| 9064 | else { |
| 9065 | uniforms_continuous = false; |
| 9066 | } |
| 9067 | } |
| 9068 | #if defined(SOKOL_GLCORE33) || defined(SOKOL_GLES2) || defined(SOKOL_GLES3) |
| 9069 | SOKOL_VALIDATE(uniform_offset == ub_desc->size, _SG_VALIDATE_SHADERDESC_UB_SIZE_MISMATCH); |
| 9070 | SOKOL_VALIDATE(num_uniforms > 0, _SG_VALIDATE_SHADERDESC_NO_UB_MEMBERS); |
| 9071 | #endif |
| 9072 | } |
| 9073 | else { |
| 9074 | uniform_blocks_continuous = false; |
| 9075 | } |
| 9076 | } |
| 9077 | bool images_continuous = true; |
| 9078 | for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) { |
| 9079 | const sg_shader_image_desc* img_desc = &stage_desc->images[img_index]; |
| 9080 | if (img_desc->type != _SG_IMAGETYPE_DEFAULT) { |
| 9081 | SOKOL_VALIDATE(images_continuous, _SG_VALIDATE_SHADERDESC_NO_CONT_IMGS); |
| 9082 | #if defined(SOKOL_GLES2) |
| 9083 | SOKOL_VALIDATE(img_desc->name, _SG_VALIDATE_SHADERDESC_IMG_NAME); |
| 9084 | #endif |
| 9085 | } |
| 9086 | else { |
| 9087 | images_continuous = false; |
| 9088 | } |
| 9089 | } |
| 9090 | } |
| 9091 | return SOKOL_VALIDATE_END(); |
| 9092 | #endif |
| 9093 | } |
| 9094 | |
| 9095 | _SOKOL_PRIVATE bool _sg_validate_pipeline_desc(const sg_pipeline_desc* desc) { |
| 9096 | #if !defined(SOKOL_DEBUG) |
| 9097 | _SOKOL_UNUSED(desc); |
| 9098 | return true; |
| 9099 | #else |
| 9100 | SOKOL_ASSERT(desc); |
| 9101 | SOKOL_VALIDATE_BEGIN(); |
| 9102 | SOKOL_VALIDATE(desc->_start_canary == 0, _SG_VALIDATE_PIPELINEDESC_CANARY); |
| 9103 | SOKOL_VALIDATE(desc->_end_canary == 0, _SG_VALIDATE_PIPELINEDESC_CANARY); |
| 9104 | SOKOL_VALIDATE(desc->shader.id != SG_INVALID_ID, _SG_VALIDATE_PIPELINEDESC_SHADER); |
| 9105 | const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, desc->shader.id); |
| 9106 | SOKOL_VALIDATE(shd && shd->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_PIPELINEDESC_SHADER); |
| 9107 | for (int buf_index = 0; buf_index < SG_MAX_SHADERSTAGE_BUFFERS; buf_index++) { |
| 9108 | const sg_buffer_layout_desc* l_desc = &desc->layout.buffers[buf_index]; |
| 9109 | if (l_desc->stride == 0) { |
| 9110 | continue; |
| 9111 | } |
| 9112 | SOKOL_VALIDATE((l_desc->stride & 3) == 0, _SG_VALIDATE_PIPELINEDESC_LAYOUT_STRIDE4); |
| 9113 | } |
| 9114 | SOKOL_VALIDATE(desc->layout.attrs[0].format != SG_VERTEXFORMAT_INVALID, _SG_VALIDATE_PIPELINEDESC_NO_ATTRS); |
| 9115 | bool attrs_cont = true; |
| 9116 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 9117 | const sg_vertex_attr_desc* a_desc = &desc->layout.attrs[attr_index]; |
| 9118 | if (a_desc->format == SG_VERTEXFORMAT_INVALID) { |
| 9119 | attrs_cont = false; |
| 9120 | continue; |
| 9121 | } |
| 9122 | SOKOL_VALIDATE(attrs_cont, _SG_VALIDATE_PIPELINEDESC_NO_ATTRS); |
| 9123 | SOKOL_ASSERT(a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS); |
| 9124 | #if defined(SOKOL_GLES2) |
| 9125 | /* on GLES2, vertex attribute names must be provided */ |
| 9126 | SOKOL_VALIDATE(!_sg_strempty(&shd->attrs[attr_index].name), _SG_VALIDATE_PIPELINEDESC_ATTR_NAME); |
| 9127 | #elif defined(SOKOL_D3D11) |
| 9128 | /* on D3D11, semantic names (and semantic indices) must be provided */ |
| 9129 | SOKOL_VALIDATE(!_sg_strempty(&shd->attrs[attr_index].sem_name), _SG_VALIDATE_PIPELINEDESC_ATTR_SEMANTICS); |
| 9130 | #endif |
| 9131 | } |
| 9132 | return SOKOL_VALIDATE_END(); |
| 9133 | #endif |
| 9134 | } |
| 9135 | |
| 9136 | _SOKOL_PRIVATE bool _sg_validate_pass_desc(const sg_pass_desc* desc) { |
| 9137 | #if !defined(SOKOL_DEBUG) |
| 9138 | _SOKOL_UNUSED(desc); |
| 9139 | return true; |
| 9140 | #else |
| 9141 | SOKOL_ASSERT(desc); |
| 9142 | SOKOL_VALIDATE_BEGIN(); |
| 9143 | SOKOL_VALIDATE(desc->_start_canary == 0, _SG_VALIDATE_PASSDESC_CANARY); |
| 9144 | SOKOL_VALIDATE(desc->_end_canary == 0, _SG_VALIDATE_PASSDESC_CANARY); |
| 9145 | bool atts_cont = true; |
| 9146 | sg_pixel_format color_fmt = SG_PIXELFORMAT_NONE; |
| 9147 | int width = -1, height = -1, sample_count = -1; |
| 9148 | for (int att_index = 0; att_index < SG_MAX_COLOR_ATTACHMENTS; att_index++) { |
| 9149 | const sg_attachment_desc* att = &desc->color_attachments[att_index]; |
| 9150 | if (att->image.id == SG_INVALID_ID) { |
| 9151 | SOKOL_VALIDATE(att_index > 0, _SG_VALIDATE_PASSDESC_NO_COLOR_ATTS); |
| 9152 | atts_cont = false; |
| 9153 | continue; |
| 9154 | } |
| 9155 | SOKOL_VALIDATE(atts_cont, _SG_VALIDATE_PASSDESC_NO_CONT_COLOR_ATTS); |
| 9156 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, att->image.id); |
| 9157 | SOKOL_VALIDATE(img && img->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_PASSDESC_IMAGE); |
| 9158 | SOKOL_VALIDATE(att->mip_level < img->num_mipmaps, _SG_VALIDATE_PASSDESC_MIPLEVEL); |
| 9159 | if (img->type == SG_IMAGETYPE_CUBE) { |
| 9160 | SOKOL_VALIDATE(att->face < 6, _SG_VALIDATE_PASSDESC_FACE); |
| 9161 | } |
| 9162 | else if (img->type == SG_IMAGETYPE_ARRAY) { |
| 9163 | SOKOL_VALIDATE(att->layer < img->depth, _SG_VALIDATE_PASSDESC_LAYER); |
| 9164 | } |
| 9165 | else if (img->type == SG_IMAGETYPE_3D) { |
| 9166 | SOKOL_VALIDATE(att->slice < img->depth, _SG_VALIDATE_PASSDESC_SLICE); |
| 9167 | } |
| 9168 | SOKOL_VALIDATE(img->render_target, _SG_VALIDATE_PASSDESC_IMAGE_NO_RT); |
| 9169 | if (att_index == 0) { |
| 9170 | color_fmt = img->pixel_format; |
| 9171 | width = img->width >> att->mip_level; |
| 9172 | height = img->height >> att->mip_level; |
| 9173 | sample_count = img->sample_count; |
| 9174 | } |
| 9175 | else { |
| 9176 | SOKOL_VALIDATE(img->pixel_format == color_fmt, _SG_VALIDATE_PASSDESC_COLOR_PIXELFORMATS); |
| 9177 | SOKOL_VALIDATE(width == img->width >> att->mip_level, _SG_VALIDATE_PASSDESC_IMAGE_SIZES); |
| 9178 | SOKOL_VALIDATE(height == img->height >> att->mip_level, _SG_VALIDATE_PASSDESC_IMAGE_SIZES); |
| 9179 | SOKOL_VALIDATE(sample_count == img->sample_count, _SG_VALIDATE_PASSDESC_IMAGE_SAMPLE_COUNTS); |
| 9180 | } |
| 9181 | SOKOL_VALIDATE(_sg_is_valid_rendertarget_color_format(img->pixel_format), _SG_VALIDATE_PASSDESC_COLOR_INV_PIXELFORMAT); |
| 9182 | } |
| 9183 | if (desc->depth_stencil_attachment.image.id != SG_INVALID_ID) { |
| 9184 | const sg_attachment_desc* att = &desc->depth_stencil_attachment; |
| 9185 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, att->image.id); |
| 9186 | SOKOL_VALIDATE(img && img->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_PASSDESC_IMAGE); |
| 9187 | SOKOL_VALIDATE(att->mip_level < img->num_mipmaps, _SG_VALIDATE_PASSDESC_MIPLEVEL); |
| 9188 | if (img->type == SG_IMAGETYPE_CUBE) { |
| 9189 | SOKOL_VALIDATE(att->face < 6, _SG_VALIDATE_PASSDESC_FACE); |
| 9190 | } |
| 9191 | else if (img->type == SG_IMAGETYPE_ARRAY) { |
| 9192 | SOKOL_VALIDATE(att->layer < img->depth, _SG_VALIDATE_PASSDESC_LAYER); |
| 9193 | } |
| 9194 | else if (img->type == SG_IMAGETYPE_3D) { |
| 9195 | SOKOL_VALIDATE(att->slice < img->depth, _SG_VALIDATE_PASSDESC_SLICE); |
| 9196 | } |
| 9197 | SOKOL_VALIDATE(img->render_target, _SG_VALIDATE_PASSDESC_IMAGE_NO_RT); |
| 9198 | SOKOL_VALIDATE(width == img->width >> att->mip_level, _SG_VALIDATE_PASSDESC_IMAGE_SIZES); |
| 9199 | SOKOL_VALIDATE(height == img->height >> att->mip_level, _SG_VALIDATE_PASSDESC_IMAGE_SIZES); |
| 9200 | SOKOL_VALIDATE(sample_count == img->sample_count, _SG_VALIDATE_PASSDESC_IMAGE_SAMPLE_COUNTS); |
| 9201 | SOKOL_VALIDATE(_sg_is_valid_rendertarget_depth_format(img->pixel_format), _SG_VALIDATE_PASSDESC_DEPTH_INV_PIXELFORMAT); |
| 9202 | } |
| 9203 | return SOKOL_VALIDATE_END(); |
| 9204 | #endif |
| 9205 | } |
| 9206 | |
| 9207 | _SOKOL_PRIVATE bool _sg_validate_begin_pass(_sg_pass_t* pass) { |
| 9208 | #if !defined(SOKOL_DEBUG) |
| 9209 | _SOKOL_UNUSED(pass); |
| 9210 | return true; |
| 9211 | #else |
| 9212 | SOKOL_VALIDATE_BEGIN(); |
| 9213 | SOKOL_VALIDATE(pass->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_BEGINPASS_PASS); |
| 9214 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 9215 | const _sg_attachment_t* att = &pass->color_atts[i]; |
| 9216 | if (att->image) { |
| 9217 | SOKOL_VALIDATE(att->image->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_BEGINPASS_IMAGE); |
| 9218 | SOKOL_VALIDATE(att->image->slot.id == att->image_id.id, _SG_VALIDATE_BEGINPASS_IMAGE); |
| 9219 | } |
| 9220 | } |
| 9221 | if (pass->ds_att.image) { |
| 9222 | const _sg_attachment_t* att = &pass->ds_att; |
| 9223 | SOKOL_VALIDATE(att->image->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_BEGINPASS_IMAGE); |
| 9224 | SOKOL_VALIDATE(att->image->slot.id == att->image_id.id, _SG_VALIDATE_BEGINPASS_IMAGE); |
| 9225 | } |
| 9226 | return SOKOL_VALIDATE_END(); |
| 9227 | #endif |
| 9228 | } |
| 9229 | |
| 9230 | _SOKOL_PRIVATE bool _sg_validate_apply_pipeline(sg_pipeline pip_id) { |
| 9231 | #if !defined(SOKOL_DEBUG) |
| 9232 | _SOKOL_UNUSED(pip_id); |
| 9233 | return true; |
| 9234 | #else |
| 9235 | SOKOL_VALIDATE_BEGIN(); |
| 9236 | /* the pipeline object must be alive and valid */ |
| 9237 | SOKOL_VALIDATE(pip_id.id != SG_INVALID_ID, _SG_VALIDATE_APIP_PIPELINE_VALID_ID); |
| 9238 | const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 9239 | SOKOL_VALIDATE(pip != 0, _SG_VALIDATE_APIP_PIPELINE_EXISTS); |
| 9240 | if (!pip) { |
| 9241 | return SOKOL_VALIDATE_END(); |
| 9242 | } |
| 9243 | SOKOL_VALIDATE(pip->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_APIP_PIPELINE_VALID); |
| 9244 | /* the pipeline's shader must be alive and valid */ |
| 9245 | SOKOL_ASSERT(pip->shader); |
| 9246 | SOKOL_VALIDATE(pip->shader->slot.id == pip->shader_id.id, _SG_VALIDATE_APIP_SHADER_EXISTS); |
| 9247 | SOKOL_VALIDATE(pip->shader->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_APIP_SHADER_VALID); |
| 9248 | /* check that pipeline attributes match current pass attributes */ |
| 9249 | const _sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, _sg.cur_pass.id); |
| 9250 | if (pass) { |
| 9251 | /* an offscreen pass */ |
| 9252 | SOKOL_VALIDATE(pip->color_attachment_count == pass->num_color_atts, _SG_VALIDATE_APIP_ATT_COUNT); |
| 9253 | SOKOL_VALIDATE(pip->color_format == pass->color_atts[0].image->pixel_format, _SG_VALIDATE_APIP_COLOR_FORMAT); |
| 9254 | SOKOL_VALIDATE(pip->sample_count == pass->color_atts[0].image->sample_count, _SG_VALIDATE_APIP_SAMPLE_COUNT); |
| 9255 | if (pass->ds_att.image) { |
| 9256 | SOKOL_VALIDATE(pip->depth_format == pass->ds_att.image->pixel_format, _SG_VALIDATE_APIP_DEPTH_FORMAT); |
| 9257 | } |
| 9258 | else { |
| 9259 | SOKOL_VALIDATE(pip->depth_format == SG_PIXELFORMAT_NONE, _SG_VALIDATE_APIP_DEPTH_FORMAT); |
| 9260 | } |
| 9261 | } |
| 9262 | else { |
| 9263 | /* default pass */ |
| 9264 | SOKOL_VALIDATE(pip->color_attachment_count == 1, _SG_VALIDATE_APIP_ATT_COUNT); |
| 9265 | SOKOL_VALIDATE(pip->color_format == SG_PIXELFORMAT_RGBA8, _SG_VALIDATE_APIP_COLOR_FORMAT); |
| 9266 | SOKOL_VALIDATE(pip->depth_format == SG_PIXELFORMAT_DEPTHSTENCIL, _SG_VALIDATE_APIP_DEPTH_FORMAT); |
| 9267 | /* FIXME: hmm, we don't know if the default framebuffer is multisampled here */ |
| 9268 | } |
| 9269 | return SOKOL_VALIDATE_END(); |
| 9270 | #endif |
| 9271 | } |
| 9272 | |
| 9273 | _SOKOL_PRIVATE bool _sg_validate_apply_bindings(const sg_bindings* bindings) { |
| 9274 | #if !defined(SOKOL_DEBUG) |
| 9275 | _SOKOL_UNUSED(bindings); |
| 9276 | return true; |
| 9277 | #else |
| 9278 | SOKOL_VALIDATE_BEGIN(); |
| 9279 | |
| 9280 | /* a pipeline object must have been applied */ |
| 9281 | SOKOL_VALIDATE(_sg.cur_pipeline.id != SG_INVALID_ID, _SG_VALIDATE_ABND_PIPELINE); |
| 9282 | const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, _sg.cur_pipeline.id); |
| 9283 | SOKOL_VALIDATE(pip != 0, _SG_VALIDATE_ABND_PIPELINE_EXISTS); |
| 9284 | if (!pip) { |
| 9285 | return SOKOL_VALIDATE_END(); |
| 9286 | } |
| 9287 | SOKOL_VALIDATE(pip->slot.state == SG_RESOURCESTATE_VALID, _SG_VALIDATE_ABND_PIPELINE_VALID); |
| 9288 | SOKOL_ASSERT(pip->shader); |
| 9289 | |
| 9290 | /* has expected vertex buffers, and vertex buffers still exist */ |
| 9291 | for (int i = 0; i < SG_MAX_SHADERSTAGE_BUFFERS; i++) { |
| 9292 | if (bindings->vertex_buffers[i].id != SG_INVALID_ID) { |
| 9293 | SOKOL_VALIDATE(pip->vertex_layout_valid[i], _SG_VALIDATE_ABND_VBS); |
| 9294 | /* buffers in vertex-buffer-slots must be of type SG_BUFFERTYPE_VERTEXBUFFER */ |
| 9295 | const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, bindings->vertex_buffers[i].id); |
| 9296 | SOKOL_VALIDATE(buf != 0, _SG_VALIDATE_ABND_VB_EXISTS); |
| 9297 | if (buf && buf->slot.state == SG_RESOURCESTATE_VALID) { |
| 9298 | SOKOL_VALIDATE(SG_BUFFERTYPE_VERTEXBUFFER == buf->type, _SG_VALIDATE_ABND_VB_TYPE); |
| 9299 | SOKOL_VALIDATE(!buf->append_overflow, _SG_VALIDATE_ABND_VB_OVERFLOW); |
| 9300 | } |
| 9301 | } |
| 9302 | else { |
| 9303 | /* vertex buffer provided in a slot which has no vertex layout in pipeline */ |
| 9304 | SOKOL_VALIDATE(!pip->vertex_layout_valid[i], _SG_VALIDATE_ABND_VBS); |
| 9305 | } |
| 9306 | } |
| 9307 | |
| 9308 | /* index buffer expected or not, and index buffer still exists */ |
| 9309 | if (pip->index_type == SG_INDEXTYPE_NONE) { |
| 9310 | /* pipeline defines non-indexed rendering, but index buffer provided */ |
| 9311 | SOKOL_VALIDATE(bindings->index_buffer.id == SG_INVALID_ID, _SG_VALIDATE_ABND_IB); |
| 9312 | } |
| 9313 | else { |
| 9314 | /* pipeline defines indexed rendering, but no index buffer provided */ |
| 9315 | SOKOL_VALIDATE(bindings->index_buffer.id != SG_INVALID_ID, _SG_VALIDATE_ABND_NO_IB); |
| 9316 | } |
| 9317 | if (bindings->index_buffer.id != SG_INVALID_ID) { |
| 9318 | /* buffer in index-buffer-slot must be of type SG_BUFFERTYPE_INDEXBUFFER */ |
| 9319 | const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, bindings->index_buffer.id); |
| 9320 | SOKOL_VALIDATE(buf != 0, _SG_VALIDATE_ABND_IB_EXISTS); |
| 9321 | if (buf && buf->slot.state == SG_RESOURCESTATE_VALID) { |
| 9322 | SOKOL_VALIDATE(SG_BUFFERTYPE_INDEXBUFFER == buf->type, _SG_VALIDATE_ABND_IB_TYPE); |
| 9323 | SOKOL_VALIDATE(!buf->append_overflow, _SG_VALIDATE_ABND_IB_OVERFLOW); |
| 9324 | } |
| 9325 | } |
| 9326 | |
| 9327 | /* has expected vertex shader images */ |
| 9328 | for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++) { |
| 9329 | _sg_shader_stage_t* stage = &pip->shader->stage[SG_SHADERSTAGE_VS]; |
| 9330 | if (bindings->vs_images[i].id != SG_INVALID_ID) { |
| 9331 | SOKOL_VALIDATE(i < stage->num_images, _SG_VALIDATE_ABND_VS_IMGS); |
| 9332 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, bindings->vs_images[i].id); |
| 9333 | SOKOL_VALIDATE(img != 0, _SG_VALIDATE_ABND_VS_IMG_EXISTS); |
| 9334 | if (img && img->slot.state == SG_RESOURCESTATE_VALID) { |
| 9335 | SOKOL_VALIDATE(img->type == stage->images[i].type, _SG_VALIDATE_ABND_VS_IMG_TYPES); |
| 9336 | } |
| 9337 | } |
| 9338 | else { |
| 9339 | SOKOL_VALIDATE(i >= stage->num_images, _SG_VALIDATE_ABND_VS_IMGS); |
| 9340 | } |
| 9341 | } |
| 9342 | |
| 9343 | /* has expected fragment shader images */ |
| 9344 | for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++) { |
| 9345 | _sg_shader_stage_t* stage = &pip->shader->stage[SG_SHADERSTAGE_FS]; |
| 9346 | if (bindings->fs_images[i].id != SG_INVALID_ID) { |
| 9347 | SOKOL_VALIDATE(i < stage->num_images, _SG_VALIDATE_ABND_FS_IMGS); |
| 9348 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, bindings->fs_images[i].id); |
| 9349 | SOKOL_VALIDATE(img != 0, _SG_VALIDATE_ABND_FS_IMG_EXISTS); |
| 9350 | if (img && img->slot.state == SG_RESOURCESTATE_VALID) { |
| 9351 | SOKOL_VALIDATE(img->type == stage->images[i].type, _SG_VALIDATE_ABND_FS_IMG_TYPES); |
| 9352 | } |
| 9353 | } |
| 9354 | else { |
| 9355 | SOKOL_VALIDATE(i >= stage->num_images, _SG_VALIDATE_ABND_FS_IMGS); |
| 9356 | } |
| 9357 | } |
| 9358 | return SOKOL_VALIDATE_END(); |
| 9359 | #endif |
| 9360 | } |
| 9361 | |
| 9362 | _SOKOL_PRIVATE bool _sg_validate_apply_uniforms(sg_shader_stage stage_index, int ub_index, const void* data, int num_bytes) { |
| 9363 | _SOKOL_UNUSED(data); |
| 9364 | #if !defined(SOKOL_DEBUG) |
| 9365 | _SOKOL_UNUSED(stage_index); |
| 9366 | _SOKOL_UNUSED(ub_index); |
| 9367 | _SOKOL_UNUSED(num_bytes); |
| 9368 | return true; |
| 9369 | #else |
| 9370 | SOKOL_ASSERT((stage_index == SG_SHADERSTAGE_VS) || (stage_index == SG_SHADERSTAGE_FS)); |
| 9371 | SOKOL_ASSERT((ub_index >= 0) && (ub_index < SG_MAX_SHADERSTAGE_UBS)); |
| 9372 | SOKOL_VALIDATE_BEGIN(); |
| 9373 | SOKOL_VALIDATE(_sg.cur_pipeline.id != SG_INVALID_ID, _SG_VALIDATE_AUB_NO_PIPELINE); |
| 9374 | const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, _sg.cur_pipeline.id); |
| 9375 | SOKOL_ASSERT(pip && (pip->slot.id == _sg.cur_pipeline.id)); |
| 9376 | SOKOL_ASSERT(pip->shader && (pip->shader->slot.id == pip->shader_id.id)); |
| 9377 | |
| 9378 | /* check that there is a uniform block at 'stage' and 'ub_index' */ |
| 9379 | const _sg_shader_stage_t* stage = &pip->shader->stage[stage_index]; |
| 9380 | SOKOL_VALIDATE(ub_index < stage->num_uniform_blocks, _SG_VALIDATE_AUB_NO_UB_AT_SLOT); |
| 9381 | |
| 9382 | /* check that the provided data size doesn't exceed the uniform block size */ |
| 9383 | SOKOL_VALIDATE(num_bytes <= stage->uniform_blocks[ub_index].size, _SG_VALIDATE_AUB_SIZE); |
| 9384 | |
| 9385 | return SOKOL_VALIDATE_END(); |
| 9386 | #endif |
| 9387 | } |
| 9388 | |
| 9389 | _SOKOL_PRIVATE bool _sg_validate_update_buffer(const _sg_buffer_t* buf, const void* data, int size) { |
| 9390 | #if !defined(SOKOL_DEBUG) |
| 9391 | _SOKOL_UNUSED(buf); |
| 9392 | _SOKOL_UNUSED(data); |
| 9393 | _SOKOL_UNUSED(size); |
| 9394 | return true; |
| 9395 | #else |
| 9396 | SOKOL_ASSERT(buf && data); |
| 9397 | SOKOL_VALIDATE_BEGIN(); |
| 9398 | SOKOL_VALIDATE(buf->usage != SG_USAGE_IMMUTABLE, _SG_VALIDATE_UPDATEBUF_USAGE); |
| 9399 | SOKOL_VALIDATE(buf->size >= size, _SG_VALIDATE_UPDATEBUF_SIZE); |
| 9400 | SOKOL_VALIDATE(buf->update_frame_index != _sg.frame_index, _SG_VALIDATE_UPDATEBUF_ONCE); |
| 9401 | SOKOL_VALIDATE(buf->append_frame_index != _sg.frame_index, _SG_VALIDATE_UPDATEBUF_APPEND); |
| 9402 | return SOKOL_VALIDATE_END(); |
| 9403 | #endif |
| 9404 | } |
| 9405 | |
| 9406 | _SOKOL_PRIVATE bool _sg_validate_append_buffer(const _sg_buffer_t* buf, const void* data, int size) { |
| 9407 | #if !defined(SOKOL_DEBUG) |
| 9408 | _SOKOL_UNUSED(buf); |
| 9409 | _SOKOL_UNUSED(data); |
| 9410 | _SOKOL_UNUSED(size); |
| 9411 | return true; |
| 9412 | #else |
| 9413 | SOKOL_ASSERT(buf && data); |
| 9414 | SOKOL_VALIDATE_BEGIN(); |
| 9415 | SOKOL_VALIDATE(buf->usage != SG_USAGE_IMMUTABLE, _SG_VALIDATE_APPENDBUF_USAGE); |
| 9416 | SOKOL_VALIDATE(buf->size >= (buf->append_pos+size), _SG_VALIDATE_APPENDBUF_SIZE); |
| 9417 | SOKOL_VALIDATE(buf->update_frame_index != _sg.frame_index, _SG_VALIDATE_APPENDBUF_UPDATE); |
| 9418 | return SOKOL_VALIDATE_END(); |
| 9419 | #endif |
| 9420 | } |
| 9421 | |
| 9422 | _SOKOL_PRIVATE bool _sg_validate_update_image(const _sg_image_t* img, const sg_image_content* data) { |
| 9423 | #if !defined(SOKOL_DEBUG) |
| 9424 | _SOKOL_UNUSED(img); |
| 9425 | _SOKOL_UNUSED(data); |
| 9426 | return true; |
| 9427 | #else |
| 9428 | SOKOL_ASSERT(img && data); |
| 9429 | SOKOL_VALIDATE_BEGIN(); |
| 9430 | SOKOL_VALIDATE(img->usage != SG_USAGE_IMMUTABLE, _SG_VALIDATE_UPDIMG_USAGE); |
| 9431 | SOKOL_VALIDATE(img->upd_frame_index != _sg.frame_index, _SG_VALIDATE_UPDIMG_ONCE); |
| 9432 | SOKOL_VALIDATE(!_sg_is_compressed_pixel_format(img->pixel_format), _SG_VALIDATE_UPDIMG_COMPRESSED); |
| 9433 | const int num_faces = (img->type == SG_IMAGETYPE_CUBE) ? 6 : 1; |
| 9434 | const int num_mips = img->num_mipmaps; |
| 9435 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 9436 | for (int mip_index = 0; mip_index < num_mips; mip_index++) { |
| 9437 | SOKOL_VALIDATE(0 != data->subimage[face_index][mip_index].ptr, _SG_VALIDATE_UPDIMG_NOTENOUGHDATA); |
| 9438 | const int mip_width = _sg_max(img->width >> mip_index, 1); |
| 9439 | const int mip_height = _sg_max(img->height >> mip_index, 1); |
| 9440 | const int bytes_per_slice = _sg_surface_pitch(img->pixel_format, mip_width, mip_height); |
| 9441 | const int expected_size = bytes_per_slice * img->depth; |
| 9442 | SOKOL_VALIDATE(data->subimage[face_index][mip_index].size <= expected_size, _SG_VALIDATE_UPDIMG_SIZE); |
| 9443 | } |
| 9444 | } |
| 9445 | return SOKOL_VALIDATE_END(); |
| 9446 | #endif |
| 9447 | } |
| 9448 | |
| 9449 | /*== fill in desc default values =============================================*/ |
| 9450 | _SOKOL_PRIVATE sg_buffer_desc _sg_buffer_desc_defaults(const sg_buffer_desc* desc) { |
| 9451 | sg_buffer_desc def = *desc; |
| 9452 | def.type = _sg_def(def.type, SG_BUFFERTYPE_VERTEXBUFFER); |
| 9453 | def.usage = _sg_def(def.usage, SG_USAGE_IMMUTABLE); |
| 9454 | return def; |
| 9455 | } |
| 9456 | |
| 9457 | _SOKOL_PRIVATE sg_image_desc _sg_image_desc_defaults(const sg_image_desc* desc) { |
| 9458 | sg_image_desc def = *desc; |
| 9459 | def.type = _sg_def(def.type, SG_IMAGETYPE_2D); |
| 9460 | def.depth = _sg_def(def.depth, 1); |
| 9461 | def.num_mipmaps = _sg_def(def.num_mipmaps, 1); |
| 9462 | def.usage = _sg_def(def.usage, SG_USAGE_IMMUTABLE); |
| 9463 | def.pixel_format = _sg_def(def.pixel_format, SG_PIXELFORMAT_RGBA8); |
| 9464 | def.sample_count = _sg_def(def.sample_count, 1); |
| 9465 | def.min_filter = _sg_def(def.min_filter, SG_FILTER_NEAREST); |
| 9466 | def.mag_filter = _sg_def(def.mag_filter, SG_FILTER_NEAREST); |
| 9467 | def.wrap_u = _sg_def(def.wrap_u, SG_WRAP_REPEAT); |
| 9468 | def.wrap_v = _sg_def(def.wrap_v, SG_WRAP_REPEAT); |
| 9469 | def.wrap_w = _sg_def(def.wrap_w, SG_WRAP_REPEAT); |
| 9470 | def.max_anisotropy = _sg_def(def.max_anisotropy, 1); |
| 9471 | def.max_lod = _sg_def_flt(def.max_lod, FLT_MAX); |
| 9472 | return def; |
| 9473 | } |
| 9474 | |
| 9475 | _SOKOL_PRIVATE sg_shader_desc _sg_shader_desc_defaults(const sg_shader_desc* desc) { |
| 9476 | sg_shader_desc def = *desc; |
| 9477 | #if defined(SOKOL_METAL) |
| 9478 | def.vs.entry = _sg_def(def.vs.entry, "_main"); |
| 9479 | def.fs.entry = _sg_def(def.fs.entry, "_main"); |
| 9480 | #else |
| 9481 | def.vs.entry = _sg_def(def.vs.entry, "main"); |
| 9482 | def.fs.entry = _sg_def(def.fs.entry, "main"); |
| 9483 | #endif |
| 9484 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 9485 | sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS)? &def.vs : &def.fs; |
| 9486 | for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) { |
| 9487 | sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index]; |
| 9488 | if (0 == ub_desc->size) { |
| 9489 | break; |
| 9490 | } |
| 9491 | for (int u_index = 0; u_index < SG_MAX_UB_MEMBERS; u_index++) { |
| 9492 | sg_shader_uniform_desc* u_desc = &ub_desc->uniforms[u_index]; |
| 9493 | if (u_desc->type == SG_UNIFORMTYPE_INVALID) { |
| 9494 | break; |
| 9495 | } |
| 9496 | u_desc->array_count = _sg_def(u_desc->array_count, 1); |
| 9497 | } |
| 9498 | } |
| 9499 | } |
| 9500 | return def; |
| 9501 | } |
| 9502 | |
| 9503 | _SOKOL_PRIVATE sg_pipeline_desc _sg_pipeline_desc_defaults(const sg_pipeline_desc* desc) { |
| 9504 | sg_pipeline_desc def = *desc; |
| 9505 | |
| 9506 | def.primitive_type = _sg_def(def.primitive_type, SG_PRIMITIVETYPE_TRIANGLES); |
| 9507 | def.index_type = _sg_def(def.index_type, SG_INDEXTYPE_NONE); |
| 9508 | |
| 9509 | def.depth_stencil.stencil_front.fail_op = _sg_def(def.depth_stencil.stencil_front.fail_op, SG_STENCILOP_KEEP); |
| 9510 | def.depth_stencil.stencil_front.depth_fail_op = _sg_def(def.depth_stencil.stencil_front.depth_fail_op, SG_STENCILOP_KEEP); |
| 9511 | def.depth_stencil.stencil_front.pass_op = _sg_def(def.depth_stencil.stencil_front.pass_op, SG_STENCILOP_KEEP); |
| 9512 | def.depth_stencil.stencil_front.compare_func = _sg_def(def.depth_stencil.stencil_front.compare_func, SG_COMPAREFUNC_ALWAYS); |
| 9513 | def.depth_stencil.stencil_back.fail_op = _sg_def(def.depth_stencil.stencil_back.fail_op, SG_STENCILOP_KEEP); |
| 9514 | def.depth_stencil.stencil_back.depth_fail_op = _sg_def(def.depth_stencil.stencil_back.depth_fail_op, SG_STENCILOP_KEEP); |
| 9515 | def.depth_stencil.stencil_back.pass_op = _sg_def(def.depth_stencil.stencil_back.pass_op, SG_STENCILOP_KEEP); |
| 9516 | def.depth_stencil.stencil_back.compare_func = _sg_def(def.depth_stencil.stencil_back.compare_func, SG_COMPAREFUNC_ALWAYS); |
| 9517 | def.depth_stencil.depth_compare_func = _sg_def(def.depth_stencil.depth_compare_func, SG_COMPAREFUNC_ALWAYS); |
| 9518 | |
| 9519 | def.blend.src_factor_rgb = _sg_def(def.blend.src_factor_rgb, SG_BLENDFACTOR_ONE); |
| 9520 | def.blend.dst_factor_rgb = _sg_def(def.blend.dst_factor_rgb, SG_BLENDFACTOR_ZERO); |
| 9521 | def.blend.op_rgb = _sg_def(def.blend.op_rgb, SG_BLENDOP_ADD); |
| 9522 | def.blend.src_factor_alpha = _sg_def(def.blend.src_factor_alpha, SG_BLENDFACTOR_ONE); |
| 9523 | def.blend.dst_factor_alpha = _sg_def(def.blend.dst_factor_alpha, SG_BLENDFACTOR_ZERO); |
| 9524 | def.blend.op_alpha = _sg_def(def.blend.op_alpha, SG_BLENDOP_ADD); |
| 9525 | if (def.blend.color_write_mask == SG_COLORMASK_NONE) { |
| 9526 | def.blend.color_write_mask = 0; |
| 9527 | } |
| 9528 | else { |
| 9529 | def.blend.color_write_mask = (uint8_t) _sg_def((sg_color_mask)def.blend.color_write_mask, SG_COLORMASK_RGBA); |
| 9530 | } |
| 9531 | def.blend.color_attachment_count = _sg_def(def.blend.color_attachment_count, 1); |
| 9532 | def.blend.color_format = _sg_def(def.blend.color_format, SG_PIXELFORMAT_RGBA8); |
| 9533 | def.blend.depth_format = _sg_def(def.blend.depth_format, SG_PIXELFORMAT_DEPTHSTENCIL); |
| 9534 | |
| 9535 | def.rasterizer.cull_mode = _sg_def(def.rasterizer.cull_mode, SG_CULLMODE_NONE); |
| 9536 | def.rasterizer.face_winding = _sg_def(def.rasterizer.face_winding, SG_FACEWINDING_CW); |
| 9537 | def.rasterizer.sample_count = _sg_def(def.rasterizer.sample_count, 1); |
| 9538 | |
| 9539 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 9540 | sg_vertex_attr_desc* a_desc = &def.layout.attrs[attr_index]; |
| 9541 | if (a_desc->format == SG_VERTEXFORMAT_INVALID) { |
| 9542 | break; |
| 9543 | } |
| 9544 | SOKOL_ASSERT((a_desc->buffer_index >= 0) && (a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS)); |
| 9545 | sg_buffer_layout_desc* b_desc = &def.layout.buffers[a_desc->buffer_index]; |
| 9546 | b_desc->step_func = _sg_def(b_desc->step_func, SG_VERTEXSTEP_PER_VERTEX); |
| 9547 | b_desc->step_rate = _sg_def(b_desc->step_rate, 1); |
| 9548 | } |
| 9549 | |
| 9550 | /* resolve vertex layout strides and offsets */ |
| 9551 | int auto_offset[SG_MAX_SHADERSTAGE_BUFFERS]; |
| 9552 | memset(auto_offset, 0, sizeof(auto_offset)); |
| 9553 | bool use_auto_offset = true; |
| 9554 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 9555 | /* to use computed offsets, *all* attr offsets must be 0 */ |
| 9556 | if (def.layout.attrs[attr_index].offset != 0) { |
| 9557 | use_auto_offset = false; |
| 9558 | } |
| 9559 | } |
| 9560 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 9561 | sg_vertex_attr_desc* a_desc = &def.layout.attrs[attr_index]; |
| 9562 | if (a_desc->format == SG_VERTEXFORMAT_INVALID) { |
| 9563 | break; |
| 9564 | } |
| 9565 | SOKOL_ASSERT((a_desc->buffer_index >= 0) && (a_desc->buffer_index < SG_MAX_SHADERSTAGE_BUFFERS)); |
| 9566 | if (use_auto_offset) { |
| 9567 | a_desc->offset = auto_offset[a_desc->buffer_index]; |
| 9568 | } |
| 9569 | auto_offset[a_desc->buffer_index] += _sg_vertexformat_bytesize(a_desc->format); |
| 9570 | } |
| 9571 | /* compute vertex strides if needed */ |
| 9572 | for (int buf_index = 0; buf_index < SG_MAX_SHADERSTAGE_BUFFERS; buf_index++) { |
| 9573 | sg_buffer_layout_desc* l_desc = &def.layout.buffers[buf_index]; |
| 9574 | if (l_desc->stride == 0) { |
| 9575 | l_desc->stride = auto_offset[buf_index]; |
| 9576 | } |
| 9577 | } |
| 9578 | |
| 9579 | return def; |
| 9580 | } |
| 9581 | |
| 9582 | _SOKOL_PRIVATE sg_pass_desc _sg_pass_desc_defaults(const sg_pass_desc* desc) { |
| 9583 | /* FIXME: no values to replace in sg_pass_desc? */ |
| 9584 | sg_pass_desc def = *desc; |
| 9585 | return def; |
| 9586 | } |
| 9587 | |
| 9588 | /*== allocate/initialize resource private functions ==========================*/ |
| 9589 | _SOKOL_PRIVATE sg_buffer _sg_alloc_buffer(void) { |
| 9590 | sg_buffer res; |
| 9591 | int slot_index = _sg_pool_alloc_index(&_sg.pools.buffer_pool); |
| 9592 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 9593 | res.id = _sg_slot_alloc(&_sg.pools.buffer_pool, &_sg.pools.buffers[slot_index].slot, slot_index); |
| 9594 | } |
| 9595 | else { |
| 9596 | /* pool is exhausted */ |
| 9597 | res.id = SG_INVALID_ID; |
| 9598 | } |
| 9599 | return res; |
| 9600 | } |
| 9601 | |
| 9602 | _SOKOL_PRIVATE sg_image _sg_alloc_image(void) { |
| 9603 | sg_image res; |
| 9604 | int slot_index = _sg_pool_alloc_index(&_sg.pools.image_pool); |
| 9605 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 9606 | res.id = _sg_slot_alloc(&_sg.pools.image_pool, &_sg.pools.images[slot_index].slot, slot_index); |
| 9607 | } |
| 9608 | else { |
| 9609 | /* pool is exhausted */ |
| 9610 | res.id = SG_INVALID_ID; |
| 9611 | } |
| 9612 | return res; |
| 9613 | } |
| 9614 | |
| 9615 | _SOKOL_PRIVATE sg_shader _sg_alloc_shader(void) { |
| 9616 | sg_shader res; |
| 9617 | int slot_index = _sg_pool_alloc_index(&_sg.pools.shader_pool); |
| 9618 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 9619 | res.id = _sg_slot_alloc(&_sg.pools.shader_pool, &_sg.pools.shaders[slot_index].slot, slot_index); |
| 9620 | } |
| 9621 | else { |
| 9622 | /* pool is exhausted */ |
| 9623 | res.id = SG_INVALID_ID; |
| 9624 | } |
| 9625 | return res; |
| 9626 | } |
| 9627 | |
| 9628 | _SOKOL_PRIVATE sg_pipeline _sg_alloc_pipeline(void) { |
| 9629 | sg_pipeline res; |
| 9630 | int slot_index = _sg_pool_alloc_index(&_sg.pools.pipeline_pool); |
| 9631 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 9632 | res.id =_sg_slot_alloc(&_sg.pools.pipeline_pool, &_sg.pools.pipelines[slot_index].slot, slot_index); |
| 9633 | } |
| 9634 | else { |
| 9635 | /* pool is exhausted */ |
| 9636 | res.id = SG_INVALID_ID; |
| 9637 | } |
| 9638 | return res; |
| 9639 | } |
| 9640 | |
| 9641 | _SOKOL_PRIVATE sg_pass _sg_alloc_pass(void) { |
| 9642 | sg_pass res; |
| 9643 | int slot_index = _sg_pool_alloc_index(&_sg.pools.pass_pool); |
| 9644 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 9645 | res.id = _sg_slot_alloc(&_sg.pools.pass_pool, &_sg.pools.passes[slot_index].slot, slot_index); |
| 9646 | } |
| 9647 | else { |
| 9648 | /* pool is exhausted */ |
| 9649 | res.id = SG_INVALID_ID; |
| 9650 | } |
| 9651 | return res; |
| 9652 | } |
| 9653 | |
| 9654 | _SOKOL_PRIVATE void _sg_init_buffer(sg_buffer buf_id, const sg_buffer_desc* desc) { |
| 9655 | SOKOL_ASSERT(buf_id.id != SG_INVALID_ID && desc); |
| 9656 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 9657 | SOKOL_ASSERT(buf && buf->slot.state == SG_RESOURCESTATE_ALLOC); |
| 9658 | buf->slot.ctx_id = _sg.active_context.id; |
| 9659 | if (_sg_validate_buffer_desc(desc)) { |
| 9660 | buf->slot.state = _sg_create_buffer(buf, desc); |
| 9661 | } |
| 9662 | else { |
| 9663 | buf->slot.state = SG_RESOURCESTATE_FAILED; |
| 9664 | } |
| 9665 | SOKOL_ASSERT((buf->slot.state == SG_RESOURCESTATE_VALID)||(buf->slot.state == SG_RESOURCESTATE_FAILED)); |
| 9666 | } |
| 9667 | |
| 9668 | _SOKOL_PRIVATE void _sg_init_image(sg_image img_id, const sg_image_desc* desc) { |
| 9669 | SOKOL_ASSERT(img_id.id != SG_INVALID_ID && desc); |
| 9670 | _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 9671 | SOKOL_ASSERT(img && img->slot.state == SG_RESOURCESTATE_ALLOC); |
| 9672 | img->slot.ctx_id = _sg.active_context.id; |
| 9673 | if (_sg_validate_image_desc(desc)) { |
| 9674 | img->slot.state = _sg_create_image(img, desc); |
| 9675 | } |
| 9676 | else { |
| 9677 | img->slot.state = SG_RESOURCESTATE_FAILED; |
| 9678 | } |
| 9679 | SOKOL_ASSERT((img->slot.state == SG_RESOURCESTATE_VALID)||(img->slot.state == SG_RESOURCESTATE_FAILED)); |
| 9680 | } |
| 9681 | |
| 9682 | _SOKOL_PRIVATE void _sg_init_shader(sg_shader shd_id, const sg_shader_desc* desc) { |
| 9683 | SOKOL_ASSERT(shd_id.id != SG_INVALID_ID && desc); |
| 9684 | _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 9685 | SOKOL_ASSERT(shd && shd->slot.state == SG_RESOURCESTATE_ALLOC); |
| 9686 | shd->slot.ctx_id = _sg.active_context.id; |
| 9687 | if (_sg_validate_shader_desc(desc)) { |
| 9688 | shd->slot.state = _sg_create_shader(shd, desc); |
| 9689 | } |
| 9690 | else { |
| 9691 | shd->slot.state = SG_RESOURCESTATE_FAILED; |
| 9692 | } |
| 9693 | SOKOL_ASSERT((shd->slot.state == SG_RESOURCESTATE_VALID)||(shd->slot.state == SG_RESOURCESTATE_FAILED)); |
| 9694 | } |
| 9695 | |
| 9696 | _SOKOL_PRIVATE void _sg_init_pipeline(sg_pipeline pip_id, const sg_pipeline_desc* desc) { |
| 9697 | SOKOL_ASSERT(pip_id.id != SG_INVALID_ID && desc); |
| 9698 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 9699 | SOKOL_ASSERT(pip && pip->slot.state == SG_RESOURCESTATE_ALLOC); |
| 9700 | pip->slot.ctx_id = _sg.active_context.id; |
| 9701 | if (_sg_validate_pipeline_desc(desc)) { |
| 9702 | _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, desc->shader.id); |
| 9703 | SOKOL_ASSERT(shd && shd->slot.state == SG_RESOURCESTATE_VALID); |
| 9704 | pip->slot.state = _sg_create_pipeline(pip, shd, desc); |
| 9705 | } |
| 9706 | else { |
| 9707 | pip->slot.state = SG_RESOURCESTATE_FAILED; |
| 9708 | } |
| 9709 | SOKOL_ASSERT((pip->slot.state == SG_RESOURCESTATE_VALID)||(pip->slot.state == SG_RESOURCESTATE_FAILED)); |
| 9710 | } |
| 9711 | |
| 9712 | _SOKOL_PRIVATE void _sg_init_pass(sg_pass pass_id, const sg_pass_desc* desc) { |
| 9713 | SOKOL_ASSERT(pass_id.id != SG_INVALID_ID && desc); |
| 9714 | _sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id); |
| 9715 | SOKOL_ASSERT(pass && pass->slot.state == SG_RESOURCESTATE_ALLOC); |
| 9716 | pass->slot.ctx_id = _sg.active_context.id; |
| 9717 | if (_sg_validate_pass_desc(desc)) { |
| 9718 | /* lookup pass attachment image pointers */ |
| 9719 | _sg_image_t* att_imgs[SG_MAX_COLOR_ATTACHMENTS + 1]; |
| 9720 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 9721 | if (desc->color_attachments[i].image.id) { |
| 9722 | att_imgs[i] = _sg_lookup_image(&_sg.pools, desc->color_attachments[i].image.id); |
| 9723 | SOKOL_ASSERT(att_imgs[i] && att_imgs[i]->slot.state == SG_RESOURCESTATE_VALID); |
| 9724 | } |
| 9725 | else { |
| 9726 | att_imgs[i] = 0; |
| 9727 | } |
| 9728 | } |
| 9729 | const int ds_att_index = SG_MAX_COLOR_ATTACHMENTS; |
| 9730 | if (desc->depth_stencil_attachment.image.id) { |
| 9731 | att_imgs[ds_att_index] = _sg_lookup_image(&_sg.pools, desc->depth_stencil_attachment.image.id); |
| 9732 | SOKOL_ASSERT(att_imgs[ds_att_index] && att_imgs[ds_att_index]->slot.state == SG_RESOURCESTATE_VALID); |
| 9733 | } |
| 9734 | else { |
| 9735 | att_imgs[ds_att_index] = 0; |
| 9736 | } |
| 9737 | pass->slot.state = _sg_create_pass(pass, att_imgs, desc); |
| 9738 | } |
| 9739 | else { |
| 9740 | pass->slot.state = SG_RESOURCESTATE_FAILED; |
| 9741 | } |
| 9742 | SOKOL_ASSERT((pass->slot.state == SG_RESOURCESTATE_VALID)||(pass->slot.state == SG_RESOURCESTATE_FAILED)); |
| 9743 | } |
| 9744 | |
| 9745 | /*== PUBLIC API FUNCTIONS ====================================================*/ |
| 9746 | SOKOL_API_IMPL void sg_setup(const sg_desc* desc) { |
| 9747 | SOKOL_ASSERT(desc); |
| 9748 | SOKOL_ASSERT((desc->_start_canary == 0) && (desc->_end_canary == 0)); |
| 9749 | memset(&_sg, 0, sizeof(_sg)); |
| 9750 | _sg.desc = *desc; |
| 9751 | |
| 9752 | /* replace zero-init items with their default values */ |
| 9753 | _sg.desc.buffer_pool_size = _sg_def(_sg.desc.buffer_pool_size, _SG_DEFAULT_BUFFER_POOL_SIZE); |
| 9754 | _sg.desc.image_pool_size = _sg_def(_sg.desc.image_pool_size, _SG_DEFAULT_IMAGE_POOL_SIZE); |
| 9755 | _sg.desc.shader_pool_size = _sg_def(_sg.desc.shader_pool_size, _SG_DEFAULT_SHADER_POOL_SIZE); |
| 9756 | _sg.desc.pipeline_pool_size = _sg_def(_sg.desc.pipeline_pool_size, _SG_DEFAULT_PIPELINE_POOL_SIZE); |
| 9757 | _sg.desc.pass_pool_size = _sg_def(_sg.desc.pass_pool_size, _SG_DEFAULT_PASS_POOL_SIZE); |
| 9758 | _sg.desc.context_pool_size = _sg_def(_sg.desc.context_pool_size, _SG_DEFAULT_CONTEXT_POOL_SIZE); |
| 9759 | _sg.desc.mtl_global_uniform_buffer_size = _sg_def(_sg.desc.mtl_global_uniform_buffer_size, _SG_MTL_DEFAULT_UB_SIZE); |
| 9760 | _sg.desc.mtl_sampler_cache_size = _sg_def(_sg.desc.mtl_sampler_cache_size, _SG_MTL_DEFAULT_SAMPLER_CACHE_CAPACITY); |
| 9761 | |
| 9762 | _sg_setup_pools(&_sg.pools, &_sg.desc); |
| 9763 | _sg.frame_index = 1; |
| 9764 | _sg_setup_backend(&_sg.desc); |
| 9765 | sg_setup_context(); |
| 9766 | _sg.valid = true; |
| 9767 | } |
| 9768 | |
| 9769 | SOKOL_API_IMPL void sg_shutdown(void) { |
| 9770 | /* can only delete resources for the currently set context here, if multiple |
| 9771 | contexts are used, the app code must take care of properly releasing them |
| 9772 | (since only the app code can switch between 3D-API contexts) |
| 9773 | */ |
| 9774 | if (_sg.active_context.id != SG_INVALID_ID) { |
| 9775 | _sg_context_t* ctx = _sg_lookup_context(&_sg.pools, _sg.active_context.id); |
| 9776 | if (ctx) { |
| 9777 | _sg_destroy_all_resources(&_sg.pools, _sg.active_context.id); |
| 9778 | _sg_destroy_context(ctx); |
| 9779 | } |
| 9780 | } |
| 9781 | _sg_discard_backend(); |
| 9782 | _sg_discard_pools(&_sg.pools); |
| 9783 | _sg.valid = false; |
| 9784 | } |
| 9785 | |
| 9786 | SOKOL_API_IMPL bool sg_isvalid(void) { |
| 9787 | return _sg.valid; |
| 9788 | } |
| 9789 | |
| 9790 | SOKOL_API_IMPL sg_desc sg_query_desc(void) { |
| 9791 | return _sg.desc; |
| 9792 | } |
| 9793 | |
| 9794 | SOKOL_API_IMPL sg_backend sg_query_backend(void) { |
| 9795 | #if defined(SOKOL_GLCORE33) |
| 9796 | return SG_BACKEND_GLCORE33; |
| 9797 | #elif defined(SOKOL_GLES2) |
| 9798 | return SG_BACKEND_GLES2; |
| 9799 | #elif defined(SOKOL_GLES3) |
| 9800 | return _sg.gl.gles2 ? SG_BACKEND_GLES2 : SG_BACKEND_GLES3; |
| 9801 | #elif defined(SOKOL_D3D11) |
| 9802 | return SG_BACKEND_D3D11; |
| 9803 | #elif defined(SOKOL_METAL) |
| 9804 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 9805 | return SG_BACKEND_METAL_MACOS; |
| 9806 | #else |
| 9807 | #if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR |
| 9808 | return SG_BACKEND_METAL_SIMULATOR; |
| 9809 | #else |
| 9810 | return SG_BACKEND_METAL_IOS; |
| 9811 | #endif |
| 9812 | #endif |
| 9813 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 9814 | return SG_BACKEND_DUMMY; |
| 9815 | #endif |
| 9816 | } |
| 9817 | |
| 9818 | SOKOL_API_IMPL bool sg_query_feature(sg_feature f) { |
| 9819 | bool res = _sg_query_feature(f); |
| 9820 | _SG_TRACE_ARGS(query_feature, f, res); |
| 9821 | return res; |
| 9822 | } |
| 9823 | |
| 9824 | SOKOL_API_IMPL sg_context sg_setup_context(void) { |
| 9825 | sg_context res; |
| 9826 | int slot_index = _sg_pool_alloc_index(&_sg.pools.context_pool); |
| 9827 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 9828 | res.id = _sg_slot_alloc(&_sg.pools.context_pool, &_sg.pools.contexts[slot_index].slot, slot_index); |
| 9829 | _sg_context_t* ctx = _sg_context_at(&_sg.pools, res.id); |
| 9830 | ctx->slot.state = _sg_create_context(ctx); |
| 9831 | SOKOL_ASSERT(ctx->slot.state == SG_RESOURCESTATE_VALID); |
| 9832 | _sg_activate_context(ctx); |
| 9833 | } |
| 9834 | else { |
| 9835 | /* pool is exhausted */ |
| 9836 | res.id = SG_INVALID_ID; |
| 9837 | } |
| 9838 | _sg.active_context = res; |
| 9839 | return res; |
| 9840 | } |
| 9841 | |
| 9842 | SOKOL_API_IMPL void sg_discard_context(sg_context ctx_id) { |
| 9843 | _sg_destroy_all_resources(&_sg.pools, ctx_id.id); |
| 9844 | _sg_context_t* ctx = _sg_lookup_context(&_sg.pools, ctx_id.id); |
| 9845 | if (ctx) { |
| 9846 | _sg_destroy_context(ctx); |
| 9847 | _sg_reset_context(ctx); |
| 9848 | _sg_pool_free_index(&_sg.pools.context_pool, _sg_slot_index(ctx_id.id)); |
| 9849 | } |
| 9850 | _sg.active_context.id = SG_INVALID_ID; |
| 9851 | _sg_activate_context(0); |
| 9852 | } |
| 9853 | |
| 9854 | SOKOL_API_IMPL void sg_activate_context(sg_context ctx_id) { |
| 9855 | _sg.active_context = ctx_id; |
| 9856 | _sg_context_t* ctx = _sg_lookup_context(&_sg.pools, ctx_id.id); |
| 9857 | /* NOTE: ctx can be 0 here if the context is no longer valid */ |
| 9858 | _sg_activate_context(ctx); |
| 9859 | } |
| 9860 | |
| 9861 | SOKOL_API_IMPL sg_trace_hooks sg_install_trace_hooks(const sg_trace_hooks* trace_hooks) { |
| 9862 | SOKOL_ASSERT(trace_hooks); |
| 9863 | #if defined(SOKOL_TRACE_HOOKS) |
| 9864 | sg_trace_hooks old_hooks = _sg.hooks; |
| 9865 | _sg.hooks = *trace_hooks; |
| 9866 | #else |
| 9867 | static sg_trace_hooks old_hooks; |
| 9868 | SOKOL_LOG("sg_install_trace_hooks() called, but SG_TRACE_HOOKS is not defined!"); |
| 9869 | #endif |
| 9870 | return old_hooks; |
| 9871 | } |
| 9872 | |
| 9873 | SOKOL_API_IMPL sg_buffer sg_alloc_buffer(void) { |
| 9874 | sg_buffer res = _sg_alloc_buffer(); |
| 9875 | _SG_TRACE_ARGS(alloc_buffer, res); |
| 9876 | return res; |
| 9877 | } |
| 9878 | |
| 9879 | SOKOL_API_IMPL sg_image sg_alloc_image(void) { |
| 9880 | sg_image res = _sg_alloc_image(); |
| 9881 | _SG_TRACE_ARGS(alloc_image, res); |
| 9882 | return res; |
| 9883 | } |
| 9884 | |
| 9885 | SOKOL_API_IMPL sg_shader sg_alloc_shader(void) { |
| 9886 | sg_shader res = _sg_alloc_shader(); |
| 9887 | _SG_TRACE_ARGS(alloc_shader, res); |
| 9888 | return res; |
| 9889 | } |
| 9890 | |
| 9891 | SOKOL_API_IMPL sg_pipeline sg_alloc_pipeline(void) { |
| 9892 | sg_pipeline res = _sg_alloc_pipeline(); |
| 9893 | _SG_TRACE_ARGS(alloc_pipeline, res); |
| 9894 | return res; |
| 9895 | } |
| 9896 | |
| 9897 | SOKOL_API_IMPL sg_pass sg_alloc_pass(void) { |
| 9898 | sg_pass res = _sg_alloc_pass(); |
| 9899 | _SG_TRACE_ARGS(alloc_pass, res); |
| 9900 | return res; |
| 9901 | } |
| 9902 | |
| 9903 | SOKOL_API_IMPL void sg_init_buffer(sg_buffer buf_id, const sg_buffer_desc* desc) { |
| 9904 | sg_buffer_desc desc_def = _sg_buffer_desc_defaults(desc); |
| 9905 | _sg_init_buffer(buf_id, &desc_def); |
| 9906 | _SG_TRACE_ARGS(init_buffer, buf_id, &desc_def); |
| 9907 | } |
| 9908 | |
| 9909 | SOKOL_API_IMPL void sg_init_image(sg_image img_id, const sg_image_desc* desc) { |
| 9910 | sg_image_desc desc_def = _sg_image_desc_defaults(desc); |
| 9911 | _sg_init_image(img_id, &desc_def); |
| 9912 | _SG_TRACE_ARGS(init_image, img_id, &desc_def); |
| 9913 | } |
| 9914 | |
| 9915 | SOKOL_API_IMPL void sg_init_shader(sg_shader shd_id, const sg_shader_desc* desc) { |
| 9916 | sg_shader_desc desc_def = _sg_shader_desc_defaults(desc); |
| 9917 | _sg_init_shader(shd_id, &desc_def); |
| 9918 | _SG_TRACE_ARGS(init_shader, shd_id, &desc_def); |
| 9919 | } |
| 9920 | |
| 9921 | SOKOL_API_IMPL void sg_init_pipeline(sg_pipeline pip_id, const sg_pipeline_desc* desc) { |
| 9922 | sg_pipeline_desc desc_def = _sg_pipeline_desc_defaults(desc); |
| 9923 | _sg_init_pipeline(pip_id, &desc_def); |
| 9924 | _SG_TRACE_ARGS(init_pipeline, pip_id, &desc_def); |
| 9925 | } |
| 9926 | |
| 9927 | SOKOL_API_IMPL void sg_init_pass(sg_pass pass_id, const sg_pass_desc* desc) { |
| 9928 | sg_pass_desc desc_def = _sg_pass_desc_defaults(desc); |
| 9929 | _sg_init_pass(pass_id, &desc_def); |
| 9930 | _SG_TRACE_ARGS(init_pass, pass_id, &desc_def); |
| 9931 | } |
| 9932 | |
| 9933 | /*-- set allocated resource to failed state ----------------------------------*/ |
| 9934 | SOKOL_API_IMPL void sg_fail_buffer(sg_buffer buf_id) { |
| 9935 | SOKOL_ASSERT(buf_id.id != SG_INVALID_ID); |
| 9936 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 9937 | SOKOL_ASSERT(buf && buf->slot.state == SG_RESOURCESTATE_ALLOC); |
| 9938 | buf->slot.ctx_id = _sg.active_context.id; |
| 9939 | buf->slot.state = SG_RESOURCESTATE_FAILED; |
| 9940 | _SG_TRACE_ARGS(fail_buffer, buf_id); |
| 9941 | } |
| 9942 | |
| 9943 | SOKOL_API_IMPL void sg_fail_image(sg_image img_id) { |
| 9944 | SOKOL_ASSERT(img_id.id != SG_INVALID_ID); |
| 9945 | _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 9946 | SOKOL_ASSERT(img && img->slot.state == SG_RESOURCESTATE_ALLOC); |
| 9947 | img->slot.ctx_id = _sg.active_context.id; |
| 9948 | img->slot.state = SG_RESOURCESTATE_FAILED; |
| 9949 | _SG_TRACE_ARGS(fail_image, img_id); |
| 9950 | } |
| 9951 | |
| 9952 | SOKOL_API_IMPL void sg_fail_shader(sg_shader shd_id) { |
| 9953 | SOKOL_ASSERT(shd_id.id != SG_INVALID_ID); |
| 9954 | _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 9955 | SOKOL_ASSERT(shd && shd->slot.state == SG_RESOURCESTATE_ALLOC); |
| 9956 | shd->slot.ctx_id = _sg.active_context.id; |
| 9957 | shd->slot.state = SG_RESOURCESTATE_FAILED; |
| 9958 | _SG_TRACE_ARGS(fail_shader, shd_id); |
| 9959 | } |
| 9960 | |
| 9961 | SOKOL_API_IMPL void sg_fail_pipeline(sg_pipeline pip_id) { |
| 9962 | SOKOL_ASSERT(pip_id.id != SG_INVALID_ID); |
| 9963 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 9964 | SOKOL_ASSERT(pip && pip->slot.state == SG_RESOURCESTATE_ALLOC); |
| 9965 | pip->slot.ctx_id = _sg.active_context.id; |
| 9966 | pip->slot.state = SG_RESOURCESTATE_FAILED; |
| 9967 | _SG_TRACE_ARGS(fail_pipeline, pip_id); |
| 9968 | } |
| 9969 | |
| 9970 | SOKOL_API_IMPL void sg_fail_pass(sg_pass pass_id) { |
| 9971 | SOKOL_ASSERT(pass_id.id != SG_INVALID_ID); |
| 9972 | _sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id); |
| 9973 | SOKOL_ASSERT(pass && pass->slot.state == SG_RESOURCESTATE_ALLOC); |
| 9974 | pass->slot.ctx_id = _sg.active_context.id; |
| 9975 | pass->slot.state = SG_RESOURCESTATE_FAILED; |
| 9976 | _SG_TRACE_ARGS(fail_pass, pass_id); |
| 9977 | } |
| 9978 | |
| 9979 | /*-- get resource state */ |
| 9980 | SOKOL_API_IMPL sg_resource_state sg_query_buffer_state(sg_buffer buf_id) { |
| 9981 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 9982 | sg_resource_state res = buf ? buf->slot.state : SG_RESOURCESTATE_INVALID; |
| 9983 | return res; |
| 9984 | } |
| 9985 | |
| 9986 | SOKOL_API_IMPL sg_resource_state sg_query_image_state(sg_image img_id) { |
| 9987 | _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 9988 | sg_resource_state res = img ? img->slot.state : SG_RESOURCESTATE_INVALID; |
| 9989 | return res; |
| 9990 | } |
| 9991 | |
| 9992 | SOKOL_API_IMPL sg_resource_state sg_query_shader_state(sg_shader shd_id) { |
| 9993 | _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 9994 | sg_resource_state res = shd ? shd->slot.state : SG_RESOURCESTATE_INVALID; |
| 9995 | return res; |
| 9996 | } |
| 9997 | |
| 9998 | SOKOL_API_IMPL sg_resource_state sg_query_pipeline_state(sg_pipeline pip_id) { |
| 9999 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 10000 | sg_resource_state res = pip ? pip->slot.state : SG_RESOURCESTATE_INVALID; |
| 10001 | return res; |
| 10002 | } |
| 10003 | |
| 10004 | SOKOL_API_IMPL sg_resource_state sg_query_pass_state(sg_pass pass_id) { |
| 10005 | _sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id); |
| 10006 | sg_resource_state res = pass ? pass->slot.state : SG_RESOURCESTATE_INVALID; |
| 10007 | return res; |
| 10008 | } |
| 10009 | |
| 10010 | /*-- allocate and initialize resource ----------------------------------------*/ |
| 10011 | SOKOL_API_IMPL sg_buffer sg_make_buffer(const sg_buffer_desc* desc) { |
| 10012 | SOKOL_ASSERT(desc); |
| 10013 | sg_buffer_desc desc_def = _sg_buffer_desc_defaults(desc); |
| 10014 | sg_buffer buf_id = _sg_alloc_buffer(); |
| 10015 | if (buf_id.id != SG_INVALID_ID) { |
| 10016 | _sg_init_buffer(buf_id, &desc_def); |
| 10017 | } |
| 10018 | else { |
| 10019 | SOKOL_LOG("buffer pool exhausted!"); |
| 10020 | _SG_TRACE_NOARGS(err_buffer_pool_exhausted); |
| 10021 | } |
| 10022 | _SG_TRACE_ARGS(make_buffer, &desc_def, buf_id); |
| 10023 | return buf_id; |
| 10024 | } |
| 10025 | |
| 10026 | SOKOL_API_IMPL sg_image sg_make_image(const sg_image_desc* desc) { |
| 10027 | SOKOL_ASSERT(desc); |
| 10028 | sg_image_desc desc_def = _sg_image_desc_defaults(desc); |
| 10029 | sg_image img_id = _sg_alloc_image(); |
| 10030 | if (img_id.id != SG_INVALID_ID) { |
| 10031 | _sg_init_image(img_id, &desc_def); |
| 10032 | } |
| 10033 | else { |
| 10034 | SOKOL_LOG("image pool exhausted!"); |
| 10035 | _SG_TRACE_NOARGS(err_image_pool_exhausted); |
| 10036 | } |
| 10037 | _SG_TRACE_ARGS(make_image, &desc_def, img_id); |
| 10038 | return img_id; |
| 10039 | } |
| 10040 | |
| 10041 | SOKOL_API_IMPL sg_shader sg_make_shader(const sg_shader_desc* desc) { |
| 10042 | SOKOL_ASSERT(desc); |
| 10043 | sg_shader_desc desc_def = _sg_shader_desc_defaults(desc); |
| 10044 | sg_shader shd_id = _sg_alloc_shader(); |
| 10045 | if (shd_id.id != SG_INVALID_ID) { |
| 10046 | _sg_init_shader(shd_id, &desc_def); |
| 10047 | } |
| 10048 | else { |
| 10049 | SOKOL_LOG("shader pool exhausted!"); |
| 10050 | _SG_TRACE_NOARGS(err_shader_pool_exhausted); |
| 10051 | } |
| 10052 | _SG_TRACE_ARGS(make_shader, &desc_def, shd_id); |
| 10053 | return shd_id; |
| 10054 | } |
| 10055 | |
| 10056 | SOKOL_API_IMPL sg_pipeline sg_make_pipeline(const sg_pipeline_desc* desc) { |
| 10057 | SOKOL_ASSERT(desc); |
| 10058 | sg_pipeline_desc desc_def = _sg_pipeline_desc_defaults(desc); |
| 10059 | sg_pipeline pip_id = _sg_alloc_pipeline(); |
| 10060 | if (pip_id.id != SG_INVALID_ID) { |
| 10061 | _sg_init_pipeline(pip_id, &desc_def); |
| 10062 | } |
| 10063 | else { |
| 10064 | SOKOL_LOG("pipeline pool exhausted!"); |
| 10065 | _SG_TRACE_NOARGS(err_pipeline_pool_exhausted); |
| 10066 | } |
| 10067 | _SG_TRACE_ARGS(make_pipeline, &desc_def, pip_id); |
| 10068 | return pip_id; |
| 10069 | } |
| 10070 | |
| 10071 | SOKOL_API_IMPL sg_pass sg_make_pass(const sg_pass_desc* desc) { |
| 10072 | SOKOL_ASSERT(desc); |
| 10073 | sg_pass_desc desc_def = _sg_pass_desc_defaults(desc); |
| 10074 | sg_pass pass_id = _sg_alloc_pass(); |
| 10075 | if (pass_id.id != SG_INVALID_ID) { |
| 10076 | _sg_init_pass(pass_id, &desc_def); |
| 10077 | } |
| 10078 | else { |
| 10079 | SOKOL_LOG("pass pool exhausted!"); |
| 10080 | _SG_TRACE_NOARGS(err_pass_pool_exhausted); |
| 10081 | } |
| 10082 | _SG_TRACE_ARGS(make_pass, &desc_def, pass_id); |
| 10083 | return pass_id; |
| 10084 | } |
| 10085 | |
| 10086 | /*-- destroy resource --------------------------------------------------------*/ |
| 10087 | SOKOL_API_IMPL void sg_destroy_buffer(sg_buffer buf_id) { |
| 10088 | _SG_TRACE_ARGS(destroy_buffer, buf_id); |
| 10089 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 10090 | if (buf) { |
| 10091 | if (buf->slot.ctx_id == _sg.active_context.id) { |
| 10092 | _sg_destroy_buffer(buf); |
| 10093 | _sg_reset_buffer(buf); |
| 10094 | _sg_pool_free_index(&_sg.pools.buffer_pool, _sg_slot_index(buf_id.id)); |
| 10095 | } |
| 10096 | else { |
| 10097 | SOKOL_LOG("sg_destroy_buffer: active context mismatch (must be same as for creation)"); |
| 10098 | _SG_TRACE_NOARGS(err_context_mismatch); |
| 10099 | } |
| 10100 | } |
| 10101 | } |
| 10102 | |
| 10103 | SOKOL_API_IMPL void sg_destroy_image(sg_image img_id) { |
| 10104 | _SG_TRACE_ARGS(destroy_image, img_id); |
| 10105 | _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 10106 | if (img) { |
| 10107 | if (img->slot.ctx_id == _sg.active_context.id) { |
| 10108 | _sg_destroy_image(img); |
| 10109 | _sg_reset_image(img); |
| 10110 | _sg_pool_free_index(&_sg.pools.image_pool, _sg_slot_index(img_id.id)); |
| 10111 | } |
| 10112 | else { |
| 10113 | SOKOL_LOG("sg_destroy_image: active context mismatch (must be same as for creation)"); |
| 10114 | _SG_TRACE_NOARGS(err_context_mismatch); |
| 10115 | } |
| 10116 | } |
| 10117 | } |
| 10118 | |
| 10119 | SOKOL_API_IMPL void sg_destroy_shader(sg_shader shd_id) { |
| 10120 | _SG_TRACE_ARGS(destroy_shader, shd_id); |
| 10121 | _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 10122 | if (shd) { |
| 10123 | if (shd->slot.ctx_id == _sg.active_context.id) { |
| 10124 | _sg_destroy_shader(shd); |
| 10125 | _sg_reset_shader(shd); |
| 10126 | _sg_pool_free_index(&_sg.pools.shader_pool, _sg_slot_index(shd_id.id)); |
| 10127 | } |
| 10128 | else { |
| 10129 | SOKOL_LOG("sg_destroy_shader: active context mismatch (must be same as for creation)"); |
| 10130 | _SG_TRACE_NOARGS(err_context_mismatch); |
| 10131 | } |
| 10132 | } |
| 10133 | } |
| 10134 | |
| 10135 | SOKOL_API_IMPL void sg_destroy_pipeline(sg_pipeline pip_id) { |
| 10136 | _SG_TRACE_ARGS(destroy_pipeline, pip_id); |
| 10137 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 10138 | if (pip) { |
| 10139 | if (pip->slot.ctx_id == _sg.active_context.id) { |
| 10140 | _sg_destroy_pipeline(pip); |
| 10141 | _sg_reset_pipeline(pip); |
| 10142 | _sg_pool_free_index(&_sg.pools.pipeline_pool, _sg_slot_index(pip_id.id)); |
| 10143 | } |
| 10144 | else { |
| 10145 | SOKOL_LOG("sg_destroy_pipeline: active context mismatch (must be same as for creation)"); |
| 10146 | _SG_TRACE_NOARGS(err_context_mismatch); |
| 10147 | } |
| 10148 | } |
| 10149 | } |
| 10150 | |
| 10151 | SOKOL_API_IMPL void sg_destroy_pass(sg_pass pass_id) { |
| 10152 | _SG_TRACE_ARGS(destroy_pass, pass_id); |
| 10153 | _sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id); |
| 10154 | if (pass) { |
| 10155 | if (pass->slot.ctx_id == _sg.active_context.id) { |
| 10156 | _sg_destroy_pass(pass); |
| 10157 | _sg_reset_pass(pass); |
| 10158 | _sg_pool_free_index(&_sg.pools.pass_pool, _sg_slot_index(pass_id.id)); |
| 10159 | } |
| 10160 | else { |
| 10161 | SOKOL_LOG("sg_destroy_pass: active context mismatch (must be same as for creation)"); |
| 10162 | _SG_TRACE_NOARGS(err_context_mismatch); |
| 10163 | } |
| 10164 | } |
| 10165 | } |
| 10166 | |
| 10167 | SOKOL_API_IMPL void sg_begin_default_pass(const sg_pass_action* pass_action, int width, int height) { |
| 10168 | SOKOL_ASSERT(pass_action); |
| 10169 | SOKOL_ASSERT((pass_action->_start_canary == 0) && (pass_action->_end_canary == 0)); |
| 10170 | sg_pass_action pa; |
| 10171 | _sg_resolve_default_pass_action(pass_action, &pa); |
| 10172 | _sg.cur_pass.id = SG_INVALID_ID; |
| 10173 | _sg.pass_valid = true; |
| 10174 | _sg_begin_pass(0, &pa, width, height); |
| 10175 | _SG_TRACE_ARGS(begin_default_pass, pass_action, width, height); |
| 10176 | } |
| 10177 | |
| 10178 | SOKOL_API_IMPL void sg_begin_pass(sg_pass pass_id, const sg_pass_action* pass_action) { |
| 10179 | SOKOL_ASSERT(pass_action); |
| 10180 | SOKOL_ASSERT((pass_action->_start_canary == 0) && (pass_action->_end_canary == 0)); |
| 10181 | _sg.cur_pass = pass_id; |
| 10182 | _sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id); |
| 10183 | if (pass && _sg_validate_begin_pass(pass)) { |
| 10184 | _sg.pass_valid = true; |
| 10185 | sg_pass_action pa; |
| 10186 | _sg_resolve_default_pass_action(pass_action, &pa); |
| 10187 | const int w = pass->color_atts[0].image->width; |
| 10188 | const int h = pass->color_atts[0].image->height; |
| 10189 | _sg_begin_pass(pass, &pa, w, h); |
| 10190 | _SG_TRACE_ARGS(begin_pass, pass_id, pass_action); |
| 10191 | } |
| 10192 | else { |
| 10193 | _sg.pass_valid = false; |
| 10194 | _SG_TRACE_NOARGS(err_pass_invalid); |
| 10195 | } |
| 10196 | } |
| 10197 | |
| 10198 | SOKOL_API_IMPL void sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left) { |
| 10199 | if (!_sg.pass_valid) { |
| 10200 | _SG_TRACE_NOARGS(err_pass_invalid); |
| 10201 | return; |
| 10202 | } |
| 10203 | _sg_apply_viewport(x, y, width, height, origin_top_left); |
| 10204 | _SG_TRACE_ARGS(apply_viewport, x, y, width, height, origin_top_left); |
| 10205 | } |
| 10206 | |
| 10207 | SOKOL_API_IMPL void sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left) { |
| 10208 | if (!_sg.pass_valid) { |
| 10209 | _SG_TRACE_NOARGS(err_pass_invalid); |
| 10210 | return; |
| 10211 | } |
| 10212 | _sg_apply_scissor_rect(x, y, width, height, origin_top_left); |
| 10213 | _SG_TRACE_ARGS(apply_scissor_rect, x, y, width, height, origin_top_left); |
| 10214 | } |
| 10215 | |
| 10216 | SOKOL_API_IMPL void sg_apply_pipeline(sg_pipeline pip_id) { |
| 10217 | _sg.bindings_valid = false; |
| 10218 | if (!_sg_validate_apply_pipeline(pip_id)) { |
| 10219 | _sg.next_draw_valid = false; |
| 10220 | _SG_TRACE_NOARGS(err_draw_invalid); |
| 10221 | return; |
| 10222 | } |
| 10223 | if (!_sg.pass_valid) { |
| 10224 | _SG_TRACE_NOARGS(err_pass_invalid); |
| 10225 | return; |
| 10226 | } |
| 10227 | _sg.cur_pipeline = pip_id; |
| 10228 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 10229 | SOKOL_ASSERT(pip); |
| 10230 | _sg.next_draw_valid = (SG_RESOURCESTATE_VALID == pip->slot.state); |
| 10231 | SOKOL_ASSERT(pip->shader && (pip->shader->slot.id == pip->shader_id.id)); |
| 10232 | _sg_apply_pipeline(pip); |
| 10233 | _SG_TRACE_ARGS(apply_pipeline, pip_id); |
| 10234 | } |
| 10235 | |
| 10236 | SOKOL_API_IMPL void sg_apply_bindings(const sg_bindings* bindings) { |
| 10237 | SOKOL_ASSERT(bindings); |
| 10238 | SOKOL_ASSERT((bindings->_start_canary == 0) && (bindings->_end_canary==0)); |
| 10239 | if (!_sg_validate_apply_bindings(bindings)) { |
| 10240 | _sg.next_draw_valid = false; |
| 10241 | _SG_TRACE_NOARGS(err_draw_invalid); |
| 10242 | return; |
| 10243 | } |
| 10244 | _sg.bindings_valid = true; |
| 10245 | |
| 10246 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, _sg.cur_pipeline.id); |
| 10247 | SOKOL_ASSERT(pip); |
| 10248 | |
| 10249 | _sg_buffer_t* vbs[SG_MAX_SHADERSTAGE_BUFFERS] = { 0 }; |
| 10250 | int num_vbs = 0; |
| 10251 | for (int i = 0; i < SG_MAX_SHADERSTAGE_BUFFERS; i++, num_vbs++) { |
| 10252 | if (bindings->vertex_buffers[i].id) { |
| 10253 | vbs[i] = _sg_lookup_buffer(&_sg.pools, bindings->vertex_buffers[i].id); |
| 10254 | SOKOL_ASSERT(vbs[i]); |
| 10255 | _sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == vbs[i]->slot.state); |
| 10256 | _sg.next_draw_valid &= !vbs[i]->append_overflow; |
| 10257 | } |
| 10258 | else { |
| 10259 | break; |
| 10260 | } |
| 10261 | } |
| 10262 | |
| 10263 | _sg_buffer_t* ib = 0; |
| 10264 | if (bindings->index_buffer.id) { |
| 10265 | ib = _sg_lookup_buffer(&_sg.pools, bindings->index_buffer.id); |
| 10266 | SOKOL_ASSERT(ib); |
| 10267 | _sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == ib->slot.state); |
| 10268 | _sg.next_draw_valid &= !ib->append_overflow; |
| 10269 | } |
| 10270 | |
| 10271 | _sg_image_t* vs_imgs[SG_MAX_SHADERSTAGE_IMAGES] = { 0 }; |
| 10272 | int num_vs_imgs = 0; |
| 10273 | for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++, num_vs_imgs++) { |
| 10274 | if (bindings->vs_images[i].id) { |
| 10275 | vs_imgs[i] = _sg_lookup_image(&_sg.pools, bindings->vs_images[i].id); |
| 10276 | SOKOL_ASSERT(vs_imgs[i]); |
| 10277 | _sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == vs_imgs[i]->slot.state); |
| 10278 | } |
| 10279 | else { |
| 10280 | break; |
| 10281 | } |
| 10282 | } |
| 10283 | |
| 10284 | _sg_image_t* fs_imgs[SG_MAX_SHADERSTAGE_IMAGES] = { 0 }; |
| 10285 | int num_fs_imgs = 0; |
| 10286 | for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++, num_fs_imgs++) { |
| 10287 | if (bindings->fs_images[i].id) { |
| 10288 | fs_imgs[i] = _sg_lookup_image(&_sg.pools, bindings->fs_images[i].id); |
| 10289 | SOKOL_ASSERT(fs_imgs[i]); |
| 10290 | _sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == fs_imgs[i]->slot.state); |
| 10291 | } |
| 10292 | else { |
| 10293 | break; |
| 10294 | } |
| 10295 | } |
| 10296 | if (_sg.next_draw_valid) { |
| 10297 | const int* vb_offsets = bindings->vertex_buffer_offsets; |
| 10298 | int ib_offset = bindings->index_buffer_offset; |
| 10299 | _sg_apply_bindings(pip, vbs, vb_offsets, num_vbs, ib, ib_offset, vs_imgs, num_vs_imgs, fs_imgs, num_fs_imgs); |
| 10300 | _SG_TRACE_ARGS(apply_bindings, bindings); |
| 10301 | } |
| 10302 | else { |
| 10303 | _SG_TRACE_NOARGS(err_draw_invalid); |
| 10304 | } |
| 10305 | } |
| 10306 | |
| 10307 | SOKOL_API_IMPL void sg_apply_uniforms(sg_shader_stage stage, int ub_index, const void* data, int num_bytes) { |
| 10308 | SOKOL_ASSERT((stage == SG_SHADERSTAGE_VS) || (stage == SG_SHADERSTAGE_FS)); |
| 10309 | SOKOL_ASSERT((ub_index >= 0) && (ub_index < SG_MAX_SHADERSTAGE_UBS)); |
| 10310 | SOKOL_ASSERT(data && (num_bytes > 0)); |
| 10311 | if (!_sg_validate_apply_uniforms(stage, ub_index, data, num_bytes)) { |
| 10312 | _sg.next_draw_valid = false; |
| 10313 | _SG_TRACE_NOARGS(err_draw_invalid); |
| 10314 | return; |
| 10315 | } |
| 10316 | if (!_sg.pass_valid) { |
| 10317 | _SG_TRACE_NOARGS(err_pass_invalid); |
| 10318 | return; |
| 10319 | } |
| 10320 | if (!_sg.next_draw_valid) { |
| 10321 | _SG_TRACE_NOARGS(err_draw_invalid); |
| 10322 | } |
| 10323 | _sg_apply_uniforms(stage, ub_index, data, num_bytes); |
| 10324 | _SG_TRACE_ARGS(apply_uniforms, stage, ub_index, data, num_bytes); |
| 10325 | } |
| 10326 | |
| 10327 | SOKOL_API_IMPL void sg_draw(int base_element, int num_elements, int num_instances) { |
| 10328 | #if defined(SOKOL_DEBUG) |
| 10329 | if (!_sg.bindings_valid) { |
| 10330 | SOKOL_LOG("attempting to draw without resource bindings"); |
| 10331 | } |
| 10332 | #endif |
| 10333 | if (!_sg.pass_valid) { |
| 10334 | _SG_TRACE_NOARGS(err_pass_invalid); |
| 10335 | return; |
| 10336 | } |
| 10337 | if (!_sg.next_draw_valid) { |
| 10338 | _SG_TRACE_NOARGS(err_draw_invalid); |
| 10339 | return; |
| 10340 | } |
| 10341 | if (!_sg.bindings_valid) { |
| 10342 | _SG_TRACE_NOARGS(err_bindings_invalid); |
| 10343 | return; |
| 10344 | } |
| 10345 | _sg_draw(base_element, num_elements, num_instances); |
| 10346 | _SG_TRACE_ARGS(draw, base_element, num_elements, num_instances); |
| 10347 | } |
| 10348 | |
| 10349 | SOKOL_API_IMPL void sg_end_pass(void) { |
| 10350 | if (!_sg.pass_valid) { |
| 10351 | _SG_TRACE_NOARGS(err_pass_invalid); |
| 10352 | return; |
| 10353 | } |
| 10354 | _sg_end_pass(); |
| 10355 | _sg.cur_pass.id = SG_INVALID_ID; |
| 10356 | _sg.cur_pipeline.id = SG_INVALID_ID; |
| 10357 | _sg.pass_valid = false; |
| 10358 | _SG_TRACE_NOARGS(end_pass); |
| 10359 | } |
| 10360 | |
| 10361 | SOKOL_API_IMPL void sg_commit(void) { |
| 10362 | _sg_commit(); |
| 10363 | _SG_TRACE_NOARGS(commit); |
| 10364 | _sg.frame_index++; |
| 10365 | } |
| 10366 | |
| 10367 | SOKOL_API_IMPL void sg_reset_state_cache(void) { |
| 10368 | _sg_reset_state_cache(); |
| 10369 | _SG_TRACE_NOARGS(reset_state_cache); |
| 10370 | } |
| 10371 | |
| 10372 | SOKOL_API_IMPL void sg_update_buffer(sg_buffer buf_id, const void* data, int num_bytes) { |
| 10373 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 10374 | if ((num_bytes > 0) && buf && (buf->slot.state == SG_RESOURCESTATE_VALID)) { |
| 10375 | if (_sg_validate_update_buffer(buf, data, num_bytes)) { |
| 10376 | SOKOL_ASSERT(num_bytes <= buf->size); |
| 10377 | /* only one update allowed per buffer and frame */ |
| 10378 | SOKOL_ASSERT(buf->update_frame_index != _sg.frame_index); |
| 10379 | /* update and append on same buffer in same frame not allowed */ |
| 10380 | SOKOL_ASSERT(buf->append_frame_index != _sg.frame_index); |
| 10381 | _sg_update_buffer(buf, data, num_bytes); |
| 10382 | buf->update_frame_index = _sg.frame_index; |
| 10383 | } |
| 10384 | } |
| 10385 | _SG_TRACE_ARGS(update_buffer, buf_id, data, num_bytes); |
| 10386 | } |
| 10387 | |
| 10388 | SOKOL_API_IMPL int sg_append_buffer(sg_buffer buf_id, const void* data, int num_bytes) { |
| 10389 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 10390 | int result; |
| 10391 | if (buf) { |
| 10392 | /* rewind append cursor in a new frame */ |
| 10393 | if (buf->append_frame_index != _sg.frame_index) { |
| 10394 | buf->append_pos = 0; |
| 10395 | buf->append_overflow = false; |
| 10396 | } |
| 10397 | if ((buf->append_pos + num_bytes) > buf->size) { |
| 10398 | buf->append_overflow = true; |
| 10399 | } |
| 10400 | const int start_pos = buf->append_pos; |
| 10401 | if (buf->slot.state == SG_RESOURCESTATE_VALID) { |
| 10402 | if (_sg_validate_append_buffer(buf, data, num_bytes)) { |
| 10403 | if (!buf->append_overflow && (num_bytes > 0)) { |
| 10404 | /* update and append on same buffer in same frame not allowed */ |
| 10405 | SOKOL_ASSERT(buf->update_frame_index != _sg.frame_index); |
| 10406 | _sg_append_buffer(buf, data, num_bytes, buf->append_frame_index != _sg.frame_index); |
| 10407 | buf->append_pos += num_bytes; |
| 10408 | buf->append_frame_index = _sg.frame_index; |
| 10409 | } |
| 10410 | } |
| 10411 | } |
| 10412 | result = start_pos; |
| 10413 | } |
| 10414 | else { |
| 10415 | /* FIXME: should we return -1 here? */ |
| 10416 | result = 0; |
| 10417 | } |
| 10418 | _SG_TRACE_ARGS(append_buffer, buf_id, data, num_bytes, result); |
| 10419 | return result; |
| 10420 | } |
| 10421 | |
| 10422 | SOKOL_API_IMPL bool sg_query_buffer_overflow(sg_buffer buf_id) { |
| 10423 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 10424 | bool result = buf ? buf->append_overflow : false; |
| 10425 | return result; |
| 10426 | } |
| 10427 | |
| 10428 | SOKOL_API_IMPL void sg_update_image(sg_image img_id, const sg_image_content* data) { |
| 10429 | _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 10430 | if (img && img->slot.state == SG_RESOURCESTATE_VALID) { |
| 10431 | if (_sg_validate_update_image(img, data)) { |
| 10432 | SOKOL_ASSERT(img->upd_frame_index != _sg.frame_index); |
| 10433 | _sg_update_image(img, data); |
| 10434 | img->upd_frame_index = _sg.frame_index; |
| 10435 | } |
| 10436 | } |
| 10437 | _SG_TRACE_ARGS(update_image, img_id, data); |
| 10438 | } |
| 10439 | |
| 10440 | SOKOL_API_IMPL void sg_push_debug_group(const char* name) { |
| 10441 | SOKOL_ASSERT(name); |
| 10442 | _SG_TRACE_ARGS(push_debug_group, name); |
| 10443 | } |
| 10444 | |
| 10445 | SOKOL_API_IMPL void sg_pop_debug_group(void) { |
| 10446 | _SG_TRACE_NOARGS(pop_debug_group); |
| 10447 | } |
| 10448 | |
| 10449 | SOKOL_API_IMPL sg_buffer_info sg_query_buffer_info(sg_buffer buf_id) { |
| 10450 | sg_buffer_info info; |
| 10451 | memset(&info, 0, sizeof(info)); |
| 10452 | const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 10453 | if (buf) { |
| 10454 | info.slot.state = buf->slot.state; |
| 10455 | info.slot.res_id = buf->slot.id; |
| 10456 | info.slot.ctx_id = buf->slot.ctx_id; |
| 10457 | info.update_frame_index = buf->update_frame_index; |
| 10458 | info.append_frame_index = buf->append_frame_index; |
| 10459 | info.append_pos = buf->append_pos; |
| 10460 | info.append_overflow = buf->append_overflow; |
| 10461 | #if defined(SOKOL_D3D11) |
| 10462 | info.num_slots = 1; |
| 10463 | info.active_slot = 0; |
| 10464 | #else |
| 10465 | info.num_slots = buf->num_slots; |
| 10466 | info.active_slot = buf->active_slot; |
| 10467 | #endif |
| 10468 | } |
| 10469 | return info; |
| 10470 | } |
| 10471 | |
| 10472 | SOKOL_API_IMPL sg_image_info sg_query_image_info(sg_image img_id) { |
| 10473 | sg_image_info info; |
| 10474 | memset(&info, 0, sizeof(info)); |
| 10475 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 10476 | if (img) { |
| 10477 | info.slot.state = img->slot.state; |
| 10478 | info.slot.res_id = img->slot.id; |
| 10479 | info.slot.ctx_id = img->slot.ctx_id; |
| 10480 | #if defined(SOKOL_D3D11) |
| 10481 | info.num_slots = 1; |
| 10482 | info.active_slot = 0; |
| 10483 | #else |
| 10484 | info.num_slots = img->num_slots; |
| 10485 | info.active_slot = img->active_slot; |
| 10486 | #endif |
| 10487 | } |
| 10488 | return info; |
| 10489 | } |
| 10490 | |
| 10491 | SOKOL_API_IMPL sg_shader_info sg_query_shader_info(sg_shader shd_id) { |
| 10492 | sg_shader_info info; |
| 10493 | memset(&info, 0, sizeof(info)); |
| 10494 | const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 10495 | if (shd) { |
| 10496 | info.slot.state = shd->slot.state; |
| 10497 | info.slot.res_id = shd->slot.id; |
| 10498 | info.slot.ctx_id = shd->slot.ctx_id; |
| 10499 | } |
| 10500 | return info; |
| 10501 | } |
| 10502 | |
| 10503 | SOKOL_API_IMPL sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip_id) { |
| 10504 | sg_pipeline_info info; |
| 10505 | memset(&info, 0, sizeof(info)); |
| 10506 | const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 10507 | if (pip) { |
| 10508 | info.slot.state = pip->slot.state; |
| 10509 | info.slot.res_id = pip->slot.id; |
| 10510 | info.slot.ctx_id = pip->slot.ctx_id; |
| 10511 | } |
| 10512 | return info; |
| 10513 | } |
| 10514 | |
| 10515 | SOKOL_API_IMPL sg_pass_info sg_query_pass_info(sg_pass pass_id) { |
| 10516 | sg_pass_info info; |
| 10517 | memset(&info, 0, sizeof(info)); |
| 10518 | const _sg_pass_t* pass = _sg_lookup_pass(&_sg.pools, pass_id.id); |
| 10519 | if (pass) { |
| 10520 | info.slot.state = pass->slot.state; |
| 10521 | info.slot.res_id = pass->slot.id; |
| 10522 | info.slot.ctx_id = pass->slot.ctx_id; |
| 10523 | } |
| 10524 | return info; |
| 10525 | } |
| 10526 | |
| 10527 | /*--- DEPRECATED ---*/ |
| 10528 | #ifndef SOKOL_NO_DEPRECATED |
| 10529 | SOKOL_API_IMPL void sg_apply_draw_state(const sg_draw_state* ds) { |
| 10530 | SOKOL_ASSERT(ds); |
| 10531 | SOKOL_ASSERT((ds->_start_canary==0) && (ds->_end_canary==0)); |
| 10532 | sg_apply_pipeline(ds->pipeline); |
| 10533 | sg_bindings bindings; |
| 10534 | memset(&bindings, 0, sizeof(bindings)); |
| 10535 | for (int i = 0; i < SG_MAX_SHADERSTAGE_BUFFERS; i++) { |
| 10536 | bindings.vertex_buffers[i] = ds->vertex_buffers[i]; |
| 10537 | bindings.vertex_buffer_offsets[i] = ds->vertex_buffer_offsets[i]; |
| 10538 | } |
| 10539 | bindings.index_buffer = ds->index_buffer; |
| 10540 | bindings.index_buffer_offset = ds->index_buffer_offset; |
| 10541 | for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++) { |
| 10542 | bindings.vs_images[i] = ds->vs_images[i]; |
| 10543 | bindings.fs_images[i] = ds->fs_images[i]; |
| 10544 | } |
| 10545 | sg_apply_bindings(&bindings); |
| 10546 | } |
| 10547 | |
| 10548 | SOKOL_API_IMPL void sg_apply_uniform_block(sg_shader_stage stage, int ub_index, const void* data, int num_bytes) { |
| 10549 | sg_apply_uniforms(stage, ub_index, data, num_bytes); |
| 10550 | } |
| 10551 | #endif |
| 10552 | |
| 10553 | #ifdef _MSC_VER |
| 10554 | #pragma warning(pop) |
| 10555 | #endif |
| 10556 | |
| 10557 | #endif /* SOKOL_IMPL */ |