st_draw.c revision a6860f0913d5d4be0c73da963e84e97fc926225f
1/**************************************************************************
2 *
3 * Copyright 2007 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/*
29 * This file implements the st_draw_vbo() function which is called from
30 * Mesa's VBO module.  All point/line/triangle rendering is done through
31 * this function whether the user called glBegin/End, glDrawArrays,
32 * glDrawElements, glEvalMesh, or glCalList, etc.
33 *
34 * We basically convert the VBO's vertex attribute/array information into
35 * Gallium vertex state, bind the vertex buffer objects and call
36 * pipe->draw_elements(), pipe->draw_range_elements() or pipe->draw_arrays().
37 *
38 * Authors:
39 *   Keith Whitwell <keith@tungstengraphics.com>
40 */
41
42
43#include "main/imports.h"
44#include "main/image.h"
45#include "main/macros.h"
46#include "main/mfeatures.h"
47#include "program/prog_uniform.h"
48
49#include "vbo/vbo.h"
50
51#include "st_context.h"
52#include "st_atom.h"
53#include "st_cb_bufferobjects.h"
54#include "st_draw.h"
55#include "st_program.h"
56
57#include "pipe/p_context.h"
58#include "pipe/p_defines.h"
59#include "util/u_inlines.h"
60#include "util/u_format.h"
61#include "util/u_prim.h"
62#include "util/u_draw_quad.h"
63#include "draw/draw_context.h"
64#include "cso_cache/cso_context.h"
65
66
67static GLuint double_types[4] = {
68   PIPE_FORMAT_R64_FLOAT,
69   PIPE_FORMAT_R64G64_FLOAT,
70   PIPE_FORMAT_R64G64B64_FLOAT,
71   PIPE_FORMAT_R64G64B64A64_FLOAT
72};
73
74static GLuint float_types[4] = {
75   PIPE_FORMAT_R32_FLOAT,
76   PIPE_FORMAT_R32G32_FLOAT,
77   PIPE_FORMAT_R32G32B32_FLOAT,
78   PIPE_FORMAT_R32G32B32A32_FLOAT
79};
80
81static GLuint half_float_types[4] = {
82   PIPE_FORMAT_R16_FLOAT,
83   PIPE_FORMAT_R16G16_FLOAT,
84   PIPE_FORMAT_R16G16B16_FLOAT,
85   PIPE_FORMAT_R16G16B16A16_FLOAT
86};
87
88static GLuint uint_types_norm[4] = {
89   PIPE_FORMAT_R32_UNORM,
90   PIPE_FORMAT_R32G32_UNORM,
91   PIPE_FORMAT_R32G32B32_UNORM,
92   PIPE_FORMAT_R32G32B32A32_UNORM
93};
94
95static GLuint uint_types_scale[4] = {
96   PIPE_FORMAT_R32_USCALED,
97   PIPE_FORMAT_R32G32_USCALED,
98   PIPE_FORMAT_R32G32B32_USCALED,
99   PIPE_FORMAT_R32G32B32A32_USCALED
100};
101
102static GLuint int_types_norm[4] = {
103   PIPE_FORMAT_R32_SNORM,
104   PIPE_FORMAT_R32G32_SNORM,
105   PIPE_FORMAT_R32G32B32_SNORM,
106   PIPE_FORMAT_R32G32B32A32_SNORM
107};
108
109static GLuint int_types_scale[4] = {
110   PIPE_FORMAT_R32_SSCALED,
111   PIPE_FORMAT_R32G32_SSCALED,
112   PIPE_FORMAT_R32G32B32_SSCALED,
113   PIPE_FORMAT_R32G32B32A32_SSCALED
114};
115
116static GLuint ushort_types_norm[4] = {
117   PIPE_FORMAT_R16_UNORM,
118   PIPE_FORMAT_R16G16_UNORM,
119   PIPE_FORMAT_R16G16B16_UNORM,
120   PIPE_FORMAT_R16G16B16A16_UNORM
121};
122
123static GLuint ushort_types_scale[4] = {
124   PIPE_FORMAT_R16_USCALED,
125   PIPE_FORMAT_R16G16_USCALED,
126   PIPE_FORMAT_R16G16B16_USCALED,
127   PIPE_FORMAT_R16G16B16A16_USCALED
128};
129
130static GLuint short_types_norm[4] = {
131   PIPE_FORMAT_R16_SNORM,
132   PIPE_FORMAT_R16G16_SNORM,
133   PIPE_FORMAT_R16G16B16_SNORM,
134   PIPE_FORMAT_R16G16B16A16_SNORM
135};
136
137static GLuint short_types_scale[4] = {
138   PIPE_FORMAT_R16_SSCALED,
139   PIPE_FORMAT_R16G16_SSCALED,
140   PIPE_FORMAT_R16G16B16_SSCALED,
141   PIPE_FORMAT_R16G16B16A16_SSCALED
142};
143
144static GLuint ubyte_types_norm[4] = {
145   PIPE_FORMAT_R8_UNORM,
146   PIPE_FORMAT_R8G8_UNORM,
147   PIPE_FORMAT_R8G8B8_UNORM,
148   PIPE_FORMAT_R8G8B8A8_UNORM
149};
150
151static GLuint ubyte_types_scale[4] = {
152   PIPE_FORMAT_R8_USCALED,
153   PIPE_FORMAT_R8G8_USCALED,
154   PIPE_FORMAT_R8G8B8_USCALED,
155   PIPE_FORMAT_R8G8B8A8_USCALED
156};
157
158static GLuint byte_types_norm[4] = {
159   PIPE_FORMAT_R8_SNORM,
160   PIPE_FORMAT_R8G8_SNORM,
161   PIPE_FORMAT_R8G8B8_SNORM,
162   PIPE_FORMAT_R8G8B8A8_SNORM
163};
164
165static GLuint byte_types_scale[4] = {
166   PIPE_FORMAT_R8_SSCALED,
167   PIPE_FORMAT_R8G8_SSCALED,
168   PIPE_FORMAT_R8G8B8_SSCALED,
169   PIPE_FORMAT_R8G8B8A8_SSCALED
170};
171
172static GLuint fixed_types[4] = {
173   PIPE_FORMAT_R32_FIXED,
174   PIPE_FORMAT_R32G32_FIXED,
175   PIPE_FORMAT_R32G32B32_FIXED,
176   PIPE_FORMAT_R32G32B32A32_FIXED
177};
178
179
180
181/**
182 * Return a PIPE_FORMAT_x for the given GL datatype and size.
183 */
184GLuint
185st_pipe_vertex_format(GLenum type, GLuint size, GLenum format,
186                      GLboolean normalized)
187{
188   assert((type >= GL_BYTE && type <= GL_DOUBLE) ||
189          type == GL_FIXED || type == GL_HALF_FLOAT);
190   assert(size >= 1);
191   assert(size <= 4);
192   assert(format == GL_RGBA || format == GL_BGRA);
193
194   if (format == GL_BGRA) {
195      /* this is an odd-ball case */
196      assert(type == GL_UNSIGNED_BYTE);
197      assert(normalized);
198      return PIPE_FORMAT_B8G8R8A8_UNORM;
199   }
200
201   if (normalized) {
202      switch (type) {
203      case GL_DOUBLE: return double_types[size-1];
204      case GL_FLOAT: return float_types[size-1];
205      case GL_HALF_FLOAT: return half_float_types[size-1];
206      case GL_INT: return int_types_norm[size-1];
207      case GL_SHORT: return short_types_norm[size-1];
208      case GL_BYTE: return byte_types_norm[size-1];
209      case GL_UNSIGNED_INT: return uint_types_norm[size-1];
210      case GL_UNSIGNED_SHORT: return ushort_types_norm[size-1];
211      case GL_UNSIGNED_BYTE: return ubyte_types_norm[size-1];
212      case GL_FIXED: return fixed_types[size-1];
213      default: assert(0); return 0;
214      }
215   }
216   else {
217      switch (type) {
218      case GL_DOUBLE: return double_types[size-1];
219      case GL_FLOAT: return float_types[size-1];
220      case GL_HALF_FLOAT: return half_float_types[size-1];
221      case GL_INT: return int_types_scale[size-1];
222      case GL_SHORT: return short_types_scale[size-1];
223      case GL_BYTE: return byte_types_scale[size-1];
224      case GL_UNSIGNED_INT: return uint_types_scale[size-1];
225      case GL_UNSIGNED_SHORT: return ushort_types_scale[size-1];
226      case GL_UNSIGNED_BYTE: return ubyte_types_scale[size-1];
227      case GL_FIXED: return fixed_types[size-1];
228      default: assert(0); return 0;
229      }
230   }
231   return 0; /* silence compiler warning */
232}
233
234
235
236
237
238/**
239 * Examine the active arrays to determine if we have interleaved
240 * vertex arrays all living in one VBO, or all living in user space.
241 * \param userSpace  returns whether the arrays are in user space.
242 */
243static GLboolean
244is_interleaved_arrays(const struct st_vertex_program *vp,
245                      const struct st_vp_variant *vpv,
246                      const struct gl_client_array **arrays,
247                      GLboolean *userSpace)
248{
249   GLuint attr;
250   const struct gl_buffer_object *firstBufObj = NULL;
251   GLint firstStride = -1;
252   GLuint num_client_arrays = 0;
253   const GLubyte *client_addr = NULL;
254
255   for (attr = 0; attr < vpv->num_inputs; attr++) {
256      const GLuint mesaAttr = vp->index_to_input[attr];
257      const struct gl_buffer_object *bufObj = arrays[mesaAttr]->BufferObj;
258      const GLsizei stride = arrays[mesaAttr]->StrideB; /* in bytes */
259
260      if (firstStride < 0) {
261         firstStride = stride;
262      }
263      else if (firstStride != stride) {
264         return GL_FALSE;
265      }
266
267      if (!bufObj || !bufObj->Name) {
268         num_client_arrays++;
269         /* Try to detect if the client-space arrays are
270          * "close" to each other.
271          */
272         if (!client_addr) {
273            client_addr = arrays[mesaAttr]->Ptr;
274         }
275         else if (abs(arrays[mesaAttr]->Ptr - client_addr) > firstStride) {
276            /* arrays start too far apart */
277            return GL_FALSE;
278         }
279      }
280      else if (!firstBufObj) {
281         firstBufObj = bufObj;
282      }
283      else if (bufObj != firstBufObj) {
284         return GL_FALSE;
285      }
286   }
287
288   *userSpace = (num_client_arrays == vpv->num_inputs);
289   /* debug_printf("user space: %s (%d arrays, %d inputs)\n",
290      (int)*userSpace ? "Yes" : "No", num_client_arrays, vp->num_inputs); */
291
292   return GL_TRUE;
293}
294
295
296/**
297 * Compute the memory range occupied by the arrays.
298 */
299static void
300get_arrays_bounds(const struct st_vertex_program *vp,
301                  const struct st_vp_variant *vpv,
302                  const struct gl_client_array **arrays,
303                  GLuint max_index,
304                  const GLubyte **low, const GLubyte **high)
305{
306   const GLubyte *low_addr = NULL;
307   const GLubyte *high_addr = NULL;
308   GLuint attr;
309
310   /* debug_printf("get_arrays_bounds: Handling %u attrs\n", vpv->num_inputs); */
311
312   for (attr = 0; attr < vpv->num_inputs; attr++) {
313      const GLuint mesaAttr = vp->index_to_input[attr];
314      const GLint stride = arrays[mesaAttr]->StrideB;
315      const GLubyte *start = arrays[mesaAttr]->Ptr;
316      const unsigned sz = (arrays[mesaAttr]->Size *
317                           _mesa_sizeof_type(arrays[mesaAttr]->Type));
318      const GLubyte *end = start + (max_index * stride) + sz;
319
320      /* debug_printf("attr %u: stride %d size %u start %p end %p\n",
321         attr, stride, sz, start, end); */
322
323      if (attr == 0) {
324         low_addr = start;
325         high_addr = end;
326      }
327      else {
328         low_addr = MIN2(low_addr, start);
329         high_addr = MAX2(high_addr, end);
330      }
331   }
332
333   *low = low_addr;
334   *high = high_addr;
335}
336
337
338/**
339 * Set up for drawing interleaved arrays that all live in one VBO
340 * or all live in user space.
341 * \param vbuffer  returns vertex buffer info
342 * \param velements  returns vertex element info
343 */
344static void
345setup_interleaved_attribs(struct gl_context *ctx,
346                          const struct st_vertex_program *vp,
347                          const struct st_vp_variant *vpv,
348                          const struct gl_client_array **arrays,
349                          GLuint max_index,
350                          GLboolean userSpace,
351                          struct pipe_vertex_buffer *vbuffer,
352                          struct pipe_vertex_element velements[])
353{
354   struct st_context *st = st_context(ctx);
355   struct pipe_context *pipe = st->pipe;
356   GLuint attr;
357   const GLubyte *offset0 = NULL;
358
359   for (attr = 0; attr < vpv->num_inputs; attr++) {
360      const GLuint mesaAttr = vp->index_to_input[attr];
361      struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
362      struct st_buffer_object *stobj = st_buffer_object(bufobj);
363      GLsizei stride = arrays[mesaAttr]->StrideB;
364
365      /*printf("stobj %u = %p\n", attr, (void*)stobj);*/
366
367      if (attr == 0) {
368         const GLubyte *low, *high;
369
370         get_arrays_bounds(vp, vpv, arrays, max_index, &low, &high);
371         /* debug_printf("buffer range: %p %p range %d max index %u\n",
372            low, high, high - low, max_index); */
373
374         offset0 = low;
375         if (userSpace) {
376            vbuffer->buffer =
377               pipe_user_buffer_create(pipe->screen, (void *) low, high - low,
378				       PIPE_BIND_VERTEX_BUFFER);
379            vbuffer->buffer_offset = 0;
380         }
381         else {
382            vbuffer->buffer = NULL;
383            pipe_resource_reference(&vbuffer->buffer, stobj->buffer);
384            vbuffer->buffer_offset = pointer_to_offset(low);
385         }
386         vbuffer->stride = stride; /* in bytes */
387         vbuffer->max_index = max_index;
388      }
389
390      /*
391      if (arrays[mesaAttr]->InstanceDivisor)
392         vbuffer[attr].max_index = arrays[mesaAttr]->_MaxElement;
393      else
394         vbuffer[attr].max_index = max_index;
395      */
396
397      velements[attr].src_offset =
398         (unsigned) (arrays[mesaAttr]->Ptr - offset0);
399      velements[attr].instance_divisor = arrays[mesaAttr]->InstanceDivisor;
400      velements[attr].vertex_buffer_index = 0;
401      velements[attr].src_format =
402         st_pipe_vertex_format(arrays[mesaAttr]->Type,
403                               arrays[mesaAttr]->Size,
404                               arrays[mesaAttr]->Format,
405                               arrays[mesaAttr]->Normalized);
406      assert(velements[attr].src_format);
407   }
408}
409
410
411/**
412 * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
413 * vertex attribute.
414 * \param vbuffer  returns vertex buffer info
415 * \param velements  returns vertex element info
416 */
417static void
418setup_non_interleaved_attribs(struct gl_context *ctx,
419                              const struct st_vertex_program *vp,
420                              const struct st_vp_variant *vpv,
421                              const struct gl_client_array **arrays,
422                              GLuint max_index,
423                              GLboolean *userSpace,
424                              struct pipe_vertex_buffer vbuffer[],
425                              struct pipe_vertex_element velements[])
426{
427   struct st_context *st = st_context(ctx);
428   struct pipe_context *pipe = st->pipe;
429   GLuint attr;
430
431   for (attr = 0; attr < vpv->num_inputs; attr++) {
432      const GLuint mesaAttr = vp->index_to_input[attr];
433      struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
434      GLsizei stride = arrays[mesaAttr]->StrideB;
435
436      *userSpace = GL_FALSE;
437
438      if (bufobj && bufobj->Name) {
439         /* Attribute data is in a VBO.
440          * Recall that for VBOs, the gl_client_array->Ptr field is
441          * really an offset from the start of the VBO, not a pointer.
442          */
443         struct st_buffer_object *stobj = st_buffer_object(bufobj);
444         assert(stobj->buffer);
445         /*printf("stobj %u = %p\n", attr, (void*) stobj);*/
446
447         vbuffer[attr].buffer = NULL;
448         pipe_resource_reference(&vbuffer[attr].buffer, stobj->buffer);
449         vbuffer[attr].buffer_offset = pointer_to_offset(arrays[mesaAttr]->Ptr);
450      }
451      else {
452         /* attribute data is in user-space memory, not a VBO */
453         uint bytes;
454         /*printf("user-space array %d stride %d\n", attr, stride);*/
455
456         *userSpace = GL_TRUE;
457
458         /* wrap user data */
459         if (arrays[mesaAttr]->Ptr) {
460            /* user's vertex array */
461            if (arrays[mesaAttr]->StrideB) {
462               bytes = arrays[mesaAttr]->StrideB * (max_index + 1);
463            }
464            else {
465               bytes = arrays[mesaAttr]->Size
466                  * _mesa_sizeof_type(arrays[mesaAttr]->Type);
467            }
468            vbuffer[attr].buffer =
469	       pipe_user_buffer_create(pipe->screen,
470				       (void *) arrays[mesaAttr]->Ptr, bytes,
471				       PIPE_BIND_VERTEX_BUFFER);
472         }
473         else {
474            /* no array, use ctx->Current.Attrib[] value */
475            bytes = sizeof(ctx->Current.Attrib[0]);
476            vbuffer[attr].buffer =
477	       pipe_user_buffer_create(pipe->screen,
478				       (void *) ctx->Current.Attrib[mesaAttr],
479				       bytes,
480				       PIPE_BIND_VERTEX_BUFFER);
481            stride = 0;
482         }
483
484         vbuffer[attr].buffer_offset = 0;
485      }
486
487      assert(velements[attr].src_offset <= 2048); /* 11-bit field */
488
489      /* common-case setup */
490      vbuffer[attr].stride = stride; /* in bytes */
491      if (arrays[mesaAttr]->InstanceDivisor)
492         vbuffer[attr].max_index = arrays[mesaAttr]->_MaxElement;
493      else
494         vbuffer[attr].max_index = max_index;
495
496      velements[attr].src_offset = 0;
497      velements[attr].instance_divisor = arrays[mesaAttr]->InstanceDivisor;
498      velements[attr].vertex_buffer_index = attr;
499      velements[attr].src_format
500         = st_pipe_vertex_format(arrays[mesaAttr]->Type,
501                                 arrays[mesaAttr]->Size,
502                                 arrays[mesaAttr]->Format,
503                                 arrays[mesaAttr]->Normalized);
504      assert(velements[attr].src_format);
505   }
506}
507
508
509static void
510setup_index_buffer(struct gl_context *ctx,
511                   const struct _mesa_index_buffer *ib,
512                   struct pipe_index_buffer *ibuffer)
513{
514   struct st_context *st = st_context(ctx);
515   struct pipe_context *pipe = st->pipe;
516
517   memset(ibuffer, 0, sizeof(*ibuffer));
518   if (ib) {
519      struct gl_buffer_object *bufobj = ib->obj;
520
521      switch (ib->type) {
522      case GL_UNSIGNED_INT:
523         ibuffer->index_size = 4;
524         break;
525      case GL_UNSIGNED_SHORT:
526         ibuffer->index_size = 2;
527         break;
528      case GL_UNSIGNED_BYTE:
529         ibuffer->index_size = 1;
530         break;
531      default:
532         assert(0);
533	 return;
534      }
535
536      /* get/create the index buffer object */
537      if (bufobj && bufobj->Name) {
538         /* elements/indexes are in a real VBO */
539         struct st_buffer_object *stobj = st_buffer_object(bufobj);
540         pipe_resource_reference(&ibuffer->buffer, stobj->buffer);
541         ibuffer->offset = pointer_to_offset(ib->ptr);
542      }
543      else {
544         /* element/indicies are in user space memory */
545         ibuffer->buffer =
546            pipe_user_buffer_create(pipe->screen, (void *) ib->ptr,
547                                    ib->count * ibuffer->index_size,
548                                    PIPE_BIND_INDEX_BUFFER);
549      }
550   }
551}
552
553/**
554 * Prior to drawing, check that any uniforms referenced by the
555 * current shader have been set.  If a uniform has not been set,
556 * issue a warning.
557 */
558static void
559check_uniforms(struct gl_context *ctx)
560{
561   struct gl_shader_program *shProg[3] = {
562      ctx->Shader.CurrentVertexProgram,
563      ctx->Shader.CurrentGeometryProgram,
564      ctx->Shader.CurrentFragmentProgram,
565   };
566   unsigned j;
567
568   for (j = 0; j < 3; j++) {
569      unsigned i;
570
571      if (shProg[j] == NULL || !shProg[j]->LinkStatus)
572	 continue;
573
574      for (i = 0; i < shProg[j]->Uniforms->NumUniforms; i++) {
575         const struct gl_uniform *u = &shProg[j]->Uniforms->Uniforms[i];
576         if (!u->Initialized) {
577            _mesa_warning(ctx,
578                          "Using shader with uninitialized uniform: %s",
579                          u->Name);
580         }
581      }
582   }
583}
584
585
586/**
587 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
588 * the corresponding Gallium type.
589 */
590static unsigned
591translate_prim(const struct gl_context *ctx, unsigned prim)
592{
593   /* GL prims should match Gallium prims, spot-check a few */
594   assert(GL_POINTS == PIPE_PRIM_POINTS);
595   assert(GL_QUADS == PIPE_PRIM_QUADS);
596   assert(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
597
598   /* Avoid quadstrips if it's easy to do so:
599    * Note: it's imporant to do the correct trimming if we change the prim type!
600    * We do that wherever this function is called.
601    */
602   if (prim == GL_QUAD_STRIP &&
603       ctx->Light.ShadeModel != GL_FLAT &&
604       ctx->Polygon.FrontMode == GL_FILL &&
605       ctx->Polygon.BackMode == GL_FILL)
606      prim = GL_TRIANGLE_STRIP;
607
608   return prim;
609}
610
611
612
613/**
614 * This function gets plugged into the VBO module and is called when
615 * we have something to render.
616 * Basically, translate the information into the format expected by gallium.
617 */
618void
619st_draw_vbo(struct gl_context *ctx,
620            const struct gl_client_array **arrays,
621            const struct _mesa_prim *prims,
622            GLuint nr_prims,
623            const struct _mesa_index_buffer *ib,
624	    GLboolean index_bounds_valid,
625            GLuint min_index,
626            GLuint max_index)
627{
628   struct st_context *st = st_context(ctx);
629   struct pipe_context *pipe = st->pipe;
630   const struct st_vertex_program *vp;
631   const struct st_vp_variant *vpv;
632   struct pipe_vertex_buffer vbuffer[PIPE_MAX_SHADER_INPUTS];
633   GLuint attr;
634   struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
635   unsigned num_vbuffers, num_velements;
636   struct pipe_index_buffer ibuffer;
637   GLboolean userSpace = GL_FALSE;
638   GLboolean vertDataEdgeFlags;
639   struct pipe_draw_info info;
640   unsigned i;
641
642   /* Mesa core state should have been validated already */
643   assert(ctx->NewState == 0x0);
644
645   /* Gallium probably doesn't want this in some cases. */
646   if (!index_bounds_valid)
647      if (!vbo_all_varyings_in_vbos(arrays))
648	 vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
649
650   /* sanity check for pointer arithmetic below */
651   assert(sizeof(arrays[0]->Ptr[0]) == 1);
652
653   vertDataEdgeFlags = arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj &&
654                       arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj->Name;
655   if (vertDataEdgeFlags != st->vertdata_edgeflags) {
656      st->vertdata_edgeflags = vertDataEdgeFlags;
657      st->dirty.st |= ST_NEW_EDGEFLAGS_DATA;
658   }
659
660   st_validate_state(st);
661
662   /* must get these after state validation! */
663   vp = st->vp;
664   vpv = st->vp_variant;
665
666#if 0
667   if (MESA_VERBOSE & VERBOSE_GLSL) {
668      check_uniforms(ctx);
669   }
670#else
671   (void) check_uniforms;
672#endif
673
674   memset(velements, 0, sizeof(struct pipe_vertex_element) * vpv->num_inputs);
675   /*
676    * Setup the vbuffer[] and velements[] arrays.
677    */
678   if (is_interleaved_arrays(vp, vpv, arrays, &userSpace)) {
679      /*printf("Draw interleaved\n");*/
680      setup_interleaved_attribs(ctx, vp, vpv, arrays, max_index, userSpace,
681                                vbuffer, velements);
682      num_vbuffers = 1;
683      num_velements = vpv->num_inputs;
684      if (num_velements == 0)
685         num_vbuffers = 0;
686   }
687   else {
688      /*printf("Draw non-interleaved\n");*/
689      setup_non_interleaved_attribs(ctx, vp, vpv, arrays, max_index,
690                                    &userSpace, vbuffer, velements);
691      num_vbuffers = vpv->num_inputs;
692      num_velements = vpv->num_inputs;
693   }
694
695#if 0
696   {
697      GLuint i;
698      for (i = 0; i < num_vbuffers; i++) {
699         printf("buffers[%d].stride = %u\n", i, vbuffer[i].stride);
700         printf("buffers[%d].max_index = %u\n", i, vbuffer[i].max_index);
701         printf("buffers[%d].buffer_offset = %u\n", i, vbuffer[i].buffer_offset);
702         printf("buffers[%d].buffer = %p\n", i, (void*) vbuffer[i].buffer);
703      }
704      for (i = 0; i < num_velements; i++) {
705         printf("vlements[%d].vbuffer_index = %u\n", i, velements[i].vertex_buffer_index);
706         printf("vlements[%d].src_offset = %u\n", i, velements[i].src_offset);
707         printf("vlements[%d].format = %s\n", i, util_format_name(velements[i].src_format));
708      }
709   }
710#endif
711
712   pipe->set_vertex_buffers(pipe, num_vbuffers, vbuffer);
713   cso_set_vertex_elements(st->cso_context, num_velements, velements);
714
715   setup_index_buffer(ctx, ib, &ibuffer);
716   pipe->set_index_buffer(pipe, &ibuffer);
717
718   util_draw_init_info(&info);
719   if (ib) {
720      info.indexed = TRUE;
721      if (min_index != ~0 && max_index != ~0) {
722         info.min_index = min_index;
723         info.max_index = max_index;
724      }
725   }
726
727   info.primitive_restart = st->ctx->Array.PrimitiveRestart;
728   info.restart_index = st->ctx->Array.RestartIndex;
729
730   /* do actual drawing */
731   for (i = 0; i < nr_prims; i++) {
732      info.mode = translate_prim( ctx, prims[i].mode );
733      info.start = prims[i].start;
734      info.count = prims[i].count;
735      info.instance_count = prims[i].num_instances;
736      info.index_bias = prims[i].basevertex;
737      if (!ib) {
738         info.min_index = info.start;
739         info.max_index = info.start + info.count - 1;
740      }
741
742      if (u_trim_pipe_prim(info.mode, &info.count))
743         pipe->draw_vbo(pipe, &info);
744   }
745
746   pipe_resource_reference(&ibuffer.buffer, NULL);
747
748   /* unreference buffers (frees wrapped user-space buffer objects) */
749   for (attr = 0; attr < num_vbuffers; attr++) {
750      pipe_resource_reference(&vbuffer[attr].buffer, NULL);
751      assert(!vbuffer[attr].buffer);
752   }
753
754   if (userSpace)
755   {
756      pipe->set_vertex_buffers(pipe, 0, NULL);
757   }
758}
759
760
761void st_init_draw( struct st_context *st )
762{
763   struct gl_context *ctx = st->ctx;
764
765   vbo_set_draw_func(ctx, st_draw_vbo);
766
767#if FEATURE_feedback || FEATURE_rastpos
768   st->draw = draw_create(st->pipe); /* for selection/feedback */
769
770   /* Disable draw options that might convert points/lines to tris, etc.
771    * as that would foul-up feedback/selection mode.
772    */
773   draw_wide_line_threshold(st->draw, 1000.0f);
774   draw_wide_point_threshold(st->draw, 1000.0f);
775   draw_enable_line_stipple(st->draw, FALSE);
776   draw_enable_point_sprites(st->draw, FALSE);
777#endif
778}
779
780
781void st_destroy_draw( struct st_context *st )
782{
783#if FEATURE_feedback || FEATURE_rastpos
784   draw_destroy(st->draw);
785#endif
786}
787
788
789