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