• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. _context:
2
3Context
4=======
5
6A Gallium rendering context encapsulates the state which effects 3D
7rendering such as blend state, depth/stencil state, texture samplers,
8etc.
9
10Note that resource/texture allocation is not per-context but per-screen.
11
12
13Methods
14-------
15
16CSO State
17^^^^^^^^^
18
19All Constant State Object (CSO) state is created, bound, and destroyed,
20with triplets of methods that all follow a specific naming scheme.
21For example, ``create_blend_state``, ``bind_blend_state``, and
22``destroy_blend_state``.
23
24CSO 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, geometry and compute shaders with the ``bind_sampler_states``
29  function.  The ``start`` and ``num_samplers`` parameters indicate a range
30  of samplers to change.  NOTE: at this time, start is always zero and
31  the CSO module will always replace all samplers at once (no sub-ranges).
32  This may change in the future.
33* :ref:`Rasterizer`: ``*_rasterizer_state``
34* :ref:`depth-stencil-alpha`: ``*_depth_stencil_alpha_state``
35* :ref:`Shader`: These are create, bind and destroy methods for vertex,
36  fragment and geometry shaders.
37* :ref:`vertexelements`: ``*_vertex_elements_state``
38
39
40Resource Binding State
41^^^^^^^^^^^^^^^^^^^^^^
42
43This state describes how resources in various flavours (textures,
44buffers, surfaces) are bound to the driver.
45
46
47* ``set_constant_buffer`` sets a constant buffer to be used for a given shader
48  type. index is used to indicate which buffer to set (some apis may allow
49  multiple ones to be set, and binding a specific one later, though drivers
50  are mostly restricted to the first one right now).
51
52* ``set_framebuffer_state``
53
54* ``set_vertex_buffers``
55
56
57Non-CSO State
58^^^^^^^^^^^^^
59
60These pieces of state are too small, variable, and/or trivial to have CSO
61objects. They all follow simple, one-method binding calls, e.g.
62``set_blend_color``.
63
64* ``set_stencil_ref`` sets the stencil front and back reference values
65  which are used as comparison values in stencil test.
66* ``set_blend_color``
67* ``set_sample_mask``  sets the per-context multisample sample mask.  Note
68  that this takes effect even if multisampling is not explicitly enabled if
69  the frambuffer surface(s) are multisampled.  Also, this mask is AND-ed
70  with the optional fragment shader sample mask output (when emitted).
71* ``set_min_samples`` sets the minimum number of samples that must be run.
72* ``set_clip_state``
73* ``set_polygon_stipple``
74* ``set_scissor_states`` sets the bounds for the scissor test, which culls
75  pixels before blending to render targets. If the :ref:`Rasterizer` does
76  not have the scissor test enabled, then the scissor bounds never need to
77  be set since they will not be used.  Note that scissor xmin and ymin are
78  inclusive, but  xmax and ymax are exclusive.  The inclusive ranges in x
79  and y would be [xmin..xmax-1] and [ymin..ymax-1]. The number of scissors
80  should be the same as the number of set viewports and can be up to
81  PIPE_MAX_VIEWPORTS.
82* ``set_viewport_states``
83* ``set_window_rectangles`` sets the window rectangles to be used for
84  rendering, as defined by GL_EXT_window_rectangles. There are two
85  modes - include and exclude, which define whether the supplied
86  rectangles are to be used for including fragments or excluding
87  them. All of the rectangles are ORed together, so in exclude mode,
88  any fragment inside any rectangle would be culled, while in include
89  mode, any fragment outside all rectangles would be culled. xmin/ymin
90  are inclusive, while xmax/ymax are exclusive (same as scissor states
91  above). Note that this only applies to draws, not clears or
92  blits. (Blits have their own way to pass the requisite rectangles
93  in.)
94* ``set_tess_state`` configures the default tessellation parameters:
95
96  * ``default_outer_level`` is the default value for the outer tessellation
97    levels. This corresponds to GL's ``PATCH_DEFAULT_OUTER_LEVEL``.
98  * ``default_inner_level`` is the default value for the inner tessellation
99    levels. This corresponds to GL's ``PATCH_DEFAULT_INNER_LEVEL``.
100
101* ``set_debug_callback`` sets the callback to be used for reporting
102  various debug messages, eventually reported via KHR_debug and
103  similar mechanisms.
104
105Samplers
106^^^^^^^^
107
108pipe_sampler_state objects control how textures are sampled (coordinate
109wrap modes, interpolation modes, etc).  Note that samplers are not used
110for texture buffer objects.  That is, pipe_context::bind_sampler_views()
111will not bind a sampler if the corresponding sampler view refers to a
112PIPE_BUFFER resource.
113
114Sampler Views
115^^^^^^^^^^^^^
116
117These are the means to bind textures to shader stages. To create one, specify
118its format, swizzle and LOD range in sampler view template.
119
120If texture format is different than template format, it is said the texture
121is being cast to another format. Casting can be done only between compatible
122formats, that is formats that have matching component order and sizes.
123
124Swizzle fields specify the way in which fetched texel components are placed
125in the result register. For example, ``swizzle_r`` specifies what is going to be
126placed in first component of result register.
127
128The ``first_level`` and ``last_level`` fields of sampler view template specify
129the LOD range the texture is going to be constrained to. Note that these
130values are in addition to the respective min_lod, max_lod values in the
131pipe_sampler_state (that is if min_lod is 2.0, and first_level 3, the first mip
132level used for sampling from the resource is effectively the fifth).
133
134The ``first_layer`` and ``last_layer`` fields specify the layer range the
135texture is going to be constrained to. Similar to the LOD range, this is added
136to the array index which is used for sampling.
137
138* ``set_sampler_views`` binds an array of sampler views to a shader stage.
139  Every binding point acquires a reference
140  to a respective sampler view and releases a reference to the previous
141  sampler view.
142
143* ``create_sampler_view`` creates a new sampler view. ``texture`` is associated
144  with the sampler view which results in sampler view holding a reference
145  to the texture. Format specified in template must be compatible
146  with texture format.
147
148* ``sampler_view_destroy`` destroys a sampler view and releases its reference
149  to associated texture.
150
151Hardware Atomic buffers
152^^^^^^^^^^^^^^^^^^^^^^^
153
154Buffers containing hw atomics are required to support the feature
155on some drivers.
156
157Drivers that require this need to fill the ``set_hw_atomic_buffers`` method.
158
159Shader Resources
160^^^^^^^^^^^^^^^^
161
162Shader resources are textures or buffers that may be read or written
163from a shader without an associated sampler.  This means that they
164have no support for floating point coordinates, address wrap modes or
165filtering.
166
167There are 2 types of shader resources: buffers and images.
168
169Buffers are specified using the ``set_shader_buffers`` method.
170
171Images are specified using the ``set_shader_images`` method. When binding
172images, the ``level``, ``first_layer`` and ``last_layer`` pipe_image_view
173fields specify the mipmap level and the range of layers the image will be
174constrained to.
175
176Surfaces
177^^^^^^^^
178
179These are the means to use resources as color render targets or depthstencil
180attachments. To create one, specify the mip level, the range of layers, and
181the bind flags (either PIPE_BIND_DEPTH_STENCIL or PIPE_BIND_RENDER_TARGET).
182Note that layer values are in addition to what is indicated by the geometry
183shader output variable XXX_FIXME (that is if first_layer is 3 and geometry
184shader indicates index 2, the 5th layer of the resource will be used). These
185first_layer and last_layer parameters will only be used for 1d array, 2d array,
186cube, and 3d textures otherwise they are 0.
187
188* ``create_surface`` creates a new surface.
189
190* ``surface_destroy`` destroys a surface and releases its reference to the
191  associated resource.
192
193Stream output targets
194^^^^^^^^^^^^^^^^^^^^^
195
196Stream output, also known as transform feedback, allows writing the primitives
197produced by the vertex pipeline to buffers. This is done after the geometry
198shader or vertex shader if no geometry shader is present.
199
200The stream output targets are views into buffer resources which can be bound
201as stream outputs and specify a memory range where it's valid to write
202primitives. The pipe driver must implement memory protection such that any
203primitives written outside of the specified memory range are discarded.
204
205Two stream output targets can use the same resource at the same time, but
206with a disjoint memory range.
207
208Additionally, the stream output target internally maintains the offset
209into the buffer which is incremented everytime something is written to it.
210The internal offset is equal to how much data has already been written.
211It can be stored in device memory and the CPU actually doesn't have to query
212it.
213
214The stream output target can be used in a draw command to provide
215the vertex count. The vertex count is derived from the internal offset
216discussed above.
217
218* ``create_stream_output_target`` create a new target.
219
220* ``stream_output_target_destroy`` destroys a target. Users of this should
221  use pipe_so_target_reference instead.
222
223* ``set_stream_output_targets`` binds stream output targets. The parameter
224  offset is an array which specifies the internal offset of the buffer. The
225  internal offset is, besides writing, used for reading the data during the
226  draw_auto stage, i.e. it specifies how much data there is in the buffer
227  for the purposes of the draw_auto stage. -1 means the buffer should
228  be appended to, and everything else sets the internal offset.
229
230NOTE: The currently-bound vertex or geometry shader must be compiled with
231the properly-filled-in structure pipe_stream_output_info describing which
232outputs should be written to buffers and how. The structure is part of
233pipe_shader_state.
234
235Clearing
236^^^^^^^^
237
238Clear is one of the most difficult concepts to nail down to a single
239interface (due to both different requirements from APIs and also driver/hw
240specific differences).
241
242``clear`` initializes some or all of the surfaces currently bound to
243the framebuffer to particular RGBA, depth, or stencil values.
244Currently, this does not take into account color or stencil write masks (as
245used by GL), and always clears the whole surfaces (no scissoring as used by
246GL clear or explicit rectangles like d3d9 uses). It can, however, also clear
247only depth or stencil in a combined depth/stencil surface.
248If a surface includes several layers then all layers will be cleared.
249
250``clear_render_target`` clears a single color rendertarget with the specified
251color value. While it is only possible to clear one surface at a time (which can
252include several layers), this surface need not be bound to the framebuffer.
253If render_condition_enabled is false, any current rendering condition is ignored
254and the clear will be unconditional.
255
256``clear_depth_stencil`` clears a single depth, stencil or depth/stencil surface
257with the specified depth and stencil values (for combined depth/stencil buffers,
258it is also possible to only clear one or the other part). While it is only
259possible to clear one surface at a time (which can include several layers),
260this surface need not be bound to the framebuffer.
261If render_condition_enabled is false, any current rendering condition is ignored
262and the clear will be unconditional.
263
264``clear_texture`` clears a non-PIPE_BUFFER resource's specified level
265and bounding box with a clear value provided in that resource's native
266format.
267
268``clear_buffer`` clears a PIPE_BUFFER resource with the specified clear value
269(which may be multiple bytes in length). Logically this is a memset with a
270multi-byte element value starting at offset bytes from resource start, going
271for size bytes. It is guaranteed that size % clear_value_size == 0.
272
273
274Uploading
275^^^^^^^^^
276
277For simple single-use uploads, use ``pipe_context::stream_uploader`` or
278``pipe_context::const_uploader``. The latter should be used for uploading
279constants, while the former should be used for uploading everything else.
280PIPE_USAGE_STREAM is implied in both cases, so don't use the uploaders
281for static allocations.
282
283Usage:
284
285Call u_upload_alloc or u_upload_data as many times as you want. After you are
286done, call u_upload_unmap. If the driver doesn't support persistent mappings,
287u_upload_unmap makes sure the previously mapped memory is unmapped.
288
289Gotchas:
290- Always fill the memory immediately after u_upload_alloc. Any following call
291to u_upload_alloc and u_upload_data can unmap memory returned by previous
292u_upload_alloc.
293- Don't interleave calls using stream_uploader and const_uploader. If you use
294one of them, do the upload, unmap, and only then can you use the other one.
295
296
297Drawing
298^^^^^^^
299
300``draw_vbo`` draws a specified primitive.  The primitive mode and other
301properties are described by ``pipe_draw_info``.
302
303The ``mode``, ``start``, and ``count`` fields of ``pipe_draw_info`` specify the
304the mode of the primitive and the vertices to be fetched, in the range between
305``start`` to ``start``+``count``-1, inclusive.
306
307Every instance with instanceID in the range between ``start_instance`` and
308``start_instance``+``instance_count``-1, inclusive, will be drawn.
309
310If  ``index_size`` != 0, all vertex indices will be looked up from the index
311buffer.
312
313In indexed draw, ``min_index`` and ``max_index`` respectively provide a lower
314and upper bound of the indices contained in the index buffer inside the range
315between ``start`` to ``start``+``count``-1.  This allows the driver to
316determine which subset of vertices will be referenced during te draw call
317without having to scan the index buffer.  Providing a over-estimation of the
318the true bounds, for example, a ``min_index`` and ``max_index`` of 0 and
3190xffffffff respectively, must give exactly the same rendering, albeit with less
320performance due to unreferenced vertex buffers being unnecessarily DMA'ed or
321processed.  Providing a underestimation of the true bounds will result in
322undefined behavior, but should not result in program or system failure.
323
324In case of non-indexed draw, ``min_index`` should be set to
325``start`` and ``max_index`` should be set to ``start``+``count``-1.
326
327``index_bias`` is a value added to every vertex index after lookup and before
328fetching vertex attributes.
329
330When drawing indexed primitives, the primitive restart index can be
331used to draw disjoint primitive strips.  For example, several separate
332line strips can be drawn by designating a special index value as the
333restart index.  The ``primitive_restart`` flag enables/disables this
334feature.  The ``restart_index`` field specifies the restart index value.
335
336When primitive restart is in use, array indexes are compared to the
337restart index before adding the index_bias offset.
338
339If a given vertex element has ``instance_divisor`` set to 0, it is said
340it contains per-vertex data and effective vertex attribute address needs
341to be recalculated for every index.
342
343  attribAddr = ``stride`` * index + ``src_offset``
344
345If a given vertex element has ``instance_divisor`` set to non-zero,
346it is said it contains per-instance data and effective vertex attribute
347address needs to recalculated for every ``instance_divisor``-th instance.
348
349  attribAddr = ``stride`` * instanceID / ``instance_divisor`` + ``src_offset``
350
351In the above formulas, ``src_offset`` is taken from the given vertex element
352and ``stride`` is taken from a vertex buffer associated with the given
353vertex element.
354
355The calculated attribAddr is used as an offset into the vertex buffer to
356fetch the attribute data.
357
358The value of ``instanceID`` can be read in a vertex shader through a system
359value register declared with INSTANCEID semantic name.
360
361
362Queries
363^^^^^^^
364
365Queries gather some statistic from the 3D pipeline over one or more
366draws.  Queries may be nested, though not all state trackers exercise this.
367
368Queries can be created with ``create_query`` and deleted with
369``destroy_query``. To start a query, use ``begin_query``, and when finished,
370use ``end_query`` to end the query.
371
372``create_query`` takes a query type (``PIPE_QUERY_*``), as well as an index,
373which is the vertex stream for ``PIPE_QUERY_PRIMITIVES_GENERATED`` and
374``PIPE_QUERY_PRIMITIVES_EMITTED``, and allocates a query structure.
375
376``begin_query`` will clear/reset previous query results.
377
378``get_query_result`` is used to retrieve the results of a query.  If
379the ``wait`` parameter is TRUE, then the ``get_query_result`` call
380will block until the results of the query are ready (and TRUE will be
381returned).  Otherwise, if the ``wait`` parameter is FALSE, the call
382will not block and the return value will be TRUE if the query has
383completed or FALSE otherwise.
384
385``get_query_result_resource`` is used to store the result of a query into
386a resource without synchronizing with the CPU. This write will optionally
387wait for the query to complete, and will optionally write whether the value
388is available instead of the value itself.
389
390``set_active_query_state`` Set whether all current non-driver queries except
391TIME_ELAPSED are active or paused.
392
393The interface currently includes the following types of queries:
394
395``PIPE_QUERY_OCCLUSION_COUNTER`` counts the number of fragments which
396are written to the framebuffer without being culled by
397:ref:`depth-stencil-alpha` testing or shader KILL instructions.
398The result is an unsigned 64-bit integer.
399This query can be used with ``render_condition``.
400
401In cases where a boolean result of an occlusion query is enough,
402``PIPE_QUERY_OCCLUSION_PREDICATE`` should be used. It is just like
403``PIPE_QUERY_OCCLUSION_COUNTER`` except that the result is a boolean
404value of FALSE for cases where COUNTER would result in 0 and TRUE
405for all other cases.
406This query can be used with ``render_condition``.
407
408In cases where a conservative approximation of an occlusion query is enough,
409``PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE`` should be used. It behaves
410like ``PIPE_QUERY_OCCLUSION_PREDICATE``, except that it may return TRUE in
411additional, implementation-dependent cases.
412This query can be used with ``render_condition``.
413
414``PIPE_QUERY_TIME_ELAPSED`` returns the amount of time, in nanoseconds,
415the context takes to perform operations.
416The result is an unsigned 64-bit integer.
417
418``PIPE_QUERY_TIMESTAMP`` returns a device/driver internal timestamp,
419scaled to nanoseconds, recorded after all commands issued prior to
420``end_query`` have been processed.
421This query does not require a call to ``begin_query``.
422The result is an unsigned 64-bit integer.
423
424``PIPE_QUERY_TIMESTAMP_DISJOINT`` can be used to check the
425internal timer resolution and whether the timestamp counter has become
426unreliable due to things like throttling etc. - only if this is FALSE
427a timestamp query (within the timestamp_disjoint query) should be trusted.
428The result is a 64-bit integer specifying the timer resolution in Hz,
429followed by a boolean value indicating whether the timestamp counter
430is discontinuous or disjoint.
431
432``PIPE_QUERY_PRIMITIVES_GENERATED`` returns a 64-bit integer indicating
433the number of primitives processed by the pipeline (regardless of whether
434stream output is active or not).
435
436``PIPE_QUERY_PRIMITIVES_EMITTED`` returns a 64-bit integer indicating
437the number of primitives written to stream output buffers.
438
439``PIPE_QUERY_SO_STATISTICS`` returns 2 64-bit integers corresponding to
440the result of
441``PIPE_QUERY_PRIMITIVES_EMITTED`` and
442the number of primitives that would have been written to stream output buffers
443if they had infinite space available (primitives_storage_needed), in this order.
444XXX the 2nd value is equivalent to ``PIPE_QUERY_PRIMITIVES_GENERATED`` but it is
445unclear if it should be increased if stream output is not active.
446
447``PIPE_QUERY_SO_OVERFLOW_PREDICATE`` returns a boolean value indicating
448whether a selected stream output target has overflowed as a result of the
449commands issued between ``begin_query`` and ``end_query``.
450This query can be used with ``render_condition``. The output stream is
451selected by the stream number passed to ``create_query``.
452
453``PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE`` returns a boolean value indicating
454whether any stream output target has overflowed as a result of the commands
455issued between ``begin_query`` and ``end_query``. This query can be used
456with ``render_condition``, and its result is the logical OR of multiple
457``PIPE_QUERY_SO_OVERFLOW_PREDICATE`` queries, one for each stream output
458target.
459
460``PIPE_QUERY_GPU_FINISHED`` returns a boolean value indicating whether
461all commands issued before ``end_query`` have completed. However, this
462does not imply serialization.
463This query does not require a call to ``begin_query``.
464
465``PIPE_QUERY_PIPELINE_STATISTICS`` returns an array of the following
46664-bit integers:
467Number of vertices read from vertex buffers.
468Number of primitives read from vertex buffers.
469Number of vertex shader threads launched.
470Number of geometry shader threads launched.
471Number of primitives generated by geometry shaders.
472Number of primitives forwarded to the rasterizer.
473Number of primitives rasterized.
474Number of fragment shader threads launched.
475Number of tessellation control shader threads launched.
476Number of tessellation evaluation shader threads launched.
477If a shader type is not supported by the device/driver,
478the corresponding values should be set to 0.
479
480Gallium does not guarantee the availability of any query types; one must
481always check the capabilities of the :ref:`Screen` first.
482
483
484Conditional Rendering
485^^^^^^^^^^^^^^^^^^^^^
486
487A drawing command can be skipped depending on the outcome of a query
488(typically an occlusion query, or streamout overflow predicate).
489The ``render_condition`` function specifies the query which should be checked
490prior to rendering anything. Functions always honoring render_condition include
491(and are limited to) draw_vbo and clear.
492The blit, clear_render_target and clear_depth_stencil functions (but
493not resource_copy_region, which seems inconsistent) can also optionally honor
494the current render condition.
495
496If ``render_condition`` is called with ``query`` = NULL, conditional
497rendering is disabled and drawing takes place normally.
498
499If ``render_condition`` is called with a non-null ``query`` subsequent
500drawing commands will be predicated on the outcome of the query.
501Commands will be skipped if ``condition`` is equal to the predicate result
502(for non-boolean queries such as OCCLUSION_QUERY, zero counts as FALSE,
503non-zero as TRUE).
504
505If ``mode`` is PIPE_RENDER_COND_WAIT the driver will wait for the
506query to complete before deciding whether to render.
507
508If ``mode`` is PIPE_RENDER_COND_NO_WAIT and the query has not yet
509completed, the drawing command will be executed normally.  If the query
510has completed, drawing will be predicated on the outcome of the query.
511
512If ``mode`` is PIPE_RENDER_COND_BY_REGION_WAIT or
513PIPE_RENDER_COND_BY_REGION_NO_WAIT rendering will be predicated as above
514for the non-REGION modes but in the case that an occlusion query returns
515a non-zero result, regions which were occluded may be ommitted by subsequent
516drawing commands.  This can result in better performance with some GPUs.
517Normally, if the occlusion query returned a non-zero result subsequent
518drawing happens normally so fragments may be generated, shaded and
519processed even where they're known to be obscured.
520
521
522Flushing
523^^^^^^^^
524
525``flush``
526
527PIPE_FLUSH_END_OF_FRAME: Whether the flush marks the end of frame.
528
529PIPE_FLUSH_DEFERRED: It is not required to flush right away, but it is required
530to return a valid fence. If fence_finish is called with the returned fence
531and the context is still unflushed, and the ctx parameter of fence_finish is
532equal to the context where the fence was created, fence_finish will flush
533the context.
534
535PIPE_FLUSH_ASYNC: The flush is allowed to be asynchronous. Unlike
536``PIPE_FLUSH_DEFERRED``, the driver must still ensure that the returned fence
537will finish in finite time. However, subsequent operations in other contexts of
538the same screen are no longer guaranteed to happen after the flush. Drivers
539which use this flag must implement pipe_context::fence_server_sync.
540
541PIPE_FLUSH_HINT_FINISH: Hints to the driver that the caller will immediately
542wait for the returned fence.
543
544Additional flags may be set together with ``PIPE_FLUSH_DEFERRED`` for even
545finer-grained fences. Note that as a general rule, GPU caches may not have been
546flushed yet when these fences are signaled. Drivers are free to ignore these
547flags and create normal fences instead. At most one of the following flags can
548be specified:
549
550PIPE_FLUSH_TOP_OF_PIPE: The fence should be signaled as soon as the next
551command is ready to start executing at the top of the pipeline, before any of
552its data is actually read (including indirect draw parameters).
553
554PIPE_FLUSH_BOTTOM_OF_PIPE: The fence should be signaled as soon as the previous
555command has finished executing on the GPU entirely (but data written by the
556command may still be in caches and inaccessible to the CPU).
557
558
559``flush_resource``
560
561Flush the resource cache, so that the resource can be used
562by an external client. Possible usage:
563- flushing a resource before presenting it on the screen
564- flushing a resource if some other process or device wants to use it
565This shouldn't be used to flush caches if the resource is only managed
566by a single pipe_screen and is not shared with another process.
567(i.e. you shouldn't use it to flush caches explicitly if you want to e.g.
568use the resource for texturing)
569
570
571
572Resource Busy Queries
573^^^^^^^^^^^^^^^^^^^^^
574
575``is_resource_referenced``
576
577
578
579Blitting
580^^^^^^^^
581
582These methods emulate classic blitter controls.
583
584These methods operate directly on ``pipe_resource`` objects, and stand
585apart from any 3D state in the context.  Blitting functionality may be
586moved to a separate abstraction at some point in the future.
587
588``resource_copy_region`` blits a region of a resource to a region of another
589resource, provided that both resources have the same format, or compatible
590formats, i.e., formats for which copying the bytes from the source resource
591unmodified to the destination resource will achieve the same effect of a
592textured quad blitter.. The source and destination may be the same resource,
593but overlapping blits are not permitted.
594This can be considered the equivalent of a CPU memcpy.
595
596``blit`` blits a region of a resource to a region of another resource, including
597scaling, format conversion, and up-/downsampling, as well as a destination clip
598rectangle (scissors) and window rectangles. It can also optionally honor the
599current render condition (but either way the blit itself never contributes
600anything to queries currently gathering data).
601As opposed to manually drawing a textured quad, this lets the pipe driver choose
602the optimal method for blitting (like using a special 2D engine), and usually
603offers, for example, accelerated stencil-only copies even where
604PIPE_CAP_SHADER_STENCIL_EXPORT is not available.
605
606
607Transfers
608^^^^^^^^^
609
610These methods are used to get data to/from a resource.
611
612``transfer_map`` creates a memory mapping and the transfer object
613associated with it.
614The returned pointer points to the start of the mapped range according to
615the box region, not the beginning of the resource. If transfer_map fails,
616the returned pointer to the buffer memory is NULL, and the pointer
617to the transfer object remains unchanged (i.e. it can be non-NULL).
618
619``transfer_unmap`` remove the memory mapping for and destroy
620the transfer object. The pointer into the resource should be considered
621invalid and discarded.
622
623``texture_subdata`` and ``buffer_subdata`` perform a simplified
624transfer for simple writes. Basically transfer_map, data write, and
625transfer_unmap all in one.
626
627
628The box parameter to some of these functions defines a 1D, 2D or 3D
629region of pixels.  This is self-explanatory for 1D, 2D and 3D texture
630targets.
631
632For PIPE_TEXTURE_1D_ARRAY and PIPE_TEXTURE_2D_ARRAY, the box::z and box::depth
633fields refer to the array dimension of the texture.
634
635For PIPE_TEXTURE_CUBE, the box:z and box::depth fields refer to the
636faces of the cube map (z + depth <= 6).
637
638For PIPE_TEXTURE_CUBE_ARRAY, the box:z and box::depth fields refer to both
639the face and array dimension of the texture (face = z % 6, array = z / 6).
640
641
642.. _transfer_flush_region:
643
644transfer_flush_region
645%%%%%%%%%%%%%%%%%%%%%
646
647If a transfer was created with ``FLUSH_EXPLICIT``, it will not automatically
648be flushed on write or unmap. Flushes must be requested with
649``transfer_flush_region``. Flush ranges are relative to the mapped range, not
650the beginning of the resource.
651
652
653
654.. _texture_barrier:
655
656texture_barrier
657%%%%%%%%%%%%%%%
658
659This function flushes all pending writes to the currently-set surfaces and
660invalidates all read caches of the currently-set samplers. This can be used
661for both regular textures as well as for framebuffers read via FBFETCH.
662
663
664
665.. _memory_barrier:
666
667memory_barrier
668%%%%%%%%%%%%%%%
669
670This function flushes caches according to which of the PIPE_BARRIER_* flags
671are set.
672
673
674
675.. _resource_commit:
676
677resource_commit
678%%%%%%%%%%%%%%%
679
680This function changes the commit state of a part of a sparse resource. Sparse
681resources are created by setting the ``PIPE_RESOURCE_FLAG_SPARSE`` flag when
682calling ``resource_create``. Initially, sparse resources only reserve a virtual
683memory region that is not backed by memory (i.e., it is uncommitted). The
684``resource_commit`` function can be called to commit or uncommit parts (or all)
685of a resource. The driver manages the underlying backing memory.
686
687The contents of newly committed memory regions are undefined. Calling this
688function to commit an already committed memory region is allowed and leaves its
689content unchanged. Similarly, calling this function to uncommit an already
690uncommitted memory region is allowed.
691
692For buffers, the given box must be aligned to multiples of
693``PIPE_CAP_SPARSE_BUFFER_PAGE_SIZE``. As an exception to this rule, if the size
694of the buffer is not a multiple of the page size, changing the commit state of
695the last (partial) page requires a box that ends at the end of the buffer
696(i.e., box->x + box->width == buffer->width0).
697
698
699
700.. _pipe_transfer:
701
702PIPE_TRANSFER
703^^^^^^^^^^^^^
704
705These flags control the behavior of a transfer object.
706
707``PIPE_TRANSFER_READ``
708  Resource contents read back (or accessed directly) at transfer create time.
709
710``PIPE_TRANSFER_WRITE``
711  Resource contents will be written back at transfer_unmap time (or modified
712  as a result of being accessed directly).
713
714``PIPE_TRANSFER_MAP_DIRECTLY``
715  a transfer should directly map the resource. May return NULL if not supported.
716
717``PIPE_TRANSFER_DISCARD_RANGE``
718  The memory within the mapped region is discarded.  Cannot be used with
719  ``PIPE_TRANSFER_READ``.
720
721``PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE``
722  Discards all memory backing the resource.  It should not be used with
723  ``PIPE_TRANSFER_READ``.
724
725``PIPE_TRANSFER_DONTBLOCK``
726  Fail if the resource cannot be mapped immediately.
727
728``PIPE_TRANSFER_UNSYNCHRONIZED``
729  Do not synchronize pending operations on the resource when mapping. The
730  interaction of any writes to the map and any operations pending on the
731  resource are undefined. Cannot be used with ``PIPE_TRANSFER_READ``.
732
733``PIPE_TRANSFER_FLUSH_EXPLICIT``
734  Written ranges will be notified later with :ref:`transfer_flush_region`.
735  Cannot be used with ``PIPE_TRANSFER_READ``.
736
737``PIPE_TRANSFER_PERSISTENT``
738  Allows the resource to be used for rendering while mapped.
739  PIPE_RESOURCE_FLAG_MAP_PERSISTENT must be set when creating
740  the resource.
741  If COHERENT is not set, memory_barrier(PIPE_BARRIER_MAPPED_BUFFER)
742  must be called to ensure the device can see what the CPU has written.
743
744``PIPE_TRANSFER_COHERENT``
745  If PERSISTENT is set, this ensures any writes done by the device are
746  immediately visible to the CPU and vice versa.
747  PIPE_RESOURCE_FLAG_MAP_COHERENT must be set when creating
748  the resource.
749
750Compute kernel execution
751^^^^^^^^^^^^^^^^^^^^^^^^
752
753A compute program can be defined, bound or destroyed using
754``create_compute_state``, ``bind_compute_state`` or
755``destroy_compute_state`` respectively.
756
757Any of the subroutines contained within the compute program can be
758executed on the device using the ``launch_grid`` method.  This method
759will execute as many instances of the program as elements in the
760specified N-dimensional grid, hopefully in parallel.
761
762The compute program has access to four special resources:
763
764* ``GLOBAL`` represents a memory space shared among all the threads
765  running on the device.  An arbitrary buffer created with the
766  ``PIPE_BIND_GLOBAL`` flag can be mapped into it using the
767  ``set_global_binding`` method.
768
769* ``LOCAL`` represents a memory space shared among all the threads
770  running in the same working group.  The initial contents of this
771  resource are undefined.
772
773* ``PRIVATE`` represents a memory space local to a single thread.
774  The initial contents of this resource are undefined.
775
776* ``INPUT`` represents a read-only memory space that can be
777  initialized at ``launch_grid`` time.
778
779These resources use a byte-based addressing scheme, and they can be
780accessed from the compute program by means of the LOAD/STORE TGSI
781opcodes.  Additional resources to be accessed using the same opcodes
782may be specified by the user with the ``set_compute_resources``
783method.
784
785In addition, normal texture sampling is allowed from the compute
786program: ``bind_sampler_states`` may be used to set up texture
787samplers for the compute stage and ``set_sampler_views`` may
788be used to bind a number of sampler views to it.
789
790Mipmap generation
791^^^^^^^^^^^^^^^^^
792
793If PIPE_CAP_GENERATE_MIPMAP is true, ``generate_mipmap`` can be used
794to generate mipmaps for the specified texture resource.
795It replaces texel image levels base_level+1 through
796last_level for layers range from first_layer through last_layer.
797It returns TRUE if mipmap generation succeeds, otherwise it
798returns FALSE. Mipmap generation may fail when it is not supported
799for particular texture types or formats.
800
801Device resets
802^^^^^^^^^^^^^
803
804The state tracker can query or request notifications of when the GPU
805is reset for whatever reason (application error, driver error). When
806a GPU reset happens, the context becomes unusable and all related state
807should be considered lost and undefined. Despite that, context
808notifications are single-shot, i.e. subsequent calls to
809``get_device_reset_status`` will return PIPE_NO_RESET.
810
811* ``get_device_reset_status`` queries whether a device reset has happened
812  since the last call or since the last notification by callback.
813* ``set_device_reset_callback`` sets a callback which will be called when
814  a device reset is detected. The callback is only called synchronously.
815
816Bindless
817^^^^^^^^
818
819If PIPE_CAP_BINDLESS_TEXTURE is TRUE, the following ``pipe_context`` functions
820are used to create/delete bindless handles, and to make them resident in the
821current context when they are going to be used by shaders.
822
823* ``create_texture_handle`` creates a 64-bit unsigned integer texture handle
824  that is going to be directly used in shaders.
825* ``delete_texture_handle`` deletes a 64-bit unsigned integer texture handle.
826* ``make_texture_handle_resident`` makes a 64-bit unsigned texture handle
827  resident in the current context to be accessible by shaders for texture
828  mapping.
829* ``create_image_handle`` creates a 64-bit unsigned integer image handle that
830  is going to be directly used in shaders.
831* ``delete_image_handle`` deletes a 64-bit unsigned integer image handle.
832* ``make_image_handle_resident`` makes a 64-bit unsigned integer image handle
833  resident in the current context to be accessible by shaders for image loads,
834  stores and atomic operations.
835
836Using several contexts
837----------------------
838
839Several contexts from the same screen can be used at the same time. Objects
840created on one context cannot be used in another context, but the objects
841created by the screen methods can be used by all contexts.
842
843Transfers
844^^^^^^^^^
845A transfer on one context is not expected to synchronize properly with
846rendering on other contexts, thus only areas not yet used for rendering should
847be locked.
848
849A flush is required after transfer_unmap to expect other contexts to see the
850uploaded data, unless:
851
852* Using persistent mapping. Associated with coherent mapping, unmapping the
853  resource is also not required to use it in other contexts. Without coherent
854  mapping, memory_barrier(PIPE_BARRIER_MAPPED_BUFFER) should be called on the
855  context that has mapped the resource. No flush is required.
856
857* Mapping the resource with PIPE_TRANSFER_MAP_DIRECTLY.
858