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 */
67 if (draw
->vcache
.idx
[slot
] != i
) {
69 /* If slot is in use, use the overflow area:
71 if (draw
->vcache
.referenced
& (1 << slot
)) {
72 slot
= VCACHE_SIZE
+ draw
->vcache
.overflow
++;
75 assert(slot
< Elements(draw
->vcache
.idx
));
77 draw
->vcache
.idx
[slot
] = i
;
79 /* Add to vertex shader queue:
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
;
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!
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
;
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.
100 draw
->vcache
.referenced
|= (1 << slot
);
101 return draw
->vcache
.vertex
[slot
];
105 static struct vertex_header
*get_uint_elt_vertex( struct draw_context
*draw
,
108 const unsigned *elts
= (const unsigned *) draw
->user
.elts
;
109 return get_vertex( draw
, elts
[i
] );
113 static struct vertex_header
*get_ushort_elt_vertex( struct draw_context
*draw
,
116 const ushort
*elts
= (const ushort
*) draw
->user
.elts
;
117 return get_vertex( draw
, elts
[i
] );
121 static struct vertex_header
*get_ubyte_elt_vertex( struct draw_context
*draw
,
124 const ubyte
*elts
= (const ubyte
*) draw
->user
.elts
;
125 return get_vertex( draw
, elts
[i
] );
129 void draw_vertex_cache_reset_vertex_ids( struct draw_context
*draw
)
133 for (i
= 0; i
< Elements(draw
->vcache
.vertex
); i
++)
134 draw
->vcache
.vertex
[i
]->vertex_id
= UNDEFINED_VERTEX_ID
;
138 void draw_vertex_cache_unreference( struct draw_context
*draw
)
140 draw
->vcache
.referenced
= 0;
141 draw
->vcache
.overflow
= 0;
145 int draw_vertex_cache_check_space( struct draw_context
*draw
,
148 if (draw
->vcache
.overflow
+ nr_verts
< VCACHE_OVERFLOW
) {
149 /* The vs queue is sized so that this can never happen:
151 assert(draw
->vs
.queue_nr
+ nr_verts
< VS_QUEUE_LENGTH
);
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.
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
171 draw_set_mapped_element_buffer( struct draw_context
*draw
,
172 unsigned eltSize
, void *elements
)
174 // draw_statechange( draw );
176 /* choose the get_vertex() function to use */
179 draw
->vcache
.get_vertex
= get_vertex
;
182 draw
->vcache
.get_vertex
= get_ubyte_elt_vertex
;
185 draw
->vcache
.get_vertex
= get_ushort_elt_vertex
;
188 draw
->vcache
.get_vertex
= get_uint_elt_vertex
;
193 draw
->user
.elts
= elements
;
194 draw
->user
.eltSize
= eltSize
;