vl_idct.c revision ed49905944243863913bc2598f734ca038c85b94
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 VS_O_STEP 67}; 68 69static const float const_matrix[8][8] = { 70 { 0.3535530f, 0.3535530f, 0.3535530f, 0.3535530f, 0.3535530f, 0.3535530f, 0.353553f, 0.3535530f }, 71 { 0.4903930f, 0.4157350f, 0.2777850f, 0.0975451f, -0.0975452f, -0.2777850f, -0.415735f, -0.4903930f }, 72 { 0.4619400f, 0.1913420f, -0.1913420f, -0.4619400f, -0.4619400f, -0.1913420f, 0.191342f, 0.4619400f }, 73 { 0.4157350f, -0.0975452f, -0.4903930f, -0.2777850f, 0.2777850f, 0.4903930f, 0.097545f, -0.4157350f }, 74 { 0.3535530f, -0.3535530f, -0.3535530f, 0.3535540f, 0.3535530f, -0.3535540f, -0.353553f, 0.3535530f }, 75 { 0.2777850f, -0.4903930f, 0.0975452f, 0.4157350f, -0.4157350f, -0.0975451f, 0.490393f, -0.2777850f }, 76 { 0.1913420f, -0.4619400f, 0.4619400f, -0.1913420f, -0.1913410f, 0.4619400f, -0.461940f, 0.1913420f }, 77 { 0.0975451f, -0.2777850f, 0.4157350f, -0.4903930f, 0.4903930f, -0.4157350f, 0.277786f, -0.0975458f } 78}; 79 80/* vertices for a quad covering a block */ 81static const struct vertex2f const_quad[4] = { 82 {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f} 83}; 84 85static void * 86create_vert_shader(struct vl_idct *idct) 87{ 88 struct ureg_program *shader; 89 struct ureg_src norm, bs; 90 struct ureg_src vrect, vpos; 91 struct ureg_dst scale, t_vpos; 92 struct ureg_dst o_vpos, o_block, o_tex, o_start, o_step; 93 94 shader = ureg_create(TGSI_PROCESSOR_VERTEX); 95 if (!shader) 96 return NULL; 97 98 norm = ureg_DECL_constant(shader, 0); 99 bs = ureg_imm2f(shader, BLOCK_WIDTH, BLOCK_HEIGHT); 100 101 scale = ureg_DECL_temporary(shader); 102 t_vpos = ureg_DECL_temporary(shader); 103 104 vrect = ureg_DECL_vs_input(shader, VS_I_RECT); 105 vpos = ureg_DECL_vs_input(shader, VS_I_VPOS); 106 107 o_vpos = ureg_DECL_output(shader, TGSI_SEMANTIC_POSITION, VS_O_VPOS); 108 o_block = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK); 109 o_tex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX); 110 o_start = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_START); 111 o_step = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_STEP); 112 113 /* 114 * scale = norm * mbs; 115 * 116 * t_vpos = vpos + vrect 117 * o_vpos.xy = t_vpos * scale 118 * o_vpos.zw = vpos 119 * 120 * o_block = vrect 121 * o_tex = t_pos 122 * o_start = vpos * scale 123 * o_step = norm 124 * 125 */ 126 ureg_MUL(shader, ureg_writemask(scale, TGSI_WRITEMASK_XY), norm, bs); 127 128 ureg_ADD(shader, ureg_writemask(t_vpos, TGSI_WRITEMASK_XY), vpos, vrect); 129 ureg_MUL(shader, ureg_writemask(t_vpos, TGSI_WRITEMASK_XY), ureg_src(t_vpos), ureg_src(scale)); 130 ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_XY), ureg_src(t_vpos)); 131 ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_ZW), vpos); 132 133 ureg_MOV(shader, ureg_writemask(o_tex, TGSI_WRITEMASK_XY), ureg_src(t_vpos)); 134 ureg_MOV(shader, ureg_writemask(o_block, TGSI_WRITEMASK_XY), vrect); 135 ureg_MUL(shader, ureg_writemask(o_start, TGSI_WRITEMASK_XY), vpos, ureg_src(scale)); 136 ureg_MOV(shader, ureg_writemask(o_step, TGSI_WRITEMASK_XY), norm); 137 138 ureg_release_temporary(shader, t_vpos); 139 ureg_release_temporary(shader, scale); 140 141 ureg_END(shader); 142 143 return ureg_create_shader_and_destroy(shader, idct->pipe); 144} 145 146static void 147matrix_mul(struct ureg_program *shader, struct ureg_dst dst, 148 struct ureg_src tc[2], struct ureg_src sampler[2], 149 struct ureg_src start[2], struct ureg_src step[2], 150 float scale) 151{ 152 struct ureg_dst t_tc[2], m[2][2], tmp[2]; 153 unsigned i, j; 154 155 for(i = 0; i < 2; ++i) { 156 t_tc[i] = ureg_DECL_temporary(shader); 157 for(j = 0; j < 2; ++j) 158 m[i][j] = ureg_DECL_temporary(shader); 159 tmp[i] = ureg_DECL_temporary(shader); 160 } 161 162 /* 163 * m[0..1][0] = ? 164 * tmp[0..1] = dot4(m[0..1][0], m[0..1][1]) 165 * fragment = tmp[0] + tmp[1] 166 */ 167 ureg_MOV(shader, ureg_writemask(t_tc[0], TGSI_WRITEMASK_X), start[0]); 168 ureg_MOV(shader, ureg_writemask(t_tc[0], TGSI_WRITEMASK_Y), tc[0]); 169 170 ureg_MOV(shader, ureg_writemask(t_tc[1], TGSI_WRITEMASK_X), tc[1]); 171 ureg_MOV(shader, ureg_writemask(t_tc[1], TGSI_WRITEMASK_Y), start[1]); 172 173 for(i = 0; i < 2; ++i) { 174 for(j = 0; j < 4; ++j) { 175 /* Nouveau and r600g can't writemask tex dst regs (yet?), do in two steps */ 176 ureg_TEX(shader, tmp[0], TGSI_TEXTURE_2D, ureg_src(t_tc[0]), sampler[0]); 177 ureg_MOV(shader, ureg_writemask(m[i][0], TGSI_WRITEMASK_X << j), ureg_scalar(ureg_src(tmp[0]), TGSI_SWIZZLE_X)); 178 179 ureg_TEX(shader, tmp[1], TGSI_TEXTURE_2D, ureg_src(t_tc[1]), sampler[1]); 180 ureg_MOV(shader, ureg_writemask(m[i][1], TGSI_WRITEMASK_X << j), ureg_scalar(ureg_src(tmp[1]), TGSI_SWIZZLE_X)); 181 182 ureg_ADD(shader, ureg_writemask(t_tc[0], TGSI_WRITEMASK_X), ureg_src(t_tc[0]), step[0]); 183 ureg_ADD(shader, ureg_writemask(t_tc[1], TGSI_WRITEMASK_Y), ureg_src(t_tc[1]), step[1]); 184 } 185 } 186 187 ureg_DP4(shader, ureg_writemask(tmp[0], TGSI_WRITEMASK_X), ureg_src(m[0][0]), ureg_src(m[0][1])); 188 ureg_DP4(shader, ureg_writemask(tmp[1], TGSI_WRITEMASK_X), ureg_src(m[1][0]), ureg_src(m[1][1])); 189 ureg_ADD(shader, ureg_writemask(tmp[0], TGSI_WRITEMASK_X), ureg_src(tmp[0]), ureg_src(tmp[1])); 190 ureg_MUL(shader, dst, ureg_src(tmp[0]), ureg_imm1f(shader, scale)); 191 192 for(i = 0; i < 2; ++i) { 193 ureg_release_temporary(shader, t_tc[i]); 194 for(j = 0; j < 2; ++j) 195 ureg_release_temporary(shader, m[i][j]); 196 ureg_release_temporary(shader, tmp[i]); 197 } 198} 199 200static void * 201create_transpose_frag_shader(struct vl_idct *idct) 202{ 203 struct ureg_program *shader; 204 struct ureg_src tc[2], sampler[2]; 205 struct ureg_src start[2], step[2]; 206 struct ureg_dst fragment; 207 208 shader = ureg_create(TGSI_PROCESSOR_FRAGMENT); 209 if (!shader) 210 return NULL; 211 212 tc[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK, TGSI_INTERPOLATE_LINEAR); 213 tc[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX, TGSI_INTERPOLATE_LINEAR); 214 215 start[0] = ureg_imm1f(shader, 0.0f); 216 start[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_START, TGSI_INTERPOLATE_CONSTANT); 217 218 step[0] = ureg_imm1f(shader, 1.0f / BLOCK_HEIGHT); 219 step[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_STEP, TGSI_INTERPOLATE_CONSTANT); 220 221 sampler[0] = ureg_DECL_sampler(shader, 0); 222 sampler[1] = ureg_DECL_sampler(shader, 2); 223 224 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0); 225 226 matrix_mul(shader, fragment, tc, sampler, start, step, STAGE1_SCALE); 227 228 ureg_END(shader); 229 230 return ureg_create_shader_and_destroy(shader, idct->pipe); 231} 232 233static void * 234create_matrix_frag_shader(struct vl_idct *idct) 235{ 236 struct ureg_program *shader; 237 struct ureg_src tc[2], sampler[2]; 238 struct ureg_src start[2], step[2]; 239 struct ureg_dst fragment; 240 241 shader = ureg_create(TGSI_PROCESSOR_FRAGMENT); 242 if (!shader) 243 return NULL; 244 245 tc[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX, TGSI_INTERPOLATE_LINEAR); 246 tc[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK, TGSI_INTERPOLATE_LINEAR); 247 248 start[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_START, TGSI_INTERPOLATE_CONSTANT); 249 start[1] = ureg_imm1f(shader, 0.0f); 250 251 step[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_STEP, TGSI_INTERPOLATE_CONSTANT); 252 step[1] = ureg_imm1f(shader, 1.0f / BLOCK_WIDTH); 253 254 sampler[0] = ureg_DECL_sampler(shader, 3); 255 sampler[1] = ureg_DECL_sampler(shader, 1); 256 257 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0); 258 259 matrix_mul(shader, fragment, tc, sampler, start, step, STAGE2_SCALE); 260 261 ureg_END(shader); 262 263 return ureg_create_shader_and_destroy(shader, idct->pipe); 264} 265 266static void * 267create_empty_block_frag_shader(struct vl_idct *idct) 268{ 269 struct ureg_program *shader; 270 struct ureg_dst fragment; 271 272 shader = ureg_create(TGSI_PROCESSOR_FRAGMENT); 273 if (!shader) 274 return NULL; 275 276 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0); 277 278 ureg_MOV(shader, fragment, ureg_imm1f(shader, 0.0f)); 279 280 ureg_END(shader); 281 282 return ureg_create_shader_and_destroy(shader, idct->pipe); 283} 284 285static void 286xfer_buffers_map(struct vl_idct *idct) 287{ 288 struct pipe_box rect = 289 { 290 0, 0, 0, 291 idct->destination->width0, 292 idct->destination->height0, 293 1 294 }; 295 296 idct->tex_transfer = idct->pipe->get_transfer 297 ( 298#if 1 299 idct->pipe, idct->textures.individual.source, 300#else 301 idct->pipe, idct->destination, 302#endif 303 u_subresource(0, 0), 304 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 305 &rect 306 ); 307 308 idct->texels = idct->pipe->transfer_map(idct->pipe, idct->tex_transfer); 309 310 idct->vectors = pipe_buffer_map 311 ( 312 idct->pipe, 313 idct->vertex_bufs.individual.pos.buffer, 314 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 315 &idct->vec_transfer 316 ); 317} 318 319static void 320xfer_buffers_unmap(struct vl_idct *idct) 321{ 322 pipe_buffer_unmap(idct->pipe, idct->vertex_bufs.individual.pos.buffer, idct->vec_transfer); 323 324 idct->pipe->transfer_unmap(idct->pipe, idct->tex_transfer); 325 idct->pipe->transfer_destroy(idct->pipe, idct->tex_transfer); 326} 327 328static bool 329init_shaders(struct vl_idct *idct) 330{ 331 idct->vs = create_vert_shader(idct); 332 idct->transpose_fs = create_transpose_frag_shader(idct); 333 idct->matrix_fs = create_matrix_frag_shader(idct); 334 idct->eb_fs = create_empty_block_frag_shader(idct); 335 336 return 337 idct->vs != NULL && 338 idct->transpose_fs != NULL && 339 idct->matrix_fs != NULL && 340 idct->eb_fs != NULL; 341} 342 343static void 344cleanup_shaders(struct vl_idct *idct) 345{ 346 idct->pipe->delete_vs_state(idct->pipe, idct->vs); 347 idct->pipe->delete_fs_state(idct->pipe, idct->transpose_fs); 348 idct->pipe->delete_fs_state(idct->pipe, idct->matrix_fs); 349 idct->pipe->delete_fs_state(idct->pipe, idct->eb_fs); 350} 351 352static bool 353init_buffers(struct vl_idct *idct) 354{ 355 struct pipe_resource template; 356 struct pipe_sampler_view sampler_view; 357 struct pipe_vertex_element vertex_elems[2]; 358 unsigned i; 359 360 idct->max_blocks = 361 align(idct->destination->width0, BLOCK_WIDTH) / BLOCK_WIDTH * 362 align(idct->destination->height0, BLOCK_HEIGHT) / BLOCK_HEIGHT * 363 idct->destination->depth0; 364 365 memset(&template, 0, sizeof(struct pipe_resource)); 366 template.target = PIPE_TEXTURE_2D; 367 template.format = PIPE_FORMAT_R32_FLOAT; 368 template.last_level = 0; 369 template.width0 = 8; 370 template.height0 = 8; 371 template.depth0 = 1; 372 template.usage = PIPE_USAGE_IMMUTABLE; 373 template.bind = PIPE_BIND_SAMPLER_VIEW; 374 template.flags = 0; 375 376 idct->textures.individual.matrix = idct->pipe->screen->resource_create(idct->pipe->screen, &template); 377 idct->textures.individual.transpose = idct->pipe->screen->resource_create(idct->pipe->screen, &template); 378 379 template.format = idct->destination->format; 380 template.width0 = idct->destination->width0; 381 template.height0 = idct->destination->height0; 382 template.depth0 = idct->destination->depth0; 383 template.usage = PIPE_USAGE_DYNAMIC; 384 idct->textures.individual.source = idct->pipe->screen->resource_create(idct->pipe->screen, &template); 385 386 template.usage = PIPE_USAGE_STATIC; 387 idct->textures.individual.intermediate = idct->pipe->screen->resource_create(idct->pipe->screen, &template); 388 389 for (i = 0; i < 4; ++i) { 390 if(idct->textures.all[i] == NULL) 391 return false; /* a texture failed to allocate */ 392 393 u_sampler_view_default_template(&sampler_view, idct->textures.all[i], idct->textures.all[i]->format); 394 idct->sampler_views.all[i] = idct->pipe->create_sampler_view(idct->pipe, idct->textures.all[i], &sampler_view); 395 } 396 397 idct->vertex_bufs.individual.quad.stride = sizeof(struct vertex2f); 398 idct->vertex_bufs.individual.quad.max_index = 4 * idct->max_blocks - 1; 399 idct->vertex_bufs.individual.quad.buffer_offset = 0; 400 idct->vertex_bufs.individual.quad.buffer = pipe_buffer_create 401 ( 402 idct->pipe->screen, 403 PIPE_BIND_VERTEX_BUFFER, 404 sizeof(struct vertex2f) * 4 * idct->max_blocks 405 ); 406 407 if(idct->vertex_bufs.individual.quad.buffer == NULL) 408 return false; 409 410 idct->vertex_bufs.individual.pos.stride = sizeof(struct vertex2f); 411 idct->vertex_bufs.individual.pos.max_index = 4 * idct->max_blocks - 1; 412 idct->vertex_bufs.individual.pos.buffer_offset = 0; 413 idct->vertex_bufs.individual.pos.buffer = pipe_buffer_create 414 ( 415 idct->pipe->screen, 416 PIPE_BIND_VERTEX_BUFFER, 417 sizeof(struct vertex2f) * 4 * idct->max_blocks 418 ); 419 420 if(idct->vertex_bufs.individual.pos.buffer == NULL) 421 return false; 422 423 /* Rect element */ 424 vertex_elems[0].src_offset = 0; 425 vertex_elems[0].instance_divisor = 0; 426 vertex_elems[0].vertex_buffer_index = 0; 427 vertex_elems[0].src_format = PIPE_FORMAT_R32G32_FLOAT; 428 429 /* Pos element */ 430 vertex_elems[1].src_offset = 0; 431 vertex_elems[1].instance_divisor = 0; 432 vertex_elems[1].vertex_buffer_index = 1; 433 vertex_elems[1].src_format = PIPE_FORMAT_R32G32_FLOAT; 434 435 idct->vertex_elems_state = idct->pipe->create_vertex_elements_state(idct->pipe, 2, vertex_elems); 436 437 idct->vs_const_buf = pipe_buffer_create 438 ( 439 idct->pipe->screen, 440 PIPE_BIND_CONSTANT_BUFFER, 441 sizeof(struct vertex_shader_consts) 442 ); 443 444 if(idct->vs_const_buf == NULL) 445 return false; 446 447 return true; 448} 449 450static void 451cleanup_buffers(struct vl_idct *idct) 452{ 453 unsigned i; 454 455 assert(idct); 456 457 pipe_resource_reference(&idct->vs_const_buf, NULL); 458 459 for (i = 0; i < 4; ++i) { 460 pipe_sampler_view_reference(&idct->sampler_views.all[i], NULL); 461 pipe_resource_reference(&idct->textures.all[i], NULL); 462 } 463 464 idct->pipe->delete_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 465 pipe_resource_reference(&idct->vertex_bufs.individual.quad.buffer, NULL); 466 pipe_resource_reference(&idct->vertex_bufs.individual.pos.buffer, NULL); 467} 468 469static void 470init_constants(struct vl_idct *idct) 471{ 472 struct pipe_transfer *buf_transfer; 473 struct vertex_shader_consts *vs_consts; 474 struct vertex2f *v; 475 float *f; 476 477 struct pipe_box rect = 478 { 479 0, 0, 0, 480 BLOCK_WIDTH, 481 BLOCK_HEIGHT, 482 1 483 }; 484 485 unsigned i, j, pitch; 486 487 /* quad vectors */ 488 v = pipe_buffer_map 489 ( 490 idct->pipe, 491 idct->vertex_bufs.individual.quad.buffer, 492 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 493 &buf_transfer 494 ); 495 for ( i = 0; i < idct->max_blocks; ++i) 496 memcpy(v + i * 4, &const_quad, sizeof(const_quad)); 497 pipe_buffer_unmap(idct->pipe, idct->vertex_bufs.individual.quad.buffer, buf_transfer); 498 499 /* transposed matrix */ 500 buf_transfer = idct->pipe->get_transfer 501 ( 502 idct->pipe, idct->textures.individual.transpose, 503 u_subresource(0, 0), 504 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 505 &rect 506 ); 507 pitch = buf_transfer->stride / util_format_get_blocksize(buf_transfer->resource->format); 508 509 f = idct->pipe->transfer_map(idct->pipe, buf_transfer); 510 for(i = 0; i < BLOCK_HEIGHT; ++i) 511 for(j = 0; j < BLOCK_WIDTH; ++j) 512 f[i * pitch + j] = const_matrix[j][i]; // transpose 513 514 idct->pipe->transfer_unmap(idct->pipe, buf_transfer); 515 idct->pipe->transfer_destroy(idct->pipe, buf_transfer); 516 517 /* matrix */ 518 buf_transfer = idct->pipe->get_transfer 519 ( 520 idct->pipe, idct->textures.individual.matrix, 521 u_subresource(0, 0), 522 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 523 &rect 524 ); 525 pitch = buf_transfer->stride / util_format_get_blocksize(buf_transfer->resource->format); 526 527 f = idct->pipe->transfer_map(idct->pipe, buf_transfer); 528 for(i = 0; i < BLOCK_HEIGHT; ++i) 529 for(j = 0; j < BLOCK_WIDTH; ++j) 530 f[i * pitch + j] = const_matrix[i][j]; 531 532 idct->pipe->transfer_unmap(idct->pipe, buf_transfer); 533 idct->pipe->transfer_destroy(idct->pipe, buf_transfer); 534 535 /* normalisation constants */ 536 vs_consts = pipe_buffer_map 537 ( 538 idct->pipe, idct->vs_const_buf, 539 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 540 &buf_transfer 541 ); 542 543 vs_consts->norm.x = 1.0f / idct->destination->width0; 544 vs_consts->norm.y = 1.0f / idct->destination->height0; 545 546 pipe_buffer_unmap(idct->pipe, idct->vs_const_buf, buf_transfer); 547} 548 549static void 550init_state(struct vl_idct *idct) 551{ 552 struct pipe_sampler_state sampler; 553 unsigned i; 554 555 idct->num_blocks = 0; 556 idct->num_empty_blocks = 0; 557 558 idct->viewport.scale[0] = idct->destination->width0; 559 idct->viewport.scale[1] = idct->destination->height0; 560 idct->viewport.scale[2] = 1; 561 idct->viewport.scale[3] = 1; 562 idct->viewport.translate[0] = 0; 563 idct->viewport.translate[1] = 0; 564 idct->viewport.translate[2] = 0; 565 idct->viewport.translate[3] = 0; 566 567 idct->fb_state.width = idct->destination->width0; 568 idct->fb_state.height = idct->destination->height0; 569 idct->fb_state.nr_cbufs = 1; 570 idct->fb_state.zsbuf = NULL; 571 572 for (i = 0; i < 4; ++i) { 573 memset(&sampler, 0, sizeof(sampler)); 574 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 575 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 576 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 577 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 578 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 579 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 580 sampler.compare_mode = PIPE_TEX_COMPARE_NONE; 581 sampler.compare_func = PIPE_FUNC_ALWAYS; 582 sampler.normalized_coords = 1; 583 /*sampler.shadow_ambient = ; */ 584 /*sampler.lod_bias = ; */ 585 sampler.min_lod = 0; 586 /*sampler.max_lod = ; */ 587 /*sampler.border_color[0] = ; */ 588 /*sampler.max_anisotropy = ; */ 589 idct->samplers.all[i] = idct->pipe->create_sampler_state(idct->pipe, &sampler); 590 } 591} 592 593static void 594cleanup_state(struct vl_idct *idct) 595{ 596 unsigned i; 597 598 for (i = 0; i < 4; ++i) 599 idct->pipe->delete_sampler_state(idct->pipe, idct->samplers.all[i]); 600} 601 602bool 603vl_idct_init(struct vl_idct *idct, struct pipe_context *pipe, struct pipe_resource *dst) 604{ 605 assert(idct && pipe && dst); 606 607 idct->pipe = pipe; 608 pipe_resource_reference(&idct->destination, dst); 609 610 init_state(idct); 611 612 if(!init_shaders(idct)) 613 return false; 614 615 if(!init_buffers(idct)) { 616 cleanup_shaders(idct); 617 return false; 618 } 619 620 idct->surfaces.intermediate = idct->pipe->screen->get_tex_surface( 621 idct->pipe->screen, idct->textures.individual.intermediate, 0, 0, 0, 622 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET); 623 624 idct->surfaces.destination = idct->pipe->screen->get_tex_surface( 625 idct->pipe->screen, idct->destination, 0, 0, 0, 626 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET); 627 628 init_constants(idct); 629 xfer_buffers_map(idct); 630 631 return true; 632} 633 634void 635vl_idct_cleanup(struct vl_idct *idct) 636{ 637 idct->pipe->screen->tex_surface_destroy(idct->surfaces.destination); 638 idct->pipe->screen->tex_surface_destroy(idct->surfaces.intermediate); 639 640 cleanup_shaders(idct); 641 cleanup_buffers(idct); 642 643 cleanup_state(idct); 644 645 pipe_resource_reference(&idct->destination, NULL); 646} 647 648void 649vl_idct_add_block(struct vl_idct *idct, unsigned x, unsigned y, short *block) 650{ 651 struct vertex2f v, *v_dst; 652 653 unsigned tex_pitch; 654 short *texels; 655 656 unsigned i; 657 658 assert(idct); 659 660 if(block) { 661 tex_pitch = idct->tex_transfer->stride / util_format_get_blocksize(idct->tex_transfer->resource->format); 662 texels = idct->texels + y * tex_pitch * BLOCK_HEIGHT + x * BLOCK_WIDTH; 663 664 for (i = 0; i < BLOCK_HEIGHT; ++i) 665 memcpy(texels + i * tex_pitch, block + i * BLOCK_WIDTH, BLOCK_WIDTH * 2); 666 667 /* non empty blocks fills the vector buffer from left to right */ 668 v_dst = idct->vectors + idct->num_blocks * 4; 669 670 idct->num_blocks++; 671 672 } else { 673 674 /* while empty blocks fills the vector buffer from right to left */ 675 v_dst = idct->vectors + (idct->max_blocks - idct->num_empty_blocks) * 4 - 4; 676 677 idct->num_empty_blocks++; 678 } 679 680 v.x = x; 681 v.y = y; 682 683 for (i = 0; i < 4; ++i) { 684 v_dst[i] = v; 685 } 686} 687 688void 689vl_idct_flush(struct vl_idct *idct) 690{ 691 xfer_buffers_unmap(idct); 692 693 idct->pipe->set_constant_buffer(idct->pipe, PIPE_SHADER_VERTEX, 0, idct->vs_const_buf); 694 695 if(idct->num_blocks > 0) { 696 697 /* first stage */ 698 idct->fb_state.cbufs[0] = idct->surfaces.intermediate; 699 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state); 700 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport); 701 702 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all); 703 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 704 idct->pipe->set_fragment_sampler_views(idct->pipe, 4, idct->sampler_views.all); 705 idct->pipe->bind_fragment_sampler_states(idct->pipe, 4, idct->samplers.all); 706 idct->pipe->bind_vs_state(idct->pipe, idct->vs); 707 idct->pipe->bind_fs_state(idct->pipe, idct->transpose_fs); 708 709 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, idct->num_blocks * 4); 710 711 idct->pipe->flush(idct->pipe, PIPE_FLUSH_RENDER_CACHE, NULL); 712 713 /* second stage */ 714 idct->fb_state.cbufs[0] = idct->surfaces.destination; 715 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state); 716 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport); 717 718 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all); 719 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 720 idct->pipe->set_fragment_sampler_views(idct->pipe, 4, idct->sampler_views.all); 721 idct->pipe->bind_fragment_sampler_states(idct->pipe, 4, idct->samplers.all); 722 idct->pipe->bind_vs_state(idct->pipe, idct->vs); 723 idct->pipe->bind_fs_state(idct->pipe, idct->matrix_fs); 724 725 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, idct->num_blocks * 4); 726 } 727 728 if(idct->num_empty_blocks > 0) { 729 730 /* empty block handling */ 731 idct->fb_state.cbufs[0] = idct->surfaces.destination; 732 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state); 733 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport); 734 735 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all); 736 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 737 idct->pipe->set_fragment_sampler_views(idct->pipe, 4, idct->sampler_views.all); 738 idct->pipe->bind_fragment_sampler_states(idct->pipe, 4, idct->samplers.all); 739 idct->pipe->bind_vs_state(idct->pipe, idct->vs); 740 idct->pipe->bind_fs_state(idct->pipe, idct->eb_fs); 741 742 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 743 (idct->max_blocks - idct->num_empty_blocks) * 4, 744 idct->num_empty_blocks * 4); 745 } 746 747 idct->pipe->flush(idct->pipe, PIPE_FLUSH_RENDER_CACHE, NULL); 748 749 idct->num_blocks = 0; 750 idct->num_empty_blocks = 0; 751 xfer_buffers_map(idct); 752} 753