4499803b8c70d6ad6a925dda61eff21b4043c17c
[mesa.git] / src / mesa / vbo / vbo_exec.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
29 #include "api_arrayelt.h"
30 #include "glheader.h"
31 #include "imports.h"
32 #include "context.h"
33 #include "macros.h"
34 #include "mtypes.h"
35 #include "dlist.h"
36 #include "vtxfmt.h"
37
38 #include "vbo_context.h"
39
40
41 #define NR_LEGACY_ATTRIBS 16
42 #define NR_GENERIC_ATTRIBS 16
43 #define NR_MAT_ATTRIBS 12
44
45 static void init_legacy_currval(GLcontext *ctx)
46 {
47 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
48 struct gl_client_array *arrays = exec->legacy_currval;
49 GLuint i;
50
51 memset(arrays, 0, sizeof(*arrays) * NR_LEGACY_ATTRIBS);
52
53 /* Set up a constant (StrideB == 0) array for each current
54 * attribute:
55 */
56 for (i = 0; i < NR_LEGACY_ATTRIBS; i++) {
57 struct gl_client_array *cl = &arrays[i];
58
59 switch (i) {
60 case VBO_ATTRIB_EDGEFLAG:
61 cl->Type = GL_UNSIGNED_BYTE;
62 cl->Ptr = (const void *)&ctx->Current.EdgeFlag;
63 break;
64 case VBO_ATTRIB_INDEX:
65 cl->Type = GL_FLOAT;
66 cl->Ptr = (const void *)&ctx->Current.Index;
67 break;
68 default:
69 cl->Type = GL_FLOAT;
70 cl->Ptr = (const void *)ctx->Current.Attrib[i];
71 break;
72 }
73
74 /* This will have to be determined at runtime:
75 */
76 cl->Size = 1;
77 cl->Stride = 0;
78 cl->StrideB = 0;
79 cl->Enabled = 1;
80 cl->BufferObj = ctx->Array.NullBufferObj;
81 }
82 }
83
84
85 static void init_generic_currval(GLcontext *ctx)
86 {
87 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
88 struct gl_client_array *arrays = exec->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
100 cl->Type = GL_FLOAT;
101 cl->Ptr = (const void *)ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + i];
102 cl->Stride = 0;
103 cl->StrideB = 0;
104 cl->Enabled = 1;
105 cl->BufferObj = ctx->Array.NullBufferObj;
106 }
107 }
108
109
110 static void init_mat_currval(GLcontext *ctx)
111 {
112 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
113 struct gl_client_array *arrays = exec->mat_currval;
114 GLuint i;
115
116 memset(arrays, 0, sizeof(*arrays) * NR_GENERIC_ATTRIBS);
117
118 /* Set up a constant (StrideB == 0) array for each current
119 * attribute:
120 */
121 for (i = 0; i < NR_GENERIC_ATTRIBS; i++) {
122 struct gl_client_array *cl = &arrays[i];
123
124 /* Size is fixed for the material attributes, for others will
125 * be determined at runtime:
126 */
127 switch (i - VERT_ATTRIB_GENERIC0) {
128 case MAT_ATTRIB_FRONT_SHININESS:
129 case MAT_ATTRIB_BACK_SHININESS:
130 cl->Size = 1;
131 break;
132 case MAT_ATTRIB_FRONT_INDEXES:
133 case MAT_ATTRIB_BACK_INDEXES:
134 cl->Size = 3;
135 break;
136 default:
137 cl->Size = 4;
138 break;
139 }
140
141 if (i < MAT_ATTRIB_MAX)
142 cl->Ptr = (const void *)ctx->Light.Material.Attrib[i];
143 else
144 cl->Ptr = (const void *)ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + i];
145
146 cl->Type = GL_FLOAT;
147 cl->Stride = 0;
148 cl->StrideB = 0;
149 cl->Enabled = 1;
150 cl->BufferObj = ctx->Array.NullBufferObj;
151 }
152 }
153
154
155 void vbo_exec_init( GLcontext *ctx )
156 {
157 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
158
159 exec->ctx = ctx;
160
161 /* Initialize the arrayelt helper
162 */
163 if (!ctx->aelt_context &&
164 !_ae_create_context( ctx ))
165 return;
166
167 vbo_exec_vtx_init( exec );
168 vbo_exec_array_init( exec );
169
170 init_legacy_currval( ctx );
171 init_generic_currval( ctx );
172 init_mat_currval( ctx );
173
174 ctx->Driver.NeedFlush = 0;
175 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
176 ctx->Driver.FlushVertices = vbo_exec_FlushVertices;
177
178 exec->eval.recalculate_maps = 1;
179 }
180
181
182 void vbo_exec_destroy( GLcontext *ctx )
183 {
184 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
185
186 if (ctx->aelt_context) {
187 _ae_destroy_context( ctx );
188 ctx->aelt_context = NULL;
189 }
190
191 vbo_exec_vtx_destroy( exec );
192 vbo_exec_array_destroy( exec );
193 }
194
195 /* Really want to install these callbacks to a central facility to be
196 * invoked according to the state flags. That will have to wait for a
197 * mesa rework:
198 */
199 void vbo_exec_invalidate_state( GLcontext *ctx, GLuint new_state )
200 {
201 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
202
203 if (new_state & (_NEW_PROGRAM|_NEW_EVAL))
204 exec->eval.recalculate_maps = 1;
205
206 _ae_invalidate_state(ctx, new_state);
207 }
208
209
210 void vbo_exec_wakeup( GLcontext *ctx )
211 {
212 struct vbo_exec_context *exec = &vbo_context(ctx)->exec;
213
214 ctx->Driver.FlushVertices = vbo_exec_FlushVertices;
215 ctx->Driver.NeedFlush |= FLUSH_UPDATE_CURRENT;
216
217 /* Hook our functions into exec and compile dispatch tables.
218 */
219 _mesa_install_exec_vtxfmt( ctx, &exec->vtxfmt );
220
221 /* Assume we haven't been getting state updates either:
222 */
223 vbo_exec_invalidate_state( ctx, ~0 );
224 }
225
226
227