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