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