vl_idct.c revision e742a1043dbd56fe11f0490cb74b7a738bab2238
1/************************************************************************** 2 * 3 * Copyright 2010 Christian König 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28#include "vl_idct.h" 29#include "util/u_draw.h" 30#include <assert.h> 31#include <pipe/p_context.h> 32#include <pipe/p_screen.h> 33#include <util/u_inlines.h> 34#include <util/u_sampler.h> 35#include <util/u_format.h> 36#include <tgsi/tgsi_ureg.h> 37#include "vl_types.h" 38 39#define BLOCK_WIDTH 8 40#define BLOCK_HEIGHT 8 41 42#define SCALE_FACTOR_16_TO_9 (32768.0f / 256.0f) 43 44#define STAGE1_SCALE 4.0f 45#define STAGE2_SCALE (SCALE_FACTOR_16_TO_9 / STAGE1_SCALE) 46 47struct vertex_shader_consts 48{ 49 struct vertex4f norm; 50}; 51 52enum VS_INPUT 53{ 54 VS_I_RECT, 55 VS_I_VPOS, 56 57 NUM_VS_INPUTS 58}; 59 60enum VS_OUTPUT 61{ 62 VS_O_VPOS, 63 VS_O_BLOCK, 64 VS_O_TEX, 65 VS_O_START 66}; 67 68static const float const_matrix[8][8] = { 69 { 0.3535530f, 0.3535530f, 0.3535530f, 0.3535530f, 0.3535530f, 0.3535530f, 0.353553f, 0.3535530f }, 70 { 0.4903930f, 0.4157350f, 0.2777850f, 0.0975451f, -0.0975452f, -0.2777850f, -0.415735f, -0.4903930f }, 71 { 0.4619400f, 0.1913420f, -0.1913420f, -0.4619400f, -0.4619400f, -0.1913420f, 0.191342f, 0.4619400f }, 72 { 0.4157350f, -0.0975452f, -0.4903930f, -0.2777850f, 0.2777850f, 0.4903930f, 0.097545f, -0.4157350f }, 73 { 0.3535530f, -0.3535530f, -0.3535530f, 0.3535540f, 0.3535530f, -0.3535540f, -0.353553f, 0.3535530f }, 74 { 0.2777850f, -0.4903930f, 0.0975452f, 0.4157350f, -0.4157350f, -0.0975451f, 0.490393f, -0.2777850f }, 75 { 0.1913420f, -0.4619400f, 0.4619400f, -0.1913420f, -0.1913410f, 0.4619400f, -0.461940f, 0.1913420f }, 76 { 0.0975451f, -0.2777850f, 0.4157350f, -0.4903930f, 0.4903930f, -0.4157350f, 0.277786f, -0.0975458f } 77}; 78 79/* vertices for a quad covering a block */ 80static const struct vertex2f const_quad[4] = { 81 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f} 82}; 83 84static void * 85create_vert_shader(struct vl_idct *idct, bool calc_src_cords) 86{ 87 struct ureg_program *shader; 88 struct ureg_src scale; 89 struct ureg_src vrect, vpos; 90 struct ureg_dst t_vpos; 91 struct ureg_dst o_vpos, o_block, o_tex, o_start; 92 93 shader = ureg_create(TGSI_PROCESSOR_VERTEX); 94 if (!shader) 95 return NULL; 96 97 t_vpos = ureg_DECL_temporary(shader); 98 99 vrect = ureg_DECL_vs_input(shader, VS_I_RECT); 100 vpos = ureg_DECL_vs_input(shader, VS_I_VPOS); 101 102 o_vpos = ureg_DECL_output(shader, TGSI_SEMANTIC_POSITION, VS_O_VPOS); 103 104 /* 105 * scale = (BLOCK_WIDTH, BLOCK_HEIGHT) / (dst.width, dst.height) 106 * 107 * t_vpos = vpos + vrect 108 * o_vpos.xy = t_vpos * scale 109 * o_vpos.zw = vpos 110 * 111 * o_block = vrect 112 * o_tex = t_pos 113 * o_start = vpos * scale 114 * 115 */ 116 scale = ureg_imm2f(shader, 117 (float)BLOCK_WIDTH / idct->destination->width0, 118 (float)BLOCK_HEIGHT / idct->destination->height0); 119 120 ureg_ADD(shader, ureg_writemask(t_vpos, TGSI_WRITEMASK_XY), vpos, vrect); 121 ureg_MUL(shader, ureg_writemask(t_vpos, TGSI_WRITEMASK_XY), ureg_src(t_vpos), scale); 122 ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_XY), ureg_src(t_vpos)); 123 ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_ZW), vpos); 124 125 if(calc_src_cords) { 126 o_block = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK); 127 o_tex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX); 128 o_start = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_START); 129 130 ureg_MOV(shader, ureg_writemask(o_block, TGSI_WRITEMASK_XY), vrect); 131 ureg_MOV(shader, ureg_writemask(o_tex, TGSI_WRITEMASK_XY), ureg_src(t_vpos)); 132 ureg_MUL(shader, ureg_writemask(o_start, TGSI_WRITEMASK_XY), vpos, scale); 133 } 134 135 ureg_release_temporary(shader, t_vpos); 136 137 ureg_END(shader); 138 139 return ureg_create_shader_and_destroy(shader, idct->pipe); 140} 141 142static void 143fetch_one(struct ureg_program *shader, struct ureg_dst m[2], 144 struct ureg_src tc, struct ureg_src sampler, 145 struct ureg_src start, struct ureg_src block, float height) 146{ 147 struct ureg_dst t_tc, tmp; 148 unsigned i, j; 149 150 t_tc = ureg_DECL_temporary(shader); 151 tmp = ureg_DECL_temporary(shader); 152 153 m[0] = ureg_DECL_temporary(shader); 154 m[1] = ureg_DECL_temporary(shader); 155 156 /* 157 * t_tc.x = right_side ? start.x : tc.x 158 * t_tc.y = right_side ? tc.y : start.y 159 * m[0..1].xyzw = tex(t_tc++, sampler) 160 */ 161 ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_X), ureg_scalar(tc, TGSI_SWIZZLE_X)); 162 ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_Y), ureg_scalar(start, TGSI_SWIZZLE_Y)); 163 ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_Z), ureg_scalar(block, TGSI_SWIZZLE_X)); 164 165 for(i = 0; i < 2; ++i) { 166 for(j = 0; j < 4; ++j) { 167 /* Nouveau and r600g can't writemask tex dst regs (yet?), do in two steps */ 168 ureg_TEX(shader, tmp, TGSI_TEXTURE_3D, ureg_src(t_tc), sampler); 169 ureg_MOV(shader, ureg_writemask(m[i], TGSI_WRITEMASK_X << j), ureg_scalar(ureg_src(tmp), TGSI_SWIZZLE_X)); 170 171 if(i != 1 || j != 3) /* skip the last add */ 172 ureg_ADD(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_Y), 173 ureg_src(t_tc), ureg_imm1f(shader, 1.0f / height)); 174 } 175 } 176 177 ureg_release_temporary(shader, t_tc); 178 ureg_release_temporary(shader, tmp); 179} 180 181static void 182fetch_four(struct ureg_program *shader, struct ureg_dst m[2], 183 struct ureg_src tc, struct ureg_src sampler, 184 struct ureg_src start, bool right_side, float width) 185{ 186 struct ureg_dst t_tc; 187 188 t_tc = ureg_DECL_temporary(shader); 189 m[0] = ureg_DECL_temporary(shader); 190 m[1] = ureg_DECL_temporary(shader); 191 192 /* 193 * t_tc.x = right_side ? start.x : tc.x 194 * t_tc.y = right_side ? tc.y : start.y 195 * m[0..1] = tex(t_tc++, sampler) 196 */ 197 if(right_side) { 198 ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_X), ureg_scalar(start, TGSI_SWIZZLE_Y)); 199 ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_Y), ureg_scalar(tc, TGSI_SWIZZLE_X)); 200 } else { 201 ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_X), ureg_scalar(start, TGSI_SWIZZLE_X)); 202 ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_Y), ureg_scalar(tc, TGSI_SWIZZLE_Y)); 203 } 204 205 ureg_TEX(shader, m[0], TGSI_TEXTURE_2D, ureg_src(t_tc), sampler); 206 ureg_ADD(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_X), ureg_src(t_tc), ureg_imm1f(shader, 1.0f / width)); 207 ureg_TEX(shader, m[1], TGSI_TEXTURE_2D, ureg_src(t_tc), sampler); 208 209 ureg_release_temporary(shader, t_tc); 210} 211 212static void 213matrix_mul(struct ureg_program *shader, struct ureg_dst dst, struct ureg_dst l[2], struct ureg_dst r[2]) 214{ 215 struct ureg_dst tmp[2]; 216 unsigned i; 217 218 for(i = 0; i < 2; ++i) { 219 tmp[i] = ureg_DECL_temporary(shader); 220 } 221 222 /* 223 * tmp[0..1] = dot4(m[0][0..1], m[1][0..1]) 224 * dst = tmp[0] + tmp[1] 225 */ 226 ureg_DP4(shader, ureg_writemask(tmp[0], TGSI_WRITEMASK_X), ureg_src(l[0]), ureg_src(r[0])); 227 ureg_DP4(shader, ureg_writemask(tmp[1], TGSI_WRITEMASK_X), ureg_src(l[1]), ureg_src(r[1])); 228 ureg_ADD(shader, dst, ureg_src(tmp[0]), ureg_src(tmp[1])); 229 230 for(i = 0; i < 2; ++i) { 231 ureg_release_temporary(shader, tmp[i]); 232 } 233} 234 235static void * 236create_transpose_frag_shader(struct vl_idct *idct) 237{ 238 struct pipe_resource *transpose = idct->textures.individual.transpose; 239 struct pipe_resource *intermediate = idct->textures.individual.intermediate; 240 241 struct ureg_program *shader; 242 243 struct ureg_src block, tex, sampler[2]; 244 struct ureg_src start[2]; 245 246 struct ureg_dst m[2][2]; 247 struct ureg_dst tmp, fragment; 248 249 shader = ureg_create(TGSI_PROCESSOR_FRAGMENT); 250 if (!shader) 251 return NULL; 252 253 block = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK, TGSI_INTERPOLATE_LINEAR); 254 tex = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX, TGSI_INTERPOLATE_CONSTANT); 255 256 sampler[0] = ureg_DECL_sampler(shader, 0); 257 sampler[1] = ureg_DECL_sampler(shader, 1); 258 259 start[0] = ureg_imm1f(shader, 0.0f); 260 start[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_START, TGSI_INTERPOLATE_CONSTANT); 261 262 fetch_four(shader, m[0], block, sampler[0], start[0], false, transpose->width0); 263 fetch_one(shader, m[1], tex, sampler[1], start[1], block, intermediate->height0); 264 265 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0); 266 267 tmp = ureg_DECL_temporary(shader); 268 matrix_mul(shader, ureg_writemask(tmp, TGSI_WRITEMASK_X), m[0], m[1]); 269 ureg_MUL(shader, fragment, ureg_src(tmp), ureg_imm1f(shader, STAGE2_SCALE)); 270 271 ureg_release_temporary(shader, tmp); 272 ureg_release_temporary(shader, m[0][0]); 273 ureg_release_temporary(shader, m[0][1]); 274 ureg_release_temporary(shader, m[1][0]); 275 ureg_release_temporary(shader, m[1][1]); 276 277 ureg_END(shader); 278 279 return ureg_create_shader_and_destroy(shader, idct->pipe); 280} 281 282static void * 283create_matrix_frag_shader(struct vl_idct *idct) 284{ 285 struct pipe_resource *matrix = idct->textures.individual.matrix; 286 struct pipe_resource *source = idct->textures.individual.source; 287 288 struct ureg_program *shader; 289 290 struct ureg_src tc[2], sampler[2]; 291 struct ureg_src start[2]; 292 293 struct ureg_dst l[2], r[2]; 294 struct ureg_dst t_tc, tmp, fragment[BLOCK_WIDTH]; 295 296 unsigned i; 297 298 shader = ureg_create(TGSI_PROCESSOR_FRAGMENT); 299 if (!shader) 300 return NULL; 301 302 t_tc = ureg_DECL_temporary(shader); 303 tmp = ureg_DECL_temporary(shader); 304 305 tc[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX, TGSI_INTERPOLATE_LINEAR); 306 tc[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK, TGSI_INTERPOLATE_LINEAR); 307 308 sampler[0] = ureg_DECL_sampler(shader, 1); 309 sampler[1] = ureg_DECL_sampler(shader, 0); 310 311 start[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_START, TGSI_INTERPOLATE_CONSTANT); 312 start[1] = ureg_imm1f(shader, 0.0f); 313 314 for (i = 0; i < BLOCK_WIDTH; ++i) 315 fragment[i] = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, i); 316 317 fetch_four(shader, l, tc[0], sampler[0], start[0], false, source->width0); 318 ureg_MUL(shader, l[0], ureg_src(l[0]), ureg_scalar(ureg_imm1f(shader, STAGE1_SCALE), TGSI_SWIZZLE_X)); 319 ureg_MUL(shader, l[1], ureg_src(l[1]), ureg_scalar(ureg_imm1f(shader, STAGE1_SCALE), TGSI_SWIZZLE_X)); 320 321 for (i = 0; i < BLOCK_WIDTH; ++i) { 322 ureg_MOV(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_X), ureg_imm1f(shader, 1.0f / BLOCK_WIDTH * i)); 323 fetch_four(shader, r, ureg_src(t_tc), sampler[1], start[1], true, matrix->width0); 324 matrix_mul(shader, fragment[i], l, r); 325 ureg_release_temporary(shader, r[0]); 326 ureg_release_temporary(shader, r[1]); 327 } 328 329 ureg_release_temporary(shader, t_tc); 330 ureg_release_temporary(shader, tmp); 331 ureg_release_temporary(shader, l[0]); 332 ureg_release_temporary(shader, l[1]); 333 334 ureg_END(shader); 335 336 return ureg_create_shader_and_destroy(shader, idct->pipe); 337} 338 339static void * 340create_empty_block_frag_shader(struct vl_idct *idct) 341{ 342 struct ureg_program *shader; 343 struct ureg_dst fragment; 344 345 shader = ureg_create(TGSI_PROCESSOR_FRAGMENT); 346 if (!shader) 347 return NULL; 348 349 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0); 350 351 ureg_MOV(shader, fragment, ureg_imm1f(shader, 0.0f)); 352 353 ureg_END(shader); 354 355 return ureg_create_shader_and_destroy(shader, idct->pipe); 356} 357 358static void 359xfer_buffers_map(struct vl_idct *idct) 360{ 361 struct pipe_box rect = 362 { 363 0, 0, 0, 364 idct->textures.individual.source->width0, 365 idct->textures.individual.source->height0, 366 1 367 }; 368 369 idct->tex_transfer = idct->pipe->get_transfer 370 ( 371 idct->pipe, idct->textures.individual.source, 372 u_subresource(0, 0), 373 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 374 &rect 375 ); 376 377 idct->texels = idct->pipe->transfer_map(idct->pipe, idct->tex_transfer); 378 379 idct->vectors = pipe_buffer_map 380 ( 381 idct->pipe, 382 idct->vertex_bufs.individual.pos.buffer, 383 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 384 &idct->vec_transfer 385 ); 386} 387 388static void 389xfer_buffers_unmap(struct vl_idct *idct) 390{ 391 pipe_buffer_unmap(idct->pipe, idct->vertex_bufs.individual.pos.buffer, idct->vec_transfer); 392 393 idct->pipe->transfer_unmap(idct->pipe, idct->tex_transfer); 394 idct->pipe->transfer_destroy(idct->pipe, idct->tex_transfer); 395} 396 397static bool 398init_shaders(struct vl_idct *idct) 399{ 400 idct->matrix_vs = create_vert_shader(idct, true); 401 idct->matrix_fs = create_matrix_frag_shader(idct); 402 403 idct->transpose_vs = create_vert_shader(idct, true); 404 idct->transpose_fs = create_transpose_frag_shader(idct); 405 406 idct->eb_vs = create_vert_shader(idct, false); 407 idct->eb_fs = create_empty_block_frag_shader(idct); 408 409 return 410 idct->transpose_vs != NULL && idct->transpose_fs != NULL && 411 idct->matrix_vs != NULL && idct->matrix_fs != NULL && 412 idct->eb_vs != NULL && idct->eb_fs != NULL; 413} 414 415static void 416cleanup_shaders(struct vl_idct *idct) 417{ 418 idct->pipe->delete_vs_state(idct->pipe, idct->transpose_vs); 419 idct->pipe->delete_fs_state(idct->pipe, idct->transpose_fs); 420 421 idct->pipe->delete_vs_state(idct->pipe, idct->matrix_vs); 422 idct->pipe->delete_fs_state(idct->pipe, idct->matrix_fs); 423 424 idct->pipe->delete_vs_state(idct->pipe, idct->eb_vs); 425 idct->pipe->delete_fs_state(idct->pipe, idct->eb_fs); 426} 427 428static bool 429init_buffers(struct vl_idct *idct) 430{ 431 struct pipe_resource template; 432 struct pipe_sampler_view sampler_view; 433 struct pipe_vertex_element vertex_elems[2]; 434 unsigned i; 435 436 idct->max_blocks = 437 align(idct->destination->width0, BLOCK_WIDTH) / BLOCK_WIDTH * 438 align(idct->destination->height0, BLOCK_HEIGHT) / BLOCK_HEIGHT * 439 idct->destination->depth0; 440 441 memset(&template, 0, sizeof(struct pipe_resource)); 442 template.last_level = 0; 443 template.depth0 = 1; 444 template.bind = PIPE_BIND_SAMPLER_VIEW; 445 template.flags = 0; 446 447 template.target = PIPE_TEXTURE_2D; 448 template.format = PIPE_FORMAT_R16G16B16A16_SNORM; 449 template.width0 = idct->destination->width0 / 4; 450 template.height0 = idct->destination->height0; 451 template.depth0 = 1; 452 template.usage = PIPE_USAGE_STREAM; 453 idct->textures.individual.source = idct->pipe->screen->resource_create(idct->pipe->screen, &template); 454 455 template.target = PIPE_TEXTURE_3D; 456 template.format = PIPE_FORMAT_R16_SNORM; 457 template.width0 = idct->destination->width0 / 8; 458 template.depth0 = 8; 459 template.usage = PIPE_USAGE_STATIC; 460 idct->textures.individual.intermediate = idct->pipe->screen->resource_create(idct->pipe->screen, &template); 461 462 for (i = 0; i < 4; ++i) { 463 if(idct->textures.all[i] == NULL) 464 return false; /* a texture failed to allocate */ 465 466 u_sampler_view_default_template(&sampler_view, idct->textures.all[i], idct->textures.all[i]->format); 467 idct->sampler_views.all[i] = idct->pipe->create_sampler_view(idct->pipe, idct->textures.all[i], &sampler_view); 468 } 469 470 idct->vertex_bufs.individual.quad.stride = sizeof(struct vertex2f); 471 idct->vertex_bufs.individual.quad.max_index = 4 * idct->max_blocks - 1; 472 idct->vertex_bufs.individual.quad.buffer_offset = 0; 473 idct->vertex_bufs.individual.quad.buffer = pipe_buffer_create 474 ( 475 idct->pipe->screen, 476 PIPE_BIND_VERTEX_BUFFER, 477 sizeof(struct vertex2f) * 4 * idct->max_blocks 478 ); 479 480 if(idct->vertex_bufs.individual.quad.buffer == NULL) 481 return false; 482 483 idct->vertex_bufs.individual.pos.stride = sizeof(struct vertex2f); 484 idct->vertex_bufs.individual.pos.max_index = 4 * idct->max_blocks - 1; 485 idct->vertex_bufs.individual.pos.buffer_offset = 0; 486 idct->vertex_bufs.individual.pos.buffer = pipe_buffer_create 487 ( 488 idct->pipe->screen, 489 PIPE_BIND_VERTEX_BUFFER, 490 sizeof(struct vertex2f) * 4 * idct->max_blocks 491 ); 492 493 if(idct->vertex_bufs.individual.pos.buffer == NULL) 494 return false; 495 496 /* Rect element */ 497 vertex_elems[0].src_offset = 0; 498 vertex_elems[0].instance_divisor = 0; 499 vertex_elems[0].vertex_buffer_index = 0; 500 vertex_elems[0].src_format = PIPE_FORMAT_R32G32_FLOAT; 501 502 /* Pos element */ 503 vertex_elems[1].src_offset = 0; 504 vertex_elems[1].instance_divisor = 0; 505 vertex_elems[1].vertex_buffer_index = 1; 506 vertex_elems[1].src_format = PIPE_FORMAT_R32G32_FLOAT; 507 508 idct->vertex_elems_state = idct->pipe->create_vertex_elements_state(idct->pipe, 2, vertex_elems); 509 510 return true; 511} 512 513static void 514cleanup_buffers(struct vl_idct *idct) 515{ 516 unsigned i; 517 518 assert(idct); 519 520 for (i = 0; i < 4; ++i) { 521 pipe_sampler_view_reference(&idct->sampler_views.all[i], NULL); 522 pipe_resource_reference(&idct->textures.all[i], NULL); 523 } 524 525 idct->pipe->delete_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 526 pipe_resource_reference(&idct->vertex_bufs.individual.quad.buffer, NULL); 527 pipe_resource_reference(&idct->vertex_bufs.individual.pos.buffer, NULL); 528} 529 530static void 531init_constants(struct vl_idct *idct) 532{ 533 struct pipe_transfer *buf_transfer; 534 struct vertex2f *v; 535 536 unsigned i; 537 538 /* quad vectors */ 539 v = pipe_buffer_map 540 ( 541 idct->pipe, 542 idct->vertex_bufs.individual.quad.buffer, 543 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 544 &buf_transfer 545 ); 546 for ( i = 0; i < idct->max_blocks; ++i) 547 memcpy(v + i * 4, &const_quad, sizeof(const_quad)); 548 pipe_buffer_unmap(idct->pipe, idct->vertex_bufs.individual.quad.buffer, buf_transfer); 549} 550 551static void 552init_state(struct vl_idct *idct) 553{ 554 struct pipe_sampler_state sampler; 555 unsigned i; 556 557 idct->num_blocks = 0; 558 idct->num_empty_blocks = 0; 559 560 idct->viewport[0].scale[0] = idct->textures.individual.intermediate->width0; 561 idct->viewport[0].scale[1] = idct->textures.individual.intermediate->height0; 562 563 idct->viewport[1].scale[0] = idct->destination->width0; 564 idct->viewport[1].scale[1] = idct->destination->height0; 565 566 idct->fb_state[0].width = idct->textures.individual.intermediate->width0; 567 idct->fb_state[0].height = idct->textures.individual.intermediate->height0; 568 569 idct->fb_state[0].nr_cbufs = 8; 570 for(i = 0; i < 8; ++i) { 571 idct->fb_state[0].cbufs[i] = idct->pipe->screen->get_tex_surface( 572 idct->pipe->screen, idct->textures.individual.intermediate, 0, 0, i, 573 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET); 574 } 575 576 idct->fb_state[1].width = idct->destination->width0; 577 idct->fb_state[1].height = idct->destination->height0; 578 579 idct->fb_state[1].nr_cbufs = 1; 580 idct->fb_state[1].cbufs[0] = idct->pipe->screen->get_tex_surface( 581 idct->pipe->screen, idct->destination, 0, 0, 0, 582 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET); 583 584 for(i = 0; i < 2; ++i) { 585 idct->viewport[i].scale[2] = 1; 586 idct->viewport[i].scale[3] = 1; 587 idct->viewport[i].translate[0] = 0; 588 idct->viewport[i].translate[1] = 0; 589 idct->viewport[i].translate[2] = 0; 590 idct->viewport[i].translate[3] = 0; 591 592 idct->fb_state[i].zsbuf = NULL; 593 } 594 595 for (i = 0; i < 4; ++i) { 596 memset(&sampler, 0, sizeof(sampler)); 597 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 598 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 599 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 600 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 601 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 602 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 603 sampler.compare_mode = PIPE_TEX_COMPARE_NONE; 604 sampler.compare_func = PIPE_FUNC_ALWAYS; 605 sampler.normalized_coords = 1; 606 /*sampler.shadow_ambient = ; */ 607 /*sampler.lod_bias = ; */ 608 sampler.min_lod = 0; 609 /*sampler.max_lod = ; */ 610 /*sampler.border_color[0] = ; */ 611 /*sampler.max_anisotropy = ; */ 612 idct->samplers.all[i] = idct->pipe->create_sampler_state(idct->pipe, &sampler); 613 } 614} 615 616static void 617cleanup_state(struct vl_idct *idct) 618{ 619 unsigned i; 620 621 for(i = 0; i < 8; ++i) { 622 idct->pipe->screen->tex_surface_destroy(idct->fb_state[0].cbufs[i]); 623 } 624 625 idct->pipe->screen->tex_surface_destroy(idct->fb_state[1].cbufs[0]); 626 627 for (i = 0; i < 4; ++i) 628 idct->pipe->delete_sampler_state(idct->pipe, idct->samplers.all[i]); 629} 630 631struct pipe_resource * 632vl_idct_upload_matrix(struct pipe_context *pipe) 633{ 634 struct pipe_resource template, *matrix; 635 struct pipe_transfer *buf_transfer; 636 unsigned i, j, pitch; 637 float *f; 638 639 struct pipe_box rect = 640 { 641 0, 0, 0, 642 BLOCK_WIDTH, 643 BLOCK_HEIGHT, 644 1 645 }; 646 647 memset(&template, 0, sizeof(struct pipe_resource)); 648 template.target = PIPE_TEXTURE_2D; 649 template.format = PIPE_FORMAT_R32G32B32A32_FLOAT; 650 template.last_level = 0; 651 template.width0 = 2; 652 template.height0 = 8; 653 template.depth0 = 1; 654 template.usage = PIPE_USAGE_IMMUTABLE; 655 template.bind = PIPE_BIND_SAMPLER_VIEW; 656 template.flags = 0; 657 658 matrix = pipe->screen->resource_create(pipe->screen, &template); 659 660 /* matrix */ 661 buf_transfer = pipe->get_transfer 662 ( 663 pipe, matrix, 664 u_subresource(0, 0), 665 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 666 &rect 667 ); 668 pitch = buf_transfer->stride / sizeof(float); 669 670 f = pipe->transfer_map(pipe, buf_transfer); 671 for(i = 0; i < BLOCK_HEIGHT; ++i) 672 for(j = 0; j < BLOCK_WIDTH; ++j) 673 f[i * pitch + j] = const_matrix[j][i]; // transpose 674 675 pipe->transfer_unmap(pipe, buf_transfer); 676 pipe->transfer_destroy(pipe, buf_transfer); 677 678 return matrix; 679} 680 681bool 682vl_idct_init(struct vl_idct *idct, struct pipe_context *pipe, struct pipe_resource *dst, struct pipe_resource *matrix) 683{ 684 assert(idct && pipe && dst); 685 686 idct->pipe = pipe; 687 pipe_resource_reference(&idct->textures.individual.matrix, matrix); 688 pipe_resource_reference(&idct->textures.individual.transpose, matrix); 689 pipe_resource_reference(&idct->destination, dst); 690 691 if(!init_buffers(idct)) 692 return false; 693 694 if(!init_shaders(idct)) { 695 cleanup_buffers(idct); 696 return false; 697 } 698 699 init_state(idct); 700 701 init_constants(idct); 702 xfer_buffers_map(idct); 703 704 return true; 705} 706 707void 708vl_idct_cleanup(struct vl_idct *idct) 709{ 710 cleanup_shaders(idct); 711 cleanup_buffers(idct); 712 713 cleanup_state(idct); 714 715 pipe_resource_reference(&idct->destination, NULL); 716} 717 718void 719vl_idct_add_block(struct vl_idct *idct, unsigned x, unsigned y, short *block) 720{ 721 struct vertex2f v, *v_dst; 722 723 unsigned tex_pitch; 724 unsigned nr_components; 725 short *texels; 726 727 unsigned i; 728 729 assert(idct); 730 731 if(block) { 732 nr_components = util_format_get_nr_components(idct->tex_transfer->resource->format); 733 734 tex_pitch = idct->tex_transfer->stride / sizeof(short); 735 texels = idct->texels + y * tex_pitch * BLOCK_HEIGHT + x * BLOCK_WIDTH; 736 737 for (i = 0; i < BLOCK_HEIGHT; ++i) 738 memcpy(texels + i * tex_pitch, block + i * BLOCK_WIDTH, BLOCK_WIDTH * sizeof(short)); 739 740 /* non empty blocks fills the vector buffer from left to right */ 741 v_dst = idct->vectors + idct->num_blocks * 4; 742 743 idct->num_blocks++; 744 745 } else { 746 747 /* while empty blocks fills the vector buffer from right to left */ 748 v_dst = idct->vectors + (idct->max_blocks - idct->num_empty_blocks) * 4 - 4; 749 750 idct->num_empty_blocks++; 751 } 752 753 v.x = x; 754 v.y = y; 755 756 for (i = 0; i < 4; ++i) { 757 v_dst[i] = v; 758 } 759} 760 761void 762vl_idct_flush(struct vl_idct *idct) 763{ 764 xfer_buffers_unmap(idct); 765 766 if(idct->num_blocks > 0) { 767 768 /* first stage */ 769 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state[0]); 770 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport[0]); 771 772 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all); 773 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 774 idct->pipe->set_fragment_sampler_views(idct->pipe, 2, idct->sampler_views.stage[0]); 775 idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[0]); 776 idct->pipe->bind_vs_state(idct->pipe, idct->matrix_vs); 777 idct->pipe->bind_fs_state(idct->pipe, idct->matrix_fs); 778 779 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, idct->num_blocks * 4); 780 781 /* second stage */ 782 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state[1]); 783 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport[1]); 784 785 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all); 786 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 787 idct->pipe->set_fragment_sampler_views(idct->pipe, 2, idct->sampler_views.stage[1]); 788 idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[1]); 789 idct->pipe->bind_vs_state(idct->pipe, idct->transpose_vs); 790 idct->pipe->bind_fs_state(idct->pipe, idct->transpose_fs); 791 792 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, idct->num_blocks * 4); 793 } 794 795 if(idct->num_empty_blocks > 0) { 796 797 /* empty block handling */ 798 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state[1]); 799 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport[1]); 800 801 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all); 802 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 803 idct->pipe->bind_vs_state(idct->pipe, idct->eb_vs); 804 idct->pipe->bind_fs_state(idct->pipe, idct->eb_fs); 805 806 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 807 (idct->max_blocks - idct->num_empty_blocks) * 4, 808 idct->num_empty_blocks * 4); 809 } 810 811 idct->num_blocks = 0; 812 idct->num_empty_blocks = 0; 813 xfer_buffers_map(idct); 814} 815