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 }
529 else {
530 /*printf("Draw non-interleaved\n");*/
531 setup_non_interleaved_attribs(ctx, vp, arrays, max_index,
532 vbuffer, velements);
533 num_vbuffers = vp->num_inputs;
534 num_velements = vp->num_inputs;
535 }
536
537 #if 0
538 {
539 GLuint i;
540 for (i = 0; i < num_vbuffers; i++) {
541 printf("buffers[%d].pitch = %u\n", i, vbuffer[i].pitch);
542 printf("buffers[%d].max_index = %u\n", i, vbuffer[i].max_index);
543 printf("buffers[%d].buffer_offset = %u\n", i, vbuffer[i].buffer_offset);
544 printf("buffers[%d].buffer = %p\n", i, (void*) vbuffer[i].buffer);
545 }
546 for (i = 0; i < num_velements; i++) {
547 printf("vlements[%d].vbuffer_index = %u\n", i, velements[i].vertex_buffer_index);
548 printf("vlements[%d].src_offset = %u\n", i, velements[i].src_offset);
549 printf("vlements[%d].nr_comps = %u\n", i, velements[i].nr_components);
550 printf("vlements[%d].format = %s\n", i, pf_name(velements[i].src_format));
551 }
552 }
553 #endif
554
555 pipe->set_vertex_buffers(pipe, num_vbuffers, vbuffer);
556 pipe->set_vertex_elements(pipe, num_velements, velements);
557
558 /* do actual drawing */
559 if (ib) {
560 /* indexed primitive */
561 struct gl_buffer_object *bufobj = ib->obj;
562 struct pipe_buffer *indexBuf = NULL;
563 unsigned indexSize, indexOffset, i;
564
565 switch (ib->type) {
566 case GL_UNSIGNED_INT:
567 indexSize = 4;
568 break;
569 case GL_UNSIGNED_SHORT:
570 indexSize = 2;
571 break;
572 case GL_UNSIGNED_BYTE:
573 indexSize = 1;
574 break;
575 default:
576 assert(0);
577 return;
578 }
579
580 /* get/create the index buffer object */
581 if (bufobj && bufobj->Name) {
582 /* elements/indexes are in a real VBO */
583 struct st_buffer_object *stobj = st_buffer_object(bufobj);
584 pipe_buffer_reference(pipe->screen, &indexBuf, stobj->buffer);
585 indexOffset = (unsigned) ib->ptr / indexSize;
586 }
587 else {
588 /* element/indicies are in user space memory */
589 indexBuf = pipe_user_buffer_create(pipe->screen, (void *) ib->ptr,
590 ib->count * indexSize);
591 indexOffset = 0;
592 }
593
594 /* draw */
595 if (nr_prims == 1 && pipe->draw_range_elements != NULL) {
596 i = 0;
597
598 /* XXX: exercise temporary path to pass min/max directly
599 * through to driver & draw module. These interfaces still
600 * need a bit of work...
601 */
602 setup_edgeflags(ctx, prims[i].mode,
603 prims[i].start + indexOffset, prims[i].count,
604 arrays[VERT_ATTRIB_EDGEFLAG]);
605
606 pipe->draw_range_elements(pipe, indexBuf, indexSize,
607 min_index,
608 max_index,
609 prims[i].mode,
610 prims[i].start + indexOffset, prims[i].count);
611 }
612 else {
613 for (i = 0; i < nr_prims; i++) {
614 setup_edgeflags(ctx, prims[i].mode,
615 prims[i].start + indexOffset, prims[i].count,
616 arrays[VERT_ATTRIB_EDGEFLAG]);
617
618 pipe->draw_elements(pipe, indexBuf, indexSize,
619 prims[i].mode,
620 prims[i].start + indexOffset, prims[i].count);
621 }
622 }
623
624 pipe_buffer_reference(pipe->screen, &indexBuf, NULL);
625 }
626 else {
627 /* non-indexed */
628 GLuint i;
629 for (i = 0; i < nr_prims; i++) {
630 setup_edgeflags(ctx, prims[i].mode,
631 prims[i].start, prims[i].count,
632 arrays[VERT_ATTRIB_EDGEFLAG]);
633
634 pipe->draw_arrays(pipe, prims[i].mode, prims[i].start, prims[i].count);
635 }
636 }
637
638 /* unreference buffers (frees wrapped user-space buffer objects) */
639 for (attr = 0; attr < num_vbuffers; attr++) {
640 pipe_buffer_reference(pipe->screen, &vbuffer[attr].buffer, NULL);
641 assert(!vbuffer[attr].buffer);
642 }
643 pipe->set_vertex_buffers(pipe, vp->num_inputs, vbuffer);
644 }
645
646
647 #if FEATURE_feedback || FEATURE_drawpix
648
649 /**
650 * Set the (private) draw module's post-transformed vertex format when in
651 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
652 */
653 static void
654 set_feedback_vertex_format(GLcontext *ctx)
655 {
656 #if 0
657 struct st_context *st = ctx->st;
658 struct vertex_info vinfo;
659 GLuint i;
660
661 memset(&vinfo, 0, sizeof(vinfo));
662
663 if (ctx->RenderMode == GL_SELECT) {
664 assert(ctx->RenderMode == GL_SELECT);
665 vinfo.num_attribs = 1;
666 vinfo.format[0] = FORMAT_4F;
667 vinfo.interp_mode[0] = INTERP_LINEAR;
668 }
669 else {
670 /* GL_FEEDBACK, or glRasterPos */
671 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
672 vinfo.num_attribs = st->state.vs->cso->state.num_outputs;
673 for (i = 0; i < vinfo.num_attribs; i++) {
674 vinfo.format[i] = FORMAT_4F;
675 vinfo.interp_mode[i] = INTERP_LINEAR;
676 }
677 }
678
679 draw_set_vertex_info(st->draw, &vinfo);
680 #endif
681 }
682
683
684 /**
685 * Called by VBO to draw arrays when in selection or feedback mode and
686 * to implement glRasterPos.
687 * This is very much like the normal draw_vbo() function above.
688 * Look at code refactoring some day.
689 * Might move this into the failover module some day.
690 */
691 void
692 st_feedback_draw_vbo(GLcontext *ctx,
693 const struct gl_client_array **arrays,
694 const struct _mesa_prim *prims,
695 GLuint nr_prims,
696 const struct _mesa_index_buffer *ib,
697 GLuint min_index,
698 GLuint max_index)
699 {
700 struct st_context *st = ctx->st;
701 struct pipe_context *pipe = st->pipe;
702 struct draw_context *draw = st->draw;
703 const struct st_vertex_program *vp;
704 const struct pipe_shader_state *vs;
705 struct pipe_buffer *index_buffer_handle = 0;
706 struct pipe_vertex_buffer vbuffers[PIPE_MAX_SHADER_INPUTS];
707 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
708 GLuint attr, i;
709 ubyte *mapped_constants;
710
711 assert(draw);
712
713 st_validate_state(ctx->st);
714
715 /* must get these after state validation! */
716 vp = ctx->st->vp;
717 vs = &st->vp->state;
718
719 if (!st->vp->draw_shader) {
720 st->vp->draw_shader = draw_create_vertex_shader(draw, vs);
721 }
722
723 /*
724 * Set up the draw module's state.
725 *
726 * We'd like to do this less frequently, but the normal state-update
727 * code sends state updates to the pipe, not to our private draw module.
728 */
729 assert(draw);
730 draw_set_viewport_state(draw, &st->state.viewport);
731 draw_set_clip_state(draw, &st->state.clip);
732 draw_set_rasterizer_state(draw, &st->state.rasterizer);
733 draw_bind_vertex_shader(draw, st->vp->draw_shader);
734 set_feedback_vertex_format(ctx);
735
736 /* loop over TGSI shader inputs to determine vertex buffer
737 * and attribute info
738 */
739 for (attr = 0; attr < vp->num_inputs; attr++) {
740 const GLuint mesaAttr = vp->index_to_input[attr];
741 struct gl_buffer_object *bufobj = arrays[mesaAttr]->BufferObj;
742 void *map;
743
744 if (bufobj && bufobj->Name) {
745 /* Attribute data is in a VBO.
746 * Recall that for VBOs, the gl_client_array->Ptr field is
747 * really an offset from the start of the VBO, not a pointer.
748 */
749 struct st_buffer_object *stobj = st_buffer_object(bufobj);
750 assert(stobj->buffer);
751
752 vbuffers[attr].buffer = NULL;
753 pipe_buffer_reference(pipe->screen, &vbuffers[attr].buffer, stobj->buffer);
754 vbuffers[attr].buffer_offset = (unsigned) arrays[0]->Ptr;/* in bytes */
755 velements[attr].src_offset = arrays[mesaAttr]->Ptr - arrays[0]->Ptr;
756 }
757 else {
758 /* attribute data is in user-space memory, not a VBO */
759 uint bytes = (arrays[mesaAttr]->Size
760 * _mesa_sizeof_type(arrays[mesaAttr]->Type)
761 * (max_index + 1));
762
763 /* wrap user data */
764 vbuffers[attr].buffer
765 = pipe_user_buffer_create(pipe->screen, (void *) arrays[mesaAttr]->Ptr,
766 bytes);
767 vbuffers[attr].buffer_offset = 0;
768 velements[attr].src_offset = 0;
769 }
770
771 /* common-case setup */
772 vbuffers[attr].pitch = arrays[mesaAttr]->StrideB; /* in bytes */
773 vbuffers[attr].max_index = max_index;
774 velements[attr].vertex_buffer_index = attr;
775 velements[attr].nr_components = arrays[mesaAttr]->Size;
776 velements[attr].src_format = pipe_vertex_format(arrays[mesaAttr]->Type,
777 arrays[mesaAttr]->Size,
778 arrays[mesaAttr]->Normalized);
779 assert(velements[attr].src_format);
780
781 /* tell draw about this attribute */
782 #if 0
783 draw_set_vertex_buffer(draw, attr, &vbuffer[attr]);
784 #endif
785
786 /* map the attrib buffer */
787 map = pipe_buffer_map(pipe->screen, vbuffers[attr].buffer,
788 PIPE_BUFFER_USAGE_CPU_READ);
789 draw_set_mapped_vertex_buffer(draw, attr, map);
790 }
791
792 draw_set_vertex_buffers(draw, vp->num_inputs, vbuffers);
793 draw_set_vertex_elements(draw, vp->num_inputs, velements);
794
795 if (ib) {
796 unsigned indexSize;
797 struct gl_buffer_object *bufobj = ib->obj;
798 struct st_buffer_object *stobj = st_buffer_object(bufobj);
799 void *map;
800
801 index_buffer_handle = stobj->buffer;
802
803 switch (ib->type) {
804 case GL_UNSIGNED_INT:
805 indexSize = 4;
806 break;
807 case GL_UNSIGNED_SHORT:
808 indexSize = 2;
809 break;
810 default:
811 assert(0);
812 return;
813 }
814
815 map = pipe_buffer_map(pipe->screen, index_buffer_handle,
816 PIPE_BUFFER_USAGE_CPU_READ);
817 draw_set_mapped_element_buffer(draw, indexSize, map);
818 }
819 else {
820 /* no index/element buffer */
821 draw_set_mapped_element_buffer(draw, 0, NULL);
822 }
823
824
825 /* map constant buffers */
826 mapped_constants = pipe_buffer_map(pipe->screen,
827 st->state.constants[PIPE_SHADER_VERTEX].buffer,
828 PIPE_BUFFER_USAGE_CPU_READ);
829 draw_set_mapped_constant_buffer(st->draw, mapped_constants,
830 st->state.constants[PIPE_SHADER_VERTEX].buffer->size);
831
832
833 /* draw here */
834 for (i = 0; i < nr_prims; i++) {
835 draw_arrays(draw, prims[i].mode, prims[i].start, prims[i].count);
836 }
837
838
839 /* unmap constant buffers */
840 pipe_buffer_unmap(pipe->screen, st->state.constants[PIPE_SHADER_VERTEX].buffer);
841
842 /*
843 * unmap vertex/index buffers
844 */
845 for (i = 0; i < PIPE_MAX_ATTRIBS; i++) {
846 if (draw->pt.vertex_buffer[i].buffer) {
847 pipe_buffer_unmap(pipe->screen, draw->pt.vertex_buffer[i].buffer);
848 pipe_buffer_reference(pipe->screen, &draw->pt.vertex_buffer[i].buffer, NULL);
849 draw_set_mapped_vertex_buffer(draw, i, NULL);
850 }
851 }
852 if (ib) {
853 pipe_buffer_unmap(pipe->screen, index_buffer_handle);
854 draw_set_mapped_element_buffer(draw, 0, NULL);
855 }
856 }
857
858 #endif /* FEATURE_feedback || FEATURE_drawpix */
859
860
861 void st_init_draw( struct st_context *st )
862 {
863 GLcontext *ctx = st->ctx;
864
865 vbo_set_draw_func(ctx, st_draw_vbo);
866 }
867
868
869 void st_destroy_draw( struct st_context *st )
870 {
871 }
872
873