1 /**************************************************************************
3 * Copyright 2009 Younes Manton.
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 **************************************************************************/
28 #include "vl_mpeg12_mc_renderer.h"
30 #include <pipe/p_context.h>
31 #include <pipe/p_inlines.h>
32 #include <util/u_format.h>
33 #include <util/u_math.h>
34 #include <util/u_memory.h>
35 #include <tgsi/tgsi_parse.h>
36 #include <tgsi/tgsi_build.h>
37 #include "vl_shader_build.h"
39 #define DEFAULT_BUF_ALIGNMENT 1
40 #define MACROBLOCK_WIDTH 16
41 #define MACROBLOCK_HEIGHT 16
43 #define BLOCK_HEIGHT 8
44 #define ZERO_BLOCK_NIL -1.0f
45 #define ZERO_BLOCK_IS_NIL(zb) ((zb).x < 0.0f)
57 struct vertex_shader_consts
59 struct vertex4f denorm
;
62 struct fragment_shader_consts
64 struct vertex4f multiplier
;
69 * Muliplier renormalizes block samples from 16 bits to 12 bits.
70 * Divider is used when calculating Y % 2 for choosing top or bottom
71 * field for P or B macroblocks.
72 * TODO: Use immediates.
74 static const struct fragment_shader_consts fs_consts
= {
75 {32767.0f
/ 255.0f
, 32767.0f
/ 255.0f
, 32767.0f
/ 255.0f
, 0.0f
},
76 {0.5f
, 2.0f
, 0.0f
, 0.0f
}
82 struct vertex2f luma_tc
;
83 struct vertex2f cb_tc
;
84 struct vertex2f cr_tc
;
89 MACROBLOCK_TYPE_INTRA
,
90 MACROBLOCK_TYPE_FWD_FRAME_PRED
,
91 MACROBLOCK_TYPE_FWD_FIELD_PRED
,
92 MACROBLOCK_TYPE_BKWD_FRAME_PRED
,
93 MACROBLOCK_TYPE_BKWD_FIELD_PRED
,
94 MACROBLOCK_TYPE_BI_FRAME_PRED
,
95 MACROBLOCK_TYPE_BI_FIELD_PRED
,
101 create_intra_vert_shader(struct vl_mpeg12_mc_renderer
*r
)
103 const unsigned max_tokens
= 50;
105 struct pipe_shader_state vs
;
106 struct tgsi_token
*tokens
;
107 struct tgsi_header
*header
;
109 struct tgsi_full_declaration decl
;
110 struct tgsi_full_instruction inst
;
118 tokens
= (struct tgsi_token
*) malloc(max_tokens
* sizeof(struct tgsi_token
));
119 header
= (struct tgsi_header
*) &tokens
[0];
120 *header
= tgsi_build_header();
121 *(struct tgsi_processor
*) &tokens
[1] = tgsi_build_processor(TGSI_PROCESSOR_VERTEX
, header
);
126 * decl i0 ; Vertex pos
127 * decl i1 ; Luma texcoords
128 * decl i2 ; Chroma Cb texcoords
129 * decl i3 ; Chroma Cr texcoords
131 for (i
= 0; i
< 4; i
++) {
132 decl
= vl_decl_input(i
== 0 ? TGSI_SEMANTIC_POSITION
: TGSI_SEMANTIC_GENERIC
, i
, i
, i
);
133 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
137 * decl o0 ; Vertex pos
138 * decl o1 ; Luma texcoords
139 * decl o2 ; Chroma Cb texcoords
140 * decl o3 ; Chroma Cr texcoords
142 for (i
= 0; i
< 4; i
++) {
143 decl
= vl_decl_output(i
== 0 ? TGSI_SEMANTIC_POSITION
: TGSI_SEMANTIC_GENERIC
, i
, i
, i
);
144 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
148 * mov o0, i0 ; Move input vertex pos to output
149 * mov o1, i1 ; Move input luma texcoords to output
150 * mov o2, i2 ; Move input chroma Cb texcoords to output
151 * mov o3, i3 ; Move input chroma Cr texcoords to output
153 for (i
= 0; i
< 4; ++i
) {
154 inst
= vl_inst2(TGSI_OPCODE_MOV
, TGSI_FILE_OUTPUT
, i
, TGSI_FILE_INPUT
, i
);
155 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
160 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
162 assert(ti
<= max_tokens
);
165 r
->i_vs
= r
->pipe
->create_vs_state(r
->pipe
, &vs
);
170 create_intra_frag_shader(struct vl_mpeg12_mc_renderer
*r
)
172 const unsigned max_tokens
= 100;
174 struct pipe_shader_state fs
;
175 struct tgsi_token
*tokens
;
176 struct tgsi_header
*header
;
178 struct tgsi_full_declaration decl
;
179 struct tgsi_full_instruction inst
;
187 tokens
= (struct tgsi_token
*) malloc(max_tokens
* sizeof(struct tgsi_token
));
188 header
= (struct tgsi_header
*) &tokens
[0];
189 *header
= tgsi_build_header();
190 *(struct tgsi_processor
*) &tokens
[1] = tgsi_build_processor(TGSI_PROCESSOR_FRAGMENT
, header
);
195 * decl i0 ; Luma texcoords
196 * decl i1 ; Chroma Cb texcoords
197 * decl i2 ; Chroma Cr texcoords
199 for (i
= 0; i
< 3; ++i
) {
200 decl
= vl_decl_interpolated_input(TGSI_SEMANTIC_GENERIC
, i
+ 1, i
, i
, TGSI_INTERPOLATE_LINEAR
);
201 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
204 /* decl c0 ; Scaling factor, rescales 16-bit snorm to 9-bit snorm */
205 decl
= vl_decl_constants(TGSI_SEMANTIC_GENERIC
, 0, 0, 0);
206 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
208 /* decl o0 ; Fragment color */
209 decl
= vl_decl_output(TGSI_SEMANTIC_COLOR
, 0, 0, 0);
210 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
213 decl
= vl_decl_temps(0, 1);
214 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
217 * decl s0 ; Sampler for luma texture
218 * decl s1 ; Sampler for chroma Cb texture
219 * decl s2 ; Sampler for chroma Cr texture
221 for (i
= 0; i
< 3; ++i
) {
222 decl
= vl_decl_samplers(i
, i
);
223 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
227 * tex2d t1, i0, s0 ; Read texel from luma texture
228 * mov t0.x, t1.x ; Move luma sample into .x component
229 * tex2d t1, i1, s1 ; Read texel from chroma Cb texture
230 * mov t0.y, t1.x ; Move Cb sample into .y component
231 * tex2d t1, i2, s2 ; Read texel from chroma Cr texture
232 * mov t0.z, t1.x ; Move Cr sample into .z component
234 for (i
= 0; i
< 3; ++i
) {
235 inst
= vl_tex(TGSI_TEXTURE_2D
, TGSI_FILE_TEMPORARY
, 1, TGSI_FILE_INPUT
, i
, TGSI_FILE_SAMPLER
, i
);
236 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
238 inst
= vl_inst2(TGSI_OPCODE_MOV
, TGSI_FILE_TEMPORARY
, 0, TGSI_FILE_TEMPORARY
, 1);
239 inst
.Src
[0].Register
.SwizzleX
= TGSI_SWIZZLE_X
;
240 inst
.Src
[0].Register
.SwizzleY
= TGSI_SWIZZLE_X
;
241 inst
.Src
[0].Register
.SwizzleZ
= TGSI_SWIZZLE_X
;
242 inst
.Dst
[0].Register
.WriteMask
= TGSI_WRITEMASK_X
<< i
;
243 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
246 /* mul o0, t0, c0 ; Rescale texel to correct range */
247 inst
= vl_inst3(TGSI_OPCODE_MUL
, TGSI_FILE_OUTPUT
, 0, TGSI_FILE_TEMPORARY
, 0, TGSI_FILE_CONSTANT
, 0);
248 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
252 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
254 assert(ti
<= max_tokens
);
257 r
->i_fs
= r
->pipe
->create_fs_state(r
->pipe
, &fs
);
262 create_frame_pred_vert_shader(struct vl_mpeg12_mc_renderer
*r
)
264 const unsigned max_tokens
= 100;
266 struct pipe_shader_state vs
;
267 struct tgsi_token
*tokens
;
268 struct tgsi_header
*header
;
270 struct tgsi_full_declaration decl
;
271 struct tgsi_full_instruction inst
;
279 tokens
= (struct tgsi_token
*) malloc(max_tokens
* sizeof(struct tgsi_token
));
280 header
= (struct tgsi_header
*) &tokens
[0];
281 *header
= tgsi_build_header();
282 *(struct tgsi_processor
*) &tokens
[1] = tgsi_build_processor(TGSI_PROCESSOR_VERTEX
, header
);
287 * decl i0 ; Vertex pos
288 * decl i1 ; Luma texcoords
289 * decl i2 ; Chroma Cb texcoords
290 * decl i3 ; Chroma Cr texcoords
291 * decl i4 ; Ref surface top field texcoords
292 * decl i5 ; Ref surface bottom field texcoords (unused, packed in the same stream)
294 for (i
= 0; i
< 6; i
++) {
295 decl
= vl_decl_input(i
== 0 ? TGSI_SEMANTIC_POSITION
: TGSI_SEMANTIC_GENERIC
, i
, i
, i
);
296 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
300 * decl o0 ; Vertex pos
301 * decl o1 ; Luma texcoords
302 * decl o2 ; Chroma Cb texcoords
303 * decl o3 ; Chroma Cr texcoords
304 * decl o4 ; Ref macroblock texcoords
306 for (i
= 0; i
< 5; i
++) {
307 decl
= vl_decl_output(i
== 0 ? TGSI_SEMANTIC_POSITION
: TGSI_SEMANTIC_GENERIC
, i
, i
, i
);
308 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
312 * mov o0, i0 ; Move input vertex pos to output
313 * mov o1, i1 ; Move input luma texcoords to output
314 * mov o2, i2 ; Move input chroma Cb texcoords to output
315 * mov o3, i3 ; Move input chroma Cr texcoords to output
317 for (i
= 0; i
< 4; ++i
) {
318 inst
= vl_inst2(TGSI_OPCODE_MOV
, TGSI_FILE_OUTPUT
, i
, TGSI_FILE_INPUT
, i
);
319 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
322 /* add o4, i0, i4 ; Translate vertex pos by motion vec to form ref macroblock texcoords */
323 inst
= vl_inst3(TGSI_OPCODE_ADD
, TGSI_FILE_OUTPUT
, 4, TGSI_FILE_INPUT
, 0, TGSI_FILE_INPUT
, 4);
324 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
328 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
330 assert(ti
<= max_tokens
);
333 r
->p_vs
[0] = r
->pipe
->create_vs_state(r
->pipe
, &vs
);
339 create_field_pred_vert_shader(struct vl_mpeg12_mc_renderer
*r
)
346 create_frame_pred_frag_shader(struct vl_mpeg12_mc_renderer
*r
)
348 const unsigned max_tokens
= 100;
350 struct pipe_shader_state fs
;
351 struct tgsi_token
*tokens
;
352 struct tgsi_header
*header
;
354 struct tgsi_full_declaration decl
;
355 struct tgsi_full_instruction inst
;
363 tokens
= (struct tgsi_token
*) malloc(max_tokens
* sizeof(struct tgsi_token
));
364 header
= (struct tgsi_header
*) &tokens
[0];
365 *header
= tgsi_build_header();
366 *(struct tgsi_processor
*) &tokens
[1] = tgsi_build_processor(TGSI_PROCESSOR_FRAGMENT
, header
);
371 * decl i0 ; Luma texcoords
372 * decl i1 ; Chroma Cb texcoords
373 * decl i2 ; Chroma Cr texcoords
374 * decl i3 ; Ref macroblock texcoords
376 for (i
= 0; i
< 4; ++i
) {
377 decl
= vl_decl_interpolated_input(TGSI_SEMANTIC_GENERIC
, i
+ 1, i
, i
, TGSI_INTERPOLATE_LINEAR
);
378 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
381 /* decl c0 ; Scaling factor, rescales 16-bit snorm to 9-bit snorm */
382 decl
= vl_decl_constants(TGSI_SEMANTIC_GENERIC
, 0, 0, 0);
383 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
385 /* decl o0 ; Fragment color */
386 decl
= vl_decl_output(TGSI_SEMANTIC_COLOR
, 0, 0, 0);
387 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
390 decl
= vl_decl_temps(0, 1);
391 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
394 * decl s0 ; Sampler for luma texture
395 * decl s1 ; Sampler for chroma Cb texture
396 * decl s2 ; Sampler for chroma Cr texture
397 * decl s3 ; Sampler for ref surface texture
399 for (i
= 0; i
< 4; ++i
) {
400 decl
= vl_decl_samplers(i
, i
);
401 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
405 * tex2d t1, i0, s0 ; Read texel from luma texture
406 * mov t0.x, t1.x ; Move luma sample into .x component
407 * tex2d t1, i1, s1 ; Read texel from chroma Cb texture
408 * mov t0.y, t1.x ; Move Cb sample into .y component
409 * tex2d t1, i2, s2 ; Read texel from chroma Cr texture
410 * mov t0.z, t1.x ; Move Cr sample into .z component
412 for (i
= 0; i
< 3; ++i
) {
413 inst
= vl_tex(TGSI_TEXTURE_2D
, TGSI_FILE_TEMPORARY
, 1, TGSI_FILE_INPUT
, i
, TGSI_FILE_SAMPLER
, i
);
414 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
416 inst
= vl_inst2(TGSI_OPCODE_MOV
, TGSI_FILE_TEMPORARY
, 0, TGSI_FILE_TEMPORARY
, 1);
417 inst
.Src
[0].Register
.SwizzleX
= TGSI_SWIZZLE_X
;
418 inst
.Src
[0].Register
.SwizzleY
= TGSI_SWIZZLE_X
;
419 inst
.Src
[0].Register
.SwizzleZ
= TGSI_SWIZZLE_X
;
420 inst
.Dst
[0].Register
.WriteMask
= TGSI_WRITEMASK_X
<< i
;
421 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
424 /* mul t0, t0, c0 ; Rescale texel to correct range */
425 inst
= vl_inst3(TGSI_OPCODE_MUL
, TGSI_FILE_TEMPORARY
, 0, TGSI_FILE_TEMPORARY
, 0, TGSI_FILE_CONSTANT
, 0);
426 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
428 /* tex2d t1, i3, s3 ; Read texel from ref macroblock */
429 inst
= vl_tex(TGSI_TEXTURE_2D
, TGSI_FILE_TEMPORARY
, 1, TGSI_FILE_INPUT
, 3, TGSI_FILE_SAMPLER
, 3);
430 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
432 /* add o0, t0, t1 ; Add ref and differential to form final output */
433 inst
= vl_inst3(TGSI_OPCODE_ADD
, TGSI_FILE_OUTPUT
, 0, TGSI_FILE_TEMPORARY
, 0, TGSI_FILE_TEMPORARY
, 1);
434 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
438 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
440 assert(ti
<= max_tokens
);
443 r
->p_fs
[0] = r
->pipe
->create_fs_state(r
->pipe
, &fs
);
449 create_field_pred_frag_shader(struct vl_mpeg12_mc_renderer
*r
)
456 create_frame_bi_pred_vert_shader(struct vl_mpeg12_mc_renderer
*r
)
458 const unsigned max_tokens
= 100;
460 struct pipe_shader_state vs
;
461 struct tgsi_token
*tokens
;
462 struct tgsi_header
*header
;
464 struct tgsi_full_declaration decl
;
465 struct tgsi_full_instruction inst
;
473 tokens
= (struct tgsi_token
*) malloc(max_tokens
* sizeof(struct tgsi_token
));
474 header
= (struct tgsi_header
*) &tokens
[0];
475 *header
= tgsi_build_header();
476 *(struct tgsi_processor
*) &tokens
[1] = tgsi_build_processor(TGSI_PROCESSOR_VERTEX
, header
);
481 * decl i0 ; Vertex pos
482 * decl i1 ; Luma texcoords
483 * decl i2 ; Chroma Cb texcoords
484 * decl i3 ; Chroma Cr texcoords
485 * decl i4 ; First ref macroblock top field texcoords
486 * decl i5 ; First ref macroblock bottom field texcoords (unused, packed in the same stream)
487 * decl i6 ; Second ref macroblock top field texcoords
488 * decl i7 ; Second ref macroblock bottom field texcoords (unused, packed in the same stream)
490 for (i
= 0; i
< 8; i
++) {
491 decl
= vl_decl_input(i
== 0 ? TGSI_SEMANTIC_POSITION
: TGSI_SEMANTIC_GENERIC
, i
, i
, i
);
492 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
496 * decl o0 ; Vertex pos
497 * decl o1 ; Luma texcoords
498 * decl o2 ; Chroma Cb texcoords
499 * decl o3 ; Chroma Cr texcoords
500 * decl o4 ; First ref macroblock texcoords
501 * decl o5 ; Second ref macroblock texcoords
503 for (i
= 0; i
< 6; i
++) {
504 decl
= vl_decl_output(i
== 0 ? TGSI_SEMANTIC_POSITION
: TGSI_SEMANTIC_GENERIC
, i
, i
, i
);
505 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
509 * mov o0, i0 ; Move input vertex pos to output
510 * mov o1, i1 ; Move input luma texcoords to output
511 * mov o2, i2 ; Move input chroma Cb texcoords to output
512 * mov o3, i3 ; Move input chroma Cr texcoords to output
514 for (i
= 0; i
< 4; ++i
) {
515 inst
= vl_inst2(TGSI_OPCODE_MOV
, TGSI_FILE_OUTPUT
, i
, TGSI_FILE_INPUT
, i
);
516 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
520 * add o4, i0, i4 ; Translate vertex pos by motion vec to form first ref macroblock texcoords
521 * add o5, i0, i6 ; Translate vertex pos by motion vec to form second ref macroblock texcoords
523 for (i
= 0; i
< 2; ++i
) {
524 inst
= vl_inst3(TGSI_OPCODE_ADD
, TGSI_FILE_OUTPUT
, i
+ 4, TGSI_FILE_INPUT
, 0, TGSI_FILE_INPUT
, (i
+ 2) * 2);
525 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
530 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
532 assert(ti
<= max_tokens
);
535 r
->b_vs
[0] = r
->pipe
->create_vs_state(r
->pipe
, &vs
);
541 create_field_bi_pred_vert_shader(struct vl_mpeg12_mc_renderer
*r
)
548 create_frame_bi_pred_frag_shader(struct vl_mpeg12_mc_renderer
*r
)
550 const unsigned max_tokens
= 100;
552 struct pipe_shader_state fs
;
553 struct tgsi_token
*tokens
;
554 struct tgsi_header
*header
;
556 struct tgsi_full_declaration decl
;
557 struct tgsi_full_instruction inst
;
565 tokens
= (struct tgsi_token
*) malloc(max_tokens
* sizeof(struct tgsi_token
));
566 header
= (struct tgsi_header
*) &tokens
[0];
567 *header
= tgsi_build_header();
568 *(struct tgsi_processor
*) &tokens
[1] = tgsi_build_processor(TGSI_PROCESSOR_FRAGMENT
, header
);
573 * decl i0 ; Luma texcoords
574 * decl i1 ; Chroma Cb texcoords
575 * decl i2 ; Chroma Cr texcoords
576 * decl i3 ; First ref macroblock texcoords
577 * decl i4 ; Second ref macroblock texcoords
579 for (i
= 0; i
< 5; ++i
) {
580 decl
= vl_decl_interpolated_input(TGSI_SEMANTIC_GENERIC
, i
+ 1, i
, i
, TGSI_INTERPOLATE_LINEAR
);
581 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
585 * decl c0 ; Scaling factor, rescales 16-bit snorm to 9-bit snorm
586 * decl c1 ; Constant 1/2 in .x channel to use as weight to blend past and future texels
588 decl
= vl_decl_constants(TGSI_SEMANTIC_GENERIC
, 0, 0, 1);
589 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
591 /* decl o0 ; Fragment color */
592 decl
= vl_decl_output(TGSI_SEMANTIC_COLOR
, 0, 0, 0);
593 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
596 decl
= vl_decl_temps(0, 2);
597 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
600 * decl s0 ; Sampler for luma texture
601 * decl s1 ; Sampler for chroma Cb texture
602 * decl s2 ; Sampler for chroma Cr texture
603 * decl s3 ; Sampler for first ref surface texture
604 * decl s4 ; Sampler for second ref surface texture
606 for (i
= 0; i
< 5; ++i
) {
607 decl
= vl_decl_samplers(i
, i
);
608 ti
+= tgsi_build_full_declaration(&decl
, &tokens
[ti
], header
, max_tokens
- ti
);
612 * tex2d t1, i0, s0 ; Read texel from luma texture
613 * mov t0.x, t1.x ; Move luma sample into .x component
614 * tex2d t1, i1, s1 ; Read texel from chroma Cb texture
615 * mov t0.y, t1.x ; Move Cb sample into .y component
616 * tex2d t1, i2, s2 ; Read texel from chroma Cr texture
617 * mov t0.z, t1.x ; Move Cr sample into .z component
619 for (i
= 0; i
< 3; ++i
) {
620 inst
= vl_tex(TGSI_TEXTURE_2D
, TGSI_FILE_TEMPORARY
, 1, TGSI_FILE_INPUT
, i
, TGSI_FILE_SAMPLER
, i
);
621 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
623 inst
= vl_inst2(TGSI_OPCODE_MOV
, TGSI_FILE_TEMPORARY
, 0, TGSI_FILE_TEMPORARY
, 1);
624 inst
.Src
[0].Register
.SwizzleX
= TGSI_SWIZZLE_X
;
625 inst
.Src
[0].Register
.SwizzleY
= TGSI_SWIZZLE_X
;
626 inst
.Src
[0].Register
.SwizzleZ
= TGSI_SWIZZLE_X
;
627 inst
.Dst
[0].Register
.WriteMask
= TGSI_WRITEMASK_X
<< i
;
628 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
631 /* mul t0, t0, c0 ; Rescale texel to correct range */
632 inst
= vl_inst3(TGSI_OPCODE_MUL
, TGSI_FILE_TEMPORARY
, 0, TGSI_FILE_TEMPORARY
, 0, TGSI_FILE_CONSTANT
, 0);
633 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
636 * tex2d t1, i3, s3 ; Read texel from first ref macroblock
637 * tex2d t2, i4, s4 ; Read texel from second ref macroblock
639 for (i
= 0; i
< 2; ++i
) {
640 inst
= vl_tex(TGSI_TEXTURE_2D
, TGSI_FILE_TEMPORARY
, i
+ 1, TGSI_FILE_INPUT
, i
+ 3, TGSI_FILE_SAMPLER
, i
+ 3);
641 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
644 /* lerp t1, c1.x, t1, t2 ; Blend past and future texels */
645 inst
= vl_inst4(TGSI_OPCODE_LRP
, TGSI_FILE_TEMPORARY
, 1, TGSI_FILE_CONSTANT
, 1, TGSI_FILE_TEMPORARY
, 1, TGSI_FILE_TEMPORARY
, 2);
646 inst
.Src
[0].Register
.SwizzleX
= TGSI_SWIZZLE_X
;
647 inst
.Src
[0].Register
.SwizzleY
= TGSI_SWIZZLE_X
;
648 inst
.Src
[0].Register
.SwizzleZ
= TGSI_SWIZZLE_X
;
649 inst
.Src
[0].Register
.SwizzleW
= TGSI_SWIZZLE_X
;
650 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
652 /* add o0, t0, t1 ; Add past/future ref and differential to form final output */
653 inst
= vl_inst3(TGSI_OPCODE_ADD
, TGSI_FILE_OUTPUT
, 0, TGSI_FILE_TEMPORARY
, 0, TGSI_FILE_TEMPORARY
, 1);
654 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
658 ti
+= tgsi_build_full_instruction(&inst
, &tokens
[ti
], header
, max_tokens
- ti
);
660 assert(ti
<= max_tokens
);
663 r
->b_fs
[0] = r
->pipe
->create_fs_state(r
->pipe
, &fs
);
669 create_field_bi_pred_frag_shader(struct vl_mpeg12_mc_renderer
*r
)
676 xfer_buffers_map(struct vl_mpeg12_mc_renderer
*r
)
682 for (i
= 0; i
< 3; ++i
) {
683 r
->tex_transfer
[i
] = r
->pipe
->screen
->get_tex_transfer
685 r
->pipe
->screen
, r
->textures
.all
[i
],
686 0, 0, 0, PIPE_TRANSFER_WRITE
, 0, 0,
687 r
->textures
.all
[i
]->width0
, r
->textures
.all
[i
]->height0
690 r
->texels
[i
] = r
->pipe
->screen
->transfer_map(r
->pipe
->screen
, r
->tex_transfer
[i
]);
695 xfer_buffers_unmap(struct vl_mpeg12_mc_renderer
*r
)
701 for (i
= 0; i
< 3; ++i
) {
702 r
->pipe
->screen
->transfer_unmap(r
->pipe
->screen
, r
->tex_transfer
[i
]);
703 r
->pipe
->screen
->tex_transfer_destroy(r
->tex_transfer
[i
]);
708 init_pipe_state(struct vl_mpeg12_mc_renderer
*r
)
710 struct pipe_sampler_state sampler
;
716 r
->viewport
.scale
[0] = r
->pot_buffers
?
717 util_next_power_of_two(r
->picture_width
) : r
->picture_width
;
718 r
->viewport
.scale
[1] = r
->pot_buffers
?
719 util_next_power_of_two(r
->picture_height
) : r
->picture_height
;
720 r
->viewport
.scale
[2] = 1;
721 r
->viewport
.scale
[3] = 1;
722 r
->viewport
.translate
[0] = 0;
723 r
->viewport
.translate
[1] = 0;
724 r
->viewport
.translate
[2] = 0;
725 r
->viewport
.translate
[3] = 0;
727 r
->scissor
.maxx
= r
->pot_buffers
?
728 util_next_power_of_two(r
->picture_width
) : r
->picture_width
;
729 r
->scissor
.maxy
= r
->pot_buffers
?
730 util_next_power_of_two(r
->picture_height
) : r
->picture_height
;
732 r
->fb_state
.width
= r
->pot_buffers
?
733 util_next_power_of_two(r
->picture_width
) : r
->picture_width
;
734 r
->fb_state
.height
= r
->pot_buffers
?
735 util_next_power_of_two(r
->picture_height
) : r
->picture_height
;
736 r
->fb_state
.nr_cbufs
= 1;
737 r
->fb_state
.zsbuf
= NULL
;
740 filters
[0] = PIPE_TEX_FILTER_NEAREST
;
742 if (r
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_444
||
743 r
->eb_handling
== VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE
) {
744 filters
[1] = PIPE_TEX_FILTER_NEAREST
;
745 filters
[2] = PIPE_TEX_FILTER_NEAREST
;
748 filters
[1] = PIPE_TEX_FILTER_LINEAR
;
749 filters
[2] = PIPE_TEX_FILTER_LINEAR
;
751 /* Fwd, bkwd ref filters */
752 filters
[3] = PIPE_TEX_FILTER_LINEAR
;
753 filters
[4] = PIPE_TEX_FILTER_LINEAR
;
755 for (i
= 0; i
< 5; ++i
) {
756 sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
757 sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
758 sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
759 sampler
.min_img_filter
= filters
[i
];
760 sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
761 sampler
.mag_img_filter
= filters
[i
];
762 sampler
.compare_mode
= PIPE_TEX_COMPARE_NONE
;
763 sampler
.compare_func
= PIPE_FUNC_ALWAYS
;
764 sampler
.normalized_coords
= 1;
765 /*sampler.prefilter = ; */
766 /*sampler.shadow_ambient = ; */
767 /*sampler.lod_bias = ; */
769 /*sampler.max_lod = ; */
770 /*sampler.border_color[i] = ; */
771 /*sampler.max_anisotropy = ; */
772 r
->samplers
.all
[i
] = r
->pipe
->create_sampler_state(r
->pipe
, &sampler
);
779 cleanup_pipe_state(struct vl_mpeg12_mc_renderer
*r
)
785 for (i
= 0; i
< 5; ++i
)
786 r
->pipe
->delete_sampler_state(r
->pipe
, r
->samplers
.all
[i
]);
790 init_shaders(struct vl_mpeg12_mc_renderer
*r
)
794 create_intra_vert_shader(r
);
795 create_intra_frag_shader(r
);
796 create_frame_pred_vert_shader(r
);
797 create_frame_pred_frag_shader(r
);
798 create_frame_bi_pred_vert_shader(r
);
799 create_frame_bi_pred_frag_shader(r
);
805 cleanup_shaders(struct vl_mpeg12_mc_renderer
*r
)
809 r
->pipe
->delete_vs_state(r
->pipe
, r
->i_vs
);
810 r
->pipe
->delete_fs_state(r
->pipe
, r
->i_fs
);
811 r
->pipe
->delete_vs_state(r
->pipe
, r
->p_vs
[0]);
812 r
->pipe
->delete_fs_state(r
->pipe
, r
->p_fs
[0]);
813 r
->pipe
->delete_vs_state(r
->pipe
, r
->b_vs
[0]);
814 r
->pipe
->delete_fs_state(r
->pipe
, r
->b_fs
[0]);
818 init_buffers(struct vl_mpeg12_mc_renderer
*r
)
820 struct pipe_texture
template;
823 align(r
->picture_width
, MACROBLOCK_WIDTH
) / MACROBLOCK_WIDTH
;
825 align(r
->picture_height
, MACROBLOCK_HEIGHT
) / MACROBLOCK_HEIGHT
;
831 r
->macroblocks_per_batch
=
832 mbw
* (r
->bufmode
== VL_MPEG12_MC_RENDERER_BUFFER_PICTURE
? mbh
: 1);
833 r
->num_macroblocks
= 0;
834 r
->macroblock_buf
= MALLOC(r
->macroblocks_per_batch
* sizeof(struct pipe_mpeg12_macroblock
));
836 memset(&template, 0, sizeof(struct pipe_texture
));
837 template.target
= PIPE_TEXTURE_2D
;
838 /* TODO: Accomodate HW that can't do this and also for cases when this isn't precise enough */
839 template.format
= PIPE_FORMAT_R16_SNORM
;
840 template.last_level
= 0;
841 template.width0
= r
->pot_buffers
?
842 util_next_power_of_two(r
->picture_width
) : r
->picture_width
;
843 template.height0
= r
->pot_buffers
?
844 util_next_power_of_two(r
->picture_height
) : r
->picture_height
;
846 template.tex_usage
= PIPE_TEXTURE_USAGE_SAMPLER
| PIPE_TEXTURE_USAGE_DYNAMIC
;
848 r
->textures
.individual
.y
= r
->pipe
->screen
->texture_create(r
->pipe
->screen
, &template);
850 if (r
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
) {
851 template.width0
= r
->pot_buffers
?
852 util_next_power_of_two(r
->picture_width
/ 2) :
853 r
->picture_width
/ 2;
854 template.height0
= r
->pot_buffers
?
855 util_next_power_of_two(r
->picture_height
/ 2) :
856 r
->picture_height
/ 2;
858 else if (r
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_422
)
859 template.height0
= r
->pot_buffers
?
860 util_next_power_of_two(r
->picture_height
/ 2) :
861 r
->picture_height
/ 2;
863 r
->textures
.individual
.cb
=
864 r
->pipe
->screen
->texture_create(r
->pipe
->screen
, &template);
865 r
->textures
.individual
.cr
=
866 r
->pipe
->screen
->texture_create(r
->pipe
->screen
, &template);
868 r
->vertex_bufs
.individual
.ycbcr
.stride
= sizeof(struct vertex2f
) * 4;
869 r
->vertex_bufs
.individual
.ycbcr
.max_index
= 24 * r
->macroblocks_per_batch
- 1;
870 r
->vertex_bufs
.individual
.ycbcr
.buffer_offset
= 0;
871 r
->vertex_bufs
.individual
.ycbcr
.buffer
= pipe_buffer_create
874 DEFAULT_BUF_ALIGNMENT
,
875 PIPE_BUFFER_USAGE_VERTEX
| PIPE_BUFFER_USAGE_DISCARD
,
876 sizeof(struct vertex2f
) * 4 * 24 * r
->macroblocks_per_batch
879 for (i
= 1; i
< 3; ++i
) {
880 r
->vertex_bufs
.all
[i
].stride
= sizeof(struct vertex2f
) * 2;
881 r
->vertex_bufs
.all
[i
].max_index
= 24 * r
->macroblocks_per_batch
- 1;
882 r
->vertex_bufs
.all
[i
].buffer_offset
= 0;
883 r
->vertex_bufs
.all
[i
].buffer
= pipe_buffer_create
886 DEFAULT_BUF_ALIGNMENT
,
887 PIPE_BUFFER_USAGE_VERTEX
| PIPE_BUFFER_USAGE_DISCARD
,
888 sizeof(struct vertex2f
) * 2 * 24 * r
->macroblocks_per_batch
892 /* Position element */
893 r
->vertex_elems
[0].src_offset
= 0;
894 r
->vertex_elems
[0].vertex_buffer_index
= 0;
895 r
->vertex_elems
[0].nr_components
= 2;
896 r
->vertex_elems
[0].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
898 /* Luma, texcoord element */
899 r
->vertex_elems
[1].src_offset
= sizeof(struct vertex2f
);
900 r
->vertex_elems
[1].vertex_buffer_index
= 0;
901 r
->vertex_elems
[1].nr_components
= 2;
902 r
->vertex_elems
[1].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
904 /* Chroma Cr texcoord element */
905 r
->vertex_elems
[2].src_offset
= sizeof(struct vertex2f
) * 2;
906 r
->vertex_elems
[2].vertex_buffer_index
= 0;
907 r
->vertex_elems
[2].nr_components
= 2;
908 r
->vertex_elems
[2].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
910 /* Chroma Cb texcoord element */
911 r
->vertex_elems
[3].src_offset
= sizeof(struct vertex2f
) * 3;
912 r
->vertex_elems
[3].vertex_buffer_index
= 0;
913 r
->vertex_elems
[3].nr_components
= 2;
914 r
->vertex_elems
[3].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
916 /* First ref surface top field texcoord element */
917 r
->vertex_elems
[4].src_offset
= 0;
918 r
->vertex_elems
[4].vertex_buffer_index
= 1;
919 r
->vertex_elems
[4].nr_components
= 2;
920 r
->vertex_elems
[4].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
922 /* First ref surface bottom field texcoord element */
923 r
->vertex_elems
[5].src_offset
= sizeof(struct vertex2f
);
924 r
->vertex_elems
[5].vertex_buffer_index
= 1;
925 r
->vertex_elems
[5].nr_components
= 2;
926 r
->vertex_elems
[5].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
928 /* Second ref surface top field texcoord element */
929 r
->vertex_elems
[6].src_offset
= 0;
930 r
->vertex_elems
[6].vertex_buffer_index
= 2;
931 r
->vertex_elems
[6].nr_components
= 2;
932 r
->vertex_elems
[6].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
934 /* Second ref surface bottom field texcoord element */
935 r
->vertex_elems
[7].src_offset
= sizeof(struct vertex2f
);
936 r
->vertex_elems
[7].vertex_buffer_index
= 2;
937 r
->vertex_elems
[7].nr_components
= 2;
938 r
->vertex_elems
[7].src_format
= PIPE_FORMAT_R32G32_FLOAT
;
940 r
->vs_const_buf
.buffer
= pipe_buffer_create
943 DEFAULT_BUF_ALIGNMENT
,
944 PIPE_BUFFER_USAGE_CONSTANT
| PIPE_BUFFER_USAGE_DISCARD
,
945 sizeof(struct vertex_shader_consts
)
948 r
->fs_const_buf
.buffer
= pipe_buffer_create
951 DEFAULT_BUF_ALIGNMENT
,
952 PIPE_BUFFER_USAGE_CONSTANT
, sizeof(struct fragment_shader_consts
)
957 pipe_buffer_map(r
->pipe
->screen
, r
->fs_const_buf
.buffer
, PIPE_BUFFER_USAGE_CPU_WRITE
),
958 &fs_consts
, sizeof(struct fragment_shader_consts
)
961 pipe_buffer_unmap(r
->pipe
->screen
, r
->fs_const_buf
.buffer
);
967 cleanup_buffers(struct vl_mpeg12_mc_renderer
*r
)
973 pipe_buffer_reference(&r
->vs_const_buf
.buffer
, NULL
);
974 pipe_buffer_reference(&r
->fs_const_buf
.buffer
, NULL
);
976 for (i
= 0; i
< 3; ++i
)
977 pipe_buffer_reference(&r
->vertex_bufs
.all
[i
].buffer
, NULL
);
979 for (i
= 0; i
< 3; ++i
)
980 pipe_texture_reference(&r
->textures
.all
[i
], NULL
);
982 FREE(r
->macroblock_buf
);
985 static enum MACROBLOCK_TYPE
986 get_macroblock_type(struct pipe_mpeg12_macroblock
*mb
)
990 switch (mb
->mb_type
) {
991 case PIPE_MPEG12_MACROBLOCK_TYPE_INTRA
:
992 return MACROBLOCK_TYPE_INTRA
;
993 case PIPE_MPEG12_MACROBLOCK_TYPE_FWD
:
994 return mb
->mo_type
== PIPE_MPEG12_MOTION_TYPE_FRAME
?
995 MACROBLOCK_TYPE_FWD_FRAME_PRED
: MACROBLOCK_TYPE_FWD_FIELD_PRED
;
996 case PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
:
997 return mb
->mo_type
== PIPE_MPEG12_MOTION_TYPE_FRAME
?
998 MACROBLOCK_TYPE_BKWD_FRAME_PRED
: MACROBLOCK_TYPE_BKWD_FIELD_PRED
;
999 case PIPE_MPEG12_MACROBLOCK_TYPE_BI
:
1000 return mb
->mo_type
== PIPE_MPEG12_MOTION_TYPE_FRAME
?
1001 MACROBLOCK_TYPE_BI_FRAME_PRED
: MACROBLOCK_TYPE_BI_FIELD_PRED
;
1010 /* XXX: One of these days this will have to be killed with fire */
1011 #define SET_BLOCK(vb, cbp, mbx, mby, unitx, unity, ofsx, ofsy, hx, hy, lm, cbm, crm, use_zb, zb) \
1013 (vb)[0].pos.x = (mbx) * (unitx) + (ofsx); (vb)[0].pos.y = (mby) * (unity) + (ofsy); \
1014 (vb)[1].pos.x = (mbx) * (unitx) + (ofsx); (vb)[1].pos.y = (mby) * (unity) + (ofsy) + (hy); \
1015 (vb)[2].pos.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[2].pos.y = (mby) * (unity) + (ofsy); \
1016 (vb)[3].pos.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[3].pos.y = (mby) * (unity) + (ofsy); \
1017 (vb)[4].pos.x = (mbx) * (unitx) + (ofsx); (vb)[4].pos.y = (mby) * (unity) + (ofsy) + (hy); \
1018 (vb)[5].pos.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[5].pos.y = (mby) * (unity) + (ofsy) + (hy); \
1020 if (!use_zb || (cbp) & (lm)) \
1022 (vb)[0].luma_tc.x = (mbx) * (unitx) + (ofsx); (vb)[0].luma_tc.y = (mby) * (unity) + (ofsy); \
1023 (vb)[1].luma_tc.x = (mbx) * (unitx) + (ofsx); (vb)[1].luma_tc.y = (mby) * (unity) + (ofsy) + (hy); \
1024 (vb)[2].luma_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[2].luma_tc.y = (mby) * (unity) + (ofsy); \
1025 (vb)[3].luma_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[3].luma_tc.y = (mby) * (unity) + (ofsy); \
1026 (vb)[4].luma_tc.x = (mbx) * (unitx) + (ofsx); (vb)[4].luma_tc.y = (mby) * (unity) + (ofsy) + (hy); \
1027 (vb)[5].luma_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[5].luma_tc.y = (mby) * (unity) + (ofsy) + (hy); \
1031 (vb)[0].luma_tc.x = (zb)[0].x; (vb)[0].luma_tc.y = (zb)[0].y; \
1032 (vb)[1].luma_tc.x = (zb)[0].x; (vb)[1].luma_tc.y = (zb)[0].y + (hy); \
1033 (vb)[2].luma_tc.x = (zb)[0].x + (hx); (vb)[2].luma_tc.y = (zb)[0].y; \
1034 (vb)[3].luma_tc.x = (zb)[0].x + (hx); (vb)[3].luma_tc.y = (zb)[0].y; \
1035 (vb)[4].luma_tc.x = (zb)[0].x; (vb)[4].luma_tc.y = (zb)[0].y + (hy); \
1036 (vb)[5].luma_tc.x = (zb)[0].x + (hx); (vb)[5].luma_tc.y = (zb)[0].y + (hy); \
1039 if (!use_zb || (cbp) & (cbm)) \
1041 (vb)[0].cb_tc.x = (mbx) * (unitx) + (ofsx); (vb)[0].cb_tc.y = (mby) * (unity) + (ofsy); \
1042 (vb)[1].cb_tc.x = (mbx) * (unitx) + (ofsx); (vb)[1].cb_tc.y = (mby) * (unity) + (ofsy) + (hy); \
1043 (vb)[2].cb_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[2].cb_tc.y = (mby) * (unity) + (ofsy); \
1044 (vb)[3].cb_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[3].cb_tc.y = (mby) * (unity) + (ofsy); \
1045 (vb)[4].cb_tc.x = (mbx) * (unitx) + (ofsx); (vb)[4].cb_tc.y = (mby) * (unity) + (ofsy) + (hy); \
1046 (vb)[5].cb_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[5].cb_tc.y = (mby) * (unity) + (ofsy) + (hy); \
1050 (vb)[0].cb_tc.x = (zb)[1].x; (vb)[0].cb_tc.y = (zb)[1].y; \
1051 (vb)[1].cb_tc.x = (zb)[1].x; (vb)[1].cb_tc.y = (zb)[1].y + (hy); \
1052 (vb)[2].cb_tc.x = (zb)[1].x + (hx); (vb)[2].cb_tc.y = (zb)[1].y; \
1053 (vb)[3].cb_tc.x = (zb)[1].x + (hx); (vb)[3].cb_tc.y = (zb)[1].y; \
1054 (vb)[4].cb_tc.x = (zb)[1].x; (vb)[4].cb_tc.y = (zb)[1].y + (hy); \
1055 (vb)[5].cb_tc.x = (zb)[1].x + (hx); (vb)[5].cb_tc.y = (zb)[1].y + (hy); \
1058 if (!use_zb || (cbp) & (crm)) \
1060 (vb)[0].cr_tc.x = (mbx) * (unitx) + (ofsx); (vb)[0].cr_tc.y = (mby) * (unity) + (ofsy); \
1061 (vb)[1].cr_tc.x = (mbx) * (unitx) + (ofsx); (vb)[1].cr_tc.y = (mby) * (unity) + (ofsy) + (hy); \
1062 (vb)[2].cr_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[2].cr_tc.y = (mby) * (unity) + (ofsy); \
1063 (vb)[3].cr_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[3].cr_tc.y = (mby) * (unity) + (ofsy); \
1064 (vb)[4].cr_tc.x = (mbx) * (unitx) + (ofsx); (vb)[4].cr_tc.y = (mby) * (unity) + (ofsy) + (hy); \
1065 (vb)[5].cr_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[5].cr_tc.y = (mby) * (unity) + (ofsy) + (hy); \
1069 (vb)[0].cr_tc.x = (zb)[2].x; (vb)[0].cr_tc.y = (zb)[2].y; \
1070 (vb)[1].cr_tc.x = (zb)[2].x; (vb)[1].cr_tc.y = (zb)[2].y + (hy); \
1071 (vb)[2].cr_tc.x = (zb)[2].x + (hx); (vb)[2].cr_tc.y = (zb)[2].y; \
1072 (vb)[3].cr_tc.x = (zb)[2].x + (hx); (vb)[3].cr_tc.y = (zb)[2].y; \
1073 (vb)[4].cr_tc.x = (zb)[2].x; (vb)[4].cr_tc.y = (zb)[2].y + (hy); \
1074 (vb)[5].cr_tc.x = (zb)[2].x + (hx); (vb)[5].cr_tc.y = (zb)[2].y + (hy); \
1079 gen_macroblock_verts(struct vl_mpeg12_mc_renderer
*r
,
1080 struct pipe_mpeg12_macroblock
*mb
, unsigned pos
,
1081 struct vert_stream_0
*ycbcr_vb
, struct vertex2f
**ref_vb
)
1083 struct vertex2f mo_vec
[2];
1090 assert(pos
< r
->macroblocks_per_batch
);
1095 switch (mb
->mb_type
) {
1096 case PIPE_MPEG12_MACROBLOCK_TYPE_BI
:
1098 struct vertex2f
*vb
;
1100 assert(ref_vb
&& ref_vb
[1]);
1102 vb
= ref_vb
[1] + pos
* 2 * 24;
1104 mo_vec
[0].x
= mb
->pmv
[0][1][0] * 0.5f
* r
->surface_tex_inv_size
.x
;
1105 mo_vec
[0].y
= mb
->pmv
[0][1][1] * 0.5f
* r
->surface_tex_inv_size
.y
;
1107 if (mb
->mo_type
== PIPE_MPEG12_MOTION_TYPE_FRAME
) {
1108 for (i
= 0; i
< 24 * 2; i
+= 2) {
1109 vb
[i
].x
= mo_vec
[0].x
;
1110 vb
[i
].y
= mo_vec
[0].y
;
1114 mo_vec
[1].x
= mb
->pmv
[1][1][0] * 0.5f
* r
->surface_tex_inv_size
.x
;
1115 mo_vec
[1].y
= mb
->pmv
[1][1][1] * 0.5f
* r
->surface_tex_inv_size
.y
;
1117 for (i
= 0; i
< 24 * 2; i
+= 2) {
1118 vb
[i
].x
= mo_vec
[0].x
;
1119 vb
[i
].y
= mo_vec
[0].y
;
1120 vb
[i
+ 1].x
= mo_vec
[1].x
;
1121 vb
[i
+ 1].y
= mo_vec
[1].y
;
1127 case PIPE_MPEG12_MACROBLOCK_TYPE_FWD
:
1128 case PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
:
1130 struct vertex2f
*vb
;
1132 assert(ref_vb
&& ref_vb
[0]);
1134 vb
= ref_vb
[0] + pos
* 2 * 24;
1136 if (mb
->mb_type
== PIPE_MPEG12_MACROBLOCK_TYPE_BKWD
) {
1137 mo_vec
[0].x
= mb
->pmv
[0][1][0] * 0.5f
* r
->surface_tex_inv_size
.x
;
1138 mo_vec
[0].y
= mb
->pmv
[0][1][1] * 0.5f
* r
->surface_tex_inv_size
.y
;
1140 if (mb
->mo_type
== PIPE_MPEG12_MOTION_TYPE_FIELD
) {
1141 mo_vec
[1].x
= mb
->pmv
[1][1][0] * 0.5f
* r
->surface_tex_inv_size
.x
;
1142 mo_vec
[1].y
= mb
->pmv
[1][1][1] * 0.5f
* r
->surface_tex_inv_size
.y
;
1146 mo_vec
[0].x
= mb
->pmv
[0][0][0] * 0.5f
* r
->surface_tex_inv_size
.x
;
1147 mo_vec
[0].y
= mb
->pmv
[0][0][1] * 0.5f
* r
->surface_tex_inv_size
.y
;
1149 if (mb
->mo_type
== PIPE_MPEG12_MOTION_TYPE_FIELD
) {
1150 mo_vec
[1].x
= mb
->pmv
[1][0][0] * 0.5f
* r
->surface_tex_inv_size
.x
;
1151 mo_vec
[1].y
= mb
->pmv
[1][0][1] * 0.5f
* r
->surface_tex_inv_size
.y
;
1155 if (mb
->mb_type
== PIPE_MPEG12_MOTION_TYPE_FRAME
) {
1156 for (i
= 0; i
< 24 * 2; i
+= 2) {
1157 vb
[i
].x
= mo_vec
[0].x
;
1158 vb
[i
].y
= mo_vec
[0].y
;
1162 for (i
= 0; i
< 24 * 2; i
+= 2) {
1163 vb
[i
].x
= mo_vec
[0].x
;
1164 vb
[i
].y
= mo_vec
[0].y
;
1165 vb
[i
+ 1].x
= mo_vec
[1].x
;
1166 vb
[i
+ 1].y
= mo_vec
[1].y
;
1172 case PIPE_MPEG12_MACROBLOCK_TYPE_INTRA
:
1174 const struct vertex2f unit
=
1176 r
->surface_tex_inv_size
.x
* MACROBLOCK_WIDTH
,
1177 r
->surface_tex_inv_size
.y
* MACROBLOCK_HEIGHT
1179 const struct vertex2f half
=
1181 r
->surface_tex_inv_size
.x
* (MACROBLOCK_WIDTH
/ 2),
1182 r
->surface_tex_inv_size
.y
* (MACROBLOCK_HEIGHT
/ 2)
1184 const bool use_zb
= r
->eb_handling
== VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE
;
1186 struct vert_stream_0
*vb
= ycbcr_vb
+ pos
* 24;
1188 SET_BLOCK(vb
, mb
->cbp
, mb
->mbx
, mb
->mby
,
1189 unit
.x
, unit
.y
, 0, 0, half
.x
, half
.y
,
1190 32, 2, 1, use_zb
, r
->zero_block
);
1192 SET_BLOCK(vb
+ 6, mb
->cbp
, mb
->mbx
, mb
->mby
,
1193 unit
.x
, unit
.y
, half
.x
, 0, half
.x
, half
.y
,
1194 16, 2, 1, use_zb
, r
->zero_block
);
1196 SET_BLOCK(vb
+ 12, mb
->cbp
, mb
->mbx
, mb
->mby
,
1197 unit
.x
, unit
.y
, 0, half
.y
, half
.x
, half
.y
,
1198 8, 2, 1, use_zb
, r
->zero_block
);
1200 SET_BLOCK(vb
+ 18, mb
->cbp
, mb
->mbx
, mb
->mby
,
1201 unit
.x
, unit
.y
, half
.x
, half
.y
, half
.x
, half
.y
,
1202 4, 2, 1, use_zb
, r
->zero_block
);
1212 gen_macroblock_stream(struct vl_mpeg12_mc_renderer
*r
,
1213 unsigned *num_macroblocks
)
1215 unsigned offset
[NUM_MACROBLOCK_TYPES
];
1216 struct vert_stream_0
*ycbcr_vb
;
1217 struct vertex2f
*ref_vb
[2];
1221 assert(num_macroblocks
);
1223 for (i
= 0; i
< r
->num_macroblocks
; ++i
) {
1224 enum MACROBLOCK_TYPE mb_type
= get_macroblock_type(&r
->macroblock_buf
[i
]);
1225 ++num_macroblocks
[mb_type
];
1230 for (i
= 1; i
< NUM_MACROBLOCK_TYPES
; ++i
)
1231 offset
[i
] = offset
[i
- 1] + num_macroblocks
[i
- 1];
1233 ycbcr_vb
= (struct vert_stream_0
*)pipe_buffer_map
1236 r
->vertex_bufs
.individual
.ycbcr
.buffer
,
1237 PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_DISCARD
1240 for (i
= 0; i
< 2; ++i
)
1241 ref_vb
[i
] = (struct vertex2f
*)pipe_buffer_map
1244 r
->vertex_bufs
.individual
.ref
[i
].buffer
,
1245 PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_DISCARD
1248 for (i
= 0; i
< r
->num_macroblocks
; ++i
) {
1249 enum MACROBLOCK_TYPE mb_type
= get_macroblock_type(&r
->macroblock_buf
[i
]);
1251 gen_macroblock_verts(r
, &r
->macroblock_buf
[i
], offset
[mb_type
],
1257 pipe_buffer_unmap(r
->pipe
->screen
, r
->vertex_bufs
.individual
.ycbcr
.buffer
);
1258 for (i
= 0; i
< 2; ++i
)
1259 pipe_buffer_unmap(r
->pipe
->screen
, r
->vertex_bufs
.individual
.ref
[i
].buffer
);
1263 flush(struct vl_mpeg12_mc_renderer
*r
)
1265 unsigned num_macroblocks
[NUM_MACROBLOCK_TYPES
] = { 0 };
1266 unsigned vb_start
= 0;
1267 struct vertex_shader_consts
*vs_consts
;
1271 assert(r
->num_macroblocks
== r
->macroblocks_per_batch
);
1273 gen_macroblock_stream(r
, num_macroblocks
);
1275 r
->fb_state
.cbufs
[0] = r
->pipe
->screen
->get_tex_surface
1277 r
->pipe
->screen
, r
->surface
,
1278 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE
1281 r
->pipe
->set_framebuffer_state(r
->pipe
, &r
->fb_state
);
1282 r
->pipe
->set_viewport_state(r
->pipe
, &r
->viewport
);
1283 r
->pipe
->set_scissor_state(r
->pipe
, &r
->scissor
);
1285 vs_consts
= pipe_buffer_map
1287 r
->pipe
->screen
, r
->vs_const_buf
.buffer
,
1288 PIPE_BUFFER_USAGE_CPU_WRITE
| PIPE_BUFFER_USAGE_DISCARD
1291 vs_consts
->denorm
.x
= r
->surface
->width0
;
1292 vs_consts
->denorm
.y
= r
->surface
->height0
;
1294 pipe_buffer_unmap(r
->pipe
->screen
, r
->vs_const_buf
.buffer
);
1296 r
->pipe
->set_constant_buffer(r
->pipe
, PIPE_SHADER_VERTEX
, 0,
1298 r
->pipe
->set_constant_buffer(r
->pipe
, PIPE_SHADER_FRAGMENT
, 0,
1301 if (num_macroblocks
[MACROBLOCK_TYPE_INTRA
] > 0) {
1302 r
->pipe
->set_vertex_buffers(r
->pipe
, 1, r
->vertex_bufs
.all
);
1303 r
->pipe
->set_vertex_elements(r
->pipe
, 4, r
->vertex_elems
);
1304 r
->pipe
->set_fragment_sampler_textures(r
->pipe
, 3, r
->textures
.all
);
1305 r
->pipe
->bind_fragment_sampler_states(r
->pipe
, 3, r
->samplers
.all
);
1306 r
->pipe
->bind_vs_state(r
->pipe
, r
->i_vs
);
1307 r
->pipe
->bind_fs_state(r
->pipe
, r
->i_fs
);
1309 r
->pipe
->draw_arrays(r
->pipe
, PIPE_PRIM_TRIANGLES
, vb_start
,
1310 num_macroblocks
[MACROBLOCK_TYPE_INTRA
] * 24);
1311 vb_start
+= num_macroblocks
[MACROBLOCK_TYPE_INTRA
] * 24;
1314 if (num_macroblocks
[MACROBLOCK_TYPE_FWD_FRAME_PRED
] > 0) {
1315 r
->pipe
->set_vertex_buffers(r
->pipe
, 2, r
->vertex_bufs
.all
);
1316 r
->pipe
->set_vertex_elements(r
->pipe
, 6, r
->vertex_elems
);
1317 r
->textures
.individual
.ref
[0] = r
->past
;
1318 r
->pipe
->set_fragment_sampler_textures(r
->pipe
, 4, r
->textures
.all
);
1319 r
->pipe
->bind_fragment_sampler_states(r
->pipe
, 4, r
->samplers
.all
);
1320 r
->pipe
->bind_vs_state(r
->pipe
, r
->p_vs
[0]);
1321 r
->pipe
->bind_fs_state(r
->pipe
, r
->p_fs
[0]);
1323 r
->pipe
->draw_arrays(r
->pipe
, PIPE_PRIM_TRIANGLES
, vb_start
,
1324 num_macroblocks
[MACROBLOCK_TYPE_FWD_FRAME_PRED
] * 24);
1325 vb_start
+= num_macroblocks
[MACROBLOCK_TYPE_FWD_FRAME_PRED
] * 24;
1328 if (false /*num_macroblocks[MACROBLOCK_TYPE_FWD_FIELD_PRED] > 0 */ ) {
1329 r
->pipe
->set_vertex_buffers(r
->pipe
, 2, r
->vertex_bufs
.all
);
1330 r
->pipe
->set_vertex_elements(r
->pipe
, 6, r
->vertex_elems
);
1331 r
->textures
.individual
.ref
[0] = r
->past
;
1332 r
->pipe
->set_fragment_sampler_textures(r
->pipe
, 4, r
->textures
.all
);
1333 r
->pipe
->bind_fragment_sampler_states(r
->pipe
, 4, r
->samplers
.all
);
1334 r
->pipe
->bind_vs_state(r
->pipe
, r
->p_vs
[1]);
1335 r
->pipe
->bind_fs_state(r
->pipe
, r
->p_fs
[1]);
1337 r
->pipe
->draw_arrays(r
->pipe
, PIPE_PRIM_TRIANGLES
, vb_start
,
1338 num_macroblocks
[MACROBLOCK_TYPE_FWD_FIELD_PRED
] * 24);
1339 vb_start
+= num_macroblocks
[MACROBLOCK_TYPE_FWD_FIELD_PRED
] * 24;
1342 if (num_macroblocks
[MACROBLOCK_TYPE_BKWD_FRAME_PRED
] > 0) {
1343 r
->pipe
->set_vertex_buffers(r
->pipe
, 2, r
->vertex_bufs
.all
);
1344 r
->pipe
->set_vertex_elements(r
->pipe
, 6, r
->vertex_elems
);
1345 r
->textures
.individual
.ref
[0] = r
->future
;
1346 r
->pipe
->set_fragment_sampler_textures(r
->pipe
, 4, r
->textures
.all
);
1347 r
->pipe
->bind_fragment_sampler_states(r
->pipe
, 4, r
->samplers
.all
);
1348 r
->pipe
->bind_vs_state(r
->pipe
, r
->p_vs
[0]);
1349 r
->pipe
->bind_fs_state(r
->pipe
, r
->p_fs
[0]);
1351 r
->pipe
->draw_arrays(r
->pipe
, PIPE_PRIM_TRIANGLES
, vb_start
,
1352 num_macroblocks
[MACROBLOCK_TYPE_BKWD_FRAME_PRED
] * 24);
1353 vb_start
+= num_macroblocks
[MACROBLOCK_TYPE_BKWD_FRAME_PRED
] * 24;
1356 if (false /*num_macroblocks[MACROBLOCK_TYPE_BKWD_FIELD_PRED] > 0 */ ) {
1357 r
->pipe
->set_vertex_buffers(r
->pipe
, 2, r
->vertex_bufs
.all
);
1358 r
->pipe
->set_vertex_elements(r
->pipe
, 6, r
->vertex_elems
);
1359 r
->textures
.individual
.ref
[0] = r
->future
;
1360 r
->pipe
->set_fragment_sampler_textures(r
->pipe
, 4, r
->textures
.all
);
1361 r
->pipe
->bind_fragment_sampler_states(r
->pipe
, 4, r
->samplers
.all
);
1362 r
->pipe
->bind_vs_state(r
->pipe
, r
->p_vs
[1]);
1363 r
->pipe
->bind_fs_state(r
->pipe
, r
->p_fs
[1]);
1365 r
->pipe
->draw_arrays(r
->pipe
, PIPE_PRIM_TRIANGLES
, vb_start
,
1366 num_macroblocks
[MACROBLOCK_TYPE_BKWD_FIELD_PRED
] * 24);
1367 vb_start
+= num_macroblocks
[MACROBLOCK_TYPE_BKWD_FIELD_PRED
] * 24;
1370 if (num_macroblocks
[MACROBLOCK_TYPE_BI_FRAME_PRED
] > 0) {
1371 r
->pipe
->set_vertex_buffers(r
->pipe
, 3, r
->vertex_bufs
.all
);
1372 r
->pipe
->set_vertex_elements(r
->pipe
, 8, r
->vertex_elems
);
1373 r
->textures
.individual
.ref
[0] = r
->past
;
1374 r
->textures
.individual
.ref
[1] = r
->future
;
1375 r
->pipe
->set_fragment_sampler_textures(r
->pipe
, 5, r
->textures
.all
);
1376 r
->pipe
->bind_fragment_sampler_states(r
->pipe
, 5, r
->samplers
.all
);
1377 r
->pipe
->bind_vs_state(r
->pipe
, r
->b_vs
[0]);
1378 r
->pipe
->bind_fs_state(r
->pipe
, r
->b_fs
[0]);
1380 r
->pipe
->draw_arrays(r
->pipe
, PIPE_PRIM_TRIANGLES
, vb_start
,
1381 num_macroblocks
[MACROBLOCK_TYPE_BI_FRAME_PRED
] * 24);
1382 vb_start
+= num_macroblocks
[MACROBLOCK_TYPE_BI_FRAME_PRED
] * 24;
1385 if (false /*num_macroblocks[MACROBLOCK_TYPE_BI_FIELD_PRED] > 0 */ ) {
1386 r
->pipe
->set_vertex_buffers(r
->pipe
, 3, r
->vertex_bufs
.all
);
1387 r
->pipe
->set_vertex_elements(r
->pipe
, 8, r
->vertex_elems
);
1388 r
->textures
.individual
.ref
[0] = r
->past
;
1389 r
->textures
.individual
.ref
[1] = r
->future
;
1390 r
->pipe
->set_fragment_sampler_textures(r
->pipe
, 5, r
->textures
.all
);
1391 r
->pipe
->bind_fragment_sampler_states(r
->pipe
, 5, r
->samplers
.all
);
1392 r
->pipe
->bind_vs_state(r
->pipe
, r
->b_vs
[1]);
1393 r
->pipe
->bind_fs_state(r
->pipe
, r
->b_fs
[1]);
1395 r
->pipe
->draw_arrays(r
->pipe
, PIPE_PRIM_TRIANGLES
, vb_start
,
1396 num_macroblocks
[MACROBLOCK_TYPE_BI_FIELD_PRED
] * 24);
1397 vb_start
+= num_macroblocks
[MACROBLOCK_TYPE_BI_FIELD_PRED
] * 24;
1400 r
->pipe
->flush(r
->pipe
, PIPE_FLUSH_RENDER_CACHE
, r
->fence
);
1401 pipe_surface_reference(&r
->fb_state
.cbufs
[0], NULL
);
1403 if (r
->eb_handling
== VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE
)
1404 for (i
= 0; i
< 3; ++i
)
1405 r
->zero_block
[i
].x
= ZERO_BLOCK_NIL
;
1407 r
->num_macroblocks
= 0;
1411 grab_frame_coded_block(short *src
, short *dst
, unsigned dst_pitch
)
1418 for (y
= 0; y
< BLOCK_HEIGHT
; ++y
)
1419 memcpy(dst
+ y
* dst_pitch
, src
+ y
* BLOCK_WIDTH
, BLOCK_WIDTH
* 2);
1423 grab_field_coded_block(short *src
, short *dst
, unsigned dst_pitch
)
1430 for (y
= 0; y
< BLOCK_HEIGHT
; ++y
)
1431 memcpy(dst
+ y
* dst_pitch
* 2, src
+ y
* BLOCK_WIDTH
, BLOCK_WIDTH
* 2);
1435 fill_zero_block(short *dst
, unsigned dst_pitch
)
1441 for (y
= 0; y
< BLOCK_HEIGHT
; ++y
)
1442 memset(dst
+ y
* dst_pitch
, 0, BLOCK_WIDTH
* 2);
1446 grab_blocks(struct vl_mpeg12_mc_renderer
*r
, unsigned mbx
, unsigned mby
,
1447 enum pipe_mpeg12_dct_type dct_type
, unsigned cbp
, short *blocks
)
1451 unsigned tb
= 0, sb
= 0;
1452 unsigned mbpx
= mbx
* MACROBLOCK_WIDTH
, mbpy
= mby
* MACROBLOCK_HEIGHT
;
1458 tex_pitch
= r
->tex_transfer
[0]->stride
/ util_format_get_blocksize(r
->tex_transfer
[0]->texture
->format
);
1459 texels
= r
->texels
[0] + mbpy
* tex_pitch
+ mbpx
;
1461 for (y
= 0; y
< 2; ++y
) {
1462 for (x
= 0; x
< 2; ++x
, ++tb
) {
1463 if ((cbp
>> (5 - tb
)) & 1) {
1464 if (dct_type
== PIPE_MPEG12_DCT_TYPE_FRAME
) {
1465 grab_frame_coded_block(blocks
+ sb
* BLOCK_WIDTH
* BLOCK_HEIGHT
,
1466 texels
+ y
* tex_pitch
* BLOCK_WIDTH
+
1467 x
* BLOCK_WIDTH
, tex_pitch
);
1470 grab_field_coded_block(blocks
+ sb
* BLOCK_WIDTH
* BLOCK_HEIGHT
,
1471 texels
+ y
* tex_pitch
+ x
* BLOCK_WIDTH
,
1477 else if (r
->eb_handling
!= VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_NONE
) {
1478 if (r
->eb_handling
== VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ALL
||
1479 ZERO_BLOCK_IS_NIL(r
->zero_block
[0])) {
1480 fill_zero_block(texels
+ y
* tex_pitch
* BLOCK_WIDTH
+ x
* BLOCK_WIDTH
, tex_pitch
);
1481 if (r
->eb_handling
== VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE
) {
1482 r
->zero_block
[0].x
= (mbpx
+ x
* 8) * r
->surface_tex_inv_size
.x
;
1483 r
->zero_block
[0].y
= (mbpy
+ y
* 8) * r
->surface_tex_inv_size
.y
;
1490 /* TODO: Implement 422, 444 */
1491 assert(r
->chroma_format
== PIPE_VIDEO_CHROMA_FORMAT_420
);
1496 for (tb
= 0; tb
< 2; ++tb
) {
1497 tex_pitch
= r
->tex_transfer
[tb
+ 1]->stride
/ util_format_get_blocksize(r
->tex_transfer
[tb
+ 1]->texture
->format
);
1498 texels
= r
->texels
[tb
+ 1] + mbpy
* tex_pitch
+ mbpx
;
1500 if ((cbp
>> (1 - tb
)) & 1) {
1501 grab_frame_coded_block(blocks
+ sb
* BLOCK_WIDTH
* BLOCK_HEIGHT
, texels
, tex_pitch
);
1504 else if (r
->eb_handling
!= VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_NONE
) {
1505 if (r
->eb_handling
== VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ALL
||
1506 ZERO_BLOCK_IS_NIL(r
->zero_block
[tb
+ 1])) {
1507 fill_zero_block(texels
, tex_pitch
);
1508 if (r
->eb_handling
== VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE
) {
1509 r
->zero_block
[tb
+ 1].x
= (mbpx
<< 1) * r
->surface_tex_inv_size
.x
;
1510 r
->zero_block
[tb
+ 1].y
= (mbpy
<< 1) * r
->surface_tex_inv_size
.y
;
1518 grab_macroblock(struct vl_mpeg12_mc_renderer
*r
,
1519 struct pipe_mpeg12_macroblock
*mb
)
1523 assert(r
->num_macroblocks
< r
->macroblocks_per_batch
);
1525 memcpy(&r
->macroblock_buf
[r
->num_macroblocks
], mb
,
1526 sizeof(struct pipe_mpeg12_macroblock
));
1528 grab_blocks(r
, mb
->mbx
, mb
->mby
, mb
->dct_type
, mb
->cbp
, mb
->blocks
);
1530 ++r
->num_macroblocks
;
1534 vl_mpeg12_mc_renderer_init(struct vl_mpeg12_mc_renderer
*renderer
,
1535 struct pipe_context
*pipe
,
1536 unsigned picture_width
,
1537 unsigned picture_height
,
1538 enum pipe_video_chroma_format chroma_format
,
1539 enum VL_MPEG12_MC_RENDERER_BUFFER_MODE bufmode
,
1540 enum VL_MPEG12_MC_RENDERER_EMPTY_BLOCK eb_handling
,
1547 /* TODO: Implement other policies */
1548 assert(bufmode
== VL_MPEG12_MC_RENDERER_BUFFER_PICTURE
);
1549 /* TODO: Implement this */
1550 /* XXX: XFER_ALL sampling issue at block edges when using bilinear filtering */
1551 assert(eb_handling
!= VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_NONE
);
1552 /* TODO: Non-pot buffers untested, probably doesn't work without changes to texcoord generation, vert shader, etc */
1553 assert(pot_buffers
);
1555 memset(renderer
, 0, sizeof(struct vl_mpeg12_mc_renderer
));
1557 renderer
->pipe
= pipe
;
1558 renderer
->picture_width
= picture_width
;
1559 renderer
->picture_height
= picture_height
;
1560 renderer
->chroma_format
= chroma_format
;
1561 renderer
->bufmode
= bufmode
;
1562 renderer
->eb_handling
= eb_handling
;
1563 renderer
->pot_buffers
= pot_buffers
;
1565 if (!init_pipe_state(renderer
))
1567 if (!init_shaders(renderer
)) {
1568 cleanup_pipe_state(renderer
);
1571 if (!init_buffers(renderer
)) {
1572 cleanup_shaders(renderer
);
1573 cleanup_pipe_state(renderer
);
1577 renderer
->surface
= NULL
;
1578 renderer
->past
= NULL
;
1579 renderer
->future
= NULL
;
1580 for (i
= 0; i
< 3; ++i
)
1581 renderer
->zero_block
[i
].x
= ZERO_BLOCK_NIL
;
1582 renderer
->num_macroblocks
= 0;
1584 xfer_buffers_map(renderer
);
1590 vl_mpeg12_mc_renderer_cleanup(struct vl_mpeg12_mc_renderer
*renderer
)
1594 xfer_buffers_unmap(renderer
);
1596 cleanup_pipe_state(renderer
);
1597 cleanup_shaders(renderer
);
1598 cleanup_buffers(renderer
);
1602 vl_mpeg12_mc_renderer_render_macroblocks(struct vl_mpeg12_mc_renderer
1604 struct pipe_texture
*surface
,
1605 struct pipe_texture
*past
,
1606 struct pipe_texture
*future
,
1607 unsigned num_macroblocks
,
1608 struct pipe_mpeg12_macroblock
1609 *mpeg12_macroblocks
,
1610 struct pipe_fence_handle
**fence
)
1612 bool new_surface
= false;
1616 assert(num_macroblocks
);
1617 assert(mpeg12_macroblocks
);
1619 if (renderer
->surface
) {
1620 if (surface
!= renderer
->surface
) {
1621 if (renderer
->num_macroblocks
> 0) {
1622 xfer_buffers_unmap(renderer
);
1629 /* If the surface we're rendering hasn't changed the ref frames shouldn't change. */
1630 assert(surface
!= renderer
->surface
|| renderer
->past
== past
);
1631 assert(surface
!= renderer
->surface
|| renderer
->future
== future
);
1637 renderer
->surface
= surface
;
1638 renderer
->past
= past
;
1639 renderer
->future
= future
;
1640 renderer
->fence
= fence
;
1641 renderer
->surface_tex_inv_size
.x
= 1.0f
/ surface
->width0
;
1642 renderer
->surface_tex_inv_size
.y
= 1.0f
/ surface
->height0
;
1645 while (num_macroblocks
) {
1646 unsigned left_in_batch
= renderer
->macroblocks_per_batch
- renderer
->num_macroblocks
;
1647 unsigned num_to_submit
= MIN2(num_macroblocks
, left_in_batch
);
1650 for (i
= 0; i
< num_to_submit
; ++i
) {
1651 assert(mpeg12_macroblocks
[i
].base
.codec
== PIPE_VIDEO_CODEC_MPEG12
);
1652 grab_macroblock(renderer
, &mpeg12_macroblocks
[i
]);
1655 num_macroblocks
-= num_to_submit
;
1657 if (renderer
->num_macroblocks
== renderer
->macroblocks_per_batch
) {
1658 xfer_buffers_unmap(renderer
);
1660 xfer_buffers_map(renderer
);
1661 /* Next time we get this surface it may have new ref frames */
1662 renderer
->surface
= NULL
;