cso: add constant buffer save/restore feature for postprocessing
[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 struct cso_context *cso = ppq->p->cso;
48
49 if (in->width0 != ppq->p->framebuffer.width ||
50 in->height0 != ppq->p->framebuffer.height) {
51 pp_debug("Resizing the temp pp buffers\n");
52 pp_free_fbos(ppq);
53 pp_init_fbos(ppq, in->width0, in->height0);
54 }
55
56 if (in == out && ppq->n_filters == 1) {
57 /* Make a copy of in to tmp[0] in this case. */
58 unsigned int w = ppq->p->framebuffer.width;
59 unsigned int h = ppq->p->framebuffer.height;
60
61 util_blit_pixels(ppq->p->blitctx, in, 0, 0, 0,
62 w, h, 0, ppq->tmps[0],
63 0, 0, w, h, 0, PIPE_TEX_MIPFILTER_NEAREST,
64 TGSI_WRITEMASK_XYZW, 0);
65
66 in = ppq->tmp[0];
67 }
68
69 /* save state (restored below) */
70 cso_save_blend(cso);
71 cso_save_depth_stencil_alpha(cso);
72 cso_save_fragment_shader(cso);
73 cso_save_framebuffer(cso);
74 cso_save_geometry_shader(cso);
75 cso_save_rasterizer(cso);
76 cso_save_sample_mask(cso);
77 cso_save_samplers(cso, PIPE_SHADER_FRAGMENT);
78 cso_save_sampler_views(cso, PIPE_SHADER_FRAGMENT);
79 cso_save_stencil_ref(cso);
80 cso_save_stream_outputs(cso);
81 cso_save_vertex_elements(cso);
82 cso_save_vertex_shader(cso);
83 cso_save_viewport(cso);
84 cso_save_aux_vertex_buffer_slot(cso);
85 cso_save_constant_buffer_slot0(cso, PIPE_SHADER_VERTEX);
86 cso_save_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
87 cso_save_render_condition(cso);
88
89 /* set default state */
90 cso_set_sample_mask(cso, ~0);
91 cso_set_stream_outputs(cso, 0, NULL, 0);
92 cso_set_geometry_shader_handle(cso, NULL);
93 cso_set_render_condition(cso, NULL, 0);
94
95 // Kept only for this frame.
96 pipe_resource_reference(&ppq->depth, indepth);
97 pipe_resource_reference(&refin, in);
98 pipe_resource_reference(&refout, out);
99
100 switch (ppq->n_filters) {
101 case 1: /* No temp buf */
102 ppq->pp_queue[0] (ppq, in, out, 0);
103 break;
104 case 2: /* One temp buf */
105
106 ppq->pp_queue[0] (ppq, in, ppq->tmp[0], 0);
107 ppq->pp_queue[1] (ppq, ppq->tmp[0], out, 1);
108
109 break;
110 default: /* Two temp bufs */
111 ppq->pp_queue[0] (ppq, in, ppq->tmp[0], 0);
112
113 for (i = 1; i < (ppq->n_filters - 1); i++) {
114 if (i % 2 == 0)
115 ppq->pp_queue[i] (ppq, ppq->tmp[1], ppq->tmp[0], i);
116
117 else
118 ppq->pp_queue[i] (ppq, ppq->tmp[0], ppq->tmp[1], i);
119 }
120
121 if (i % 2 == 0)
122 ppq->pp_queue[i] (ppq, ppq->tmp[1], out, i);
123
124 else
125 ppq->pp_queue[i] (ppq, ppq->tmp[0], out, i);
126
127 break;
128 }
129
130 /* restore state we changed */
131 cso_restore_blend(cso);
132 cso_restore_depth_stencil_alpha(cso);
133 cso_restore_fragment_shader(cso);
134 cso_restore_framebuffer(cso);
135 cso_restore_geometry_shader(cso);
136 cso_restore_rasterizer(cso);
137 cso_restore_sample_mask(cso);
138 cso_restore_samplers(cso, PIPE_SHADER_FRAGMENT);
139 cso_restore_sampler_views(cso, PIPE_SHADER_FRAGMENT);
140 cso_restore_stencil_ref(cso);
141 cso_restore_stream_outputs(cso);
142 cso_restore_vertex_elements(cso);
143 cso_restore_vertex_shader(cso);
144 cso_restore_viewport(cso);
145 cso_restore_aux_vertex_buffer_slot(cso);
146 cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_VERTEX);
147 cso_restore_constant_buffer_slot0(cso, PIPE_SHADER_FRAGMENT);
148 cso_restore_render_condition(cso);
149
150 pipe_resource_reference(&ppq->depth, NULL);
151 pipe_resource_reference(&refin, NULL);
152 pipe_resource_reference(&refout, NULL);
153 }
154
155
156 /* Utility functions for the filters. You're not forced to use these if */
157 /* your filter is more complicated. */
158
159 /** Setup this resource as the filter input. */
160 void
161 pp_filter_setup_in(struct program *p, struct pipe_resource *in)
162 {
163 struct pipe_sampler_view v_tmp;
164 u_sampler_view_default_template(&v_tmp, in, in->format);
165 p->view = p->pipe->create_sampler_view(p->pipe, in, &v_tmp);
166 }
167
168 /** Setup this resource as the filter output. */
169 void
170 pp_filter_setup_out(struct program *p, struct pipe_resource *out)
171 {
172 p->surf.format = out->format;
173
174 p->framebuffer.cbufs[0] = p->pipe->create_surface(p->pipe, out, &p->surf);
175 }
176
177 /** Clean up the input and output set with the above. */
178 void
179 pp_filter_end_pass(struct program *p)
180 {
181 pipe_surface_reference(&p->framebuffer.cbufs[0], NULL);
182 pipe_sampler_view_reference(&p->view, NULL);
183 }
184
185 /**
186 * Convert the TGSI assembly to a runnable shader.
187 *
188 * We need not care about geometry shaders. All we have is screen quads.
189 */
190 void *
191 pp_tgsi_to_state(struct pipe_context *pipe, const char *text, bool isvs,
192 const char *name)
193 {
194 struct pipe_shader_state state;
195 struct tgsi_token tokens[PP_MAX_TOKENS];
196
197 if (tgsi_text_translate(text, tokens, Elements(tokens)) == FALSE) {
198 pp_debug("Failed to translate %s\n", name);
199 return NULL;
200 }
201
202 state.tokens = tokens;
203 memset(&state.stream_output, 0, sizeof(state.stream_output));
204
205 if (isvs)
206 return pipe->create_vs_state(pipe, &state);
207 else
208 return pipe->create_fs_state(pipe, &state);
209 }
210
211 /** Setup misc state for the filter. */
212 void
213 pp_filter_misc_state(struct program *p)
214 {
215 cso_set_blend(p->cso, &p->blend);
216 cso_set_depth_stencil_alpha(p->cso, &p->depthstencil);
217 cso_set_rasterizer(p->cso, &p->rasterizer);
218 cso_set_viewport(p->cso, &p->viewport);
219
220 cso_set_vertex_elements(p->cso, 2, p->velem);
221 }
222
223 /** Draw with the filter to the set output. */
224 void
225 pp_filter_draw(struct program *p)
226 {
227 util_draw_vertex_buffer(p->pipe, p->cso, p->vbuf, 0, 0,
228 PIPE_PRIM_QUADS, 4, 2);
229 }
230
231 /** Set the framebuffer as active. */
232 void
233 pp_filter_set_fb(struct program *p)
234 {
235 cso_set_framebuffer(p->cso, &p->framebuffer);
236 }
237
238 /** Set the framebuffer as active and clear it. */
239 void
240 pp_filter_set_clear_fb(struct program *p)
241 {
242 cso_set_framebuffer(p->cso, &p->framebuffer);
243 p->pipe->clear(p->pipe, PIPE_CLEAR_COLOR, &p->clear_color, 0, 0);
244 }