Allow RBUG to start blocked
[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 test
271 * the stride. 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 && !_mesa_is_bufferobj(arrays[i]->BufferObj))
280 return GL_FALSE;
281
282 return GL_TRUE;
283 }
284
285
286 /**
287 * Examine the active arrays to determine if we have interleaved
288 * vertex arrays all living in one VBO, or all living in user space.
289 */
290 static GLboolean
291 is_interleaved_arrays(const struct st_vertex_program *vp,
292 const struct st_vp_variant *vpv,
293 const struct gl_client_array **arrays)
294 {
295 GLuint attr;
296 const struct gl_buffer_object *firstBufObj = NULL;
297 GLint firstStride = -1;
298 const GLubyte *firstPtr = NULL;
299 GLboolean userSpaceBuffer = GL_FALSE;
300
301 for (attr = 0; attr < vpv->num_inputs; attr++) {
302 const GLuint mesaAttr = vp->index_to_input[attr];
303 const struct gl_client_array *array = arrays[mesaAttr];
304 const struct gl_buffer_object *bufObj = array->BufferObj;
305 const GLsizei stride = array->StrideB; /* in bytes */
306
307 if (attr == 0) {
308 /* save info about the first array */
309 firstStride = stride;
310 firstPtr = array->Ptr;
311 firstBufObj = bufObj;
312 userSpaceBuffer = !bufObj || !bufObj->Name;
313 }
314 else {
315 /* check if other arrays interleave with the first, in same buffer */
316 if (stride != firstStride)
317 return GL_FALSE; /* strides don't match */
318
319 if (bufObj != firstBufObj)
320 return GL_FALSE; /* arrays in different VBOs */
321
322 if (abs(array->Ptr - firstPtr) > firstStride)
323 return GL_FALSE; /* arrays start too far apart */
324
325 if ((!bufObj || !_mesa_is_bufferobj(bufObj)) != userSpaceBuffer)
326 return GL_FALSE; /* mix of VBO and user-space arrays */
327 }
328 }
329
330 return GL_TRUE;
331 }
332
333
334 /**
335 * Set up for drawing interleaved arrays that all live in one VBO
336 * or all live in user space.
337 * \param vbuffer returns vertex buffer info
338 * \param velements returns vertex element info
339 */
340 static void
341 setup_interleaved_attribs(struct gl_context *ctx,
342 const struct st_vertex_program *vp,
343 const struct st_vp_variant *vpv,
344 const struct gl_client_array **arrays,
345 struct pipe_vertex_buffer *vbuffer,
346 struct pipe_vertex_element velements[],
347 unsigned max_index,
348 unsigned num_instances)
349 {
350 struct st_context *st = st_context(ctx);
351 struct pipe_context *pipe = st->pipe;
352 GLuint attr;
353 const GLubyte *low_addr = NULL;
354
355 /* Find the lowest address of the arrays we're drawing */
356 if (vpv->num_inputs) {
357 low_addr = arrays[vp->index_to_input[0]]->Ptr;
358
359 for (attr = 1; attr < vpv->num_inputs; attr++) {
360 const GLubyte *start = arrays[vp->index_to_input[attr]]->Ptr;
361 low_addr = MIN2(low_addr, start);
362 }
363 }
364
365 for (attr = 0; attr < vpv->num_inputs; attr++) {
366 const GLuint mesaAttr = vp->index_to_input[attr];
367 const struct gl_client_array *array = arrays[mesaAttr];
368 struct gl_buffer_object *bufobj = array->BufferObj;
369 struct st_buffer_object *stobj = st_buffer_object(bufobj);
370 unsigned src_offset = (unsigned) (array->Ptr - low_addr);
371 GLuint element_size = array->_ElementSize;
372 GLsizei stride = array->StrideB;
373
374 assert(element_size == array->Size * _mesa_sizeof_type(array->Type));
375
376 if (attr == 0) {
377 if (bufobj && _mesa_is_bufferobj(bufobj)) {
378 vbuffer->buffer = NULL;
379 pipe_resource_reference(&vbuffer->buffer, stobj->buffer);
380 vbuffer->buffer_offset = pointer_to_offset(low_addr);
381 }
382 else {
383 uint divisor = array->InstanceDivisor;
384 uint last_index = divisor ? num_instances / divisor : max_index;
385 uint bytes = src_offset + stride * last_index + element_size;
386
387 vbuffer->buffer = pipe_user_buffer_create(pipe->screen,
388 (void*) low_addr,
389 bytes,
390 PIPE_BIND_VERTEX_BUFFER);
391 vbuffer->buffer_offset = 0;
392
393 /* Track user vertex buffers. */
394 pipe_resource_reference(&st->user_attrib[0].buffer, vbuffer->buffer);
395 st->user_attrib[0].element_size = element_size;
396 st->user_attrib[0].stride = stride;
397 st->num_user_attribs = 1;
398 }
399 vbuffer->stride = stride; /* in bytes */
400 }
401
402 velements[attr].src_offset = src_offset;
403 velements[attr].instance_divisor = array->InstanceDivisor;
404 velements[attr].vertex_buffer_index = 0;
405 velements[attr].src_format = st_pipe_vertex_format(array->Type,
406 array->Size,
407 array->Format,
408 array->Normalized);
409 assert(velements[attr].src_format);
410 }
411 }
412
413
414 /**
415 * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
416 * vertex attribute.
417 * \param vbuffer returns vertex buffer info
418 * \param velements returns vertex element info
419 */
420 static void
421 setup_non_interleaved_attribs(struct gl_context *ctx,
422 const struct st_vertex_program *vp,
423 const struct st_vp_variant *vpv,
424 const struct gl_client_array **arrays,
425 struct pipe_vertex_buffer vbuffer[],
426 struct pipe_vertex_element velements[],
427 unsigned max_index,
428 unsigned num_instances)
429 {
430 struct st_context *st = st_context(ctx);
431 struct pipe_context *pipe = st->pipe;
432 GLuint attr;
433
434 for (attr = 0; attr < vpv->num_inputs; attr++) {
435 const GLuint mesaAttr = vp->index_to_input[attr];
436 const struct gl_client_array *array = arrays[mesaAttr];
437 struct gl_buffer_object *bufobj = array->BufferObj;
438 GLuint element_size = array->_ElementSize;
439 GLsizei stride = array->StrideB;
440
441 assert(element_size == array->Size * _mesa_sizeof_type(array->Type));
442
443 if (bufobj && _mesa_is_bufferobj(bufobj)) {
444 /* Attribute data is in a VBO.
445 * Recall that for VBOs, the gl_client_array->Ptr field is
446 * really an offset from the start of the VBO, not a pointer.
447 */
448 struct st_buffer_object *stobj = st_buffer_object(bufobj);
449 assert(stobj->buffer);
450
451 vbuffer[attr].buffer = NULL;
452 pipe_resource_reference(&vbuffer[attr].buffer, stobj->buffer);
453 vbuffer[attr].buffer_offset = pointer_to_offset(array->Ptr);
454 }
455 else {
456 /* wrap user data */
457 uint bytes;
458 void *ptr;
459
460 if (array->Ptr) {
461 uint divisor = array->InstanceDivisor;
462 uint last_index = divisor ? num_instances / divisor : max_index;
463
464 bytes = stride * last_index + element_size;
465
466 ptr = (void *) array->Ptr;
467 }
468 else {
469 /* no array, use ctx->Current.Attrib[] value */
470 bytes = element_size = sizeof(ctx->Current.Attrib[0]);
471 ptr = (void *) ctx->Current.Attrib[mesaAttr];
472 stride = 0;
473 }
474
475 assert(ptr);
476 assert(bytes);
477
478 vbuffer[attr].buffer =
479 pipe_user_buffer_create(pipe->screen, ptr, bytes,
480 PIPE_BIND_VERTEX_BUFFER);
481
482 vbuffer[attr].buffer_offset = 0;
483
484 /* Track user vertex buffers. */
485 pipe_resource_reference(&st->user_attrib[attr].buffer, vbuffer[attr].buffer);
486 st->user_attrib[attr].element_size = element_size;
487 st->user_attrib[attr].stride = stride;
488 st->num_user_attribs = MAX2(st->num_user_attribs, attr + 1);
489 }
490
491 /* common-case setup */
492 vbuffer[attr].stride = stride; /* in bytes */
493
494 velements[attr].src_offset = 0;
495 velements[attr].instance_divisor = array->InstanceDivisor;
496 velements[attr].vertex_buffer_index = attr;
497 velements[attr].src_format = st_pipe_vertex_format(array->Type,
498 array->Size,
499 array->Format,
500 array->Normalized);
501 assert(velements[attr].src_format);
502 }
503 }
504
505
506 static void
507 setup_index_buffer(struct gl_context *ctx,
508 const struct _mesa_index_buffer *ib,
509 struct pipe_index_buffer *ibuffer)
510 {
511 struct st_context *st = st_context(ctx);
512 struct pipe_context *pipe = st->pipe;
513
514 memset(ibuffer, 0, sizeof(*ibuffer));
515 if (ib) {
516 struct gl_buffer_object *bufobj = ib->obj;
517
518 switch (ib->type) {
519 case GL_UNSIGNED_INT:
520 ibuffer->index_size = 4;
521 break;
522 case GL_UNSIGNED_SHORT:
523 ibuffer->index_size = 2;
524 break;
525 case GL_UNSIGNED_BYTE:
526 ibuffer->index_size = 1;
527 break;
528 default:
529 assert(0);
530 return;
531 }
532
533 /* get/create the index buffer object */
534 if (bufobj && _mesa_is_bufferobj(bufobj)) {
535 /* elements/indexes are in a real VBO */
536 struct st_buffer_object *stobj = st_buffer_object(bufobj);
537 pipe_resource_reference(&ibuffer->buffer, stobj->buffer);
538 ibuffer->offset = pointer_to_offset(ib->ptr);
539 }
540 else {
541 /* element/indicies are in user space memory */
542 ibuffer->buffer =
543 pipe_user_buffer_create(pipe->screen, (void *) ib->ptr,
544 ib->count * ibuffer->index_size,
545 PIPE_BIND_INDEX_BUFFER);
546 }
547 }
548 }
549
550
551 /**
552 * Prior to drawing, check that any uniforms referenced by the
553 * current shader have been set. If a uniform has not been set,
554 * issue a warning.
555 */
556 static void
557 check_uniforms(struct gl_context *ctx)
558 {
559 struct gl_shader_program *shProg[3] = {
560 ctx->Shader.CurrentVertexProgram,
561 ctx->Shader.CurrentGeometryProgram,
562 ctx->Shader.CurrentFragmentProgram,
563 };
564 unsigned j;
565
566 for (j = 0; j < 3; j++) {
567 unsigned i;
568
569 if (shProg[j] == NULL || !shProg[j]->LinkStatus)
570 continue;
571
572 for (i = 0; i < shProg[j]->Uniforms->NumUniforms; i++) {
573 const struct gl_uniform *u = &shProg[j]->Uniforms->Uniforms[i];
574 if (!u->Initialized) {
575 _mesa_warning(ctx,
576 "Using shader with uninitialized uniform: %s",
577 u->Name);
578 }
579 }
580 }
581 }
582
583
584 /**
585 * Translate OpenGL primtive type (GL_POINTS, GL_TRIANGLE_STRIP, etc) to
586 * the corresponding Gallium type.
587 */
588 static unsigned
589 translate_prim(const struct gl_context *ctx, unsigned prim)
590 {
591 /* GL prims should match Gallium prims, spot-check a few */
592 assert(GL_POINTS == PIPE_PRIM_POINTS);
593 assert(GL_QUADS == PIPE_PRIM_QUADS);
594 assert(GL_TRIANGLE_STRIP_ADJACENCY == PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
595
596 /* Avoid quadstrips if it's easy to do so:
597 * Note: it's important to do the correct trimming if we change the
598 * prim type! We do that wherever this function is called.
599 */
600 if (prim == GL_QUAD_STRIP &&
601 ctx->Light.ShadeModel != GL_FLAT &&
602 ctx->Polygon.FrontMode == GL_FILL &&
603 ctx->Polygon.BackMode == GL_FILL)
604 prim = GL_TRIANGLE_STRIP;
605
606 return prim;
607 }
608
609
610 static void
611 st_validate_varrays(struct gl_context *ctx,
612 const struct gl_client_array **arrays,
613 unsigned max_index,
614 unsigned num_instances)
615 {
616 struct st_context *st = st_context(ctx);
617 const struct st_vertex_program *vp;
618 const struct st_vp_variant *vpv;
619 struct pipe_vertex_buffer vbuffer[PIPE_MAX_SHADER_INPUTS];
620 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
621 unsigned num_vbuffers, num_velements;
622 GLuint attr;
623 unsigned i;
624
625 /* must get these after state validation! */
626 vp = st->vp;
627 vpv = st->vp_variant;
628
629 memset(velements, 0, sizeof(struct pipe_vertex_element) * vpv->num_inputs);
630
631 /* Unreference any user vertex buffers. */
632 for (i = 0; i < st->num_user_attribs; i++) {
633 pipe_resource_reference(&st->user_attrib[i].buffer, NULL);
634 }
635 st->num_user_attribs = 0;
636
637 /*
638 * Setup the vbuffer[] and velements[] arrays.
639 */
640 if (is_interleaved_arrays(vp, vpv, arrays)) {
641 setup_interleaved_attribs(ctx, vp, vpv, arrays, vbuffer, velements,
642 max_index, num_instances);
643
644 num_vbuffers = 1;
645 num_velements = vpv->num_inputs;
646 if (num_velements == 0)
647 num_vbuffers = 0;
648 }
649 else {
650 setup_non_interleaved_attribs(ctx, vp, vpv, arrays,
651 vbuffer, velements, max_index,
652 num_instances);
653 num_vbuffers = vpv->num_inputs;
654 num_velements = vpv->num_inputs;
655 }
656
657 cso_set_vertex_buffers(st->cso_context, num_vbuffers, vbuffer);
658 cso_set_vertex_elements(st->cso_context, num_velements, velements);
659
660 /* unreference buffers (frees wrapped user-space buffer objects)
661 * This is OK, because the pipe driver should reference buffers by itself
662 * in set_vertex_buffers. */
663 for (attr = 0; attr < num_vbuffers; attr++) {
664 pipe_resource_reference(&vbuffer[attr].buffer, NULL);
665 assert(!vbuffer[attr].buffer);
666 }
667 }
668
669
670 /**
671 * This function gets plugged into the VBO module and is called when
672 * we have something to render.
673 * Basically, translate the information into the format expected by gallium.
674 */
675 void
676 st_draw_vbo(struct gl_context *ctx,
677 const struct gl_client_array **arrays,
678 const struct _mesa_prim *prims,
679 GLuint nr_prims,
680 const struct _mesa_index_buffer *ib,
681 GLboolean index_bounds_valid,
682 GLuint min_index,
683 GLuint max_index)
684 {
685 struct st_context *st = st_context(ctx);
686 struct pipe_context *pipe = st->pipe;
687 struct pipe_index_buffer ibuffer;
688 struct pipe_draw_info info;
689 unsigned i, num_instances = 1;
690 GLboolean new_array =
691 st->dirty.st &&
692 (st->dirty.mesa & (_NEW_ARRAY | _NEW_PROGRAM | _NEW_BUFFER_OBJECT)) != 0;
693
694 /* Mesa core state should have been validated already */
695 assert(ctx->NewState == 0x0);
696
697 if (ib) {
698 /* Gallium probably doesn't want this in some cases. */
699 if (!index_bounds_valid)
700 if (!all_varyings_in_vbos(arrays))
701 vbo_get_minmax_index(ctx, prims, ib, &min_index, &max_index);
702
703 for (i = 0; i < nr_prims; i++) {
704 num_instances = MAX2(num_instances, prims[i].num_instances);
705 }
706 }
707 else {
708 /* Get min/max index for non-indexed drawing. */
709 min_index = ~0;
710 max_index = 0;
711
712 for (i = 0; i < nr_prims; i++) {
713 min_index = MIN2(min_index, prims[i].start);
714 max_index = MAX2(max_index, prims[i].start + prims[i].count - 1);
715 num_instances = MAX2(num_instances, prims[i].num_instances);
716 }
717 }
718
719 /* Validate state. */
720 if (st->dirty.st) {
721 GLboolean vertDataEdgeFlags;
722
723 /* sanity check for pointer arithmetic below */
724 assert(sizeof(arrays[0]->Ptr[0]) == 1);
725
726 vertDataEdgeFlags = arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj &&
727 arrays[VERT_ATTRIB_EDGEFLAG]->BufferObj->Name;
728 if (vertDataEdgeFlags != st->vertdata_edgeflags) {
729 st->vertdata_edgeflags = vertDataEdgeFlags;
730 st->dirty.st |= ST_NEW_EDGEFLAGS_DATA;
731 }
732
733 st_validate_state(st);
734
735 if (new_array) {
736 st_validate_varrays(ctx, arrays, max_index, num_instances);
737 }
738
739 #if 0
740 if (MESA_VERBOSE & VERBOSE_GLSL) {
741 check_uniforms(ctx);
742 }
743 #else
744 (void) check_uniforms;
745 #endif
746 }
747
748 /* Notify the driver that the content of user buffers may have been
749 * changed. */
750 assert(max_index >= min_index);
751 if (!new_array && st->num_user_attribs) {
752 for (i = 0; i < st->num_user_attribs; i++) {
753 if (st->user_attrib[i].buffer) {
754 unsigned element_size = st->user_attrib[i].element_size;
755 unsigned stride = st->user_attrib[i].stride;
756 unsigned min_offset = min_index * stride;
757 unsigned max_offset = max_index * stride + element_size;
758
759 assert(max_offset > min_offset);
760
761 pipe->redefine_user_buffer(pipe, st->user_attrib[i].buffer,
762 min_offset,
763 max_offset - min_offset);
764 }
765 }
766 }
767
768 setup_index_buffer(ctx, ib, &ibuffer);
769 pipe->set_index_buffer(pipe, &ibuffer);
770
771 util_draw_init_info(&info);
772 if (ib) {
773 info.indexed = TRUE;
774 if (min_index != ~0 && max_index != ~0) {
775 info.min_index = min_index;
776 info.max_index = max_index;
777 }
778 }
779
780 info.primitive_restart = ctx->Array.PrimitiveRestart;
781 info.restart_index = ctx->Array.RestartIndex;
782
783 /* do actual drawing */
784 for (i = 0; i < nr_prims; i++) {
785 info.mode = translate_prim( ctx, prims[i].mode );
786 info.start = prims[i].start;
787 info.count = prims[i].count;
788 info.instance_count = prims[i].num_instances;
789 info.index_bias = prims[i].basevertex;
790 if (!ib) {
791 info.min_index = info.start;
792 info.max_index = info.start + info.count - 1;
793 }
794
795 if (u_trim_pipe_prim(info.mode, &info.count))
796 pipe->draw_vbo(pipe, &info);
797 }
798
799 pipe_resource_reference(&ibuffer.buffer, NULL);
800 }
801
802
803 void
804 st_init_draw(struct st_context *st)
805 {
806 struct gl_context *ctx = st->ctx;
807
808 vbo_set_draw_func(ctx, st_draw_vbo);
809
810 #if FEATURE_feedback || FEATURE_rastpos
811 st->draw = draw_create(st->pipe); /* for selection/feedback */
812
813 /* Disable draw options that might convert points/lines to tris, etc.
814 * as that would foul-up feedback/selection mode.
815 */
816 draw_wide_line_threshold(st->draw, 1000.0f);
817 draw_wide_point_threshold(st->draw, 1000.0f);
818 draw_enable_line_stipple(st->draw, FALSE);
819 draw_enable_point_sprites(st->draw, FALSE);
820 #endif
821 }
822
823
824 void
825 st_destroy_draw(struct st_context *st)
826 {
827 #if FEATURE_feedback || FEATURE_rastpos
828 draw_destroy(st->draw);
829 #endif
830 }