st_draw.c revision bce4f9ac395986ee0acae2702ed73448333d81b8
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{
248   GLuint attr;
249   const struct gl_buffer_object *firstBufObj = NULL;
250   GLint firstStride = -1;
251   const GLubyte *client_addr = NULL;
252   GLboolean user_memory;
253
254   for (attr = 0; attr < vpv->num_inputs; attr++) {
255      const GLuint mesaAttr = vp->index_to_input[attr];
256      const struct gl_buffer_object *bufObj = arrays[mesaAttr]->BufferObj;
257      const GLsizei stride = arrays[mesaAttr]->StrideB; /* in bytes */
258
259      if (firstStride < 0) {
260         firstStride = stride;
261         user_memory = !bufObj || !bufObj->Name;
262      }
263      else if (firstStride != stride) {
264         return GL_FALSE;
265      }
266
267      if (!bufObj || !bufObj->Name) {
268         /* Try to detect if the client-space arrays are
269          * "close" to each other.
270          */
271         if (!user_memory) {
272            return GL_FALSE;
273         }
274         if (!client_addr) {
275            client_addr = arrays[mesaAttr]->Ptr;
276         }
277         else if (abs(arrays[mesaAttr]->Ptr - client_addr) > firstStride) {
278            /* arrays start too far apart */
279            return GL_FALSE;
280         }
281      }
282      else if (!firstBufObj) {
283         if (user_memory) {
284            return GL_FALSE;
285         }
286         firstBufObj = bufObj;
287      }
288      else if (bufObj != firstBufObj) {
289         return GL_FALSE;
290      }
291   }
292
293   return GL_TRUE;
294}
295
296
297/**
298 * Set up for drawing interleaved arrays that all live in one VBO
299 * or all live in user space.
300 * \param vbuffer  returns vertex buffer info
301 * \param velements  returns vertex element info
302 */
303static void
304setup_interleaved_attribs(struct gl_context *ctx,
305                          const struct st_vertex_program *vp,
306                          const struct st_vp_variant *vpv,
307                          const struct gl_client_array **arrays,
308                          struct pipe_vertex_buffer *vbuffer,
309                          struct pipe_vertex_element velements[],
310                          unsigned max_index)
311{
312   struct st_context *st = st_context(ctx);
313   struct pipe_context *pipe = st->pipe;
314   GLuint attr;
315   const GLubyte *low_addr = NULL;
316
317   /* Find the lowest address. */
318   if(vpv->num_inputs) {
319      low_addr = arrays[vp->index_to_input[0]]->Ptr;
320
321      for (attr = 1; attr < vpv->num_inputs; attr++) {
322         const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr;
323         low_addr = MIN2(low_addr, start);
324      }
325   }
326
327   for (attr = 0; attr < vpv->num_inputs; attr++) {
328      const GLuint mesaAttr = vp->index_to_input[attr];
329      struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
330      struct st_buffer_object *stobj = st_buffer_object(bufobj);
331      GLsizei stride = arrays[mesaAttr]->StrideB;
332
333      if (attr == 0) {
334         if (bufobj && bufobj->Name) {
335            vbuffer->buffer = NULL;
336            pipe_resource_reference(&vbuffer->buffer, stobj->buffer);
337            vbuffer->buffer_offset = pointer_to_offset(low_addr);
338         } else {
339            vbuffer->buffer =
340               pipe_user_buffer_create(pipe->screen, (void*)low_addr,
341                                       stride * (max_index + 1),
342				       PIPE_BIND_VERTEX_BUFFER);
343            vbuffer->buffer_offset = 0;
344
345            /* Track user vertex buffers. */
346            pipe_resource_reference(&st->user_vb[0], vbuffer->buffer);
347            st->user_vb_stride[0] = stride;
348            st->num_user_vbs = 1;
349         }
350         vbuffer->stride = stride; /* in bytes */
351      }
352
353      velements[attr].src_offset =
354         (unsigned) (arrays[mesaAttr]->Ptr - low_addr);
355      velements[attr].instance_divisor = arrays[mesaAttr]->InstanceDivisor;
356      velements[attr].vertex_buffer_index = 0;
357      velements[attr].src_format =
358         st_pipe_vertex_format(arrays[mesaAttr]->Type,
359                               arrays[mesaAttr]->Size,
360                               arrays[mesaAttr]->Format,
361                               arrays[mesaAttr]->Normalized);
362      assert(velements[attr].src_format);
363   }
364}
365
366
367/**
368 * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
369 * vertex attribute.
370 * \param vbuffer  returns vertex buffer info
371 * \param velements  returns vertex element info
372 */
373static void
374setup_non_interleaved_attribs(struct gl_context *ctx,
375                              const struct st_vertex_program *vp,
376                              const struct st_vp_variant *vpv,
377                              const struct gl_client_array **arrays,
378                              struct pipe_vertex_buffer vbuffer[],
379                              struct pipe_vertex_element velements[],
380                              unsigned max_index)
381{
382   struct st_context *st = st_context(ctx);
383   struct pipe_context *pipe = st->pipe;
384   GLuint attr;
385
386   for (attr = 0; attr < vpv->num_inputs; attr++) {
387      const GLuint mesaAttr = vp->index_to_input[attr];
388      struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
389      GLsizei stride = arrays[mesaAttr]->StrideB;
390
391      if (bufobj && bufobj->Name) {
392         /* Attribute data is in a VBO.
393          * Recall that for VBOs, the gl_client_array->Ptr field is
394          * really an offset from the start of the VBO, not a pointer.
395          */
396         struct st_buffer_object *stobj = st_buffer_object(bufobj);
397         assert(stobj->buffer);
398
399         vbuffer[attr].buffer = NULL;
400         pipe_resource_reference(&vbuffer[attr].buffer, stobj->buffer);
401         vbuffer[attr].buffer_offset = pointer_to_offset(arrays[mesaAttr]->Ptr);
402      }
403      else {
404         /* wrap user data */
405         if (arrays[mesaAttr]->Ptr) {
406            vbuffer[attr].buffer =
407	       pipe_user_buffer_create(pipe->screen,
408				       (void *) arrays[mesaAttr]->Ptr,
409				       stride * (max_index + 1),
410				       PIPE_BIND_VERTEX_BUFFER);
411         }
412         else {
413            /* no array, use ctx->Current.Attrib[] value */
414            uint bytes = sizeof(ctx->Current.Attrib[0]);
415            vbuffer[attr].buffer =
416	       pipe_user_buffer_create(pipe->screen,
417				       (void *) ctx->Current.Attrib[mesaAttr],
418				       bytes,
419				       PIPE_BIND_VERTEX_BUFFER);
420            stride = 0;
421         }
422
423         vbuffer[attr].buffer_offset = 0;
424
425         /* Track user vertex buffers. */
426         pipe_resource_reference(&st->user_vb[attr], vbuffer->buffer);
427         st->user_vb_stride[attr] = stride;
428         st->num_user_vbs = MAX2(st->num_user_vbs, attr+1);
429      }
430
431      /* common-case setup */
432      vbuffer[attr].stride = stride; /* in bytes */
433
434      velements[attr].src_offset = 0;
435      velements[attr].instance_divisor = arrays[mesaAttr]->InstanceDivisor;
436      velements[attr].vertex_buffer_index = attr;
437      velements[attr].src_format
438         = st_pipe_vertex_format(arrays[mesaAttr]->Type,
439                                 arrays[mesaAttr]->Size,
440                                 arrays[mesaAttr]->Format,
441                                 arrays[mesaAttr]->Normalized);
442      assert(velements[attr].src_format);
443   }
444}
445
446
447static void
448setup_index_buffer(struct gl_context *ctx,
449                   const struct _mesa_index_buffer *ib,
450                   struct pipe_index_buffer *ibuffer)
451{
452   struct st_context *st = st_context(ctx);
453   struct pipe_context *pipe = st->pipe;
454
455   memset(ibuffer, 0, sizeof(*ibuffer));
456   if (ib) {
457      struct gl_buffer_object *bufobj = ib->obj;
458
459      switch (ib->type) {
460      case GL_UNSIGNED_INT:
461         ibuffer->index_size = 4;
462         break;
463      case GL_UNSIGNED_SHORT:
464         ibuffer->index_size = 2;
465         break;
466      case GL_UNSIGNED_BYTE:
467         ibuffer->index_size = 1;
468         break;
469      default:
470         assert(0);
471	 return;
472      }
473
474      /* get/create the index buffer object */
475      if (bufobj && bufobj->Name) {
476         /* elements/indexes are in a real VBO */
477         struct st_buffer_object *stobj = st_buffer_object(bufobj);
478         pipe_resource_reference(&ibuffer->buffer, stobj->buffer);
479         ibuffer->offset = pointer_to_offset(ib->ptr);
480      }
481      else {
482         /* element/indicies are in user space memory */
483         ibuffer->buffer =
484            pipe_user_buffer_create(pipe->screen, (void *) ib->ptr,
485                                    ib->count * ibuffer->index_size,
486                                    PIPE_BIND_INDEX_BUFFER);
487      }
488   }
489}
490
491/**
492 * Prior to drawing, check that any uniforms referenced by the
493 * current shader have been set.  If a uniform has not been set,
494 * issue a warning.
495 */
496static void
497check_uniforms(struct gl_context *ctx)
498{
499   struct gl_shader_program *shProg[3] = {
500      ctx->Shader.CurrentVertexProgram,
501      ctx->Shader.CurrentGeometryProgram,
502      ctx->Shader.CurrentFragmentProgram,
503   };
504   unsigned j;
505
506   for (j = 0; j < 3; j++) {
507      unsigned i;
508
509      if (shProg[j] == NULL || !shProg[j]->LinkStatus)
510	 continue;
511
512      for (i = 0; i < shProg[j]->Uniforms->NumUniforms; i++) {
513         const struct gl_uniform *u = &shProg[j]->Uniforms->Uniforms[i];
514         if (!u->Initialized) {
515            _mesa_warning(ctx,
516                          "Using shader with uninitialized uniform: %s",
517                          u->Name);
518         }
519      }
520   }
521}
522
523
524/**
525 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
526 * the corresponding Gallium type.
527 */
528static unsigned
529translate_prim(const struct gl_context *ctx, unsigned prim)
530{
531   /* GL prims should match Gallium prims, spot-check a few */
532   assert(GL_POINTS == PIPE_PRIM_POINTS);
533   assert(GL_QUADS == PIPE_PRIM_QUADS);
534   assert(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
535
536   /* Avoid quadstrips if it's easy to do so:
537    * Note: it's imporant to do the correct trimming if we change the prim type!
538    * We do that wherever this function is called.
539    */
540   if (prim == GL_QUAD_STRIP &&
541       ctx->Light.ShadeModel != GL_FLAT &&
542       ctx->Polygon.FrontMode == GL_FILL &&
543       ctx->Polygon.BackMode == GL_FILL)
544      prim = GL_TRIANGLE_STRIP;
545
546   return prim;
547}
548
549
550static void
551st_validate_varrays(struct gl_context *ctx,
552                    const struct gl_client_array **arrays,
553                    unsigned max_index)
554{
555   struct st_context *st = st_context(ctx);
556   const struct st_vertex_program *vp;
557   const struct st_vp_variant *vpv;
558   struct pipe_vertex_buffer vbuffer[PIPE_MAX_SHADER_INPUTS];
559   struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
560   unsigned num_vbuffers, num_velements;
561   GLuint attr;
562   unsigned i;
563
564   /* must get these after state validation! */
565   vp = st->vp;
566   vpv = st->vp_variant;
567
568   memset(velements, 0, sizeof(struct pipe_vertex_element) * vpv->num_inputs);
569
570   /* Unreference any user vertex buffers. */
571   for (i = 0; i < st->num_user_vbs; i++) {
572      pipe_resource_reference(&st->user_vb[i], NULL);
573   }
574   st->num_user_vbs = 0;
575
576   /*
577    * Setup the vbuffer[] and velements[] arrays.
578    */
579   if (is_interleaved_arrays(vp, vpv, arrays)) {
580      setup_interleaved_attribs(ctx, vp, vpv, arrays, vbuffer, velements,
581                                max_index);
582
583      num_vbuffers = 1;
584      num_velements = vpv->num_inputs;
585      if (num_velements == 0)
586         num_vbuffers = 0;
587   }
588   else {
589      setup_non_interleaved_attribs(ctx, vp, vpv, arrays,
590                                    vbuffer, velements, max_index);
591      num_vbuffers = vpv->num_inputs;
592      num_velements = vpv->num_inputs;
593   }
594
595   cso_set_vertex_buffers(st->cso_context, num_vbuffers, vbuffer);
596   cso_set_vertex_elements(st->cso_context, num_velements, velements);
597
598   /* unreference buffers (frees wrapped user-space buffer objects)
599    * This is OK, because the pipe driver should reference buffers by itself
600    * in set_vertex_buffers. */
601   for (attr = 0; attr < num_vbuffers; attr++) {
602      pipe_resource_reference(&vbuffer[attr].buffer, NULL);
603      assert(!vbuffer[attr].buffer);
604   }
605}
606
607
608/**
609 * This function gets plugged into the VBO module and is called when
610 * we have something to render.
611 * Basically, translate the information into the format expected by gallium.
612 */
613void
614st_draw_vbo(struct gl_context *ctx,
615            const struct gl_client_array **arrays,
616            const struct _mesa_prim *prims,
617            GLuint nr_prims,
618            const struct _mesa_index_buffer *ib,
619	    GLboolean index_bounds_valid,
620            GLuint min_index,
621            GLuint max_index)
622{
623   struct st_context *st = st_context(ctx);
624   struct pipe_context *pipe = st->pipe;
625   struct pipe_index_buffer ibuffer;
626   struct pipe_draw_info info;
627   unsigned i;
628   GLboolean new_array = GL_TRUE;
629   /* Fix this (Bug 34378):
630   GLboolean new_array =
631         st->dirty.st && (st->dirty.mesa & (_NEW_ARRAY | _NEW_PROGRAM)) != 0;*/
632
633   /* Mesa core state should have been validated already */
634   assert(ctx->NewState == 0x0);
635
636   if (ib) {
637      /* Gallium probably doesn't want this in some cases. */
638      if (!index_bounds_valid)
639         if (!vbo_all_varyings_in_vbos(arrays))
640            vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
641   } else {
642      /* Get min/max index for non-indexed drawing. */
643      min_index = ~0;
644      max_index = 0;
645
646      for (i = 0; i < nr_prims; i++) {
647         min_index = MIN2(min_index, prims[i].start);
648         max_index = MAX2(max_index, prims[i].start + prims[i].count - 1);
649         max_index = MAX2(max_index, prims[i].num_instances);
650      }
651   }
652
653   /* Validate state. */
654   if (st->dirty.st) {
655      GLboolean vertDataEdgeFlags;
656
657      /* sanity check for pointer arithmetic below */
658      assert(sizeof(arrays[0]->Ptr[0]) == 1);
659
660      vertDataEdgeFlags = arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj &&
661                          arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj->Name;
662      if (vertDataEdgeFlags != st->vertdata_edgeflags) {
663         st->vertdata_edgeflags = vertDataEdgeFlags;
664         st->dirty.st |= ST_NEW_EDGEFLAGS_DATA;
665      }
666
667      st_validate_state(st);
668
669      if (new_array) {
670         st_validate_varrays(ctx, arrays, max_index);
671      }
672
673#if 0
674      if (MESA_VERBOSE & VERBOSE_GLSL) {
675         check_uniforms(ctx);
676      }
677#else
678      (void) check_uniforms;
679#endif
680   }
681
682   /* Notify the driver that the content of user buffers may have been
683    * changed. */
684   if (!new_array && st->num_user_vbs) {
685      for (i = 0; i < st->num_user_vbs; i++) {
686         if (st->user_vb[i]) {
687            unsigned stride = st->user_vb_stride[i];
688
689            if (stride) {
690               pipe->redefine_user_buffer(pipe, st->user_vb[i],
691                                          min_index * stride,
692                                          (max_index + 1 - min_index) * stride);
693            } else {
694               /* stride == 0 */
695               pipe->redefine_user_buffer(pipe, st->user_vb[i],
696                                          0, st->user_vb[i]->width0);
697            }
698         }
699      }
700   }
701
702   setup_index_buffer(ctx, ib, &ibuffer);
703   pipe->set_index_buffer(pipe, &ibuffer);
704
705   util_draw_init_info(&info);
706   if (ib) {
707      info.indexed = TRUE;
708      if (min_index != ~0 && max_index != ~0) {
709         info.min_index = min_index;
710         info.max_index = max_index;
711      }
712   }
713
714   info.primitive_restart = st->ctx->Array.PrimitiveRestart;
715   info.restart_index = st->ctx->Array.RestartIndex;
716
717   /* do actual drawing */
718   for (i = 0; i < nr_prims; i++) {
719      info.mode = translate_prim( ctx, prims[i].mode );
720      info.start = prims[i].start;
721      info.count = prims[i].count;
722      info.instance_count = prims[i].num_instances;
723      info.index_bias = prims[i].basevertex;
724      if (!ib) {
725         info.min_index = info.start;
726         info.max_index = info.start + info.count - 1;
727      }
728
729      if (u_trim_pipe_prim(info.mode, &info.count))
730         pipe->draw_vbo(pipe, &info);
731   }
732
733   pipe_resource_reference(&ibuffer.buffer, NULL);
734}
735
736
737void st_init_draw( struct st_context *st )
738{
739   struct gl_context *ctx = st->ctx;
740
741   vbo_set_draw_func(ctx, st_draw_vbo);
742
743#if FEATURE_feedback || FEATURE_rastpos
744   st->draw = draw_create(st->pipe); /* for selection/feedback */
745
746   /* Disable draw options that might convert points/lines to tris, etc.
747    * as that would foul-up feedback/selection mode.
748    */
749   draw_wide_line_threshold(st->draw, 1000.0f);
750   draw_wide_point_threshold(st->draw, 1000.0f);
751   draw_enable_line_stipple(st->draw, FALSE);
752   draw_enable_point_sprites(st->draw, FALSE);
753#endif
754}
755
756
757void st_destroy_draw( struct st_context *st )
758{
759#if FEATURE_feedback || FEATURE_rastpos
760   draw_destroy(st->draw);
761#endif
762}
763
764
765