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