blob: cdd84bf99cd116260e634afa5eaea022db812d15 [file] [log] [blame]
Corbin Simpsone7d05f12010-06-16 16:52:52 -07001.. _context:
2
Corbin Simpson8283e202009-12-20 15:28:00 -08003Context
4=======
5
6The context object represents the purest, most directly accessible, abilities
7of the device's 3D rendering pipeline.
8
9Methods
10-------
11
Corbin Simpsona524aab2009-12-20 19:41:50 -080012CSO State
13^^^^^^^^^
14
15All CSO state is created, bound, and destroyed, with triplets of methods that
16all follow a specific naming scheme. For example, ``create_blend_state``,
17``bind_blend_state``, and ``destroy_blend_state``.
18
19CSO objects handled by the context object:
20
21* :ref:`Blend`: ``*_blend_state``
22* :ref:`Sampler`: These are special; they can be bound to either vertex or
23 fragment samplers, and they are bound in groups.
24 ``bind_fragment_sampler_states``, ``bind_vertex_sampler_states``
25* :ref:`Rasterizer`: ``*_rasterizer_state``
26* :ref:`Depth, Stencil, & Alpha`: ``*_depth_stencil_alpha_state``
27* :ref:`Shader`: These have two sets of methods. ``*_fs_state`` is for
28 fragment shaders, and ``*_vs_state`` is for vertex shaders.
Roland Scheidegger8397c802010-03-01 18:42:47 +010029* :ref:`Vertex Elements`: ``*_vertex_elements_state``
Corbin Simpsona524aab2009-12-20 19:41:50 -080030
Keith Whitwellf3347fe2009-12-21 23:44:32 +000031
32Resource Binding State
33^^^^^^^^^^^^^^^^^^^^^^
34
35This state describes how resources in various flavours (textures,
36buffers, surfaces) are bound to the driver.
37
38
Roland Scheideggerbf575b62010-01-15 18:25:14 +010039* ``set_constant_buffer`` sets a constant buffer to be used for a given shader
40 type. index is used to indicate which buffer to set (some apis may allow
41 multiple ones to be set, and binding a specific one later, though drivers
42 are mostly restricted to the first one right now).
43
Keith Whitwellf3347fe2009-12-21 23:44:32 +000044* ``set_framebuffer_state``
Michal Krole81caad2010-02-25 15:33:15 +010045
Keith Whitwellf3347fe2009-12-21 23:44:32 +000046* ``set_vertex_buffers``
47
Chia-I Wue7f69c42010-07-17 22:00:04 +080048* ``set_index_buffer``
Keith Whitwellf3347fe2009-12-21 23:44:32 +000049
Corbin Simpsona524aab2009-12-20 19:41:50 -080050Non-CSO State
51^^^^^^^^^^^^^
52
53These pieces of state are too small, variable, and/or trivial to have CSO
54objects. They all follow simple, one-method binding calls, e.g.
Roland Scheidegger98f8c4d02010-02-09 21:48:43 +010055``set_blend_color``.
Corbin Simpson8e1768c2010-03-19 00:07:55 -070056
Roland Scheidegger98f8c4d02010-02-09 21:48:43 +010057* ``set_stencil_ref`` sets the stencil front and back reference values
58 which are used as comparison values in stencil test.
Corbin Simpsona524aab2009-12-20 19:41:50 -080059* ``set_blend_color``
Roland Scheideggeraac2cccc2010-04-26 19:50:57 +020060* ``set_sample_mask``
Corbin Simpsona524aab2009-12-20 19:41:50 -080061* ``set_clip_state``
Corbin Simpsona524aab2009-12-20 19:41:50 -080062* ``set_polygon_stipple``
Corbin Simpson8cf1af42010-01-25 01:12:30 -080063* ``set_scissor_state`` sets the bounds for the scissor test, which culls
64 pixels before blending to render targets. If the :ref:`Rasterizer` does
65 not have the scissor test enabled, then the scissor bounds never need to
Keith Whitwellbc3cff22010-08-20 11:38:33 +010066 be set since they will not be used. Note that scissor xmin and ymin are
67 inclusive, but xmax and ymax are exclusive. The inclusive ranges in x
68 and y would be [xmin..xmax-1] and [ymin..ymax-1].
Corbin Simpsona524aab2009-12-20 19:41:50 -080069* ``set_viewport_state``
Corbin Simpsona524aab2009-12-20 19:41:50 -080070
Keith Whitwellf3347fe2009-12-21 23:44:32 +000071
Michal Krole4b8a302010-03-16 10:58:33 +010072Sampler Views
73^^^^^^^^^^^^^
74
75These are the means to bind textures to shader stages. To create one, specify
76its format, swizzle and LOD range in sampler view template.
77
78If texture format is different than template format, it is said the texture
79is being cast to another format. Casting can be done only between compatible
80formats, that is formats that have matching component order and sizes.
81
82Swizzle fields specify they way in which fetched texel components are placed
Michal Krol980da4a2010-03-19 09:08:33 +010083in the result register. For example, ``swizzle_r`` specifies what is going to be
84placed in first component of result register.
Michal Krole4b8a302010-03-16 10:58:33 +010085
Michal Krol980da4a2010-03-19 09:08:33 +010086The ``first_level`` and ``last_level`` fields of sampler view template specify
Roland Scheidegger4c700142010-12-02 04:33:43 +010087the LOD range the texture is going to be constrained to. Note that these
88values are in addition to the respective min_lod, max_lod values in the
89pipe_sampler_state (that is if min_lod is 2.0, and first_level 3, the first mip
90level used for sampling from the resource is effectively the fifth).
91
92The ``first_layer`` and ``last_layer`` fields specify the layer range the
93texture is going to be constrained to. Similar to the LOD range, this is added
94to the array index which is used for sampling.
Michal Krole4b8a302010-03-16 10:58:33 +010095
96* ``set_fragment_sampler_views`` binds an array of sampler views to
97 fragment shader stage. Every binding point acquires a reference
98 to a respective sampler view and releases a reference to the previous
99 sampler view.
100
101* ``set_vertex_sampler_views`` binds an array of sampler views to vertex
102 shader stage. Every binding point acquires a reference to a respective
103 sampler view and releases a reference to the previous sampler view.
104
Michal Krol980da4a2010-03-19 09:08:33 +0100105* ``create_sampler_view`` creates a new sampler view. ``texture`` is associated
Michal Krole4b8a302010-03-16 10:58:33 +0100106 with the sampler view which results in sampler view holding a reference
107 to the texture. Format specified in template must be compatible
108 with texture format.
109
110* ``sampler_view_destroy`` destroys a sampler view and releases its reference
111 to associated texture.
112
Roland Scheidegger4c700142010-12-02 04:33:43 +0100113Surfaces
114^^^^^^^^
115
116These are the means to use resources as color render targets or depthstencil
117attachments. To create one, specify the mip level, the range of layers, and
118the bind flags (either PIPE_BIND_DEPTH_STENCIL or PIPE_BIND_RENDER_TARGET).
119Note that layer values are in addition to what is indicated by the geometry
120shader output variable XXX_FIXME (that is if first_layer is 3 and geometry
121shader indicates index 2, the 5th layer of the resource will be used). These
122first_layer and last_layer parameters will only be used for 1d array, 2d array,
123cube, and 3d textures otherwise they are 0.
124
125* ``create_surface`` creates a new surface.
126
127* ``surface_destroy`` destroys a surface and releases its reference to the
128 associated resource.
Michal Krole4b8a302010-03-16 10:58:33 +0100129
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000130Clearing
131^^^^^^^^
132
Roland Scheidegger0cd70b52010-05-28 23:57:47 +0200133Clear is one of the most difficult concepts to nail down to a single
134interface (due to both different requirements from APIs and also driver/hw
135specific differences).
136
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000137``clear`` initializes some or all of the surfaces currently bound to
138the framebuffer to particular RGBA, depth, or stencil values.
Roland Scheidegger0cd70b52010-05-28 23:57:47 +0200139Currently, this does not take into account color or stencil write masks (as
140used by GL), and always clears the whole surfaces (no scissoring as used by
141GL clear or explicit rectangles like d3d9 uses). It can, however, also clear
142only depth or stencil in a combined depth/stencil surface, if the driver
143supports PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE.
Roland Scheidegger4c700142010-12-02 04:33:43 +0100144If a surface includes several layers then all layers will be cleared.
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000145
Roland Scheideggera6e5c6c2010-06-03 16:33:25 +0200146``clear_render_target`` clears a single color rendertarget with the specified
147color value. While it is only possible to clear one surface at a time (which can
Roland Scheidegger0cd70b52010-05-28 23:57:47 +0200148include several layers), this surface need not be bound to the framebuffer.
149
Corbin Simpson517a4fb2010-06-16 11:10:46 -0700150``clear_depth_stencil`` clears a single depth, stencil or depth/stencil surface
Roland Scheideggera6e5c6c2010-06-03 16:33:25 +0200151with the specified depth and stencil values (for combined depth/stencil buffers,
Roland Scheidegger0cd70b52010-05-28 23:57:47 +0200152is is also possible to only clear one or the other part). While it is only
153possible to clear one surface at a time (which can include several layers),
154this surface need not be bound to the framebuffer.
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000155
156
157Drawing
Corbin Simpsona524aab2009-12-20 19:41:50 -0800158^^^^^^^
159
Chia-I Wue7f69c42010-07-17 22:00:04 +0800160``draw_vbo`` draws a specified primitive. The primitive mode and other
161properties are described by ``pipe_draw_info``.
Corbin Simpsona524aab2009-12-20 19:41:50 -0800162
Chia-I Wue7f69c42010-07-17 22:00:04 +0800163The ``mode``, ``start``, and ``count`` fields of ``pipe_draw_info`` specify the
164the mode of the primitive and the vertices to be fetched, in the range between
165``start`` to ``start``+``count``-1, inclusive.
Michal Krolffd28482010-01-14 18:55:52 +0100166
Chia-I Wue7f69c42010-07-17 22:00:04 +0800167Every instance with instanceID in the range between ``start_instance`` and
168``start_instance``+``instance_count``-1, inclusive, will be drawn.
Michal Krolffd28482010-01-14 18:55:52 +0100169
Chia-I Wue7f69c42010-07-17 22:00:04 +0800170All vertex indices must fall inside the range given by ``min_index`` and
171``max_index``. In case non-indexed draw, ``min_index`` should be set to
172``start`` and ``max_index`` should be set to ``start``+``count``-1.
Corbin Simpsona524aab2009-12-20 19:41:50 -0800173
Chia-I Wue7f69c42010-07-17 22:00:04 +0800174``index_bias`` is a value added to every vertex index before fetching vertex
175attributes. It does not affect ``min_index`` and ``max_index``.
Corbin Simpsona524aab2009-12-20 19:41:50 -0800176
Chia-I Wue7f69c42010-07-17 22:00:04 +0800177If there is an index buffer bound, and ``indexed`` field is true, all vertex
178indices will be looked up in the index buffer. ``min_index``, ``max_index``,
179and ``index_bias`` apply after index lookup.
José Fonseca493a1bb2010-04-20 10:22:28 +0200180
Brian Pauladf35e82010-10-21 19:03:38 -0600181When drawing indexed primitives, the primitive restart index can be
182used to draw disjoint primitive strips. For example, several separate
183line strips can be drawn by designating a special index value as the
184restart index. The ``primitive_restart`` flag enables/disables this
185feature. The ``restart_index`` field specifies the restart index value.
186
187When primitive restart is in use, array indexes are compared to the
188restart index before adding the index_bias offset.
189
Michal Krolffd28482010-01-14 18:55:52 +0100190If a given vertex element has ``instance_divisor`` set to 0, it is said
191it contains per-vertex data and effective vertex attribute address needs
192to be recalculated for every index.
193
194 attribAddr = ``stride`` * index + ``src_offset``
195
196If a given vertex element has ``instance_divisor`` set to non-zero,
197it is said it contains per-instance data and effective vertex attribute
198address needs to recalculated for every ``instance_divisor``-th instance.
199
200 attribAddr = ``stride`` * instanceID / ``instance_divisor`` + ``src_offset``
201
202In the above formulas, ``src_offset`` is taken from the given vertex element
203and ``stride`` is taken from a vertex buffer associated with the given
204vertex element.
205
206The calculated attribAddr is used as an offset into the vertex buffer to
207fetch the attribute data.
208
209The value of ``instanceID`` can be read in a vertex shader through a system
210value register declared with INSTANCEID semantic name.
211
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000212
213Queries
214^^^^^^^
215
216Queries gather some statistic from the 3D pipeline over one or more
217draws. Queries may be nested, though no state tracker currently
218exercises this.
219
220Queries can be created with ``create_query`` and deleted with
Brian Paul98f3f1c2010-01-29 12:36:26 -0700221``destroy_query``. To start a query, use ``begin_query``, and when finished,
222use ``end_query`` to end the query.
223
224``get_query_result`` is used to retrieve the results of a query. If
225the ``wait`` parameter is TRUE, then the ``get_query_result`` call
226will block until the results of the query are ready (and TRUE will be
227returned). Otherwise, if the ``wait`` parameter is FALSE, the call
228will not block and the return value will be TRUE if the query has
229completed or FALSE otherwise.
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000230
Corbin Simpsonf1cf6b02010-05-17 12:00:59 -0700231The most common type of query is the occlusion query,
232``PIPE_QUERY_OCCLUSION_COUNTER``, which counts the number of fragments which
233are written to the framebuffer without being culled by
234:ref:`Depth, Stencil, & Alpha` testing or shader KILL instructions.
Brian Paul34613c62011-01-18 16:34:22 -0700235The result is an unsigned 64-bit integer.
Zack Rusin0657fc02011-01-26 00:01:51 -0500236In cases where a boolean result of an occlusion query is enough,
237``PIPE_QUERY_OCCLUSION_PREDICATE`` should be used. It is just like
238``PIPE_QUERY_OCCLUSION_COUNTER`` except that the result is a boolean
239value of FALSE for cases where COUNTER would result in 0 and TRUE
240for all other cases.
Corbin Simpsonf1cf6b02010-05-17 12:00:59 -0700241
242Another type of query, ``PIPE_QUERY_TIME_ELAPSED``, returns the amount of
Mathias Fröhlich7f19b652010-05-19 08:46:51 -0600243time, in nanoseconds, the context takes to perform operations.
Brian Paul34613c62011-01-18 16:34:22 -0700244The result is an unsigned 64-bit integer.
Corbin Simpsonf1cf6b02010-05-17 12:00:59 -0700245
246Gallium does not guarantee the availability of any query types; one must
247always check the capabilities of the :ref:`Screen` first.
Brian Paul6c1549a2010-01-21 11:52:36 -0700248
249
250Conditional Rendering
251^^^^^^^^^^^^^^^^^^^^^
252
253A drawing command can be skipped depending on the outcome of a query
254(typically an occlusion query). The ``render_condition`` function specifies
255the query which should be checked prior to rendering anything.
256
257If ``render_condition`` is called with ``query`` = NULL, conditional
258rendering is disabled and drawing takes place normally.
259
260If ``render_condition`` is called with a non-null ``query`` subsequent
261drawing commands will be predicated on the outcome of the query. If
262the query result is zero subsequent drawing commands will be skipped.
263
264If ``mode`` is PIPE_RENDER_COND_WAIT the driver will wait for the
265query to complete before deciding whether to render.
266
267If ``mode`` is PIPE_RENDER_COND_NO_WAIT and the query has not yet
268completed, the drawing command will be executed normally. If the query
269has completed, drawing will be predicated on the outcome of the query.
270
271If ``mode`` is PIPE_RENDER_COND_BY_REGION_WAIT or
272PIPE_RENDER_COND_BY_REGION_NO_WAIT rendering will be predicated as above
273for the non-REGION modes but in the case that an occulusion query returns
274a non-zero result, regions which were occluded may be ommitted by subsequent
275drawing commands. This can result in better performance with some GPUs.
276Normally, if the occlusion query returned a non-zero result subsequent
277drawing happens normally so fragments may be generated, shaded and
278processed even where they're known to be obscured.
279
280
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000281Flushing
282^^^^^^^^
283
Corbin Simpsona524aab2009-12-20 19:41:50 -0800284``flush``
285
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000286
287Resource Busy Queries
288^^^^^^^^^^^^^^^^^^^^^
289
Keith Whitwell287c94e2010-04-10 16:05:54 +0100290``is_resource_referenced``
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000291
292
293
294Blitting
295^^^^^^^^
Corbin Simpsona524aab2009-12-20 19:41:50 -0800296
Roland Scheidegger379db6a2010-05-17 21:02:24 +0200297These methods emulate classic blitter controls.
Corbin Simpsona524aab2009-12-20 19:41:50 -0800298
Roland Scheideggeraac2cccc2010-04-26 19:50:57 +0200299These methods operate directly on ``pipe_resource`` objects, and stand
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000300apart from any 3D state in the context. Blitting functionality may be
301moved to a separate abstraction at some point in the future.
302
Roland Scheidegger4c700142010-12-02 04:33:43 +0100303``resource_copy_region`` blits a region of a resource to a region of another
304resource, provided that both resources have the same format, or compatible
305formats, i.e., formats for which copying the bytes from the source resource
306unmodified to the destination resource will achieve the same effect of a
307textured quad blitter.. The source and destination may be the same resource,
308but overlapping blits are not permitted.
Roland Scheideggeraac2cccc2010-04-26 19:50:57 +0200309
310``resource_resolve`` resolves a multisampled resource into a non-multisampled
311one. Formats and dimensions must match. This function must be present if a driver
312supports multisampling.
Corbin Simpsona524aab2009-12-20 19:41:50 -0800313
Keith Whitwellf3347fe2009-12-21 23:44:32 +0000314The interfaces to these calls are likely to change to make it easier
315for a driver to batch multiple blits with the same source and
316destination.
317
Keith Whitwell287c94e2010-04-10 16:05:54 +0100318
Zack Rusin2c22b8e2010-06-01 12:47:23 -0400319Stream Output
320^^^^^^^^^^^^^
321
322Stream output, also known as transform feedback allows writing the results of the
323vertex pipeline (after the geometry shader or vertex shader if no geometry shader
324is present) to be written to a buffer created with a ``PIPE_BIND_STREAM_OUTPUT``
325flag.
326
327First a stream output state needs to be created with the
328``create_stream_output_state`` call. It specific the details of what's being written,
329to which buffer and with what kind of a writemask.
330
331Then target buffers needs to be set with the call to ``set_stream_output_buffers``
332which sets the buffers and the offsets from the start of those buffer to where
333the data will be written to.
334
335
Keith Whitwell287c94e2010-04-10 16:05:54 +0100336Transfers
337^^^^^^^^^
338
339These methods are used to get data to/from a resource.
340
341``get_transfer`` creates a transfer object.
342
343``transfer_destroy`` destroys the transfer object. May cause
344data to be written to the resource at this point.
345
346``transfer_map`` creates a memory mapping for the transfer object.
347The returned map points to the start of the mapped range according to
348the box region, not the beginning of the resource.
349
Keith Whitwell287c94e2010-04-10 16:05:54 +0100350``transfer_unmap`` remove the memory mapping for the transfer object.
351Any pointers into the map should be considered invalid and discarded.
352
353``transfer_inline_write`` performs a simplified transfer for simple writes.
354Basically get_transfer, transfer_map, data write, transfer_unmap, and
355transfer_destroy all in one.
356
Brian Paulc5fb0512011-01-28 20:25:27 -0700357
358The box parameter to some of these functions defines a 1D, 2D or 3D
359region of pixels. This is self-explanatory for 1D, 2D and 3D texture
360targets.
361
362For PIPE_TEXTURE_1D_ARRAY, the box::y and box::height fields refer to the
363array dimension of the texture.
364
365For PIPE_TEXTURE_2D_ARRAY, the box::z and box::depth fields refer to the
366array dimension of the texture.
367
368For PIPE_TEXTURE_CUBE, the box:z and box::depth fields refer to the
369faces of the cube map (z + depth <= 6).
370
371
372
Corbin Simpsonbb81f652010-05-17 12:58:29 -0700373.. _transfer_flush_region:
374
375transfer_flush_region
376%%%%%%%%%%%%%%%%%%%%%
377
378If a transfer was created with ``FLUSH_EXPLICIT``, it will not automatically
379be flushed on write or unmap. Flushes must be requested with
380``transfer_flush_region``. Flush ranges are relative to the mapped range, not
381the beginning of the resource.
382
Keith Whitwell287c94e2010-04-10 16:05:54 +0100383.. _pipe_transfer:
384
385PIPE_TRANSFER
386^^^^^^^^^^^^^
387
388These flags control the behavior of a transfer object.
389
390* ``READ``: resource contents are read at transfer create time.
391* ``WRITE``: resource contents will be written back at transfer destroy time.
392* ``MAP_DIRECTLY``: a transfer should directly map the resource. May return
393 NULL if not supported.
394* ``DISCARD``: The memory within the mapped region is discarded.
395 Cannot be used with ``READ``.
396* ``DONTBLOCK``: Fail if the resource cannot be mapped immediately.
397* ``UNSYNCHRONIZED``: Do not synchronize pending operations on the resource
398 when mapping. The interaction of any writes to the map and any
399 operations pending on the resource are undefined. Cannot be used with
400 ``READ``.
401* ``FLUSH_EXPLICIT``: Written ranges will be notified later with
Corbin Simpsonbb81f652010-05-17 12:58:29 -0700402 :ref:`transfer_flush_region`. Cannot be used with ``READ``.