u_blitter.h revision ef1bf6d69e99d9a12557a09eccaf4f2b6c628997
10511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com/**************************************************************************
23484964a86451e86dcf04be9bd8c0d76ee04f081rossberg@chromium.org *
33484964a86451e86dcf04be9bd8c0d76ee04f081rossberg@chromium.org * Copyright 2009 Marek Olšák <maraeo@gmail.com>
40511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com *
50511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com * Permission is hereby granted, free of charge, to any person obtaining a
60511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com * copy of this software and associated documentation files (the
70511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com * "Software"), to deal in the Software without restriction, including
8196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org * without limitation the rights to use, copy, modify, merge, publish,
91c09276ce2ac5214e81ca554360b9f101187893blrn@chromium.org * distribute, sub license, and/or sell copies of the Software, and to
100511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com * permit persons to whom the Software is furnished to do so, subject to
110511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com * the following conditions:
120511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com *
130511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com * The above copyright notice and this permission notice (including the
140511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com * next paragraph) shall be included in all copies or substantial portions
150511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com * of the Software.
160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com *
17196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
184b0feeef5d01dbc2948080b4f69daa37e1083461machenbach@chromium.org * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
210511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
220511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
244f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org *
254f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org **************************************************************************/
260511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
270511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#ifndef U_BLITTER_H
28fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org#define U_BLITTER_H
290511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3070d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org#include "util/u_framebuffer.h"
3170d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org#include "util/u_inlines.h"
3270d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org#include "util/u_memory.h"
337bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org
340511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#include "pipe/p_state.h"
354f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
364f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
370511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#ifdef __cplusplus
38fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.orgextern "C" {
390511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#endif
40fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org
41fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.orgstruct pipe_context;
42fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org
430511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comenum blitter_attrib_type {
440511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com   UTIL_BLITTER_ATTRIB_NONE,
450511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com   UTIL_BLITTER_ATTRIB_COLOR,
460511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com   UTIL_BLITTER_ATTRIB_TEXCOORD
470511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com};
480511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
490511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comstruct blitter_context
500511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com{
510511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com   /**
520511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    * Draw a rectangle.
530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    *
540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    * \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
159void util_blitter_clear_depth_custom(struct blitter_context *blitter,
160                                     unsigned width, unsigned height,
161                                     double depth, void *custom_dsa);
162
163/**
164 * Copy a block of pixels from one surface to another.
165 *
166 * You can copy from any color format to any other color format provided
167 * the former can be sampled from and the latter can be rendered to. Otherwise,
168 * a software fallback path is taken and both surfaces must be of the same
169 * format.
170 *
171 * The same holds for depth-stencil formats with the exception that stencil
172 * cannot be copied unless you set ignore_stencil to FALSE. In that case,
173 * a software fallback path is taken and both surfaces must be of the same
174 * format. If the shader stencil export is supported, stencil copy is always
175 * accelerated.
176 *
177 * Use pipe_screen->is_format_supported to know your options.
178 *
179 * These states must be saved in the blitter in addition to the state objects
180 * already required to be saved:
181 * - fragment shader
182 * - depth stencil alpha state
183 * - blend state
184 * - fragment sampler states
185 * - fragment sampler textures
186 * - framebuffer state
187 */
188void util_blitter_copy_texture(struct blitter_context *blitter,
189                               struct pipe_resource *dst,
190                               unsigned dst_level,
191                               unsigned dstx, unsigned dsty, unsigned dstz,
192                               struct pipe_resource *src,
193                               unsigned src_level,
194                               const struct pipe_box *srcbox,
195                               boolean ignore_stencil);
196
197/**
198 * Same as util_blitter_copy_texture, but dst and src are pipe_surface and
199 * pipe_sampler_view, respectively. The mipmap level and dstz are part of
200 * the views.
201 *
202 * Drivers can use this to change resource properties (like format, width,
203 * height) by changing how the views interpret them, instead of changing
204 * pipe_resource directly. This is usually needed to accelerate copying of
205 * compressed formats.
206 *
207 * src_width0 and src_height0 are sampler_view-private properties that
208 * override pipe_resource. The blitter uses them for computation of texture
209 * coordinates. The dst dimensions are supplied through pipe_surface::width
210 * and height.
211 *
212 * NOTE: There are no checks whether the blit is actually supported.
213 */
214void util_blitter_copy_texture_view(struct blitter_context *blitter,
215                                    struct pipe_surface *dst,
216                                    unsigned dstx, unsigned dsty,
217                                    struct pipe_sampler_view *src,
218                                    const struct pipe_box *srcbox,
219                                    unsigned src_width0, unsigned src_height0,
220                                    unsigned mask);
221
222/**
223 * Helper function to initialize a view for copy_texture_view.
224 * The parameters must match copy_texture_view.
225 */
226void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
227                                      struct pipe_resource *dst,
228                                      unsigned dstlevel,
229                                      unsigned dstz,
230                                      const struct pipe_box *srcbox);
231
232/**
233 * Helper function to initialize a view for copy_texture_view.
234 * The parameters must match copy_texture_view.
235 */
236void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
237                                      struct pipe_resource *src,
238                                      unsigned srclevel);
239
240/**
241 * Copy data from one buffer to another using the Stream Output functionality.
242 * Some alignment is required, otherwise software fallback is used.
243 */
244void util_blitter_copy_buffer(struct blitter_context *blitter,
245                              struct pipe_resource *dst,
246                              unsigned dstx,
247                              struct pipe_resource *src,
248                              unsigned srcx,
249                              unsigned size);
250
251/**
252 * Clear a region of a (color) surface to a constant value.
253 *
254 * These states must be saved in the blitter in addition to the state objects
255 * already required to be saved:
256 * - fragment shader
257 * - depth stencil alpha state
258 * - blend state
259 * - framebuffer state
260 */
261void util_blitter_clear_render_target(struct blitter_context *blitter,
262                                      struct pipe_surface *dst,
263                                      const union pipe_color_union *color,
264                                      unsigned dstx, unsigned dsty,
265                                      unsigned width, unsigned height);
266
267/**
268 * Clear a region of a depth-stencil surface, both stencil and depth
269 * or only one of them if this is a combined depth-stencil surface.
270 *
271 * These states must be saved in the blitter in addition to the state objects
272 * already required to be saved:
273 * - fragment shader
274 * - depth stencil alpha state
275 * - blend state
276 * - framebuffer state
277 */
278void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
279                                      struct pipe_surface *dst,
280                                      unsigned clear_flags,
281                                      double depth,
282                                      unsigned stencil,
283                                      unsigned dstx, unsigned dsty,
284                                      unsigned width, unsigned height);
285
286void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
287				       struct pipe_surface *zsurf,
288				       struct pipe_surface *cbsurf,
289				       void *dsa_stage, float depth);
290
291/* The functions below should be used to save currently bound constant state
292 * objects inside a driver. The objects are automatically restored at the end
293 * of the util_blitter_{clear, copy_region, fill_region} functions and then
294 * forgotten.
295 *
296 * States not listed here are not affected by util_blitter. */
297
298static INLINE
299void util_blitter_save_blend(struct blitter_context *blitter,
300                             void *state)
301{
302   blitter->saved_blend_state = state;
303}
304
305static INLINE
306void util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter,
307                                           void *state)
308{
309   blitter->saved_dsa_state = state;
310}
311
312static INLINE
313void util_blitter_save_vertex_elements(struct blitter_context *blitter,
314                                       void *state)
315{
316   blitter->saved_velem_state = state;
317}
318
319static INLINE
320void util_blitter_save_stencil_ref(struct blitter_context *blitter,
321                                   const struct pipe_stencil_ref *state)
322{
323   blitter->saved_stencil_ref = *state;
324}
325
326static INLINE
327void util_blitter_save_rasterizer(struct blitter_context *blitter,
328                                  void *state)
329{
330   blitter->saved_rs_state = state;
331}
332
333static INLINE
334void util_blitter_save_fragment_shader(struct blitter_context *blitter,
335                                       void *fs)
336{
337   blitter->saved_fs = fs;
338}
339
340static INLINE
341void util_blitter_save_vertex_shader(struct blitter_context *blitter,
342                                     void *vs)
343{
344   blitter->saved_vs = vs;
345}
346
347static INLINE
348void util_blitter_save_geometry_shader(struct blitter_context *blitter,
349                                       void *gs)
350{
351   blitter->saved_gs = gs;
352}
353
354static INLINE
355void util_blitter_save_framebuffer(struct blitter_context *blitter,
356                                   const struct pipe_framebuffer_state *state)
357{
358   blitter->saved_fb_state.nr_cbufs = 0; /* It's ~0 now, meaning it's unsaved. */
359   util_copy_framebuffer_state(&blitter->saved_fb_state, state);
360}
361
362static INLINE
363void util_blitter_save_viewport(struct blitter_context *blitter,
364                                struct pipe_viewport_state *state)
365{
366   blitter->saved_viewport = *state;
367}
368
369static INLINE
370void util_blitter_save_fragment_sampler_states(
371                  struct blitter_context *blitter,
372                  int num_sampler_states,
373                  void **sampler_states)
374{
375   assert(num_sampler_states <= Elements(blitter->saved_sampler_states));
376
377   blitter->saved_num_sampler_states = num_sampler_states;
378   memcpy(blitter->saved_sampler_states, sampler_states,
379          num_sampler_states * sizeof(void *));
380}
381
382static INLINE void
383util_blitter_save_fragment_sampler_views(struct blitter_context *blitter,
384                                         int num_views,
385                                         struct pipe_sampler_view **views)
386{
387   unsigned i;
388   assert(num_views <= Elements(blitter->saved_sampler_views));
389
390   blitter->saved_num_sampler_views = num_views;
391   for (i = 0; i < num_views; i++)
392      pipe_sampler_view_reference(&blitter->saved_sampler_views[i],
393                                  views[i]);
394}
395
396static INLINE void
397util_blitter_save_vertex_buffers(struct blitter_context *blitter,
398                                 int num_vertex_buffers,
399                                 struct pipe_vertex_buffer *vertex_buffers)
400{
401   assert(num_vertex_buffers <= Elements(blitter->saved_vertex_buffers));
402
403   blitter->saved_num_vertex_buffers = 0;
404   util_copy_vertex_buffers(blitter->saved_vertex_buffers,
405                            (unsigned*)&blitter->saved_num_vertex_buffers,
406                            vertex_buffers,
407                            num_vertex_buffers);
408}
409
410static INLINE void
411util_blitter_save_so_targets(struct blitter_context *blitter,
412                             int num_targets,
413                             struct pipe_stream_output_target **targets)
414{
415   unsigned i;
416   assert(num_targets <= Elements(blitter->saved_so_targets));
417
418   blitter->saved_num_so_targets = num_targets;
419   for (i = 0; i < num_targets; i++)
420      pipe_so_target_reference(&blitter->saved_so_targets[i],
421                               targets[i]);
422}
423
424static INLINE void
425util_blitter_save_sample_mask(struct blitter_context *blitter,
426                              unsigned sample_mask)
427{
428   blitter->is_sample_mask_saved = TRUE;
429   blitter->saved_sample_mask = sample_mask;
430}
431
432#ifdef __cplusplus
433}
434#endif
435
436#endif
437