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