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