xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/draw/draw_split_tmp.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright 2008 VMware, Inc.
5  * Copyright (C) 2010 LunarG Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  */
25 
26 static void
FUNC(FUNC_VARS)27 FUNC(FUNC_VARS)
28 {
29    unsigned first, incr;
30    LOCAL_VARS
31 
32    /*
33     * prim, start, count, and max_count_{simple,loop,fan} should have been
34     * defined
35     */
36    if (0) {
37       debug_printf("%s: prim 0x%x, start %d, count %d, max_count_simple %d, "
38                    "max_count_loop %d, max_count_fan %d\n",
39                    __func__, prim, start, count, max_count_simple,
40                    max_count_loop, max_count_fan);
41    }
42 
43    if (prim == MESA_PRIM_PATCHES) {
44       first = vsplit->draw->pt.vertices_per_patch;
45       incr = vsplit->draw->pt.vertices_per_patch;
46    } else
47       draw_pt_split_prim(prim, &first, &incr);
48    /* sanitize primitive length */
49    count = draw_pt_trim_count(count, first, incr);
50    if (count < first)
51       return;
52 
53    /* try flushing the entire primitive */
54    if (PRIMITIVE(start, count))
55       return;
56 
57    /* must be able to at least flush two complete primitives */
58    assert(max_count_simple >= first + incr &&
59           max_count_loop >= first + incr &&
60           max_count_fan >= first + incr);
61 
62    /* no splitting required */
63    if (count <= max_count_simple) {
64       SEGMENT_SIMPLE(0x0, start, count);
65    } else {
66       const unsigned rollback = first - incr;
67       unsigned flags = DRAW_SPLIT_AFTER, seg_start = 0, seg_max;
68 
69       /*
70        * Both count and seg_max below are explicitly trimmed.  Because
71        *
72        *   seg_start = N * (seg_max - rollback) = N' * incr,
73        *
74        * we have
75        *
76        *   remaining = count - seg_start = first + N'' * incr.
77        *
78        * That is, remaining is implicitly trimmed.
79        */
80       switch (prim) {
81       case MESA_PRIM_PATCHES:
82       case MESA_PRIM_POINTS:
83       case MESA_PRIM_LINES:
84       case MESA_PRIM_LINE_STRIP:
85       case MESA_PRIM_TRIANGLES:
86       case MESA_PRIM_TRIANGLE_STRIP:
87       case MESA_PRIM_QUADS:
88       case MESA_PRIM_QUAD_STRIP:
89       case MESA_PRIM_LINES_ADJACENCY:
90       case MESA_PRIM_LINE_STRIP_ADJACENCY:
91       case MESA_PRIM_TRIANGLES_ADJACENCY:
92       case MESA_PRIM_TRIANGLE_STRIP_ADJACENCY:
93          seg_max =
94             draw_pt_trim_count(MIN2(max_count_simple, count), first, incr);
95          if (prim == MESA_PRIM_TRIANGLE_STRIP ||
96              prim == MESA_PRIM_TRIANGLE_STRIP_ADJACENCY) {
97             /* make sure we flush even number of triangles at a time */
98             if (seg_max < count && !(((seg_max - first) / incr) & 1))
99                seg_max -= incr;
100          }
101 
102          do {
103             const unsigned remaining = count - seg_start;
104 
105             if (remaining > seg_max) {
106                SEGMENT_SIMPLE(flags, start + seg_start, seg_max);
107                seg_start += seg_max - rollback;
108 
109                flags |= DRAW_SPLIT_BEFORE;
110             } else {
111                flags &= ~DRAW_SPLIT_AFTER;
112 
113                SEGMENT_SIMPLE(flags, start + seg_start, remaining);
114                seg_start += remaining;
115             }
116          } while (seg_start < count);
117          break;
118 
119       case MESA_PRIM_LINE_LOOP:
120          seg_max =
121             draw_pt_trim_count(MIN2(max_count_loop, count), first, incr);
122 
123          do {
124             const unsigned remaining = count - seg_start;
125 
126             if (remaining > seg_max) {
127                SEGMENT_LOOP(flags, start + seg_start, seg_max, start);
128                seg_start += seg_max - rollback;
129 
130                flags |= DRAW_SPLIT_BEFORE;
131             } else {
132                flags &= ~DRAW_SPLIT_AFTER;
133 
134                SEGMENT_LOOP(flags, start + seg_start, remaining, start);
135                seg_start += remaining;
136             }
137          } while (seg_start < count);
138          break;
139 
140       case MESA_PRIM_TRIANGLE_FAN:
141       case MESA_PRIM_POLYGON:
142          seg_max =
143             draw_pt_trim_count(MIN2(max_count_fan, count), first, incr);
144 
145          do {
146             const unsigned remaining = count - seg_start;
147 
148             if (remaining > seg_max) {
149                SEGMENT_FAN(flags, start + seg_start, seg_max, start);
150                seg_start += seg_max - rollback;
151 
152                flags |= DRAW_SPLIT_BEFORE;
153             } else {
154                flags &= ~DRAW_SPLIT_AFTER;
155 
156                SEGMENT_FAN(flags, start + seg_start, remaining, start);
157                seg_start += remaining;
158             }
159          } while (seg_start < count);
160          break;
161 
162       default:
163          assert(0);
164          break;
165       }
166    }
167 }
168 
169 #undef FUNC
170 #undef FUNC_VARS
171 #undef LOCAL_VARS
172 
173 #undef PRIMITIVE
174 #undef SEGMENT_SIMPLE
175 #undef SEGMENT_LOOP
176 #undef SEGMENT_FAN
177