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