Merge branch 'kasanen-post-process-v2'
[mesa.git] / src / gallium / auxiliary / postprocess / pp_mlaa.c
1 /**
2 * Copyright (C) 2010 Jorge Jimenez (jorge@iryoku.com)
3 * Copyright (C) 2010 Belen Masia (bmasia@unizar.es)
4 * Copyright (C) 2010 Jose I. Echevarria (joseignacioechevarria@gmail.com)
5 * Copyright (C) 2010 Fernando Navarro (fernandn@microsoft.com)
6 * Copyright (C) 2010 Diego Gutierrez (diegog@unizar.es)
7 * Copyright (C) 2011 Lauri Kasanen (cand@gmx.com)
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the following statement:
17 *
18 * "Uses Jimenez's MLAA. Copyright (C) 2010 by Jorge Jimenez, Belen Masia,
19 * Jose I. Echevarria, Fernando Navarro and Diego Gutierrez."
20 *
21 * Only for use in the Mesa project, this point 2 is filled by naming the
22 * technique Jimenez's MLAA in the Mesa config options.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
25 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 * The views and conclusions contained in the software and documentation are
37 * those of the authors and should not be interpreted as representing official
38 * policies, either expressed or implied, of the copyright holders.
39 */
40
41 #include <stdio.h>
42 #include <string.h>
43 #include "postprocess/postprocess.h"
44 #include "postprocess/pp_mlaa.h"
45 #include "postprocess/pp_filters.h"
46 #include "util/u_blit.h"
47 #include "util/u_box.h"
48 #include "util/u_sampler.h"
49 #include "util/u_inlines.h"
50 #include "pipe/p_screen.h"
51
52 #define IMM_SPACE 80
53
54 static float constants[] = { 1, 1, 0, 0 };
55 static unsigned int dimensions[2] = { 0, 0 };
56
57 static struct pipe_resource *constbuf, *areamaptex;
58
59 /** Upload the constants. */
60 static void
61 up_consts(struct pipe_context *pipe)
62 {
63 struct pipe_box box;
64
65 u_box_2d(0, 0, sizeof(constants), 1, &box);
66 pipe->transfer_inline_write(pipe, constbuf, 0, PIPE_TRANSFER_WRITE,
67 &box, constants, sizeof(constants),
68 sizeof(constants));
69 }
70
71 /** Run function of the MLAA filter. */
72 static void
73 pp_jimenezmlaa_run(struct pp_queue_t *ppq, struct pipe_resource *in,
74 struct pipe_resource *out, unsigned int n, bool iscolor)
75 {
76
77 struct program *p = ppq->p;
78
79 struct pipe_depth_stencil_alpha_state mstencil;
80 struct pipe_sampler_view v_tmp, *arr[3];
81
82 unsigned int w = p->framebuffer.width;
83 unsigned int h = p->framebuffer.height;
84
85 const struct pipe_stencil_ref ref = { {1} };
86 memset(&mstencil, 0, sizeof(mstencil));
87 cso_set_stencil_ref(p->cso, &ref);
88
89 /* Init the pixel size constant */
90 if (dimensions[0] != p->framebuffer.width ||
91 dimensions[1] != p->framebuffer.height) {
92 constants[0] = 1.0 / p->framebuffer.width;
93 constants[1] = 1.0 / p->framebuffer.height;
94
95 up_consts(p->pipe);
96 dimensions[0] = p->framebuffer.width;
97 dimensions[1] = p->framebuffer.height;
98 }
99
100 p->pipe->set_constant_buffer(p->pipe, PIPE_SHADER_VERTEX, 0, constbuf);
101 p->pipe->set_constant_buffer(p->pipe, PIPE_SHADER_FRAGMENT, 0, constbuf);
102
103 mstencil.stencil[0].enabled = 1;
104 mstencil.stencil[0].valuemask = mstencil.stencil[0].writemask = ~0;
105 mstencil.stencil[0].func = PIPE_FUNC_ALWAYS;
106 mstencil.stencil[0].fail_op = PIPE_STENCIL_OP_KEEP;
107 mstencil.stencil[0].zfail_op = PIPE_STENCIL_OP_KEEP;
108 mstencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
109
110 p->framebuffer.zsbuf = ppq->stencils;
111
112 /* First pass: depth edge detection */
113 if (iscolor)
114 pp_filter_setup_in(p, in);
115 else
116 pp_filter_setup_in(p, ppq->depth);
117
118 pp_filter_setup_out(p, ppq->inner_tmp[0]);
119
120 pp_filter_set_fb(p);
121 pp_filter_misc_state(p);
122 cso_set_depth_stencil_alpha(p->cso, &mstencil);
123 p->pipe->clear(p->pipe, PIPE_CLEAR_STENCIL | PIPE_CLEAR_COLOR,
124 p->clear_color, 0, 0);
125
126 cso_single_sampler(p->cso, 0, &p->sampler_point);
127 cso_single_sampler_done(p->cso);
128 cso_set_fragment_sampler_views(p->cso, 1, &p->view);
129
130 cso_set_vertex_shader_handle(p->cso, ppq->shaders[n][1]); /* offsetvs */
131 cso_set_fragment_shader_handle(p->cso, ppq->shaders[n][2]);
132
133 pp_filter_draw(p);
134 pp_filter_end_pass(p);
135
136
137 /* Second pass: blend weights */
138 /* Sampler order: areamap, edgesmap, edgesmapL (reversed, thx compiler) */
139 mstencil.stencil[0].func = PIPE_FUNC_EQUAL;
140 mstencil.stencil[0].zpass_op = PIPE_STENCIL_OP_KEEP;
141 cso_set_depth_stencil_alpha(p->cso, &mstencil);
142
143 pp_filter_setup_in(p, areamaptex);
144 pp_filter_setup_out(p, ppq->inner_tmp[1]);
145
146 u_sampler_view_default_template(&v_tmp, ppq->inner_tmp[0],
147 ppq->inner_tmp[0]->format);
148 arr[1] = arr[2] = p->pipe->create_sampler_view(p->pipe,
149 ppq->inner_tmp[0], &v_tmp);
150
151 pp_filter_set_clear_fb(p);
152
153 cso_single_sampler(p->cso, 0, &p->sampler_point);
154 cso_single_sampler(p->cso, 1, &p->sampler_point);
155 cso_single_sampler(p->cso, 2, &p->sampler);
156 cso_single_sampler_done(p->cso);
157
158 arr[0] = p->view;
159 cso_set_fragment_sampler_views(p->cso, 3, arr);
160
161 cso_set_vertex_shader_handle(p->cso, ppq->shaders[n][0]); /* passvs */
162 cso_set_fragment_shader_handle(p->cso, ppq->shaders[n][3]);
163
164 pp_filter_draw(p);
165 pp_filter_end_pass(p);
166 pipe_sampler_view_reference(&arr[1], NULL);
167
168
169 /* Third pass: smoothed edges */
170 /* Sampler order: colormap, blendmap (wtf compiler) */
171 pp_filter_setup_in(p, ppq->inner_tmp[1]);
172 pp_filter_setup_out(p, out);
173
174 pp_filter_set_fb(p);
175
176 /* Blit the input to the output */
177 util_blit_pixels(p->blitctx, in, 0, 0, 0,
178 w, h, 0, p->framebuffer.cbufs[0],
179 0, 0, w, h, 0, PIPE_TEX_MIPFILTER_NEAREST);
180
181 u_sampler_view_default_template(&v_tmp, in, in->format);
182 arr[0] = p->pipe->create_sampler_view(p->pipe, in, &v_tmp);
183
184 cso_single_sampler(p->cso, 0, &p->sampler_point);
185 cso_single_sampler(p->cso, 1, &p->sampler_point);
186 cso_single_sampler_done(p->cso);
187
188 arr[1] = p->view;
189 cso_set_fragment_sampler_views(p->cso, 2, arr);
190
191 cso_set_vertex_shader_handle(p->cso, ppq->shaders[n][1]); /* offsetvs */
192 cso_set_fragment_shader_handle(p->cso, ppq->shaders[n][4]);
193
194 p->blend.rt[0].blend_enable = 1;
195 cso_set_blend(p->cso, &p->blend);
196
197 pp_filter_draw(p);
198 pp_filter_end_pass(p);
199 pipe_sampler_view_reference(&arr[0], NULL);
200
201 p->blend.rt[0].blend_enable = 0;
202 p->framebuffer.zsbuf = NULL;
203 }
204
205 /** The init function of the MLAA filter. */
206 static void
207 pp_jimenezmlaa_init_run(struct pp_queue_t *ppq, unsigned int n,
208 unsigned int val, bool iscolor)
209 {
210
211 struct pipe_box box;
212 struct pipe_resource res;
213
214 char *tmp_text = calloc(sizeof(blend2fs_1) + sizeof(blend2fs_2) +
215 IMM_SPACE, sizeof(char));
216
217 constbuf = pipe_buffer_create(ppq->p->screen, PIPE_BIND_CONSTANT_BUFFER,
218 PIPE_USAGE_STATIC, sizeof(constants));
219 if (!constbuf) {
220 pp_debug("Failed to allocate constant buffer\n");
221 return;
222 }
223
224
225 pp_debug("mlaa: using %u max search steps\n", val);
226
227 if (!tmp_text) {
228 pp_debug("Failed to allocate shader space\n");
229 return;
230 }
231 sprintf(tmp_text, "%s"
232 "IMM FLT32 { %.8f, 0.0000, 0.0000, 0.0000}\n"
233 "%s\n", blend2fs_1, (float) val, blend2fs_2);
234
235 memset(&res, 0, sizeof(res));
236
237 res.target = PIPE_TEXTURE_2D;
238 res.format = PIPE_FORMAT_R8G8_UNORM;
239 res.width0 = res.height0 = 165;
240 res.bind = PIPE_BIND_SAMPLER_VIEW;
241 res.usage = PIPE_USAGE_STATIC;
242 res.depth0 = res.array_size = res.nr_samples = 1;
243
244 if (!ppq->p->screen->is_format_supported(ppq->p->screen, res.format,
245 res.target, 1, res.bind))
246 pp_debug("Areamap format not supported\n");
247
248 areamaptex = ppq->p->screen->resource_create(ppq->p->screen, &res);
249 u_box_2d(0, 0, 165, 165, &box);
250
251 ppq->p->pipe->transfer_inline_write(ppq->p->pipe, areamaptex, 0,
252 PIPE_TRANSFER_WRITE, &box,
253 areamap, 165 * 2, sizeof(areamap));
254
255
256
257 ppq->shaders[n][1] = pp_tgsi_to_state(ppq->p->pipe, offsetvs, true,
258 "offsetvs");
259 if (iscolor)
260 ppq->shaders[n][2] = pp_tgsi_to_state(ppq->p->pipe, color1fs,
261 false, "color1fs");
262 else
263 ppq->shaders[n][2] = pp_tgsi_to_state(ppq->p->pipe, depth1fs,
264 false, "depth1fs");
265 ppq->shaders[n][3] = pp_tgsi_to_state(ppq->p->pipe, tmp_text, false,
266 "blend2fs");
267 ppq->shaders[n][4] = pp_tgsi_to_state(ppq->p->pipe, neigh3fs, false,
268 "neigh3fs");
269
270 free(tmp_text);
271 }
272
273 /** Short wrapper to init the depth version. */
274 void
275 pp_jimenezmlaa_init(struct pp_queue_t *ppq, unsigned int n, unsigned int val)
276 {
277
278 pp_jimenezmlaa_init_run(ppq, n, val, false);
279 }
280
281 /** Short wrapper to init the color version. */
282 void
283 pp_jimenezmlaa_init_color(struct pp_queue_t *ppq, unsigned int n,
284 unsigned int val)
285 {
286
287 pp_jimenezmlaa_init_run(ppq, n, val, true);
288 }
289
290 /** Short wrapper to run the depth version. */
291 void
292 pp_jimenezmlaa(struct pp_queue_t *ppq, struct pipe_resource *in,
293 struct pipe_resource *out, unsigned int n)
294 {
295 pp_jimenezmlaa_run(ppq, in, out, n, false);
296 }
297
298 /** Short wrapper to run the color version. */
299 void
300 pp_jimenezmlaa_color(struct pp_queue_t *ppq, struct pipe_resource *in,
301 struct pipe_resource *out, unsigned int n)
302 {
303 pp_jimenezmlaa_run(ppq, in, out, n, true);
304 }