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 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"
30 #include "postprocess/pp_filters.h"
31 #include "util/u_blit.h"
32 #include "util/u_inlines.h"
33 #include "util/u_sampler.h"
36 * Main run function of the PP queue. Called on swapbuffers/flush.
38 * Runs all requested filters in order and handles shuffling the temp
42 pp_run(struct pp_queue_t
*ppq
, struct pipe_resource
*in
,
43 struct pipe_resource
*out
, struct pipe_resource
*indepth
)
48 if (in
->width0
!= ppq
->p
->framebuffer
.width
||
49 in
->height0
!= ppq
->p
->framebuffer
.height
) {
50 pp_debug("Resizing the temp pp buffers\n");
52 pp_init_fbos(ppq
, in
->width0
, in
->height0
, indepth
);
55 if (in
== out
&& ppq
->n_filters
== 1) {
56 /* Make a copy of in to tmp[0] in this case. */
57 unsigned int w
= ppq
->p
->framebuffer
.width
;
58 unsigned int h
= ppq
->p
->framebuffer
.height
;
60 util_blit_pixels(ppq
->p
->blitctx
, in
, 0, 0, 0,
61 w
, h
, 0, ppq
->tmps
[0],
62 0, 0, w
, h
, 0, PIPE_TEX_MIPFILTER_NEAREST
);
67 switch (ppq
->n_filters
) {
68 case 1: /* No temp buf */
69 ppq
->pp_queue
[0] (ppq
, in
, out
, 0);
71 case 2: /* One temp buf */
73 ppq
->pp_queue
[0] (ppq
, in
, ppq
->tmp
[0], 0);
74 ppq
->pp_queue
[1] (ppq
, ppq
->tmp
[0], out
, 1);
77 default: /* Two temp bufs */
78 ppq
->pp_queue
[0] (ppq
, in
, ppq
->tmp
[0], 0);
80 for (i
= 1; i
< (ppq
->n_filters
- 1); i
++) {
82 ppq
->pp_queue
[i
] (ppq
, ppq
->tmp
[1], ppq
->tmp
[0], i
);
85 ppq
->pp_queue
[i
] (ppq
, ppq
->tmp
[0], ppq
->tmp
[1], i
);
89 ppq
->pp_queue
[i
] (ppq
, ppq
->tmp
[1], out
, i
);
92 ppq
->pp_queue
[i
] (ppq
, ppq
->tmp
[0], out
, i
);
99 /* Utility functions for the filters. You're not forced to use these if */
100 /* your filter is more complicated. */
102 /** Setup this resource as the filter input. */
104 pp_filter_setup_in(struct program
*p
, struct pipe_resource
*in
)
106 struct pipe_sampler_view v_tmp
;
107 u_sampler_view_default_template(&v_tmp
, in
, in
->format
);
108 p
->view
= p
->pipe
->create_sampler_view(p
->pipe
, in
, &v_tmp
);
111 /** Setup this resource as the filter output. */
113 pp_filter_setup_out(struct program
*p
, struct pipe_resource
*out
)
115 p
->surf
.format
= out
->format
;
116 p
->surf
.usage
= PIPE_BIND_RENDER_TARGET
;
118 p
->framebuffer
.cbufs
[0] = p
->pipe
->create_surface(p
->pipe
, out
, &p
->surf
);
121 /** Clean up the input and output set with the above. */
123 pp_filter_end_pass(struct program
*p
)
125 pipe_surface_reference(&p
->framebuffer
.cbufs
[0], NULL
);
126 pipe_sampler_view_reference(&p
->view
, NULL
);
130 * Convert the TGSI assembly to a runnable shader.
132 * We need not care about geometry shaders. All we have is screen quads.
135 pp_tgsi_to_state(struct pipe_context
*pipe
, const char *text
, bool isvs
,
138 struct pipe_shader_state state
;
139 struct tgsi_token tokens
[PP_MAX_TOKENS
];
141 if (tgsi_text_translate(text
, tokens
, Elements(tokens
)) == FALSE
) {
142 pp_debug("Failed to translate %s\n", name
);
146 state
.tokens
= tokens
;
149 return pipe
->create_vs_state(pipe
, &state
);
151 return pipe
->create_fs_state(pipe
, &state
);
154 /** Setup misc state for the filter. */
156 pp_filter_misc_state(struct program
*p
)
158 cso_set_blend(p
->cso
, &p
->blend
);
159 cso_set_depth_stencil_alpha(p
->cso
, &p
->depthstencil
);
160 cso_set_rasterizer(p
->cso
, &p
->rasterizer
);
161 cso_set_viewport(p
->cso
, &p
->viewport
);
163 cso_set_vertex_elements(p
->cso
, 2, p
->velem
);
166 /** Draw with the filter to the set output. */
168 pp_filter_draw(struct program
*p
)
170 util_draw_vertex_buffer(p
->pipe
, p
->cso
, p
->vbuf
, 0,
171 PIPE_PRIM_QUADS
, 4, 2);
172 p
->pipe
->flush(p
->pipe
, NULL
);
175 /** Set the framebuffer as active. */
177 pp_filter_set_fb(struct program
*p
)
179 cso_set_framebuffer(p
->cso
, &p
->framebuffer
);
182 /** Set the framebuffer as active and clear it. */
184 pp_filter_set_clear_fb(struct program
*p
)
186 cso_set_framebuffer(p
->cso
, &p
->framebuffer
);
187 p
->pipe
->clear(p
->pipe
, PIPE_CLEAR_COLOR
, &p
->clear_color
, 0, 0);