1 | /* $Id: vb.c,v 1.2 2000-05-23 20:41:00 jeroen Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Mesa 3-D graphics library
|
---|
5 | * Version: 3.3
|
---|
6 | *
|
---|
7 | * Copyright (C) 1999 Brian Paul All Rights Reserved.
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person obtaining a
|
---|
10 | * copy of this software and associated documentation files (the "Software"),
|
---|
11 | * to deal in the Software without restriction, including without limitation
|
---|
12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
---|
13 | * and/or sell copies of the Software, and to permit persons to whom the
|
---|
14 | * Software is furnished to do so, subject to the following conditions:
|
---|
15 | *
|
---|
16 | * The above copyright notice and this permission notice shall be included
|
---|
17 | * in all copies or substantial portions of the Software.
|
---|
18 | *
|
---|
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
---|
20 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
---|
22 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
---|
23 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
---|
24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 |
|
---|
31 | #ifdef PC_HEADER
|
---|
32 | #include "all.h"
|
---|
33 | #else
|
---|
34 | #include "glheader.h"
|
---|
35 | #include "types.h"
|
---|
36 | #include "vb.h"
|
---|
37 | #include "vbxform.h"
|
---|
38 | #include "xform.h"
|
---|
39 | #include "mem.h"
|
---|
40 | #endif
|
---|
41 |
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * Allocate and initialize a vertex buffer.
|
---|
45 | */
|
---|
46 | struct vertex_buffer *gl_vb_create_for_immediate( GLcontext *ctx )
|
---|
47 | {
|
---|
48 | struct vertex_buffer *VB;
|
---|
49 | struct immediate *IM;
|
---|
50 | GLuint alignment = 32;
|
---|
51 |
|
---|
52 | VB = CALLOC_STRUCT(vertex_buffer);
|
---|
53 | if (!VB)
|
---|
54 | return 0;
|
---|
55 |
|
---|
56 | VB->ctx = ctx;
|
---|
57 | VB->ClipAndMask = CLIP_ALL_BITS;
|
---|
58 | VB->Size = VB_SIZE;
|
---|
59 | VB->FirstFree = VB_MAX;
|
---|
60 | VB->Type = VB_IMMEDIATE;
|
---|
61 | VB->Start = VB_START;
|
---|
62 | VB->pipeline = &ctx->CVA.elt;
|
---|
63 |
|
---|
64 | gl_vector4f_alloc( &VB->Eye, 2, VEC_WRITABLE, VB_SIZE, alignment );
|
---|
65 | gl_vector4f_alloc( &VB->Clip, 2, VEC_WRITABLE, VB_SIZE, alignment );
|
---|
66 | gl_vector4f_alloc( &VB->Win, 2, VEC_WRITABLE, VB_SIZE, alignment );
|
---|
67 | gl_vector4ub_alloc( &VB->BColor, VEC_WRITABLE, VB_SIZE, alignment );
|
---|
68 | gl_vector1ui_alloc( &VB->BIndex, VEC_WRITABLE, VB_SIZE, alignment );
|
---|
69 |
|
---|
70 | VB->ClipMask = (GLubyte *)MALLOC(sizeof(GLubyte) * VB_SIZE);
|
---|
71 | VB->UserClipMask = (GLubyte *)CALLOC(sizeof(GLubyte) * VB_SIZE);
|
---|
72 | VB->CullMask = (GLubyte *)MALLOC(sizeof(GLubyte) * VB_SIZE);
|
---|
73 | VB->NormCullMask = (GLubyte *)MALLOC(sizeof(GLubyte) * VB_SIZE);
|
---|
74 | VB->Spec[0] = (GLubyte (*)[4])MALLOC(sizeof(GLubyte) * 4 * VB_SIZE);
|
---|
75 | VB->Spec[1] = (GLubyte (*)[4])MALLOC(sizeof(GLubyte) * 4 * VB_SIZE);
|
---|
76 |
|
---|
77 | IM = VB->IM = gl_immediate_alloc( ctx );
|
---|
78 |
|
---|
79 | VB->store.Obj = &IM->v.Obj;
|
---|
80 | VB->store.Normal = &IM->v.Normal;
|
---|
81 | VB->store.Color = 0; /* not used */
|
---|
82 | VB->store.Index = 0; /* not used */
|
---|
83 | VB->store.EdgeFlag = &IM->v.EdgeFlag;
|
---|
84 | VB->store.TexCoord[0] = &IM->v.TexCoord[0];
|
---|
85 | VB->store.TexCoord[1] = &IM->v.TexCoord[1];
|
---|
86 | VB->store.Elt = &IM->v.Elt;
|
---|
87 |
|
---|
88 | VB->LitColor[0] = VB->FoggedColor[0] = &IM->v.Color;
|
---|
89 | VB->LitColor[1] = VB->FoggedColor[1] = &VB->BColor;
|
---|
90 |
|
---|
91 | VB->LitIndex[0] = VB->FoggedIndex[0] = &IM->v.Index;
|
---|
92 | VB->LitIndex[1] = VB->FoggedIndex[1] = &VB->BIndex;
|
---|
93 |
|
---|
94 |
|
---|
95 | VB->prev_buffer = IM;
|
---|
96 | /* prev_buffer is now also a reference for the immediate buffer */
|
---|
97 | IM->ref_count++;
|
---|
98 |
|
---|
99 | if (ctx->Driver.RegisterVB)
|
---|
100 | ctx->Driver.RegisterVB( VB );
|
---|
101 |
|
---|
102 | return VB;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 |
|
---|
107 | struct vertex_buffer *gl_vb_create_for_cva( GLcontext *ctx, GLuint size )
|
---|
108 | {
|
---|
109 | struct vertex_buffer *VB;
|
---|
110 | GLuint alignment = 32;
|
---|
111 |
|
---|
112 | VB = CALLOC_STRUCT(vertex_buffer);
|
---|
113 | if (!VB)
|
---|
114 | return 0;
|
---|
115 |
|
---|
116 | /* set non-zero fields */
|
---|
117 |
|
---|
118 | VB->ctx = ctx;
|
---|
119 | VB->Type = VB_CVA_PRECALC;
|
---|
120 | VB->FirstFree = size;
|
---|
121 | VB->CullDone = 1;
|
---|
122 |
|
---|
123 | size += VB_MAX_CLIPPED_VERTS;
|
---|
124 | VB->Size = size;
|
---|
125 | VB->ClipAndMask = CLIP_ALL_BITS;
|
---|
126 | VB->pipeline = &ctx->CVA.pre;
|
---|
127 |
|
---|
128 | VB->ClipMask = (GLubyte *)MALLOC(sizeof(GLubyte) * size);
|
---|
129 | VB->UserClipMask = (GLubyte *)CALLOC(sizeof(GLubyte) * size);
|
---|
130 | VB->Spec[0] = (GLubyte (*)[4])MALLOC(sizeof(GLubyte) * 4 * size);
|
---|
131 | VB->Spec[1] = (GLubyte (*)[4])MALLOC(sizeof(GLubyte) * 4 * size);
|
---|
132 | VB->Flag = (GLuint *)MALLOC(sizeof(GLuint) * size);
|
---|
133 |
|
---|
134 | gl_vector4f_alloc( &VB->Eye, 2, VEC_WRITABLE, size, alignment );
|
---|
135 | gl_vector4f_alloc( &VB->Clip, 2, VEC_WRITABLE, size, alignment );
|
---|
136 | gl_vector4f_alloc( &VB->Win, 2, VEC_WRITABLE, size, alignment );
|
---|
137 |
|
---|
138 | VB->store.Obj = (GLvector4f *) CALLOC(sizeof(GLvector4f));
|
---|
139 | VB->store.Normal = (GLvector3f *) CALLOC(sizeof(GLvector3f));
|
---|
140 | VB->store.Color = 0;
|
---|
141 | VB->store.Index = 0;
|
---|
142 | VB->store.EdgeFlag = (GLvector1ub *) CALLOC(sizeof(GLvector1ub));
|
---|
143 | VB->store.TexCoord[0] = (GLvector4f *) CALLOC(sizeof(GLvector4f));
|
---|
144 | VB->store.TexCoord[1] = (GLvector4f *) CALLOC(sizeof(GLvector4f));
|
---|
145 | VB->store.Elt = (GLvector1ui *) CALLOC(sizeof(GLvector1ui));
|
---|
146 | VB->LitColor[0] = (GLvector4ub *) CALLOC(sizeof(GLvector4ub));
|
---|
147 | VB->LitColor[1] = (GLvector4ub *) CALLOC(sizeof(GLvector4ub));
|
---|
148 | VB->LitIndex[0] = (GLvector1ui *) CALLOC(sizeof(GLvector1ui));
|
---|
149 | VB->LitIndex[1] = (GLvector1ui *) CALLOC(sizeof(GLvector1ui));
|
---|
150 | VB->FoggedColor[0] = (GLvector4ub *) CALLOC(sizeof(GLvector4ub));
|
---|
151 | VB->FoggedColor[1] = (GLvector4ub *) CALLOC(sizeof(GLvector4ub));
|
---|
152 | VB->FoggedIndex[0] = (GLvector1ui *) CALLOC(sizeof(GLvector1ui));
|
---|
153 | VB->FoggedIndex[1] = (GLvector1ui *) CALLOC(sizeof(GLvector1ui));
|
---|
154 |
|
---|
155 | VB->ColorPtr = VB->Color[0] = VB->LitColor[0];
|
---|
156 | VB->IndexPtr = VB->Index[0] = VB->LitIndex[0];
|
---|
157 | VB->Specular = VB->Spec[0];
|
---|
158 | VB->TexCoordPtr[0] = VB->store.TexCoord[0];
|
---|
159 | VB->TexCoordPtr[1] = VB->store.TexCoord[1];
|
---|
160 | VB->EdgeFlagPtr = VB->store.EdgeFlag;
|
---|
161 | VB->NormalPtr = VB->store.Normal;
|
---|
162 | VB->ObjPtr = VB->store.Obj;
|
---|
163 | VB->EltPtr = VB->store.Elt;
|
---|
164 |
|
---|
165 | gl_vector4f_alloc( VB->store.Obj, 2, VEC_WRITABLE, size, alignment );
|
---|
166 | gl_vector3f_alloc( VB->store.Normal, VEC_WRITABLE, size, alignment );
|
---|
167 | gl_vector1ub_alloc( VB->store.EdgeFlag, VEC_WRITABLE, size, alignment );
|
---|
168 | gl_vector4f_alloc( VB->store.TexCoord[0], 2, VEC_WRITABLE, size, alignment );
|
---|
169 | gl_vector4f_alloc( VB->store.TexCoord[1], 2, VEC_WRITABLE, size, alignment );
|
---|
170 |
|
---|
171 | /* TODO: allocate these on demand.
|
---|
172 | */
|
---|
173 | gl_vector4ub_alloc( VB->LitColor[0], VEC_WRITABLE, size, alignment );
|
---|
174 | gl_vector4ub_alloc( VB->LitColor[1], VEC_WRITABLE, size, alignment );
|
---|
175 | gl_vector1ui_alloc( VB->LitIndex[0], VEC_WRITABLE, size, alignment );
|
---|
176 | gl_vector1ui_alloc( VB->LitIndex[1], VEC_WRITABLE, size, alignment );
|
---|
177 | gl_vector4ub_alloc( VB->FoggedColor[0], VEC_WRITABLE, size, alignment );
|
---|
178 | gl_vector4ub_alloc( VB->FoggedColor[1], VEC_WRITABLE, size, alignment );
|
---|
179 | gl_vector1ui_alloc( VB->FoggedIndex[0], VEC_WRITABLE, size, alignment );
|
---|
180 | gl_vector1ui_alloc( VB->FoggedIndex[1], VEC_WRITABLE, size, alignment );
|
---|
181 |
|
---|
182 |
|
---|
183 |
|
---|
184 | VB->prev_buffer = 0;
|
---|
185 | VB->Start = 0;
|
---|
186 |
|
---|
187 | if (ctx->Driver.RegisterVB)
|
---|
188 | ctx->Driver.RegisterVB( VB );
|
---|
189 |
|
---|
190 | return VB;
|
---|
191 | }
|
---|
192 |
|
---|
193 | void gl_vb_free( struct vertex_buffer *VB )
|
---|
194 | {
|
---|
195 | gl_vector4f_free( &VB->Eye );
|
---|
196 | gl_vector4f_free( &VB->Clip );
|
---|
197 | gl_vector4f_free( &VB->Win );
|
---|
198 | gl_vector4ub_free( &VB->BColor );
|
---|
199 | gl_vector1ui_free( &VB->BIndex );
|
---|
200 |
|
---|
201 | if ( VB->prev_buffer && ! --VB->prev_buffer->ref_count )
|
---|
202 | gl_immediate_free( VB->prev_buffer );
|
---|
203 |
|
---|
204 | if (VB->IM) {
|
---|
205 | if ( ! --VB->IM->ref_count )
|
---|
206 | gl_immediate_free( VB->IM );
|
---|
207 |
|
---|
208 | FREE( VB->CullMask );
|
---|
209 | FREE( VB->NormCullMask );
|
---|
210 | } else {
|
---|
211 | if (VB->store.Elt)
|
---|
212 | gl_vector4f_free( VB->store.Obj ); FREE( VB->store.Obj );
|
---|
213 | gl_vector3f_free( VB->store.Normal ); FREE( VB->store.Normal );
|
---|
214 | gl_vector1ub_free( VB->store.EdgeFlag ); FREE( VB->store.EdgeFlag );
|
---|
215 | gl_vector4f_free( VB->store.TexCoord[0] ); FREE( VB->store.TexCoord[0]);
|
---|
216 | gl_vector4f_free( VB->store.TexCoord[1] ); FREE( VB->store.TexCoord[1]);
|
---|
217 |
|
---|
218 | gl_vector4ub_free( VB->LitColor[0] ); FREE( VB->LitColor[0] );
|
---|
219 | gl_vector4ub_free( VB->LitColor[1] ); FREE( VB->LitColor[1] );
|
---|
220 | gl_vector1ui_free( VB->LitIndex[0] ); FREE( VB->LitIndex[0] );
|
---|
221 | gl_vector1ui_free( VB->LitIndex[1] ); FREE( VB->LitIndex[1] );
|
---|
222 |
|
---|
223 | gl_vector4ub_free( VB->FoggedColor[0] ); FREE( VB->FoggedColor[0] );
|
---|
224 | gl_vector4ub_free( VB->FoggedColor[1] ); FREE( VB->FoggedColor[1] );
|
---|
225 | gl_vector1ui_free( VB->FoggedIndex[0] ); FREE( VB->FoggedIndex[0] );
|
---|
226 | gl_vector1ui_free( VB->FoggedIndex[1] ); FREE( VB->FoggedIndex[1] );
|
---|
227 |
|
---|
228 | FREE( VB->Flag );
|
---|
229 | }
|
---|
230 |
|
---|
231 | if (VB->tmp_f) FREE(VB->tmp_f);
|
---|
232 | if (VB->tmp_m) FREE(VB->tmp_m);
|
---|
233 | if (VB->EvaluatedFlags) FREE(VB->EvaluatedFlags);
|
---|
234 |
|
---|
235 | FREE( VB->Spec[0] );
|
---|
236 | FREE( VB->Spec[1] );
|
---|
237 | FREE( VB->ClipMask );
|
---|
238 | FREE( VB->UserClipMask );
|
---|
239 |
|
---|
240 | if (VB->ctx->Driver.UnregisterVB)
|
---|
241 | VB->ctx->Driver.UnregisterVB( VB );
|
---|
242 |
|
---|
243 | FREE( VB );
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | struct immediate *gl_immediate_alloc( GLcontext *ctx )
|
---|
248 | {
|
---|
249 | static int id = 0;
|
---|
250 | struct immediate *IM;
|
---|
251 | GLuint j;
|
---|
252 |
|
---|
253 | if (ctx->freed_im_queue) {
|
---|
254 | IM = ctx->freed_im_queue;
|
---|
255 | ctx->freed_im_queue = IM->next;
|
---|
256 | ctx->nr_im_queued--;
|
---|
257 | IM->ref_count = 1;
|
---|
258 | return IM;
|
---|
259 | }
|
---|
260 |
|
---|
261 | IM= (struct immediate *) MALLOC(sizeof(*IM));
|
---|
262 | if (!IM) return 0;
|
---|
263 |
|
---|
264 | IM->id = id++;
|
---|
265 | IM->ref_count = 1;
|
---|
266 | IM->backref = ctx;
|
---|
267 | IM->maybe_transform_vb = gl_maybe_transform_vb;
|
---|
268 | /* IM->Bounds = 0; */
|
---|
269 | IM->NormalLengths = 0;
|
---|
270 | IM->LastCalcedLength = 0;
|
---|
271 | IM->FlushElt = 0;
|
---|
272 | IM->LastPrimitive = VB_START;
|
---|
273 | IM->Count = VB_MAX; /* force clear of Flag. */
|
---|
274 | IM->Start = VB_START;
|
---|
275 | IM->Material = 0;
|
---|
276 | IM->MaterialMask = 0;
|
---|
277 |
|
---|
278 | if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
|
---|
279 | fprintf(stderr, "alloc immediate %d\n", id);
|
---|
280 |
|
---|
281 | gl_vector4f_init( &IM->v.Obj, VEC_WRITABLE, IM->Obj );
|
---|
282 | gl_vector3f_init( &IM->v.Normal, VEC_WRITABLE, IM->Normal );
|
---|
283 | gl_vector4ub_init( &IM->v.Color, VEC_WRITABLE, IM->Color );
|
---|
284 | gl_vector1ui_init( &IM->v.Index, VEC_WRITABLE, IM->Index );
|
---|
285 | gl_vector1ub_init( &IM->v.EdgeFlag, VEC_WRITABLE, IM->EdgeFlag );
|
---|
286 | gl_vector1ui_init( &IM->v.Elt, VEC_WRITABLE, IM->Elt );
|
---|
287 |
|
---|
288 | for (j=0;j<MAX_TEXTURE_UNITS;j++) {
|
---|
289 | IM->TexCoordPtr[j] = IM->TexCoord[j];
|
---|
290 | gl_vector4f_init( &IM->v.TexCoord[j], VEC_WRITABLE, IM->TexCoord[j]);
|
---|
291 |
|
---|
292 | /* Precalculate some flags and keep them in a handy place.
|
---|
293 | */
|
---|
294 | IM->TF1[j] = VERT_TEX0_1 << (j*NR_TEXSIZE_BITS);
|
---|
295 | IM->TF2[j] = VERT_TEX0_12 << (j*NR_TEXSIZE_BITS);
|
---|
296 | IM->TF3[j] = VERT_TEX0_123 << (j*NR_TEXSIZE_BITS);
|
---|
297 | IM->TF4[j] = VERT_TEX0_1234 << (j*NR_TEXSIZE_BITS);
|
---|
298 | }
|
---|
299 |
|
---|
300 | return IM;
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | void gl_immediate_free( struct immediate *IM )
|
---|
305 | {
|
---|
306 | GLcontext *ctx = IM->backref;
|
---|
307 |
|
---|
308 | if (IM->NormalLengths) {
|
---|
309 | FREE( IM->NormalLengths );
|
---|
310 | IM->NormalLengths = 0;
|
---|
311 | IM->LastCalcedLength = 0;
|
---|
312 | }
|
---|
313 |
|
---|
314 | if (IM->Material) {
|
---|
315 | FREE( IM->Material );
|
---|
316 | FREE( IM->MaterialMask );
|
---|
317 | IM->Material = 0;
|
---|
318 | IM->MaterialMask = 0;
|
---|
319 | }
|
---|
320 |
|
---|
321 | if (ctx->nr_im_queued > 5) {
|
---|
322 | if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
|
---|
323 | fprintf(stderr, "really free immediate %d\n", IM->id);
|
---|
324 |
|
---|
325 | FREE( IM );
|
---|
326 | }
|
---|
327 | else {
|
---|
328 | if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
|
---|
329 | fprintf(stderr, "requeue immediate %d\n", IM->id);
|
---|
330 |
|
---|
331 | IM->next = ctx->freed_im_queue;
|
---|
332 | ctx->freed_im_queue = IM;
|
---|
333 | ctx->nr_im_queued++;
|
---|
334 | }
|
---|
335 | }
|
---|