xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/draw/draw_pt_mesh_pipeline.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2023 Red Hat.
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 "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  **************************************************************************/
25 
26 #include "draw/draw_context.h"
27 #include "draw/draw_private.h"
28 #include "draw/draw_pt.h"
29 #include "draw/draw_mesh.h"
30 
31 /*
32  * Mesh shader middle end,
33  * This doesn't need a frontend as mesh shader frontend
34  * is done with compute shaders in llvmpipe.
35  */
36 
37 struct mesh_pipeline_middle_end {
38    struct draw_pt_middle_end base;
39    struct draw_context *draw;
40 
41    struct pt_so_emit *so_emit;
42    struct pt_post_vs *post_vs;
43 };
44 
45 /** cast wrapper */
46 static inline struct mesh_pipeline_middle_end *
mesh_pipeline_middle_end(struct draw_pt_middle_end * middle)47 mesh_pipeline_middle_end(struct draw_pt_middle_end *middle)
48 {
49    return (struct mesh_pipeline_middle_end *) middle;
50 }
51 
52 static void
mesh_pipeline_destroy(struct draw_pt_middle_end * middle)53 mesh_pipeline_destroy(struct draw_pt_middle_end *middle)
54 {
55    struct mesh_pipeline_middle_end *mpme = mesh_pipeline_middle_end(middle);
56 
57    if (mpme->so_emit)
58       draw_pt_so_emit_destroy(mpme->so_emit);
59    if (mpme->post_vs)
60       draw_pt_post_vs_destroy(mpme->post_vs);
61    FREE(middle);
62 }
63 
64 static void
mesh_middle_end_prepare(struct draw_pt_middle_end * middle,enum mesa_prim prim,unsigned opt,unsigned * max_vertices)65 mesh_middle_end_prepare(struct draw_pt_middle_end *middle,
66                         enum mesa_prim prim,
67                         unsigned opt,
68                         unsigned *max_vertices)
69 {
70    struct mesh_pipeline_middle_end *mpme = mesh_pipeline_middle_end(middle);
71    struct draw_context *draw = mpme->draw;
72    enum mesa_prim out_prim = draw->ms.mesh_shader->output_primitive;
73    unsigned point_clip = draw->rasterizer->fill_front == PIPE_POLYGON_MODE_POINT ||
74                          out_prim == MESA_PRIM_POINTS;
75    draw_pt_post_vs_prepare(mpme->post_vs,
76                            draw->clip_xy,
77                            draw->clip_z,
78                            draw->clip_user,
79                            point_clip ? draw->guard_band_points_lines_xy :
80                                         draw->guard_band_xy,
81                            draw->bypass_viewport,
82                            draw->rasterizer->clip_halfz,
83                            false);
84 
85    draw_pt_so_emit_prepare(mpme->so_emit, false);
86 
87    draw_do_flush(draw, DRAW_FLUSH_BACKEND);
88 
89 }
90 
91 void
draw_mesh_middle_end_run(struct draw_pt_middle_end * middle,struct draw_vertex_info * vert_info,struct draw_prim_info * prim_info)92 draw_mesh_middle_end_run(struct draw_pt_middle_end *middle,
93                          struct draw_vertex_info *vert_info,
94                          struct draw_prim_info *prim_info)
95 {
96    struct mesh_pipeline_middle_end *mpme = mesh_pipeline_middle_end(middle);
97 
98    draw_pt_post_vs_run(mpme->post_vs, vert_info, prim_info);
99 
100    /* just for primgen query */
101    draw_pt_so_emit(mpme->so_emit, 1, vert_info, prim_info);
102    draw_pipeline_run_linear(mpme->draw, vert_info, prim_info);
103 }
104 
105 struct draw_pt_middle_end *
draw_pt_mesh_pipeline_or_emit(struct draw_context * draw)106 draw_pt_mesh_pipeline_or_emit(struct draw_context *draw)
107 {
108    struct mesh_pipeline_middle_end *mpme =
109       CALLOC_STRUCT(mesh_pipeline_middle_end);
110    if (!mpme)
111       goto fail;
112 
113    mpme->base.prepare = mesh_middle_end_prepare;
114    mpme->base.destroy = mesh_pipeline_destroy;
115    mpme->draw = draw;
116 
117    mpme->post_vs = draw_pt_post_vs_create(draw);
118    if (!mpme->post_vs)
119       goto fail;
120 
121    mpme->so_emit = draw_pt_so_emit_create(draw);
122    if (!mpme->so_emit)
123       goto fail;
124 
125    return &mpme->base;
126  fail:
127    if (mpme)
128       mesh_pipeline_destroy(&mpme->base);
129 
130    return NULL;
131 }
132