u_blitter.h revision 6c420b166814fccb160e39cc9ea8cbdaf4e7c830
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#include "util/u_memory.h" 33 34#include "pipe/p_state.h" 35 36 37#ifdef __cplusplus 38extern "C" { 39#endif 40 41struct pipe_context; 42 43enum blitter_attrib_type { 44 UTIL_BLITTER_ATTRIB_NONE, 45 UTIL_BLITTER_ATTRIB_COLOR, 46 UTIL_BLITTER_ATTRIB_TEXCOORD 47}; 48 49struct blitter_context 50{ 51 /** 52 * Draw a rectangle. 53 * 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. 59 * 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. 69 * 70 * \param attrib See type. 71 * 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. 75 */ 76 void (*draw_rectangle)(struct blitter_context *blitter, 77 unsigned x1, unsigned y1, unsigned x2, unsigned y2, 78 float depth, 79 enum blitter_attrib_type type, 80 const union pipe_color_union *color); 81 82 /* Whether the blitter is running. */ 83 boolean running; 84 85 /* Private members, really. */ 86 struct pipe_context *pipe; /**< pipe context */ 87 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 */ 93 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; 99 100 int saved_num_sampler_states; 101 void *saved_sampler_states[PIPE_MAX_SAMPLERS]; 102 103 int saved_num_sampler_views; 104 struct pipe_sampler_view *saved_sampler_views[PIPE_MAX_SAMPLERS]; 105 106 int saved_num_vertex_buffers; 107 struct pipe_vertex_buffer saved_vertex_buffers[PIPE_MAX_ATTRIBS]; 108 109 int saved_num_so_targets; 110 struct pipe_stream_output_target *saved_so_targets[PIPE_MAX_SO_BUFFERS]; 111}; 112 113/** 114 * Create a blitter context. 115 */ 116struct blitter_context *util_blitter_create(struct pipe_context *pipe); 117 118/** 119 * Destroy a blitter context. 120 */ 121void util_blitter_destroy(struct blitter_context *blitter); 122 123/** 124 * Return the pipe context associated with a blitter context. 125 */ 126static INLINE 127struct pipe_context *util_blitter_get_pipe(struct blitter_context *blitter) 128{ 129 return blitter->pipe; 130} 131 132/* 133 * These states must be saved before any of the following functions are called: 134 * - vertex buffers 135 * - vertex elements 136 * - vertex shader 137 * - geometry shader (if supported) 138 * - stream output targets (if supported) 139 * - rasterizer state 140 */ 141 142/** 143 * Clear a specified set of currently bound buffers to specified values. 144 * 145 * These states must be saved in the blitter in addition to the state objects 146 * already required to be saved: 147 * - fragment shader 148 * - depth stencil alpha state 149 * - blend state 150 */ 151void util_blitter_clear(struct blitter_context *blitter, 152 unsigned width, unsigned height, 153 unsigned num_cbufs, 154 unsigned clear_buffers, 155 enum pipe_format cbuf_format, 156 const union pipe_color_union *color, 157 double depth, unsigned stencil); 158 159void util_blitter_clear_depth_custom(struct blitter_context *blitter, 160 unsigned width, unsigned height, 161 double depth, void *custom_dsa); 162 163/** 164 * Copy a block of pixels from one surface to another. 165 * 166 * You can copy from any color format to any other color format provided 167 * the former can be sampled from and the latter can be rendered to. Otherwise, 168 * a software fallback path is taken and both surfaces must be of the same 169 * format. 170 * 171 * The same holds for depth-stencil formats with the exception that stencil 172 * cannot be copied unless you set ignore_stencil to FALSE. In that case, 173 * a software fallback path is taken and both surfaces must be of the same 174 * format. If the shader stencil export is supported, stencil copy is always 175 * accelerated. 176 * 177 * Use pipe_screen->is_format_supported to know your options. 178 * 179 * These states must be saved in the blitter in addition to the state objects 180 * already required to be saved: 181 * - fragment shader 182 * - depth stencil alpha state 183 * - blend state 184 * - fragment sampler states 185 * - fragment sampler textures 186 * - framebuffer state 187 */ 188void util_blitter_copy_texture(struct blitter_context *blitter, 189 struct pipe_resource *dst, 190 unsigned dstlevel, 191 unsigned dstx, unsigned dsty, unsigned dstz, 192 struct pipe_resource *src, 193 unsigned srclevel, 194 const struct pipe_box *srcbox, 195 boolean ignore_stencil); 196 197/** 198 * Same as util_blitter_copy_texture, but dst and src are pipe_surface and 199 * pipe_sampler_view, respectively. The mipmap level and dstz are part of 200 * the views. 201 * 202 * Drivers can use this to change resource properties (like format, width, 203 * height) by changing how the views interpret them, instead of changing 204 * pipe_resource directly. This is usually needed to accelerate copying of 205 * compressed formats. 206 * 207 * src_width0 and src_height0 are sampler_view-private properties that 208 * override pipe_resource. The blitter uses them for computation of texture 209 * coordinates. The dst dimensions are supplied through pipe_surface::width 210 * and height. 211 * 212 * NOTE: There are no checks whether the blit is actually supported. 213 */ 214void util_blitter_copy_texture_view(struct blitter_context *blitter, 215 struct pipe_surface *dst, 216 unsigned dstx, unsigned dsty, 217 struct pipe_sampler_view *src, 218 const struct pipe_box *srcbox, 219 unsigned src_width0, unsigned src_height0); 220 221/** 222 * Helper function to initialize a view for copy_texture_view. 223 * The parameters must match copy_texture_view. 224 */ 225void util_blitter_default_dst_texture(struct pipe_surface *dst_templ, 226 struct pipe_resource *dst, 227 unsigned dstlevel, 228 unsigned dstz, 229 const struct pipe_box *srcbox); 230 231/** 232 * Helper function to initialize a view for copy_texture_view. 233 * The parameters must match copy_texture_view. 234 */ 235void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ, 236 struct pipe_resource *src, 237 unsigned srclevel); 238 239/** 240 * Copy data from one buffer to another using the Stream Output functionality. 241 * Some alignment is required, otherwise software fallback is used. 242 */ 243void util_blitter_copy_buffer(struct blitter_context *blitter, 244 struct pipe_resource *dst, 245 unsigned dstx, 246 struct pipe_resource *src, 247 unsigned srcx, 248 unsigned size); 249 250/** 251 * Clear a region of a (color) surface to a constant value. 252 * 253 * These states must be saved in the blitter in addition to the state objects 254 * already required to be saved: 255 * - fragment shader 256 * - depth stencil alpha state 257 * - blend state 258 * - framebuffer state 259 */ 260void util_blitter_clear_render_target(struct blitter_context *blitter, 261 struct pipe_surface *dst, 262 const union pipe_color_union *color, 263 unsigned dstx, unsigned dsty, 264 unsigned width, unsigned height); 265 266/** 267 * Clear a region of a depth-stencil surface, both stencil and depth 268 * or only one of them if this is a combined depth-stencil surface. 269 * 270 * These states must be saved in the blitter in addition to the state objects 271 * already required to be saved: 272 * - fragment shader 273 * - depth stencil alpha state 274 * - blend state 275 * - framebuffer state 276 */ 277void util_blitter_clear_depth_stencil(struct blitter_context *blitter, 278 struct pipe_surface *dst, 279 unsigned clear_flags, 280 double depth, 281 unsigned stencil, 282 unsigned dstx, unsigned dsty, 283 unsigned width, unsigned height); 284 285void util_blitter_custom_depth_stencil(struct blitter_context *blitter, 286 struct pipe_surface *zsurf, 287 struct pipe_surface *cbsurf, 288 void *dsa_stage, float depth); 289 290/* The functions below should be used to save currently bound constant state 291 * objects inside a driver. The objects are automatically restored at the end 292 * of the util_blitter_{clear, copy_region, fill_region} functions and then 293 * forgotten. 294 * 295 * States not listed here are not affected by util_blitter. */ 296 297static INLINE 298void util_blitter_save_blend(struct blitter_context *blitter, 299 void *state) 300{ 301 blitter->saved_blend_state = state; 302} 303 304static INLINE 305void util_blitter_save_depth_stencil_alpha(struct blitter_context *blitter, 306 void *state) 307{ 308 blitter->saved_dsa_state = state; 309} 310 311static INLINE 312void util_blitter_save_vertex_elements(struct blitter_context *blitter, 313 void *state) 314{ 315 blitter->saved_velem_state = state; 316} 317 318static INLINE 319void util_blitter_save_stencil_ref(struct blitter_context *blitter, 320 const struct pipe_stencil_ref *state) 321{ 322 blitter->saved_stencil_ref = *state; 323} 324 325static INLINE 326void util_blitter_save_rasterizer(struct blitter_context *blitter, 327 void *state) 328{ 329 blitter->saved_rs_state = state; 330} 331 332static INLINE 333void util_blitter_save_fragment_shader(struct blitter_context *blitter, 334 void *fs) 335{ 336 blitter->saved_fs = fs; 337} 338 339static INLINE 340void util_blitter_save_vertex_shader(struct blitter_context *blitter, 341 void *vs) 342{ 343 blitter->saved_vs = vs; 344} 345 346static INLINE 347void util_blitter_save_geometry_shader(struct blitter_context *blitter, 348 void *gs) 349{ 350 blitter->saved_gs = gs; 351} 352 353static INLINE 354void util_blitter_save_framebuffer(struct blitter_context *blitter, 355 const struct pipe_framebuffer_state *state) 356{ 357 blitter->saved_fb_state.nr_cbufs = 0; /* It's ~0 now, meaning it's unsaved. */ 358 util_copy_framebuffer_state(&blitter->saved_fb_state, state); 359} 360 361static INLINE 362void util_blitter_save_viewport(struct blitter_context *blitter, 363 struct pipe_viewport_state *state) 364{ 365 blitter->saved_viewport = *state; 366} 367 368static INLINE 369void util_blitter_save_fragment_sampler_states( 370 struct blitter_context *blitter, 371 int num_sampler_states, 372 void **sampler_states) 373{ 374 assert(num_sampler_states <= Elements(blitter->saved_sampler_states)); 375 376 blitter->saved_num_sampler_states = num_sampler_states; 377 memcpy(blitter->saved_sampler_states, sampler_states, 378 num_sampler_states * sizeof(void *)); 379} 380 381static INLINE void 382util_blitter_save_fragment_sampler_views(struct blitter_context *blitter, 383 int num_views, 384 struct pipe_sampler_view **views) 385{ 386 unsigned i; 387 assert(num_views <= Elements(blitter->saved_sampler_views)); 388 389 blitter->saved_num_sampler_views = num_views; 390 for (i = 0; i < num_views; i++) 391 pipe_sampler_view_reference(&blitter->saved_sampler_views[i], 392 views[i]); 393} 394 395static INLINE void 396util_blitter_save_vertex_buffers(struct blitter_context *blitter, 397 int num_vertex_buffers, 398 struct pipe_vertex_buffer *vertex_buffers) 399{ 400 assert(num_vertex_buffers <= Elements(blitter->saved_vertex_buffers)); 401 402 blitter->saved_num_vertex_buffers = 0; 403 util_copy_vertex_buffers(blitter->saved_vertex_buffers, 404 (unsigned*)&blitter->saved_num_vertex_buffers, 405 vertex_buffers, 406 num_vertex_buffers); 407} 408 409static INLINE void 410util_blitter_save_so_targets(struct blitter_context *blitter, 411 int num_targets, 412 struct pipe_stream_output_target **targets) 413{ 414 unsigned i; 415 assert(num_targets <= Elements(blitter->saved_so_targets)); 416 417 blitter->saved_num_so_targets = num_targets; 418 for (i = 0; i < num_targets; i++) 419 pipe_so_target_reference(&blitter->saved_so_targets[i], 420 targets[i]); 421} 422 423static INLINE void 424util_blitter_save_sample_mask(struct blitter_context *blitter, 425 unsigned sample_mask) 426{ 427 blitter->is_sample_mask_saved = TRUE; 428 blitter->saved_sample_mask = sample_mask; 429} 430 431#ifdef __cplusplus 432} 433#endif 434 435#endif 436