u_blitter.c revision 2dca61bcb357d70be2bb4f2d28321dfc5fc10c69
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/** 28 * @file 29 * Blitter utility to facilitate acceleration of the clear, clear_render_target, 30 * clear_depth_stencil, and resource_copy_region functions. 31 * 32 * @author Marek Olšák 33 */ 34 35#include "pipe/p_context.h" 36#include "pipe/p_defines.h" 37#include "util/u_inlines.h" 38#include "pipe/p_shader_tokens.h" 39#include "pipe/p_state.h" 40 41#include "util/u_format.h" 42#include "util/u_memory.h" 43#include "util/u_math.h" 44#include "util/u_blitter.h" 45#include "util/u_draw_quad.h" 46#include "util/u_sampler.h" 47#include "util/u_simple_shaders.h" 48#include "util/u_surface.h" 49#include "util/u_texture.h" 50#include "util/u_upload_mgr.h" 51 52#define INVALID_PTR ((void*)~0) 53 54struct blitter_context_priv 55{ 56 struct blitter_context base; 57 58 struct u_upload_mgr *upload; 59 60 float vertices[4][2][4]; /**< {pos, color} or {pos, texcoord} */ 61 62 /* Templates for various state objects. */ 63 64 /* Constant state objects. */ 65 /* Vertex shaders. */ 66 void *vs; /**< Vertex shader which passes {pos, generic} to the output.*/ 67 void *vs_pos_only; /**< Vertex shader which passes pos to the output.*/ 68 69 /* Fragment shaders. */ 70 /* The shader at index i outputs color to color buffers 0,1,...,i-1. */ 71 void *fs_col[PIPE_MAX_COLOR_BUFS+1]; 72 void *fs_col_int[PIPE_MAX_COLOR_BUFS+1]; 73 74 /* FS which outputs a color from a texture, 75 where the index is PIPE_TEXTURE_* to be sampled. */ 76 void *fs_texfetch_col[PIPE_MAX_TEXTURE_TYPES]; 77 78 /* FS which outputs a depth from a texture, 79 where the index is PIPE_TEXTURE_* to be sampled. */ 80 void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES]; 81 void *fs_texfetch_depthstencil[PIPE_MAX_TEXTURE_TYPES]; 82 void *fs_texfetch_stencil[PIPE_MAX_TEXTURE_TYPES]; 83 84 /* Blend state. */ 85 void *blend_write_color; /**< blend state with writemask of RGBA */ 86 void *blend_keep_color; /**< blend state with writemask of 0 */ 87 88 /* Depth stencil alpha state. */ 89 void *dsa_write_depth_stencil; 90 void *dsa_write_depth_keep_stencil; 91 void *dsa_keep_depth_stencil; 92 void *dsa_keep_depth_write_stencil; 93 94 /* Vertex elements states. */ 95 void *velem_state; 96 void *velem_uint_state; 97 void *velem_sint_state; 98 void *velem_state_readbuf; 99 100 /* Sampler state. */ 101 void *sampler_state; 102 103 /* Rasterizer state. */ 104 void *rs_state; 105 void *rs_discard_state; 106 107 /* Viewport state. */ 108 struct pipe_viewport_state viewport; 109 110 /* Destination surface dimensions. */ 111 unsigned dst_width; 112 unsigned dst_height; 113 114 boolean has_geometry_shader; 115 boolean vertex_has_integers; 116 boolean has_stream_out; 117 boolean has_stencil_export; 118}; 119 120static void blitter_draw_rectangle(struct blitter_context *blitter, 121 unsigned x, unsigned y, 122 unsigned width, unsigned height, 123 float depth, 124 enum blitter_attrib_type type, 125 const union pipe_color_union *attrib); 126 127 128struct blitter_context *util_blitter_create(struct pipe_context *pipe) 129{ 130 struct blitter_context_priv *ctx; 131 struct pipe_blend_state blend; 132 struct pipe_depth_stencil_alpha_state dsa; 133 struct pipe_rasterizer_state rs_state; 134 struct pipe_sampler_state sampler_state; 135 struct pipe_vertex_element velem[2]; 136 unsigned i; 137 138 ctx = CALLOC_STRUCT(blitter_context_priv); 139 if (!ctx) 140 return NULL; 141 142 ctx->base.pipe = pipe; 143 ctx->base.draw_rectangle = blitter_draw_rectangle; 144 145 /* init state objects for them to be considered invalid */ 146 ctx->base.saved_blend_state = INVALID_PTR; 147 ctx->base.saved_dsa_state = INVALID_PTR; 148 ctx->base.saved_rs_state = INVALID_PTR; 149 ctx->base.saved_fs = INVALID_PTR; 150 ctx->base.saved_vs = INVALID_PTR; 151 ctx->base.saved_gs = INVALID_PTR; 152 ctx->base.saved_velem_state = INVALID_PTR; 153 ctx->base.saved_fb_state.nr_cbufs = ~0; 154 ctx->base.saved_num_sampler_views = ~0; 155 ctx->base.saved_num_sampler_states = ~0; 156 ctx->base.saved_num_vertex_buffers = ~0; 157 ctx->base.saved_num_so_targets = ~0; 158 159 ctx->has_geometry_shader = 160 pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY, 161 PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0; 162 ctx->vertex_has_integers = 163 pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_VERTEX, 164 PIPE_SHADER_CAP_INTEGERS); 165 ctx->has_stream_out = 166 pipe->screen->get_param(pipe->screen, 167 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0; 168 169 ctx->has_stencil_export = 170 pipe->screen->get_param(pipe->screen, 171 PIPE_CAP_SHADER_STENCIL_EXPORT); 172 173 /* blend state objects */ 174 memset(&blend, 0, sizeof(blend)); 175 ctx->blend_keep_color = pipe->create_blend_state(pipe, &blend); 176 177 blend.rt[0].colormask = PIPE_MASK_RGBA; 178 ctx->blend_write_color = pipe->create_blend_state(pipe, &blend); 179 180 /* depth stencil alpha state objects */ 181 memset(&dsa, 0, sizeof(dsa)); 182 ctx->dsa_keep_depth_stencil = 183 pipe->create_depth_stencil_alpha_state(pipe, &dsa); 184 185 dsa.depth.enabled = 1; 186 dsa.depth.writemask = 1; 187 dsa.depth.func = PIPE_FUNC_ALWAYS; 188 ctx->dsa_write_depth_keep_stencil = 189 pipe->create_depth_stencil_alpha_state(pipe, &dsa); 190 191 dsa.stencil[0].enabled = 1; 192 dsa.stencil[0].func = PIPE_FUNC_ALWAYS; 193 dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE; 194 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE; 195 dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE; 196 dsa.stencil[0].valuemask = 0xff; 197 dsa.stencil[0].writemask = 0xff; 198 ctx->dsa_write_depth_stencil = 199 pipe->create_depth_stencil_alpha_state(pipe, &dsa); 200 201 dsa.depth.enabled = 0; 202 dsa.depth.writemask = 0; 203 ctx->dsa_keep_depth_write_stencil = 204 pipe->create_depth_stencil_alpha_state(pipe, &dsa); 205 206 /* sampler state */ 207 memset(&sampler_state, 0, sizeof(sampler_state)); 208 sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 209 sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 210 sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 211 sampler_state.normalized_coords = 1; 212 ctx->sampler_state = pipe->create_sampler_state(pipe, &sampler_state); 213 214 /* rasterizer state */ 215 memset(&rs_state, 0, sizeof(rs_state)); 216 rs_state.cull_face = PIPE_FACE_NONE; 217 rs_state.gl_rasterization_rules = 1; 218 rs_state.flatshade = 1; 219 rs_state.depth_clip = 1; 220 ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state); 221 222 if (ctx->has_stream_out) { 223 rs_state.rasterizer_discard = 1; 224 ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state); 225 } 226 227 /* vertex elements states */ 228 memset(&velem[0], 0, sizeof(velem[0]) * 2); 229 for (i = 0; i < 2; i++) { 230 velem[i].src_offset = i * 4 * sizeof(float); 231 velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 232 } 233 ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]); 234 235 if (ctx->vertex_has_integers) { 236 memset(&velem[0], 0, sizeof(velem[0]) * 2); 237 velem[0].src_offset = 0; 238 velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 239 velem[1].src_offset = 4 * sizeof(float); 240 velem[1].src_format = PIPE_FORMAT_R32G32B32A32_SINT; 241 ctx->velem_sint_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]); 242 243 memset(&velem[0], 0, sizeof(velem[0]) * 2); 244 velem[0].src_offset = 0; 245 velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 246 velem[1].src_offset = 4 * sizeof(float); 247 velem[1].src_format = PIPE_FORMAT_R32G32B32A32_UINT; 248 ctx->velem_uint_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]); 249 } 250 251 if (ctx->has_stream_out) { 252 velem[0].src_format = PIPE_FORMAT_R32_UINT; 253 ctx->velem_state_readbuf = pipe->create_vertex_elements_state(pipe, 1, &velem[0]); 254 } 255 256 /* fragment shaders are created on-demand */ 257 258 /* vertex shaders */ 259 { 260 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION, 261 TGSI_SEMANTIC_GENERIC }; 262 const uint semantic_indices[] = { 0, 0 }; 263 ctx->vs = 264 util_make_vertex_passthrough_shader(pipe, 2, semantic_names, 265 semantic_indices); 266 } 267 if (ctx->has_stream_out) { 268 struct pipe_stream_output_info so; 269 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION }; 270 const uint semantic_indices[] = { 0 }; 271 272 memset(&so, 0, sizeof(so)); 273 so.num_outputs = 1; 274 so.output[0].num_components = 1; 275 so.stride[0] = 1; 276 277 ctx->vs_pos_only = 278 util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names, 279 semantic_indices, &so); 280 } 281 282 /* set invariant vertex coordinates */ 283 for (i = 0; i < 4; i++) 284 ctx->vertices[i][0][3] = 1; /*v.w*/ 285 286 ctx->upload = u_upload_create(pipe, 65536, 4, PIPE_BIND_VERTEX_BUFFER); 287 288 return &ctx->base; 289} 290 291void util_blitter_destroy(struct blitter_context *blitter) 292{ 293 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 294 struct pipe_context *pipe = blitter->pipe; 295 int i; 296 297 pipe->delete_blend_state(pipe, ctx->blend_write_color); 298 pipe->delete_blend_state(pipe, ctx->blend_keep_color); 299 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); 300 pipe->delete_depth_stencil_alpha_state(pipe, 301 ctx->dsa_write_depth_keep_stencil); 302 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil); 303 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil); 304 305 pipe->delete_rasterizer_state(pipe, ctx->rs_state); 306 if (ctx->rs_discard_state) 307 pipe->delete_rasterizer_state(pipe, ctx->rs_discard_state); 308 pipe->delete_vs_state(pipe, ctx->vs); 309 if (ctx->vs_pos_only) 310 pipe->delete_vs_state(pipe, ctx->vs_pos_only); 311 pipe->delete_vertex_elements_state(pipe, ctx->velem_state); 312 if (ctx->vertex_has_integers) { 313 pipe->delete_vertex_elements_state(pipe, ctx->velem_sint_state); 314 pipe->delete_vertex_elements_state(pipe, ctx->velem_uint_state); 315 } 316 if (ctx->velem_state_readbuf) 317 pipe->delete_vertex_elements_state(pipe, ctx->velem_state_readbuf); 318 319 for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) { 320 if (ctx->fs_texfetch_col[i]) 321 pipe->delete_fs_state(pipe, ctx->fs_texfetch_col[i]); 322 if (ctx->fs_texfetch_depth[i]) 323 pipe->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]); 324 if (ctx->fs_texfetch_depthstencil[i]) 325 pipe->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil[i]); 326 if (ctx->fs_texfetch_stencil[i]) 327 pipe->delete_fs_state(pipe, ctx->fs_texfetch_stencil[i]); 328 } 329 330 for (i = 0; i <= PIPE_MAX_COLOR_BUFS; i++) { 331 if (ctx->fs_col[i]) 332 pipe->delete_fs_state(pipe, ctx->fs_col[i]); 333 if (ctx->fs_col_int[i]) 334 pipe->delete_fs_state(pipe, ctx->fs_col_int[i]); 335 } 336 337 pipe->delete_sampler_state(pipe, ctx->sampler_state); 338 u_upload_destroy(ctx->upload); 339 FREE(ctx); 340} 341 342static void blitter_set_running_flag(struct blitter_context_priv *ctx) 343{ 344 if (ctx->base.running) { 345 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n", 346 __LINE__); 347 } 348 ctx->base.running = TRUE; 349} 350 351static void blitter_unset_running_flag(struct blitter_context_priv *ctx) 352{ 353 if (!ctx->base.running) { 354 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n", 355 __LINE__); 356 } 357 ctx->base.running = FALSE; 358} 359 360static void blitter_check_saved_vertex_states(struct blitter_context_priv *ctx) 361{ 362 assert(ctx->base.saved_num_vertex_buffers != ~0); 363 assert(ctx->base.saved_velem_state != INVALID_PTR); 364 assert(ctx->base.saved_vs != INVALID_PTR); 365 assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR); 366 assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0); 367 assert(ctx->base.saved_rs_state != INVALID_PTR); 368} 369 370static void blitter_restore_vertex_states(struct blitter_context_priv *ctx) 371{ 372 struct pipe_context *pipe = ctx->base.pipe; 373 unsigned i; 374 375 /* Vertex buffers. */ 376 pipe->set_vertex_buffers(pipe, 377 ctx->base.saved_num_vertex_buffers, 378 ctx->base.saved_vertex_buffers); 379 380 for (i = 0; i < ctx->base.saved_num_vertex_buffers; i++) { 381 if (ctx->base.saved_vertex_buffers[i].buffer) { 382 pipe_resource_reference(&ctx->base.saved_vertex_buffers[i].buffer, 383 NULL); 384 } 385 } 386 ctx->base.saved_num_vertex_buffers = ~0; 387 388 /* Vertex elements. */ 389 pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state); 390 ctx->base.saved_velem_state = INVALID_PTR; 391 392 /* Vertex shader. */ 393 pipe->bind_vs_state(pipe, ctx->base.saved_vs); 394 ctx->base.saved_vs = INVALID_PTR; 395 396 /* Geometry shader. */ 397 if (ctx->has_geometry_shader) { 398 pipe->bind_gs_state(pipe, ctx->base.saved_gs); 399 ctx->base.saved_gs = INVALID_PTR; 400 } 401 402 /* Stream outputs. */ 403 if (ctx->has_stream_out) { 404 pipe->set_stream_output_targets(pipe, 405 ctx->base.saved_num_so_targets, 406 ctx->base.saved_so_targets, ~0); 407 408 for (i = 0; i < ctx->base.saved_num_so_targets; i++) 409 pipe_so_target_reference(&ctx->base.saved_so_targets[i], NULL); 410 411 ctx->base.saved_num_so_targets = ~0; 412 } 413 414 /* Rasterizer. */ 415 pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state); 416 ctx->base.saved_rs_state = INVALID_PTR; 417} 418 419static void blitter_check_saved_fragment_states(struct blitter_context_priv *ctx) 420{ 421 assert(ctx->base.saved_fs != INVALID_PTR); 422 assert(ctx->base.saved_dsa_state != INVALID_PTR); 423 assert(ctx->base.saved_blend_state != INVALID_PTR); 424} 425 426static void blitter_restore_fragment_states(struct blitter_context_priv *ctx) 427{ 428 struct pipe_context *pipe = ctx->base.pipe; 429 430 /* Fragment shader. */ 431 pipe->bind_fs_state(pipe, ctx->base.saved_fs); 432 ctx->base.saved_fs = INVALID_PTR; 433 434 /* Depth, stencil, alpha. */ 435 pipe->bind_depth_stencil_alpha_state(pipe, ctx->base.saved_dsa_state); 436 ctx->base.saved_dsa_state = INVALID_PTR; 437 438 /* Blend state. */ 439 pipe->bind_blend_state(pipe, ctx->base.saved_blend_state); 440 ctx->base.saved_blend_state = INVALID_PTR; 441 442 /* Miscellaneous states. */ 443 /* XXX check whether these are saved and whether they need to be restored 444 * (depending on the operation) */ 445 pipe->set_stencil_ref(pipe, &ctx->base.saved_stencil_ref); 446 pipe->set_viewport_state(pipe, &ctx->base.saved_viewport); 447} 448 449static void blitter_check_saved_fb_state(struct blitter_context_priv *ctx) 450{ 451 assert(ctx->base.saved_fb_state.nr_cbufs != ~0); 452} 453 454static void blitter_restore_fb_state(struct blitter_context_priv *ctx) 455{ 456 struct pipe_context *pipe = ctx->base.pipe; 457 458 pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state); 459 util_unreference_framebuffer_state(&ctx->base.saved_fb_state); 460} 461 462static void blitter_check_saved_textures(struct blitter_context_priv *ctx) 463{ 464 assert(ctx->base.saved_num_sampler_states != ~0); 465 assert(ctx->base.saved_num_sampler_views != ~0); 466} 467 468static void blitter_restore_textures(struct blitter_context_priv *ctx) 469{ 470 struct pipe_context *pipe = ctx->base.pipe; 471 unsigned i; 472 473 /* Fragment sampler states. */ 474 pipe->bind_fragment_sampler_states(pipe, 475 ctx->base.saved_num_sampler_states, 476 ctx->base.saved_sampler_states); 477 ctx->base.saved_num_sampler_states = ~0; 478 479 /* Fragment sampler views. */ 480 pipe->set_fragment_sampler_views(pipe, 481 ctx->base.saved_num_sampler_views, 482 ctx->base.saved_sampler_views); 483 484 for (i = 0; i < ctx->base.saved_num_sampler_views; i++) 485 pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL); 486 487 ctx->base.saved_num_sampler_views = ~0; 488} 489 490static void blitter_set_rectangle(struct blitter_context_priv *ctx, 491 unsigned x1, unsigned y1, 492 unsigned x2, unsigned y2, 493 float depth) 494{ 495 int i; 496 497 /* set vertex positions */ 498 ctx->vertices[0][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v0.x*/ 499 ctx->vertices[0][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v0.y*/ 500 501 ctx->vertices[1][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v1.x*/ 502 ctx->vertices[1][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v1.y*/ 503 504 ctx->vertices[2][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v2.x*/ 505 ctx->vertices[2][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v2.y*/ 506 507 ctx->vertices[3][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v3.x*/ 508 ctx->vertices[3][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v3.y*/ 509 510 for (i = 0; i < 4; i++) 511 ctx->vertices[i][0][2] = depth; /*z*/ 512 513 /* viewport */ 514 ctx->viewport.scale[0] = 0.5f * ctx->dst_width; 515 ctx->viewport.scale[1] = 0.5f * ctx->dst_height; 516 ctx->viewport.scale[2] = 1.0f; 517 ctx->viewport.scale[3] = 1.0f; 518 ctx->viewport.translate[0] = 0.5f * ctx->dst_width; 519 ctx->viewport.translate[1] = 0.5f * ctx->dst_height; 520 ctx->viewport.translate[2] = 0.0f; 521 ctx->viewport.translate[3] = 0.0f; 522 ctx->base.pipe->set_viewport_state(ctx->base.pipe, &ctx->viewport); 523} 524 525static void blitter_set_clear_color(struct blitter_context_priv *ctx, 526 const union pipe_color_union *color) 527{ 528 int i; 529 530 if (color) { 531 for (i = 0; i < 4; i++) { 532 uint32_t *uiverts = (uint32_t *)ctx->vertices[i][1]; 533 uiverts[0] = color->ui[0]; 534 uiverts[1] = color->ui[1]; 535 uiverts[2] = color->ui[2]; 536 uiverts[3] = color->ui[3]; 537 } 538 } else { 539 for (i = 0; i < 4; i++) { 540 ctx->vertices[i][1][0] = 0; 541 ctx->vertices[i][1][1] = 0; 542 ctx->vertices[i][1][2] = 0; 543 ctx->vertices[i][1][3] = 0; 544 } 545 } 546} 547 548static void get_texcoords(struct pipe_sampler_view *src, 549 unsigned src_width0, unsigned src_height0, 550 unsigned x1, unsigned y1, 551 unsigned x2, unsigned y2, 552 float out[4]) 553{ 554 struct pipe_resource *tex = src->texture; 555 unsigned level = src->u.tex.first_level; 556 boolean normalized = tex->target != PIPE_TEXTURE_RECT; 557 558 if (normalized) { 559 out[0] = x1 / (float)u_minify(src_width0, level); 560 out[1] = y1 / (float)u_minify(src_height0, level); 561 out[2] = x2 / (float)u_minify(src_width0, level); 562 out[3] = y2 / (float)u_minify(src_height0, level); 563 } else { 564 out[0] = x1; 565 out[1] = y1; 566 out[2] = x2; 567 out[3] = y2; 568 } 569} 570 571static void set_texcoords_in_vertices(const float coord[4], 572 float *out, unsigned stride) 573{ 574 out[0] = coord[0]; /*t0.s*/ 575 out[1] = coord[1]; /*t0.t*/ 576 out += stride; 577 out[0] = coord[2]; /*t1.s*/ 578 out[1] = coord[1]; /*t1.t*/ 579 out += stride; 580 out[0] = coord[2]; /*t2.s*/ 581 out[1] = coord[3]; /*t2.t*/ 582 out += stride; 583 out[0] = coord[0]; /*t3.s*/ 584 out[1] = coord[3]; /*t3.t*/ 585} 586 587static void blitter_set_texcoords(struct blitter_context_priv *ctx, 588 struct pipe_sampler_view *src, 589 unsigned src_width0, unsigned src_height0, 590 unsigned layer, 591 unsigned x1, unsigned y1, 592 unsigned x2, unsigned y2) 593{ 594 unsigned i; 595 float coord[4]; 596 float face_coord[4][2]; 597 598 get_texcoords(src, src_width0, src_height0, x1, y1, x2, y2, coord); 599 600 if (src->texture->target == PIPE_TEXTURE_CUBE) { 601 set_texcoords_in_vertices(coord, &face_coord[0][0], 2); 602 util_map_texcoords2d_onto_cubemap(layer, 603 /* pointer, stride in floats */ 604 &face_coord[0][0], 2, 605 &ctx->vertices[0][1][0], 8); 606 } else { 607 set_texcoords_in_vertices(coord, &ctx->vertices[0][1][0], 8); 608 } 609 610 /* Set the layer. */ 611 switch (src->texture->target) { 612 case PIPE_TEXTURE_3D: 613 { 614 float r = layer / (float)u_minify(src->texture->depth0, 615 src->u.tex.first_level); 616 for (i = 0; i < 4; i++) 617 ctx->vertices[i][1][2] = r; /*r*/ 618 } 619 break; 620 621 case PIPE_TEXTURE_1D_ARRAY: 622 for (i = 0; i < 4; i++) 623 ctx->vertices[i][1][1] = layer; /*t*/ 624 break; 625 626 case PIPE_TEXTURE_2D_ARRAY: 627 for (i = 0; i < 4; i++) 628 ctx->vertices[i][1][2] = layer; /*r*/ 629 break; 630 631 default:; 632 } 633} 634 635static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx, 636 unsigned width, unsigned height) 637{ 638 ctx->dst_width = width; 639 ctx->dst_height = height; 640} 641 642static INLINE 643void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs, 644 boolean int_format) 645{ 646 struct pipe_context *pipe = ctx->base.pipe; 647 648 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS); 649 650 if (int_format) { 651 if (!ctx->fs_col_int[num_cbufs]) 652 ctx->fs_col_int[num_cbufs] = 653 util_make_fragment_cloneinput_shader(pipe, num_cbufs, 654 TGSI_SEMANTIC_GENERIC, 655 TGSI_INTERPOLATE_CONSTANT); 656 return ctx->fs_col_int[num_cbufs]; 657 } else { 658 if (!ctx->fs_col[num_cbufs]) 659 ctx->fs_col[num_cbufs] = 660 util_make_fragment_cloneinput_shader(pipe, num_cbufs, 661 TGSI_SEMANTIC_GENERIC, 662 TGSI_INTERPOLATE_LINEAR); 663 return ctx->fs_col[num_cbufs]; 664 } 665} 666 667static INLINE 668void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx, 669 unsigned tex_target) 670{ 671 struct pipe_context *pipe = ctx->base.pipe; 672 673 assert(tex_target < PIPE_MAX_TEXTURE_TYPES); 674 675 /* Create the fragment shader on-demand. */ 676 if (!ctx->fs_texfetch_col[tex_target]) { 677 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex_target); 678 679 ctx->fs_texfetch_col[tex_target] = 680 util_make_fragment_tex_shader(pipe, tgsi_tex, TGSI_INTERPOLATE_LINEAR); 681 } 682 683 return ctx->fs_texfetch_col[tex_target]; 684} 685 686static INLINE 687void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx, 688 unsigned tex_target) 689{ 690 struct pipe_context *pipe = ctx->base.pipe; 691 692 assert(tex_target < PIPE_MAX_TEXTURE_TYPES); 693 694 /* Create the fragment shader on-demand. */ 695 if (!ctx->fs_texfetch_depth[tex_target]) { 696 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex_target); 697 698 ctx->fs_texfetch_depth[tex_target] = 699 util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex, 700 TGSI_INTERPOLATE_LINEAR); 701 } 702 703 return ctx->fs_texfetch_depth[tex_target]; 704} 705 706static INLINE 707void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx, 708 unsigned tex_target) 709{ 710 struct pipe_context *pipe = ctx->base.pipe; 711 712 assert(tex_target < PIPE_MAX_TEXTURE_TYPES); 713 714 /* Create the fragment shader on-demand. */ 715 if (!ctx->fs_texfetch_depthstencil[tex_target]) { 716 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex_target); 717 718 ctx->fs_texfetch_depthstencil[tex_target] = 719 util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex, 720 TGSI_INTERPOLATE_LINEAR); 721 } 722 723 return ctx->fs_texfetch_depthstencil[tex_target]; 724} 725 726static INLINE 727void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx, 728 unsigned tex_target) 729{ 730 struct pipe_context *pipe = ctx->base.pipe; 731 732 assert(tex_target < PIPE_MAX_TEXTURE_TYPES); 733 734 /* Create the fragment shader on-demand. */ 735 if (!ctx->fs_texfetch_stencil[tex_target]) { 736 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex_target); 737 738 ctx->fs_texfetch_stencil[tex_target] = 739 util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex, 740 TGSI_INTERPOLATE_LINEAR); 741 } 742 743 return ctx->fs_texfetch_stencil[tex_target]; 744} 745 746static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx) 747{ 748 struct pipe_context *pipe = ctx->base.pipe; 749 750 pipe->bind_rasterizer_state(pipe, ctx->rs_state); 751 pipe->bind_vs_state(pipe, ctx->vs); 752 if (ctx->has_geometry_shader) 753 pipe->bind_gs_state(pipe, NULL); 754 if (ctx->has_stream_out) 755 pipe->set_stream_output_targets(pipe, 0, NULL, 0); 756} 757 758static void blitter_draw(struct blitter_context_priv *ctx, 759 unsigned x1, unsigned y1, 760 unsigned x2, unsigned y2, 761 float depth) 762{ 763 struct pipe_resource *buf = NULL; 764 unsigned offset = 0; 765 766 blitter_set_rectangle(ctx, x1, y1, x2, y2, depth); 767 768 u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), ctx->vertices, 769 &offset, &buf); 770 u_upload_unmap(ctx->upload); 771 util_draw_vertex_buffer(ctx->base.pipe, NULL, buf, offset, 772 PIPE_PRIM_TRIANGLE_FAN, 4, 2); 773 pipe_resource_reference(&buf, NULL); 774} 775 776static void blitter_draw_rectangle(struct blitter_context *blitter, 777 unsigned x1, unsigned y1, 778 unsigned x2, unsigned y2, 779 float depth, 780 enum blitter_attrib_type type, 781 const union pipe_color_union *attrib) 782{ 783 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 784 785 switch (type) { 786 case UTIL_BLITTER_ATTRIB_COLOR: 787 blitter_set_clear_color(ctx, attrib); 788 break; 789 790 case UTIL_BLITTER_ATTRIB_TEXCOORD: 791 set_texcoords_in_vertices(attrib->f, &ctx->vertices[0][1][0], 8); 792 break; 793 794 default:; 795 } 796 797 blitter_draw(ctx, x1, y1, x2, y2, depth); 798} 799 800static void util_blitter_clear_custom(struct blitter_context *blitter, 801 unsigned width, unsigned height, 802 unsigned num_cbufs, 803 unsigned clear_buffers, 804 enum pipe_format cbuf_format, 805 const union pipe_color_union *color, 806 double depth, unsigned stencil, 807 void *custom_blend, void *custom_dsa) 808{ 809 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 810 struct pipe_context *pipe = ctx->base.pipe; 811 struct pipe_stencil_ref sr = { { 0 } }; 812 boolean int_format = util_format_is_pure_integer(cbuf_format); 813 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS); 814 815 blitter_set_running_flag(ctx); 816 blitter_check_saved_vertex_states(ctx); 817 blitter_check_saved_fragment_states(ctx); 818 819 /* bind states */ 820 if (custom_blend) { 821 pipe->bind_blend_state(pipe, custom_blend); 822 } else if (clear_buffers & PIPE_CLEAR_COLOR) { 823 pipe->bind_blend_state(pipe, ctx->blend_write_color); 824 } else { 825 pipe->bind_blend_state(pipe, ctx->blend_keep_color); 826 } 827 828 if (custom_dsa) { 829 pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa); 830 } else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) { 831 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil); 832 } else if (clear_buffers & PIPE_CLEAR_DEPTH) { 833 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil); 834 } else if (clear_buffers & PIPE_CLEAR_STENCIL) { 835 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil); 836 } else { 837 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); 838 } 839 840 sr.ref_value[0] = stencil & 0xff; 841 pipe->set_stencil_ref(pipe, &sr); 842 843 if (util_format_is_pure_sint(cbuf_format)) { 844 pipe->bind_vertex_elements_state(pipe, ctx->velem_sint_state); 845 } else if (util_format_is_pure_uint(cbuf_format)) { 846 pipe->bind_vertex_elements_state(pipe, ctx->velem_uint_state); 847 } else { 848 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 849 } 850 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs, int_format)); 851 852 blitter_set_common_draw_rect_state(ctx); 853 blitter_set_dst_dimensions(ctx, width, height); 854 blitter->draw_rectangle(blitter, 0, 0, width, height, depth, 855 UTIL_BLITTER_ATTRIB_COLOR, color); 856 857 blitter_restore_vertex_states(ctx); 858 blitter_restore_fragment_states(ctx); 859 blitter_unset_running_flag(ctx); 860} 861 862void util_blitter_clear(struct blitter_context *blitter, 863 unsigned width, unsigned height, 864 unsigned num_cbufs, 865 unsigned clear_buffers, 866 enum pipe_format cbuf_format, 867 const union pipe_color_union *color, 868 double depth, unsigned stencil) 869{ 870 util_blitter_clear_custom(blitter, width, height, num_cbufs, 871 clear_buffers, cbuf_format, color, depth, stencil, 872 NULL, NULL); 873} 874 875void util_blitter_clear_depth_custom(struct blitter_context *blitter, 876 unsigned width, unsigned height, 877 double depth, void *custom_dsa) 878{ 879 static const union pipe_color_union color; 880 util_blitter_clear_custom(blitter, width, height, 0, 881 0, PIPE_FORMAT_NONE, &color, depth, 0, NULL, custom_dsa); 882} 883 884static 885boolean is_overlap(unsigned sx1, unsigned sx2, unsigned sy1, unsigned sy2, 886 unsigned dx1, unsigned dx2, unsigned dy1, unsigned dy2) 887{ 888 return sx1 < dx2 && sx2 > dx1 && sy1 < dy2 && sy2 > dy1; 889} 890 891void util_blitter_default_dst_texture(struct pipe_surface *dst_templ, 892 struct pipe_resource *dst, 893 unsigned dstlevel, 894 unsigned dstz, 895 const struct pipe_box *srcbox) 896{ 897 memset(dst_templ, 0, sizeof(*dst_templ)); 898 dst_templ->format = dst->format; 899 if (util_format_is_depth_or_stencil(dst->format)) { 900 dst_templ->usage = PIPE_BIND_DEPTH_STENCIL; 901 } else { 902 dst_templ->usage = PIPE_BIND_RENDER_TARGET; 903 } 904 dst_templ->format = util_format_linear(dst->format); 905 dst_templ->u.tex.level = dstlevel; 906 dst_templ->u.tex.first_layer = dstz; 907 dst_templ->u.tex.last_layer = dstz + srcbox->depth - 1; 908} 909 910void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ, 911 struct pipe_resource *src, 912 unsigned srclevel) 913{ 914 memset(src_templ, 0, sizeof(*src_templ)); 915 src_templ->format = util_format_linear(src->format); 916 src_templ->u.tex.first_level = srclevel; 917 src_templ->u.tex.last_level = srclevel; 918 src_templ->u.tex.first_layer = 0; 919 src_templ->u.tex.last_layer = 920 src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1 921 : src->array_size - 1; 922 src_templ->swizzle_r = PIPE_SWIZZLE_RED; 923 src_templ->swizzle_g = PIPE_SWIZZLE_GREEN; 924 src_templ->swizzle_b = PIPE_SWIZZLE_BLUE; 925 src_templ->swizzle_a = PIPE_SWIZZLE_ALPHA; 926} 927 928void util_blitter_copy_texture(struct blitter_context *blitter, 929 struct pipe_resource *dst, 930 unsigned dstlevel, 931 unsigned dstx, unsigned dsty, unsigned dstz, 932 struct pipe_resource *src, 933 unsigned srclevel, 934 const struct pipe_box *srcbox, 935 boolean ignore_stencil) 936{ 937 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 938 struct pipe_context *pipe = ctx->base.pipe; 939 struct pipe_screen *screen = pipe->screen; 940 struct pipe_surface *dst_view, dst_templ; 941 struct pipe_sampler_view src_templ, *src_view; 942 unsigned bind; 943 boolean is_stencil, is_depth; 944 const struct util_format_description *src_desc = 945 util_format_description(src->format); 946 947 /* Give up if textures are not set. */ 948 assert(dst && src); 949 if (!dst || !src) 950 return; 951 952 assert(src->target < PIPE_MAX_TEXTURE_TYPES); 953 954 /* Is this a ZS format? */ 955 is_depth = util_format_has_depth(src_desc); 956 is_stencil = util_format_has_stencil(src_desc); 957 958 if (is_depth || is_stencil) 959 bind = PIPE_BIND_DEPTH_STENCIL; 960 else 961 bind = PIPE_BIND_RENDER_TARGET; 962 963 /* Check if we can sample from and render to the surfaces. */ 964 /* (assuming copying a stencil buffer is not possible) */ 965 if ((!ignore_stencil && is_stencil && !ctx->has_stencil_export) || 966 !screen->is_format_supported(screen, dst->format, dst->target, 967 dst->nr_samples, bind) || 968 !screen->is_format_supported(screen, src->format, src->target, 969 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) { 970 blitter_set_running_flag(ctx); 971 util_resource_copy_region(pipe, dst, dstlevel, dstx, dsty, dstz, 972 src, srclevel, srcbox); 973 blitter_unset_running_flag(ctx); 974 return; 975 } 976 977 /* Initialize the surface. */ 978 util_blitter_default_dst_texture(&dst_templ, dst, dstlevel, dstz, srcbox); 979 dst_view = pipe->create_surface(pipe, dst, &dst_templ); 980 981 /* Initialize the sampler view. */ 982 util_blitter_default_src_texture(&src_templ, src, srclevel); 983 src_view = pipe->create_sampler_view(pipe, src, &src_templ); 984 985 /* Copy. */ 986 util_blitter_copy_texture_view(blitter, dst_view, dstx, dsty, src_view, 987 srcbox, src->width0, src->height0); 988 989 pipe_surface_reference(&dst_view, NULL); 990 pipe_sampler_view_reference(&src_view, NULL); 991} 992 993void util_blitter_copy_texture_view(struct blitter_context *blitter, 994 struct pipe_surface *dst, 995 unsigned dstx, unsigned dsty, 996 struct pipe_sampler_view *src, 997 const struct pipe_box *srcbox, 998 unsigned src_width0, unsigned src_height0) 999{ 1000 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1001 struct pipe_context *pipe = ctx->base.pipe; 1002 struct pipe_framebuffer_state fb_state; 1003 enum pipe_texture_target src_target = src->texture->target; 1004 unsigned width = srcbox->width; 1005 unsigned height = srcbox->height; 1006 boolean is_stencil, is_depth; 1007 const struct util_format_description *src_desc = 1008 util_format_description(src->format); 1009 1010 is_depth = util_format_has_depth(src_desc); 1011 is_stencil = util_format_has_stencil(src_desc); 1012 1013 /* If you want a fallback for stencil copies, 1014 * use util_blitter_copy_texture. */ 1015 if (is_stencil && !ctx->has_stencil_export) { 1016 is_stencil = FALSE; 1017 1018 if (!is_depth) 1019 return; 1020 } 1021 1022 /* Sanity checks. */ 1023 if (dst->texture == src->texture && 1024 dst->u.tex.level == src->u.tex.first_level) { 1025 assert(!is_overlap(srcbox->x, srcbox->x + width, srcbox->y, srcbox->y + height, 1026 dstx, dstx + width, dsty, dsty + height)); 1027 } 1028 /* XXX should handle 3d regions */ 1029 assert(srcbox->depth == 1); 1030 1031 /* Check whether the states are properly saved. */ 1032 blitter_set_running_flag(ctx); 1033 blitter_check_saved_vertex_states(ctx); 1034 blitter_check_saved_fragment_states(ctx); 1035 blitter_check_saved_textures(ctx); 1036 blitter_check_saved_fb_state(ctx); 1037 1038 /* Initialize framebuffer state. */ 1039 fb_state.width = dst->width; 1040 fb_state.height = dst->height; 1041 1042 if (is_depth || is_stencil) { 1043 pipe->bind_blend_state(pipe, ctx->blend_keep_color); 1044 1045 if (is_depth && is_stencil) { 1046 pipe->bind_depth_stencil_alpha_state(pipe, 1047 ctx->dsa_write_depth_stencil); 1048 pipe->bind_fs_state(pipe, 1049 blitter_get_fs_texfetch_depthstencil(ctx, src_target)); 1050 } else if (is_depth) { 1051 pipe->bind_depth_stencil_alpha_state(pipe, 1052 ctx->dsa_write_depth_keep_stencil); 1053 pipe->bind_fs_state(pipe, 1054 blitter_get_fs_texfetch_depth(ctx, src_target)); 1055 } else { /* is_stencil */ 1056 pipe->bind_depth_stencil_alpha_state(pipe, 1057 ctx->dsa_keep_depth_write_stencil); 1058 pipe->bind_fs_state(pipe, 1059 blitter_get_fs_texfetch_stencil(ctx, src_target)); 1060 } 1061 1062 fb_state.nr_cbufs = 0; 1063 fb_state.zsbuf = dst; 1064 } else { 1065 pipe->bind_blend_state(pipe, ctx->blend_write_color); 1066 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); 1067 pipe->bind_fs_state(pipe, 1068 blitter_get_fs_texfetch_col(ctx, src_target)); 1069 1070 fb_state.nr_cbufs = 1; 1071 fb_state.cbufs[0] = dst; 1072 fb_state.zsbuf = 0; 1073 } 1074 1075 if (is_depth && is_stencil) { 1076 /* Setup two samplers, one for depth and the other one for stencil. */ 1077 struct pipe_sampler_view templ; 1078 struct pipe_sampler_view *views[2]; 1079 void *samplers[2] = {ctx->sampler_state, ctx->sampler_state}; 1080 1081 templ = *src; 1082 templ.format = util_format_stencil_only(templ.format); 1083 assert(templ.format != PIPE_FORMAT_NONE); 1084 1085 views[0] = src; 1086 views[1] = pipe->create_sampler_view(pipe, src->texture, &templ); 1087 1088 pipe->set_fragment_sampler_views(pipe, 2, views); 1089 pipe->bind_fragment_sampler_states(pipe, 2, samplers); 1090 1091 pipe_sampler_view_reference(&views[1], NULL); 1092 } else { 1093 pipe->set_fragment_sampler_views(pipe, 1, &src); 1094 pipe->bind_fragment_sampler_states(pipe, 1, &ctx->sampler_state); 1095 } 1096 1097 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 1098 pipe->set_framebuffer_state(pipe, &fb_state); 1099 1100 blitter_set_common_draw_rect_state(ctx); 1101 blitter_set_dst_dimensions(ctx, dst->width, dst->height); 1102 1103 switch (src_target) { 1104 /* Draw the quad with the draw_rectangle callback. */ 1105 case PIPE_TEXTURE_1D: 1106 case PIPE_TEXTURE_2D: 1107 case PIPE_TEXTURE_RECT: 1108 { 1109 /* Set texture coordinates. - use a pipe color union 1110 * for interface purposes. 1111 * XXX pipe_color_union is a wrong name since we use that to set 1112 * texture coordinates too. 1113 */ 1114 union pipe_color_union coord; 1115 get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y, 1116 srcbox->x+width, srcbox->y+height, coord.f); 1117 1118 /* Draw. */ 1119 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0, 1120 UTIL_BLITTER_ATTRIB_TEXCOORD, &coord); 1121 } 1122 break; 1123 1124 /* Draw the quad with the generic codepath. */ 1125 default: 1126 /* Set texture coordinates. */ 1127 switch (src_target) { 1128 case PIPE_TEXTURE_1D_ARRAY: 1129 case PIPE_TEXTURE_2D_ARRAY: 1130 case PIPE_TEXTURE_3D: 1131 case PIPE_TEXTURE_CUBE: 1132 blitter_set_texcoords(ctx, src, src_width0, src_height0, srcbox->z, 1133 srcbox->y, srcbox->x, 1134 srcbox->x + width, srcbox->y + height); 1135 break; 1136 1137 default: 1138 assert(0); 1139 } 1140 1141 blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, 0); 1142 break; 1143 } 1144 1145 blitter_restore_vertex_states(ctx); 1146 blitter_restore_fragment_states(ctx); 1147 blitter_restore_textures(ctx); 1148 blitter_restore_fb_state(ctx); 1149 blitter_unset_running_flag(ctx); 1150} 1151 1152/* Clear a region of a color surface to a constant value. */ 1153void util_blitter_clear_render_target(struct blitter_context *blitter, 1154 struct pipe_surface *dstsurf, 1155 const union pipe_color_union *color, 1156 unsigned dstx, unsigned dsty, 1157 unsigned width, unsigned height) 1158{ 1159 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1160 struct pipe_context *pipe = ctx->base.pipe; 1161 struct pipe_framebuffer_state fb_state; 1162 1163 assert(dstsurf->texture); 1164 if (!dstsurf->texture) 1165 return; 1166 1167 /* check the saved state */ 1168 blitter_set_running_flag(ctx); 1169 blitter_check_saved_vertex_states(ctx); 1170 blitter_check_saved_fragment_states(ctx); 1171 blitter_check_saved_fb_state(ctx); 1172 1173 /* bind states */ 1174 pipe->bind_blend_state(pipe, ctx->blend_write_color); 1175 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); 1176 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE)); 1177 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 1178 1179 /* set a framebuffer state */ 1180 fb_state.width = dstsurf->width; 1181 fb_state.height = dstsurf->height; 1182 fb_state.nr_cbufs = 1; 1183 fb_state.cbufs[0] = dstsurf; 1184 fb_state.zsbuf = 0; 1185 pipe->set_framebuffer_state(pipe, &fb_state); 1186 1187 blitter_set_common_draw_rect_state(ctx); 1188 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height); 1189 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0, 1190 UTIL_BLITTER_ATTRIB_COLOR, color); 1191 1192 blitter_restore_vertex_states(ctx); 1193 blitter_restore_fragment_states(ctx); 1194 blitter_restore_fb_state(ctx); 1195 blitter_unset_running_flag(ctx); 1196} 1197 1198/* Clear a region of a depth stencil surface. */ 1199void util_blitter_clear_depth_stencil(struct blitter_context *blitter, 1200 struct pipe_surface *dstsurf, 1201 unsigned clear_flags, 1202 double depth, 1203 unsigned stencil, 1204 unsigned dstx, unsigned dsty, 1205 unsigned width, unsigned height) 1206{ 1207 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1208 struct pipe_context *pipe = ctx->base.pipe; 1209 struct pipe_framebuffer_state fb_state; 1210 struct pipe_stencil_ref sr = { { 0 } }; 1211 1212 assert(dstsurf->texture); 1213 if (!dstsurf->texture) 1214 return; 1215 1216 /* check the saved state */ 1217 blitter_set_running_flag(ctx); 1218 blitter_check_saved_vertex_states(ctx); 1219 blitter_check_saved_fragment_states(ctx); 1220 blitter_check_saved_fb_state(ctx); 1221 1222 /* bind states */ 1223 pipe->bind_blend_state(pipe, ctx->blend_keep_color); 1224 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) { 1225 sr.ref_value[0] = stencil & 0xff; 1226 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil); 1227 pipe->set_stencil_ref(pipe, &sr); 1228 } 1229 else if (clear_flags & PIPE_CLEAR_DEPTH) { 1230 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil); 1231 } 1232 else if (clear_flags & PIPE_CLEAR_STENCIL) { 1233 sr.ref_value[0] = stencil & 0xff; 1234 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil); 1235 pipe->set_stencil_ref(pipe, &sr); 1236 } 1237 else 1238 /* hmm that should be illegal probably, or make it a no-op somewhere */ 1239 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); 1240 1241 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE)); 1242 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 1243 1244 /* set a framebuffer state */ 1245 fb_state.width = dstsurf->width; 1246 fb_state.height = dstsurf->height; 1247 fb_state.nr_cbufs = 0; 1248 fb_state.cbufs[0] = 0; 1249 fb_state.zsbuf = dstsurf; 1250 pipe->set_framebuffer_state(pipe, &fb_state); 1251 1252 blitter_set_common_draw_rect_state(ctx); 1253 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height); 1254 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, depth, 1255 UTIL_BLITTER_ATTRIB_NONE, NULL); 1256 1257 blitter_restore_vertex_states(ctx); 1258 blitter_restore_fragment_states(ctx); 1259 blitter_restore_fb_state(ctx); 1260 blitter_unset_running_flag(ctx); 1261} 1262 1263/* draw a rectangle across a region using a custom dsa stage - for r600g */ 1264void util_blitter_custom_depth_stencil(struct blitter_context *blitter, 1265 struct pipe_surface *zsurf, 1266 struct pipe_surface *cbsurf, 1267 void *dsa_stage, float depth) 1268{ 1269 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1270 struct pipe_context *pipe = ctx->base.pipe; 1271 struct pipe_framebuffer_state fb_state; 1272 1273 assert(zsurf->texture); 1274 if (!zsurf->texture) 1275 return; 1276 1277 /* check the saved state */ 1278 blitter_set_running_flag(ctx); 1279 blitter_check_saved_vertex_states(ctx); 1280 blitter_check_saved_fragment_states(ctx); 1281 blitter_check_saved_fb_state(ctx); 1282 1283 /* bind states */ 1284 pipe->bind_blend_state(pipe, ctx->blend_write_color); 1285 pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage); 1286 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE)); 1287 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 1288 1289 /* set a framebuffer state */ 1290 fb_state.width = zsurf->width; 1291 fb_state.height = zsurf->height; 1292 fb_state.nr_cbufs = 1; 1293 if (cbsurf) { 1294 fb_state.cbufs[0] = cbsurf; 1295 fb_state.nr_cbufs = 1; 1296 } else { 1297 fb_state.cbufs[0] = NULL; 1298 fb_state.nr_cbufs = 0; 1299 } 1300 fb_state.zsbuf = zsurf; 1301 pipe->set_framebuffer_state(pipe, &fb_state); 1302 1303 blitter_set_common_draw_rect_state(ctx); 1304 blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height); 1305 blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth, 1306 UTIL_BLITTER_ATTRIB_NONE, NULL); 1307 1308 blitter_restore_vertex_states(ctx); 1309 blitter_restore_fragment_states(ctx); 1310 blitter_restore_fb_state(ctx); 1311 blitter_unset_running_flag(ctx); 1312} 1313 1314void util_blitter_copy_buffer(struct blitter_context *blitter, 1315 struct pipe_resource *dst, 1316 unsigned dstx, 1317 struct pipe_resource *src, 1318 unsigned srcx, 1319 unsigned size) 1320{ 1321 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1322 struct pipe_context *pipe = ctx->base.pipe; 1323 struct pipe_vertex_buffer vb; 1324 struct pipe_stream_output_target *so_target; 1325 1326 if (srcx >= src->width0 || 1327 dstx >= dst->width0) { 1328 return; 1329 } 1330 if (srcx + size > src->width0) { 1331 size = src->width0 - srcx; 1332 } 1333 if (dstx + size > dst->width0) { 1334 size = dst->width0 - dstx; 1335 } 1336 1337 /* Drivers not capable of Stream Out should not call this function 1338 * in the first place. */ 1339 assert(ctx->has_stream_out); 1340 1341 /* Some alignment is required. */ 1342 if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 || 1343 !ctx->has_stream_out) { 1344 struct pipe_box box; 1345 u_box_1d(srcx, size, &box); 1346 util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box); 1347 return; 1348 } 1349 1350 blitter_set_running_flag(ctx); 1351 blitter_check_saved_vertex_states(ctx); 1352 1353 vb.buffer = src; 1354 vb.buffer_offset = srcx; 1355 vb.stride = 4; 1356 1357 pipe->set_vertex_buffers(pipe, 1, &vb); 1358 pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf); 1359 pipe->bind_vs_state(pipe, ctx->vs_pos_only); 1360 if (ctx->has_geometry_shader) 1361 pipe->bind_gs_state(pipe, NULL); 1362 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state); 1363 1364 so_target = pipe->create_stream_output_target(pipe, dst, dstx, size); 1365 pipe->set_stream_output_targets(pipe, 1, &so_target, 0); 1366 1367 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4); 1368 1369 blitter_restore_vertex_states(ctx); 1370 blitter_unset_running_flag(ctx); 1371 pipe_so_target_reference(&so_target, NULL); 1372} 1373