1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2011 Lauri Kasanen
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg#include "pipe/p_compiler.h"
29848b8605Smrg
30848b8605Smrg#include "postprocess/filters.h"
31848b8605Smrg#include "postprocess/pp_private.h"
32848b8605Smrg
33848b8605Smrg#include "pipe/p_screen.h"
34848b8605Smrg#include "util/u_inlines.h"
35848b8605Smrg#include "util/u_math.h"
36848b8605Smrg#include "util/u_debug.h"
37848b8605Smrg#include "util/u_memory.h"
38848b8605Smrg#include "cso_cache/cso_context.h"
39848b8605Smrg
40848b8605Smrg/** Initialize the post-processing queue. */
41848b8605Smrgstruct pp_queue_t *
42848b8605Smrgpp_init(struct pipe_context *pipe, const unsigned int *enabled,
43848b8605Smrg        struct cso_context *cso)
44848b8605Smrg{
45848b8605Smrg   unsigned int num_filters = 0;
46848b8605Smrg   unsigned int curpos = 0, i, tmp_req = 0;
47848b8605Smrg   struct pp_queue_t *ppq;
48848b8605Smrg
49848b8605Smrg   pp_debug("Initializing the post-processing queue.\n");
50848b8605Smrg
51848b8605Smrg   /* How many filters were requested? */
52848b8605Smrg   for (i = 0; i < PP_FILTERS; i++) {
53848b8605Smrg      if (enabled[i])
54848b8605Smrg         num_filters++;
55848b8605Smrg   }
56848b8605Smrg   if (num_filters == 0)
57848b8605Smrg      return NULL;
58848b8605Smrg
59848b8605Smrg   ppq = CALLOC(1, sizeof(struct pp_queue_t));
60848b8605Smrg
61b8e80941Smrg   if (!ppq) {
62848b8605Smrg      pp_debug("Unable to allocate memory for ppq.\n");
63848b8605Smrg      goto error;
64848b8605Smrg   }
65848b8605Smrg
66848b8605Smrg   ppq->pp_queue = CALLOC(num_filters, sizeof(pp_func));
67848b8605Smrg   if (ppq->pp_queue == NULL) {
68848b8605Smrg      pp_debug("Unable to allocate memory for pp_queue.\n");
69848b8605Smrg      goto error;
70848b8605Smrg   }
71848b8605Smrg
72848b8605Smrg   ppq->shaders = CALLOC(num_filters, sizeof(void *));
73848b8605Smrg   ppq->filters = CALLOC(num_filters, sizeof(unsigned int));
74848b8605Smrg
75848b8605Smrg   if ((ppq->shaders == NULL) ||
76848b8605Smrg       (ppq->filters == NULL)) {
77848b8605Smrg      pp_debug("Unable to allocate memory for shaders and filter arrays.\n");
78848b8605Smrg      goto error;
79848b8605Smrg   }
80848b8605Smrg
81848b8605Smrg   ppq->p = pp_init_prog(ppq, pipe, cso);
82848b8605Smrg   if (ppq->p == NULL) {
83848b8605Smrg      pp_debug("pp_init_prog returned NULL.\n");
84848b8605Smrg      goto error;
85848b8605Smrg   }
86848b8605Smrg
87848b8605Smrg   /* Add the enabled filters to the queue, in order */
88848b8605Smrg   curpos = 0;
89848b8605Smrg   for (i = 0; i < PP_FILTERS; i++) {
90848b8605Smrg      if (enabled[i]) {
91848b8605Smrg         ppq->pp_queue[curpos] = pp_filters[i].main;
92848b8605Smrg         tmp_req = MAX2(tmp_req, pp_filters[i].inner_tmps);
93848b8605Smrg         ppq->filters[curpos] = i;
94848b8605Smrg
95848b8605Smrg         if (pp_filters[i].shaders) {
96848b8605Smrg            ppq->shaders[curpos] =
97848b8605Smrg               CALLOC(pp_filters[i].shaders + 1, sizeof(void *));
98848b8605Smrg            if (!ppq->shaders[curpos]) {
99848b8605Smrg               pp_debug("Unable to allocate memory for shader list.\n");
100848b8605Smrg               goto error;
101848b8605Smrg            }
102848b8605Smrg         }
103848b8605Smrg
104848b8605Smrg         /* Call the initialization function for the filter. */
105848b8605Smrg         if (!pp_filters[i].init(ppq, curpos, enabled[i])) {
106848b8605Smrg            pp_debug("Initialization for filter %u failed.\n", i);
107848b8605Smrg            goto error;
108848b8605Smrg         }
109848b8605Smrg
110848b8605Smrg         curpos++;
111848b8605Smrg      }
112848b8605Smrg   }
113848b8605Smrg
114848b8605Smrg   ppq->n_filters = curpos;
115848b8605Smrg   ppq->n_tmp = (curpos > 2 ? 2 : 1);
116848b8605Smrg   ppq->n_inner_tmp = tmp_req;
117848b8605Smrg
118848b8605Smrg   ppq->fbos_init = false;
119848b8605Smrg
120848b8605Smrg   for (i = 0; i < curpos; i++)
121848b8605Smrg      ppq->shaders[i][0] = ppq->p->passvs;
122848b8605Smrg
123848b8605Smrg   pp_debug("Queue successfully allocated. %u filter(s).\n", curpos);
124848b8605Smrg
125848b8605Smrg   return ppq;
126848b8605Smrg
127848b8605Smrg error:
128848b8605Smrg
129848b8605Smrg   if (ppq) {
130848b8605Smrg      /* Assign curpos, since we only need to destroy initialized filters. */
131848b8605Smrg      ppq->n_filters = curpos;
132848b8605Smrg
133848b8605Smrg      /* Call the common free function which must handle partial initialization. */
134848b8605Smrg      pp_free(ppq);
135848b8605Smrg   }
136848b8605Smrg
137848b8605Smrg   return NULL;
138848b8605Smrg}
139848b8605Smrg
140848b8605Smrg/** Free any allocated FBOs (temp buffers). Called after resizing for example. */
141848b8605Smrgvoid
142848b8605Smrgpp_free_fbos(struct pp_queue_t *ppq)
143848b8605Smrg{
144848b8605Smrg
145848b8605Smrg   unsigned int i;
146848b8605Smrg
147848b8605Smrg   if (!ppq->fbos_init)
148848b8605Smrg      return;
149848b8605Smrg
150848b8605Smrg   for (i = 0; i < ppq->n_tmp; i++) {
151848b8605Smrg      pipe_surface_reference(&ppq->tmps[i], NULL);
152848b8605Smrg      pipe_resource_reference(&ppq->tmp[i], NULL);
153848b8605Smrg   }
154848b8605Smrg   for (i = 0; i < ppq->n_inner_tmp; i++) {
155848b8605Smrg      pipe_surface_reference(&ppq->inner_tmps[i], NULL);
156848b8605Smrg      pipe_resource_reference(&ppq->inner_tmp[i], NULL);
157848b8605Smrg   }
158848b8605Smrg   pipe_surface_reference(&ppq->stencils, NULL);
159848b8605Smrg   pipe_resource_reference(&ppq->stencil, NULL);
160848b8605Smrg
161848b8605Smrg   ppq->fbos_init = false;
162848b8605Smrg}
163848b8605Smrg
164848b8605Smrg/**
165848b8605Smrg * Free the pp queue. Called on context termination and failure in
166848b8605Smrg * pp_init.
167848b8605Smrg */
168848b8605Smrgvoid
169848b8605Smrgpp_free(struct pp_queue_t *ppq)
170848b8605Smrg{
171848b8605Smrg   unsigned int i, j;
172848b8605Smrg
173848b8605Smrg   if (!ppq)
174848b8605Smrg      return;
175848b8605Smrg
176848b8605Smrg   pp_free_fbos(ppq);
177848b8605Smrg
178848b8605Smrg   if (ppq->p) {
179848b8605Smrg      if (ppq->p->pipe && ppq->filters && ppq->shaders) {
180848b8605Smrg         for (i = 0; i < ppq->n_filters; i++) {
181848b8605Smrg            unsigned int filter = ppq->filters[i];
182848b8605Smrg
183848b8605Smrg            if (ppq->shaders[i] == NULL) {
184848b8605Smrg               continue;
185848b8605Smrg            }
186848b8605Smrg
187848b8605Smrg            /*
188848b8605Smrg             * Common shader destruction code for all postprocessing
189848b8605Smrg             * filters.
190848b8605Smrg             */
191848b8605Smrg            for (j = 0; j < pp_filters[filter].shaders; j++) {
192848b8605Smrg               if (ppq->shaders[i][j] == NULL) {
193848b8605Smrg                  /* We reached the end of initialized shaders. */
194848b8605Smrg                  break;
195848b8605Smrg               }
196848b8605Smrg
197848b8605Smrg               if (ppq->shaders[i][j] == ppq->p->passvs) {
198848b8605Smrg                  continue;
199848b8605Smrg               }
200848b8605Smrg
201848b8605Smrg               assert(ppq);
202848b8605Smrg               assert(ppq->p);
203848b8605Smrg               assert(ppq->p->pipe);
204848b8605Smrg
205848b8605Smrg               if (j >= pp_filters[filter].verts) {
206848b8605Smrg                  assert(ppq->p->pipe->delete_fs_state);
207848b8605Smrg                  ppq->p->pipe->delete_fs_state(ppq->p->pipe,
208848b8605Smrg                                                ppq->shaders[i][j]);
209848b8605Smrg                  ppq->shaders[i][j] = NULL;
210848b8605Smrg               } else {
211848b8605Smrg                  assert(ppq->p->pipe->delete_vs_state);
212848b8605Smrg                  ppq->p->pipe->delete_vs_state(ppq->p->pipe,
213848b8605Smrg                                                ppq->shaders[i][j]);
214848b8605Smrg                  ppq->shaders[i][j] = NULL;
215848b8605Smrg               }
216848b8605Smrg            }
217848b8605Smrg
218848b8605Smrg            /* Finally call each filter type's free functionality. */
219848b8605Smrg            pp_filters[filter].free(ppq, i);
220848b8605Smrg         }
221848b8605Smrg      }
222848b8605Smrg
223848b8605Smrg      FREE(ppq->p);
224848b8605Smrg   }
225848b8605Smrg
226848b8605Smrg   /*
227848b8605Smrg    * Handle partial initialization for common resource destruction
228848b8605Smrg    * in the create path.
229848b8605Smrg    */
230848b8605Smrg   FREE(ppq->filters);
231848b8605Smrg   FREE(ppq->shaders);
232848b8605Smrg   FREE(ppq->pp_queue);
233848b8605Smrg
234848b8605Smrg   FREE(ppq);
235848b8605Smrg
236848b8605Smrg   pp_debug("Queue taken down.\n");
237848b8605Smrg}
238848b8605Smrg
239848b8605Smrg/** Internal debug function. Should be available to final users. */
240848b8605Smrgvoid
241848b8605Smrgpp_debug(const char *fmt, ...)
242848b8605Smrg{
243848b8605Smrg   va_list ap;
244848b8605Smrg
245848b8605Smrg   if (!debug_get_bool_option("PP_DEBUG", FALSE))
246848b8605Smrg      return;
247848b8605Smrg
248848b8605Smrg   va_start(ap, fmt);
249848b8605Smrg   _debug_vprintf(fmt, ap);
250848b8605Smrg   va_end(ap);
251848b8605Smrg}
252848b8605Smrg
253848b8605Smrg/** Allocate the temp FBOs. Called on makecurrent and resize. */
254848b8605Smrgvoid
255848b8605Smrgpp_init_fbos(struct pp_queue_t *ppq, unsigned int w,
256848b8605Smrg             unsigned int h)
257848b8605Smrg{
258848b8605Smrg
259848b8605Smrg   struct pp_program *p = ppq->p;  /* The lazy will inherit the earth */
260848b8605Smrg
261848b8605Smrg   unsigned int i;
262848b8605Smrg   struct pipe_resource tmp_res;
263848b8605Smrg
264848b8605Smrg   if (ppq->fbos_init)
265848b8605Smrg      return;
266848b8605Smrg
267848b8605Smrg   pp_debug("Initializing FBOs, size %ux%u\n", w, h);
268848b8605Smrg   pp_debug("Requesting %u temps and %u inner temps\n", ppq->n_tmp,
269848b8605Smrg            ppq->n_inner_tmp);
270848b8605Smrg
271848b8605Smrg   memset(&tmp_res, 0, sizeof(tmp_res));
272848b8605Smrg   tmp_res.target = PIPE_TEXTURE_2D;
273848b8605Smrg   tmp_res.format = p->surf.format = PIPE_FORMAT_B8G8R8A8_UNORM;
274848b8605Smrg   tmp_res.width0 = w;
275848b8605Smrg   tmp_res.height0 = h;
276848b8605Smrg   tmp_res.depth0 = 1;
277848b8605Smrg   tmp_res.array_size = 1;
278848b8605Smrg   tmp_res.last_level = 0;
279848b8605Smrg   tmp_res.bind = PIPE_BIND_RENDER_TARGET;
280848b8605Smrg
281848b8605Smrg   if (!p->screen->is_format_supported(p->screen, tmp_res.format,
282b8e80941Smrg                                       tmp_res.target, 1, 1, tmp_res.bind))
283848b8605Smrg      pp_debug("Temp buffers' format fail\n");
284848b8605Smrg
285848b8605Smrg   for (i = 0; i < ppq->n_tmp; i++) {
286848b8605Smrg      ppq->tmp[i] = p->screen->resource_create(p->screen, &tmp_res);
287848b8605Smrg      ppq->tmps[i] = p->pipe->create_surface(p->pipe, ppq->tmp[i], &p->surf);
288848b8605Smrg
289848b8605Smrg      if (!ppq->tmp[i] || !ppq->tmps[i])
290848b8605Smrg         goto error;
291848b8605Smrg   }
292848b8605Smrg
293848b8605Smrg   for (i = 0; i < ppq->n_inner_tmp; i++) {
294848b8605Smrg      ppq->inner_tmp[i] = p->screen->resource_create(p->screen, &tmp_res);
295848b8605Smrg      ppq->inner_tmps[i] = p->pipe->create_surface(p->pipe,
296848b8605Smrg                                                   ppq->inner_tmp[i],
297848b8605Smrg                                                   &p->surf);
298848b8605Smrg
299848b8605Smrg      if (!ppq->inner_tmp[i] || !ppq->inner_tmps[i])
300848b8605Smrg         goto error;
301848b8605Smrg   }
302848b8605Smrg
303848b8605Smrg   tmp_res.bind = PIPE_BIND_DEPTH_STENCIL;
304848b8605Smrg
305848b8605Smrg   tmp_res.format = p->surf.format = PIPE_FORMAT_S8_UINT_Z24_UNORM;
306848b8605Smrg
307848b8605Smrg   if (!p->screen->is_format_supported(p->screen, tmp_res.format,
308b8e80941Smrg                                       tmp_res.target, 1, 1, tmp_res.bind)) {
309848b8605Smrg
310848b8605Smrg      tmp_res.format = p->surf.format = PIPE_FORMAT_Z24_UNORM_S8_UINT;
311848b8605Smrg
312848b8605Smrg      if (!p->screen->is_format_supported(p->screen, tmp_res.format,
313b8e80941Smrg                                          tmp_res.target, 1, 1, tmp_res.bind))
314848b8605Smrg         pp_debug("Temp Sbuffer format fail\n");
315848b8605Smrg   }
316848b8605Smrg
317848b8605Smrg   ppq->stencil = p->screen->resource_create(p->screen, &tmp_res);
318848b8605Smrg   ppq->stencils = p->pipe->create_surface(p->pipe, ppq->stencil, &p->surf);
319848b8605Smrg   if (!ppq->stencil || !ppq->stencils)
320848b8605Smrg      goto error;
321848b8605Smrg
322848b8605Smrg   p->framebuffer.width = w;
323848b8605Smrg   p->framebuffer.height = h;
324848b8605Smrg
325848b8605Smrg   p->viewport.scale[0] = p->viewport.translate[0] = (float) w / 2.0f;
326848b8605Smrg   p->viewport.scale[1] = p->viewport.translate[1] = (float) h / 2.0f;
327848b8605Smrg
328848b8605Smrg   ppq->fbos_init = true;
329848b8605Smrg
330848b8605Smrg   return;
331848b8605Smrg
332848b8605Smrg error:
333848b8605Smrg   pp_debug("Failed to allocate temp buffers!\n");
334848b8605Smrg}
335