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 "postprocess.h"
29 #include "postprocess/pp_filters.h"
30 #include "postprocess/pp_private.h"
32 #include "util/u_inlines.h"
33 #include "util/u_sampler.h"
35 #include "tgsi/tgsi_parse.h"
39 pp_blit(struct pipe_context
*pipe
,
40 struct pipe_resource
*src_tex
,
44 struct pipe_surface
*dst
,
48 struct pipe_blit_info blit
;
50 memset(&blit
, 0, sizeof(blit
));
52 blit
.src
.resource
= src_tex
;
54 blit
.src
.format
= src_tex
->format
;
55 blit
.src
.box
.x
= srcX0
;
56 blit
.src
.box
.y
= srcY0
;
57 blit
.src
.box
.z
= srcZ0
;
58 blit
.src
.box
.width
= srcX1
- srcX0
;
59 blit
.src
.box
.height
= srcY1
- srcY0
;
60 blit
.src
.box
.depth
= 1;
62 blit
.dst
.resource
= dst
->texture
;
63 blit
.dst
.level
= dst
->u
.tex
.level
;
64 blit
.dst
.format
= dst
->format
;
65 blit
.dst
.box
.x
= dstX0
;
66 blit
.dst
.box
.y
= dstY0
;
68 blit
.dst
.box
.width
= dstX1
- dstX0
;
69 blit
.dst
.box
.height
= dstY1
- dstY0
;
70 blit
.dst
.box
.depth
= 1;
72 blit
.mask
= PIPE_MASK_RGBA
;
74 pipe
->blit(pipe
, &blit
);
78 * Main run function of the PP queue. Called on swapbuffers/flush.
80 * Runs all requested filters in order and handles shuffling the temp
84 pp_run(struct pp_queue_t
*ppq
, struct pipe_resource
*in
,
85 struct pipe_resource
*out
, struct pipe_resource
*indepth
)
87 struct pipe_resource
*refin
= NULL
, *refout
= NULL
;
89 struct cso_context
*cso
= ppq
->p
->cso
;
91 if (ppq
->n_filters
== 0)
94 assert(ppq
->pp_queue
);
97 if (in
->width0
!= ppq
->p
->framebuffer
.width
||
98 in
->height0
!= ppq
->p
->framebuffer
.height
) {
99 pp_debug("Resizing the temp pp buffers\n");
101 pp_init_fbos(ppq
, in
->width0
, in
->height0
);
104 if (in
== out
&& ppq
->n_filters
== 1) {
105 /* Make a copy of in to tmp[0] in this case. */
106 unsigned int w
= ppq
->p
->framebuffer
.width
;
107 unsigned int h
= ppq
->p
->framebuffer
.height
;
110 pp_blit(ppq
->p
->pipe
, in
, 0, 0,
111 w
, h
, 0, ppq
->tmps
[0],
117 /* save state (restored below) */
119 cso_save_depth_stencil_alpha(cso
);
120 cso_save_fragment_shader(cso
);
121 cso_save_framebuffer(cso
);
122 cso_save_tessctrl_shader(cso
);
123 cso_save_tesseval_shader(cso
);
124 cso_save_geometry_shader(cso
);
125 cso_save_rasterizer(cso
);
126 cso_save_sample_mask(cso
);
127 cso_save_min_samples(cso
);
128 cso_save_samplers(cso
, PIPE_SHADER_FRAGMENT
);
129 cso_save_fragment_sampler_views(cso
);
130 cso_save_stencil_ref(cso
);
131 cso_save_stream_outputs(cso
);
132 cso_save_vertex_elements(cso
);
133 cso_save_vertex_shader(cso
);
134 cso_save_viewport(cso
);
135 cso_save_aux_vertex_buffer_slot(cso
);
136 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_VERTEX
);
137 cso_save_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
138 cso_save_render_condition(cso
);
140 /* set default state */
141 cso_set_sample_mask(cso
, ~0);
142 cso_set_min_samples(cso
, 1);
143 cso_set_stream_outputs(cso
, 0, NULL
, NULL
);
144 cso_set_tessctrl_shader_handle(cso
, NULL
);
145 cso_set_tesseval_shader_handle(cso
, NULL
);
146 cso_set_geometry_shader_handle(cso
, NULL
);
147 cso_set_render_condition(cso
, NULL
, FALSE
, 0);
149 // Kept only for this frame.
150 pipe_resource_reference(&ppq
->depth
, indepth
);
151 pipe_resource_reference(&refin
, in
);
152 pipe_resource_reference(&refout
, out
);
154 switch (ppq
->n_filters
) {
156 /* Failsafe, but never reached. */
158 case 1: /* No temp buf */
159 ppq
->pp_queue
[0] (ppq
, in
, out
, 0);
161 case 2: /* One temp buf */
163 ppq
->pp_queue
[0] (ppq
, in
, ppq
->tmp
[0], 0);
164 ppq
->pp_queue
[1] (ppq
, ppq
->tmp
[0], out
, 1);
167 default: /* Two temp bufs */
169 ppq
->pp_queue
[0] (ppq
, in
, ppq
->tmp
[0], 0);
171 for (i
= 1; i
< (ppq
->n_filters
- 1); i
++) {
173 ppq
->pp_queue
[i
] (ppq
, ppq
->tmp
[1], ppq
->tmp
[0], i
);
176 ppq
->pp_queue
[i
] (ppq
, ppq
->tmp
[0], ppq
->tmp
[1], i
);
180 ppq
->pp_queue
[i
] (ppq
, ppq
->tmp
[1], out
, i
);
183 ppq
->pp_queue
[i
] (ppq
, ppq
->tmp
[0], out
, i
);
188 /* restore state we changed */
189 cso_restore_blend(cso
);
190 cso_restore_depth_stencil_alpha(cso
);
191 cso_restore_fragment_shader(cso
);
192 cso_restore_framebuffer(cso
);
193 cso_restore_tessctrl_shader(cso
);
194 cso_restore_tesseval_shader(cso
);
195 cso_restore_geometry_shader(cso
);
196 cso_restore_rasterizer(cso
);
197 cso_restore_sample_mask(cso
);
198 cso_restore_min_samples(cso
);
199 cso_restore_samplers(cso
, PIPE_SHADER_FRAGMENT
);
200 cso_restore_fragment_sampler_views(cso
);
201 cso_restore_stencil_ref(cso
);
202 cso_restore_stream_outputs(cso
);
203 cso_restore_vertex_elements(cso
);
204 cso_restore_vertex_shader(cso
);
205 cso_restore_viewport(cso
);
206 cso_restore_aux_vertex_buffer_slot(cso
);
207 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_VERTEX
);
208 cso_restore_constant_buffer_slot0(cso
, PIPE_SHADER_FRAGMENT
);
209 cso_restore_render_condition(cso
);
211 pipe_resource_reference(&ppq
->depth
, NULL
);
212 pipe_resource_reference(&refin
, NULL
);
213 pipe_resource_reference(&refout
, NULL
);
217 /* Utility functions for the filters. You're not forced to use these if */
218 /* your filter is more complicated. */
220 /** Setup this resource as the filter input. */
222 pp_filter_setup_in(struct pp_program
*p
, struct pipe_resource
*in
)
224 struct pipe_sampler_view v_tmp
;
225 u_sampler_view_default_template(&v_tmp
, in
, in
->format
);
226 p
->view
= p
->pipe
->create_sampler_view(p
->pipe
, in
, &v_tmp
);
229 /** Setup this resource as the filter output. */
231 pp_filter_setup_out(struct pp_program
*p
, struct pipe_resource
*out
)
233 p
->surf
.format
= out
->format
;
235 p
->framebuffer
.cbufs
[0] = p
->pipe
->create_surface(p
->pipe
, out
, &p
->surf
);
238 /** Clean up the input and output set with the above. */
240 pp_filter_end_pass(struct pp_program
*p
)
242 pipe_surface_reference(&p
->framebuffer
.cbufs
[0], NULL
);
243 pipe_sampler_view_reference(&p
->view
, NULL
);
247 * Convert the TGSI assembly to a runnable shader.
249 * We need not care about geometry shaders. All we have is screen quads.
252 pp_tgsi_to_state(struct pipe_context
*pipe
, const char *text
, bool isvs
,
255 struct pipe_shader_state state
;
256 struct tgsi_token
*tokens
= NULL
;
257 void *ret_state
= NULL
;
260 * Allocate temporary token storage. State creation will duplicate
261 * tokens so we must free them on exit.
263 tokens
= tgsi_alloc_tokens(PP_MAX_TOKENS
);
265 if (tokens
== NULL
) {
266 pp_debug("Failed to allocate temporary token storage.\n");
270 if (tgsi_text_translate(text
, tokens
, PP_MAX_TOKENS
) == FALSE
) {
271 _debug_printf("pp: Failed to translate a shader for %s\n", name
);
275 state
.tokens
= tokens
;
276 memset(&state
.stream_output
, 0, sizeof(state
.stream_output
));
279 ret_state
= pipe
->create_vs_state(pipe
, &state
);
282 ret_state
= pipe
->create_fs_state(pipe
, &state
);
289 /** Setup misc state for the filter. */
291 pp_filter_misc_state(struct pp_program
*p
)
293 cso_set_blend(p
->cso
, &p
->blend
);
294 cso_set_depth_stencil_alpha(p
->cso
, &p
->depthstencil
);
295 cso_set_rasterizer(p
->cso
, &p
->rasterizer
);
296 cso_set_viewport(p
->cso
, &p
->viewport
);
298 cso_set_vertex_elements(p
->cso
, 2, p
->velem
);
301 /** Draw with the filter to the set output. */
303 pp_filter_draw(struct pp_program
*p
)
305 util_draw_vertex_buffer(p
->pipe
, p
->cso
, p
->vbuf
, 0, 0,
306 PIPE_PRIM_QUADS
, 4, 2);
309 /** Set the framebuffer as active. */
311 pp_filter_set_fb(struct pp_program
*p
)
313 cso_set_framebuffer(p
->cso
, &p
->framebuffer
);
316 /** Set the framebuffer as active and clear it. */
318 pp_filter_set_clear_fb(struct pp_program
*p
)
320 cso_set_framebuffer(p
->cso
, &p
->framebuffer
);
321 p
->pipe
->clear(p
->pipe
, PIPE_CLEAR_COLOR0
, &p
->clear_color
, 0, 0);