1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "pipe/p_util.h"
34 #include "draw_private.h"
35 #include "draw_context.h"
38 void draw_vertex_cache_invalidate( struct draw_context
*draw
)
40 assert(draw
->pq
.queue_nr
== 0);
41 assert(draw
->vs
.queue_nr
== 0);
42 assert(draw
->vcache
.referenced
== 0);
44 // memset(draw->vcache.idx, ~0, sizeof(draw->vcache.idx));
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.
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.
56 * \return pointer to location of (post-transformed) vertex header in the cache
58 static struct vertex_header
*get_vertex( struct draw_context
*draw
,
61 unsigned slot
= (i
+ (i
>>5)) % VCACHE_SIZE
;
63 assert(slot
< 32); /* so we don't exceed the bitfield size below */
65 if (draw
->vcache
.referenced
& (1<<slot
))
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
;
75 /* Otherwise a collision
77 slot
= VCACHE_SIZE
+ draw
->vcache
.overflow
++;
78 // _mesa_printf("XXX %d --> %d\n", i, slot);
81 /* Deal with the cache miss:
86 assert(slot
< Elements(draw
->vcache
.idx
));
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
);
94 /* Add to vertex shader queue:
96 assert(draw
->vs
.queue_nr
< VS_QUEUE_LENGTH
);
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
;
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!
108 return draw
->vs
.queue
[draw
->vcache
.idx
[slot
].out
].vertex
;
113 static struct vertex_header
*get_uint_elt_vertex( struct draw_context
*draw
,
116 const unsigned *elts
= (const unsigned *) draw
->user
.elts
;
117 return get_vertex( draw
, elts
[i
] );
121 static struct vertex_header
*get_ushort_elt_vertex( struct draw_context
*draw
,
124 const ushort
*elts
= (const ushort
*) draw
->user
.elts
;
125 return get_vertex( draw
, elts
[i
] );
129 static struct vertex_header
*get_ubyte_elt_vertex( struct draw_context
*draw
,
132 const ubyte
*elts
= (const ubyte
*) draw
->user
.elts
;
133 return get_vertex( draw
, elts
[i
] );
137 void draw_vertex_cache_reset_vertex_ids( struct draw_context
*draw
)
141 for (i
= 0; i
< draw
->vs
.post_nr
; i
++)
142 draw
->vs
.queue
[i
].vertex
->vertex_id
= UNDEFINED_VERTEX_ID
;
146 void draw_vertex_cache_unreference( struct draw_context
*draw
)
148 draw
->vcache
.referenced
= 0;
149 draw
->vcache
.overflow
= 0;
153 int draw_vertex_cache_check_space( struct draw_context
*draw
,
156 if (draw
->vcache
.overflow
+ nr_verts
< VCACHE_OVERFLOW
) {
157 /* The vs queue is sized so that this can never happen:
159 assert(draw
->vs
.queue_nr
+ nr_verts
< VS_QUEUE_LENGTH
);
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.
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
179 draw_set_mapped_element_buffer( struct draw_context
*draw
,
180 unsigned eltSize
, void *elements
)
182 // draw_statechange( draw );
184 /* choose the get_vertex() function to use */
187 draw
->vcache
.get_vertex
= get_vertex
;
190 draw
->vcache
.get_vertex
= get_ubyte_elt_vertex
;
193 draw
->vcache
.get_vertex
= get_ushort_elt_vertex
;
196 draw
->vcache
.get_vertex
= get_uint_elt_vertex
;
201 draw
->user
.elts
= elements
;
202 draw
->user
.eltSize
= eltSize
;