1/**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#ifndef PIPE_CONTEXT_H
29#define PIPE_CONTEXT_H
30
31#include "p_compiler.h"
32#include "p_format.h"
33#include "p_video_enums.h"
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39
40struct pipe_blend_color;
41struct pipe_blend_state;
42struct pipe_box;
43struct pipe_clip_state;
44struct pipe_constant_buffer;
45struct pipe_depth_stencil_alpha_state;
46struct pipe_draw_info;
47struct pipe_fence_handle;
48struct pipe_framebuffer_state;
49struct pipe_index_buffer;
50struct pipe_query;
51struct pipe_poly_stipple;
52struct pipe_rasterizer_state;
53struct pipe_resolve_info;
54struct pipe_resource;
55struct pipe_sampler_state;
56struct pipe_sampler_view;
57struct pipe_scissor_state;
58struct pipe_shader_state;
59struct pipe_stencil_ref;
60struct pipe_stream_output_target;
61struct pipe_surface;
62struct pipe_vertex_buffer;
63struct pipe_vertex_element;
64struct pipe_video_buffer;
65struct pipe_video_decoder;
66struct pipe_viewport_state;
67struct pipe_compute_state;
68union pipe_color_union;
69union pipe_query_result;
70
71/**
72 * Gallium rendering context.  Basically:
73 *  - state setting functions
74 *  - VBO drawing functions
75 *  - surface functions
76 */
77struct pipe_context {
78   struct pipe_screen *screen;
79
80   void *priv;  /**< context private data (for DRI for example) */
81   void *draw;  /**< private, for draw module (temporary?) */
82
83   void (*destroy)( struct pipe_context * );
84
85   /**
86    * VBO drawing
87    */
88   /*@{*/
89   void (*draw_vbo)( struct pipe_context *pipe,
90                     const struct pipe_draw_info *info );
91   /*@}*/
92
93   /**
94    * Predicate subsequent rendering on occlusion query result
95    * \param query  the query predicate, or NULL if no predicate
96    * \param mode  one of PIPE_RENDER_COND_x
97    */
98   void (*render_condition)( struct pipe_context *pipe,
99                             struct pipe_query *query,
100                             uint mode );
101
102   /**
103    * Query objects
104    */
105   /*@{*/
106   struct pipe_query *(*create_query)( struct pipe_context *pipe,
107                                       unsigned query_type );
108
109   void (*destroy_query)(struct pipe_context *pipe,
110                         struct pipe_query *q);
111
112   void (*begin_query)(struct pipe_context *pipe, struct pipe_query *q);
113   void (*end_query)(struct pipe_context *pipe, struct pipe_query *q);
114
115   /**
116    * Get results of a query.
117    * \param wait  if true, this query will block until the result is ready
118    * \return TRUE if results are ready, FALSE otherwise
119    */
120   boolean (*get_query_result)(struct pipe_context *pipe,
121                               struct pipe_query *q,
122                               boolean wait,
123                               union pipe_query_result *result);
124   /*@}*/
125
126   /**
127    * State functions (create/bind/destroy state objects)
128    */
129   /*@{*/
130   void * (*create_blend_state)(struct pipe_context *,
131                                const struct pipe_blend_state *);
132   void   (*bind_blend_state)(struct pipe_context *, void *);
133   void   (*delete_blend_state)(struct pipe_context *, void  *);
134
135   void * (*create_sampler_state)(struct pipe_context *,
136                                  const struct pipe_sampler_state *);
137   void   (*bind_fragment_sampler_states)(struct pipe_context *,
138                                          unsigned num_samplers,
139                                          void **samplers);
140   void   (*bind_vertex_sampler_states)(struct pipe_context *,
141                                        unsigned num_samplers,
142                                        void **samplers);
143   void   (*bind_geometry_sampler_states)(struct pipe_context *,
144                                          unsigned num_samplers,
145                                          void **samplers);
146   void   (*bind_compute_sampler_states)(struct pipe_context *,
147                                         unsigned start_slot,
148                                         unsigned num_samplers,
149                                         void **samplers);
150   void   (*delete_sampler_state)(struct pipe_context *, void *);
151
152   void * (*create_rasterizer_state)(struct pipe_context *,
153                                     const struct pipe_rasterizer_state *);
154   void   (*bind_rasterizer_state)(struct pipe_context *, void *);
155   void   (*delete_rasterizer_state)(struct pipe_context *, void *);
156
157   void * (*create_depth_stencil_alpha_state)(struct pipe_context *,
158                                        const struct pipe_depth_stencil_alpha_state *);
159   void   (*bind_depth_stencil_alpha_state)(struct pipe_context *, void *);
160   void   (*delete_depth_stencil_alpha_state)(struct pipe_context *, void *);
161
162   void * (*create_fs_state)(struct pipe_context *,
163                             const struct pipe_shader_state *);
164   void   (*bind_fs_state)(struct pipe_context *, void *);
165   void   (*delete_fs_state)(struct pipe_context *, void *);
166
167   void * (*create_vs_state)(struct pipe_context *,
168                             const struct pipe_shader_state *);
169   void   (*bind_vs_state)(struct pipe_context *, void *);
170   void   (*delete_vs_state)(struct pipe_context *, void *);
171
172   void * (*create_gs_state)(struct pipe_context *,
173                             const struct pipe_shader_state *);
174   void   (*bind_gs_state)(struct pipe_context *, void *);
175   void   (*delete_gs_state)(struct pipe_context *, void *);
176
177   void * (*create_vertex_elements_state)(struct pipe_context *,
178                                          unsigned num_elements,
179                                          const struct pipe_vertex_element *);
180   void   (*bind_vertex_elements_state)(struct pipe_context *, void *);
181   void   (*delete_vertex_elements_state)(struct pipe_context *, void *);
182
183   /*@}*/
184
185   /**
186    * Parameter-like state (or properties)
187    */
188   /*@{*/
189   void (*set_blend_color)( struct pipe_context *,
190                            const struct pipe_blend_color * );
191
192   void (*set_stencil_ref)( struct pipe_context *,
193                            const struct pipe_stencil_ref * );
194
195   void (*set_sample_mask)( struct pipe_context *,
196                            unsigned sample_mask );
197
198   void (*set_clip_state)( struct pipe_context *,
199                            const struct pipe_clip_state * );
200
201   void (*set_constant_buffer)( struct pipe_context *,
202                                uint shader, uint index,
203                                struct pipe_constant_buffer *buf );
204
205   void (*set_framebuffer_state)( struct pipe_context *,
206                                  const struct pipe_framebuffer_state * );
207
208   void (*set_polygon_stipple)( struct pipe_context *,
209				const struct pipe_poly_stipple * );
210
211   void (*set_scissor_state)( struct pipe_context *,
212                              const struct pipe_scissor_state * );
213
214   void (*set_viewport_state)( struct pipe_context *,
215                               const struct pipe_viewport_state * );
216
217   void (*set_fragment_sampler_views)(struct pipe_context *,
218                                      unsigned num_views,
219                                      struct pipe_sampler_view **);
220
221   void (*set_vertex_sampler_views)(struct pipe_context *,
222                                    unsigned num_views,
223                                    struct pipe_sampler_view **);
224
225   void (*set_geometry_sampler_views)(struct pipe_context *,
226                                      unsigned num_views,
227                                      struct pipe_sampler_view **);
228
229   void (*set_compute_sampler_views)(struct pipe_context *,
230                                     unsigned start_slot, unsigned num_views,
231                                     struct pipe_sampler_view **);
232
233   /**
234    * Bind an array of shader resources that will be used by the
235    * graphics pipeline.  Any resources that were previously bound to
236    * the specified range will be unbound after this call.
237    *
238    * \param first      first resource to bind.
239    * \param count      number of consecutive resources to bind.
240    * \param resources  array of pointers to the resources to bind, it
241    *                   should contain at least \a count elements
242    *                   unless it's NULL, in which case no new
243    *                   resources will be bound.
244    */
245   void (*set_shader_resources)(struct pipe_context *,
246                                unsigned start, unsigned count,
247                                struct pipe_surface **resources);
248
249   void (*set_vertex_buffers)( struct pipe_context *,
250                               unsigned num_buffers,
251                               const struct pipe_vertex_buffer * );
252
253   void (*set_index_buffer)( struct pipe_context *pipe,
254                             const struct pipe_index_buffer * );
255
256   /*@}*/
257
258   /**
259    * Stream output functions.
260    */
261   /*@{*/
262
263   struct pipe_stream_output_target *(*create_stream_output_target)(
264                        struct pipe_context *,
265                        struct pipe_resource *,
266                        unsigned buffer_offset,
267                        unsigned buffer_size);
268
269   void (*stream_output_target_destroy)(struct pipe_context *,
270                                        struct pipe_stream_output_target *);
271
272   void (*set_stream_output_targets)(struct pipe_context *,
273                              unsigned num_targets,
274                              struct pipe_stream_output_target **targets,
275                              unsigned append_bitmask);
276
277   /*@}*/
278
279
280   /**
281    * Resource functions for blit-like functionality
282    *
283    * If a driver supports multisampling, resource_resolve must be available.
284    */
285   /*@{*/
286
287   /**
288    * Copy a block of pixels from one resource to another.
289    * The resource must be of the same format.
290    * Resources with nr_samples > 1 are not allowed.
291    */
292   void (*resource_copy_region)(struct pipe_context *pipe,
293                                struct pipe_resource *dst,
294                                unsigned dst_level,
295                                unsigned dstx, unsigned dsty, unsigned dstz,
296                                struct pipe_resource *src,
297                                unsigned src_level,
298                                const struct pipe_box *src_box);
299
300   /**
301    * Resolve a multisampled resource into a non-multisampled one.
302    * Source and destination must be of the same format.
303    */
304   void (*resource_resolve)(struct pipe_context *pipe,
305                            const struct pipe_resolve_info *info);
306
307   /*@}*/
308
309   /**
310    * Clear the specified set of currently bound buffers to specified values.
311    * The entire buffers are cleared (no scissor, no colormask, etc).
312    *
313    * \param buffers  bitfield of PIPE_CLEAR_* values.
314    * \param color  pointer to a union of fiu array for each of r, g, b, a.
315    * \param depth  depth clear value in [0,1].
316    * \param stencil  stencil clear value
317    */
318   void (*clear)(struct pipe_context *pipe,
319                 unsigned buffers,
320                 const union pipe_color_union *color,
321                 double depth,
322                 unsigned stencil);
323
324   /**
325    * Clear a color rendertarget surface.
326    * \param color  pointer to an union of fiu array for each of r, g, b, a.
327    */
328   void (*clear_render_target)(struct pipe_context *pipe,
329                               struct pipe_surface *dst,
330                               const union pipe_color_union *color,
331                               unsigned dstx, unsigned dsty,
332                               unsigned width, unsigned height);
333
334   /**
335    * Clear a depth-stencil surface.
336    * \param clear_flags  bitfield of PIPE_CLEAR_DEPTH/STENCIL values.
337    * \param depth  depth clear value in [0,1].
338    * \param stencil  stencil clear value
339    */
340   void (*clear_depth_stencil)(struct pipe_context *pipe,
341                               struct pipe_surface *dst,
342                               unsigned clear_flags,
343                               double depth,
344                               unsigned stencil,
345                               unsigned dstx, unsigned dsty,
346                               unsigned width, unsigned height);
347
348   /** Flush draw commands
349    */
350   void (*flush)( struct pipe_context *pipe,
351                  struct pipe_fence_handle **fence );
352
353   /**
354    * Create a view on a texture to be used by a shader stage.
355    */
356   struct pipe_sampler_view * (*create_sampler_view)(struct pipe_context *ctx,
357                                                     struct pipe_resource *texture,
358                                                     const struct pipe_sampler_view *templat);
359
360   void (*sampler_view_destroy)(struct pipe_context *ctx,
361                                struct pipe_sampler_view *view);
362
363
364   /**
365    * Get a surface which is a "view" into a resource, used by
366    * render target / depth stencil stages.
367    * \param usage  bitmaks of PIPE_BIND_* flags
368    */
369   struct pipe_surface *(*create_surface)(struct pipe_context *ctx,
370                                          struct pipe_resource *resource,
371                                          const struct pipe_surface *templat);
372
373   void (*surface_destroy)(struct pipe_context *ctx,
374                           struct pipe_surface *);
375
376   /**
377    * Get a transfer object for transferring data to/from a texture.
378    *
379    * Transfers are (by default) context-private and allow uploads to be
380    * interleaved with
381    */
382   struct pipe_transfer *(*get_transfer)(struct pipe_context *,
383                                         struct pipe_resource *resource,
384                                         unsigned level,
385                                         unsigned usage,  /* a combination of PIPE_TRANSFER_x */
386                                         const struct pipe_box *);
387
388   void (*transfer_destroy)(struct pipe_context *,
389                            struct pipe_transfer *);
390
391   void *(*transfer_map)( struct pipe_context *,
392                          struct pipe_transfer *transfer );
393
394   /* If transfer was created with WRITE|FLUSH_EXPLICIT, only the
395    * regions specified with this call are guaranteed to be written to
396    * the resource.
397    */
398   void (*transfer_flush_region)( struct pipe_context *,
399				  struct pipe_transfer *transfer,
400				  const struct pipe_box *);
401
402   void (*transfer_unmap)( struct pipe_context *,
403                           struct pipe_transfer *transfer );
404
405
406   /* One-shot transfer operation with data supplied in a user
407    * pointer.  XXX: strides??
408    */
409   void (*transfer_inline_write)( struct pipe_context *,
410                                  struct pipe_resource *,
411                                  unsigned level,
412                                  unsigned usage, /* a combination of PIPE_TRANSFER_x */
413                                  const struct pipe_box *,
414                                  const void *data,
415                                  unsigned stride,
416                                  unsigned layer_stride);
417
418   /**
419    * Flush any pending framebuffer writes and invalidate texture caches.
420    */
421   void (*texture_barrier)(struct pipe_context *);
422
423   /**
424    * Creates a video decoder for a specific video codec/profile
425    */
426   struct pipe_video_decoder *(*create_video_decoder)( struct pipe_context *context,
427                                                       enum pipe_video_profile profile,
428                                                       enum pipe_video_entrypoint entrypoint,
429                                                       enum pipe_video_chroma_format chroma_format,
430                                                       unsigned width, unsigned height, unsigned max_references,
431                                                       bool expect_chunked_decode);
432
433   /**
434    * Creates a video buffer as decoding target
435    */
436   struct pipe_video_buffer *(*create_video_buffer)( struct pipe_context *context,
437                                                     const struct pipe_video_buffer *templat );
438
439   /**
440    * Compute kernel execution
441    */
442   /*@{*/
443   /**
444    * Define the compute program and parameters to be used by
445    * pipe_context::launch_grid.
446    */
447   void *(*create_compute_state)(struct pipe_context *context,
448				 const struct pipe_compute_state *);
449   void (*bind_compute_state)(struct pipe_context *, void *);
450   void (*delete_compute_state)(struct pipe_context *, void *);
451
452   /**
453    * Bind an array of shader resources that will be used by the
454    * compute program.  Any resources that were previously bound to
455    * the specified range will be unbound after this call.
456    *
457    * \param first      first resource to bind.
458    * \param count      number of consecutive resources to bind.
459    * \param resources  array of pointers to the resources to bind, it
460    *                   should contain at least \a count elements
461    *                   unless it's NULL, in which case no new
462    *                   resources will be bound.
463    */
464   void (*set_compute_resources)(struct pipe_context *,
465                                 unsigned start, unsigned count,
466                                 struct pipe_surface **resources);
467
468   /**
469    * Bind an array of buffers to be mapped into the address space of
470    * the GLOBAL resource.  Any buffers that were previously bound
471    * between [first, first + count - 1] are unbound after this call.
472    *
473    * \param first      first buffer to map.
474    * \param count      number of consecutive buffers to map.
475    * \param resources  array of pointers to the buffers to map, it
476    *                   should contain at least \a count elements
477    *                   unless it's NULL, in which case no new
478    *                   resources will be bound.
479    * \param handles    array of pointers to the memory locations that
480    *                   will be filled with the respective base
481    *                   addresses each buffer will be mapped to.  It
482    *                   should contain at least \a count elements,
483    *                   unless \a resources is NULL in which case \a
484    *                   handles should be NULL as well.
485    *
486    * Note that the driver isn't required to make any guarantees about
487    * the contents of the \a handles array being valid anytime except
488    * during the subsequent calls to pipe_context::launch_grid.  This
489    * means that the only sensible location handles[i] may point to is
490    * somewhere within the INPUT buffer itself.  This is so to
491    * accommodate implementations that lack virtual memory but
492    * nevertheless migrate buffers on the fly, leading to resource
493    * base addresses that change on each kernel invocation or are
494    * unknown to the pipe driver.
495    */
496   void (*set_global_binding)(struct pipe_context *context,
497                              unsigned first, unsigned count,
498                              struct pipe_resource **resources,
499                              uint32_t **handles);
500
501   /**
502    * Launch the compute kernel starting from instruction \a pc of the
503    * currently bound compute program.
504    *
505    * \a grid_layout and \a block_layout are arrays of size \a
506    * PIPE_COMPUTE_CAP_GRID_DIMENSION that determine the layout of the
507    * grid (in block units) and working block (in thread units) to be
508    * used, respectively.
509    *
510    * \a input will be used to initialize the INPUT resource, and it
511    * should point to a buffer of at least
512    * pipe_compute_state::req_input_mem bytes.
513    */
514   void (*launch_grid)(struct pipe_context *context,
515                       const uint *block_layout, const uint *grid_layout,
516                       uint32_t pc, const void *input);
517   /*@}*/
518};
519
520
521#ifdef __cplusplus
522}
523#endif
524
525#endif /* PIPE_CONTEXT_H */
526