st/mesa: use SINT/UINT formats for VertexAttribIPointer
[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 }
828 else {
829 ptr = ib->ptr;
830 }
831
832 if (!ptr)
833 return;
834
835 ptr = ADD_POINTERS(ptr, ibuffer->offset);
836
837 sub_prims = find_sub_primitives(ptr, ibuffer->index_size,
838 0, count, orig_info->restart_index,
839 &num_sub_prims);
840
841 if (transfer)
842 pipe_buffer_unmap(pipe, transfer);
843
844 /* Now draw the sub primitives.
845 * Need to loop over instances as well to preserve draw order.
846 */
847 for (instance = 0; instance < orig_info->instance_count; instance++) {
848 info.start_instance = instance + orig_info->start_instance;
849 for (i = 0; i < num_sub_prims; i++) {
850 info.start = sub_prims[i].start;
851 info.count = sub_prims[i].count;
852 if (u_trim_pipe_prim(info.mode, &info.count)) {
853 pipe->draw_vbo(pipe, &info);
854 }
855 }
856 }
857
858 if (sub_prims)
859 free(sub_prims);
860 }
861
862
863 /**
864 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
865 * the corresponding Gallium type.
866 */
867 static unsigned
868 translate_prim(const struct gl_context *ctx, unsigned prim)
869 {
870 /* GL prims should match Gallium prims, spot-check a few */
871 assert(GL_POINTS == PIPE_PRIM_POINTS);
872 assert(GL_QUADS == PIPE_PRIM_QUADS);
873 assert(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
874
875 /* Avoid quadstrips if it's easy to do so:
876 * Note: it's important to do the correct trimming if we change the
877 * prim type! We do that wherever this function is called.
878 */
879 if (prim == GL_QUAD_STRIP &&
880 ctx->Light.ShadeModel != GL_FLAT &&
881 ctx->Polygon.FrontMode == GL_FILL &&
882 ctx->Polygon.BackMode == GL_FILL)
883 prim = GL_TRIANGLE_STRIP;
884
885 return prim;
886 }
887
888
889 /**
890 * Setup vertex arrays and buffers prior to drawing.
891 * \return GL_TRUE for success, GL_FALSE otherwise (probably out of memory)
892 */
893 static GLboolean
894 st_validate_varrays(struct gl_context *ctx,
895 const struct gl_client_array **arrays,
896 unsigned max_index,
897 unsigned num_instances)
898 {
899 struct st_context *st = st_context(ctx);
900 const struct st_vertex_program *vp;
901 const struct st_vp_variant *vpv;
902 struct pipe_vertex_buffer vbuffer[PIPE_MAX_SHADER_INPUTS];
903 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
904 unsigned num_vbuffers, num_velements;
905 GLuint attr;
906 unsigned i;
907
908 /* must get these after state validation! */
909 vp = st->vp;
910 vpv = st->vp_variant;
911
912 memset(velements, 0, sizeof(struct pipe_vertex_element) * vpv->num_inputs);
913
914 /* Unreference any user vertex buffers. */
915 for (i = 0; i < st->num_user_attribs; i++) {
916 pipe_resource_reference(&st->user_attrib[i].buffer, NULL);
917 }
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 return GL_TRUE;
957 }
958
959
960 /**
961 * This function gets plugged into the VBO module and is called when
962 * we have something to render.
963 * Basically, translate the information into the format expected by gallium.
964 */
965 void
966 st_draw_vbo(struct gl_context *ctx,
967 const struct gl_client_array **arrays,
968 const struct _mesa_prim *prims,
969 GLuint nr_prims,
970 const struct _mesa_index_buffer *ib,
971 GLboolean index_bounds_valid,
972 GLuint min_index,
973 GLuint max_index,
974 struct gl_transform_feedback_object *tfb_vertcount)
975 {
976 struct st_context *st = st_context(ctx);
977 struct pipe_context *pipe = st->pipe;
978 struct pipe_index_buffer ibuffer;
979 struct pipe_draw_info info;
980 unsigned i, num_instances = 1;
981 GLboolean new_array =
982 st->dirty.st &&
983 (st->dirty.mesa & (_NEW_ARRAY | _NEW_PROGRAM | _NEW_BUFFER_OBJECT)) != 0;
984
985 /* Mesa core state should have been validated already */
986 assert(ctx->NewState == 0x0);
987
988 if (ib) {
989 /* Gallium probably doesn't want this in some cases. */
990 if (!index_bounds_valid)
991 if (!all_varyings_in_vbos(arrays))
992 vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
993
994 for (i = 0; i < nr_prims; i++) {
995 num_instances = MAX2(num_instances, prims[i].num_instances);
996 }
997 }
998 else {
999 /* Get min/max index for non-indexed drawing. */
1000 min_index = ~0;
1001 max_index = 0;
1002
1003 for (i = 0; i < nr_prims; i++) {
1004 min_index = MIN2(min_index, prims[i].start);
1005 max_index = MAX2(max_index, prims[i].start + prims[i].count - 1);
1006 num_instances = MAX2(num_instances, prims[i].num_instances);
1007 }
1008 }
1009
1010 /* Validate state. */
1011 if (st->dirty.st) {
1012 GLboolean vertDataEdgeFlags;
1013
1014 /* sanity check for pointer arithmetic below */
1015 assert(sizeof(arrays[0]->Ptr[0]) == 1);
1016
1017 vertDataEdgeFlags = arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj &&
1018 arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj->Name;
1019 if (vertDataEdgeFlags != st->vertdata_edgeflags) {
1020 st->vertdata_edgeflags = vertDataEdgeFlags;
1021 st->dirty.st |= ST_NEW_EDGEFLAGS_DATA;
1022 }
1023
1024 st_validate_state(st);
1025
1026 if (new_array) {
1027 if (!st_validate_varrays(ctx, arrays, max_index, num_instances)) {
1028 /* probably out of memory, no-op the draw call */
1029 return;
1030 }
1031 }
1032
1033 #if 0
1034 if (MESA_VERBOSE & VERBOSE_GLSL) {
1035 check_uniforms(ctx);
1036 }
1037 #else
1038 (void) check_uniforms;
1039 #endif
1040 }
1041
1042 /* Notify the driver that the content of user buffers may have been
1043 * changed. */
1044 assert(max_index >= min_index);
1045 if (!new_array && st->num_user_attribs) {
1046 for (i = 0; i < st->num_user_attribs; i++) {
1047 if (st->user_attrib[i].buffer) {
1048 unsigned element_size = st->user_attrib[i].element_size;
1049 unsigned stride = st->user_attrib[i].stride;
1050 unsigned min_offset = min_index * stride;
1051 unsigned max_offset = max_index * stride + element_size;
1052
1053 assert(max_offset > min_offset);
1054
1055 pipe->redefine_user_buffer(pipe, st->user_attrib[i].buffer,
1056 min_offset,
1057 max_offset - min_offset);
1058 }
1059 }
1060 }
1061
1062 setup_index_buffer(ctx, ib, &ibuffer);
1063 pipe->set_index_buffer(pipe, &ibuffer);
1064
1065 util_draw_init_info(&info);
1066 if (ib) {
1067 info.indexed = TRUE;
1068 if (min_index != ~0 && max_index != ~0) {
1069 info.min_index = min_index;
1070 info.max_index = max_index;
1071 }
1072
1073 /* The VBO module handles restart for the non-indexed GLDrawArrays
1074 * so we only set these fields for indexed drawing:
1075 */
1076 info.primitive_restart = ctx->Array.PrimitiveRestart;
1077 info.restart_index = ctx->Array.RestartIndex;
1078 }
1079
1080 /* Set info.count_from_stream_output. */
1081 if (tfb_vertcount) {
1082 st_transform_feedback_draw_init(tfb_vertcount, &info);
1083 }
1084
1085 /* do actual drawing */
1086 for (i = 0; i < nr_prims; i++) {
1087 info.mode = translate_prim( ctx, prims[i].mode );
1088 info.start = prims[i].start;
1089 info.count = prims[i].count;
1090 info.instance_count = prims[i].num_instances;
1091 info.index_bias = prims[i].basevertex;
1092 if (!ib) {
1093 info.min_index = info.start;
1094 info.max_index = info.start + info.count - 1;
1095 }
1096
1097 if (info.count_from_stream_output) {
1098 pipe->draw_vbo(pipe, &info);
1099 }
1100 else if (info.primitive_restart) {
1101 if (st->sw_primitive_restart) {
1102 /* Handle primitive restart for drivers that doesn't support it */
1103 handle_fallback_primitive_restart(pipe, ib, &ibuffer, &info);
1104 }
1105 else {
1106 /* don't trim, restarts might be inside index list */
1107 pipe->draw_vbo(pipe, &info);
1108 }
1109 }
1110 else if (u_trim_pipe_prim(info.mode, &info.count))
1111 pipe->draw_vbo(pipe, &info);
1112 }
1113
1114 pipe_resource_reference(&ibuffer.buffer, NULL);
1115 }
1116
1117
1118 void
1119 st_init_draw(struct st_context *st)
1120 {
1121 struct gl_context *ctx = st->ctx;
1122
1123 vbo_set_draw_func(ctx, st_draw_vbo);
1124
1125 #if FEATURE_feedback || FEATURE_rastpos
1126 st->draw = draw_create(st->pipe); /* for selection/feedback */
1127
1128 /* Disable draw options that might convert points/lines to tris, etc.
1129 * as that would foul-up feedback/selection mode.
1130 */
1131 draw_wide_line_threshold(st->draw, 1000.0f);
1132 draw_wide_point_threshold(st->draw, 1000.0f);
1133 draw_enable_line_stipple(st->draw, FALSE);
1134 draw_enable_point_sprites(st->draw, FALSE);
1135 #endif
1136 }
1137
1138
1139 void
1140 st_destroy_draw(struct st_context *st)
1141 {
1142 #if FEATURE_feedback || FEATURE_rastpos
1143 draw_destroy(st->draw);
1144 #endif
1145 }