1 /**************************************************************************
3 * Copyright 2010 Christian König
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 #include "util/u_draw.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>
40 #define BLOCK_HEIGHT 8
42 #define SCALE_FACTOR_16_TO_9 (32768.0f / 256.0f)
44 #define STAGE1_SCALE 4.0f
45 #define STAGE2_SCALE (SCALE_FACTOR_16_TO_9 / STAGE1_SCALE)
47 struct vertex_shader_consts
69 static const float const_matrix
[8][8] = {
70 { 0.3535530f
, 0.3535530f
, 0.3535530f
, 0.3535530f
, 0.3535530f
, 0.3535530f
, 0.353553f
, 0.3535530f
},
71 { 0.4903930f
, 0.4157350f
, 0.2777850f
, 0.0975451f
, -0.0975452f
, -0.2777850f
, -0.415735f
, -0.4903930f
},
72 { 0.4619400f
, 0.1913420f
, -0.1913420f
, -0.4619400f
, -0.4619400f
, -0.1913420f
, 0.191342f
, 0.4619400f
},
73 { 0.4157350f
, -0.0975452f
, -0.4903930f
, -0.2777850f
, 0.2777850f
, 0.4903930f
, 0.097545f
, -0.4157350f
},
74 { 0.3535530f
, -0.3535530f
, -0.3535530f
, 0.3535540f
, 0.3535530f
, -0.3535540f
, -0.353553f
, 0.3535530f
},
75 { 0.2777850f
, -0.4903930f
, 0.0975452f
, 0.4157350f
, -0.4157350f
, -0.0975451f
, 0.490393f
, -0.2777850f
},
76 { 0.1913420f
, -0.4619400f
, 0.4619400f
, -0.1913420f
, -0.1913410f
, 0.4619400f
, -0.461940f
, 0.1913420f
},
77 { 0.0975451f
, -0.2777850f
, 0.4157350f
, -0.4903930f
, 0.4903930f
, -0.4157350f
, 0.277786f
, -0.0975458f
}
80 /* vertices for a quad covering a block */
81 static const struct vertex2f const_quad
[4] = {
82 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
86 create_vert_shader(struct vl_idct
*idct
)
88 struct ureg_program
*shader
;
89 struct ureg_src norm
, bs
;
90 struct ureg_src vrect
, vpos
;
91 struct ureg_dst scale
, t_vpos
;
92 struct ureg_dst o_vpos
, o_block
, o_tex
, o_start
, o_step
;
94 shader
= ureg_create(TGSI_PROCESSOR_VERTEX
);
98 norm
= ureg_DECL_constant(shader
, 0);
99 bs
= ureg_imm2f(shader
, BLOCK_WIDTH
, BLOCK_HEIGHT
);
101 scale
= ureg_DECL_temporary(shader
);
102 t_vpos
= ureg_DECL_temporary(shader
);
104 vrect
= ureg_DECL_vs_input(shader
, VS_I_RECT
);
105 vpos
= ureg_DECL_vs_input(shader
, VS_I_VPOS
);
107 o_vpos
= ureg_DECL_output(shader
, TGSI_SEMANTIC_POSITION
, VS_O_VPOS
);
108 o_block
= ureg_DECL_output(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_BLOCK
);
109 o_tex
= ureg_DECL_output(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_TEX
);
110 o_start
= ureg_DECL_output(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_START
);
111 o_step
= ureg_DECL_output(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_STEP
);
114 * scale = norm * mbs;
116 * t_vpos = vpos + vrect
117 * o_vpos.xy = t_vpos * scale
122 * o_start = vpos * scale
126 ureg_MUL(shader
, ureg_writemask(scale
, TGSI_WRITEMASK_XY
), norm
, bs
);
128 ureg_ADD(shader
, ureg_writemask(t_vpos
, TGSI_WRITEMASK_XY
), vpos
, vrect
);
129 ureg_MUL(shader
, ureg_writemask(t_vpos
, TGSI_WRITEMASK_XY
), ureg_src(t_vpos
), ureg_src(scale
));
130 ureg_MOV(shader
, ureg_writemask(o_vpos
, TGSI_WRITEMASK_XY
), ureg_src(t_vpos
));
131 ureg_MOV(shader
, ureg_writemask(o_vpos
, TGSI_WRITEMASK_ZW
), vpos
);
133 ureg_MOV(shader
, ureg_writemask(o_tex
, TGSI_WRITEMASK_XY
), ureg_src(t_vpos
));
134 ureg_MOV(shader
, ureg_writemask(o_block
, TGSI_WRITEMASK_XY
), vrect
);
135 ureg_MUL(shader
, ureg_writemask(o_start
, TGSI_WRITEMASK_XY
), vpos
, ureg_src(scale
));
136 ureg_MOV(shader
, ureg_writemask(o_step
, TGSI_WRITEMASK_XY
), norm
);
138 ureg_release_temporary(shader
, t_vpos
);
139 ureg_release_temporary(shader
, scale
);
143 return ureg_create_shader_and_destroy(shader
, idct
->pipe
);
147 matrix_mul(struct ureg_program
*shader
, struct ureg_dst dst
,
148 struct ureg_src tc
[2], struct ureg_src sampler
[2],
149 struct ureg_src start
[2], struct ureg_src step
[2],
152 struct ureg_dst t_tc
[2], m
[2][2], tmp
[2];
155 for(i
= 0; i
< 2; ++i
) {
156 t_tc
[i
] = ureg_DECL_temporary(shader
);
157 for(j
= 0; j
< 2; ++j
)
158 m
[i
][j
] = ureg_DECL_temporary(shader
);
159 tmp
[i
] = ureg_DECL_temporary(shader
);
164 * tmp[0..1] = dot4(m[0..1][0], m[0..1][1])
165 * fragment = tmp[0] + tmp[1]
167 ureg_MOV(shader
, ureg_writemask(t_tc
[0], TGSI_WRITEMASK_X
), start
[0]);
168 ureg_MOV(shader
, ureg_writemask(t_tc
[0], TGSI_WRITEMASK_Y
), tc
[0]);
170 ureg_MOV(shader
, ureg_writemask(t_tc
[1], TGSI_WRITEMASK_X
), tc
[1]);
171 ureg_MOV(shader
, ureg_writemask(t_tc
[1], TGSI_WRITEMASK_Y
), start
[1]);
173 for(i
= 0; i
< 2; ++i
) {
174 for(j
= 0; j
< 4; ++j
) {
175 /* Nouveau and r600g can't writemask tex dst regs (yet?), do in two steps */
176 ureg_TEX(shader
, tmp
[0], TGSI_TEXTURE_2D
, ureg_src(t_tc
[0]), sampler
[0]);
177 ureg_MOV(shader
, ureg_writemask(m
[i
][0], TGSI_WRITEMASK_X
<< j
), ureg_scalar(ureg_src(tmp
[0]), TGSI_SWIZZLE_X
));
179 ureg_TEX(shader
, tmp
[1], TGSI_TEXTURE_2D
, ureg_src(t_tc
[1]), sampler
[1]);
180 ureg_MOV(shader
, ureg_writemask(m
[i
][1], TGSI_WRITEMASK_X
<< j
), ureg_scalar(ureg_src(tmp
[1]), TGSI_SWIZZLE_X
));
182 ureg_ADD(shader
, ureg_writemask(t_tc
[0], TGSI_WRITEMASK_X
), ureg_src(t_tc
[0]), step
[0]);
183 ureg_ADD(shader
, ureg_writemask(t_tc
[1], TGSI_WRITEMASK_Y
), ureg_src(t_tc
[1]), step
[1]);
187 ureg_DP4(shader
, ureg_writemask(tmp
[0], TGSI_WRITEMASK_X
), ureg_src(m
[0][0]), ureg_src(m
[0][1]));
188 ureg_DP4(shader
, ureg_writemask(tmp
[1], TGSI_WRITEMASK_X
), ureg_src(m
[1][0]), ureg_src(m
[1][1]));
189 ureg_ADD(shader
, ureg_writemask(tmp
[0], TGSI_WRITEMASK_X
), ureg_src(tmp
[0]), ureg_src(tmp
[1]));
190 ureg_MUL(shader
, dst
, ureg_src(tmp
[0]), ureg_imm1f(shader
, scale
));
192 for(i
= 0; i
< 2; ++i
) {
193 ureg_release_temporary(shader
, t_tc
[i
]);
194 for(j
= 0; j
< 2; ++j
)
195 ureg_release_temporary(shader
, m
[i
][j
]);
196 ureg_release_temporary(shader
, tmp
[i
]);
201 create_transpose_frag_shader(struct vl_idct
*idct
)
203 struct ureg_program
*shader
;
204 struct ureg_src tc
[2], sampler
[2];
205 struct ureg_src start
[2], step
[2];
206 struct ureg_dst fragment
;
208 shader
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
212 tc
[0] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_BLOCK
, TGSI_INTERPOLATE_LINEAR
);
213 tc
[1] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_TEX
, TGSI_INTERPOLATE_LINEAR
);
215 start
[0] = ureg_imm1f(shader
, 0.0f
);
216 start
[1] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_START
, TGSI_INTERPOLATE_CONSTANT
);
218 step
[0] = ureg_imm1f(shader
, 1.0f
/ BLOCK_HEIGHT
);
219 step
[1] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_STEP
, TGSI_INTERPOLATE_CONSTANT
);
221 sampler
[0] = ureg_DECL_sampler(shader
, 0);
222 sampler
[1] = ureg_DECL_sampler(shader
, 1);
224 fragment
= ureg_DECL_output(shader
, TGSI_SEMANTIC_COLOR
, 0);
226 matrix_mul(shader
, fragment
, tc
, sampler
, start
, step
, STAGE1_SCALE
);
230 return ureg_create_shader_and_destroy(shader
, idct
->pipe
);
234 create_matrix_frag_shader(struct vl_idct
*idct
)
236 struct ureg_program
*shader
;
237 struct ureg_src tc
[2], sampler
[2];
238 struct ureg_src start
[2], step
[2];
239 struct ureg_dst fragment
;
241 shader
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
245 tc
[0] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_TEX
, TGSI_INTERPOLATE_LINEAR
);
246 tc
[1] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_BLOCK
, TGSI_INTERPOLATE_LINEAR
);
248 start
[0] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_START
, TGSI_INTERPOLATE_CONSTANT
);
249 start
[1] = ureg_imm1f(shader
, 0.0f
);
251 step
[0] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_STEP
, TGSI_INTERPOLATE_CONSTANT
);
252 step
[1] = ureg_imm1f(shader
, 1.0f
/ BLOCK_WIDTH
);
254 sampler
[0] = ureg_DECL_sampler(shader
, 1);
255 sampler
[1] = ureg_DECL_sampler(shader
, 0);
257 fragment
= ureg_DECL_output(shader
, TGSI_SEMANTIC_COLOR
, 0);
259 matrix_mul(shader
, fragment
, tc
, sampler
, start
, step
, STAGE2_SCALE
);
263 return ureg_create_shader_and_destroy(shader
, idct
->pipe
);
267 create_empty_block_frag_shader(struct vl_idct
*idct
)
269 struct ureg_program
*shader
;
270 struct ureg_dst fragment
;
272 shader
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
276 fragment
= ureg_DECL_output(shader
, TGSI_SEMANTIC_COLOR
, 0);
278 ureg_MOV(shader
, fragment
, ureg_imm1f(shader
, 0.0f
));
282 return ureg_create_shader_and_destroy(shader
, idct
->pipe
);
286 xfer_buffers_map(struct vl_idct
*idct
)
288 struct pipe_box rect
=
291 idct
->destination
->width0
,
292 idct
->destination
->height0
,
296 idct
->tex_transfer
= idct
->pipe
->get_transfer
298 idct
->pipe
, idct
->textures
.individual
.source
,
300 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
304 idct
->texels
= idct
->pipe
->transfer_map(idct
->pipe
, idct
->tex_transfer
);
306 idct
->vectors
= pipe_buffer_map
309 idct
->vertex_bufs
.individual
.pos
.buffer
,
310 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
316 xfer_buffers_unmap(struct vl_idct
*idct
)
318 pipe_buffer_unmap(idct
->pipe
, idct
->vertex_bufs
.individual
.pos
.buffer
, idct
->vec_transfer
);
320 idct
->pipe
->transfer_unmap(idct
->pipe
, idct
->tex_transfer
);
321 idct
->pipe
->transfer_destroy(idct
->pipe
, idct
->tex_transfer
);
325 init_shaders(struct vl_idct
*idct
)
327 idct
->vs
= create_vert_shader(idct
);
328 idct
->transpose_fs
= create_transpose_frag_shader(idct
);
329 idct
->matrix_fs
= create_matrix_frag_shader(idct
);
330 idct
->eb_fs
= create_empty_block_frag_shader(idct
);
334 idct
->transpose_fs
!= NULL
&&
335 idct
->matrix_fs
!= NULL
&&
340 cleanup_shaders(struct vl_idct
*idct
)
342 idct
->pipe
->delete_vs_state(idct
->pipe
, idct
->vs
);
343 idct
->pipe
->delete_fs_state(idct
->pipe
, idct
->transpose_fs
);
344 idct
->pipe
->delete_fs_state(idct
->pipe
, idct
->matrix_fs
);
345 idct
->pipe
->delete_fs_state(idct
->pipe
, idct
->eb_fs
);
349 init_buffers(struct vl_idct
*idct
)
351 struct pipe_resource
template;
352 struct pipe_sampler_view sampler_view
;
353 struct pipe_vertex_element vertex_elems
[2];
357 align(idct
->destination
->width0
, BLOCK_WIDTH
) / BLOCK_WIDTH
*
358 align(idct
->destination
->height0
, BLOCK_HEIGHT
) / BLOCK_HEIGHT
*
359 idct
->destination
->depth0
;
361 memset(&template, 0, sizeof(struct pipe_resource
));
362 template.target
= PIPE_TEXTURE_2D
;
363 template.format
= PIPE_FORMAT_R32_FLOAT
;
364 template.last_level
= 0;
366 template.height0
= 8;
368 template.usage
= PIPE_USAGE_IMMUTABLE
;
369 template.bind
= PIPE_BIND_SAMPLER_VIEW
;
372 idct
->textures
.individual
.matrix
= idct
->pipe
->screen
->resource_create(idct
->pipe
->screen
, &template);
373 idct
->textures
.individual
.transpose
= idct
->pipe
->screen
->resource_create(idct
->pipe
->screen
, &template);
375 template.format
= idct
->destination
->format
;
376 template.width0
= idct
->destination
->width0
;
377 template.height0
= idct
->destination
->height0
;
378 template.depth0
= idct
->destination
->depth0
;
379 template.usage
= PIPE_USAGE_DYNAMIC
;
380 idct
->textures
.individual
.source
= idct
->pipe
->screen
->resource_create(idct
->pipe
->screen
, &template);
382 template.usage
= PIPE_USAGE_STATIC
;
383 idct
->textures
.individual
.intermediate
= idct
->pipe
->screen
->resource_create(idct
->pipe
->screen
, &template);
385 for (i
= 0; i
< 4; ++i
) {
386 if(idct
->textures
.all
[i
] == NULL
)
387 return false; /* a texture failed to allocate */
389 u_sampler_view_default_template(&sampler_view
, idct
->textures
.all
[i
], idct
->textures
.all
[i
]->format
);
390 idct
->sampler_views
.all
[i
] = idct
->pipe
->create_sampler_view(idct
->pipe
, idct
->textures
.all
[i
], &sampler_view
);
393 idct
->vertex_bufs
.individual
.quad
.stride
= sizeof(struct vertex2f
);
394 idct
->vertex_bufs
.individual
.quad
.max_index
= 4 * idct
->max_blocks
- 1;
395 idct
->vertex_bufs
.individual
.quad
.buffer_offset
= 0;
396 idct
->vertex_bufs
.individual
.quad
.buffer
= pipe_buffer_create
399 PIPE_BIND_VERTEX_BUFFER
,
400 sizeof(struct vertex2f
) * 4 * idct
->max_blocks
403 if(idct
->vertex_bufs
.individual
.quad
.buffer
== NULL
)
406 idct
->vertex_bufs
.individual
.pos
.stride
= sizeof(struct vertex2f
);
407 idct
->vertex_bufs
.individual
.pos
.max_index
= 4 * idct
->max_blocks
- 1;
408 idct
->vertex_bufs
.individual
.pos
.buffer_offset
= 0;
409 idct
->vertex_bufs
.individual
.pos
.buffer
= pipe_buffer_create
412 PIPE_BIND_VERTEX_BUFFER
,
413 sizeof(struct vertex2f
) * 4 * idct
->max_blocks
416 if(idct
->vertex_bufs
.individual
.pos
.buffer
== NULL
)
420 vertex_elems
[0].src_offset
= 0;
421 vertex_elems
[0].instance_divisor
= 0;
422 vertex_elems
[0].vertex_buffer_index
= 0;
423 vertex_elems
[0].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
426 vertex_elems
[1].src_offset
= 0;
427 vertex_elems
[1].instance_divisor
= 0;
428 vertex_elems
[1].vertex_buffer_index
= 1;
429 vertex_elems
[1].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
431 idct
->vertex_elems_state
= idct
->pipe
->create_vertex_elements_state(idct
->pipe
, 2, vertex_elems
);
433 idct
->vs_const_buf
= pipe_buffer_create
436 PIPE_BIND_CONSTANT_BUFFER
,
437 sizeof(struct vertex_shader_consts
)
440 if(idct
->vs_const_buf
== NULL
)
447 cleanup_buffers(struct vl_idct
*idct
)
453 pipe_resource_reference(&idct
->vs_const_buf
, NULL
);
455 for (i
= 0; i
< 4; ++i
) {
456 pipe_sampler_view_reference(&idct
->sampler_views
.all
[i
], NULL
);
457 pipe_resource_reference(&idct
->textures
.all
[i
], NULL
);
460 idct
->pipe
->delete_vertex_elements_state(idct
->pipe
, idct
->vertex_elems_state
);
461 pipe_resource_reference(&idct
->vertex_bufs
.individual
.quad
.buffer
, NULL
);
462 pipe_resource_reference(&idct
->vertex_bufs
.individual
.pos
.buffer
, NULL
);
466 init_constants(struct vl_idct
*idct
)
468 struct pipe_transfer
*buf_transfer
;
469 struct vertex_shader_consts
*vs_consts
;
473 struct pipe_box rect
=
481 unsigned i
, j
, pitch
;
487 idct
->vertex_bufs
.individual
.quad
.buffer
,
488 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
491 for ( i
= 0; i
< idct
->max_blocks
; ++i
)
492 memcpy(v
+ i
* 4, &const_quad
, sizeof(const_quad
));
493 pipe_buffer_unmap(idct
->pipe
, idct
->vertex_bufs
.individual
.quad
.buffer
, buf_transfer
);
495 /* transposed matrix */
496 buf_transfer
= idct
->pipe
->get_transfer
498 idct
->pipe
, idct
->textures
.individual
.transpose
,
500 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
503 pitch
= buf_transfer
->stride
/ util_format_get_blocksize(buf_transfer
->resource
->format
);
505 f
= idct
->pipe
->transfer_map(idct
->pipe
, buf_transfer
);
506 for(i
= 0; i
< BLOCK_HEIGHT
; ++i
)
507 for(j
= 0; j
< BLOCK_WIDTH
; ++j
)
508 f
[i
* pitch
+ j
] = const_matrix
[j
][i
]; // transpose
510 idct
->pipe
->transfer_unmap(idct
->pipe
, buf_transfer
);
511 idct
->pipe
->transfer_destroy(idct
->pipe
, buf_transfer
);
514 buf_transfer
= idct
->pipe
->get_transfer
516 idct
->pipe
, idct
->textures
.individual
.matrix
,
518 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
521 pitch
= buf_transfer
->stride
/ util_format_get_blocksize(buf_transfer
->resource
->format
);
523 f
= idct
->pipe
->transfer_map(idct
->pipe
, buf_transfer
);
524 for(i
= 0; i
< BLOCK_HEIGHT
; ++i
)
525 for(j
= 0; j
< BLOCK_WIDTH
; ++j
)
526 f
[i
* pitch
+ j
] = const_matrix
[i
][j
];
528 idct
->pipe
->transfer_unmap(idct
->pipe
, buf_transfer
);
529 idct
->pipe
->transfer_destroy(idct
->pipe
, buf_transfer
);
531 /* normalisation constants */
532 vs_consts
= pipe_buffer_map
534 idct
->pipe
, idct
->vs_const_buf
,
535 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
539 vs_consts
->norm
.x
= 1.0f
/ idct
->destination
->width0
;
540 vs_consts
->norm
.y
= 1.0f
/ idct
->destination
->height0
;
542 pipe_buffer_unmap(idct
->pipe
, idct
->vs_const_buf
, buf_transfer
);
546 init_state(struct vl_idct
*idct
)
548 struct pipe_sampler_state sampler
;
551 idct
->num_blocks
= 0;
552 idct
->num_empty_blocks
= 0;
554 idct
->viewport
.scale
[0] = idct
->destination
->width0
;
555 idct
->viewport
.scale
[1] = idct
->destination
->height0
;
556 idct
->viewport
.scale
[2] = 1;
557 idct
->viewport
.scale
[3] = 1;
558 idct
->viewport
.translate
[0] = 0;
559 idct
->viewport
.translate
[1] = 0;
560 idct
->viewport
.translate
[2] = 0;
561 idct
->viewport
.translate
[3] = 0;
563 idct
->fb_state
.width
= idct
->destination
->width0
;
564 idct
->fb_state
.height
= idct
->destination
->height0
;
565 idct
->fb_state
.nr_cbufs
= 1;
566 idct
->fb_state
.zsbuf
= NULL
;
568 for (i
= 0; i
< 4; ++i
) {
569 memset(&sampler
, 0, sizeof(sampler
));
570 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
571 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
572 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
573 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
574 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
575 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
576 sampler
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
577 sampler
.compare_func
= PIPE_FUNC_ALWAYS
;
578 sampler
.normalized_coords
= 1;
579 /*sampler.shadow_ambient = ; */
580 /*sampler.lod_bias = ; */
582 /*sampler.max_lod = ; */
583 /*sampler.border_color[0] = ; */
584 /*sampler.max_anisotropy = ; */
585 idct
->samplers
.all
[i
] = idct
->pipe
->create_sampler_state(idct
->pipe
, &sampler
);
590 cleanup_state(struct vl_idct
*idct
)
594 for (i
= 0; i
< 4; ++i
)
595 idct
->pipe
->delete_sampler_state(idct
->pipe
, idct
->samplers
.all
[i
]);
599 vl_idct_init(struct vl_idct
*idct
, struct pipe_context
*pipe
, struct pipe_resource
*dst
)
601 assert(idct
&& pipe
&& dst
);
604 pipe_resource_reference(&idct
->destination
, dst
);
608 if(!init_shaders(idct
))
611 if(!init_buffers(idct
)) {
612 cleanup_shaders(idct
);
616 idct
->surfaces
.intermediate
= idct
->pipe
->screen
->get_tex_surface(
617 idct
->pipe
->screen
, idct
->textures
.individual
.intermediate
, 0, 0, 0,
618 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
);
620 idct
->surfaces
.destination
= idct
->pipe
->screen
->get_tex_surface(
621 idct
->pipe
->screen
, idct
->destination
, 0, 0, 0,
622 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
);
624 init_constants(idct
);
625 xfer_buffers_map(idct
);
631 vl_idct_cleanup(struct vl_idct
*idct
)
633 idct
->pipe
->screen
->tex_surface_destroy(idct
->surfaces
.destination
);
634 idct
->pipe
->screen
->tex_surface_destroy(idct
->surfaces
.intermediate
);
636 cleanup_shaders(idct
);
637 cleanup_buffers(idct
);
641 pipe_resource_reference(&idct
->destination
, NULL
);
645 vl_idct_add_block(struct vl_idct
*idct
, unsigned x
, unsigned y
, short *block
)
647 struct vertex2f v
, *v_dst
;
657 tex_pitch
= idct
->tex_transfer
->stride
/ util_format_get_blocksize(idct
->tex_transfer
->resource
->format
);
658 texels
= idct
->texels
+ y
* tex_pitch
* BLOCK_HEIGHT
+ x
* BLOCK_WIDTH
;
660 for (i
= 0; i
< BLOCK_HEIGHT
; ++i
)
661 memcpy(texels
+ i
* tex_pitch
, block
+ i
* BLOCK_WIDTH
, BLOCK_WIDTH
* 2);
663 /* non empty blocks fills the vector buffer from left to right */
664 v_dst
= idct
->vectors
+ idct
->num_blocks
* 4;
670 /* while empty blocks fills the vector buffer from right to left */
671 v_dst
= idct
->vectors
+ (idct
->max_blocks
- idct
->num_empty_blocks
) * 4 - 4;
673 idct
->num_empty_blocks
++;
679 for (i
= 0; i
< 4; ++i
) {
685 vl_idct_flush(struct vl_idct
*idct
)
687 xfer_buffers_unmap(idct
);
689 idct
->pipe
->set_constant_buffer(idct
->pipe
, PIPE_SHADER_VERTEX
, 0, idct
->vs_const_buf
);
691 if(idct
->num_blocks
> 0) {
694 idct
->fb_state
.cbufs
[0] = idct
->surfaces
.intermediate
;
695 idct
->pipe
->set_framebuffer_state(idct
->pipe
, &idct
->fb_state
);
696 idct
->pipe
->set_viewport_state(idct
->pipe
, &idct
->viewport
);
698 idct
->pipe
->set_vertex_buffers(idct
->pipe
, 2, idct
->vertex_bufs
.all
);
699 idct
->pipe
->bind_vertex_elements_state(idct
->pipe
, idct
->vertex_elems_state
);
700 idct
->pipe
->set_fragment_sampler_views(idct
->pipe
, 2, idct
->sampler_views
.stage
[0]);
701 idct
->pipe
->bind_fragment_sampler_states(idct
->pipe
, 2, idct
->samplers
.stage
[0]);
702 idct
->pipe
->bind_vs_state(idct
->pipe
, idct
->vs
);
703 idct
->pipe
->bind_fs_state(idct
->pipe
, idct
->transpose_fs
);
705 util_draw_arrays(idct
->pipe
, PIPE_PRIM_QUADS
, 0, idct
->num_blocks
* 4);
708 idct
->fb_state
.cbufs
[0] = idct
->surfaces
.destination
;
709 idct
->pipe
->set_framebuffer_state(idct
->pipe
, &idct
->fb_state
);
710 idct
->pipe
->set_viewport_state(idct
->pipe
, &idct
->viewport
);
712 idct
->pipe
->set_vertex_buffers(idct
->pipe
, 2, idct
->vertex_bufs
.all
);
713 idct
->pipe
->bind_vertex_elements_state(idct
->pipe
, idct
->vertex_elems_state
);
714 idct
->pipe
->set_fragment_sampler_views(idct
->pipe
, 2, idct
->sampler_views
.stage
[1]);
715 idct
->pipe
->bind_fragment_sampler_states(idct
->pipe
, 2, idct
->samplers
.stage
[1]);
716 idct
->pipe
->bind_vs_state(idct
->pipe
, idct
->vs
);
717 idct
->pipe
->bind_fs_state(idct
->pipe
, idct
->matrix_fs
);
719 util_draw_arrays(idct
->pipe
, PIPE_PRIM_QUADS
, 0, idct
->num_blocks
* 4);
722 if(idct
->num_empty_blocks
> 0) {
724 /* empty block handling */
725 idct
->fb_state
.cbufs
[0] = idct
->surfaces
.destination
;
726 idct
->pipe
->set_framebuffer_state(idct
->pipe
, &idct
->fb_state
);
727 idct
->pipe
->set_viewport_state(idct
->pipe
, &idct
->viewport
);
729 idct
->pipe
->set_vertex_buffers(idct
->pipe
, 2, idct
->vertex_bufs
.all
);
730 idct
->pipe
->bind_vertex_elements_state(idct
->pipe
, idct
->vertex_elems_state
);
731 idct
->pipe
->set_fragment_sampler_views(idct
->pipe
, 4, idct
->sampler_views
.all
);
732 idct
->pipe
->bind_fragment_sampler_states(idct
->pipe
, 4, idct
->samplers
.all
);
733 idct
->pipe
->bind_vs_state(idct
->pipe
, idct
->vs
);
734 idct
->pipe
->bind_fs_state(idct
->pipe
, idct
->eb_fs
);
736 util_draw_arrays(idct
->pipe
, PIPE_PRIM_QUADS
,
737 (idct
->max_blocks
- idct
->num_empty_blocks
) * 4,
738 idct
->num_empty_blocks
* 4);
741 idct
->num_blocks
= 0;
742 idct
->num_empty_blocks
= 0;
743 xfer_buffers_map(idct
);