X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fgallium%2Fauxiliary%2Fvl%2Fvl_mpeg12_mc_renderer.c;h=de83b6a5338b0b363991b5e9a31b2cae5d3cc9dd;hb=78faf8d0e9c276a0ff1465e501d58fb3d66de2f7;hp=6b9ecd42685db66bd0522be7395be1543f95d6c7;hpb=464a72dd4154f314e08c9d0c4d07417e2bf255f0;p=mesa.git diff --git a/src/gallium/auxiliary/vl/vl_mpeg12_mc_renderer.c b/src/gallium/auxiliary/vl/vl_mpeg12_mc_renderer.c index 6b9ecd42685..de83b6a5338 100644 --- a/src/gallium/auxiliary/vl/vl_mpeg12_mc_renderer.c +++ b/src/gallium/auxiliary/vl/vl_mpeg12_mc_renderer.c @@ -1,8 +1,8 @@ /************************************************************************** - * + * * Copyright 2009 Younes Manton. * All Rights Reserved. - * + * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including @@ -10,11 +10,11 @@ * distribute, sub license, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: - * + * * The above copyright notice and this permission notice (including the * next paragraph) shall be included in all copies or substantial portions * of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. @@ -22,701 +22,420 @@ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * + * **************************************************************************/ #include "vl_mpeg12_mc_renderer.h" +#include "util/u_draw.h" #include #include #include #include #include #include -#include -#include -#include "vl_shader_build.h" +#include +#include +#include +#include #define DEFAULT_BUF_ALIGNMENT 1 #define MACROBLOCK_WIDTH 16 #define MACROBLOCK_HEIGHT 16 #define BLOCK_WIDTH 8 #define BLOCK_HEIGHT 8 -#define ZERO_BLOCK_NIL -1.0f -#define ZERO_BLOCK_IS_NIL(zb) ((zb).x < 0.0f) -struct vertex2f +struct vertex_stream { - float x, y; + struct vertex2s pos; + struct vertex2s mv[4]; + struct { + int8_t y; + int8_t cr; + int8_t cb; + int8_t flag; + } eb[2][2]; }; -struct vertex4f +enum VS_INPUT { - float x, y, z, w; + VS_I_RECT, + VS_I_VPOS, + VS_I_MV0, + VS_I_MV1, + VS_I_MV2, + VS_I_MV3, + VS_I_EB_0_0, + VS_I_EB_0_1, + VS_I_EB_1_0, + VS_I_EB_1_1, + + NUM_VS_INPUTS }; -struct vertex_shader_consts +enum VS_OUTPUT { - struct vertex4f denorm; + VS_O_VPOS, + VS_O_LINE, + VS_O_TEX0, + VS_O_TEX1, + VS_O_TEX2, + VS_O_EB_0, + VS_O_EB_1, + VS_O_INFO, + VS_O_MV0, + VS_O_MV1, + VS_O_MV2, + VS_O_MV3 }; -struct fragment_shader_consts -{ - struct vertex4f multiplier; - struct vertex4f div; -}; - -/* - * Muliplier renormalizes block samples from 16 bits to 12 bits. - * Divider is used when calculating Y % 2 for choosing top or bottom - * field for P or B macroblocks. - * TODO: Use immediates. - */ -static const struct fragment_shader_consts fs_consts = { - {32767.0f / 255.0f, 32767.0f / 255.0f, 32767.0f / 255.0f, 0.0f}, - {0.5f, 2.0f, 0.0f, 0.0f} -}; - -struct vert_stream_0 -{ - struct vertex2f pos; - struct vertex2f luma_tc; - struct vertex2f cb_tc; - struct vertex2f cr_tc; +static const unsigned const_empty_block_mask_420[3][2][2] = { + { { 0x20, 0x10 }, { 0x08, 0x04 } }, + { { 0x02, 0x02 }, { 0x02, 0x02 } }, + { { 0x01, 0x01 }, { 0x01, 0x01 } } }; -enum MACROBLOCK_TYPE +static void * +create_vert_shader(struct vl_mpeg12_mc_renderer *r) { - MACROBLOCK_TYPE_INTRA, - MACROBLOCK_TYPE_FWD_FRAME_PRED, - MACROBLOCK_TYPE_FWD_FIELD_PRED, - MACROBLOCK_TYPE_BKWD_FRAME_PRED, - MACROBLOCK_TYPE_BKWD_FIELD_PRED, - MACROBLOCK_TYPE_BI_FRAME_PRED, - MACROBLOCK_TYPE_BI_FIELD_PRED, - - NUM_MACROBLOCK_TYPES -}; - -static void -create_intra_vert_shader(struct vl_mpeg12_mc_renderer *r) -{ - const unsigned max_tokens = 50; - - struct pipe_shader_state vs; - struct tgsi_token *tokens; - struct tgsi_header *header; - - struct tgsi_full_declaration decl; - struct tgsi_full_instruction inst; - - unsigned ti; - - unsigned i; + struct ureg_program *shader; + struct ureg_src block_scale, mv_scale; + struct ureg_src vrect, vpos, eb[2][2], vmv[4]; + struct ureg_dst t_vpos, t_vtex, t_vmv; + struct ureg_dst o_vpos, o_line, o_vtex[3], o_eb[2], o_vmv[4], o_info; + unsigned i, label; + + shader = ureg_create(TGSI_PROCESSOR_VERTEX); + if (!shader) + return NULL; + + t_vpos = ureg_DECL_temporary(shader); + t_vtex = ureg_DECL_temporary(shader); + t_vmv = ureg_DECL_temporary(shader); + + vrect = ureg_DECL_vs_input(shader, VS_I_RECT); + vpos = ureg_DECL_vs_input(shader, VS_I_VPOS); + eb[0][0] = ureg_DECL_vs_input(shader, VS_I_EB_0_0); + eb[1][0] = ureg_DECL_vs_input(shader, VS_I_EB_1_0); + eb[0][1] = ureg_DECL_vs_input(shader, VS_I_EB_0_1); + eb[1][1] = ureg_DECL_vs_input(shader, VS_I_EB_1_1); + + o_vpos = ureg_DECL_output(shader, TGSI_SEMANTIC_POSITION, VS_O_VPOS); + o_line = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_LINE); + o_vtex[0] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX0); + o_vtex[1] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX1); + o_vtex[2] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX2); + o_eb[0] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_EB_0); + o_eb[1] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_EB_1); + o_info = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_INFO); - assert(r); - - tokens = (struct tgsi_token *) malloc(max_tokens * sizeof(struct tgsi_token)); - header = (struct tgsi_header *) &tokens[0]; - *header = tgsi_build_header(); - *(struct tgsi_processor *) &tokens[1] = tgsi_build_processor(TGSI_PROCESSOR_VERTEX, header); - - ti = 2; - - /* - * decl i0 ; Vertex pos - * decl i1 ; Luma texcoords - * decl i2 ; Chroma Cb texcoords - * decl i3 ; Chroma Cr texcoords - */ - for (i = 0; i < 4; i++) { - decl = vl_decl_input(i == 0 ? TGSI_SEMANTIC_POSITION : TGSI_SEMANTIC_GENERIC, i, i, i); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - } - - /* - * decl o0 ; Vertex pos - * decl o1 ; Luma texcoords - * decl o2 ; Chroma Cb texcoords - * decl o3 ; Chroma Cr texcoords - */ - for (i = 0; i < 4; i++) { - decl = vl_decl_output(i == 0 ? TGSI_SEMANTIC_POSITION : TGSI_SEMANTIC_GENERIC, i, i, i); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - } - - /* - * mov o0, i0 ; Move input vertex pos to output - * mov o1, i1 ; Move input luma texcoords to output - * mov o2, i2 ; Move input chroma Cb texcoords to output - * mov o3, i3 ; Move input chroma Cr texcoords to output - */ for (i = 0; i < 4; ++i) { - inst = vl_inst2(TGSI_OPCODE_MOV, TGSI_FILE_OUTPUT, i, TGSI_FILE_INPUT, i); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); + vmv[i] = ureg_DECL_vs_input(shader, VS_I_MV0 + i); + o_vmv[i] = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_MV0 + i); } - /* end */ - inst = vl_end(); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - assert(ti <= max_tokens); - - vs.tokens = tokens; - r->i_vs = r->pipe->create_vs_state(r->pipe, &vs); - free(tokens); -} - -static void -create_intra_frag_shader(struct vl_mpeg12_mc_renderer *r) -{ - const unsigned max_tokens = 100; - - struct pipe_shader_state fs; - struct tgsi_token *tokens; - struct tgsi_header *header; - - struct tgsi_full_declaration decl; - struct tgsi_full_instruction inst; - - unsigned ti; - - unsigned i; - - assert(r); - - tokens = (struct tgsi_token *) malloc(max_tokens * sizeof(struct tgsi_token)); - header = (struct tgsi_header *) &tokens[0]; - *header = tgsi_build_header(); - *(struct tgsi_processor *) &tokens[1] = tgsi_build_processor(TGSI_PROCESSOR_FRAGMENT, header); - - ti = 2; - - /* - * decl i0 ; Luma texcoords - * decl i1 ; Chroma Cb texcoords - * decl i2 ; Chroma Cr texcoords - */ - for (i = 0; i < 3; ++i) { - decl = vl_decl_interpolated_input(TGSI_SEMANTIC_GENERIC, i + 1, i, i, TGSI_INTERPOLATE_LINEAR); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - } - - /* decl c0 ; Scaling factor, rescales 16-bit snorm to 9-bit snorm */ - decl = vl_decl_constants(TGSI_SEMANTIC_GENERIC, 0, 0, 0); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - - /* decl o0 ; Fragment color */ - decl = vl_decl_output(TGSI_SEMANTIC_COLOR, 0, 0, 0); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - - /* decl t0, t1 */ - decl = vl_decl_temps(0, 1); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - /* - * decl s0 ; Sampler for luma texture - * decl s1 ; Sampler for chroma Cb texture - * decl s2 ; Sampler for chroma Cr texture + * block_scale = (MACROBLOCK_WIDTH, MACROBLOCK_HEIGHT) / (dst.width, dst.height) + * mv_scale = 0.5 / (dst.width, dst.height); + * + * t_vpos = (vpos + vrect) * block_scale + * o_vpos.xy = t_vpos + * o_vpos.zw = vpos + * + * o_eb[0..1] = vrect.x ? eb[0..1][1] : eb[0..1][0] + * + * o_frame_pred = frame_pred + * o_info.x = ref_frames + * o_info.y = ref_frames > 0 + * o_info.z = bkwd_pred + * + * // Apply motion vectors + * o_vmv[0..count] = t_vpos + vmv[0..count] * mv_scale + * + * o_line.xy = vrect * 8 + * o_line.z = interlaced + * + * if(eb[0][0].w) { //interlaced + * t_vtex.x = vrect.x + * t_vtex.y = vrect.y * 0.5 + * t_vtex += vpos + * + * o_vtex[0].xy = t_vtex * block_scale + * + * t_vtex.y += 0.5 + * o_vtex[1].xy = t_vtex * block_scale + * } else { + * o_vtex[0..1].xy = t_vpos + * } + * o_vtex[2].xy = t_vpos + * */ - for (i = 0; i < 3; ++i) { - decl = vl_decl_samplers(i, i); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - } - - /* - * tex2d t1, i0, s0 ; Read texel from luma texture - * mov t0.x, t1.x ; Move luma sample into .x component - * tex2d t1, i1, s1 ; Read texel from chroma Cb texture - * mov t0.y, t1.x ; Move Cb sample into .y component - * tex2d t1, i2, s2 ; Read texel from chroma Cr texture - * mov t0.z, t1.x ; Move Cr sample into .z component - */ - for (i = 0; i < 3; ++i) { - inst = vl_tex(TGSI_TEXTURE_2D, TGSI_FILE_TEMPORARY, 1, TGSI_FILE_INPUT, i, TGSI_FILE_SAMPLER, i); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - inst = vl_inst2(TGSI_OPCODE_MOV, TGSI_FILE_TEMPORARY, 0, TGSI_FILE_TEMPORARY, 1); - inst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_X; - inst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_X; - inst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_X; - inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_X << i; - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - } - - /* mul o0, t0, c0 ; Rescale texel to correct range */ - inst = vl_inst3(TGSI_OPCODE_MUL, TGSI_FILE_OUTPUT, 0, TGSI_FILE_TEMPORARY, 0, TGSI_FILE_CONSTANT, 0); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - /* end */ - inst = vl_end(); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - assert(ti <= max_tokens); - - fs.tokens = tokens; - r->i_fs = r->pipe->create_fs_state(r->pipe, &fs); - free(tokens); + block_scale = ureg_imm2f(shader, + (float)MACROBLOCK_WIDTH / r->buffer_width, + (float)MACROBLOCK_HEIGHT / r->buffer_height); + + mv_scale = ureg_imm2f(shader, + 0.5f / r->buffer_width, + 0.5f / r->buffer_height); + + ureg_ADD(shader, ureg_writemask(t_vpos, TGSI_WRITEMASK_XY), vpos, vrect); + ureg_MUL(shader, ureg_writemask(t_vpos, TGSI_WRITEMASK_XY), ureg_src(t_vpos), block_scale); + ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_XY), ureg_src(t_vpos)); + ureg_MOV(shader, ureg_writemask(o_vpos, TGSI_WRITEMASK_ZW), vpos); + + ureg_CMP(shader, ureg_writemask(o_eb[0], TGSI_WRITEMASK_XYZ), + ureg_negate(ureg_scalar(vrect, TGSI_SWIZZLE_X)), + eb[0][1], eb[0][0]); + ureg_CMP(shader, ureg_writemask(o_eb[1], TGSI_WRITEMASK_XYZ), + ureg_negate(ureg_scalar(vrect, TGSI_SWIZZLE_X)), + eb[1][1], eb[1][0]); + + ureg_MOV(shader, ureg_writemask(o_info, TGSI_WRITEMASK_X), + ureg_scalar(eb[1][1], TGSI_SWIZZLE_W)); + ureg_SGE(shader, ureg_writemask(o_info, TGSI_WRITEMASK_Y), + ureg_scalar(eb[1][1], TGSI_SWIZZLE_W), ureg_imm1f(shader, 0.0f)); + ureg_MOV(shader, ureg_writemask(o_info, TGSI_WRITEMASK_Z), + ureg_scalar(eb[1][0], TGSI_SWIZZLE_W)); + + ureg_MAD(shader, ureg_writemask(o_vmv[0], TGSI_WRITEMASK_XY), mv_scale, vmv[0], ureg_src(t_vpos)); + ureg_MAD(shader, ureg_writemask(o_vmv[2], TGSI_WRITEMASK_XY), mv_scale, vmv[2], ureg_src(t_vpos)); + + ureg_CMP(shader, ureg_writemask(t_vmv, TGSI_WRITEMASK_XY), + ureg_negate(ureg_scalar(eb[0][1], TGSI_SWIZZLE_W)), + vmv[0], vmv[1]); + ureg_MAD(shader, ureg_writemask(o_vmv[1], TGSI_WRITEMASK_XY), mv_scale, ureg_src(t_vmv), ureg_src(t_vpos)); + + ureg_CMP(shader, ureg_writemask(t_vmv, TGSI_WRITEMASK_XY), + ureg_negate(ureg_scalar(eb[0][1], TGSI_SWIZZLE_W)), + vmv[2], vmv[3]); + ureg_MAD(shader, ureg_writemask(o_vmv[3], TGSI_WRITEMASK_XY), mv_scale, ureg_src(t_vmv), ureg_src(t_vpos)); + + ureg_MOV(shader, ureg_writemask(o_vtex[0], TGSI_WRITEMASK_XY), ureg_src(t_vpos)); + ureg_MOV(shader, ureg_writemask(o_vtex[1], TGSI_WRITEMASK_XY), ureg_src(t_vpos)); + ureg_MOV(shader, ureg_writemask(o_vtex[2], TGSI_WRITEMASK_XY), ureg_src(t_vpos)); + + ureg_MOV(shader, ureg_writemask(o_line, TGSI_WRITEMASK_X), ureg_scalar(vrect, TGSI_SWIZZLE_Y)); + ureg_MUL(shader, ureg_writemask(o_line, TGSI_WRITEMASK_Y), + vrect, ureg_imm1f(shader, MACROBLOCK_HEIGHT / 2)); + + ureg_IF(shader, ureg_scalar(eb[0][0], TGSI_SWIZZLE_W), &label); + + ureg_MOV(shader, ureg_writemask(t_vtex, TGSI_WRITEMASK_X), vrect); + ureg_MUL(shader, ureg_writemask(t_vtex, TGSI_WRITEMASK_Y), vrect, ureg_imm1f(shader, 0.5f)); + ureg_ADD(shader, ureg_writemask(t_vtex, TGSI_WRITEMASK_XY), vpos, ureg_src(t_vtex)); + ureg_MUL(shader, ureg_writemask(o_vtex[0], TGSI_WRITEMASK_XY), ureg_src(t_vtex), block_scale); + ureg_ADD(shader, ureg_writemask(t_vtex, TGSI_WRITEMASK_Y), ureg_src(t_vtex), ureg_imm1f(shader, 0.5f)); + ureg_MUL(shader, ureg_writemask(o_vtex[1], TGSI_WRITEMASK_XY), ureg_src(t_vtex), block_scale); + + ureg_MUL(shader, ureg_writemask(o_line, TGSI_WRITEMASK_X), + ureg_scalar(vrect, TGSI_SWIZZLE_Y), + ureg_imm1f(shader, MACROBLOCK_HEIGHT / 2)); + + ureg_fixup_label(shader, label, ureg_get_instruction_number(shader)); + ureg_ENDIF(shader); + + ureg_release_temporary(shader, t_vtex); + ureg_release_temporary(shader, t_vpos); + ureg_release_temporary(shader, t_vmv); + + ureg_END(shader); + + return ureg_create_shader_and_destroy(shader, r->pipe); } -static void -create_frame_pred_vert_shader(struct vl_mpeg12_mc_renderer *r) +static struct ureg_dst +calc_field(struct ureg_program *shader) { - const unsigned max_tokens = 100; - - struct pipe_shader_state vs; - struct tgsi_token *tokens; - struct tgsi_header *header; - - struct tgsi_full_declaration decl; - struct tgsi_full_instruction inst; - - unsigned ti; - - unsigned i; - - assert(r); - - tokens = (struct tgsi_token *) malloc(max_tokens * sizeof(struct tgsi_token)); - header = (struct tgsi_header *) &tokens[0]; - *header = tgsi_build_header(); - *(struct tgsi_processor *) &tokens[1] = tgsi_build_processor(TGSI_PROCESSOR_VERTEX, header); + struct ureg_dst tmp; + struct ureg_src line; - ti = 2; + tmp = ureg_DECL_temporary(shader); - /* - * decl i0 ; Vertex pos - * decl i1 ; Luma texcoords - * decl i2 ; Chroma Cb texcoords - * decl i3 ; Chroma Cr texcoords - * decl i4 ; Ref surface top field texcoords - * decl i5 ; Ref surface bottom field texcoords (unused, packed in the same stream) - */ - for (i = 0; i < 6; i++) { - decl = vl_decl_input(i == 0 ? TGSI_SEMANTIC_POSITION : TGSI_SEMANTIC_GENERIC, i, i, i); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - } - - /* - * decl o0 ; Vertex pos - * decl o1 ; Luma texcoords - * decl o2 ; Chroma Cb texcoords - * decl o3 ; Chroma Cr texcoords - * decl o4 ; Ref macroblock texcoords - */ - for (i = 0; i < 5; i++) { - decl = vl_decl_output(i == 0 ? TGSI_SEMANTIC_POSITION : TGSI_SEMANTIC_GENERIC, i, i, i); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - } + line = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_LINE, TGSI_INTERPOLATE_LINEAR); /* - * mov o0, i0 ; Move input vertex pos to output - * mov o1, i1 ; Move input luma texcoords to output - * mov o2, i2 ; Move input chroma Cb texcoords to output - * mov o3, i3 ; Move input chroma Cr texcoords to output + * line.x going from 0 to 1 if not interlaced + * line.x going from 0 to 8 in steps of 0.5 if interlaced + * line.y going from 0 to 8 in steps of 0.5 + * + * tmp.xy = fraction(line) + * tmp.xy = tmp.xy >= 0.5 ? 1 : 0 */ - for (i = 0; i < 4; ++i) { - inst = vl_inst2(TGSI_OPCODE_MOV, TGSI_FILE_OUTPUT, i, TGSI_FILE_INPUT, i); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - } - - /* add o4, i0, i4 ; Translate vertex pos by motion vec to form ref macroblock texcoords */ - inst = vl_inst3(TGSI_OPCODE_ADD, TGSI_FILE_OUTPUT, 4, TGSI_FILE_INPUT, 0, TGSI_FILE_INPUT, 4); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - /* end */ - inst = vl_end(); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - assert(ti <= max_tokens); + ureg_FRC(shader, ureg_writemask(tmp, TGSI_WRITEMASK_XY), line); + ureg_SGE(shader, ureg_writemask(tmp, TGSI_WRITEMASK_XY), ureg_src(tmp), ureg_imm1f(shader, 0.5f)); - vs.tokens = tokens; - r->p_vs[0] = r->pipe->create_vs_state(r->pipe, &vs); - free(tokens); + return tmp; } -#if 0 -static void -create_field_pred_vert_shader(struct vl_mpeg12_mc_renderer *r) +static struct ureg_dst +fetch_ycbcr(struct vl_mpeg12_mc_renderer *r, struct ureg_program *shader, struct ureg_dst field) { - assert(false); -} -#endif - -static void -create_frame_pred_frag_shader(struct vl_mpeg12_mc_renderer *r) -{ - const unsigned max_tokens = 100; - - struct pipe_shader_state fs; - struct tgsi_token *tokens; - struct tgsi_header *header; - - struct tgsi_full_declaration decl; - struct tgsi_full_instruction inst; - - unsigned ti; - - unsigned i; - - assert(r); - - tokens = (struct tgsi_token *) malloc(max_tokens * sizeof(struct tgsi_token)); - header = (struct tgsi_header *) &tokens[0]; - *header = tgsi_build_header(); - *(struct tgsi_processor *) &tokens[1] = tgsi_build_processor(TGSI_PROCESSOR_FRAGMENT, header); - - ti = 2; + struct ureg_src tc[3], sampler[3], eb[2]; + struct ureg_dst texel, t_tc, t_eb_info; + unsigned i, label; - /* - * decl i0 ; Luma texcoords - * decl i1 ; Chroma Cb texcoords - * decl i2 ; Chroma Cr texcoords - * decl i3 ; Ref macroblock texcoords - */ - for (i = 0; i < 4; ++i) { - decl = vl_decl_interpolated_input(TGSI_SEMANTIC_GENERIC, i + 1, i, i, TGSI_INTERPOLATE_LINEAR); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - } + texel = ureg_DECL_temporary(shader); + t_tc = ureg_DECL_temporary(shader); + t_eb_info = ureg_DECL_temporary(shader); - /* decl c0 ; Scaling factor, rescales 16-bit snorm to 9-bit snorm */ - decl = vl_decl_constants(TGSI_SEMANTIC_GENERIC, 0, 0, 0); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); + tc[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX0, TGSI_INTERPOLATE_LINEAR); + tc[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX1, TGSI_INTERPOLATE_LINEAR); + tc[2] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_TEX2, TGSI_INTERPOLATE_LINEAR); - /* decl o0 ; Fragment color */ - decl = vl_decl_output(TGSI_SEMANTIC_COLOR, 0, 0, 0); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); + eb[0] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_EB_0, TGSI_INTERPOLATE_CONSTANT); + eb[1] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_EB_1, TGSI_INTERPOLATE_CONSTANT); - /* decl t0, t1 */ - decl = vl_decl_temps(0, 1); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - - /* - * decl s0 ; Sampler for luma texture - * decl s1 ; Sampler for chroma Cb texture - * decl s2 ; Sampler for chroma Cr texture - * decl s3 ; Sampler for ref surface texture - */ - for (i = 0; i < 4; ++i) { - decl = vl_decl_samplers(i, i); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); + for (i = 0; i < 3; ++i) { + sampler[i] = ureg_DECL_sampler(shader, i); } /* - * tex2d t1, i0, s0 ; Read texel from luma texture - * mov t0.x, t1.x ; Move luma sample into .x component - * tex2d t1, i1, s1 ; Read texel from chroma Cb texture - * mov t0.y, t1.x ; Move Cb sample into .y component - * tex2d t1, i2, s2 ; Read texel from chroma Cr texture - * mov t0.z, t1.x ; Move Cr sample into .z component + * texel.y = tex(field.y ? tc[1] : tc[0], sampler[0]) + * texel.cb = tex(tc[2], sampler[1]) + * texel.cr = tex(tc[2], sampler[2]) */ - for (i = 0; i < 3; ++i) { - inst = vl_tex(TGSI_TEXTURE_2D, TGSI_FILE_TEMPORARY, 1, TGSI_FILE_INPUT, i, TGSI_FILE_SAMPLER, i); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - inst = vl_inst2(TGSI_OPCODE_MOV, TGSI_FILE_TEMPORARY, 0, TGSI_FILE_TEMPORARY, 1); - inst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_X; - inst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_X; - inst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_X; - inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_X << i; - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - } - - /* mul t0, t0, c0 ; Rescale texel to correct range */ - inst = vl_inst3(TGSI_OPCODE_MUL, TGSI_FILE_TEMPORARY, 0, TGSI_FILE_TEMPORARY, 0, TGSI_FILE_CONSTANT, 0); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - /* tex2d t1, i3, s3 ; Read texel from ref macroblock */ - inst = vl_tex(TGSI_TEXTURE_2D, TGSI_FILE_TEMPORARY, 1, TGSI_FILE_INPUT, 3, TGSI_FILE_SAMPLER, 3); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); + ureg_CMP(shader, ureg_writemask(t_tc, TGSI_WRITEMASK_XY), + ureg_negate(ureg_scalar(ureg_src(field), TGSI_SWIZZLE_X)), + tc[1], tc[0]); - /* add o0, t0, t1 ; Add ref and differential to form final output */ - inst = vl_inst3(TGSI_OPCODE_ADD, TGSI_FILE_OUTPUT, 0, TGSI_FILE_TEMPORARY, 0, TGSI_FILE_TEMPORARY, 1); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); + ureg_CMP(shader, ureg_writemask(t_eb_info, TGSI_WRITEMASK_XYZ), + ureg_negate(ureg_scalar(ureg_src(field), TGSI_SWIZZLE_X)), + eb[1], eb[0]); - /* end */ - inst = vl_end(); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - assert(ti <= max_tokens); - - fs.tokens = tokens; - r->p_fs[0] = r->pipe->create_fs_state(r->pipe, &fs); - free(tokens); -} + /* r600g is ignoring TGSI_INTERPOLATE_CONSTANT, just workaround this */ + ureg_SLT(shader, ureg_writemask(t_eb_info, TGSI_WRITEMASK_XYZ), ureg_src(t_eb_info), ureg_imm1f(shader, 0.5f)); -#if 0 -static void -create_field_pred_frag_shader(struct vl_mpeg12_mc_renderer *r) -{ - assert(false); -} -#endif - -static void -create_frame_bi_pred_vert_shader(struct vl_mpeg12_mc_renderer *r) -{ - const unsigned max_tokens = 100; - - struct pipe_shader_state vs; - struct tgsi_token *tokens; - struct tgsi_header *header; - - struct tgsi_full_declaration decl; - struct tgsi_full_instruction inst; - - unsigned ti; - - unsigned i; - - assert(r); - - tokens = (struct tgsi_token *) malloc(max_tokens * sizeof(struct tgsi_token)); - header = (struct tgsi_header *) &tokens[0]; - *header = tgsi_build_header(); - *(struct tgsi_processor *) &tokens[1] = tgsi_build_processor(TGSI_PROCESSOR_VERTEX, header); - - ti = 2; - - /* - * decl i0 ; Vertex pos - * decl i1 ; Luma texcoords - * decl i2 ; Chroma Cb texcoords - * decl i3 ; Chroma Cr texcoords - * decl i4 ; First ref macroblock top field texcoords - * decl i5 ; First ref macroblock bottom field texcoords (unused, packed in the same stream) - * decl i6 ; Second ref macroblock top field texcoords - * decl i7 ; Second ref macroblock bottom field texcoords (unused, packed in the same stream) - */ - for (i = 0; i < 8; i++) { - decl = vl_decl_input(i == 0 ? TGSI_SEMANTIC_POSITION : TGSI_SEMANTIC_GENERIC, i, i, i); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - } - - /* - * decl o0 ; Vertex pos - * decl o1 ; Luma texcoords - * decl o2 ; Chroma Cb texcoords - * decl o3 ; Chroma Cr texcoords - * decl o4 ; First ref macroblock texcoords - * decl o5 ; Second ref macroblock texcoords - */ - for (i = 0; i < 6; i++) { - decl = vl_decl_output(i == 0 ? TGSI_SEMANTIC_POSITION : TGSI_SEMANTIC_GENERIC, i, i, i); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - } + ureg_MOV(shader, ureg_writemask(texel, TGSI_WRITEMASK_XYZ), ureg_imm1f(shader, 0.0f)); + for (i = 0; i < 3; ++i) { + ureg_IF(shader, ureg_scalar(ureg_src(t_eb_info), TGSI_SWIZZLE_X + i), &label); - /* - * mov o0, i0 ; Move input vertex pos to output - * mov o1, i1 ; Move input luma texcoords to output - * mov o2, i2 ; Move input chroma Cb texcoords to output - * mov o3, i3 ; Move input chroma Cr texcoords to output - */ - for (i = 0; i < 4; ++i) { - inst = vl_inst2(TGSI_OPCODE_MOV, TGSI_FILE_OUTPUT, i, TGSI_FILE_INPUT, i); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - } + /* Nouveau can't writemask tex dst regs (yet?), so this won't work anymore on nvidia hardware */ + if(i==0 || r->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_444) { + ureg_TEX(shader, ureg_writemask(texel, TGSI_WRITEMASK_X << i), TGSI_TEXTURE_3D, ureg_src(t_tc), sampler[i]); + } else { + ureg_TEX(shader, ureg_writemask(texel, TGSI_WRITEMASK_X << i), TGSI_TEXTURE_3D, tc[2], sampler[i]); + } - /* - * add o4, i0, i4 ; Translate vertex pos by motion vec to form first ref macroblock texcoords - * add o5, i0, i6 ; Translate vertex pos by motion vec to form second ref macroblock texcoords - */ - for (i = 0; i < 2; ++i) { - inst = vl_inst3(TGSI_OPCODE_ADD, TGSI_FILE_OUTPUT, i + 4, TGSI_FILE_INPUT, 0, TGSI_FILE_INPUT, (i + 2) * 2); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); + ureg_fixup_label(shader, label, ureg_get_instruction_number(shader)); + ureg_ENDIF(shader); } - /* end */ - inst = vl_end(); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - assert(ti <= max_tokens); - - vs.tokens = tokens; - r->b_vs[0] = r->pipe->create_vs_state(r->pipe, &vs); - free(tokens); -} + ureg_release_temporary(shader, t_tc); + ureg_release_temporary(shader, t_eb_info); -#if 0 -static void -create_field_bi_pred_vert_shader(struct vl_mpeg12_mc_renderer *r) -{ - assert(false); + return texel; } -#endif -static void -create_frame_bi_pred_frag_shader(struct vl_mpeg12_mc_renderer *r) +static struct ureg_dst +fetch_ref(struct ureg_program *shader, struct ureg_dst field) { - const unsigned max_tokens = 100; - - struct pipe_shader_state fs; - struct tgsi_token *tokens; - struct tgsi_header *header; - - struct tgsi_full_declaration decl; - struct tgsi_full_instruction inst; - - unsigned ti; - - unsigned i; - - assert(r); - - tokens = (struct tgsi_token *) malloc(max_tokens * sizeof(struct tgsi_token)); - header = (struct tgsi_header *) &tokens[0]; - *header = tgsi_build_header(); - *(struct tgsi_processor *) &tokens[1] = tgsi_build_processor(TGSI_PROCESSOR_FRAGMENT, header); - - ti = 2; - - /* - * decl i0 ; Luma texcoords - * decl i1 ; Chroma Cb texcoords - * decl i2 ; Chroma Cr texcoords - * decl i3 ; First ref macroblock texcoords - * decl i4 ; Second ref macroblock texcoords - */ - for (i = 0; i < 5; ++i) { - decl = vl_decl_interpolated_input(TGSI_SEMANTIC_GENERIC, i + 1, i, i, TGSI_INTERPOLATE_LINEAR); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - } - - /* - * decl c0 ; Scaling factor, rescales 16-bit snorm to 9-bit snorm - * decl c1 ; Constant 1/2 in .x channel to use as weight to blend past and future texels - */ - decl = vl_decl_constants(TGSI_SEMANTIC_GENERIC, 0, 0, 1); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - - /* decl o0 ; Fragment color */ - decl = vl_decl_output(TGSI_SEMANTIC_COLOR, 0, 0, 0); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); + struct ureg_src info; + struct ureg_src tc[4], sampler[2]; + struct ureg_dst ref[2], result; + unsigned i, intra_label, bi_label, label; - /* decl t0-t2 */ - decl = vl_decl_temps(0, 2); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); + info = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_INFO, TGSI_INTERPOLATE_CONSTANT); - /* - * decl s0 ; Sampler for luma texture - * decl s1 ; Sampler for chroma Cb texture - * decl s2 ; Sampler for chroma Cr texture - * decl s3 ; Sampler for first ref surface texture - * decl s4 ; Sampler for second ref surface texture - */ - for (i = 0; i < 5; ++i) { - decl = vl_decl_samplers(i, i); - ti += tgsi_build_full_declaration(&decl, &tokens[ti], header, max_tokens - ti); - } - - /* - * tex2d t1, i0, s0 ; Read texel from luma texture - * mov t0.x, t1.x ; Move luma sample into .x component - * tex2d t1, i1, s1 ; Read texel from chroma Cb texture - * mov t0.y, t1.x ; Move Cb sample into .y component - * tex2d t1, i2, s2 ; Read texel from chroma Cr texture - * mov t0.z, t1.x ; Move Cr sample into .z component - */ - for (i = 0; i < 3; ++i) { - inst = vl_tex(TGSI_TEXTURE_2D, TGSI_FILE_TEMPORARY, 1, TGSI_FILE_INPUT, i, TGSI_FILE_SAMPLER, i); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - inst = vl_inst2(TGSI_OPCODE_MOV, TGSI_FILE_TEMPORARY, 0, TGSI_FILE_TEMPORARY, 1); - inst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_X; - inst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_X; - inst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_X; - inst.Dst[0].Register.WriteMask = TGSI_WRITEMASK_X << i; - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - } + for (i = 0; i < 4; ++i) + tc[i] = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_MV0 + i, TGSI_INTERPOLATE_LINEAR); - /* mul t0, t0, c0 ; Rescale texel to correct range */ - inst = vl_inst3(TGSI_OPCODE_MUL, TGSI_FILE_TEMPORARY, 0, TGSI_FILE_TEMPORARY, 0, TGSI_FILE_CONSTANT, 0); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - /* - * tex2d t1, i3, s3 ; Read texel from first ref macroblock - * tex2d t2, i4, s4 ; Read texel from second ref macroblock - */ for (i = 0; i < 2; ++i) { - inst = vl_tex(TGSI_TEXTURE_2D, TGSI_FILE_TEMPORARY, i + 1, TGSI_FILE_INPUT, i + 3, TGSI_FILE_SAMPLER, i + 3); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); + sampler[i] = ureg_DECL_sampler(shader, i + 3); + ref[i] = ureg_DECL_temporary(shader); } - /* lerp t1, c1.x, t1, t2 ; Blend past and future texels */ - inst = vl_inst4(TGSI_OPCODE_LRP, TGSI_FILE_TEMPORARY, 1, TGSI_FILE_CONSTANT, 1, TGSI_FILE_TEMPORARY, 1, TGSI_FILE_TEMPORARY, 2); - inst.Src[0].Register.SwizzleX = TGSI_SWIZZLE_X; - inst.Src[0].Register.SwizzleY = TGSI_SWIZZLE_X; - inst.Src[0].Register.SwizzleZ = TGSI_SWIZZLE_X; - inst.Src[0].Register.SwizzleW = TGSI_SWIZZLE_X; - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - /* add o0, t0, t1 ; Add past/future ref and differential to form final output */ - inst = vl_inst3(TGSI_OPCODE_ADD, TGSI_FILE_OUTPUT, 0, TGSI_FILE_TEMPORARY, 0, TGSI_FILE_TEMPORARY, 1); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); - - /* end */ - inst = vl_end(); - ti += tgsi_build_full_instruction(&inst, &tokens[ti], header, max_tokens - ti); + result = ureg_DECL_temporary(shader); + + ureg_MOV(shader, ureg_writemask(result, TGSI_WRITEMASK_XYZ), ureg_imm1f(shader, 0.5f)); + + ureg_IF(shader, ureg_scalar(info, TGSI_SWIZZLE_Y), &intra_label); + ureg_CMP(shader, ureg_writemask(ref[0], TGSI_WRITEMASK_XY), + ureg_negate(ureg_scalar(ureg_src(field), TGSI_SWIZZLE_Y)), + tc[1], tc[0]); + + ureg_IF(shader, ureg_scalar(info, TGSI_SWIZZLE_X), &bi_label); + + /* + * result = tex(field.z ? tc[1] : tc[0], sampler[bkwd_pred ? 1 : 0]) + */ + ureg_IF(shader, ureg_scalar(info, TGSI_SWIZZLE_Z), &label); + ureg_TEX(shader, result, TGSI_TEXTURE_2D, ureg_src(ref[0]), sampler[1]); + ureg_fixup_label(shader, label, ureg_get_instruction_number(shader)); + ureg_ELSE(shader, &label); + ureg_TEX(shader, result, TGSI_TEXTURE_2D, ureg_src(ref[0]), sampler[0]); + ureg_fixup_label(shader, label, ureg_get_instruction_number(shader)); + ureg_ENDIF(shader); + + ureg_fixup_label(shader, bi_label, ureg_get_instruction_number(shader)); + ureg_ELSE(shader, &bi_label); + + /* + * if (field.z) + * ref[0..1] = tex(tc[0..1], sampler[0..1]) + * else + * ref[0..1] = tex(tc[2..3], sampler[0..1]) + */ + ureg_CMP(shader, ureg_writemask(ref[1], TGSI_WRITEMASK_XY), + ureg_negate(ureg_scalar(ureg_src(field), TGSI_SWIZZLE_Y)), + tc[3], tc[2]); + ureg_TEX(shader, ref[0], TGSI_TEXTURE_2D, ureg_src(ref[0]), sampler[0]); + ureg_TEX(shader, ref[1], TGSI_TEXTURE_2D, ureg_src(ref[1]), sampler[1]); + + ureg_LRP(shader, ureg_writemask(result, TGSI_WRITEMASK_XYZ), ureg_imm1f(shader, 0.5f), + ureg_src(ref[0]), ureg_src(ref[1])); + + ureg_fixup_label(shader, bi_label, ureg_get_instruction_number(shader)); + ureg_ENDIF(shader); + ureg_fixup_label(shader, intra_label, ureg_get_instruction_number(shader)); + ureg_ENDIF(shader); - assert(ti <= max_tokens); + for (i = 0; i < 2; ++i) + ureg_release_temporary(shader, ref[i]); - fs.tokens = tokens; - r->b_fs[0] = r->pipe->create_fs_state(r->pipe, &fs); - free(tokens); + return result; } -#if 0 -static void -create_field_bi_pred_frag_shader(struct vl_mpeg12_mc_renderer *r) +static void * +create_frag_shader(struct vl_mpeg12_mc_renderer *r) { - assert(false); -} -#endif + struct ureg_program *shader; + struct ureg_dst result; + struct ureg_dst field, texel; + struct ureg_dst fragment; -static void -xfer_buffers_map(struct vl_mpeg12_mc_renderer *r) -{ - unsigned i; + shader = ureg_create(TGSI_PROCESSOR_FRAGMENT); + if (!shader) + return NULL; - assert(r); + fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0); - for (i = 0; i < 3; ++i) { - r->tex_transfer[i] = r->pipe->screen->get_tex_transfer - ( - r->pipe->screen, r->textures.all[i], - 0, 0, 0, PIPE_TRANSFER_WRITE, 0, 0, - r->textures.all[i]->width0, r->textures.all[i]->height0 - ); - - r->texels[i] = r->pipe->screen->transfer_map(r->pipe->screen, r->tex_transfer[i]); - } -} + field = calc_field(shader); + texel = fetch_ycbcr(r, shader, field); -static void -xfer_buffers_unmap(struct vl_mpeg12_mc_renderer *r) -{ - unsigned i; + result = fetch_ref(shader, field); - assert(r); + ureg_ADD(shader, ureg_writemask(fragment, TGSI_WRITEMASK_XYZ), ureg_src(texel), ureg_src(result)); - for (i = 0; i < 3; ++i) { - r->pipe->screen->transfer_unmap(r->pipe->screen, r->tex_transfer[i]); - r->pipe->screen->tex_transfer_destroy(r->tex_transfer[i]); - } + ureg_release_temporary(shader, field); + ureg_release_temporary(shader, texel); + ureg_release_temporary(shader, result); + ureg_END(shader); + + return ureg_create_shader_and_destroy(shader, r->pipe); } static bool init_pipe_state(struct vl_mpeg12_mc_renderer *r) { struct pipe_sampler_state sampler; + struct pipe_rasterizer_state rs_state; unsigned filters[5]; unsigned i; assert(r); - r->viewport.scale[0] = r->pot_buffers ? - util_next_power_of_two(r->picture_width) : r->picture_width; - r->viewport.scale[1] = r->pot_buffers ? - util_next_power_of_two(r->picture_height) : r->picture_height; + r->viewport.scale[0] = r->buffer_width; + r->viewport.scale[1] = r->buffer_height; r->viewport.scale[2] = 1; r->viewport.scale[3] = 1; r->viewport.translate[0] = 0; @@ -724,23 +443,15 @@ init_pipe_state(struct vl_mpeg12_mc_renderer *r) r->viewport.translate[2] = 0; r->viewport.translate[3] = 0; - r->scissor.maxx = r->pot_buffers ? - util_next_power_of_two(r->picture_width) : r->picture_width; - r->scissor.maxy = r->pot_buffers ? - util_next_power_of_two(r->picture_height) : r->picture_height; - - r->fb_state.width = r->pot_buffers ? - util_next_power_of_two(r->picture_width) : r->picture_width; - r->fb_state.height = r->pot_buffers ? - util_next_power_of_two(r->picture_height) : r->picture_height; + r->fb_state.width = r->buffer_width; + r->fb_state.height = r->buffer_height; r->fb_state.nr_cbufs = 1; r->fb_state.zsbuf = NULL; /* Luma filter */ filters[0] = PIPE_TEX_FILTER_NEAREST; /* Chroma filters */ - if (r->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_444 || - r->eb_handling == VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE) { + if (r->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_444 || true) { //TODO filters[1] = PIPE_TEX_FILTER_NEAREST; filters[2] = PIPE_TEX_FILTER_NEAREST; } @@ -753,9 +464,10 @@ init_pipe_state(struct vl_mpeg12_mc_renderer *r) filters[4] = PIPE_TEX_FILTER_LINEAR; for (i = 0; i < 5; ++i) { + memset(&sampler, 0, sizeof(sampler)); sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE; sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE; - sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE; + sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_BORDER; sampler.min_img_filter = filters[i]; sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE; sampler.mag_img_filter = filters[i]; @@ -766,11 +478,22 @@ init_pipe_state(struct vl_mpeg12_mc_renderer *r) /*sampler.lod_bias = ; */ sampler.min_lod = 0; /*sampler.max_lod = ; */ - /*sampler.border_color[i] = ; */ + sampler.border_color[0] = 0.0f; + sampler.border_color[1] = 0.0f; + sampler.border_color[2] = 0.0f; + sampler.border_color[3] = 0.0f; /*sampler.max_anisotropy = ; */ r->samplers.all[i] = r->pipe->create_sampler_state(r->pipe, &sampler); } + memset(&rs_state, 0, sizeof(rs_state)); + /*rs_state.sprite_coord_enable */ + rs_state.sprite_coord_mode = PIPE_SPRITE_COORD_UPPER_LEFT; + rs_state.point_quad_rasterization = true; + rs_state.point_size = BLOCK_WIDTH; + rs_state.gl_rasterization_rules = true; + r->rs_state = r->pipe->create_rasterizer_state(r->pipe, &rs_state); + return true; } @@ -783,181 +506,85 @@ cleanup_pipe_state(struct vl_mpeg12_mc_renderer *r) for (i = 0; i < 5; ++i) r->pipe->delete_sampler_state(r->pipe, r->samplers.all[i]); -} - -static bool -init_shaders(struct vl_mpeg12_mc_renderer *r) -{ - assert(r); - - create_intra_vert_shader(r); - create_intra_frag_shader(r); - create_frame_pred_vert_shader(r); - create_frame_pred_frag_shader(r); - create_frame_bi_pred_vert_shader(r); - create_frame_bi_pred_frag_shader(r); - - return true; -} -static void -cleanup_shaders(struct vl_mpeg12_mc_renderer *r) -{ - assert(r); - - r->pipe->delete_vs_state(r->pipe, r->i_vs); - r->pipe->delete_fs_state(r->pipe, r->i_fs); - r->pipe->delete_vs_state(r->pipe, r->p_vs[0]); - r->pipe->delete_fs_state(r->pipe, r->p_fs[0]); - r->pipe->delete_vs_state(r->pipe, r->b_vs[0]); - r->pipe->delete_fs_state(r->pipe, r->b_fs[0]); + r->pipe->delete_rasterizer_state(r->pipe, r->rs_state); } static bool init_buffers(struct vl_mpeg12_mc_renderer *r) { - struct pipe_texture template; + struct pipe_resource *idct_matrix; + struct pipe_vertex_element vertex_elems[NUM_VS_INPUTS]; const unsigned mbw = - align(r->picture_width, MACROBLOCK_WIDTH) / MACROBLOCK_WIDTH; + align(r->buffer_width, MACROBLOCK_WIDTH) / MACROBLOCK_WIDTH; const unsigned mbh = - align(r->picture_height, MACROBLOCK_HEIGHT) / MACROBLOCK_HEIGHT; + align(r->buffer_height, MACROBLOCK_HEIGHT) / MACROBLOCK_HEIGHT; - unsigned i; + unsigned i, chroma_width, chroma_height; assert(r); r->macroblocks_per_batch = mbw * (r->bufmode == VL_MPEG12_MC_RENDERER_BUFFER_PICTURE ? mbh : 1); - r->num_macroblocks = 0; - r->macroblock_buf = MALLOC(r->macroblocks_per_batch * sizeof(struct pipe_mpeg12_macroblock)); - memset(&template, 0, sizeof(struct pipe_texture)); - template.target = PIPE_TEXTURE_2D; - /* TODO: Accomodate HW that can't do this and also for cases when this isn't precise enough */ - template.format = PIPE_FORMAT_R16_SNORM; - template.last_level = 0; - template.width0 = r->pot_buffers ? - util_next_power_of_two(r->picture_width) : r->picture_width; - template.height0 = r->pot_buffers ? - util_next_power_of_two(r->picture_height) : r->picture_height; - template.depth0 = 1; - template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER | PIPE_TEXTURE_USAGE_DYNAMIC; + if (!(idct_matrix = vl_idct_upload_matrix(r->pipe))) + return false; - r->textures.individual.y = r->pipe->screen->texture_create(r->pipe->screen, &template); + if (!vl_idct_init(&r->idct_luma, r->pipe, r->buffer_width, r->buffer_height, idct_matrix)) + return false; if (r->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) { - template.width0 = r->pot_buffers ? - util_next_power_of_two(r->picture_width / 2) : - r->picture_width / 2; - template.height0 = r->pot_buffers ? - util_next_power_of_two(r->picture_height / 2) : - r->picture_height / 2; - } - else if (r->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) - template.height0 = r->pot_buffers ? - util_next_power_of_two(r->picture_height / 2) : - r->picture_height / 2; - - r->textures.individual.cb = - r->pipe->screen->texture_create(r->pipe->screen, &template); - r->textures.individual.cr = - r->pipe->screen->texture_create(r->pipe->screen, &template); - - r->vertex_bufs.individual.ycbcr.stride = sizeof(struct vertex2f) * 4; - r->vertex_bufs.individual.ycbcr.max_index = 24 * r->macroblocks_per_batch - 1; - r->vertex_bufs.individual.ycbcr.buffer_offset = 0; - r->vertex_bufs.individual.ycbcr.buffer = pipe_buffer_create - ( - r->pipe->screen, - DEFAULT_BUF_ALIGNMENT, - PIPE_BUFFER_USAGE_VERTEX | PIPE_BUFFER_USAGE_DISCARD, - sizeof(struct vertex2f) * 4 * 24 * r->macroblocks_per_batch - ); - - for (i = 1; i < 3; ++i) { - r->vertex_bufs.all[i].stride = sizeof(struct vertex2f) * 2; - r->vertex_bufs.all[i].max_index = 24 * r->macroblocks_per_batch - 1; - r->vertex_bufs.all[i].buffer_offset = 0; - r->vertex_bufs.all[i].buffer = pipe_buffer_create - ( - r->pipe->screen, - DEFAULT_BUF_ALIGNMENT, - PIPE_BUFFER_USAGE_VERTEX | PIPE_BUFFER_USAGE_DISCARD, - sizeof(struct vertex2f) * 2 * 24 * r->macroblocks_per_batch - ); + chroma_width = r->buffer_width / 2; + chroma_height = r->buffer_height / 2; + } else if (r->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) { + chroma_width = r->buffer_width; + chroma_height = r->buffer_height / 2; + } else { + chroma_width = r->buffer_width; + chroma_height = r->buffer_height; } + if(!vl_idct_init(&r->idct_chroma, r->pipe, chroma_width, chroma_height, idct_matrix)) + return false; + + memset(&vertex_elems, 0, sizeof(vertex_elems)); + + vertex_elems[VS_I_RECT] = vl_vb_get_quad_vertex_element(); + r->quad = vl_vb_upload_quads(r->pipe, r->macroblocks_per_batch); + /* Position element */ - r->vertex_elems[0].src_offset = 0; - r->vertex_elems[0].instance_divisor = 0; - r->vertex_elems[0].vertex_buffer_index = 0; - r->vertex_elems[0].src_format = PIPE_FORMAT_R32G32_FLOAT; - - /* Luma, texcoord element */ - r->vertex_elems[1].src_offset = sizeof(struct vertex2f); - r->vertex_elems[1].instance_divisor = 0; - r->vertex_elems[1].vertex_buffer_index = 0; - r->vertex_elems[1].src_format = PIPE_FORMAT_R32G32_FLOAT; - - /* Chroma Cr texcoord element */ - r->vertex_elems[2].src_offset = sizeof(struct vertex2f) * 2; - r->vertex_elems[2].instance_divisor = 0; - r->vertex_elems[2].vertex_buffer_index = 0; - r->vertex_elems[2].src_format = PIPE_FORMAT_R32G32_FLOAT; - - /* Chroma Cb texcoord element */ - r->vertex_elems[3].src_offset = sizeof(struct vertex2f) * 3; - r->vertex_elems[3].instance_divisor = 0; - r->vertex_elems[3].vertex_buffer_index = 0; - r->vertex_elems[3].src_format = PIPE_FORMAT_R32G32_FLOAT; - - /* First ref surface top field texcoord element */ - r->vertex_elems[4].src_offset = 0; - r->vertex_elems[4].instance_divisor = 0; - r->vertex_elems[4].vertex_buffer_index = 1; - r->vertex_elems[4].src_format = PIPE_FORMAT_R32G32_FLOAT; - - /* First ref surface bottom field texcoord element */ - r->vertex_elems[5].src_offset = sizeof(struct vertex2f); - r->vertex_elems[5].instance_divisor = 0; - r->vertex_elems[5].vertex_buffer_index = 1; - r->vertex_elems[5].src_format = PIPE_FORMAT_R32G32_FLOAT; - - /* Second ref surface top field texcoord element */ - r->vertex_elems[6].src_offset = 0; - r->vertex_elems[6].instance_divisor = 0; - r->vertex_elems[6].vertex_buffer_index = 2; - r->vertex_elems[6].src_format = PIPE_FORMAT_R32G32_FLOAT; - - /* Second ref surface bottom field texcoord element */ - r->vertex_elems[7].src_offset = sizeof(struct vertex2f); - r->vertex_elems[7].instance_divisor = 0; - r->vertex_elems[7].vertex_buffer_index = 2; - r->vertex_elems[7].src_format = PIPE_FORMAT_R32G32_FLOAT; - - r->vs_const_buf = pipe_buffer_create - ( - r->pipe->screen, - DEFAULT_BUF_ALIGNMENT, - PIPE_BUFFER_USAGE_CONSTANT | PIPE_BUFFER_USAGE_DISCARD, - sizeof(struct vertex_shader_consts) - ); - - r->fs_const_buf = pipe_buffer_create - ( - r->pipe->screen, - DEFAULT_BUF_ALIGNMENT, - PIPE_BUFFER_USAGE_CONSTANT, sizeof(struct fragment_shader_consts) - ); - - memcpy - ( - pipe_buffer_map(r->pipe->screen, r->fs_const_buf, PIPE_BUFFER_USAGE_CPU_WRITE), - &fs_consts, sizeof(struct fragment_shader_consts) - ); - - pipe_buffer_unmap(r->pipe->screen, r->fs_const_buf); + vertex_elems[VS_I_VPOS].src_format = PIPE_FORMAT_R16G16_SSCALED; + + for (i = 0; i < 4; ++i) + /* motion vector 0..4 element */ + vertex_elems[VS_I_MV0 + i].src_format = PIPE_FORMAT_R16G16_SSCALED; + + /* y, cr, cb empty block element top left block */ + vertex_elems[VS_I_EB_0_0].src_format = PIPE_FORMAT_R8G8B8A8_SSCALED; + + /* y, cr, cb empty block element top right block */ + vertex_elems[VS_I_EB_0_1].src_format = PIPE_FORMAT_R8G8B8A8_SSCALED; + + /* y, cr, cb empty block element bottom left block */ + vertex_elems[VS_I_EB_1_0].src_format = PIPE_FORMAT_R8G8B8A8_SSCALED; + + /* y, cr, cb empty block element bottom right block */ + vertex_elems[VS_I_EB_1_1].src_format = PIPE_FORMAT_R8G8B8A8_SSCALED; + + r->vertex_stream_stride = vl_vb_element_helper(&vertex_elems[VS_I_VPOS], 9, 1); + + r->vertex_elems_state = r->pipe->create_vertex_elements_state( + r->pipe, NUM_VS_INPUTS, vertex_elems); + + if (r->vertex_elems_state == NULL) + return false; + + r->vs = create_vert_shader(r); + r->fs = create_frag_shader(r); + + if (r->vs == NULL || r->fs == NULL) + return false; return true; } @@ -965,160 +592,63 @@ init_buffers(struct vl_mpeg12_mc_renderer *r) static void cleanup_buffers(struct vl_mpeg12_mc_renderer *r) { - unsigned i; - assert(r); - pipe_buffer_reference(&r->vs_const_buf, NULL); - pipe_buffer_reference(&r->fs_const_buf, NULL); - - for (i = 0; i < 3; ++i) - pipe_buffer_reference(&r->vertex_bufs.all[i].buffer, NULL); + r->pipe->delete_vs_state(r->pipe, r->vs); + r->pipe->delete_fs_state(r->pipe, r->fs); - for (i = 0; i < 3; ++i) - pipe_texture_reference(&r->textures.all[i], NULL); + vl_idct_cleanup(&r->idct_luma); + vl_idct_cleanup(&r->idct_chroma); - FREE(r->macroblock_buf); + r->pipe->delete_vertex_elements_state(r->pipe, r->vertex_elems_state); } -static enum MACROBLOCK_TYPE -get_macroblock_type(struct pipe_mpeg12_macroblock *mb) +static struct pipe_sampler_view +*find_or_create_sampler_view(struct vl_mpeg12_mc_renderer *r, struct pipe_surface *surface) { - assert(mb); + struct pipe_sampler_view *sampler_view; + assert(r); + assert(surface); - switch (mb->mb_type) { - case PIPE_MPEG12_MACROBLOCK_TYPE_INTRA: - return MACROBLOCK_TYPE_INTRA; - case PIPE_MPEG12_MACROBLOCK_TYPE_FWD: - return mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME ? - MACROBLOCK_TYPE_FWD_FRAME_PRED : MACROBLOCK_TYPE_FWD_FIELD_PRED; - case PIPE_MPEG12_MACROBLOCK_TYPE_BKWD: - return mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME ? - MACROBLOCK_TYPE_BKWD_FRAME_PRED : MACROBLOCK_TYPE_BKWD_FIELD_PRED; - case PIPE_MPEG12_MACROBLOCK_TYPE_BI: - return mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME ? - MACROBLOCK_TYPE_BI_FRAME_PRED : MACROBLOCK_TYPE_BI_FIELD_PRED; - default: - assert(0); + sampler_view = (struct pipe_sampler_view*)util_keymap_lookup(r->texview_map, &surface); + if (!sampler_view) { + struct pipe_sampler_view templat; + boolean added_to_map; + + u_sampler_view_default_template(&templat, surface->texture, + surface->texture->format); + sampler_view = r->pipe->create_sampler_view(r->pipe, surface->texture, + &templat); + if (!sampler_view) + return NULL; + + added_to_map = util_keymap_insert(r->texview_map, &surface, + sampler_view, r->pipe); + assert(added_to_map); } - /* Unreachable */ - return -1; + return sampler_view; } -/* XXX: One of these days this will have to be killed with fire */ -#define SET_BLOCK(vb, cbp, mbx, mby, unitx, unity, ofsx, ofsy, hx, hy, lm, cbm, crm, use_zb, zb) \ - do { \ - (vb)[0].pos.x = (mbx) * (unitx) + (ofsx); (vb)[0].pos.y = (mby) * (unity) + (ofsy); \ - (vb)[1].pos.x = (mbx) * (unitx) + (ofsx); (vb)[1].pos.y = (mby) * (unity) + (ofsy) + (hy); \ - (vb)[2].pos.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[2].pos.y = (mby) * (unity) + (ofsy); \ - (vb)[3].pos.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[3].pos.y = (mby) * (unity) + (ofsy); \ - (vb)[4].pos.x = (mbx) * (unitx) + (ofsx); (vb)[4].pos.y = (mby) * (unity) + (ofsy) + (hy); \ - (vb)[5].pos.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[5].pos.y = (mby) * (unity) + (ofsy) + (hy); \ - \ - if (!use_zb || (cbp) & (lm)) \ - { \ - (vb)[0].luma_tc.x = (mbx) * (unitx) + (ofsx); (vb)[0].luma_tc.y = (mby) * (unity) + (ofsy); \ - (vb)[1].luma_tc.x = (mbx) * (unitx) + (ofsx); (vb)[1].luma_tc.y = (mby) * (unity) + (ofsy) + (hy); \ - (vb)[2].luma_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[2].luma_tc.y = (mby) * (unity) + (ofsy); \ - (vb)[3].luma_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[3].luma_tc.y = (mby) * (unity) + (ofsy); \ - (vb)[4].luma_tc.x = (mbx) * (unitx) + (ofsx); (vb)[4].luma_tc.y = (mby) * (unity) + (ofsy) + (hy); \ - (vb)[5].luma_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[5].luma_tc.y = (mby) * (unity) + (ofsy) + (hy); \ - } \ - else \ - { \ - (vb)[0].luma_tc.x = (zb)[0].x; (vb)[0].luma_tc.y = (zb)[0].y; \ - (vb)[1].luma_tc.x = (zb)[0].x; (vb)[1].luma_tc.y = (zb)[0].y + (hy); \ - (vb)[2].luma_tc.x = (zb)[0].x + (hx); (vb)[2].luma_tc.y = (zb)[0].y; \ - (vb)[3].luma_tc.x = (zb)[0].x + (hx); (vb)[3].luma_tc.y = (zb)[0].y; \ - (vb)[4].luma_tc.x = (zb)[0].x; (vb)[4].luma_tc.y = (zb)[0].y + (hy); \ - (vb)[5].luma_tc.x = (zb)[0].x + (hx); (vb)[5].luma_tc.y = (zb)[0].y + (hy); \ - } \ - \ - if (!use_zb || (cbp) & (cbm)) \ - { \ - (vb)[0].cb_tc.x = (mbx) * (unitx) + (ofsx); (vb)[0].cb_tc.y = (mby) * (unity) + (ofsy); \ - (vb)[1].cb_tc.x = (mbx) * (unitx) + (ofsx); (vb)[1].cb_tc.y = (mby) * (unity) + (ofsy) + (hy); \ - (vb)[2].cb_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[2].cb_tc.y = (mby) * (unity) + (ofsy); \ - (vb)[3].cb_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[3].cb_tc.y = (mby) * (unity) + (ofsy); \ - (vb)[4].cb_tc.x = (mbx) * (unitx) + (ofsx); (vb)[4].cb_tc.y = (mby) * (unity) + (ofsy) + (hy); \ - (vb)[5].cb_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[5].cb_tc.y = (mby) * (unity) + (ofsy) + (hy); \ - } \ - else \ - { \ - (vb)[0].cb_tc.x = (zb)[1].x; (vb)[0].cb_tc.y = (zb)[1].y; \ - (vb)[1].cb_tc.x = (zb)[1].x; (vb)[1].cb_tc.y = (zb)[1].y + (hy); \ - (vb)[2].cb_tc.x = (zb)[1].x + (hx); (vb)[2].cb_tc.y = (zb)[1].y; \ - (vb)[3].cb_tc.x = (zb)[1].x + (hx); (vb)[3].cb_tc.y = (zb)[1].y; \ - (vb)[4].cb_tc.x = (zb)[1].x; (vb)[4].cb_tc.y = (zb)[1].y + (hy); \ - (vb)[5].cb_tc.x = (zb)[1].x + (hx); (vb)[5].cb_tc.y = (zb)[1].y + (hy); \ - } \ - \ - if (!use_zb || (cbp) & (crm)) \ - { \ - (vb)[0].cr_tc.x = (mbx) * (unitx) + (ofsx); (vb)[0].cr_tc.y = (mby) * (unity) + (ofsy); \ - (vb)[1].cr_tc.x = (mbx) * (unitx) + (ofsx); (vb)[1].cr_tc.y = (mby) * (unity) + (ofsy) + (hy); \ - (vb)[2].cr_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[2].cr_tc.y = (mby) * (unity) + (ofsy); \ - (vb)[3].cr_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[3].cr_tc.y = (mby) * (unity) + (ofsy); \ - (vb)[4].cr_tc.x = (mbx) * (unitx) + (ofsx); (vb)[4].cr_tc.y = (mby) * (unity) + (ofsy) + (hy); \ - (vb)[5].cr_tc.x = (mbx) * (unitx) + (ofsx) + (hx); (vb)[5].cr_tc.y = (mby) * (unity) + (ofsy) + (hy); \ - } \ - else \ - { \ - (vb)[0].cr_tc.x = (zb)[2].x; (vb)[0].cr_tc.y = (zb)[2].y; \ - (vb)[1].cr_tc.x = (zb)[2].x; (vb)[1].cr_tc.y = (zb)[2].y + (hy); \ - (vb)[2].cr_tc.x = (zb)[2].x + (hx); (vb)[2].cr_tc.y = (zb)[2].y; \ - (vb)[3].cr_tc.x = (zb)[2].x + (hx); (vb)[3].cr_tc.y = (zb)[2].y; \ - (vb)[4].cr_tc.x = (zb)[2].x; (vb)[4].cr_tc.y = (zb)[2].y + (hy); \ - (vb)[5].cr_tc.x = (zb)[2].x + (hx); (vb)[5].cr_tc.y = (zb)[2].y + (hy); \ - } \ - } while (0) - static void -gen_macroblock_verts(struct vl_mpeg12_mc_renderer *r, - struct pipe_mpeg12_macroblock *mb, unsigned pos, - struct vert_stream_0 *ycbcr_vb, struct vertex2f **ref_vb) +get_motion_vectors(struct pipe_mpeg12_macroblock *mb, struct vertex2s mv[4]) { - struct vertex2f mo_vec[2]; - - unsigned i; - - assert(r); - assert(mb); - assert(ycbcr_vb); - assert(pos < r->macroblocks_per_batch); - - mo_vec[1].x = 0; - mo_vec[1].y = 0; - switch (mb->mb_type) { case PIPE_MPEG12_MACROBLOCK_TYPE_BI: { - struct vertex2f *vb; - - assert(ref_vb && ref_vb[1]); + if (mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME) { + mv[2].x = mb->pmv[0][1][0]; + mv[2].y = mb->pmv[0][1][1]; - vb = ref_vb[1] + pos * 2 * 24; + } else { + mv[2].x = mb->pmv[0][1][0]; + mv[2].y = mb->pmv[0][1][1] - (mb->pmv[0][1][1] % 4); - mo_vec[0].x = mb->pmv[0][1][0] * 0.5f * r->surface_tex_inv_size.x; - mo_vec[0].y = mb->pmv[0][1][1] * 0.5f * r->surface_tex_inv_size.y; + mv[3].x = mb->pmv[1][1][0]; + mv[3].y = mb->pmv[1][1][1] - (mb->pmv[1][1][1] % 4); - if (mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME) { - for (i = 0; i < 24 * 2; i += 2) { - vb[i].x = mo_vec[0].x; - vb[i].y = mo_vec[0].y; - } - } - else { - mo_vec[1].x = mb->pmv[1][1][0] * 0.5f * r->surface_tex_inv_size.x; - mo_vec[1].y = mb->pmv[1][1][1] * 0.5f * r->surface_tex_inv_size.y; - - for (i = 0; i < 24 * 2; i += 2) { - vb[i].x = mo_vec[0].x; - vb[i].y = mo_vec[0].y; - vb[i + 1].x = mo_vec[1].x; - vb[i + 1].y = mo_vec[1].y; - } + if(mb->mvfs[0][1]) mv[2].y += 2; + if(!mb->mvfs[1][1]) mv[3].y -= 2; } /* fall-through */ @@ -1126,362 +656,109 @@ gen_macroblock_verts(struct vl_mpeg12_mc_renderer *r, case PIPE_MPEG12_MACROBLOCK_TYPE_FWD: case PIPE_MPEG12_MACROBLOCK_TYPE_BKWD: { - struct vertex2f *vb; - - assert(ref_vb && ref_vb[0]); + if (mb->mb_type == PIPE_MPEG12_MACROBLOCK_TYPE_BKWD) { - vb = ref_vb[0] + pos * 2 * 24; + if (mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME) { + mv[0].x = mb->pmv[0][1][0]; + mv[0].y = mb->pmv[0][1][1]; - if (mb->mb_type == PIPE_MPEG12_MACROBLOCK_TYPE_BKWD) { - mo_vec[0].x = mb->pmv[0][1][0] * 0.5f * r->surface_tex_inv_size.x; - mo_vec[0].y = mb->pmv[0][1][1] * 0.5f * r->surface_tex_inv_size.y; + } else { + mv[0].x = mb->pmv[0][1][0]; + mv[0].y = mb->pmv[0][1][1] - (mb->pmv[0][1][1] % 4); - if (mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FIELD) { - mo_vec[1].x = mb->pmv[1][1][0] * 0.5f * r->surface_tex_inv_size.x; - mo_vec[1].y = mb->pmv[1][1][1] * 0.5f * r->surface_tex_inv_size.y; - } - } - else { - mo_vec[0].x = mb->pmv[0][0][0] * 0.5f * r->surface_tex_inv_size.x; - mo_vec[0].y = mb->pmv[0][0][1] * 0.5f * r->surface_tex_inv_size.y; + mv[1].x = mb->pmv[1][1][0]; + mv[1].y = mb->pmv[1][1][1] - (mb->pmv[1][1][1] % 4); - if (mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FIELD) { - mo_vec[1].x = mb->pmv[1][0][0] * 0.5f * r->surface_tex_inv_size.x; - mo_vec[1].y = mb->pmv[1][0][1] * 0.5f * r->surface_tex_inv_size.y; + if(mb->mvfs[0][1]) mv[0].y += 2; + if(!mb->mvfs[1][1]) mv[1].y -= 2; } - } - if (mb->mb_type == PIPE_MPEG12_MOTION_TYPE_FRAME) { - for (i = 0; i < 24 * 2; i += 2) { - vb[i].x = mo_vec[0].x; - vb[i].y = mo_vec[0].y; - } - } - else { - for (i = 0; i < 24 * 2; i += 2) { - vb[i].x = mo_vec[0].x; - vb[i].y = mo_vec[0].y; - vb[i + 1].x = mo_vec[1].x; - vb[i + 1].y = mo_vec[1].y; - } - } + } else { - /* fall-through */ - } - case PIPE_MPEG12_MACROBLOCK_TYPE_INTRA: - { - const struct vertex2f unit = - { - r->surface_tex_inv_size.x * MACROBLOCK_WIDTH, - r->surface_tex_inv_size.y * MACROBLOCK_HEIGHT - }; - const struct vertex2f half = - { - r->surface_tex_inv_size.x * (MACROBLOCK_WIDTH / 2), - r->surface_tex_inv_size.y * (MACROBLOCK_HEIGHT / 2) - }; - const bool use_zb = r->eb_handling == VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE; - - struct vert_stream_0 *vb = ycbcr_vb + pos * 24; - - SET_BLOCK(vb, mb->cbp, mb->mbx, mb->mby, - unit.x, unit.y, 0, 0, half.x, half.y, - 32, 2, 1, use_zb, r->zero_block); - - SET_BLOCK(vb + 6, mb->cbp, mb->mbx, mb->mby, - unit.x, unit.y, half.x, 0, half.x, half.y, - 16, 2, 1, use_zb, r->zero_block); - - SET_BLOCK(vb + 12, mb->cbp, mb->mbx, mb->mby, - unit.x, unit.y, 0, half.y, half.x, half.y, - 8, 2, 1, use_zb, r->zero_block); - - SET_BLOCK(vb + 18, mb->cbp, mb->mbx, mb->mby, - unit.x, unit.y, half.x, half.y, half.x, half.y, - 4, 2, 1, use_zb, r->zero_block); + if (mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME) { + mv[0].x = mb->pmv[0][0][0]; + mv[0].y = mb->pmv[0][0][1]; - break; + } else { + mv[0].x = mb->pmv[0][0][0]; + mv[0].y = mb->pmv[0][0][1] - (mb->pmv[0][0][1] % 4); + + mv[1].x = mb->pmv[1][0][0]; + mv[1].y = mb->pmv[1][0][1] - (mb->pmv[1][0][1] % 4); + + if(mb->mvfs[0][0]) mv[0].y += 2; + if(!mb->mvfs[1][0]) mv[1].y -= 2; + } + } } default: - assert(0); + break; } } static void -gen_macroblock_stream(struct vl_mpeg12_mc_renderer *r, - unsigned *num_macroblocks) +grab_vectors(struct vl_mpeg12_mc_renderer *r, + struct vl_mpeg12_mc_buffer *buffer, + struct pipe_mpeg12_macroblock *mb) { - unsigned offset[NUM_MACROBLOCK_TYPES]; - struct vert_stream_0 *ycbcr_vb; - struct vertex2f *ref_vb[2]; - unsigned i; - - assert(r); - assert(num_macroblocks); - - for (i = 0; i < r->num_macroblocks; ++i) { - enum MACROBLOCK_TYPE mb_type = get_macroblock_type(&r->macroblock_buf[i]); - ++num_macroblocks[mb_type]; - } - - offset[0] = 0; - - for (i = 1; i < NUM_MACROBLOCK_TYPES; ++i) - offset[i] = offset[i - 1] + num_macroblocks[i - 1]; + struct vertex_stream stream; - ycbcr_vb = (struct vert_stream_0 *)pipe_buffer_map - ( - r->pipe->screen, - r->vertex_bufs.individual.ycbcr.buffer, - PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_DISCARD - ); - - for (i = 0; i < 2; ++i) - ref_vb[i] = (struct vertex2f *)pipe_buffer_map - ( - r->pipe->screen, - r->vertex_bufs.individual.ref[i].buffer, - PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_DISCARD - ); - - for (i = 0; i < r->num_macroblocks; ++i) { - enum MACROBLOCK_TYPE mb_type = get_macroblock_type(&r->macroblock_buf[i]); - - gen_macroblock_verts(r, &r->macroblock_buf[i], offset[mb_type], - ycbcr_vb, ref_vb); - - ++offset[mb_type]; - } - - pipe_buffer_unmap(r->pipe->screen, r->vertex_bufs.individual.ycbcr.buffer); - for (i = 0; i < 2; ++i) - pipe_buffer_unmap(r->pipe->screen, r->vertex_bufs.individual.ref[i].buffer); -} - -static void -flush(struct vl_mpeg12_mc_renderer *r) -{ - unsigned num_macroblocks[NUM_MACROBLOCK_TYPES] = { 0 }; - unsigned vb_start = 0; - struct vertex_shader_consts *vs_consts; - unsigned i; + unsigned i, j; assert(r); - assert(r->num_macroblocks == r->macroblocks_per_batch); - - gen_macroblock_stream(r, num_macroblocks); - - r->fb_state.cbufs[0] = r->pipe->screen->get_tex_surface - ( - r->pipe->screen, r->surface, - 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE - ); - - r->pipe->set_framebuffer_state(r->pipe, &r->fb_state); - r->pipe->set_viewport_state(r->pipe, &r->viewport); - r->pipe->set_scissor_state(r->pipe, &r->scissor); - - vs_consts = pipe_buffer_map - ( - r->pipe->screen, r->vs_const_buf, - PIPE_BUFFER_USAGE_CPU_WRITE | PIPE_BUFFER_USAGE_DISCARD - ); - - vs_consts->denorm.x = r->surface->width0; - vs_consts->denorm.y = r->surface->height0; - - pipe_buffer_unmap(r->pipe->screen, r->vs_const_buf); - - r->pipe->set_constant_buffer(r->pipe, PIPE_SHADER_VERTEX, 0, - r->vs_const_buf); - r->pipe->set_constant_buffer(r->pipe, PIPE_SHADER_FRAGMENT, 0, - r->fs_const_buf); - - if (num_macroblocks[MACROBLOCK_TYPE_INTRA] > 0) { - r->pipe->set_vertex_buffers(r->pipe, 1, r->vertex_bufs.all); - r->pipe->set_vertex_elements(r->pipe, 4, r->vertex_elems); - r->pipe->set_fragment_sampler_textures(r->pipe, 3, r->textures.all); - r->pipe->bind_fragment_sampler_states(r->pipe, 3, r->samplers.all); - r->pipe->bind_vs_state(r->pipe, r->i_vs); - r->pipe->bind_fs_state(r->pipe, r->i_fs); - - r->pipe->draw_arrays(r->pipe, PIPE_PRIM_TRIANGLES, vb_start, - num_macroblocks[MACROBLOCK_TYPE_INTRA] * 24); - vb_start += num_macroblocks[MACROBLOCK_TYPE_INTRA] * 24; - } - - if (num_macroblocks[MACROBLOCK_TYPE_FWD_FRAME_PRED] > 0) { - r->pipe->set_vertex_buffers(r->pipe, 2, r->vertex_bufs.all); - r->pipe->set_vertex_elements(r->pipe, 6, r->vertex_elems); - r->textures.individual.ref[0] = r->past; - r->pipe->set_fragment_sampler_textures(r->pipe, 4, r->textures.all); - r->pipe->bind_fragment_sampler_states(r->pipe, 4, r->samplers.all); - r->pipe->bind_vs_state(r->pipe, r->p_vs[0]); - r->pipe->bind_fs_state(r->pipe, r->p_fs[0]); - - r->pipe->draw_arrays(r->pipe, PIPE_PRIM_TRIANGLES, vb_start, - num_macroblocks[MACROBLOCK_TYPE_FWD_FRAME_PRED] * 24); - vb_start += num_macroblocks[MACROBLOCK_TYPE_FWD_FRAME_PRED] * 24; - } - - if (false /*num_macroblocks[MACROBLOCK_TYPE_FWD_FIELD_PRED] > 0 */ ) { - r->pipe->set_vertex_buffers(r->pipe, 2, r->vertex_bufs.all); - r->pipe->set_vertex_elements(r->pipe, 6, r->vertex_elems); - r->textures.individual.ref[0] = r->past; - r->pipe->set_fragment_sampler_textures(r->pipe, 4, r->textures.all); - r->pipe->bind_fragment_sampler_states(r->pipe, 4, r->samplers.all); - r->pipe->bind_vs_state(r->pipe, r->p_vs[1]); - r->pipe->bind_fs_state(r->pipe, r->p_fs[1]); - - r->pipe->draw_arrays(r->pipe, PIPE_PRIM_TRIANGLES, vb_start, - num_macroblocks[MACROBLOCK_TYPE_FWD_FIELD_PRED] * 24); - vb_start += num_macroblocks[MACROBLOCK_TYPE_FWD_FIELD_PRED] * 24; - } + assert(mb); - if (num_macroblocks[MACROBLOCK_TYPE_BKWD_FRAME_PRED] > 0) { - r->pipe->set_vertex_buffers(r->pipe, 2, r->vertex_bufs.all); - r->pipe->set_vertex_elements(r->pipe, 6, r->vertex_elems); - r->textures.individual.ref[0] = r->future; - r->pipe->set_fragment_sampler_textures(r->pipe, 4, r->textures.all); - r->pipe->bind_fragment_sampler_states(r->pipe, 4, r->samplers.all); - r->pipe->bind_vs_state(r->pipe, r->p_vs[0]); - r->pipe->bind_fs_state(r->pipe, r->p_fs[0]); - - r->pipe->draw_arrays(r->pipe, PIPE_PRIM_TRIANGLES, vb_start, - num_macroblocks[MACROBLOCK_TYPE_BKWD_FRAME_PRED] * 24); - vb_start += num_macroblocks[MACROBLOCK_TYPE_BKWD_FRAME_PRED] * 24; + stream.pos.x = mb->mbx; + stream.pos.y = mb->mby; + for ( i = 0; i < 2; ++i) { + for ( j = 0; j < 2; ++j) { + stream.eb[i][j].y = !(mb->cbp & (*r->empty_block_mask)[0][i][j]); + stream.eb[i][j].cr = !(mb->cbp & (*r->empty_block_mask)[1][i][j]); + stream.eb[i][j].cb = !(mb->cbp & (*r->empty_block_mask)[2][i][j]); + } } + stream.eb[0][0].flag = mb->dct_type == PIPE_MPEG12_DCT_TYPE_FIELD; + stream.eb[0][1].flag = mb->mo_type == PIPE_MPEG12_MOTION_TYPE_FRAME; + stream.eb[1][0].flag = mb->mb_type == PIPE_MPEG12_MACROBLOCK_TYPE_BKWD; + switch (mb->mb_type) { + case PIPE_MPEG12_MACROBLOCK_TYPE_INTRA: + stream.eb[1][1].flag = -1; + break; - if (false /*num_macroblocks[MACROBLOCK_TYPE_BKWD_FIELD_PRED] > 0 */ ) { - r->pipe->set_vertex_buffers(r->pipe, 2, r->vertex_bufs.all); - r->pipe->set_vertex_elements(r->pipe, 6, r->vertex_elems); - r->textures.individual.ref[0] = r->future; - r->pipe->set_fragment_sampler_textures(r->pipe, 4, r->textures.all); - r->pipe->bind_fragment_sampler_states(r->pipe, 4, r->samplers.all); - r->pipe->bind_vs_state(r->pipe, r->p_vs[1]); - r->pipe->bind_fs_state(r->pipe, r->p_fs[1]); - - r->pipe->draw_arrays(r->pipe, PIPE_PRIM_TRIANGLES, vb_start, - num_macroblocks[MACROBLOCK_TYPE_BKWD_FIELD_PRED] * 24); - vb_start += num_macroblocks[MACROBLOCK_TYPE_BKWD_FIELD_PRED] * 24; - } + case PIPE_MPEG12_MACROBLOCK_TYPE_FWD: + case PIPE_MPEG12_MACROBLOCK_TYPE_BKWD: + stream.eb[1][1].flag = 1; + break; - if (num_macroblocks[MACROBLOCK_TYPE_BI_FRAME_PRED] > 0) { - r->pipe->set_vertex_buffers(r->pipe, 3, r->vertex_bufs.all); - r->pipe->set_vertex_elements(r->pipe, 8, r->vertex_elems); - r->textures.individual.ref[0] = r->past; - r->textures.individual.ref[1] = r->future; - r->pipe->set_fragment_sampler_textures(r->pipe, 5, r->textures.all); - r->pipe->bind_fragment_sampler_states(r->pipe, 5, r->samplers.all); - r->pipe->bind_vs_state(r->pipe, r->b_vs[0]); - r->pipe->bind_fs_state(r->pipe, r->b_fs[0]); - - r->pipe->draw_arrays(r->pipe, PIPE_PRIM_TRIANGLES, vb_start, - num_macroblocks[MACROBLOCK_TYPE_BI_FRAME_PRED] * 24); - vb_start += num_macroblocks[MACROBLOCK_TYPE_BI_FRAME_PRED] * 24; - } + case PIPE_MPEG12_MACROBLOCK_TYPE_BI: + stream.eb[1][1].flag = 0; + break; - if (false /*num_macroblocks[MACROBLOCK_TYPE_BI_FIELD_PRED] > 0 */ ) { - r->pipe->set_vertex_buffers(r->pipe, 3, r->vertex_bufs.all); - r->pipe->set_vertex_elements(r->pipe, 8, r->vertex_elems); - r->textures.individual.ref[0] = r->past; - r->textures.individual.ref[1] = r->future; - r->pipe->set_fragment_sampler_textures(r->pipe, 5, r->textures.all); - r->pipe->bind_fragment_sampler_states(r->pipe, 5, r->samplers.all); - r->pipe->bind_vs_state(r->pipe, r->b_vs[1]); - r->pipe->bind_fs_state(r->pipe, r->b_fs[1]); - - r->pipe->draw_arrays(r->pipe, PIPE_PRIM_TRIANGLES, vb_start, - num_macroblocks[MACROBLOCK_TYPE_BI_FIELD_PRED] * 24); - vb_start += num_macroblocks[MACROBLOCK_TYPE_BI_FIELD_PRED] * 24; + default: + assert(0); } - r->pipe->flush(r->pipe, PIPE_FLUSH_RENDER_CACHE, r->fence); - pipe_surface_reference(&r->fb_state.cbufs[0], NULL); - - if (r->eb_handling == VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE) - for (i = 0; i < 3; ++i) - r->zero_block[i].x = ZERO_BLOCK_NIL; - - r->num_macroblocks = 0; -} - -static void -grab_frame_coded_block(short *src, short *dst, unsigned dst_pitch) -{ - unsigned y; - - assert(src); - assert(dst); - - for (y = 0; y < BLOCK_HEIGHT; ++y) - memcpy(dst + y * dst_pitch, src + y * BLOCK_WIDTH, BLOCK_WIDTH * 2); -} - -static void -grab_field_coded_block(short *src, short *dst, unsigned dst_pitch) -{ - unsigned y; - - assert(src); - assert(dst); - - for (y = 0; y < BLOCK_HEIGHT; ++y) - memcpy(dst + y * dst_pitch * 2, src + y * BLOCK_WIDTH, BLOCK_WIDTH * 2); -} - -static void -fill_zero_block(short *dst, unsigned dst_pitch) -{ - unsigned y; - - assert(dst); - - for (y = 0; y < BLOCK_HEIGHT; ++y) - memset(dst + y * dst_pitch, 0, BLOCK_WIDTH * 2); + get_motion_vectors(mb, stream.mv); + vl_vb_add_block(&buffer->vertex_stream, &stream); } static void -grab_blocks(struct vl_mpeg12_mc_renderer *r, unsigned mbx, unsigned mby, - enum pipe_mpeg12_dct_type dct_type, unsigned cbp, short *blocks) +grab_blocks(struct vl_mpeg12_mc_renderer *r, + struct vl_mpeg12_mc_buffer *buffer, + unsigned mbx, unsigned mby, + unsigned cbp, short *blocks) { - unsigned tex_pitch; - short *texels; - unsigned tb = 0, sb = 0; - unsigned mbpx = mbx * MACROBLOCK_WIDTH, mbpy = mby * MACROBLOCK_HEIGHT; + unsigned tb = 0; unsigned x, y; assert(r); assert(blocks); - tex_pitch = r->tex_transfer[0]->stride / util_format_get_blocksize(r->tex_transfer[0]->texture->format); - texels = r->texels[0] + mbpy * tex_pitch + mbpx; - for (y = 0; y < 2; ++y) { for (x = 0; x < 2; ++x, ++tb) { - if ((cbp >> (5 - tb)) & 1) { - if (dct_type == PIPE_MPEG12_DCT_TYPE_FRAME) { - grab_frame_coded_block(blocks + sb * BLOCK_WIDTH * BLOCK_HEIGHT, - texels + y * tex_pitch * BLOCK_WIDTH + - x * BLOCK_WIDTH, tex_pitch); - } - else { - grab_field_coded_block(blocks + sb * BLOCK_WIDTH * BLOCK_HEIGHT, - texels + y * tex_pitch + x * BLOCK_WIDTH, - tex_pitch); - } - - ++sb; - } - else if (r->eb_handling != VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_NONE) { - if (r->eb_handling == VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ALL || - ZERO_BLOCK_IS_NIL(r->zero_block[0])) { - fill_zero_block(texels + y * tex_pitch * BLOCK_WIDTH + x * BLOCK_WIDTH, tex_pitch); - if (r->eb_handling == VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE) { - r->zero_block[0].x = (mbpx + x * 8) * r->surface_tex_inv_size.x; - r->zero_block[0].y = (mbpy + y * 8) * r->surface_tex_inv_size.y; - } - } + if (cbp & (*r->empty_block_mask)[0][y][x]) { + vl_idct_add_block(&buffer->idct_y, mbx * 2 + x, mby * 2 + y, blocks); + blocks += BLOCK_WIDTH * BLOCK_HEIGHT; } } } @@ -1489,176 +766,328 @@ grab_blocks(struct vl_mpeg12_mc_renderer *r, unsigned mbx, unsigned mby, /* TODO: Implement 422, 444 */ assert(r->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420); - mbpx /= 2; - mbpy /= 2; - - for (tb = 0; tb < 2; ++tb) { - tex_pitch = r->tex_transfer[tb + 1]->stride / util_format_get_blocksize(r->tex_transfer[tb + 1]->texture->format); - texels = r->texels[tb + 1] + mbpy * tex_pitch + mbpx; - - if ((cbp >> (1 - tb)) & 1) { - grab_frame_coded_block(blocks + sb * BLOCK_WIDTH * BLOCK_HEIGHT, texels, tex_pitch); - ++sb; - } - else if (r->eb_handling != VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_NONE) { - if (r->eb_handling == VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ALL || - ZERO_BLOCK_IS_NIL(r->zero_block[tb + 1])) { - fill_zero_block(texels, tex_pitch); - if (r->eb_handling == VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_ONE) { - r->zero_block[tb + 1].x = (mbpx << 1) * r->surface_tex_inv_size.x; - r->zero_block[tb + 1].y = (mbpy << 1) * r->surface_tex_inv_size.y; - } - } + for (tb = 1; tb < 3; ++tb) { + if (cbp & (*r->empty_block_mask)[tb][0][0]) { + if(tb == 1) + vl_idct_add_block(&buffer->idct_cb, mbx, mby, blocks); + else + vl_idct_add_block(&buffer->idct_cr, mbx, mby, blocks); + blocks += BLOCK_WIDTH * BLOCK_HEIGHT; } } } static void grab_macroblock(struct vl_mpeg12_mc_renderer *r, + struct vl_mpeg12_mc_buffer *buffer, struct pipe_mpeg12_macroblock *mb) { assert(r); assert(mb); - assert(r->num_macroblocks < r->macroblocks_per_batch); + assert(mb->blocks); + assert(buffer->num_macroblocks < r->macroblocks_per_batch); - memcpy(&r->macroblock_buf[r->num_macroblocks], mb, - sizeof(struct pipe_mpeg12_macroblock)); + grab_vectors(r, buffer, mb); + grab_blocks(r, buffer, mb->mbx, mb->mby, mb->cbp, mb->blocks); - grab_blocks(r, mb->mbx, mb->mby, mb->dct_type, mb->cbp, mb->blocks); + ++buffer->num_macroblocks; +} + +static void +texview_map_delete(const struct keymap *map, + const void *key, void *data, + void *user) +{ + struct pipe_sampler_view *sv = (struct pipe_sampler_view*)data; - ++r->num_macroblocks; + assert(map); + assert(key); + assert(data); + assert(user); + + pipe_sampler_view_reference(&sv, NULL); } bool vl_mpeg12_mc_renderer_init(struct vl_mpeg12_mc_renderer *renderer, struct pipe_context *pipe, - unsigned picture_width, - unsigned picture_height, + unsigned buffer_width, + unsigned buffer_height, enum pipe_video_chroma_format chroma_format, - enum VL_MPEG12_MC_RENDERER_BUFFER_MODE bufmode, - enum VL_MPEG12_MC_RENDERER_EMPTY_BLOCK eb_handling, - bool pot_buffers) + enum VL_MPEG12_MC_RENDERER_BUFFER_MODE bufmode) { - unsigned i; - assert(renderer); assert(pipe); + /* TODO: Implement other policies */ assert(bufmode == VL_MPEG12_MC_RENDERER_BUFFER_PICTURE); - /* TODO: Implement this */ - /* XXX: XFER_ALL sampling issue at block edges when using bilinear filtering */ - assert(eb_handling != VL_MPEG12_MC_RENDERER_EMPTY_BLOCK_XFER_NONE); - /* TODO: Non-pot buffers untested, probably doesn't work without changes to texcoord generation, vert shader, etc */ - assert(pot_buffers); memset(renderer, 0, sizeof(struct vl_mpeg12_mc_renderer)); renderer->pipe = pipe; - renderer->picture_width = picture_width; - renderer->picture_height = picture_height; + renderer->buffer_width = buffer_width; + renderer->buffer_height = buffer_height; renderer->chroma_format = chroma_format; renderer->bufmode = bufmode; - renderer->eb_handling = eb_handling; - renderer->pot_buffers = pot_buffers; - if (!init_pipe_state(renderer)) + /* TODO: Implement 422, 444 */ + assert(chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420); + renderer->empty_block_mask = &const_empty_block_mask_420; + + renderer->texview_map = util_new_keymap(sizeof(struct pipe_surface*), -1, + texview_map_delete); + if (!renderer->texview_map) return false; - if (!init_shaders(renderer)) { - cleanup_pipe_state(renderer); + + if (!init_pipe_state(renderer)) + goto error_pipe_state; + + if (!init_buffers(renderer)) + goto error_buffers; + + return true; + +error_buffers: + cleanup_pipe_state(renderer); + +error_pipe_state: + util_delete_keymap(renderer->texview_map, renderer->pipe); + return false; +} + +void +vl_mpeg12_mc_renderer_cleanup(struct vl_mpeg12_mc_renderer *renderer) +{ + assert(renderer); + + util_delete_keymap(renderer->texview_map, renderer->pipe); + cleanup_pipe_state(renderer); + cleanup_buffers(renderer); +} + +bool +vl_mpeg12_mc_init_buffer(struct vl_mpeg12_mc_renderer *renderer, struct vl_mpeg12_mc_buffer *buffer) +{ + struct pipe_resource template; + struct pipe_sampler_view sampler_view; + + unsigned i; + + assert(renderer && buffer); + + buffer->surface = NULL; + buffer->past = NULL; + buffer->future = NULL; + buffer->num_macroblocks = 0; + + memset(&template, 0, sizeof(struct pipe_resource)); + template.target = PIPE_TEXTURE_2D; + /* TODO: Accomodate HW that can't do this and also for cases when this isn't precise enough */ + template.format = PIPE_FORMAT_R16_SNORM; + template.last_level = 0; + template.width0 = renderer->buffer_width; + template.height0 = renderer->buffer_height; + template.depth0 = 1; + template.usage = PIPE_USAGE_STATIC; + template.bind = PIPE_BIND_SAMPLER_VIEW; + template.flags = 0; + + buffer->textures.individual.y = renderer->pipe->screen->resource_create(renderer->pipe->screen, &template); + + if (!vl_idct_init_buffer(&renderer->idct_luma, &buffer->idct_y, buffer->textures.individual.y)) return false; + + if (renderer->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_420) { + template.width0 = renderer->buffer_width / 2; + template.height0 = renderer->buffer_height / 2; } - if (!init_buffers(renderer)) { - cleanup_shaders(renderer); - cleanup_pipe_state(renderer); + else if (renderer->chroma_format == PIPE_VIDEO_CHROMA_FORMAT_422) + template.height0 = renderer->buffer_height / 2; + + buffer->textures.individual.cb = + renderer->pipe->screen->resource_create(renderer->pipe->screen, &template); + buffer->textures.individual.cr = + renderer->pipe->screen->resource_create(renderer->pipe->screen, &template); + + if (!vl_idct_init_buffer(&renderer->idct_chroma, &buffer->idct_cb, buffer->textures.individual.cb)) + return false; + + if (!vl_idct_init_buffer(&renderer->idct_chroma, &buffer->idct_cr, buffer->textures.individual.cr)) return false; + + for (i = 0; i < 3; ++i) { + u_sampler_view_default_template(&sampler_view, + buffer->textures.all[i], + buffer->textures.all[i]->format); + sampler_view.swizzle_r = i == 0 ? PIPE_SWIZZLE_RED : PIPE_SWIZZLE_ZERO; + sampler_view.swizzle_g = i == 1 ? PIPE_SWIZZLE_RED : PIPE_SWIZZLE_ZERO; + sampler_view.swizzle_b = i == 2 ? PIPE_SWIZZLE_RED : PIPE_SWIZZLE_ZERO; + sampler_view.swizzle_a = PIPE_SWIZZLE_ONE; + buffer->sampler_views.all[i] = renderer->pipe->create_sampler_view( + renderer->pipe, buffer->textures.all[i], &sampler_view); } - renderer->surface = NULL; - renderer->past = NULL; - renderer->future = NULL; - for (i = 0; i < 3; ++i) - renderer->zero_block[i].x = ZERO_BLOCK_NIL; - renderer->num_macroblocks = 0; + buffer->vertex_bufs.individual.quad.stride = renderer->quad.stride; + buffer->vertex_bufs.individual.quad.max_index = renderer->quad.max_index; + buffer->vertex_bufs.individual.quad.buffer_offset = renderer->quad.buffer_offset; + pipe_resource_reference(&buffer->vertex_bufs.individual.quad.buffer, renderer->quad.buffer); - xfer_buffers_map(renderer); + buffer->vertex_bufs.individual.stream = vl_vb_init( + &buffer->vertex_stream, renderer->pipe, renderer->macroblocks_per_batch, + renderer->vertex_stream_stride); return true; } void -vl_mpeg12_mc_renderer_cleanup(struct vl_mpeg12_mc_renderer *renderer) +vl_mpeg12_mc_cleanup_buffer(struct vl_mpeg12_mc_renderer *renderer, struct vl_mpeg12_mc_buffer *buffer) { - assert(renderer); + unsigned i; - xfer_buffers_unmap(renderer); + assert(renderer && buffer); - cleanup_pipe_state(renderer); - cleanup_shaders(renderer); - cleanup_buffers(renderer); + for (i = 0; i < 3; ++i) { + pipe_sampler_view_reference(&buffer->sampler_views.all[i], NULL); + pipe_resource_reference(&buffer->vertex_bufs.all[i].buffer, NULL); + pipe_resource_reference(&buffer->textures.all[i], NULL); + } + + pipe_resource_reference(&buffer->vertex_bufs.individual.quad.buffer, NULL); + vl_vb_cleanup(&buffer->vertex_stream); + + vl_idct_cleanup_buffer(&renderer->idct_luma, &buffer->idct_y); + vl_idct_cleanup_buffer(&renderer->idct_chroma, &buffer->idct_cb); + vl_idct_cleanup_buffer(&renderer->idct_chroma, &buffer->idct_cr); + + pipe_surface_reference(&buffer->surface, NULL); + pipe_surface_reference(&buffer->past, NULL); + pipe_surface_reference(&buffer->future, NULL); +} + +void +vl_mpeg12_mc_map_buffer(struct vl_mpeg12_mc_renderer *renderer, struct vl_mpeg12_mc_buffer *buffer) +{ + assert(renderer && buffer); + + vl_idct_map_buffers(&renderer->idct_luma, &buffer->idct_y); + vl_idct_map_buffers(&renderer->idct_chroma, &buffer->idct_cr); + vl_idct_map_buffers(&renderer->idct_chroma, &buffer->idct_cb); + + vl_vb_map(&buffer->vertex_stream, renderer->pipe); } void -vl_mpeg12_mc_renderer_render_macroblocks(struct vl_mpeg12_mc_renderer - *renderer, - struct pipe_texture *surface, - struct pipe_texture *past, - struct pipe_texture *future, +vl_mpeg12_mc_renderer_render_macroblocks(struct vl_mpeg12_mc_renderer *renderer, + struct vl_mpeg12_mc_buffer *buffer, + struct pipe_surface *surface, + struct pipe_surface *past, + struct pipe_surface *future, unsigned num_macroblocks, - struct pipe_mpeg12_macroblock - *mpeg12_macroblocks, + struct pipe_mpeg12_macroblock *mpeg12_macroblocks, struct pipe_fence_handle **fence) { - bool new_surface = false; - - assert(renderer); + assert(renderer && buffer); assert(surface); assert(num_macroblocks); assert(mpeg12_macroblocks); - if (renderer->surface) { - if (surface != renderer->surface) { - if (renderer->num_macroblocks > 0) { - xfer_buffers_unmap(renderer); - flush(renderer); - } - - new_surface = true; - } - + if (surface != buffer->surface) { + pipe_surface_reference(&buffer->surface, surface); + pipe_surface_reference(&buffer->past, past); + pipe_surface_reference(&buffer->future, future); + buffer->fence = fence; + } else { /* If the surface we're rendering hasn't changed the ref frames shouldn't change. */ - assert(surface != renderer->surface || renderer->past == past); - assert(surface != renderer->surface || renderer->future == future); - } - else - new_surface = true; - - if (new_surface) { - renderer->surface = surface; - renderer->past = past; - renderer->future = future; - renderer->fence = fence; - renderer->surface_tex_inv_size.x = 1.0f / surface->width0; - renderer->surface_tex_inv_size.y = 1.0f / surface->height0; + assert(buffer->past == past); + assert(buffer->future == future); } while (num_macroblocks) { - unsigned left_in_batch = renderer->macroblocks_per_batch - renderer->num_macroblocks; + unsigned left_in_batch = renderer->macroblocks_per_batch - buffer->num_macroblocks; unsigned num_to_submit = MIN2(num_macroblocks, left_in_batch); unsigned i; for (i = 0; i < num_to_submit; ++i) { assert(mpeg12_macroblocks[i].base.codec == PIPE_VIDEO_CODEC_MPEG12); - grab_macroblock(renderer, &mpeg12_macroblocks[i]); + grab_macroblock(renderer, buffer, &mpeg12_macroblocks[i]); } num_macroblocks -= num_to_submit; - if (renderer->num_macroblocks == renderer->macroblocks_per_batch) { - xfer_buffers_unmap(renderer); - flush(renderer); - xfer_buffers_map(renderer); - /* Next time we get this surface it may have new ref frames */ - renderer->surface = NULL; + if (buffer->num_macroblocks == renderer->macroblocks_per_batch) { + vl_mpeg12_mc_unmap_buffer(renderer, buffer); + vl_mpeg12_mc_renderer_flush(renderer, buffer); + pipe_surface_reference(&buffer->surface, surface); + pipe_surface_reference(&buffer->past, past); + pipe_surface_reference(&buffer->future, future); + vl_mpeg12_mc_map_buffer(renderer, buffer); } } } + +void +vl_mpeg12_mc_unmap_buffer(struct vl_mpeg12_mc_renderer *renderer, struct vl_mpeg12_mc_buffer *buffer) +{ + assert(renderer && buffer); + + vl_idct_unmap_buffers(&renderer->idct_luma, &buffer->idct_y); + vl_idct_unmap_buffers(&renderer->idct_chroma, &buffer->idct_cr); + vl_idct_unmap_buffers(&renderer->idct_chroma, &buffer->idct_cb); + + vl_vb_unmap(&buffer->vertex_stream, renderer->pipe); +} + +void +vl_mpeg12_mc_renderer_flush(struct vl_mpeg12_mc_renderer *renderer, struct vl_mpeg12_mc_buffer *buffer) +{ + assert(renderer && buffer); + assert(buffer->num_macroblocks <= renderer->macroblocks_per_batch); + + if (buffer->num_macroblocks == 0) + return; + + vl_idct_flush(&renderer->idct_luma, &buffer->idct_y); + vl_idct_flush(&renderer->idct_chroma, &buffer->idct_cr); + vl_idct_flush(&renderer->idct_chroma, &buffer->idct_cb); + + vl_vb_restart(&buffer->vertex_stream); + + renderer->fb_state.cbufs[0] = buffer->surface; + renderer->pipe->bind_rasterizer_state(renderer->pipe, renderer->rs_state); + renderer->pipe->set_framebuffer_state(renderer->pipe, &renderer->fb_state); + renderer->pipe->set_viewport_state(renderer->pipe, &renderer->viewport); + renderer->pipe->set_vertex_buffers(renderer->pipe, 2, buffer->vertex_bufs.all); + renderer->pipe->bind_vertex_elements_state(renderer->pipe, renderer->vertex_elems_state); + + if (buffer->past) { + buffer->textures.individual.ref[0] = buffer->past->texture; + buffer->sampler_views.individual.ref[0] = find_or_create_sampler_view(renderer, buffer->past); + } else { + buffer->textures.individual.ref[0] = buffer->surface->texture; + buffer->sampler_views.individual.ref[0] = find_or_create_sampler_view(renderer, buffer->surface); + } + + if (buffer->future) { + buffer->textures.individual.ref[1] = buffer->future->texture; + buffer->sampler_views.individual.ref[1] = find_or_create_sampler_view(renderer, buffer->future); + } else { + buffer->textures.individual.ref[1] = buffer->surface->texture; + buffer->sampler_views.individual.ref[1] = find_or_create_sampler_view(renderer, buffer->surface); + } + + renderer->pipe->set_fragment_sampler_views(renderer->pipe, 5, buffer->sampler_views.all); + renderer->pipe->bind_fragment_sampler_states(renderer->pipe, 5, renderer->samplers.all); + + renderer->pipe->bind_vs_state(renderer->pipe, renderer->vs); + renderer->pipe->bind_fs_state(renderer->pipe, renderer->fs); + util_draw_arrays(renderer->pipe, PIPE_PRIM_QUADS, 0, buffer->num_macroblocks * 4); + + renderer->pipe->flush(renderer->pipe, PIPE_FLUSH_RENDER_CACHE, buffer->fence); + + /* Next time we get this surface it may have new ref frames */ + pipe_surface_reference(&buffer->surface, NULL); + pipe_surface_reference(&buffer->past, NULL); + pipe_surface_reference(&buffer->future, NULL); + + buffer->num_macroblocks = 0; +}