vbo_exec_array.c revision 5a652f595716a82ebd79e33011f6082199c4b0d0
1/**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include "glheader.h"
29#include "context.h"
30#include "state.h"
31#include "api_validate.h"
32#include "api_noop.h"
33#include "dispatch.h"
34
35#include "vbo_context.h"
36
37static GLuint get_max_index( GLuint count, GLuint type,
38			     const GLvoid *indices )
39{
40   GLint i;
41
42   /* Compute max element.  This is only needed for upload of non-VBO,
43    * non-constant data elements.
44    *
45    * XXX: Postpone this calculation until it is known that it is
46    * needed.  Otherwise could scan this pointlessly in the all-vbo
47    * case.
48    */
49   switch(type) {
50   case GL_UNSIGNED_INT: {
51      const GLuint *ui_indices = (const GLuint *)indices;
52      GLuint max_ui = 0;
53      for (i = 0; i < count; i++)
54	 if (ui_indices[i] > max_ui)
55	    max_ui = ui_indices[i];
56      return max_ui;
57   }
58   case GL_UNSIGNED_SHORT: {
59      const GLushort *us_indices = (const GLushort *)indices;
60      GLuint max_us = 0;
61      for (i = 0; i < count; i++)
62	 if (us_indices[i] > max_us)
63	    max_us = us_indices[i];
64      return max_us;
65   }
66   case GL_UNSIGNED_BYTE: {
67      const GLubyte *ub_indices = (const GLubyte *)indices;
68      GLuint max_ub = 0;
69      for (i = 0; i < count; i++)
70	 if (ub_indices[i] > max_ub)
71	    max_ub = ub_indices[i];
72      return max_ub;
73   }
74   default:
75      return 0;
76   }
77}
78
79
80/* Just translate the arrayobj into a sane layout.
81 */
82static void bind_array_obj( GLcontext *ctx )
83{
84   struct vbo_context *vbo = vbo_context(ctx);
85   struct vbo_exec_context *exec = &vbo->exec;
86   GLuint i;
87
88   /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
89    * rather than as individual named arrays.  Then this function can
90    * go away.
91    */
92   exec->array.legacy_array[VERT_ATTRIB_POS] = &ctx->Array.ArrayObj->Vertex;
93   exec->array.legacy_array[VERT_ATTRIB_WEIGHT] = &vbo->legacy_currval[i];
94   exec->array.legacy_array[VERT_ATTRIB_NORMAL] = &ctx->Array.ArrayObj->Normal;
95   exec->array.legacy_array[VERT_ATTRIB_COLOR0] = &ctx->Array.ArrayObj->Color;
96   exec->array.legacy_array[VERT_ATTRIB_COLOR1] = &ctx->Array.ArrayObj->SecondaryColor;
97   exec->array.legacy_array[VERT_ATTRIB_FOG] = &ctx->Array.ArrayObj->FogCoord;
98   exec->array.legacy_array[VERT_ATTRIB_COLOR_INDEX] = &ctx->Array.ArrayObj->Index;
99   exec->array.legacy_array[VBO_ATTRIB_EDGEFLAG] = &ctx->Array.ArrayObj->EdgeFlag;
100
101   for (i = 0; i < 8; i++)
102      exec->array.legacy_array[VBO_ATTRIB_TEX0 + i] = &ctx->Array.ArrayObj->TexCoord[i];
103
104   for (i = 0; i < VERT_ATTRIB_MAX; i++)
105      exec->array.generic_array[i] = &ctx->Array.ArrayObj->VertexAttrib[i];
106
107   exec->array.array_obj = ctx->Array.ArrayObj->Name;
108}
109
110static void recalculate_input_bindings( GLcontext *ctx )
111{
112   struct vbo_context *vbo = vbo_context(ctx);
113   struct vbo_exec_context *exec = &vbo->exec;
114   const struct gl_client_array **inputs = &exec->array.inputs[0];
115   GLuint i;
116
117   exec->array.program_mode = get_program_mode(ctx);
118   exec->array.enabled_flags = ctx->Array.ArrayObj->_Enabled;
119
120   /* TODO:  Get rid of NV_program (please!).
121    */
122   switch (exec->array.program_mode) {
123   case VP_NONE:
124      /* When no vertex program is active, we put the material values
125       * into the generic slots.  This is the only situation where
126       * material values are available as per-vertex attributes.
127       */
128      for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
129	 if (exec->array.legacy_array[i]->Enabled)
130	    inputs[i] = exec->array.legacy_array[i];
131	 else
132	    inputs[i] = &vbo->legacy_currval[i];
133      }
134
135      for (i = 0; i < MAT_ATTRIB_MAX; i++) {
136	 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->mat_currval[i];
137      }
138
139      /* Could use just about anything, just to fill in the empty
140       * slots:
141       */
142      for (i = MAT_ATTRIB_MAX; i < VERT_ATTRIB_MAX; i++)
143	 inputs[i] = &vbo->generic_currval[i - VERT_ATTRIB_GENERIC0];
144
145      break;
146   case VP_NV:
147      /* NV_vertex_program - attribute arrays alias and override
148       * conventional, legacy arrays.  No materials, and the generic
149       * slots are vacant.
150       */
151      for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
152	 if (exec->array.generic_array[i]->Enabled)
153	    inputs[i] = exec->array.generic_array[i];
154	 else if (exec->array.legacy_array[i]->Enabled)
155	    inputs[i] = exec->array.legacy_array[i];
156	 else
157	    inputs[i] = &vbo->legacy_currval[i];
158      }
159      break;
160   case VP_ARB:
161      /* ARB_vertex_program - Only the attribute zero (position) array
162       * aliases and overrides the legacy position array.
163       *
164       * Otherwise, legacy attributes available in the legacy slots,
165       * generic attributes in the generic slots and materials are not
166       * available as per-vertex attributes.
167       */
168      if (exec->array.generic_array[0]->Enabled)
169	 inputs[0] = exec->array.generic_array[0];
170      else if (exec->array.legacy_array[0]->Enabled)
171	 inputs[0] = exec->array.legacy_array[0];
172      else
173	 inputs[0] = &vbo->legacy_currval[0];
174
175
176      for (i = 1; i <= VERT_ATTRIB_TEX7; i++) {
177	 if (exec->array.legacy_array[i]->Enabled)
178	    inputs[i] = exec->array.legacy_array[i];
179	 else
180	    inputs[i] = &vbo->legacy_currval[i];
181      }
182
183      for (i = 0; i < 16; i++) {
184	 if (exec->array.generic_array[0]->Enabled)
185	    inputs[VERT_ATTRIB_GENERIC0 + i] = exec->array.generic_array[i];
186	 else
187	    inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i];
188      }
189      break;
190   }
191}
192
193static void bind_arrays( GLcontext *ctx )
194{
195#if 0
196   if (ctx->Array.ArrayObj.Name != exec->array.array_obj) {
197      bind_array_obj(ctx);
198      recalculate_input_bindings(ctx);
199   }
200   else if (exec->array.program_mode != get_program_mode(ctx) ||
201	    exec->array.enabled_flags != ctx->Array.ArrayObj->_Enabled) {
202
203      recalculate_input_bindings(ctx);
204   }
205#else
206   bind_array_obj(ctx);
207   recalculate_input_bindings(ctx);
208#endif
209}
210
211
212
213/***********************************************************************
214 * API functions.
215 */
216
217static void GLAPIENTRY
218vbo_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
219{
220   GET_CURRENT_CONTEXT(ctx);
221   struct vbo_context *vbo = vbo_context(ctx);
222   struct vbo_exec_context *exec = &vbo->exec;
223   struct _mesa_prim prim[1];
224
225   if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
226      return;
227
228   FLUSH_CURRENT( ctx, 0 );
229
230   if (ctx->NewState)
231      _mesa_update_state( ctx );
232
233   bind_arrays( ctx );
234
235   prim[0].begin = 1;
236   prim[0].end = 1;
237   prim[0].weak = 0;
238   prim[0].pad = 0;
239
240   if (exec->array.inputs[0]->BufferObj->Name) {
241      /* Use vertex attribute as a hint to tell us if we expect all
242       * arrays to be in VBO's and if so, don't worry about avoiding
243       * the upload of elements < start.
244       */
245      prim[0].mode = mode;
246      prim[0].start = start;
247      prim[0].count = count;
248      prim[0].indexed = 0;
249
250      vbo->draw_prims( ctx, exec->array.inputs, prim, 1, NULL, 0, start + count );
251   }
252   else {
253      /* If not using VBO's, we don't want to upload any more elements
254       * than necessary from the arrays as they will not be valid next
255       * time the application tries to draw with them.
256       */
257      prim[0].mode = mode;
258      prim[0].start = 0;
259      prim[0].count = count;
260      prim[0].indexed = 0;
261
262      vbo->draw_prims( ctx, exec->array.inputs, prim, 1, NULL, start, start + count );
263   }
264}
265
266
267
268static void GLAPIENTRY
269vbo_exec_DrawRangeElements(GLenum mode,
270			   GLuint start, GLuint end,
271			   GLsizei count, GLenum type, const GLvoid *indices)
272{
273   GET_CURRENT_CONTEXT(ctx);
274   struct vbo_context *vbo = vbo_context(ctx);
275   struct vbo_exec_context *exec = &vbo->exec;
276   struct _mesa_index_buffer ib;
277   struct _mesa_prim prim[1];
278
279   if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count, type, indices ))
280      return;
281
282   FLUSH_CURRENT( ctx, 0 );
283
284   if (ctx->NewState)
285      _mesa_update_state( ctx );
286
287   ib.count = count;
288   ib.type = type;
289   ib.obj = ctx->Array.ElementArrayBufferObj;
290   ib.ptr = indices;
291
292   if (ctx->Array.ElementArrayBufferObj->Name) {
293      /* Use the fact that indices are in a VBO as a hint that the
294       * program has put all the arrays in VBO's and we don't have to
295       * worry about performance implications of start > 0.
296       *
297       * XXX: consider passing start as min_index to draw_prims instead.
298       */
299      ib.rebase = 0;
300   }
301   else {
302      ib.rebase = start;
303   }
304
305   prim[0].begin = 1;
306   prim[0].end = 1;
307   prim[0].weak = 0;
308   prim[0].pad = 0;
309   prim[0].mode = mode;
310   prim[0].start = 0;
311   prim[0].count = count;
312   prim[0].indexed = 1;
313
314   vbo->draw_prims( ctx, exec->array.inputs, prim, 1, &ib, ib.rebase, end+1 );
315}
316
317
318static void GLAPIENTRY
319vbo_exec_DrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
320{
321   GET_CURRENT_CONTEXT(ctx);
322   GLuint max_index;
323
324   if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
325      return;
326
327   if (ctx->Array.ElementArrayBufferObj->Name) {
328      const GLvoid *map = ctx->Driver.MapBuffer(ctx,
329						 GL_ELEMENT_ARRAY_BUFFER_ARB,
330						 GL_DYNAMIC_READ_ARB,
331						 ctx->Array.ElementArrayBufferObj);
332
333      max_index = get_max_index(count, type, ADD_POINTERS(map, indices));
334
335      ctx->Driver.UnmapBuffer(ctx,
336			      GL_ELEMENT_ARRAY_BUFFER_ARB,
337			      ctx->Array.ElementArrayBufferObj);
338   }
339   else {
340      max_index = get_max_index(count, type, indices);
341   }
342
343   vbo_exec_DrawRangeElements(mode, 0, max_index, count, type, indices);
344}
345
346
347/***********************************************************************
348 * Initialization
349 */
350
351
352
353
354void vbo_exec_array_init( struct vbo_exec_context *exec )
355{
356   GLcontext *ctx = exec->ctx;
357
358#if 1
359   exec->vtxfmt.DrawArrays = vbo_exec_DrawArrays;
360   exec->vtxfmt.DrawElements = vbo_exec_DrawElements;
361   exec->vtxfmt.DrawRangeElements = vbo_exec_DrawRangeElements;
362#else
363   exec->vtxfmt.DrawArrays = _mesa_noop_DrawArrays;
364   exec->vtxfmt.DrawElements = _mesa_noop_DrawElements;
365   exec->vtxfmt.DrawRangeElements = _mesa_noop_DrawRangeElements;
366#endif
367
368   exec->array.index_obj = ctx->Driver.NewBufferObject(ctx, 1, GL_ARRAY_BUFFER_ARB);
369}
370
371
372void vbo_exec_array_destroy( struct vbo_exec_context *exec )
373{
374   GLcontext *ctx = exec->ctx;
375
376   ctx->Driver.DeleteBuffer(ctx, exec->array.index_obj);
377}
378