xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/draw/draw_pt_post_vs.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 #include "util/u_memory.h"
29 #include "util/u_math.h"
30 #include "util/u_prim.h"
31 #include "pipe/p_context.h"
32 #include "draw/draw_context.h"
33 #include "draw/draw_private.h"
34 #include "draw/draw_pt.h"
35 #include "draw/draw_vs.h"
36 
37 #define DO_CLIP_XY           0x1
38 #define DO_CLIP_FULL_Z       0x2
39 #define DO_CLIP_HALF_Z       0x4
40 #define DO_CLIP_USER         0x8
41 #define DO_VIEWPORT          0x10
42 #define DO_EDGEFLAG          0x20
43 #define DO_CLIP_XY_GUARD_BAND 0x40
44 
45 
46 struct pt_post_vs {
47    struct draw_context *draw;
48 
49    unsigned flags;
50 
51    bool (*run)(struct pt_post_vs *pvs,
52                struct draw_vertex_info *info,
53                const struct draw_prim_info *prim_info);
54 };
55 
56 
57 static inline void
initialize_vertex_header(struct vertex_header * header)58 initialize_vertex_header(struct vertex_header *header)
59 {
60    header->clipmask = 0;
61    header->edgeflag = 1;
62    header->pad = 0;
63    header->vertex_id = UNDEFINED_VERTEX_ID;
64 }
65 
66 
67 static inline float
dot4(const float * a,const float * b)68 dot4(const float *a, const float *b)
69 {
70    return (a[0] * b[0] +
71            a[1] * b[1] +
72            a[2] * b[2] +
73            a[3] * b[3]);
74 }
75 
76 
77 #define FLAGS (0)
78 #define TAG(x) x##_none
79 #include "draw_cliptest_tmp.h"
80 
81 #define FLAGS (DO_CLIP_XY | DO_CLIP_FULL_Z | DO_VIEWPORT)
82 #define TAG(x) x##_xy_fullz_viewport
83 #include "draw_cliptest_tmp.h"
84 
85 #define FLAGS (DO_CLIP_XY | DO_CLIP_HALF_Z | DO_VIEWPORT)
86 #define TAG(x) x##_xy_halfz_viewport
87 #include "draw_cliptest_tmp.h"
88 
89 #define FLAGS (DO_CLIP_XY_GUARD_BAND | DO_CLIP_HALF_Z | DO_VIEWPORT)
90 #define TAG(x) x##_xy_gb_halfz_viewport
91 #include "draw_cliptest_tmp.h"
92 
93 #define FLAGS (DO_CLIP_XY_GUARD_BAND | DO_CLIP_FULL_Z | DO_VIEWPORT)
94 #define TAG(x) x##_xy_gb_fullz_viewport
95 #include "draw_cliptest_tmp.h"
96 
97 #define FLAGS (DO_CLIP_FULL_Z | DO_VIEWPORT)
98 #define TAG(x) x##_fullz_viewport
99 #include "draw_cliptest_tmp.h"
100 
101 #define FLAGS (DO_CLIP_HALF_Z | DO_VIEWPORT)
102 #define TAG(x) x##_halfz_viewport
103 #include "draw_cliptest_tmp.h"
104 
105 #define FLAGS (DO_CLIP_XY | DO_CLIP_FULL_Z | DO_CLIP_USER | DO_VIEWPORT)
106 #define TAG(x) x##_xy_fullz_user_viewport
107 #include "draw_cliptest_tmp.h"
108 
109 #define FLAGS (DO_CLIP_XY | DO_CLIP_FULL_Z | DO_CLIP_USER | DO_VIEWPORT | DO_EDGEFLAG)
110 #define TAG(x) x##_xy_fullz_user_viewport_edgeflag
111 #include "draw_cliptest_tmp.h"
112 
113 
114 
115 /* Don't want to create 64 versions of this function, so catch the
116  * less common ones here.  This is looking like something which should
117  * be code-generated, perhaps appended to the end of the vertex
118  * shader.
119  */
120 #define FLAGS (pvs->flags)
121 #define TAG(x) x##_generic
122 #include "draw_cliptest_tmp.h"
123 
124 
125 bool
draw_pt_post_vs_run(struct pt_post_vs * pvs,struct draw_vertex_info * info,const struct draw_prim_info * prim_info)126 draw_pt_post_vs_run(struct pt_post_vs *pvs,
127                     struct draw_vertex_info *info,
128                     const struct draw_prim_info *prim_info)
129 {
130    return pvs->run(pvs, info, prim_info);
131 }
132 
133 
134 void
draw_pt_post_vs_prepare(struct pt_post_vs * pvs,bool clip_xy,bool clip_z,bool clip_user,bool guard_band,bool bypass_viewport,bool clip_halfz,bool need_edgeflags)135 draw_pt_post_vs_prepare(struct pt_post_vs *pvs,
136                         bool clip_xy,
137                         bool clip_z,
138                         bool clip_user,
139                         bool guard_band,
140                         bool bypass_viewport,
141                         bool clip_halfz,
142                         bool need_edgeflags)
143 {
144    pvs->flags = 0;
145 
146    if (clip_xy && !guard_band) {
147       pvs->flags |= DO_CLIP_XY;
148       ASSIGN_4V(pvs->draw->plane[0], -1,  0,  0, 1);
149       ASSIGN_4V(pvs->draw->plane[1],  1,  0,  0, 1);
150       ASSIGN_4V(pvs->draw->plane[2],  0, -1,  0, 1);
151       ASSIGN_4V(pvs->draw->plane[3],  0,  1,  0, 1);
152    } else if (clip_xy && guard_band) {
153       pvs->flags |= DO_CLIP_XY_GUARD_BAND;
154       ASSIGN_4V(pvs->draw->plane[0], -0.5,  0,  0, 1);
155       ASSIGN_4V(pvs->draw->plane[1],  0.5,  0,  0, 1);
156       ASSIGN_4V(pvs->draw->plane[2],  0, -0.5,  0, 1);
157       ASSIGN_4V(pvs->draw->plane[3],  0,  0.5,  0, 1);
158    }
159 
160    if (clip_z) {
161       if (clip_halfz) {
162          pvs->flags |= DO_CLIP_HALF_Z;
163          ASSIGN_4V(pvs->draw->plane[4],  0,  0,  1, 0);
164       } else {
165          pvs->flags |= DO_CLIP_FULL_Z;
166          ASSIGN_4V(pvs->draw->plane[4],  0,  0,  1, 1);
167       }
168    }
169 
170    if (clip_user)
171       pvs->flags |= DO_CLIP_USER;
172 
173    if (!bypass_viewport)
174       pvs->flags |= DO_VIEWPORT;
175 
176    if (need_edgeflags)
177       pvs->flags |= DO_EDGEFLAG;
178 
179    /* Now select the relevant function:
180     */
181    switch (pvs->flags) {
182    case 0:
183       pvs->run = do_cliptest_none;
184       break;
185    case DO_CLIP_XY | DO_CLIP_FULL_Z | DO_VIEWPORT:
186       pvs->run = do_cliptest_xy_fullz_viewport;
187       break;
188    case DO_CLIP_XY | DO_CLIP_HALF_Z | DO_VIEWPORT:
189       pvs->run = do_cliptest_xy_halfz_viewport;
190       break;
191    case DO_CLIP_XY_GUARD_BAND | DO_CLIP_HALF_Z | DO_VIEWPORT:
192       pvs->run = do_cliptest_xy_gb_halfz_viewport;
193       break;
194    case DO_CLIP_XY_GUARD_BAND | DO_CLIP_FULL_Z | DO_VIEWPORT:
195       pvs->run = do_cliptest_xy_gb_fullz_viewport;
196       break;
197    case DO_CLIP_FULL_Z | DO_VIEWPORT:
198       pvs->run = do_cliptest_fullz_viewport;
199       break;
200    case DO_CLIP_HALF_Z | DO_VIEWPORT:
201       pvs->run = do_cliptest_halfz_viewport;
202       break;
203    case DO_CLIP_XY | DO_CLIP_FULL_Z | DO_CLIP_USER | DO_VIEWPORT:
204       pvs->run = do_cliptest_xy_fullz_user_viewport;
205       break;
206    case (DO_CLIP_XY | DO_CLIP_FULL_Z | DO_CLIP_USER |
207          DO_VIEWPORT | DO_EDGEFLAG):
208       pvs->run = do_cliptest_xy_fullz_user_viewport_edgeflag;
209       break;
210    default:
211       pvs->run = do_cliptest_generic;
212       break;
213    }
214 }
215 
216 
217 struct pt_post_vs *
draw_pt_post_vs_create(struct draw_context * draw)218 draw_pt_post_vs_create(struct draw_context *draw)
219 {
220    struct pt_post_vs *pvs = CALLOC_STRUCT(pt_post_vs);
221    if (!pvs)
222       return NULL;
223 
224    pvs->draw = draw;
225 
226    return pvs;
227 }
228 
229 
230 void
draw_pt_post_vs_destroy(struct pt_post_vs * pvs)231 draw_pt_post_vs_destroy(struct pt_post_vs *pvs)
232 {
233    FREE(pvs);
234 }
235