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