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 "pipe/p_compiler.h"
42
43 #include "postprocess/postprocess.h"
44 #include "postprocess/pp_mlaa.h"
45 #include "postprocess/pp_filters.h"
46 #include "postprocess/pp_private.h"
47
48 #include "util/u_box.h"
49 #include "util/u_sampler.h"
50 #include "util/u_inlines.h"
51 #include "util/u_memory.h"
52 #include "util/u_string.h"
53 #include "pipe/p_screen.h"
54
55 #define IMM_SPACE 80
56
57 static float constants[] = { 1, 1, 0, 0 };
58 static unsigned int dimensions[2] = { 0, 0 };
59
60 /** Run function of the MLAA filter. */
61 static void
pp_jimenezmlaa_run(struct pp_queue_t * ppq,struct pipe_resource * in,struct pipe_resource * out,unsigned int n,bool iscolor)62 pp_jimenezmlaa_run(struct pp_queue_t *ppq, struct pipe_resource *in,
63 struct pipe_resource *out, unsigned int n, bool iscolor)
64 {
65
66 struct pp_program *p = ppq->p;
67
68 struct pipe_depth_stencil_alpha_state mstencil;
69 struct pipe_sampler_view v_tmp, *arr[3];
70
71 unsigned int w = 0;
72 unsigned int h = 0;
73
74 const struct pipe_stencil_ref ref = { {1} };
75
76 /* Insufficient initialization checks. */
77 assert(p);
78 assert(ppq);
79 assert(ppq->areamaptex);
80 assert(ppq->inner_tmp);
81 assert(ppq->shaders[n]);
82
83 w = p->framebuffer.width;
84 h = p->framebuffer.height;
85
86 memset(&mstencil, 0, sizeof(mstencil));
87
88 cso_set_stencil_ref(p->cso, ref);
89
90 /* Init the pixel size constant */
91 if (dimensions[0] != p->framebuffer.width ||
92 dimensions[1] != p->framebuffer.height) {
93 constants[0] = 1.0f / p->framebuffer.width;
94 constants[1] = 1.0f / p->framebuffer.height;
95
96 dimensions[0] = p->framebuffer.width;
97 dimensions[1] = p->framebuffer.height;
98 }
99
100 struct pipe_constant_buffer cb;
101 cb.buffer = NULL;
102 cb.buffer_offset = 0;
103 cb.buffer_size = sizeof(constants);
104 cb.user_buffer = constants;
105
106 struct pipe_context *pipe = ppq->p->pipe;
107 pipe->set_constant_buffer(pipe, PIPE_SHADER_VERTEX, 0, false, &cb);
108 pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, 0, false, &cb);
109
110 mstencil.stencil[0].enabled = 1;
111 mstencil.stencil[0].valuemask = mstencil.stencil[0].writemask = ~0;
112 mstencil.stencil[0].func = PIPE_FUNC_ALWAYS;
113 mstencil.stencil[0].fail_op = PIPE_STENCIL_OP_KEEP;
114 mstencil.stencil[0].zfail_op = PIPE_STENCIL_OP_KEEP;
115 mstencil.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
116
117 p->framebuffer.zsbuf = ppq->stencils;
118
119 /* First pass: depth edge detection */
120 if (iscolor)
121 pp_filter_setup_in(p, in);
122 else
123 pp_filter_setup_in(p, ppq->depth);
124
125 pp_filter_setup_out(p, ppq->inner_tmp[0]);
126
127 pp_filter_set_fb(p);
128 pp_filter_misc_state(p);
129 cso_set_depth_stencil_alpha(p->cso, &mstencil);
130 p->pipe->clear(p->pipe, PIPE_CLEAR_STENCIL | PIPE_CLEAR_COLOR0, NULL,
131 &p->clear_color, 0, 0);
132
133 {
134 const struct pipe_sampler_state *samplers[] = {&p->sampler_point};
135 cso_set_samplers(p->cso, PIPE_SHADER_FRAGMENT, 1, samplers);
136 }
137 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &p->view);
138
139 cso_set_vertex_shader_handle(p->cso, ppq->shaders[n][1]); /* offsetvs */
140 cso_set_fragment_shader_handle(p->cso, ppq->shaders[n][2]);
141
142 pp_filter_draw(p);
143 pp_filter_end_pass(p);
144
145
146 /* Second pass: blend weights */
147 /* Sampler order: areamap, edgesmap, edgesmapL (reversed, thx compiler) */
148 mstencil.stencil[0].func = PIPE_FUNC_EQUAL;
149 mstencil.stencil[0].zpass_op = PIPE_STENCIL_OP_KEEP;
150 cso_set_depth_stencil_alpha(p->cso, &mstencil);
151
152 pp_filter_setup_in(p, ppq->areamaptex);
153 pp_filter_setup_out(p, ppq->inner_tmp[1]);
154
155 u_sampler_view_default_template(&v_tmp, ppq->inner_tmp[0],
156 ppq->inner_tmp[0]->format);
157 arr[1] = arr[2] = p->pipe->create_sampler_view(p->pipe,
158 ppq->inner_tmp[0], &v_tmp);
159
160 pp_filter_set_clear_fb(p);
161
162 {
163 const struct pipe_sampler_state *samplers[] =
164 {&p->sampler_point, &p->sampler_point, &p->sampler};
165 cso_set_samplers(p->cso, PIPE_SHADER_FRAGMENT, 3, samplers);
166 }
167
168 arr[0] = p->view;
169 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 3, 0, false, arr);
170
171 cso_set_vertex_shader_handle(p->cso, ppq->shaders[n][0]); /* passvs */
172 cso_set_fragment_shader_handle(p->cso, ppq->shaders[n][3]);
173
174 pp_filter_draw(p);
175 pp_filter_end_pass(p);
176 pipe_sampler_view_reference(&arr[1], NULL);
177
178
179 /* Third pass: smoothed edges */
180 /* Sampler order: colormap, blendmap (wtf compiler) */
181 pp_filter_setup_in(p, ppq->inner_tmp[1]);
182 pp_filter_setup_out(p, out);
183
184 pp_filter_set_fb(p);
185
186 /* Blit the input to the output */
187 pp_blit(p->pipe, in, 0, 0,
188 w, h, 0, p->framebuffer.cbufs[0],
189 0, 0, w, h);
190
191 u_sampler_view_default_template(&v_tmp, in, in->format);
192 arr[0] = p->pipe->create_sampler_view(p->pipe, in, &v_tmp);
193
194 {
195 const struct pipe_sampler_state *samplers[] =
196 {&p->sampler_point, &p->sampler_point};
197 cso_set_samplers(p->cso, PIPE_SHADER_FRAGMENT, 2, samplers);
198 }
199
200 arr[1] = p->view;
201 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 2, 0, false, arr);
202
203 cso_set_vertex_shader_handle(p->cso, ppq->shaders[n][1]); /* offsetvs */
204 cso_set_fragment_shader_handle(p->cso, ppq->shaders[n][4]);
205
206 p->blend.rt[0].blend_enable = 1;
207 cso_set_blend(p->cso, &p->blend);
208
209 pp_filter_draw(p);
210 pp_filter_end_pass(p);
211 pipe_sampler_view_reference(&arr[0], NULL);
212
213 p->blend.rt[0].blend_enable = 0;
214 p->framebuffer.zsbuf = NULL;
215 }
216
217 /** The init function of the MLAA filter. */
218 static bool
pp_jimenezmlaa_init_run(struct pp_queue_t * ppq,unsigned int n,unsigned int val,bool iscolor)219 pp_jimenezmlaa_init_run(struct pp_queue_t *ppq, unsigned int n,
220 unsigned int val, bool iscolor)
221 {
222
223 struct pipe_box box;
224 struct pipe_resource res;
225 char *tmp_text = NULL;
226
227 tmp_text = CALLOC(sizeof(blend2fs_1) + sizeof(blend2fs_2) +
228 IMM_SPACE, sizeof(char));
229
230 if (!tmp_text) {
231 pp_debug("Failed to allocate shader space\n");
232 return FALSE;
233 }
234
235 pp_debug("mlaa: using %u max search steps\n", val);
236
237 sprintf(tmp_text, "%s"
238 "IMM FLT32 { %.8f, 0.0000, 0.0000, 0.0000}\n"
239 "%s\n", blend2fs_1, (float) val, blend2fs_2);
240
241 memset(&res, 0, sizeof(res));
242
243 res.target = PIPE_TEXTURE_2D;
244 res.format = PIPE_FORMAT_R8G8_UNORM;
245 res.width0 = res.height0 = 165;
246 res.bind = PIPE_BIND_SAMPLER_VIEW;
247 res.usage = PIPE_USAGE_DEFAULT;
248 res.depth0 = res.array_size = res.nr_samples = res.nr_storage_samples = 1;
249
250 if (!ppq->p->screen->is_format_supported(ppq->p->screen, res.format,
251 res.target, 1, 1, res.bind))
252 pp_debug("Areamap format not supported\n");
253
254 ppq->areamaptex = ppq->p->screen->resource_create(ppq->p->screen, &res);
255
256 if (ppq->areamaptex == NULL) {
257 pp_debug("Failed to allocate area map texture\n");
258 goto fail;
259 }
260
261 u_box_2d(0, 0, 165, 165, &box);
262
263 ppq->p->pipe->texture_subdata(ppq->p->pipe, ppq->areamaptex, 0,
264 PIPE_MAP_WRITE, &box,
265 areamap, 165 * 2, sizeof(areamap));
266
267 ppq->shaders[n][1] = pp_tgsi_to_state(ppq->p->pipe, offsetvs, true,
268 "offsetvs");
269 if (iscolor)
270 ppq->shaders[n][2] = pp_tgsi_to_state(ppq->p->pipe, color1fs,
271 false, "color1fs");
272 else
273 ppq->shaders[n][2] = pp_tgsi_to_state(ppq->p->pipe, depth1fs,
274 false, "depth1fs");
275 ppq->shaders[n][3] = pp_tgsi_to_state(ppq->p->pipe, tmp_text, false,
276 "blend2fs");
277 ppq->shaders[n][4] = pp_tgsi_to_state(ppq->p->pipe, neigh3fs, false,
278 "neigh3fs");
279
280 FREE(tmp_text);
281
282 return TRUE;
283
284 fail:
285
286 FREE(tmp_text);
287
288 /*
289 * Call the common free function for destruction of partially initialized
290 * resources.
291 */
292 pp_jimenezmlaa_free(ppq, n);
293
294 return FALSE;
295 }
296
297 /** Short wrapper to init the depth version. */
298 bool
pp_jimenezmlaa_init(struct pp_queue_t * ppq,unsigned int n,unsigned int val)299 pp_jimenezmlaa_init(struct pp_queue_t *ppq, unsigned int n, unsigned int val)
300 {
301 return pp_jimenezmlaa_init_run(ppq, n, val, false);
302 }
303
304 /** Short wrapper to init the color version. */
305 bool
pp_jimenezmlaa_init_color(struct pp_queue_t * ppq,unsigned int n,unsigned int val)306 pp_jimenezmlaa_init_color(struct pp_queue_t *ppq, unsigned int n,
307 unsigned int val)
308 {
309 return pp_jimenezmlaa_init_run(ppq, n, val, true);
310 }
311
312 /** Short wrapper to run the depth version. */
313 void
pp_jimenezmlaa(struct pp_queue_t * ppq,struct pipe_resource * in,struct pipe_resource * out,unsigned int n)314 pp_jimenezmlaa(struct pp_queue_t *ppq, struct pipe_resource *in,
315 struct pipe_resource *out, unsigned int n)
316 {
317 if (!ppq->depth) {
318 return;
319 }
320 pp_jimenezmlaa_run(ppq, in, out, n, false);
321 }
322
323 /** Short wrapper to run the color version. */
324 void
pp_jimenezmlaa_color(struct pp_queue_t * ppq,struct pipe_resource * in,struct pipe_resource * out,unsigned int n)325 pp_jimenezmlaa_color(struct pp_queue_t *ppq, struct pipe_resource *in,
326 struct pipe_resource *out, unsigned int n)
327 {
328 pp_jimenezmlaa_run(ppq, in, out, n, true);
329 }
330
331
332 /**
333 * Short wrapper to free the mlaa filter resources. Shaders are freed in
334 * the common code in pp_free.
335 */
336 void
pp_jimenezmlaa_free(struct pp_queue_t * ppq,unsigned int n)337 pp_jimenezmlaa_free(struct pp_queue_t *ppq, unsigned int n)
338 {
339 pipe_resource_reference(&ppq->areamaptex, NULL);
340 }
341
342