rework to correctly respect _ac_import_range()
[mesa.git] / src / mesa / array_cache / ac_context.c
1 /* $Id: ac_context.c,v 1.2 2001/02/04 00:47:28 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
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 * Author:
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.Normal;
50 cl->Enabled = 1;
51
52 cl = &ac->Fallback.Color;
53 cl->Size = 4;
54 cl->Type = GL_UNSIGNED_BYTE;
55 cl->Stride = 0;
56 cl->StrideB = 0;
57 cl->Ptr = (void *) ctx->Current.Color;
58 cl->Enabled = 1;
59
60 cl = &ac->Fallback.SecondaryColor;
61 cl->Size = 3;
62 cl->Type = GL_UNSIGNED_BYTE;
63 cl->Stride = 0;
64 cl->StrideB = 0;
65 cl->Ptr = (void *) ctx->Current.SecondaryColor;
66 cl->Enabled = 1;
67
68 cl = &ac->Fallback.FogCoord;
69 cl->Size = 1;
70 cl->Type = GL_FLOAT;
71 cl->Stride = 0;
72 cl->StrideB = 0;
73 cl->Ptr = (void *) &ctx->Current.FogCoord;
74 cl->Enabled = 1;
75
76 cl = &ac->Fallback.Index;
77 cl->Size = 1;
78 cl->Type = GL_UNSIGNED_INT;
79 cl->Stride = 0;
80 cl->StrideB = 0;
81 cl->Ptr = (void *) &ctx->Current.Index;
82 cl->Enabled = 1;
83
84 for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++) {
85 cl = &ac->Fallback.TexCoord[i];
86 cl->Size = 4;
87 cl->Type = GL_FLOAT;
88 cl->Stride = 0;
89 cl->StrideB = 0;
90 cl->Ptr = (void *) ctx->Current.Texcoord[i];
91 cl->Enabled = 1;
92 }
93
94 cl = &ac->Fallback.EdgeFlag;
95 cl->Size = 1;
96 cl->Type = GL_UNSIGNED_BYTE;
97 cl->Stride = 0;
98 cl->StrideB = 0;
99 cl->Ptr = (void *) &ctx->Current.EdgeFlag;
100 cl->Enabled = 1;
101 }
102
103
104 static void _ac_cache_init( GLcontext *ctx )
105 {
106 ACcontext *ac = AC_CONTEXT(ctx);
107 struct gl_client_array *cl;
108 GLuint size = ctx->Const.MaxArrayLockSize + MAX_CLIPPED_VERTICES;
109 GLuint i;
110
111 cl = &ac->Cache.Vertex;
112 cl->Size = 4;
113 cl->Type = GL_FLOAT;
114 cl->Stride = 0;
115 cl->StrideB = 4 * sizeof(GLfloat);
116 cl->Ptr = MALLOC( cl->StrideB * size );
117 cl->Enabled = 1;
118
119 cl = &ac->Cache.Normal;
120 cl->Size = 3;
121 cl->Type = GL_FLOAT;
122 cl->Stride = 0;
123 cl->StrideB = 3 * sizeof(GLfloat);
124 cl->Ptr = MALLOC( cl->StrideB * size );
125 cl->Enabled = 1;
126
127 cl = &ac->Cache.Color;
128 cl->Size = 4;
129 cl->Type = GL_UNSIGNED_BYTE;
130 cl->Stride = 0;
131 cl->StrideB = 4 * sizeof(GLubyte);
132 cl->Ptr = MALLOC( cl->StrideB * size );
133 cl->Enabled = 1;
134
135 cl = &ac->Cache.SecondaryColor;
136 cl->Size = 3;
137 cl->Type = GL_UNSIGNED_BYTE;
138 cl->Stride = 0;
139 cl->StrideB = 4 * sizeof(GLubyte);
140 cl->Ptr = MALLOC( cl->StrideB * size );
141 cl->Enabled = 1;
142
143 cl = &ac->Cache.FogCoord;
144 cl->Size = 1;
145 cl->Type = GL_FLOAT;
146 cl->Stride = 0;
147 cl->StrideB = sizeof(GLfloat);
148 cl->Ptr = MALLOC( cl->StrideB * size );
149 cl->Enabled = 1;
150
151 cl = &ac->Cache.Index;
152 cl->Size = 1;
153 cl->Type = GL_UNSIGNED_INT;
154 cl->Stride = 0;
155 cl->StrideB = sizeof(GLuint);
156 cl->Ptr = MALLOC( cl->StrideB * size );
157 cl->Enabled = 1;
158
159 for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++) {
160 cl = &ac->Cache.TexCoord[i];
161 cl->Size = 4;
162 cl->Type = GL_FLOAT;
163 cl->Stride = 0;
164 cl->StrideB = 4 * sizeof(GLfloat);
165 cl->Ptr = MALLOC( cl->StrideB * size );
166 cl->Enabled = 1;
167 }
168
169 cl = &ac->Cache.EdgeFlag;
170 cl->Size = 1;
171 cl->Type = GL_UNSIGNED_BYTE;
172 cl->Stride = 0;
173 cl->StrideB = sizeof(GLubyte);
174 cl->Ptr = MALLOC( cl->StrideB * size );
175 cl->Enabled = 1;
176 }
177
178
179 /* This storage used to hold translated client data if type or stride
180 * need to be fixed.
181 */
182 static void _ac_elts_init( GLcontext *ctx )
183 {
184 ACcontext *ac = AC_CONTEXT(ctx);
185 GLuint size = 1000;
186
187 ac->Elts = (GLuint *)MALLOC( sizeof(GLuint) * size );
188 ac->elt_size = size;
189 }
190
191 static void _ac_raw_init( GLcontext *ctx )
192 {
193 ACcontext *ac = AC_CONTEXT(ctx);
194 GLuint i;
195
196 ac->Raw.Color = ac->Fallback.Color;
197 ac->Raw.EdgeFlag = ac->Fallback.EdgeFlag;
198 ac->Raw.FogCoord = ac->Fallback.FogCoord;
199 ac->Raw.Index = ac->Fallback.Index;
200 ac->Raw.Normal = ac->Fallback.Normal;
201 ac->Raw.SecondaryColor = ac->Fallback.SecondaryColor;
202 ac->Raw.Vertex = ctx->Array.Vertex;
203
204 ac->IsCached.Color = GL_FALSE;
205 ac->IsCached.EdgeFlag = GL_FALSE;
206 ac->IsCached.FogCoord = GL_FALSE;
207 ac->IsCached.Index = GL_FALSE;
208 ac->IsCached.Normal = GL_FALSE;
209 ac->IsCached.SecondaryColor = GL_FALSE;
210 ac->IsCached.Vertex = GL_FALSE;
211
212 for (i = 0 ; i < MAX_TEXTURE_UNITS ; i++) {
213 ac->Raw.TexCoord[i] = ac->Fallback.TexCoord[i];
214 ac->IsCached.TexCoord[i] = GL_FALSE;
215 }
216
217 }
218
219 GLboolean _ac_CreateContext( GLcontext *ctx )
220 {
221 ctx->acache_context = CALLOC(sizeof(ACcontext));
222 if (ctx->acache_context) {
223 _ac_cache_init( ctx );
224 _ac_fallbacks_init( ctx );
225 _ac_raw_init( ctx );
226 _ac_elts_init( ctx );
227 return GL_TRUE;
228 }
229 return GL_FALSE;
230 }
231
232 void _ac_DestroyContext( GLcontext *ctx )
233 {
234 ACcontext *ac = AC_CONTEXT(ctx);
235 GLint i;
236
237 if (ac->Cache.Vertex.Ptr) FREE( ac->Cache.Vertex.Ptr );
238 if (ac->Cache.Normal.Ptr) FREE( ac->Cache.Normal.Ptr );
239 if (ac->Cache.Color.Ptr) FREE( ac->Cache.Color.Ptr );
240 if (ac->Cache.SecondaryColor.Ptr) FREE( ac->Cache.SecondaryColor.Ptr );
241 if (ac->Cache.EdgeFlag.Ptr) FREE( ac->Cache.EdgeFlag.Ptr );
242 if (ac->Cache.Index.Ptr) FREE( ac->Cache.Index.Ptr );
243 if (ac->Cache.FogCoord.Ptr) FREE( ac->Cache.FogCoord.Ptr );
244
245 for (i = 0; i < MAX_TEXTURE_UNITS; i++) {
246 if (ac->Cache.TexCoord[i].Ptr)
247 FREE( ac->Cache.TexCoord[i].Ptr );
248 }
249
250 if (ac->Elts) FREE( ac->Elts );
251 }
252
253 void _ac_InvalidateState( GLcontext *ctx, GLuint new_state )
254 {
255 AC_CONTEXT(ctx)->NewState |= new_state;
256 AC_CONTEXT(ctx)->NewArrayState |= ctx->Array.NewState;
257 }