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