OSDN Git Service

draw: quieten some debug
[android-x86/external-mesa.git] / src / gallium / auxiliary / draw / draw_pt_vcache.c
1 /**************************************************************************
2  * 
3  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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  /*
29   * Authors:
30   *   Keith Whitwell <keith@tungstengraphics.com>
31   */
32
33 #include "pipe/p_util.h"
34 #include "draw/draw_context.h"
35 #include "draw/draw_private.h"
36 #include "draw/draw_pt.h"
37
38
39 #define CACHE_MAX 1024
40 #define FETCH_MAX 4096
41 #define DRAW_MAX (16*1024)
42
43 struct vcache_frontend {
44    struct draw_pt_front_end base;
45    struct draw_context *draw;
46
47    unsigned in[CACHE_MAX];
48    ushort out[CACHE_MAX];
49
50    ushort draw_elts[DRAW_MAX];
51    unsigned fetch_elts[FETCH_MAX];
52
53    unsigned draw_count;
54    unsigned fetch_count;
55    
56    struct draw_pt_middle_end *middle;
57
58    unsigned input_prim;
59    unsigned output_prim;
60 };
61
62 static void vcache_flush( struct vcache_frontend *vcache )
63 {
64    if (vcache->draw_count) {
65       vcache->middle->run( vcache->middle,
66                            vcache->fetch_elts,
67                            vcache->fetch_count,
68                            vcache->draw_elts,
69                            vcache->draw_count );
70    }
71
72    memset(vcache->in, ~0, sizeof(vcache->in));
73    vcache->fetch_count = 0;
74    vcache->draw_count = 0;
75 }
76
77 static void vcache_check_flush( struct vcache_frontend *vcache )
78 {
79    if ( vcache->draw_count + 6 >= DRAW_MAX ||
80         vcache->fetch_count + 4 >= FETCH_MAX )
81    {
82       vcache_flush( vcache );
83    }
84 }
85
86
87 static INLINE void vcache_elt( struct vcache_frontend *vcache,
88                                unsigned felt,
89                                ushort flags )
90 {
91    unsigned idx = felt % CACHE_MAX;
92
93    if (vcache->in[idx] != felt) {
94       assert(vcache->fetch_count < FETCH_MAX);
95
96       vcache->in[idx] = felt;
97       vcache->out[idx] = (ushort)vcache->fetch_count;
98       vcache->fetch_elts[vcache->fetch_count++] = felt;
99    }
100
101    vcache->draw_elts[vcache->draw_count++] = vcache->out[idx] | flags;
102 }
103
104
105                    
106 static void vcache_triangle( struct vcache_frontend *vcache,
107                              unsigned i0,
108                              unsigned i1,
109                              unsigned i2 )
110 {
111    vcache_elt(vcache, i0, 0);
112    vcache_elt(vcache, i1, 0);
113    vcache_elt(vcache, i2, 0);
114    vcache_check_flush(vcache);
115 }
116
117                           
118 static void vcache_triangle_flags( struct vcache_frontend *vcache,
119                                    ushort flags,
120                                    unsigned i0,
121                                    unsigned i1,
122                                    unsigned i2 )
123 {
124    vcache_elt(vcache, i0, flags);
125    vcache_elt(vcache, i1, 0);
126    vcache_elt(vcache, i2, 0);
127    vcache_check_flush(vcache);
128 }
129
130 static void vcache_line( struct vcache_frontend *vcache,
131                          unsigned i0,
132                          unsigned i1 )
133 {
134    vcache_elt(vcache, i0, 0);
135    vcache_elt(vcache, i1, 0);
136    vcache_check_flush(vcache);
137 }
138
139
140 static void vcache_line_flags( struct vcache_frontend *vcache,
141                                ushort flags,
142                                unsigned i0,
143                                unsigned i1 )
144 {
145    vcache_elt(vcache, i0, flags);
146    vcache_elt(vcache, i1, 0);
147    vcache_check_flush(vcache);
148 }
149
150
151 static void vcache_point( struct vcache_frontend *vcache,
152                           unsigned i0 )
153 {
154    vcache_elt(vcache, i0, 0);
155    vcache_check_flush(vcache);
156 }
157
158 static void vcache_quad( struct vcache_frontend *vcache,
159                          unsigned i0,
160                          unsigned i1,
161                          unsigned i2,
162                          unsigned i3 )
163 {
164    vcache_triangle( vcache, i0, i1, i3 );
165    vcache_triangle( vcache, i1, i2, i3 );
166 }
167
168 static void vcache_ef_quad( struct vcache_frontend *vcache,
169                             unsigned i0,
170                             unsigned i1,
171                             unsigned i2,
172                             unsigned i3 )
173 {
174    vcache_triangle_flags( vcache,
175                           ( DRAW_PIPE_RESET_STIPPLE |
176                             DRAW_PIPE_EDGE_FLAG_0 |
177                             DRAW_PIPE_EDGE_FLAG_2 ),
178                           i0, i1, i3 );
179
180    vcache_triangle_flags( vcache,
181                           ( DRAW_PIPE_EDGE_FLAG_0 |
182                             DRAW_PIPE_EDGE_FLAG_1 ),
183                           i1, i2, i3 );
184 }
185
186 /* At least for now, we're back to using a template include file for
187  * this.  The two paths aren't too different though - it may be
188  * possible to reunify them.
189  */
190 #define TRIANGLE(vc,flags,i0,i1,i2) vcache_triangle_flags(vc,flags,i0,i1,i2)
191 #define QUAD(vc,i0,i1,i2,i3)        vcache_ef_quad(vc,i0,i1,i2,i3)
192 #define LINE(vc,flags,i0,i1)        vcache_line_flags(vc,flags,i0,i1)
193 #define POINT(vc,i0)                vcache_point(vc,i0)
194 #define FUNC vcache_run_extras
195 #include "draw_pt_vcache_tmp.h"
196
197 #define TRIANGLE(vc,flags,i0,i1,i2) vcache_triangle(vc,i0,i1,i2)
198 #define QUAD(vc,i0,i1,i2,i3)        vcache_quad(vc,i0,i1,i2,i3)
199 #define LINE(vc,flags,i0,i1)        vcache_line(vc,i0,i1)
200 #define POINT(vc,i0)                vcache_point(vc,i0)
201 #define FUNC vcache_run
202 #include "draw_pt_vcache_tmp.h"
203
204 static void translate_uint_elts( const unsigned *src,
205                                  unsigned count,
206                                  int delta,
207                                  ushort *dest )
208 {
209    unsigned i;
210
211    for (i = 0; i < count; i++) 
212       dest[i] = (ushort)(src[i] + delta);
213 }
214
215 static void translate_ushort_elts( const ushort *src,
216                                    unsigned count,
217                                    int delta,
218                                    ushort *dest )
219 {
220    unsigned i;
221
222    for (i = 0; i < count; i++) 
223       dest[i] = (ushort)(src[i] + delta);
224 }
225
226 static void translate_ubyte_elts( const ubyte *src,
227                                   unsigned count,
228                                   int delta,
229                                   ushort *dest )
230 {
231    unsigned i;
232
233    for (i = 0; i < count; i++) 
234       dest[i] = (ushort)(src[i] + delta);
235 }
236
237 #if 0
238 static enum pipe_format format_from_get_elt( pt_elt_func get_elt )
239 {
240    switch (draw->pt.user.eltSize) {
241    case 1: return PIPE_FORMAT_R8_UNORM;
242    case 2: return PIPE_FORMAT_R16_UNORM;
243    case 4: return PIPE_FORMAT_R32_UNORM;
244    default: return PIPE_FORMAT_NONE;
245    }
246 }
247 #endif
248
249 static void vcache_check_run( struct draw_pt_front_end *frontend, 
250                               pt_elt_func get_elt,
251                               const void *elts,
252                               unsigned draw_count )
253 {
254    struct vcache_frontend *vcache = (struct vcache_frontend *)frontend; 
255    struct draw_context *draw = vcache->draw;
256    unsigned min_index = draw->pt.user.min_index;
257    unsigned max_index = draw->pt.user.max_index;
258    unsigned index_size = draw->pt.user.eltSize;
259    unsigned fetch_count = max_index + 1 - min_index;
260    const ushort *transformed_elts;
261    ushort *storage = NULL;
262
263
264    if (0) debug_printf("fetch_count %d draw_count %d\n", fetch_count, draw_count);
265       
266    if (max_index == 0xffffffff ||
267        fetch_count >= FETCH_MAX ||
268        fetch_count > draw_count) {
269       if (0) debug_printf("fail\n");
270       goto fail;
271    }
272       
273
274    if (min_index == 0 &&
275        index_size == 2) 
276    {
277       transformed_elts = (const ushort *)elts;
278    }
279    else 
280    {
281       storage = MALLOC( draw_count * sizeof(ushort) );
282       if (!storage)
283          goto fail;
284       
285       switch(index_size) {
286       case 1:
287          translate_ubyte_elts( (const ubyte *)elts,
288                                draw_count,
289                                0 - (int)min_index,
290                                storage );
291          break;
292
293       case 2:
294          translate_ushort_elts( (const ushort *)elts,
295                                 draw_count,
296                                 0 - (int)min_index,
297                                 storage );
298          break;
299
300       case 4:
301          translate_uint_elts( (const uint *)elts,
302                               draw_count,
303                               0 - (int)min_index,
304                               storage );
305          break;
306
307       default:
308          assert(0);
309          return;
310       }
311       transformed_elts = storage;
312    }
313
314    vcache->middle->run_linear_elts( vcache->middle,
315                                     min_index, /* start */
316                                     fetch_count,
317                                     transformed_elts,
318                                     draw_count );
319
320    FREE(storage);
321    return;
322
323  fail:
324    vcache_run( frontend, get_elt, elts, draw_count );
325 }
326
327
328
329
330 static void vcache_prepare( struct draw_pt_front_end *frontend,
331                             unsigned prim,
332                             struct draw_pt_middle_end *middle,
333                             unsigned opt )
334 {
335    struct vcache_frontend *vcache = (struct vcache_frontend *)frontend;
336
337    if (opt & PT_PIPELINE)
338    {
339       vcache->base.run = vcache_run_extras;
340    }
341    else 
342    {
343       vcache->base.run = vcache_check_run;
344    }
345
346    vcache->input_prim = prim;
347    vcache->output_prim = draw_pt_reduced_prim(prim);
348
349    vcache->middle = middle;
350    middle->prepare( middle, vcache->output_prim, opt );
351 }
352
353
354
355
356 static void vcache_finish( struct draw_pt_front_end *frontend )
357 {
358    struct vcache_frontend *vcache = (struct vcache_frontend *)frontend;
359    vcache->middle->finish( vcache->middle );
360    vcache->middle = NULL;
361 }
362
363 static void vcache_destroy( struct draw_pt_front_end *frontend )
364 {
365    FREE(frontend);
366 }
367
368
369 struct draw_pt_front_end *draw_pt_vcache( struct draw_context *draw )
370 {
371    struct vcache_frontend *vcache = CALLOC_STRUCT( vcache_frontend );
372    if (vcache == NULL)
373       return NULL;
374  
375    vcache->base.prepare = vcache_prepare;
376    vcache->base.run     = NULL;
377    vcache->base.finish  = vcache_finish;
378    vcache->base.destroy = vcache_destroy;
379    vcache->draw = draw;
380    
381    memset(vcache->in, ~0, sizeof(vcache->in));
382   
383    return &vcache->base;
384 }