gallium/u_blitter: add ability to disable and restore the render condition
[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 #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 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 struct pipe_query *saved_render_cond_query;
113 uint saved_render_cond_mode;
114 };
115
116 /**
117 * Create a blitter context.
118 */
119 struct blitter_context *util_blitter_create(struct pipe_context *pipe);
120
121 /**
122 * Destroy a blitter context.
123 */
124 void util_blitter_destroy(struct blitter_context *blitter);
125
126 void util_blitter_cache_all_shaders(struct blitter_context *blitter);
127
128 /**
129 * Return the pipe context associated with a blitter context.
130 */
131 static INLINE
132 struct pipe_context *util_blitter_get_pipe(struct blitter_context *blitter)
133 {
134 return blitter->pipe;
135 }
136
137 /* The default function to draw a rectangle. This can only be used
138 * inside of the draw_rectangle callback if the driver overrides it. */
139 void util_blitter_draw_rectangle(struct blitter_context *blitter,
140 unsigned x1, unsigned y1,
141 unsigned x2, unsigned y2,
142 float depth,
143 enum blitter_attrib_type type,
144 const union pipe_color_union *attrib);
145
146 /*
147 * These states must be saved before any of the following functions are called:
148 * - vertex buffers
149 * - vertex elements
150 * - vertex shader
151 * - geometry shader (if supported)
152 * - stream output targets (if supported)
153 * - rasterizer state
154 */
155
156 /**
157 * Clear a specified set of currently bound buffers to specified values.
158 *
159 * These states must be saved in the blitter in addition to the state objects
160 * already required to be saved:
161 * - fragment shader
162 * - depth stencil alpha state
163 * - blend state
164 */
165 void util_blitter_clear(struct blitter_context *blitter,
166 unsigned width, unsigned height,
167 unsigned num_cbufs,
168 unsigned clear_buffers,
169 enum pipe_format cbuf_format,
170 const union pipe_color_union *color,
171 double depth, unsigned stencil);
172
173 /**
174 * Check if the blitter (with the help of the driver) can blit between
175 * the two resources.
176 * The mask is a combination of the PIPE_MASK_* flags.
177 * Set to PIPE_MASK_RGBAZS if unsure.
178 */
179 boolean util_blitter_is_copy_supported(struct blitter_context *blitter,
180 const struct pipe_resource *dst,
181 const struct pipe_resource *src,
182 unsigned mask);
183 /**
184 * Copy a block of pixels from one surface to another.
185 *
186 * You can copy from any color format to any other color format provided
187 * the former can be sampled from and the latter can be rendered to. Otherwise,
188 * a software fallback path is taken and both surfaces must be of the same
189 * format.
190 *
191 * Only one sample of a multisample texture can be copied and is specified by
192 * src_sample. If the destination is a multisample resource, dst_sample_mask
193 * specifies the sample mask. For single-sample resources, set dst_sample_mask
194 * to ~0.
195 *
196 * These states must be saved in the blitter in addition to the state objects
197 * already required to be saved:
198 * - fragment shader
199 * - depth stencil alpha state
200 * - blend state
201 * - fragment sampler states
202 * - fragment sampler textures
203 * - framebuffer state
204 */
205 void util_blitter_copy_texture(struct blitter_context *blitter,
206 struct pipe_resource *dst,
207 unsigned dst_level, unsigned dst_sample_mask,
208 unsigned dstx, unsigned dsty, unsigned dstz,
209 struct pipe_resource *src,
210 unsigned src_level, unsigned src_sample,
211 const struct pipe_box *srcbox);
212
213 /**
214 * Same as util_blitter_copy_texture, but dst and src are pipe_surface and
215 * pipe_sampler_view, respectively. The mipmap level and dstz are part of
216 * the views.
217 *
218 * Drivers can use this to change resource properties (like format, width,
219 * height) by changing how the views interpret them, instead of changing
220 * pipe_resource directly. This is usually needed to accelerate copying of
221 * compressed formats.
222 *
223 * src_width0 and src_height0 are sampler_view-private properties that
224 * override pipe_resource. The blitter uses them for computation of texture
225 * coordinates. The dst dimensions are supplied through pipe_surface::width
226 * and height.
227 *
228 * The mask is a combination of the PIPE_MASK_* flags.
229 * Set to PIPE_MASK_RGBAZS if unsure.
230 *
231 * NOTE: There are no checks whether the blit is actually supported.
232 */
233 void util_blitter_copy_texture_view(struct blitter_context *blitter,
234 struct pipe_surface *dst,
235 unsigned dst_sample_mask,
236 unsigned dstx, unsigned dsty,
237 struct pipe_sampler_view *src,
238 unsigned src_sample,
239 const struct pipe_box *srcbox,
240 unsigned src_width0, unsigned src_height0,
241 unsigned mask);
242
243 /**
244 * Helper function to initialize a view for copy_texture_view.
245 * The parameters must match copy_texture_view.
246 */
247 void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
248 struct pipe_resource *dst,
249 unsigned dstlevel,
250 unsigned dstz,
251 const struct pipe_box *srcbox);
252
253 /**
254 * Helper function to initialize a view for copy_texture_view.
255 * The parameters must match copy_texture_view.
256 */
257 void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
258 struct pipe_resource *src,
259 unsigned srclevel);
260
261 /**
262 * Copy data from one buffer to another using the Stream Output functionality.
263 * Some alignment is required, otherwise software fallback is used.
264 */
265 void util_blitter_copy_buffer(struct blitter_context *blitter,
266 struct pipe_resource *dst,
267 unsigned dstx,
268 struct pipe_resource *src,
269 unsigned srcx,
270 unsigned size);
271
272 /**
273 * Clear a region of a (color) surface to a constant value.
274 *
275 * These states must be saved in the blitter in addition to the state objects
276 * already required to be saved:
277 * - fragment shader
278 * - depth stencil alpha state
279 * - blend state
280 * - framebuffer state
281 */
282 void util_blitter_clear_render_target(struct blitter_context *blitter,
283 struct pipe_surface *dst,
284 const union pipe_color_union *color,
285 unsigned dstx, unsigned dsty,
286 unsigned width, unsigned height);
287
288 /**
289 * Clear a region of a depth-stencil surface, both stencil and depth
290 * or only one of them if this is a combined depth-stencil surface.
291 *
292 * These states must be saved in the blitter in addition to the state objects
293 * already required to be saved:
294 * - fragment shader
295 * - depth stencil alpha state
296 * - blend state
297 * - framebuffer state
298 */
299 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
300 struct pipe_surface *dst,
301 unsigned clear_flags,
302 double depth,
303 unsigned stencil,
304 unsigned dstx, unsigned dsty,
305 unsigned width, unsigned height);
306
307 /* The following functions are customized variants of the clear functions.
308 * Some drivers use them internally to do things like MSAA resolve
309 * and resource decompression. It usually consists of rendering a full-screen
310 * quad with a special blend or DSA state.
311 */
312
313 /* Used by r300g for depth decompression. */
314 void util_blitter_custom_clear_depth(struct blitter_context *blitter,
315 unsigned width, unsigned height,
316 double depth, void *custom_dsa);
317
318 /* Used by r600g for depth decompression. */
319 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
320 struct pipe_surface *zsurf,
321 struct pipe_surface *cbsurf,
322 unsigned sample_mask,
323 void *dsa_stage, float depth);
324
325 /* Used by r600g for color decompression. */
326 void util_blitter_custom_color(struct blitter_context *blitter,
327 struct pipe_surface *dstsurf,
328 void *custom_blend);
329
330 /* Used by r600g for MSAA color resolve. */
331 void util_blitter_custom_resolve_color(struct blitter_context *blitter,
332 struct pipe_resource *dst,
333 unsigned dst_level,
334 unsigned dst_layer,
335 struct pipe_resource *src,
336 unsigned src_layer,
337 unsigned sampled_mask,
338 void *custom_blend);
339
340 /* The functions below should be used to save currently bound constant state
341 * objects inside a driver. The objects are automatically restored at the end
342 * of the util_blitter_{clear, copy_region, fill_region} functions and then
343 * forgotten.
344 *
345 * States not listed here are not affected by util_blitter. */
346
347 static INLINE
348 void util_blitter_save_blend(struct blitter_context *blitter,
349 void *state)
350 {
351 blitter->saved_blend_state = state;
352 }
353
354 static INLINE
355 void util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter,
356 void *state)
357 {
358 blitter->saved_dsa_state = state;
359 }
360
361 static INLINE
362 void util_blitter_save_vertex_elements(struct blitter_context *blitter,
363 void *state)
364 {
365 blitter->saved_velem_state = state;
366 }
367
368 static INLINE
369 void util_blitter_save_stencil_ref(struct blitter_context *blitter,
370 const struct pipe_stencil_ref *state)
371 {
372 blitter->saved_stencil_ref = *state;
373 }
374
375 static INLINE
376 void util_blitter_save_rasterizer(struct blitter_context *blitter,
377 void *state)
378 {
379 blitter->saved_rs_state = state;
380 }
381
382 static INLINE
383 void util_blitter_save_fragment_shader(struct blitter_context *blitter,
384 void *fs)
385 {
386 blitter->saved_fs = fs;
387 }
388
389 static INLINE
390 void util_blitter_save_vertex_shader(struct blitter_context *blitter,
391 void *vs)
392 {
393 blitter->saved_vs = vs;
394 }
395
396 static INLINE
397 void util_blitter_save_geometry_shader(struct blitter_context *blitter,
398 void *gs)
399 {
400 blitter->saved_gs = gs;
401 }
402
403 static INLINE
404 void util_blitter_save_framebuffer(struct blitter_context *blitter,
405 const struct pipe_framebuffer_state *state)
406 {
407 blitter->saved_fb_state.nr_cbufs = 0; /* It's ~0 now, meaning it's unsaved. */
408 util_copy_framebuffer_state(&blitter->saved_fb_state, state);
409 }
410
411 static INLINE
412 void util_blitter_save_viewport(struct blitter_context *blitter,
413 struct pipe_viewport_state *state)
414 {
415 blitter->saved_viewport = *state;
416 }
417
418 static INLINE
419 void util_blitter_save_fragment_sampler_states(
420 struct blitter_context *blitter,
421 int num_sampler_states,
422 void **sampler_states)
423 {
424 assert(num_sampler_states <= Elements(blitter->saved_sampler_states));
425
426 blitter->saved_num_sampler_states = num_sampler_states;
427 memcpy(blitter->saved_sampler_states, sampler_states,
428 num_sampler_states * sizeof(void *));
429 }
430
431 static INLINE void
432 util_blitter_save_fragment_sampler_views(struct blitter_context *blitter,
433 int num_views,
434 struct pipe_sampler_view **views)
435 {
436 unsigned i;
437 assert(num_views <= Elements(blitter->saved_sampler_views));
438
439 blitter->saved_num_sampler_views = num_views;
440 for (i = 0; i < num_views; i++)
441 pipe_sampler_view_reference(&blitter->saved_sampler_views[i],
442 views[i]);
443 }
444
445 static INLINE void
446 util_blitter_save_vertex_buffers(struct blitter_context *blitter,
447 int num_vertex_buffers,
448 struct pipe_vertex_buffer *vertex_buffers)
449 {
450 assert(num_vertex_buffers <= Elements(blitter->saved_vertex_buffers));
451
452 blitter->saved_num_vertex_buffers = 0;
453 util_copy_vertex_buffers(blitter->saved_vertex_buffers,
454 (unsigned*)&blitter->saved_num_vertex_buffers,
455 vertex_buffers,
456 num_vertex_buffers);
457 }
458
459 static INLINE void
460 util_blitter_save_so_targets(struct blitter_context *blitter,
461 int num_targets,
462 struct pipe_stream_output_target **targets)
463 {
464 unsigned i;
465 assert(num_targets <= Elements(blitter->saved_so_targets));
466
467 blitter->saved_num_so_targets = num_targets;
468 for (i = 0; i < num_targets; i++)
469 pipe_so_target_reference(&blitter->saved_so_targets[i],
470 targets[i]);
471 }
472
473 static INLINE void
474 util_blitter_save_sample_mask(struct blitter_context *blitter,
475 unsigned sample_mask)
476 {
477 blitter->is_sample_mask_saved = TRUE;
478 blitter->saved_sample_mask = sample_mask;
479 }
480
481 static INLINE void
482 util_blitter_save_render_condition(struct blitter_context *blitter,
483 struct pipe_query *query,
484 uint mode)
485 {
486 blitter->saved_render_cond_query = query;
487 blitter->saved_render_cond_mode = mode;
488 }
489
490 #ifdef __cplusplus
491 }
492 #endif
493
494 #endif