28d4b225feb7318c01b13a1be73e89e2b3754133
[mesa.git] / src / mesa / array_cache / ac_context.c
1 /* $Id: ac_context.c,v 1.11 2003/03/01 01:50:24 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 5.1
6 *
7 * Copyright (C) 1999-2002 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 * Authors:
27 * Keith Whitwell <keith@tungstengraphics.com>
28 */
29
30 #include "glheader.h"
31 #include "macros.h"
32 #include "imports.h"
33 #include "mtypes.h"
34
35 #include "array_cache/ac_context.h"
36
37
38 /*
39 * Initialize the array fallbacks. That is, by default the fallback arrays
40 * point into the current vertex attribute values in ctx->Current.Attrib[]
41 */
42 static void _ac_fallbacks_init( GLcontext *ctx )
43 {
44 ACcontext *ac = AC_CONTEXT(ctx);
45 struct gl_client_array *cl;
46 GLuint i;
47
48 cl = &ac->Fallback.Normal;
49 cl->Size = 3;
50 cl->Type = GL_FLOAT;
51 cl->Stride = 0;
52 cl->StrideB = 0;
53 cl->Ptr = (void *) ctx->Current.Attrib[VERT_ATTRIB_NORMAL];
54 cl->Enabled = 1;
55 cl->Flags = CA_CLIENT_DATA; /* hack */
56
57 cl = &ac->Fallback.Color;
58 cl->Size = 4;
59 cl->Type = GL_FLOAT;
60 cl->Stride = 0;
61 cl->StrideB = 0;
62 cl->Ptr = (void *) ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
63 cl->Enabled = 1;
64 cl->Flags = CA_CLIENT_DATA; /* hack */
65
66 cl = &ac->Fallback.SecondaryColor;
67 cl->Size = 3;
68 cl->Type = GL_FLOAT;
69 cl->Stride = 0;
70 cl->StrideB = 0;
71 cl->Ptr = (void *) ctx->Current.Attrib[VERT_ATTRIB_COLOR1];
72 cl->Enabled = 1;
73 cl->Flags = CA_CLIENT_DATA; /* hack */
74
75 cl = &ac->Fallback.FogCoord;
76 cl->Size = 1;
77 cl->Type = GL_FLOAT;
78 cl->Stride = 0;
79 cl->StrideB = 0;
80 cl->Ptr = (void *) &ctx->Current.Attrib[VERT_ATTRIB_FOG];
81 cl->Enabled = 1;
82 cl->Flags = CA_CLIENT_DATA; /* hack */
83
84 cl = &ac->Fallback.Index;
85 cl->Size = 1;
86 cl->Type = GL_UNSIGNED_INT;
87 cl->Stride = 0;
88 cl->StrideB = 0;
89 cl->Ptr = (void *) &ctx->Current.Index;
90 cl->Enabled = 1;
91 cl->Flags = CA_CLIENT_DATA; /* hack */
92
93 for (i = 0 ; i < MAX_TEXTURE_COORD_UNITS ; i++) {
94 cl = &ac->Fallback.TexCoord[i];
95 cl->Size = 4;
96 cl->Type = GL_FLOAT;
97 cl->Stride = 0;
98 cl->StrideB = 0;
99 cl->Ptr = (void *) ctx->Current.Attrib[VERT_ATTRIB_TEX0 + i];
100 cl->Enabled = 1;
101 cl->Flags = CA_CLIENT_DATA; /* hack */
102 }
103
104 cl = &ac->Fallback.EdgeFlag;
105 cl->Size = 1;
106 cl->Type = GL_UNSIGNED_BYTE;
107 cl->Stride = 0;
108 cl->StrideB = 0;
109 cl->Ptr = (void *) &ctx->Current.EdgeFlag;
110 cl->Enabled = 1;
111 cl->Flags = CA_CLIENT_DATA; /* hack */
112
113 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
114 cl = &ac->Fallback.Attrib[i];
115 cl->Size = 4;
116 cl->Type = GL_FLOAT;
117 cl->Stride = 0;
118 cl->StrideB = 0;
119 cl->Ptr = (void *) ctx->Current.Attrib[i];
120 cl->Enabled = 1;
121 cl->Flags = CA_CLIENT_DATA; /* hack */
122 }
123 }
124
125
126 /*
127 * Initialize the array cache pointers, types, strides, etc.
128 */
129 static void _ac_cache_init( GLcontext *ctx )
130 {
131 ACcontext *ac = AC_CONTEXT(ctx);
132 struct gl_client_array *cl;
133 GLuint size = ctx->Const.MaxArrayLockSize + MAX_CLIPPED_VERTICES;
134 GLuint i;
135
136 cl = &ac->Cache.Vertex;
137 cl->Size = 4;
138 cl->Type = GL_FLOAT;
139 cl->Stride = 0;
140 cl->StrideB = 4 * sizeof(GLfloat);
141 cl->Ptr = MALLOC( cl->StrideB * size );
142 cl->Enabled = 1;
143 cl->Flags = 0;
144
145 cl = &ac->Cache.Normal;
146 cl->Size = 3;
147 cl->Type = GL_FLOAT;
148 cl->Stride = 0;
149 cl->StrideB = 3 * sizeof(GLfloat);
150 cl->Ptr = MALLOC( cl->StrideB * size );
151 cl->Enabled = 1;
152 cl->Flags = 0;
153
154 cl = &ac->Cache.Color;
155 cl->Size = 4;
156 cl->Type = GL_FLOAT;
157 cl->Stride = 0;
158 cl->StrideB = 4 * sizeof(GLfloat);
159 cl->Ptr = MALLOC( cl->StrideB * size );
160 cl->Enabled = 1;
161 cl->Flags = 0;
162
163 cl = &ac->Cache.SecondaryColor;
164 cl->Size = 3;
165 cl->Type = GL_FLOAT;
166 cl->Stride = 0;
167 cl->StrideB = 4 * sizeof(GLfloat);
168 cl->Ptr = MALLOC( cl->StrideB * size );
169 cl->Enabled = 1;
170 cl->Flags = 0;
171
172 cl = &ac->Cache.FogCoord;
173 cl->Size = 1;
174 cl->Type = GL_FLOAT;
175 cl->Stride = 0;
176 cl->StrideB = sizeof(GLfloat);
177 cl->Ptr = MALLOC( cl->StrideB * size );
178 cl->Enabled = 1;
179 cl->Flags = 0;
180
181 cl = &ac->Cache.Index;
182 cl->Size = 1;
183 cl->Type = GL_UNSIGNED_INT;
184 cl->Stride = 0;
185 cl->StrideB = sizeof(GLuint);
186 cl->Ptr = MALLOC( cl->StrideB * size );
187 cl->Enabled = 1;
188 cl->Flags = 0;
189
190 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
191 cl = &ac->Cache.TexCoord[i];
192 cl->Size = 4;
193 cl->Type = GL_FLOAT;
194 cl->Stride = 0;
195 cl->StrideB = 4 * sizeof(GLfloat);
196 cl->Ptr = MALLOC( cl->StrideB * size );
197 cl->Enabled = 1;
198 cl->Flags = 0;
199 }
200
201 cl = &ac->Cache.EdgeFlag;
202 cl->Size = 1;
203 cl->Type = GL_UNSIGNED_BYTE;
204 cl->Stride = 0;
205 cl->StrideB = sizeof(GLubyte);
206 cl->Ptr = MALLOC( cl->StrideB * size );
207 cl->Enabled = 1;
208 cl->Flags = 0;
209
210 for (i = 0 ; i < VERT_ATTRIB_MAX; i++) {
211 cl = &ac->Cache.Attrib[i];
212 cl->Size = 4;
213 cl->Type = GL_FLOAT;
214 cl->Stride = 0;
215 cl->StrideB = 4 * sizeof(GLfloat);
216 cl->Ptr = MALLOC( cl->StrideB * size );
217 cl->Enabled = 1;
218 cl->Flags = 0;
219 }
220 }
221
222
223 /* This storage used to hold translated client data if type or stride
224 * need to be fixed.
225 */
226 static void _ac_elts_init( GLcontext *ctx )
227 {
228 ACcontext *ac = AC_CONTEXT(ctx);
229 GLuint size = 1000;
230
231 ac->Elts = (GLuint *)MALLOC( sizeof(GLuint) * size );
232 ac->elt_size = size;
233 }
234
235 static void _ac_raw_init( GLcontext *ctx )
236 {
237 ACcontext *ac = AC_CONTEXT(ctx);
238 GLuint i;
239
240 ac->Raw.Color = ac->Fallback.Color;
241 ac->Raw.EdgeFlag = ac->Fallback.EdgeFlag;
242 ac->Raw.FogCoord = ac->Fallback.FogCoord;
243 ac->Raw.Index = ac->Fallback.Index;
244 ac->Raw.Normal = ac->Fallback.Normal;
245 ac->Raw.SecondaryColor = ac->Fallback.SecondaryColor;
246 ac->Raw.Vertex = ctx->Array.Vertex;
247
248 ac->IsCached.Color = GL_FALSE;
249 ac->IsCached.EdgeFlag = GL_FALSE;
250 ac->IsCached.FogCoord = GL_FALSE;
251 ac->IsCached.Index = GL_FALSE;
252 ac->IsCached.Normal = GL_FALSE;
253 ac->IsCached.SecondaryColor = GL_FALSE;
254 ac->IsCached.Vertex = GL_FALSE;
255
256 for (i = 0 ; i < MAX_TEXTURE_COORD_UNITS ; i++) {
257 ac->Raw.TexCoord[i] = ac->Fallback.TexCoord[i];
258 ac->IsCached.TexCoord[i] = GL_FALSE;
259 }
260
261 for (i = 0 ; i < VERT_ATTRIB_MAX ; i++) {
262 ac->Raw.Attrib[i] = ac->Fallback.Attrib[i];
263 ac->IsCached.Attrib[i] = GL_FALSE;
264 }
265 }
266
267 GLboolean _ac_CreateContext( GLcontext *ctx )
268 {
269 ctx->acache_context = CALLOC(sizeof(ACcontext));
270 if (ctx->acache_context) {
271 _ac_cache_init( ctx );
272 _ac_fallbacks_init( ctx );
273 _ac_raw_init( ctx );
274 _ac_elts_init( ctx );
275 return GL_TRUE;
276 }
277 return GL_FALSE;
278 }
279
280 void _ac_DestroyContext( GLcontext *ctx )
281 {
282 ACcontext *ac = AC_CONTEXT(ctx);
283 GLint i;
284
285 if (ac->Cache.Vertex.Ptr) FREE( ac->Cache.Vertex.Ptr );
286 if (ac->Cache.Normal.Ptr) FREE( ac->Cache.Normal.Ptr );
287 if (ac->Cache.Color.Ptr) FREE( ac->Cache.Color.Ptr );
288 if (ac->Cache.SecondaryColor.Ptr) FREE( ac->Cache.SecondaryColor.Ptr );
289 if (ac->Cache.EdgeFlag.Ptr) FREE( ac->Cache.EdgeFlag.Ptr );
290 if (ac->Cache.Index.Ptr) FREE( ac->Cache.Index.Ptr );
291 if (ac->Cache.FogCoord.Ptr) FREE( ac->Cache.FogCoord.Ptr );
292
293 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
294 if (ac->Cache.TexCoord[i].Ptr)
295 FREE( ac->Cache.TexCoord[i].Ptr );
296 }
297
298 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
299 if (ac->Cache.Attrib[i].Ptr)
300 FREE( ac->Cache.Attrib[i].Ptr );
301 }
302
303 if (ac->Elts) FREE( ac->Elts );
304
305 /* Free the context structure itself */
306 FREE(ac);
307 ctx->acache_context = NULL;
308 }
309
310 void _ac_InvalidateState( GLcontext *ctx, GLuint new_state )
311 {
312 AC_CONTEXT(ctx)->NewState |= new_state;
313 AC_CONTEXT(ctx)->NewArrayState |= ctx->Array.NewState;
314 }