vl_idct.c revision ab130400cf91ab471e265e58193c95f04c7aeeda
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
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_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
142static void
143fetch_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
177static void
178matrix_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
202static void *
203create_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
246static void *
247create_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
319static bool
320init_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
332static void
333cleanup_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
340static bool
341init_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
394static void
395cleanup_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
406static bool
407init_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
449static void
450cleanup_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
462static bool
463init_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
482static void
483cleanup_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
493struct pipe_resource *
494vl_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      u_subresource(0, 0),
527      PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
528      &rect
529   );
530   pitch = buf_transfer->stride / sizeof(float);
531
532   f = pipe->transfer_map(pipe, buf_transfer);
533   for(i = 0; i < BLOCK_HEIGHT; ++i)
534      for(j = 0; j < BLOCK_WIDTH; ++j)
535         // transpose and scale
536         f[i * pitch + j] = const_matrix[j][i] * STAGE1_SCALE;
537
538   pipe->transfer_unmap(pipe, buf_transfer);
539   pipe->transfer_destroy(pipe, buf_transfer);
540
541   return matrix;
542}
543
544bool vl_idct_init(struct vl_idct *idct, struct pipe_context *pipe,
545                  unsigned buffer_width, unsigned buffer_height,
546                  struct pipe_resource *matrix)
547{
548   assert(idct && pipe && matrix);
549
550   idct->pipe = pipe;
551   idct->buffer_width = buffer_width;
552   idct->buffer_height = buffer_height;
553   pipe_resource_reference(&idct->matrix, matrix);
554
555   idct->max_blocks =
556      align(buffer_width, BLOCK_WIDTH) / BLOCK_WIDTH *
557      align(buffer_height, BLOCK_HEIGHT) / BLOCK_HEIGHT;
558
559   if(!init_shaders(idct))
560      return false;
561
562   if(!init_state(idct)) {
563      cleanup_shaders(idct);
564      return false;
565   }
566
567   return true;
568}
569
570void
571vl_idct_cleanup(struct vl_idct *idct)
572{
573   cleanup_shaders(idct);
574   cleanup_state(idct);
575
576   pipe_resource_reference(&idct->matrix, NULL);
577}
578
579bool
580vl_idct_init_buffer(struct vl_idct *idct, struct vl_idct_buffer *buffer, struct pipe_resource *dst)
581{
582   unsigned i;
583
584   assert(buffer);
585   assert(idct);
586   assert(dst);
587
588   pipe_resource_reference(&buffer->textures.individual.matrix, idct->matrix);
589   pipe_resource_reference(&buffer->textures.individual.transpose, idct->matrix);
590   pipe_resource_reference(&buffer->destination, dst);
591
592   if (!init_textures(idct, buffer))
593      return false;
594
595   if (!init_vertex_buffers(idct, buffer))
596      return false;
597
598   /* init state */
599   buffer->viewport[0].scale[0] = buffer->textures.individual.intermediate->width0;
600   buffer->viewport[0].scale[1] = buffer->textures.individual.intermediate->height0;
601
602   buffer->viewport[1].scale[0] = buffer->destination->width0;
603   buffer->viewport[1].scale[1] = buffer->destination->height0;
604
605   buffer->fb_state[0].width = buffer->textures.individual.intermediate->width0;
606   buffer->fb_state[0].height = buffer->textures.individual.intermediate->height0;
607
608   buffer->fb_state[0].nr_cbufs = NR_RENDER_TARGETS;
609   for(i = 0; i < NR_RENDER_TARGETS; ++i) {
610      buffer->fb_state[0].cbufs[i] = idct->pipe->screen->get_tex_surface(
611         idct->pipe->screen, buffer->textures.individual.intermediate, 0, 0, i,
612         PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET);
613   }
614
615   buffer->fb_state[1].width = buffer->destination->width0;
616   buffer->fb_state[1].height = buffer->destination->height0;
617
618   buffer->fb_state[1].nr_cbufs = 1;
619   buffer->fb_state[1].cbufs[0] = idct->pipe->screen->get_tex_surface(
620      idct->pipe->screen, buffer->destination, 0, 0, 0,
621      PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET);
622
623   for(i = 0; i < 2; ++i) {
624      buffer->viewport[i].scale[2] = 1;
625      buffer->viewport[i].scale[3] = 1;
626      buffer->viewport[i].translate[0] = 0;
627      buffer->viewport[i].translate[1] = 0;
628      buffer->viewport[i].translate[2] = 0;
629      buffer->viewport[i].translate[3] = 0;
630
631      buffer->fb_state[i].zsbuf = NULL;
632   }
633
634   return true;
635}
636
637void
638vl_idct_cleanup_buffer(struct vl_idct *idct, struct vl_idct_buffer *buffer)
639{
640   unsigned i;
641
642   assert(buffer);
643
644   for(i = 0; i < NR_RENDER_TARGETS; ++i) {
645      idct->pipe->screen->tex_surface_destroy(buffer->fb_state[0].cbufs[i]);
646   }
647
648   idct->pipe->screen->tex_surface_destroy(buffer->fb_state[1].cbufs[0]);
649
650   cleanup_textures(idct, buffer);
651   cleanup_vertex_buffers(idct, buffer);
652}
653
654void
655vl_idct_map_buffers(struct vl_idct *idct, struct vl_idct_buffer *buffer)
656{
657   assert(idct);
658
659   struct pipe_box rect =
660   {
661      0, 0, 0,
662      buffer->textures.individual.source->width0,
663      buffer->textures.individual.source->height0,
664      1
665   };
666
667   buffer->tex_transfer = idct->pipe->get_transfer
668   (
669      idct->pipe, buffer->textures.individual.source,
670      u_subresource(0, 0),
671      PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD,
672      &rect
673   );
674
675   buffer->texels = idct->pipe->transfer_map(idct->pipe, buffer->tex_transfer);
676
677   vl_vb_map(&buffer->blocks, idct->pipe);
678}
679
680void
681vl_idct_add_block(struct vl_idct_buffer *buffer, unsigned x, unsigned y, short *block)
682{
683   struct vertex2f v;
684   unsigned tex_pitch;
685   short *texels;
686
687   unsigned i;
688
689   assert(buffer);
690
691   tex_pitch = buffer->tex_transfer->stride / sizeof(short);
692   texels = buffer->texels + y * tex_pitch * BLOCK_HEIGHT + x * BLOCK_WIDTH;
693
694   for (i = 0; i < BLOCK_HEIGHT; ++i)
695      memcpy(texels + i * tex_pitch, block + i * BLOCK_WIDTH, BLOCK_WIDTH * sizeof(short));
696
697   v.x = x;
698   v.y = y;
699   vl_vb_add_block(&buffer->blocks, (float*)&v);
700}
701
702void
703vl_idct_unmap_buffers(struct vl_idct *idct, struct vl_idct_buffer *buffer)
704{
705   assert(idct && buffer);
706
707   idct->pipe->transfer_unmap(idct->pipe, buffer->tex_transfer);
708   idct->pipe->transfer_destroy(idct->pipe, buffer->tex_transfer);
709   vl_vb_unmap(&buffer->blocks, idct->pipe);
710}
711
712void
713vl_idct_flush(struct vl_idct *idct, struct vl_idct_buffer *buffer)
714{
715   unsigned num_verts;
716
717   assert(idct);
718
719   num_verts = vl_vb_restart(&buffer->blocks);
720
721   if(num_verts > 0) {
722
723      idct->pipe->bind_rasterizer_state(idct->pipe, idct->rs_state);
724      idct->pipe->set_vertex_buffers(idct->pipe, 2, buffer->vertex_bufs.all);
725      idct->pipe->bind_vertex_elements_state(idct->pipe, idct->vertex_elems_state);
726      idct->pipe->bind_vs_state(idct->pipe, idct->vs);
727
728      /* first stage */
729      idct->pipe->set_framebuffer_state(idct->pipe, &buffer->fb_state[0]);
730      idct->pipe->set_viewport_state(idct->pipe, &buffer->viewport[0]);
731      idct->pipe->set_fragment_sampler_views(idct->pipe, 2, buffer->sampler_views.stage[0]);
732      idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[0]);
733      idct->pipe->bind_fs_state(idct->pipe, idct->matrix_fs);
734      util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, num_verts);
735
736      /* second stage */
737      idct->pipe->set_framebuffer_state(idct->pipe, &buffer->fb_state[1]);
738      idct->pipe->set_viewport_state(idct->pipe, &buffer->viewport[1]);
739      idct->pipe->set_fragment_sampler_views(idct->pipe, 2, buffer->sampler_views.stage[1]);
740      idct->pipe->bind_fragment_sampler_states(idct->pipe, 2, idct->samplers.stage[1]);
741      idct->pipe->bind_fs_state(idct->pipe, idct->transpose_fs);
742      util_draw_arrays(idct->pipe, PIPE_PRIM_QUADS, 0, num_verts);
743   }
744}
745