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