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