st/mesa: remove primitive restart assertion
[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_vbo().
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/bufferobj.h"
46 #include "main/macros.h"
47 #include "main/mfeatures.h"
48 #include "program/prog_uniform.h"
49
50 #include "vbo/vbo.h"
51
52 #include "st_context.h"
53 #include "st_atom.h"
54 #include "st_cb_bufferobjects.h"
55 #include "st_draw.h"
56 #include "st_program.h"
57
58 #include "pipe/p_context.h"
59 #include "pipe/p_defines.h"
60 #include "util/u_inlines.h"
61 #include "util/u_format.h"
62 #include "util/u_prim.h"
63 #include "util/u_draw_quad.h"
64 #include "draw/draw_context.h"
65 #include "cso_cache/cso_context.h"
66
67
68 static GLuint double_types[4] = {
69 PIPE_FORMAT_R64_FLOAT,
70 PIPE_FORMAT_R64G64_FLOAT,
71 PIPE_FORMAT_R64G64B64_FLOAT,
72 PIPE_FORMAT_R64G64B64A64_FLOAT
73 };
74
75 static GLuint float_types[4] = {
76 PIPE_FORMAT_R32_FLOAT,
77 PIPE_FORMAT_R32G32_FLOAT,
78 PIPE_FORMAT_R32G32B32_FLOAT,
79 PIPE_FORMAT_R32G32B32A32_FLOAT
80 };
81
82 static GLuint half_float_types[4] = {
83 PIPE_FORMAT_R16_FLOAT,
84 PIPE_FORMAT_R16G16_FLOAT,
85 PIPE_FORMAT_R16G16B16_FLOAT,
86 PIPE_FORMAT_R16G16B16A16_FLOAT
87 };
88
89 static GLuint uint_types_norm[4] = {
90 PIPE_FORMAT_R32_UNORM,
91 PIPE_FORMAT_R32G32_UNORM,
92 PIPE_FORMAT_R32G32B32_UNORM,
93 PIPE_FORMAT_R32G32B32A32_UNORM
94 };
95
96 static GLuint uint_types_scale[4] = {
97 PIPE_FORMAT_R32_USCALED,
98 PIPE_FORMAT_R32G32_USCALED,
99 PIPE_FORMAT_R32G32B32_USCALED,
100 PIPE_FORMAT_R32G32B32A32_USCALED
101 };
102
103 static GLuint int_types_norm[4] = {
104 PIPE_FORMAT_R32_SNORM,
105 PIPE_FORMAT_R32G32_SNORM,
106 PIPE_FORMAT_R32G32B32_SNORM,
107 PIPE_FORMAT_R32G32B32A32_SNORM
108 };
109
110 static GLuint int_types_scale[4] = {
111 PIPE_FORMAT_R32_SSCALED,
112 PIPE_FORMAT_R32G32_SSCALED,
113 PIPE_FORMAT_R32G32B32_SSCALED,
114 PIPE_FORMAT_R32G32B32A32_SSCALED
115 };
116
117 static GLuint ushort_types_norm[4] = {
118 PIPE_FORMAT_R16_UNORM,
119 PIPE_FORMAT_R16G16_UNORM,
120 PIPE_FORMAT_R16G16B16_UNORM,
121 PIPE_FORMAT_R16G16B16A16_UNORM
122 };
123
124 static GLuint ushort_types_scale[4] = {
125 PIPE_FORMAT_R16_USCALED,
126 PIPE_FORMAT_R16G16_USCALED,
127 PIPE_FORMAT_R16G16B16_USCALED,
128 PIPE_FORMAT_R16G16B16A16_USCALED
129 };
130
131 static GLuint short_types_norm[4] = {
132 PIPE_FORMAT_R16_SNORM,
133 PIPE_FORMAT_R16G16_SNORM,
134 PIPE_FORMAT_R16G16B16_SNORM,
135 PIPE_FORMAT_R16G16B16A16_SNORM
136 };
137
138 static GLuint short_types_scale[4] = {
139 PIPE_FORMAT_R16_SSCALED,
140 PIPE_FORMAT_R16G16_SSCALED,
141 PIPE_FORMAT_R16G16B16_SSCALED,
142 PIPE_FORMAT_R16G16B16A16_SSCALED
143 };
144
145 static GLuint ubyte_types_norm[4] = {
146 PIPE_FORMAT_R8_UNORM,
147 PIPE_FORMAT_R8G8_UNORM,
148 PIPE_FORMAT_R8G8B8_UNORM,
149 PIPE_FORMAT_R8G8B8A8_UNORM
150 };
151
152 static GLuint ubyte_types_scale[4] = {
153 PIPE_FORMAT_R8_USCALED,
154 PIPE_FORMAT_R8G8_USCALED,
155 PIPE_FORMAT_R8G8B8_USCALED,
156 PIPE_FORMAT_R8G8B8A8_USCALED
157 };
158
159 static GLuint byte_types_norm[4] = {
160 PIPE_FORMAT_R8_SNORM,
161 PIPE_FORMAT_R8G8_SNORM,
162 PIPE_FORMAT_R8G8B8_SNORM,
163 PIPE_FORMAT_R8G8B8A8_SNORM
164 };
165
166 static GLuint byte_types_scale[4] = {
167 PIPE_FORMAT_R8_SSCALED,
168 PIPE_FORMAT_R8G8_SSCALED,
169 PIPE_FORMAT_R8G8B8_SSCALED,
170 PIPE_FORMAT_R8G8B8A8_SSCALED
171 };
172
173 static GLuint fixed_types[4] = {
174 PIPE_FORMAT_R32_FIXED,
175 PIPE_FORMAT_R32G32_FIXED,
176 PIPE_FORMAT_R32G32B32_FIXED,
177 PIPE_FORMAT_R32G32B32A32_FIXED
178 };
179
180
181
182 /**
183 * Return a PIPE_FORMAT_x for the given GL datatype and size.
184 */
185 enum pipe_format
186 st_pipe_vertex_format(GLenum type, GLuint size, GLenum format,
187 GLboolean normalized)
188 {
189 assert((type >= GL_BYTE && type <= GL_DOUBLE) ||
190 type == GL_FIXED || type == GL_HALF_FLOAT ||
191 type == GL_INT_2_10_10_10_REV ||
192 type == GL_UNSIGNED_INT_2_10_10_10_REV);
193 assert(size >= 1);
194 assert(size <= 4);
195 assert(format == GL_RGBA || format == GL_BGRA);
196
197 if (type == GL_INT_2_10_10_10_REV ||
198 type == GL_UNSIGNED_INT_2_10_10_10_REV) {
199 assert(size == 4);
200
201 if (format == GL_BGRA) {
202 if (type == GL_INT_2_10_10_10_REV) {
203 if (normalized)
204 return PIPE_FORMAT_B10G10R10A2_SNORM;
205 else
206 return PIPE_FORMAT_B10G10R10A2_SSCALED;
207 } else {
208 if (normalized)
209 return PIPE_FORMAT_B10G10R10A2_UNORM;
210 else
211 return PIPE_FORMAT_B10G10R10A2_USCALED;
212 }
213 } else {
214 if (type == GL_INT_2_10_10_10_REV) {
215 if (normalized)
216 return PIPE_FORMAT_R10G10B10A2_SNORM;
217 else
218 return PIPE_FORMAT_R10G10B10A2_SSCALED;
219 } else {
220 if (normalized)
221 return PIPE_FORMAT_R10G10B10A2_UNORM;
222 else
223 return PIPE_FORMAT_R10G10B10A2_USCALED;
224 }
225 }
226 }
227
228 if (format == GL_BGRA) {
229 /* this is an odd-ball case */
230 assert(type == GL_UNSIGNED_BYTE);
231 assert(normalized);
232 return PIPE_FORMAT_B8G8R8A8_UNORM;
233 }
234
235 if (normalized) {
236 switch (type) {
237 case GL_DOUBLE: return double_types[size-1];
238 case GL_FLOAT: return float_types[size-1];
239 case GL_HALF_FLOAT: return half_float_types[size-1];
240 case GL_INT: return int_types_norm[size-1];
241 case GL_SHORT: return short_types_norm[size-1];
242 case GL_BYTE: return byte_types_norm[size-1];
243 case GL_UNSIGNED_INT: return uint_types_norm[size-1];
244 case GL_UNSIGNED_SHORT: return ushort_types_norm[size-1];
245 case GL_UNSIGNED_BYTE: return ubyte_types_norm[size-1];
246 case GL_FIXED: return fixed_types[size-1];
247 default: assert(0); return 0;
248 }
249 }
250 else {
251 switch (type) {
252 case GL_DOUBLE: return double_types[size-1];
253 case GL_FLOAT: return float_types[size-1];
254 case GL_HALF_FLOAT: return half_float_types[size-1];
255 case GL_INT: return int_types_scale[size-1];
256 case GL_SHORT: return short_types_scale[size-1];
257 case GL_BYTE: return byte_types_scale[size-1];
258 case GL_UNSIGNED_INT: return uint_types_scale[size-1];
259 case GL_UNSIGNED_SHORT: return ushort_types_scale[size-1];
260 case GL_UNSIGNED_BYTE: return ubyte_types_scale[size-1];
261 case GL_FIXED: return fixed_types[size-1];
262 default: assert(0); return 0;
263 }
264 }
265 return PIPE_FORMAT_NONE; /* silence compiler warning */
266 }
267
268
269 /**
270 * This is very similar to vbo_all_varyings_in_vbos() but we are
271 * only interested in per-vertex data. See bug 38626.
272 */
273 static GLboolean
274 all_varyings_in_vbos(const struct gl_client_array *arrays[])
275 {
276 GLuint i;
277
278 for (i = 0; i < VERT_ATTRIB_MAX; i++)
279 if (arrays[i]->StrideB &&
280 !arrays[i]->InstanceDivisor &&
281 !_mesa_is_bufferobj(arrays[i]->BufferObj))
282 return GL_FALSE;
283
284 return GL_TRUE;
285 }
286
287
288 /**
289 * Examine the active arrays to determine if we have interleaved
290 * vertex arrays all living in one VBO, or all living in user space.
291 */
292 static GLboolean
293 is_interleaved_arrays(const struct st_vertex_program *vp,
294 const struct st_vp_variant *vpv,
295 const struct gl_client_array **arrays)
296 {
297 GLuint attr;
298 const struct gl_buffer_object *firstBufObj = NULL;
299 GLint firstStride = -1;
300 const GLubyte *firstPtr = NULL;
301 GLboolean userSpaceBuffer = GL_FALSE;
302
303 for (attr = 0; attr < vpv->num_inputs; attr++) {
304 const GLuint mesaAttr = vp->index_to_input[attr];
305 const struct gl_client_array *array = arrays[mesaAttr];
306 const struct gl_buffer_object *bufObj = array->BufferObj;
307 const GLsizei stride = array->StrideB; /* in bytes */
308
309 if (attr == 0) {
310 /* save info about the first array */
311 firstStride = stride;
312 firstPtr = array->Ptr;
313 firstBufObj = bufObj;
314 userSpaceBuffer = !bufObj || !bufObj->Name;
315 }
316 else {
317 /* check if other arrays interleave with the first, in same buffer */
318 if (stride != firstStride)
319 return GL_FALSE; /* strides don't match */
320
321 if (bufObj != firstBufObj)
322 return GL_FALSE; /* arrays in different VBOs */
323
324 if (abs(array->Ptr - firstPtr) > firstStride)
325 return GL_FALSE; /* arrays start too far apart */
326
327 if ((!bufObj || !_mesa_is_bufferobj(bufObj)) != userSpaceBuffer)
328 return GL_FALSE; /* mix of VBO and user-space arrays */
329 }
330 }
331
332 return GL_TRUE;
333 }
334
335
336 /**
337 * Set up for drawing interleaved arrays that all live in one VBO
338 * or all live in user space.
339 * \param vbuffer returns vertex buffer info
340 * \param velements returns vertex element info
341 */
342 static void
343 setup_interleaved_attribs(struct gl_context *ctx,
344 const struct st_vertex_program *vp,
345 const struct st_vp_variant *vpv,
346 const struct gl_client_array **arrays,
347 struct pipe_vertex_buffer *vbuffer,
348 struct pipe_vertex_element velements[],
349 unsigned max_index,
350 unsigned num_instances)
351 {
352 struct st_context *st = st_context(ctx);
353 struct pipe_context *pipe = st->pipe;
354 GLuint attr;
355 const GLubyte *low_addr = NULL;
356
357 /* Find the lowest address of the arrays we're drawing */
358 if (vpv->num_inputs) {
359 low_addr = arrays[vp->index_to_input[0]]->Ptr;
360
361 for (attr = 1; attr < vpv->num_inputs; attr++) {
362 const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr;
363 low_addr = MIN2(low_addr, start);
364 }
365 }
366
367 for (attr = 0; attr < vpv->num_inputs; attr++) {
368 const GLuint mesaAttr = vp->index_to_input[attr];
369 const struct gl_client_array *array = arrays[mesaAttr];
370 struct gl_buffer_object *bufobj = array->BufferObj;
371 struct st_buffer_object *stobj = st_buffer_object(bufobj);
372 unsigned src_offset = (unsigned) (array->Ptr - low_addr);
373 GLuint element_size = array->_ElementSize;
374 GLsizei stride = array->StrideB;
375
376 assert(element_size == array->Size * _mesa_sizeof_type(array->Type));
377
378 if (attr == 0) {
379 if (bufobj && _mesa_is_bufferobj(bufobj)) {
380 vbuffer->buffer = NULL;
381 pipe_resource_reference(&vbuffer->buffer, stobj->buffer);
382 vbuffer->buffer_offset = pointer_to_offset(low_addr);
383 }
384 else {
385 uint divisor = array->InstanceDivisor;
386 uint last_index = divisor ? num_instances / divisor : max_index;
387 uint bytes = src_offset + stride * last_index + element_size;
388
389 vbuffer->buffer = pipe_user_buffer_create(pipe->screen,
390 (void*) low_addr,
391 bytes,
392 PIPE_BIND_VERTEX_BUFFER);
393 vbuffer->buffer_offset = 0;
394
395 /* Track user vertex buffers. */
396 pipe_resource_reference(&st->user_attrib[0].buffer, vbuffer->buffer);
397 st->user_attrib[0].element_size = element_size;
398 st->user_attrib[0].stride = stride;
399 st->num_user_attribs = 1;
400 }
401 vbuffer->stride = stride; /* in bytes */
402 }
403
404 velements[attr].src_offset = src_offset;
405 velements[attr].instance_divisor = array->InstanceDivisor;
406 velements[attr].vertex_buffer_index = 0;
407 velements[attr].src_format = st_pipe_vertex_format(array->Type,
408 array->Size,
409 array->Format,
410 array->Normalized);
411 assert(velements[attr].src_format);
412 }
413 }
414
415
416 /**
417 * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
418 * vertex attribute.
419 * \param vbuffer returns vertex buffer info
420 * \param velements returns vertex element info
421 */
422 static void
423 setup_non_interleaved_attribs(struct gl_context *ctx,
424 const struct st_vertex_program *vp,
425 const struct st_vp_variant *vpv,
426 const struct gl_client_array **arrays,
427 struct pipe_vertex_buffer vbuffer[],
428 struct pipe_vertex_element velements[],
429 unsigned max_index,
430 unsigned num_instances)
431 {
432 struct st_context *st = st_context(ctx);
433 struct pipe_context *pipe = st->pipe;
434 GLuint attr;
435
436 for (attr = 0; attr < vpv->num_inputs; attr++) {
437 const GLuint mesaAttr = vp->index_to_input[attr];
438 const struct gl_client_array *array = arrays[mesaAttr];
439 struct gl_buffer_object *bufobj = array->BufferObj;
440 GLuint element_size = array->_ElementSize;
441 GLsizei stride = array->StrideB;
442
443 assert(element_size == array->Size * _mesa_sizeof_type(array->Type));
444
445 if (bufobj && _mesa_is_bufferobj(bufobj)) {
446 /* Attribute data is in a VBO.
447 * Recall that for VBOs, the gl_client_array->Ptr field is
448 * really an offset from the start of the VBO, not a pointer.
449 */
450 struct st_buffer_object *stobj = st_buffer_object(bufobj);
451 assert(stobj->buffer);
452
453 vbuffer[attr].buffer = NULL;
454 pipe_resource_reference(&vbuffer[attr].buffer, stobj->buffer);
455 vbuffer[attr].buffer_offset = pointer_to_offset(array->Ptr);
456 }
457 else {
458 /* wrap user data */
459 uint bytes;
460 void *ptr;
461
462 if (array->Ptr) {
463 uint divisor = array->InstanceDivisor;
464 uint last_index = divisor ? num_instances / divisor : max_index;
465
466 bytes = stride * last_index + element_size;
467
468 ptr = (void *) array->Ptr;
469 }
470 else {
471 /* no array, use ctx->Current.Attrib[] value */
472 bytes = element_size = sizeof(ctx->Current.Attrib[0]);
473 ptr = (void *) ctx->Current.Attrib[mesaAttr];
474 stride = 0;
475 }
476
477 assert(ptr);
478 assert(bytes);
479
480 vbuffer[attr].buffer =
481 pipe_user_buffer_create(pipe->screen, ptr, bytes,
482 PIPE_BIND_VERTEX_BUFFER);
483
484 vbuffer[attr].buffer_offset = 0;
485
486 /* Track user vertex buffers. */
487 pipe_resource_reference(&st->user_attrib[attr].buffer, vbuffer[attr].buffer);
488 st->user_attrib[attr].element_size = element_size;
489 st->user_attrib[attr].stride = stride;
490 st->num_user_attribs = MAX2(st->num_user_attribs, attr + 1);
491 }
492
493 /* common-case setup */
494 vbuffer[attr].stride = stride; /* in bytes */
495
496 velements[attr].src_offset = 0;
497 velements[attr].instance_divisor = array->InstanceDivisor;
498 velements[attr].vertex_buffer_index = attr;
499 velements[attr].src_format = st_pipe_vertex_format(array->Type,
500 array->Size,
501 array->Format,
502 array->Normalized);
503 assert(velements[attr].src_format);
504 }
505 }
506
507
508 static void
509 setup_index_buffer(struct gl_context *ctx,
510 const struct _mesa_index_buffer *ib,
511 struct pipe_index_buffer *ibuffer)
512 {
513 struct st_context *st = st_context(ctx);
514 struct pipe_context *pipe = st->pipe;
515
516 memset(ibuffer, 0, sizeof(*ibuffer));
517 if (ib) {
518 struct gl_buffer_object *bufobj = ib->obj;
519
520 switch (ib->type) {
521 case GL_UNSIGNED_INT:
522 ibuffer->index_size = 4;
523 break;
524 case GL_UNSIGNED_SHORT:
525 ibuffer->index_size = 2;
526 break;
527 case GL_UNSIGNED_BYTE:
528 ibuffer->index_size = 1;
529 break;
530 default:
531 assert(0);
532 return;
533 }
534
535 /* get/create the index buffer object */
536 if (bufobj && _mesa_is_bufferobj(bufobj)) {
537 /* elements/indexes are in a real VBO */
538 struct st_buffer_object *stobj = st_buffer_object(bufobj);
539 pipe_resource_reference(&ibuffer->buffer, stobj->buffer);
540 ibuffer->offset = pointer_to_offset(ib->ptr);
541 }
542 else {
543 /* element/indicies are in user space memory */
544 ibuffer->buffer =
545 pipe_user_buffer_create(pipe->screen, (void *) ib->ptr,
546 ib->count * ibuffer->index_size,
547 PIPE_BIND_INDEX_BUFFER);
548 }
549 }
550 }
551
552
553 /**
554 * Prior to drawing, check that any uniforms referenced by the
555 * current shader have been set. If a uniform has not been set,
556 * issue a warning.
557 */
558 static void
559 check_uniforms(struct gl_context *ctx)
560 {
561 struct gl_shader_program *shProg[3] = {
562 ctx->Shader.CurrentVertexProgram,
563 ctx->Shader.CurrentGeometryProgram,
564 ctx->Shader.CurrentFragmentProgram,
565 };
566 unsigned j;
567
568 for (j = 0; j < 3; j++) {
569 unsigned i;
570
571 if (shProg[j] == NULL || !shProg[j]->LinkStatus)
572 continue;
573
574 for (i = 0; i < shProg[j]->Uniforms->NumUniforms; i++) {
575 const struct gl_uniform *u = &shProg[j]->Uniforms->Uniforms[i];
576 if (!u->Initialized) {
577 _mesa_warning(ctx,
578 "Using shader with uninitialized uniform: %s",
579 u->Name);
580 }
581 }
582 }
583 }
584
585 /** Helper code for primitive restart fallback */
586 #define DO_DRAW(pipe, cur_start, cur_count) \
587 do { \
588 info.start = cur_start; \
589 info.count = cur_count; \
590 if (u_trim_pipe_prim(info.mode, &info.count)) { \
591 if (transfer) \
592 pipe_buffer_unmap(pipe, transfer); \
593 pipe->draw_vbo(pipe, &info); \
594 if (transfer) { \
595 ptr = pipe_buffer_map(pipe, ibuffer->buffer, PIPE_TRANSFER_READ, &transfer); \
596 assert(ptr != NULL); \
597 ptr = ADD_POINTERS(ptr, ibuffer->offset); \
598 } \
599 } \
600 } while(0)
601
602 /** More helper code for primitive restart fallback */
603 #define PRIM_RESTART_LOOP(elements) \
604 do { \
605 for (i = start; i < end; i++) { \
606 if (elements[i] == info.restart_index) { \
607 if (cur_count > 0) { \
608 /* draw elts up to prev pos */ \
609 DO_DRAW(pipe, cur_start, cur_count); \
610 } \
611 /* begin new prim at next elt */ \
612 cur_start = i + 1; \
613 cur_count = 0; \
614 } \
615 else { \
616 cur_count++; \
617 } \
618 } \
619 if (cur_count > 0) { \
620 DO_DRAW(pipe, cur_start, cur_count); \
621 } \
622 } while (0)
623
624 static void
625 handle_fallback_primitive_restart(struct pipe_context *pipe,
626 const struct _mesa_index_buffer *ib,
627 struct pipe_index_buffer *ibuffer,
628 struct pipe_draw_info *orig_info)
629 {
630 const unsigned start = orig_info->start;
631 const unsigned count = orig_info->count;
632 const unsigned end = start + count;
633 struct pipe_draw_info info = *orig_info;
634 struct pipe_transfer *transfer = NULL;
635 unsigned instance, i, cur_start, cur_count;
636 const void *ptr;
637
638 info.primitive_restart = FALSE;
639
640 if (!info.indexed) {
641 /* Splitting the draw arrays call is handled by the VBO module */
642 if (u_trim_pipe_prim(info.mode, &info.count))
643 pipe->draw_vbo(pipe, &info);
644
645 return;
646 }
647
648 /* info.indexed == TRUE */
649 assert(ibuffer);
650 assert(ibuffer->buffer);
651
652 if (ib) {
653 struct gl_buffer_object *bufobj = ib->obj;
654 if (bufobj && bufobj->Name) {
655 ptr = NULL;
656 }
657 else {
658 ptr = ib->ptr;
659 }
660 }
661
662 if (!ptr)
663 ptr = pipe_buffer_map(pipe, ibuffer->buffer, PIPE_TRANSFER_READ, &transfer);
664
665 if (!ptr)
666 return;
667 ptr = ADD_POINTERS(ptr, ibuffer->offset);
668
669 /* Need to loop over instances as well to preserve draw order */
670 for (instance = 0; instance < orig_info->instance_count; instance++) {
671 info.start_instance = instance + orig_info->start_instance;
672 info.instance_count = 1;
673 cur_start = start;
674 cur_count = 0;
675
676 switch (ibuffer->index_size) {
677 case 1:
678 {
679 const ubyte *elt_ub = (const ubyte *)ptr;
680 PRIM_RESTART_LOOP(elt_ub);
681 }
682 break;
683 case 2:
684 {
685 const ushort *elt_us = (const ushort *)ptr;
686 PRIM_RESTART_LOOP(elt_us);
687 }
688 break;
689 case 4:
690 {
691 const uint *elt_ui = (const uint *)ptr;
692 PRIM_RESTART_LOOP(elt_ui);
693 }
694 break;
695 default:
696 assert(0 && "bad index_size in handle_fallback_primitive_restart()");
697 }
698 }
699
700 if (transfer)
701 pipe_buffer_unmap(pipe, transfer);
702 }
703
704
705 /**
706 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
707 * the corresponding Gallium type.
708 */
709 static unsigned
710 translate_prim(const struct gl_context *ctx, unsigned prim)
711 {
712 /* GL prims should match Gallium prims, spot-check a few */
713 assert(GL_POINTS == PIPE_PRIM_POINTS);
714 assert(GL_QUADS == PIPE_PRIM_QUADS);
715 assert(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
716
717 /* Avoid quadstrips if it's easy to do so:
718 * Note: it's important to do the correct trimming if we change the
719 * prim type! We do that wherever this function is called.
720 */
721 if (prim == GL_QUAD_STRIP &&
722 ctx->Light.ShadeModel != GL_FLAT &&
723 ctx->Polygon.FrontMode == GL_FILL &&
724 ctx->Polygon.BackMode == GL_FILL)
725 prim = GL_TRIANGLE_STRIP;
726
727 return prim;
728 }
729
730
731 static void
732 st_validate_varrays(struct gl_context *ctx,
733 const struct gl_client_array **arrays,
734 unsigned max_index,
735 unsigned num_instances)
736 {
737 struct st_context *st = st_context(ctx);
738 const struct st_vertex_program *vp;
739 const struct st_vp_variant *vpv;
740 struct pipe_vertex_buffer vbuffer[PIPE_MAX_SHADER_INPUTS];
741 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
742 unsigned num_vbuffers, num_velements;
743 GLuint attr;
744 unsigned i;
745
746 /* must get these after state validation! */
747 vp = st->vp;
748 vpv = st->vp_variant;
749
750 memset(velements, 0, sizeof(struct pipe_vertex_element) * vpv->num_inputs);
751
752 /* Unreference any user vertex buffers. */
753 for (i = 0; i < st->num_user_attribs; i++) {
754 pipe_resource_reference(&st->user_attrib[i].buffer, NULL);
755 }
756 st->num_user_attribs = 0;
757
758 /*
759 * Setup the vbuffer[] and velements[] arrays.
760 */
761 if (is_interleaved_arrays(vp, vpv, arrays)) {
762 setup_interleaved_attribs(ctx, vp, vpv, arrays, vbuffer, velements,
763 max_index, num_instances);
764
765 num_vbuffers = 1;
766 num_velements = vpv->num_inputs;
767 if (num_velements == 0)
768 num_vbuffers = 0;
769 }
770 else {
771 setup_non_interleaved_attribs(ctx, vp, vpv, arrays,
772 vbuffer, velements, max_index,
773 num_instances);
774 num_vbuffers = vpv->num_inputs;
775 num_velements = vpv->num_inputs;
776 }
777
778 cso_set_vertex_buffers(st->cso_context, num_vbuffers, vbuffer);
779 cso_set_vertex_elements(st->cso_context, num_velements, velements);
780
781 /* unreference buffers (frees wrapped user-space buffer objects)
782 * This is OK, because the pipe driver should reference buffers by itself
783 * in set_vertex_buffers. */
784 for (attr = 0; attr < num_vbuffers; attr++) {
785 pipe_resource_reference(&vbuffer[attr].buffer, NULL);
786 assert(!vbuffer[attr].buffer);
787 }
788 }
789
790
791 /**
792 * This function gets plugged into the VBO module and is called when
793 * we have something to render.
794 * Basically, translate the information into the format expected by gallium.
795 */
796 void
797 st_draw_vbo(struct gl_context *ctx,
798 const struct gl_client_array **arrays,
799 const struct _mesa_prim *prims,
800 GLuint nr_prims,
801 const struct _mesa_index_buffer *ib,
802 GLboolean index_bounds_valid,
803 GLuint min_index,
804 GLuint max_index)
805 {
806 struct st_context *st = st_context(ctx);
807 struct pipe_context *pipe = st->pipe;
808 struct pipe_index_buffer ibuffer;
809 struct pipe_draw_info info;
810 unsigned i, num_instances = 1;
811 GLboolean new_array =
812 st->dirty.st &&
813 (st->dirty.mesa & (_NEW_ARRAY | _NEW_PROGRAM | _NEW_BUFFER_OBJECT)) != 0;
814
815 /* Mesa core state should have been validated already */
816 assert(ctx->NewState == 0x0);
817
818 if (ib) {
819 /* Gallium probably doesn't want this in some cases. */
820 if (!index_bounds_valid)
821 if (!all_varyings_in_vbos(arrays))
822 vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
823
824 for (i = 0; i < nr_prims; i++) {
825 num_instances = MAX2(num_instances, prims[i].num_instances);
826 }
827 }
828 else {
829 /* Get min/max index for non-indexed drawing. */
830 min_index = ~0;
831 max_index = 0;
832
833 for (i = 0; i < nr_prims; i++) {
834 min_index = MIN2(min_index, prims[i].start);
835 max_index = MAX2(max_index, prims[i].start + prims[i].count - 1);
836 num_instances = MAX2(num_instances, prims[i].num_instances);
837 }
838 }
839
840 /* Validate state. */
841 if (st->dirty.st) {
842 GLboolean vertDataEdgeFlags;
843
844 /* sanity check for pointer arithmetic below */
845 assert(sizeof(arrays[0]->Ptr[0]) == 1);
846
847 vertDataEdgeFlags = arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj &&
848 arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj->Name;
849 if (vertDataEdgeFlags != st->vertdata_edgeflags) {
850 st->vertdata_edgeflags = vertDataEdgeFlags;
851 st->dirty.st |= ST_NEW_EDGEFLAGS_DATA;
852 }
853
854 st_validate_state(st);
855
856 if (new_array) {
857 st_validate_varrays(ctx, arrays, max_index, num_instances);
858 }
859
860 #if 0
861 if (MESA_VERBOSE & VERBOSE_GLSL) {
862 check_uniforms(ctx);
863 }
864 #else
865 (void) check_uniforms;
866 #endif
867 }
868
869 /* Notify the driver that the content of user buffers may have been
870 * changed. */
871 assert(max_index >= min_index);
872 if (!new_array && st->num_user_attribs) {
873 for (i = 0; i < st->num_user_attribs; i++) {
874 if (st->user_attrib[i].buffer) {
875 unsigned element_size = st->user_attrib[i].element_size;
876 unsigned stride = st->user_attrib[i].stride;
877 unsigned min_offset = min_index * stride;
878 unsigned max_offset = max_index * stride + element_size;
879
880 assert(max_offset > min_offset);
881
882 pipe->redefine_user_buffer(pipe, st->user_attrib[i].buffer,
883 min_offset,
884 max_offset - min_offset);
885 }
886 }
887 }
888
889 setup_index_buffer(ctx, ib, &ibuffer);
890 pipe->set_index_buffer(pipe, &ibuffer);
891
892 util_draw_init_info(&info);
893 if (ib) {
894 info.indexed = TRUE;
895 if (min_index != ~0 && max_index != ~0) {
896 info.min_index = min_index;
897 info.max_index = max_index;
898 }
899 }
900
901 info.primitive_restart = ctx->Array.PrimitiveRestart;
902 info.restart_index = ctx->Array.RestartIndex;
903
904 /* do actual drawing */
905 for (i = 0; i < nr_prims; i++) {
906 info.mode = translate_prim( ctx, prims[i].mode );
907 info.start = prims[i].start;
908 info.count = prims[i].count;
909 info.instance_count = prims[i].num_instances;
910 info.index_bias = prims[i].basevertex;
911 if (!ib) {
912 info.min_index = info.start;
913 info.max_index = info.start + info.count - 1;
914 }
915
916 if (info.primitive_restart) {
917 /*
918 * Handle primitive restart for drivers that doesn't support it.
919 *
920 * The VBO module handles restart inside of draw_arrays for us,
921 * but we should still remove the primitive_restart flag on the
922 * info struct, the fallback function does this for us. Just
923 * remove the flag for all drivers in this case as well.
924 */
925 if (st->sw_primitive_restart || !info.indexed)
926 handle_fallback_primitive_restart(pipe, ib, &ibuffer, &info);
927 else
928 /* don't trim, restarts might be inside index list */
929 pipe->draw_vbo(pipe, &info);
930 }
931 else if (u_trim_pipe_prim(info.mode, &info.count))
932 pipe->draw_vbo(pipe, &info);
933 }
934
935 pipe_resource_reference(&ibuffer.buffer, NULL);
936 }
937
938
939 void
940 st_init_draw(struct st_context *st)
941 {
942 struct gl_context *ctx = st->ctx;
943
944 vbo_set_draw_func(ctx, st_draw_vbo);
945
946 #if FEATURE_feedback || FEATURE_rastpos
947 st->draw = draw_create(st->pipe); /* for selection/feedback */
948
949 /* Disable draw options that might convert points/lines to tris, etc.
950 * as that would foul-up feedback/selection mode.
951 */
952 draw_wide_line_threshold(st->draw, 1000.0f);
953 draw_wide_point_threshold(st->draw, 1000.0f);
954 draw_enable_line_stipple(st->draw, FALSE);
955 draw_enable_point_sprites(st->draw, FALSE);
956 #endif
957 }
958
959
960 void
961 st_destroy_draw(struct st_context *st)
962 {
963 #if FEATURE_feedback || FEATURE_rastpos
964 draw_destroy(st->draw);
965 #endif
966 }