radeonsi: make sure that blend state != NULL and remove all NULL checking
[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_XY,
45 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW,
46 };
47
48 union blitter_attrib {
49 float color[4];
50
51 struct {
52 float x1, y1, x2, y2, z, w;
53 } texcoord;
54 };
55
56 struct blitter_context;
57
58 typedef void *(*blitter_get_vs_func)(struct blitter_context *blitter);
59
60 struct blitter_context
61 {
62 /**
63 * Draw a rectangle.
64 *
65 * \param get_vs Callback for obtaining the vertex shader for the draw call.
66 * It might invoke the shader compiler. The driver is
67 * responsible for setting the vertex shader, and the callback
68 * allows the driver to query the vertex shader CSO if it
69 * wants to use the default one.
70 * \param x1 An X coordinate of the top-left corner.
71 * \param y1 A Y coordinate of the top-left corner.
72 * \param x2 An X coordinate of the bottom-right corner.
73 * \param y2 A Y coordinate of the bottom-right corner.
74 * \param depth A depth which the rectangle is rendered at.
75 *
76 * \param type Semantics of the attributes "attrib".
77 * If type is UTIL_BLITTER_ATTRIB_NONE, ignore them.
78 * If type is UTIL_BLITTER_ATTRIB_COLOR, the attributes
79 * make up a constant RGBA color, and should go
80 * to the GENERIC0 varying slot of a fragment shader.
81 * If type is UTIL_BLITTER_ATTRIB_TEXCOORD, {a1, a2} and
82 * {a3, a4} specify top-left and bottom-right texture
83 * coordinates of the rectangle, respectively, and should go
84 * to the GENERIC0 varying slot of a fragment shader.
85 *
86 * \param attrib See type.
87 *
88 * \note A driver may optionally override this callback to implement
89 * a specialized hardware path for drawing a rectangle, e.g. using
90 * a rectangular point sprite.
91 */
92 void (*draw_rectangle)(struct blitter_context *blitter,
93 void *vertex_elements_cso,
94 blitter_get_vs_func get_vs,
95 int x1, int y1, int x2, int y2,
96 float depth, unsigned num_instances,
97 enum blitter_attrib_type type,
98 const union blitter_attrib *attrib);
99
100 /* Whether the blitter is running. */
101 bool running;
102
103 bool use_index_buffer;
104
105 /* Private members, really. */
106 struct pipe_context *pipe; /**< pipe context */
107
108 void *saved_blend_state; /**< blend state */
109 void *saved_dsa_state; /**< depth stencil alpha state */
110 void *saved_velem_state; /**< vertex elements state */
111 void *saved_rs_state; /**< rasterizer state */
112 void *saved_fs, *saved_vs, *saved_gs, *saved_tcs, *saved_tes; /**< shaders */
113
114 struct pipe_framebuffer_state saved_fb_state; /**< framebuffer state */
115 struct pipe_stencil_ref saved_stencil_ref; /**< stencil ref */
116 struct pipe_viewport_state saved_viewport;
117 struct pipe_scissor_state saved_scissor;
118 bool skip_viewport_restore;
119 bool is_sample_mask_saved;
120 unsigned saved_sample_mask;
121
122 unsigned saved_num_sampler_states;
123 void *saved_sampler_states[PIPE_MAX_SAMPLERS];
124
125 unsigned saved_num_sampler_views;
126 struct pipe_sampler_view *saved_sampler_views[PIPE_MAX_SAMPLERS];
127
128 unsigned cb_slot;
129 struct pipe_constant_buffer saved_fs_constant_buffer;
130
131 unsigned vb_slot;
132 struct pipe_vertex_buffer saved_vertex_buffer;
133
134 unsigned saved_num_so_targets;
135 struct pipe_stream_output_target *saved_so_targets[PIPE_MAX_SO_BUFFERS];
136
137 struct pipe_query *saved_render_cond_query;
138 uint saved_render_cond_mode;
139 bool saved_render_cond_cond;
140
141 boolean saved_window_rectangles_include;
142 unsigned saved_num_window_rectangles;
143 struct pipe_scissor_state saved_window_rectangles[PIPE_MAX_WINDOW_RECTANGLES];
144 };
145
146 /**
147 * Create a blitter context.
148 */
149 struct blitter_context *util_blitter_create(struct pipe_context *pipe);
150
151 /**
152 * Destroy a blitter context.
153 */
154 void util_blitter_destroy(struct blitter_context *blitter);
155
156 void util_blitter_cache_all_shaders(struct blitter_context *blitter);
157 void *util_blitter_get_noop_blend_state(struct blitter_context *blitter);
158
159
160 /**
161 * Return the pipe context associated with a blitter context.
162 */
163 static inline
164 struct pipe_context *util_blitter_get_pipe(struct blitter_context *blitter)
165 {
166 return blitter->pipe;
167 }
168
169 /**
170 * Override PIPE_CAP_TEXTURE_MULTISAMPLE as reported by the driver.
171 */
172 void util_blitter_set_texture_multisample(struct blitter_context *blitter,
173 bool supported);
174
175 /* The default function to draw a rectangle. This can only be used
176 * inside of the draw_rectangle callback if the driver overrides it. */
177 void util_blitter_draw_rectangle(struct blitter_context *blitter,
178 void *vertex_elements_cso,
179 blitter_get_vs_func get_vs,
180 int x1, int y1, int x2, int y2,
181 float depth, unsigned num_instances,
182 enum blitter_attrib_type type,
183 const union blitter_attrib *attrib);
184
185
186 /*
187 * These states must be saved before any of the following functions are called:
188 * - vertex buffers
189 * - vertex elements
190 * - vertex shader
191 * - geometry shader (if supported)
192 * - stream output targets (if supported)
193 * - rasterizer state
194 */
195
196 /**
197 * Clear a specified set of currently bound buffers to specified values.
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 */
205 void util_blitter_clear(struct blitter_context *blitter,
206 unsigned width, unsigned height, unsigned num_layers,
207 unsigned clear_buffers,
208 const union pipe_color_union *color,
209 double depth, unsigned stencil,
210 bool msaa);
211
212 /**
213 * Check if the blitter (with the help of the driver) can blit between
214 * the two resources.
215 */
216 bool util_blitter_is_copy_supported(struct blitter_context *blitter,
217 const struct pipe_resource *dst,
218 const struct pipe_resource *src);
219
220 bool util_blitter_is_blit_supported(struct blitter_context *blitter,
221 const struct pipe_blit_info *info);
222
223 /**
224 * Copy a block of pixels from one surface to another.
225 *
226 * These states must be saved in the blitter in addition to the state objects
227 * already required to be saved:
228 * - fragment shader
229 * - depth stencil alpha state
230 * - blend state
231 * - fragment sampler states
232 * - fragment sampler textures
233 * - framebuffer state
234 * - sample mask
235 */
236 void util_blitter_copy_texture(struct blitter_context *blitter,
237 struct pipe_resource *dst,
238 unsigned dst_level,
239 unsigned dstx, unsigned dsty, unsigned dstz,
240 struct pipe_resource *src,
241 unsigned src_level,
242 const struct pipe_box *srcbox);
243
244 /**
245 * This is a generic implementation of pipe->blit, which accepts
246 * sampler/surface views instead of resources.
247 *
248 * The layer and mipmap level are specified by the views.
249 *
250 * Drivers can use this to change resource properties (like format, width,
251 * height) by changing how the views interpret them, instead of changing
252 * pipe_resource directly. This is used to blit resources of formats which
253 * are not renderable.
254 *
255 * src_width0 and src_height0 are sampler_view-private properties that
256 * override pipe_resource. The blitter uses them for computation of texture
257 * coordinates. The dst dimensions are supplied through pipe_surface::width
258 * and height.
259 *
260 * The mask is a combination of the PIPE_MASK_* flags.
261 * Set to PIPE_MASK_RGBAZS if unsure.
262 */
263 void util_blitter_blit_generic(struct blitter_context *blitter,
264 struct pipe_surface *dst,
265 const struct pipe_box *dstbox,
266 struct pipe_sampler_view *src,
267 const struct pipe_box *srcbox,
268 unsigned src_width0, unsigned src_height0,
269 unsigned mask, unsigned filter,
270 const struct pipe_scissor_state *scissor,
271 bool alpha_blend);
272
273 void util_blitter_blit(struct blitter_context *blitter,
274 const struct pipe_blit_info *info);
275
276 void util_blitter_generate_mipmap(struct blitter_context *blitter,
277 struct pipe_resource *tex,
278 enum pipe_format format,
279 unsigned base_level, unsigned last_level,
280 unsigned first_layer, unsigned last_layer);
281
282 /**
283 * Helper function to initialize a view for copy_texture_view.
284 * The parameters must match copy_texture_view.
285 */
286 void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
287 struct pipe_resource *dst,
288 unsigned dstlevel,
289 unsigned dstz);
290
291 /**
292 * Helper function to initialize a view for copy_texture_view.
293 * The parameters must match copy_texture_view.
294 */
295 void util_blitter_default_src_texture(struct blitter_context *blitter,
296 struct pipe_sampler_view *src_templ,
297 struct pipe_resource *src,
298 unsigned srclevel);
299
300 /**
301 * Copy data from one buffer to another using the Stream Output functionality.
302 * 4-byte alignment is required, otherwise software fallback is used.
303 */
304 void util_blitter_copy_buffer(struct blitter_context *blitter,
305 struct pipe_resource *dst,
306 unsigned dstx,
307 struct pipe_resource *src,
308 unsigned srcx,
309 unsigned size);
310
311 /**
312 * Clear the contents of a buffer using the Stream Output functionality.
313 * 4-byte alignment is required.
314 *
315 * "num_channels" can be 1, 2, 3, or 4, and specifies if the clear value is
316 * R, RG, RGB, or RGBA.
317 *
318 * For each element, only "num_channels" components of "clear_value" are
319 * copied to the buffer, then the offset is incremented by num_channels*4.
320 */
321 void util_blitter_clear_buffer(struct blitter_context *blitter,
322 struct pipe_resource *dst,
323 unsigned offset, unsigned size,
324 unsigned num_channels,
325 const union pipe_color_union *clear_value);
326
327 /**
328 * Clear a region of a (color) surface to a constant value.
329 *
330 * These states must be saved in the blitter in addition to the state objects
331 * already required to be saved:
332 * - fragment shader
333 * - depth stencil alpha state
334 * - blend state
335 * - framebuffer state
336 */
337 void util_blitter_clear_render_target(struct blitter_context *blitter,
338 struct pipe_surface *dst,
339 const union pipe_color_union *color,
340 unsigned dstx, unsigned dsty,
341 unsigned width, unsigned height);
342
343 /**
344 * Clear a region of a depth-stencil surface, both stencil and depth
345 * or only one of them if this is a combined depth-stencil surface.
346 *
347 * These states must be saved in the blitter in addition to the state objects
348 * already required to be saved:
349 * - fragment shader
350 * - depth stencil alpha state
351 * - blend state
352 * - framebuffer state
353 */
354 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
355 struct pipe_surface *dst,
356 unsigned clear_flags,
357 double depth,
358 unsigned stencil,
359 unsigned dstx, unsigned dsty,
360 unsigned width, unsigned height);
361
362 /* The following functions are customized variants of the clear functions.
363 * Some drivers use them internally to do things like MSAA resolve
364 * and resource decompression. It usually consists of rendering a full-screen
365 * quad with a special blend or DSA state.
366 */
367
368 /* Used by r300g for depth decompression. */
369 void util_blitter_custom_clear_depth(struct blitter_context *blitter,
370 unsigned width, unsigned height,
371 double depth, void *custom_dsa);
372
373 /* Used by r600g for depth decompression. */
374 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
375 struct pipe_surface *zsurf,
376 struct pipe_surface *cbsurf,
377 unsigned sample_mask,
378 void *dsa_stage, float depth);
379
380 /* Used by r600g for color decompression. */
381 void util_blitter_custom_color(struct blitter_context *blitter,
382 struct pipe_surface *dstsurf,
383 void *custom_blend);
384
385 /* Used by r600g for MSAA color resolve. */
386 void util_blitter_custom_resolve_color(struct blitter_context *blitter,
387 struct pipe_resource *dst,
388 unsigned dst_level,
389 unsigned dst_layer,
390 struct pipe_resource *src,
391 unsigned src_layer,
392 unsigned sampled_mask,
393 void *custom_blend,
394 enum pipe_format format);
395
396 /* Used by vc4 for 8/16-bit linear-to-tiled blits */
397 void util_blitter_custom_shader(struct blitter_context *blitter,
398 struct pipe_surface *dstsurf,
399 void *custom_vs, void *custom_fs);
400
401 /* The functions below should be used to save currently bound constant state
402 * objects inside a driver. The objects are automatically restored at the end
403 * of the util_blitter_{clear, copy_region, fill_region} functions and then
404 * forgotten.
405 *
406 * States not listed here are not affected by util_blitter. */
407
408 static inline void
409 util_blitter_save_blend(struct blitter_context *blitter, void *state)
410 {
411 blitter->saved_blend_state = state;
412 }
413
414 static inline void
415 util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter,
416 void *state)
417 {
418 blitter->saved_dsa_state = state;
419 }
420
421 static inline void
422 util_blitter_save_vertex_elements(struct blitter_context *blitter, void *state)
423 {
424 blitter->saved_velem_state = state;
425 }
426
427 static inline void
428 util_blitter_save_stencil_ref(struct blitter_context *blitter,
429 const struct pipe_stencil_ref *state)
430 {
431 blitter->saved_stencil_ref = *state;
432 }
433
434 static inline void
435 util_blitter_save_rasterizer(struct blitter_context *blitter, void *state)
436 {
437 blitter->saved_rs_state = state;
438 }
439
440 static inline void
441 util_blitter_save_fragment_shader(struct blitter_context *blitter, void *fs)
442 {
443 blitter->saved_fs = fs;
444 }
445
446 static inline void
447 util_blitter_save_vertex_shader(struct blitter_context *blitter, void *vs)
448 {
449 blitter->saved_vs = vs;
450 }
451
452 static inline void
453 util_blitter_save_geometry_shader(struct blitter_context *blitter, void *gs)
454 {
455 blitter->saved_gs = gs;
456 }
457
458 static inline void
459 util_blitter_save_tessctrl_shader(struct blitter_context *blitter,
460 void *sh)
461 {
462 blitter->saved_tcs = sh;
463 }
464
465 static inline void
466 util_blitter_save_tesseval_shader(struct blitter_context *blitter,
467 void *sh)
468 {
469 blitter->saved_tes = sh;
470 }
471
472 static inline void
473 util_blitter_save_framebuffer(struct blitter_context *blitter,
474 const struct pipe_framebuffer_state *state)
475 {
476 blitter->saved_fb_state.nr_cbufs = 0; /* It's ~0 now, meaning it's unsaved. */
477 util_copy_framebuffer_state(&blitter->saved_fb_state, state);
478 }
479
480 static inline void
481 util_blitter_save_viewport(struct blitter_context *blitter,
482 struct pipe_viewport_state *state)
483 {
484 blitter->saved_viewport = *state;
485 }
486
487 static inline void
488 util_blitter_save_scissor(struct blitter_context *blitter,
489 struct pipe_scissor_state *state)
490 {
491 blitter->saved_scissor = *state;
492 }
493
494 static inline void
495 util_blitter_save_fragment_sampler_states(
496 struct blitter_context *blitter,
497 unsigned num_sampler_states,
498 void **sampler_states)
499 {
500 assert(num_sampler_states <= ARRAY_SIZE(blitter->saved_sampler_states));
501
502 blitter->saved_num_sampler_states = num_sampler_states;
503 memcpy(blitter->saved_sampler_states, sampler_states,
504 num_sampler_states * sizeof(void *));
505 }
506
507 static inline void
508 util_blitter_save_fragment_sampler_views(struct blitter_context *blitter,
509 unsigned num_views,
510 struct pipe_sampler_view **views)
511 {
512 unsigned i;
513 assert(num_views <= ARRAY_SIZE(blitter->saved_sampler_views));
514
515 blitter->saved_num_sampler_views = num_views;
516 for (i = 0; i < num_views; i++)
517 pipe_sampler_view_reference(&blitter->saved_sampler_views[i],
518 views[i]);
519 }
520
521 static inline void
522 util_blitter_save_fragment_constant_buffer_slot(
523 struct blitter_context *blitter,
524 struct pipe_constant_buffer *constant_buffers)
525 {
526 pipe_resource_reference(&blitter->saved_fs_constant_buffer.buffer,
527 constant_buffers[blitter->cb_slot].buffer);
528 memcpy(&blitter->saved_fs_constant_buffer, &constant_buffers[blitter->cb_slot],
529 sizeof(struct pipe_constant_buffer));
530 }
531
532 static inline void
533 util_blitter_save_vertex_buffer_slot(struct blitter_context *blitter,
534 struct pipe_vertex_buffer *vertex_buffers)
535 {
536 pipe_vertex_buffer_reference(&blitter->saved_vertex_buffer,
537 &vertex_buffers[blitter->vb_slot]);
538 }
539
540 static inline void
541 util_blitter_save_so_targets(struct blitter_context *blitter,
542 unsigned num_targets,
543 struct pipe_stream_output_target **targets)
544 {
545 unsigned i;
546 assert(num_targets <= ARRAY_SIZE(blitter->saved_so_targets));
547
548 blitter->saved_num_so_targets = num_targets;
549 for (i = 0; i < num_targets; i++)
550 pipe_so_target_reference(&blitter->saved_so_targets[i],
551 targets[i]);
552 }
553
554 static inline void
555 util_blitter_save_sample_mask(struct blitter_context *blitter,
556 unsigned sample_mask)
557 {
558 blitter->is_sample_mask_saved = true;
559 blitter->saved_sample_mask = sample_mask;
560 }
561
562 static inline void
563 util_blitter_save_render_condition(struct blitter_context *blitter,
564 struct pipe_query *query,
565 bool condition,
566 enum pipe_render_cond_flag mode)
567 {
568 blitter->saved_render_cond_query = query;
569 blitter->saved_render_cond_mode = mode;
570 blitter->saved_render_cond_cond = condition;
571 }
572
573 static inline void
574 util_blitter_save_window_rectangles(struct blitter_context *blitter,
575 boolean include,
576 unsigned num_rectangles,
577 const struct pipe_scissor_state *rects)
578 {
579 blitter->saved_window_rectangles_include = include;
580 blitter->saved_num_window_rectangles = num_rectangles;
581 if (num_rectangles > 0) {
582 assert(num_rectangles < ARRAY_SIZE(blitter->saved_window_rectangles));
583 memcpy(blitter->saved_window_rectangles, rects,
584 sizeof(*rects) * num_rectangles);
585 }
586 }
587
588 void util_blitter_common_clear_setup(struct blitter_context *blitter,
589 unsigned width, unsigned height,
590 unsigned clear_buffers,
591 void *custom_blend, void *custom_dsa);
592
593 void util_blitter_set_running_flag(struct blitter_context *blitter);
594 void util_blitter_unset_running_flag(struct blitter_context *blitter);
595
596 void util_blitter_restore_vertex_states(struct blitter_context *blitter);
597 void util_blitter_restore_fragment_states(struct blitter_context *blitter);
598 void util_blitter_restore_render_cond(struct blitter_context *blitter);
599 void util_blitter_restore_fb_state(struct blitter_context *blitter);
600 void util_blitter_restore_textures(struct blitter_context *blitter);
601 void util_blitter_restore_constant_buffer_state(struct blitter_context *blitter);
602
603 /* These are supported combinations of blits from ZS to color and vice versa.
604 * The blitter will do the packing/unpacking of depth and stencil
605 * in the fragment shader.
606 */
607 static inline enum pipe_format
608 util_blitter_get_color_format_for_zs(enum pipe_format format)
609 {
610 switch (format) {
611 case PIPE_FORMAT_Z16_UNORM:
612 return PIPE_FORMAT_R16_UNORM;
613
614 case PIPE_FORMAT_Z32_FLOAT:
615 return PIPE_FORMAT_R32_FLOAT;
616
617 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
618 case PIPE_FORMAT_Z24X8_UNORM:
619 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
620 case PIPE_FORMAT_X8Z24_UNORM:
621 return PIPE_FORMAT_R32_UINT;
622
623 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
624 return PIPE_FORMAT_R32G32_UINT;
625
626 case PIPE_FORMAT_Z32_UNORM:
627 default:
628 assert(0);
629 }
630 return PIPE_FORMAT_NONE;
631 }
632
633 #ifdef __cplusplus
634 }
635 #endif
636
637 #endif