u_blitter.c revision 84645fa61390475e6efb080685e0dec059622a39
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 /* Sample mask. */ 443 if (ctx->base.is_sample_mask_saved) { 444 pipe->set_sample_mask(pipe, ctx->base.saved_sample_mask); 445 ctx->base.is_sample_mask_saved = FALSE; 446 } 447 448 /* Miscellaneous states. */ 449 /* XXX check whether these are saved and whether they need to be restored 450 * (depending on the operation) */ 451 pipe->set_stencil_ref(pipe, &ctx->base.saved_stencil_ref); 452 pipe->set_viewport_state(pipe, &ctx->base.saved_viewport); 453} 454 455static void blitter_check_saved_fb_state(struct blitter_context_priv *ctx) 456{ 457 assert(ctx->base.saved_fb_state.nr_cbufs != ~0); 458} 459 460static void blitter_restore_fb_state(struct blitter_context_priv *ctx) 461{ 462 struct pipe_context *pipe = ctx->base.pipe; 463 464 pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state); 465 util_unreference_framebuffer_state(&ctx->base.saved_fb_state); 466} 467 468static void blitter_check_saved_textures(struct blitter_context_priv *ctx) 469{ 470 assert(ctx->base.saved_num_sampler_states != ~0); 471 assert(ctx->base.saved_num_sampler_views != ~0); 472} 473 474static void blitter_restore_textures(struct blitter_context_priv *ctx) 475{ 476 struct pipe_context *pipe = ctx->base.pipe; 477 unsigned i; 478 479 /* Fragment sampler states. */ 480 pipe->bind_fragment_sampler_states(pipe, 481 ctx->base.saved_num_sampler_states, 482 ctx->base.saved_sampler_states); 483 ctx->base.saved_num_sampler_states = ~0; 484 485 /* Fragment sampler views. */ 486 pipe->set_fragment_sampler_views(pipe, 487 ctx->base.saved_num_sampler_views, 488 ctx->base.saved_sampler_views); 489 490 for (i = 0; i < ctx->base.saved_num_sampler_views; i++) 491 pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL); 492 493 ctx->base.saved_num_sampler_views = ~0; 494} 495 496static void blitter_set_rectangle(struct blitter_context_priv *ctx, 497 unsigned x1, unsigned y1, 498 unsigned x2, unsigned y2, 499 float depth) 500{ 501 int i; 502 503 /* set vertex positions */ 504 ctx->vertices[0][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v0.x*/ 505 ctx->vertices[0][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v0.y*/ 506 507 ctx->vertices[1][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v1.x*/ 508 ctx->vertices[1][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v1.y*/ 509 510 ctx->vertices[2][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v2.x*/ 511 ctx->vertices[2][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v2.y*/ 512 513 ctx->vertices[3][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v3.x*/ 514 ctx->vertices[3][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v3.y*/ 515 516 for (i = 0; i < 4; i++) 517 ctx->vertices[i][0][2] = depth; /*z*/ 518 519 /* viewport */ 520 ctx->viewport.scale[0] = 0.5f * ctx->dst_width; 521 ctx->viewport.scale[1] = 0.5f * ctx->dst_height; 522 ctx->viewport.scale[2] = 1.0f; 523 ctx->viewport.scale[3] = 1.0f; 524 ctx->viewport.translate[0] = 0.5f * ctx->dst_width; 525 ctx->viewport.translate[1] = 0.5f * ctx->dst_height; 526 ctx->viewport.translate[2] = 0.0f; 527 ctx->viewport.translate[3] = 0.0f; 528 ctx->base.pipe->set_viewport_state(ctx->base.pipe, &ctx->viewport); 529} 530 531static void blitter_set_clear_color(struct blitter_context_priv *ctx, 532 const union pipe_color_union *color) 533{ 534 int i; 535 536 if (color) { 537 for (i = 0; i < 4; i++) { 538 uint32_t *uiverts = (uint32_t *)ctx->vertices[i][1]; 539 uiverts[0] = color->ui[0]; 540 uiverts[1] = color->ui[1]; 541 uiverts[2] = color->ui[2]; 542 uiverts[3] = color->ui[3]; 543 } 544 } else { 545 for (i = 0; i < 4; i++) { 546 ctx->vertices[i][1][0] = 0; 547 ctx->vertices[i][1][1] = 0; 548 ctx->vertices[i][1][2] = 0; 549 ctx->vertices[i][1][3] = 0; 550 } 551 } 552} 553 554static void get_texcoords(struct pipe_sampler_view *src, 555 unsigned src_width0, unsigned src_height0, 556 unsigned x1, unsigned y1, 557 unsigned x2, unsigned y2, 558 float out[4]) 559{ 560 struct pipe_resource *tex = src->texture; 561 unsigned level = src->u.tex.first_level; 562 boolean normalized = tex->target != PIPE_TEXTURE_RECT; 563 564 if (normalized) { 565 out[0] = x1 / (float)u_minify(src_width0, level); 566 out[1] = y1 / (float)u_minify(src_height0, level); 567 out[2] = x2 / (float)u_minify(src_width0, level); 568 out[3] = y2 / (float)u_minify(src_height0, level); 569 } else { 570 out[0] = x1; 571 out[1] = y1; 572 out[2] = x2; 573 out[3] = y2; 574 } 575} 576 577static void set_texcoords_in_vertices(const float coord[4], 578 float *out, unsigned stride) 579{ 580 out[0] = coord[0]; /*t0.s*/ 581 out[1] = coord[1]; /*t0.t*/ 582 out += stride; 583 out[0] = coord[2]; /*t1.s*/ 584 out[1] = coord[1]; /*t1.t*/ 585 out += stride; 586 out[0] = coord[2]; /*t2.s*/ 587 out[1] = coord[3]; /*t2.t*/ 588 out += stride; 589 out[0] = coord[0]; /*t3.s*/ 590 out[1] = coord[3]; /*t3.t*/ 591} 592 593static void blitter_set_texcoords(struct blitter_context_priv *ctx, 594 struct pipe_sampler_view *src, 595 unsigned src_width0, unsigned src_height0, 596 unsigned layer, 597 unsigned x1, unsigned y1, 598 unsigned x2, unsigned y2) 599{ 600 unsigned i; 601 float coord[4]; 602 float face_coord[4][2]; 603 604 get_texcoords(src, src_width0, src_height0, x1, y1, x2, y2, coord); 605 606 if (src->texture->target == PIPE_TEXTURE_CUBE) { 607 set_texcoords_in_vertices(coord, &face_coord[0][0], 2); 608 util_map_texcoords2d_onto_cubemap(layer, 609 /* pointer, stride in floats */ 610 &face_coord[0][0], 2, 611 &ctx->vertices[0][1][0], 8); 612 } else { 613 set_texcoords_in_vertices(coord, &ctx->vertices[0][1][0], 8); 614 } 615 616 /* Set the layer. */ 617 switch (src->texture->target) { 618 case PIPE_TEXTURE_3D: 619 { 620 float r = layer / (float)u_minify(src->texture->depth0, 621 src->u.tex.first_level); 622 for (i = 0; i < 4; i++) 623 ctx->vertices[i][1][2] = r; /*r*/ 624 } 625 break; 626 627 case PIPE_TEXTURE_1D_ARRAY: 628 for (i = 0; i < 4; i++) 629 ctx->vertices[i][1][1] = layer; /*t*/ 630 break; 631 632 case PIPE_TEXTURE_2D_ARRAY: 633 for (i = 0; i < 4; i++) 634 ctx->vertices[i][1][2] = layer; /*r*/ 635 break; 636 637 default:; 638 } 639} 640 641static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx, 642 unsigned width, unsigned height) 643{ 644 ctx->dst_width = width; 645 ctx->dst_height = height; 646} 647 648static INLINE 649void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs, 650 boolean int_format) 651{ 652 struct pipe_context *pipe = ctx->base.pipe; 653 654 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS); 655 656 if (int_format) { 657 if (!ctx->fs_col_int[num_cbufs]) 658 ctx->fs_col_int[num_cbufs] = 659 util_make_fragment_cloneinput_shader(pipe, num_cbufs, 660 TGSI_SEMANTIC_GENERIC, 661 TGSI_INTERPOLATE_CONSTANT); 662 return ctx->fs_col_int[num_cbufs]; 663 } else { 664 if (!ctx->fs_col[num_cbufs]) 665 ctx->fs_col[num_cbufs] = 666 util_make_fragment_cloneinput_shader(pipe, num_cbufs, 667 TGSI_SEMANTIC_GENERIC, 668 TGSI_INTERPOLATE_LINEAR); 669 return ctx->fs_col[num_cbufs]; 670 } 671} 672 673static INLINE 674void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx, 675 unsigned tex_target) 676{ 677 struct pipe_context *pipe = ctx->base.pipe; 678 679 assert(tex_target < PIPE_MAX_TEXTURE_TYPES); 680 681 /* Create the fragment shader on-demand. */ 682 if (!ctx->fs_texfetch_col[tex_target]) { 683 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex_target); 684 685 ctx->fs_texfetch_col[tex_target] = 686 util_make_fragment_tex_shader(pipe, tgsi_tex, TGSI_INTERPOLATE_LINEAR); 687 } 688 689 return ctx->fs_texfetch_col[tex_target]; 690} 691 692static INLINE 693void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx, 694 unsigned tex_target) 695{ 696 struct pipe_context *pipe = ctx->base.pipe; 697 698 assert(tex_target < PIPE_MAX_TEXTURE_TYPES); 699 700 /* Create the fragment shader on-demand. */ 701 if (!ctx->fs_texfetch_depth[tex_target]) { 702 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex_target); 703 704 ctx->fs_texfetch_depth[tex_target] = 705 util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex, 706 TGSI_INTERPOLATE_LINEAR); 707 } 708 709 return ctx->fs_texfetch_depth[tex_target]; 710} 711 712static INLINE 713void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx, 714 unsigned tex_target) 715{ 716 struct pipe_context *pipe = ctx->base.pipe; 717 718 assert(tex_target < PIPE_MAX_TEXTURE_TYPES); 719 720 /* Create the fragment shader on-demand. */ 721 if (!ctx->fs_texfetch_depthstencil[tex_target]) { 722 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex_target); 723 724 ctx->fs_texfetch_depthstencil[tex_target] = 725 util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex, 726 TGSI_INTERPOLATE_LINEAR); 727 } 728 729 return ctx->fs_texfetch_depthstencil[tex_target]; 730} 731 732static INLINE 733void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx, 734 unsigned tex_target) 735{ 736 struct pipe_context *pipe = ctx->base.pipe; 737 738 assert(tex_target < PIPE_MAX_TEXTURE_TYPES); 739 740 /* Create the fragment shader on-demand. */ 741 if (!ctx->fs_texfetch_stencil[tex_target]) { 742 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex_target); 743 744 ctx->fs_texfetch_stencil[tex_target] = 745 util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex, 746 TGSI_INTERPOLATE_LINEAR); 747 } 748 749 return ctx->fs_texfetch_stencil[tex_target]; 750} 751 752static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx) 753{ 754 struct pipe_context *pipe = ctx->base.pipe; 755 756 pipe->bind_rasterizer_state(pipe, ctx->rs_state); 757 pipe->bind_vs_state(pipe, ctx->vs); 758 if (ctx->has_geometry_shader) 759 pipe->bind_gs_state(pipe, NULL); 760 if (ctx->has_stream_out) 761 pipe->set_stream_output_targets(pipe, 0, NULL, 0); 762} 763 764static void blitter_draw(struct blitter_context_priv *ctx, 765 unsigned x1, unsigned y1, 766 unsigned x2, unsigned y2, 767 float depth) 768{ 769 struct pipe_resource *buf = NULL; 770 unsigned offset = 0; 771 772 blitter_set_rectangle(ctx, x1, y1, x2, y2, depth); 773 774 u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), ctx->vertices, 775 &offset, &buf); 776 u_upload_unmap(ctx->upload); 777 util_draw_vertex_buffer(ctx->base.pipe, NULL, buf, offset, 778 PIPE_PRIM_TRIANGLE_FAN, 4, 2); 779 pipe_resource_reference(&buf, NULL); 780} 781 782static void blitter_draw_rectangle(struct blitter_context *blitter, 783 unsigned x1, unsigned y1, 784 unsigned x2, unsigned y2, 785 float depth, 786 enum blitter_attrib_type type, 787 const union pipe_color_union *attrib) 788{ 789 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 790 791 switch (type) { 792 case UTIL_BLITTER_ATTRIB_COLOR: 793 blitter_set_clear_color(ctx, attrib); 794 break; 795 796 case UTIL_BLITTER_ATTRIB_TEXCOORD: 797 set_texcoords_in_vertices(attrib->f, &ctx->vertices[0][1][0], 8); 798 break; 799 800 default:; 801 } 802 803 blitter_draw(ctx, x1, y1, x2, y2, depth); 804} 805 806static void util_blitter_clear_custom(struct blitter_context *blitter, 807 unsigned width, unsigned height, 808 unsigned num_cbufs, 809 unsigned clear_buffers, 810 enum pipe_format cbuf_format, 811 const union pipe_color_union *color, 812 double depth, unsigned stencil, 813 void *custom_blend, void *custom_dsa) 814{ 815 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 816 struct pipe_context *pipe = ctx->base.pipe; 817 struct pipe_stencil_ref sr = { { 0 } }; 818 boolean int_format = util_format_is_pure_integer(cbuf_format); 819 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS); 820 821 blitter_set_running_flag(ctx); 822 blitter_check_saved_vertex_states(ctx); 823 blitter_check_saved_fragment_states(ctx); 824 825 /* bind states */ 826 if (custom_blend) { 827 pipe->bind_blend_state(pipe, custom_blend); 828 } else if (clear_buffers & PIPE_CLEAR_COLOR) { 829 pipe->bind_blend_state(pipe, ctx->blend_write_color); 830 } else { 831 pipe->bind_blend_state(pipe, ctx->blend_keep_color); 832 } 833 834 if (custom_dsa) { 835 pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa); 836 } else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) { 837 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil); 838 } else if (clear_buffers & PIPE_CLEAR_DEPTH) { 839 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil); 840 } else if (clear_buffers & PIPE_CLEAR_STENCIL) { 841 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil); 842 } else { 843 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); 844 } 845 846 sr.ref_value[0] = stencil & 0xff; 847 pipe->set_stencil_ref(pipe, &sr); 848 849 if (util_format_is_pure_sint(cbuf_format)) { 850 pipe->bind_vertex_elements_state(pipe, ctx->velem_sint_state); 851 } else if (util_format_is_pure_uint(cbuf_format)) { 852 pipe->bind_vertex_elements_state(pipe, ctx->velem_uint_state); 853 } else { 854 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 855 } 856 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs, int_format)); 857 pipe->set_sample_mask(pipe, ~0); 858 859 blitter_set_common_draw_rect_state(ctx); 860 blitter_set_dst_dimensions(ctx, width, height); 861 blitter->draw_rectangle(blitter, 0, 0, width, height, depth, 862 UTIL_BLITTER_ATTRIB_COLOR, color); 863 864 blitter_restore_vertex_states(ctx); 865 blitter_restore_fragment_states(ctx); 866 blitter_unset_running_flag(ctx); 867} 868 869void util_blitter_clear(struct blitter_context *blitter, 870 unsigned width, unsigned height, 871 unsigned num_cbufs, 872 unsigned clear_buffers, 873 enum pipe_format cbuf_format, 874 const union pipe_color_union *color, 875 double depth, unsigned stencil) 876{ 877 util_blitter_clear_custom(blitter, width, height, num_cbufs, 878 clear_buffers, cbuf_format, color, depth, stencil, 879 NULL, NULL); 880} 881 882void util_blitter_clear_depth_custom(struct blitter_context *blitter, 883 unsigned width, unsigned height, 884 double depth, void *custom_dsa) 885{ 886 static const union pipe_color_union color; 887 util_blitter_clear_custom(blitter, width, height, 0, 888 0, PIPE_FORMAT_NONE, &color, depth, 0, NULL, custom_dsa); 889} 890 891static 892boolean is_overlap(unsigned sx1, unsigned sx2, unsigned sy1, unsigned sy2, 893 unsigned dx1, unsigned dx2, unsigned dy1, unsigned dy2) 894{ 895 return sx1 < dx2 && sx2 > dx1 && sy1 < dy2 && sy2 > dy1; 896} 897 898void util_blitter_default_dst_texture(struct pipe_surface *dst_templ, 899 struct pipe_resource *dst, 900 unsigned dstlevel, 901 unsigned dstz, 902 const struct pipe_box *srcbox) 903{ 904 memset(dst_templ, 0, sizeof(*dst_templ)); 905 dst_templ->format = dst->format; 906 if (util_format_is_depth_or_stencil(dst->format)) { 907 dst_templ->usage = PIPE_BIND_DEPTH_STENCIL; 908 } else { 909 dst_templ->usage = PIPE_BIND_RENDER_TARGET; 910 } 911 dst_templ->format = util_format_linear(dst->format); 912 dst_templ->u.tex.level = dstlevel; 913 dst_templ->u.tex.first_layer = dstz; 914 dst_templ->u.tex.last_layer = dstz + srcbox->depth - 1; 915} 916 917void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ, 918 struct pipe_resource *src, 919 unsigned srclevel) 920{ 921 memset(src_templ, 0, sizeof(*src_templ)); 922 src_templ->format = util_format_linear(src->format); 923 src_templ->u.tex.first_level = srclevel; 924 src_templ->u.tex.last_level = srclevel; 925 src_templ->u.tex.first_layer = 0; 926 src_templ->u.tex.last_layer = 927 src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1 928 : src->array_size - 1; 929 src_templ->swizzle_r = PIPE_SWIZZLE_RED; 930 src_templ->swizzle_g = PIPE_SWIZZLE_GREEN; 931 src_templ->swizzle_b = PIPE_SWIZZLE_BLUE; 932 src_templ->swizzle_a = PIPE_SWIZZLE_ALPHA; 933} 934 935boolean util_blitter_is_copy_supported(struct blitter_context *blitter, 936 const struct pipe_resource *dst, 937 const struct pipe_resource *src, 938 unsigned mask) 939{ 940 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 941 struct pipe_screen *screen = ctx->base.pipe->screen; 942 943 if (dst) { 944 unsigned bind; 945 boolean is_stencil; 946 const struct util_format_description *desc = 947 util_format_description(dst->format); 948 949 is_stencil = util_format_has_stencil(desc); 950 951 /* Stencil export must be supported for stencil copy. */ 952 if ((mask & PIPE_MASK_S) && is_stencil && !ctx->has_stencil_export) { 953 return FALSE; 954 } 955 956 if (is_stencil || util_format_has_depth(desc)) 957 bind = PIPE_BIND_DEPTH_STENCIL; 958 else 959 bind = PIPE_BIND_RENDER_TARGET; 960 961 if (!screen->is_format_supported(screen, dst->format, dst->target, 962 dst->nr_samples, bind)) { 963 return FALSE; 964 } 965 } 966 967 if (src) { 968 if (!screen->is_format_supported(screen, src->format, src->target, 969 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) { 970 return FALSE; 971 } 972 973 /* Check stencil sampler support for stencil copy. */ 974 if (util_format_has_stencil(util_format_description(src->format))) { 975 enum pipe_format stencil_format = 976 util_format_stencil_only(src->format); 977 assert(stencil_format != PIPE_FORMAT_NONE); 978 979 if (stencil_format != src->format && 980 !screen->is_format_supported(screen, stencil_format, src->target, 981 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) { 982 return FALSE; 983 } 984 } 985 } 986 987 return TRUE; 988} 989 990void util_blitter_copy_texture(struct blitter_context *blitter, 991 struct pipe_resource *dst, 992 unsigned dst_level, 993 unsigned dstx, unsigned dsty, unsigned dstz, 994 struct pipe_resource *src, 995 unsigned src_level, 996 const struct pipe_box *srcbox) 997{ 998 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 999 struct pipe_context *pipe = ctx->base.pipe; 1000 struct pipe_surface *dst_view, dst_templ; 1001 struct pipe_sampler_view src_templ, *src_view; 1002 1003 assert(dst && src); 1004 assert(src->target < PIPE_MAX_TEXTURE_TYPES); 1005 1006 /* Initialize the surface. */ 1007 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz, srcbox); 1008 dst_view = pipe->create_surface(pipe, dst, &dst_templ); 1009 1010 /* Initialize the sampler view. */ 1011 util_blitter_default_src_texture(&src_templ, src, src_level); 1012 src_view = pipe->create_sampler_view(pipe, src, &src_templ); 1013 1014 /* Copy. */ 1015 util_blitter_copy_texture_view(blitter, dst_view, dstx, dsty, src_view, 1016 srcbox, src->width0, src->height0, 1017 PIPE_MASK_RGBAZS); 1018 1019 pipe_surface_reference(&dst_view, NULL); 1020 pipe_sampler_view_reference(&src_view, NULL); 1021} 1022 1023void util_blitter_copy_texture_view(struct blitter_context *blitter, 1024 struct pipe_surface *dst, 1025 unsigned dstx, unsigned dsty, 1026 struct pipe_sampler_view *src, 1027 const struct pipe_box *srcbox, 1028 unsigned src_width0, unsigned src_height0, 1029 unsigned mask) 1030{ 1031 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1032 struct pipe_context *pipe = ctx->base.pipe; 1033 struct pipe_framebuffer_state fb_state; 1034 enum pipe_texture_target src_target = src->texture->target; 1035 unsigned width = srcbox->width; 1036 unsigned height = srcbox->height; 1037 boolean blit_stencil, blit_depth; 1038 const struct util_format_description *src_desc = 1039 util_format_description(src->format); 1040 1041 blit_depth = util_format_has_depth(src_desc) && (mask & PIPE_MASK_Z); 1042 blit_stencil = util_format_has_stencil(src_desc) && (mask & PIPE_MASK_S); 1043 1044 /* If you want a fallback for stencil copies, 1045 * use util_blitter_copy_texture. */ 1046 if (blit_stencil && !ctx->has_stencil_export) { 1047 blit_stencil = FALSE; 1048 1049 if (!blit_depth) 1050 return; 1051 } 1052 1053 /* Sanity checks. */ 1054 if (dst->texture == src->texture && 1055 dst->u.tex.level == src->u.tex.first_level) { 1056 assert(!is_overlap(srcbox->x, srcbox->x + width, srcbox->y, srcbox->y + height, 1057 dstx, dstx + width, dsty, dsty + height)); 1058 } 1059 /* XXX should handle 3d regions */ 1060 assert(srcbox->depth == 1); 1061 1062 /* Check whether the states are properly saved. */ 1063 blitter_set_running_flag(ctx); 1064 blitter_check_saved_vertex_states(ctx); 1065 blitter_check_saved_fragment_states(ctx); 1066 blitter_check_saved_textures(ctx); 1067 blitter_check_saved_fb_state(ctx); 1068 1069 /* Initialize framebuffer state. */ 1070 fb_state.width = dst->width; 1071 fb_state.height = dst->height; 1072 1073 if (blit_depth || blit_stencil) { 1074 pipe->bind_blend_state(pipe, ctx->blend_keep_color); 1075 1076 if (blit_depth && blit_stencil) { 1077 pipe->bind_depth_stencil_alpha_state(pipe, 1078 ctx->dsa_write_depth_stencil); 1079 pipe->bind_fs_state(pipe, 1080 blitter_get_fs_texfetch_depthstencil(ctx, src_target)); 1081 } else if (blit_depth) { 1082 pipe->bind_depth_stencil_alpha_state(pipe, 1083 ctx->dsa_write_depth_keep_stencil); 1084 pipe->bind_fs_state(pipe, 1085 blitter_get_fs_texfetch_depth(ctx, src_target)); 1086 } else { /* is_stencil */ 1087 pipe->bind_depth_stencil_alpha_state(pipe, 1088 ctx->dsa_keep_depth_write_stencil); 1089 pipe->bind_fs_state(pipe, 1090 blitter_get_fs_texfetch_stencil(ctx, src_target)); 1091 } 1092 1093 fb_state.nr_cbufs = 0; 1094 fb_state.zsbuf = dst; 1095 } else { 1096 pipe->bind_blend_state(pipe, ctx->blend_write_color); 1097 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); 1098 pipe->bind_fs_state(pipe, 1099 blitter_get_fs_texfetch_col(ctx, src_target)); 1100 1101 fb_state.nr_cbufs = 1; 1102 fb_state.cbufs[0] = dst; 1103 fb_state.zsbuf = 0; 1104 } 1105 1106 if (blit_depth && blit_stencil) { 1107 /* Setup two samplers, one for depth and the other one for stencil. */ 1108 struct pipe_sampler_view templ; 1109 struct pipe_sampler_view *views[2]; 1110 void *samplers[2] = {ctx->sampler_state, ctx->sampler_state}; 1111 1112 templ = *src; 1113 templ.format = util_format_stencil_only(templ.format); 1114 assert(templ.format != PIPE_FORMAT_NONE); 1115 1116 views[0] = src; 1117 views[1] = pipe->create_sampler_view(pipe, src->texture, &templ); 1118 1119 pipe->set_fragment_sampler_views(pipe, 2, views); 1120 pipe->bind_fragment_sampler_states(pipe, 2, samplers); 1121 1122 pipe_sampler_view_reference(&views[1], NULL); 1123 } else { 1124 pipe->set_fragment_sampler_views(pipe, 1, &src); 1125 pipe->bind_fragment_sampler_states(pipe, 1, &ctx->sampler_state); 1126 } 1127 1128 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 1129 pipe->set_framebuffer_state(pipe, &fb_state); 1130 pipe->set_sample_mask(pipe, ~0); 1131 1132 blitter_set_common_draw_rect_state(ctx); 1133 blitter_set_dst_dimensions(ctx, dst->width, dst->height); 1134 1135 switch (src_target) { 1136 /* Draw the quad with the draw_rectangle callback. */ 1137 case PIPE_TEXTURE_1D: 1138 case PIPE_TEXTURE_2D: 1139 case PIPE_TEXTURE_RECT: 1140 { 1141 /* Set texture coordinates. - use a pipe color union 1142 * for interface purposes. 1143 * XXX pipe_color_union is a wrong name since we use that to set 1144 * texture coordinates too. 1145 */ 1146 union pipe_color_union coord; 1147 get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y, 1148 srcbox->x+width, srcbox->y+height, coord.f); 1149 1150 /* Draw. */ 1151 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0, 1152 UTIL_BLITTER_ATTRIB_TEXCOORD, &coord); 1153 } 1154 break; 1155 1156 /* Draw the quad with the generic codepath. */ 1157 default: 1158 /* Set texture coordinates. */ 1159 switch (src_target) { 1160 case PIPE_TEXTURE_1D_ARRAY: 1161 case PIPE_TEXTURE_2D_ARRAY: 1162 case PIPE_TEXTURE_3D: 1163 case PIPE_TEXTURE_CUBE: 1164 blitter_set_texcoords(ctx, src, src_width0, src_height0, srcbox->z, 1165 srcbox->y, srcbox->x, 1166 srcbox->x + width, srcbox->y + height); 1167 break; 1168 1169 default: 1170 assert(0); 1171 } 1172 1173 blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, 0); 1174 break; 1175 } 1176 1177 blitter_restore_vertex_states(ctx); 1178 blitter_restore_fragment_states(ctx); 1179 blitter_restore_textures(ctx); 1180 blitter_restore_fb_state(ctx); 1181 blitter_unset_running_flag(ctx); 1182} 1183 1184/* Clear a region of a color surface to a constant value. */ 1185void util_blitter_clear_render_target(struct blitter_context *blitter, 1186 struct pipe_surface *dstsurf, 1187 const union pipe_color_union *color, 1188 unsigned dstx, unsigned dsty, 1189 unsigned width, unsigned height) 1190{ 1191 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1192 struct pipe_context *pipe = ctx->base.pipe; 1193 struct pipe_framebuffer_state fb_state; 1194 1195 assert(dstsurf->texture); 1196 if (!dstsurf->texture) 1197 return; 1198 1199 /* check the saved state */ 1200 blitter_set_running_flag(ctx); 1201 blitter_check_saved_vertex_states(ctx); 1202 blitter_check_saved_fragment_states(ctx); 1203 blitter_check_saved_fb_state(ctx); 1204 1205 /* bind states */ 1206 pipe->bind_blend_state(pipe, ctx->blend_write_color); 1207 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); 1208 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE)); 1209 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 1210 1211 /* set a framebuffer state */ 1212 fb_state.width = dstsurf->width; 1213 fb_state.height = dstsurf->height; 1214 fb_state.nr_cbufs = 1; 1215 fb_state.cbufs[0] = dstsurf; 1216 fb_state.zsbuf = 0; 1217 pipe->set_framebuffer_state(pipe, &fb_state); 1218 pipe->set_sample_mask(pipe, ~0); 1219 1220 blitter_set_common_draw_rect_state(ctx); 1221 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height); 1222 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0, 1223 UTIL_BLITTER_ATTRIB_COLOR, color); 1224 1225 blitter_restore_vertex_states(ctx); 1226 blitter_restore_fragment_states(ctx); 1227 blitter_restore_fb_state(ctx); 1228 blitter_unset_running_flag(ctx); 1229} 1230 1231/* Clear a region of a depth stencil surface. */ 1232void util_blitter_clear_depth_stencil(struct blitter_context *blitter, 1233 struct pipe_surface *dstsurf, 1234 unsigned clear_flags, 1235 double depth, 1236 unsigned stencil, 1237 unsigned dstx, unsigned dsty, 1238 unsigned width, unsigned height) 1239{ 1240 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1241 struct pipe_context *pipe = ctx->base.pipe; 1242 struct pipe_framebuffer_state fb_state; 1243 struct pipe_stencil_ref sr = { { 0 } }; 1244 1245 assert(dstsurf->texture); 1246 if (!dstsurf->texture) 1247 return; 1248 1249 /* check the saved state */ 1250 blitter_set_running_flag(ctx); 1251 blitter_check_saved_vertex_states(ctx); 1252 blitter_check_saved_fragment_states(ctx); 1253 blitter_check_saved_fb_state(ctx); 1254 1255 /* bind states */ 1256 pipe->bind_blend_state(pipe, ctx->blend_keep_color); 1257 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) { 1258 sr.ref_value[0] = stencil & 0xff; 1259 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil); 1260 pipe->set_stencil_ref(pipe, &sr); 1261 } 1262 else if (clear_flags & PIPE_CLEAR_DEPTH) { 1263 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil); 1264 } 1265 else if (clear_flags & PIPE_CLEAR_STENCIL) { 1266 sr.ref_value[0] = stencil & 0xff; 1267 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil); 1268 pipe->set_stencil_ref(pipe, &sr); 1269 } 1270 else 1271 /* hmm that should be illegal probably, or make it a no-op somewhere */ 1272 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil); 1273 1274 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE)); 1275 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 1276 1277 /* set a framebuffer state */ 1278 fb_state.width = dstsurf->width; 1279 fb_state.height = dstsurf->height; 1280 fb_state.nr_cbufs = 0; 1281 fb_state.cbufs[0] = 0; 1282 fb_state.zsbuf = dstsurf; 1283 pipe->set_framebuffer_state(pipe, &fb_state); 1284 pipe->set_sample_mask(pipe, ~0); 1285 1286 blitter_set_common_draw_rect_state(ctx); 1287 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height); 1288 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, depth, 1289 UTIL_BLITTER_ATTRIB_NONE, NULL); 1290 1291 blitter_restore_vertex_states(ctx); 1292 blitter_restore_fragment_states(ctx); 1293 blitter_restore_fb_state(ctx); 1294 blitter_unset_running_flag(ctx); 1295} 1296 1297/* draw a rectangle across a region using a custom dsa stage - for r600g */ 1298void util_blitter_custom_depth_stencil(struct blitter_context *blitter, 1299 struct pipe_surface *zsurf, 1300 struct pipe_surface *cbsurf, 1301 void *dsa_stage, float depth) 1302{ 1303 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1304 struct pipe_context *pipe = ctx->base.pipe; 1305 struct pipe_framebuffer_state fb_state; 1306 1307 assert(zsurf->texture); 1308 if (!zsurf->texture) 1309 return; 1310 1311 /* check the saved state */ 1312 blitter_set_running_flag(ctx); 1313 blitter_check_saved_vertex_states(ctx); 1314 blitter_check_saved_fragment_states(ctx); 1315 blitter_check_saved_fb_state(ctx); 1316 1317 /* bind states */ 1318 pipe->bind_blend_state(pipe, ctx->blend_write_color); 1319 pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage); 1320 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE)); 1321 pipe->bind_vertex_elements_state(pipe, ctx->velem_state); 1322 1323 /* set a framebuffer state */ 1324 fb_state.width = zsurf->width; 1325 fb_state.height = zsurf->height; 1326 fb_state.nr_cbufs = 1; 1327 if (cbsurf) { 1328 fb_state.cbufs[0] = cbsurf; 1329 fb_state.nr_cbufs = 1; 1330 } else { 1331 fb_state.cbufs[0] = NULL; 1332 fb_state.nr_cbufs = 0; 1333 } 1334 fb_state.zsbuf = zsurf; 1335 pipe->set_framebuffer_state(pipe, &fb_state); 1336 1337 blitter_set_common_draw_rect_state(ctx); 1338 blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height); 1339 blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth, 1340 UTIL_BLITTER_ATTRIB_NONE, NULL); 1341 1342 blitter_restore_vertex_states(ctx); 1343 blitter_restore_fragment_states(ctx); 1344 blitter_restore_fb_state(ctx); 1345 blitter_unset_running_flag(ctx); 1346} 1347 1348void util_blitter_copy_buffer(struct blitter_context *blitter, 1349 struct pipe_resource *dst, 1350 unsigned dstx, 1351 struct pipe_resource *src, 1352 unsigned srcx, 1353 unsigned size) 1354{ 1355 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter; 1356 struct pipe_context *pipe = ctx->base.pipe; 1357 struct pipe_vertex_buffer vb; 1358 struct pipe_stream_output_target *so_target; 1359 1360 if (srcx >= src->width0 || 1361 dstx >= dst->width0) { 1362 return; 1363 } 1364 if (srcx + size > src->width0) { 1365 size = src->width0 - srcx; 1366 } 1367 if (dstx + size > dst->width0) { 1368 size = dst->width0 - dstx; 1369 } 1370 1371 /* Drivers not capable of Stream Out should not call this function 1372 * in the first place. */ 1373 assert(ctx->has_stream_out); 1374 1375 /* Some alignment is required. */ 1376 if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 || 1377 !ctx->has_stream_out) { 1378 struct pipe_box box; 1379 u_box_1d(srcx, size, &box); 1380 util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box); 1381 return; 1382 } 1383 1384 blitter_set_running_flag(ctx); 1385 blitter_check_saved_vertex_states(ctx); 1386 1387 vb.buffer = src; 1388 vb.buffer_offset = srcx; 1389 vb.stride = 4; 1390 1391 pipe->set_vertex_buffers(pipe, 1, &vb); 1392 pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf); 1393 pipe->bind_vs_state(pipe, ctx->vs_pos_only); 1394 if (ctx->has_geometry_shader) 1395 pipe->bind_gs_state(pipe, NULL); 1396 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state); 1397 1398 so_target = pipe->create_stream_output_target(pipe, dst, dstx, size); 1399 pipe->set_stream_output_targets(pipe, 1, &so_target, 0); 1400 1401 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4); 1402 1403 blitter_restore_vertex_states(ctx); 1404 blitter_unset_running_flag(ctx); 1405 pipe_so_target_reference(&so_target, NULL); 1406} 1407