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 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.
25 **************************************************************************/
30 #include "util/u_framebuffer.h"
31 #include "util/u_inlines.h"
32 #include "util/u_memory.h"
34 #include "pipe/p_state.h"
43 enum blitter_attrib_type
{
44 UTIL_BLITTER_ATTRIB_NONE
,
45 UTIL_BLITTER_ATTRIB_COLOR
,
46 UTIL_BLITTER_ATTRIB_TEXCOORD
49 struct blitter_context
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.
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.
70 * \param attrib See type.
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.
76 void (*draw_rectangle
)(struct blitter_context
*blitter
,
77 unsigned x1
, unsigned y1
, unsigned x2
, unsigned y2
,
79 enum blitter_attrib_type type
,
80 const union pipe_color_union
*color
);
82 /* Whether the blitter is running. */
85 /* Private members, really. */
86 struct pipe_context
*pipe
; /**< pipe context */
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 */
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
;
100 int saved_num_sampler_states
;
101 void *saved_sampler_states
[PIPE_MAX_SAMPLERS
];
103 int saved_num_sampler_views
;
104 struct pipe_sampler_view
*saved_sampler_views
[PIPE_MAX_SAMPLERS
];
106 int saved_num_vertex_buffers
;
107 struct pipe_vertex_buffer saved_vertex_buffers
[PIPE_MAX_ATTRIBS
];
109 int saved_num_so_targets
;
110 struct pipe_stream_output_target
*saved_so_targets
[PIPE_MAX_SO_BUFFERS
];
112 struct pipe_query
*saved_render_cond_query
;
113 uint saved_render_cond_mode
;
117 * Create a blitter context.
119 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
);
122 * Destroy a blitter context.
124 void util_blitter_destroy(struct blitter_context
*blitter
);
126 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
);
129 * Return the pipe context associated with a blitter context.
132 struct pipe_context
*util_blitter_get_pipe(struct blitter_context
*blitter
)
134 return blitter
->pipe
;
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
,
143 enum blitter_attrib_type type
,
144 const union pipe_color_union
*attrib
);
147 * These states must be saved before any of the following functions are called:
151 * - geometry shader (if supported)
152 * - stream output targets (if supported)
157 * Clear a specified set of currently bound buffers to specified values.
159 * These states must be saved in the blitter in addition to the state objects
160 * already required to be saved:
162 * - depth stencil alpha state
165 void util_blitter_clear(struct blitter_context
*blitter
,
166 unsigned width
, unsigned height
,
168 unsigned clear_buffers
,
169 enum pipe_format cbuf_format
,
170 const union pipe_color_union
*color
,
171 double depth
, unsigned stencil
);
174 * Check if the blitter (with the help of the driver) can blit between
176 * The mask is a combination of the PIPE_MASK_* flags.
177 * Set to PIPE_MASK_RGBAZS if unsure.
179 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
180 const struct pipe_resource
*dst
,
181 const struct pipe_resource
*src
,
184 * Copy a block of pixels from one surface to another.
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
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
196 * These states must be saved in the blitter in addition to the state objects
197 * already required to be saved:
199 * - depth stencil alpha state
201 * - fragment sampler states
202 * - fragment sampler textures
203 * - framebuffer state
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
);
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
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.
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
228 * The mask is a combination of the PIPE_MASK_* flags.
229 * Set to PIPE_MASK_RGBAZS if unsure.
231 * NOTE: There are no checks whether the blit is actually supported.
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
,
239 const struct pipe_box
*srcbox
,
240 unsigned src_width0
, unsigned src_height0
,
244 * Helper function to initialize a view for copy_texture_view.
245 * The parameters must match copy_texture_view.
247 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
248 struct pipe_resource
*dst
,
251 const struct pipe_box
*srcbox
);
254 * Helper function to initialize a view for copy_texture_view.
255 * The parameters must match copy_texture_view.
257 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
258 struct pipe_resource
*src
,
262 * Copy data from one buffer to another using the Stream Output functionality.
263 * Some alignment is required, otherwise software fallback is used.
265 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
266 struct pipe_resource
*dst
,
268 struct pipe_resource
*src
,
273 * Clear a region of a (color) surface to a constant value.
275 * These states must be saved in the blitter in addition to the state objects
276 * already required to be saved:
278 * - depth stencil alpha state
280 * - framebuffer state
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
);
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.
292 * These states must be saved in the blitter in addition to the state objects
293 * already required to be saved:
295 * - depth stencil alpha state
297 * - framebuffer state
299 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
300 struct pipe_surface
*dst
,
301 unsigned clear_flags
,
304 unsigned dstx
, unsigned dsty
,
305 unsigned width
, unsigned height
);
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.
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
);
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
);
325 /* Used by r600g for color decompression. */
326 void util_blitter_custom_color(struct blitter_context
*blitter
,
327 struct pipe_surface
*dstsurf
,
330 /* Used by r600g for MSAA color resolve. */
331 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
332 struct pipe_resource
*dst
,
335 struct pipe_resource
*src
,
337 unsigned sampled_mask
,
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
345 * States not listed here are not affected by util_blitter. */
348 void util_blitter_save_blend(struct blitter_context
*blitter
,
351 blitter
->saved_blend_state
= state
;
355 void util_blitter_save_depth_stencil_alpha(struct blitter_context
*blitter
,
358 blitter
->saved_dsa_state
= state
;
362 void util_blitter_save_vertex_elements(struct blitter_context
*blitter
,
365 blitter
->saved_velem_state
= state
;
369 void util_blitter_save_stencil_ref(struct blitter_context
*blitter
,
370 const struct pipe_stencil_ref
*state
)
372 blitter
->saved_stencil_ref
= *state
;
376 void util_blitter_save_rasterizer(struct blitter_context
*blitter
,
379 blitter
->saved_rs_state
= state
;
383 void util_blitter_save_fragment_shader(struct blitter_context
*blitter
,
386 blitter
->saved_fs
= fs
;
390 void util_blitter_save_vertex_shader(struct blitter_context
*blitter
,
393 blitter
->saved_vs
= vs
;
397 void util_blitter_save_geometry_shader(struct blitter_context
*blitter
,
400 blitter
->saved_gs
= gs
;
404 void util_blitter_save_framebuffer(struct blitter_context
*blitter
,
405 const struct pipe_framebuffer_state
*state
)
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
);
412 void util_blitter_save_viewport(struct blitter_context
*blitter
,
413 struct pipe_viewport_state
*state
)
415 blitter
->saved_viewport
= *state
;
419 void util_blitter_save_fragment_sampler_states(
420 struct blitter_context
*blitter
,
421 int num_sampler_states
,
422 void **sampler_states
)
424 assert(num_sampler_states
<= Elements(blitter
->saved_sampler_states
));
426 blitter
->saved_num_sampler_states
= num_sampler_states
;
427 memcpy(blitter
->saved_sampler_states
, sampler_states
,
428 num_sampler_states
* sizeof(void *));
432 util_blitter_save_fragment_sampler_views(struct blitter_context
*blitter
,
434 struct pipe_sampler_view
**views
)
437 assert(num_views
<= Elements(blitter
->saved_sampler_views
));
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
],
446 util_blitter_save_vertex_buffers(struct blitter_context
*blitter
,
447 int num_vertex_buffers
,
448 struct pipe_vertex_buffer
*vertex_buffers
)
450 assert(num_vertex_buffers
<= Elements(blitter
->saved_vertex_buffers
));
452 blitter
->saved_num_vertex_buffers
= 0;
453 util_copy_vertex_buffers(blitter
->saved_vertex_buffers
,
454 (unsigned*)&blitter
->saved_num_vertex_buffers
,
460 util_blitter_save_so_targets(struct blitter_context
*blitter
,
462 struct pipe_stream_output_target
**targets
)
465 assert(num_targets
<= Elements(blitter
->saved_so_targets
));
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
],
474 util_blitter_save_sample_mask(struct blitter_context
*blitter
,
475 unsigned sample_mask
)
477 blitter
->is_sample_mask_saved
= TRUE
;
478 blitter
->saved_sample_mask
= sample_mask
;
482 util_blitter_save_render_condition(struct blitter_context
*blitter
,
483 struct pipe_query
*query
,
486 blitter
->saved_render_cond_query
= query
;
487 blitter
->saved_render_cond_mode
= mode
;