pp_init.c revision 7219af5ec184d4f92682e75f3d992ae048005d6a
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 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 "pipe/p_compiler.h"
29
30#include "postprocess/filters.h"
31
32#include "pipe/p_screen.h"
33#include "util/u_inlines.h"
34#include "util/u_blit.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/** Initialize the post-processing queue. */
41struct pp_queue_t *
42pp_init(struct pipe_screen *pscreen, const unsigned int *enabled)
43{
44
45   unsigned int curpos = 0, i, tmp_req = 0;
46   struct pp_queue_t *ppq;
47   pp_func *tmp_q;
48
49   pp_debug("Initializing the post-processing queue.\n");
50
51   /* How many filters were requested? */
52   for (i = 0; i < PP_FILTERS; i++) {
53      if (enabled[i])
54         curpos++;
55   }
56   if (!curpos)
57      return NULL;
58
59   ppq = CALLOC(1, sizeof(struct pp_queue_t));
60   tmp_q = CALLOC(curpos, sizeof(pp_func));
61   ppq->shaders = CALLOC(curpos, sizeof(void *));
62   ppq->verts = CALLOC(curpos, sizeof(unsigned int));
63
64   if (!tmp_q || !ppq || !ppq->shaders || !ppq->verts)
65      goto error;
66
67   ppq->p = pp_init_prog(ppq, pscreen);
68   if (!ppq->p)
69      goto error;
70
71   /* Add the enabled filters to the queue, in order */
72   curpos = 0;
73   ppq->pp_queue = tmp_q;
74   for (i = 0; i < PP_FILTERS; i++) {
75      if (enabled[i]) {
76         ppq->pp_queue[curpos] = pp_filters[i].main;
77         tmp_req = MAX2(tmp_req, pp_filters[i].inner_tmps);
78
79         if (pp_filters[i].shaders) {
80            ppq->shaders[curpos] =
81               CALLOC(pp_filters[i].shaders + 1, sizeof(void *));
82            ppq->verts[curpos] = pp_filters[i].verts;
83            if (!ppq->shaders[curpos])
84               goto error;
85         }
86         pp_filters[i].init(ppq, curpos, enabled[i]);
87
88         curpos++;
89      }
90   }
91
92   ppq->p->blitctx = util_create_blit(ppq->p->pipe, ppq->p->cso);
93   if (!ppq->p->blitctx)
94      goto error;
95
96   ppq->n_filters = curpos;
97   ppq->n_tmp = (curpos > 2 ? 2 : 1);
98   ppq->n_inner_tmp = tmp_req;
99
100   ppq->fbos_init = false;
101
102   for (i = 0; i < curpos; i++)
103      ppq->shaders[i][0] = ppq->p->passvs;
104
105   pp_debug("Queue successfully allocated. %u filter(s).\n", curpos);
106
107   return ppq;
108
109 error:
110   pp_debug("Error setting up pp\n");
111
112   if (ppq)
113      FREE(ppq->p);
114   FREE(ppq);
115   FREE(tmp_q);
116
117   return NULL;
118}
119
120/** Free any allocated FBOs (temp buffers). Called after resizing for example. */
121void
122pp_free_fbos(struct pp_queue_t *ppq)
123{
124
125   unsigned int i;
126
127   if (!ppq->fbos_init)
128      return;
129
130   for (i = 0; i < ppq->n_tmp; i++) {
131      pipe_surface_reference(&ppq->tmps[i], NULL);
132      pipe_resource_reference(&ppq->tmp[i], NULL);
133   }
134   for (i = 0; i < ppq->n_inner_tmp; i++) {
135      pipe_surface_reference(&ppq->inner_tmps[i], NULL);
136      pipe_resource_reference(&ppq->inner_tmp[i], NULL);
137   }
138   pipe_resource_reference(&ppq->depth, NULL);
139   pipe_surface_reference(&ppq->stencils, NULL);
140   pipe_resource_reference(&ppq->stencil, NULL);
141
142   ppq->fbos_init = false;
143}
144
145/** Free the pp queue. Called on context termination. */
146void
147pp_free(struct pp_queue_t *ppq)
148{
149
150   unsigned int i, j;
151
152   pp_free_fbos(ppq);
153
154   util_destroy_blit(ppq->p->blitctx);
155
156   cso_set_fragment_sampler_views(ppq->p->cso, 0, NULL);
157   cso_release_all(ppq->p->cso);
158
159   for (i = 0; i < ppq->n_filters; i++) {
160      for (j = 0; j < PP_MAX_PASSES && ppq->shaders[i][j]; j++) {
161         if (j >= ppq->verts[i]) {
162            ppq->p->pipe->delete_fs_state(ppq->p->pipe, ppq->shaders[i][j]);
163            ppq->shaders[i][j] = NULL;
164         }
165         else if (ppq->shaders[i][j] != ppq->p->passvs) {
166            ppq->p->pipe->delete_vs_state(ppq->p->pipe, ppq->shaders[i][j]);
167            ppq->shaders[i][j] = NULL;
168         }
169      }
170   }
171
172   cso_destroy_context(ppq->p->cso);
173   ppq->p->pipe->destroy(ppq->p->pipe);
174
175   FREE(ppq->p);
176   FREE(ppq->pp_queue);
177   FREE(ppq);
178
179   pp_debug("Queue taken down.\n");
180}
181
182/** Internal debug function. Should be available to final users. */
183void
184pp_debug(const char *fmt, ...)
185{
186   va_list ap;
187
188   if (!debug_get_bool_option("PP_DEBUG", FALSE))
189      return;
190
191   va_start(ap, fmt);
192   _debug_vprintf(fmt, ap);
193   va_end(ap);
194}
195
196/** Allocate the temp FBOs. Called on makecurrent and resize. */
197void
198pp_init_fbos(struct pp_queue_t *ppq, unsigned int w,
199             unsigned int h, struct pipe_resource *indepth)
200{
201
202   struct program *p = ppq->p;  /* The lazy will inherit the earth */
203
204   unsigned int i;
205   struct pipe_resource tmp_res;
206
207   if (ppq->fbos_init)
208      return;
209
210   pp_debug("Initializing FBOs, size %ux%u\n", w, h);
211   pp_debug("Requesting %u temps and %u inner temps\n", ppq->n_tmp,
212            ppq->n_inner_tmp);
213
214   memset(&tmp_res, 0, sizeof(tmp_res));
215   tmp_res.target = PIPE_TEXTURE_2D;
216   tmp_res.format = p->surf.format = PIPE_FORMAT_B8G8R8A8_UNORM;
217   tmp_res.width0 = w;
218   tmp_res.height0 = h;
219   tmp_res.depth0 = 1;
220   tmp_res.array_size = 1;
221   tmp_res.last_level = 0;
222   tmp_res.bind = p->surf.usage = PIPE_BIND_RENDER_TARGET;
223
224   if (!p->screen->is_format_supported(p->screen, tmp_res.format,
225                                       tmp_res.target, 1, tmp_res.bind))
226      pp_debug("Temp buffers' format fail\n");
227
228   for (i = 0; i < ppq->n_tmp; i++) {
229      ppq->tmp[i] = p->screen->resource_create(p->screen, &tmp_res);
230      ppq->tmps[i] = p->pipe->create_surface(p->pipe, ppq->tmp[i], &p->surf);
231
232      if (!ppq->tmp[i] || !ppq->tmps[i])
233         goto error;
234   }
235
236   for (i = 0; i < ppq->n_inner_tmp; i++) {
237      ppq->inner_tmp[i] = p->screen->resource_create(p->screen, &tmp_res);
238      ppq->inner_tmps[i] = p->pipe->create_surface(p->pipe,
239                                                   ppq->inner_tmp[i],
240                                                   &p->surf);
241
242      if (!ppq->inner_tmp[i] || !ppq->inner_tmps[i])
243         goto error;
244   }
245
246   tmp_res.format = p->surf.format = indepth->format;
247   tmp_res.bind = p->surf.usage = PIPE_BIND_DEPTH_STENCIL;
248   pipe_resource_reference(&ppq->depth, indepth);
249   if (!ppq->depth)
250      goto error;
251
252   tmp_res.format = p->surf.format = PIPE_FORMAT_S8_UINT_Z24_UNORM;
253
254   if (!p->screen->is_format_supported(p->screen, tmp_res.format,
255                                       tmp_res.target, 1, tmp_res.bind)) {
256
257      tmp_res.format = p->surf.format = PIPE_FORMAT_Z24_UNORM_S8_UINT;
258
259      if (!p->screen->is_format_supported(p->screen, tmp_res.format,
260                                          tmp_res.target, 1, tmp_res.bind))
261         pp_debug("Temp Sbuffer format fail\n");
262   }
263
264   ppq->stencil = p->screen->resource_create(p->screen, &tmp_res);
265   ppq->stencils = p->pipe->create_surface(p->pipe, ppq->stencil, &p->surf);
266   if (!ppq->stencil || !ppq->stencils)
267      goto error;
268
269
270   p->framebuffer.width = w;
271   p->framebuffer.height = h;
272
273   p->viewport.scale[0] = p->viewport.translate[0] = (float) w / 2.0;
274   p->viewport.scale[1] = p->viewport.translate[1] = (float) h / 2.0;
275   p->viewport.scale[3] = 1.0f;
276   p->viewport.translate[3] = 0.0f;
277
278   ppq->fbos_init = true;
279
280   return;
281
282 error:
283   pp_debug("Failed to allocate temp buffers!\n");
284}
285