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