| 1 | #if defined(SOKOL_IMPL) && !defined(SOKOL_GFX_IMPL) |
| 2 | #define SOKOL_GFX_IMPL |
| 3 | #endif |
| 4 | #ifndef SOKOL_GFX_INCLUDED |
| 5 | /* |
| 6 | sokol_gfx.h -- simple 3D API wrapper |
| 7 | |
| 8 | Project URL: https://github.com/floooh/sokol |
| 9 | |
| 10 | Example code: https://github.com/floooh/sokol-samples |
| 11 | |
| 12 | Do this: |
| 13 | #define SOKOL_IMPL or |
| 14 | #define SOKOL_GFX_IMPL |
| 15 | before you include this file in *one* C or C++ file to create the |
| 16 | implementation. |
| 17 | |
| 18 | In the same place define one of the following to select the rendering |
| 19 | backend: |
| 20 | #define SOKOL_GLCORE |
| 21 | #define SOKOL_GLES3 |
| 22 | #define SOKOL_D3D11 |
| 23 | #define SOKOL_METAL |
| 24 | #define SOKOL_WGPU |
| 25 | #define SOKOL_DUMMY_BACKEND |
| 26 | |
| 27 | I.e. for the desktop GL it should look like this: |
| 28 | |
| 29 | #include ... |
| 30 | #include ... |
| 31 | #define SOKOL_IMPL |
| 32 | #define SOKOL_GLCORE |
| 33 | #include "sokol_gfx.h" |
| 34 | |
| 35 | The dummy backend replaces the platform-specific backend code with empty |
| 36 | stub functions. This is useful for writing tests that need to run on the |
| 37 | command line. |
| 38 | |
| 39 | Optionally provide the following defines with your own implementations: |
| 40 | |
| 41 | SOKOL_ASSERT(c) - your own assert macro (default: assert(c)) |
| 42 | SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false)) |
| 43 | SOKOL_GFX_API_DECL - public function declaration prefix (default: extern) |
| 44 | SOKOL_API_DECL - same as SOKOL_GFX_API_DECL |
| 45 | SOKOL_API_IMPL - public function implementation prefix (default: -) |
| 46 | SOKOL_TRACE_HOOKS - enable trace hook callbacks (search below for TRACE HOOKS) |
| 47 | SOKOL_EXTERNAL_GL_LOADER - indicates that you're using your own GL loader, in this case |
| 48 | sokol_gfx.h will not include any platform GL headers and disable |
| 49 | the integrated Win32 GL loader |
| 50 | |
| 51 | If sokol_gfx.h is compiled as a DLL, define the following before |
| 52 | including the declaration or implementation: |
| 53 | |
| 54 | SOKOL_DLL |
| 55 | |
| 56 | On Windows, SOKOL_DLL will define SOKOL_GFX_API_DECL as __declspec(dllexport) |
| 57 | or __declspec(dllimport) as needed. |
| 58 | |
| 59 | If you want to compile without deprecated structs and functions, |
| 60 | define: |
| 61 | |
| 62 | SOKOL_NO_DEPRECATED |
| 63 | |
| 64 | Optionally define the following to force debug checks and validations |
| 65 | even in release mode: |
| 66 | |
| 67 | SOKOL_DEBUG - by default this is defined if _DEBUG is defined |
| 68 | |
| 69 | sokol_gfx DOES NOT: |
| 70 | =================== |
| 71 | - create a window, swapchain or the 3D-API context/device, you must do this |
| 72 | before sokol_gfx is initialized, and pass any required information |
| 73 | (like 3D device pointers) to the sokol_gfx initialization call |
| 74 | |
| 75 | - present the rendered frame, how this is done exactly usually depends |
| 76 | on how the window and 3D-API context/device was created |
| 77 | |
| 78 | - provide a unified shader language, instead 3D-API-specific shader |
| 79 | source-code or shader-bytecode must be provided (for the "official" |
| 80 | offline shader cross-compiler, see here: |
| 81 | https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md) |
| 82 | |
| 83 | |
| 84 | STEP BY STEP |
| 85 | ============ |
| 86 | --- to initialize sokol_gfx, after creating a window and a 3D-API |
| 87 | context/device, call: |
| 88 | |
| 89 | sg_setup(const sg_desc*) |
| 90 | |
| 91 | Depending on the selected 3D backend, sokol-gfx requires some |
| 92 | information, like a device pointer, default swapchain pixel formats |
| 93 | and so on. If you are using sokol_app.h for the window system |
| 94 | glue, you can use a helper function provided in the sokol_glue.h |
| 95 | header: |
| 96 | |
| 97 | #include "sokol_gfx.h" |
| 98 | #include "sokol_app.h" |
| 99 | #include "sokol_glue.h" |
| 100 | //... |
| 101 | sg_setup(&(sg_desc){ |
| 102 | .environment = sglue_environment(), |
| 103 | }); |
| 104 | |
| 105 | To get any logging output for errors and from the validation layer, you |
| 106 | need to provide a logging callback. Easiest way is through sokol_log.h: |
| 107 | |
| 108 | #include "sokol_log.h" |
| 109 | //... |
| 110 | sg_setup(&(sg_desc){ |
| 111 | //... |
| 112 | .logger.func = slog_func, |
| 113 | }); |
| 114 | |
| 115 | --- create resource objects (at least buffers, shaders and pipelines, |
| 116 | and optionally images, samplers and render-pass-attachments): |
| 117 | |
| 118 | sg_buffer sg_make_buffer(const sg_buffer_desc*) |
| 119 | sg_image sg_make_image(const sg_image_desc*) |
| 120 | sg_sampler sg_make_sampler(const sg_sampler_desc*) |
| 121 | sg_shader sg_make_shader(const sg_shader_desc*) |
| 122 | sg_pipeline sg_make_pipeline(const sg_pipeline_desc*) |
| 123 | sg_attachments sg_make_attachments(const sg_attachments_desc*) |
| 124 | |
| 125 | --- start a render pass: |
| 126 | |
| 127 | sg_begin_pass(const sg_pass* pass); |
| 128 | |
| 129 | Typically, passes render into an externally provided swapchain which |
| 130 | presents the rendering result on the display. Such a 'swapchain pass' |
| 131 | is started like this: |
| 132 | |
| 133 | sg_begin_pass(&(sg_pass){ .action = { ... }, .swapchain = sglue_swapchain() }) |
| 134 | |
| 135 | ...where .action is an sg_pass_action struct containing actions to be performed |
| 136 | at the start and end of a render pass (such as clearing the render surfaces to |
| 137 | a specific color), and .swapchain is an sg_swapchain |
| 138 | struct all the required information to render into the swapchain's surfaces. |
| 139 | |
| 140 | To start an 'offscreen pass' into sokol-gfx image objects, an sg_attachment |
| 141 | object handle is required instead of an sg_swapchain struct. An offscreen |
| 142 | pass is started like this (assuming attachments is an sg_attachments handle): |
| 143 | |
| 144 | sg_begin_pass(&(sg_pass){ .action = { ... }, .attachments = attachments }); |
| 145 | |
| 146 | --- set the render pipeline state for the next draw call with: |
| 147 | |
| 148 | sg_apply_pipeline(sg_pipeline pip) |
| 149 | |
| 150 | --- fill an sg_bindings struct with the resource bindings for the next |
| 151 | draw call (0..N vertex buffers, 0 or 1 index buffer, 0..N image-objects, |
| 152 | samplers and storage-buffers), and call: |
| 153 | |
| 154 | sg_apply_bindings(const sg_bindings* bindings) |
| 155 | |
| 156 | to update the resource bindings |
| 157 | |
| 158 | --- optionally update shader uniform data with: |
| 159 | |
| 160 | sg_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range* data) |
| 161 | |
| 162 | Read the section 'UNIFORM DATA LAYOUT' to learn about the expected memory layout |
| 163 | of the uniform data passed into sg_apply_uniforms(). |
| 164 | |
| 165 | --- kick off a draw call with: |
| 166 | |
| 167 | sg_draw(int base_element, int num_elements, int num_instances) |
| 168 | |
| 169 | The sg_draw() function unifies all the different ways to render primitives |
| 170 | in a single call (indexed vs non-indexed rendering, and instanced vs non-instanced |
| 171 | rendering). In case of indexed rendering, base_element and num_element specify |
| 172 | indices in the currently bound index buffer. In case of non-indexed rendering |
| 173 | base_element and num_elements specify vertices in the currently bound |
| 174 | vertex-buffer(s). To perform instanced rendering, the rendering pipeline |
| 175 | must be setup for instancing (see sg_pipeline_desc below), a separate vertex buffer |
| 176 | containing per-instance data must be bound, and the num_instances parameter |
| 177 | must be > 1. |
| 178 | |
| 179 | --- finish the current rendering pass with: |
| 180 | |
| 181 | sg_end_pass() |
| 182 | |
| 183 | --- when done with the current frame, call |
| 184 | |
| 185 | sg_commit() |
| 186 | |
| 187 | --- at the end of your program, shutdown sokol_gfx with: |
| 188 | |
| 189 | sg_shutdown() |
| 190 | |
| 191 | --- if you need to destroy resources before sg_shutdown(), call: |
| 192 | |
| 193 | sg_destroy_buffer(sg_buffer buf) |
| 194 | sg_destroy_image(sg_image img) |
| 195 | sg_destroy_sampler(sg_sampler smp) |
| 196 | sg_destroy_shader(sg_shader shd) |
| 197 | sg_destroy_pipeline(sg_pipeline pip) |
| 198 | sg_destroy_attachments(sg_attachments atts) |
| 199 | |
| 200 | --- to set a new viewport rectangle, call |
| 201 | |
| 202 | sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left) |
| 203 | |
| 204 | ...or if you want to specify the viewport rectangle with float values: |
| 205 | |
| 206 | sg_apply_viewportf(float x, float y, float width, float height, bool origin_top_left) |
| 207 | |
| 208 | --- to set a new scissor rect, call: |
| 209 | |
| 210 | sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left) |
| 211 | |
| 212 | ...or with float values: |
| 213 | |
| 214 | sg_apply_scissor_rectf(float x, float y, float width, float height, bool origin_top_left) |
| 215 | |
| 216 | Both sg_apply_viewport() and sg_apply_scissor_rect() must be called |
| 217 | inside a rendering pass |
| 218 | |
| 219 | Note that sg_begin_default_pass() and sg_begin_pass() will reset both the |
| 220 | viewport and scissor rectangles to cover the entire framebuffer. |
| 221 | |
| 222 | --- to update (overwrite) the content of buffer and image resources, call: |
| 223 | |
| 224 | sg_update_buffer(sg_buffer buf, const sg_range* data) |
| 225 | sg_update_image(sg_image img, const sg_image_data* data) |
| 226 | |
| 227 | Buffers and images to be updated must have been created with |
| 228 | SG_USAGE_DYNAMIC or SG_USAGE_STREAM |
| 229 | |
| 230 | Only one update per frame is allowed for buffer and image resources when |
| 231 | using the sg_update_*() functions. The rationale is to have a simple |
| 232 | countermeasure to avoid the CPU scribbling over data the GPU is currently |
| 233 | using, or the CPU having to wait for the GPU |
| 234 | |
| 235 | Buffer and image updates can be partial, as long as a rendering |
| 236 | operation only references the valid (updated) data in the |
| 237 | buffer or image. |
| 238 | |
| 239 | --- to append a chunk of data to a buffer resource, call: |
| 240 | |
| 241 | int sg_append_buffer(sg_buffer buf, const sg_range* data) |
| 242 | |
| 243 | The difference to sg_update_buffer() is that sg_append_buffer() |
| 244 | can be called multiple times per frame to append new data to the |
| 245 | buffer piece by piece, optionally interleaved with draw calls referencing |
| 246 | the previously written data. |
| 247 | |
| 248 | sg_append_buffer() returns a byte offset to the start of the |
| 249 | written data, this offset can be assigned to |
| 250 | sg_bindings.vertex_buffer_offsets[n] or |
| 251 | sg_bindings.index_buffer_offset |
| 252 | |
| 253 | Code example: |
| 254 | |
| 255 | for (...) { |
| 256 | const void* data = ...; |
| 257 | const int num_bytes = ...; |
| 258 | int offset = sg_append_buffer(buf, &(sg_range) { .ptr=data, .size=num_bytes }); |
| 259 | bindings.vertex_buffer_offsets[0] = offset; |
| 260 | sg_apply_pipeline(pip); |
| 261 | sg_apply_bindings(&bindings); |
| 262 | sg_apply_uniforms(...); |
| 263 | sg_draw(...); |
| 264 | } |
| 265 | |
| 266 | A buffer to be used with sg_append_buffer() must have been created |
| 267 | with SG_USAGE_DYNAMIC or SG_USAGE_STREAM. |
| 268 | |
| 269 | If the application appends more data to the buffer then fits into |
| 270 | the buffer, the buffer will go into the "overflow" state for the |
| 271 | rest of the frame. |
| 272 | |
| 273 | Any draw calls attempting to render an overflown buffer will be |
| 274 | silently dropped (in debug mode this will also result in a |
| 275 | validation error). |
| 276 | |
| 277 | You can also check manually if a buffer is in overflow-state by calling |
| 278 | |
| 279 | bool sg_query_buffer_overflow(sg_buffer buf) |
| 280 | |
| 281 | You can manually check to see if an overflow would occur before adding |
| 282 | any data to a buffer by calling |
| 283 | |
| 284 | bool sg_query_buffer_will_overflow(sg_buffer buf, size_t size) |
| 285 | |
| 286 | NOTE: Due to restrictions in underlying 3D-APIs, appended chunks of |
| 287 | data will be 4-byte aligned in the destination buffer. This means |
| 288 | that there will be gaps in index buffers containing 16-bit indices |
| 289 | when the number of indices in a call to sg_append_buffer() is |
| 290 | odd. This isn't a problem when each call to sg_append_buffer() |
| 291 | is associated with one draw call, but will be problematic when |
| 292 | a single indexed draw call spans several appended chunks of indices. |
| 293 | |
| 294 | --- to check at runtime for optional features, limits and pixelformat support, |
| 295 | call: |
| 296 | |
| 297 | sg_features sg_query_features() |
| 298 | sg_limits sg_query_limits() |
| 299 | sg_pixelformat_info sg_query_pixelformat(sg_pixel_format fmt) |
| 300 | |
| 301 | --- if you need to call into the underlying 3D-API directly, you must call: |
| 302 | |
| 303 | sg_reset_state_cache() |
| 304 | |
| 305 | ...before calling sokol_gfx functions again |
| 306 | |
| 307 | --- you can inspect the original sg_desc structure handed to sg_setup() |
| 308 | by calling sg_query_desc(). This will return an sg_desc struct with |
| 309 | the default values patched in instead of any zero-initialized values |
| 310 | |
| 311 | --- you can get a desc struct matching the creation attributes of a |
| 312 | specific resource object via: |
| 313 | |
| 314 | sg_buffer_desc sg_query_buffer_desc(sg_buffer buf) |
| 315 | sg_image_desc sg_query_image_desc(sg_image img) |
| 316 | sg_sampler_desc sg_query_sampler_desc(sg_sampler smp) |
| 317 | sg_shader_desc sq_query_shader_desc(sg_shader shd) |
| 318 | sg_pipeline_desc sg_query_pipeline_desc(sg_pipeline pip) |
| 319 | sg_attachments_desc sg_query_attachments_desc(sg_attachments atts) |
| 320 | |
| 321 | ...but NOTE that the returned desc structs may be incomplete, only |
| 322 | creation attributes that are kept around internally after resource |
| 323 | creation will be filled in, and in some cases (like shaders) that's |
| 324 | very little. Any missing attributes will be set to zero. The returned |
| 325 | desc structs might still be useful as partial blueprint for creating |
| 326 | similar resources if filled up with the missing attributes. |
| 327 | |
| 328 | Calling the query-desc functions on an invalid resource will return |
| 329 | completely zeroed structs (it makes sense to check the resource state |
| 330 | with sg_query_*_state() first) |
| 331 | |
| 332 | --- you can query the default resource creation parameters through the functions |
| 333 | |
| 334 | sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc* desc) |
| 335 | sg_image_desc sg_query_image_defaults(const sg_image_desc* desc) |
| 336 | sg_sampler_desc sg_query_sampler_defaults(const sg_sampler_desc* desc) |
| 337 | sg_shader_desc sg_query_shader_defaults(const sg_shader_desc* desc) |
| 338 | sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc* desc) |
| 339 | sg_attachments_desc sg_query_attachments_defaults(const sg_attachments_desc* desc) |
| 340 | |
| 341 | These functions take a pointer to a desc structure which may contain |
| 342 | zero-initialized items for default values. These zero-init values |
| 343 | will be replaced with their concrete values in the returned desc |
| 344 | struct. |
| 345 | |
| 346 | --- you can inspect various internal resource runtime values via: |
| 347 | |
| 348 | sg_buffer_info sg_query_buffer_info(sg_buffer buf) |
| 349 | sg_image_info sg_query_image_info(sg_image img) |
| 350 | sg_sampler_info sg_query_sampler_info(sg_sampler smp) |
| 351 | sg_shader_info sg_query_shader_info(sg_shader shd) |
| 352 | sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip) |
| 353 | sg_attachments_info sg_query_attachments_info(sg_attachments atts) |
| 354 | |
| 355 | ...please note that the returned info-structs are tied quite closely |
| 356 | to sokol_gfx.h internals, and may change more often than other |
| 357 | public API functions and structs. |
| 358 | |
| 359 | --- you can query frame stats and control stats collection via: |
| 360 | |
| 361 | sg_query_frame_stats() |
| 362 | sg_enable_frame_stats() |
| 363 | sg_disable_frame_stats() |
| 364 | sg_frame_stats_enabled() |
| 365 | |
| 366 | --- you can ask at runtime what backend sokol_gfx.h has been compiled for: |
| 367 | |
| 368 | sg_backend sg_query_backend(void) |
| 369 | |
| 370 | --- call the following helper functions to compute the number of |
| 371 | bytes in a texture row or surface for a specific pixel format. |
| 372 | These functions might be helpful when preparing image data for consumption |
| 373 | by sg_make_image() or sg_update_image(): |
| 374 | |
| 375 | int sg_query_row_pitch(sg_pixel_format fmt, int width, int int row_align_bytes); |
| 376 | int sg_query_surface_pitch(sg_pixel_format fmt, int width, int height, int row_align_bytes); |
| 377 | |
| 378 | Width and height are generally in number pixels, but note that 'row' has different meaning |
| 379 | for uncompressed vs compressed pixel formats: for uncompressed formats, a row is identical |
| 380 | with a single line if pixels, while in compressed formats, one row is a line of *compression blocks*. |
| 381 | |
| 382 | This is why calling sg_query_surface_pitch() for a compressed pixel format and height |
| 383 | N, N+1, N+2, ... may return the same result. |
| 384 | |
| 385 | The row_align_bytes parammeter is for added flexibility. For image data that goes into |
| 386 | the sg_make_image() or sg_update_image() this should generally be 1, because these |
| 387 | functions take tightly packed image data as input no matter what alignment restrictions |
| 388 | exist in the backend 3D APIs. |
| 389 | |
| 390 | ON INITIALIZATION: |
| 391 | ================== |
| 392 | When calling sg_setup(), a pointer to an sg_desc struct must be provided |
| 393 | which contains initialization options. These options provide two types |
| 394 | of information to sokol-gfx: |
| 395 | |
| 396 | (1) upper bounds and limits needed to allocate various internal |
| 397 | data structures: |
| 398 | - the max number of resources of each type that can |
| 399 | be alive at the same time, this is used for allocating |
| 400 | internal pools |
| 401 | - the max overall size of uniform data that can be |
| 402 | updated per frame, including a worst-case alignment |
| 403 | per uniform update (this worst-case alignment is 256 bytes) |
| 404 | - the max size of all dynamic resource updates (sg_update_buffer, |
| 405 | sg_append_buffer and sg_update_image) per frame |
| 406 | Not all of those limit values are used by all backends, but it is |
| 407 | good practice to provide them none-the-less. |
| 408 | |
| 409 | (2) 3D backend "environment information" in a nested sg_environment struct: |
| 410 | - pointers to backend-specific context- or device-objects (for instance |
| 411 | the D3D11, WebGPU or Metal device objects) |
| 412 | - defaults for external swapchain pixel formats and sample counts, |
| 413 | these will be used as default values in image and pipeline objects, |
| 414 | and the sg_swapchain struct passed into sg_begin_pass() |
| 415 | Usually you provide a complete sg_environment struct through |
| 416 | a helper function, as an example look at the sglue_environment() |
| 417 | function in the sokol_glue.h header. |
| 418 | |
| 419 | See the documentation block of the sg_desc struct below for more information. |
| 420 | |
| 421 | |
| 422 | ON RENDER PASSES |
| 423 | ================ |
| 424 | Relevant samples: |
| 425 | - https://floooh.github.io/sokol-html5/offscreen-sapp.html |
| 426 | - https://floooh.github.io/sokol-html5/offscreen-msaa-sapp.html |
| 427 | - https://floooh.github.io/sokol-html5/mrt-sapp.html |
| 428 | - https://floooh.github.io/sokol-html5/mrt-pixelformats-sapp.html |
| 429 | |
| 430 | A render pass groups rendering commands into a set of render target images |
| 431 | (called 'pass attachments'). Render target images can be used in subsequent |
| 432 | passes as textures (it is invalid to use the same image both as render target |
| 433 | and as texture in the same pass). |
| 434 | |
| 435 | The following sokol-gfx functions must only be called inside a render pass: |
| 436 | |
| 437 | sg_apply_viewport(f) |
| 438 | sg_apply_scissor_rect(f) |
| 439 | sg_apply_pipeline |
| 440 | sg_apply_bindings |
| 441 | sg_apply_uniforms |
| 442 | sg_draw |
| 443 | |
| 444 | A frame must have at least one 'swapchain render pass' which renders into an |
| 445 | externally provided swapchain provided as an sg_swapchain struct to the |
| 446 | sg_begin_pass() function. The sg_swapchain struct must contain the |
| 447 | following information: |
| 448 | |
| 449 | - the color pixel-format of the swapchain's render surface |
| 450 | - an optional depth/stencil pixel format if the swapchain |
| 451 | has a depth/stencil buffer |
| 452 | - an optional sample-count for MSAA rendering |
| 453 | - NOTE: the above three values can be zero-initialized, in that |
| 454 | case the defaults from the sg_environment struct will be used that |
| 455 | had been passed to the sg_setup() function. |
| 456 | - a number of backend specific objects: |
| 457 | - GL/GLES3: just a GL framebuffer handle |
| 458 | - D3D11: |
| 459 | - an ID3D11RenderTargetView for the rendering surface |
| 460 | - if MSAA is used, an ID3D11RenderTargetView as |
| 461 | MSAA resolve-target |
| 462 | - an optional ID3D11DepthStencilView for the |
| 463 | depth/stencil buffer |
| 464 | - WebGPU |
| 465 | - a WGPUTextureView object for the rendering surface |
| 466 | - if MSAA is used, a WGPUTextureView object as MSAA resolve target |
| 467 | - an optional WGPUTextureView for the |
| 468 | - Metal (NOTE that the roles of provided surfaces is slightly |
| 469 | different in Metal than in D3D11 or WebGPU, notably, the |
| 470 | CAMetalDrawable is either rendered to directly, or serves |
| 471 | as MSAA resolve target): |
| 472 | - a CAMetalDrawable object which is either rendered |
| 473 | into directly, or in case of MSAA rendering, serves |
| 474 | as MSAA-resolve-target |
| 475 | - if MSAA is used, an multisampled MTLTexture where |
| 476 | rendering goes into |
| 477 | - an optional MTLTexture for the depth/stencil buffer |
| 478 | |
| 479 | It's recommended that you create a helper function which returns an |
| 480 | initialized sg_swapchain struct by value. This can then be directly plugged |
| 481 | into the sg_begin_pass function like this: |
| 482 | |
| 483 | sg_begin_pass(&(sg_pass){ .swapchain = sglue_swapchain() }); |
| 484 | |
| 485 | As an example for such a helper function check out the function sglue_swapchain() |
| 486 | in the sokol_glue.h header. |
| 487 | |
| 488 | For offscreen render passes, the render target images used in a render pass |
| 489 | are baked into an immutable sg_attachments object. |
| 490 | |
| 491 | For a simple offscreen scenario with one color-, one depth-stencil-render |
| 492 | target and without multisampling, creating an attachment object looks like this: |
| 493 | |
| 494 | First create two render target images, one with a color pixel format, |
| 495 | and one with the depth- or depth-stencil pixel format. Both images |
| 496 | must have the same dimensions: |
| 497 | |
| 498 | const sg_image color_img = sg_make_image(&(sg_image_desc){ |
| 499 | .render_target = true, |
| 500 | .width = 256, |
| 501 | .height = 256, |
| 502 | .pixel_format = SG_PIXELFORMAT_RGBA8, |
| 503 | .sample_count = 1, |
| 504 | }); |
| 505 | const sg_image depth_img = sg_make_image(&(sg_image_desc){ |
| 506 | .render_target = true, |
| 507 | .width = 256, |
| 508 | .height = 256, |
| 509 | .pixel_format = SG_PIXELFORMAT_DEPTH, |
| 510 | .sample_count = 1, |
| 511 | }); |
| 512 | |
| 513 | NOTE: when creating render target images, have in mind that some default values |
| 514 | are aligned with the default environment attributes in the sg_environment struct |
| 515 | that was passed into the sg_setup() call: |
| 516 | |
| 517 | - the default value for sg_image_desc.pixel_format is taken from |
| 518 | sg_environment.defaults.color_format |
| 519 | - the default value for sg_image_desc.sample_count is taken from |
| 520 | sg_environment.defaults.sample_count |
| 521 | - the default value for sg_image_desc.num_mipmaps is always 1 |
| 522 | |
| 523 | Next create an attachments object: |
| 524 | |
| 525 | const sg_attachments atts = sg_make_attachments(&(sg_attachments_desc){ |
| 526 | .colors[0].image = color_img, |
| 527 | .depth_stencil.image = depth_img, |
| 528 | }); |
| 529 | |
| 530 | This attachments object is then passed into the sg_begin_pass() function |
| 531 | in place of the swapchain struct: |
| 532 | |
| 533 | sg_begin_pass(&(sg_pass){ .attachments = atts }); |
| 534 | |
| 535 | Swapchain and offscreen passes form dependency trees each with a swapchain |
| 536 | pass at the root, offscreen passes as nodes, and render target images as |
| 537 | dependencies between passes. |
| 538 | |
| 539 | sg_pass_action structs are used to define actions that should happen at the |
| 540 | start and end of rendering passes (such as clearing pass attachments to a |
| 541 | specific color or depth-value, or performing an MSAA resolve operation at |
| 542 | the end of a pass). |
| 543 | |
| 544 | A typical sg_pass_action object which clears the color attachment to black |
| 545 | might look like this: |
| 546 | |
| 547 | const sg_pass_action = { |
| 548 | .colors[0] = { |
| 549 | .load_action = SG_LOADACTION_CLEAR, |
| 550 | .clear_value = { 0.0f, 0.0f, 0.0f, 1.0f } |
| 551 | } |
| 552 | }; |
| 553 | |
| 554 | This omits the defaults for the color attachment store action, and |
| 555 | the depth-stencil-attachments actions. The same pass action with the |
| 556 | defaults explicitly filled in would look like this: |
| 557 | |
| 558 | const sg_pass_action pass_action = { |
| 559 | .colors[0] = { |
| 560 | .load_action = SG_LOADACTION_CLEAR, |
| 561 | .store_action = SG_STOREACTION_STORE, |
| 562 | .clear_value = { 0.0f, 0.0f, 0.0f, 1.0f } |
| 563 | }, |
| 564 | .depth = = { |
| 565 | .load_action = SG_LOADACTION_CLEAR, |
| 566 | .store_action = SG_STOREACTION_DONTCARE, |
| 567 | .clear_value = 1.0f, |
| 568 | }, |
| 569 | .stencil = { |
| 570 | .load_action = SG_LOADACTION_CLEAR, |
| 571 | .store_action = SG_STOREACTION_DONTCARE, |
| 572 | .clear_value = 0 |
| 573 | } |
| 574 | }; |
| 575 | |
| 576 | With the sg_pass object and sg_pass_action struct in place everything |
| 577 | is ready now for the actual render pass: |
| 578 | |
| 579 | Using such this prepared sg_pass_action in a swapchain pass looks like |
| 580 | this: |
| 581 | |
| 582 | sg_begin_pass(&(sg_pass){ |
| 583 | .action = pass_action, |
| 584 | .swapchain = sglue_swapchain() |
| 585 | }); |
| 586 | ... |
| 587 | sg_end_pass(); |
| 588 | |
| 589 | ...of alternatively in one offscreen pass: |
| 590 | |
| 591 | sg_begin_pass(&(sg_pass){ |
| 592 | .action = pass_action, |
| 593 | .attachments = attachments, |
| 594 | }); |
| 595 | ... |
| 596 | sg_end_pass(); |
| 597 | |
| 598 | Offscreen rendering can also go into a mipmap, or a slice/face of |
| 599 | a cube-, array- or 3d-image (which some restrictions, for instance |
| 600 | it's not possible to create a 3D image with a depth/stencil pixel format, |
| 601 | these exceptions are generally caught by the sokol-gfx validation layer). |
| 602 | |
| 603 | The mipmap/slice selection happens at attachments creation time, for instance |
| 604 | to render into mipmap 2 of slice 3 of an array texture: |
| 605 | |
| 606 | const sg_attachments atts = sg_make_attachments(&(sg_attachments_desc){ |
| 607 | .colors[0] = { |
| 608 | .image = color_img, |
| 609 | .mip_level = 2, |
| 610 | .slice = 3, |
| 611 | }, |
| 612 | .depth_stencil.image = depth_img, |
| 613 | }); |
| 614 | |
| 615 | If MSAA offscreen rendering is desired, the multi-sample rendering result |
| 616 | must be 'resolved' into a separate 'resolve image', before that image can |
| 617 | be used as texture. |
| 618 | |
| 619 | NOTE: currently multisample-images cannot be bound as textures. |
| 620 | |
| 621 | Creating a simple attachments object for multisampled rendering requires |
| 622 | 3 attachment images: the color attachment image which has a sample |
| 623 | count > 1, a resolve attachment image of the same size and pixel format |
| 624 | but a sample count == 1, and a depth/stencil attachment image with |
| 625 | the same size and sample count as the color attachment image: |
| 626 | |
| 627 | const sg_image color_img = sg_make_image(&(sg_image_desc){ |
| 628 | .render_target = true, |
| 629 | .width = 256, |
| 630 | .height = 256, |
| 631 | .pixel_format = SG_PIXELFORMAT_RGBA8, |
| 632 | .sample_count = 4, |
| 633 | }); |
| 634 | const sg_image resolve_img = sg_make_image(&(sg_image_desc){ |
| 635 | .render_target = true, |
| 636 | .width = 256, |
| 637 | .height = 256, |
| 638 | .pixel_format = SG_PIXELFORMAT_RGBA8, |
| 639 | .sample_count = 1, |
| 640 | }); |
| 641 | const sg_image depth_img = sg_make_image(&(sg_image_desc){ |
| 642 | .render_target = true, |
| 643 | .width = 256, |
| 644 | .height = 256, |
| 645 | .pixel_format = SG_PIXELFORMAT_DEPTH, |
| 646 | .sample_count = 4, |
| 647 | }); |
| 648 | |
| 649 | ...create the attachments object: |
| 650 | |
| 651 | const sg_attachments atts = sg_make_attachments(&(sg_attachments_desc){ |
| 652 | .colors[0].image = color_img, |
| 653 | .resolves[0].image = resolve_img, |
| 654 | .depth_stencil.image = depth_img, |
| 655 | }); |
| 656 | |
| 657 | If an attachments object defines a resolve image in a specific resolve attachment slot, |
| 658 | an 'msaa resolve operation' will happen in sg_end_pass(). |
| 659 | |
| 660 | In this scenario, the content of the MSAA color attachment doesn't need to be |
| 661 | preserved (since it's only needed inside sg_end_pass for the msaa-resolve), so |
| 662 | the .store_action should be set to "don't care": |
| 663 | |
| 664 | const sg_pass_action = { |
| 665 | .colors[0] = { |
| 666 | .load_action = SG_LOADACTION_CLEAR, |
| 667 | .store_action = SG_STOREACTION_DONTCARE, |
| 668 | .clear_value = { 0.0f, 0.0f, 0.0f, 1.0f } |
| 669 | } |
| 670 | }; |
| 671 | |
| 672 | The actual render pass looks as usual: |
| 673 | |
| 674 | sg_begin_pass(&(sg_pass){ .action = pass_action, .attachments = atts }); |
| 675 | ... |
| 676 | sg_end_pass(); |
| 677 | |
| 678 | ...after sg_end_pass() the only difference to the non-msaa scenario is that the |
| 679 | rendering result which is going to be used as texture in a followup pass is |
| 680 | in 'resolve_img', not in 'color_img' (in fact, trying to bind color_img as a |
| 681 | texture would result in a validation error). |
| 682 | |
| 683 | |
| 684 | ON SHADER CREATION |
| 685 | ================== |
| 686 | sokol-gfx doesn't come with an integrated shader cross-compiler, instead |
| 687 | backend-specific shader sources or binary blobs need to be provided when |
| 688 | creating a shader object, along with information about the shader resource |
| 689 | binding interface needed in the sokol-gfx validation layer and to properly |
| 690 | bind shader resources on the CPU-side to be consumable by the GPU-side. |
| 691 | |
| 692 | The easiest way to provide all this shader creation data is to use the |
| 693 | sokol-shdc shader compiler tool to compile shaders from a common |
| 694 | GLSL syntax into backend-specific sources or binary blobs, along with |
| 695 | shader interface information and uniform blocks mapped to C structs. |
| 696 | |
| 697 | To create a shader using a C header which has been code-generated by sokol-shdc: |
| 698 | |
| 699 | // include the C header code-generated by sokol-shdc: |
| 700 | #include "myshader.glsl.h" |
| 701 | ... |
| 702 | |
| 703 | // create shader using a code-generated helper function from the C header: |
| 704 | sg_shader shd = sg_make_shader(myshader_shader_desc(sg_query_backend())); |
| 705 | |
| 706 | The samples in the 'sapp' subdirectory of the sokol-samples project |
| 707 | also use the sokol-shdc approach: |
| 708 | |
| 709 | https://github.com/floooh/sokol-samples/tree/master/sapp |
| 710 | |
| 711 | If you're planning to use sokol-shdc, you can stop reading here, instead |
| 712 | continue with the sokol-shdc documentation: |
| 713 | |
| 714 | https://github.com/floooh/sokol-tools/blob/master/docs/sokol-shdc.md |
| 715 | |
| 716 | To create shaders with backend-specific shader code or binary blobs, |
| 717 | the sg_make_shader() function requires the following information: |
| 718 | |
| 719 | - Shader code or shader binary blobs for the vertex- and fragment- shader-stage: |
| 720 | - for the desktop GL backend, source code can be provided in '#version 410' or |
| 721 | '#version 430', version 430 is required for storage buffer support, but note |
| 722 | that this is not available on macOS |
| 723 | - for the GLES3 backend, source code must be provided in '#version 300 es' syntax |
| 724 | - for the D3D11 backend, shaders can be provided as source or binary blobs, the |
| 725 | source code should be in HLSL4.0 (for best compatibility) or alternatively |
| 726 | in HLSL5.0 syntax (other versions may work but are not tested), NOTE: when |
| 727 | shader source code is provided for the D3D11 backend, sokol-gfx will dynamically |
| 728 | load 'd3dcompiler_47.dll' |
| 729 | - for the Metal backends, shaders can be provided as source or binary blobs, the |
| 730 | MSL version should be in 'metal-1.1' (other versions may work but are not tested) |
| 731 | - for the WebGPU backend, shader must be provided as WGSL source code |
| 732 | - optionally the following shader-code related attributes can be provided: |
| 733 | - an entry function name (only on D3D11 or Metal, but not OpenGL) |
| 734 | - on D3D11 only, a compilation target (default is "vs_4_0" and "ps_4_0") |
| 735 | |
| 736 | - Depending on backend, information about the input vertex attributes used by the |
| 737 | vertex shader: |
| 738 | - Metal: no information needed since vertex attributes are always bound |
| 739 | by their attribute location defined in the shader via '[[attribute(N)]]' |
| 740 | - WebGPU: no information needed since vertex attributes are always |
| 741 | bound by their attribute location defined in the shader via `@location(N)` |
| 742 | - GLSL: vertex attribute names can be optionally provided, in that case their |
| 743 | location will be looked up by name, otherwise, the vertex attribute location |
| 744 | can be defined with 'layout(location = N)', PLEASE NOTE that the name-lookup method |
| 745 | may be removed at some point |
| 746 | - D3D11: a 'semantic name' and 'semantic index' must be provided for each vertex |
| 747 | attribute, e.g. if the vertex attribute is defined as 'TEXCOORD1' in the shader, |
| 748 | the semantic name would be 'TEXCOORD', and the semantic index would be '1' |
| 749 | |
| 750 | - Information about each uniform block used in the shader: |
| 751 | - The size of the uniform block in number of bytes. |
| 752 | - A memory layout hint (currently 'native' or 'std140') where 'native' defines a |
| 753 | backend-specific memory layout which shouldn't be used for cross-platform code. |
| 754 | Only std140 guarantees a backend-agnostic memory layout. |
| 755 | - For GLSL only: a description of the internal uniform block layout, which maps |
| 756 | member types and their offsets on the CPU side to uniform variable names |
| 757 | in the GLSL shader |
| 758 | - please also NOTE the documentation sections about UNIFORM DATA LAYOUT |
| 759 | and CROSS-BACKEND COMMON UNIFORM DATA LAYOUT below! |
| 760 | |
| 761 | - A description of each storage buffer used in the shader: |
| 762 | - a boolean 'readonly' flag, note that currently only |
| 763 | readonly storage buffers are supported |
| 764 | - note that storage buffers are not supported on all backends |
| 765 | and platforms |
| 766 | |
| 767 | - A description of each texture/image used in the shader: |
| 768 | - the expected image type: |
| 769 | - SG_IMAGETYPE_2D |
| 770 | - SG_IMAGETYPE_CUBE |
| 771 | - SG_IMAGETYPE_3D |
| 772 | - SG_IMAGETYPE_ARRAY |
| 773 | - the expected 'image sample type': |
| 774 | - SG_IMAGESAMPLETYPE_FLOAT |
| 775 | - SG_IMAGESAMPLETYPE_DEPTH |
| 776 | - SG_IMAGESAMPLETYPE_SINT |
| 777 | - SG_IMAGESAMPLETYPE_UINT |
| 778 | - SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT |
| 779 | - a flag whether the texture is expected to be multisampled |
| 780 | (currently it's not supported to fetch data from multisampled |
| 781 | textures in shaders, but this is planned for a later time) |
| 782 | |
| 783 | - A description of each texture sampler used in the shader: |
| 784 | - SG_SAMPLERTYPE_FILTERING, |
| 785 | - SG_SAMPLERTYPE_NONFILTERING, |
| 786 | - SG_SAMPLERTYPE_COMPARISON, |
| 787 | |
| 788 | - An array of 'image-sampler-pairs' used by the shader to sample textures, |
| 789 | for D3D11, Metal and WebGPU this is used for validation purposes to check |
| 790 | whether the texture and sampler are compatible with each other (especially |
| 791 | WebGPU is very picky about combining the correct |
| 792 | texture-sample-type with the correct sampler-type). For GLSL an |
| 793 | additional 'combined-image-sampler name' must be provided because 'OpenGL |
| 794 | style GLSL' cannot handle separate texture and sampler objects, but still |
| 795 | groups them into a traditional GLSL 'sampler object'. |
| 796 | |
| 797 | Compatibility rules for image-sample-type vs sampler-type are as follows: |
| 798 | |
| 799 | - SG_IMAGESAMPLETYPE_FLOAT => (SG_SAMPLERTYPE_FILTERING or SG_SAMPLERTYPE_NONFILTERING) |
| 800 | - SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT => SG_SAMPLERTYPE_NONFILTERING |
| 801 | - SG_IMAGESAMPLETYPE_SINT => SG_SAMPLERTYPE_NONFILTERING |
| 802 | - SG_IMAGESAMPLETYPE_UINT => SG_SAMPLERTYPE_NONFILTERING |
| 803 | - SG_IMAGESAMPLETYPE_DEPTH => SG_SAMPLERTYPE_COMPARISON |
| 804 | |
| 805 | For example code of how to create backend-specific shader objects, |
| 806 | please refer to the following samples: |
| 807 | |
| 808 | - for D3D11: https://github.com/floooh/sokol-samples/tree/master/d3d11 |
| 809 | - for Metal: https://github.com/floooh/sokol-samples/tree/master/metal |
| 810 | - for OpenGL: https://github.com/floooh/sokol-samples/tree/master/glfw |
| 811 | - for GLES3: https://github.com/floooh/sokol-samples/tree/master/html5 |
| 812 | - for WebGPI: https://github.com/floooh/sokol-samples/tree/master/wgpu |
| 813 | |
| 814 | |
| 815 | ON SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT AND SG_SAMPLERTYPE_NONFILTERING |
| 816 | ======================================================================== |
| 817 | The WebGPU backend introduces the concept of 'unfilterable-float' textures, |
| 818 | which can only be combined with 'nonfiltering' samplers (this is a restriction |
| 819 | specific to WebGPU, but since the same sokol-gfx code should work across |
| 820 | all backend, the sokol-gfx validation layer also enforces this restriction |
| 821 | - the alternative would be undefined behaviour in some backend APIs on |
| 822 | some devices). |
| 823 | |
| 824 | The background is that some mobile devices (most notably iOS devices) can |
| 825 | not perform linear filtering when sampling textures with certain pixel |
| 826 | formats, most notable the 32F formats: |
| 827 | |
| 828 | - SG_PIXELFORMAT_R32F |
| 829 | - SG_PIXELFORMAT_RG32F |
| 830 | - SG_PIXELFORMAT_RGBA32F |
| 831 | |
| 832 | The information of whether a shader is going to be used with such an |
| 833 | unfilterable-float texture must already be provided in the sg_shader_desc |
| 834 | struct when creating the shader (see the above section "ON SHADER CREATION"). |
| 835 | |
| 836 | If you are using the sokol-shdc shader compiler, the information whether a |
| 837 | texture/sampler binding expects an 'unfilterable-float/nonfiltering' |
| 838 | texture/sampler combination cannot be inferred from the shader source |
| 839 | alone, you'll need to provide this hint via annotation-tags. For instance |
| 840 | here is an example from the ozz-skin-sapp.c sample shader which samples an |
| 841 | RGBA32F texture with skinning matrices in the vertex shader: |
| 842 | |
| 843 | ```glsl |
| 844 | @image_sample_type joint_tex unfilterable_float |
| 845 | uniform texture2D joint_tex; |
| 846 | @sampler_type smp nonfiltering |
| 847 | uniform sampler smp; |
| 848 | ``` |
| 849 | |
| 850 | This will result in SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT and |
| 851 | SG_SAMPLERTYPE_NONFILTERING being written to the code-generated |
| 852 | sg_shader_desc struct. |
| 853 | |
| 854 | |
| 855 | UNIFORM DATA LAYOUT: |
| 856 | ==================== |
| 857 | NOTE: if you use the sokol-shdc shader compiler tool, you don't need to worry |
| 858 | about the following details. |
| 859 | |
| 860 | The data that's passed into the sg_apply_uniforms() function must adhere to |
| 861 | specific layout rules so that the GPU shader finds the uniform block |
| 862 | items at the right offset. |
| 863 | |
| 864 | For the D3D11 and Metal backends, sokol-gfx only cares about the size of uniform |
| 865 | blocks, but not about the internal layout. The data will just be copied into |
| 866 | a uniform/constant buffer in a single operation and it's up you to arrange the |
| 867 | CPU-side layout so that it matches the GPU side layout. This also means that with |
| 868 | the D3D11 and Metal backends you are not limited to a 'cross-platform' subset |
| 869 | of uniform variable types. |
| 870 | |
| 871 | If you ever only use one of the D3D11, Metal *or* WebGPU backend, you can stop reading here. |
| 872 | |
| 873 | For the GL backends, the internal layout of uniform blocks matters though, |
| 874 | and you are limited to a small number of uniform variable types. This is |
| 875 | because sokol-gfx must be able to locate the uniform block members in order |
| 876 | to upload them to the GPU with glUniformXXX() calls. |
| 877 | |
| 878 | To describe the uniform block layout to sokol-gfx, the following information |
| 879 | must be passed to the sg_make_shader() call in the sg_shader_desc struct: |
| 880 | |
| 881 | - a hint about the used packing rule (either SG_UNIFORMLAYOUT_NATIVE or |
| 882 | SG_UNIFORMLAYOUT_STD140) |
| 883 | - a list of the uniform block members types in the correct order they |
| 884 | appear on the CPU side |
| 885 | |
| 886 | For example if the GLSL shader has the following uniform declarations: |
| 887 | |
| 888 | uniform mat4 mvp; |
| 889 | uniform vec2 offset0; |
| 890 | uniform vec2 offset1; |
| 891 | uniform vec2 offset2; |
| 892 | |
| 893 | ...and on the CPU side, there's a similar C struct: |
| 894 | |
| 895 | typedef struct { |
| 896 | float mvp[16]; |
| 897 | float offset0[2]; |
| 898 | float offset1[2]; |
| 899 | float offset2[2]; |
| 900 | } params_t; |
| 901 | |
| 902 | ...the uniform block description in the sg_shader_desc must look like this: |
| 903 | |
| 904 | sg_shader_desc desc = { |
| 905 | .vs.uniform_blocks[0] = { |
| 906 | .size = sizeof(params_t), |
| 907 | .layout = SG_UNIFORMLAYOUT_NATIVE, // this is the default and can be omitted |
| 908 | .uniforms = { |
| 909 | // order must be the same as in 'params_t': |
| 910 | [0] = { .name = "mvp", .type = SG_UNIFORMTYPE_MAT4 }, |
| 911 | [1] = { .name = "offset0", .type = SG_UNIFORMTYPE_VEC2 }, |
| 912 | [2] = { .name = "offset1", .type = SG_UNIFORMTYPE_VEC2 }, |
| 913 | [3] = { .name = "offset2", .type = SG_UNIFORMTYPE_VEC2 }, |
| 914 | } |
| 915 | } |
| 916 | }; |
| 917 | |
| 918 | With this information sokol-gfx can now compute the correct offsets of the data items |
| 919 | within the uniform block struct. |
| 920 | |
| 921 | The SG_UNIFORMLAYOUT_NATIVE packing rule works fine if only the GL backends are used, |
| 922 | but for proper D3D11/Metal/GL a subset of the std140 layout must be used which is |
| 923 | described in the next section: |
| 924 | |
| 925 | |
| 926 | CROSS-BACKEND COMMON UNIFORM DATA LAYOUT |
| 927 | ======================================== |
| 928 | For cross-platform / cross-3D-backend code it is important that the same uniform block |
| 929 | layout on the CPU side can be used for all sokol-gfx backends. To achieve this, |
| 930 | a common subset of the std140 layout must be used: |
| 931 | |
| 932 | - The uniform block layout hint in sg_shader_desc must be explicitly set to |
| 933 | SG_UNIFORMLAYOUT_STD140. |
| 934 | - Only the following GLSL uniform types can be used (with their associated sokol-gfx enums): |
| 935 | - float => SG_UNIFORMTYPE_FLOAT |
| 936 | - vec2 => SG_UNIFORMTYPE_FLOAT2 |
| 937 | - vec3 => SG_UNIFORMTYPE_FLOAT3 |
| 938 | - vec4 => SG_UNIFORMTYPE_FLOAT4 |
| 939 | - int => SG_UNIFORMTYPE_INT |
| 940 | - ivec2 => SG_UNIFORMTYPE_INT2 |
| 941 | - ivec3 => SG_UNIFORMTYPE_INT3 |
| 942 | - ivec4 => SG_UNIFORMTYPE_INT4 |
| 943 | - mat4 => SG_UNIFORMTYPE_MAT4 |
| 944 | - Alignment for those types must be as follows (in bytes): |
| 945 | - float => 4 |
| 946 | - vec2 => 8 |
| 947 | - vec3 => 16 |
| 948 | - vec4 => 16 |
| 949 | - int => 4 |
| 950 | - ivec2 => 8 |
| 951 | - ivec3 => 16 |
| 952 | - ivec4 => 16 |
| 953 | - mat4 => 16 |
| 954 | - Arrays are only allowed for the following types: vec4, int4, mat4. |
| 955 | |
| 956 | Note that the HLSL cbuffer layout rules are slightly different from the |
| 957 | std140 layout rules, this means that the cbuffer declarations in HLSL code |
| 958 | must be tweaked so that the layout is compatible with std140. |
| 959 | |
| 960 | The by far easiest way to tackle the common uniform block layout problem is |
| 961 | to use the sokol-shdc shader cross-compiler tool! |
| 962 | |
| 963 | ON STORAGE BUFFERS |
| 964 | ================== |
| 965 | Storage buffers can be used to pass large amounts of random access structured |
| 966 | data fromt the CPU side to the shaders. They are similar to data textures, but are |
| 967 | more convenient to use both on the CPU and shader side since they can be accessed |
| 968 | in shaders as as a 1-dimensional array of struct items. |
| 969 | |
| 970 | Storage buffers are *NOT* supported on the following platform/backend combos: |
| 971 | |
| 972 | - macOS+GL (because storage buffers require GL 4.3, while macOS only goes up to GL 4.1) |
| 973 | - all GLES3 platforms (WebGL2, iOS, Android - with the option that support on |
| 974 | Android may be added at a later point) |
| 975 | |
| 976 | Currently only 'readonly' storage buffers are supported (meaning it's not possible |
| 977 | to write to storage buffers from shaders). |
| 978 | |
| 979 | To use storage buffers, the following steps are required: |
| 980 | |
| 981 | - write a shader which uses storage buffers (also see the example links below) |
| 982 | - create one or more storage buffers via sg_make_buffer() with the |
| 983 | buffer type SG_BUFFERTYPE_STORAGEBUFFER |
| 984 | - when creating a shader via sg_make_shader(), populate the sg_shader_desc |
| 985 | struct with binding info (when using sokol-shdc, this step will be taken care |
| 986 | of automatically) |
| 987 | - which storage buffer bind slots on the vertex- and fragment-stage |
| 988 | are occupied |
| 989 | - whether the storage buffer on that bind slot is readonly (this is currently required |
| 990 | to be true) |
| 991 | - when calling sg_apply_bindings(), apply the matching bind slots with the previously |
| 992 | created storage buffers |
| 993 | - ...and that's it. |
| 994 | |
| 995 | For more details, see the following backend-agnostic sokol samples: |
| 996 | |
| 997 | - simple vertex pulling from a storage buffer: |
| 998 | - C code: https://github.com/floooh/sokol-samples/blob/master/sapp/vertexpull-sapp.c |
| 999 | - shader: https://github.com/floooh/sokol-samples/blob/master/sapp/vertexpull-sapp.glsl |
| 1000 | - instanced rendering via storage buffers (vertex- and instance-pulling): |
| 1001 | - C code: https://github.com/floooh/sokol-samples/blob/master/sapp/instancing-pull-sapp.c |
| 1002 | - shader: https://github.com/floooh/sokol-samples/blob/master/sapp/instancing-pull-sapp.glsl |
| 1003 | - storage buffers both on the vertex- and fragment-stage: |
| 1004 | - C code: https://github.com/floooh/sokol-samples/blob/master/sapp/sbuftex-sapp.c |
| 1005 | - shader: https://github.com/floooh/sokol-samples/blob/master/sapp/sbuftex-sapp.glsl |
| 1006 | - the Ozz animation sample rewritten to pull all rendering data from storage buffers: |
| 1007 | - C code: https://github.com/floooh/sokol-samples/blob/master/sapp/ozz-storagebuffer-sapp.cc |
| 1008 | - shader: https://github.com/floooh/sokol-samples/blob/master/sapp/ozz-storagebuffer-sapp.glsl |
| 1009 | |
| 1010 | ...also see the following backend-specific vertex pulling samples (those also don't use sokol-shdc): |
| 1011 | |
| 1012 | - D3D11: https://github.com/floooh/sokol-samples/blob/master/d3d11/vertexpulling-d3d11.c |
| 1013 | - desktop GL: https://github.com/floooh/sokol-samples/blob/master/glfw/vertexpulling-glfw.c |
| 1014 | - Metal: https://github.com/floooh/sokol-samples/blob/master/metal/vertexpulling-metal.c |
| 1015 | - WebGPU: https://github.com/floooh/sokol-samples/blob/master/wgpu/vertexpulling-wgpu.c |
| 1016 | |
| 1017 | Storage buffer shader authoring caveats when using sokol-shdc: |
| 1018 | |
| 1019 | - declare a storage buffer interface block with `readonly buffer [name] { ... }` |
| 1020 | - do NOT annotate storage buffers with `layout(...)`, sokol-shdc will take care of that |
| 1021 | - declare a struct which describes a single array item in the storage buffer interface block |
| 1022 | - only put a single flexible array member into the storage buffer interface block |
| 1023 | |
| 1024 | E.g. a complete example in 'sokol-shdc GLSL': |
| 1025 | |
| 1026 | ```glsl |
| 1027 | // declare a struct: |
| 1028 | struct sb_vertex { |
| 1029 | vec3 pos; |
| 1030 | vec4 color; |
| 1031 | } |
| 1032 | // declare a buffer interface block with a single flexible struct array: |
| 1033 | readonly buffer vertices { |
| 1034 | sb_vertex vtx[]; |
| 1035 | } |
| 1036 | // in the shader function, access the storage buffer like this: |
| 1037 | void main() { |
| 1038 | vec3 pos = vtx[gl_VertexIndex].pos; |
| 1039 | ... |
| 1040 | } |
| 1041 | ``` |
| 1042 | |
| 1043 | Backend-specific storage-buffer caveats (not relevant when using sokol-shdc): |
| 1044 | |
| 1045 | D3D11: |
| 1046 | - storage buffers are created as 'raw' Byte Address Buffers |
| 1047 | (https://learn.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-resources-intro#raw-views-of-buffers) |
| 1048 | - in HLSL, use a ByteAddressBuffer to access the buffer content |
| 1049 | (https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-byteaddressbuffer) |
| 1050 | - in D3D11, storage buffers and textures share the same bind slots, sokol-gfx reserves |
| 1051 | shader resource slots 0..15 for textures and 16..23 for storage buffers. |
| 1052 | - e.g. in HLSL, storage buffer bindings start at register(t16) no matter the shader stage |
| 1053 | |
| 1054 | Metal: |
| 1055 | - in Metal there is no internal difference between vertex-, uniform- and |
| 1056 | storage-buffers, all are bound to the same 'buffer bind slots' with the |
| 1057 | following reserved ranges: |
| 1058 | - vertex shader stage: |
| 1059 | - uniform buffers (internal): slots 0..3 |
| 1060 | - vertex buffers: slots 4..11 |
| 1061 | - storage buffers: slots 12..19 |
| 1062 | - fragment shader stage: |
| 1063 | - uniform buffers (internal): slots 0..3 |
| 1064 | - storage buffers: slots 4..11 |
| 1065 | - this means in MSL, storage buffer bindings start at [[buffer(12)]] in the vertex |
| 1066 | shaders, and at [[buffer(4)]] in fragment shaders |
| 1067 | |
| 1068 | GL: |
| 1069 | - the GL backend doesn't use name-lookup to find storage buffer bindings, this |
| 1070 | means you must annotate buffers with `layout(std430, binding=N)` in GLSL |
| 1071 | - ...where N is 0..7 in the vertex shader, and 8..15 in the fragment shader |
| 1072 | |
| 1073 | WebGPU: |
| 1074 | - in WGSL, use the following bind locations for the various shader resource types: |
| 1075 | - vertex shader stage: |
| 1076 | - textures `@group(1) @binding(0..15)` |
| 1077 | - samplers `@group(1) @binding(16..31)` |
| 1078 | - storage buffers `@group(1) @binding(32..47)` |
| 1079 | - fragment shader stage: |
| 1080 | - textures `@group(1) @binding(48..63)` |
| 1081 | - samplers `@group(1) @binding(64..79)` |
| 1082 | - storage buffers `@group(1) @binding(80..95)` |
| 1083 | |
| 1084 | TRACE HOOKS: |
| 1085 | ============ |
| 1086 | sokol_gfx.h optionally allows to install "trace hook" callbacks for |
| 1087 | each public API functions. When a public API function is called, and |
| 1088 | a trace hook callback has been installed for this function, the |
| 1089 | callback will be invoked with the parameters and result of the function. |
| 1090 | This is useful for things like debugging- and profiling-tools, or |
| 1091 | keeping track of resource creation and destruction. |
| 1092 | |
| 1093 | To use the trace hook feature: |
| 1094 | |
| 1095 | --- Define SOKOL_TRACE_HOOKS before including the implementation. |
| 1096 | |
| 1097 | --- Setup an sg_trace_hooks structure with your callback function |
| 1098 | pointers (keep all function pointers you're not interested |
| 1099 | in zero-initialized), optionally set the user_data member |
| 1100 | in the sg_trace_hooks struct. |
| 1101 | |
| 1102 | --- Install the trace hooks by calling sg_install_trace_hooks(), |
| 1103 | the return value of this function is another sg_trace_hooks |
| 1104 | struct which contains the previously set of trace hooks. |
| 1105 | You should keep this struct around, and call those previous |
| 1106 | functions pointers from your own trace callbacks for proper |
| 1107 | chaining. |
| 1108 | |
| 1109 | As an example of how trace hooks are used, have a look at the |
| 1110 | imgui/sokol_gfx_imgui.h header which implements a realtime |
| 1111 | debugging UI for sokol_gfx.h on top of Dear ImGui. |
| 1112 | |
| 1113 | |
| 1114 | A NOTE ON PORTABLE PACKED VERTEX FORMATS: |
| 1115 | ========================================= |
| 1116 | There are two things to consider when using packed |
| 1117 | vertex formats like UBYTE4, SHORT2, etc which need to work |
| 1118 | across all backends: |
| 1119 | |
| 1120 | - D3D11 can only convert *normalized* vertex formats to |
| 1121 | floating point during vertex fetch, normalized formats |
| 1122 | have a trailing 'N', and are "normalized" to a range |
| 1123 | -1.0..+1.0 (for the signed formats) or 0.0..1.0 (for the |
| 1124 | unsigned formats): |
| 1125 | |
| 1126 | - SG_VERTEXFORMAT_BYTE4N |
| 1127 | - SG_VERTEXFORMAT_UBYTE4N |
| 1128 | - SG_VERTEXFORMAT_SHORT2N |
| 1129 | - SG_VERTEXFORMAT_USHORT2N |
| 1130 | - SG_VERTEXFORMAT_SHORT4N |
| 1131 | - SG_VERTEXFORMAT_USHORT4N |
| 1132 | |
| 1133 | D3D11 will not convert *non-normalized* vertex formats to floating point |
| 1134 | vertex shader inputs, those can only be uses with the *ivecn* vertex shader |
| 1135 | input types when D3D11 is used as backend (GL and Metal can use both formats) |
| 1136 | |
| 1137 | - SG_VERTEXFORMAT_BYTE4, |
| 1138 | - SG_VERTEXFORMAT_UBYTE4 |
| 1139 | - SG_VERTEXFORMAT_SHORT2 |
| 1140 | - SG_VERTEXFORMAT_SHORT4 |
| 1141 | |
| 1142 | For a vertex input layout which works on all platforms, only use the following |
| 1143 | vertex formats, and if needed "expand" the normalized vertex shader |
| 1144 | inputs in the vertex shader by multiplying with 127.0, 255.0, 32767.0 or |
| 1145 | 65535.0: |
| 1146 | |
| 1147 | - SG_VERTEXFORMAT_FLOAT, |
| 1148 | - SG_VERTEXFORMAT_FLOAT2, |
| 1149 | - SG_VERTEXFORMAT_FLOAT3, |
| 1150 | - SG_VERTEXFORMAT_FLOAT4, |
| 1151 | - SG_VERTEXFORMAT_BYTE4N, |
| 1152 | - SG_VERTEXFORMAT_UBYTE4N, |
| 1153 | - SG_VERTEXFORMAT_SHORT2N, |
| 1154 | - SG_VERTEXFORMAT_USHORT2N |
| 1155 | - SG_VERTEXFORMAT_SHORT4N, |
| 1156 | - SG_VERTEXFORMAT_USHORT4N |
| 1157 | - SG_VERTEXFORMAT_UINT10_N2 |
| 1158 | - SG_VERTEXFORMAT_HALF2 |
| 1159 | - SG_VERTEXFORMAT_HALF4 |
| 1160 | |
| 1161 | |
| 1162 | MEMORY ALLOCATION OVERRIDE |
| 1163 | ========================== |
| 1164 | You can override the memory allocation functions at initialization time |
| 1165 | like this: |
| 1166 | |
| 1167 | void* my_alloc(size_t size, void* user_data) { |
| 1168 | return malloc(size); |
| 1169 | } |
| 1170 | |
| 1171 | void my_free(void* ptr, void* user_data) { |
| 1172 | free(ptr); |
| 1173 | } |
| 1174 | |
| 1175 | ... |
| 1176 | sg_setup(&(sg_desc){ |
| 1177 | // ... |
| 1178 | .allocator = { |
| 1179 | .alloc_fn = my_alloc, |
| 1180 | .free_fn = my_free, |
| 1181 | .user_data = ..., |
| 1182 | } |
| 1183 | }); |
| 1184 | ... |
| 1185 | |
| 1186 | If no overrides are provided, malloc and free will be used. |
| 1187 | |
| 1188 | This only affects memory allocation calls done by sokol_gfx.h |
| 1189 | itself though, not any allocations in OS libraries. |
| 1190 | |
| 1191 | |
| 1192 | ERROR REPORTING AND LOGGING |
| 1193 | =========================== |
| 1194 | To get any logging information at all you need to provide a logging callback in the setup call |
| 1195 | the easiest way is to use sokol_log.h: |
| 1196 | |
| 1197 | #include "sokol_log.h" |
| 1198 | |
| 1199 | sg_setup(&(sg_desc){ .logger.func = slog_func }); |
| 1200 | |
| 1201 | To override logging with your own callback, first write a logging function like this: |
| 1202 | |
| 1203 | void my_log(const char* tag, // e.g. 'sg' |
| 1204 | uint32_t log_level, // 0=panic, 1=error, 2=warn, 3=info |
| 1205 | uint32_t log_item_id, // SG_LOGITEM_* |
| 1206 | const char* message_or_null, // a message string, may be nullptr in release mode |
| 1207 | uint32_t line_nr, // line number in sokol_gfx.h |
| 1208 | const char* filename_or_null, // source filename, may be nullptr in release mode |
| 1209 | void* user_data) |
| 1210 | { |
| 1211 | ... |
| 1212 | } |
| 1213 | |
| 1214 | ...and then setup sokol-gfx like this: |
| 1215 | |
| 1216 | sg_setup(&(sg_desc){ |
| 1217 | .logger = { |
| 1218 | .func = my_log, |
| 1219 | .user_data = my_user_data, |
| 1220 | } |
| 1221 | }); |
| 1222 | |
| 1223 | The provided logging function must be reentrant (e.g. be callable from |
| 1224 | different threads). |
| 1225 | |
| 1226 | If you don't want to provide your own custom logger it is highly recommended to use |
| 1227 | the standard logger in sokol_log.h instead, otherwise you won't see any warnings or |
| 1228 | errors. |
| 1229 | |
| 1230 | |
| 1231 | COMMIT LISTENERS |
| 1232 | ================ |
| 1233 | It's possible to hook callback functions into sokol-gfx which are called from |
| 1234 | inside sg_commit() in unspecified order. This is mainly useful for libraries |
| 1235 | that build on top of sokol_gfx.h to be notified about the end/start of a frame. |
| 1236 | |
| 1237 | To add a commit listener, call: |
| 1238 | |
| 1239 | static void my_commit_listener(void* user_data) { |
| 1240 | ... |
| 1241 | } |
| 1242 | |
| 1243 | bool success = sg_add_commit_listener((sg_commit_listener){ |
| 1244 | .func = my_commit_listener, |
| 1245 | .user_data = ..., |
| 1246 | }); |
| 1247 | |
| 1248 | The function returns false if the internal array of commit listeners is full, |
| 1249 | or the same commit listener had already been added. |
| 1250 | |
| 1251 | If the function returns true, my_commit_listener() will be called each frame |
| 1252 | from inside sg_commit(). |
| 1253 | |
| 1254 | By default, 1024 distinct commit listeners can be added, but this number |
| 1255 | can be tweaked in the sg_setup() call: |
| 1256 | |
| 1257 | sg_setup(&(sg_desc){ |
| 1258 | .max_commit_listeners = 2048, |
| 1259 | }); |
| 1260 | |
| 1261 | An sg_commit_listener item is equal to another if both the function |
| 1262 | pointer and user_data field are equal. |
| 1263 | |
| 1264 | To remove a commit listener: |
| 1265 | |
| 1266 | bool success = sg_remove_commit_listener((sg_commit_listener){ |
| 1267 | .func = my_commit_listener, |
| 1268 | .user_data = ..., |
| 1269 | }); |
| 1270 | |
| 1271 | ...where the .func and .user_data field are equal to a previous |
| 1272 | sg_add_commit_listener() call. The function returns true if the commit |
| 1273 | listener item was found and removed, and false otherwise. |
| 1274 | |
| 1275 | |
| 1276 | RESOURCE CREATION AND DESTRUCTION IN DETAIL |
| 1277 | =========================================== |
| 1278 | The 'vanilla' way to create resource objects is with the 'make functions': |
| 1279 | |
| 1280 | sg_buffer sg_make_buffer(const sg_buffer_desc* desc) |
| 1281 | sg_image sg_make_image(const sg_image_desc* desc) |
| 1282 | sg_sampler sg_make_sampler(const sg_sampler_desc* desc) |
| 1283 | sg_shader sg_make_shader(const sg_shader_desc* desc) |
| 1284 | sg_pipeline sg_make_pipeline(const sg_pipeline_desc* desc) |
| 1285 | sg_attachments sg_make_attachments(const sg_attachments_desc* desc) |
| 1286 | |
| 1287 | This will result in one of three cases: |
| 1288 | |
| 1289 | 1. The returned handle is invalid. This happens when there are no more |
| 1290 | free slots in the resource pool for this resource type. An invalid |
| 1291 | handle is associated with the INVALID resource state, for instance: |
| 1292 | |
| 1293 | sg_buffer buf = sg_make_buffer(...) |
| 1294 | if (sg_query_buffer_state(buf) == SG_RESOURCESTATE_INVALID) { |
| 1295 | // buffer pool is exhausted |
| 1296 | } |
| 1297 | |
| 1298 | 2. The returned handle is valid, but creating the underlying resource |
| 1299 | has failed for some reason. This results in a resource object in the |
| 1300 | FAILED state. The reason *why* resource creation has failed differ |
| 1301 | by resource type. Look for log messages with more details. A failed |
| 1302 | resource state can be checked with: |
| 1303 | |
| 1304 | sg_buffer buf = sg_make_buffer(...) |
| 1305 | if (sg_query_buffer_state(buf) == SG_RESOURCESTATE_FAILED) { |
| 1306 | // creating the resource has failed |
| 1307 | } |
| 1308 | |
| 1309 | 3. And finally, if everything goes right, the returned resource is |
| 1310 | in resource state VALID and ready to use. This can be checked |
| 1311 | with: |
| 1312 | |
| 1313 | sg_buffer buf = sg_make_buffer(...) |
| 1314 | if (sg_query_buffer_state(buf) == SG_RESOURCESTATE_VALID) { |
| 1315 | // creating the resource has failed |
| 1316 | } |
| 1317 | |
| 1318 | When calling the 'make functions', the created resource goes through a number |
| 1319 | of states: |
| 1320 | |
| 1321 | - INITIAL: the resource slot associated with the new resource is currently |
| 1322 | free (technically, there is no resource yet, just an empty pool slot) |
| 1323 | - ALLOC: a handle for the new resource has been allocated, this just means |
| 1324 | a pool slot has been reserved. |
| 1325 | - VALID or FAILED: in VALID state any 3D API backend resource objects have |
| 1326 | been successfully created, otherwise if anything went wrong, the resource |
| 1327 | will be in FAILED state. |
| 1328 | |
| 1329 | Sometimes it makes sense to first grab a handle, but initialize the |
| 1330 | underlying resource at a later time. For instance when loading data |
| 1331 | asynchronously from a slow data source, you may know what buffers and |
| 1332 | textures are needed at an early stage of the loading process, but actually |
| 1333 | loading the buffer or texture content can only be completed at a later time. |
| 1334 | |
| 1335 | For such situations, sokol-gfx resource objects can be created in two steps. |
| 1336 | You can allocate a handle upfront with one of the 'alloc functions': |
| 1337 | |
| 1338 | sg_buffer sg_alloc_buffer(void) |
| 1339 | sg_image sg_alloc_image(void) |
| 1340 | sg_sampler sg_alloc_sampler(void) |
| 1341 | sg_shader sg_alloc_shader(void) |
| 1342 | sg_pipeline sg_alloc_pipeline(void) |
| 1343 | sg_attachments sg_alloc_attachments(void) |
| 1344 | |
| 1345 | This will return a handle with the underlying resource object in the |
| 1346 | ALLOC state: |
| 1347 | |
| 1348 | sg_image img = sg_alloc_image(); |
| 1349 | if (sg_query_image_state(img) == SG_RESOURCESTATE_ALLOC) { |
| 1350 | // allocating an image handle has succeeded, otherwise |
| 1351 | // the image pool is full |
| 1352 | } |
| 1353 | |
| 1354 | Such an 'incomplete' handle can be used in most sokol-gfx rendering functions |
| 1355 | without doing any harm, sokol-gfx will simply skip any rendering operation |
| 1356 | that involve resources which are not in VALID state. |
| 1357 | |
| 1358 | At a later time (for instance once the texture has completed loading |
| 1359 | asynchronously), the resource creation can be completed by calling one of |
| 1360 | the 'init functions', those functions take an existing resource handle and |
| 1361 | 'desc struct': |
| 1362 | |
| 1363 | void sg_init_buffer(sg_buffer buf, const sg_buffer_desc* desc) |
| 1364 | void sg_init_image(sg_image img, const sg_image_desc* desc) |
| 1365 | void sg_init_sampler(sg_sampler smp, const sg_sampler_desc* desc) |
| 1366 | void sg_init_shader(sg_shader shd, const sg_shader_desc* desc) |
| 1367 | void sg_init_pipeline(sg_pipeline pip, const sg_pipeline_desc* desc) |
| 1368 | void sg_init_attachments(sg_attachments atts, const sg_attachments_desc* desc) |
| 1369 | |
| 1370 | The init functions expect a resource in ALLOC state, and after the function |
| 1371 | returns, the resource will be either in VALID or FAILED state. Calling |
| 1372 | an 'alloc function' followed by the matching 'init function' is fully |
| 1373 | equivalent with calling the 'make function' alone. |
| 1374 | |
| 1375 | Destruction can also happen as a two-step process. The 'uninit functions' |
| 1376 | will put a resource object from the VALID or FAILED state back into the |
| 1377 | ALLOC state: |
| 1378 | |
| 1379 | void sg_uninit_buffer(sg_buffer buf) |
| 1380 | void sg_uninit_image(sg_image img) |
| 1381 | void sg_uninit_sampler(sg_sampler smp) |
| 1382 | void sg_uninit_shader(sg_shader shd) |
| 1383 | void sg_uninit_pipeline(sg_pipeline pip) |
| 1384 | void sg_uninit_attachments(sg_attachments pass) |
| 1385 | |
| 1386 | Calling the 'uninit functions' with a resource that is not in the VALID or |
| 1387 | FAILED state is a no-op. |
| 1388 | |
| 1389 | To finally free the pool slot for recycling call the 'dealloc functions': |
| 1390 | |
| 1391 | void sg_dealloc_buffer(sg_buffer buf) |
| 1392 | void sg_dealloc_image(sg_image img) |
| 1393 | void sg_dealloc_sampler(sg_sampler smp) |
| 1394 | void sg_dealloc_shader(sg_shader shd) |
| 1395 | void sg_dealloc_pipeline(sg_pipeline pip) |
| 1396 | void sg_dealloc_attachments(sg_attachments atts) |
| 1397 | |
| 1398 | Calling the 'dealloc functions' on a resource that's not in ALLOC state is |
| 1399 | a no-op, but will generate a warning log message. |
| 1400 | |
| 1401 | Calling an 'uninit function' and 'dealloc function' in sequence is equivalent |
| 1402 | with calling the associated 'destroy function': |
| 1403 | |
| 1404 | void sg_destroy_buffer(sg_buffer buf) |
| 1405 | void sg_destroy_image(sg_image img) |
| 1406 | void sg_destroy_sampler(sg_sampler smp) |
| 1407 | void sg_destroy_shader(sg_shader shd) |
| 1408 | void sg_destroy_pipeline(sg_pipeline pip) |
| 1409 | void sg_destroy_attachments(sg_attachments atts) |
| 1410 | |
| 1411 | The 'destroy functions' can be called on resources in any state and generally |
| 1412 | do the right thing (for instance if the resource is in ALLOC state, the destroy |
| 1413 | function will be equivalent to the 'dealloc function' and skip the 'uninit part'). |
| 1414 | |
| 1415 | And finally to close the circle, the 'fail functions' can be called to manually |
| 1416 | put a resource in ALLOC state into the FAILED state: |
| 1417 | |
| 1418 | sg_fail_buffer(sg_buffer buf) |
| 1419 | sg_fail_image(sg_image img) |
| 1420 | sg_fail_sampler(sg_sampler smp) |
| 1421 | sg_fail_shader(sg_shader shd) |
| 1422 | sg_fail_pipeline(sg_pipeline pip) |
| 1423 | sg_fail_attachments(sg_attachments atts) |
| 1424 | |
| 1425 | This is recommended if anything went wrong outside of sokol-gfx during asynchronous |
| 1426 | resource setup (for instance a file loading operation failed). In this case, |
| 1427 | the 'fail function' should be called instead of the 'init function'. |
| 1428 | |
| 1429 | Calling a 'fail function' on a resource that's not in ALLOC state is a no-op, |
| 1430 | but will generate a warning log message. |
| 1431 | |
| 1432 | NOTE: that two-step resource creation usually only makes sense for buffers |
| 1433 | and images, but not for samplers, shaders, pipelines or attachments. Most notably, trying |
| 1434 | to create a pipeline object with a shader that's not in VALID state will |
| 1435 | trigger a validation layer error, or if the validation layer is disabled, |
| 1436 | result in a pipeline object in FAILED state. Same when trying to create |
| 1437 | an attachments object with invalid image objects. |
| 1438 | |
| 1439 | |
| 1440 | WEBGPU CAVEATS |
| 1441 | ============== |
| 1442 | For a general overview and design notes of the WebGPU backend see: |
| 1443 | |
| 1444 | https://floooh.github.io/2023/10/16/sokol-webgpu.html |
| 1445 | |
| 1446 | In general, don't expect an automatic speedup when switching from the WebGL2 |
| 1447 | backend to the WebGPU backend. Some WebGPU functions currently actually |
| 1448 | have a higher CPU overhead than similar WebGL2 functions, leading to the |
| 1449 | paradoxical situation that some WebGPU code may be slower than similar WebGL2 |
| 1450 | code. |
| 1451 | |
| 1452 | - when writing WGSL shader code by hand, a specific bind-slot convention |
| 1453 | must be used: |
| 1454 | |
| 1455 | All uniform block structs must use `@group(0)`, with up to |
| 1456 | 4 uniform blocks per shader stage. |
| 1457 | - Vertex shader uniform block bindings must start at `@group(0) @binding(0)` |
| 1458 | - Fragment shader uniform blocks bindings must start at `@group(0) @binding(4)` |
| 1459 | |
| 1460 | All textures and samplers must use `@group(1)` and start at specific |
| 1461 | offsets depending on resource type and shader stage. |
| 1462 | - Vertex shader textures must start at `@group(1) @binding(0)` |
| 1463 | - Vertex shader samplers must start at `@group(1) @binding(16)` |
| 1464 | - Vertex shader storage buffers must start at `@group(1) @binding(32)` |
| 1465 | - Fragment shader textures must start at `@group(1) @binding(48)` |
| 1466 | - Fragment shader samplers must start at `@group(1) @binding(64)` |
| 1467 | - Fragment shader storage buffers must start at `@group(1) @binding(80)` |
| 1468 | |
| 1469 | Note that the actual number of allowed per-stage texture- and sampler-bindings |
| 1470 | in sokol-gfx is currently lower than the above ranges (currently only up to |
| 1471 | 12 textures, 8 samplers and 8 storage buffers are allowed per shader stage). |
| 1472 | |
| 1473 | If you use sokol-shdc to generate WGSL shader code, you don't need to worry |
| 1474 | about the above binding convention since sokol-shdc assigns bind slots |
| 1475 | automatically. |
| 1476 | |
| 1477 | - The sokol-gfx WebGPU backend uses the sg_desc.uniform_buffer_size item |
| 1478 | to allocate a single per-frame uniform buffer which must be big enough |
| 1479 | to hold all data written by sg_apply_uniforms() during a single frame, |
| 1480 | including a worst-case 256-byte alignment (e.g. each sg_apply_uniform |
| 1481 | call will cost 256 bytes of uniform buffer size). The default size |
| 1482 | is 4 MB, which is enough for 16384 sg_apply_uniform() calls per |
| 1483 | frame (assuming the uniform data 'payload' is less than 256 bytes |
| 1484 | per call). These rules are the same as for the Metal backend, so if |
| 1485 | you are already using the Metal backend you'll be fine. |
| 1486 | |
| 1487 | - sg_apply_bindings(): the sokol-gfx WebGPU backend implements a bindgroup |
| 1488 | cache to prevent excessive creation and destruction of BindGroup objects |
| 1489 | when calling sg_apply_bindings(). The number of slots in the bindgroups |
| 1490 | cache is defined in sg_desc.wgpu_bindgroups_cache_size when calling |
| 1491 | sg_setup. The cache size must be a power-of-2 number, with the default being |
| 1492 | 1024. The bindgroups cache behaviour can be observed by calling the new |
| 1493 | function sg_query_frame_stats(), where the following struct items are |
| 1494 | of interest: |
| 1495 | |
| 1496 | .wgpu.num_bindgroup_cache_hits |
| 1497 | .wgpu.num_bindgroup_cache_misses |
| 1498 | .wgpu.num_bindgroup_cache_collisions |
| 1499 | .wgpu.num_bindgroup_cache_vs_hash_key_mismatch |
| 1500 | |
| 1501 | The value to pay attention to is `.wgpu.num_bindgroup_cache_collisions`, |
| 1502 | if this number if consistently higher than a few percent of the |
| 1503 | .wgpu.num_set_bindgroup value, it might be a good idea to bump the |
| 1504 | bindgroups cache size to the next power-of-2. |
| 1505 | |
| 1506 | - sg_apply_viewport(): WebGPU currently has a unique restriction that viewport |
| 1507 | rectangles must be contained entirely within the framebuffer. As a shitty |
| 1508 | workaround sokol_gfx.h will clip incoming viewport rectangles against |
| 1509 | the framebuffer, but this will distort the clipspace-to-screenspace mapping. |
| 1510 | There's no proper way to handle this inside sokol_gfx.h, this must be fixed |
| 1511 | in a future WebGPU update. |
| 1512 | |
| 1513 | - The sokol shader compiler generally adds `diagnostic(off, derivative_uniformity);` |
| 1514 | into the WGSL output. Currently only the Chrome WebGPU implementation seems |
| 1515 | to recognize this. |
| 1516 | |
| 1517 | - The vertex format SG_VERTEXFORMAT_UINT10_N2 is currently not supported because |
| 1518 | WebGPU lacks a matching vertex format (this is currently being worked on though, |
| 1519 | as soon as the vertex format shows up in webgpu.h, sokol_gfx.h will add support. |
| 1520 | |
| 1521 | - Likewise, the following sokol-gfx vertex formats are not supported in WebGPU: |
| 1522 | R16, R16SN, RG16, RG16SN, RGBA16, RGBA16SN and all PVRTC compressed format. |
| 1523 | Unlike unsupported vertex formats, unsupported pixel formats can be queried |
| 1524 | in cross-backend code via sg_query_pixel_format() though. |
| 1525 | |
| 1526 | - The Emscripten WebGPU shim currently doesn't support the Closure minification |
| 1527 | post-link-step (e.g. currently the emcc argument '--closure 1' or '--closure 2' |
| 1528 | will generate broken Javascript code. |
| 1529 | |
| 1530 | - sokol-gfx requires the WebGPU device feature `depth32float-stencil8` to be enabled |
| 1531 | (this should be widely supported) |
| 1532 | |
| 1533 | - sokol-gfx expects that the WebGPU device feature `float32-filterable` to *not* be |
| 1534 | enabled (since this would exclude all iOS devices) |
| 1535 | |
| 1536 | |
| 1537 | LICENSE |
| 1538 | ======= |
| 1539 | zlib/libpng license |
| 1540 | |
| 1541 | Copyright (c) 2018 Andre Weissflog |
| 1542 | |
| 1543 | This software is provided 'as-is', without any express or implied warranty. |
| 1544 | In no event will the authors be held liable for any damages arising from the |
| 1545 | use of this software. |
| 1546 | |
| 1547 | Permission is granted to anyone to use this software for any purpose, |
| 1548 | including commercial applications, and to alter it and redistribute it |
| 1549 | freely, subject to the following restrictions: |
| 1550 | |
| 1551 | 1. The origin of this software must not be misrepresented; you must not |
| 1552 | claim that you wrote the original software. If you use this software in a |
| 1553 | product, an acknowledgment in the product documentation would be |
| 1554 | appreciated but is not required. |
| 1555 | |
| 1556 | 2. Altered source versions must be plainly marked as such, and must not |
| 1557 | be misrepresented as being the original software. |
| 1558 | |
| 1559 | 3. This notice may not be removed or altered from any source |
| 1560 | distribution. |
| 1561 | */ |
| 1562 | #define SOKOL_GFX_INCLUDED (1) |
| 1563 | #include <stddef.h> // size_t |
| 1564 | #include <stdint.h> |
| 1565 | #include <stdbool.h> |
| 1566 | |
| 1567 | #if defined(SOKOL_API_DECL) && !defined(SOKOL_GFX_API_DECL) |
| 1568 | #define SOKOL_GFX_API_DECL SOKOL_API_DECL |
| 1569 | #endif |
| 1570 | #ifndef SOKOL_GFX_API_DECL |
| 1571 | #if defined(_WIN32) && defined(SOKOL_DLL) && defined(SOKOL_GFX_IMPL) |
| 1572 | #define SOKOL_GFX_API_DECL __declspec(dllexport) |
| 1573 | #elif defined(_WIN32) && defined(SOKOL_DLL) |
| 1574 | #define SOKOL_GFX_API_DECL __declspec(dllimport) |
| 1575 | #else |
| 1576 | #define SOKOL_GFX_API_DECL extern |
| 1577 | #endif |
| 1578 | #endif |
| 1579 | |
| 1580 | #ifdef __cplusplus |
| 1581 | extern "C" { |
| 1582 | #endif |
| 1583 | |
| 1584 | /* |
| 1585 | Resource id typedefs: |
| 1586 | |
| 1587 | sg_buffer: vertex- and index-buffers |
| 1588 | sg_image: images used as textures and render targets |
| 1589 | sg_sampler sampler object describing how a texture is sampled in a shader |
| 1590 | sg_shader: vertex- and fragment-shaders and shader interface information |
| 1591 | sg_pipeline: associated shader and vertex-layouts, and render states |
| 1592 | sg_attachments: a baked collection of render pass attachment images |
| 1593 | |
| 1594 | Instead of pointers, resource creation functions return a 32-bit |
| 1595 | number which uniquely identifies the resource object. |
| 1596 | |
| 1597 | The 32-bit resource id is split into a 16-bit pool index in the lower bits, |
| 1598 | and a 16-bit 'generation counter' in the upper bits. The index allows fast |
| 1599 | pool lookups, and combined with the generation-counter it allows to detect |
| 1600 | 'dangling accesses' (trying to use an object which no longer exists, and |
| 1601 | its pool slot has been reused for a new object) |
| 1602 | |
| 1603 | The resource ids are wrapped into a strongly-typed struct so that |
| 1604 | trying to pass an incompatible resource id is a compile error. |
| 1605 | */ |
| 1606 | typedef struct sg_buffer { uint32_t id; } sg_buffer; |
| 1607 | typedef struct sg_image { uint32_t id; } sg_image; |
| 1608 | typedef struct sg_sampler { uint32_t id; } sg_sampler; |
| 1609 | typedef struct sg_shader { uint32_t id; } sg_shader; |
| 1610 | typedef struct sg_pipeline { uint32_t id; } sg_pipeline; |
| 1611 | typedef struct sg_attachments { uint32_t id; } sg_attachments; |
| 1612 | |
| 1613 | /* |
| 1614 | sg_range is a pointer-size-pair struct used to pass memory blobs into |
| 1615 | sokol-gfx. When initialized from a value type (array or struct), you can |
| 1616 | use the SG_RANGE() macro to build an sg_range struct. For functions which |
| 1617 | take either a sg_range pointer, or a (C++) sg_range reference, use the |
| 1618 | SG_RANGE_REF macro as a solution which compiles both in C and C++. |
| 1619 | */ |
| 1620 | typedef struct sg_range { |
| 1621 | const void* ptr; |
| 1622 | size_t size; |
| 1623 | } sg_range; |
| 1624 | |
| 1625 | // disabling this for every includer isn't great, but the warnings are also quite pointless |
| 1626 | #if defined(_MSC_VER) |
| 1627 | #pragma warning(disable:4221) // /W4 only: nonstandard extension used: 'x': cannot be initialized using address of automatic variable 'y' |
| 1628 | #pragma warning(disable:4204) // VS2015: nonstandard extension used: non-constant aggregate initializer |
| 1629 | #endif |
| 1630 | #if defined(__cplusplus) |
| 1631 | #define SG_RANGE(x) sg_range{ &x, sizeof(x) } |
| 1632 | #define SG_RANGE_REF(x) sg_range{ &x, sizeof(x) } |
| 1633 | #else |
| 1634 | #define SG_RANGE(x) (sg_range){ &x, sizeof(x) } |
| 1635 | #define SG_RANGE_REF(x) &(sg_range){ &x, sizeof(x) } |
| 1636 | #endif |
| 1637 | |
| 1638 | // various compile-time constants |
| 1639 | enum { |
| 1640 | SG_INVALID_ID = 0, |
| 1641 | SG_NUM_SHADER_STAGES = 2, |
| 1642 | SG_NUM_INFLIGHT_FRAMES = 2, |
| 1643 | SG_MAX_COLOR_ATTACHMENTS = 4, |
| 1644 | SG_MAX_VERTEX_BUFFERS = 8, |
| 1645 | SG_MAX_SHADERSTAGE_IMAGES = 12, |
| 1646 | SG_MAX_SHADERSTAGE_SAMPLERS = 8, |
| 1647 | SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS = 12, |
| 1648 | SG_MAX_SHADERSTAGE_STORAGEBUFFERS = 8, |
| 1649 | SG_MAX_SHADERSTAGE_UBS = 4, |
| 1650 | SG_MAX_UB_MEMBERS = 16, |
| 1651 | SG_MAX_VERTEX_ATTRIBUTES = 16, |
| 1652 | SG_MAX_MIPMAPS = 16, |
| 1653 | SG_MAX_TEXTUREARRAY_LAYERS = 128 |
| 1654 | }; |
| 1655 | |
| 1656 | /* |
| 1657 | sg_color |
| 1658 | |
| 1659 | An RGBA color value. |
| 1660 | */ |
| 1661 | typedef struct sg_color { float r, g, b, a; } sg_color; |
| 1662 | |
| 1663 | /* |
| 1664 | sg_backend |
| 1665 | |
| 1666 | The active 3D-API backend, use the function sg_query_backend() |
| 1667 | to get the currently active backend. |
| 1668 | */ |
| 1669 | typedef enum sg_backend { |
| 1670 | SG_BACKEND_GLCORE, |
| 1671 | SG_BACKEND_GLES3, |
| 1672 | SG_BACKEND_D3D11, |
| 1673 | SG_BACKEND_METAL_IOS, |
| 1674 | SG_BACKEND_METAL_MACOS, |
| 1675 | SG_BACKEND_METAL_SIMULATOR, |
| 1676 | SG_BACKEND_WGPU, |
| 1677 | SG_BACKEND_DUMMY, |
| 1678 | } sg_backend; |
| 1679 | |
| 1680 | /* |
| 1681 | sg_pixel_format |
| 1682 | |
| 1683 | sokol_gfx.h basically uses the same pixel formats as WebGPU, since these |
| 1684 | are supported on most newer GPUs. |
| 1685 | |
| 1686 | A pixelformat name consist of three parts: |
| 1687 | |
| 1688 | - components (R, RG, RGB or RGBA) |
| 1689 | - bit width per component (8, 16 or 32) |
| 1690 | - component data type: |
| 1691 | - unsigned normalized (no postfix) |
| 1692 | - signed normalized (SN postfix) |
| 1693 | - unsigned integer (UI postfix) |
| 1694 | - signed integer (SI postfix) |
| 1695 | - float (F postfix) |
| 1696 | |
| 1697 | Not all pixel formats can be used for everything, call sg_query_pixelformat() |
| 1698 | to inspect the capabilities of a given pixelformat. The function returns |
| 1699 | an sg_pixelformat_info struct with the following members: |
| 1700 | |
| 1701 | - sample: the pixelformat can be sampled as texture at least with |
| 1702 | nearest filtering |
| 1703 | - filter: the pixelformat can be samples as texture with linear |
| 1704 | filtering |
| 1705 | - render: the pixelformat can be used for render targets |
| 1706 | - blend: blending is supported when using the pixelformat for |
| 1707 | render targets |
| 1708 | - msaa: multisample-antialiasing is supported when using the |
| 1709 | pixelformat for render targets |
| 1710 | - depth: the pixelformat can be used for depth-stencil attachments |
| 1711 | - compressed: this is a block-compressed format |
| 1712 | - bytes_per_pixel: the numbers of bytes in a pixel (0 for compressed formats) |
| 1713 | |
| 1714 | The default pixel format for texture images is SG_PIXELFORMAT_RGBA8. |
| 1715 | |
| 1716 | The default pixel format for render target images is platform-dependent |
| 1717 | and taken from the sg_environment struct passed into sg_setup(). Typically |
| 1718 | the default formats are: |
| 1719 | |
| 1720 | - for the Metal, D3D11 and WebGPU backends: SG_PIXELFORMAT_BGRA8 |
| 1721 | - for GL backends: SG_PIXELFORMAT_RGBA8 |
| 1722 | */ |
| 1723 | typedef enum sg_pixel_format { |
| 1724 | _SG_PIXELFORMAT_DEFAULT, // value 0 reserved for default-init |
| 1725 | SG_PIXELFORMAT_NONE, |
| 1726 | |
| 1727 | SG_PIXELFORMAT_R8, |
| 1728 | SG_PIXELFORMAT_R8SN, |
| 1729 | SG_PIXELFORMAT_R8UI, |
| 1730 | SG_PIXELFORMAT_R8SI, |
| 1731 | |
| 1732 | SG_PIXELFORMAT_R16, |
| 1733 | SG_PIXELFORMAT_R16SN, |
| 1734 | SG_PIXELFORMAT_R16UI, |
| 1735 | SG_PIXELFORMAT_R16SI, |
| 1736 | SG_PIXELFORMAT_R16F, |
| 1737 | SG_PIXELFORMAT_RG8, |
| 1738 | SG_PIXELFORMAT_RG8SN, |
| 1739 | SG_PIXELFORMAT_RG8UI, |
| 1740 | SG_PIXELFORMAT_RG8SI, |
| 1741 | |
| 1742 | SG_PIXELFORMAT_R32UI, |
| 1743 | SG_PIXELFORMAT_R32SI, |
| 1744 | SG_PIXELFORMAT_R32F, |
| 1745 | SG_PIXELFORMAT_RG16, |
| 1746 | SG_PIXELFORMAT_RG16SN, |
| 1747 | SG_PIXELFORMAT_RG16UI, |
| 1748 | SG_PIXELFORMAT_RG16SI, |
| 1749 | SG_PIXELFORMAT_RG16F, |
| 1750 | SG_PIXELFORMAT_RGBA8, |
| 1751 | SG_PIXELFORMAT_SRGB8A8, |
| 1752 | SG_PIXELFORMAT_RGBA8SN, |
| 1753 | SG_PIXELFORMAT_RGBA8UI, |
| 1754 | SG_PIXELFORMAT_RGBA8SI, |
| 1755 | SG_PIXELFORMAT_BGRA8, |
| 1756 | SG_PIXELFORMAT_RGB10A2, |
| 1757 | SG_PIXELFORMAT_RG11B10F, |
| 1758 | SG_PIXELFORMAT_RGB9E5, |
| 1759 | |
| 1760 | SG_PIXELFORMAT_RG32UI, |
| 1761 | SG_PIXELFORMAT_RG32SI, |
| 1762 | SG_PIXELFORMAT_RG32F, |
| 1763 | SG_PIXELFORMAT_RGBA16, |
| 1764 | SG_PIXELFORMAT_RGBA16SN, |
| 1765 | SG_PIXELFORMAT_RGBA16UI, |
| 1766 | SG_PIXELFORMAT_RGBA16SI, |
| 1767 | SG_PIXELFORMAT_RGBA16F, |
| 1768 | |
| 1769 | SG_PIXELFORMAT_RGBA32UI, |
| 1770 | SG_PIXELFORMAT_RGBA32SI, |
| 1771 | SG_PIXELFORMAT_RGBA32F, |
| 1772 | |
| 1773 | // NOTE: when adding/removing pixel formats before DEPTH, also update sokol_app.h/_SAPP_PIXELFORMAT_* |
| 1774 | SG_PIXELFORMAT_DEPTH, |
| 1775 | SG_PIXELFORMAT_DEPTH_STENCIL, |
| 1776 | |
| 1777 | // NOTE: don't put any new compressed format in front of here |
| 1778 | SG_PIXELFORMAT_BC1_RGBA, |
| 1779 | SG_PIXELFORMAT_BC2_RGBA, |
| 1780 | SG_PIXELFORMAT_BC3_RGBA, |
| 1781 | SG_PIXELFORMAT_BC3_SRGBA, |
| 1782 | SG_PIXELFORMAT_BC4_R, |
| 1783 | SG_PIXELFORMAT_BC4_RSN, |
| 1784 | SG_PIXELFORMAT_BC5_RG, |
| 1785 | SG_PIXELFORMAT_BC5_RGSN, |
| 1786 | SG_PIXELFORMAT_BC6H_RGBF, |
| 1787 | SG_PIXELFORMAT_BC6H_RGBUF, |
| 1788 | SG_PIXELFORMAT_BC7_RGBA, |
| 1789 | SG_PIXELFORMAT_BC7_SRGBA, |
| 1790 | SG_PIXELFORMAT_PVRTC_RGB_2BPP, // FIXME: deprecated |
| 1791 | SG_PIXELFORMAT_PVRTC_RGB_4BPP, // FIXME: deprecated |
| 1792 | SG_PIXELFORMAT_PVRTC_RGBA_2BPP, // FIXME: deprecated |
| 1793 | SG_PIXELFORMAT_PVRTC_RGBA_4BPP, // FIXME: deprecated |
| 1794 | SG_PIXELFORMAT_ETC2_RGB8, |
| 1795 | SG_PIXELFORMAT_ETC2_SRGB8, |
| 1796 | SG_PIXELFORMAT_ETC2_RGB8A1, |
| 1797 | SG_PIXELFORMAT_ETC2_RGBA8, |
| 1798 | SG_PIXELFORMAT_ETC2_SRGB8A8, |
| 1799 | SG_PIXELFORMAT_ETC2_RG11, |
| 1800 | SG_PIXELFORMAT_ETC2_RG11SN, |
| 1801 | |
| 1802 | SG_PIXELFORMAT_ASTC_4x4_RGBA, |
| 1803 | SG_PIXELFORMAT_ASTC_4x4_SRGBA, |
| 1804 | |
| 1805 | _SG_PIXELFORMAT_NUM, |
| 1806 | _SG_PIXELFORMAT_FORCE_U32 = 0x7FFFFFFF |
| 1807 | } sg_pixel_format; |
| 1808 | |
| 1809 | /* |
| 1810 | Runtime information about a pixel format, returned |
| 1811 | by sg_query_pixelformat(). |
| 1812 | */ |
| 1813 | typedef struct sg_pixelformat_info { |
| 1814 | bool sample; // pixel format can be sampled in shaders at least with nearest filtering |
| 1815 | bool filter; // pixel format can be sampled with linear filtering |
| 1816 | bool render; // pixel format can be used as render target |
| 1817 | bool blend; // alpha-blending is supported |
| 1818 | bool msaa; // pixel format can be used as MSAA render target |
| 1819 | bool depth; // pixel format is a depth format |
| 1820 | bool compressed; // true if this is a hardware-compressed format |
| 1821 | int bytes_per_pixel; // NOTE: this is 0 for compressed formats, use sg_query_row_pitch() / sg_query_surface_pitch() as alternative |
| 1822 | } sg_pixelformat_info; |
| 1823 | |
| 1824 | /* |
| 1825 | Runtime information about available optional features, |
| 1826 | returned by sg_query_features() |
| 1827 | */ |
| 1828 | typedef struct sg_features { |
| 1829 | bool origin_top_left; // framebuffer and texture origin is in top left corner |
| 1830 | bool image_clamp_to_border; // border color and clamp-to-border UV-wrap mode is supported |
| 1831 | bool mrt_independent_blend_state; // multiple-render-target rendering can use per-render-target blend state |
| 1832 | bool mrt_independent_write_mask; // multiple-render-target rendering can use per-render-target color write masks |
| 1833 | bool storage_buffer; // storage buffers are supported |
| 1834 | } sg_features; |
| 1835 | |
| 1836 | /* |
| 1837 | Runtime information about resource limits, returned by sg_query_limit() |
| 1838 | */ |
| 1839 | typedef struct sg_limits { |
| 1840 | int max_image_size_2d; // max width/height of SG_IMAGETYPE_2D images |
| 1841 | int max_image_size_cube; // max width/height of SG_IMAGETYPE_CUBE images |
| 1842 | int max_image_size_3d; // max width/height/depth of SG_IMAGETYPE_3D images |
| 1843 | int max_image_size_array; // max width/height of SG_IMAGETYPE_ARRAY images |
| 1844 | int max_image_array_layers; // max number of layers in SG_IMAGETYPE_ARRAY images |
| 1845 | int max_vertex_attrs; // max number of vertex attributes, clamped to SG_MAX_VERTEX_ATTRIBUTES |
| 1846 | int gl_max_vertex_uniform_components; // <= GL_MAX_VERTEX_UNIFORM_COMPONENTS (only on GL backends) |
| 1847 | int gl_max_combined_texture_image_units; // <= GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS (only on GL backends) |
| 1848 | } sg_limits; |
| 1849 | |
| 1850 | /* |
| 1851 | sg_resource_state |
| 1852 | |
| 1853 | The current state of a resource in its resource pool. |
| 1854 | Resources start in the INITIAL state, which means the |
| 1855 | pool slot is unoccupied and can be allocated. When a resource is |
| 1856 | created, first an id is allocated, and the resource pool slot |
| 1857 | is set to state ALLOC. After allocation, the resource is |
| 1858 | initialized, which may result in the VALID or FAILED state. The |
| 1859 | reason why allocation and initialization are separate is because |
| 1860 | some resource types (e.g. buffers and images) might be asynchronously |
| 1861 | initialized by the user application. If a resource which is not |
| 1862 | in the VALID state is attempted to be used for rendering, rendering |
| 1863 | operations will silently be dropped. |
| 1864 | |
| 1865 | The special INVALID state is returned in sg_query_xxx_state() if no |
| 1866 | resource object exists for the provided resource id. |
| 1867 | */ |
| 1868 | typedef enum sg_resource_state { |
| 1869 | SG_RESOURCESTATE_INITIAL, |
| 1870 | SG_RESOURCESTATE_ALLOC, |
| 1871 | SG_RESOURCESTATE_VALID, |
| 1872 | SG_RESOURCESTATE_FAILED, |
| 1873 | SG_RESOURCESTATE_INVALID, |
| 1874 | _SG_RESOURCESTATE_FORCE_U32 = 0x7FFFFFFF |
| 1875 | } sg_resource_state; |
| 1876 | |
| 1877 | /* |
| 1878 | sg_usage |
| 1879 | |
| 1880 | A resource usage hint describing the update strategy of |
| 1881 | buffers and images. This is used in the sg_buffer_desc.usage |
| 1882 | and sg_image_desc.usage members when creating buffers |
| 1883 | and images: |
| 1884 | |
| 1885 | SG_USAGE_IMMUTABLE: the resource will never be updated with |
| 1886 | new data, instead the content of the |
| 1887 | resource must be provided on creation |
| 1888 | SG_USAGE_DYNAMIC: the resource will be updated infrequently |
| 1889 | with new data (this could range from "once |
| 1890 | after creation", to "quite often but not |
| 1891 | every frame") |
| 1892 | SG_USAGE_STREAM: the resource will be updated each frame |
| 1893 | with new content |
| 1894 | |
| 1895 | The rendering backends use this hint to prevent that the |
| 1896 | CPU needs to wait for the GPU when attempting to update |
| 1897 | a resource that might be currently accessed by the GPU. |
| 1898 | |
| 1899 | Resource content is updated with the functions sg_update_buffer() or |
| 1900 | sg_append_buffer() for buffer objects, and sg_update_image() for image |
| 1901 | objects. For the sg_update_*() functions, only one update is allowed per |
| 1902 | frame and resource object, while sg_append_buffer() can be called |
| 1903 | multiple times per frame on the same buffer. The application must update |
| 1904 | all data required for rendering (this means that the update data can be |
| 1905 | smaller than the resource size, if only a part of the overall resource |
| 1906 | size is used for rendering, you only need to make sure that the data that |
| 1907 | *is* used is valid). |
| 1908 | |
| 1909 | The default usage is SG_USAGE_IMMUTABLE. |
| 1910 | */ |
| 1911 | typedef enum sg_usage { |
| 1912 | _SG_USAGE_DEFAULT, // value 0 reserved for default-init |
| 1913 | SG_USAGE_IMMUTABLE, |
| 1914 | SG_USAGE_DYNAMIC, |
| 1915 | SG_USAGE_STREAM, |
| 1916 | _SG_USAGE_NUM, |
| 1917 | _SG_USAGE_FORCE_U32 = 0x7FFFFFFF |
| 1918 | } sg_usage; |
| 1919 | |
| 1920 | /* |
| 1921 | sg_buffer_type |
| 1922 | |
| 1923 | Indicates whether a buffer will be bound as vertex-, |
| 1924 | index- or storage-buffer. |
| 1925 | |
| 1926 | Used in the sg_buffer_desc.type member when creating a buffer. |
| 1927 | |
| 1928 | The default value is SG_BUFFERTYPE_VERTEXBUFFER. |
| 1929 | */ |
| 1930 | typedef enum sg_buffer_type { |
| 1931 | _SG_BUFFERTYPE_DEFAULT, // value 0 reserved for default-init |
| 1932 | SG_BUFFERTYPE_VERTEXBUFFER, |
| 1933 | SG_BUFFERTYPE_INDEXBUFFER, |
| 1934 | SG_BUFFERTYPE_STORAGEBUFFER, |
| 1935 | _SG_BUFFERTYPE_NUM, |
| 1936 | _SG_BUFFERTYPE_FORCE_U32 = 0x7FFFFFFF |
| 1937 | } sg_buffer_type; |
| 1938 | |
| 1939 | /* |
| 1940 | sg_index_type |
| 1941 | |
| 1942 | Indicates whether indexed rendering (fetching vertex-indices from an |
| 1943 | index buffer) is used, and if yes, the index data type (16- or 32-bits). |
| 1944 | This is used in the sg_pipeline_desc.index_type member when creating a |
| 1945 | pipeline object. |
| 1946 | |
| 1947 | The default index type is SG_INDEXTYPE_NONE. |
| 1948 | */ |
| 1949 | typedef enum sg_index_type { |
| 1950 | _SG_INDEXTYPE_DEFAULT, // value 0 reserved for default-init |
| 1951 | SG_INDEXTYPE_NONE, |
| 1952 | SG_INDEXTYPE_UINT16, |
| 1953 | SG_INDEXTYPE_UINT32, |
| 1954 | _SG_INDEXTYPE_NUM, |
| 1955 | _SG_INDEXTYPE_FORCE_U32 = 0x7FFFFFFF |
| 1956 | } sg_index_type; |
| 1957 | |
| 1958 | /* |
| 1959 | sg_image_type |
| 1960 | |
| 1961 | Indicates the basic type of an image object (2D-texture, cubemap, |
| 1962 | 3D-texture or 2D-array-texture). Used in the sg_image_desc.type member when |
| 1963 | creating an image, and in sg_shader_image_desc to describe a sampled texture |
| 1964 | in the shader (both must match and will be checked in the validation layer |
| 1965 | when calling sg_apply_bindings). |
| 1966 | |
| 1967 | The default image type when creating an image is SG_IMAGETYPE_2D. |
| 1968 | */ |
| 1969 | typedef enum sg_image_type { |
| 1970 | _SG_IMAGETYPE_DEFAULT, // value 0 reserved for default-init |
| 1971 | SG_IMAGETYPE_2D, |
| 1972 | SG_IMAGETYPE_CUBE, |
| 1973 | SG_IMAGETYPE_3D, |
| 1974 | SG_IMAGETYPE_ARRAY, |
| 1975 | _SG_IMAGETYPE_NUM, |
| 1976 | _SG_IMAGETYPE_FORCE_U32 = 0x7FFFFFFF |
| 1977 | } sg_image_type; |
| 1978 | |
| 1979 | /* |
| 1980 | sg_image_sample_type |
| 1981 | |
| 1982 | The basic data type of a texture sample as expected by a shader. |
| 1983 | Must be provided in sg_shader_image_desc and used by the validation |
| 1984 | layer in sg_apply_bindings() to check if the provided image object |
| 1985 | is compatible with what the shader expects. Apart from the sokol-gfx |
| 1986 | validation layer, WebGPU is the only backend API which actually requires |
| 1987 | matching texture and sampler type to be provided upfront for validation |
| 1988 | (other 3D APIs treat texture/sampler type mismatches as undefined behaviour). |
| 1989 | |
| 1990 | NOTE that the following texture pixel formats require the use |
| 1991 | of SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT, combined with a sampler |
| 1992 | of type SG_SAMPLERTYPE_NONFILTERING: |
| 1993 | |
| 1994 | - SG_PIXELFORMAT_R32F |
| 1995 | - SG_PIXELFORMAT_RG32F |
| 1996 | - SG_PIXELFORMAT_RGBA32F |
| 1997 | |
| 1998 | (when using sokol-shdc, also check out the meta tags `@image_sample_type` |
| 1999 | and `@sampler_type`) |
| 2000 | */ |
| 2001 | typedef enum sg_image_sample_type { |
| 2002 | _SG_IMAGESAMPLETYPE_DEFAULT, // value 0 reserved for default-init |
| 2003 | SG_IMAGESAMPLETYPE_FLOAT, |
| 2004 | SG_IMAGESAMPLETYPE_DEPTH, |
| 2005 | SG_IMAGESAMPLETYPE_SINT, |
| 2006 | SG_IMAGESAMPLETYPE_UINT, |
| 2007 | SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT, |
| 2008 | _SG_IMAGESAMPLETYPE_NUM, |
| 2009 | _SG_IMAGESAMPLETYPE_FORCE_U32 = 0x7FFFFFFF |
| 2010 | } sg_image_sample_type; |
| 2011 | |
| 2012 | /* |
| 2013 | sg_sampler_type |
| 2014 | |
| 2015 | The basic type of a texture sampler (sampling vs comparison) as |
| 2016 | defined in a shader. Must be provided in sg_shader_sampler_desc. |
| 2017 | |
| 2018 | sg_image_sample_type and sg_sampler_type for a texture/sampler |
| 2019 | pair must be compatible with each other, specifically only |
| 2020 | the following pairs are allowed: |
| 2021 | |
| 2022 | - SG_IMAGESAMPLETYPE_FLOAT => (SG_SAMPLERTYPE_FILTERING or SG_SAMPLERTYPE_NONFILTERING) |
| 2023 | - SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT => SG_SAMPLERTYPE_NONFILTERING |
| 2024 | - SG_IMAGESAMPLETYPE_SINT => SG_SAMPLERTYPE_NONFILTERING |
| 2025 | - SG_IMAGESAMPLETYPE_UINT => SG_SAMPLERTYPE_NONFILTERING |
| 2026 | - SG_IMAGESAMPLETYPE_DEPTH => SG_SAMPLERTYPE_COMPARISON |
| 2027 | */ |
| 2028 | typedef enum sg_sampler_type { |
| 2029 | _SG_SAMPLERTYPE_DEFAULT, |
| 2030 | SG_SAMPLERTYPE_FILTERING, |
| 2031 | SG_SAMPLERTYPE_NONFILTERING, |
| 2032 | SG_SAMPLERTYPE_COMPARISON, |
| 2033 | _SG_SAMPLERTYPE_NUM, |
| 2034 | _SG_SAMPLERTYPE_FORCE_U32, |
| 2035 | } sg_sampler_type; |
| 2036 | |
| 2037 | /* |
| 2038 | sg_cube_face |
| 2039 | |
| 2040 | The cubemap faces. Use these as indices in the sg_image_desc.content |
| 2041 | array. |
| 2042 | */ |
| 2043 | typedef enum sg_cube_face { |
| 2044 | SG_CUBEFACE_POS_X, |
| 2045 | SG_CUBEFACE_NEG_X, |
| 2046 | SG_CUBEFACE_POS_Y, |
| 2047 | SG_CUBEFACE_NEG_Y, |
| 2048 | SG_CUBEFACE_POS_Z, |
| 2049 | SG_CUBEFACE_NEG_Z, |
| 2050 | SG_CUBEFACE_NUM, |
| 2051 | _SG_CUBEFACE_FORCE_U32 = 0x7FFFFFFF |
| 2052 | } sg_cube_face; |
| 2053 | |
| 2054 | /* |
| 2055 | sg_shader_stage |
| 2056 | |
| 2057 | There are 2 shader stages: vertex- and fragment-shader-stage. |
| 2058 | Each shader stage |
| 2059 | |
| 2060 | - SG_MAX_SHADERSTAGE_UBS slots for applying uniform data |
| 2061 | - SG_MAX_SHADERSTAGE_IMAGES slots for images used as textures |
| 2062 | - SG_MAX_SHADERSTAGE_SAMPLERS slots for texture samplers |
| 2063 | - SG_MAX_SHADERSTAGE_STORAGEBUFFERS slots for storage buffer bindings |
| 2064 | */ |
| 2065 | typedef enum sg_shader_stage { |
| 2066 | SG_SHADERSTAGE_VS, |
| 2067 | SG_SHADERSTAGE_FS, |
| 2068 | _SG_SHADERSTAGE_FORCE_U32 = 0x7FFFFFFF |
| 2069 | } sg_shader_stage; |
| 2070 | |
| 2071 | /* |
| 2072 | sg_primitive_type |
| 2073 | |
| 2074 | This is the common subset of 3D primitive types supported across all 3D |
| 2075 | APIs. This is used in the sg_pipeline_desc.primitive_type member when |
| 2076 | creating a pipeline object. |
| 2077 | |
| 2078 | The default primitive type is SG_PRIMITIVETYPE_TRIANGLES. |
| 2079 | */ |
| 2080 | typedef enum sg_primitive_type { |
| 2081 | _SG_PRIMITIVETYPE_DEFAULT, // value 0 reserved for default-init |
| 2082 | SG_PRIMITIVETYPE_POINTS, |
| 2083 | SG_PRIMITIVETYPE_LINES, |
| 2084 | SG_PRIMITIVETYPE_LINE_STRIP, |
| 2085 | SG_PRIMITIVETYPE_TRIANGLES, |
| 2086 | SG_PRIMITIVETYPE_TRIANGLE_STRIP, |
| 2087 | _SG_PRIMITIVETYPE_NUM, |
| 2088 | _SG_PRIMITIVETYPE_FORCE_U32 = 0x7FFFFFFF |
| 2089 | } sg_primitive_type; |
| 2090 | |
| 2091 | /* |
| 2092 | sg_filter |
| 2093 | |
| 2094 | The filtering mode when sampling a texture image. This is |
| 2095 | used in the sg_sampler_desc.min_filter, sg_sampler_desc.mag_filter |
| 2096 | and sg_sampler_desc.mipmap_filter members when creating a sampler object. |
| 2097 | |
| 2098 | For min_filter and mag_filter the default is SG_FILTER_NEAREST. |
| 2099 | |
| 2100 | For mipmap_filter the default is SG_FILTER_NONE. |
| 2101 | */ |
| 2102 | typedef enum sg_filter { |
| 2103 | _SG_FILTER_DEFAULT, // value 0 reserved for default-init |
| 2104 | SG_FILTER_NONE, // FIXME: deprecated |
| 2105 | SG_FILTER_NEAREST, |
| 2106 | SG_FILTER_LINEAR, |
| 2107 | _SG_FILTER_NUM, |
| 2108 | _SG_FILTER_FORCE_U32 = 0x7FFFFFFF |
| 2109 | } sg_filter; |
| 2110 | |
| 2111 | /* |
| 2112 | sg_wrap |
| 2113 | |
| 2114 | The texture coordinates wrapping mode when sampling a texture |
| 2115 | image. This is used in the sg_image_desc.wrap_u, .wrap_v |
| 2116 | and .wrap_w members when creating an image. |
| 2117 | |
| 2118 | The default wrap mode is SG_WRAP_REPEAT. |
| 2119 | |
| 2120 | NOTE: SG_WRAP_CLAMP_TO_BORDER is not supported on all backends |
| 2121 | and platforms. To check for support, call sg_query_features() |
| 2122 | and check the "clamp_to_border" boolean in the returned |
| 2123 | sg_features struct. |
| 2124 | |
| 2125 | Platforms which don't support SG_WRAP_CLAMP_TO_BORDER will silently fall back |
| 2126 | to SG_WRAP_CLAMP_TO_EDGE without a validation error. |
| 2127 | */ |
| 2128 | typedef enum sg_wrap { |
| 2129 | _SG_WRAP_DEFAULT, // value 0 reserved for default-init |
| 2130 | SG_WRAP_REPEAT, |
| 2131 | SG_WRAP_CLAMP_TO_EDGE, |
| 2132 | SG_WRAP_CLAMP_TO_BORDER, |
| 2133 | SG_WRAP_MIRRORED_REPEAT, |
| 2134 | _SG_WRAP_NUM, |
| 2135 | _SG_WRAP_FORCE_U32 = 0x7FFFFFFF |
| 2136 | } sg_wrap; |
| 2137 | |
| 2138 | /* |
| 2139 | sg_border_color |
| 2140 | |
| 2141 | The border color to use when sampling a texture, and the UV wrap |
| 2142 | mode is SG_WRAP_CLAMP_TO_BORDER. |
| 2143 | |
| 2144 | The default border color is SG_BORDERCOLOR_OPAQUE_BLACK |
| 2145 | */ |
| 2146 | typedef enum sg_border_color { |
| 2147 | _SG_BORDERCOLOR_DEFAULT, // value 0 reserved for default-init |
| 2148 | SG_BORDERCOLOR_TRANSPARENT_BLACK, |
| 2149 | SG_BORDERCOLOR_OPAQUE_BLACK, |
| 2150 | SG_BORDERCOLOR_OPAQUE_WHITE, |
| 2151 | _SG_BORDERCOLOR_NUM, |
| 2152 | _SG_BORDERCOLOR_FORCE_U32 = 0x7FFFFFFF |
| 2153 | } sg_border_color; |
| 2154 | |
| 2155 | /* |
| 2156 | sg_vertex_format |
| 2157 | |
| 2158 | The data type of a vertex component. This is used to describe |
| 2159 | the layout of vertex data when creating a pipeline object. |
| 2160 | */ |
| 2161 | typedef enum sg_vertex_format { |
| 2162 | SG_VERTEXFORMAT_INVALID, |
| 2163 | SG_VERTEXFORMAT_FLOAT, |
| 2164 | SG_VERTEXFORMAT_FLOAT2, |
| 2165 | SG_VERTEXFORMAT_FLOAT3, |
| 2166 | SG_VERTEXFORMAT_FLOAT4, |
| 2167 | SG_VERTEXFORMAT_BYTE4, |
| 2168 | SG_VERTEXFORMAT_BYTE4N, |
| 2169 | SG_VERTEXFORMAT_UBYTE4, |
| 2170 | SG_VERTEXFORMAT_UBYTE4N, |
| 2171 | SG_VERTEXFORMAT_SHORT2, |
| 2172 | SG_VERTEXFORMAT_SHORT2N, |
| 2173 | SG_VERTEXFORMAT_USHORT2N, |
| 2174 | SG_VERTEXFORMAT_SHORT4, |
| 2175 | SG_VERTEXFORMAT_SHORT4N, |
| 2176 | SG_VERTEXFORMAT_USHORT4N, |
| 2177 | SG_VERTEXFORMAT_UINT10_N2, |
| 2178 | SG_VERTEXFORMAT_HALF2, |
| 2179 | SG_VERTEXFORMAT_HALF4, |
| 2180 | _SG_VERTEXFORMAT_NUM, |
| 2181 | _SG_VERTEXFORMAT_FORCE_U32 = 0x7FFFFFFF |
| 2182 | } sg_vertex_format; |
| 2183 | |
| 2184 | /* |
| 2185 | sg_vertex_step |
| 2186 | |
| 2187 | Defines whether the input pointer of a vertex input stream is advanced |
| 2188 | 'per vertex' or 'per instance'. The default step-func is |
| 2189 | SG_VERTEXSTEP_PER_VERTEX. SG_VERTEXSTEP_PER_INSTANCE is used with |
| 2190 | instanced-rendering. |
| 2191 | |
| 2192 | The vertex-step is part of the vertex-layout definition |
| 2193 | when creating pipeline objects. |
| 2194 | */ |
| 2195 | typedef enum sg_vertex_step { |
| 2196 | _SG_VERTEXSTEP_DEFAULT, // value 0 reserved for default-init |
| 2197 | SG_VERTEXSTEP_PER_VERTEX, |
| 2198 | SG_VERTEXSTEP_PER_INSTANCE, |
| 2199 | _SG_VERTEXSTEP_NUM, |
| 2200 | _SG_VERTEXSTEP_FORCE_U32 = 0x7FFFFFFF |
| 2201 | } sg_vertex_step; |
| 2202 | |
| 2203 | /* |
| 2204 | sg_uniform_type |
| 2205 | |
| 2206 | The data type of a uniform block member. This is used to |
| 2207 | describe the internal layout of uniform blocks when creating |
| 2208 | a shader object. |
| 2209 | */ |
| 2210 | typedef enum sg_uniform_type { |
| 2211 | SG_UNIFORMTYPE_INVALID, |
| 2212 | SG_UNIFORMTYPE_FLOAT, |
| 2213 | SG_UNIFORMTYPE_FLOAT2, |
| 2214 | SG_UNIFORMTYPE_FLOAT3, |
| 2215 | SG_UNIFORMTYPE_FLOAT4, |
| 2216 | SG_UNIFORMTYPE_INT, |
| 2217 | SG_UNIFORMTYPE_INT2, |
| 2218 | SG_UNIFORMTYPE_INT3, |
| 2219 | SG_UNIFORMTYPE_INT4, |
| 2220 | SG_UNIFORMTYPE_MAT4, |
| 2221 | _SG_UNIFORMTYPE_NUM, |
| 2222 | _SG_UNIFORMTYPE_FORCE_U32 = 0x7FFFFFFF |
| 2223 | } sg_uniform_type; |
| 2224 | |
| 2225 | /* |
| 2226 | sg_uniform_layout |
| 2227 | |
| 2228 | A hint for the interior memory layout of uniform blocks. This is |
| 2229 | only really relevant for the GL backend where the internal layout |
| 2230 | of uniform blocks must be known to sokol-gfx. For all other backends the |
| 2231 | internal memory layout of uniform blocks doesn't matter, sokol-gfx |
| 2232 | will just pass uniform data as a single memory blob to the |
| 2233 | 3D backend. |
| 2234 | |
| 2235 | SG_UNIFORMLAYOUT_NATIVE (default) |
| 2236 | Native layout means that a 'backend-native' memory layout |
| 2237 | is used. For the GL backend this means that uniforms |
| 2238 | are packed tightly in memory (e.g. there are no padding |
| 2239 | bytes). |
| 2240 | |
| 2241 | SG_UNIFORMLAYOUT_STD140 |
| 2242 | The memory layout is a subset of std140. Arrays are only |
| 2243 | allowed for the FLOAT4, INT4 and MAT4. Alignment is as |
| 2244 | is as follows: |
| 2245 | |
| 2246 | FLOAT, INT: 4 byte alignment |
| 2247 | FLOAT2, INT2: 8 byte alignment |
| 2248 | FLOAT3, INT3: 16 byte alignment(!) |
| 2249 | FLOAT4, INT4: 16 byte alignment |
| 2250 | MAT4: 16 byte alignment |
| 2251 | FLOAT4[], INT4[]: 16 byte alignment |
| 2252 | |
| 2253 | The overall size of the uniform block must be a multiple |
| 2254 | of 16. |
| 2255 | |
| 2256 | For more information search for 'UNIFORM DATA LAYOUT' in the documentation block |
| 2257 | at the start of the header. |
| 2258 | */ |
| 2259 | typedef enum sg_uniform_layout { |
| 2260 | _SG_UNIFORMLAYOUT_DEFAULT, // value 0 reserved for default-init |
| 2261 | SG_UNIFORMLAYOUT_NATIVE, // default: layout depends on currently active backend |
| 2262 | SG_UNIFORMLAYOUT_STD140, // std140: memory layout according to std140 |
| 2263 | _SG_UNIFORMLAYOUT_NUM, |
| 2264 | _SG_UNIFORMLAYOUT_FORCE_U32 = 0x7FFFFFFF |
| 2265 | } sg_uniform_layout; |
| 2266 | |
| 2267 | /* |
| 2268 | sg_cull_mode |
| 2269 | |
| 2270 | The face-culling mode, this is used in the |
| 2271 | sg_pipeline_desc.cull_mode member when creating a |
| 2272 | pipeline object. |
| 2273 | |
| 2274 | The default cull mode is SG_CULLMODE_NONE |
| 2275 | */ |
| 2276 | typedef enum sg_cull_mode { |
| 2277 | _SG_CULLMODE_DEFAULT, // value 0 reserved for default-init |
| 2278 | SG_CULLMODE_NONE, |
| 2279 | SG_CULLMODE_FRONT, |
| 2280 | SG_CULLMODE_BACK, |
| 2281 | _SG_CULLMODE_NUM, |
| 2282 | _SG_CULLMODE_FORCE_U32 = 0x7FFFFFFF |
| 2283 | } sg_cull_mode; |
| 2284 | |
| 2285 | /* |
| 2286 | sg_face_winding |
| 2287 | |
| 2288 | The vertex-winding rule that determines a front-facing primitive. This |
| 2289 | is used in the member sg_pipeline_desc.face_winding |
| 2290 | when creating a pipeline object. |
| 2291 | |
| 2292 | The default winding is SG_FACEWINDING_CW (clockwise) |
| 2293 | */ |
| 2294 | typedef enum sg_face_winding { |
| 2295 | _SG_FACEWINDING_DEFAULT, // value 0 reserved for default-init |
| 2296 | SG_FACEWINDING_CCW, |
| 2297 | SG_FACEWINDING_CW, |
| 2298 | _SG_FACEWINDING_NUM, |
| 2299 | _SG_FACEWINDING_FORCE_U32 = 0x7FFFFFFF |
| 2300 | } sg_face_winding; |
| 2301 | |
| 2302 | /* |
| 2303 | sg_compare_func |
| 2304 | |
| 2305 | The compare-function for configuring depth- and stencil-ref tests |
| 2306 | in pipeline objects, and for texture samplers which perform a comparison |
| 2307 | instead of regular sampling operation. |
| 2308 | |
| 2309 | sg_pipeline_desc |
| 2310 | .depth |
| 2311 | .compare |
| 2312 | .stencil |
| 2313 | .front.compare |
| 2314 | .back.compar |
| 2315 | |
| 2316 | sg_sampler_desc |
| 2317 | .compare |
| 2318 | |
| 2319 | The default compare func for depth- and stencil-tests is |
| 2320 | SG_COMPAREFUNC_ALWAYS. |
| 2321 | |
| 2322 | The default compare func for sampler is SG_COMPAREFUNC_NEVER. |
| 2323 | */ |
| 2324 | typedef enum sg_compare_func { |
| 2325 | _SG_COMPAREFUNC_DEFAULT, // value 0 reserved for default-init |
| 2326 | SG_COMPAREFUNC_NEVER, |
| 2327 | SG_COMPAREFUNC_LESS, |
| 2328 | SG_COMPAREFUNC_EQUAL, |
| 2329 | SG_COMPAREFUNC_LESS_EQUAL, |
| 2330 | SG_COMPAREFUNC_GREATER, |
| 2331 | SG_COMPAREFUNC_NOT_EQUAL, |
| 2332 | SG_COMPAREFUNC_GREATER_EQUAL, |
| 2333 | SG_COMPAREFUNC_ALWAYS, |
| 2334 | _SG_COMPAREFUNC_NUM, |
| 2335 | _SG_COMPAREFUNC_FORCE_U32 = 0x7FFFFFFF |
| 2336 | } sg_compare_func; |
| 2337 | |
| 2338 | /* |
| 2339 | sg_stencil_op |
| 2340 | |
| 2341 | The operation performed on a currently stored stencil-value when a |
| 2342 | comparison test passes or fails. This is used when creating a pipeline |
| 2343 | object in the members: |
| 2344 | |
| 2345 | sg_pipeline_desc |
| 2346 | .stencil |
| 2347 | .front |
| 2348 | .fail_op |
| 2349 | .depth_fail_op |
| 2350 | .pass_op |
| 2351 | .back |
| 2352 | .fail_op |
| 2353 | .depth_fail_op |
| 2354 | .pass_op |
| 2355 | |
| 2356 | The default value is SG_STENCILOP_KEEP. |
| 2357 | */ |
| 2358 | typedef enum sg_stencil_op { |
| 2359 | _SG_STENCILOP_DEFAULT, // value 0 reserved for default-init |
| 2360 | SG_STENCILOP_KEEP, |
| 2361 | SG_STENCILOP_ZERO, |
| 2362 | SG_STENCILOP_REPLACE, |
| 2363 | SG_STENCILOP_INCR_CLAMP, |
| 2364 | SG_STENCILOP_DECR_CLAMP, |
| 2365 | SG_STENCILOP_INVERT, |
| 2366 | SG_STENCILOP_INCR_WRAP, |
| 2367 | SG_STENCILOP_DECR_WRAP, |
| 2368 | _SG_STENCILOP_NUM, |
| 2369 | _SG_STENCILOP_FORCE_U32 = 0x7FFFFFFF |
| 2370 | } sg_stencil_op; |
| 2371 | |
| 2372 | /* |
| 2373 | sg_blend_factor |
| 2374 | |
| 2375 | The source and destination factors in blending operations. |
| 2376 | This is used in the following members when creating a pipeline object: |
| 2377 | |
| 2378 | sg_pipeline_desc |
| 2379 | .colors[i] |
| 2380 | .blend |
| 2381 | .src_factor_rgb |
| 2382 | .dst_factor_rgb |
| 2383 | .src_factor_alpha |
| 2384 | .dst_factor_alpha |
| 2385 | |
| 2386 | The default value is SG_BLENDFACTOR_ONE for source |
| 2387 | factors, and SG_BLENDFACTOR_ZERO for destination factors. |
| 2388 | */ |
| 2389 | typedef enum sg_blend_factor { |
| 2390 | _SG_BLENDFACTOR_DEFAULT, // value 0 reserved for default-init |
| 2391 | SG_BLENDFACTOR_ZERO, |
| 2392 | SG_BLENDFACTOR_ONE, |
| 2393 | SG_BLENDFACTOR_SRC_COLOR, |
| 2394 | SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR, |
| 2395 | SG_BLENDFACTOR_SRC_ALPHA, |
| 2396 | SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, |
| 2397 | SG_BLENDFACTOR_DST_COLOR, |
| 2398 | SG_BLENDFACTOR_ONE_MINUS_DST_COLOR, |
| 2399 | SG_BLENDFACTOR_DST_ALPHA, |
| 2400 | SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA, |
| 2401 | SG_BLENDFACTOR_SRC_ALPHA_SATURATED, |
| 2402 | SG_BLENDFACTOR_BLEND_COLOR, |
| 2403 | SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR, |
| 2404 | SG_BLENDFACTOR_BLEND_ALPHA, |
| 2405 | SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA, |
| 2406 | _SG_BLENDFACTOR_NUM, |
| 2407 | _SG_BLENDFACTOR_FORCE_U32 = 0x7FFFFFFF |
| 2408 | } sg_blend_factor; |
| 2409 | |
| 2410 | /* |
| 2411 | sg_blend_op |
| 2412 | |
| 2413 | Describes how the source and destination values are combined in the |
| 2414 | fragment blending operation. It is used in the following members when |
| 2415 | creating a pipeline object: |
| 2416 | |
| 2417 | sg_pipeline_desc |
| 2418 | .colors[i] |
| 2419 | .blend |
| 2420 | .op_rgb |
| 2421 | .op_alpha |
| 2422 | |
| 2423 | The default value is SG_BLENDOP_ADD. |
| 2424 | */ |
| 2425 | typedef enum sg_blend_op { |
| 2426 | _SG_BLENDOP_DEFAULT, // value 0 reserved for default-init |
| 2427 | SG_BLENDOP_ADD, |
| 2428 | SG_BLENDOP_SUBTRACT, |
| 2429 | SG_BLENDOP_REVERSE_SUBTRACT, |
| 2430 | _SG_BLENDOP_NUM, |
| 2431 | _SG_BLENDOP_FORCE_U32 = 0x7FFFFFFF |
| 2432 | } sg_blend_op; |
| 2433 | |
| 2434 | /* |
| 2435 | sg_color_mask |
| 2436 | |
| 2437 | Selects the active color channels when writing a fragment color to the |
| 2438 | framebuffer. This is used in the members |
| 2439 | sg_pipeline_desc.colors[i].write_mask when creating a pipeline object. |
| 2440 | |
| 2441 | The default colormask is SG_COLORMASK_RGBA (write all colors channels) |
| 2442 | |
| 2443 | NOTE: since the color mask value 0 is reserved for the default value |
| 2444 | (SG_COLORMASK_RGBA), use SG_COLORMASK_NONE if all color channels |
| 2445 | should be disabled. |
| 2446 | */ |
| 2447 | typedef enum sg_color_mask { |
| 2448 | _SG_COLORMASK_DEFAULT = 0, // value 0 reserved for default-init |
| 2449 | SG_COLORMASK_NONE = 0x10, // special value for 'all channels disabled |
| 2450 | SG_COLORMASK_R = 0x1, |
| 2451 | SG_COLORMASK_G = 0x2, |
| 2452 | SG_COLORMASK_RG = 0x3, |
| 2453 | SG_COLORMASK_B = 0x4, |
| 2454 | SG_COLORMASK_RB = 0x5, |
| 2455 | SG_COLORMASK_GB = 0x6, |
| 2456 | SG_COLORMASK_RGB = 0x7, |
| 2457 | SG_COLORMASK_A = 0x8, |
| 2458 | SG_COLORMASK_RA = 0x9, |
| 2459 | SG_COLORMASK_GA = 0xA, |
| 2460 | SG_COLORMASK_RGA = 0xB, |
| 2461 | SG_COLORMASK_BA = 0xC, |
| 2462 | SG_COLORMASK_RBA = 0xD, |
| 2463 | SG_COLORMASK_GBA = 0xE, |
| 2464 | SG_COLORMASK_RGBA = 0xF, |
| 2465 | _SG_COLORMASK_FORCE_U32 = 0x7FFFFFFF |
| 2466 | } sg_color_mask; |
| 2467 | |
| 2468 | /* |
| 2469 | sg_load_action |
| 2470 | |
| 2471 | Defines the load action that should be performed at the start of a render pass: |
| 2472 | |
| 2473 | SG_LOADACTION_CLEAR: clear the render target |
| 2474 | SG_LOADACTION_LOAD: load the previous content of the render target |
| 2475 | SG_LOADACTION_DONTCARE: leave the render target in an undefined state |
| 2476 | |
| 2477 | This is used in the sg_pass_action structure. |
| 2478 | |
| 2479 | The default load action for all pass attachments is SG_LOADACTION_CLEAR, |
| 2480 | with the values rgba = { 0.5f, 0.5f, 0.5f, 1.0f }, depth=1.0f and stencil=0. |
| 2481 | |
| 2482 | If you want to override the default behaviour, it is important to not |
| 2483 | only set the clear color, but the 'action' field as well (as long as this |
| 2484 | is _SG_LOADACTION_DEFAULT, the value fields will be ignored). |
| 2485 | */ |
| 2486 | typedef enum sg_load_action { |
| 2487 | _SG_LOADACTION_DEFAULT, |
| 2488 | SG_LOADACTION_CLEAR, |
| 2489 | SG_LOADACTION_LOAD, |
| 2490 | SG_LOADACTION_DONTCARE, |
| 2491 | _SG_LOADACTION_FORCE_U32 = 0x7FFFFFFF |
| 2492 | } sg_load_action; |
| 2493 | |
| 2494 | /* |
| 2495 | sg_store_action |
| 2496 | |
| 2497 | Defines the store action that be performed at the end of a render pass: |
| 2498 | |
| 2499 | SG_STOREACTION_STORE: store the rendered content to the color attachment image |
| 2500 | SG_STOREACTION_DONTCARE: allows the GPU to discard the rendered content |
| 2501 | */ |
| 2502 | typedef enum sg_store_action { |
| 2503 | _SG_STOREACTION_DEFAULT, |
| 2504 | SG_STOREACTION_STORE, |
| 2505 | SG_STOREACTION_DONTCARE, |
| 2506 | _SG_STOREACTION_FORCE_U32 = 0x7FFFFFFF |
| 2507 | } sg_store_action; |
| 2508 | |
| 2509 | |
| 2510 | /* |
| 2511 | sg_pass_action |
| 2512 | |
| 2513 | The sg_pass_action struct defines the actions to be performed |
| 2514 | at the start and end of a render pass. |
| 2515 | |
| 2516 | - at the start of the pass: whether the render targets should be cleared, |
| 2517 | loaded with their previous content, or start in an undefined state |
| 2518 | - for clear operations: the clear value (color, depth, or stencil values) |
| 2519 | - at the end of the pass: whether the rendering result should be |
| 2520 | stored back into the render target or discarded |
| 2521 | */ |
| 2522 | typedef struct sg_color_attachment_action { |
| 2523 | sg_load_action load_action; // default: SG_LOADACTION_CLEAR |
| 2524 | sg_store_action store_action; // default: SG_STOREACTION_STORE |
| 2525 | sg_color clear_value; // default: { 0.5f, 0.5f, 0.5f, 1.0f } |
| 2526 | } sg_color_attachment_action; |
| 2527 | |
| 2528 | typedef struct sg_depth_attachment_action { |
| 2529 | sg_load_action load_action; // default: SG_LOADACTION_CLEAR |
| 2530 | sg_store_action store_action; // default: SG_STOREACTION_DONTCARE |
| 2531 | float clear_value; // default: 1.0 |
| 2532 | } sg_depth_attachment_action; |
| 2533 | |
| 2534 | typedef struct sg_stencil_attachment_action { |
| 2535 | sg_load_action load_action; // default: SG_LOADACTION_CLEAR |
| 2536 | sg_store_action store_action; // default: SG_STOREACTION_DONTCARE |
| 2537 | uint8_t clear_value; // default: 0 |
| 2538 | } sg_stencil_attachment_action; |
| 2539 | |
| 2540 | typedef struct sg_pass_action { |
| 2541 | sg_color_attachment_action colors[SG_MAX_COLOR_ATTACHMENTS]; |
| 2542 | sg_depth_attachment_action depth; |
| 2543 | sg_stencil_attachment_action stencil; |
| 2544 | } sg_pass_action; |
| 2545 | |
| 2546 | /* |
| 2547 | sg_swapchain |
| 2548 | |
| 2549 | Used in sg_begin_pass() to provide details about an external swapchain |
| 2550 | (pixel formats, sample count and backend-API specific render surface objects). |
| 2551 | |
| 2552 | The following information must be provided: |
| 2553 | |
| 2554 | - the width and height of the swapchain surfaces in number of pixels, |
| 2555 | - the pixel format of the render- and optional msaa-resolve-surface |
| 2556 | - the pixel format of the optional depth- or depth-stencil-surface |
| 2557 | - the MSAA sample count for the render and depth-stencil surface |
| 2558 | |
| 2559 | If the pixel formats and MSAA sample counts are left zero-initialized, |
| 2560 | their defaults are taken from the sg_environment struct provided in the |
| 2561 | sg_setup() call. |
| 2562 | |
| 2563 | The width and height *must* be > 0. |
| 2564 | |
| 2565 | Additionally the following backend API specific objects must be passed in |
| 2566 | as 'type erased' void pointers: |
| 2567 | |
| 2568 | GL: on all GL backends, a GL framebuffer object must be provided. This |
| 2569 | can be zero for the default framebuffer. |
| 2570 | |
| 2571 | D3D11: |
| 2572 | - an ID3D11RenderTargetView for the rendering surface, without |
| 2573 | MSAA rendering this surface will also be displayed |
| 2574 | - an optional ID3D11DepthStencilView for the depth- or depth/stencil |
| 2575 | buffer surface |
| 2576 | - when MSAA rendering is used, another ID3D11RenderTargetView |
| 2577 | which serves as MSAA resolve target and will be displayed |
| 2578 | |
| 2579 | WebGPU (same as D3D11, except different types) |
| 2580 | - a WGPUTextureView for the rendering surface, without |
| 2581 | MSAA rendering this surface will also be displayed |
| 2582 | - an optional WGPUTextureView for the depth- or depth/stencil |
| 2583 | buffer surface |
| 2584 | - when MSAA rendering is used, another WGPUTextureView |
| 2585 | which serves as MSAA resolve target and will be displayed |
| 2586 | |
| 2587 | Metal (NOTE that the rolves of provided surfaces is slightly different |
| 2588 | than on D3D11 or WebGPU in case of MSAA vs non-MSAA rendering): |
| 2589 | |
| 2590 | - A current CAMetalDrawable (NOT an MTLDrawable!) which will be presented. |
| 2591 | This will either be rendered to directly (if no MSAA is used), or serve |
| 2592 | as MSAA-resolve target. |
| 2593 | - an optional MTLTexture for the depth- or depth-stencil buffer |
| 2594 | - an optional multisampled MTLTexture which serves as intermediate |
| 2595 | rendering surface which will then be resolved into the |
| 2596 | CAMetalDrawable. |
| 2597 | |
| 2598 | NOTE that for Metal you must use an ObjC __bridge cast to |
| 2599 | properly tunnel the ObjC object handle through a C void*, e.g.: |
| 2600 | |
| 2601 | swapchain.metal.current_drawable = (__bridge const void*) [mtkView currentDrawable]; |
| 2602 | |
| 2603 | On all other backends you shouldn't need to mess with the reference count. |
| 2604 | |
| 2605 | It's a good practice to write a helper function which returns an initialized |
| 2606 | sg_swapchain structs, which can then be plugged directly into |
| 2607 | sg_pass.swapchain. Look at the function sglue_swapchain() in the sokol_glue.h |
| 2608 | as an example. |
| 2609 | */ |
| 2610 | typedef struct sg_metal_swapchain { |
| 2611 | const void* current_drawable; // CAMetalDrawable (NOT MTLDrawable!!!) |
| 2612 | const void* depth_stencil_texture; // MTLTexture |
| 2613 | const void* msaa_color_texture; // MTLTexture |
| 2614 | } sg_metal_swapchain; |
| 2615 | |
| 2616 | typedef struct sg_d3d11_swapchain { |
| 2617 | const void* render_view; // ID3D11RenderTargetView |
| 2618 | const void* resolve_view; // ID3D11RenderTargetView |
| 2619 | const void* depth_stencil_view; // ID3D11DepthStencilView |
| 2620 | } sg_d3d11_swapchain; |
| 2621 | |
| 2622 | typedef struct sg_wgpu_swapchain { |
| 2623 | const void* render_view; // WGPUTextureView |
| 2624 | const void* resolve_view; // WGPUTextureView |
| 2625 | const void* depth_stencil_view; // WGPUTextureView |
| 2626 | } sg_wgpu_swapchain; |
| 2627 | |
| 2628 | typedef struct sg_gl_swapchain { |
| 2629 | uint32_t framebuffer; // GL framebuffer object |
| 2630 | } sg_gl_swapchain; |
| 2631 | |
| 2632 | typedef struct sg_swapchain { |
| 2633 | int width; |
| 2634 | int height; |
| 2635 | int sample_count; |
| 2636 | sg_pixel_format color_format; |
| 2637 | sg_pixel_format depth_format; |
| 2638 | sg_metal_swapchain metal; |
| 2639 | sg_d3d11_swapchain d3d11; |
| 2640 | sg_wgpu_swapchain wgpu; |
| 2641 | sg_gl_swapchain gl; |
| 2642 | } sg_swapchain; |
| 2643 | |
| 2644 | /* |
| 2645 | sg_pass |
| 2646 | |
| 2647 | The sg_pass structure is passed as argument into the sg_begin_pass() |
| 2648 | function. |
| 2649 | |
| 2650 | For an offscreen rendering pass, an sg_pass_action struct and sg_attachments |
| 2651 | object must be provided, and for swapchain passes, and sg_pass_action and |
| 2652 | an sg_swapchain struct. It is an error to provide both an sg_attachments |
| 2653 | handle and an initialized sg_swapchain struct in the same sg_begin_pass(). |
| 2654 | |
| 2655 | An sg_begin_pass() call for an offscreen pass would look like this (where |
| 2656 | `attachments` is an sg_attachments handle): |
| 2657 | |
| 2658 | sg_begin_pass(&(sg_pass){ |
| 2659 | .action = { ... }, |
| 2660 | .attachments = attachments, |
| 2661 | }); |
| 2662 | |
| 2663 | ...and a swapchain render pass would look like this (using the sokol_glue.h |
| 2664 | helper function sglue_swapchain() which gets the swapchain properties from |
| 2665 | sokol_app.h): |
| 2666 | |
| 2667 | sg_begin_pass(&(sg_pass){ |
| 2668 | .action = { ... }, |
| 2669 | .swapchain = sglue_swapchain(), |
| 2670 | }); |
| 2671 | |
| 2672 | You can also omit the .action object to get default pass action behaviour |
| 2673 | (clear to color=grey, depth=1 and stencil=0). |
| 2674 | */ |
| 2675 | typedef struct sg_pass { |
| 2676 | uint32_t _start_canary; |
| 2677 | sg_pass_action action; |
| 2678 | sg_attachments attachments; |
| 2679 | sg_swapchain swapchain; |
| 2680 | const char* label; |
| 2681 | uint32_t _end_canary; |
| 2682 | } sg_pass; |
| 2683 | |
| 2684 | /* |
| 2685 | sg_bindings |
| 2686 | |
| 2687 | The sg_bindings structure defines the resource binding slots |
| 2688 | of the sokol_gfx render pipeline, used as argument to the |
| 2689 | sg_apply_bindings() function. |
| 2690 | |
| 2691 | A resource binding struct contains: |
| 2692 | |
| 2693 | - 1..N vertex buffers |
| 2694 | - 0..N vertex buffer offsets |
| 2695 | - 0..1 index buffers |
| 2696 | - 0..1 index buffer offsets |
| 2697 | - 0..N vertex shader stage images |
| 2698 | - 0..N vertex shader stage samplers |
| 2699 | - 0..N vertex shader storage buffers |
| 2700 | - 0..N fragment shader stage images |
| 2701 | - 0..N fragment shader stage samplers |
| 2702 | - 0..N fragment shader storage buffers |
| 2703 | |
| 2704 | For the max number of bindings, see the constant definitions: |
| 2705 | |
| 2706 | - SG_MAX_VERTEX_BUFFERS |
| 2707 | - SG_MAX_SHADERSTAGE_IMAGES |
| 2708 | - SG_MAX_SHADERSTAGE_SAMPLERS |
| 2709 | - SG_MAX_SHADERSTAGE_STORAGEBUFFERS |
| 2710 | |
| 2711 | The optional buffer offsets can be used to put different unrelated |
| 2712 | chunks of vertex- and/or index-data into the same buffer objects. |
| 2713 | */ |
| 2714 | typedef struct sg_stage_bindings { |
| 2715 | sg_image images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 2716 | sg_sampler samplers[SG_MAX_SHADERSTAGE_SAMPLERS]; |
| 2717 | sg_buffer storage_buffers[SG_MAX_SHADERSTAGE_STORAGEBUFFERS]; |
| 2718 | } sg_stage_bindings; |
| 2719 | |
| 2720 | typedef struct sg_bindings { |
| 2721 | uint32_t _start_canary; |
| 2722 | sg_buffer vertex_buffers[SG_MAX_VERTEX_BUFFERS]; |
| 2723 | int vertex_buffer_offsets[SG_MAX_VERTEX_BUFFERS]; |
| 2724 | sg_buffer index_buffer; |
| 2725 | int index_buffer_offset; |
| 2726 | sg_stage_bindings vs; |
| 2727 | sg_stage_bindings fs; |
| 2728 | uint32_t _end_canary; |
| 2729 | } sg_bindings; |
| 2730 | |
| 2731 | /* |
| 2732 | sg_buffer_desc |
| 2733 | |
| 2734 | Creation parameters for sg_buffer objects, used in the |
| 2735 | sg_make_buffer() call. |
| 2736 | |
| 2737 | The default configuration is: |
| 2738 | |
| 2739 | .size: 0 (*must* be >0 for buffers without data) |
| 2740 | .type: SG_BUFFERTYPE_VERTEXBUFFER |
| 2741 | .usage: SG_USAGE_IMMUTABLE |
| 2742 | .data.ptr 0 (*must* be valid for immutable buffers) |
| 2743 | .data.size 0 (*must* be > 0 for immutable buffers) |
| 2744 | .label 0 (optional string label) |
| 2745 | |
| 2746 | For immutable buffers which are initialized with initial data, |
| 2747 | keep the .size item zero-initialized, and set the size together with the |
| 2748 | pointer to the initial data in the .data item. |
| 2749 | |
| 2750 | For mutable buffers without initial data, keep the .data item |
| 2751 | zero-initialized, and set the buffer size in the .size item instead. |
| 2752 | |
| 2753 | You can also set both size values, but currently both size values must |
| 2754 | be identical (this may change in the future when the dynamic resource |
| 2755 | management may become more flexible). |
| 2756 | |
| 2757 | ADVANCED TOPIC: Injecting native 3D-API buffers: |
| 2758 | |
| 2759 | The following struct members allow to inject your own GL, Metal |
| 2760 | or D3D11 buffers into sokol_gfx: |
| 2761 | |
| 2762 | .gl_buffers[SG_NUM_INFLIGHT_FRAMES] |
| 2763 | .mtl_buffers[SG_NUM_INFLIGHT_FRAMES] |
| 2764 | .d3d11_buffer |
| 2765 | |
| 2766 | You must still provide all other struct items except the .data item, and |
| 2767 | these must match the creation parameters of the native buffers you |
| 2768 | provide. For SG_USAGE_IMMUTABLE, only provide a single native 3D-API |
| 2769 | buffer, otherwise you need to provide SG_NUM_INFLIGHT_FRAMES buffers |
| 2770 | (only for GL and Metal, not D3D11). Providing multiple buffers for GL and |
| 2771 | Metal is necessary because sokol_gfx will rotate through them when |
| 2772 | calling sg_update_buffer() to prevent lock-stalls. |
| 2773 | |
| 2774 | Note that it is expected that immutable injected buffer have already been |
| 2775 | initialized with content, and the .content member must be 0! |
| 2776 | |
| 2777 | Also you need to call sg_reset_state_cache() after calling native 3D-API |
| 2778 | functions, and before calling any sokol_gfx function. |
| 2779 | */ |
| 2780 | typedef struct sg_buffer_desc { |
| 2781 | uint32_t _start_canary; |
| 2782 | size_t size; |
| 2783 | sg_buffer_type type; |
| 2784 | sg_usage usage; |
| 2785 | sg_range data; |
| 2786 | const char* label; |
| 2787 | // optionally inject backend-specific resources |
| 2788 | uint32_t gl_buffers[SG_NUM_INFLIGHT_FRAMES]; |
| 2789 | const void* mtl_buffers[SG_NUM_INFLIGHT_FRAMES]; |
| 2790 | const void* d3d11_buffer; |
| 2791 | const void* wgpu_buffer; |
| 2792 | uint32_t _end_canary; |
| 2793 | } sg_buffer_desc; |
| 2794 | |
| 2795 | /* |
| 2796 | sg_image_data |
| 2797 | |
| 2798 | Defines the content of an image through a 2D array of sg_range structs. |
| 2799 | The first array dimension is the cubemap face, and the second array |
| 2800 | dimension the mipmap level. |
| 2801 | */ |
| 2802 | typedef struct sg_image_data { |
| 2803 | sg_range subimage[SG_CUBEFACE_NUM][SG_MAX_MIPMAPS]; |
| 2804 | } sg_image_data; |
| 2805 | |
| 2806 | /* |
| 2807 | sg_image_desc |
| 2808 | |
| 2809 | Creation parameters for sg_image objects, used in the sg_make_image() call. |
| 2810 | |
| 2811 | The default configuration is: |
| 2812 | |
| 2813 | .type: SG_IMAGETYPE_2D |
| 2814 | .render_target: false |
| 2815 | .width 0 (must be set to >0) |
| 2816 | .height 0 (must be set to >0) |
| 2817 | .num_slices 1 (3D textures: depth; array textures: number of layers) |
| 2818 | .num_mipmaps: 1 |
| 2819 | .usage: SG_USAGE_IMMUTABLE |
| 2820 | .pixel_format: SG_PIXELFORMAT_RGBA8 for textures, or sg_desc.environment.defaults.color_format for render targets |
| 2821 | .sample_count: 1 for textures, or sg_desc.environment.defaults.sample_count for render targets |
| 2822 | .data an sg_image_data struct to define the initial content |
| 2823 | .label 0 (optional string label for trace hooks) |
| 2824 | |
| 2825 | Q: Why is the default sample_count for render targets identical with the |
| 2826 | "default sample count" from sg_desc.environment.defaults.sample_count? |
| 2827 | |
| 2828 | A: So that it matches the default sample count in pipeline objects. Even |
| 2829 | though it is a bit strange/confusing that offscreen render targets by default |
| 2830 | get the same sample count as 'default swapchains', but it's better that |
| 2831 | an offscreen render target created with default parameters matches |
| 2832 | a pipeline object created with default parameters. |
| 2833 | |
| 2834 | NOTE: |
| 2835 | |
| 2836 | Images with usage SG_USAGE_IMMUTABLE must be fully initialized by |
| 2837 | providing a valid .data member which points to initialization data. |
| 2838 | |
| 2839 | ADVANCED TOPIC: Injecting native 3D-API textures: |
| 2840 | |
| 2841 | The following struct members allow to inject your own GL, Metal or D3D11 |
| 2842 | textures into sokol_gfx: |
| 2843 | |
| 2844 | .gl_textures[SG_NUM_INFLIGHT_FRAMES] |
| 2845 | .mtl_textures[SG_NUM_INFLIGHT_FRAMES] |
| 2846 | .d3d11_texture |
| 2847 | .d3d11_shader_resource_view |
| 2848 | .wgpu_texture |
| 2849 | .wgpu_texture_view |
| 2850 | |
| 2851 | For GL, you can also specify the texture target or leave it empty to use |
| 2852 | the default texture target for the image type (GL_TEXTURE_2D for |
| 2853 | SG_IMAGETYPE_2D etc) |
| 2854 | |
| 2855 | For D3D11 and WebGPU, either only provide a texture, or both a texture and |
| 2856 | shader-resource-view / texture-view object. If you want to use access the |
| 2857 | injected texture in a shader you *must* provide a shader-resource-view. |
| 2858 | |
| 2859 | The same rules apply as for injecting native buffers (see sg_buffer_desc |
| 2860 | documentation for more details). |
| 2861 | */ |
| 2862 | typedef struct sg_image_desc { |
| 2863 | uint32_t _start_canary; |
| 2864 | sg_image_type type; |
| 2865 | bool render_target; |
| 2866 | int width; |
| 2867 | int height; |
| 2868 | int num_slices; |
| 2869 | int num_mipmaps; |
| 2870 | sg_usage usage; |
| 2871 | sg_pixel_format pixel_format; |
| 2872 | int sample_count; |
| 2873 | sg_image_data data; |
| 2874 | const char* label; |
| 2875 | // optionally inject backend-specific resources |
| 2876 | uint32_t gl_textures[SG_NUM_INFLIGHT_FRAMES]; |
| 2877 | uint32_t gl_texture_target; |
| 2878 | const void* mtl_textures[SG_NUM_INFLIGHT_FRAMES]; |
| 2879 | const void* d3d11_texture; |
| 2880 | const void* d3d11_shader_resource_view; |
| 2881 | const void* wgpu_texture; |
| 2882 | const void* wgpu_texture_view; |
| 2883 | uint32_t _end_canary; |
| 2884 | } sg_image_desc; |
| 2885 | |
| 2886 | /* |
| 2887 | sg_sampler_desc |
| 2888 | |
| 2889 | Creation parameters for sg_sampler objects, used in the sg_make_sampler() call |
| 2890 | |
| 2891 | .min_filter: SG_FILTER_NEAREST |
| 2892 | .mag_filter: SG_FILTER_NEAREST |
| 2893 | .mipmap_filter SG_FILTER_NONE |
| 2894 | .wrap_u: SG_WRAP_REPEAT |
| 2895 | .wrap_v: SG_WRAP_REPEAT |
| 2896 | .wrap_w: SG_WRAP_REPEAT (only SG_IMAGETYPE_3D) |
| 2897 | .min_lod 0.0f |
| 2898 | .max_lod FLT_MAX |
| 2899 | .border_color SG_BORDERCOLOR_OPAQUE_BLACK |
| 2900 | .compare SG_COMPAREFUNC_NEVER |
| 2901 | .max_anisotropy 1 (must be 1..16) |
| 2902 | |
| 2903 | */ |
| 2904 | typedef struct sg_sampler_desc { |
| 2905 | uint32_t _start_canary; |
| 2906 | sg_filter min_filter; |
| 2907 | sg_filter mag_filter; |
| 2908 | sg_filter mipmap_filter; |
| 2909 | sg_wrap wrap_u; |
| 2910 | sg_wrap wrap_v; |
| 2911 | sg_wrap wrap_w; |
| 2912 | float min_lod; |
| 2913 | float max_lod; |
| 2914 | sg_border_color border_color; |
| 2915 | sg_compare_func compare; |
| 2916 | uint32_t max_anisotropy; |
| 2917 | const char* label; |
| 2918 | // optionally inject backend-specific resources |
| 2919 | uint32_t gl_sampler; |
| 2920 | const void* mtl_sampler; |
| 2921 | const void* d3d11_sampler; |
| 2922 | const void* wgpu_sampler; |
| 2923 | uint32_t _end_canary; |
| 2924 | } sg_sampler_desc; |
| 2925 | |
| 2926 | /* |
| 2927 | sg_shader_desc |
| 2928 | |
| 2929 | The structure sg_shader_desc defines all creation parameters for shader |
| 2930 | programs, used as input to the sg_make_shader() function: |
| 2931 | |
| 2932 | - reflection information for vertex attributes (vertex shader inputs): |
| 2933 | - vertex attribute name (only optionally used by GLES3 and GL) |
| 2934 | - a semantic name and index (required for D3D11) |
| 2935 | - for each shader-stage (vertex and fragment): |
| 2936 | - the shader source or bytecode |
| 2937 | - an optional entry function name |
| 2938 | - an optional compile target (only for D3D11 when source is provided, |
| 2939 | defaults are "vs_4_0" and "ps_4_0") |
| 2940 | - reflection info for each uniform block used by the shader stage: |
| 2941 | - the size of the uniform block in bytes |
| 2942 | - a memory layout hint (native vs std140, only required for GL backends) |
| 2943 | - reflection info for each uniform block member (only required for GL backends): |
| 2944 | - member name |
| 2945 | - member type (SG_UNIFORMTYPE_xxx) |
| 2946 | - if the member is an array, the number of array items |
| 2947 | - reflection info for textures used in the shader stage: |
| 2948 | - the image type (SG_IMAGETYPE_xxx) |
| 2949 | - the image-sample type (SG_IMAGESAMPLETYPE_xxx, default is SG_IMAGESAMPLETYPE_FLOAT) |
| 2950 | - whether the shader expects a multisampled texture |
| 2951 | - reflection info for samplers used in the shader stage: |
| 2952 | - the sampler type (SG_SAMPLERTYPE_xxx) |
| 2953 | - reflection info for each image-sampler-pair used by the shader: |
| 2954 | - the texture slot of the involved texture |
| 2955 | - the sampler slot of the involved sampler |
| 2956 | - for GLSL only: the name of the combined image-sampler object |
| 2957 | - reflection info for each storage-buffer used by the shader: |
| 2958 | - whether the storage buffer is readonly (currently this |
| 2959 | must be true) |
| 2960 | |
| 2961 | For all GL backends, shader source-code must be provided. For D3D11 and Metal, |
| 2962 | either shader source-code or byte-code can be provided. |
| 2963 | |
| 2964 | For D3D11, if source code is provided, the d3dcompiler_47.dll will be loaded |
| 2965 | on demand. If this fails, shader creation will fail. When compiling HLSL |
| 2966 | source code, you can provide an optional target string via |
| 2967 | sg_shader_stage_desc.d3d11_target, the default target is "vs_4_0" for the |
| 2968 | vertex shader stage and "ps_4_0" for the pixel shader stage. |
| 2969 | */ |
| 2970 | typedef struct sg_shader_attr_desc { |
| 2971 | const char* name; // GLSL vertex attribute name (optional) |
| 2972 | const char* sem_name; // HLSL semantic name |
| 2973 | int sem_index; // HLSL semantic index |
| 2974 | } sg_shader_attr_desc; |
| 2975 | |
| 2976 | typedef struct sg_shader_uniform_desc { |
| 2977 | const char* name; |
| 2978 | sg_uniform_type type; |
| 2979 | int array_count; |
| 2980 | } sg_shader_uniform_desc; |
| 2981 | |
| 2982 | typedef struct sg_shader_uniform_block_desc { |
| 2983 | size_t size; |
| 2984 | sg_uniform_layout layout; |
| 2985 | sg_shader_uniform_desc uniforms[SG_MAX_UB_MEMBERS]; |
| 2986 | } sg_shader_uniform_block_desc; |
| 2987 | |
| 2988 | typedef struct sg_shader_storage_buffer_desc { |
| 2989 | bool used; |
| 2990 | bool readonly; |
| 2991 | } sg_shader_storage_buffer_desc; |
| 2992 | |
| 2993 | typedef struct sg_shader_image_desc { |
| 2994 | bool used; |
| 2995 | bool multisampled; |
| 2996 | sg_image_type image_type; |
| 2997 | sg_image_sample_type sample_type; |
| 2998 | } sg_shader_image_desc; |
| 2999 | |
| 3000 | typedef struct sg_shader_sampler_desc { |
| 3001 | bool used; |
| 3002 | sg_sampler_type sampler_type; |
| 3003 | } sg_shader_sampler_desc; |
| 3004 | |
| 3005 | typedef struct sg_shader_image_sampler_pair_desc { |
| 3006 | bool used; |
| 3007 | int image_slot; |
| 3008 | int sampler_slot; |
| 3009 | const char* glsl_name; |
| 3010 | } sg_shader_image_sampler_pair_desc; |
| 3011 | |
| 3012 | typedef struct sg_shader_stage_desc { |
| 3013 | const char* source; |
| 3014 | sg_range bytecode; |
| 3015 | const char* entry; |
| 3016 | const char* d3d11_target; |
| 3017 | sg_shader_uniform_block_desc uniform_blocks[SG_MAX_SHADERSTAGE_UBS]; |
| 3018 | sg_shader_storage_buffer_desc storage_buffers[SG_MAX_SHADERSTAGE_STORAGEBUFFERS]; |
| 3019 | sg_shader_image_desc images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 3020 | sg_shader_sampler_desc samplers[SG_MAX_SHADERSTAGE_SAMPLERS]; |
| 3021 | sg_shader_image_sampler_pair_desc image_sampler_pairs[SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS]; |
| 3022 | } sg_shader_stage_desc; |
| 3023 | |
| 3024 | typedef struct sg_shader_desc { |
| 3025 | uint32_t _start_canary; |
| 3026 | sg_shader_attr_desc attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 3027 | sg_shader_stage_desc vs; |
| 3028 | sg_shader_stage_desc fs; |
| 3029 | const char* label; |
| 3030 | uint32_t _end_canary; |
| 3031 | } sg_shader_desc; |
| 3032 | |
| 3033 | /* |
| 3034 | sg_pipeline_desc |
| 3035 | |
| 3036 | The sg_pipeline_desc struct defines all creation parameters for an |
| 3037 | sg_pipeline object, used as argument to the sg_make_pipeline() function: |
| 3038 | |
| 3039 | - the vertex layout for all input vertex buffers |
| 3040 | - a shader object |
| 3041 | - the 3D primitive type (points, lines, triangles, ...) |
| 3042 | - the index type (none, 16- or 32-bit) |
| 3043 | - all the fixed-function-pipeline state (depth-, stencil-, blend-state, etc...) |
| 3044 | |
| 3045 | If the vertex data has no gaps between vertex components, you can omit |
| 3046 | the .layout.buffers[].stride and layout.attrs[].offset items (leave them |
| 3047 | default-initialized to 0), sokol-gfx will then compute the offsets and |
| 3048 | strides from the vertex component formats (.layout.attrs[].format). |
| 3049 | Please note that ALL vertex attribute offsets must be 0 in order for the |
| 3050 | automatic offset computation to kick in. |
| 3051 | |
| 3052 | The default configuration is as follows: |
| 3053 | |
| 3054 | .shader: 0 (must be initialized with a valid sg_shader id!) |
| 3055 | .layout: |
| 3056 | .buffers[]: vertex buffer layouts |
| 3057 | .stride: 0 (if no stride is given it will be computed) |
| 3058 | .step_func SG_VERTEXSTEP_PER_VERTEX |
| 3059 | .step_rate 1 |
| 3060 | .attrs[]: vertex attribute declarations |
| 3061 | .buffer_index 0 the vertex buffer bind slot |
| 3062 | .offset 0 (offsets can be omitted if the vertex layout has no gaps) |
| 3063 | .format SG_VERTEXFORMAT_INVALID (must be initialized!) |
| 3064 | .depth: |
| 3065 | .pixel_format: sg_desc.context.depth_format |
| 3066 | .compare: SG_COMPAREFUNC_ALWAYS |
| 3067 | .write_enabled: false |
| 3068 | .bias: 0.0f |
| 3069 | .bias_slope_scale: 0.0f |
| 3070 | .bias_clamp: 0.0f |
| 3071 | .stencil: |
| 3072 | .enabled: false |
| 3073 | .front/back: |
| 3074 | .compare: SG_COMPAREFUNC_ALWAYS |
| 3075 | .fail_op: SG_STENCILOP_KEEP |
| 3076 | .depth_fail_op: SG_STENCILOP_KEEP |
| 3077 | .pass_op: SG_STENCILOP_KEEP |
| 3078 | .read_mask: 0 |
| 3079 | .write_mask: 0 |
| 3080 | .ref: 0 |
| 3081 | .color_count 1 |
| 3082 | .colors[0..color_count] |
| 3083 | .pixel_format sg_desc.context.color_format |
| 3084 | .write_mask: SG_COLORMASK_RGBA |
| 3085 | .blend: |
| 3086 | .enabled: false |
| 3087 | .src_factor_rgb: SG_BLENDFACTOR_ONE |
| 3088 | .dst_factor_rgb: SG_BLENDFACTOR_ZERO |
| 3089 | .op_rgb: SG_BLENDOP_ADD |
| 3090 | .src_factor_alpha: SG_BLENDFACTOR_ONE |
| 3091 | .dst_factor_alpha: SG_BLENDFACTOR_ZERO |
| 3092 | .op_alpha: SG_BLENDOP_ADD |
| 3093 | .primitive_type: SG_PRIMITIVETYPE_TRIANGLES |
| 3094 | .index_type: SG_INDEXTYPE_NONE |
| 3095 | .cull_mode: SG_CULLMODE_NONE |
| 3096 | .face_winding: SG_FACEWINDING_CW |
| 3097 | .sample_count: sg_desc.context.sample_count |
| 3098 | .blend_color: (sg_color) { 0.0f, 0.0f, 0.0f, 0.0f } |
| 3099 | .alpha_to_coverage_enabled: false |
| 3100 | .label 0 (optional string label for trace hooks) |
| 3101 | */ |
| 3102 | typedef struct sg_vertex_buffer_layout_state { |
| 3103 | int stride; |
| 3104 | sg_vertex_step step_func; |
| 3105 | int step_rate; |
| 3106 | } sg_vertex_buffer_layout_state; |
| 3107 | |
| 3108 | typedef struct sg_vertex_attr_state { |
| 3109 | int buffer_index; |
| 3110 | int offset; |
| 3111 | sg_vertex_format format; |
| 3112 | } sg_vertex_attr_state; |
| 3113 | |
| 3114 | typedef struct sg_vertex_layout_state { |
| 3115 | sg_vertex_buffer_layout_state buffers[SG_MAX_VERTEX_BUFFERS]; |
| 3116 | sg_vertex_attr_state attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 3117 | } sg_vertex_layout_state; |
| 3118 | |
| 3119 | typedef struct sg_stencil_face_state { |
| 3120 | sg_compare_func compare; |
| 3121 | sg_stencil_op fail_op; |
| 3122 | sg_stencil_op depth_fail_op; |
| 3123 | sg_stencil_op pass_op; |
| 3124 | } sg_stencil_face_state; |
| 3125 | |
| 3126 | typedef struct sg_stencil_state { |
| 3127 | bool enabled; |
| 3128 | sg_stencil_face_state front; |
| 3129 | sg_stencil_face_state back; |
| 3130 | uint8_t read_mask; |
| 3131 | uint8_t write_mask; |
| 3132 | uint8_t ref; |
| 3133 | } sg_stencil_state; |
| 3134 | |
| 3135 | typedef struct sg_depth_state { |
| 3136 | sg_pixel_format pixel_format; |
| 3137 | sg_compare_func compare; |
| 3138 | bool write_enabled; |
| 3139 | float bias; |
| 3140 | float bias_slope_scale; |
| 3141 | float bias_clamp; |
| 3142 | } sg_depth_state; |
| 3143 | |
| 3144 | typedef struct sg_blend_state { |
| 3145 | bool enabled; |
| 3146 | sg_blend_factor src_factor_rgb; |
| 3147 | sg_blend_factor dst_factor_rgb; |
| 3148 | sg_blend_op op_rgb; |
| 3149 | sg_blend_factor src_factor_alpha; |
| 3150 | sg_blend_factor dst_factor_alpha; |
| 3151 | sg_blend_op op_alpha; |
| 3152 | } sg_blend_state; |
| 3153 | |
| 3154 | typedef struct sg_color_target_state { |
| 3155 | sg_pixel_format pixel_format; |
| 3156 | sg_color_mask write_mask; |
| 3157 | sg_blend_state blend; |
| 3158 | } sg_color_target_state; |
| 3159 | |
| 3160 | typedef struct sg_pipeline_desc { |
| 3161 | uint32_t _start_canary; |
| 3162 | sg_shader shader; |
| 3163 | sg_vertex_layout_state layout; |
| 3164 | sg_depth_state depth; |
| 3165 | sg_stencil_state stencil; |
| 3166 | int color_count; |
| 3167 | sg_color_target_state colors[SG_MAX_COLOR_ATTACHMENTS]; |
| 3168 | sg_primitive_type primitive_type; |
| 3169 | sg_index_type index_type; |
| 3170 | sg_cull_mode cull_mode; |
| 3171 | sg_face_winding face_winding; |
| 3172 | int sample_count; |
| 3173 | sg_color blend_color; |
| 3174 | bool alpha_to_coverage_enabled; |
| 3175 | const char* label; |
| 3176 | uint32_t _end_canary; |
| 3177 | } sg_pipeline_desc; |
| 3178 | |
| 3179 | /* |
| 3180 | sg_attachments_desc |
| 3181 | |
| 3182 | Creation parameters for an sg_attachments object, used as argument to the |
| 3183 | sg_make_attachments() function. |
| 3184 | |
| 3185 | An attachments object bundles 0..4 color attachments, 0..4 msaa-resolve |
| 3186 | attachments, and none or one depth-stencil attachmente for use |
| 3187 | in a render pass. At least one color attachment or one depth-stencil |
| 3188 | attachment must be provided (no color attachment and a depth-stencil |
| 3189 | attachment is useful for a depth-only render pass). |
| 3190 | |
| 3191 | Each attachment definition consists of an image object, and two additional indices |
| 3192 | describing which subimage the pass will render into: one mipmap index, and if the image |
| 3193 | is a cubemap, array-texture or 3D-texture, the face-index, array-layer or |
| 3194 | depth-slice. |
| 3195 | |
| 3196 | All attachments must have the same width and height. |
| 3197 | |
| 3198 | All color attachments and the depth-stencil attachment must have the |
| 3199 | same sample count. |
| 3200 | |
| 3201 | If a resolve attachment is set, an MSAA-resolve operation from the |
| 3202 | associated color attachment image into the resolve attachment image will take |
| 3203 | place in the sg_end_pass() function. In this case, the color attachment |
| 3204 | must have a (sample_count>1), and the resolve attachment a |
| 3205 | (sample_count==1). The resolve attachment also must have the same pixel |
| 3206 | format as the color attachment. |
| 3207 | |
| 3208 | NOTE that MSAA depth-stencil attachments cannot be msaa-resolved! |
| 3209 | */ |
| 3210 | typedef struct sg_attachment_desc { |
| 3211 | sg_image image; |
| 3212 | int mip_level; |
| 3213 | int slice; // cube texture: face; array texture: layer; 3D texture: slice |
| 3214 | } sg_attachment_desc; |
| 3215 | |
| 3216 | typedef struct sg_attachments_desc { |
| 3217 | uint32_t _start_canary; |
| 3218 | sg_attachment_desc colors[SG_MAX_COLOR_ATTACHMENTS]; |
| 3219 | sg_attachment_desc resolves[SG_MAX_COLOR_ATTACHMENTS]; |
| 3220 | sg_attachment_desc depth_stencil; |
| 3221 | const char* label; |
| 3222 | uint32_t _end_canary; |
| 3223 | } sg_attachments_desc; |
| 3224 | |
| 3225 | /* |
| 3226 | sg_trace_hooks |
| 3227 | |
| 3228 | Installable callback functions to keep track of the sokol-gfx calls, |
| 3229 | this is useful for debugging, or keeping track of resource creation |
| 3230 | and destruction. |
| 3231 | |
| 3232 | Trace hooks are installed with sg_install_trace_hooks(), this returns |
| 3233 | another sg_trace_hooks struct with the previous set of |
| 3234 | trace hook function pointers. These should be invoked by the |
| 3235 | new trace hooks to form a proper call chain. |
| 3236 | */ |
| 3237 | typedef struct sg_trace_hooks { |
| 3238 | void* user_data; |
| 3239 | void (*reset_state_cache)(void* user_data); |
| 3240 | void (*make_buffer)(const sg_buffer_desc* desc, sg_buffer result, void* user_data); |
| 3241 | void (*make_image)(const sg_image_desc* desc, sg_image result, void* user_data); |
| 3242 | void (*make_sampler)(const sg_sampler_desc* desc, sg_sampler result, void* user_data); |
| 3243 | void (*make_shader)(const sg_shader_desc* desc, sg_shader result, void* user_data); |
| 3244 | void (*make_pipeline)(const sg_pipeline_desc* desc, sg_pipeline result, void* user_data); |
| 3245 | void (*make_attachments)(const sg_attachments_desc* desc, sg_attachments result, void* user_data); |
| 3246 | void (*destroy_buffer)(sg_buffer buf, void* user_data); |
| 3247 | void (*destroy_image)(sg_image img, void* user_data); |
| 3248 | void (*destroy_sampler)(sg_sampler smp, void* user_data); |
| 3249 | void (*destroy_shader)(sg_shader shd, void* user_data); |
| 3250 | void (*destroy_pipeline)(sg_pipeline pip, void* user_data); |
| 3251 | void (*destroy_attachments)(sg_attachments atts, void* user_data); |
| 3252 | void (*update_buffer)(sg_buffer buf, const sg_range* data, void* user_data); |
| 3253 | void (*update_image)(sg_image img, const sg_image_data* data, void* user_data); |
| 3254 | void (*append_buffer)(sg_buffer buf, const sg_range* data, int result, void* user_data); |
| 3255 | void (*begin_pass)(const sg_pass* pass, void* user_data); |
| 3256 | void (*apply_viewport)(int x, int y, int width, int height, bool origin_top_left, void* user_data); |
| 3257 | void (*apply_scissor_rect)(int x, int y, int width, int height, bool origin_top_left, void* user_data); |
| 3258 | void (*apply_pipeline)(sg_pipeline pip, void* user_data); |
| 3259 | void (*apply_bindings)(const sg_bindings* bindings, void* user_data); |
| 3260 | void (*apply_uniforms)(sg_shader_stage stage, int ub_index, const sg_range* data, void* user_data); |
| 3261 | void (*draw)(int base_element, int num_elements, int num_instances, void* user_data); |
| 3262 | void (*end_pass)(void* user_data); |
| 3263 | void (*commit)(void* user_data); |
| 3264 | void (*alloc_buffer)(sg_buffer result, void* user_data); |
| 3265 | void (*alloc_image)(sg_image result, void* user_data); |
| 3266 | void (*alloc_sampler)(sg_sampler result, void* user_data); |
| 3267 | void (*alloc_shader)(sg_shader result, void* user_data); |
| 3268 | void (*alloc_pipeline)(sg_pipeline result, void* user_data); |
| 3269 | void (*alloc_attachments)(sg_attachments result, void* user_data); |
| 3270 | void (*dealloc_buffer)(sg_buffer buf_id, void* user_data); |
| 3271 | void (*dealloc_image)(sg_image img_id, void* user_data); |
| 3272 | void (*dealloc_sampler)(sg_sampler smp_id, void* user_data); |
| 3273 | void (*dealloc_shader)(sg_shader shd_id, void* user_data); |
| 3274 | void (*dealloc_pipeline)(sg_pipeline pip_id, void* user_data); |
| 3275 | void (*dealloc_attachments)(sg_attachments atts_id, void* user_data); |
| 3276 | void (*init_buffer)(sg_buffer buf_id, const sg_buffer_desc* desc, void* user_data); |
| 3277 | void (*init_image)(sg_image img_id, const sg_image_desc* desc, void* user_data); |
| 3278 | void (*init_sampler)(sg_sampler smp_id, const sg_sampler_desc* desc, void* user_data); |
| 3279 | void (*init_shader)(sg_shader shd_id, const sg_shader_desc* desc, void* user_data); |
| 3280 | void (*init_pipeline)(sg_pipeline pip_id, const sg_pipeline_desc* desc, void* user_data); |
| 3281 | void (*init_attachments)(sg_attachments atts_id, const sg_attachments_desc* desc, void* user_data); |
| 3282 | void (*uninit_buffer)(sg_buffer buf_id, void* user_data); |
| 3283 | void (*uninit_image)(sg_image img_id, void* user_data); |
| 3284 | void (*uninit_sampler)(sg_sampler smp_id, void* user_data); |
| 3285 | void (*uninit_shader)(sg_shader shd_id, void* user_data); |
| 3286 | void (*uninit_pipeline)(sg_pipeline pip_id, void* user_data); |
| 3287 | void (*uninit_attachments)(sg_attachments atts_id, void* user_data); |
| 3288 | void (*fail_buffer)(sg_buffer buf_id, void* user_data); |
| 3289 | void (*fail_image)(sg_image img_id, void* user_data); |
| 3290 | void (*fail_sampler)(sg_sampler smp_id, void* user_data); |
| 3291 | void (*fail_shader)(sg_shader shd_id, void* user_data); |
| 3292 | void (*fail_pipeline)(sg_pipeline pip_id, void* user_data); |
| 3293 | void (*fail_attachments)(sg_attachments atts_id, void* user_data); |
| 3294 | void (*push_debug_group)(const char* name, void* user_data); |
| 3295 | void (*pop_debug_group)(void* user_data); |
| 3296 | } sg_trace_hooks; |
| 3297 | |
| 3298 | /* |
| 3299 | sg_buffer_info |
| 3300 | sg_image_info |
| 3301 | sg_sampler_info |
| 3302 | sg_shader_info |
| 3303 | sg_pipeline_info |
| 3304 | sg_attachments_info |
| 3305 | |
| 3306 | These structs contain various internal resource attributes which |
| 3307 | might be useful for debug-inspection. Please don't rely on the |
| 3308 | actual content of those structs too much, as they are quite closely |
| 3309 | tied to sokol_gfx.h internals and may change more frequently than |
| 3310 | the other public API elements. |
| 3311 | |
| 3312 | The *_info structs are used as the return values of the following functions: |
| 3313 | |
| 3314 | sg_query_buffer_info() |
| 3315 | sg_query_image_info() |
| 3316 | sg_query_sampler_info() |
| 3317 | sg_query_shader_info() |
| 3318 | sg_query_pipeline_info() |
| 3319 | sg_query_pass_info() |
| 3320 | */ |
| 3321 | typedef struct sg_slot_info { |
| 3322 | sg_resource_state state; // the current state of this resource slot |
| 3323 | uint32_t res_id; // type-neutral resource if (e.g. sg_buffer.id) |
| 3324 | } sg_slot_info; |
| 3325 | |
| 3326 | typedef struct sg_buffer_info { |
| 3327 | sg_slot_info slot; // resource pool slot info |
| 3328 | uint32_t update_frame_index; // frame index of last sg_update_buffer() |
| 3329 | uint32_t append_frame_index; // frame index of last sg_append_buffer() |
| 3330 | int append_pos; // current position in buffer for sg_append_buffer() |
| 3331 | bool append_overflow; // is buffer in overflow state (due to sg_append_buffer) |
| 3332 | int num_slots; // number of renaming-slots for dynamically updated buffers |
| 3333 | int active_slot; // currently active write-slot for dynamically updated buffers |
| 3334 | } sg_buffer_info; |
| 3335 | |
| 3336 | typedef struct sg_image_info { |
| 3337 | sg_slot_info slot; // resource pool slot info |
| 3338 | uint32_t upd_frame_index; // frame index of last sg_update_image() |
| 3339 | int num_slots; // number of renaming-slots for dynamically updated images |
| 3340 | int active_slot; // currently active write-slot for dynamically updated images |
| 3341 | } sg_image_info; |
| 3342 | |
| 3343 | typedef struct sg_sampler_info { |
| 3344 | sg_slot_info slot; // resource pool slot info |
| 3345 | } sg_sampler_info; |
| 3346 | |
| 3347 | typedef struct sg_shader_info { |
| 3348 | sg_slot_info slot; // resource pool slot info |
| 3349 | } sg_shader_info; |
| 3350 | |
| 3351 | typedef struct sg_pipeline_info { |
| 3352 | sg_slot_info slot; // resource pool slot info |
| 3353 | } sg_pipeline_info; |
| 3354 | |
| 3355 | typedef struct sg_attachments_info { |
| 3356 | sg_slot_info slot; // resource pool slot info |
| 3357 | } sg_attachments_info; |
| 3358 | |
| 3359 | /* |
| 3360 | sg_frame_stats |
| 3361 | |
| 3362 | Allows to track generic and backend-specific stats about a |
| 3363 | render frame. Obtained by calling sg_query_frame_stats(). The returned |
| 3364 | struct contains information about the *previous* frame. |
| 3365 | */ |
| 3366 | typedef struct sg_frame_stats_gl { |
| 3367 | uint32_t num_bind_buffer; |
| 3368 | uint32_t num_active_texture; |
| 3369 | uint32_t num_bind_texture; |
| 3370 | uint32_t num_bind_sampler; |
| 3371 | uint32_t num_use_program; |
| 3372 | uint32_t num_render_state; |
| 3373 | uint32_t num_vertex_attrib_pointer; |
| 3374 | uint32_t num_vertex_attrib_divisor; |
| 3375 | uint32_t num_enable_vertex_attrib_array; |
| 3376 | uint32_t num_disable_vertex_attrib_array; |
| 3377 | uint32_t num_uniform; |
| 3378 | } sg_frame_stats_gl; |
| 3379 | |
| 3380 | typedef struct sg_frame_stats_d3d11_pass { |
| 3381 | uint32_t num_om_set_render_targets; |
| 3382 | uint32_t num_clear_render_target_view; |
| 3383 | uint32_t num_clear_depth_stencil_view; |
| 3384 | uint32_t num_resolve_subresource; |
| 3385 | } sg_frame_stats_d3d11_pass; |
| 3386 | |
| 3387 | typedef struct sg_frame_stats_d3d11_pipeline { |
| 3388 | uint32_t num_rs_set_state; |
| 3389 | uint32_t num_om_set_depth_stencil_state; |
| 3390 | uint32_t num_om_set_blend_state; |
| 3391 | uint32_t num_ia_set_primitive_topology; |
| 3392 | uint32_t num_ia_set_input_layout; |
| 3393 | uint32_t num_vs_set_shader; |
| 3394 | uint32_t num_vs_set_constant_buffers; |
| 3395 | uint32_t num_ps_set_shader; |
| 3396 | uint32_t num_ps_set_constant_buffers; |
| 3397 | } sg_frame_stats_d3d11_pipeline; |
| 3398 | |
| 3399 | typedef struct sg_frame_stats_d3d11_bindings { |
| 3400 | uint32_t num_ia_set_vertex_buffers; |
| 3401 | uint32_t num_ia_set_index_buffer; |
| 3402 | uint32_t num_vs_set_shader_resources; |
| 3403 | uint32_t num_ps_set_shader_resources; |
| 3404 | uint32_t num_vs_set_samplers; |
| 3405 | uint32_t num_ps_set_samplers; |
| 3406 | } sg_frame_stats_d3d11_bindings; |
| 3407 | |
| 3408 | typedef struct sg_frame_stats_d3d11_uniforms { |
| 3409 | uint32_t num_update_subresource; |
| 3410 | } sg_frame_stats_d3d11_uniforms; |
| 3411 | |
| 3412 | typedef struct sg_frame_stats_d3d11_draw { |
| 3413 | uint32_t num_draw_indexed_instanced; |
| 3414 | uint32_t num_draw_indexed; |
| 3415 | uint32_t num_draw_instanced; |
| 3416 | uint32_t num_draw; |
| 3417 | } sg_frame_stats_d3d11_draw; |
| 3418 | |
| 3419 | typedef struct sg_frame_stats_d3d11 { |
| 3420 | sg_frame_stats_d3d11_pass pass; |
| 3421 | sg_frame_stats_d3d11_pipeline pipeline; |
| 3422 | sg_frame_stats_d3d11_bindings bindings; |
| 3423 | sg_frame_stats_d3d11_uniforms uniforms; |
| 3424 | sg_frame_stats_d3d11_draw draw; |
| 3425 | uint32_t num_map; |
| 3426 | uint32_t num_unmap; |
| 3427 | } sg_frame_stats_d3d11; |
| 3428 | |
| 3429 | typedef struct sg_frame_stats_metal_idpool { |
| 3430 | uint32_t num_added; |
| 3431 | uint32_t num_released; |
| 3432 | uint32_t num_garbage_collected; |
| 3433 | } sg_frame_stats_metal_idpool; |
| 3434 | |
| 3435 | typedef struct sg_frame_stats_metal_pipeline { |
| 3436 | uint32_t num_set_blend_color; |
| 3437 | uint32_t num_set_cull_mode; |
| 3438 | uint32_t num_set_front_facing_winding; |
| 3439 | uint32_t num_set_stencil_reference_value; |
| 3440 | uint32_t num_set_depth_bias; |
| 3441 | uint32_t num_set_render_pipeline_state; |
| 3442 | uint32_t num_set_depth_stencil_state; |
| 3443 | } sg_frame_stats_metal_pipeline; |
| 3444 | |
| 3445 | typedef struct sg_frame_stats_metal_bindings { |
| 3446 | uint32_t num_set_vertex_buffer; |
| 3447 | uint32_t num_set_vertex_texture; |
| 3448 | uint32_t num_set_vertex_sampler_state; |
| 3449 | uint32_t num_set_fragment_buffer; |
| 3450 | uint32_t num_set_fragment_texture; |
| 3451 | uint32_t num_set_fragment_sampler_state; |
| 3452 | } sg_frame_stats_metal_bindings; |
| 3453 | |
| 3454 | typedef struct sg_frame_stats_metal_uniforms { |
| 3455 | uint32_t num_set_vertex_buffer_offset; |
| 3456 | uint32_t num_set_fragment_buffer_offset; |
| 3457 | } sg_frame_stats_metal_uniforms; |
| 3458 | |
| 3459 | typedef struct sg_frame_stats_metal { |
| 3460 | sg_frame_stats_metal_idpool idpool; |
| 3461 | sg_frame_stats_metal_pipeline pipeline; |
| 3462 | sg_frame_stats_metal_bindings bindings; |
| 3463 | sg_frame_stats_metal_uniforms uniforms; |
| 3464 | } sg_frame_stats_metal; |
| 3465 | |
| 3466 | typedef struct sg_frame_stats_wgpu_uniforms { |
| 3467 | uint32_t num_set_bindgroup; |
| 3468 | uint32_t size_write_buffer; |
| 3469 | } sg_frame_stats_wgpu_uniforms; |
| 3470 | |
| 3471 | typedef struct sg_frame_stats_wgpu_bindings { |
| 3472 | uint32_t num_set_vertex_buffer; |
| 3473 | uint32_t num_skip_redundant_vertex_buffer; |
| 3474 | uint32_t num_set_index_buffer; |
| 3475 | uint32_t num_skip_redundant_index_buffer; |
| 3476 | uint32_t num_create_bindgroup; |
| 3477 | uint32_t num_discard_bindgroup; |
| 3478 | uint32_t num_set_bindgroup; |
| 3479 | uint32_t num_skip_redundant_bindgroup; |
| 3480 | uint32_t num_bindgroup_cache_hits; |
| 3481 | uint32_t num_bindgroup_cache_misses; |
| 3482 | uint32_t num_bindgroup_cache_collisions; |
| 3483 | uint32_t num_bindgroup_cache_hash_vs_key_mismatch; |
| 3484 | } sg_frame_stats_wgpu_bindings; |
| 3485 | |
| 3486 | typedef struct sg_frame_stats_wgpu { |
| 3487 | sg_frame_stats_wgpu_uniforms uniforms; |
| 3488 | sg_frame_stats_wgpu_bindings bindings; |
| 3489 | } sg_frame_stats_wgpu; |
| 3490 | |
| 3491 | typedef struct sg_frame_stats { |
| 3492 | uint32_t frame_index; // current frame counter, starts at 0 |
| 3493 | |
| 3494 | uint32_t num_passes; |
| 3495 | uint32_t num_apply_viewport; |
| 3496 | uint32_t num_apply_scissor_rect; |
| 3497 | uint32_t num_apply_pipeline; |
| 3498 | uint32_t num_apply_bindings; |
| 3499 | uint32_t num_apply_uniforms; |
| 3500 | uint32_t num_draw; |
| 3501 | uint32_t num_update_buffer; |
| 3502 | uint32_t num_append_buffer; |
| 3503 | uint32_t num_update_image; |
| 3504 | |
| 3505 | uint32_t size_apply_uniforms; |
| 3506 | uint32_t size_update_buffer; |
| 3507 | uint32_t size_append_buffer; |
| 3508 | uint32_t size_update_image; |
| 3509 | |
| 3510 | sg_frame_stats_gl gl; |
| 3511 | sg_frame_stats_d3d11 d3d11; |
| 3512 | sg_frame_stats_metal metal; |
| 3513 | sg_frame_stats_wgpu wgpu; |
| 3514 | } sg_frame_stats; |
| 3515 | |
| 3516 | /* |
| 3517 | sg_log_item |
| 3518 | |
| 3519 | An enum with a unique item for each log message, warning, error |
| 3520 | and validation layer message. |
| 3521 | */ |
| 3522 | #define _SG_LOG_ITEMS \ |
| 3523 | _SG_LOGITEM_XMACRO(OK, "Ok") \ |
| 3524 | _SG_LOGITEM_XMACRO(MALLOC_FAILED, "memory allocation failed") \ |
| 3525 | _SG_LOGITEM_XMACRO(GL_TEXTURE_FORMAT_NOT_SUPPORTED, "pixel format not supported for texture (gl)") \ |
| 3526 | _SG_LOGITEM_XMACRO(GL_3D_TEXTURES_NOT_SUPPORTED, "3d textures not supported (gl)") \ |
| 3527 | _SG_LOGITEM_XMACRO(GL_ARRAY_TEXTURES_NOT_SUPPORTED, "array textures not supported (gl)") \ |
| 3528 | _SG_LOGITEM_XMACRO(GL_SHADER_COMPILATION_FAILED, "shader compilation failed (gl)") \ |
| 3529 | _SG_LOGITEM_XMACRO(GL_SHADER_LINKING_FAILED, "shader linking failed (gl)") \ |
| 3530 | _SG_LOGITEM_XMACRO(GL_VERTEX_ATTRIBUTE_NOT_FOUND_IN_SHADER, "vertex attribute not found in shader (gl)") \ |
| 3531 | _SG_LOGITEM_XMACRO(GL_TEXTURE_NAME_NOT_FOUND_IN_SHADER, "texture name not found in shader (gl)") \ |
| 3532 | _SG_LOGITEM_XMACRO(GL_FRAMEBUFFER_STATUS_UNDEFINED, "framebuffer completeness check failed with GL_FRAMEBUFFER_UNDEFINED (gl)") \ |
| 3533 | _SG_LOGITEM_XMACRO(GL_FRAMEBUFFER_STATUS_INCOMPLETE_ATTACHMENT, "framebuffer completeness check failed with GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT (gl)") \ |
| 3534 | _SG_LOGITEM_XMACRO(GL_FRAMEBUFFER_STATUS_INCOMPLETE_MISSING_ATTACHMENT, "framebuffer completeness check failed with GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT (gl)") \ |
| 3535 | _SG_LOGITEM_XMACRO(GL_FRAMEBUFFER_STATUS_UNSUPPORTED, "framebuffer completeness check failed with GL_FRAMEBUFFER_UNSUPPORTED (gl)") \ |
| 3536 | _SG_LOGITEM_XMACRO(GL_FRAMEBUFFER_STATUS_INCOMPLETE_MULTISAMPLE, "framebuffer completeness check failed with GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE (gl)") \ |
| 3537 | _SG_LOGITEM_XMACRO(GL_FRAMEBUFFER_STATUS_UNKNOWN, "framebuffer completeness check failed (unknown reason) (gl)") \ |
| 3538 | _SG_LOGITEM_XMACRO(D3D11_CREATE_BUFFER_FAILED, "CreateBuffer() failed (d3d11)") \ |
| 3539 | _SG_LOGITEM_XMACRO(D3D11_CREATE_BUFFER_SRV_FAILED, "CreateShaderResourceView() failed for storage buffer (d3d11)") \ |
| 3540 | _SG_LOGITEM_XMACRO(D3D11_CREATE_DEPTH_TEXTURE_UNSUPPORTED_PIXEL_FORMAT, "pixel format not supported for depth-stencil texture (d3d11)") \ |
| 3541 | _SG_LOGITEM_XMACRO(D3D11_CREATE_DEPTH_TEXTURE_FAILED, "CreateTexture2D() failed for depth-stencil texture (d3d11)") \ |
| 3542 | _SG_LOGITEM_XMACRO(D3D11_CREATE_2D_TEXTURE_UNSUPPORTED_PIXEL_FORMAT, "pixel format not supported for 2d-, cube- or array-texture (d3d11)") \ |
| 3543 | _SG_LOGITEM_XMACRO(D3D11_CREATE_2D_TEXTURE_FAILED, "CreateTexture2D() failed for 2d-, cube- or array-texture (d3d11)") \ |
| 3544 | _SG_LOGITEM_XMACRO(D3D11_CREATE_2D_SRV_FAILED, "CreateShaderResourceView() failed for 2d-, cube- or array-texture (d3d11)") \ |
| 3545 | _SG_LOGITEM_XMACRO(D3D11_CREATE_3D_TEXTURE_UNSUPPORTED_PIXEL_FORMAT, "pixel format not supported for 3D texture (d3d11)") \ |
| 3546 | _SG_LOGITEM_XMACRO(D3D11_CREATE_3D_TEXTURE_FAILED, "CreateTexture3D() failed (d3d11)") \ |
| 3547 | _SG_LOGITEM_XMACRO(D3D11_CREATE_3D_SRV_FAILED, "CreateShaderResourceView() failed for 3d texture (d3d11)") \ |
| 3548 | _SG_LOGITEM_XMACRO(D3D11_CREATE_MSAA_TEXTURE_FAILED, "CreateTexture2D() failed for MSAA render target texture (d3d11)") \ |
| 3549 | _SG_LOGITEM_XMACRO(D3D11_CREATE_SAMPLER_STATE_FAILED, "CreateSamplerState() failed (d3d11)") \ |
| 3550 | _SG_LOGITEM_XMACRO(D3D11_LOAD_D3DCOMPILER_47_DLL_FAILED, "loading d3dcompiler_47.dll failed (d3d11)") \ |
| 3551 | _SG_LOGITEM_XMACRO(D3D11_SHADER_COMPILATION_FAILED, "shader compilation failed (d3d11)") \ |
| 3552 | _SG_LOGITEM_XMACRO(D3D11_SHADER_COMPILATION_OUTPUT, "") \ |
| 3553 | _SG_LOGITEM_XMACRO(D3D11_CREATE_CONSTANT_BUFFER_FAILED, "CreateBuffer() failed for uniform constant buffer (d3d11)") \ |
| 3554 | _SG_LOGITEM_XMACRO(D3D11_CREATE_INPUT_LAYOUT_FAILED, "CreateInputLayout() failed (d3d11)") \ |
| 3555 | _SG_LOGITEM_XMACRO(D3D11_CREATE_RASTERIZER_STATE_FAILED, "CreateRasterizerState() failed (d3d11)") \ |
| 3556 | _SG_LOGITEM_XMACRO(D3D11_CREATE_DEPTH_STENCIL_STATE_FAILED, "CreateDepthStencilState() failed (d3d11)") \ |
| 3557 | _SG_LOGITEM_XMACRO(D3D11_CREATE_BLEND_STATE_FAILED, "CreateBlendState() failed (d3d11)") \ |
| 3558 | _SG_LOGITEM_XMACRO(D3D11_CREATE_RTV_FAILED, "CreateRenderTargetView() failed (d3d11)") \ |
| 3559 | _SG_LOGITEM_XMACRO(D3D11_CREATE_DSV_FAILED, "CreateDepthStencilView() failed (d3d11)") \ |
| 3560 | _SG_LOGITEM_XMACRO(D3D11_MAP_FOR_UPDATE_BUFFER_FAILED, "Map() failed when updating buffer (d3d11)") \ |
| 3561 | _SG_LOGITEM_XMACRO(D3D11_MAP_FOR_APPEND_BUFFER_FAILED, "Map() failed when appending to buffer (d3d11)") \ |
| 3562 | _SG_LOGITEM_XMACRO(D3D11_MAP_FOR_UPDATE_IMAGE_FAILED, "Map() failed when updating image (d3d11)") \ |
| 3563 | _SG_LOGITEM_XMACRO(METAL_CREATE_BUFFER_FAILED, "failed to create buffer object (metal)") \ |
| 3564 | _SG_LOGITEM_XMACRO(METAL_TEXTURE_FORMAT_NOT_SUPPORTED, "pixel format not supported for texture (metal)") \ |
| 3565 | _SG_LOGITEM_XMACRO(METAL_CREATE_TEXTURE_FAILED, "failed to create texture object (metal)") \ |
| 3566 | _SG_LOGITEM_XMACRO(METAL_CREATE_SAMPLER_FAILED, "failed to create sampler object (metal)") \ |
| 3567 | _SG_LOGITEM_XMACRO(METAL_SHADER_COMPILATION_FAILED, "shader compilation failed (metal)") \ |
| 3568 | _SG_LOGITEM_XMACRO(METAL_SHADER_CREATION_FAILED, "shader creation failed (metal)") \ |
| 3569 | _SG_LOGITEM_XMACRO(METAL_SHADER_COMPILATION_OUTPUT, "") \ |
| 3570 | _SG_LOGITEM_XMACRO(METAL_VERTEX_SHADER_ENTRY_NOT_FOUND, "vertex shader entry function not found (metal)") \ |
| 3571 | _SG_LOGITEM_XMACRO(METAL_FRAGMENT_SHADER_ENTRY_NOT_FOUND, "fragment shader entry not found (metal)") \ |
| 3572 | _SG_LOGITEM_XMACRO(METAL_CREATE_RPS_FAILED, "failed to create render pipeline state (metal)") \ |
| 3573 | _SG_LOGITEM_XMACRO(METAL_CREATE_RPS_OUTPUT, "") \ |
| 3574 | _SG_LOGITEM_XMACRO(METAL_CREATE_DSS_FAILED, "failed to create depth stencil state (metal)") \ |
| 3575 | _SG_LOGITEM_XMACRO(WGPU_BINDGROUPS_POOL_EXHAUSTED, "bindgroups pool exhausted (increase sg_desc.bindgroups_cache_size) (wgpu)") \ |
| 3576 | _SG_LOGITEM_XMACRO(WGPU_BINDGROUPSCACHE_SIZE_GREATER_ONE, "sg_desc.wgpu_bindgroups_cache_size must be > 1 (wgpu)") \ |
| 3577 | _SG_LOGITEM_XMACRO(WGPU_BINDGROUPSCACHE_SIZE_POW2, "sg_desc.wgpu_bindgroups_cache_size must be a power of 2 (wgpu)") \ |
| 3578 | _SG_LOGITEM_XMACRO(WGPU_CREATEBINDGROUP_FAILED, "wgpuDeviceCreateBindGroup failed") \ |
| 3579 | _SG_LOGITEM_XMACRO(WGPU_CREATE_BUFFER_FAILED, "wgpuDeviceCreateBuffer() failed") \ |
| 3580 | _SG_LOGITEM_XMACRO(WGPU_CREATE_TEXTURE_FAILED, "wgpuDeviceCreateTexture() failed") \ |
| 3581 | _SG_LOGITEM_XMACRO(WGPU_CREATE_TEXTURE_VIEW_FAILED, "wgpuTextureCreateView() failed") \ |
| 3582 | _SG_LOGITEM_XMACRO(WGPU_CREATE_SAMPLER_FAILED, "wgpuDeviceCreateSampler() failed") \ |
| 3583 | _SG_LOGITEM_XMACRO(WGPU_CREATE_SHADER_MODULE_FAILED, "wgpuDeviceCreateShaderModule() failed") \ |
| 3584 | _SG_LOGITEM_XMACRO(WGPU_SHADER_TOO_MANY_IMAGES, "shader uses too many sampled images on shader stage (wgpu)") \ |
| 3585 | _SG_LOGITEM_XMACRO(WGPU_SHADER_TOO_MANY_SAMPLERS, "shader uses too many samplers on shader stage (wgpu)") \ |
| 3586 | _SG_LOGITEM_XMACRO(WGPU_SHADER_TOO_MANY_STORAGEBUFFERS, "shader uses too many storage buffer bindings on shader stage (wgpu)") \ |
| 3587 | _SG_LOGITEM_XMACRO(WGPU_SHADER_CREATE_BINDGROUP_LAYOUT_FAILED, "wgpuDeviceCreateBindGroupLayout() for shader stage failed") \ |
| 3588 | _SG_LOGITEM_XMACRO(WGPU_CREATE_PIPELINE_LAYOUT_FAILED, "wgpuDeviceCreatePipelineLayout() failed") \ |
| 3589 | _SG_LOGITEM_XMACRO(WGPU_CREATE_RENDER_PIPELINE_FAILED, "wgpuDeviceCreateRenderPipeline() failed") \ |
| 3590 | _SG_LOGITEM_XMACRO(WGPU_ATTACHMENTS_CREATE_TEXTURE_VIEW_FAILED, "wgpuTextureCreateView() failed in create attachments") \ |
| 3591 | _SG_LOGITEM_XMACRO(IDENTICAL_COMMIT_LISTENER, "attempting to add identical commit listener") \ |
| 3592 | _SG_LOGITEM_XMACRO(COMMIT_LISTENER_ARRAY_FULL, "commit listener array full") \ |
| 3593 | _SG_LOGITEM_XMACRO(TRACE_HOOKS_NOT_ENABLED, "sg_install_trace_hooks() called, but SG_TRACE_HOOKS is not defined") \ |
| 3594 | _SG_LOGITEM_XMACRO(DEALLOC_BUFFER_INVALID_STATE, "sg_dealloc_buffer(): buffer must be in ALLOC state") \ |
| 3595 | _SG_LOGITEM_XMACRO(DEALLOC_IMAGE_INVALID_STATE, "sg_dealloc_image(): image must be in alloc state") \ |
| 3596 | _SG_LOGITEM_XMACRO(DEALLOC_SAMPLER_INVALID_STATE, "sg_dealloc_sampler(): sampler must be in alloc state") \ |
| 3597 | _SG_LOGITEM_XMACRO(DEALLOC_SHADER_INVALID_STATE, "sg_dealloc_shader(): shader must be in ALLOC state") \ |
| 3598 | _SG_LOGITEM_XMACRO(DEALLOC_PIPELINE_INVALID_STATE, "sg_dealloc_pipeline(): pipeline must be in ALLOC state") \ |
| 3599 | _SG_LOGITEM_XMACRO(DEALLOC_ATTACHMENTS_INVALID_STATE, "sg_dealloc_attachments(): attachments must be in ALLOC state") \ |
| 3600 | _SG_LOGITEM_XMACRO(INIT_BUFFER_INVALID_STATE, "sg_init_buffer(): buffer must be in ALLOC state") \ |
| 3601 | _SG_LOGITEM_XMACRO(INIT_IMAGE_INVALID_STATE, "sg_init_image(): image must be in ALLOC state") \ |
| 3602 | _SG_LOGITEM_XMACRO(INIT_SAMPLER_INVALID_STATE, "sg_init_sampler(): sampler must be in ALLOC state") \ |
| 3603 | _SG_LOGITEM_XMACRO(INIT_SHADER_INVALID_STATE, "sg_init_shader(): shader must be in ALLOC state") \ |
| 3604 | _SG_LOGITEM_XMACRO(INIT_PIPELINE_INVALID_STATE, "sg_init_pipeline(): pipeline must be in ALLOC state") \ |
| 3605 | _SG_LOGITEM_XMACRO(INIT_ATTACHMENTS_INVALID_STATE, "sg_init_attachments(): pass must be in ALLOC state") \ |
| 3606 | _SG_LOGITEM_XMACRO(UNINIT_BUFFER_INVALID_STATE, "sg_uninit_buffer(): buffer must be in VALID or FAILED state") \ |
| 3607 | _SG_LOGITEM_XMACRO(UNINIT_IMAGE_INVALID_STATE, "sg_uninit_image(): image must be in VALID or FAILED state") \ |
| 3608 | _SG_LOGITEM_XMACRO(UNINIT_SAMPLER_INVALID_STATE, "sg_uninit_sampler(): sampler must be in VALID or FAILED state") \ |
| 3609 | _SG_LOGITEM_XMACRO(UNINIT_SHADER_INVALID_STATE, "sg_uninit_shader(): shader must be in VALID or FAILED state") \ |
| 3610 | _SG_LOGITEM_XMACRO(UNINIT_PIPELINE_INVALID_STATE, "sg_uninit_pipeline(): pipeline must be in VALID or FAILED state") \ |
| 3611 | _SG_LOGITEM_XMACRO(UNINIT_ATTACHMENTS_INVALID_STATE, "sg_uninit_attachments(): attachments must be in VALID or FAILED state") \ |
| 3612 | _SG_LOGITEM_XMACRO(FAIL_BUFFER_INVALID_STATE, "sg_fail_buffer(): buffer must be in ALLOC state") \ |
| 3613 | _SG_LOGITEM_XMACRO(FAIL_IMAGE_INVALID_STATE, "sg_fail_image(): image must be in ALLOC state") \ |
| 3614 | _SG_LOGITEM_XMACRO(FAIL_SAMPLER_INVALID_STATE, "sg_fail_sampler(): sampler must be in ALLOC state") \ |
| 3615 | _SG_LOGITEM_XMACRO(FAIL_SHADER_INVALID_STATE, "sg_fail_shader(): shader must be in ALLOC state") \ |
| 3616 | _SG_LOGITEM_XMACRO(FAIL_PIPELINE_INVALID_STATE, "sg_fail_pipeline(): pipeline must be in ALLOC state") \ |
| 3617 | _SG_LOGITEM_XMACRO(FAIL_ATTACHMENTS_INVALID_STATE, "sg_fail_attachments(): attachments must be in ALLOC state") \ |
| 3618 | _SG_LOGITEM_XMACRO(BUFFER_POOL_EXHAUSTED, "buffer pool exhausted") \ |
| 3619 | _SG_LOGITEM_XMACRO(IMAGE_POOL_EXHAUSTED, "image pool exhausted") \ |
| 3620 | _SG_LOGITEM_XMACRO(SAMPLER_POOL_EXHAUSTED, "sampler pool exhausted") \ |
| 3621 | _SG_LOGITEM_XMACRO(SHADER_POOL_EXHAUSTED, "shader pool exhausted") \ |
| 3622 | _SG_LOGITEM_XMACRO(PIPELINE_POOL_EXHAUSTED, "pipeline pool exhausted") \ |
| 3623 | _SG_LOGITEM_XMACRO(PASS_POOL_EXHAUSTED, "pass pool exhausted") \ |
| 3624 | _SG_LOGITEM_XMACRO(BEGINPASS_ATTACHMENT_INVALID, "sg_begin_pass: an attachment was provided that no longer exists") \ |
| 3625 | _SG_LOGITEM_XMACRO(DRAW_WITHOUT_BINDINGS, "attempting to draw without resource bindings") \ |
| 3626 | _SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_CANARY, "sg_buffer_desc not initialized") \ |
| 3627 | _SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_SIZE, "sg_buffer_desc.size and .data.size cannot both be 0") \ |
| 3628 | _SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_DATA, "immutable buffers must be initialized with data (sg_buffer_desc.data.ptr and sg_buffer_desc.data.size)") \ |
| 3629 | _SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_DATA_SIZE, "immutable buffer data size differs from buffer size") \ |
| 3630 | _SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_NO_DATA, "dynamic/stream usage buffers cannot be initialized with data") \ |
| 3631 | _SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_STORAGEBUFFER_SUPPORTED, "storage buffers not supported by the backend 3D API (requires OpenGL >= 4.3)") \ |
| 3632 | _SG_LOGITEM_XMACRO(VALIDATE_BUFFERDESC_STORAGEBUFFER_SIZE_MULTIPLE_4, "size of storage buffers must be a multiple of 4") \ |
| 3633 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDATA_NODATA, "sg_image_data: no data (.ptr and/or .size is zero)") \ |
| 3634 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDATA_DATA_SIZE, "sg_image_data: data size doesn't match expected surface size") \ |
| 3635 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_CANARY, "sg_image_desc not initialized") \ |
| 3636 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_WIDTH, "sg_image_desc.width must be > 0") \ |
| 3637 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_HEIGHT, "sg_image_desc.height must be > 0") \ |
| 3638 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_RT_PIXELFORMAT, "invalid pixel format for render-target image") \ |
| 3639 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_NONRT_PIXELFORMAT, "invalid pixel format for non-render-target image") \ |
| 3640 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_MSAA_BUT_NO_RT, "non-render-target images cannot be multisampled") \ |
| 3641 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_NO_MSAA_RT_SUPPORT, "MSAA not supported for this pixel format") \ |
| 3642 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_MSAA_NUM_MIPMAPS, "MSAA images must have num_mipmaps == 1") \ |
| 3643 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_MSAA_3D_IMAGE, "3D images cannot have a sample_count > 1") \ |
| 3644 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_DEPTH_3D_IMAGE, "3D images cannot have a depth/stencil image format") \ |
| 3645 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_RT_IMMUTABLE, "render target images must be SG_USAGE_IMMUTABLE") \ |
| 3646 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_RT_NO_DATA, "render target images cannot be initialized with data") \ |
| 3647 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_INJECTED_NO_DATA, "images with injected textures cannot be initialized with data") \ |
| 3648 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_DYNAMIC_NO_DATA, "dynamic/stream images cannot be initialized with data") \ |
| 3649 | _SG_LOGITEM_XMACRO(VALIDATE_IMAGEDESC_COMPRESSED_IMMUTABLE, "compressed images must be immutable") \ |
| 3650 | _SG_LOGITEM_XMACRO(VALIDATE_SAMPLERDESC_CANARY, "sg_sampler_desc not initialized") \ |
| 3651 | _SG_LOGITEM_XMACRO(VALIDATE_SAMPLERDESC_MINFILTER_NONE, "sg_sampler_desc.min_filter cannot be SG_FILTER_NONE") \ |
| 3652 | _SG_LOGITEM_XMACRO(VALIDATE_SAMPLERDESC_MAGFILTER_NONE, "sg_sampler_desc.mag_filter cannot be SG_FILTER_NONE") \ |
| 3653 | _SG_LOGITEM_XMACRO(VALIDATE_SAMPLERDESC_ANISTROPIC_REQUIRES_LINEAR_FILTERING, "sg_sampler_desc.max_anisotropy > 1 requires min/mag/mipmap_filter to be SG_FILTER_LINEAR") \ |
| 3654 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_CANARY, "sg_shader_desc not initialized") \ |
| 3655 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_SOURCE, "shader source code required") \ |
| 3656 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_BYTECODE, "shader byte code required") \ |
| 3657 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE, "shader source or byte code required") \ |
| 3658 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_BYTECODE_SIZE, "shader byte code length (in bytes) required") \ |
| 3659 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_CONT_UBS, "shader uniform blocks must occupy continuous slots") \ |
| 3660 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_CONT_UB_MEMBERS, "uniform block members must occupy continuous slots") \ |
| 3661 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_UB_MEMBERS, "GL backend requires uniform block member declarations") \ |
| 3662 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_UB_MEMBER_NAME, "uniform block member name missing") \ |
| 3663 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_UB_SIZE_MISMATCH, "size of uniform block members doesn't match uniform block size") \ |
| 3664 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_UB_ARRAY_COUNT, "uniform array count must be >= 1") \ |
| 3665 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_UB_STD140_ARRAY_TYPE, "uniform arrays only allowed for FLOAT4, INT4, MAT4 in std140 layout") \ |
| 3666 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_CONT_STORAGEBUFFERS, "shader stage storage buffers must occupy continuous slots (sg_shader_desc.vs|fs.storage_buffers[])") \ |
| 3667 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_STORAGEBUFFER_READONLY, "shader stage storage buffers must be readonly (sg_shader_desc.vs|fs.storage_buffers[].readonly)") \ |
| 3668 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_CONT_IMAGES, "shader stage images must occupy continuous slots (sg_shader_desc.vs|fs.images[])") \ |
| 3669 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_CONT_SAMPLERS, "shader stage samplers must occupy continuous slots (sg_shader_desc.vs|fs.samplers[])") \ |
| 3670 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_IMAGE_SLOT_OUT_OF_RANGE, "shader stage: image-sampler-pair image slot index is out of range (sg_shader_desc.vs|fs.image_sampler_pairs[].image_slot)") \ |
| 3671 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_SAMPLER_SLOT_OUT_OF_RANGE, "shader stage: image-sampler-pair image slot index is out of range (sg_shader_desc.vs|fs.image_sampler_pairs[].sampler_slot)") \ |
| 3672 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_NAME_REQUIRED_FOR_GL, "shader stage: image-sampler-pairs must be named in GL (sg_shader_desc.vs|fs.image_sampler_pairs[].name)") \ |
| 3673 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_HAS_NAME_BUT_NOT_USED, "shader stage: image-sampler-pair has name but .used field not true") \ |
| 3674 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_HAS_IMAGE_BUT_NOT_USED, "shader stage: image-sampler-pair has .image_slot != 0 but .used field not true") \ |
| 3675 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_HAS_SAMPLER_BUT_NOT_USED, "shader stage: image-sampler-pair .sampler_slot != 0 but .used field not true") \ |
| 3676 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NONFILTERING_SAMPLER_REQUIRED, "shader stage: image sample type UNFILTERABLE_FLOAT, UINT, SINT can only be used with NONFILTERING sampler") \ |
| 3677 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_COMPARISON_SAMPLER_REQUIRED, "shader stage: image sample type DEPTH can only be used with COMPARISON sampler") \ |
| 3678 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_IMAGE_NOT_REFERENCED_BY_IMAGE_SAMPLER_PAIRS, "shader stage: one or more images are note referenced by (sg_shader_desc.vs|fs.image_sampler_pairs[].image_slot)") \ |
| 3679 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_SAMPLER_NOT_REFERENCED_BY_IMAGE_SAMPLER_PAIRS, "shader stage: one or more samplers are not referenced by image-sampler-pairs (sg_shader_desc.vs|fs.image_sampler_pairs[].sampler_slot)") \ |
| 3680 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_NO_CONT_IMAGE_SAMPLER_PAIRS, "shader stage image-sampler-pairs must occupy continuous slots (sg_shader_desc.vs|fs.image_samplers[])") \ |
| 3681 | _SG_LOGITEM_XMACRO(VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG, "vertex attribute name/semantic string too long (max len 16)") \ |
| 3682 | _SG_LOGITEM_XMACRO(VALIDATE_PIPELINEDESC_CANARY, "sg_pipeline_desc not initialized") \ |
| 3683 | _SG_LOGITEM_XMACRO(VALIDATE_PIPELINEDESC_SHADER, "sg_pipeline_desc.shader missing or invalid") \ |
| 3684 | _SG_LOGITEM_XMACRO(VALIDATE_PIPELINEDESC_NO_CONT_ATTRS, "sg_pipeline_desc.layout.attrs is not continuous") \ |
| 3685 | _SG_LOGITEM_XMACRO(VALIDATE_PIPELINEDESC_LAYOUT_STRIDE4, "sg_pipeline_desc.layout.buffers[].stride must be multiple of 4") \ |
| 3686 | _SG_LOGITEM_XMACRO(VALIDATE_PIPELINEDESC_ATTR_SEMANTICS, "D3D11 missing vertex attribute semantics in shader") \ |
| 3687 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_CANARY, "sg_attachments_desc not initialized") \ |
| 3688 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_NO_ATTACHMENTS, "sg_attachments_desc no color or depth-stencil attachments") \ |
| 3689 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_NO_CONT_COLOR_ATTS, "color attachments must occupy continuous slots") \ |
| 3690 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_IMAGE, "pass attachment image is not valid") \ |
| 3691 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_MIPLEVEL, "pass attachment mip level is bigger than image has mipmaps") \ |
| 3692 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_FACE, "pass attachment image is cubemap, but face index is too big") \ |
| 3693 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_LAYER, "pass attachment image is array texture, but layer index is too big") \ |
| 3694 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_SLICE, "pass attachment image is 3d texture, but slice value is too big") \ |
| 3695 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_IMAGE_NO_RT, "pass attachment image must be have render_target=true") \ |
| 3696 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_COLOR_INV_PIXELFORMAT, "pass color-attachment images must be renderable color pixel format") \ |
| 3697 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_INV_PIXELFORMAT, "pass depth-attachment image must be depth or depth-stencil pixel format") \ |
| 3698 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_IMAGE_SIZES, "all pass attachments must have the same size") \ |
| 3699 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_IMAGE_SAMPLE_COUNTS, "all pass attachments must have the same sample count") \ |
| 3700 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_COLOR_IMAGE_MSAA, "pass resolve attachments must have a color attachment image with sample count > 1") \ |
| 3701 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE, "pass resolve attachment image not valid") \ |
| 3702 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_SAMPLE_COUNT, "pass resolve attachment image sample count must be 1") \ |
| 3703 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_MIPLEVEL, "pass resolve attachment mip level is bigger than image has mipmaps") \ |
| 3704 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_FACE, "pass resolve attachment is cubemap, but face index is too big") \ |
| 3705 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_LAYER, "pass resolve attachment is array texture, but layer index is too big") \ |
| 3706 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_SLICE, "pass resolve attachment is 3d texture, but slice value is too big") \ |
| 3707 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_NO_RT, "pass resolve attachment image must have render_target=true") \ |
| 3708 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_SIZES, "pass resolve attachment size must match color attachment image size") \ |
| 3709 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_FORMAT, "pass resolve attachment pixel format must match color attachment pixel format") \ |
| 3710 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE, "pass depth attachment image is not valid") \ |
| 3711 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_MIPLEVEL, "pass depth attachment mip level is bigger than image has mipmaps") \ |
| 3712 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_FACE, "pass depth attachment image is cubemap, but face index is too big") \ |
| 3713 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_LAYER, "pass depth attachment image is array texture, but layer index is too big") \ |
| 3714 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_SLICE, "pass depth attachment image is 3d texture, but slice value is too big") \ |
| 3715 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_NO_RT, "pass depth attachment image must be have render_target=true") \ |
| 3716 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_SIZES, "pass depth attachment image size must match color attachment image size") \ |
| 3717 | _SG_LOGITEM_XMACRO(VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_SAMPLE_COUNT, "pass depth attachment sample count must match color attachment sample count") \ |
| 3718 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_CANARY, "sg_begin_pass: pass struct not initialized") \ |
| 3719 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_ATTACHMENTS_EXISTS, "sg_begin_pass: attachments object no longer alive") \ |
| 3720 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_ATTACHMENTS_VALID, "sg_begin_pass: attachments object not in resource state VALID") \ |
| 3721 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_COLOR_ATTACHMENT_IMAGE, "sg_begin_pass: one or more color attachment images are not valid") \ |
| 3722 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_RESOLVE_ATTACHMENT_IMAGE, "sg_begin_pass: one or more resolve attachment images are not valid") \ |
| 3723 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_DEPTHSTENCIL_ATTACHMENT_IMAGE, "sg_begin_pass: one or more depth-stencil attachment images are not valid") \ |
| 3724 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_WIDTH, "sg_begin_pass: expected pass.swapchain.width > 0") \ |
| 3725 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_WIDTH_NOTSET, "sg_begin_pass: expected pass.swapchain.width == 0") \ |
| 3726 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_HEIGHT, "sg_begin_pass: expected pass.swapchain.height > 0") \ |
| 3727 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_HEIGHT_NOTSET, "sg_begin_pass: expected pass.swapchain.height == 0") \ |
| 3728 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_SAMPLECOUNT, "sg_begin_pass: expected pass.swapchain.sample_count > 0") \ |
| 3729 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_SAMPLECOUNT_NOTSET, "sg_begin_pass: expected pass.swapchain.sample_count == 0") \ |
| 3730 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_COLORFORMAT, "sg_begin_pass: expected pass.swapchain.color_format to be valid") \ |
| 3731 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_COLORFORMAT_NOTSET, "sg_begin_pass: expected pass.swapchain.color_format to be unset") \ |
| 3732 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_DEPTHFORMAT_NOTSET, "sg_begin_pass: expected pass.swapchain.depth_format to be unset") \ |
| 3733 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_CURRENTDRAWABLE, "sg_begin_pass: expected pass.swapchain.metal.current_drawable != 0") \ |
| 3734 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_CURRENTDRAWABLE_NOTSET, "sg_begin_pass: expected pass.swapchain.metal.current_drawable == 0") \ |
| 3735 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_DEPTHSTENCILTEXTURE, "sg_begin_pass: expected pass.swapchain.metal.depth_stencil_texture != 0") \ |
| 3736 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_DEPTHSTENCILTEXTURE_NOTSET, "sg_begin_pass: expected pass.swapchain.metal.depth_stencil_texture == 0") \ |
| 3737 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_MSAACOLORTEXTURE, "sg_begin_pass: expected pass.swapchain.metal.msaa_color_texture != 0") \ |
| 3738 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_MSAACOLORTEXTURE_NOTSET, "sg_begin_pass: expected pass.swapchain.metal.msaa_color_texture == 0") \ |
| 3739 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RENDERVIEW, "sg_begin_pass: expected pass.swapchain.d3d11.render_view != 0") \ |
| 3740 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RENDERVIEW_NOTSET, "sg_begin_pass: expected pass.swapchain.d3d11.render_view == 0") \ |
| 3741 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RESOLVEVIEW, "sg_begin_pass: expected pass.swapchain.d3d11.resolve_view != 0") \ |
| 3742 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RESOLVEVIEW_NOTSET, "sg_begin_pass: expected pass.swapchain.d3d11.resolve_view == 0") \ |
| 3743 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_DEPTHSTENCILVIEW, "sg_begin_pass: expected pass.swapchain.d3d11.depth_stencil_view != 0") \ |
| 3744 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_DEPTHSTENCILVIEW_NOTSET, "sg_begin_pass: expected pass.swapchain.d3d11.depth_stencil_view == 0") \ |
| 3745 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RENDERVIEW, "sg_begin_pass: expected pass.swapchain.wgpu.render_view != 0") \ |
| 3746 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RENDERVIEW_NOTSET, "sg_begin_pass: expected pass.swapchain.wgpu.render_view == 0") \ |
| 3747 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RESOLVEVIEW, "sg_begin_pass: expected pass.swapchain.wgpu.resolve_view != 0") \ |
| 3748 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RESOLVEVIEW_NOTSET, "sg_begin_pass: expected pass.swapchain.wgpu.resolve_view == 0") \ |
| 3749 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_DEPTHSTENCILVIEW, "sg_begin_pass: expected pass.swapchain.wgpu.depth_stencil_view != 0") \ |
| 3750 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_DEPTHSTENCILVIEW_NOTSET, "sg_begin_pass: expected pass.swapchain.wgpu.depth_stencil_view == 0") \ |
| 3751 | _SG_LOGITEM_XMACRO(VALIDATE_BEGINPASS_SWAPCHAIN_GL_EXPECT_FRAMEBUFFER_NOTSET, "sg_begin_pass: expected pass.swapchain.gl.framebuffer == 0") \ |
| 3752 | _SG_LOGITEM_XMACRO(VALIDATE_APIP_PIPELINE_VALID_ID, "sg_apply_pipeline: invalid pipeline id provided") \ |
| 3753 | _SG_LOGITEM_XMACRO(VALIDATE_APIP_PIPELINE_EXISTS, "sg_apply_pipeline: pipeline object no longer alive") \ |
| 3754 | _SG_LOGITEM_XMACRO(VALIDATE_APIP_PIPELINE_VALID, "sg_apply_pipeline: pipeline object not in valid state") \ |
| 3755 | _SG_LOGITEM_XMACRO(VALIDATE_APIP_SHADER_EXISTS, "sg_apply_pipeline: shader object no longer alive") \ |
| 3756 | _SG_LOGITEM_XMACRO(VALIDATE_APIP_SHADER_VALID, "sg_apply_pipeline: shader object not in valid state") \ |
| 3757 | _SG_LOGITEM_XMACRO(VALIDATE_APIP_CURPASS_ATTACHMENTS_EXISTS, "sg_apply_pipeline: current pass attachments no longer alive") \ |
| 3758 | _SG_LOGITEM_XMACRO(VALIDATE_APIP_CURPASS_ATTACHMENTS_VALID, "sg_apply_pipeline: current pass attachments not in valid state") \ |
| 3759 | _SG_LOGITEM_XMACRO(VALIDATE_APIP_ATT_COUNT, "sg_apply_pipeline: number of pipeline color attachments doesn't match number of pass color attachments") \ |
| 3760 | _SG_LOGITEM_XMACRO(VALIDATE_APIP_COLOR_FORMAT, "sg_apply_pipeline: pipeline color attachment pixel format doesn't match pass color attachment pixel format") \ |
| 3761 | _SG_LOGITEM_XMACRO(VALIDATE_APIP_DEPTH_FORMAT, "sg_apply_pipeline: pipeline depth pixel_format doesn't match pass depth attachment pixel format") \ |
| 3762 | _SG_LOGITEM_XMACRO(VALIDATE_APIP_SAMPLE_COUNT, "sg_apply_pipeline: pipeline MSAA sample count doesn't match render pass attachment sample count") \ |
| 3763 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_PIPELINE, "sg_apply_bindings: must be called after sg_apply_pipeline") \ |
| 3764 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_PIPELINE_EXISTS, "sg_apply_bindings: currently applied pipeline object no longer alive") \ |
| 3765 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_PIPELINE_VALID, "sg_apply_bindings: currently applied pipeline object not in valid state") \ |
| 3766 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VBS, "sg_apply_bindings: number of vertex buffers doesn't match number of pipeline vertex layouts") \ |
| 3767 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VB_EXISTS, "sg_apply_bindings: vertex buffer no longer alive") \ |
| 3768 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VB_TYPE, "sg_apply_bindings: buffer in vertex buffer slot is not a SG_BUFFERTYPE_VERTEXBUFFER") \ |
| 3769 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VB_OVERFLOW, "sg_apply_bindings: buffer in vertex buffer slot is overflown") \ |
| 3770 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_NO_IB, "sg_apply_bindings: pipeline object defines indexed rendering, but no index buffer provided") \ |
| 3771 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_IB, "sg_apply_bindings: pipeline object defines non-indexed rendering, but index buffer provided") \ |
| 3772 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_IB_EXISTS, "sg_apply_bindings: index buffer no longer alive") \ |
| 3773 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_IB_TYPE, "sg_apply_bindings: buffer in index buffer slot is not a SG_BUFFERTYPE_INDEXBUFFER") \ |
| 3774 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_IB_OVERFLOW, "sg_apply_bindings: buffer in index buffer slot is overflown") \ |
| 3775 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_IMAGE_BINDING, "sg_apply_bindings: image binding on vertex stage is missing or the image handle is invalid") \ |
| 3776 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_IMG_EXISTS, "sg_apply_bindings: image bound to vertex stage no longer alive") \ |
| 3777 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_IMAGE_TYPE_MISMATCH, "sg_apply_bindings: type of image bound to vertex stage doesn't match shader desc") \ |
| 3778 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_IMAGE_MSAA, "sg_apply_bindings: cannot bind image with sample_count>1 to vertex stage") \ |
| 3779 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_FILTERABLE_IMAGE, "sg_apply_bindings: filterable image expected on vertex stage") \ |
| 3780 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_DEPTH_IMAGE, "sg_apply_bindings: depth image expected on vertex stage") \ |
| 3781 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_UNEXPECTED_IMAGE_BINDING, "sg_apply_bindings: unexpected image binding on vertex stage") \ |
| 3782 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_SAMPLER_BINDING, "sg_apply_bindings: sampler binding on vertex stage is missing or the sampler handle is invalid") \ |
| 3783 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_UNEXPECTED_SAMPLER_COMPARE_NEVER, "sg_apply_bindings: shader expects SG_SAMPLERTYPE_COMPARISON on vertex stage but sampler has SG_COMPAREFUNC_NEVER") \ |
| 3784 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_SAMPLER_COMPARE_NEVER, "sg_apply_bindings: shader expects SG_SAMPLERTYPE_FILTERING or SG_SAMPLERTYPE_NONFILTERING on vertex stage but sampler doesn't have SG_COMPAREFUNC_NEVER") \ |
| 3785 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_NONFILTERING_SAMPLER, "sg_apply_bindings: shader expected SG_SAMPLERTYPE_NONFILTERING on vertex stage, but sampler has SG_FILTER_LINEAR filters") \ |
| 3786 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_UNEXPECTED_SAMPLER_BINDING, "sg_apply_bindings: unexpected sampler binding on vertex stage") \ |
| 3787 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_SMP_EXISTS, "sg_apply_bindings: sampler bound to vertex stage no longer alive") \ |
| 3788 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_EXPECTED_STORAGEBUFFER_BINDING, "sg_apply_bindings: storage buffer binding on vertex stage is missing or the buffer handle is invalid") \ |
| 3789 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_STORAGEBUFFER_EXISTS, "sg_apply_bindings: storage buffer bound to vertex stage no longer alive") \ |
| 3790 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_STORAGEBUFFER_BINDING_BUFFERTYPE, "sg_apply_bindings: buffer bound to vertex stage storage buffer slot is not of type storage buffer") \ |
| 3791 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_VS_UNEXPECTED_STORAGEBUFFER_BINDING, "sg_apply_bindings: unexpected storage buffer binding on vertex stage") \ |
| 3792 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_IMAGE_BINDING, "sg_apply_bindings: image binding on fragment stage is missing or the image handle is invalid") \ |
| 3793 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_IMG_EXISTS, "sg_apply_bindings: image bound to fragment stage no longer alive") \ |
| 3794 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_IMAGE_TYPE_MISMATCH, "sg_apply_bindings: type of image bound to fragment stage doesn't match shader desc") \ |
| 3795 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_IMAGE_MSAA, "sg_apply_bindings: cannot bind image with sample_count>1 to fragment stage") \ |
| 3796 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_FILTERABLE_IMAGE, "sg_apply_bindings: filterable image expected on fragment stage") \ |
| 3797 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_DEPTH_IMAGE, "sg_apply_bindings: depth image expected on fragment stage") \ |
| 3798 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_UNEXPECTED_IMAGE_BINDING, "sg_apply_bindings: unexpected image binding on fragment stage") \ |
| 3799 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_SAMPLER_BINDING, "sg_apply_bindings: sampler binding on fragment stage is missing or the sampler handle is invalid") \ |
| 3800 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_UNEXPECTED_SAMPLER_COMPARE_NEVER, "sg_apply_bindings: shader expects SG_SAMPLERTYPE_COMPARISON on fragment stage but sampler has SG_COMPAREFUNC_NEVER") \ |
| 3801 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_SAMPLER_COMPARE_NEVER, "sg_apply_bindings: shader expects SG_SAMPLERTYPE_FILTERING on fragment stage but sampler doesn't have SG_COMPAREFUNC_NEVER") \ |
| 3802 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_NONFILTERING_SAMPLER, "sg_apply_bindings: shader expected SG_SAMPLERTYPE_NONFILTERING on fragment stage, but sampler has SG_FILTER_LINEAR filters") \ |
| 3803 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_UNEXPECTED_SAMPLER_BINDING, "sg_apply_bindings: unexpected sampler binding on fragment stage") \ |
| 3804 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_SMP_EXISTS, "sg_apply_bindings: sampler bound to fragment stage no longer alive") \ |
| 3805 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_EXPECTED_STORAGEBUFFER_BINDING, "sg_apply_bindings: storage buffer binding on fragment stage is missing or the buffer handle is invalid") \ |
| 3806 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_STORAGEBUFFER_EXISTS, "sg_apply_bindings: storage buffer bound to fragment stage no longer alive") \ |
| 3807 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_STORAGEBUFFER_BINDING_BUFFERTYPE, "sg_apply_bindings: buffer bound to frahment stage storage buffer slot is not of type storage buffer") \ |
| 3808 | _SG_LOGITEM_XMACRO(VALIDATE_ABND_FS_UNEXPECTED_STORAGEBUFFER_BINDING, "sg_apply_bindings: unexpected storage buffer binding on fragment stage") \ |
| 3809 | _SG_LOGITEM_XMACRO(VALIDATE_AUB_NO_PIPELINE, "sg_apply_uniforms: must be called after sg_apply_pipeline()") \ |
| 3810 | _SG_LOGITEM_XMACRO(VALIDATE_AUB_NO_UB_AT_SLOT, "sg_apply_uniforms: no uniform block declaration at this shader stage UB slot") \ |
| 3811 | _SG_LOGITEM_XMACRO(VALIDATE_AUB_SIZE, "sg_apply_uniforms: data size doesn't match declared uniform block size") \ |
| 3812 | _SG_LOGITEM_XMACRO(VALIDATE_UPDATEBUF_USAGE, "sg_update_buffer: cannot update immutable buffer") \ |
| 3813 | _SG_LOGITEM_XMACRO(VALIDATE_UPDATEBUF_SIZE, "sg_update_buffer: update size is bigger than buffer size") \ |
| 3814 | _SG_LOGITEM_XMACRO(VALIDATE_UPDATEBUF_ONCE, "sg_update_buffer: only one update allowed per buffer and frame") \ |
| 3815 | _SG_LOGITEM_XMACRO(VALIDATE_UPDATEBUF_APPEND, "sg_update_buffer: cannot call sg_update_buffer and sg_append_buffer in same frame") \ |
| 3816 | _SG_LOGITEM_XMACRO(VALIDATE_APPENDBUF_USAGE, "sg_append_buffer: cannot append to immutable buffer") \ |
| 3817 | _SG_LOGITEM_XMACRO(VALIDATE_APPENDBUF_SIZE, "sg_append_buffer: overall appended size is bigger than buffer size") \ |
| 3818 | _SG_LOGITEM_XMACRO(VALIDATE_APPENDBUF_UPDATE, "sg_append_buffer: cannot call sg_append_buffer and sg_update_buffer in same frame") \ |
| 3819 | _SG_LOGITEM_XMACRO(VALIDATE_UPDIMG_USAGE, "sg_update_image: cannot update immutable image") \ |
| 3820 | _SG_LOGITEM_XMACRO(VALIDATE_UPDIMG_ONCE, "sg_update_image: only one update allowed per image and frame") \ |
| 3821 | _SG_LOGITEM_XMACRO(VALIDATION_FAILED, "validation layer checks failed") \ |
| 3822 | |
| 3823 | #define _SG_LOGITEM_XMACRO(item,msg) SG_LOGITEM_##item, |
| 3824 | typedef enum sg_log_item { |
| 3825 | _SG_LOG_ITEMS |
| 3826 | } sg_log_item; |
| 3827 | #undef _SG_LOGITEM_XMACRO |
| 3828 | |
| 3829 | /* |
| 3830 | sg_desc |
| 3831 | |
| 3832 | The sg_desc struct contains configuration values for sokol_gfx, |
| 3833 | it is used as parameter to the sg_setup() call. |
| 3834 | |
| 3835 | The default configuration is: |
| 3836 | |
| 3837 | .buffer_pool_size 128 |
| 3838 | .image_pool_size 128 |
| 3839 | .sampler_pool_size 64 |
| 3840 | .shader_pool_size 32 |
| 3841 | .pipeline_pool_size 64 |
| 3842 | .pass_pool_size 16 |
| 3843 | .uniform_buffer_size 4 MB (4*1024*1024) |
| 3844 | .max_commit_listeners 1024 |
| 3845 | .disable_validation false |
| 3846 | .mtl_force_managed_storage_mode false |
| 3847 | .wgpu_disable_bindgroups_cache false |
| 3848 | .wgpu_bindgroups_cache_size 1024 |
| 3849 | |
| 3850 | .allocator.alloc_fn 0 (in this case, malloc() will be called) |
| 3851 | .allocator.free_fn 0 (in this case, free() will be called) |
| 3852 | .allocator.user_data 0 |
| 3853 | |
| 3854 | .environment.defaults.color_format: default value depends on selected backend: |
| 3855 | all GL backends: SG_PIXELFORMAT_RGBA8 |
| 3856 | Metal and D3D11: SG_PIXELFORMAT_BGRA8 |
| 3857 | WebGPU: *no default* (must be queried from WebGPU swapchain object) |
| 3858 | .environment.defaults.depth_format: SG_PIXELFORMAT_DEPTH_STENCIL |
| 3859 | .environment.defaults.sample_count: 1 |
| 3860 | |
| 3861 | Metal specific: |
| 3862 | (NOTE: All Objective-C object references are transferred through |
| 3863 | a bridged (const void*) to sokol_gfx, which will use a unretained |
| 3864 | bridged cast (__bridged id<xxx>) to retrieve the Objective-C |
| 3865 | references back. Since the bridge cast is unretained, the caller |
| 3866 | must hold a strong reference to the Objective-C object for the |
| 3867 | duration of the sokol_gfx call! |
| 3868 | |
| 3869 | .mtl_force_managed_storage_mode |
| 3870 | when enabled, Metal buffers and texture resources are created in managed storage |
| 3871 | mode, otherwise sokol-gfx will decide whether to create buffers and |
| 3872 | textures in managed or shared storage mode (this is mainly a debugging option) |
| 3873 | .mtl_use_command_buffer_with_retained_references |
| 3874 | when true, the sokol-gfx Metal backend will use Metal command buffers which |
| 3875 | bump the reference count of resource objects as long as they are inflight, |
| 3876 | this is slower than the default command-buffer-with-unretained-references |
| 3877 | method, this may be a workaround when confronted with lifetime validation |
| 3878 | errors from the Metal validation layer until a proper fix has been implemented |
| 3879 | .environment.metal.device |
| 3880 | a pointer to the MTLDevice object |
| 3881 | |
| 3882 | D3D11 specific: |
| 3883 | .environment.d3d11.device |
| 3884 | a pointer to the ID3D11Device object, this must have been created |
| 3885 | before sg_setup() is called |
| 3886 | .environment.d3d11.device_context |
| 3887 | a pointer to the ID3D11DeviceContext object |
| 3888 | |
| 3889 | WebGPU specific: |
| 3890 | .wgpu_disable_bindgroups_cache |
| 3891 | When this is true, the WebGPU backend will create and immediately |
| 3892 | release a BindGroup object in the sg_apply_bindings() call, only |
| 3893 | use this for debugging purposes. |
| 3894 | .wgpu_bindgroups_cache_size |
| 3895 | The size of the bindgroups cache for re-using BindGroup objects |
| 3896 | between sg_apply_bindings() calls. The smaller the cache size, |
| 3897 | the more likely are cache slot collisions which will cause |
| 3898 | a BindGroups object to be destroyed and a new one created. |
| 3899 | Use the information returned by sg_query_stats() to check |
| 3900 | if this is a frequent occurrence, and increase the cache size as |
| 3901 | needed (the default is 1024). |
| 3902 | NOTE: wgpu_bindgroups_cache_size must be a power-of-2 number! |
| 3903 | .environment.wgpu.device |
| 3904 | a WGPUDevice handle |
| 3905 | |
| 3906 | When using sokol_gfx.h and sokol_app.h together, consider using the |
| 3907 | helper function sglue_environment() in the sokol_glue.h header to |
| 3908 | initialize the sg_desc.environment nested struct. sglue_environment() returns |
| 3909 | a completely initialized sg_environment struct with information |
| 3910 | provided by sokol_app.h. |
| 3911 | */ |
| 3912 | typedef struct sg_environment_defaults { |
| 3913 | sg_pixel_format color_format; |
| 3914 | sg_pixel_format depth_format; |
| 3915 | int sample_count; |
| 3916 | } sg_environment_defaults; |
| 3917 | |
| 3918 | typedef struct sg_metal_environment { |
| 3919 | const void* device; |
| 3920 | } sg_metal_environment; |
| 3921 | |
| 3922 | typedef struct sg_d3d11_environment { |
| 3923 | const void* device; |
| 3924 | const void* device_context; |
| 3925 | } sg_d3d11_environment; |
| 3926 | |
| 3927 | typedef struct sg_wgpu_environment { |
| 3928 | const void* device; |
| 3929 | } sg_wgpu_environment; |
| 3930 | |
| 3931 | typedef struct sg_environment { |
| 3932 | sg_environment_defaults defaults; |
| 3933 | sg_metal_environment metal; |
| 3934 | sg_d3d11_environment d3d11; |
| 3935 | sg_wgpu_environment wgpu; |
| 3936 | } sg_environment; |
| 3937 | |
| 3938 | /* |
| 3939 | sg_commit_listener |
| 3940 | |
| 3941 | Used with function sg_add_commit_listener() to add a callback |
| 3942 | which will be called in sg_commit(). This is useful for libraries |
| 3943 | building on top of sokol-gfx to be notified about when a frame |
| 3944 | ends (instead of having to guess, or add a manual 'new-frame' |
| 3945 | function. |
| 3946 | */ |
| 3947 | typedef struct sg_commit_listener { |
| 3948 | void (*func)(void* user_data); |
| 3949 | void* user_data; |
| 3950 | } sg_commit_listener; |
| 3951 | |
| 3952 | /* |
| 3953 | sg_allocator |
| 3954 | |
| 3955 | Used in sg_desc to provide custom memory-alloc and -free functions |
| 3956 | to sokol_gfx.h. If memory management should be overridden, both the |
| 3957 | alloc_fn and free_fn function must be provided (e.g. it's not valid to |
| 3958 | override one function but not the other). |
| 3959 | */ |
| 3960 | typedef struct sg_allocator { |
| 3961 | void* (*alloc_fn)(size_t size, void* user_data); |
| 3962 | void (*free_fn)(void* ptr, void* user_data); |
| 3963 | void* user_data; |
| 3964 | } sg_allocator; |
| 3965 | |
| 3966 | /* |
| 3967 | sg_logger |
| 3968 | |
| 3969 | Used in sg_desc to provide a logging function. Please be aware |
| 3970 | that without logging function, sokol-gfx will be completely |
| 3971 | silent, e.g. it will not report errors, warnings and |
| 3972 | validation layer messages. For maximum error verbosity, |
| 3973 | compile in debug mode (e.g. NDEBUG *not* defined) and provide a |
| 3974 | compatible logger function in the sg_setup() call |
| 3975 | (for instance the standard logging function from sokol_log.h). |
| 3976 | */ |
| 3977 | typedef struct sg_logger { |
| 3978 | void (*func)( |
| 3979 | const char* tag, // always "sg" |
| 3980 | uint32_t log_level, // 0=panic, 1=error, 2=warning, 3=info |
| 3981 | uint32_t log_item_id, // SG_LOGITEM_* |
| 3982 | const char* message_or_null, // a message string, may be nullptr in release mode |
| 3983 | uint32_t line_nr, // line number in sokol_gfx.h |
| 3984 | const char* filename_or_null, // source filename, may be nullptr in release mode |
| 3985 | void* user_data); |
| 3986 | void* user_data; |
| 3987 | } sg_logger; |
| 3988 | |
| 3989 | typedef struct sg_desc { |
| 3990 | uint32_t _start_canary; |
| 3991 | int buffer_pool_size; |
| 3992 | int image_pool_size; |
| 3993 | int sampler_pool_size; |
| 3994 | int shader_pool_size; |
| 3995 | int pipeline_pool_size; |
| 3996 | int attachments_pool_size; |
| 3997 | int uniform_buffer_size; |
| 3998 | int max_commit_listeners; |
| 3999 | bool disable_validation; // disable validation layer even in debug mode, useful for tests |
| 4000 | bool mtl_force_managed_storage_mode; // for debugging: use Metal managed storage mode for resources even with UMA |
| 4001 | bool mtl_use_command_buffer_with_retained_references; // Metal: use a managed MTLCommandBuffer which ref-counts used resources |
| 4002 | bool wgpu_disable_bindgroups_cache; // set to true to disable the WebGPU backend BindGroup cache |
| 4003 | int wgpu_bindgroups_cache_size; // number of slots in the WebGPU bindgroup cache (must be 2^N) |
| 4004 | sg_allocator allocator; |
| 4005 | sg_logger logger; // optional log function override |
| 4006 | sg_environment environment; |
| 4007 | uint32_t _end_canary; |
| 4008 | } sg_desc; |
| 4009 | |
| 4010 | // setup and misc functions |
| 4011 | SOKOL_GFX_API_DECL void sg_setup(const sg_desc* desc); |
| 4012 | SOKOL_GFX_API_DECL void sg_shutdown(void); |
| 4013 | SOKOL_GFX_API_DECL bool sg_isvalid(void); |
| 4014 | SOKOL_GFX_API_DECL void sg_reset_state_cache(void); |
| 4015 | SOKOL_GFX_API_DECL sg_trace_hooks sg_install_trace_hooks(const sg_trace_hooks* trace_hooks); |
| 4016 | SOKOL_GFX_API_DECL void sg_push_debug_group(const char* name); |
| 4017 | SOKOL_GFX_API_DECL void sg_pop_debug_group(void); |
| 4018 | SOKOL_GFX_API_DECL bool sg_add_commit_listener(sg_commit_listener listener); |
| 4019 | SOKOL_GFX_API_DECL bool sg_remove_commit_listener(sg_commit_listener listener); |
| 4020 | |
| 4021 | // resource creation, destruction and updating |
| 4022 | SOKOL_GFX_API_DECL sg_buffer sg_make_buffer(const sg_buffer_desc* desc); |
| 4023 | SOKOL_GFX_API_DECL sg_image sg_make_image(const sg_image_desc* desc); |
| 4024 | SOKOL_GFX_API_DECL sg_sampler sg_make_sampler(const sg_sampler_desc* desc); |
| 4025 | SOKOL_GFX_API_DECL sg_shader sg_make_shader(const sg_shader_desc* desc); |
| 4026 | SOKOL_GFX_API_DECL sg_pipeline sg_make_pipeline(const sg_pipeline_desc* desc); |
| 4027 | SOKOL_GFX_API_DECL sg_attachments sg_make_attachments(const sg_attachments_desc* desc); |
| 4028 | SOKOL_GFX_API_DECL void sg_destroy_buffer(sg_buffer buf); |
| 4029 | SOKOL_GFX_API_DECL void sg_destroy_image(sg_image img); |
| 4030 | SOKOL_GFX_API_DECL void sg_destroy_sampler(sg_sampler smp); |
| 4031 | SOKOL_GFX_API_DECL void sg_destroy_shader(sg_shader shd); |
| 4032 | SOKOL_GFX_API_DECL void sg_destroy_pipeline(sg_pipeline pip); |
| 4033 | SOKOL_GFX_API_DECL void sg_destroy_attachments(sg_attachments atts); |
| 4034 | SOKOL_GFX_API_DECL void sg_update_buffer(sg_buffer buf, const sg_range* data); |
| 4035 | SOKOL_GFX_API_DECL void sg_update_image(sg_image img, const sg_image_data* data); |
| 4036 | SOKOL_GFX_API_DECL int sg_append_buffer(sg_buffer buf, const sg_range* data); |
| 4037 | SOKOL_GFX_API_DECL bool sg_query_buffer_overflow(sg_buffer buf); |
| 4038 | SOKOL_GFX_API_DECL bool sg_query_buffer_will_overflow(sg_buffer buf, size_t size); |
| 4039 | |
| 4040 | // rendering functions |
| 4041 | SOKOL_GFX_API_DECL void sg_begin_pass(const sg_pass* pass); |
| 4042 | SOKOL_GFX_API_DECL void sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left); |
| 4043 | SOKOL_GFX_API_DECL void sg_apply_viewportf(float x, float y, float width, float height, bool origin_top_left); |
| 4044 | SOKOL_GFX_API_DECL void sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left); |
| 4045 | SOKOL_GFX_API_DECL void sg_apply_scissor_rectf(float x, float y, float width, float height, bool origin_top_left); |
| 4046 | SOKOL_GFX_API_DECL void sg_apply_pipeline(sg_pipeline pip); |
| 4047 | SOKOL_GFX_API_DECL void sg_apply_bindings(const sg_bindings* bindings); |
| 4048 | SOKOL_GFX_API_DECL void sg_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range* data); |
| 4049 | SOKOL_GFX_API_DECL void sg_draw(int base_element, int num_elements, int num_instances); |
| 4050 | SOKOL_GFX_API_DECL void sg_end_pass(void); |
| 4051 | SOKOL_GFX_API_DECL void sg_commit(void); |
| 4052 | |
| 4053 | // getting information |
| 4054 | SOKOL_GFX_API_DECL sg_desc sg_query_desc(void); |
| 4055 | SOKOL_GFX_API_DECL sg_backend sg_query_backend(void); |
| 4056 | SOKOL_GFX_API_DECL sg_features sg_query_features(void); |
| 4057 | SOKOL_GFX_API_DECL sg_limits sg_query_limits(void); |
| 4058 | SOKOL_GFX_API_DECL sg_pixelformat_info sg_query_pixelformat(sg_pixel_format fmt); |
| 4059 | SOKOL_GFX_API_DECL int sg_query_row_pitch(sg_pixel_format fmt, int width, int row_align_bytes); |
| 4060 | SOKOL_GFX_API_DECL int sg_query_surface_pitch(sg_pixel_format fmt, int width, int height, int row_align_bytes); |
| 4061 | // get current state of a resource (INITIAL, ALLOC, VALID, FAILED, INVALID) |
| 4062 | SOKOL_GFX_API_DECL sg_resource_state sg_query_buffer_state(sg_buffer buf); |
| 4063 | SOKOL_GFX_API_DECL sg_resource_state sg_query_image_state(sg_image img); |
| 4064 | SOKOL_GFX_API_DECL sg_resource_state sg_query_sampler_state(sg_sampler smp); |
| 4065 | SOKOL_GFX_API_DECL sg_resource_state sg_query_shader_state(sg_shader shd); |
| 4066 | SOKOL_GFX_API_DECL sg_resource_state sg_query_pipeline_state(sg_pipeline pip); |
| 4067 | SOKOL_GFX_API_DECL sg_resource_state sg_query_attachments_state(sg_attachments atts); |
| 4068 | // get runtime information about a resource |
| 4069 | SOKOL_GFX_API_DECL sg_buffer_info sg_query_buffer_info(sg_buffer buf); |
| 4070 | SOKOL_GFX_API_DECL sg_image_info sg_query_image_info(sg_image img); |
| 4071 | SOKOL_GFX_API_DECL sg_sampler_info sg_query_sampler_info(sg_sampler smp); |
| 4072 | SOKOL_GFX_API_DECL sg_shader_info sg_query_shader_info(sg_shader shd); |
| 4073 | SOKOL_GFX_API_DECL sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip); |
| 4074 | SOKOL_GFX_API_DECL sg_attachments_info sg_query_attachments_info(sg_attachments atts); |
| 4075 | // get desc structs matching a specific resource (NOTE that not all creation attributes may be provided) |
| 4076 | SOKOL_GFX_API_DECL sg_buffer_desc sg_query_buffer_desc(sg_buffer buf); |
| 4077 | SOKOL_GFX_API_DECL sg_image_desc sg_query_image_desc(sg_image img); |
| 4078 | SOKOL_GFX_API_DECL sg_sampler_desc sg_query_sampler_desc(sg_sampler smp); |
| 4079 | SOKOL_GFX_API_DECL sg_shader_desc sg_query_shader_desc(sg_shader shd); |
| 4080 | SOKOL_GFX_API_DECL sg_pipeline_desc sg_query_pipeline_desc(sg_pipeline pip); |
| 4081 | SOKOL_GFX_API_DECL sg_attachments_desc sg_query_attachments_desc(sg_attachments atts); |
| 4082 | // get resource creation desc struct with their default values replaced |
| 4083 | SOKOL_GFX_API_DECL sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc* desc); |
| 4084 | SOKOL_GFX_API_DECL sg_image_desc sg_query_image_defaults(const sg_image_desc* desc); |
| 4085 | SOKOL_GFX_API_DECL sg_sampler_desc sg_query_sampler_defaults(const sg_sampler_desc* desc); |
| 4086 | SOKOL_GFX_API_DECL sg_shader_desc sg_query_shader_defaults(const sg_shader_desc* desc); |
| 4087 | SOKOL_GFX_API_DECL sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc* desc); |
| 4088 | SOKOL_GFX_API_DECL sg_attachments_desc sg_query_attachments_defaults(const sg_attachments_desc* desc); |
| 4089 | |
| 4090 | // separate resource allocation and initialization (for async setup) |
| 4091 | SOKOL_GFX_API_DECL sg_buffer sg_alloc_buffer(void); |
| 4092 | SOKOL_GFX_API_DECL sg_image sg_alloc_image(void); |
| 4093 | SOKOL_GFX_API_DECL sg_sampler sg_alloc_sampler(void); |
| 4094 | SOKOL_GFX_API_DECL sg_shader sg_alloc_shader(void); |
| 4095 | SOKOL_GFX_API_DECL sg_pipeline sg_alloc_pipeline(void); |
| 4096 | SOKOL_GFX_API_DECL sg_attachments sg_alloc_attachments(void); |
| 4097 | SOKOL_GFX_API_DECL void sg_dealloc_buffer(sg_buffer buf); |
| 4098 | SOKOL_GFX_API_DECL void sg_dealloc_image(sg_image img); |
| 4099 | SOKOL_GFX_API_DECL void sg_dealloc_sampler(sg_sampler smp); |
| 4100 | SOKOL_GFX_API_DECL void sg_dealloc_shader(sg_shader shd); |
| 4101 | SOKOL_GFX_API_DECL void sg_dealloc_pipeline(sg_pipeline pip); |
| 4102 | SOKOL_GFX_API_DECL void sg_dealloc_attachments(sg_attachments attachments); |
| 4103 | SOKOL_GFX_API_DECL void sg_init_buffer(sg_buffer buf, const sg_buffer_desc* desc); |
| 4104 | SOKOL_GFX_API_DECL void sg_init_image(sg_image img, const sg_image_desc* desc); |
| 4105 | SOKOL_GFX_API_DECL void sg_init_sampler(sg_sampler smg, const sg_sampler_desc* desc); |
| 4106 | SOKOL_GFX_API_DECL void sg_init_shader(sg_shader shd, const sg_shader_desc* desc); |
| 4107 | SOKOL_GFX_API_DECL void sg_init_pipeline(sg_pipeline pip, const sg_pipeline_desc* desc); |
| 4108 | SOKOL_GFX_API_DECL void sg_init_attachments(sg_attachments attachments, const sg_attachments_desc* desc); |
| 4109 | SOKOL_GFX_API_DECL void sg_uninit_buffer(sg_buffer buf); |
| 4110 | SOKOL_GFX_API_DECL void sg_uninit_image(sg_image img); |
| 4111 | SOKOL_GFX_API_DECL void sg_uninit_sampler(sg_sampler smp); |
| 4112 | SOKOL_GFX_API_DECL void sg_uninit_shader(sg_shader shd); |
| 4113 | SOKOL_GFX_API_DECL void sg_uninit_pipeline(sg_pipeline pip); |
| 4114 | SOKOL_GFX_API_DECL void sg_uninit_attachments(sg_attachments atts); |
| 4115 | SOKOL_GFX_API_DECL void sg_fail_buffer(sg_buffer buf); |
| 4116 | SOKOL_GFX_API_DECL void sg_fail_image(sg_image img); |
| 4117 | SOKOL_GFX_API_DECL void sg_fail_sampler(sg_sampler smp); |
| 4118 | SOKOL_GFX_API_DECL void sg_fail_shader(sg_shader shd); |
| 4119 | SOKOL_GFX_API_DECL void sg_fail_pipeline(sg_pipeline pip); |
| 4120 | SOKOL_GFX_API_DECL void sg_fail_attachments(sg_attachments atts); |
| 4121 | |
| 4122 | // frame stats |
| 4123 | SOKOL_GFX_API_DECL void sg_enable_frame_stats(void); |
| 4124 | SOKOL_GFX_API_DECL void sg_disable_frame_stats(void); |
| 4125 | SOKOL_GFX_API_DECL bool sg_frame_stats_enabled(void); |
| 4126 | SOKOL_GFX_API_DECL sg_frame_stats sg_query_frame_stats(void); |
| 4127 | |
| 4128 | /* Backend-specific structs and functions, these may come in handy for mixing |
| 4129 | sokol-gfx rendering with 'native backend' rendering functions. |
| 4130 | |
| 4131 | This group of functions will be expanded as needed. |
| 4132 | */ |
| 4133 | |
| 4134 | typedef struct sg_d3d11_buffer_info { |
| 4135 | const void* buf; // ID3D11Buffer* |
| 4136 | } sg_d3d11_buffer_info; |
| 4137 | |
| 4138 | typedef struct sg_d3d11_image_info { |
| 4139 | const void* tex2d; // ID3D11Texture2D* |
| 4140 | const void* tex3d; // ID3D11Texture3D* |
| 4141 | const void* res; // ID3D11Resource* (either tex2d or tex3d) |
| 4142 | const void* srv; // ID3D11ShaderResourceView* |
| 4143 | } sg_d3d11_image_info; |
| 4144 | |
| 4145 | typedef struct sg_d3d11_sampler_info { |
| 4146 | const void* smp; // ID3D11SamplerState* |
| 4147 | } sg_d3d11_sampler_info; |
| 4148 | |
| 4149 | typedef struct sg_d3d11_shader_info { |
| 4150 | const void* vs_cbufs[SG_MAX_SHADERSTAGE_UBS]; // ID3D11Buffer* (vertex stage constant buffers) |
| 4151 | const void* fs_cbufs[SG_MAX_SHADERSTAGE_UBS]; // ID3D11Buffer* (fragment stage constant buffers) |
| 4152 | const void* vs; // ID3D11VertexShader* |
| 4153 | const void* fs; // ID3D11PixelShader* |
| 4154 | } sg_d3d11_shader_info; |
| 4155 | |
| 4156 | typedef struct sg_d3d11_pipeline_info { |
| 4157 | const void* il; // ID3D11InputLayout* |
| 4158 | const void* rs; // ID3D11RasterizerState* |
| 4159 | const void* dss; // ID3D11DepthStencilState* |
| 4160 | const void* bs; // ID3D11BlendState* |
| 4161 | } sg_d3d11_pipeline_info; |
| 4162 | |
| 4163 | typedef struct sg_d3d11_attachments_info { |
| 4164 | const void* color_rtv[SG_MAX_COLOR_ATTACHMENTS]; // ID3D11RenderTargetView |
| 4165 | const void* resolve_rtv[SG_MAX_COLOR_ATTACHMENTS]; // ID3D11RenderTargetView |
| 4166 | const void* dsv; // ID3D11DepthStencilView |
| 4167 | } sg_d3d11_attachments_info; |
| 4168 | |
| 4169 | typedef struct sg_mtl_buffer_info { |
| 4170 | const void* buf[SG_NUM_INFLIGHT_FRAMES]; // id<MTLBuffer> |
| 4171 | int active_slot; |
| 4172 | } sg_mtl_buffer_info; |
| 4173 | |
| 4174 | typedef struct sg_mtl_image_info { |
| 4175 | const void* tex[SG_NUM_INFLIGHT_FRAMES]; // id<MTLTexture> |
| 4176 | int active_slot; |
| 4177 | } sg_mtl_image_info; |
| 4178 | |
| 4179 | typedef struct sg_mtl_sampler_info { |
| 4180 | const void* smp; // id<MTLSamplerState> |
| 4181 | } sg_mtl_sampler_info; |
| 4182 | |
| 4183 | typedef struct sg_mtl_shader_info { |
| 4184 | const void* vs_lib; // id<MTLLibrary> |
| 4185 | const void* fs_lib; // id<MTLLibrary> |
| 4186 | const void* vs_func; // id<MTLFunction> |
| 4187 | const void* fs_func; // id<MTLFunction> |
| 4188 | } sg_mtl_shader_info; |
| 4189 | |
| 4190 | typedef struct sg_mtl_pipeline_info { |
| 4191 | const void* rps; // id<MTLRenderPipelineState> |
| 4192 | const void* dss; // id<MTLDepthStencilState> |
| 4193 | } sg_mtl_pipeline_info; |
| 4194 | |
| 4195 | typedef struct sg_wgpu_buffer_info { |
| 4196 | const void* buf; // WGPUBuffer |
| 4197 | } sg_wgpu_buffer_info; |
| 4198 | |
| 4199 | typedef struct sg_wgpu_image_info { |
| 4200 | const void* tex; // WGPUTexture |
| 4201 | const void* view; // WGPUTextureView |
| 4202 | } sg_wgpu_image_info; |
| 4203 | |
| 4204 | typedef struct sg_wgpu_sampler_info { |
| 4205 | const void* smp; // WGPUSampler |
| 4206 | } sg_wgpu_sampler_info; |
| 4207 | |
| 4208 | typedef struct sg_wgpu_shader_info { |
| 4209 | const void* vs_mod; // WGPUShaderModule |
| 4210 | const void* fs_mod; // WGPUShaderModule |
| 4211 | const void* bgl; // WGPUBindGroupLayout; |
| 4212 | } sg_wgpu_shader_info; |
| 4213 | |
| 4214 | typedef struct sg_wgpu_pipeline_info { |
| 4215 | const void* pip; // WGPURenderPipeline |
| 4216 | } sg_wgpu_pipeline_info; |
| 4217 | |
| 4218 | typedef struct sg_wgpu_attachments_info { |
| 4219 | const void* color_view[SG_MAX_COLOR_ATTACHMENTS]; // WGPUTextureView |
| 4220 | const void* resolve_view[SG_MAX_COLOR_ATTACHMENTS]; // WGPUTextureView |
| 4221 | const void* ds_view; // WGPUTextureView |
| 4222 | } sg_wgpu_attachments_info; |
| 4223 | |
| 4224 | typedef struct sg_gl_buffer_info { |
| 4225 | uint32_t buf[SG_NUM_INFLIGHT_FRAMES]; |
| 4226 | int active_slot; |
| 4227 | } sg_gl_buffer_info; |
| 4228 | |
| 4229 | typedef struct sg_gl_image_info { |
| 4230 | uint32_t tex[SG_NUM_INFLIGHT_FRAMES]; |
| 4231 | uint32_t tex_target; |
| 4232 | uint32_t msaa_render_buffer; |
| 4233 | int active_slot; |
| 4234 | } sg_gl_image_info; |
| 4235 | |
| 4236 | typedef struct sg_gl_sampler_info { |
| 4237 | uint32_t smp; |
| 4238 | } sg_gl_sampler_info; |
| 4239 | |
| 4240 | typedef struct sg_gl_shader_info { |
| 4241 | uint32_t prog; |
| 4242 | } sg_gl_shader_info; |
| 4243 | |
| 4244 | typedef struct sg_gl_attachments_info { |
| 4245 | uint32_t framebuffer; |
| 4246 | uint32_t msaa_resolve_framebuffer[SG_MAX_COLOR_ATTACHMENTS]; |
| 4247 | } sg_gl_attachments_info; |
| 4248 | |
| 4249 | // D3D11: return ID3D11Device |
| 4250 | SOKOL_GFX_API_DECL const void* sg_d3d11_device(void); |
| 4251 | // D3D11: return ID3D11DeviceContext |
| 4252 | SOKOL_GFX_API_DECL const void* sg_d3d11_device_context(void); |
| 4253 | // D3D11: get internal buffer resource objects |
| 4254 | SOKOL_GFX_API_DECL sg_d3d11_buffer_info sg_d3d11_query_buffer_info(sg_buffer buf); |
| 4255 | // D3D11: get internal image resource objects |
| 4256 | SOKOL_GFX_API_DECL sg_d3d11_image_info sg_d3d11_query_image_info(sg_image img); |
| 4257 | // D3D11: get internal sampler resource objects |
| 4258 | SOKOL_GFX_API_DECL sg_d3d11_sampler_info sg_d3d11_query_sampler_info(sg_sampler smp); |
| 4259 | // D3D11: get internal shader resource objects |
| 4260 | SOKOL_GFX_API_DECL sg_d3d11_shader_info sg_d3d11_query_shader_info(sg_shader shd); |
| 4261 | // D3D11: get internal pipeline resource objects |
| 4262 | SOKOL_GFX_API_DECL sg_d3d11_pipeline_info sg_d3d11_query_pipeline_info(sg_pipeline pip); |
| 4263 | // D3D11: get internal pass resource objects |
| 4264 | SOKOL_GFX_API_DECL sg_d3d11_attachments_info sg_d3d11_query_attachments_info(sg_attachments atts); |
| 4265 | |
| 4266 | // Metal: return __bridge-casted MTLDevice |
| 4267 | SOKOL_GFX_API_DECL const void* sg_mtl_device(void); |
| 4268 | // Metal: return __bridge-casted MTLRenderCommandEncoder in current pass (or zero if outside pass) |
| 4269 | SOKOL_GFX_API_DECL const void* sg_mtl_render_command_encoder(void); |
| 4270 | // Metal: get internal __bridge-casted buffer resource objects |
| 4271 | SOKOL_GFX_API_DECL sg_mtl_buffer_info sg_mtl_query_buffer_info(sg_buffer buf); |
| 4272 | // Metal: get internal __bridge-casted image resource objects |
| 4273 | SOKOL_GFX_API_DECL sg_mtl_image_info sg_mtl_query_image_info(sg_image img); |
| 4274 | // Metal: get internal __bridge-casted sampler resource objects |
| 4275 | SOKOL_GFX_API_DECL sg_mtl_sampler_info sg_mtl_query_sampler_info(sg_sampler smp); |
| 4276 | // Metal: get internal __bridge-casted shader resource objects |
| 4277 | SOKOL_GFX_API_DECL sg_mtl_shader_info sg_mtl_query_shader_info(sg_shader shd); |
| 4278 | // Metal: get internal __bridge-casted pipeline resource objects |
| 4279 | SOKOL_GFX_API_DECL sg_mtl_pipeline_info sg_mtl_query_pipeline_info(sg_pipeline pip); |
| 4280 | |
| 4281 | // WebGPU: return WGPUDevice object |
| 4282 | SOKOL_GFX_API_DECL const void* sg_wgpu_device(void); |
| 4283 | // WebGPU: return WGPUQueue object |
| 4284 | SOKOL_GFX_API_DECL const void* sg_wgpu_queue(void); |
| 4285 | // WebGPU: return this frame's WGPUCommandEncoder |
| 4286 | SOKOL_GFX_API_DECL const void* sg_wgpu_command_encoder(void); |
| 4287 | // WebGPU: return WGPURenderPassEncoder of current pass |
| 4288 | SOKOL_GFX_API_DECL const void* sg_wgpu_render_pass_encoder(void); |
| 4289 | // WebGPU: get internal buffer resource objects |
| 4290 | SOKOL_GFX_API_DECL sg_wgpu_buffer_info sg_wgpu_query_buffer_info(sg_buffer buf); |
| 4291 | // WebGPU: get internal image resource objects |
| 4292 | SOKOL_GFX_API_DECL sg_wgpu_image_info sg_wgpu_query_image_info(sg_image img); |
| 4293 | // WebGPU: get internal sampler resource objects |
| 4294 | SOKOL_GFX_API_DECL sg_wgpu_sampler_info sg_wgpu_query_sampler_info(sg_sampler smp); |
| 4295 | // WebGPU: get internal shader resource objects |
| 4296 | SOKOL_GFX_API_DECL sg_wgpu_shader_info sg_wgpu_query_shader_info(sg_shader shd); |
| 4297 | // WebGPU: get internal pipeline resource objects |
| 4298 | SOKOL_GFX_API_DECL sg_wgpu_pipeline_info sg_wgpu_query_pipeline_info(sg_pipeline pip); |
| 4299 | // WebGPU: get internal pass resource objects |
| 4300 | SOKOL_GFX_API_DECL sg_wgpu_attachments_info sg_wgpu_query_attachments_info(sg_attachments atts); |
| 4301 | |
| 4302 | // GL: get internal buffer resource objects |
| 4303 | SOKOL_GFX_API_DECL sg_gl_buffer_info sg_gl_query_buffer_info(sg_buffer buf); |
| 4304 | // GL: get internal image resource objects |
| 4305 | SOKOL_GFX_API_DECL sg_gl_image_info sg_gl_query_image_info(sg_image img); |
| 4306 | // GL: get internal sampler resource objects |
| 4307 | SOKOL_GFX_API_DECL sg_gl_sampler_info sg_gl_query_sampler_info(sg_sampler smp); |
| 4308 | // GL: get internal shader resource objects |
| 4309 | SOKOL_GFX_API_DECL sg_gl_shader_info sg_gl_query_shader_info(sg_shader shd); |
| 4310 | // GL: get internal pass resource objects |
| 4311 | SOKOL_GFX_API_DECL sg_gl_attachments_info sg_gl_query_attachments_info(sg_attachments atts); |
| 4312 | |
| 4313 | #ifdef __cplusplus |
| 4314 | } // extern "C" |
| 4315 | |
| 4316 | // reference-based equivalents for c++ |
| 4317 | inline void sg_setup(const sg_desc& desc) { return sg_setup(&desc); } |
| 4318 | |
| 4319 | inline sg_buffer sg_make_buffer(const sg_buffer_desc& desc) { return sg_make_buffer(&desc); } |
| 4320 | inline sg_image sg_make_image(const sg_image_desc& desc) { return sg_make_image(&desc); } |
| 4321 | inline sg_sampler sg_make_sampler(const sg_sampler_desc& desc) { return sg_make_sampler(&desc); } |
| 4322 | inline sg_shader sg_make_shader(const sg_shader_desc& desc) { return sg_make_shader(&desc); } |
| 4323 | inline sg_pipeline sg_make_pipeline(const sg_pipeline_desc& desc) { return sg_make_pipeline(&desc); } |
| 4324 | inline sg_attachments sg_make_attachments(const sg_attachments_desc& desc) { return sg_make_attachments(&desc); } |
| 4325 | inline void sg_update_image(sg_image img, const sg_image_data& data) { return sg_update_image(img, &data); } |
| 4326 | |
| 4327 | inline void sg_begin_pass(const sg_pass& pass) { return sg_begin_pass(&pass); } |
| 4328 | inline void sg_apply_bindings(const sg_bindings& bindings) { return sg_apply_bindings(&bindings); } |
| 4329 | inline void sg_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range& data) { return sg_apply_uniforms(stage, ub_index, &data); } |
| 4330 | |
| 4331 | inline sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc& desc) { return sg_query_buffer_defaults(&desc); } |
| 4332 | inline sg_image_desc sg_query_image_defaults(const sg_image_desc& desc) { return sg_query_image_defaults(&desc); } |
| 4333 | inline sg_sampler_desc sg_query_sampler_defaults(const sg_sampler_desc& desc) { return sg_query_sampler_defaults(&desc); } |
| 4334 | inline sg_shader_desc sg_query_shader_defaults(const sg_shader_desc& desc) { return sg_query_shader_defaults(&desc); } |
| 4335 | inline sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc& desc) { return sg_query_pipeline_defaults(&desc); } |
| 4336 | inline sg_attachments_desc sg_query_attachments_defaults(const sg_attachments_desc& desc) { return sg_query_attachments_defaults(&desc); } |
| 4337 | |
| 4338 | inline void sg_init_buffer(sg_buffer buf, const sg_buffer_desc& desc) { return sg_init_buffer(buf, &desc); } |
| 4339 | inline void sg_init_image(sg_image img, const sg_image_desc& desc) { return sg_init_image(img, &desc); } |
| 4340 | inline void sg_init_sampler(sg_sampler smp, const sg_sampler_desc& desc) { return sg_init_sampler(smp, &desc); } |
| 4341 | inline void sg_init_shader(sg_shader shd, const sg_shader_desc& desc) { return sg_init_shader(shd, &desc); } |
| 4342 | inline void sg_init_pipeline(sg_pipeline pip, const sg_pipeline_desc& desc) { return sg_init_pipeline(pip, &desc); } |
| 4343 | inline void sg_init_attachments(sg_attachments atts, const sg_attachments_desc& desc) { return sg_init_attachments(atts, &desc); } |
| 4344 | |
| 4345 | inline void sg_update_buffer(sg_buffer buf_id, const sg_range& data) { return sg_update_buffer(buf_id, &data); } |
| 4346 | inline int sg_append_buffer(sg_buffer buf_id, const sg_range& data) { return sg_append_buffer(buf_id, &data); } |
| 4347 | #endif |
| 4348 | #endif // SOKOL_GFX_INCLUDED |
| 4349 | |
| 4350 | // ██ ███ ███ ██████ ██ ███████ ███ ███ ███████ ███ ██ ████████ █████ ████████ ██ ██████ ███ ██ |
| 4351 | // ██ ████ ████ ██ ██ ██ ██ ████ ████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ |
| 4352 | // ██ ██ ████ ██ ██████ ██ █████ ██ ████ ██ █████ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ |
| 4353 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 4354 | // ██ ██ ██ ██ ███████ ███████ ██ ██ ███████ ██ ████ ██ ██ ██ ██ ██ ██████ ██ ████ |
| 4355 | // |
| 4356 | // >>implementation |
| 4357 | #ifdef SOKOL_GFX_IMPL |
| 4358 | #define SOKOL_GFX_IMPL_INCLUDED (1) |
| 4359 | |
| 4360 | #if !(defined(SOKOL_GLCORE)||defined(SOKOL_GLES3)||defined(SOKOL_D3D11)||defined(SOKOL_METAL)||defined(SOKOL_WGPU)||defined(SOKOL_DUMMY_BACKEND)) |
| 4361 | #error "Please select a backend with SOKOL_GLCORE, SOKOL_GLES3, SOKOL_D3D11, SOKOL_METAL, SOKOL_WGPU or SOKOL_DUMMY_BACKEND" |
| 4362 | #endif |
| 4363 | #if defined(SOKOL_MALLOC) || defined(SOKOL_CALLOC) || defined(SOKOL_FREE) |
| 4364 | #error "SOKOL_MALLOC/CALLOC/FREE macros are no longer supported, please use sg_desc.allocator to override memory allocation functions" |
| 4365 | #endif |
| 4366 | |
| 4367 | #include <stdlib.h> // malloc, free |
| 4368 | #include <string.h> // memset |
| 4369 | #include <float.h> // FLT_MAX |
| 4370 | |
| 4371 | #ifndef SOKOL_API_IMPL |
| 4372 | #define SOKOL_API_IMPL |
| 4373 | #endif |
| 4374 | #ifndef SOKOL_DEBUG |
| 4375 | #ifndef NDEBUG |
| 4376 | #define SOKOL_DEBUG |
| 4377 | #endif |
| 4378 | #endif |
| 4379 | #ifndef SOKOL_ASSERT |
| 4380 | #include <assert.h> |
| 4381 | #define SOKOL_ASSERT(c) assert(c) |
| 4382 | #endif |
| 4383 | #ifndef SOKOL_UNREACHABLE |
| 4384 | #define SOKOL_UNREACHABLE SOKOL_ASSERT(false) |
| 4385 | #endif |
| 4386 | |
| 4387 | #ifndef _SOKOL_PRIVATE |
| 4388 | #if defined(__GNUC__) || defined(__clang__) |
| 4389 | #define _SOKOL_PRIVATE __attribute__((unused)) static |
| 4390 | #else |
| 4391 | #define _SOKOL_PRIVATE static |
| 4392 | #endif |
| 4393 | #endif |
| 4394 | |
| 4395 | #ifndef _SOKOL_UNUSED |
| 4396 | #define _SOKOL_UNUSED(x) (void)(x) |
| 4397 | #endif |
| 4398 | |
| 4399 | #if defined(SOKOL_TRACE_HOOKS) |
| 4400 | #define _SG_TRACE_ARGS(fn, ...) if (_sg.hooks.fn) { _sg.hooks.fn(__VA_ARGS__, _sg.hooks.user_data); } |
| 4401 | #define _SG_TRACE_NOARGS(fn) if (_sg.hooks.fn) { _sg.hooks.fn(_sg.hooks.user_data); } |
| 4402 | #else |
| 4403 | #define _SG_TRACE_ARGS(fn, ...) |
| 4404 | #define _SG_TRACE_NOARGS(fn) |
| 4405 | #endif |
| 4406 | |
| 4407 | // default clear values |
| 4408 | #ifndef SG_DEFAULT_CLEAR_RED |
| 4409 | #define SG_DEFAULT_CLEAR_RED (0.5f) |
| 4410 | #endif |
| 4411 | #ifndef SG_DEFAULT_CLEAR_GREEN |
| 4412 | #define SG_DEFAULT_CLEAR_GREEN (0.5f) |
| 4413 | #endif |
| 4414 | #ifndef SG_DEFAULT_CLEAR_BLUE |
| 4415 | #define SG_DEFAULT_CLEAR_BLUE (0.5f) |
| 4416 | #endif |
| 4417 | #ifndef SG_DEFAULT_CLEAR_ALPHA |
| 4418 | #define SG_DEFAULT_CLEAR_ALPHA (1.0f) |
| 4419 | #endif |
| 4420 | #ifndef SG_DEFAULT_CLEAR_DEPTH |
| 4421 | #define SG_DEFAULT_CLEAR_DEPTH (1.0f) |
| 4422 | #endif |
| 4423 | #ifndef SG_DEFAULT_CLEAR_STENCIL |
| 4424 | #define SG_DEFAULT_CLEAR_STENCIL (0) |
| 4425 | #endif |
| 4426 | |
| 4427 | #ifdef _MSC_VER |
| 4428 | #pragma warning(push) |
| 4429 | #pragma warning(disable:4115) // named type definition in parentheses |
| 4430 | #pragma warning(disable:4505) // unreferenced local function has been removed |
| 4431 | #pragma warning(disable:4201) // nonstandard extension used: nameless struct/union (needed by d3d11.h) |
| 4432 | #pragma warning(disable:4054) // 'type cast': from function pointer |
| 4433 | #pragma warning(disable:4055) // 'type cast': from data pointer |
| 4434 | #endif |
| 4435 | |
| 4436 | #if defined(SOKOL_D3D11) |
| 4437 | #ifndef D3D11_NO_HELPERS |
| 4438 | #define D3D11_NO_HELPERS |
| 4439 | #endif |
| 4440 | #ifndef WIN32_LEAN_AND_MEAN |
| 4441 | #define WIN32_LEAN_AND_MEAN |
| 4442 | #endif |
| 4443 | #ifndef NOMINMAX |
| 4444 | #define NOMINMAX |
| 4445 | #endif |
| 4446 | #include <d3d11.h> |
| 4447 | #include <d3dcompiler.h> |
| 4448 | #ifdef _MSC_VER |
| 4449 | #pragma comment (lib, "kernel32") |
| 4450 | #pragma comment (lib, "user32") |
| 4451 | #pragma comment (lib, "dxgi") |
| 4452 | #pragma comment (lib, "d3d11") |
| 4453 | #endif |
| 4454 | #elif defined(SOKOL_METAL) |
| 4455 | // see https://clang.llvm.org/docs/LanguageExtensions.html#automatic-reference-counting |
| 4456 | #if !defined(__cplusplus) |
| 4457 | #if __has_feature(objc_arc) && !__has_feature(objc_arc_fields) |
| 4458 | #error "sokol_gfx.h requires __has_feature(objc_arc_field) if ARC is enabled (use a more recent compiler version)" |
| 4459 | #endif |
| 4460 | #endif |
| 4461 | #include <TargetConditionals.h> |
| 4462 | #include <AvailabilityMacros.h> |
| 4463 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 4464 | #define _SG_TARGET_MACOS (1) |
| 4465 | #else |
| 4466 | #define _SG_TARGET_IOS (1) |
| 4467 | #if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR |
| 4468 | #define _SG_TARGET_IOS_SIMULATOR (1) |
| 4469 | #endif |
| 4470 | #endif |
| 4471 | #import <Metal/Metal.h> |
| 4472 | #import <QuartzCore/CoreAnimation.h> // needed for CAMetalDrawable |
| 4473 | #elif defined(SOKOL_WGPU) |
| 4474 | #include <webgpu/webgpu.h> |
| 4475 | #if defined(__EMSCRIPTEN__) |
| 4476 | #include <emscripten/emscripten.h> |
| 4477 | #endif |
| 4478 | #elif defined(SOKOL_GLCORE) || defined(SOKOL_GLES3) |
| 4479 | #define _SOKOL_ANY_GL (1) |
| 4480 | |
| 4481 | // include platform specific GL headers (or on Win32: use an embedded GL loader) |
| 4482 | #if !defined(SOKOL_EXTERNAL_GL_LOADER) |
| 4483 | #if defined(_WIN32) |
| 4484 | #if defined(SOKOL_GLCORE) && !defined(SOKOL_EXTERNAL_GL_LOADER) |
| 4485 | #ifndef WIN32_LEAN_AND_MEAN |
| 4486 | #define WIN32_LEAN_AND_MEAN |
| 4487 | #endif |
| 4488 | #ifndef NOMINMAX |
| 4489 | #define NOMINMAX |
| 4490 | #endif |
| 4491 | #include <windows.h> |
| 4492 | #define _SOKOL_USE_WIN32_GL_LOADER (1) |
| 4493 | #pragma comment (lib, "kernel32") // GetProcAddress() |
| 4494 | #endif |
| 4495 | #elif defined(__APPLE__) |
| 4496 | #include <TargetConditionals.h> |
| 4497 | #ifndef GL_SILENCE_DEPRECATION |
| 4498 | #define GL_SILENCE_DEPRECATION |
| 4499 | #endif |
| 4500 | #if defined(TARGET_OS_IPHONE) && !TARGET_OS_IPHONE |
| 4501 | #include <OpenGL/gl3.h> |
| 4502 | #else |
| 4503 | #include <OpenGLES/ES3/gl.h> |
| 4504 | #include <OpenGLES/ES3/glext.h> |
| 4505 | #endif |
| 4506 | #elif defined(__EMSCRIPTEN__) || defined(__ANDROID__) |
| 4507 | #if defined(SOKOL_GLES3) |
| 4508 | #include <GLES3/gl3.h> |
| 4509 | #endif |
| 4510 | #elif defined(__linux__) || defined(__unix__) |
| 4511 | #if defined(SOKOL_GLCORE) |
| 4512 | #define GL_GLEXT_PROTOTYPES |
| 4513 | #include <GL/gl.h> |
| 4514 | #else |
| 4515 | #include <GLES3/gl3.h> |
| 4516 | #include <GLES3/gl3ext.h> |
| 4517 | #endif |
| 4518 | #endif |
| 4519 | #endif |
| 4520 | |
| 4521 | // optional GL loader definitions (only on Win32) |
| 4522 | #if defined(_SOKOL_USE_WIN32_GL_LOADER) |
| 4523 | #define __gl_h_ 1 |
| 4524 | #define __gl32_h_ 1 |
| 4525 | #define __gl31_h_ 1 |
| 4526 | #define __GL_H__ 1 |
| 4527 | #define __glext_h_ 1 |
| 4528 | #define __GLEXT_H_ 1 |
| 4529 | #define __gltypes_h_ 1 |
| 4530 | #define __glcorearb_h_ 1 |
| 4531 | #define __gl_glcorearb_h_ 1 |
| 4532 | #define GL_APIENTRY APIENTRY |
| 4533 | |
| 4534 | typedef unsigned int GLenum; |
| 4535 | typedef unsigned int GLuint; |
| 4536 | typedef int GLsizei; |
| 4537 | typedef char GLchar; |
| 4538 | typedef ptrdiff_t GLintptr; |
| 4539 | typedef ptrdiff_t GLsizeiptr; |
| 4540 | typedef double GLclampd; |
| 4541 | typedef unsigned short GLushort; |
| 4542 | typedef unsigned char GLubyte; |
| 4543 | typedef unsigned char GLboolean; |
| 4544 | typedef uint64_t GLuint64; |
| 4545 | typedef double GLdouble; |
| 4546 | typedef unsigned short GLhalf; |
| 4547 | typedef float GLclampf; |
| 4548 | typedef unsigned int GLbitfield; |
| 4549 | typedef signed char GLbyte; |
| 4550 | typedef short GLshort; |
| 4551 | typedef void GLvoid; |
| 4552 | typedef int64_t GLint64; |
| 4553 | typedef float GLfloat; |
| 4554 | typedef int GLint; |
| 4555 | #define GL_INT_2_10_10_10_REV 0x8D9F |
| 4556 | #define GL_R32F 0x822E |
| 4557 | #define GL_PROGRAM_POINT_SIZE 0x8642 |
| 4558 | #define GL_DEPTH_ATTACHMENT 0x8D00 |
| 4559 | #define GL_DEPTH_STENCIL_ATTACHMENT 0x821A |
| 4560 | #define GL_COLOR_ATTACHMENT2 0x8CE2 |
| 4561 | #define GL_COLOR_ATTACHMENT0 0x8CE0 |
| 4562 | #define GL_R16F 0x822D |
| 4563 | #define GL_COLOR_ATTACHMENT22 0x8CF6 |
| 4564 | #define GL_DRAW_FRAMEBUFFER 0x8CA9 |
| 4565 | #define GL_FRAMEBUFFER_COMPLETE 0x8CD5 |
| 4566 | #define GL_NUM_EXTENSIONS 0x821D |
| 4567 | #define GL_INFO_LOG_LENGTH 0x8B84 |
| 4568 | #define GL_VERTEX_SHADER 0x8B31 |
| 4569 | #define GL_INCR 0x1E02 |
| 4570 | #define GL_DYNAMIC_DRAW 0x88E8 |
| 4571 | #define GL_STATIC_DRAW 0x88E4 |
| 4572 | #define GL_TEXTURE_CUBE_MAP_POSITIVE_Z 0x8519 |
| 4573 | #define GL_TEXTURE_CUBE_MAP 0x8513 |
| 4574 | #define GL_FUNC_SUBTRACT 0x800A |
| 4575 | #define GL_FUNC_REVERSE_SUBTRACT 0x800B |
| 4576 | #define GL_CONSTANT_COLOR 0x8001 |
| 4577 | #define GL_DECR_WRAP 0x8508 |
| 4578 | #define GL_R8 0x8229 |
| 4579 | #define GL_LINEAR_MIPMAP_LINEAR 0x2703 |
| 4580 | #define GL_ELEMENT_ARRAY_BUFFER 0x8893 |
| 4581 | #define GL_SHORT 0x1402 |
| 4582 | #define GL_DEPTH_TEST 0x0B71 |
| 4583 | #define GL_TEXTURE_CUBE_MAP_NEGATIVE_Y 0x8518 |
| 4584 | #define GL_LINK_STATUS 0x8B82 |
| 4585 | #define GL_TEXTURE_CUBE_MAP_POSITIVE_Y 0x8517 |
| 4586 | #define GL_SAMPLE_ALPHA_TO_COVERAGE 0x809E |
| 4587 | #define GL_RGBA16F 0x881A |
| 4588 | #define GL_CONSTANT_ALPHA 0x8003 |
| 4589 | #define GL_READ_FRAMEBUFFER 0x8CA8 |
| 4590 | #define GL_TEXTURE0 0x84C0 |
| 4591 | #define GL_TEXTURE_MIN_LOD 0x813A |
| 4592 | #define GL_CLAMP_TO_EDGE 0x812F |
| 4593 | #define GL_UNSIGNED_SHORT_5_6_5 0x8363 |
| 4594 | #define GL_TEXTURE_WRAP_R 0x8072 |
| 4595 | #define GL_UNSIGNED_SHORT_5_5_5_1 0x8034 |
| 4596 | #define GL_NEAREST_MIPMAP_NEAREST 0x2700 |
| 4597 | #define GL_UNSIGNED_SHORT_4_4_4_4 0x8033 |
| 4598 | #define GL_SRC_ALPHA_SATURATE 0x0308 |
| 4599 | #define GL_STREAM_DRAW 0x88E0 |
| 4600 | #define GL_ONE 1 |
| 4601 | #define GL_NEAREST_MIPMAP_LINEAR 0x2702 |
| 4602 | #define GL_RGB10_A2 0x8059 |
| 4603 | #define GL_RGBA8 0x8058 |
| 4604 | #define GL_SRGB8_ALPHA8 0x8C43 |
| 4605 | #define GL_COLOR_ATTACHMENT1 0x8CE1 |
| 4606 | #define GL_RGBA4 0x8056 |
| 4607 | #define GL_RGB8 0x8051 |
| 4608 | #define GL_ARRAY_BUFFER 0x8892 |
| 4609 | #define GL_STENCIL 0x1802 |
| 4610 | #define GL_TEXTURE_2D 0x0DE1 |
| 4611 | #define GL_DEPTH 0x1801 |
| 4612 | #define GL_FRONT 0x0404 |
| 4613 | #define GL_STENCIL_BUFFER_BIT 0x00000400 |
| 4614 | #define GL_REPEAT 0x2901 |
| 4615 | #define GL_RGBA 0x1908 |
| 4616 | #define GL_TEXTURE_CUBE_MAP_POSITIVE_X 0x8515 |
| 4617 | #define GL_DECR 0x1E03 |
| 4618 | #define GL_FRAGMENT_SHADER 0x8B30 |
| 4619 | #define GL_FLOAT 0x1406 |
| 4620 | #define GL_TEXTURE_MAX_LOD 0x813B |
| 4621 | #define GL_DEPTH_COMPONENT 0x1902 |
| 4622 | #define GL_ONE_MINUS_DST_ALPHA 0x0305 |
| 4623 | #define GL_COLOR 0x1800 |
| 4624 | #define GL_TEXTURE_2D_ARRAY 0x8C1A |
| 4625 | #define GL_TRIANGLES 0x0004 |
| 4626 | #define GL_UNSIGNED_BYTE 0x1401 |
| 4627 | #define GL_TEXTURE_MAG_FILTER 0x2800 |
| 4628 | #define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004 |
| 4629 | #define GL_NONE 0 |
| 4630 | #define GL_SRC_COLOR 0x0300 |
| 4631 | #define GL_BYTE 0x1400 |
| 4632 | #define GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 0x851A |
| 4633 | #define GL_LINE_STRIP 0x0003 |
| 4634 | #define GL_TEXTURE_3D 0x806F |
| 4635 | #define GL_CW 0x0900 |
| 4636 | #define GL_LINEAR 0x2601 |
| 4637 | #define GL_RENDERBUFFER 0x8D41 |
| 4638 | #define GL_GEQUAL 0x0206 |
| 4639 | #define GL_COLOR_BUFFER_BIT 0x00004000 |
| 4640 | #define GL_RGBA32F 0x8814 |
| 4641 | #define GL_BLEND 0x0BE2 |
| 4642 | #define GL_ONE_MINUS_SRC_ALPHA 0x0303 |
| 4643 | #define GL_ONE_MINUS_CONSTANT_COLOR 0x8002 |
| 4644 | #define GL_TEXTURE_WRAP_T 0x2803 |
| 4645 | #define GL_TEXTURE_WRAP_S 0x2802 |
| 4646 | #define GL_TEXTURE_MIN_FILTER 0x2801 |
| 4647 | #define GL_LINEAR_MIPMAP_NEAREST 0x2701 |
| 4648 | #define GL_EXTENSIONS 0x1F03 |
| 4649 | #define GL_NO_ERROR 0 |
| 4650 | #define GL_REPLACE 0x1E01 |
| 4651 | #define GL_KEEP 0x1E00 |
| 4652 | #define GL_CCW 0x0901 |
| 4653 | #define GL_TEXTURE_CUBE_MAP_NEGATIVE_X 0x8516 |
| 4654 | #define GL_RGB 0x1907 |
| 4655 | #define GL_TRIANGLE_STRIP 0x0005 |
| 4656 | #define GL_FALSE 0 |
| 4657 | #define GL_ZERO 0 |
| 4658 | #define GL_CULL_FACE 0x0B44 |
| 4659 | #define GL_INVERT 0x150A |
| 4660 | #define GL_INT 0x1404 |
| 4661 | #define GL_UNSIGNED_INT 0x1405 |
| 4662 | #define GL_UNSIGNED_SHORT 0x1403 |
| 4663 | #define GL_NEAREST 0x2600 |
| 4664 | #define GL_SCISSOR_TEST 0x0C11 |
| 4665 | #define GL_LEQUAL 0x0203 |
| 4666 | #define GL_STENCIL_TEST 0x0B90 |
| 4667 | #define GL_DITHER 0x0BD0 |
| 4668 | #define GL_DEPTH_COMPONENT32F 0x8CAC |
| 4669 | #define GL_EQUAL 0x0202 |
| 4670 | #define GL_FRAMEBUFFER 0x8D40 |
| 4671 | #define GL_RGB5 0x8050 |
| 4672 | #define GL_LINES 0x0001 |
| 4673 | #define GL_DEPTH_BUFFER_BIT 0x00000100 |
| 4674 | #define GL_SRC_ALPHA 0x0302 |
| 4675 | #define GL_INCR_WRAP 0x8507 |
| 4676 | #define GL_LESS 0x0201 |
| 4677 | #define GL_MULTISAMPLE 0x809D |
| 4678 | #define GL_FRAMEBUFFER_BINDING 0x8CA6 |
| 4679 | #define GL_BACK 0x0405 |
| 4680 | #define GL_ALWAYS 0x0207 |
| 4681 | #define GL_FUNC_ADD 0x8006 |
| 4682 | #define GL_ONE_MINUS_DST_COLOR 0x0307 |
| 4683 | #define GL_NOTEQUAL 0x0205 |
| 4684 | #define GL_DST_COLOR 0x0306 |
| 4685 | #define GL_COMPILE_STATUS 0x8B81 |
| 4686 | #define GL_RED 0x1903 |
| 4687 | #define GL_COLOR_ATTACHMENT3 0x8CE3 |
| 4688 | #define GL_DST_ALPHA 0x0304 |
| 4689 | #define GL_RGB5_A1 0x8057 |
| 4690 | #define GL_GREATER 0x0204 |
| 4691 | #define GL_POLYGON_OFFSET_FILL 0x8037 |
| 4692 | #define GL_TRUE 1 |
| 4693 | #define GL_NEVER 0x0200 |
| 4694 | #define GL_POINTS 0x0000 |
| 4695 | #define GL_ONE_MINUS_SRC_COLOR 0x0301 |
| 4696 | #define GL_MIRRORED_REPEAT 0x8370 |
| 4697 | #define GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS 0x8B4D |
| 4698 | #define GL_R11F_G11F_B10F 0x8C3A |
| 4699 | #define GL_UNSIGNED_INT_10F_11F_11F_REV 0x8C3B |
| 4700 | #define GL_RGB9_E5 0x8C3D |
| 4701 | #define GL_UNSIGNED_INT_5_9_9_9_REV 0x8C3E |
| 4702 | #define GL_RGBA32UI 0x8D70 |
| 4703 | #define GL_RGB32UI 0x8D71 |
| 4704 | #define GL_RGBA16UI 0x8D76 |
| 4705 | #define GL_RGB16UI 0x8D77 |
| 4706 | #define GL_RGBA8UI 0x8D7C |
| 4707 | #define GL_RGB8UI 0x8D7D |
| 4708 | #define GL_RGBA32I 0x8D82 |
| 4709 | #define GL_RGB32I 0x8D83 |
| 4710 | #define GL_RGBA16I 0x8D88 |
| 4711 | #define GL_RGB16I 0x8D89 |
| 4712 | #define GL_RGBA8I 0x8D8E |
| 4713 | #define GL_RGB8I 0x8D8F |
| 4714 | #define GL_RED_INTEGER 0x8D94 |
| 4715 | #define GL_RG 0x8227 |
| 4716 | #define GL_RG_INTEGER 0x8228 |
| 4717 | #define GL_R8 0x8229 |
| 4718 | #define GL_R16 0x822A |
| 4719 | #define GL_RG8 0x822B |
| 4720 | #define GL_RG16 0x822C |
| 4721 | #define GL_R16F 0x822D |
| 4722 | #define GL_R32F 0x822E |
| 4723 | #define GL_RG16F 0x822F |
| 4724 | #define GL_RG32F 0x8230 |
| 4725 | #define GL_R8I 0x8231 |
| 4726 | #define GL_R8UI 0x8232 |
| 4727 | #define GL_R16I 0x8233 |
| 4728 | #define GL_R16UI 0x8234 |
| 4729 | #define GL_R32I 0x8235 |
| 4730 | #define GL_R32UI 0x8236 |
| 4731 | #define GL_RG8I 0x8237 |
| 4732 | #define GL_RG8UI 0x8238 |
| 4733 | #define GL_RG16I 0x8239 |
| 4734 | #define GL_RG16UI 0x823A |
| 4735 | #define GL_RG32I 0x823B |
| 4736 | #define GL_RG32UI 0x823C |
| 4737 | #define GL_RGBA_INTEGER 0x8D99 |
| 4738 | #define GL_R8_SNORM 0x8F94 |
| 4739 | #define GL_RG8_SNORM 0x8F95 |
| 4740 | #define GL_RGB8_SNORM 0x8F96 |
| 4741 | #define GL_RGBA8_SNORM 0x8F97 |
| 4742 | #define GL_R16_SNORM 0x8F98 |
| 4743 | #define GL_RG16_SNORM 0x8F99 |
| 4744 | #define GL_RGB16_SNORM 0x8F9A |
| 4745 | #define GL_RGBA16_SNORM 0x8F9B |
| 4746 | #define GL_RGBA16 0x805B |
| 4747 | #define GL_MAX_TEXTURE_SIZE 0x0D33 |
| 4748 | #define GL_MAX_CUBE_MAP_TEXTURE_SIZE 0x851C |
| 4749 | #define GL_MAX_3D_TEXTURE_SIZE 0x8073 |
| 4750 | #define GL_MAX_ARRAY_TEXTURE_LAYERS 0x88FF |
| 4751 | #define GL_MAX_VERTEX_ATTRIBS 0x8869 |
| 4752 | #define GL_CLAMP_TO_BORDER 0x812D |
| 4753 | #define GL_TEXTURE_BORDER_COLOR 0x1004 |
| 4754 | #define GL_CURRENT_PROGRAM 0x8B8D |
| 4755 | #define GL_MAX_VERTEX_UNIFORM_COMPONENTS 0x8B4A |
| 4756 | #define GL_UNPACK_ALIGNMENT 0x0CF5 |
| 4757 | #define GL_FRAMEBUFFER_SRGB 0x8DB9 |
| 4758 | #define GL_TEXTURE_COMPARE_MODE 0x884C |
| 4759 | #define GL_TEXTURE_COMPARE_FUNC 0x884D |
| 4760 | #define GL_COMPARE_REF_TO_TEXTURE 0x884E |
| 4761 | #define GL_TEXTURE_CUBE_MAP_SEAMLESS 0x884F |
| 4762 | #define GL_TEXTURE_MAX_LEVEL 0x813D |
| 4763 | #define GL_FRAMEBUFFER_UNDEFINED 0x8219 |
| 4764 | #define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT 0x8CD6 |
| 4765 | #define GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT 0x8CD7 |
| 4766 | #define GL_FRAMEBUFFER_UNSUPPORTED 0x8CDD |
| 4767 | #define GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE 0x8D56 |
| 4768 | #define GL_MAJOR_VERSION 0x821B |
| 4769 | #define GL_MINOR_VERSION 0x821C |
| 4770 | #endif |
| 4771 | |
| 4772 | #ifndef GL_UNSIGNED_INT_2_10_10_10_REV |
| 4773 | #define GL_UNSIGNED_INT_2_10_10_10_REV 0x8368 |
| 4774 | #endif |
| 4775 | #ifndef GL_UNSIGNED_INT_24_8 |
| 4776 | #define GL_UNSIGNED_INT_24_8 0x84FA |
| 4777 | #endif |
| 4778 | #ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT |
| 4779 | #define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE |
| 4780 | #endif |
| 4781 | #ifndef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT |
| 4782 | #define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF |
| 4783 | #endif |
| 4784 | #ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT |
| 4785 | #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1 |
| 4786 | #endif |
| 4787 | #ifndef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT |
| 4788 | #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2 |
| 4789 | #endif |
| 4790 | #ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT |
| 4791 | #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3 |
| 4792 | #endif |
| 4793 | #ifndef GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT |
| 4794 | #define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F |
| 4795 | #endif |
| 4796 | #ifndef GL_COMPRESSED_RED_RGTC1 |
| 4797 | #define GL_COMPRESSED_RED_RGTC1 0x8DBB |
| 4798 | #endif |
| 4799 | #ifndef GL_COMPRESSED_SIGNED_RED_RGTC1 |
| 4800 | #define GL_COMPRESSED_SIGNED_RED_RGTC1 0x8DBC |
| 4801 | #endif |
| 4802 | #ifndef GL_COMPRESSED_RED_GREEN_RGTC2 |
| 4803 | #define GL_COMPRESSED_RED_GREEN_RGTC2 0x8DBD |
| 4804 | #endif |
| 4805 | #ifndef GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2 |
| 4806 | #define GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2 0x8DBE |
| 4807 | #endif |
| 4808 | #ifndef GL_COMPRESSED_RGBA_BPTC_UNORM_ARB |
| 4809 | #define GL_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C |
| 4810 | #endif |
| 4811 | #ifndef GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB |
| 4812 | #define GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D |
| 4813 | #endif |
| 4814 | #ifndef GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB |
| 4815 | #define GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E |
| 4816 | #endif |
| 4817 | #ifndef GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB |
| 4818 | #define GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F |
| 4819 | #endif |
| 4820 | #ifndef GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG |
| 4821 | #define GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01 |
| 4822 | #endif |
| 4823 | #ifndef GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG |
| 4824 | #define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00 |
| 4825 | #endif |
| 4826 | #ifndef GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG |
| 4827 | #define GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03 |
| 4828 | #endif |
| 4829 | #ifndef GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG |
| 4830 | #define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02 |
| 4831 | #endif |
| 4832 | #ifndef GL_COMPRESSED_RGB8_ETC2 |
| 4833 | #define GL_COMPRESSED_RGB8_ETC2 0x9274 |
| 4834 | #endif |
| 4835 | #ifndef GL_COMPRESSED_SRGB8_ETC2 |
| 4836 | #define GL_COMPRESSED_SRGB8_ETC2 0x9275 |
| 4837 | #endif |
| 4838 | #ifndef GL_COMPRESSED_RGBA8_ETC2_EAC |
| 4839 | #define GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278 |
| 4840 | #endif |
| 4841 | #ifndef GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC |
| 4842 | #define GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279 |
| 4843 | #endif |
| 4844 | #ifndef GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 |
| 4845 | #define GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276 |
| 4846 | #endif |
| 4847 | #ifndef GL_COMPRESSED_RG11_EAC |
| 4848 | #define GL_COMPRESSED_RG11_EAC 0x9272 |
| 4849 | #endif |
| 4850 | #ifndef GL_COMPRESSED_SIGNED_RG11_EAC |
| 4851 | #define GL_COMPRESSED_SIGNED_RG11_EAC 0x9273 |
| 4852 | #endif |
| 4853 | #ifndef GL_COMPRESSED_RGBA_ASTC_4x4_KHR |
| 4854 | #define GL_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B0 |
| 4855 | #endif |
| 4856 | #ifndef GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR |
| 4857 | #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0 |
| 4858 | #endif |
| 4859 | #ifndef GL_DEPTH24_STENCIL8 |
| 4860 | #define GL_DEPTH24_STENCIL8 0x88F0 |
| 4861 | #endif |
| 4862 | #ifndef GL_HALF_FLOAT |
| 4863 | #define GL_HALF_FLOAT 0x140B |
| 4864 | #endif |
| 4865 | #ifndef GL_DEPTH_STENCIL |
| 4866 | #define GL_DEPTH_STENCIL 0x84F9 |
| 4867 | #endif |
| 4868 | #ifndef GL_LUMINANCE |
| 4869 | #define GL_LUMINANCE 0x1909 |
| 4870 | #endif |
| 4871 | #ifndef _SG_GL_CHECK_ERROR |
| 4872 | #define _SG_GL_CHECK_ERROR() { SOKOL_ASSERT(glGetError() == GL_NO_ERROR); } |
| 4873 | #endif |
| 4874 | #endif |
| 4875 | |
| 4876 | #if defined(SOKOL_GLES3) |
| 4877 | // on WebGL2, GL_FRAMEBUFFER_UNDEFINED technically doesn't exist (it is defined |
| 4878 | // in the Emscripten headers, but may not exist in other WebGL2 shims) |
| 4879 | // see: https://github.com/floooh/sokol/pull/933 |
| 4880 | #ifndef GL_FRAMEBUFFER_UNDEFINED |
| 4881 | #define GL_FRAMEBUFFER_UNDEFINED 0x8219 |
| 4882 | #endif |
| 4883 | #endif |
| 4884 | |
| 4885 | // make some GL constants generally available to simplify compilation, |
| 4886 | // use of those constants will be filtered by runtime flags |
| 4887 | #ifndef GL_SHADER_STORAGE_BUFFER |
| 4888 | #define GL_SHADER_STORAGE_BUFFER 0x90D2 |
| 4889 | #endif |
| 4890 | |
| 4891 | // ███████ ████████ ██████ ██ ██ ██████ ████████ ███████ |
| 4892 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 4893 | // ███████ ██ ██████ ██ ██ ██ ██ ███████ |
| 4894 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 4895 | // ███████ ██ ██ ██ ██████ ██████ ██ ███████ |
| 4896 | // |
| 4897 | // >>structs |
| 4898 | // resource pool slots |
| 4899 | typedef struct { |
| 4900 | uint32_t id; |
| 4901 | sg_resource_state state; |
| 4902 | } _sg_slot_t; |
| 4903 | |
| 4904 | // resource pool housekeeping struct |
| 4905 | typedef struct { |
| 4906 | int size; |
| 4907 | int queue_top; |
| 4908 | uint32_t* gen_ctrs; |
| 4909 | int* free_queue; |
| 4910 | } _sg_pool_t; |
| 4911 | |
| 4912 | _SOKOL_PRIVATE void _sg_init_pool(_sg_pool_t* pool, int num); |
| 4913 | _SOKOL_PRIVATE void _sg_discard_pool(_sg_pool_t* pool); |
| 4914 | _SOKOL_PRIVATE int _sg_pool_alloc_index(_sg_pool_t* pool); |
| 4915 | _SOKOL_PRIVATE void _sg_pool_free_index(_sg_pool_t* pool, int slot_index); |
| 4916 | _SOKOL_PRIVATE void _sg_reset_slot(_sg_slot_t* slot); |
| 4917 | _SOKOL_PRIVATE uint32_t _sg_slot_alloc(_sg_pool_t* pool, _sg_slot_t* slot, int slot_index); |
| 4918 | _SOKOL_PRIVATE int _sg_slot_index(uint32_t id); |
| 4919 | |
| 4920 | // constants |
| 4921 | enum { |
| 4922 | _SG_STRING_SIZE = 32, |
| 4923 | _SG_SLOT_SHIFT = 16, |
| 4924 | _SG_SLOT_MASK = (1<<_SG_SLOT_SHIFT)-1, |
| 4925 | _SG_MAX_POOL_SIZE = (1<<_SG_SLOT_SHIFT), |
| 4926 | _SG_DEFAULT_BUFFER_POOL_SIZE = 128, |
| 4927 | _SG_DEFAULT_IMAGE_POOL_SIZE = 128, |
| 4928 | _SG_DEFAULT_SAMPLER_POOL_SIZE = 64, |
| 4929 | _SG_DEFAULT_SHADER_POOL_SIZE = 32, |
| 4930 | _SG_DEFAULT_PIPELINE_POOL_SIZE = 64, |
| 4931 | _SG_DEFAULT_ATTACHMENTS_POOL_SIZE = 16, |
| 4932 | _SG_DEFAULT_UB_SIZE = 4 * 1024 * 1024, |
| 4933 | _SG_DEFAULT_MAX_COMMIT_LISTENERS = 1024, |
| 4934 | _SG_DEFAULT_WGPU_BINDGROUP_CACHE_SIZE = 1024, |
| 4935 | }; |
| 4936 | |
| 4937 | // fixed-size string |
| 4938 | typedef struct { |
| 4939 | char buf[_SG_STRING_SIZE]; |
| 4940 | } _sg_str_t; |
| 4941 | |
| 4942 | // helper macros |
| 4943 | #define _sg_def(val, def) (((val) == 0) ? (def) : (val)) |
| 4944 | #define _sg_def_flt(val, def) (((val) == 0.0f) ? (def) : (val)) |
| 4945 | #define _sg_min(a,b) (((a)<(b))?(a):(b)) |
| 4946 | #define _sg_max(a,b) (((a)>(b))?(a):(b)) |
| 4947 | #define _sg_clamp(v,v0,v1) (((v)<(v0))?(v0):(((v)>(v1))?(v1):(v))) |
| 4948 | #define _sg_fequal(val,cmp,delta) ((((val)-(cmp))> -(delta))&&(((val)-(cmp))<(delta))) |
| 4949 | #define _sg_ispow2(val) ((val&(val-1))==0) |
| 4950 | #define _sg_stats_add(key,val) {if(_sg.stats_enabled){ _sg.stats.key+=val;}} |
| 4951 | |
| 4952 | _SOKOL_PRIVATE void* _sg_malloc_clear(size_t size); |
| 4953 | _SOKOL_PRIVATE void _sg_free(void* ptr); |
| 4954 | _SOKOL_PRIVATE void _sg_clear(void* ptr, size_t size); |
| 4955 | |
| 4956 | typedef struct { |
| 4957 | int size; |
| 4958 | int append_pos; |
| 4959 | bool append_overflow; |
| 4960 | uint32_t update_frame_index; |
| 4961 | uint32_t append_frame_index; |
| 4962 | int num_slots; |
| 4963 | int active_slot; |
| 4964 | sg_buffer_type type; |
| 4965 | sg_usage usage; |
| 4966 | } _sg_buffer_common_t; |
| 4967 | |
| 4968 | _SOKOL_PRIVATE void _sg_buffer_common_init(_sg_buffer_common_t* cmn, const sg_buffer_desc* desc) { |
| 4969 | cmn->size = (int)desc->size; |
| 4970 | cmn->append_pos = 0; |
| 4971 | cmn->append_overflow = false; |
| 4972 | cmn->update_frame_index = 0; |
| 4973 | cmn->append_frame_index = 0; |
| 4974 | cmn->num_slots = (desc->usage == SG_USAGE_IMMUTABLE) ? 1 : SG_NUM_INFLIGHT_FRAMES; |
| 4975 | cmn->active_slot = 0; |
| 4976 | cmn->type = desc->type; |
| 4977 | cmn->usage = desc->usage; |
| 4978 | } |
| 4979 | |
| 4980 | typedef struct { |
| 4981 | uint32_t upd_frame_index; |
| 4982 | int num_slots; |
| 4983 | int active_slot; |
| 4984 | sg_image_type type; |
| 4985 | bool render_target; |
| 4986 | int width; |
| 4987 | int height; |
| 4988 | int num_slices; |
| 4989 | int num_mipmaps; |
| 4990 | sg_usage usage; |
| 4991 | sg_pixel_format pixel_format; |
| 4992 | int sample_count; |
| 4993 | } _sg_image_common_t; |
| 4994 | |
| 4995 | _SOKOL_PRIVATE void _sg_image_common_init(_sg_image_common_t* cmn, const sg_image_desc* desc) { |
| 4996 | cmn->upd_frame_index = 0; |
| 4997 | cmn->num_slots = (desc->usage == SG_USAGE_IMMUTABLE) ? 1 : SG_NUM_INFLIGHT_FRAMES; |
| 4998 | cmn->active_slot = 0; |
| 4999 | cmn->type = desc->type; |
| 5000 | cmn->render_target = desc->render_target; |
| 5001 | cmn->width = desc->width; |
| 5002 | cmn->height = desc->height; |
| 5003 | cmn->num_slices = desc->num_slices; |
| 5004 | cmn->num_mipmaps = desc->num_mipmaps; |
| 5005 | cmn->usage = desc->usage; |
| 5006 | cmn->pixel_format = desc->pixel_format; |
| 5007 | cmn->sample_count = desc->sample_count; |
| 5008 | } |
| 5009 | |
| 5010 | typedef struct { |
| 5011 | sg_filter min_filter; |
| 5012 | sg_filter mag_filter; |
| 5013 | sg_filter mipmap_filter; |
| 5014 | sg_wrap wrap_u; |
| 5015 | sg_wrap wrap_v; |
| 5016 | sg_wrap wrap_w; |
| 5017 | float min_lod; |
| 5018 | float max_lod; |
| 5019 | sg_border_color border_color; |
| 5020 | sg_compare_func compare; |
| 5021 | uint32_t max_anisotropy; |
| 5022 | } _sg_sampler_common_t; |
| 5023 | |
| 5024 | _SOKOL_PRIVATE void _sg_sampler_common_init(_sg_sampler_common_t* cmn, const sg_sampler_desc* desc) { |
| 5025 | cmn->min_filter = desc->min_filter; |
| 5026 | cmn->mag_filter = desc->mag_filter; |
| 5027 | cmn->mipmap_filter = desc->mipmap_filter; |
| 5028 | cmn->wrap_u = desc->wrap_u; |
| 5029 | cmn->wrap_v = desc->wrap_v; |
| 5030 | cmn->wrap_w = desc->wrap_w; |
| 5031 | cmn->min_lod = desc->min_lod; |
| 5032 | cmn->max_lod = desc->max_lod; |
| 5033 | cmn->border_color = desc->border_color; |
| 5034 | cmn->compare = desc->compare; |
| 5035 | cmn->max_anisotropy = desc->max_anisotropy; |
| 5036 | } |
| 5037 | |
| 5038 | typedef struct { |
| 5039 | size_t size; |
| 5040 | } _sg_shader_uniform_block_t; |
| 5041 | |
| 5042 | typedef struct { |
| 5043 | bool used; |
| 5044 | bool readonly; |
| 5045 | } _sg_shader_storage_buffer_t; |
| 5046 | |
| 5047 | typedef struct { |
| 5048 | sg_image_type image_type; |
| 5049 | sg_image_sample_type sample_type; |
| 5050 | bool multisampled; |
| 5051 | } _sg_shader_image_t; |
| 5052 | |
| 5053 | typedef struct { |
| 5054 | sg_sampler_type sampler_type; |
| 5055 | } _sg_shader_sampler_t; |
| 5056 | |
| 5057 | // combined image sampler mappings, only needed on GL |
| 5058 | typedef struct { |
| 5059 | int image_slot; |
| 5060 | int sampler_slot; |
| 5061 | } _sg_shader_image_sampler_t; |
| 5062 | |
| 5063 | typedef struct { |
| 5064 | int num_uniform_blocks; |
| 5065 | int num_storage_buffers; |
| 5066 | int num_images; |
| 5067 | int num_samplers; |
| 5068 | int num_image_samplers; |
| 5069 | _sg_shader_uniform_block_t uniform_blocks[SG_MAX_SHADERSTAGE_UBS]; |
| 5070 | _sg_shader_storage_buffer_t storage_buffers[SG_MAX_SHADERSTAGE_STORAGEBUFFERS]; |
| 5071 | _sg_shader_image_t images[SG_MAX_SHADERSTAGE_IMAGES]; |
| 5072 | _sg_shader_sampler_t samplers[SG_MAX_SHADERSTAGE_SAMPLERS]; |
| 5073 | _sg_shader_image_sampler_t image_samplers[SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS]; |
| 5074 | } _sg_shader_stage_t; |
| 5075 | |
| 5076 | typedef struct { |
| 5077 | _sg_shader_stage_t stage[SG_NUM_SHADER_STAGES]; |
| 5078 | } _sg_shader_common_t; |
| 5079 | |
| 5080 | _SOKOL_PRIVATE void _sg_shader_common_init(_sg_shader_common_t* cmn, const sg_shader_desc* desc) { |
| 5081 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 5082 | const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS) ? &desc->vs : &desc->fs; |
| 5083 | _sg_shader_stage_t* stage = &cmn->stage[stage_index]; |
| 5084 | SOKOL_ASSERT(stage->num_uniform_blocks == 0); |
| 5085 | for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) { |
| 5086 | const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index]; |
| 5087 | if (0 == ub_desc->size) { |
| 5088 | break; |
| 5089 | } |
| 5090 | stage->uniform_blocks[ub_index].size = ub_desc->size; |
| 5091 | stage->num_uniform_blocks++; |
| 5092 | } |
| 5093 | SOKOL_ASSERT(stage->num_images == 0); |
| 5094 | for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) { |
| 5095 | const sg_shader_image_desc* img_desc = &stage_desc->images[img_index]; |
| 5096 | if (!img_desc->used) { |
| 5097 | break; |
| 5098 | } |
| 5099 | stage->images[img_index].multisampled = img_desc->multisampled; |
| 5100 | stage->images[img_index].image_type = img_desc->image_type; |
| 5101 | stage->images[img_index].sample_type = img_desc->sample_type; |
| 5102 | stage->num_images++; |
| 5103 | } |
| 5104 | SOKOL_ASSERT(stage->num_samplers == 0); |
| 5105 | for (int smp_index = 0; smp_index < SG_MAX_SHADERSTAGE_SAMPLERS; smp_index++) { |
| 5106 | const sg_shader_sampler_desc* smp_desc = &stage_desc->samplers[smp_index]; |
| 5107 | if (!smp_desc->used) { |
| 5108 | break; |
| 5109 | } |
| 5110 | stage->samplers[smp_index].sampler_type = smp_desc->sampler_type; |
| 5111 | stage->num_samplers++; |
| 5112 | } |
| 5113 | SOKOL_ASSERT(stage->num_image_samplers == 0); |
| 5114 | for (int img_smp_index = 0; img_smp_index < SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS; img_smp_index++) { |
| 5115 | const sg_shader_image_sampler_pair_desc* img_smp_desc = &stage_desc->image_sampler_pairs[img_smp_index]; |
| 5116 | if (!img_smp_desc->used) { |
| 5117 | break; |
| 5118 | } |
| 5119 | SOKOL_ASSERT((img_smp_desc->image_slot >= 0) && (img_smp_desc->image_slot < stage->num_images)); |
| 5120 | stage->image_samplers[img_smp_index].image_slot = img_smp_desc->image_slot; |
| 5121 | SOKOL_ASSERT((img_smp_desc->sampler_slot >= 0) && (img_smp_desc->sampler_slot < stage->num_samplers)); |
| 5122 | stage->image_samplers[img_smp_index].sampler_slot = img_smp_desc->sampler_slot; |
| 5123 | stage->num_image_samplers++; |
| 5124 | } |
| 5125 | SOKOL_ASSERT(stage->num_storage_buffers == 0); |
| 5126 | for (int sbuf_index = 0; sbuf_index < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; sbuf_index++) { |
| 5127 | const sg_shader_storage_buffer_desc* sbuf_desc = &stage_desc->storage_buffers[sbuf_index]; |
| 5128 | if (!sbuf_desc->used) { |
| 5129 | break; |
| 5130 | } |
| 5131 | stage->storage_buffers[sbuf_index].used = sbuf_desc->used; |
| 5132 | stage->storage_buffers[sbuf_index].readonly = sbuf_desc->readonly; |
| 5133 | stage->num_storage_buffers++; |
| 5134 | } |
| 5135 | } |
| 5136 | } |
| 5137 | |
| 5138 | typedef struct { |
| 5139 | bool vertex_buffer_layout_active[SG_MAX_VERTEX_BUFFERS]; |
| 5140 | bool use_instanced_draw; |
| 5141 | sg_shader shader_id; |
| 5142 | sg_vertex_layout_state layout; |
| 5143 | sg_depth_state depth; |
| 5144 | sg_stencil_state stencil; |
| 5145 | int color_count; |
| 5146 | sg_color_target_state colors[SG_MAX_COLOR_ATTACHMENTS]; |
| 5147 | sg_primitive_type primitive_type; |
| 5148 | sg_index_type index_type; |
| 5149 | sg_cull_mode cull_mode; |
| 5150 | sg_face_winding face_winding; |
| 5151 | int sample_count; |
| 5152 | sg_color blend_color; |
| 5153 | bool alpha_to_coverage_enabled; |
| 5154 | } _sg_pipeline_common_t; |
| 5155 | |
| 5156 | _SOKOL_PRIVATE void _sg_pipeline_common_init(_sg_pipeline_common_t* cmn, const sg_pipeline_desc* desc) { |
| 5157 | SOKOL_ASSERT((desc->color_count >= 0) && (desc->color_count <= SG_MAX_COLOR_ATTACHMENTS)); |
| 5158 | for (int i = 0; i < SG_MAX_VERTEX_BUFFERS; i++) { |
| 5159 | cmn->vertex_buffer_layout_active[i] = false; |
| 5160 | } |
| 5161 | cmn->use_instanced_draw = false; |
| 5162 | cmn->shader_id = desc->shader; |
| 5163 | cmn->layout = desc->layout; |
| 5164 | cmn->depth = desc->depth; |
| 5165 | cmn->stencil = desc->stencil; |
| 5166 | cmn->color_count = desc->color_count; |
| 5167 | for (int i = 0; i < desc->color_count; i++) { |
| 5168 | cmn->colors[i] = desc->colors[i]; |
| 5169 | } |
| 5170 | cmn->primitive_type = desc->primitive_type; |
| 5171 | cmn->index_type = desc->index_type; |
| 5172 | cmn->cull_mode = desc->cull_mode; |
| 5173 | cmn->face_winding = desc->face_winding; |
| 5174 | cmn->sample_count = desc->sample_count; |
| 5175 | cmn->blend_color = desc->blend_color; |
| 5176 | cmn->alpha_to_coverage_enabled = desc->alpha_to_coverage_enabled; |
| 5177 | } |
| 5178 | |
| 5179 | typedef struct { |
| 5180 | sg_image image_id; |
| 5181 | int mip_level; |
| 5182 | int slice; |
| 5183 | } _sg_attachment_common_t; |
| 5184 | |
| 5185 | typedef struct { |
| 5186 | int width; |
| 5187 | int height; |
| 5188 | int num_colors; |
| 5189 | _sg_attachment_common_t colors[SG_MAX_COLOR_ATTACHMENTS]; |
| 5190 | _sg_attachment_common_t resolves[SG_MAX_COLOR_ATTACHMENTS]; |
| 5191 | _sg_attachment_common_t depth_stencil; |
| 5192 | } _sg_attachments_common_t; |
| 5193 | |
| 5194 | _SOKOL_PRIVATE void _sg_attachment_common_init(_sg_attachment_common_t* cmn, const sg_attachment_desc* desc) { |
| 5195 | cmn->image_id = desc->image; |
| 5196 | cmn->mip_level = desc->mip_level; |
| 5197 | cmn->slice = desc->slice; |
| 5198 | } |
| 5199 | |
| 5200 | _SOKOL_PRIVATE void _sg_attachments_common_init(_sg_attachments_common_t* cmn, const sg_attachments_desc* desc, int width, int height) { |
| 5201 | SOKOL_ASSERT((width > 0) && (height > 0)); |
| 5202 | cmn->width = width; |
| 5203 | cmn->height = height; |
| 5204 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 5205 | if (desc->colors[i].image.id != SG_INVALID_ID) { |
| 5206 | cmn->num_colors++; |
| 5207 | _sg_attachment_common_init(&cmn->colors[i], &desc->colors[i]); |
| 5208 | _sg_attachment_common_init(&cmn->resolves[i], &desc->resolves[i]); |
| 5209 | } |
| 5210 | } |
| 5211 | if (desc->depth_stencil.image.id != SG_INVALID_ID) { |
| 5212 | _sg_attachment_common_init(&cmn->depth_stencil, &desc->depth_stencil); |
| 5213 | } |
| 5214 | } |
| 5215 | |
| 5216 | #if defined(SOKOL_DUMMY_BACKEND) |
| 5217 | typedef struct { |
| 5218 | _sg_slot_t slot; |
| 5219 | _sg_buffer_common_t cmn; |
| 5220 | } _sg_dummy_buffer_t; |
| 5221 | typedef _sg_dummy_buffer_t _sg_buffer_t; |
| 5222 | |
| 5223 | typedef struct { |
| 5224 | _sg_slot_t slot; |
| 5225 | _sg_image_common_t cmn; |
| 5226 | } _sg_dummy_image_t; |
| 5227 | typedef _sg_dummy_image_t _sg_image_t; |
| 5228 | |
| 5229 | typedef struct { |
| 5230 | _sg_slot_t slot; |
| 5231 | _sg_sampler_common_t cmn; |
| 5232 | } _sg_dummy_sampler_t; |
| 5233 | typedef _sg_dummy_sampler_t _sg_sampler_t; |
| 5234 | |
| 5235 | typedef struct { |
| 5236 | _sg_slot_t slot; |
| 5237 | _sg_shader_common_t cmn; |
| 5238 | } _sg_dummy_shader_t; |
| 5239 | typedef _sg_dummy_shader_t _sg_shader_t; |
| 5240 | |
| 5241 | typedef struct { |
| 5242 | _sg_slot_t slot; |
| 5243 | _sg_shader_t* shader; |
| 5244 | _sg_pipeline_common_t cmn; |
| 5245 | } _sg_dummy_pipeline_t; |
| 5246 | typedef _sg_dummy_pipeline_t _sg_pipeline_t; |
| 5247 | |
| 5248 | typedef struct { |
| 5249 | _sg_image_t* image; |
| 5250 | } _sg_dummy_attachment_t; |
| 5251 | |
| 5252 | typedef struct { |
| 5253 | _sg_slot_t slot; |
| 5254 | _sg_attachments_common_t cmn; |
| 5255 | struct { |
| 5256 | _sg_dummy_attachment_t colors[SG_MAX_COLOR_ATTACHMENTS]; |
| 5257 | _sg_dummy_attachment_t resolves[SG_MAX_COLOR_ATTACHMENTS]; |
| 5258 | _sg_dummy_attachment_t depth_stencil; |
| 5259 | } dmy; |
| 5260 | } _sg_dummy_attachments_t; |
| 5261 | typedef _sg_dummy_attachments_t _sg_attachments_t; |
| 5262 | |
| 5263 | #elif defined(_SOKOL_ANY_GL) |
| 5264 | |
| 5265 | #define _SG_GL_TEXTURE_SAMPLER_CACHE_SIZE (SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS * SG_NUM_SHADER_STAGES) |
| 5266 | #define _SG_GL_STORAGEBUFFER_STAGE_INDEX_PITCH (SG_MAX_SHADERSTAGE_STORAGEBUFFERS) |
| 5267 | |
| 5268 | typedef struct { |
| 5269 | _sg_slot_t slot; |
| 5270 | _sg_buffer_common_t cmn; |
| 5271 | struct { |
| 5272 | GLuint buf[SG_NUM_INFLIGHT_FRAMES]; |
| 5273 | bool injected; // if true, external buffers were injected with sg_buffer_desc.gl_buffers |
| 5274 | } gl; |
| 5275 | } _sg_gl_buffer_t; |
| 5276 | typedef _sg_gl_buffer_t _sg_buffer_t; |
| 5277 | |
| 5278 | typedef struct { |
| 5279 | _sg_slot_t slot; |
| 5280 | _sg_image_common_t cmn; |
| 5281 | struct { |
| 5282 | GLenum target; |
| 5283 | GLuint msaa_render_buffer; |
| 5284 | GLuint tex[SG_NUM_INFLIGHT_FRAMES]; |
| 5285 | bool injected; // if true, external textures were injected with sg_image_desc.gl_textures |
| 5286 | } gl; |
| 5287 | } _sg_gl_image_t; |
| 5288 | typedef _sg_gl_image_t _sg_image_t; |
| 5289 | |
| 5290 | typedef struct { |
| 5291 | _sg_slot_t slot; |
| 5292 | _sg_sampler_common_t cmn; |
| 5293 | struct { |
| 5294 | GLuint smp; |
| 5295 | bool injected; // true if external sampler was injects in sg_sampler_desc.gl_sampler |
| 5296 | } gl; |
| 5297 | } _sg_gl_sampler_t; |
| 5298 | typedef _sg_gl_sampler_t _sg_sampler_t; |
| 5299 | |
| 5300 | typedef struct { |
| 5301 | GLint gl_loc; |
| 5302 | sg_uniform_type type; |
| 5303 | uint16_t count; |
| 5304 | uint16_t offset; |
| 5305 | } _sg_gl_uniform_t; |
| 5306 | |
| 5307 | typedef struct { |
| 5308 | int num_uniforms; |
| 5309 | _sg_gl_uniform_t uniforms[SG_MAX_UB_MEMBERS]; |
| 5310 | } _sg_gl_uniform_block_t; |
| 5311 | |
| 5312 | typedef struct { |
| 5313 | int gl_tex_slot; |
| 5314 | } _sg_gl_shader_image_sampler_t; |
| 5315 | |
| 5316 | typedef struct { |
| 5317 | _sg_str_t name; |
| 5318 | } _sg_gl_shader_attr_t; |
| 5319 | |
| 5320 | typedef struct { |
| 5321 | _sg_gl_uniform_block_t uniform_blocks[SG_MAX_SHADERSTAGE_UBS]; |
| 5322 | _sg_gl_shader_image_sampler_t image_samplers[SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS]; |
| 5323 | } _sg_gl_shader_stage_t; |
| 5324 | |
| 5325 | typedef struct { |
| 5326 | _sg_slot_t slot; |
| 5327 | _sg_shader_common_t cmn; |
| 5328 | struct { |
| 5329 | GLuint prog; |
| 5330 | _sg_gl_shader_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 5331 | _sg_gl_shader_stage_t stage[SG_NUM_SHADER_STAGES]; |
| 5332 | } gl; |
| 5333 | } _sg_gl_shader_t; |
| 5334 | typedef _sg_gl_shader_t _sg_shader_t; |
| 5335 | |
| 5336 | typedef struct { |
| 5337 | int8_t vb_index; // -1 if attr is not enabled |
| 5338 | int8_t divisor; // -1 if not initialized |
| 5339 | uint8_t stride; |
| 5340 | uint8_t size; |
| 5341 | uint8_t normalized; |
| 5342 | int offset; |
| 5343 | GLenum type; |
| 5344 | } _sg_gl_attr_t; |
| 5345 | |
| 5346 | typedef struct { |
| 5347 | _sg_slot_t slot; |
| 5348 | _sg_pipeline_common_t cmn; |
| 5349 | _sg_shader_t* shader; |
| 5350 | struct { |
| 5351 | _sg_gl_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 5352 | sg_depth_state depth; |
| 5353 | sg_stencil_state stencil; |
| 5354 | sg_primitive_type primitive_type; |
| 5355 | sg_blend_state blend; |
| 5356 | sg_color_mask color_write_mask[SG_MAX_COLOR_ATTACHMENTS]; |
| 5357 | sg_cull_mode cull_mode; |
| 5358 | sg_face_winding face_winding; |
| 5359 | int sample_count; |
| 5360 | bool alpha_to_coverage_enabled; |
| 5361 | } gl; |
| 5362 | } _sg_gl_pipeline_t; |
| 5363 | typedef _sg_gl_pipeline_t _sg_pipeline_t; |
| 5364 | |
| 5365 | typedef struct { |
| 5366 | _sg_image_t* image; |
| 5367 | } _sg_gl_attachment_t; |
| 5368 | |
| 5369 | typedef struct { |
| 5370 | _sg_slot_t slot; |
| 5371 | _sg_attachments_common_t cmn; |
| 5372 | struct { |
| 5373 | GLuint fb; |
| 5374 | _sg_gl_attachment_t colors[SG_MAX_COLOR_ATTACHMENTS]; |
| 5375 | _sg_gl_attachment_t resolves[SG_MAX_COLOR_ATTACHMENTS]; |
| 5376 | _sg_gl_attachment_t depth_stencil; |
| 5377 | GLuint msaa_resolve_framebuffer[SG_MAX_COLOR_ATTACHMENTS]; |
| 5378 | } gl; |
| 5379 | } _sg_gl_attachments_t; |
| 5380 | typedef _sg_gl_attachments_t _sg_attachments_t; |
| 5381 | |
| 5382 | typedef struct { |
| 5383 | _sg_gl_attr_t gl_attr; |
| 5384 | GLuint gl_vbuf; |
| 5385 | } _sg_gl_cache_attr_t; |
| 5386 | |
| 5387 | typedef struct { |
| 5388 | GLenum target; |
| 5389 | GLuint texture; |
| 5390 | GLuint sampler; |
| 5391 | } _sg_gl_cache_texture_sampler_bind_slot; |
| 5392 | |
| 5393 | typedef struct { |
| 5394 | sg_depth_state depth; |
| 5395 | sg_stencil_state stencil; |
| 5396 | sg_blend_state blend; |
| 5397 | sg_color_mask color_write_mask[SG_MAX_COLOR_ATTACHMENTS]; |
| 5398 | sg_cull_mode cull_mode; |
| 5399 | sg_face_winding face_winding; |
| 5400 | bool polygon_offset_enabled; |
| 5401 | int sample_count; |
| 5402 | sg_color blend_color; |
| 5403 | bool alpha_to_coverage_enabled; |
| 5404 | _sg_gl_cache_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 5405 | GLuint vertex_buffer; |
| 5406 | GLuint index_buffer; |
| 5407 | GLuint storage_buffer; // general bind point |
| 5408 | GLuint stage_storage_buffers[SG_NUM_SHADER_STAGES][SG_MAX_SHADERSTAGE_STORAGEBUFFERS]; |
| 5409 | GLuint stored_vertex_buffer; |
| 5410 | GLuint stored_index_buffer; |
| 5411 | GLuint stored_storage_buffer; |
| 5412 | GLuint prog; |
| 5413 | _sg_gl_cache_texture_sampler_bind_slot texture_samplers[_SG_GL_TEXTURE_SAMPLER_CACHE_SIZE]; |
| 5414 | _sg_gl_cache_texture_sampler_bind_slot stored_texture_sampler; |
| 5415 | int cur_ib_offset; |
| 5416 | GLenum cur_primitive_type; |
| 5417 | GLenum cur_index_type; |
| 5418 | GLenum cur_active_texture; |
| 5419 | _sg_pipeline_t* cur_pipeline; |
| 5420 | sg_pipeline cur_pipeline_id; |
| 5421 | } _sg_gl_state_cache_t; |
| 5422 | |
| 5423 | typedef struct { |
| 5424 | bool valid; |
| 5425 | GLuint vao; |
| 5426 | _sg_gl_state_cache_t cache; |
| 5427 | bool ext_anisotropic; |
| 5428 | GLint max_anisotropy; |
| 5429 | sg_store_action color_store_actions[SG_MAX_COLOR_ATTACHMENTS]; |
| 5430 | sg_store_action depth_store_action; |
| 5431 | sg_store_action stencil_store_action; |
| 5432 | #if _SOKOL_USE_WIN32_GL_LOADER |
| 5433 | HINSTANCE opengl32_dll; |
| 5434 | #endif |
| 5435 | } _sg_gl_backend_t; |
| 5436 | |
| 5437 | #elif defined(SOKOL_D3D11) |
| 5438 | |
| 5439 | typedef struct { |
| 5440 | _sg_slot_t slot; |
| 5441 | _sg_buffer_common_t cmn; |
| 5442 | struct { |
| 5443 | ID3D11Buffer* buf; |
| 5444 | ID3D11ShaderResourceView* srv; |
| 5445 | } d3d11; |
| 5446 | } _sg_d3d11_buffer_t; |
| 5447 | typedef _sg_d3d11_buffer_t _sg_buffer_t; |
| 5448 | |
| 5449 | typedef struct { |
| 5450 | _sg_slot_t slot; |
| 5451 | _sg_image_common_t cmn; |
| 5452 | struct { |
| 5453 | DXGI_FORMAT format; |
| 5454 | ID3D11Texture2D* tex2d; |
| 5455 | ID3D11Texture3D* tex3d; |
| 5456 | ID3D11Resource* res; // either tex2d or tex3d |
| 5457 | ID3D11ShaderResourceView* srv; |
| 5458 | } d3d11; |
| 5459 | } _sg_d3d11_image_t; |
| 5460 | typedef _sg_d3d11_image_t _sg_image_t; |
| 5461 | |
| 5462 | typedef struct { |
| 5463 | _sg_slot_t slot; |
| 5464 | _sg_sampler_common_t cmn; |
| 5465 | struct { |
| 5466 | ID3D11SamplerState* smp; |
| 5467 | } d3d11; |
| 5468 | } _sg_d3d11_sampler_t; |
| 5469 | typedef _sg_d3d11_sampler_t _sg_sampler_t; |
| 5470 | |
| 5471 | typedef struct { |
| 5472 | _sg_str_t sem_name; |
| 5473 | int sem_index; |
| 5474 | } _sg_d3d11_shader_attr_t; |
| 5475 | |
| 5476 | typedef struct { |
| 5477 | ID3D11Buffer* cbufs[SG_MAX_SHADERSTAGE_UBS]; |
| 5478 | } _sg_d3d11_shader_stage_t; |
| 5479 | |
| 5480 | typedef struct { |
| 5481 | _sg_slot_t slot; |
| 5482 | _sg_shader_common_t cmn; |
| 5483 | struct { |
| 5484 | _sg_d3d11_shader_attr_t attrs[SG_MAX_VERTEX_ATTRIBUTES]; |
| 5485 | _sg_d3d11_shader_stage_t stage[SG_NUM_SHADER_STAGES]; |
| 5486 | ID3D11VertexShader* vs; |
| 5487 | ID3D11PixelShader* fs; |
| 5488 | void* vs_blob; |
| 5489 | size_t vs_blob_length; |
| 5490 | } d3d11; |
| 5491 | } _sg_d3d11_shader_t; |
| 5492 | typedef _sg_d3d11_shader_t _sg_shader_t; |
| 5493 | |
| 5494 | typedef struct { |
| 5495 | _sg_slot_t slot; |
| 5496 | _sg_pipeline_common_t cmn; |
| 5497 | _sg_shader_t* shader; |
| 5498 | struct { |
| 5499 | UINT stencil_ref; |
| 5500 | UINT vb_strides[SG_MAX_VERTEX_BUFFERS]; |
| 5501 | D3D_PRIMITIVE_TOPOLOGY topology; |
| 5502 | DXGI_FORMAT index_format; |
| 5503 | ID3D11InputLayout* il; |
| 5504 | ID3D11RasterizerState* rs; |
| 5505 | ID3D11DepthStencilState* dss; |
| 5506 | ID3D11BlendState* bs; |
| 5507 | } d3d11; |
| 5508 | } _sg_d3d11_pipeline_t; |
| 5509 | typedef _sg_d3d11_pipeline_t _sg_pipeline_t; |
| 5510 | |
| 5511 | typedef struct { |
| 5512 | _sg_image_t* image; |
| 5513 | union { |
| 5514 | ID3D11RenderTargetView* rtv; |
| 5515 | ID3D11DepthStencilView* dsv; |
| 5516 | } view; |
| 5517 | } _sg_d3d11_attachment_t; |
| 5518 | |
| 5519 | typedef struct { |
| 5520 | _sg_slot_t slot; |
| 5521 | _sg_attachments_common_t cmn; |
| 5522 | struct { |
| 5523 | _sg_d3d11_attachment_t colors[SG_MAX_COLOR_ATTACHMENTS]; |
| 5524 | _sg_d3d11_attachment_t resolves[SG_MAX_COLOR_ATTACHMENTS]; |
| 5525 | _sg_d3d11_attachment_t depth_stencil; |
| 5526 | } d3d11; |
| 5527 | } _sg_d3d11_attachments_t; |
| 5528 | typedef _sg_d3d11_attachments_t _sg_attachments_t; |
| 5529 | |
| 5530 | typedef struct { |
| 5531 | bool valid; |
| 5532 | ID3D11Device* dev; |
| 5533 | ID3D11DeviceContext* ctx; |
| 5534 | bool use_indexed_draw; |
| 5535 | bool use_instanced_draw; |
| 5536 | _sg_pipeline_t* cur_pipeline; |
| 5537 | sg_pipeline cur_pipeline_id; |
| 5538 | struct { |
| 5539 | ID3D11RenderTargetView* render_view; |
| 5540 | ID3D11RenderTargetView* resolve_view; |
| 5541 | } cur_pass; |
| 5542 | // on-demand loaded d3dcompiler_47.dll handles |
| 5543 | HINSTANCE d3dcompiler_dll; |
| 5544 | bool d3dcompiler_dll_load_failed; |
| 5545 | pD3DCompile D3DCompile_func; |
| 5546 | // global subresourcedata array for texture updates |
| 5547 | D3D11_SUBRESOURCE_DATA subres_data[SG_MAX_MIPMAPS * SG_MAX_TEXTUREARRAY_LAYERS]; |
| 5548 | } _sg_d3d11_backend_t; |
| 5549 | |
| 5550 | #elif defined(SOKOL_METAL) |
| 5551 | |
| 5552 | #if defined(_SG_TARGET_MACOS) || defined(_SG_TARGET_IOS_SIMULATOR) |
| 5553 | #define _SG_MTL_UB_ALIGN (256) |
| 5554 | #else |
| 5555 | #define _SG_MTL_UB_ALIGN (16) |
| 5556 | #endif |
| 5557 | #define _SG_MTL_INVALID_SLOT_INDEX (0) |
| 5558 | |
| 5559 | typedef struct { |
| 5560 | uint32_t frame_index; // frame index at which it is safe to release this resource |
| 5561 | int slot_index; |
| 5562 | } _sg_mtl_release_item_t; |
| 5563 | |
| 5564 | typedef struct { |
| 5565 | NSMutableArray* pool; |
| 5566 | int num_slots; |
| 5567 | int free_queue_top; |
| 5568 | int* free_queue; |
| 5569 | int release_queue_front; |
| 5570 | int release_queue_back; |
| 5571 | _sg_mtl_release_item_t* release_queue; |
| 5572 | } _sg_mtl_idpool_t; |
| 5573 | |
| 5574 | typedef struct { |
| 5575 | _sg_slot_t slot; |
| 5576 | _sg_buffer_common_t cmn; |
| 5577 | struct { |
| 5578 | int buf[SG_NUM_INFLIGHT_FRAMES]; // index into _sg_mtl_pool |
| 5579 | } mtl; |
| 5580 | } _sg_mtl_buffer_t; |
| 5581 | typedef _sg_mtl_buffer_t _sg_buffer_t; |
| 5582 | |
| 5583 | typedef struct { |
| 5584 | _sg_slot_t slot; |
| 5585 | _sg_image_common_t cmn; |
| 5586 | struct { |
| 5587 | int tex[SG_NUM_INFLIGHT_FRAMES]; |
| 5588 | } mtl; |
| 5589 | } _sg_mtl_image_t; |
| 5590 | typedef _sg_mtl_image_t _sg_image_t; |
| 5591 | |
| 5592 | typedef struct { |
| 5593 | _sg_slot_t slot; |
| 5594 | _sg_sampler_common_t cmn; |
| 5595 | struct { |
| 5596 | int sampler_state; |
| 5597 | } mtl; |
| 5598 | } _sg_mtl_sampler_t; |
| 5599 | typedef _sg_mtl_sampler_t _sg_sampler_t; |
| 5600 | |
| 5601 | typedef struct { |
| 5602 | int mtl_lib; |
| 5603 | int mtl_func; |
| 5604 | } _sg_mtl_shader_stage_t; |
| 5605 | |
| 5606 | typedef struct { |
| 5607 | _sg_slot_t slot; |
| 5608 | _sg_shader_common_t cmn; |
| 5609 | struct { |
| 5610 | _sg_mtl_shader_stage_t stage[SG_NUM_SHADER_STAGES]; |
| 5611 | } mtl; |
| 5612 | } _sg_mtl_shader_t; |
| 5613 | typedef _sg_mtl_shader_t _sg_shader_t; |
| 5614 | |
| 5615 | typedef struct { |
| 5616 | _sg_slot_t slot; |
| 5617 | _sg_pipeline_common_t cmn; |
| 5618 | _sg_shader_t* shader; |
| 5619 | struct { |
| 5620 | MTLPrimitiveType prim_type; |
| 5621 | int index_size; |
| 5622 | MTLIndexType index_type; |
| 5623 | MTLCullMode cull_mode; |
| 5624 | MTLWinding winding; |
| 5625 | uint32_t stencil_ref; |
| 5626 | int rps; |
| 5627 | int dss; |
| 5628 | } mtl; |
| 5629 | } _sg_mtl_pipeline_t; |
| 5630 | typedef _sg_mtl_pipeline_t _sg_pipeline_t; |
| 5631 | |
| 5632 | typedef struct { |
| 5633 | _sg_image_t* image; |
| 5634 | } _sg_mtl_attachment_t; |
| 5635 | |
| 5636 | typedef struct { |
| 5637 | _sg_slot_t slot; |
| 5638 | _sg_attachments_common_t cmn; |
| 5639 | struct { |
| 5640 | _sg_mtl_attachment_t colors[SG_MAX_COLOR_ATTACHMENTS]; |
| 5641 | _sg_mtl_attachment_t resolves[SG_MAX_COLOR_ATTACHMENTS]; |
| 5642 | _sg_mtl_attachment_t depth_stencil; |
| 5643 | } mtl; |
| 5644 | } _sg_mtl_attachments_t; |
| 5645 | typedef _sg_mtl_attachments_t _sg_attachments_t; |
| 5646 | |
| 5647 | // resource binding state cache |
| 5648 | typedef struct { |
| 5649 | const _sg_pipeline_t* cur_pipeline; |
| 5650 | sg_pipeline cur_pipeline_id; |
| 5651 | const _sg_buffer_t* cur_indexbuffer; |
| 5652 | sg_buffer cur_indexbuffer_id; |
| 5653 | int cur_indexbuffer_offset; |
| 5654 | int cur_vertexbuffer_offsets[SG_MAX_VERTEX_BUFFERS]; |
| 5655 | sg_buffer cur_vertexbuffer_ids[SG_MAX_VERTEX_BUFFERS]; |
| 5656 | sg_image cur_vs_image_ids[SG_MAX_SHADERSTAGE_IMAGES]; |
| 5657 | sg_image cur_fs_image_ids[SG_MAX_SHADERSTAGE_IMAGES]; |
| 5658 | sg_sampler cur_vs_sampler_ids[SG_MAX_SHADERSTAGE_SAMPLERS]; |
| 5659 | sg_sampler cur_fs_sampler_ids[SG_MAX_SHADERSTAGE_SAMPLERS]; |
| 5660 | sg_buffer cur_vs_storagebuffer_ids[SG_MAX_SHADERSTAGE_STORAGEBUFFERS]; |
| 5661 | sg_buffer cur_fs_storagebuffer_ids[SG_MAX_SHADERSTAGE_STORAGEBUFFERS]; |
| 5662 | } _sg_mtl_state_cache_t; |
| 5663 | |
| 5664 | typedef struct { |
| 5665 | bool valid; |
| 5666 | bool use_shared_storage_mode; |
| 5667 | uint32_t cur_frame_rotate_index; |
| 5668 | int ub_size; |
| 5669 | int cur_ub_offset; |
| 5670 | uint8_t* cur_ub_base_ptr; |
| 5671 | _sg_mtl_state_cache_t state_cache; |
| 5672 | _sg_mtl_idpool_t idpool; |
| 5673 | dispatch_semaphore_t sem; |
| 5674 | id<MTLDevice> device; |
| 5675 | id<MTLCommandQueue> cmd_queue; |
| 5676 | id<MTLCommandBuffer> cmd_buffer; |
| 5677 | id<MTLRenderCommandEncoder> cmd_encoder; |
| 5678 | id<CAMetalDrawable> cur_drawable; |
| 5679 | id<MTLBuffer> uniform_buffers[SG_NUM_INFLIGHT_FRAMES]; |
| 5680 | } _sg_mtl_backend_t; |
| 5681 | |
| 5682 | #elif defined(SOKOL_WGPU) |
| 5683 | |
| 5684 | #define _SG_WGPU_ROWPITCH_ALIGN (256) |
| 5685 | #define _SG_WGPU_MAX_UNIFORM_UPDATE_SIZE (1<<16) // also see WGPULimits.maxUniformBufferBindingSize |
| 5686 | #define _SG_WGPU_NUM_BINDGROUPS (2) // 0: uniforms, 1: images and sampler on both shader stages |
| 5687 | #define _SG_WGPU_UNIFORM_BINDGROUP_INDEX (0) |
| 5688 | #define _SG_WGPU_IMAGE_SAMPLER_BINDGROUP_INDEX (1) |
| 5689 | #define _SG_WGPU_MAX_BINDGROUP_ENTRIES (SG_NUM_SHADER_STAGES * (SG_MAX_SHADERSTAGE_IMAGES + SG_MAX_SHADERSTAGE_SAMPLERS + SG_MAX_SHADERSTAGE_STORAGEBUFFERS)) |
| 5690 | |
| 5691 | typedef struct { |
| 5692 | _sg_slot_t slot; |
| 5693 | _sg_buffer_common_t cmn; |
| 5694 | struct { |
| 5695 | WGPUBuffer buf; |
| 5696 | } wgpu; |
| 5697 | } _sg_wgpu_buffer_t; |
| 5698 | typedef _sg_wgpu_buffer_t _sg_buffer_t; |
| 5699 | |
| 5700 | typedef struct { |
| 5701 | _sg_slot_t slot; |
| 5702 | _sg_image_common_t cmn; |
| 5703 | struct { |
| 5704 | WGPUTexture tex; |
| 5705 | WGPUTextureView view; |
| 5706 | } wgpu; |
| 5707 | } _sg_wgpu_image_t; |
| 5708 | typedef _sg_wgpu_image_t _sg_image_t; |
| 5709 | |
| 5710 | typedef struct { |
| 5711 | _sg_slot_t slot; |
| 5712 | _sg_sampler_common_t cmn; |
| 5713 | struct { |
| 5714 | WGPUSampler smp; |
| 5715 | } wgpu; |
| 5716 | } _sg_wgpu_sampler_t; |
| 5717 | typedef _sg_wgpu_sampler_t _sg_sampler_t; |
| 5718 | |
| 5719 | typedef struct { |
| 5720 | WGPUShaderModule module; |
| 5721 | _sg_str_t entry; |
| 5722 | } _sg_wgpu_shader_stage_t; |
| 5723 | |
| 5724 | typedef struct { |
| 5725 | _sg_slot_t slot; |
| 5726 | _sg_shader_common_t cmn; |
| 5727 | struct { |
| 5728 | _sg_wgpu_shader_stage_t stage[SG_NUM_SHADER_STAGES]; |
| 5729 | WGPUBindGroupLayout bind_group_layout; |
| 5730 | } wgpu; |
| 5731 | } _sg_wgpu_shader_t; |
| 5732 | typedef _sg_wgpu_shader_t _sg_shader_t; |
| 5733 | |
| 5734 | typedef struct { |
| 5735 | _sg_slot_t slot; |
| 5736 | _sg_pipeline_common_t cmn; |
| 5737 | _sg_shader_t* shader; |
| 5738 | struct { |
| 5739 | WGPURenderPipeline pip; |
| 5740 | WGPUColor blend_color; |
| 5741 | } wgpu; |
| 5742 | } _sg_wgpu_pipeline_t; |
| 5743 | typedef _sg_wgpu_pipeline_t _sg_pipeline_t; |
| 5744 | |
| 5745 | typedef struct { |
| 5746 | _sg_image_t* image; |
| 5747 | WGPUTextureView view; |
| 5748 | } _sg_wgpu_attachment_t; |
| 5749 | |
| 5750 | typedef struct { |
| 5751 | _sg_slot_t slot; |
| 5752 | _sg_attachments_common_t cmn; |
| 5753 | struct { |
| 5754 | _sg_wgpu_attachment_t colors[SG_MAX_COLOR_ATTACHMENTS]; |
| 5755 | _sg_wgpu_attachment_t resolves[SG_MAX_COLOR_ATTACHMENTS]; |
| 5756 | _sg_wgpu_attachment_t depth_stencil; |
| 5757 | } wgpu; |
| 5758 | } _sg_wgpu_attachments_t; |
| 5759 | typedef _sg_wgpu_attachments_t _sg_attachments_t; |
| 5760 | |
| 5761 | // a pool of per-frame uniform buffers |
| 5762 | typedef struct { |
| 5763 | uint32_t num_bytes; |
| 5764 | uint32_t offset; // current offset into buf |
| 5765 | uint8_t* staging; // intermediate buffer for uniform data updates |
| 5766 | WGPUBuffer buf; // the GPU-side uniform buffer |
| 5767 | struct { |
| 5768 | WGPUBindGroupLayout group_layout; |
| 5769 | WGPUBindGroup group; |
| 5770 | uint32_t offsets[SG_NUM_SHADER_STAGES][SG_MAX_SHADERSTAGE_UBS]; |
| 5771 | } bind; |
| 5772 | } _sg_wgpu_uniform_buffer_t; |
| 5773 | |
| 5774 | typedef struct { |
| 5775 | uint32_t id; |
| 5776 | } _sg_wgpu_bindgroup_handle_t; |
| 5777 | |
| 5778 | #define _SG_WGPU_BINDGROUPSCACHE_NUM_ITEMS (1 + _SG_WGPU_MAX_BINDGROUP_ENTRIES) |
| 5779 | typedef struct { |
| 5780 | uint64_t hash; |
| 5781 | uint32_t items[_SG_WGPU_BINDGROUPSCACHE_NUM_ITEMS]; |
| 5782 | } _sg_wgpu_bindgroups_cache_key_t; |
| 5783 | |
| 5784 | typedef struct { |
| 5785 | uint32_t num; // must be 2^n |
| 5786 | uint32_t index_mask; // mask to turn hash into valid index |
| 5787 | _sg_wgpu_bindgroup_handle_t* items; |
| 5788 | } _sg_wgpu_bindgroups_cache_t; |
| 5789 | |
| 5790 | typedef struct { |
| 5791 | _sg_slot_t slot; |
| 5792 | WGPUBindGroup bindgroup; |
| 5793 | _sg_wgpu_bindgroups_cache_key_t key; |
| 5794 | } _sg_wgpu_bindgroup_t; |
| 5795 | |
| 5796 | typedef struct { |
| 5797 | _sg_pool_t pool; |
| 5798 | _sg_wgpu_bindgroup_t* bindgroups; |
| 5799 | } _sg_wgpu_bindgroups_pool_t; |
| 5800 | |
| 5801 | typedef struct { |
| 5802 | struct { |
| 5803 | sg_buffer buffer; |
| 5804 | int offset; |
| 5805 | } vbs[SG_MAX_VERTEX_BUFFERS]; |
| 5806 | struct { |
| 5807 | sg_buffer buffer; |
| 5808 | int offset; |
| 5809 | } ib; |
| 5810 | _sg_wgpu_bindgroup_handle_t bg; |
| 5811 | } _sg_wgpu_bindings_cache_t; |
| 5812 | |
| 5813 | // the WGPU backend state |
| 5814 | typedef struct { |
| 5815 | bool valid; |
| 5816 | bool use_indexed_draw; |
| 5817 | WGPUDevice dev; |
| 5818 | WGPUSupportedLimits limits; |
| 5819 | WGPUQueue queue; |
| 5820 | WGPUCommandEncoder cmd_enc; |
| 5821 | WGPURenderPassEncoder pass_enc; |
| 5822 | WGPUBindGroup empty_bind_group; |
| 5823 | const _sg_pipeline_t* cur_pipeline; |
| 5824 | sg_pipeline cur_pipeline_id; |
| 5825 | _sg_wgpu_uniform_buffer_t uniform; |
| 5826 | _sg_wgpu_bindings_cache_t bindings_cache; |
| 5827 | _sg_wgpu_bindgroups_cache_t bindgroups_cache; |
| 5828 | _sg_wgpu_bindgroups_pool_t bindgroups_pool; |
| 5829 | } _sg_wgpu_backend_t; |
| 5830 | #endif |
| 5831 | |
| 5832 | // POOL STRUCTS |
| 5833 | |
| 5834 | // this *MUST* remain 0 |
| 5835 | #define _SG_INVALID_SLOT_INDEX (0) |
| 5836 | |
| 5837 | typedef struct { |
| 5838 | _sg_pool_t buffer_pool; |
| 5839 | _sg_pool_t image_pool; |
| 5840 | _sg_pool_t sampler_pool; |
| 5841 | _sg_pool_t shader_pool; |
| 5842 | _sg_pool_t pipeline_pool; |
| 5843 | _sg_pool_t attachments_pool; |
| 5844 | _sg_buffer_t* buffers; |
| 5845 | _sg_image_t* images; |
| 5846 | _sg_sampler_t* samplers; |
| 5847 | _sg_shader_t* shaders; |
| 5848 | _sg_pipeline_t* pipelines; |
| 5849 | _sg_attachments_t* attachments; |
| 5850 | } _sg_pools_t; |
| 5851 | |
| 5852 | typedef struct { |
| 5853 | int num; // number of allocated commit listener items |
| 5854 | int upper; // the current upper index (no valid items past this point) |
| 5855 | sg_commit_listener* items; |
| 5856 | } _sg_commit_listeners_t; |
| 5857 | |
| 5858 | // resolved resource bindings struct |
| 5859 | typedef struct { |
| 5860 | _sg_pipeline_t* pip; |
| 5861 | int num_vbs; |
| 5862 | int num_vs_imgs; |
| 5863 | int num_vs_smps; |
| 5864 | int num_vs_sbufs; |
| 5865 | int num_fs_imgs; |
| 5866 | int num_fs_smps; |
| 5867 | int num_fs_sbufs; |
| 5868 | int vb_offsets[SG_MAX_VERTEX_BUFFERS]; |
| 5869 | int ib_offset; |
| 5870 | _sg_buffer_t* vbs[SG_MAX_VERTEX_BUFFERS]; |
| 5871 | _sg_buffer_t* ib; |
| 5872 | _sg_image_t* vs_imgs[SG_MAX_SHADERSTAGE_IMAGES]; |
| 5873 | _sg_sampler_t* vs_smps[SG_MAX_SHADERSTAGE_SAMPLERS]; |
| 5874 | _sg_buffer_t* vs_sbufs[SG_MAX_SHADERSTAGE_STORAGEBUFFERS]; |
| 5875 | _sg_image_t* fs_imgs[SG_MAX_SHADERSTAGE_IMAGES]; |
| 5876 | _sg_sampler_t* fs_smps[SG_MAX_SHADERSTAGE_SAMPLERS]; |
| 5877 | _sg_buffer_t* fs_sbufs[SG_MAX_SHADERSTAGE_STORAGEBUFFERS]; |
| 5878 | } _sg_bindings_t; |
| 5879 | |
| 5880 | typedef struct { |
| 5881 | bool sample; |
| 5882 | bool filter; |
| 5883 | bool render; |
| 5884 | bool blend; |
| 5885 | bool msaa; |
| 5886 | bool depth; |
| 5887 | } _sg_pixelformat_info_t; |
| 5888 | |
| 5889 | typedef struct { |
| 5890 | bool valid; |
| 5891 | sg_desc desc; // original desc with default values patched in |
| 5892 | uint32_t frame_index; |
| 5893 | struct { |
| 5894 | bool valid; |
| 5895 | bool in_pass; |
| 5896 | sg_attachments atts_id; // SG_INVALID_ID in a swapchain pass |
| 5897 | _sg_attachments_t* atts; // 0 in a swapchain pass |
| 5898 | int width; |
| 5899 | int height; |
| 5900 | struct { |
| 5901 | sg_pixel_format color_fmt; |
| 5902 | sg_pixel_format depth_fmt; |
| 5903 | int sample_count; |
| 5904 | } swapchain; |
| 5905 | } cur_pass; |
| 5906 | sg_pipeline cur_pipeline; |
| 5907 | bool next_draw_valid; |
| 5908 | #if defined(SOKOL_DEBUG) |
| 5909 | sg_log_item validate_error; |
| 5910 | #endif |
| 5911 | _sg_pools_t pools; |
| 5912 | sg_backend backend; |
| 5913 | sg_features features; |
| 5914 | sg_limits limits; |
| 5915 | _sg_pixelformat_info_t formats[_SG_PIXELFORMAT_NUM]; |
| 5916 | bool stats_enabled; |
| 5917 | sg_frame_stats stats; |
| 5918 | sg_frame_stats prev_stats; |
| 5919 | #if defined(_SOKOL_ANY_GL) |
| 5920 | _sg_gl_backend_t gl; |
| 5921 | #elif defined(SOKOL_METAL) |
| 5922 | _sg_mtl_backend_t mtl; |
| 5923 | #elif defined(SOKOL_D3D11) |
| 5924 | _sg_d3d11_backend_t d3d11; |
| 5925 | #elif defined(SOKOL_WGPU) |
| 5926 | _sg_wgpu_backend_t wgpu; |
| 5927 | #endif |
| 5928 | #if defined(SOKOL_TRACE_HOOKS) |
| 5929 | sg_trace_hooks hooks; |
| 5930 | #endif |
| 5931 | _sg_commit_listeners_t commit_listeners; |
| 5932 | } _sg_state_t; |
| 5933 | static _sg_state_t _sg; |
| 5934 | |
| 5935 | // ██ ██████ ██████ ██████ ██ ███ ██ ██████ |
| 5936 | // ██ ██ ██ ██ ██ ██ ████ ██ ██ |
| 5937 | // ██ ██ ██ ██ ███ ██ ███ ██ ██ ██ ██ ██ ███ |
| 5938 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 5939 | // ███████ ██████ ██████ ██████ ██ ██ ████ ██████ |
| 5940 | // |
| 5941 | // >>logging |
| 5942 | #if defined(SOKOL_DEBUG) |
| 5943 | #define _SG_LOGITEM_XMACRO(item,msg) #item ": " msg, |
| 5944 | static const char* _sg_log_messages[] = { |
| 5945 | _SG_LOG_ITEMS |
| 5946 | }; |
| 5947 | #undef _SG_LOGITEM_XMACRO |
| 5948 | #endif // SOKOL_DEBUG |
| 5949 | |
| 5950 | #define _SG_PANIC(code) _sg_log(SG_LOGITEM_ ##code, 0, 0, __LINE__) |
| 5951 | #define _SG_ERROR(code) _sg_log(SG_LOGITEM_ ##code, 1, 0, __LINE__) |
| 5952 | #define _SG_WARN(code) _sg_log(SG_LOGITEM_ ##code, 2, 0, __LINE__) |
| 5953 | #define _SG_INFO(code) _sg_log(SG_LOGITEM_ ##code, 3, 0, __LINE__) |
| 5954 | #define _SG_LOGMSG(code,msg) _sg_log(SG_LOGITEM_ ##code, 3, msg, __LINE__) |
| 5955 | #define _SG_VALIDATE(cond,code) if (!(cond)){ _sg.validate_error = SG_LOGITEM_ ##code; _sg_log(SG_LOGITEM_ ##code, 1, 0, __LINE__); } |
| 5956 | |
| 5957 | static void _sg_log(sg_log_item log_item, uint32_t log_level, const char* msg, uint32_t line_nr) { |
| 5958 | if (_sg.desc.logger.func) { |
| 5959 | const char* filename = 0; |
| 5960 | #if defined(SOKOL_DEBUG) |
| 5961 | filename = __FILE__; |
| 5962 | if (0 == msg) { |
| 5963 | msg = _sg_log_messages[log_item]; |
| 5964 | } |
| 5965 | #endif |
| 5966 | _sg.desc.logger.func("sg", log_level, log_item, msg, line_nr, filename, _sg.desc.logger.user_data); |
| 5967 | } else { |
| 5968 | // for log level PANIC it would be 'undefined behaviour' to continue |
| 5969 | if (log_level == 0) { |
| 5970 | abort(); |
| 5971 | } |
| 5972 | } |
| 5973 | } |
| 5974 | |
| 5975 | // ███ ███ ███████ ███ ███ ██████ ██████ ██ ██ |
| 5976 | // ████ ████ ██ ████ ████ ██ ██ ██ ██ ██ ██ |
| 5977 | // ██ ████ ██ █████ ██ ████ ██ ██ ██ ██████ ████ |
| 5978 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 5979 | // ██ ██ ███████ ██ ██ ██████ ██ ██ ██ |
| 5980 | // |
| 5981 | // >>memory |
| 5982 | |
| 5983 | // a helper macro to clear a struct with potentially ARC'ed ObjC references |
| 5984 | #if defined(SOKOL_METAL) |
| 5985 | #if defined(__cplusplus) |
| 5986 | #define _SG_CLEAR_ARC_STRUCT(type, item) { item = type(); } |
| 5987 | #else |
| 5988 | #define _SG_CLEAR_ARC_STRUCT(type, item) { item = (type) { 0 }; } |
| 5989 | #endif |
| 5990 | #else |
| 5991 | #define _SG_CLEAR_ARC_STRUCT(type, item) { _sg_clear(&item, sizeof(item)); } |
| 5992 | #endif |
| 5993 | |
| 5994 | _SOKOL_PRIVATE void _sg_clear(void* ptr, size_t size) { |
| 5995 | SOKOL_ASSERT(ptr && (size > 0)); |
| 5996 | memset(ptr, 0, size); |
| 5997 | } |
| 5998 | |
| 5999 | _SOKOL_PRIVATE void* _sg_malloc(size_t size) { |
| 6000 | SOKOL_ASSERT(size > 0); |
| 6001 | void* ptr; |
| 6002 | if (_sg.desc.allocator.alloc_fn) { |
| 6003 | ptr = _sg.desc.allocator.alloc_fn(size, _sg.desc.allocator.user_data); |
| 6004 | } else { |
| 6005 | ptr = malloc(size); |
| 6006 | } |
| 6007 | if (0 == ptr) { |
| 6008 | _SG_PANIC(MALLOC_FAILED); |
| 6009 | } |
| 6010 | return ptr; |
| 6011 | } |
| 6012 | |
| 6013 | _SOKOL_PRIVATE void* _sg_malloc_clear(size_t size) { |
| 6014 | void* ptr = _sg_malloc(size); |
| 6015 | _sg_clear(ptr, size); |
| 6016 | return ptr; |
| 6017 | } |
| 6018 | |
| 6019 | _SOKOL_PRIVATE void _sg_free(void* ptr) { |
| 6020 | if (_sg.desc.allocator.free_fn) { |
| 6021 | _sg.desc.allocator.free_fn(ptr, _sg.desc.allocator.user_data); |
| 6022 | } else { |
| 6023 | free(ptr); |
| 6024 | } |
| 6025 | } |
| 6026 | |
| 6027 | _SOKOL_PRIVATE bool _sg_strempty(const _sg_str_t* str) { |
| 6028 | return 0 == str->buf[0]; |
| 6029 | } |
| 6030 | |
| 6031 | _SOKOL_PRIVATE const char* _sg_strptr(const _sg_str_t* str) { |
| 6032 | return &str->buf[0]; |
| 6033 | } |
| 6034 | |
| 6035 | _SOKOL_PRIVATE void _sg_strcpy(_sg_str_t* dst, const char* src) { |
| 6036 | SOKOL_ASSERT(dst); |
| 6037 | if (src) { |
| 6038 | #if defined(_MSC_VER) |
| 6039 | strncpy_s(dst->buf, _SG_STRING_SIZE, src, (_SG_STRING_SIZE-1)); |
| 6040 | #else |
| 6041 | strncpy(dst->buf, src, _SG_STRING_SIZE); |
| 6042 | #endif |
| 6043 | dst->buf[_SG_STRING_SIZE-1] = 0; |
| 6044 | } else { |
| 6045 | _sg_clear(dst->buf, _SG_STRING_SIZE); |
| 6046 | } |
| 6047 | } |
| 6048 | |
| 6049 | // ██ ██ ███████ ██ ██████ ███████ ██████ ███████ |
| 6050 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 6051 | // ███████ █████ ██ ██████ █████ ██████ ███████ |
| 6052 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 6053 | // ██ ██ ███████ ███████ ██ ███████ ██ ██ ███████ |
| 6054 | // |
| 6055 | // >>helpers |
| 6056 | _SOKOL_PRIVATE uint32_t _sg_align_u32(uint32_t val, uint32_t align) { |
| 6057 | SOKOL_ASSERT((align > 0) && ((align & (align - 1)) == 0)); |
| 6058 | return (val + (align - 1)) & ~(align - 1); |
| 6059 | } |
| 6060 | |
| 6061 | typedef struct { int x, y, w, h; } _sg_recti_t; |
| 6062 | |
| 6063 | _SOKOL_PRIVATE _sg_recti_t _sg_clipi(int x, int y, int w, int h, int clip_width, int clip_height) { |
| 6064 | x = _sg_min(_sg_max(0, x), clip_width-1); |
| 6065 | y = _sg_min(_sg_max(0, y), clip_height-1); |
| 6066 | if ((x + w) > clip_width) { |
| 6067 | w = clip_width - x; |
| 6068 | } |
| 6069 | if ((y + h) > clip_height) { |
| 6070 | h = clip_height - y; |
| 6071 | } |
| 6072 | w = _sg_max(w, 1); |
| 6073 | h = _sg_max(h, 1); |
| 6074 | const _sg_recti_t res = { x, y, w, h }; |
| 6075 | return res; |
| 6076 | } |
| 6077 | |
| 6078 | _SOKOL_PRIVATE int _sg_vertexformat_bytesize(sg_vertex_format fmt) { |
| 6079 | switch (fmt) { |
| 6080 | case SG_VERTEXFORMAT_FLOAT: return 4; |
| 6081 | case SG_VERTEXFORMAT_FLOAT2: return 8; |
| 6082 | case SG_VERTEXFORMAT_FLOAT3: return 12; |
| 6083 | case SG_VERTEXFORMAT_FLOAT4: return 16; |
| 6084 | case SG_VERTEXFORMAT_BYTE4: return 4; |
| 6085 | case SG_VERTEXFORMAT_BYTE4N: return 4; |
| 6086 | case SG_VERTEXFORMAT_UBYTE4: return 4; |
| 6087 | case SG_VERTEXFORMAT_UBYTE4N: return 4; |
| 6088 | case SG_VERTEXFORMAT_SHORT2: return 4; |
| 6089 | case SG_VERTEXFORMAT_SHORT2N: return 4; |
| 6090 | case SG_VERTEXFORMAT_USHORT2N: return 4; |
| 6091 | case SG_VERTEXFORMAT_SHORT4: return 8; |
| 6092 | case SG_VERTEXFORMAT_SHORT4N: return 8; |
| 6093 | case SG_VERTEXFORMAT_USHORT4N: return 8; |
| 6094 | case SG_VERTEXFORMAT_UINT10_N2: return 4; |
| 6095 | case SG_VERTEXFORMAT_HALF2: return 4; |
| 6096 | case SG_VERTEXFORMAT_HALF4: return 8; |
| 6097 | case SG_VERTEXFORMAT_INVALID: return 0; |
| 6098 | default: |
| 6099 | SOKOL_UNREACHABLE; |
| 6100 | return -1; |
| 6101 | } |
| 6102 | } |
| 6103 | |
| 6104 | _SOKOL_PRIVATE uint32_t _sg_uniform_alignment(sg_uniform_type type, int array_count, sg_uniform_layout ub_layout) { |
| 6105 | if (ub_layout == SG_UNIFORMLAYOUT_NATIVE) { |
| 6106 | return 1; |
| 6107 | } else { |
| 6108 | SOKOL_ASSERT(array_count > 0); |
| 6109 | if (array_count == 1) { |
| 6110 | switch (type) { |
| 6111 | case SG_UNIFORMTYPE_FLOAT: |
| 6112 | case SG_UNIFORMTYPE_INT: |
| 6113 | return 4; |
| 6114 | case SG_UNIFORMTYPE_FLOAT2: |
| 6115 | case SG_UNIFORMTYPE_INT2: |
| 6116 | return 8; |
| 6117 | case SG_UNIFORMTYPE_FLOAT3: |
| 6118 | case SG_UNIFORMTYPE_FLOAT4: |
| 6119 | case SG_UNIFORMTYPE_INT3: |
| 6120 | case SG_UNIFORMTYPE_INT4: |
| 6121 | return 16; |
| 6122 | case SG_UNIFORMTYPE_MAT4: |
| 6123 | return 16; |
| 6124 | default: |
| 6125 | SOKOL_UNREACHABLE; |
| 6126 | return 1; |
| 6127 | } |
| 6128 | } else { |
| 6129 | return 16; |
| 6130 | } |
| 6131 | } |
| 6132 | } |
| 6133 | |
| 6134 | _SOKOL_PRIVATE uint32_t _sg_uniform_size(sg_uniform_type type, int array_count, sg_uniform_layout ub_layout) { |
| 6135 | SOKOL_ASSERT(array_count > 0); |
| 6136 | if (array_count == 1) { |
| 6137 | switch (type) { |
| 6138 | case SG_UNIFORMTYPE_FLOAT: |
| 6139 | case SG_UNIFORMTYPE_INT: |
| 6140 | return 4; |
| 6141 | case SG_UNIFORMTYPE_FLOAT2: |
| 6142 | case SG_UNIFORMTYPE_INT2: |
| 6143 | return 8; |
| 6144 | case SG_UNIFORMTYPE_FLOAT3: |
| 6145 | case SG_UNIFORMTYPE_INT3: |
| 6146 | return 12; |
| 6147 | case SG_UNIFORMTYPE_FLOAT4: |
| 6148 | case SG_UNIFORMTYPE_INT4: |
| 6149 | return 16; |
| 6150 | case SG_UNIFORMTYPE_MAT4: |
| 6151 | return 64; |
| 6152 | default: |
| 6153 | SOKOL_UNREACHABLE; |
| 6154 | return 0; |
| 6155 | } |
| 6156 | } else { |
| 6157 | if (ub_layout == SG_UNIFORMLAYOUT_NATIVE) { |
| 6158 | switch (type) { |
| 6159 | case SG_UNIFORMTYPE_FLOAT: |
| 6160 | case SG_UNIFORMTYPE_INT: |
| 6161 | return 4 * (uint32_t)array_count; |
| 6162 | case SG_UNIFORMTYPE_FLOAT2: |
| 6163 | case SG_UNIFORMTYPE_INT2: |
| 6164 | return 8 * (uint32_t)array_count; |
| 6165 | case SG_UNIFORMTYPE_FLOAT3: |
| 6166 | case SG_UNIFORMTYPE_INT3: |
| 6167 | return 12 * (uint32_t)array_count; |
| 6168 | case SG_UNIFORMTYPE_FLOAT4: |
| 6169 | case SG_UNIFORMTYPE_INT4: |
| 6170 | return 16 * (uint32_t)array_count; |
| 6171 | case SG_UNIFORMTYPE_MAT4: |
| 6172 | return 64 * (uint32_t)array_count; |
| 6173 | default: |
| 6174 | SOKOL_UNREACHABLE; |
| 6175 | return 0; |
| 6176 | } |
| 6177 | } else { |
| 6178 | switch (type) { |
| 6179 | case SG_UNIFORMTYPE_FLOAT: |
| 6180 | case SG_UNIFORMTYPE_FLOAT2: |
| 6181 | case SG_UNIFORMTYPE_FLOAT3: |
| 6182 | case SG_UNIFORMTYPE_FLOAT4: |
| 6183 | case SG_UNIFORMTYPE_INT: |
| 6184 | case SG_UNIFORMTYPE_INT2: |
| 6185 | case SG_UNIFORMTYPE_INT3: |
| 6186 | case SG_UNIFORMTYPE_INT4: |
| 6187 | return 16 * (uint32_t)array_count; |
| 6188 | case SG_UNIFORMTYPE_MAT4: |
| 6189 | return 64 * (uint32_t)array_count; |
| 6190 | default: |
| 6191 | SOKOL_UNREACHABLE; |
| 6192 | return 0; |
| 6193 | } |
| 6194 | } |
| 6195 | } |
| 6196 | } |
| 6197 | |
| 6198 | _SOKOL_PRIVATE bool _sg_is_compressed_pixel_format(sg_pixel_format fmt) { |
| 6199 | switch (fmt) { |
| 6200 | case SG_PIXELFORMAT_BC1_RGBA: |
| 6201 | case SG_PIXELFORMAT_BC2_RGBA: |
| 6202 | case SG_PIXELFORMAT_BC3_RGBA: |
| 6203 | case SG_PIXELFORMAT_BC3_SRGBA: |
| 6204 | case SG_PIXELFORMAT_BC4_R: |
| 6205 | case SG_PIXELFORMAT_BC4_RSN: |
| 6206 | case SG_PIXELFORMAT_BC5_RG: |
| 6207 | case SG_PIXELFORMAT_BC5_RGSN: |
| 6208 | case SG_PIXELFORMAT_BC6H_RGBF: |
| 6209 | case SG_PIXELFORMAT_BC6H_RGBUF: |
| 6210 | case SG_PIXELFORMAT_BC7_RGBA: |
| 6211 | case SG_PIXELFORMAT_BC7_SRGBA: |
| 6212 | case SG_PIXELFORMAT_PVRTC_RGB_2BPP: |
| 6213 | case SG_PIXELFORMAT_PVRTC_RGB_4BPP: |
| 6214 | case SG_PIXELFORMAT_PVRTC_RGBA_2BPP: |
| 6215 | case SG_PIXELFORMAT_PVRTC_RGBA_4BPP: |
| 6216 | case SG_PIXELFORMAT_ETC2_RGB8: |
| 6217 | case SG_PIXELFORMAT_ETC2_SRGB8: |
| 6218 | case SG_PIXELFORMAT_ETC2_RGB8A1: |
| 6219 | case SG_PIXELFORMAT_ETC2_RGBA8: |
| 6220 | case SG_PIXELFORMAT_ETC2_SRGB8A8: |
| 6221 | case SG_PIXELFORMAT_ETC2_RG11: |
| 6222 | case SG_PIXELFORMAT_ETC2_RG11SN: |
| 6223 | case SG_PIXELFORMAT_ASTC_4x4_RGBA: |
| 6224 | case SG_PIXELFORMAT_ASTC_4x4_SRGBA: |
| 6225 | return true; |
| 6226 | default: |
| 6227 | return false; |
| 6228 | } |
| 6229 | } |
| 6230 | |
| 6231 | _SOKOL_PRIVATE bool _sg_is_valid_rendertarget_color_format(sg_pixel_format fmt) { |
| 6232 | const int fmt_index = (int) fmt; |
| 6233 | SOKOL_ASSERT((fmt_index >= 0) && (fmt_index < _SG_PIXELFORMAT_NUM)); |
| 6234 | return _sg.formats[fmt_index].render && !_sg.formats[fmt_index].depth; |
| 6235 | } |
| 6236 | |
| 6237 | _SOKOL_PRIVATE bool _sg_is_valid_rendertarget_depth_format(sg_pixel_format fmt) { |
| 6238 | const int fmt_index = (int) fmt; |
| 6239 | SOKOL_ASSERT((fmt_index >= 0) && (fmt_index < _SG_PIXELFORMAT_NUM)); |
| 6240 | return _sg.formats[fmt_index].render && _sg.formats[fmt_index].depth; |
| 6241 | } |
| 6242 | |
| 6243 | _SOKOL_PRIVATE bool _sg_is_depth_or_depth_stencil_format(sg_pixel_format fmt) { |
| 6244 | return (SG_PIXELFORMAT_DEPTH == fmt) || (SG_PIXELFORMAT_DEPTH_STENCIL == fmt); |
| 6245 | } |
| 6246 | |
| 6247 | _SOKOL_PRIVATE bool _sg_is_depth_stencil_format(sg_pixel_format fmt) { |
| 6248 | return (SG_PIXELFORMAT_DEPTH_STENCIL == fmt); |
| 6249 | } |
| 6250 | |
| 6251 | _SOKOL_PRIVATE int _sg_pixelformat_bytesize(sg_pixel_format fmt) { |
| 6252 | switch (fmt) { |
| 6253 | case SG_PIXELFORMAT_R8: |
| 6254 | case SG_PIXELFORMAT_R8SN: |
| 6255 | case SG_PIXELFORMAT_R8UI: |
| 6256 | case SG_PIXELFORMAT_R8SI: |
| 6257 | return 1; |
| 6258 | case SG_PIXELFORMAT_R16: |
| 6259 | case SG_PIXELFORMAT_R16SN: |
| 6260 | case SG_PIXELFORMAT_R16UI: |
| 6261 | case SG_PIXELFORMAT_R16SI: |
| 6262 | case SG_PIXELFORMAT_R16F: |
| 6263 | case SG_PIXELFORMAT_RG8: |
| 6264 | case SG_PIXELFORMAT_RG8SN: |
| 6265 | case SG_PIXELFORMAT_RG8UI: |
| 6266 | case SG_PIXELFORMAT_RG8SI: |
| 6267 | return 2; |
| 6268 | case SG_PIXELFORMAT_R32UI: |
| 6269 | case SG_PIXELFORMAT_R32SI: |
| 6270 | case SG_PIXELFORMAT_R32F: |
| 6271 | case SG_PIXELFORMAT_RG16: |
| 6272 | case SG_PIXELFORMAT_RG16SN: |
| 6273 | case SG_PIXELFORMAT_RG16UI: |
| 6274 | case SG_PIXELFORMAT_RG16SI: |
| 6275 | case SG_PIXELFORMAT_RG16F: |
| 6276 | case SG_PIXELFORMAT_RGBA8: |
| 6277 | case SG_PIXELFORMAT_SRGB8A8: |
| 6278 | case SG_PIXELFORMAT_RGBA8SN: |
| 6279 | case SG_PIXELFORMAT_RGBA8UI: |
| 6280 | case SG_PIXELFORMAT_RGBA8SI: |
| 6281 | case SG_PIXELFORMAT_BGRA8: |
| 6282 | case SG_PIXELFORMAT_RGB10A2: |
| 6283 | case SG_PIXELFORMAT_RG11B10F: |
| 6284 | case SG_PIXELFORMAT_RGB9E5: |
| 6285 | return 4; |
| 6286 | case SG_PIXELFORMAT_RG32UI: |
| 6287 | case SG_PIXELFORMAT_RG32SI: |
| 6288 | case SG_PIXELFORMAT_RG32F: |
| 6289 | case SG_PIXELFORMAT_RGBA16: |
| 6290 | case SG_PIXELFORMAT_RGBA16SN: |
| 6291 | case SG_PIXELFORMAT_RGBA16UI: |
| 6292 | case SG_PIXELFORMAT_RGBA16SI: |
| 6293 | case SG_PIXELFORMAT_RGBA16F: |
| 6294 | return 8; |
| 6295 | case SG_PIXELFORMAT_RGBA32UI: |
| 6296 | case SG_PIXELFORMAT_RGBA32SI: |
| 6297 | case SG_PIXELFORMAT_RGBA32F: |
| 6298 | return 16; |
| 6299 | case SG_PIXELFORMAT_DEPTH: |
| 6300 | case SG_PIXELFORMAT_DEPTH_STENCIL: |
| 6301 | return 4; |
| 6302 | default: |
| 6303 | SOKOL_UNREACHABLE; |
| 6304 | return 0; |
| 6305 | } |
| 6306 | } |
| 6307 | |
| 6308 | _SOKOL_PRIVATE int _sg_roundup(int val, int round_to) { |
| 6309 | return (val+(round_to-1)) & ~(round_to-1); |
| 6310 | } |
| 6311 | |
| 6312 | _SOKOL_PRIVATE uint32_t _sg_roundup_u32(uint32_t val, uint32_t round_to) { |
| 6313 | return (val+(round_to-1)) & ~(round_to-1); |
| 6314 | } |
| 6315 | |
| 6316 | _SOKOL_PRIVATE uint64_t _sg_roundup_u64(uint64_t val, uint64_t round_to) { |
| 6317 | return (val+(round_to-1)) & ~(round_to-1); |
| 6318 | } |
| 6319 | |
| 6320 | _SOKOL_PRIVATE bool _sg_multiple_u64(uint64_t val, uint64_t of) { |
| 6321 | return (val & (of-1)) == 0; |
| 6322 | } |
| 6323 | |
| 6324 | /* return row pitch for an image |
| 6325 | |
| 6326 | see ComputePitch in https://github.com/microsoft/DirectXTex/blob/master/DirectXTex/DirectXTexUtil.cpp |
| 6327 | |
| 6328 | For the special PVRTC pitch computation, see: |
| 6329 | GL extension requirement (https://www.khronos.org/registry/OpenGL/extensions/IMG/IMG_texture_compression_pvrtc.txt) |
| 6330 | |
| 6331 | Quote: |
| 6332 | |
| 6333 | 6) How is the imageSize argument calculated for the CompressedTexImage2D |
| 6334 | and CompressedTexSubImage2D functions. |
| 6335 | |
| 6336 | Resolution: For PVRTC 4BPP formats the imageSize is calculated as: |
| 6337 | ( max(width, 8) * max(height, 8) * 4 + 7) / 8 |
| 6338 | For PVRTC 2BPP formats the imageSize is calculated as: |
| 6339 | ( max(width, 16) * max(height, 8) * 2 + 7) / 8 |
| 6340 | */ |
| 6341 | _SOKOL_PRIVATE int _sg_row_pitch(sg_pixel_format fmt, int width, int row_align) { |
| 6342 | int pitch; |
| 6343 | switch (fmt) { |
| 6344 | case SG_PIXELFORMAT_BC1_RGBA: |
| 6345 | case SG_PIXELFORMAT_BC4_R: |
| 6346 | case SG_PIXELFORMAT_BC4_RSN: |
| 6347 | case SG_PIXELFORMAT_ETC2_RGB8: |
| 6348 | case SG_PIXELFORMAT_ETC2_SRGB8: |
| 6349 | case SG_PIXELFORMAT_ETC2_RGB8A1: |
| 6350 | pitch = ((width + 3) / 4) * 8; |
| 6351 | pitch = pitch < 8 ? 8 : pitch; |
| 6352 | break; |
| 6353 | case SG_PIXELFORMAT_BC2_RGBA: |
| 6354 | case SG_PIXELFORMAT_BC3_RGBA: |
| 6355 | case SG_PIXELFORMAT_BC3_SRGBA: |
| 6356 | case SG_PIXELFORMAT_BC5_RG: |
| 6357 | case SG_PIXELFORMAT_BC5_RGSN: |
| 6358 | case SG_PIXELFORMAT_BC6H_RGBF: |
| 6359 | case SG_PIXELFORMAT_BC6H_RGBUF: |
| 6360 | case SG_PIXELFORMAT_BC7_RGBA: |
| 6361 | case SG_PIXELFORMAT_BC7_SRGBA: |
| 6362 | case SG_PIXELFORMAT_ETC2_RGBA8: |
| 6363 | case SG_PIXELFORMAT_ETC2_SRGB8A8: |
| 6364 | case SG_PIXELFORMAT_ETC2_RG11: |
| 6365 | case SG_PIXELFORMAT_ETC2_RG11SN: |
| 6366 | case SG_PIXELFORMAT_ASTC_4x4_RGBA: |
| 6367 | case SG_PIXELFORMAT_ASTC_4x4_SRGBA: |
| 6368 | pitch = ((width + 3) / 4) * 16; |
| 6369 | pitch = pitch < 16 ? 16 : pitch; |
| 6370 | break; |
| 6371 | case SG_PIXELFORMAT_PVRTC_RGB_4BPP: |
| 6372 | case SG_PIXELFORMAT_PVRTC_RGBA_4BPP: |
| 6373 | pitch = (_sg_max(width, 8) * 4 + 7) / 8; |
| 6374 | break; |
| 6375 | case SG_PIXELFORMAT_PVRTC_RGB_2BPP: |
| 6376 | case SG_PIXELFORMAT_PVRTC_RGBA_2BPP: |
| 6377 | pitch = (_sg_max(width, 16) * 2 + 7) / 8; |
| 6378 | break; |
| 6379 | default: |
| 6380 | pitch = width * _sg_pixelformat_bytesize(fmt); |
| 6381 | break; |
| 6382 | } |
| 6383 | pitch = _sg_roundup(pitch, row_align); |
| 6384 | return pitch; |
| 6385 | } |
| 6386 | |
| 6387 | // compute the number of rows in a surface depending on pixel format |
| 6388 | _SOKOL_PRIVATE int _sg_num_rows(sg_pixel_format fmt, int height) { |
| 6389 | int num_rows; |
| 6390 | switch (fmt) { |
| 6391 | case SG_PIXELFORMAT_BC1_RGBA: |
| 6392 | case SG_PIXELFORMAT_BC4_R: |
| 6393 | case SG_PIXELFORMAT_BC4_RSN: |
| 6394 | case SG_PIXELFORMAT_ETC2_RGB8: |
| 6395 | case SG_PIXELFORMAT_ETC2_SRGB8: |
| 6396 | case SG_PIXELFORMAT_ETC2_RGB8A1: |
| 6397 | case SG_PIXELFORMAT_ETC2_RGBA8: |
| 6398 | case SG_PIXELFORMAT_ETC2_SRGB8A8: |
| 6399 | case SG_PIXELFORMAT_ETC2_RG11: |
| 6400 | case SG_PIXELFORMAT_ETC2_RG11SN: |
| 6401 | case SG_PIXELFORMAT_BC2_RGBA: |
| 6402 | case SG_PIXELFORMAT_BC3_RGBA: |
| 6403 | case SG_PIXELFORMAT_BC3_SRGBA: |
| 6404 | case SG_PIXELFORMAT_BC5_RG: |
| 6405 | case SG_PIXELFORMAT_BC5_RGSN: |
| 6406 | case SG_PIXELFORMAT_BC6H_RGBF: |
| 6407 | case SG_PIXELFORMAT_BC6H_RGBUF: |
| 6408 | case SG_PIXELFORMAT_BC7_RGBA: |
| 6409 | case SG_PIXELFORMAT_BC7_SRGBA: |
| 6410 | case SG_PIXELFORMAT_ASTC_4x4_RGBA: |
| 6411 | case SG_PIXELFORMAT_ASTC_4x4_SRGBA: |
| 6412 | num_rows = ((height + 3) / 4); |
| 6413 | break; |
| 6414 | case SG_PIXELFORMAT_PVRTC_RGB_4BPP: |
| 6415 | case SG_PIXELFORMAT_PVRTC_RGBA_4BPP: |
| 6416 | case SG_PIXELFORMAT_PVRTC_RGB_2BPP: |
| 6417 | case SG_PIXELFORMAT_PVRTC_RGBA_2BPP: |
| 6418 | /* NOTE: this is most likely not correct because it ignores any |
| 6419 | PVCRTC block size, but multiplied with _sg_row_pitch() |
| 6420 | it gives the correct surface pitch. |
| 6421 | |
| 6422 | See: https://www.khronos.org/registry/OpenGL/extensions/IMG/IMG_texture_compression_pvrtc.txt |
| 6423 | */ |
| 6424 | num_rows = ((_sg_max(height, 8) + 7) / 8) * 8; |
| 6425 | break; |
| 6426 | default: |
| 6427 | num_rows = height; |
| 6428 | break; |
| 6429 | } |
| 6430 | if (num_rows < 1) { |
| 6431 | num_rows = 1; |
| 6432 | } |
| 6433 | return num_rows; |
| 6434 | } |
| 6435 | |
| 6436 | // return size of a mipmap level |
| 6437 | _SOKOL_PRIVATE int _sg_miplevel_dim(int base_dim, int mip_level) { |
| 6438 | return _sg_max(base_dim >> mip_level, 1); |
| 6439 | } |
| 6440 | |
| 6441 | /* return pitch of a 2D subimage / texture slice |
| 6442 | see ComputePitch in https://github.com/microsoft/DirectXTex/blob/master/DirectXTex/DirectXTexUtil.cpp |
| 6443 | */ |
| 6444 | _SOKOL_PRIVATE int _sg_surface_pitch(sg_pixel_format fmt, int width, int height, int row_align) { |
| 6445 | int num_rows = _sg_num_rows(fmt, height); |
| 6446 | return num_rows * _sg_row_pitch(fmt, width, row_align); |
| 6447 | } |
| 6448 | |
| 6449 | // capability table pixel format helper functions |
| 6450 | _SOKOL_PRIVATE void _sg_pixelformat_all(_sg_pixelformat_info_t* pfi) { |
| 6451 | pfi->sample = true; |
| 6452 | pfi->filter = true; |
| 6453 | pfi->blend = true; |
| 6454 | pfi->render = true; |
| 6455 | pfi->msaa = true; |
| 6456 | } |
| 6457 | |
| 6458 | _SOKOL_PRIVATE void _sg_pixelformat_s(_sg_pixelformat_info_t* pfi) { |
| 6459 | pfi->sample = true; |
| 6460 | } |
| 6461 | |
| 6462 | _SOKOL_PRIVATE void _sg_pixelformat_sf(_sg_pixelformat_info_t* pfi) { |
| 6463 | pfi->sample = true; |
| 6464 | pfi->filter = true; |
| 6465 | } |
| 6466 | |
| 6467 | _SOKOL_PRIVATE void _sg_pixelformat_sr(_sg_pixelformat_info_t* pfi) { |
| 6468 | pfi->sample = true; |
| 6469 | pfi->render = true; |
| 6470 | } |
| 6471 | |
| 6472 | _SOKOL_PRIVATE void _sg_pixelformat_srmd(_sg_pixelformat_info_t* pfi) { |
| 6473 | pfi->sample = true; |
| 6474 | pfi->render = true; |
| 6475 | pfi->msaa = true; |
| 6476 | pfi->depth = true; |
| 6477 | } |
| 6478 | |
| 6479 | _SOKOL_PRIVATE void _sg_pixelformat_srm(_sg_pixelformat_info_t* pfi) { |
| 6480 | pfi->sample = true; |
| 6481 | pfi->render = true; |
| 6482 | pfi->msaa = true; |
| 6483 | } |
| 6484 | |
| 6485 | _SOKOL_PRIVATE void _sg_pixelformat_sfrm(_sg_pixelformat_info_t* pfi) { |
| 6486 | pfi->sample = true; |
| 6487 | pfi->filter = true; |
| 6488 | pfi->render = true; |
| 6489 | pfi->msaa = true; |
| 6490 | } |
| 6491 | _SOKOL_PRIVATE void _sg_pixelformat_sbrm(_sg_pixelformat_info_t* pfi) { |
| 6492 | pfi->sample = true; |
| 6493 | pfi->blend = true; |
| 6494 | pfi->render = true; |
| 6495 | pfi->msaa = true; |
| 6496 | } |
| 6497 | |
| 6498 | _SOKOL_PRIVATE void _sg_pixelformat_sbr(_sg_pixelformat_info_t* pfi) { |
| 6499 | pfi->sample = true; |
| 6500 | pfi->blend = true; |
| 6501 | pfi->render = true; |
| 6502 | } |
| 6503 | |
| 6504 | _SOKOL_PRIVATE void _sg_pixelformat_sfbr(_sg_pixelformat_info_t* pfi) { |
| 6505 | pfi->sample = true; |
| 6506 | pfi->filter = true; |
| 6507 | pfi->blend = true; |
| 6508 | pfi->render = true; |
| 6509 | } |
| 6510 | |
| 6511 | _SOKOL_PRIVATE sg_pass_action _sg_pass_action_defaults(const sg_pass_action* action) { |
| 6512 | SOKOL_ASSERT(action); |
| 6513 | sg_pass_action res = *action; |
| 6514 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 6515 | if (res.colors[i].load_action == _SG_LOADACTION_DEFAULT) { |
| 6516 | res.colors[i].load_action = SG_LOADACTION_CLEAR; |
| 6517 | res.colors[i].clear_value.r = SG_DEFAULT_CLEAR_RED; |
| 6518 | res.colors[i].clear_value.g = SG_DEFAULT_CLEAR_GREEN; |
| 6519 | res.colors[i].clear_value.b = SG_DEFAULT_CLEAR_BLUE; |
| 6520 | res.colors[i].clear_value.a = SG_DEFAULT_CLEAR_ALPHA; |
| 6521 | } |
| 6522 | if (res.colors[i].store_action == _SG_STOREACTION_DEFAULT) { |
| 6523 | res.colors[i].store_action = SG_STOREACTION_STORE; |
| 6524 | } |
| 6525 | } |
| 6526 | if (res.depth.load_action == _SG_LOADACTION_DEFAULT) { |
| 6527 | res.depth.load_action = SG_LOADACTION_CLEAR; |
| 6528 | res.depth.clear_value = SG_DEFAULT_CLEAR_DEPTH; |
| 6529 | } |
| 6530 | if (res.depth.store_action == _SG_STOREACTION_DEFAULT) { |
| 6531 | res.depth.store_action = SG_STOREACTION_DONTCARE; |
| 6532 | } |
| 6533 | if (res.stencil.load_action == _SG_LOADACTION_DEFAULT) { |
| 6534 | res.stencil.load_action = SG_LOADACTION_CLEAR; |
| 6535 | res.stencil.clear_value = SG_DEFAULT_CLEAR_STENCIL; |
| 6536 | } |
| 6537 | if (res.stencil.store_action == _SG_STOREACTION_DEFAULT) { |
| 6538 | res.stencil.store_action = SG_STOREACTION_DONTCARE; |
| 6539 | } |
| 6540 | return res; |
| 6541 | } |
| 6542 | |
| 6543 | // ██████ ██ ██ ███ ███ ███ ███ ██ ██ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████ |
| 6544 | // ██ ██ ██ ██ ████ ████ ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ |
| 6545 | // ██ ██ ██ ██ ██ ████ ██ ██ ████ ██ ████ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██ |
| 6546 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 6547 | // ██████ ██████ ██ ██ ██ ██ ██ ██████ ██ ██ ██████ ██ ██ ███████ ██ ████ ██████ |
| 6548 | // |
| 6549 | // >>dummy backend |
| 6550 | #if defined(SOKOL_DUMMY_BACKEND) |
| 6551 | |
| 6552 | _SOKOL_PRIVATE void _sg_dummy_setup_backend(const sg_desc* desc) { |
| 6553 | SOKOL_ASSERT(desc); |
| 6554 | _SOKOL_UNUSED(desc); |
| 6555 | _sg.backend = SG_BACKEND_DUMMY; |
| 6556 | for (int i = SG_PIXELFORMAT_R8; i < SG_PIXELFORMAT_BC1_RGBA; i++) { |
| 6557 | _sg.formats[i].sample = true; |
| 6558 | _sg.formats[i].filter = true; |
| 6559 | _sg.formats[i].render = true; |
| 6560 | _sg.formats[i].blend = true; |
| 6561 | _sg.formats[i].msaa = true; |
| 6562 | } |
| 6563 | _sg.formats[SG_PIXELFORMAT_DEPTH].depth = true; |
| 6564 | _sg.formats[SG_PIXELFORMAT_DEPTH_STENCIL].depth = true; |
| 6565 | } |
| 6566 | |
| 6567 | _SOKOL_PRIVATE void _sg_dummy_discard_backend(void) { |
| 6568 | // empty |
| 6569 | } |
| 6570 | |
| 6571 | _SOKOL_PRIVATE void _sg_dummy_reset_state_cache(void) { |
| 6572 | // empty |
| 6573 | } |
| 6574 | |
| 6575 | _SOKOL_PRIVATE sg_resource_state _sg_dummy_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) { |
| 6576 | SOKOL_ASSERT(buf && desc); |
| 6577 | _SOKOL_UNUSED(buf); |
| 6578 | _SOKOL_UNUSED(desc); |
| 6579 | return SG_RESOURCESTATE_VALID; |
| 6580 | } |
| 6581 | |
| 6582 | _SOKOL_PRIVATE void _sg_dummy_discard_buffer(_sg_buffer_t* buf) { |
| 6583 | SOKOL_ASSERT(buf); |
| 6584 | _SOKOL_UNUSED(buf); |
| 6585 | } |
| 6586 | |
| 6587 | _SOKOL_PRIVATE sg_resource_state _sg_dummy_create_image(_sg_image_t* img, const sg_image_desc* desc) { |
| 6588 | SOKOL_ASSERT(img && desc); |
| 6589 | _SOKOL_UNUSED(img); |
| 6590 | _SOKOL_UNUSED(desc); |
| 6591 | return SG_RESOURCESTATE_VALID; |
| 6592 | } |
| 6593 | |
| 6594 | _SOKOL_PRIVATE void _sg_dummy_discard_image(_sg_image_t* img) { |
| 6595 | SOKOL_ASSERT(img); |
| 6596 | _SOKOL_UNUSED(img); |
| 6597 | } |
| 6598 | |
| 6599 | _SOKOL_PRIVATE sg_resource_state _sg_dummy_create_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) { |
| 6600 | SOKOL_ASSERT(smp && desc); |
| 6601 | _SOKOL_UNUSED(smp); |
| 6602 | _SOKOL_UNUSED(desc); |
| 6603 | return SG_RESOURCESTATE_VALID; |
| 6604 | } |
| 6605 | |
| 6606 | _SOKOL_PRIVATE void _sg_dummy_discard_sampler(_sg_sampler_t* smp) { |
| 6607 | SOKOL_ASSERT(smp); |
| 6608 | _SOKOL_UNUSED(smp); |
| 6609 | } |
| 6610 | |
| 6611 | _SOKOL_PRIVATE sg_resource_state _sg_dummy_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) { |
| 6612 | SOKOL_ASSERT(shd && desc); |
| 6613 | _SOKOL_UNUSED(shd); |
| 6614 | _SOKOL_UNUSED(desc); |
| 6615 | return SG_RESOURCESTATE_VALID; |
| 6616 | } |
| 6617 | |
| 6618 | _SOKOL_PRIVATE void _sg_dummy_discard_shader(_sg_shader_t* shd) { |
| 6619 | SOKOL_ASSERT(shd); |
| 6620 | _SOKOL_UNUSED(shd); |
| 6621 | } |
| 6622 | |
| 6623 | _SOKOL_PRIVATE sg_resource_state _sg_dummy_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) { |
| 6624 | SOKOL_ASSERT(pip && desc); |
| 6625 | pip->shader = shd; |
| 6626 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 6627 | const sg_vertex_attr_state* a_state = &desc->layout.attrs[attr_index]; |
| 6628 | if (a_state->format == SG_VERTEXFORMAT_INVALID) { |
| 6629 | break; |
| 6630 | } |
| 6631 | SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS); |
| 6632 | pip->cmn.vertex_buffer_layout_active[a_state->buffer_index] = true; |
| 6633 | } |
| 6634 | return SG_RESOURCESTATE_VALID; |
| 6635 | } |
| 6636 | |
| 6637 | _SOKOL_PRIVATE void _sg_dummy_discard_pipeline(_sg_pipeline_t* pip) { |
| 6638 | SOKOL_ASSERT(pip); |
| 6639 | _SOKOL_UNUSED(pip); |
| 6640 | } |
| 6641 | |
| 6642 | _SOKOL_PRIVATE sg_resource_state _sg_dummy_create_attachments(_sg_attachments_t* atts, _sg_image_t** color_images, _sg_image_t** resolve_images, _sg_image_t* ds_img, const sg_attachments_desc* desc) { |
| 6643 | SOKOL_ASSERT(atts && desc); |
| 6644 | SOKOL_ASSERT(color_images && resolve_images); |
| 6645 | |
| 6646 | for (int i = 0; i < atts->cmn.num_colors; i++) { |
| 6647 | const sg_attachment_desc* color_desc = &desc->colors[i]; |
| 6648 | _SOKOL_UNUSED(color_desc); |
| 6649 | SOKOL_ASSERT(color_desc->image.id != SG_INVALID_ID); |
| 6650 | SOKOL_ASSERT(0 == atts->dmy.colors[i].image); |
| 6651 | SOKOL_ASSERT(color_images[i] && (color_images[i]->slot.id == color_desc->image.id)); |
| 6652 | SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(color_images[i]->cmn.pixel_format)); |
| 6653 | atts->dmy.colors[i].image = color_images[i]; |
| 6654 | |
| 6655 | const sg_attachment_desc* resolve_desc = &desc->resolves[i]; |
| 6656 | if (resolve_desc->image.id != SG_INVALID_ID) { |
| 6657 | SOKOL_ASSERT(0 == atts->dmy.resolves[i].image); |
| 6658 | SOKOL_ASSERT(resolve_images[i] && (resolve_images[i]->slot.id == resolve_desc->image.id)); |
| 6659 | SOKOL_ASSERT(color_images[i] && (color_images[i]->cmn.pixel_format == resolve_images[i]->cmn.pixel_format)); |
| 6660 | atts->dmy.resolves[i].image = resolve_images[i]; |
| 6661 | } |
| 6662 | } |
| 6663 | |
| 6664 | SOKOL_ASSERT(0 == atts->dmy.depth_stencil.image); |
| 6665 | const sg_attachment_desc* ds_desc = &desc->depth_stencil; |
| 6666 | if (ds_desc->image.id != SG_INVALID_ID) { |
| 6667 | SOKOL_ASSERT(ds_img && (ds_img->slot.id == ds_desc->image.id)); |
| 6668 | SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(ds_img->cmn.pixel_format)); |
| 6669 | atts->dmy.depth_stencil.image = ds_img; |
| 6670 | } |
| 6671 | return SG_RESOURCESTATE_VALID; |
| 6672 | } |
| 6673 | |
| 6674 | _SOKOL_PRIVATE void _sg_dummy_discard_attachments(_sg_attachments_t* atts) { |
| 6675 | SOKOL_ASSERT(atts); |
| 6676 | _SOKOL_UNUSED(atts); |
| 6677 | } |
| 6678 | |
| 6679 | _SOKOL_PRIVATE _sg_image_t* _sg_dummy_attachments_color_image(const _sg_attachments_t* atts, int index) { |
| 6680 | SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS)); |
| 6681 | return atts->dmy.colors[index].image; |
| 6682 | } |
| 6683 | |
| 6684 | _SOKOL_PRIVATE _sg_image_t* _sg_dummy_attachments_resolve_image(const _sg_attachments_t* atts, int index) { |
| 6685 | SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS)); |
| 6686 | return atts->dmy.resolves[index].image; |
| 6687 | } |
| 6688 | |
| 6689 | _SOKOL_PRIVATE _sg_image_t* _sg_dummy_attachments_ds_image(const _sg_attachments_t* atts) { |
| 6690 | SOKOL_ASSERT(atts); |
| 6691 | return atts->dmy.depth_stencil.image; |
| 6692 | } |
| 6693 | |
| 6694 | _SOKOL_PRIVATE void _sg_dummy_begin_pass(const sg_pass* pass) { |
| 6695 | SOKOL_ASSERT(pass); |
| 6696 | _SOKOL_UNUSED(pass); |
| 6697 | } |
| 6698 | |
| 6699 | _SOKOL_PRIVATE void _sg_dummy_end_pass(void) { |
| 6700 | // empty |
| 6701 | } |
| 6702 | |
| 6703 | _SOKOL_PRIVATE void _sg_dummy_commit(void) { |
| 6704 | // empty |
| 6705 | } |
| 6706 | |
| 6707 | _SOKOL_PRIVATE void _sg_dummy_apply_viewport(int x, int y, int w, int h, bool origin_top_left) { |
| 6708 | _SOKOL_UNUSED(x); |
| 6709 | _SOKOL_UNUSED(y); |
| 6710 | _SOKOL_UNUSED(w); |
| 6711 | _SOKOL_UNUSED(h); |
| 6712 | _SOKOL_UNUSED(origin_top_left); |
| 6713 | } |
| 6714 | |
| 6715 | _SOKOL_PRIVATE void _sg_dummy_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) { |
| 6716 | _SOKOL_UNUSED(x); |
| 6717 | _SOKOL_UNUSED(y); |
| 6718 | _SOKOL_UNUSED(w); |
| 6719 | _SOKOL_UNUSED(h); |
| 6720 | _SOKOL_UNUSED(origin_top_left); |
| 6721 | } |
| 6722 | |
| 6723 | _SOKOL_PRIVATE void _sg_dummy_apply_pipeline(_sg_pipeline_t* pip) { |
| 6724 | SOKOL_ASSERT(pip); |
| 6725 | _SOKOL_UNUSED(pip); |
| 6726 | } |
| 6727 | |
| 6728 | _SOKOL_PRIVATE bool _sg_dummy_apply_bindings(_sg_bindings_t* bnd) { |
| 6729 | SOKOL_ASSERT(bnd); |
| 6730 | SOKOL_ASSERT(bnd->pip); |
| 6731 | _SOKOL_UNUSED(bnd); |
| 6732 | return true; |
| 6733 | } |
| 6734 | |
| 6735 | _SOKOL_PRIVATE void _sg_dummy_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) { |
| 6736 | _SOKOL_UNUSED(stage_index); |
| 6737 | _SOKOL_UNUSED(ub_index); |
| 6738 | _SOKOL_UNUSED(data); |
| 6739 | } |
| 6740 | |
| 6741 | _SOKOL_PRIVATE void _sg_dummy_draw(int base_element, int num_elements, int num_instances) { |
| 6742 | _SOKOL_UNUSED(base_element); |
| 6743 | _SOKOL_UNUSED(num_elements); |
| 6744 | _SOKOL_UNUSED(num_instances); |
| 6745 | } |
| 6746 | |
| 6747 | _SOKOL_PRIVATE void _sg_dummy_update_buffer(_sg_buffer_t* buf, const sg_range* data) { |
| 6748 | SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0)); |
| 6749 | _SOKOL_UNUSED(data); |
| 6750 | if (++buf->cmn.active_slot >= buf->cmn.num_slots) { |
| 6751 | buf->cmn.active_slot = 0; |
| 6752 | } |
| 6753 | } |
| 6754 | |
| 6755 | _SOKOL_PRIVATE bool _sg_dummy_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) { |
| 6756 | SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0)); |
| 6757 | _SOKOL_UNUSED(data); |
| 6758 | if (new_frame) { |
| 6759 | if (++buf->cmn.active_slot >= buf->cmn.num_slots) { |
| 6760 | buf->cmn.active_slot = 0; |
| 6761 | } |
| 6762 | } |
| 6763 | return true; |
| 6764 | } |
| 6765 | |
| 6766 | _SOKOL_PRIVATE void _sg_dummy_update_image(_sg_image_t* img, const sg_image_data* data) { |
| 6767 | SOKOL_ASSERT(img && data); |
| 6768 | _SOKOL_UNUSED(data); |
| 6769 | if (++img->cmn.active_slot >= img->cmn.num_slots) { |
| 6770 | img->cmn.active_slot = 0; |
| 6771 | } |
| 6772 | } |
| 6773 | |
| 6774 | // ██████ ██████ ███████ ███ ██ ██████ ██ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████ |
| 6775 | // ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ |
| 6776 | // ██ ██ ██████ █████ ██ ██ ██ ██ ███ ██ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██ |
| 6777 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 6778 | // ██████ ██ ███████ ██ ████ ██████ ███████ ██████ ██ ██ ██████ ██ ██ ███████ ██ ████ ██████ |
| 6779 | // |
| 6780 | // >>opengl backend |
| 6781 | #elif defined(_SOKOL_ANY_GL) |
| 6782 | |
| 6783 | // optional GL loader for win32 |
| 6784 | #if defined(_SOKOL_USE_WIN32_GL_LOADER) |
| 6785 | |
| 6786 | #ifndef SG_GL_FUNCS_EXT |
| 6787 | #define SG_GL_FUNCS_EXT |
| 6788 | #endif |
| 6789 | |
| 6790 | // X Macro list of GL function names and signatures |
| 6791 | #define _SG_GL_FUNCS \ |
| 6792 | SG_GL_FUNCS_EXT \ |
| 6793 | _SG_XMACRO(glBindVertexArray, void, (GLuint array)) \ |
| 6794 | _SG_XMACRO(glFramebufferTextureLayer, void, (GLenum target, GLenum attachment, GLuint texture, GLint level, GLint layer)) \ |
| 6795 | _SG_XMACRO(glGenFramebuffers, void, (GLsizei n, GLuint * framebuffers)) \ |
| 6796 | _SG_XMACRO(glBindFramebuffer, void, (GLenum target, GLuint framebuffer)) \ |
| 6797 | _SG_XMACRO(glBindRenderbuffer, void, (GLenum target, GLuint renderbuffer)) \ |
| 6798 | _SG_XMACRO(glGetStringi, const GLubyte *, (GLenum name, GLuint index)) \ |
| 6799 | _SG_XMACRO(glClearBufferfi, void, (GLenum buffer, GLint drawbuffer, GLfloat depth, GLint stencil)) \ |
| 6800 | _SG_XMACRO(glClearBufferfv, void, (GLenum buffer, GLint drawbuffer, const GLfloat * value)) \ |
| 6801 | _SG_XMACRO(glClearBufferuiv, void, (GLenum buffer, GLint drawbuffer, const GLuint * value)) \ |
| 6802 | _SG_XMACRO(glClearBufferiv, void, (GLenum buffer, GLint drawbuffer, const GLint * value)) \ |
| 6803 | _SG_XMACRO(glDeleteRenderbuffers, void, (GLsizei n, const GLuint * renderbuffers)) \ |
| 6804 | _SG_XMACRO(glUniform1fv, void, (GLint location, GLsizei count, const GLfloat * value)) \ |
| 6805 | _SG_XMACRO(glUniform2fv, void, (GLint location, GLsizei count, const GLfloat * value)) \ |
| 6806 | _SG_XMACRO(glUniform3fv, void, (GLint location, GLsizei count, const GLfloat * value)) \ |
| 6807 | _SG_XMACRO(glUniform4fv, void, (GLint location, GLsizei count, const GLfloat * value)) \ |
| 6808 | _SG_XMACRO(glUniform1iv, void, (GLint location, GLsizei count, const GLint * value)) \ |
| 6809 | _SG_XMACRO(glUniform2iv, void, (GLint location, GLsizei count, const GLint * value)) \ |
| 6810 | _SG_XMACRO(glUniform3iv, void, (GLint location, GLsizei count, const GLint * value)) \ |
| 6811 | _SG_XMACRO(glUniform4iv, void, (GLint location, GLsizei count, const GLint * value)) \ |
| 6812 | _SG_XMACRO(glUniformMatrix4fv, void, (GLint location, GLsizei count, GLboolean transpose, const GLfloat * value)) \ |
| 6813 | _SG_XMACRO(glUseProgram, void, (GLuint program)) \ |
| 6814 | _SG_XMACRO(glShaderSource, void, (GLuint shader, GLsizei count, const GLchar *const* string, const GLint * length)) \ |
| 6815 | _SG_XMACRO(glLinkProgram, void, (GLuint program)) \ |
| 6816 | _SG_XMACRO(glGetUniformLocation, GLint, (GLuint program, const GLchar * name)) \ |
| 6817 | _SG_XMACRO(glGetShaderiv, void, (GLuint shader, GLenum pname, GLint * params)) \ |
| 6818 | _SG_XMACRO(glGetProgramInfoLog, void, (GLuint program, GLsizei bufSize, GLsizei * length, GLchar * infoLog)) \ |
| 6819 | _SG_XMACRO(glGetAttribLocation, GLint, (GLuint program, const GLchar * name)) \ |
| 6820 | _SG_XMACRO(glDisableVertexAttribArray, void, (GLuint index)) \ |
| 6821 | _SG_XMACRO(glDeleteShader, void, (GLuint shader)) \ |
| 6822 | _SG_XMACRO(glDeleteProgram, void, (GLuint program)) \ |
| 6823 | _SG_XMACRO(glCompileShader, void, (GLuint shader)) \ |
| 6824 | _SG_XMACRO(glStencilFuncSeparate, void, (GLenum face, GLenum func, GLint ref, GLuint mask)) \ |
| 6825 | _SG_XMACRO(glStencilOpSeparate, void, (GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass)) \ |
| 6826 | _SG_XMACRO(glRenderbufferStorageMultisample, void, (GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height)) \ |
| 6827 | _SG_XMACRO(glDrawBuffers, void, (GLsizei n, const GLenum * bufs)) \ |
| 6828 | _SG_XMACRO(glVertexAttribDivisor, void, (GLuint index, GLuint divisor)) \ |
| 6829 | _SG_XMACRO(glBufferSubData, void, (GLenum target, GLintptr offset, GLsizeiptr size, const void * data)) \ |
| 6830 | _SG_XMACRO(glGenBuffers, void, (GLsizei n, GLuint * buffers)) \ |
| 6831 | _SG_XMACRO(glCheckFramebufferStatus, GLenum, (GLenum target)) \ |
| 6832 | _SG_XMACRO(glFramebufferRenderbuffer, void, (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer)) \ |
| 6833 | _SG_XMACRO(glCompressedTexImage2D, void, (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const void * data)) \ |
| 6834 | _SG_XMACRO(glCompressedTexImage3D, void, (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLsizei imageSize, const void * data)) \ |
| 6835 | _SG_XMACRO(glActiveTexture, void, (GLenum texture)) \ |
| 6836 | _SG_XMACRO(glTexSubImage3D, void, (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void * pixels)) \ |
| 6837 | _SG_XMACRO(glRenderbufferStorage, void, (GLenum target, GLenum internalformat, GLsizei width, GLsizei height)) \ |
| 6838 | _SG_XMACRO(glGenTextures, void, (GLsizei n, GLuint * textures)) \ |
| 6839 | _SG_XMACRO(glPolygonOffset, void, (GLfloat factor, GLfloat units)) \ |
| 6840 | _SG_XMACRO(glDrawElements, void, (GLenum mode, GLsizei count, GLenum type, const void * indices)) \ |
| 6841 | _SG_XMACRO(glDeleteFramebuffers, void, (GLsizei n, const GLuint * framebuffers)) \ |
| 6842 | _SG_XMACRO(glBlendEquationSeparate, void, (GLenum modeRGB, GLenum modeAlpha)) \ |
| 6843 | _SG_XMACRO(glDeleteTextures, void, (GLsizei n, const GLuint * textures)) \ |
| 6844 | _SG_XMACRO(glGetProgramiv, void, (GLuint program, GLenum pname, GLint * params)) \ |
| 6845 | _SG_XMACRO(glBindTexture, void, (GLenum target, GLuint texture)) \ |
| 6846 | _SG_XMACRO(glTexImage3D, void, (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void * pixels)) \ |
| 6847 | _SG_XMACRO(glCreateShader, GLuint, (GLenum type)) \ |
| 6848 | _SG_XMACRO(glTexSubImage2D, void, (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void * pixels)) \ |
| 6849 | _SG_XMACRO(glFramebufferTexture2D, void, (GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)) \ |
| 6850 | _SG_XMACRO(glCreateProgram, GLuint, (void)) \ |
| 6851 | _SG_XMACRO(glViewport, void, (GLint x, GLint y, GLsizei width, GLsizei height)) \ |
| 6852 | _SG_XMACRO(glDeleteBuffers, void, (GLsizei n, const GLuint * buffers)) \ |
| 6853 | _SG_XMACRO(glDrawArrays, void, (GLenum mode, GLint first, GLsizei count)) \ |
| 6854 | _SG_XMACRO(glDrawElementsInstanced, void, (GLenum mode, GLsizei count, GLenum type, const void * indices, GLsizei instancecount)) \ |
| 6855 | _SG_XMACRO(glVertexAttribPointer, void, (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void * pointer)) \ |
| 6856 | _SG_XMACRO(glUniform1i, void, (GLint location, GLint v0)) \ |
| 6857 | _SG_XMACRO(glDisable, void, (GLenum cap)) \ |
| 6858 | _SG_XMACRO(glColorMask, void, (GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)) \ |
| 6859 | _SG_XMACRO(glColorMaski, void, (GLuint buf, GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha)) \ |
| 6860 | _SG_XMACRO(glBindBuffer, void, (GLenum target, GLuint buffer)) \ |
| 6861 | _SG_XMACRO(glDeleteVertexArrays, void, (GLsizei n, const GLuint * arrays)) \ |
| 6862 | _SG_XMACRO(glDepthMask, void, (GLboolean flag)) \ |
| 6863 | _SG_XMACRO(glDrawArraysInstanced, void, (GLenum mode, GLint first, GLsizei count, GLsizei instancecount)) \ |
| 6864 | _SG_XMACRO(glScissor, void, (GLint x, GLint y, GLsizei width, GLsizei height)) \ |
| 6865 | _SG_XMACRO(glGenRenderbuffers, void, (GLsizei n, GLuint * renderbuffers)) \ |
| 6866 | _SG_XMACRO(glBufferData, void, (GLenum target, GLsizeiptr size, const void * data, GLenum usage)) \ |
| 6867 | _SG_XMACRO(glBlendFuncSeparate, void, (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha)) \ |
| 6868 | _SG_XMACRO(glTexParameteri, void, (GLenum target, GLenum pname, GLint param)) \ |
| 6869 | _SG_XMACRO(glGetIntegerv, void, (GLenum pname, GLint * data)) \ |
| 6870 | _SG_XMACRO(glEnable, void, (GLenum cap)) \ |
| 6871 | _SG_XMACRO(glBlitFramebuffer, void, (GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter)) \ |
| 6872 | _SG_XMACRO(glStencilMask, void, (GLuint mask)) \ |
| 6873 | _SG_XMACRO(glAttachShader, void, (GLuint program, GLuint shader)) \ |
| 6874 | _SG_XMACRO(glGetError, GLenum, (void)) \ |
| 6875 | _SG_XMACRO(glBlendColor, void, (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)) \ |
| 6876 | _SG_XMACRO(glTexParameterf, void, (GLenum target, GLenum pname, GLfloat param)) \ |
| 6877 | _SG_XMACRO(glTexParameterfv, void, (GLenum target, GLenum pname, const GLfloat* params)) \ |
| 6878 | _SG_XMACRO(glGetShaderInfoLog, void, (GLuint shader, GLsizei bufSize, GLsizei * length, GLchar * infoLog)) \ |
| 6879 | _SG_XMACRO(glDepthFunc, void, (GLenum func)) \ |
| 6880 | _SG_XMACRO(glStencilOp , void, (GLenum fail, GLenum zfail, GLenum zpass)) \ |
| 6881 | _SG_XMACRO(glStencilFunc, void, (GLenum func, GLint ref, GLuint mask)) \ |
| 6882 | _SG_XMACRO(glEnableVertexAttribArray, void, (GLuint index)) \ |
| 6883 | _SG_XMACRO(glBlendFunc, void, (GLenum sfactor, GLenum dfactor)) \ |
| 6884 | _SG_XMACRO(glReadBuffer, void, (GLenum src)) \ |
| 6885 | _SG_XMACRO(glTexImage2D, void, (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void * pixels)) \ |
| 6886 | _SG_XMACRO(glGenVertexArrays, void, (GLsizei n, GLuint * arrays)) \ |
| 6887 | _SG_XMACRO(glFrontFace, void, (GLenum mode)) \ |
| 6888 | _SG_XMACRO(glCullFace, void, (GLenum mode)) \ |
| 6889 | _SG_XMACRO(glPixelStorei, void, (GLenum pname, GLint param)) \ |
| 6890 | _SG_XMACRO(glBindSampler, void, (GLuint unit, GLuint sampler)) \ |
| 6891 | _SG_XMACRO(glGenSamplers, void, (GLsizei n, GLuint* samplers)) \ |
| 6892 | _SG_XMACRO(glSamplerParameteri, void, (GLuint sampler, GLenum pname, GLint param)) \ |
| 6893 | _SG_XMACRO(glSamplerParameterf, void, (GLuint sampler, GLenum pname, GLfloat param)) \ |
| 6894 | _SG_XMACRO(glSamplerParameterfv, void, (GLuint sampler, GLenum pname, const GLfloat* params)) \ |
| 6895 | _SG_XMACRO(glDeleteSamplers, void, (GLsizei n, const GLuint* samplers)) \ |
| 6896 | _SG_XMACRO(glBindBufferBase, void, (GLenum target, GLuint index, GLuint buffer)) |
| 6897 | |
| 6898 | // generate GL function pointer typedefs |
| 6899 | #define _SG_XMACRO(name, ret, args) typedef ret (GL_APIENTRY* PFN_ ## name) args; |
| 6900 | _SG_GL_FUNCS |
| 6901 | #undef _SG_XMACRO |
| 6902 | |
| 6903 | // generate GL function pointers |
| 6904 | #define _SG_XMACRO(name, ret, args) static PFN_ ## name name; |
| 6905 | _SG_GL_FUNCS |
| 6906 | #undef _SG_XMACRO |
| 6907 | |
| 6908 | // helper function to lookup GL functions in GL DLL |
| 6909 | typedef PROC (WINAPI * _sg_wglGetProcAddress)(LPCSTR); |
| 6910 | _SOKOL_PRIVATE void* _sg_gl_getprocaddr(const char* name, _sg_wglGetProcAddress wgl_getprocaddress) { |
| 6911 | void* proc_addr = (void*) wgl_getprocaddress(name); |
| 6912 | if (0 == proc_addr) { |
| 6913 | proc_addr = (void*) GetProcAddress(_sg.gl.opengl32_dll, name); |
| 6914 | } |
| 6915 | SOKOL_ASSERT(proc_addr); |
| 6916 | return proc_addr; |
| 6917 | } |
| 6918 | |
| 6919 | // populate GL function pointers |
| 6920 | _SOKOL_PRIVATE void _sg_gl_load_opengl(void) { |
| 6921 | SOKOL_ASSERT(0 == _sg.gl.opengl32_dll); |
| 6922 | _sg.gl.opengl32_dll = LoadLibraryA("opengl32.dll"); |
| 6923 | SOKOL_ASSERT(_sg.gl.opengl32_dll); |
| 6924 | _sg_wglGetProcAddress wgl_getprocaddress = (_sg_wglGetProcAddress) GetProcAddress(_sg.gl.opengl32_dll, "wglGetProcAddress"); |
| 6925 | SOKOL_ASSERT(wgl_getprocaddress); |
| 6926 | #define _SG_XMACRO(name, ret, args) name = (PFN_ ## name) _sg_gl_getprocaddr(#name, wgl_getprocaddress); |
| 6927 | _SG_GL_FUNCS |
| 6928 | #undef _SG_XMACRO |
| 6929 | } |
| 6930 | |
| 6931 | _SOKOL_PRIVATE void _sg_gl_unload_opengl(void) { |
| 6932 | SOKOL_ASSERT(_sg.gl.opengl32_dll); |
| 6933 | FreeLibrary(_sg.gl.opengl32_dll); |
| 6934 | _sg.gl.opengl32_dll = 0; |
| 6935 | } |
| 6936 | #endif // _SOKOL_USE_WIN32_GL_LOADER |
| 6937 | |
| 6938 | //-- type translation ---------------------------------------------------------- |
| 6939 | _SOKOL_PRIVATE GLenum _sg_gl_buffer_target(sg_buffer_type t) { |
| 6940 | switch (t) { |
| 6941 | case SG_BUFFERTYPE_VERTEXBUFFER: return GL_ARRAY_BUFFER; |
| 6942 | case SG_BUFFERTYPE_INDEXBUFFER: return GL_ELEMENT_ARRAY_BUFFER; |
| 6943 | case SG_BUFFERTYPE_STORAGEBUFFER: return GL_SHADER_STORAGE_BUFFER; |
| 6944 | default: SOKOL_UNREACHABLE; return 0; |
| 6945 | } |
| 6946 | } |
| 6947 | |
| 6948 | _SOKOL_PRIVATE GLenum _sg_gl_texture_target(sg_image_type t) { |
| 6949 | switch (t) { |
| 6950 | case SG_IMAGETYPE_2D: return GL_TEXTURE_2D; |
| 6951 | case SG_IMAGETYPE_CUBE: return GL_TEXTURE_CUBE_MAP; |
| 6952 | case SG_IMAGETYPE_3D: return GL_TEXTURE_3D; |
| 6953 | case SG_IMAGETYPE_ARRAY: return GL_TEXTURE_2D_ARRAY; |
| 6954 | default: SOKOL_UNREACHABLE; return 0; |
| 6955 | } |
| 6956 | } |
| 6957 | |
| 6958 | _SOKOL_PRIVATE GLenum _sg_gl_usage(sg_usage u) { |
| 6959 | switch (u) { |
| 6960 | case SG_USAGE_IMMUTABLE: return GL_STATIC_DRAW; |
| 6961 | case SG_USAGE_DYNAMIC: return GL_DYNAMIC_DRAW; |
| 6962 | case SG_USAGE_STREAM: return GL_STREAM_DRAW; |
| 6963 | default: SOKOL_UNREACHABLE; return 0; |
| 6964 | } |
| 6965 | } |
| 6966 | |
| 6967 | _SOKOL_PRIVATE GLenum _sg_gl_shader_stage(sg_shader_stage stage) { |
| 6968 | switch (stage) { |
| 6969 | case SG_SHADERSTAGE_VS: return GL_VERTEX_SHADER; |
| 6970 | case SG_SHADERSTAGE_FS: return GL_FRAGMENT_SHADER; |
| 6971 | default: SOKOL_UNREACHABLE; return 0; |
| 6972 | } |
| 6973 | } |
| 6974 | |
| 6975 | _SOKOL_PRIVATE GLint _sg_gl_vertexformat_size(sg_vertex_format fmt) { |
| 6976 | switch (fmt) { |
| 6977 | case SG_VERTEXFORMAT_FLOAT: return 1; |
| 6978 | case SG_VERTEXFORMAT_FLOAT2: return 2; |
| 6979 | case SG_VERTEXFORMAT_FLOAT3: return 3; |
| 6980 | case SG_VERTEXFORMAT_FLOAT4: return 4; |
| 6981 | case SG_VERTEXFORMAT_BYTE4: return 4; |
| 6982 | case SG_VERTEXFORMAT_BYTE4N: return 4; |
| 6983 | case SG_VERTEXFORMAT_UBYTE4: return 4; |
| 6984 | case SG_VERTEXFORMAT_UBYTE4N: return 4; |
| 6985 | case SG_VERTEXFORMAT_SHORT2: return 2; |
| 6986 | case SG_VERTEXFORMAT_SHORT2N: return 2; |
| 6987 | case SG_VERTEXFORMAT_USHORT2N: return 2; |
| 6988 | case SG_VERTEXFORMAT_SHORT4: return 4; |
| 6989 | case SG_VERTEXFORMAT_SHORT4N: return 4; |
| 6990 | case SG_VERTEXFORMAT_USHORT4N: return 4; |
| 6991 | case SG_VERTEXFORMAT_UINT10_N2: return 4; |
| 6992 | case SG_VERTEXFORMAT_HALF2: return 2; |
| 6993 | case SG_VERTEXFORMAT_HALF4: return 4; |
| 6994 | default: SOKOL_UNREACHABLE; return 0; |
| 6995 | } |
| 6996 | } |
| 6997 | |
| 6998 | _SOKOL_PRIVATE GLenum _sg_gl_vertexformat_type(sg_vertex_format fmt) { |
| 6999 | switch (fmt) { |
| 7000 | case SG_VERTEXFORMAT_FLOAT: |
| 7001 | case SG_VERTEXFORMAT_FLOAT2: |
| 7002 | case SG_VERTEXFORMAT_FLOAT3: |
| 7003 | case SG_VERTEXFORMAT_FLOAT4: |
| 7004 | return GL_FLOAT; |
| 7005 | case SG_VERTEXFORMAT_BYTE4: |
| 7006 | case SG_VERTEXFORMAT_BYTE4N: |
| 7007 | return GL_BYTE; |
| 7008 | case SG_VERTEXFORMAT_UBYTE4: |
| 7009 | case SG_VERTEXFORMAT_UBYTE4N: |
| 7010 | return GL_UNSIGNED_BYTE; |
| 7011 | case SG_VERTEXFORMAT_SHORT2: |
| 7012 | case SG_VERTEXFORMAT_SHORT2N: |
| 7013 | case SG_VERTEXFORMAT_SHORT4: |
| 7014 | case SG_VERTEXFORMAT_SHORT4N: |
| 7015 | return GL_SHORT; |
| 7016 | case SG_VERTEXFORMAT_USHORT2N: |
| 7017 | case SG_VERTEXFORMAT_USHORT4N: |
| 7018 | return GL_UNSIGNED_SHORT; |
| 7019 | case SG_VERTEXFORMAT_UINT10_N2: |
| 7020 | return GL_UNSIGNED_INT_2_10_10_10_REV; |
| 7021 | case SG_VERTEXFORMAT_HALF2: |
| 7022 | case SG_VERTEXFORMAT_HALF4: |
| 7023 | return GL_HALF_FLOAT; |
| 7024 | default: |
| 7025 | SOKOL_UNREACHABLE; return 0; |
| 7026 | } |
| 7027 | } |
| 7028 | |
| 7029 | _SOKOL_PRIVATE GLboolean _sg_gl_vertexformat_normalized(sg_vertex_format fmt) { |
| 7030 | switch (fmt) { |
| 7031 | case SG_VERTEXFORMAT_BYTE4N: |
| 7032 | case SG_VERTEXFORMAT_UBYTE4N: |
| 7033 | case SG_VERTEXFORMAT_SHORT2N: |
| 7034 | case SG_VERTEXFORMAT_USHORT2N: |
| 7035 | case SG_VERTEXFORMAT_SHORT4N: |
| 7036 | case SG_VERTEXFORMAT_USHORT4N: |
| 7037 | case SG_VERTEXFORMAT_UINT10_N2: |
| 7038 | return GL_TRUE; |
| 7039 | default: |
| 7040 | return GL_FALSE; |
| 7041 | } |
| 7042 | } |
| 7043 | |
| 7044 | _SOKOL_PRIVATE GLenum _sg_gl_primitive_type(sg_primitive_type t) { |
| 7045 | switch (t) { |
| 7046 | case SG_PRIMITIVETYPE_POINTS: return GL_POINTS; |
| 7047 | case SG_PRIMITIVETYPE_LINES: return GL_LINES; |
| 7048 | case SG_PRIMITIVETYPE_LINE_STRIP: return GL_LINE_STRIP; |
| 7049 | case SG_PRIMITIVETYPE_TRIANGLES: return GL_TRIANGLES; |
| 7050 | case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return GL_TRIANGLE_STRIP; |
| 7051 | default: SOKOL_UNREACHABLE; return 0; |
| 7052 | } |
| 7053 | } |
| 7054 | |
| 7055 | _SOKOL_PRIVATE GLenum _sg_gl_index_type(sg_index_type t) { |
| 7056 | switch (t) { |
| 7057 | case SG_INDEXTYPE_NONE: return 0; |
| 7058 | case SG_INDEXTYPE_UINT16: return GL_UNSIGNED_SHORT; |
| 7059 | case SG_INDEXTYPE_UINT32: return GL_UNSIGNED_INT; |
| 7060 | default: SOKOL_UNREACHABLE; return 0; |
| 7061 | } |
| 7062 | } |
| 7063 | |
| 7064 | _SOKOL_PRIVATE GLenum _sg_gl_compare_func(sg_compare_func cmp) { |
| 7065 | switch (cmp) { |
| 7066 | case SG_COMPAREFUNC_NEVER: return GL_NEVER; |
| 7067 | case SG_COMPAREFUNC_LESS: return GL_LESS; |
| 7068 | case SG_COMPAREFUNC_EQUAL: return GL_EQUAL; |
| 7069 | case SG_COMPAREFUNC_LESS_EQUAL: return GL_LEQUAL; |
| 7070 | case SG_COMPAREFUNC_GREATER: return GL_GREATER; |
| 7071 | case SG_COMPAREFUNC_NOT_EQUAL: return GL_NOTEQUAL; |
| 7072 | case SG_COMPAREFUNC_GREATER_EQUAL: return GL_GEQUAL; |
| 7073 | case SG_COMPAREFUNC_ALWAYS: return GL_ALWAYS; |
| 7074 | default: SOKOL_UNREACHABLE; return 0; |
| 7075 | } |
| 7076 | } |
| 7077 | |
| 7078 | _SOKOL_PRIVATE GLenum _sg_gl_stencil_op(sg_stencil_op op) { |
| 7079 | switch (op) { |
| 7080 | case SG_STENCILOP_KEEP: return GL_KEEP; |
| 7081 | case SG_STENCILOP_ZERO: return GL_ZERO; |
| 7082 | case SG_STENCILOP_REPLACE: return GL_REPLACE; |
| 7083 | case SG_STENCILOP_INCR_CLAMP: return GL_INCR; |
| 7084 | case SG_STENCILOP_DECR_CLAMP: return GL_DECR; |
| 7085 | case SG_STENCILOP_INVERT: return GL_INVERT; |
| 7086 | case SG_STENCILOP_INCR_WRAP: return GL_INCR_WRAP; |
| 7087 | case SG_STENCILOP_DECR_WRAP: return GL_DECR_WRAP; |
| 7088 | default: SOKOL_UNREACHABLE; return 0; |
| 7089 | } |
| 7090 | } |
| 7091 | |
| 7092 | _SOKOL_PRIVATE GLenum _sg_gl_blend_factor(sg_blend_factor f) { |
| 7093 | switch (f) { |
| 7094 | case SG_BLENDFACTOR_ZERO: return GL_ZERO; |
| 7095 | case SG_BLENDFACTOR_ONE: return GL_ONE; |
| 7096 | case SG_BLENDFACTOR_SRC_COLOR: return GL_SRC_COLOR; |
| 7097 | case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return GL_ONE_MINUS_SRC_COLOR; |
| 7098 | case SG_BLENDFACTOR_SRC_ALPHA: return GL_SRC_ALPHA; |
| 7099 | case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return GL_ONE_MINUS_SRC_ALPHA; |
| 7100 | case SG_BLENDFACTOR_DST_COLOR: return GL_DST_COLOR; |
| 7101 | case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return GL_ONE_MINUS_DST_COLOR; |
| 7102 | case SG_BLENDFACTOR_DST_ALPHA: return GL_DST_ALPHA; |
| 7103 | case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return GL_ONE_MINUS_DST_ALPHA; |
| 7104 | case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return GL_SRC_ALPHA_SATURATE; |
| 7105 | case SG_BLENDFACTOR_BLEND_COLOR: return GL_CONSTANT_COLOR; |
| 7106 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return GL_ONE_MINUS_CONSTANT_COLOR; |
| 7107 | case SG_BLENDFACTOR_BLEND_ALPHA: return GL_CONSTANT_ALPHA; |
| 7108 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return GL_ONE_MINUS_CONSTANT_ALPHA; |
| 7109 | default: SOKOL_UNREACHABLE; return 0; |
| 7110 | } |
| 7111 | } |
| 7112 | |
| 7113 | _SOKOL_PRIVATE GLenum _sg_gl_blend_op(sg_blend_op op) { |
| 7114 | switch (op) { |
| 7115 | case SG_BLENDOP_ADD: return GL_FUNC_ADD; |
| 7116 | case SG_BLENDOP_SUBTRACT: return GL_FUNC_SUBTRACT; |
| 7117 | case SG_BLENDOP_REVERSE_SUBTRACT: return GL_FUNC_REVERSE_SUBTRACT; |
| 7118 | default: SOKOL_UNREACHABLE; return 0; |
| 7119 | } |
| 7120 | } |
| 7121 | |
| 7122 | _SOKOL_PRIVATE GLenum _sg_gl_min_filter(sg_filter min_f, sg_filter mipmap_f) { |
| 7123 | if (min_f == SG_FILTER_NEAREST) { |
| 7124 | switch (mipmap_f) { |
| 7125 | case SG_FILTER_NONE: return GL_NEAREST; |
| 7126 | case SG_FILTER_NEAREST: return GL_NEAREST_MIPMAP_NEAREST; |
| 7127 | case SG_FILTER_LINEAR: return GL_NEAREST_MIPMAP_LINEAR; |
| 7128 | default: SOKOL_UNREACHABLE; return (GLenum)0; |
| 7129 | } |
| 7130 | } else if (min_f == SG_FILTER_LINEAR) { |
| 7131 | switch (mipmap_f) { |
| 7132 | case SG_FILTER_NONE: return GL_LINEAR; |
| 7133 | case SG_FILTER_NEAREST: return GL_LINEAR_MIPMAP_NEAREST; |
| 7134 | case SG_FILTER_LINEAR: return GL_LINEAR_MIPMAP_LINEAR; |
| 7135 | default: SOKOL_UNREACHABLE; return (GLenum)0; |
| 7136 | } |
| 7137 | } else { |
| 7138 | SOKOL_UNREACHABLE; return (GLenum)0; |
| 7139 | } |
| 7140 | } |
| 7141 | |
| 7142 | _SOKOL_PRIVATE GLenum _sg_gl_mag_filter(sg_filter mag_f) { |
| 7143 | if (mag_f == SG_FILTER_NEAREST) { |
| 7144 | return GL_NEAREST; |
| 7145 | } else { |
| 7146 | return GL_LINEAR; |
| 7147 | } |
| 7148 | } |
| 7149 | |
| 7150 | _SOKOL_PRIVATE GLenum _sg_gl_wrap(sg_wrap w) { |
| 7151 | switch (w) { |
| 7152 | case SG_WRAP_CLAMP_TO_EDGE: return GL_CLAMP_TO_EDGE; |
| 7153 | #if defined(SOKOL_GLCORE) |
| 7154 | case SG_WRAP_CLAMP_TO_BORDER: return GL_CLAMP_TO_BORDER; |
| 7155 | #else |
| 7156 | case SG_WRAP_CLAMP_TO_BORDER: return GL_CLAMP_TO_EDGE; |
| 7157 | #endif |
| 7158 | case SG_WRAP_REPEAT: return GL_REPEAT; |
| 7159 | case SG_WRAP_MIRRORED_REPEAT: return GL_MIRRORED_REPEAT; |
| 7160 | default: SOKOL_UNREACHABLE; return 0; |
| 7161 | } |
| 7162 | } |
| 7163 | |
| 7164 | _SOKOL_PRIVATE GLenum _sg_gl_teximage_type(sg_pixel_format fmt) { |
| 7165 | switch (fmt) { |
| 7166 | case SG_PIXELFORMAT_R8: |
| 7167 | case SG_PIXELFORMAT_R8UI: |
| 7168 | case SG_PIXELFORMAT_RG8: |
| 7169 | case SG_PIXELFORMAT_RG8UI: |
| 7170 | case SG_PIXELFORMAT_RGBA8: |
| 7171 | case SG_PIXELFORMAT_SRGB8A8: |
| 7172 | case SG_PIXELFORMAT_RGBA8UI: |
| 7173 | case SG_PIXELFORMAT_BGRA8: |
| 7174 | return GL_UNSIGNED_BYTE; |
| 7175 | case SG_PIXELFORMAT_R8SN: |
| 7176 | case SG_PIXELFORMAT_R8SI: |
| 7177 | case SG_PIXELFORMAT_RG8SN: |
| 7178 | case SG_PIXELFORMAT_RG8SI: |
| 7179 | case SG_PIXELFORMAT_RGBA8SN: |
| 7180 | case SG_PIXELFORMAT_RGBA8SI: |
| 7181 | return GL_BYTE; |
| 7182 | case SG_PIXELFORMAT_R16: |
| 7183 | case SG_PIXELFORMAT_R16UI: |
| 7184 | case SG_PIXELFORMAT_RG16: |
| 7185 | case SG_PIXELFORMAT_RG16UI: |
| 7186 | case SG_PIXELFORMAT_RGBA16: |
| 7187 | case SG_PIXELFORMAT_RGBA16UI: |
| 7188 | return GL_UNSIGNED_SHORT; |
| 7189 | case SG_PIXELFORMAT_R16SN: |
| 7190 | case SG_PIXELFORMAT_R16SI: |
| 7191 | case SG_PIXELFORMAT_RG16SN: |
| 7192 | case SG_PIXELFORMAT_RG16SI: |
| 7193 | case SG_PIXELFORMAT_RGBA16SN: |
| 7194 | case SG_PIXELFORMAT_RGBA16SI: |
| 7195 | return GL_SHORT; |
| 7196 | case SG_PIXELFORMAT_R16F: |
| 7197 | case SG_PIXELFORMAT_RG16F: |
| 7198 | case SG_PIXELFORMAT_RGBA16F: |
| 7199 | return GL_HALF_FLOAT; |
| 7200 | case SG_PIXELFORMAT_R32UI: |
| 7201 | case SG_PIXELFORMAT_RG32UI: |
| 7202 | case SG_PIXELFORMAT_RGBA32UI: |
| 7203 | return GL_UNSIGNED_INT; |
| 7204 | case SG_PIXELFORMAT_R32SI: |
| 7205 | case SG_PIXELFORMAT_RG32SI: |
| 7206 | case SG_PIXELFORMAT_RGBA32SI: |
| 7207 | return GL_INT; |
| 7208 | case SG_PIXELFORMAT_R32F: |
| 7209 | case SG_PIXELFORMAT_RG32F: |
| 7210 | case SG_PIXELFORMAT_RGBA32F: |
| 7211 | return GL_FLOAT; |
| 7212 | case SG_PIXELFORMAT_RGB10A2: |
| 7213 | return GL_UNSIGNED_INT_2_10_10_10_REV; |
| 7214 | case SG_PIXELFORMAT_RG11B10F: |
| 7215 | return GL_UNSIGNED_INT_10F_11F_11F_REV; |
| 7216 | case SG_PIXELFORMAT_RGB9E5: |
| 7217 | return GL_UNSIGNED_INT_5_9_9_9_REV; |
| 7218 | case SG_PIXELFORMAT_DEPTH: |
| 7219 | return GL_FLOAT; |
| 7220 | case SG_PIXELFORMAT_DEPTH_STENCIL: |
| 7221 | return GL_UNSIGNED_INT_24_8; |
| 7222 | default: |
| 7223 | SOKOL_UNREACHABLE; return 0; |
| 7224 | } |
| 7225 | } |
| 7226 | |
| 7227 | _SOKOL_PRIVATE GLenum _sg_gl_teximage_format(sg_pixel_format fmt) { |
| 7228 | switch (fmt) { |
| 7229 | case SG_PIXELFORMAT_R8: |
| 7230 | case SG_PIXELFORMAT_R8SN: |
| 7231 | case SG_PIXELFORMAT_R16: |
| 7232 | case SG_PIXELFORMAT_R16SN: |
| 7233 | case SG_PIXELFORMAT_R16F: |
| 7234 | case SG_PIXELFORMAT_R32F: |
| 7235 | return GL_RED; |
| 7236 | case SG_PIXELFORMAT_R8UI: |
| 7237 | case SG_PIXELFORMAT_R8SI: |
| 7238 | case SG_PIXELFORMAT_R16UI: |
| 7239 | case SG_PIXELFORMAT_R16SI: |
| 7240 | case SG_PIXELFORMAT_R32UI: |
| 7241 | case SG_PIXELFORMAT_R32SI: |
| 7242 | return GL_RED_INTEGER; |
| 7243 | case SG_PIXELFORMAT_RG8: |
| 7244 | case SG_PIXELFORMAT_RG8SN: |
| 7245 | case SG_PIXELFORMAT_RG16: |
| 7246 | case SG_PIXELFORMAT_RG16SN: |
| 7247 | case SG_PIXELFORMAT_RG16F: |
| 7248 | case SG_PIXELFORMAT_RG32F: |
| 7249 | return GL_RG; |
| 7250 | case SG_PIXELFORMAT_RG8UI: |
| 7251 | case SG_PIXELFORMAT_RG8SI: |
| 7252 | case SG_PIXELFORMAT_RG16UI: |
| 7253 | case SG_PIXELFORMAT_RG16SI: |
| 7254 | case SG_PIXELFORMAT_RG32UI: |
| 7255 | case SG_PIXELFORMAT_RG32SI: |
| 7256 | return GL_RG_INTEGER; |
| 7257 | case SG_PIXELFORMAT_RGBA8: |
| 7258 | case SG_PIXELFORMAT_SRGB8A8: |
| 7259 | case SG_PIXELFORMAT_RGBA8SN: |
| 7260 | case SG_PIXELFORMAT_RGBA16: |
| 7261 | case SG_PIXELFORMAT_RGBA16SN: |
| 7262 | case SG_PIXELFORMAT_RGBA16F: |
| 7263 | case SG_PIXELFORMAT_RGBA32F: |
| 7264 | case SG_PIXELFORMAT_RGB10A2: |
| 7265 | return GL_RGBA; |
| 7266 | case SG_PIXELFORMAT_RGBA8UI: |
| 7267 | case SG_PIXELFORMAT_RGBA8SI: |
| 7268 | case SG_PIXELFORMAT_RGBA16UI: |
| 7269 | case SG_PIXELFORMAT_RGBA16SI: |
| 7270 | case SG_PIXELFORMAT_RGBA32UI: |
| 7271 | case SG_PIXELFORMAT_RGBA32SI: |
| 7272 | return GL_RGBA_INTEGER; |
| 7273 | case SG_PIXELFORMAT_RG11B10F: |
| 7274 | case SG_PIXELFORMAT_RGB9E5: |
| 7275 | return GL_RGB; |
| 7276 | case SG_PIXELFORMAT_DEPTH: |
| 7277 | return GL_DEPTH_COMPONENT; |
| 7278 | case SG_PIXELFORMAT_DEPTH_STENCIL: |
| 7279 | return GL_DEPTH_STENCIL; |
| 7280 | case SG_PIXELFORMAT_BC1_RGBA: |
| 7281 | return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
| 7282 | case SG_PIXELFORMAT_BC2_RGBA: |
| 7283 | return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; |
| 7284 | case SG_PIXELFORMAT_BC3_RGBA: |
| 7285 | return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; |
| 7286 | case SG_PIXELFORMAT_BC3_SRGBA: |
| 7287 | return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT; |
| 7288 | case SG_PIXELFORMAT_BC4_R: |
| 7289 | return GL_COMPRESSED_RED_RGTC1; |
| 7290 | case SG_PIXELFORMAT_BC4_RSN: |
| 7291 | return GL_COMPRESSED_SIGNED_RED_RGTC1; |
| 7292 | case SG_PIXELFORMAT_BC5_RG: |
| 7293 | return GL_COMPRESSED_RED_GREEN_RGTC2; |
| 7294 | case SG_PIXELFORMAT_BC5_RGSN: |
| 7295 | return GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2; |
| 7296 | case SG_PIXELFORMAT_BC6H_RGBF: |
| 7297 | return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB; |
| 7298 | case SG_PIXELFORMAT_BC6H_RGBUF: |
| 7299 | return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB; |
| 7300 | case SG_PIXELFORMAT_BC7_RGBA: |
| 7301 | return GL_COMPRESSED_RGBA_BPTC_UNORM_ARB; |
| 7302 | case SG_PIXELFORMAT_BC7_SRGBA: |
| 7303 | return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB; |
| 7304 | case SG_PIXELFORMAT_PVRTC_RGB_2BPP: |
| 7305 | return GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG; |
| 7306 | case SG_PIXELFORMAT_PVRTC_RGB_4BPP: |
| 7307 | return GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG; |
| 7308 | case SG_PIXELFORMAT_PVRTC_RGBA_2BPP: |
| 7309 | return GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; |
| 7310 | case SG_PIXELFORMAT_PVRTC_RGBA_4BPP: |
| 7311 | return GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; |
| 7312 | case SG_PIXELFORMAT_ETC2_RGB8: |
| 7313 | return GL_COMPRESSED_RGB8_ETC2; |
| 7314 | case SG_PIXELFORMAT_ETC2_SRGB8: |
| 7315 | return GL_COMPRESSED_SRGB8_ETC2; |
| 7316 | case SG_PIXELFORMAT_ETC2_RGB8A1: |
| 7317 | return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2; |
| 7318 | case SG_PIXELFORMAT_ETC2_RGBA8: |
| 7319 | return GL_COMPRESSED_RGBA8_ETC2_EAC; |
| 7320 | case SG_PIXELFORMAT_ETC2_SRGB8A8: |
| 7321 | return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC; |
| 7322 | case SG_PIXELFORMAT_ETC2_RG11: |
| 7323 | return GL_COMPRESSED_RG11_EAC; |
| 7324 | case SG_PIXELFORMAT_ETC2_RG11SN: |
| 7325 | return GL_COMPRESSED_SIGNED_RG11_EAC; |
| 7326 | case SG_PIXELFORMAT_ASTC_4x4_RGBA: |
| 7327 | return GL_COMPRESSED_RGBA_ASTC_4x4_KHR; |
| 7328 | case SG_PIXELFORMAT_ASTC_4x4_SRGBA: |
| 7329 | return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR; |
| 7330 | default: |
| 7331 | SOKOL_UNREACHABLE; return 0; |
| 7332 | } |
| 7333 | } |
| 7334 | |
| 7335 | _SOKOL_PRIVATE GLenum _sg_gl_teximage_internal_format(sg_pixel_format fmt) { |
| 7336 | switch (fmt) { |
| 7337 | case SG_PIXELFORMAT_R8: return GL_R8; |
| 7338 | case SG_PIXELFORMAT_R8SN: return GL_R8_SNORM; |
| 7339 | case SG_PIXELFORMAT_R8UI: return GL_R8UI; |
| 7340 | case SG_PIXELFORMAT_R8SI: return GL_R8I; |
| 7341 | #if !defined(SOKOL_GLES3) |
| 7342 | case SG_PIXELFORMAT_R16: return GL_R16; |
| 7343 | case SG_PIXELFORMAT_R16SN: return GL_R16_SNORM; |
| 7344 | #endif |
| 7345 | case SG_PIXELFORMAT_R16UI: return GL_R16UI; |
| 7346 | case SG_PIXELFORMAT_R16SI: return GL_R16I; |
| 7347 | case SG_PIXELFORMAT_R16F: return GL_R16F; |
| 7348 | case SG_PIXELFORMAT_RG8: return GL_RG8; |
| 7349 | case SG_PIXELFORMAT_RG8SN: return GL_RG8_SNORM; |
| 7350 | case SG_PIXELFORMAT_RG8UI: return GL_RG8UI; |
| 7351 | case SG_PIXELFORMAT_RG8SI: return GL_RG8I; |
| 7352 | case SG_PIXELFORMAT_R32UI: return GL_R32UI; |
| 7353 | case SG_PIXELFORMAT_R32SI: return GL_R32I; |
| 7354 | case SG_PIXELFORMAT_R32F: return GL_R32F; |
| 7355 | #if !defined(SOKOL_GLES3) |
| 7356 | case SG_PIXELFORMAT_RG16: return GL_RG16; |
| 7357 | case SG_PIXELFORMAT_RG16SN: return GL_RG16_SNORM; |
| 7358 | #endif |
| 7359 | case SG_PIXELFORMAT_RG16UI: return GL_RG16UI; |
| 7360 | case SG_PIXELFORMAT_RG16SI: return GL_RG16I; |
| 7361 | case SG_PIXELFORMAT_RG16F: return GL_RG16F; |
| 7362 | case SG_PIXELFORMAT_RGBA8: return GL_RGBA8; |
| 7363 | case SG_PIXELFORMAT_SRGB8A8: return GL_SRGB8_ALPHA8; |
| 7364 | case SG_PIXELFORMAT_RGBA8SN: return GL_RGBA8_SNORM; |
| 7365 | case SG_PIXELFORMAT_RGBA8UI: return GL_RGBA8UI; |
| 7366 | case SG_PIXELFORMAT_RGBA8SI: return GL_RGBA8I; |
| 7367 | case SG_PIXELFORMAT_RGB10A2: return GL_RGB10_A2; |
| 7368 | case SG_PIXELFORMAT_RG11B10F: return GL_R11F_G11F_B10F; |
| 7369 | case SG_PIXELFORMAT_RGB9E5: return GL_RGB9_E5; |
| 7370 | case SG_PIXELFORMAT_RG32UI: return GL_RG32UI; |
| 7371 | case SG_PIXELFORMAT_RG32SI: return GL_RG32I; |
| 7372 | case SG_PIXELFORMAT_RG32F: return GL_RG32F; |
| 7373 | #if !defined(SOKOL_GLES3) |
| 7374 | case SG_PIXELFORMAT_RGBA16: return GL_RGBA16; |
| 7375 | case SG_PIXELFORMAT_RGBA16SN: return GL_RGBA16_SNORM; |
| 7376 | #endif |
| 7377 | case SG_PIXELFORMAT_RGBA16UI: return GL_RGBA16UI; |
| 7378 | case SG_PIXELFORMAT_RGBA16SI: return GL_RGBA16I; |
| 7379 | case SG_PIXELFORMAT_RGBA16F: return GL_RGBA16F; |
| 7380 | case SG_PIXELFORMAT_RGBA32UI: return GL_RGBA32UI; |
| 7381 | case SG_PIXELFORMAT_RGBA32SI: return GL_RGBA32I; |
| 7382 | case SG_PIXELFORMAT_RGBA32F: return GL_RGBA32F; |
| 7383 | case SG_PIXELFORMAT_DEPTH: return GL_DEPTH_COMPONENT32F; |
| 7384 | case SG_PIXELFORMAT_DEPTH_STENCIL: return GL_DEPTH24_STENCIL8; |
| 7385 | case SG_PIXELFORMAT_BC1_RGBA: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; |
| 7386 | case SG_PIXELFORMAT_BC2_RGBA: return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; |
| 7387 | case SG_PIXELFORMAT_BC3_RGBA: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; |
| 7388 | case SG_PIXELFORMAT_BC3_SRGBA: return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT; |
| 7389 | case SG_PIXELFORMAT_BC4_R: return GL_COMPRESSED_RED_RGTC1; |
| 7390 | case SG_PIXELFORMAT_BC4_RSN: return GL_COMPRESSED_SIGNED_RED_RGTC1; |
| 7391 | case SG_PIXELFORMAT_BC5_RG: return GL_COMPRESSED_RED_GREEN_RGTC2; |
| 7392 | case SG_PIXELFORMAT_BC5_RGSN: return GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2; |
| 7393 | case SG_PIXELFORMAT_BC6H_RGBF: return GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB; |
| 7394 | case SG_PIXELFORMAT_BC6H_RGBUF: return GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB; |
| 7395 | case SG_PIXELFORMAT_BC7_RGBA: return GL_COMPRESSED_RGBA_BPTC_UNORM_ARB; |
| 7396 | case SG_PIXELFORMAT_BC7_SRGBA: return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB; |
| 7397 | case SG_PIXELFORMAT_PVRTC_RGB_2BPP: return GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG; |
| 7398 | case SG_PIXELFORMAT_PVRTC_RGB_4BPP: return GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG; |
| 7399 | case SG_PIXELFORMAT_PVRTC_RGBA_2BPP: return GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; |
| 7400 | case SG_PIXELFORMAT_PVRTC_RGBA_4BPP: return GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; |
| 7401 | case SG_PIXELFORMAT_ETC2_RGB8: return GL_COMPRESSED_RGB8_ETC2; |
| 7402 | case SG_PIXELFORMAT_ETC2_SRGB8: return GL_COMPRESSED_SRGB8_ETC2; |
| 7403 | case SG_PIXELFORMAT_ETC2_RGB8A1: return GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2; |
| 7404 | case SG_PIXELFORMAT_ETC2_RGBA8: return GL_COMPRESSED_RGBA8_ETC2_EAC; |
| 7405 | case SG_PIXELFORMAT_ETC2_SRGB8A8: return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC; |
| 7406 | case SG_PIXELFORMAT_ETC2_RG11: return GL_COMPRESSED_RG11_EAC; |
| 7407 | case SG_PIXELFORMAT_ETC2_RG11SN: return GL_COMPRESSED_SIGNED_RG11_EAC; |
| 7408 | case SG_PIXELFORMAT_ASTC_4x4_RGBA: return GL_COMPRESSED_RGBA_ASTC_4x4_KHR; |
| 7409 | case SG_PIXELFORMAT_ASTC_4x4_SRGBA: return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR; |
| 7410 | default: SOKOL_UNREACHABLE; return 0; |
| 7411 | } |
| 7412 | } |
| 7413 | |
| 7414 | _SOKOL_PRIVATE GLenum _sg_gl_cubeface_target(int face_index) { |
| 7415 | switch (face_index) { |
| 7416 | case 0: return GL_TEXTURE_CUBE_MAP_POSITIVE_X; |
| 7417 | case 1: return GL_TEXTURE_CUBE_MAP_NEGATIVE_X; |
| 7418 | case 2: return GL_TEXTURE_CUBE_MAP_POSITIVE_Y; |
| 7419 | case 3: return GL_TEXTURE_CUBE_MAP_NEGATIVE_Y; |
| 7420 | case 4: return GL_TEXTURE_CUBE_MAP_POSITIVE_Z; |
| 7421 | case 5: return GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; |
| 7422 | default: SOKOL_UNREACHABLE; return 0; |
| 7423 | } |
| 7424 | } |
| 7425 | |
| 7426 | // see: https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml |
| 7427 | _SOKOL_PRIVATE void _sg_gl_init_pixelformats(bool has_bgra) { |
| 7428 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R8]); |
| 7429 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R8SN]); |
| 7430 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8UI]); |
| 7431 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8SI]); |
| 7432 | #if !defined(SOKOL_GLES3) |
| 7433 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16]); |
| 7434 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16SN]); |
| 7435 | #endif |
| 7436 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16UI]); |
| 7437 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16SI]); |
| 7438 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG8]); |
| 7439 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG8SN]); |
| 7440 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8UI]); |
| 7441 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8SI]); |
| 7442 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32UI]); |
| 7443 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32SI]); |
| 7444 | #if !defined(SOKOL_GLES3) |
| 7445 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16]); |
| 7446 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16SN]); |
| 7447 | #endif |
| 7448 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16UI]); |
| 7449 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16SI]); |
| 7450 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA8]); |
| 7451 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_SRGB8A8]); |
| 7452 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA8SN]); |
| 7453 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8UI]); |
| 7454 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8SI]); |
| 7455 | if (has_bgra) { |
| 7456 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_BGRA8]); |
| 7457 | } |
| 7458 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGB10A2]); |
| 7459 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG11B10F]); |
| 7460 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGB9E5]); |
| 7461 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG32UI]); |
| 7462 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG32SI]); |
| 7463 | #if !defined(SOKOL_GLES3) |
| 7464 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16]); |
| 7465 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16SN]); |
| 7466 | #endif |
| 7467 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16UI]); |
| 7468 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16SI]); |
| 7469 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA32UI]); |
| 7470 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA32SI]); |
| 7471 | _sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH]); |
| 7472 | _sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH_STENCIL]); |
| 7473 | } |
| 7474 | |
| 7475 | // FIXME: OES_half_float_blend |
| 7476 | _SOKOL_PRIVATE void _sg_gl_init_pixelformats_half_float(bool has_colorbuffer_half_float) { |
| 7477 | if (has_colorbuffer_half_float) { |
| 7478 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16F]); |
| 7479 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16F]); |
| 7480 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16F]); |
| 7481 | } else { |
| 7482 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R16F]); |
| 7483 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG16F]); |
| 7484 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA16F]); |
| 7485 | } |
| 7486 | } |
| 7487 | |
| 7488 | _SOKOL_PRIVATE void _sg_gl_init_pixelformats_float(bool has_colorbuffer_float, bool has_texture_float_linear, bool has_float_blend) { |
| 7489 | if (has_texture_float_linear) { |
| 7490 | if (has_colorbuffer_float) { |
| 7491 | if (has_float_blend) { |
| 7492 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R32F]); |
| 7493 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG32F]); |
| 7494 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA32F]); |
| 7495 | } else { |
| 7496 | _sg_pixelformat_sfrm(&_sg.formats[SG_PIXELFORMAT_R32F]); |
| 7497 | _sg_pixelformat_sfrm(&_sg.formats[SG_PIXELFORMAT_RG32F]); |
| 7498 | _sg_pixelformat_sfrm(&_sg.formats[SG_PIXELFORMAT_RGBA32F]); |
| 7499 | } |
| 7500 | } else { |
| 7501 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R32F]); |
| 7502 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG32F]); |
| 7503 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA32F]); |
| 7504 | } |
| 7505 | } else { |
| 7506 | if (has_colorbuffer_float) { |
| 7507 | _sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_R32F]); |
| 7508 | _sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_RG32F]); |
| 7509 | _sg_pixelformat_sbrm(&_sg.formats[SG_PIXELFORMAT_RGBA32F]); |
| 7510 | } else { |
| 7511 | _sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_R32F]); |
| 7512 | _sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_RG32F]); |
| 7513 | _sg_pixelformat_s(&_sg.formats[SG_PIXELFORMAT_RGBA32F]); |
| 7514 | } |
| 7515 | } |
| 7516 | } |
| 7517 | |
| 7518 | _SOKOL_PRIVATE void _sg_gl_init_pixelformats_s3tc(void) { |
| 7519 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC1_RGBA]); |
| 7520 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC2_RGBA]); |
| 7521 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_RGBA]); |
| 7522 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_SRGBA]); |
| 7523 | } |
| 7524 | |
| 7525 | _SOKOL_PRIVATE void _sg_gl_init_pixelformats_rgtc(void) { |
| 7526 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_R]); |
| 7527 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_RSN]); |
| 7528 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RG]); |
| 7529 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RGSN]); |
| 7530 | } |
| 7531 | |
| 7532 | _SOKOL_PRIVATE void _sg_gl_init_pixelformats_bptc(void) { |
| 7533 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBF]); |
| 7534 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBUF]); |
| 7535 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_RGBA]); |
| 7536 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_SRGBA]); |
| 7537 | } |
| 7538 | |
| 7539 | _SOKOL_PRIVATE void _sg_gl_init_pixelformats_pvrtc(void) { |
| 7540 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGB_2BPP]); |
| 7541 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGB_4BPP]); |
| 7542 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGBA_2BPP]); |
| 7543 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGBA_4BPP]); |
| 7544 | } |
| 7545 | |
| 7546 | _SOKOL_PRIVATE void _sg_gl_init_pixelformats_etc2(void) { |
| 7547 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8]); |
| 7548 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_SRGB8]); |
| 7549 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8A1]); |
| 7550 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGBA8]); |
| 7551 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_SRGB8A8]); |
| 7552 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RG11]); |
| 7553 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RG11SN]); |
| 7554 | } |
| 7555 | |
| 7556 | _SOKOL_PRIVATE void _sg_gl_init_pixelformats_astc(void) { |
| 7557 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ASTC_4x4_RGBA]); |
| 7558 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ASTC_4x4_SRGBA]); |
| 7559 | } |
| 7560 | |
| 7561 | _SOKOL_PRIVATE void _sg_gl_init_limits(void) { |
| 7562 | _SG_GL_CHECK_ERROR(); |
| 7563 | GLint gl_int; |
| 7564 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &gl_int); |
| 7565 | _SG_GL_CHECK_ERROR(); |
| 7566 | _sg.limits.max_image_size_2d = gl_int; |
| 7567 | _sg.limits.max_image_size_array = gl_int; |
| 7568 | glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &gl_int); |
| 7569 | _SG_GL_CHECK_ERROR(); |
| 7570 | _sg.limits.max_image_size_cube = gl_int; |
| 7571 | glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &gl_int); |
| 7572 | _SG_GL_CHECK_ERROR(); |
| 7573 | if (gl_int > SG_MAX_VERTEX_ATTRIBUTES) { |
| 7574 | gl_int = SG_MAX_VERTEX_ATTRIBUTES; |
| 7575 | } |
| 7576 | _sg.limits.max_vertex_attrs = gl_int; |
| 7577 | glGetIntegerv(GL_MAX_VERTEX_UNIFORM_COMPONENTS, &gl_int); |
| 7578 | _SG_GL_CHECK_ERROR(); |
| 7579 | _sg.limits.gl_max_vertex_uniform_components = gl_int; |
| 7580 | glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &gl_int); |
| 7581 | _SG_GL_CHECK_ERROR(); |
| 7582 | _sg.limits.max_image_size_3d = gl_int; |
| 7583 | glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, &gl_int); |
| 7584 | _SG_GL_CHECK_ERROR(); |
| 7585 | _sg.limits.max_image_array_layers = gl_int; |
| 7586 | if (_sg.gl.ext_anisotropic) { |
| 7587 | glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &gl_int); |
| 7588 | _SG_GL_CHECK_ERROR(); |
| 7589 | _sg.gl.max_anisotropy = gl_int; |
| 7590 | } else { |
| 7591 | _sg.gl.max_anisotropy = 1; |
| 7592 | } |
| 7593 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &gl_int); |
| 7594 | _SG_GL_CHECK_ERROR(); |
| 7595 | _sg.limits.gl_max_combined_texture_image_units = gl_int; |
| 7596 | } |
| 7597 | |
| 7598 | #if defined(SOKOL_GLCORE) |
| 7599 | _SOKOL_PRIVATE void _sg_gl_init_caps_glcore(void) { |
| 7600 | _sg.backend = SG_BACKEND_GLCORE; |
| 7601 | |
| 7602 | GLint major_version = 0; |
| 7603 | GLint minor_version = 0; |
| 7604 | glGetIntegerv(GL_MAJOR_VERSION, &major_version); |
| 7605 | glGetIntegerv(GL_MINOR_VERSION, &minor_version); |
| 7606 | const int version = major_version * 100 + minor_version * 10; |
| 7607 | _sg.features.origin_top_left = false; |
| 7608 | _sg.features.image_clamp_to_border = true; |
| 7609 | _sg.features.mrt_independent_blend_state = false; |
| 7610 | _sg.features.mrt_independent_write_mask = true; |
| 7611 | _sg.features.storage_buffer = version >= 430; |
| 7612 | |
| 7613 | // scan extensions |
| 7614 | bool has_s3tc = false; // BC1..BC3 |
| 7615 | bool has_rgtc = false; // BC4 and BC5 |
| 7616 | bool has_bptc = false; // BC6H and BC7 |
| 7617 | bool has_pvrtc = false; |
| 7618 | bool has_etc2 = false; |
| 7619 | bool has_astc = false; |
| 7620 | GLint num_ext = 0; |
| 7621 | glGetIntegerv(GL_NUM_EXTENSIONS, &num_ext); |
| 7622 | for (int i = 0; i < num_ext; i++) { |
| 7623 | const char* ext = (const char*) glGetStringi(GL_EXTENSIONS, (GLuint)i); |
| 7624 | if (ext) { |
| 7625 | if (strstr(ext, "_texture_compression_s3tc")) { |
| 7626 | has_s3tc = true; |
| 7627 | } else if (strstr(ext, "_texture_compression_rgtc")) { |
| 7628 | has_rgtc = true; |
| 7629 | } else if (strstr(ext, "_texture_compression_bptc")) { |
| 7630 | has_bptc = true; |
| 7631 | } else if (strstr(ext, "_texture_compression_pvrtc")) { |
| 7632 | has_pvrtc = true; |
| 7633 | } else if (strstr(ext, "_ES3_compatibility")) { |
| 7634 | has_etc2 = true; |
| 7635 | } else if (strstr(ext, "_texture_filter_anisotropic")) { |
| 7636 | _sg.gl.ext_anisotropic = true; |
| 7637 | } else if (strstr(ext, "_texture_compression_astc_ldr")) { |
| 7638 | has_astc = true; |
| 7639 | } |
| 7640 | } |
| 7641 | } |
| 7642 | |
| 7643 | // limits |
| 7644 | _sg_gl_init_limits(); |
| 7645 | |
| 7646 | // pixel formats |
| 7647 | const bool has_bgra = false; // not a bug |
| 7648 | const bool has_colorbuffer_float = true; |
| 7649 | const bool has_colorbuffer_half_float = true; |
| 7650 | const bool has_texture_float_linear = true; // FIXME??? |
| 7651 | const bool has_float_blend = true; |
| 7652 | _sg_gl_init_pixelformats(has_bgra); |
| 7653 | _sg_gl_init_pixelformats_float(has_colorbuffer_float, has_texture_float_linear, has_float_blend); |
| 7654 | _sg_gl_init_pixelformats_half_float(has_colorbuffer_half_float); |
| 7655 | if (has_s3tc) { |
| 7656 | _sg_gl_init_pixelformats_s3tc(); |
| 7657 | } |
| 7658 | if (has_rgtc) { |
| 7659 | _sg_gl_init_pixelformats_rgtc(); |
| 7660 | } |
| 7661 | if (has_bptc) { |
| 7662 | _sg_gl_init_pixelformats_bptc(); |
| 7663 | } |
| 7664 | if (has_pvrtc) { |
| 7665 | _sg_gl_init_pixelformats_pvrtc(); |
| 7666 | } |
| 7667 | if (has_etc2) { |
| 7668 | _sg_gl_init_pixelformats_etc2(); |
| 7669 | } |
| 7670 | if (has_astc) { |
| 7671 | _sg_gl_init_pixelformats_astc(); |
| 7672 | } |
| 7673 | } |
| 7674 | #endif |
| 7675 | |
| 7676 | #if defined(SOKOL_GLES3) |
| 7677 | _SOKOL_PRIVATE void _sg_gl_init_caps_gles3(void) { |
| 7678 | _sg.backend = SG_BACKEND_GLES3; |
| 7679 | |
| 7680 | _sg.features.origin_top_left = false; |
| 7681 | _sg.features.image_clamp_to_border = false; |
| 7682 | _sg.features.mrt_independent_blend_state = false; |
| 7683 | _sg.features.mrt_independent_write_mask = false; |
| 7684 | _sg.features.storage_buffer = false; |
| 7685 | |
| 7686 | bool has_s3tc = false; // BC1..BC3 |
| 7687 | bool has_rgtc = false; // BC4 and BC5 |
| 7688 | bool has_bptc = false; // BC6H and BC7 |
| 7689 | bool has_pvrtc = false; |
| 7690 | #if defined(__EMSCRIPTEN__) |
| 7691 | bool has_etc2 = false; |
| 7692 | #else |
| 7693 | bool has_etc2 = true; |
| 7694 | #endif |
| 7695 | bool has_astc = false; |
| 7696 | bool has_colorbuffer_float = false; |
| 7697 | bool has_colorbuffer_half_float = false; |
| 7698 | bool has_texture_float_linear = false; |
| 7699 | bool has_float_blend = false; |
| 7700 | GLint num_ext = 0; |
| 7701 | glGetIntegerv(GL_NUM_EXTENSIONS, &num_ext); |
| 7702 | for (int i = 0; i < num_ext; i++) { |
| 7703 | const char* ext = (const char*) glGetStringi(GL_EXTENSIONS, (GLuint)i); |
| 7704 | if (ext) { |
| 7705 | if (strstr(ext, "_texture_compression_s3tc")) { |
| 7706 | has_s3tc = true; |
| 7707 | } else if (strstr(ext, "_compressed_texture_s3tc")) { |
| 7708 | has_s3tc = true; |
| 7709 | } else if (strstr(ext, "_texture_compression_rgtc")) { |
| 7710 | has_rgtc = true; |
| 7711 | } else if (strstr(ext, "_texture_compression_bptc")) { |
| 7712 | has_bptc = true; |
| 7713 | } else if (strstr(ext, "_texture_compression_pvrtc")) { |
| 7714 | has_pvrtc = true; |
| 7715 | } else if (strstr(ext, "_compressed_texture_pvrtc")) { |
| 7716 | has_pvrtc = true; |
| 7717 | } else if (strstr(ext, "_compressed_texture_etc")) { |
| 7718 | has_etc2 = true; |
| 7719 | } else if (strstr(ext, "_compressed_texture_astc")) { |
| 7720 | has_astc = true; |
| 7721 | } else if (strstr(ext, "_color_buffer_float")) { |
| 7722 | has_colorbuffer_float = true; |
| 7723 | } else if (strstr(ext, "_color_buffer_half_float")) { |
| 7724 | has_colorbuffer_half_float = true; |
| 7725 | } else if (strstr(ext, "_texture_float_linear")) { |
| 7726 | has_texture_float_linear = true; |
| 7727 | } else if (strstr(ext, "_float_blend")) { |
| 7728 | has_float_blend = true; |
| 7729 | } else if (strstr(ext, "_texture_filter_anisotropic")) { |
| 7730 | _sg.gl.ext_anisotropic = true; |
| 7731 | } |
| 7732 | } |
| 7733 | } |
| 7734 | |
| 7735 | /* on WebGL2, color_buffer_float also includes 16-bit formats |
| 7736 | see: https://developer.mozilla.org/en-US/docs/Web/API/EXT_color_buffer_float |
| 7737 | */ |
| 7738 | #if defined(__EMSCRIPTEN__) |
| 7739 | if (!has_colorbuffer_half_float && has_colorbuffer_float) { |
| 7740 | has_colorbuffer_half_float = has_colorbuffer_float; |
| 7741 | } |
| 7742 | #endif |
| 7743 | |
| 7744 | // limits |
| 7745 | _sg_gl_init_limits(); |
| 7746 | |
| 7747 | // pixel formats |
| 7748 | const bool has_bgra = false; // not a bug |
| 7749 | _sg_gl_init_pixelformats(has_bgra); |
| 7750 | _sg_gl_init_pixelformats_float(has_colorbuffer_float, has_texture_float_linear, has_float_blend); |
| 7751 | _sg_gl_init_pixelformats_half_float(has_colorbuffer_half_float); |
| 7752 | if (has_s3tc) { |
| 7753 | _sg_gl_init_pixelformats_s3tc(); |
| 7754 | } |
| 7755 | if (has_rgtc) { |
| 7756 | _sg_gl_init_pixelformats_rgtc(); |
| 7757 | } |
| 7758 | if (has_bptc) { |
| 7759 | _sg_gl_init_pixelformats_bptc(); |
| 7760 | } |
| 7761 | if (has_pvrtc) { |
| 7762 | _sg_gl_init_pixelformats_pvrtc(); |
| 7763 | } |
| 7764 | if (has_etc2) { |
| 7765 | _sg_gl_init_pixelformats_etc2(); |
| 7766 | } |
| 7767 | if (has_astc) { |
| 7768 | _sg_gl_init_pixelformats_astc(); |
| 7769 | } |
| 7770 | } |
| 7771 | #endif |
| 7772 | |
| 7773 | //-- state cache implementation ------------------------------------------------ |
| 7774 | _SOKOL_PRIVATE GLuint _sg_gl_storagebuffer_bind_index(int stage, int slot) { |
| 7775 | SOKOL_ASSERT((stage >= 0) && (stage < SG_NUM_SHADER_STAGES)); |
| 7776 | SOKOL_ASSERT((slot >= 0) && (slot < SG_MAX_SHADERSTAGE_STORAGEBUFFERS)); |
| 7777 | return (GLuint) (stage * _SG_GL_STORAGEBUFFER_STAGE_INDEX_PITCH + slot); |
| 7778 | } |
| 7779 | |
| 7780 | _SOKOL_PRIVATE void _sg_gl_cache_clear_buffer_bindings(bool force) { |
| 7781 | if (force || (_sg.gl.cache.vertex_buffer != 0)) { |
| 7782 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 7783 | _sg.gl.cache.vertex_buffer = 0; |
| 7784 | _sg_stats_add(gl.num_bind_buffer, 1); |
| 7785 | } |
| 7786 | if (force || (_sg.gl.cache.index_buffer != 0)) { |
| 7787 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 7788 | _sg.gl.cache.index_buffer = 0; |
| 7789 | _sg_stats_add(gl.num_bind_buffer, 1); |
| 7790 | } |
| 7791 | if (force || (_sg.gl.cache.storage_buffer != 0)) { |
| 7792 | if (_sg.features.storage_buffer) { |
| 7793 | glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); |
| 7794 | } |
| 7795 | _sg.gl.cache.storage_buffer = 0; |
| 7796 | _sg_stats_add(gl.num_bind_buffer, 1); |
| 7797 | } |
| 7798 | for (int stage = 0; stage < SG_NUM_SHADER_STAGES; stage++) { |
| 7799 | for (int i = 0; i < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; i++) { |
| 7800 | if (force || (_sg.gl.cache.stage_storage_buffers[stage][i] != 0)) { |
| 7801 | const GLuint bind_index = _sg_gl_storagebuffer_bind_index(stage, i); |
| 7802 | if (_sg.features.storage_buffer) { |
| 7803 | glBindBufferBase(GL_SHADER_STORAGE_BUFFER, bind_index, 0); |
| 7804 | } |
| 7805 | _sg.gl.cache.stage_storage_buffers[stage][i] = 0; |
| 7806 | _sg_stats_add(gl.num_bind_buffer, 1); |
| 7807 | } |
| 7808 | } |
| 7809 | } |
| 7810 | } |
| 7811 | |
| 7812 | _SOKOL_PRIVATE void _sg_gl_cache_bind_buffer(GLenum target, GLuint buffer) { |
| 7813 | SOKOL_ASSERT((GL_ARRAY_BUFFER == target) || (GL_ELEMENT_ARRAY_BUFFER == target) || (GL_SHADER_STORAGE_BUFFER == target)); |
| 7814 | if (target == GL_ARRAY_BUFFER) { |
| 7815 | if (_sg.gl.cache.vertex_buffer != buffer) { |
| 7816 | _sg.gl.cache.vertex_buffer = buffer; |
| 7817 | glBindBuffer(target, buffer); |
| 7818 | _sg_stats_add(gl.num_bind_buffer, 1); |
| 7819 | } |
| 7820 | } else if (target == GL_ELEMENT_ARRAY_BUFFER) { |
| 7821 | if (_sg.gl.cache.index_buffer != buffer) { |
| 7822 | _sg.gl.cache.index_buffer = buffer; |
| 7823 | glBindBuffer(target, buffer); |
| 7824 | _sg_stats_add(gl.num_bind_buffer, 1); |
| 7825 | } |
| 7826 | } else if (target == GL_SHADER_STORAGE_BUFFER) { |
| 7827 | if (_sg.gl.cache.storage_buffer != buffer) { |
| 7828 | _sg.gl.cache.storage_buffer = buffer; |
| 7829 | if (_sg.features.storage_buffer) { |
| 7830 | glBindBuffer(target, buffer); |
| 7831 | } |
| 7832 | _sg_stats_add(gl.num_bind_buffer, 1); |
| 7833 | } |
| 7834 | } else { |
| 7835 | SOKOL_UNREACHABLE; |
| 7836 | } |
| 7837 | } |
| 7838 | |
| 7839 | _SOKOL_PRIVATE void _sg_gl_cache_bind_storage_buffer(int stage, int slot, GLuint buffer) { |
| 7840 | SOKOL_ASSERT((stage >= 0) && (stage < SG_NUM_SHADER_STAGES)); |
| 7841 | SOKOL_ASSERT((slot >= 0) && (slot < SG_MAX_SHADERSTAGE_STORAGEBUFFERS)); |
| 7842 | if (_sg.gl.cache.stage_storage_buffers[stage][slot] != buffer) { |
| 7843 | _sg.gl.cache.stage_storage_buffers[stage][slot] = buffer; |
| 7844 | _sg.gl.cache.storage_buffer = buffer; // not a bug |
| 7845 | GLuint bind_index = _sg_gl_storagebuffer_bind_index(stage, slot); |
| 7846 | if (_sg.features.storage_buffer) { |
| 7847 | glBindBufferBase(GL_SHADER_STORAGE_BUFFER, bind_index, buffer); |
| 7848 | } |
| 7849 | _sg_stats_add(gl.num_bind_buffer, 1); |
| 7850 | } |
| 7851 | } |
| 7852 | |
| 7853 | _SOKOL_PRIVATE void _sg_gl_cache_store_buffer_binding(GLenum target) { |
| 7854 | if (target == GL_ARRAY_BUFFER) { |
| 7855 | _sg.gl.cache.stored_vertex_buffer = _sg.gl.cache.vertex_buffer; |
| 7856 | } else if (target == GL_ELEMENT_ARRAY_BUFFER) { |
| 7857 | _sg.gl.cache.stored_index_buffer = _sg.gl.cache.index_buffer; |
| 7858 | } else if (target == GL_SHADER_STORAGE_BUFFER) { |
| 7859 | _sg.gl.cache.stored_storage_buffer = _sg.gl.cache.storage_buffer; |
| 7860 | } else { |
| 7861 | SOKOL_UNREACHABLE; |
| 7862 | } |
| 7863 | } |
| 7864 | |
| 7865 | _SOKOL_PRIVATE void _sg_gl_cache_restore_buffer_binding(GLenum target) { |
| 7866 | if (target == GL_ARRAY_BUFFER) { |
| 7867 | if (_sg.gl.cache.stored_vertex_buffer != 0) { |
| 7868 | // we only care about restoring valid ids |
| 7869 | _sg_gl_cache_bind_buffer(target, _sg.gl.cache.stored_vertex_buffer); |
| 7870 | _sg.gl.cache.stored_vertex_buffer = 0; |
| 7871 | } |
| 7872 | } else if (target == GL_ELEMENT_ARRAY_BUFFER) { |
| 7873 | if (_sg.gl.cache.stored_index_buffer != 0) { |
| 7874 | // we only care about restoring valid ids |
| 7875 | _sg_gl_cache_bind_buffer(target, _sg.gl.cache.stored_index_buffer); |
| 7876 | _sg.gl.cache.stored_index_buffer = 0; |
| 7877 | } |
| 7878 | } else if (target == GL_SHADER_STORAGE_BUFFER) { |
| 7879 | if (_sg.gl.cache.stored_storage_buffer != 0) { |
| 7880 | // we only care about restoring valid ids |
| 7881 | _sg_gl_cache_bind_buffer(target, _sg.gl.cache.stored_storage_buffer); |
| 7882 | _sg.gl.cache.stored_storage_buffer = 0; |
| 7883 | } |
| 7884 | } else { |
| 7885 | SOKOL_UNREACHABLE; |
| 7886 | } |
| 7887 | } |
| 7888 | |
| 7889 | // called when _sg_gl_discard_buffer() |
| 7890 | _SOKOL_PRIVATE void _sg_gl_cache_invalidate_buffer(GLuint buf) { |
| 7891 | if (buf == _sg.gl.cache.vertex_buffer) { |
| 7892 | _sg.gl.cache.vertex_buffer = 0; |
| 7893 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 7894 | _sg_stats_add(gl.num_bind_buffer, 1); |
| 7895 | } |
| 7896 | if (buf == _sg.gl.cache.index_buffer) { |
| 7897 | _sg.gl.cache.index_buffer = 0; |
| 7898 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 7899 | _sg_stats_add(gl.num_bind_buffer, 1); |
| 7900 | } |
| 7901 | if (buf == _sg.gl.cache.storage_buffer) { |
| 7902 | _sg.gl.cache.storage_buffer = 0; |
| 7903 | glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); |
| 7904 | _sg_stats_add(gl.num_bind_buffer, 1); |
| 7905 | } |
| 7906 | for (int stage = 0; stage < SG_NUM_SHADER_STAGES; stage++) { |
| 7907 | for (int i = 0; i < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; i++) { |
| 7908 | if (buf == _sg.gl.cache.stage_storage_buffers[stage][i]) { |
| 7909 | _sg.gl.cache.stage_storage_buffers[stage][i] = 0; |
| 7910 | _sg.gl.cache.storage_buffer = 0; // not a bug! |
| 7911 | const GLuint bind_index = _sg_gl_storagebuffer_bind_index(stage, i); |
| 7912 | glBindBufferBase(GL_SHADER_STORAGE_BUFFER, bind_index, 0); |
| 7913 | _sg_stats_add(gl.num_bind_buffer, 1); |
| 7914 | } |
| 7915 | } |
| 7916 | } |
| 7917 | if (buf == _sg.gl.cache.stored_vertex_buffer) { |
| 7918 | _sg.gl.cache.stored_vertex_buffer = 0; |
| 7919 | } |
| 7920 | if (buf == _sg.gl.cache.stored_index_buffer) { |
| 7921 | _sg.gl.cache.stored_index_buffer = 0; |
| 7922 | } |
| 7923 | if (buf == _sg.gl.cache.stored_storage_buffer) { |
| 7924 | _sg.gl.cache.stored_storage_buffer = 0; |
| 7925 | } |
| 7926 | for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) { |
| 7927 | if (buf == _sg.gl.cache.attrs[i].gl_vbuf) { |
| 7928 | _sg.gl.cache.attrs[i].gl_vbuf = 0; |
| 7929 | } |
| 7930 | } |
| 7931 | } |
| 7932 | |
| 7933 | _SOKOL_PRIVATE void _sg_gl_cache_active_texture(GLenum texture) { |
| 7934 | _SG_GL_CHECK_ERROR(); |
| 7935 | if (_sg.gl.cache.cur_active_texture != texture) { |
| 7936 | _sg.gl.cache.cur_active_texture = texture; |
| 7937 | glActiveTexture(texture); |
| 7938 | _sg_stats_add(gl.num_active_texture, 1); |
| 7939 | } |
| 7940 | _SG_GL_CHECK_ERROR(); |
| 7941 | } |
| 7942 | |
| 7943 | _SOKOL_PRIVATE void _sg_gl_cache_clear_texture_sampler_bindings(bool force) { |
| 7944 | _SG_GL_CHECK_ERROR(); |
| 7945 | for (int i = 0; (i < _SG_GL_TEXTURE_SAMPLER_CACHE_SIZE) && (i < _sg.limits.gl_max_combined_texture_image_units); i++) { |
| 7946 | if (force || (_sg.gl.cache.texture_samplers[i].texture != 0)) { |
| 7947 | GLenum gl_texture_unit = (GLenum) (GL_TEXTURE0 + i); |
| 7948 | glActiveTexture(gl_texture_unit); |
| 7949 | _sg_stats_add(gl.num_active_texture, 1); |
| 7950 | glBindTexture(GL_TEXTURE_2D, 0); |
| 7951 | glBindTexture(GL_TEXTURE_CUBE_MAP, 0); |
| 7952 | glBindTexture(GL_TEXTURE_3D, 0); |
| 7953 | glBindTexture(GL_TEXTURE_2D_ARRAY, 0); |
| 7954 | _sg_stats_add(gl.num_bind_texture, 4); |
| 7955 | glBindSampler((GLuint)i, 0); |
| 7956 | _sg_stats_add(gl.num_bind_sampler, 1); |
| 7957 | _sg.gl.cache.texture_samplers[i].target = 0; |
| 7958 | _sg.gl.cache.texture_samplers[i].texture = 0; |
| 7959 | _sg.gl.cache.texture_samplers[i].sampler = 0; |
| 7960 | _sg.gl.cache.cur_active_texture = gl_texture_unit; |
| 7961 | } |
| 7962 | } |
| 7963 | _SG_GL_CHECK_ERROR(); |
| 7964 | } |
| 7965 | |
| 7966 | _SOKOL_PRIVATE void _sg_gl_cache_bind_texture_sampler(int slot_index, GLenum target, GLuint texture, GLuint sampler) { |
| 7967 | /* it's valid to call this function with target=0 and/or texture=0 |
| 7968 | target=0 will unbind the previous binding, texture=0 will clear |
| 7969 | the new binding |
| 7970 | */ |
| 7971 | SOKOL_ASSERT((slot_index >= 0) && (slot_index < _SG_GL_TEXTURE_SAMPLER_CACHE_SIZE)); |
| 7972 | if (slot_index >= _sg.limits.gl_max_combined_texture_image_units) { |
| 7973 | return; |
| 7974 | } |
| 7975 | _SG_GL_CHECK_ERROR(); |
| 7976 | _sg_gl_cache_texture_sampler_bind_slot* slot = &_sg.gl.cache.texture_samplers[slot_index]; |
| 7977 | if ((slot->target != target) || (slot->texture != texture) || (slot->sampler != sampler)) { |
| 7978 | _sg_gl_cache_active_texture((GLenum)(GL_TEXTURE0 + slot_index)); |
| 7979 | // if the target has changed, clear the previous binding on that target |
| 7980 | if ((target != slot->target) && (slot->target != 0)) { |
| 7981 | glBindTexture(slot->target, 0); |
| 7982 | _SG_GL_CHECK_ERROR(); |
| 7983 | _sg_stats_add(gl.num_bind_texture, 1); |
| 7984 | } |
| 7985 | // apply new binding (can be 0 to unbind) |
| 7986 | if (target != 0) { |
| 7987 | glBindTexture(target, texture); |
| 7988 | _SG_GL_CHECK_ERROR(); |
| 7989 | _sg_stats_add(gl.num_bind_texture, 1); |
| 7990 | } |
| 7991 | // apply new sampler (can be 0 to unbind) |
| 7992 | glBindSampler((GLuint)slot_index, sampler); |
| 7993 | _SG_GL_CHECK_ERROR(); |
| 7994 | _sg_stats_add(gl.num_bind_sampler, 1); |
| 7995 | |
| 7996 | slot->target = target; |
| 7997 | slot->texture = texture; |
| 7998 | slot->sampler = sampler; |
| 7999 | } |
| 8000 | } |
| 8001 | |
| 8002 | _SOKOL_PRIVATE void _sg_gl_cache_store_texture_sampler_binding(int slot_index) { |
| 8003 | SOKOL_ASSERT((slot_index >= 0) && (slot_index < _SG_GL_TEXTURE_SAMPLER_CACHE_SIZE)); |
| 8004 | _sg.gl.cache.stored_texture_sampler = _sg.gl.cache.texture_samplers[slot_index]; |
| 8005 | } |
| 8006 | |
| 8007 | _SOKOL_PRIVATE void _sg_gl_cache_restore_texture_sampler_binding(int slot_index) { |
| 8008 | SOKOL_ASSERT((slot_index >= 0) && (slot_index < _SG_GL_TEXTURE_SAMPLER_CACHE_SIZE)); |
| 8009 | _sg_gl_cache_texture_sampler_bind_slot* slot = &_sg.gl.cache.stored_texture_sampler; |
| 8010 | if (slot->texture != 0) { |
| 8011 | // we only care about restoring valid ids |
| 8012 | SOKOL_ASSERT(slot->target != 0); |
| 8013 | _sg_gl_cache_bind_texture_sampler(slot_index, slot->target, slot->texture, slot->sampler); |
| 8014 | slot->target = 0; |
| 8015 | slot->texture = 0; |
| 8016 | slot->sampler = 0; |
| 8017 | } |
| 8018 | } |
| 8019 | |
| 8020 | // called from _sg_gl_discard_texture() and _sg_gl_discard_sampler() |
| 8021 | _SOKOL_PRIVATE void _sg_gl_cache_invalidate_texture_sampler(GLuint tex, GLuint smp) { |
| 8022 | _SG_GL_CHECK_ERROR(); |
| 8023 | for (int i = 0; i < _SG_GL_TEXTURE_SAMPLER_CACHE_SIZE; i++) { |
| 8024 | _sg_gl_cache_texture_sampler_bind_slot* slot = &_sg.gl.cache.texture_samplers[i]; |
| 8025 | if ((0 != slot->target) && ((tex == slot->texture) || (smp == slot->sampler))) { |
| 8026 | _sg_gl_cache_active_texture((GLenum)(GL_TEXTURE0 + i)); |
| 8027 | glBindTexture(slot->target, 0); |
| 8028 | _SG_GL_CHECK_ERROR(); |
| 8029 | _sg_stats_add(gl.num_bind_texture, 1); |
| 8030 | glBindSampler((GLuint)i, 0); |
| 8031 | _SG_GL_CHECK_ERROR(); |
| 8032 | _sg_stats_add(gl.num_bind_sampler, 1); |
| 8033 | slot->target = 0; |
| 8034 | slot->texture = 0; |
| 8035 | slot->sampler = 0; |
| 8036 | } |
| 8037 | } |
| 8038 | if ((tex == _sg.gl.cache.stored_texture_sampler.texture) || (smp == _sg.gl.cache.stored_texture_sampler.sampler)) { |
| 8039 | _sg.gl.cache.stored_texture_sampler.target = 0; |
| 8040 | _sg.gl.cache.stored_texture_sampler.texture = 0; |
| 8041 | _sg.gl.cache.stored_texture_sampler.sampler = 0; |
| 8042 | } |
| 8043 | } |
| 8044 | |
| 8045 | // called from _sg_gl_discard_shader() |
| 8046 | _SOKOL_PRIVATE void _sg_gl_cache_invalidate_program(GLuint prog) { |
| 8047 | if (prog == _sg.gl.cache.prog) { |
| 8048 | _sg.gl.cache.prog = 0; |
| 8049 | glUseProgram(0); |
| 8050 | _sg_stats_add(gl.num_use_program, 1); |
| 8051 | } |
| 8052 | } |
| 8053 | |
| 8054 | // called from _sg_gl_discard_pipeline() |
| 8055 | _SOKOL_PRIVATE void _sg_gl_cache_invalidate_pipeline(_sg_pipeline_t* pip) { |
| 8056 | if (pip == _sg.gl.cache.cur_pipeline) { |
| 8057 | _sg.gl.cache.cur_pipeline = 0; |
| 8058 | _sg.gl.cache.cur_pipeline_id.id = SG_INVALID_ID; |
| 8059 | } |
| 8060 | } |
| 8061 | |
| 8062 | _SOKOL_PRIVATE void _sg_gl_reset_state_cache(void) { |
| 8063 | _SG_GL_CHECK_ERROR(); |
| 8064 | glBindVertexArray(_sg.gl.vao); |
| 8065 | _SG_GL_CHECK_ERROR(); |
| 8066 | _sg_clear(&_sg.gl.cache, sizeof(_sg.gl.cache)); |
| 8067 | _sg_gl_cache_clear_buffer_bindings(true); |
| 8068 | _SG_GL_CHECK_ERROR(); |
| 8069 | _sg_gl_cache_clear_texture_sampler_bindings(true); |
| 8070 | _SG_GL_CHECK_ERROR(); |
| 8071 | for (int i = 0; i < _sg.limits.max_vertex_attrs; i++) { |
| 8072 | _sg_gl_attr_t* attr = &_sg.gl.cache.attrs[i].gl_attr; |
| 8073 | attr->vb_index = -1; |
| 8074 | attr->divisor = -1; |
| 8075 | glDisableVertexAttribArray((GLuint)i); |
| 8076 | _SG_GL_CHECK_ERROR(); |
| 8077 | _sg_stats_add(gl.num_disable_vertex_attrib_array, 1); |
| 8078 | } |
| 8079 | _sg.gl.cache.cur_primitive_type = GL_TRIANGLES; |
| 8080 | |
| 8081 | // shader program |
| 8082 | glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&_sg.gl.cache.prog); |
| 8083 | _SG_GL_CHECK_ERROR(); |
| 8084 | |
| 8085 | // depth and stencil state |
| 8086 | _sg.gl.cache.depth.compare = SG_COMPAREFUNC_ALWAYS; |
| 8087 | _sg.gl.cache.stencil.front.compare = SG_COMPAREFUNC_ALWAYS; |
| 8088 | _sg.gl.cache.stencil.front.fail_op = SG_STENCILOP_KEEP; |
| 8089 | _sg.gl.cache.stencil.front.depth_fail_op = SG_STENCILOP_KEEP; |
| 8090 | _sg.gl.cache.stencil.front.pass_op = SG_STENCILOP_KEEP; |
| 8091 | _sg.gl.cache.stencil.back.compare = SG_COMPAREFUNC_ALWAYS; |
| 8092 | _sg.gl.cache.stencil.back.fail_op = SG_STENCILOP_KEEP; |
| 8093 | _sg.gl.cache.stencil.back.depth_fail_op = SG_STENCILOP_KEEP; |
| 8094 | _sg.gl.cache.stencil.back.pass_op = SG_STENCILOP_KEEP; |
| 8095 | glEnable(GL_DEPTH_TEST); |
| 8096 | glDepthFunc(GL_ALWAYS); |
| 8097 | glDepthMask(GL_FALSE); |
| 8098 | glDisable(GL_STENCIL_TEST); |
| 8099 | glStencilFunc(GL_ALWAYS, 0, 0); |
| 8100 | glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); |
| 8101 | glStencilMask(0); |
| 8102 | _sg_stats_add(gl.num_render_state, 7); |
| 8103 | |
| 8104 | // blend state |
| 8105 | _sg.gl.cache.blend.src_factor_rgb = SG_BLENDFACTOR_ONE; |
| 8106 | _sg.gl.cache.blend.dst_factor_rgb = SG_BLENDFACTOR_ZERO; |
| 8107 | _sg.gl.cache.blend.op_rgb = SG_BLENDOP_ADD; |
| 8108 | _sg.gl.cache.blend.src_factor_alpha = SG_BLENDFACTOR_ONE; |
| 8109 | _sg.gl.cache.blend.dst_factor_alpha = SG_BLENDFACTOR_ZERO; |
| 8110 | _sg.gl.cache.blend.op_alpha = SG_BLENDOP_ADD; |
| 8111 | glDisable(GL_BLEND); |
| 8112 | glBlendFuncSeparate(GL_ONE, GL_ZERO, GL_ONE, GL_ZERO); |
| 8113 | glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD); |
| 8114 | glBlendColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 8115 | _sg_stats_add(gl.num_render_state, 4); |
| 8116 | |
| 8117 | // standalone state |
| 8118 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 8119 | _sg.gl.cache.color_write_mask[i] = SG_COLORMASK_RGBA; |
| 8120 | } |
| 8121 | _sg.gl.cache.cull_mode = SG_CULLMODE_NONE; |
| 8122 | _sg.gl.cache.face_winding = SG_FACEWINDING_CW; |
| 8123 | _sg.gl.cache.sample_count = 1; |
| 8124 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 8125 | glPolygonOffset(0.0f, 0.0f); |
| 8126 | glDisable(GL_POLYGON_OFFSET_FILL); |
| 8127 | glDisable(GL_CULL_FACE); |
| 8128 | glFrontFace(GL_CW); |
| 8129 | glCullFace(GL_BACK); |
| 8130 | glEnable(GL_SCISSOR_TEST); |
| 8131 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE); |
| 8132 | glEnable(GL_DITHER); |
| 8133 | glDisable(GL_POLYGON_OFFSET_FILL); |
| 8134 | _sg_stats_add(gl.num_render_state, 10); |
| 8135 | #if defined(SOKOL_GLCORE) |
| 8136 | glEnable(GL_MULTISAMPLE); |
| 8137 | glEnable(GL_PROGRAM_POINT_SIZE); |
| 8138 | _sg_stats_add(gl.num_render_state, 2); |
| 8139 | #endif |
| 8140 | } |
| 8141 | |
| 8142 | _SOKOL_PRIVATE void _sg_gl_setup_backend(const sg_desc* desc) { |
| 8143 | _SOKOL_UNUSED(desc); |
| 8144 | |
| 8145 | // assumes that _sg.gl is already zero-initialized |
| 8146 | _sg.gl.valid = true; |
| 8147 | |
| 8148 | #if defined(_SOKOL_USE_WIN32_GL_LOADER) |
| 8149 | _sg_gl_load_opengl(); |
| 8150 | #endif |
| 8151 | |
| 8152 | // clear initial GL error state |
| 8153 | #if defined(SOKOL_DEBUG) |
| 8154 | while (glGetError() != GL_NO_ERROR); |
| 8155 | #endif |
| 8156 | #if defined(SOKOL_GLCORE) |
| 8157 | _sg_gl_init_caps_glcore(); |
| 8158 | #elif defined(SOKOL_GLES3) |
| 8159 | _sg_gl_init_caps_gles3(); |
| 8160 | #endif |
| 8161 | |
| 8162 | glGenVertexArrays(1, &_sg.gl.vao); |
| 8163 | glBindVertexArray(_sg.gl.vao); |
| 8164 | _SG_GL_CHECK_ERROR(); |
| 8165 | // incoming texture data is generally expected to be packed tightly |
| 8166 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 8167 | #if defined(SOKOL_GLCORE) |
| 8168 | // enable seamless cubemap sampling (only desktop GL) |
| 8169 | glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); |
| 8170 | #endif |
| 8171 | _sg_gl_reset_state_cache(); |
| 8172 | } |
| 8173 | |
| 8174 | _SOKOL_PRIVATE void _sg_gl_discard_backend(void) { |
| 8175 | SOKOL_ASSERT(_sg.gl.valid); |
| 8176 | if (_sg.gl.vao) { |
| 8177 | glDeleteVertexArrays(1, &_sg.gl.vao); |
| 8178 | } |
| 8179 | #if defined(_SOKOL_USE_WIN32_GL_LOADER) |
| 8180 | _sg_gl_unload_opengl(); |
| 8181 | #endif |
| 8182 | _sg.gl.valid = false; |
| 8183 | } |
| 8184 | |
| 8185 | //-- GL backend resource creation and destruction ------------------------------ |
| 8186 | _SOKOL_PRIVATE sg_resource_state _sg_gl_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) { |
| 8187 | SOKOL_ASSERT(buf && desc); |
| 8188 | _SG_GL_CHECK_ERROR(); |
| 8189 | buf->gl.injected = (0 != desc->gl_buffers[0]); |
| 8190 | const GLenum gl_target = _sg_gl_buffer_target(buf->cmn.type); |
| 8191 | const GLenum gl_usage = _sg_gl_usage(buf->cmn.usage); |
| 8192 | for (int slot = 0; slot < buf->cmn.num_slots; slot++) { |
| 8193 | GLuint gl_buf = 0; |
| 8194 | if (buf->gl.injected) { |
| 8195 | SOKOL_ASSERT(desc->gl_buffers[slot]); |
| 8196 | gl_buf = desc->gl_buffers[slot]; |
| 8197 | } else { |
| 8198 | glGenBuffers(1, &gl_buf); |
| 8199 | SOKOL_ASSERT(gl_buf); |
| 8200 | _sg_gl_cache_store_buffer_binding(gl_target); |
| 8201 | _sg_gl_cache_bind_buffer(gl_target, gl_buf); |
| 8202 | glBufferData(gl_target, buf->cmn.size, 0, gl_usage); |
| 8203 | if (buf->cmn.usage == SG_USAGE_IMMUTABLE) { |
| 8204 | SOKOL_ASSERT(desc->data.ptr); |
| 8205 | glBufferSubData(gl_target, 0, buf->cmn.size, desc->data.ptr); |
| 8206 | } |
| 8207 | _sg_gl_cache_restore_buffer_binding(gl_target); |
| 8208 | } |
| 8209 | buf->gl.buf[slot] = gl_buf; |
| 8210 | } |
| 8211 | _SG_GL_CHECK_ERROR(); |
| 8212 | return SG_RESOURCESTATE_VALID; |
| 8213 | } |
| 8214 | |
| 8215 | _SOKOL_PRIVATE void _sg_gl_discard_buffer(_sg_buffer_t* buf) { |
| 8216 | SOKOL_ASSERT(buf); |
| 8217 | _SG_GL_CHECK_ERROR(); |
| 8218 | for (int slot = 0; slot < buf->cmn.num_slots; slot++) { |
| 8219 | if (buf->gl.buf[slot]) { |
| 8220 | _sg_gl_cache_invalidate_buffer(buf->gl.buf[slot]); |
| 8221 | if (!buf->gl.injected) { |
| 8222 | glDeleteBuffers(1, &buf->gl.buf[slot]); |
| 8223 | } |
| 8224 | } |
| 8225 | } |
| 8226 | _SG_GL_CHECK_ERROR(); |
| 8227 | } |
| 8228 | |
| 8229 | _SOKOL_PRIVATE bool _sg_gl_supported_texture_format(sg_pixel_format fmt) { |
| 8230 | const int fmt_index = (int) fmt; |
| 8231 | SOKOL_ASSERT((fmt_index > SG_PIXELFORMAT_NONE) && (fmt_index < _SG_PIXELFORMAT_NUM)); |
| 8232 | return _sg.formats[fmt_index].sample; |
| 8233 | } |
| 8234 | |
| 8235 | _SOKOL_PRIVATE sg_resource_state _sg_gl_create_image(_sg_image_t* img, const sg_image_desc* desc) { |
| 8236 | SOKOL_ASSERT(img && desc); |
| 8237 | _SG_GL_CHECK_ERROR(); |
| 8238 | img->gl.injected = (0 != desc->gl_textures[0]); |
| 8239 | |
| 8240 | // check if texture format is support |
| 8241 | if (!_sg_gl_supported_texture_format(img->cmn.pixel_format)) { |
| 8242 | _SG_ERROR(GL_TEXTURE_FORMAT_NOT_SUPPORTED); |
| 8243 | return SG_RESOURCESTATE_FAILED; |
| 8244 | } |
| 8245 | const GLenum gl_internal_format = _sg_gl_teximage_internal_format(img->cmn.pixel_format); |
| 8246 | |
| 8247 | // if this is a MSAA render target, a render buffer object will be created instead of a regulat texture |
| 8248 | // (since GLES3 has no multisampled texture objects) |
| 8249 | if (img->cmn.render_target && (img->cmn.sample_count > 1)) { |
| 8250 | glGenRenderbuffers(1, &img->gl.msaa_render_buffer); |
| 8251 | glBindRenderbuffer(GL_RENDERBUFFER, img->gl.msaa_render_buffer); |
| 8252 | glRenderbufferStorageMultisample(GL_RENDERBUFFER, img->cmn.sample_count, gl_internal_format, img->cmn.width, img->cmn.height); |
| 8253 | } else if (img->gl.injected) { |
| 8254 | img->gl.target = _sg_gl_texture_target(img->cmn.type); |
| 8255 | // inject externally GL textures |
| 8256 | for (int slot = 0; slot < img->cmn.num_slots; slot++) { |
| 8257 | SOKOL_ASSERT(desc->gl_textures[slot]); |
| 8258 | img->gl.tex[slot] = desc->gl_textures[slot]; |
| 8259 | } |
| 8260 | if (desc->gl_texture_target) { |
| 8261 | img->gl.target = (GLenum)desc->gl_texture_target; |
| 8262 | } |
| 8263 | } else { |
| 8264 | // create our own GL texture(s) |
| 8265 | img->gl.target = _sg_gl_texture_target(img->cmn.type); |
| 8266 | const GLenum gl_format = _sg_gl_teximage_format(img->cmn.pixel_format); |
| 8267 | const bool is_compressed = _sg_is_compressed_pixel_format(img->cmn.pixel_format); |
| 8268 | for (int slot = 0; slot < img->cmn.num_slots; slot++) { |
| 8269 | glGenTextures(1, &img->gl.tex[slot]); |
| 8270 | SOKOL_ASSERT(img->gl.tex[slot]); |
| 8271 | _sg_gl_cache_store_texture_sampler_binding(0); |
| 8272 | _sg_gl_cache_bind_texture_sampler(0, img->gl.target, img->gl.tex[slot], 0); |
| 8273 | glTexParameteri(img->gl.target, GL_TEXTURE_MAX_LEVEL, img->cmn.num_mipmaps - 1); |
| 8274 | const int num_faces = img->cmn.type == SG_IMAGETYPE_CUBE ? 6 : 1; |
| 8275 | int data_index = 0; |
| 8276 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 8277 | for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++, data_index++) { |
| 8278 | GLenum gl_img_target = img->gl.target; |
| 8279 | if (SG_IMAGETYPE_CUBE == img->cmn.type) { |
| 8280 | gl_img_target = _sg_gl_cubeface_target(face_index); |
| 8281 | } |
| 8282 | const GLvoid* data_ptr = desc->data.subimage[face_index][mip_index].ptr; |
| 8283 | const int mip_width = _sg_miplevel_dim(img->cmn.width, mip_index); |
| 8284 | const int mip_height = _sg_miplevel_dim(img->cmn.height, mip_index); |
| 8285 | if ((SG_IMAGETYPE_2D == img->cmn.type) || (SG_IMAGETYPE_CUBE == img->cmn.type)) { |
| 8286 | if (is_compressed) { |
| 8287 | const GLsizei data_size = (GLsizei) desc->data.subimage[face_index][mip_index].size; |
| 8288 | glCompressedTexImage2D(gl_img_target, mip_index, gl_internal_format, |
| 8289 | mip_width, mip_height, 0, data_size, data_ptr); |
| 8290 | } else { |
| 8291 | const GLenum gl_type = _sg_gl_teximage_type(img->cmn.pixel_format); |
| 8292 | glTexImage2D(gl_img_target, mip_index, (GLint)gl_internal_format, |
| 8293 | mip_width, mip_height, 0, gl_format, gl_type, data_ptr); |
| 8294 | } |
| 8295 | } else if ((SG_IMAGETYPE_3D == img->cmn.type) || (SG_IMAGETYPE_ARRAY == img->cmn.type)) { |
| 8296 | int mip_depth = img->cmn.num_slices; |
| 8297 | if (SG_IMAGETYPE_3D == img->cmn.type) { |
| 8298 | mip_depth = _sg_miplevel_dim(mip_depth, mip_index); |
| 8299 | } |
| 8300 | if (is_compressed) { |
| 8301 | const GLsizei data_size = (GLsizei) desc->data.subimage[face_index][mip_index].size; |
| 8302 | glCompressedTexImage3D(gl_img_target, mip_index, gl_internal_format, |
| 8303 | mip_width, mip_height, mip_depth, 0, data_size, data_ptr); |
| 8304 | } else { |
| 8305 | const GLenum gl_type = _sg_gl_teximage_type(img->cmn.pixel_format); |
| 8306 | glTexImage3D(gl_img_target, mip_index, (GLint)gl_internal_format, |
| 8307 | mip_width, mip_height, mip_depth, 0, gl_format, gl_type, data_ptr); |
| 8308 | } |
| 8309 | } |
| 8310 | } |
| 8311 | } |
| 8312 | _sg_gl_cache_restore_texture_sampler_binding(0); |
| 8313 | } |
| 8314 | } |
| 8315 | _SG_GL_CHECK_ERROR(); |
| 8316 | return SG_RESOURCESTATE_VALID; |
| 8317 | } |
| 8318 | |
| 8319 | _SOKOL_PRIVATE void _sg_gl_discard_image(_sg_image_t* img) { |
| 8320 | SOKOL_ASSERT(img); |
| 8321 | _SG_GL_CHECK_ERROR(); |
| 8322 | for (int slot = 0; slot < img->cmn.num_slots; slot++) { |
| 8323 | if (img->gl.tex[slot]) { |
| 8324 | _sg_gl_cache_invalidate_texture_sampler(img->gl.tex[slot], 0); |
| 8325 | if (!img->gl.injected) { |
| 8326 | glDeleteTextures(1, &img->gl.tex[slot]); |
| 8327 | } |
| 8328 | } |
| 8329 | } |
| 8330 | if (img->gl.msaa_render_buffer) { |
| 8331 | glDeleteRenderbuffers(1, &img->gl.msaa_render_buffer); |
| 8332 | } |
| 8333 | _SG_GL_CHECK_ERROR(); |
| 8334 | } |
| 8335 | |
| 8336 | _SOKOL_PRIVATE sg_resource_state _sg_gl_create_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) { |
| 8337 | SOKOL_ASSERT(smp && desc); |
| 8338 | _SG_GL_CHECK_ERROR(); |
| 8339 | smp->gl.injected = (0 != desc->gl_sampler); |
| 8340 | if (smp->gl.injected) { |
| 8341 | smp->gl.smp = (GLuint) desc->gl_sampler; |
| 8342 | } else { |
| 8343 | glGenSamplers(1, &smp->gl.smp); |
| 8344 | SOKOL_ASSERT(smp->gl.smp); |
| 8345 | |
| 8346 | const GLenum gl_min_filter = _sg_gl_min_filter(smp->cmn.min_filter, smp->cmn.mipmap_filter); |
| 8347 | const GLenum gl_mag_filter = _sg_gl_mag_filter(smp->cmn.mag_filter); |
| 8348 | glSamplerParameteri(smp->gl.smp, GL_TEXTURE_MIN_FILTER, (GLint)gl_min_filter); |
| 8349 | glSamplerParameteri(smp->gl.smp, GL_TEXTURE_MAG_FILTER, (GLint)gl_mag_filter); |
| 8350 | // GL spec has strange defaults for mipmap min/max lod: -1000 to +1000 |
| 8351 | const float min_lod = _sg_clamp(desc->min_lod, 0.0f, 1000.0f); |
| 8352 | const float max_lod = _sg_clamp(desc->max_lod, 0.0f, 1000.0f); |
| 8353 | glSamplerParameterf(smp->gl.smp, GL_TEXTURE_MIN_LOD, min_lod); |
| 8354 | glSamplerParameterf(smp->gl.smp, GL_TEXTURE_MAX_LOD, max_lod); |
| 8355 | glSamplerParameteri(smp->gl.smp, GL_TEXTURE_WRAP_S, (GLint)_sg_gl_wrap(smp->cmn.wrap_u)); |
| 8356 | glSamplerParameteri(smp->gl.smp, GL_TEXTURE_WRAP_T, (GLint)_sg_gl_wrap(smp->cmn.wrap_v)); |
| 8357 | glSamplerParameteri(smp->gl.smp, GL_TEXTURE_WRAP_R, (GLint)_sg_gl_wrap(smp->cmn.wrap_w)); |
| 8358 | #if defined(SOKOL_GLCORE) |
| 8359 | float border[4]; |
| 8360 | switch (smp->cmn.border_color) { |
| 8361 | case SG_BORDERCOLOR_TRANSPARENT_BLACK: |
| 8362 | border[0] = 0.0f; border[1] = 0.0f; border[2] = 0.0f; border[3] = 0.0f; |
| 8363 | break; |
| 8364 | case SG_BORDERCOLOR_OPAQUE_WHITE: |
| 8365 | border[0] = 1.0f; border[1] = 1.0f; border[2] = 1.0f; border[3] = 1.0f; |
| 8366 | break; |
| 8367 | default: |
| 8368 | border[0] = 0.0f; border[1] = 0.0f; border[2] = 0.0f; border[3] = 1.0f; |
| 8369 | break; |
| 8370 | } |
| 8371 | glSamplerParameterfv(smp->gl.smp, GL_TEXTURE_BORDER_COLOR, border); |
| 8372 | #endif |
| 8373 | if (smp->cmn.compare != SG_COMPAREFUNC_NEVER) { |
| 8374 | glSamplerParameteri(smp->gl.smp, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); |
| 8375 | glSamplerParameteri(smp->gl.smp, GL_TEXTURE_COMPARE_FUNC, (GLint)_sg_gl_compare_func(smp->cmn.compare)); |
| 8376 | } else { |
| 8377 | glSamplerParameteri(smp->gl.smp, GL_TEXTURE_COMPARE_MODE, GL_NONE); |
| 8378 | } |
| 8379 | if (_sg.gl.ext_anisotropic && (smp->cmn.max_anisotropy > 1)) { |
| 8380 | GLint max_aniso = (GLint) smp->cmn.max_anisotropy; |
| 8381 | if (max_aniso > _sg.gl.max_anisotropy) { |
| 8382 | max_aniso = _sg.gl.max_anisotropy; |
| 8383 | } |
| 8384 | glSamplerParameteri(smp->gl.smp, GL_TEXTURE_MAX_ANISOTROPY_EXT, max_aniso); |
| 8385 | } |
| 8386 | } |
| 8387 | _SG_GL_CHECK_ERROR(); |
| 8388 | return SG_RESOURCESTATE_VALID; |
| 8389 | } |
| 8390 | |
| 8391 | _SOKOL_PRIVATE void _sg_gl_discard_sampler(_sg_sampler_t* smp) { |
| 8392 | SOKOL_ASSERT(smp); |
| 8393 | _SG_GL_CHECK_ERROR(); |
| 8394 | _sg_gl_cache_invalidate_texture_sampler(0, smp->gl.smp); |
| 8395 | if (!smp->gl.injected) { |
| 8396 | glDeleteSamplers(1, &smp->gl.smp); |
| 8397 | } |
| 8398 | _SG_GL_CHECK_ERROR(); |
| 8399 | } |
| 8400 | |
| 8401 | _SOKOL_PRIVATE GLuint _sg_gl_compile_shader(sg_shader_stage stage, const char* src) { |
| 8402 | SOKOL_ASSERT(src); |
| 8403 | _SG_GL_CHECK_ERROR(); |
| 8404 | GLuint gl_shd = glCreateShader(_sg_gl_shader_stage(stage)); |
| 8405 | glShaderSource(gl_shd, 1, &src, 0); |
| 8406 | glCompileShader(gl_shd); |
| 8407 | GLint compile_status = 0; |
| 8408 | glGetShaderiv(gl_shd, GL_COMPILE_STATUS, &compile_status); |
| 8409 | if (!compile_status) { |
| 8410 | // compilation failed, log error and delete shader |
| 8411 | GLint log_len = 0; |
| 8412 | glGetShaderiv(gl_shd, GL_INFO_LOG_LENGTH, &log_len); |
| 8413 | if (log_len > 0) { |
| 8414 | GLchar* log_buf = (GLchar*) _sg_malloc((size_t)log_len); |
| 8415 | glGetShaderInfoLog(gl_shd, log_len, &log_len, log_buf); |
| 8416 | _SG_ERROR(GL_SHADER_COMPILATION_FAILED); |
| 8417 | _SG_LOGMSG(GL_SHADER_COMPILATION_FAILED, log_buf); |
| 8418 | _sg_free(log_buf); |
| 8419 | } |
| 8420 | glDeleteShader(gl_shd); |
| 8421 | gl_shd = 0; |
| 8422 | } |
| 8423 | _SG_GL_CHECK_ERROR(); |
| 8424 | return gl_shd; |
| 8425 | } |
| 8426 | |
| 8427 | _SOKOL_PRIVATE sg_resource_state _sg_gl_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) { |
| 8428 | SOKOL_ASSERT(shd && desc); |
| 8429 | SOKOL_ASSERT(!shd->gl.prog); |
| 8430 | _SG_GL_CHECK_ERROR(); |
| 8431 | |
| 8432 | // copy the optional vertex attribute names over |
| 8433 | for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) { |
| 8434 | _sg_strcpy(&shd->gl.attrs[i].name, desc->attrs[i].name); |
| 8435 | } |
| 8436 | |
| 8437 | GLuint gl_vs = _sg_gl_compile_shader(SG_SHADERSTAGE_VS, desc->vs.source); |
| 8438 | GLuint gl_fs = _sg_gl_compile_shader(SG_SHADERSTAGE_FS, desc->fs.source); |
| 8439 | if (!(gl_vs && gl_fs)) { |
| 8440 | return SG_RESOURCESTATE_FAILED; |
| 8441 | } |
| 8442 | GLuint gl_prog = glCreateProgram(); |
| 8443 | glAttachShader(gl_prog, gl_vs); |
| 8444 | glAttachShader(gl_prog, gl_fs); |
| 8445 | glLinkProgram(gl_prog); |
| 8446 | glDeleteShader(gl_vs); |
| 8447 | glDeleteShader(gl_fs); |
| 8448 | _SG_GL_CHECK_ERROR(); |
| 8449 | |
| 8450 | GLint link_status; |
| 8451 | glGetProgramiv(gl_prog, GL_LINK_STATUS, &link_status); |
| 8452 | if (!link_status) { |
| 8453 | GLint log_len = 0; |
| 8454 | glGetProgramiv(gl_prog, GL_INFO_LOG_LENGTH, &log_len); |
| 8455 | if (log_len > 0) { |
| 8456 | GLchar* log_buf = (GLchar*) _sg_malloc((size_t)log_len); |
| 8457 | glGetProgramInfoLog(gl_prog, log_len, &log_len, log_buf); |
| 8458 | _SG_ERROR(GL_SHADER_LINKING_FAILED); |
| 8459 | _SG_LOGMSG(GL_SHADER_LINKING_FAILED, log_buf); |
| 8460 | _sg_free(log_buf); |
| 8461 | } |
| 8462 | glDeleteProgram(gl_prog); |
| 8463 | return SG_RESOURCESTATE_FAILED; |
| 8464 | } |
| 8465 | shd->gl.prog = gl_prog; |
| 8466 | |
| 8467 | // resolve uniforms |
| 8468 | _SG_GL_CHECK_ERROR(); |
| 8469 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 8470 | const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS)? &desc->vs : &desc->fs; |
| 8471 | const _sg_shader_stage_t* stage = &shd->cmn.stage[stage_index]; |
| 8472 | _sg_gl_shader_stage_t* gl_stage = &shd->gl.stage[stage_index]; |
| 8473 | for (int ub_index = 0; ub_index < stage->num_uniform_blocks; ub_index++) { |
| 8474 | const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index]; |
| 8475 | SOKOL_ASSERT(ub_desc->size > 0); |
| 8476 | _sg_gl_uniform_block_t* ub = &gl_stage->uniform_blocks[ub_index]; |
| 8477 | SOKOL_ASSERT(ub->num_uniforms == 0); |
| 8478 | uint32_t cur_uniform_offset = 0; |
| 8479 | for (int u_index = 0; u_index < SG_MAX_UB_MEMBERS; u_index++) { |
| 8480 | const sg_shader_uniform_desc* u_desc = &ub_desc->uniforms[u_index]; |
| 8481 | if (u_desc->type == SG_UNIFORMTYPE_INVALID) { |
| 8482 | break; |
| 8483 | } |
| 8484 | const uint32_t u_align = _sg_uniform_alignment(u_desc->type, u_desc->array_count, ub_desc->layout); |
| 8485 | const uint32_t u_size = _sg_uniform_size(u_desc->type, u_desc->array_count, ub_desc->layout); |
| 8486 | cur_uniform_offset = _sg_align_u32(cur_uniform_offset, u_align); |
| 8487 | _sg_gl_uniform_t* u = &ub->uniforms[u_index]; |
| 8488 | u->type = u_desc->type; |
| 8489 | u->count = (uint16_t) u_desc->array_count; |
| 8490 | u->offset = (uint16_t) cur_uniform_offset; |
| 8491 | cur_uniform_offset += u_size; |
| 8492 | if (u_desc->name) { |
| 8493 | u->gl_loc = glGetUniformLocation(gl_prog, u_desc->name); |
| 8494 | } else { |
| 8495 | u->gl_loc = u_index; |
| 8496 | } |
| 8497 | ub->num_uniforms++; |
| 8498 | } |
| 8499 | if (ub_desc->layout == SG_UNIFORMLAYOUT_STD140) { |
| 8500 | cur_uniform_offset = _sg_align_u32(cur_uniform_offset, 16); |
| 8501 | } |
| 8502 | SOKOL_ASSERT(ub_desc->size == (size_t)cur_uniform_offset); |
| 8503 | _SOKOL_UNUSED(cur_uniform_offset); |
| 8504 | } |
| 8505 | } |
| 8506 | |
| 8507 | // resolve combined image samplers |
| 8508 | _SG_GL_CHECK_ERROR(); |
| 8509 | GLuint cur_prog = 0; |
| 8510 | glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&cur_prog); |
| 8511 | glUseProgram(gl_prog); |
| 8512 | int gl_tex_slot = 0; |
| 8513 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 8514 | const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS)? &desc->vs : &desc->fs; |
| 8515 | const _sg_shader_stage_t* stage = &shd->cmn.stage[stage_index]; |
| 8516 | _sg_gl_shader_stage_t* gl_stage = &shd->gl.stage[stage_index]; |
| 8517 | for (int img_smp_index = 0; img_smp_index < stage->num_image_samplers; img_smp_index++) { |
| 8518 | const sg_shader_image_sampler_pair_desc* img_smp_desc = &stage_desc->image_sampler_pairs[img_smp_index]; |
| 8519 | _sg_gl_shader_image_sampler_t* gl_img_smp = &gl_stage->image_samplers[img_smp_index]; |
| 8520 | SOKOL_ASSERT(img_smp_desc->glsl_name); |
| 8521 | GLint gl_loc = glGetUniformLocation(gl_prog, img_smp_desc->glsl_name); |
| 8522 | if (gl_loc != -1) { |
| 8523 | gl_img_smp->gl_tex_slot = gl_tex_slot++; |
| 8524 | glUniform1i(gl_loc, gl_img_smp->gl_tex_slot); |
| 8525 | } else { |
| 8526 | gl_img_smp->gl_tex_slot = -1; |
| 8527 | _SG_ERROR(GL_TEXTURE_NAME_NOT_FOUND_IN_SHADER); |
| 8528 | _SG_LOGMSG(GL_TEXTURE_NAME_NOT_FOUND_IN_SHADER, img_smp_desc->glsl_name); |
| 8529 | } |
| 8530 | } |
| 8531 | } |
| 8532 | // it's legal to call glUseProgram with 0 |
| 8533 | glUseProgram(cur_prog); |
| 8534 | _SG_GL_CHECK_ERROR(); |
| 8535 | return SG_RESOURCESTATE_VALID; |
| 8536 | } |
| 8537 | |
| 8538 | _SOKOL_PRIVATE void _sg_gl_discard_shader(_sg_shader_t* shd) { |
| 8539 | SOKOL_ASSERT(shd); |
| 8540 | _SG_GL_CHECK_ERROR(); |
| 8541 | if (shd->gl.prog) { |
| 8542 | _sg_gl_cache_invalidate_program(shd->gl.prog); |
| 8543 | glDeleteProgram(shd->gl.prog); |
| 8544 | } |
| 8545 | _SG_GL_CHECK_ERROR(); |
| 8546 | } |
| 8547 | |
| 8548 | _SOKOL_PRIVATE sg_resource_state _sg_gl_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) { |
| 8549 | SOKOL_ASSERT(pip && shd && desc); |
| 8550 | SOKOL_ASSERT((pip->shader == 0) && (pip->cmn.shader_id.id != SG_INVALID_ID)); |
| 8551 | SOKOL_ASSERT(desc->shader.id == shd->slot.id); |
| 8552 | SOKOL_ASSERT(shd->gl.prog); |
| 8553 | pip->shader = shd; |
| 8554 | pip->gl.primitive_type = desc->primitive_type; |
| 8555 | pip->gl.depth = desc->depth; |
| 8556 | pip->gl.stencil = desc->stencil; |
| 8557 | // FIXME: blend color and write mask per draw-buffer-attachment (requires GL4) |
| 8558 | pip->gl.blend = desc->colors[0].blend; |
| 8559 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 8560 | pip->gl.color_write_mask[i] = desc->colors[i].write_mask; |
| 8561 | } |
| 8562 | pip->gl.cull_mode = desc->cull_mode; |
| 8563 | pip->gl.face_winding = desc->face_winding; |
| 8564 | pip->gl.sample_count = desc->sample_count; |
| 8565 | pip->gl.alpha_to_coverage_enabled = desc->alpha_to_coverage_enabled; |
| 8566 | |
| 8567 | // resolve vertex attributes |
| 8568 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 8569 | pip->gl.attrs[attr_index].vb_index = -1; |
| 8570 | } |
| 8571 | for (int attr_index = 0; attr_index < _sg.limits.max_vertex_attrs; attr_index++) { |
| 8572 | const sg_vertex_attr_state* a_state = &desc->layout.attrs[attr_index]; |
| 8573 | if (a_state->format == SG_VERTEXFORMAT_INVALID) { |
| 8574 | break; |
| 8575 | } |
| 8576 | SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS); |
| 8577 | const sg_vertex_buffer_layout_state* l_state = &desc->layout.buffers[a_state->buffer_index]; |
| 8578 | const sg_vertex_step step_func = l_state->step_func; |
| 8579 | const int step_rate = l_state->step_rate; |
| 8580 | GLint attr_loc = attr_index; |
| 8581 | if (!_sg_strempty(&shd->gl.attrs[attr_index].name)) { |
| 8582 | attr_loc = glGetAttribLocation(pip->shader->gl.prog, _sg_strptr(&shd->gl.attrs[attr_index].name)); |
| 8583 | } |
| 8584 | SOKOL_ASSERT(attr_loc < (GLint)_sg.limits.max_vertex_attrs); |
| 8585 | if (attr_loc != -1) { |
| 8586 | _sg_gl_attr_t* gl_attr = &pip->gl.attrs[attr_loc]; |
| 8587 | SOKOL_ASSERT(gl_attr->vb_index == -1); |
| 8588 | gl_attr->vb_index = (int8_t) a_state->buffer_index; |
| 8589 | if (step_func == SG_VERTEXSTEP_PER_VERTEX) { |
| 8590 | gl_attr->divisor = 0; |
| 8591 | } else { |
| 8592 | gl_attr->divisor = (int8_t) step_rate; |
| 8593 | pip->cmn.use_instanced_draw = true; |
| 8594 | } |
| 8595 | SOKOL_ASSERT(l_state->stride > 0); |
| 8596 | gl_attr->stride = (uint8_t) l_state->stride; |
| 8597 | gl_attr->offset = a_state->offset; |
| 8598 | gl_attr->size = (uint8_t) _sg_gl_vertexformat_size(a_state->format); |
| 8599 | gl_attr->type = _sg_gl_vertexformat_type(a_state->format); |
| 8600 | gl_attr->normalized = _sg_gl_vertexformat_normalized(a_state->format); |
| 8601 | pip->cmn.vertex_buffer_layout_active[a_state->buffer_index] = true; |
| 8602 | } else { |
| 8603 | _SG_ERROR(GL_VERTEX_ATTRIBUTE_NOT_FOUND_IN_SHADER); |
| 8604 | _SG_LOGMSG(GL_VERTEX_ATTRIBUTE_NOT_FOUND_IN_SHADER, _sg_strptr(&shd->gl.attrs[attr_index].name)); |
| 8605 | } |
| 8606 | } |
| 8607 | return SG_RESOURCESTATE_VALID; |
| 8608 | } |
| 8609 | |
| 8610 | _SOKOL_PRIVATE void _sg_gl_discard_pipeline(_sg_pipeline_t* pip) { |
| 8611 | SOKOL_ASSERT(pip); |
| 8612 | _sg_gl_cache_invalidate_pipeline(pip); |
| 8613 | } |
| 8614 | |
| 8615 | _SOKOL_PRIVATE void _sg_gl_fb_attach_texture(const _sg_gl_attachment_t* gl_att, const _sg_attachment_common_t* cmn_att, GLenum gl_att_type) { |
| 8616 | const _sg_image_t* img = gl_att->image; |
| 8617 | SOKOL_ASSERT(img); |
| 8618 | const GLuint gl_tex = img->gl.tex[0]; |
| 8619 | SOKOL_ASSERT(gl_tex); |
| 8620 | const GLuint gl_target = img->gl.target; |
| 8621 | SOKOL_ASSERT(gl_target); |
| 8622 | const int mip_level = cmn_att->mip_level; |
| 8623 | const int slice = cmn_att->slice; |
| 8624 | switch (img->cmn.type) { |
| 8625 | case SG_IMAGETYPE_2D: |
| 8626 | glFramebufferTexture2D(GL_FRAMEBUFFER, gl_att_type, gl_target, gl_tex, mip_level); |
| 8627 | break; |
| 8628 | case SG_IMAGETYPE_CUBE: |
| 8629 | glFramebufferTexture2D(GL_FRAMEBUFFER, gl_att_type, _sg_gl_cubeface_target(slice), gl_tex, mip_level); |
| 8630 | break; |
| 8631 | default: |
| 8632 | glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_att_type, gl_tex, mip_level, slice); |
| 8633 | break; |
| 8634 | } |
| 8635 | } |
| 8636 | |
| 8637 | _SOKOL_PRIVATE GLenum _sg_gl_depth_stencil_attachment_type(const _sg_gl_attachment_t* ds_att) { |
| 8638 | const _sg_image_t* img = ds_att->image; |
| 8639 | SOKOL_ASSERT(img); |
| 8640 | if (_sg_is_depth_stencil_format(img->cmn.pixel_format)) { |
| 8641 | return GL_DEPTH_STENCIL_ATTACHMENT; |
| 8642 | } else { |
| 8643 | return GL_DEPTH_ATTACHMENT; |
| 8644 | } |
| 8645 | } |
| 8646 | |
| 8647 | _SOKOL_PRIVATE sg_resource_state _sg_gl_create_attachments(_sg_attachments_t* atts, _sg_image_t** color_images, _sg_image_t** resolve_images, _sg_image_t* ds_image, const sg_attachments_desc* desc) { |
| 8648 | SOKOL_ASSERT(atts && desc); |
| 8649 | SOKOL_ASSERT(color_images && resolve_images); |
| 8650 | _SG_GL_CHECK_ERROR(); |
| 8651 | |
| 8652 | // copy image pointers |
| 8653 | for (int i = 0; i < atts->cmn.num_colors; i++) { |
| 8654 | const sg_attachment_desc* color_desc = &desc->colors[i]; |
| 8655 | _SOKOL_UNUSED(color_desc); |
| 8656 | SOKOL_ASSERT(color_desc->image.id != SG_INVALID_ID); |
| 8657 | SOKOL_ASSERT(0 == atts->gl.colors[i].image); |
| 8658 | SOKOL_ASSERT(color_images[i] && (color_images[i]->slot.id == color_desc->image.id)); |
| 8659 | SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(color_images[i]->cmn.pixel_format)); |
| 8660 | atts->gl.colors[i].image = color_images[i]; |
| 8661 | |
| 8662 | const sg_attachment_desc* resolve_desc = &desc->resolves[i]; |
| 8663 | if (resolve_desc->image.id != SG_INVALID_ID) { |
| 8664 | SOKOL_ASSERT(0 == atts->gl.resolves[i].image); |
| 8665 | SOKOL_ASSERT(resolve_images[i] && (resolve_images[i]->slot.id == resolve_desc->image.id)); |
| 8666 | SOKOL_ASSERT(color_images[i] && (color_images[i]->cmn.pixel_format == resolve_images[i]->cmn.pixel_format)); |
| 8667 | atts->gl.resolves[i].image = resolve_images[i]; |
| 8668 | } |
| 8669 | } |
| 8670 | SOKOL_ASSERT(0 == atts->gl.depth_stencil.image); |
| 8671 | const sg_attachment_desc* ds_desc = &desc->depth_stencil; |
| 8672 | if (ds_desc->image.id != SG_INVALID_ID) { |
| 8673 | SOKOL_ASSERT(ds_image && (ds_image->slot.id == ds_desc->image.id)); |
| 8674 | SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(ds_image->cmn.pixel_format)); |
| 8675 | atts->gl.depth_stencil.image = ds_image; |
| 8676 | } |
| 8677 | |
| 8678 | // store current framebuffer binding (restored at end of function) |
| 8679 | GLuint gl_orig_fb; |
| 8680 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, (GLint*)&gl_orig_fb); |
| 8681 | |
| 8682 | // create a framebuffer object |
| 8683 | glGenFramebuffers(1, &atts->gl.fb); |
| 8684 | glBindFramebuffer(GL_FRAMEBUFFER, atts->gl.fb); |
| 8685 | |
| 8686 | // attach color attachments to framebuffer |
| 8687 | for (int i = 0; i < atts->cmn.num_colors; i++) { |
| 8688 | const _sg_image_t* color_img = atts->gl.colors[i].image; |
| 8689 | SOKOL_ASSERT(color_img); |
| 8690 | const GLuint gl_msaa_render_buffer = color_img->gl.msaa_render_buffer; |
| 8691 | if (gl_msaa_render_buffer) { |
| 8692 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, (GLenum)(GL_COLOR_ATTACHMENT0+i), GL_RENDERBUFFER, gl_msaa_render_buffer); |
| 8693 | } else { |
| 8694 | const GLenum gl_att_type = (GLenum)(GL_COLOR_ATTACHMENT0 + i); |
| 8695 | _sg_gl_fb_attach_texture(&atts->gl.colors[i], &atts->cmn.colors[i], gl_att_type); |
| 8696 | } |
| 8697 | } |
| 8698 | // attach depth-stencil attachment |
| 8699 | if (atts->gl.depth_stencil.image) { |
| 8700 | const GLenum gl_att = _sg_gl_depth_stencil_attachment_type(&atts->gl.depth_stencil); |
| 8701 | const _sg_image_t* ds_img = atts->gl.depth_stencil.image; |
| 8702 | const GLuint gl_msaa_render_buffer = ds_img->gl.msaa_render_buffer; |
| 8703 | if (gl_msaa_render_buffer) { |
| 8704 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, gl_att, GL_RENDERBUFFER, gl_msaa_render_buffer); |
| 8705 | } else { |
| 8706 | const GLenum gl_att_type = _sg_gl_depth_stencil_attachment_type(&atts->gl.depth_stencil); |
| 8707 | _sg_gl_fb_attach_texture(&atts->gl.depth_stencil, &atts->cmn.depth_stencil, gl_att_type); |
| 8708 | } |
| 8709 | } |
| 8710 | |
| 8711 | // check if framebuffer is complete |
| 8712 | { |
| 8713 | const GLenum fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 8714 | if (fb_status != GL_FRAMEBUFFER_COMPLETE) { |
| 8715 | switch (fb_status) { |
| 8716 | case GL_FRAMEBUFFER_UNDEFINED: |
| 8717 | _SG_ERROR(GL_FRAMEBUFFER_STATUS_UNDEFINED); |
| 8718 | break; |
| 8719 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: |
| 8720 | _SG_ERROR(GL_FRAMEBUFFER_STATUS_INCOMPLETE_ATTACHMENT); |
| 8721 | break; |
| 8722 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: |
| 8723 | _SG_ERROR(GL_FRAMEBUFFER_STATUS_INCOMPLETE_MISSING_ATTACHMENT); |
| 8724 | break; |
| 8725 | case GL_FRAMEBUFFER_UNSUPPORTED: |
| 8726 | _SG_ERROR(GL_FRAMEBUFFER_STATUS_UNSUPPORTED); |
| 8727 | break; |
| 8728 | case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: |
| 8729 | _SG_ERROR(GL_FRAMEBUFFER_STATUS_INCOMPLETE_MULTISAMPLE); |
| 8730 | break; |
| 8731 | default: |
| 8732 | _SG_ERROR(GL_FRAMEBUFFER_STATUS_UNKNOWN); |
| 8733 | break; |
| 8734 | } |
| 8735 | return SG_RESOURCESTATE_FAILED; |
| 8736 | } |
| 8737 | } |
| 8738 | |
| 8739 | // setup color attachments for the framebuffer |
| 8740 | static const GLenum gl_draw_bufs[SG_MAX_COLOR_ATTACHMENTS] = { |
| 8741 | GL_COLOR_ATTACHMENT0, |
| 8742 | GL_COLOR_ATTACHMENT1, |
| 8743 | GL_COLOR_ATTACHMENT2, |
| 8744 | GL_COLOR_ATTACHMENT3 |
| 8745 | }; |
| 8746 | glDrawBuffers(atts->cmn.num_colors, gl_draw_bufs); |
| 8747 | |
| 8748 | // create MSAA resolve framebuffers if necessary |
| 8749 | for (int i = 0; i < atts->cmn.num_colors; i++) { |
| 8750 | _sg_gl_attachment_t* gl_resolve_att = &atts->gl.resolves[i]; |
| 8751 | if (gl_resolve_att->image) { |
| 8752 | _sg_attachment_common_t* cmn_resolve_att = &atts->cmn.resolves[i]; |
| 8753 | SOKOL_ASSERT(0 == atts->gl.msaa_resolve_framebuffer[i]); |
| 8754 | glGenFramebuffers(1, &atts->gl.msaa_resolve_framebuffer[i]); |
| 8755 | glBindFramebuffer(GL_FRAMEBUFFER, atts->gl.msaa_resolve_framebuffer[i]); |
| 8756 | _sg_gl_fb_attach_texture(gl_resolve_att, cmn_resolve_att, GL_COLOR_ATTACHMENT0); |
| 8757 | // check if framebuffer is complete |
| 8758 | const GLenum fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 8759 | if (fb_status != GL_FRAMEBUFFER_COMPLETE) { |
| 8760 | switch (fb_status) { |
| 8761 | case GL_FRAMEBUFFER_UNDEFINED: |
| 8762 | _SG_ERROR(GL_FRAMEBUFFER_STATUS_UNDEFINED); |
| 8763 | break; |
| 8764 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: |
| 8765 | _SG_ERROR(GL_FRAMEBUFFER_STATUS_INCOMPLETE_ATTACHMENT); |
| 8766 | break; |
| 8767 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: |
| 8768 | _SG_ERROR(GL_FRAMEBUFFER_STATUS_INCOMPLETE_MISSING_ATTACHMENT); |
| 8769 | break; |
| 8770 | case GL_FRAMEBUFFER_UNSUPPORTED: |
| 8771 | _SG_ERROR(GL_FRAMEBUFFER_STATUS_UNSUPPORTED); |
| 8772 | break; |
| 8773 | case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: |
| 8774 | _SG_ERROR(GL_FRAMEBUFFER_STATUS_INCOMPLETE_MULTISAMPLE); |
| 8775 | break; |
| 8776 | default: |
| 8777 | _SG_ERROR(GL_FRAMEBUFFER_STATUS_UNKNOWN); |
| 8778 | break; |
| 8779 | } |
| 8780 | return SG_RESOURCESTATE_FAILED; |
| 8781 | } |
| 8782 | // setup color attachments for the framebuffer |
| 8783 | glDrawBuffers(1, &gl_draw_bufs[0]); |
| 8784 | } |
| 8785 | } |
| 8786 | |
| 8787 | // restore original framebuffer binding |
| 8788 | glBindFramebuffer(GL_FRAMEBUFFER, gl_orig_fb); |
| 8789 | _SG_GL_CHECK_ERROR(); |
| 8790 | return SG_RESOURCESTATE_VALID; |
| 8791 | } |
| 8792 | |
| 8793 | _SOKOL_PRIVATE void _sg_gl_discard_attachments(_sg_attachments_t* atts) { |
| 8794 | SOKOL_ASSERT(atts); |
| 8795 | _SG_GL_CHECK_ERROR(); |
| 8796 | if (0 != atts->gl.fb) { |
| 8797 | glDeleteFramebuffers(1, &atts->gl.fb); |
| 8798 | } |
| 8799 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 8800 | if (atts->gl.msaa_resolve_framebuffer[i]) { |
| 8801 | glDeleteFramebuffers(1, &atts->gl.msaa_resolve_framebuffer[i]); |
| 8802 | } |
| 8803 | } |
| 8804 | _SG_GL_CHECK_ERROR(); |
| 8805 | } |
| 8806 | |
| 8807 | _SOKOL_PRIVATE _sg_image_t* _sg_gl_attachments_color_image(const _sg_attachments_t* atts, int index) { |
| 8808 | SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS)); |
| 8809 | return atts->gl.colors[index].image; |
| 8810 | } |
| 8811 | |
| 8812 | _SOKOL_PRIVATE _sg_image_t* _sg_gl_attachments_resolve_image(const _sg_attachments_t* atts, int index) { |
| 8813 | SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS)); |
| 8814 | return atts->gl.resolves[index].image; |
| 8815 | } |
| 8816 | |
| 8817 | _SOKOL_PRIVATE _sg_image_t* _sg_gl_attachments_ds_image(const _sg_attachments_t* atts) { |
| 8818 | SOKOL_ASSERT(atts); |
| 8819 | return atts->gl.depth_stencil.image; |
| 8820 | } |
| 8821 | |
| 8822 | _SOKOL_PRIVATE void _sg_gl_begin_pass(const sg_pass* pass) { |
| 8823 | // FIXME: what if a texture used as render target is still bound, should we |
| 8824 | // unbind all currently bound textures in begin pass? |
| 8825 | SOKOL_ASSERT(pass); |
| 8826 | _SG_GL_CHECK_ERROR(); |
| 8827 | const _sg_attachments_t* atts = _sg.cur_pass.atts; |
| 8828 | const sg_swapchain* swapchain = &pass->swapchain; |
| 8829 | const sg_pass_action* action = &pass->action; |
| 8830 | |
| 8831 | // bind the render pass framebuffer |
| 8832 | // |
| 8833 | // FIXME: Disabling SRGB conversion for the default framebuffer is |
| 8834 | // a crude hack to make behaviour for sRGB render target textures |
| 8835 | // identical with the Metal and D3D11 swapchains created by sokol-app. |
| 8836 | // |
| 8837 | // This will need a cleaner solution (e.g. allowing to configure |
| 8838 | // sokol_app.h with an sRGB or RGB framebuffer. |
| 8839 | if (atts) { |
| 8840 | // offscreen pass |
| 8841 | SOKOL_ASSERT(atts->gl.fb); |
| 8842 | #if defined(SOKOL_GLCORE) |
| 8843 | glEnable(GL_FRAMEBUFFER_SRGB); |
| 8844 | #endif |
| 8845 | glBindFramebuffer(GL_FRAMEBUFFER, atts->gl.fb); |
| 8846 | } else { |
| 8847 | // default pass |
| 8848 | #if defined(SOKOL_GLCORE) |
| 8849 | glDisable(GL_FRAMEBUFFER_SRGB); |
| 8850 | #endif |
| 8851 | // NOTE: on some platforms, the default framebuffer of a context |
| 8852 | // is null, so we can't actually assert here that the |
| 8853 | // framebuffer has been provided |
| 8854 | glBindFramebuffer(GL_FRAMEBUFFER, swapchain->gl.framebuffer); |
| 8855 | } |
| 8856 | glViewport(0, 0, _sg.cur_pass.width, _sg.cur_pass.height); |
| 8857 | glScissor(0, 0, _sg.cur_pass.width, _sg.cur_pass.height); |
| 8858 | |
| 8859 | // number of color attachments |
| 8860 | const int num_color_atts = atts ? atts->cmn.num_colors : 1; |
| 8861 | |
| 8862 | // clear color and depth-stencil attachments if needed |
| 8863 | bool clear_any_color = false; |
| 8864 | for (int i = 0; i < num_color_atts; i++) { |
| 8865 | if (SG_LOADACTION_CLEAR == action->colors[i].load_action) { |
| 8866 | clear_any_color = true; |
| 8867 | break; |
| 8868 | } |
| 8869 | } |
| 8870 | const bool clear_depth = (action->depth.load_action == SG_LOADACTION_CLEAR); |
| 8871 | const bool clear_stencil = (action->stencil.load_action == SG_LOADACTION_CLEAR); |
| 8872 | |
| 8873 | bool need_pip_cache_flush = false; |
| 8874 | if (clear_any_color) { |
| 8875 | bool need_color_mask_flush = false; |
| 8876 | // NOTE: not a bug to iterate over all possible color attachments |
| 8877 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 8878 | if (SG_COLORMASK_RGBA != _sg.gl.cache.color_write_mask[i]) { |
| 8879 | need_pip_cache_flush = true; |
| 8880 | need_color_mask_flush = true; |
| 8881 | _sg.gl.cache.color_write_mask[i] = SG_COLORMASK_RGBA; |
| 8882 | } |
| 8883 | } |
| 8884 | if (need_color_mask_flush) { |
| 8885 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
| 8886 | } |
| 8887 | } |
| 8888 | if (clear_depth) { |
| 8889 | if (!_sg.gl.cache.depth.write_enabled) { |
| 8890 | need_pip_cache_flush = true; |
| 8891 | _sg.gl.cache.depth.write_enabled = true; |
| 8892 | glDepthMask(GL_TRUE); |
| 8893 | } |
| 8894 | if (_sg.gl.cache.depth.compare != SG_COMPAREFUNC_ALWAYS) { |
| 8895 | need_pip_cache_flush = true; |
| 8896 | _sg.gl.cache.depth.compare = SG_COMPAREFUNC_ALWAYS; |
| 8897 | glDepthFunc(GL_ALWAYS); |
| 8898 | } |
| 8899 | } |
| 8900 | if (clear_stencil) { |
| 8901 | if (_sg.gl.cache.stencil.write_mask != 0xFF) { |
| 8902 | need_pip_cache_flush = true; |
| 8903 | _sg.gl.cache.stencil.write_mask = 0xFF; |
| 8904 | glStencilMask(0xFF); |
| 8905 | } |
| 8906 | } |
| 8907 | if (need_pip_cache_flush) { |
| 8908 | // we messed with the state cache directly, need to clear cached |
| 8909 | // pipeline to force re-evaluation in next sg_apply_pipeline() |
| 8910 | _sg.gl.cache.cur_pipeline = 0; |
| 8911 | _sg.gl.cache.cur_pipeline_id.id = SG_INVALID_ID; |
| 8912 | } |
| 8913 | for (int i = 0; i < num_color_atts; i++) { |
| 8914 | if (action->colors[i].load_action == SG_LOADACTION_CLEAR) { |
| 8915 | glClearBufferfv(GL_COLOR, i, &action->colors[i].clear_value.r); |
| 8916 | } |
| 8917 | } |
| 8918 | if ((atts == 0) || (atts->gl.depth_stencil.image)) { |
| 8919 | if (clear_depth && clear_stencil) { |
| 8920 | glClearBufferfi(GL_DEPTH_STENCIL, 0, action->depth.clear_value, action->stencil.clear_value); |
| 8921 | } else if (clear_depth) { |
| 8922 | glClearBufferfv(GL_DEPTH, 0, &action->depth.clear_value); |
| 8923 | } else if (clear_stencil) { |
| 8924 | GLint val = (GLint) action->stencil.clear_value; |
| 8925 | glClearBufferiv(GL_STENCIL, 0, &val); |
| 8926 | } |
| 8927 | } |
| 8928 | // keep store actions for end-pass |
| 8929 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 8930 | _sg.gl.color_store_actions[i] = action->colors[i].store_action; |
| 8931 | } |
| 8932 | _sg.gl.depth_store_action = action->depth.store_action; |
| 8933 | _sg.gl.stencil_store_action = action->stencil.store_action; |
| 8934 | |
| 8935 | _SG_GL_CHECK_ERROR(); |
| 8936 | } |
| 8937 | |
| 8938 | _SOKOL_PRIVATE void _sg_gl_end_pass(void) { |
| 8939 | _SG_GL_CHECK_ERROR(); |
| 8940 | |
| 8941 | if (_sg.cur_pass.atts) { |
| 8942 | const _sg_attachments_t* atts = _sg.cur_pass.atts; |
| 8943 | SOKOL_ASSERT(atts->slot.id == _sg.cur_pass.atts_id.id); |
| 8944 | bool fb_read_bound = false; |
| 8945 | bool fb_draw_bound = false; |
| 8946 | const int num_color_atts = atts->cmn.num_colors; |
| 8947 | for (int i = 0; i < num_color_atts; i++) { |
| 8948 | // perform MSAA resolve if needed |
| 8949 | if (atts->gl.msaa_resolve_framebuffer[i] != 0) { |
| 8950 | if (!fb_read_bound) { |
| 8951 | SOKOL_ASSERT(atts->gl.fb); |
| 8952 | glBindFramebuffer(GL_READ_FRAMEBUFFER, atts->gl.fb); |
| 8953 | fb_read_bound = true; |
| 8954 | } |
| 8955 | const int w = atts->gl.colors[i].image->cmn.width; |
| 8956 | const int h = atts->gl.colors[i].image->cmn.height; |
| 8957 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, atts->gl.msaa_resolve_framebuffer[i]); |
| 8958 | glReadBuffer((GLenum)(GL_COLOR_ATTACHMENT0 + i)); |
| 8959 | glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST); |
| 8960 | fb_draw_bound = true; |
| 8961 | } |
| 8962 | } |
| 8963 | |
| 8964 | // invalidate framebuffers |
| 8965 | _SOKOL_UNUSED(fb_draw_bound); |
| 8966 | #if defined(SOKOL_GLES3) |
| 8967 | // need to restore framebuffer binding before invalidate if the MSAA resolve had changed the binding |
| 8968 | if (fb_draw_bound) { |
| 8969 | glBindFramebuffer(GL_FRAMEBUFFER, atts->gl.fb); |
| 8970 | } |
| 8971 | GLenum invalidate_atts[SG_MAX_COLOR_ATTACHMENTS + 2] = { 0 }; |
| 8972 | int att_index = 0; |
| 8973 | for (int i = 0; i < num_color_atts; i++) { |
| 8974 | if (_sg.gl.color_store_actions[i] == SG_STOREACTION_DONTCARE) { |
| 8975 | invalidate_atts[att_index++] = (GLenum)(GL_COLOR_ATTACHMENT0 + i); |
| 8976 | } |
| 8977 | } |
| 8978 | if ((_sg.gl.depth_store_action == SG_STOREACTION_DONTCARE) && (_sg.cur_pass.atts->cmn.depth_stencil.image_id.id != SG_INVALID_ID)) { |
| 8979 | invalidate_atts[att_index++] = GL_DEPTH_ATTACHMENT; |
| 8980 | } |
| 8981 | if ((_sg.gl.stencil_store_action == SG_STOREACTION_DONTCARE) && (_sg.cur_pass.atts->cmn.depth_stencil.image_id.id != SG_INVALID_ID)) { |
| 8982 | invalidate_atts[att_index++] = GL_STENCIL_ATTACHMENT; |
| 8983 | } |
| 8984 | if (att_index > 0) { |
| 8985 | glInvalidateFramebuffer(GL_DRAW_FRAMEBUFFER, att_index, invalidate_atts); |
| 8986 | } |
| 8987 | #endif |
| 8988 | } |
| 8989 | _SG_GL_CHECK_ERROR(); |
| 8990 | } |
| 8991 | |
| 8992 | _SOKOL_PRIVATE void _sg_gl_apply_viewport(int x, int y, int w, int h, bool origin_top_left) { |
| 8993 | y = origin_top_left ? (_sg.cur_pass.height - (y+h)) : y; |
| 8994 | glViewport(x, y, w, h); |
| 8995 | } |
| 8996 | |
| 8997 | _SOKOL_PRIVATE void _sg_gl_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) { |
| 8998 | y = origin_top_left ? (_sg.cur_pass.height - (y+h)) : y; |
| 8999 | glScissor(x, y, w, h); |
| 9000 | } |
| 9001 | |
| 9002 | _SOKOL_PRIVATE void _sg_gl_apply_pipeline(_sg_pipeline_t* pip) { |
| 9003 | SOKOL_ASSERT(pip); |
| 9004 | SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id)); |
| 9005 | _SG_GL_CHECK_ERROR(); |
| 9006 | if ((_sg.gl.cache.cur_pipeline != pip) || (_sg.gl.cache.cur_pipeline_id.id != pip->slot.id)) { |
| 9007 | _sg.gl.cache.cur_pipeline = pip; |
| 9008 | _sg.gl.cache.cur_pipeline_id.id = pip->slot.id; |
| 9009 | _sg.gl.cache.cur_primitive_type = _sg_gl_primitive_type(pip->gl.primitive_type); |
| 9010 | _sg.gl.cache.cur_index_type = _sg_gl_index_type(pip->cmn.index_type); |
| 9011 | |
| 9012 | // update depth state |
| 9013 | { |
| 9014 | const sg_depth_state* state_ds = &pip->gl.depth; |
| 9015 | sg_depth_state* cache_ds = &_sg.gl.cache.depth; |
| 9016 | if (state_ds->compare != cache_ds->compare) { |
| 9017 | cache_ds->compare = state_ds->compare; |
| 9018 | glDepthFunc(_sg_gl_compare_func(state_ds->compare)); |
| 9019 | _sg_stats_add(gl.num_render_state, 1); |
| 9020 | } |
| 9021 | if (state_ds->write_enabled != cache_ds->write_enabled) { |
| 9022 | cache_ds->write_enabled = state_ds->write_enabled; |
| 9023 | glDepthMask(state_ds->write_enabled); |
| 9024 | _sg_stats_add(gl.num_render_state, 1); |
| 9025 | } |
| 9026 | if (!_sg_fequal(state_ds->bias, cache_ds->bias, 0.000001f) || |
| 9027 | !_sg_fequal(state_ds->bias_slope_scale, cache_ds->bias_slope_scale, 0.000001f)) |
| 9028 | { |
| 9029 | /* according to ANGLE's D3D11 backend: |
| 9030 | D3D11 SlopeScaledDepthBias ==> GL polygonOffsetFactor |
| 9031 | D3D11 DepthBias ==> GL polygonOffsetUnits |
| 9032 | DepthBiasClamp has no meaning on GL |
| 9033 | */ |
| 9034 | cache_ds->bias = state_ds->bias; |
| 9035 | cache_ds->bias_slope_scale = state_ds->bias_slope_scale; |
| 9036 | glPolygonOffset(state_ds->bias_slope_scale, state_ds->bias); |
| 9037 | _sg_stats_add(gl.num_render_state, 1); |
| 9038 | bool po_enabled = true; |
| 9039 | if (_sg_fequal(state_ds->bias, 0.0f, 0.000001f) && |
| 9040 | _sg_fequal(state_ds->bias_slope_scale, 0.0f, 0.000001f)) |
| 9041 | { |
| 9042 | po_enabled = false; |
| 9043 | } |
| 9044 | if (po_enabled != _sg.gl.cache.polygon_offset_enabled) { |
| 9045 | _sg.gl.cache.polygon_offset_enabled = po_enabled; |
| 9046 | if (po_enabled) { |
| 9047 | glEnable(GL_POLYGON_OFFSET_FILL); |
| 9048 | } else { |
| 9049 | glDisable(GL_POLYGON_OFFSET_FILL); |
| 9050 | } |
| 9051 | _sg_stats_add(gl.num_render_state, 1); |
| 9052 | } |
| 9053 | } |
| 9054 | } |
| 9055 | |
| 9056 | // update stencil state |
| 9057 | { |
| 9058 | const sg_stencil_state* state_ss = &pip->gl.stencil; |
| 9059 | sg_stencil_state* cache_ss = &_sg.gl.cache.stencil; |
| 9060 | if (state_ss->enabled != cache_ss->enabled) { |
| 9061 | cache_ss->enabled = state_ss->enabled; |
| 9062 | if (state_ss->enabled) { |
| 9063 | glEnable(GL_STENCIL_TEST); |
| 9064 | } else { |
| 9065 | glDisable(GL_STENCIL_TEST); |
| 9066 | } |
| 9067 | _sg_stats_add(gl.num_render_state, 1); |
| 9068 | } |
| 9069 | if (state_ss->write_mask != cache_ss->write_mask) { |
| 9070 | cache_ss->write_mask = state_ss->write_mask; |
| 9071 | glStencilMask(state_ss->write_mask); |
| 9072 | _sg_stats_add(gl.num_render_state, 1); |
| 9073 | } |
| 9074 | for (int i = 0; i < 2; i++) { |
| 9075 | const sg_stencil_face_state* state_sfs = (i==0)? &state_ss->front : &state_ss->back; |
| 9076 | sg_stencil_face_state* cache_sfs = (i==0)? &cache_ss->front : &cache_ss->back; |
| 9077 | GLenum gl_face = (i==0)? GL_FRONT : GL_BACK; |
| 9078 | if ((state_sfs->compare != cache_sfs->compare) || |
| 9079 | (state_ss->read_mask != cache_ss->read_mask) || |
| 9080 | (state_ss->ref != cache_ss->ref)) |
| 9081 | { |
| 9082 | cache_sfs->compare = state_sfs->compare; |
| 9083 | glStencilFuncSeparate(gl_face, |
| 9084 | _sg_gl_compare_func(state_sfs->compare), |
| 9085 | state_ss->ref, |
| 9086 | state_ss->read_mask); |
| 9087 | _sg_stats_add(gl.num_render_state, 1); |
| 9088 | } |
| 9089 | if ((state_sfs->fail_op != cache_sfs->fail_op) || |
| 9090 | (state_sfs->depth_fail_op != cache_sfs->depth_fail_op) || |
| 9091 | (state_sfs->pass_op != cache_sfs->pass_op)) |
| 9092 | { |
| 9093 | cache_sfs->fail_op = state_sfs->fail_op; |
| 9094 | cache_sfs->depth_fail_op = state_sfs->depth_fail_op; |
| 9095 | cache_sfs->pass_op = state_sfs->pass_op; |
| 9096 | glStencilOpSeparate(gl_face, |
| 9097 | _sg_gl_stencil_op(state_sfs->fail_op), |
| 9098 | _sg_gl_stencil_op(state_sfs->depth_fail_op), |
| 9099 | _sg_gl_stencil_op(state_sfs->pass_op)); |
| 9100 | _sg_stats_add(gl.num_render_state, 1); |
| 9101 | } |
| 9102 | } |
| 9103 | cache_ss->read_mask = state_ss->read_mask; |
| 9104 | cache_ss->ref = state_ss->ref; |
| 9105 | } |
| 9106 | |
| 9107 | if (pip->cmn.color_count > 0) { |
| 9108 | // update blend state |
| 9109 | // FIXME: separate blend state per color attachment not support, needs GL4 |
| 9110 | const sg_blend_state* state_bs = &pip->gl.blend; |
| 9111 | sg_blend_state* cache_bs = &_sg.gl.cache.blend; |
| 9112 | if (state_bs->enabled != cache_bs->enabled) { |
| 9113 | cache_bs->enabled = state_bs->enabled; |
| 9114 | if (state_bs->enabled) { |
| 9115 | glEnable(GL_BLEND); |
| 9116 | } else { |
| 9117 | glDisable(GL_BLEND); |
| 9118 | } |
| 9119 | _sg_stats_add(gl.num_render_state, 1); |
| 9120 | } |
| 9121 | if ((state_bs->src_factor_rgb != cache_bs->src_factor_rgb) || |
| 9122 | (state_bs->dst_factor_rgb != cache_bs->dst_factor_rgb) || |
| 9123 | (state_bs->src_factor_alpha != cache_bs->src_factor_alpha) || |
| 9124 | (state_bs->dst_factor_alpha != cache_bs->dst_factor_alpha)) |
| 9125 | { |
| 9126 | cache_bs->src_factor_rgb = state_bs->src_factor_rgb; |
| 9127 | cache_bs->dst_factor_rgb = state_bs->dst_factor_rgb; |
| 9128 | cache_bs->src_factor_alpha = state_bs->src_factor_alpha; |
| 9129 | cache_bs->dst_factor_alpha = state_bs->dst_factor_alpha; |
| 9130 | glBlendFuncSeparate(_sg_gl_blend_factor(state_bs->src_factor_rgb), |
| 9131 | _sg_gl_blend_factor(state_bs->dst_factor_rgb), |
| 9132 | _sg_gl_blend_factor(state_bs->src_factor_alpha), |
| 9133 | _sg_gl_blend_factor(state_bs->dst_factor_alpha)); |
| 9134 | _sg_stats_add(gl.num_render_state, 1); |
| 9135 | } |
| 9136 | if ((state_bs->op_rgb != cache_bs->op_rgb) || (state_bs->op_alpha != cache_bs->op_alpha)) { |
| 9137 | cache_bs->op_rgb = state_bs->op_rgb; |
| 9138 | cache_bs->op_alpha = state_bs->op_alpha; |
| 9139 | glBlendEquationSeparate(_sg_gl_blend_op(state_bs->op_rgb), _sg_gl_blend_op(state_bs->op_alpha)); |
| 9140 | _sg_stats_add(gl.num_render_state, 1); |
| 9141 | } |
| 9142 | |
| 9143 | // standalone color target state |
| 9144 | for (GLuint i = 0; i < (GLuint)pip->cmn.color_count; i++) { |
| 9145 | if (pip->gl.color_write_mask[i] != _sg.gl.cache.color_write_mask[i]) { |
| 9146 | const sg_color_mask cm = pip->gl.color_write_mask[i]; |
| 9147 | _sg.gl.cache.color_write_mask[i] = cm; |
| 9148 | #ifdef SOKOL_GLCORE |
| 9149 | glColorMaski(i, |
| 9150 | (cm & SG_COLORMASK_R) != 0, |
| 9151 | (cm & SG_COLORMASK_G) != 0, |
| 9152 | (cm & SG_COLORMASK_B) != 0, |
| 9153 | (cm & SG_COLORMASK_A) != 0); |
| 9154 | #else |
| 9155 | if (0 == i) { |
| 9156 | glColorMask((cm & SG_COLORMASK_R) != 0, |
| 9157 | (cm & SG_COLORMASK_G) != 0, |
| 9158 | (cm & SG_COLORMASK_B) != 0, |
| 9159 | (cm & SG_COLORMASK_A) != 0); |
| 9160 | } |
| 9161 | #endif |
| 9162 | _sg_stats_add(gl.num_render_state, 1); |
| 9163 | } |
| 9164 | } |
| 9165 | |
| 9166 | if (!_sg_fequal(pip->cmn.blend_color.r, _sg.gl.cache.blend_color.r, 0.0001f) || |
| 9167 | !_sg_fequal(pip->cmn.blend_color.g, _sg.gl.cache.blend_color.g, 0.0001f) || |
| 9168 | !_sg_fequal(pip->cmn.blend_color.b, _sg.gl.cache.blend_color.b, 0.0001f) || |
| 9169 | !_sg_fequal(pip->cmn.blend_color.a, _sg.gl.cache.blend_color.a, 0.0001f)) |
| 9170 | { |
| 9171 | sg_color c = pip->cmn.blend_color; |
| 9172 | _sg.gl.cache.blend_color = c; |
| 9173 | glBlendColor(c.r, c.g, c.b, c.a); |
| 9174 | _sg_stats_add(gl.num_render_state, 1); |
| 9175 | } |
| 9176 | } // pip->cmn.color_count > 0 |
| 9177 | |
| 9178 | if (pip->gl.cull_mode != _sg.gl.cache.cull_mode) { |
| 9179 | _sg.gl.cache.cull_mode = pip->gl.cull_mode; |
| 9180 | if (SG_CULLMODE_NONE == pip->gl.cull_mode) { |
| 9181 | glDisable(GL_CULL_FACE); |
| 9182 | _sg_stats_add(gl.num_render_state, 1); |
| 9183 | } else { |
| 9184 | glEnable(GL_CULL_FACE); |
| 9185 | GLenum gl_mode = (SG_CULLMODE_FRONT == pip->gl.cull_mode) ? GL_FRONT : GL_BACK; |
| 9186 | glCullFace(gl_mode); |
| 9187 | _sg_stats_add(gl.num_render_state, 2); |
| 9188 | } |
| 9189 | } |
| 9190 | if (pip->gl.face_winding != _sg.gl.cache.face_winding) { |
| 9191 | _sg.gl.cache.face_winding = pip->gl.face_winding; |
| 9192 | GLenum gl_winding = (SG_FACEWINDING_CW == pip->gl.face_winding) ? GL_CW : GL_CCW; |
| 9193 | glFrontFace(gl_winding); |
| 9194 | _sg_stats_add(gl.num_render_state, 1); |
| 9195 | } |
| 9196 | if (pip->gl.alpha_to_coverage_enabled != _sg.gl.cache.alpha_to_coverage_enabled) { |
| 9197 | _sg.gl.cache.alpha_to_coverage_enabled = pip->gl.alpha_to_coverage_enabled; |
| 9198 | if (pip->gl.alpha_to_coverage_enabled) { |
| 9199 | glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE); |
| 9200 | } else { |
| 9201 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE); |
| 9202 | } |
| 9203 | _sg_stats_add(gl.num_render_state, 1); |
| 9204 | } |
| 9205 | #ifdef SOKOL_GLCORE |
| 9206 | if (pip->gl.sample_count != _sg.gl.cache.sample_count) { |
| 9207 | _sg.gl.cache.sample_count = pip->gl.sample_count; |
| 9208 | if (pip->gl.sample_count > 1) { |
| 9209 | glEnable(GL_MULTISAMPLE); |
| 9210 | } else { |
| 9211 | glDisable(GL_MULTISAMPLE); |
| 9212 | } |
| 9213 | _sg_stats_add(gl.num_render_state, 1); |
| 9214 | } |
| 9215 | #endif |
| 9216 | |
| 9217 | // bind shader program |
| 9218 | if (pip->shader->gl.prog != _sg.gl.cache.prog) { |
| 9219 | _sg.gl.cache.prog = pip->shader->gl.prog; |
| 9220 | glUseProgram(pip->shader->gl.prog); |
| 9221 | _sg_stats_add(gl.num_use_program, 1); |
| 9222 | } |
| 9223 | } |
| 9224 | _SG_GL_CHECK_ERROR(); |
| 9225 | } |
| 9226 | |
| 9227 | _SOKOL_PRIVATE bool _sg_gl_apply_bindings(_sg_bindings_t* bnd) { |
| 9228 | SOKOL_ASSERT(bnd); |
| 9229 | SOKOL_ASSERT(bnd->pip); |
| 9230 | _SG_GL_CHECK_ERROR(); |
| 9231 | |
| 9232 | // bind combined image-samplers |
| 9233 | _SG_GL_CHECK_ERROR(); |
| 9234 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 9235 | const _sg_shader_stage_t* stage = &bnd->pip->shader->cmn.stage[stage_index]; |
| 9236 | const _sg_gl_shader_stage_t* gl_stage = &bnd->pip->shader->gl.stage[stage_index]; |
| 9237 | _sg_image_t** imgs = (stage_index == SG_SHADERSTAGE_VS) ? bnd->vs_imgs : bnd->fs_imgs; |
| 9238 | _sg_sampler_t** smps = (stage_index == SG_SHADERSTAGE_VS) ? bnd->vs_smps : bnd->fs_smps; |
| 9239 | const int num_imgs = (stage_index == SG_SHADERSTAGE_VS) ? bnd->num_vs_imgs : bnd->num_fs_imgs; |
| 9240 | const int num_smps = (stage_index == SG_SHADERSTAGE_VS) ? bnd->num_vs_smps : bnd->num_fs_smps; |
| 9241 | SOKOL_ASSERT(num_imgs == stage->num_images); _SOKOL_UNUSED(num_imgs); |
| 9242 | SOKOL_ASSERT(num_smps == stage->num_samplers); _SOKOL_UNUSED(num_smps); |
| 9243 | for (int img_smp_index = 0; img_smp_index < stage->num_image_samplers; img_smp_index++) { |
| 9244 | const int gl_tex_slot = gl_stage->image_samplers[img_smp_index].gl_tex_slot; |
| 9245 | if (gl_tex_slot != -1) { |
| 9246 | const int img_index = stage->image_samplers[img_smp_index].image_slot; |
| 9247 | const int smp_index = stage->image_samplers[img_smp_index].sampler_slot; |
| 9248 | SOKOL_ASSERT(img_index < num_imgs); |
| 9249 | SOKOL_ASSERT(smp_index < num_smps); |
| 9250 | _sg_image_t* img = imgs[img_index]; |
| 9251 | _sg_sampler_t* smp = smps[smp_index]; |
| 9252 | const GLenum gl_tgt = img->gl.target; |
| 9253 | const GLuint gl_tex = img->gl.tex[img->cmn.active_slot]; |
| 9254 | const GLuint gl_smp = smp->gl.smp; |
| 9255 | _sg_gl_cache_bind_texture_sampler(gl_tex_slot, gl_tgt, gl_tex, gl_smp); |
| 9256 | } |
| 9257 | } |
| 9258 | } |
| 9259 | _SG_GL_CHECK_ERROR(); |
| 9260 | |
| 9261 | // bind storage buffers |
| 9262 | for (int slot = 0; slot < bnd->num_vs_sbufs; slot++) { |
| 9263 | _sg_buffer_t* sb = bnd->vs_sbufs[slot]; |
| 9264 | GLuint gl_sb = sb->gl.buf[sb->cmn.active_slot]; |
| 9265 | _sg_gl_cache_bind_storage_buffer(SG_SHADERSTAGE_VS, slot, gl_sb); |
| 9266 | } |
| 9267 | for (int slot = 0; slot < bnd->num_fs_sbufs; slot++) { |
| 9268 | _sg_buffer_t* sb = bnd->fs_sbufs[slot]; |
| 9269 | GLuint gl_sb = sb->gl.buf[sb->cmn.active_slot]; |
| 9270 | _sg_gl_cache_bind_storage_buffer(SG_SHADERSTAGE_FS, slot, gl_sb); |
| 9271 | } |
| 9272 | |
| 9273 | // index buffer (can be 0) |
| 9274 | const GLuint gl_ib = bnd->ib ? bnd->ib->gl.buf[bnd->ib->cmn.active_slot] : 0; |
| 9275 | _sg_gl_cache_bind_buffer(GL_ELEMENT_ARRAY_BUFFER, gl_ib); |
| 9276 | _sg.gl.cache.cur_ib_offset = bnd->ib_offset; |
| 9277 | |
| 9278 | // vertex attributes |
| 9279 | for (GLuint attr_index = 0; attr_index < (GLuint)_sg.limits.max_vertex_attrs; attr_index++) { |
| 9280 | _sg_gl_attr_t* attr = &bnd->pip->gl.attrs[attr_index]; |
| 9281 | _sg_gl_cache_attr_t* cache_attr = &_sg.gl.cache.attrs[attr_index]; |
| 9282 | bool cache_attr_dirty = false; |
| 9283 | int vb_offset = 0; |
| 9284 | GLuint gl_vb = 0; |
| 9285 | if (attr->vb_index >= 0) { |
| 9286 | // attribute is enabled |
| 9287 | SOKOL_ASSERT(attr->vb_index < bnd->num_vbs); |
| 9288 | _sg_buffer_t* vb = bnd->vbs[attr->vb_index]; |
| 9289 | SOKOL_ASSERT(vb); |
| 9290 | gl_vb = vb->gl.buf[vb->cmn.active_slot]; |
| 9291 | vb_offset = bnd->vb_offsets[attr->vb_index] + attr->offset; |
| 9292 | if ((gl_vb != cache_attr->gl_vbuf) || |
| 9293 | (attr->size != cache_attr->gl_attr.size) || |
| 9294 | (attr->type != cache_attr->gl_attr.type) || |
| 9295 | (attr->normalized != cache_attr->gl_attr.normalized) || |
| 9296 | (attr->stride != cache_attr->gl_attr.stride) || |
| 9297 | (vb_offset != cache_attr->gl_attr.offset) || |
| 9298 | (cache_attr->gl_attr.divisor != attr->divisor)) |
| 9299 | { |
| 9300 | _sg_gl_cache_bind_buffer(GL_ARRAY_BUFFER, gl_vb); |
| 9301 | glVertexAttribPointer(attr_index, attr->size, attr->type, attr->normalized, attr->stride, (const GLvoid*)(GLintptr)vb_offset); |
| 9302 | _sg_stats_add(gl.num_vertex_attrib_pointer, 1); |
| 9303 | glVertexAttribDivisor(attr_index, (GLuint)attr->divisor); |
| 9304 | _sg_stats_add(gl.num_vertex_attrib_divisor, 1); |
| 9305 | cache_attr_dirty = true; |
| 9306 | } |
| 9307 | if (cache_attr->gl_attr.vb_index == -1) { |
| 9308 | glEnableVertexAttribArray(attr_index); |
| 9309 | _sg_stats_add(gl.num_enable_vertex_attrib_array, 1); |
| 9310 | cache_attr_dirty = true; |
| 9311 | } |
| 9312 | } else { |
| 9313 | // attribute is disabled |
| 9314 | if (cache_attr->gl_attr.vb_index != -1) { |
| 9315 | glDisableVertexAttribArray(attr_index); |
| 9316 | _sg_stats_add(gl.num_disable_vertex_attrib_array, 1); |
| 9317 | cache_attr_dirty = true; |
| 9318 | } |
| 9319 | } |
| 9320 | if (cache_attr_dirty) { |
| 9321 | cache_attr->gl_attr = *attr; |
| 9322 | cache_attr->gl_attr.offset = vb_offset; |
| 9323 | cache_attr->gl_vbuf = gl_vb; |
| 9324 | } |
| 9325 | } |
| 9326 | _SG_GL_CHECK_ERROR(); |
| 9327 | return true; |
| 9328 | } |
| 9329 | |
| 9330 | _SOKOL_PRIVATE void _sg_gl_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) { |
| 9331 | SOKOL_ASSERT(_sg.gl.cache.cur_pipeline); |
| 9332 | SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->slot.id == _sg.gl.cache.cur_pipeline_id.id); |
| 9333 | SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->shader->slot.id == _sg.gl.cache.cur_pipeline->cmn.shader_id.id); |
| 9334 | SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->shader->cmn.stage[stage_index].num_uniform_blocks > ub_index); |
| 9335 | SOKOL_ASSERT(_sg.gl.cache.cur_pipeline->shader->cmn.stage[stage_index].uniform_blocks[ub_index].size == data->size); |
| 9336 | const _sg_gl_shader_stage_t* gl_stage = &_sg.gl.cache.cur_pipeline->shader->gl.stage[stage_index]; |
| 9337 | const _sg_gl_uniform_block_t* gl_ub = &gl_stage->uniform_blocks[ub_index]; |
| 9338 | for (int u_index = 0; u_index < gl_ub->num_uniforms; u_index++) { |
| 9339 | const _sg_gl_uniform_t* u = &gl_ub->uniforms[u_index]; |
| 9340 | SOKOL_ASSERT(u->type != SG_UNIFORMTYPE_INVALID); |
| 9341 | if (u->gl_loc == -1) { |
| 9342 | continue; |
| 9343 | } |
| 9344 | _sg_stats_add(gl.num_uniform, 1); |
| 9345 | GLfloat* fptr = (GLfloat*) (((uint8_t*)data->ptr) + u->offset); |
| 9346 | GLint* iptr = (GLint*) (((uint8_t*)data->ptr) + u->offset); |
| 9347 | switch (u->type) { |
| 9348 | case SG_UNIFORMTYPE_INVALID: |
| 9349 | break; |
| 9350 | case SG_UNIFORMTYPE_FLOAT: |
| 9351 | glUniform1fv(u->gl_loc, u->count, fptr); |
| 9352 | break; |
| 9353 | case SG_UNIFORMTYPE_FLOAT2: |
| 9354 | glUniform2fv(u->gl_loc, u->count, fptr); |
| 9355 | break; |
| 9356 | case SG_UNIFORMTYPE_FLOAT3: |
| 9357 | glUniform3fv(u->gl_loc, u->count, fptr); |
| 9358 | break; |
| 9359 | case SG_UNIFORMTYPE_FLOAT4: |
| 9360 | glUniform4fv(u->gl_loc, u->count, fptr); |
| 9361 | break; |
| 9362 | case SG_UNIFORMTYPE_INT: |
| 9363 | glUniform1iv(u->gl_loc, u->count, iptr); |
| 9364 | break; |
| 9365 | case SG_UNIFORMTYPE_INT2: |
| 9366 | glUniform2iv(u->gl_loc, u->count, iptr); |
| 9367 | break; |
| 9368 | case SG_UNIFORMTYPE_INT3: |
| 9369 | glUniform3iv(u->gl_loc, u->count, iptr); |
| 9370 | break; |
| 9371 | case SG_UNIFORMTYPE_INT4: |
| 9372 | glUniform4iv(u->gl_loc, u->count, iptr); |
| 9373 | break; |
| 9374 | case SG_UNIFORMTYPE_MAT4: |
| 9375 | glUniformMatrix4fv(u->gl_loc, u->count, GL_FALSE, fptr); |
| 9376 | break; |
| 9377 | default: |
| 9378 | SOKOL_UNREACHABLE; |
| 9379 | break; |
| 9380 | } |
| 9381 | } |
| 9382 | } |
| 9383 | |
| 9384 | _SOKOL_PRIVATE void _sg_gl_draw(int base_element, int num_elements, int num_instances) { |
| 9385 | SOKOL_ASSERT(_sg.gl.cache.cur_pipeline); |
| 9386 | const GLenum i_type = _sg.gl.cache.cur_index_type; |
| 9387 | const GLenum p_type = _sg.gl.cache.cur_primitive_type; |
| 9388 | const bool use_instanced_draw = (num_instances > 1) || (_sg.gl.cache.cur_pipeline->cmn.use_instanced_draw); |
| 9389 | if (0 != i_type) { |
| 9390 | // indexed rendering |
| 9391 | const int i_size = (i_type == GL_UNSIGNED_SHORT) ? 2 : 4; |
| 9392 | const int ib_offset = _sg.gl.cache.cur_ib_offset; |
| 9393 | const GLvoid* indices = (const GLvoid*)(GLintptr)(base_element*i_size+ib_offset); |
| 9394 | if (use_instanced_draw) { |
| 9395 | glDrawElementsInstanced(p_type, num_elements, i_type, indices, num_instances); |
| 9396 | } else { |
| 9397 | glDrawElements(p_type, num_elements, i_type, indices); |
| 9398 | } |
| 9399 | } else { |
| 9400 | // non-indexed rendering |
| 9401 | if (use_instanced_draw) { |
| 9402 | glDrawArraysInstanced(p_type, base_element, num_elements, num_instances); |
| 9403 | } else { |
| 9404 | glDrawArrays(p_type, base_element, num_elements); |
| 9405 | } |
| 9406 | } |
| 9407 | } |
| 9408 | |
| 9409 | _SOKOL_PRIVATE void _sg_gl_commit(void) { |
| 9410 | // "soft" clear bindings (only those that are actually bound) |
| 9411 | _sg_gl_cache_clear_buffer_bindings(false); |
| 9412 | _sg_gl_cache_clear_texture_sampler_bindings(false); |
| 9413 | } |
| 9414 | |
| 9415 | _SOKOL_PRIVATE void _sg_gl_update_buffer(_sg_buffer_t* buf, const sg_range* data) { |
| 9416 | SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0)); |
| 9417 | // only one update per buffer per frame allowed |
| 9418 | if (++buf->cmn.active_slot >= buf->cmn.num_slots) { |
| 9419 | buf->cmn.active_slot = 0; |
| 9420 | } |
| 9421 | GLenum gl_tgt = _sg_gl_buffer_target(buf->cmn.type); |
| 9422 | SOKOL_ASSERT(buf->cmn.active_slot < SG_NUM_INFLIGHT_FRAMES); |
| 9423 | GLuint gl_buf = buf->gl.buf[buf->cmn.active_slot]; |
| 9424 | SOKOL_ASSERT(gl_buf); |
| 9425 | _SG_GL_CHECK_ERROR(); |
| 9426 | _sg_gl_cache_store_buffer_binding(gl_tgt); |
| 9427 | _sg_gl_cache_bind_buffer(gl_tgt, gl_buf); |
| 9428 | glBufferSubData(gl_tgt, 0, (GLsizeiptr)data->size, data->ptr); |
| 9429 | _sg_gl_cache_restore_buffer_binding(gl_tgt); |
| 9430 | _SG_GL_CHECK_ERROR(); |
| 9431 | } |
| 9432 | |
| 9433 | _SOKOL_PRIVATE void _sg_gl_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) { |
| 9434 | SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0)); |
| 9435 | if (new_frame) { |
| 9436 | if (++buf->cmn.active_slot >= buf->cmn.num_slots) { |
| 9437 | buf->cmn.active_slot = 0; |
| 9438 | } |
| 9439 | } |
| 9440 | GLenum gl_tgt = _sg_gl_buffer_target(buf->cmn.type); |
| 9441 | SOKOL_ASSERT(buf->cmn.active_slot < SG_NUM_INFLIGHT_FRAMES); |
| 9442 | GLuint gl_buf = buf->gl.buf[buf->cmn.active_slot]; |
| 9443 | SOKOL_ASSERT(gl_buf); |
| 9444 | _SG_GL_CHECK_ERROR(); |
| 9445 | _sg_gl_cache_store_buffer_binding(gl_tgt); |
| 9446 | _sg_gl_cache_bind_buffer(gl_tgt, gl_buf); |
| 9447 | glBufferSubData(gl_tgt, buf->cmn.append_pos, (GLsizeiptr)data->size, data->ptr); |
| 9448 | _sg_gl_cache_restore_buffer_binding(gl_tgt); |
| 9449 | _SG_GL_CHECK_ERROR(); |
| 9450 | } |
| 9451 | |
| 9452 | _SOKOL_PRIVATE void _sg_gl_update_image(_sg_image_t* img, const sg_image_data* data) { |
| 9453 | SOKOL_ASSERT(img && data); |
| 9454 | // only one update per image per frame allowed |
| 9455 | if (++img->cmn.active_slot >= img->cmn.num_slots) { |
| 9456 | img->cmn.active_slot = 0; |
| 9457 | } |
| 9458 | SOKOL_ASSERT(img->cmn.active_slot < SG_NUM_INFLIGHT_FRAMES); |
| 9459 | SOKOL_ASSERT(0 != img->gl.tex[img->cmn.active_slot]); |
| 9460 | _sg_gl_cache_store_texture_sampler_binding(0); |
| 9461 | _sg_gl_cache_bind_texture_sampler(0, img->gl.target, img->gl.tex[img->cmn.active_slot], 0); |
| 9462 | const GLenum gl_img_format = _sg_gl_teximage_format(img->cmn.pixel_format); |
| 9463 | const GLenum gl_img_type = _sg_gl_teximage_type(img->cmn.pixel_format); |
| 9464 | const int num_faces = img->cmn.type == SG_IMAGETYPE_CUBE ? 6 : 1; |
| 9465 | const int num_mips = img->cmn.num_mipmaps; |
| 9466 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 9467 | for (int mip_index = 0; mip_index < num_mips; mip_index++) { |
| 9468 | GLenum gl_img_target = img->gl.target; |
| 9469 | if (SG_IMAGETYPE_CUBE == img->cmn.type) { |
| 9470 | gl_img_target = _sg_gl_cubeface_target(face_index); |
| 9471 | } |
| 9472 | const GLvoid* data_ptr = data->subimage[face_index][mip_index].ptr; |
| 9473 | int mip_width = _sg_miplevel_dim(img->cmn.width, mip_index); |
| 9474 | int mip_height = _sg_miplevel_dim(img->cmn.height, mip_index); |
| 9475 | if ((SG_IMAGETYPE_2D == img->cmn.type) || (SG_IMAGETYPE_CUBE == img->cmn.type)) { |
| 9476 | glTexSubImage2D(gl_img_target, mip_index, |
| 9477 | 0, 0, |
| 9478 | mip_width, mip_height, |
| 9479 | gl_img_format, gl_img_type, |
| 9480 | data_ptr); |
| 9481 | } else if ((SG_IMAGETYPE_3D == img->cmn.type) || (SG_IMAGETYPE_ARRAY == img->cmn.type)) { |
| 9482 | int mip_depth = img->cmn.num_slices; |
| 9483 | if (SG_IMAGETYPE_3D == img->cmn.type) { |
| 9484 | mip_depth = _sg_miplevel_dim(img->cmn.num_slices, mip_index); |
| 9485 | } |
| 9486 | glTexSubImage3D(gl_img_target, mip_index, |
| 9487 | 0, 0, 0, |
| 9488 | mip_width, mip_height, mip_depth, |
| 9489 | gl_img_format, gl_img_type, |
| 9490 | data_ptr); |
| 9491 | |
| 9492 | } |
| 9493 | } |
| 9494 | } |
| 9495 | _sg_gl_cache_restore_texture_sampler_binding(0); |
| 9496 | } |
| 9497 | |
| 9498 | // ██████ ██████ ██████ ██ ██ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████ |
| 9499 | // ██ ██ ██ ██ ██ ███ ███ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ |
| 9500 | // ██ ██ █████ ██ ██ ██ ██ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██ |
| 9501 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 9502 | // ██████ ██████ ██████ ██ ██ ██████ ██ ██ ██████ ██ ██ ███████ ██ ████ ██████ |
| 9503 | // |
| 9504 | // >>d3d11 backend |
| 9505 | #elif defined(SOKOL_D3D11) |
| 9506 | |
| 9507 | #define _SG_D3D11_MAX_SHADERSTAGE_SRVS (32) |
| 9508 | #define _SG_D3D11_SHADERSTAGE_IMAGE_SRV_OFFSET (0) |
| 9509 | #define _SG_D3D11_SHADERSTAGE_BUFFER_SRV_OFFSET (16) |
| 9510 | |
| 9511 | #if defined(__cplusplus) |
| 9512 | #define _sg_d3d11_AddRef(self) (self)->AddRef() |
| 9513 | #else |
| 9514 | #define _sg_d3d11_AddRef(self) (self)->lpVtbl->AddRef(self) |
| 9515 | #endif |
| 9516 | |
| 9517 | #if defined(__cplusplus) |
| 9518 | #define _sg_d3d11_Release(self) (self)->Release() |
| 9519 | #else |
| 9520 | #define _sg_d3d11_Release(self) (self)->lpVtbl->Release(self) |
| 9521 | #endif |
| 9522 | |
| 9523 | // NOTE: This needs to be a macro since we can't use the polymorphism in C. It's called on many kinds of resources. |
| 9524 | // NOTE: Based on microsoft docs, it's fine to call this with pData=NULL if DataSize is also zero. |
| 9525 | #if defined(__cplusplus) |
| 9526 | #define _sg_d3d11_SetPrivateData(self, guid, DataSize, pData) (self)->SetPrivateData(guid, DataSize, pData) |
| 9527 | #else |
| 9528 | #define _sg_d3d11_SetPrivateData(self, guid, DataSize, pData) (self)->lpVtbl->SetPrivateData(self, guid, DataSize, pData) |
| 9529 | #endif |
| 9530 | |
| 9531 | #if defined(__cplusplus) |
| 9532 | #define _sg_win32_refguid(guid) guid |
| 9533 | #else |
| 9534 | #define _sg_win32_refguid(guid) &guid |
| 9535 | #endif |
| 9536 | |
| 9537 | static const GUID _sg_d3d11_WKPDID_D3DDebugObjectName = { 0x429b8c22,0x9188,0x4b0c, {0x87,0x42,0xac,0xb0,0xbf,0x85,0xc2,0x00} }; |
| 9538 | |
| 9539 | #if defined(SOKOL_DEBUG) |
| 9540 | #define _sg_d3d11_setlabel(self, label) _sg_d3d11_SetPrivateData(self, _sg_win32_refguid(_sg_d3d11_WKPDID_D3DDebugObjectName), label ? (UINT)strlen(label) : 0, label) |
| 9541 | #else |
| 9542 | #define _sg_d3d11_setlabel(self, label) |
| 9543 | #endif |
| 9544 | |
| 9545 | |
| 9546 | //-- D3D11 C/C++ wrappers ------------------------------------------------------ |
| 9547 | static inline HRESULT _sg_d3d11_CheckFormatSupport(ID3D11Device* self, DXGI_FORMAT Format, UINT* pFormatSupport) { |
| 9548 | #if defined(__cplusplus) |
| 9549 | return self->CheckFormatSupport(Format, pFormatSupport); |
| 9550 | #else |
| 9551 | return self->lpVtbl->CheckFormatSupport(self, Format, pFormatSupport); |
| 9552 | #endif |
| 9553 | } |
| 9554 | |
| 9555 | static inline void _sg_d3d11_OMSetRenderTargets(ID3D11DeviceContext* self, UINT NumViews, ID3D11RenderTargetView* const* ppRenderTargetViews, ID3D11DepthStencilView *pDepthStencilView) { |
| 9556 | #if defined(__cplusplus) |
| 9557 | self->OMSetRenderTargets(NumViews, ppRenderTargetViews, pDepthStencilView); |
| 9558 | #else |
| 9559 | self->lpVtbl->OMSetRenderTargets(self, NumViews, ppRenderTargetViews, pDepthStencilView); |
| 9560 | #endif |
| 9561 | } |
| 9562 | |
| 9563 | static inline void _sg_d3d11_RSSetState(ID3D11DeviceContext* self, ID3D11RasterizerState* pRasterizerState) { |
| 9564 | #if defined(__cplusplus) |
| 9565 | self->RSSetState(pRasterizerState); |
| 9566 | #else |
| 9567 | self->lpVtbl->RSSetState(self, pRasterizerState); |
| 9568 | #endif |
| 9569 | } |
| 9570 | |
| 9571 | static inline void _sg_d3d11_OMSetDepthStencilState(ID3D11DeviceContext* self, ID3D11DepthStencilState* pDepthStencilState, UINT StencilRef) { |
| 9572 | #if defined(__cplusplus) |
| 9573 | self->OMSetDepthStencilState(pDepthStencilState, StencilRef); |
| 9574 | #else |
| 9575 | self->lpVtbl->OMSetDepthStencilState(self, pDepthStencilState, StencilRef); |
| 9576 | #endif |
| 9577 | } |
| 9578 | |
| 9579 | static inline void _sg_d3d11_OMSetBlendState(ID3D11DeviceContext* self, ID3D11BlendState* pBlendState, const FLOAT BlendFactor[4], UINT SampleMask) { |
| 9580 | #if defined(__cplusplus) |
| 9581 | self->OMSetBlendState(pBlendState, BlendFactor, SampleMask); |
| 9582 | #else |
| 9583 | self->lpVtbl->OMSetBlendState(self, pBlendState, BlendFactor, SampleMask); |
| 9584 | #endif |
| 9585 | } |
| 9586 | |
| 9587 | static inline void _sg_d3d11_IASetVertexBuffers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumBuffers, ID3D11Buffer* const* ppVertexBuffers, const UINT* pStrides, const UINT* pOffsets) { |
| 9588 | #if defined(__cplusplus) |
| 9589 | self->IASetVertexBuffers(StartSlot, NumBuffers, ppVertexBuffers, pStrides, pOffsets); |
| 9590 | #else |
| 9591 | self->lpVtbl->IASetVertexBuffers(self, StartSlot, NumBuffers, ppVertexBuffers, pStrides, pOffsets); |
| 9592 | #endif |
| 9593 | } |
| 9594 | |
| 9595 | static inline void _sg_d3d11_IASetIndexBuffer(ID3D11DeviceContext* self, ID3D11Buffer* pIndexBuffer, DXGI_FORMAT Format, UINT Offset) { |
| 9596 | #if defined(__cplusplus) |
| 9597 | self->IASetIndexBuffer(pIndexBuffer, Format, Offset); |
| 9598 | #else |
| 9599 | self->lpVtbl->IASetIndexBuffer(self, pIndexBuffer, Format, Offset); |
| 9600 | #endif |
| 9601 | } |
| 9602 | |
| 9603 | static inline void _sg_d3d11_IASetInputLayout(ID3D11DeviceContext* self, ID3D11InputLayout* pInputLayout) { |
| 9604 | #if defined(__cplusplus) |
| 9605 | self->IASetInputLayout(pInputLayout); |
| 9606 | #else |
| 9607 | self->lpVtbl->IASetInputLayout(self, pInputLayout); |
| 9608 | #endif |
| 9609 | } |
| 9610 | |
| 9611 | static inline void _sg_d3d11_VSSetShader(ID3D11DeviceContext* self, ID3D11VertexShader* pVertexShader, ID3D11ClassInstance* const* ppClassInstances, UINT NumClassInstances) { |
| 9612 | #if defined(__cplusplus) |
| 9613 | self->VSSetShader(pVertexShader, ppClassInstances, NumClassInstances); |
| 9614 | #else |
| 9615 | self->lpVtbl->VSSetShader(self, pVertexShader, ppClassInstances, NumClassInstances); |
| 9616 | #endif |
| 9617 | } |
| 9618 | |
| 9619 | static inline void _sg_d3d11_PSSetShader(ID3D11DeviceContext* self, ID3D11PixelShader* pPixelShader, ID3D11ClassInstance* const* ppClassInstances, UINT NumClassInstances) { |
| 9620 | #if defined(__cplusplus) |
| 9621 | self->PSSetShader(pPixelShader, ppClassInstances, NumClassInstances); |
| 9622 | #else |
| 9623 | self->lpVtbl->PSSetShader(self, pPixelShader, ppClassInstances, NumClassInstances); |
| 9624 | #endif |
| 9625 | } |
| 9626 | |
| 9627 | static inline void _sg_d3d11_VSSetConstantBuffers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumBuffers, ID3D11Buffer* const* ppConstantBuffers) { |
| 9628 | #if defined(__cplusplus) |
| 9629 | self->VSSetConstantBuffers(StartSlot, NumBuffers, ppConstantBuffers); |
| 9630 | #else |
| 9631 | self->lpVtbl->VSSetConstantBuffers(self, StartSlot, NumBuffers, ppConstantBuffers); |
| 9632 | #endif |
| 9633 | } |
| 9634 | |
| 9635 | static inline void _sg_d3d11_PSSetConstantBuffers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumBuffers, ID3D11Buffer* const* ppConstantBuffers) { |
| 9636 | #if defined(__cplusplus) |
| 9637 | self->PSSetConstantBuffers(StartSlot, NumBuffers, ppConstantBuffers); |
| 9638 | #else |
| 9639 | self->lpVtbl->PSSetConstantBuffers(self, StartSlot, NumBuffers, ppConstantBuffers); |
| 9640 | #endif |
| 9641 | } |
| 9642 | |
| 9643 | static inline void _sg_d3d11_VSSetShaderResources(ID3D11DeviceContext* self, UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView* const* ppShaderResourceViews) { |
| 9644 | #if defined(__cplusplus) |
| 9645 | self->VSSetShaderResources(StartSlot, NumViews, ppShaderResourceViews); |
| 9646 | #else |
| 9647 | self->lpVtbl->VSSetShaderResources(self, StartSlot, NumViews, ppShaderResourceViews); |
| 9648 | #endif |
| 9649 | } |
| 9650 | |
| 9651 | static inline void _sg_d3d11_PSSetShaderResources(ID3D11DeviceContext* self, UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView* const* ppShaderResourceViews) { |
| 9652 | #if defined(__cplusplus) |
| 9653 | self->PSSetShaderResources(StartSlot, NumViews, ppShaderResourceViews); |
| 9654 | #else |
| 9655 | self->lpVtbl->PSSetShaderResources(self, StartSlot, NumViews, ppShaderResourceViews); |
| 9656 | #endif |
| 9657 | } |
| 9658 | |
| 9659 | static inline void _sg_d3d11_VSSetSamplers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumSamplers, ID3D11SamplerState* const* ppSamplers) { |
| 9660 | #if defined(__cplusplus) |
| 9661 | self->VSSetSamplers(StartSlot, NumSamplers, ppSamplers); |
| 9662 | #else |
| 9663 | self->lpVtbl->VSSetSamplers(self, StartSlot, NumSamplers, ppSamplers); |
| 9664 | #endif |
| 9665 | } |
| 9666 | |
| 9667 | static inline void _sg_d3d11_PSSetSamplers(ID3D11DeviceContext* self, UINT StartSlot, UINT NumSamplers, ID3D11SamplerState* const* ppSamplers) { |
| 9668 | #if defined(__cplusplus) |
| 9669 | self->PSSetSamplers(StartSlot, NumSamplers, ppSamplers); |
| 9670 | #else |
| 9671 | self->lpVtbl->PSSetSamplers(self, StartSlot, NumSamplers, ppSamplers); |
| 9672 | #endif |
| 9673 | } |
| 9674 | |
| 9675 | static inline HRESULT _sg_d3d11_CreateBuffer(ID3D11Device* self, const D3D11_BUFFER_DESC* pDesc, const D3D11_SUBRESOURCE_DATA* pInitialData, ID3D11Buffer** ppBuffer) { |
| 9676 | #if defined(__cplusplus) |
| 9677 | return self->CreateBuffer(pDesc, pInitialData, ppBuffer); |
| 9678 | #else |
| 9679 | return self->lpVtbl->CreateBuffer(self, pDesc, pInitialData, ppBuffer); |
| 9680 | #endif |
| 9681 | } |
| 9682 | |
| 9683 | static inline HRESULT _sg_d3d11_CreateTexture2D(ID3D11Device* self, const D3D11_TEXTURE2D_DESC* pDesc, const D3D11_SUBRESOURCE_DATA* pInitialData, ID3D11Texture2D** ppTexture2D) { |
| 9684 | #if defined(__cplusplus) |
| 9685 | return self->CreateTexture2D(pDesc, pInitialData, ppTexture2D); |
| 9686 | #else |
| 9687 | return self->lpVtbl->CreateTexture2D(self, pDesc, pInitialData, ppTexture2D); |
| 9688 | #endif |
| 9689 | } |
| 9690 | |
| 9691 | static inline HRESULT _sg_d3d11_CreateShaderResourceView(ID3D11Device* self, ID3D11Resource* pResource, const D3D11_SHADER_RESOURCE_VIEW_DESC* pDesc, ID3D11ShaderResourceView** ppSRView) { |
| 9692 | #if defined(__cplusplus) |
| 9693 | return self->CreateShaderResourceView(pResource, pDesc, ppSRView); |
| 9694 | #else |
| 9695 | return self->lpVtbl->CreateShaderResourceView(self, pResource, pDesc, ppSRView); |
| 9696 | #endif |
| 9697 | } |
| 9698 | |
| 9699 | static inline void _sg_d3d11_GetResource(ID3D11View* self, ID3D11Resource** ppResource) { |
| 9700 | #if defined(__cplusplus) |
| 9701 | self->GetResource(ppResource); |
| 9702 | #else |
| 9703 | self->lpVtbl->GetResource(self, ppResource); |
| 9704 | #endif |
| 9705 | } |
| 9706 | |
| 9707 | static inline HRESULT _sg_d3d11_CreateTexture3D(ID3D11Device* self, const D3D11_TEXTURE3D_DESC* pDesc, const D3D11_SUBRESOURCE_DATA* pInitialData, ID3D11Texture3D** ppTexture3D) { |
| 9708 | #if defined(__cplusplus) |
| 9709 | return self->CreateTexture3D(pDesc, pInitialData, ppTexture3D); |
| 9710 | #else |
| 9711 | return self->lpVtbl->CreateTexture3D(self, pDesc, pInitialData, ppTexture3D); |
| 9712 | #endif |
| 9713 | } |
| 9714 | |
| 9715 | static inline HRESULT _sg_d3d11_CreateSamplerState(ID3D11Device* self, const D3D11_SAMPLER_DESC* pSamplerDesc, ID3D11SamplerState** ppSamplerState) { |
| 9716 | #if defined(__cplusplus) |
| 9717 | return self->CreateSamplerState(pSamplerDesc, ppSamplerState); |
| 9718 | #else |
| 9719 | return self->lpVtbl->CreateSamplerState(self, pSamplerDesc, ppSamplerState); |
| 9720 | #endif |
| 9721 | } |
| 9722 | |
| 9723 | static inline LPVOID _sg_d3d11_GetBufferPointer(ID3D10Blob* self) { |
| 9724 | #if defined(__cplusplus) |
| 9725 | return self->GetBufferPointer(); |
| 9726 | #else |
| 9727 | return self->lpVtbl->GetBufferPointer(self); |
| 9728 | #endif |
| 9729 | } |
| 9730 | |
| 9731 | static inline SIZE_T _sg_d3d11_GetBufferSize(ID3D10Blob* self) { |
| 9732 | #if defined(__cplusplus) |
| 9733 | return self->GetBufferSize(); |
| 9734 | #else |
| 9735 | return self->lpVtbl->GetBufferSize(self); |
| 9736 | #endif |
| 9737 | } |
| 9738 | |
| 9739 | static inline HRESULT _sg_d3d11_CreateVertexShader(ID3D11Device* self, const void* pShaderBytecode, SIZE_T BytecodeLength, ID3D11ClassLinkage* pClassLinkage, ID3D11VertexShader** ppVertexShader) { |
| 9740 | #if defined(__cplusplus) |
| 9741 | return self->CreateVertexShader(pShaderBytecode, BytecodeLength, pClassLinkage, ppVertexShader); |
| 9742 | #else |
| 9743 | return self->lpVtbl->CreateVertexShader(self, pShaderBytecode, BytecodeLength, pClassLinkage, ppVertexShader); |
| 9744 | #endif |
| 9745 | } |
| 9746 | |
| 9747 | static inline HRESULT _sg_d3d11_CreatePixelShader(ID3D11Device* self, const void* pShaderBytecode, SIZE_T BytecodeLength, ID3D11ClassLinkage* pClassLinkage, ID3D11PixelShader** ppPixelShader) { |
| 9748 | #if defined(__cplusplus) |
| 9749 | return self->CreatePixelShader(pShaderBytecode, BytecodeLength, pClassLinkage, ppPixelShader); |
| 9750 | #else |
| 9751 | return self->lpVtbl->CreatePixelShader(self, pShaderBytecode, BytecodeLength, pClassLinkage, ppPixelShader); |
| 9752 | #endif |
| 9753 | } |
| 9754 | |
| 9755 | static inline HRESULT _sg_d3d11_CreateInputLayout(ID3D11Device* self, const D3D11_INPUT_ELEMENT_DESC* pInputElementDescs, UINT NumElements, const void* pShaderBytecodeWithInputSignature, SIZE_T BytecodeLength, ID3D11InputLayout **ppInputLayout) { |
| 9756 | #if defined(__cplusplus) |
| 9757 | return self->CreateInputLayout(pInputElementDescs, NumElements, pShaderBytecodeWithInputSignature, BytecodeLength, ppInputLayout); |
| 9758 | #else |
| 9759 | return self->lpVtbl->CreateInputLayout(self, pInputElementDescs, NumElements, pShaderBytecodeWithInputSignature, BytecodeLength, ppInputLayout); |
| 9760 | #endif |
| 9761 | } |
| 9762 | |
| 9763 | static inline HRESULT _sg_d3d11_CreateRasterizerState(ID3D11Device* self, const D3D11_RASTERIZER_DESC* pRasterizerDesc, ID3D11RasterizerState** ppRasterizerState) { |
| 9764 | #if defined(__cplusplus) |
| 9765 | return self->CreateRasterizerState(pRasterizerDesc, ppRasterizerState); |
| 9766 | #else |
| 9767 | return self->lpVtbl->CreateRasterizerState(self, pRasterizerDesc, ppRasterizerState); |
| 9768 | #endif |
| 9769 | } |
| 9770 | |
| 9771 | static inline HRESULT _sg_d3d11_CreateDepthStencilState(ID3D11Device* self, const D3D11_DEPTH_STENCIL_DESC* pDepthStencilDesc, ID3D11DepthStencilState** ppDepthStencilState) { |
| 9772 | #if defined(__cplusplus) |
| 9773 | return self->CreateDepthStencilState(pDepthStencilDesc, ppDepthStencilState); |
| 9774 | #else |
| 9775 | return self->lpVtbl->CreateDepthStencilState(self, pDepthStencilDesc, ppDepthStencilState); |
| 9776 | #endif |
| 9777 | } |
| 9778 | |
| 9779 | static inline HRESULT _sg_d3d11_CreateBlendState(ID3D11Device* self, const D3D11_BLEND_DESC* pBlendStateDesc, ID3D11BlendState** ppBlendState) { |
| 9780 | #if defined(__cplusplus) |
| 9781 | return self->CreateBlendState(pBlendStateDesc, ppBlendState); |
| 9782 | #else |
| 9783 | return self->lpVtbl->CreateBlendState(self, pBlendStateDesc, ppBlendState); |
| 9784 | #endif |
| 9785 | } |
| 9786 | |
| 9787 | static inline HRESULT _sg_d3d11_CreateRenderTargetView(ID3D11Device* self, ID3D11Resource *pResource, const D3D11_RENDER_TARGET_VIEW_DESC* pDesc, ID3D11RenderTargetView** ppRTView) { |
| 9788 | #if defined(__cplusplus) |
| 9789 | return self->CreateRenderTargetView(pResource, pDesc, ppRTView); |
| 9790 | #else |
| 9791 | return self->lpVtbl->CreateRenderTargetView(self, pResource, pDesc, ppRTView); |
| 9792 | #endif |
| 9793 | } |
| 9794 | |
| 9795 | static inline HRESULT _sg_d3d11_CreateDepthStencilView(ID3D11Device* self, ID3D11Resource* pResource, const D3D11_DEPTH_STENCIL_VIEW_DESC* pDesc, ID3D11DepthStencilView** ppDepthStencilView) { |
| 9796 | #if defined(__cplusplus) |
| 9797 | return self->CreateDepthStencilView(pResource, pDesc, ppDepthStencilView); |
| 9798 | #else |
| 9799 | return self->lpVtbl->CreateDepthStencilView(self, pResource, pDesc, ppDepthStencilView); |
| 9800 | #endif |
| 9801 | } |
| 9802 | |
| 9803 | static inline void _sg_d3d11_RSSetViewports(ID3D11DeviceContext* self, UINT NumViewports, const D3D11_VIEWPORT* pViewports) { |
| 9804 | #if defined(__cplusplus) |
| 9805 | self->RSSetViewports(NumViewports, pViewports); |
| 9806 | #else |
| 9807 | self->lpVtbl->RSSetViewports(self, NumViewports, pViewports); |
| 9808 | #endif |
| 9809 | } |
| 9810 | |
| 9811 | static inline void _sg_d3d11_RSSetScissorRects(ID3D11DeviceContext* self, UINT NumRects, const D3D11_RECT* pRects) { |
| 9812 | #if defined(__cplusplus) |
| 9813 | self->RSSetScissorRects(NumRects, pRects); |
| 9814 | #else |
| 9815 | self->lpVtbl->RSSetScissorRects(self, NumRects, pRects); |
| 9816 | #endif |
| 9817 | } |
| 9818 | |
| 9819 | static inline void _sg_d3d11_ClearRenderTargetView(ID3D11DeviceContext* self, ID3D11RenderTargetView* pRenderTargetView, const FLOAT ColorRGBA[4]) { |
| 9820 | #if defined(__cplusplus) |
| 9821 | self->ClearRenderTargetView(pRenderTargetView, ColorRGBA); |
| 9822 | #else |
| 9823 | self->lpVtbl->ClearRenderTargetView(self, pRenderTargetView, ColorRGBA); |
| 9824 | #endif |
| 9825 | } |
| 9826 | |
| 9827 | static inline void _sg_d3d11_ClearDepthStencilView(ID3D11DeviceContext* self, ID3D11DepthStencilView* pDepthStencilView, UINT ClearFlags, FLOAT Depth, UINT8 Stencil) { |
| 9828 | #if defined(__cplusplus) |
| 9829 | self->ClearDepthStencilView(pDepthStencilView, ClearFlags, Depth, Stencil); |
| 9830 | #else |
| 9831 | self->lpVtbl->ClearDepthStencilView(self, pDepthStencilView, ClearFlags, Depth, Stencil); |
| 9832 | #endif |
| 9833 | } |
| 9834 | |
| 9835 | static inline void _sg_d3d11_ResolveSubresource(ID3D11DeviceContext* self, ID3D11Resource* pDstResource, UINT DstSubresource, ID3D11Resource* pSrcResource, UINT SrcSubresource, DXGI_FORMAT Format) { |
| 9836 | #if defined(__cplusplus) |
| 9837 | self->ResolveSubresource(pDstResource, DstSubresource, pSrcResource, SrcSubresource, Format); |
| 9838 | #else |
| 9839 | self->lpVtbl->ResolveSubresource(self, pDstResource, DstSubresource, pSrcResource, SrcSubresource, Format); |
| 9840 | #endif |
| 9841 | } |
| 9842 | |
| 9843 | static inline void _sg_d3d11_IASetPrimitiveTopology(ID3D11DeviceContext* self, D3D11_PRIMITIVE_TOPOLOGY Topology) { |
| 9844 | #if defined(__cplusplus) |
| 9845 | self->IASetPrimitiveTopology(Topology); |
| 9846 | #else |
| 9847 | self->lpVtbl->IASetPrimitiveTopology(self, Topology); |
| 9848 | #endif |
| 9849 | } |
| 9850 | |
| 9851 | static inline void _sg_d3d11_UpdateSubresource(ID3D11DeviceContext* self, ID3D11Resource* pDstResource, UINT DstSubresource, const D3D11_BOX* pDstBox, const void* pSrcData, UINT SrcRowPitch, UINT SrcDepthPitch) { |
| 9852 | #if defined(__cplusplus) |
| 9853 | self->UpdateSubresource(pDstResource, DstSubresource, pDstBox, pSrcData, SrcRowPitch, SrcDepthPitch); |
| 9854 | #else |
| 9855 | self->lpVtbl->UpdateSubresource(self, pDstResource, DstSubresource, pDstBox, pSrcData, SrcRowPitch, SrcDepthPitch); |
| 9856 | #endif |
| 9857 | } |
| 9858 | |
| 9859 | static inline void _sg_d3d11_DrawIndexed(ID3D11DeviceContext* self, UINT IndexCount, UINT StartIndexLocation, INT BaseVertexLocation) { |
| 9860 | #if defined(__cplusplus) |
| 9861 | self->DrawIndexed(IndexCount, StartIndexLocation, BaseVertexLocation); |
| 9862 | #else |
| 9863 | self->lpVtbl->DrawIndexed(self, IndexCount, StartIndexLocation, BaseVertexLocation); |
| 9864 | #endif |
| 9865 | } |
| 9866 | |
| 9867 | static inline void _sg_d3d11_DrawIndexedInstanced(ID3D11DeviceContext* self, UINT IndexCountPerInstance, UINT InstanceCount, UINT StartIndexLocation, INT BaseVertexLocation, UINT StartInstanceLocation) { |
| 9868 | #if defined(__cplusplus) |
| 9869 | self->DrawIndexedInstanced(IndexCountPerInstance, InstanceCount, StartIndexLocation, BaseVertexLocation, StartInstanceLocation); |
| 9870 | #else |
| 9871 | self->lpVtbl->DrawIndexedInstanced(self, IndexCountPerInstance, InstanceCount, StartIndexLocation, BaseVertexLocation, StartInstanceLocation); |
| 9872 | #endif |
| 9873 | } |
| 9874 | |
| 9875 | static inline void _sg_d3d11_Draw(ID3D11DeviceContext* self, UINT VertexCount, UINT StartVertexLocation) { |
| 9876 | #if defined(__cplusplus) |
| 9877 | self->Draw(VertexCount, StartVertexLocation); |
| 9878 | #else |
| 9879 | self->lpVtbl->Draw(self, VertexCount, StartVertexLocation); |
| 9880 | #endif |
| 9881 | } |
| 9882 | |
| 9883 | static inline void _sg_d3d11_DrawInstanced(ID3D11DeviceContext* self, UINT VertexCountPerInstance, UINT InstanceCount, UINT StartVertexLocation, UINT StartInstanceLocation) { |
| 9884 | #if defined(__cplusplus) |
| 9885 | self->DrawInstanced(VertexCountPerInstance, InstanceCount, StartVertexLocation, StartInstanceLocation); |
| 9886 | #else |
| 9887 | self->lpVtbl->DrawInstanced(self, VertexCountPerInstance, InstanceCount, StartVertexLocation, StartInstanceLocation); |
| 9888 | #endif |
| 9889 | } |
| 9890 | |
| 9891 | static inline HRESULT _sg_d3d11_Map(ID3D11DeviceContext* self, ID3D11Resource* pResource, UINT Subresource, D3D11_MAP MapType, UINT MapFlags, D3D11_MAPPED_SUBRESOURCE* pMappedResource) { |
| 9892 | #if defined(__cplusplus) |
| 9893 | return self->Map(pResource, Subresource, MapType, MapFlags, pMappedResource); |
| 9894 | #else |
| 9895 | return self->lpVtbl->Map(self, pResource, Subresource, MapType, MapFlags, pMappedResource); |
| 9896 | #endif |
| 9897 | } |
| 9898 | |
| 9899 | static inline void _sg_d3d11_Unmap(ID3D11DeviceContext* self, ID3D11Resource* pResource, UINT Subresource) { |
| 9900 | #if defined(__cplusplus) |
| 9901 | self->Unmap(pResource, Subresource); |
| 9902 | #else |
| 9903 | self->lpVtbl->Unmap(self, pResource, Subresource); |
| 9904 | #endif |
| 9905 | } |
| 9906 | |
| 9907 | static inline void _sg_d3d11_ClearState(ID3D11DeviceContext* self) { |
| 9908 | #if defined(__cplusplus) |
| 9909 | self->ClearState(); |
| 9910 | #else |
| 9911 | self->lpVtbl->ClearState(self); |
| 9912 | #endif |
| 9913 | } |
| 9914 | |
| 9915 | //-- enum translation functions ------------------------------------------------ |
| 9916 | _SOKOL_PRIVATE D3D11_USAGE _sg_d3d11_usage(sg_usage usg) { |
| 9917 | switch (usg) { |
| 9918 | case SG_USAGE_IMMUTABLE: |
| 9919 | return D3D11_USAGE_IMMUTABLE; |
| 9920 | case SG_USAGE_DYNAMIC: |
| 9921 | case SG_USAGE_STREAM: |
| 9922 | return D3D11_USAGE_DYNAMIC; |
| 9923 | default: |
| 9924 | SOKOL_UNREACHABLE; |
| 9925 | return (D3D11_USAGE) 0; |
| 9926 | } |
| 9927 | } |
| 9928 | |
| 9929 | _SOKOL_PRIVATE UINT _sg_d3d11_buffer_bind_flags(sg_buffer_type t) { |
| 9930 | switch (t) { |
| 9931 | case SG_BUFFERTYPE_VERTEXBUFFER: |
| 9932 | return D3D11_BIND_VERTEX_BUFFER; |
| 9933 | case SG_BUFFERTYPE_INDEXBUFFER: |
| 9934 | return D3D11_BIND_INDEX_BUFFER; |
| 9935 | case SG_BUFFERTYPE_STORAGEBUFFER: |
| 9936 | // FIXME: for compute shaders we'd want UNORDERED_ACCESS? |
| 9937 | return D3D11_BIND_SHADER_RESOURCE; |
| 9938 | default: |
| 9939 | SOKOL_UNREACHABLE; |
| 9940 | return 0; |
| 9941 | } |
| 9942 | } |
| 9943 | |
| 9944 | _SOKOL_PRIVATE UINT _sg_d3d11_buffer_misc_flags(sg_buffer_type t) { |
| 9945 | switch (t) { |
| 9946 | case SG_BUFFERTYPE_VERTEXBUFFER: |
| 9947 | case SG_BUFFERTYPE_INDEXBUFFER: |
| 9948 | return 0; |
| 9949 | case SG_BUFFERTYPE_STORAGEBUFFER: |
| 9950 | return D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS; |
| 9951 | default: |
| 9952 | SOKOL_UNREACHABLE; |
| 9953 | return 0; |
| 9954 | } |
| 9955 | } |
| 9956 | |
| 9957 | _SOKOL_PRIVATE UINT _sg_d3d11_cpu_access_flags(sg_usage usg) { |
| 9958 | switch (usg) { |
| 9959 | case SG_USAGE_IMMUTABLE: |
| 9960 | return 0; |
| 9961 | case SG_USAGE_DYNAMIC: |
| 9962 | case SG_USAGE_STREAM: |
| 9963 | return D3D11_CPU_ACCESS_WRITE; |
| 9964 | default: |
| 9965 | SOKOL_UNREACHABLE; |
| 9966 | return 0; |
| 9967 | } |
| 9968 | } |
| 9969 | |
| 9970 | _SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_texture_pixel_format(sg_pixel_format fmt) { |
| 9971 | switch (fmt) { |
| 9972 | case SG_PIXELFORMAT_R8: return DXGI_FORMAT_R8_UNORM; |
| 9973 | case SG_PIXELFORMAT_R8SN: return DXGI_FORMAT_R8_SNORM; |
| 9974 | case SG_PIXELFORMAT_R8UI: return DXGI_FORMAT_R8_UINT; |
| 9975 | case SG_PIXELFORMAT_R8SI: return DXGI_FORMAT_R8_SINT; |
| 9976 | case SG_PIXELFORMAT_R16: return DXGI_FORMAT_R16_UNORM; |
| 9977 | case SG_PIXELFORMAT_R16SN: return DXGI_FORMAT_R16_SNORM; |
| 9978 | case SG_PIXELFORMAT_R16UI: return DXGI_FORMAT_R16_UINT; |
| 9979 | case SG_PIXELFORMAT_R16SI: return DXGI_FORMAT_R16_SINT; |
| 9980 | case SG_PIXELFORMAT_R16F: return DXGI_FORMAT_R16_FLOAT; |
| 9981 | case SG_PIXELFORMAT_RG8: return DXGI_FORMAT_R8G8_UNORM; |
| 9982 | case SG_PIXELFORMAT_RG8SN: return DXGI_FORMAT_R8G8_SNORM; |
| 9983 | case SG_PIXELFORMAT_RG8UI: return DXGI_FORMAT_R8G8_UINT; |
| 9984 | case SG_PIXELFORMAT_RG8SI: return DXGI_FORMAT_R8G8_SINT; |
| 9985 | case SG_PIXELFORMAT_R32UI: return DXGI_FORMAT_R32_UINT; |
| 9986 | case SG_PIXELFORMAT_R32SI: return DXGI_FORMAT_R32_SINT; |
| 9987 | case SG_PIXELFORMAT_R32F: return DXGI_FORMAT_R32_FLOAT; |
| 9988 | case SG_PIXELFORMAT_RG16: return DXGI_FORMAT_R16G16_UNORM; |
| 9989 | case SG_PIXELFORMAT_RG16SN: return DXGI_FORMAT_R16G16_SNORM; |
| 9990 | case SG_PIXELFORMAT_RG16UI: return DXGI_FORMAT_R16G16_UINT; |
| 9991 | case SG_PIXELFORMAT_RG16SI: return DXGI_FORMAT_R16G16_SINT; |
| 9992 | case SG_PIXELFORMAT_RG16F: return DXGI_FORMAT_R16G16_FLOAT; |
| 9993 | case SG_PIXELFORMAT_RGBA8: return DXGI_FORMAT_R8G8B8A8_UNORM; |
| 9994 | case SG_PIXELFORMAT_SRGB8A8: return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; |
| 9995 | case SG_PIXELFORMAT_RGBA8SN: return DXGI_FORMAT_R8G8B8A8_SNORM; |
| 9996 | case SG_PIXELFORMAT_RGBA8UI: return DXGI_FORMAT_R8G8B8A8_UINT; |
| 9997 | case SG_PIXELFORMAT_RGBA8SI: return DXGI_FORMAT_R8G8B8A8_SINT; |
| 9998 | case SG_PIXELFORMAT_BGRA8: return DXGI_FORMAT_B8G8R8A8_UNORM; |
| 9999 | case SG_PIXELFORMAT_RGB10A2: return DXGI_FORMAT_R10G10B10A2_UNORM; |
| 10000 | case SG_PIXELFORMAT_RG11B10F: return DXGI_FORMAT_R11G11B10_FLOAT; |
| 10001 | case SG_PIXELFORMAT_RGB9E5: return DXGI_FORMAT_R9G9B9E5_SHAREDEXP; |
| 10002 | case SG_PIXELFORMAT_RG32UI: return DXGI_FORMAT_R32G32_UINT; |
| 10003 | case SG_PIXELFORMAT_RG32SI: return DXGI_FORMAT_R32G32_SINT; |
| 10004 | case SG_PIXELFORMAT_RG32F: return DXGI_FORMAT_R32G32_FLOAT; |
| 10005 | case SG_PIXELFORMAT_RGBA16: return DXGI_FORMAT_R16G16B16A16_UNORM; |
| 10006 | case SG_PIXELFORMAT_RGBA16SN: return DXGI_FORMAT_R16G16B16A16_SNORM; |
| 10007 | case SG_PIXELFORMAT_RGBA16UI: return DXGI_FORMAT_R16G16B16A16_UINT; |
| 10008 | case SG_PIXELFORMAT_RGBA16SI: return DXGI_FORMAT_R16G16B16A16_SINT; |
| 10009 | case SG_PIXELFORMAT_RGBA16F: return DXGI_FORMAT_R16G16B16A16_FLOAT; |
| 10010 | case SG_PIXELFORMAT_RGBA32UI: return DXGI_FORMAT_R32G32B32A32_UINT; |
| 10011 | case SG_PIXELFORMAT_RGBA32SI: return DXGI_FORMAT_R32G32B32A32_SINT; |
| 10012 | case SG_PIXELFORMAT_RGBA32F: return DXGI_FORMAT_R32G32B32A32_FLOAT; |
| 10013 | case SG_PIXELFORMAT_DEPTH: return DXGI_FORMAT_R32_TYPELESS; |
| 10014 | case SG_PIXELFORMAT_DEPTH_STENCIL: return DXGI_FORMAT_R24G8_TYPELESS; |
| 10015 | case SG_PIXELFORMAT_BC1_RGBA: return DXGI_FORMAT_BC1_UNORM; |
| 10016 | case SG_PIXELFORMAT_BC2_RGBA: return DXGI_FORMAT_BC2_UNORM; |
| 10017 | case SG_PIXELFORMAT_BC3_RGBA: return DXGI_FORMAT_BC3_UNORM; |
| 10018 | case SG_PIXELFORMAT_BC3_SRGBA: return DXGI_FORMAT_BC3_UNORM_SRGB; |
| 10019 | case SG_PIXELFORMAT_BC4_R: return DXGI_FORMAT_BC4_UNORM; |
| 10020 | case SG_PIXELFORMAT_BC4_RSN: return DXGI_FORMAT_BC4_SNORM; |
| 10021 | case SG_PIXELFORMAT_BC5_RG: return DXGI_FORMAT_BC5_UNORM; |
| 10022 | case SG_PIXELFORMAT_BC5_RGSN: return DXGI_FORMAT_BC5_SNORM; |
| 10023 | case SG_PIXELFORMAT_BC6H_RGBF: return DXGI_FORMAT_BC6H_SF16; |
| 10024 | case SG_PIXELFORMAT_BC6H_RGBUF: return DXGI_FORMAT_BC6H_UF16; |
| 10025 | case SG_PIXELFORMAT_BC7_RGBA: return DXGI_FORMAT_BC7_UNORM; |
| 10026 | case SG_PIXELFORMAT_BC7_SRGBA: return DXGI_FORMAT_BC7_UNORM_SRGB; |
| 10027 | default: return DXGI_FORMAT_UNKNOWN; |
| 10028 | }; |
| 10029 | } |
| 10030 | |
| 10031 | _SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_srv_pixel_format(sg_pixel_format fmt) { |
| 10032 | if (fmt == SG_PIXELFORMAT_DEPTH) { |
| 10033 | return DXGI_FORMAT_R32_FLOAT; |
| 10034 | } else if (fmt == SG_PIXELFORMAT_DEPTH_STENCIL) { |
| 10035 | return DXGI_FORMAT_R24_UNORM_X8_TYPELESS; |
| 10036 | } else { |
| 10037 | return _sg_d3d11_texture_pixel_format(fmt); |
| 10038 | } |
| 10039 | } |
| 10040 | |
| 10041 | _SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_dsv_pixel_format(sg_pixel_format fmt) { |
| 10042 | if (fmt == SG_PIXELFORMAT_DEPTH) { |
| 10043 | return DXGI_FORMAT_D32_FLOAT; |
| 10044 | } else if (fmt == SG_PIXELFORMAT_DEPTH_STENCIL) { |
| 10045 | return DXGI_FORMAT_D24_UNORM_S8_UINT; |
| 10046 | } else { |
| 10047 | return _sg_d3d11_texture_pixel_format(fmt); |
| 10048 | } |
| 10049 | } |
| 10050 | |
| 10051 | _SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_rtv_pixel_format(sg_pixel_format fmt) { |
| 10052 | if (fmt == SG_PIXELFORMAT_DEPTH) { |
| 10053 | return DXGI_FORMAT_R32_FLOAT; |
| 10054 | } else if (fmt == SG_PIXELFORMAT_DEPTH_STENCIL) { |
| 10055 | return DXGI_FORMAT_R24_UNORM_X8_TYPELESS; |
| 10056 | } else { |
| 10057 | return _sg_d3d11_texture_pixel_format(fmt); |
| 10058 | } |
| 10059 | } |
| 10060 | |
| 10061 | _SOKOL_PRIVATE D3D11_PRIMITIVE_TOPOLOGY _sg_d3d11_primitive_topology(sg_primitive_type prim_type) { |
| 10062 | switch (prim_type) { |
| 10063 | case SG_PRIMITIVETYPE_POINTS: return D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; |
| 10064 | case SG_PRIMITIVETYPE_LINES: return D3D11_PRIMITIVE_TOPOLOGY_LINELIST; |
| 10065 | case SG_PRIMITIVETYPE_LINE_STRIP: return D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP; |
| 10066 | case SG_PRIMITIVETYPE_TRIANGLES: return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST; |
| 10067 | case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; |
| 10068 | default: SOKOL_UNREACHABLE; return (D3D11_PRIMITIVE_TOPOLOGY) 0; |
| 10069 | } |
| 10070 | } |
| 10071 | |
| 10072 | _SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_index_format(sg_index_type index_type) { |
| 10073 | switch (index_type) { |
| 10074 | case SG_INDEXTYPE_NONE: return DXGI_FORMAT_UNKNOWN; |
| 10075 | case SG_INDEXTYPE_UINT16: return DXGI_FORMAT_R16_UINT; |
| 10076 | case SG_INDEXTYPE_UINT32: return DXGI_FORMAT_R32_UINT; |
| 10077 | default: SOKOL_UNREACHABLE; return (DXGI_FORMAT) 0; |
| 10078 | } |
| 10079 | } |
| 10080 | |
| 10081 | _SOKOL_PRIVATE D3D11_FILTER _sg_d3d11_filter(sg_filter min_f, sg_filter mag_f, sg_filter mipmap_f, bool comparison, uint32_t max_anisotropy) { |
| 10082 | uint32_t d3d11_filter = 0; |
| 10083 | if (max_anisotropy > 1) { |
| 10084 | // D3D11_FILTER_ANISOTROPIC = 0x55, |
| 10085 | d3d11_filter |= 0x55; |
| 10086 | } else { |
| 10087 | // D3D11_FILTER_MIN_MAG_MIP_POINT = 0, |
| 10088 | // D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR = 0x1, |
| 10089 | // D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT = 0x4, |
| 10090 | // D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR = 0x5, |
| 10091 | // D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT = 0x10, |
| 10092 | // D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR = 0x11, |
| 10093 | // D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT = 0x14, |
| 10094 | // D3D11_FILTER_MIN_MAG_MIP_LINEAR = 0x15, |
| 10095 | if (mipmap_f == SG_FILTER_LINEAR) { |
| 10096 | d3d11_filter |= 0x01; |
| 10097 | } |
| 10098 | if (mag_f == SG_FILTER_LINEAR) { |
| 10099 | d3d11_filter |= 0x04; |
| 10100 | } |
| 10101 | if (min_f == SG_FILTER_LINEAR) { |
| 10102 | d3d11_filter |= 0x10; |
| 10103 | } |
| 10104 | } |
| 10105 | // D3D11_FILTER_COMPARISON_MIN_MAG_MIP_POINT = 0x80, |
| 10106 | // D3D11_FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR = 0x81, |
| 10107 | // D3D11_FILTER_COMPARISON_MIN_POINT_MAG_LINEAR_MIP_POINT = 0x84, |
| 10108 | // D3D11_FILTER_COMPARISON_MIN_POINT_MAG_MIP_LINEAR = 0x85, |
| 10109 | // D3D11_FILTER_COMPARISON_MIN_LINEAR_MAG_MIP_POINT = 0x90, |
| 10110 | // D3D11_FILTER_COMPARISON_MIN_LINEAR_MAG_POINT_MIP_LINEAR = 0x91, |
| 10111 | // D3D11_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT = 0x94, |
| 10112 | // D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR = 0x95, |
| 10113 | // D3D11_FILTER_COMPARISON_ANISOTROPIC = 0xd5, |
| 10114 | if (comparison) { |
| 10115 | d3d11_filter |= 0x80; |
| 10116 | } |
| 10117 | return (D3D11_FILTER)d3d11_filter; |
| 10118 | } |
| 10119 | |
| 10120 | _SOKOL_PRIVATE D3D11_TEXTURE_ADDRESS_MODE _sg_d3d11_address_mode(sg_wrap m) { |
| 10121 | switch (m) { |
| 10122 | case SG_WRAP_REPEAT: return D3D11_TEXTURE_ADDRESS_WRAP; |
| 10123 | case SG_WRAP_CLAMP_TO_EDGE: return D3D11_TEXTURE_ADDRESS_CLAMP; |
| 10124 | case SG_WRAP_CLAMP_TO_BORDER: return D3D11_TEXTURE_ADDRESS_BORDER; |
| 10125 | case SG_WRAP_MIRRORED_REPEAT: return D3D11_TEXTURE_ADDRESS_MIRROR; |
| 10126 | default: SOKOL_UNREACHABLE; return (D3D11_TEXTURE_ADDRESS_MODE) 0; |
| 10127 | } |
| 10128 | } |
| 10129 | |
| 10130 | _SOKOL_PRIVATE DXGI_FORMAT _sg_d3d11_vertex_format(sg_vertex_format fmt) { |
| 10131 | switch (fmt) { |
| 10132 | case SG_VERTEXFORMAT_FLOAT: return DXGI_FORMAT_R32_FLOAT; |
| 10133 | case SG_VERTEXFORMAT_FLOAT2: return DXGI_FORMAT_R32G32_FLOAT; |
| 10134 | case SG_VERTEXFORMAT_FLOAT3: return DXGI_FORMAT_R32G32B32_FLOAT; |
| 10135 | case SG_VERTEXFORMAT_FLOAT4: return DXGI_FORMAT_R32G32B32A32_FLOAT; |
| 10136 | case SG_VERTEXFORMAT_BYTE4: return DXGI_FORMAT_R8G8B8A8_SINT; |
| 10137 | case SG_VERTEXFORMAT_BYTE4N: return DXGI_FORMAT_R8G8B8A8_SNORM; |
| 10138 | case SG_VERTEXFORMAT_UBYTE4: return DXGI_FORMAT_R8G8B8A8_UINT; |
| 10139 | case SG_VERTEXFORMAT_UBYTE4N: return DXGI_FORMAT_R8G8B8A8_UNORM; |
| 10140 | case SG_VERTEXFORMAT_SHORT2: return DXGI_FORMAT_R16G16_SINT; |
| 10141 | case SG_VERTEXFORMAT_SHORT2N: return DXGI_FORMAT_R16G16_SNORM; |
| 10142 | case SG_VERTEXFORMAT_USHORT2N: return DXGI_FORMAT_R16G16_UNORM; |
| 10143 | case SG_VERTEXFORMAT_SHORT4: return DXGI_FORMAT_R16G16B16A16_SINT; |
| 10144 | case SG_VERTEXFORMAT_SHORT4N: return DXGI_FORMAT_R16G16B16A16_SNORM; |
| 10145 | case SG_VERTEXFORMAT_USHORT4N: return DXGI_FORMAT_R16G16B16A16_UNORM; |
| 10146 | case SG_VERTEXFORMAT_UINT10_N2: return DXGI_FORMAT_R10G10B10A2_UNORM; |
| 10147 | case SG_VERTEXFORMAT_HALF2: return DXGI_FORMAT_R16G16_FLOAT; |
| 10148 | case SG_VERTEXFORMAT_HALF4: return DXGI_FORMAT_R16G16B16A16_FLOAT; |
| 10149 | default: SOKOL_UNREACHABLE; return (DXGI_FORMAT) 0; |
| 10150 | } |
| 10151 | } |
| 10152 | |
| 10153 | _SOKOL_PRIVATE D3D11_INPUT_CLASSIFICATION _sg_d3d11_input_classification(sg_vertex_step step) { |
| 10154 | switch (step) { |
| 10155 | case SG_VERTEXSTEP_PER_VERTEX: return D3D11_INPUT_PER_VERTEX_DATA; |
| 10156 | case SG_VERTEXSTEP_PER_INSTANCE: return D3D11_INPUT_PER_INSTANCE_DATA; |
| 10157 | default: SOKOL_UNREACHABLE; return (D3D11_INPUT_CLASSIFICATION) 0; |
| 10158 | } |
| 10159 | } |
| 10160 | |
| 10161 | _SOKOL_PRIVATE D3D11_CULL_MODE _sg_d3d11_cull_mode(sg_cull_mode m) { |
| 10162 | switch (m) { |
| 10163 | case SG_CULLMODE_NONE: return D3D11_CULL_NONE; |
| 10164 | case SG_CULLMODE_FRONT: return D3D11_CULL_FRONT; |
| 10165 | case SG_CULLMODE_BACK: return D3D11_CULL_BACK; |
| 10166 | default: SOKOL_UNREACHABLE; return (D3D11_CULL_MODE) 0; |
| 10167 | } |
| 10168 | } |
| 10169 | |
| 10170 | _SOKOL_PRIVATE D3D11_COMPARISON_FUNC _sg_d3d11_compare_func(sg_compare_func f) { |
| 10171 | switch (f) { |
| 10172 | case SG_COMPAREFUNC_NEVER: return D3D11_COMPARISON_NEVER; |
| 10173 | case SG_COMPAREFUNC_LESS: return D3D11_COMPARISON_LESS; |
| 10174 | case SG_COMPAREFUNC_EQUAL: return D3D11_COMPARISON_EQUAL; |
| 10175 | case SG_COMPAREFUNC_LESS_EQUAL: return D3D11_COMPARISON_LESS_EQUAL; |
| 10176 | case SG_COMPAREFUNC_GREATER: return D3D11_COMPARISON_GREATER; |
| 10177 | case SG_COMPAREFUNC_NOT_EQUAL: return D3D11_COMPARISON_NOT_EQUAL; |
| 10178 | case SG_COMPAREFUNC_GREATER_EQUAL: return D3D11_COMPARISON_GREATER_EQUAL; |
| 10179 | case SG_COMPAREFUNC_ALWAYS: return D3D11_COMPARISON_ALWAYS; |
| 10180 | default: SOKOL_UNREACHABLE; return (D3D11_COMPARISON_FUNC) 0; |
| 10181 | } |
| 10182 | } |
| 10183 | |
| 10184 | _SOKOL_PRIVATE D3D11_STENCIL_OP _sg_d3d11_stencil_op(sg_stencil_op op) { |
| 10185 | switch (op) { |
| 10186 | case SG_STENCILOP_KEEP: return D3D11_STENCIL_OP_KEEP; |
| 10187 | case SG_STENCILOP_ZERO: return D3D11_STENCIL_OP_ZERO; |
| 10188 | case SG_STENCILOP_REPLACE: return D3D11_STENCIL_OP_REPLACE; |
| 10189 | case SG_STENCILOP_INCR_CLAMP: return D3D11_STENCIL_OP_INCR_SAT; |
| 10190 | case SG_STENCILOP_DECR_CLAMP: return D3D11_STENCIL_OP_DECR_SAT; |
| 10191 | case SG_STENCILOP_INVERT: return D3D11_STENCIL_OP_INVERT; |
| 10192 | case SG_STENCILOP_INCR_WRAP: return D3D11_STENCIL_OP_INCR; |
| 10193 | case SG_STENCILOP_DECR_WRAP: return D3D11_STENCIL_OP_DECR; |
| 10194 | default: SOKOL_UNREACHABLE; return (D3D11_STENCIL_OP) 0; |
| 10195 | } |
| 10196 | } |
| 10197 | |
| 10198 | _SOKOL_PRIVATE D3D11_BLEND _sg_d3d11_blend_factor(sg_blend_factor f) { |
| 10199 | switch (f) { |
| 10200 | case SG_BLENDFACTOR_ZERO: return D3D11_BLEND_ZERO; |
| 10201 | case SG_BLENDFACTOR_ONE: return D3D11_BLEND_ONE; |
| 10202 | case SG_BLENDFACTOR_SRC_COLOR: return D3D11_BLEND_SRC_COLOR; |
| 10203 | case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return D3D11_BLEND_INV_SRC_COLOR; |
| 10204 | case SG_BLENDFACTOR_SRC_ALPHA: return D3D11_BLEND_SRC_ALPHA; |
| 10205 | case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return D3D11_BLEND_INV_SRC_ALPHA; |
| 10206 | case SG_BLENDFACTOR_DST_COLOR: return D3D11_BLEND_DEST_COLOR; |
| 10207 | case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return D3D11_BLEND_INV_DEST_COLOR; |
| 10208 | case SG_BLENDFACTOR_DST_ALPHA: return D3D11_BLEND_DEST_ALPHA; |
| 10209 | case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return D3D11_BLEND_INV_DEST_ALPHA; |
| 10210 | case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return D3D11_BLEND_SRC_ALPHA_SAT; |
| 10211 | case SG_BLENDFACTOR_BLEND_COLOR: return D3D11_BLEND_BLEND_FACTOR; |
| 10212 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return D3D11_BLEND_INV_BLEND_FACTOR; |
| 10213 | case SG_BLENDFACTOR_BLEND_ALPHA: return D3D11_BLEND_BLEND_FACTOR; |
| 10214 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return D3D11_BLEND_INV_BLEND_FACTOR; |
| 10215 | default: SOKOL_UNREACHABLE; return (D3D11_BLEND) 0; |
| 10216 | } |
| 10217 | } |
| 10218 | |
| 10219 | _SOKOL_PRIVATE D3D11_BLEND_OP _sg_d3d11_blend_op(sg_blend_op op) { |
| 10220 | switch (op) { |
| 10221 | case SG_BLENDOP_ADD: return D3D11_BLEND_OP_ADD; |
| 10222 | case SG_BLENDOP_SUBTRACT: return D3D11_BLEND_OP_SUBTRACT; |
| 10223 | case SG_BLENDOP_REVERSE_SUBTRACT: return D3D11_BLEND_OP_REV_SUBTRACT; |
| 10224 | default: SOKOL_UNREACHABLE; return (D3D11_BLEND_OP) 0; |
| 10225 | } |
| 10226 | } |
| 10227 | |
| 10228 | _SOKOL_PRIVATE UINT8 _sg_d3d11_color_write_mask(sg_color_mask m) { |
| 10229 | UINT8 res = 0; |
| 10230 | if (m & SG_COLORMASK_R) { |
| 10231 | res |= D3D11_COLOR_WRITE_ENABLE_RED; |
| 10232 | } |
| 10233 | if (m & SG_COLORMASK_G) { |
| 10234 | res |= D3D11_COLOR_WRITE_ENABLE_GREEN; |
| 10235 | } |
| 10236 | if (m & SG_COLORMASK_B) { |
| 10237 | res |= D3D11_COLOR_WRITE_ENABLE_BLUE; |
| 10238 | } |
| 10239 | if (m & SG_COLORMASK_A) { |
| 10240 | res |= D3D11_COLOR_WRITE_ENABLE_ALPHA; |
| 10241 | } |
| 10242 | return res; |
| 10243 | } |
| 10244 | |
| 10245 | _SOKOL_PRIVATE UINT _sg_d3d11_dxgi_fmt_caps(DXGI_FORMAT dxgi_fmt) { |
| 10246 | UINT dxgi_fmt_caps = 0; |
| 10247 | if (dxgi_fmt != DXGI_FORMAT_UNKNOWN) { |
| 10248 | HRESULT hr = _sg_d3d11_CheckFormatSupport(_sg.d3d11.dev, dxgi_fmt, &dxgi_fmt_caps); |
| 10249 | SOKOL_ASSERT(SUCCEEDED(hr) || (E_FAIL == hr)); |
| 10250 | if (!SUCCEEDED(hr)) { |
| 10251 | dxgi_fmt_caps = 0; |
| 10252 | } |
| 10253 | } |
| 10254 | return dxgi_fmt_caps; |
| 10255 | } |
| 10256 | |
| 10257 | // see: https://docs.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-resources-limits#resource-limits-for-feature-level-11-hardware |
| 10258 | _SOKOL_PRIVATE void _sg_d3d11_init_caps(void) { |
| 10259 | _sg.backend = SG_BACKEND_D3D11; |
| 10260 | |
| 10261 | _sg.features.origin_top_left = true; |
| 10262 | _sg.features.image_clamp_to_border = true; |
| 10263 | _sg.features.mrt_independent_blend_state = true; |
| 10264 | _sg.features.mrt_independent_write_mask = true; |
| 10265 | _sg.features.storage_buffer = true; |
| 10266 | |
| 10267 | _sg.limits.max_image_size_2d = 16 * 1024; |
| 10268 | _sg.limits.max_image_size_cube = 16 * 1024; |
| 10269 | _sg.limits.max_image_size_3d = 2 * 1024; |
| 10270 | _sg.limits.max_image_size_array = 16 * 1024; |
| 10271 | _sg.limits.max_image_array_layers = 2 * 1024; |
| 10272 | _sg.limits.max_vertex_attrs = SG_MAX_VERTEX_ATTRIBUTES; |
| 10273 | |
| 10274 | // see: https://docs.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_format_support |
| 10275 | for (int fmt = (SG_PIXELFORMAT_NONE+1); fmt < _SG_PIXELFORMAT_NUM; fmt++) { |
| 10276 | const UINT srv_dxgi_fmt_caps = _sg_d3d11_dxgi_fmt_caps(_sg_d3d11_srv_pixel_format((sg_pixel_format)fmt)); |
| 10277 | const UINT rtv_dxgi_fmt_caps = _sg_d3d11_dxgi_fmt_caps(_sg_d3d11_rtv_pixel_format((sg_pixel_format)fmt)); |
| 10278 | const UINT dsv_dxgi_fmt_caps = _sg_d3d11_dxgi_fmt_caps(_sg_d3d11_dsv_pixel_format((sg_pixel_format)fmt)); |
| 10279 | _sg_pixelformat_info_t* info = &_sg.formats[fmt]; |
| 10280 | const bool render = 0 != (rtv_dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_RENDER_TARGET); |
| 10281 | const bool depth = 0 != (dsv_dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL); |
| 10282 | info->sample = 0 != (srv_dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_TEXTURE2D); |
| 10283 | info->filter = 0 != (srv_dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_SHADER_SAMPLE); |
| 10284 | info->render = render || depth; |
| 10285 | info->blend = 0 != (rtv_dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_BLENDABLE); |
| 10286 | info->msaa = 0 != (rtv_dxgi_fmt_caps & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET); |
| 10287 | info->depth = depth; |
| 10288 | } |
| 10289 | } |
| 10290 | |
| 10291 | _SOKOL_PRIVATE void _sg_d3d11_setup_backend(const sg_desc* desc) { |
| 10292 | // assume _sg.d3d11 already is zero-initialized |
| 10293 | SOKOL_ASSERT(desc); |
| 10294 | SOKOL_ASSERT(desc->environment.d3d11.device); |
| 10295 | SOKOL_ASSERT(desc->environment.d3d11.device_context); |
| 10296 | _sg.d3d11.valid = true; |
| 10297 | _sg.d3d11.dev = (ID3D11Device*) desc->environment.d3d11.device; |
| 10298 | _sg.d3d11.ctx = (ID3D11DeviceContext*) desc->environment.d3d11.device_context; |
| 10299 | _sg_d3d11_init_caps(); |
| 10300 | } |
| 10301 | |
| 10302 | _SOKOL_PRIVATE void _sg_d3d11_discard_backend(void) { |
| 10303 | SOKOL_ASSERT(_sg.d3d11.valid); |
| 10304 | _sg.d3d11.valid = false; |
| 10305 | } |
| 10306 | |
| 10307 | _SOKOL_PRIVATE void _sg_d3d11_clear_state(void) { |
| 10308 | // clear all the device context state, so that resource refs don't keep stuck in the d3d device context |
| 10309 | _sg_d3d11_ClearState(_sg.d3d11.ctx); |
| 10310 | } |
| 10311 | |
| 10312 | _SOKOL_PRIVATE void _sg_d3d11_reset_state_cache(void) { |
| 10313 | // just clear the d3d11 device context state |
| 10314 | _sg_d3d11_clear_state(); |
| 10315 | } |
| 10316 | |
| 10317 | _SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) { |
| 10318 | SOKOL_ASSERT(buf && desc); |
| 10319 | SOKOL_ASSERT(!buf->d3d11.buf); |
| 10320 | const bool injected = (0 != desc->d3d11_buffer); |
| 10321 | if (injected) { |
| 10322 | buf->d3d11.buf = (ID3D11Buffer*) desc->d3d11_buffer; |
| 10323 | _sg_d3d11_AddRef(buf->d3d11.buf); |
| 10324 | // FIXME: for storage buffers also need to inject resource view |
| 10325 | } else { |
| 10326 | D3D11_BUFFER_DESC d3d11_buf_desc; |
| 10327 | _sg_clear(&d3d11_buf_desc, sizeof(d3d11_buf_desc)); |
| 10328 | d3d11_buf_desc.ByteWidth = (UINT)buf->cmn.size; |
| 10329 | d3d11_buf_desc.Usage = _sg_d3d11_usage(buf->cmn.usage); |
| 10330 | d3d11_buf_desc.BindFlags = _sg_d3d11_buffer_bind_flags(buf->cmn.type); |
| 10331 | d3d11_buf_desc.CPUAccessFlags = _sg_d3d11_cpu_access_flags(buf->cmn.usage); |
| 10332 | d3d11_buf_desc.MiscFlags = _sg_d3d11_buffer_misc_flags(buf->cmn.type); |
| 10333 | D3D11_SUBRESOURCE_DATA* init_data_ptr = 0; |
| 10334 | D3D11_SUBRESOURCE_DATA init_data; |
| 10335 | _sg_clear(&init_data, sizeof(init_data)); |
| 10336 | if (buf->cmn.usage == SG_USAGE_IMMUTABLE) { |
| 10337 | SOKOL_ASSERT(desc->data.ptr); |
| 10338 | init_data.pSysMem = desc->data.ptr; |
| 10339 | init_data_ptr = &init_data; |
| 10340 | } |
| 10341 | HRESULT hr = _sg_d3d11_CreateBuffer(_sg.d3d11.dev, &d3d11_buf_desc, init_data_ptr, &buf->d3d11.buf); |
| 10342 | if (!(SUCCEEDED(hr) && buf->d3d11.buf)) { |
| 10343 | _SG_ERROR(D3D11_CREATE_BUFFER_FAILED); |
| 10344 | return SG_RESOURCESTATE_FAILED; |
| 10345 | } |
| 10346 | |
| 10347 | // for storage buffers need to create a view object |
| 10348 | if (buf->cmn.type == SG_BUFFERTYPE_STORAGEBUFFER) { |
| 10349 | // FIXME: currently only shader-resource-view, in future also UAV |
| 10350 | // storage buffer size must be multiple of 4 |
| 10351 | SOKOL_ASSERT(_sg_multiple_u64(buf->cmn.size, 4)); |
| 10352 | D3D11_SHADER_RESOURCE_VIEW_DESC d3d11_srv_desc; |
| 10353 | _sg_clear(&d3d11_srv_desc, sizeof(d3d11_srv_desc)); |
| 10354 | d3d11_srv_desc.Format = DXGI_FORMAT_R32_TYPELESS; |
| 10355 | d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX; |
| 10356 | d3d11_srv_desc.BufferEx.FirstElement = 0; |
| 10357 | d3d11_srv_desc.BufferEx.NumElements = buf->cmn.size / 4; |
| 10358 | d3d11_srv_desc.BufferEx.Flags = D3D11_BUFFEREX_SRV_FLAG_RAW; |
| 10359 | hr = _sg_d3d11_CreateShaderResourceView(_sg.d3d11.dev, (ID3D11Resource*)buf->d3d11.buf, &d3d11_srv_desc, &buf->d3d11.srv); |
| 10360 | if (!(SUCCEEDED(hr) && buf->d3d11.srv)) { |
| 10361 | _SG_ERROR(D3D11_CREATE_BUFFER_SRV_FAILED); |
| 10362 | return SG_RESOURCESTATE_FAILED; |
| 10363 | } |
| 10364 | } |
| 10365 | _sg_d3d11_setlabel(buf->d3d11.buf, desc->label); |
| 10366 | } |
| 10367 | return SG_RESOURCESTATE_VALID; |
| 10368 | } |
| 10369 | |
| 10370 | _SOKOL_PRIVATE void _sg_d3d11_discard_buffer(_sg_buffer_t* buf) { |
| 10371 | SOKOL_ASSERT(buf); |
| 10372 | if (buf->d3d11.buf) { |
| 10373 | _sg_d3d11_Release(buf->d3d11.buf); |
| 10374 | } |
| 10375 | if (buf->d3d11.srv) { |
| 10376 | _sg_d3d11_Release(buf->d3d11.srv); |
| 10377 | } |
| 10378 | } |
| 10379 | |
| 10380 | _SOKOL_PRIVATE void _sg_d3d11_fill_subres_data(const _sg_image_t* img, const sg_image_data* data) { |
| 10381 | const int num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6:1; |
| 10382 | const int num_slices = (img->cmn.type == SG_IMAGETYPE_ARRAY) ? img->cmn.num_slices:1; |
| 10383 | int subres_index = 0; |
| 10384 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 10385 | for (int slice_index = 0; slice_index < num_slices; slice_index++) { |
| 10386 | for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++, subres_index++) { |
| 10387 | SOKOL_ASSERT(subres_index < (SG_MAX_MIPMAPS * SG_MAX_TEXTUREARRAY_LAYERS)); |
| 10388 | D3D11_SUBRESOURCE_DATA* subres_data = &_sg.d3d11.subres_data[subres_index]; |
| 10389 | const int mip_width = _sg_miplevel_dim(img->cmn.width, mip_index); |
| 10390 | const int mip_height = _sg_miplevel_dim(img->cmn.height, mip_index); |
| 10391 | const sg_range* subimg_data = &(data->subimage[face_index][mip_index]); |
| 10392 | const size_t slice_size = subimg_data->size / (size_t)num_slices; |
| 10393 | const size_t slice_offset = slice_size * (size_t)slice_index; |
| 10394 | const uint8_t* ptr = (const uint8_t*) subimg_data->ptr; |
| 10395 | subres_data->pSysMem = ptr + slice_offset; |
| 10396 | subres_data->SysMemPitch = (UINT)_sg_row_pitch(img->cmn.pixel_format, mip_width, 1); |
| 10397 | if (img->cmn.type == SG_IMAGETYPE_3D) { |
| 10398 | // FIXME? const int mip_depth = _sg_miplevel_dim(img->depth, mip_index); |
| 10399 | subres_data->SysMemSlicePitch = (UINT)_sg_surface_pitch(img->cmn.pixel_format, mip_width, mip_height, 1); |
| 10400 | } else { |
| 10401 | subres_data->SysMemSlicePitch = 0; |
| 10402 | } |
| 10403 | } |
| 10404 | } |
| 10405 | } |
| 10406 | } |
| 10407 | |
| 10408 | _SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_image(_sg_image_t* img, const sg_image_desc* desc) { |
| 10409 | SOKOL_ASSERT(img && desc); |
| 10410 | SOKOL_ASSERT((0 == img->d3d11.tex2d) && (0 == img->d3d11.tex3d) && (0 == img->d3d11.res) && (0 == img->d3d11.srv)); |
| 10411 | HRESULT hr; |
| 10412 | |
| 10413 | const bool injected = (0 != desc->d3d11_texture); |
| 10414 | const bool msaa = (img->cmn.sample_count > 1); |
| 10415 | img->d3d11.format = _sg_d3d11_texture_pixel_format(img->cmn.pixel_format); |
| 10416 | if (img->d3d11.format == DXGI_FORMAT_UNKNOWN) { |
| 10417 | _SG_ERROR(D3D11_CREATE_2D_TEXTURE_UNSUPPORTED_PIXEL_FORMAT); |
| 10418 | return SG_RESOURCESTATE_FAILED; |
| 10419 | } |
| 10420 | |
| 10421 | // prepare initial content pointers |
| 10422 | D3D11_SUBRESOURCE_DATA* init_data = 0; |
| 10423 | if (!injected && (img->cmn.usage == SG_USAGE_IMMUTABLE) && !img->cmn.render_target) { |
| 10424 | _sg_d3d11_fill_subres_data(img, &desc->data); |
| 10425 | init_data = _sg.d3d11.subres_data; |
| 10426 | } |
| 10427 | if (img->cmn.type != SG_IMAGETYPE_3D) { |
| 10428 | // 2D-, cube- or array-texture |
| 10429 | // first check for injected texture and/or resource view |
| 10430 | if (injected) { |
| 10431 | img->d3d11.tex2d = (ID3D11Texture2D*) desc->d3d11_texture; |
| 10432 | _sg_d3d11_AddRef(img->d3d11.tex2d); |
| 10433 | img->d3d11.srv = (ID3D11ShaderResourceView*) desc->d3d11_shader_resource_view; |
| 10434 | if (img->d3d11.srv) { |
| 10435 | _sg_d3d11_AddRef(img->d3d11.srv); |
| 10436 | } |
| 10437 | } else { |
| 10438 | // if not injected, create 2D texture |
| 10439 | D3D11_TEXTURE2D_DESC d3d11_tex_desc; |
| 10440 | _sg_clear(&d3d11_tex_desc, sizeof(d3d11_tex_desc)); |
| 10441 | d3d11_tex_desc.Width = (UINT)img->cmn.width; |
| 10442 | d3d11_tex_desc.Height = (UINT)img->cmn.height; |
| 10443 | d3d11_tex_desc.MipLevels = (UINT)img->cmn.num_mipmaps; |
| 10444 | switch (img->cmn.type) { |
| 10445 | case SG_IMAGETYPE_ARRAY: d3d11_tex_desc.ArraySize = (UINT)img->cmn.num_slices; break; |
| 10446 | case SG_IMAGETYPE_CUBE: d3d11_tex_desc.ArraySize = 6; break; |
| 10447 | default: d3d11_tex_desc.ArraySize = 1; break; |
| 10448 | } |
| 10449 | d3d11_tex_desc.Format = img->d3d11.format; |
| 10450 | if (img->cmn.render_target) { |
| 10451 | d3d11_tex_desc.Usage = D3D11_USAGE_DEFAULT; |
| 10452 | if (_sg_is_depth_or_depth_stencil_format(img->cmn.pixel_format)) { |
| 10453 | d3d11_tex_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL; |
| 10454 | } else { |
| 10455 | d3d11_tex_desc.BindFlags = D3D11_BIND_RENDER_TARGET; |
| 10456 | } |
| 10457 | if (!msaa) { |
| 10458 | d3d11_tex_desc.BindFlags |= D3D11_BIND_SHADER_RESOURCE; |
| 10459 | } |
| 10460 | d3d11_tex_desc.CPUAccessFlags = 0; |
| 10461 | } else { |
| 10462 | d3d11_tex_desc.Usage = _sg_d3d11_usage(img->cmn.usage); |
| 10463 | d3d11_tex_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; |
| 10464 | d3d11_tex_desc.CPUAccessFlags = _sg_d3d11_cpu_access_flags(img->cmn.usage); |
| 10465 | } |
| 10466 | d3d11_tex_desc.SampleDesc.Count = (UINT)img->cmn.sample_count; |
| 10467 | d3d11_tex_desc.SampleDesc.Quality = (UINT) (msaa ? D3D11_STANDARD_MULTISAMPLE_PATTERN : 0); |
| 10468 | d3d11_tex_desc.MiscFlags = (img->cmn.type == SG_IMAGETYPE_CUBE) ? D3D11_RESOURCE_MISC_TEXTURECUBE : 0; |
| 10469 | |
| 10470 | hr = _sg_d3d11_CreateTexture2D(_sg.d3d11.dev, &d3d11_tex_desc, init_data, &img->d3d11.tex2d); |
| 10471 | if (!(SUCCEEDED(hr) && img->d3d11.tex2d)) { |
| 10472 | _SG_ERROR(D3D11_CREATE_2D_TEXTURE_FAILED); |
| 10473 | return SG_RESOURCESTATE_FAILED; |
| 10474 | } |
| 10475 | _sg_d3d11_setlabel(img->d3d11.tex2d, desc->label); |
| 10476 | |
| 10477 | // create shader-resource-view for 2D texture |
| 10478 | // FIXME: currently we don't support setting MSAA texture as shader resource |
| 10479 | if (!msaa) { |
| 10480 | D3D11_SHADER_RESOURCE_VIEW_DESC d3d11_srv_desc; |
| 10481 | _sg_clear(&d3d11_srv_desc, sizeof(d3d11_srv_desc)); |
| 10482 | d3d11_srv_desc.Format = _sg_d3d11_srv_pixel_format(img->cmn.pixel_format); |
| 10483 | switch (img->cmn.type) { |
| 10484 | case SG_IMAGETYPE_2D: |
| 10485 | d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; |
| 10486 | d3d11_srv_desc.Texture2D.MipLevels = (UINT)img->cmn.num_mipmaps; |
| 10487 | break; |
| 10488 | case SG_IMAGETYPE_CUBE: |
| 10489 | d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; |
| 10490 | d3d11_srv_desc.TextureCube.MipLevels = (UINT)img->cmn.num_mipmaps; |
| 10491 | break; |
| 10492 | case SG_IMAGETYPE_ARRAY: |
| 10493 | d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2DARRAY; |
| 10494 | d3d11_srv_desc.Texture2DArray.MipLevels = (UINT)img->cmn.num_mipmaps; |
| 10495 | d3d11_srv_desc.Texture2DArray.ArraySize = (UINT)img->cmn.num_slices; |
| 10496 | break; |
| 10497 | default: |
| 10498 | SOKOL_UNREACHABLE; break; |
| 10499 | } |
| 10500 | hr = _sg_d3d11_CreateShaderResourceView(_sg.d3d11.dev, (ID3D11Resource*)img->d3d11.tex2d, &d3d11_srv_desc, &img->d3d11.srv); |
| 10501 | if (!(SUCCEEDED(hr) && img->d3d11.srv)) { |
| 10502 | _SG_ERROR(D3D11_CREATE_2D_SRV_FAILED); |
| 10503 | return SG_RESOURCESTATE_FAILED; |
| 10504 | } |
| 10505 | _sg_d3d11_setlabel(img->d3d11.srv, desc->label); |
| 10506 | } |
| 10507 | } |
| 10508 | SOKOL_ASSERT(img->d3d11.tex2d); |
| 10509 | img->d3d11.res = (ID3D11Resource*)img->d3d11.tex2d; |
| 10510 | _sg_d3d11_AddRef(img->d3d11.res); |
| 10511 | } else { |
| 10512 | // 3D texture - same procedure, first check if injected, than create non-injected |
| 10513 | if (injected) { |
| 10514 | img->d3d11.tex3d = (ID3D11Texture3D*) desc->d3d11_texture; |
| 10515 | _sg_d3d11_AddRef(img->d3d11.tex3d); |
| 10516 | img->d3d11.srv = (ID3D11ShaderResourceView*) desc->d3d11_shader_resource_view; |
| 10517 | if (img->d3d11.srv) { |
| 10518 | _sg_d3d11_AddRef(img->d3d11.srv); |
| 10519 | } |
| 10520 | } else { |
| 10521 | // not injected, create 3d texture |
| 10522 | D3D11_TEXTURE3D_DESC d3d11_tex_desc; |
| 10523 | _sg_clear(&d3d11_tex_desc, sizeof(d3d11_tex_desc)); |
| 10524 | d3d11_tex_desc.Width = (UINT)img->cmn.width; |
| 10525 | d3d11_tex_desc.Height = (UINT)img->cmn.height; |
| 10526 | d3d11_tex_desc.Depth = (UINT)img->cmn.num_slices; |
| 10527 | d3d11_tex_desc.MipLevels = (UINT)img->cmn.num_mipmaps; |
| 10528 | d3d11_tex_desc.Format = img->d3d11.format; |
| 10529 | if (img->cmn.render_target) { |
| 10530 | d3d11_tex_desc.Usage = D3D11_USAGE_DEFAULT; |
| 10531 | d3d11_tex_desc.BindFlags = D3D11_BIND_RENDER_TARGET; |
| 10532 | d3d11_tex_desc.CPUAccessFlags = 0; |
| 10533 | } else { |
| 10534 | d3d11_tex_desc.Usage = _sg_d3d11_usage(img->cmn.usage); |
| 10535 | d3d11_tex_desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; |
| 10536 | d3d11_tex_desc.CPUAccessFlags = _sg_d3d11_cpu_access_flags(img->cmn.usage); |
| 10537 | } |
| 10538 | if (img->d3d11.format == DXGI_FORMAT_UNKNOWN) { |
| 10539 | _SG_ERROR(D3D11_CREATE_3D_TEXTURE_UNSUPPORTED_PIXEL_FORMAT); |
| 10540 | return SG_RESOURCESTATE_FAILED; |
| 10541 | } |
| 10542 | hr = _sg_d3d11_CreateTexture3D(_sg.d3d11.dev, &d3d11_tex_desc, init_data, &img->d3d11.tex3d); |
| 10543 | if (!(SUCCEEDED(hr) && img->d3d11.tex3d)) { |
| 10544 | _SG_ERROR(D3D11_CREATE_3D_TEXTURE_FAILED); |
| 10545 | return SG_RESOURCESTATE_FAILED; |
| 10546 | } |
| 10547 | _sg_d3d11_setlabel(img->d3d11.tex3d, desc->label); |
| 10548 | |
| 10549 | // create shader-resource-view for 3D texture |
| 10550 | if (!msaa) { |
| 10551 | D3D11_SHADER_RESOURCE_VIEW_DESC d3d11_srv_desc; |
| 10552 | _sg_clear(&d3d11_srv_desc, sizeof(d3d11_srv_desc)); |
| 10553 | d3d11_srv_desc.Format = _sg_d3d11_srv_pixel_format(img->cmn.pixel_format); |
| 10554 | d3d11_srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE3D; |
| 10555 | d3d11_srv_desc.Texture3D.MipLevels = (UINT)img->cmn.num_mipmaps; |
| 10556 | hr = _sg_d3d11_CreateShaderResourceView(_sg.d3d11.dev, (ID3D11Resource*)img->d3d11.tex3d, &d3d11_srv_desc, &img->d3d11.srv); |
| 10557 | if (!(SUCCEEDED(hr) && img->d3d11.srv)) { |
| 10558 | _SG_ERROR(D3D11_CREATE_3D_SRV_FAILED); |
| 10559 | return SG_RESOURCESTATE_FAILED; |
| 10560 | } |
| 10561 | _sg_d3d11_setlabel(img->d3d11.srv, desc->label); |
| 10562 | } |
| 10563 | } |
| 10564 | SOKOL_ASSERT(img->d3d11.tex3d); |
| 10565 | img->d3d11.res = (ID3D11Resource*)img->d3d11.tex3d; |
| 10566 | _sg_d3d11_AddRef(img->d3d11.res); |
| 10567 | } |
| 10568 | return SG_RESOURCESTATE_VALID; |
| 10569 | } |
| 10570 | |
| 10571 | _SOKOL_PRIVATE void _sg_d3d11_discard_image(_sg_image_t* img) { |
| 10572 | SOKOL_ASSERT(img); |
| 10573 | if (img->d3d11.tex2d) { |
| 10574 | _sg_d3d11_Release(img->d3d11.tex2d); |
| 10575 | } |
| 10576 | if (img->d3d11.tex3d) { |
| 10577 | _sg_d3d11_Release(img->d3d11.tex3d); |
| 10578 | } |
| 10579 | if (img->d3d11.res) { |
| 10580 | _sg_d3d11_Release(img->d3d11.res); |
| 10581 | } |
| 10582 | if (img->d3d11.srv) { |
| 10583 | _sg_d3d11_Release(img->d3d11.srv); |
| 10584 | } |
| 10585 | } |
| 10586 | |
| 10587 | _SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) { |
| 10588 | SOKOL_ASSERT(smp && desc); |
| 10589 | SOKOL_ASSERT(0 == smp->d3d11.smp); |
| 10590 | const bool injected = (0 != desc->d3d11_sampler); |
| 10591 | if (injected) { |
| 10592 | smp->d3d11.smp = (ID3D11SamplerState*)desc->d3d11_sampler; |
| 10593 | _sg_d3d11_AddRef(smp->d3d11.smp); |
| 10594 | } else { |
| 10595 | D3D11_SAMPLER_DESC d3d11_smp_desc; |
| 10596 | _sg_clear(&d3d11_smp_desc, sizeof(d3d11_smp_desc)); |
| 10597 | d3d11_smp_desc.Filter = _sg_d3d11_filter(desc->min_filter, desc->mag_filter, desc->mipmap_filter, desc->compare != SG_COMPAREFUNC_NEVER, desc->max_anisotropy); |
| 10598 | d3d11_smp_desc.AddressU = _sg_d3d11_address_mode(desc->wrap_u); |
| 10599 | d3d11_smp_desc.AddressV = _sg_d3d11_address_mode(desc->wrap_v); |
| 10600 | d3d11_smp_desc.AddressW = _sg_d3d11_address_mode(desc->wrap_w); |
| 10601 | d3d11_smp_desc.MipLODBias = 0.0f; // FIXME? |
| 10602 | switch (desc->border_color) { |
| 10603 | case SG_BORDERCOLOR_TRANSPARENT_BLACK: |
| 10604 | // all 0.0f |
| 10605 | break; |
| 10606 | case SG_BORDERCOLOR_OPAQUE_WHITE: |
| 10607 | for (int i = 0; i < 4; i++) { |
| 10608 | d3d11_smp_desc.BorderColor[i] = 1.0f; |
| 10609 | } |
| 10610 | break; |
| 10611 | default: |
| 10612 | // opaque black |
| 10613 | d3d11_smp_desc.BorderColor[3] = 1.0f; |
| 10614 | break; |
| 10615 | } |
| 10616 | d3d11_smp_desc.MaxAnisotropy = desc->max_anisotropy; |
| 10617 | d3d11_smp_desc.ComparisonFunc = _sg_d3d11_compare_func(desc->compare); |
| 10618 | d3d11_smp_desc.MinLOD = desc->min_lod; |
| 10619 | d3d11_smp_desc.MaxLOD = desc->max_lod; |
| 10620 | HRESULT hr = _sg_d3d11_CreateSamplerState(_sg.d3d11.dev, &d3d11_smp_desc, &smp->d3d11.smp); |
| 10621 | if (!(SUCCEEDED(hr) && smp->d3d11.smp)) { |
| 10622 | _SG_ERROR(D3D11_CREATE_SAMPLER_STATE_FAILED); |
| 10623 | return SG_RESOURCESTATE_FAILED; |
| 10624 | } |
| 10625 | _sg_d3d11_setlabel(smp->d3d11.smp, desc->label); |
| 10626 | } |
| 10627 | return SG_RESOURCESTATE_VALID; |
| 10628 | } |
| 10629 | |
| 10630 | _SOKOL_PRIVATE void _sg_d3d11_discard_sampler(_sg_sampler_t* smp) { |
| 10631 | SOKOL_ASSERT(smp); |
| 10632 | if (smp->d3d11.smp) { |
| 10633 | _sg_d3d11_Release(smp->d3d11.smp); |
| 10634 | } |
| 10635 | } |
| 10636 | |
| 10637 | _SOKOL_PRIVATE bool _sg_d3d11_load_d3dcompiler_dll(void) { |
| 10638 | if ((0 == _sg.d3d11.d3dcompiler_dll) && !_sg.d3d11.d3dcompiler_dll_load_failed) { |
| 10639 | _sg.d3d11.d3dcompiler_dll = LoadLibraryA("d3dcompiler_47.dll"); |
| 10640 | if (0 == _sg.d3d11.d3dcompiler_dll) { |
| 10641 | // don't attempt to load missing DLL in the future |
| 10642 | _SG_ERROR(D3D11_LOAD_D3DCOMPILER_47_DLL_FAILED); |
| 10643 | _sg.d3d11.d3dcompiler_dll_load_failed = true; |
| 10644 | return false; |
| 10645 | } |
| 10646 | // look up function pointers |
| 10647 | _sg.d3d11.D3DCompile_func = (pD3DCompile)(void*) GetProcAddress(_sg.d3d11.d3dcompiler_dll, "D3DCompile"); |
| 10648 | SOKOL_ASSERT(_sg.d3d11.D3DCompile_func); |
| 10649 | } |
| 10650 | return 0 != _sg.d3d11.d3dcompiler_dll; |
| 10651 | } |
| 10652 | |
| 10653 | _SOKOL_PRIVATE ID3DBlob* _sg_d3d11_compile_shader(const sg_shader_stage_desc* stage_desc) { |
| 10654 | if (!_sg_d3d11_load_d3dcompiler_dll()) { |
| 10655 | return NULL; |
| 10656 | } |
| 10657 | SOKOL_ASSERT(stage_desc->d3d11_target); |
| 10658 | ID3DBlob* output = NULL; |
| 10659 | ID3DBlob* errors_or_warnings = NULL; |
| 10660 | HRESULT hr = _sg.d3d11.D3DCompile_func( |
| 10661 | stage_desc->source, // pSrcData |
| 10662 | strlen(stage_desc->source), // SrcDataSize |
| 10663 | NULL, // pSourceName |
| 10664 | NULL, // pDefines |
| 10665 | NULL, // pInclude |
| 10666 | stage_desc->entry ? stage_desc->entry : "main", // pEntryPoint |
| 10667 | stage_desc->d3d11_target, // pTarget |
| 10668 | D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR | D3DCOMPILE_OPTIMIZATION_LEVEL3, // Flags1 |
| 10669 | 0, // Flags2 |
| 10670 | &output, // ppCode |
| 10671 | &errors_or_warnings); // ppErrorMsgs |
| 10672 | if (FAILED(hr)) { |
| 10673 | _SG_ERROR(D3D11_SHADER_COMPILATION_FAILED); |
| 10674 | } |
| 10675 | if (errors_or_warnings) { |
| 10676 | _SG_WARN(D3D11_SHADER_COMPILATION_OUTPUT); |
| 10677 | _SG_LOGMSG(D3D11_SHADER_COMPILATION_OUTPUT, (LPCSTR)_sg_d3d11_GetBufferPointer(errors_or_warnings)); |
| 10678 | _sg_d3d11_Release(errors_or_warnings); errors_or_warnings = NULL; |
| 10679 | } |
| 10680 | if (FAILED(hr)) { |
| 10681 | // just in case, usually output is NULL here |
| 10682 | if (output) { |
| 10683 | _sg_d3d11_Release(output); |
| 10684 | output = NULL; |
| 10685 | } |
| 10686 | } |
| 10687 | return output; |
| 10688 | } |
| 10689 | |
| 10690 | _SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) { |
| 10691 | SOKOL_ASSERT(shd && desc); |
| 10692 | SOKOL_ASSERT(!shd->d3d11.vs && !shd->d3d11.fs && !shd->d3d11.vs_blob); |
| 10693 | HRESULT hr; |
| 10694 | |
| 10695 | // copy vertex attribute semantic names and indices |
| 10696 | for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) { |
| 10697 | _sg_strcpy(&shd->d3d11.attrs[i].sem_name, desc->attrs[i].sem_name); |
| 10698 | shd->d3d11.attrs[i].sem_index = desc->attrs[i].sem_index; |
| 10699 | } |
| 10700 | |
| 10701 | // shader stage uniform blocks and image slots |
| 10702 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 10703 | _sg_shader_stage_t* cmn_stage = &shd->cmn.stage[stage_index]; |
| 10704 | _sg_d3d11_shader_stage_t* d3d11_stage = &shd->d3d11.stage[stage_index]; |
| 10705 | for (int ub_index = 0; ub_index < cmn_stage->num_uniform_blocks; ub_index++) { |
| 10706 | const _sg_shader_uniform_block_t* ub = &cmn_stage->uniform_blocks[ub_index]; |
| 10707 | |
| 10708 | // create a D3D constant buffer for each uniform block |
| 10709 | SOKOL_ASSERT(0 == d3d11_stage->cbufs[ub_index]); |
| 10710 | D3D11_BUFFER_DESC cb_desc; |
| 10711 | _sg_clear(&cb_desc, sizeof(cb_desc)); |
| 10712 | cb_desc.ByteWidth = (UINT)_sg_roundup((int)ub->size, 16); |
| 10713 | cb_desc.Usage = D3D11_USAGE_DEFAULT; |
| 10714 | cb_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; |
| 10715 | hr = _sg_d3d11_CreateBuffer(_sg.d3d11.dev, &cb_desc, NULL, &d3d11_stage->cbufs[ub_index]); |
| 10716 | if (!(SUCCEEDED(hr) && d3d11_stage->cbufs[ub_index])) { |
| 10717 | _SG_ERROR(D3D11_CREATE_CONSTANT_BUFFER_FAILED); |
| 10718 | return SG_RESOURCESTATE_FAILED; |
| 10719 | } |
| 10720 | _sg_d3d11_setlabel(d3d11_stage->cbufs[ub_index], desc->label); |
| 10721 | } |
| 10722 | } |
| 10723 | |
| 10724 | const void* vs_ptr = 0, *fs_ptr = 0; |
| 10725 | SIZE_T vs_length = 0, fs_length = 0; |
| 10726 | ID3DBlob* vs_blob = 0, *fs_blob = 0; |
| 10727 | if (desc->vs.bytecode.ptr && desc->fs.bytecode.ptr) { |
| 10728 | // create from shader byte code |
| 10729 | vs_ptr = desc->vs.bytecode.ptr; |
| 10730 | fs_ptr = desc->fs.bytecode.ptr; |
| 10731 | vs_length = desc->vs.bytecode.size; |
| 10732 | fs_length = desc->fs.bytecode.size; |
| 10733 | } else { |
| 10734 | // compile from shader source code |
| 10735 | vs_blob = _sg_d3d11_compile_shader(&desc->vs); |
| 10736 | fs_blob = _sg_d3d11_compile_shader(&desc->fs); |
| 10737 | if (vs_blob && fs_blob) { |
| 10738 | vs_ptr = _sg_d3d11_GetBufferPointer(vs_blob); |
| 10739 | vs_length = _sg_d3d11_GetBufferSize(vs_blob); |
| 10740 | fs_ptr = _sg_d3d11_GetBufferPointer(fs_blob); |
| 10741 | fs_length = _sg_d3d11_GetBufferSize(fs_blob); |
| 10742 | } |
| 10743 | } |
| 10744 | sg_resource_state result = SG_RESOURCESTATE_FAILED; |
| 10745 | if (vs_ptr && fs_ptr && (vs_length > 0) && (fs_length > 0)) { |
| 10746 | // create the D3D vertex- and pixel-shader objects |
| 10747 | hr = _sg_d3d11_CreateVertexShader(_sg.d3d11.dev, vs_ptr, vs_length, NULL, &shd->d3d11.vs); |
| 10748 | bool vs_succeeded = SUCCEEDED(hr) && shd->d3d11.vs; |
| 10749 | hr = _sg_d3d11_CreatePixelShader(_sg.d3d11.dev, fs_ptr, fs_length, NULL, &shd->d3d11.fs); |
| 10750 | bool fs_succeeded = SUCCEEDED(hr) && shd->d3d11.fs; |
| 10751 | |
| 10752 | // need to store the vertex shader byte code, this is needed later in sg_create_pipeline |
| 10753 | if (vs_succeeded && fs_succeeded) { |
| 10754 | shd->d3d11.vs_blob_length = vs_length; |
| 10755 | shd->d3d11.vs_blob = _sg_malloc((size_t)vs_length); |
| 10756 | SOKOL_ASSERT(shd->d3d11.vs_blob); |
| 10757 | memcpy(shd->d3d11.vs_blob, vs_ptr, vs_length); |
| 10758 | result = SG_RESOURCESTATE_VALID; |
| 10759 | _sg_d3d11_setlabel(shd->d3d11.vs, desc->label); |
| 10760 | _sg_d3d11_setlabel(shd->d3d11.fs, desc->label); |
| 10761 | } |
| 10762 | } |
| 10763 | if (vs_blob) { |
| 10764 | _sg_d3d11_Release(vs_blob); vs_blob = 0; |
| 10765 | } |
| 10766 | if (fs_blob) { |
| 10767 | _sg_d3d11_Release(fs_blob); fs_blob = 0; |
| 10768 | } |
| 10769 | return result; |
| 10770 | } |
| 10771 | |
| 10772 | _SOKOL_PRIVATE void _sg_d3d11_discard_shader(_sg_shader_t* shd) { |
| 10773 | SOKOL_ASSERT(shd); |
| 10774 | if (shd->d3d11.vs) { |
| 10775 | _sg_d3d11_Release(shd->d3d11.vs); |
| 10776 | } |
| 10777 | if (shd->d3d11.fs) { |
| 10778 | _sg_d3d11_Release(shd->d3d11.fs); |
| 10779 | } |
| 10780 | if (shd->d3d11.vs_blob) { |
| 10781 | _sg_free(shd->d3d11.vs_blob); |
| 10782 | } |
| 10783 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 10784 | _sg_shader_stage_t* cmn_stage = &shd->cmn.stage[stage_index]; |
| 10785 | _sg_d3d11_shader_stage_t* d3d11_stage = &shd->d3d11.stage[stage_index]; |
| 10786 | for (int ub_index = 0; ub_index < cmn_stage->num_uniform_blocks; ub_index++) { |
| 10787 | if (d3d11_stage->cbufs[ub_index]) { |
| 10788 | _sg_d3d11_Release(d3d11_stage->cbufs[ub_index]); |
| 10789 | } |
| 10790 | } |
| 10791 | } |
| 10792 | } |
| 10793 | |
| 10794 | _SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) { |
| 10795 | SOKOL_ASSERT(pip && shd && desc); |
| 10796 | SOKOL_ASSERT(desc->shader.id == shd->slot.id); |
| 10797 | SOKOL_ASSERT(shd->slot.state == SG_RESOURCESTATE_VALID); |
| 10798 | SOKOL_ASSERT(shd->d3d11.vs_blob && shd->d3d11.vs_blob_length > 0); |
| 10799 | SOKOL_ASSERT(!pip->d3d11.il && !pip->d3d11.rs && !pip->d3d11.dss && !pip->d3d11.bs); |
| 10800 | |
| 10801 | pip->shader = shd; |
| 10802 | pip->d3d11.index_format = _sg_d3d11_index_format(pip->cmn.index_type); |
| 10803 | pip->d3d11.topology = _sg_d3d11_primitive_topology(desc->primitive_type); |
| 10804 | pip->d3d11.stencil_ref = desc->stencil.ref; |
| 10805 | |
| 10806 | // create input layout object |
| 10807 | HRESULT hr; |
| 10808 | D3D11_INPUT_ELEMENT_DESC d3d11_comps[SG_MAX_VERTEX_ATTRIBUTES]; |
| 10809 | _sg_clear(d3d11_comps, sizeof(d3d11_comps)); |
| 10810 | int attr_index = 0; |
| 10811 | for (; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 10812 | const sg_vertex_attr_state* a_state = &desc->layout.attrs[attr_index]; |
| 10813 | if (a_state->format == SG_VERTEXFORMAT_INVALID) { |
| 10814 | break; |
| 10815 | } |
| 10816 | SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS); |
| 10817 | const sg_vertex_buffer_layout_state* l_state = &desc->layout.buffers[a_state->buffer_index]; |
| 10818 | const sg_vertex_step step_func = l_state->step_func; |
| 10819 | const int step_rate = l_state->step_rate; |
| 10820 | D3D11_INPUT_ELEMENT_DESC* d3d11_comp = &d3d11_comps[attr_index]; |
| 10821 | d3d11_comp->SemanticName = _sg_strptr(&shd->d3d11.attrs[attr_index].sem_name); |
| 10822 | d3d11_comp->SemanticIndex = (UINT)shd->d3d11.attrs[attr_index].sem_index; |
| 10823 | d3d11_comp->Format = _sg_d3d11_vertex_format(a_state->format); |
| 10824 | d3d11_comp->InputSlot = (UINT)a_state->buffer_index; |
| 10825 | d3d11_comp->AlignedByteOffset = (UINT)a_state->offset; |
| 10826 | d3d11_comp->InputSlotClass = _sg_d3d11_input_classification(step_func); |
| 10827 | if (SG_VERTEXSTEP_PER_INSTANCE == step_func) { |
| 10828 | d3d11_comp->InstanceDataStepRate = (UINT)step_rate; |
| 10829 | pip->cmn.use_instanced_draw = true; |
| 10830 | } |
| 10831 | pip->cmn.vertex_buffer_layout_active[a_state->buffer_index] = true; |
| 10832 | } |
| 10833 | for (int layout_index = 0; layout_index < SG_MAX_VERTEX_BUFFERS; layout_index++) { |
| 10834 | if (pip->cmn.vertex_buffer_layout_active[layout_index]) { |
| 10835 | const sg_vertex_buffer_layout_state* l_state = &desc->layout.buffers[layout_index]; |
| 10836 | SOKOL_ASSERT(l_state->stride > 0); |
| 10837 | pip->d3d11.vb_strides[layout_index] = (UINT)l_state->stride; |
| 10838 | } else { |
| 10839 | pip->d3d11.vb_strides[layout_index] = 0; |
| 10840 | } |
| 10841 | } |
| 10842 | if (attr_index > 0) { |
| 10843 | hr = _sg_d3d11_CreateInputLayout(_sg.d3d11.dev, |
| 10844 | d3d11_comps, // pInputElementDesc |
| 10845 | (UINT)attr_index, // NumElements |
| 10846 | shd->d3d11.vs_blob, // pShaderByteCodeWithInputSignature |
| 10847 | shd->d3d11.vs_blob_length, // BytecodeLength |
| 10848 | &pip->d3d11.il); |
| 10849 | if (!(SUCCEEDED(hr) && pip->d3d11.il)) { |
| 10850 | _SG_ERROR(D3D11_CREATE_INPUT_LAYOUT_FAILED); |
| 10851 | return SG_RESOURCESTATE_FAILED; |
| 10852 | } |
| 10853 | _sg_d3d11_setlabel(pip->d3d11.il, desc->label); |
| 10854 | } |
| 10855 | |
| 10856 | // create rasterizer state |
| 10857 | D3D11_RASTERIZER_DESC rs_desc; |
| 10858 | _sg_clear(&rs_desc, sizeof(rs_desc)); |
| 10859 | rs_desc.FillMode = D3D11_FILL_SOLID; |
| 10860 | rs_desc.CullMode = _sg_d3d11_cull_mode(desc->cull_mode); |
| 10861 | rs_desc.FrontCounterClockwise = desc->face_winding == SG_FACEWINDING_CCW; |
| 10862 | rs_desc.DepthBias = (INT) pip->cmn.depth.bias; |
| 10863 | rs_desc.DepthBiasClamp = pip->cmn.depth.bias_clamp; |
| 10864 | rs_desc.SlopeScaledDepthBias = pip->cmn.depth.bias_slope_scale; |
| 10865 | rs_desc.DepthClipEnable = TRUE; |
| 10866 | rs_desc.ScissorEnable = TRUE; |
| 10867 | rs_desc.MultisampleEnable = desc->sample_count > 1; |
| 10868 | rs_desc.AntialiasedLineEnable = FALSE; |
| 10869 | hr = _sg_d3d11_CreateRasterizerState(_sg.d3d11.dev, &rs_desc, &pip->d3d11.rs); |
| 10870 | if (!(SUCCEEDED(hr) && pip->d3d11.rs)) { |
| 10871 | _SG_ERROR(D3D11_CREATE_RASTERIZER_STATE_FAILED); |
| 10872 | return SG_RESOURCESTATE_FAILED; |
| 10873 | } |
| 10874 | _sg_d3d11_setlabel(pip->d3d11.rs, desc->label); |
| 10875 | |
| 10876 | // create depth-stencil state |
| 10877 | D3D11_DEPTH_STENCIL_DESC dss_desc; |
| 10878 | _sg_clear(&dss_desc, sizeof(dss_desc)); |
| 10879 | dss_desc.DepthEnable = TRUE; |
| 10880 | dss_desc.DepthWriteMask = desc->depth.write_enabled ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO; |
| 10881 | dss_desc.DepthFunc = _sg_d3d11_compare_func(desc->depth.compare); |
| 10882 | dss_desc.StencilEnable = desc->stencil.enabled; |
| 10883 | dss_desc.StencilReadMask = desc->stencil.read_mask; |
| 10884 | dss_desc.StencilWriteMask = desc->stencil.write_mask; |
| 10885 | const sg_stencil_face_state* sf = &desc->stencil.front; |
| 10886 | dss_desc.FrontFace.StencilFailOp = _sg_d3d11_stencil_op(sf->fail_op); |
| 10887 | dss_desc.FrontFace.StencilDepthFailOp = _sg_d3d11_stencil_op(sf->depth_fail_op); |
| 10888 | dss_desc.FrontFace.StencilPassOp = _sg_d3d11_stencil_op(sf->pass_op); |
| 10889 | dss_desc.FrontFace.StencilFunc = _sg_d3d11_compare_func(sf->compare); |
| 10890 | const sg_stencil_face_state* sb = &desc->stencil.back; |
| 10891 | dss_desc.BackFace.StencilFailOp = _sg_d3d11_stencil_op(sb->fail_op); |
| 10892 | dss_desc.BackFace.StencilDepthFailOp = _sg_d3d11_stencil_op(sb->depth_fail_op); |
| 10893 | dss_desc.BackFace.StencilPassOp = _sg_d3d11_stencil_op(sb->pass_op); |
| 10894 | dss_desc.BackFace.StencilFunc = _sg_d3d11_compare_func(sb->compare); |
| 10895 | hr = _sg_d3d11_CreateDepthStencilState(_sg.d3d11.dev, &dss_desc, &pip->d3d11.dss); |
| 10896 | if (!(SUCCEEDED(hr) && pip->d3d11.dss)) { |
| 10897 | _SG_ERROR(D3D11_CREATE_DEPTH_STENCIL_STATE_FAILED); |
| 10898 | return SG_RESOURCESTATE_FAILED; |
| 10899 | } |
| 10900 | _sg_d3d11_setlabel(pip->d3d11.dss, desc->label); |
| 10901 | |
| 10902 | // create blend state |
| 10903 | D3D11_BLEND_DESC bs_desc; |
| 10904 | _sg_clear(&bs_desc, sizeof(bs_desc)); |
| 10905 | bs_desc.AlphaToCoverageEnable = desc->alpha_to_coverage_enabled; |
| 10906 | bs_desc.IndependentBlendEnable = TRUE; |
| 10907 | { |
| 10908 | int i = 0; |
| 10909 | for (i = 0; i < desc->color_count; i++) { |
| 10910 | const sg_blend_state* src = &desc->colors[i].blend; |
| 10911 | D3D11_RENDER_TARGET_BLEND_DESC* dst = &bs_desc.RenderTarget[i]; |
| 10912 | dst->BlendEnable = src->enabled; |
| 10913 | dst->SrcBlend = _sg_d3d11_blend_factor(src->src_factor_rgb); |
| 10914 | dst->DestBlend = _sg_d3d11_blend_factor(src->dst_factor_rgb); |
| 10915 | dst->BlendOp = _sg_d3d11_blend_op(src->op_rgb); |
| 10916 | dst->SrcBlendAlpha = _sg_d3d11_blend_factor(src->src_factor_alpha); |
| 10917 | dst->DestBlendAlpha = _sg_d3d11_blend_factor(src->dst_factor_alpha); |
| 10918 | dst->BlendOpAlpha = _sg_d3d11_blend_op(src->op_alpha); |
| 10919 | dst->RenderTargetWriteMask = _sg_d3d11_color_write_mask(desc->colors[i].write_mask); |
| 10920 | } |
| 10921 | for (; i < 8; i++) { |
| 10922 | D3D11_RENDER_TARGET_BLEND_DESC* dst = &bs_desc.RenderTarget[i]; |
| 10923 | dst->BlendEnable = FALSE; |
| 10924 | dst->SrcBlend = dst->SrcBlendAlpha = D3D11_BLEND_ONE; |
| 10925 | dst->DestBlend = dst->DestBlendAlpha = D3D11_BLEND_ZERO; |
| 10926 | dst->BlendOp = dst->BlendOpAlpha = D3D11_BLEND_OP_ADD; |
| 10927 | dst->RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; |
| 10928 | } |
| 10929 | } |
| 10930 | hr = _sg_d3d11_CreateBlendState(_sg.d3d11.dev, &bs_desc, &pip->d3d11.bs); |
| 10931 | if (!(SUCCEEDED(hr) && pip->d3d11.bs)) { |
| 10932 | _SG_ERROR(D3D11_CREATE_BLEND_STATE_FAILED); |
| 10933 | return SG_RESOURCESTATE_FAILED; |
| 10934 | } |
| 10935 | _sg_d3d11_setlabel(pip->d3d11.bs, desc->label); |
| 10936 | return SG_RESOURCESTATE_VALID; |
| 10937 | } |
| 10938 | |
| 10939 | _SOKOL_PRIVATE void _sg_d3d11_discard_pipeline(_sg_pipeline_t* pip) { |
| 10940 | SOKOL_ASSERT(pip); |
| 10941 | if (pip == _sg.d3d11.cur_pipeline) { |
| 10942 | _sg.d3d11.cur_pipeline = 0; |
| 10943 | _sg.d3d11.cur_pipeline_id.id = SG_INVALID_ID; |
| 10944 | } |
| 10945 | if (pip->d3d11.il) { |
| 10946 | _sg_d3d11_Release(pip->d3d11.il); |
| 10947 | } |
| 10948 | if (pip->d3d11.rs) { |
| 10949 | _sg_d3d11_Release(pip->d3d11.rs); |
| 10950 | } |
| 10951 | if (pip->d3d11.dss) { |
| 10952 | _sg_d3d11_Release(pip->d3d11.dss); |
| 10953 | } |
| 10954 | if (pip->d3d11.bs) { |
| 10955 | _sg_d3d11_Release(pip->d3d11.bs); |
| 10956 | } |
| 10957 | } |
| 10958 | |
| 10959 | _SOKOL_PRIVATE sg_resource_state _sg_d3d11_create_attachments(_sg_attachments_t* atts, _sg_image_t** color_images, _sg_image_t** resolve_images, _sg_image_t* ds_img, const sg_attachments_desc* desc) { |
| 10960 | SOKOL_ASSERT(atts && desc); |
| 10961 | SOKOL_ASSERT(color_images && resolve_images); |
| 10962 | SOKOL_ASSERT(_sg.d3d11.dev); |
| 10963 | |
| 10964 | // copy image pointers |
| 10965 | for (int i = 0; i < atts->cmn.num_colors; i++) { |
| 10966 | const sg_attachment_desc* color_desc = &desc->colors[i]; |
| 10967 | _SOKOL_UNUSED(color_desc); |
| 10968 | SOKOL_ASSERT(color_desc->image.id != SG_INVALID_ID); |
| 10969 | SOKOL_ASSERT(0 == atts->d3d11.colors[i].image); |
| 10970 | SOKOL_ASSERT(color_images[i] && (color_images[i]->slot.id == color_desc->image.id)); |
| 10971 | SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(color_images[i]->cmn.pixel_format)); |
| 10972 | atts->d3d11.colors[i].image = color_images[i]; |
| 10973 | |
| 10974 | const sg_attachment_desc* resolve_desc = &desc->resolves[i]; |
| 10975 | if (resolve_desc->image.id != SG_INVALID_ID) { |
| 10976 | SOKOL_ASSERT(0 == atts->d3d11.resolves[i].image); |
| 10977 | SOKOL_ASSERT(resolve_images[i] && (resolve_images[i]->slot.id == resolve_desc->image.id)); |
| 10978 | SOKOL_ASSERT(color_images[i] && (color_images[i]->cmn.pixel_format == resolve_images[i]->cmn.pixel_format)); |
| 10979 | atts->d3d11.resolves[i].image = resolve_images[i]; |
| 10980 | } |
| 10981 | } |
| 10982 | SOKOL_ASSERT(0 == atts->d3d11.depth_stencil.image); |
| 10983 | const sg_attachment_desc* ds_desc = &desc->depth_stencil; |
| 10984 | if (ds_desc->image.id != SG_INVALID_ID) { |
| 10985 | SOKOL_ASSERT(ds_img && (ds_img->slot.id == ds_desc->image.id)); |
| 10986 | SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(ds_img->cmn.pixel_format)); |
| 10987 | atts->d3d11.depth_stencil.image = ds_img; |
| 10988 | } |
| 10989 | |
| 10990 | // create render-target views |
| 10991 | for (int i = 0; i < atts->cmn.num_colors; i++) { |
| 10992 | const _sg_attachment_common_t* cmn_color_att = &atts->cmn.colors[i]; |
| 10993 | const _sg_image_t* color_img = color_images[i]; |
| 10994 | SOKOL_ASSERT(0 == atts->d3d11.colors[i].view.rtv); |
| 10995 | const bool msaa = color_img->cmn.sample_count > 1; |
| 10996 | D3D11_RENDER_TARGET_VIEW_DESC d3d11_rtv_desc; |
| 10997 | _sg_clear(&d3d11_rtv_desc, sizeof(d3d11_rtv_desc)); |
| 10998 | d3d11_rtv_desc.Format = _sg_d3d11_rtv_pixel_format(color_img->cmn.pixel_format); |
| 10999 | if (color_img->cmn.type == SG_IMAGETYPE_2D) { |
| 11000 | if (msaa) { |
| 11001 | d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMS; |
| 11002 | } else { |
| 11003 | d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; |
| 11004 | d3d11_rtv_desc.Texture2D.MipSlice = (UINT)cmn_color_att->mip_level; |
| 11005 | } |
| 11006 | } else if ((color_img->cmn.type == SG_IMAGETYPE_CUBE) || (color_img->cmn.type == SG_IMAGETYPE_ARRAY)) { |
| 11007 | if (msaa) { |
| 11008 | d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY; |
| 11009 | d3d11_rtv_desc.Texture2DMSArray.FirstArraySlice = (UINT)cmn_color_att->slice; |
| 11010 | d3d11_rtv_desc.Texture2DMSArray.ArraySize = 1; |
| 11011 | } else { |
| 11012 | d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY; |
| 11013 | d3d11_rtv_desc.Texture2DArray.MipSlice = (UINT)cmn_color_att->mip_level; |
| 11014 | d3d11_rtv_desc.Texture2DArray.FirstArraySlice = (UINT)cmn_color_att->slice; |
| 11015 | d3d11_rtv_desc.Texture2DArray.ArraySize = 1; |
| 11016 | } |
| 11017 | } else { |
| 11018 | SOKOL_ASSERT(color_img->cmn.type == SG_IMAGETYPE_3D); |
| 11019 | SOKOL_ASSERT(!msaa); |
| 11020 | d3d11_rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE3D; |
| 11021 | d3d11_rtv_desc.Texture3D.MipSlice = (UINT)cmn_color_att->mip_level; |
| 11022 | d3d11_rtv_desc.Texture3D.FirstWSlice = (UINT)cmn_color_att->slice; |
| 11023 | d3d11_rtv_desc.Texture3D.WSize = 1; |
| 11024 | } |
| 11025 | SOKOL_ASSERT(color_img->d3d11.res); |
| 11026 | HRESULT hr = _sg_d3d11_CreateRenderTargetView(_sg.d3d11.dev, color_img->d3d11.res, &d3d11_rtv_desc, &atts->d3d11.colors[i].view.rtv); |
| 11027 | if (!(SUCCEEDED(hr) && atts->d3d11.colors[i].view.rtv)) { |
| 11028 | _SG_ERROR(D3D11_CREATE_RTV_FAILED); |
| 11029 | return SG_RESOURCESTATE_FAILED; |
| 11030 | } |
| 11031 | _sg_d3d11_setlabel(atts->d3d11.colors[i].view.rtv, desc->label); |
| 11032 | } |
| 11033 | SOKOL_ASSERT(0 == atts->d3d11.depth_stencil.view.dsv); |
| 11034 | if (ds_desc->image.id != SG_INVALID_ID) { |
| 11035 | const _sg_attachment_common_t* cmn_ds_att = &atts->cmn.depth_stencil; |
| 11036 | const bool msaa = ds_img->cmn.sample_count > 1; |
| 11037 | D3D11_DEPTH_STENCIL_VIEW_DESC d3d11_dsv_desc; |
| 11038 | _sg_clear(&d3d11_dsv_desc, sizeof(d3d11_dsv_desc)); |
| 11039 | d3d11_dsv_desc.Format = _sg_d3d11_dsv_pixel_format(ds_img->cmn.pixel_format); |
| 11040 | SOKOL_ASSERT(ds_img && ds_img->cmn.type != SG_IMAGETYPE_3D); |
| 11041 | if (ds_img->cmn.type == SG_IMAGETYPE_2D) { |
| 11042 | if (msaa) { |
| 11043 | d3d11_dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS; |
| 11044 | } else { |
| 11045 | d3d11_dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; |
| 11046 | d3d11_dsv_desc.Texture2D.MipSlice = (UINT)cmn_ds_att->mip_level; |
| 11047 | } |
| 11048 | } else if ((ds_img->cmn.type == SG_IMAGETYPE_CUBE) || (ds_img->cmn.type == SG_IMAGETYPE_ARRAY)) { |
| 11049 | if (msaa) { |
| 11050 | d3d11_dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY; |
| 11051 | d3d11_dsv_desc.Texture2DMSArray.FirstArraySlice = (UINT)cmn_ds_att->slice; |
| 11052 | d3d11_dsv_desc.Texture2DMSArray.ArraySize = 1; |
| 11053 | } else { |
| 11054 | d3d11_dsv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY; |
| 11055 | d3d11_dsv_desc.Texture2DArray.MipSlice = (UINT)cmn_ds_att->mip_level; |
| 11056 | d3d11_dsv_desc.Texture2DArray.FirstArraySlice = (UINT)cmn_ds_att->slice; |
| 11057 | d3d11_dsv_desc.Texture2DArray.ArraySize = 1; |
| 11058 | } |
| 11059 | } |
| 11060 | SOKOL_ASSERT(ds_img->d3d11.res); |
| 11061 | HRESULT hr = _sg_d3d11_CreateDepthStencilView(_sg.d3d11.dev, ds_img->d3d11.res, &d3d11_dsv_desc, &atts->d3d11.depth_stencil.view.dsv); |
| 11062 | if (!(SUCCEEDED(hr) && atts->d3d11.depth_stencil.view.dsv)) { |
| 11063 | _SG_ERROR(D3D11_CREATE_DSV_FAILED); |
| 11064 | return SG_RESOURCESTATE_FAILED; |
| 11065 | } |
| 11066 | _sg_d3d11_setlabel(atts->d3d11.depth_stencil.view.dsv, desc->label); |
| 11067 | } |
| 11068 | return SG_RESOURCESTATE_VALID; |
| 11069 | } |
| 11070 | |
| 11071 | _SOKOL_PRIVATE void _sg_d3d11_discard_attachments(_sg_attachments_t* atts) { |
| 11072 | SOKOL_ASSERT(atts); |
| 11073 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 11074 | if (atts->d3d11.colors[i].view.rtv) { |
| 11075 | _sg_d3d11_Release(atts->d3d11.colors[i].view.rtv); |
| 11076 | } |
| 11077 | if (atts->d3d11.resolves[i].view.rtv) { |
| 11078 | _sg_d3d11_Release(atts->d3d11.resolves[i].view.rtv); |
| 11079 | } |
| 11080 | } |
| 11081 | if (atts->d3d11.depth_stencil.view.dsv) { |
| 11082 | _sg_d3d11_Release(atts->d3d11.depth_stencil.view.dsv); |
| 11083 | } |
| 11084 | } |
| 11085 | |
| 11086 | _SOKOL_PRIVATE _sg_image_t* _sg_d3d11_attachments_color_image(const _sg_attachments_t* atts, int index) { |
| 11087 | SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS)); |
| 11088 | return atts->d3d11.colors[index].image; |
| 11089 | } |
| 11090 | |
| 11091 | _SOKOL_PRIVATE _sg_image_t* _sg_d3d11_attachments_resolve_image(const _sg_attachments_t* atts, int index) { |
| 11092 | SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS)); |
| 11093 | return atts->d3d11.resolves[index].image; |
| 11094 | } |
| 11095 | |
| 11096 | _SOKOL_PRIVATE _sg_image_t* _sg_d3d11_attachments_ds_image(const _sg_attachments_t* atts) { |
| 11097 | SOKOL_ASSERT(atts); |
| 11098 | return atts->d3d11.depth_stencil.image; |
| 11099 | } |
| 11100 | |
| 11101 | _SOKOL_PRIVATE void _sg_d3d11_begin_pass(const sg_pass* pass) { |
| 11102 | SOKOL_ASSERT(pass); |
| 11103 | |
| 11104 | const _sg_attachments_t* atts = _sg.cur_pass.atts; |
| 11105 | const sg_swapchain* swapchain = &pass->swapchain; |
| 11106 | const sg_pass_action* action = &pass->action; |
| 11107 | |
| 11108 | int num_rtvs = 0; |
| 11109 | ID3D11RenderTargetView* rtvs[SG_MAX_COLOR_ATTACHMENTS] = { 0 }; |
| 11110 | ID3D11DepthStencilView* dsv = 0; |
| 11111 | _sg.d3d11.cur_pass.render_view = 0; |
| 11112 | _sg.d3d11.cur_pass.resolve_view = 0; |
| 11113 | if (atts) { |
| 11114 | num_rtvs = atts->cmn.num_colors; |
| 11115 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 11116 | rtvs[i] = atts->d3d11.colors[i].view.rtv; |
| 11117 | } |
| 11118 | dsv = atts->d3d11.depth_stencil.view.dsv; |
| 11119 | } else { |
| 11120 | // NOTE: depth-stencil-view is optional |
| 11121 | SOKOL_ASSERT(swapchain->d3d11.render_view); |
| 11122 | num_rtvs = 1; |
| 11123 | rtvs[0] = (ID3D11RenderTargetView*) swapchain->d3d11.render_view; |
| 11124 | dsv = (ID3D11DepthStencilView*) swapchain->d3d11.depth_stencil_view; |
| 11125 | _sg.d3d11.cur_pass.render_view = (ID3D11RenderTargetView*) swapchain->d3d11.render_view; |
| 11126 | _sg.d3d11.cur_pass.resolve_view = (ID3D11RenderTargetView*) swapchain->d3d11.resolve_view; |
| 11127 | } |
| 11128 | // apply the render-target- and depth-stencil-views |
| 11129 | _sg_d3d11_OMSetRenderTargets(_sg.d3d11.ctx, SG_MAX_COLOR_ATTACHMENTS, rtvs, dsv); |
| 11130 | _sg_stats_add(d3d11.pass.num_om_set_render_targets, 1); |
| 11131 | |
| 11132 | // set viewport and scissor rect to cover whole screen |
| 11133 | D3D11_VIEWPORT vp; |
| 11134 | _sg_clear(&vp, sizeof(vp)); |
| 11135 | vp.Width = (FLOAT) _sg.cur_pass.width; |
| 11136 | vp.Height = (FLOAT) _sg.cur_pass.height; |
| 11137 | vp.MaxDepth = 1.0f; |
| 11138 | _sg_d3d11_RSSetViewports(_sg.d3d11.ctx, 1, &vp); |
| 11139 | D3D11_RECT rect; |
| 11140 | rect.left = 0; |
| 11141 | rect.top = 0; |
| 11142 | rect.right = _sg.cur_pass.width; |
| 11143 | rect.bottom = _sg.cur_pass.height; |
| 11144 | _sg_d3d11_RSSetScissorRects(_sg.d3d11.ctx, 1, &rect); |
| 11145 | |
| 11146 | // perform clear action |
| 11147 | for (int i = 0; i < num_rtvs; i++) { |
| 11148 | if (action->colors[i].load_action == SG_LOADACTION_CLEAR) { |
| 11149 | _sg_d3d11_ClearRenderTargetView(_sg.d3d11.ctx, rtvs[i], (float*)&action->colors[i].clear_value); |
| 11150 | _sg_stats_add(d3d11.pass.num_clear_render_target_view, 1); |
| 11151 | } |
| 11152 | } |
| 11153 | UINT ds_flags = 0; |
| 11154 | if (action->depth.load_action == SG_LOADACTION_CLEAR) { |
| 11155 | ds_flags |= D3D11_CLEAR_DEPTH; |
| 11156 | } |
| 11157 | if (action->stencil.load_action == SG_LOADACTION_CLEAR) { |
| 11158 | ds_flags |= D3D11_CLEAR_STENCIL; |
| 11159 | } |
| 11160 | if ((0 != ds_flags) && dsv) { |
| 11161 | _sg_d3d11_ClearDepthStencilView(_sg.d3d11.ctx, dsv, ds_flags, action->depth.clear_value, action->stencil.clear_value); |
| 11162 | _sg_stats_add(d3d11.pass.num_clear_depth_stencil_view, 1); |
| 11163 | } |
| 11164 | } |
| 11165 | |
| 11166 | // D3D11CalcSubresource only exists for C++ |
| 11167 | _SOKOL_PRIVATE UINT _sg_d3d11_calcsubresource(UINT mip_slice, UINT array_slice, UINT mip_levels) { |
| 11168 | return mip_slice + array_slice * mip_levels; |
| 11169 | } |
| 11170 | |
| 11171 | _SOKOL_PRIVATE void _sg_d3d11_end_pass(void) { |
| 11172 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 11173 | |
| 11174 | // need to resolve MSAA render attachments into texture? |
| 11175 | if (_sg.cur_pass.atts_id.id != SG_INVALID_ID) { |
| 11176 | // ...for offscreen pass... |
| 11177 | SOKOL_ASSERT(_sg.cur_pass.atts && _sg.cur_pass.atts->slot.id == _sg.cur_pass.atts_id.id); |
| 11178 | for (int i = 0; i < _sg.cur_pass.atts->cmn.num_colors; i++) { |
| 11179 | const _sg_image_t* resolve_img = _sg.cur_pass.atts->d3d11.resolves[i].image; |
| 11180 | if (resolve_img) { |
| 11181 | const _sg_image_t* color_img = _sg.cur_pass.atts->d3d11.colors[i].image; |
| 11182 | const _sg_attachment_common_t* cmn_color_att = &_sg.cur_pass.atts->cmn.colors[i]; |
| 11183 | const _sg_attachment_common_t* cmn_resolve_att = &_sg.cur_pass.atts->cmn.resolves[i]; |
| 11184 | SOKOL_ASSERT(resolve_img->slot.id == cmn_resolve_att->image_id.id); |
| 11185 | SOKOL_ASSERT(color_img && (color_img->slot.id == cmn_color_att->image_id.id)); |
| 11186 | SOKOL_ASSERT(color_img->cmn.sample_count > 1); |
| 11187 | SOKOL_ASSERT(resolve_img->cmn.sample_count == 1); |
| 11188 | const UINT src_subres = _sg_d3d11_calcsubresource( |
| 11189 | (UINT)cmn_color_att->mip_level, |
| 11190 | (UINT)cmn_color_att->slice, |
| 11191 | (UINT)color_img->cmn.num_mipmaps); |
| 11192 | const UINT dst_subres = _sg_d3d11_calcsubresource( |
| 11193 | (UINT)cmn_resolve_att->mip_level, |
| 11194 | (UINT)cmn_resolve_att->slice, |
| 11195 | (UINT)resolve_img->cmn.num_mipmaps); |
| 11196 | _sg_d3d11_ResolveSubresource(_sg.d3d11.ctx, |
| 11197 | resolve_img->d3d11.res, |
| 11198 | dst_subres, |
| 11199 | color_img->d3d11.res, |
| 11200 | src_subres, |
| 11201 | color_img->d3d11.format); |
| 11202 | _sg_stats_add(d3d11.pass.num_resolve_subresource, 1); |
| 11203 | } |
| 11204 | } |
| 11205 | } else { |
| 11206 | // ...for swapchain pass... |
| 11207 | if (_sg.d3d11.cur_pass.resolve_view) { |
| 11208 | SOKOL_ASSERT(_sg.d3d11.cur_pass.render_view); |
| 11209 | SOKOL_ASSERT(_sg.cur_pass.swapchain.sample_count > 1); |
| 11210 | SOKOL_ASSERT(_sg.cur_pass.swapchain.color_fmt > SG_PIXELFORMAT_NONE); |
| 11211 | ID3D11Resource* d3d11_render_res = 0; |
| 11212 | ID3D11Resource* d3d11_resolve_res = 0; |
| 11213 | _sg_d3d11_GetResource((ID3D11View*)_sg.d3d11.cur_pass.render_view, &d3d11_render_res); |
| 11214 | _sg_d3d11_GetResource((ID3D11View*)_sg.d3d11.cur_pass.resolve_view, &d3d11_resolve_res); |
| 11215 | SOKOL_ASSERT(d3d11_render_res); |
| 11216 | SOKOL_ASSERT(d3d11_resolve_res); |
| 11217 | const sg_pixel_format color_fmt = _sg.cur_pass.swapchain.color_fmt; |
| 11218 | _sg_d3d11_ResolveSubresource(_sg.d3d11.ctx, d3d11_resolve_res, 0, d3d11_render_res, 0, _sg_d3d11_rtv_pixel_format(color_fmt)); |
| 11219 | _sg_d3d11_Release(d3d11_render_res); |
| 11220 | _sg_d3d11_Release(d3d11_resolve_res); |
| 11221 | _sg_stats_add(d3d11.pass.num_resolve_subresource, 1); |
| 11222 | } |
| 11223 | } |
| 11224 | _sg.d3d11.cur_pass.render_view = 0; |
| 11225 | _sg.d3d11.cur_pass.resolve_view = 0; |
| 11226 | _sg.d3d11.cur_pipeline = 0; |
| 11227 | _sg.d3d11.cur_pipeline_id.id = SG_INVALID_ID; |
| 11228 | _sg_d3d11_clear_state(); |
| 11229 | } |
| 11230 | |
| 11231 | _SOKOL_PRIVATE void _sg_d3d11_apply_viewport(int x, int y, int w, int h, bool origin_top_left) { |
| 11232 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 11233 | D3D11_VIEWPORT vp; |
| 11234 | vp.TopLeftX = (FLOAT) x; |
| 11235 | vp.TopLeftY = (FLOAT) (origin_top_left ? y : (_sg.cur_pass.height - (y + h))); |
| 11236 | vp.Width = (FLOAT) w; |
| 11237 | vp.Height = (FLOAT) h; |
| 11238 | vp.MinDepth = 0.0f; |
| 11239 | vp.MaxDepth = 1.0f; |
| 11240 | _sg_d3d11_RSSetViewports(_sg.d3d11.ctx, 1, &vp); |
| 11241 | } |
| 11242 | |
| 11243 | _SOKOL_PRIVATE void _sg_d3d11_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) { |
| 11244 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 11245 | D3D11_RECT rect; |
| 11246 | rect.left = x; |
| 11247 | rect.top = (origin_top_left ? y : (_sg.cur_pass.height - (y + h))); |
| 11248 | rect.right = x + w; |
| 11249 | rect.bottom = origin_top_left ? (y + h) : (_sg.cur_pass.height - y); |
| 11250 | _sg_d3d11_RSSetScissorRects(_sg.d3d11.ctx, 1, &rect); |
| 11251 | } |
| 11252 | |
| 11253 | _SOKOL_PRIVATE void _sg_d3d11_apply_pipeline(_sg_pipeline_t* pip) { |
| 11254 | SOKOL_ASSERT(pip); |
| 11255 | SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id)); |
| 11256 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 11257 | SOKOL_ASSERT(pip->d3d11.rs && pip->d3d11.bs && pip->d3d11.dss); |
| 11258 | |
| 11259 | _sg.d3d11.cur_pipeline = pip; |
| 11260 | _sg.d3d11.cur_pipeline_id.id = pip->slot.id; |
| 11261 | _sg.d3d11.use_indexed_draw = (pip->d3d11.index_format != DXGI_FORMAT_UNKNOWN); |
| 11262 | _sg.d3d11.use_instanced_draw = pip->cmn.use_instanced_draw; |
| 11263 | |
| 11264 | _sg_d3d11_RSSetState(_sg.d3d11.ctx, pip->d3d11.rs); |
| 11265 | _sg_d3d11_OMSetDepthStencilState(_sg.d3d11.ctx, pip->d3d11.dss, pip->d3d11.stencil_ref); |
| 11266 | _sg_d3d11_OMSetBlendState(_sg.d3d11.ctx, pip->d3d11.bs, (float*)&pip->cmn.blend_color, 0xFFFFFFFF); |
| 11267 | _sg_d3d11_IASetPrimitiveTopology(_sg.d3d11.ctx, pip->d3d11.topology); |
| 11268 | _sg_d3d11_IASetInputLayout(_sg.d3d11.ctx, pip->d3d11.il); |
| 11269 | _sg_d3d11_VSSetShader(_sg.d3d11.ctx, pip->shader->d3d11.vs, NULL, 0); |
| 11270 | _sg_d3d11_VSSetConstantBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_UBS, pip->shader->d3d11.stage[SG_SHADERSTAGE_VS].cbufs); |
| 11271 | _sg_d3d11_PSSetShader(_sg.d3d11.ctx, pip->shader->d3d11.fs, NULL, 0); |
| 11272 | _sg_d3d11_PSSetConstantBuffers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_UBS, pip->shader->d3d11.stage[SG_SHADERSTAGE_FS].cbufs); |
| 11273 | _sg_stats_add(d3d11.pipeline.num_rs_set_state, 1); |
| 11274 | _sg_stats_add(d3d11.pipeline.num_om_set_depth_stencil_state, 1); |
| 11275 | _sg_stats_add(d3d11.pipeline.num_om_set_blend_state, 1); |
| 11276 | _sg_stats_add(d3d11.pipeline.num_ia_set_primitive_topology, 1); |
| 11277 | _sg_stats_add(d3d11.pipeline.num_ia_set_input_layout, 1); |
| 11278 | _sg_stats_add(d3d11.pipeline.num_vs_set_shader, 1); |
| 11279 | _sg_stats_add(d3d11.pipeline.num_vs_set_constant_buffers, 1); |
| 11280 | _sg_stats_add(d3d11.pipeline.num_ps_set_shader, 1); |
| 11281 | _sg_stats_add(d3d11.pipeline.num_ps_set_constant_buffers, 1); |
| 11282 | } |
| 11283 | |
| 11284 | _SOKOL_PRIVATE bool _sg_d3d11_apply_bindings(_sg_bindings_t* bnd) { |
| 11285 | SOKOL_ASSERT(bnd); |
| 11286 | SOKOL_ASSERT(bnd->pip); |
| 11287 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 11288 | |
| 11289 | // gather all the D3D11 resources into arrays |
| 11290 | ID3D11Buffer* d3d11_ib = bnd->ib ? bnd->ib->d3d11.buf : 0; |
| 11291 | ID3D11Buffer* d3d11_vbs[SG_MAX_VERTEX_BUFFERS] = {0}; |
| 11292 | UINT d3d11_vb_offsets[SG_MAX_VERTEX_BUFFERS] = {0}; |
| 11293 | ID3D11ShaderResourceView* d3d11_vs_srvs[_SG_D3D11_MAX_SHADERSTAGE_SRVS] = {0}; |
| 11294 | ID3D11ShaderResourceView* d3d11_fs_srvs[_SG_D3D11_MAX_SHADERSTAGE_SRVS] = {0}; |
| 11295 | ID3D11SamplerState* d3d11_vs_smps[SG_MAX_SHADERSTAGE_SAMPLERS] = {0}; |
| 11296 | ID3D11SamplerState* d3d11_fs_smps[SG_MAX_SHADERSTAGE_SAMPLERS] = {0}; |
| 11297 | for (int i = 0; i < bnd->num_vbs; i++) { |
| 11298 | SOKOL_ASSERT(bnd->vbs[i]->d3d11.buf); |
| 11299 | d3d11_vbs[i] = bnd->vbs[i]->d3d11.buf; |
| 11300 | d3d11_vb_offsets[i] = (UINT)bnd->vb_offsets[i]; |
| 11301 | } |
| 11302 | for (int i = 0; i < bnd->num_vs_imgs; i++) { |
| 11303 | SOKOL_ASSERT(bnd->vs_imgs[i]->d3d11.srv); |
| 11304 | d3d11_vs_srvs[_SG_D3D11_SHADERSTAGE_IMAGE_SRV_OFFSET + i] = bnd->vs_imgs[i]->d3d11.srv; |
| 11305 | } |
| 11306 | for (int i = 0; i < bnd->num_vs_sbufs; i++) { |
| 11307 | SOKOL_ASSERT(bnd->vs_sbufs[i]->d3d11.srv); |
| 11308 | d3d11_vs_srvs[_SG_D3D11_SHADERSTAGE_BUFFER_SRV_OFFSET + i] = bnd->vs_sbufs[i]->d3d11.srv; |
| 11309 | } |
| 11310 | for (int i = 0; i < bnd->num_fs_imgs; i++) { |
| 11311 | SOKOL_ASSERT(bnd->fs_imgs[i]->d3d11.srv); |
| 11312 | d3d11_fs_srvs[_SG_D3D11_SHADERSTAGE_IMAGE_SRV_OFFSET + i] = bnd->fs_imgs[i]->d3d11.srv; |
| 11313 | } |
| 11314 | for (int i = 0; i < bnd->num_fs_sbufs; i++) { |
| 11315 | SOKOL_ASSERT(bnd->fs_sbufs[i]->d3d11.srv); |
| 11316 | d3d11_fs_srvs[_SG_D3D11_SHADERSTAGE_BUFFER_SRV_OFFSET + i] = bnd->fs_sbufs[i]->d3d11.srv; |
| 11317 | } |
| 11318 | for (int i = 0; i < bnd->num_vs_smps; i++) { |
| 11319 | SOKOL_ASSERT(bnd->vs_smps[i]->d3d11.smp); |
| 11320 | d3d11_vs_smps[i] = bnd->vs_smps[i]->d3d11.smp; |
| 11321 | } |
| 11322 | for (int i = 0; i < bnd->num_fs_smps; i++) { |
| 11323 | SOKOL_ASSERT(bnd->fs_smps[i]->d3d11.smp); |
| 11324 | d3d11_fs_smps[i] = bnd->fs_smps[i]->d3d11.smp; |
| 11325 | } |
| 11326 | _sg_d3d11_IASetVertexBuffers(_sg.d3d11.ctx, 0, SG_MAX_VERTEX_BUFFERS, d3d11_vbs, bnd->pip->d3d11.vb_strides, d3d11_vb_offsets); |
| 11327 | _sg_d3d11_IASetIndexBuffer(_sg.d3d11.ctx, d3d11_ib, bnd->pip->d3d11.index_format, (UINT)bnd->ib_offset); |
| 11328 | _sg_d3d11_VSSetShaderResources(_sg.d3d11.ctx, 0, _SG_D3D11_MAX_SHADERSTAGE_SRVS, d3d11_vs_srvs); |
| 11329 | _sg_d3d11_PSSetShaderResources(_sg.d3d11.ctx, 0, _SG_D3D11_MAX_SHADERSTAGE_SRVS, d3d11_fs_srvs); |
| 11330 | _sg_d3d11_VSSetSamplers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_SAMPLERS, d3d11_vs_smps); |
| 11331 | _sg_d3d11_PSSetSamplers(_sg.d3d11.ctx, 0, SG_MAX_SHADERSTAGE_SAMPLERS, d3d11_fs_smps); |
| 11332 | _sg_stats_add(d3d11.bindings.num_ia_set_vertex_buffers, 1); |
| 11333 | _sg_stats_add(d3d11.bindings.num_ia_set_index_buffer, 1); |
| 11334 | _sg_stats_add(d3d11.bindings.num_vs_set_shader_resources, 1); |
| 11335 | _sg_stats_add(d3d11.bindings.num_ps_set_shader_resources, 1); |
| 11336 | _sg_stats_add(d3d11.bindings.num_vs_set_samplers, 1); |
| 11337 | _sg_stats_add(d3d11.bindings.num_ps_set_samplers, 1); |
| 11338 | return true; |
| 11339 | } |
| 11340 | |
| 11341 | _SOKOL_PRIVATE void _sg_d3d11_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) { |
| 11342 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 11343 | SOKOL_ASSERT(_sg.d3d11.cur_pipeline && _sg.d3d11.cur_pipeline->slot.id == _sg.d3d11.cur_pipeline_id.id); |
| 11344 | SOKOL_ASSERT(_sg.d3d11.cur_pipeline->shader && _sg.d3d11.cur_pipeline->shader->slot.id == _sg.d3d11.cur_pipeline->cmn.shader_id.id); |
| 11345 | SOKOL_ASSERT(ub_index < _sg.d3d11.cur_pipeline->shader->cmn.stage[stage_index].num_uniform_blocks); |
| 11346 | SOKOL_ASSERT(data->size == _sg.d3d11.cur_pipeline->shader->cmn.stage[stage_index].uniform_blocks[ub_index].size); |
| 11347 | ID3D11Buffer* cb = _sg.d3d11.cur_pipeline->shader->d3d11.stage[stage_index].cbufs[ub_index]; |
| 11348 | SOKOL_ASSERT(cb); |
| 11349 | _sg_d3d11_UpdateSubresource(_sg.d3d11.ctx, (ID3D11Resource*)cb, 0, NULL, data->ptr, 0, 0); |
| 11350 | _sg_stats_add(d3d11.uniforms.num_update_subresource, 1); |
| 11351 | } |
| 11352 | |
| 11353 | _SOKOL_PRIVATE void _sg_d3d11_draw(int base_element, int num_elements, int num_instances) { |
| 11354 | const bool use_instanced_draw = (num_instances > 1) || (_sg.d3d11.use_instanced_draw); |
| 11355 | if (_sg.d3d11.use_indexed_draw) { |
| 11356 | if (use_instanced_draw) { |
| 11357 | _sg_d3d11_DrawIndexedInstanced(_sg.d3d11.ctx, (UINT)num_elements, (UINT)num_instances, (UINT)base_element, 0, 0); |
| 11358 | _sg_stats_add(d3d11.draw.num_draw_indexed_instanced, 1); |
| 11359 | } else { |
| 11360 | _sg_d3d11_DrawIndexed(_sg.d3d11.ctx, (UINT)num_elements, (UINT)base_element, 0); |
| 11361 | _sg_stats_add(d3d11.draw.num_draw_indexed, 1); |
| 11362 | } |
| 11363 | } else { |
| 11364 | if (use_instanced_draw) { |
| 11365 | _sg_d3d11_DrawInstanced(_sg.d3d11.ctx, (UINT)num_elements, (UINT)num_instances, (UINT)base_element, 0); |
| 11366 | _sg_stats_add(d3d11.draw.num_draw_instanced, 1); |
| 11367 | } else { |
| 11368 | _sg_d3d11_Draw(_sg.d3d11.ctx, (UINT)num_elements, (UINT)base_element); |
| 11369 | _sg_stats_add(d3d11.draw.num_draw, 1); |
| 11370 | } |
| 11371 | } |
| 11372 | } |
| 11373 | |
| 11374 | _SOKOL_PRIVATE void _sg_d3d11_commit(void) { |
| 11375 | // empty |
| 11376 | } |
| 11377 | |
| 11378 | _SOKOL_PRIVATE void _sg_d3d11_update_buffer(_sg_buffer_t* buf, const sg_range* data) { |
| 11379 | SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0)); |
| 11380 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 11381 | SOKOL_ASSERT(buf->d3d11.buf); |
| 11382 | D3D11_MAPPED_SUBRESOURCE d3d11_msr; |
| 11383 | HRESULT hr = _sg_d3d11_Map(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11.buf, 0, D3D11_MAP_WRITE_DISCARD, 0, &d3d11_msr); |
| 11384 | _sg_stats_add(d3d11.num_map, 1); |
| 11385 | if (SUCCEEDED(hr)) { |
| 11386 | memcpy(d3d11_msr.pData, data->ptr, data->size); |
| 11387 | _sg_d3d11_Unmap(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11.buf, 0); |
| 11388 | _sg_stats_add(d3d11.num_unmap, 1); |
| 11389 | } else { |
| 11390 | _SG_ERROR(D3D11_MAP_FOR_UPDATE_BUFFER_FAILED); |
| 11391 | } |
| 11392 | } |
| 11393 | |
| 11394 | _SOKOL_PRIVATE void _sg_d3d11_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) { |
| 11395 | SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0)); |
| 11396 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 11397 | SOKOL_ASSERT(buf->d3d11.buf); |
| 11398 | D3D11_MAP map_type = new_frame ? D3D11_MAP_WRITE_DISCARD : D3D11_MAP_WRITE_NO_OVERWRITE; |
| 11399 | D3D11_MAPPED_SUBRESOURCE d3d11_msr; |
| 11400 | HRESULT hr = _sg_d3d11_Map(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11.buf, 0, map_type, 0, &d3d11_msr); |
| 11401 | _sg_stats_add(d3d11.num_map, 1); |
| 11402 | if (SUCCEEDED(hr)) { |
| 11403 | uint8_t* dst_ptr = (uint8_t*)d3d11_msr.pData + buf->cmn.append_pos; |
| 11404 | memcpy(dst_ptr, data->ptr, data->size); |
| 11405 | _sg_d3d11_Unmap(_sg.d3d11.ctx, (ID3D11Resource*)buf->d3d11.buf, 0); |
| 11406 | _sg_stats_add(d3d11.num_unmap, 1); |
| 11407 | } else { |
| 11408 | _SG_ERROR(D3D11_MAP_FOR_APPEND_BUFFER_FAILED); |
| 11409 | } |
| 11410 | } |
| 11411 | |
| 11412 | _SOKOL_PRIVATE void _sg_d3d11_update_image(_sg_image_t* img, const sg_image_data* data) { |
| 11413 | SOKOL_ASSERT(img && data); |
| 11414 | SOKOL_ASSERT(_sg.d3d11.ctx); |
| 11415 | SOKOL_ASSERT(img->d3d11.res); |
| 11416 | const int num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6:1; |
| 11417 | const int num_slices = (img->cmn.type == SG_IMAGETYPE_ARRAY) ? img->cmn.num_slices:1; |
| 11418 | UINT subres_index = 0; |
| 11419 | HRESULT hr; |
| 11420 | D3D11_MAPPED_SUBRESOURCE d3d11_msr; |
| 11421 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 11422 | for (int slice_index = 0; slice_index < num_slices; slice_index++) { |
| 11423 | for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++, subres_index++) { |
| 11424 | SOKOL_ASSERT(subres_index < (SG_MAX_MIPMAPS * SG_MAX_TEXTUREARRAY_LAYERS)); |
| 11425 | const int mip_width = _sg_miplevel_dim(img->cmn.width, mip_index); |
| 11426 | const int mip_height = _sg_miplevel_dim(img->cmn.height, mip_index); |
| 11427 | const int src_pitch = _sg_row_pitch(img->cmn.pixel_format, mip_width, 1); |
| 11428 | const sg_range* subimg_data = &(data->subimage[face_index][mip_index]); |
| 11429 | const size_t slice_size = subimg_data->size / (size_t)num_slices; |
| 11430 | const size_t slice_offset = slice_size * (size_t)slice_index; |
| 11431 | const uint8_t* slice_ptr = ((const uint8_t*)subimg_data->ptr) + slice_offset; |
| 11432 | hr = _sg_d3d11_Map(_sg.d3d11.ctx, img->d3d11.res, subres_index, D3D11_MAP_WRITE_DISCARD, 0, &d3d11_msr); |
| 11433 | _sg_stats_add(d3d11.num_map, 1); |
| 11434 | if (SUCCEEDED(hr)) { |
| 11435 | // FIXME: need to handle difference in depth-pitch for 3D textures as well! |
| 11436 | if (src_pitch == (int)d3d11_msr.RowPitch) { |
| 11437 | memcpy(d3d11_msr.pData, slice_ptr, slice_size); |
| 11438 | } else { |
| 11439 | SOKOL_ASSERT(src_pitch < (int)d3d11_msr.RowPitch); |
| 11440 | const uint8_t* src_ptr = slice_ptr; |
| 11441 | uint8_t* dst_ptr = (uint8_t*) d3d11_msr.pData; |
| 11442 | for (int row_index = 0; row_index < mip_height; row_index++) { |
| 11443 | memcpy(dst_ptr, src_ptr, (size_t)src_pitch); |
| 11444 | src_ptr += src_pitch; |
| 11445 | dst_ptr += d3d11_msr.RowPitch; |
| 11446 | } |
| 11447 | } |
| 11448 | _sg_d3d11_Unmap(_sg.d3d11.ctx, img->d3d11.res, subres_index); |
| 11449 | _sg_stats_add(d3d11.num_unmap, 1); |
| 11450 | } else { |
| 11451 | _SG_ERROR(D3D11_MAP_FOR_UPDATE_IMAGE_FAILED); |
| 11452 | } |
| 11453 | } |
| 11454 | } |
| 11455 | } |
| 11456 | } |
| 11457 | |
| 11458 | // ███ ███ ███████ ████████ █████ ██ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████ |
| 11459 | // ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ |
| 11460 | // ██ ████ ██ █████ ██ ███████ ██ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██ |
| 11461 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 11462 | // ██ ██ ███████ ██ ██ ██ ███████ ██████ ██ ██ ██████ ██ ██ ███████ ██ ████ ██████ |
| 11463 | // |
| 11464 | // >>metal backend |
| 11465 | #elif defined(SOKOL_METAL) |
| 11466 | |
| 11467 | #if __has_feature(objc_arc) |
| 11468 | #define _SG_OBJC_RETAIN(obj) { } |
| 11469 | #define _SG_OBJC_RELEASE(obj) { obj = nil; } |
| 11470 | #else |
| 11471 | #define _SG_OBJC_RETAIN(obj) { [obj retain]; } |
| 11472 | #define _SG_OBJC_RELEASE(obj) { [obj release]; obj = nil; } |
| 11473 | #endif |
| 11474 | |
| 11475 | //-- enum translation functions ------------------------------------------------ |
| 11476 | _SOKOL_PRIVATE MTLLoadAction _sg_mtl_load_action(sg_load_action a) { |
| 11477 | switch (a) { |
| 11478 | case SG_LOADACTION_CLEAR: return MTLLoadActionClear; |
| 11479 | case SG_LOADACTION_LOAD: return MTLLoadActionLoad; |
| 11480 | case SG_LOADACTION_DONTCARE: return MTLLoadActionDontCare; |
| 11481 | default: SOKOL_UNREACHABLE; return (MTLLoadAction)0; |
| 11482 | } |
| 11483 | } |
| 11484 | |
| 11485 | _SOKOL_PRIVATE MTLStoreAction _sg_mtl_store_action(sg_store_action a, bool resolve) { |
| 11486 | switch (a) { |
| 11487 | case SG_STOREACTION_STORE: |
| 11488 | if (resolve) { |
| 11489 | return MTLStoreActionStoreAndMultisampleResolve; |
| 11490 | } else { |
| 11491 | return MTLStoreActionStore; |
| 11492 | } |
| 11493 | break; |
| 11494 | case SG_STOREACTION_DONTCARE: |
| 11495 | if (resolve) { |
| 11496 | return MTLStoreActionMultisampleResolve; |
| 11497 | } else { |
| 11498 | return MTLStoreActionDontCare; |
| 11499 | } |
| 11500 | break; |
| 11501 | default: SOKOL_UNREACHABLE; return (MTLStoreAction)0; |
| 11502 | } |
| 11503 | } |
| 11504 | |
| 11505 | _SOKOL_PRIVATE MTLResourceOptions _sg_mtl_resource_options_storage_mode_managed_or_shared(void) { |
| 11506 | #if defined(_SG_TARGET_MACOS) |
| 11507 | if (_sg.mtl.use_shared_storage_mode) { |
| 11508 | return MTLResourceStorageModeShared; |
| 11509 | } else { |
| 11510 | return MTLResourceStorageModeManaged; |
| 11511 | } |
| 11512 | #else |
| 11513 | // MTLResourceStorageModeManaged is not even defined on iOS SDK |
| 11514 | return MTLResourceStorageModeShared; |
| 11515 | #endif |
| 11516 | } |
| 11517 | |
| 11518 | _SOKOL_PRIVATE MTLResourceOptions _sg_mtl_buffer_resource_options(sg_usage usg) { |
| 11519 | switch (usg) { |
| 11520 | case SG_USAGE_IMMUTABLE: |
| 11521 | return _sg_mtl_resource_options_storage_mode_managed_or_shared(); |
| 11522 | case SG_USAGE_DYNAMIC: |
| 11523 | case SG_USAGE_STREAM: |
| 11524 | return MTLResourceCPUCacheModeWriteCombined | _sg_mtl_resource_options_storage_mode_managed_or_shared(); |
| 11525 | default: |
| 11526 | SOKOL_UNREACHABLE; |
| 11527 | return 0; |
| 11528 | } |
| 11529 | } |
| 11530 | |
| 11531 | _SOKOL_PRIVATE MTLVertexStepFunction _sg_mtl_step_function(sg_vertex_step step) { |
| 11532 | switch (step) { |
| 11533 | case SG_VERTEXSTEP_PER_VERTEX: return MTLVertexStepFunctionPerVertex; |
| 11534 | case SG_VERTEXSTEP_PER_INSTANCE: return MTLVertexStepFunctionPerInstance; |
| 11535 | default: SOKOL_UNREACHABLE; return (MTLVertexStepFunction)0; |
| 11536 | } |
| 11537 | } |
| 11538 | |
| 11539 | _SOKOL_PRIVATE MTLVertexFormat _sg_mtl_vertex_format(sg_vertex_format fmt) { |
| 11540 | switch (fmt) { |
| 11541 | case SG_VERTEXFORMAT_FLOAT: return MTLVertexFormatFloat; |
| 11542 | case SG_VERTEXFORMAT_FLOAT2: return MTLVertexFormatFloat2; |
| 11543 | case SG_VERTEXFORMAT_FLOAT3: return MTLVertexFormatFloat3; |
| 11544 | case SG_VERTEXFORMAT_FLOAT4: return MTLVertexFormatFloat4; |
| 11545 | case SG_VERTEXFORMAT_BYTE4: return MTLVertexFormatChar4; |
| 11546 | case SG_VERTEXFORMAT_BYTE4N: return MTLVertexFormatChar4Normalized; |
| 11547 | case SG_VERTEXFORMAT_UBYTE4: return MTLVertexFormatUChar4; |
| 11548 | case SG_VERTEXFORMAT_UBYTE4N: return MTLVertexFormatUChar4Normalized; |
| 11549 | case SG_VERTEXFORMAT_SHORT2: return MTLVertexFormatShort2; |
| 11550 | case SG_VERTEXFORMAT_SHORT2N: return MTLVertexFormatShort2Normalized; |
| 11551 | case SG_VERTEXFORMAT_USHORT2N: return MTLVertexFormatUShort2Normalized; |
| 11552 | case SG_VERTEXFORMAT_SHORT4: return MTLVertexFormatShort4; |
| 11553 | case SG_VERTEXFORMAT_SHORT4N: return MTLVertexFormatShort4Normalized; |
| 11554 | case SG_VERTEXFORMAT_USHORT4N: return MTLVertexFormatUShort4Normalized; |
| 11555 | case SG_VERTEXFORMAT_UINT10_N2: return MTLVertexFormatUInt1010102Normalized; |
| 11556 | case SG_VERTEXFORMAT_HALF2: return MTLVertexFormatHalf2; |
| 11557 | case SG_VERTEXFORMAT_HALF4: return MTLVertexFormatHalf4; |
| 11558 | default: SOKOL_UNREACHABLE; return (MTLVertexFormat)0; |
| 11559 | } |
| 11560 | } |
| 11561 | |
| 11562 | _SOKOL_PRIVATE MTLPrimitiveType _sg_mtl_primitive_type(sg_primitive_type t) { |
| 11563 | switch (t) { |
| 11564 | case SG_PRIMITIVETYPE_POINTS: return MTLPrimitiveTypePoint; |
| 11565 | case SG_PRIMITIVETYPE_LINES: return MTLPrimitiveTypeLine; |
| 11566 | case SG_PRIMITIVETYPE_LINE_STRIP: return MTLPrimitiveTypeLineStrip; |
| 11567 | case SG_PRIMITIVETYPE_TRIANGLES: return MTLPrimitiveTypeTriangle; |
| 11568 | case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return MTLPrimitiveTypeTriangleStrip; |
| 11569 | default: SOKOL_UNREACHABLE; return (MTLPrimitiveType)0; |
| 11570 | } |
| 11571 | } |
| 11572 | |
| 11573 | _SOKOL_PRIVATE MTLPixelFormat _sg_mtl_pixel_format(sg_pixel_format fmt) { |
| 11574 | switch (fmt) { |
| 11575 | case SG_PIXELFORMAT_R8: return MTLPixelFormatR8Unorm; |
| 11576 | case SG_PIXELFORMAT_R8SN: return MTLPixelFormatR8Snorm; |
| 11577 | case SG_PIXELFORMAT_R8UI: return MTLPixelFormatR8Uint; |
| 11578 | case SG_PIXELFORMAT_R8SI: return MTLPixelFormatR8Sint; |
| 11579 | case SG_PIXELFORMAT_R16: return MTLPixelFormatR16Unorm; |
| 11580 | case SG_PIXELFORMAT_R16SN: return MTLPixelFormatR16Snorm; |
| 11581 | case SG_PIXELFORMAT_R16UI: return MTLPixelFormatR16Uint; |
| 11582 | case SG_PIXELFORMAT_R16SI: return MTLPixelFormatR16Sint; |
| 11583 | case SG_PIXELFORMAT_R16F: return MTLPixelFormatR16Float; |
| 11584 | case SG_PIXELFORMAT_RG8: return MTLPixelFormatRG8Unorm; |
| 11585 | case SG_PIXELFORMAT_RG8SN: return MTLPixelFormatRG8Snorm; |
| 11586 | case SG_PIXELFORMAT_RG8UI: return MTLPixelFormatRG8Uint; |
| 11587 | case SG_PIXELFORMAT_RG8SI: return MTLPixelFormatRG8Sint; |
| 11588 | case SG_PIXELFORMAT_R32UI: return MTLPixelFormatR32Uint; |
| 11589 | case SG_PIXELFORMAT_R32SI: return MTLPixelFormatR32Sint; |
| 11590 | case SG_PIXELFORMAT_R32F: return MTLPixelFormatR32Float; |
| 11591 | case SG_PIXELFORMAT_RG16: return MTLPixelFormatRG16Unorm; |
| 11592 | case SG_PIXELFORMAT_RG16SN: return MTLPixelFormatRG16Snorm; |
| 11593 | case SG_PIXELFORMAT_RG16UI: return MTLPixelFormatRG16Uint; |
| 11594 | case SG_PIXELFORMAT_RG16SI: return MTLPixelFormatRG16Sint; |
| 11595 | case SG_PIXELFORMAT_RG16F: return MTLPixelFormatRG16Float; |
| 11596 | case SG_PIXELFORMAT_RGBA8: return MTLPixelFormatRGBA8Unorm; |
| 11597 | case SG_PIXELFORMAT_SRGB8A8: return MTLPixelFormatRGBA8Unorm_sRGB; |
| 11598 | case SG_PIXELFORMAT_RGBA8SN: return MTLPixelFormatRGBA8Snorm; |
| 11599 | case SG_PIXELFORMAT_RGBA8UI: return MTLPixelFormatRGBA8Uint; |
| 11600 | case SG_PIXELFORMAT_RGBA8SI: return MTLPixelFormatRGBA8Sint; |
| 11601 | case SG_PIXELFORMAT_BGRA8: return MTLPixelFormatBGRA8Unorm; |
| 11602 | case SG_PIXELFORMAT_RGB10A2: return MTLPixelFormatRGB10A2Unorm; |
| 11603 | case SG_PIXELFORMAT_RG11B10F: return MTLPixelFormatRG11B10Float; |
| 11604 | case SG_PIXELFORMAT_RGB9E5: return MTLPixelFormatRGB9E5Float; |
| 11605 | case SG_PIXELFORMAT_RG32UI: return MTLPixelFormatRG32Uint; |
| 11606 | case SG_PIXELFORMAT_RG32SI: return MTLPixelFormatRG32Sint; |
| 11607 | case SG_PIXELFORMAT_RG32F: return MTLPixelFormatRG32Float; |
| 11608 | case SG_PIXELFORMAT_RGBA16: return MTLPixelFormatRGBA16Unorm; |
| 11609 | case SG_PIXELFORMAT_RGBA16SN: return MTLPixelFormatRGBA16Snorm; |
| 11610 | case SG_PIXELFORMAT_RGBA16UI: return MTLPixelFormatRGBA16Uint; |
| 11611 | case SG_PIXELFORMAT_RGBA16SI: return MTLPixelFormatRGBA16Sint; |
| 11612 | case SG_PIXELFORMAT_RGBA16F: return MTLPixelFormatRGBA16Float; |
| 11613 | case SG_PIXELFORMAT_RGBA32UI: return MTLPixelFormatRGBA32Uint; |
| 11614 | case SG_PIXELFORMAT_RGBA32SI: return MTLPixelFormatRGBA32Sint; |
| 11615 | case SG_PIXELFORMAT_RGBA32F: return MTLPixelFormatRGBA32Float; |
| 11616 | case SG_PIXELFORMAT_DEPTH: return MTLPixelFormatDepth32Float; |
| 11617 | case SG_PIXELFORMAT_DEPTH_STENCIL: return MTLPixelFormatDepth32Float_Stencil8; |
| 11618 | #if defined(_SG_TARGET_MACOS) |
| 11619 | case SG_PIXELFORMAT_BC1_RGBA: return MTLPixelFormatBC1_RGBA; |
| 11620 | case SG_PIXELFORMAT_BC2_RGBA: return MTLPixelFormatBC2_RGBA; |
| 11621 | case SG_PIXELFORMAT_BC3_RGBA: return MTLPixelFormatBC3_RGBA; |
| 11622 | case SG_PIXELFORMAT_BC3_SRGBA: return MTLPixelFormatBC3_RGBA_sRGB; |
| 11623 | case SG_PIXELFORMAT_BC4_R: return MTLPixelFormatBC4_RUnorm; |
| 11624 | case SG_PIXELFORMAT_BC4_RSN: return MTLPixelFormatBC4_RSnorm; |
| 11625 | case SG_PIXELFORMAT_BC5_RG: return MTLPixelFormatBC5_RGUnorm; |
| 11626 | case SG_PIXELFORMAT_BC5_RGSN: return MTLPixelFormatBC5_RGSnorm; |
| 11627 | case SG_PIXELFORMAT_BC6H_RGBF: return MTLPixelFormatBC6H_RGBFloat; |
| 11628 | case SG_PIXELFORMAT_BC6H_RGBUF: return MTLPixelFormatBC6H_RGBUfloat; |
| 11629 | case SG_PIXELFORMAT_BC7_RGBA: return MTLPixelFormatBC7_RGBAUnorm; |
| 11630 | case SG_PIXELFORMAT_BC7_SRGBA: return MTLPixelFormatBC7_RGBAUnorm_sRGB; |
| 11631 | #else |
| 11632 | case SG_PIXELFORMAT_PVRTC_RGB_2BPP: return MTLPixelFormatPVRTC_RGB_2BPP; |
| 11633 | case SG_PIXELFORMAT_PVRTC_RGB_4BPP: return MTLPixelFormatPVRTC_RGB_4BPP; |
| 11634 | case SG_PIXELFORMAT_PVRTC_RGBA_2BPP: return MTLPixelFormatPVRTC_RGBA_2BPP; |
| 11635 | case SG_PIXELFORMAT_PVRTC_RGBA_4BPP: return MTLPixelFormatPVRTC_RGBA_4BPP; |
| 11636 | case SG_PIXELFORMAT_ETC2_RGB8: return MTLPixelFormatETC2_RGB8; |
| 11637 | case SG_PIXELFORMAT_ETC2_SRGB8: return MTLPixelFormatETC2_RGB8_sRGB; |
| 11638 | case SG_PIXELFORMAT_ETC2_RGB8A1: return MTLPixelFormatETC2_RGB8A1; |
| 11639 | case SG_PIXELFORMAT_ETC2_RGBA8: return MTLPixelFormatEAC_RGBA8; |
| 11640 | case SG_PIXELFORMAT_ETC2_SRGB8A8: return MTLPixelFormatEAC_RGBA8_sRGB; |
| 11641 | case SG_PIXELFORMAT_ETC2_RG11: return MTLPixelFormatEAC_RG11Unorm; |
| 11642 | case SG_PIXELFORMAT_ETC2_RG11SN: return MTLPixelFormatEAC_RG11Snorm; |
| 11643 | case SG_PIXELFORMAT_ASTC_4x4_RGBA: return MTLPixelFormatASTC_4x4_LDR; |
| 11644 | case SG_PIXELFORMAT_ASTC_4x4_SRGBA: return MTLPixelFormatASTC_4x4_sRGB; |
| 11645 | #endif |
| 11646 | default: return MTLPixelFormatInvalid; |
| 11647 | } |
| 11648 | } |
| 11649 | |
| 11650 | _SOKOL_PRIVATE MTLColorWriteMask _sg_mtl_color_write_mask(sg_color_mask m) { |
| 11651 | MTLColorWriteMask mtl_mask = MTLColorWriteMaskNone; |
| 11652 | if (m & SG_COLORMASK_R) { |
| 11653 | mtl_mask |= MTLColorWriteMaskRed; |
| 11654 | } |
| 11655 | if (m & SG_COLORMASK_G) { |
| 11656 | mtl_mask |= MTLColorWriteMaskGreen; |
| 11657 | } |
| 11658 | if (m & SG_COLORMASK_B) { |
| 11659 | mtl_mask |= MTLColorWriteMaskBlue; |
| 11660 | } |
| 11661 | if (m & SG_COLORMASK_A) { |
| 11662 | mtl_mask |= MTLColorWriteMaskAlpha; |
| 11663 | } |
| 11664 | return mtl_mask; |
| 11665 | } |
| 11666 | |
| 11667 | _SOKOL_PRIVATE MTLBlendOperation _sg_mtl_blend_op(sg_blend_op op) { |
| 11668 | switch (op) { |
| 11669 | case SG_BLENDOP_ADD: return MTLBlendOperationAdd; |
| 11670 | case SG_BLENDOP_SUBTRACT: return MTLBlendOperationSubtract; |
| 11671 | case SG_BLENDOP_REVERSE_SUBTRACT: return MTLBlendOperationReverseSubtract; |
| 11672 | default: SOKOL_UNREACHABLE; return (MTLBlendOperation)0; |
| 11673 | } |
| 11674 | } |
| 11675 | |
| 11676 | _SOKOL_PRIVATE MTLBlendFactor _sg_mtl_blend_factor(sg_blend_factor f) { |
| 11677 | switch (f) { |
| 11678 | case SG_BLENDFACTOR_ZERO: return MTLBlendFactorZero; |
| 11679 | case SG_BLENDFACTOR_ONE: return MTLBlendFactorOne; |
| 11680 | case SG_BLENDFACTOR_SRC_COLOR: return MTLBlendFactorSourceColor; |
| 11681 | case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return MTLBlendFactorOneMinusSourceColor; |
| 11682 | case SG_BLENDFACTOR_SRC_ALPHA: return MTLBlendFactorSourceAlpha; |
| 11683 | case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return MTLBlendFactorOneMinusSourceAlpha; |
| 11684 | case SG_BLENDFACTOR_DST_COLOR: return MTLBlendFactorDestinationColor; |
| 11685 | case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return MTLBlendFactorOneMinusDestinationColor; |
| 11686 | case SG_BLENDFACTOR_DST_ALPHA: return MTLBlendFactorDestinationAlpha; |
| 11687 | case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return MTLBlendFactorOneMinusDestinationAlpha; |
| 11688 | case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return MTLBlendFactorSourceAlphaSaturated; |
| 11689 | case SG_BLENDFACTOR_BLEND_COLOR: return MTLBlendFactorBlendColor; |
| 11690 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return MTLBlendFactorOneMinusBlendColor; |
| 11691 | case SG_BLENDFACTOR_BLEND_ALPHA: return MTLBlendFactorBlendAlpha; |
| 11692 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return MTLBlendFactorOneMinusBlendAlpha; |
| 11693 | default: SOKOL_UNREACHABLE; return (MTLBlendFactor)0; |
| 11694 | } |
| 11695 | } |
| 11696 | |
| 11697 | _SOKOL_PRIVATE MTLCompareFunction _sg_mtl_compare_func(sg_compare_func f) { |
| 11698 | switch (f) { |
| 11699 | case SG_COMPAREFUNC_NEVER: return MTLCompareFunctionNever; |
| 11700 | case SG_COMPAREFUNC_LESS: return MTLCompareFunctionLess; |
| 11701 | case SG_COMPAREFUNC_EQUAL: return MTLCompareFunctionEqual; |
| 11702 | case SG_COMPAREFUNC_LESS_EQUAL: return MTLCompareFunctionLessEqual; |
| 11703 | case SG_COMPAREFUNC_GREATER: return MTLCompareFunctionGreater; |
| 11704 | case SG_COMPAREFUNC_NOT_EQUAL: return MTLCompareFunctionNotEqual; |
| 11705 | case SG_COMPAREFUNC_GREATER_EQUAL: return MTLCompareFunctionGreaterEqual; |
| 11706 | case SG_COMPAREFUNC_ALWAYS: return MTLCompareFunctionAlways; |
| 11707 | default: SOKOL_UNREACHABLE; return (MTLCompareFunction)0; |
| 11708 | } |
| 11709 | } |
| 11710 | |
| 11711 | _SOKOL_PRIVATE MTLStencilOperation _sg_mtl_stencil_op(sg_stencil_op op) { |
| 11712 | switch (op) { |
| 11713 | case SG_STENCILOP_KEEP: return MTLStencilOperationKeep; |
| 11714 | case SG_STENCILOP_ZERO: return MTLStencilOperationZero; |
| 11715 | case SG_STENCILOP_REPLACE: return MTLStencilOperationReplace; |
| 11716 | case SG_STENCILOP_INCR_CLAMP: return MTLStencilOperationIncrementClamp; |
| 11717 | case SG_STENCILOP_DECR_CLAMP: return MTLStencilOperationDecrementClamp; |
| 11718 | case SG_STENCILOP_INVERT: return MTLStencilOperationInvert; |
| 11719 | case SG_STENCILOP_INCR_WRAP: return MTLStencilOperationIncrementWrap; |
| 11720 | case SG_STENCILOP_DECR_WRAP: return MTLStencilOperationDecrementWrap; |
| 11721 | default: SOKOL_UNREACHABLE; return (MTLStencilOperation)0; |
| 11722 | } |
| 11723 | } |
| 11724 | |
| 11725 | _SOKOL_PRIVATE MTLCullMode _sg_mtl_cull_mode(sg_cull_mode m) { |
| 11726 | switch (m) { |
| 11727 | case SG_CULLMODE_NONE: return MTLCullModeNone; |
| 11728 | case SG_CULLMODE_FRONT: return MTLCullModeFront; |
| 11729 | case SG_CULLMODE_BACK: return MTLCullModeBack; |
| 11730 | default: SOKOL_UNREACHABLE; return (MTLCullMode)0; |
| 11731 | } |
| 11732 | } |
| 11733 | |
| 11734 | _SOKOL_PRIVATE MTLWinding _sg_mtl_winding(sg_face_winding w) { |
| 11735 | switch (w) { |
| 11736 | case SG_FACEWINDING_CW: return MTLWindingClockwise; |
| 11737 | case SG_FACEWINDING_CCW: return MTLWindingCounterClockwise; |
| 11738 | default: SOKOL_UNREACHABLE; return (MTLWinding)0; |
| 11739 | } |
| 11740 | } |
| 11741 | |
| 11742 | _SOKOL_PRIVATE MTLIndexType _sg_mtl_index_type(sg_index_type t) { |
| 11743 | switch (t) { |
| 11744 | case SG_INDEXTYPE_UINT16: return MTLIndexTypeUInt16; |
| 11745 | case SG_INDEXTYPE_UINT32: return MTLIndexTypeUInt32; |
| 11746 | default: SOKOL_UNREACHABLE; return (MTLIndexType)0; |
| 11747 | } |
| 11748 | } |
| 11749 | |
| 11750 | _SOKOL_PRIVATE int _sg_mtl_index_size(sg_index_type t) { |
| 11751 | switch (t) { |
| 11752 | case SG_INDEXTYPE_NONE: return 0; |
| 11753 | case SG_INDEXTYPE_UINT16: return 2; |
| 11754 | case SG_INDEXTYPE_UINT32: return 4; |
| 11755 | default: SOKOL_UNREACHABLE; return 0; |
| 11756 | } |
| 11757 | } |
| 11758 | |
| 11759 | _SOKOL_PRIVATE MTLTextureType _sg_mtl_texture_type(sg_image_type t) { |
| 11760 | switch (t) { |
| 11761 | case SG_IMAGETYPE_2D: return MTLTextureType2D; |
| 11762 | case SG_IMAGETYPE_CUBE: return MTLTextureTypeCube; |
| 11763 | case SG_IMAGETYPE_3D: return MTLTextureType3D; |
| 11764 | case SG_IMAGETYPE_ARRAY: return MTLTextureType2DArray; |
| 11765 | default: SOKOL_UNREACHABLE; return (MTLTextureType)0; |
| 11766 | } |
| 11767 | } |
| 11768 | |
| 11769 | _SOKOL_PRIVATE bool _sg_mtl_is_pvrtc(sg_pixel_format fmt) { |
| 11770 | switch (fmt) { |
| 11771 | case SG_PIXELFORMAT_PVRTC_RGB_2BPP: |
| 11772 | case SG_PIXELFORMAT_PVRTC_RGB_4BPP: |
| 11773 | case SG_PIXELFORMAT_PVRTC_RGBA_2BPP: |
| 11774 | case SG_PIXELFORMAT_PVRTC_RGBA_4BPP: |
| 11775 | return true; |
| 11776 | default: |
| 11777 | return false; |
| 11778 | } |
| 11779 | } |
| 11780 | |
| 11781 | _SOKOL_PRIVATE MTLSamplerAddressMode _sg_mtl_address_mode(sg_wrap w) { |
| 11782 | if (_sg.features.image_clamp_to_border) { |
| 11783 | if (@available(macOS 12.0, iOS 14.0, *)) { |
| 11784 | // border color feature available |
| 11785 | switch (w) { |
| 11786 | case SG_WRAP_REPEAT: return MTLSamplerAddressModeRepeat; |
| 11787 | case SG_WRAP_CLAMP_TO_EDGE: return MTLSamplerAddressModeClampToEdge; |
| 11788 | case SG_WRAP_CLAMP_TO_BORDER: return MTLSamplerAddressModeClampToBorderColor; |
| 11789 | case SG_WRAP_MIRRORED_REPEAT: return MTLSamplerAddressModeMirrorRepeat; |
| 11790 | default: SOKOL_UNREACHABLE; return (MTLSamplerAddressMode)0; |
| 11791 | } |
| 11792 | } |
| 11793 | } |
| 11794 | // fallthrough: clamp to border no supported |
| 11795 | switch (w) { |
| 11796 | case SG_WRAP_REPEAT: return MTLSamplerAddressModeRepeat; |
| 11797 | case SG_WRAP_CLAMP_TO_EDGE: return MTLSamplerAddressModeClampToEdge; |
| 11798 | case SG_WRAP_CLAMP_TO_BORDER: return MTLSamplerAddressModeClampToEdge; |
| 11799 | case SG_WRAP_MIRRORED_REPEAT: return MTLSamplerAddressModeMirrorRepeat; |
| 11800 | default: SOKOL_UNREACHABLE; return (MTLSamplerAddressMode)0; |
| 11801 | } |
| 11802 | } |
| 11803 | |
| 11804 | _SOKOL_PRIVATE API_AVAILABLE(ios(14.0), macos(12.0)) MTLSamplerBorderColor _sg_mtl_border_color(sg_border_color c) { |
| 11805 | switch (c) { |
| 11806 | case SG_BORDERCOLOR_TRANSPARENT_BLACK: return MTLSamplerBorderColorTransparentBlack; |
| 11807 | case SG_BORDERCOLOR_OPAQUE_BLACK: return MTLSamplerBorderColorOpaqueBlack; |
| 11808 | case SG_BORDERCOLOR_OPAQUE_WHITE: return MTLSamplerBorderColorOpaqueWhite; |
| 11809 | default: SOKOL_UNREACHABLE; return (MTLSamplerBorderColor)0; |
| 11810 | } |
| 11811 | } |
| 11812 | |
| 11813 | _SOKOL_PRIVATE MTLSamplerMinMagFilter _sg_mtl_minmag_filter(sg_filter f) { |
| 11814 | switch (f) { |
| 11815 | case SG_FILTER_NEAREST: |
| 11816 | return MTLSamplerMinMagFilterNearest; |
| 11817 | case SG_FILTER_LINEAR: |
| 11818 | return MTLSamplerMinMagFilterLinear; |
| 11819 | default: |
| 11820 | SOKOL_UNREACHABLE; return (MTLSamplerMinMagFilter)0; |
| 11821 | } |
| 11822 | } |
| 11823 | |
| 11824 | _SOKOL_PRIVATE MTLSamplerMipFilter _sg_mtl_mipmap_filter(sg_filter f) { |
| 11825 | switch (f) { |
| 11826 | case SG_FILTER_NONE: |
| 11827 | return MTLSamplerMipFilterNotMipmapped; |
| 11828 | case SG_FILTER_NEAREST: |
| 11829 | return MTLSamplerMipFilterNearest; |
| 11830 | case SG_FILTER_LINEAR: |
| 11831 | return MTLSamplerMipFilterLinear; |
| 11832 | default: |
| 11833 | SOKOL_UNREACHABLE; return (MTLSamplerMipFilter)0; |
| 11834 | } |
| 11835 | } |
| 11836 | |
| 11837 | //-- a pool for all Metal resource objects, with deferred release queue --------- |
| 11838 | _SOKOL_PRIVATE void _sg_mtl_init_pool(const sg_desc* desc) { |
| 11839 | _sg.mtl.idpool.num_slots = 2 * |
| 11840 | ( |
| 11841 | 2 * desc->buffer_pool_size + |
| 11842 | 4 * desc->image_pool_size + |
| 11843 | 1 * desc->sampler_pool_size + |
| 11844 | 4 * desc->shader_pool_size + |
| 11845 | 2 * desc->pipeline_pool_size + |
| 11846 | desc->attachments_pool_size + |
| 11847 | 128 |
| 11848 | ); |
| 11849 | _sg.mtl.idpool.pool = [NSMutableArray arrayWithCapacity:(NSUInteger)_sg.mtl.idpool.num_slots]; |
| 11850 | _SG_OBJC_RETAIN(_sg.mtl.idpool.pool); |
| 11851 | NSNull* null = [NSNull null]; |
| 11852 | for (int i = 0; i < _sg.mtl.idpool.num_slots; i++) { |
| 11853 | [_sg.mtl.idpool.pool addObject:null]; |
| 11854 | } |
| 11855 | SOKOL_ASSERT([_sg.mtl.idpool.pool count] == (NSUInteger)_sg.mtl.idpool.num_slots); |
| 11856 | // a queue of currently free slot indices |
| 11857 | _sg.mtl.idpool.free_queue_top = 0; |
| 11858 | _sg.mtl.idpool.free_queue = (int*)_sg_malloc_clear((size_t)_sg.mtl.idpool.num_slots * sizeof(int)); |
| 11859 | // pool slot 0 is reserved! |
| 11860 | for (int i = _sg.mtl.idpool.num_slots-1; i >= 1; i--) { |
| 11861 | _sg.mtl.idpool.free_queue[_sg.mtl.idpool.free_queue_top++] = i; |
| 11862 | } |
| 11863 | // a circular queue which holds release items (frame index when a resource is to be released, and the resource's pool index |
| 11864 | _sg.mtl.idpool.release_queue_front = 0; |
| 11865 | _sg.mtl.idpool.release_queue_back = 0; |
| 11866 | _sg.mtl.idpool.release_queue = (_sg_mtl_release_item_t*)_sg_malloc_clear((size_t)_sg.mtl.idpool.num_slots * sizeof(_sg_mtl_release_item_t)); |
| 11867 | for (int i = 0; i < _sg.mtl.idpool.num_slots; i++) { |
| 11868 | _sg.mtl.idpool.release_queue[i].frame_index = 0; |
| 11869 | _sg.mtl.idpool.release_queue[i].slot_index = _SG_MTL_INVALID_SLOT_INDEX; |
| 11870 | } |
| 11871 | } |
| 11872 | |
| 11873 | _SOKOL_PRIVATE void _sg_mtl_destroy_pool(void) { |
| 11874 | _sg_free(_sg.mtl.idpool.release_queue); _sg.mtl.idpool.release_queue = 0; |
| 11875 | _sg_free(_sg.mtl.idpool.free_queue); _sg.mtl.idpool.free_queue = 0; |
| 11876 | _SG_OBJC_RELEASE(_sg.mtl.idpool.pool); |
| 11877 | } |
| 11878 | |
| 11879 | // get a new free resource pool slot |
| 11880 | _SOKOL_PRIVATE int _sg_mtl_alloc_pool_slot(void) { |
| 11881 | SOKOL_ASSERT(_sg.mtl.idpool.free_queue_top > 0); |
| 11882 | const int slot_index = _sg.mtl.idpool.free_queue[--_sg.mtl.idpool.free_queue_top]; |
| 11883 | SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots)); |
| 11884 | return slot_index; |
| 11885 | } |
| 11886 | |
| 11887 | // put a free resource pool slot back into the free-queue |
| 11888 | _SOKOL_PRIVATE void _sg_mtl_free_pool_slot(int slot_index) { |
| 11889 | SOKOL_ASSERT(_sg.mtl.idpool.free_queue_top < _sg.mtl.idpool.num_slots); |
| 11890 | SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots)); |
| 11891 | _sg.mtl.idpool.free_queue[_sg.mtl.idpool.free_queue_top++] = slot_index; |
| 11892 | } |
| 11893 | |
| 11894 | // add an MTLResource to the pool, return pool index or 0 if input was 'nil' |
| 11895 | _SOKOL_PRIVATE int _sg_mtl_add_resource(id res) { |
| 11896 | if (nil == res) { |
| 11897 | return _SG_MTL_INVALID_SLOT_INDEX; |
| 11898 | } |
| 11899 | _sg_stats_add(metal.idpool.num_added, 1); |
| 11900 | const int slot_index = _sg_mtl_alloc_pool_slot(); |
| 11901 | // NOTE: the NSMutableArray will take ownership of its items |
| 11902 | SOKOL_ASSERT([NSNull null] == _sg.mtl.idpool.pool[(NSUInteger)slot_index]); |
| 11903 | _sg.mtl.idpool.pool[(NSUInteger)slot_index] = res; |
| 11904 | return slot_index; |
| 11905 | } |
| 11906 | |
| 11907 | /* mark an MTLResource for release, this will put the resource into the |
| 11908 | deferred-release queue, and the resource will then be released N frames later, |
| 11909 | the special pool index 0 will be ignored (this means that a nil |
| 11910 | value was provided to _sg_mtl_add_resource() |
| 11911 | */ |
| 11912 | _SOKOL_PRIVATE void _sg_mtl_release_resource(uint32_t frame_index, int slot_index) { |
| 11913 | if (slot_index == _SG_MTL_INVALID_SLOT_INDEX) { |
| 11914 | return; |
| 11915 | } |
| 11916 | _sg_stats_add(metal.idpool.num_released, 1); |
| 11917 | SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots)); |
| 11918 | SOKOL_ASSERT([NSNull null] != _sg.mtl.idpool.pool[(NSUInteger)slot_index]); |
| 11919 | int release_index = _sg.mtl.idpool.release_queue_front++; |
| 11920 | if (_sg.mtl.idpool.release_queue_front >= _sg.mtl.idpool.num_slots) { |
| 11921 | // wrap-around |
| 11922 | _sg.mtl.idpool.release_queue_front = 0; |
| 11923 | } |
| 11924 | // release queue full? |
| 11925 | SOKOL_ASSERT(_sg.mtl.idpool.release_queue_front != _sg.mtl.idpool.release_queue_back); |
| 11926 | SOKOL_ASSERT(0 == _sg.mtl.idpool.release_queue[release_index].frame_index); |
| 11927 | const uint32_t safe_to_release_frame_index = frame_index + SG_NUM_INFLIGHT_FRAMES + 1; |
| 11928 | _sg.mtl.idpool.release_queue[release_index].frame_index = safe_to_release_frame_index; |
| 11929 | _sg.mtl.idpool.release_queue[release_index].slot_index = slot_index; |
| 11930 | } |
| 11931 | |
| 11932 | // run garbage-collection pass on all resources in the release-queue |
| 11933 | _SOKOL_PRIVATE void _sg_mtl_garbage_collect(uint32_t frame_index) { |
| 11934 | while (_sg.mtl.idpool.release_queue_back != _sg.mtl.idpool.release_queue_front) { |
| 11935 | if (frame_index < _sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].frame_index) { |
| 11936 | // don't need to check further, release-items past this are too young |
| 11937 | break; |
| 11938 | } |
| 11939 | _sg_stats_add(metal.idpool.num_garbage_collected, 1); |
| 11940 | // safe to release this resource |
| 11941 | const int slot_index = _sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].slot_index; |
| 11942 | SOKOL_ASSERT((slot_index > 0) && (slot_index < _sg.mtl.idpool.num_slots)); |
| 11943 | // note: the NSMutableArray takes ownership of its items, assigning an NSNull object will |
| 11944 | // release the object, no matter if using ARC or not |
| 11945 | SOKOL_ASSERT(_sg.mtl.idpool.pool[(NSUInteger)slot_index] != [NSNull null]); |
| 11946 | _sg.mtl.idpool.pool[(NSUInteger)slot_index] = [NSNull null]; |
| 11947 | // put the now free pool index back on the free queue |
| 11948 | _sg_mtl_free_pool_slot(slot_index); |
| 11949 | // reset the release queue slot and advance the back index |
| 11950 | _sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].frame_index = 0; |
| 11951 | _sg.mtl.idpool.release_queue[_sg.mtl.idpool.release_queue_back].slot_index = _SG_MTL_INVALID_SLOT_INDEX; |
| 11952 | _sg.mtl.idpool.release_queue_back++; |
| 11953 | if (_sg.mtl.idpool.release_queue_back >= _sg.mtl.idpool.num_slots) { |
| 11954 | // wrap-around |
| 11955 | _sg.mtl.idpool.release_queue_back = 0; |
| 11956 | } |
| 11957 | } |
| 11958 | } |
| 11959 | |
| 11960 | _SOKOL_PRIVATE id _sg_mtl_id(int slot_index) { |
| 11961 | return _sg.mtl.idpool.pool[(NSUInteger)slot_index]; |
| 11962 | } |
| 11963 | |
| 11964 | _SOKOL_PRIVATE void _sg_mtl_clear_state_cache(void) { |
| 11965 | _sg_clear(&_sg.mtl.state_cache, sizeof(_sg.mtl.state_cache)); |
| 11966 | } |
| 11967 | |
| 11968 | // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf |
| 11969 | _SOKOL_PRIVATE void _sg_mtl_init_caps(void) { |
| 11970 | #if defined(_SG_TARGET_MACOS) |
| 11971 | _sg.backend = SG_BACKEND_METAL_MACOS; |
| 11972 | #elif defined(_SG_TARGET_IOS) |
| 11973 | #if defined(_SG_TARGET_IOS_SIMULATOR) |
| 11974 | _sg.backend = SG_BACKEND_METAL_SIMULATOR; |
| 11975 | #else |
| 11976 | _sg.backend = SG_BACKEND_METAL_IOS; |
| 11977 | #endif |
| 11978 | #endif |
| 11979 | _sg.features.origin_top_left = true; |
| 11980 | _sg.features.mrt_independent_blend_state = true; |
| 11981 | _sg.features.mrt_independent_write_mask = true; |
| 11982 | _sg.features.storage_buffer = true; |
| 11983 | |
| 11984 | _sg.features.image_clamp_to_border = false; |
| 11985 | #if (MAC_OS_X_VERSION_MAX_ALLOWED >= 120000) || (__IPHONE_OS_VERSION_MAX_ALLOWED >= 140000) |
| 11986 | if (@available(macOS 12.0, iOS 14.0, *)) { |
| 11987 | _sg.features.image_clamp_to_border = [_sg.mtl.device supportsFamily:MTLGPUFamilyApple7] |
| 11988 | || [_sg.mtl.device supportsFamily:MTLGPUFamilyMac2]; |
| 11989 | #if (MAC_OS_X_VERSION_MAX_ALLOWED >= 130000) || (__IPHONE_OS_VERSION_MAX_ALLOWED >= 160000) |
| 11990 | if (!_sg.features.image_clamp_to_border) { |
| 11991 | if (@available(macOS 13.0, iOS 16.0, *)) { |
| 11992 | _sg.features.image_clamp_to_border = [_sg.mtl.device supportsFamily:MTLGPUFamilyMetal3]; |
| 11993 | } |
| 11994 | } |
| 11995 | #endif |
| 11996 | } |
| 11997 | #endif |
| 11998 | |
| 11999 | #if defined(_SG_TARGET_MACOS) |
| 12000 | _sg.limits.max_image_size_2d = 16 * 1024; |
| 12001 | _sg.limits.max_image_size_cube = 16 * 1024; |
| 12002 | _sg.limits.max_image_size_3d = 2 * 1024; |
| 12003 | _sg.limits.max_image_size_array = 16 * 1024; |
| 12004 | _sg.limits.max_image_array_layers = 2 * 1024; |
| 12005 | #else |
| 12006 | // FIXME: newer iOS devices support 16k textures |
| 12007 | _sg.limits.max_image_size_2d = 8 * 1024; |
| 12008 | _sg.limits.max_image_size_cube = 8 * 1024; |
| 12009 | _sg.limits.max_image_size_3d = 2 * 1024; |
| 12010 | _sg.limits.max_image_size_array = 8 * 1024; |
| 12011 | _sg.limits.max_image_array_layers = 2 * 1024; |
| 12012 | #endif |
| 12013 | _sg.limits.max_vertex_attrs = SG_MAX_VERTEX_ATTRIBUTES; |
| 12014 | |
| 12015 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R8]); |
| 12016 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R8SN]); |
| 12017 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8UI]); |
| 12018 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R8SI]); |
| 12019 | #if defined(_SG_TARGET_MACOS) |
| 12020 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16]); |
| 12021 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16SN]); |
| 12022 | #else |
| 12023 | _sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_R16]); |
| 12024 | _sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_R16SN]); |
| 12025 | #endif |
| 12026 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16UI]); |
| 12027 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_R16SI]); |
| 12028 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16F]); |
| 12029 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG8]); |
| 12030 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG8SN]); |
| 12031 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8UI]); |
| 12032 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG8SI]); |
| 12033 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32UI]); |
| 12034 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32SI]); |
| 12035 | #if defined(_SG_TARGET_MACOS) |
| 12036 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R32F]); |
| 12037 | #else |
| 12038 | _sg_pixelformat_sbr(&_sg.formats[SG_PIXELFORMAT_R32F]); |
| 12039 | #endif |
| 12040 | #if defined(_SG_TARGET_MACOS) |
| 12041 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16]); |
| 12042 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16SN]); |
| 12043 | #else |
| 12044 | _sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_RG16]); |
| 12045 | _sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_RG16SN]); |
| 12046 | #endif |
| 12047 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16UI]); |
| 12048 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG16SI]); |
| 12049 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16F]); |
| 12050 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA8]); |
| 12051 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_SRGB8A8]); |
| 12052 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA8SN]); |
| 12053 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8UI]); |
| 12054 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA8SI]); |
| 12055 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_BGRA8]); |
| 12056 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGB10A2]); |
| 12057 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG11B10F]); |
| 12058 | #if defined(_SG_TARGET_MACOS) |
| 12059 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGB9E5]); |
| 12060 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG32UI]); |
| 12061 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RG32SI]); |
| 12062 | #else |
| 12063 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGB9E5]); |
| 12064 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32UI]); |
| 12065 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32SI]); |
| 12066 | #endif |
| 12067 | #if defined(_SG_TARGET_MACOS) |
| 12068 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG32F]); |
| 12069 | #else |
| 12070 | _sg_pixelformat_sbr(&_sg.formats[SG_PIXELFORMAT_RG32F]); |
| 12071 | #endif |
| 12072 | #if defined(_SG_TARGET_MACOS) |
| 12073 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16]); |
| 12074 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16SN]); |
| 12075 | #else |
| 12076 | _sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_RGBA16]); |
| 12077 | _sg_pixelformat_sfbr(&_sg.formats[SG_PIXELFORMAT_RGBA16SN]); |
| 12078 | #endif |
| 12079 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16UI]); |
| 12080 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA16SI]); |
| 12081 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16F]); |
| 12082 | #if defined(_SG_TARGET_MACOS) |
| 12083 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA32UI]); |
| 12084 | _sg_pixelformat_srm(&_sg.formats[SG_PIXELFORMAT_RGBA32SI]); |
| 12085 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA32F]); |
| 12086 | #else |
| 12087 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32UI]); |
| 12088 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32SI]); |
| 12089 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32F]); |
| 12090 | #endif |
| 12091 | _sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH]); |
| 12092 | _sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH_STENCIL]); |
| 12093 | #if defined(_SG_TARGET_MACOS) |
| 12094 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC1_RGBA]); |
| 12095 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC2_RGBA]); |
| 12096 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_RGBA]); |
| 12097 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_SRGBA]); |
| 12098 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_R]); |
| 12099 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_RSN]); |
| 12100 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RG]); |
| 12101 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RGSN]); |
| 12102 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBF]); |
| 12103 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBUF]); |
| 12104 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_RGBA]); |
| 12105 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_SRGBA]); |
| 12106 | #else |
| 12107 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGB_2BPP]); |
| 12108 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGB_4BPP]); |
| 12109 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGBA_2BPP]); |
| 12110 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_PVRTC_RGBA_4BPP]); |
| 12111 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8]); |
| 12112 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_SRGB8]); |
| 12113 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8A1]); |
| 12114 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGBA8]); |
| 12115 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_SRGB8A8]); |
| 12116 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RG11]); |
| 12117 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RG11SN]); |
| 12118 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ASTC_4x4_RGBA]); |
| 12119 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ASTC_4x4_SRGBA]); |
| 12120 | |
| 12121 | #endif |
| 12122 | } |
| 12123 | |
| 12124 | //-- main Metal backend state and functions ------------------------------------ |
| 12125 | _SOKOL_PRIVATE void _sg_mtl_setup_backend(const sg_desc* desc) { |
| 12126 | // assume already zero-initialized |
| 12127 | SOKOL_ASSERT(desc); |
| 12128 | SOKOL_ASSERT(desc->environment.metal.device); |
| 12129 | SOKOL_ASSERT(desc->uniform_buffer_size > 0); |
| 12130 | _sg_mtl_init_pool(desc); |
| 12131 | _sg_mtl_clear_state_cache(); |
| 12132 | _sg.mtl.valid = true; |
| 12133 | _sg.mtl.ub_size = desc->uniform_buffer_size; |
| 12134 | _sg.mtl.sem = dispatch_semaphore_create(SG_NUM_INFLIGHT_FRAMES); |
| 12135 | _sg.mtl.device = (__bridge id<MTLDevice>) desc->environment.metal.device; |
| 12136 | _sg.mtl.cmd_queue = [_sg.mtl.device newCommandQueue]; |
| 12137 | |
| 12138 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 12139 | _sg.mtl.uniform_buffers[i] = [_sg.mtl.device |
| 12140 | newBufferWithLength:(NSUInteger)_sg.mtl.ub_size |
| 12141 | options:MTLResourceCPUCacheModeWriteCombined|MTLResourceStorageModeShared |
| 12142 | ]; |
| 12143 | #if defined(SOKOL_DEBUG) |
| 12144 | _sg.mtl.uniform_buffers[i].label = [NSString stringWithFormat:@"sg-uniform-buffer.%d", i]; |
| 12145 | #endif |
| 12146 | } |
| 12147 | |
| 12148 | if (desc->mtl_force_managed_storage_mode) { |
| 12149 | _sg.mtl.use_shared_storage_mode = false; |
| 12150 | } else if (@available(macOS 10.15, iOS 13.0, *)) { |
| 12151 | // on Intel Macs, always use managed resources even though the |
| 12152 | // device says it supports unified memory (because of texture restrictions) |
| 12153 | const bool is_apple_gpu = [_sg.mtl.device supportsFamily:MTLGPUFamilyApple1]; |
| 12154 | if (!is_apple_gpu) { |
| 12155 | _sg.mtl.use_shared_storage_mode = false; |
| 12156 | } else { |
| 12157 | _sg.mtl.use_shared_storage_mode = true; |
| 12158 | } |
| 12159 | } else { |
| 12160 | #if defined(_SG_TARGET_MACOS) |
| 12161 | _sg.mtl.use_shared_storage_mode = false; |
| 12162 | #else |
| 12163 | _sg.mtl.use_shared_storage_mode = true; |
| 12164 | #endif |
| 12165 | } |
| 12166 | _sg_mtl_init_caps(); |
| 12167 | } |
| 12168 | |
| 12169 | _SOKOL_PRIVATE void _sg_mtl_discard_backend(void) { |
| 12170 | SOKOL_ASSERT(_sg.mtl.valid); |
| 12171 | // wait for the last frame to finish |
| 12172 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 12173 | dispatch_semaphore_wait(_sg.mtl.sem, DISPATCH_TIME_FOREVER); |
| 12174 | } |
| 12175 | // semaphore must be "relinquished" before destruction |
| 12176 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 12177 | dispatch_semaphore_signal(_sg.mtl.sem); |
| 12178 | } |
| 12179 | _sg_mtl_garbage_collect(_sg.frame_index + SG_NUM_INFLIGHT_FRAMES + 2); |
| 12180 | _sg_mtl_destroy_pool(); |
| 12181 | _sg.mtl.valid = false; |
| 12182 | |
| 12183 | _SG_OBJC_RELEASE(_sg.mtl.sem); |
| 12184 | _SG_OBJC_RELEASE(_sg.mtl.device); |
| 12185 | _SG_OBJC_RELEASE(_sg.mtl.cmd_queue); |
| 12186 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 12187 | _SG_OBJC_RELEASE(_sg.mtl.uniform_buffers[i]); |
| 12188 | } |
| 12189 | // NOTE: MTLCommandBuffer and MTLRenderCommandEncoder are auto-released |
| 12190 | _sg.mtl.cmd_buffer = nil; |
| 12191 | _sg.mtl.cmd_encoder = nil; |
| 12192 | } |
| 12193 | |
| 12194 | _SOKOL_PRIVATE void _sg_mtl_bind_uniform_buffers(void) { |
| 12195 | SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder); |
| 12196 | for (int slot = 0; slot < SG_MAX_SHADERSTAGE_UBS; slot++) { |
| 12197 | [_sg.mtl.cmd_encoder |
| 12198 | setVertexBuffer:_sg.mtl.uniform_buffers[_sg.mtl.cur_frame_rotate_index] |
| 12199 | offset:0 |
| 12200 | atIndex:(NSUInteger)slot]; |
| 12201 | [_sg.mtl.cmd_encoder |
| 12202 | setFragmentBuffer:_sg.mtl.uniform_buffers[_sg.mtl.cur_frame_rotate_index] |
| 12203 | offset:0 |
| 12204 | atIndex:(NSUInteger)slot]; |
| 12205 | } |
| 12206 | } |
| 12207 | |
| 12208 | _SOKOL_PRIVATE void _sg_mtl_reset_state_cache(void) { |
| 12209 | _sg_mtl_clear_state_cache(); |
| 12210 | // need to restore the uniform buffer binding (normally happens in _sg_mtl_begin_pass() |
| 12211 | if (nil != _sg.mtl.cmd_encoder) { |
| 12212 | _sg_mtl_bind_uniform_buffers(); |
| 12213 | } |
| 12214 | } |
| 12215 | |
| 12216 | _SOKOL_PRIVATE sg_resource_state _sg_mtl_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) { |
| 12217 | SOKOL_ASSERT(buf && desc); |
| 12218 | const bool injected = (0 != desc->mtl_buffers[0]); |
| 12219 | MTLResourceOptions mtl_options = _sg_mtl_buffer_resource_options(buf->cmn.usage); |
| 12220 | for (int slot = 0; slot < buf->cmn.num_slots; slot++) { |
| 12221 | id<MTLBuffer> mtl_buf; |
| 12222 | if (injected) { |
| 12223 | SOKOL_ASSERT(desc->mtl_buffers[slot]); |
| 12224 | mtl_buf = (__bridge id<MTLBuffer>) desc->mtl_buffers[slot]; |
| 12225 | } else { |
| 12226 | if (buf->cmn.usage == SG_USAGE_IMMUTABLE) { |
| 12227 | SOKOL_ASSERT(desc->data.ptr); |
| 12228 | mtl_buf = [_sg.mtl.device newBufferWithBytes:desc->data.ptr length:(NSUInteger)buf->cmn.size options:mtl_options]; |
| 12229 | } else { |
| 12230 | mtl_buf = [_sg.mtl.device newBufferWithLength:(NSUInteger)buf->cmn.size options:mtl_options]; |
| 12231 | } |
| 12232 | if (nil == mtl_buf) { |
| 12233 | _SG_ERROR(METAL_CREATE_BUFFER_FAILED); |
| 12234 | return SG_RESOURCESTATE_FAILED; |
| 12235 | } |
| 12236 | } |
| 12237 | #if defined(SOKOL_DEBUG) |
| 12238 | if (desc->label) { |
| 12239 | mtl_buf.label = [NSString stringWithFormat:@"%s.%d", desc->label, slot]; |
| 12240 | } |
| 12241 | #endif |
| 12242 | buf->mtl.buf[slot] = _sg_mtl_add_resource(mtl_buf); |
| 12243 | _SG_OBJC_RELEASE(mtl_buf); |
| 12244 | } |
| 12245 | return SG_RESOURCESTATE_VALID; |
| 12246 | } |
| 12247 | |
| 12248 | _SOKOL_PRIVATE void _sg_mtl_discard_buffer(_sg_buffer_t* buf) { |
| 12249 | SOKOL_ASSERT(buf); |
| 12250 | for (int slot = 0; slot < buf->cmn.num_slots; slot++) { |
| 12251 | // it's valid to call release resource with '0' |
| 12252 | _sg_mtl_release_resource(_sg.frame_index, buf->mtl.buf[slot]); |
| 12253 | } |
| 12254 | } |
| 12255 | |
| 12256 | _SOKOL_PRIVATE void _sg_mtl_copy_image_data(const _sg_image_t* img, __unsafe_unretained id<MTLTexture> mtl_tex, const sg_image_data* data) { |
| 12257 | const int num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6:1; |
| 12258 | const int num_slices = (img->cmn.type == SG_IMAGETYPE_ARRAY) ? img->cmn.num_slices : 1; |
| 12259 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 12260 | for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++) { |
| 12261 | SOKOL_ASSERT(data->subimage[face_index][mip_index].ptr); |
| 12262 | SOKOL_ASSERT(data->subimage[face_index][mip_index].size > 0); |
| 12263 | const uint8_t* data_ptr = (const uint8_t*)data->subimage[face_index][mip_index].ptr; |
| 12264 | const int mip_width = _sg_miplevel_dim(img->cmn.width, mip_index); |
| 12265 | const int mip_height = _sg_miplevel_dim(img->cmn.height, mip_index); |
| 12266 | // special case PVRTC formats: bytePerRow and bytesPerImage must be 0 |
| 12267 | int bytes_per_row = 0; |
| 12268 | int bytes_per_slice = 0; |
| 12269 | if (!_sg_mtl_is_pvrtc(img->cmn.pixel_format)) { |
| 12270 | bytes_per_row = _sg_row_pitch(img->cmn.pixel_format, mip_width, 1); |
| 12271 | bytes_per_slice = _sg_surface_pitch(img->cmn.pixel_format, mip_width, mip_height, 1); |
| 12272 | } |
| 12273 | /* bytesPerImage special case: https://developer.apple.com/documentation/metal/mtltexture/1515679-replaceregion |
| 12274 | |
| 12275 | "Supply a nonzero value only when you copy data to a MTLTextureType3D type texture" |
| 12276 | */ |
| 12277 | MTLRegion region; |
| 12278 | int bytes_per_image; |
| 12279 | if (img->cmn.type == SG_IMAGETYPE_3D) { |
| 12280 | const int mip_depth = _sg_miplevel_dim(img->cmn.num_slices, mip_index); |
| 12281 | region = MTLRegionMake3D(0, 0, 0, (NSUInteger)mip_width, (NSUInteger)mip_height, (NSUInteger)mip_depth); |
| 12282 | bytes_per_image = bytes_per_slice; |
| 12283 | // FIXME: apparently the minimal bytes_per_image size for 3D texture is 4 KByte... somehow need to handle this |
| 12284 | } else { |
| 12285 | region = MTLRegionMake2D(0, 0, (NSUInteger)mip_width, (NSUInteger)mip_height); |
| 12286 | bytes_per_image = 0; |
| 12287 | } |
| 12288 | |
| 12289 | for (int slice_index = 0; slice_index < num_slices; slice_index++) { |
| 12290 | const int mtl_slice_index = (img->cmn.type == SG_IMAGETYPE_CUBE) ? face_index : slice_index; |
| 12291 | const int slice_offset = slice_index * bytes_per_slice; |
| 12292 | SOKOL_ASSERT((slice_offset + bytes_per_slice) <= (int)data->subimage[face_index][mip_index].size); |
| 12293 | [mtl_tex replaceRegion:region |
| 12294 | mipmapLevel:(NSUInteger)mip_index |
| 12295 | slice:(NSUInteger)mtl_slice_index |
| 12296 | withBytes:data_ptr + slice_offset |
| 12297 | bytesPerRow:(NSUInteger)bytes_per_row |
| 12298 | bytesPerImage:(NSUInteger)bytes_per_image]; |
| 12299 | } |
| 12300 | } |
| 12301 | } |
| 12302 | } |
| 12303 | |
| 12304 | // initialize MTLTextureDescriptor with common attributes |
| 12305 | _SOKOL_PRIVATE bool _sg_mtl_init_texdesc_common(MTLTextureDescriptor* mtl_desc, _sg_image_t* img) { |
| 12306 | mtl_desc.textureType = _sg_mtl_texture_type(img->cmn.type); |
| 12307 | mtl_desc.pixelFormat = _sg_mtl_pixel_format(img->cmn.pixel_format); |
| 12308 | if (MTLPixelFormatInvalid == mtl_desc.pixelFormat) { |
| 12309 | _SG_ERROR(METAL_TEXTURE_FORMAT_NOT_SUPPORTED); |
| 12310 | return false; |
| 12311 | } |
| 12312 | mtl_desc.width = (NSUInteger)img->cmn.width; |
| 12313 | mtl_desc.height = (NSUInteger)img->cmn.height; |
| 12314 | if (SG_IMAGETYPE_3D == img->cmn.type) { |
| 12315 | mtl_desc.depth = (NSUInteger)img->cmn.num_slices; |
| 12316 | } else { |
| 12317 | mtl_desc.depth = 1; |
| 12318 | } |
| 12319 | mtl_desc.mipmapLevelCount = (NSUInteger)img->cmn.num_mipmaps; |
| 12320 | if (SG_IMAGETYPE_ARRAY == img->cmn.type) { |
| 12321 | mtl_desc.arrayLength = (NSUInteger)img->cmn.num_slices; |
| 12322 | } else { |
| 12323 | mtl_desc.arrayLength = 1; |
| 12324 | } |
| 12325 | mtl_desc.usage = MTLTextureUsageShaderRead; |
| 12326 | MTLResourceOptions res_options = 0; |
| 12327 | if (img->cmn.usage != SG_USAGE_IMMUTABLE) { |
| 12328 | res_options |= MTLResourceCPUCacheModeWriteCombined; |
| 12329 | } |
| 12330 | res_options |= _sg_mtl_resource_options_storage_mode_managed_or_shared(); |
| 12331 | mtl_desc.resourceOptions = res_options; |
| 12332 | return true; |
| 12333 | } |
| 12334 | |
| 12335 | // initialize MTLTextureDescriptor with rendertarget attributes |
| 12336 | _SOKOL_PRIVATE void _sg_mtl_init_texdesc_rt(MTLTextureDescriptor* mtl_desc, _sg_image_t* img) { |
| 12337 | SOKOL_ASSERT(img->cmn.render_target); |
| 12338 | _SOKOL_UNUSED(img); |
| 12339 | mtl_desc.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget; |
| 12340 | mtl_desc.resourceOptions = MTLResourceStorageModePrivate; |
| 12341 | } |
| 12342 | |
| 12343 | // initialize MTLTextureDescriptor with MSAA attributes |
| 12344 | _SOKOL_PRIVATE void _sg_mtl_init_texdesc_rt_msaa(MTLTextureDescriptor* mtl_desc, _sg_image_t* img) { |
| 12345 | SOKOL_ASSERT(img->cmn.sample_count > 1); |
| 12346 | mtl_desc.usage = MTLTextureUsageRenderTarget; |
| 12347 | mtl_desc.resourceOptions = MTLResourceStorageModePrivate; |
| 12348 | mtl_desc.textureType = MTLTextureType2DMultisample; |
| 12349 | mtl_desc.sampleCount = (NSUInteger)img->cmn.sample_count; |
| 12350 | } |
| 12351 | |
| 12352 | _SOKOL_PRIVATE sg_resource_state _sg_mtl_create_image(_sg_image_t* img, const sg_image_desc* desc) { |
| 12353 | SOKOL_ASSERT(img && desc); |
| 12354 | const bool injected = (0 != desc->mtl_textures[0]); |
| 12355 | |
| 12356 | // first initialize all Metal resource pool slots to 'empty' |
| 12357 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 12358 | img->mtl.tex[i] = _sg_mtl_add_resource(nil); |
| 12359 | } |
| 12360 | |
| 12361 | // initialize a Metal texture descriptor |
| 12362 | MTLTextureDescriptor* mtl_desc = [[MTLTextureDescriptor alloc] init]; |
| 12363 | if (!_sg_mtl_init_texdesc_common(mtl_desc, img)) { |
| 12364 | _SG_OBJC_RELEASE(mtl_desc); |
| 12365 | return SG_RESOURCESTATE_FAILED; |
| 12366 | } |
| 12367 | if (img->cmn.render_target) { |
| 12368 | if (img->cmn.sample_count > 1) { |
| 12369 | _sg_mtl_init_texdesc_rt_msaa(mtl_desc, img); |
| 12370 | } else { |
| 12371 | _sg_mtl_init_texdesc_rt(mtl_desc, img); |
| 12372 | } |
| 12373 | } |
| 12374 | for (int slot = 0; slot < img->cmn.num_slots; slot++) { |
| 12375 | id<MTLTexture> mtl_tex; |
| 12376 | if (injected) { |
| 12377 | SOKOL_ASSERT(desc->mtl_textures[slot]); |
| 12378 | mtl_tex = (__bridge id<MTLTexture>) desc->mtl_textures[slot]; |
| 12379 | } else { |
| 12380 | mtl_tex = [_sg.mtl.device newTextureWithDescriptor:mtl_desc]; |
| 12381 | if (nil == mtl_tex) { |
| 12382 | _SG_OBJC_RELEASE(mtl_desc); |
| 12383 | _SG_ERROR(METAL_CREATE_TEXTURE_FAILED); |
| 12384 | return SG_RESOURCESTATE_FAILED; |
| 12385 | } |
| 12386 | if ((img->cmn.usage == SG_USAGE_IMMUTABLE) && !img->cmn.render_target) { |
| 12387 | _sg_mtl_copy_image_data(img, mtl_tex, &desc->data); |
| 12388 | } |
| 12389 | } |
| 12390 | #if defined(SOKOL_DEBUG) |
| 12391 | if (desc->label) { |
| 12392 | mtl_tex.label = [NSString stringWithFormat:@"%s.%d", desc->label, slot]; |
| 12393 | } |
| 12394 | #endif |
| 12395 | img->mtl.tex[slot] = _sg_mtl_add_resource(mtl_tex); |
| 12396 | _SG_OBJC_RELEASE(mtl_tex); |
| 12397 | } |
| 12398 | _SG_OBJC_RELEASE(mtl_desc); |
| 12399 | return SG_RESOURCESTATE_VALID; |
| 12400 | } |
| 12401 | |
| 12402 | _SOKOL_PRIVATE void _sg_mtl_discard_image(_sg_image_t* img) { |
| 12403 | SOKOL_ASSERT(img); |
| 12404 | // it's valid to call release resource with a 'null resource' |
| 12405 | for (int slot = 0; slot < img->cmn.num_slots; slot++) { |
| 12406 | _sg_mtl_release_resource(_sg.frame_index, img->mtl.tex[slot]); |
| 12407 | } |
| 12408 | } |
| 12409 | |
| 12410 | _SOKOL_PRIVATE sg_resource_state _sg_mtl_create_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) { |
| 12411 | SOKOL_ASSERT(smp && desc); |
| 12412 | id<MTLSamplerState> mtl_smp; |
| 12413 | const bool injected = (0 != desc->mtl_sampler); |
| 12414 | if (injected) { |
| 12415 | SOKOL_ASSERT(desc->mtl_sampler); |
| 12416 | mtl_smp = (__bridge id<MTLSamplerState>) desc->mtl_sampler; |
| 12417 | } else { |
| 12418 | MTLSamplerDescriptor* mtl_desc = [[MTLSamplerDescriptor alloc] init]; |
| 12419 | mtl_desc.sAddressMode = _sg_mtl_address_mode(desc->wrap_u); |
| 12420 | mtl_desc.tAddressMode = _sg_mtl_address_mode(desc->wrap_v); |
| 12421 | mtl_desc.rAddressMode = _sg_mtl_address_mode(desc->wrap_w); |
| 12422 | if (_sg.features.image_clamp_to_border) { |
| 12423 | if (@available(macOS 12.0, iOS 14.0, *)) { |
| 12424 | mtl_desc.borderColor = _sg_mtl_border_color(desc->border_color); |
| 12425 | } |
| 12426 | } |
| 12427 | mtl_desc.minFilter = _sg_mtl_minmag_filter(desc->min_filter); |
| 12428 | mtl_desc.magFilter = _sg_mtl_minmag_filter(desc->mag_filter); |
| 12429 | mtl_desc.mipFilter = _sg_mtl_mipmap_filter(desc->mipmap_filter); |
| 12430 | mtl_desc.lodMinClamp = desc->min_lod; |
| 12431 | mtl_desc.lodMaxClamp = desc->max_lod; |
| 12432 | // FIXME: lodAverage? |
| 12433 | mtl_desc.maxAnisotropy = desc->max_anisotropy; |
| 12434 | mtl_desc.normalizedCoordinates = YES; |
| 12435 | mtl_desc.compareFunction = _sg_mtl_compare_func(desc->compare); |
| 12436 | #if defined(SOKOL_DEBUG) |
| 12437 | if (desc->label) { |
| 12438 | mtl_desc.label = [NSString stringWithUTF8String:desc->label]; |
| 12439 | } |
| 12440 | #endif |
| 12441 | mtl_smp = [_sg.mtl.device newSamplerStateWithDescriptor:mtl_desc]; |
| 12442 | _SG_OBJC_RELEASE(mtl_desc); |
| 12443 | if (nil == mtl_smp) { |
| 12444 | _SG_ERROR(METAL_CREATE_SAMPLER_FAILED); |
| 12445 | return SG_RESOURCESTATE_FAILED; |
| 12446 | } |
| 12447 | } |
| 12448 | smp->mtl.sampler_state = _sg_mtl_add_resource(mtl_smp); |
| 12449 | _SG_OBJC_RELEASE(mtl_smp); |
| 12450 | return SG_RESOURCESTATE_VALID; |
| 12451 | } |
| 12452 | |
| 12453 | _SOKOL_PRIVATE void _sg_mtl_discard_sampler(_sg_sampler_t* smp) { |
| 12454 | SOKOL_ASSERT(smp); |
| 12455 | // it's valid to call release resource with a 'null resource' |
| 12456 | _sg_mtl_release_resource(_sg.frame_index, smp->mtl.sampler_state); |
| 12457 | } |
| 12458 | |
| 12459 | _SOKOL_PRIVATE id<MTLLibrary> _sg_mtl_compile_library(const char* src) { |
| 12460 | NSError* err = NULL; |
| 12461 | id<MTLLibrary> lib = [_sg.mtl.device |
| 12462 | newLibraryWithSource:[NSString stringWithUTF8String:src] |
| 12463 | options:nil |
| 12464 | error:&err |
| 12465 | ]; |
| 12466 | if (err) { |
| 12467 | _SG_ERROR(METAL_SHADER_COMPILATION_FAILED); |
| 12468 | _SG_LOGMSG(METAL_SHADER_COMPILATION_OUTPUT, [err.localizedDescription UTF8String]); |
| 12469 | } |
| 12470 | return lib; |
| 12471 | } |
| 12472 | |
| 12473 | _SOKOL_PRIVATE id<MTLLibrary> _sg_mtl_library_from_bytecode(const void* ptr, size_t num_bytes) { |
| 12474 | NSError* err = NULL; |
| 12475 | dispatch_data_t lib_data = dispatch_data_create(ptr, num_bytes, NULL, DISPATCH_DATA_DESTRUCTOR_DEFAULT); |
| 12476 | id<MTLLibrary> lib = [_sg.mtl.device newLibraryWithData:lib_data error:&err]; |
| 12477 | if (err) { |
| 12478 | _SG_ERROR(METAL_SHADER_CREATION_FAILED); |
| 12479 | _SG_LOGMSG(METAL_SHADER_COMPILATION_OUTPUT, [err.localizedDescription UTF8String]); |
| 12480 | } |
| 12481 | _SG_OBJC_RELEASE(lib_data); |
| 12482 | return lib; |
| 12483 | } |
| 12484 | |
| 12485 | _SOKOL_PRIVATE sg_resource_state _sg_mtl_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) { |
| 12486 | SOKOL_ASSERT(shd && desc); |
| 12487 | |
| 12488 | // create metal library objects and lookup entry functions |
| 12489 | id<MTLLibrary> vs_lib = nil; |
| 12490 | id<MTLLibrary> fs_lib = nil; |
| 12491 | id<MTLFunction> vs_func = nil; |
| 12492 | id<MTLFunction> fs_func = nil; |
| 12493 | const char* vs_entry = desc->vs.entry; |
| 12494 | const char* fs_entry = desc->fs.entry; |
| 12495 | if (desc->vs.bytecode.ptr && desc->fs.bytecode.ptr) { |
| 12496 | // separate byte code provided |
| 12497 | vs_lib = _sg_mtl_library_from_bytecode(desc->vs.bytecode.ptr, desc->vs.bytecode.size); |
| 12498 | fs_lib = _sg_mtl_library_from_bytecode(desc->fs.bytecode.ptr, desc->fs.bytecode.size); |
| 12499 | if ((nil == vs_lib) || (nil == fs_lib)) { |
| 12500 | goto failed; |
| 12501 | } |
| 12502 | vs_func = [vs_lib newFunctionWithName:[NSString stringWithUTF8String:vs_entry]]; |
| 12503 | fs_func = [fs_lib newFunctionWithName:[NSString stringWithUTF8String:fs_entry]]; |
| 12504 | } else if (desc->vs.source && desc->fs.source) { |
| 12505 | // separate sources provided |
| 12506 | vs_lib = _sg_mtl_compile_library(desc->vs.source); |
| 12507 | fs_lib = _sg_mtl_compile_library(desc->fs.source); |
| 12508 | if ((nil == vs_lib) || (nil == fs_lib)) { |
| 12509 | goto failed; |
| 12510 | } |
| 12511 | vs_func = [vs_lib newFunctionWithName:[NSString stringWithUTF8String:vs_entry]]; |
| 12512 | fs_func = [fs_lib newFunctionWithName:[NSString stringWithUTF8String:fs_entry]]; |
| 12513 | } else { |
| 12514 | goto failed; |
| 12515 | } |
| 12516 | if (nil == vs_func) { |
| 12517 | _SG_ERROR(METAL_VERTEX_SHADER_ENTRY_NOT_FOUND); |
| 12518 | goto failed; |
| 12519 | } |
| 12520 | if (nil == fs_func) { |
| 12521 | _SG_ERROR(METAL_FRAGMENT_SHADER_ENTRY_NOT_FOUND); |
| 12522 | goto failed; |
| 12523 | } |
| 12524 | #if defined(SOKOL_DEBUG) |
| 12525 | if (desc->label) { |
| 12526 | vs_lib.label = [NSString stringWithFormat:@"%s.vs", desc->label]; |
| 12527 | fs_lib.label = [NSString stringWithFormat:@"%s.fs", desc->label]; |
| 12528 | } |
| 12529 | #endif |
| 12530 | // it is legal to call _sg_mtl_add_resource with a nil value, this will return a special 0xFFFFFFFF index |
| 12531 | shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_lib = _sg_mtl_add_resource(vs_lib); |
| 12532 | _SG_OBJC_RELEASE(vs_lib); |
| 12533 | shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_lib = _sg_mtl_add_resource(fs_lib); |
| 12534 | _SG_OBJC_RELEASE(fs_lib); |
| 12535 | shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func = _sg_mtl_add_resource(vs_func); |
| 12536 | _SG_OBJC_RELEASE(vs_func); |
| 12537 | shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func = _sg_mtl_add_resource(fs_func); |
| 12538 | _SG_OBJC_RELEASE(fs_func); |
| 12539 | return SG_RESOURCESTATE_VALID; |
| 12540 | failed: |
| 12541 | if (vs_lib != nil) { |
| 12542 | _SG_OBJC_RELEASE(vs_lib); |
| 12543 | } |
| 12544 | if (fs_lib != nil) { |
| 12545 | _SG_OBJC_RELEASE(fs_lib); |
| 12546 | } |
| 12547 | if (vs_func != nil) { |
| 12548 | _SG_OBJC_RELEASE(vs_func); |
| 12549 | } |
| 12550 | if (fs_func != nil) { |
| 12551 | _SG_OBJC_RELEASE(fs_func); |
| 12552 | } |
| 12553 | return SG_RESOURCESTATE_FAILED; |
| 12554 | } |
| 12555 | |
| 12556 | _SOKOL_PRIVATE void _sg_mtl_discard_shader(_sg_shader_t* shd) { |
| 12557 | SOKOL_ASSERT(shd); |
| 12558 | // it is valid to call _sg_mtl_release_resource with a 'null resource' |
| 12559 | _sg_mtl_release_resource(_sg.frame_index, shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func); |
| 12560 | _sg_mtl_release_resource(_sg.frame_index, shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_lib); |
| 12561 | _sg_mtl_release_resource(_sg.frame_index, shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func); |
| 12562 | _sg_mtl_release_resource(_sg.frame_index, shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_lib); |
| 12563 | } |
| 12564 | |
| 12565 | _SOKOL_PRIVATE sg_resource_state _sg_mtl_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) { |
| 12566 | SOKOL_ASSERT(pip && shd && desc); |
| 12567 | SOKOL_ASSERT(desc->shader.id == shd->slot.id); |
| 12568 | |
| 12569 | pip->shader = shd; |
| 12570 | |
| 12571 | sg_primitive_type prim_type = desc->primitive_type; |
| 12572 | pip->mtl.prim_type = _sg_mtl_primitive_type(prim_type); |
| 12573 | pip->mtl.index_size = _sg_mtl_index_size(pip->cmn.index_type); |
| 12574 | if (SG_INDEXTYPE_NONE != pip->cmn.index_type) { |
| 12575 | pip->mtl.index_type = _sg_mtl_index_type(pip->cmn.index_type); |
| 12576 | } |
| 12577 | pip->mtl.cull_mode = _sg_mtl_cull_mode(desc->cull_mode); |
| 12578 | pip->mtl.winding = _sg_mtl_winding(desc->face_winding); |
| 12579 | pip->mtl.stencil_ref = desc->stencil.ref; |
| 12580 | |
| 12581 | // create vertex-descriptor |
| 12582 | MTLVertexDescriptor* vtx_desc = [MTLVertexDescriptor vertexDescriptor]; |
| 12583 | for (NSUInteger attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 12584 | const sg_vertex_attr_state* a_state = &desc->layout.attrs[attr_index]; |
| 12585 | if (a_state->format == SG_VERTEXFORMAT_INVALID) { |
| 12586 | break; |
| 12587 | } |
| 12588 | SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS); |
| 12589 | vtx_desc.attributes[attr_index].format = _sg_mtl_vertex_format(a_state->format); |
| 12590 | vtx_desc.attributes[attr_index].offset = (NSUInteger)a_state->offset; |
| 12591 | vtx_desc.attributes[attr_index].bufferIndex = (NSUInteger)(a_state->buffer_index + SG_MAX_SHADERSTAGE_UBS); |
| 12592 | pip->cmn.vertex_buffer_layout_active[a_state->buffer_index] = true; |
| 12593 | } |
| 12594 | for (NSUInteger layout_index = 0; layout_index < SG_MAX_VERTEX_BUFFERS; layout_index++) { |
| 12595 | if (pip->cmn.vertex_buffer_layout_active[layout_index]) { |
| 12596 | const sg_vertex_buffer_layout_state* l_state = &desc->layout.buffers[layout_index]; |
| 12597 | const NSUInteger mtl_vb_slot = layout_index + SG_MAX_SHADERSTAGE_UBS; |
| 12598 | SOKOL_ASSERT(l_state->stride > 0); |
| 12599 | vtx_desc.layouts[mtl_vb_slot].stride = (NSUInteger)l_state->stride; |
| 12600 | vtx_desc.layouts[mtl_vb_slot].stepFunction = _sg_mtl_step_function(l_state->step_func); |
| 12601 | vtx_desc.layouts[mtl_vb_slot].stepRate = (NSUInteger)l_state->step_rate; |
| 12602 | if (SG_VERTEXSTEP_PER_INSTANCE == l_state->step_func) { |
| 12603 | // NOTE: not actually used in _sg_mtl_draw() |
| 12604 | pip->cmn.use_instanced_draw = true; |
| 12605 | } |
| 12606 | } |
| 12607 | } |
| 12608 | |
| 12609 | // render-pipeline descriptor |
| 12610 | MTLRenderPipelineDescriptor* rp_desc = [[MTLRenderPipelineDescriptor alloc] init]; |
| 12611 | rp_desc.vertexDescriptor = vtx_desc; |
| 12612 | SOKOL_ASSERT(shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func != _SG_MTL_INVALID_SLOT_INDEX); |
| 12613 | rp_desc.vertexFunction = _sg_mtl_id(shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func); |
| 12614 | SOKOL_ASSERT(shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func != _SG_MTL_INVALID_SLOT_INDEX); |
| 12615 | rp_desc.fragmentFunction = _sg_mtl_id(shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func); |
| 12616 | rp_desc.rasterSampleCount = (NSUInteger)desc->sample_count; |
| 12617 | rp_desc.alphaToCoverageEnabled = desc->alpha_to_coverage_enabled; |
| 12618 | rp_desc.alphaToOneEnabled = NO; |
| 12619 | rp_desc.rasterizationEnabled = YES; |
| 12620 | rp_desc.depthAttachmentPixelFormat = _sg_mtl_pixel_format(desc->depth.pixel_format); |
| 12621 | if (desc->depth.pixel_format == SG_PIXELFORMAT_DEPTH_STENCIL) { |
| 12622 | rp_desc.stencilAttachmentPixelFormat = _sg_mtl_pixel_format(desc->depth.pixel_format); |
| 12623 | } |
| 12624 | if (@available(macOS 10.13, iOS 11.0, *)) { |
| 12625 | for (NSUInteger i = 0; i < (SG_MAX_SHADERSTAGE_UBS+SG_MAX_VERTEX_BUFFERS); i++) { |
| 12626 | rp_desc.vertexBuffers[i].mutability = MTLMutabilityImmutable; |
| 12627 | } |
| 12628 | for (NSUInteger i = 0; i < SG_MAX_SHADERSTAGE_UBS; i++) { |
| 12629 | rp_desc.fragmentBuffers[i].mutability = MTLMutabilityImmutable; |
| 12630 | } |
| 12631 | } |
| 12632 | for (NSUInteger i = 0; i < (NSUInteger)desc->color_count; i++) { |
| 12633 | SOKOL_ASSERT(i < SG_MAX_COLOR_ATTACHMENTS); |
| 12634 | const sg_color_target_state* cs = &desc->colors[i]; |
| 12635 | rp_desc.colorAttachments[i].pixelFormat = _sg_mtl_pixel_format(cs->pixel_format); |
| 12636 | rp_desc.colorAttachments[i].writeMask = _sg_mtl_color_write_mask(cs->write_mask); |
| 12637 | rp_desc.colorAttachments[i].blendingEnabled = cs->blend.enabled; |
| 12638 | rp_desc.colorAttachments[i].alphaBlendOperation = _sg_mtl_blend_op(cs->blend.op_alpha); |
| 12639 | rp_desc.colorAttachments[i].rgbBlendOperation = _sg_mtl_blend_op(cs->blend.op_rgb); |
| 12640 | rp_desc.colorAttachments[i].destinationAlphaBlendFactor = _sg_mtl_blend_factor(cs->blend.dst_factor_alpha); |
| 12641 | rp_desc.colorAttachments[i].destinationRGBBlendFactor = _sg_mtl_blend_factor(cs->blend.dst_factor_rgb); |
| 12642 | rp_desc.colorAttachments[i].sourceAlphaBlendFactor = _sg_mtl_blend_factor(cs->blend.src_factor_alpha); |
| 12643 | rp_desc.colorAttachments[i].sourceRGBBlendFactor = _sg_mtl_blend_factor(cs->blend.src_factor_rgb); |
| 12644 | } |
| 12645 | #if defined(SOKOL_DEBUG) |
| 12646 | if (desc->label) { |
| 12647 | rp_desc.label = [NSString stringWithFormat:@"%s", desc->label]; |
| 12648 | } |
| 12649 | #endif |
| 12650 | NSError* err = NULL; |
| 12651 | id<MTLRenderPipelineState> mtl_rps = [_sg.mtl.device newRenderPipelineStateWithDescriptor:rp_desc error:&err]; |
| 12652 | _SG_OBJC_RELEASE(rp_desc); |
| 12653 | if (nil == mtl_rps) { |
| 12654 | SOKOL_ASSERT(err); |
| 12655 | _SG_ERROR(METAL_CREATE_RPS_FAILED); |
| 12656 | _SG_LOGMSG(METAL_CREATE_RPS_OUTPUT, [err.localizedDescription UTF8String]); |
| 12657 | return SG_RESOURCESTATE_FAILED; |
| 12658 | } |
| 12659 | pip->mtl.rps = _sg_mtl_add_resource(mtl_rps); |
| 12660 | _SG_OBJC_RELEASE(mtl_rps); |
| 12661 | |
| 12662 | // depth-stencil-state |
| 12663 | MTLDepthStencilDescriptor* ds_desc = [[MTLDepthStencilDescriptor alloc] init]; |
| 12664 | ds_desc.depthCompareFunction = _sg_mtl_compare_func(desc->depth.compare); |
| 12665 | ds_desc.depthWriteEnabled = desc->depth.write_enabled; |
| 12666 | if (desc->stencil.enabled) { |
| 12667 | const sg_stencil_face_state* sb = &desc->stencil.back; |
| 12668 | ds_desc.backFaceStencil = [[MTLStencilDescriptor alloc] init]; |
| 12669 | ds_desc.backFaceStencil.stencilFailureOperation = _sg_mtl_stencil_op(sb->fail_op); |
| 12670 | ds_desc.backFaceStencil.depthFailureOperation = _sg_mtl_stencil_op(sb->depth_fail_op); |
| 12671 | ds_desc.backFaceStencil.depthStencilPassOperation = _sg_mtl_stencil_op(sb->pass_op); |
| 12672 | ds_desc.backFaceStencil.stencilCompareFunction = _sg_mtl_compare_func(sb->compare); |
| 12673 | ds_desc.backFaceStencil.readMask = desc->stencil.read_mask; |
| 12674 | ds_desc.backFaceStencil.writeMask = desc->stencil.write_mask; |
| 12675 | const sg_stencil_face_state* sf = &desc->stencil.front; |
| 12676 | ds_desc.frontFaceStencil = [[MTLStencilDescriptor alloc] init]; |
| 12677 | ds_desc.frontFaceStencil.stencilFailureOperation = _sg_mtl_stencil_op(sf->fail_op); |
| 12678 | ds_desc.frontFaceStencil.depthFailureOperation = _sg_mtl_stencil_op(sf->depth_fail_op); |
| 12679 | ds_desc.frontFaceStencil.depthStencilPassOperation = _sg_mtl_stencil_op(sf->pass_op); |
| 12680 | ds_desc.frontFaceStencil.stencilCompareFunction = _sg_mtl_compare_func(sf->compare); |
| 12681 | ds_desc.frontFaceStencil.readMask = desc->stencil.read_mask; |
| 12682 | ds_desc.frontFaceStencil.writeMask = desc->stencil.write_mask; |
| 12683 | } |
| 12684 | #if defined(SOKOL_DEBUG) |
| 12685 | if (desc->label) { |
| 12686 | ds_desc.label = [NSString stringWithFormat:@"%s.dss", desc->label]; |
| 12687 | } |
| 12688 | #endif |
| 12689 | id<MTLDepthStencilState> mtl_dss = [_sg.mtl.device newDepthStencilStateWithDescriptor:ds_desc]; |
| 12690 | _SG_OBJC_RELEASE(ds_desc); |
| 12691 | if (nil == mtl_dss) { |
| 12692 | _SG_ERROR(METAL_CREATE_DSS_FAILED); |
| 12693 | return SG_RESOURCESTATE_FAILED; |
| 12694 | } |
| 12695 | pip->mtl.dss = _sg_mtl_add_resource(mtl_dss); |
| 12696 | _SG_OBJC_RELEASE(mtl_dss); |
| 12697 | return SG_RESOURCESTATE_VALID; |
| 12698 | } |
| 12699 | |
| 12700 | _SOKOL_PRIVATE void _sg_mtl_discard_pipeline(_sg_pipeline_t* pip) { |
| 12701 | SOKOL_ASSERT(pip); |
| 12702 | // it's valid to call release resource with a 'null resource' |
| 12703 | _sg_mtl_release_resource(_sg.frame_index, pip->mtl.rps); |
| 12704 | _sg_mtl_release_resource(_sg.frame_index, pip->mtl.dss); |
| 12705 | } |
| 12706 | |
| 12707 | _SOKOL_PRIVATE sg_resource_state _sg_mtl_create_attachments(_sg_attachments_t* atts, _sg_image_t** color_images, _sg_image_t** resolve_images, _sg_image_t* ds_img, const sg_attachments_desc* desc) { |
| 12708 | SOKOL_ASSERT(atts && desc); |
| 12709 | SOKOL_ASSERT(color_images && resolve_images); |
| 12710 | |
| 12711 | // copy image pointers |
| 12712 | for (int i = 0; i < atts->cmn.num_colors; i++) { |
| 12713 | const sg_attachment_desc* color_desc = &desc->colors[i]; |
| 12714 | _SOKOL_UNUSED(color_desc); |
| 12715 | SOKOL_ASSERT(color_desc->image.id != SG_INVALID_ID); |
| 12716 | SOKOL_ASSERT(0 == atts->mtl.colors[i].image); |
| 12717 | SOKOL_ASSERT(color_images[i] && (color_images[i]->slot.id == color_desc->image.id)); |
| 12718 | SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(color_images[i]->cmn.pixel_format)); |
| 12719 | atts->mtl.colors[i].image = color_images[i]; |
| 12720 | |
| 12721 | const sg_attachment_desc* resolve_desc = &desc->resolves[i]; |
| 12722 | if (resolve_desc->image.id != SG_INVALID_ID) { |
| 12723 | SOKOL_ASSERT(0 == atts->mtl.resolves[i].image); |
| 12724 | SOKOL_ASSERT(resolve_images[i] && (resolve_images[i]->slot.id == resolve_desc->image.id)); |
| 12725 | SOKOL_ASSERT(color_images[i] && (color_images[i]->cmn.pixel_format == resolve_images[i]->cmn.pixel_format)); |
| 12726 | atts->mtl.resolves[i].image = resolve_images[i]; |
| 12727 | } |
| 12728 | } |
| 12729 | SOKOL_ASSERT(0 == atts->mtl.depth_stencil.image); |
| 12730 | const sg_attachment_desc* ds_desc = &desc->depth_stencil; |
| 12731 | if (ds_desc->image.id != SG_INVALID_ID) { |
| 12732 | SOKOL_ASSERT(ds_img && (ds_img->slot.id == ds_desc->image.id)); |
| 12733 | SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(ds_img->cmn.pixel_format)); |
| 12734 | atts->mtl.depth_stencil.image = ds_img; |
| 12735 | } |
| 12736 | return SG_RESOURCESTATE_VALID; |
| 12737 | } |
| 12738 | |
| 12739 | _SOKOL_PRIVATE void _sg_mtl_discard_attachments(_sg_attachments_t* atts) { |
| 12740 | SOKOL_ASSERT(atts); |
| 12741 | _SOKOL_UNUSED(atts); |
| 12742 | } |
| 12743 | |
| 12744 | _SOKOL_PRIVATE _sg_image_t* _sg_mtl_attachments_color_image(const _sg_attachments_t* atts, int index) { |
| 12745 | // NOTE: may return null |
| 12746 | SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS)); |
| 12747 | return atts->mtl.colors[index].image; |
| 12748 | } |
| 12749 | |
| 12750 | _SOKOL_PRIVATE _sg_image_t* _sg_mtl_attachments_resolve_image(const _sg_attachments_t* atts, int index) { |
| 12751 | // NOTE: may return null |
| 12752 | SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS)); |
| 12753 | return atts->mtl.resolves[index].image; |
| 12754 | } |
| 12755 | |
| 12756 | _SOKOL_PRIVATE _sg_image_t* _sg_mtl_attachments_ds_image(const _sg_attachments_t* atts) { |
| 12757 | // NOTE: may return null |
| 12758 | SOKOL_ASSERT(atts); |
| 12759 | return atts->mtl.depth_stencil.image; |
| 12760 | } |
| 12761 | |
| 12762 | _SOKOL_PRIVATE void _sg_mtl_begin_pass(const sg_pass* pass) { |
| 12763 | SOKOL_ASSERT(pass); |
| 12764 | SOKOL_ASSERT(_sg.mtl.cmd_queue); |
| 12765 | SOKOL_ASSERT(nil == _sg.mtl.cmd_encoder); |
| 12766 | SOKOL_ASSERT(nil == _sg.mtl.cur_drawable); |
| 12767 | _sg_mtl_clear_state_cache(); |
| 12768 | |
| 12769 | const _sg_attachments_t* atts = _sg.cur_pass.atts; |
| 12770 | const sg_swapchain* swapchain = &pass->swapchain; |
| 12771 | const sg_pass_action* action = &pass->action; |
| 12772 | |
| 12773 | /* |
| 12774 | if this is the first pass in the frame, create command buffers |
| 12775 | |
| 12776 | NOTE: we're creating two command buffers here, one with unretained references |
| 12777 | for storing the regular commands, and one with retained references for |
| 12778 | storing the presentDrawable call (this needs to hold on the drawable until |
| 12779 | presentation has happened - and the easiest way to do this is to let the |
| 12780 | command buffer manage the lifetime of the drawable). |
| 12781 | |
| 12782 | Also see: https://github.com/floooh/sokol/issues/762 |
| 12783 | */ |
| 12784 | if (nil == _sg.mtl.cmd_buffer) { |
| 12785 | // block until the oldest frame in flight has finished |
| 12786 | dispatch_semaphore_wait(_sg.mtl.sem, DISPATCH_TIME_FOREVER); |
| 12787 | if (_sg.desc.mtl_use_command_buffer_with_retained_references) { |
| 12788 | _sg.mtl.cmd_buffer = [_sg.mtl.cmd_queue commandBuffer]; |
| 12789 | } else { |
| 12790 | _sg.mtl.cmd_buffer = [_sg.mtl.cmd_queue commandBufferWithUnretainedReferences]; |
| 12791 | } |
| 12792 | [_sg.mtl.cmd_buffer enqueue]; |
| 12793 | [_sg.mtl.cmd_buffer addCompletedHandler:^(id<MTLCommandBuffer> cmd_buf) { |
| 12794 | // NOTE: this code is called on a different thread! |
| 12795 | _SOKOL_UNUSED(cmd_buf); |
| 12796 | dispatch_semaphore_signal(_sg.mtl.sem); |
| 12797 | }]; |
| 12798 | } |
| 12799 | |
| 12800 | // if this is first pass in frame, get uniform buffer base pointer |
| 12801 | if (0 == _sg.mtl.cur_ub_base_ptr) { |
| 12802 | _sg.mtl.cur_ub_base_ptr = (uint8_t*)[_sg.mtl.uniform_buffers[_sg.mtl.cur_frame_rotate_index] contents]; |
| 12803 | } |
| 12804 | |
| 12805 | MTLRenderPassDescriptor* pass_desc = [MTLRenderPassDescriptor renderPassDescriptor]; |
| 12806 | SOKOL_ASSERT(pass_desc); |
| 12807 | if (atts) { |
| 12808 | // setup pass descriptor for offscreen rendering |
| 12809 | SOKOL_ASSERT(atts->slot.state == SG_RESOURCESTATE_VALID); |
| 12810 | for (NSUInteger i = 0; i < (NSUInteger)atts->cmn.num_colors; i++) { |
| 12811 | const _sg_attachment_common_t* cmn_color_att = &atts->cmn.colors[i]; |
| 12812 | const _sg_mtl_attachment_t* mtl_color_att = &atts->mtl.colors[i]; |
| 12813 | const _sg_image_t* color_att_img = mtl_color_att->image; |
| 12814 | const _sg_attachment_common_t* cmn_resolve_att = &atts->cmn.resolves[i]; |
| 12815 | const _sg_mtl_attachment_t* mtl_resolve_att = &atts->mtl.resolves[i]; |
| 12816 | const _sg_image_t* resolve_att_img = mtl_resolve_att->image; |
| 12817 | SOKOL_ASSERT(color_att_img->slot.state == SG_RESOURCESTATE_VALID); |
| 12818 | SOKOL_ASSERT(color_att_img->slot.id == cmn_color_att->image_id.id); |
| 12819 | SOKOL_ASSERT(color_att_img->mtl.tex[color_att_img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 12820 | pass_desc.colorAttachments[i].loadAction = _sg_mtl_load_action(action->colors[i].load_action); |
| 12821 | pass_desc.colorAttachments[i].storeAction = _sg_mtl_store_action(action->colors[i].store_action, resolve_att_img != 0); |
| 12822 | sg_color c = action->colors[i].clear_value; |
| 12823 | pass_desc.colorAttachments[i].clearColor = MTLClearColorMake(c.r, c.g, c.b, c.a); |
| 12824 | pass_desc.colorAttachments[i].texture = _sg_mtl_id(color_att_img->mtl.tex[color_att_img->cmn.active_slot]); |
| 12825 | pass_desc.colorAttachments[i].level = (NSUInteger)cmn_color_att->mip_level; |
| 12826 | switch (color_att_img->cmn.type) { |
| 12827 | case SG_IMAGETYPE_CUBE: |
| 12828 | case SG_IMAGETYPE_ARRAY: |
| 12829 | pass_desc.colorAttachments[i].slice = (NSUInteger)cmn_color_att->slice; |
| 12830 | break; |
| 12831 | case SG_IMAGETYPE_3D: |
| 12832 | pass_desc.colorAttachments[i].depthPlane = (NSUInteger)cmn_color_att->slice; |
| 12833 | break; |
| 12834 | default: break; |
| 12835 | } |
| 12836 | if (resolve_att_img) { |
| 12837 | SOKOL_ASSERT(resolve_att_img->slot.state == SG_RESOURCESTATE_VALID); |
| 12838 | SOKOL_ASSERT(resolve_att_img->slot.id == cmn_resolve_att->image_id.id); |
| 12839 | SOKOL_ASSERT(resolve_att_img->mtl.tex[resolve_att_img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 12840 | pass_desc.colorAttachments[i].resolveTexture = _sg_mtl_id(resolve_att_img->mtl.tex[resolve_att_img->cmn.active_slot]); |
| 12841 | pass_desc.colorAttachments[i].resolveLevel = (NSUInteger)cmn_resolve_att->mip_level; |
| 12842 | switch (resolve_att_img->cmn.type) { |
| 12843 | case SG_IMAGETYPE_CUBE: |
| 12844 | case SG_IMAGETYPE_ARRAY: |
| 12845 | pass_desc.colorAttachments[i].resolveSlice = (NSUInteger)cmn_resolve_att->slice; |
| 12846 | break; |
| 12847 | case SG_IMAGETYPE_3D: |
| 12848 | pass_desc.colorAttachments[i].resolveDepthPlane = (NSUInteger)cmn_resolve_att->slice; |
| 12849 | break; |
| 12850 | default: break; |
| 12851 | } |
| 12852 | } |
| 12853 | } |
| 12854 | const _sg_image_t* ds_att_img = atts->mtl.depth_stencil.image; |
| 12855 | if (0 != ds_att_img) { |
| 12856 | SOKOL_ASSERT(ds_att_img->slot.state == SG_RESOURCESTATE_VALID); |
| 12857 | SOKOL_ASSERT(ds_att_img->slot.id == atts->cmn.depth_stencil.image_id.id); |
| 12858 | SOKOL_ASSERT(ds_att_img->mtl.tex[ds_att_img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 12859 | pass_desc.depthAttachment.texture = _sg_mtl_id(ds_att_img->mtl.tex[ds_att_img->cmn.active_slot]); |
| 12860 | pass_desc.depthAttachment.loadAction = _sg_mtl_load_action(action->depth.load_action); |
| 12861 | pass_desc.depthAttachment.storeAction = _sg_mtl_store_action(action->depth.store_action, false); |
| 12862 | pass_desc.depthAttachment.clearDepth = action->depth.clear_value; |
| 12863 | const _sg_attachment_common_t* cmn_ds_att = &atts->cmn.depth_stencil; |
| 12864 | switch (ds_att_img->cmn.type) { |
| 12865 | case SG_IMAGETYPE_CUBE: |
| 12866 | case SG_IMAGETYPE_ARRAY: |
| 12867 | pass_desc.depthAttachment.slice = (NSUInteger)cmn_ds_att->slice; |
| 12868 | break; |
| 12869 | case SG_IMAGETYPE_3D: |
| 12870 | pass_desc.depthAttachment.resolveDepthPlane = (NSUInteger)cmn_ds_att->slice; |
| 12871 | break; |
| 12872 | default: break; |
| 12873 | } |
| 12874 | if (_sg_is_depth_stencil_format(ds_att_img->cmn.pixel_format)) { |
| 12875 | pass_desc.stencilAttachment.texture = _sg_mtl_id(ds_att_img->mtl.tex[ds_att_img->cmn.active_slot]); |
| 12876 | pass_desc.stencilAttachment.loadAction = _sg_mtl_load_action(action->stencil.load_action); |
| 12877 | pass_desc.stencilAttachment.storeAction = _sg_mtl_store_action(action->depth.store_action, false); |
| 12878 | pass_desc.stencilAttachment.clearStencil = action->stencil.clear_value; |
| 12879 | switch (ds_att_img->cmn.type) { |
| 12880 | case SG_IMAGETYPE_CUBE: |
| 12881 | case SG_IMAGETYPE_ARRAY: |
| 12882 | pass_desc.stencilAttachment.slice = (NSUInteger)cmn_ds_att->slice; |
| 12883 | break; |
| 12884 | case SG_IMAGETYPE_3D: |
| 12885 | pass_desc.stencilAttachment.resolveDepthPlane = (NSUInteger)cmn_ds_att->slice; |
| 12886 | break; |
| 12887 | default: break; |
| 12888 | } |
| 12889 | } |
| 12890 | } |
| 12891 | } else { |
| 12892 | // setup pass descriptor for swapchain rendering |
| 12893 | // |
| 12894 | // NOTE: at least in macOS Sonoma this no longer seems to be the case, the |
| 12895 | // current drawable is also valid in a minimized window |
| 12896 | // === |
| 12897 | // an MTKView current_drawable will not be valid if window is minimized, don't do any rendering in this case |
| 12898 | if (0 == swapchain->metal.current_drawable) { |
| 12899 | _sg.cur_pass.valid = false; |
| 12900 | return; |
| 12901 | } |
| 12902 | // pin the swapchain resources into memory so that they outlive their command buffer |
| 12903 | // (this is necessary because the command buffer doesn't retain references) |
| 12904 | int pass_desc_ref = _sg_mtl_add_resource(pass_desc); |
| 12905 | _sg_mtl_release_resource(_sg.frame_index, pass_desc_ref); |
| 12906 | |
| 12907 | _sg.mtl.cur_drawable = (__bridge id<CAMetalDrawable>) swapchain->metal.current_drawable; |
| 12908 | if (swapchain->sample_count > 1) { |
| 12909 | // multi-sampling: render into msaa texture, resolve into drawable texture |
| 12910 | id<MTLTexture> msaa_tex = (__bridge id<MTLTexture>) swapchain->metal.msaa_color_texture; |
| 12911 | SOKOL_ASSERT(msaa_tex != nil); |
| 12912 | pass_desc.colorAttachments[0].texture = msaa_tex; |
| 12913 | pass_desc.colorAttachments[0].resolveTexture = _sg.mtl.cur_drawable.texture; |
| 12914 | pass_desc.colorAttachments[0].storeAction = MTLStoreActionMultisampleResolve; |
| 12915 | } else { |
| 12916 | // non-msaa: render into current_drawable |
| 12917 | pass_desc.colorAttachments[0].texture = _sg.mtl.cur_drawable.texture; |
| 12918 | pass_desc.colorAttachments[0].storeAction = MTLStoreActionStore; |
| 12919 | } |
| 12920 | pass_desc.colorAttachments[0].loadAction = _sg_mtl_load_action(action->colors[0].load_action); |
| 12921 | const sg_color c = action->colors[0].clear_value; |
| 12922 | pass_desc.colorAttachments[0].clearColor = MTLClearColorMake(c.r, c.g, c.b, c.a); |
| 12923 | |
| 12924 | // optional depth-stencil texture |
| 12925 | if (swapchain->metal.depth_stencil_texture) { |
| 12926 | id<MTLTexture> ds_tex = (__bridge id<MTLTexture>) swapchain->metal.depth_stencil_texture; |
| 12927 | SOKOL_ASSERT(ds_tex != nil); |
| 12928 | pass_desc.depthAttachment.texture = ds_tex; |
| 12929 | pass_desc.depthAttachment.storeAction = MTLStoreActionDontCare; |
| 12930 | pass_desc.depthAttachment.loadAction = _sg_mtl_load_action(action->depth.load_action); |
| 12931 | pass_desc.depthAttachment.clearDepth = action->depth.clear_value; |
| 12932 | if (_sg_is_depth_stencil_format(swapchain->depth_format)) { |
| 12933 | pass_desc.stencilAttachment.texture = ds_tex; |
| 12934 | pass_desc.stencilAttachment.storeAction = MTLStoreActionDontCare; |
| 12935 | pass_desc.stencilAttachment.loadAction = _sg_mtl_load_action(action->stencil.load_action); |
| 12936 | pass_desc.stencilAttachment.clearStencil = action->stencil.clear_value; |
| 12937 | } |
| 12938 | } |
| 12939 | } |
| 12940 | |
| 12941 | // NOTE: at least in macOS Sonoma, the following is no longer the case, a valid |
| 12942 | // render command encoder is also returned in a minimized window |
| 12943 | // === |
| 12944 | // create a render command encoder, this might return nil if window is minimized |
| 12945 | _sg.mtl.cmd_encoder = [_sg.mtl.cmd_buffer renderCommandEncoderWithDescriptor:pass_desc]; |
| 12946 | if (nil == _sg.mtl.cmd_encoder) { |
| 12947 | _sg.cur_pass.valid = false; |
| 12948 | return; |
| 12949 | } |
| 12950 | |
| 12951 | #if defined(SOKOL_DEBUG) |
| 12952 | if (pass->label) { |
| 12953 | _sg.mtl.cmd_encoder.label = [NSString stringWithUTF8String:pass->label]; |
| 12954 | } |
| 12955 | #endif |
| 12956 | |
| 12957 | // bind the global uniform buffer, this only happens once per pass |
| 12958 | _sg_mtl_bind_uniform_buffers(); |
| 12959 | } |
| 12960 | |
| 12961 | _SOKOL_PRIVATE void _sg_mtl_end_pass(void) { |
| 12962 | if (nil != _sg.mtl.cmd_encoder) { |
| 12963 | [_sg.mtl.cmd_encoder endEncoding]; |
| 12964 | // NOTE: MTLRenderCommandEncoder is autoreleased |
| 12965 | _sg.mtl.cmd_encoder = nil; |
| 12966 | } |
| 12967 | // if this is a swapchain pass, present the drawable |
| 12968 | if (nil != _sg.mtl.cur_drawable) { |
| 12969 | [_sg.mtl.cmd_buffer presentDrawable:_sg.mtl.cur_drawable]; |
| 12970 | _sg.mtl.cur_drawable = nil; |
| 12971 | } |
| 12972 | } |
| 12973 | |
| 12974 | _SOKOL_PRIVATE void _sg_mtl_commit(void) { |
| 12975 | SOKOL_ASSERT(nil == _sg.mtl.cmd_encoder); |
| 12976 | SOKOL_ASSERT(nil != _sg.mtl.cmd_buffer); |
| 12977 | |
| 12978 | // commit the frame's command buffer |
| 12979 | [_sg.mtl.cmd_buffer commit]; |
| 12980 | |
| 12981 | // garbage-collect resources pending for release |
| 12982 | _sg_mtl_garbage_collect(_sg.frame_index); |
| 12983 | |
| 12984 | // rotate uniform buffer slot |
| 12985 | if (++_sg.mtl.cur_frame_rotate_index >= SG_NUM_INFLIGHT_FRAMES) { |
| 12986 | _sg.mtl.cur_frame_rotate_index = 0; |
| 12987 | } |
| 12988 | _sg.mtl.cur_ub_offset = 0; |
| 12989 | _sg.mtl.cur_ub_base_ptr = 0; |
| 12990 | // NOTE: MTLCommandBuffer is autoreleased |
| 12991 | _sg.mtl.cmd_buffer = nil; |
| 12992 | } |
| 12993 | |
| 12994 | _SOKOL_PRIVATE void _sg_mtl_apply_viewport(int x, int y, int w, int h, bool origin_top_left) { |
| 12995 | SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder); |
| 12996 | SOKOL_ASSERT(_sg.cur_pass.height > 0); |
| 12997 | MTLViewport vp; |
| 12998 | vp.originX = (double) x; |
| 12999 | vp.originY = (double) (origin_top_left ? y : (_sg.cur_pass.height - (y + h))); |
| 13000 | vp.width = (double) w; |
| 13001 | vp.height = (double) h; |
| 13002 | vp.znear = 0.0; |
| 13003 | vp.zfar = 1.0; |
| 13004 | [_sg.mtl.cmd_encoder setViewport:vp]; |
| 13005 | } |
| 13006 | |
| 13007 | _SOKOL_PRIVATE void _sg_mtl_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) { |
| 13008 | SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder); |
| 13009 | SOKOL_ASSERT(_sg.cur_pass.width > 0); |
| 13010 | SOKOL_ASSERT(_sg.cur_pass.height > 0); |
| 13011 | // clip against framebuffer rect |
| 13012 | const _sg_recti_t clip = _sg_clipi(x, y, w, h, _sg.cur_pass.width, _sg.cur_pass.height); |
| 13013 | MTLScissorRect r; |
| 13014 | r.x = (NSUInteger)clip.x; |
| 13015 | r.y = (NSUInteger) (origin_top_left ? clip.y : (_sg.cur_pass.height - (clip.y + clip.h))); |
| 13016 | r.width = (NSUInteger)clip.w; |
| 13017 | r.height = (NSUInteger)clip.h; |
| 13018 | [_sg.mtl.cmd_encoder setScissorRect:r]; |
| 13019 | } |
| 13020 | |
| 13021 | _SOKOL_PRIVATE void _sg_mtl_apply_pipeline(_sg_pipeline_t* pip) { |
| 13022 | SOKOL_ASSERT(pip); |
| 13023 | SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id)); |
| 13024 | SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder); |
| 13025 | |
| 13026 | if (_sg.mtl.state_cache.cur_pipeline_id.id != pip->slot.id) { |
| 13027 | _sg.mtl.state_cache.cur_pipeline = pip; |
| 13028 | _sg.mtl.state_cache.cur_pipeline_id.id = pip->slot.id; |
| 13029 | sg_color c = pip->cmn.blend_color; |
| 13030 | [_sg.mtl.cmd_encoder setBlendColorRed:c.r green:c.g blue:c.b alpha:c.a]; |
| 13031 | _sg_stats_add(metal.pipeline.num_set_blend_color, 1); |
| 13032 | [_sg.mtl.cmd_encoder setCullMode:pip->mtl.cull_mode]; |
| 13033 | _sg_stats_add(metal.pipeline.num_set_cull_mode, 1); |
| 13034 | [_sg.mtl.cmd_encoder setFrontFacingWinding:pip->mtl.winding]; |
| 13035 | _sg_stats_add(metal.pipeline.num_set_front_facing_winding, 1); |
| 13036 | [_sg.mtl.cmd_encoder setStencilReferenceValue:pip->mtl.stencil_ref]; |
| 13037 | _sg_stats_add(metal.pipeline.num_set_stencil_reference_value, 1); |
| 13038 | [_sg.mtl.cmd_encoder setDepthBias:pip->cmn.depth.bias slopeScale:pip->cmn.depth.bias_slope_scale clamp:pip->cmn.depth.bias_clamp]; |
| 13039 | _sg_stats_add(metal.pipeline.num_set_depth_bias, 1); |
| 13040 | SOKOL_ASSERT(pip->mtl.rps != _SG_MTL_INVALID_SLOT_INDEX); |
| 13041 | [_sg.mtl.cmd_encoder setRenderPipelineState:_sg_mtl_id(pip->mtl.rps)]; |
| 13042 | _sg_stats_add(metal.pipeline.num_set_render_pipeline_state, 1); |
| 13043 | SOKOL_ASSERT(pip->mtl.dss != _SG_MTL_INVALID_SLOT_INDEX); |
| 13044 | [_sg.mtl.cmd_encoder setDepthStencilState:_sg_mtl_id(pip->mtl.dss)]; |
| 13045 | _sg_stats_add(metal.pipeline.num_set_depth_stencil_state, 1); |
| 13046 | } |
| 13047 | } |
| 13048 | |
| 13049 | _SOKOL_PRIVATE bool _sg_mtl_apply_bindings(_sg_bindings_t* bnd) { |
| 13050 | SOKOL_ASSERT(bnd); |
| 13051 | SOKOL_ASSERT(bnd->pip); |
| 13052 | SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder); |
| 13053 | |
| 13054 | // store index buffer binding, this will be needed later in sg_draw() |
| 13055 | _sg.mtl.state_cache.cur_indexbuffer = bnd->ib; |
| 13056 | _sg.mtl.state_cache.cur_indexbuffer_offset = bnd->ib_offset; |
| 13057 | if (bnd->ib) { |
| 13058 | SOKOL_ASSERT(bnd->pip->cmn.index_type != SG_INDEXTYPE_NONE); |
| 13059 | _sg.mtl.state_cache.cur_indexbuffer_id.id = bnd->ib->slot.id; |
| 13060 | } else { |
| 13061 | SOKOL_ASSERT(bnd->pip->cmn.index_type == SG_INDEXTYPE_NONE); |
| 13062 | _sg.mtl.state_cache.cur_indexbuffer_id.id = SG_INVALID_ID; |
| 13063 | } |
| 13064 | |
| 13065 | // apply vertex buffers |
| 13066 | for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_vbs; slot++) { |
| 13067 | const _sg_buffer_t* vb = bnd->vbs[slot]; |
| 13068 | const int vb_offset = bnd->vb_offsets[slot]; |
| 13069 | if ((_sg.mtl.state_cache.cur_vertexbuffer_ids[slot].id != vb->slot.id) || |
| 13070 | (_sg.mtl.state_cache.cur_vertexbuffer_offsets[slot] != vb_offset)) |
| 13071 | { |
| 13072 | _sg.mtl.state_cache.cur_vertexbuffer_offsets[slot] = vb_offset; |
| 13073 | const NSUInteger mtl_slot = SG_MAX_SHADERSTAGE_UBS + slot; |
| 13074 | if (_sg.mtl.state_cache.cur_vertexbuffer_ids[slot].id != vb->slot.id) { |
| 13075 | _sg.mtl.state_cache.cur_vertexbuffer_ids[slot].id = vb->slot.id; |
| 13076 | SOKOL_ASSERT(vb->mtl.buf[vb->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 13077 | [_sg.mtl.cmd_encoder setVertexBuffer:_sg_mtl_id(vb->mtl.buf[vb->cmn.active_slot]) |
| 13078 | offset:(NSUInteger)vb_offset |
| 13079 | atIndex:mtl_slot]; |
| 13080 | } else { |
| 13081 | [_sg.mtl.cmd_encoder setVertexBufferOffset:(NSUInteger)vb_offset atIndex:mtl_slot]; |
| 13082 | } |
| 13083 | _sg_stats_add(metal.bindings.num_set_vertex_buffer, 1); |
| 13084 | } |
| 13085 | } |
| 13086 | |
| 13087 | // apply vertex stage images |
| 13088 | for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_vs_imgs; slot++) { |
| 13089 | const _sg_image_t* img = bnd->vs_imgs[slot]; |
| 13090 | if (_sg.mtl.state_cache.cur_vs_image_ids[slot].id != img->slot.id) { |
| 13091 | _sg.mtl.state_cache.cur_vs_image_ids[slot].id = img->slot.id; |
| 13092 | SOKOL_ASSERT(img->mtl.tex[img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 13093 | [_sg.mtl.cmd_encoder setVertexTexture:_sg_mtl_id(img->mtl.tex[img->cmn.active_slot]) atIndex:slot]; |
| 13094 | _sg_stats_add(metal.bindings.num_set_vertex_texture, 1); |
| 13095 | } |
| 13096 | } |
| 13097 | |
| 13098 | // apply vertex stage samplers |
| 13099 | for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_vs_smps; slot++) { |
| 13100 | const _sg_sampler_t* smp = bnd->vs_smps[slot]; |
| 13101 | if (_sg.mtl.state_cache.cur_vs_sampler_ids[slot].id != smp->slot.id) { |
| 13102 | _sg.mtl.state_cache.cur_vs_sampler_ids[slot].id = smp->slot.id; |
| 13103 | SOKOL_ASSERT(smp->mtl.sampler_state != _SG_MTL_INVALID_SLOT_INDEX); |
| 13104 | [_sg.mtl.cmd_encoder setVertexSamplerState:_sg_mtl_id(smp->mtl.sampler_state) atIndex:slot]; |
| 13105 | _sg_stats_add(metal.bindings.num_set_vertex_sampler_state, 1); |
| 13106 | } |
| 13107 | } |
| 13108 | |
| 13109 | // apply vertex stage storage buffers |
| 13110 | for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_vs_sbufs; slot++) { |
| 13111 | const _sg_buffer_t* sbuf = bnd->vs_sbufs[slot]; |
| 13112 | if (_sg.mtl.state_cache.cur_vs_storagebuffer_ids[slot].id != sbuf->slot.id) { |
| 13113 | _sg.mtl.state_cache.cur_vs_storagebuffer_ids[slot].id = sbuf->slot.id; |
| 13114 | SOKOL_ASSERT(sbuf->mtl.buf[sbuf->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 13115 | const NSUInteger mtl_slot = SG_MAX_SHADERSTAGE_UBS + SG_MAX_VERTEX_BUFFERS + slot; |
| 13116 | [_sg.mtl.cmd_encoder setVertexBuffer:_sg_mtl_id(sbuf->mtl.buf[sbuf->cmn.active_slot]) offset:0 atIndex:mtl_slot]; |
| 13117 | _sg_stats_add(metal.bindings.num_set_vertex_buffer, 1); |
| 13118 | } |
| 13119 | } |
| 13120 | |
| 13121 | // apply fragment stage images |
| 13122 | for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_fs_imgs; slot++) { |
| 13123 | const _sg_image_t* img = bnd->fs_imgs[slot]; |
| 13124 | if (_sg.mtl.state_cache.cur_fs_image_ids[slot].id != img->slot.id) { |
| 13125 | _sg.mtl.state_cache.cur_fs_image_ids[slot].id = img->slot.id; |
| 13126 | SOKOL_ASSERT(img->mtl.tex[img->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 13127 | [_sg.mtl.cmd_encoder setFragmentTexture:_sg_mtl_id(img->mtl.tex[img->cmn.active_slot]) atIndex:slot]; |
| 13128 | _sg_stats_add(metal.bindings.num_set_fragment_texture, 1); |
| 13129 | } |
| 13130 | } |
| 13131 | |
| 13132 | // apply fragment stage samplers |
| 13133 | for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_fs_smps; slot++) { |
| 13134 | const _sg_sampler_t* smp = bnd->fs_smps[slot]; |
| 13135 | if (_sg.mtl.state_cache.cur_fs_sampler_ids[slot].id != smp->slot.id) { |
| 13136 | _sg.mtl.state_cache.cur_fs_sampler_ids[slot].id = smp->slot.id; |
| 13137 | SOKOL_ASSERT(smp->mtl.sampler_state != _SG_MTL_INVALID_SLOT_INDEX); |
| 13138 | [_sg.mtl.cmd_encoder setFragmentSamplerState:_sg_mtl_id(smp->mtl.sampler_state) atIndex:slot]; |
| 13139 | _sg_stats_add(metal.bindings.num_set_fragment_sampler_state, 1); |
| 13140 | } |
| 13141 | } |
| 13142 | |
| 13143 | // apply fragment stage storage buffers |
| 13144 | for (NSUInteger slot = 0; slot < (NSUInteger)bnd->num_fs_sbufs; slot++) { |
| 13145 | const _sg_buffer_t* sbuf = bnd->fs_sbufs[slot]; |
| 13146 | if (_sg.mtl.state_cache.cur_fs_storagebuffer_ids[slot].id != sbuf->slot.id) { |
| 13147 | _sg.mtl.state_cache.cur_fs_storagebuffer_ids[slot].id = sbuf->slot.id; |
| 13148 | SOKOL_ASSERT(sbuf->mtl.buf[sbuf->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 13149 | const NSUInteger mtl_slot = SG_MAX_SHADERSTAGE_UBS + slot; |
| 13150 | [_sg.mtl.cmd_encoder setFragmentBuffer:_sg_mtl_id(sbuf->mtl.buf[sbuf->cmn.active_slot]) offset:0 atIndex:mtl_slot]; |
| 13151 | _sg_stats_add(metal.bindings.num_set_fragment_buffer, 1); |
| 13152 | } |
| 13153 | } |
| 13154 | |
| 13155 | return true; |
| 13156 | } |
| 13157 | |
| 13158 | _SOKOL_PRIVATE void _sg_mtl_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) { |
| 13159 | SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder); |
| 13160 | SOKOL_ASSERT(((size_t)_sg.mtl.cur_ub_offset + data->size) <= (size_t)_sg.mtl.ub_size); |
| 13161 | SOKOL_ASSERT((_sg.mtl.cur_ub_offset & (_SG_MTL_UB_ALIGN-1)) == 0); |
| 13162 | SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline && _sg.mtl.state_cache.cur_pipeline->shader); |
| 13163 | SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline->slot.id == _sg.mtl.state_cache.cur_pipeline_id.id); |
| 13164 | SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline->shader->slot.id == _sg.mtl.state_cache.cur_pipeline->cmn.shader_id.id); |
| 13165 | SOKOL_ASSERT(ub_index < _sg.mtl.state_cache.cur_pipeline->shader->cmn.stage[stage_index].num_uniform_blocks); |
| 13166 | SOKOL_ASSERT(data->size == _sg.mtl.state_cache.cur_pipeline->shader->cmn.stage[stage_index].uniform_blocks[ub_index].size); |
| 13167 | |
| 13168 | // copy to global uniform buffer, record offset into cmd encoder, and advance offset |
| 13169 | uint8_t* dst = &_sg.mtl.cur_ub_base_ptr[_sg.mtl.cur_ub_offset]; |
| 13170 | memcpy(dst, data->ptr, data->size); |
| 13171 | if (stage_index == SG_SHADERSTAGE_VS) { |
| 13172 | [_sg.mtl.cmd_encoder setVertexBufferOffset:(NSUInteger)_sg.mtl.cur_ub_offset atIndex:(NSUInteger)ub_index]; |
| 13173 | _sg_stats_add(metal.uniforms.num_set_vertex_buffer_offset, 1); |
| 13174 | } else { |
| 13175 | [_sg.mtl.cmd_encoder setFragmentBufferOffset:(NSUInteger)_sg.mtl.cur_ub_offset atIndex:(NSUInteger)ub_index]; |
| 13176 | _sg_stats_add(metal.uniforms.num_set_fragment_buffer_offset, 1); |
| 13177 | } |
| 13178 | _sg.mtl.cur_ub_offset = _sg_roundup(_sg.mtl.cur_ub_offset + (int)data->size, _SG_MTL_UB_ALIGN); |
| 13179 | } |
| 13180 | |
| 13181 | _SOKOL_PRIVATE void _sg_mtl_draw(int base_element, int num_elements, int num_instances) { |
| 13182 | SOKOL_ASSERT(nil != _sg.mtl.cmd_encoder); |
| 13183 | SOKOL_ASSERT(_sg.mtl.state_cache.cur_pipeline && (_sg.mtl.state_cache.cur_pipeline->slot.id == _sg.mtl.state_cache.cur_pipeline_id.id)); |
| 13184 | if (SG_INDEXTYPE_NONE != _sg.mtl.state_cache.cur_pipeline->cmn.index_type) { |
| 13185 | // indexed rendering |
| 13186 | SOKOL_ASSERT(_sg.mtl.state_cache.cur_indexbuffer && (_sg.mtl.state_cache.cur_indexbuffer->slot.id == _sg.mtl.state_cache.cur_indexbuffer_id.id)); |
| 13187 | const _sg_buffer_t* ib = _sg.mtl.state_cache.cur_indexbuffer; |
| 13188 | SOKOL_ASSERT(ib->mtl.buf[ib->cmn.active_slot] != _SG_MTL_INVALID_SLOT_INDEX); |
| 13189 | const NSUInteger index_buffer_offset = (NSUInteger) (_sg.mtl.state_cache.cur_indexbuffer_offset + base_element * _sg.mtl.state_cache.cur_pipeline->mtl.index_size); |
| 13190 | [_sg.mtl.cmd_encoder drawIndexedPrimitives:_sg.mtl.state_cache.cur_pipeline->mtl.prim_type |
| 13191 | indexCount:(NSUInteger)num_elements |
| 13192 | indexType:_sg.mtl.state_cache.cur_pipeline->mtl.index_type |
| 13193 | indexBuffer:_sg_mtl_id(ib->mtl.buf[ib->cmn.active_slot]) |
| 13194 | indexBufferOffset:index_buffer_offset |
| 13195 | instanceCount:(NSUInteger)num_instances]; |
| 13196 | } else { |
| 13197 | // non-indexed rendering |
| 13198 | [_sg.mtl.cmd_encoder drawPrimitives:_sg.mtl.state_cache.cur_pipeline->mtl.prim_type |
| 13199 | vertexStart:(NSUInteger)base_element |
| 13200 | vertexCount:(NSUInteger)num_elements |
| 13201 | instanceCount:(NSUInteger)num_instances]; |
| 13202 | } |
| 13203 | } |
| 13204 | |
| 13205 | _SOKOL_PRIVATE void _sg_mtl_update_buffer(_sg_buffer_t* buf, const sg_range* data) { |
| 13206 | SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0)); |
| 13207 | if (++buf->cmn.active_slot >= buf->cmn.num_slots) { |
| 13208 | buf->cmn.active_slot = 0; |
| 13209 | } |
| 13210 | __unsafe_unretained id<MTLBuffer> mtl_buf = _sg_mtl_id(buf->mtl.buf[buf->cmn.active_slot]); |
| 13211 | void* dst_ptr = [mtl_buf contents]; |
| 13212 | memcpy(dst_ptr, data->ptr, data->size); |
| 13213 | #if defined(_SG_TARGET_MACOS) |
| 13214 | if (_sg_mtl_resource_options_storage_mode_managed_or_shared() == MTLResourceStorageModeManaged) { |
| 13215 | [mtl_buf didModifyRange:NSMakeRange(0, data->size)]; |
| 13216 | } |
| 13217 | #endif |
| 13218 | } |
| 13219 | |
| 13220 | _SOKOL_PRIVATE void _sg_mtl_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) { |
| 13221 | SOKOL_ASSERT(buf && data && data->ptr && (data->size > 0)); |
| 13222 | if (new_frame) { |
| 13223 | if (++buf->cmn.active_slot >= buf->cmn.num_slots) { |
| 13224 | buf->cmn.active_slot = 0; |
| 13225 | } |
| 13226 | } |
| 13227 | __unsafe_unretained id<MTLBuffer> mtl_buf = _sg_mtl_id(buf->mtl.buf[buf->cmn.active_slot]); |
| 13228 | uint8_t* dst_ptr = (uint8_t*) [mtl_buf contents]; |
| 13229 | dst_ptr += buf->cmn.append_pos; |
| 13230 | memcpy(dst_ptr, data->ptr, data->size); |
| 13231 | #if defined(_SG_TARGET_MACOS) |
| 13232 | if (_sg_mtl_resource_options_storage_mode_managed_or_shared() == MTLResourceStorageModeManaged) { |
| 13233 | [mtl_buf didModifyRange:NSMakeRange((NSUInteger)buf->cmn.append_pos, (NSUInteger)data->size)]; |
| 13234 | } |
| 13235 | #endif |
| 13236 | } |
| 13237 | |
| 13238 | _SOKOL_PRIVATE void _sg_mtl_update_image(_sg_image_t* img, const sg_image_data* data) { |
| 13239 | SOKOL_ASSERT(img && data); |
| 13240 | if (++img->cmn.active_slot >= img->cmn.num_slots) { |
| 13241 | img->cmn.active_slot = 0; |
| 13242 | } |
| 13243 | __unsafe_unretained id<MTLTexture> mtl_tex = _sg_mtl_id(img->mtl.tex[img->cmn.active_slot]); |
| 13244 | _sg_mtl_copy_image_data(img, mtl_tex, data); |
| 13245 | } |
| 13246 | |
| 13247 | _SOKOL_PRIVATE void _sg_mtl_push_debug_group(const char* name) { |
| 13248 | SOKOL_ASSERT(name); |
| 13249 | if (_sg.mtl.cmd_encoder) { |
| 13250 | [_sg.mtl.cmd_encoder pushDebugGroup:[NSString stringWithUTF8String:name]]; |
| 13251 | } |
| 13252 | } |
| 13253 | |
| 13254 | _SOKOL_PRIVATE void _sg_mtl_pop_debug_group(void) { |
| 13255 | if (_sg.mtl.cmd_encoder) { |
| 13256 | [_sg.mtl.cmd_encoder popDebugGroup]; |
| 13257 | } |
| 13258 | } |
| 13259 | |
| 13260 | // ██ ██ ███████ ██████ ██████ ██████ ██ ██ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████ |
| 13261 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ |
| 13262 | // ██ █ ██ █████ ██████ ██ ███ ██████ ██ ██ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██ |
| 13263 | // ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 13264 | // ███ ███ ███████ ██████ ██████ ██ ██████ ██████ ██ ██ ██████ ██ ██ ███████ ██ ████ ██████ |
| 13265 | // |
| 13266 | // >>webgpu |
| 13267 | // >>wgpu |
| 13268 | #elif defined(SOKOL_WGPU) |
| 13269 | |
| 13270 | _SOKOL_PRIVATE WGPUBufferUsageFlags _sg_wgpu_buffer_usage(sg_buffer_type t, sg_usage u) { |
| 13271 | WGPUBufferUsageFlags res = 0; |
| 13272 | if (SG_BUFFERTYPE_VERTEXBUFFER == t) { |
| 13273 | res = WGPUBufferUsage_Vertex; |
| 13274 | } else if (SG_BUFFERTYPE_STORAGEBUFFER == t) { |
| 13275 | res = WGPUBufferUsage_Storage; |
| 13276 | } else { |
| 13277 | res = WGPUBufferUsage_Index; |
| 13278 | } |
| 13279 | if (SG_USAGE_IMMUTABLE != u) { |
| 13280 | res |= WGPUBufferUsage_CopyDst; |
| 13281 | } |
| 13282 | return res; |
| 13283 | } |
| 13284 | |
| 13285 | _SOKOL_PRIVATE WGPULoadOp _sg_wgpu_load_op(WGPUTextureView view, sg_load_action a) { |
| 13286 | if (0 == view) { |
| 13287 | return WGPULoadOp_Undefined; |
| 13288 | } else switch (a) { |
| 13289 | case SG_LOADACTION_CLEAR: |
| 13290 | case SG_LOADACTION_DONTCARE: |
| 13291 | return WGPULoadOp_Clear; |
| 13292 | case SG_LOADACTION_LOAD: |
| 13293 | return WGPULoadOp_Load; |
| 13294 | default: |
| 13295 | SOKOL_UNREACHABLE; |
| 13296 | return WGPULoadOp_Force32; |
| 13297 | } |
| 13298 | } |
| 13299 | |
| 13300 | _SOKOL_PRIVATE WGPUStoreOp _sg_wgpu_store_op(WGPUTextureView view, sg_store_action a) { |
| 13301 | if (0 == view) { |
| 13302 | return WGPUStoreOp_Undefined; |
| 13303 | } else switch (a) { |
| 13304 | case SG_STOREACTION_STORE: |
| 13305 | return WGPUStoreOp_Store; |
| 13306 | case SG_STOREACTION_DONTCARE: |
| 13307 | return WGPUStoreOp_Discard; |
| 13308 | default: |
| 13309 | SOKOL_UNREACHABLE; |
| 13310 | return WGPUStoreOp_Force32; |
| 13311 | } |
| 13312 | } |
| 13313 | |
| 13314 | _SOKOL_PRIVATE WGPUTextureViewDimension _sg_wgpu_texture_view_dimension(sg_image_type t) { |
| 13315 | switch (t) { |
| 13316 | case SG_IMAGETYPE_2D: return WGPUTextureViewDimension_2D; |
| 13317 | case SG_IMAGETYPE_CUBE: return WGPUTextureViewDimension_Cube; |
| 13318 | case SG_IMAGETYPE_3D: return WGPUTextureViewDimension_3D; |
| 13319 | case SG_IMAGETYPE_ARRAY: return WGPUTextureViewDimension_2DArray; |
| 13320 | default: SOKOL_UNREACHABLE; return WGPUTextureViewDimension_Force32; |
| 13321 | } |
| 13322 | } |
| 13323 | |
| 13324 | _SOKOL_PRIVATE WGPUTextureDimension _sg_wgpu_texture_dimension(sg_image_type t) { |
| 13325 | if (SG_IMAGETYPE_3D == t) { |
| 13326 | return WGPUTextureDimension_3D; |
| 13327 | } else { |
| 13328 | return WGPUTextureDimension_2D; |
| 13329 | } |
| 13330 | } |
| 13331 | |
| 13332 | _SOKOL_PRIVATE WGPUTextureSampleType _sg_wgpu_texture_sample_type(sg_image_sample_type t) { |
| 13333 | switch (t) { |
| 13334 | case SG_IMAGESAMPLETYPE_FLOAT: return WGPUTextureSampleType_Float; |
| 13335 | case SG_IMAGESAMPLETYPE_DEPTH: return WGPUTextureSampleType_Depth; |
| 13336 | case SG_IMAGESAMPLETYPE_SINT: return WGPUTextureSampleType_Sint; |
| 13337 | case SG_IMAGESAMPLETYPE_UINT: return WGPUTextureSampleType_Uint; |
| 13338 | case SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT: return WGPUTextureSampleType_UnfilterableFloat; |
| 13339 | default: SOKOL_UNREACHABLE; return WGPUTextureSampleType_Force32; |
| 13340 | } |
| 13341 | } |
| 13342 | |
| 13343 | _SOKOL_PRIVATE WGPUSamplerBindingType _sg_wgpu_sampler_binding_type(sg_sampler_type t) { |
| 13344 | switch (t) { |
| 13345 | case SG_SAMPLERTYPE_FILTERING: return WGPUSamplerBindingType_Filtering; |
| 13346 | case SG_SAMPLERTYPE_COMPARISON: return WGPUSamplerBindingType_Comparison; |
| 13347 | case SG_SAMPLERTYPE_NONFILTERING: return WGPUSamplerBindingType_NonFiltering; |
| 13348 | default: SOKOL_UNREACHABLE; return WGPUSamplerBindingType_Force32; |
| 13349 | } |
| 13350 | } |
| 13351 | |
| 13352 | _SOKOL_PRIVATE WGPUAddressMode _sg_wgpu_sampler_address_mode(sg_wrap m) { |
| 13353 | switch (m) { |
| 13354 | case SG_WRAP_REPEAT: |
| 13355 | return WGPUAddressMode_Repeat; |
| 13356 | case SG_WRAP_CLAMP_TO_EDGE: |
| 13357 | case SG_WRAP_CLAMP_TO_BORDER: |
| 13358 | return WGPUAddressMode_ClampToEdge; |
| 13359 | case SG_WRAP_MIRRORED_REPEAT: |
| 13360 | return WGPUAddressMode_MirrorRepeat; |
| 13361 | default: |
| 13362 | SOKOL_UNREACHABLE; |
| 13363 | return WGPUAddressMode_Force32; |
| 13364 | } |
| 13365 | } |
| 13366 | |
| 13367 | _SOKOL_PRIVATE WGPUFilterMode _sg_wgpu_sampler_minmag_filter(sg_filter f) { |
| 13368 | switch (f) { |
| 13369 | case SG_FILTER_NEAREST: |
| 13370 | return WGPUFilterMode_Nearest; |
| 13371 | case SG_FILTER_LINEAR: |
| 13372 | return WGPUFilterMode_Linear; |
| 13373 | default: |
| 13374 | SOKOL_UNREACHABLE; |
| 13375 | return WGPUFilterMode_Force32; |
| 13376 | } |
| 13377 | } |
| 13378 | |
| 13379 | _SOKOL_PRIVATE WGPUMipmapFilterMode _sg_wgpu_sampler_mipmap_filter(sg_filter f) { |
| 13380 | switch (f) { |
| 13381 | case SG_FILTER_NONE: |
| 13382 | case SG_FILTER_NEAREST: |
| 13383 | return WGPUMipmapFilterMode_Nearest; |
| 13384 | case SG_FILTER_LINEAR: |
| 13385 | return WGPUMipmapFilterMode_Linear; |
| 13386 | default: |
| 13387 | SOKOL_UNREACHABLE; |
| 13388 | return WGPUMipmapFilterMode_Force32; |
| 13389 | } |
| 13390 | } |
| 13391 | |
| 13392 | _SOKOL_PRIVATE WGPUIndexFormat _sg_wgpu_indexformat(sg_index_type t) { |
| 13393 | // NOTE: there's no WGPUIndexFormat_None |
| 13394 | return (t == SG_INDEXTYPE_UINT16) ? WGPUIndexFormat_Uint16 : WGPUIndexFormat_Uint32; |
| 13395 | } |
| 13396 | |
| 13397 | _SOKOL_PRIVATE WGPUIndexFormat _sg_wgpu_stripindexformat(sg_primitive_type prim_type, sg_index_type idx_type) { |
| 13398 | if (idx_type == SG_INDEXTYPE_NONE) { |
| 13399 | return WGPUIndexFormat_Undefined; |
| 13400 | } else if ((prim_type == SG_PRIMITIVETYPE_LINE_STRIP) || (prim_type == SG_PRIMITIVETYPE_TRIANGLE_STRIP)) { |
| 13401 | return _sg_wgpu_indexformat(idx_type); |
| 13402 | } else { |
| 13403 | return WGPUIndexFormat_Undefined; |
| 13404 | } |
| 13405 | } |
| 13406 | |
| 13407 | _SOKOL_PRIVATE WGPUVertexStepMode _sg_wgpu_stepmode(sg_vertex_step s) { |
| 13408 | return (s == SG_VERTEXSTEP_PER_VERTEX) ? WGPUVertexStepMode_Vertex : WGPUVertexStepMode_Instance; |
| 13409 | } |
| 13410 | |
| 13411 | _SOKOL_PRIVATE WGPUVertexFormat _sg_wgpu_vertexformat(sg_vertex_format f) { |
| 13412 | switch (f) { |
| 13413 | case SG_VERTEXFORMAT_FLOAT: return WGPUVertexFormat_Float32; |
| 13414 | case SG_VERTEXFORMAT_FLOAT2: return WGPUVertexFormat_Float32x2; |
| 13415 | case SG_VERTEXFORMAT_FLOAT3: return WGPUVertexFormat_Float32x3; |
| 13416 | case SG_VERTEXFORMAT_FLOAT4: return WGPUVertexFormat_Float32x4; |
| 13417 | case SG_VERTEXFORMAT_BYTE4: return WGPUVertexFormat_Sint8x4; |
| 13418 | case SG_VERTEXFORMAT_BYTE4N: return WGPUVertexFormat_Snorm8x4; |
| 13419 | case SG_VERTEXFORMAT_UBYTE4: return WGPUVertexFormat_Uint8x4; |
| 13420 | case SG_VERTEXFORMAT_UBYTE4N: return WGPUVertexFormat_Unorm8x4; |
| 13421 | case SG_VERTEXFORMAT_SHORT2: return WGPUVertexFormat_Sint16x2; |
| 13422 | case SG_VERTEXFORMAT_SHORT2N: return WGPUVertexFormat_Snorm16x2; |
| 13423 | case SG_VERTEXFORMAT_USHORT2N: return WGPUVertexFormat_Unorm16x2; |
| 13424 | case SG_VERTEXFORMAT_SHORT4: return WGPUVertexFormat_Sint16x4; |
| 13425 | case SG_VERTEXFORMAT_SHORT4N: return WGPUVertexFormat_Snorm16x4; |
| 13426 | case SG_VERTEXFORMAT_USHORT4N: return WGPUVertexFormat_Unorm16x4; |
| 13427 | case SG_VERTEXFORMAT_HALF2: return WGPUVertexFormat_Float16x2; |
| 13428 | case SG_VERTEXFORMAT_HALF4: return WGPUVertexFormat_Float16x4; |
| 13429 | // FIXME! UINT10_N2 (see https://github.com/gpuweb/gpuweb/issues/4275) |
| 13430 | case SG_VERTEXFORMAT_UINT10_N2: return WGPUVertexFormat_Undefined; |
| 13431 | default: |
| 13432 | SOKOL_UNREACHABLE; |
| 13433 | return WGPUVertexFormat_Force32; |
| 13434 | } |
| 13435 | } |
| 13436 | |
| 13437 | _SOKOL_PRIVATE WGPUPrimitiveTopology _sg_wgpu_topology(sg_primitive_type t) { |
| 13438 | switch (t) { |
| 13439 | case SG_PRIMITIVETYPE_POINTS: return WGPUPrimitiveTopology_PointList; |
| 13440 | case SG_PRIMITIVETYPE_LINES: return WGPUPrimitiveTopology_LineList; |
| 13441 | case SG_PRIMITIVETYPE_LINE_STRIP: return WGPUPrimitiveTopology_LineStrip; |
| 13442 | case SG_PRIMITIVETYPE_TRIANGLES: return WGPUPrimitiveTopology_TriangleList; |
| 13443 | case SG_PRIMITIVETYPE_TRIANGLE_STRIP: return WGPUPrimitiveTopology_TriangleStrip; |
| 13444 | default: |
| 13445 | SOKOL_UNREACHABLE; |
| 13446 | return WGPUPrimitiveTopology_Force32; |
| 13447 | } |
| 13448 | } |
| 13449 | |
| 13450 | _SOKOL_PRIVATE WGPUFrontFace _sg_wgpu_frontface(sg_face_winding fw) { |
| 13451 | return (fw == SG_FACEWINDING_CCW) ? WGPUFrontFace_CCW : WGPUFrontFace_CW; |
| 13452 | } |
| 13453 | |
| 13454 | _SOKOL_PRIVATE WGPUCullMode _sg_wgpu_cullmode(sg_cull_mode cm) { |
| 13455 | switch (cm) { |
| 13456 | case SG_CULLMODE_NONE: return WGPUCullMode_None; |
| 13457 | case SG_CULLMODE_FRONT: return WGPUCullMode_Front; |
| 13458 | case SG_CULLMODE_BACK: return WGPUCullMode_Back; |
| 13459 | default: |
| 13460 | SOKOL_UNREACHABLE; |
| 13461 | return WGPUCullMode_Force32; |
| 13462 | } |
| 13463 | } |
| 13464 | |
| 13465 | _SOKOL_PRIVATE WGPUTextureFormat _sg_wgpu_textureformat(sg_pixel_format p) { |
| 13466 | switch (p) { |
| 13467 | case SG_PIXELFORMAT_NONE: return WGPUTextureFormat_Undefined; |
| 13468 | case SG_PIXELFORMAT_R8: return WGPUTextureFormat_R8Unorm; |
| 13469 | case SG_PIXELFORMAT_R8SN: return WGPUTextureFormat_R8Snorm; |
| 13470 | case SG_PIXELFORMAT_R8UI: return WGPUTextureFormat_R8Uint; |
| 13471 | case SG_PIXELFORMAT_R8SI: return WGPUTextureFormat_R8Sint; |
| 13472 | case SG_PIXELFORMAT_R16UI: return WGPUTextureFormat_R16Uint; |
| 13473 | case SG_PIXELFORMAT_R16SI: return WGPUTextureFormat_R16Sint; |
| 13474 | case SG_PIXELFORMAT_R16F: return WGPUTextureFormat_R16Float; |
| 13475 | case SG_PIXELFORMAT_RG8: return WGPUTextureFormat_RG8Unorm; |
| 13476 | case SG_PIXELFORMAT_RG8SN: return WGPUTextureFormat_RG8Snorm; |
| 13477 | case SG_PIXELFORMAT_RG8UI: return WGPUTextureFormat_RG8Uint; |
| 13478 | case SG_PIXELFORMAT_RG8SI: return WGPUTextureFormat_RG8Sint; |
| 13479 | case SG_PIXELFORMAT_R32UI: return WGPUTextureFormat_R32Uint; |
| 13480 | case SG_PIXELFORMAT_R32SI: return WGPUTextureFormat_R32Sint; |
| 13481 | case SG_PIXELFORMAT_R32F: return WGPUTextureFormat_R32Float; |
| 13482 | case SG_PIXELFORMAT_RG16UI: return WGPUTextureFormat_RG16Uint; |
| 13483 | case SG_PIXELFORMAT_RG16SI: return WGPUTextureFormat_RG16Sint; |
| 13484 | case SG_PIXELFORMAT_RG16F: return WGPUTextureFormat_RG16Float; |
| 13485 | case SG_PIXELFORMAT_RGBA8: return WGPUTextureFormat_RGBA8Unorm; |
| 13486 | case SG_PIXELFORMAT_SRGB8A8: return WGPUTextureFormat_RGBA8UnormSrgb; |
| 13487 | case SG_PIXELFORMAT_RGBA8SN: return WGPUTextureFormat_RGBA8Snorm; |
| 13488 | case SG_PIXELFORMAT_RGBA8UI: return WGPUTextureFormat_RGBA8Uint; |
| 13489 | case SG_PIXELFORMAT_RGBA8SI: return WGPUTextureFormat_RGBA8Sint; |
| 13490 | case SG_PIXELFORMAT_BGRA8: return WGPUTextureFormat_BGRA8Unorm; |
| 13491 | case SG_PIXELFORMAT_RGB10A2: return WGPUTextureFormat_RGB10A2Unorm; |
| 13492 | case SG_PIXELFORMAT_RG11B10F: return WGPUTextureFormat_RG11B10Ufloat; |
| 13493 | case SG_PIXELFORMAT_RG32UI: return WGPUTextureFormat_RG32Uint; |
| 13494 | case SG_PIXELFORMAT_RG32SI: return WGPUTextureFormat_RG32Sint; |
| 13495 | case SG_PIXELFORMAT_RG32F: return WGPUTextureFormat_RG32Float; |
| 13496 | case SG_PIXELFORMAT_RGBA16UI: return WGPUTextureFormat_RGBA16Uint; |
| 13497 | case SG_PIXELFORMAT_RGBA16SI: return WGPUTextureFormat_RGBA16Sint; |
| 13498 | case SG_PIXELFORMAT_RGBA16F: return WGPUTextureFormat_RGBA16Float; |
| 13499 | case SG_PIXELFORMAT_RGBA32UI: return WGPUTextureFormat_RGBA32Uint; |
| 13500 | case SG_PIXELFORMAT_RGBA32SI: return WGPUTextureFormat_RGBA32Sint; |
| 13501 | case SG_PIXELFORMAT_RGBA32F: return WGPUTextureFormat_RGBA32Float; |
| 13502 | case SG_PIXELFORMAT_DEPTH: return WGPUTextureFormat_Depth32Float; |
| 13503 | case SG_PIXELFORMAT_DEPTH_STENCIL: return WGPUTextureFormat_Depth32FloatStencil8; |
| 13504 | case SG_PIXELFORMAT_BC1_RGBA: return WGPUTextureFormat_BC1RGBAUnorm; |
| 13505 | case SG_PIXELFORMAT_BC2_RGBA: return WGPUTextureFormat_BC2RGBAUnorm; |
| 13506 | case SG_PIXELFORMAT_BC3_RGBA: return WGPUTextureFormat_BC3RGBAUnorm; |
| 13507 | case SG_PIXELFORMAT_BC3_SRGBA: return WGPUTextureFormat_BC3RGBAUnormSrgb; |
| 13508 | case SG_PIXELFORMAT_BC4_R: return WGPUTextureFormat_BC4RUnorm; |
| 13509 | case SG_PIXELFORMAT_BC4_RSN: return WGPUTextureFormat_BC4RSnorm; |
| 13510 | case SG_PIXELFORMAT_BC5_RG: return WGPUTextureFormat_BC5RGUnorm; |
| 13511 | case SG_PIXELFORMAT_BC5_RGSN: return WGPUTextureFormat_BC5RGSnorm; |
| 13512 | case SG_PIXELFORMAT_BC6H_RGBF: return WGPUTextureFormat_BC6HRGBFloat; |
| 13513 | case SG_PIXELFORMAT_BC6H_RGBUF: return WGPUTextureFormat_BC6HRGBUfloat; |
| 13514 | case SG_PIXELFORMAT_BC7_RGBA: return WGPUTextureFormat_BC7RGBAUnorm; |
| 13515 | case SG_PIXELFORMAT_BC7_SRGBA: return WGPUTextureFormat_BC7RGBAUnormSrgb; |
| 13516 | case SG_PIXELFORMAT_ETC2_RGB8: return WGPUTextureFormat_ETC2RGB8Unorm; |
| 13517 | case SG_PIXELFORMAT_ETC2_RGB8A1: return WGPUTextureFormat_ETC2RGB8A1Unorm; |
| 13518 | case SG_PIXELFORMAT_ETC2_RGBA8: return WGPUTextureFormat_ETC2RGBA8Unorm; |
| 13519 | case SG_PIXELFORMAT_ETC2_SRGB8: return WGPUTextureFormat_ETC2RGB8UnormSrgb; |
| 13520 | case SG_PIXELFORMAT_ETC2_SRGB8A8: return WGPUTextureFormat_ETC2RGBA8UnormSrgb; |
| 13521 | case SG_PIXELFORMAT_ETC2_RG11: return WGPUTextureFormat_EACR11Unorm; |
| 13522 | case SG_PIXELFORMAT_ETC2_RG11SN: return WGPUTextureFormat_EACR11Snorm; |
| 13523 | case SG_PIXELFORMAT_RGB9E5: return WGPUTextureFormat_RGB9E5Ufloat; |
| 13524 | case SG_PIXELFORMAT_ASTC_4x4_RGBA: return WGPUTextureFormat_ASTC4x4Unorm; |
| 13525 | case SG_PIXELFORMAT_ASTC_4x4_SRGBA: return WGPUTextureFormat_ASTC4x4UnormSrgb; |
| 13526 | // NOT SUPPORTED |
| 13527 | case SG_PIXELFORMAT_R16: |
| 13528 | case SG_PIXELFORMAT_R16SN: |
| 13529 | case SG_PIXELFORMAT_RG16: |
| 13530 | case SG_PIXELFORMAT_RG16SN: |
| 13531 | case SG_PIXELFORMAT_RGBA16: |
| 13532 | case SG_PIXELFORMAT_RGBA16SN: |
| 13533 | case SG_PIXELFORMAT_PVRTC_RGB_2BPP: |
| 13534 | case SG_PIXELFORMAT_PVRTC_RGB_4BPP: |
| 13535 | case SG_PIXELFORMAT_PVRTC_RGBA_2BPP: |
| 13536 | case SG_PIXELFORMAT_PVRTC_RGBA_4BPP: |
| 13537 | return WGPUTextureFormat_Undefined; |
| 13538 | |
| 13539 | default: |
| 13540 | SOKOL_UNREACHABLE; |
| 13541 | return WGPUTextureFormat_Force32; |
| 13542 | } |
| 13543 | } |
| 13544 | |
| 13545 | _SOKOL_PRIVATE WGPUCompareFunction _sg_wgpu_comparefunc(sg_compare_func f) { |
| 13546 | switch (f) { |
| 13547 | case SG_COMPAREFUNC_NEVER: return WGPUCompareFunction_Never; |
| 13548 | case SG_COMPAREFUNC_LESS: return WGPUCompareFunction_Less; |
| 13549 | case SG_COMPAREFUNC_EQUAL: return WGPUCompareFunction_Equal; |
| 13550 | case SG_COMPAREFUNC_LESS_EQUAL: return WGPUCompareFunction_LessEqual; |
| 13551 | case SG_COMPAREFUNC_GREATER: return WGPUCompareFunction_Greater; |
| 13552 | case SG_COMPAREFUNC_NOT_EQUAL: return WGPUCompareFunction_NotEqual; |
| 13553 | case SG_COMPAREFUNC_GREATER_EQUAL: return WGPUCompareFunction_GreaterEqual; |
| 13554 | case SG_COMPAREFUNC_ALWAYS: return WGPUCompareFunction_Always; |
| 13555 | default: |
| 13556 | SOKOL_UNREACHABLE; |
| 13557 | return WGPUCompareFunction_Force32; |
| 13558 | } |
| 13559 | } |
| 13560 | |
| 13561 | _SOKOL_PRIVATE WGPUStencilOperation _sg_wgpu_stencilop(sg_stencil_op op) { |
| 13562 | switch (op) { |
| 13563 | case SG_STENCILOP_KEEP: return WGPUStencilOperation_Keep; |
| 13564 | case SG_STENCILOP_ZERO: return WGPUStencilOperation_Zero; |
| 13565 | case SG_STENCILOP_REPLACE: return WGPUStencilOperation_Replace; |
| 13566 | case SG_STENCILOP_INCR_CLAMP: return WGPUStencilOperation_IncrementClamp; |
| 13567 | case SG_STENCILOP_DECR_CLAMP: return WGPUStencilOperation_DecrementClamp; |
| 13568 | case SG_STENCILOP_INVERT: return WGPUStencilOperation_Invert; |
| 13569 | case SG_STENCILOP_INCR_WRAP: return WGPUStencilOperation_IncrementWrap; |
| 13570 | case SG_STENCILOP_DECR_WRAP: return WGPUStencilOperation_DecrementWrap; |
| 13571 | default: |
| 13572 | SOKOL_UNREACHABLE; |
| 13573 | return WGPUStencilOperation_Force32; |
| 13574 | } |
| 13575 | } |
| 13576 | |
| 13577 | _SOKOL_PRIVATE WGPUBlendOperation _sg_wgpu_blendop(sg_blend_op op) { |
| 13578 | switch (op) { |
| 13579 | case SG_BLENDOP_ADD: return WGPUBlendOperation_Add; |
| 13580 | case SG_BLENDOP_SUBTRACT: return WGPUBlendOperation_Subtract; |
| 13581 | case SG_BLENDOP_REVERSE_SUBTRACT: return WGPUBlendOperation_ReverseSubtract; |
| 13582 | default: |
| 13583 | SOKOL_UNREACHABLE; |
| 13584 | return WGPUBlendOperation_Force32; |
| 13585 | } |
| 13586 | } |
| 13587 | |
| 13588 | _SOKOL_PRIVATE WGPUBlendFactor _sg_wgpu_blendfactor(sg_blend_factor f) { |
| 13589 | switch (f) { |
| 13590 | case SG_BLENDFACTOR_ZERO: return WGPUBlendFactor_Zero; |
| 13591 | case SG_BLENDFACTOR_ONE: return WGPUBlendFactor_One; |
| 13592 | case SG_BLENDFACTOR_SRC_COLOR: return WGPUBlendFactor_Src; |
| 13593 | case SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR: return WGPUBlendFactor_OneMinusSrc; |
| 13594 | case SG_BLENDFACTOR_SRC_ALPHA: return WGPUBlendFactor_SrcAlpha; |
| 13595 | case SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA: return WGPUBlendFactor_OneMinusSrcAlpha; |
| 13596 | case SG_BLENDFACTOR_DST_COLOR: return WGPUBlendFactor_Dst; |
| 13597 | case SG_BLENDFACTOR_ONE_MINUS_DST_COLOR: return WGPUBlendFactor_OneMinusDst; |
| 13598 | case SG_BLENDFACTOR_DST_ALPHA: return WGPUBlendFactor_DstAlpha; |
| 13599 | case SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA: return WGPUBlendFactor_OneMinusDstAlpha; |
| 13600 | case SG_BLENDFACTOR_SRC_ALPHA_SATURATED: return WGPUBlendFactor_SrcAlphaSaturated; |
| 13601 | case SG_BLENDFACTOR_BLEND_COLOR: return WGPUBlendFactor_Constant; |
| 13602 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR: return WGPUBlendFactor_OneMinusConstant; |
| 13603 | // FIXME: separate blend alpha value not supported? |
| 13604 | case SG_BLENDFACTOR_BLEND_ALPHA: return WGPUBlendFactor_Constant; |
| 13605 | case SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA: return WGPUBlendFactor_OneMinusConstant; |
| 13606 | default: |
| 13607 | SOKOL_UNREACHABLE; |
| 13608 | return WGPUBlendFactor_Force32; |
| 13609 | } |
| 13610 | } |
| 13611 | |
| 13612 | _SOKOL_PRIVATE WGPUColorWriteMaskFlags _sg_wgpu_colorwritemask(uint8_t m) { |
| 13613 | WGPUColorWriteMaskFlags res = 0; |
| 13614 | if (0 != (m & SG_COLORMASK_R)) { |
| 13615 | res |= WGPUColorWriteMask_Red; |
| 13616 | } |
| 13617 | if (0 != (m & SG_COLORMASK_G)) { |
| 13618 | res |= WGPUColorWriteMask_Green; |
| 13619 | } |
| 13620 | if (0 != (m & SG_COLORMASK_B)) { |
| 13621 | res |= WGPUColorWriteMask_Blue; |
| 13622 | } |
| 13623 | if (0 != (m & SG_COLORMASK_A)) { |
| 13624 | res |= WGPUColorWriteMask_Alpha; |
| 13625 | } |
| 13626 | return res; |
| 13627 | } |
| 13628 | |
| 13629 | // image/sampler binding on wgpu follows this convention: |
| 13630 | // |
| 13631 | // - all images and sampler are in @group(1) |
| 13632 | // - vertex stage images start at @binding(0) |
| 13633 | // - vertex stage samplers start at @binding(16) |
| 13634 | // - vertex stage storage buffers start at @binding(32) |
| 13635 | // - fragment stage images start at @binding(48) |
| 13636 | // - fragment stage samplers start at @binding(64) |
| 13637 | // - fragment stage storage buffers start at @binding(80) |
| 13638 | // |
| 13639 | _SOKOL_PRIVATE uint32_t _sg_wgpu_image_binding(sg_shader_stage stage, int img_slot) { |
| 13640 | SOKOL_ASSERT((img_slot >= 0) && (img_slot < 16)); |
| 13641 | if (SG_SHADERSTAGE_VS == stage) { |
| 13642 | return 0 + (uint32_t)img_slot; |
| 13643 | } else { |
| 13644 | return 48 + (uint32_t)img_slot; |
| 13645 | } |
| 13646 | } |
| 13647 | |
| 13648 | _SOKOL_PRIVATE uint32_t _sg_wgpu_sampler_binding(sg_shader_stage stage, int smp_slot) { |
| 13649 | SOKOL_ASSERT((smp_slot >= 0) && (smp_slot < 16)); |
| 13650 | if (SG_SHADERSTAGE_VS == stage) { |
| 13651 | return 16 + (uint32_t)smp_slot; |
| 13652 | } else { |
| 13653 | return 64 + (uint32_t)smp_slot; |
| 13654 | } |
| 13655 | } |
| 13656 | |
| 13657 | _SOKOL_PRIVATE uint32_t _sg_wgpu_storagebuffer_binding(sg_shader_stage stage, int sbuf_slot) { |
| 13658 | SOKOL_ASSERT((sbuf_slot >= 0) && (sbuf_slot < 16)); |
| 13659 | if (SG_SHADERSTAGE_VS == stage) { |
| 13660 | return 32 + (uint32_t)sbuf_slot; |
| 13661 | } else { |
| 13662 | return 80 + (uint32_t)sbuf_slot; |
| 13663 | } |
| 13664 | } |
| 13665 | |
| 13666 | _SOKOL_PRIVATE WGPUShaderStage _sg_wgpu_shader_stage(sg_shader_stage stage) { |
| 13667 | switch (stage) { |
| 13668 | case SG_SHADERSTAGE_VS: return WGPUShaderStage_Vertex; |
| 13669 | case SG_SHADERSTAGE_FS: return WGPUShaderStage_Fragment; |
| 13670 | default: SOKOL_UNREACHABLE; return WGPUShaderStage_None; |
| 13671 | } |
| 13672 | } |
| 13673 | |
| 13674 | _SOKOL_PRIVATE void _sg_wgpu_init_caps(void) { |
| 13675 | _sg.backend = SG_BACKEND_WGPU; |
| 13676 | _sg.features.origin_top_left = true; |
| 13677 | _sg.features.image_clamp_to_border = false; |
| 13678 | _sg.features.mrt_independent_blend_state = true; |
| 13679 | _sg.features.mrt_independent_write_mask = true; |
| 13680 | _sg.features.storage_buffer = true; |
| 13681 | |
| 13682 | wgpuDeviceGetLimits(_sg.wgpu.dev, &_sg.wgpu.limits); |
| 13683 | |
| 13684 | const WGPULimits* l = &_sg.wgpu.limits.limits; |
| 13685 | _sg.limits.max_image_size_2d = (int) l->maxTextureDimension2D; |
| 13686 | _sg.limits.max_image_size_cube = (int) l->maxTextureDimension2D; // not a bug, see: https://github.com/gpuweb/gpuweb/issues/1327 |
| 13687 | _sg.limits.max_image_size_3d = (int) l->maxTextureDimension3D; |
| 13688 | _sg.limits.max_image_size_array = (int) l->maxTextureDimension2D; |
| 13689 | _sg.limits.max_image_array_layers = (int) l->maxTextureArrayLayers; |
| 13690 | _sg.limits.max_vertex_attrs = SG_MAX_VERTEX_ATTRIBUTES; |
| 13691 | |
| 13692 | // NOTE: no WGPUTextureFormat_R16Unorm |
| 13693 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R8]); |
| 13694 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG8]); |
| 13695 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA8]); |
| 13696 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_SRGB8A8]); |
| 13697 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_BGRA8]); |
| 13698 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_R16F]); |
| 13699 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RG16F]); |
| 13700 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGBA16F]); |
| 13701 | _sg_pixelformat_all(&_sg.formats[SG_PIXELFORMAT_RGB10A2]); |
| 13702 | |
| 13703 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_R8SN]); |
| 13704 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG8SN]); |
| 13705 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGBA8SN]); |
| 13706 | |
| 13707 | // FIXME: can be made renderable via extension |
| 13708 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RG11B10F]); |
| 13709 | |
| 13710 | // NOTE: msaa rendering is possible in WebGPU, but no resolve |
| 13711 | // which is a combination that's not currently supported in sokol-gfx |
| 13712 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R8UI]); |
| 13713 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R8SI]); |
| 13714 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG8UI]); |
| 13715 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG8SI]); |
| 13716 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA8UI]); |
| 13717 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA8SI]); |
| 13718 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R16UI]); |
| 13719 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R16SI]); |
| 13720 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG16UI]); |
| 13721 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG16SI]); |
| 13722 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA16UI]); |
| 13723 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA16SI]); |
| 13724 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32UI]); |
| 13725 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32SI]); |
| 13726 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32UI]); |
| 13727 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32SI]); |
| 13728 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32UI]); |
| 13729 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32SI]); |
| 13730 | |
| 13731 | // FIXME: can be made filterable with extension |
| 13732 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_R32F]); |
| 13733 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RG32F]); |
| 13734 | _sg_pixelformat_sr(&_sg.formats[SG_PIXELFORMAT_RGBA32F]); |
| 13735 | |
| 13736 | _sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH]); |
| 13737 | _sg_pixelformat_srmd(&_sg.formats[SG_PIXELFORMAT_DEPTH_STENCIL]); |
| 13738 | |
| 13739 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_RGB9E5]); |
| 13740 | |
| 13741 | if (wgpuDeviceHasFeature(_sg.wgpu.dev, WGPUFeatureName_TextureCompressionBC)) { |
| 13742 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC1_RGBA]); |
| 13743 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC2_RGBA]); |
| 13744 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_RGBA]); |
| 13745 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC3_SRGBA]); |
| 13746 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_R]); |
| 13747 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC4_RSN]); |
| 13748 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RG]); |
| 13749 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC5_RGSN]); |
| 13750 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBF]); |
| 13751 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC6H_RGBUF]); |
| 13752 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_RGBA]); |
| 13753 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_BC7_SRGBA]); |
| 13754 | } |
| 13755 | if (wgpuDeviceHasFeature(_sg.wgpu.dev, WGPUFeatureName_TextureCompressionETC2)) { |
| 13756 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8]); |
| 13757 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_SRGB8]); |
| 13758 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGB8A1]); |
| 13759 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RGBA8]); |
| 13760 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_SRGB8A8]); |
| 13761 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RG11]); |
| 13762 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ETC2_RG11SN]); |
| 13763 | } |
| 13764 | |
| 13765 | if (wgpuDeviceHasFeature(_sg.wgpu.dev, WGPUFeatureName_TextureCompressionASTC)) { |
| 13766 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ASTC_4x4_RGBA]); |
| 13767 | _sg_pixelformat_sf(&_sg.formats[SG_PIXELFORMAT_ASTC_4x4_SRGBA]); |
| 13768 | } |
| 13769 | } |
| 13770 | |
| 13771 | _SOKOL_PRIVATE void _sg_wgpu_uniform_buffer_init(const sg_desc* desc) { |
| 13772 | SOKOL_ASSERT(0 == _sg.wgpu.uniform.staging); |
| 13773 | SOKOL_ASSERT(0 == _sg.wgpu.uniform.buf); |
| 13774 | SOKOL_ASSERT(0 == _sg.wgpu.uniform.bind.group_layout); |
| 13775 | SOKOL_ASSERT(0 == _sg.wgpu.uniform.bind.group); |
| 13776 | |
| 13777 | // Add the max-uniform-update size (64 KB) to the requested buffer size, |
| 13778 | // this is to prevent validation errors in the WebGPU implementation |
| 13779 | // if the entire buffer size is used per frame. 64 KB is the allowed |
| 13780 | // max uniform update size on NVIDIA |
| 13781 | // |
| 13782 | // FIXME: is this still needed? |
| 13783 | _sg.wgpu.uniform.num_bytes = (uint32_t)(desc->uniform_buffer_size + _SG_WGPU_MAX_UNIFORM_UPDATE_SIZE); |
| 13784 | _sg.wgpu.uniform.staging = (uint8_t*)_sg_malloc(_sg.wgpu.uniform.num_bytes); |
| 13785 | |
| 13786 | WGPUBufferDescriptor ub_desc; |
| 13787 | _sg_clear(&ub_desc, sizeof(ub_desc)); |
| 13788 | ub_desc.size = _sg.wgpu.uniform.num_bytes; |
| 13789 | ub_desc.usage = WGPUBufferUsage_Uniform|WGPUBufferUsage_CopyDst; |
| 13790 | _sg.wgpu.uniform.buf = wgpuDeviceCreateBuffer(_sg.wgpu.dev, &ub_desc); |
| 13791 | SOKOL_ASSERT(_sg.wgpu.uniform.buf); |
| 13792 | |
| 13793 | WGPUBindGroupLayoutEntry ub_bgle_desc[SG_NUM_SHADER_STAGES][SG_MAX_SHADERSTAGE_UBS]; |
| 13794 | _sg_clear(ub_bgle_desc, sizeof(ub_bgle_desc)); |
| 13795 | for (uint32_t stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 13796 | WGPUShaderStage vis = (stage_index == SG_SHADERSTAGE_VS) ? WGPUShaderStage_Vertex : WGPUShaderStage_Fragment; |
| 13797 | for (uint32_t ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) { |
| 13798 | uint32_t bind_index = stage_index * SG_MAX_SHADERSTAGE_UBS + ub_index; |
| 13799 | ub_bgle_desc[stage_index][ub_index].binding = bind_index; |
| 13800 | ub_bgle_desc[stage_index][ub_index].visibility = vis; |
| 13801 | ub_bgle_desc[stage_index][ub_index].buffer.type = WGPUBufferBindingType_Uniform; |
| 13802 | ub_bgle_desc[stage_index][ub_index].buffer.hasDynamicOffset = true; |
| 13803 | } |
| 13804 | } |
| 13805 | |
| 13806 | WGPUBindGroupLayoutDescriptor ub_bgl_desc; |
| 13807 | _sg_clear(&ub_bgl_desc, sizeof(ub_bgl_desc)); |
| 13808 | ub_bgl_desc.entryCount = SG_NUM_SHADER_STAGES * SG_MAX_SHADERSTAGE_UBS; |
| 13809 | ub_bgl_desc.entries = &ub_bgle_desc[0][0]; |
| 13810 | _sg.wgpu.uniform.bind.group_layout = wgpuDeviceCreateBindGroupLayout(_sg.wgpu.dev, &ub_bgl_desc); |
| 13811 | SOKOL_ASSERT(_sg.wgpu.uniform.bind.group_layout); |
| 13812 | |
| 13813 | WGPUBindGroupEntry ub_bge[SG_NUM_SHADER_STAGES][SG_MAX_SHADERSTAGE_UBS]; |
| 13814 | _sg_clear(ub_bge, sizeof(ub_bge)); |
| 13815 | for (uint32_t stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 13816 | for (uint32_t ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) { |
| 13817 | uint32_t bind_index = stage_index * SG_MAX_SHADERSTAGE_UBS + ub_index; |
| 13818 | ub_bge[stage_index][ub_index].binding = bind_index; |
| 13819 | ub_bge[stage_index][ub_index].buffer = _sg.wgpu.uniform.buf; |
| 13820 | ub_bge[stage_index][ub_index].size = _SG_WGPU_MAX_UNIFORM_UPDATE_SIZE; |
| 13821 | } |
| 13822 | } |
| 13823 | WGPUBindGroupDescriptor bg_desc; |
| 13824 | _sg_clear(&bg_desc, sizeof(bg_desc)); |
| 13825 | bg_desc.layout = _sg.wgpu.uniform.bind.group_layout; |
| 13826 | bg_desc.entryCount = SG_NUM_SHADER_STAGES * SG_MAX_SHADERSTAGE_UBS; |
| 13827 | bg_desc.entries = &ub_bge[0][0]; |
| 13828 | _sg.wgpu.uniform.bind.group = wgpuDeviceCreateBindGroup(_sg.wgpu.dev, &bg_desc); |
| 13829 | SOKOL_ASSERT(_sg.wgpu.uniform.bind.group); |
| 13830 | } |
| 13831 | |
| 13832 | _SOKOL_PRIVATE void _sg_wgpu_uniform_buffer_discard(void) { |
| 13833 | if (_sg.wgpu.uniform.buf) { |
| 13834 | wgpuBufferRelease(_sg.wgpu.uniform.buf); |
| 13835 | _sg.wgpu.uniform.buf = 0; |
| 13836 | } |
| 13837 | if (_sg.wgpu.uniform.bind.group) { |
| 13838 | wgpuBindGroupRelease(_sg.wgpu.uniform.bind.group); |
| 13839 | _sg.wgpu.uniform.bind.group = 0; |
| 13840 | } |
| 13841 | if (_sg.wgpu.uniform.bind.group_layout) { |
| 13842 | wgpuBindGroupLayoutRelease(_sg.wgpu.uniform.bind.group_layout); |
| 13843 | _sg.wgpu.uniform.bind.group_layout = 0; |
| 13844 | } |
| 13845 | if (_sg.wgpu.uniform.staging) { |
| 13846 | _sg_free(_sg.wgpu.uniform.staging); |
| 13847 | _sg.wgpu.uniform.staging = 0; |
| 13848 | } |
| 13849 | } |
| 13850 | |
| 13851 | _SOKOL_PRIVATE void _sg_wgpu_uniform_buffer_on_begin_pass(void) { |
| 13852 | wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc, |
| 13853 | 0, // groupIndex 0 is reserved for uniform buffers |
| 13854 | _sg.wgpu.uniform.bind.group, |
| 13855 | SG_NUM_SHADER_STAGES * SG_MAX_SHADERSTAGE_UBS, |
| 13856 | &_sg.wgpu.uniform.bind.offsets[0][0]); |
| 13857 | } |
| 13858 | |
| 13859 | _SOKOL_PRIVATE void _sg_wgpu_uniform_buffer_on_commit(void) { |
| 13860 | wgpuQueueWriteBuffer(_sg.wgpu.queue, _sg.wgpu.uniform.buf, 0, _sg.wgpu.uniform.staging, _sg.wgpu.uniform.offset); |
| 13861 | _sg_stats_add(wgpu.uniforms.size_write_buffer, _sg.wgpu.uniform.offset); |
| 13862 | _sg.wgpu.uniform.offset = 0; |
| 13863 | _sg_clear(&_sg.wgpu.uniform.bind.offsets[0][0], sizeof(_sg.wgpu.uniform.bind.offsets)); |
| 13864 | } |
| 13865 | |
| 13866 | _SOKOL_PRIVATE void _sg_wgpu_bindgroups_pool_init(const sg_desc* desc) { |
| 13867 | SOKOL_ASSERT((desc->wgpu_bindgroups_cache_size > 0) && (desc->wgpu_bindgroups_cache_size < _SG_MAX_POOL_SIZE)); |
| 13868 | _sg_wgpu_bindgroups_pool_t* p = &_sg.wgpu.bindgroups_pool; |
| 13869 | SOKOL_ASSERT(0 == p->bindgroups); |
| 13870 | const int pool_size = desc->wgpu_bindgroups_cache_size; |
| 13871 | _sg_init_pool(&p->pool, pool_size); |
| 13872 | size_t pool_byte_size = sizeof(_sg_wgpu_bindgroup_t) * (size_t)p->pool.size; |
| 13873 | p->bindgroups = (_sg_wgpu_bindgroup_t*) _sg_malloc_clear(pool_byte_size); |
| 13874 | } |
| 13875 | |
| 13876 | _SOKOL_PRIVATE void _sg_wgpu_bindgroups_pool_discard(void) { |
| 13877 | _sg_wgpu_bindgroups_pool_t* p = &_sg.wgpu.bindgroups_pool; |
| 13878 | SOKOL_ASSERT(p->bindgroups); |
| 13879 | _sg_free(p->bindgroups); p->bindgroups = 0; |
| 13880 | _sg_discard_pool(&p->pool); |
| 13881 | } |
| 13882 | |
| 13883 | _SOKOL_PRIVATE _sg_wgpu_bindgroup_t* _sg_wgpu_bindgroup_at(uint32_t bg_id) { |
| 13884 | SOKOL_ASSERT(SG_INVALID_ID != bg_id); |
| 13885 | _sg_wgpu_bindgroups_pool_t* p = &_sg.wgpu.bindgroups_pool; |
| 13886 | int slot_index = _sg_slot_index(bg_id); |
| 13887 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->pool.size)); |
| 13888 | return &p->bindgroups[slot_index]; |
| 13889 | } |
| 13890 | |
| 13891 | _SOKOL_PRIVATE _sg_wgpu_bindgroup_t* _sg_wgpu_lookup_bindgroup(uint32_t bg_id) { |
| 13892 | if (SG_INVALID_ID != bg_id) { |
| 13893 | _sg_wgpu_bindgroup_t* bg = _sg_wgpu_bindgroup_at(bg_id); |
| 13894 | if (bg->slot.id == bg_id) { |
| 13895 | return bg; |
| 13896 | } |
| 13897 | } |
| 13898 | return 0; |
| 13899 | } |
| 13900 | |
| 13901 | _SOKOL_PRIVATE _sg_wgpu_bindgroup_handle_t _sg_wgpu_alloc_bindgroup(void) { |
| 13902 | _sg_wgpu_bindgroups_pool_t* p = &_sg.wgpu.bindgroups_pool; |
| 13903 | _sg_wgpu_bindgroup_handle_t res; |
| 13904 | int slot_index = _sg_pool_alloc_index(&p->pool); |
| 13905 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 13906 | res.id = _sg_slot_alloc(&p->pool, &p->bindgroups[slot_index].slot, slot_index); |
| 13907 | } else { |
| 13908 | res.id = SG_INVALID_ID; |
| 13909 | _SG_ERROR(WGPU_BINDGROUPS_POOL_EXHAUSTED); |
| 13910 | } |
| 13911 | return res; |
| 13912 | } |
| 13913 | |
| 13914 | _SOKOL_PRIVATE void _sg_wgpu_dealloc_bindgroup(_sg_wgpu_bindgroup_t* bg) { |
| 13915 | SOKOL_ASSERT(bg && (bg->slot.state == SG_RESOURCESTATE_ALLOC) && (bg->slot.id != SG_INVALID_ID)); |
| 13916 | _sg_wgpu_bindgroups_pool_t* p = &_sg.wgpu.bindgroups_pool; |
| 13917 | _sg_pool_free_index(&p->pool, _sg_slot_index(bg->slot.id)); |
| 13918 | _sg_reset_slot(&bg->slot); |
| 13919 | } |
| 13920 | |
| 13921 | _SOKOL_PRIVATE void _sg_wgpu_reset_bindgroup_to_alloc_state(_sg_wgpu_bindgroup_t* bg) { |
| 13922 | SOKOL_ASSERT(bg); |
| 13923 | _sg_slot_t slot = bg->slot; |
| 13924 | _sg_clear(bg, sizeof(_sg_wgpu_bindgroup_t)); |
| 13925 | bg->slot = slot; |
| 13926 | bg->slot.state = SG_RESOURCESTATE_ALLOC; |
| 13927 | } |
| 13928 | |
| 13929 | // MurmurHash64B (see: https://github.com/aappleby/smhasher/blob/61a0530f28277f2e850bfc39600ce61d02b518de/src/MurmurHash2.cpp#L142) |
| 13930 | _SOKOL_PRIVATE uint64_t _sg_wgpu_hash(const void* key, int len, uint64_t seed) { |
| 13931 | const uint32_t m = 0x5bd1e995; |
| 13932 | const int r = 24; |
| 13933 | uint32_t h1 = (uint32_t)seed ^ (uint32_t)len; |
| 13934 | uint32_t h2 = (uint32_t)(seed >> 32); |
| 13935 | const uint32_t * data = (const uint32_t *)key; |
| 13936 | while (len >= 8) { |
| 13937 | uint32_t k1 = *data++; |
| 13938 | k1 *= m; k1 ^= k1 >> r; k1 *= m; |
| 13939 | h1 *= m; h1 ^= k1; |
| 13940 | len -= 4; |
| 13941 | uint32_t k2 = *data++; |
| 13942 | k2 *= m; k2 ^= k2 >> r; k2 *= m; |
| 13943 | h2 *= m; h2 ^= k2; |
| 13944 | len -= 4; |
| 13945 | } |
| 13946 | if (len >= 4) { |
| 13947 | uint32_t k1 = *data++; |
| 13948 | k1 *= m; k1 ^= k1 >> r; k1 *= m; |
| 13949 | h1 *= m; h1 ^= k1; |
| 13950 | len -= 4; |
| 13951 | } |
| 13952 | switch(len) { |
| 13953 | case 3: h2 ^= (uint32_t)(((unsigned char*)data)[2] << 16); |
| 13954 | case 2: h2 ^= (uint32_t)(((unsigned char*)data)[1] << 8); |
| 13955 | case 1: h2 ^= ((unsigned char*)data)[0]; |
| 13956 | h2 *= m; |
| 13957 | }; |
| 13958 | h1 ^= h2 >> 18; h1 *= m; |
| 13959 | h2 ^= h1 >> 22; h2 *= m; |
| 13960 | h1 ^= h2 >> 17; h1 *= m; |
| 13961 | h2 ^= h1 >> 19; h2 *= m; |
| 13962 | uint64_t h = h1; |
| 13963 | h = (h << 32) | h2; |
| 13964 | return h; |
| 13965 | } |
| 13966 | |
| 13967 | _SOKOL_PRIVATE void _sg_wgpu_init_bindgroups_cache_key(_sg_wgpu_bindgroups_cache_key_t* key, const _sg_bindings_t* bnd) { |
| 13968 | SOKOL_ASSERT(bnd); |
| 13969 | SOKOL_ASSERT(bnd->pip); |
| 13970 | SOKOL_ASSERT(bnd->num_vs_imgs <= SG_MAX_SHADERSTAGE_IMAGES); |
| 13971 | SOKOL_ASSERT(bnd->num_vs_smps <= SG_MAX_SHADERSTAGE_SAMPLERS); |
| 13972 | SOKOL_ASSERT(bnd->num_vs_sbufs <= SG_MAX_SHADERSTAGE_STORAGEBUFFERS); |
| 13973 | SOKOL_ASSERT(bnd->num_fs_imgs <= SG_MAX_SHADERSTAGE_IMAGES); |
| 13974 | SOKOL_ASSERT(bnd->num_fs_smps <= SG_MAX_SHADERSTAGE_SAMPLERS); |
| 13975 | SOKOL_ASSERT(bnd->num_fs_sbufs <= SG_MAX_SHADERSTAGE_STORAGEBUFFERS); |
| 13976 | |
| 13977 | _sg_clear(key->items, sizeof(key->items)); |
| 13978 | key->items[0] = bnd->pip->slot.id; |
| 13979 | const int vs_imgs_offset = 1; |
| 13980 | const int vs_smps_offset = vs_imgs_offset + SG_MAX_SHADERSTAGE_IMAGES; |
| 13981 | const int vs_sbufs_offset = vs_smps_offset + SG_MAX_SHADERSTAGE_SAMPLERS; |
| 13982 | const int fs_imgs_offset = vs_sbufs_offset + SG_MAX_SHADERSTAGE_STORAGEBUFFERS; |
| 13983 | const int fs_smps_offset = fs_imgs_offset + SG_MAX_SHADERSTAGE_IMAGES; |
| 13984 | const int fs_sbufs_offset = fs_smps_offset + SG_MAX_SHADERSTAGE_SAMPLERS; |
| 13985 | SOKOL_ASSERT((fs_sbufs_offset + SG_MAX_SHADERSTAGE_STORAGEBUFFERS) == _SG_WGPU_BINDGROUPSCACHE_NUM_ITEMS); |
| 13986 | for (int i = 0; i < bnd->num_vs_imgs; i++) { |
| 13987 | SOKOL_ASSERT(bnd->vs_imgs[i]); |
| 13988 | key->items[vs_imgs_offset + i] = bnd->vs_imgs[i]->slot.id; |
| 13989 | } |
| 13990 | for (int i = 0; i < bnd->num_vs_smps; i++) { |
| 13991 | SOKOL_ASSERT(bnd->vs_smps[i]); |
| 13992 | key->items[vs_smps_offset + i] = bnd->vs_smps[i]->slot.id; |
| 13993 | } |
| 13994 | for (int i = 0; i < bnd->num_vs_sbufs; i++) { |
| 13995 | SOKOL_ASSERT(bnd->vs_sbufs[i]); |
| 13996 | key->items[vs_sbufs_offset + i] = bnd->vs_sbufs[i]->slot.id; |
| 13997 | } |
| 13998 | for (int i = 0; i < bnd->num_fs_imgs; i++) { |
| 13999 | SOKOL_ASSERT(bnd->fs_imgs[i]); |
| 14000 | key->items[fs_imgs_offset + i] = bnd->fs_imgs[i]->slot.id; |
| 14001 | } |
| 14002 | for (int i = 0; i < bnd->num_fs_smps; i++) { |
| 14003 | SOKOL_ASSERT(bnd->fs_smps[i]); |
| 14004 | key->items[fs_smps_offset + i] = bnd->fs_smps[i]->slot.id; |
| 14005 | } |
| 14006 | for (int i = 0; i < bnd->num_fs_sbufs; i++) { |
| 14007 | SOKOL_ASSERT(bnd->fs_sbufs[i]); |
| 14008 | key->items[fs_sbufs_offset + i] = bnd->fs_sbufs[i]->slot.id; |
| 14009 | } |
| 14010 | key->hash = _sg_wgpu_hash(&key->items, (int)sizeof(key->items), 0x1234567887654321); |
| 14011 | } |
| 14012 | |
| 14013 | _SOKOL_PRIVATE bool _sg_wgpu_compare_bindgroups_cache_key(_sg_wgpu_bindgroups_cache_key_t* k0, _sg_wgpu_bindgroups_cache_key_t* k1) { |
| 14014 | SOKOL_ASSERT(k0 && k1); |
| 14015 | if (k0->hash != k1->hash) { |
| 14016 | return false; |
| 14017 | } |
| 14018 | if (memcmp(&k0->items, &k1->items, sizeof(k0->items)) != 0) { |
| 14019 | _sg_stats_add(wgpu.bindings.num_bindgroup_cache_hash_vs_key_mismatch, 1); |
| 14020 | return false; |
| 14021 | } |
| 14022 | return true; |
| 14023 | } |
| 14024 | |
| 14025 | _SOKOL_PRIVATE _sg_wgpu_bindgroup_t* _sg_wgpu_create_bindgroup(_sg_bindings_t* bnd) { |
| 14026 | SOKOL_ASSERT(_sg.wgpu.dev); |
| 14027 | SOKOL_ASSERT(bnd->pip); |
| 14028 | SOKOL_ASSERT(bnd->pip->shader && (bnd->pip->cmn.shader_id.id == bnd->pip->shader->slot.id)); |
| 14029 | _sg_stats_add(wgpu.bindings.num_create_bindgroup, 1); |
| 14030 | _sg_wgpu_bindgroup_handle_t bg_id = _sg_wgpu_alloc_bindgroup(); |
| 14031 | if (bg_id.id == SG_INVALID_ID) { |
| 14032 | return 0; |
| 14033 | } |
| 14034 | _sg_wgpu_bindgroup_t* bg = _sg_wgpu_bindgroup_at(bg_id.id); |
| 14035 | SOKOL_ASSERT(bg && (bg->slot.state == SG_RESOURCESTATE_ALLOC)); |
| 14036 | |
| 14037 | // create wgpu bindgroup object |
| 14038 | WGPUBindGroupLayout bgl = bnd->pip->shader->wgpu.bind_group_layout; |
| 14039 | SOKOL_ASSERT(bgl); |
| 14040 | WGPUBindGroupEntry wgpu_entries[_SG_WGPU_MAX_BINDGROUP_ENTRIES]; |
| 14041 | _sg_clear(&wgpu_entries, sizeof(wgpu_entries)); |
| 14042 | int bge_index = 0; |
| 14043 | for (int i = 0; i < bnd->num_vs_imgs; i++) { |
| 14044 | WGPUBindGroupEntry* wgpu_entry = &wgpu_entries[bge_index++]; |
| 14045 | wgpu_entry->binding = _sg_wgpu_image_binding(SG_SHADERSTAGE_VS, i); |
| 14046 | wgpu_entry->textureView = bnd->vs_imgs[i]->wgpu.view; |
| 14047 | } |
| 14048 | for (int i = 0; i < bnd->num_vs_smps; i++) { |
| 14049 | WGPUBindGroupEntry* wgpu_entry = &wgpu_entries[bge_index++]; |
| 14050 | wgpu_entry->binding = _sg_wgpu_sampler_binding(SG_SHADERSTAGE_VS, i); |
| 14051 | wgpu_entry->sampler = bnd->vs_smps[i]->wgpu.smp; |
| 14052 | } |
| 14053 | for (int i = 0; i < bnd->num_vs_sbufs; i++) { |
| 14054 | WGPUBindGroupEntry* wgpu_entry = &wgpu_entries[bge_index++]; |
| 14055 | wgpu_entry->binding = _sg_wgpu_storagebuffer_binding(SG_SHADERSTAGE_VS, i); |
| 14056 | wgpu_entry->buffer = bnd->vs_sbufs[i]->wgpu.buf; |
| 14057 | wgpu_entry->size = (uint64_t) bnd->vs_sbufs[i]->cmn.size; |
| 14058 | } |
| 14059 | for (int i = 0; i < bnd->num_fs_imgs; i++) { |
| 14060 | WGPUBindGroupEntry* wgpu_entry = &wgpu_entries[bge_index++]; |
| 14061 | wgpu_entry->binding = _sg_wgpu_image_binding(SG_SHADERSTAGE_FS, i); |
| 14062 | wgpu_entry->textureView = bnd->fs_imgs[i]->wgpu.view; |
| 14063 | } |
| 14064 | for (int i = 0; i < bnd->num_fs_smps; i++) { |
| 14065 | WGPUBindGroupEntry* wgpu_entry = &wgpu_entries[bge_index++]; |
| 14066 | wgpu_entry->binding = _sg_wgpu_sampler_binding(SG_SHADERSTAGE_FS, i); |
| 14067 | wgpu_entry->sampler = bnd->fs_smps[i]->wgpu.smp; |
| 14068 | } |
| 14069 | for (int i = 0; i < bnd->num_fs_sbufs; i++) { |
| 14070 | WGPUBindGroupEntry* wgpu_entry = &wgpu_entries[bge_index++]; |
| 14071 | wgpu_entry->binding = _sg_wgpu_storagebuffer_binding(SG_SHADERSTAGE_FS, i); |
| 14072 | wgpu_entry->buffer = bnd->fs_sbufs[i]->wgpu.buf; |
| 14073 | wgpu_entry->size = (uint64_t) bnd->fs_sbufs[i]->cmn.size; |
| 14074 | } |
| 14075 | WGPUBindGroupDescriptor bg_desc; |
| 14076 | _sg_clear(&bg_desc, sizeof(bg_desc)); |
| 14077 | bg_desc.layout = bgl; |
| 14078 | bg_desc.entryCount = (size_t)bge_index; |
| 14079 | bg_desc.entries = &wgpu_entries[0]; |
| 14080 | bg->bindgroup = wgpuDeviceCreateBindGroup(_sg.wgpu.dev, &bg_desc); |
| 14081 | if (bg->bindgroup == 0) { |
| 14082 | _SG_ERROR(WGPU_CREATEBINDGROUP_FAILED); |
| 14083 | bg->slot.state = SG_RESOURCESTATE_FAILED; |
| 14084 | return bg; |
| 14085 | } |
| 14086 | |
| 14087 | _sg_wgpu_init_bindgroups_cache_key(&bg->key, bnd); |
| 14088 | |
| 14089 | bg->slot.state = SG_RESOURCESTATE_VALID; |
| 14090 | return bg; |
| 14091 | } |
| 14092 | |
| 14093 | _SOKOL_PRIVATE void _sg_wgpu_discard_bindgroup(_sg_wgpu_bindgroup_t* bg) { |
| 14094 | SOKOL_ASSERT(bg); |
| 14095 | _sg_stats_add(wgpu.bindings.num_discard_bindgroup, 1); |
| 14096 | if (bg->slot.state == SG_RESOURCESTATE_VALID) { |
| 14097 | if (bg->bindgroup) { |
| 14098 | wgpuBindGroupRelease(bg->bindgroup); |
| 14099 | bg->bindgroup = 0; |
| 14100 | } |
| 14101 | _sg_wgpu_reset_bindgroup_to_alloc_state(bg); |
| 14102 | SOKOL_ASSERT(bg->slot.state == SG_RESOURCESTATE_ALLOC); |
| 14103 | } |
| 14104 | if (bg->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 14105 | _sg_wgpu_dealloc_bindgroup(bg); |
| 14106 | SOKOL_ASSERT(bg->slot.state == SG_RESOURCESTATE_INITIAL); |
| 14107 | } |
| 14108 | } |
| 14109 | |
| 14110 | _SOKOL_PRIVATE void _sg_wgpu_discard_all_bindgroups(void) { |
| 14111 | _sg_wgpu_bindgroups_pool_t* p = &_sg.wgpu.bindgroups_pool; |
| 14112 | for (int i = 0; i < p->pool.size; i++) { |
| 14113 | sg_resource_state state = p->bindgroups[i].slot.state; |
| 14114 | if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) { |
| 14115 | _sg_wgpu_discard_bindgroup(&p->bindgroups[i]); |
| 14116 | } |
| 14117 | } |
| 14118 | } |
| 14119 | |
| 14120 | _SOKOL_PRIVATE void _sg_wgpu_bindgroups_cache_init(const sg_desc* desc) { |
| 14121 | SOKOL_ASSERT(desc); |
| 14122 | SOKOL_ASSERT(_sg.wgpu.bindgroups_cache.num == 0); |
| 14123 | SOKOL_ASSERT(_sg.wgpu.bindgroups_cache.index_mask == 0); |
| 14124 | SOKOL_ASSERT(_sg.wgpu.bindgroups_cache.items == 0); |
| 14125 | const int num = desc->wgpu_bindgroups_cache_size; |
| 14126 | if (num <= 1) { |
| 14127 | _SG_PANIC(WGPU_BINDGROUPSCACHE_SIZE_GREATER_ONE); |
| 14128 | } |
| 14129 | if (!_sg_ispow2(num)) { |
| 14130 | _SG_PANIC(WGPU_BINDGROUPSCACHE_SIZE_POW2); |
| 14131 | } |
| 14132 | _sg.wgpu.bindgroups_cache.num = (uint32_t)desc->wgpu_bindgroups_cache_size; |
| 14133 | _sg.wgpu.bindgroups_cache.index_mask = _sg.wgpu.bindgroups_cache.num - 1; |
| 14134 | size_t size_in_bytes = sizeof(_sg_wgpu_bindgroup_handle_t) * (size_t)num; |
| 14135 | _sg.wgpu.bindgroups_cache.items = (_sg_wgpu_bindgroup_handle_t*)_sg_malloc_clear(size_in_bytes); |
| 14136 | } |
| 14137 | |
| 14138 | _SOKOL_PRIVATE void _sg_wgpu_bindgroups_cache_discard(void) { |
| 14139 | if (_sg.wgpu.bindgroups_cache.items) { |
| 14140 | _sg_free(_sg.wgpu.bindgroups_cache.items); |
| 14141 | _sg.wgpu.bindgroups_cache.items = 0; |
| 14142 | } |
| 14143 | _sg.wgpu.bindgroups_cache.num = 0; |
| 14144 | _sg.wgpu.bindgroups_cache.index_mask = 0; |
| 14145 | } |
| 14146 | |
| 14147 | _SOKOL_PRIVATE void _sg_wgpu_bindgroups_cache_set(uint64_t hash, uint32_t bg_id) { |
| 14148 | uint32_t index = hash & _sg.wgpu.bindgroups_cache.index_mask; |
| 14149 | SOKOL_ASSERT(index < _sg.wgpu.bindgroups_cache.num); |
| 14150 | SOKOL_ASSERT(_sg.wgpu.bindgroups_cache.items); |
| 14151 | _sg.wgpu.bindgroups_cache.items[index].id = bg_id; |
| 14152 | } |
| 14153 | |
| 14154 | _SOKOL_PRIVATE uint32_t _sg_wgpu_bindgroups_cache_get(uint64_t hash) { |
| 14155 | uint32_t index = hash & _sg.wgpu.bindgroups_cache.index_mask; |
| 14156 | SOKOL_ASSERT(index < _sg.wgpu.bindgroups_cache.num); |
| 14157 | SOKOL_ASSERT(_sg.wgpu.bindgroups_cache.items); |
| 14158 | return _sg.wgpu.bindgroups_cache.items[index].id; |
| 14159 | } |
| 14160 | |
| 14161 | _SOKOL_PRIVATE void _sg_wgpu_bindings_cache_clear(void) { |
| 14162 | memset(&_sg.wgpu.bindings_cache, 0, sizeof(_sg.wgpu.bindings_cache)); |
| 14163 | } |
| 14164 | |
| 14165 | _SOKOL_PRIVATE bool _sg_wgpu_bindings_cache_vb_dirty(int index, const _sg_buffer_t* vb, int offset) { |
| 14166 | SOKOL_ASSERT((index >= 0) && (index < SG_MAX_VERTEX_BUFFERS)); |
| 14167 | if (vb) { |
| 14168 | return (_sg.wgpu.bindings_cache.vbs[index].buffer.id != vb->slot.id) |
| 14169 | || (_sg.wgpu.bindings_cache.vbs[index].offset != offset); |
| 14170 | } else { |
| 14171 | return _sg.wgpu.bindings_cache.vbs[index].buffer.id != SG_INVALID_ID; |
| 14172 | } |
| 14173 | } |
| 14174 | |
| 14175 | _SOKOL_PRIVATE void _sg_wgpu_bindings_cache_vb_update(int index, const _sg_buffer_t* vb, int offset) { |
| 14176 | SOKOL_ASSERT((index >= 0) && (index < SG_MAX_VERTEX_BUFFERS)); |
| 14177 | if (vb) { |
| 14178 | _sg.wgpu.bindings_cache.vbs[index].buffer.id = vb->slot.id; |
| 14179 | _sg.wgpu.bindings_cache.vbs[index].offset = offset; |
| 14180 | } else { |
| 14181 | _sg.wgpu.bindings_cache.vbs[index].buffer.id = SG_INVALID_ID; |
| 14182 | _sg.wgpu.bindings_cache.vbs[index].offset = 0; |
| 14183 | } |
| 14184 | } |
| 14185 | |
| 14186 | _SOKOL_PRIVATE bool _sg_wgpu_bindings_cache_ib_dirty(const _sg_buffer_t* ib, int offset) { |
| 14187 | if (ib) { |
| 14188 | return (_sg.wgpu.bindings_cache.ib.buffer.id != ib->slot.id) |
| 14189 | || (_sg.wgpu.bindings_cache.ib.offset != offset); |
| 14190 | } else { |
| 14191 | return _sg.wgpu.bindings_cache.ib.buffer.id != SG_INVALID_ID; |
| 14192 | } |
| 14193 | } |
| 14194 | |
| 14195 | _SOKOL_PRIVATE void _sg_wgpu_bindings_cache_ib_update(const _sg_buffer_t* ib, int offset) { |
| 14196 | if (ib) { |
| 14197 | _sg.wgpu.bindings_cache.ib.buffer.id = ib->slot.id; |
| 14198 | _sg.wgpu.bindings_cache.ib.offset = offset; |
| 14199 | } else { |
| 14200 | _sg.wgpu.bindings_cache.ib.buffer.id = SG_INVALID_ID; |
| 14201 | _sg.wgpu.bindings_cache.ib.offset = 0; |
| 14202 | } |
| 14203 | } |
| 14204 | |
| 14205 | _SOKOL_PRIVATE bool _sg_wgpu_bindings_cache_bg_dirty(const _sg_wgpu_bindgroup_t* bg) { |
| 14206 | if (bg) { |
| 14207 | return _sg.wgpu.bindings_cache.bg.id != bg->slot.id; |
| 14208 | } else { |
| 14209 | return _sg.wgpu.bindings_cache.bg.id != SG_INVALID_ID; |
| 14210 | } |
| 14211 | } |
| 14212 | |
| 14213 | _SOKOL_PRIVATE void _sg_wgpu_bindings_cache_bg_update(const _sg_wgpu_bindgroup_t* bg) { |
| 14214 | if (bg) { |
| 14215 | _sg.wgpu.bindings_cache.bg.id = bg->slot.id; |
| 14216 | } else { |
| 14217 | _sg.wgpu.bindings_cache.bg.id = SG_INVALID_ID; |
| 14218 | } |
| 14219 | } |
| 14220 | |
| 14221 | _SOKOL_PRIVATE void _sg_wgpu_set_bindings_bindgroup(_sg_wgpu_bindgroup_t* bg) { |
| 14222 | if (_sg_wgpu_bindings_cache_bg_dirty(bg)) { |
| 14223 | _sg_wgpu_bindings_cache_bg_update(bg); |
| 14224 | _sg_stats_add(wgpu.bindings.num_set_bindgroup, 1); |
| 14225 | if (bg) { |
| 14226 | SOKOL_ASSERT(bg->slot.state == SG_RESOURCESTATE_VALID); |
| 14227 | SOKOL_ASSERT(bg->bindgroup); |
| 14228 | wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc, _SG_WGPU_IMAGE_SAMPLER_BINDGROUP_INDEX, bg->bindgroup, 0, 0); |
| 14229 | } else { |
| 14230 | // a nullptr bindgroup means setting the empty bindgroup |
| 14231 | wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc, _SG_WGPU_IMAGE_SAMPLER_BINDGROUP_INDEX, _sg.wgpu.empty_bind_group, 0, 0); |
| 14232 | } |
| 14233 | } else { |
| 14234 | _sg_stats_add(wgpu.bindings.num_skip_redundant_bindgroup, 1); |
| 14235 | } |
| 14236 | } |
| 14237 | |
| 14238 | _SOKOL_PRIVATE bool _sg_wgpu_apply_bindgroup(_sg_bindings_t* bnd) { |
| 14239 | if ((bnd->num_vs_imgs + bnd->num_vs_smps + bnd->num_vs_sbufs + bnd->num_fs_imgs + bnd->num_fs_smps + bnd->num_fs_sbufs) > 0) { |
| 14240 | if (!_sg.desc.wgpu_disable_bindgroups_cache) { |
| 14241 | _sg_wgpu_bindgroup_t* bg = 0; |
| 14242 | _sg_wgpu_bindgroups_cache_key_t key; |
| 14243 | _sg_wgpu_init_bindgroups_cache_key(&key, bnd); |
| 14244 | uint32_t bg_id = _sg_wgpu_bindgroups_cache_get(key.hash); |
| 14245 | if (bg_id != SG_INVALID_ID) { |
| 14246 | // potential cache hit |
| 14247 | bg = _sg_wgpu_lookup_bindgroup(bg_id); |
| 14248 | SOKOL_ASSERT(bg && (bg->slot.state == SG_RESOURCESTATE_VALID)); |
| 14249 | if (!_sg_wgpu_compare_bindgroups_cache_key(&key, &bg->key)) { |
| 14250 | // cache collision, need to delete cached bindgroup |
| 14251 | _sg_stats_add(wgpu.bindings.num_bindgroup_cache_collisions, 1); |
| 14252 | _sg_wgpu_discard_bindgroup(bg); |
| 14253 | _sg_wgpu_bindgroups_cache_set(key.hash, SG_INVALID_ID); |
| 14254 | bg = 0; |
| 14255 | } else { |
| 14256 | _sg_stats_add(wgpu.bindings.num_bindgroup_cache_hits, 1); |
| 14257 | } |
| 14258 | } else { |
| 14259 | _sg_stats_add(wgpu.bindings.num_bindgroup_cache_misses, 1); |
| 14260 | } |
| 14261 | if (bg == 0) { |
| 14262 | // either no cache entry yet, or cache collision, create new bindgroup and store in cache |
| 14263 | bg = _sg_wgpu_create_bindgroup(bnd); |
| 14264 | _sg_wgpu_bindgroups_cache_set(key.hash, bg->slot.id); |
| 14265 | } |
| 14266 | if (bg && bg->slot.state == SG_RESOURCESTATE_VALID) { |
| 14267 | _sg_wgpu_set_bindings_bindgroup(bg); |
| 14268 | } else { |
| 14269 | return false; |
| 14270 | } |
| 14271 | } else { |
| 14272 | // bindgroups cache disabled, create and destroy bindgroup on the fly (expensive!) |
| 14273 | _sg_wgpu_bindgroup_t* bg = _sg_wgpu_create_bindgroup(bnd); |
| 14274 | if (bg) { |
| 14275 | if (bg->slot.state == SG_RESOURCESTATE_VALID) { |
| 14276 | _sg_wgpu_set_bindings_bindgroup(bg); |
| 14277 | } |
| 14278 | _sg_wgpu_discard_bindgroup(bg); |
| 14279 | } else { |
| 14280 | return false; |
| 14281 | } |
| 14282 | } |
| 14283 | } else { |
| 14284 | _sg_wgpu_set_bindings_bindgroup(0); |
| 14285 | } |
| 14286 | return true; |
| 14287 | } |
| 14288 | |
| 14289 | _SOKOL_PRIVATE bool _sg_wgpu_apply_index_buffer(_sg_bindings_t* bnd) { |
| 14290 | if (_sg_wgpu_bindings_cache_ib_dirty(bnd->ib, bnd->ib_offset)) { |
| 14291 | _sg_wgpu_bindings_cache_ib_update(bnd->ib, bnd->ib_offset); |
| 14292 | if (bnd->ib) { |
| 14293 | const WGPUIndexFormat format = _sg_wgpu_indexformat(bnd->pip->cmn.index_type); |
| 14294 | const uint64_t buf_size = (uint64_t)bnd->ib->cmn.size; |
| 14295 | const uint64_t offset = (uint64_t)bnd->ib_offset; |
| 14296 | SOKOL_ASSERT(buf_size > offset); |
| 14297 | const uint64_t max_bytes = buf_size - offset; |
| 14298 | wgpuRenderPassEncoderSetIndexBuffer(_sg.wgpu.pass_enc, bnd->ib->wgpu.buf, format, offset, max_bytes); |
| 14299 | _sg_stats_add(wgpu.bindings.num_set_index_buffer, 1); |
| 14300 | } |
| 14301 | // FIXME: else-path should actually set a null index buffer (this was just recently implemented in WebGPU) |
| 14302 | } else { |
| 14303 | _sg_stats_add(wgpu.bindings.num_skip_redundant_index_buffer, 1); |
| 14304 | } |
| 14305 | return true; |
| 14306 | } |
| 14307 | |
| 14308 | _SOKOL_PRIVATE bool _sg_wgpu_apply_vertex_buffers(_sg_bindings_t* bnd) { |
| 14309 | for (int slot = 0; slot < bnd->num_vbs; slot++) { |
| 14310 | if (_sg_wgpu_bindings_cache_vb_dirty(slot, bnd->vbs[slot], bnd->vb_offsets[slot])) { |
| 14311 | _sg_wgpu_bindings_cache_vb_update(slot, bnd->vbs[slot], bnd->vb_offsets[slot]); |
| 14312 | const uint64_t buf_size = (uint64_t)bnd->vbs[slot]->cmn.size; |
| 14313 | const uint64_t offset = (uint64_t)bnd->vb_offsets[slot]; |
| 14314 | SOKOL_ASSERT(buf_size > offset); |
| 14315 | const uint64_t max_bytes = buf_size - offset; |
| 14316 | wgpuRenderPassEncoderSetVertexBuffer(_sg.wgpu.pass_enc, (uint32_t)slot, bnd->vbs[slot]->wgpu.buf, offset, max_bytes); |
| 14317 | _sg_stats_add(wgpu.bindings.num_set_vertex_buffer, 1); |
| 14318 | } else { |
| 14319 | _sg_stats_add(wgpu.bindings.num_skip_redundant_vertex_buffer, 1); |
| 14320 | } |
| 14321 | } |
| 14322 | // FIXME: remaining vb slots should actually set a null vertex buffer (this was just recently implemented in WebGPU) |
| 14323 | return true; |
| 14324 | } |
| 14325 | |
| 14326 | _SOKOL_PRIVATE void _sg_wgpu_setup_backend(const sg_desc* desc) { |
| 14327 | SOKOL_ASSERT(desc); |
| 14328 | SOKOL_ASSERT(desc->environment.wgpu.device); |
| 14329 | SOKOL_ASSERT(desc->uniform_buffer_size > 0); |
| 14330 | _sg.backend = SG_BACKEND_WGPU; |
| 14331 | _sg.wgpu.valid = true; |
| 14332 | _sg.wgpu.dev = (WGPUDevice) desc->environment.wgpu.device; |
| 14333 | _sg.wgpu.queue = wgpuDeviceGetQueue(_sg.wgpu.dev); |
| 14334 | SOKOL_ASSERT(_sg.wgpu.queue); |
| 14335 | |
| 14336 | _sg_wgpu_init_caps(); |
| 14337 | _sg_wgpu_uniform_buffer_init(desc); |
| 14338 | _sg_wgpu_bindgroups_pool_init(desc); |
| 14339 | _sg_wgpu_bindgroups_cache_init(desc); |
| 14340 | _sg_wgpu_bindings_cache_clear(); |
| 14341 | |
| 14342 | // create an empty bind group for shader stages without bound images |
| 14343 | // FIXME: once WebGPU supports setting null objects, this can be removed |
| 14344 | WGPUBindGroupLayoutDescriptor bgl_desc; |
| 14345 | _sg_clear(&bgl_desc, sizeof(bgl_desc)); |
| 14346 | WGPUBindGroupLayout empty_bgl = wgpuDeviceCreateBindGroupLayout(_sg.wgpu.dev, &bgl_desc); |
| 14347 | SOKOL_ASSERT(empty_bgl); |
| 14348 | WGPUBindGroupDescriptor bg_desc; |
| 14349 | _sg_clear(&bg_desc, sizeof(bg_desc)); |
| 14350 | bg_desc.layout = empty_bgl; |
| 14351 | _sg.wgpu.empty_bind_group = wgpuDeviceCreateBindGroup(_sg.wgpu.dev, &bg_desc); |
| 14352 | SOKOL_ASSERT(_sg.wgpu.empty_bind_group); |
| 14353 | wgpuBindGroupLayoutRelease(empty_bgl); |
| 14354 | |
| 14355 | // create initial per-frame command encoder |
| 14356 | WGPUCommandEncoderDescriptor cmd_enc_desc; |
| 14357 | _sg_clear(&cmd_enc_desc, sizeof(cmd_enc_desc)); |
| 14358 | _sg.wgpu.cmd_enc = wgpuDeviceCreateCommandEncoder(_sg.wgpu.dev, &cmd_enc_desc); |
| 14359 | SOKOL_ASSERT(_sg.wgpu.cmd_enc); |
| 14360 | } |
| 14361 | |
| 14362 | _SOKOL_PRIVATE void _sg_wgpu_discard_backend(void) { |
| 14363 | SOKOL_ASSERT(_sg.wgpu.valid); |
| 14364 | SOKOL_ASSERT(_sg.wgpu.cmd_enc); |
| 14365 | _sg.wgpu.valid = false; |
| 14366 | _sg_wgpu_discard_all_bindgroups(); |
| 14367 | _sg_wgpu_bindgroups_cache_discard(); |
| 14368 | _sg_wgpu_bindgroups_pool_discard(); |
| 14369 | _sg_wgpu_uniform_buffer_discard(); |
| 14370 | wgpuBindGroupRelease(_sg.wgpu.empty_bind_group); _sg.wgpu.empty_bind_group = 0; |
| 14371 | wgpuCommandEncoderRelease(_sg.wgpu.cmd_enc); _sg.wgpu.cmd_enc = 0; |
| 14372 | wgpuQueueRelease(_sg.wgpu.queue); _sg.wgpu.queue = 0; |
| 14373 | } |
| 14374 | |
| 14375 | _SOKOL_PRIVATE void _sg_wgpu_reset_state_cache(void) { |
| 14376 | _sg_wgpu_bindings_cache_clear(); |
| 14377 | } |
| 14378 | |
| 14379 | _SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) { |
| 14380 | SOKOL_ASSERT(buf && desc); |
| 14381 | const bool injected = (0 != desc->wgpu_buffer); |
| 14382 | if (injected) { |
| 14383 | buf->wgpu.buf = (WGPUBuffer) desc->wgpu_buffer; |
| 14384 | wgpuBufferReference(buf->wgpu.buf); |
| 14385 | } else { |
| 14386 | // buffer mapping size must be multiple of 4, so round up buffer size (only a problem |
| 14387 | // with index buffers containing odd number of indices) |
| 14388 | const uint64_t wgpu_buf_size = _sg_roundup_u64((uint64_t)buf->cmn.size, 4); |
| 14389 | const bool map_at_creation = (SG_USAGE_IMMUTABLE == buf->cmn.usage); |
| 14390 | |
| 14391 | WGPUBufferDescriptor wgpu_buf_desc; |
| 14392 | _sg_clear(&wgpu_buf_desc, sizeof(wgpu_buf_desc)); |
| 14393 | wgpu_buf_desc.usage = _sg_wgpu_buffer_usage(buf->cmn.type, buf->cmn.usage); |
| 14394 | wgpu_buf_desc.size = wgpu_buf_size; |
| 14395 | wgpu_buf_desc.mappedAtCreation = map_at_creation; |
| 14396 | wgpu_buf_desc.label = desc->label; |
| 14397 | buf->wgpu.buf = wgpuDeviceCreateBuffer(_sg.wgpu.dev, &wgpu_buf_desc); |
| 14398 | if (0 == buf->wgpu.buf) { |
| 14399 | _SG_ERROR(WGPU_CREATE_BUFFER_FAILED); |
| 14400 | return SG_RESOURCESTATE_FAILED; |
| 14401 | } |
| 14402 | if (map_at_creation) { |
| 14403 | SOKOL_ASSERT(desc->data.ptr && (desc->data.size > 0)); |
| 14404 | SOKOL_ASSERT(desc->data.size <= (size_t)buf->cmn.size); |
| 14405 | // FIXME: inefficient on WASM |
| 14406 | void* ptr = wgpuBufferGetMappedRange(buf->wgpu.buf, 0, wgpu_buf_size); |
| 14407 | SOKOL_ASSERT(ptr); |
| 14408 | memcpy(ptr, desc->data.ptr, desc->data.size); |
| 14409 | wgpuBufferUnmap(buf->wgpu.buf); |
| 14410 | } |
| 14411 | } |
| 14412 | return SG_RESOURCESTATE_VALID; |
| 14413 | } |
| 14414 | |
| 14415 | _SOKOL_PRIVATE void _sg_wgpu_discard_buffer(_sg_buffer_t* buf) { |
| 14416 | SOKOL_ASSERT(buf); |
| 14417 | if (buf->wgpu.buf) { |
| 14418 | wgpuBufferDestroy(buf->wgpu.buf); |
| 14419 | wgpuBufferRelease(buf->wgpu.buf); |
| 14420 | } |
| 14421 | } |
| 14422 | |
| 14423 | _SOKOL_PRIVATE void _sg_wgpu_copy_buffer_data(const _sg_buffer_t* buf, uint64_t offset, const sg_range* data) { |
| 14424 | SOKOL_ASSERT((offset + data->size) <= (size_t)buf->cmn.size); |
| 14425 | // WebGPU's write-buffer requires the size to be a multiple of four, so we may need to split the copy |
| 14426 | // operation into two writeBuffer calls |
| 14427 | uint64_t clamped_size = data->size & ~3UL; |
| 14428 | uint64_t extra_size = data->size & 3UL; |
| 14429 | SOKOL_ASSERT(extra_size < 4); |
| 14430 | wgpuQueueWriteBuffer(_sg.wgpu.queue, buf->wgpu.buf, offset, data->ptr, clamped_size); |
| 14431 | if (extra_size > 0) { |
| 14432 | const uint64_t extra_src_offset = clamped_size; |
| 14433 | const uint64_t extra_dst_offset = offset + clamped_size; |
| 14434 | uint8_t extra_data[4] = { 0 }; |
| 14435 | uint8_t* extra_src_ptr = ((uint8_t*)data->ptr) + extra_src_offset; |
| 14436 | for (size_t i = 0; i < extra_size; i++) { |
| 14437 | extra_data[i] = extra_src_ptr[i]; |
| 14438 | } |
| 14439 | wgpuQueueWriteBuffer(_sg.wgpu.queue, buf->wgpu.buf, extra_dst_offset, extra_src_ptr, 4); |
| 14440 | } |
| 14441 | } |
| 14442 | |
| 14443 | _SOKOL_PRIVATE void _sg_wgpu_copy_image_data(const _sg_image_t* img, WGPUTexture wgpu_tex, const sg_image_data* data) { |
| 14444 | WGPUTextureDataLayout wgpu_layout; |
| 14445 | _sg_clear(&wgpu_layout, sizeof(wgpu_layout)); |
| 14446 | WGPUImageCopyTexture wgpu_copy_tex; |
| 14447 | _sg_clear(&wgpu_copy_tex, sizeof(wgpu_copy_tex)); |
| 14448 | wgpu_copy_tex.texture = wgpu_tex; |
| 14449 | wgpu_copy_tex.aspect = WGPUTextureAspect_All; |
| 14450 | WGPUExtent3D wgpu_extent; |
| 14451 | _sg_clear(&wgpu_extent, sizeof(wgpu_extent)); |
| 14452 | const int num_faces = (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6 : 1; |
| 14453 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 14454 | for (int mip_index = 0; mip_index < img->cmn.num_mipmaps; mip_index++) { |
| 14455 | wgpu_copy_tex.mipLevel = (uint32_t)mip_index; |
| 14456 | wgpu_copy_tex.origin.z = (uint32_t)face_index; |
| 14457 | int mip_width = _sg_miplevel_dim(img->cmn.width, mip_index); |
| 14458 | int mip_height = _sg_miplevel_dim(img->cmn.height, mip_index); |
| 14459 | int mip_slices; |
| 14460 | switch (img->cmn.type) { |
| 14461 | case SG_IMAGETYPE_CUBE: |
| 14462 | mip_slices = 1; |
| 14463 | break; |
| 14464 | case SG_IMAGETYPE_3D: |
| 14465 | mip_slices = _sg_miplevel_dim(img->cmn.num_slices, mip_index); |
| 14466 | break; |
| 14467 | default: |
| 14468 | mip_slices = img->cmn.num_slices; |
| 14469 | break; |
| 14470 | } |
| 14471 | const int row_pitch = _sg_row_pitch(img->cmn.pixel_format, mip_width, 1); |
| 14472 | const int num_rows = _sg_num_rows(img->cmn.pixel_format, mip_height); |
| 14473 | if (_sg_is_compressed_pixel_format(img->cmn.pixel_format)) { |
| 14474 | mip_width = _sg_roundup(mip_width, 4); |
| 14475 | mip_height = _sg_roundup(mip_height, 4); |
| 14476 | } |
| 14477 | wgpu_layout.offset = 0; |
| 14478 | wgpu_layout.bytesPerRow = (uint32_t)row_pitch; |
| 14479 | wgpu_layout.rowsPerImage = (uint32_t)num_rows; |
| 14480 | wgpu_extent.width = (uint32_t)mip_width; |
| 14481 | wgpu_extent.height = (uint32_t)mip_height; |
| 14482 | wgpu_extent.depthOrArrayLayers = (uint32_t)mip_slices; |
| 14483 | const sg_range* mip_data = &data->subimage[face_index][mip_index]; |
| 14484 | wgpuQueueWriteTexture(_sg.wgpu.queue, &wgpu_copy_tex, mip_data->ptr, mip_data->size, &wgpu_layout, &wgpu_extent); |
| 14485 | } |
| 14486 | } |
| 14487 | } |
| 14488 | |
| 14489 | _SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_image(_sg_image_t* img, const sg_image_desc* desc) { |
| 14490 | SOKOL_ASSERT(img && desc); |
| 14491 | const bool injected = (0 != desc->wgpu_texture); |
| 14492 | if (injected) { |
| 14493 | img->wgpu.tex = (WGPUTexture)desc->wgpu_texture; |
| 14494 | wgpuTextureReference(img->wgpu.tex); |
| 14495 | img->wgpu.view = (WGPUTextureView)desc->wgpu_texture_view; |
| 14496 | if (img->wgpu.view) { |
| 14497 | wgpuTextureViewReference(img->wgpu.view); |
| 14498 | } |
| 14499 | } else { |
| 14500 | WGPUTextureDescriptor wgpu_tex_desc; |
| 14501 | _sg_clear(&wgpu_tex_desc, sizeof(wgpu_tex_desc)); |
| 14502 | wgpu_tex_desc.label = desc->label; |
| 14503 | wgpu_tex_desc.usage = WGPUTextureUsage_TextureBinding|WGPUTextureUsage_CopyDst; |
| 14504 | if (desc->render_target) { |
| 14505 | wgpu_tex_desc.usage |= WGPUTextureUsage_RenderAttachment; |
| 14506 | } |
| 14507 | wgpu_tex_desc.dimension = _sg_wgpu_texture_dimension(img->cmn.type); |
| 14508 | wgpu_tex_desc.size.width = (uint32_t) img->cmn.width; |
| 14509 | wgpu_tex_desc.size.height = (uint32_t) img->cmn.height; |
| 14510 | if (desc->type == SG_IMAGETYPE_CUBE) { |
| 14511 | wgpu_tex_desc.size.depthOrArrayLayers = 6; |
| 14512 | } else { |
| 14513 | wgpu_tex_desc.size.depthOrArrayLayers = (uint32_t) img->cmn.num_slices; |
| 14514 | } |
| 14515 | wgpu_tex_desc.format = _sg_wgpu_textureformat(img->cmn.pixel_format); |
| 14516 | wgpu_tex_desc.mipLevelCount = (uint32_t) img->cmn.num_mipmaps; |
| 14517 | wgpu_tex_desc.sampleCount = (uint32_t) img->cmn.sample_count; |
| 14518 | img->wgpu.tex = wgpuDeviceCreateTexture(_sg.wgpu.dev, &wgpu_tex_desc); |
| 14519 | if (0 == img->wgpu.tex) { |
| 14520 | _SG_ERROR(WGPU_CREATE_TEXTURE_FAILED); |
| 14521 | return SG_RESOURCESTATE_FAILED; |
| 14522 | } |
| 14523 | if ((img->cmn.usage == SG_USAGE_IMMUTABLE) && !img->cmn.render_target) { |
| 14524 | _sg_wgpu_copy_image_data(img, img->wgpu.tex, &desc->data); |
| 14525 | } |
| 14526 | WGPUTextureViewDescriptor wgpu_texview_desc; |
| 14527 | _sg_clear(&wgpu_texview_desc, sizeof(wgpu_texview_desc)); |
| 14528 | wgpu_texview_desc.label = desc->label; |
| 14529 | wgpu_texview_desc.dimension = _sg_wgpu_texture_view_dimension(img->cmn.type); |
| 14530 | wgpu_texview_desc.mipLevelCount = (uint32_t)img->cmn.num_mipmaps; |
| 14531 | if (img->cmn.type == SG_IMAGETYPE_CUBE) { |
| 14532 | wgpu_texview_desc.arrayLayerCount = 6; |
| 14533 | } else if (img->cmn.type == SG_IMAGETYPE_ARRAY) { |
| 14534 | wgpu_texview_desc.arrayLayerCount = (uint32_t)img->cmn.num_slices; |
| 14535 | } else { |
| 14536 | wgpu_texview_desc.arrayLayerCount = 1; |
| 14537 | } |
| 14538 | if (_sg_is_depth_or_depth_stencil_format(img->cmn.pixel_format)) { |
| 14539 | wgpu_texview_desc.aspect = WGPUTextureAspect_DepthOnly; |
| 14540 | } else { |
| 14541 | wgpu_texview_desc.aspect = WGPUTextureAspect_All; |
| 14542 | } |
| 14543 | img->wgpu.view = wgpuTextureCreateView(img->wgpu.tex, &wgpu_texview_desc); |
| 14544 | if (0 == img->wgpu.view) { |
| 14545 | _SG_ERROR(WGPU_CREATE_TEXTURE_VIEW_FAILED); |
| 14546 | return SG_RESOURCESTATE_FAILED; |
| 14547 | } |
| 14548 | } |
| 14549 | return SG_RESOURCESTATE_VALID; |
| 14550 | } |
| 14551 | |
| 14552 | _SOKOL_PRIVATE void _sg_wgpu_discard_image(_sg_image_t* img) { |
| 14553 | SOKOL_ASSERT(img); |
| 14554 | if (img->wgpu.view) { |
| 14555 | wgpuTextureViewRelease(img->wgpu.view); |
| 14556 | img->wgpu.view = 0; |
| 14557 | } |
| 14558 | if (img->wgpu.tex) { |
| 14559 | wgpuTextureDestroy(img->wgpu.tex); |
| 14560 | wgpuTextureRelease(img->wgpu.tex); |
| 14561 | img->wgpu.tex = 0; |
| 14562 | } |
| 14563 | } |
| 14564 | |
| 14565 | _SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) { |
| 14566 | SOKOL_ASSERT(smp && desc); |
| 14567 | SOKOL_ASSERT(_sg.wgpu.dev); |
| 14568 | const bool injected = (0 != desc->wgpu_sampler); |
| 14569 | if (injected) { |
| 14570 | smp->wgpu.smp = (WGPUSampler) desc->wgpu_sampler; |
| 14571 | wgpuSamplerReference(smp->wgpu.smp); |
| 14572 | } else { |
| 14573 | WGPUSamplerDescriptor wgpu_desc; |
| 14574 | _sg_clear(&wgpu_desc, sizeof(wgpu_desc)); |
| 14575 | wgpu_desc.label = desc->label; |
| 14576 | wgpu_desc.addressModeU = _sg_wgpu_sampler_address_mode(desc->wrap_u); |
| 14577 | wgpu_desc.addressModeV = _sg_wgpu_sampler_address_mode(desc->wrap_v); |
| 14578 | wgpu_desc.addressModeW = _sg_wgpu_sampler_address_mode(desc->wrap_w); |
| 14579 | wgpu_desc.magFilter = _sg_wgpu_sampler_minmag_filter(desc->mag_filter); |
| 14580 | wgpu_desc.minFilter = _sg_wgpu_sampler_minmag_filter(desc->min_filter); |
| 14581 | wgpu_desc.mipmapFilter = _sg_wgpu_sampler_mipmap_filter(desc->mipmap_filter); |
| 14582 | wgpu_desc.lodMinClamp = desc->min_lod; |
| 14583 | wgpu_desc.lodMaxClamp = desc->max_lod; |
| 14584 | wgpu_desc.compare = _sg_wgpu_comparefunc(desc->compare); |
| 14585 | if (wgpu_desc.compare == WGPUCompareFunction_Never) { |
| 14586 | wgpu_desc.compare = WGPUCompareFunction_Undefined; |
| 14587 | } |
| 14588 | wgpu_desc.maxAnisotropy = (uint16_t)desc->max_anisotropy; |
| 14589 | smp->wgpu.smp = wgpuDeviceCreateSampler(_sg.wgpu.dev, &wgpu_desc); |
| 14590 | if (0 == smp->wgpu.smp) { |
| 14591 | _SG_ERROR(WGPU_CREATE_SAMPLER_FAILED); |
| 14592 | return SG_RESOURCESTATE_FAILED; |
| 14593 | } |
| 14594 | } |
| 14595 | return SG_RESOURCESTATE_VALID; |
| 14596 | } |
| 14597 | |
| 14598 | _SOKOL_PRIVATE void _sg_wgpu_discard_sampler(_sg_sampler_t* smp) { |
| 14599 | SOKOL_ASSERT(smp); |
| 14600 | if (smp->wgpu.smp) { |
| 14601 | wgpuSamplerRelease(smp->wgpu.smp); |
| 14602 | smp->wgpu.smp = 0; |
| 14603 | } |
| 14604 | } |
| 14605 | |
| 14606 | _SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) { |
| 14607 | SOKOL_ASSERT(shd && desc); |
| 14608 | SOKOL_ASSERT(desc->vs.source && desc->fs.source); |
| 14609 | |
| 14610 | WGPUBindGroupLayoutEntry wgpu_bgl_entries[_SG_WGPU_MAX_BINDGROUP_ENTRIES]; |
| 14611 | _sg_clear(wgpu_bgl_entries, sizeof(wgpu_bgl_entries)); |
| 14612 | int bgl_index = 0; |
| 14613 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 14614 | const sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS) ? &desc->vs : &desc->fs; |
| 14615 | |
| 14616 | _sg_shader_stage_t* cmn_stage = &shd->cmn.stage[stage_index]; |
| 14617 | _sg_wgpu_shader_stage_t* wgpu_stage = &shd->wgpu.stage[stage_index]; |
| 14618 | _sg_strcpy(&wgpu_stage->entry, stage_desc->entry); |
| 14619 | |
| 14620 | WGPUShaderModuleWGSLDescriptor wgpu_shdmod_wgsl_desc; |
| 14621 | _sg_clear(&wgpu_shdmod_wgsl_desc, sizeof(wgpu_shdmod_wgsl_desc)); |
| 14622 | wgpu_shdmod_wgsl_desc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor; |
| 14623 | wgpu_shdmod_wgsl_desc.code = stage_desc->source; |
| 14624 | |
| 14625 | WGPUShaderModuleDescriptor wgpu_shdmod_desc; |
| 14626 | _sg_clear(&wgpu_shdmod_desc, sizeof(wgpu_shdmod_desc)); |
| 14627 | wgpu_shdmod_desc.nextInChain = &wgpu_shdmod_wgsl_desc.chain; |
| 14628 | wgpu_shdmod_desc.label = desc->label; |
| 14629 | |
| 14630 | wgpu_stage->module = wgpuDeviceCreateShaderModule(_sg.wgpu.dev, &wgpu_shdmod_desc); |
| 14631 | if (0 == wgpu_stage->module) { |
| 14632 | _SG_ERROR(WGPU_CREATE_SHADER_MODULE_FAILED); |
| 14633 | return SG_RESOURCESTATE_FAILED; |
| 14634 | } |
| 14635 | |
| 14636 | const int num_images = cmn_stage->num_images; |
| 14637 | if (num_images > (int)_sg.wgpu.limits.limits.maxSampledTexturesPerShaderStage) { |
| 14638 | _SG_ERROR(WGPU_SHADER_TOO_MANY_IMAGES); |
| 14639 | return SG_RESOURCESTATE_FAILED; |
| 14640 | } |
| 14641 | const int num_samplers = cmn_stage->num_samplers; |
| 14642 | if (num_samplers > (int)_sg.wgpu.limits.limits.maxSamplersPerShaderStage) { |
| 14643 | _SG_ERROR(WGPU_SHADER_TOO_MANY_SAMPLERS); |
| 14644 | return SG_RESOURCESTATE_FAILED; |
| 14645 | } |
| 14646 | const int num_sbufs = cmn_stage->num_storage_buffers; |
| 14647 | if (num_sbufs > (int)_sg.wgpu.limits.limits.maxStorageBuffersPerShaderStage) { |
| 14648 | _SG_ERROR(WGPU_SHADER_TOO_MANY_STORAGEBUFFERS); |
| 14649 | return SG_RESOURCESTATE_FAILED; |
| 14650 | } |
| 14651 | for (int img_index = 0; img_index < num_images; img_index++) { |
| 14652 | SOKOL_ASSERT(bgl_index < _SG_WGPU_MAX_BINDGROUP_ENTRIES); |
| 14653 | WGPUBindGroupLayoutEntry* wgpu_bgl_entry = &wgpu_bgl_entries[bgl_index++]; |
| 14654 | const sg_shader_image_desc* img_desc = &stage_desc->images[img_index]; |
| 14655 | wgpu_bgl_entry->binding = _sg_wgpu_image_binding((sg_shader_stage)stage_index, img_index); |
| 14656 | wgpu_bgl_entry->visibility = _sg_wgpu_shader_stage((sg_shader_stage)stage_index); |
| 14657 | wgpu_bgl_entry->texture.viewDimension = _sg_wgpu_texture_view_dimension(img_desc->image_type); |
| 14658 | wgpu_bgl_entry->texture.sampleType = _sg_wgpu_texture_sample_type(img_desc->sample_type); |
| 14659 | wgpu_bgl_entry->texture.multisampled = img_desc->multisampled; |
| 14660 | } |
| 14661 | for (int smp_index = 0; smp_index < num_samplers; smp_index++) { |
| 14662 | SOKOL_ASSERT(bgl_index < _SG_WGPU_MAX_BINDGROUP_ENTRIES); |
| 14663 | WGPUBindGroupLayoutEntry* wgpu_bgl_entry = &wgpu_bgl_entries[bgl_index++]; |
| 14664 | const sg_shader_sampler_desc* smp_desc = &stage_desc->samplers[smp_index]; |
| 14665 | wgpu_bgl_entry->binding = _sg_wgpu_sampler_binding((sg_shader_stage)stage_index, smp_index); |
| 14666 | wgpu_bgl_entry->visibility = _sg_wgpu_shader_stage((sg_shader_stage)stage_index); |
| 14667 | wgpu_bgl_entry->sampler.type = _sg_wgpu_sampler_binding_type(smp_desc->sampler_type); |
| 14668 | } |
| 14669 | for (int sbuf_index = 0; sbuf_index < num_sbufs; sbuf_index++) { |
| 14670 | SOKOL_ASSERT(bgl_index < _SG_WGPU_MAX_BINDGROUP_ENTRIES); |
| 14671 | WGPUBindGroupLayoutEntry* wgpu_bgl_entry = &wgpu_bgl_entries[bgl_index++]; |
| 14672 | const sg_shader_storage_buffer_desc* sbuf_desc = &stage_desc->storage_buffers[sbuf_index]; |
| 14673 | wgpu_bgl_entry->binding = _sg_wgpu_storagebuffer_binding((sg_shader_stage)stage_index, sbuf_index); |
| 14674 | wgpu_bgl_entry->visibility = _sg_wgpu_shader_stage((sg_shader_stage)stage_index); |
| 14675 | wgpu_bgl_entry->buffer.type = sbuf_desc->readonly ? WGPUBufferBindingType_ReadOnlyStorage : WGPUBufferBindingType_Storage; |
| 14676 | } |
| 14677 | } |
| 14678 | |
| 14679 | WGPUBindGroupLayoutDescriptor wgpu_bgl_desc; |
| 14680 | _sg_clear(&wgpu_bgl_desc, sizeof(wgpu_bgl_desc)); |
| 14681 | wgpu_bgl_desc.entryCount = (size_t)bgl_index; |
| 14682 | wgpu_bgl_desc.entries = &wgpu_bgl_entries[0]; |
| 14683 | shd->wgpu.bind_group_layout = wgpuDeviceCreateBindGroupLayout(_sg.wgpu.dev, &wgpu_bgl_desc); |
| 14684 | if (shd->wgpu.bind_group_layout == 0) { |
| 14685 | _SG_ERROR(WGPU_SHADER_CREATE_BINDGROUP_LAYOUT_FAILED); |
| 14686 | return SG_RESOURCESTATE_FAILED; |
| 14687 | } |
| 14688 | return SG_RESOURCESTATE_VALID; |
| 14689 | } |
| 14690 | |
| 14691 | _SOKOL_PRIVATE void _sg_wgpu_discard_shader(_sg_shader_t* shd) { |
| 14692 | SOKOL_ASSERT(shd); |
| 14693 | if (shd->wgpu.bind_group_layout) { |
| 14694 | wgpuBindGroupLayoutRelease(shd->wgpu.bind_group_layout); |
| 14695 | shd->wgpu.bind_group_layout = 0; |
| 14696 | } |
| 14697 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 14698 | _sg_wgpu_shader_stage_t* wgpu_stage = &shd->wgpu.stage[stage_index]; |
| 14699 | if (wgpu_stage->module) { |
| 14700 | wgpuShaderModuleRelease(wgpu_stage->module); |
| 14701 | wgpu_stage->module = 0; |
| 14702 | } |
| 14703 | } |
| 14704 | } |
| 14705 | |
| 14706 | _SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) { |
| 14707 | SOKOL_ASSERT(pip && shd && desc); |
| 14708 | SOKOL_ASSERT(desc->shader.id == shd->slot.id); |
| 14709 | SOKOL_ASSERT(shd->wgpu.bind_group_layout); |
| 14710 | pip->shader = shd; |
| 14711 | |
| 14712 | pip->wgpu.blend_color.r = (double) desc->blend_color.r; |
| 14713 | pip->wgpu.blend_color.g = (double) desc->blend_color.g; |
| 14714 | pip->wgpu.blend_color.b = (double) desc->blend_color.b; |
| 14715 | pip->wgpu.blend_color.a = (double) desc->blend_color.a; |
| 14716 | |
| 14717 | // - @group(0) for uniform blocks |
| 14718 | // - @group(1) for all image and sampler resources |
| 14719 | WGPUBindGroupLayout wgpu_bgl[_SG_WGPU_NUM_BINDGROUPS]; |
| 14720 | _sg_clear(&wgpu_bgl, sizeof(wgpu_bgl)); |
| 14721 | wgpu_bgl[_SG_WGPU_UNIFORM_BINDGROUP_INDEX] = _sg.wgpu.uniform.bind.group_layout; |
| 14722 | wgpu_bgl[_SG_WGPU_IMAGE_SAMPLER_BINDGROUP_INDEX] = shd->wgpu.bind_group_layout; |
| 14723 | WGPUPipelineLayoutDescriptor wgpu_pl_desc; |
| 14724 | _sg_clear(&wgpu_pl_desc, sizeof(wgpu_pl_desc)); |
| 14725 | wgpu_pl_desc.bindGroupLayoutCount = _SG_WGPU_NUM_BINDGROUPS; |
| 14726 | wgpu_pl_desc.bindGroupLayouts = &wgpu_bgl[0]; |
| 14727 | const WGPUPipelineLayout wgpu_pip_layout = wgpuDeviceCreatePipelineLayout(_sg.wgpu.dev, &wgpu_pl_desc); |
| 14728 | if (0 == wgpu_pip_layout) { |
| 14729 | _SG_ERROR(WGPU_CREATE_PIPELINE_LAYOUT_FAILED); |
| 14730 | return SG_RESOURCESTATE_FAILED; |
| 14731 | } |
| 14732 | SOKOL_ASSERT(wgpu_pip_layout); |
| 14733 | |
| 14734 | WGPUVertexBufferLayout wgpu_vb_layouts[SG_MAX_VERTEX_BUFFERS]; |
| 14735 | _sg_clear(wgpu_vb_layouts, sizeof(wgpu_vb_layouts)); |
| 14736 | WGPUVertexAttribute wgpu_vtx_attrs[SG_MAX_VERTEX_BUFFERS][SG_MAX_VERTEX_ATTRIBUTES]; |
| 14737 | _sg_clear(wgpu_vtx_attrs, sizeof(wgpu_vtx_attrs)); |
| 14738 | int wgpu_vb_num = 0; |
| 14739 | for (int vb_idx = 0; vb_idx < SG_MAX_VERTEX_BUFFERS; vb_idx++, wgpu_vb_num++) { |
| 14740 | const sg_vertex_buffer_layout_state* vbl_state = &desc->layout.buffers[vb_idx]; |
| 14741 | if (0 == vbl_state->stride) { |
| 14742 | break; |
| 14743 | } |
| 14744 | wgpu_vb_layouts[vb_idx].arrayStride = (uint64_t)vbl_state->stride; |
| 14745 | wgpu_vb_layouts[vb_idx].stepMode = _sg_wgpu_stepmode(vbl_state->step_func); |
| 14746 | wgpu_vb_layouts[vb_idx].attributes = &wgpu_vtx_attrs[vb_idx][0]; |
| 14747 | } |
| 14748 | for (int va_idx = 0; va_idx < SG_MAX_VERTEX_ATTRIBUTES; va_idx++) { |
| 14749 | const sg_vertex_attr_state* va_state = &desc->layout.attrs[va_idx]; |
| 14750 | if (SG_VERTEXFORMAT_INVALID == va_state->format) { |
| 14751 | break; |
| 14752 | } |
| 14753 | const int vb_idx = va_state->buffer_index; |
| 14754 | SOKOL_ASSERT(vb_idx < SG_MAX_VERTEX_BUFFERS); |
| 14755 | pip->cmn.vertex_buffer_layout_active[vb_idx] = true; |
| 14756 | const size_t wgpu_attr_idx = wgpu_vb_layouts[vb_idx].attributeCount; |
| 14757 | wgpu_vb_layouts[vb_idx].attributeCount += 1; |
| 14758 | wgpu_vtx_attrs[vb_idx][wgpu_attr_idx].format = _sg_wgpu_vertexformat(va_state->format); |
| 14759 | wgpu_vtx_attrs[vb_idx][wgpu_attr_idx].offset = (uint64_t)va_state->offset; |
| 14760 | wgpu_vtx_attrs[vb_idx][wgpu_attr_idx].shaderLocation = (uint32_t)va_idx; |
| 14761 | } |
| 14762 | |
| 14763 | WGPURenderPipelineDescriptor wgpu_pip_desc; |
| 14764 | _sg_clear(&wgpu_pip_desc, sizeof(wgpu_pip_desc)); |
| 14765 | WGPUDepthStencilState wgpu_ds_state; |
| 14766 | _sg_clear(&wgpu_ds_state, sizeof(wgpu_ds_state)); |
| 14767 | WGPUFragmentState wgpu_frag_state; |
| 14768 | _sg_clear(&wgpu_frag_state, sizeof(wgpu_frag_state)); |
| 14769 | WGPUColorTargetState wgpu_ctgt_state[SG_MAX_COLOR_ATTACHMENTS]; |
| 14770 | _sg_clear(&wgpu_ctgt_state, sizeof(wgpu_ctgt_state)); |
| 14771 | WGPUBlendState wgpu_blend_state[SG_MAX_COLOR_ATTACHMENTS]; |
| 14772 | _sg_clear(&wgpu_blend_state, sizeof(wgpu_blend_state)); |
| 14773 | wgpu_pip_desc.label = desc->label; |
| 14774 | wgpu_pip_desc.layout = wgpu_pip_layout; |
| 14775 | wgpu_pip_desc.vertex.module = shd->wgpu.stage[SG_SHADERSTAGE_VS].module; |
| 14776 | wgpu_pip_desc.vertex.entryPoint = shd->wgpu.stage[SG_SHADERSTAGE_VS].entry.buf; |
| 14777 | wgpu_pip_desc.vertex.bufferCount = (size_t)wgpu_vb_num; |
| 14778 | wgpu_pip_desc.vertex.buffers = &wgpu_vb_layouts[0]; |
| 14779 | wgpu_pip_desc.primitive.topology = _sg_wgpu_topology(desc->primitive_type); |
| 14780 | wgpu_pip_desc.primitive.stripIndexFormat = _sg_wgpu_stripindexformat(desc->primitive_type, desc->index_type); |
| 14781 | wgpu_pip_desc.primitive.frontFace = _sg_wgpu_frontface(desc->face_winding); |
| 14782 | wgpu_pip_desc.primitive.cullMode = _sg_wgpu_cullmode(desc->cull_mode); |
| 14783 | if (SG_PIXELFORMAT_NONE != desc->depth.pixel_format) { |
| 14784 | wgpu_ds_state.format = _sg_wgpu_textureformat(desc->depth.pixel_format); |
| 14785 | wgpu_ds_state.depthWriteEnabled = desc->depth.write_enabled; |
| 14786 | wgpu_ds_state.depthCompare = _sg_wgpu_comparefunc(desc->depth.compare); |
| 14787 | wgpu_ds_state.stencilFront.compare = _sg_wgpu_comparefunc(desc->stencil.front.compare); |
| 14788 | wgpu_ds_state.stencilFront.failOp = _sg_wgpu_stencilop(desc->stencil.front.fail_op); |
| 14789 | wgpu_ds_state.stencilFront.depthFailOp = _sg_wgpu_stencilop(desc->stencil.front.depth_fail_op); |
| 14790 | wgpu_ds_state.stencilFront.passOp = _sg_wgpu_stencilop(desc->stencil.front.pass_op); |
| 14791 | wgpu_ds_state.stencilBack.compare = _sg_wgpu_comparefunc(desc->stencil.back.compare); |
| 14792 | wgpu_ds_state.stencilBack.failOp = _sg_wgpu_stencilop(desc->stencil.back.fail_op); |
| 14793 | wgpu_ds_state.stencilBack.depthFailOp = _sg_wgpu_stencilop(desc->stencil.back.depth_fail_op); |
| 14794 | wgpu_ds_state.stencilBack.passOp = _sg_wgpu_stencilop(desc->stencil.back.pass_op); |
| 14795 | wgpu_ds_state.stencilReadMask = desc->stencil.read_mask; |
| 14796 | wgpu_ds_state.stencilWriteMask = desc->stencil.write_mask; |
| 14797 | wgpu_ds_state.depthBias = (int32_t)desc->depth.bias; |
| 14798 | wgpu_ds_state.depthBiasSlopeScale = desc->depth.bias_slope_scale; |
| 14799 | wgpu_ds_state.depthBiasClamp = desc->depth.bias_clamp; |
| 14800 | wgpu_pip_desc.depthStencil = &wgpu_ds_state; |
| 14801 | } |
| 14802 | wgpu_pip_desc.multisample.count = (uint32_t)desc->sample_count; |
| 14803 | wgpu_pip_desc.multisample.mask = 0xFFFFFFFF; |
| 14804 | wgpu_pip_desc.multisample.alphaToCoverageEnabled = desc->alpha_to_coverage_enabled; |
| 14805 | if (desc->color_count > 0) { |
| 14806 | wgpu_frag_state.module = shd->wgpu.stage[SG_SHADERSTAGE_FS].module; |
| 14807 | wgpu_frag_state.entryPoint = shd->wgpu.stage[SG_SHADERSTAGE_FS].entry.buf; |
| 14808 | wgpu_frag_state.targetCount = (size_t)desc->color_count; |
| 14809 | wgpu_frag_state.targets = &wgpu_ctgt_state[0]; |
| 14810 | for (int i = 0; i < desc->color_count; i++) { |
| 14811 | SOKOL_ASSERT(i < SG_MAX_COLOR_ATTACHMENTS); |
| 14812 | wgpu_ctgt_state[i].format = _sg_wgpu_textureformat(desc->colors[i].pixel_format); |
| 14813 | wgpu_ctgt_state[i].writeMask = _sg_wgpu_colorwritemask(desc->colors[i].write_mask); |
| 14814 | if (desc->colors[i].blend.enabled) { |
| 14815 | wgpu_ctgt_state[i].blend = &wgpu_blend_state[i]; |
| 14816 | wgpu_blend_state[i].color.operation = _sg_wgpu_blendop(desc->colors[i].blend.op_rgb); |
| 14817 | wgpu_blend_state[i].color.srcFactor = _sg_wgpu_blendfactor(desc->colors[i].blend.src_factor_rgb); |
| 14818 | wgpu_blend_state[i].color.dstFactor = _sg_wgpu_blendfactor(desc->colors[i].blend.dst_factor_rgb); |
| 14819 | wgpu_blend_state[i].alpha.operation = _sg_wgpu_blendop(desc->colors[i].blend.op_alpha); |
| 14820 | wgpu_blend_state[i].alpha.srcFactor = _sg_wgpu_blendfactor(desc->colors[i].blend.src_factor_alpha); |
| 14821 | wgpu_blend_state[i].alpha.dstFactor = _sg_wgpu_blendfactor(desc->colors[i].blend.dst_factor_alpha); |
| 14822 | } |
| 14823 | } |
| 14824 | wgpu_pip_desc.fragment = &wgpu_frag_state; |
| 14825 | } |
| 14826 | pip->wgpu.pip = wgpuDeviceCreateRenderPipeline(_sg.wgpu.dev, &wgpu_pip_desc); |
| 14827 | wgpuPipelineLayoutRelease(wgpu_pip_layout); |
| 14828 | if (0 == pip->wgpu.pip) { |
| 14829 | _SG_ERROR(WGPU_CREATE_RENDER_PIPELINE_FAILED); |
| 14830 | return SG_RESOURCESTATE_FAILED; |
| 14831 | } |
| 14832 | return SG_RESOURCESTATE_VALID; |
| 14833 | } |
| 14834 | |
| 14835 | _SOKOL_PRIVATE void _sg_wgpu_discard_pipeline(_sg_pipeline_t* pip) { |
| 14836 | SOKOL_ASSERT(pip); |
| 14837 | if (pip == _sg.wgpu.cur_pipeline) { |
| 14838 | _sg.wgpu.cur_pipeline = 0; |
| 14839 | _sg.wgpu.cur_pipeline_id.id = SG_INVALID_ID; |
| 14840 | } |
| 14841 | if (pip->wgpu.pip) { |
| 14842 | wgpuRenderPipelineRelease(pip->wgpu.pip); |
| 14843 | pip->wgpu.pip = 0; |
| 14844 | } |
| 14845 | } |
| 14846 | |
| 14847 | _SOKOL_PRIVATE sg_resource_state _sg_wgpu_create_attachments(_sg_attachments_t* atts, _sg_image_t** color_images, _sg_image_t** resolve_images, _sg_image_t* ds_img, const sg_attachments_desc* desc) { |
| 14848 | SOKOL_ASSERT(atts && desc); |
| 14849 | SOKOL_ASSERT(color_images && resolve_images); |
| 14850 | |
| 14851 | // copy image pointers and create renderable wgpu texture views |
| 14852 | for (int i = 0; i < atts->cmn.num_colors; i++) { |
| 14853 | const sg_attachment_desc* color_desc = &desc->colors[i]; |
| 14854 | _SOKOL_UNUSED(color_desc); |
| 14855 | SOKOL_ASSERT(color_desc->image.id != SG_INVALID_ID); |
| 14856 | SOKOL_ASSERT(0 == atts->wgpu.colors[i].image); |
| 14857 | SOKOL_ASSERT(color_images[i] && (color_images[i]->slot.id == color_desc->image.id)); |
| 14858 | SOKOL_ASSERT(_sg_is_valid_rendertarget_color_format(color_images[i]->cmn.pixel_format)); |
| 14859 | SOKOL_ASSERT(color_images[i]->wgpu.tex); |
| 14860 | atts->wgpu.colors[i].image = color_images[i]; |
| 14861 | |
| 14862 | WGPUTextureViewDescriptor wgpu_color_view_desc; |
| 14863 | _sg_clear(&wgpu_color_view_desc, sizeof(wgpu_color_view_desc)); |
| 14864 | wgpu_color_view_desc.baseMipLevel = (uint32_t) color_desc->mip_level; |
| 14865 | wgpu_color_view_desc.mipLevelCount = 1; |
| 14866 | wgpu_color_view_desc.baseArrayLayer = (uint32_t) color_desc->slice; |
| 14867 | wgpu_color_view_desc.arrayLayerCount = 1; |
| 14868 | atts->wgpu.colors[i].view = wgpuTextureCreateView(color_images[i]->wgpu.tex, &wgpu_color_view_desc); |
| 14869 | if (0 == atts->wgpu.colors[i].view) { |
| 14870 | _SG_ERROR(WGPU_ATTACHMENTS_CREATE_TEXTURE_VIEW_FAILED); |
| 14871 | return SG_RESOURCESTATE_FAILED; |
| 14872 | } |
| 14873 | |
| 14874 | const sg_attachment_desc* resolve_desc = &desc->resolves[i]; |
| 14875 | if (resolve_desc->image.id != SG_INVALID_ID) { |
| 14876 | SOKOL_ASSERT(0 == atts->wgpu.resolves[i].image); |
| 14877 | SOKOL_ASSERT(resolve_images[i] && (resolve_images[i]->slot.id == resolve_desc->image.id)); |
| 14878 | SOKOL_ASSERT(color_images[i] && (color_images[i]->cmn.pixel_format == resolve_images[i]->cmn.pixel_format)); |
| 14879 | SOKOL_ASSERT(resolve_images[i]->wgpu.tex); |
| 14880 | atts->wgpu.resolves[i].image = resolve_images[i]; |
| 14881 | |
| 14882 | WGPUTextureViewDescriptor wgpu_resolve_view_desc; |
| 14883 | _sg_clear(&wgpu_resolve_view_desc, sizeof(wgpu_resolve_view_desc)); |
| 14884 | wgpu_resolve_view_desc.baseMipLevel = (uint32_t) resolve_desc->mip_level; |
| 14885 | wgpu_resolve_view_desc.mipLevelCount = 1; |
| 14886 | wgpu_resolve_view_desc.baseArrayLayer = (uint32_t) resolve_desc->slice; |
| 14887 | wgpu_resolve_view_desc.arrayLayerCount = 1; |
| 14888 | atts->wgpu.resolves[i].view = wgpuTextureCreateView(resolve_images[i]->wgpu.tex, &wgpu_resolve_view_desc); |
| 14889 | if (0 == atts->wgpu.resolves[i].view) { |
| 14890 | _SG_ERROR(WGPU_ATTACHMENTS_CREATE_TEXTURE_VIEW_FAILED); |
| 14891 | return SG_RESOURCESTATE_FAILED; |
| 14892 | } |
| 14893 | } |
| 14894 | } |
| 14895 | SOKOL_ASSERT(0 == atts->wgpu.depth_stencil.image); |
| 14896 | const sg_attachment_desc* ds_desc = &desc->depth_stencil; |
| 14897 | if (ds_desc->image.id != SG_INVALID_ID) { |
| 14898 | SOKOL_ASSERT(ds_img && (ds_img->slot.id == ds_desc->image.id)); |
| 14899 | SOKOL_ASSERT(_sg_is_valid_rendertarget_depth_format(ds_img->cmn.pixel_format)); |
| 14900 | SOKOL_ASSERT(ds_img->wgpu.tex); |
| 14901 | atts->wgpu.depth_stencil.image = ds_img; |
| 14902 | |
| 14903 | WGPUTextureViewDescriptor wgpu_ds_view_desc; |
| 14904 | _sg_clear(&wgpu_ds_view_desc, sizeof(wgpu_ds_view_desc)); |
| 14905 | wgpu_ds_view_desc.baseMipLevel = (uint32_t) ds_desc->mip_level; |
| 14906 | wgpu_ds_view_desc.mipLevelCount = 1; |
| 14907 | wgpu_ds_view_desc.baseArrayLayer = (uint32_t) ds_desc->slice; |
| 14908 | wgpu_ds_view_desc.arrayLayerCount = 1; |
| 14909 | atts->wgpu.depth_stencil.view = wgpuTextureCreateView(ds_img->wgpu.tex, &wgpu_ds_view_desc); |
| 14910 | if (0 == atts->wgpu.depth_stencil.view) { |
| 14911 | _SG_ERROR(WGPU_ATTACHMENTS_CREATE_TEXTURE_VIEW_FAILED); |
| 14912 | return SG_RESOURCESTATE_FAILED; |
| 14913 | } |
| 14914 | } |
| 14915 | return SG_RESOURCESTATE_VALID; |
| 14916 | } |
| 14917 | |
| 14918 | _SOKOL_PRIVATE void _sg_wgpu_discard_attachments(_sg_attachments_t* atts) { |
| 14919 | SOKOL_ASSERT(atts); |
| 14920 | for (int i = 0; i < atts->cmn.num_colors; i++) { |
| 14921 | if (atts->wgpu.colors[i].view) { |
| 14922 | wgpuTextureViewRelease(atts->wgpu.colors[i].view); |
| 14923 | atts->wgpu.colors[i].view = 0; |
| 14924 | } |
| 14925 | if (atts->wgpu.resolves[i].view) { |
| 14926 | wgpuTextureViewRelease(atts->wgpu.resolves[i].view); |
| 14927 | atts->wgpu.resolves[i].view = 0; |
| 14928 | } |
| 14929 | } |
| 14930 | if (atts->wgpu.depth_stencil.view) { |
| 14931 | wgpuTextureViewRelease(atts->wgpu.depth_stencil.view); |
| 14932 | atts->wgpu.depth_stencil.view = 0; |
| 14933 | } |
| 14934 | } |
| 14935 | |
| 14936 | _SOKOL_PRIVATE _sg_image_t* _sg_wgpu_attachments_color_image(const _sg_attachments_t* atts, int index) { |
| 14937 | SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS)); |
| 14938 | // NOTE: may return null |
| 14939 | return atts->wgpu.colors[index].image; |
| 14940 | } |
| 14941 | |
| 14942 | _SOKOL_PRIVATE _sg_image_t* _sg_wgpu_attachments_resolve_image(const _sg_attachments_t* atts, int index) { |
| 14943 | SOKOL_ASSERT(atts && (index >= 0) && (index < SG_MAX_COLOR_ATTACHMENTS)); |
| 14944 | // NOTE: may return null |
| 14945 | return atts->wgpu.resolves[index].image; |
| 14946 | } |
| 14947 | |
| 14948 | _SOKOL_PRIVATE _sg_image_t* _sg_wgpu_attachments_ds_image(const _sg_attachments_t* atts) { |
| 14949 | // NOTE: may return null |
| 14950 | SOKOL_ASSERT(atts); |
| 14951 | return atts->wgpu.depth_stencil.image; |
| 14952 | } |
| 14953 | |
| 14954 | _SOKOL_PRIVATE void _sg_wgpu_init_color_att(WGPURenderPassColorAttachment* wgpu_att, const sg_color_attachment_action* action, WGPUTextureView color_view, WGPUTextureView resolve_view) { |
| 14955 | wgpu_att->depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; |
| 14956 | wgpu_att->view = color_view; |
| 14957 | wgpu_att->resolveTarget = resolve_view; |
| 14958 | wgpu_att->loadOp = _sg_wgpu_load_op(color_view, action->load_action); |
| 14959 | wgpu_att->storeOp = _sg_wgpu_store_op(color_view, action->store_action); |
| 14960 | wgpu_att->clearValue.r = action->clear_value.r; |
| 14961 | wgpu_att->clearValue.g = action->clear_value.g; |
| 14962 | wgpu_att->clearValue.b = action->clear_value.b; |
| 14963 | wgpu_att->clearValue.a = action->clear_value.a; |
| 14964 | } |
| 14965 | |
| 14966 | _SOKOL_PRIVATE void _sg_wgpu_init_ds_att(WGPURenderPassDepthStencilAttachment* wgpu_att, const sg_pass_action* action, sg_pixel_format fmt, WGPUTextureView view) { |
| 14967 | wgpu_att->view = view; |
| 14968 | wgpu_att->depthLoadOp = _sg_wgpu_load_op(view, action->depth.load_action); |
| 14969 | wgpu_att->depthStoreOp = _sg_wgpu_store_op(view, action->depth.store_action); |
| 14970 | wgpu_att->depthClearValue = action->depth.clear_value; |
| 14971 | wgpu_att->depthReadOnly = false; |
| 14972 | if (_sg_is_depth_stencil_format(fmt)) { |
| 14973 | wgpu_att->stencilLoadOp = _sg_wgpu_load_op(view, action->stencil.load_action); |
| 14974 | wgpu_att->stencilStoreOp = _sg_wgpu_store_op(view, action->stencil.store_action); |
| 14975 | } else { |
| 14976 | wgpu_att->stencilLoadOp = WGPULoadOp_Undefined; |
| 14977 | wgpu_att->stencilStoreOp = WGPUStoreOp_Undefined; |
| 14978 | } |
| 14979 | wgpu_att->stencilClearValue = action->stencil.clear_value; |
| 14980 | wgpu_att->stencilReadOnly = false; |
| 14981 | } |
| 14982 | |
| 14983 | _SOKOL_PRIVATE void _sg_wgpu_begin_pass(const sg_pass* pass) { |
| 14984 | SOKOL_ASSERT(pass); |
| 14985 | SOKOL_ASSERT(_sg.wgpu.cmd_enc); |
| 14986 | SOKOL_ASSERT(_sg.wgpu.dev); |
| 14987 | |
| 14988 | const _sg_attachments_t* atts = _sg.cur_pass.atts; |
| 14989 | const sg_swapchain* swapchain = &pass->swapchain; |
| 14990 | const sg_pass_action* action = &pass->action; |
| 14991 | |
| 14992 | _sg.wgpu.cur_pipeline = 0; |
| 14993 | _sg.wgpu.cur_pipeline_id.id = SG_INVALID_ID; |
| 14994 | |
| 14995 | WGPURenderPassDescriptor wgpu_pass_desc; |
| 14996 | WGPURenderPassColorAttachment wgpu_color_att[SG_MAX_COLOR_ATTACHMENTS]; |
| 14997 | WGPURenderPassDepthStencilAttachment wgpu_ds_att; |
| 14998 | _sg_clear(&wgpu_pass_desc, sizeof(wgpu_pass_desc)); |
| 14999 | _sg_clear(&wgpu_color_att, sizeof(wgpu_color_att)); |
| 15000 | _sg_clear(&wgpu_ds_att, sizeof(wgpu_ds_att)); |
| 15001 | wgpu_pass_desc.label = pass->label; |
| 15002 | if (atts) { |
| 15003 | SOKOL_ASSERT(atts->slot.state == SG_RESOURCESTATE_VALID); |
| 15004 | for (int i = 0; i < atts->cmn.num_colors; i++) { |
| 15005 | _sg_wgpu_init_color_att(&wgpu_color_att[i], &action->colors[i], atts->wgpu.colors[i].view, atts->wgpu.resolves[i].view); |
| 15006 | } |
| 15007 | wgpu_pass_desc.colorAttachmentCount = (size_t)atts->cmn.num_colors; |
| 15008 | wgpu_pass_desc.colorAttachments = &wgpu_color_att[0]; |
| 15009 | if (atts->wgpu.depth_stencil.image) { |
| 15010 | _sg_wgpu_init_ds_att(&wgpu_ds_att, action, atts->wgpu.depth_stencil.image->cmn.pixel_format, atts->wgpu.depth_stencil.view); |
| 15011 | wgpu_pass_desc.depthStencilAttachment = &wgpu_ds_att; |
| 15012 | } |
| 15013 | } else { |
| 15014 | WGPUTextureView wgpu_color_view = (WGPUTextureView) swapchain->wgpu.render_view; |
| 15015 | WGPUTextureView wgpu_resolve_view = (WGPUTextureView) swapchain->wgpu.resolve_view; |
| 15016 | WGPUTextureView wgpu_depth_stencil_view = (WGPUTextureView) swapchain->wgpu.depth_stencil_view; |
| 15017 | _sg_wgpu_init_color_att(&wgpu_color_att[0], &action->colors[0], wgpu_color_view, wgpu_resolve_view); |
| 15018 | wgpu_pass_desc.colorAttachmentCount = 1; |
| 15019 | wgpu_pass_desc.colorAttachments = &wgpu_color_att[0]; |
| 15020 | if (wgpu_depth_stencil_view) { |
| 15021 | SOKOL_ASSERT(swapchain->depth_format > SG_PIXELFORMAT_NONE); |
| 15022 | _sg_wgpu_init_ds_att(&wgpu_ds_att, action, swapchain->depth_format, wgpu_depth_stencil_view); |
| 15023 | wgpu_pass_desc.depthStencilAttachment = &wgpu_ds_att; |
| 15024 | } |
| 15025 | } |
| 15026 | _sg.wgpu.pass_enc = wgpuCommandEncoderBeginRenderPass(_sg.wgpu.cmd_enc, &wgpu_pass_desc); |
| 15027 | SOKOL_ASSERT(_sg.wgpu.pass_enc); |
| 15028 | |
| 15029 | // clear bindings cache and apply an empty image-sampler bindgroup |
| 15030 | _sg_wgpu_bindings_cache_clear(); |
| 15031 | wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc, _SG_WGPU_IMAGE_SAMPLER_BINDGROUP_INDEX, _sg.wgpu.empty_bind_group, 0, 0); |
| 15032 | _sg_stats_add(wgpu.bindings.num_set_bindgroup, 1); |
| 15033 | |
| 15034 | // initial uniform buffer binding (required even if no uniforms are set in the frame) |
| 15035 | _sg_wgpu_uniform_buffer_on_begin_pass(); |
| 15036 | } |
| 15037 | |
| 15038 | _SOKOL_PRIVATE void _sg_wgpu_end_pass(void) { |
| 15039 | if (_sg.wgpu.pass_enc) { |
| 15040 | wgpuRenderPassEncoderEnd(_sg.wgpu.pass_enc); |
| 15041 | wgpuRenderPassEncoderRelease(_sg.wgpu.pass_enc); |
| 15042 | _sg.wgpu.pass_enc = 0; |
| 15043 | } |
| 15044 | } |
| 15045 | |
| 15046 | _SOKOL_PRIVATE void _sg_wgpu_commit(void) { |
| 15047 | SOKOL_ASSERT(_sg.wgpu.cmd_enc); |
| 15048 | |
| 15049 | _sg_wgpu_uniform_buffer_on_commit(); |
| 15050 | |
| 15051 | WGPUCommandBufferDescriptor cmd_buf_desc; |
| 15052 | _sg_clear(&cmd_buf_desc, sizeof(cmd_buf_desc)); |
| 15053 | WGPUCommandBuffer wgpu_cmd_buf = wgpuCommandEncoderFinish(_sg.wgpu.cmd_enc, &cmd_buf_desc); |
| 15054 | SOKOL_ASSERT(wgpu_cmd_buf); |
| 15055 | wgpuCommandEncoderRelease(_sg.wgpu.cmd_enc); |
| 15056 | _sg.wgpu.cmd_enc = 0; |
| 15057 | |
| 15058 | wgpuQueueSubmit(_sg.wgpu.queue, 1, &wgpu_cmd_buf); |
| 15059 | wgpuCommandBufferRelease(wgpu_cmd_buf); |
| 15060 | |
| 15061 | // create a new render-command-encoder for next frame |
| 15062 | WGPUCommandEncoderDescriptor cmd_enc_desc; |
| 15063 | _sg_clear(&cmd_enc_desc, sizeof(cmd_enc_desc)); |
| 15064 | _sg.wgpu.cmd_enc = wgpuDeviceCreateCommandEncoder(_sg.wgpu.dev, &cmd_enc_desc); |
| 15065 | } |
| 15066 | |
| 15067 | _SOKOL_PRIVATE void _sg_wgpu_apply_viewport(int x, int y, int w, int h, bool origin_top_left) { |
| 15068 | SOKOL_ASSERT(_sg.wgpu.pass_enc); |
| 15069 | // FIXME FIXME FIXME: CLIPPING THE VIEWPORT HERE IS WRONG!!! |
| 15070 | // (but currently required because WebGPU insists that the viewport rectangle must be |
| 15071 | // fully contained inside the framebuffer, but this doesn't make any sense, and also |
| 15072 | // isn't required by the backend APIs) |
| 15073 | const _sg_recti_t clip = _sg_clipi(x, y, w, h, _sg.cur_pass.width, _sg.cur_pass.height); |
| 15074 | float xf = (float) clip.x; |
| 15075 | float yf = (float) (origin_top_left ? clip.y : (_sg.cur_pass.height - (clip.y + clip.h))); |
| 15076 | float wf = (float) clip.w; |
| 15077 | float hf = (float) clip.h; |
| 15078 | wgpuRenderPassEncoderSetViewport(_sg.wgpu.pass_enc, xf, yf, wf, hf, 0.0f, 1.0f); |
| 15079 | } |
| 15080 | |
| 15081 | _SOKOL_PRIVATE void _sg_wgpu_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) { |
| 15082 | SOKOL_ASSERT(_sg.wgpu.pass_enc); |
| 15083 | const _sg_recti_t clip = _sg_clipi(x, y, w, h, _sg.cur_pass.width, _sg.cur_pass.height); |
| 15084 | uint32_t sx = (uint32_t) clip.x; |
| 15085 | uint32_t sy = (uint32_t) (origin_top_left ? clip.y : (_sg.cur_pass.height - (clip.y + clip.h))); |
| 15086 | uint32_t sw = (uint32_t) clip.w; |
| 15087 | uint32_t sh = (uint32_t) clip.h; |
| 15088 | wgpuRenderPassEncoderSetScissorRect(_sg.wgpu.pass_enc, sx, sy, sw, sh); |
| 15089 | } |
| 15090 | |
| 15091 | _SOKOL_PRIVATE void _sg_wgpu_apply_pipeline(_sg_pipeline_t* pip) { |
| 15092 | SOKOL_ASSERT(pip); |
| 15093 | SOKOL_ASSERT(pip->wgpu.pip); |
| 15094 | SOKOL_ASSERT(_sg.wgpu.pass_enc); |
| 15095 | _sg.wgpu.use_indexed_draw = (pip->cmn.index_type != SG_INDEXTYPE_NONE); |
| 15096 | _sg.wgpu.cur_pipeline = pip; |
| 15097 | _sg.wgpu.cur_pipeline_id.id = pip->slot.id; |
| 15098 | wgpuRenderPassEncoderSetPipeline(_sg.wgpu.pass_enc, pip->wgpu.pip); |
| 15099 | wgpuRenderPassEncoderSetBlendConstant(_sg.wgpu.pass_enc, &pip->wgpu.blend_color); |
| 15100 | wgpuRenderPassEncoderSetStencilReference(_sg.wgpu.pass_enc, pip->cmn.stencil.ref); |
| 15101 | } |
| 15102 | |
| 15103 | _SOKOL_PRIVATE bool _sg_wgpu_apply_bindings(_sg_bindings_t* bnd) { |
| 15104 | SOKOL_ASSERT(_sg.wgpu.pass_enc); |
| 15105 | SOKOL_ASSERT(bnd); |
| 15106 | SOKOL_ASSERT(bnd->pip->shader && (bnd->pip->cmn.shader_id.id == bnd->pip->shader->slot.id)); |
| 15107 | bool retval = true; |
| 15108 | retval &= _sg_wgpu_apply_index_buffer(bnd); |
| 15109 | retval &= _sg_wgpu_apply_vertex_buffers(bnd); |
| 15110 | retval &= _sg_wgpu_apply_bindgroup(bnd); |
| 15111 | return retval; |
| 15112 | } |
| 15113 | |
| 15114 | _SOKOL_PRIVATE void _sg_wgpu_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) { |
| 15115 | const uint32_t alignment = _sg.wgpu.limits.limits.minUniformBufferOffsetAlignment; |
| 15116 | SOKOL_ASSERT(_sg.wgpu.pass_enc); |
| 15117 | SOKOL_ASSERT(_sg.wgpu.uniform.staging); |
| 15118 | SOKOL_ASSERT((_sg.wgpu.uniform.offset + data->size) <= _sg.wgpu.uniform.num_bytes); |
| 15119 | SOKOL_ASSERT((_sg.wgpu.uniform.offset & (alignment - 1)) == 0); |
| 15120 | SOKOL_ASSERT(_sg.wgpu.cur_pipeline && _sg.wgpu.cur_pipeline->shader); |
| 15121 | SOKOL_ASSERT(_sg.wgpu.cur_pipeline->slot.id == _sg.wgpu.cur_pipeline_id.id); |
| 15122 | SOKOL_ASSERT(_sg.wgpu.cur_pipeline->shader->slot.id == _sg.wgpu.cur_pipeline->cmn.shader_id.id); |
| 15123 | SOKOL_ASSERT(ub_index < _sg.wgpu.cur_pipeline->shader->cmn.stage[stage_index].num_uniform_blocks); |
| 15124 | SOKOL_ASSERT(data->size <= _sg.wgpu.cur_pipeline->shader->cmn.stage[stage_index].uniform_blocks[ub_index].size); |
| 15125 | SOKOL_ASSERT(data->size <= _SG_WGPU_MAX_UNIFORM_UPDATE_SIZE); |
| 15126 | |
| 15127 | _sg_stats_add(wgpu.uniforms.num_set_bindgroup, 1); |
| 15128 | memcpy(_sg.wgpu.uniform.staging + _sg.wgpu.uniform.offset, data->ptr, data->size); |
| 15129 | _sg.wgpu.uniform.bind.offsets[stage_index][ub_index] = _sg.wgpu.uniform.offset; |
| 15130 | _sg.wgpu.uniform.offset = _sg_roundup_u32(_sg.wgpu.uniform.offset + (uint32_t)data->size, alignment); |
| 15131 | wgpuRenderPassEncoderSetBindGroup(_sg.wgpu.pass_enc, |
| 15132 | _SG_WGPU_UNIFORM_BINDGROUP_INDEX, |
| 15133 | _sg.wgpu.uniform.bind.group, |
| 15134 | SG_NUM_SHADER_STAGES * SG_MAX_SHADERSTAGE_UBS, |
| 15135 | &_sg.wgpu.uniform.bind.offsets[0][0]); |
| 15136 | } |
| 15137 | |
| 15138 | _SOKOL_PRIVATE void _sg_wgpu_draw(int base_element, int num_elements, int num_instances) { |
| 15139 | SOKOL_ASSERT(_sg.wgpu.pass_enc); |
| 15140 | SOKOL_ASSERT(_sg.wgpu.cur_pipeline && (_sg.wgpu.cur_pipeline->slot.id == _sg.wgpu.cur_pipeline_id.id)); |
| 15141 | if (SG_INDEXTYPE_NONE != _sg.wgpu.cur_pipeline->cmn.index_type) { |
| 15142 | wgpuRenderPassEncoderDrawIndexed(_sg.wgpu.pass_enc, (uint32_t)num_elements, (uint32_t)num_instances, (uint32_t)base_element, 0, 0); |
| 15143 | } else { |
| 15144 | wgpuRenderPassEncoderDraw(_sg.wgpu.pass_enc, (uint32_t)num_elements, (uint32_t)num_instances, (uint32_t)base_element, 0); |
| 15145 | } |
| 15146 | } |
| 15147 | |
| 15148 | _SOKOL_PRIVATE void _sg_wgpu_update_buffer(_sg_buffer_t* buf, const sg_range* data) { |
| 15149 | SOKOL_ASSERT(data && data->ptr && (data->size > 0)); |
| 15150 | SOKOL_ASSERT(buf); |
| 15151 | _sg_wgpu_copy_buffer_data(buf, 0, data); |
| 15152 | } |
| 15153 | |
| 15154 | _SOKOL_PRIVATE void _sg_wgpu_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) { |
| 15155 | SOKOL_ASSERT(data && data->ptr && (data->size > 0)); |
| 15156 | _SOKOL_UNUSED(new_frame); |
| 15157 | _sg_wgpu_copy_buffer_data(buf, (uint64_t)buf->cmn.append_pos, data); |
| 15158 | } |
| 15159 | |
| 15160 | _SOKOL_PRIVATE void _sg_wgpu_update_image(_sg_image_t* img, const sg_image_data* data) { |
| 15161 | SOKOL_ASSERT(img && data); |
| 15162 | _sg_wgpu_copy_image_data(img, img->wgpu.tex, data); |
| 15163 | } |
| 15164 | #endif |
| 15165 | |
| 15166 | // ██████ ███████ ███ ██ ███████ ██████ ██ ██████ ██████ █████ ██████ ██ ██ ███████ ███ ██ ██████ |
| 15167 | // ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ |
| 15168 | // ██ ███ █████ ██ ██ ██ █████ ██████ ██ ██ ██████ ███████ ██ █████ █████ ██ ██ ██ ██ ██ |
| 15169 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 15170 | // ██████ ███████ ██ ████ ███████ ██ ██ ██ ██████ ██████ ██ ██ ██████ ██ ██ ███████ ██ ████ ██████ |
| 15171 | // |
| 15172 | // >>generic backend |
| 15173 | static inline void _sg_setup_backend(const sg_desc* desc) { |
| 15174 | #if defined(_SOKOL_ANY_GL) |
| 15175 | _sg_gl_setup_backend(desc); |
| 15176 | #elif defined(SOKOL_METAL) |
| 15177 | _sg_mtl_setup_backend(desc); |
| 15178 | #elif defined(SOKOL_D3D11) |
| 15179 | _sg_d3d11_setup_backend(desc); |
| 15180 | #elif defined(SOKOL_WGPU) |
| 15181 | _sg_wgpu_setup_backend(desc); |
| 15182 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15183 | _sg_dummy_setup_backend(desc); |
| 15184 | #else |
| 15185 | #error("INVALID BACKEND"); |
| 15186 | #endif |
| 15187 | } |
| 15188 | |
| 15189 | static inline void _sg_discard_backend(void) { |
| 15190 | #if defined(_SOKOL_ANY_GL) |
| 15191 | _sg_gl_discard_backend(); |
| 15192 | #elif defined(SOKOL_METAL) |
| 15193 | _sg_mtl_discard_backend(); |
| 15194 | #elif defined(SOKOL_D3D11) |
| 15195 | _sg_d3d11_discard_backend(); |
| 15196 | #elif defined(SOKOL_WGPU) |
| 15197 | _sg_wgpu_discard_backend(); |
| 15198 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15199 | _sg_dummy_discard_backend(); |
| 15200 | #else |
| 15201 | #error("INVALID BACKEND"); |
| 15202 | #endif |
| 15203 | } |
| 15204 | |
| 15205 | static inline void _sg_reset_state_cache(void) { |
| 15206 | #if defined(_SOKOL_ANY_GL) |
| 15207 | _sg_gl_reset_state_cache(); |
| 15208 | #elif defined(SOKOL_METAL) |
| 15209 | _sg_mtl_reset_state_cache(); |
| 15210 | #elif defined(SOKOL_D3D11) |
| 15211 | _sg_d3d11_reset_state_cache(); |
| 15212 | #elif defined(SOKOL_WGPU) |
| 15213 | _sg_wgpu_reset_state_cache(); |
| 15214 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15215 | _sg_dummy_reset_state_cache(); |
| 15216 | #else |
| 15217 | #error("INVALID BACKEND"); |
| 15218 | #endif |
| 15219 | } |
| 15220 | |
| 15221 | static inline sg_resource_state _sg_create_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) { |
| 15222 | #if defined(_SOKOL_ANY_GL) |
| 15223 | return _sg_gl_create_buffer(buf, desc); |
| 15224 | #elif defined(SOKOL_METAL) |
| 15225 | return _sg_mtl_create_buffer(buf, desc); |
| 15226 | #elif defined(SOKOL_D3D11) |
| 15227 | return _sg_d3d11_create_buffer(buf, desc); |
| 15228 | #elif defined(SOKOL_WGPU) |
| 15229 | return _sg_wgpu_create_buffer(buf, desc); |
| 15230 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15231 | return _sg_dummy_create_buffer(buf, desc); |
| 15232 | #else |
| 15233 | #error("INVALID BACKEND"); |
| 15234 | #endif |
| 15235 | } |
| 15236 | |
| 15237 | static inline void _sg_discard_buffer(_sg_buffer_t* buf) { |
| 15238 | #if defined(_SOKOL_ANY_GL) |
| 15239 | _sg_gl_discard_buffer(buf); |
| 15240 | #elif defined(SOKOL_METAL) |
| 15241 | _sg_mtl_discard_buffer(buf); |
| 15242 | #elif defined(SOKOL_D3D11) |
| 15243 | _sg_d3d11_discard_buffer(buf); |
| 15244 | #elif defined(SOKOL_WGPU) |
| 15245 | _sg_wgpu_discard_buffer(buf); |
| 15246 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15247 | _sg_dummy_discard_buffer(buf); |
| 15248 | #else |
| 15249 | #error("INVALID BACKEND"); |
| 15250 | #endif |
| 15251 | } |
| 15252 | |
| 15253 | static inline sg_resource_state _sg_create_image(_sg_image_t* img, const sg_image_desc* desc) { |
| 15254 | #if defined(_SOKOL_ANY_GL) |
| 15255 | return _sg_gl_create_image(img, desc); |
| 15256 | #elif defined(SOKOL_METAL) |
| 15257 | return _sg_mtl_create_image(img, desc); |
| 15258 | #elif defined(SOKOL_D3D11) |
| 15259 | return _sg_d3d11_create_image(img, desc); |
| 15260 | #elif defined(SOKOL_WGPU) |
| 15261 | return _sg_wgpu_create_image(img, desc); |
| 15262 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15263 | return _sg_dummy_create_image(img, desc); |
| 15264 | #else |
| 15265 | #error("INVALID BACKEND"); |
| 15266 | #endif |
| 15267 | } |
| 15268 | |
| 15269 | static inline void _sg_discard_image(_sg_image_t* img) { |
| 15270 | #if defined(_SOKOL_ANY_GL) |
| 15271 | _sg_gl_discard_image(img); |
| 15272 | #elif defined(SOKOL_METAL) |
| 15273 | _sg_mtl_discard_image(img); |
| 15274 | #elif defined(SOKOL_D3D11) |
| 15275 | _sg_d3d11_discard_image(img); |
| 15276 | #elif defined(SOKOL_WGPU) |
| 15277 | _sg_wgpu_discard_image(img); |
| 15278 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15279 | _sg_dummy_discard_image(img); |
| 15280 | #else |
| 15281 | #error("INVALID BACKEND"); |
| 15282 | #endif |
| 15283 | } |
| 15284 | |
| 15285 | static inline sg_resource_state _sg_create_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) { |
| 15286 | #if defined(_SOKOL_ANY_GL) |
| 15287 | return _sg_gl_create_sampler(smp, desc); |
| 15288 | #elif defined(SOKOL_METAL) |
| 15289 | return _sg_mtl_create_sampler(smp, desc); |
| 15290 | #elif defined(SOKOL_D3D11) |
| 15291 | return _sg_d3d11_create_sampler(smp, desc); |
| 15292 | #elif defined(SOKOL_WGPU) |
| 15293 | return _sg_wgpu_create_sampler(smp, desc); |
| 15294 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15295 | return _sg_dummy_create_sampler(smp, desc); |
| 15296 | #else |
| 15297 | #error("INVALID BACKEND"); |
| 15298 | #endif |
| 15299 | } |
| 15300 | |
| 15301 | static inline void _sg_discard_sampler(_sg_sampler_t* smp) { |
| 15302 | #if defined(_SOKOL_ANY_GL) |
| 15303 | _sg_gl_discard_sampler(smp); |
| 15304 | #elif defined(SOKOL_METAL) |
| 15305 | _sg_mtl_discard_sampler(smp); |
| 15306 | #elif defined(SOKOL_D3D11) |
| 15307 | _sg_d3d11_discard_sampler(smp); |
| 15308 | #elif defined(SOKOL_WGPU) |
| 15309 | _sg_wgpu_discard_sampler(smp); |
| 15310 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15311 | _sg_dummy_discard_sampler(smp); |
| 15312 | #else |
| 15313 | #error("INVALID BACKEND"); |
| 15314 | #endif |
| 15315 | } |
| 15316 | |
| 15317 | static inline sg_resource_state _sg_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) { |
| 15318 | #if defined(_SOKOL_ANY_GL) |
| 15319 | return _sg_gl_create_shader(shd, desc); |
| 15320 | #elif defined(SOKOL_METAL) |
| 15321 | return _sg_mtl_create_shader(shd, desc); |
| 15322 | #elif defined(SOKOL_D3D11) |
| 15323 | return _sg_d3d11_create_shader(shd, desc); |
| 15324 | #elif defined(SOKOL_WGPU) |
| 15325 | return _sg_wgpu_create_shader(shd, desc); |
| 15326 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15327 | return _sg_dummy_create_shader(shd, desc); |
| 15328 | #else |
| 15329 | #error("INVALID BACKEND"); |
| 15330 | #endif |
| 15331 | } |
| 15332 | |
| 15333 | static inline void _sg_discard_shader(_sg_shader_t* shd) { |
| 15334 | #if defined(_SOKOL_ANY_GL) |
| 15335 | _sg_gl_discard_shader(shd); |
| 15336 | #elif defined(SOKOL_METAL) |
| 15337 | _sg_mtl_discard_shader(shd); |
| 15338 | #elif defined(SOKOL_D3D11) |
| 15339 | _sg_d3d11_discard_shader(shd); |
| 15340 | #elif defined(SOKOL_WGPU) |
| 15341 | _sg_wgpu_discard_shader(shd); |
| 15342 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15343 | _sg_dummy_discard_shader(shd); |
| 15344 | #else |
| 15345 | #error("INVALID BACKEND"); |
| 15346 | #endif |
| 15347 | } |
| 15348 | |
| 15349 | static inline sg_resource_state _sg_create_pipeline(_sg_pipeline_t* pip, _sg_shader_t* shd, const sg_pipeline_desc* desc) { |
| 15350 | #if defined(_SOKOL_ANY_GL) |
| 15351 | return _sg_gl_create_pipeline(pip, shd, desc); |
| 15352 | #elif defined(SOKOL_METAL) |
| 15353 | return _sg_mtl_create_pipeline(pip, shd, desc); |
| 15354 | #elif defined(SOKOL_D3D11) |
| 15355 | return _sg_d3d11_create_pipeline(pip, shd, desc); |
| 15356 | #elif defined(SOKOL_WGPU) |
| 15357 | return _sg_wgpu_create_pipeline(pip, shd, desc); |
| 15358 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15359 | return _sg_dummy_create_pipeline(pip, shd, desc); |
| 15360 | #else |
| 15361 | #error("INVALID BACKEND"); |
| 15362 | #endif |
| 15363 | } |
| 15364 | |
| 15365 | static inline void _sg_discard_pipeline(_sg_pipeline_t* pip) { |
| 15366 | #if defined(_SOKOL_ANY_GL) |
| 15367 | _sg_gl_discard_pipeline(pip); |
| 15368 | #elif defined(SOKOL_METAL) |
| 15369 | _sg_mtl_discard_pipeline(pip); |
| 15370 | #elif defined(SOKOL_D3D11) |
| 15371 | _sg_d3d11_discard_pipeline(pip); |
| 15372 | #elif defined(SOKOL_WGPU) |
| 15373 | _sg_wgpu_discard_pipeline(pip); |
| 15374 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15375 | _sg_dummy_discard_pipeline(pip); |
| 15376 | #else |
| 15377 | #error("INVALID BACKEND"); |
| 15378 | #endif |
| 15379 | } |
| 15380 | |
| 15381 | static inline sg_resource_state _sg_create_attachments(_sg_attachments_t* atts, _sg_image_t** color_images, _sg_image_t** resolve_images, _sg_image_t* ds_image, const sg_attachments_desc* desc) { |
| 15382 | #if defined(_SOKOL_ANY_GL) |
| 15383 | return _sg_gl_create_attachments(atts, color_images, resolve_images, ds_image, desc); |
| 15384 | #elif defined(SOKOL_METAL) |
| 15385 | return _sg_mtl_create_attachments(atts, color_images, resolve_images, ds_image, desc); |
| 15386 | #elif defined(SOKOL_D3D11) |
| 15387 | return _sg_d3d11_create_attachments(atts, color_images, resolve_images, ds_image, desc); |
| 15388 | #elif defined(SOKOL_WGPU) |
| 15389 | return _sg_wgpu_create_attachments(atts, color_images, resolve_images, ds_image, desc); |
| 15390 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15391 | return _sg_dummy_create_attachments(atts, color_images, resolve_images, ds_image, desc); |
| 15392 | #else |
| 15393 | #error("INVALID BACKEND"); |
| 15394 | #endif |
| 15395 | } |
| 15396 | |
| 15397 | static inline void _sg_discard_attachments(_sg_attachments_t* atts) { |
| 15398 | #if defined(_SOKOL_ANY_GL) |
| 15399 | _sg_gl_discard_attachments(atts); |
| 15400 | #elif defined(SOKOL_METAL) |
| 15401 | _sg_mtl_discard_attachments(atts); |
| 15402 | #elif defined(SOKOL_D3D11) |
| 15403 | _sg_d3d11_discard_attachments(atts); |
| 15404 | #elif defined(SOKOL_WGPU) |
| 15405 | return _sg_wgpu_discard_attachments(atts); |
| 15406 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15407 | _sg_dummy_discard_attachments(atts); |
| 15408 | #else |
| 15409 | #error("INVALID BACKEND"); |
| 15410 | #endif |
| 15411 | } |
| 15412 | |
| 15413 | static inline _sg_image_t* _sg_attachments_color_image(const _sg_attachments_t* atts, int index) { |
| 15414 | #if defined(_SOKOL_ANY_GL) |
| 15415 | return _sg_gl_attachments_color_image(atts, index); |
| 15416 | #elif defined(SOKOL_METAL) |
| 15417 | return _sg_mtl_attachments_color_image(atts, index); |
| 15418 | #elif defined(SOKOL_D3D11) |
| 15419 | return _sg_d3d11_attachments_color_image(atts, index); |
| 15420 | #elif defined(SOKOL_WGPU) |
| 15421 | return _sg_wgpu_attachments_color_image(atts, index); |
| 15422 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15423 | return _sg_dummy_attachments_color_image(atts, index); |
| 15424 | #else |
| 15425 | #error("INVALID BACKEND"); |
| 15426 | #endif |
| 15427 | } |
| 15428 | |
| 15429 | static inline _sg_image_t* _sg_attachments_resolve_image(const _sg_attachments_t* atts, int index) { |
| 15430 | #if defined(_SOKOL_ANY_GL) |
| 15431 | return _sg_gl_attachments_resolve_image(atts, index); |
| 15432 | #elif defined(SOKOL_METAL) |
| 15433 | return _sg_mtl_attachments_resolve_image(atts, index); |
| 15434 | #elif defined(SOKOL_D3D11) |
| 15435 | return _sg_d3d11_attachments_resolve_image(atts, index); |
| 15436 | #elif defined(SOKOL_WGPU) |
| 15437 | return _sg_wgpu_attachments_resolve_image(atts, index); |
| 15438 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15439 | return _sg_dummy_attachments_resolve_image(atts, index); |
| 15440 | #else |
| 15441 | #error("INVALID BACKEND"); |
| 15442 | #endif |
| 15443 | } |
| 15444 | |
| 15445 | static inline _sg_image_t* _sg_attachments_ds_image(const _sg_attachments_t* atts) { |
| 15446 | #if defined(_SOKOL_ANY_GL) |
| 15447 | return _sg_gl_attachments_ds_image(atts); |
| 15448 | #elif defined(SOKOL_METAL) |
| 15449 | return _sg_mtl_attachments_ds_image(atts); |
| 15450 | #elif defined(SOKOL_D3D11) |
| 15451 | return _sg_d3d11_attachments_ds_image(atts); |
| 15452 | #elif defined(SOKOL_WGPU) |
| 15453 | return _sg_wgpu_attachments_ds_image(atts); |
| 15454 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15455 | return _sg_dummy_attachments_ds_image(atts); |
| 15456 | #else |
| 15457 | #error("INVALID BACKEND"); |
| 15458 | #endif |
| 15459 | } |
| 15460 | |
| 15461 | static inline void _sg_begin_pass(const sg_pass* pass) { |
| 15462 | #if defined(_SOKOL_ANY_GL) |
| 15463 | _sg_gl_begin_pass(pass); |
| 15464 | #elif defined(SOKOL_METAL) |
| 15465 | _sg_mtl_begin_pass(pass); |
| 15466 | #elif defined(SOKOL_D3D11) |
| 15467 | _sg_d3d11_begin_pass(pass); |
| 15468 | #elif defined(SOKOL_WGPU) |
| 15469 | _sg_wgpu_begin_pass(pass); |
| 15470 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15471 | _sg_dummy_begin_pass(pass); |
| 15472 | #else |
| 15473 | #error("INVALID BACKEND"); |
| 15474 | #endif |
| 15475 | } |
| 15476 | |
| 15477 | static inline void _sg_end_pass(void) { |
| 15478 | #if defined(_SOKOL_ANY_GL) |
| 15479 | _sg_gl_end_pass(); |
| 15480 | #elif defined(SOKOL_METAL) |
| 15481 | _sg_mtl_end_pass(); |
| 15482 | #elif defined(SOKOL_D3D11) |
| 15483 | _sg_d3d11_end_pass(); |
| 15484 | #elif defined(SOKOL_WGPU) |
| 15485 | _sg_wgpu_end_pass(); |
| 15486 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15487 | _sg_dummy_end_pass(); |
| 15488 | #else |
| 15489 | #error("INVALID BACKEND"); |
| 15490 | #endif |
| 15491 | } |
| 15492 | |
| 15493 | static inline void _sg_apply_viewport(int x, int y, int w, int h, bool origin_top_left) { |
| 15494 | #if defined(_SOKOL_ANY_GL) |
| 15495 | _sg_gl_apply_viewport(x, y, w, h, origin_top_left); |
| 15496 | #elif defined(SOKOL_METAL) |
| 15497 | _sg_mtl_apply_viewport(x, y, w, h, origin_top_left); |
| 15498 | #elif defined(SOKOL_D3D11) |
| 15499 | _sg_d3d11_apply_viewport(x, y, w, h, origin_top_left); |
| 15500 | #elif defined(SOKOL_WGPU) |
| 15501 | _sg_wgpu_apply_viewport(x, y, w, h, origin_top_left); |
| 15502 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15503 | _sg_dummy_apply_viewport(x, y, w, h, origin_top_left); |
| 15504 | #else |
| 15505 | #error("INVALID BACKEND"); |
| 15506 | #endif |
| 15507 | } |
| 15508 | |
| 15509 | static inline void _sg_apply_scissor_rect(int x, int y, int w, int h, bool origin_top_left) { |
| 15510 | #if defined(_SOKOL_ANY_GL) |
| 15511 | _sg_gl_apply_scissor_rect(x, y, w, h, origin_top_left); |
| 15512 | #elif defined(SOKOL_METAL) |
| 15513 | _sg_mtl_apply_scissor_rect(x, y, w, h, origin_top_left); |
| 15514 | #elif defined(SOKOL_D3D11) |
| 15515 | _sg_d3d11_apply_scissor_rect(x, y, w, h, origin_top_left); |
| 15516 | #elif defined(SOKOL_WGPU) |
| 15517 | _sg_wgpu_apply_scissor_rect(x, y, w, h, origin_top_left); |
| 15518 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15519 | _sg_dummy_apply_scissor_rect(x, y, w, h, origin_top_left); |
| 15520 | #else |
| 15521 | #error("INVALID BACKEND"); |
| 15522 | #endif |
| 15523 | } |
| 15524 | |
| 15525 | static inline void _sg_apply_pipeline(_sg_pipeline_t* pip) { |
| 15526 | #if defined(_SOKOL_ANY_GL) |
| 15527 | _sg_gl_apply_pipeline(pip); |
| 15528 | #elif defined(SOKOL_METAL) |
| 15529 | _sg_mtl_apply_pipeline(pip); |
| 15530 | #elif defined(SOKOL_D3D11) |
| 15531 | _sg_d3d11_apply_pipeline(pip); |
| 15532 | #elif defined(SOKOL_WGPU) |
| 15533 | _sg_wgpu_apply_pipeline(pip); |
| 15534 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15535 | _sg_dummy_apply_pipeline(pip); |
| 15536 | #else |
| 15537 | #error("INVALID BACKEND"); |
| 15538 | #endif |
| 15539 | } |
| 15540 | |
| 15541 | static inline bool _sg_apply_bindings(_sg_bindings_t* bnd) { |
| 15542 | #if defined(_SOKOL_ANY_GL) |
| 15543 | return _sg_gl_apply_bindings(bnd); |
| 15544 | #elif defined(SOKOL_METAL) |
| 15545 | return _sg_mtl_apply_bindings(bnd); |
| 15546 | #elif defined(SOKOL_D3D11) |
| 15547 | return _sg_d3d11_apply_bindings(bnd); |
| 15548 | #elif defined(SOKOL_WGPU) |
| 15549 | return _sg_wgpu_apply_bindings(bnd); |
| 15550 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15551 | return _sg_dummy_apply_bindings(bnd); |
| 15552 | #else |
| 15553 | #error("INVALID BACKEND"); |
| 15554 | #endif |
| 15555 | } |
| 15556 | |
| 15557 | static inline void _sg_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) { |
| 15558 | #if defined(_SOKOL_ANY_GL) |
| 15559 | _sg_gl_apply_uniforms(stage_index, ub_index, data); |
| 15560 | #elif defined(SOKOL_METAL) |
| 15561 | _sg_mtl_apply_uniforms(stage_index, ub_index, data); |
| 15562 | #elif defined(SOKOL_D3D11) |
| 15563 | _sg_d3d11_apply_uniforms(stage_index, ub_index, data); |
| 15564 | #elif defined(SOKOL_WGPU) |
| 15565 | _sg_wgpu_apply_uniforms(stage_index, ub_index, data); |
| 15566 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15567 | _sg_dummy_apply_uniforms(stage_index, ub_index, data); |
| 15568 | #else |
| 15569 | #error("INVALID BACKEND"); |
| 15570 | #endif |
| 15571 | } |
| 15572 | |
| 15573 | static inline void _sg_draw(int base_element, int num_elements, int num_instances) { |
| 15574 | #if defined(_SOKOL_ANY_GL) |
| 15575 | _sg_gl_draw(base_element, num_elements, num_instances); |
| 15576 | #elif defined(SOKOL_METAL) |
| 15577 | _sg_mtl_draw(base_element, num_elements, num_instances); |
| 15578 | #elif defined(SOKOL_D3D11) |
| 15579 | _sg_d3d11_draw(base_element, num_elements, num_instances); |
| 15580 | #elif defined(SOKOL_WGPU) |
| 15581 | _sg_wgpu_draw(base_element, num_elements, num_instances); |
| 15582 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15583 | _sg_dummy_draw(base_element, num_elements, num_instances); |
| 15584 | #else |
| 15585 | #error("INVALID BACKEND"); |
| 15586 | #endif |
| 15587 | } |
| 15588 | |
| 15589 | static inline void _sg_commit(void) { |
| 15590 | #if defined(_SOKOL_ANY_GL) |
| 15591 | _sg_gl_commit(); |
| 15592 | #elif defined(SOKOL_METAL) |
| 15593 | _sg_mtl_commit(); |
| 15594 | #elif defined(SOKOL_D3D11) |
| 15595 | _sg_d3d11_commit(); |
| 15596 | #elif defined(SOKOL_WGPU) |
| 15597 | _sg_wgpu_commit(); |
| 15598 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15599 | _sg_dummy_commit(); |
| 15600 | #else |
| 15601 | #error("INVALID BACKEND"); |
| 15602 | #endif |
| 15603 | } |
| 15604 | |
| 15605 | static inline void _sg_update_buffer(_sg_buffer_t* buf, const sg_range* data) { |
| 15606 | #if defined(_SOKOL_ANY_GL) |
| 15607 | _sg_gl_update_buffer(buf, data); |
| 15608 | #elif defined(SOKOL_METAL) |
| 15609 | _sg_mtl_update_buffer(buf, data); |
| 15610 | #elif defined(SOKOL_D3D11) |
| 15611 | _sg_d3d11_update_buffer(buf, data); |
| 15612 | #elif defined(SOKOL_WGPU) |
| 15613 | _sg_wgpu_update_buffer(buf, data); |
| 15614 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15615 | _sg_dummy_update_buffer(buf, data); |
| 15616 | #else |
| 15617 | #error("INVALID BACKEND"); |
| 15618 | #endif |
| 15619 | } |
| 15620 | |
| 15621 | static inline void _sg_append_buffer(_sg_buffer_t* buf, const sg_range* data, bool new_frame) { |
| 15622 | #if defined(_SOKOL_ANY_GL) |
| 15623 | _sg_gl_append_buffer(buf, data, new_frame); |
| 15624 | #elif defined(SOKOL_METAL) |
| 15625 | _sg_mtl_append_buffer(buf, data, new_frame); |
| 15626 | #elif defined(SOKOL_D3D11) |
| 15627 | _sg_d3d11_append_buffer(buf, data, new_frame); |
| 15628 | #elif defined(SOKOL_WGPU) |
| 15629 | _sg_wgpu_append_buffer(buf, data, new_frame); |
| 15630 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15631 | _sg_dummy_append_buffer(buf, data, new_frame); |
| 15632 | #else |
| 15633 | #error("INVALID BACKEND"); |
| 15634 | #endif |
| 15635 | } |
| 15636 | |
| 15637 | static inline void _sg_update_image(_sg_image_t* img, const sg_image_data* data) { |
| 15638 | #if defined(_SOKOL_ANY_GL) |
| 15639 | _sg_gl_update_image(img, data); |
| 15640 | #elif defined(SOKOL_METAL) |
| 15641 | _sg_mtl_update_image(img, data); |
| 15642 | #elif defined(SOKOL_D3D11) |
| 15643 | _sg_d3d11_update_image(img, data); |
| 15644 | #elif defined(SOKOL_WGPU) |
| 15645 | _sg_wgpu_update_image(img, data); |
| 15646 | #elif defined(SOKOL_DUMMY_BACKEND) |
| 15647 | _sg_dummy_update_image(img, data); |
| 15648 | #else |
| 15649 | #error("INVALID BACKEND"); |
| 15650 | #endif |
| 15651 | } |
| 15652 | |
| 15653 | static inline void _sg_push_debug_group(const char* name) { |
| 15654 | #if defined(SOKOL_METAL) |
| 15655 | _sg_mtl_push_debug_group(name); |
| 15656 | #else |
| 15657 | _SOKOL_UNUSED(name); |
| 15658 | #endif |
| 15659 | } |
| 15660 | |
| 15661 | static inline void _sg_pop_debug_group(void) { |
| 15662 | #if defined(SOKOL_METAL) |
| 15663 | _sg_mtl_pop_debug_group(); |
| 15664 | #endif |
| 15665 | } |
| 15666 | |
| 15667 | // ██████ ██████ ██████ ██ |
| 15668 | // ██ ██ ██ ██ ██ ██ ██ |
| 15669 | // ██████ ██ ██ ██ ██ ██ |
| 15670 | // ██ ██ ██ ██ ██ ██ |
| 15671 | // ██ ██████ ██████ ███████ |
| 15672 | // |
| 15673 | // >>pool |
| 15674 | _SOKOL_PRIVATE void _sg_init_pool(_sg_pool_t* pool, int num) { |
| 15675 | SOKOL_ASSERT(pool && (num >= 1)); |
| 15676 | // slot 0 is reserved for the 'invalid id', so bump the pool size by 1 |
| 15677 | pool->size = num + 1; |
| 15678 | pool->queue_top = 0; |
| 15679 | // generation counters indexable by pool slot index, slot 0 is reserved |
| 15680 | size_t gen_ctrs_size = sizeof(uint32_t) * (size_t)pool->size; |
| 15681 | pool->gen_ctrs = (uint32_t*)_sg_malloc_clear(gen_ctrs_size); |
| 15682 | // it's not a bug to only reserve 'num' here |
| 15683 | pool->free_queue = (int*) _sg_malloc_clear(sizeof(int) * (size_t)num); |
| 15684 | // never allocate the zero-th pool item since the invalid id is 0 |
| 15685 | for (int i = pool->size-1; i >= 1; i--) { |
| 15686 | pool->free_queue[pool->queue_top++] = i; |
| 15687 | } |
| 15688 | } |
| 15689 | |
| 15690 | _SOKOL_PRIVATE void _sg_discard_pool(_sg_pool_t* pool) { |
| 15691 | SOKOL_ASSERT(pool); |
| 15692 | SOKOL_ASSERT(pool->free_queue); |
| 15693 | _sg_free(pool->free_queue); |
| 15694 | pool->free_queue = 0; |
| 15695 | SOKOL_ASSERT(pool->gen_ctrs); |
| 15696 | _sg_free(pool->gen_ctrs); |
| 15697 | pool->gen_ctrs = 0; |
| 15698 | pool->size = 0; |
| 15699 | pool->queue_top = 0; |
| 15700 | } |
| 15701 | |
| 15702 | _SOKOL_PRIVATE int _sg_pool_alloc_index(_sg_pool_t* pool) { |
| 15703 | SOKOL_ASSERT(pool); |
| 15704 | SOKOL_ASSERT(pool->free_queue); |
| 15705 | if (pool->queue_top > 0) { |
| 15706 | int slot_index = pool->free_queue[--pool->queue_top]; |
| 15707 | SOKOL_ASSERT((slot_index > 0) && (slot_index < pool->size)); |
| 15708 | return slot_index; |
| 15709 | } else { |
| 15710 | // pool exhausted |
| 15711 | return _SG_INVALID_SLOT_INDEX; |
| 15712 | } |
| 15713 | } |
| 15714 | |
| 15715 | _SOKOL_PRIVATE void _sg_pool_free_index(_sg_pool_t* pool, int slot_index) { |
| 15716 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < pool->size)); |
| 15717 | SOKOL_ASSERT(pool); |
| 15718 | SOKOL_ASSERT(pool->free_queue); |
| 15719 | SOKOL_ASSERT(pool->queue_top < pool->size); |
| 15720 | #ifdef SOKOL_DEBUG |
| 15721 | // debug check against double-free |
| 15722 | for (int i = 0; i < pool->queue_top; i++) { |
| 15723 | SOKOL_ASSERT(pool->free_queue[i] != slot_index); |
| 15724 | } |
| 15725 | #endif |
| 15726 | pool->free_queue[pool->queue_top++] = slot_index; |
| 15727 | SOKOL_ASSERT(pool->queue_top <= (pool->size-1)); |
| 15728 | } |
| 15729 | |
| 15730 | _SOKOL_PRIVATE void _sg_reset_slot(_sg_slot_t* slot) { |
| 15731 | SOKOL_ASSERT(slot); |
| 15732 | _sg_clear(slot, sizeof(_sg_slot_t)); |
| 15733 | } |
| 15734 | |
| 15735 | _SOKOL_PRIVATE void _sg_reset_buffer_to_alloc_state(_sg_buffer_t* buf) { |
| 15736 | SOKOL_ASSERT(buf); |
| 15737 | _sg_slot_t slot = buf->slot; |
| 15738 | _sg_clear(buf, sizeof(*buf)); |
| 15739 | buf->slot = slot; |
| 15740 | buf->slot.state = SG_RESOURCESTATE_ALLOC; |
| 15741 | } |
| 15742 | |
| 15743 | _SOKOL_PRIVATE void _sg_reset_image_to_alloc_state(_sg_image_t* img) { |
| 15744 | SOKOL_ASSERT(img); |
| 15745 | _sg_slot_t slot = img->slot; |
| 15746 | _sg_clear(img, sizeof(*img)); |
| 15747 | img->slot = slot; |
| 15748 | img->slot.state = SG_RESOURCESTATE_ALLOC; |
| 15749 | } |
| 15750 | |
| 15751 | _SOKOL_PRIVATE void _sg_reset_sampler_to_alloc_state(_sg_sampler_t* smp) { |
| 15752 | SOKOL_ASSERT(smp); |
| 15753 | _sg_slot_t slot = smp->slot; |
| 15754 | _sg_clear(smp, sizeof(*smp)); |
| 15755 | smp->slot = slot; |
| 15756 | smp->slot.state = SG_RESOURCESTATE_ALLOC; |
| 15757 | } |
| 15758 | |
| 15759 | _SOKOL_PRIVATE void _sg_reset_shader_to_alloc_state(_sg_shader_t* shd) { |
| 15760 | SOKOL_ASSERT(shd); |
| 15761 | _sg_slot_t slot = shd->slot; |
| 15762 | _sg_clear(shd, sizeof(*shd)); |
| 15763 | shd->slot = slot; |
| 15764 | shd->slot.state = SG_RESOURCESTATE_ALLOC; |
| 15765 | } |
| 15766 | |
| 15767 | _SOKOL_PRIVATE void _sg_reset_pipeline_to_alloc_state(_sg_pipeline_t* pip) { |
| 15768 | SOKOL_ASSERT(pip); |
| 15769 | _sg_slot_t slot = pip->slot; |
| 15770 | _sg_clear(pip, sizeof(*pip)); |
| 15771 | pip->slot = slot; |
| 15772 | pip->slot.state = SG_RESOURCESTATE_ALLOC; |
| 15773 | } |
| 15774 | |
| 15775 | _SOKOL_PRIVATE void _sg_reset_attachments_to_alloc_state(_sg_attachments_t* atts) { |
| 15776 | SOKOL_ASSERT(atts); |
| 15777 | _sg_slot_t slot = atts->slot; |
| 15778 | _sg_clear(atts, sizeof(*atts)); |
| 15779 | atts->slot = slot; |
| 15780 | atts->slot.state = SG_RESOURCESTATE_ALLOC; |
| 15781 | } |
| 15782 | |
| 15783 | _SOKOL_PRIVATE void _sg_setup_pools(_sg_pools_t* p, const sg_desc* desc) { |
| 15784 | SOKOL_ASSERT(p); |
| 15785 | SOKOL_ASSERT(desc); |
| 15786 | // note: the pools here will have an additional item, since slot 0 is reserved |
| 15787 | SOKOL_ASSERT((desc->buffer_pool_size > 0) && (desc->buffer_pool_size < _SG_MAX_POOL_SIZE)); |
| 15788 | _sg_init_pool(&p->buffer_pool, desc->buffer_pool_size); |
| 15789 | size_t buffer_pool_byte_size = sizeof(_sg_buffer_t) * (size_t)p->buffer_pool.size; |
| 15790 | p->buffers = (_sg_buffer_t*) _sg_malloc_clear(buffer_pool_byte_size); |
| 15791 | |
| 15792 | SOKOL_ASSERT((desc->image_pool_size > 0) && (desc->image_pool_size < _SG_MAX_POOL_SIZE)); |
| 15793 | _sg_init_pool(&p->image_pool, desc->image_pool_size); |
| 15794 | size_t image_pool_byte_size = sizeof(_sg_image_t) * (size_t)p->image_pool.size; |
| 15795 | p->images = (_sg_image_t*) _sg_malloc_clear(image_pool_byte_size); |
| 15796 | |
| 15797 | SOKOL_ASSERT((desc->sampler_pool_size > 0) && (desc->sampler_pool_size < _SG_MAX_POOL_SIZE)); |
| 15798 | _sg_init_pool(&p->sampler_pool, desc->sampler_pool_size); |
| 15799 | size_t sampler_pool_byte_size = sizeof(_sg_sampler_t) * (size_t)p->sampler_pool.size; |
| 15800 | p->samplers = (_sg_sampler_t*) _sg_malloc_clear(sampler_pool_byte_size); |
| 15801 | |
| 15802 | SOKOL_ASSERT((desc->shader_pool_size > 0) && (desc->shader_pool_size < _SG_MAX_POOL_SIZE)); |
| 15803 | _sg_init_pool(&p->shader_pool, desc->shader_pool_size); |
| 15804 | size_t shader_pool_byte_size = sizeof(_sg_shader_t) * (size_t)p->shader_pool.size; |
| 15805 | p->shaders = (_sg_shader_t*) _sg_malloc_clear(shader_pool_byte_size); |
| 15806 | |
| 15807 | SOKOL_ASSERT((desc->pipeline_pool_size > 0) && (desc->pipeline_pool_size < _SG_MAX_POOL_SIZE)); |
| 15808 | _sg_init_pool(&p->pipeline_pool, desc->pipeline_pool_size); |
| 15809 | size_t pipeline_pool_byte_size = sizeof(_sg_pipeline_t) * (size_t)p->pipeline_pool.size; |
| 15810 | p->pipelines = (_sg_pipeline_t*) _sg_malloc_clear(pipeline_pool_byte_size); |
| 15811 | |
| 15812 | SOKOL_ASSERT((desc->attachments_pool_size > 0) && (desc->attachments_pool_size < _SG_MAX_POOL_SIZE)); |
| 15813 | _sg_init_pool(&p->attachments_pool, desc->attachments_pool_size); |
| 15814 | size_t attachments_pool_byte_size = sizeof(_sg_attachments_t) * (size_t)p->attachments_pool.size; |
| 15815 | p->attachments = (_sg_attachments_t*) _sg_malloc_clear(attachments_pool_byte_size); |
| 15816 | } |
| 15817 | |
| 15818 | _SOKOL_PRIVATE void _sg_discard_pools(_sg_pools_t* p) { |
| 15819 | SOKOL_ASSERT(p); |
| 15820 | _sg_free(p->attachments); p->attachments = 0; |
| 15821 | _sg_free(p->pipelines); p->pipelines = 0; |
| 15822 | _sg_free(p->shaders); p->shaders = 0; |
| 15823 | _sg_free(p->samplers); p->samplers = 0; |
| 15824 | _sg_free(p->images); p->images = 0; |
| 15825 | _sg_free(p->buffers); p->buffers = 0; |
| 15826 | _sg_discard_pool(&p->attachments_pool); |
| 15827 | _sg_discard_pool(&p->pipeline_pool); |
| 15828 | _sg_discard_pool(&p->shader_pool); |
| 15829 | _sg_discard_pool(&p->sampler_pool); |
| 15830 | _sg_discard_pool(&p->image_pool); |
| 15831 | _sg_discard_pool(&p->buffer_pool); |
| 15832 | } |
| 15833 | |
| 15834 | /* allocate the slot at slot_index: |
| 15835 | - bump the slot's generation counter |
| 15836 | - create a resource id from the generation counter and slot index |
| 15837 | - set the slot's id to this id |
| 15838 | - set the slot's state to ALLOC |
| 15839 | - return the resource id |
| 15840 | */ |
| 15841 | _SOKOL_PRIVATE uint32_t _sg_slot_alloc(_sg_pool_t* pool, _sg_slot_t* slot, int slot_index) { |
| 15842 | /* FIXME: add handling for an overflowing generation counter, |
| 15843 | for now, just overflow (another option is to disable |
| 15844 | the slot) |
| 15845 | */ |
| 15846 | SOKOL_ASSERT(pool && pool->gen_ctrs); |
| 15847 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < pool->size)); |
| 15848 | SOKOL_ASSERT(slot->id == SG_INVALID_ID); |
| 15849 | SOKOL_ASSERT(slot->state == SG_RESOURCESTATE_INITIAL); |
| 15850 | uint32_t ctr = ++pool->gen_ctrs[slot_index]; |
| 15851 | slot->id = (ctr<<_SG_SLOT_SHIFT)|(slot_index & _SG_SLOT_MASK); |
| 15852 | slot->state = SG_RESOURCESTATE_ALLOC; |
| 15853 | return slot->id; |
| 15854 | } |
| 15855 | |
| 15856 | // extract slot index from id |
| 15857 | _SOKOL_PRIVATE int _sg_slot_index(uint32_t id) { |
| 15858 | int slot_index = (int) (id & _SG_SLOT_MASK); |
| 15859 | SOKOL_ASSERT(_SG_INVALID_SLOT_INDEX != slot_index); |
| 15860 | return slot_index; |
| 15861 | } |
| 15862 | |
| 15863 | // returns pointer to resource by id without matching id check |
| 15864 | _SOKOL_PRIVATE _sg_buffer_t* _sg_buffer_at(const _sg_pools_t* p, uint32_t buf_id) { |
| 15865 | SOKOL_ASSERT(p && (SG_INVALID_ID != buf_id)); |
| 15866 | int slot_index = _sg_slot_index(buf_id); |
| 15867 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->buffer_pool.size)); |
| 15868 | return &p->buffers[slot_index]; |
| 15869 | } |
| 15870 | |
| 15871 | _SOKOL_PRIVATE _sg_image_t* _sg_image_at(const _sg_pools_t* p, uint32_t img_id) { |
| 15872 | SOKOL_ASSERT(p && (SG_INVALID_ID != img_id)); |
| 15873 | int slot_index = _sg_slot_index(img_id); |
| 15874 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->image_pool.size)); |
| 15875 | return &p->images[slot_index]; |
| 15876 | } |
| 15877 | |
| 15878 | _SOKOL_PRIVATE _sg_sampler_t* _sg_sampler_at(const _sg_pools_t* p, uint32_t smp_id) { |
| 15879 | SOKOL_ASSERT(p && (SG_INVALID_ID != smp_id)); |
| 15880 | int slot_index = _sg_slot_index(smp_id); |
| 15881 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->sampler_pool.size)); |
| 15882 | return &p->samplers[slot_index]; |
| 15883 | } |
| 15884 | |
| 15885 | _SOKOL_PRIVATE _sg_shader_t* _sg_shader_at(const _sg_pools_t* p, uint32_t shd_id) { |
| 15886 | SOKOL_ASSERT(p && (SG_INVALID_ID != shd_id)); |
| 15887 | int slot_index = _sg_slot_index(shd_id); |
| 15888 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->shader_pool.size)); |
| 15889 | return &p->shaders[slot_index]; |
| 15890 | } |
| 15891 | |
| 15892 | _SOKOL_PRIVATE _sg_pipeline_t* _sg_pipeline_at(const _sg_pools_t* p, uint32_t pip_id) { |
| 15893 | SOKOL_ASSERT(p && (SG_INVALID_ID != pip_id)); |
| 15894 | int slot_index = _sg_slot_index(pip_id); |
| 15895 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->pipeline_pool.size)); |
| 15896 | return &p->pipelines[slot_index]; |
| 15897 | } |
| 15898 | |
| 15899 | _SOKOL_PRIVATE _sg_attachments_t* _sg_attachments_at(const _sg_pools_t* p, uint32_t atts_id) { |
| 15900 | SOKOL_ASSERT(p && (SG_INVALID_ID != atts_id)); |
| 15901 | int slot_index = _sg_slot_index(atts_id); |
| 15902 | SOKOL_ASSERT((slot_index > _SG_INVALID_SLOT_INDEX) && (slot_index < p->attachments_pool.size)); |
| 15903 | return &p->attachments[slot_index]; |
| 15904 | } |
| 15905 | |
| 15906 | // returns pointer to resource with matching id check, may return 0 |
| 15907 | _SOKOL_PRIVATE _sg_buffer_t* _sg_lookup_buffer(const _sg_pools_t* p, uint32_t buf_id) { |
| 15908 | if (SG_INVALID_ID != buf_id) { |
| 15909 | _sg_buffer_t* buf = _sg_buffer_at(p, buf_id); |
| 15910 | if (buf->slot.id == buf_id) { |
| 15911 | return buf; |
| 15912 | } |
| 15913 | } |
| 15914 | return 0; |
| 15915 | } |
| 15916 | |
| 15917 | _SOKOL_PRIVATE _sg_image_t* _sg_lookup_image(const _sg_pools_t* p, uint32_t img_id) { |
| 15918 | if (SG_INVALID_ID != img_id) { |
| 15919 | _sg_image_t* img = _sg_image_at(p, img_id); |
| 15920 | if (img->slot.id == img_id) { |
| 15921 | return img; |
| 15922 | } |
| 15923 | } |
| 15924 | return 0; |
| 15925 | } |
| 15926 | |
| 15927 | _SOKOL_PRIVATE _sg_sampler_t* _sg_lookup_sampler(const _sg_pools_t* p, uint32_t smp_id) { |
| 15928 | if (SG_INVALID_ID != smp_id) { |
| 15929 | _sg_sampler_t* smp = _sg_sampler_at(p, smp_id); |
| 15930 | if (smp->slot.id == smp_id) { |
| 15931 | return smp; |
| 15932 | } |
| 15933 | } |
| 15934 | return 0; |
| 15935 | } |
| 15936 | |
| 15937 | _SOKOL_PRIVATE _sg_shader_t* _sg_lookup_shader(const _sg_pools_t* p, uint32_t shd_id) { |
| 15938 | SOKOL_ASSERT(p); |
| 15939 | if (SG_INVALID_ID != shd_id) { |
| 15940 | _sg_shader_t* shd = _sg_shader_at(p, shd_id); |
| 15941 | if (shd->slot.id == shd_id) { |
| 15942 | return shd; |
| 15943 | } |
| 15944 | } |
| 15945 | return 0; |
| 15946 | } |
| 15947 | |
| 15948 | _SOKOL_PRIVATE _sg_pipeline_t* _sg_lookup_pipeline(const _sg_pools_t* p, uint32_t pip_id) { |
| 15949 | SOKOL_ASSERT(p); |
| 15950 | if (SG_INVALID_ID != pip_id) { |
| 15951 | _sg_pipeline_t* pip = _sg_pipeline_at(p, pip_id); |
| 15952 | if (pip->slot.id == pip_id) { |
| 15953 | return pip; |
| 15954 | } |
| 15955 | } |
| 15956 | return 0; |
| 15957 | } |
| 15958 | |
| 15959 | _SOKOL_PRIVATE _sg_attachments_t* _sg_lookup_attachments(const _sg_pools_t* p, uint32_t atts_id) { |
| 15960 | SOKOL_ASSERT(p); |
| 15961 | if (SG_INVALID_ID != atts_id) { |
| 15962 | _sg_attachments_t* atts = _sg_attachments_at(p, atts_id); |
| 15963 | if (atts->slot.id == atts_id) { |
| 15964 | return atts; |
| 15965 | } |
| 15966 | } |
| 15967 | return 0; |
| 15968 | } |
| 15969 | |
| 15970 | _SOKOL_PRIVATE void _sg_discard_all_resources(_sg_pools_t* p) { |
| 15971 | /* this is a bit dumb since it loops over all pool slots to |
| 15972 | find the occupied slots, on the other hand it is only ever |
| 15973 | executed at shutdown |
| 15974 | NOTE: ONLY EXECUTE THIS AT SHUTDOWN |
| 15975 | ...because the free queues will not be reset |
| 15976 | and the resource slots not be cleared! |
| 15977 | */ |
| 15978 | for (int i = 1; i < p->buffer_pool.size; i++) { |
| 15979 | sg_resource_state state = p->buffers[i].slot.state; |
| 15980 | if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) { |
| 15981 | _sg_discard_buffer(&p->buffers[i]); |
| 15982 | } |
| 15983 | } |
| 15984 | for (int i = 1; i < p->image_pool.size; i++) { |
| 15985 | sg_resource_state state = p->images[i].slot.state; |
| 15986 | if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) { |
| 15987 | _sg_discard_image(&p->images[i]); |
| 15988 | } |
| 15989 | } |
| 15990 | for (int i = 1; i < p->sampler_pool.size; i++) { |
| 15991 | sg_resource_state state = p->samplers[i].slot.state; |
| 15992 | if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) { |
| 15993 | _sg_discard_sampler(&p->samplers[i]); |
| 15994 | } |
| 15995 | } |
| 15996 | for (int i = 1; i < p->shader_pool.size; i++) { |
| 15997 | sg_resource_state state = p->shaders[i].slot.state; |
| 15998 | if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) { |
| 15999 | _sg_discard_shader(&p->shaders[i]); |
| 16000 | } |
| 16001 | } |
| 16002 | for (int i = 1; i < p->pipeline_pool.size; i++) { |
| 16003 | sg_resource_state state = p->pipelines[i].slot.state; |
| 16004 | if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) { |
| 16005 | _sg_discard_pipeline(&p->pipelines[i]); |
| 16006 | } |
| 16007 | } |
| 16008 | for (int i = 1; i < p->attachments_pool.size; i++) { |
| 16009 | sg_resource_state state = p->attachments[i].slot.state; |
| 16010 | if ((state == SG_RESOURCESTATE_VALID) || (state == SG_RESOURCESTATE_FAILED)) { |
| 16011 | _sg_discard_attachments(&p->attachments[i]); |
| 16012 | } |
| 16013 | } |
| 16014 | } |
| 16015 | |
| 16016 | // ██ ██ █████ ██ ██ ██████ █████ ████████ ██ ██████ ███ ██ |
| 16017 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ |
| 16018 | // ██ ██ ███████ ██ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ |
| 16019 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 16020 | // ████ ██ ██ ███████ ██ ██████ ██ ██ ██ ██ ██████ ██ ████ |
| 16021 | // |
| 16022 | // >>validation |
| 16023 | #if defined(SOKOL_DEBUG) |
| 16024 | _SOKOL_PRIVATE void _sg_validate_begin(void) { |
| 16025 | _sg.validate_error = SG_LOGITEM_OK; |
| 16026 | } |
| 16027 | |
| 16028 | _SOKOL_PRIVATE bool _sg_validate_end(void) { |
| 16029 | if (_sg.validate_error != SG_LOGITEM_OK) { |
| 16030 | #if !defined(SOKOL_VALIDATE_NON_FATAL) |
| 16031 | _SG_PANIC(VALIDATION_FAILED); |
| 16032 | return false; |
| 16033 | #else |
| 16034 | return false; |
| 16035 | #endif |
| 16036 | } else { |
| 16037 | return true; |
| 16038 | } |
| 16039 | } |
| 16040 | #endif |
| 16041 | |
| 16042 | _SOKOL_PRIVATE bool _sg_validate_buffer_desc(const sg_buffer_desc* desc) { |
| 16043 | #if !defined(SOKOL_DEBUG) |
| 16044 | _SOKOL_UNUSED(desc); |
| 16045 | return true; |
| 16046 | #else |
| 16047 | if (_sg.desc.disable_validation) { |
| 16048 | return true; |
| 16049 | } |
| 16050 | SOKOL_ASSERT(desc); |
| 16051 | _sg_validate_begin(); |
| 16052 | _SG_VALIDATE(desc->_start_canary == 0, VALIDATE_BUFFERDESC_CANARY); |
| 16053 | _SG_VALIDATE(desc->_end_canary == 0, VALIDATE_BUFFERDESC_CANARY); |
| 16054 | _SG_VALIDATE(desc->size > 0, VALIDATE_BUFFERDESC_SIZE); |
| 16055 | bool injected = (0 != desc->gl_buffers[0]) || |
| 16056 | (0 != desc->mtl_buffers[0]) || |
| 16057 | (0 != desc->d3d11_buffer) || |
| 16058 | (0 != desc->wgpu_buffer); |
| 16059 | if (!injected && (desc->usage == SG_USAGE_IMMUTABLE)) { |
| 16060 | _SG_VALIDATE((0 != desc->data.ptr) && (desc->data.size > 0), VALIDATE_BUFFERDESC_DATA); |
| 16061 | _SG_VALIDATE(desc->size == desc->data.size, VALIDATE_BUFFERDESC_DATA_SIZE); |
| 16062 | } else { |
| 16063 | _SG_VALIDATE(0 == desc->data.ptr, VALIDATE_BUFFERDESC_NO_DATA); |
| 16064 | } |
| 16065 | if (desc->type == SG_BUFFERTYPE_STORAGEBUFFER) { |
| 16066 | _SG_VALIDATE(_sg.features.storage_buffer, VALIDATE_BUFFERDESC_STORAGEBUFFER_SUPPORTED); |
| 16067 | _SG_VALIDATE(_sg_multiple_u64(desc->size, 4), VALIDATE_BUFFERDESC_STORAGEBUFFER_SIZE_MULTIPLE_4); |
| 16068 | } |
| 16069 | return _sg_validate_end(); |
| 16070 | #endif |
| 16071 | } |
| 16072 | |
| 16073 | _SOKOL_PRIVATE void _sg_validate_image_data(const sg_image_data* data, sg_pixel_format fmt, int width, int height, int num_faces, int num_mips, int num_slices) { |
| 16074 | #if !defined(SOKOL_DEBUG) |
| 16075 | _SOKOL_UNUSED(data); |
| 16076 | _SOKOL_UNUSED(fmt); |
| 16077 | _SOKOL_UNUSED(width); |
| 16078 | _SOKOL_UNUSED(height); |
| 16079 | _SOKOL_UNUSED(num_faces); |
| 16080 | _SOKOL_UNUSED(num_mips); |
| 16081 | _SOKOL_UNUSED(num_slices); |
| 16082 | #else |
| 16083 | for (int face_index = 0; face_index < num_faces; face_index++) { |
| 16084 | for (int mip_index = 0; mip_index < num_mips; mip_index++) { |
| 16085 | const bool has_data = data->subimage[face_index][mip_index].ptr != 0; |
| 16086 | const bool has_size = data->subimage[face_index][mip_index].size > 0; |
| 16087 | _SG_VALIDATE(has_data && has_size, VALIDATE_IMAGEDATA_NODATA); |
| 16088 | const int mip_width = _sg_miplevel_dim(width, mip_index); |
| 16089 | const int mip_height = _sg_miplevel_dim(height, mip_index); |
| 16090 | const int bytes_per_slice = _sg_surface_pitch(fmt, mip_width, mip_height, 1); |
| 16091 | const int expected_size = bytes_per_slice * num_slices; |
| 16092 | _SG_VALIDATE(expected_size == (int)data->subimage[face_index][mip_index].size, VALIDATE_IMAGEDATA_DATA_SIZE); |
| 16093 | } |
| 16094 | } |
| 16095 | #endif |
| 16096 | } |
| 16097 | |
| 16098 | _SOKOL_PRIVATE bool _sg_validate_image_desc(const sg_image_desc* desc) { |
| 16099 | #if !defined(SOKOL_DEBUG) |
| 16100 | _SOKOL_UNUSED(desc); |
| 16101 | return true; |
| 16102 | #else |
| 16103 | if (_sg.desc.disable_validation) { |
| 16104 | return true; |
| 16105 | } |
| 16106 | SOKOL_ASSERT(desc); |
| 16107 | _sg_validate_begin(); |
| 16108 | _SG_VALIDATE(desc->_start_canary == 0, VALIDATE_IMAGEDESC_CANARY); |
| 16109 | _SG_VALIDATE(desc->_end_canary == 0, VALIDATE_IMAGEDESC_CANARY); |
| 16110 | _SG_VALIDATE(desc->width > 0, VALIDATE_IMAGEDESC_WIDTH); |
| 16111 | _SG_VALIDATE(desc->height > 0, VALIDATE_IMAGEDESC_HEIGHT); |
| 16112 | const sg_pixel_format fmt = desc->pixel_format; |
| 16113 | const sg_usage usage = desc->usage; |
| 16114 | const bool injected = (0 != desc->gl_textures[0]) || |
| 16115 | (0 != desc->mtl_textures[0]) || |
| 16116 | (0 != desc->d3d11_texture) || |
| 16117 | (0 != desc->wgpu_texture); |
| 16118 | if (_sg_is_depth_or_depth_stencil_format(fmt)) { |
| 16119 | _SG_VALIDATE(desc->type != SG_IMAGETYPE_3D, VALIDATE_IMAGEDESC_DEPTH_3D_IMAGE); |
| 16120 | } |
| 16121 | if (desc->render_target) { |
| 16122 | SOKOL_ASSERT(((int)fmt >= 0) && ((int)fmt < _SG_PIXELFORMAT_NUM)); |
| 16123 | _SG_VALIDATE(_sg.formats[fmt].render, VALIDATE_IMAGEDESC_RT_PIXELFORMAT); |
| 16124 | _SG_VALIDATE(usage == SG_USAGE_IMMUTABLE, VALIDATE_IMAGEDESC_RT_IMMUTABLE); |
| 16125 | _SG_VALIDATE(desc->data.subimage[0][0].ptr==0, VALIDATE_IMAGEDESC_RT_NO_DATA); |
| 16126 | if (desc->sample_count > 1) { |
| 16127 | _SG_VALIDATE(_sg.formats[fmt].msaa, VALIDATE_IMAGEDESC_NO_MSAA_RT_SUPPORT); |
| 16128 | _SG_VALIDATE(desc->num_mipmaps == 1, VALIDATE_IMAGEDESC_MSAA_NUM_MIPMAPS); |
| 16129 | _SG_VALIDATE(desc->type != SG_IMAGETYPE_3D, VALIDATE_IMAGEDESC_MSAA_3D_IMAGE); |
| 16130 | } |
| 16131 | } else { |
| 16132 | _SG_VALIDATE(desc->sample_count == 1, VALIDATE_IMAGEDESC_MSAA_BUT_NO_RT); |
| 16133 | const bool valid_nonrt_fmt = !_sg_is_valid_rendertarget_depth_format(fmt); |
| 16134 | _SG_VALIDATE(valid_nonrt_fmt, VALIDATE_IMAGEDESC_NONRT_PIXELFORMAT); |
| 16135 | const bool is_compressed = _sg_is_compressed_pixel_format(desc->pixel_format); |
| 16136 | const bool is_immutable = (usage == SG_USAGE_IMMUTABLE); |
| 16137 | if (is_compressed) { |
| 16138 | _SG_VALIDATE(is_immutable, VALIDATE_IMAGEDESC_COMPRESSED_IMMUTABLE); |
| 16139 | } |
| 16140 | if (!injected && is_immutable) { |
| 16141 | // image desc must have valid data |
| 16142 | _sg_validate_image_data(&desc->data, |
| 16143 | desc->pixel_format, |
| 16144 | desc->width, |
| 16145 | desc->height, |
| 16146 | (desc->type == SG_IMAGETYPE_CUBE) ? 6 : 1, |
| 16147 | desc->num_mipmaps, |
| 16148 | desc->num_slices); |
| 16149 | } else { |
| 16150 | // image desc must not have data |
| 16151 | for (int face_index = 0; face_index < SG_CUBEFACE_NUM; face_index++) { |
| 16152 | for (int mip_index = 0; mip_index < SG_MAX_MIPMAPS; mip_index++) { |
| 16153 | const bool no_data = 0 == desc->data.subimage[face_index][mip_index].ptr; |
| 16154 | const bool no_size = 0 == desc->data.subimage[face_index][mip_index].size; |
| 16155 | if (injected) { |
| 16156 | _SG_VALIDATE(no_data && no_size, VALIDATE_IMAGEDESC_INJECTED_NO_DATA); |
| 16157 | } |
| 16158 | if (!is_immutable) { |
| 16159 | _SG_VALIDATE(no_data && no_size, VALIDATE_IMAGEDESC_DYNAMIC_NO_DATA); |
| 16160 | } |
| 16161 | } |
| 16162 | } |
| 16163 | } |
| 16164 | } |
| 16165 | return _sg_validate_end(); |
| 16166 | #endif |
| 16167 | } |
| 16168 | |
| 16169 | _SOKOL_PRIVATE bool _sg_validate_sampler_desc(const sg_sampler_desc* desc) { |
| 16170 | #if !defined(SOKOL_DEBUG) |
| 16171 | _SOKOL_UNUSED(desc); |
| 16172 | return true; |
| 16173 | #else |
| 16174 | if (_sg.desc.disable_validation) { |
| 16175 | return true; |
| 16176 | } |
| 16177 | SOKOL_ASSERT(desc); |
| 16178 | _sg_validate_begin(); |
| 16179 | _SG_VALIDATE(desc->_start_canary == 0, VALIDATE_SAMPLERDESC_CANARY); |
| 16180 | _SG_VALIDATE(desc->_end_canary == 0, VALIDATE_SAMPLERDESC_CANARY); |
| 16181 | _SG_VALIDATE(desc->min_filter != SG_FILTER_NONE, VALIDATE_SAMPLERDESC_MINFILTER_NONE); |
| 16182 | _SG_VALIDATE(desc->mag_filter != SG_FILTER_NONE, VALIDATE_SAMPLERDESC_MAGFILTER_NONE); |
| 16183 | // restriction from WebGPU: when anisotropy > 1, all filters must be linear |
| 16184 | if (desc->max_anisotropy > 1) { |
| 16185 | _SG_VALIDATE((desc->min_filter == SG_FILTER_LINEAR) |
| 16186 | && (desc->mag_filter == SG_FILTER_LINEAR) |
| 16187 | && (desc->mipmap_filter == SG_FILTER_LINEAR), |
| 16188 | VALIDATE_SAMPLERDESC_ANISTROPIC_REQUIRES_LINEAR_FILTERING); |
| 16189 | } |
| 16190 | return _sg_validate_end(); |
| 16191 | #endif |
| 16192 | } |
| 16193 | |
| 16194 | _SOKOL_PRIVATE bool _sg_validate_shader_desc(const sg_shader_desc* desc) { |
| 16195 | #if !defined(SOKOL_DEBUG) |
| 16196 | _SOKOL_UNUSED(desc); |
| 16197 | return true; |
| 16198 | #else |
| 16199 | if (_sg.desc.disable_validation) { |
| 16200 | return true; |
| 16201 | } |
| 16202 | SOKOL_ASSERT(desc); |
| 16203 | _sg_validate_begin(); |
| 16204 | _SG_VALIDATE(desc->_start_canary == 0, VALIDATE_SHADERDESC_CANARY); |
| 16205 | _SG_VALIDATE(desc->_end_canary == 0, VALIDATE_SHADERDESC_CANARY); |
| 16206 | #if defined(SOKOL_GLCORE) || defined(SOKOL_GLES3) || defined(SOKOL_WGPU) |
| 16207 | // on GL or WebGPU, must provide shader source code |
| 16208 | _SG_VALIDATE(0 != desc->vs.source, VALIDATE_SHADERDESC_SOURCE); |
| 16209 | _SG_VALIDATE(0 != desc->fs.source, VALIDATE_SHADERDESC_SOURCE); |
| 16210 | #elif defined(SOKOL_METAL) || defined(SOKOL_D3D11) |
| 16211 | // on Metal or D3D11, must provide shader source code or byte code |
| 16212 | _SG_VALIDATE((0 != desc->vs.source)||(0 != desc->vs.bytecode.ptr), VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE); |
| 16213 | _SG_VALIDATE((0 != desc->fs.source)||(0 != desc->fs.bytecode.ptr), VALIDATE_SHADERDESC_SOURCE_OR_BYTECODE); |
| 16214 | #else |
| 16215 | // Dummy Backend, don't require source or bytecode |
| 16216 | #endif |
| 16217 | for (int i = 0; i < SG_MAX_VERTEX_ATTRIBUTES; i++) { |
| 16218 | if (desc->attrs[i].name) { |
| 16219 | _SG_VALIDATE(strlen(desc->attrs[i].name) < _SG_STRING_SIZE, VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG); |
| 16220 | } |
| 16221 | if (desc->attrs[i].sem_name) { |
| 16222 | _SG_VALIDATE(strlen(desc->attrs[i].sem_name) < _SG_STRING_SIZE, VALIDATE_SHADERDESC_ATTR_STRING_TOO_LONG); |
| 16223 | } |
| 16224 | } |
| 16225 | // if shader byte code, the size must also be provided |
| 16226 | if (0 != desc->vs.bytecode.ptr) { |
| 16227 | _SG_VALIDATE(desc->vs.bytecode.size > 0, VALIDATE_SHADERDESC_NO_BYTECODE_SIZE); |
| 16228 | } |
| 16229 | if (0 != desc->fs.bytecode.ptr) { |
| 16230 | _SG_VALIDATE(desc->fs.bytecode.size > 0, VALIDATE_SHADERDESC_NO_BYTECODE_SIZE); |
| 16231 | } |
| 16232 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 16233 | const sg_shader_stage_desc* stage_desc = (stage_index == 0)? &desc->vs : &desc->fs; |
| 16234 | bool uniform_blocks_continuous = true; |
| 16235 | for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) { |
| 16236 | const sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index]; |
| 16237 | if (ub_desc->size > 0) { |
| 16238 | _SG_VALIDATE(uniform_blocks_continuous, VALIDATE_SHADERDESC_NO_CONT_UBS); |
| 16239 | #if defined(_SOKOL_ANY_GL) |
| 16240 | bool uniforms_continuous = true; |
| 16241 | uint32_t uniform_offset = 0; |
| 16242 | int num_uniforms = 0; |
| 16243 | for (int u_index = 0; u_index < SG_MAX_UB_MEMBERS; u_index++) { |
| 16244 | const sg_shader_uniform_desc* u_desc = &ub_desc->uniforms[u_index]; |
| 16245 | if (u_desc->type != SG_UNIFORMTYPE_INVALID) { |
| 16246 | _SG_VALIDATE(uniforms_continuous, VALIDATE_SHADERDESC_NO_CONT_UB_MEMBERS); |
| 16247 | #if defined(SOKOL_GLES3) |
| 16248 | _SG_VALIDATE(0 != u_desc->name, VALIDATE_SHADERDESC_UB_MEMBER_NAME); |
| 16249 | #endif |
| 16250 | const int array_count = u_desc->array_count; |
| 16251 | _SG_VALIDATE(array_count > 0, VALIDATE_SHADERDESC_UB_ARRAY_COUNT); |
| 16252 | const uint32_t u_align = _sg_uniform_alignment(u_desc->type, array_count, ub_desc->layout); |
| 16253 | const uint32_t u_size = _sg_uniform_size(u_desc->type, array_count, ub_desc->layout); |
| 16254 | uniform_offset = _sg_align_u32(uniform_offset, u_align); |
| 16255 | uniform_offset += u_size; |
| 16256 | num_uniforms++; |
| 16257 | // with std140, arrays are only allowed for FLOAT4, INT4, MAT4 |
| 16258 | if (ub_desc->layout == SG_UNIFORMLAYOUT_STD140) { |
| 16259 | if (array_count > 1) { |
| 16260 | _SG_VALIDATE((u_desc->type == SG_UNIFORMTYPE_FLOAT4) || (u_desc->type == SG_UNIFORMTYPE_INT4) || (u_desc->type == SG_UNIFORMTYPE_MAT4), VALIDATE_SHADERDESC_UB_STD140_ARRAY_TYPE); |
| 16261 | } |
| 16262 | } |
| 16263 | } else { |
| 16264 | uniforms_continuous = false; |
| 16265 | } |
| 16266 | } |
| 16267 | if (ub_desc->layout == SG_UNIFORMLAYOUT_STD140) { |
| 16268 | uniform_offset = _sg_align_u32(uniform_offset, 16); |
| 16269 | } |
| 16270 | _SG_VALIDATE((size_t)uniform_offset == ub_desc->size, VALIDATE_SHADERDESC_UB_SIZE_MISMATCH); |
| 16271 | _SG_VALIDATE(num_uniforms > 0, VALIDATE_SHADERDESC_NO_UB_MEMBERS); |
| 16272 | #endif |
| 16273 | } else { |
| 16274 | uniform_blocks_continuous = false; |
| 16275 | } |
| 16276 | } |
| 16277 | bool storage_buffers_continuous = true; |
| 16278 | for (int sbuf_index = 0; sbuf_index < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; sbuf_index++) { |
| 16279 | const sg_shader_storage_buffer_desc* sbuf_desc = &stage_desc->storage_buffers[sbuf_index]; |
| 16280 | if (sbuf_desc->used) { |
| 16281 | _SG_VALIDATE(storage_buffers_continuous, VALIDATE_SHADERDESC_NO_CONT_STORAGEBUFFERS); |
| 16282 | _SG_VALIDATE(sbuf_desc->readonly, VALIDATE_SHADERDESC_STORAGEBUFFER_READONLY); |
| 16283 | } else { |
| 16284 | storage_buffers_continuous = false; |
| 16285 | } |
| 16286 | } |
| 16287 | bool images_continuous = true; |
| 16288 | int num_images = 0; |
| 16289 | for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) { |
| 16290 | const sg_shader_image_desc* img_desc = &stage_desc->images[img_index]; |
| 16291 | if (img_desc->used) { |
| 16292 | _SG_VALIDATE(images_continuous, VALIDATE_SHADERDESC_NO_CONT_IMAGES); |
| 16293 | num_images++; |
| 16294 | } else { |
| 16295 | images_continuous = false; |
| 16296 | } |
| 16297 | } |
| 16298 | bool samplers_continuous = true; |
| 16299 | int num_samplers = 0; |
| 16300 | for (int smp_index = 0; smp_index < SG_MAX_SHADERSTAGE_SAMPLERS; smp_index++) { |
| 16301 | const sg_shader_sampler_desc* smp_desc = &stage_desc->samplers[smp_index]; |
| 16302 | if (smp_desc->used) { |
| 16303 | _SG_VALIDATE(samplers_continuous, VALIDATE_SHADERDESC_NO_CONT_SAMPLERS); |
| 16304 | num_samplers++; |
| 16305 | } else { |
| 16306 | samplers_continuous = false; |
| 16307 | } |
| 16308 | } |
| 16309 | bool image_samplers_continuous = true; |
| 16310 | int num_image_samplers = 0; |
| 16311 | for (int img_smp_index = 0; img_smp_index < SG_MAX_SHADERSTAGE_IMAGESAMPLERPAIRS; img_smp_index++) { |
| 16312 | const sg_shader_image_sampler_pair_desc* img_smp_desc = &stage_desc->image_sampler_pairs[img_smp_index]; |
| 16313 | if (img_smp_desc->used) { |
| 16314 | _SG_VALIDATE(image_samplers_continuous, VALIDATE_SHADERDESC_NO_CONT_IMAGE_SAMPLER_PAIRS); |
| 16315 | num_image_samplers++; |
| 16316 | const bool img_slot_in_range = (img_smp_desc->image_slot >= 0) && (img_smp_desc->image_slot < SG_MAX_SHADERSTAGE_IMAGES); |
| 16317 | const bool smp_slot_in_range = (img_smp_desc->sampler_slot >= 0) && (img_smp_desc->sampler_slot < SG_MAX_SHADERSTAGE_SAMPLERS); |
| 16318 | _SG_VALIDATE(img_slot_in_range && (img_smp_desc->image_slot < num_images), VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_IMAGE_SLOT_OUT_OF_RANGE); |
| 16319 | _SG_VALIDATE(smp_slot_in_range && (img_smp_desc->sampler_slot < num_samplers), VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_IMAGE_SLOT_OUT_OF_RANGE); |
| 16320 | #if defined(_SOKOL_ANY_GL) |
| 16321 | _SG_VALIDATE(img_smp_desc->glsl_name != 0, VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_NAME_REQUIRED_FOR_GL); |
| 16322 | #endif |
| 16323 | if (img_slot_in_range && smp_slot_in_range) { |
| 16324 | const sg_shader_image_desc* img_desc = &stage_desc->images[img_smp_desc->image_slot]; |
| 16325 | const sg_shader_sampler_desc* smp_desc = &stage_desc->samplers[img_smp_desc->sampler_slot]; |
| 16326 | const bool needs_nonfiltering = (img_desc->sample_type == SG_IMAGESAMPLETYPE_UINT) |
| 16327 | || (img_desc->sample_type == SG_IMAGESAMPLETYPE_SINT) |
| 16328 | || (img_desc->sample_type == SG_IMAGESAMPLETYPE_UNFILTERABLE_FLOAT); |
| 16329 | const bool needs_comparison = img_desc->sample_type == SG_IMAGESAMPLETYPE_DEPTH; |
| 16330 | if (needs_nonfiltering) { |
| 16331 | _SG_VALIDATE(needs_nonfiltering && (smp_desc->sampler_type == SG_SAMPLERTYPE_NONFILTERING), VALIDATE_SHADERDESC_NONFILTERING_SAMPLER_REQUIRED); |
| 16332 | } |
| 16333 | if (needs_comparison) { |
| 16334 | _SG_VALIDATE(needs_comparison && (smp_desc->sampler_type == SG_SAMPLERTYPE_COMPARISON), VALIDATE_SHADERDESC_COMPARISON_SAMPLER_REQUIRED); |
| 16335 | } |
| 16336 | } |
| 16337 | } else { |
| 16338 | _SG_VALIDATE(img_smp_desc->glsl_name == 0, VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_HAS_NAME_BUT_NOT_USED); |
| 16339 | _SG_VALIDATE(img_smp_desc->image_slot == 0, VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_HAS_IMAGE_BUT_NOT_USED); |
| 16340 | _SG_VALIDATE(img_smp_desc->sampler_slot == 0, VALIDATE_SHADERDESC_IMAGE_SAMPLER_PAIR_HAS_SAMPLER_BUT_NOT_USED); |
| 16341 | image_samplers_continuous = false; |
| 16342 | } |
| 16343 | } |
| 16344 | // each image and sampler must be referenced by an image sampler |
| 16345 | const uint32_t expected_img_slot_mask = (uint32_t)((1 << num_images) - 1); |
| 16346 | const uint32_t expected_smp_slot_mask = (uint32_t)((1 << num_samplers) - 1); |
| 16347 | uint32_t actual_img_slot_mask = 0; |
| 16348 | uint32_t actual_smp_slot_mask = 0; |
| 16349 | for (int img_smp_index = 0; img_smp_index < num_image_samplers; img_smp_index++) { |
| 16350 | const sg_shader_image_sampler_pair_desc* img_smp_desc = &stage_desc->image_sampler_pairs[img_smp_index]; |
| 16351 | actual_img_slot_mask |= (1 << ((uint32_t)img_smp_desc->image_slot & 31)); |
| 16352 | actual_smp_slot_mask |= (1 << ((uint32_t)img_smp_desc->sampler_slot & 31)); |
| 16353 | } |
| 16354 | _SG_VALIDATE(expected_img_slot_mask == actual_img_slot_mask, VALIDATE_SHADERDESC_IMAGE_NOT_REFERENCED_BY_IMAGE_SAMPLER_PAIRS); |
| 16355 | _SG_VALIDATE(expected_smp_slot_mask == actual_smp_slot_mask, VALIDATE_SHADERDESC_SAMPLER_NOT_REFERENCED_BY_IMAGE_SAMPLER_PAIRS); |
| 16356 | } |
| 16357 | return _sg_validate_end(); |
| 16358 | #endif |
| 16359 | } |
| 16360 | |
| 16361 | _SOKOL_PRIVATE bool _sg_validate_pipeline_desc(const sg_pipeline_desc* desc) { |
| 16362 | #if !defined(SOKOL_DEBUG) |
| 16363 | _SOKOL_UNUSED(desc); |
| 16364 | return true; |
| 16365 | #else |
| 16366 | if (_sg.desc.disable_validation) { |
| 16367 | return true; |
| 16368 | } |
| 16369 | SOKOL_ASSERT(desc); |
| 16370 | _sg_validate_begin(); |
| 16371 | _SG_VALIDATE(desc->_start_canary == 0, VALIDATE_PIPELINEDESC_CANARY); |
| 16372 | _SG_VALIDATE(desc->_end_canary == 0, VALIDATE_PIPELINEDESC_CANARY); |
| 16373 | _SG_VALIDATE(desc->shader.id != SG_INVALID_ID, VALIDATE_PIPELINEDESC_SHADER); |
| 16374 | for (int buf_index = 0; buf_index < SG_MAX_VERTEX_BUFFERS; buf_index++) { |
| 16375 | const sg_vertex_buffer_layout_state* l_state = &desc->layout.buffers[buf_index]; |
| 16376 | if (l_state->stride == 0) { |
| 16377 | continue; |
| 16378 | } |
| 16379 | _SG_VALIDATE(_sg_multiple_u64((uint64_t)l_state->stride, 4), VALIDATE_PIPELINEDESC_LAYOUT_STRIDE4); |
| 16380 | } |
| 16381 | const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, desc->shader.id); |
| 16382 | _SG_VALIDATE(0 != shd, VALIDATE_PIPELINEDESC_SHADER); |
| 16383 | if (shd) { |
| 16384 | _SG_VALIDATE(shd->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_PIPELINEDESC_SHADER); |
| 16385 | bool attrs_cont = true; |
| 16386 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 16387 | const sg_vertex_attr_state* a_state = &desc->layout.attrs[attr_index]; |
| 16388 | if (a_state->format == SG_VERTEXFORMAT_INVALID) { |
| 16389 | attrs_cont = false; |
| 16390 | continue; |
| 16391 | } |
| 16392 | _SG_VALIDATE(attrs_cont, VALIDATE_PIPELINEDESC_NO_CONT_ATTRS); |
| 16393 | SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS); |
| 16394 | #if defined(SOKOL_D3D11) |
| 16395 | // on D3D11, semantic names (and semantic indices) must be provided |
| 16396 | _SG_VALIDATE(!_sg_strempty(&shd->d3d11.attrs[attr_index].sem_name), VALIDATE_PIPELINEDESC_ATTR_SEMANTICS); |
| 16397 | #endif |
| 16398 | } |
| 16399 | } |
| 16400 | return _sg_validate_end(); |
| 16401 | #endif |
| 16402 | } |
| 16403 | |
| 16404 | _SOKOL_PRIVATE bool _sg_validate_attachments_desc(const sg_attachments_desc* desc) { |
| 16405 | #if !defined(SOKOL_DEBUG) |
| 16406 | _SOKOL_UNUSED(desc); |
| 16407 | return true; |
| 16408 | #else |
| 16409 | if (_sg.desc.disable_validation) { |
| 16410 | return true; |
| 16411 | } |
| 16412 | SOKOL_ASSERT(desc); |
| 16413 | _sg_validate_begin(); |
| 16414 | _SG_VALIDATE(desc->_start_canary == 0, VALIDATE_ATTACHMENTSDESC_CANARY); |
| 16415 | _SG_VALIDATE(desc->_end_canary == 0, VALIDATE_ATTACHMENTSDESC_CANARY); |
| 16416 | bool atts_cont = true; |
| 16417 | int color_width = -1, color_height = -1, color_sample_count = -1; |
| 16418 | bool has_color_atts = false; |
| 16419 | for (int att_index = 0; att_index < SG_MAX_COLOR_ATTACHMENTS; att_index++) { |
| 16420 | const sg_attachment_desc* att = &desc->colors[att_index]; |
| 16421 | if (att->image.id == SG_INVALID_ID) { |
| 16422 | atts_cont = false; |
| 16423 | continue; |
| 16424 | } |
| 16425 | _SG_VALIDATE(atts_cont, VALIDATE_ATTACHMENTSDESC_NO_CONT_COLOR_ATTS); |
| 16426 | has_color_atts = true; |
| 16427 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, att->image.id); |
| 16428 | _SG_VALIDATE(img, VALIDATE_ATTACHMENTSDESC_IMAGE); |
| 16429 | if (0 != img) { |
| 16430 | _SG_VALIDATE(img->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_ATTACHMENTSDESC_IMAGE); |
| 16431 | _SG_VALIDATE(img->cmn.render_target, VALIDATE_ATTACHMENTSDESC_IMAGE_NO_RT); |
| 16432 | _SG_VALIDATE(att->mip_level < img->cmn.num_mipmaps, VALIDATE_ATTACHMENTSDESC_MIPLEVEL); |
| 16433 | if (img->cmn.type == SG_IMAGETYPE_CUBE) { |
| 16434 | _SG_VALIDATE(att->slice < 6, VALIDATE_ATTACHMENTSDESC_FACE); |
| 16435 | } else if (img->cmn.type == SG_IMAGETYPE_ARRAY) { |
| 16436 | _SG_VALIDATE(att->slice < img->cmn.num_slices, VALIDATE_ATTACHMENTSDESC_LAYER); |
| 16437 | } else if (img->cmn.type == SG_IMAGETYPE_3D) { |
| 16438 | _SG_VALIDATE(att->slice < img->cmn.num_slices, VALIDATE_ATTACHMENTSDESC_SLICE); |
| 16439 | } |
| 16440 | if (att_index == 0) { |
| 16441 | color_width = _sg_miplevel_dim(img->cmn.width, att->mip_level); |
| 16442 | color_height = _sg_miplevel_dim(img->cmn.height, att->mip_level); |
| 16443 | color_sample_count = img->cmn.sample_count; |
| 16444 | } else { |
| 16445 | _SG_VALIDATE(color_width == _sg_miplevel_dim(img->cmn.width, att->mip_level), VALIDATE_ATTACHMENTSDESC_IMAGE_SIZES); |
| 16446 | _SG_VALIDATE(color_height == _sg_miplevel_dim(img->cmn.height, att->mip_level), VALIDATE_ATTACHMENTSDESC_IMAGE_SIZES); |
| 16447 | _SG_VALIDATE(color_sample_count == img->cmn.sample_count, VALIDATE_ATTACHMENTSDESC_IMAGE_SAMPLE_COUNTS); |
| 16448 | } |
| 16449 | _SG_VALIDATE(_sg_is_valid_rendertarget_color_format(img->cmn.pixel_format), VALIDATE_ATTACHMENTSDESC_COLOR_INV_PIXELFORMAT); |
| 16450 | |
| 16451 | // check resolve attachment |
| 16452 | const sg_attachment_desc* res_att = &desc->resolves[att_index]; |
| 16453 | if (res_att->image.id != SG_INVALID_ID) { |
| 16454 | // associated color attachment must be MSAA |
| 16455 | _SG_VALIDATE(img->cmn.sample_count > 1, VALIDATE_ATTACHMENTSDESC_RESOLVE_COLOR_IMAGE_MSAA); |
| 16456 | const _sg_image_t* res_img = _sg_lookup_image(&_sg.pools, res_att->image.id); |
| 16457 | _SG_VALIDATE(res_img, VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE); |
| 16458 | if (res_img != 0) { |
| 16459 | _SG_VALIDATE(res_img->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE); |
| 16460 | _SG_VALIDATE(res_img->cmn.render_target, VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_NO_RT); |
| 16461 | _SG_VALIDATE(res_img->cmn.sample_count == 1, VALIDATE_ATTACHMENTSDESC_RESOLVE_SAMPLE_COUNT); |
| 16462 | _SG_VALIDATE(res_att->mip_level < res_img->cmn.num_mipmaps, VALIDATE_ATTACHMENTSDESC_RESOLVE_MIPLEVEL); |
| 16463 | if (res_img->cmn.type == SG_IMAGETYPE_CUBE) { |
| 16464 | _SG_VALIDATE(res_att->slice < 6, VALIDATE_ATTACHMENTSDESC_RESOLVE_FACE); |
| 16465 | } else if (res_img->cmn.type == SG_IMAGETYPE_ARRAY) { |
| 16466 | _SG_VALIDATE(res_att->slice < res_img->cmn.num_slices, VALIDATE_ATTACHMENTSDESC_RESOLVE_LAYER); |
| 16467 | } else if (res_img->cmn.type == SG_IMAGETYPE_3D) { |
| 16468 | _SG_VALIDATE(res_att->slice < res_img->cmn.num_slices, VALIDATE_ATTACHMENTSDESC_RESOLVE_SLICE); |
| 16469 | } |
| 16470 | _SG_VALIDATE(img->cmn.pixel_format == res_img->cmn.pixel_format, VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_FORMAT); |
| 16471 | _SG_VALIDATE(color_width == _sg_miplevel_dim(res_img->cmn.width, res_att->mip_level), VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_SIZES); |
| 16472 | _SG_VALIDATE(color_height == _sg_miplevel_dim(res_img->cmn.height, res_att->mip_level), VALIDATE_ATTACHMENTSDESC_RESOLVE_IMAGE_SIZES); |
| 16473 | } |
| 16474 | } |
| 16475 | } |
| 16476 | } |
| 16477 | bool has_depth_stencil_att = false; |
| 16478 | if (desc->depth_stencil.image.id != SG_INVALID_ID) { |
| 16479 | const sg_attachment_desc* att = &desc->depth_stencil; |
| 16480 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, att->image.id); |
| 16481 | _SG_VALIDATE(img, VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE); |
| 16482 | has_depth_stencil_att = true; |
| 16483 | if (img) { |
| 16484 | _SG_VALIDATE(img->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE); |
| 16485 | _SG_VALIDATE(att->mip_level < img->cmn.num_mipmaps, VALIDATE_ATTACHMENTSDESC_DEPTH_MIPLEVEL); |
| 16486 | if (img->cmn.type == SG_IMAGETYPE_CUBE) { |
| 16487 | _SG_VALIDATE(att->slice < 6, VALIDATE_ATTACHMENTSDESC_DEPTH_FACE); |
| 16488 | } else if (img->cmn.type == SG_IMAGETYPE_ARRAY) { |
| 16489 | _SG_VALIDATE(att->slice < img->cmn.num_slices, VALIDATE_ATTACHMENTSDESC_DEPTH_LAYER); |
| 16490 | } else if (img->cmn.type == SG_IMAGETYPE_3D) { |
| 16491 | // NOTE: this can't actually happen because of VALIDATE_IMAGEDESC_DEPTH_3D_IMAGE |
| 16492 | _SG_VALIDATE(att->slice < img->cmn.num_slices, VALIDATE_ATTACHMENTSDESC_DEPTH_SLICE); |
| 16493 | } |
| 16494 | _SG_VALIDATE(img->cmn.render_target, VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_NO_RT); |
| 16495 | _SG_VALIDATE((color_width == -1) || (color_width == _sg_miplevel_dim(img->cmn.width, att->mip_level)), VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_SIZES); |
| 16496 | _SG_VALIDATE((color_height == -1) || (color_height == _sg_miplevel_dim(img->cmn.height, att->mip_level)), VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_SIZES); |
| 16497 | _SG_VALIDATE((color_sample_count == -1) || (color_sample_count == img->cmn.sample_count), VALIDATE_ATTACHMENTSDESC_DEPTH_IMAGE_SAMPLE_COUNT); |
| 16498 | _SG_VALIDATE(_sg_is_valid_rendertarget_depth_format(img->cmn.pixel_format), VALIDATE_ATTACHMENTSDESC_DEPTH_INV_PIXELFORMAT); |
| 16499 | } |
| 16500 | } |
| 16501 | _SG_VALIDATE(has_color_atts || has_depth_stencil_att, VALIDATE_ATTACHMENTSDESC_NO_ATTACHMENTS); |
| 16502 | return _sg_validate_end(); |
| 16503 | #endif |
| 16504 | } |
| 16505 | |
| 16506 | _SOKOL_PRIVATE bool _sg_validate_begin_pass(const sg_pass* pass) { |
| 16507 | #if !defined(SOKOL_DEBUG) |
| 16508 | _SOKOL_UNUSED(pass); |
| 16509 | return true; |
| 16510 | #else |
| 16511 | if (_sg.desc.disable_validation) { |
| 16512 | return true; |
| 16513 | } |
| 16514 | _sg_validate_begin(); |
| 16515 | _SG_VALIDATE(pass->_start_canary == 0, VALIDATE_BEGINPASS_CANARY); |
| 16516 | _SG_VALIDATE(pass->_end_canary == 0, VALIDATE_BEGINPASS_CANARY); |
| 16517 | if (pass->attachments.id == SG_INVALID_ID) { |
| 16518 | // this is a swapchain pass |
| 16519 | _SG_VALIDATE(pass->swapchain.width > 0, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_WIDTH); |
| 16520 | _SG_VALIDATE(pass->swapchain.height > 0, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_HEIGHT); |
| 16521 | _SG_VALIDATE(pass->swapchain.sample_count > 0, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_SAMPLECOUNT); |
| 16522 | _SG_VALIDATE(pass->swapchain.color_format > SG_PIXELFORMAT_NONE, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_COLORFORMAT); |
| 16523 | // NOTE: depth buffer is optional, so depth_format is allowed to be invalid |
| 16524 | // NOTE: the GL framebuffer handle may actually be 0 |
| 16525 | #if defined(SOKOL_METAL) |
| 16526 | _SG_VALIDATE(pass->swapchain.metal.current_drawable != 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_CURRENTDRAWABLE); |
| 16527 | if (pass->swapchain.depth_format == SG_PIXELFORMAT_NONE) { |
| 16528 | _SG_VALIDATE(pass->swapchain.metal.depth_stencil_texture == 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_DEPTHSTENCILTEXTURE_NOTSET); |
| 16529 | } else { |
| 16530 | _SG_VALIDATE(pass->swapchain.metal.depth_stencil_texture != 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_DEPTHSTENCILTEXTURE); |
| 16531 | } |
| 16532 | if (pass->swapchain.sample_count > 1) { |
| 16533 | _SG_VALIDATE(pass->swapchain.metal.msaa_color_texture != 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_MSAACOLORTEXTURE); |
| 16534 | } else { |
| 16535 | _SG_VALIDATE(pass->swapchain.metal.msaa_color_texture == 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_MSAACOLORTEXTURE_NOTSET); |
| 16536 | } |
| 16537 | #elif defined(SOKOL_D3D11) |
| 16538 | _SG_VALIDATE(pass->swapchain.d3d11.render_view != 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RENDERVIEW); |
| 16539 | if (pass->swapchain.depth_format == SG_PIXELFORMAT_NONE) { |
| 16540 | _SG_VALIDATE(pass->swapchain.d3d11.depth_stencil_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_DEPTHSTENCILVIEW_NOTSET); |
| 16541 | } else { |
| 16542 | _SG_VALIDATE(pass->swapchain.d3d11.depth_stencil_view != 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_DEPTHSTENCILVIEW); |
| 16543 | } |
| 16544 | if (pass->swapchain.sample_count > 1) { |
| 16545 | _SG_VALIDATE(pass->swapchain.d3d11.resolve_view != 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RESOLVEVIEW); |
| 16546 | } else { |
| 16547 | _SG_VALIDATE(pass->swapchain.d3d11.resolve_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RESOLVEVIEW_NOTSET); |
| 16548 | } |
| 16549 | #elif defined(SOKOL_WGPU) |
| 16550 | _SG_VALIDATE(pass->swapchain.wgpu.render_view != 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RENDERVIEW); |
| 16551 | if (pass->swapchain.depth_format == SG_PIXELFORMAT_NONE) { |
| 16552 | _SG_VALIDATE(pass->swapchain.wgpu.depth_stencil_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_DEPTHSTENCILVIEW_NOTSET); |
| 16553 | } else { |
| 16554 | _SG_VALIDATE(pass->swapchain.wgpu.depth_stencil_view != 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_DEPTHSTENCILVIEW); |
| 16555 | } |
| 16556 | if (pass->swapchain.sample_count > 1) { |
| 16557 | _SG_VALIDATE(pass->swapchain.wgpu.resolve_view != 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RESOLVEVIEW); |
| 16558 | } else { |
| 16559 | _SG_VALIDATE(pass->swapchain.wgpu.resolve_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RESOLVEVIEW_NOTSET); |
| 16560 | } |
| 16561 | #endif |
| 16562 | } else { |
| 16563 | // this is an 'offscreen pass' |
| 16564 | const _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, pass->attachments.id); |
| 16565 | if (atts) { |
| 16566 | _SG_VALIDATE(atts->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_BEGINPASS_ATTACHMENTS_VALID); |
| 16567 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 16568 | const _sg_attachment_common_t* color_att = &atts->cmn.colors[i]; |
| 16569 | const _sg_image_t* color_img = _sg_attachments_color_image(atts, i); |
| 16570 | if (color_img) { |
| 16571 | _SG_VALIDATE(color_img->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_BEGINPASS_COLOR_ATTACHMENT_IMAGE); |
| 16572 | _SG_VALIDATE(color_img->slot.id == color_att->image_id.id, VALIDATE_BEGINPASS_COLOR_ATTACHMENT_IMAGE); |
| 16573 | } |
| 16574 | const _sg_attachment_common_t* resolve_att = &atts->cmn.resolves[i]; |
| 16575 | const _sg_image_t* resolve_img = _sg_attachments_resolve_image(atts, i); |
| 16576 | if (resolve_img) { |
| 16577 | _SG_VALIDATE(resolve_img->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_BEGINPASS_RESOLVE_ATTACHMENT_IMAGE); |
| 16578 | _SG_VALIDATE(resolve_img->slot.id == resolve_att->image_id.id, VALIDATE_BEGINPASS_RESOLVE_ATTACHMENT_IMAGE); |
| 16579 | } |
| 16580 | } |
| 16581 | const _sg_image_t* ds_img = _sg_attachments_ds_image(atts); |
| 16582 | if (ds_img) { |
| 16583 | const _sg_attachment_common_t* att = &atts->cmn.depth_stencil; |
| 16584 | _SG_VALIDATE(ds_img->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_BEGINPASS_DEPTHSTENCIL_ATTACHMENT_IMAGE); |
| 16585 | _SG_VALIDATE(ds_img->slot.id == att->image_id.id, VALIDATE_BEGINPASS_DEPTHSTENCIL_ATTACHMENT_IMAGE); |
| 16586 | } |
| 16587 | } else { |
| 16588 | _SG_VALIDATE(atts != 0, VALIDATE_BEGINPASS_ATTACHMENTS_EXISTS); |
| 16589 | } |
| 16590 | // swapchain params must be all zero! |
| 16591 | _SG_VALIDATE(pass->swapchain.width == 0, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_WIDTH_NOTSET); |
| 16592 | _SG_VALIDATE(pass->swapchain.height == 0, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_HEIGHT_NOTSET); |
| 16593 | _SG_VALIDATE(pass->swapchain.sample_count == 0, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_SAMPLECOUNT_NOTSET); |
| 16594 | _SG_VALIDATE(pass->swapchain.color_format == _SG_PIXELFORMAT_DEFAULT, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_COLORFORMAT_NOTSET); |
| 16595 | _SG_VALIDATE(pass->swapchain.depth_format == _SG_PIXELFORMAT_DEFAULT, VALIDATE_BEGINPASS_SWAPCHAIN_EXPECT_DEPTHFORMAT_NOTSET); |
| 16596 | #if defined(SOKOL_METAL) |
| 16597 | _SG_VALIDATE(pass->swapchain.metal.current_drawable == 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_CURRENTDRAWABLE_NOTSET); |
| 16598 | _SG_VALIDATE(pass->swapchain.metal.depth_stencil_texture == 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_DEPTHSTENCILTEXTURE_NOTSET); |
| 16599 | _SG_VALIDATE(pass->swapchain.metal.msaa_color_texture == 0, VALIDATE_BEGINPASS_SWAPCHAIN_METAL_EXPECT_MSAACOLORTEXTURE_NOTSET); |
| 16600 | #elif defined(SOKOL_D3D11) |
| 16601 | _SG_VALIDATE(pass->swapchain.d3d11.render_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RENDERVIEW_NOTSET); |
| 16602 | _SG_VALIDATE(pass->swapchain.d3d11.depth_stencil_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_DEPTHSTENCILVIEW_NOTSET); |
| 16603 | _SG_VALIDATE(pass->swapchain.d3d11.resolve_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_D3D11_EXPECT_RESOLVEVIEW_NOTSET); |
| 16604 | #elif defined(SOKOL_WGPU) |
| 16605 | _SG_VALIDATE(pass->swapchain.wgpu.render_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RENDERVIEW_NOTSET); |
| 16606 | _SG_VALIDATE(pass->swapchain.wgpu.depth_stencil_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_DEPTHSTENCILVIEW_NOTSET); |
| 16607 | _SG_VALIDATE(pass->swapchain.wgpu.resolve_view == 0, VALIDATE_BEGINPASS_SWAPCHAIN_WGPU_EXPECT_RESOLVEVIEW_NOTSET); |
| 16608 | #elif defined(_SOKOL_ANY_GL) |
| 16609 | _SG_VALIDATE(pass->swapchain.gl.framebuffer == 0, VALIDATE_BEGINPASS_SWAPCHAIN_GL_EXPECT_FRAMEBUFFER_NOTSET); |
| 16610 | #endif |
| 16611 | } |
| 16612 | return _sg_validate_end(); |
| 16613 | #endif |
| 16614 | } |
| 16615 | |
| 16616 | _SOKOL_PRIVATE bool _sg_validate_apply_pipeline(sg_pipeline pip_id) { |
| 16617 | #if !defined(SOKOL_DEBUG) |
| 16618 | _SOKOL_UNUSED(pip_id); |
| 16619 | return true; |
| 16620 | #else |
| 16621 | if (_sg.desc.disable_validation) { |
| 16622 | return true; |
| 16623 | } |
| 16624 | _sg_validate_begin(); |
| 16625 | // the pipeline object must be alive and valid |
| 16626 | _SG_VALIDATE(pip_id.id != SG_INVALID_ID, VALIDATE_APIP_PIPELINE_VALID_ID); |
| 16627 | const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 16628 | _SG_VALIDATE(pip != 0, VALIDATE_APIP_PIPELINE_EXISTS); |
| 16629 | if (!pip) { |
| 16630 | return _sg_validate_end(); |
| 16631 | } |
| 16632 | _SG_VALIDATE(pip->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_APIP_PIPELINE_VALID); |
| 16633 | // the pipeline's shader must be alive and valid |
| 16634 | SOKOL_ASSERT(pip->shader); |
| 16635 | _SG_VALIDATE(pip->shader->slot.id == pip->cmn.shader_id.id, VALIDATE_APIP_SHADER_EXISTS); |
| 16636 | _SG_VALIDATE(pip->shader->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_APIP_SHADER_VALID); |
| 16637 | // check that pipeline attributes match current pass attributes |
| 16638 | if (_sg.cur_pass.atts_id.id != SG_INVALID_ID) { |
| 16639 | // an offscreen pass |
| 16640 | const _sg_attachments_t* atts = _sg.cur_pass.atts; |
| 16641 | SOKOL_ASSERT(atts); |
| 16642 | _SG_VALIDATE(atts->slot.id == _sg.cur_pass.atts_id.id, VALIDATE_APIP_CURPASS_ATTACHMENTS_EXISTS); |
| 16643 | _SG_VALIDATE(atts->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_APIP_CURPASS_ATTACHMENTS_VALID); |
| 16644 | |
| 16645 | _SG_VALIDATE(pip->cmn.color_count == atts->cmn.num_colors, VALIDATE_APIP_ATT_COUNT); |
| 16646 | for (int i = 0; i < pip->cmn.color_count; i++) { |
| 16647 | const _sg_image_t* att_img = _sg_attachments_color_image(atts, i); |
| 16648 | _SG_VALIDATE(pip->cmn.colors[i].pixel_format == att_img->cmn.pixel_format, VALIDATE_APIP_COLOR_FORMAT); |
| 16649 | _SG_VALIDATE(pip->cmn.sample_count == att_img->cmn.sample_count, VALIDATE_APIP_SAMPLE_COUNT); |
| 16650 | } |
| 16651 | const _sg_image_t* att_dsimg = _sg_attachments_ds_image(atts); |
| 16652 | if (att_dsimg) { |
| 16653 | _SG_VALIDATE(pip->cmn.depth.pixel_format == att_dsimg->cmn.pixel_format, VALIDATE_APIP_DEPTH_FORMAT); |
| 16654 | } else { |
| 16655 | _SG_VALIDATE(pip->cmn.depth.pixel_format == SG_PIXELFORMAT_NONE, VALIDATE_APIP_DEPTH_FORMAT); |
| 16656 | } |
| 16657 | } else { |
| 16658 | // default pass |
| 16659 | _SG_VALIDATE(pip->cmn.color_count == 1, VALIDATE_APIP_ATT_COUNT); |
| 16660 | _SG_VALIDATE(pip->cmn.colors[0].pixel_format == _sg.cur_pass.swapchain.color_fmt, VALIDATE_APIP_COLOR_FORMAT); |
| 16661 | _SG_VALIDATE(pip->cmn.depth.pixel_format == _sg.cur_pass.swapchain.depth_fmt, VALIDATE_APIP_DEPTH_FORMAT); |
| 16662 | _SG_VALIDATE(pip->cmn.sample_count == _sg.cur_pass.swapchain.sample_count, VALIDATE_APIP_SAMPLE_COUNT); |
| 16663 | } |
| 16664 | return _sg_validate_end(); |
| 16665 | #endif |
| 16666 | } |
| 16667 | |
| 16668 | _SOKOL_PRIVATE bool _sg_validate_apply_bindings(const sg_bindings* bindings) { |
| 16669 | #if !defined(SOKOL_DEBUG) |
| 16670 | _SOKOL_UNUSED(bindings); |
| 16671 | return true; |
| 16672 | #else |
| 16673 | if (_sg.desc.disable_validation) { |
| 16674 | return true; |
| 16675 | } |
| 16676 | _sg_validate_begin(); |
| 16677 | |
| 16678 | // a pipeline object must have been applied |
| 16679 | _SG_VALIDATE(_sg.cur_pipeline.id != SG_INVALID_ID, VALIDATE_ABND_PIPELINE); |
| 16680 | const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, _sg.cur_pipeline.id); |
| 16681 | _SG_VALIDATE(pip != 0, VALIDATE_ABND_PIPELINE_EXISTS); |
| 16682 | if (!pip) { |
| 16683 | return _sg_validate_end(); |
| 16684 | } |
| 16685 | _SG_VALIDATE(pip->slot.state == SG_RESOURCESTATE_VALID, VALIDATE_ABND_PIPELINE_VALID); |
| 16686 | SOKOL_ASSERT(pip->shader && (pip->cmn.shader_id.id == pip->shader->slot.id)); |
| 16687 | |
| 16688 | // has expected vertex buffers, and vertex buffers still exist |
| 16689 | for (int i = 0; i < SG_MAX_VERTEX_BUFFERS; i++) { |
| 16690 | if (bindings->vertex_buffers[i].id != SG_INVALID_ID) { |
| 16691 | _SG_VALIDATE(pip->cmn.vertex_buffer_layout_active[i], VALIDATE_ABND_VBS); |
| 16692 | // buffers in vertex-buffer-slots must be of type SG_BUFFERTYPE_VERTEXBUFFER |
| 16693 | const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, bindings->vertex_buffers[i].id); |
| 16694 | _SG_VALIDATE(buf != 0, VALIDATE_ABND_VB_EXISTS); |
| 16695 | if (buf && buf->slot.state == SG_RESOURCESTATE_VALID) { |
| 16696 | _SG_VALIDATE(SG_BUFFERTYPE_VERTEXBUFFER == buf->cmn.type, VALIDATE_ABND_VB_TYPE); |
| 16697 | _SG_VALIDATE(!buf->cmn.append_overflow, VALIDATE_ABND_VB_OVERFLOW); |
| 16698 | } |
| 16699 | } else { |
| 16700 | // vertex buffer provided in a slot which has no vertex layout in pipeline |
| 16701 | _SG_VALIDATE(!pip->cmn.vertex_buffer_layout_active[i], VALIDATE_ABND_VBS); |
| 16702 | } |
| 16703 | } |
| 16704 | |
| 16705 | // index buffer expected or not, and index buffer still exists |
| 16706 | if (pip->cmn.index_type == SG_INDEXTYPE_NONE) { |
| 16707 | // pipeline defines non-indexed rendering, but index buffer provided |
| 16708 | _SG_VALIDATE(bindings->index_buffer.id == SG_INVALID_ID, VALIDATE_ABND_IB); |
| 16709 | } else { |
| 16710 | // pipeline defines indexed rendering, but no index buffer provided |
| 16711 | _SG_VALIDATE(bindings->index_buffer.id != SG_INVALID_ID, VALIDATE_ABND_NO_IB); |
| 16712 | } |
| 16713 | if (bindings->index_buffer.id != SG_INVALID_ID) { |
| 16714 | // buffer in index-buffer-slot must be of type SG_BUFFERTYPE_INDEXBUFFER |
| 16715 | const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, bindings->index_buffer.id); |
| 16716 | _SG_VALIDATE(buf != 0, VALIDATE_ABND_IB_EXISTS); |
| 16717 | if (buf && buf->slot.state == SG_RESOURCESTATE_VALID) { |
| 16718 | _SG_VALIDATE(SG_BUFFERTYPE_INDEXBUFFER == buf->cmn.type, VALIDATE_ABND_IB_TYPE); |
| 16719 | _SG_VALIDATE(!buf->cmn.append_overflow, VALIDATE_ABND_IB_OVERFLOW); |
| 16720 | } |
| 16721 | } |
| 16722 | |
| 16723 | // has expected vertex shader images |
| 16724 | for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++) { |
| 16725 | const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_VS]; |
| 16726 | if (stage->images[i].image_type != _SG_IMAGETYPE_DEFAULT) { |
| 16727 | _SG_VALIDATE(bindings->vs.images[i].id != SG_INVALID_ID, VALIDATE_ABND_VS_EXPECTED_IMAGE_BINDING); |
| 16728 | if (bindings->vs.images[i].id != SG_INVALID_ID) { |
| 16729 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, bindings->vs.images[i].id); |
| 16730 | _SG_VALIDATE(img != 0, VALIDATE_ABND_VS_IMG_EXISTS); |
| 16731 | if (img && img->slot.state == SG_RESOURCESTATE_VALID) { |
| 16732 | _SG_VALIDATE(img->cmn.type == stage->images[i].image_type, VALIDATE_ABND_VS_IMAGE_TYPE_MISMATCH); |
| 16733 | _SG_VALIDATE(img->cmn.sample_count == 1, VALIDATE_ABND_VS_IMAGE_MSAA); |
| 16734 | const _sg_pixelformat_info_t* info = &_sg.formats[img->cmn.pixel_format]; |
| 16735 | switch (stage->images[i].sample_type) { |
| 16736 | case SG_IMAGESAMPLETYPE_FLOAT: |
| 16737 | _SG_VALIDATE(info->filter, VALIDATE_ABND_VS_EXPECTED_FILTERABLE_IMAGE); |
| 16738 | break; |
| 16739 | case SG_IMAGESAMPLETYPE_DEPTH: |
| 16740 | _SG_VALIDATE(info->depth, VALIDATE_ABND_VS_EXPECTED_DEPTH_IMAGE); |
| 16741 | break; |
| 16742 | default: |
| 16743 | break; |
| 16744 | } |
| 16745 | } |
| 16746 | } |
| 16747 | } else { |
| 16748 | _SG_VALIDATE(bindings->vs.images[i].id == SG_INVALID_ID, VALIDATE_ABND_VS_UNEXPECTED_IMAGE_BINDING); |
| 16749 | } |
| 16750 | } |
| 16751 | |
| 16752 | // has expected vertex shader image samplers |
| 16753 | for (int i = 0; i < SG_MAX_SHADERSTAGE_SAMPLERS; i++) { |
| 16754 | const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_VS]; |
| 16755 | if (stage->samplers[i].sampler_type != _SG_SAMPLERTYPE_DEFAULT) { |
| 16756 | _SG_VALIDATE(bindings->vs.samplers[i].id != SG_INVALID_ID, VALIDATE_ABND_VS_EXPECTED_SAMPLER_BINDING); |
| 16757 | if (bindings->vs.samplers[i].id != SG_INVALID_ID) { |
| 16758 | const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, bindings->vs.samplers[i].id); |
| 16759 | _SG_VALIDATE(smp != 0, VALIDATE_ABND_VS_SMP_EXISTS); |
| 16760 | if (smp) { |
| 16761 | if (stage->samplers[i].sampler_type == SG_SAMPLERTYPE_COMPARISON) { |
| 16762 | _SG_VALIDATE(smp->cmn.compare != SG_COMPAREFUNC_NEVER, VALIDATE_ABND_VS_UNEXPECTED_SAMPLER_COMPARE_NEVER); |
| 16763 | } else { |
| 16764 | _SG_VALIDATE(smp->cmn.compare == SG_COMPAREFUNC_NEVER, VALIDATE_ABND_VS_EXPECTED_SAMPLER_COMPARE_NEVER); |
| 16765 | } |
| 16766 | if (stage->samplers[i].sampler_type == SG_SAMPLERTYPE_NONFILTERING) { |
| 16767 | const bool nonfiltering = (smp->cmn.min_filter != SG_FILTER_LINEAR) |
| 16768 | && (smp->cmn.mag_filter != SG_FILTER_LINEAR) |
| 16769 | && (smp->cmn.mipmap_filter != SG_FILTER_LINEAR); |
| 16770 | _SG_VALIDATE(nonfiltering, VALIDATE_ABND_VS_EXPECTED_NONFILTERING_SAMPLER); |
| 16771 | } |
| 16772 | } |
| 16773 | } |
| 16774 | } else { |
| 16775 | _SG_VALIDATE(bindings->vs.samplers[i].id == SG_INVALID_ID, VALIDATE_ABND_VS_UNEXPECTED_SAMPLER_BINDING); |
| 16776 | } |
| 16777 | } |
| 16778 | |
| 16779 | // has expected vertex shader storage buffers |
| 16780 | for (int i = 0; i < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; i++) { |
| 16781 | const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_VS]; |
| 16782 | if (stage->storage_buffers[i].used) { |
| 16783 | _SG_VALIDATE(bindings->vs.storage_buffers[i].id != SG_INVALID_ID, VALIDATE_ABND_VS_EXPECTED_STORAGEBUFFER_BINDING); |
| 16784 | if (bindings->vs.storage_buffers[i].id != SG_INVALID_ID) { |
| 16785 | const _sg_buffer_t* sbuf = _sg_lookup_buffer(&_sg.pools, bindings->vs.storage_buffers[i].id); |
| 16786 | _SG_VALIDATE(sbuf != 0, VALIDATE_ABND_VS_STORAGEBUFFER_EXISTS); |
| 16787 | if (sbuf) { |
| 16788 | _SG_VALIDATE(sbuf->cmn.type == SG_BUFFERTYPE_STORAGEBUFFER, VALIDATE_ABND_VS_STORAGEBUFFER_BINDING_BUFFERTYPE); |
| 16789 | } |
| 16790 | } |
| 16791 | } else { |
| 16792 | _SG_VALIDATE(bindings->vs.storage_buffers[i].id == SG_INVALID_ID, VALIDATE_ABND_VS_UNEXPECTED_STORAGEBUFFER_BINDING); |
| 16793 | } |
| 16794 | } |
| 16795 | |
| 16796 | // has expected fragment shader images |
| 16797 | for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++) { |
| 16798 | const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_FS]; |
| 16799 | if (stage->images[i].image_type != _SG_IMAGETYPE_DEFAULT) { |
| 16800 | _SG_VALIDATE(bindings->fs.images[i].id != SG_INVALID_ID, VALIDATE_ABND_FS_EXPECTED_IMAGE_BINDING); |
| 16801 | if (bindings->fs.images[i].id != SG_INVALID_ID) { |
| 16802 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, bindings->fs.images[i].id); |
| 16803 | _SG_VALIDATE(img != 0, VALIDATE_ABND_FS_IMG_EXISTS); |
| 16804 | if (img && img->slot.state == SG_RESOURCESTATE_VALID) { |
| 16805 | _SG_VALIDATE(img->cmn.type == stage->images[i].image_type, VALIDATE_ABND_FS_IMAGE_TYPE_MISMATCH); |
| 16806 | _SG_VALIDATE(img->cmn.sample_count == 1, VALIDATE_ABND_FS_IMAGE_MSAA); |
| 16807 | const _sg_pixelformat_info_t* info = &_sg.formats[img->cmn.pixel_format]; |
| 16808 | switch (stage->images[i].sample_type) { |
| 16809 | case SG_IMAGESAMPLETYPE_FLOAT: |
| 16810 | _SG_VALIDATE(info->filter, VALIDATE_ABND_FS_EXPECTED_FILTERABLE_IMAGE); |
| 16811 | break; |
| 16812 | case SG_IMAGESAMPLETYPE_DEPTH: |
| 16813 | _SG_VALIDATE(info->depth, VALIDATE_ABND_FS_EXPECTED_DEPTH_IMAGE); |
| 16814 | break; |
| 16815 | default: |
| 16816 | break; |
| 16817 | } |
| 16818 | } |
| 16819 | } |
| 16820 | } else { |
| 16821 | _SG_VALIDATE(bindings->fs.images[i].id == SG_INVALID_ID, VALIDATE_ABND_FS_UNEXPECTED_IMAGE_BINDING); |
| 16822 | } |
| 16823 | } |
| 16824 | |
| 16825 | // has expected fragment shader samplers |
| 16826 | for (int i = 0; i < SG_MAX_SHADERSTAGE_SAMPLERS; i++) { |
| 16827 | const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_FS]; |
| 16828 | if (stage->samplers[i].sampler_type != _SG_SAMPLERTYPE_DEFAULT) { |
| 16829 | _SG_VALIDATE(bindings->fs.samplers[i].id != SG_INVALID_ID, VALIDATE_ABND_FS_EXPECTED_SAMPLER_BINDING); |
| 16830 | if (bindings->fs.samplers[i].id != SG_INVALID_ID) { |
| 16831 | const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, bindings->fs.samplers[i].id); |
| 16832 | _SG_VALIDATE(smp != 0, VALIDATE_ABND_FS_SMP_EXISTS); |
| 16833 | if (smp) { |
| 16834 | if (stage->samplers[i].sampler_type == SG_SAMPLERTYPE_COMPARISON) { |
| 16835 | _SG_VALIDATE(smp->cmn.compare != SG_COMPAREFUNC_NEVER, VALIDATE_ABND_FS_UNEXPECTED_SAMPLER_COMPARE_NEVER); |
| 16836 | } else { |
| 16837 | _SG_VALIDATE(smp->cmn.compare == SG_COMPAREFUNC_NEVER, VALIDATE_ABND_FS_EXPECTED_SAMPLER_COMPARE_NEVER); |
| 16838 | } |
| 16839 | if (stage->samplers[i].sampler_type == SG_SAMPLERTYPE_NONFILTERING) { |
| 16840 | const bool nonfiltering = (smp->cmn.min_filter != SG_FILTER_LINEAR) |
| 16841 | && (smp->cmn.mag_filter != SG_FILTER_LINEAR) |
| 16842 | && (smp->cmn.mipmap_filter != SG_FILTER_LINEAR); |
| 16843 | _SG_VALIDATE(nonfiltering, VALIDATE_ABND_FS_EXPECTED_NONFILTERING_SAMPLER); |
| 16844 | } |
| 16845 | } |
| 16846 | } |
| 16847 | } else { |
| 16848 | _SG_VALIDATE(bindings->fs.samplers[i].id == SG_INVALID_ID, VALIDATE_ABND_FS_UNEXPECTED_SAMPLER_BINDING); |
| 16849 | } |
| 16850 | } |
| 16851 | |
| 16852 | // has expected fragment shader storage buffers |
| 16853 | for (int i = 0; i < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; i++) { |
| 16854 | const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[SG_SHADERSTAGE_FS]; |
| 16855 | if (stage->storage_buffers[i].used) { |
| 16856 | _SG_VALIDATE(bindings->fs.storage_buffers[i].id != SG_INVALID_ID, VALIDATE_ABND_FS_EXPECTED_STORAGEBUFFER_BINDING); |
| 16857 | if (bindings->fs.storage_buffers[i].id != SG_INVALID_ID) { |
| 16858 | const _sg_buffer_t* sbuf = _sg_lookup_buffer(&_sg.pools, bindings->fs.storage_buffers[i].id); |
| 16859 | _SG_VALIDATE(sbuf != 0, VALIDATE_ABND_FS_STORAGEBUFFER_EXISTS); |
| 16860 | if (sbuf) { |
| 16861 | _SG_VALIDATE(sbuf->cmn.type == SG_BUFFERTYPE_STORAGEBUFFER, VALIDATE_ABND_FS_STORAGEBUFFER_BINDING_BUFFERTYPE); |
| 16862 | } |
| 16863 | } |
| 16864 | } else { |
| 16865 | _SG_VALIDATE(bindings->fs.storage_buffers[i].id == SG_INVALID_ID, VALIDATE_ABND_FS_UNEXPECTED_STORAGEBUFFER_BINDING); |
| 16866 | } |
| 16867 | } |
| 16868 | |
| 16869 | return _sg_validate_end(); |
| 16870 | #endif |
| 16871 | } |
| 16872 | |
| 16873 | _SOKOL_PRIVATE bool _sg_validate_apply_uniforms(sg_shader_stage stage_index, int ub_index, const sg_range* data) { |
| 16874 | #if !defined(SOKOL_DEBUG) |
| 16875 | _SOKOL_UNUSED(stage_index); |
| 16876 | _SOKOL_UNUSED(ub_index); |
| 16877 | _SOKOL_UNUSED(data); |
| 16878 | return true; |
| 16879 | #else |
| 16880 | if (_sg.desc.disable_validation) { |
| 16881 | return true; |
| 16882 | } |
| 16883 | SOKOL_ASSERT((stage_index == SG_SHADERSTAGE_VS) || (stage_index == SG_SHADERSTAGE_FS)); |
| 16884 | SOKOL_ASSERT((ub_index >= 0) && (ub_index < SG_MAX_SHADERSTAGE_UBS)); |
| 16885 | _sg_validate_begin(); |
| 16886 | _SG_VALIDATE(_sg.cur_pipeline.id != SG_INVALID_ID, VALIDATE_AUB_NO_PIPELINE); |
| 16887 | const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, _sg.cur_pipeline.id); |
| 16888 | SOKOL_ASSERT(pip && (pip->slot.id == _sg.cur_pipeline.id)); |
| 16889 | SOKOL_ASSERT(pip->shader && (pip->shader->slot.id == pip->cmn.shader_id.id)); |
| 16890 | |
| 16891 | // check that there is a uniform block at 'stage' and 'ub_index' |
| 16892 | const _sg_shader_stage_t* stage = &pip->shader->cmn.stage[stage_index]; |
| 16893 | _SG_VALIDATE(ub_index < stage->num_uniform_blocks, VALIDATE_AUB_NO_UB_AT_SLOT); |
| 16894 | |
| 16895 | // check that the provided data size matches the uniform block size |
| 16896 | _SG_VALIDATE(data->size == stage->uniform_blocks[ub_index].size, VALIDATE_AUB_SIZE); |
| 16897 | |
| 16898 | return _sg_validate_end(); |
| 16899 | #endif |
| 16900 | } |
| 16901 | |
| 16902 | _SOKOL_PRIVATE bool _sg_validate_update_buffer(const _sg_buffer_t* buf, const sg_range* data) { |
| 16903 | #if !defined(SOKOL_DEBUG) |
| 16904 | _SOKOL_UNUSED(buf); |
| 16905 | _SOKOL_UNUSED(data); |
| 16906 | return true; |
| 16907 | #else |
| 16908 | if (_sg.desc.disable_validation) { |
| 16909 | return true; |
| 16910 | } |
| 16911 | SOKOL_ASSERT(buf && data && data->ptr); |
| 16912 | _sg_validate_begin(); |
| 16913 | _SG_VALIDATE(buf->cmn.usage != SG_USAGE_IMMUTABLE, VALIDATE_UPDATEBUF_USAGE); |
| 16914 | _SG_VALIDATE(buf->cmn.size >= (int)data->size, VALIDATE_UPDATEBUF_SIZE); |
| 16915 | _SG_VALIDATE(buf->cmn.update_frame_index != _sg.frame_index, VALIDATE_UPDATEBUF_ONCE); |
| 16916 | _SG_VALIDATE(buf->cmn.append_frame_index != _sg.frame_index, VALIDATE_UPDATEBUF_APPEND); |
| 16917 | return _sg_validate_end(); |
| 16918 | #endif |
| 16919 | } |
| 16920 | |
| 16921 | _SOKOL_PRIVATE bool _sg_validate_append_buffer(const _sg_buffer_t* buf, const sg_range* data) { |
| 16922 | #if !defined(SOKOL_DEBUG) |
| 16923 | _SOKOL_UNUSED(buf); |
| 16924 | _SOKOL_UNUSED(data); |
| 16925 | return true; |
| 16926 | #else |
| 16927 | if (_sg.desc.disable_validation) { |
| 16928 | return true; |
| 16929 | } |
| 16930 | SOKOL_ASSERT(buf && data && data->ptr); |
| 16931 | _sg_validate_begin(); |
| 16932 | _SG_VALIDATE(buf->cmn.usage != SG_USAGE_IMMUTABLE, VALIDATE_APPENDBUF_USAGE); |
| 16933 | _SG_VALIDATE(buf->cmn.size >= (buf->cmn.append_pos + (int)data->size), VALIDATE_APPENDBUF_SIZE); |
| 16934 | _SG_VALIDATE(buf->cmn.update_frame_index != _sg.frame_index, VALIDATE_APPENDBUF_UPDATE); |
| 16935 | return _sg_validate_end(); |
| 16936 | #endif |
| 16937 | } |
| 16938 | |
| 16939 | _SOKOL_PRIVATE bool _sg_validate_update_image(const _sg_image_t* img, const sg_image_data* data) { |
| 16940 | #if !defined(SOKOL_DEBUG) |
| 16941 | _SOKOL_UNUSED(img); |
| 16942 | _SOKOL_UNUSED(data); |
| 16943 | return true; |
| 16944 | #else |
| 16945 | if (_sg.desc.disable_validation) { |
| 16946 | return true; |
| 16947 | } |
| 16948 | SOKOL_ASSERT(img && data); |
| 16949 | _sg_validate_begin(); |
| 16950 | _SG_VALIDATE(img->cmn.usage != SG_USAGE_IMMUTABLE, VALIDATE_UPDIMG_USAGE); |
| 16951 | _SG_VALIDATE(img->cmn.upd_frame_index != _sg.frame_index, VALIDATE_UPDIMG_ONCE); |
| 16952 | _sg_validate_image_data(data, |
| 16953 | img->cmn.pixel_format, |
| 16954 | img->cmn.width, |
| 16955 | img->cmn.height, |
| 16956 | (img->cmn.type == SG_IMAGETYPE_CUBE) ? 6 : 1, |
| 16957 | img->cmn.num_mipmaps, |
| 16958 | img->cmn.num_slices); |
| 16959 | return _sg_validate_end(); |
| 16960 | #endif |
| 16961 | } |
| 16962 | |
| 16963 | // ██████ ███████ ███████ ██████ ██ ██ ██████ ██████ ███████ ███████ |
| 16964 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 16965 | // ██████ █████ ███████ ██ ██ ██ ██ ██████ ██ █████ ███████ |
| 16966 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 16967 | // ██ ██ ███████ ███████ ██████ ██████ ██ ██ ██████ ███████ ███████ |
| 16968 | // |
| 16969 | // >>resources |
| 16970 | _SOKOL_PRIVATE sg_buffer_desc _sg_buffer_desc_defaults(const sg_buffer_desc* desc) { |
| 16971 | sg_buffer_desc def = *desc; |
| 16972 | def.type = _sg_def(def.type, SG_BUFFERTYPE_VERTEXBUFFER); |
| 16973 | def.usage = _sg_def(def.usage, SG_USAGE_IMMUTABLE); |
| 16974 | if (def.size == 0) { |
| 16975 | def.size = def.data.size; |
| 16976 | } else if (def.data.size == 0) { |
| 16977 | def.data.size = def.size; |
| 16978 | } |
| 16979 | return def; |
| 16980 | } |
| 16981 | |
| 16982 | _SOKOL_PRIVATE sg_image_desc _sg_image_desc_defaults(const sg_image_desc* desc) { |
| 16983 | sg_image_desc def = *desc; |
| 16984 | def.type = _sg_def(def.type, SG_IMAGETYPE_2D); |
| 16985 | def.num_slices = _sg_def(def.num_slices, 1); |
| 16986 | def.num_mipmaps = _sg_def(def.num_mipmaps, 1); |
| 16987 | def.usage = _sg_def(def.usage, SG_USAGE_IMMUTABLE); |
| 16988 | if (desc->render_target) { |
| 16989 | def.pixel_format = _sg_def(def.pixel_format, _sg.desc.environment.defaults.color_format); |
| 16990 | def.sample_count = _sg_def(def.sample_count, _sg.desc.environment.defaults.sample_count); |
| 16991 | } else { |
| 16992 | def.pixel_format = _sg_def(def.pixel_format, SG_PIXELFORMAT_RGBA8); |
| 16993 | def.sample_count = _sg_def(def.sample_count, 1); |
| 16994 | } |
| 16995 | return def; |
| 16996 | } |
| 16997 | |
| 16998 | _SOKOL_PRIVATE sg_sampler_desc _sg_sampler_desc_defaults(const sg_sampler_desc* desc) { |
| 16999 | sg_sampler_desc def = *desc; |
| 17000 | def.min_filter = _sg_def(def.min_filter, SG_FILTER_NEAREST); |
| 17001 | def.mag_filter = _sg_def(def.mag_filter, SG_FILTER_NEAREST); |
| 17002 | def.mipmap_filter = _sg_def(def.mipmap_filter, SG_FILTER_NONE); |
| 17003 | def.wrap_u = _sg_def(def.wrap_u, SG_WRAP_REPEAT); |
| 17004 | def.wrap_v = _sg_def(def.wrap_v, SG_WRAP_REPEAT); |
| 17005 | def.wrap_w = _sg_def(def.wrap_w, SG_WRAP_REPEAT); |
| 17006 | def.max_lod = _sg_def_flt(def.max_lod, FLT_MAX); |
| 17007 | def.border_color = _sg_def(def.border_color, SG_BORDERCOLOR_OPAQUE_BLACK); |
| 17008 | def.compare = _sg_def(def.compare, SG_COMPAREFUNC_NEVER); |
| 17009 | def.max_anisotropy = _sg_def(def.max_anisotropy, 1); |
| 17010 | return def; |
| 17011 | } |
| 17012 | |
| 17013 | _SOKOL_PRIVATE sg_shader_desc _sg_shader_desc_defaults(const sg_shader_desc* desc) { |
| 17014 | sg_shader_desc def = *desc; |
| 17015 | #if defined(SOKOL_METAL) |
| 17016 | def.vs.entry = _sg_def(def.vs.entry, "_main"); |
| 17017 | def.fs.entry = _sg_def(def.fs.entry, "_main"); |
| 17018 | #else |
| 17019 | def.vs.entry = _sg_def(def.vs.entry, "main"); |
| 17020 | def.fs.entry = _sg_def(def.fs.entry, "main"); |
| 17021 | #endif |
| 17022 | #if defined(SOKOL_D3D11) |
| 17023 | if (def.vs.source) { |
| 17024 | def.vs.d3d11_target = _sg_def(def.vs.d3d11_target, "vs_4_0"); |
| 17025 | } |
| 17026 | if (def.fs.source) { |
| 17027 | def.fs.d3d11_target = _sg_def(def.fs.d3d11_target, "ps_4_0"); |
| 17028 | } |
| 17029 | #endif |
| 17030 | for (int stage_index = 0; stage_index < SG_NUM_SHADER_STAGES; stage_index++) { |
| 17031 | sg_shader_stage_desc* stage_desc = (stage_index == SG_SHADERSTAGE_VS)? &def.vs : &def.fs; |
| 17032 | for (int ub_index = 0; ub_index < SG_MAX_SHADERSTAGE_UBS; ub_index++) { |
| 17033 | sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_index]; |
| 17034 | if (0 == ub_desc->size) { |
| 17035 | break; |
| 17036 | } |
| 17037 | ub_desc->layout = _sg_def(ub_desc->layout, SG_UNIFORMLAYOUT_NATIVE); |
| 17038 | for (int u_index = 0; u_index < SG_MAX_UB_MEMBERS; u_index++) { |
| 17039 | sg_shader_uniform_desc* u_desc = &ub_desc->uniforms[u_index]; |
| 17040 | if (u_desc->type == SG_UNIFORMTYPE_INVALID) { |
| 17041 | break; |
| 17042 | } |
| 17043 | u_desc->array_count = _sg_def(u_desc->array_count, 1); |
| 17044 | } |
| 17045 | } |
| 17046 | for (int img_index = 0; img_index < SG_MAX_SHADERSTAGE_IMAGES; img_index++) { |
| 17047 | sg_shader_image_desc* img_desc = &stage_desc->images[img_index]; |
| 17048 | if (!img_desc->used) { |
| 17049 | break; |
| 17050 | } |
| 17051 | img_desc->image_type = _sg_def(img_desc->image_type, SG_IMAGETYPE_2D); |
| 17052 | img_desc->sample_type = _sg_def(img_desc->sample_type, SG_IMAGESAMPLETYPE_FLOAT); |
| 17053 | } |
| 17054 | for (int smp_index = 0; smp_index < SG_MAX_SHADERSTAGE_SAMPLERS; smp_index++) { |
| 17055 | sg_shader_sampler_desc* smp_desc = &stage_desc->samplers[smp_index]; |
| 17056 | if (!smp_desc->used) { |
| 17057 | break; |
| 17058 | } |
| 17059 | smp_desc->sampler_type = _sg_def(smp_desc->sampler_type, SG_SAMPLERTYPE_FILTERING); |
| 17060 | } |
| 17061 | } |
| 17062 | return def; |
| 17063 | } |
| 17064 | |
| 17065 | _SOKOL_PRIVATE sg_pipeline_desc _sg_pipeline_desc_defaults(const sg_pipeline_desc* desc) { |
| 17066 | sg_pipeline_desc def = *desc; |
| 17067 | |
| 17068 | def.primitive_type = _sg_def(def.primitive_type, SG_PRIMITIVETYPE_TRIANGLES); |
| 17069 | def.index_type = _sg_def(def.index_type, SG_INDEXTYPE_NONE); |
| 17070 | def.cull_mode = _sg_def(def.cull_mode, SG_CULLMODE_NONE); |
| 17071 | def.face_winding = _sg_def(def.face_winding, SG_FACEWINDING_CW); |
| 17072 | def.sample_count = _sg_def(def.sample_count, _sg.desc.environment.defaults.sample_count); |
| 17073 | |
| 17074 | def.stencil.front.compare = _sg_def(def.stencil.front.compare, SG_COMPAREFUNC_ALWAYS); |
| 17075 | def.stencil.front.fail_op = _sg_def(def.stencil.front.fail_op, SG_STENCILOP_KEEP); |
| 17076 | def.stencil.front.depth_fail_op = _sg_def(def.stencil.front.depth_fail_op, SG_STENCILOP_KEEP); |
| 17077 | def.stencil.front.pass_op = _sg_def(def.stencil.front.pass_op, SG_STENCILOP_KEEP); |
| 17078 | def.stencil.back.compare = _sg_def(def.stencil.back.compare, SG_COMPAREFUNC_ALWAYS); |
| 17079 | def.stencil.back.fail_op = _sg_def(def.stencil.back.fail_op, SG_STENCILOP_KEEP); |
| 17080 | def.stencil.back.depth_fail_op = _sg_def(def.stencil.back.depth_fail_op, SG_STENCILOP_KEEP); |
| 17081 | def.stencil.back.pass_op = _sg_def(def.stencil.back.pass_op, SG_STENCILOP_KEEP); |
| 17082 | |
| 17083 | def.depth.compare = _sg_def(def.depth.compare, SG_COMPAREFUNC_ALWAYS); |
| 17084 | def.depth.pixel_format = _sg_def(def.depth.pixel_format, _sg.desc.environment.defaults.depth_format); |
| 17085 | if (def.colors[0].pixel_format == SG_PIXELFORMAT_NONE) { |
| 17086 | // special case depth-only rendering, enforce a color count of 0 |
| 17087 | def.color_count = 0; |
| 17088 | } else { |
| 17089 | def.color_count = _sg_def(def.color_count, 1); |
| 17090 | } |
| 17091 | if (def.color_count > SG_MAX_COLOR_ATTACHMENTS) { |
| 17092 | def.color_count = SG_MAX_COLOR_ATTACHMENTS; |
| 17093 | } |
| 17094 | for (int i = 0; i < def.color_count; i++) { |
| 17095 | sg_color_target_state* cs = &def.colors[i]; |
| 17096 | cs->pixel_format = _sg_def(cs->pixel_format, _sg.desc.environment.defaults.color_format); |
| 17097 | cs->write_mask = _sg_def(cs->write_mask, SG_COLORMASK_RGBA); |
| 17098 | sg_blend_state* bs = &def.colors[i].blend; |
| 17099 | bs->src_factor_rgb = _sg_def(bs->src_factor_rgb, SG_BLENDFACTOR_ONE); |
| 17100 | bs->dst_factor_rgb = _sg_def(bs->dst_factor_rgb, SG_BLENDFACTOR_ZERO); |
| 17101 | bs->op_rgb = _sg_def(bs->op_rgb, SG_BLENDOP_ADD); |
| 17102 | bs->src_factor_alpha = _sg_def(bs->src_factor_alpha, SG_BLENDFACTOR_ONE); |
| 17103 | bs->dst_factor_alpha = _sg_def(bs->dst_factor_alpha, SG_BLENDFACTOR_ZERO); |
| 17104 | bs->op_alpha = _sg_def(bs->op_alpha, SG_BLENDOP_ADD); |
| 17105 | } |
| 17106 | |
| 17107 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 17108 | sg_vertex_attr_state* a_state = &def.layout.attrs[attr_index]; |
| 17109 | if (a_state->format == SG_VERTEXFORMAT_INVALID) { |
| 17110 | break; |
| 17111 | } |
| 17112 | SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS); |
| 17113 | sg_vertex_buffer_layout_state* l_state = &def.layout.buffers[a_state->buffer_index]; |
| 17114 | l_state->step_func = _sg_def(l_state->step_func, SG_VERTEXSTEP_PER_VERTEX); |
| 17115 | l_state->step_rate = _sg_def(l_state->step_rate, 1); |
| 17116 | } |
| 17117 | |
| 17118 | // resolve vertex layout strides and offsets |
| 17119 | int auto_offset[SG_MAX_VERTEX_BUFFERS]; |
| 17120 | _sg_clear(auto_offset, sizeof(auto_offset)); |
| 17121 | bool use_auto_offset = true; |
| 17122 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 17123 | // to use computed offsets, *all* attr offsets must be 0 |
| 17124 | if (def.layout.attrs[attr_index].offset != 0) { |
| 17125 | use_auto_offset = false; |
| 17126 | } |
| 17127 | } |
| 17128 | for (int attr_index = 0; attr_index < SG_MAX_VERTEX_ATTRIBUTES; attr_index++) { |
| 17129 | sg_vertex_attr_state* a_state = &def.layout.attrs[attr_index]; |
| 17130 | if (a_state->format == SG_VERTEXFORMAT_INVALID) { |
| 17131 | break; |
| 17132 | } |
| 17133 | SOKOL_ASSERT(a_state->buffer_index < SG_MAX_VERTEX_BUFFERS); |
| 17134 | if (use_auto_offset) { |
| 17135 | a_state->offset = auto_offset[a_state->buffer_index]; |
| 17136 | } |
| 17137 | auto_offset[a_state->buffer_index] += _sg_vertexformat_bytesize(a_state->format); |
| 17138 | } |
| 17139 | // compute vertex strides if needed |
| 17140 | for (int buf_index = 0; buf_index < SG_MAX_VERTEX_BUFFERS; buf_index++) { |
| 17141 | sg_vertex_buffer_layout_state* l_state = &def.layout.buffers[buf_index]; |
| 17142 | if (l_state->stride == 0) { |
| 17143 | l_state->stride = auto_offset[buf_index]; |
| 17144 | } |
| 17145 | } |
| 17146 | |
| 17147 | return def; |
| 17148 | } |
| 17149 | |
| 17150 | _SOKOL_PRIVATE sg_attachments_desc _sg_attachments_desc_defaults(const sg_attachments_desc* desc) { |
| 17151 | sg_attachments_desc def = *desc; |
| 17152 | return def; |
| 17153 | } |
| 17154 | |
| 17155 | _SOKOL_PRIVATE sg_buffer _sg_alloc_buffer(void) { |
| 17156 | sg_buffer res; |
| 17157 | int slot_index = _sg_pool_alloc_index(&_sg.pools.buffer_pool); |
| 17158 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 17159 | res.id = _sg_slot_alloc(&_sg.pools.buffer_pool, &_sg.pools.buffers[slot_index].slot, slot_index); |
| 17160 | } else { |
| 17161 | res.id = SG_INVALID_ID; |
| 17162 | _SG_ERROR(BUFFER_POOL_EXHAUSTED); |
| 17163 | } |
| 17164 | return res; |
| 17165 | } |
| 17166 | |
| 17167 | _SOKOL_PRIVATE sg_image _sg_alloc_image(void) { |
| 17168 | sg_image res; |
| 17169 | int slot_index = _sg_pool_alloc_index(&_sg.pools.image_pool); |
| 17170 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 17171 | res.id = _sg_slot_alloc(&_sg.pools.image_pool, &_sg.pools.images[slot_index].slot, slot_index); |
| 17172 | } else { |
| 17173 | res.id = SG_INVALID_ID; |
| 17174 | _SG_ERROR(IMAGE_POOL_EXHAUSTED); |
| 17175 | } |
| 17176 | return res; |
| 17177 | } |
| 17178 | |
| 17179 | _SOKOL_PRIVATE sg_sampler _sg_alloc_sampler(void) { |
| 17180 | sg_sampler res; |
| 17181 | int slot_index = _sg_pool_alloc_index(&_sg.pools.sampler_pool); |
| 17182 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 17183 | res.id = _sg_slot_alloc(&_sg.pools.sampler_pool, &_sg.pools.samplers[slot_index].slot, slot_index); |
| 17184 | } else { |
| 17185 | res.id = SG_INVALID_ID; |
| 17186 | _SG_ERROR(SAMPLER_POOL_EXHAUSTED); |
| 17187 | } |
| 17188 | return res; |
| 17189 | } |
| 17190 | |
| 17191 | _SOKOL_PRIVATE sg_shader _sg_alloc_shader(void) { |
| 17192 | sg_shader res; |
| 17193 | int slot_index = _sg_pool_alloc_index(&_sg.pools.shader_pool); |
| 17194 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 17195 | res.id = _sg_slot_alloc(&_sg.pools.shader_pool, &_sg.pools.shaders[slot_index].slot, slot_index); |
| 17196 | } else { |
| 17197 | res.id = SG_INVALID_ID; |
| 17198 | _SG_ERROR(SHADER_POOL_EXHAUSTED); |
| 17199 | } |
| 17200 | return res; |
| 17201 | } |
| 17202 | |
| 17203 | _SOKOL_PRIVATE sg_pipeline _sg_alloc_pipeline(void) { |
| 17204 | sg_pipeline res; |
| 17205 | int slot_index = _sg_pool_alloc_index(&_sg.pools.pipeline_pool); |
| 17206 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 17207 | res.id =_sg_slot_alloc(&_sg.pools.pipeline_pool, &_sg.pools.pipelines[slot_index].slot, slot_index); |
| 17208 | } else { |
| 17209 | res.id = SG_INVALID_ID; |
| 17210 | _SG_ERROR(PIPELINE_POOL_EXHAUSTED); |
| 17211 | } |
| 17212 | return res; |
| 17213 | } |
| 17214 | |
| 17215 | _SOKOL_PRIVATE sg_attachments _sg_alloc_attachments(void) { |
| 17216 | sg_attachments res; |
| 17217 | int slot_index = _sg_pool_alloc_index(&_sg.pools.attachments_pool); |
| 17218 | if (_SG_INVALID_SLOT_INDEX != slot_index) { |
| 17219 | res.id = _sg_slot_alloc(&_sg.pools.attachments_pool, &_sg.pools.attachments[slot_index].slot, slot_index); |
| 17220 | } else { |
| 17221 | res.id = SG_INVALID_ID; |
| 17222 | _SG_ERROR(PASS_POOL_EXHAUSTED); |
| 17223 | } |
| 17224 | return res; |
| 17225 | } |
| 17226 | |
| 17227 | _SOKOL_PRIVATE void _sg_dealloc_buffer(_sg_buffer_t* buf) { |
| 17228 | SOKOL_ASSERT(buf && (buf->slot.state == SG_RESOURCESTATE_ALLOC) && (buf->slot.id != SG_INVALID_ID)); |
| 17229 | _sg_pool_free_index(&_sg.pools.buffer_pool, _sg_slot_index(buf->slot.id)); |
| 17230 | _sg_reset_slot(&buf->slot); |
| 17231 | } |
| 17232 | |
| 17233 | _SOKOL_PRIVATE void _sg_dealloc_image(_sg_image_t* img) { |
| 17234 | SOKOL_ASSERT(img && (img->slot.state == SG_RESOURCESTATE_ALLOC) && (img->slot.id != SG_INVALID_ID)); |
| 17235 | _sg_pool_free_index(&_sg.pools.image_pool, _sg_slot_index(img->slot.id)); |
| 17236 | _sg_reset_slot(&img->slot); |
| 17237 | } |
| 17238 | |
| 17239 | _SOKOL_PRIVATE void _sg_dealloc_sampler(_sg_sampler_t* smp) { |
| 17240 | SOKOL_ASSERT(smp && (smp->slot.state == SG_RESOURCESTATE_ALLOC) && (smp->slot.id != SG_INVALID_ID)); |
| 17241 | _sg_pool_free_index(&_sg.pools.sampler_pool, _sg_slot_index(smp->slot.id)); |
| 17242 | _sg_reset_slot(&smp->slot); |
| 17243 | } |
| 17244 | |
| 17245 | _SOKOL_PRIVATE void _sg_dealloc_shader(_sg_shader_t* shd) { |
| 17246 | SOKOL_ASSERT(shd && (shd->slot.state == SG_RESOURCESTATE_ALLOC) && (shd->slot.id != SG_INVALID_ID)); |
| 17247 | _sg_pool_free_index(&_sg.pools.shader_pool, _sg_slot_index(shd->slot.id)); |
| 17248 | _sg_reset_slot(&shd->slot); |
| 17249 | } |
| 17250 | |
| 17251 | _SOKOL_PRIVATE void _sg_dealloc_pipeline(_sg_pipeline_t* pip) { |
| 17252 | SOKOL_ASSERT(pip && (pip->slot.state == SG_RESOURCESTATE_ALLOC) && (pip->slot.id != SG_INVALID_ID)); |
| 17253 | _sg_pool_free_index(&_sg.pools.pipeline_pool, _sg_slot_index(pip->slot.id)); |
| 17254 | _sg_reset_slot(&pip->slot); |
| 17255 | } |
| 17256 | |
| 17257 | _SOKOL_PRIVATE void _sg_dealloc_attachments(_sg_attachments_t* atts) { |
| 17258 | SOKOL_ASSERT(atts && (atts->slot.state == SG_RESOURCESTATE_ALLOC) && (atts->slot.id != SG_INVALID_ID)); |
| 17259 | _sg_pool_free_index(&_sg.pools.attachments_pool, _sg_slot_index(atts->slot.id)); |
| 17260 | _sg_reset_slot(&atts->slot); |
| 17261 | } |
| 17262 | |
| 17263 | _SOKOL_PRIVATE void _sg_init_buffer(_sg_buffer_t* buf, const sg_buffer_desc* desc) { |
| 17264 | SOKOL_ASSERT(buf && (buf->slot.state == SG_RESOURCESTATE_ALLOC)); |
| 17265 | SOKOL_ASSERT(desc); |
| 17266 | if (_sg_validate_buffer_desc(desc)) { |
| 17267 | _sg_buffer_common_init(&buf->cmn, desc); |
| 17268 | buf->slot.state = _sg_create_buffer(buf, desc); |
| 17269 | } else { |
| 17270 | buf->slot.state = SG_RESOURCESTATE_FAILED; |
| 17271 | } |
| 17272 | SOKOL_ASSERT((buf->slot.state == SG_RESOURCESTATE_VALID)||(buf->slot.state == SG_RESOURCESTATE_FAILED)); |
| 17273 | } |
| 17274 | |
| 17275 | _SOKOL_PRIVATE void _sg_init_image(_sg_image_t* img, const sg_image_desc* desc) { |
| 17276 | SOKOL_ASSERT(img && (img->slot.state == SG_RESOURCESTATE_ALLOC)); |
| 17277 | SOKOL_ASSERT(desc); |
| 17278 | if (_sg_validate_image_desc(desc)) { |
| 17279 | _sg_image_common_init(&img->cmn, desc); |
| 17280 | img->slot.state = _sg_create_image(img, desc); |
| 17281 | } else { |
| 17282 | img->slot.state = SG_RESOURCESTATE_FAILED; |
| 17283 | } |
| 17284 | SOKOL_ASSERT((img->slot.state == SG_RESOURCESTATE_VALID)||(img->slot.state == SG_RESOURCESTATE_FAILED)); |
| 17285 | } |
| 17286 | |
| 17287 | _SOKOL_PRIVATE void _sg_init_sampler(_sg_sampler_t* smp, const sg_sampler_desc* desc) { |
| 17288 | SOKOL_ASSERT(smp && (smp->slot.state == SG_RESOURCESTATE_ALLOC)); |
| 17289 | SOKOL_ASSERT(desc); |
| 17290 | if (_sg_validate_sampler_desc(desc)) { |
| 17291 | _sg_sampler_common_init(&smp->cmn, desc); |
| 17292 | smp->slot.state = _sg_create_sampler(smp, desc); |
| 17293 | } else { |
| 17294 | smp->slot.state = SG_RESOURCESTATE_FAILED; |
| 17295 | } |
| 17296 | SOKOL_ASSERT((smp->slot.state == SG_RESOURCESTATE_VALID)||(smp->slot.state == SG_RESOURCESTATE_FAILED)); |
| 17297 | } |
| 17298 | |
| 17299 | _SOKOL_PRIVATE void _sg_init_shader(_sg_shader_t* shd, const sg_shader_desc* desc) { |
| 17300 | SOKOL_ASSERT(shd && (shd->slot.state == SG_RESOURCESTATE_ALLOC)); |
| 17301 | SOKOL_ASSERT(desc); |
| 17302 | if (_sg_validate_shader_desc(desc)) { |
| 17303 | _sg_shader_common_init(&shd->cmn, desc); |
| 17304 | shd->slot.state = _sg_create_shader(shd, desc); |
| 17305 | } else { |
| 17306 | shd->slot.state = SG_RESOURCESTATE_FAILED; |
| 17307 | } |
| 17308 | SOKOL_ASSERT((shd->slot.state == SG_RESOURCESTATE_VALID)||(shd->slot.state == SG_RESOURCESTATE_FAILED)); |
| 17309 | } |
| 17310 | |
| 17311 | _SOKOL_PRIVATE void _sg_init_pipeline(_sg_pipeline_t* pip, const sg_pipeline_desc* desc) { |
| 17312 | SOKOL_ASSERT(pip && (pip->slot.state == SG_RESOURCESTATE_ALLOC)); |
| 17313 | SOKOL_ASSERT(desc); |
| 17314 | if (_sg_validate_pipeline_desc(desc)) { |
| 17315 | _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, desc->shader.id); |
| 17316 | if (shd && (shd->slot.state == SG_RESOURCESTATE_VALID)) { |
| 17317 | _sg_pipeline_common_init(&pip->cmn, desc); |
| 17318 | pip->slot.state = _sg_create_pipeline(pip, shd, desc); |
| 17319 | } else { |
| 17320 | pip->slot.state = SG_RESOURCESTATE_FAILED; |
| 17321 | } |
| 17322 | } else { |
| 17323 | pip->slot.state = SG_RESOURCESTATE_FAILED; |
| 17324 | } |
| 17325 | SOKOL_ASSERT((pip->slot.state == SG_RESOURCESTATE_VALID)||(pip->slot.state == SG_RESOURCESTATE_FAILED)); |
| 17326 | } |
| 17327 | |
| 17328 | _SOKOL_PRIVATE void _sg_init_attachments(_sg_attachments_t* atts, const sg_attachments_desc* desc) { |
| 17329 | SOKOL_ASSERT(atts && atts->slot.state == SG_RESOURCESTATE_ALLOC); |
| 17330 | SOKOL_ASSERT(desc); |
| 17331 | if (_sg_validate_attachments_desc(desc)) { |
| 17332 | // lookup pass attachment image pointers |
| 17333 | _sg_image_t* color_images[SG_MAX_COLOR_ATTACHMENTS] = { 0 }; |
| 17334 | _sg_image_t* resolve_images[SG_MAX_COLOR_ATTACHMENTS] = { 0 }; |
| 17335 | _sg_image_t* ds_image = 0; |
| 17336 | // NOTE: validation already checked that all surfaces are same width/height |
| 17337 | int width = 0; |
| 17338 | int height = 0; |
| 17339 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 17340 | if (desc->colors[i].image.id) { |
| 17341 | color_images[i] = _sg_lookup_image(&_sg.pools, desc->colors[i].image.id); |
| 17342 | if (!(color_images[i] && color_images[i]->slot.state == SG_RESOURCESTATE_VALID)) { |
| 17343 | atts->slot.state = SG_RESOURCESTATE_FAILED; |
| 17344 | return; |
| 17345 | } |
| 17346 | const int mip_level = desc->colors[i].mip_level; |
| 17347 | width = _sg_miplevel_dim(color_images[i]->cmn.width, mip_level); |
| 17348 | height = _sg_miplevel_dim(color_images[i]->cmn.height, mip_level); |
| 17349 | } |
| 17350 | if (desc->resolves[i].image.id) { |
| 17351 | resolve_images[i] = _sg_lookup_image(&_sg.pools, desc->resolves[i].image.id); |
| 17352 | if (!(resolve_images[i] && resolve_images[i]->slot.state == SG_RESOURCESTATE_VALID)) { |
| 17353 | atts->slot.state = SG_RESOURCESTATE_FAILED; |
| 17354 | return; |
| 17355 | } |
| 17356 | } |
| 17357 | } |
| 17358 | if (desc->depth_stencil.image.id) { |
| 17359 | ds_image = _sg_lookup_image(&_sg.pools, desc->depth_stencil.image.id); |
| 17360 | if (!(ds_image && ds_image->slot.state == SG_RESOURCESTATE_VALID)) { |
| 17361 | atts->slot.state = SG_RESOURCESTATE_FAILED; |
| 17362 | return; |
| 17363 | } |
| 17364 | const int mip_level = desc->depth_stencil.mip_level; |
| 17365 | width = _sg_miplevel_dim(ds_image->cmn.width, mip_level); |
| 17366 | height = _sg_miplevel_dim(ds_image->cmn.height, mip_level); |
| 17367 | } |
| 17368 | _sg_attachments_common_init(&atts->cmn, desc, width, height); |
| 17369 | atts->slot.state = _sg_create_attachments(atts, color_images, resolve_images, ds_image, desc); |
| 17370 | } else { |
| 17371 | atts->slot.state = SG_RESOURCESTATE_FAILED; |
| 17372 | } |
| 17373 | SOKOL_ASSERT((atts->slot.state == SG_RESOURCESTATE_VALID)||(atts->slot.state == SG_RESOURCESTATE_FAILED)); |
| 17374 | } |
| 17375 | |
| 17376 | _SOKOL_PRIVATE void _sg_uninit_buffer(_sg_buffer_t* buf) { |
| 17377 | SOKOL_ASSERT(buf && ((buf->slot.state == SG_RESOURCESTATE_VALID) || (buf->slot.state == SG_RESOURCESTATE_FAILED))); |
| 17378 | _sg_discard_buffer(buf); |
| 17379 | _sg_reset_buffer_to_alloc_state(buf); |
| 17380 | } |
| 17381 | |
| 17382 | _SOKOL_PRIVATE void _sg_uninit_image(_sg_image_t* img) { |
| 17383 | SOKOL_ASSERT(img && ((img->slot.state == SG_RESOURCESTATE_VALID) || (img->slot.state == SG_RESOURCESTATE_FAILED))); |
| 17384 | _sg_discard_image(img); |
| 17385 | _sg_reset_image_to_alloc_state(img); |
| 17386 | } |
| 17387 | |
| 17388 | _SOKOL_PRIVATE void _sg_uninit_sampler(_sg_sampler_t* smp) { |
| 17389 | SOKOL_ASSERT(smp && ((smp->slot.state == SG_RESOURCESTATE_VALID) || (smp->slot.state == SG_RESOURCESTATE_FAILED))); |
| 17390 | _sg_discard_sampler(smp); |
| 17391 | _sg_reset_sampler_to_alloc_state(smp); |
| 17392 | } |
| 17393 | |
| 17394 | _SOKOL_PRIVATE void _sg_uninit_shader(_sg_shader_t* shd) { |
| 17395 | SOKOL_ASSERT(shd && ((shd->slot.state == SG_RESOURCESTATE_VALID) || (shd->slot.state == SG_RESOURCESTATE_FAILED))); |
| 17396 | _sg_discard_shader(shd); |
| 17397 | _sg_reset_shader_to_alloc_state(shd); |
| 17398 | } |
| 17399 | |
| 17400 | _SOKOL_PRIVATE void _sg_uninit_pipeline(_sg_pipeline_t* pip) { |
| 17401 | SOKOL_ASSERT(pip && ((pip->slot.state == SG_RESOURCESTATE_VALID) || (pip->slot.state == SG_RESOURCESTATE_FAILED))); |
| 17402 | _sg_discard_pipeline(pip); |
| 17403 | _sg_reset_pipeline_to_alloc_state(pip); |
| 17404 | } |
| 17405 | |
| 17406 | _SOKOL_PRIVATE void _sg_uninit_attachments(_sg_attachments_t* atts) { |
| 17407 | SOKOL_ASSERT(atts && ((atts->slot.state == SG_RESOURCESTATE_VALID) || (atts->slot.state == SG_RESOURCESTATE_FAILED))); |
| 17408 | _sg_discard_attachments(atts); |
| 17409 | _sg_reset_attachments_to_alloc_state(atts); |
| 17410 | } |
| 17411 | |
| 17412 | _SOKOL_PRIVATE void _sg_setup_commit_listeners(const sg_desc* desc) { |
| 17413 | SOKOL_ASSERT(desc->max_commit_listeners > 0); |
| 17414 | SOKOL_ASSERT(0 == _sg.commit_listeners.items); |
| 17415 | SOKOL_ASSERT(0 == _sg.commit_listeners.num); |
| 17416 | SOKOL_ASSERT(0 == _sg.commit_listeners.upper); |
| 17417 | _sg.commit_listeners.num = desc->max_commit_listeners; |
| 17418 | const size_t size = (size_t)_sg.commit_listeners.num * sizeof(sg_commit_listener); |
| 17419 | _sg.commit_listeners.items = (sg_commit_listener*)_sg_malloc_clear(size); |
| 17420 | } |
| 17421 | |
| 17422 | _SOKOL_PRIVATE void _sg_discard_commit_listeners(void) { |
| 17423 | SOKOL_ASSERT(0 != _sg.commit_listeners.items); |
| 17424 | _sg_free(_sg.commit_listeners.items); |
| 17425 | _sg.commit_listeners.items = 0; |
| 17426 | } |
| 17427 | |
| 17428 | _SOKOL_PRIVATE void _sg_notify_commit_listeners(void) { |
| 17429 | SOKOL_ASSERT(_sg.commit_listeners.items); |
| 17430 | for (int i = 0; i < _sg.commit_listeners.upper; i++) { |
| 17431 | const sg_commit_listener* listener = &_sg.commit_listeners.items[i]; |
| 17432 | if (listener->func) { |
| 17433 | listener->func(listener->user_data); |
| 17434 | } |
| 17435 | } |
| 17436 | } |
| 17437 | |
| 17438 | _SOKOL_PRIVATE bool _sg_add_commit_listener(const sg_commit_listener* new_listener) { |
| 17439 | SOKOL_ASSERT(new_listener && new_listener->func); |
| 17440 | SOKOL_ASSERT(_sg.commit_listeners.items); |
| 17441 | // first check if the listener hadn't been added already |
| 17442 | for (int i = 0; i < _sg.commit_listeners.upper; i++) { |
| 17443 | const sg_commit_listener* slot = &_sg.commit_listeners.items[i]; |
| 17444 | if ((slot->func == new_listener->func) && (slot->user_data == new_listener->user_data)) { |
| 17445 | _SG_ERROR(IDENTICAL_COMMIT_LISTENER); |
| 17446 | return false; |
| 17447 | } |
| 17448 | } |
| 17449 | // first try to plug a hole |
| 17450 | sg_commit_listener* slot = 0; |
| 17451 | for (int i = 0; i < _sg.commit_listeners.upper; i++) { |
| 17452 | if (_sg.commit_listeners.items[i].func == 0) { |
| 17453 | slot = &_sg.commit_listeners.items[i]; |
| 17454 | break; |
| 17455 | } |
| 17456 | } |
| 17457 | if (!slot) { |
| 17458 | // append to end |
| 17459 | if (_sg.commit_listeners.upper < _sg.commit_listeners.num) { |
| 17460 | slot = &_sg.commit_listeners.items[_sg.commit_listeners.upper++]; |
| 17461 | } |
| 17462 | } |
| 17463 | if (!slot) { |
| 17464 | _SG_ERROR(COMMIT_LISTENER_ARRAY_FULL); |
| 17465 | return false; |
| 17466 | } |
| 17467 | *slot = *new_listener; |
| 17468 | return true; |
| 17469 | } |
| 17470 | |
| 17471 | _SOKOL_PRIVATE bool _sg_remove_commit_listener(const sg_commit_listener* listener) { |
| 17472 | SOKOL_ASSERT(listener && listener->func); |
| 17473 | SOKOL_ASSERT(_sg.commit_listeners.items); |
| 17474 | for (int i = 0; i < _sg.commit_listeners.upper; i++) { |
| 17475 | sg_commit_listener* slot = &_sg.commit_listeners.items[i]; |
| 17476 | // both the function pointer and user data must match! |
| 17477 | if ((slot->func == listener->func) && (slot->user_data == listener->user_data)) { |
| 17478 | slot->func = 0; |
| 17479 | slot->user_data = 0; |
| 17480 | // NOTE: since _sg_add_commit_listener() already catches duplicates, |
| 17481 | // we don't need to worry about them here |
| 17482 | return true; |
| 17483 | } |
| 17484 | } |
| 17485 | return false; |
| 17486 | } |
| 17487 | |
| 17488 | _SOKOL_PRIVATE sg_desc _sg_desc_defaults(const sg_desc* desc) { |
| 17489 | /* |
| 17490 | NOTE: on WebGPU, the default color pixel format MUST be provided, |
| 17491 | cannot be a default compile-time constant. |
| 17492 | */ |
| 17493 | sg_desc res = *desc; |
| 17494 | #if defined(SOKOL_WGPU) |
| 17495 | SOKOL_ASSERT(SG_PIXELFORMAT_NONE < res.environment.defaults.color_format); |
| 17496 | #elif defined(SOKOL_METAL) || defined(SOKOL_D3D11) |
| 17497 | res.environment.defaults.color_format = _sg_def(res.environment.defaults.color_format, SG_PIXELFORMAT_BGRA8); |
| 17498 | #else |
| 17499 | res.environment.defaults.color_format = _sg_def(res.environment.defaults.color_format, SG_PIXELFORMAT_RGBA8); |
| 17500 | #endif |
| 17501 | res.environment.defaults.depth_format = _sg_def(res.environment.defaults.depth_format, SG_PIXELFORMAT_DEPTH_STENCIL); |
| 17502 | res.environment.defaults.sample_count = _sg_def(res.environment.defaults.sample_count, 1); |
| 17503 | res.buffer_pool_size = _sg_def(res.buffer_pool_size, _SG_DEFAULT_BUFFER_POOL_SIZE); |
| 17504 | res.image_pool_size = _sg_def(res.image_pool_size, _SG_DEFAULT_IMAGE_POOL_SIZE); |
| 17505 | res.sampler_pool_size = _sg_def(res.sampler_pool_size, _SG_DEFAULT_SAMPLER_POOL_SIZE); |
| 17506 | res.shader_pool_size = _sg_def(res.shader_pool_size, _SG_DEFAULT_SHADER_POOL_SIZE); |
| 17507 | res.pipeline_pool_size = _sg_def(res.pipeline_pool_size, _SG_DEFAULT_PIPELINE_POOL_SIZE); |
| 17508 | res.attachments_pool_size = _sg_def(res.attachments_pool_size, _SG_DEFAULT_ATTACHMENTS_POOL_SIZE); |
| 17509 | res.uniform_buffer_size = _sg_def(res.uniform_buffer_size, _SG_DEFAULT_UB_SIZE); |
| 17510 | res.max_commit_listeners = _sg_def(res.max_commit_listeners, _SG_DEFAULT_MAX_COMMIT_LISTENERS); |
| 17511 | res.wgpu_bindgroups_cache_size = _sg_def(res.wgpu_bindgroups_cache_size, _SG_DEFAULT_WGPU_BINDGROUP_CACHE_SIZE); |
| 17512 | return res; |
| 17513 | } |
| 17514 | |
| 17515 | _SOKOL_PRIVATE sg_pass _sg_pass_defaults(const sg_pass* pass) { |
| 17516 | sg_pass res = *pass; |
| 17517 | if (res.attachments.id == SG_INVALID_ID) { |
| 17518 | // this is a swapchain-pass |
| 17519 | res.swapchain.sample_count = _sg_def(res.swapchain.sample_count, _sg.desc.environment.defaults.sample_count); |
| 17520 | res.swapchain.color_format = _sg_def(res.swapchain.color_format, _sg.desc.environment.defaults.color_format); |
| 17521 | res.swapchain.depth_format = _sg_def(res.swapchain.depth_format, _sg.desc.environment.defaults.depth_format); |
| 17522 | } |
| 17523 | res.action = _sg_pass_action_defaults(&res.action); |
| 17524 | return res; |
| 17525 | } |
| 17526 | |
| 17527 | // ██████ ██ ██ ██████ ██ ██ ██████ |
| 17528 | // ██ ██ ██ ██ ██ ██ ██ ██ ██ |
| 17529 | // ██████ ██ ██ ██████ ██ ██ ██ |
| 17530 | // ██ ██ ██ ██ ██ ██ ██ ██ |
| 17531 | // ██ ██████ ██████ ███████ ██ ██████ |
| 17532 | // |
| 17533 | // >>public |
| 17534 | SOKOL_API_IMPL void sg_setup(const sg_desc* desc) { |
| 17535 | SOKOL_ASSERT(desc); |
| 17536 | SOKOL_ASSERT((desc->_start_canary == 0) && (desc->_end_canary == 0)); |
| 17537 | SOKOL_ASSERT((desc->allocator.alloc_fn && desc->allocator.free_fn) || (!desc->allocator.alloc_fn && !desc->allocator.free_fn)); |
| 17538 | _SG_CLEAR_ARC_STRUCT(_sg_state_t, _sg); |
| 17539 | _sg.desc = _sg_desc_defaults(desc); |
| 17540 | _sg_setup_pools(&_sg.pools, &_sg.desc); |
| 17541 | _sg_setup_commit_listeners(&_sg.desc); |
| 17542 | _sg.frame_index = 1; |
| 17543 | _sg.stats_enabled = true; |
| 17544 | _sg_setup_backend(&_sg.desc); |
| 17545 | _sg.valid = true; |
| 17546 | } |
| 17547 | |
| 17548 | SOKOL_API_IMPL void sg_shutdown(void) { |
| 17549 | _sg_discard_all_resources(&_sg.pools); |
| 17550 | _sg_discard_backend(); |
| 17551 | _sg_discard_commit_listeners(); |
| 17552 | _sg_discard_pools(&_sg.pools); |
| 17553 | _SG_CLEAR_ARC_STRUCT(_sg_state_t, _sg); |
| 17554 | } |
| 17555 | |
| 17556 | SOKOL_API_IMPL bool sg_isvalid(void) { |
| 17557 | return _sg.valid; |
| 17558 | } |
| 17559 | |
| 17560 | SOKOL_API_IMPL sg_desc sg_query_desc(void) { |
| 17561 | SOKOL_ASSERT(_sg.valid); |
| 17562 | return _sg.desc; |
| 17563 | } |
| 17564 | |
| 17565 | SOKOL_API_IMPL sg_backend sg_query_backend(void) { |
| 17566 | SOKOL_ASSERT(_sg.valid); |
| 17567 | return _sg.backend; |
| 17568 | } |
| 17569 | |
| 17570 | SOKOL_API_IMPL sg_features sg_query_features(void) { |
| 17571 | SOKOL_ASSERT(_sg.valid); |
| 17572 | return _sg.features; |
| 17573 | } |
| 17574 | |
| 17575 | SOKOL_API_IMPL sg_limits sg_query_limits(void) { |
| 17576 | SOKOL_ASSERT(_sg.valid); |
| 17577 | return _sg.limits; |
| 17578 | } |
| 17579 | |
| 17580 | SOKOL_API_IMPL sg_pixelformat_info sg_query_pixelformat(sg_pixel_format fmt) { |
| 17581 | SOKOL_ASSERT(_sg.valid); |
| 17582 | int fmt_index = (int) fmt; |
| 17583 | SOKOL_ASSERT((fmt_index > SG_PIXELFORMAT_NONE) && (fmt_index < _SG_PIXELFORMAT_NUM)); |
| 17584 | const _sg_pixelformat_info_t* src = &_sg.formats[fmt_index]; |
| 17585 | sg_pixelformat_info res; |
| 17586 | _sg_clear(&res, sizeof(res)); |
| 17587 | res.sample = src->sample; |
| 17588 | res.filter = src->filter; |
| 17589 | res.render = src->render; |
| 17590 | res.blend = src->blend; |
| 17591 | res.msaa = src->msaa; |
| 17592 | res.depth = src->depth; |
| 17593 | res.compressed = _sg_is_compressed_pixel_format(fmt); |
| 17594 | if (!res.compressed) { |
| 17595 | res.bytes_per_pixel = _sg_pixelformat_bytesize(fmt); |
| 17596 | } |
| 17597 | return res; |
| 17598 | } |
| 17599 | |
| 17600 | SOKOL_API_IMPL int sg_query_row_pitch(sg_pixel_format fmt, int width, int row_align_bytes) { |
| 17601 | SOKOL_ASSERT(_sg.valid); |
| 17602 | SOKOL_ASSERT(width > 0); |
| 17603 | SOKOL_ASSERT((row_align_bytes > 0) && _sg_ispow2(row_align_bytes)); |
| 17604 | SOKOL_ASSERT(((int)fmt > SG_PIXELFORMAT_NONE) && ((int)fmt < _SG_PIXELFORMAT_NUM)); |
| 17605 | return _sg_row_pitch(fmt, width, row_align_bytes); |
| 17606 | } |
| 17607 | |
| 17608 | SOKOL_API_IMPL int sg_query_surface_pitch(sg_pixel_format fmt, int width, int height, int row_align_bytes) { |
| 17609 | SOKOL_ASSERT(_sg.valid); |
| 17610 | SOKOL_ASSERT((width > 0) && (height > 0)); |
| 17611 | SOKOL_ASSERT((row_align_bytes > 0) && _sg_ispow2(row_align_bytes)); |
| 17612 | SOKOL_ASSERT(((int)fmt > SG_PIXELFORMAT_NONE) && ((int)fmt < _SG_PIXELFORMAT_NUM)); |
| 17613 | return _sg_surface_pitch(fmt, width, height, row_align_bytes); |
| 17614 | } |
| 17615 | |
| 17616 | SOKOL_API_IMPL sg_frame_stats sg_query_frame_stats(void) { |
| 17617 | SOKOL_ASSERT(_sg.valid); |
| 17618 | return _sg.prev_stats; |
| 17619 | } |
| 17620 | |
| 17621 | SOKOL_API_IMPL sg_trace_hooks sg_install_trace_hooks(const sg_trace_hooks* trace_hooks) { |
| 17622 | SOKOL_ASSERT(_sg.valid); |
| 17623 | SOKOL_ASSERT(trace_hooks); |
| 17624 | _SOKOL_UNUSED(trace_hooks); |
| 17625 | #if defined(SOKOL_TRACE_HOOKS) |
| 17626 | sg_trace_hooks old_hooks = _sg.hooks; |
| 17627 | _sg.hooks = *trace_hooks; |
| 17628 | #else |
| 17629 | static sg_trace_hooks old_hooks; |
| 17630 | _SG_WARN(TRACE_HOOKS_NOT_ENABLED); |
| 17631 | #endif |
| 17632 | return old_hooks; |
| 17633 | } |
| 17634 | |
| 17635 | SOKOL_API_IMPL sg_buffer sg_alloc_buffer(void) { |
| 17636 | SOKOL_ASSERT(_sg.valid); |
| 17637 | sg_buffer res = _sg_alloc_buffer(); |
| 17638 | _SG_TRACE_ARGS(alloc_buffer, res); |
| 17639 | return res; |
| 17640 | } |
| 17641 | |
| 17642 | SOKOL_API_IMPL sg_image sg_alloc_image(void) { |
| 17643 | SOKOL_ASSERT(_sg.valid); |
| 17644 | sg_image res = _sg_alloc_image(); |
| 17645 | _SG_TRACE_ARGS(alloc_image, res); |
| 17646 | return res; |
| 17647 | } |
| 17648 | |
| 17649 | SOKOL_API_IMPL sg_sampler sg_alloc_sampler(void) { |
| 17650 | SOKOL_ASSERT(_sg.valid); |
| 17651 | sg_sampler res = _sg_alloc_sampler(); |
| 17652 | _SG_TRACE_ARGS(alloc_sampler, res); |
| 17653 | return res; |
| 17654 | } |
| 17655 | |
| 17656 | SOKOL_API_IMPL sg_shader sg_alloc_shader(void) { |
| 17657 | SOKOL_ASSERT(_sg.valid); |
| 17658 | sg_shader res = _sg_alloc_shader(); |
| 17659 | _SG_TRACE_ARGS(alloc_shader, res); |
| 17660 | return res; |
| 17661 | } |
| 17662 | |
| 17663 | SOKOL_API_IMPL sg_pipeline sg_alloc_pipeline(void) { |
| 17664 | SOKOL_ASSERT(_sg.valid); |
| 17665 | sg_pipeline res = _sg_alloc_pipeline(); |
| 17666 | _SG_TRACE_ARGS(alloc_pipeline, res); |
| 17667 | return res; |
| 17668 | } |
| 17669 | |
| 17670 | SOKOL_API_IMPL sg_attachments sg_alloc_attachments(void) { |
| 17671 | SOKOL_ASSERT(_sg.valid); |
| 17672 | sg_attachments res = _sg_alloc_attachments(); |
| 17673 | _SG_TRACE_ARGS(alloc_attachments, res); |
| 17674 | return res; |
| 17675 | } |
| 17676 | |
| 17677 | SOKOL_API_IMPL void sg_dealloc_buffer(sg_buffer buf_id) { |
| 17678 | SOKOL_ASSERT(_sg.valid); |
| 17679 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 17680 | if (buf) { |
| 17681 | if (buf->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17682 | _sg_dealloc_buffer(buf); |
| 17683 | } else { |
| 17684 | _SG_ERROR(DEALLOC_BUFFER_INVALID_STATE); |
| 17685 | } |
| 17686 | } |
| 17687 | _SG_TRACE_ARGS(dealloc_buffer, buf_id); |
| 17688 | } |
| 17689 | |
| 17690 | SOKOL_API_IMPL void sg_dealloc_image(sg_image img_id) { |
| 17691 | SOKOL_ASSERT(_sg.valid); |
| 17692 | _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 17693 | if (img) { |
| 17694 | if (img->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17695 | _sg_dealloc_image(img); |
| 17696 | } else { |
| 17697 | _SG_ERROR(DEALLOC_IMAGE_INVALID_STATE); |
| 17698 | } |
| 17699 | } |
| 17700 | _SG_TRACE_ARGS(dealloc_image, img_id); |
| 17701 | } |
| 17702 | |
| 17703 | SOKOL_API_IMPL void sg_dealloc_sampler(sg_sampler smp_id) { |
| 17704 | SOKOL_ASSERT(_sg.valid); |
| 17705 | _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id); |
| 17706 | if (smp) { |
| 17707 | if (smp->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17708 | _sg_dealloc_sampler(smp); |
| 17709 | } else { |
| 17710 | _SG_ERROR(DEALLOC_SAMPLER_INVALID_STATE); |
| 17711 | } |
| 17712 | } |
| 17713 | _SG_TRACE_ARGS(dealloc_sampler, smp_id); |
| 17714 | } |
| 17715 | |
| 17716 | SOKOL_API_IMPL void sg_dealloc_shader(sg_shader shd_id) { |
| 17717 | SOKOL_ASSERT(_sg.valid); |
| 17718 | _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 17719 | if (shd) { |
| 17720 | if (shd->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17721 | _sg_dealloc_shader(shd); |
| 17722 | } else { |
| 17723 | _SG_ERROR(DEALLOC_SHADER_INVALID_STATE); |
| 17724 | } |
| 17725 | } |
| 17726 | _SG_TRACE_ARGS(dealloc_shader, shd_id); |
| 17727 | } |
| 17728 | |
| 17729 | SOKOL_API_IMPL void sg_dealloc_pipeline(sg_pipeline pip_id) { |
| 17730 | SOKOL_ASSERT(_sg.valid); |
| 17731 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 17732 | if (pip) { |
| 17733 | if (pip->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17734 | _sg_dealloc_pipeline(pip); |
| 17735 | } else { |
| 17736 | _SG_ERROR(DEALLOC_PIPELINE_INVALID_STATE); |
| 17737 | } |
| 17738 | } |
| 17739 | _SG_TRACE_ARGS(dealloc_pipeline, pip_id); |
| 17740 | } |
| 17741 | |
| 17742 | SOKOL_API_IMPL void sg_dealloc_attachments(sg_attachments atts_id) { |
| 17743 | SOKOL_ASSERT(_sg.valid); |
| 17744 | _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id); |
| 17745 | if (atts) { |
| 17746 | if (atts->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17747 | _sg_dealloc_attachments(atts); |
| 17748 | } else { |
| 17749 | _SG_ERROR(DEALLOC_ATTACHMENTS_INVALID_STATE); |
| 17750 | } |
| 17751 | } |
| 17752 | _SG_TRACE_ARGS(dealloc_attachments, atts_id); |
| 17753 | } |
| 17754 | |
| 17755 | SOKOL_API_IMPL void sg_init_buffer(sg_buffer buf_id, const sg_buffer_desc* desc) { |
| 17756 | SOKOL_ASSERT(_sg.valid); |
| 17757 | sg_buffer_desc desc_def = _sg_buffer_desc_defaults(desc); |
| 17758 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 17759 | if (buf) { |
| 17760 | if (buf->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17761 | _sg_init_buffer(buf, &desc_def); |
| 17762 | SOKOL_ASSERT((buf->slot.state == SG_RESOURCESTATE_VALID) || (buf->slot.state == SG_RESOURCESTATE_FAILED)); |
| 17763 | } else { |
| 17764 | _SG_ERROR(INIT_BUFFER_INVALID_STATE); |
| 17765 | } |
| 17766 | } |
| 17767 | _SG_TRACE_ARGS(init_buffer, buf_id, &desc_def); |
| 17768 | } |
| 17769 | |
| 17770 | SOKOL_API_IMPL void sg_init_image(sg_image img_id, const sg_image_desc* desc) { |
| 17771 | SOKOL_ASSERT(_sg.valid); |
| 17772 | sg_image_desc desc_def = _sg_image_desc_defaults(desc); |
| 17773 | _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 17774 | if (img) { |
| 17775 | if (img->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17776 | _sg_init_image(img, &desc_def); |
| 17777 | SOKOL_ASSERT((img->slot.state == SG_RESOURCESTATE_VALID) || (img->slot.state == SG_RESOURCESTATE_FAILED)); |
| 17778 | } else { |
| 17779 | _SG_ERROR(INIT_IMAGE_INVALID_STATE); |
| 17780 | } |
| 17781 | } |
| 17782 | _SG_TRACE_ARGS(init_image, img_id, &desc_def); |
| 17783 | } |
| 17784 | |
| 17785 | SOKOL_API_IMPL void sg_init_sampler(sg_sampler smp_id, const sg_sampler_desc* desc) { |
| 17786 | SOKOL_ASSERT(_sg.valid); |
| 17787 | sg_sampler_desc desc_def = _sg_sampler_desc_defaults(desc); |
| 17788 | _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id); |
| 17789 | if (smp) { |
| 17790 | if (smp->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17791 | _sg_init_sampler(smp, &desc_def); |
| 17792 | SOKOL_ASSERT((smp->slot.state == SG_RESOURCESTATE_VALID) || (smp->slot.state == SG_RESOURCESTATE_FAILED)); |
| 17793 | } else { |
| 17794 | _SG_ERROR(INIT_SAMPLER_INVALID_STATE); |
| 17795 | } |
| 17796 | } |
| 17797 | _SG_TRACE_ARGS(init_sampler, smp_id, &desc_def); |
| 17798 | } |
| 17799 | |
| 17800 | SOKOL_API_IMPL void sg_init_shader(sg_shader shd_id, const sg_shader_desc* desc) { |
| 17801 | SOKOL_ASSERT(_sg.valid); |
| 17802 | sg_shader_desc desc_def = _sg_shader_desc_defaults(desc); |
| 17803 | _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 17804 | if (shd) { |
| 17805 | if (shd->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17806 | _sg_init_shader(shd, &desc_def); |
| 17807 | SOKOL_ASSERT((shd->slot.state == SG_RESOURCESTATE_VALID) || (shd->slot.state == SG_RESOURCESTATE_FAILED)); |
| 17808 | } else { |
| 17809 | _SG_ERROR(INIT_SHADER_INVALID_STATE); |
| 17810 | } |
| 17811 | } |
| 17812 | _SG_TRACE_ARGS(init_shader, shd_id, &desc_def); |
| 17813 | } |
| 17814 | |
| 17815 | SOKOL_API_IMPL void sg_init_pipeline(sg_pipeline pip_id, const sg_pipeline_desc* desc) { |
| 17816 | SOKOL_ASSERT(_sg.valid); |
| 17817 | sg_pipeline_desc desc_def = _sg_pipeline_desc_defaults(desc); |
| 17818 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 17819 | if (pip) { |
| 17820 | if (pip->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17821 | _sg_init_pipeline(pip, &desc_def); |
| 17822 | SOKOL_ASSERT((pip->slot.state == SG_RESOURCESTATE_VALID) || (pip->slot.state == SG_RESOURCESTATE_FAILED)); |
| 17823 | } else { |
| 17824 | _SG_ERROR(INIT_PIPELINE_INVALID_STATE); |
| 17825 | } |
| 17826 | } |
| 17827 | _SG_TRACE_ARGS(init_pipeline, pip_id, &desc_def); |
| 17828 | } |
| 17829 | |
| 17830 | SOKOL_API_IMPL void sg_init_attachments(sg_attachments atts_id, const sg_attachments_desc* desc) { |
| 17831 | SOKOL_ASSERT(_sg.valid); |
| 17832 | sg_attachments_desc desc_def = _sg_attachments_desc_defaults(desc); |
| 17833 | _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id); |
| 17834 | if (atts) { |
| 17835 | if (atts->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17836 | _sg_init_attachments(atts, &desc_def); |
| 17837 | SOKOL_ASSERT((atts->slot.state == SG_RESOURCESTATE_VALID) || (atts->slot.state == SG_RESOURCESTATE_FAILED)); |
| 17838 | } else { |
| 17839 | _SG_ERROR(INIT_ATTACHMENTS_INVALID_STATE); |
| 17840 | } |
| 17841 | } |
| 17842 | _SG_TRACE_ARGS(init_attachments, atts_id, &desc_def); |
| 17843 | } |
| 17844 | |
| 17845 | SOKOL_API_IMPL void sg_uninit_buffer(sg_buffer buf_id) { |
| 17846 | SOKOL_ASSERT(_sg.valid); |
| 17847 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 17848 | if (buf) { |
| 17849 | if ((buf->slot.state == SG_RESOURCESTATE_VALID) || (buf->slot.state == SG_RESOURCESTATE_FAILED)) { |
| 17850 | _sg_uninit_buffer(buf); |
| 17851 | SOKOL_ASSERT(buf->slot.state == SG_RESOURCESTATE_ALLOC); |
| 17852 | } else { |
| 17853 | _SG_ERROR(UNINIT_BUFFER_INVALID_STATE); |
| 17854 | } |
| 17855 | } |
| 17856 | _SG_TRACE_ARGS(uninit_buffer, buf_id); |
| 17857 | } |
| 17858 | |
| 17859 | SOKOL_API_IMPL void sg_uninit_image(sg_image img_id) { |
| 17860 | SOKOL_ASSERT(_sg.valid); |
| 17861 | _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 17862 | if (img) { |
| 17863 | if ((img->slot.state == SG_RESOURCESTATE_VALID) || (img->slot.state == SG_RESOURCESTATE_FAILED)) { |
| 17864 | _sg_uninit_image(img); |
| 17865 | SOKOL_ASSERT(img->slot.state == SG_RESOURCESTATE_ALLOC); |
| 17866 | } else { |
| 17867 | _SG_ERROR(UNINIT_IMAGE_INVALID_STATE); |
| 17868 | } |
| 17869 | } |
| 17870 | _SG_TRACE_ARGS(uninit_image, img_id); |
| 17871 | } |
| 17872 | |
| 17873 | SOKOL_API_IMPL void sg_uninit_sampler(sg_sampler smp_id) { |
| 17874 | SOKOL_ASSERT(_sg.valid); |
| 17875 | _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id); |
| 17876 | if (smp) { |
| 17877 | if ((smp->slot.state == SG_RESOURCESTATE_VALID) || (smp->slot.state == SG_RESOURCESTATE_FAILED)) { |
| 17878 | _sg_uninit_sampler(smp); |
| 17879 | SOKOL_ASSERT(smp->slot.state == SG_RESOURCESTATE_ALLOC); |
| 17880 | } else { |
| 17881 | _SG_ERROR(UNINIT_SAMPLER_INVALID_STATE); |
| 17882 | } |
| 17883 | } |
| 17884 | _SG_TRACE_ARGS(uninit_sampler, smp_id); |
| 17885 | } |
| 17886 | |
| 17887 | SOKOL_API_IMPL void sg_uninit_shader(sg_shader shd_id) { |
| 17888 | SOKOL_ASSERT(_sg.valid); |
| 17889 | _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 17890 | if (shd) { |
| 17891 | if ((shd->slot.state == SG_RESOURCESTATE_VALID) || (shd->slot.state == SG_RESOURCESTATE_FAILED)) { |
| 17892 | _sg_uninit_shader(shd); |
| 17893 | SOKOL_ASSERT(shd->slot.state == SG_RESOURCESTATE_ALLOC); |
| 17894 | } else { |
| 17895 | _SG_ERROR(UNINIT_SHADER_INVALID_STATE); |
| 17896 | } |
| 17897 | } |
| 17898 | _SG_TRACE_ARGS(uninit_shader, shd_id); |
| 17899 | } |
| 17900 | |
| 17901 | SOKOL_API_IMPL void sg_uninit_pipeline(sg_pipeline pip_id) { |
| 17902 | SOKOL_ASSERT(_sg.valid); |
| 17903 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 17904 | if (pip) { |
| 17905 | if ((pip->slot.state == SG_RESOURCESTATE_VALID) || (pip->slot.state == SG_RESOURCESTATE_FAILED)) { |
| 17906 | _sg_uninit_pipeline(pip); |
| 17907 | SOKOL_ASSERT(pip->slot.state == SG_RESOURCESTATE_ALLOC); |
| 17908 | } else { |
| 17909 | _SG_ERROR(UNINIT_PIPELINE_INVALID_STATE); |
| 17910 | } |
| 17911 | } |
| 17912 | _SG_TRACE_ARGS(uninit_pipeline, pip_id); |
| 17913 | } |
| 17914 | |
| 17915 | SOKOL_API_IMPL void sg_uninit_attachments(sg_attachments atts_id) { |
| 17916 | SOKOL_ASSERT(_sg.valid); |
| 17917 | _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id); |
| 17918 | if (atts) { |
| 17919 | if ((atts->slot.state == SG_RESOURCESTATE_VALID) || (atts->slot.state == SG_RESOURCESTATE_FAILED)) { |
| 17920 | _sg_uninit_attachments(atts); |
| 17921 | SOKOL_ASSERT(atts->slot.state == SG_RESOURCESTATE_ALLOC); |
| 17922 | } else { |
| 17923 | _SG_ERROR(UNINIT_ATTACHMENTS_INVALID_STATE); |
| 17924 | } |
| 17925 | } |
| 17926 | _SG_TRACE_ARGS(uninit_attachments, atts_id); |
| 17927 | } |
| 17928 | |
| 17929 | SOKOL_API_IMPL void sg_fail_buffer(sg_buffer buf_id) { |
| 17930 | SOKOL_ASSERT(_sg.valid); |
| 17931 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 17932 | if (buf) { |
| 17933 | if (buf->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17934 | buf->slot.state = SG_RESOURCESTATE_FAILED; |
| 17935 | } else { |
| 17936 | _SG_ERROR(FAIL_BUFFER_INVALID_STATE); |
| 17937 | } |
| 17938 | } |
| 17939 | _SG_TRACE_ARGS(fail_buffer, buf_id); |
| 17940 | } |
| 17941 | |
| 17942 | SOKOL_API_IMPL void sg_fail_image(sg_image img_id) { |
| 17943 | SOKOL_ASSERT(_sg.valid); |
| 17944 | _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 17945 | if (img) { |
| 17946 | if (img->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17947 | img->slot.state = SG_RESOURCESTATE_FAILED; |
| 17948 | } else { |
| 17949 | _SG_ERROR(FAIL_IMAGE_INVALID_STATE); |
| 17950 | } |
| 17951 | } |
| 17952 | _SG_TRACE_ARGS(fail_image, img_id); |
| 17953 | } |
| 17954 | |
| 17955 | SOKOL_API_IMPL void sg_fail_sampler(sg_sampler smp_id) { |
| 17956 | SOKOL_ASSERT(_sg.valid); |
| 17957 | _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id); |
| 17958 | if (smp) { |
| 17959 | if (smp->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17960 | smp->slot.state = SG_RESOURCESTATE_FAILED; |
| 17961 | } else { |
| 17962 | _SG_ERROR(FAIL_SAMPLER_INVALID_STATE); |
| 17963 | } |
| 17964 | } |
| 17965 | _SG_TRACE_ARGS(fail_sampler, smp_id); |
| 17966 | } |
| 17967 | |
| 17968 | SOKOL_API_IMPL void sg_fail_shader(sg_shader shd_id) { |
| 17969 | SOKOL_ASSERT(_sg.valid); |
| 17970 | _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 17971 | if (shd) { |
| 17972 | if (shd->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17973 | shd->slot.state = SG_RESOURCESTATE_FAILED; |
| 17974 | } else { |
| 17975 | _SG_ERROR(FAIL_SHADER_INVALID_STATE); |
| 17976 | } |
| 17977 | } |
| 17978 | _SG_TRACE_ARGS(fail_shader, shd_id); |
| 17979 | } |
| 17980 | |
| 17981 | SOKOL_API_IMPL void sg_fail_pipeline(sg_pipeline pip_id) { |
| 17982 | SOKOL_ASSERT(_sg.valid); |
| 17983 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 17984 | if (pip) { |
| 17985 | if (pip->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17986 | pip->slot.state = SG_RESOURCESTATE_FAILED; |
| 17987 | } else { |
| 17988 | _SG_ERROR(FAIL_PIPELINE_INVALID_STATE); |
| 17989 | } |
| 17990 | } |
| 17991 | _SG_TRACE_ARGS(fail_pipeline, pip_id); |
| 17992 | } |
| 17993 | |
| 17994 | SOKOL_API_IMPL void sg_fail_attachments(sg_attachments atts_id) { |
| 17995 | SOKOL_ASSERT(_sg.valid); |
| 17996 | _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id); |
| 17997 | if (atts) { |
| 17998 | if (atts->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 17999 | atts->slot.state = SG_RESOURCESTATE_FAILED; |
| 18000 | } else { |
| 18001 | _SG_ERROR(FAIL_ATTACHMENTS_INVALID_STATE); |
| 18002 | } |
| 18003 | } |
| 18004 | _SG_TRACE_ARGS(fail_attachments, atts_id); |
| 18005 | } |
| 18006 | |
| 18007 | SOKOL_API_IMPL sg_resource_state sg_query_buffer_state(sg_buffer buf_id) { |
| 18008 | SOKOL_ASSERT(_sg.valid); |
| 18009 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 18010 | sg_resource_state res = buf ? buf->slot.state : SG_RESOURCESTATE_INVALID; |
| 18011 | return res; |
| 18012 | } |
| 18013 | |
| 18014 | SOKOL_API_IMPL sg_resource_state sg_query_image_state(sg_image img_id) { |
| 18015 | SOKOL_ASSERT(_sg.valid); |
| 18016 | _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 18017 | sg_resource_state res = img ? img->slot.state : SG_RESOURCESTATE_INVALID; |
| 18018 | return res; |
| 18019 | } |
| 18020 | |
| 18021 | SOKOL_API_IMPL sg_resource_state sg_query_sampler_state(sg_sampler smp_id) { |
| 18022 | SOKOL_ASSERT(_sg.valid); |
| 18023 | _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id); |
| 18024 | sg_resource_state res = smp ? smp->slot.state : SG_RESOURCESTATE_INVALID; |
| 18025 | return res; |
| 18026 | } |
| 18027 | |
| 18028 | SOKOL_API_IMPL sg_resource_state sg_query_shader_state(sg_shader shd_id) { |
| 18029 | SOKOL_ASSERT(_sg.valid); |
| 18030 | _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 18031 | sg_resource_state res = shd ? shd->slot.state : SG_RESOURCESTATE_INVALID; |
| 18032 | return res; |
| 18033 | } |
| 18034 | |
| 18035 | SOKOL_API_IMPL sg_resource_state sg_query_pipeline_state(sg_pipeline pip_id) { |
| 18036 | SOKOL_ASSERT(_sg.valid); |
| 18037 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 18038 | sg_resource_state res = pip ? pip->slot.state : SG_RESOURCESTATE_INVALID; |
| 18039 | return res; |
| 18040 | } |
| 18041 | |
| 18042 | SOKOL_API_IMPL sg_resource_state sg_query_attachments_state(sg_attachments atts_id) { |
| 18043 | SOKOL_ASSERT(_sg.valid); |
| 18044 | _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id); |
| 18045 | sg_resource_state res = atts ? atts->slot.state : SG_RESOURCESTATE_INVALID; |
| 18046 | return res; |
| 18047 | } |
| 18048 | |
| 18049 | SOKOL_API_IMPL sg_buffer sg_make_buffer(const sg_buffer_desc* desc) { |
| 18050 | SOKOL_ASSERT(_sg.valid); |
| 18051 | SOKOL_ASSERT(desc); |
| 18052 | sg_buffer_desc desc_def = _sg_buffer_desc_defaults(desc); |
| 18053 | sg_buffer buf_id = _sg_alloc_buffer(); |
| 18054 | if (buf_id.id != SG_INVALID_ID) { |
| 18055 | _sg_buffer_t* buf = _sg_buffer_at(&_sg.pools, buf_id.id); |
| 18056 | SOKOL_ASSERT(buf && (buf->slot.state == SG_RESOURCESTATE_ALLOC)); |
| 18057 | _sg_init_buffer(buf, &desc_def); |
| 18058 | SOKOL_ASSERT((buf->slot.state == SG_RESOURCESTATE_VALID) || (buf->slot.state == SG_RESOURCESTATE_FAILED)); |
| 18059 | } |
| 18060 | _SG_TRACE_ARGS(make_buffer, &desc_def, buf_id); |
| 18061 | return buf_id; |
| 18062 | } |
| 18063 | |
| 18064 | SOKOL_API_IMPL sg_image sg_make_image(const sg_image_desc* desc) { |
| 18065 | SOKOL_ASSERT(_sg.valid); |
| 18066 | SOKOL_ASSERT(desc); |
| 18067 | sg_image_desc desc_def = _sg_image_desc_defaults(desc); |
| 18068 | sg_image img_id = _sg_alloc_image(); |
| 18069 | if (img_id.id != SG_INVALID_ID) { |
| 18070 | _sg_image_t* img = _sg_image_at(&_sg.pools, img_id.id); |
| 18071 | SOKOL_ASSERT(img && (img->slot.state == SG_RESOURCESTATE_ALLOC)); |
| 18072 | _sg_init_image(img, &desc_def); |
| 18073 | SOKOL_ASSERT((img->slot.state == SG_RESOURCESTATE_VALID) || (img->slot.state == SG_RESOURCESTATE_FAILED)); |
| 18074 | } |
| 18075 | _SG_TRACE_ARGS(make_image, &desc_def, img_id); |
| 18076 | return img_id; |
| 18077 | } |
| 18078 | |
| 18079 | SOKOL_API_IMPL sg_sampler sg_make_sampler(const sg_sampler_desc* desc) { |
| 18080 | SOKOL_ASSERT(_sg.valid); |
| 18081 | SOKOL_ASSERT(desc); |
| 18082 | sg_sampler_desc desc_def = _sg_sampler_desc_defaults(desc); |
| 18083 | sg_sampler smp_id = _sg_alloc_sampler(); |
| 18084 | if (smp_id.id != SG_INVALID_ID) { |
| 18085 | _sg_sampler_t* smp = _sg_sampler_at(&_sg.pools, smp_id.id); |
| 18086 | SOKOL_ASSERT(smp && (smp->slot.state == SG_RESOURCESTATE_ALLOC)); |
| 18087 | _sg_init_sampler(smp, &desc_def); |
| 18088 | SOKOL_ASSERT((smp->slot.state == SG_RESOURCESTATE_VALID) || (smp->slot.state == SG_RESOURCESTATE_FAILED)); |
| 18089 | } |
| 18090 | _SG_TRACE_ARGS(make_sampler, &desc_def, smp_id); |
| 18091 | return smp_id; |
| 18092 | } |
| 18093 | |
| 18094 | SOKOL_API_IMPL sg_shader sg_make_shader(const sg_shader_desc* desc) { |
| 18095 | SOKOL_ASSERT(_sg.valid); |
| 18096 | SOKOL_ASSERT(desc); |
| 18097 | sg_shader_desc desc_def = _sg_shader_desc_defaults(desc); |
| 18098 | sg_shader shd_id = _sg_alloc_shader(); |
| 18099 | if (shd_id.id != SG_INVALID_ID) { |
| 18100 | _sg_shader_t* shd = _sg_shader_at(&_sg.pools, shd_id.id); |
| 18101 | SOKOL_ASSERT(shd && (shd->slot.state == SG_RESOURCESTATE_ALLOC)); |
| 18102 | _sg_init_shader(shd, &desc_def); |
| 18103 | SOKOL_ASSERT((shd->slot.state == SG_RESOURCESTATE_VALID) || (shd->slot.state == SG_RESOURCESTATE_FAILED)); |
| 18104 | } |
| 18105 | _SG_TRACE_ARGS(make_shader, &desc_def, shd_id); |
| 18106 | return shd_id; |
| 18107 | } |
| 18108 | |
| 18109 | SOKOL_API_IMPL sg_pipeline sg_make_pipeline(const sg_pipeline_desc* desc) { |
| 18110 | SOKOL_ASSERT(_sg.valid); |
| 18111 | SOKOL_ASSERT(desc); |
| 18112 | sg_pipeline_desc desc_def = _sg_pipeline_desc_defaults(desc); |
| 18113 | sg_pipeline pip_id = _sg_alloc_pipeline(); |
| 18114 | if (pip_id.id != SG_INVALID_ID) { |
| 18115 | _sg_pipeline_t* pip = _sg_pipeline_at(&_sg.pools, pip_id.id); |
| 18116 | SOKOL_ASSERT(pip && (pip->slot.state == SG_RESOURCESTATE_ALLOC)); |
| 18117 | _sg_init_pipeline(pip, &desc_def); |
| 18118 | SOKOL_ASSERT((pip->slot.state == SG_RESOURCESTATE_VALID) || (pip->slot.state == SG_RESOURCESTATE_FAILED)); |
| 18119 | } |
| 18120 | _SG_TRACE_ARGS(make_pipeline, &desc_def, pip_id); |
| 18121 | return pip_id; |
| 18122 | } |
| 18123 | |
| 18124 | SOKOL_API_IMPL sg_attachments sg_make_attachments(const sg_attachments_desc* desc) { |
| 18125 | SOKOL_ASSERT(_sg.valid); |
| 18126 | SOKOL_ASSERT(desc); |
| 18127 | sg_attachments_desc desc_def = _sg_attachments_desc_defaults(desc); |
| 18128 | sg_attachments atts_id = _sg_alloc_attachments(); |
| 18129 | if (atts_id.id != SG_INVALID_ID) { |
| 18130 | _sg_attachments_t* atts = _sg_attachments_at(&_sg.pools, atts_id.id); |
| 18131 | SOKOL_ASSERT(atts && (atts->slot.state == SG_RESOURCESTATE_ALLOC)); |
| 18132 | _sg_init_attachments(atts, &desc_def); |
| 18133 | SOKOL_ASSERT((atts->slot.state == SG_RESOURCESTATE_VALID) || (atts->slot.state == SG_RESOURCESTATE_FAILED)); |
| 18134 | } |
| 18135 | _SG_TRACE_ARGS(make_attachments, &desc_def, atts_id); |
| 18136 | return atts_id; |
| 18137 | } |
| 18138 | |
| 18139 | SOKOL_API_IMPL void sg_destroy_buffer(sg_buffer buf_id) { |
| 18140 | SOKOL_ASSERT(_sg.valid); |
| 18141 | _SG_TRACE_ARGS(destroy_buffer, buf_id); |
| 18142 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 18143 | if (buf) { |
| 18144 | if ((buf->slot.state == SG_RESOURCESTATE_VALID) || (buf->slot.state == SG_RESOURCESTATE_FAILED)) { |
| 18145 | _sg_uninit_buffer(buf); |
| 18146 | SOKOL_ASSERT(buf->slot.state == SG_RESOURCESTATE_ALLOC); |
| 18147 | } |
| 18148 | if (buf->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 18149 | _sg_dealloc_buffer(buf); |
| 18150 | SOKOL_ASSERT(buf->slot.state == SG_RESOURCESTATE_INITIAL); |
| 18151 | } |
| 18152 | } |
| 18153 | } |
| 18154 | |
| 18155 | SOKOL_API_IMPL void sg_destroy_image(sg_image img_id) { |
| 18156 | SOKOL_ASSERT(_sg.valid); |
| 18157 | _SG_TRACE_ARGS(destroy_image, img_id); |
| 18158 | _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 18159 | if (img) { |
| 18160 | if ((img->slot.state == SG_RESOURCESTATE_VALID) || (img->slot.state == SG_RESOURCESTATE_FAILED)) { |
| 18161 | _sg_uninit_image(img); |
| 18162 | SOKOL_ASSERT(img->slot.state == SG_RESOURCESTATE_ALLOC); |
| 18163 | } |
| 18164 | if (img->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 18165 | _sg_dealloc_image(img); |
| 18166 | SOKOL_ASSERT(img->slot.state == SG_RESOURCESTATE_INITIAL); |
| 18167 | } |
| 18168 | } |
| 18169 | } |
| 18170 | |
| 18171 | SOKOL_API_IMPL void sg_destroy_sampler(sg_sampler smp_id) { |
| 18172 | SOKOL_ASSERT(_sg.valid); |
| 18173 | _SG_TRACE_ARGS(destroy_sampler, smp_id); |
| 18174 | _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id); |
| 18175 | if (smp) { |
| 18176 | if ((smp->slot.state == SG_RESOURCESTATE_VALID) || (smp->slot.state == SG_RESOURCESTATE_FAILED)) { |
| 18177 | _sg_uninit_sampler(smp); |
| 18178 | SOKOL_ASSERT(smp->slot.state == SG_RESOURCESTATE_ALLOC); |
| 18179 | } |
| 18180 | if (smp->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 18181 | _sg_dealloc_sampler(smp); |
| 18182 | SOKOL_ASSERT(smp->slot.state == SG_RESOURCESTATE_INITIAL); |
| 18183 | } |
| 18184 | } |
| 18185 | } |
| 18186 | |
| 18187 | SOKOL_API_IMPL void sg_destroy_shader(sg_shader shd_id) { |
| 18188 | SOKOL_ASSERT(_sg.valid); |
| 18189 | _SG_TRACE_ARGS(destroy_shader, shd_id); |
| 18190 | _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 18191 | if (shd) { |
| 18192 | if ((shd->slot.state == SG_RESOURCESTATE_VALID) || (shd->slot.state == SG_RESOURCESTATE_FAILED)) { |
| 18193 | _sg_uninit_shader(shd); |
| 18194 | SOKOL_ASSERT(shd->slot.state == SG_RESOURCESTATE_ALLOC); |
| 18195 | } |
| 18196 | if (shd->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 18197 | _sg_dealloc_shader(shd); |
| 18198 | SOKOL_ASSERT(shd->slot.state == SG_RESOURCESTATE_INITIAL); |
| 18199 | } |
| 18200 | } |
| 18201 | } |
| 18202 | |
| 18203 | SOKOL_API_IMPL void sg_destroy_pipeline(sg_pipeline pip_id) { |
| 18204 | SOKOL_ASSERT(_sg.valid); |
| 18205 | _SG_TRACE_ARGS(destroy_pipeline, pip_id); |
| 18206 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 18207 | if (pip) { |
| 18208 | if ((pip->slot.state == SG_RESOURCESTATE_VALID) || (pip->slot.state == SG_RESOURCESTATE_FAILED)) { |
| 18209 | _sg_uninit_pipeline(pip); |
| 18210 | SOKOL_ASSERT(pip->slot.state == SG_RESOURCESTATE_ALLOC); |
| 18211 | } |
| 18212 | if (pip->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 18213 | _sg_dealloc_pipeline(pip); |
| 18214 | SOKOL_ASSERT(pip->slot.state == SG_RESOURCESTATE_INITIAL); |
| 18215 | } |
| 18216 | } |
| 18217 | } |
| 18218 | |
| 18219 | SOKOL_API_IMPL void sg_destroy_attachments(sg_attachments atts_id) { |
| 18220 | SOKOL_ASSERT(_sg.valid); |
| 18221 | _SG_TRACE_ARGS(destroy_attachments, atts_id); |
| 18222 | _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id); |
| 18223 | if (atts) { |
| 18224 | if ((atts->slot.state == SG_RESOURCESTATE_VALID) || (atts->slot.state == SG_RESOURCESTATE_FAILED)) { |
| 18225 | _sg_uninit_attachments(atts); |
| 18226 | SOKOL_ASSERT(atts->slot.state == SG_RESOURCESTATE_ALLOC); |
| 18227 | } |
| 18228 | if (atts->slot.state == SG_RESOURCESTATE_ALLOC) { |
| 18229 | _sg_dealloc_attachments(atts); |
| 18230 | SOKOL_ASSERT(atts->slot.state == SG_RESOURCESTATE_INITIAL); |
| 18231 | } |
| 18232 | } |
| 18233 | } |
| 18234 | |
| 18235 | SOKOL_API_IMPL void sg_begin_pass(const sg_pass* pass) { |
| 18236 | SOKOL_ASSERT(_sg.valid); |
| 18237 | SOKOL_ASSERT(!_sg.cur_pass.valid); |
| 18238 | SOKOL_ASSERT(!_sg.cur_pass.in_pass); |
| 18239 | SOKOL_ASSERT(pass); |
| 18240 | SOKOL_ASSERT((pass->_start_canary == 0) && (pass->_end_canary == 0)); |
| 18241 | const sg_pass pass_def = _sg_pass_defaults(pass); |
| 18242 | if (!_sg_validate_begin_pass(&pass_def)) { |
| 18243 | return; |
| 18244 | } |
| 18245 | if (pass_def.attachments.id != SG_INVALID_ID) { |
| 18246 | // an offscreen pass |
| 18247 | SOKOL_ASSERT(_sg.cur_pass.atts == 0); |
| 18248 | _sg.cur_pass.atts = _sg_lookup_attachments(&_sg.pools, pass_def.attachments.id); |
| 18249 | if (0 == _sg.cur_pass.atts) { |
| 18250 | _SG_ERROR(BEGINPASS_ATTACHMENT_INVALID); |
| 18251 | return; |
| 18252 | } |
| 18253 | _sg.cur_pass.atts_id = pass_def.attachments; |
| 18254 | _sg.cur_pass.width = _sg.cur_pass.atts->cmn.width; |
| 18255 | _sg.cur_pass.height = _sg.cur_pass.atts->cmn.height; |
| 18256 | } else { |
| 18257 | // a swapchain pass |
| 18258 | SOKOL_ASSERT(pass_def.swapchain.width > 0); |
| 18259 | SOKOL_ASSERT(pass_def.swapchain.height > 0); |
| 18260 | SOKOL_ASSERT(pass_def.swapchain.color_format > SG_PIXELFORMAT_NONE); |
| 18261 | SOKOL_ASSERT(pass_def.swapchain.sample_count > 0); |
| 18262 | _sg.cur_pass.width = pass_def.swapchain.width; |
| 18263 | _sg.cur_pass.height = pass_def.swapchain.height; |
| 18264 | _sg.cur_pass.swapchain.color_fmt = pass_def.swapchain.color_format; |
| 18265 | _sg.cur_pass.swapchain.depth_fmt = pass_def.swapchain.depth_format; |
| 18266 | _sg.cur_pass.swapchain.sample_count = pass_def.swapchain.sample_count; |
| 18267 | } |
| 18268 | _sg.cur_pass.valid = true; // may be overruled by backend begin-pass functions |
| 18269 | _sg.cur_pass.in_pass = true; |
| 18270 | _sg_begin_pass(&pass_def); |
| 18271 | _SG_TRACE_ARGS(begin_pass, &pass_def); |
| 18272 | } |
| 18273 | |
| 18274 | SOKOL_API_IMPL void sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left) { |
| 18275 | SOKOL_ASSERT(_sg.valid); |
| 18276 | SOKOL_ASSERT(_sg.cur_pass.in_pass); |
| 18277 | _sg_stats_add(num_apply_viewport, 1); |
| 18278 | if (!_sg.cur_pass.valid) { |
| 18279 | return; |
| 18280 | } |
| 18281 | _sg_apply_viewport(x, y, width, height, origin_top_left); |
| 18282 | _SG_TRACE_ARGS(apply_viewport, x, y, width, height, origin_top_left); |
| 18283 | } |
| 18284 | |
| 18285 | SOKOL_API_IMPL void sg_apply_viewportf(float x, float y, float width, float height, bool origin_top_left) { |
| 18286 | sg_apply_viewport((int)x, (int)y, (int)width, (int)height, origin_top_left); |
| 18287 | } |
| 18288 | |
| 18289 | SOKOL_API_IMPL void sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left) { |
| 18290 | SOKOL_ASSERT(_sg.valid); |
| 18291 | SOKOL_ASSERT(_sg.cur_pass.in_pass); |
| 18292 | _sg_stats_add(num_apply_scissor_rect, 1); |
| 18293 | if (!_sg.cur_pass.valid) { |
| 18294 | return; |
| 18295 | } |
| 18296 | _sg_apply_scissor_rect(x, y, width, height, origin_top_left); |
| 18297 | _SG_TRACE_ARGS(apply_scissor_rect, x, y, width, height, origin_top_left); |
| 18298 | } |
| 18299 | |
| 18300 | SOKOL_API_IMPL void sg_apply_scissor_rectf(float x, float y, float width, float height, bool origin_top_left) { |
| 18301 | sg_apply_scissor_rect((int)x, (int)y, (int)width, (int)height, origin_top_left); |
| 18302 | } |
| 18303 | |
| 18304 | SOKOL_API_IMPL void sg_apply_pipeline(sg_pipeline pip_id) { |
| 18305 | SOKOL_ASSERT(_sg.valid); |
| 18306 | SOKOL_ASSERT(_sg.cur_pass.in_pass); |
| 18307 | _sg_stats_add(num_apply_pipeline, 1); |
| 18308 | if (!_sg_validate_apply_pipeline(pip_id)) { |
| 18309 | _sg.next_draw_valid = false; |
| 18310 | return; |
| 18311 | } |
| 18312 | if (!_sg.cur_pass.valid) { |
| 18313 | return; |
| 18314 | } |
| 18315 | _sg.cur_pipeline = pip_id; |
| 18316 | _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 18317 | SOKOL_ASSERT(pip); |
| 18318 | _sg.next_draw_valid = (SG_RESOURCESTATE_VALID == pip->slot.state); |
| 18319 | SOKOL_ASSERT(pip->shader && (pip->shader->slot.id == pip->cmn.shader_id.id)); |
| 18320 | _sg_apply_pipeline(pip); |
| 18321 | _SG_TRACE_ARGS(apply_pipeline, pip_id); |
| 18322 | } |
| 18323 | |
| 18324 | SOKOL_API_IMPL void sg_apply_bindings(const sg_bindings* bindings) { |
| 18325 | SOKOL_ASSERT(_sg.valid); |
| 18326 | SOKOL_ASSERT(_sg.cur_pass.in_pass); |
| 18327 | SOKOL_ASSERT(bindings); |
| 18328 | SOKOL_ASSERT((bindings->_start_canary == 0) && (bindings->_end_canary==0)); |
| 18329 | _sg_stats_add(num_apply_bindings, 1); |
| 18330 | if (!_sg_validate_apply_bindings(bindings)) { |
| 18331 | _sg.next_draw_valid = false; |
| 18332 | return; |
| 18333 | } |
| 18334 | if (!_sg.cur_pass.valid) { |
| 18335 | return; |
| 18336 | } |
| 18337 | |
| 18338 | _sg_bindings_t bnd; |
| 18339 | _sg_clear(&bnd, sizeof(bnd)); |
| 18340 | bnd.pip = _sg_lookup_pipeline(&_sg.pools, _sg.cur_pipeline.id); |
| 18341 | if (0 == bnd.pip) { |
| 18342 | _sg.next_draw_valid = false; |
| 18343 | } |
| 18344 | |
| 18345 | for (int i = 0; i < SG_MAX_VERTEX_BUFFERS; i++, bnd.num_vbs++) { |
| 18346 | if (bindings->vertex_buffers[i].id) { |
| 18347 | bnd.vbs[i] = _sg_lookup_buffer(&_sg.pools, bindings->vertex_buffers[i].id); |
| 18348 | bnd.vb_offsets[i] = bindings->vertex_buffer_offsets[i]; |
| 18349 | if (bnd.vbs[i]) { |
| 18350 | _sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.vbs[i]->slot.state); |
| 18351 | _sg.next_draw_valid &= !bnd.vbs[i]->cmn.append_overflow; |
| 18352 | } else { |
| 18353 | _sg.next_draw_valid = false; |
| 18354 | } |
| 18355 | } else { |
| 18356 | break; |
| 18357 | } |
| 18358 | } |
| 18359 | |
| 18360 | if (bindings->index_buffer.id) { |
| 18361 | bnd.ib = _sg_lookup_buffer(&_sg.pools, bindings->index_buffer.id); |
| 18362 | bnd.ib_offset = bindings->index_buffer_offset; |
| 18363 | if (bnd.ib) { |
| 18364 | _sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.ib->slot.state); |
| 18365 | _sg.next_draw_valid &= !bnd.ib->cmn.append_overflow; |
| 18366 | } else { |
| 18367 | _sg.next_draw_valid = false; |
| 18368 | } |
| 18369 | } |
| 18370 | |
| 18371 | for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++, bnd.num_vs_imgs++) { |
| 18372 | if (bindings->vs.images[i].id) { |
| 18373 | bnd.vs_imgs[i] = _sg_lookup_image(&_sg.pools, bindings->vs.images[i].id); |
| 18374 | if (bnd.vs_imgs[i]) { |
| 18375 | _sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.vs_imgs[i]->slot.state); |
| 18376 | } else { |
| 18377 | _sg.next_draw_valid = false; |
| 18378 | } |
| 18379 | } else { |
| 18380 | break; |
| 18381 | } |
| 18382 | } |
| 18383 | |
| 18384 | for (int i = 0; i < SG_MAX_SHADERSTAGE_SAMPLERS; i++, bnd.num_vs_smps++) { |
| 18385 | if (bindings->vs.samplers[i].id) { |
| 18386 | bnd.vs_smps[i] = _sg_lookup_sampler(&_sg.pools, bindings->vs.samplers[i].id); |
| 18387 | if (bnd.vs_smps[i]) { |
| 18388 | _sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.vs_smps[i]->slot.state); |
| 18389 | } else { |
| 18390 | _sg.next_draw_valid = false; |
| 18391 | } |
| 18392 | } else { |
| 18393 | break; |
| 18394 | } |
| 18395 | } |
| 18396 | |
| 18397 | for (int i = 0; i < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; i++, bnd.num_vs_sbufs++) { |
| 18398 | if (bindings->vs.storage_buffers[i].id) { |
| 18399 | bnd.vs_sbufs[i] = _sg_lookup_buffer(&_sg.pools, bindings->vs.storage_buffers[i].id); |
| 18400 | if (bnd.vs_sbufs[i]) { |
| 18401 | _sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.vs_sbufs[i]->slot.state); |
| 18402 | } else { |
| 18403 | _sg.next_draw_valid = false; |
| 18404 | } |
| 18405 | } else { |
| 18406 | break; |
| 18407 | } |
| 18408 | } |
| 18409 | |
| 18410 | for (int i = 0; i < SG_MAX_SHADERSTAGE_IMAGES; i++, bnd.num_fs_imgs++) { |
| 18411 | if (bindings->fs.images[i].id) { |
| 18412 | bnd.fs_imgs[i] = _sg_lookup_image(&_sg.pools, bindings->fs.images[i].id); |
| 18413 | if (bnd.fs_imgs[i]) { |
| 18414 | _sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.fs_imgs[i]->slot.state); |
| 18415 | } else { |
| 18416 | _sg.next_draw_valid = false; |
| 18417 | } |
| 18418 | } else { |
| 18419 | break; |
| 18420 | } |
| 18421 | } |
| 18422 | |
| 18423 | for (int i = 0; i < SG_MAX_SHADERSTAGE_SAMPLERS; i++, bnd.num_fs_smps++) { |
| 18424 | if (bindings->fs.samplers[i].id) { |
| 18425 | bnd.fs_smps[i] = _sg_lookup_sampler(&_sg.pools, bindings->fs.samplers[i].id); |
| 18426 | if (bnd.fs_smps[i]) { |
| 18427 | _sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.fs_smps[i]->slot.state); |
| 18428 | } else { |
| 18429 | _sg.next_draw_valid = false; |
| 18430 | } |
| 18431 | } else { |
| 18432 | break; |
| 18433 | } |
| 18434 | } |
| 18435 | |
| 18436 | for (int i = 0; i < SG_MAX_SHADERSTAGE_STORAGEBUFFERS; i++, bnd.num_fs_sbufs++) { |
| 18437 | if (bindings->fs.storage_buffers[i].id) { |
| 18438 | bnd.fs_sbufs[i] = _sg_lookup_buffer(&_sg.pools, bindings->fs.storage_buffers[i].id); |
| 18439 | if (bnd.fs_sbufs[i]) { |
| 18440 | _sg.next_draw_valid &= (SG_RESOURCESTATE_VALID == bnd.fs_sbufs[i]->slot.state); |
| 18441 | } else { |
| 18442 | _sg.next_draw_valid = false; |
| 18443 | } |
| 18444 | } else { |
| 18445 | break; |
| 18446 | } |
| 18447 | } |
| 18448 | if (_sg.next_draw_valid) { |
| 18449 | _sg.next_draw_valid &= _sg_apply_bindings(&bnd); |
| 18450 | _SG_TRACE_ARGS(apply_bindings, bindings); |
| 18451 | } |
| 18452 | } |
| 18453 | |
| 18454 | SOKOL_API_IMPL void sg_apply_uniforms(sg_shader_stage stage, int ub_index, const sg_range* data) { |
| 18455 | SOKOL_ASSERT(_sg.valid); |
| 18456 | SOKOL_ASSERT(_sg.cur_pass.in_pass); |
| 18457 | SOKOL_ASSERT((stage == SG_SHADERSTAGE_VS) || (stage == SG_SHADERSTAGE_FS)); |
| 18458 | SOKOL_ASSERT((ub_index >= 0) && (ub_index < SG_MAX_SHADERSTAGE_UBS)); |
| 18459 | SOKOL_ASSERT(data && data->ptr && (data->size > 0)); |
| 18460 | _sg_stats_add(num_apply_uniforms, 1); |
| 18461 | _sg_stats_add(size_apply_uniforms, (uint32_t)data->size); |
| 18462 | if (!_sg_validate_apply_uniforms(stage, ub_index, data)) { |
| 18463 | _sg.next_draw_valid = false; |
| 18464 | return; |
| 18465 | } |
| 18466 | if (!_sg.cur_pass.valid) { |
| 18467 | return; |
| 18468 | } |
| 18469 | if (!_sg.next_draw_valid) { |
| 18470 | return; |
| 18471 | } |
| 18472 | _sg_apply_uniforms(stage, ub_index, data); |
| 18473 | _SG_TRACE_ARGS(apply_uniforms, stage, ub_index, data); |
| 18474 | } |
| 18475 | |
| 18476 | SOKOL_API_IMPL void sg_draw(int base_element, int num_elements, int num_instances) { |
| 18477 | SOKOL_ASSERT(_sg.valid); |
| 18478 | SOKOL_ASSERT(_sg.cur_pass.in_pass); |
| 18479 | SOKOL_ASSERT(base_element >= 0); |
| 18480 | SOKOL_ASSERT(num_elements >= 0); |
| 18481 | SOKOL_ASSERT(num_instances >= 0); |
| 18482 | _sg_stats_add(num_draw, 1); |
| 18483 | if (!_sg.cur_pass.valid) { |
| 18484 | return; |
| 18485 | } |
| 18486 | if (!_sg.next_draw_valid) { |
| 18487 | return; |
| 18488 | } |
| 18489 | /* attempting to draw with zero elements or instances is not technically an |
| 18490 | error, but might be handled as an error in the backend API (e.g. on Metal) |
| 18491 | */ |
| 18492 | if ((0 == num_elements) || (0 == num_instances)) { |
| 18493 | return; |
| 18494 | } |
| 18495 | _sg_draw(base_element, num_elements, num_instances); |
| 18496 | _SG_TRACE_ARGS(draw, base_element, num_elements, num_instances); |
| 18497 | } |
| 18498 | |
| 18499 | SOKOL_API_IMPL void sg_end_pass(void) { |
| 18500 | SOKOL_ASSERT(_sg.valid); |
| 18501 | SOKOL_ASSERT(_sg.cur_pass.in_pass); |
| 18502 | _sg_stats_add(num_passes, 1); |
| 18503 | // NOTE: don't exit early if !_sg.cur_pass.valid |
| 18504 | _sg_end_pass(); |
| 18505 | _sg.cur_pipeline.id = SG_INVALID_ID; |
| 18506 | _sg_clear(&_sg.cur_pass, sizeof(_sg.cur_pass)); |
| 18507 | _SG_TRACE_NOARGS(end_pass); |
| 18508 | } |
| 18509 | |
| 18510 | SOKOL_API_IMPL void sg_commit(void) { |
| 18511 | SOKOL_ASSERT(_sg.valid); |
| 18512 | SOKOL_ASSERT(!_sg.cur_pass.valid); |
| 18513 | SOKOL_ASSERT(!_sg.cur_pass.in_pass); |
| 18514 | _sg_commit(); |
| 18515 | _sg.stats.frame_index = _sg.frame_index; |
| 18516 | _sg.prev_stats = _sg.stats; |
| 18517 | _sg_clear(&_sg.stats, sizeof(_sg.stats)); |
| 18518 | _sg_notify_commit_listeners(); |
| 18519 | _SG_TRACE_NOARGS(commit); |
| 18520 | _sg.frame_index++; |
| 18521 | } |
| 18522 | |
| 18523 | SOKOL_API_IMPL void sg_reset_state_cache(void) { |
| 18524 | SOKOL_ASSERT(_sg.valid); |
| 18525 | _sg_reset_state_cache(); |
| 18526 | _SG_TRACE_NOARGS(reset_state_cache); |
| 18527 | } |
| 18528 | |
| 18529 | SOKOL_API_IMPL void sg_update_buffer(sg_buffer buf_id, const sg_range* data) { |
| 18530 | SOKOL_ASSERT(_sg.valid); |
| 18531 | SOKOL_ASSERT(data && data->ptr && (data->size > 0)); |
| 18532 | _sg_stats_add(num_update_buffer, 1); |
| 18533 | _sg_stats_add(size_update_buffer, (uint32_t)data->size); |
| 18534 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 18535 | if ((data->size > 0) && buf && (buf->slot.state == SG_RESOURCESTATE_VALID)) { |
| 18536 | if (_sg_validate_update_buffer(buf, data)) { |
| 18537 | SOKOL_ASSERT(data->size <= (size_t)buf->cmn.size); |
| 18538 | // only one update allowed per buffer and frame |
| 18539 | SOKOL_ASSERT(buf->cmn.update_frame_index != _sg.frame_index); |
| 18540 | // update and append on same buffer in same frame not allowed |
| 18541 | SOKOL_ASSERT(buf->cmn.append_frame_index != _sg.frame_index); |
| 18542 | _sg_update_buffer(buf, data); |
| 18543 | buf->cmn.update_frame_index = _sg.frame_index; |
| 18544 | } |
| 18545 | } |
| 18546 | _SG_TRACE_ARGS(update_buffer, buf_id, data); |
| 18547 | } |
| 18548 | |
| 18549 | SOKOL_API_IMPL int sg_append_buffer(sg_buffer buf_id, const sg_range* data) { |
| 18550 | SOKOL_ASSERT(_sg.valid); |
| 18551 | SOKOL_ASSERT(data && data->ptr); |
| 18552 | _sg_stats_add(num_append_buffer, 1); |
| 18553 | _sg_stats_add(size_append_buffer, (uint32_t)data->size); |
| 18554 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 18555 | int result; |
| 18556 | if (buf) { |
| 18557 | // rewind append cursor in a new frame |
| 18558 | if (buf->cmn.append_frame_index != _sg.frame_index) { |
| 18559 | buf->cmn.append_pos = 0; |
| 18560 | buf->cmn.append_overflow = false; |
| 18561 | } |
| 18562 | if (((size_t)buf->cmn.append_pos + data->size) > (size_t)buf->cmn.size) { |
| 18563 | buf->cmn.append_overflow = true; |
| 18564 | } |
| 18565 | const int start_pos = buf->cmn.append_pos; |
| 18566 | // NOTE: the multiple-of-4 requirement for the buffer offset is coming |
| 18567 | // from WebGPU, but we want identical behaviour between backends |
| 18568 | SOKOL_ASSERT(_sg_multiple_u64((uint64_t)start_pos, 4)); |
| 18569 | if (buf->slot.state == SG_RESOURCESTATE_VALID) { |
| 18570 | if (_sg_validate_append_buffer(buf, data)) { |
| 18571 | if (!buf->cmn.append_overflow && (data->size > 0)) { |
| 18572 | // update and append on same buffer in same frame not allowed |
| 18573 | SOKOL_ASSERT(buf->cmn.update_frame_index != _sg.frame_index); |
| 18574 | _sg_append_buffer(buf, data, buf->cmn.append_frame_index != _sg.frame_index); |
| 18575 | buf->cmn.append_pos += (int) _sg_roundup_u64(data->size, 4); |
| 18576 | buf->cmn.append_frame_index = _sg.frame_index; |
| 18577 | } |
| 18578 | } |
| 18579 | } |
| 18580 | result = start_pos; |
| 18581 | } else { |
| 18582 | // FIXME: should we return -1 here? |
| 18583 | result = 0; |
| 18584 | } |
| 18585 | _SG_TRACE_ARGS(append_buffer, buf_id, data, result); |
| 18586 | return result; |
| 18587 | } |
| 18588 | |
| 18589 | SOKOL_API_IMPL bool sg_query_buffer_overflow(sg_buffer buf_id) { |
| 18590 | SOKOL_ASSERT(_sg.valid); |
| 18591 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 18592 | bool result = buf ? buf->cmn.append_overflow : false; |
| 18593 | return result; |
| 18594 | } |
| 18595 | |
| 18596 | SOKOL_API_IMPL bool sg_query_buffer_will_overflow(sg_buffer buf_id, size_t size) { |
| 18597 | SOKOL_ASSERT(_sg.valid); |
| 18598 | _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 18599 | bool result = false; |
| 18600 | if (buf) { |
| 18601 | int append_pos = buf->cmn.append_pos; |
| 18602 | // rewind append cursor in a new frame |
| 18603 | if (buf->cmn.append_frame_index != _sg.frame_index) { |
| 18604 | append_pos = 0; |
| 18605 | } |
| 18606 | if ((append_pos + _sg_roundup((int)size, 4)) > buf->cmn.size) { |
| 18607 | result = true; |
| 18608 | } |
| 18609 | } |
| 18610 | return result; |
| 18611 | } |
| 18612 | |
| 18613 | SOKOL_API_IMPL void sg_update_image(sg_image img_id, const sg_image_data* data) { |
| 18614 | SOKOL_ASSERT(_sg.valid); |
| 18615 | _sg_stats_add(num_update_image, 1); |
| 18616 | for (int face_index = 0; face_index < SG_CUBEFACE_NUM; face_index++) { |
| 18617 | for (int mip_index = 0; mip_index < SG_MAX_MIPMAPS; mip_index++) { |
| 18618 | if (data->subimage[face_index][mip_index].size == 0) { |
| 18619 | break; |
| 18620 | } |
| 18621 | _sg_stats_add(size_update_image, (uint32_t)data->subimage[face_index][mip_index].size); |
| 18622 | } |
| 18623 | } |
| 18624 | _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 18625 | if (img && img->slot.state == SG_RESOURCESTATE_VALID) { |
| 18626 | if (_sg_validate_update_image(img, data)) { |
| 18627 | SOKOL_ASSERT(img->cmn.upd_frame_index != _sg.frame_index); |
| 18628 | _sg_update_image(img, data); |
| 18629 | img->cmn.upd_frame_index = _sg.frame_index; |
| 18630 | } |
| 18631 | } |
| 18632 | _SG_TRACE_ARGS(update_image, img_id, data); |
| 18633 | } |
| 18634 | |
| 18635 | SOKOL_API_IMPL void sg_push_debug_group(const char* name) { |
| 18636 | SOKOL_ASSERT(_sg.valid); |
| 18637 | SOKOL_ASSERT(name); |
| 18638 | _sg_push_debug_group(name); |
| 18639 | _SG_TRACE_ARGS(push_debug_group, name); |
| 18640 | } |
| 18641 | |
| 18642 | SOKOL_API_IMPL void sg_pop_debug_group(void) { |
| 18643 | SOKOL_ASSERT(_sg.valid); |
| 18644 | _sg_pop_debug_group(); |
| 18645 | _SG_TRACE_NOARGS(pop_debug_group); |
| 18646 | } |
| 18647 | |
| 18648 | SOKOL_API_IMPL bool sg_add_commit_listener(sg_commit_listener listener) { |
| 18649 | SOKOL_ASSERT(_sg.valid); |
| 18650 | return _sg_add_commit_listener(&listener); |
| 18651 | } |
| 18652 | |
| 18653 | SOKOL_API_IMPL bool sg_remove_commit_listener(sg_commit_listener listener) { |
| 18654 | SOKOL_ASSERT(_sg.valid); |
| 18655 | return _sg_remove_commit_listener(&listener); |
| 18656 | } |
| 18657 | |
| 18658 | SOKOL_API_IMPL void sg_enable_frame_stats(void) { |
| 18659 | SOKOL_ASSERT(_sg.valid); |
| 18660 | _sg.stats_enabled = true; |
| 18661 | } |
| 18662 | |
| 18663 | SOKOL_API_IMPL void sg_disable_frame_stats(void) { |
| 18664 | SOKOL_ASSERT(_sg.valid); |
| 18665 | _sg.stats_enabled = false; |
| 18666 | } |
| 18667 | |
| 18668 | SOKOL_API_IMPL bool sg_frame_stats_enabled(void) { |
| 18669 | return _sg.stats_enabled; |
| 18670 | } |
| 18671 | |
| 18672 | SOKOL_API_IMPL sg_buffer_info sg_query_buffer_info(sg_buffer buf_id) { |
| 18673 | SOKOL_ASSERT(_sg.valid); |
| 18674 | sg_buffer_info info; |
| 18675 | _sg_clear(&info, sizeof(info)); |
| 18676 | const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 18677 | if (buf) { |
| 18678 | info.slot.state = buf->slot.state; |
| 18679 | info.slot.res_id = buf->slot.id; |
| 18680 | info.update_frame_index = buf->cmn.update_frame_index; |
| 18681 | info.append_frame_index = buf->cmn.append_frame_index; |
| 18682 | info.append_pos = buf->cmn.append_pos; |
| 18683 | info.append_overflow = buf->cmn.append_overflow; |
| 18684 | #if defined(SOKOL_D3D11) |
| 18685 | info.num_slots = 1; |
| 18686 | info.active_slot = 0; |
| 18687 | #else |
| 18688 | info.num_slots = buf->cmn.num_slots; |
| 18689 | info.active_slot = buf->cmn.active_slot; |
| 18690 | #endif |
| 18691 | } |
| 18692 | return info; |
| 18693 | } |
| 18694 | |
| 18695 | SOKOL_API_IMPL sg_image_info sg_query_image_info(sg_image img_id) { |
| 18696 | SOKOL_ASSERT(_sg.valid); |
| 18697 | sg_image_info info; |
| 18698 | _sg_clear(&info, sizeof(info)); |
| 18699 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 18700 | if (img) { |
| 18701 | info.slot.state = img->slot.state; |
| 18702 | info.slot.res_id = img->slot.id; |
| 18703 | info.upd_frame_index = img->cmn.upd_frame_index; |
| 18704 | #if defined(SOKOL_D3D11) |
| 18705 | info.num_slots = 1; |
| 18706 | info.active_slot = 0; |
| 18707 | #else |
| 18708 | info.num_slots = img->cmn.num_slots; |
| 18709 | info.active_slot = img->cmn.active_slot; |
| 18710 | #endif |
| 18711 | } |
| 18712 | return info; |
| 18713 | } |
| 18714 | |
| 18715 | SOKOL_API_IMPL sg_sampler_info sg_query_sampler_info(sg_sampler smp_id) { |
| 18716 | SOKOL_ASSERT(_sg.valid); |
| 18717 | sg_sampler_info info; |
| 18718 | _sg_clear(&info, sizeof(info)); |
| 18719 | const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id); |
| 18720 | if (smp) { |
| 18721 | info.slot.state = smp->slot.state; |
| 18722 | info.slot.res_id = smp->slot.id; |
| 18723 | } |
| 18724 | return info; |
| 18725 | } |
| 18726 | |
| 18727 | SOKOL_API_IMPL sg_shader_info sg_query_shader_info(sg_shader shd_id) { |
| 18728 | SOKOL_ASSERT(_sg.valid); |
| 18729 | sg_shader_info info; |
| 18730 | _sg_clear(&info, sizeof(info)); |
| 18731 | const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 18732 | if (shd) { |
| 18733 | info.slot.state = shd->slot.state; |
| 18734 | info.slot.res_id = shd->slot.id; |
| 18735 | } |
| 18736 | return info; |
| 18737 | } |
| 18738 | |
| 18739 | SOKOL_API_IMPL sg_pipeline_info sg_query_pipeline_info(sg_pipeline pip_id) { |
| 18740 | SOKOL_ASSERT(_sg.valid); |
| 18741 | sg_pipeline_info info; |
| 18742 | _sg_clear(&info, sizeof(info)); |
| 18743 | const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 18744 | if (pip) { |
| 18745 | info.slot.state = pip->slot.state; |
| 18746 | info.slot.res_id = pip->slot.id; |
| 18747 | } |
| 18748 | return info; |
| 18749 | } |
| 18750 | |
| 18751 | SOKOL_API_IMPL sg_attachments_info sg_query_attachments_info(sg_attachments atts_id) { |
| 18752 | SOKOL_ASSERT(_sg.valid); |
| 18753 | sg_attachments_info info; |
| 18754 | _sg_clear(&info, sizeof(info)); |
| 18755 | const _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id); |
| 18756 | if (atts) { |
| 18757 | info.slot.state = atts->slot.state; |
| 18758 | info.slot.res_id = atts->slot.id; |
| 18759 | } |
| 18760 | return info; |
| 18761 | } |
| 18762 | |
| 18763 | SOKOL_API_IMPL sg_buffer_desc sg_query_buffer_desc(sg_buffer buf_id) { |
| 18764 | SOKOL_ASSERT(_sg.valid); |
| 18765 | sg_buffer_desc desc; |
| 18766 | _sg_clear(&desc, sizeof(desc)); |
| 18767 | const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 18768 | if (buf) { |
| 18769 | desc.size = (size_t)buf->cmn.size; |
| 18770 | desc.type = buf->cmn.type; |
| 18771 | desc.usage = buf->cmn.usage; |
| 18772 | } |
| 18773 | return desc; |
| 18774 | } |
| 18775 | |
| 18776 | SOKOL_API_IMPL sg_image_desc sg_query_image_desc(sg_image img_id) { |
| 18777 | SOKOL_ASSERT(_sg.valid); |
| 18778 | sg_image_desc desc; |
| 18779 | _sg_clear(&desc, sizeof(desc)); |
| 18780 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 18781 | if (img) { |
| 18782 | desc.type = img->cmn.type; |
| 18783 | desc.render_target = img->cmn.render_target; |
| 18784 | desc.width = img->cmn.width; |
| 18785 | desc.height = img->cmn.height; |
| 18786 | desc.num_slices = img->cmn.num_slices; |
| 18787 | desc.num_mipmaps = img->cmn.num_mipmaps; |
| 18788 | desc.usage = img->cmn.usage; |
| 18789 | desc.pixel_format = img->cmn.pixel_format; |
| 18790 | desc.sample_count = img->cmn.sample_count; |
| 18791 | } |
| 18792 | return desc; |
| 18793 | } |
| 18794 | |
| 18795 | SOKOL_API_IMPL sg_sampler_desc sg_query_sampler_desc(sg_sampler smp_id) { |
| 18796 | SOKOL_ASSERT(_sg.valid); |
| 18797 | sg_sampler_desc desc; |
| 18798 | _sg_clear(&desc, sizeof(desc)); |
| 18799 | const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id); |
| 18800 | if (smp) { |
| 18801 | desc.min_filter = smp->cmn.min_filter; |
| 18802 | desc.mag_filter = smp->cmn.mag_filter; |
| 18803 | desc.mipmap_filter = smp->cmn.mipmap_filter; |
| 18804 | desc.wrap_u = smp->cmn.wrap_u; |
| 18805 | desc.wrap_v = smp->cmn.wrap_v; |
| 18806 | desc.wrap_w = smp->cmn.wrap_w; |
| 18807 | desc.min_lod = smp->cmn.min_lod; |
| 18808 | desc.max_lod = smp->cmn.max_lod; |
| 18809 | desc.border_color = smp->cmn.border_color; |
| 18810 | desc.compare = smp->cmn.compare; |
| 18811 | desc.max_anisotropy = smp->cmn.max_anisotropy; |
| 18812 | } |
| 18813 | return desc; |
| 18814 | } |
| 18815 | |
| 18816 | SOKOL_API_IMPL sg_shader_desc sg_query_shader_desc(sg_shader shd_id) { |
| 18817 | SOKOL_ASSERT(_sg.valid); |
| 18818 | sg_shader_desc desc; |
| 18819 | _sg_clear(&desc, sizeof(desc)); |
| 18820 | const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 18821 | if (shd) { |
| 18822 | for (int stage_idx = 0; stage_idx < SG_NUM_SHADER_STAGES; stage_idx++) { |
| 18823 | sg_shader_stage_desc* stage_desc = (stage_idx == 0) ? &desc.vs : &desc.fs; |
| 18824 | const _sg_shader_stage_t* stage = &shd->cmn.stage[stage_idx]; |
| 18825 | for (int ub_idx = 0; ub_idx < stage->num_uniform_blocks; ub_idx++) { |
| 18826 | sg_shader_uniform_block_desc* ub_desc = &stage_desc->uniform_blocks[ub_idx]; |
| 18827 | const _sg_shader_uniform_block_t* ub = &stage->uniform_blocks[ub_idx]; |
| 18828 | ub_desc->size = ub->size; |
| 18829 | } |
| 18830 | for (int img_idx = 0; img_idx < stage->num_images; img_idx++) { |
| 18831 | sg_shader_image_desc* img_desc = &stage_desc->images[img_idx]; |
| 18832 | const _sg_shader_image_t* img = &stage->images[img_idx]; |
| 18833 | img_desc->used = true; |
| 18834 | img_desc->image_type = img->image_type; |
| 18835 | img_desc->sample_type = img->sample_type; |
| 18836 | img_desc->multisampled = img->multisampled; |
| 18837 | } |
| 18838 | for (int smp_idx = 0; smp_idx < stage->num_samplers; smp_idx++) { |
| 18839 | sg_shader_sampler_desc* smp_desc = &stage_desc->samplers[smp_idx]; |
| 18840 | const _sg_shader_sampler_t* smp = &stage->samplers[smp_idx]; |
| 18841 | smp_desc->used = true; |
| 18842 | smp_desc->sampler_type = smp->sampler_type; |
| 18843 | } |
| 18844 | for (int img_smp_idx = 0; img_smp_idx < stage->num_image_samplers; img_smp_idx++) { |
| 18845 | sg_shader_image_sampler_pair_desc* img_smp_desc = &stage_desc->image_sampler_pairs[img_smp_idx]; |
| 18846 | const _sg_shader_image_sampler_t* img_smp = &stage->image_samplers[img_smp_idx]; |
| 18847 | img_smp_desc->used = true; |
| 18848 | img_smp_desc->image_slot = img_smp->image_slot; |
| 18849 | img_smp_desc->sampler_slot = img_smp->sampler_slot; |
| 18850 | img_smp_desc->glsl_name = 0; |
| 18851 | } |
| 18852 | } |
| 18853 | } |
| 18854 | return desc; |
| 18855 | } |
| 18856 | |
| 18857 | SOKOL_API_IMPL sg_pipeline_desc sg_query_pipeline_desc(sg_pipeline pip_id) { |
| 18858 | SOKOL_ASSERT(_sg.valid); |
| 18859 | sg_pipeline_desc desc; |
| 18860 | _sg_clear(&desc, sizeof(desc)); |
| 18861 | const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 18862 | if (pip) { |
| 18863 | desc.shader = pip->cmn.shader_id; |
| 18864 | desc.layout = pip->cmn.layout; |
| 18865 | desc.depth = pip->cmn.depth; |
| 18866 | desc.stencil = pip->cmn.stencil; |
| 18867 | desc.color_count = pip->cmn.color_count; |
| 18868 | for (int i = 0; i < pip->cmn.color_count; i++) { |
| 18869 | desc.colors[i] = pip->cmn.colors[i]; |
| 18870 | } |
| 18871 | desc.primitive_type = pip->cmn.primitive_type; |
| 18872 | desc.index_type = pip->cmn.index_type; |
| 18873 | desc.cull_mode = pip->cmn.cull_mode; |
| 18874 | desc.face_winding = pip->cmn.face_winding; |
| 18875 | desc.sample_count = pip->cmn.sample_count; |
| 18876 | desc.blend_color = pip->cmn.blend_color; |
| 18877 | desc.alpha_to_coverage_enabled = pip->cmn.alpha_to_coverage_enabled; |
| 18878 | } |
| 18879 | return desc; |
| 18880 | } |
| 18881 | |
| 18882 | SOKOL_API_IMPL sg_attachments_desc sg_query_attachments_desc(sg_attachments atts_id) { |
| 18883 | SOKOL_ASSERT(_sg.valid); |
| 18884 | sg_attachments_desc desc; |
| 18885 | _sg_clear(&desc, sizeof(desc)); |
| 18886 | const _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id); |
| 18887 | if (atts) { |
| 18888 | for (int i = 0; i < atts->cmn.num_colors; i++) { |
| 18889 | desc.colors[i].image = atts->cmn.colors[i].image_id; |
| 18890 | desc.colors[i].mip_level = atts->cmn.colors[i].mip_level; |
| 18891 | desc.colors[i].slice = atts->cmn.colors[i].slice; |
| 18892 | } |
| 18893 | desc.depth_stencil.image = atts->cmn.depth_stencil.image_id; |
| 18894 | desc.depth_stencil.mip_level = atts->cmn.depth_stencil.mip_level; |
| 18895 | desc.depth_stencil.slice = atts->cmn.depth_stencil.slice; |
| 18896 | } |
| 18897 | return desc; |
| 18898 | } |
| 18899 | |
| 18900 | SOKOL_API_IMPL sg_buffer_desc sg_query_buffer_defaults(const sg_buffer_desc* desc) { |
| 18901 | SOKOL_ASSERT(_sg.valid && desc); |
| 18902 | return _sg_buffer_desc_defaults(desc); |
| 18903 | } |
| 18904 | |
| 18905 | SOKOL_API_IMPL sg_image_desc sg_query_image_defaults(const sg_image_desc* desc) { |
| 18906 | SOKOL_ASSERT(_sg.valid && desc); |
| 18907 | return _sg_image_desc_defaults(desc); |
| 18908 | } |
| 18909 | |
| 18910 | SOKOL_API_IMPL sg_sampler_desc sg_query_sampler_defaults(const sg_sampler_desc* desc) { |
| 18911 | SOKOL_ASSERT(_sg.valid && desc); |
| 18912 | return _sg_sampler_desc_defaults(desc); |
| 18913 | } |
| 18914 | |
| 18915 | SOKOL_API_IMPL sg_shader_desc sg_query_shader_defaults(const sg_shader_desc* desc) { |
| 18916 | SOKOL_ASSERT(_sg.valid && desc); |
| 18917 | return _sg_shader_desc_defaults(desc); |
| 18918 | } |
| 18919 | |
| 18920 | SOKOL_API_IMPL sg_pipeline_desc sg_query_pipeline_defaults(const sg_pipeline_desc* desc) { |
| 18921 | SOKOL_ASSERT(_sg.valid && desc); |
| 18922 | return _sg_pipeline_desc_defaults(desc); |
| 18923 | } |
| 18924 | |
| 18925 | SOKOL_API_IMPL sg_attachments_desc sg_query_attachments_defaults(const sg_attachments_desc* desc) { |
| 18926 | SOKOL_ASSERT(_sg.valid && desc); |
| 18927 | return _sg_attachments_desc_defaults(desc); |
| 18928 | } |
| 18929 | |
| 18930 | SOKOL_API_IMPL const void* sg_d3d11_device(void) { |
| 18931 | #if defined(SOKOL_D3D11) |
| 18932 | return (const void*) _sg.d3d11.dev; |
| 18933 | #else |
| 18934 | return 0; |
| 18935 | #endif |
| 18936 | } |
| 18937 | |
| 18938 | SOKOL_API_IMPL const void* sg_d3d11_device_context(void) { |
| 18939 | #if defined(SOKOL_D3D11) |
| 18940 | return (const void*) _sg.d3d11.ctx; |
| 18941 | #else |
| 18942 | return 0; |
| 18943 | #endif |
| 18944 | } |
| 18945 | |
| 18946 | SOKOL_API_IMPL sg_d3d11_buffer_info sg_d3d11_query_buffer_info(sg_buffer buf_id) { |
| 18947 | SOKOL_ASSERT(_sg.valid); |
| 18948 | sg_d3d11_buffer_info res; |
| 18949 | _sg_clear(&res, sizeof(res)); |
| 18950 | #if defined(SOKOL_D3D11) |
| 18951 | const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 18952 | if (buf) { |
| 18953 | res.buf = (const void*) buf->d3d11.buf; |
| 18954 | } |
| 18955 | #else |
| 18956 | _SOKOL_UNUSED(buf_id); |
| 18957 | #endif |
| 18958 | return res; |
| 18959 | } |
| 18960 | |
| 18961 | SOKOL_API_IMPL sg_d3d11_image_info sg_d3d11_query_image_info(sg_image img_id) { |
| 18962 | SOKOL_ASSERT(_sg.valid); |
| 18963 | sg_d3d11_image_info res; |
| 18964 | _sg_clear(&res, sizeof(res)); |
| 18965 | #if defined(SOKOL_D3D11) |
| 18966 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 18967 | if (img) { |
| 18968 | res.tex2d = (const void*) img->d3d11.tex2d; |
| 18969 | res.tex3d = (const void*) img->d3d11.tex3d; |
| 18970 | res.res = (const void*) img->d3d11.res; |
| 18971 | res.srv = (const void*) img->d3d11.srv; |
| 18972 | } |
| 18973 | #else |
| 18974 | _SOKOL_UNUSED(img_id); |
| 18975 | #endif |
| 18976 | return res; |
| 18977 | } |
| 18978 | |
| 18979 | SOKOL_API_IMPL sg_d3d11_sampler_info sg_d3d11_query_sampler_info(sg_sampler smp_id) { |
| 18980 | SOKOL_ASSERT(_sg.valid); |
| 18981 | sg_d3d11_sampler_info res; |
| 18982 | _sg_clear(&res, sizeof(res)); |
| 18983 | #if defined(SOKOL_D3D11) |
| 18984 | const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id); |
| 18985 | if (smp) { |
| 18986 | res.smp = (const void*) smp->d3d11.smp; |
| 18987 | } |
| 18988 | #else |
| 18989 | _SOKOL_UNUSED(smp_id); |
| 18990 | #endif |
| 18991 | return res; |
| 18992 | } |
| 18993 | |
| 18994 | SOKOL_API_IMPL sg_d3d11_shader_info sg_d3d11_query_shader_info(sg_shader shd_id) { |
| 18995 | SOKOL_ASSERT(_sg.valid); |
| 18996 | sg_d3d11_shader_info res; |
| 18997 | _sg_clear(&res, sizeof(res)); |
| 18998 | #if defined(SOKOL_D3D11) |
| 18999 | const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 19000 | if (shd) { |
| 19001 | for (int i = 0; i < SG_MAX_SHADERSTAGE_UBS; i++) { |
| 19002 | res.vs_cbufs[i] = (const void*) shd->d3d11.stage[SG_SHADERSTAGE_VS].cbufs[i]; |
| 19003 | res.fs_cbufs[i] = (const void*) shd->d3d11.stage[SG_SHADERSTAGE_FS].cbufs[i]; |
| 19004 | } |
| 19005 | res.vs = (const void*) shd->d3d11.vs; |
| 19006 | res.fs = (const void*) shd->d3d11.fs; |
| 19007 | } |
| 19008 | #else |
| 19009 | _SOKOL_UNUSED(shd_id); |
| 19010 | #endif |
| 19011 | return res; |
| 19012 | } |
| 19013 | |
| 19014 | SOKOL_API_IMPL sg_d3d11_pipeline_info sg_d3d11_query_pipeline_info(sg_pipeline pip_id) { |
| 19015 | SOKOL_ASSERT(_sg.valid); |
| 19016 | sg_d3d11_pipeline_info res; |
| 19017 | _sg_clear(&res, sizeof(res)); |
| 19018 | #if defined(SOKOL_D3D11) |
| 19019 | const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 19020 | if (pip) { |
| 19021 | res.il = (const void*) pip->d3d11.il; |
| 19022 | res.rs = (const void*) pip->d3d11.rs; |
| 19023 | res.dss = (const void*) pip->d3d11.dss; |
| 19024 | res.bs = (const void*) pip->d3d11.bs; |
| 19025 | } |
| 19026 | #else |
| 19027 | _SOKOL_UNUSED(pip_id); |
| 19028 | #endif |
| 19029 | return res; |
| 19030 | } |
| 19031 | |
| 19032 | SOKOL_API_IMPL sg_d3d11_attachments_info sg_d3d11_query_attachments_info(sg_attachments atts_id) { |
| 19033 | SOKOL_ASSERT(_sg.valid); |
| 19034 | sg_d3d11_attachments_info res; |
| 19035 | _sg_clear(&res, sizeof(res)); |
| 19036 | #if defined(SOKOL_D3D11) |
| 19037 | const _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id); |
| 19038 | if (atts) { |
| 19039 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 19040 | res.color_rtv[i] = (const void*) atts->d3d11.colors[i].view.rtv; |
| 19041 | res.resolve_rtv[i] = (const void*) atts->d3d11.resolves[i].view.rtv; |
| 19042 | } |
| 19043 | res.dsv = (const void*) atts->d3d11.depth_stencil.view.dsv; |
| 19044 | } |
| 19045 | #else |
| 19046 | _SOKOL_UNUSED(atts_id); |
| 19047 | #endif |
| 19048 | return res; |
| 19049 | } |
| 19050 | |
| 19051 | SOKOL_API_IMPL const void* sg_mtl_device(void) { |
| 19052 | #if defined(SOKOL_METAL) |
| 19053 | if (nil != _sg.mtl.device) { |
| 19054 | return (__bridge const void*) _sg.mtl.device; |
| 19055 | } else { |
| 19056 | return 0; |
| 19057 | } |
| 19058 | #else |
| 19059 | return 0; |
| 19060 | #endif |
| 19061 | } |
| 19062 | |
| 19063 | SOKOL_API_IMPL const void* sg_mtl_render_command_encoder(void) { |
| 19064 | #if defined(SOKOL_METAL) |
| 19065 | if (nil != _sg.mtl.cmd_encoder) { |
| 19066 | return (__bridge const void*) _sg.mtl.cmd_encoder; |
| 19067 | } else { |
| 19068 | return 0; |
| 19069 | } |
| 19070 | #else |
| 19071 | return 0; |
| 19072 | #endif |
| 19073 | } |
| 19074 | |
| 19075 | SOKOL_API_IMPL sg_mtl_buffer_info sg_mtl_query_buffer_info(sg_buffer buf_id) { |
| 19076 | SOKOL_ASSERT(_sg.valid); |
| 19077 | sg_mtl_buffer_info res; |
| 19078 | _sg_clear(&res, sizeof(res)); |
| 19079 | #if defined(SOKOL_METAL) |
| 19080 | const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 19081 | if (buf) { |
| 19082 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 19083 | if (buf->mtl.buf[i] != 0) { |
| 19084 | res.buf[i] = (__bridge void*) _sg_mtl_id(buf->mtl.buf[i]); |
| 19085 | } |
| 19086 | } |
| 19087 | res.active_slot = buf->cmn.active_slot; |
| 19088 | } |
| 19089 | #else |
| 19090 | _SOKOL_UNUSED(buf_id); |
| 19091 | #endif |
| 19092 | return res; |
| 19093 | } |
| 19094 | |
| 19095 | SOKOL_API_IMPL sg_mtl_image_info sg_mtl_query_image_info(sg_image img_id) { |
| 19096 | SOKOL_ASSERT(_sg.valid); |
| 19097 | sg_mtl_image_info res; |
| 19098 | _sg_clear(&res, sizeof(res)); |
| 19099 | #if defined(SOKOL_METAL) |
| 19100 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 19101 | if (img) { |
| 19102 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 19103 | if (img->mtl.tex[i] != 0) { |
| 19104 | res.tex[i] = (__bridge void*) _sg_mtl_id(img->mtl.tex[i]); |
| 19105 | } |
| 19106 | } |
| 19107 | res.active_slot = img->cmn.active_slot; |
| 19108 | } |
| 19109 | #else |
| 19110 | _SOKOL_UNUSED(img_id); |
| 19111 | #endif |
| 19112 | return res; |
| 19113 | } |
| 19114 | |
| 19115 | SOKOL_API_IMPL sg_mtl_sampler_info sg_mtl_query_sampler_info(sg_sampler smp_id) { |
| 19116 | SOKOL_ASSERT(_sg.valid); |
| 19117 | sg_mtl_sampler_info res; |
| 19118 | _sg_clear(&res, sizeof(res)); |
| 19119 | #if defined(SOKOL_METAL) |
| 19120 | const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id); |
| 19121 | if (smp) { |
| 19122 | if (smp->mtl.sampler_state != 0) { |
| 19123 | res.smp = (__bridge void*) _sg_mtl_id(smp->mtl.sampler_state); |
| 19124 | } |
| 19125 | } |
| 19126 | #else |
| 19127 | _SOKOL_UNUSED(smp_id); |
| 19128 | #endif |
| 19129 | return res; |
| 19130 | } |
| 19131 | |
| 19132 | SOKOL_API_IMPL sg_mtl_shader_info sg_mtl_query_shader_info(sg_shader shd_id) { |
| 19133 | SOKOL_ASSERT(_sg.valid); |
| 19134 | sg_mtl_shader_info res; |
| 19135 | _sg_clear(&res, sizeof(res)); |
| 19136 | #if defined(SOKOL_METAL) |
| 19137 | const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 19138 | if (shd) { |
| 19139 | const int vs_lib = shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_lib; |
| 19140 | const int vs_func = shd->mtl.stage[SG_SHADERSTAGE_VS].mtl_func; |
| 19141 | const int fs_lib = shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_lib; |
| 19142 | const int fs_func = shd->mtl.stage[SG_SHADERSTAGE_FS].mtl_func; |
| 19143 | if (vs_lib != 0) { |
| 19144 | res.vs_lib = (__bridge void*) _sg_mtl_id(vs_lib); |
| 19145 | } |
| 19146 | if (fs_lib != 0) { |
| 19147 | res.fs_lib = (__bridge void*) _sg_mtl_id(fs_lib); |
| 19148 | } |
| 19149 | if (vs_func != 0) { |
| 19150 | res.vs_func = (__bridge void*) _sg_mtl_id(vs_func); |
| 19151 | } |
| 19152 | if (fs_func != 0) { |
| 19153 | res.fs_func = (__bridge void*) _sg_mtl_id(fs_func); |
| 19154 | } |
| 19155 | } |
| 19156 | #else |
| 19157 | _SOKOL_UNUSED(shd_id); |
| 19158 | #endif |
| 19159 | return res; |
| 19160 | } |
| 19161 | |
| 19162 | SOKOL_API_IMPL sg_mtl_pipeline_info sg_mtl_query_pipeline_info(sg_pipeline pip_id) { |
| 19163 | SOKOL_ASSERT(_sg.valid); |
| 19164 | sg_mtl_pipeline_info res; |
| 19165 | _sg_clear(&res, sizeof(res)); |
| 19166 | #if defined(SOKOL_METAL) |
| 19167 | const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 19168 | if (pip) { |
| 19169 | if (pip->mtl.rps != 0) { |
| 19170 | res.rps = (__bridge void*) _sg_mtl_id(pip->mtl.rps); |
| 19171 | } |
| 19172 | if (pip->mtl.dss != 0) { |
| 19173 | res.dss = (__bridge void*) _sg_mtl_id(pip->mtl.dss); |
| 19174 | } |
| 19175 | } |
| 19176 | #else |
| 19177 | _SOKOL_UNUSED(pip_id); |
| 19178 | #endif |
| 19179 | return res; |
| 19180 | } |
| 19181 | |
| 19182 | SOKOL_API_IMPL const void* sg_wgpu_device(void) { |
| 19183 | #if defined(SOKOL_WGPU) |
| 19184 | return (const void*) _sg.wgpu.dev; |
| 19185 | #else |
| 19186 | return 0; |
| 19187 | #endif |
| 19188 | } |
| 19189 | |
| 19190 | SOKOL_API_IMPL const void* sg_wgpu_queue(void) { |
| 19191 | #if defined(SOKOL_WGPU) |
| 19192 | return (const void*) _sg.wgpu.queue; |
| 19193 | #else |
| 19194 | return 0; |
| 19195 | #endif |
| 19196 | } |
| 19197 | |
| 19198 | SOKOL_API_IMPL const void* sg_wgpu_command_encoder(void) { |
| 19199 | #if defined(SOKOL_WGPU) |
| 19200 | return (const void*) _sg.wgpu.cmd_enc; |
| 19201 | #else |
| 19202 | return 0; |
| 19203 | #endif |
| 19204 | } |
| 19205 | |
| 19206 | SOKOL_API_IMPL const void* sg_wgpu_render_pass_encoder(void) { |
| 19207 | #if defined(SOKOL_WGPU) |
| 19208 | return (const void*) _sg.wgpu.pass_enc; |
| 19209 | #else |
| 19210 | return 0; |
| 19211 | #endif |
| 19212 | } |
| 19213 | |
| 19214 | SOKOL_API_IMPL sg_wgpu_buffer_info sg_wgpu_query_buffer_info(sg_buffer buf_id) { |
| 19215 | SOKOL_ASSERT(_sg.valid); |
| 19216 | sg_wgpu_buffer_info res; |
| 19217 | _sg_clear(&res, sizeof(res)); |
| 19218 | #if defined(SOKOL_WGPU) |
| 19219 | const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 19220 | if (buf) { |
| 19221 | res.buf = (const void*) buf->wgpu.buf; |
| 19222 | } |
| 19223 | #else |
| 19224 | _SOKOL_UNUSED(buf_id); |
| 19225 | #endif |
| 19226 | return res; |
| 19227 | } |
| 19228 | |
| 19229 | SOKOL_API_IMPL sg_wgpu_image_info sg_wgpu_query_image_info(sg_image img_id) { |
| 19230 | SOKOL_ASSERT(_sg.valid); |
| 19231 | sg_wgpu_image_info res; |
| 19232 | _sg_clear(&res, sizeof(res)); |
| 19233 | #if defined(SOKOL_WGPU) |
| 19234 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 19235 | if (img) { |
| 19236 | res.tex = (const void*) img->wgpu.tex; |
| 19237 | res.view = (const void*) img->wgpu.view; |
| 19238 | } |
| 19239 | #else |
| 19240 | _SOKOL_UNUSED(img_id); |
| 19241 | #endif |
| 19242 | return res; |
| 19243 | } |
| 19244 | |
| 19245 | SOKOL_API_IMPL sg_wgpu_sampler_info sg_wgpu_query_sampler_info(sg_sampler smp_id) { |
| 19246 | SOKOL_ASSERT(_sg.valid); |
| 19247 | sg_wgpu_sampler_info res; |
| 19248 | _sg_clear(&res, sizeof(res)); |
| 19249 | #if defined(SOKOL_WGPU) |
| 19250 | const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id); |
| 19251 | if (smp) { |
| 19252 | res.smp = (const void*) smp->wgpu.smp; |
| 19253 | } |
| 19254 | #else |
| 19255 | _SOKOL_UNUSED(smp_id); |
| 19256 | #endif |
| 19257 | return res; |
| 19258 | } |
| 19259 | |
| 19260 | SOKOL_API_IMPL sg_wgpu_shader_info sg_wgpu_query_shader_info(sg_shader shd_id) { |
| 19261 | SOKOL_ASSERT(_sg.valid); |
| 19262 | sg_wgpu_shader_info res; |
| 19263 | _sg_clear(&res, sizeof(res)); |
| 19264 | #if defined(SOKOL_WGPU) |
| 19265 | const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 19266 | if (shd) { |
| 19267 | res.vs_mod = (const void*) shd->wgpu.stage[SG_SHADERSTAGE_VS].module; |
| 19268 | res.fs_mod = (const void*) shd->wgpu.stage[SG_SHADERSTAGE_FS].module; |
| 19269 | res.bgl = (const void*) shd->wgpu.bind_group_layout; |
| 19270 | } |
| 19271 | #else |
| 19272 | _SOKOL_UNUSED(shd_id); |
| 19273 | #endif |
| 19274 | return res; |
| 19275 | } |
| 19276 | |
| 19277 | SOKOL_API_IMPL sg_wgpu_pipeline_info sg_wgpu_query_pipeline_info(sg_pipeline pip_id) { |
| 19278 | SOKOL_ASSERT(_sg.valid); |
| 19279 | sg_wgpu_pipeline_info res; |
| 19280 | _sg_clear(&res, sizeof(res)); |
| 19281 | #if defined(SOKOL_WGPU) |
| 19282 | const _sg_pipeline_t* pip = _sg_lookup_pipeline(&_sg.pools, pip_id.id); |
| 19283 | if (pip) { |
| 19284 | res.pip = (const void*) pip->wgpu.pip; |
| 19285 | } |
| 19286 | #else |
| 19287 | _SOKOL_UNUSED(pip_id); |
| 19288 | #endif |
| 19289 | return res; |
| 19290 | } |
| 19291 | |
| 19292 | SOKOL_API_IMPL sg_wgpu_attachments_info sg_wgpu_query_attachments_info(sg_attachments atts_id) { |
| 19293 | SOKOL_ASSERT(_sg.valid); |
| 19294 | sg_wgpu_attachments_info res; |
| 19295 | _sg_clear(&res, sizeof(res)); |
| 19296 | #if defined(SOKOL_WGPU) |
| 19297 | const _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id); |
| 19298 | if (atts) { |
| 19299 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 19300 | res.color_view[i] = (const void*) atts->wgpu.colors[i].view; |
| 19301 | res.resolve_view[i] = (const void*) atts->wgpu.resolves[i].view; |
| 19302 | } |
| 19303 | res.ds_view = (const void*) atts->wgpu.depth_stencil.view; |
| 19304 | } |
| 19305 | #else |
| 19306 | _SOKOL_UNUSED(atts_id); |
| 19307 | #endif |
| 19308 | return res; |
| 19309 | } |
| 19310 | |
| 19311 | SOKOL_API_IMPL sg_gl_buffer_info sg_gl_query_buffer_info(sg_buffer buf_id) { |
| 19312 | SOKOL_ASSERT(_sg.valid); |
| 19313 | sg_gl_buffer_info res; |
| 19314 | _sg_clear(&res, sizeof(res)); |
| 19315 | #if defined(_SOKOL_ANY_GL) |
| 19316 | const _sg_buffer_t* buf = _sg_lookup_buffer(&_sg.pools, buf_id.id); |
| 19317 | if (buf) { |
| 19318 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 19319 | res.buf[i] = buf->gl.buf[i]; |
| 19320 | } |
| 19321 | res.active_slot = buf->cmn.active_slot; |
| 19322 | } |
| 19323 | #else |
| 19324 | _SOKOL_UNUSED(buf_id); |
| 19325 | #endif |
| 19326 | return res; |
| 19327 | } |
| 19328 | |
| 19329 | SOKOL_API_IMPL sg_gl_image_info sg_gl_query_image_info(sg_image img_id) { |
| 19330 | SOKOL_ASSERT(_sg.valid); |
| 19331 | sg_gl_image_info res; |
| 19332 | _sg_clear(&res, sizeof(res)); |
| 19333 | #if defined(_SOKOL_ANY_GL) |
| 19334 | const _sg_image_t* img = _sg_lookup_image(&_sg.pools, img_id.id); |
| 19335 | if (img) { |
| 19336 | for (int i = 0; i < SG_NUM_INFLIGHT_FRAMES; i++) { |
| 19337 | res.tex[i] = img->gl.tex[i]; |
| 19338 | } |
| 19339 | res.tex_target = img->gl.target; |
| 19340 | res.msaa_render_buffer = img->gl.msaa_render_buffer; |
| 19341 | res.active_slot = img->cmn.active_slot; |
| 19342 | } |
| 19343 | #else |
| 19344 | _SOKOL_UNUSED(img_id); |
| 19345 | #endif |
| 19346 | return res; |
| 19347 | } |
| 19348 | |
| 19349 | SOKOL_API_IMPL sg_gl_sampler_info sg_gl_query_sampler_info(sg_sampler smp_id) { |
| 19350 | SOKOL_ASSERT(_sg.valid); |
| 19351 | sg_gl_sampler_info res; |
| 19352 | _sg_clear(&res, sizeof(res)); |
| 19353 | #if defined(_SOKOL_ANY_GL) |
| 19354 | const _sg_sampler_t* smp = _sg_lookup_sampler(&_sg.pools, smp_id.id); |
| 19355 | if (smp) { |
| 19356 | res.smp = smp->gl.smp; |
| 19357 | } |
| 19358 | #else |
| 19359 | _SOKOL_UNUSED(smp_id); |
| 19360 | #endif |
| 19361 | return res; |
| 19362 | } |
| 19363 | |
| 19364 | SOKOL_API_IMPL sg_gl_shader_info sg_gl_query_shader_info(sg_shader shd_id) { |
| 19365 | SOKOL_ASSERT(_sg.valid); |
| 19366 | sg_gl_shader_info res; |
| 19367 | _sg_clear(&res, sizeof(res)); |
| 19368 | #if defined(_SOKOL_ANY_GL) |
| 19369 | const _sg_shader_t* shd = _sg_lookup_shader(&_sg.pools, shd_id.id); |
| 19370 | if (shd) { |
| 19371 | res.prog = shd->gl.prog; |
| 19372 | } |
| 19373 | #else |
| 19374 | _SOKOL_UNUSED(shd_id); |
| 19375 | #endif |
| 19376 | return res; |
| 19377 | } |
| 19378 | |
| 19379 | SOKOL_API_IMPL sg_gl_attachments_info sg_gl_query_attachments_info(sg_attachments atts_id) { |
| 19380 | SOKOL_ASSERT(_sg.valid); |
| 19381 | sg_gl_attachments_info res; |
| 19382 | _sg_clear(&res, sizeof(res)); |
| 19383 | #if defined(_SOKOL_ANY_GL) |
| 19384 | const _sg_attachments_t* atts = _sg_lookup_attachments(&_sg.pools, atts_id.id); |
| 19385 | if (atts) { |
| 19386 | res.framebuffer = atts->gl.fb; |
| 19387 | for (int i = 0; i < SG_MAX_COLOR_ATTACHMENTS; i++) { |
| 19388 | res.msaa_resolve_framebuffer[i] = atts->gl.msaa_resolve_framebuffer[i]; |
| 19389 | } |
| 19390 | } |
| 19391 | #else |
| 19392 | _SOKOL_UNUSED(atts_id); |
| 19393 | #endif |
| 19394 | return res; |
| 19395 | } |
| 19396 | |
| 19397 | #ifdef _MSC_VER |
| 19398 | #pragma warning(pop) |
| 19399 | #endif |
| 19400 | |
| 19401 | #endif // SOKOL_GFX_IMPL |