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