Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / auxiliary / vl / vl_mpeg12_mc_renderer.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Younes Manton.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "vl_mpeg12_mc_renderer.h"
29 #include <assert.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"
38
39 #define DEFAULT_BUF_ALIGNMENT 1
40 #define MACROBLOCK_WIDTH 16
41 #define MACROBLOCK_HEIGHT 16
42 #define BLOCK_WIDTH 8
43 #define BLOCK_HEIGHT 8
44 #define ZERO_BLOCK_NIL -1.0f
45 #define ZERO_BLOCK_IS_NIL(zb) ((zb).x < 0.0f)
46
47 struct vertex2f
48 {
49 float x, y;
50 };
51
52 struct vertex4f
53 {
54 float x, y, z, w;
55 };
56
57 struct vertex_shader_consts
58 {
59 struct vertex4f denorm;
60 };
61
62 struct fragment_shader_consts
63 {
64 struct vertex4f multiplier;
65 struct vertex4f div;
66 };
67
68 /*
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.
73 */
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}
77 };
78
79 struct vert_stream_0
80 {
81 struct vertex2f pos;
82 struct vertex2f luma_tc;
83 struct vertex2f cb_tc;
84 struct vertex2f cr_tc;
85 };
86
87 enum MACROBLOCK_TYPE
88 {
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,
96
97 NUM_MACROBLOCK_TYPES
98 };
99
100 static void
101 create_intra_vert_shader(struct vl_mpeg12_mc_renderer *r)
102 {
103 const unsigned max_tokens = 50;
104
105 struct pipe_shader_state vs;
106 struct tgsi_token *tokens;
107 struct tgsi_header *header;
108
109 struct tgsi_full_declaration decl;
110 struct tgsi_full_instruction inst;
111
112 unsigned ti;
113
114 unsigned i;
115
116 assert(r);
117
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);
122
123 ti = 2;
124
125 /*
126 * decl i0 ; Vertex pos
127 * decl i1 ; Luma texcoords
128 * decl i2 ; Chroma Cb texcoords
129 * decl i3 ; Chroma Cr texcoords
130 */
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);
134 }
135
136 /*
137 * decl o0 ; Vertex pos
138 * decl o1 ; Luma texcoords
139 * decl o2 ; Chroma Cb texcoords
140 * decl o3 ; Chroma Cr texcoords
141 */
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);
145 }
146
147 /*
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
152 */
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);
156 }
157
158 /* end */
159 inst = vl_end();
160 ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti);
161
162 assert(ti <= max_tokens);
163
164 vs.tokens = tokens;
165 r->i_vs = r->pipe->create_vs_state(r->pipe, &vs);
166 free(tokens);
167 }
168
169 static void
170 create_intra_frag_shader(struct vl_mpeg12_mc_renderer *r)
171 {
172 const unsigned max_tokens = 100;
173
174 struct pipe_shader_state fs;
175 struct tgsi_token *tokens;
176 struct tgsi_header *header;
177
178 struct tgsi_full_declaration decl;
179 struct tgsi_full_instruction inst;
180
181 unsigned ti;
182
183 unsigned i;
184
185 assert(r);
186
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);
191
192 ti = 2;
193
194 /*
195 * decl i0 ; Luma texcoords
196 * decl i1 ; Chroma Cb texcoords
197 * decl i2 ; Chroma Cr texcoords
198 */
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);
202 }
203
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);
207
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);
211
212 /* decl t0, t1 */
213 decl = vl_decl_temps(0, 1);
214 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
215
216 /*
217 * decl s0 ; Sampler for luma texture
218 * decl s1 ; Sampler for chroma Cb texture
219 * decl s2 ; Sampler for chroma Cr texture
220 */
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);
224 }
225
226 /*
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
233 */
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);
237
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);
244 }
245
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);
249
250 /* end */
251 inst = vl_end();
252 ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti);
253
254 assert(ti <= max_tokens);
255
256 fs.tokens = tokens;
257 r->i_fs = r->pipe->create_fs_state(r->pipe, &fs);
258 free(tokens);
259 }
260
261 static void
262 create_frame_pred_vert_shader(struct vl_mpeg12_mc_renderer *r)
263 {
264 const unsigned max_tokens = 100;
265
266 struct pipe_shader_state vs;
267 struct tgsi_token *tokens;
268 struct tgsi_header *header;
269
270 struct tgsi_full_declaration decl;
271 struct tgsi_full_instruction inst;
272
273 unsigned ti;
274
275 unsigned i;
276
277 assert(r);
278
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);
283
284 ti = 2;
285
286 /*
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)
293 */
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);
297 }
298
299 /*
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
305 */
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);
309 }
310
311 /*
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
316 */
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);
320 }
321
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);
325
326 /* end */
327 inst = vl_end();
328 ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti);
329
330 assert(ti <= max_tokens);
331
332 vs.tokens = tokens;
333 r->p_vs[0] = r->pipe->create_vs_state(r->pipe, &vs);
334 free(tokens);
335 }
336
337 #if 0
338 static void
339 create_field_pred_vert_shader(struct vl_mpeg12_mc_renderer *r)
340 {
341 assert(false);
342 }
343 #endif
344
345 static void
346 create_frame_pred_frag_shader(struct vl_mpeg12_mc_renderer *r)
347 {
348 const unsigned max_tokens = 100;
349
350 struct pipe_shader_state fs;
351 struct tgsi_token *tokens;
352 struct tgsi_header *header;
353
354 struct tgsi_full_declaration decl;
355 struct tgsi_full_instruction inst;
356
357 unsigned ti;
358
359 unsigned i;
360
361 assert(r);
362
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);
367
368 ti = 2;
369
370 /*
371 * decl i0 ; Luma texcoords
372 * decl i1 ; Chroma Cb texcoords
373 * decl i2 ; Chroma Cr texcoords
374 * decl i3 ; Ref macroblock texcoords
375 */
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);
379 }
380
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);
384
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);
388
389 /* decl t0, t1 */
390 decl = vl_decl_temps(0, 1);
391 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
392
393 /*
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
398 */
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);
402 }
403
404 /*
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
411 */
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);
415
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);
422 }
423
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);
427
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);
431
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);
435
436 /* end */
437 inst = vl_end();
438 ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti);
439
440 assert(ti <= max_tokens);
441
442 fs.tokens = tokens;
443 r->p_fs[0] = r->pipe->create_fs_state(r->pipe, &fs);
444 free(tokens);
445 }
446
447 #if 0
448 static void
449 create_field_pred_frag_shader(struct vl_mpeg12_mc_renderer *r)
450 {
451 assert(false);
452 }
453 #endif
454
455 static void
456 create_frame_bi_pred_vert_shader(struct vl_mpeg12_mc_renderer *r)
457 {
458 const unsigned max_tokens = 100;
459
460 struct pipe_shader_state vs;
461 struct tgsi_token *tokens;
462 struct tgsi_header *header;
463
464 struct tgsi_full_declaration decl;
465 struct tgsi_full_instruction inst;
466
467 unsigned ti;
468
469 unsigned i;
470
471 assert(r);
472
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);
477
478 ti = 2;
479
480 /*
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)
489 */
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);
493 }
494
495 /*
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
502 */
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);
506 }
507
508 /*
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
513 */
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);
517 }
518
519 /*
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
522 */
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);
526 }
527
528 /* end */
529 inst = vl_end();
530 ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti);
531
532 assert(ti <= max_tokens);
533
534 vs.tokens = tokens;
535 r->b_vs[0] = r->pipe->create_vs_state(r->pipe, &vs);
536 free(tokens);
537 }
538
539 #if 0
540 static void
541 create_field_bi_pred_vert_shader(struct vl_mpeg12_mc_renderer *r)
542 {
543 assert(false);
544 }
545 #endif
546
547 static void
548 create_frame_bi_pred_frag_shader(struct vl_mpeg12_mc_renderer *r)
549 {
550 const unsigned max_tokens = 100;
551
552 struct pipe_shader_state fs;
553 struct tgsi_token *tokens;
554 struct tgsi_header *header;
555
556 struct tgsi_full_declaration decl;
557 struct tgsi_full_instruction inst;
558
559 unsigned ti;
560
561 unsigned i;
562
563 assert(r);
564
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);
569
570 ti = 2;
571
572 /*
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
578 */
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);
582 }
583
584 /*
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
587 */
588 decl = vl_decl_constants(TGSI_SEMANTIC_GENERIC, 0, 0, 1);
589 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
590
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);
594
595 /* decl t0-t2 */
596 decl = vl_decl_temps(0, 2);
597 ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti);
598
599 /*
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
605 */
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);
609 }
610
611 /*
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
618 */
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);
622
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);
629 }
630
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);
634
635 /*
636 * tex2d t1, i3, s3 ; Read texel from first ref macroblock
637 * tex2d t2, i4, s4 ; Read texel from second ref macroblock
638 */
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);
642 }
643
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);
651
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);
655
656 /* end */
657 inst = vl_end();
658 ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti);
659
660 assert(ti <= max_tokens);
661
662 fs.tokens = tokens;
663 r->b_fs[0] = r->pipe->create_fs_state(r->pipe, &fs);
664 free(tokens);
665 }
666
667 #if 0
668 static void
669 create_field_bi_pred_frag_shader(struct vl_mpeg12_mc_renderer *r)
670 {
671 assert(false);
672 }
673 #endif
674
675 static void
676 xfer_buffers_map(struct vl_mpeg12_mc_renderer *r)
677 {
678 unsigned i;
679
680 assert(r);
681
682 for (i = 0; i < 3; ++i) {
683 r->tex_transfer[i] = r->pipe->screen->get_tex_transfer
684 (
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
688 );
689
690 r->texels[i] = r->pipe->screen->transfer_map(r->pipe->screen, r->tex_transfer[i]);
691 }
692 }
693
694 static void
695 xfer_buffers_unmap(struct vl_mpeg12_mc_renderer *r)
696 {
697 unsigned i;
698
699 assert(r);
700
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]);
704 }
705 }
706
707 static bool
708 init_pipe_state(struct vl_mpeg12_mc_renderer *r)
709 {
710 struct pipe_sampler_state sampler;
711 unsigned filters[5];
712 unsigned i;
713
714 assert(r);
715
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;
726
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;
731
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;
738
739 /* Luma filter */
740 filters[0] = PIPE_TEX_FILTER_NEAREST;
741 /* Chroma filters */
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;
746 }
747 else {
748 filters[1] = PIPE_TEX_FILTER_LINEAR;
749 filters[2] = PIPE_TEX_FILTER_LINEAR;
750 }
751 /* Fwd, bkwd ref filters */
752 filters[3] = PIPE_TEX_FILTER_LINEAR;
753 filters[4] = PIPE_TEX_FILTER_LINEAR;
754
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 = ; */
768 sampler.min_lod = 0;
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);
773 }
774
775 return true;
776 }
777
778 static void
779 cleanup_pipe_state(struct vl_mpeg12_mc_renderer *r)
780 {
781 unsigned i;
782
783 assert(r);
784
785 for (i = 0; i < 5; ++i)
786 r->pipe->delete_sampler_state(r->pipe, r->samplers.all[i]);
787 }
788
789 static bool
790 init_shaders(struct vl_mpeg12_mc_renderer *r)
791 {
792 assert(r);
793
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);
800
801 return true;
802 }
803
804 static void
805 cleanup_shaders(struct vl_mpeg12_mc_renderer *r)
806 {
807 assert(r);
808
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]);
815 }
816
817 static bool
818 init_buffers(struct vl_mpeg12_mc_renderer *r)
819 {
820 struct pipe_texture template;
821
822 const unsigned mbw =
823 align(r->picture_width, MACROBLOCK_WIDTH) / MACROBLOCK_WIDTH;
824 const unsigned mbh =
825 align(r->picture_height, MACROBLOCK_HEIGHT) / MACROBLOCK_HEIGHT;
826
827 unsigned i;
828
829 assert(r);
830
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));
835
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;
845 template.depth0 = 1;
846 template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER | PIPE_TEXTURE_USAGE_DYNAMIC;
847
848 r->textures.individual.y = r->pipe->screen->texture_create(r->pipe->screen, &template);
849
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;
857 }
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;
862
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);
867
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
872 (
873 r->pipe->screen,
874 DEFAULT_BUF_ALIGNMENT,
875 PIPE_BUFFER_USAGE_VERTEX | PIPE_BUFFER_USAGE_DISCARD,
876 sizeof(struct vertex2f) * 4 * 24 * r->macroblocks_per_batch
877 );
878
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
884 (
885 r->pipe->screen,
886 DEFAULT_BUF_ALIGNMENT,
887 PIPE_BUFFER_USAGE_VERTEX | PIPE_BUFFER_USAGE_DISCARD,
888 sizeof(struct vertex2f) * 2 * 24 * r->macroblocks_per_batch
889 );
890 }
891
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;
897
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;
903
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;
909
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;
915
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;
921
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;
927
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;
933
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;
939
940 r->vs_const_buf.buffer = pipe_buffer_create
941 (
942 r->pipe->screen,
943 DEFAULT_BUF_ALIGNMENT,
944 PIPE_BUFFER_USAGE_CONSTANT | PIPE_BUFFER_USAGE_DISCARD,
945 sizeof(struct vertex_shader_consts)
946 );
947
948 r->fs_const_buf.buffer = pipe_buffer_create
949 (
950 r->pipe->screen,
951 DEFAULT_BUF_ALIGNMENT,
952 PIPE_BUFFER_USAGE_CONSTANT, sizeof(struct fragment_shader_consts)
953 );
954
955 memcpy
956 (
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)
959 );
960
961 pipe_buffer_unmap(r->pipe->screen, r->fs_const_buf.buffer);
962
963 return true;
964 }
965
966 static void
967 cleanup_buffers(struct vl_mpeg12_mc_renderer *r)
968 {
969 unsigned i;
970
971 assert(r);
972
973 pipe_buffer_reference(&r->vs_const_buf.buffer, NULL);
974 pipe_buffer_reference(&r->fs_const_buf.buffer, NULL);
975
976 for (i = 0; i < 3; ++i)
977 pipe_buffer_reference(&r->vertex_bufs.all[i].buffer, NULL);
978
979 for (i = 0; i < 3; ++i)
980 pipe_texture_reference(&r->textures.all[i], NULL);
981
982 FREE(r->macroblock_buf);
983 }
984
985 static enum MACROBLOCK_TYPE
986 get_macroblock_type(struct pipe_mpeg12_macroblock *mb)
987 {
988 assert(mb);
989
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;
1002 default:
1003 assert(0);
1004 }
1005
1006 /* Unreachable */
1007 return -1;
1008 }
1009
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) \
1012 do { \
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); \
1019 \
1020 if (!use_zb || (cbp) & (lm)) \
1021 { \
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); \
1028 } \
1029 else \
1030 { \
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); \
1037 } \
1038 \
1039 if (!use_zb || (cbp) & (cbm)) \
1040 { \
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); \
1047 } \
1048 else \
1049 { \
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); \
1056 } \
1057 \
1058 if (!use_zb || (cbp) & (crm)) \
1059 { \
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); \
1066 } \
1067 else \
1068 { \
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); \
1075 } \
1076 } while (0)
1077
1078 static void
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)
1082 {
1083 struct vertex2f mo_vec[2];
1084
1085 unsigned i;
1086
1087 assert(r);
1088 assert(mb);
1089 assert(ycbcr_vb);
1090 assert(pos < r->macroblocks_per_batch);
1091
1092 mo_vec[1].x = 0;
1093 mo_vec[1].y = 0;
1094
1095 switch (mb->mb_type) {
1096 case PIPE_MPEG12_MACROBLOCK_TYPE_BI:
1097 {
1098 struct vertex2f *vb;
1099
1100 assert(ref_vb && ref_vb[1]);
1101
1102 vb = ref_vb[1] + pos * 2 * 24;
1103
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;
1106
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;
1111 }
1112 }
1113 else {
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;
1116
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;
1122 }
1123 }
1124
1125 /* fall-through */
1126 }
1127 case PIPE_MPEG12_MACROBLOCK_TYPE_FWD:
1128 case PIPE_MPEG12_MACROBLOCK_TYPE_BKWD:
1129 {
1130 struct vertex2f *vb;
1131
1132 assert(ref_vb && ref_vb[0]);
1133
1134 vb = ref_vb[0] + pos * 2 * 24;
1135
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;
1139
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;
1143 }
1144 }
1145 else {
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;
1148
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;
1152 }
1153 }
1154
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;
1159 }
1160 }
1161 else {
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;
1167 }
1168 }
1169
1170 /* fall-through */
1171 }
1172 case PIPE_MPEG12_MACROBLOCK_TYPE_INTRA:
1173 {
1174 const struct vertex2f unit =
1175 {
1176 r->surface_tex_inv_size.x * MACROBLOCK_WIDTH,
1177 r->surface_tex_inv_size.y * MACROBLOCK_HEIGHT
1178 };
1179 const struct vertex2f half =
1180 {
1181 r->surface_tex_inv_size.x * (MACROBLOCK_WIDTH / 2),
1182 r->surface_tex_inv_size.y * (MACROBLOCK_HEIGHT / 2)
1183 };
1184 const bool use_zb = r->eb_handling == VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE;
1185
1186 struct vert_stream_0 *vb = ycbcr_vb + pos * 24;
1187
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);
1191
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);
1195
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);
1199
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);
1203
1204 break;
1205 }
1206 default:
1207 assert(0);
1208 }
1209 }
1210
1211 static void
1212 gen_macroblock_stream(struct vl_mpeg12_mc_renderer *r,
1213 unsigned *num_macroblocks)
1214 {
1215 unsigned offset[NUM_MACROBLOCK_TYPES];
1216 struct vert_stream_0 *ycbcr_vb;
1217 struct vertex2f *ref_vb[2];
1218 unsigned i;
1219
1220 assert(r);
1221 assert(num_macroblocks);
1222
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];
1226 }
1227
1228 offset[0] = 0;
1229
1230 for (i = 1; i < NUM_MACROBLOCK_TYPES; ++i)
1231 offset[i] = offset[i - 1] + num_macroblocks[i - 1];
1232
1233 ycbcr_vb = (struct vert_stream_0 *)pipe_buffer_map
1234 (
1235 r->pipe->screen,
1236 r->vertex_bufs.individual.ycbcr.buffer,
1237 PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_DISCARD
1238 );
1239
1240 for (i = 0; i < 2; ++i)
1241 ref_vb[i] = (struct vertex2f *)pipe_buffer_map
1242 (
1243 r->pipe->screen,
1244 r->vertex_bufs.individual.ref[i].buffer,
1245 PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_DISCARD
1246 );
1247
1248 for (i = 0; i < r->num_macroblocks; ++i) {
1249 enum MACROBLOCK_TYPE mb_type = get_macroblock_type(&r->macroblock_buf[i]);
1250
1251 gen_macroblock_verts(r, &r->macroblock_buf[i], offset[mb_type],
1252 ycbcr_vb, ref_vb);
1253
1254 ++offset[mb_type];
1255 }
1256
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);
1260 }
1261
1262 static void
1263 flush(struct vl_mpeg12_mc_renderer *r)
1264 {
1265 unsigned num_macroblocks[NUM_MACROBLOCK_TYPES] = { 0 };
1266 unsigned vb_start = 0;
1267 struct vertex_shader_consts *vs_consts;
1268 unsigned i;
1269
1270 assert(r);
1271 assert(r->num_macroblocks == r->macroblocks_per_batch);
1272
1273 gen_macroblock_stream(r, num_macroblocks);
1274
1275 r->fb_state.cbufs[0] = r->pipe->screen->get_tex_surface
1276 (
1277 r->pipe->screen, r->surface,
1278 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE
1279 );
1280
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);
1284
1285 vs_consts = pipe_buffer_map
1286 (
1287 r->pipe->screen, r->vs_const_buf.buffer,
1288 PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_DISCARD
1289 );
1290
1291 vs_consts->denorm.x = r->surface->width0;
1292 vs_consts->denorm.y = r->surface->height0;
1293
1294 pipe_buffer_unmap(r->pipe->screen, r->vs_const_buf.buffer);
1295
1296 r->pipe->set_constant_buffer(r->pipe, PIPE_SHADER_VERTEX, 0,
1297 &r->vs_const_buf);
1298 r->pipe->set_constant_buffer(r->pipe, PIPE_SHADER_FRAGMENT, 0,
1299 &r->fs_const_buf);
1300
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);
1308
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;
1312 }
1313
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]);
1322
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;
1326 }
1327
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]);
1336
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;
1340 }
1341
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]);
1350
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;
1354 }
1355
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]);
1364
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;
1368 }
1369
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]);
1379
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;
1383 }
1384
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]);
1394
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;
1398 }
1399
1400 r->pipe->flush(r->pipe, PIPE_FLUSH_RENDER_CACHE, r->fence);
1401 pipe_surface_reference(&r->fb_state.cbufs[0], NULL);
1402
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;
1406
1407 r->num_macroblocks = 0;
1408 }
1409
1410 static void
1411 grab_frame_coded_block(short *src, short *dst, unsigned dst_pitch)
1412 {
1413 unsigned y;
1414
1415 assert(src);
1416 assert(dst);
1417
1418 for (y = 0; y < BLOCK_HEIGHT; ++y)
1419 memcpy(dst + y * dst_pitch, src + y * BLOCK_WIDTH, BLOCK_WIDTH * 2);
1420 }
1421
1422 static void
1423 grab_field_coded_block(short *src, short *dst, unsigned dst_pitch)
1424 {
1425 unsigned y;
1426
1427 assert(src);
1428 assert(dst);
1429
1430 for (y = 0; y < BLOCK_HEIGHT; ++y)
1431 memcpy(dst + y * dst_pitch * 2, src + y * BLOCK_WIDTH, BLOCK_WIDTH * 2);
1432 }
1433
1434 static void
1435 fill_zero_block(short *dst, unsigned dst_pitch)
1436 {
1437 unsigned y;
1438
1439 assert(dst);
1440
1441 for (y = 0; y < BLOCK_HEIGHT; ++y)
1442 memset(dst + y * dst_pitch, 0, BLOCK_WIDTH * 2);
1443 }
1444
1445 static void
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)
1448 {
1449 unsigned tex_pitch;
1450 short *texels;
1451 unsigned tb = 0, sb = 0;
1452 unsigned mbpx = mbx * MACROBLOCK_WIDTH, mbpy = mby * MACROBLOCK_HEIGHT;
1453 unsigned x, y;
1454
1455 assert(r);
1456 assert(blocks);
1457
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;
1460
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);
1468 }
1469 else {
1470 grab_field_coded_block(blocks + sb * BLOCK_WIDTH * BLOCK_HEIGHT,
1471 texels + y * tex_pitch + x * BLOCK_WIDTH,
1472 tex_pitch);
1473 }
1474
1475 ++sb;
1476 }
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;
1484 }
1485 }
1486 }
1487 }
1488 }
1489
1490 /* TODO: Implement 422, 444 */
1491 assert(r->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420);
1492
1493 mbpx /= 2;
1494 mbpy /= 2;
1495
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;
1499
1500 if ((cbp >> (1 - tb)) & 1) {
1501 grab_frame_coded_block(blocks + sb * BLOCK_WIDTH * BLOCK_HEIGHT, texels, tex_pitch);
1502 ++sb;
1503 }
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;
1511 }
1512 }
1513 }
1514 }
1515 }
1516
1517 static void
1518 grab_macroblock(struct vl_mpeg12_mc_renderer *r,
1519 struct pipe_mpeg12_macroblock *mb)
1520 {
1521 assert(r);
1522 assert(mb);
1523 assert(r->num_macroblocks < r->macroblocks_per_batch);
1524
1525 memcpy(&r->macroblock_buf[r->num_macroblocks], mb,
1526 sizeof(struct pipe_mpeg12_macroblock));
1527
1528 grab_blocks(r, mb->mbx, mb->mby, mb->dct_type, mb->cbp, mb->blocks);
1529
1530 ++r->num_macroblocks;
1531 }
1532
1533 bool
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,
1541 bool pot_buffers)
1542 {
1543 unsigned i;
1544
1545 assert(renderer);
1546 assert(pipe);
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);
1554
1555 memset(renderer, 0, sizeof(struct vl_mpeg12_mc_renderer));
1556
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;
1564
1565 if (!init_pipe_state(renderer))
1566 return false;
1567 if (!init_shaders(renderer)) {
1568 cleanup_pipe_state(renderer);
1569 return false;
1570 }
1571 if (!init_buffers(renderer)) {
1572 cleanup_shaders(renderer);
1573 cleanup_pipe_state(renderer);
1574 return false;
1575 }
1576
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;
1583
1584 xfer_buffers_map(renderer);
1585
1586 return true;
1587 }
1588
1589 void
1590 vl_mpeg12_mc_renderer_cleanup(struct vl_mpeg12_mc_renderer *renderer)
1591 {
1592 assert(renderer);
1593
1594 xfer_buffers_unmap(renderer);
1595
1596 cleanup_pipe_state(renderer);
1597 cleanup_shaders(renderer);
1598 cleanup_buffers(renderer);
1599 }
1600
1601 void
1602 vl_mpeg12_mc_renderer_render_macroblocks(struct vl_mpeg12_mc_renderer
1603 *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)
1611 {
1612 bool new_surface = false;
1613
1614 assert(renderer);
1615 assert(surface);
1616 assert(num_macroblocks);
1617 assert(mpeg12_macroblocks);
1618
1619 if (renderer->surface) {
1620 if (surface != renderer->surface) {
1621 if (renderer->num_macroblocks > 0) {
1622 xfer_buffers_unmap(renderer);
1623 flush(renderer);
1624 }
1625
1626 new_surface = true;
1627 }
1628
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);
1632 }
1633 else
1634 new_surface = true;
1635
1636 if (new_surface) {
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;
1643 }
1644
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);
1648 unsigned i;
1649
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]);
1653 }
1654
1655 num_macroblocks -= num_to_submit;
1656
1657 if (renderer->num_macroblocks == renderer->macroblocks_per_batch) {
1658 xfer_buffers_unmap(renderer);
1659 flush(renderer);
1660 xfer_buffers_map(renderer);
1661 /* Next time we get this surface it may have new ref frames */
1662 renderer->surface = NULL;
1663 }
1664 }
1665 }