vl_idct.c revision 3dd7bf7d39781f3ef4c0b53732945674c9924cdf
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 if(fetch4[1]) { 171 ureg_MOV(shader, ureg_writemask(t_tc[1], TGSI_WRITEMASK_X), ureg_scalar(start[1], TGSI_SWIZZLE_Y)); 172 ureg_MOV(shader, ureg_writemask(t_tc[1], TGSI_WRITEMASK_Y), ureg_scalar(tc[1], TGSI_SWIZZLE_X)); 173 } else { 174 ureg_MOV(shader, ureg_writemask(t_tc[1], TGSI_WRITEMASK_X), tc[1]); 175 ureg_MOV(shader, ureg_writemask(t_tc[1], TGSI_WRITEMASK_Y), start[1]); 176 } 177 178 for(side = 0; side < 2; ++side) { 179 for(i = 0; i < 2; ++i) { 180 if(fetch4[side]) { 181 ureg_TEX(shader, m[i][side], TGSI_TEXTURE_2D, ureg_src(t_tc[side]), sampler[side]); 182 ureg_MOV(shader, ureg_writemask(t_tc[side], TGSI_WRITEMASK_X), step[side]); 183 184 } else for(j = 0; j < 4; ++j) { 185 /* Nouveau and r600g can't writemask tex dst regs (yet?), do in two steps */ 186 ureg_TEX(shader, tmp[side], TGSI_TEXTURE_2D, ureg_src(t_tc[side]), sampler[side]); 187 ureg_MOV(shader, ureg_writemask(m[i][side], TGSI_WRITEMASK_X << j), ureg_scalar(ureg_src(tmp[side]), TGSI_SWIZZLE_X)); 188 189 ureg_ADD(shader, ureg_writemask(t_tc[side], TGSI_WRITEMASK_X << side), ureg_src(t_tc[side]), step[side]); 190 } 191 } 192 } 193 194 ureg_DP4(shader, ureg_writemask(tmp[0], TGSI_WRITEMASK_X), ureg_src(m[0][0]), ureg_src(m[0][1])); 195 ureg_DP4(shader, ureg_writemask(tmp[1], TGSI_WRITEMASK_X), ureg_src(m[1][0]), ureg_src(m[1][1])); 196 ureg_ADD(shader, ureg_writemask(tmp[0], TGSI_WRITEMASK_X), ureg_src(tmp[0]), ureg_src(tmp[1])); 197 ureg_MUL(shader, dst, ureg_src(tmp[0]), ureg_imm1f(shader, scale)); 198 199 for(i = 0; i < 2; ++i) { 200 ureg_release_temporary(shader, t_tc[i]); 201 for(j = 0; j < 2; ++j) 202 ureg_release_temporary(shader, m[i][j]); 203 ureg_release_temporary(shader, tmp[i]); 204 } 205} 206 207static void * 208create_transpose_frag_shader(struct vl_idct *idct) 209{ 210 struct ureg_program *shader; 211 struct ureg_src tc[2], sampler[2]; 212 struct ureg_src start[2], step[2]; 213 struct ureg_dst fragment; 214 bool fetch4[2]; 215 216 shader = ureg_create(TGSI_PROCESSOR_FRAGMENT); 217 if (!shader) 218 return NULL; 219 220 tc[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK, TGSI_INTERPOLATE_LINEAR); 221 tc[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX, TGSI_INTERPOLATE_LINEAR); 222 223 start[0] = ureg_imm1f(shader, 0.0f); 224 start[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_START, TGSI_INTERPOLATE_CONSTANT); 225 226 step[0] = ureg_imm1f(shader, 4.0f / BLOCK_HEIGHT); 227 step[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_STEP, TGSI_INTERPOLATE_CONSTANT); 228 229 sampler[0] = ureg_DECL_sampler(shader, 0); 230 sampler[1] = ureg_DECL_sampler(shader, 1); 231 232 fetch4[0] = true; 233 fetch4[1] = false; 234 235 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0); 236 237 matrix_mul(shader, fragment, tc, sampler, start, step, fetch4, STAGE1_SCALE); 238 239 ureg_END(shader); 240 241 return ureg_create_shader_and_destroy(shader, idct->pipe); 242} 243 244static void * 245create_matrix_frag_shader(struct vl_idct *idct) 246{ 247 struct ureg_program *shader; 248 struct ureg_src tc[2], sampler[2]; 249 struct ureg_src start[2], step[2]; 250 struct ureg_dst fragment; 251 bool fetch4[2]; 252 253 shader = ureg_create(TGSI_PROCESSOR_FRAGMENT); 254 if (!shader) 255 return NULL; 256 257 tc[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX, TGSI_INTERPOLATE_LINEAR); 258 tc[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK, TGSI_INTERPOLATE_LINEAR); 259 260 start[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_START, TGSI_INTERPOLATE_CONSTANT); 261 start[1] = ureg_imm1f(shader, 0.0f); 262 263 step[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_STEP, TGSI_INTERPOLATE_CONSTANT); 264 step[1] = ureg_imm1f(shader, 4.0f / BLOCK_WIDTH); 265 266 sampler[0] = ureg_DECL_sampler(shader, 1); 267 sampler[1] = ureg_DECL_sampler(shader, 0); 268 269 fetch4[0] = false; 270 fetch4[1] = true; 271 272 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0); 273 274 matrix_mul(shader, fragment, tc, sampler, start, step, fetch4, STAGE2_SCALE); 275 276 ureg_END(shader); 277 278 return ureg_create_shader_and_destroy(shader, idct->pipe); 279} 280 281static void * 282create_empty_block_frag_shader(struct vl_idct *idct) 283{ 284 struct ureg_program *shader; 285 struct ureg_dst fragment; 286 287 shader = ureg_create(TGSI_PROCESSOR_FRAGMENT); 288 if (!shader) 289 return NULL; 290 291 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0); 292 293 ureg_MOV(shader, fragment, ureg_imm1f(shader, 0.0f)); 294 295 ureg_END(shader); 296 297 return ureg_create_shader_and_destroy(shader, idct->pipe); 298} 299 300static void 301xfer_buffers_map(struct vl_idct *idct) 302{ 303 struct pipe_box rect = 304 { 305 0, 0, 0, 306 idct->destination->width0, 307 idct->destination->height0, 308 1 309 }; 310 311 idct->tex_transfer = idct->pipe->get_transfer 312 ( 313 idct->pipe, idct->textures.individual.source, 314 u_subresource(0, 0), 315 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 316 &rect 317 ); 318 319 idct->texels = idct->pipe->transfer_map(idct->pipe, idct->tex_transfer); 320 321 idct->vectors = pipe_buffer_map 322 ( 323 idct->pipe, 324 idct->vertex_bufs.individual.pos.buffer, 325 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 326 &idct->vec_transfer 327 ); 328} 329 330static void 331xfer_buffers_unmap(struct vl_idct *idct) 332{ 333 pipe_buffer_unmap(idct->pipe, idct->vertex_bufs.individual.pos.buffer, idct->vec_transfer); 334 335 idct->pipe->transfer_unmap(idct->pipe, idct->tex_transfer); 336 idct->pipe->transfer_destroy(idct->pipe, idct->tex_transfer); 337} 338 339static bool 340init_shaders(struct vl_idct *idct) 341{ 342 idct->vs = create_vert_shader(idct); 343 idct->transpose_fs = create_transpose_frag_shader(idct); 344 idct->matrix_fs = create_matrix_frag_shader(idct); 345 idct->eb_fs = create_empty_block_frag_shader(idct); 346 347 return 348 idct->vs != NULL && 349 idct->transpose_fs != NULL && 350 idct->matrix_fs != NULL && 351 idct->eb_fs != NULL; 352} 353 354static void 355cleanup_shaders(struct vl_idct *idct) 356{ 357 idct->pipe->delete_vs_state(idct->pipe, idct->vs); 358 idct->pipe->delete_fs_state(idct->pipe, idct->transpose_fs); 359 idct->pipe->delete_fs_state(idct->pipe, idct->matrix_fs); 360 idct->pipe->delete_fs_state(idct->pipe, idct->eb_fs); 361} 362 363static bool 364init_buffers(struct vl_idct *idct) 365{ 366 struct pipe_resource template; 367 struct pipe_sampler_view sampler_view; 368 struct pipe_vertex_element vertex_elems[2]; 369 unsigned i; 370 371 idct->max_blocks = 372 align(idct->destination->width0, BLOCK_WIDTH) / BLOCK_WIDTH * 373 align(idct->destination->height0, BLOCK_HEIGHT) / BLOCK_HEIGHT * 374 idct->destination->depth0; 375 376 memset(&template, 0, sizeof(struct pipe_resource)); 377 template.target = PIPE_TEXTURE_2D; 378 template.format = PIPE_FORMAT_R32G32B32A32_FLOAT; 379 template.last_level = 0; 380 template.width0 = 2; 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 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 484 unsigned i; 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 /* normalisation constants */ 499 vs_consts = pipe_buffer_map 500 ( 501 idct->pipe, idct->vs_const_buf, 502 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 503 &buf_transfer 504 ); 505 506 vs_consts->norm.x = 1.0f / idct->destination->width0; 507 vs_consts->norm.y = 1.0f / idct->destination->height0; 508 509 pipe_buffer_unmap(idct->pipe, idct->vs_const_buf, buf_transfer); 510} 511 512static void 513init_state(struct vl_idct *idct) 514{ 515 struct pipe_sampler_state sampler; 516 unsigned i; 517 518 idct->num_blocks = 0; 519 idct->num_empty_blocks = 0; 520 521 idct->viewport.scale[0] = idct->destination->width0; 522 idct->viewport.scale[1] = idct->destination->height0; 523 idct->viewport.scale[2] = 1; 524 idct->viewport.scale[3] = 1; 525 idct->viewport.translate[0] = 0; 526 idct->viewport.translate[1] = 0; 527 idct->viewport.translate[2] = 0; 528 idct->viewport.translate[3] = 0; 529 530 idct->fb_state.width = idct->destination->width0; 531 idct->fb_state.height = idct->destination->height0; 532 idct->fb_state.nr_cbufs = 1; 533 idct->fb_state.zsbuf = NULL; 534 535 for (i = 0; i < 4; ++i) { 536 memset(&sampler, 0, sizeof(sampler)); 537 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 538 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 539 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; 540 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST; 541 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; 542 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST; 543 sampler.compare_mode = PIPE_TEX_COMPARE_NONE; 544 sampler.compare_func = PIPE_FUNC_ALWAYS; 545 sampler.normalized_coords = 1; 546 /*sampler.shadow_ambient = ; */ 547 /*sampler.lod_bias = ; */ 548 sampler.min_lod = 0; 549 /*sampler.max_lod = ; */ 550 /*sampler.border_color[0] = ; */ 551 /*sampler.max_anisotropy = ; */ 552 idct->samplers.all[i] = idct->pipe->create_sampler_state(idct->pipe, &sampler); 553 } 554} 555 556static void 557cleanup_state(struct vl_idct *idct) 558{ 559 unsigned i; 560 561 for (i = 0; i < 4; ++i) 562 idct->pipe->delete_sampler_state(idct->pipe, idct->samplers.all[i]); 563} 564 565struct pipe_resource * 566vl_idct_upload_matrix(struct pipe_context *pipe) 567{ 568 struct pipe_resource template, *matrix; 569 struct pipe_transfer *buf_transfer; 570 unsigned i, j, pitch; 571 float *f; 572 573 struct pipe_box rect = 574 { 575 0, 0, 0, 576 BLOCK_WIDTH, 577 BLOCK_HEIGHT, 578 1 579 }; 580 581 memset(&template, 0, sizeof(struct pipe_resource)); 582 template.target = PIPE_TEXTURE_2D; 583 template.format = PIPE_FORMAT_R32G32B32A32_FLOAT; 584 template.last_level = 0; 585 template.width0 = 2; 586 template.height0 = 8; 587 template.depth0 = 1; 588 template.usage = PIPE_USAGE_IMMUTABLE; 589 template.bind = PIPE_BIND_SAMPLER_VIEW; 590 template.flags = 0; 591 592 matrix = pipe->screen->resource_create(pipe->screen, &template); 593 594 /* matrix */ 595 buf_transfer = pipe->get_transfer 596 ( 597 pipe, matrix, 598 u_subresource(0, 0), 599 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD, 600 &rect 601 ); 602 pitch = buf_transfer->stride / util_format_get_blocksize(buf_transfer->resource->format); 603 604 f = pipe->transfer_map(pipe, buf_transfer); 605 for(i = 0; i < BLOCK_HEIGHT; ++i) 606 for(j = 0; j < BLOCK_WIDTH; ++j) 607 f[i * pitch * 4 + j] = const_matrix[j][i]; // transpose 608 609 pipe->transfer_unmap(pipe, buf_transfer); 610 pipe->transfer_destroy(pipe, buf_transfer); 611 612 return matrix; 613} 614 615bool 616vl_idct_init(struct vl_idct *idct, struct pipe_context *pipe, struct pipe_resource *dst, struct pipe_resource *matrix) 617{ 618 assert(idct && pipe && dst); 619 620 idct->pipe = pipe; 621 pipe_resource_reference(&idct->textures.individual.matrix, matrix); 622 pipe_resource_reference(&idct->textures.individual.transpose, matrix); 623 pipe_resource_reference(&idct->destination, dst); 624 625 init_state(idct); 626 627 if(!init_shaders(idct)) 628 return false; 629 630 if(!init_buffers(idct)) { 631 cleanup_shaders(idct); 632 return false; 633 } 634 635 idct->surfaces.intermediate = idct->pipe->screen->get_tex_surface( 636 idct->pipe->screen, idct->textures.individual.intermediate, 0, 0, 0, 637 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET); 638 639 idct->surfaces.destination = idct->pipe->screen->get_tex_surface( 640 idct->pipe->screen, idct->destination, 0, 0, 0, 641 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET); 642 643 init_constants(idct); 644 xfer_buffers_map(idct); 645 646 return true; 647} 648 649void 650vl_idct_cleanup(struct vl_idct *idct) 651{ 652 idct->pipe->screen->tex_surface_destroy(idct->surfaces.destination); 653 idct->pipe->screen->tex_surface_destroy(idct->surfaces.intermediate); 654 655 cleanup_shaders(idct); 656 cleanup_buffers(idct); 657 658 cleanup_state(idct); 659 660 pipe_resource_reference(&idct->destination, NULL); 661} 662 663void 664vl_idct_add_block(struct vl_idct *idct, unsigned x, unsigned y, short *block) 665{ 666 struct vertex2f v, *v_dst; 667 668 unsigned tex_pitch; 669 short *texels; 670 671 unsigned i; 672 673 assert(idct); 674 675 if(block) { 676 tex_pitch = idct->tex_transfer->stride / util_format_get_blocksize(idct->tex_transfer->resource->format); 677 texels = idct->texels + y * tex_pitch * BLOCK_HEIGHT + x * BLOCK_WIDTH; 678 679 for (i = 0; i < BLOCK_HEIGHT; ++i) 680 memcpy(texels + i * tex_pitch, block + i * BLOCK_WIDTH, BLOCK_WIDTH * 2); 681 682 /* non empty blocks fills the vector buffer from left to right */ 683 v_dst = idct->vectors + idct->num_blocks * 4; 684 685 idct->num_blocks++; 686 687 } else { 688 689 /* while empty blocks fills the vector buffer from right to left */ 690 v_dst = idct->vectors + (idct->max_blocks - idct->num_empty_blocks) * 4 - 4; 691 692 idct->num_empty_blocks++; 693 } 694 695 v.x = x; 696 v.y = y; 697 698 for (i = 0; i < 4; ++i) { 699 v_dst[i] = v; 700 } 701} 702 703void 704vl_idct_flush(struct vl_idct *idct) 705{ 706 xfer_buffers_unmap(idct); 707 708 idct->pipe->set_constant_buffer(idct->pipe, PIPE_SHADER_VERTEX, 0, idct->vs_const_buf); 709 710 if(idct->num_blocks > 0) { 711 712 /* first stage */ 713 idct->fb_state.cbufs[0] = idct->surfaces.intermediate; 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, 2, idct->sampler_views.stage[0]); 720 idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[0]); 721 idct->pipe->bind_vs_state(idct->pipe, idct->vs); 722 idct->pipe->bind_fs_state(idct->pipe, idct->transpose_fs); 723 724 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, idct->num_blocks * 4); 725 726 /* second stage */ 727 idct->fb_state.cbufs[0] = idct->surfaces.destination; 728 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state); 729 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport); 730 731 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all); 732 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 733 idct->pipe->set_fragment_sampler_views(idct->pipe, 2, idct->sampler_views.stage[1]); 734 idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[1]); 735 idct->pipe->bind_vs_state(idct->pipe, idct->vs); 736 idct->pipe->bind_fs_state(idct->pipe, idct->matrix_fs); 737 738 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, idct->num_blocks * 4); 739 } 740 741 if(idct->num_empty_blocks > 0) { 742 743 /* empty block handling */ 744 idct->fb_state.cbufs[0] = idct->surfaces.destination; 745 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state); 746 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport); 747 748 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all); 749 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state); 750 idct->pipe->set_fragment_sampler_views(idct->pipe, 4, idct->sampler_views.all); 751 idct->pipe->bind_fragment_sampler_states(idct->pipe, 4, idct->samplers.all); 752 idct->pipe->bind_vs_state(idct->pipe, idct->vs); 753 idct->pipe->bind_fs_state(idct->pipe, idct->eb_fs); 754 755 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 756 (idct->max_blocks - idct->num_empty_blocks) * 4, 757 idct->num_empty_blocks * 4); 758 } 759 760 idct->num_blocks = 0; 761 idct->num_empty_blocks = 0; 762 xfer_buffers_map(idct); 763} 764