1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Keith Whitwell <keith@tungstengraphics.com>
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
36 #include "shader/prog_uniform.h"
40 #include "st_context.h"
42 #include "st_cb_bufferobjects.h"
44 #include "st_program.h"
46 #include "pipe/p_context.h"
47 #include "pipe/p_defines.h"
48 #include "pipe/p_inlines.h"
50 #include "draw/draw_private.h"
51 #include "draw/draw_context.h"
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
162 * Return a PIPE_FORMAT_x for the given GL datatype and size.
165 pipe_vertex_format(GLenum type
, GLuint size
, GLboolean normalized
)
167 assert((type
>= GL_BYTE
&& type
<= GL_DOUBLE
) ||
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;
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;
200 return 0; /* silence compiler warning */
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
210 setup_edgeflags(GLcontext
*ctx
, GLenum primMode
, GLint start
, GLint count
,
211 const struct gl_client_array
*array
)
213 struct pipe_context
*pipe
= ctx
->st
->pipe
;
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 */
223 struct st_buffer_object
*stobj
= st_buffer_object(array
->BufferObj
);
229 vec
= (unsigned *) calloc(sizeof(unsigned), (count
+ 31) / 32);
233 map
= pipe_buffer_map(pipe
->screen
, stobj
->buffer
, PIPE_BUFFER_USAGE_CPU_READ
);
234 map
= ADD_POINTERS(map
, array
->Ptr
);
236 for (i
= 0; i
< count
; i
++) {
237 if (*((float *) map
))
238 vec
[i
/32] |= 1 << (i
% 32);
240 map
+= array
->StrideB
;
243 pipe_buffer_unmap(pipe
->screen
, stobj
->buffer
);
245 pipe
->set_edgeflags(pipe
, vec
);
250 /* edge flags not needed */
251 pipe
->set_edgeflags(pipe
, NULL
);
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.
263 is_interleaved_arrays(const struct st_vertex_program
*vp
,
264 const struct gl_client_array
**arrays
,
265 GLboolean
*userSpace
)
268 const struct gl_buffer_object
*firstBufObj
= NULL
;
269 GLint firstStride
= -1;
270 GLuint num_client_arrays
= 0;
271 const GLubyte
*client_addr
= NULL
;
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 */
278 if (firstStride
< 0) {
279 firstStride
= stride
;
281 else if (firstStride
!= stride
) {
285 if (!bufObj
|| !bufObj
->Name
) {
287 /* Try to detect if the client-space arrays are
288 * "close" to each other.
291 client_addr
= arrays
[mesaAttr
]->Ptr
;
293 else if (abs(arrays
[mesaAttr
]->Ptr
- client_addr
) > firstStride
) {
294 /* arrays start too far apart */
298 else if (!firstBufObj
) {
299 firstBufObj
= bufObj
;
301 else if (bufObj
!= firstBufObj
) {
306 *userSpace
= (num_client_arrays
== vp
->num_inputs
);
307 /*printf("user space: %d\n", (int) *userSpace);*/
314 * Once we know all the arrays are in user space, this function
315 * computes the memory range occupied by the arrays.
318 get_user_arrays_bounds(const struct st_vertex_program
*vp
,
319 const struct gl_client_array
**arrays
,
321 const GLubyte
**low
, const GLubyte
**high
)
323 const GLubyte
*low_addr
= NULL
;
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
;
335 low_addr
= MIN2(low_addr
, start
);
340 *high
= low_addr
+ (max_index
+ 1) * stride
;
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
351 setup_interleaved_attribs(GLcontext
*ctx
,
352 const struct st_vertex_program
*vp
,
353 const struct gl_client_array
**arrays
,
356 struct pipe_vertex_buffer
*vbuffer
,
357 struct pipe_vertex_element velements
[])
359 struct pipe_context
*pipe
= ctx
->st
->pipe
;
361 const GLubyte
*offset0
;
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
;
369 /*printf("stobj %u = %p\n", attr, (void*)stobj);*/
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);*/
377 pipe_user_buffer_create(pipe
->screen
, (void *) low
, high
- low
);
378 vbuffer
->buffer_offset
= 0;
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
;
387 vbuffer
->pitch
= stride
; /* in bytes */
388 vbuffer
->max_index
= max_index
;
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
);
405 * Set up a separate pipe_vertex_buffer and pipe_vertex_element for each
407 * \param vbuffer returns vertex buffer info
408 * \param velements returns vertex element info
411 setup_non_interleaved_attribs(GLcontext
*ctx
,
412 const struct st_vertex_program
*vp
,
413 const struct gl_client_array
**arrays
,
415 struct pipe_vertex_buffer vbuffer
[],
416 struct pipe_vertex_element velements
[])
418 struct pipe_context
*pipe
= ctx
->st
->pipe
;
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
;
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.
431 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
432 assert(stobj
->buffer
);
433 /*printf("stobj %u = %p\n", attr, (void*) stobj);*/
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;
441 /* attribute data is in user-space memory, not a VBO */
443 /*printf("user-space array %d stride %d\n", attr, stride);*/
446 if (arrays
[mesaAttr
]->Ptr
) {
447 /* user's vertex array */
448 if (arrays
[mesaAttr
]->StrideB
) {
449 bytes
= arrays
[mesaAttr
]->StrideB
* (max_index
+ 1);
452 bytes
= arrays
[mesaAttr
]->Size
453 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
);
455 vbuffer
[attr
].buffer
= pipe_user_buffer_create(pipe
->screen
,
456 (void *) arrays
[mesaAttr
]->Ptr
, bytes
);
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
);
466 vbuffer
[attr
].buffer_offset
= 0;
467 velements
[attr
].src_offset
= 0;
470 assert(velements
[attr
].src_offset
<= 2048); /* 11-bit field */
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
);
488 * Prior to drawing, check that any uniforms referenced by the
489 * current shader have been set. If a uniform has not been set,
493 check_uniforms(GLcontext
*ctx
)
495 const struct gl_shader_program
*shProg
= ctx
->Shader
.CurrentProgram
;
496 if (shProg
&& shProg
->LinkStatus
) {
498 for (i
= 0; i
< shProg
->Uniforms
->NumUniforms
; i
++) {
499 const struct gl_uniform
*u
= &shProg
->Uniforms
->Uniforms
[i
];
500 if (!u
->Initialized
) {
502 "Using shader with uninitialized uniform: %s",
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.
516 st_draw_vbo(GLcontext
*ctx
,
517 const struct gl_client_array
**arrays
,
518 const struct _mesa_prim
*prims
,
520 const struct _mesa_index_buffer
*ib
,
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
];
529 struct pipe_vertex_element velements
[PIPE_MAX_ATTRIBS
];
530 unsigned num_vbuffers
, num_velements
;
533 /* sanity check for pointer arithmetic below */
534 assert(sizeof(arrays
[0]->Ptr
[0]) == 1);
536 st_validate_state(ctx
->st
);
538 /* must get these after state validation! */
540 vs
= &ctx
->st
->vp
->state
;
542 if (MESA_VERBOSE
& VERBOSE_GLSL
) {
547 * Setup the vbuffer[] and velements[] arrays.
549 if (is_interleaved_arrays(vp
, arrays
, &userSpace
)) {
550 /*printf("Draw interleaved\n");*/
551 setup_interleaved_attribs(ctx
, vp
, arrays
, max_index
, userSpace
,
554 num_velements
= vp
->num_inputs
;
555 if (num_velements
== 0)
559 /*printf("Draw non-interleaved\n");*/
560 setup_non_interleaved_attribs(ctx
, vp
, arrays
, max_index
,
562 num_vbuffers
= vp
->num_inputs
;
563 num_velements
= vp
->num_inputs
;
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
);
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
));
584 pipe
->set_vertex_buffers(pipe
, num_vbuffers
, vbuffer
);
585 pipe
->set_vertex_elements(pipe
, num_velements
, velements
);
587 if (num_vbuffers
== 0 || num_velements
== 0)
590 /* do actual drawing */
592 /* indexed primitive */
593 struct gl_buffer_object
*bufobj
= ib
->obj
;
594 struct pipe_buffer
*indexBuf
= NULL
;
595 unsigned indexSize
, indexOffset
, i
;
598 case GL_UNSIGNED_INT
:
601 case GL_UNSIGNED_SHORT
:
604 case GL_UNSIGNED_BYTE
:
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
;
620 /* element/indicies are in user space memory */
621 indexBuf
= pipe_user_buffer_create(pipe
->screen
, (void *) ib
->ptr
,
622 ib
->count
* indexSize
);
627 if (nr_prims
== 1 && pipe
->draw_range_elements
!= NULL
) {
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...
634 setup_edgeflags(ctx
, prims
[i
].mode
,
635 prims
[i
].start
+ indexOffset
, prims
[i
].count
,
636 arrays
[VERT_ATTRIB_EDGEFLAG
]);
638 pipe
->draw_range_elements(pipe
, indexBuf
, indexSize
,
642 prims
[i
].start
+ indexOffset
, prims
[i
].count
);
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
]);
650 pipe
->draw_elements(pipe
, indexBuf
, indexSize
,
652 prims
[i
].start
+ indexOffset
, prims
[i
].count
);
656 pipe_buffer_reference(pipe
->screen
, &indexBuf
, NULL
);
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
]);
666 pipe
->draw_arrays(pipe
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
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
);
675 pipe
->set_vertex_buffers(pipe
, vp
->num_inputs
, vbuffer
);
679 #if FEATURE_feedback || FEATURE_drawpix
682 * Set the (private) draw module's post-transformed vertex format when in
683 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
686 set_feedback_vertex_format(GLcontext
*ctx
)
689 struct st_context
*st
= ctx
->st
;
690 struct vertex_info vinfo
;
693 memset(&vinfo
, 0, sizeof(vinfo
));
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
;
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
;
711 draw_set_vertex_info(st
->draw
, &vinfo
);
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.
724 st_feedback_draw_vbo(GLcontext
*ctx
,
725 const struct gl_client_array
**arrays
,
726 const struct _mesa_prim
*prims
,
728 const struct _mesa_index_buffer
*ib
,
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
];
741 ubyte
*mapped_constants
;
745 st_validate_state(ctx
->st
);
747 /* must get these after state validation! */
751 if (!st
->vp
->draw_shader
) {
752 st
->vp
->draw_shader
= draw_create_vertex_shader(draw
, vs
);
756 * Set up the draw module's state.
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.
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
);
768 /* loop over TGSI shader inputs to determine vertex buffer
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
;
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.
781 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
782 assert(stobj
->buffer
);
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
;
790 /* attribute data is in user-space memory, not a VBO */
791 uint bytes
= (arrays
[mesaAttr
]->Size
792 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
)
796 vbuffers
[attr
].buffer
797 = pipe_user_buffer_create(pipe
->screen
, (void *) arrays
[mesaAttr
]->Ptr
,
799 vbuffers
[attr
].buffer_offset
= 0;
800 velements
[attr
].src_offset
= 0;
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
);
813 /* tell draw about this attribute */
815 draw_set_vertex_buffer(draw
, attr
, &vbuffer
[attr
]);
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
);
824 draw_set_vertex_buffers(draw
, vp
->num_inputs
, vbuffers
);
825 draw_set_vertex_elements(draw
, vp
->num_inputs
, velements
);
829 struct gl_buffer_object
*bufobj
= ib
->obj
;
830 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
833 index_buffer_handle
= stobj
->buffer
;
836 case GL_UNSIGNED_INT
:
839 case GL_UNSIGNED_SHORT
:
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
);
852 /* no index/element buffer */
853 draw_set_mapped_element_buffer(draw
, 0, NULL
);
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
);
866 for (i
= 0; i
< nr_prims
; i
++) {
867 draw_arrays(draw
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
871 /* unmap constant buffers */
872 pipe_buffer_unmap(pipe
->screen
, st
->state
.constants
[PIPE_SHADER_VERTEX
].buffer
);
875 * unmap vertex/index buffers
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
);
885 pipe_buffer_unmap(pipe
->screen
, index_buffer_handle
);
886 draw_set_mapped_element_buffer(draw
, 0, NULL
);
890 #endif /* FEATURE_feedback || FEATURE_drawpix */
893 void st_init_draw( struct st_context
*st
)
895 GLcontext
*ctx
= st
->ctx
;
897 vbo_set_draw_func(ctx
, st_draw_vbo
);
901 void st_destroy_draw( struct st_context
*st
)