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
41 #define SCALE_FACTOR_16_TO_12 (32768.0f / 2048.0f)
42 #define SCALE_FACTOR_9_TO_16 (256.0f / 32768.0f)
44 struct vertex_shader_consts
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
}
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
}
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
}
95 create_vert_shader(struct vl_idct
*idct
)
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
;
103 shader
= ureg_create(TGSI_PROCESSOR_VERTEX
);
107 norm
= ureg_DECL_constant(shader
, 0);
108 bs
= ureg_imm2f(shader
, BLOCK_WIDTH
, BLOCK_HEIGHT
);
110 scale
= ureg_DECL_temporary(shader
);
111 t_vpos
= ureg_DECL_temporary(shader
);
113 vrect
= ureg_DECL_vs_input(shader
, VS_I_RECT
);
114 vpos
= ureg_DECL_vs_input(shader
, VS_I_VPOS
);
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
);
123 * scale = norm * mbs;
125 * t_vpos = vpos + vrect
126 * o_vpos.xy = t_vpos * scale
131 * o_start = vpos * scale
135 ureg_MUL(shader
, ureg_writemask(scale
, TGSI_WRITEMASK_XY
), norm
, bs
);
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
);
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
);
147 ureg_release_temporary(shader
, t_vpos
);
148 ureg_release_temporary(shader
, scale
);
152 return ureg_create_shader_and_destroy(shader
, idct
->pipe
);
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],
161 struct ureg_dst t_tc
[2], m
[2][2], tmp
[2];
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
);
173 * tmp[0..1] = dot4(m[0..1][0], m[0..1][1])
174 * fragment = tmp[0] + tmp[1]
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]);
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]);
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
));
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
));
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]);
196 ureg_MUL(shader
, m
[i
][0], ureg_src(m
[i
][0]), ureg_scalar(ureg_imm1f(shader
, scale
[0]), TGSI_SWIZZLE_X
));
199 ureg_MUL(shader
, m
[i
][1], ureg_src(m
[i
][1]), ureg_scalar(ureg_imm1f(shader
, scale
[1]), TGSI_SWIZZLE_X
));
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]));
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
]);
215 create_transpose_frag_shader(struct vl_idct
*idct
)
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
;
223 shader
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
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
);
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
);
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
);
236 sampler
[0] = ureg_DECL_sampler(shader
, 0);
237 sampler
[1] = ureg_DECL_sampler(shader
, 2);
240 scale
[1] = SCALE_FACTOR_16_TO_12
;
242 fragment
= ureg_DECL_output(shader
, TGSI_SEMANTIC_COLOR
, 0);
244 //matrix_mul(shader, fragment, tc, sampler, start, step, scale);
245 //ureg_MOV(shader, fragment, ureg_imm1f(shader, 0.0f));
249 return ureg_create_shader_and_destroy(shader
, idct
->pipe
);
253 create_matrix_frag_shader(struct vl_idct
*idct
)
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
;
261 shader
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
265 tmp
= ureg_DECL_temporary(shader
);
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
);
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
);
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
);
276 sampler
[0] = ureg_DECL_sampler(shader
, 3);
277 sampler
[1] = ureg_DECL_sampler(shader
, 1);
282 fragment
= ureg_DECL_output(shader
, TGSI_SEMANTIC_COLOR
, 0);
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));
291 return ureg_create_shader_and_destroy(shader
, idct
->pipe
);
295 xfer_buffers_map(struct vl_idct
*idct
)
297 struct pipe_box rect
=
300 idct
->destination
->width0
,
301 idct
->destination
->height0
,
305 idct
->tex_transfer
= idct
->pipe
->get_transfer
308 idct
->pipe
, idct
->textures
.individual
.intermediate
,
310 idct
->pipe
, idct
->destination
,
313 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
317 idct
->texels
= idct
->pipe
->transfer_map(idct
->pipe
, idct
->tex_transfer
);
319 idct
->vectors
= pipe_buffer_map
322 idct
->vertex_bufs
.individual
.pos
.buffer
,
323 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
329 xfer_buffers_unmap(struct vl_idct
*idct
)
331 pipe_buffer_unmap(idct
->pipe
, idct
->vertex_bufs
.individual
.pos
.buffer
, idct
->vec_transfer
);
333 idct
->pipe
->transfer_unmap(idct
->pipe
, idct
->tex_transfer
);
334 idct
->pipe
->transfer_destroy(idct
->pipe
, idct
->tex_transfer
);
338 init_shaders(struct vl_idct
*idct
)
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
));
350 cleanup_shaders(struct vl_idct
*idct
)
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
);
360 init_buffers(struct vl_idct
*idct
)
362 struct pipe_resource
template;
363 struct pipe_sampler_view sampler_view
;
364 struct pipe_vertex_element vertex_elems
[2];
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
;
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;
378 template.height0
= 8;
380 template.usage
= PIPE_USAGE_IMMUTABLE
;
381 template.bind
= PIPE_BIND_SAMPLER_VIEW
;
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);
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);
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);
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
);
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
409 PIPE_BIND_VERTEX_BUFFER
,
410 sizeof(struct vertex2f
) * 4 * max_blocks
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
419 PIPE_BIND_VERTEX_BUFFER
,
420 sizeof(struct vertex2f
) * 4 * max_blocks
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
;
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
;
435 idct
->vertex_elems_state
= idct
->pipe
->create_vertex_elements_state(idct
->pipe
, 2, vertex_elems
);
437 idct
->vs_const_buf
= pipe_buffer_create
440 PIPE_BIND_CONSTANT_BUFFER
,
441 sizeof(struct vertex_shader_consts
)
448 cleanup_buffers(struct vl_idct
*idct
)
454 pipe_resource_reference(&idct
->vs_const_buf
, NULL
);
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
);
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
);
467 init_constants(struct vl_idct
*idct
)
469 struct pipe_transfer
*buf_transfer
;
470 struct vertex_shader_consts
*vs_consts
;
478 idct
->vertex_bufs
.individual
.quad
.buffer
,
479 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
483 for ( i
= 0; i
<= idct
->vertex_bufs
.individual
.quad
.max_index
; i
+= 4)
484 memcpy(v
+ i
, &const_quad
, sizeof(const_quad
));
486 pipe_buffer_unmap(idct
->pipe
, idct
->vertex_bufs
.individual
.quad
.buffer
, buf_transfer
);
492 idct
->textures
.individual
.matrix
,
493 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
497 memcpy(v
, &const_matrix
, sizeof(const_matrix
));
499 pipe_buffer_unmap(idct
->pipe
, idct
->textures
.individual
.matrix
, buf_transfer
);
504 idct
->textures
.individual
.transpose
,
505 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
509 memcpy(v
, &const_transpose
, sizeof(const_transpose
));
511 pipe_buffer_unmap(idct
->pipe
, idct
->textures
.individual
.transpose
, buf_transfer
);
513 vs_consts
= pipe_buffer_map
515 idct
->pipe
, idct
->vs_const_buf
,
516 PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_DISCARD
,
520 vs_consts
->norm
.x
= 1.0f
/ idct
->destination
->width0
;
521 vs_consts
->norm
.y
= 1.0f
/ idct
->destination
->height0
;
523 pipe_buffer_unmap(idct
->pipe
, idct
->vs_const_buf
, buf_transfer
);
527 init_state(struct vl_idct
*idct
)
529 struct pipe_sampler_state sampler
;
532 idct
->num_blocks
= 0;
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;
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
;
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 = ; */
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
);
570 cleanup_state(struct vl_idct
*idct
)
574 for (i
= 0; i
< 4; ++i
)
575 idct
->pipe
->delete_sampler_state(idct
->pipe
, idct
->samplers
.all
[i
]);
578 bool vl_idct_init(struct vl_idct
*idct
, struct pipe_context
*pipe
, struct pipe_resource
*dst
)
580 assert(idct
&& pipe
&& dst
);
583 pipe_resource_reference(&idct
->destination
, dst
);
587 if(!init_shaders(idct
))
590 if(!init_buffers(idct
)) {
591 cleanup_shaders(idct
);
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
);
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
);
603 init_constants(idct
);
604 xfer_buffers_map(idct
);
609 void vl_idct_cleanup(struct vl_idct
*idct
)
611 idct
->pipe
->screen
->tex_surface_destroy(idct
->surfaces
.destination
);
612 idct
->pipe
->screen
->tex_surface_destroy(idct
->surfaces
.intermediate
);
614 cleanup_shaders(idct
);
615 cleanup_buffers(idct
);
619 pipe_resource_reference(&idct
->destination
, NULL
);
622 void vl_idct_add_block(struct vl_idct
*idct
, unsigned x
, unsigned y
, short *block
)
637 for (i
= 0; i
< 4; ++i
) {
638 idct
->vectors
[idct
->num_blocks
* 4 + i
] = v
;
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
;
644 for (i
= 0; i
< BLOCK_HEIGHT
; ++i
)
645 memcpy(texels
+ i
* tex_pitch
, block
+ i
* BLOCK_WIDTH
, BLOCK_WIDTH
* 2);
650 void vl_idct_flush(struct vl_idct
*idct
)
652 xfer_buffers_unmap(idct
);
654 idct
->pipe
->set_constant_buffer(idct
->pipe
, PIPE_SHADER_VERTEX
, 0, idct
->vs_const_buf
);
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
);
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
);
669 util_draw_arrays(idct
->pipe
, PIPE_PRIM_QUADS
, 0, idct
->num_blocks
* 4);
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
);
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
);
683 util_draw_arrays(idct
->pipe
, PIPE_PRIM_QUADS
, 0, idct
->num_blocks
* 4);
685 idct
->pipe
->flush(idct
->pipe
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
688 idct
->num_blocks
= 0;
689 xfer_buffers_map(idct
);