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"
38 #include "st_context.h"
40 #include "st_cb_bufferobjects.h"
42 #include "st_program.h"
44 #include "pipe/p_context.h"
45 #include "pipe/p_defines.h"
46 #include "pipe/p_inlines.h"
48 #include "draw/draw_private.h"
49 #include "draw/draw_context.h"
52 static GLuint double_types
[4] = {
53 PIPE_FORMAT_R64_FLOAT
,
54 PIPE_FORMAT_R64G64_FLOAT
,
55 PIPE_FORMAT_R64G64B64_FLOAT
,
56 PIPE_FORMAT_R64G64B64A64_FLOAT
59 static GLuint float_types
[4] = {
60 PIPE_FORMAT_R32_FLOAT
,
61 PIPE_FORMAT_R32G32_FLOAT
,
62 PIPE_FORMAT_R32G32B32_FLOAT
,
63 PIPE_FORMAT_R32G32B32A32_FLOAT
66 static GLuint uint_types_norm
[4] = {
67 PIPE_FORMAT_R32_UNORM
,
68 PIPE_FORMAT_R32G32_UNORM
,
69 PIPE_FORMAT_R32G32B32_UNORM
,
70 PIPE_FORMAT_R32G32B32A32_UNORM
73 static GLuint uint_types_scale
[4] = {
74 PIPE_FORMAT_R32_USCALED
,
75 PIPE_FORMAT_R32G32_USCALED
,
76 PIPE_FORMAT_R32G32B32_USCALED
,
77 PIPE_FORMAT_R32G32B32A32_USCALED
80 static GLuint int_types_norm
[4] = {
81 PIPE_FORMAT_R32_SNORM
,
82 PIPE_FORMAT_R32G32_SNORM
,
83 PIPE_FORMAT_R32G32B32_SNORM
,
84 PIPE_FORMAT_R32G32B32A32_SNORM
87 static GLuint int_types_scale
[4] = {
88 PIPE_FORMAT_R32_SSCALED
,
89 PIPE_FORMAT_R32G32_SSCALED
,
90 PIPE_FORMAT_R32G32B32_SSCALED
,
91 PIPE_FORMAT_R32G32B32A32_SSCALED
94 static GLuint ushort_types_norm
[4] = {
95 PIPE_FORMAT_R16_UNORM
,
96 PIPE_FORMAT_R16G16_UNORM
,
97 PIPE_FORMAT_R16G16B16_UNORM
,
98 PIPE_FORMAT_R16G16B16A16_UNORM
101 static GLuint ushort_types_scale
[4] = {
102 PIPE_FORMAT_R16_USCALED
,
103 PIPE_FORMAT_R16G16_USCALED
,
104 PIPE_FORMAT_R16G16B16_USCALED
,
105 PIPE_FORMAT_R16G16B16A16_USCALED
108 static GLuint short_types_norm
[4] = {
109 PIPE_FORMAT_R16_SNORM
,
110 PIPE_FORMAT_R16G16_SNORM
,
111 PIPE_FORMAT_R16G16B16_SNORM
,
112 PIPE_FORMAT_R16G16B16A16_SNORM
115 static GLuint short_types_scale
[4] = {
116 PIPE_FORMAT_R16_SSCALED
,
117 PIPE_FORMAT_R16G16_SSCALED
,
118 PIPE_FORMAT_R16G16B16_SSCALED
,
119 PIPE_FORMAT_R16G16B16A16_SSCALED
122 static GLuint ubyte_types_norm
[4] = {
123 PIPE_FORMAT_R8_UNORM
,
124 PIPE_FORMAT_R8G8_UNORM
,
125 PIPE_FORMAT_R8G8B8_UNORM
,
126 PIPE_FORMAT_R8G8B8A8_UNORM
129 static GLuint ubyte_types_scale
[4] = {
130 PIPE_FORMAT_R8_USCALED
,
131 PIPE_FORMAT_R8G8_USCALED
,
132 PIPE_FORMAT_R8G8B8_USCALED
,
133 PIPE_FORMAT_R8G8B8A8_USCALED
136 static GLuint byte_types_norm
[4] = {
137 PIPE_FORMAT_R8_SNORM
,
138 PIPE_FORMAT_R8G8_SNORM
,
139 PIPE_FORMAT_R8G8B8_SNORM
,
140 PIPE_FORMAT_R8G8B8A8_SNORM
143 static GLuint byte_types_scale
[4] = {
144 PIPE_FORMAT_R8_SSCALED
,
145 PIPE_FORMAT_R8G8_SSCALED
,
146 PIPE_FORMAT_R8G8B8_SSCALED
,
147 PIPE_FORMAT_R8G8B8A8_SSCALED
152 * Return a PIPE_FORMAT_x for the given GL datatype and size.
155 pipe_vertex_format(GLenum type
, GLuint size
, GLboolean normalized
)
157 assert(type
>= GL_BYTE
);
158 assert(type
<= GL_DOUBLE
);
164 case GL_DOUBLE
: return double_types
[size
-1];
165 case GL_FLOAT
: return float_types
[size
-1];
166 case GL_INT
: return int_types_norm
[size
-1];
167 case GL_SHORT
: return short_types_norm
[size
-1];
168 case GL_BYTE
: return byte_types_norm
[size
-1];
169 case GL_UNSIGNED_INT
: return uint_types_norm
[size
-1];
170 case GL_UNSIGNED_SHORT
: return ushort_types_norm
[size
-1];
171 case GL_UNSIGNED_BYTE
: return ubyte_types_norm
[size
-1];
172 default: assert(0); return 0;
177 case GL_DOUBLE
: return double_types
[size
-1];
178 case GL_FLOAT
: return float_types
[size
-1];
179 case GL_INT
: return int_types_scale
[size
-1];
180 case GL_SHORT
: return short_types_scale
[size
-1];
181 case GL_BYTE
: return byte_types_scale
[size
-1];
182 case GL_UNSIGNED_INT
: return uint_types_scale
[size
-1];
183 case GL_UNSIGNED_SHORT
: return ushort_types_scale
[size
-1];
184 case GL_UNSIGNED_BYTE
: return ubyte_types_scale
[size
-1];
185 default: assert(0); return 0;
188 return 0; /* silence compiler warning */
193 * If edge flags are needed, setup an bitvector of flags and call
194 * pipe->set_edgeflags().
195 * XXX memleak: need to free the returned pointer at some point
198 setup_edgeflags(GLcontext
*ctx
, GLenum primMode
, GLint start
, GLint count
,
199 const struct gl_client_array
*array
)
201 struct pipe_context
*pipe
= ctx
->st
->pipe
;
203 if ((primMode
== GL_TRIANGLES
||
204 primMode
== GL_QUADS
||
205 primMode
== GL_POLYGON
) &&
206 (ctx
->Polygon
.FrontMode
!= GL_FILL
||
207 ctx
->Polygon
.BackMode
!= GL_FILL
)) {
208 /* need edge flags */
211 struct st_buffer_object
*stobj
= st_buffer_object(array
->BufferObj
);
217 vec
= (unsigned *) calloc(sizeof(unsigned), (count
+ 31) / 32);
221 map
= pipe_buffer_map(pipe
, stobj
->buffer
, PIPE_BUFFER_USAGE_CPU_READ
);
222 map
= ADD_POINTERS(map
, array
->Ptr
);
224 for (i
= 0; i
< count
; i
++) {
225 if (*((float *) map
))
226 vec
[i
/32] |= 1 << (i
% 32);
228 map
+= array
->StrideB
;
231 pipe_buffer_unmap(pipe
, stobj
->buffer
);
233 pipe
->set_edgeflags(pipe
, vec
);
238 /* edge flags not needed */
239 pipe
->set_edgeflags(pipe
, NULL
);
247 * This function gets plugged into the VBO module and is called when
248 * we have something to render.
249 * Basically, translate the information into the format expected by pipe.
252 st_draw_vbo(GLcontext
*ctx
,
253 const struct gl_client_array
**arrays
,
254 const struct _mesa_prim
*prims
,
256 const struct _mesa_index_buffer
*ib
,
260 struct pipe_context
*pipe
= ctx
->st
->pipe
;
261 const struct st_vertex_program
*vp
;
262 const struct pipe_shader_state
*vs
;
263 struct pipe_vertex_buffer vbuffer
[PIPE_MAX_SHADER_INPUTS
];
265 struct pipe_vertex_element velements
[PIPE_MAX_ATTRIBS
];
267 /* sanity check for pointer arithmetic below */
268 assert(sizeof(arrays
[0]->Ptr
[0]) == 1);
270 st_validate_state(ctx
->st
);
272 /* must get these after state validation! */
274 vs
= &ctx
->st
->vp
->state
;
276 /* loop over TGSI shader inputs to determine vertex buffer
279 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
280 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
281 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
283 if (bufobj
&& bufobj
->Name
) {
284 /* Attribute data is in a VBO.
285 * Recall that for VBOs, the gl_client_array->Ptr field is
286 * really an offset from the start of the VBO, not a pointer.
288 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
289 assert(stobj
->buffer
);
291 vbuffer
[attr
].buffer
= NULL
;
292 pipe_reference_buffer(pipe
, &vbuffer
[attr
].buffer
, stobj
->buffer
);
293 vbuffer
[attr
].buffer_offset
= (unsigned) arrays
[0]->Ptr
;/* in bytes */
294 velements
[attr
].src_offset
= arrays
[mesaAttr
]->Ptr
- arrays
[0]->Ptr
;
295 assert(velements
[attr
].src_offset
<= 2048); /* 11-bit field */
298 /* attribute data is in user-space memory, not a VBO */
301 if (!arrays
[mesaAttr
]->StrideB
) {
302 bytes
= arrays
[mesaAttr
]->Size
303 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
);
305 bytes
= arrays
[mesaAttr
]->StrideB
* (max_index
+ 1);
310 = pipe_user_buffer_create(pipe
, (void *) arrays
[mesaAttr
]->Ptr
,
312 vbuffer
[attr
].buffer_offset
= 0;
313 velements
[attr
].src_offset
= 0;
316 /* common-case setup */
317 vbuffer
[attr
].pitch
= arrays
[mesaAttr
]->StrideB
; /* in bytes */
318 vbuffer
[attr
].max_index
= max_index
;
319 velements
[attr
].vertex_buffer_index
= attr
;
320 velements
[attr
].nr_components
= arrays
[mesaAttr
]->Size
;
321 velements
[attr
].src_format
322 = pipe_vertex_format(arrays
[mesaAttr
]->Type
,
323 arrays
[mesaAttr
]->Size
,
324 arrays
[mesaAttr
]->Normalized
);
325 assert(velements
[attr
].src_format
);
328 pipe
->set_vertex_buffers(pipe
, vp
->num_inputs
, vbuffer
);
329 pipe
->set_vertex_elements(pipe
, vp
->num_inputs
, velements
);
331 /* do actual drawing */
333 /* indexed primitive */
334 struct gl_buffer_object
*bufobj
= ib
->obj
;
335 struct pipe_buffer
*indexBuf
= NULL
;
336 unsigned indexSize
, indexOffset
, i
;
339 case GL_UNSIGNED_INT
:
342 case GL_UNSIGNED_SHORT
:
345 case GL_UNSIGNED_BYTE
:
353 /* get/create the index buffer object */
354 if (bufobj
&& bufobj
->Name
) {
355 /* elements/indexes are in a real VBO */
356 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
357 pipe_reference_buffer(pipe
, &indexBuf
, stobj
->buffer
);
358 indexOffset
= (unsigned) ib
->ptr
/ indexSize
;
361 /* element/indicies are in user space memory */
362 indexBuf
= pipe_user_buffer_create(pipe
, (void *) ib
->ptr
,
363 ib
->count
* indexSize
);
368 for (i
= 0; i
< nr_prims
; i
++) {
369 setup_edgeflags(ctx
, prims
[i
].mode
,
370 prims
[i
].start
+ indexOffset
, prims
[i
].count
,
371 arrays
[VERT_ATTRIB_EDGEFLAG
]);
373 pipe
->draw_elements(pipe
, indexBuf
, indexSize
,
375 prims
[i
].start
+ indexOffset
, prims
[i
].count
);
378 pipe_reference_buffer(pipe
, &indexBuf
, NULL
);
383 for (i
= 0; i
< nr_prims
; i
++) {
384 setup_edgeflags(ctx
, prims
[i
].mode
,
385 prims
[i
].start
, prims
[i
].count
,
386 arrays
[VERT_ATTRIB_EDGEFLAG
]);
388 pipe
->draw_arrays(pipe
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
392 /* unreference buffers (frees wrapped user-space buffer objects) */
393 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
394 pipe_reference_buffer(pipe
, &vbuffer
[attr
].buffer
, NULL
);
395 assert(!vbuffer
[attr
].buffer
);
397 pipe
->set_vertex_buffers(pipe
, vp
->num_inputs
, vbuffer
);
403 * Set the (private) draw module's post-transformed vertex format when in
404 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
407 set_feedback_vertex_format(GLcontext
*ctx
)
410 struct st_context
*st
= ctx
->st
;
411 struct vertex_info vinfo
;
414 memset(&vinfo
, 0, sizeof(vinfo
));
416 if (ctx
->RenderMode
== GL_SELECT
) {
417 assert(ctx
->RenderMode
== GL_SELECT
);
418 vinfo
.num_attribs
= 1;
419 vinfo
.format
[0] = FORMAT_4F
;
420 vinfo
.interp_mode
[0] = INTERP_LINEAR
;
423 /* GL_FEEDBACK, or glRasterPos */
424 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
425 vinfo
.num_attribs
= st
->state
.vs
->cso
->state
.num_outputs
;
426 for (i
= 0; i
< vinfo
.num_attribs
; i
++) {
427 vinfo
.format
[i
] = FORMAT_4F
;
428 vinfo
.interp_mode
[i
] = INTERP_LINEAR
;
432 draw_set_vertex_info(st
->draw
, &vinfo
);
438 * Called by VBO to draw arrays when in selection or feedback mode and
439 * to implement glRasterPos.
440 * This is very much like the normal draw_vbo() function above.
441 * Look at code refactoring some day.
442 * Might move this into the failover module some day.
445 st_feedback_draw_vbo(GLcontext
*ctx
,
446 const struct gl_client_array
**arrays
,
447 const struct _mesa_prim
*prims
,
449 const struct _mesa_index_buffer
*ib
,
453 struct st_context
*st
= ctx
->st
;
454 struct pipe_context
*pipe
= st
->pipe
;
455 struct draw_context
*draw
= st
->draw
;
456 const struct st_vertex_program
*vp
;
457 const struct pipe_shader_state
*vs
;
458 struct pipe_buffer
*index_buffer_handle
= 0;
459 struct pipe_vertex_buffer vbuffers
[PIPE_MAX_SHADER_INPUTS
];
460 struct pipe_vertex_element velements
[PIPE_MAX_ATTRIBS
];
462 ubyte
*mapped_constants
;
466 st_validate_state(ctx
->st
);
468 /* must get these after state validation! */
472 if (!st
->vp
->draw_shader
) {
473 st
->vp
->draw_shader
= draw_create_vertex_shader(draw
, vs
);
477 * Set up the draw module's state.
479 * We'd like to do this less frequently, but the normal state-update
480 * code sends state updates to the pipe, not to our private draw module.
483 draw_set_viewport_state(draw
, &st
->state
.viewport
);
484 draw_set_clip_state(draw
, &st
->state
.clip
);
485 draw_set_rasterizer_state(draw
, &st
->state
.rasterizer
);
486 draw_bind_vertex_shader(draw
, st
->vp
->draw_shader
);
487 set_feedback_vertex_format(ctx
);
489 /* loop over TGSI shader inputs to determine vertex buffer
492 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
493 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
494 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
497 if (bufobj
&& bufobj
->Name
) {
498 /* Attribute data is in a VBO.
499 * Recall that for VBOs, the gl_client_array->Ptr field is
500 * really an offset from the start of the VBO, not a pointer.
502 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
503 assert(stobj
->buffer
);
505 vbuffers
[attr
].buffer
= NULL
;
506 pipe_reference_buffer(pipe
, &vbuffers
[attr
].buffer
, stobj
->buffer
);
507 vbuffers
[attr
].buffer_offset
= (unsigned) arrays
[0]->Ptr
;/* in bytes */
508 velements
[attr
].src_offset
= arrays
[mesaAttr
]->Ptr
- arrays
[0]->Ptr
;
511 /* attribute data is in user-space memory, not a VBO */
512 uint bytes
= (arrays
[mesaAttr
]->Size
513 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
)
517 vbuffers
[attr
].buffer
518 = pipe_user_buffer_create(pipe
, (void *) arrays
[mesaAttr
]->Ptr
,
520 vbuffers
[attr
].buffer_offset
= 0;
521 velements
[attr
].src_offset
= 0;
524 /* common-case setup */
525 vbuffers
[attr
].pitch
= arrays
[mesaAttr
]->StrideB
; /* in bytes */
526 vbuffers
[attr
].max_index
= max_index
;
527 velements
[attr
].vertex_buffer_index
= attr
;
528 velements
[attr
].nr_components
= arrays
[mesaAttr
]->Size
;
529 velements
[attr
].src_format
= pipe_vertex_format(arrays
[mesaAttr
]->Type
,
530 arrays
[mesaAttr
]->Size
,
531 arrays
[mesaAttr
]->Normalized
);
532 assert(velements
[attr
].src_format
);
534 /* tell draw about this attribute */
536 draw_set_vertex_buffer(draw
, attr
, &vbuffer
[attr
]);
539 /* map the attrib buffer */
540 map
= pipe_buffer_map(pipe
, vbuffers
[attr
].buffer
,
541 PIPE_BUFFER_USAGE_CPU_READ
);
542 draw_set_mapped_vertex_buffer(draw
, attr
, map
);
545 draw_set_vertex_buffers(draw
, vp
->num_inputs
, vbuffers
);
546 draw_set_vertex_elements(draw
, vp
->num_inputs
, velements
);
550 struct gl_buffer_object
*bufobj
= ib
->obj
;
551 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
554 index_buffer_handle
= stobj
->buffer
;
557 case GL_UNSIGNED_INT
:
560 case GL_UNSIGNED_SHORT
:
568 map
= pipe_buffer_map(pipe
, index_buffer_handle
,
569 PIPE_BUFFER_USAGE_CPU_READ
);
570 draw_set_mapped_element_buffer(draw
, indexSize
, map
);
573 /* no index/element buffer */
574 draw_set_mapped_element_buffer(draw
, 0, NULL
);
578 /* map constant buffers */
579 mapped_constants
= pipe_buffer_map(pipe
,
580 st
->state
.constants
[PIPE_SHADER_VERTEX
].buffer
,
581 PIPE_BUFFER_USAGE_CPU_READ
);
582 draw_set_mapped_constant_buffer(st
->draw
, mapped_constants
);
586 for (i
= 0; i
< nr_prims
; i
++) {
587 draw_arrays(draw
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
591 /* unmap constant buffers */
592 pipe_buffer_unmap(pipe
, st
->state
.constants
[PIPE_SHADER_VERTEX
].buffer
);
595 * unmap vertex/index buffers
597 for (i
= 0; i
< PIPE_MAX_ATTRIBS
; i
++) {
598 if (draw
->pt
.vertex_buffer
[i
].buffer
) {
599 pipe_buffer_unmap(pipe
, draw
->pt
.vertex_buffer
[i
].buffer
);
600 pipe_reference_buffer(pipe
, &draw
->pt
.vertex_buffer
[i
].buffer
, NULL
);
601 draw_set_mapped_vertex_buffer(draw
, i
, NULL
);
605 pipe_buffer_unmap(pipe
, index_buffer_handle
);
606 draw_set_mapped_element_buffer(draw
, 0, NULL
);
612 void st_init_draw( struct st_context
*st
)
614 GLcontext
*ctx
= st
->ctx
;
616 vbo_set_draw_func(ctx
, st_draw_vbo
);
620 void st_destroy_draw( struct st_context
*st
)