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