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