[g3dvl] next round of idct implementation
[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 "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_9_TO_16 (256.0f / 32768.0f)
43
44 struct vertex_shader_consts
45 {
46 struct vertex4f norm;
47 };
48
49 enum VS_INPUT
50 {
51 VS_I_RECT,
52 VS_I_VPOS,
53
54 NUM_VS_INPUTS
55 };
56
57 enum 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
67 static 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
78 static 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 */
90 static 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
94 static void *
95 create_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
155 static void
156 matrix_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
214 static void *
215 create_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 fragment;
221 float scale[2];
222
223 shader = ureg_create(TGSI_PROCESSOR_FRAGMENT);
224 if (!shader)
225 return NULL;
226
227 tc[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK, TGSI_INTERPOLATE_LINEAR);
228 tc[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX, TGSI_INTERPOLATE_LINEAR);
229
230 start[0] = ureg_imm1f(shader, 0.0f);
231 start[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_START, TGSI_INTERPOLATE_CONSTANT);
232
233 step[0] = ureg_imm1f(shader, 1.0f / BLOCK_HEIGHT);
234 step[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_STEP, TGSI_INTERPOLATE_CONSTANT);
235
236 sampler[0] = ureg_DECL_sampler(shader, 0);
237 sampler[1] = ureg_DECL_sampler(shader, 2);
238
239 scale[0] = 1.0f;
240 scale[1] = SCALE_FACTOR_16_TO_12;
241
242 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
243
244 //matrix_mul(shader, fragment, tc, sampler, start, step, scale);
245 //ureg_MOV(shader, fragment, ureg_imm1f(shader, 0.0f));
246
247 ureg_END(shader);
248
249 return ureg_create_shader_and_destroy(shader, idct->pipe);
250 }
251
252 static void *
253 create_matrix_frag_shader(struct vl_idct *idct)
254 {
255 struct ureg_program *shader;
256 struct ureg_src tc[2], sampler[2];
257 struct ureg_src start[2], step[2];
258 struct ureg_dst tmp, fragment;
259 float scale[2];
260
261 shader = ureg_create(TGSI_PROCESSOR_FRAGMENT);
262 if (!shader)
263 return NULL;
264
265 tmp = ureg_DECL_temporary(shader);
266
267 tc[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX, TGSI_INTERPOLATE_LINEAR);
268 tc[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_BLOCK, TGSI_INTERPOLATE_LINEAR);
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 step[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_STEP, TGSI_INTERPOLATE_CONSTANT);
274 step[1] = ureg_imm1f(shader, 1.0f / BLOCK_WIDTH);
275
276 sampler[0] = ureg_DECL_sampler(shader, 3);
277 sampler[1] = ureg_DECL_sampler(shader, 1);
278
279 scale[0] = 1.0f;
280 scale[1] = 1.0f;
281
282 fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
283
284 // matrix_mul(shader, tmp, tc, sampler, start, step, scale);
285 // ureg_MUL(shader, fragment, ureg_src(tmp), ureg_scalar(ureg_imm1f(shader, SCALE_FACTOR_9_TO_16), TGSI_SWIZZLE_X));
286 ureg_TEX(shader, fragment, TGSI_TEXTURE_2D, tc[0], sampler[0]);
287 //ureg_MUL(shader, , ureg_scalar(ureg_src(tmp), TGSI_SWIZZLE_X), ureg_imm1f(shader, 1.0f));
288
289 ureg_END(shader);
290
291 return ureg_create_shader_and_destroy(shader, idct->pipe);
292 }
293
294 static void
295 xfer_buffers_map(struct vl_idct *idct)
296 {
297 struct pipe_box rect =
298 {
299 0, 0, 0,
300 idct->destination->width0,
301 idct->destination->height0,
302 1
303 };
304
305 idct->tex_transfer = idct->pipe->get_transfer
306 (
307 #if 0
308 idct->pipe, idct->textures.individual.intermediate,
309 #else
310 idct->pipe, idct->destination,
311 #endif
312 u_subresource(0, 0),
313 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
314 &rect
315 );
316
317 idct->texels = idct->pipe->transfer_map(idct->pipe, idct->tex_transfer);
318
319 idct->vectors = pipe_buffer_map
320 (
321 idct->pipe,
322 idct->vertex_bufs.individual.pos.buffer,
323 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
324 &idct->vec_transfer
325 );
326 }
327
328 static void
329 xfer_buffers_unmap(struct vl_idct *idct)
330 {
331 pipe_buffer_unmap(idct->pipe, idct->vertex_bufs.individual.pos.buffer, idct->vec_transfer);
332
333 idct->pipe->transfer_unmap(idct->pipe, idct->tex_transfer);
334 idct->pipe->transfer_destroy(idct->pipe, idct->tex_transfer);
335 }
336
337 static bool
338 init_shaders(struct vl_idct *idct)
339 {
340 assert(idct);
341
342 assert(idct->vs = create_vert_shader(idct));
343 assert(idct->transpose_fs = create_transpose_frag_shader(idct));
344 assert(idct->matrix_fs = create_matrix_frag_shader(idct));
345
346 return true;
347 }
348
349 static void
350 cleanup_shaders(struct vl_idct *idct)
351 {
352 assert(idct);
353
354 idct->pipe->delete_vs_state(idct->pipe, idct->vs);
355 idct->pipe->delete_fs_state(idct->pipe, idct->transpose_fs);
356 idct->pipe->delete_fs_state(idct->pipe, idct->matrix_fs);
357 }
358
359 static bool
360 init_buffers(struct vl_idct *idct)
361 {
362 struct pipe_resource template;
363 struct pipe_sampler_view sampler_view;
364 struct pipe_vertex_element vertex_elems[2];
365
366 const unsigned max_blocks =
367 align(idct->destination->width0, BLOCK_WIDTH) / BLOCK_WIDTH *
368 align(idct->destination->height0, BLOCK_HEIGHT) / BLOCK_HEIGHT *
369 idct->destination->depth0;
370
371 unsigned i;
372
373 memset(&template, 0, sizeof(struct pipe_resource));
374 template.target = PIPE_TEXTURE_2D;
375 template.format = PIPE_FORMAT_R32_FLOAT;
376 template.last_level = 0;
377 template.width0 = 8;
378 template.height0 = 8;
379 template.depth0 = 1;
380 template.usage = PIPE_USAGE_IMMUTABLE;
381 template.bind = PIPE_BIND_SAMPLER_VIEW;
382 template.flags = 0;
383
384 idct->textures.individual.matrix = idct->pipe->screen->resource_create(idct->pipe->screen, &template);
385 idct->textures.individual.transpose = idct->pipe->screen->resource_create(idct->pipe->screen, &template);
386
387 template.format = idct->destination->format;
388 template.width0 = idct->destination->width0;
389 template.height0 = idct->destination->height0;
390 template.depth0 = idct->destination->depth0;
391 template.usage = PIPE_USAGE_DYNAMIC;
392 idct->textures.individual.source = idct->pipe->screen->resource_create(idct->pipe->screen, &template);
393
394 //template.format = PIPE_FORMAT_R32_FLOAT;
395 //template.usage = PIPE_USAGE_STATIC;
396 idct->textures.individual.intermediate = idct->pipe->screen->resource_create(idct->pipe->screen, &template);
397
398 for (i = 0; i < 4; ++i) {
399 u_sampler_view_default_template(&sampler_view, idct->textures.all[i], idct->textures.all[i]->format);
400 idct->sampler_views.all[i] = idct->pipe->create_sampler_view(idct->pipe, idct->textures.all[i], &sampler_view);
401 }
402
403 idct->vertex_bufs.individual.quad.stride = sizeof(struct vertex2f);
404 idct->vertex_bufs.individual.quad.max_index = 4 * max_blocks - 1;
405 idct->vertex_bufs.individual.quad.buffer_offset = 0;
406 idct->vertex_bufs.individual.quad.buffer = pipe_buffer_create
407 (
408 idct->pipe->screen,
409 PIPE_BIND_VERTEX_BUFFER,
410 sizeof(struct vertex2f) * 4 * max_blocks
411 );
412
413 idct->vertex_bufs.individual.pos.stride = sizeof(struct vertex2f);
414 idct->vertex_bufs.individual.pos.max_index = 4 * max_blocks - 1;
415 idct->vertex_bufs.individual.pos.buffer_offset = 0;
416 idct->vertex_bufs.individual.pos.buffer = pipe_buffer_create
417 (
418 idct->pipe->screen,
419 PIPE_BIND_VERTEX_BUFFER,
420 sizeof(struct vertex2f) * 4 * max_blocks
421 );
422
423 /* Rect element */
424 vertex_elems[0].src_offset = 0;
425 vertex_elems[0].instance_divisor = 0;
426 vertex_elems[0].vertex_buffer_index = 0;
427 vertex_elems[0].src_format = PIPE_FORMAT_R32G32_FLOAT;
428
429 /* Pos element */
430 vertex_elems[1].src_offset = 0;
431 vertex_elems[1].instance_divisor = 0;
432 vertex_elems[1].vertex_buffer_index = 1;
433 vertex_elems[1].src_format = PIPE_FORMAT_R32G32_FLOAT;
434
435 idct->vertex_elems_state = idct->pipe->create_vertex_elements_state(idct->pipe, 2, vertex_elems);
436
437 idct->vs_const_buf = pipe_buffer_create
438 (
439 idct->pipe->screen,
440 PIPE_BIND_CONSTANT_BUFFER,
441 sizeof(struct vertex_shader_consts)
442 );
443
444 return true;
445 }
446
447 static void
448 cleanup_buffers(struct vl_idct *idct)
449 {
450 unsigned i;
451
452 assert(idct);
453
454 pipe_resource_reference(&idct->vs_const_buf, NULL);
455
456 for (i = 0; i < 4; ++i) {
457 pipe_sampler_view_reference(&idct->sampler_views.all[i], NULL);
458 pipe_resource_reference(&idct->textures.all[i], NULL);
459 }
460
461 idct->pipe->delete_vertex_elements_state(idct->pipe, idct->vertex_elems_state);
462 pipe_resource_reference(&idct->vertex_bufs.individual.quad.buffer, NULL);
463 pipe_resource_reference(&idct->vertex_bufs.individual.pos.buffer, NULL);
464 }
465
466 static void
467 init_constants(struct vl_idct *idct)
468 {
469 struct pipe_transfer *buf_transfer;
470 struct vertex_shader_consts *vs_consts;
471 struct vertex2f *v;
472
473 unsigned i;
474
475 v = pipe_buffer_map
476 (
477 idct->pipe,
478 idct->vertex_bufs.individual.quad.buffer,
479 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
480 &buf_transfer
481 );
482
483 for ( i = 0; i <= idct->vertex_bufs.individual.quad.max_index; i += 4)
484 memcpy(v + i, &const_quad, sizeof(const_quad));
485
486 pipe_buffer_unmap(idct->pipe, idct->vertex_bufs.individual.quad.buffer, buf_transfer);
487
488
489 v = pipe_buffer_map
490 (
491 idct->pipe,
492 idct->textures.individual.matrix,
493 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
494 &buf_transfer
495 );
496
497 memcpy(v, &const_matrix, sizeof(const_matrix));
498
499 pipe_buffer_unmap(idct->pipe, idct->textures.individual.matrix, buf_transfer);
500
501 v = pipe_buffer_map
502 (
503 idct->pipe,
504 idct->textures.individual.transpose,
505 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
506 &buf_transfer
507 );
508
509 memcpy(v, &const_transpose, sizeof(const_transpose));
510
511 pipe_buffer_unmap(idct->pipe, idct->textures.individual.transpose, buf_transfer);
512
513 vs_consts = pipe_buffer_map
514 (
515 idct->pipe, idct->vs_const_buf,
516 PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
517 &buf_transfer
518 );
519
520 vs_consts->norm.x = 1.0f / idct->destination->width0;
521 vs_consts->norm.y = 1.0f / idct->destination->height0;
522
523 pipe_buffer_unmap(idct->pipe, idct->vs_const_buf, buf_transfer);
524 }
525
526 static void
527 init_state(struct vl_idct *idct)
528 {
529 struct pipe_sampler_state sampler;
530 unsigned i;
531
532 idct->num_blocks = 0;
533
534 idct->viewport.scale[0] = idct->destination->width0;
535 idct->viewport.scale[1] = idct->destination->height0;
536 idct->viewport.scale[2] = 1;
537 idct->viewport.scale[3] = 1;
538 idct->viewport.translate[0] = 0;
539 idct->viewport.translate[1] = 0;
540 idct->viewport.translate[2] = 0;
541 idct->viewport.translate[3] = 0;
542
543 idct->fb_state.width = idct->destination->width0;
544 idct->fb_state.height = idct->destination->height0;
545 idct->fb_state.nr_cbufs = 1;
546 idct->fb_state.zsbuf = NULL;
547
548 for (i = 0; i < 4; ++i) {
549 memset(&sampler, 0, sizeof(sampler));
550 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
551 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
552 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
553 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
554 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
555 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
556 sampler.compare_mode = PIPE_TEX_COMPARE_NONE;
557 sampler.compare_func = PIPE_FUNC_ALWAYS;
558 sampler.normalized_coords = 1;
559 /*sampler.shadow_ambient = ; */
560 /*sampler.lod_bias = ; */
561 sampler.min_lod = 0;
562 /*sampler.max_lod = ; */
563 /*sampler.border_color[0] = ; */
564 /*sampler.max_anisotropy = ; */
565 idct->samplers.all[i] = idct->pipe->create_sampler_state(idct->pipe, &sampler);
566 }
567 }
568
569 static void
570 cleanup_state(struct vl_idct *idct)
571 {
572 unsigned i;
573
574 for (i = 0; i < 4; ++i)
575 idct->pipe->delete_sampler_state(idct->pipe, idct->samplers.all[i]);
576 }
577
578 bool vl_idct_init(struct vl_idct *idct, struct pipe_context *pipe, struct pipe_resource *dst)
579 {
580 assert(idct && pipe && dst);
581
582 idct->pipe = pipe;
583 pipe_resource_reference(&idct->destination, dst);
584
585 init_state(idct);
586
587 if(!init_shaders(idct))
588 return false;
589
590 if(!init_buffers(idct)) {
591 cleanup_shaders(idct);
592 return false;
593 }
594
595 idct->surfaces.intermediate = idct->pipe->screen->get_tex_surface(
596 idct->pipe->screen, idct->textures.individual.intermediate, 0, 0, 0,
597 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET);
598
599 idct->surfaces.destination = idct->pipe->screen->get_tex_surface(
600 idct->pipe->screen, idct->destination, 0, 0, 0,
601 PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET);
602
603 init_constants(idct);
604 xfer_buffers_map(idct);
605
606 return true;
607 }
608
609 void vl_idct_cleanup(struct vl_idct *idct)
610 {
611 idct->pipe->screen->tex_surface_destroy(idct->surfaces.destination);
612 idct->pipe->screen->tex_surface_destroy(idct->surfaces.intermediate);
613
614 cleanup_shaders(idct);
615 cleanup_buffers(idct);
616
617 cleanup_state(idct);
618
619 pipe_resource_reference(&idct->destination, NULL);
620 }
621
622 void vl_idct_add_block(struct vl_idct *idct, unsigned x, unsigned y, short *block)
623 {
624 struct vertex2f v;
625
626 unsigned tex_pitch;
627 short *texels;
628
629 unsigned i;
630
631 assert(idct);
632 assert(block);
633
634 v.x = x;
635 v.y = y;
636
637 for (i = 0; i < 4; ++i) {
638 idct->vectors[idct->num_blocks * 4 + i] = v;
639 }
640
641 tex_pitch = idct->tex_transfer->stride / util_format_get_blocksize(idct->tex_transfer->resource->format);
642 texels = idct->texels + y * tex_pitch * BLOCK_HEIGHT + x * BLOCK_WIDTH;
643
644 for (i = 0; i < BLOCK_HEIGHT; ++i)
645 memcpy(texels + i * tex_pitch, block + i * BLOCK_WIDTH, BLOCK_WIDTH * 2);
646
647 idct->num_blocks++;
648 }
649
650 void vl_idct_flush(struct vl_idct *idct)
651 {
652 xfer_buffers_unmap(idct);
653
654 idct->pipe->set_constant_buffer(idct->pipe, PIPE_SHADER_VERTEX, 0, idct->vs_const_buf);
655
656 #if 0
657 /* first stage */
658 idct->fb_state.cbufs[0] = idct->surfaces.intermediate;
659 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state);
660 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport);
661
662 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all);
663 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state);
664 idct->pipe->set_fragment_sampler_views(idct->pipe, 4, idct->sampler_views.all);
665 idct->pipe->bind_fragment_sampler_states(idct->pipe, 4, idct->samplers.all);
666 idct->pipe->bind_vs_state(idct->pipe, idct->vs);
667 idct->pipe->bind_fs_state(idct->pipe, idct->transpose_fs);
668
669 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, idct->num_blocks * 4);
670
671 /* second stage */
672 idct->fb_state.cbufs[0] = idct->surfaces.destination;
673 idct->pipe->set_framebuffer_state(idct->pipe, &idct->fb_state);
674 idct->pipe->set_viewport_state(idct->pipe, &idct->viewport);
675
676 idct->pipe->set_vertex_buffers(idct->pipe, 2, idct->vertex_bufs.all);
677 idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state);
678 idct->pipe->set_fragment_sampler_views(idct->pipe, 4, idct->sampler_views.all);
679 idct->pipe->bind_fragment_sampler_states(idct->pipe, 4, idct->samplers.all);
680 idct->pipe->bind_vs_state(idct->pipe, idct->vs);
681 idct->pipe->bind_fs_state(idct->pipe, idct->matrix_fs);
682
683 util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, idct->num_blocks * 4);
684
685 idct->pipe->flush(idct->pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
686 #endif
687
688 idct->num_blocks = 0;
689 xfer_buffers_map(idct);
690 }