4bc734bad382935e32fdc778377bd8375f376ed5
[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 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
36 #include "shader/prog_uniform.h"
37
38 #include "vbo/vbo.h"
39
40 #include "st_context.h"
41 #include "st_atom.h"
42 #include "st_cb_bufferobjects.h"
43 #include "st_draw.h"
44 #include "st_program.h"
45
46 #include "pipe/p_context.h"
47 #include "pipe/p_defines.h"
48 #include "pipe/p_inlines.h"
49
50 #include "draw/draw_private.h"
51 #include "draw/draw_context.h"
52
53
54 static GLuint double_types[4] = {
55 PIPE_FORMAT_R64_FLOAT,
56 PIPE_FORMAT_R64G64_FLOAT,
57 PIPE_FORMAT_R64G64B64_FLOAT,
58 PIPE_FORMAT_R64G64B64A64_FLOAT
59 };
60
61 static GLuint float_types[4] = {
62 PIPE_FORMAT_R32_FLOAT,
63 PIPE_FORMAT_R32G32_FLOAT,
64 PIPE_FORMAT_R32G32B32_FLOAT,
65 PIPE_FORMAT_R32G32B32A32_FLOAT
66 };
67
68 static GLuint uint_types_norm[4] = {
69 PIPE_FORMAT_R32_UNORM,
70 PIPE_FORMAT_R32G32_UNORM,
71 PIPE_FORMAT_R32G32B32_UNORM,
72 PIPE_FORMAT_R32G32B32A32_UNORM
73 };
74
75 static GLuint uint_types_scale[4] = {
76 PIPE_FORMAT_R32_USCALED,
77 PIPE_FORMAT_R32G32_USCALED,
78 PIPE_FORMAT_R32G32B32_USCALED,
79 PIPE_FORMAT_R32G32B32A32_USCALED
80 };
81
82 static GLuint int_types_norm[4] = {
83 PIPE_FORMAT_R32_SNORM,
84 PIPE_FORMAT_R32G32_SNORM,
85 PIPE_FORMAT_R32G32B32_SNORM,
86 PIPE_FORMAT_R32G32B32A32_SNORM
87 };
88
89 static GLuint int_types_scale[4] = {
90 PIPE_FORMAT_R32_SSCALED,
91 PIPE_FORMAT_R32G32_SSCALED,
92 PIPE_FORMAT_R32G32B32_SSCALED,
93 PIPE_FORMAT_R32G32B32A32_SSCALED
94 };
95
96 static GLuint ushort_types_norm[4] = {
97 PIPE_FORMAT_R16_UNORM,
98 PIPE_FORMAT_R16G16_UNORM,
99 PIPE_FORMAT_R16G16B16_UNORM,
100 PIPE_FORMAT_R16G16B16A16_UNORM
101 };
102
103 static GLuint ushort_types_scale[4] = {
104 PIPE_FORMAT_R16_USCALED,
105 PIPE_FORMAT_R16G16_USCALED,
106 PIPE_FORMAT_R16G16B16_USCALED,
107 PIPE_FORMAT_R16G16B16A16_USCALED
108 };
109
110 static GLuint short_types_norm[4] = {
111 PIPE_FORMAT_R16_SNORM,
112 PIPE_FORMAT_R16G16_SNORM,
113 PIPE_FORMAT_R16G16B16_SNORM,
114 PIPE_FORMAT_R16G16B16A16_SNORM
115 };
116
117 static GLuint short_types_scale[4] = {
118 PIPE_FORMAT_R16_SSCALED,
119 PIPE_FORMAT_R16G16_SSCALED,
120 PIPE_FORMAT_R16G16B16_SSCALED,
121 PIPE_FORMAT_R16G16B16A16_SSCALED
122 };
123
124 static GLuint ubyte_types_norm[4] = {
125 PIPE_FORMAT_R8_UNORM,
126 PIPE_FORMAT_R8G8_UNORM,
127 PIPE_FORMAT_R8G8B8_UNORM,
128 PIPE_FORMAT_R8G8B8A8_UNORM
129 };
130
131 static GLuint ubyte_types_scale[4] = {
132 PIPE_FORMAT_R8_USCALED,
133 PIPE_FORMAT_R8G8_USCALED,
134 PIPE_FORMAT_R8G8B8_USCALED,
135 PIPE_FORMAT_R8G8B8A8_USCALED
136 };
137
138 static GLuint byte_types_norm[4] = {
139 PIPE_FORMAT_R8_SNORM,
140 PIPE_FORMAT_R8G8_SNORM,
141 PIPE_FORMAT_R8G8B8_SNORM,
142 PIPE_FORMAT_R8G8B8A8_SNORM
143 };
144
145 static GLuint byte_types_scale[4] = {
146 PIPE_FORMAT_R8_SSCALED,
147 PIPE_FORMAT_R8G8_SSCALED,
148 PIPE_FORMAT_R8G8B8_SSCALED,
149 PIPE_FORMAT_R8G8B8A8_SSCALED
150 };
151
152 static GLuint fixed_types[4] = {
153 PIPE_FORMAT_R32_FIXED,
154 PIPE_FORMAT_R32G32_FIXED,
155 PIPE_FORMAT_R32G32B32_FIXED,
156 PIPE_FORMAT_R32G32B32A32_FIXED
157 };
158
159
160
161 /**
162 * Return a PIPE_FORMAT_x for the given GL datatype and size.
163 */
164 static GLuint
165 pipe_vertex_format(GLenum type, GLuint size, GLboolean normalized)
166 {
167 assert((type >= GL_BYTE && type <= GL_DOUBLE) ||
168 type == GL_FIXED);
169 assert(size >= 1);
170 assert(size <= 4);
171
172 if (normalized) {
173 switch (type) {
174 case GL_DOUBLE: return double_types[size-1];
175 case GL_FLOAT: return float_types[size-1];
176 case GL_INT: return int_types_norm[size-1];
177 case GL_SHORT: return short_types_norm[size-1];
178 case GL_BYTE: return byte_types_norm[size-1];
179 case GL_UNSIGNED_INT: return uint_types_norm[size-1];
180 case GL_UNSIGNED_SHORT: return ushort_types_norm[size-1];
181 case GL_UNSIGNED_BYTE: return ubyte_types_norm[size-1];
182 case GL_FIXED: return fixed_types[size-1];
183 default: assert(0); return 0;
184 }
185 }
186 else {
187 switch (type) {
188 case GL_DOUBLE: return double_types[size-1];
189 case GL_FLOAT: return float_types[size-1];
190 case GL_INT: return int_types_scale[size-1];
191 case GL_SHORT: return short_types_scale[size-1];
192 case GL_BYTE: return byte_types_scale[size-1];
193 case GL_UNSIGNED_INT: return uint_types_scale[size-1];
194 case GL_UNSIGNED_SHORT: return ushort_types_scale[size-1];
195 case GL_UNSIGNED_BYTE: return ubyte_types_scale[size-1];
196 case GL_FIXED: return fixed_types[size-1];
197 default: assert(0); return 0;
198 }
199 }
200 return 0; /* silence compiler warning */
201 }
202
203
204 /*
205 * If edge flags are needed, setup an bitvector of flags and call
206 * pipe->set_edgeflags().
207 * XXX memleak: need to free the returned pointer at some point
208 */
209 static void *
210 setup_edgeflags(GLcontext *ctx, GLenum primMode, GLint start, GLint count,
211 const struct gl_client_array *array)
212 {
213 struct pipe_context *pipe = ctx->st->pipe;
214
215 if ((primMode == GL_TRIANGLES ||
216 primMode == GL_QUADS ||
217 primMode == GL_POLYGON) &&
218 (ctx->Polygon.FrontMode != GL_FILL ||
219 ctx->Polygon.BackMode != GL_FILL)) {
220 /* need edge flags */
221 GLint i;
222 unsigned *vec;
223 struct st_buffer_object *stobj = st_buffer_object(array->BufferObj);
224 ubyte *map;
225
226 if (!stobj)
227 return NULL;
228
229 vec = (unsigned *) calloc(sizeof(unsigned), (count + 31) / 32);
230 if (!vec)
231 return NULL;
232
233 map = pipe_buffer_map(pipe->screen, stobj->buffer, PIPE_BUFFER_USAGE_CPU_READ);
234 map = ADD_POINTERS(map, array->Ptr);
235
236 for (i = 0; i < count; i++) {
237 if (*((float *) map))
238 vec[i/32] |= 1 << (i % 32);
239
240 map += array->StrideB;
241 }
242
243 pipe_buffer_unmap(pipe->screen, stobj->buffer);
244
245 pipe->set_edgeflags(pipe, vec);
246
247 return vec;
248 }
249 else {
250 /* edge flags not needed */
251 pipe->set_edgeflags(pipe, NULL);
252 return NULL;
253 }
254 }
255
256
257 /**
258 * Examine the active arrays to determine if we have interleaved
259 * vertex arrays all living in one VBO, or all living in user space.
260 * \param userSpace returns whether the arrays are in user space.
261 */
262 static GLboolean
263 is_interleaved_arrays(const struct st_vertex_program *vp,
264 const struct gl_client_array **arrays,
265 GLboolean *userSpace)
266 {
267 GLuint attr;
268 const struct gl_buffer_object *firstBufObj = NULL;
269 GLint firstStride = -1;
270 GLuint num_client_arrays = 0;
271 const GLubyte *client_addr = NULL;
272
273 for (attr = 0; attr < vp->num_inputs; attr++) {
274 const GLuint mesaAttr = vp->index_to_input[attr];
275 const struct gl_buffer_object *bufObj = arrays[mesaAttr]->BufferObj;
276 const GLsizei stride = arrays[mesaAttr]->StrideB; /* in bytes */
277
278 if (firstStride < 0) {
279 firstStride = stride;
280 }
281 else if (firstStride != stride) {
282 return GL_FALSE;
283 }
284
285 if (!bufObj || !bufObj->Name) {
286 num_client_arrays++;
287 /* Try to detect if the client-space arrays are
288 * "close" to each other.
289 */
290 if (!client_addr) {
291 client_addr = arrays[mesaAttr]->Ptr;
292 }
293 else if (abs(arrays[mesaAttr]->Ptr - client_addr) > firstStride) {
294 /* arrays start too far apart */
295 return GL_FALSE;
296 }
297 }
298 else if (!firstBufObj) {
299 firstBufObj = bufObj;
300 }
301 else if (bufObj != firstBufObj) {
302 return GL_FALSE;
303 }
304 }
305
306 *userSpace = (num_client_arrays == vp->num_inputs);
307 /*printf("user space: %d\n", (int) *userSpace);*/
308
309 return GL_TRUE;
310 }
311
312
313 /**
314 * Once we know all the arrays are in user space, this function
315 * computes the memory range occupied by the arrays.
316 */
317 static void
318 get_user_arrays_bounds(const struct st_vertex_program *vp,
319 const struct gl_client_array **arrays,
320 GLuint max_index,
321 const GLubyte **low, const GLubyte **high)
322 {
323 const GLubyte *low_addr = NULL;
324 GLuint attr;
325 GLint stride;
326
327 for (attr = 0; attr < vp->num_inputs; attr++) {
328 const GLuint mesaAttr = vp->index_to_input[attr];
329 const GLubyte *start = arrays[mesaAttr]->Ptr;
330 stride = arrays[mesaAttr]->StrideB;
331 if (attr == 0) {
332 low_addr = start;
333 }
334 else {
335 low_addr = MIN2(low_addr, start);
336 }
337 }
338
339 *low = low_addr;
340 *high = low_addr + (max_index + 1) * stride;
341 }
342
343
344 /**
345 * Set up for drawing interleaved arrays that all live in one VBO
346 * or all live in user space.
347 * \param vbuffer returns vertex buffer info
348 * \param velements returns vertex element info
349 */
350 static void
351 setup_interleaved_attribs(GLcontext *ctx,
352 const struct st_vertex_program *vp,
353 const struct gl_client_array **arrays,
354 GLuint max_index,
355 GLboolean userSpace,
356 struct pipe_vertex_buffer *vbuffer,
357 struct pipe_vertex_element velements[])
358 {
359 struct pipe_context *pipe = ctx->st->pipe;
360 GLuint attr;
361 const GLubyte *offset0;
362
363 for (attr = 0; attr < vp->num_inputs; attr++) {
364 const GLuint mesaAttr = vp->index_to_input[attr];
365 struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
366 struct st_buffer_object *stobj = st_buffer_object(bufobj);
367 GLsizei stride = arrays[mesaAttr]->StrideB;
368
369 /*printf("stobj %u = %p\n", attr, (void*)stobj);*/
370
371 if (attr == 0) {
372 if (userSpace) {
373 const GLubyte *low, *high;
374 get_user_arrays_bounds(vp, arrays, max_index, &low, &high);
375 /*printf("user buffer range: %p %p %d\n", low, high, high-low);*/
376 vbuffer->buffer =
377 pipe_user_buffer_create(pipe->screen, (void *) low, high - low);
378 vbuffer->buffer_offset = 0;
379 offset0 = low;
380 }
381 else {
382 vbuffer->buffer = NULL;
383 pipe_buffer_reference(pipe->screen, &vbuffer->buffer, stobj->buffer);
384 vbuffer->buffer_offset = (unsigned) arrays[mesaAttr]->Ptr;
385 offset0 = arrays[mesaAttr]->Ptr;
386 }
387 vbuffer->pitch = stride; /* in bytes */
388 vbuffer->max_index = max_index;
389 }
390
391 velements[attr].src_offset =
392 (unsigned) (arrays[mesaAttr]->Ptr - offset0);
393 velements[attr].vertex_buffer_index = 0;
394 velements[attr].nr_components = arrays[mesaAttr]->Size;
395 velements[attr].src_format =
396 pipe_vertex_format(arrays[mesaAttr]->Type,
397 arrays[mesaAttr]->Size,
398 arrays[mesaAttr]->Normalized);
399 assert(velements[attr].src_format);
400 }
401 }
402
403
404 /**
405 * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
406 * vertex attribute.
407 * \param vbuffer returns vertex buffer info
408 * \param velements returns vertex element info
409 */
410 static void
411 setup_non_interleaved_attribs(GLcontext *ctx,
412 const struct st_vertex_program *vp,
413 const struct gl_client_array **arrays,
414 GLuint max_index,
415 struct pipe_vertex_buffer vbuffer[],
416 struct pipe_vertex_element velements[])
417 {
418 struct pipe_context *pipe = ctx->st->pipe;
419 GLuint attr;
420
421 for (attr = 0; attr < vp->num_inputs; attr++) {
422 const GLuint mesaAttr = vp->index_to_input[attr];
423 struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
424 GLsizei stride = arrays[mesaAttr]->StrideB;
425
426 if (bufobj && bufobj->Name) {
427 /* Attribute data is in a VBO.
428 * Recall that for VBOs, the gl_client_array->Ptr field is
429 * really an offset from the start of the VBO, not a pointer.
430 */
431 struct st_buffer_object *stobj = st_buffer_object(bufobj);
432 assert(stobj->buffer);
433 /*printf("stobj %u = %p\n", attr, (void*) stobj);*/
434
435 vbuffer[attr].buffer = NULL;
436 pipe_buffer_reference(pipe->screen, &vbuffer[attr].buffer, stobj->buffer);
437 vbuffer[attr].buffer_offset = (unsigned) arrays[mesaAttr]->Ptr;
438 velements[attr].src_offset = 0;
439 }
440 else {
441 /* attribute data is in user-space memory, not a VBO */
442 uint bytes;
443 /*printf("user-space array %d stride %d\n", attr, stride);*/
444
445 /* wrap user data */
446 if (arrays[mesaAttr]->Ptr) {
447 /* user's vertex array */
448 if (arrays[mesaAttr]->StrideB) {
449 bytes = arrays[mesaAttr]->StrideB * (max_index + 1);
450 }
451 else {
452 bytes = arrays[mesaAttr]->Size
453 * _mesa_sizeof_type(arrays[mesaAttr]->Type);
454 }
455 vbuffer[attr].buffer = pipe_user_buffer_create(pipe->screen,
456 (void *) arrays[mesaAttr]->Ptr, bytes);
457 }
458 else {
459 /* no array, use ctx->Current.Attrib[] value */
460 bytes = sizeof(ctx->Current.Attrib[0]);
461 vbuffer[attr].buffer = pipe_user_buffer_create(pipe->screen,
462 (void *) ctx->Current.Attrib[mesaAttr], bytes);
463 stride = 0;
464 }
465
466 vbuffer[attr].buffer_offset = 0;
467 velements[attr].src_offset = 0;
468 }
469
470 assert(velements[attr].src_offset <= 2048); /* 11-bit field */
471
472 /* common-case setup */
473 vbuffer[attr].pitch = stride; /* in bytes */
474 vbuffer[attr].max_index = max_index;
475 velements[attr].vertex_buffer_index = attr;
476 velements[attr].nr_components = arrays[mesaAttr]->Size;
477 velements[attr].src_format
478 = pipe_vertex_format(arrays[mesaAttr]->Type,
479 arrays[mesaAttr]->Size,
480 arrays[mesaAttr]->Normalized);
481 assert(velements[attr].src_format);
482 }
483 }
484
485
486
487 /**
488 * Prior to drawing, check that any uniforms referenced by the
489 * current shader have been set. If a uniform has not been set,
490 * issue a warning.
491 */
492 static void
493 check_uniforms(GLcontext *ctx)
494 {
495 const struct gl_shader_program *shProg = ctx->Shader.CurrentProgram;
496 if (shProg && shProg->LinkStatus) {
497 GLuint i;
498 for (i = 0; i < shProg->Uniforms->NumUniforms; i++) {
499 const struct gl_uniform *u = &shProg->Uniforms->Uniforms[i];
500 if (!u->Initialized) {
501 _mesa_warning(ctx,
502 "Using shader with uninitialized uniform: %s",
503 u->Name);
504 }
505 }
506 }
507 }
508
509
510 /**
511 * This function gets plugged into the VBO module and is called when
512 * we have something to render.
513 * Basically, translate the information into the format expected by gallium.
514 */
515 void
516 st_draw_vbo(GLcontext *ctx,
517 const struct gl_client_array **arrays,
518 const struct _mesa_prim *prims,
519 GLuint nr_prims,
520 const struct _mesa_index_buffer *ib,
521 GLuint min_index,
522 GLuint max_index)
523 {
524 struct pipe_context *pipe = ctx->st->pipe;
525 const struct st_vertex_program *vp;
526 const struct pipe_shader_state *vs;
527 struct pipe_vertex_buffer vbuffer[PIPE_MAX_SHADER_INPUTS];
528 GLuint attr;
529 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
530 unsigned num_vbuffers, num_velements;
531 GLboolean userSpace;
532
533 /* sanity check for pointer arithmetic below */
534 assert(sizeof(arrays[0]->Ptr[0]) == 1);
535
536 st_validate_state(ctx->st);
537
538 /* must get these after state validation! */
539 vp = ctx->st->vp;
540 vs = &ctx->st->vp->state;
541
542 if (MESA_VERBOSE & VERBOSE_GLSL) {
543 check_uniforms(ctx);
544 }
545
546 /*
547 * Setup the vbuffer[] and velements[] arrays.
548 */
549 if (is_interleaved_arrays(vp, arrays, &userSpace)) {
550 /*printf("Draw interleaved\n");*/
551 setup_interleaved_attribs(ctx, vp, arrays, max_index, userSpace,
552 vbuffer, velements);
553 num_vbuffers = 1;
554 num_velements = vp->num_inputs;
555 if (num_velements == 0)
556 num_vbuffers = 0;
557 }
558 else {
559 /*printf("Draw non-interleaved\n");*/
560 setup_non_interleaved_attribs(ctx, vp, arrays, max_index,
561 vbuffer, velements);
562 num_vbuffers = vp->num_inputs;
563 num_velements = vp->num_inputs;
564 }
565
566 #if 0
567 {
568 GLuint i;
569 for (i = 0; i < num_vbuffers; i++) {
570 printf("buffers[%d].pitch = %u\n", i, vbuffer[i].pitch);
571 printf("buffers[%d].max_index = %u\n", i, vbuffer[i].max_index);
572 printf("buffers[%d].buffer_offset = %u\n", i, vbuffer[i].buffer_offset);
573 printf("buffers[%d].buffer = %p\n", i, (void*) vbuffer[i].buffer);
574 }
575 for (i = 0; i < num_velements; i++) {
576 printf("vlements[%d].vbuffer_index = %u\n", i, velements[i].vertex_buffer_index);
577 printf("vlements[%d].src_offset = %u\n", i, velements[i].src_offset);
578 printf("vlements[%d].nr_comps = %u\n", i, velements[i].nr_components);
579 printf("vlements[%d].format = %s\n", i, pf_name(velements[i].src_format));
580 }
581 }
582 #endif
583
584 pipe->set_vertex_buffers(pipe, num_vbuffers, vbuffer);
585 pipe->set_vertex_elements(pipe, num_velements, velements);
586
587 if (num_vbuffers == 0 || num_velements == 0)
588 return;
589
590 /* do actual drawing */
591 if (ib) {
592 /* indexed primitive */
593 struct gl_buffer_object *bufobj = ib->obj;
594 struct pipe_buffer *indexBuf = NULL;
595 unsigned indexSize, indexOffset, i;
596
597 switch (ib->type) {
598 case GL_UNSIGNED_INT:
599 indexSize = 4;
600 break;
601 case GL_UNSIGNED_SHORT:
602 indexSize = 2;
603 break;
604 case GL_UNSIGNED_BYTE:
605 indexSize = 1;
606 break;
607 default:
608 assert(0);
609 return;
610 }
611
612 /* get/create the index buffer object */
613 if (bufobj && bufobj->Name) {
614 /* elements/indexes are in a real VBO */
615 struct st_buffer_object *stobj = st_buffer_object(bufobj);
616 pipe_buffer_reference(pipe->screen, &indexBuf, stobj->buffer);
617 indexOffset = (unsigned) ib->ptr / indexSize;
618 }
619 else {
620 /* element/indicies are in user space memory */
621 indexBuf = pipe_user_buffer_create(pipe->screen, (void *) ib->ptr,
622 ib->count * indexSize);
623 indexOffset = 0;
624 }
625
626 /* draw */
627 if (nr_prims == 1 && pipe->draw_range_elements != NULL) {
628 i = 0;
629
630 /* XXX: exercise temporary path to pass min/max directly
631 * through to driver & draw module. These interfaces still
632 * need a bit of work...
633 */
634 setup_edgeflags(ctx, prims[i].mode,
635 prims[i].start + indexOffset, prims[i].count,
636 arrays[VERT_ATTRIB_EDGEFLAG]);
637
638 pipe->draw_range_elements(pipe, indexBuf, indexSize,
639 min_index,
640 max_index,
641 prims[i].mode,
642 prims[i].start + indexOffset, prims[i].count);
643 }
644 else {
645 for (i = 0; i < nr_prims; i++) {
646 setup_edgeflags(ctx, prims[i].mode,
647 prims[i].start + indexOffset, prims[i].count,
648 arrays[VERT_ATTRIB_EDGEFLAG]);
649
650 pipe->draw_elements(pipe, indexBuf, indexSize,
651 prims[i].mode,
652 prims[i].start + indexOffset, prims[i].count);
653 }
654 }
655
656 pipe_buffer_reference(pipe->screen, &indexBuf, NULL);
657 }
658 else {
659 /* non-indexed */
660 GLuint i;
661 for (i = 0; i < nr_prims; i++) {
662 setup_edgeflags(ctx, prims[i].mode,
663 prims[i].start, prims[i].count,
664 arrays[VERT_ATTRIB_EDGEFLAG]);
665
666 pipe->draw_arrays(pipe, prims[i].mode, prims[i].start, prims[i].count);
667 }
668 }
669
670 /* unreference buffers (frees wrapped user-space buffer objects) */
671 for (attr = 0; attr < num_vbuffers; attr++) {
672 pipe_buffer_reference(pipe->screen, &vbuffer[attr].buffer, NULL);
673 assert(!vbuffer[attr].buffer);
674 }
675 pipe->set_vertex_buffers(pipe, num_vbuffers, vbuffer);
676 }
677
678
679 #if FEATURE_feedback || FEATURE_drawpix
680
681 /**
682 * Set the (private) draw module's post-transformed vertex format when in
683 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
684 */
685 static void
686 set_feedback_vertex_format(GLcontext *ctx)
687 {
688 #if 0
689 struct st_context *st = ctx->st;
690 struct vertex_info vinfo;
691 GLuint i;
692
693 memset(&vinfo, 0, sizeof(vinfo));
694
695 if (ctx->RenderMode == GL_SELECT) {
696 assert(ctx->RenderMode == GL_SELECT);
697 vinfo.num_attribs = 1;
698 vinfo.format[0] = FORMAT_4F;
699 vinfo.interp_mode[0] = INTERP_LINEAR;
700 }
701 else {
702 /* GL_FEEDBACK, or glRasterPos */
703 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
704 vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
705 for (i = 0; i < vinfo.num_attribs; i++) {
706 vinfo.format[i] = FORMAT_4F;
707 vinfo.interp_mode[i] = INTERP_LINEAR;
708 }
709 }
710
711 draw_set_vertex_info(st->draw, &vinfo);
712 #endif
713 }
714
715
716 /**
717 * Called by VBO to draw arrays when in selection or feedback mode and
718 * to implement glRasterPos.
719 * This is very much like the normal draw_vbo() function above.
720 * Look at code refactoring some day.
721 * Might move this into the failover module some day.
722 */
723 void
724 st_feedback_draw_vbo(GLcontext *ctx,
725 const struct gl_client_array **arrays,
726 const struct _mesa_prim *prims,
727 GLuint nr_prims,
728 const struct _mesa_index_buffer *ib,
729 GLuint min_index,
730 GLuint max_index)
731 {
732 struct st_context *st = ctx->st;
733 struct pipe_context *pipe = st->pipe;
734 struct draw_context *draw = st->draw;
735 const struct st_vertex_program *vp;
736 const struct pipe_shader_state *vs;
737 struct pipe_buffer *index_buffer_handle = 0;
738 struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
739 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
740 GLuint attr, i;
741 ubyte *mapped_constants;
742
743 assert(draw);
744
745 st_validate_state(ctx->st);
746
747 /* must get these after state validation! */
748 vp = ctx->st->vp;
749 vs = &st->vp->state;
750
751 if (!st->vp->draw_shader) {
752 st->vp->draw_shader = draw_create_vertex_shader(draw, vs);
753 }
754
755 /*
756 * Set up the draw module's state.
757 *
758 * We'd like to do this less frequently, but the normal state-update
759 * code sends state updates to the pipe, not to our private draw module.
760 */
761 assert(draw);
762 draw_set_viewport_state(draw, &st->state.viewport);
763 draw_set_clip_state(draw, &st->state.clip);
764 draw_set_rasterizer_state(draw, &st->state.rasterizer);
765 draw_bind_vertex_shader(draw, st->vp->draw_shader);
766 set_feedback_vertex_format(ctx);
767
768 /* loop over TGSI shader inputs to determine vertex buffer
769 * and attribute info
770 */
771 for (attr = 0; attr < vp->num_inputs; attr++) {
772 const GLuint mesaAttr = vp->index_to_input[attr];
773 struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
774 void *map;
775
776 if (bufobj && bufobj->Name) {
777 /* Attribute data is in a VBO.
778 * Recall that for VBOs, the gl_client_array->Ptr field is
779 * really an offset from the start of the VBO, not a pointer.
780 */
781 struct st_buffer_object *stobj = st_buffer_object(bufobj);
782 assert(stobj->buffer);
783
784 vbuffers[attr].buffer = NULL;
785 pipe_buffer_reference(pipe->screen, &vbuffers[attr].buffer, stobj->buffer);
786 vbuffers[attr].buffer_offset = (unsigned) arrays[0]->Ptr;/* in bytes */
787 velements[attr].src_offset = arrays[mesaAttr]->Ptr - arrays[0]->Ptr;
788 }
789 else {
790 /* attribute data is in user-space memory, not a VBO */
791 uint bytes = (arrays[mesaAttr]->Size
792 * _mesa_sizeof_type(arrays[mesaAttr]->Type)
793 * (max_index + 1));
794
795 /* wrap user data */
796 vbuffers[attr].buffer
797 = pipe_user_buffer_create(pipe->screen, (void *) arrays[mesaAttr]->Ptr,
798 bytes);
799 vbuffers[attr].buffer_offset = 0;
800 velements[attr].src_offset = 0;
801 }
802
803 /* common-case setup */
804 vbuffers[attr].pitch = arrays[mesaAttr]->StrideB; /* in bytes */
805 vbuffers[attr].max_index = max_index;
806 velements[attr].vertex_buffer_index = attr;
807 velements[attr].nr_components = arrays[mesaAttr]->Size;
808 velements[attr].src_format = pipe_vertex_format(arrays[mesaAttr]->Type,
809 arrays[mesaAttr]->Size,
810 arrays[mesaAttr]->Normalized);
811 assert(velements[attr].src_format);
812
813 /* tell draw about this attribute */
814 #if 0
815 draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
816 #endif
817
818 /* map the attrib buffer */
819 map = pipe_buffer_map(pipe->screen, vbuffers[attr].buffer,
820 PIPE_BUFFER_USAGE_CPU_READ);
821 draw_set_mapped_vertex_buffer(draw, attr, map);
822 }
823
824 draw_set_vertex_buffers(draw, vp->num_inputs, vbuffers);
825 draw_set_vertex_elements(draw, vp->num_inputs, velements);
826
827 if (ib) {
828 unsigned indexSize;
829 struct gl_buffer_object *bufobj = ib->obj;
830 struct st_buffer_object *stobj = st_buffer_object(bufobj);
831 void *map;
832
833 index_buffer_handle = stobj->buffer;
834
835 switch (ib->type) {
836 case GL_UNSIGNED_INT:
837 indexSize = 4;
838 break;
839 case GL_UNSIGNED_SHORT:
840 indexSize = 2;
841 break;
842 default:
843 assert(0);
844 return;
845 }
846
847 map = pipe_buffer_map(pipe->screen, index_buffer_handle,
848 PIPE_BUFFER_USAGE_CPU_READ);
849 draw_set_mapped_element_buffer(draw, indexSize, map);
850 }
851 else {
852 /* no index/element buffer */
853 draw_set_mapped_element_buffer(draw, 0, NULL);
854 }
855
856
857 /* map constant buffers */
858 mapped_constants = pipe_buffer_map(pipe->screen,
859 st->state.constants[PIPE_SHADER_VERTEX].buffer,
860 PIPE_BUFFER_USAGE_CPU_READ);
861 draw_set_mapped_constant_buffer(st->draw, mapped_constants,
862 st->state.constants[PIPE_SHADER_VERTEX].buffer->size);
863
864
865 /* draw here */
866 for (i = 0; i < nr_prims; i++) {
867 draw_arrays(draw, prims[i].mode, prims[i].start, prims[i].count);
868 }
869
870
871 /* unmap constant buffers */
872 pipe_buffer_unmap(pipe->screen, st->state.constants[PIPE_SHADER_VERTEX].buffer);
873
874 /*
875 * unmap vertex/index buffers
876 */
877 for (i = 0; i < PIPE_MAX_ATTRIBS; i++) {
878 if (draw->pt.vertex_buffer[i].buffer) {
879 pipe_buffer_unmap(pipe->screen, draw->pt.vertex_buffer[i].buffer);
880 pipe_buffer_reference(pipe->screen, &draw->pt.vertex_buffer[i].buffer, NULL);
881 draw_set_mapped_vertex_buffer(draw, i, NULL);
882 }
883 }
884 if (ib) {
885 pipe_buffer_unmap(pipe->screen, index_buffer_handle);
886 draw_set_mapped_element_buffer(draw, 0, NULL);
887 }
888 }
889
890 #endif /* FEATURE_feedback || FEATURE_drawpix */
891
892
893 void st_init_draw( struct st_context *st )
894 {
895 GLcontext *ctx = st->ctx;
896
897 vbo_set_draw_func(ctx, st_draw_vbo);
898 }
899
900
901 void st_destroy_draw( struct st_context *st )
902 {
903 }
904
905