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