1 /**************************************************************************
3 * Copyright 2011 Lauri Kasanen
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "pipe/p_compiler.h"
30 #include "postprocess/filters.h"
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"
40 /** Initialize the post-processing queue. */
42 pp_init(struct pipe_context
*pipe
, const unsigned int *enabled
,
43 struct cso_context
*cso
)
45 unsigned int num_filters
= 0;
46 unsigned int curpos
= 0, i
, tmp_req
= 0;
47 struct pp_queue_t
*ppq
;
49 pp_debug("Initializing the post-processing queue.\n");
51 /* How many filters were requested? */
52 for (i
= 0; i
< PP_FILTERS
; i
++) {
59 ppq
= CALLOC(1, sizeof(struct pp_queue_t
));
62 pp_debug("Unable to allocate memory for ppq.\n");
66 ppq
->pp_queue
= CALLOC(num_filters
, sizeof(pp_func
));
67 if (ppq
->pp_queue
== NULL
) {
68 pp_debug("Unable to allocate memory for pp_queue.\n");
72 ppq
->shaders
= CALLOC(num_filters
, sizeof(void *));
73 ppq
->filters
= CALLOC(num_filters
, sizeof(unsigned int));
75 if ((ppq
->shaders
== NULL
) ||
76 (ppq
->filters
== NULL
)) {
77 pp_debug("Unable to allocate memory for shaders and filter arrays.\n");
81 ppq
->p
= pp_init_prog(ppq
, pipe
, cso
);
83 pp_debug("pp_init_prog returned NULL.\n");
87 /* Add the enabled filters to the queue, in order */
89 for (i
= 0; i
< PP_FILTERS
; i
++) {
91 ppq
->pp_queue
[curpos
] = pp_filters
[i
].main
;
92 tmp_req
= MAX2(tmp_req
, pp_filters
[i
].inner_tmps
);
93 ppq
->filters
[curpos
] = i
;
95 if (pp_filters
[i
].shaders
) {
96 ppq
->shaders
[curpos
] =
97 CALLOC(pp_filters
[i
].shaders
+ 1, sizeof(void *));
98 if (!ppq
->shaders
[curpos
]) {
99 pp_debug("Unable to allocate memory for shader list.\n");
104 /* Call the initialization function for the filter. */
105 if (!pp_filters
[i
].init(ppq
, curpos
, enabled
[i
])) {
106 pp_debug("Initialization for filter %u failed.\n", i
);
114 ppq
->p
->blitctx
= util_create_blit(ppq
->p
->pipe
, cso
);
116 if (ppq
->p
->blitctx
== NULL
) {
117 pp_debug("Unable to create a blit context.\n");
121 ppq
->n_filters
= curpos
;
122 ppq
->n_tmp
= (curpos
> 2 ? 2 : 1);
123 ppq
->n_inner_tmp
= tmp_req
;
125 ppq
->fbos_init
= false;
127 for (i
= 0; i
< curpos
; i
++)
128 ppq
->shaders
[i
][0] = ppq
->p
->passvs
;
130 pp_debug("Queue successfully allocated. %u filter(s).\n", curpos
);
137 /* Assign curpos, since we only need to destroy initialized filters. */
138 ppq
->n_filters
= curpos
;
140 /* Call the common free function which must handle partial initialization. */
147 /** Free any allocated FBOs (temp buffers). Called after resizing for example. */
149 pp_free_fbos(struct pp_queue_t
*ppq
)
157 for (i
= 0; i
< ppq
->n_tmp
; i
++) {
158 pipe_surface_reference(&ppq
->tmps
[i
], NULL
);
159 pipe_resource_reference(&ppq
->tmp
[i
], NULL
);
161 for (i
= 0; i
< ppq
->n_inner_tmp
; i
++) {
162 pipe_surface_reference(&ppq
->inner_tmps
[i
], NULL
);
163 pipe_resource_reference(&ppq
->inner_tmp
[i
], NULL
);
165 pipe_surface_reference(&ppq
->stencils
, NULL
);
166 pipe_resource_reference(&ppq
->stencil
, NULL
);
168 ppq
->fbos_init
= false;
172 * Free the pp queue. Called on context termination and failure in
176 pp_free(struct pp_queue_t
*ppq
)
183 /* Only destroy created contexts. */
184 if (ppq
->p
->blitctx
) {
185 util_destroy_blit(ppq
->p
->blitctx
);
188 if (ppq
->p
->pipe
&& ppq
->filters
&& ppq
->shaders
) {
189 for (i
= 0; i
< ppq
->n_filters
; i
++) {
190 unsigned int filter
= ppq
->filters
[i
];
192 if (ppq
->shaders
[i
] == NULL
) {
197 * Common shader destruction code for all postprocessing
200 for (j
= 0; j
< pp_filters
[filter
].shaders
; j
++) {
201 if (ppq
->shaders
[i
][j
] == NULL
) {
202 /* We reached the end of initialized shaders. */
206 if (ppq
->shaders
[i
][j
] == ppq
->p
->passvs
) {
212 assert(ppq
->p
->pipe
);
214 if (j
>= pp_filters
[filter
].verts
) {
215 assert(ppq
->p
->pipe
->delete_fs_state
);
216 ppq
->p
->pipe
->delete_fs_state(ppq
->p
->pipe
,
218 ppq
->shaders
[i
][j
] = NULL
;
220 assert(ppq
->p
->pipe
->delete_vs_state
);
221 ppq
->p
->pipe
->delete_vs_state(ppq
->p
->pipe
,
223 ppq
->shaders
[i
][j
] = NULL
;
227 /* Finally call each filter type's free functionality. */
228 pp_filters
[filter
].free(ppq
, i
);
237 * Handle partial initialization for common resource destruction
238 * in the create path.
247 pp_debug("Queue taken down.\n");
250 /** Internal debug function. Should be available to final users. */
252 pp_debug(const char *fmt
, ...)
256 if (!debug_get_bool_option("PP_DEBUG", FALSE
))
260 _debug_vprintf(fmt
, ap
);
264 /** Allocate the temp FBOs. Called on makecurrent and resize. */
266 pp_init_fbos(struct pp_queue_t
*ppq
, unsigned int w
,
270 struct program
*p
= ppq
->p
; /* The lazy will inherit the earth */
273 struct pipe_resource tmp_res
;
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
,
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
;
288 tmp_res
.array_size
= 1;
289 tmp_res
.last_level
= 0;
290 tmp_res
.bind
= PIPE_BIND_RENDER_TARGET
;
292 if (!p
->screen
->is_format_supported(p
->screen
, tmp_res
.format
,
293 tmp_res
.target
, 1, tmp_res
.bind
))
294 pp_debug("Temp buffers' format fail\n");
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
);
300 if (!ppq
->tmp
[i
] || !ppq
->tmps
[i
])
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
,
310 if (!ppq
->inner_tmp
[i
] || !ppq
->inner_tmps
[i
])
314 tmp_res
.bind
= PIPE_BIND_DEPTH_STENCIL
;
316 tmp_res
.format
= p
->surf
.format
= PIPE_FORMAT_S8_UINT_Z24_UNORM
;
318 if (!p
->screen
->is_format_supported(p
->screen
, tmp_res
.format
,
319 tmp_res
.target
, 1, tmp_res
.bind
)) {
321 tmp_res
.format
= p
->surf
.format
= PIPE_FORMAT_Z24_UNORM_S8_UINT
;
323 if (!p
->screen
->is_format_supported(p
->screen
, tmp_res
.format
,
324 tmp_res
.target
, 1, tmp_res
.bind
))
325 pp_debug("Temp Sbuffer format fail\n");
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
)
333 p
->framebuffer
.width
= w
;
334 p
->framebuffer
.height
= h
;
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
.scale
[3] = 1.0f
;
339 p
->viewport
.translate
[3] = 0.0f
;
341 ppq
->fbos_init
= true;
346 pp_debug("Failed to allocate temp buffers!\n");