1/********************************************************** 2 * Copyright 2009-2011 VMware, Inc. All rights reserved. 3 * 4 * Permission is hereby granted, free of charge, to any person 5 * obtaining a copy of this software and associated documentation 6 * files (the "Software"), to deal in the Software without 7 * restriction, including without limitation the rights to use, copy, 8 * modify, merge, publish, distribute, sublicense, and/or sell copies 9 * of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be 13 * included in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 * 24 ********************************************************* 25 * Authors: 26 * Zack Rusin <zackr-at-vmware-dot-com> 27 */ 28 29#include "xa_context.h" 30#include "xa_priv.h" 31#include <math.h> 32#include "cso_cache/cso_context.h" 33#include "util/u_inlines.h" 34#include "util/u_sampler.h" 35#include "util/u_draw_quad.h" 36 37#define floatsEqual(x, y) (fabsf(x - y) <= 0.00001f * MIN2(fabsf(x), fabsf(y))) 38#define floatIsZero(x) (floatsEqual((x) + 1.0f, 1.0f)) 39 40#define NUM_COMPONENTS 4 41 42void 43 44 45renderer_set_constants(struct xa_context *r, 46 int shader_type, const float *params, int param_bytes); 47 48static inline boolean 49is_affine(const float *matrix) 50{ 51 return floatIsZero(matrix[2]) && floatIsZero(matrix[5]) 52 && floatsEqual(matrix[8], 1.0f); 53} 54 55static inline void 56map_point(const float *mat, float x, float y, float *out_x, float *out_y) 57{ 58 if (!mat) { 59 *out_x = x; 60 *out_y = y; 61 return; 62 } 63 64 *out_x = mat[0] * x + mat[3] * y + mat[6]; 65 *out_y = mat[1] * x + mat[4] * y + mat[7]; 66 if (!is_affine(mat)) { 67 float w = 1 / (mat[2] * x + mat[5] * y + mat[8]); 68 69 *out_x *= w; 70 *out_y *= w; 71 } 72} 73 74static inline void 75renderer_draw(struct xa_context *r) 76{ 77 int num_verts = r->buffer_size / (r->attrs_per_vertex * NUM_COMPONENTS); 78 79 if (!r->buffer_size) 80 return; 81 82 if (!r->scissor_valid) { 83 r->scissor.minx = 0; 84 r->scissor.miny = 0; 85 r->scissor.maxx = r->dst->tex->width0; 86 r->scissor.maxy = r->dst->tex->height0; 87 } 88 89 r->pipe->set_scissor_states(r->pipe, 0, 1, &r->scissor); 90 91 struct cso_velems_state velems; 92 velems.count = r->attrs_per_vertex; 93 memcpy(velems.velems, r->velems, sizeof(r->velems[0]) * velems.count); 94 95 cso_set_vertex_elements(r->cso, &velems); 96 util_draw_user_vertex_buffer(r->cso, r->buffer, PIPE_PRIM_QUADS, 97 num_verts, /* verts */ 98 r->attrs_per_vertex); /* attribs/vert */ 99 r->buffer_size = 0; 100 101 xa_scissor_reset(r); 102} 103 104static inline void 105renderer_draw_conditional(struct xa_context *r, int next_batch) 106{ 107 if (r->buffer_size + next_batch >= XA_VB_SIZE || 108 (next_batch == 0 && r->buffer_size)) { 109 renderer_draw(r); 110 } 111} 112 113void 114renderer_init_state(struct xa_context *r) 115{ 116 struct pipe_depth_stencil_alpha_state dsa; 117 struct pipe_rasterizer_state raster; 118 unsigned i; 119 120 /* set common initial clip state */ 121 memset(&dsa, 0, sizeof(struct pipe_depth_stencil_alpha_state)); 122 cso_set_depth_stencil_alpha(r->cso, &dsa); 123 124 /* XXX: move to renderer_init_state? */ 125 memset(&raster, 0, sizeof(struct pipe_rasterizer_state)); 126 raster.half_pixel_center = 1; 127 raster.bottom_edge_rule = 1; 128 raster.depth_clip_near = 1; 129 raster.depth_clip_far = 1; 130 raster.scissor = 1; 131 cso_set_rasterizer(r->cso, &raster); 132 133 /* vertex elements state */ 134 memset(&r->velems[0], 0, sizeof(r->velems[0]) * 3); 135 for (i = 0; i < 3; i++) { 136 r->velems[i].src_offset = i * 4 * sizeof(float); 137 r->velems[i].instance_divisor = 0; 138 r->velems[i].vertex_buffer_index = 0; 139 r->velems[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT; 140 } 141} 142 143static inline void 144add_vertex_none(struct xa_context *r, float x, float y) 145{ 146 float *vertex = r->buffer + r->buffer_size; 147 148 vertex[0] = x; 149 vertex[1] = y; 150 vertex[2] = 0.f; /*z */ 151 vertex[3] = 1.f; /*w */ 152 153 r->buffer_size += 4; 154} 155 156static inline void 157add_vertex_1tex(struct xa_context *r, float x, float y, float s, float t) 158{ 159 float *vertex = r->buffer + r->buffer_size; 160 161 vertex[0] = x; 162 vertex[1] = y; 163 vertex[2] = 0.f; /*z */ 164 vertex[3] = 1.f; /*w */ 165 166 vertex[4] = s; /*s */ 167 vertex[5] = t; /*t */ 168 vertex[6] = 0.f; /*r */ 169 vertex[7] = 1.f; /*q */ 170 171 r->buffer_size += 8; 172} 173 174static inline void 175add_vertex_2tex(struct xa_context *r, 176 float x, float y, float s0, float t0, float s1, float t1) 177{ 178 float *vertex = r->buffer + r->buffer_size; 179 180 vertex[0] = x; 181 vertex[1] = y; 182 vertex[2] = 0.f; /*z */ 183 vertex[3] = 1.f; /*w */ 184 185 vertex[4] = s0; /*s */ 186 vertex[5] = t0; /*t */ 187 vertex[6] = 0.f; /*r */ 188 vertex[7] = 1.f; /*q */ 189 190 vertex[8] = s1; /*s */ 191 vertex[9] = t1; /*t */ 192 vertex[10] = 0.f; /*r */ 193 vertex[11] = 1.f; /*q */ 194 195 r->buffer_size += 12; 196} 197 198static void 199compute_src_coords(float sx, float sy, const struct pipe_resource *src, 200 const float *src_matrix, 201 float width, float height, 202 float tc0[2], float tc1[2], float tc2[2], float tc3[2]) 203{ 204 tc0[0] = sx; 205 tc0[1] = sy; 206 tc1[0] = sx + width; 207 tc1[1] = sy; 208 tc2[0] = sx + width; 209 tc2[1] = sy + height; 210 tc3[0] = sx; 211 tc3[1] = sy + height; 212 213 if (src_matrix) { 214 map_point(src_matrix, tc0[0], tc0[1], &tc0[0], &tc0[1]); 215 map_point(src_matrix, tc1[0], tc1[1], &tc1[0], &tc1[1]); 216 map_point(src_matrix, tc2[0], tc2[1], &tc2[0], &tc2[1]); 217 map_point(src_matrix, tc3[0], tc3[1], &tc3[0], &tc3[1]); 218 } 219 220 tc0[0] /= src->width0; 221 tc1[0] /= src->width0; 222 tc2[0] /= src->width0; 223 tc3[0] /= src->width0; 224 tc0[1] /= src->height0; 225 tc1[1] /= src->height0; 226 tc2[1] /= src->height0; 227 tc3[1] /= src->height0; 228} 229 230static void 231add_vertex_data1(struct xa_context *r, 232 float srcX, float srcY, float dstX, float dstY, 233 float width, float height, 234 const struct pipe_resource *src, const float *src_matrix) 235{ 236 float tc0[2], tc1[2], tc2[2], tc3[2]; 237 238 compute_src_coords(srcX, srcY, src, src_matrix, width, height, 239 tc0, tc1, tc2, tc3); 240 /* 1st vertex */ 241 add_vertex_1tex(r, dstX, dstY, tc0[0], tc0[1]); 242 /* 2nd vertex */ 243 add_vertex_1tex(r, dstX + width, dstY, tc1[0], tc1[1]); 244 /* 3rd vertex */ 245 add_vertex_1tex(r, dstX + width, dstY + height, tc2[0], tc2[1]); 246 /* 4th vertex */ 247 add_vertex_1tex(r, dstX, dstY + height, tc3[0], tc3[1]); 248} 249 250static void 251add_vertex_data2(struct xa_context *r, 252 float srcX, float srcY, float maskX, float maskY, 253 float dstX, float dstY, float width, float height, 254 struct pipe_resource *src, 255 struct pipe_resource *mask, 256 const float *src_matrix, const float *mask_matrix) 257{ 258 float spt0[2], spt1[2], spt2[2], spt3[2]; 259 float mpt0[2], mpt1[2], mpt2[2], mpt3[2]; 260 261 compute_src_coords(srcX, srcY, src, src_matrix, width, height, 262 spt0, spt1, spt2, spt3); 263 compute_src_coords(maskX, maskY, mask, mask_matrix, width, height, 264 mpt0, mpt1, mpt2, mpt3); 265 266 /* 1st vertex */ 267 add_vertex_2tex(r, dstX, dstY, 268 spt0[0], spt0[1], mpt0[0], mpt0[1]); 269 /* 2nd vertex */ 270 add_vertex_2tex(r, dstX + width, dstY, 271 spt1[0], spt1[1], mpt1[0], mpt1[1]); 272 /* 3rd vertex */ 273 add_vertex_2tex(r, dstX + width, dstY + height, 274 spt2[0], spt2[1], mpt2[0], mpt2[1]); 275 /* 4th vertex */ 276 add_vertex_2tex(r, dstX, dstY + height, 277 spt3[0], spt3[1], mpt3[0], mpt3[1]); 278} 279 280static void 281setup_vertex_data_yuv(struct xa_context *r, 282 float srcX, 283 float srcY, 284 float srcW, 285 float srcH, 286 float dstX, 287 float dstY, 288 float dstW, float dstH, struct xa_surface *srf[]) 289{ 290 float s0, t0, s1, t1; 291 float spt0[2], spt1[2]; 292 struct pipe_resource *tex; 293 294 spt0[0] = srcX; 295 spt0[1] = srcY; 296 spt1[0] = srcX + srcW; 297 spt1[1] = srcY + srcH; 298 299 tex = srf[0]->tex; 300 s0 = spt0[0] / tex->width0; 301 t0 = spt0[1] / tex->height0; 302 s1 = spt1[0] / tex->width0; 303 t1 = spt1[1] / tex->height0; 304 305 /* 1st vertex */ 306 add_vertex_1tex(r, dstX, dstY, s0, t0); 307 /* 2nd vertex */ 308 add_vertex_1tex(r, dstX + dstW, dstY, s1, t0); 309 /* 3rd vertex */ 310 add_vertex_1tex(r, dstX + dstW, dstY + dstH, s1, t1); 311 /* 4th vertex */ 312 add_vertex_1tex(r, dstX, dstY + dstH, s0, t1); 313} 314 315/* Set up framebuffer, viewport and vertex shader constant buffer 316 * state for a particular destinaton surface. In all our rendering, 317 * these concepts are linked. 318 */ 319void 320renderer_bind_destination(struct xa_context *r, 321 struct pipe_surface *surface) 322{ 323 int width = surface->width; 324 int height = surface->height; 325 326 struct pipe_framebuffer_state fb; 327 struct pipe_viewport_state viewport; 328 329 xa_scissor_reset(r); 330 331 /* Framebuffer uses actual surface width/height 332 */ 333 memset(&fb, 0, sizeof fb); 334 fb.width = surface->width; 335 fb.height = surface->height; 336 fb.nr_cbufs = 1; 337 fb.cbufs[0] = surface; 338 fb.zsbuf = NULL; 339 340 /* Viewport just touches the bit we're interested in: 341 */ 342 viewport.scale[0] = width / 2.f; 343 viewport.scale[1] = height / 2.f; 344 viewport.scale[2] = 1.0; 345 viewport.translate[0] = width / 2.f; 346 viewport.translate[1] = height / 2.f; 347 viewport.translate[2] = 0.0; 348 viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X; 349 viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y; 350 viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z; 351 viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W; 352 353 /* Constant buffer set up to match viewport dimensions: 354 */ 355 if (r->fb_width != width || r->fb_height != height) { 356 float vs_consts[8] = { 357 2.f / width, 2.f / height, 1, 1, 358 -1, -1, 0, 0 359 }; 360 361 r->fb_width = width; 362 r->fb_height = height; 363 364 renderer_set_constants(r, PIPE_SHADER_VERTEX, 365 vs_consts, sizeof vs_consts); 366 } 367 368 cso_set_framebuffer(r->cso, &fb); 369 cso_set_viewport(r->cso, &viewport); 370} 371 372void 373renderer_set_constants(struct xa_context *r, 374 int shader_type, const float *params, int param_bytes) 375{ 376 struct pipe_resource **cbuf = 377 (shader_type == PIPE_SHADER_VERTEX) ? &r->vs_const_buffer : 378 &r->fs_const_buffer; 379 380 pipe_resource_reference(cbuf, NULL); 381 *cbuf = pipe_buffer_create_const0(r->pipe->screen, 382 PIPE_BIND_CONSTANT_BUFFER, 383 PIPE_USAGE_DEFAULT, 384 param_bytes); 385 386 if (*cbuf) { 387 pipe_buffer_write(r->pipe, *cbuf, 0, param_bytes, params); 388 } 389 pipe_set_constant_buffer(r->pipe, shader_type, 0, *cbuf); 390} 391 392void 393renderer_copy_prepare(struct xa_context *r, 394 struct pipe_surface *dst_surface, 395 struct pipe_resource *src_texture, 396 const enum xa_formats src_xa_format, 397 const enum xa_formats dst_xa_format) 398{ 399 struct pipe_context *pipe = r->pipe; 400 struct pipe_screen *screen = pipe->screen; 401 struct xa_shader shader; 402 uint32_t fs_traits = FS_COMPOSITE; 403 404 assert(screen->is_format_supported(screen, dst_surface->format, 405 PIPE_TEXTURE_2D, 0, 0, 406 PIPE_BIND_RENDER_TARGET)); 407 (void)screen; 408 409 renderer_bind_destination(r, dst_surface); 410 411 /* set misc state we care about */ 412 { 413 struct pipe_blend_state blend; 414 415 memset(&blend, 0, sizeof(blend)); 416 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE; 417 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE; 418 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO; 419 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO; 420 blend.rt[0].colormask = PIPE_MASK_RGBA; 421 cso_set_blend(r->cso, &blend); 422 } 423 424 /* sampler */ 425 { 426 struct pipe_sampler_state sampler; 427 const struct pipe_sampler_state *p_sampler = &sampler; 428 429 memset(&sampler, 0, sizeof(sampler)); 430 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 431 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 432 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 433 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 434 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 435 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 436 sampler.normalized_coords = 1; 437 cso_set_samplers(r->cso, PIPE_SHADER_FRAGMENT, 1, &p_sampler); 438 r->num_bound_samplers = 1; 439 } 440 441 /* texture/sampler view */ 442 { 443 struct pipe_sampler_view templ; 444 struct pipe_sampler_view *src_view; 445 446 u_sampler_view_default_template(&templ, 447 src_texture, src_texture->format); 448 src_view = pipe->create_sampler_view(pipe, src_texture, &templ); 449 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &src_view); 450 pipe_sampler_view_reference(&src_view, NULL); 451 } 452 453 /* shaders */ 454 if (src_texture->format == PIPE_FORMAT_L8_UNORM || 455 src_texture->format == PIPE_FORMAT_R8_UNORM) 456 fs_traits |= FS_SRC_LUMINANCE; 457 if (dst_surface->format == PIPE_FORMAT_L8_UNORM || 458 dst_surface->format == PIPE_FORMAT_R8_UNORM) 459 fs_traits |= FS_DST_LUMINANCE; 460 if (xa_format_a(dst_xa_format) != 0 && 461 xa_format_a(src_xa_format) == 0) 462 fs_traits |= FS_SRC_SET_ALPHA; 463 464 shader = xa_shaders_get(r->shaders, VS_COMPOSITE, fs_traits); 465 cso_set_vertex_shader_handle(r->cso, shader.vs); 466 cso_set_fragment_shader_handle(r->cso, shader.fs); 467 468 r->buffer_size = 0; 469 r->attrs_per_vertex = 2; 470} 471 472void 473renderer_copy(struct xa_context *r, 474 int dx, 475 int dy, 476 int sx, 477 int sy, 478 int width, int height, float src_width, float src_height) 479{ 480 float s0, t0, s1, t1; 481 float x0, y0, x1, y1; 482 483 /* XXX: could put the texcoord scaling calculation into the vertex 484 * shader. 485 */ 486 s0 = sx / src_width; 487 s1 = (sx + width) / src_width; 488 t0 = sy / src_height; 489 t1 = (sy + height) / src_height; 490 491 x0 = dx; 492 x1 = dx + width; 493 y0 = dy; 494 y1 = dy + height; 495 496 /* draw quad */ 497 renderer_draw_conditional(r, 4 * 8); 498 add_vertex_1tex(r, x0, y0, s0, t0); 499 add_vertex_1tex(r, x1, y0, s1, t0); 500 add_vertex_1tex(r, x1, y1, s1, t1); 501 add_vertex_1tex(r, x0, y1, s0, t1); 502} 503 504void 505renderer_draw_yuv(struct xa_context *r, 506 float src_x, 507 float src_y, 508 float src_w, 509 float src_h, 510 int dst_x, 511 int dst_y, int dst_w, int dst_h, struct xa_surface *srf[]) 512{ 513 const int num_attribs = 2; /*pos + tex coord */ 514 515 setup_vertex_data_yuv(r, 516 src_x, src_y, src_w, src_h, 517 dst_x, dst_y, dst_w, dst_h, srf); 518 519 if (!r->scissor_valid) { 520 r->scissor.minx = 0; 521 r->scissor.miny = 0; 522 r->scissor.maxx = r->dst->tex->width0; 523 r->scissor.maxy = r->dst->tex->height0; 524 } 525 526 r->pipe->set_scissor_states(r->pipe, 0, 1, &r->scissor); 527 528 struct cso_velems_state velems; 529 velems.count = num_attribs; 530 memcpy(velems.velems, r->velems, sizeof(r->velems[0]) * velems.count); 531 532 cso_set_vertex_elements(r->cso, &velems); 533 util_draw_user_vertex_buffer(r->cso, r->buffer, PIPE_PRIM_QUADS, 534 4, /* verts */ 535 num_attribs); /* attribs/vert */ 536 r->buffer_size = 0; 537 538 xa_scissor_reset(r); 539} 540 541void 542renderer_begin_solid(struct xa_context *r) 543{ 544 r->buffer_size = 0; 545 r->attrs_per_vertex = 1; 546 renderer_set_constants(r, PIPE_SHADER_FRAGMENT, r->solid_color, 547 4 * sizeof(float)); 548} 549 550void 551renderer_solid(struct xa_context *r, 552 int x0, int y0, int x1, int y1) 553{ 554 /* 555 * debug_printf("solid rect[(%d, %d), (%d, %d)], rgba[%f, %f, %f, %f]\n", 556 * x0, y0, x1, y1, color[0], color[1], color[2], color[3]); */ 557 558 renderer_draw_conditional(r, 4 * 4); 559 560 /* 1st vertex */ 561 add_vertex_none(r, x0, y0); 562 /* 2nd vertex */ 563 add_vertex_none(r, x1, y0); 564 /* 3rd vertex */ 565 add_vertex_none(r, x1, y1); 566 /* 4th vertex */ 567 add_vertex_none(r, x0, y1); 568} 569 570void 571renderer_draw_flush(struct xa_context *r) 572{ 573 renderer_draw_conditional(r, 0); 574} 575 576void 577renderer_begin_textures(struct xa_context *r) 578{ 579 r->attrs_per_vertex = 1 + r->num_bound_samplers; 580 r->buffer_size = 0; 581 if (r->has_solid_src || r->has_solid_mask) 582 renderer_set_constants(r, PIPE_SHADER_FRAGMENT, r->solid_color, 583 4 * sizeof(float)); 584} 585 586void 587renderer_texture(struct xa_context *r, 588 int *pos, 589 int width, int height, 590 const float *src_matrix, 591 const float *mask_matrix) 592{ 593 struct pipe_sampler_view **sampler_view = r->bound_sampler_views; 594 595#if 0 596 if (src_matrix) { 597 debug_printf("src_matrix = \n"); 598 debug_printf("%f, %f, %f\n", src_matrix[0], src_matrix[1], src_matrix[2]); 599 debug_printf("%f, %f, %f\n", src_matrix[3], src_matrix[4], src_matrix[5]); 600 debug_printf("%f, %f, %f\n", src_matrix[6], src_matrix[7], src_matrix[8]); 601 } 602 if (mask_matrix) { 603 debug_printf("mask_matrix = \n"); 604 debug_printf("%f, %f, %f\n", mask_matrix[0], mask_matrix[1], mask_matrix[2]); 605 debug_printf("%f, %f, %f\n", mask_matrix[3], mask_matrix[4], mask_matrix[5]); 606 debug_printf("%f, %f, %f\n", mask_matrix[6], mask_matrix[7], mask_matrix[8]); 607 } 608#endif 609 610 switch(r->attrs_per_vertex) { 611 case 2: 612 renderer_draw_conditional(r, 4 * 8); 613 if (!r->has_solid_src) { 614 add_vertex_data1(r, 615 pos[0], pos[1], /* src */ 616 pos[4], pos[5], /* dst */ 617 width, height, 618 sampler_view[0]->texture, src_matrix); 619 } else { 620 add_vertex_data1(r, 621 pos[2], pos[3], /* mask */ 622 pos[4], pos[5], /* dst */ 623 width, height, 624 sampler_view[0]->texture, mask_matrix); 625 } 626 break; 627 case 3: 628 renderer_draw_conditional(r, 4 * 12); 629 add_vertex_data2(r, 630 pos[0], pos[1], /* src */ 631 pos[2], pos[3], /* mask */ 632 pos[4], pos[5], /* dst */ 633 width, height, 634 sampler_view[0]->texture, sampler_view[1]->texture, 635 src_matrix, mask_matrix); 636 break; 637 default: 638 break; 639 } 640} 641