sync with tizen_2.2
[sdk/emulator/qemu.git] / gl / mesa / src / gallium / auxiliary / util / u_blitter.h
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
38 extern "C" {
39 #endif
40
41 struct pipe_context;
42
43 enum blitter_attrib_type {
44    UTIL_BLITTER_ATTRIB_NONE,
45    UTIL_BLITTER_ATTRIB_COLOR,
46    UTIL_BLITTER_ATTRIB_TEXCOORD
47 };
48
49 struct 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
98    int saved_num_sampler_states;
99    void *saved_sampler_states[PIPE_MAX_SAMPLERS];
100
101    int saved_num_sampler_views;
102    struct pipe_sampler_view *saved_sampler_views[PIPE_MAX_SAMPLERS];
103
104    int saved_num_vertex_buffers;
105    struct pipe_vertex_buffer saved_vertex_buffers[PIPE_MAX_ATTRIBS];
106
107    int saved_num_so_targets;
108    struct pipe_stream_output_target *saved_so_targets[PIPE_MAX_SO_BUFFERS];
109 };
110
111 /**
112  * Create a blitter context.
113  */
114 struct blitter_context *util_blitter_create(struct pipe_context *pipe);
115
116 /**
117  * Destroy a blitter context.
118  */
119 void util_blitter_destroy(struct blitter_context *blitter);
120
121 /**
122  * Return the pipe context associated with a blitter context.
123  */
124 static INLINE
125 struct pipe_context *util_blitter_get_pipe(struct blitter_context *blitter)
126 {
127    return blitter->pipe;
128 }
129
130 /*
131  * These states must be saved before any of the following functions are called:
132  * - vertex buffers
133  * - vertex elements
134  * - vertex shader
135  * - geometry shader (if supported)
136  * - stream output targets (if supported)
137  * - rasterizer state
138  */
139
140 /**
141  * Clear a specified set of currently bound buffers to specified values.
142  *
143  * These states must be saved in the blitter in addition to the state objects
144  * already required to be saved:
145  * - fragment shader
146  * - depth stencil alpha state
147  * - blend state
148  */
149 void util_blitter_clear(struct blitter_context *blitter,
150                         unsigned width, unsigned height,
151                         unsigned num_cbufs,
152                         unsigned clear_buffers,
153                         enum pipe_format cbuf_format,
154                         const union pipe_color_union *color,
155                         double depth, unsigned stencil);
156
157 void util_blitter_clear_depth_custom(struct blitter_context *blitter,
158                                      unsigned width, unsigned height,
159                                      double depth, void *custom_dsa);
160
161 /**
162  * Copy a block of pixels from one surface to another.
163  *
164  * You can copy from any color format to any other color format provided
165  * the former can be sampled from and the latter can be rendered to. Otherwise,
166  * a software fallback path is taken and both surfaces must be of the same
167  * format.
168  *
169  * The same holds for depth-stencil formats with the exception that stencil
170  * cannot be copied unless you set ignore_stencil to FALSE. In that case,
171  * a software fallback path is taken and both surfaces must be of the same
172  * format.
173  * XXX implement hw-accel stencil copy using shader stencil export.
174  *
175  * Use pipe_screen->is_format_supported to know your options.
176  *
177  * These states must be saved in the blitter in addition to the state objects
178  * already required to be saved:
179  * - fragment shader
180  * - depth stencil alpha state
181  * - blend state
182  * - fragment sampler states
183  * - fragment sampler textures
184  * - framebuffer state
185  */
186 void util_blitter_copy_texture(struct blitter_context *blitter,
187                                struct pipe_resource *dst,
188                                unsigned dstlevel,
189                                unsigned dstx, unsigned dsty, unsigned dstz,
190                                struct pipe_resource *src,
191                                unsigned srclevel,
192                                const struct pipe_box *srcbox,
193                                boolean ignore_stencil);
194
195 /**
196  * Same as util_blitter_copy_texture, but dst and src are pipe_surface and
197  * pipe_sampler_view, respectively. The mipmap level and dstz are part of
198  * the views.
199  *
200  * Drivers can use this to change resource properties (like format, width,
201  * height) by changing how the views interpret them, instead of changing
202  * pipe_resource directly. This is usually needed to accelerate copying of
203  * compressed formats.
204  *
205  * src_width0 and src_height0 are sampler_view-private properties that
206  * override pipe_resource. The blitter uses them for computation of texture
207  * coordinates. The dst dimensions are supplied through pipe_surface::width
208  * and height.
209  *
210  * NOTE: There are no checks whether the blit is actually supported.
211  */
212 void util_blitter_copy_texture_view(struct blitter_context *blitter,
213                                     struct pipe_surface *dst,
214                                     unsigned dstx, unsigned dsty,
215                                     struct pipe_sampler_view *src,
216                                     const struct pipe_box *srcbox,
217                                     unsigned src_width0, unsigned src_height0);
218
219 /**
220  * Helper function to initialize a view for copy_texture_view.
221  * The parameters must match copy_texture_view.
222  */
223 void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
224                                       struct pipe_resource *dst,
225                                       unsigned dstlevel,
226                                       unsigned dstz,
227                                       const struct pipe_box *srcbox);
228
229 /**
230  * Helper function to initialize a view for copy_texture_view.
231  * The parameters must match copy_texture_view.
232  */
233 void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
234                                       struct pipe_resource *src,
235                                       unsigned srclevel);
236
237 /**
238  * Copy data from one buffer to another using the Stream Output functionality.
239  * Some alignment is required, otherwise software fallback is used.
240  */
241 void util_blitter_copy_buffer(struct blitter_context *blitter,
242                               struct pipe_resource *dst,
243                               unsigned dstx,
244                               struct pipe_resource *src,
245                               unsigned srcx,
246                               unsigned size);
247
248 /**
249  * Clear a region of a (color) surface to a constant value.
250  *
251  * These states must be saved in the blitter in addition to the state objects
252  * already required to be saved:
253  * - fragment shader
254  * - depth stencil alpha state
255  * - blend state
256  * - framebuffer state
257  */
258 void util_blitter_clear_render_target(struct blitter_context *blitter,
259                                       struct pipe_surface *dst,
260                                       const union pipe_color_union *color,
261                                       unsigned dstx, unsigned dsty,
262                                       unsigned width, unsigned height);
263
264 /**
265  * Clear a region of a depth-stencil surface, both stencil and depth
266  * or only one of them if this is a combined depth-stencil surface.
267  *
268  * These states must be saved in the blitter in addition to the state objects
269  * already required to be saved:
270  * - fragment shader
271  * - depth stencil alpha state
272  * - blend state
273  * - framebuffer state
274  */
275 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
276                                       struct pipe_surface *dst,
277                                       unsigned clear_flags,
278                                       double depth,
279                                       unsigned stencil,
280                                       unsigned dstx, unsigned dsty,
281                                       unsigned width, unsigned height);
282
283 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
284                                        struct pipe_surface *zsurf,
285                                        struct pipe_surface *cbsurf,
286                                        void *dsa_stage, float depth);
287
288 /* The functions below should be used to save currently bound constant state
289  * objects inside a driver. The objects are automatically restored at the end
290  * of the util_blitter_{clear, copy_region, fill_region} functions and then
291  * forgotten.
292  *
293  * States not listed here are not affected by util_blitter. */
294
295 static INLINE
296 void util_blitter_save_blend(struct blitter_context *blitter,
297                              void *state)
298 {
299    blitter->saved_blend_state = state;
300 }
301
302 static INLINE
303 void util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter,
304                                            void *state)
305 {
306    blitter->saved_dsa_state = state;
307 }
308
309 static INLINE
310 void util_blitter_save_vertex_elements(struct blitter_context *blitter,
311                                        void *state)
312 {
313    blitter->saved_velem_state = state;
314 }
315
316 static INLINE
317 void util_blitter_save_stencil_ref(struct blitter_context *blitter,
318                                    const struct pipe_stencil_ref *state)
319 {
320    blitter->saved_stencil_ref = *state;
321 }
322
323 static INLINE
324 void util_blitter_save_rasterizer(struct blitter_context *blitter,
325                                   void *state)
326 {
327    blitter->saved_rs_state = state;
328 }
329
330 static INLINE
331 void util_blitter_save_fragment_shader(struct blitter_context *blitter,
332                                        void *fs)
333 {
334    blitter->saved_fs = fs;
335 }
336
337 static INLINE
338 void util_blitter_save_vertex_shader(struct blitter_context *blitter,
339                                      void *vs)
340 {
341    blitter->saved_vs = vs;
342 }
343
344 static INLINE
345 void util_blitter_save_geometry_shader(struct blitter_context *blitter,
346                                        void *gs)
347 {
348    blitter->saved_gs = gs;
349 }
350
351 static INLINE
352 void util_blitter_save_framebuffer(struct blitter_context *blitter,
353                                    const struct pipe_framebuffer_state *state)
354 {
355    blitter->saved_fb_state.nr_cbufs = 0; /* It's ~0 now, meaning it's unsaved. */
356    util_copy_framebuffer_state(&blitter->saved_fb_state, state);
357 }
358
359 static INLINE
360 void util_blitter_save_viewport(struct blitter_context *blitter,
361                                 struct pipe_viewport_state *state)
362 {
363    blitter->saved_viewport = *state;
364 }
365
366 static INLINE
367 void util_blitter_save_fragment_sampler_states(
368                   struct blitter_context *blitter,
369                   int num_sampler_states,
370                   void **sampler_states)
371 {
372    assert(num_sampler_states <= Elements(blitter->saved_sampler_states));
373
374    blitter->saved_num_sampler_states = num_sampler_states;
375    memcpy(blitter->saved_sampler_states, sampler_states,
376           num_sampler_states * sizeof(void *));
377 }
378
379 static INLINE void
380 util_blitter_save_fragment_sampler_views(struct blitter_context *blitter,
381                                          int num_views,
382                                          struct pipe_sampler_view **views)
383 {
384    unsigned i;
385    assert(num_views <= Elements(blitter->saved_sampler_views));
386
387    blitter->saved_num_sampler_views = num_views;
388    for (i = 0; i < num_views; i++)
389       pipe_sampler_view_reference(&blitter->saved_sampler_views[i],
390                                   views[i]);
391 }
392
393 static INLINE void
394 util_blitter_save_vertex_buffers(struct blitter_context *blitter,
395                                  int num_vertex_buffers,
396                                  struct pipe_vertex_buffer *vertex_buffers)
397 {
398    assert(num_vertex_buffers <= Elements(blitter->saved_vertex_buffers));
399
400    blitter->saved_num_vertex_buffers = 0;
401    util_copy_vertex_buffers(blitter->saved_vertex_buffers,
402                             (unsigned*)&blitter->saved_num_vertex_buffers,
403                             vertex_buffers,
404                             num_vertex_buffers);
405 }
406
407 static INLINE void
408 util_blitter_save_so_targets(struct blitter_context *blitter,
409                              int num_targets,
410                              struct pipe_stream_output_target **targets)
411 {
412    unsigned i;
413    assert(num_targets <= Elements(blitter->saved_so_targets));
414
415    blitter->saved_num_so_targets = num_targets;
416    for (i = 0; i < num_targets; i++)
417       pipe_so_target_reference(&blitter->saved_so_targets[i],
418                                targets[i]);
419 }
420
421 #ifdef __cplusplus
422 }
423 #endif
424
425 #endif