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