xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/draw/draw_pipe_pstipple.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2008 VMware, Inc.
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 VMWARE AND/OR ITS SUPPLIERS 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 /**
29  * Polygon stipple stage:  implement polygon stipple with texture map and
30  * fragment program.  The fragment program samples the texture using the
31  * fragment window coordinate register and does a fragment kill for the
32  * stipple-failing fragments.
33  *
34  * Authors:  Brian Paul
35  */
36 
37 
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_shader_tokens.h"
41 #include "util/u_inlines.h"
42 
43 #include "util/format/u_format.h"
44 #include "util/u_math.h"
45 #include "util/u_memory.h"
46 #include "util/u_pstipple.h"
47 #include "util/u_sampler.h"
48 
49 #include "tgsi/tgsi_parse.h"
50 
51 #include "draw_context.h"
52 #include "draw_pipe.h"
53 
54 #include "nir.h"
55 #include "nir/nir_draw_helpers.h"
56 
57 /** Approx number of new tokens for instructions in pstip_transform_inst() */
58 #define NUM_NEW_TOKENS 53
59 
60 
61 /**
62  * Subclass of pipe_shader_state to carry extra fragment shader info.
63  */
64 struct pstip_fragment_shader
65 {
66    struct pipe_shader_state state;
67    void *driver_fs;
68    void *pstip_fs;
69    unsigned sampler_unit;
70 };
71 
72 
73 /**
74  * Subclass of draw_stage
75  */
76 struct pstip_stage
77 {
78    struct draw_stage stage;
79 
80    void *sampler_cso;
81    struct pipe_resource *texture;
82    struct pipe_sampler_view *sampler_view;
83    unsigned num_samplers;
84    unsigned num_sampler_views;
85 
86    /*
87     * Currently bound state
88     */
89    struct pstip_fragment_shader *fs;
90    struct {
91       void *samplers[PIPE_MAX_SAMPLERS];
92       struct pipe_sampler_view *sampler_views[PIPE_MAX_SHADER_SAMPLER_VIEWS];
93       const struct pipe_poly_stipple *stipple;
94    } state;
95 
96    /*
97     * Driver interface/override functions
98     */
99    void * (*driver_create_fs_state)(struct pipe_context *,
100                                     const struct pipe_shader_state *);
101    void (*driver_bind_fs_state)(struct pipe_context *, void *);
102    void (*driver_delete_fs_state)(struct pipe_context *, void *);
103 
104    void (*driver_bind_sampler_states)(struct pipe_context *,
105                                       enum pipe_shader_type,
106                                       unsigned, unsigned, void **);
107 
108    void (*driver_set_sampler_views)(struct pipe_context *,
109                                     enum pipe_shader_type shader,
110                                     unsigned start, unsigned count,
111                                     unsigned unbind_num_trailing_slots,
112                                     bool take_ownership,
113                                     struct pipe_sampler_view **);
114 
115    void (*driver_set_polygon_stipple)(struct pipe_context *,
116                                       const struct pipe_poly_stipple *);
117 
118    struct pipe_context *pipe;
119 };
120 
121 
122 /**
123  * Generate the frag shader we'll use for doing polygon stipple.
124  * This will be the user's shader prefixed with a TEX and KIL instruction.
125  */
126 static bool
generate_pstip_fs(struct pstip_stage * pstip)127 generate_pstip_fs(struct pstip_stage *pstip)
128 {
129    struct pipe_context *pipe = pstip->pipe;
130    struct pipe_screen *screen = pipe->screen;
131    const struct pipe_shader_state *orig_fs = &pstip->fs->state;
132    /*struct draw_context *draw = pstip->stage.draw;*/
133    struct pipe_shader_state pstip_fs;
134    enum tgsi_file_type wincoord_file;
135 
136    wincoord_file = screen->get_param(screen, PIPE_CAP_FS_POSITION_IS_SYSVAL) ?
137                    TGSI_FILE_SYSTEM_VALUE : TGSI_FILE_INPUT;
138 
139    pstip_fs = *orig_fs; /* copy to init */
140    if (orig_fs->type == PIPE_SHADER_IR_TGSI) {
141       pstip_fs.tokens = util_pstipple_create_fragment_shader(orig_fs->tokens,
142                                                              &pstip->fs->sampler_unit,
143                                                              0,
144                                                              wincoord_file);
145       if (pstip_fs.tokens == NULL)
146          return false;
147    } else {
148       pstip_fs.ir.nir = nir_shader_clone(NULL, orig_fs->ir.nir);
149       nir_lower_pstipple_fs(pstip_fs.ir.nir,
150                             &pstip->fs->sampler_unit, 0, wincoord_file == TGSI_FILE_SYSTEM_VALUE,
151                             nir_type_bool32);
152    }
153 
154    assert(pstip->fs->sampler_unit < PIPE_MAX_SAMPLERS);
155 
156    pstip->fs->pstip_fs = pstip->driver_create_fs_state(pipe, &pstip_fs);
157 
158    FREE((void *)pstip_fs.tokens);
159 
160    if (!pstip->fs->pstip_fs)
161       return false;
162 
163    return true;
164 }
165 
166 
167 /**
168  * When we're about to draw our first stipple polygon in a batch, this function
169  * is called to tell the driver to bind our modified fragment shader.
170  */
171 static bool
bind_pstip_fragment_shader(struct pstip_stage * pstip)172 bind_pstip_fragment_shader(struct pstip_stage *pstip)
173 {
174    struct draw_context *draw = pstip->stage.draw;
175    if (!pstip->fs->pstip_fs &&
176        !generate_pstip_fs(pstip))
177       return false;
178 
179    draw->suspend_flushing = true;
180    pstip->driver_bind_fs_state(pstip->pipe, pstip->fs->pstip_fs);
181    draw->suspend_flushing = false;
182    return true;
183 }
184 
185 
186 static inline struct pstip_stage *
pstip_stage(struct draw_stage * stage)187 pstip_stage(struct draw_stage *stage)
188 {
189    return (struct pstip_stage *) stage;
190 }
191 
192 
193 static void
pstip_first_tri(struct draw_stage * stage,struct prim_header * header)194 pstip_first_tri(struct draw_stage *stage, struct prim_header *header)
195 {
196    struct pstip_stage *pstip = pstip_stage(stage);
197    struct pipe_context *pipe = pstip->pipe;
198    struct draw_context *draw = stage->draw;
199    unsigned num_samplers;
200    unsigned num_sampler_views;
201 
202    assert(stage->draw->rasterizer->poly_stipple_enable);
203 
204    /* bind our fragprog */
205    if (!bind_pstip_fragment_shader(pstip)) {
206       stage->tri = draw_pipe_passthrough_tri;
207       stage->tri(stage, header);
208       return;
209    }
210 
211    /* how many samplers? */
212    /* we'll use sampler/texture[pstip->sampler_unit] for the stipple */
213    num_samplers = MAX2(pstip->num_samplers, pstip->fs->sampler_unit + 1);
214    num_sampler_views = MAX2(pstip->num_sampler_views, num_samplers);
215 
216    /* plug in our sampler, texture */
217    pstip->state.samplers[pstip->fs->sampler_unit] = pstip->sampler_cso;
218    pipe_sampler_view_reference(&pstip->state.sampler_views[pstip->fs->sampler_unit],
219                                pstip->sampler_view);
220 
221    assert(num_samplers <= PIPE_MAX_SAMPLERS);
222 
223    draw->suspend_flushing = true;
224 
225    pstip->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
226                                      num_samplers, pstip->state.samplers);
227 
228    pstip->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
229                                    num_sampler_views, 0, false,
230                                    pstip->state.sampler_views);
231 
232    draw->suspend_flushing = false;
233 
234    /* now really draw first triangle */
235    stage->tri = draw_pipe_passthrough_tri;
236    stage->tri(stage, header);
237 }
238 
239 
240 static void
pstip_flush(struct draw_stage * stage,unsigned flags)241 pstip_flush(struct draw_stage *stage, unsigned flags)
242 {
243    struct draw_context *draw = stage->draw;
244    struct pstip_stage *pstip = pstip_stage(stage);
245    struct pipe_context *pipe = pstip->pipe;
246 
247    stage->tri = pstip_first_tri;
248    stage->next->flush(stage->next, flags);
249 
250    /* restore original frag shader, texture, sampler state */
251    draw->suspend_flushing = true;
252    pstip->driver_bind_fs_state(pipe, pstip->fs ? pstip->fs->driver_fs : NULL);
253 
254    pstip->driver_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
255                                      pstip->num_samplers,
256                                      pstip->state.samplers);
257 
258    pstip->driver_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
259                                    pstip->num_sampler_views, 0, false,
260                                    pstip->state.sampler_views);
261 
262    draw->suspend_flushing = false;
263 }
264 
265 
266 static void
pstip_reset_stipple_counter(struct draw_stage * stage)267 pstip_reset_stipple_counter(struct draw_stage *stage)
268 {
269    stage->next->reset_stipple_counter(stage->next);
270 }
271 
272 
273 static void
pstip_destroy(struct draw_stage * stage)274 pstip_destroy(struct draw_stage *stage)
275 {
276    struct pstip_stage *pstip = pstip_stage(stage);
277 
278    for (unsigned i = 0; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; i++) {
279       pipe_sampler_view_reference(&pstip->state.sampler_views[i], NULL);
280    }
281 
282    pstip->pipe->delete_sampler_state(pstip->pipe, pstip->sampler_cso);
283 
284    pipe_resource_reference(&pstip->texture, NULL);
285 
286    if (pstip->sampler_view) {
287       pipe_sampler_view_reference(&pstip->sampler_view, NULL);
288    }
289 
290    draw_free_temp_verts(stage);
291    FREE(stage);
292 }
293 
294 
295 /** Create a new polygon stipple drawing stage object */
296 static struct pstip_stage *
draw_pstip_stage(struct draw_context * draw,struct pipe_context * pipe)297 draw_pstip_stage(struct draw_context *draw, struct pipe_context *pipe)
298 {
299    struct pstip_stage *pstip = CALLOC_STRUCT(pstip_stage);
300    if (!pstip)
301       goto fail;
302 
303    pstip->pipe = pipe;
304 
305    pstip->stage.draw = draw;
306    pstip->stage.name = "pstip";
307    pstip->stage.next = NULL;
308    pstip->stage.point = draw_pipe_passthrough_point;
309    pstip->stage.line = draw_pipe_passthrough_line;
310    pstip->stage.tri = pstip_first_tri;
311    pstip->stage.flush = pstip_flush;
312    pstip->stage.reset_stipple_counter = pstip_reset_stipple_counter;
313    pstip->stage.destroy = pstip_destroy;
314 
315    if (!draw_alloc_temp_verts(&pstip->stage, 8))
316       goto fail;
317 
318    return pstip;
319 
320 fail:
321    if (pstip)
322       pstip->stage.destroy(&pstip->stage);
323 
324    return NULL;
325 }
326 
327 
328 static struct pstip_stage *
pstip_stage_from_pipe(struct pipe_context * pipe)329 pstip_stage_from_pipe(struct pipe_context *pipe)
330 {
331    struct draw_context *draw = (struct draw_context *) pipe->draw;
332    return pstip_stage(draw->pipeline.pstipple);
333 }
334 
335 
336 /**
337  * This function overrides the driver's create_fs_state() function and
338  * will typically be called by the gallium frontend.
339  */
340 static void *
pstip_create_fs_state(struct pipe_context * pipe,const struct pipe_shader_state * fs)341 pstip_create_fs_state(struct pipe_context *pipe,
342                        const struct pipe_shader_state *fs)
343 {
344    struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
345    struct pstip_fragment_shader *pstipfs = CALLOC_STRUCT(pstip_fragment_shader);
346 
347    if (pstipfs) {
348       pstipfs->state.type = fs->type;
349       if (fs->type == PIPE_SHADER_IR_TGSI)
350          pstipfs->state.tokens = tgsi_dup_tokens(fs->tokens);
351       else
352          pstipfs->state.ir.nir = nir_shader_clone(NULL, fs->ir.nir);
353 
354       /* pass-through */
355       pstipfs->driver_fs = pstip->driver_create_fs_state(pstip->pipe, fs);
356    }
357 
358    return pstipfs;
359 }
360 
361 
362 static void
pstip_bind_fs_state(struct pipe_context * pipe,void * fs)363 pstip_bind_fs_state(struct pipe_context *pipe, void *fs)
364 {
365    struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
366    struct pstip_fragment_shader *pstipfs = (struct pstip_fragment_shader *) fs;
367    /* save current */
368    pstip->fs = pstipfs;
369    /* pass-through */
370    pstip->driver_bind_fs_state(pstip->pipe,
371                                (pstipfs ? pstipfs->driver_fs : NULL));
372 }
373 
374 
375 static void
pstip_delete_fs_state(struct pipe_context * pipe,void * fs)376 pstip_delete_fs_state(struct pipe_context *pipe, void *fs)
377 {
378    struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
379    struct pstip_fragment_shader *pstipfs = (struct pstip_fragment_shader *) fs;
380    /* pass-through */
381    pstip->driver_delete_fs_state(pstip->pipe, pstipfs->driver_fs);
382 
383    if (pstipfs->pstip_fs)
384       pstip->driver_delete_fs_state(pstip->pipe, pstipfs->pstip_fs);
385 
386    if (pstipfs->state.type == PIPE_SHADER_IR_TGSI)
387       FREE((void*)pstipfs->state.tokens);
388    else
389       ralloc_free(pstipfs->state.ir.nir);
390    FREE(pstipfs);
391 }
392 
393 
394 static void
pstip_bind_sampler_states(struct pipe_context * pipe,enum pipe_shader_type shader,unsigned start,unsigned num,void ** sampler)395 pstip_bind_sampler_states(struct pipe_context *pipe,
396                           enum pipe_shader_type shader,
397                           unsigned start, unsigned num, void **sampler)
398 {
399    struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
400 
401    assert(start == 0);
402 
403    if (shader == PIPE_SHADER_FRAGMENT) {
404       /* save current */
405       memcpy(pstip->state.samplers, sampler, num * sizeof(void *));
406       for (unsigned i = num; i < PIPE_MAX_SAMPLERS; i++) {
407          pstip->state.samplers[i] = NULL;
408       }
409       pstip->num_samplers = num;
410    }
411 
412    /* pass-through */
413    pstip->driver_bind_sampler_states(pstip->pipe, shader, start, num, sampler);
414 }
415 
416 
417 static void
pstip_set_sampler_views(struct pipe_context * pipe,enum pipe_shader_type shader,unsigned start,unsigned num,unsigned unbind_num_trailing_slots,bool take_ownership,struct pipe_sampler_view ** views)418 pstip_set_sampler_views(struct pipe_context *pipe,
419                         enum pipe_shader_type shader,
420                         unsigned start, unsigned num,
421                         unsigned unbind_num_trailing_slots,
422                         bool take_ownership,
423                         struct pipe_sampler_view **views)
424 {
425    struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
426 
427    if (shader == PIPE_SHADER_FRAGMENT) {
428       /* save current */
429       unsigned i;
430       for (i = 0; i < num; i++) {
431          pipe_sampler_view_reference(&pstip->state.sampler_views[start + i],
432                                      views[i]);
433       }
434       for (; i < num + unbind_num_trailing_slots; i++) {
435          pipe_sampler_view_reference(&pstip->state.sampler_views[start + i],
436                                      NULL);
437       }
438       pstip->num_sampler_views = num;
439    }
440 
441    /* pass-through */
442    pstip->driver_set_sampler_views(pstip->pipe, shader, start, num,
443                                    unbind_num_trailing_slots, take_ownership, views);
444 }
445 
446 
447 static void
pstip_set_polygon_stipple(struct pipe_context * pipe,const struct pipe_poly_stipple * stipple)448 pstip_set_polygon_stipple(struct pipe_context *pipe,
449                           const struct pipe_poly_stipple *stipple)
450 {
451    struct pstip_stage *pstip = pstip_stage_from_pipe(pipe);
452 
453    /* save current */
454    pstip->state.stipple = stipple;
455 
456    /* pass-through */
457    pstip->driver_set_polygon_stipple(pstip->pipe, stipple);
458 
459    util_pstipple_update_stipple_texture(pstip->pipe, pstip->texture,
460                                         pstip->state.stipple->stipple);
461 }
462 
463 
464 /**
465  * Called by drivers that want to install this polygon stipple stage
466  * into the draw module's pipeline.  This will not be used if the
467  * hardware has native support for polygon stipple.
468  */
469 bool
draw_install_pstipple_stage(struct draw_context * draw,struct pipe_context * pipe)470 draw_install_pstipple_stage(struct draw_context *draw,
471                             struct pipe_context *pipe)
472 {
473    pipe->draw = (void *) draw;
474 
475    /*
476     * Create / install pgon stipple drawing / prim stage
477     */
478    struct pstip_stage *pstip = draw_pstip_stage(draw, pipe);
479    if (!pstip)
480       goto fail;
481 
482    draw->pipeline.pstipple = &pstip->stage;
483 
484    /* save original driver functions */
485    pstip->driver_create_fs_state = pipe->create_fs_state;
486    pstip->driver_bind_fs_state = pipe->bind_fs_state;
487    pstip->driver_delete_fs_state = pipe->delete_fs_state;
488 
489    pstip->driver_bind_sampler_states = pipe->bind_sampler_states;
490    pstip->driver_set_sampler_views = pipe->set_sampler_views;
491    pstip->driver_set_polygon_stipple = pipe->set_polygon_stipple;
492 
493    /* create special texture, sampler state */
494    pstip->texture = util_pstipple_create_stipple_texture(pipe, NULL);
495    if (!pstip->texture)
496       goto fail;
497 
498    pstip->sampler_view = util_pstipple_create_sampler_view(pipe,
499                                                            pstip->texture);
500    if (!pstip->sampler_view)
501       goto fail;
502 
503    pstip->sampler_cso = util_pstipple_create_sampler(pipe);
504    if (!pstip->sampler_cso)
505       goto fail;
506 
507    /* override the driver's functions */
508    pipe->create_fs_state = pstip_create_fs_state;
509    pipe->bind_fs_state = pstip_bind_fs_state;
510    pipe->delete_fs_state = pstip_delete_fs_state;
511 
512    pipe->bind_sampler_states = pstip_bind_sampler_states;
513    pipe->set_sampler_views = pstip_set_sampler_views;
514    pipe->set_polygon_stipple = pstip_set_polygon_stipple;
515 
516    return true;
517 
518  fail:
519    if (pstip)
520       pstip->stage.destroy(&pstip->stage);
521 
522    return false;
523 }
524