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