vbo_context.c revision 7ec681f3
11.1Schristos/*
21.1Schristos * Mesa 3-D graphics library
31.1Schristos *
41.1Schristos * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
51.1Schristos *
61.1Schristos * Permission is hereby granted, free of charge, to any person obtaining a
71.1Schristos * copy of this software and associated documentation files (the "Software"),
81.1Schristos * to deal in the Software without restriction, including without limitation
91.5Sandvar * the rights to use, copy, modify, merge, publish, distribute, sublicense,
101.1Schristos * and/or sell copies of the Software, and to permit persons to whom the
111.1Schristos * Software is furnished to do so, subject to the following conditions:
121.1Schristos *
131.1Schristos * The above copyright notice and this permission notice shall be included
141.1Schristos * in all copies or substantial portions of the Software.
151.1Schristos *
161.1Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
171.1Schristos * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181.1Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
191.1Schristos * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
201.1Schristos * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
211.1Schristos * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
221.1Schristos * OTHER DEALINGS IN THE SOFTWARE.
231.1Schristos *
241.1Schristos * Authors:
251.1Schristos *    Keith Whitwell <keithw@vmware.com>
261.1Schristos */
271.4Sandvar
281.1Schristos#include "main/errors.h"
291.1Schristos#include "main/bufferobj.h"
301.1Schristos#include "math/m_eval.h"
311.1Schristos#include "main/vtxfmt.h"
321.1Schristos#include "main/api_arrayelt.h"
331.1Schristos#include "main/arrayobj.h"
341.1Schristos#include "main/varray.h"
351.1Schristos#include "util/u_memory.h"
361.1Schristos#include "vbo.h"
371.1Schristos#include "vbo_private.h"
381.1Schristos
391.1Schristos
401.1Schristosstatic GLuint
411.1Schristoscheck_size(const GLfloat *attr)
421.1Schristos{
431.1Schristos   if (attr[3] != 1.0F)
441.1Schristos      return 4;
451.1Schristos   if (attr[2] != 0.0F)
461.1Schristos      return 3;
471.1Schristos   if (attr[1] != 0.0F)
481.1Schristos      return 2;
491.1Schristos   return 1;
501.1Schristos}
511.1Schristos
521.1Schristos
531.1Schristos/**
541.1Schristos * Helper for initializing a vertex array.
551.1Schristos */
561.1Schristosstatic void
571.1Schristosinit_array(struct gl_context *ctx, struct gl_array_attributes *attrib,
581.1Schristos           unsigned size, const void *pointer)
591.1Schristos{
601.1Schristos   memset(attrib, 0, sizeof(*attrib));
611.1Schristos
621.1Schristos   vbo_set_vertex_format(&attrib->Format, size, GL_FLOAT);
631.1Schristos   attrib->Stride = 0;
641.1Schristos   attrib->Ptr = pointer;
651.1Schristos}
661.1Schristos
671.1Schristos
681.1Schristos/**
691.1Schristos * Set up the vbo->currval arrays to point at the context's current
701.1Schristos * vertex attributes (with strides = 0).
711.1Schristos */
721.1Schristosstatic void
731.1Schristosinit_legacy_currval(struct gl_context *ctx)
741.1Schristos{
751.1Schristos   struct vbo_context *vbo = vbo_context(ctx);
761.1Schristos
771.1Schristos   /* Set up a constant (Stride == 0) array for each current
781.1Schristos    * attribute:
791.1Schristos    */
801.1Schristos   for (int attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
811.1Schristos      if (VERT_BIT(attr) & VERT_BIT_GENERIC_ALL)
821.1Schristos         continue;
831.1Schristos
841.3Sandvar      struct gl_array_attributes *attrib = &vbo->current[attr];
851.1Schristos
861.1Schristos      init_array(ctx, attrib, check_size(ctx->Current.Attrib[attr]),
871.1Schristos                 ctx->Current.Attrib[attr]);
881.1Schristos   }
891.1Schristos}
901.1Schristos
911.1Schristos
921.5Sandvarstatic void
931.1Schristosinit_generic_currval(struct gl_context *ctx)
941.1Schristos{
951.1Schristos   struct vbo_context *vbo = vbo_context(ctx);
961.1Schristos   GLuint i;
971.1Schristos
981.1Schristos   for (i = 0; i < VERT_ATTRIB_GENERIC_MAX; i++) {
991.1Schristos      const unsigned attr = VBO_ATTRIB_GENERIC0 + i;
1001.1Schristos      struct gl_array_attributes *attrib = &vbo->current[attr];
1011.1Schristos
1021.1Schristos      init_array(ctx, attrib, 1, ctx->Current.Attrib[attr]);
1031.1Schristos   }
1041.1Schristos}
1051.1Schristos
1061.1Schristos
1071.1Schristosstatic void
1081.1Schristosinit_mat_currval(struct gl_context *ctx)
1091.1Schristos{
1101.1Schristos   struct vbo_context *vbo = vbo_context(ctx);
1111.1Schristos   GLuint i;
1121.1Schristos
1131.1Schristos   /* Set up a constant (StrideB == 0) array for each current
1141.1Schristos    * attribute:
1151.1Schristos    */
1161.1Schristos   for (i = 0; i < MAT_ATTRIB_MAX; i++) {
1171.1Schristos      const unsigned attr = VBO_ATTRIB_MAT_FRONT_AMBIENT + i;
1181.1Schristos      struct gl_array_attributes *attrib = &vbo->current[attr];
1191.1Schristos      unsigned size;
1201.1Schristos
1211.1Schristos      /* Size is fixed for the material attributes, for others will
1221.1Schristos       * be determined at runtime:
1231.1Schristos       */
1241.1Schristos      switch (i) {
1251.1Schristos      case MAT_ATTRIB_FRONT_SHININESS:
1261.1Schristos      case MAT_ATTRIB_BACK_SHININESS:
1271.1Schristos         size = 1;
1281.1Schristos         break;
1291.1Schristos      case MAT_ATTRIB_FRONT_INDEXES:
1301.1Schristos      case MAT_ATTRIB_BACK_INDEXES:
1311.1Schristos         size = 3;
1321.1Schristos         break;
1331.1Schristos      default:
1341.1Schristos         size = 4;
1351.1Schristos         break;
1361.1Schristos      }
1371.1Schristos
1381.1Schristos      init_array(ctx, attrib, size, ctx->Light.Material.Attrib[i]);
1391.1Schristos   }
1401.1Schristos}
1411.1Schristos
1421.2Sjakllsch
143void
144_vbo_install_exec_vtxfmt(struct gl_context *ctx)
145{
146   struct vbo_context *vbo = vbo_context(ctx);
147
148   _mesa_install_exec_vtxfmt(ctx, &vbo->exec.vtxfmt);
149}
150
151
152void
153vbo_exec_update_eval_maps(struct gl_context *ctx)
154{
155   struct vbo_context *vbo = vbo_context(ctx);
156
157   vbo->exec.eval.recalculate_maps = GL_TRUE;
158}
159
160
161GLboolean
162_vbo_CreateContext(struct gl_context *ctx, bool use_buffer_objects)
163{
164   struct vbo_context *vbo = &ctx->vbo_context;
165
166   memset(vbo, 0, sizeof(*vbo));
167
168   vbo->binding.Offset = 0;
169   vbo->binding.Stride = 0;
170   vbo->binding.InstanceDivisor = 0;
171
172   init_legacy_currval(ctx);
173   init_generic_currval(ctx);
174   init_mat_currval(ctx);
175
176   /* make sure all VBO_ATTRIB_ values can fit in an unsigned byte */
177   STATIC_ASSERT(VBO_ATTRIB_MAX <= 255);
178
179   /* Hook our functions into exec and compile dispatch tables.  These
180    * will pretty much be permanently installed, which means that the
181    * vtxfmt mechanism can be removed now.
182    */
183   vbo_exec_init(ctx, use_buffer_objects);
184   if (ctx->API == API_OPENGL_COMPAT)
185      vbo_save_init(ctx);
186
187   vbo->VAO = _mesa_new_vao(ctx, ~((GLuint)0));
188   /* The exec VAO assumes to have all arributes bound to binding 0 */
189   for (unsigned i = 0; i < VERT_ATTRIB_MAX; ++i)
190      _mesa_vertex_attrib_binding(ctx, vbo->VAO, i, 0);
191
192   _math_init_eval();
193
194   return GL_TRUE;
195}
196
197
198void
199_vbo_DestroyContext(struct gl_context *ctx)
200{
201   struct vbo_context *vbo = vbo_context(ctx);
202
203   if (vbo) {
204      _mesa_reference_buffer_object(ctx, &vbo->binding.BufferObj, NULL);
205
206      vbo_exec_destroy(ctx);
207      if (ctx->API == API_OPENGL_COMPAT)
208         vbo_save_destroy(ctx);
209      _mesa_reference_vao(ctx, &vbo->VAO, NULL);
210   }
211}
212
213
214const struct gl_array_attributes *
215_vbo_current_attrib(const struct gl_context *ctx, gl_vert_attrib attr)
216{
217   const struct vbo_context *vbo = vbo_context_const(ctx);
218   const gl_vertex_processing_mode vmp = ctx->VertexProgram._VPMode;
219   return &vbo->current[_vbo_attribute_alias_map[vmp][attr]];
220}
221
222
223const struct gl_vertex_buffer_binding *
224_vbo_current_binding(const struct gl_context *ctx)
225{
226   const struct vbo_context *vbo = vbo_context_const(ctx);
227   return &vbo->binding;
228}
229