gallium: modify draw_find_vs_output() to search vertex shader outputs
[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 if (draw->vcache.referenced & (1<<slot))
66 {
67 /* Cache hit?
68 */
69 if (draw->vcache.idx[slot].in == i) {
70 // _mesa_printf("HIT %d %d\n", slot, i);
71 assert(draw->vcache.idx[slot].out < draw->vs.queue_nr);
72 return draw->vs.queue[draw->vcache.idx[slot].out].vertex;
73 }
74
75 /* Otherwise a collision
76 */
77 slot = VCACHE_SIZE + draw->vcache.overflow++;
78 // _mesa_printf("XXX %d --> %d\n", i, slot);
79 }
80
81 /* Deal with the cache miss:
82 */
83 {
84 unsigned out;
85
86 assert(slot < Elements(draw->vcache.idx));
87
88 // _mesa_printf("NEW %d %d\n", slot, i);
89 draw->vcache.idx[slot].in = i;
90 draw->vcache.idx[slot].out = out = draw->vs.queue_nr++;
91 draw->vcache.referenced |= (1 << slot);
92
93
94 /* Add to vertex shader queue:
95 */
96 assert(draw->vs.queue_nr < VS_QUEUE_LENGTH);
97
98 draw->vs.queue[out].elt = i;
99 draw->vs.queue[out].vertex->clipmask = 0;
100 draw->vs.queue[out].vertex->edgeflag = 1; /*XXX use user's edge flag! */
101 draw->vs.queue[out].vertex->pad = 0;
102 draw->vs.queue[out].vertex->vertex_id = UNDEFINED_VERTEX_ID;
103
104 /* Need to set the vertex's edge flag here. If we're being called
105 * by do_ef_triangle(), that function needs edge flag info!
106 */
107
108 return draw->vs.queue[draw->vcache.idx[slot].out].vertex;
109 }
110 }
111
112
113 static struct vertex_header *get_uint_elt_vertex( struct draw_context *draw,
114 unsigned i )
115 {
116 const unsigned *elts = (const unsigned *) draw->user.elts;
117 return get_vertex( draw, elts[i] );
118 }
119
120
121 static struct vertex_header *get_ushort_elt_vertex( struct draw_context *draw,
122 unsigned i )
123 {
124 const ushort *elts = (const ushort *) draw->user.elts;
125 return get_vertex( draw, elts[i] );
126 }
127
128
129 static struct vertex_header *get_ubyte_elt_vertex( struct draw_context *draw,
130 unsigned i )
131 {
132 const ubyte *elts = (const ubyte *) draw->user.elts;
133 return get_vertex( draw, elts[i] );
134 }
135
136
137 void draw_vertex_cache_reset_vertex_ids( struct draw_context *draw )
138 {
139 unsigned i;
140
141 for (i = 0; i < draw->vs.post_nr; i++)
142 draw->vs.queue[i].vertex->vertex_id = UNDEFINED_VERTEX_ID;
143 }
144
145
146 void draw_vertex_cache_unreference( struct draw_context *draw )
147 {
148 draw->vcache.referenced = 0;
149 draw->vcache.overflow = 0;
150 }
151
152
153 int draw_vertex_cache_check_space( struct draw_context *draw,
154 unsigned nr_verts )
155 {
156 if (draw->vcache.overflow + nr_verts < VCACHE_OVERFLOW) {
157 /* The vs queue is sized so that this can never happen:
158 */
159 assert(draw->vs.queue_nr + nr_verts < VS_QUEUE_LENGTH);
160 return TRUE;
161 }
162 else
163 return FALSE;
164 }
165
166
167
168 /**
169 * Tell the drawing context about the index/element buffer to use
170 * (ala glDrawElements)
171 * If no element buffer is to be used (i.e. glDrawArrays) then this
172 * should be called with eltSize=0 and elements=NULL.
173 *
174 * \param draw the drawing context
175 * \param eltSize size of each element (1, 2 or 4 bytes)
176 * \param elements the element buffer ptr
177 */
178 void
179 draw_set_mapped_element_buffer( struct draw_context *draw,
180 unsigned eltSize, void *elements )
181 {
182 // draw_statechange( draw );
183
184 /* choose the get_vertex() function to use */
185 switch (eltSize) {
186 case 0:
187 draw->vcache.get_vertex = get_vertex;
188 break;
189 case 1:
190 draw->vcache.get_vertex = get_ubyte_elt_vertex;
191 break;
192 case 2:
193 draw->vcache.get_vertex = get_ushort_elt_vertex;
194 break;
195 case 4:
196 draw->vcache.get_vertex = get_uint_elt_vertex;
197 break;
198 default:
199 assert(0);
200 }
201 draw->user.elts = elements;
202 draw->user.eltSize = eltSize;
203 }
204