summaryrefslogtreecommitdiff
path: root/src/gallium/auxiliary/draw/draw_pt_vsplit_tmp.h
blob: 34c210c8a431cc3cc0324cc031cdd3115ad4998e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * Mesa 3-D graphics library
 *
 * Copyright 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
 * Copyright (C) 2010 LunarG Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#define CONCAT2(name, elt_type) name ## elt_type
#define CONCAT(name, elt_type) CONCAT2(name, elt_type)

#ifdef ELT_TYPE

/**
 * Fetch all elements in [min_index, max_index] with bias, and use the
 * (rebased) index buffer as the draw elements.
 */
static boolean
CONCAT(vsplit_primitive_, ELT_TYPE)(struct vsplit_frontend *vsplit,
                                    unsigned istart, unsigned icount)
{
   struct draw_context *draw = vsplit->draw;
   const ELT_TYPE *ib = (const ELT_TYPE *) draw->pt.user.elts;
   const unsigned min_index = draw->pt.user.min_index;
   const unsigned max_index = draw->pt.user.max_index;
   const int elt_bias = draw->pt.user.eltBias;
   unsigned fetch_start, fetch_count;
   const ushort *draw_elts = NULL;
   unsigned i;
   const unsigned start = istart;
   const unsigned end = istart + icount;

   /* use the ib directly */
   if (min_index == 0 && sizeof(ib[0]) == sizeof(draw_elts[0])) {
      if (icount > vsplit->max_vertices)
         return FALSE;

      for (i = start; i < end; i++) {
         ELT_TYPE idx = DRAW_GET_IDX(ib, i);
         if (idx < min_index || idx > max_index) {
            debug_printf("warning: index out of range\n");
         }
      }
      draw_elts = (const ushort *) (ib + istart);
   }
   else {
      /* have to go through vsplit->draw_elts */
      if (icount > vsplit->segment_size)
         return FALSE;
   }

   /* this is faster only when we fetch less elements than the normal path */
   if (max_index - min_index > icount - 1)
      return FALSE;

   if (elt_bias < 0 && (int) min_index < -elt_bias)
      return FALSE;

   /* why this check? */
   for (i = 0; i < draw->pt.nr_vertex_elements; i++) {
      if (draw->pt.vertex_element[i].instance_divisor)
         return FALSE;
   }

   fetch_start = min_index + elt_bias;
   fetch_count = max_index - min_index + 1;

   if (!draw_elts) {
      if (min_index == 0) {
         for (i = start; i < end; i++) {
            ELT_TYPE idx = DRAW_GET_IDX(ib, i);

            if (idx < min_index || idx > max_index) {
               debug_printf("warning: index out of range\n");
            }
            vsplit->draw_elts[i - start] = (ushort) idx;
         }
      }
      else {
         for (i = start; i < end; i++) {
            ELT_TYPE idx = DRAW_GET_IDX(ib, i);

            if (idx < min_index || idx > max_index) {
               debug_printf("warning: index out of range\n");
            }
            vsplit->draw_elts[i - start] = (ushort) (idx - min_index);
         }
      }

      draw_elts = vsplit->draw_elts;
   }

   return vsplit->middle->run_linear_elts(vsplit->middle,
                                          fetch_start, fetch_count,
                                          draw_elts, icount, 0x0);
}

/**
 * Use the cache to prepare the fetch and draw elements, and flush.
 *
 * When spoken is TRUE, ispoken replaces istart;  When close is TRUE, iclose is
 * appended.
 */
static INLINE void
CONCAT(vsplit_segment_cache_, ELT_TYPE)(struct vsplit_frontend *vsplit,
                                        unsigned flags,
                                        unsigned istart, unsigned icount,
                                        boolean spoken, unsigned ispoken,
                                        boolean close, unsigned iclose)
{
   struct draw_context *draw = vsplit->draw;
   const ELT_TYPE *ib = (const ELT_TYPE *) draw->pt.user.elts;
   const int ibias = draw->pt.user.eltBias;
   unsigned i;

   assert(icount + !!close <= vsplit->segment_size);

   vsplit_clear_cache(vsplit);

   spoken = !!spoken;
   if (ibias == 0) {
      if (spoken)
         ADD_CACHE(vsplit, DRAW_GET_IDX(ib, ispoken));

      for (i = spoken; i < icount; i++)
         ADD_CACHE(vsplit, DRAW_GET_IDX(ib, istart + i));

      if (close)
         ADD_CACHE(vsplit, DRAW_GET_IDX(ib, iclose));
   }
   else if (ibias > 0) {
      if (spoken)
         ADD_CACHE(vsplit, (uint) DRAW_GET_IDX(ib, ispoken) + ibias);

      for (i = spoken; i < icount; i++)
         ADD_CACHE(vsplit, (uint) DRAW_GET_IDX(ib, istart + i) + ibias);

      if (close)
         ADD_CACHE(vsplit, (uint) DRAW_GET_IDX(ib, iclose) + ibias);
   }
   else {
      if (spoken) {
         if ((int) ib[ispoken] < -ibias)
            return;
         ADD_CACHE(vsplit, DRAW_GET_IDX(ib, ispoken) + ibias);
      }

      for (i = spoken; i < icount; i++) {
         if ((int) DRAW_GET_IDX(ib, istart + i) < -ibias)
            return;
         ADD_CACHE(vsplit, DRAW_GET_IDX(ib, istart + i) + ibias);
      }

      if (close) {
         if ((int) DRAW_GET_IDX(ib, iclose) < -ibias)
            return;
         ADD_CACHE(vsplit, DRAW_GET_IDX(ib, iclose) + ibias);
      }
   }

   vsplit_flush_cache(vsplit, flags);
}

static void
CONCAT(vsplit_segment_simple_, ELT_TYPE)(struct vsplit_frontend *vsplit,
                                         unsigned flags,
                                         unsigned istart,
                                         unsigned icount)
{
   CONCAT(vsplit_segment_cache_, ELT_TYPE)(vsplit,
         flags, istart, icount, FALSE, 0, FALSE, 0);
}

static void
CONCAT(vsplit_segment_loop_, ELT_TYPE)(struct vsplit_frontend *vsplit,
                                       unsigned flags,
                                       unsigned istart,
                                       unsigned icount,
                                       unsigned i0)
{
   const boolean close_loop = ((flags) == DRAW_SPLIT_BEFORE);

   CONCAT(vsplit_segment_cache_, ELT_TYPE)(vsplit,
         flags, istart, icount, FALSE, 0, close_loop, i0);
}

static void
CONCAT(vsplit_segment_fan_, ELT_TYPE)(struct vsplit_frontend *vsplit,
                                      unsigned flags,
                                      unsigned istart,
                                      unsigned icount,
                                      unsigned i0)
{
   const boolean use_spoken = (((flags) & DRAW_SPLIT_BEFORE) != 0);

   CONCAT(vsplit_segment_cache_, ELT_TYPE)(vsplit,
         flags, istart, icount, use_spoken, i0, FALSE, 0);
}

#define LOCAL_VARS                                                         \
   struct vsplit_frontend *vsplit = (struct vsplit_frontend *) frontend;   \
   const unsigned prim = vsplit->prim;                                     \
   const unsigned max_count_simple = vsplit->segment_size;                 \
   const unsigned max_count_loop = vsplit->segment_size - 1;               \
   const unsigned max_count_fan = vsplit->segment_size;

#define PRIMITIVE(istart, icount)   \
   CONCAT(vsplit_primitive_, ELT_TYPE)(vsplit, istart, icount)

#else /* ELT_TYPE */

static void
vsplit_segment_simple_linear(struct vsplit_frontend *vsplit, unsigned flags,
                             unsigned istart, unsigned icount)
{
   assert(icount <= vsplit->max_vertices);
   vsplit->middle->run_linear(vsplit->middle, istart, icount, flags);
}

static void
vsplit_segment_loop_linear(struct vsplit_frontend *vsplit, unsigned flags,
                           unsigned istart, unsigned icount, unsigned i0)
{
   boolean close_loop = (flags == DRAW_SPLIT_BEFORE);
   unsigned nr;

   assert(icount + !!close_loop <= vsplit->segment_size);

   if (close_loop) {
      for (nr = 0; nr < icount; nr++)
         vsplit->fetch_elts[nr] = istart + nr;
      vsplit->fetch_elts[nr++] = i0;

      vsplit->middle->run(vsplit->middle, vsplit->fetch_elts, nr,
            vsplit->identity_draw_elts, nr, flags);
   }
   else {
      vsplit->middle->run_linear(vsplit->middle, istart, icount, flags);
   }
}

static void
vsplit_segment_fan_linear(struct vsplit_frontend *vsplit, unsigned flags,
                          unsigned istart, unsigned icount, unsigned i0)
{
   boolean use_spoken = ((flags & DRAW_SPLIT_BEFORE) != 0);
   unsigned nr = 0, i;

   assert(icount <= vsplit->segment_size);

   if (use_spoken) {
      /* replace istart by i0 */
      vsplit->fetch_elts[nr++] = i0;
      for (i = 1 ; i < icount; i++)
         vsplit->fetch_elts[nr++] = istart + i;

      vsplit->middle->run(vsplit->middle, vsplit->fetch_elts, nr,
            vsplit->identity_draw_elts, nr, flags);
   }
   else {
      vsplit->middle->run_linear(vsplit->middle, istart, icount, flags);
   }
}

#define LOCAL_VARS                                                         \
   struct vsplit_frontend *vsplit = (struct vsplit_frontend *) frontend;   \
   const unsigned prim = vsplit->prim;                                     \
   const unsigned max_count_simple = vsplit->max_vertices;                 \
   const unsigned max_count_loop = vsplit->segment_size - 1;               \
   const unsigned max_count_fan = vsplit->segment_size;

#define PRIMITIVE(istart, icount) FALSE

#define ELT_TYPE linear

#endif /* ELT_TYPE */

#define FUNC_VARS                      \
   struct draw_pt_front_end *frontend, \
   unsigned start,                     \
   unsigned count

#define SEGMENT_SIMPLE(flags, istart, icount)   \
   CONCAT(vsplit_segment_simple_, ELT_TYPE)(vsplit, flags, istart, icount)

#define SEGMENT_LOOP(flags, istart, icount, i0) \
   CONCAT(vsplit_segment_loop_, ELT_TYPE)(vsplit, flags, istart, icount, i0)

#define SEGMENT_FAN(flags, istart, icount, i0)  \
   CONCAT(vsplit_segment_fan_, ELT_TYPE)(vsplit, flags, istart, icount, i0)

#include "draw_split_tmp.h"

#undef CONCAT2
#undef CONCAT

#undef ELT_TYPE
#undef ADD_CACHE