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
)
46 unsigned int curpos
= 0, i
, tmp_req
= 0;
47 struct pp_queue_t
*ppq
;
50 pp_debug("Initializing the post-processing queue.\n");
52 /* How many filters were requested? */
53 for (i
= 0; i
< PP_FILTERS
; i
++) {
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));
65 if (!tmp_q
|| !ppq
|| !ppq
->shaders
|| !ppq
->verts
)
68 ppq
->p
= pp_init_prog(ppq
, pipe
, cso
);
72 /* Add the enabled filters to the queue, in order */
74 ppq
->pp_queue
= tmp_q
;
75 for (i
= 0; i
< PP_FILTERS
; i
++) {
77 ppq
->pp_queue
[curpos
] = pp_filters
[i
].main
;
78 tmp_req
= MAX2(tmp_req
, pp_filters
[i
].inner_tmps
);
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
])
87 pp_filters
[i
].init(ppq
, curpos
, enabled
[i
]);
93 ppq
->p
->blitctx
= util_create_blit(ppq
->p
->pipe
, cso
);
97 ppq
->n_filters
= curpos
;
98 ppq
->n_tmp
= (curpos
> 2 ? 2 : 1);
99 ppq
->n_inner_tmp
= tmp_req
;
101 ppq
->fbos_init
= false;
103 for (i
= 0; i
< curpos
; i
++)
104 ppq
->shaders
[i
][0] = ppq
->p
->passvs
;
106 pp_debug("Queue successfully allocated. %u filter(s).\n", curpos
);
111 pp_debug("Error setting up pp\n");
121 /** Free any allocated FBOs (temp buffers). Called after resizing for example. */
123 pp_free_fbos(struct pp_queue_t
*ppq
)
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
);
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
);
139 pipe_surface_reference(&ppq
->stencils
, NULL
);
140 pipe_resource_reference(&ppq
->stencil
, NULL
);
142 ppq
->fbos_init
= false;
145 /** Free the pp queue. Called on context termination. */
147 pp_free(struct pp_queue_t
*ppq
)
154 util_destroy_blit(ppq
->p
->blitctx
);
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
;
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
;
173 pp_debug("Queue taken down.\n");
176 /** Internal debug function. Should be available to final users. */
178 pp_debug(const char *fmt
, ...)
182 if (!debug_get_bool_option("PP_DEBUG", FALSE
))
186 _debug_vprintf(fmt
, ap
);
190 /** Allocate the temp FBOs. Called on makecurrent and resize. */
192 pp_init_fbos(struct pp_queue_t
*ppq
, unsigned int w
,
196 struct program
*p
= ppq
->p
; /* The lazy will inherit the earth */
199 struct pipe_resource tmp_res
;
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
,
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
;
214 tmp_res
.array_size
= 1;
215 tmp_res
.last_level
= 0;
216 tmp_res
.bind
= PIPE_BIND_RENDER_TARGET
;
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");
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
);
226 if (!ppq
->tmp
[i
] || !ppq
->tmps
[i
])
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
,
236 if (!ppq
->inner_tmp
[i
] || !ppq
->inner_tmps
[i
])
240 tmp_res
.bind
= PIPE_BIND_DEPTH_STENCIL
;
242 tmp_res
.format
= p
->surf
.format
= PIPE_FORMAT_S8_UINT_Z24_UNORM
;
244 if (!p
->screen
->is_format_supported(p
->screen
, tmp_res
.format
,
245 tmp_res
.target
, 1, tmp_res
.bind
)) {
247 tmp_res
.format
= p
->surf
.format
= PIPE_FORMAT_Z24_UNORM_S8_UINT
;
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");
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
)
260 p
->framebuffer
.width
= w
;
261 p
->framebuffer
.height
= h
;
263 p
->viewport
.scale
[0] = p
->viewport
.translate
[0] = (float) w
/ 2.0f
;
264 p
->viewport
.scale
[1] = p
->viewport
.translate
[1] = (float) h
/ 2.0f
;
265 p
->viewport
.scale
[3] = 1.0f
;
266 p
->viewport
.translate
[3] = 0.0f
;
268 ppq
->fbos_init
= true;
273 pp_debug("Failed to allocate temp buffers!\n");