1a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw
2a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw/*
3a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * Mesa 3-D graphics library
4a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * Version:  6.5
5a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw *
6a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
7a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw *
8a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * Permission is hereby granted, free of charge, to any person obtaining a
9a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * copy of this software and associated documentation files (the "Software"),
10a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * to deal in the Software without restriction, including without limitation
11a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * and/or sell copies of the Software, and to permit persons to whom the
13a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * Software is furnished to do so, subject to the following conditions:
14a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw *
15a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * The above copyright notice and this permission notice shall be included
16a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * in all copies or substantial portions of the Software.
17a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw *
18a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw *
25a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * Authors:
26a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw *    Keith Whitwell <keith@tungstengraphics.com>
27a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw */
28a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw
29a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw/* Deal with hardware and/or swtnl maximums:
30a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * - maximum number of vertices in buffer
31a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * - maximum number of elements (maybe zero)
32a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw *
33a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * The maximums may vary with opengl state (eg if a larger hardware
34a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * vertex is required in this state, the maximum number of vertices
35a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * may be smaller than in another state).
36a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw *
37a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * We want buffer splitting to be a convenience function for the code
38a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * actually drawing the primitives rather than a system-wide maximum,
39a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * otherwise it is hard to avoid pessimism.
40a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw *
41a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * For instance, if a driver has no hardware limits on vertex buffer
42a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * dimensions, it would not ordinarily want to split vbos.  But if
43a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * there is an unexpected fallback, eg memory manager fails to upload
44a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * textures, it will want to pass the drawing commands onto swtnl,
45a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * which does have limitations.  A convenience function allows swtnl
46a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * to split the drawing and vbos internally without imposing its
47a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * limitations on drivers which want to use it as a fallback path.
48a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw */
49a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw
50c223c6b663cd5db39ba19c2be74b88cc3b8f53f3Brian#include "main/glheader.h"
51c223c6b663cd5db39ba19c2be74b88cc3b8f53f3Brian#include "main/imports.h"
52c223c6b663cd5db39ba19c2be74b88cc3b8f53f3Brian#include "main/mtypes.h"
5392d7ed8a20d4a018ce5324e6537ae7b478b9e5bfEric Anholt#include "main/macros.h"
54a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw
55a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw#include "vbo_split.h"
56a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw#include "vbo.h"
57a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw
58a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw/* True if a primitive can be split without copying of vertices, false
59a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw * otherwise.
60a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw */
61a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithwGLboolean split_prim_inplace(GLenum mode, GLuint *first, GLuint *incr)
62a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw{
63a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   switch (mode) {
64a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   case GL_POINTS:
65a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *first = 1;
66a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *incr = 1;
67a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      return GL_TRUE;
68a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   case GL_LINES:
69a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *first = 2;
70a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *incr = 2;
71a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      return GL_TRUE;
72a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   case GL_LINE_STRIP:
73a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *first = 2;
74a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *incr = 1;
75a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      return GL_TRUE;
76a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   case GL_TRIANGLES:
77a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *first = 3;
78a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *incr = 3;
79a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      return GL_TRUE;
80a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   case GL_TRIANGLE_STRIP:
81a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *first = 3;
82a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *incr = 1;
83a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      return GL_TRUE;
84a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   case GL_QUADS:
85a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *first = 4;
86a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *incr = 4;
87a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      return GL_TRUE;
88a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   case GL_QUAD_STRIP:
89a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *first = 4;
90a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *incr = 2;
91a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      return GL_TRUE;
92a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   default:
93a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *first = 0;
94a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      *incr = 1;		/* so that count % incr works */
95a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      return GL_FALSE;
96a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   }
97a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw}
98a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw
99a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw
100a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw
101f9995b30756140724f41daf963fa06167912be7fKristian Høgsbergvoid vbo_split_prims( struct gl_context *ctx,
102a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw		      const struct gl_client_array *arrays[],
103a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw		      const struct _mesa_prim *prim,
104a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw		      GLuint nr_prims,
105a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw		      const struct _mesa_index_buffer *ib,
106a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw		      GLuint min_index,
107a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw		      GLuint max_index,
108a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw		      vbo_draw_func draw,
109a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw		      const struct split_limits *limits )
110a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw{
111dd89ac249c56d04bbc23ecd9877426af9f09269cBrian Paul   GLint max_basevertex = prim->basevertex;
11292d7ed8a20d4a018ce5324e6537ae7b478b9e5bfEric Anholt   GLuint i;
11392d7ed8a20d4a018ce5324e6537ae7b478b9e5bfEric Anholt
11492d7ed8a20d4a018ce5324e6537ae7b478b9e5bfEric Anholt   for (i = 1; i < nr_prims; i++)
11592d7ed8a20d4a018ce5324e6537ae7b478b9e5bfEric Anholt      max_basevertex = MAX2(max_basevertex, prim[i].basevertex);
11692d7ed8a20d4a018ce5324e6537ae7b478b9e5bfEric Anholt
117dd89ac249c56d04bbc23ecd9877426af9f09269cBrian Paul   /* XXX max_basevertex is computed but not used, why? */
118dd89ac249c56d04bbc23ecd9877426af9f09269cBrian Paul
119a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   if (ib) {
120a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      if (limits->max_indices == 0) {
121a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	 /* Could traverse the indices, re-emitting vertices in turn.
122a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  * But it's hard to see why this case would be needed - for
123a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  * software tnl, it is better to convert to non-indexed
1246118bbd0a67773d9247f3252a0643657bbd898a0Ian Romanick	  * rendering after transformation is complete.  Are there any devices
125a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  * with hardware tnl that cannot do indexed rendering?
126a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  *
127a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  * For now, this path is disabled.
128a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  */
129a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	 assert(0);
130a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      }
131feeed10dfd1495eb15924868fec3b795fb4b1ba3Aapo Tahkola      else if (max_index - min_index >= limits->max_verts) {
132a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	 /* The vertex buffers are too large for hardware (or the
133a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  * swtnl module).  Traverse the indices, re-emitting vertices
134a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  * in turn.  Use a vertex cache to preserve some of the
135a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  * sharing from the original index list.
136a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  */
137a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	 vbo_split_copy(ctx, arrays, prim, nr_prims, ib,
138a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw			draw, limits );
139a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      }
140a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      else if (ib->count > limits->max_indices) {
141a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	 /* The index buffer is too large for hardware.  Try to split
142a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  * on whole-primitive boundaries, otherwise try to split the
143a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  * individual primitives.
144a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  */
145a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	 vbo_split_inplace(ctx, arrays, prim, nr_prims, ib,
146a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw			   min_index, max_index, draw, limits );
147a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      }
148a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      else {
149a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	 /* Why were we called? */
150a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	 assert(0);
151a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      }
152a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   }
153a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   else {
154a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      if (max_index - min_index >= limits->max_verts) {
155a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	 /* The vertex buffer is too large for hardware (or the swtnl
156a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  * module).  Try to split on whole-primitive boundaries,
157a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  * otherwise try to split the individual primitives.
158a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	  */
159a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	 vbo_split_inplace(ctx, arrays, prim, nr_prims, ib,
160a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw			   min_index, max_index, draw, limits );
161a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      }
162a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      else {
163a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	 /* Why were we called? */
164a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw	 assert(0);
165a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw      }
166a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw   }
167a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw}
168a38cb37913b7cd2f9c3c7aa11dcbb9485d623924keithw
169