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