99175409be728bcb507cac9d1a75047936ea1ea9
[mesa.git] / 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
33 #include "pipe/p_state.h"
34
35 /* u_memory.h conflicts with st/mesa */
36 #ifndef Elements
37 #define Elements(x) (sizeof(x)/sizeof((x)[0]))
38 #endif
39
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 struct pipe_context;
46
47 enum blitter_attrib_type {
48 UTIL_BLITTER_ATTRIB_NONE,
49 UTIL_BLITTER_ATTRIB_COLOR,
50 UTIL_BLITTER_ATTRIB_TEXCOORD
51 };
52
53 struct blitter_context
54 {
55 /**
56 * Draw a rectangle.
57 *
58 * \param x1 An X coordinate of the top-left corner.
59 * \param y1 A Y coordinate of the top-left corner.
60 * \param x2 An X coordinate of the bottom-right corner.
61 * \param y2 A Y coordinate of the bottom-right corner.
62 * \param depth A depth which the rectangle is rendered at.
63 *
64 * \param type Semantics of the attributes "attrib".
65 * If type is UTIL_BLITTER_ATTRIB_NONE, ignore them.
66 * If type is UTIL_BLITTER_ATTRIB_COLOR, the attributes
67 * make up a constant RGBA color, and should go
68 * to the GENERIC0 varying slot of a fragment shader.
69 * If type is UTIL_BLITTER_ATTRIB_TEXCOORD, {a1, a2} and
70 * {a3, a4} specify top-left and bottom-right texture
71 * coordinates of the rectangle, respectively, and should go
72 * to the GENERIC0 varying slot of a fragment shader.
73 *
74 * \param attrib See type.
75 *
76 * \note A driver may optionally override this callback to implement
77 * a specialized hardware path for drawing a rectangle, e.g. using
78 * a rectangular point sprite.
79 */
80 void (*draw_rectangle)(struct blitter_context *blitter,
81 int x1, int y1, int x2, int y2,
82 float depth,
83 enum blitter_attrib_type type,
84 const union pipe_color_union *color);
85
86 /* Whether the blitter is running. */
87 boolean running;
88
89 /* Private members, really. */
90 struct pipe_context *pipe; /**< pipe context */
91
92 void *saved_blend_state; /**< blend state */
93 void *saved_dsa_state; /**< depth stencil alpha state */
94 void *saved_velem_state; /**< vertex elements state */
95 void *saved_rs_state; /**< rasterizer state */
96 void *saved_fs, *saved_vs, *saved_gs; /**< shaders */
97
98 struct pipe_framebuffer_state saved_fb_state; /**< framebuffer state */
99 struct pipe_stencil_ref saved_stencil_ref; /**< stencil ref */
100 struct pipe_viewport_state saved_viewport;
101 struct pipe_scissor_state saved_scissor;
102 boolean is_sample_mask_saved;
103 unsigned saved_sample_mask;
104
105 unsigned saved_num_sampler_states;
106 void *saved_sampler_states[PIPE_MAX_SAMPLERS];
107
108 unsigned saved_num_sampler_views;
109 struct pipe_sampler_view *saved_sampler_views[PIPE_MAX_SAMPLERS];
110
111 unsigned vb_slot;
112 struct pipe_vertex_buffer saved_vertex_buffer;
113
114 unsigned saved_num_so_targets;
115 struct pipe_stream_output_target *saved_so_targets[PIPE_MAX_SO_BUFFERS];
116
117 struct pipe_query *saved_render_cond_query;
118 uint saved_render_cond_mode;
119 };
120
121 /**
122 * Create a blitter context.
123 */
124 struct blitter_context *util_blitter_create(struct pipe_context *pipe);
125
126 /**
127 * Destroy a blitter context.
128 */
129 void util_blitter_destroy(struct blitter_context *blitter);
130
131 void util_blitter_cache_all_shaders(struct blitter_context *blitter);
132
133 /**
134 * Return the pipe context associated with a blitter context.
135 */
136 static INLINE
137 struct pipe_context *util_blitter_get_pipe(struct blitter_context *blitter)
138 {
139 return blitter->pipe;
140 }
141
142 /**
143 * Override PIPE_CAP_TEXTURE_MULTISAMPLE as reported by the driver.
144 */
145 void util_blitter_set_texture_multisample(struct blitter_context *blitter,
146 boolean supported);
147
148 /* The default function to draw a rectangle. This can only be used
149 * inside of the draw_rectangle callback if the driver overrides it. */
150 void util_blitter_draw_rectangle(struct blitter_context *blitter,
151 int x1, int y1, int x2, int y2, float depth,
152 enum blitter_attrib_type type,
153 const union pipe_color_union *attrib);
154
155 /**
156 * Try to do a blit using resource_copy_region. The function calls
157 * resource_copy_region if the blit description is compatible with it.
158 *
159 * It returns TRUE if the blit was done using resource_copy_region.
160 *
161 * It returns FALSE otherwise and the caller must fall back to a more generic
162 * codepath for the blit operation. (e.g. by using u_blitter)
163 */
164 boolean util_try_blit_via_copy_region(struct pipe_context *ctx,
165 const struct pipe_blit_info *blit);
166
167 /*
168 * These states must be saved before any of the following functions are called:
169 * - vertex buffers
170 * - vertex elements
171 * - vertex shader
172 * - geometry shader (if supported)
173 * - stream output targets (if supported)
174 * - rasterizer state
175 */
176
177 /**
178 * Clear a specified set of currently bound buffers to specified values.
179 *
180 * These states must be saved in the blitter in addition to the state objects
181 * already required to be saved:
182 * - fragment shader
183 * - depth stencil alpha state
184 * - blend state
185 */
186 void util_blitter_clear(struct blitter_context *blitter,
187 unsigned width, unsigned height,
188 unsigned num_cbufs,
189 unsigned clear_buffers,
190 enum pipe_format cbuf_format,
191 const union pipe_color_union *color,
192 double depth, unsigned stencil);
193
194 /**
195 * Check if the blitter (with the help of the driver) can blit between
196 * the two resources.
197 * The mask is a combination of the PIPE_MASK_* flags.
198 * Set to PIPE_MASK_RGBAZS if unsure.
199 */
200 boolean util_blitter_is_copy_supported(struct blitter_context *blitter,
201 const struct pipe_resource *dst,
202 const struct pipe_resource *src,
203 unsigned mask);
204
205 boolean util_blitter_is_blit_supported(struct blitter_context *blitter,
206 const struct pipe_blit_info *info);
207
208 /**
209 * Copy a block of pixels from one surface to another.
210 *
211 * These states must be saved in the blitter in addition to the state objects
212 * already required to be saved:
213 * - fragment shader
214 * - depth stencil alpha state
215 * - blend state
216 * - fragment sampler states
217 * - fragment sampler textures
218 * - framebuffer state
219 * - sample mask
220 */
221 void util_blitter_copy_texture(struct blitter_context *blitter,
222 struct pipe_resource *dst,
223 unsigned dst_level,
224 unsigned dstx, unsigned dsty, unsigned dstz,
225 struct pipe_resource *src,
226 unsigned src_level,
227 const struct pipe_box *srcbox, unsigned mask,
228 boolean copy_all_samples);
229
230 /**
231 * Same as util_blitter_copy_texture with the capabilities of util_blitter_blit,
232 * but dst and src are pipe_surface and pipe_sampler_view, respectively.
233 * The mipmap level and dstz are part of the views.
234 *
235 * Drivers can use this to change resource properties (like format, width,
236 * height) by changing how the views interpret them, instead of changing
237 * pipe_resource directly. This is usually needed to accelerate copying of
238 * compressed formats.
239 *
240 * src_width0 and src_height0 are sampler_view-private properties that
241 * override pipe_resource. The blitter uses them for computation of texture
242 * coordinates. The dst dimensions are supplied through pipe_surface::width
243 * and height.
244 *
245 * The mask is a combination of the PIPE_MASK_* flags.
246 * Set to PIPE_MASK_RGBAZS if unsure.
247 */
248 void util_blitter_blit_generic(struct blitter_context *blitter,
249 struct pipe_surface *dst,
250 int dstx, int dsty,
251 unsigned dst_width, unsigned dst_height,
252 struct pipe_sampler_view *src,
253 const struct pipe_box *srcbox,
254 unsigned src_width0, unsigned src_height0,
255 unsigned mask, unsigned filter,
256 const struct pipe_scissor_state *scissor,
257 boolean copy_all_samples);
258
259 void util_blitter_blit(struct blitter_context *blitter,
260 const struct pipe_blit_info *info);
261
262 /**
263 * Helper function to initialize a view for copy_texture_view.
264 * The parameters must match copy_texture_view.
265 */
266 void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
267 struct pipe_resource *dst,
268 unsigned dstlevel,
269 unsigned dstz,
270 const struct pipe_box *srcbox);
271
272 /**
273 * Helper function to initialize a view for copy_texture_view.
274 * The parameters must match copy_texture_view.
275 */
276 void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
277 struct pipe_resource *src,
278 unsigned srclevel);
279
280 /**
281 * Copy data from one buffer to another using the Stream Output functionality.
282 * Some alignment is required, otherwise software fallback is used.
283 */
284 void util_blitter_copy_buffer(struct blitter_context *blitter,
285 struct pipe_resource *dst,
286 unsigned dstx,
287 struct pipe_resource *src,
288 unsigned srcx,
289 unsigned size);
290
291 /**
292 * Clear a region of a (color) surface to a constant value.
293 *
294 * These states must be saved in the blitter in addition to the state objects
295 * already required to be saved:
296 * - fragment shader
297 * - depth stencil alpha state
298 * - blend state
299 * - framebuffer state
300 */
301 void util_blitter_clear_render_target(struct blitter_context *blitter,
302 struct pipe_surface *dst,
303 const union pipe_color_union *color,
304 unsigned dstx, unsigned dsty,
305 unsigned width, unsigned height);
306
307 /**
308 * Clear a region of a depth-stencil surface, both stencil and depth
309 * or only one of them if this is a combined depth-stencil surface.
310 *
311 * These states must be saved in the blitter in addition to the state objects
312 * already required to be saved:
313 * - fragment shader
314 * - depth stencil alpha state
315 * - blend state
316 * - framebuffer state
317 */
318 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
319 struct pipe_surface *dst,
320 unsigned clear_flags,
321 double depth,
322 unsigned stencil,
323 unsigned dstx, unsigned dsty,
324 unsigned width, unsigned height);
325
326 /* The following functions are customized variants of the clear functions.
327 * Some drivers use them internally to do things like MSAA resolve
328 * and resource decompression. It usually consists of rendering a full-screen
329 * quad with a special blend or DSA state.
330 */
331
332 /* Used by r300g for depth decompression. */
333 void util_blitter_custom_clear_depth(struct blitter_context *blitter,
334 unsigned width, unsigned height,
335 double depth, void *custom_dsa);
336
337 /* Used by r600g for depth decompression. */
338 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
339 struct pipe_surface *zsurf,
340 struct pipe_surface *cbsurf,
341 unsigned sample_mask,
342 void *dsa_stage, float depth);
343
344 /* Used by r600g for color decompression. */
345 void util_blitter_custom_color(struct blitter_context *blitter,
346 struct pipe_surface *dstsurf,
347 void *custom_blend);
348
349 /* Used by r600g for MSAA color resolve. */
350 void util_blitter_custom_resolve_color(struct blitter_context *blitter,
351 struct pipe_resource *dst,
352 unsigned dst_level,
353 unsigned dst_layer,
354 struct pipe_resource *src,
355 unsigned src_layer,
356 unsigned sampled_mask,
357 void *custom_blend,
358 enum pipe_format format);
359
360 /* The functions below should be used to save currently bound constant state
361 * objects inside a driver. The objects are automatically restored at the end
362 * of the util_blitter_{clear, copy_region, fill_region} functions and then
363 * forgotten.
364 *
365 * States not listed here are not affected by util_blitter. */
366
367 static INLINE
368 void util_blitter_save_blend(struct blitter_context *blitter,
369 void *state)
370 {
371 blitter->saved_blend_state = state;
372 }
373
374 static INLINE
375 void util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter,
376 void *state)
377 {
378 blitter->saved_dsa_state = state;
379 }
380
381 static INLINE
382 void util_blitter_save_vertex_elements(struct blitter_context *blitter,
383 void *state)
384 {
385 blitter->saved_velem_state = state;
386 }
387
388 static INLINE
389 void util_blitter_save_stencil_ref(struct blitter_context *blitter,
390 const struct pipe_stencil_ref *state)
391 {
392 blitter->saved_stencil_ref = *state;
393 }
394
395 static INLINE
396 void util_blitter_save_rasterizer(struct blitter_context *blitter,
397 void *state)
398 {
399 blitter->saved_rs_state = state;
400 }
401
402 static INLINE
403 void util_blitter_save_fragment_shader(struct blitter_context *blitter,
404 void *fs)
405 {
406 blitter->saved_fs = fs;
407 }
408
409 static INLINE
410 void util_blitter_save_vertex_shader(struct blitter_context *blitter,
411 void *vs)
412 {
413 blitter->saved_vs = vs;
414 }
415
416 static INLINE
417 void util_blitter_save_geometry_shader(struct blitter_context *blitter,
418 void *gs)
419 {
420 blitter->saved_gs = gs;
421 }
422
423 static INLINE
424 void util_blitter_save_framebuffer(struct blitter_context *blitter,
425 const struct pipe_framebuffer_state *state)
426 {
427 blitter->saved_fb_state.nr_cbufs = 0; /* It's ~0 now, meaning it's unsaved. */
428 util_copy_framebuffer_state(&blitter->saved_fb_state, state);
429 }
430
431 static INLINE
432 void util_blitter_save_viewport(struct blitter_context *blitter,
433 struct pipe_viewport_state *state)
434 {
435 blitter->saved_viewport = *state;
436 }
437
438 static INLINE
439 void util_blitter_save_scissor(struct blitter_context *blitter,
440 struct pipe_scissor_state *state)
441 {
442 blitter->saved_scissor = *state;
443 }
444
445 static INLINE
446 void util_blitter_save_fragment_sampler_states(
447 struct blitter_context *blitter,
448 unsigned num_sampler_states,
449 void **sampler_states)
450 {
451 assert(num_sampler_states <= Elements(blitter->saved_sampler_states));
452
453 blitter->saved_num_sampler_states = num_sampler_states;
454 memcpy(blitter->saved_sampler_states, sampler_states,
455 num_sampler_states * sizeof(void *));
456 }
457
458 static INLINE void
459 util_blitter_save_fragment_sampler_views(struct blitter_context *blitter,
460 unsigned num_views,
461 struct pipe_sampler_view **views)
462 {
463 unsigned i;
464 assert(num_views <= Elements(blitter->saved_sampler_views));
465
466 blitter->saved_num_sampler_views = num_views;
467 for (i = 0; i < num_views; i++)
468 pipe_sampler_view_reference(&blitter->saved_sampler_views[i],
469 views[i]);
470 }
471
472 static INLINE void
473 util_blitter_save_vertex_buffer_slot(struct blitter_context *blitter,
474 struct pipe_vertex_buffer *vertex_buffers)
475 {
476 pipe_resource_reference(&blitter->saved_vertex_buffer.buffer,
477 vertex_buffers[blitter->vb_slot].buffer);
478 memcpy(&blitter->saved_vertex_buffer, &vertex_buffers[blitter->vb_slot],
479 sizeof(struct pipe_vertex_buffer));
480 }
481
482 static INLINE void
483 util_blitter_save_so_targets(struct blitter_context *blitter,
484 unsigned num_targets,
485 struct pipe_stream_output_target **targets)
486 {
487 unsigned i;
488 assert(num_targets <= Elements(blitter->saved_so_targets));
489
490 blitter->saved_num_so_targets = num_targets;
491 for (i = 0; i < num_targets; i++)
492 pipe_so_target_reference(&blitter->saved_so_targets[i],
493 targets[i]);
494 }
495
496 static INLINE void
497 util_blitter_save_sample_mask(struct blitter_context *blitter,
498 unsigned sample_mask)
499 {
500 blitter->is_sample_mask_saved = TRUE;
501 blitter->saved_sample_mask = sample_mask;
502 }
503
504 static INLINE void
505 util_blitter_save_render_condition(struct blitter_context *blitter,
506 struct pipe_query *query,
507 uint mode)
508 {
509 blitter->saved_render_cond_query = query;
510 blitter->saved_render_cond_mode = mode;
511 }
512
513 #ifdef __cplusplus
514 }
515 #endif
516
517 #endif