xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/postprocess/pp_run.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 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.
25  *
26  **************************************************************************/
27 
28 #include "postprocess.h"
29 #include "postprocess/pp_filters.h"
30 #include "postprocess/pp_private.h"
31 
32 #include "frontend/api.h"
33 #include "util/u_inlines.h"
34 #include "util/u_sampler.h"
35 
36 #include "tgsi/tgsi_parse.h"
37 #include "tgsi/tgsi_text.h"
38 
39 
40 void
pp_blit(struct pipe_context * pipe,struct pipe_resource * src_tex,int srcX0,int srcY0,int srcX1,int srcY1,int srcZ0,struct pipe_surface * dst,int dstX0,int dstY0,int dstX1,int dstY1)41 pp_blit(struct pipe_context *pipe,
42         struct pipe_resource *src_tex,
43         int srcX0, int srcY0,
44         int srcX1, int srcY1,
45         int srcZ0,
46         struct pipe_surface *dst,
47         int dstX0, int dstY0,
48         int dstX1, int dstY1)
49 {
50    struct pipe_blit_info blit;
51 
52    memset(&blit, 0, sizeof(blit));
53 
54    blit.src.resource = src_tex;
55    blit.src.level = 0;
56    blit.src.format = src_tex->format;
57    blit.src.box.x = srcX0;
58    blit.src.box.y = srcY0;
59    blit.src.box.z = srcZ0;
60    blit.src.box.width = srcX1 - srcX0;
61    blit.src.box.height = srcY1 - srcY0;
62    blit.src.box.depth = 1;
63 
64    blit.dst.resource = dst->texture;
65    blit.dst.level = dst->u.tex.level;
66    blit.dst.format = dst->format;
67    blit.dst.box.x = dstX0;
68    blit.dst.box.y = dstY0;
69    blit.dst.box.z = 0;
70    blit.dst.box.width = dstX1 - dstX0;
71    blit.dst.box.height = dstY1 - dstY0;
72    blit.dst.box.depth = 1;
73 
74    blit.mask = PIPE_MASK_RGBA;
75 
76    pipe->blit(pipe, &blit);
77 }
78 
79 /**
80 *	Main run function of the PP queue. Called on swapbuffers/flush.
81 *
82 *	Runs all requested filters in order and handles shuffling the temp
83 *	buffers in between.
84 */
85 void
pp_run(struct pp_queue_t * ppq,struct pipe_resource * in,struct pipe_resource * out,struct pipe_resource * indepth)86 pp_run(struct pp_queue_t *ppq, struct pipe_resource *in,
87        struct pipe_resource *out, struct pipe_resource *indepth)
88 {
89    struct pipe_resource *refin = NULL, *refout = NULL;
90    unsigned int i;
91    struct cso_context *cso = ppq->p->cso;
92 
93    if (ppq->n_filters == 0)
94       return;
95 
96    assert(ppq->pp_queue);
97    assert(ppq->tmp[0]);
98 
99    if (in->width0 != ppq->p->framebuffer.width ||
100        in->height0 != ppq->p->framebuffer.height) {
101       pp_debug("Resizing the temp pp buffers\n");
102       pp_free_fbos(ppq);
103       pp_init_fbos(ppq, in->width0, in->height0);
104    }
105 
106    if (in == out && ppq->n_filters == 1) {
107       /* Make a copy of in to tmp[0] in this case. */
108       unsigned int w = ppq->p->framebuffer.width;
109       unsigned int h = ppq->p->framebuffer.height;
110 
111 
112       pp_blit(ppq->p->pipe, in, 0, 0,
113               w, h, 0, ppq->tmps[0],
114               0, 0, w, h);
115 
116       in = ppq->tmp[0];
117    }
118 
119    /* save state (restored below) */
120    cso_save_state(cso, (CSO_BIT_BLEND |
121                         CSO_BIT_DEPTH_STENCIL_ALPHA |
122                         CSO_BIT_FRAGMENT_SHADER |
123                         CSO_BIT_FRAMEBUFFER |
124                         CSO_BIT_TESSCTRL_SHADER |
125                         CSO_BIT_TESSEVAL_SHADER |
126                         CSO_BIT_GEOMETRY_SHADER |
127                         CSO_BIT_RASTERIZER |
128                         CSO_BIT_SAMPLE_MASK |
129                         CSO_BIT_MIN_SAMPLES |
130                         CSO_BIT_FRAGMENT_SAMPLERS |
131                         CSO_BIT_STENCIL_REF |
132                         CSO_BIT_STREAM_OUTPUTS |
133                         CSO_BIT_VERTEX_ELEMENTS |
134                         CSO_BIT_VERTEX_SHADER |
135                         CSO_BIT_VIEWPORT |
136                         CSO_BIT_PAUSE_QUERIES |
137                         CSO_BIT_RENDER_CONDITION));
138 
139    /* set default state */
140    cso_set_sample_mask(cso, ~0);
141    cso_set_min_samples(cso, 1);
142    cso_set_stream_outputs(cso, 0, NULL, NULL);
143    cso_set_tessctrl_shader_handle(cso, NULL);
144    cso_set_tesseval_shader_handle(cso, NULL);
145    cso_set_geometry_shader_handle(cso, NULL);
146    cso_set_render_condition(cso, NULL, false, 0);
147 
148    // Kept only for this frame.
149    pipe_resource_reference(&ppq->depth, indepth);
150    pipe_resource_reference(&refin, in);
151    pipe_resource_reference(&refout, out);
152 
153    switch (ppq->n_filters) {
154    case 0:
155       /* Failsafe, but never reached. */
156       break;
157    case 1:                     /* No temp buf */
158       ppq->pp_queue[0] (ppq, in, out, 0);
159       break;
160    case 2:                     /* One temp buf */
161 
162       ppq->pp_queue[0] (ppq, in, ppq->tmp[0], 0);
163       ppq->pp_queue[1] (ppq, ppq->tmp[0], out, 1);
164 
165       break;
166    default:                    /* Two temp bufs */
167       assert(ppq->tmp[1]);
168       ppq->pp_queue[0] (ppq, in, ppq->tmp[0], 0);
169 
170       for (i = 1; i < (ppq->n_filters - 1); i++) {
171          if (i % 2 == 0)
172             ppq->pp_queue[i] (ppq, ppq->tmp[1], ppq->tmp[0], i);
173 
174          else
175             ppq->pp_queue[i] (ppq, ppq->tmp[0], ppq->tmp[1], i);
176       }
177 
178       if (i % 2 == 0)
179          ppq->pp_queue[i] (ppq, ppq->tmp[1], out, i);
180 
181       else
182          ppq->pp_queue[i] (ppq, ppq->tmp[0], out, i);
183 
184       break;
185    }
186 
187    /* restore state we changed */
188    cso_restore_state(cso, CSO_UNBIND_FS_SAMPLERVIEWS |
189                           CSO_UNBIND_FS_IMAGE0 |
190                           CSO_UNBIND_VS_CONSTANTS |
191                           CSO_UNBIND_FS_CONSTANTS);
192 
193    /* restore states not restored by cso */
194    if (ppq->p->st) {
195       ppq->p->st_invalidate_state(ppq->p->st,
196                                   ST_INVALIDATE_FS_SAMPLER_VIEWS |
197                                   ST_INVALIDATE_FS_CONSTBUF0 |
198                                   ST_INVALIDATE_VS_CONSTBUF0 |
199                                   ST_INVALIDATE_VERTEX_BUFFERS);
200    }
201 
202    pipe_resource_reference(&ppq->depth, NULL);
203    pipe_resource_reference(&refin, NULL);
204    pipe_resource_reference(&refout, NULL);
205 }
206 
207 
208 /* Utility functions for the filters. You're not forced to use these if */
209 /* your filter is more complicated. */
210 
211 /** Setup this resource as the filter input. */
212 void
pp_filter_setup_in(struct pp_program * p,struct pipe_resource * in)213 pp_filter_setup_in(struct pp_program *p, struct pipe_resource *in)
214 {
215    struct pipe_sampler_view v_tmp;
216    u_sampler_view_default_template(&v_tmp, in, in->format);
217    p->view = p->pipe->create_sampler_view(p->pipe, in, &v_tmp);
218 }
219 
220 /** Setup this resource as the filter output. */
221 void
pp_filter_setup_out(struct pp_program * p,struct pipe_resource * out)222 pp_filter_setup_out(struct pp_program *p, struct pipe_resource *out)
223 {
224    p->surf.format = out->format;
225 
226    p->framebuffer.cbufs[0] = p->pipe->create_surface(p->pipe, out, &p->surf);
227 }
228 
229 /** Clean up the input and output set with the above. */
230 void
pp_filter_end_pass(struct pp_program * p)231 pp_filter_end_pass(struct pp_program *p)
232 {
233    pipe_surface_reference(&p->framebuffer.cbufs[0], NULL);
234    pipe_sampler_view_reference(&p->view, NULL);
235 }
236 
237 /**
238 *	Convert the TGSI assembly to a runnable shader.
239 *
240 * We need not care about geometry shaders. All we have is screen quads.
241 */
242 void *
pp_tgsi_to_state(struct pipe_context * pipe,const char * text,bool isvs,const char * name)243 pp_tgsi_to_state(struct pipe_context *pipe, const char *text, bool isvs,
244                  const char *name)
245 {
246    struct pipe_shader_state state;
247    struct tgsi_token *tokens = NULL;
248    void *ret_state = NULL;
249 
250    /*
251     * Allocate temporary token storage. State creation will duplicate
252     * tokens so we must free them on exit.
253     */
254    tokens = tgsi_alloc_tokens(PP_MAX_TOKENS);
255 
256    if (!tokens) {
257       pp_debug("Failed to allocate temporary token storage.\n");
258       return NULL;
259    }
260 
261    if (tgsi_text_translate(text, tokens, PP_MAX_TOKENS) == false) {
262       _debug_printf("pp: Failed to translate a shader for %s\n", name);
263       return NULL;
264    }
265 
266    pipe_shader_state_from_tgsi(&state, tokens);
267 
268    if (isvs) {
269       ret_state = pipe->create_vs_state(pipe, &state);
270       FREE(tokens);
271    } else {
272       ret_state = pipe->create_fs_state(pipe, &state);
273       FREE(tokens);
274    }
275 
276    return ret_state;
277 }
278 
279 /** Setup misc state for the filter. */
280 void
pp_filter_misc_state(struct pp_program * p)281 pp_filter_misc_state(struct pp_program *p)
282 {
283    cso_set_blend(p->cso, &p->blend);
284    cso_set_depth_stencil_alpha(p->cso, &p->depthstencil);
285    cso_set_rasterizer(p->cso, &p->rasterizer);
286    cso_set_viewport(p->cso, &p->viewport);
287 
288    cso_set_vertex_elements(p->cso, &p->velem);
289 }
290 
291 /** Draw with the filter to the set output. */
292 void
pp_filter_draw(struct pp_program * p)293 pp_filter_draw(struct pp_program *p)
294 {
295    util_draw_vertex_buffer(p->pipe, p->cso, p->vbuf, 0, false,
296                            MESA_PRIM_QUADS, 4, 2);
297 }
298 
299 /** Set the framebuffer as active. */
300 void
pp_filter_set_fb(struct pp_program * p)301 pp_filter_set_fb(struct pp_program *p)
302 {
303    cso_set_framebuffer(p->cso, &p->framebuffer);
304 }
305 
306 /** Set the framebuffer as active and clear it. */
307 void
pp_filter_set_clear_fb(struct pp_program * p)308 pp_filter_set_clear_fb(struct pp_program *p)
309 {
310    cso_set_framebuffer(p->cso, &p->framebuffer);
311    p->pipe->clear(p->pipe, PIPE_CLEAR_COLOR0, NULL, &p->clear_color, 0, 0);
312 }
313