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