1af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy/**************************************************************************
2af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *
3af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * Copyright 2013 Grigori Goronzy <greg@chown.ath.cx>.
4af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * All Rights Reserved.
5af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *
6af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * Permission is hereby granted, free of charge, to any person obtaining a
7af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * copy of this software and associated documentation files (the
8af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * "Software"), to deal in the Software without restriction, including
9af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * without limitation the rights to use, copy, modify, merge, publish,
10af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * distribute, sub license, and/or sell copies of the Software, and to
11af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * permit persons to whom the Software is furnished to do so, subject to
12af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * the following conditions:
13af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *
14af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * The above copyright notice and this permission notice (including the
15af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * next paragraph) shall be included in all copies or substantial portions
16af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * of the Software.
17af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *
18af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *
26af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy **************************************************************************/
27af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
28af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy/*
29af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *  References:
30af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *
31af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *  Lin, S. F., Chang, Y. L., & Chen, L. G. (2003).
32af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *  Motion adaptive interpolation with horizontal motion detection for deinterlacing.
33af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *  Consumer Electronics, IEEE Transactions on, 49(4), 1256-1265.
34af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *
35af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *  Pei-Yin, C. H. E. N., & Yao-Hsien, L. A. I. (2007).
36af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *  A low-complexity interpolation method for deinterlacing.
37af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *  IEICE transactions on information and systems, 90(2), 606-608.
38af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy *
39af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy */
40af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
41af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy#include <stdio.h>
42af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
43af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy#include "pipe/p_context.h"
44af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
45af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy#include "tgsi/tgsi_ureg.h"
46af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
47af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy#include "util/u_draw.h"
48af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy#include "util/u_memory.h"
49af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy#include "util/u_math.h"
505fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König#include "util/u_format.h"
51af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
52af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy#include "vl_types.h"
53af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy#include "vl_video_buffer.h"
54af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy#include "vl_vertex_buffers.h"
55af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy#include "vl_deint_filter.h"
56af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
57af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyenum VS_OUTPUT
58af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy{
59af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   VS_O_VPOS = 0,
60af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   VS_O_VTEX = 0
61af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy};
62af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
63af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzystatic void *
64af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzycreate_vert_shader(struct vl_deint_filter *filter)
65af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy{
66af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_program *shader;
67af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_src i_vpos;
68af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_dst o_vpos, o_vtex;
69af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
70af249a7da9bf2621ab836d5074ef692677b11bbfMarek Olšák   shader = ureg_create(PIPE_SHADER_VERTEX);
71af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (!shader)
72af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      return NULL;
73af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
74af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   i_vpos = ureg_DECL_vs_input(shader, 0);
75af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   o_vpos = ureg_DECL_output(shader, TGSI_SEMANTIC_POSITION, VS_O_VPOS);
76af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   o_vtex = ureg_DECL_output(shader, TGSI_SEMANTIC_GENERIC, VS_O_VTEX);
77af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
78af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_MOV(shader, o_vpos, i_vpos);
79af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_MOV(shader, o_vtex, i_vpos);
80af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
81af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_END(shader);
82af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
83af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   return ureg_create_shader_and_destroy(shader, filter->pipe);
84af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy}
85af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
86af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzystatic void *
87af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzycreate_copy_frag_shader(struct vl_deint_filter *filter, unsigned field)
88af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy{
89af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_program *shader;
90af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_src i_vtex;
91af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_src sampler;
92af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_dst o_fragment;
93af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_dst t_tex;
94af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
95af249a7da9bf2621ab836d5074ef692677b11bbfMarek Olšák   shader = ureg_create(PIPE_SHADER_FRAGMENT);
96af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (!shader) {
97af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      return NULL;
98af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   }
99af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   t_tex = ureg_DECL_temporary(shader);
100af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
101af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   i_vtex = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_VTEX, TGSI_INTERPOLATE_LINEAR);
102af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sampler = ureg_DECL_sampler(shader, 2);
103af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   o_fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
104af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
105af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_MOV(shader, t_tex, i_vtex);
106af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (field) {
107af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_MOV(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_ZW),
108af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy               ureg_imm4f(shader, 0, 0, 1.0f, 0));
109af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   } else {
110af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_MOV(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_ZW),
111af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy               ureg_imm1f(shader, 0));
112af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   }
113af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
114af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_TEX(shader, o_fragment, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_tex), sampler);
115af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
116af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_release_temporary(shader, t_tex);
117af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_END(shader);
118af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
119af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   return ureg_create_shader_and_destroy(shader, filter->pipe);
120af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy}
121af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
122af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzystatic void *
123af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzycreate_deint_frag_shader(struct vl_deint_filter *filter, unsigned field,
124af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                         struct vertex2f *sizes, bool spatial_filter)
125af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy{
126af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_program *shader;
127af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_src i_vtex;
128af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_src sampler_cur;
129af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_src sampler_prevprev;
130af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_src sampler_prev;
131af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_src sampler_next;
132af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_dst o_fragment;
133af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_dst t_tex;
134af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_dst t_comp_top, t_comp_bot;
135af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_dst t_diff;
136af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_dst t_a, t_b;
137af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct ureg_dst t_weave, t_linear;
138af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
139af249a7da9bf2621ab836d5074ef692677b11bbfMarek Olšák   shader = ureg_create(PIPE_SHADER_FRAGMENT);
140af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (!shader) {
141af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      return NULL;
142af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   }
143af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
144af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   t_tex = ureg_DECL_temporary(shader);
145af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   t_comp_top = ureg_DECL_temporary(shader);
146af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   t_comp_bot = ureg_DECL_temporary(shader);
147af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   t_diff = ureg_DECL_temporary(shader);
148af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   t_a = ureg_DECL_temporary(shader);
149af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   t_b = ureg_DECL_temporary(shader);
150af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   t_weave = ureg_DECL_temporary(shader);
151af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   t_linear = ureg_DECL_temporary(shader);
152af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
153af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   i_vtex = ureg_DECL_fs_input(shader, TGSI_SEMANTIC_GENERIC, VS_O_VTEX, TGSI_INTERPOLATE_LINEAR);
154af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sampler_prevprev = ureg_DECL_sampler(shader, 0);
155af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sampler_prev = ureg_DECL_sampler(shader, 1);
156af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sampler_cur = ureg_DECL_sampler(shader, 2);
157af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sampler_next = ureg_DECL_sampler(shader, 3);
158af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   o_fragment = ureg_DECL_output(shader, TGSI_SEMANTIC_COLOR, 0);
159af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
160af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   // we don't care about ZW interpolation (allows better optimization)
161af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_MOV(shader, t_tex, i_vtex);
162af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_MOV(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_ZW),
163af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy            ureg_imm1f(shader, 0));
164af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
165af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   // sample between texels for cheap lowpass
166af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_ADD(shader, t_comp_top, ureg_src(t_tex),
167af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy            ureg_imm4f(shader, sizes->x * 0.5f, sizes->y * -0.5f, 0, 0));
168af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_ADD(shader, t_comp_bot, ureg_src(t_tex),
169af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy            ureg_imm4f(shader, sizes->x * -0.5f, sizes->y * 0.5f, 1.0f, 0));
170af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
171af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (field == 0) {
172af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      /* interpolating top field -> current field is a bottom field */
173af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      // cur vs prev2
174af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_TEX(shader, t_a, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_comp_bot), sampler_cur);
175af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_TEX(shader, t_b, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_comp_bot), sampler_prevprev);
176d995115b1733ec14182e6bb4653b8f8389b87518Marek Olšák      ureg_ADD(shader, ureg_writemask(t_diff, TGSI_WRITEMASK_X), ureg_src(t_a), ureg_negate(ureg_src(t_b)));
177af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      // prev vs next
178af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_TEX(shader, t_a, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_comp_top), sampler_prev);
179af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_TEX(shader, t_b, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_comp_top), sampler_next);
180d995115b1733ec14182e6bb4653b8f8389b87518Marek Olšák      ureg_ADD(shader, ureg_writemask(t_diff, TGSI_WRITEMASK_Y), ureg_src(t_a), ureg_negate(ureg_src(t_b)));
181af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   } else {
182af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      /* interpolating bottom field -> current field is a top field */
183af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      // cur vs prev2
184af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_TEX(shader, t_a, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_comp_top), sampler_cur);
185af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_TEX(shader, t_b, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_comp_top), sampler_prevprev);
186d995115b1733ec14182e6bb4653b8f8389b87518Marek Olšák      ureg_ADD(shader, ureg_writemask(t_diff, TGSI_WRITEMASK_X), ureg_src(t_a), ureg_negate(ureg_src(t_b)));
187af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      // prev vs next
188af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_TEX(shader, t_a, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_comp_bot), sampler_prev);
189af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_TEX(shader, t_b, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_comp_bot), sampler_next);
190d995115b1733ec14182e6bb4653b8f8389b87518Marek Olšák      ureg_ADD(shader, ureg_writemask(t_diff, TGSI_WRITEMASK_Y), ureg_src(t_a), ureg_negate(ureg_src(t_b)));
191af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   }
192af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
193af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   // absolute maximum of differences
194af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_MAX(shader, ureg_writemask(t_diff, TGSI_WRITEMASK_X), ureg_abs(ureg_src(t_diff)),
195af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy            ureg_scalar(ureg_abs(ureg_src(t_diff)), TGSI_SWIZZLE_Y));
196af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
197af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (field == 0) {
198af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      /* weave with prev top field */
199af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_TEX(shader, t_weave, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_tex), sampler_prev);
200af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      /* get linear interpolation from current bottom field */
201af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_ADD(shader, t_comp_top, ureg_src(t_tex), ureg_imm4f(shader, 0, sizes->y * -1.0f, 1.0f, 0));
202af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_TEX(shader, t_linear, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_comp_top), sampler_cur);
203af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   } else {
204af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      /* weave with prev bottom field */
205af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_ADD(shader, t_comp_bot, ureg_src(t_tex), ureg_imm4f(shader, 0, 0, 1.0f, 0));
206af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_TEX(shader, t_weave, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_comp_bot), sampler_prev);
207af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      /* get linear interpolation from current top field */
208af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_ADD(shader, t_comp_bot, ureg_src(t_tex), ureg_imm4f(shader, 0, sizes->y * 1.0f, 0, 0));
209af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      ureg_TEX(shader, t_linear, TGSI_TEXTURE_2D_ARRAY, ureg_src(t_comp_bot), sampler_cur);
210af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   }
211af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
212af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   // mix between weave and linear
213af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   // fully weave if diff < 6 (0.02353), fully interpolate if diff > 14 (0.05490)
214af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_ADD(shader, ureg_writemask(t_diff, TGSI_WRITEMASK_X), ureg_src(t_diff),
215af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy            ureg_imm4f(shader, -0.02353f, 0, 0, 0));
216af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_MUL(shader, ureg_saturate(ureg_writemask(t_diff, TGSI_WRITEMASK_X)),
217af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy            ureg_src(t_diff), ureg_imm4f(shader, 31.8750f, 0, 0, 0));
2185fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   ureg_LRP(shader, ureg_writemask(t_tex, TGSI_WRITEMASK_X), ureg_src(t_diff),
219af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy            ureg_src(t_linear), ureg_src(t_weave));
2205fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   ureg_MOV(shader, o_fragment, ureg_scalar(ureg_src(t_tex), TGSI_SWIZZLE_X));
221af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
222af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_release_temporary(shader, t_tex);
223af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_release_temporary(shader, t_comp_top);
224af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_release_temporary(shader, t_comp_bot);
225af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_release_temporary(shader, t_diff);
226af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_release_temporary(shader, t_a);
227af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_release_temporary(shader, t_b);
228af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_release_temporary(shader, t_weave);
229af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_release_temporary(shader, t_linear);
230af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ureg_END(shader);
231af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
232af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   return ureg_create_shader_and_destroy(shader, filter->pipe);
233af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy}
234af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
235af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzybool
236af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyvl_deint_filter_init(struct vl_deint_filter *filter, struct pipe_context *pipe,
237af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                     unsigned video_width, unsigned video_height,
238af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                     bool skip_chroma, bool spatial_filter)
239af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy{
240af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_rasterizer_state rs_state;
241af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_blend_state blend;
242af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_sampler_state sampler;
243af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_vertex_element ve;
244af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct vertex2f sizes;
245af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_video_buffer templ;
246af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
247af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   assert(filter && pipe);
248af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   assert(video_width && video_height);
249af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
250af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   memset(filter, 0, sizeof(*filter));
251af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe = pipe;
252af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->skip_chroma = skip_chroma;
253af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->video_width = video_width;
254af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->video_height = video_height;
255af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
256af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   /* TODO: handle other than 4:2:0 subsampling */
257af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   memset(&templ, 0, sizeof(templ));
2586f898f740c4932f3d34f5f69d60b9b9b6b3a9327Christian König   templ.buffer_format = pipe->screen->get_video_param
2596f898f740c4932f3d34f5f69d60b9b9b6b3a9327Christian König   (
2606f898f740c4932f3d34f5f69d60b9b9b6b3a9327Christian König      pipe->screen,
2616f898f740c4932f3d34f5f69d60b9b9b6b3a9327Christian König      PIPE_VIDEO_PROFILE_UNKNOWN,
2626f898f740c4932f3d34f5f69d60b9b9b6b3a9327Christian König      PIPE_VIDEO_ENTRYPOINT_UNKNOWN,
2636f898f740c4932f3d34f5f69d60b9b9b6b3a9327Christian König      PIPE_VIDEO_CAP_PREFERED_FORMAT
2646f898f740c4932f3d34f5f69d60b9b9b6b3a9327Christian König   );
265af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   templ.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
266af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   templ.width = video_width;
267af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   templ.height = video_height;
268af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   templ.interlaced = true;
269af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->video_buffer = vl_video_buffer_create(pipe, &templ);
270af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (!filter->video_buffer)
271af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      goto error_video_buffer;
272af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
273af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   memset(&rs_state, 0, sizeof(rs_state));
274af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   rs_state.half_pixel_center = true;
275af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   rs_state.bottom_edge_rule = true;
276af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   rs_state.depth_clip = 1;
277af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
278af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (!filter->rs_state)
279af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      goto error_rs_state;
280af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
281af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   memset(&blend, 0, sizeof blend);
2825fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   blend.rt[0].colormask = PIPE_MASK_R;
2835fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   filter->blend[0] = pipe->create_blend_state(pipe, &blend);
2845fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   if (!filter->blend[0])
2855fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      goto error_blendR;
2865fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König
2875fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   blend.rt[0].colormask = PIPE_MASK_G;
2885fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   filter->blend[1] = pipe->create_blend_state(pipe, &blend);
2895fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   if (!filter->blend[1])
2905fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      goto error_blendG;
2915fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König
2925fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   blend.rt[0].colormask = PIPE_MASK_B;
2935fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   filter->blend[2] = pipe->create_blend_state(pipe, &blend);
2945fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   if (!filter->blend[2])
2955fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      goto error_blendB;
296af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
297af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   memset(&sampler, 0, sizeof(sampler));
298af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
299af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
300af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
301af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
302af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
303af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
304af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sampler.normalized_coords = 1;
305af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->sampler[0] = pipe->create_sampler_state(pipe, &sampler);
306af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->sampler[1] = filter->sampler[2] = filter->sampler[3] = filter->sampler[0];
307af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (!filter->sampler[0])
308af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      goto error_sampler;
309af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
310af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->quad = vl_vb_upload_quads(pipe);
311af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if(!filter->quad.buffer)
312af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      goto error_quad;
313af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
314af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   memset(&ve, 0, sizeof(ve));
315af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ve.src_offset = 0;
316af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ve.instance_divisor = 0;
317af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ve.vertex_buffer_index = 0;
318af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   ve.src_format = PIPE_FORMAT_R32G32_FLOAT;
319af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->ves = pipe->create_vertex_elements_state(pipe, 1, &ve);
320af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (!filter->ves)
321af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      goto error_ves;
322af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
323af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sizes.x = 1.0f / video_width;
324af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   sizes.y = 1.0f / video_height;
325af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
326af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->vs = create_vert_shader(filter);
327af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (!filter->vs)
328af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      goto error_vs;
329af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
330af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->fs_copy_top = create_copy_frag_shader(filter, 0);
331af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (!filter->fs_copy_top)
332af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      goto error_fs_copy_top;
333af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
334af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->fs_copy_bottom = create_copy_frag_shader(filter, 1);
335af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (!filter->fs_copy_bottom)
336af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      goto error_fs_copy_bottom;
337af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
338af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->fs_deint_top = create_deint_frag_shader(filter, 0, &sizes, spatial_filter);
339af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (!filter->fs_deint_top)
340af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      goto error_fs_deint_top;
341af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
342af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->fs_deint_bottom = create_deint_frag_shader(filter, 1, &sizes, spatial_filter);
343af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   if (!filter->fs_deint_bottom)
344af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      goto error_fs_deint_bottom;
345af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
346af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   return true;
347af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
348af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyerror_fs_deint_bottom:
349af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   pipe->delete_fs_state(pipe, filter->fs_deint_top);
350af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
351af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyerror_fs_deint_top:
352af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   pipe->delete_fs_state(pipe, filter->fs_copy_bottom);
353af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
354af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyerror_fs_copy_bottom:
355af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   pipe->delete_fs_state(pipe, filter->fs_copy_top);
356af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
357af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyerror_fs_copy_top:
358af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   pipe->delete_vs_state(pipe, filter->vs);
359af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
360af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyerror_vs:
361af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   pipe->delete_vertex_elements_state(pipe, filter->ves);
362af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
363af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyerror_ves:
364af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   pipe_resource_reference(&filter->quad.buffer, NULL);
365af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
366af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyerror_quad:
367af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   pipe->delete_sampler_state(pipe, filter->sampler);
368af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
369af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyerror_sampler:
3705fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   pipe->delete_blend_state(pipe, filter->blend[2]);
3715fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König
3725fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian Königerror_blendB:
3735fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   pipe->delete_blend_state(pipe, filter->blend[1]);
3745fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König
3755fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian Königerror_blendG:
3765fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   pipe->delete_blend_state(pipe, filter->blend[0]);
377af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
3785fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian Königerror_blendR:
379af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   pipe->delete_rasterizer_state(pipe, filter->rs_state);
380af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
381af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyerror_rs_state:
382af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->video_buffer->destroy(filter->video_buffer);
383af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
384af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyerror_video_buffer:
385af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   return false;
386af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy}
387af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
388af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyvoid
389af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyvl_deint_filter_cleanup(struct vl_deint_filter *filter)
390af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy{
391af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   assert(filter);
392af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
393af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->delete_sampler_state(filter->pipe, filter->sampler[0]);
3945fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   filter->pipe->delete_blend_state(filter->pipe, filter->blend[0]);
3955fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   filter->pipe->delete_blend_state(filter->pipe, filter->blend[1]);
3965fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   filter->pipe->delete_blend_state(filter->pipe, filter->blend[2]);
397af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->delete_rasterizer_state(filter->pipe, filter->rs_state);
398af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->delete_vertex_elements_state(filter->pipe, filter->ves);
399af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   pipe_resource_reference(&filter->quad.buffer, NULL);
400af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
401af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->delete_vs_state(filter->pipe, filter->vs);
402af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->delete_fs_state(filter->pipe, filter->fs_copy_top);
403af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->delete_fs_state(filter->pipe, filter->fs_copy_bottom);
404af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->delete_fs_state(filter->pipe, filter->fs_deint_top);
405af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->delete_fs_state(filter->pipe, filter->fs_deint_bottom);
406af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
407af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->video_buffer->destroy(filter->video_buffer);
408af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy}
409af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
410af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzybool
411af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyvl_deint_filter_check_buffers(struct vl_deint_filter *filter,
412af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                              struct pipe_video_buffer *prevprev,
413af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                              struct pipe_video_buffer *prev,
414af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                              struct pipe_video_buffer *cur,
415af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                              struct pipe_video_buffer *next)
416af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy{
417af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   int i;
418af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_video_buffer *bufs[] = { prevprev, prev, cur, next };
419af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
420af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   for (i = 0; i < 4; i++) {
421af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      if (bufs[i]->chroma_format != PIPE_VIDEO_CHROMA_FORMAT_420)
422af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy         return false;
423af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      if (bufs[i]->width < filter->video_width ||
424af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy          bufs[i]->height < filter->video_height)
425af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy         return false;
426af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      if (!bufs[i]->interlaced)
427af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy         return false;
428af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   }
429af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
430af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   return true;
431af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy}
432af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
433af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyvoid
434af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzyvl_deint_filter_render(struct vl_deint_filter *filter,
435af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                       struct pipe_video_buffer *prevprev,
436af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                       struct pipe_video_buffer *prev,
437af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                       struct pipe_video_buffer *cur,
438af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                       struct pipe_video_buffer *next,
439af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                       unsigned field)
440af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy{
441af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_viewport_state viewport;
442af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_framebuffer_state fb_state;
443af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_sampler_view **cur_sv;
444af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_sampler_view **prevprev_sv;
445af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_sampler_view **prev_sv;
446af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_sampler_view **next_sv;
447af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_sampler_view *sampler_views[4];
448af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   struct pipe_surface **dst_surfaces;
4495fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   const unsigned *plane_order;
4501fb4179f927442354f93dfc8494f0236e50af838Jan Vesely   int i;
4511fb4179f927442354f93dfc8494f0236e50af838Jan Vesely   unsigned j;
452af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
453af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   assert(filter && prevprev && prev && cur && next && field <= 1);
454af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
455af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   /* set up destination and source */
456af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   dst_surfaces = filter->video_buffer->get_surfaces(filter->video_buffer);
4575fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   plane_order = vl_video_buffer_plane_order(filter->video_buffer->buffer_format);
458af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   cur_sv = cur->get_sampler_view_components(cur);
459af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   prevprev_sv = prevprev->get_sampler_view_components(prevprev);
460af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   prev_sv = prev->get_sampler_view_components(prev);
461af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   next_sv = next->get_sampler_view_components(next);
462af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
463af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   /* set up pipe state */
464af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->bind_rasterizer_state(filter->pipe, filter->rs_state);
465af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->set_vertex_buffers(filter->pipe, 0, 1, &filter->quad);
466af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->bind_vertex_elements_state(filter->pipe, filter->ves);
467af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->bind_vs_state(filter->pipe, filter->vs);
468af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   filter->pipe->bind_sampler_states(filter->pipe, PIPE_SHADER_FRAGMENT,
469af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy                                     0, 4, filter->sampler);
470af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
471af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   /* prepare viewport */
472af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   memset(&viewport, 0, sizeof(viewport));
473af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   viewport.scale[2] = 1;
474af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
475af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   /* prepare framebuffer */
476af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   memset(&fb_state, 0, sizeof(fb_state));
477af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   fb_state.nr_cbufs = 1;
478af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
479af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   /* process each plane separately */
4805fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König   for (i = 0, j = 0; i < VL_NUM_COMPONENTS; ++i) {
4815fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      struct pipe_surface *blit_surf = dst_surfaces[field];
4825fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      struct pipe_surface *dst_surf = dst_surfaces[1 - field];
4835fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      int k = plane_order[i];
4845fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König
4855fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      /* bind blend state for this component in the plane */
4865fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      filter->pipe->bind_blend_state(filter->pipe, filter->blend[j]);
487af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
488af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      /* update render target state */
489af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      viewport.scale[0] = blit_surf->texture->width0;
490af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      viewport.scale[1] = blit_surf->texture->height0;
491af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      fb_state.width = blit_surf->texture->width0;
492af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      fb_state.height = blit_surf->texture->height0;
493af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
494af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      /* update sampler view sources  */
4955fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      sampler_views[0] = prevprev_sv[k];
4965fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      sampler_views[1] = prev_sv[k];
4975fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      sampler_views[2] = cur_sv[k];
4985fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      sampler_views[3] = next_sv[k];
499af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      filter->pipe->set_sampler_views(filter->pipe, PIPE_SHADER_FRAGMENT, 0, 4, sampler_views);
500af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
501af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      /* blit current field */
502af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      fb_state.cbufs[0] = blit_surf;
503af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      filter->pipe->bind_fs_state(filter->pipe, field ? filter->fs_copy_bottom : filter->fs_copy_top);
504af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      filter->pipe->set_framebuffer_state(filter->pipe, &fb_state);
505af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      filter->pipe->set_viewport_states(filter->pipe, 0, 1, &viewport);
506af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      util_draw_arrays(filter->pipe, PIPE_PRIM_QUADS, 0, 4);
507af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy
508af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      /* blit or interpolate other field */
509af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      fb_state.cbufs[0] = dst_surf;
510af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      filter->pipe->set_framebuffer_state(filter->pipe, &fb_state);
5115fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      if (i > 0 && filter->skip_chroma) {
512af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy         util_draw_arrays(filter->pipe, PIPE_PRIM_QUADS, 0, 4);
513af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      } else {
514af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy         filter->pipe->bind_fs_state(filter->pipe, field ? filter->fs_deint_top : filter->fs_deint_bottom);
515af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy         util_draw_arrays(filter->pipe, PIPE_PRIM_QUADS, 0, 4);
516af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy      }
5175fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König
5185fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      if (++j >= util_format_get_nr_components(dst_surf->format)) {
5195fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König         dst_surfaces += 2;
5205fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König         j = 0;
5215fdd4a5aef122a8aa1da91ac0555a817af0d1b06Christian König      }
522af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy   }
523af34c3fd10e2511f02a40810cddebea49093a526Grigori Goronzy}
524