1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/**************************************************************************
*
* Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
/*
* Authors:
* Keith Whitwell <keith@tungstengraphics.com>
*/
#include "pipe/p_util.h"
#include "draw_private.h"
#include "draw_context.h"
void draw_vertex_cache_invalidate( struct draw_context *draw )
{
assert(draw->pq.queue_nr == 0);
assert(draw->vs.queue_nr == 0);
assert(draw->vcache.referenced == 0);
/* There's an error somewhere in the vcache code that requires this
* memset. The bug is exposed in q3demo demo001, but probably
* elsewhere as well. Will track it down later.
*/
memset(draw->vcache.idx, ~0, sizeof(draw->vcache.idx));
}
/**
* Check if vertex is in cache, otherwise add it. It won't go through
* VS yet, not until there is a flush operation or the VS queue fills up.
*
* Note that cache entries are basically just two pointers: the first
* an index into the user's vertex arrays, the second a location in
* the vertex shader cache for the post-transformed vertex.
*
* \return pointer to location of (post-transformed) vertex header in the cache
*/
static struct vertex_header *get_vertex( struct draw_context *draw,
unsigned i )
{
unsigned slot = (i + (i>>5)) % VCACHE_SIZE;
assert(slot < 32); /* so we don't exceed the bitfield size below */
if (draw->vcache.referenced & (1<<slot))
{
/* Cache hit?
*/
if (draw->vcache.idx[slot].in == i) {
// _mesa_printf("HIT %d %d\n", slot, i);
assert(draw->vcache.idx[slot].out < draw->vs.queue_nr);
return draw->vs.queue[draw->vcache.idx[slot].out].vertex;
}
/* Otherwise a collision
*/
slot = VCACHE_SIZE + draw->vcache.overflow++;
// _mesa_printf("XXX %d --> %d\n", i, slot);
}
/* Deal with the cache miss:
*/
{
unsigned out;
assert(slot < Elements(draw->vcache.idx));
// _mesa_printf("NEW %d %d\n", slot, i);
draw->vcache.idx[slot].in = i;
draw->vcache.idx[slot].out = out = draw->vs.queue_nr++;
draw->vcache.referenced |= (1 << slot);
/* Add to vertex shader queue:
*/
assert(draw->vs.queue_nr < VS_QUEUE_LENGTH);
draw->vs.queue[out].elt = i;
draw->vs.queue[out].vertex->clipmask = 0;
draw->vs.queue[out].vertex->edgeflag = 1; /*XXX use user's edge flag! */
draw->vs.queue[out].vertex->pad = 0;
draw->vs.queue[out].vertex->vertex_id = UNDEFINED_VERTEX_ID;
/* Need to set the vertex's edge flag here. If we're being called
* by do_ef_triangle(), that function needs edge flag info!
*/
return draw->vs.queue[draw->vcache.idx[slot].out].vertex;
}
}
static struct vertex_header *get_uint_elt_vertex( struct draw_context *draw,
unsigned i )
{
const unsigned *elts = (const unsigned *) draw->user.elts;
return get_vertex( draw, elts[i] );
}
static struct vertex_header *get_ushort_elt_vertex( struct draw_context *draw,
unsigned i )
{
const ushort *elts = (const ushort *) draw->user.elts;
return get_vertex( draw, elts[i] );
}
static struct vertex_header *get_ubyte_elt_vertex( struct draw_context *draw,
unsigned i )
{
const ubyte *elts = (const ubyte *) draw->user.elts;
return get_vertex( draw, elts[i] );
}
void draw_vertex_cache_reset_vertex_ids( struct draw_context *draw )
{
unsigned i;
for (i = 0; i < draw->vs.post_nr; i++)
draw->vs.queue[i].vertex->vertex_id = UNDEFINED_VERTEX_ID;
}
void draw_vertex_cache_unreference( struct draw_context *draw )
{
draw->vcache.referenced = 0;
draw->vcache.overflow = 0;
}
int draw_vertex_cache_check_space( struct draw_context *draw,
unsigned nr_verts )
{
if (draw->vcache.overflow + nr_verts < VCACHE_OVERFLOW) {
/* The vs queue is sized so that this can never happen:
*/
assert(draw->vs.queue_nr + nr_verts < VS_QUEUE_LENGTH);
return TRUE;
}
else
return FALSE;
}
/**
* Tell the drawing context about the index/element buffer to use
* (ala glDrawElements)
* If no element buffer is to be used (i.e. glDrawArrays) then this
* should be called with eltSize=0 and elements=NULL.
*
* \param draw the drawing context
* \param eltSize size of each element (1, 2 or 4 bytes)
* \param elements the element buffer ptr
*/
void
draw_set_mapped_element_buffer( struct draw_context *draw,
unsigned eltSize, void *elements )
{
// draw_statechange( draw );
/* choose the get_vertex() function to use */
switch (eltSize) {
case 0:
draw->vcache.get_vertex = get_vertex;
break;
case 1:
draw->vcache.get_vertex = get_ubyte_elt_vertex;
break;
case 2:
draw->vcache.get_vertex = get_ushort_elt_vertex;
break;
case 4:
draw->vcache.get_vertex = get_uint_elt_vertex;
break;
default:
assert(0);
}
draw->user.elts = elements;
draw->user.eltSize = eltSize;
}
|