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