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