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