st/mesa: make user index buffers optional
[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
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_cb_xformfb.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 "util/u_upload_mgr.h"
65 #include "draw/draw_context.h"
66 #include "cso_cache/cso_context.h"
67
68 #include "../glsl/ir_uniform.h"
69
70
71 static GLuint double_types[4] = {
72 PIPE_FORMAT_R64_FLOAT,
73 PIPE_FORMAT_R64G64_FLOAT,
74 PIPE_FORMAT_R64G64B64_FLOAT,
75 PIPE_FORMAT_R64G64B64A64_FLOAT
76 };
77
78 static GLuint float_types[4] = {
79 PIPE_FORMAT_R32_FLOAT,
80 PIPE_FORMAT_R32G32_FLOAT,
81 PIPE_FORMAT_R32G32B32_FLOAT,
82 PIPE_FORMAT_R32G32B32A32_FLOAT
83 };
84
85 static GLuint half_float_types[4] = {
86 PIPE_FORMAT_R16_FLOAT,
87 PIPE_FORMAT_R16G16_FLOAT,
88 PIPE_FORMAT_R16G16B16_FLOAT,
89 PIPE_FORMAT_R16G16B16A16_FLOAT
90 };
91
92 static GLuint uint_types_norm[4] = {
93 PIPE_FORMAT_R32_UNORM,
94 PIPE_FORMAT_R32G32_UNORM,
95 PIPE_FORMAT_R32G32B32_UNORM,
96 PIPE_FORMAT_R32G32B32A32_UNORM
97 };
98
99 static GLuint uint_types_scale[4] = {
100 PIPE_FORMAT_R32_USCALED,
101 PIPE_FORMAT_R32G32_USCALED,
102 PIPE_FORMAT_R32G32B32_USCALED,
103 PIPE_FORMAT_R32G32B32A32_USCALED
104 };
105
106 static GLuint uint_types_int[4] = {
107 PIPE_FORMAT_R32_UINT,
108 PIPE_FORMAT_R32G32_UINT,
109 PIPE_FORMAT_R32G32B32_UINT,
110 PIPE_FORMAT_R32G32B32A32_UINT
111 };
112
113 static GLuint int_types_norm[4] = {
114 PIPE_FORMAT_R32_SNORM,
115 PIPE_FORMAT_R32G32_SNORM,
116 PIPE_FORMAT_R32G32B32_SNORM,
117 PIPE_FORMAT_R32G32B32A32_SNORM
118 };
119
120 static GLuint int_types_scale[4] = {
121 PIPE_FORMAT_R32_SSCALED,
122 PIPE_FORMAT_R32G32_SSCALED,
123 PIPE_FORMAT_R32G32B32_SSCALED,
124 PIPE_FORMAT_R32G32B32A32_SSCALED
125 };
126
127 static GLuint int_types_int[4] = {
128 PIPE_FORMAT_R32_SINT,
129 PIPE_FORMAT_R32G32_SINT,
130 PIPE_FORMAT_R32G32B32_SINT,
131 PIPE_FORMAT_R32G32B32A32_SINT
132 };
133
134 static GLuint ushort_types_norm[4] = {
135 PIPE_FORMAT_R16_UNORM,
136 PIPE_FORMAT_R16G16_UNORM,
137 PIPE_FORMAT_R16G16B16_UNORM,
138 PIPE_FORMAT_R16G16B16A16_UNORM
139 };
140
141 static GLuint ushort_types_scale[4] = {
142 PIPE_FORMAT_R16_USCALED,
143 PIPE_FORMAT_R16G16_USCALED,
144 PIPE_FORMAT_R16G16B16_USCALED,
145 PIPE_FORMAT_R16G16B16A16_USCALED
146 };
147
148 static GLuint ushort_types_int[4] = {
149 PIPE_FORMAT_R16_UINT,
150 PIPE_FORMAT_R16G16_UINT,
151 PIPE_FORMAT_R16G16B16_UINT,
152 PIPE_FORMAT_R16G16B16A16_UINT
153 };
154
155 static GLuint short_types_norm[4] = {
156 PIPE_FORMAT_R16_SNORM,
157 PIPE_FORMAT_R16G16_SNORM,
158 PIPE_FORMAT_R16G16B16_SNORM,
159 PIPE_FORMAT_R16G16B16A16_SNORM
160 };
161
162 static GLuint short_types_scale[4] = {
163 PIPE_FORMAT_R16_SSCALED,
164 PIPE_FORMAT_R16G16_SSCALED,
165 PIPE_FORMAT_R16G16B16_SSCALED,
166 PIPE_FORMAT_R16G16B16A16_SSCALED
167 };
168
169 static GLuint short_types_int[4] = {
170 PIPE_FORMAT_R16_SINT,
171 PIPE_FORMAT_R16G16_SINT,
172 PIPE_FORMAT_R16G16B16_SINT,
173 PIPE_FORMAT_R16G16B16A16_SINT
174 };
175
176 static GLuint ubyte_types_norm[4] = {
177 PIPE_FORMAT_R8_UNORM,
178 PIPE_FORMAT_R8G8_UNORM,
179 PIPE_FORMAT_R8G8B8_UNORM,
180 PIPE_FORMAT_R8G8B8A8_UNORM
181 };
182
183 static GLuint ubyte_types_scale[4] = {
184 PIPE_FORMAT_R8_USCALED,
185 PIPE_FORMAT_R8G8_USCALED,
186 PIPE_FORMAT_R8G8B8_USCALED,
187 PIPE_FORMAT_R8G8B8A8_USCALED
188 };
189
190 static GLuint ubyte_types_int[4] = {
191 PIPE_FORMAT_R8_UINT,
192 PIPE_FORMAT_R8G8_UINT,
193 PIPE_FORMAT_R8G8B8_UINT,
194 PIPE_FORMAT_R8G8B8A8_UINT
195 };
196
197 static GLuint byte_types_norm[4] = {
198 PIPE_FORMAT_R8_SNORM,
199 PIPE_FORMAT_R8G8_SNORM,
200 PIPE_FORMAT_R8G8B8_SNORM,
201 PIPE_FORMAT_R8G8B8A8_SNORM
202 };
203
204 static GLuint byte_types_scale[4] = {
205 PIPE_FORMAT_R8_SSCALED,
206 PIPE_FORMAT_R8G8_SSCALED,
207 PIPE_FORMAT_R8G8B8_SSCALED,
208 PIPE_FORMAT_R8G8B8A8_SSCALED
209 };
210
211 static GLuint byte_types_int[4] = {
212 PIPE_FORMAT_R8_SINT,
213 PIPE_FORMAT_R8G8_SINT,
214 PIPE_FORMAT_R8G8B8_SINT,
215 PIPE_FORMAT_R8G8B8A8_SINT
216 };
217
218 static GLuint fixed_types[4] = {
219 PIPE_FORMAT_R32_FIXED,
220 PIPE_FORMAT_R32G32_FIXED,
221 PIPE_FORMAT_R32G32B32_FIXED,
222 PIPE_FORMAT_R32G32B32A32_FIXED
223 };
224
225
226
227 /**
228 * Return a PIPE_FORMAT_x for the given GL datatype and size.
229 */
230 enum pipe_format
231 st_pipe_vertex_format(GLenum type, GLuint size, GLenum format,
232 GLboolean normalized, GLboolean integer)
233 {
234 assert((type >= GL_BYTE && type <= GL_DOUBLE) ||
235 type == GL_FIXED || type == GL_HALF_FLOAT ||
236 type == GL_INT_2_10_10_10_REV ||
237 type == GL_UNSIGNED_INT_2_10_10_10_REV);
238 assert(size >= 1);
239 assert(size <= 4);
240 assert(format == GL_RGBA || format == GL_BGRA);
241
242 if (type == GL_INT_2_10_10_10_REV ||
243 type == GL_UNSIGNED_INT_2_10_10_10_REV) {
244 assert(size == 4);
245 assert(!integer);
246
247 if (format == GL_BGRA) {
248 if (type == GL_INT_2_10_10_10_REV) {
249 if (normalized)
250 return PIPE_FORMAT_B10G10R10A2_SNORM;
251 else
252 return PIPE_FORMAT_B10G10R10A2_SSCALED;
253 } else {
254 if (normalized)
255 return PIPE_FORMAT_B10G10R10A2_UNORM;
256 else
257 return PIPE_FORMAT_B10G10R10A2_USCALED;
258 }
259 } else {
260 if (type == GL_INT_2_10_10_10_REV) {
261 if (normalized)
262 return PIPE_FORMAT_R10G10B10A2_SNORM;
263 else
264 return PIPE_FORMAT_R10G10B10A2_SSCALED;
265 } else {
266 if (normalized)
267 return PIPE_FORMAT_R10G10B10A2_UNORM;
268 else
269 return PIPE_FORMAT_R10G10B10A2_USCALED;
270 }
271 }
272 }
273
274 if (format == GL_BGRA) {
275 /* this is an odd-ball case */
276 assert(type == GL_UNSIGNED_BYTE);
277 assert(normalized);
278 return PIPE_FORMAT_B8G8R8A8_UNORM;
279 }
280
281 if (integer) {
282 switch (type) {
283 case GL_INT: return int_types_int[size-1];
284 case GL_SHORT: return short_types_int[size-1];
285 case GL_BYTE: return byte_types_int[size-1];
286 case GL_UNSIGNED_INT: return uint_types_int[size-1];
287 case GL_UNSIGNED_SHORT: return ushort_types_int[size-1];
288 case GL_UNSIGNED_BYTE: return ubyte_types_int[size-1];
289 default: assert(0); return 0;
290 }
291 }
292 else if (normalized) {
293 switch (type) {
294 case GL_DOUBLE: return double_types[size-1];
295 case GL_FLOAT: return float_types[size-1];
296 case GL_HALF_FLOAT: return half_float_types[size-1];
297 case GL_INT: return int_types_norm[size-1];
298 case GL_SHORT: return short_types_norm[size-1];
299 case GL_BYTE: return byte_types_norm[size-1];
300 case GL_UNSIGNED_INT: return uint_types_norm[size-1];
301 case GL_UNSIGNED_SHORT: return ushort_types_norm[size-1];
302 case GL_UNSIGNED_BYTE: return ubyte_types_norm[size-1];
303 case GL_FIXED: return fixed_types[size-1];
304 default: assert(0); return 0;
305 }
306 }
307 else {
308 switch (type) {
309 case GL_DOUBLE: return double_types[size-1];
310 case GL_FLOAT: return float_types[size-1];
311 case GL_HALF_FLOAT: return half_float_types[size-1];
312 case GL_INT: return int_types_scale[size-1];
313 case GL_SHORT: return short_types_scale[size-1];
314 case GL_BYTE: return byte_types_scale[size-1];
315 case GL_UNSIGNED_INT: return uint_types_scale[size-1];
316 case GL_UNSIGNED_SHORT: return ushort_types_scale[size-1];
317 case GL_UNSIGNED_BYTE: return ubyte_types_scale[size-1];
318 case GL_FIXED: return fixed_types[size-1];
319 default: assert(0); return 0;
320 }
321 }
322 return PIPE_FORMAT_NONE; /* silence compiler warning */
323 }
324
325
326 /**
327 * This is very similar to vbo_all_varyings_in_vbos() but we are
328 * only interested in per-vertex data. See bug 38626.
329 */
330 static GLboolean
331 all_varyings_in_vbos(const struct gl_client_array *arrays[])
332 {
333 GLuint i;
334
335 for (i = 0; i < VERT_ATTRIB_MAX; i++)
336 if (arrays[i]->StrideB &&
337 !arrays[i]->InstanceDivisor &&
338 !_mesa_is_bufferobj(arrays[i]->BufferObj))
339 return GL_FALSE;
340
341 return GL_TRUE;
342 }
343
344
345 /**
346 * Examine the active arrays to determine if we have interleaved
347 * vertex arrays all living in one VBO, or all living in user space.
348 */
349 static GLboolean
350 is_interleaved_arrays(const struct st_vertex_program *vp,
351 const struct st_vp_variant *vpv,
352 const struct gl_client_array **arrays)
353 {
354 GLuint attr;
355 const struct gl_buffer_object *firstBufObj = NULL;
356 GLint firstStride = -1;
357 const GLubyte *firstPtr = NULL;
358 GLboolean userSpaceBuffer = GL_FALSE;
359
360 for (attr = 0; attr < vpv->num_inputs; attr++) {
361 const GLuint mesaAttr = vp->index_to_input[attr];
362 const struct gl_client_array *array = arrays[mesaAttr];
363 const struct gl_buffer_object *bufObj = array->BufferObj;
364 const GLsizei stride = array->StrideB; /* in bytes */
365
366 if (attr == 0) {
367 /* save info about the first array */
368 firstStride = stride;
369 firstPtr = array->Ptr;
370 firstBufObj = bufObj;
371 userSpaceBuffer = !bufObj || !bufObj->Name;
372 }
373 else {
374 /* check if other arrays interleave with the first, in same buffer */
375 if (stride != firstStride)
376 return GL_FALSE; /* strides don't match */
377
378 if (bufObj != firstBufObj)
379 return GL_FALSE; /* arrays in different VBOs */
380
381 if (abs(array->Ptr - firstPtr) > firstStride)
382 return GL_FALSE; /* arrays start too far apart */
383
384 if ((!_mesa_is_bufferobj(bufObj)) != userSpaceBuffer)
385 return GL_FALSE; /* mix of VBO and user-space arrays */
386 }
387 }
388
389 return GL_TRUE;
390 }
391
392
393 /**
394 * Set up for drawing interleaved arrays that all live in one VBO
395 * or all live in user space.
396 * \param vbuffer returns vertex buffer info
397 * \param velements returns vertex element info
398 * \return GL_TRUE for success, GL_FALSE otherwise (probably out of memory)
399 */
400 static GLboolean
401 setup_interleaved_attribs(struct gl_context *ctx,
402 const struct st_vertex_program *vp,
403 const struct st_vp_variant *vpv,
404 const struct gl_client_array **arrays,
405 struct pipe_vertex_buffer *vbuffer,
406 struct pipe_vertex_element velements[],
407 unsigned max_index,
408 unsigned num_instances)
409 {
410 struct st_context *st = st_context(ctx);
411 struct pipe_context *pipe = st->pipe;
412 GLuint attr;
413 const GLubyte *low_addr = NULL;
414 GLboolean usingVBO; /* all arrays in a VBO? */
415 struct gl_buffer_object *bufobj;
416 GLuint user_buffer_size = 0;
417 GLuint vertex_size = 0; /* bytes per vertex, in bytes */
418 GLsizei stride;
419
420 /* Find the lowest address of the arrays we're drawing,
421 * Init bufobj and stride.
422 */
423 if (vpv->num_inputs) {
424 const GLuint mesaAttr0 = vp->index_to_input[0];
425 const struct gl_client_array *array = arrays[mesaAttr0];
426
427 /* Since we're doing interleaved arrays, we know there'll be at most
428 * one buffer object and the stride will be the same for all arrays.
429 * Grab them now.
430 */
431 bufobj = array->BufferObj;
432 stride = array->StrideB;
433
434 low_addr = arrays[vp->index_to_input[0]]->Ptr;
435
436 for (attr = 1; attr < vpv->num_inputs; attr++) {
437 const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr;
438 low_addr = MIN2(low_addr, start);
439 }
440 }
441 else {
442 /* not sure we'll ever have zero inputs, but play it safe */
443 bufobj = NULL;
444 stride = 0;
445 low_addr = 0;
446 }
447
448 /* are the arrays in user space? */
449 usingVBO = _mesa_is_bufferobj(bufobj);
450
451 for (attr = 0; attr < vpv->num_inputs; attr++) {
452 const GLuint mesaAttr = vp->index_to_input[attr];
453 const struct gl_client_array *array = arrays[mesaAttr];
454 unsigned src_offset = (unsigned) (array->Ptr - low_addr);
455 GLuint element_size = array->_ElementSize;
456
457 assert(element_size == array->Size * _mesa_sizeof_type(array->Type));
458
459 velements[attr].src_offset = src_offset;
460 velements[attr].instance_divisor = array->InstanceDivisor;
461 velements[attr].vertex_buffer_index = 0;
462 velements[attr].src_format = st_pipe_vertex_format(array->Type,
463 array->Size,
464 array->Format,
465 array->Normalized,
466 array->Integer);
467 assert(velements[attr].src_format);
468
469 if (!usingVBO) {
470 /* how many bytes referenced by this attribute array? */
471 uint divisor = array->InstanceDivisor;
472 uint last_index = divisor ? num_instances / divisor : max_index;
473 uint bytes = src_offset + stride * last_index + element_size;
474
475 user_buffer_size = MAX2(user_buffer_size, bytes);
476
477 /* update vertex size */
478 vertex_size = MAX2(vertex_size, src_offset + element_size);
479 }
480 }
481
482 /*
483 * Return the vbuffer info and setup user-space attrib info, if needed.
484 */
485 if (vpv->num_inputs == 0) {
486 /* just defensive coding here */
487 vbuffer->buffer = NULL;
488 vbuffer->buffer_offset = 0;
489 vbuffer->stride = 0;
490 st->num_user_attribs = 0;
491 }
492 else if (usingVBO) {
493 /* all interleaved arrays in a VBO */
494 struct st_buffer_object *stobj = st_buffer_object(bufobj);
495
496 if (!stobj || !stobj->buffer) {
497 /* probably out of memory (or zero-sized buffer) */
498 return GL_FALSE;
499 }
500
501 vbuffer->buffer = NULL;
502 pipe_resource_reference(&vbuffer->buffer, stobj->buffer);
503 vbuffer->buffer_offset = pointer_to_offset(low_addr);
504 vbuffer->stride = stride;
505 st->num_user_attribs = 0;
506 }
507 else {
508 /* all interleaved arrays in user memory */
509 vbuffer->buffer = pipe_user_buffer_create(pipe->screen,
510 (void*) low_addr,
511 user_buffer_size,
512 PIPE_BIND_VERTEX_BUFFER);
513 vbuffer->buffer_offset = 0;
514 vbuffer->stride = stride;
515
516 /* Track user vertex buffers. */
517 pipe_resource_reference(&st->user_attrib[0].buffer, vbuffer->buffer);
518 st->user_attrib[0].element_size = vertex_size;
519 st->user_attrib[0].stride = stride;
520 st->num_user_attribs = 1;
521 }
522
523 return GL_TRUE;
524 }
525
526
527 /**
528 * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
529 * vertex attribute.
530 * \param vbuffer returns vertex buffer info
531 * \param velements returns vertex element info
532 * \return GL_TRUE for success, GL_FALSE otherwise (probably out of memory)
533 */
534 static GLboolean
535 setup_non_interleaved_attribs(struct gl_context *ctx,
536 const struct st_vertex_program *vp,
537 const struct st_vp_variant *vpv,
538 const struct gl_client_array **arrays,
539 struct pipe_vertex_buffer vbuffer[],
540 struct pipe_vertex_element velements[],
541 unsigned max_index,
542 unsigned num_instances)
543 {
544 struct st_context *st = st_context(ctx);
545 struct pipe_context *pipe = st->pipe;
546 GLuint attr;
547
548 for (attr = 0; attr < vpv->num_inputs; attr++) {
549 const GLuint mesaAttr = vp->index_to_input[attr];
550 const struct gl_client_array *array = arrays[mesaAttr];
551 struct gl_buffer_object *bufobj = array->BufferObj;
552 GLuint element_size = array->_ElementSize;
553 GLsizei stride = array->StrideB;
554
555 assert(element_size == array->Size * _mesa_sizeof_type(array->Type));
556
557 if (_mesa_is_bufferobj(bufobj)) {
558 /* Attribute data is in a VBO.
559 * Recall that for VBOs, the gl_client_array->Ptr field is
560 * really an offset from the start of the VBO, not a pointer.
561 */
562 struct st_buffer_object *stobj = st_buffer_object(bufobj);
563
564 if (!stobj || !stobj->buffer) {
565 /* probably out of memory (or zero-sized buffer) */
566 return GL_FALSE;
567 }
568
569 vbuffer[attr].buffer = NULL;
570 pipe_resource_reference(&vbuffer[attr].buffer, stobj->buffer);
571 vbuffer[attr].buffer_offset = pointer_to_offset(array->Ptr);
572 }
573 else {
574 /* wrap user data */
575 uint bytes;
576 void *ptr;
577
578 if (array->Ptr) {
579 uint divisor = array->InstanceDivisor;
580 uint last_index = divisor ? num_instances / divisor : max_index;
581
582 bytes = stride * last_index + element_size;
583
584 ptr = (void *) array->Ptr;
585 }
586 else {
587 /* no array, use ctx->Current.Attrib[] value */
588 bytes = element_size = sizeof(ctx->Current.Attrib[0]);
589 ptr = (void *) ctx->Current.Attrib[mesaAttr];
590 stride = 0;
591 }
592
593 assert(ptr);
594 assert(bytes);
595
596 vbuffer[attr].buffer =
597 pipe_user_buffer_create(pipe->screen, ptr, bytes,
598 PIPE_BIND_VERTEX_BUFFER);
599
600 vbuffer[attr].buffer_offset = 0;
601
602 /* Track user vertex buffers. */
603 pipe_resource_reference(&st->user_attrib[attr].buffer, vbuffer[attr].buffer);
604 st->user_attrib[attr].element_size = element_size;
605 st->user_attrib[attr].stride = stride;
606 st->num_user_attribs = MAX2(st->num_user_attribs, attr + 1);
607
608 if (!vbuffer[attr].buffer) {
609 /* probably ran out of memory */
610 return GL_FALSE;
611 }
612 }
613
614 /* common-case setup */
615 vbuffer[attr].stride = stride; /* in bytes */
616
617 velements[attr].src_offset = 0;
618 velements[attr].instance_divisor = array->InstanceDivisor;
619 velements[attr].vertex_buffer_index = attr;
620 velements[attr].src_format = st_pipe_vertex_format(array->Type,
621 array->Size,
622 array->Format,
623 array->Normalized,
624 array->Integer);
625 assert(velements[attr].src_format);
626 }
627
628 return GL_TRUE;
629 }
630
631
632 static void
633 setup_index_buffer(struct st_context *st,
634 const struct _mesa_index_buffer *ib,
635 struct pipe_index_buffer *ibuffer)
636 {
637 struct pipe_context *pipe = st->pipe;
638 struct gl_buffer_object *bufobj = ib->obj;
639
640 ibuffer->index_size = vbo_sizeof_ib_type(ib->type);
641
642 /* get/create the index buffer object */
643 if (_mesa_is_bufferobj(bufobj)) {
644 /* indices are in a real VBO */
645 struct st_buffer_object *stobj = st_buffer_object(bufobj);
646 pipe_resource_reference(&ibuffer->buffer, stobj->buffer);
647 ibuffer->offset = pointer_to_offset(ib->ptr);
648 }
649 else if (st->indexbuf_uploader) {
650 u_upload_data(st->indexbuf_uploader, 0, ib->count * ibuffer->index_size,
651 ib->ptr, &ibuffer->offset, &ibuffer->buffer);
652 }
653 else {
654 /* indices are in user space memory */
655 ibuffer->buffer =
656 pipe_user_buffer_create(pipe->screen, (void *) ib->ptr,
657 ib->count * ibuffer->index_size,
658 PIPE_BIND_INDEX_BUFFER);
659 }
660
661 cso_set_index_buffer(st->cso_context, ibuffer);
662 }
663
664
665 /**
666 * Prior to drawing, check that any uniforms referenced by the
667 * current shader have been set. If a uniform has not been set,
668 * issue a warning.
669 */
670 static void
671 check_uniforms(struct gl_context *ctx)
672 {
673 struct gl_shader_program *shProg[3] = {
674 ctx->Shader.CurrentVertexProgram,
675 ctx->Shader.CurrentGeometryProgram,
676 ctx->Shader.CurrentFragmentProgram,
677 };
678 unsigned j;
679
680 for (j = 0; j < 3; j++) {
681 unsigned i;
682
683 if (shProg[j] == NULL || !shProg[j]->LinkStatus)
684 continue;
685
686 for (i = 0; i < shProg[j]->NumUserUniformStorage; i++) {
687 const struct gl_uniform_storage *u = &shProg[j]->UniformStorage[i];
688 if (!u->initialized) {
689 _mesa_warning(ctx,
690 "Using shader with uninitialized uniform: %s",
691 u->name);
692 }
693 }
694 }
695 }
696
697
698 /*
699 * Notes on primitive restart:
700 * The code below is used when the gallium driver does not support primitive
701 * restart itself. We map the index buffer, find the restart indexes, unmap
702 * the index buffer then draw the sub-primitives delineated by the restarts.
703 * A couple possible optimizations:
704 * 1. Save the list of sub-primitive (start, count) values in a list attached
705 * to the index buffer for re-use in subsequent draws. The list would be
706 * invalidated when the contents of the buffer changed.
707 * 2. If drawing triangle strips or quad strips, create a new index buffer
708 * that uses duplicated vertices to render the disjoint strips as one
709 * long strip. We'd have to be careful to avoid using too much memory
710 * for this.
711 * Finally, some apps might perform better if they don't use primitive restart
712 * at all rather than this fallback path. Set MESA_EXTENSION_OVERRIDE to
713 * "-GL_NV_primitive_restart" to test that.
714 */
715
716
717 struct sub_primitive
718 {
719 unsigned start, count;
720 };
721
722
723 /**
724 * Scan the elements array to find restart indexes. Return a list
725 * of primitive (start,count) pairs to indicate how to draw the sub-
726 * primitives delineated by the restart index.
727 */
728 static struct sub_primitive *
729 find_sub_primitives(const void *elements, unsigned element_size,
730 unsigned start, unsigned end, unsigned restart_index,
731 unsigned *num_sub_prims)
732 {
733 const unsigned max_prims = end - start;
734 struct sub_primitive *sub_prims;
735 unsigned i, cur_start, cur_count, num;
736
737 sub_prims = (struct sub_primitive *)
738 malloc(max_prims * sizeof(struct sub_primitive));
739
740 if (!sub_prims) {
741 *num_sub_prims = 0;
742 return NULL;
743 }
744
745 cur_start = start;
746 cur_count = 0;
747 num = 0;
748
749 #define SCAN_ELEMENTS(TYPE) \
750 for (i = start; i < end; i++) { \
751 if (((const TYPE *) elements)[i] == restart_index) { \
752 if (cur_count > 0) { \
753 assert(num < max_prims); \
754 sub_prims[num].start = cur_start; \
755 sub_prims[num].count = cur_count; \
756 num++; \
757 } \
758 cur_start = i + 1; \
759 cur_count = 0; \
760 } \
761 else { \
762 cur_count++; \
763 } \
764 } \
765 if (cur_count > 0) { \
766 assert(num < max_prims); \
767 sub_prims[num].start = cur_start; \
768 sub_prims[num].count = cur_count; \
769 num++; \
770 }
771
772 switch (element_size) {
773 case 1:
774 SCAN_ELEMENTS(ubyte);
775 break;
776 case 2:
777 SCAN_ELEMENTS(ushort);
778 break;
779 case 4:
780 SCAN_ELEMENTS(uint);
781 break;
782 default:
783 assert(0 && "bad index_size in find_sub_primitives()");
784 }
785
786 #undef SCAN_ELEMENTS
787
788 *num_sub_prims = num;
789
790 return sub_prims;
791 }
792
793
794 /**
795 * For gallium drivers that don't support the primitive restart
796 * feature, handle it here by breaking up the indexed primitive into
797 * sub-primitives.
798 */
799 static void
800 handle_fallback_primitive_restart(struct cso_context *cso,
801 struct pipe_context *pipe,
802 const struct _mesa_index_buffer *ib,
803 struct pipe_index_buffer *ibuffer,
804 struct pipe_draw_info *orig_info)
805 {
806 const unsigned start = orig_info->start;
807 const unsigned count = orig_info->count;
808 struct pipe_draw_info info = *orig_info;
809 struct pipe_transfer *transfer = NULL;
810 unsigned instance, i;
811 const void *ptr = NULL;
812 struct sub_primitive *sub_prims;
813 unsigned num_sub_prims;
814
815 assert(info.indexed);
816 assert(ibuffer->buffer);
817 assert(ib);
818
819 if (!ibuffer->buffer || !ib)
820 return;
821
822 info.primitive_restart = FALSE;
823 info.instance_count = 1;
824
825 if (_mesa_is_bufferobj(ib->obj)) {
826 ptr = pipe_buffer_map_range(pipe, ibuffer->buffer,
827 start * ibuffer->index_size, /* start */
828 count * ibuffer->index_size, /* length */
829 PIPE_TRANSFER_READ, &transfer);
830 if (!ptr)
831 return;
832
833 ptr = (uint8_t*)ptr + (ibuffer->offset - start * ibuffer->index_size);
834 }
835 else {
836 ptr = ib->ptr;
837 if (!ptr)
838 return;
839 }
840
841 sub_prims = find_sub_primitives(ptr, ibuffer->index_size,
842 0, count, orig_info->restart_index,
843 &num_sub_prims);
844
845 if (transfer)
846 pipe_buffer_unmap(pipe, transfer);
847
848 /* Now draw the sub primitives.
849 * Need to loop over instances as well to preserve draw order.
850 */
851 for (instance = 0; instance < orig_info->instance_count; instance++) {
852 info.start_instance = instance + orig_info->start_instance;
853 for (i = 0; i < num_sub_prims; i++) {
854 info.start = sub_prims[i].start;
855 info.count = sub_prims[i].count;
856 if (u_trim_pipe_prim(info.mode, &info.count)) {
857 cso_draw_vbo(cso, &info);
858 }
859 }
860 }
861
862 if (sub_prims)
863 free(sub_prims);
864 }
865
866
867 /**
868 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
869 * the corresponding Gallium type.
870 */
871 static unsigned
872 translate_prim(const struct gl_context *ctx, unsigned prim)
873 {
874 /* GL prims should match Gallium prims, spot-check a few */
875 assert(GL_POINTS == PIPE_PRIM_POINTS);
876 assert(GL_QUADS == PIPE_PRIM_QUADS);
877 assert(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
878
879 /* Avoid quadstrips if it's easy to do so:
880 * Note: it's important to do the correct trimming if we change the
881 * prim type! We do that wherever this function is called.
882 */
883 if (prim == GL_QUAD_STRIP &&
884 ctx->Light.ShadeModel != GL_FLAT &&
885 ctx->Polygon.FrontMode == GL_FILL &&
886 ctx->Polygon.BackMode == GL_FILL)
887 prim = GL_TRIANGLE_STRIP;
888
889 return prim;
890 }
891
892
893 /**
894 * Setup vertex arrays and buffers prior to drawing.
895 * \return GL_TRUE for success, GL_FALSE otherwise (probably out of memory)
896 */
897 static GLboolean
898 st_validate_varrays(struct gl_context *ctx,
899 const struct gl_client_array **arrays,
900 unsigned max_index,
901 unsigned num_instances)
902 {
903 struct st_context *st = st_context(ctx);
904 const struct st_vertex_program *vp;
905 const struct st_vp_variant *vpv;
906 struct pipe_vertex_buffer vbuffer[PIPE_MAX_SHADER_INPUTS];
907 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
908 unsigned num_vbuffers, num_velements;
909 GLuint attr;
910 unsigned i;
911
912 /* must get these after state validation! */
913 vp = st->vp;
914 vpv = st->vp_variant;
915
916 memset(velements, 0, sizeof(struct pipe_vertex_element) * vpv->num_inputs);
917
918 /* Unreference any user vertex buffers. */
919 for (i = 0; i < st->num_user_attribs; i++) {
920 pipe_resource_reference(&st->user_attrib[i].buffer, NULL);
921 }
922 st->num_user_attribs = 0;
923
924 /*
925 * Setup the vbuffer[] and velements[] arrays.
926 */
927 if (is_interleaved_arrays(vp, vpv, arrays)) {
928 if (!setup_interleaved_attribs(ctx, vp, vpv, arrays, vbuffer, velements,
929 max_index, num_instances)) {
930 return GL_FALSE;
931 }
932
933 num_vbuffers = 1;
934 num_velements = vpv->num_inputs;
935 if (num_velements == 0)
936 num_vbuffers = 0;
937 }
938 else {
939 if (!setup_non_interleaved_attribs(ctx, vp, vpv, arrays,
940 vbuffer, velements, max_index,
941 num_instances)) {
942 return GL_FALSE;
943 }
944
945 num_vbuffers = vpv->num_inputs;
946 num_velements = vpv->num_inputs;
947 }
948
949 cso_set_vertex_buffers(st->cso_context, num_vbuffers, vbuffer);
950 cso_set_vertex_elements(st->cso_context, num_velements, velements);
951
952 /* unreference buffers (frees wrapped user-space buffer objects)
953 * This is OK, because the pipe driver should reference buffers by itself
954 * in set_vertex_buffers. */
955 for (attr = 0; attr < num_vbuffers; attr++) {
956 pipe_resource_reference(&vbuffer[attr].buffer, NULL);
957 assert(!vbuffer[attr].buffer);
958 }
959
960 return GL_TRUE;
961 }
962
963
964 /**
965 * This function gets plugged into the VBO module and is called when
966 * we have something to render.
967 * Basically, translate the information into the format expected by gallium.
968 */
969 void
970 st_draw_vbo(struct gl_context *ctx,
971 const struct gl_client_array **arrays,
972 const struct _mesa_prim *prims,
973 GLuint nr_prims,
974 const struct _mesa_index_buffer *ib,
975 GLboolean index_bounds_valid,
976 GLuint min_index,
977 GLuint max_index,
978 struct gl_transform_feedback_object *tfb_vertcount)
979 {
980 struct st_context *st = st_context(ctx);
981 struct pipe_context *pipe = st->pipe;
982 struct pipe_index_buffer ibuffer = {0};
983 struct pipe_draw_info info;
984 unsigned i, num_instances = 1;
985 unsigned max_index_plus_base;
986 GLboolean new_array =
987 st->dirty.st &&
988 (st->dirty.mesa & (_NEW_ARRAY | _NEW_PROGRAM | _NEW_BUFFER_OBJECT)) != 0;
989
990 /* Mesa core state should have been validated already */
991 assert(ctx->NewState == 0x0);
992
993 if (ib) {
994 int max_base_vertex = 0;
995
996 /* Gallium probably doesn't want this in some cases. */
997 if (!index_bounds_valid)
998 if (!all_varyings_in_vbos(arrays))
999 vbo_get_minmax_indices(ctx, prims, ib, &min_index, &max_index,
1000 nr_prims);
1001
1002 for (i = 0; i < nr_prims; i++) {
1003 num_instances = MAX2(num_instances, prims[i].num_instances);
1004 max_base_vertex = MAX2(max_base_vertex, prims[i].basevertex);
1005 }
1006
1007 /* Compute the sum of max_index and max_base_vertex. That's the value
1008 * we need to use when creating buffers.
1009 */
1010 if (max_index == ~0)
1011 max_index_plus_base = max_index;
1012 else
1013 max_index_plus_base = max_index + max_base_vertex;
1014 }
1015 else {
1016 /* Get min/max index for non-indexed drawing. */
1017 min_index = ~0;
1018 max_index = 0;
1019
1020 for (i = 0; i < nr_prims; i++) {
1021 min_index = MIN2(min_index, prims[i].start);
1022 max_index = MAX2(max_index, prims[i].start + prims[i].count - 1);
1023 num_instances = MAX2(num_instances, prims[i].num_instances);
1024 }
1025
1026 /* The base vertex offset only applies to indexed drawing */
1027 max_index_plus_base = max_index;
1028 }
1029
1030 /* Validate state. */
1031 if (st->dirty.st) {
1032 GLboolean vertDataEdgeFlags;
1033
1034 vertDataEdgeFlags = arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj &&
1035 arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj->Name;
1036 if (vertDataEdgeFlags != st->vertdata_edgeflags) {
1037 st->vertdata_edgeflags = vertDataEdgeFlags;
1038 st->dirty.st |= ST_NEW_EDGEFLAGS_DATA;
1039 }
1040
1041 st_validate_state(st);
1042
1043 if (new_array) {
1044 if (!st_validate_varrays(ctx, arrays, max_index_plus_base,
1045 num_instances)) {
1046 /* probably out of memory, no-op the draw call */
1047 return;
1048 }
1049 }
1050
1051 #if 0
1052 if (MESA_VERBOSE & VERBOSE_GLSL) {
1053 check_uniforms(ctx);
1054 }
1055 #else
1056 (void) check_uniforms;
1057 #endif
1058 }
1059
1060 /* Notify the driver that the content of user buffers may have been
1061 * changed. */
1062 assert(max_index >= min_index);
1063 if (!new_array && st->num_user_attribs) {
1064 for (i = 0; i < st->num_user_attribs; i++) {
1065 if (st->user_attrib[i].buffer) {
1066 unsigned element_size = st->user_attrib[i].element_size;
1067 unsigned stride = st->user_attrib[i].stride;
1068 unsigned min_offset = min_index * stride;
1069 unsigned max_offset = max_index_plus_base * stride + element_size;
1070
1071 assert(max_offset > min_offset);
1072
1073 pipe->redefine_user_buffer(pipe, st->user_attrib[i].buffer,
1074 min_offset,
1075 max_offset - min_offset);
1076 }
1077 }
1078 }
1079
1080 util_draw_init_info(&info);
1081 if (ib) {
1082 setup_index_buffer(st, ib, &ibuffer);
1083
1084 info.indexed = TRUE;
1085 if (min_index != ~0 && max_index != ~0) {
1086 info.min_index = min_index;
1087 info.max_index = max_index;
1088 }
1089
1090 /* The VBO module handles restart for the non-indexed GLDrawArrays
1091 * so we only set these fields for indexed drawing:
1092 */
1093 info.primitive_restart = ctx->Array.PrimitiveRestart;
1094 info.restart_index = ctx->Array.RestartIndex;
1095 }
1096
1097 /* Set info.count_from_stream_output. */
1098 if (tfb_vertcount) {
1099 st_transform_feedback_draw_init(tfb_vertcount, &info);
1100 }
1101
1102 /* do actual drawing */
1103 for (i = 0; i < nr_prims; i++) {
1104 info.mode = translate_prim( ctx, prims[i].mode );
1105 info.start = prims[i].start;
1106 info.count = prims[i].count;
1107 info.instance_count = prims[i].num_instances;
1108 info.index_bias = prims[i].basevertex;
1109 if (!ib) {
1110 info.min_index = info.start;
1111 info.max_index = info.start + info.count - 1;
1112 }
1113
1114 if (info.count_from_stream_output) {
1115 cso_draw_vbo(st->cso_context, &info);
1116 }
1117 else if (info.primitive_restart) {
1118 if (st->sw_primitive_restart) {
1119 /* Handle primitive restart for drivers that doesn't support it */
1120 handle_fallback_primitive_restart(st->cso_context, pipe, ib,
1121 &ibuffer, &info);
1122 }
1123 else {
1124 /* don't trim, restarts might be inside index list */
1125 cso_draw_vbo(st->cso_context, &info);
1126 }
1127 }
1128 else if (u_trim_pipe_prim(info.mode, &info.count))
1129 cso_draw_vbo(st->cso_context, &info);
1130 }
1131
1132 pipe_resource_reference(&ibuffer.buffer, NULL);
1133 }
1134
1135
1136 void
1137 st_init_draw(struct st_context *st)
1138 {
1139 struct gl_context *ctx = st->ctx;
1140
1141 vbo_set_draw_func(ctx, st_draw_vbo);
1142
1143 #if FEATURE_feedback || FEATURE_rastpos
1144 st->draw = draw_create(st->pipe); /* for selection/feedback */
1145
1146 /* Disable draw options that might convert points/lines to tris, etc.
1147 * as that would foul-up feedback/selection mode.
1148 */
1149 draw_wide_line_threshold(st->draw, 1000.0f);
1150 draw_wide_point_threshold(st->draw, 1000.0f);
1151 draw_enable_line_stipple(st->draw, FALSE);
1152 draw_enable_point_sprites(st->draw, FALSE);
1153 #endif
1154 }
1155
1156
1157 void
1158 st_destroy_draw(struct st_context *st)
1159 {
1160 #if FEATURE_feedback || FEATURE_rastpos
1161 draw_destroy(st->draw);
1162 #endif
1163 }