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