1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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.
34 * We basically convert the VBO's vertex attribute/array information into
35 * Gallium vertex state, bind the vertex buffer objects and call
39 * Keith Whitwell <keith@tungstengraphics.com>
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"
51 #include "st_context.h"
53 #include "st_cb_bufferobjects.h"
55 #include "st_program.h"
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"
66 #include "../glsl/ir_uniform.h"
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
184 * Return a PIPE_FORMAT_x for the given GL datatype and size.
187 st_pipe_vertex_format(GLenum type
, GLuint size
, GLenum format
,
188 GLboolean normalized
)
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
);
196 assert(format
== GL_RGBA
|| format
== GL_BGRA
);
198 if (type
== GL_INT_2_10_10_10_REV
||
199 type
== GL_UNSIGNED_INT_2_10_10_10_REV
) {
202 if (format
== GL_BGRA
) {
203 if (type
== GL_INT_2_10_10_10_REV
) {
205 return PIPE_FORMAT_B10G10R10A2_SNORM
;
207 return PIPE_FORMAT_B10G10R10A2_SSCALED
;
210 return PIPE_FORMAT_B10G10R10A2_UNORM
;
212 return PIPE_FORMAT_B10G10R10A2_USCALED
;
215 if (type
== GL_INT_2_10_10_10_REV
) {
217 return PIPE_FORMAT_R10G10B10A2_SNORM
;
219 return PIPE_FORMAT_R10G10B10A2_SSCALED
;
222 return PIPE_FORMAT_R10G10B10A2_UNORM
;
224 return PIPE_FORMAT_R10G10B10A2_USCALED
;
229 if (format
== GL_BGRA
) {
230 /* this is an odd-ball case */
231 assert(type
== GL_UNSIGNED_BYTE
);
233 return PIPE_FORMAT_B8G8R8A8_UNORM
;
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;
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;
266 return PIPE_FORMAT_NONE
; /* silence compiler warning */
271 * This is very similar to vbo_all_varyings_in_vbos() but we are
272 * only interested in per-vertex data. See bug 38626.
275 all_varyings_in_vbos(const struct gl_client_array
*arrays
[])
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
))
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.
294 is_interleaved_arrays(const struct st_vertex_program
*vp
,
295 const struct st_vp_variant
*vpv
,
296 const struct gl_client_array
**arrays
)
299 const struct gl_buffer_object
*firstBufObj
= NULL
;
300 GLint firstStride
= -1;
301 const GLubyte
*firstPtr
= NULL
;
302 GLboolean userSpaceBuffer
= GL_FALSE
;
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 */
311 /* save info about the first array */
312 firstStride
= stride
;
313 firstPtr
= array
->Ptr
;
314 firstBufObj
= bufObj
;
315 userSpaceBuffer
= !bufObj
|| !bufObj
->Name
;
318 /* check if other arrays interleave with the first, in same buffer */
319 if (stride
!= firstStride
)
320 return GL_FALSE
; /* strides don't match */
322 if (bufObj
!= firstBufObj
)
323 return GL_FALSE
; /* arrays in different VBOs */
325 if (abs(array
->Ptr
- firstPtr
) > firstStride
)
326 return GL_FALSE
; /* arrays start too far apart */
328 if ((!bufObj
|| !_mesa_is_bufferobj(bufObj
)) != userSpaceBuffer
)
329 return GL_FALSE
; /* mix of VBO and user-space arrays */
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)
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
[],
352 unsigned num_instances
)
354 struct st_context
*st
= st_context(ctx
);
355 struct pipe_context
*pipe
= st
->pipe
;
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 */
364 /* Find the lowest address of the arrays we're drawing,
365 * Init bufobj and stride.
367 if (vpv
->num_inputs
) {
368 const GLuint mesaAttr0
= vp
->index_to_input
[0];
369 const struct gl_client_array
*array
= arrays
[mesaAttr0
];
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.
375 bufobj
= array
->BufferObj
;
376 stride
= array
->StrideB
;
378 low_addr
= arrays
[vp
->index_to_input
[0]]->Ptr
;
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
);
386 /* not sure we'll ever have zero inputs, but play it safe */
392 /* are the arrays in user space? */
393 usingVBO
= bufobj
&& _mesa_is_bufferobj(bufobj
);
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
;
401 assert(element_size
== array
->Size
* _mesa_sizeof_type(array
->Type
));
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
,
410 assert(velements
[attr
].src_format
);
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
;
418 user_buffer_size
= MAX2(user_buffer_size
, bytes
);
420 /* update vertex size */
421 vertex_size
= MAX2(vertex_size
, src_offset
+ element_size
);
426 * Return the vbuffer info and setup user-space attrib info, if needed.
428 if (vpv
->num_inputs
== 0) {
429 /* just defensive coding here */
430 vbuffer
->buffer
= NULL
;
431 vbuffer
->buffer_offset
= 0;
433 st
->num_user_attribs
= 0;
436 /* all interleaved arrays in a VBO */
437 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
439 if (!stobj
|| !stobj
->buffer
) {
440 /* probably out of memory (or zero-sized buffer) */
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;
451 /* all interleaved arrays in user memory */
452 vbuffer
->buffer
= pipe_user_buffer_create(pipe
->screen
,
455 PIPE_BIND_VERTEX_BUFFER
);
456 vbuffer
->buffer_offset
= 0;
457 vbuffer
->stride
= stride
;
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;
471 * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
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)
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
[],
485 unsigned num_instances
)
487 struct st_context
*st
= st_context(ctx
);
488 struct pipe_context
*pipe
= st
->pipe
;
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
;
498 assert(element_size
== array
->Size
* _mesa_sizeof_type(array
->Type
));
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.
505 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
507 if (!stobj
|| !stobj
->buffer
) {
508 /* probably out of memory (or zero-sized buffer) */
512 vbuffer
[attr
].buffer
= NULL
;
513 pipe_resource_reference(&vbuffer
[attr
].buffer
, stobj
->buffer
);
514 vbuffer
[attr
].buffer_offset
= pointer_to_offset(array
->Ptr
);
522 uint divisor
= array
->InstanceDivisor
;
523 uint last_index
= divisor
? num_instances
/ divisor
: max_index
;
525 bytes
= stride
* last_index
+ element_size
;
527 ptr
= (void *) array
->Ptr
;
530 /* no array, use ctx->Current.Attrib[] value */
531 bytes
= element_size
= sizeof(ctx
->Current
.Attrib
[0]);
532 ptr
= (void *) ctx
->Current
.Attrib
[mesaAttr
];
539 vbuffer
[attr
].buffer
=
540 pipe_user_buffer_create(pipe
->screen
, ptr
, bytes
,
541 PIPE_BIND_VERTEX_BUFFER
);
543 vbuffer
[attr
].buffer_offset
= 0;
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);
551 if (!vbuffer
[attr
].buffer
) {
552 /* probably ran out of memory */
557 /* common-case setup */
558 vbuffer
[attr
].stride
= stride
; /* in bytes */
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
,
567 assert(velements
[attr
].src_format
);
575 setup_index_buffer(struct gl_context
*ctx
,
576 const struct _mesa_index_buffer
*ib
,
577 struct pipe_index_buffer
*ibuffer
)
579 struct st_context
*st
= st_context(ctx
);
580 struct pipe_context
*pipe
= st
->pipe
;
582 memset(ibuffer
, 0, sizeof(*ibuffer
));
584 struct gl_buffer_object
*bufobj
= ib
->obj
;
587 case GL_UNSIGNED_INT
:
588 ibuffer
->index_size
= 4;
590 case GL_UNSIGNED_SHORT
:
591 ibuffer
->index_size
= 2;
593 case GL_UNSIGNED_BYTE
:
594 ibuffer
->index_size
= 1;
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
);
609 /* element/indicies are in user space memory */
611 pipe_user_buffer_create(pipe
->screen
, (void *) ib
->ptr
,
612 ib
->count
* ibuffer
->index_size
,
613 PIPE_BIND_INDEX_BUFFER
);
620 * Prior to drawing, check that any uniforms referenced by the
621 * current shader have been set. If a uniform has not been set,
625 check_uniforms(struct gl_context
*ctx
)
627 struct gl_shader_program
*shProg
[3] = {
628 ctx
->Shader
.CurrentVertexProgram
,
629 ctx
->Shader
.CurrentGeometryProgram
,
630 ctx
->Shader
.CurrentFragmentProgram
,
634 for (j
= 0; j
< 3; j
++) {
637 if (shProg
[j
] == NULL
|| !shProg
[j
]->LinkStatus
)
640 for (i
= 0; i
< shProg
[j
]->NumUserUniformStorage
; i
++) {
641 const struct gl_uniform_storage
*u
= &shProg
[j
]->UniformStorage
[i
];
642 if (!u
->initialized
) {
644 "Using shader with uninitialized uniform: %s",
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
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.
673 unsigned start
, count
;
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.
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
)
687 const unsigned max_prims
= end
- start
;
688 struct sub_primitive
*sub_prims
;
689 unsigned i
, cur_start
, cur_count
, num
;
691 sub_prims
= (struct sub_primitive
*)
692 malloc(max_prims
* sizeof(struct sub_primitive
));
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; \
719 if (cur_count > 0) { \
720 assert(num < max_prims); \
721 sub_prims[num].start = cur_start; \
722 sub_prims[num].count = cur_count; \
726 switch (element_size
) {
728 SCAN_ELEMENTS(ubyte
);
731 SCAN_ELEMENTS(ushort
);
737 assert(0 && "bad index_size in find_sub_primitives()");
742 *num_sub_prims
= num
;
749 * For gallium drivers that don't support the primitive restart
750 * feature, handle it here by breaking up the indexed primitive into
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
)
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
;
768 assert(info
.indexed
);
769 assert(ibuffer
->buffer
);
772 if (!ibuffer
->buffer
|| !ib
)
775 info
.primitive_restart
= FALSE
;
776 info
.instance_count
= 1;
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
);
791 ptr
= ADD_POINTERS(ptr
, ibuffer
->offset
);
793 sub_prims
= find_sub_primitives(ptr
, ibuffer
->index_size
,
794 0, count
, orig_info
->restart_index
,
798 pipe_buffer_unmap(pipe
, transfer
);
800 /* Now draw the sub primitives.
801 * Need to loop over instances as well to preserve draw order.
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
);
820 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
821 * the corresponding Gallium type.
824 translate_prim(const struct gl_context
*ctx
, unsigned prim
)
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
);
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.
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
;
846 * Setup vertex arrays and buffers prior to drawing.
847 * \return GL_TRUE for success, GL_FALSE otherwise (probably out of memory)
850 st_validate_varrays(struct gl_context
*ctx
,
851 const struct gl_client_array
**arrays
,
853 unsigned num_instances
)
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
;
864 /* must get these after state validation! */
866 vpv
= st
->vp_variant
;
868 memset(velements
, 0, sizeof(struct pipe_vertex_element
) * vpv
->num_inputs
);
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
);
874 st
->num_user_attribs
= 0;
877 * Setup the vbuffer[] and velements[] arrays.
879 if (is_interleaved_arrays(vp
, vpv
, arrays
)) {
880 if (!setup_interleaved_attribs(ctx
, vp
, vpv
, arrays
, vbuffer
, velements
,
881 max_index
, num_instances
)) {
886 num_velements
= vpv
->num_inputs
;
887 if (num_velements
== 0)
891 if (!setup_non_interleaved_attribs(ctx
, vp
, vpv
, arrays
,
892 vbuffer
, velements
, max_index
,
897 num_vbuffers
= vpv
->num_inputs
;
898 num_velements
= vpv
->num_inputs
;
901 cso_set_vertex_buffers(st
->cso_context
, num_vbuffers
, vbuffer
);
902 cso_set_vertex_elements(st
->cso_context
, num_velements
, velements
);
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
);
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.
922 st_draw_vbo(struct gl_context
*ctx
,
923 const struct gl_client_array
**arrays
,
924 const struct _mesa_prim
*prims
,
926 const struct _mesa_index_buffer
*ib
,
927 GLboolean index_bounds_valid
,
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
=
938 (st
->dirty
.mesa
& (_NEW_ARRAY
| _NEW_PROGRAM
| _NEW_BUFFER_OBJECT
)) != 0;
940 /* Mesa core state should have been validated already */
941 assert(ctx
->NewState
== 0x0);
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
);
949 for (i
= 0; i
< nr_prims
; i
++) {
950 num_instances
= MAX2(num_instances
, prims
[i
].num_instances
);
954 /* Get min/max index for non-indexed drawing. */
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
);
965 /* Validate state. */
967 GLboolean vertDataEdgeFlags
;
969 /* sanity check for pointer arithmetic below */
970 assert(sizeof(arrays
[0]->Ptr
[0]) == 1);
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
;
979 st_validate_state(st
);
982 if (!st_validate_varrays(ctx
, arrays
, max_index
, num_instances
)) {
983 /* probably out of memory, no-op the draw call */
989 if (MESA_VERBOSE
& VERBOSE_GLSL
) {
993 (void) check_uniforms
;
997 /* Notify the driver that the content of user buffers may have been
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
;
1008 assert(max_offset
> min_offset
);
1010 pipe
->redefine_user_buffer(pipe
, st
->user_attrib
[i
].buffer
,
1012 max_offset
- min_offset
);
1017 setup_index_buffer(ctx
, ib
, &ibuffer
);
1018 pipe
->set_index_buffer(pipe
, &ibuffer
);
1020 util_draw_init_info(&info
);
1022 info
.indexed
= TRUE
;
1023 if (min_index
!= ~0 && max_index
!= ~0) {
1024 info
.min_index
= min_index
;
1025 info
.max_index
= max_index
;
1028 /* The VBO module handles restart for the non-indexed GLDrawArrays
1029 * so we only set these fields for indexed drawing:
1031 info
.primitive_restart
= ctx
->Array
.PrimitiveRestart
;
1032 info
.restart_index
= ctx
->Array
.RestartIndex
;
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
;
1043 info
.min_index
= info
.start
;
1044 info
.max_index
= info
.start
+ info
.count
- 1;
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
);
1053 /* don't trim, restarts might be inside index list */
1054 pipe
->draw_vbo(pipe
, &info
);
1057 else if (u_trim_pipe_prim(info
.mode
, &info
.count
))
1058 pipe
->draw_vbo(pipe
, &info
);
1061 pipe_resource_reference(&ibuffer
.buffer
, NULL
);
1066 st_init_draw(struct st_context
*st
)
1068 struct gl_context
*ctx
= st
->ctx
;
1070 vbo_set_draw_func(ctx
, st_draw_vbo
);
1072 #if FEATURE_feedback || FEATURE_rastpos
1073 st
->draw
= draw_create(st
->pipe
); /* for selection/feedback */
1075 /* Disable draw options that might convert points/lines to tris, etc.
1076 * as that would foul-up feedback/selection mode.
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
);
1087 st_destroy_draw(struct st_context
*st
)
1089 #if FEATURE_feedback || FEATURE_rastpos
1090 draw_destroy(st
->draw
);