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