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"
39 #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_winsys.h"
47 #include "pipe/p_inlines.h"
49 #include "draw/draw_private.h"
50 #include "draw/draw_context.h"
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
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
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
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
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
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
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
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
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
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
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
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
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
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
153 * Return a PIPE_FORMAT_x for the given GL datatype and size.
156 pipe_vertex_format(GLenum type
, GLuint size
, GLboolean normalized
)
158 assert(type
>= GL_BYTE
);
159 assert(type
<= GL_DOUBLE
);
165 case GL_DOUBLE
: return double_types
[size
-1];
166 case GL_FLOAT
: return float_types
[size
-1];
167 case GL_INT
: return int_types_norm
[size
-1];
168 case GL_SHORT
: return short_types_norm
[size
-1];
169 case GL_BYTE
: return byte_types_norm
[size
-1];
170 case GL_UNSIGNED_INT
: return uint_types_norm
[size
-1];
171 case GL_UNSIGNED_SHORT
: return ushort_types_norm
[size
-1];
172 case GL_UNSIGNED_BYTE
: return ubyte_types_norm
[size
-1];
173 default: assert(0); return 0;
178 case GL_DOUBLE
: return double_types
[size
-1];
179 case GL_FLOAT
: return float_types
[size
-1];
180 case GL_INT
: return int_types_scale
[size
-1];
181 case GL_SHORT
: return short_types_scale
[size
-1];
182 case GL_BYTE
: return byte_types_scale
[size
-1];
183 case GL_UNSIGNED_INT
: return uint_types_scale
[size
-1];
184 case GL_UNSIGNED_SHORT
: return ushort_types_scale
[size
-1];
185 case GL_UNSIGNED_BYTE
: return ubyte_types_scale
[size
-1];
186 default: assert(0); return 0;
189 return 0; /* silence compiler warning */
194 * If edge flags are needed, setup an bitvector of flags and call
195 * pipe->set_edgeflags().
196 * XXX memleak: need to free the returned pointer at some point
199 setup_edgeflags(GLcontext
*ctx
, GLenum primMode
, GLint start
, GLint count
,
200 const struct gl_client_array
*array
)
202 struct pipe_context
*pipe
= ctx
->st
->pipe
;
204 if ((primMode
== GL_TRIANGLES
||
205 primMode
== GL_QUADS
||
206 primMode
== GL_POLYGON
) &&
207 (ctx
->Polygon
.FrontMode
!= GL_FILL
||
208 ctx
->Polygon
.BackMode
!= GL_FILL
)) {
209 /* need edge flags */
212 struct st_buffer_object
*stobj
= st_buffer_object(array
->BufferObj
);
218 vec
= (unsigned *) calloc(sizeof(unsigned), (count
+ 31) / 32);
222 map
= pipe
->winsys
->buffer_map(pipe
->winsys
, stobj
->buffer
,
223 PIPE_BUFFER_USAGE_CPU_READ
);
224 map
= ADD_POINTERS(map
, array
->Ptr
);
226 for (i
= 0; i
< count
; i
++) {
227 if (*((float *) map
))
228 vec
[i
/32] |= 1 << (i
% 32);
230 map
+= array
->StrideB
;
233 pipe
->winsys
->buffer_unmap(pipe
->winsys
, stobj
->buffer
);
235 pipe
->set_edgeflags(pipe
, vec
);
240 /* edge flags not needed */
241 pipe
->set_edgeflags(pipe
, NULL
);
249 * This function gets plugged into the VBO module and is called when
250 * we have something to render.
251 * Basically, translate the information into the format expected by pipe.
254 st_draw_vbo(GLcontext
*ctx
,
255 const struct gl_client_array
**arrays
,
256 const struct _mesa_prim
*prims
,
258 const struct _mesa_index_buffer
*ib
,
262 struct pipe_context
*pipe
= ctx
->st
->pipe
;
263 struct pipe_winsys
*winsys
= pipe
->winsys
;
264 const struct st_vertex_program
*vp
;
265 const struct pipe_shader_state
*vs
;
266 struct pipe_vertex_buffer vbuffer
[PIPE_MAX_SHADER_INPUTS
];
268 struct pipe_vertex_element velements
[PIPE_MAX_ATTRIBS
];
270 /* sanity check for pointer arithmetic below */
271 assert(sizeof(arrays
[0]->Ptr
[0]) == 1);
273 st_validate_state(ctx
->st
);
275 /* must get these after state validation! */
277 vs
= &ctx
->st
->vp
->state
;
279 /* loop over TGSI shader inputs to determine vertex buffer
282 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
283 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
284 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
286 if (bufobj
&& bufobj
->Name
) {
287 /* Attribute data is in a VBO.
288 * Recall that for VBOs, the gl_client_array->Ptr field is
289 * really an offset from the start of the VBO, not a pointer.
291 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
292 assert(stobj
->buffer
);
294 vbuffer
[attr
].buffer
= NULL
;
295 pipe_buffer_reference(winsys
, &vbuffer
[attr
].buffer
, stobj
->buffer
);
296 vbuffer
[attr
].buffer_offset
= (unsigned) arrays
[0]->Ptr
;/* in bytes */
297 velements
[attr
].src_offset
= arrays
[mesaAttr
]->Ptr
- arrays
[0]->Ptr
;
298 assert(velements
[attr
].src_offset
<= 2048); /* 11-bit field */
301 /* attribute data is in user-space memory, not a VBO */
304 if (!arrays
[mesaAttr
]->StrideB
) {
305 bytes
= arrays
[mesaAttr
]->Size
306 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
);
308 bytes
= arrays
[mesaAttr
]->StrideB
* (max_index
+ 1);
313 = winsys
->user_buffer_create(winsys
,
314 (void *) arrays
[mesaAttr
]->Ptr
,
316 vbuffer
[attr
].buffer_offset
= 0;
317 velements
[attr
].src_offset
= 0;
320 /* common-case setup */
321 vbuffer
[attr
].pitch
= arrays
[mesaAttr
]->StrideB
; /* in bytes */
322 vbuffer
[attr
].max_index
= max_index
;
323 velements
[attr
].vertex_buffer_index
= attr
;
324 velements
[attr
].nr_components
= arrays
[mesaAttr
]->Size
;
325 velements
[attr
].src_format
326 = pipe_vertex_format(arrays
[mesaAttr
]->Type
,
327 arrays
[mesaAttr
]->Size
,
328 arrays
[mesaAttr
]->Normalized
);
329 assert(velements
[attr
].src_format
);
332 pipe
->set_vertex_buffers(pipe
, vp
->num_inputs
, vbuffer
);
333 pipe
->set_vertex_elements(pipe
, vp
->num_inputs
, velements
);
335 /* do actual drawing */
337 /* indexed primitive */
338 struct gl_buffer_object
*bufobj
= ib
->obj
;
339 struct pipe_buffer
*indexBuf
= NULL
;
340 unsigned indexSize
, indexOffset
, i
;
343 case GL_UNSIGNED_INT
:
346 case GL_UNSIGNED_SHORT
:
349 case GL_UNSIGNED_BYTE
:
357 /* get/create the index buffer object */
358 if (bufobj
&& bufobj
->Name
) {
359 /* elements/indexes are in a real VBO */
360 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
361 pipe_buffer_reference(winsys
, &indexBuf
, stobj
->buffer
);
362 indexOffset
= (unsigned) ib
->ptr
/ indexSize
;
365 /* element/indicies are in user space memory */
366 indexBuf
= winsys
->user_buffer_create(winsys
,
368 ib
->count
* indexSize
);
373 for (i
= 0; i
< nr_prims
; i
++) {
374 setup_edgeflags(ctx
, prims
[i
].mode
,
375 prims
[i
].start
+ indexOffset
, prims
[i
].count
,
376 arrays
[VERT_ATTRIB_EDGEFLAG
]);
378 pipe
->draw_elements(pipe
, indexBuf
, indexSize
,
380 prims
[i
].start
+ indexOffset
, prims
[i
].count
);
383 pipe_buffer_reference(winsys
, &indexBuf
, NULL
);
388 for (i
= 0; i
< nr_prims
; i
++) {
389 setup_edgeflags(ctx
, prims
[i
].mode
,
390 prims
[i
].start
, prims
[i
].count
,
391 arrays
[VERT_ATTRIB_EDGEFLAG
]);
393 pipe
->draw_arrays(pipe
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
397 /* unreference buffers (frees wrapped user-space buffer objects) */
398 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
399 pipe_buffer_reference(winsys
, &vbuffer
[attr
].buffer
, NULL
);
400 assert(!vbuffer
[attr
].buffer
);
402 pipe
->set_vertex_buffers(pipe
, vp
->num_inputs
, vbuffer
);
408 * Set the (private) draw module's post-transformed vertex format when in
409 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
412 set_feedback_vertex_format(GLcontext
*ctx
)
415 struct st_context
*st
= ctx
->st
;
416 struct vertex_info vinfo
;
419 memset(&vinfo
, 0, sizeof(vinfo
));
421 if (ctx
->RenderMode
== GL_SELECT
) {
422 assert(ctx
->RenderMode
== GL_SELECT
);
423 vinfo
.num_attribs
= 1;
424 vinfo
.format
[0] = FORMAT_4F
;
425 vinfo
.interp_mode
[0] = INTERP_LINEAR
;
428 /* GL_FEEDBACK, or glRasterPos */
429 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
430 vinfo
.num_attribs
= st
->state
.vs
->cso
->state
.num_outputs
;
431 for (i
= 0; i
< vinfo
.num_attribs
; i
++) {
432 vinfo
.format
[i
] = FORMAT_4F
;
433 vinfo
.interp_mode
[i
] = INTERP_LINEAR
;
437 draw_set_vertex_info(st
->draw
, &vinfo
);
443 * Called by VBO to draw arrays when in selection or feedback mode and
444 * to implement glRasterPos.
445 * This is very much like the normal draw_vbo() function above.
446 * Look at code refactoring some day.
447 * Might move this into the failover module some day.
450 st_feedback_draw_vbo(GLcontext
*ctx
,
451 const struct gl_client_array
**arrays
,
452 const struct _mesa_prim
*prims
,
454 const struct _mesa_index_buffer
*ib
,
458 struct st_context
*st
= ctx
->st
;
459 struct pipe_context
*pipe
= st
->pipe
;
460 struct draw_context
*draw
= st
->draw
;
461 struct pipe_winsys
*winsys
= pipe
->winsys
;
462 const struct st_vertex_program
*vp
;
463 const struct pipe_shader_state
*vs
;
464 struct pipe_buffer
*index_buffer_handle
= 0;
465 struct pipe_vertex_buffer vbuffers
[PIPE_MAX_SHADER_INPUTS
];
466 struct pipe_vertex_element velements
[PIPE_MAX_ATTRIBS
];
468 ubyte
*mapped_constants
;
472 st_validate_state(ctx
->st
);
474 /* must get these after state validation! */
478 if (!st
->vp
->draw_shader
) {
479 st
->vp
->draw_shader
= draw_create_vertex_shader(draw
, vs
);
483 * Set up the draw module's state.
485 * We'd like to do this less frequently, but the normal state-update
486 * code sends state updates to the pipe, not to our private draw module.
489 draw_set_viewport_state(draw
, &st
->state
.viewport
);
490 draw_set_clip_state(draw
, &st
->state
.clip
);
491 draw_set_rasterizer_state(draw
, &st
->state
.rasterizer
);
492 draw_bind_vertex_shader(draw
, st
->vp
->draw_shader
);
493 set_feedback_vertex_format(ctx
);
495 /* loop over TGSI shader inputs to determine vertex buffer
498 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
499 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
500 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
503 if (bufobj
&& bufobj
->Name
) {
504 /* Attribute data is in a VBO.
505 * Recall that for VBOs, the gl_client_array->Ptr field is
506 * really an offset from the start of the VBO, not a pointer.
508 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
509 assert(stobj
->buffer
);
511 vbuffers
[attr
].buffer
= NULL
;
512 pipe_buffer_reference(winsys
, &vbuffers
[attr
].buffer
, stobj
->buffer
);
513 vbuffers
[attr
].buffer_offset
= (unsigned) arrays
[0]->Ptr
;/* in bytes */
514 velements
[attr
].src_offset
= arrays
[mesaAttr
]->Ptr
- arrays
[0]->Ptr
;
517 /* attribute data is in user-space memory, not a VBO */
518 uint bytes
= (arrays
[mesaAttr
]->Size
519 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
)
523 vbuffers
[attr
].buffer
524 = winsys
->user_buffer_create(winsys
,
525 (void *) arrays
[mesaAttr
]->Ptr
,
527 vbuffers
[attr
].buffer_offset
= 0;
528 velements
[attr
].src_offset
= 0;
531 /* common-case setup */
532 vbuffers
[attr
].pitch
= arrays
[mesaAttr
]->StrideB
; /* in bytes */
533 vbuffers
[attr
].max_index
= max_index
;
534 velements
[attr
].vertex_buffer_index
= attr
;
535 velements
[attr
].nr_components
= arrays
[mesaAttr
]->Size
;
536 velements
[attr
].src_format
= pipe_vertex_format(arrays
[mesaAttr
]->Type
,
537 arrays
[mesaAttr
]->Size
,
538 arrays
[mesaAttr
]->Normalized
);
539 assert(velements
[attr
].src_format
);
541 /* tell draw about this attribute */
543 draw_set_vertex_buffer(draw
, attr
, &vbuffer
[attr
]);
546 /* map the attrib buffer */
547 map
= pipe
->winsys
->buffer_map(pipe
->winsys
,
548 vbuffers
[attr
].buffer
,
549 PIPE_BUFFER_USAGE_CPU_READ
);
550 draw_set_mapped_vertex_buffer(draw
, attr
, map
);
553 draw_set_vertex_buffers(draw
, vp
->num_inputs
, vbuffers
);
554 draw_set_vertex_elements(draw
, vp
->num_inputs
, velements
);
558 struct gl_buffer_object
*bufobj
= ib
->obj
;
559 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
560 index_buffer_handle
= stobj
->buffer
;
564 case GL_UNSIGNED_INT
:
567 case GL_UNSIGNED_SHORT
:
575 map
= pipe
->winsys
->buffer_map(pipe
->winsys
,
577 PIPE_BUFFER_USAGE_CPU_READ
);
578 draw_set_mapped_element_buffer(draw
, indexSize
, map
);
581 /* no index/element buffer */
582 draw_set_mapped_element_buffer(draw
, 0, NULL
);
586 /* map constant buffers */
587 mapped_constants
= winsys
->buffer_map(winsys
,
588 st
->state
.constants
[PIPE_SHADER_VERTEX
].buffer
,
589 PIPE_BUFFER_USAGE_CPU_READ
);
590 draw_set_mapped_constant_buffer(st
->draw
, mapped_constants
);
594 for (i
= 0; i
< nr_prims
; i
++) {
595 draw_arrays(draw
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
599 /* unmap constant buffers */
600 winsys
->buffer_unmap(winsys
, st
->state
.constants
[PIPE_SHADER_VERTEX
].buffer
);
603 * unmap vertex/index buffers
605 for (i
= 0; i
< PIPE_MAX_ATTRIBS
; i
++) {
606 if (draw
->pt
.vertex_buffer
[i
].buffer
) {
607 pipe
->winsys
->buffer_unmap(pipe
->winsys
,
608 draw
->pt
.vertex_buffer
[i
].buffer
);
609 pipe_buffer_reference(winsys
, &draw
->pt
.vertex_buffer
[i
].buffer
, NULL
);
610 draw_set_mapped_vertex_buffer(draw
, i
, NULL
);
614 pipe
->winsys
->buffer_unmap(pipe
->winsys
, index_buffer_handle
);
615 draw_set_mapped_element_buffer(draw
, 0, NULL
);
621 void st_init_draw( struct st_context
*st
)
623 GLcontext
*ctx
= st
->ctx
;
625 vbo_set_draw_func(ctx
, st_draw_vbo
);
629 void st_destroy_draw( struct st_context
*st
)