1 /**************************************************************************
2 *
3 * Copyright 2011 Lauri Kasanen
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/compiler.h"
29
30 #include "postprocess/filters.h"
31 #include "postprocess/pp_private.h"
32
33 #include "pipe/p_screen.h"
34 #include "util/u_inlines.h"
35 #include "util/u_math.h"
36 #include "util/u_debug.h"
37 #include "util/u_memory.h"
38 #include "cso_cache/cso_context.h"
39
40 const struct pp_filter_t pp_filters[PP_FILTERS] = {
41 /* name inner shaders verts init run free */
42 { "pp_noblue", 0, 2, 1, pp_noblue_init, pp_nocolor, pp_nocolor_free },
43 { "pp_nogreen", 0, 2, 1, pp_nogreen_init, pp_nocolor, pp_nocolor_free },
44 { "pp_nored", 0, 2, 1, pp_nored_init, pp_nocolor, pp_nocolor_free },
45 { "pp_celshade", 0, 2, 1, pp_celshade_init, pp_nocolor, pp_celshade_free },
46 { "pp_jimenezmlaa", 2, 5, 2, pp_jimenezmlaa_init, pp_jimenezmlaa, pp_jimenezmlaa_free },
47 { "pp_jimenezmlaa_color", 2, 5, 2, pp_jimenezmlaa_init_color, pp_jimenezmlaa_color, pp_jimenezmlaa_free },
48 };
49
50 /** Initialize the post-processing queue. */
51 struct pp_queue_t *
pp_init(struct pipe_context * pipe,const unsigned int * enabled,struct cso_context * cso,struct st_context * st,pp_st_invalidate_state_func st_invalidate_state)52 pp_init(struct pipe_context *pipe, const unsigned int *enabled,
53 struct cso_context *cso, struct st_context *st,
54 pp_st_invalidate_state_func st_invalidate_state)
55 {
56 unsigned int num_filters = 0;
57 unsigned int curpos = 0, i, tmp_req = 0;
58 struct pp_queue_t *ppq;
59
60 pp_debug("Initializing the post-processing queue.\n");
61
62 /* How many filters were requested? */
63 for (i = 0; i < PP_FILTERS; i++) {
64 if (enabled[i])
65 num_filters++;
66 }
67 if (num_filters == 0)
68 return NULL;
69
70 ppq = CALLOC(1, sizeof(struct pp_queue_t));
71
72 if (!ppq) {
73 pp_debug("Unable to allocate memory for ppq.\n");
74 goto error;
75 }
76
77 ppq->pp_queue = CALLOC(num_filters, sizeof(pp_func));
78 if (ppq->pp_queue == NULL) {
79 pp_debug("Unable to allocate memory for pp_queue.\n");
80 goto error;
81 }
82
83 ppq->shaders = CALLOC(num_filters, sizeof(void *));
84 ppq->filters = CALLOC(num_filters, sizeof(unsigned int));
85
86 if ((ppq->shaders == NULL) ||
87 (ppq->filters == NULL)) {
88 pp_debug("Unable to allocate memory for shaders and filter arrays.\n");
89 goto error;
90 }
91
92 ppq->p = pp_init_prog(ppq, pipe, cso, st, st_invalidate_state);
93 if (ppq->p == NULL) {
94 pp_debug("pp_init_prog returned NULL.\n");
95 goto error;
96 }
97
98 /* Add the enabled filters to the queue, in order */
99 curpos = 0;
100 for (i = 0; i < PP_FILTERS; i++) {
101 if (enabled[i]) {
102 ppq->pp_queue[curpos] = pp_filters[i].main;
103 tmp_req = MAX2(tmp_req, pp_filters[i].inner_tmps);
104 ppq->filters[curpos] = i;
105
106 if (pp_filters[i].shaders) {
107 ppq->shaders[curpos] =
108 CALLOC(pp_filters[i].shaders + 1, sizeof(void *));
109 if (!ppq->shaders[curpos]) {
110 pp_debug("Unable to allocate memory for shader list.\n");
111 goto error;
112 }
113 }
114
115 /* Call the initialization function for the filter. */
116 if (!pp_filters[i].init(ppq, curpos, enabled[i])) {
117 pp_debug("Initialization for filter %u failed.\n", i);
118 goto error;
119 }
120
121 curpos++;
122 }
123 }
124
125 ppq->n_filters = curpos;
126 ppq->n_tmp = (curpos > 2 ? 2 : 1);
127 ppq->n_inner_tmp = tmp_req;
128
129 ppq->fbos_init = false;
130
131 for (i = 0; i < curpos; i++)
132 ppq->shaders[i][0] = ppq->p->passvs;
133
134 pp_debug("Queue successfully allocated. %u filter(s).\n", curpos);
135
136 return ppq;
137
138 error:
139
140 if (ppq) {
141 /* Assign curpos, since we only need to destroy initialized filters. */
142 ppq->n_filters = curpos;
143
144 /* Call the common free function which must handle partial initialization. */
145 pp_free(ppq);
146 }
147
148 return NULL;
149 }
150
151 /** Free any allocated FBOs (temp buffers). Called after resizing for example. */
152 void
pp_free_fbos(struct pp_queue_t * ppq)153 pp_free_fbos(struct pp_queue_t *ppq)
154 {
155
156 unsigned int i;
157
158 if (!ppq->fbos_init)
159 return;
160
161 for (i = 0; i < ppq->n_tmp; i++) {
162 pipe_surface_reference(&ppq->tmps[i], NULL);
163 pipe_resource_reference(&ppq->tmp[i], NULL);
164 }
165 for (i = 0; i < ppq->n_inner_tmp; i++) {
166 pipe_surface_reference(&ppq->inner_tmps[i], NULL);
167 pipe_resource_reference(&ppq->inner_tmp[i], NULL);
168 }
169 pipe_surface_reference(&ppq->stencils, NULL);
170 pipe_resource_reference(&ppq->stencil, NULL);
171
172 ppq->fbos_init = false;
173 }
174
175 /**
176 * Free the pp queue. Called on context termination and failure in
177 * pp_init.
178 */
179 void
pp_free(struct pp_queue_t * ppq)180 pp_free(struct pp_queue_t *ppq)
181 {
182 unsigned int i, j;
183
184 if (!ppq)
185 return;
186
187 pp_free_fbos(ppq);
188
189 if (ppq->p) {
190 if (ppq->p->pipe && ppq->filters && ppq->shaders) {
191 for (i = 0; i < ppq->n_filters; i++) {
192 unsigned int filter = ppq->filters[i];
193
194 if (ppq->shaders[i] == NULL) {
195 continue;
196 }
197
198 /*
199 * Common shader destruction code for all postprocessing
200 * filters.
201 */
202 for (j = 0; j < pp_filters[filter].shaders; j++) {
203 if (ppq->shaders[i][j] == NULL) {
204 /* We reached the end of initialized shaders. */
205 break;
206 }
207
208 if (ppq->shaders[i][j] == ppq->p->passvs) {
209 continue;
210 }
211
212 assert(ppq);
213 assert(ppq->p);
214 assert(ppq->p->pipe);
215
216 if (j >= pp_filters[filter].verts) {
217 assert(ppq->p->pipe->delete_fs_state);
218 ppq->p->pipe->delete_fs_state(ppq->p->pipe,
219 ppq->shaders[i][j]);
220 ppq->shaders[i][j] = NULL;
221 } else {
222 assert(ppq->p->pipe->delete_vs_state);
223 ppq->p->pipe->delete_vs_state(ppq->p->pipe,
224 ppq->shaders[i][j]);
225 ppq->shaders[i][j] = NULL;
226 }
227 }
228
229 /* Finally call each filter type's free functionality. */
230 pp_filters[filter].free(ppq, i);
231 }
232 }
233
234 FREE(ppq->p);
235 }
236
237 /*
238 * Handle partial initialization for common resource destruction
239 * in the create path.
240 */
241 FREE(ppq->filters);
242 FREE(ppq->shaders);
243 FREE(ppq->pp_queue);
244
245 FREE(ppq);
246
247 pp_debug("Queue taken down.\n");
248 }
249
250 /** Internal debug function. Should be available to final users. */
251 void
pp_debug(const char * fmt,...)252 pp_debug(const char *fmt, ...)
253 {
254 va_list ap;
255
256 if (!debug_get_bool_option("PP_DEBUG", false))
257 return;
258
259 va_start(ap, fmt);
260 _debug_vprintf(fmt, ap);
261 va_end(ap);
262 }
263
264 /** Allocate the temp FBOs. Called on makecurrent and resize. */
265 void
pp_init_fbos(struct pp_queue_t * ppq,unsigned int w,unsigned int h)266 pp_init_fbos(struct pp_queue_t *ppq, unsigned int w,
267 unsigned int h)
268 {
269
270 struct pp_program *p = ppq->p; /* The lazy will inherit the earth */
271
272 unsigned int i;
273 struct pipe_resource tmp_res;
274
275 if (ppq->fbos_init)
276 return;
277
278 pp_debug("Initializing FBOs, size %ux%u\n", w, h);
279 pp_debug("Requesting %u temps and %u inner temps\n", ppq->n_tmp,
280 ppq->n_inner_tmp);
281
282 memset(&tmp_res, 0, sizeof(tmp_res));
283 tmp_res.target = PIPE_TEXTURE_2D;
284 tmp_res.format = p->surf.format = PIPE_FORMAT_B8G8R8A8_UNORM;
285 tmp_res.width0 = w;
286 tmp_res.height0 = h;
287 tmp_res.depth0 = 1;
288 tmp_res.array_size = 1;
289 tmp_res.last_level = 0;
290 tmp_res.bind = PIPE_BIND_RENDER_TARGET;
291
292 if (!p->screen->is_format_supported(p->screen, tmp_res.format,
293 tmp_res.target, 1, 1, tmp_res.bind))
294 pp_debug("Temp buffers' format fail\n");
295
296 for (i = 0; i < ppq->n_tmp; i++) {
297 ppq->tmp[i] = p->screen->resource_create(p->screen, &tmp_res);
298 ppq->tmps[i] = p->pipe->create_surface(p->pipe, ppq->tmp[i], &p->surf);
299
300 if (!ppq->tmp[i] || !ppq->tmps[i])
301 goto error;
302 }
303
304 for (i = 0; i < ppq->n_inner_tmp; i++) {
305 ppq->inner_tmp[i] = p->screen->resource_create(p->screen, &tmp_res);
306 ppq->inner_tmps[i] = p->pipe->create_surface(p->pipe,
307 ppq->inner_tmp[i],
308 &p->surf);
309
310 if (!ppq->inner_tmp[i] || !ppq->inner_tmps[i])
311 goto error;
312 }
313
314 tmp_res.bind = PIPE_BIND_DEPTH_STENCIL;
315
316 tmp_res.format = p->surf.format = PIPE_FORMAT_S8_UINT_Z24_UNORM;
317
318 if (!p->screen->is_format_supported(p->screen, tmp_res.format,
319 tmp_res.target, 1, 1, tmp_res.bind)) {
320
321 tmp_res.format = p->surf.format = PIPE_FORMAT_Z24_UNORM_S8_UINT;
322
323 if (!p->screen->is_format_supported(p->screen, tmp_res.format,
324 tmp_res.target, 1, 1, tmp_res.bind))
325 pp_debug("Temp Sbuffer format fail\n");
326 }
327
328 ppq->stencil = p->screen->resource_create(p->screen, &tmp_res);
329 ppq->stencils = p->pipe->create_surface(p->pipe, ppq->stencil, &p->surf);
330 if (!ppq->stencil || !ppq->stencils)
331 goto error;
332
333 p->framebuffer.width = w;
334 p->framebuffer.height = h;
335
336 p->viewport.scale[0] = p->viewport.translate[0] = (float) w / 2.0f;
337 p->viewport.scale[1] = p->viewport.translate[1] = (float) h / 2.0f;
338 p->viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
339 p->viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
340 p->viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
341 p->viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
342
343 ppq->fbos_init = true;
344
345 return;
346
347 error:
348 pp_debug("Failed to allocate temp buffers!\n");
349 }
350