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 "util/u_upload_mgr.h"
65 #include "draw/draw_context.h"
66 #include "cso_cache/cso_context.h"
68 #include "../glsl/ir_uniform.h"
71 static GLuint double_types
[4] = {
72 PIPE_FORMAT_R64_FLOAT
,
73 PIPE_FORMAT_R64G64_FLOAT
,
74 PIPE_FORMAT_R64G64B64_FLOAT
,
75 PIPE_FORMAT_R64G64B64A64_FLOAT
78 static GLuint float_types
[4] = {
79 PIPE_FORMAT_R32_FLOAT
,
80 PIPE_FORMAT_R32G32_FLOAT
,
81 PIPE_FORMAT_R32G32B32_FLOAT
,
82 PIPE_FORMAT_R32G32B32A32_FLOAT
85 static GLuint half_float_types
[4] = {
86 PIPE_FORMAT_R16_FLOAT
,
87 PIPE_FORMAT_R16G16_FLOAT
,
88 PIPE_FORMAT_R16G16B16_FLOAT
,
89 PIPE_FORMAT_R16G16B16A16_FLOAT
92 static GLuint uint_types_norm
[4] = {
93 PIPE_FORMAT_R32_UNORM
,
94 PIPE_FORMAT_R32G32_UNORM
,
95 PIPE_FORMAT_R32G32B32_UNORM
,
96 PIPE_FORMAT_R32G32B32A32_UNORM
99 static GLuint uint_types_scale
[4] = {
100 PIPE_FORMAT_R32_USCALED
,
101 PIPE_FORMAT_R32G32_USCALED
,
102 PIPE_FORMAT_R32G32B32_USCALED
,
103 PIPE_FORMAT_R32G32B32A32_USCALED
106 static GLuint uint_types_int
[4] = {
107 PIPE_FORMAT_R32_UINT
,
108 PIPE_FORMAT_R32G32_UINT
,
109 PIPE_FORMAT_R32G32B32_UINT
,
110 PIPE_FORMAT_R32G32B32A32_UINT
113 static GLuint int_types_norm
[4] = {
114 PIPE_FORMAT_R32_SNORM
,
115 PIPE_FORMAT_R32G32_SNORM
,
116 PIPE_FORMAT_R32G32B32_SNORM
,
117 PIPE_FORMAT_R32G32B32A32_SNORM
120 static GLuint int_types_scale
[4] = {
121 PIPE_FORMAT_R32_SSCALED
,
122 PIPE_FORMAT_R32G32_SSCALED
,
123 PIPE_FORMAT_R32G32B32_SSCALED
,
124 PIPE_FORMAT_R32G32B32A32_SSCALED
127 static GLuint int_types_int
[4] = {
128 PIPE_FORMAT_R32_SINT
,
129 PIPE_FORMAT_R32G32_SINT
,
130 PIPE_FORMAT_R32G32B32_SINT
,
131 PIPE_FORMAT_R32G32B32A32_SINT
134 static GLuint ushort_types_norm
[4] = {
135 PIPE_FORMAT_R16_UNORM
,
136 PIPE_FORMAT_R16G16_UNORM
,
137 PIPE_FORMAT_R16G16B16_UNORM
,
138 PIPE_FORMAT_R16G16B16A16_UNORM
141 static GLuint ushort_types_scale
[4] = {
142 PIPE_FORMAT_R16_USCALED
,
143 PIPE_FORMAT_R16G16_USCALED
,
144 PIPE_FORMAT_R16G16B16_USCALED
,
145 PIPE_FORMAT_R16G16B16A16_USCALED
148 static GLuint ushort_types_int
[4] = {
149 PIPE_FORMAT_R16_UINT
,
150 PIPE_FORMAT_R16G16_UINT
,
151 PIPE_FORMAT_R16G16B16_UINT
,
152 PIPE_FORMAT_R16G16B16A16_UINT
155 static GLuint short_types_norm
[4] = {
156 PIPE_FORMAT_R16_SNORM
,
157 PIPE_FORMAT_R16G16_SNORM
,
158 PIPE_FORMAT_R16G16B16_SNORM
,
159 PIPE_FORMAT_R16G16B16A16_SNORM
162 static GLuint short_types_scale
[4] = {
163 PIPE_FORMAT_R16_SSCALED
,
164 PIPE_FORMAT_R16G16_SSCALED
,
165 PIPE_FORMAT_R16G16B16_SSCALED
,
166 PIPE_FORMAT_R16G16B16A16_SSCALED
169 static GLuint short_types_int
[4] = {
170 PIPE_FORMAT_R16_SINT
,
171 PIPE_FORMAT_R16G16_SINT
,
172 PIPE_FORMAT_R16G16B16_SINT
,
173 PIPE_FORMAT_R16G16B16A16_SINT
176 static GLuint ubyte_types_norm
[4] = {
177 PIPE_FORMAT_R8_UNORM
,
178 PIPE_FORMAT_R8G8_UNORM
,
179 PIPE_FORMAT_R8G8B8_UNORM
,
180 PIPE_FORMAT_R8G8B8A8_UNORM
183 static GLuint ubyte_types_scale
[4] = {
184 PIPE_FORMAT_R8_USCALED
,
185 PIPE_FORMAT_R8G8_USCALED
,
186 PIPE_FORMAT_R8G8B8_USCALED
,
187 PIPE_FORMAT_R8G8B8A8_USCALED
190 static GLuint ubyte_types_int
[4] = {
192 PIPE_FORMAT_R8G8_UINT
,
193 PIPE_FORMAT_R8G8B8_UINT
,
194 PIPE_FORMAT_R8G8B8A8_UINT
197 static GLuint byte_types_norm
[4] = {
198 PIPE_FORMAT_R8_SNORM
,
199 PIPE_FORMAT_R8G8_SNORM
,
200 PIPE_FORMAT_R8G8B8_SNORM
,
201 PIPE_FORMAT_R8G8B8A8_SNORM
204 static GLuint byte_types_scale
[4] = {
205 PIPE_FORMAT_R8_SSCALED
,
206 PIPE_FORMAT_R8G8_SSCALED
,
207 PIPE_FORMAT_R8G8B8_SSCALED
,
208 PIPE_FORMAT_R8G8B8A8_SSCALED
211 static GLuint byte_types_int
[4] = {
213 PIPE_FORMAT_R8G8_SINT
,
214 PIPE_FORMAT_R8G8B8_SINT
,
215 PIPE_FORMAT_R8G8B8A8_SINT
218 static GLuint fixed_types
[4] = {
219 PIPE_FORMAT_R32_FIXED
,
220 PIPE_FORMAT_R32G32_FIXED
,
221 PIPE_FORMAT_R32G32B32_FIXED
,
222 PIPE_FORMAT_R32G32B32A32_FIXED
228 * Return a PIPE_FORMAT_x for the given GL datatype and size.
231 st_pipe_vertex_format(GLenum type
, GLuint size
, GLenum format
,
232 GLboolean normalized
, GLboolean integer
)
234 assert((type
>= GL_BYTE
&& type
<= GL_DOUBLE
) ||
235 type
== GL_FIXED
|| type
== GL_HALF_FLOAT
||
236 type
== GL_INT_2_10_10_10_REV
||
237 type
== GL_UNSIGNED_INT_2_10_10_10_REV
);
240 assert(format
== GL_RGBA
|| format
== GL_BGRA
);
242 if (type
== GL_INT_2_10_10_10_REV
||
243 type
== GL_UNSIGNED_INT_2_10_10_10_REV
) {
247 if (format
== GL_BGRA
) {
248 if (type
== GL_INT_2_10_10_10_REV
) {
250 return PIPE_FORMAT_B10G10R10A2_SNORM
;
252 return PIPE_FORMAT_B10G10R10A2_SSCALED
;
255 return PIPE_FORMAT_B10G10R10A2_UNORM
;
257 return PIPE_FORMAT_B10G10R10A2_USCALED
;
260 if (type
== GL_INT_2_10_10_10_REV
) {
262 return PIPE_FORMAT_R10G10B10A2_SNORM
;
264 return PIPE_FORMAT_R10G10B10A2_SSCALED
;
267 return PIPE_FORMAT_R10G10B10A2_UNORM
;
269 return PIPE_FORMAT_R10G10B10A2_USCALED
;
274 if (format
== GL_BGRA
) {
275 /* this is an odd-ball case */
276 assert(type
== GL_UNSIGNED_BYTE
);
278 return PIPE_FORMAT_B8G8R8A8_UNORM
;
283 case GL_INT
: return int_types_int
[size
-1];
284 case GL_SHORT
: return short_types_int
[size
-1];
285 case GL_BYTE
: return byte_types_int
[size
-1];
286 case GL_UNSIGNED_INT
: return uint_types_int
[size
-1];
287 case GL_UNSIGNED_SHORT
: return ushort_types_int
[size
-1];
288 case GL_UNSIGNED_BYTE
: return ubyte_types_int
[size
-1];
289 default: assert(0); return 0;
292 else if (normalized
) {
294 case GL_DOUBLE
: return double_types
[size
-1];
295 case GL_FLOAT
: return float_types
[size
-1];
296 case GL_HALF_FLOAT
: return half_float_types
[size
-1];
297 case GL_INT
: return int_types_norm
[size
-1];
298 case GL_SHORT
: return short_types_norm
[size
-1];
299 case GL_BYTE
: return byte_types_norm
[size
-1];
300 case GL_UNSIGNED_INT
: return uint_types_norm
[size
-1];
301 case GL_UNSIGNED_SHORT
: return ushort_types_norm
[size
-1];
302 case GL_UNSIGNED_BYTE
: return ubyte_types_norm
[size
-1];
303 case GL_FIXED
: return fixed_types
[size
-1];
304 default: assert(0); return 0;
309 case GL_DOUBLE
: return double_types
[size
-1];
310 case GL_FLOAT
: return float_types
[size
-1];
311 case GL_HALF_FLOAT
: return half_float_types
[size
-1];
312 case GL_INT
: return int_types_scale
[size
-1];
313 case GL_SHORT
: return short_types_scale
[size
-1];
314 case GL_BYTE
: return byte_types_scale
[size
-1];
315 case GL_UNSIGNED_INT
: return uint_types_scale
[size
-1];
316 case GL_UNSIGNED_SHORT
: return ushort_types_scale
[size
-1];
317 case GL_UNSIGNED_BYTE
: return ubyte_types_scale
[size
-1];
318 case GL_FIXED
: return fixed_types
[size
-1];
319 default: assert(0); return 0;
322 return PIPE_FORMAT_NONE
; /* silence compiler warning */
327 * This is very similar to vbo_all_varyings_in_vbos() but we are
328 * only interested in per-vertex data. See bug 38626.
331 all_varyings_in_vbos(const struct gl_client_array
*arrays
[])
335 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
336 if (arrays
[i
]->StrideB
&&
337 !arrays
[i
]->InstanceDivisor
&&
338 !_mesa_is_bufferobj(arrays
[i
]->BufferObj
))
346 * Examine the active arrays to determine if we have interleaved
347 * vertex arrays all living in one VBO, or all living in user space.
350 is_interleaved_arrays(const struct st_vertex_program
*vp
,
351 const struct st_vp_variant
*vpv
,
352 const struct gl_client_array
**arrays
)
355 const struct gl_buffer_object
*firstBufObj
= NULL
;
356 GLint firstStride
= -1;
357 const GLubyte
*firstPtr
= NULL
;
358 GLboolean userSpaceBuffer
= GL_FALSE
;
360 for (attr
= 0; attr
< vpv
->num_inputs
; attr
++) {
361 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
362 const struct gl_client_array
*array
= arrays
[mesaAttr
];
363 const struct gl_buffer_object
*bufObj
= array
->BufferObj
;
364 const GLsizei stride
= array
->StrideB
; /* in bytes */
367 /* save info about the first array */
368 firstStride
= stride
;
369 firstPtr
= array
->Ptr
;
370 firstBufObj
= bufObj
;
371 userSpaceBuffer
= !bufObj
|| !bufObj
->Name
;
374 /* check if other arrays interleave with the first, in same buffer */
375 if (stride
!= firstStride
)
376 return GL_FALSE
; /* strides don't match */
378 if (bufObj
!= firstBufObj
)
379 return GL_FALSE
; /* arrays in different VBOs */
381 if (abs(array
->Ptr
- firstPtr
) > firstStride
)
382 return GL_FALSE
; /* arrays start too far apart */
384 if ((!_mesa_is_bufferobj(bufObj
)) != userSpaceBuffer
)
385 return GL_FALSE
; /* mix of VBO and user-space arrays */
394 * Set up for drawing interleaved arrays that all live in one VBO
395 * or all live in user space.
396 * \param vbuffer returns vertex buffer info
397 * \param velements returns vertex element info
398 * \return GL_TRUE for success, GL_FALSE otherwise (probably out of memory)
401 setup_interleaved_attribs(struct gl_context
*ctx
,
402 const struct st_vertex_program
*vp
,
403 const struct st_vp_variant
*vpv
,
404 const struct gl_client_array
**arrays
,
405 struct pipe_vertex_buffer
*vbuffer
,
406 struct pipe_vertex_element velements
[])
409 const GLubyte
*low_addr
= NULL
;
410 GLboolean usingVBO
; /* all arrays in a VBO? */
411 struct gl_buffer_object
*bufobj
;
414 /* Find the lowest address of the arrays we're drawing,
415 * Init bufobj and stride.
417 if (vpv
->num_inputs
) {
418 const GLuint mesaAttr0
= vp
->index_to_input
[0];
419 const struct gl_client_array
*array
= arrays
[mesaAttr0
];
421 /* Since we're doing interleaved arrays, we know there'll be at most
422 * one buffer object and the stride will be the same for all arrays.
425 bufobj
= array
->BufferObj
;
426 stride
= array
->StrideB
;
428 low_addr
= arrays
[vp
->index_to_input
[0]]->Ptr
;
430 for (attr
= 1; attr
< vpv
->num_inputs
; attr
++) {
431 const GLubyte
*start
= arrays
[vp
->index_to_input
[attr
]]->Ptr
;
432 low_addr
= MIN2(low_addr
, start
);
436 /* not sure we'll ever have zero inputs, but play it safe */
442 /* are the arrays in user space? */
443 usingVBO
= _mesa_is_bufferobj(bufobj
);
445 for (attr
= 0; attr
< vpv
->num_inputs
; attr
++) {
446 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
447 const struct gl_client_array
*array
= arrays
[mesaAttr
];
448 unsigned src_offset
= (unsigned) (array
->Ptr
- low_addr
);
449 GLuint element_size
= array
->_ElementSize
;
451 assert(element_size
== array
->Size
* _mesa_sizeof_type(array
->Type
));
453 velements
[attr
].src_offset
= src_offset
;
454 velements
[attr
].instance_divisor
= array
->InstanceDivisor
;
455 velements
[attr
].vertex_buffer_index
= 0;
456 velements
[attr
].src_format
= st_pipe_vertex_format(array
->Type
,
461 assert(velements
[attr
].src_format
);
465 * Return the vbuffer info and setup user-space attrib info, if needed.
467 if (vpv
->num_inputs
== 0) {
468 /* just defensive coding here */
469 vbuffer
->buffer
= NULL
;
470 vbuffer
->user_buffer
= NULL
;
471 vbuffer
->buffer_offset
= 0;
475 /* all interleaved arrays in a VBO */
476 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
478 if (!stobj
|| !stobj
->buffer
) {
479 /* probably out of memory (or zero-sized buffer) */
483 vbuffer
->buffer
= stobj
->buffer
;
484 vbuffer
->user_buffer
= NULL
;
485 vbuffer
->buffer_offset
= pointer_to_offset(low_addr
);
486 vbuffer
->stride
= stride
;
489 /* all interleaved arrays in user memory */
490 vbuffer
->buffer
= NULL
;
491 vbuffer
->user_buffer
= low_addr
;
492 vbuffer
->buffer_offset
= 0;
493 vbuffer
->stride
= stride
;
501 * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
503 * \param vbuffer returns vertex buffer info
504 * \param velements returns vertex element info
505 * \return GL_TRUE for success, GL_FALSE otherwise (probably out of memory)
508 setup_non_interleaved_attribs(struct gl_context
*ctx
,
509 const struct st_vertex_program
*vp
,
510 const struct st_vp_variant
*vpv
,
511 const struct gl_client_array
**arrays
,
512 struct pipe_vertex_buffer vbuffer
[],
513 struct pipe_vertex_element velements
[])
517 for (attr
= 0; attr
< vpv
->num_inputs
; attr
++) {
518 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
519 const struct gl_client_array
*array
= arrays
[mesaAttr
];
520 struct gl_buffer_object
*bufobj
= array
->BufferObj
;
521 GLsizei stride
= array
->StrideB
;
523 assert(array
->_ElementSize
== array
->Size
* _mesa_sizeof_type(array
->Type
));
525 if (_mesa_is_bufferobj(bufobj
)) {
526 /* Attribute data is in a VBO.
527 * Recall that for VBOs, the gl_client_array->Ptr field is
528 * really an offset from the start of the VBO, not a pointer.
530 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
532 if (!stobj
|| !stobj
->buffer
) {
533 /* probably out of memory (or zero-sized buffer) */
537 vbuffer
[attr
].buffer
= stobj
->buffer
;
538 vbuffer
[attr
].user_buffer
= NULL
;
539 vbuffer
[attr
].buffer_offset
= pointer_to_offset(array
->Ptr
);
546 ptr
= (void *) array
->Ptr
;
549 /* no array, use ctx->Current.Attrib[] value */
550 ptr
= (void *) ctx
->Current
.Attrib
[mesaAttr
];
556 vbuffer
[attr
].buffer
= NULL
;
557 vbuffer
[attr
].user_buffer
= ptr
;
558 vbuffer
[attr
].buffer_offset
= 0;
561 /* common-case setup */
562 vbuffer
[attr
].stride
= stride
; /* in bytes */
564 velements
[attr
].src_offset
= 0;
565 velements
[attr
].instance_divisor
= array
->InstanceDivisor
;
566 velements
[attr
].vertex_buffer_index
= attr
;
567 velements
[attr
].src_format
= st_pipe_vertex_format(array
->Type
,
572 assert(velements
[attr
].src_format
);
580 setup_index_buffer(struct st_context
*st
,
581 const struct _mesa_index_buffer
*ib
,
582 struct pipe_index_buffer
*ibuffer
)
584 struct gl_buffer_object
*bufobj
= ib
->obj
;
586 ibuffer
->index_size
= vbo_sizeof_ib_type(ib
->type
);
588 /* get/create the index buffer object */
589 if (_mesa_is_bufferobj(bufobj
)) {
590 /* indices are in a real VBO */
591 ibuffer
->buffer
= st_buffer_object(bufobj
)->buffer
;
592 ibuffer
->offset
= pointer_to_offset(ib
->ptr
);
594 else if (st
->indexbuf_uploader
) {
595 u_upload_data(st
->indexbuf_uploader
, 0, ib
->count
* ibuffer
->index_size
,
596 ib
->ptr
, &ibuffer
->offset
, &ibuffer
->buffer
);
597 u_upload_unmap(st
->indexbuf_uploader
);
600 /* indices are in user space memory */
601 ibuffer
->user_buffer
= ib
->ptr
;
604 cso_set_index_buffer(st
->cso_context
, ibuffer
);
609 * Prior to drawing, check that any uniforms referenced by the
610 * current shader have been set. If a uniform has not been set,
614 check_uniforms(struct gl_context
*ctx
)
616 struct gl_shader_program
*shProg
[3] = {
617 ctx
->Shader
.CurrentVertexProgram
,
618 ctx
->Shader
.CurrentGeometryProgram
,
619 ctx
->Shader
.CurrentFragmentProgram
,
623 for (j
= 0; j
< 3; j
++) {
626 if (shProg
[j
] == NULL
|| !shProg
[j
]->LinkStatus
)
629 for (i
= 0; i
< shProg
[j
]->NumUserUniformStorage
; i
++) {
630 const struct gl_uniform_storage
*u
= &shProg
[j
]->UniformStorage
[i
];
631 if (!u
->initialized
) {
633 "Using shader with uninitialized uniform: %s",
642 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
643 * the corresponding Gallium type.
646 translate_prim(const struct gl_context
*ctx
, unsigned prim
)
648 /* GL prims should match Gallium prims, spot-check a few */
649 assert(GL_POINTS
== PIPE_PRIM_POINTS
);
650 assert(GL_QUADS
== PIPE_PRIM_QUADS
);
651 assert(GL_TRIANGLE_STRIP_ADJACENCY
== PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
);
653 /* Avoid quadstrips if it's easy to do so:
654 * Note: it's important to do the correct trimming if we change the
655 * prim type! We do that wherever this function is called.
657 if (prim
== GL_QUAD_STRIP
&&
658 ctx
->Light
.ShadeModel
!= GL_FLAT
&&
659 ctx
->Polygon
.FrontMode
== GL_FILL
&&
660 ctx
->Polygon
.BackMode
== GL_FILL
)
661 prim
= GL_TRIANGLE_STRIP
;
668 * Setup vertex arrays and buffers prior to drawing.
669 * \return GL_TRUE for success, GL_FALSE otherwise (probably out of memory)
672 st_validate_varrays(struct gl_context
*ctx
,
673 const struct gl_client_array
**arrays
)
675 struct st_context
*st
= st_context(ctx
);
676 const struct st_vertex_program
*vp
;
677 const struct st_vp_variant
*vpv
;
678 struct pipe_vertex_buffer vbuffer
[PIPE_MAX_SHADER_INPUTS
];
679 struct pipe_vertex_element velements
[PIPE_MAX_ATTRIBS
];
680 unsigned num_vbuffers
, num_velements
;
682 /* must get these after state validation! */
684 vpv
= st
->vp_variant
;
686 memset(velements
, 0, sizeof(struct pipe_vertex_element
) * vpv
->num_inputs
);
689 * Setup the vbuffer[] and velements[] arrays.
691 if (is_interleaved_arrays(vp
, vpv
, arrays
)) {
692 if (!setup_interleaved_attribs(ctx
, vp
, vpv
, arrays
, vbuffer
,
698 num_velements
= vpv
->num_inputs
;
699 if (num_velements
== 0)
703 if (!setup_non_interleaved_attribs(ctx
, vp
, vpv
, arrays
,
704 vbuffer
, velements
)) {
708 num_vbuffers
= vpv
->num_inputs
;
709 num_velements
= vpv
->num_inputs
;
712 cso_set_vertex_buffers(st
->cso_context
, num_vbuffers
, vbuffer
);
713 cso_set_vertex_elements(st
->cso_context
, num_velements
, velements
);
720 * This function gets plugged into the VBO module and is called when
721 * we have something to render.
722 * Basically, translate the information into the format expected by gallium.
725 st_draw_vbo(struct gl_context
*ctx
,
726 const struct _mesa_prim
*prims
,
728 const struct _mesa_index_buffer
*ib
,
729 GLboolean index_bounds_valid
,
732 struct gl_transform_feedback_object
*tfb_vertcount
)
734 struct st_context
*st
= st_context(ctx
);
735 struct pipe_context
*pipe
= st
->pipe
;
736 struct pipe_index_buffer ibuffer
= {0};
737 struct pipe_draw_info info
;
738 const struct gl_client_array
**arrays
= ctx
->Array
._DrawArrays
;
742 /* Mesa core state should have been validated already */
743 assert(ctx
->NewState
== 0x0);
745 /* Get Mesa driver state. */
746 st
->dirty
.st
|= ctx
->NewDriverState
;
747 ctx
->NewDriverState
= 0;
750 (st
->dirty
.st
& (ST_NEW_VERTEX_ARRAYS
| ST_NEW_VERTEX_PROGRAM
)) ||
751 (st
->dirty
.mesa
& (_NEW_PROGRAM
| _NEW_BUFFER_OBJECT
)) != 0;
753 /* Validate state. */
755 GLboolean vertDataEdgeFlags
;
757 vertDataEdgeFlags
= arrays
[VERT_ATTRIB_EDGEFLAG
]->BufferObj
&&
758 arrays
[VERT_ATTRIB_EDGEFLAG
]->BufferObj
->Name
;
759 if (vertDataEdgeFlags
!= st
->vertdata_edgeflags
) {
760 st
->vertdata_edgeflags
= vertDataEdgeFlags
;
761 st
->dirty
.st
|= ST_NEW_EDGEFLAGS_DATA
;
764 st_validate_state(st
);
767 if (!st_validate_varrays(ctx
, arrays
)) {
768 /* probably out of memory, no-op the draw call */
774 if (MESA_VERBOSE
& VERBOSE_GLSL
) {
778 (void) check_uniforms
;
782 util_draw_init_info(&info
);
784 /* Get index bounds for user buffers. */
785 if (!index_bounds_valid
)
786 if (!all_varyings_in_vbos(arrays
))
787 vbo_get_minmax_indices(ctx
, prims
, ib
, &min_index
, &max_index
,
790 setup_index_buffer(st
, ib
, &ibuffer
);
793 if (min_index
!= ~0 && max_index
!= ~0) {
794 info
.min_index
= min_index
;
795 info
.max_index
= max_index
;
798 /* The VBO module handles restart for the non-indexed GLDrawArrays
799 * so we only set these fields for indexed drawing:
801 info
.primitive_restart
= ctx
->Array
.PrimitiveRestart
;
802 info
.restart_index
= ctx
->Array
.RestartIndex
;
805 /* Transform feedback drawing is always non-indexed. */
806 /* Set info.count_from_stream_output. */
808 st_transform_feedback_draw_init(tfb_vertcount
, &info
);
812 /* do actual drawing */
813 for (i
= 0; i
< nr_prims
; i
++) {
814 info
.mode
= translate_prim( ctx
, prims
[i
].mode
);
815 info
.start
= prims
[i
].start
;
816 info
.count
= prims
[i
].count
;
817 info
.instance_count
= prims
[i
].num_instances
;
818 info
.index_bias
= prims
[i
].basevertex
;
820 info
.min_index
= info
.start
;
821 info
.max_index
= info
.start
+ info
.count
- 1;
824 if (info
.count_from_stream_output
) {
825 cso_draw_vbo(st
->cso_context
, &info
);
827 else if (info
.primitive_restart
) {
828 /* don't trim, restarts might be inside index list */
829 cso_draw_vbo(st
->cso_context
, &info
);
831 else if (u_trim_pipe_prim(info
.mode
, &info
.count
))
832 cso_draw_vbo(st
->cso_context
, &info
);
835 if (ib
&& st
->indexbuf_uploader
&& !_mesa_is_bufferobj(ib
->obj
)) {
836 pipe_resource_reference(&ibuffer
.buffer
, NULL
);
842 st_init_draw(struct st_context
*st
)
844 struct gl_context
*ctx
= st
->ctx
;
846 vbo_set_draw_func(ctx
, st_draw_vbo
);
848 #if FEATURE_feedback || FEATURE_rastpos
849 st
->draw
= draw_create(st
->pipe
); /* for selection/feedback */
851 /* Disable draw options that might convert points/lines to tris, etc.
852 * as that would foul-up feedback/selection mode.
854 draw_wide_line_threshold(st
->draw
, 1000.0f
);
855 draw_wide_point_threshold(st
->draw
, 1000.0f
);
856 draw_enable_line_stipple(st
->draw
, FALSE
);
857 draw_enable_point_sprites(st
->draw
, FALSE
);
863 st_destroy_draw(struct st_context
*st
)
865 #if FEATURE_feedback || FEATURE_rastpos
866 draw_destroy(st
->draw
);