u_blitter.h revision e0cc61bd91f5ef84bacaf5e7c6cda9eeefed478d
1/**************************************************************************
2 *
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27#ifndef U_BLITTER_H
28#define U_BLITTER_H
29
30#include "util/u_framebuffer.h"
31#include "util/u_inlines.h"
32#include "util/u_memory.h"
33
34#include "pipe/p_state.h"
35
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41struct pipe_context;
42
43enum blitter_attrib_type {
44   UTIL_BLITTER_ATTRIB_NONE,
45   UTIL_BLITTER_ATTRIB_COLOR,
46   UTIL_BLITTER_ATTRIB_TEXCOORD
47};
48
49struct blitter_context
50{
51   /**
52    * Draw a rectangle.
53    *
54    * \param x1      An X coordinate of the top-left corner.
55    * \param y1      A Y coordinate of the top-left corner.
56    * \param x2      An X coordinate of the bottom-right corner.
57    * \param y2      A Y coordinate of the bottom-right corner.
58    * \param depth   A depth which the rectangle is rendered at.
59    *
60    * \param type   Semantics of the attributes "attrib".
61    *               If type is UTIL_BLITTER_ATTRIB_NONE, ignore them.
62    *               If type is UTIL_BLITTER_ATTRIB_COLOR, the attributes
63    *               make up a constant RGBA color, and should go
64    *               to the GENERIC0 varying slot of a fragment shader.
65    *               If type is UTIL_BLITTER_ATTRIB_TEXCOORD, {a1, a2} and
66    *               {a3, a4} specify top-left and bottom-right texture
67    *               coordinates of the rectangle, respectively, and should go
68    *               to the GENERIC0 varying slot of a fragment shader.
69    *
70    * \param attrib See type.
71    *
72    * \note A driver may optionally override this callback to implement
73    *       a specialized hardware path for drawing a rectangle, e.g. using
74    *       a rectangular point sprite.
75    */
76   void (*draw_rectangle)(struct blitter_context *blitter,
77                          unsigned x1, unsigned y1, unsigned x2, unsigned y2,
78                          float depth,
79                          enum blitter_attrib_type type,
80                          const union pipe_color_union *color);
81
82   /* Whether the blitter is running. */
83   boolean running;
84
85   /* Private members, really. */
86   struct pipe_context *pipe; /**< pipe context */
87
88   void *saved_blend_state;   /**< blend state */
89   void *saved_dsa_state;     /**< depth stencil alpha state */
90   void *saved_velem_state;   /**< vertex elements state */
91   void *saved_rs_state;      /**< rasterizer state */
92   void *saved_fs, *saved_vs, *saved_gs; /**< shaders */
93
94   struct pipe_framebuffer_state saved_fb_state;  /**< framebuffer state */
95   struct pipe_stencil_ref saved_stencil_ref;     /**< stencil ref */
96   struct pipe_viewport_state saved_viewport;
97   boolean is_sample_mask_saved;
98   unsigned saved_sample_mask;
99
100   int saved_num_sampler_states;
101   void *saved_sampler_states[PIPE_MAX_SAMPLERS];
102
103   int saved_num_sampler_views;
104   struct pipe_sampler_view *saved_sampler_views[PIPE_MAX_SAMPLERS];
105
106   int saved_num_vertex_buffers;
107   struct pipe_vertex_buffer saved_vertex_buffers[PIPE_MAX_ATTRIBS];
108
109   int saved_num_so_targets;
110   struct pipe_stream_output_target *saved_so_targets[PIPE_MAX_SO_BUFFERS];
111};
112
113/**
114 * Create a blitter context.
115 */
116struct blitter_context *util_blitter_create(struct pipe_context *pipe);
117
118/**
119 * Destroy a blitter context.
120 */
121void util_blitter_destroy(struct blitter_context *blitter);
122
123/**
124 * Return the pipe context associated with a blitter context.
125 */
126static INLINE
127struct pipe_context *util_blitter_get_pipe(struct blitter_context *blitter)
128{
129   return blitter->pipe;
130}
131
132/*
133 * These states must be saved before any of the following functions are called:
134 * - vertex buffers
135 * - vertex elements
136 * - vertex shader
137 * - geometry shader (if supported)
138 * - stream output targets (if supported)
139 * - rasterizer state
140 */
141
142/**
143 * Clear a specified set of currently bound buffers to specified values.
144 *
145 * These states must be saved in the blitter in addition to the state objects
146 * already required to be saved:
147 * - fragment shader
148 * - depth stencil alpha state
149 * - blend state
150 */
151void util_blitter_clear(struct blitter_context *blitter,
152                        unsigned width, unsigned height,
153                        unsigned num_cbufs,
154                        unsigned clear_buffers,
155                        enum pipe_format cbuf_format,
156                        const union pipe_color_union *color,
157                        double depth, unsigned stencil);
158
159/**
160 * Check if the blitter (with the help of the driver) can blit between
161 * the two resources.
162 * The mask is a combination of the PIPE_MASK_* flags.
163 * Set to PIPE_MASK_RGBAZS if unsure.
164 */
165boolean util_blitter_is_copy_supported(struct blitter_context *blitter,
166                                       const struct pipe_resource *dst,
167                                       const struct pipe_resource *src,
168                                       unsigned mask);
169/**
170 * Copy a block of pixels from one surface to another.
171 *
172 * You can copy from any color format to any other color format provided
173 * the former can be sampled from and the latter can be rendered to. Otherwise,
174 * a software fallback path is taken and both surfaces must be of the same
175 * format.
176 *
177 * Only one sample of a multisample texture can be copied and is specified by
178 * src_sample. If the destination is a multisample resource, dst_sample_mask
179 * specifies the sample mask. For single-sample resources, set dst_sample_mask
180 * to ~0.
181 *
182 * These states must be saved in the blitter in addition to the state objects
183 * already required to be saved:
184 * - fragment shader
185 * - depth stencil alpha state
186 * - blend state
187 * - fragment sampler states
188 * - fragment sampler textures
189 * - framebuffer state
190 */
191void util_blitter_copy_texture(struct blitter_context *blitter,
192                               struct pipe_resource *dst,
193                               unsigned dst_level, unsigned dst_sample_mask,
194                               unsigned dstx, unsigned dsty, unsigned dstz,
195                               struct pipe_resource *src,
196                               unsigned src_level, unsigned src_sample,
197                               const struct pipe_box *srcbox);
198
199/**
200 * Same as util_blitter_copy_texture, but dst and src are pipe_surface and
201 * pipe_sampler_view, respectively. The mipmap level and dstz are part of
202 * the views.
203 *
204 * Drivers can use this to change resource properties (like format, width,
205 * height) by changing how the views interpret them, instead of changing
206 * pipe_resource directly. This is usually needed to accelerate copying of
207 * compressed formats.
208 *
209 * src_width0 and src_height0 are sampler_view-private properties that
210 * override pipe_resource. The blitter uses them for computation of texture
211 * coordinates. The dst dimensions are supplied through pipe_surface::width
212 * and height.
213 *
214 * The mask is a combination of the PIPE_MASK_* flags.
215 * Set to PIPE_MASK_RGBAZS if unsure.
216 *
217 * NOTE: There are no checks whether the blit is actually supported.
218 */
219void util_blitter_copy_texture_view(struct blitter_context *blitter,
220                                    struct pipe_surface *dst,
221                                    unsigned dst_sample_mask,
222                                    unsigned dstx, unsigned dsty,
223                                    struct pipe_sampler_view *src,
224                                    unsigned src_sample,
225                                    const struct pipe_box *srcbox,
226                                    unsigned src_width0, unsigned src_height0,
227                                    unsigned mask);
228
229/**
230 * Helper function to initialize a view for copy_texture_view.
231 * The parameters must match copy_texture_view.
232 */
233void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
234                                      struct pipe_resource *dst,
235                                      unsigned dstlevel,
236                                      unsigned dstz,
237                                      const struct pipe_box *srcbox);
238
239/**
240 * Helper function to initialize a view for copy_texture_view.
241 * The parameters must match copy_texture_view.
242 */
243void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
244                                      struct pipe_resource *src,
245                                      unsigned srclevel);
246
247/**
248 * Copy data from one buffer to another using the Stream Output functionality.
249 * Some alignment is required, otherwise software fallback is used.
250 */
251void util_blitter_copy_buffer(struct blitter_context *blitter,
252                              struct pipe_resource *dst,
253                              unsigned dstx,
254                              struct pipe_resource *src,
255                              unsigned srcx,
256                              unsigned size);
257
258/**
259 * Clear a region of a (color) surface to a constant value.
260 *
261 * These states must be saved in the blitter in addition to the state objects
262 * already required to be saved:
263 * - fragment shader
264 * - depth stencil alpha state
265 * - blend state
266 * - framebuffer state
267 */
268void util_blitter_clear_render_target(struct blitter_context *blitter,
269                                      struct pipe_surface *dst,
270                                      const union pipe_color_union *color,
271                                      unsigned dstx, unsigned dsty,
272                                      unsigned width, unsigned height);
273
274/**
275 * Clear a region of a depth-stencil surface, both stencil and depth
276 * or only one of them if this is a combined depth-stencil surface.
277 *
278 * These states must be saved in the blitter in addition to the state objects
279 * already required to be saved:
280 * - fragment shader
281 * - depth stencil alpha state
282 * - blend state
283 * - framebuffer state
284 */
285void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
286                                      struct pipe_surface *dst,
287                                      unsigned clear_flags,
288                                      double depth,
289                                      unsigned stencil,
290                                      unsigned dstx, unsigned dsty,
291                                      unsigned width, unsigned height);
292
293/* The following functions are customized variants of the clear functions.
294 * Some drivers use them internally to do things like MSAA resolve
295 * and resource decompression. It usually consists of rendering a full-screen
296 * quad with a special blend or DSA state.
297 */
298
299/* Used by r300g for depth decompression. */
300void util_blitter_custom_clear_depth(struct blitter_context *blitter,
301                                     unsigned width, unsigned height,
302                                     double depth, void *custom_dsa);
303
304/* Used by r600g for depth decompression. */
305void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
306				       struct pipe_surface *zsurf,
307				       struct pipe_surface *cbsurf,
308				       unsigned sample_mask,
309				       void *dsa_stage, float depth);
310
311/* Used by r600g for MSAA color resolve. */
312void util_blitter_custom_resolve_color(struct blitter_context *blitter,
313                                       struct pipe_resource *dst,
314                                       unsigned dst_level,
315                                       unsigned dst_layer,
316                                       struct pipe_resource *src,
317                                       unsigned src_layer,
318                                       void *custom_blend);
319
320/* The functions below should be used to save currently bound constant state
321 * objects inside a driver. The objects are automatically restored at the end
322 * of the util_blitter_{clear, copy_region, fill_region} functions and then
323 * forgotten.
324 *
325 * States not listed here are not affected by util_blitter. */
326
327static INLINE
328void util_blitter_save_blend(struct blitter_context *blitter,
329                             void *state)
330{
331   blitter->saved_blend_state = state;
332}
333
334static INLINE
335void util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter,
336                                           void *state)
337{
338   blitter->saved_dsa_state = state;
339}
340
341static INLINE
342void util_blitter_save_vertex_elements(struct blitter_context *blitter,
343                                       void *state)
344{
345   blitter->saved_velem_state = state;
346}
347
348static INLINE
349void util_blitter_save_stencil_ref(struct blitter_context *blitter,
350                                   const struct pipe_stencil_ref *state)
351{
352   blitter->saved_stencil_ref = *state;
353}
354
355static INLINE
356void util_blitter_save_rasterizer(struct blitter_context *blitter,
357                                  void *state)
358{
359   blitter->saved_rs_state = state;
360}
361
362static INLINE
363void util_blitter_save_fragment_shader(struct blitter_context *blitter,
364                                       void *fs)
365{
366   blitter->saved_fs = fs;
367}
368
369static INLINE
370void util_blitter_save_vertex_shader(struct blitter_context *blitter,
371                                     void *vs)
372{
373   blitter->saved_vs = vs;
374}
375
376static INLINE
377void util_blitter_save_geometry_shader(struct blitter_context *blitter,
378                                       void *gs)
379{
380   blitter->saved_gs = gs;
381}
382
383static INLINE
384void util_blitter_save_framebuffer(struct blitter_context *blitter,
385                                   const struct pipe_framebuffer_state *state)
386{
387   blitter->saved_fb_state.nr_cbufs = 0; /* It's ~0 now, meaning it's unsaved. */
388   util_copy_framebuffer_state(&blitter->saved_fb_state, state);
389}
390
391static INLINE
392void util_blitter_save_viewport(struct blitter_context *blitter,
393                                struct pipe_viewport_state *state)
394{
395   blitter->saved_viewport = *state;
396}
397
398static INLINE
399void util_blitter_save_fragment_sampler_states(
400                  struct blitter_context *blitter,
401                  int num_sampler_states,
402                  void **sampler_states)
403{
404   assert(num_sampler_states <= Elements(blitter->saved_sampler_states));
405
406   blitter->saved_num_sampler_states = num_sampler_states;
407   memcpy(blitter->saved_sampler_states, sampler_states,
408          num_sampler_states * sizeof(void *));
409}
410
411static INLINE void
412util_blitter_save_fragment_sampler_views(struct blitter_context *blitter,
413                                         int num_views,
414                                         struct pipe_sampler_view **views)
415{
416   unsigned i;
417   assert(num_views <= Elements(blitter->saved_sampler_views));
418
419   blitter->saved_num_sampler_views = num_views;
420   for (i = 0; i < num_views; i++)
421      pipe_sampler_view_reference(&blitter->saved_sampler_views[i],
422                                  views[i]);
423}
424
425static INLINE void
426util_blitter_save_vertex_buffers(struct blitter_context *blitter,
427                                 int num_vertex_buffers,
428                                 struct pipe_vertex_buffer *vertex_buffers)
429{
430   assert(num_vertex_buffers <= Elements(blitter->saved_vertex_buffers));
431
432   blitter->saved_num_vertex_buffers = 0;
433   util_copy_vertex_buffers(blitter->saved_vertex_buffers,
434                            (unsigned*)&blitter->saved_num_vertex_buffers,
435                            vertex_buffers,
436                            num_vertex_buffers);
437}
438
439static INLINE void
440util_blitter_save_so_targets(struct blitter_context *blitter,
441                             int num_targets,
442                             struct pipe_stream_output_target **targets)
443{
444   unsigned i;
445   assert(num_targets <= Elements(blitter->saved_so_targets));
446
447   blitter->saved_num_so_targets = num_targets;
448   for (i = 0; i < num_targets; i++)
449      pipe_so_target_reference(&blitter->saved_so_targets[i],
450                               targets[i]);
451}
452
453static INLINE void
454util_blitter_save_sample_mask(struct blitter_context *blitter,
455                              unsigned sample_mask)
456{
457   blitter->is_sample_mask_saved = TRUE;
458   blitter->saved_sample_mask = sample_mask;
459}
460
461#ifdef __cplusplus
462}
463#endif
464
465#endif
466