Merge branch '965-glsl'
[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 /* Reach out and grab this to use as the default:
35 */
36 extern void _tnl_draw_prims( GLcontext *ctx,
37 const struct gl_client_array *arrays[],
38 const struct _mesa_prim *prims,
39 GLuint nr_prims,
40 const struct _mesa_index_buffer *ib,
41 GLuint min_index,
42 GLuint max_index );
43
44
45
46 #define NR_LEGACY_ATTRIBS 16
47 #define NR_GENERIC_ATTRIBS 16
48 #define NR_MAT_ATTRIBS 12
49
50 static GLuint check_size( const GLfloat *attr )
51 {
52 if (attr[3] != 1.0) return 4;
53 if (attr[2] != 0.0) return 3;
54 if (attr[1] != 0.0) return 2;
55 return 1;
56 }
57
58 static void init_legacy_currval(GLcontext *ctx)
59 {
60 struct vbo_context *vbo = vbo_context(ctx);
61 struct gl_client_array *arrays = vbo->legacy_currval;
62 GLuint i;
63
64 memset(arrays, 0, sizeof(*arrays) * NR_LEGACY_ATTRIBS);
65
66 /* Set up a constant (StrideB == 0) array for each current
67 * attribute:
68 */
69 for (i = 0; i < NR_LEGACY_ATTRIBS; i++) {
70 struct gl_client_array *cl = &arrays[i];
71
72 /* Size will have to be determined at runtime:
73 */
74 cl->Size = check_size(ctx->Current.Attrib[i]);
75 cl->Stride = 0;
76 cl->StrideB = 0;
77 cl->Enabled = 1;
78 cl->Type = GL_FLOAT;
79 cl->Ptr = (const void *)ctx->Current.Attrib[i];
80 cl->BufferObj = ctx->Array.NullBufferObj;
81 }
82 }
83
84
85 static void init_generic_currval(GLcontext *ctx)
86 {
87 struct vbo_context *vbo = vbo_context(ctx);
88 struct gl_client_array *arrays = vbo->generic_currval;
89 GLuint i;
90
91 memset(arrays, 0, sizeof(*arrays) * NR_GENERIC_ATTRIBS);
92
93 for (i = 0; i < NR_GENERIC_ATTRIBS; i++) {
94 struct gl_client_array *cl = &arrays[i];
95
96 /* This will have to be determined at runtime:
97 */
98 cl->Size = 1;
99 cl->Type = GL_FLOAT;
100 cl->Ptr = (const void *)ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + i];
101 cl->Stride = 0;
102 cl->StrideB = 0;
103 cl->Enabled = 1;
104 cl->BufferObj = ctx->Array.NullBufferObj;
105 }
106 }
107
108
109 static void init_mat_currval(GLcontext *ctx)
110 {
111 struct vbo_context *vbo = vbo_context(ctx);
112 struct gl_client_array *arrays = vbo->mat_currval;
113 GLuint i;
114
115 ASSERT(NR_MAT_ATTRIBS == MAT_ATTRIB_MAX);
116
117 memset(arrays, 0, sizeof(*arrays) * NR_MAT_ATTRIBS);
118
119 /* Set up a constant (StrideB == 0) array for each current
120 * attribute:
121 */
122 for (i = 0; i < NR_MAT_ATTRIBS; i++) {
123 struct gl_client_array *cl = &arrays[i];
124
125 /* Size is fixed for the material attributes, for others will
126 * be determined at runtime:
127 */
128 switch (i - VERT_ATTRIB_GENERIC0) {
129 case MAT_ATTRIB_FRONT_SHININESS:
130 case MAT_ATTRIB_BACK_SHININESS:
131 cl->Size = 1;
132 break;
133 case MAT_ATTRIB_FRONT_INDEXES:
134 case MAT_ATTRIB_BACK_INDEXES:
135 cl->Size = 3;
136 break;
137 default:
138 cl->Size = 4;
139 break;
140 }
141
142 cl->Ptr = (const void *)ctx->Light.Material.Attrib[i];
143 cl->Type = GL_FLOAT;
144 cl->Stride = 0;
145 cl->StrideB = 0;
146 cl->Enabled = 1;
147 cl->BufferObj = ctx->Array.NullBufferObj;
148 }
149 }
150
151 #if 0
152
153 static void vbo_exec_current_init( struct vbo_exec_context *exec )
154 {
155 GLcontext *ctx = exec->ctx;
156 GLint i;
157
158 /* setup the pointers for the typical 16 vertex attributes */
159 for (i = 0; i < VBO_ATTRIB_FIRST_MATERIAL; i++)
160 exec->vtx.current[i] = ctx->Current.Attrib[i];
161
162 /* setup pointers for the 12 material attributes */
163 for (i = 0; i < MAT_ATTRIB_MAX; i++)
164 exec->vtx.current[VBO_ATTRIB_FIRST_MATERIAL + i] =
165 ctx->Light.Material.Attrib[i];
166 }
167 #endif
168
169 GLboolean _vbo_CreateContext( GLcontext *ctx )
170 {
171 struct vbo_context *vbo = CALLOC_STRUCT(vbo_context);
172
173 ctx->swtnl_im = (void *)vbo;
174
175 /* Initialize the arrayelt helper
176 */
177 if (!ctx->aelt_context &&
178 !_ae_create_context( ctx )) {
179 return GL_FALSE;
180 }
181
182 /* TODO: remove these pointers.
183 */
184 vbo->legacy_currval = &vbo->currval[VBO_ATTRIB_POS];
185 vbo->generic_currval = &vbo->currval[VBO_ATTRIB_GENERIC0];
186 vbo->mat_currval = &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT];
187
188 init_legacy_currval( ctx );
189 init_generic_currval( ctx );
190 init_mat_currval( ctx );
191
192 /* Build mappings from VERT_ATTRIB -> VBO_ATTRIB depending on type
193 * of vertex program active.
194 */
195 {
196 GLuint i;
197
198 /* When no vertex program, pull in the material attributes in
199 * the 16..32 generic range.
200 */
201 for (i = 0; i < 16; i++)
202 vbo->map_vp_none[i] = i;
203 for (i = 0; i < 12; i++)
204 vbo->map_vp_none[16+i] = VBO_ATTRIB_MAT_FRONT_AMBIENT + i;
205 for (i = 0; i < 4; i++)
206 vbo->map_vp_none[28+i] = i;
207
208 for (i = 0; i < VERT_ATTRIB_MAX; i++)
209 vbo->map_vp_arb[i] = i;
210 }
211
212
213 /* By default:
214 */
215 vbo->draw_prims = _tnl_draw_prims;
216
217 /* Hook our functions into exec and compile dispatch tables. These
218 * will pretty much be permanently installed, which means that the
219 * vtxfmt mechanism can be removed now.
220 */
221 vbo_exec_init( ctx );
222 vbo_save_init( ctx );
223
224
225 return GL_TRUE;
226 }
227
228 void _vbo_InvalidateState( GLcontext *ctx, GLuint new_state )
229 {
230 _ae_invalidate_state(ctx, new_state);
231 vbo_exec_invalidate_state(ctx, new_state);
232 }
233
234
235 void _vbo_DestroyContext( GLcontext *ctx )
236 {
237 if (ctx->aelt_context) {
238 _ae_destroy_context( ctx );
239 ctx->aelt_context = NULL;
240 }
241
242 vbo_exec_destroy(ctx);
243 FREE(vbo_context(ctx));
244 ctx->swtnl_im = NULL;
245 }