Merge branch 'gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / vbo / vbo_context.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.3
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28 #include "main/imports.h"
29 #include "main/mtypes.h"
30 #include "main/api_arrayelt.h"
31 #include "vbo.h"
32 #include "vbo_context.h"
33
34 #if 0
35 /* Reach out and grab this to use as the default:
36 */
37 extern void _tnl_draw_prims( GLcontext *ctx,
38 const struct gl_client_array *arrays[],
39 const struct _mesa_prim *prims,
40 GLuint nr_prims,
41 const struct _mesa_index_buffer *ib,
42 GLuint min_index,
43 GLuint max_index );
44 #endif
45
46
47
48 #define NR_LEGACY_ATTRIBS 16
49 #define NR_GENERIC_ATTRIBS 16
50 #define NR_MAT_ATTRIBS 12
51
52 static GLuint check_size( const GLfloat *attr )
53 {
54 if (attr[3] != 1.0) return 4;
55 if (attr[2] != 0.0) return 3;
56 if (attr[1] != 0.0) return 2;
57 return 1;
58 }
59
60 static void init_legacy_currval(GLcontext *ctx)
61 {
62 struct vbo_context *vbo = vbo_context(ctx);
63 struct gl_client_array *arrays = vbo->legacy_currval;
64 GLuint i;
65
66 memset(arrays, 0, sizeof(*arrays) * NR_LEGACY_ATTRIBS);
67
68 /* Set up a constant (StrideB == 0) array for each current
69 * attribute:
70 */
71 for (i = 0; i < NR_LEGACY_ATTRIBS; i++) {
72 struct gl_client_array *cl = &arrays[i];
73
74 /* Size will have to be determined at runtime:
75 */
76 cl->Size = check_size(ctx->Current.Attrib[i]);
77 cl->Stride = 0;
78 cl->StrideB = 0;
79 cl->Enabled = 1;
80 cl->Type = GL_FLOAT;
81 cl->Ptr = (const void *)ctx->Current.Attrib[i];
82 cl->BufferObj = ctx->Array.NullBufferObj;
83 }
84 }
85
86
87 static void init_generic_currval(GLcontext *ctx)
88 {
89 struct vbo_context *vbo = vbo_context(ctx);
90 struct gl_client_array *arrays = vbo->generic_currval;
91 GLuint i;
92
93 memset(arrays, 0, sizeof(*arrays) * NR_GENERIC_ATTRIBS);
94
95 for (i = 0; i < NR_GENERIC_ATTRIBS; i++) {
96 struct gl_client_array *cl = &arrays[i];
97
98 /* This will have to be determined at runtime:
99 */
100 cl->Size = 1;
101 cl->Type = GL_FLOAT;
102 cl->Ptr = (const void *)ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + i];
103 cl->Stride = 0;
104 cl->StrideB = 0;
105 cl->Enabled = 1;
106 cl->BufferObj = ctx->Array.NullBufferObj;
107 }
108 }
109
110
111 static void init_mat_currval(GLcontext *ctx)
112 {
113 struct vbo_context *vbo = vbo_context(ctx);
114 struct gl_client_array *arrays = vbo->mat_currval;
115 GLuint i;
116
117 ASSERT(NR_MAT_ATTRIBS == MAT_ATTRIB_MAX);
118
119 memset(arrays, 0, sizeof(*arrays) * NR_MAT_ATTRIBS);
120
121 /* Set up a constant (StrideB == 0) array for each current
122 * attribute:
123 */
124 for (i = 0; i < NR_MAT_ATTRIBS; i++) {
125 struct gl_client_array *cl = &arrays[i];
126
127 /* Size is fixed for the material attributes, for others will
128 * be determined at runtime:
129 */
130 switch (i - VERT_ATTRIB_GENERIC0) {
131 case MAT_ATTRIB_FRONT_SHININESS:
132 case MAT_ATTRIB_BACK_SHININESS:
133 cl->Size = 1;
134 break;
135 case MAT_ATTRIB_FRONT_INDEXES:
136 case MAT_ATTRIB_BACK_INDEXES:
137 cl->Size = 3;
138 break;
139 default:
140 cl->Size = 4;
141 break;
142 }
143
144 cl->Ptr = (const void *)ctx->Light.Material.Attrib[i];
145 cl->Type = GL_FLOAT;
146 cl->Stride = 0;
147 cl->StrideB = 0;
148 cl->Enabled = 1;
149 cl->BufferObj = ctx->Array.NullBufferObj;
150 }
151 }
152
153 #if 0
154
155 static void vbo_exec_current_init( struct vbo_exec_context *exec )
156 {
157 GLcontext *ctx = exec->ctx;
158 GLint i;
159
160 /* setup the pointers for the typical 16 vertex attributes */
161 for (i = 0; i < VBO_ATTRIB_FIRST_MATERIAL; i++)
162 exec->vtx.current[i] = ctx->Current.Attrib[i];
163
164 /* setup pointers for the 12 material attributes */
165 for (i = 0; i < MAT_ATTRIB_MAX; i++)
166 exec->vtx.current[VBO_ATTRIB_FIRST_MATERIAL + i] =
167 ctx->Light.Material.Attrib[i];
168 }
169 #endif
170
171 GLboolean _vbo_CreateContext( GLcontext *ctx )
172 {
173 struct vbo_context *vbo = CALLOC_STRUCT(vbo_context);
174
175 ctx->swtnl_im = (void *)vbo;
176
177 /* Initialize the arrayelt helper
178 */
179 if (!ctx->aelt_context &&
180 !_ae_create_context( ctx )) {
181 return GL_FALSE;
182 }
183
184 /* TODO: remove these pointers.
185 */
186 vbo->legacy_currval = &vbo->currval[VBO_ATTRIB_POS];
187 vbo->generic_currval = &vbo->currval[VBO_ATTRIB_GENERIC0];
188 vbo->mat_currval = &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT];
189
190 init_legacy_currval( ctx );
191 init_generic_currval( ctx );
192 init_mat_currval( ctx );
193
194 /* Build mappings from VERT_ATTRIB -> VBO_ATTRIB depending on type
195 * of vertex program active.
196 */
197 {
198 GLuint i;
199
200 /* When no vertex program, pull in the material attributes in
201 * the 16..32 generic range.
202 */
203 for (i = 0; i < 16; i++)
204 vbo->map_vp_none[i] = i;
205 for (i = 0; i < 12; i++)
206 vbo->map_vp_none[16+i] = VBO_ATTRIB_MAT_FRONT_AMBIENT + i;
207 for (i = 0; i < 4; i++)
208 vbo->map_vp_none[28+i] = i;
209
210 for (i = 0; i < VERT_ATTRIB_MAX; i++)
211 vbo->map_vp_arb[i] = i;
212 }
213
214
215 /* By default:
216 */
217 #if 0 /* dead - see vbo_set_draw_func() */
218 vbo->draw_prims = _tnl_draw_prims;
219 #endif
220
221 /* Hook our functions into exec and compile dispatch tables. These
222 * will pretty much be permanently installed, which means that the
223 * vtxfmt mechanism can be removed now.
224 */
225 vbo_exec_init( ctx );
226 #if FEATURE_dlist
227 vbo_save_init( ctx );
228 #endif
229
230 return GL_TRUE;
231 }
232
233 void _vbo_InvalidateState( GLcontext *ctx, GLuint new_state )
234 {
235 _ae_invalidate_state(ctx, new_state);
236 vbo_exec_invalidate_state(ctx, new_state);
237 }
238
239
240 void _vbo_DestroyContext( GLcontext *ctx )
241 {
242 if (ctx->aelt_context) {
243 _ae_destroy_context( ctx );
244 ctx->aelt_context = NULL;
245 }
246
247 vbo_exec_destroy(ctx);
248 #if FEATURE_dlist
249 vbo_save_destroy(ctx);
250 #endif
251 FREE(vbo_context(ctx));
252 ctx->swtnl_im = NULL;
253 }
254
255
256 void vbo_set_draw_func(GLcontext *ctx, vbo_draw_func func)
257 {
258 struct vbo_context *vbo = vbo_context(ctx);
259 vbo->draw_prims = func;
260 }
261