vbo: get rid of needless NR_MAT_ATTRIBS constant
[mesa.git] / src / mesa / vbo / vbo_context.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Keith Whitwell <keithw@vmware.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
37 static GLuint check_size( const GLfloat *attr )
38 {
39 if (attr[3] != 1.0F) return 4;
40 if (attr[2] != 0.0F) return 3;
41 if (attr[1] != 0.0F) return 2;
42 return 1;
43 }
44
45
46 static void init_legacy_currval(struct gl_context *ctx)
47 {
48 struct vbo_context *vbo = vbo_context(ctx);
49 struct gl_client_array *arrays = &vbo->currval[VBO_ATTRIB_POS];
50 GLuint i;
51
52 memset(arrays, 0, sizeof(*arrays) * VERT_ATTRIB_FF_MAX);
53
54 /* Set up a constant (StrideB == 0) array for each current
55 * attribute:
56 */
57 for (i = 0; i < VERT_ATTRIB_FF_MAX; i++) {
58 struct gl_client_array *cl = &arrays[i];
59
60 /* Size will have to be determined at runtime:
61 */
62 cl->Size = check_size(ctx->Current.Attrib[i]);
63 cl->Stride = 0;
64 cl->StrideB = 0;
65 cl->Enabled = 1;
66 cl->Type = GL_FLOAT;
67 cl->Format = GL_RGBA;
68 cl->Ptr = (const void *)ctx->Current.Attrib[i];
69 cl->_ElementSize = cl->Size * sizeof(GLfloat);
70 _mesa_reference_buffer_object(ctx, &cl->BufferObj,
71 ctx->Shared->NullBufferObj);
72 }
73 }
74
75
76 static void init_generic_currval(struct gl_context *ctx)
77 {
78 struct vbo_context *vbo = vbo_context(ctx);
79 struct gl_client_array *arrays = &vbo->currval[VBO_ATTRIB_GENERIC0];
80 GLuint i;
81
82 memset(arrays, 0, sizeof(*arrays) * VERT_ATTRIB_GENERIC_MAX);
83
84 for (i = 0; i < VERT_ATTRIB_GENERIC_MAX; i++) {
85 struct gl_client_array *cl = &arrays[i];
86
87 /* This will have to be determined at runtime:
88 */
89 cl->Size = 1;
90 cl->Type = GL_FLOAT;
91 cl->Format = GL_RGBA;
92 cl->Ptr = (const void *)ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + i];
93 cl->Stride = 0;
94 cl->StrideB = 0;
95 cl->Enabled = 1;
96 cl->_ElementSize = cl->Size * sizeof(GLfloat);
97 _mesa_reference_buffer_object(ctx, &cl->BufferObj,
98 ctx->Shared->NullBufferObj);
99 }
100 }
101
102
103 static void init_mat_currval(struct gl_context *ctx)
104 {
105 struct vbo_context *vbo = vbo_context(ctx);
106 struct gl_client_array *arrays =
107 &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT];
108 GLuint i;
109
110 memset(arrays, 0, sizeof(*arrays) * MAT_ATTRIB_MAX);
111
112 /* Set up a constant (StrideB == 0) array for each current
113 * attribute:
114 */
115 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
116 struct gl_client_array *cl = &arrays[i];
117
118 /* Size is fixed for the material attributes, for others will
119 * be determined at runtime:
120 */
121 switch (i) {
122 case MAT_ATTRIB_FRONT_SHININESS:
123 case MAT_ATTRIB_BACK_SHININESS:
124 cl->Size = 1;
125 break;
126 case MAT_ATTRIB_FRONT_INDEXES:
127 case MAT_ATTRIB_BACK_INDEXES:
128 cl->Size = 3;
129 break;
130 default:
131 cl->Size = 4;
132 break;
133 }
134
135 cl->Ptr = (const void *)ctx->Light.Material.Attrib[i];
136 cl->Type = GL_FLOAT;
137 cl->Format = GL_RGBA;
138 cl->Stride = 0;
139 cl->StrideB = 0;
140 cl->Enabled = 1;
141 cl->_ElementSize = cl->Size * sizeof(GLfloat);
142 _mesa_reference_buffer_object(ctx, &cl->BufferObj,
143 ctx->Shared->NullBufferObj);
144 }
145 }
146
147
148 GLboolean _vbo_CreateContext( struct gl_context *ctx )
149 {
150 struct vbo_context *vbo = CALLOC_STRUCT(vbo_context);
151
152 ctx->vbo_context = vbo;
153
154 /* Initialize the arrayelt helper
155 */
156 if (!ctx->aelt_context &&
157 !_ae_create_context( ctx )) {
158 return GL_FALSE;
159 }
160
161 init_legacy_currval( ctx );
162 init_generic_currval( ctx );
163 init_mat_currval( ctx );
164
165 /* Build mappings from VERT_ATTRIB -> VBO_ATTRIB depending on type
166 * of vertex program active.
167 */
168 {
169 GLuint i;
170
171 /* identity mapping */
172 for (i = 0; i < ARRAY_SIZE(vbo->map_vp_none); i++)
173 vbo->map_vp_none[i] = i;
174 /* map material attribs to generic slots */
175 for (i = 0; i < MAT_ATTRIB_MAX; i++)
176 vbo->map_vp_none[VERT_ATTRIB_GENERIC(i)]
177 = VBO_ATTRIB_MAT_FRONT_AMBIENT + i;
178
179 for (i = 0; i < ARRAY_SIZE(vbo->map_vp_arb); i++)
180 vbo->map_vp_arb[i] = i;
181 }
182
183
184 /* Hook our functions into exec and compile dispatch tables. These
185 * will pretty much be permanently installed, which means that the
186 * vtxfmt mechanism can be removed now.
187 */
188 vbo_exec_init( ctx );
189 if (ctx->API == API_OPENGL_COMPAT)
190 vbo_save_init( ctx );
191
192 _math_init_eval();
193
194 return GL_TRUE;
195 }
196
197
198 void _vbo_InvalidateState( struct gl_context *ctx, GLuint new_state )
199 {
200 vbo_exec_invalidate_state(ctx, new_state);
201 }
202
203
204 void _vbo_DestroyContext( struct gl_context *ctx )
205 {
206 struct vbo_context *vbo = vbo_context(ctx);
207
208 if (ctx->aelt_context) {
209 _ae_destroy_context( ctx );
210 ctx->aelt_context = NULL;
211 }
212
213 if (vbo) {
214 GLuint i;
215
216 for (i = 0; i < VBO_ATTRIB_MAX; i++) {
217 _mesa_reference_buffer_object(ctx, &vbo->currval[i].BufferObj, NULL);
218 }
219
220 vbo_exec_destroy(ctx);
221 if (ctx->API == API_OPENGL_COMPAT)
222 vbo_save_destroy(ctx);
223 free(vbo);
224 ctx->vbo_context = NULL;
225 }
226 }
227
228
229 void vbo_set_draw_func(struct gl_context *ctx, vbo_draw_func func)
230 {
231 struct vbo_context *vbo = vbo_context(ctx);
232 vbo->draw_prims = func;
233 }
234