Simplify makefile boilerplate code.
[mesa.git] / src / gallium / auxiliary / draw / draw_vertex_cache.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_private.h"
35 #include "draw_context.h"
36
37
38 void draw_vertex_cache_invalidate( struct draw_context *draw )
39 {
40 assert(draw->pq.queue_nr == 0);
41 assert(draw->vs.queue_nr == 0);
42 assert(draw->vcache.referenced == 0);
43
44 memset(draw->vcache.idx, ~0, sizeof(draw->vcache.idx));
45 }
46
47
48 /**
49 * Check if vertex is in cache, otherwise add it. It won't go through
50 * VS yet, not until there is a flush operation or the VS queue fills up.
51 *
52 * Note that cache entries are basically just two pointers: the first
53 * an index into the user's vertex arrays, the second a location in
54 * the vertex shader cache for the post-transformed vertex.
55 *
56 * \return pointer to location of (post-transformed) vertex header in the cache
57 */
58 static struct vertex_header *get_vertex( struct draw_context *draw,
59 unsigned i )
60 {
61 unsigned slot = (i + (i>>5)) % VCACHE_SIZE;
62
63 assert(slot < 32); /* so we don't exceed the bitfield size below */
64
65 /* Cache miss?
66 */
67 if (draw->vcache.idx[slot] != i) {
68
69 /* If slot is in use, use the overflow area:
70 */
71 if (draw->vcache.referenced & (1 << slot)) {
72 slot = VCACHE_SIZE + draw->vcache.overflow++;
73 }
74
75 assert(slot < Elements(draw->vcache.idx));
76
77 draw->vcache.idx[slot] = i;
78
79 /* Add to vertex shader queue:
80 */
81 assert(draw->vs.queue_nr < VS_QUEUE_LENGTH);
82 draw->vs.queue[draw->vs.queue_nr].dest = draw->vcache.vertex[slot];
83 draw->vs.queue[draw->vs.queue_nr].elt = i;
84 draw->vs.queue_nr++;
85
86 /* Need to set the vertex's edge flag here. If we're being called
87 * by do_ef_triangle(), that function needs edge flag info!
88 */
89 draw->vcache.vertex[slot]->clipmask = 0;
90 draw->vcache.vertex[slot]->edgeflag = 1; /*XXX use user's edge flag! */
91 draw->vcache.vertex[slot]->pad = 0;
92 draw->vcache.vertex[slot]->vertex_id = UNDEFINED_VERTEX_ID;
93 }
94
95
96 /* primitive flushing may have cleared the bitfield but did not
97 * clear the idx[] array values. Set the bit now. This fixes a
98 * bug found when drawing long triangle fans.
99 */
100 draw->vcache.referenced |= (1 << slot);
101 return draw->vcache.vertex[slot];
102 }
103
104
105 static struct vertex_header *get_uint_elt_vertex( struct draw_context *draw,
106 unsigned i )
107 {
108 const unsigned *elts = (const unsigned *) draw->user.elts;
109 return get_vertex( draw, elts[i] );
110 }
111
112
113 static struct vertex_header *get_ushort_elt_vertex( struct draw_context *draw,
114 unsigned i )
115 {
116 const ushort *elts = (const ushort *) draw->user.elts;
117 return get_vertex( draw, elts[i] );
118 }
119
120
121 static struct vertex_header *get_ubyte_elt_vertex( struct draw_context *draw,
122 unsigned i )
123 {
124 const ubyte *elts = (const ubyte *) draw->user.elts;
125 return get_vertex( draw, elts[i] );
126 }
127
128
129 void draw_vertex_cache_reset_vertex_ids( struct draw_context *draw )
130 {
131 unsigned i;
132
133 for (i = 0; i < Elements(draw->vcache.vertex); i++)
134 draw->vcache.vertex[i]->vertex_id = UNDEFINED_VERTEX_ID;
135 }
136
137
138 void draw_vertex_cache_unreference( struct draw_context *draw )
139 {
140 draw->vcache.referenced = 0;
141 draw->vcache.overflow = 0;
142 }
143
144
145 int draw_vertex_cache_check_space( struct draw_context *draw,
146 unsigned nr_verts )
147 {
148 if (draw->vcache.overflow + nr_verts < VCACHE_OVERFLOW) {
149 /* The vs queue is sized so that this can never happen:
150 */
151 assert(draw->vs.queue_nr + nr_verts < VS_QUEUE_LENGTH);
152 return TRUE;
153 }
154 else
155 return FALSE;
156 }
157
158
159
160 /**
161 * Tell the drawing context about the index/element buffer to use
162 * (ala glDrawElements)
163 * If no element buffer is to be used (i.e. glDrawArrays) then this
164 * should be called with eltSize=0 and elements=NULL.
165 *
166 * \param draw the drawing context
167 * \param eltSize size of each element (1, 2 or 4 bytes)
168 * \param elements the element buffer ptr
169 */
170 void
171 draw_set_mapped_element_buffer( struct draw_context *draw,
172 unsigned eltSize, void *elements )
173 {
174 // draw_statechange( draw );
175
176 /* choose the get_vertex() function to use */
177 switch (eltSize) {
178 case 0:
179 draw->vcache.get_vertex = get_vertex;
180 break;
181 case 1:
182 draw->vcache.get_vertex = get_ubyte_elt_vertex;
183 break;
184 case 2:
185 draw->vcache.get_vertex = get_ushort_elt_vertex;
186 break;
187 case 4:
188 draw->vcache.get_vertex = get_uint_elt_vertex;
189 break;
190 default:
191 assert(0);
192 }
193 draw->user.elts = elements;
194 draw->user.eltSize = eltSize;
195 }
196