blob: 8aee21b2ed480a656dc526f4e5982a1aa184bcdd [file] [log] [blame]
Corbin Simpsone7d05f12010-06-16 16:52:52 -07001.. _context:
2
Corbin Simpson8283e202009-12-20 15:28:00 -08003Context
4=======
5
Brian Paul73e37d92011-02-03 12:30:19 -07006A 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
Corbin Simpson8283e202009-12-20 15:28:00 -080012
13Methods
14-------
15
Corbin Simpsona524aab2009-12-20 19:41:50 -080016CSO State
17^^^^^^^^^
18
Brian Paul73e37d92011-02-03 12:30:19 -070019All 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``.
Corbin Simpsona524aab2009-12-20 19:41:50 -080023
24CSO objects handled by the context object:
25
26* :ref:`Blend`: ``*_blend_state``
Brian Paul73e37d92011-02-03 12:30:19 -070027* :ref:`Sampler`: Texture sampler states are bound separately for fragment,
Brian Paul55e81b02013-09-12 17:31:08 -060028 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.
Corbin Simpsona524aab2009-12-20 19:41:50 -080033* :ref:`Rasterizer`: ``*_rasterizer_state``
Ilia Mirkin05d02232014-03-31 18:08:07 -040034* :ref:`depth-stencil-alpha`: ``*_depth_stencil_alpha_state``
Brian Paul73e37d92011-02-03 12:30:19 -070035* :ref:`Shader`: These are create, bind and destroy methods for vertex,
36 fragment and geometry shaders.
Ilia Mirkin05d02232014-03-31 18:08:07 -040037* :ref:`vertexelements`: ``*_vertex_elements_state``
Corbin Simpsona524aab2009-12-20 19:41:50 -080038
Keith Whitwellf3347fe2009-12-21 23:44:32 +000039
40Resource Binding State
41^^^^^^^^^^^^^^^^^^^^^^
42
43This state describes how resources in various flavours (textures,
44buffers, surfaces) are bound to the driver.
45
46
Roland Scheideggerbf575b62010-01-15 18:25:14 +010047* ``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
Keith Whitwellf3347fe2009-12-21 23:44:32 +000052* ``set_framebuffer_state``
Michal Krole81caad2010-02-25 15:33:15 +010053
Keith Whitwellf3347fe2009-12-21 23:44:32 +000054* ``set_vertex_buffers``
55
Brian Paul73e37d92011-02-03 12:30:19 -070056
Corbin Simpsona524aab2009-12-20 19:41:50 -080057Non-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.
Roland Scheidegger98f8c4d02010-02-09 21:48:43 +010062``set_blend_color``.
Corbin Simpson8e1768c2010-03-19 00:07:55 -070063
Roland Scheidegger98f8c4d02010-02-09 21:48:43 +010064* ``set_stencil_ref`` sets the stencil front and back reference values
65 which are used as comparison values in stencil test.
Corbin Simpsona524aab2009-12-20 19:41:50 -080066* ``set_blend_color``
Roland Scheideggeraac2cccc2010-04-26 19:50:57 +020067* ``set_sample_mask``
Ilia Mirkin88d8d882014-03-30 18:21:04 -040068* ``set_min_samples`` sets the minimum number of samples that must be run.
Corbin Simpsona524aab2009-12-20 19:41:50 -080069* ``set_clip_state``
Corbin Simpsona524aab2009-12-20 19:41:50 -080070* ``set_polygon_stipple``
Zack Rusineaabb4e2013-05-24 16:08:39 -040071* ``set_scissor_states`` sets the bounds for the scissor test, which culls
Corbin Simpson8cf1af42010-01-25 01:12:30 -080072 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
Keith Whitwellbc3cff22010-08-20 11:38:33 +010074 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
Zack Rusineaabb4e2013-05-24 16:08:39 -040076 and y would be [xmin..xmax-1] and [ymin..ymax-1]. The number of scissors
77 should be the same as the number of set viewports and can be up to
78 PIPE_MAX_VIEWPORTS.
79* ``set_viewport_states``
Ilia Mirkin82fab732016-06-11 11:35:01 -040080* ``set_window_rectangles`` sets the window rectangles to be used for
81 rendering, as defined by GL_EXT_window_rectangles. There are two
82 modes - include and exclude, which define whether the supplied
83 rectangles are to be used for including fragments or excluding
84 them. All of the rectangles are ORed together, so in exclude mode,
85 any fragment inside any rectangle would be culled, while in include
86 mode, any fragment outside all rectangles would be culled. xmin/ymin
87 are inclusive, while xmax/ymax are exclusive (same as scissor states
88 above). Note that this only applies to draws, not clears or
89 blits. (Blits have their own way to pass the requisite rectangles
90 in.)
Ilia Mirkin6b262062014-07-20 11:36:49 -040091* ``set_tess_state`` configures the default tessellation parameters:
Eric Engestrom3a0d2c52017-02-21 14:15:35 +000092
Ilia Mirkin6b262062014-07-20 11:36:49 -040093 * ``default_outer_level`` is the default value for the outer tessellation
94 levels. This corresponds to GL's ``PATCH_DEFAULT_OUTER_LEVEL``.
95 * ``default_inner_level`` is the default value for the inner tessellation
96 levels. This corresponds to GL's ``PATCH_DEFAULT_INNER_LEVEL``.
Eric Engestrom3a0d2c52017-02-21 14:15:35 +000097
Ilia Mirkinfc76cc02015-10-30 03:17:35 -040098* ``set_debug_callback`` sets the callback to be used for reporting
99 various debug messages, eventually reported via KHR_debug and
100 similar mechanisms.
Corbin Simpsona524aab2009-12-20 19:41:50 -0800101
Brian Paulf4091e12017-06-30 09:23:17 -0600102Samplers
103^^^^^^^^
104
105pipe_sampler_state objects control how textures are sampled (coordinate
106wrap modes, interpolation modes, etc). Note that samplers are not used
107for texture buffer objects. That is, pipe_context::bind_sampler_views()
108will not bind a sampler if the corresponding sampler view refers to a
109PIPE_BUFFER resource.
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000110
Michal Krole4b8a302010-03-16 10:58:33 +0100111Sampler Views
112^^^^^^^^^^^^^
113
114These are the means to bind textures to shader stages. To create one, specify
115its format, swizzle and LOD range in sampler view template.
116
117If texture format is different than template format, it is said the texture
118is being cast to another format. Casting can be done only between compatible
119formats, that is formats that have matching component order and sizes.
120
Gwan-gyeong Mundb91b852017-08-26 17:39:04 +0900121Swizzle fields specify the way in which fetched texel components are placed
Michal Krol980da4a2010-03-19 09:08:33 +0100122in the result register. For example, ``swizzle_r`` specifies what is going to be
123placed in first component of result register.
Michal Krole4b8a302010-03-16 10:58:33 +0100124
Michal Krol980da4a2010-03-19 09:08:33 +0100125The ``first_level`` and ``last_level`` fields of sampler view template specify
Roland Scheidegger4c700142010-12-02 04:33:43 +0100126the LOD range the texture is going to be constrained to. Note that these
127values are in addition to the respective min_lod, max_lod values in the
128pipe_sampler_state (that is if min_lod is 2.0, and first_level 3, the first mip
129level used for sampling from the resource is effectively the fifth).
130
131The ``first_layer`` and ``last_layer`` fields specify the layer range the
132texture is going to be constrained to. Similar to the LOD range, this is added
133to the array index which is used for sampling.
Michal Krole4b8a302010-03-16 10:58:33 +0100134
Brian Paula3ed98f2013-10-07 18:16:22 -0600135* ``set_sampler_views`` binds an array of sampler views to a shader stage.
136 Every binding point acquires a reference
Michal Krole4b8a302010-03-16 10:58:33 +0100137 to a respective sampler view and releases a reference to the previous
Brian Paula3ed98f2013-10-07 18:16:22 -0600138 sampler view.
Michal Krole4b8a302010-03-16 10:58:33 +0100139
Michal Krol980da4a2010-03-19 09:08:33 +0100140* ``create_sampler_view`` creates a new sampler view. ``texture`` is associated
Michal Krole4b8a302010-03-16 10:58:33 +0100141 with the sampler view which results in sampler view holding a reference
142 to the texture. Format specified in template must be compatible
143 with texture format.
144
145* ``sampler_view_destroy`` destroys a sampler view and releases its reference
146 to associated texture.
147
Francisco Jerez5f55cbc2012-05-01 02:47:03 +0200148Shader Resources
149^^^^^^^^^^^^^^^^
150
151Shader resources are textures or buffers that may be read or written
152from a shader without an associated sampler. This means that they
153have no support for floating point coordinates, address wrap modes or
154filtering.
155
Marek Olšák05a12c52015-07-05 14:48:33 +0200156There are 2 types of shader resources: buffers and images.
157
158Buffers are specified using the ``set_shader_buffers`` method.
159
160Images are specified using the ``set_shader_images`` method. When binding
161images, the ``level``, ``first_layer`` and ``last_layer`` pipe_image_view
162fields specify the mipmap level and the range of layers the image will be
163constrained to.
Francisco Jerez5f55cbc2012-05-01 02:47:03 +0200164
Roland Scheidegger4c700142010-12-02 04:33:43 +0100165Surfaces
166^^^^^^^^
167
168These are the means to use resources as color render targets or depthstencil
169attachments. To create one, specify the mip level, the range of layers, and
170the bind flags (either PIPE_BIND_DEPTH_STENCIL or PIPE_BIND_RENDER_TARGET).
171Note that layer values are in addition to what is indicated by the geometry
172shader output variable XXX_FIXME (that is if first_layer is 3 and geometry
173shader indicates index 2, the 5th layer of the resource will be used). These
174first_layer and last_layer parameters will only be used for 1d array, 2d array,
175cube, and 3d textures otherwise they are 0.
176
177* ``create_surface`` creates a new surface.
178
179* ``surface_destroy`` destroys a surface and releases its reference to the
180 associated resource.
Michal Krole4b8a302010-03-16 10:58:33 +0100181
Marek Olšák861a0292011-12-15 18:42:21 +0100182Stream output targets
183^^^^^^^^^^^^^^^^^^^^^
184
185Stream output, also known as transform feedback, allows writing the primitives
186produced by the vertex pipeline to buffers. This is done after the geometry
187shader or vertex shader if no geometry shader is present.
188
189The stream output targets are views into buffer resources which can be bound
190as stream outputs and specify a memory range where it's valid to write
191primitives. The pipe driver must implement memory protection such that any
192primitives written outside of the specified memory range are discarded.
193
194Two stream output targets can use the same resource at the same time, but
195with a disjoint memory range.
196
197Additionally, the stream output target internally maintains the offset
198into the buffer which is incremented everytime something is written to it.
199The internal offset is equal to how much data has already been written.
200It can be stored in device memory and the CPU actually doesn't have to query
201it.
202
203The stream output target can be used in a draw command to provide
204the vertex count. The vertex count is derived from the internal offset
205discussed above.
206
207* ``create_stream_output_target`` create a new target.
208
209* ``stream_output_target_destroy`` destroys a target. Users of this should
210 use pipe_so_target_reference instead.
211
212* ``set_stream_output_targets`` binds stream output targets. The parameter
Zack Rusindfa25ea2014-03-06 18:43:44 -0500213 offset is an array which specifies the internal offset of the buffer. The
214 internal offset is, besides writing, used for reading the data during the
215 draw_auto stage, i.e. it specifies how much data there is in the buffer
216 for the purposes of the draw_auto stage. -1 means the buffer should
217 be appended to, and everything else sets the internal offset.
Marek Olšák861a0292011-12-15 18:42:21 +0100218
219NOTE: The currently-bound vertex or geometry shader must be compiled with
220the properly-filled-in structure pipe_stream_output_info describing which
221outputs should be written to buffers and how. The structure is part of
222pipe_shader_state.
223
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000224Clearing
225^^^^^^^^
226
Roland Scheidegger0cd70b52010-05-28 23:57:47 +0200227Clear is one of the most difficult concepts to nail down to a single
228interface (due to both different requirements from APIs and also driver/hw
229specific differences).
230
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000231``clear`` initializes some or all of the surfaces currently bound to
232the framebuffer to particular RGBA, depth, or stencil values.
Roland Scheidegger0cd70b52010-05-28 23:57:47 +0200233Currently, this does not take into account color or stencil write masks (as
234used by GL), and always clears the whole surfaces (no scissoring as used by
235GL clear or explicit rectangles like d3d9 uses). It can, however, also clear
Marek Olšákf04dd3d2013-01-14 06:58:52 +0100236only depth or stencil in a combined depth/stencil surface.
Roland Scheidegger4c700142010-12-02 04:33:43 +0100237If a surface includes several layers then all layers will be cleared.
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000238
Roland Scheideggera6e5c6c2010-06-03 16:33:25 +0200239``clear_render_target`` clears a single color rendertarget with the specified
240color value. While it is only possible to clear one surface at a time (which can
Roland Scheidegger0cd70b52010-05-28 23:57:47 +0200241include several layers), this surface need not be bound to the framebuffer.
Brian Paulc87e8c82016-08-31 10:03:53 -0600242If render_condition_enabled is false, any current rendering condition is ignored
243and the clear will be unconditional.
Roland Scheidegger0cd70b52010-05-28 23:57:47 +0200244
Corbin Simpson517a4fb2010-06-16 11:10:46 -0700245``clear_depth_stencil`` clears a single depth, stencil or depth/stencil surface
Roland Scheideggera6e5c6c2010-06-03 16:33:25 +0200246with the specified depth and stencil values (for combined depth/stencil buffers,
Giuseppe Bilotta60a27ad2016-06-23 19:20:18 +0200247it is also possible to only clear one or the other part). While it is only
Roland Scheidegger0cd70b52010-05-28 23:57:47 +0200248possible to clear one surface at a time (which can include several layers),
249this surface need not be bound to the framebuffer.
Brian Paulc87e8c82016-08-31 10:03:53 -0600250If render_condition_enabled is false, any current rendering condition is ignored
251and the clear will be unconditional.
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000252
Ilia Mirkin3695b252015-11-09 13:27:07 -0500253``clear_texture`` clears a non-PIPE_BUFFER resource's specified level
254and bounding box with a clear value provided in that resource's native
255format.
256
Ilia Mirkin24b86cb2014-03-25 17:10:54 -0400257``clear_buffer`` clears a PIPE_BUFFER resource with the specified clear value
258(which may be multiple bytes in length). Logically this is a memset with a
259multi-byte element value starting at offset bytes from resource start, going
260for size bytes. It is guaranteed that size % clear_value_size == 0.
261
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000262
Marek Olšákd71bc0d2017-01-26 22:24:13 +0100263Uploading
264^^^^^^^^^
265
266For simple single-use uploads, use ``pipe_context::stream_uploader`` or
267``pipe_context::const_uploader``. The latter should be used for uploading
268constants, while the former should be used for uploading everything else.
269PIPE_USAGE_STREAM is implied in both cases, so don't use the uploaders
270for static allocations.
271
272Usage:
273
274Call u_upload_alloc or u_upload_data as many times as you want. After you are
275done, call u_upload_unmap. If the driver doesn't support persistent mappings,
276u_upload_unmap makes sure the previously mapped memory is unmapped.
277
278Gotchas:
279- Always fill the memory immediately after u_upload_alloc. Any following call
280to u_upload_alloc and u_upload_data can unmap memory returned by previous
281u_upload_alloc.
282- Don't interleave calls using stream_uploader and const_uploader. If you use
283one of them, do the upload, unmap, and only then can you use the other one.
284
285
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000286Drawing
Corbin Simpsona524aab2009-12-20 19:41:50 -0800287^^^^^^^
288
Chia-I Wue7f69c42010-07-17 22:00:04 +0800289``draw_vbo`` draws a specified primitive. The primitive mode and other
290properties are described by ``pipe_draw_info``.
Corbin Simpsona524aab2009-12-20 19:41:50 -0800291
Chia-I Wue7f69c42010-07-17 22:00:04 +0800292The ``mode``, ``start``, and ``count`` fields of ``pipe_draw_info`` specify the
293the mode of the primitive and the vertices to be fetched, in the range between
294``start`` to ``start``+``count``-1, inclusive.
Michal Krolffd28482010-01-14 18:55:52 +0100295
Chia-I Wue7f69c42010-07-17 22:00:04 +0800296Every instance with instanceID in the range between ``start_instance`` and
297``start_instance``+``instance_count``-1, inclusive, will be drawn.
Michal Krolffd28482010-01-14 18:55:52 +0100298
Marek Olšák330d0602017-04-02 16:24:39 +0200299If ``index_size`` != 0, all vertex indices will be looked up from the index
300buffer.
José Fonsecabb78f6a2011-04-16 10:18:20 +0100301
302In indexed draw, ``min_index`` and ``max_index`` respectively provide a lower
303and upper bound of the indices contained in the index buffer inside the range
304between ``start`` to ``start``+``count``-1. This allows the driver to
305determine which subset of vertices will be referenced during te draw call
306without having to scan the index buffer. Providing a over-estimation of the
307the true bounds, for example, a ``min_index`` and ``max_index`` of 0 and
3080xffffffff respectively, must give exactly the same rendering, albeit with less
309performance due to unreferenced vertex buffers being unnecessarily DMA'ed or
310processed. Providing a underestimation of the true bounds will result in
311undefined behavior, but should not result in program or system failure.
312
313In case of non-indexed draw, ``min_index`` should be set to
Chia-I Wue7f69c42010-07-17 22:00:04 +0800314``start`` and ``max_index`` should be set to ``start``+``count``-1.
Corbin Simpsona524aab2009-12-20 19:41:50 -0800315
José Fonsecabb78f6a2011-04-16 10:18:20 +0100316``index_bias`` is a value added to every vertex index after lookup and before
317fetching vertex attributes.
José Fonseca493a1bb2010-04-20 10:22:28 +0200318
Brian Pauladf35e82010-10-21 19:03:38 -0600319When drawing indexed primitives, the primitive restart index can be
320used to draw disjoint primitive strips. For example, several separate
321line strips can be drawn by designating a special index value as the
322restart index. The ``primitive_restart`` flag enables/disables this
323feature. The ``restart_index`` field specifies the restart index value.
324
325When primitive restart is in use, array indexes are compared to the
326restart index before adding the index_bias offset.
327
Michal Krolffd28482010-01-14 18:55:52 +0100328If a given vertex element has ``instance_divisor`` set to 0, it is said
329it contains per-vertex data and effective vertex attribute address needs
330to be recalculated for every index.
331
332 attribAddr = ``stride`` * index + ``src_offset``
333
334If a given vertex element has ``instance_divisor`` set to non-zero,
335it is said it contains per-instance data and effective vertex attribute
336address needs to recalculated for every ``instance_divisor``-th instance.
337
338 attribAddr = ``stride`` * instanceID / ``instance_divisor`` + ``src_offset``
339
340In the above formulas, ``src_offset`` is taken from the given vertex element
341and ``stride`` is taken from a vertex buffer associated with the given
342vertex element.
343
344The calculated attribAddr is used as an offset into the vertex buffer to
345fetch the attribute data.
346
347The value of ``instanceID`` can be read in a vertex shader through a system
348value register declared with INSTANCEID semantic name.
349
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000350
351Queries
352^^^^^^^
353
354Queries gather some statistic from the 3D pipeline over one or more
José Fonseca44a8e512013-03-11 10:13:47 +0000355draws. Queries may be nested, though not all state trackers exercise this.
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000356
357Queries can be created with ``create_query`` and deleted with
Brian Paul98f3f1c2010-01-29 12:36:26 -0700358``destroy_query``. To start a query, use ``begin_query``, and when finished,
359use ``end_query`` to end the query.
360
Ilia Mirkin43e4b3e2014-06-26 19:33:07 -0400361``create_query`` takes a query type (``PIPE_QUERY_*``), as well as an index,
362which is the vertex stream for ``PIPE_QUERY_PRIMITIVES_GENERATED`` and
363``PIPE_QUERY_PRIMITIVES_EMITTED``, and allocates a query structure.
364
Rob Clark20952202014-05-12 22:19:03 -0400365``begin_query`` will clear/reset previous query results.
366
Brian Paul98f3f1c2010-01-29 12:36:26 -0700367``get_query_result`` is used to retrieve the results of a query. If
368the ``wait`` parameter is TRUE, then the ``get_query_result`` call
369will block until the results of the query are ready (and TRUE will be
370returned). Otherwise, if the ``wait`` parameter is FALSE, the call
371will not block and the return value will be TRUE if the query has
372completed or FALSE otherwise.
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000373
Ilia Mirkin40d7f022015-05-02 20:28:11 -0400374``get_query_result_resource`` is used to store the result of a query into
375a resource without synchronizing with the CPU. This write will optionally
376wait for the query to complete, and will optionally write whether the value
377is available instead of the value itself.
378
Marek Olšák26171bd2016-04-08 01:42:00 +0200379``set_active_query_state`` Set whether all current non-driver queries except
380TIME_ELAPSED are active or paused.
381
Christoph Bumiller10f67c02011-10-20 18:03:23 +0200382The interface currently includes the following types of queries:
383
384``PIPE_QUERY_OCCLUSION_COUNTER`` counts the number of fragments which
Corbin Simpsonf1cf6b02010-05-17 12:00:59 -0700385are written to the framebuffer without being culled by
Ilia Mirkin05d02232014-03-31 18:08:07 -0400386:ref:`depth-stencil-alpha` testing or shader KILL instructions.
Brian Paul34613c62011-01-18 16:34:22 -0700387The result is an unsigned 64-bit integer.
Christoph Bumiller10f67c02011-10-20 18:03:23 +0200388This query can be used with ``render_condition``.
389
Zack Rusin0657fc02011-01-26 00:01:51 -0500390In cases where a boolean result of an occlusion query is enough,
391``PIPE_QUERY_OCCLUSION_PREDICATE`` should be used. It is just like
392``PIPE_QUERY_OCCLUSION_COUNTER`` except that the result is a boolean
393value of FALSE for cases where COUNTER would result in 0 and TRUE
394for all other cases.
Christoph Bumiller10f67c02011-10-20 18:03:23 +0200395This query can be used with ``render_condition``.
Corbin Simpsonf1cf6b02010-05-17 12:00:59 -0700396
Nicolai Hähnle3f6b3d92017-09-12 18:46:46 +0200397In cases where a conservative approximation of an occlusion query is enough,
398``PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE`` should be used. It behaves
399like ``PIPE_QUERY_OCCLUSION_PREDICATE``, except that it may return TRUE in
400additional, implementation-dependent cases.
401This query can be used with ``render_condition``.
402
Christoph Bumiller10f67c02011-10-20 18:03:23 +0200403``PIPE_QUERY_TIME_ELAPSED`` returns the amount of time, in nanoseconds,
404the context takes to perform operations.
Brian Paul34613c62011-01-18 16:34:22 -0700405The result is an unsigned 64-bit integer.
Corbin Simpsonf1cf6b02010-05-17 12:00:59 -0700406
Christoph Bumiller10f67c02011-10-20 18:03:23 +0200407``PIPE_QUERY_TIMESTAMP`` returns a device/driver internal timestamp,
408scaled to nanoseconds, recorded after all commands issued prior to
409``end_query`` have been processed.
410This query does not require a call to ``begin_query``.
411The result is an unsigned 64-bit integer.
412
Roland Scheideggercdf89d02013-06-19 23:25:39 +0200413``PIPE_QUERY_TIMESTAMP_DISJOINT`` can be used to check the
414internal timer resolution and whether the timestamp counter has become
415unreliable due to things like throttling etc. - only if this is FALSE
416a timestamp query (within the timestamp_disjoint query) should be trusted.
Christoph Bumiller10f67c02011-10-20 18:03:23 +0200417The result is a 64-bit integer specifying the timer resolution in Hz,
Roland Scheideggercdf89d02013-06-19 23:25:39 +0200418followed by a boolean value indicating whether the timestamp counter
419is discontinuous or disjoint.
Christoph Bumiller10f67c02011-10-20 18:03:23 +0200420
421``PIPE_QUERY_PRIMITIVES_GENERATED`` returns a 64-bit integer indicating
Christoph Bumillerc620aad2013-03-29 14:30:49 +0100422the number of primitives processed by the pipeline (regardless of whether
423stream output is active or not).
Christoph Bumiller10f67c02011-10-20 18:03:23 +0200424
425``PIPE_QUERY_PRIMITIVES_EMITTED`` returns a 64-bit integer indicating
426the number of primitives written to stream output buffers.
427
428``PIPE_QUERY_SO_STATISTICS`` returns 2 64-bit integers corresponding to
Christoph Bumillerc620aad2013-03-29 14:30:49 +0100429the result of
Christoph Bumiller10f67c02011-10-20 18:03:23 +0200430``PIPE_QUERY_PRIMITIVES_EMITTED`` and
Christoph Bumillerc620aad2013-03-29 14:30:49 +0100431the number of primitives that would have been written to stream output buffers
432if they had infinite space available (primitives_storage_needed), in this order.
Roland Scheideggerbd3909f2013-08-23 23:08:43 +0200433XXX the 2nd value is equivalent to ``PIPE_QUERY_PRIMITIVES_GENERATED`` but it is
434unclear if it should be increased if stream output is not active.
Christoph Bumiller10f67c02011-10-20 18:03:23 +0200435
436``PIPE_QUERY_SO_OVERFLOW_PREDICATE`` returns a boolean value indicating
Nicolai Hähnlea6777992017-07-26 19:16:14 +0200437whether a selected stream output target has overflowed as a result of the
Christoph Bumiller10f67c02011-10-20 18:03:23 +0200438commands issued between ``begin_query`` and ``end_query``.
Nicolai Hähnlea6777992017-07-26 19:16:14 +0200439This query can be used with ``render_condition``. The output stream is
440selected by the stream number passed to ``create_query``.
441
442``PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE`` returns a boolean value indicating
443whether any stream output target has overflowed as a result of the commands
444issued between ``begin_query`` and ``end_query``. This query can be used
445with ``render_condition``, and its result is the logical OR of multiple
446``PIPE_QUERY_SO_OVERFLOW_PREDICATE`` queries, one for each stream output
447target.
Christoph Bumiller10f67c02011-10-20 18:03:23 +0200448
449``PIPE_QUERY_GPU_FINISHED`` returns a boolean value indicating whether
450all commands issued before ``end_query`` have completed. However, this
451does not imply serialization.
452This query does not require a call to ``begin_query``.
453
454``PIPE_QUERY_PIPELINE_STATISTICS`` returns an array of the following
45564-bit integers:
456Number of vertices read from vertex buffers.
457Number of primitives read from vertex buffers.
458Number of vertex shader threads launched.
459Number of geometry shader threads launched.
460Number of primitives generated by geometry shaders.
461Number of primitives forwarded to the rasterizer.
462Number of primitives rasterized.
463Number of fragment shader threads launched.
464Number of tessellation control shader threads launched.
465Number of tessellation evaluation shader threads launched.
466If a shader type is not supported by the device/driver,
467the corresponding values should be set to 0.
468
Corbin Simpsonf1cf6b02010-05-17 12:00:59 -0700469Gallium does not guarantee the availability of any query types; one must
470always check the capabilities of the :ref:`Screen` first.
Brian Paul6c1549a2010-01-21 11:52:36 -0700471
472
473Conditional Rendering
474^^^^^^^^^^^^^^^^^^^^^
475
476A drawing command can be skipped depending on the outcome of a query
Roland Scheidegger793e8e32013-06-14 19:48:57 +0200477(typically an occlusion query, or streamout overflow predicate).
478The ``render_condition`` function specifies the query which should be checked
Roland Scheideggerf49e2012014-05-29 01:21:20 +0200479prior to rendering anything. Functions always honoring render_condition include
Marek Olšáka9092102016-08-09 00:37:39 +0200480(and are limited to) draw_vbo and clear.
481The blit, clear_render_target and clear_depth_stencil functions (but
482not resource_copy_region, which seems inconsistent) can also optionally honor
483the current render condition.
Brian Paul6c1549a2010-01-21 11:52:36 -0700484
485If ``render_condition`` is called with ``query`` = NULL, conditional
486rendering is disabled and drawing takes place normally.
487
488If ``render_condition`` is called with a non-null ``query`` subsequent
Roland Scheidegger793e8e32013-06-14 19:48:57 +0200489drawing commands will be predicated on the outcome of the query.
490Commands will be skipped if ``condition`` is equal to the predicate result
491(for non-boolean queries such as OCCLUSION_QUERY, zero counts as FALSE,
492non-zero as TRUE).
Brian Paul6c1549a2010-01-21 11:52:36 -0700493
494If ``mode`` is PIPE_RENDER_COND_WAIT the driver will wait for the
495query to complete before deciding whether to render.
496
497If ``mode`` is PIPE_RENDER_COND_NO_WAIT and the query has not yet
498completed, the drawing command will be executed normally. If the query
499has completed, drawing will be predicated on the outcome of the query.
500
501If ``mode`` is PIPE_RENDER_COND_BY_REGION_WAIT or
502PIPE_RENDER_COND_BY_REGION_NO_WAIT rendering will be predicated as above
Roland Scheidegger793e8e32013-06-14 19:48:57 +0200503for the non-REGION modes but in the case that an occlusion query returns
Brian Paul6c1549a2010-01-21 11:52:36 -0700504a non-zero result, regions which were occluded may be ommitted by subsequent
505drawing commands. This can result in better performance with some GPUs.
506Normally, if the occlusion query returned a non-zero result subsequent
507drawing happens normally so fragments may be generated, shaded and
508processed even where they're known to be obscured.
509
510
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000511Flushing
512^^^^^^^^
513
Corbin Simpsona524aab2009-12-20 19:41:50 -0800514``flush``
515
Marek Olšákd17b35e2016-07-15 15:44:29 +0200516PIPE_FLUSH_END_OF_FRAME: Whether the flush marks the end of frame.
517
518PIPE_FLUSH_DEFERRED: It is not required to flush right away, but it is required
Marek Olšák54272e12016-08-06 16:41:42 +0200519to return a valid fence. If fence_finish is called with the returned fence
520and the context is still unflushed, and the ctx parameter of fence_finish is
521equal to the context where the fence was created, fence_finish will flush
522the context.
Marek Olšákd17b35e2016-07-15 15:44:29 +0200523
Nicolai Hähnleea6df1c2017-10-22 17:38:47 +0200524PIPE_FLUSH_ASYNC: The flush is allowed to be asynchronous. Unlike
525``PIPE_FLUSH_DEFERRED``, the driver must still ensure that the returned fence
526will finish in finite time. However, subsequent operations in other contexts of
527the same screen are no longer guaranteed to happen after the flush. Drivers
528which use this flag must implement pipe_context::fence_server_sync.
529
530PIPE_FLUSH_HINT_FINISH: Hints to the driver that the caller will immediately
531wait for the returned fence.
532
Nicolai Hähnle1e5c9cf2017-10-22 17:38:48 +0200533Additional flags may be set together with ``PIPE_FLUSH_DEFERRED`` for even
534finer-grained fences. Note that as a general rule, GPU caches may not have been
535flushed yet when these fences are signaled. Drivers are free to ignore these
536flags and create normal fences instead. At most one of the following flags can
537be specified:
538
539PIPE_FLUSH_TOP_OF_PIPE: The fence should be signaled as soon as the next
540command is ready to start executing at the top of the pipeline, before any of
541its data is actually read (including indirect draw parameters).
542
543PIPE_FLUSH_BOTTOM_OF_PIPE: The fence should be signaled as soon as the previous
544command has finished executing on the GPU entirely (but data written by the
545command may still be in caches and inaccessible to the CPU).
546
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000547
Marek Olšák419cd5f2013-09-20 15:08:29 +0200548``flush_resource``
549
550Flush the resource cache, so that the resource can be used
551by an external client. Possible usage:
552- flushing a resource before presenting it on the screen
553- flushing a resource if some other process or device wants to use it
554This shouldn't be used to flush caches if the resource is only managed
555by a single pipe_screen and is not shared with another process.
556(i.e. you shouldn't use it to flush caches explicitly if you want to e.g.
557use the resource for texturing)
558
559
560
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000561Resource Busy Queries
562^^^^^^^^^^^^^^^^^^^^^
563
Keith Whitwell287c94e2010-04-10 16:05:54 +0100564``is_resource_referenced``
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000565
566
567
568Blitting
569^^^^^^^^
Corbin Simpsona524aab2009-12-20 19:41:50 -0800570
Roland Scheidegger379db6a2010-05-17 21:02:24 +0200571These methods emulate classic blitter controls.
Corbin Simpsona524aab2009-12-20 19:41:50 -0800572
Roland Scheideggeraac2cccc2010-04-26 19:50:57 +0200573These methods operate directly on ``pipe_resource`` objects, and stand
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000574apart from any 3D state in the context. Blitting functionality may be
575moved to a separate abstraction at some point in the future.
576
Roland Scheidegger4c700142010-12-02 04:33:43 +0100577``resource_copy_region`` blits a region of a resource to a region of another
578resource, provided that both resources have the same format, or compatible
579formats, i.e., formats for which copying the bytes from the source resource
580unmodified to the destination resource will achieve the same effect of a
581textured quad blitter.. The source and destination may be the same resource,
582but overlapping blits are not permitted.
Marek Olšákc4df2e32012-09-12 01:36:31 +0200583This can be considered the equivalent of a CPU memcpy.
584
585``blit`` blits a region of a resource to a region of another resource, including
Roland Scheideggerf49e2012014-05-29 01:21:20 +0200586scaling, format conversion, and up-/downsampling, as well as a destination clip
Ilia Mirkin82fab732016-06-11 11:35:01 -0400587rectangle (scissors) and window rectangles. It can also optionally honor the
588current render condition (but either way the blit itself never contributes
589anything to queries currently gathering data).
Marek Olšákc4df2e32012-09-12 01:36:31 +0200590As opposed to manually drawing a textured quad, this lets the pipe driver choose
591the optimal method for blitting (like using a special 2D engine), and usually
592offers, for example, accelerated stencil-only copies even where
593PIPE_CAP_SHADER_STENCIL_EXPORT is not available.
Roland Scheideggeraac2cccc2010-04-26 19:50:57 +0200594
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000595
Keith Whitwell287c94e2010-04-10 16:05:54 +0100596Transfers
597^^^^^^^^^
598
599These methods are used to get data to/from a resource.
600
Marek Olšák369e4682012-10-08 04:06:42 +0200601``transfer_map`` creates a memory mapping and the transfer object
602associated with it.
603The returned pointer points to the start of the mapped range according to
604the box region, not the beginning of the resource. If transfer_map fails,
605the returned pointer to the buffer memory is NULL, and the pointer
606to the transfer object remains unchanged (i.e. it can be non-NULL).
Keith Whitwell287c94e2010-04-10 16:05:54 +0100607
Marek Olšák369e4682012-10-08 04:06:42 +0200608``transfer_unmap`` remove the memory mapping for and destroy
609the transfer object. The pointer into the resource should be considered
610invalid and discarded.
Keith Whitwell287c94e2010-04-10 16:05:54 +0100611
Marek Olšák1ffe77e2016-07-16 21:19:48 +0200612``texture_subdata`` and ``buffer_subdata`` perform a simplified
613transfer for simple writes. Basically transfer_map, data write, and
614transfer_unmap all in one.
Keith Whitwell287c94e2010-04-10 16:05:54 +0100615
Brian Paulc5fb0512011-01-28 20:25:27 -0700616
617The box parameter to some of these functions defines a 1D, 2D or 3D
618region of pixels. This is self-explanatory for 1D, 2D and 3D texture
619targets.
620
Roland Scheidegger0f4c08a2013-06-07 20:54:54 +0200621For PIPE_TEXTURE_1D_ARRAY and PIPE_TEXTURE_2D_ARRAY, the box::z and box::depth
622fields refer to the array dimension of the texture.
Brian Paulc5fb0512011-01-28 20:25:27 -0700623
624For PIPE_TEXTURE_CUBE, the box:z and box::depth fields refer to the
625faces of the cube map (z + depth <= 6).
626
Roland Scheidegger0f4c08a2013-06-07 20:54:54 +0200627For PIPE_TEXTURE_CUBE_ARRAY, the box:z and box::depth fields refer to both
628the face and array dimension of the texture (face = z % 6, array = z / 6).
Brian Paulc5fb0512011-01-28 20:25:27 -0700629
630
Corbin Simpsonbb81f652010-05-17 12:58:29 -0700631.. _transfer_flush_region:
632
633transfer_flush_region
634%%%%%%%%%%%%%%%%%%%%%
635
636If a transfer was created with ``FLUSH_EXPLICIT``, it will not automatically
637be flushed on write or unmap. Flushes must be requested with
638``transfer_flush_region``. Flush ranges are relative to the mapped range, not
639the beginning of the resource.
640
Marek Olšák588fa882011-02-09 01:10:11 +0100641
642
Andreas Bollecb02c22012-10-23 18:29:41 +0200643.. _texture_barrier:
Marek Olšákaea4ed42011-03-08 11:32:35 +0100644
645texture_barrier
646%%%%%%%%%%%%%%%
647
648This function flushes all pending writes to the currently-set surfaces and
Ilia Mirkina1c84842017-01-01 23:42:17 -0500649invalidates all read caches of the currently-set samplers. This can be used
650for both regular textures as well as for framebuffers read via FBFETCH.
Marek Olšákaea4ed42011-03-08 11:32:35 +0100651
652
653
Marek Olšák5f61f052014-01-27 21:42:07 +0100654.. _memory_barrier:
655
656memory_barrier
657%%%%%%%%%%%%%%%
658
659This function flushes caches according to which of the PIPE_BARRIER_* flags
660are set.
661
662
663
Nicolai Hähnled6e6fa02017-02-02 21:10:44 +0100664.. _resource_commit:
665
666resource_commit
667%%%%%%%%%%%%%%%
668
669This function changes the commit state of a part of a sparse resource. Sparse
670resources are created by setting the ``PIPE_RESOURCE_FLAG_SPARSE`` flag when
671calling ``resource_create``. Initially, sparse resources only reserve a virtual
672memory region that is not backed by memory (i.e., it is uncommitted). The
673``resource_commit`` function can be called to commit or uncommit parts (or all)
674of a resource. The driver manages the underlying backing memory.
675
676The contents of newly committed memory regions are undefined. Calling this
677function to commit an already committed memory region is allowed and leaves its
678content unchanged. Similarly, calling this function to uncommit an already
679uncommitted memory region is allowed.
680
681For buffers, the given box must be aligned to multiples of
682``PIPE_CAP_SPARSE_BUFFER_PAGE_SIZE``. As an exception to this rule, if the size
683of the buffer is not a multiple of the page size, changing the commit state of
684the last (partial) page requires a box that ends at the end of the buffer
685(i.e., box->x + box->width == buffer->width0).
686
687
688
Keith Whitwell287c94e2010-04-10 16:05:54 +0100689.. _pipe_transfer:
690
691PIPE_TRANSFER
692^^^^^^^^^^^^^
693
694These flags control the behavior of a transfer object.
695
José Fonseca0562f442011-02-22 14:14:22 +0000696``PIPE_TRANSFER_READ``
697 Resource contents read back (or accessed directly) at transfer create time.
698
699``PIPE_TRANSFER_WRITE``
Marek Olšák369e4682012-10-08 04:06:42 +0200700 Resource contents will be written back at transfer_unmap time (or modified
José Fonseca0562f442011-02-22 14:14:22 +0000701 as a result of being accessed directly).
702
703``PIPE_TRANSFER_MAP_DIRECTLY``
704 a transfer should directly map the resource. May return NULL if not supported.
705
706``PIPE_TRANSFER_DISCARD_RANGE``
707 The memory within the mapped region is discarded. Cannot be used with
708 ``PIPE_TRANSFER_READ``.
709
710``PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE``
711 Discards all memory backing the resource. It should not be used with
712 ``PIPE_TRANSFER_READ``.
713
714``PIPE_TRANSFER_DONTBLOCK``
715 Fail if the resource cannot be mapped immediately.
716
717``PIPE_TRANSFER_UNSYNCHRONIZED``
718 Do not synchronize pending operations on the resource when mapping. The
719 interaction of any writes to the map and any operations pending on the
720 resource are undefined. Cannot be used with ``PIPE_TRANSFER_READ``.
721
722``PIPE_TRANSFER_FLUSH_EXPLICIT``
723 Written ranges will be notified later with :ref:`transfer_flush_region`.
724 Cannot be used with ``PIPE_TRANSFER_READ``.
Francisco Jerezd9d82dc2012-04-25 22:15:16 +0200725
Marek Olšák5f61f052014-01-27 21:42:07 +0100726``PIPE_TRANSFER_PERSISTENT``
727 Allows the resource to be used for rendering while mapped.
728 PIPE_RESOURCE_FLAG_MAP_PERSISTENT must be set when creating
729 the resource.
730 If COHERENT is not set, memory_barrier(PIPE_BARRIER_MAPPED_BUFFER)
731 must be called to ensure the device can see what the CPU has written.
732
733``PIPE_TRANSFER_COHERENT``
734 If PERSISTENT is set, this ensures any writes done by the device are
735 immediately visible to the CPU and vice versa.
736 PIPE_RESOURCE_FLAG_MAP_COHERENT must be set when creating
737 the resource.
Francisco Jerezd9d82dc2012-04-25 22:15:16 +0200738
739Compute kernel execution
740^^^^^^^^^^^^^^^^^^^^^^^^
741
742A compute program can be defined, bound or destroyed using
743``create_compute_state``, ``bind_compute_state`` or
744``destroy_compute_state`` respectively.
745
746Any of the subroutines contained within the compute program can be
747executed on the device using the ``launch_grid`` method. This method
748will execute as many instances of the program as elements in the
749specified N-dimensional grid, hopefully in parallel.
750
751The compute program has access to four special resources:
752
753* ``GLOBAL`` represents a memory space shared among all the threads
754 running on the device. An arbitrary buffer created with the
755 ``PIPE_BIND_GLOBAL`` flag can be mapped into it using the
756 ``set_global_binding`` method.
757
758* ``LOCAL`` represents a memory space shared among all the threads
759 running in the same working group. The initial contents of this
760 resource are undefined.
761
762* ``PRIVATE`` represents a memory space local to a single thread.
763 The initial contents of this resource are undefined.
764
765* ``INPUT`` represents a read-only memory space that can be
766 initialized at ``launch_grid`` time.
767
768These resources use a byte-based addressing scheme, and they can be
769accessed from the compute program by means of the LOAD/STORE TGSI
Francisco Jerez5f55cbc2012-05-01 02:47:03 +0200770opcodes. Additional resources to be accessed using the same opcodes
771may be specified by the user with the ``set_compute_resources``
772method.
Francisco Jerezd9d82dc2012-04-25 22:15:16 +0200773
774In addition, normal texture sampling is allowed from the compute
Brian Paul55e81b02013-09-12 17:31:08 -0600775program: ``bind_sampler_states`` may be used to set up texture
Brian Paula3ed98f2013-10-07 18:16:22 -0600776samplers for the compute stage and ``set_sampler_views`` may
Francisco Jerezd9d82dc2012-04-25 22:15:16 +0200777be used to bind a number of sampler views to it.
Charmaine Lee3038e892016-01-14 10:22:17 -0700778
779Mipmap generation
780^^^^^^^^^^^^^^^^^
781
782If PIPE_CAP_GENERATE_MIPMAP is true, ``generate_mipmap`` can be used
783to generate mipmaps for the specified texture resource.
784It replaces texel image levels base_level+1 through
785last_level for layers range from first_layer through last_layer.
786It returns TRUE if mipmap generation succeeds, otherwise it
787returns FALSE. Mipmap generation may fail when it is not supported
788for particular texture types or formats.
Nicolai Hähnle1a3c75e2016-09-30 12:32:02 +0200789
790Device resets
791^^^^^^^^^^^^^
792
793The state tracker can query or request notifications of when the GPU
794is reset for whatever reason (application error, driver error). When
795a GPU reset happens, the context becomes unusable and all related state
796should be considered lost and undefined. Despite that, context
797notifications are single-shot, i.e. subsequent calls to
798``get_device_reset_status`` will return PIPE_NO_RESET.
799
800* ``get_device_reset_status`` queries whether a device reset has happened
801 since the last call or since the last notification by callback.
802* ``set_device_reset_callback`` sets a callback which will be called when
803 a device reset is detected. The callback is only called synchronously.
Axel Davyc4268fd2016-12-19 20:06:51 +0100804
Samuel Pitoiset8a68b4d2017-03-29 01:34:05 +0200805Bindless
806^^^^^^^^
807
808If PIPE_CAP_BINDLESS_TEXTURE is TRUE, the following ``pipe_context`` functions
809are used to create/delete bindless handles, and to make them resident in the
810current context when they are going to be used by shaders.
811
812* ``create_texture_handle`` creates a 64-bit unsigned integer texture handle
813 that is going to be directly used in shaders.
814* ``delete_texture_handle`` deletes a 64-bit unsigned integer texture handle.
815* ``make_texture_handle_resident`` makes a 64-bit unsigned texture handle
816 resident in the current context to be accessible by shaders for texture
817 mapping.
818* ``create_image_handle`` creates a 64-bit unsigned integer image handle that
819 is going to be directly used in shaders.
820* ``delete_image_handle`` deletes a 64-bit unsigned integer image handle.
821* ``make_image_handle_resident`` makes a 64-bit unsigned integer image handle
822 resident in the current context to be accessible by shaders for image loads,
823 stores and atomic operations.
824
Axel Davyc4268fd2016-12-19 20:06:51 +0100825Using several contexts
826----------------------
827
828Several contexts from the same screen can be used at the same time. Objects
829created on one context cannot be used in another context, but the objects
830created by the screen methods can be used by all contexts.
831
832Transfers
833^^^^^^^^^
834A transfer on one context is not expected to synchronize properly with
835rendering on other contexts, thus only areas not yet used for rendering should
836be locked.
837
838A flush is required after transfer_unmap to expect other contexts to see the
839uploaded data, unless:
840
841* Using persistent mapping. Associated with coherent mapping, unmapping the
842 resource is also not required to use it in other contexts. Without coherent
843 mapping, memory_barrier(PIPE_BARRIER_MAPPED_BUFFER) should be called on the
844 context that has mapped the resource. No flush is required.
845
846* Mapping the resource with PIPE_TRANSFER_MAP_DIRECTLY.