u_blitter.c revision 496399d8e918463650d88c3a9ca829357dc71f97
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 if (ctx->has_stream_out) 819 pipe->set_stream_output_targets(pipe, 0, NULL, 0); 820 821 blitter_set_dst_dimensions(ctx, width, height); 822 blitter->draw_rectangle(blitter, 0, 0, width, height, depth, 823 UTIL_BLITTER_ATTRIB_COLOR, color); 824 825 blitter_restore_vertex_states(ctx); 826 blitter_restore_fragment_states(ctx); 827 blitter_unset_running_flag(ctx); 828} 829 830void util_blitter_clear(struct blitter_context *blitter, 831 unsigned width, unsigned height, 832 unsigned num_cbufs, 833 unsigned clear_buffers, 834 enum pipe_format cbuf_format, 835 const union pipe_color_union *color, 836 double depth, unsigned stencil) 837{ 838 util_blitter_clear_custom(blitter, width, height, num_cbufs, 839 clear_buffers, cbuf_format, color, depth, stencil, 840 NULL, NULL); 841} 842 843void util_blitter_clear_depth_custom(struct blitter_context *blitter, 844 unsigned width, unsigned height, 845 double depth, void *custom_dsa) 846{ 847 static const union pipe_color_union color; 848 util_blitter_clear_custom(blitter, width, height, 0, 849 0, PIPE_FORMAT_NONE, &color, depth, 0, NULL, custom_dsa); 850} 851 852static 853boolean is_overlap(unsigned sx1, unsigned sx2, unsigned sy1, unsigned sy2, 854 unsigned dx1, unsigned dx2, unsigned dy1, unsigned dy2) 855{ 856 return sx1 < dx2 && sx2 > dx1 && sy1 < dy2 && sy2 > dy1; 857} 858 859void util_blitter_default_dst_texture(struct pipe_surface *dst_templ, 860 struct pipe_resource *dst, 861 unsigned dstlevel, 862 unsigned dstz, 863 const struct pipe_box *srcbox) 864{ 865 memset(dst_templ, 0, sizeof(*dst_templ)); 866 dst_templ->format = dst->format; 867 if (util_format_is_depth_or_stencil(dst->format)) { 868 dst_templ->usage = PIPE_BIND_DEPTH_STENCIL; 869 } else { 870 dst_templ->usage = PIPE_BIND_RENDER_TARGET; 871 } 872 dst_templ->format = util_format_linear(dst->format); 873 dst_templ->u.tex.level = dstlevel; 874 dst_templ->u.tex.first_layer = dstz; 875 dst_templ->u.tex.last_layer = dstz + srcbox->depth - 1; 876} 877 878void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ, 879 struct pipe_resource *src, 880 unsigned srclevel) 881{ 882 memset(src_templ, 0, sizeof(*src_templ)); 883 src_templ->format = util_format_linear(src->format); 884 src_templ->u.tex.first_level = srclevel; 885 src_templ->u.tex.last_level = srclevel; 886 src_templ->u.tex.first_layer = 0; 887 src_templ->u.tex.last_layer = 888 src->target == PIPE_TEXTURE_3D ? src->depth0 - 1 889 : src->array_size - 1; 890 src_templ->swizzle_r = PIPE_SWIZZLE_RED; 891 src_templ->swizzle_g = PIPE_SWIZZLE_GREEN; 892 src_templ->swizzle_b = PIPE_SWIZZLE_BLUE; 893 src_templ->swizzle_a = PIPE_SWIZZLE_ALPHA; 894} 895 896void util_blitter_copy_texture(struct blitter_context *blitter, 897 struct pipe_resource *dst, 898 unsigned dstlevel, 899 unsigned dstx, unsigned dsty, unsigned dstz, 900 struct pipe_resource *src, 901 unsigned srclevel, 902 const struct pipe_box *srcbox, 903 boolean ignore_stencil) 904{ 905 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 906 struct pipe_context *pipe = ctx->base.pipe; 907 struct pipe_screen *screen = pipe->screen; 908 struct pipe_surface *dst_view, dst_templ; 909 struct pipe_sampler_view src_templ, *src_view; 910 unsigned bind; 911 boolean is_stencil, is_depth; 912 913 /* Give up if textures are not set. */ 914 assert(dst && src); 915 if (!dst || !src) 916 return; 917 918 assert(src->target < PIPE_MAX_TEXTURE_TYPES); 919 920 /* Is this a ZS format? */ 921 is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0; 922 is_stencil = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 1) != 0; 923 924 if (is_depth || is_stencil) 925 bind = PIPE_BIND_DEPTH_STENCIL; 926 else 927 bind = PIPE_BIND_RENDER_TARGET; 928 929 /* Check if we can sample from and render to the surfaces. */ 930 /* (assuming copying a stencil buffer is not possible) */ 931 if ((!ignore_stencil && is_stencil) || 932 !screen->is_format_supported(screen, dst->format, dst->target, 933 dst->nr_samples, bind) || 934 !screen->is_format_supported(screen, src->format, src->target, 935 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) { 936 blitter_set_running_flag(ctx); 937 util_resource_copy_region(pipe, dst, dstlevel, dstx, dsty, dstz, 938 src, srclevel, srcbox); 939 blitter_unset_running_flag(ctx); 940 return; 941 } 942 943 /* Initialize the surface. */ 944 util_blitter_default_dst_texture(&dst_templ, dst, dstlevel, dstz, srcbox); 945 dst_view = pipe->create_surface(pipe, dst, &dst_templ); 946 947 /* Initialize the sampler view. */ 948 util_blitter_default_src_texture(&src_templ, src, srclevel); 949 src_view = pipe->create_sampler_view(pipe, src, &src_templ); 950 951 /* Copy. */ 952 util_blitter_copy_texture_view(blitter, dst_view, dstx, dsty, src_view, 953 srcbox, src->width0, src->height0); 954 955 pipe_surface_reference(&dst_view, NULL); 956 pipe_sampler_view_reference(&src_view, NULL); 957} 958 959void util_blitter_copy_texture_view(struct blitter_context *blitter, 960 struct pipe_surface *dst, 961 unsigned dstx, unsigned dsty, 962 struct pipe_sampler_view *src, 963 const struct pipe_box *srcbox, 964 unsigned src_width0, unsigned src_height0) 965{ 966 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 967 struct pipe_context *pipe = ctx->base.pipe; 968 struct pipe_framebuffer_state fb_state; 969 enum pipe_texture_target src_target = src->texture->target; 970 unsigned width = srcbox->width; 971 unsigned height = srcbox->height; 972 973 /* Sanity checks. */ 974 if (dst->texture == src->texture && 975 dst->u.tex.level == src->u.tex.first_level) { 976 assert(!is_overlap(srcbox->x, srcbox->x + width, srcbox->y, srcbox->y + height, 977 dstx, dstx + width, dsty, dsty + height)); 978 } 979 /* XXX should handle 3d regions */ 980 assert(srcbox->depth == 1); 981 982 /* Check whether the states are properly saved. */ 983 blitter_set_running_flag(ctx); 984 blitter_check_saved_vertex_states(ctx); 985 blitter_check_saved_fragment_states(ctx); 986 blitter_check_saved_textures(ctx); 987 blitter_check_saved_fb_state(ctx); 988 989 /* Initialize framebuffer state. */ 990 fb_state.width = dst->width; 991 fb_state.height = dst->height; 992 993 if (util_format_is_depth_or_stencil(dst->format)) { 994 pipe->bind_blend_state(pipe, ctx->blend_keep_color); 995 pipe->bind_depth_stencil_alpha_state(pipe, 996 ctx->dsa_write_depth_keep_stencil); 997 pipe->bind_fs_state(pipe, 998 blitter_get_fs_texfetch_depth(ctx, src_target)); 999 1000 fb_state.nr_cbufs = 0; 1001 fb_state.zsbuf = dst; 1002 } else { 1003 pipe->bind_blend_state(pipe, ctx->blend_write_color); 1004 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); 1005 pipe->bind_fs_state(pipe, 1006 blitter_get_fs_texfetch_col(ctx, src_target)); 1007 1008 fb_state.nr_cbufs = 1; 1009 fb_state.cbufs[0] = dst; 1010 fb_state.zsbuf = 0; 1011 } 1012 1013 /* Set rasterizer state, shaders, and textures. */ 1014 pipe->bind_rasterizer_state(pipe, ctx->rs_state); 1015 pipe->bind_vs_state(pipe, ctx->vs); 1016 if (ctx->has_geometry_shader) 1017 pipe->bind_gs_state(pipe, NULL); 1018 if (ctx->has_stream_out) 1019 pipe->set_stream_output_targets(pipe, 0, NULL, 0); 1020 pipe->bind_fragment_sampler_states(pipe, 1, &ctx->sampler_state); 1021 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 1022 pipe->set_fragment_sampler_views(pipe, 1, &src); 1023 pipe->set_framebuffer_state(pipe, &fb_state); 1024 1025 blitter_set_dst_dimensions(ctx, dst->width, dst->height); 1026 1027 switch (src_target) { 1028 /* Draw the quad with the draw_rectangle callback. */ 1029 case PIPE_TEXTURE_1D: 1030 case PIPE_TEXTURE_2D: 1031 case PIPE_TEXTURE_RECT: 1032 { 1033 /* Set texture coordinates. - use a pipe color union 1034 * for interface purposes. 1035 * XXX pipe_color_union is a wrong name since we use that to set 1036 * texture coordinates too. 1037 */ 1038 union pipe_color_union coord; 1039 get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y, 1040 srcbox->x+width, srcbox->y+height, coord.f); 1041 1042 /* Draw. */ 1043 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0, 1044 UTIL_BLITTER_ATTRIB_TEXCOORD, &coord); 1045 } 1046 break; 1047 1048 /* Draw the quad with the generic codepath. */ 1049 default: 1050 /* Set texture coordinates. */ 1051 switch (src_target) { 1052 case PIPE_TEXTURE_1D_ARRAY: 1053 case PIPE_TEXTURE_2D_ARRAY: 1054 case PIPE_TEXTURE_3D: 1055 case PIPE_TEXTURE_CUBE: 1056 blitter_set_texcoords(ctx, src, src_width0, src_height0, srcbox->z, 1057 srcbox->y, srcbox->x, 1058 srcbox->x + width, srcbox->y + height); 1059 break; 1060 1061 default: 1062 assert(0); 1063 } 1064 1065 blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, 0); 1066 break; 1067 } 1068 1069 blitter_restore_vertex_states(ctx); 1070 blitter_restore_fragment_states(ctx); 1071 blitter_restore_textures(ctx); 1072 blitter_restore_fb_state(ctx); 1073 blitter_unset_running_flag(ctx); 1074} 1075 1076/* Clear a region of a color surface to a constant value. */ 1077void util_blitter_clear_render_target(struct blitter_context *blitter, 1078 struct pipe_surface *dstsurf, 1079 const union pipe_color_union *color, 1080 unsigned dstx, unsigned dsty, 1081 unsigned width, unsigned height) 1082{ 1083 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1084 struct pipe_context *pipe = ctx->base.pipe; 1085 struct pipe_framebuffer_state fb_state; 1086 1087 assert(dstsurf->texture); 1088 if (!dstsurf->texture) 1089 return; 1090 1091 /* check the saved state */ 1092 blitter_set_running_flag(ctx); 1093 blitter_check_saved_vertex_states(ctx); 1094 blitter_check_saved_fragment_states(ctx); 1095 blitter_check_saved_fb_state(ctx); 1096 1097 /* bind states */ 1098 pipe->bind_blend_state(pipe, ctx->blend_write_color); 1099 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); 1100 pipe->bind_rasterizer_state(pipe, ctx->rs_state); 1101 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE)); 1102 pipe->bind_vs_state(pipe, ctx->vs); 1103 if (ctx->has_geometry_shader) 1104 pipe->bind_gs_state(pipe, NULL); 1105 if (ctx->has_stream_out) 1106 pipe->set_stream_output_targets(pipe, 0, NULL, 0); 1107 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 1108 1109 /* set a framebuffer state */ 1110 fb_state.width = dstsurf->width; 1111 fb_state.height = dstsurf->height; 1112 fb_state.nr_cbufs = 1; 1113 fb_state.cbufs[0] = dstsurf; 1114 fb_state.zsbuf = 0; 1115 pipe->set_framebuffer_state(pipe, &fb_state); 1116 1117 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height); 1118 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0, 1119 UTIL_BLITTER_ATTRIB_COLOR, color); 1120 1121 blitter_restore_vertex_states(ctx); 1122 blitter_restore_fragment_states(ctx); 1123 blitter_restore_fb_state(ctx); 1124 blitter_unset_running_flag(ctx); 1125} 1126 1127/* Clear a region of a depth stencil surface. */ 1128void util_blitter_clear_depth_stencil(struct blitter_context *blitter, 1129 struct pipe_surface *dstsurf, 1130 unsigned clear_flags, 1131 double depth, 1132 unsigned stencil, 1133 unsigned dstx, unsigned dsty, 1134 unsigned width, unsigned height) 1135{ 1136 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1137 struct pipe_context *pipe = ctx->base.pipe; 1138 struct pipe_framebuffer_state fb_state; 1139 struct pipe_stencil_ref sr = { { 0 } }; 1140 1141 assert(dstsurf->texture); 1142 if (!dstsurf->texture) 1143 return; 1144 1145 /* check the saved state */ 1146 blitter_set_running_flag(ctx); 1147 blitter_check_saved_vertex_states(ctx); 1148 blitter_check_saved_fragment_states(ctx); 1149 blitter_check_saved_fb_state(ctx); 1150 1151 /* bind states */ 1152 pipe->bind_blend_state(pipe, ctx->blend_keep_color); 1153 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) { 1154 sr.ref_value[0] = stencil & 0xff; 1155 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil); 1156 pipe->set_stencil_ref(pipe, &sr); 1157 } 1158 else if (clear_flags & PIPE_CLEAR_DEPTH) { 1159 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil); 1160 } 1161 else if (clear_flags & PIPE_CLEAR_STENCIL) { 1162 sr.ref_value[0] = stencil & 0xff; 1163 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil); 1164 pipe->set_stencil_ref(pipe, &sr); 1165 } 1166 else 1167 /* hmm that should be illegal probably, or make it a no-op somewhere */ 1168 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); 1169 1170 pipe->bind_rasterizer_state(pipe, ctx->rs_state); 1171 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE)); 1172 pipe->bind_vs_state(pipe, ctx->vs); 1173 if (ctx->has_geometry_shader) 1174 pipe->bind_gs_state(pipe, NULL); 1175 if (ctx->has_stream_out) 1176 pipe->set_stream_output_targets(pipe, 0, NULL, 0); 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 = 0; 1183 fb_state.cbufs[0] = 0; 1184 fb_state.zsbuf = dstsurf; 1185 pipe->set_framebuffer_state(pipe, &fb_state); 1186 1187 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height); 1188 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, depth, 1189 UTIL_BLITTER_ATTRIB_NONE, NULL); 1190 1191 blitter_restore_vertex_states(ctx); 1192 blitter_restore_fragment_states(ctx); 1193 blitter_restore_fb_state(ctx); 1194 blitter_unset_running_flag(ctx); 1195} 1196 1197/* draw a rectangle across a region using a custom dsa stage - for r600g */ 1198void util_blitter_custom_depth_stencil(struct blitter_context *blitter, 1199 struct pipe_surface *zsurf, 1200 struct pipe_surface *cbsurf, 1201 void *dsa_stage, float depth) 1202{ 1203 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1204 struct pipe_context *pipe = ctx->base.pipe; 1205 struct pipe_framebuffer_state fb_state; 1206 1207 assert(zsurf->texture); 1208 if (!zsurf->texture) 1209 return; 1210 1211 /* check the saved state */ 1212 blitter_set_running_flag(ctx); 1213 blitter_check_saved_vertex_states(ctx); 1214 blitter_check_saved_fragment_states(ctx); 1215 blitter_check_saved_fb_state(ctx); 1216 1217 /* bind states */ 1218 pipe->bind_blend_state(pipe, ctx->blend_write_color); 1219 pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage); 1220 1221 pipe->bind_rasterizer_state(pipe, ctx->rs_state); 1222 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE)); 1223 pipe->bind_vs_state(pipe, ctx->vs); 1224 if (ctx->has_geometry_shader) 1225 pipe->bind_gs_state(pipe, NULL); 1226 if (ctx->has_stream_out) 1227 pipe->set_stream_output_targets(pipe, 0, NULL, 0); 1228 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 1229 1230 /* set a framebuffer state */ 1231 fb_state.width = zsurf->width; 1232 fb_state.height = zsurf->height; 1233 fb_state.nr_cbufs = 1; 1234 if (cbsurf) { 1235 fb_state.cbufs[0] = cbsurf; 1236 fb_state.nr_cbufs = 1; 1237 } else { 1238 fb_state.cbufs[0] = NULL; 1239 fb_state.nr_cbufs = 0; 1240 } 1241 fb_state.zsbuf = zsurf; 1242 pipe->set_framebuffer_state(pipe, &fb_state); 1243 1244 blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height); 1245 blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth, 1246 UTIL_BLITTER_ATTRIB_NONE, NULL); 1247 1248 blitter_restore_vertex_states(ctx); 1249 blitter_restore_fragment_states(ctx); 1250 blitter_restore_fb_state(ctx); 1251 blitter_unset_running_flag(ctx); 1252} 1253 1254void util_blitter_copy_buffer(struct blitter_context *blitter, 1255 struct pipe_resource *dst, 1256 unsigned dstx, 1257 struct pipe_resource *src, 1258 unsigned srcx, 1259 unsigned size) 1260{ 1261 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1262 struct pipe_context *pipe = ctx->base.pipe; 1263 struct pipe_vertex_buffer vb; 1264 struct pipe_stream_output_target *so_target; 1265 1266 if (srcx >= src->width0 || 1267 dstx >= dst->width0) { 1268 return; 1269 } 1270 if (srcx + size > src->width0) { 1271 size = src->width0 - srcx; 1272 } 1273 if (dstx + size > dst->width0) { 1274 size = dst->width0 - dstx; 1275 } 1276 1277 /* Drivers not capable of Stream Out should not call this function 1278 * in the first place. */ 1279 assert(ctx->has_stream_out); 1280 1281 /* Some alignment is required. */ 1282 if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 || 1283 !ctx->has_stream_out) { 1284 struct pipe_box box; 1285 u_box_1d(srcx, size, &box); 1286 util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box); 1287 return; 1288 } 1289 1290 blitter_set_running_flag(ctx); 1291 blitter_check_saved_vertex_states(ctx); 1292 1293 vb.buffer = src; 1294 vb.buffer_offset = srcx; 1295 vb.stride = 4; 1296 1297 pipe->set_vertex_buffers(pipe, 1, &vb); 1298 pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf); 1299 pipe->bind_vs_state(pipe, ctx->vs_pos_only); 1300 if (ctx->has_geometry_shader) 1301 pipe->bind_gs_state(pipe, NULL); 1302 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state); 1303 1304 so_target = pipe->create_stream_output_target(pipe, dst, dstx, size); 1305 pipe->set_stream_output_targets(pipe, 1, &so_target, 0); 1306 1307 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4); 1308 1309 blitter_restore_vertex_states(ctx); 1310 blitter_unset_running_flag(ctx); 1311 pipe_so_target_reference(&so_target, NULL); 1312} 1313