89b88a5214c393d201d897b1cbad57c55c20afc0
[mesa.git] / src / gallium / auxiliary / postprocess / pp_run.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 "postprocess.h"
29
30 #include "postprocess/pp_filters.h"
31 #include "util/u_blit.h"
32 #include "util/u_inlines.h"
33 #include "util/u_sampler.h"
34
35 /**
36 * Main run function of the PP queue. Called on swapbuffers/flush.
37 *
38 * Runs all requested filters in order and handles shuffling the temp
39 * buffers in between.
40 */
41 void
42 pp_run(struct pp_queue_t *ppq, struct pipe_resource *in,
43 struct pipe_resource *out, struct pipe_resource *indepth)
44 {
45 struct pipe_resource *refin = NULL, *refout = NULL;
46 unsigned int i;
47
48 if (in->width0 != ppq->p->framebuffer.width ||
49 in->height0 != ppq->p->framebuffer.height) {
50 pp_debug("Resizing the temp pp buffers\n");
51 pp_free_fbos(ppq);
52 pp_init_fbos(ppq, in->width0, in->height0);
53 }
54
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;
59
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);
63
64 in = ppq->tmp[0];
65 }
66
67 // Kept only for this frame.
68 pipe_resource_reference(&ppq->depth, indepth);
69 pipe_resource_reference(&refin, in);
70 pipe_resource_reference(&refout, out);
71
72 switch (ppq->n_filters) {
73 case 1: /* No temp buf */
74 ppq->pp_queue[0] (ppq, in, out, 0);
75 break;
76 case 2: /* One temp buf */
77
78 ppq->pp_queue[0] (ppq, in, ppq->tmp[0], 0);
79 ppq->pp_queue[1] (ppq, ppq->tmp[0], out, 1);
80
81 break;
82 default: /* Two temp bufs */
83 ppq->pp_queue[0] (ppq, in, ppq->tmp[0], 0);
84
85 for (i = 1; i < (ppq->n_filters - 1); i++) {
86 if (i % 2 == 0)
87 ppq->pp_queue[i] (ppq, ppq->tmp[1], ppq->tmp[0], i);
88
89 else
90 ppq->pp_queue[i] (ppq, ppq->tmp[0], ppq->tmp[1], i);
91 }
92
93 if (i % 2 == 0)
94 ppq->pp_queue[i] (ppq, ppq->tmp[1], out, i);
95
96 else
97 ppq->pp_queue[i] (ppq, ppq->tmp[0], out, i);
98
99 break;
100 }
101
102 pipe_resource_reference(&ppq->depth, NULL);
103 pipe_resource_reference(&refin, NULL);
104 pipe_resource_reference(&refout, NULL);
105 }
106
107
108 /* Utility functions for the filters. You're not forced to use these if */
109 /* your filter is more complicated. */
110
111 /** Setup this resource as the filter input. */
112 void
113 pp_filter_setup_in(struct program *p, struct pipe_resource *in)
114 {
115 struct pipe_sampler_view v_tmp;
116 u_sampler_view_default_template(&v_tmp, in, in->format);
117 p->view = p->pipe->create_sampler_view(p->pipe, in, &v_tmp);
118 }
119
120 /** Setup this resource as the filter output. */
121 void
122 pp_filter_setup_out(struct program *p, struct pipe_resource *out)
123 {
124 p->surf.format = out->format;
125 p->surf.usage = PIPE_BIND_RENDER_TARGET;
126
127 p->framebuffer.cbufs[0] = p->pipe->create_surface(p->pipe, out, &p->surf);
128 }
129
130 /** Clean up the input and output set with the above. */
131 void
132 pp_filter_end_pass(struct program *p)
133 {
134 pipe_surface_reference(&p->framebuffer.cbufs[0], NULL);
135 pipe_sampler_view_reference(&p->view, NULL);
136 }
137
138 /**
139 * Convert the TGSI assembly to a runnable shader.
140 *
141 * We need not care about geometry shaders. All we have is screen quads.
142 */
143 void *
144 pp_tgsi_to_state(struct pipe_context *pipe, const char *text, bool isvs,
145 const char *name)
146 {
147 struct pipe_shader_state state;
148 struct tgsi_token tokens[PP_MAX_TOKENS];
149
150 if (tgsi_text_translate(text, tokens, Elements(tokens)) == FALSE) {
151 pp_debug("Failed to translate %s\n", name);
152 return NULL;
153 }
154
155 state.tokens = tokens;
156 memset(&state.stream_output, 0, sizeof(state.stream_output));
157
158 if (isvs)
159 return pipe->create_vs_state(pipe, &state);
160 else
161 return pipe->create_fs_state(pipe, &state);
162 }
163
164 /** Setup misc state for the filter. */
165 void
166 pp_filter_misc_state(struct program *p)
167 {
168 cso_set_blend(p->cso, &p->blend);
169 cso_set_depth_stencil_alpha(p->cso, &p->depthstencil);
170 cso_set_rasterizer(p->cso, &p->rasterizer);
171 cso_set_viewport(p->cso, &p->viewport);
172
173 cso_set_vertex_elements(p->cso, 2, p->velem);
174 }
175
176 /** Draw with the filter to the set output. */
177 void
178 pp_filter_draw(struct program *p)
179 {
180 util_draw_vertex_buffer(p->pipe, p->cso, p->vbuf, 0,
181 PIPE_PRIM_QUADS, 4, 2);
182 p->pipe->flush(p->pipe, NULL);
183 }
184
185 /** Set the framebuffer as active. */
186 void
187 pp_filter_set_fb(struct program *p)
188 {
189 cso_set_framebuffer(p->cso, &p->framebuffer);
190 }
191
192 /** Set the framebuffer as active and clear it. */
193 void
194 pp_filter_set_clear_fb(struct program *p)
195 {
196 cso_set_framebuffer(p->cso, &p->framebuffer);
197 p->pipe->clear(p->pipe, PIPE_CLEAR_COLOR, &p->clear_color, 0, 0);
198 }