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 struct vertex_shader_consts
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
}
77 /* vertices for a quad covering a block */
78 static const struct vertex2f const_quad
[4] = {
79 {0.0f
, 0.0f
}, {1.0f
, 0.0f
}, {1.0f
, 1.0f
}, {0.0f
, 1.0f
}
83 create_vert_shader(struct vl_idct
*idct
)
85 struct ureg_program
*shader
;
86 struct ureg_src norm
, bs
;
87 struct ureg_src vrect
, vpos
;
88 struct ureg_dst scale
, t_vpos
;
89 struct ureg_dst o_vpos
, o_block
, o_tex
, o_start
, o_step
;
91 shader
= ureg_create(TGSI_PROCESSOR_VERTEX
);
95 norm
= ureg_DECL_constant(shader
, 0);
96 bs
= ureg_imm2f(shader
, BLOCK_WIDTH
, BLOCK_HEIGHT
);
98 scale
= ureg_DECL_temporary(shader
);
99 t_vpos
= ureg_DECL_temporary(shader
);
101 vrect
= ureg_DECL_vs_input(shader
, VS_I_RECT
);
102 vpos
= ureg_DECL_vs_input(shader
, VS_I_VPOS
);
104 o_vpos
= ureg_DECL_output(shader
, TGSI_SEMANTIC_POSITION
, VS_O_VPOS
);
105 o_block
= ureg_DECL_output(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_BLOCK
);
106 o_tex
= ureg_DECL_output(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_TEX
);
107 o_start
= ureg_DECL_output(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_START
);
108 o_step
= ureg_DECL_output(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_STEP
);
111 * scale = norm * mbs;
113 * t_vpos = vpos + vrect
114 * o_vpos.xy = t_vpos * scale
119 * o_start = vpos * scale
123 ureg_MUL(shader
, ureg_writemask(scale
, TGSI_WRITEMASK_XY
), norm
, bs
);
125 ureg_ADD(shader
, ureg_writemask(t_vpos
, TGSI_WRITEMASK_XY
), vpos
, vrect
);
126 ureg_MUL(shader
, ureg_writemask(t_vpos
, TGSI_WRITEMASK_XY
), ureg_src(t_vpos
), ureg_src(scale
));
127 ureg_MOV(shader
, ureg_writemask(o_vpos
, TGSI_WRITEMASK_XY
), ureg_src(t_vpos
));
128 ureg_MOV(shader
, ureg_writemask(o_vpos
, TGSI_WRITEMASK_ZW
), vpos
);
130 ureg_MOV(shader
, ureg_writemask(o_tex
, TGSI_WRITEMASK_XY
), ureg_src(t_vpos
));
131 ureg_MOV(shader
, ureg_writemask(o_block
, TGSI_WRITEMASK_XY
), vrect
);
132 ureg_MUL(shader
, ureg_writemask(o_start
, TGSI_WRITEMASK_XY
), vpos
, ureg_src(scale
));
133 ureg_MOV(shader
, ureg_writemask(o_step
, TGSI_WRITEMASK_XY
), norm
);
135 ureg_release_temporary(shader
, t_vpos
);
136 ureg_release_temporary(shader
, scale
);
140 return ureg_create_shader_and_destroy(shader
, idct
->pipe
);
144 matrix_mul(struct ureg_program
*shader
, struct ureg_dst dst
,
145 struct ureg_src tc
[2], struct ureg_src sampler
[2],
146 struct ureg_src start
[2], struct ureg_src step
[2],
149 struct ureg_dst t_tc
[2], m
[2][2], tmp
[2];
152 for(i
= 0; i
< 2; ++i
) {
153 t_tc
[i
] = ureg_DECL_temporary(shader
);
154 for(j
= 0; j
< 2; ++j
)
155 m
[i
][j
] = ureg_DECL_temporary(shader
);
156 tmp
[i
] = ureg_DECL_temporary(shader
);
161 * tmp[0..1] = dot4(m[0..1][0], m[0..1][1])
162 * fragment = tmp[0] + tmp[1]
164 ureg_MOV(shader
, ureg_writemask(t_tc
[0], TGSI_WRITEMASK_X
), start
[0]);
165 ureg_MOV(shader
, ureg_writemask(t_tc
[0], TGSI_WRITEMASK_Y
), tc
[0]);
167 ureg_MOV(shader
, ureg_writemask(t_tc
[1], TGSI_WRITEMASK_X
), tc
[1]);
168 ureg_MOV(shader
, ureg_writemask(t_tc
[1], TGSI_WRITEMASK_Y
), start
[1]);
170 for(i
= 0; i
< 2; ++i
) {
171 for(j
= 0; j
< 4; ++j
) {
172 /* Nouveau and r600g can't writemask tex dst regs (yet?), do in two steps */
173 ureg_TEX(shader
, tmp
[0], TGSI_TEXTURE_2D
, ureg_src(t_tc
[0]), sampler
[0]);
174 ureg_MOV(shader
, ureg_writemask(m
[i
][0], TGSI_WRITEMASK_X
<< j
), ureg_scalar(ureg_src(tmp
[0]), TGSI_SWIZZLE_X
));
176 ureg_TEX(shader
, tmp
[1], TGSI_TEXTURE_2D
, ureg_src(t_tc
[1]), sampler
[1]);
177 ureg_MOV(shader
, ureg_writemask(m
[i
][1], TGSI_WRITEMASK_X
<< j
), ureg_scalar(ureg_src(tmp
[1]), TGSI_SWIZZLE_X
));
179 ureg_ADD(shader
, ureg_writemask(t_tc
[0], TGSI_WRITEMASK_X
), ureg_src(t_tc
[0]), step
[0]);
180 ureg_ADD(shader
, ureg_writemask(t_tc
[1], TGSI_WRITEMASK_Y
), ureg_src(t_tc
[1]), step
[1]);
184 ureg_MUL(shader
, m
[i
][0], ureg_src(m
[i
][0]), ureg_scalar(ureg_imm1f(shader
, scale
[0]), TGSI_SWIZZLE_X
));
187 ureg_MUL(shader
, m
[i
][1], ureg_src(m
[i
][1]), ureg_scalar(ureg_imm1f(shader
, scale
[1]), TGSI_SWIZZLE_X
));
190 ureg_DP4(shader
, ureg_writemask(tmp
[0], TGSI_WRITEMASK_X
), ureg_src(m
[0][0]), ureg_src(m
[0][1]));
191 ureg_DP4(shader
, ureg_writemask(tmp
[1], TGSI_WRITEMASK_X
), ureg_src(m
[1][0]), ureg_src(m
[1][1]));
192 ureg_ADD(shader
, ureg_writemask(dst
, TGSI_WRITEMASK_X
), ureg_src(tmp
[0]), ureg_src(tmp
[1]));
194 for(i
= 0; i
< 2; ++i
) {
195 ureg_release_temporary(shader
, t_tc
[i
]);
196 for(j
= 0; j
< 2; ++j
)
197 ureg_release_temporary(shader
, m
[i
][j
]);
198 ureg_release_temporary(shader
, tmp
[i
]);
203 create_transpose_frag_shader(struct vl_idct
*idct
)
205 struct ureg_program
*shader
;
206 struct ureg_src tc
[2], sampler
[2];
207 struct ureg_src start
[2], step
[2];
208 struct ureg_dst fragment
;
211 shader
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
215 tc
[0] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_BLOCK
, TGSI_INTERPOLATE_LINEAR
);
216 tc
[1] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_TEX
, TGSI_INTERPOLATE_LINEAR
);
218 start
[0] = ureg_imm1f(shader
, 0.0f
);
219 start
[1] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_START
, TGSI_INTERPOLATE_CONSTANT
);
221 step
[0] = ureg_imm1f(shader
, 1.0f
/ BLOCK_HEIGHT
);
222 step
[1] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_STEP
, TGSI_INTERPOLATE_CONSTANT
);
224 sampler
[0] = ureg_DECL_sampler(shader
, 0);
225 sampler
[1] = ureg_DECL_sampler(shader
, 2);
228 scale
[1] = SCALE_FACTOR_16_TO_9
;
230 fragment
= ureg_DECL_output(shader
, TGSI_SEMANTIC_COLOR
, 0);
232 matrix_mul(shader
, fragment
, tc
, sampler
, start
, step
, scale
);
236 return ureg_create_shader_and_destroy(shader
, idct
->pipe
);
240 create_matrix_frag_shader(struct vl_idct
*idct
)
242 struct ureg_program
*shader
;
243 struct ureg_src tc
[2], sampler
[2];
244 struct ureg_src start
[2], step
[2];
245 struct ureg_dst fragment
;
248 shader
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
252 tc
[0] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_TEX
, TGSI_INTERPOLATE_LINEAR
);
253 tc
[1] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_BLOCK
, TGSI_INTERPOLATE_LINEAR
);
255 start
[0] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_START
, TGSI_INTERPOLATE_CONSTANT
);
256 start
[1] = ureg_imm1f(shader
, 0.0f
);
258 step
[0] = ureg_DECL_fs_input(shader
, TGSI_SEMANTIC_GENERIC
, VS_O_STEP
, TGSI_INTERPOLATE_CONSTANT
);
259 step
[1] = ureg_imm1f(shader
, 1.0f
/ BLOCK_WIDTH
);
261 sampler
[0] = ureg_DECL_sampler(shader
, 3);
262 sampler
[1] = ureg_DECL_sampler(shader
, 1);
267 fragment
= ureg_DECL_output(shader
, TGSI_SEMANTIC_COLOR
, 0);
269 matrix_mul(shader
, fragment
, tc
, sampler
, start
, step
, scale
);
273 return ureg_create_shader_and_destroy(shader
, idct
->pipe
);
277 create_empty_block_frag_shader(struct vl_idct
*idct
)
279 struct ureg_program
*shader
;
280 struct ureg_dst fragment
;
282 shader
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
286 fragment
= ureg_DECL_output(shader
, TGSI_SEMANTIC_COLOR
, 0);
288 ureg_MOV(shader
, fragment
, ureg_imm1f(shader
, 0.0f
));
292 return ureg_create_shader_and_destroy(shader
, idct
->pipe
);
296 xfer_buffers_map(struct vl_idct
*idct
)
298 struct pipe_box rect
=
301 idct
->destination
->width0
,
302 idct
->destination
->height0
,
306 idct
->tex_transfer
= idct
->pipe
->get_transfer
309 idct
->pipe
, idct
->textures
.individual
.source
,
311 idct
->pipe
, idct
->destination
,
314 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
318 idct
->texels
= idct
->pipe
->transfer_map(idct
->pipe
, idct
->tex_transfer
);
320 idct
->vectors
= pipe_buffer_map
323 idct
->vertex_bufs
.individual
.pos
.buffer
,
324 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
330 xfer_buffers_unmap(struct vl_idct
*idct
)
332 pipe_buffer_unmap(idct
->pipe
, idct
->vertex_bufs
.individual
.pos
.buffer
, idct
->vec_transfer
);
334 idct
->pipe
->transfer_unmap(idct
->pipe
, idct
->tex_transfer
);
335 idct
->pipe
->transfer_destroy(idct
->pipe
, idct
->tex_transfer
);
339 init_shaders(struct vl_idct
*idct
)
341 assert(idct
->vs
= create_vert_shader(idct
));
342 assert(idct
->transpose_fs
= create_transpose_frag_shader(idct
));
343 assert(idct
->matrix_fs
= create_matrix_frag_shader(idct
));
344 assert(idct
->eb_fs
= create_empty_block_frag_shader(idct
));
348 idct
->transpose_fs
!= NULL
&&
349 idct
->matrix_fs
!= NULL
&&
354 cleanup_shaders(struct vl_idct
*idct
)
356 idct
->pipe
->delete_vs_state(idct
->pipe
, idct
->vs
);
357 idct
->pipe
->delete_fs_state(idct
->pipe
, idct
->transpose_fs
);
358 idct
->pipe
->delete_fs_state(idct
->pipe
, idct
->matrix_fs
);
359 idct
->pipe
->delete_fs_state(idct
->pipe
, idct
->eb_fs
);
363 init_buffers(struct vl_idct
*idct
)
365 struct pipe_resource
template;
366 struct pipe_sampler_view sampler_view
;
367 struct pipe_vertex_element vertex_elems
[2];
371 align(idct
->destination
->width0
, BLOCK_WIDTH
) / BLOCK_WIDTH
*
372 align(idct
->destination
->height0
, BLOCK_HEIGHT
) / BLOCK_HEIGHT
*
373 idct
->destination
->depth0
;
375 memset(&template, 0, sizeof(struct pipe_resource
));
376 template.target
= PIPE_TEXTURE_2D
;
377 template.format
= PIPE_FORMAT_R32_FLOAT
;
378 template.last_level
= 0;
380 template.height0
= 8;
382 template.usage
= PIPE_USAGE_IMMUTABLE
;
383 template.bind
= PIPE_BIND_SAMPLER_VIEW
;
386 idct
->textures
.individual
.matrix
= idct
->pipe
->screen
->resource_create(idct
->pipe
->screen
, &template);
387 idct
->textures
.individual
.transpose
= idct
->pipe
->screen
->resource_create(idct
->pipe
->screen
, &template);
389 template.format
= idct
->destination
->format
;
390 template.width0
= idct
->destination
->width0
;
391 template.height0
= idct
->destination
->height0
;
392 template.depth0
= idct
->destination
->depth0
;
393 template.usage
= PIPE_USAGE_DYNAMIC
;
394 idct
->textures
.individual
.source
= idct
->pipe
->screen
->resource_create(idct
->pipe
->screen
, &template);
396 template.usage
= PIPE_USAGE_STATIC
;
397 idct
->textures
.individual
.intermediate
= idct
->pipe
->screen
->resource_create(idct
->pipe
->screen
, &template);
399 for (i
= 0; i
< 4; ++i
) {
400 if(idct
->textures
.all
[i
] == NULL
)
401 return false; /* a texture failed to allocate */
403 u_sampler_view_default_template(&sampler_view
, idct
->textures
.all
[i
], idct
->textures
.all
[i
]->format
);
404 idct
->sampler_views
.all
[i
] = idct
->pipe
->create_sampler_view(idct
->pipe
, idct
->textures
.all
[i
], &sampler_view
);
407 idct
->vertex_bufs
.individual
.quad
.stride
= sizeof(struct vertex2f
);
408 idct
->vertex_bufs
.individual
.quad
.max_index
= 4 * idct
->max_blocks
- 1;
409 idct
->vertex_bufs
.individual
.quad
.buffer_offset
= 0;
410 idct
->vertex_bufs
.individual
.quad
.buffer
= pipe_buffer_create
413 PIPE_BIND_VERTEX_BUFFER
,
414 sizeof(struct vertex2f
) * 4 * idct
->max_blocks
417 if(idct
->vertex_bufs
.individual
.quad
.buffer
== NULL
)
420 idct
->vertex_bufs
.individual
.pos
.stride
= sizeof(struct vertex2f
);
421 idct
->vertex_bufs
.individual
.pos
.max_index
= 4 * idct
->max_blocks
- 1;
422 idct
->vertex_bufs
.individual
.pos
.buffer_offset
= 0;
423 idct
->vertex_bufs
.individual
.pos
.buffer
= pipe_buffer_create
426 PIPE_BIND_VERTEX_BUFFER
,
427 sizeof(struct vertex2f
) * 4 * idct
->max_blocks
430 if(idct
->vertex_bufs
.individual
.pos
.buffer
== NULL
)
434 vertex_elems
[0].src_offset
= 0;
435 vertex_elems
[0].instance_divisor
= 0;
436 vertex_elems
[0].vertex_buffer_index
= 0;
437 vertex_elems
[0].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
440 vertex_elems
[1].src_offset
= 0;
441 vertex_elems
[1].instance_divisor
= 0;
442 vertex_elems
[1].vertex_buffer_index
= 1;
443 vertex_elems
[1].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
445 idct
->vertex_elems_state
= idct
->pipe
->create_vertex_elements_state(idct
->pipe
, 2, vertex_elems
);
447 idct
->vs_const_buf
= pipe_buffer_create
450 PIPE_BIND_CONSTANT_BUFFER
,
451 sizeof(struct vertex_shader_consts
)
454 if(idct
->vs_const_buf
== NULL
)
461 cleanup_buffers(struct vl_idct
*idct
)
467 pipe_resource_reference(&idct
->vs_const_buf
, NULL
);
469 for (i
= 0; i
< 4; ++i
) {
470 pipe_sampler_view_reference(&idct
->sampler_views
.all
[i
], NULL
);
471 pipe_resource_reference(&idct
->textures
.all
[i
], NULL
);
474 idct
->pipe
->delete_vertex_elements_state(idct
->pipe
, idct
->vertex_elems_state
);
475 pipe_resource_reference(&idct
->vertex_bufs
.individual
.quad
.buffer
, NULL
);
476 pipe_resource_reference(&idct
->vertex_bufs
.individual
.pos
.buffer
, NULL
);
480 init_constants(struct vl_idct
*idct
)
482 struct pipe_transfer
*buf_transfer
;
483 struct vertex_shader_consts
*vs_consts
;
487 struct pipe_box rect
=
495 unsigned i
, j
, pitch
;
501 idct
->vertex_bufs
.individual
.quad
.buffer
,
502 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
505 for ( i
= 0; i
< idct
->max_blocks
; ++i
)
506 memcpy(v
+ i
* 4, &const_quad
, sizeof(const_quad
));
507 pipe_buffer_unmap(idct
->pipe
, idct
->vertex_bufs
.individual
.quad
.buffer
, buf_transfer
);
509 /* transposed matrix */
510 buf_transfer
= idct
->pipe
->get_transfer
512 idct
->pipe
, idct
->textures
.individual
.transpose
,
514 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
517 pitch
= buf_transfer
->stride
/ util_format_get_blocksize(buf_transfer
->resource
->format
);
519 f
= idct
->pipe
->transfer_map(idct
->pipe
, buf_transfer
);
520 for(i
= 0; i
< BLOCK_HEIGHT
; ++i
)
521 for(j
= 0; j
< BLOCK_WIDTH
; ++j
)
522 f
[i
* pitch
+ j
] = const_matrix
[j
][i
]; // transpose
524 idct
->pipe
->transfer_unmap(idct
->pipe
, buf_transfer
);
525 idct
->pipe
->transfer_destroy(idct
->pipe
, buf_transfer
);
528 buf_transfer
= idct
->pipe
->get_transfer
530 idct
->pipe
, idct
->textures
.individual
.matrix
,
532 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
535 pitch
= buf_transfer
->stride
/ util_format_get_blocksize(buf_transfer
->resource
->format
);
537 f
= idct
->pipe
->transfer_map(idct
->pipe
, buf_transfer
);
538 for(i
= 0; i
< BLOCK_HEIGHT
; ++i
)
539 for(j
= 0; j
< BLOCK_WIDTH
; ++j
)
540 f
[i
* pitch
+ j
] = const_matrix
[i
][j
];
542 idct
->pipe
->transfer_unmap(idct
->pipe
, buf_transfer
);
543 idct
->pipe
->transfer_destroy(idct
->pipe
, buf_transfer
);
545 /* normalisation constants */
546 vs_consts
= pipe_buffer_map
548 idct
->pipe
, idct
->vs_const_buf
,
549 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
553 vs_consts
->norm
.x
= 1.0f
/ idct
->destination
->width0
;
554 vs_consts
->norm
.y
= 1.0f
/ idct
->destination
->height0
;
556 pipe_buffer_unmap(idct
->pipe
, idct
->vs_const_buf
, buf_transfer
);
560 init_state(struct vl_idct
*idct
)
562 struct pipe_sampler_state sampler
;
565 idct
->num_blocks
= 0;
566 idct
->num_empty_blocks
= 0;
568 idct
->viewport
.scale
[0] = idct
->destination
->width0
;
569 idct
->viewport
.scale
[1] = idct
->destination
->height0
;
570 idct
->viewport
.scale
[2] = 1;
571 idct
->viewport
.scale
[3] = 1;
572 idct
->viewport
.translate
[0] = 0;
573 idct
->viewport
.translate
[1] = 0;
574 idct
->viewport
.translate
[2] = 0;
575 idct
->viewport
.translate
[3] = 0;
577 idct
->fb_state
.width
= idct
->destination
->width0
;
578 idct
->fb_state
.height
= idct
->destination
->height0
;
579 idct
->fb_state
.nr_cbufs
= 1;
580 idct
->fb_state
.zsbuf
= NULL
;
582 for (i
= 0; i
< 4; ++i
) {
583 memset(&sampler
, 0, sizeof(sampler
));
584 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
585 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
586 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
587 sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
588 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
589 sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
590 sampler
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
591 sampler
.compare_func
= PIPE_FUNC_ALWAYS
;
592 sampler
.normalized_coords
= 1;
593 /*sampler.shadow_ambient = ; */
594 /*sampler.lod_bias = ; */
596 /*sampler.max_lod = ; */
597 /*sampler.border_color[0] = ; */
598 /*sampler.max_anisotropy = ; */
599 idct
->samplers
.all
[i
] = idct
->pipe
->create_sampler_state(idct
->pipe
, &sampler
);
604 cleanup_state(struct vl_idct
*idct
)
608 for (i
= 0; i
< 4; ++i
)
609 idct
->pipe
->delete_sampler_state(idct
->pipe
, idct
->samplers
.all
[i
]);
613 vl_idct_init(struct vl_idct
*idct
, struct pipe_context
*pipe
, struct pipe_resource
*dst
)
615 assert(idct
&& pipe
&& dst
);
618 pipe_resource_reference(&idct
->destination
, dst
);
622 if(!init_shaders(idct
))
625 if(!init_buffers(idct
)) {
626 cleanup_shaders(idct
);
630 idct
->surfaces
.intermediate
= idct
->pipe
->screen
->get_tex_surface(
631 idct
->pipe
->screen
, idct
->textures
.individual
.intermediate
, 0, 0, 0,
632 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
);
634 idct
->surfaces
.destination
= idct
->pipe
->screen
->get_tex_surface(
635 idct
->pipe
->screen
, idct
->destination
, 0, 0, 0,
636 PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
);
638 init_constants(idct
);
639 xfer_buffers_map(idct
);
645 vl_idct_cleanup(struct vl_idct
*idct
)
647 idct
->pipe
->screen
->tex_surface_destroy(idct
->surfaces
.destination
);
648 idct
->pipe
->screen
->tex_surface_destroy(idct
->surfaces
.intermediate
);
650 cleanup_shaders(idct
);
651 cleanup_buffers(idct
);
655 pipe_resource_reference(&idct
->destination
, NULL
);
659 vl_idct_add_block(struct vl_idct
*idct
, unsigned x
, unsigned y
, short *block
)
661 struct vertex2f v
, *v_dst
;
671 tex_pitch
= idct
->tex_transfer
->stride
/ util_format_get_blocksize(idct
->tex_transfer
->resource
->format
);
672 texels
= idct
->texels
+ y
* tex_pitch
* BLOCK_HEIGHT
+ x
* BLOCK_WIDTH
;
674 for (i
= 0; i
< BLOCK_HEIGHT
; ++i
)
675 memcpy(texels
+ i
* tex_pitch
, block
+ i
* BLOCK_WIDTH
, BLOCK_WIDTH
* 2);
677 /* non empty blocks fills the vector buffer from left to right */
678 v_dst
= idct
->vectors
+ idct
->num_blocks
* 4;
684 /* while empty blocks fills the vector buffer from right to left */
685 v_dst
= idct
->vectors
+ (idct
->max_blocks
- idct
->num_empty_blocks
) * 4 - 4;
687 idct
->num_empty_blocks
++;
693 for (i
= 0; i
< 4; ++i
) {
699 vl_idct_flush(struct vl_idct
*idct
)
701 xfer_buffers_unmap(idct
);
703 idct
->pipe
->set_constant_buffer(idct
->pipe
, PIPE_SHADER_VERTEX
, 0, idct
->vs_const_buf
);
705 if(idct
->num_blocks
> 0) {
708 idct
->fb_state
.cbufs
[0] = idct
->surfaces
.intermediate
;
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
, 4, idct
->sampler_views
.all
);
715 idct
->pipe
->bind_fragment_sampler_states(idct
->pipe
, 4, idct
->samplers
.all
);
716 idct
->pipe
->bind_vs_state(idct
->pipe
, idct
->vs
);
717 idct
->pipe
->bind_fs_state(idct
->pipe
, idct
->transpose_fs
);
719 util_draw_arrays(idct
->pipe
, PIPE_PRIM_QUADS
, 0, idct
->num_blocks
* 4);
721 idct
->pipe
->flush(idct
->pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
724 idct
->fb_state
.cbufs
[0] = idct
->surfaces
.destination
;
725 idct
->pipe
->set_framebuffer_state(idct
->pipe
, &idct
->fb_state
);
726 idct
->pipe
->set_viewport_state(idct
->pipe
, &idct
->viewport
);
728 idct
->pipe
->set_vertex_buffers(idct
->pipe
, 2, idct
->vertex_bufs
.all
);
729 idct
->pipe
->bind_vertex_elements_state(idct
->pipe
, idct
->vertex_elems_state
);
730 idct
->pipe
->set_fragment_sampler_views(idct
->pipe
, 4, idct
->sampler_views
.all
);
731 idct
->pipe
->bind_fragment_sampler_states(idct
->pipe
, 4, idct
->samplers
.all
);
732 idct
->pipe
->bind_vs_state(idct
->pipe
, idct
->vs
);
733 idct
->pipe
->bind_fs_state(idct
->pipe
, idct
->matrix_fs
);
735 util_draw_arrays(idct
->pipe
, PIPE_PRIM_QUADS
, 0, idct
->num_blocks
* 4);
738 if(idct
->num_empty_blocks
> 0) {
740 /* empty block handling */
741 idct
->fb_state
.cbufs
[0] = idct
->surfaces
.destination
;
742 idct
->pipe
->set_framebuffer_state(idct
->pipe
, &idct
->fb_state
);
743 idct
->pipe
->set_viewport_state(idct
->pipe
, &idct
->viewport
);
745 idct
->pipe
->set_vertex_buffers(idct
->pipe
, 2, idct
->vertex_bufs
.all
);
746 idct
->pipe
->bind_vertex_elements_state(idct
->pipe
, idct
->vertex_elems_state
);
747 idct
->pipe
->set_fragment_sampler_views(idct
->pipe
, 4, idct
->sampler_views
.all
);
748 idct
->pipe
->bind_fragment_sampler_states(idct
->pipe
, 4, idct
->samplers
.all
);
749 idct
->pipe
->bind_vs_state(idct
->pipe
, idct
->vs
);
750 idct
->pipe
->bind_fs_state(idct
->pipe
, idct
->eb_fs
);
752 util_draw_arrays(idct
->pipe
, PIPE_PRIM_QUADS
,
753 (idct
->max_blocks
- idct
->num_empty_blocks
) * 4,
754 idct
->num_empty_blocks
* 4);
757 idct
->pipe
->flush(idct
->pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
759 idct
->num_blocks
= 0;
760 idct
->num_empty_blocks
= 0;
761 xfer_buffers_map(idct
);