1 /**************************************************************************
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
25 **************************************************************************/
30 #include "util/u_framebuffer.h"
31 #include "util/u_inlines.h"
33 #include "pipe/p_state.h"
41 enum blitter_attrib_type
{
42 UTIL_BLITTER_ATTRIB_NONE
,
43 UTIL_BLITTER_ATTRIB_COLOR
,
44 UTIL_BLITTER_ATTRIB_TEXCOORD
47 union blitter_attrib
{
55 struct blitter_context
60 * \param x1 An X coordinate of the top-left corner.
61 * \param y1 A Y coordinate of the top-left corner.
62 * \param x2 An X coordinate of the bottom-right corner.
63 * \param y2 A Y coordinate of the bottom-right corner.
64 * \param depth A depth which the rectangle is rendered at.
66 * \param type Semantics of the attributes "attrib".
67 * If type is UTIL_BLITTER_ATTRIB_NONE, ignore them.
68 * If type is UTIL_BLITTER_ATTRIB_COLOR, the attributes
69 * make up a constant RGBA color, and should go
70 * to the GENERIC0 varying slot of a fragment shader.
71 * If type is UTIL_BLITTER_ATTRIB_TEXCOORD, {a1, a2} and
72 * {a3, a4} specify top-left and bottom-right texture
73 * coordinates of the rectangle, respectively, and should go
74 * to the GENERIC0 varying slot of a fragment shader.
76 * \param attrib See type.
78 * \note A driver may optionally override this callback to implement
79 * a specialized hardware path for drawing a rectangle, e.g. using
80 * a rectangular point sprite.
82 void (*draw_rectangle
)(struct blitter_context
*blitter
,
83 int x1
, int y1
, int x2
, int y2
,
84 float depth
, unsigned num_instances
,
85 enum blitter_attrib_type type
,
86 const union blitter_attrib
*attrib
);
88 /* Whether the blitter is running. */
91 /* Private members, really. */
92 struct pipe_context
*pipe
; /**< pipe context */
94 void *saved_blend_state
; /**< blend state */
95 void *saved_dsa_state
; /**< depth stencil alpha state */
96 void *saved_velem_state
; /**< vertex elements state */
97 void *saved_rs_state
; /**< rasterizer state */
98 void *saved_fs
, *saved_vs
, *saved_gs
, *saved_tcs
, *saved_tes
; /**< shaders */
100 struct pipe_framebuffer_state saved_fb_state
; /**< framebuffer state */
101 struct pipe_stencil_ref saved_stencil_ref
; /**< stencil ref */
102 struct pipe_viewport_state saved_viewport
;
103 struct pipe_scissor_state saved_scissor
;
104 bool is_sample_mask_saved
;
105 unsigned saved_sample_mask
;
107 unsigned saved_num_sampler_states
;
108 void *saved_sampler_states
[PIPE_MAX_SAMPLERS
];
110 unsigned saved_num_sampler_views
;
111 struct pipe_sampler_view
*saved_sampler_views
[PIPE_MAX_SAMPLERS
];
114 struct pipe_constant_buffer saved_fs_constant_buffer
;
117 struct pipe_vertex_buffer saved_vertex_buffer
;
119 unsigned saved_num_so_targets
;
120 struct pipe_stream_output_target
*saved_so_targets
[PIPE_MAX_SO_BUFFERS
];
122 struct pipe_query
*saved_render_cond_query
;
123 uint saved_render_cond_mode
;
124 bool saved_render_cond_cond
;
128 * Create a blitter context.
130 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
);
133 * Destroy a blitter context.
135 void util_blitter_destroy(struct blitter_context
*blitter
);
137 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
);
140 * Return the pipe context associated with a blitter context.
143 struct pipe_context
*util_blitter_get_pipe(struct blitter_context
*blitter
)
145 return blitter
->pipe
;
149 * Override PIPE_CAP_TEXTURE_MULTISAMPLE as reported by the driver.
151 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
154 /* The default function to draw a rectangle. This can only be used
155 * inside of the draw_rectangle callback if the driver overrides it. */
156 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
157 int x1
, int y1
, int x2
, int y2
,
158 float depth
, unsigned num_instances
,
159 enum blitter_attrib_type type
,
160 const union blitter_attrib
*attrib
);
164 * These states must be saved before any of the following functions are called:
168 * - geometry shader (if supported)
169 * - stream output targets (if supported)
174 * Clear a specified set of currently bound buffers to specified values.
176 * These states must be saved in the blitter in addition to the state objects
177 * already required to be saved:
179 * - depth stencil alpha state
182 void util_blitter_clear(struct blitter_context
*blitter
,
183 unsigned width
, unsigned height
, unsigned num_layers
,
184 unsigned clear_buffers
,
185 const union pipe_color_union
*color
,
186 double depth
, unsigned stencil
);
189 * Check if the blitter (with the help of the driver) can blit between
192 bool util_blitter_is_copy_supported(struct blitter_context
*blitter
,
193 const struct pipe_resource
*dst
,
194 const struct pipe_resource
*src
);
196 bool util_blitter_is_blit_supported(struct blitter_context
*blitter
,
197 const struct pipe_blit_info
*info
);
200 * Copy a block of pixels from one surface to another.
202 * These states must be saved in the blitter in addition to the state objects
203 * already required to be saved:
205 * - depth stencil alpha state
207 * - fragment sampler states
208 * - fragment sampler textures
209 * - framebuffer state
212 void util_blitter_copy_texture(struct blitter_context
*blitter
,
213 struct pipe_resource
*dst
,
215 unsigned dstx
, unsigned dsty
, unsigned dstz
,
216 struct pipe_resource
*src
,
218 const struct pipe_box
*srcbox
);
221 * This is a generic implementation of pipe->blit, which accepts
222 * sampler/surface views instead of resources.
224 * The layer and mipmap level are specified by the views.
226 * Drivers can use this to change resource properties (like format, width,
227 * height) by changing how the views interpret them, instead of changing
228 * pipe_resource directly. This is used to blit resources of formats which
229 * are not renderable.
231 * src_width0 and src_height0 are sampler_view-private properties that
232 * override pipe_resource. The blitter uses them for computation of texture
233 * coordinates. The dst dimensions are supplied through pipe_surface::width
236 * The mask is a combination of the PIPE_MASK_* flags.
237 * Set to PIPE_MASK_RGBAZS if unsure.
239 void util_blitter_blit_generic(struct blitter_context
*blitter
,
240 struct pipe_surface
*dst
,
241 const struct pipe_box
*dstbox
,
242 struct pipe_sampler_view
*src
,
243 const struct pipe_box
*srcbox
,
244 unsigned src_width0
, unsigned src_height0
,
245 unsigned mask
, unsigned filter
,
246 const struct pipe_scissor_state
*scissor
,
249 void util_blitter_blit(struct blitter_context
*blitter
,
250 const struct pipe_blit_info
*info
);
252 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
253 struct pipe_resource
*tex
,
254 enum pipe_format format
,
255 unsigned base_level
, unsigned last_level
,
256 unsigned first_layer
, unsigned last_layer
);
259 * Helper function to initialize a view for copy_texture_view.
260 * The parameters must match copy_texture_view.
262 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
263 struct pipe_resource
*dst
,
268 * Helper function to initialize a view for copy_texture_view.
269 * The parameters must match copy_texture_view.
271 void util_blitter_default_src_texture(struct blitter_context
*blitter
,
272 struct pipe_sampler_view
*src_templ
,
273 struct pipe_resource
*src
,
277 * Copy data from one buffer to another using the Stream Output functionality.
278 * 4-byte alignment is required, otherwise software fallback is used.
280 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
281 struct pipe_resource
*dst
,
283 struct pipe_resource
*src
,
288 * Clear the contents of a buffer using the Stream Output functionality.
289 * 4-byte alignment is required.
291 * "num_channels" can be 1, 2, 3, or 4, and specifies if the clear value is
292 * R, RG, RGB, or RGBA.
294 * For each element, only "num_channels" components of "clear_value" are
295 * copied to the buffer, then the offset is incremented by num_channels*4.
297 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
298 struct pipe_resource
*dst
,
299 unsigned offset
, unsigned size
,
300 unsigned num_channels
,
301 const union pipe_color_union
*clear_value
);
304 * Clear a region of a (color) surface to a constant value.
306 * These states must be saved in the blitter in addition to the state objects
307 * already required to be saved:
309 * - depth stencil alpha state
311 * - framebuffer state
313 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
314 struct pipe_surface
*dst
,
315 const union pipe_color_union
*color
,
316 unsigned dstx
, unsigned dsty
,
317 unsigned width
, unsigned height
);
320 * Clear a region of a depth-stencil surface, both stencil and depth
321 * or only one of them if this is a combined depth-stencil surface.
323 * These states must be saved in the blitter in addition to the state objects
324 * already required to be saved:
326 * - depth stencil alpha state
328 * - framebuffer state
330 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
331 struct pipe_surface
*dst
,
332 unsigned clear_flags
,
335 unsigned dstx
, unsigned dsty
,
336 unsigned width
, unsigned height
);
338 /* The following functions are customized variants of the clear functions.
339 * Some drivers use them internally to do things like MSAA resolve
340 * and resource decompression. It usually consists of rendering a full-screen
341 * quad with a special blend or DSA state.
344 /* Used by r300g for depth decompression. */
345 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
346 unsigned width
, unsigned height
,
347 double depth
, void *custom_dsa
);
349 /* Used by r600g for depth decompression. */
350 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
351 struct pipe_surface
*zsurf
,
352 struct pipe_surface
*cbsurf
,
353 unsigned sample_mask
,
354 void *dsa_stage
, float depth
);
356 /* Used by r600g for color decompression. */
357 void util_blitter_custom_color(struct blitter_context
*blitter
,
358 struct pipe_surface
*dstsurf
,
361 /* Used by r600g for MSAA color resolve. */
362 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
363 struct pipe_resource
*dst
,
366 struct pipe_resource
*src
,
368 unsigned sampled_mask
,
370 enum pipe_format format
);
372 /* The functions below should be used to save currently bound constant state
373 * objects inside a driver. The objects are automatically restored at the end
374 * of the util_blitter_{clear, copy_region, fill_region} functions and then
377 * States not listed here are not affected by util_blitter. */
380 util_blitter_save_blend(struct blitter_context
*blitter
, void *state
)
382 blitter
->saved_blend_state
= state
;
386 util_blitter_save_depth_stencil_alpha(struct blitter_context
*blitter
,
389 blitter
->saved_dsa_state
= state
;
393 util_blitter_save_vertex_elements(struct blitter_context
*blitter
, void *state
)
395 blitter
->saved_velem_state
= state
;
399 util_blitter_save_stencil_ref(struct blitter_context
*blitter
,
400 const struct pipe_stencil_ref
*state
)
402 blitter
->saved_stencil_ref
= *state
;
406 util_blitter_save_rasterizer(struct blitter_context
*blitter
, void *state
)
408 blitter
->saved_rs_state
= state
;
412 util_blitter_save_fragment_shader(struct blitter_context
*blitter
, void *fs
)
414 blitter
->saved_fs
= fs
;
418 util_blitter_save_vertex_shader(struct blitter_context
*blitter
, void *vs
)
420 blitter
->saved_vs
= vs
;
424 util_blitter_save_geometry_shader(struct blitter_context
*blitter
, void *gs
)
426 blitter
->saved_gs
= gs
;
430 util_blitter_save_tessctrl_shader(struct blitter_context
*blitter
,
433 blitter
->saved_tcs
= sh
;
437 util_blitter_save_tesseval_shader(struct blitter_context
*blitter
,
440 blitter
->saved_tes
= sh
;
444 util_blitter_save_framebuffer(struct blitter_context
*blitter
,
445 const struct pipe_framebuffer_state
*state
)
447 blitter
->saved_fb_state
.nr_cbufs
= 0; /* It's ~0 now, meaning it's unsaved. */
448 util_copy_framebuffer_state(&blitter
->saved_fb_state
, state
);
452 util_blitter_save_viewport(struct blitter_context
*blitter
,
453 struct pipe_viewport_state
*state
)
455 blitter
->saved_viewport
= *state
;
459 util_blitter_save_scissor(struct blitter_context
*blitter
,
460 struct pipe_scissor_state
*state
)
462 blitter
->saved_scissor
= *state
;
466 util_blitter_save_fragment_sampler_states(
467 struct blitter_context
*blitter
,
468 unsigned num_sampler_states
,
469 void **sampler_states
)
471 assert(num_sampler_states
<= ARRAY_SIZE(blitter
->saved_sampler_states
));
473 blitter
->saved_num_sampler_states
= num_sampler_states
;
474 memcpy(blitter
->saved_sampler_states
, sampler_states
,
475 num_sampler_states
* sizeof(void *));
479 util_blitter_save_fragment_sampler_views(struct blitter_context
*blitter
,
481 struct pipe_sampler_view
**views
)
484 assert(num_views
<= ARRAY_SIZE(blitter
->saved_sampler_views
));
486 blitter
->saved_num_sampler_views
= num_views
;
487 for (i
= 0; i
< num_views
; i
++)
488 pipe_sampler_view_reference(&blitter
->saved_sampler_views
[i
],
493 util_blitter_save_fragment_constant_buffer_slot(
494 struct blitter_context
*blitter
,
495 struct pipe_constant_buffer
*constant_buffers
)
497 pipe_resource_reference(&blitter
->saved_fs_constant_buffer
.buffer
,
498 constant_buffers
[blitter
->cb_slot
].buffer
);
499 memcpy(&blitter
->saved_fs_constant_buffer
, &constant_buffers
[blitter
->cb_slot
],
500 sizeof(struct pipe_constant_buffer
));
504 util_blitter_save_vertex_buffer_slot(struct blitter_context
*blitter
,
505 struct pipe_vertex_buffer
*vertex_buffers
)
507 pipe_vertex_buffer_reference(&blitter
->saved_vertex_buffer
,
508 &vertex_buffers
[blitter
->vb_slot
]);
512 util_blitter_save_so_targets(struct blitter_context
*blitter
,
513 unsigned num_targets
,
514 struct pipe_stream_output_target
**targets
)
517 assert(num_targets
<= ARRAY_SIZE(blitter
->saved_so_targets
));
519 blitter
->saved_num_so_targets
= num_targets
;
520 for (i
= 0; i
< num_targets
; i
++)
521 pipe_so_target_reference(&blitter
->saved_so_targets
[i
],
526 util_blitter_save_sample_mask(struct blitter_context
*blitter
,
527 unsigned sample_mask
)
529 blitter
->is_sample_mask_saved
= true;
530 blitter
->saved_sample_mask
= sample_mask
;
534 util_blitter_save_render_condition(struct blitter_context
*blitter
,
535 struct pipe_query
*query
,
537 enum pipe_render_cond_flag mode
)
539 blitter
->saved_render_cond_query
= query
;
540 blitter
->saved_render_cond_mode
= mode
;
541 blitter
->saved_render_cond_cond
= condition
;
544 void util_blitter_common_clear_setup(struct blitter_context
*blitter
,
545 unsigned width
, unsigned height
,
546 unsigned clear_buffers
,
547 void *custom_blend
, void *custom_dsa
);
549 void util_blitter_set_running_flag(struct blitter_context
*blitter
);
550 void util_blitter_unset_running_flag(struct blitter_context
*blitter
);
552 void util_blitter_restore_vertex_states(struct blitter_context
*blitter
);
553 void util_blitter_restore_fragment_states(struct blitter_context
*blitter
);
554 void util_blitter_restore_render_cond(struct blitter_context
*blitter
);
555 void util_blitter_restore_fb_state(struct blitter_context
*blitter
);
556 void util_blitter_restore_textures(struct blitter_context
*blitter
);
557 void util_blitter_restore_constant_buffer_state(struct blitter_context
*blitter
);