vl_idct.c revision c9e10c666adc64f6c5dfb04422560508f115aa54
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 bool fetch4[2], float scale) 151{ 152 struct ureg_dst t_tc[2], m[2][2], tmp[2]; 153 unsigned side, 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(side = 0; side < 2; ++side) { 174 for(i = 0; i < 2; ++i) { 175 if(fetch4[side]) { 176 ureg_TEX(shader, m[i][side], TGSI_TEXTURE_2D, ureg_src(t_tc[side]), sampler[side]); 177 ureg_ADD(shader, ureg_writemask(t_tc[side], TGSI_WRITEMASK_X), ureg_src(t_tc[side]), step[side]); 178 179 } else for(j = 0; j < 4; ++j) { 180 /* Nouveau and r600g can't writemask tex dst regs (yet?), do in two steps */ 181 ureg_TEX(shader, tmp[side], TGSI_TEXTURE_2D, ureg_src(t_tc[side]), sampler[side]); 182 ureg_MOV(shader, ureg_writemask(m[i][side], TGSI_WRITEMASK_X << j), ureg_scalar(ureg_src(tmp[side]), TGSI_SWIZZLE_X)); 183 184 ureg_ADD(shader, ureg_writemask(t_tc[side], TGSI_WRITEMASK_X << side), ureg_src(t_tc[side]), step[side]); 185 } 186 } 187 } 188 189 ureg_DP4(shader, ureg_writemask(tmp[0], TGSI_WRITEMASK_X), ureg_src(m[0][0]), ureg_src(m[0][1])); 190 ureg_DP4(shader, ureg_writemask(tmp[1], TGSI_WRITEMASK_X), ureg_src(m[1][0]), ureg_src(m[1][1])); 191 ureg_ADD(shader, ureg_writemask(tmp[0], TGSI_WRITEMASK_X), ureg_src(tmp[0]), ureg_src(tmp[1])); 192 ureg_MUL(shader, dst, ureg_src(tmp[0]), ureg_imm1f(shader, scale)); 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 bool fetch4[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, 4.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, 1); 226 227 fetch4[0] = true; 228 fetch4[1] = false; 229 230 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0); 231 232 matrix_mul(shader, fragment, tc, sampler, start, step, fetch4, STAGE1_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 bool fetch4[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, 1); 262 sampler[1] = ureg_DECL_sampler(shader, 0); 263 264 fetch4[0] = false; 265 fetch4[1] = false; 266 267 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0); 268 269 matrix_mul(shader, fragment, tc, sampler, start, step, fetch4, STAGE2_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 idct->pipe, idct->textures.individual.source, 309 u_subresource(0, 0), 310 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 311 &rect 312 ); 313 314 idct->texels = idct->pipe->transfer_map(idct->pipe, idct->tex_transfer); 315 316 idct->vectors = pipe_buffer_map 317 ( 318 idct->pipe, 319 idct->vertex_bufs.individual.pos.buffer, 320 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 321 &idct->vec_transfer 322 ); 323} 324 325static void 326xfer_buffers_unmap(struct vl_idct *idct) 327{ 328 pipe_buffer_unmap(idct->pipe, idct->vertex_bufs.individual.pos.buffer, idct->vec_transfer); 329 330 idct->pipe->transfer_unmap(idct->pipe, idct->tex_transfer); 331 idct->pipe->transfer_destroy(idct->pipe, idct->tex_transfer); 332} 333 334static bool 335init_shaders(struct vl_idct *idct) 336{ 337 idct->vs = create_vert_shader(idct); 338 idct->transpose_fs = create_transpose_frag_shader(idct); 339 idct->matrix_fs = create_matrix_frag_shader(idct); 340 idct->eb_fs = create_empty_block_frag_shader(idct); 341 342 return 343 idct->vs != NULL && 344 idct->transpose_fs != NULL && 345 idct->matrix_fs != NULL && 346 idct->eb_fs != NULL; 347} 348 349static void 350cleanup_shaders(struct vl_idct *idct) 351{ 352 idct->pipe->delete_vs_state(idct->pipe, idct->vs); 353 idct->pipe->delete_fs_state(idct->pipe, idct->transpose_fs); 354 idct->pipe->delete_fs_state(idct->pipe, idct->matrix_fs); 355 idct->pipe->delete_fs_state(idct->pipe, idct->eb_fs); 356} 357 358static bool 359init_buffers(struct vl_idct *idct) 360{ 361 struct pipe_resource template; 362 struct pipe_sampler_view sampler_view; 363 struct pipe_vertex_element vertex_elems[2]; 364 unsigned i; 365 366 idct->max_blocks = 367 align(idct->destination->width0, BLOCK_WIDTH) / BLOCK_WIDTH * 368 align(idct->destination->height0, BLOCK_HEIGHT) / BLOCK_HEIGHT * 369 idct->destination->depth0; 370 371 memset(&template, 0, sizeof(struct pipe_resource)); 372 template.target = PIPE_TEXTURE_2D; 373 template.format = PIPE_FORMAT_R32G32B32A32_FLOAT; 374 template.last_level = 0; 375 template.width0 = 2; 376 template.height0 = 8; 377 template.depth0 = 1; 378 template.usage = PIPE_USAGE_IMMUTABLE; 379 template.bind = PIPE_BIND_SAMPLER_VIEW; 380 template.flags = 0; 381 382 idct->textures.individual.transpose = idct->pipe->screen->resource_create(idct->pipe->screen, &template); 383 384 template.width0 = 8; 385 idct->textures.individual.matrix = idct->pipe->screen->resource_create(idct->pipe->screen, &template); 386 387 template.format = idct->destination->format; 388 template.width0 = idct->destination->width0; 389 template.height0 = idct->destination->height0; 390 template.depth0 = idct->destination->depth0; 391 template.usage = PIPE_USAGE_DYNAMIC; 392 idct->textures.individual.source = idct->pipe->screen->resource_create(idct->pipe->screen, &template); 393 394 template.usage = PIPE_USAGE_STATIC; 395 idct->textures.individual.intermediate = idct->pipe->screen->resource_create(idct->pipe->screen, &template); 396 397 for (i = 0; i < 4; ++i) { 398 if(idct->textures.all[i] == NULL) 399 return false; /* a texture failed to allocate */ 400 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 if(idct->vertex_bufs.individual.quad.buffer == NULL) 416 return false; 417 418 idct->vertex_bufs.individual.pos.stride = sizeof(struct vertex2f); 419 idct->vertex_bufs.individual.pos.max_index = 4 * idct->max_blocks - 1; 420 idct->vertex_bufs.individual.pos.buffer_offset = 0; 421 idct->vertex_bufs.individual.pos.buffer = pipe_buffer_create 422 ( 423 idct->pipe->screen, 424 PIPE_BIND_VERTEX_BUFFER, 425 sizeof(struct vertex2f) * 4 * idct->max_blocks 426 ); 427 428 if(idct->vertex_bufs.individual.pos.buffer == NULL) 429 return false; 430 431 /* Rect element */ 432 vertex_elems[0].src_offset = 0; 433 vertex_elems[0].instance_divisor = 0; 434 vertex_elems[0].vertex_buffer_index = 0; 435 vertex_elems[0].src_format = PIPE_FORMAT_R32G32_FLOAT; 436 437 /* Pos element */ 438 vertex_elems[1].src_offset = 0; 439 vertex_elems[1].instance_divisor = 0; 440 vertex_elems[1].vertex_buffer_index = 1; 441 vertex_elems[1].src_format = PIPE_FORMAT_R32G32_FLOAT; 442 443 idct->vertex_elems_state = idct->pipe->create_vertex_elements_state(idct->pipe, 2, vertex_elems); 444 445 idct->vs_const_buf = pipe_buffer_create 446 ( 447 idct->pipe->screen, 448 PIPE_BIND_CONSTANT_BUFFER, 449 sizeof(struct vertex_shader_consts) 450 ); 451 452 if(idct->vs_const_buf == NULL) 453 return false; 454 455 return true; 456} 457 458static void 459cleanup_buffers(struct vl_idct *idct) 460{ 461 unsigned i; 462 463 assert(idct); 464 465 pipe_resource_reference(&idct->vs_const_buf, NULL); 466 467 for (i = 0; i < 4; ++i) { 468 pipe_sampler_view_reference(&idct->sampler_views.all[i], NULL); 469 pipe_resource_reference(&idct->textures.all[i], NULL); 470 } 471 472 idct->pipe->delete_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 473 pipe_resource_reference(&idct->vertex_bufs.individual.quad.buffer, NULL); 474 pipe_resource_reference(&idct->vertex_bufs.individual.pos.buffer, NULL); 475} 476 477static void 478init_constants(struct vl_idct *idct) 479{ 480 struct pipe_transfer *buf_transfer; 481 struct vertex_shader_consts *vs_consts; 482 struct vertex2f *v; 483 float *f; 484 485 struct pipe_box rect = 486 { 487 0, 0, 0, 488 BLOCK_WIDTH, 489 BLOCK_HEIGHT, 490 1 491 }; 492 493 unsigned i, j, pitch; 494 495 /* quad vectors */ 496 v = pipe_buffer_map 497 ( 498 idct->pipe, 499 idct->vertex_bufs.individual.quad.buffer, 500 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 501 &buf_transfer 502 ); 503 for ( i = 0; i < idct->max_blocks; ++i) 504 memcpy(v + i * 4, &const_quad, sizeof(const_quad)); 505 pipe_buffer_unmap(idct->pipe, idct->vertex_bufs.individual.quad.buffer, buf_transfer); 506 507 /* transposed matrix */ 508 buf_transfer = idct->pipe->get_transfer 509 ( 510 idct->pipe, idct->textures.individual.transpose, 511 u_subresource(0, 0), 512 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 513 &rect 514 ); 515 pitch = buf_transfer->stride / util_format_get_blocksize(buf_transfer->resource->format); 516 517 f = idct->pipe->transfer_map(idct->pipe, buf_transfer); 518 for(i = 0; i < BLOCK_HEIGHT; ++i) 519 for(j = 0; j < BLOCK_WIDTH; ++j) 520 f[i * pitch * 4 + j] = const_matrix[j][i]; // transpose 521 522 idct->pipe->transfer_unmap(idct->pipe, buf_transfer); 523 idct->pipe->transfer_destroy(idct->pipe, buf_transfer); 524 525 /* matrix */ 526 buf_transfer = idct->pipe->get_transfer 527 ( 528 idct->pipe, idct->textures.individual.matrix, 529 u_subresource(0, 0), 530 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 531 &rect 532 ); 533 pitch = buf_transfer->stride / util_format_get_blocksize(buf_transfer->resource->format); 534 535 f = idct->pipe->transfer_map(idct->pipe, buf_transfer); 536 for(i = 0; i < BLOCK_HEIGHT; ++i) 537 for(j = 0; j < BLOCK_WIDTH; ++j) 538 f[i * pitch * 4 + j * 4] = const_matrix[i][j]; 539 540 idct->pipe->transfer_unmap(idct->pipe, buf_transfer); 541 idct->pipe->transfer_destroy(idct->pipe, buf_transfer); 542 543 /* normalisation constants */ 544 vs_consts = pipe_buffer_map 545 ( 546 idct->pipe, idct->vs_const_buf, 547 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 548 &buf_transfer 549 ); 550 551 vs_consts->norm.x = 1.0f / idct->destination->width0; 552 vs_consts->norm.y = 1.0f / idct->destination->height0; 553 554 pipe_buffer_unmap(idct->pipe, idct->vs_const_buf, buf_transfer); 555} 556 557static void 558init_state(struct vl_idct *idct) 559{ 560 struct pipe_sampler_state sampler; 561 unsigned i; 562 563 idct->num_blocks = 0; 564 idct->num_empty_blocks = 0; 565 566 idct->viewport.scale[0] = idct->destination->width0; 567 idct->viewport.scale[1] = idct->destination->height0; 568 idct->viewport.scale[2] = 1; 569 idct->viewport.scale[3] = 1; 570 idct->viewport.translate[0] = 0; 571 idct->viewport.translate[1] = 0; 572 idct->viewport.translate[2] = 0; 573 idct->viewport.translate[3] = 0; 574 575 idct->fb_state.width = idct->destination->width0; 576 idct->fb_state.height = idct->destination->height0; 577 idct->fb_state.nr_cbufs = 1; 578 idct->fb_state.zsbuf = NULL; 579 580 for (i = 0; i < 4; ++i) { 581 memset(&sampler, 0, sizeof(sampler)); 582 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 583 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 584 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 585 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 586 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 587 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 588 sampler.compare_mode = PIPE_TEX_COMPARE_NONE; 589 sampler.compare_func = PIPE_FUNC_ALWAYS; 590 sampler.normalized_coords = 1; 591 /*sampler.shadow_ambient = ; */ 592 /*sampler.lod_bias = ; */ 593 sampler.min_lod = 0; 594 /*sampler.max_lod = ; */ 595 /*sampler.border_color[0] = ; */ 596 /*sampler.max_anisotropy = ; */ 597 idct->samplers.all[i] = idct->pipe->create_sampler_state(idct->pipe, &sampler); 598 } 599} 600 601static void 602cleanup_state(struct vl_idct *idct) 603{ 604 unsigned i; 605 606 for (i = 0; i < 4; ++i) 607 idct->pipe->delete_sampler_state(idct->pipe, idct->samplers.all[i]); 608} 609 610bool 611vl_idct_init(struct vl_idct *idct, struct pipe_context *pipe, struct pipe_resource *dst) 612{ 613 assert(idct && pipe && dst); 614 615 idct->pipe = pipe; 616 pipe_resource_reference(&idct->destination, dst); 617 618 init_state(idct); 619 620 if(!init_shaders(idct)) 621 return false; 622 623 if(!init_buffers(idct)) { 624 cleanup_shaders(idct); 625 return false; 626 } 627 628 idct->surfaces.intermediate = idct->pipe->screen->get_tex_surface( 629 idct->pipe->screen, idct->textures.individual.intermediate, 0, 0, 0, 630 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET); 631 632 idct->surfaces.destination = idct->pipe->screen->get_tex_surface( 633 idct->pipe->screen, idct->destination, 0, 0, 0, 634 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET); 635 636 init_constants(idct); 637 xfer_buffers_map(idct); 638 639 return true; 640} 641 642void 643vl_idct_cleanup(struct vl_idct *idct) 644{ 645 idct->pipe->screen->tex_surface_destroy(idct->surfaces.destination); 646 idct->pipe->screen->tex_surface_destroy(idct->surfaces.intermediate); 647 648 cleanup_shaders(idct); 649 cleanup_buffers(idct); 650 651 cleanup_state(idct); 652 653 pipe_resource_reference(&idct->destination, NULL); 654} 655 656void 657vl_idct_add_block(struct vl_idct *idct, unsigned x, unsigned y, short *block) 658{ 659 struct vertex2f v, *v_dst; 660 661 unsigned tex_pitch; 662 short *texels; 663 664 unsigned i; 665 666 assert(idct); 667 668 if(block) { 669 tex_pitch = idct->tex_transfer->stride / util_format_get_blocksize(idct->tex_transfer->resource->format); 670 texels = idct->texels + y * tex_pitch * BLOCK_HEIGHT + x * BLOCK_WIDTH; 671 672 for (i = 0; i < BLOCK_HEIGHT; ++i) 673 memcpy(texels + i * tex_pitch, block + i * BLOCK_WIDTH, BLOCK_WIDTH * 2); 674 675 /* non empty blocks fills the vector buffer from left to right */ 676 v_dst = idct->vectors + idct->num_blocks * 4; 677 678 idct->num_blocks++; 679 680 } else { 681 682 /* while empty blocks fills the vector buffer from right to left */ 683 v_dst = idct->vectors + (idct->max_blocks - idct->num_empty_blocks) * 4 - 4; 684 685 idct->num_empty_blocks++; 686 } 687 688 v.x = x; 689 v.y = y; 690 691 for (i = 0; i < 4; ++i) { 692 v_dst[i] = v; 693 } 694} 695 696void 697vl_idct_flush(struct vl_idct *idct) 698{ 699 xfer_buffers_unmap(idct); 700 701 idct->pipe->set_constant_buffer(idct->pipe, PIPE_SHADER_VERTEX, 0, idct->vs_const_buf); 702 703 if(idct->num_blocks > 0) { 704 705 /* first stage */ 706 idct->fb_state.cbufs[0] = idct->surfaces.intermediate; 707 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state); 708 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport); 709 710 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all); 711 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 712 idct->pipe->set_fragment_sampler_views(idct->pipe, 2, idct->sampler_views.stage[0]); 713 idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[0]); 714 idct->pipe->bind_vs_state(idct->pipe, idct->vs); 715 idct->pipe->bind_fs_state(idct->pipe, idct->transpose_fs); 716 717 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, idct->num_blocks * 4); 718 719 /* second stage */ 720 idct->fb_state.cbufs[0] = idct->surfaces.destination; 721 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state); 722 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport); 723 724 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all); 725 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 726 idct->pipe->set_fragment_sampler_views(idct->pipe, 2, idct->sampler_views.stage[1]); 727 idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[1]); 728 idct->pipe->bind_vs_state(idct->pipe, idct->vs); 729 idct->pipe->bind_fs_state(idct->pipe, idct->matrix_fs); 730 731 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, idct->num_blocks * 4); 732 } 733 734 if(idct->num_empty_blocks > 0) { 735 736 /* empty block handling */ 737 idct->fb_state.cbufs[0] = idct->surfaces.destination; 738 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state); 739 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport); 740 741 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all); 742 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 743 idct->pipe->set_fragment_sampler_views(idct->pipe, 4, idct->sampler_views.all); 744 idct->pipe->bind_fragment_sampler_states(idct->pipe, 4, idct->samplers.all); 745 idct->pipe->bind_vs_state(idct->pipe, idct->vs); 746 idct->pipe->bind_fs_state(idct->pipe, idct->eb_fs); 747 748 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 749 (idct->max_blocks - idct->num_empty_blocks) * 4, 750 idct->num_empty_blocks * 4); 751 } 752 753 idct->num_blocks = 0; 754 idct->num_empty_blocks = 0; 755 xfer_buffers_map(idct); 756} 757