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