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"
54 #include "st_cb_xformfb.h"
56 #include "st_program.h"
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"
67 #include "../glsl/ir_uniform.h"
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
189 static GLuint ubyte_types_int
[4] = {
191 PIPE_FORMAT_R8G8_UINT
,
192 PIPE_FORMAT_R8G8B8_UINT
,
193 PIPE_FORMAT_R8G8B8A8_UINT
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
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
210 static GLuint byte_types_int
[4] = {
212 PIPE_FORMAT_R8G8_SINT
,
213 PIPE_FORMAT_R8G8B8_SINT
,
214 PIPE_FORMAT_R8G8B8A8_SINT
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
227 * Return a PIPE_FORMAT_x for the given GL datatype and size.
230 st_pipe_vertex_format(GLenum type
, GLuint size
, GLenum format
,
231 GLboolean normalized
, GLboolean integer
)
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
);
239 assert(format
== GL_RGBA
|| format
== GL_BGRA
);
241 if (type
== GL_INT_2_10_10_10_REV
||
242 type
== GL_UNSIGNED_INT_2_10_10_10_REV
) {
246 if (format
== GL_BGRA
) {
247 if (type
== GL_INT_2_10_10_10_REV
) {
249 return PIPE_FORMAT_B10G10R10A2_SNORM
;
251 return PIPE_FORMAT_B10G10R10A2_SSCALED
;
254 return PIPE_FORMAT_B10G10R10A2_UNORM
;
256 return PIPE_FORMAT_B10G10R10A2_USCALED
;
259 if (type
== GL_INT_2_10_10_10_REV
) {
261 return PIPE_FORMAT_R10G10B10A2_SNORM
;
263 return PIPE_FORMAT_R10G10B10A2_SSCALED
;
266 return PIPE_FORMAT_R10G10B10A2_UNORM
;
268 return PIPE_FORMAT_R10G10B10A2_USCALED
;
273 if (format
== GL_BGRA
) {
274 /* this is an odd-ball case */
275 assert(type
== GL_UNSIGNED_BYTE
);
277 return PIPE_FORMAT_B8G8R8A8_UNORM
;
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;
291 else if (normalized
) {
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;
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;
321 return PIPE_FORMAT_NONE
; /* silence compiler warning */
326 * This is very similar to vbo_all_varyings_in_vbos() but we are
327 * only interested in per-vertex data. See bug 38626.
330 all_varyings_in_vbos(const struct gl_client_array
*arrays
[])
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
))
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.
349 is_interleaved_arrays(const struct st_vertex_program
*vp
,
350 const struct st_vp_variant
*vpv
,
351 const struct gl_client_array
**arrays
)
354 const struct gl_buffer_object
*firstBufObj
= NULL
;
355 GLint firstStride
= -1;
356 const GLubyte
*firstPtr
= NULL
;
357 GLboolean userSpaceBuffer
= GL_FALSE
;
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 */
366 /* save info about the first array */
367 firstStride
= stride
;
368 firstPtr
= array
->Ptr
;
369 firstBufObj
= bufObj
;
370 userSpaceBuffer
= !bufObj
|| !bufObj
->Name
;
373 /* check if other arrays interleave with the first, in same buffer */
374 if (stride
!= firstStride
)
375 return GL_FALSE
; /* strides don't match */
377 if (bufObj
!= firstBufObj
)
378 return GL_FALSE
; /* arrays in different VBOs */
380 if (abs(array
->Ptr
- firstPtr
) > firstStride
)
381 return GL_FALSE
; /* arrays start too far apart */
383 if ((!_mesa_is_bufferobj(bufObj
)) != userSpaceBuffer
)
384 return GL_FALSE
; /* mix of VBO and user-space arrays */
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)
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
[],
407 unsigned num_instances
)
409 struct st_context
*st
= st_context(ctx
);
410 struct pipe_context
*pipe
= st
->pipe
;
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 */
419 /* Find the lowest address of the arrays we're drawing,
420 * Init bufobj and stride.
422 if (vpv
->num_inputs
) {
423 const GLuint mesaAttr0
= vp
->index_to_input
[0];
424 const struct gl_client_array
*array
= arrays
[mesaAttr0
];
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.
430 bufobj
= array
->BufferObj
;
431 stride
= array
->StrideB
;
433 low_addr
= arrays
[vp
->index_to_input
[0]]->Ptr
;
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
);
441 /* not sure we'll ever have zero inputs, but play it safe */
447 /* are the arrays in user space? */
448 usingVBO
= _mesa_is_bufferobj(bufobj
);
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
;
456 assert(element_size
== array
->Size
* _mesa_sizeof_type(array
->Type
));
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
,
466 assert(velements
[attr
].src_format
);
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
;
474 user_buffer_size
= MAX2(user_buffer_size
, bytes
);
476 /* update vertex size */
477 vertex_size
= MAX2(vertex_size
, src_offset
+ element_size
);
482 * Return the vbuffer info and setup user-space attrib info, if needed.
484 if (vpv
->num_inputs
== 0) {
485 /* just defensive coding here */
486 vbuffer
->buffer
= NULL
;
487 vbuffer
->buffer_offset
= 0;
489 st
->num_user_attribs
= 0;
492 /* all interleaved arrays in a VBO */
493 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
495 if (!stobj
|| !stobj
->buffer
) {
496 /* probably out of memory (or zero-sized buffer) */
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;
507 /* all interleaved arrays in user memory */
508 vbuffer
->buffer
= pipe_user_buffer_create(pipe
->screen
,
511 PIPE_BIND_VERTEX_BUFFER
);
512 vbuffer
->buffer_offset
= 0;
513 vbuffer
->stride
= stride
;
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;
527 * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
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)
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
[],
541 unsigned num_instances
)
543 struct st_context
*st
= st_context(ctx
);
544 struct pipe_context
*pipe
= st
->pipe
;
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
;
554 assert(element_size
== array
->Size
* _mesa_sizeof_type(array
->Type
));
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.
561 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
563 if (!stobj
|| !stobj
->buffer
) {
564 /* probably out of memory (or zero-sized buffer) */
568 vbuffer
[attr
].buffer
= NULL
;
569 pipe_resource_reference(&vbuffer
[attr
].buffer
, stobj
->buffer
);
570 vbuffer
[attr
].buffer_offset
= pointer_to_offset(array
->Ptr
);
578 uint divisor
= array
->InstanceDivisor
;
579 uint last_index
= divisor
? num_instances
/ divisor
: max_index
;
581 bytes
= stride
* last_index
+ element_size
;
583 ptr
= (void *) array
->Ptr
;
586 /* no array, use ctx->Current.Attrib[] value */
587 bytes
= element_size
= sizeof(ctx
->Current
.Attrib
[0]);
588 ptr
= (void *) ctx
->Current
.Attrib
[mesaAttr
];
595 vbuffer
[attr
].buffer
=
596 pipe_user_buffer_create(pipe
->screen
, ptr
, bytes
,
597 PIPE_BIND_VERTEX_BUFFER
);
599 vbuffer
[attr
].buffer_offset
= 0;
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);
607 if (!vbuffer
[attr
].buffer
) {
608 /* probably ran out of memory */
613 /* common-case setup */
614 vbuffer
[attr
].stride
= stride
; /* in bytes */
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
,
624 assert(velements
[attr
].src_format
);
632 setup_index_buffer(struct gl_context
*ctx
,
633 const struct _mesa_index_buffer
*ib
,
634 struct pipe_index_buffer
*ibuffer
)
636 struct st_context
*st
= st_context(ctx
);
637 struct pipe_context
*pipe
= st
->pipe
;
639 memset(ibuffer
, 0, sizeof(*ibuffer
));
641 struct gl_buffer_object
*bufobj
= ib
->obj
;
643 ibuffer
->index_size
= vbo_sizeof_ib_type(ib
->type
);
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
);
653 /* element/indicies are in user space memory */
655 pipe_user_buffer_create(pipe
->screen
, (void *) ib
->ptr
,
656 ib
->count
* ibuffer
->index_size
,
657 PIPE_BIND_INDEX_BUFFER
);
664 * Prior to drawing, check that any uniforms referenced by the
665 * current shader have been set. If a uniform has not been set,
669 check_uniforms(struct gl_context
*ctx
)
671 struct gl_shader_program
*shProg
[3] = {
672 ctx
->Shader
.CurrentVertexProgram
,
673 ctx
->Shader
.CurrentGeometryProgram
,
674 ctx
->Shader
.CurrentFragmentProgram
,
678 for (j
= 0; j
< 3; j
++) {
681 if (shProg
[j
] == NULL
|| !shProg
[j
]->LinkStatus
)
684 for (i
= 0; i
< shProg
[j
]->NumUserUniformStorage
; i
++) {
685 const struct gl_uniform_storage
*u
= &shProg
[j
]->UniformStorage
[i
];
686 if (!u
->initialized
) {
688 "Using shader with uninitialized uniform: %s",
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
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.
717 unsigned start
, count
;
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.
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
)
731 const unsigned max_prims
= end
- start
;
732 struct sub_primitive
*sub_prims
;
733 unsigned i
, cur_start
, cur_count
, num
;
735 sub_prims
= (struct sub_primitive
*)
736 malloc(max_prims
* sizeof(struct sub_primitive
));
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; \
763 if (cur_count > 0) { \
764 assert(num < max_prims); \
765 sub_prims[num].start = cur_start; \
766 sub_prims[num].count = cur_count; \
770 switch (element_size
) {
772 SCAN_ELEMENTS(ubyte
);
775 SCAN_ELEMENTS(ushort
);
781 assert(0 && "bad index_size in find_sub_primitives()");
786 *num_sub_prims
= num
;
793 * For gallium drivers that don't support the primitive restart
794 * feature, handle it here by breaking up the indexed primitive into
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
)
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
;
812 assert(info
.indexed
);
813 assert(ibuffer
->buffer
);
816 if (!ibuffer
->buffer
|| !ib
)
819 info
.primitive_restart
= FALSE
;
820 info
.instance_count
= 1;
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
);
830 ptr
= (uint8_t*)ptr
+ (ibuffer
->offset
- start
* ibuffer
->index_size
);
838 sub_prims
= find_sub_primitives(ptr
, ibuffer
->index_size
,
839 0, count
, orig_info
->restart_index
,
843 pipe_buffer_unmap(pipe
, transfer
);
845 /* Now draw the sub primitives.
846 * Need to loop over instances as well to preserve draw order.
848 for (instance
= 0; instance
< orig_info
->instance_count
; instance
++) {
849 info
.start_instance
= instance
+ orig_info
->start_instance
;
850 for (i
= 0; i
< num_sub_prims
; i
++) {
851 info
.start
= sub_prims
[i
].start
;
852 info
.count
= sub_prims
[i
].count
;
853 if (u_trim_pipe_prim(info
.mode
, &info
.count
)) {
854 pipe
->draw_vbo(pipe
, &info
);
865 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
866 * the corresponding Gallium type.
869 translate_prim(const struct gl_context
*ctx
, unsigned prim
)
871 /* GL prims should match Gallium prims, spot-check a few */
872 assert(GL_POINTS
== PIPE_PRIM_POINTS
);
873 assert(GL_QUADS
== PIPE_PRIM_QUADS
);
874 assert(GL_TRIANGLE_STRIP_ADJACENCY
== PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
);
876 /* Avoid quadstrips if it's easy to do so:
877 * Note: it's important to do the correct trimming if we change the
878 * prim type! We do that wherever this function is called.
880 if (prim
== GL_QUAD_STRIP
&&
881 ctx
->Light
.ShadeModel
!= GL_FLAT
&&
882 ctx
->Polygon
.FrontMode
== GL_FILL
&&
883 ctx
->Polygon
.BackMode
== GL_FILL
)
884 prim
= GL_TRIANGLE_STRIP
;
891 * Setup vertex arrays and buffers prior to drawing.
892 * \return GL_TRUE for success, GL_FALSE otherwise (probably out of memory)
895 st_validate_varrays(struct gl_context
*ctx
,
896 const struct gl_client_array
**arrays
,
898 unsigned num_instances
)
900 struct st_context
*st
= st_context(ctx
);
901 const struct st_vertex_program
*vp
;
902 const struct st_vp_variant
*vpv
;
903 struct pipe_vertex_buffer vbuffer
[PIPE_MAX_SHADER_INPUTS
];
904 struct pipe_vertex_element velements
[PIPE_MAX_ATTRIBS
];
905 unsigned num_vbuffers
, num_velements
;
909 /* must get these after state validation! */
911 vpv
= st
->vp_variant
;
913 memset(velements
, 0, sizeof(struct pipe_vertex_element
) * vpv
->num_inputs
);
915 /* Unreference any user vertex buffers. */
916 for (i
= 0; i
< st
->num_user_attribs
; i
++) {
917 pipe_resource_reference(&st
->user_attrib
[i
].buffer
, NULL
);
919 st
->num_user_attribs
= 0;
922 * Setup the vbuffer[] and velements[] arrays.
924 if (is_interleaved_arrays(vp
, vpv
, arrays
)) {
925 if (!setup_interleaved_attribs(ctx
, vp
, vpv
, arrays
, vbuffer
, velements
,
926 max_index
, num_instances
)) {
931 num_velements
= vpv
->num_inputs
;
932 if (num_velements
== 0)
936 if (!setup_non_interleaved_attribs(ctx
, vp
, vpv
, arrays
,
937 vbuffer
, velements
, max_index
,
942 num_vbuffers
= vpv
->num_inputs
;
943 num_velements
= vpv
->num_inputs
;
946 cso_set_vertex_buffers(st
->cso_context
, num_vbuffers
, vbuffer
);
947 cso_set_vertex_elements(st
->cso_context
, num_velements
, velements
);
949 /* unreference buffers (frees wrapped user-space buffer objects)
950 * This is OK, because the pipe driver should reference buffers by itself
951 * in set_vertex_buffers. */
952 for (attr
= 0; attr
< num_vbuffers
; attr
++) {
953 pipe_resource_reference(&vbuffer
[attr
].buffer
, NULL
);
954 assert(!vbuffer
[attr
].buffer
);
962 * This function gets plugged into the VBO module and is called when
963 * we have something to render.
964 * Basically, translate the information into the format expected by gallium.
967 st_draw_vbo(struct gl_context
*ctx
,
968 const struct gl_client_array
**arrays
,
969 const struct _mesa_prim
*prims
,
971 const struct _mesa_index_buffer
*ib
,
972 GLboolean index_bounds_valid
,
975 struct gl_transform_feedback_object
*tfb_vertcount
)
977 struct st_context
*st
= st_context(ctx
);
978 struct pipe_context
*pipe
= st
->pipe
;
979 struct pipe_index_buffer ibuffer
;
980 struct pipe_draw_info info
;
981 unsigned i
, num_instances
= 1;
982 GLboolean new_array
=
984 (st
->dirty
.mesa
& (_NEW_ARRAY
| _NEW_PROGRAM
| _NEW_BUFFER_OBJECT
)) != 0;
986 /* Mesa core state should have been validated already */
987 assert(ctx
->NewState
== 0x0);
990 /* Gallium probably doesn't want this in some cases. */
991 if (!index_bounds_valid
)
992 if (!all_varyings_in_vbos(arrays
))
993 vbo_get_minmax_index(ctx
, prims
, ib
, &min_index
, &max_index
);
995 for (i
= 0; i
< nr_prims
; i
++) {
996 num_instances
= MAX2(num_instances
, prims
[i
].num_instances
);
1000 /* Get min/max index for non-indexed drawing. */
1004 for (i
= 0; i
< nr_prims
; i
++) {
1005 min_index
= MIN2(min_index
, prims
[i
].start
);
1006 max_index
= MAX2(max_index
, prims
[i
].start
+ prims
[i
].count
- 1);
1007 num_instances
= MAX2(num_instances
, prims
[i
].num_instances
);
1011 /* Validate state. */
1013 GLboolean vertDataEdgeFlags
;
1015 /* sanity check for pointer arithmetic below */
1016 assert(sizeof(arrays
[0]->Ptr
[0]) == 1);
1018 vertDataEdgeFlags
= arrays
[VERT_ATTRIB_EDGEFLAG
]->BufferObj
&&
1019 arrays
[VERT_ATTRIB_EDGEFLAG
]->BufferObj
->Name
;
1020 if (vertDataEdgeFlags
!= st
->vertdata_edgeflags
) {
1021 st
->vertdata_edgeflags
= vertDataEdgeFlags
;
1022 st
->dirty
.st
|= ST_NEW_EDGEFLAGS_DATA
;
1025 st_validate_state(st
);
1028 if (!st_validate_varrays(ctx
, arrays
, max_index
, num_instances
)) {
1029 /* probably out of memory, no-op the draw call */
1035 if (MESA_VERBOSE
& VERBOSE_GLSL
) {
1036 check_uniforms(ctx
);
1039 (void) check_uniforms
;
1043 /* Notify the driver that the content of user buffers may have been
1045 assert(max_index
>= min_index
);
1046 if (!new_array
&& st
->num_user_attribs
) {
1047 for (i
= 0; i
< st
->num_user_attribs
; i
++) {
1048 if (st
->user_attrib
[i
].buffer
) {
1049 unsigned element_size
= st
->user_attrib
[i
].element_size
;
1050 unsigned stride
= st
->user_attrib
[i
].stride
;
1051 unsigned min_offset
= min_index
* stride
;
1052 unsigned max_offset
= max_index
* stride
+ element_size
;
1054 assert(max_offset
> min_offset
);
1056 pipe
->redefine_user_buffer(pipe
, st
->user_attrib
[i
].buffer
,
1058 max_offset
- min_offset
);
1063 setup_index_buffer(ctx
, ib
, &ibuffer
);
1064 pipe
->set_index_buffer(pipe
, &ibuffer
);
1066 util_draw_init_info(&info
);
1068 info
.indexed
= TRUE
;
1069 if (min_index
!= ~0 && max_index
!= ~0) {
1070 info
.min_index
= min_index
;
1071 info
.max_index
= max_index
;
1074 /* The VBO module handles restart for the non-indexed GLDrawArrays
1075 * so we only set these fields for indexed drawing:
1077 info
.primitive_restart
= ctx
->Array
.PrimitiveRestart
;
1078 info
.restart_index
= ctx
->Array
.RestartIndex
;
1081 /* Set info.count_from_stream_output. */
1082 if (tfb_vertcount
) {
1083 st_transform_feedback_draw_init(tfb_vertcount
, &info
);
1086 /* do actual drawing */
1087 for (i
= 0; i
< nr_prims
; i
++) {
1088 info
.mode
= translate_prim( ctx
, prims
[i
].mode
);
1089 info
.start
= prims
[i
].start
;
1090 info
.count
= prims
[i
].count
;
1091 info
.instance_count
= prims
[i
].num_instances
;
1092 info
.index_bias
= prims
[i
].basevertex
;
1094 info
.min_index
= info
.start
;
1095 info
.max_index
= info
.start
+ info
.count
- 1;
1098 if (info
.count_from_stream_output
) {
1099 pipe
->draw_vbo(pipe
, &info
);
1101 else if (info
.primitive_restart
) {
1102 if (st
->sw_primitive_restart
) {
1103 /* Handle primitive restart for drivers that doesn't support it */
1104 handle_fallback_primitive_restart(pipe
, ib
, &ibuffer
, &info
);
1107 /* don't trim, restarts might be inside index list */
1108 pipe
->draw_vbo(pipe
, &info
);
1111 else if (u_trim_pipe_prim(info
.mode
, &info
.count
))
1112 pipe
->draw_vbo(pipe
, &info
);
1115 pipe_resource_reference(&ibuffer
.buffer
, NULL
);
1120 st_init_draw(struct st_context
*st
)
1122 struct gl_context
*ctx
= st
->ctx
;
1124 vbo_set_draw_func(ctx
, st_draw_vbo
);
1126 #if FEATURE_feedback || FEATURE_rastpos
1127 st
->draw
= draw_create(st
->pipe
); /* for selection/feedback */
1129 /* Disable draw options that might convert points/lines to tris, etc.
1130 * as that would foul-up feedback/selection mode.
1132 draw_wide_line_threshold(st
->draw
, 1000.0f
);
1133 draw_wide_point_threshold(st
->draw
, 1000.0f
);
1134 draw_enable_line_stipple(st
->draw
, FALSE
);
1135 draw_enable_point_sprites(st
->draw
, FALSE
);
1141 st_destroy_draw(struct st_context
*st
)
1143 #if FEATURE_feedback || FEATURE_rastpos
1144 draw_destroy(st
->draw
);