1 .. _context: 2 3 Context 4 ======= 5 6 A Gallium rendering context encapsulates the state which effects 3D 7 rendering such as blend state, depth/stencil state, texture samplers, 8 etc. 9 10 Note that resource/texture allocation is not per-context but per-screen. 11 12 13 Methods 14 ------- 15 16 CSO State 17 ^^^^^^^^^ 18 19 All Constant State Object (CSO) state is created, bound, and destroyed, 20 with triplets of methods that all follow a specific naming scheme. 21 For example, ``create_blend_state``, ``bind_blend_state``, and 22 ``destroy_blend_state``. 23 24 CSO objects handled by the context object: 25 26 * :ref:`Blend`: ``*_blend_state`` 27 * :ref:`Sampler`: Texture sampler states are bound separately for fragment, 28 vertex and geometry samplers. Note that sampler states are set en masse. 29 If M is the max number of sampler units supported by the driver and N 30 samplers are bound with ``bind_fragment_sampler_states`` then sampler 31 units N..M-1 are considered disabled/NULL. 32 * :ref:`Rasterizer`: ``*_rasterizer_state`` 33 * :ref:`Depth, Stencil, & Alpha`: ``*_depth_stencil_alpha_state`` 34 * :ref:`Shader`: These are create, bind and destroy methods for vertex, 35 fragment and geometry shaders. 36 * :ref:`Vertex Elements`: ``*_vertex_elements_state`` 37 38 39 Resource Binding State 40 ^^^^^^^^^^^^^^^^^^^^^^ 41 42 This state describes how resources in various flavours (textures, 43 buffers, surfaces) are bound to the driver. 44 45 46 * ``set_constant_buffer`` sets a constant buffer to be used for a given shader 47 type. index is used to indicate which buffer to set (some apis may allow 48 multiple ones to be set, and binding a specific one later, though drivers 49 are mostly restricted to the first one right now). 50 51 * ``set_framebuffer_state`` 52 53 * ``set_vertex_buffers`` 54 55 * ``set_index_buffer`` 56 57 58 Non-CSO State 59 ^^^^^^^^^^^^^ 60 61 These pieces of state are too small, variable, and/or trivial to have CSO 62 objects. They all follow simple, one-method binding calls, e.g. 63 ``set_blend_color``. 64 65 * ``set_stencil_ref`` sets the stencil front and back reference values 66 which are used as comparison values in stencil test. 67 * ``set_blend_color`` 68 * ``set_sample_mask`` 69 * ``set_clip_state`` 70 * ``set_polygon_stipple`` 71 * ``set_scissor_state`` sets the bounds for the scissor test, which culls 72 pixels before blending to render targets. If the :ref:`Rasterizer` does 73 not have the scissor test enabled, then the scissor bounds never need to 74 be set since they will not be used. Note that scissor xmin and ymin are 75 inclusive, but xmax and ymax are exclusive. The inclusive ranges in x 76 and y would be [xmin..xmax-1] and [ymin..ymax-1]. 77 * ``set_viewport_state`` 78 79 80 Sampler Views 81 ^^^^^^^^^^^^^ 82 83 These are the means to bind textures to shader stages. To create one, specify 84 its format, swizzle and LOD range in sampler view template. 85 86 If texture format is different than template format, it is said the texture 87 is being cast to another format. Casting can be done only between compatible 88 formats, that is formats that have matching component order and sizes. 89 90 Swizzle fields specify they way in which fetched texel components are placed 91 in the result register. For example, ``swizzle_r`` specifies what is going to be 92 placed in first component of result register. 93 94 The ``first_level`` and ``last_level`` fields of sampler view template specify 95 the LOD range the texture is going to be constrained to. Note that these 96 values are in addition to the respective min_lod, max_lod values in the 97 pipe_sampler_state (that is if min_lod is 2.0, and first_level 3, the first mip 98 level used for sampling from the resource is effectively the fifth). 99 100 The ``first_layer`` and ``last_layer`` fields specify the layer range the 101 texture is going to be constrained to. Similar to the LOD range, this is added 102 to the array index which is used for sampling. 103 104 * ``set_fragment_sampler_views`` binds an array of sampler views to 105 fragment shader stage. Every binding point acquires a reference 106 to a respective sampler view and releases a reference to the previous 107 sampler view. If M is the maximum number of sampler units and N units 108 is passed to set_fragment_sampler_views, the driver should unbind the 109 sampler views for units N..M-1. 110 111 * ``set_vertex_sampler_views`` binds an array of sampler views to vertex 112 shader stage. Every binding point acquires a reference to a respective 113 sampler view and releases a reference to the previous sampler view. 114 115 * ``create_sampler_view`` creates a new sampler view. ``texture`` is associated 116 with the sampler view which results in sampler view holding a reference 117 to the texture. Format specified in template must be compatible 118 with texture format. 119 120 * ``sampler_view_destroy`` destroys a sampler view and releases its reference 121 to associated texture. 122 123 Shader Resources 124 ^^^^^^^^^^^^^^^^ 125 126 Shader resources are textures or buffers that may be read or written 127 from a shader without an associated sampler. This means that they 128 have no support for floating point coordinates, address wrap modes or 129 filtering. 130 131 Shader resources are specified for all the shader stages at once using 132 the ``set_shader_resources`` method. When binding texture resources, 133 the ``level``, ``first_layer`` and ``last_layer`` pipe_surface fields 134 specify the mipmap level and the range of layers the texture will be 135 constrained to. In the case of buffers, ``first_element`` and 136 ``last_element`` specify the range within the buffer that will be used 137 by the shader resource. Writes to a shader resource are only allowed 138 when the ``writable`` flag is set. 139 140 Surfaces 141 ^^^^^^^^ 142 143 These are the means to use resources as color render targets or depthstencil 144 attachments. To create one, specify the mip level, the range of layers, and 145 the bind flags (either PIPE_BIND_DEPTH_STENCIL or PIPE_BIND_RENDER_TARGET). 146 Note that layer values are in addition to what is indicated by the geometry 147 shader output variable XXX_FIXME (that is if first_layer is 3 and geometry 148 shader indicates index 2, the 5th layer of the resource will be used). These 149 first_layer and last_layer parameters will only be used for 1d array, 2d array, 150 cube, and 3d textures otherwise they are 0. 151 152 * ``create_surface`` creates a new surface. 153 154 * ``surface_destroy`` destroys a surface and releases its reference to the 155 associated resource. 156 157 Stream output targets 158 ^^^^^^^^^^^^^^^^^^^^^ 159 160 Stream output, also known as transform feedback, allows writing the primitives 161 produced by the vertex pipeline to buffers. This is done after the geometry 162 shader or vertex shader if no geometry shader is present. 163 164 The stream output targets are views into buffer resources which can be bound 165 as stream outputs and specify a memory range where it's valid to write 166 primitives. The pipe driver must implement memory protection such that any 167 primitives written outside of the specified memory range are discarded. 168 169 Two stream output targets can use the same resource at the same time, but 170 with a disjoint memory range. 171 172 Additionally, the stream output target internally maintains the offset 173 into the buffer which is incremented everytime something is written to it. 174 The internal offset is equal to how much data has already been written. 175 It can be stored in device memory and the CPU actually doesn't have to query 176 it. 177 178 The stream output target can be used in a draw command to provide 179 the vertex count. The vertex count is derived from the internal offset 180 discussed above. 181 182 * ``create_stream_output_target`` create a new target. 183 184 * ``stream_output_target_destroy`` destroys a target. Users of this should 185 use pipe_so_target_reference instead. 186 187 * ``set_stream_output_targets`` binds stream output targets. The parameter 188 append_bitmask is a bitmask, where the i-th bit specifies whether new 189 primitives should be appended to the i-th buffer (writing starts at 190 the internal offset), or whether writing should start at the beginning 191 (the internal offset is effectively set to 0). 192 193 NOTE: The currently-bound vertex or geometry shader must be compiled with 194 the properly-filled-in structure pipe_stream_output_info describing which 195 outputs should be written to buffers and how. The structure is part of 196 pipe_shader_state. 197 198 Clearing 199 ^^^^^^^^ 200 201 Clear is one of the most difficult concepts to nail down to a single 202 interface (due to both different requirements from APIs and also driver/hw 203 specific differences). 204 205 ``clear`` initializes some or all of the surfaces currently bound to 206 the framebuffer to particular RGBA, depth, or stencil values. 207 Currently, this does not take into account color or stencil write masks (as 208 used by GL), and always clears the whole surfaces (no scissoring as used by 209 GL clear or explicit rectangles like d3d9 uses). It can, however, also clear 210 only depth or stencil in a combined depth/stencil surface, if the driver 211 supports PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE. 212 If a surface includes several layers then all layers will be cleared. 213 214 ``clear_render_target`` clears a single color rendertarget with the specified 215 color value. While it is only possible to clear one surface at a time (which can 216 include several layers), this surface need not be bound to the framebuffer. 217 218 ``clear_depth_stencil`` clears a single depth, stencil or depth/stencil surface 219 with the specified depth and stencil values (for combined depth/stencil buffers, 220 is is also possible to only clear one or the other part). While it is only 221 possible to clear one surface at a time (which can include several layers), 222 this surface need not be bound to the framebuffer. 223 224 225 Drawing 226 ^^^^^^^ 227 228 ``draw_vbo`` draws a specified primitive. The primitive mode and other 229 properties are described by ``pipe_draw_info``. 230 231 The ``mode``, ``start``, and ``count`` fields of ``pipe_draw_info`` specify the 232 the mode of the primitive and the vertices to be fetched, in the range between 233 ``start`` to ``start``+``count``-1, inclusive. 234 235 Every instance with instanceID in the range between ``start_instance`` and 236 ``start_instance``+``instance_count``-1, inclusive, will be drawn. 237 238 If there is an index buffer bound, and ``indexed`` field is true, all vertex 239 indices will be looked up in the index buffer. 240 241 In indexed draw, ``min_index`` and ``max_index`` respectively provide a lower 242 and upper bound of the indices contained in the index buffer inside the range 243 between ``start`` to ``start``+``count``-1. This allows the driver to 244 determine which subset of vertices will be referenced during te draw call 245 without having to scan the index buffer. Providing a over-estimation of the 246 the true bounds, for example, a ``min_index`` and ``max_index`` of 0 and 247 0xffffffff respectively, must give exactly the same rendering, albeit with less 248 performance due to unreferenced vertex buffers being unnecessarily DMA'ed or 249 processed. Providing a underestimation of the true bounds will result in 250 undefined behavior, but should not result in program or system failure. 251 252 In case of non-indexed draw, ``min_index`` should be set to 253 ``start`` and ``max_index`` should be set to ``start``+``count``-1. 254 255 ``index_bias`` is a value added to every vertex index after lookup and before 256 fetching vertex attributes. 257 258 When drawing indexed primitives, the primitive restart index can be 259 used to draw disjoint primitive strips. For example, several separate 260 line strips can be drawn by designating a special index value as the 261 restart index. The ``primitive_restart`` flag enables/disables this 262 feature. The ``restart_index`` field specifies the restart index value. 263 264 When primitive restart is in use, array indexes are compared to the 265 restart index before adding the index_bias offset. 266 267 If a given vertex element has ``instance_divisor`` set to 0, it is said 268 it contains per-vertex data and effective vertex attribute address needs 269 to be recalculated for every index. 270 271 attribAddr = ``stride`` * index + ``src_offset`` 272 273 If a given vertex element has ``instance_divisor`` set to non-zero, 274 it is said it contains per-instance data and effective vertex attribute 275 address needs to recalculated for every ``instance_divisor``-th instance. 276 277 attribAddr = ``stride`` * instanceID / ``instance_divisor`` + ``src_offset`` 278 279 In the above formulas, ``src_offset`` is taken from the given vertex element 280 and ``stride`` is taken from a vertex buffer associated with the given 281 vertex element. 282 283 The calculated attribAddr is used as an offset into the vertex buffer to 284 fetch the attribute data. 285 286 The value of ``instanceID`` can be read in a vertex shader through a system 287 value register declared with INSTANCEID semantic name. 288 289 290 Queries 291 ^^^^^^^ 292 293 Queries gather some statistic from the 3D pipeline over one or more 294 draws. Queries may be nested, though only d3d1x currently exercises this. 295 296 Queries can be created with ``create_query`` and deleted with 297 ``destroy_query``. To start a query, use ``begin_query``, and when finished, 298 use ``end_query`` to end the query. 299 300 ``get_query_result`` is used to retrieve the results of a query. If 301 the ``wait`` parameter is TRUE, then the ``get_query_result`` call 302 will block until the results of the query are ready (and TRUE will be 303 returned). Otherwise, if the ``wait`` parameter is FALSE, the call 304 will not block and the return value will be TRUE if the query has 305 completed or FALSE otherwise. 306 307 The interface currently includes the following types of queries: 308 309 ``PIPE_QUERY_OCCLUSION_COUNTER`` counts the number of fragments which 310 are written to the framebuffer without being culled by 311 :ref:`Depth, Stencil, & Alpha` testing or shader KILL instructions. 312 The result is an unsigned 64-bit integer. 313 This query can be used with ``render_condition``. 314 315 In cases where a boolean result of an occlusion query is enough, 316 ``PIPE_QUERY_OCCLUSION_PREDICATE`` should be used. It is just like 317 ``PIPE_QUERY_OCCLUSION_COUNTER`` except that the result is a boolean 318 value of FALSE for cases where COUNTER would result in 0 and TRUE 319 for all other cases. 320 This query can be used with ``render_condition``. 321 322 ``PIPE_QUERY_TIME_ELAPSED`` returns the amount of time, in nanoseconds, 323 the context takes to perform operations. 324 The result is an unsigned 64-bit integer. 325 326 ``PIPE_QUERY_TIMESTAMP`` returns a device/driver internal timestamp, 327 scaled to nanoseconds, recorded after all commands issued prior to 328 ``end_query`` have been processed. 329 This query does not require a call to ``begin_query``. 330 The result is an unsigned 64-bit integer. 331 332 ``PIPE_QUERY_TIMESTAMP_DISJOINT`` can be used to check whether the 333 internal timer resolution is good enough to distinguish between the 334 events at ``begin_query`` and ``end_query``. 335 The result is a 64-bit integer specifying the timer resolution in Hz, 336 followed by a boolean value indicating whether the timer has incremented. 337 338 ``PIPE_QUERY_PRIMITIVES_GENERATED`` returns a 64-bit integer indicating 339 the number of primitives processed by the pipeline. 340 341 ``PIPE_QUERY_PRIMITIVES_EMITTED`` returns a 64-bit integer indicating 342 the number of primitives written to stream output buffers. 343 344 ``PIPE_QUERY_SO_STATISTICS`` returns 2 64-bit integers corresponding to 345 the results of 346 ``PIPE_QUERY_PRIMITIVES_EMITTED`` and 347 ``PIPE_QUERY_PRIMITIVES_GENERATED``, in this order. 348 349 ``PIPE_QUERY_SO_OVERFLOW_PREDICATE`` returns a boolean value indicating 350 whether the stream output targets have overflowed as a result of the 351 commands issued between ``begin_query`` and ``end_query``. 352 This query can be used with ``render_condition``. 353 354 ``PIPE_QUERY_GPU_FINISHED`` returns a boolean value indicating whether 355 all commands issued before ``end_query`` have completed. However, this 356 does not imply serialization. 357 This query does not require a call to ``begin_query``. 358 359 ``PIPE_QUERY_PIPELINE_STATISTICS`` returns an array of the following 360 64-bit integers: 361 Number of vertices read from vertex buffers. 362 Number of primitives read from vertex buffers. 363 Number of vertex shader threads launched. 364 Number of geometry shader threads launched. 365 Number of primitives generated by geometry shaders. 366 Number of primitives forwarded to the rasterizer. 367 Number of primitives rasterized. 368 Number of fragment shader threads launched. 369 Number of tessellation control shader threads launched. 370 Number of tessellation evaluation shader threads launched. 371 If a shader type is not supported by the device/driver, 372 the corresponding values should be set to 0. 373 374 Gallium does not guarantee the availability of any query types; one must 375 always check the capabilities of the :ref:`Screen` first. 376 377 378 Conditional Rendering 379 ^^^^^^^^^^^^^^^^^^^^^ 380 381 A drawing command can be skipped depending on the outcome of a query 382 (typically an occlusion query). The ``render_condition`` function specifies 383 the query which should be checked prior to rendering anything. 384 385 If ``render_condition`` is called with ``query`` = NULL, conditional 386 rendering is disabled and drawing takes place normally. 387 388 If ``render_condition`` is called with a non-null ``query`` subsequent 389 drawing commands will be predicated on the outcome of the query. If 390 the query result is zero subsequent drawing commands will be skipped. 391 392 If ``mode`` is PIPE_RENDER_COND_WAIT the driver will wait for the 393 query to complete before deciding whether to render. 394 395 If ``mode`` is PIPE_RENDER_COND_NO_WAIT and the query has not yet 396 completed, the drawing command will be executed normally. If the query 397 has completed, drawing will be predicated on the outcome of the query. 398 399 If ``mode`` is PIPE_RENDER_COND_BY_REGION_WAIT or 400 PIPE_RENDER_COND_BY_REGION_NO_WAIT rendering will be predicated as above 401 for the non-REGION modes but in the case that an occulusion query returns 402 a non-zero result, regions which were occluded may be ommitted by subsequent 403 drawing commands. This can result in better performance with some GPUs. 404 Normally, if the occlusion query returned a non-zero result subsequent 405 drawing happens normally so fragments may be generated, shaded and 406 processed even where they're known to be obscured. 407 408 409 Flushing 410 ^^^^^^^^ 411 412 ``flush`` 413 414 415 Resource Busy Queries 416 ^^^^^^^^^^^^^^^^^^^^^ 417 418 ``is_resource_referenced`` 419 420 421 422 Blitting 423 ^^^^^^^^ 424 425 These methods emulate classic blitter controls. 426 427 These methods operate directly on ``pipe_resource`` objects, and stand 428 apart from any 3D state in the context. Blitting functionality may be 429 moved to a separate abstraction at some point in the future. 430 431 ``resource_copy_region`` blits a region of a resource to a region of another 432 resource, provided that both resources have the same format, or compatible 433 formats, i.e., formats for which copying the bytes from the source resource 434 unmodified to the destination resource will achieve the same effect of a 435 textured quad blitter.. The source and destination may be the same resource, 436 but overlapping blits are not permitted. 437 438 ``resource_resolve`` resolves a multisampled resource into a non-multisampled 439 one. Their formats must match. This function must be present if a driver 440 supports multisampling. 441 The region that is to be resolved is described by ``pipe_resolve_info``, which 442 provides a source and a destination rectangle. 443 The source rectangle may be vertically flipped, but otherwise the dimensions 444 of the rectangles must match, unless PIPE_CAP_SCALED_RESOLVE is supported, 445 in which case scaling and horizontal flipping are allowed as well. 446 The result of resolving depth/stencil values may be any function of the values at 447 the sample points, but returning the value of the centermost sample is preferred. 448 449 The interfaces to these calls are likely to change to make it easier 450 for a driver to batch multiple blits with the same source and 451 destination. 452 453 Transfers 454 ^^^^^^^^^ 455 456 These methods are used to get data to/from a resource. 457 458 ``get_transfer`` creates a transfer object. 459 460 ``transfer_destroy`` destroys the transfer object. May cause 461 data to be written to the resource at this point. 462 463 ``transfer_map`` creates a memory mapping for the transfer object. 464 The returned map points to the start of the mapped range according to 465 the box region, not the beginning of the resource. 466 467 ``transfer_unmap`` remove the memory mapping for the transfer object. 468 Any pointers into the map should be considered invalid and discarded. 469 470 ``transfer_inline_write`` performs a simplified transfer for simple writes. 471 Basically get_transfer, transfer_map, data write, transfer_unmap, and 472 transfer_destroy all in one. 473 474 475 The box parameter to some of these functions defines a 1D, 2D or 3D 476 region of pixels. This is self-explanatory for 1D, 2D and 3D texture 477 targets. 478 479 For PIPE_TEXTURE_1D_ARRAY, the box::y and box::height fields refer to the 480 array dimension of the texture. 481 482 For PIPE_TEXTURE_2D_ARRAY, the box::z and box::depth fields refer to the 483 array dimension of the texture. 484 485 For PIPE_TEXTURE_CUBE, the box:z and box::depth fields refer to the 486 faces of the cube map (z + depth <= 6). 487 488 489 490 .. _transfer_flush_region: 491 492 transfer_flush_region 493 %%%%%%%%%%%%%%%%%%%%% 494 495 If a transfer was created with ``FLUSH_EXPLICIT``, it will not automatically 496 be flushed on write or unmap. Flushes must be requested with 497 ``transfer_flush_region``. Flush ranges are relative to the mapped range, not 498 the beginning of the resource. 499 500 501 502 .. _texture_barrier 503 504 texture_barrier 505 %%%%%%%%%%%%%%% 506 507 This function flushes all pending writes to the currently-set surfaces and 508 invalidates all read caches of the currently-set samplers. 509 510 511 512 .. _pipe_transfer: 513 514 PIPE_TRANSFER 515 ^^^^^^^^^^^^^ 516 517 These flags control the behavior of a transfer object. 518 519 ``PIPE_TRANSFER_READ`` 520 Resource contents read back (or accessed directly) at transfer create time. 521 522 ``PIPE_TRANSFER_WRITE`` 523 Resource contents will be written back at transfer_destroy time (or modified 524 as a result of being accessed directly). 525 526 ``PIPE_TRANSFER_MAP_DIRECTLY`` 527 a transfer should directly map the resource. May return NULL if not supported. 528 529 ``PIPE_TRANSFER_DISCARD_RANGE`` 530 The memory within the mapped region is discarded. Cannot be used with 531 ``PIPE_TRANSFER_READ``. 532 533 ``PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE`` 534 Discards all memory backing the resource. It should not be used with 535 ``PIPE_TRANSFER_READ``. 536 537 ``PIPE_TRANSFER_DONTBLOCK`` 538 Fail if the resource cannot be mapped immediately. 539 540 ``PIPE_TRANSFER_UNSYNCHRONIZED`` 541 Do not synchronize pending operations on the resource when mapping. The 542 interaction of any writes to the map and any operations pending on the 543 resource are undefined. Cannot be used with ``PIPE_TRANSFER_READ``. 544 545 ``PIPE_TRANSFER_FLUSH_EXPLICIT`` 546 Written ranges will be notified later with :ref:`transfer_flush_region`. 547 Cannot be used with ``PIPE_TRANSFER_READ``. 548 549 550 Compute kernel execution 551 ^^^^^^^^^^^^^^^^^^^^^^^^ 552 553 A compute program can be defined, bound or destroyed using 554 ``create_compute_state``, ``bind_compute_state`` or 555 ``destroy_compute_state`` respectively. 556 557 Any of the subroutines contained within the compute program can be 558 executed on the device using the ``launch_grid`` method. This method 559 will execute as many instances of the program as elements in the 560 specified N-dimensional grid, hopefully in parallel. 561 562 The compute program has access to four special resources: 563 564 * ``GLOBAL`` represents a memory space shared among all the threads 565 running on the device. An arbitrary buffer created with the 566 ``PIPE_BIND_GLOBAL`` flag can be mapped into it using the 567 ``set_global_binding`` method. 568 569 * ``LOCAL`` represents a memory space shared among all the threads 570 running in the same working group. The initial contents of this 571 resource are undefined. 572 573 * ``PRIVATE`` represents a memory space local to a single thread. 574 The initial contents of this resource are undefined. 575 576 * ``INPUT`` represents a read-only memory space that can be 577 initialized at ``launch_grid`` time. 578 579 These resources use a byte-based addressing scheme, and they can be 580 accessed from the compute program by means of the LOAD/STORE TGSI 581 opcodes. Additional resources to be accessed using the same opcodes 582 may be specified by the user with the ``set_compute_resources`` 583 method. 584 585 In addition, normal texture sampling is allowed from the compute 586 program: ``bind_compute_sampler_states`` may be used to set up texture 587 samplers for the compute stage and ``set_compute_sampler_views`` may 588 be used to bind a number of sampler views to it. 589