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
150 static GLuint fixed_types
[4] = {
151 PIPE_FORMAT_R32_FIXED
,
152 PIPE_FORMAT_R32G32_FIXED
,
153 PIPE_FORMAT_R32G32B32_FIXED
,
154 PIPE_FORMAT_R32G32B32A32_FIXED
160 * Return a PIPE_FORMAT_x for the given GL datatype and size.
163 pipe_vertex_format(GLenum type
, GLuint size
, GLboolean normalized
)
165 assert((type
>= GL_BYTE
&& type
<= GL_DOUBLE
) ||
172 case GL_DOUBLE
: return double_types
[size
-1];
173 case GL_FLOAT
: return float_types
[size
-1];
174 case GL_INT
: return int_types_norm
[size
-1];
175 case GL_SHORT
: return short_types_norm
[size
-1];
176 case GL_BYTE
: return byte_types_norm
[size
-1];
177 case GL_UNSIGNED_INT
: return uint_types_norm
[size
-1];
178 case GL_UNSIGNED_SHORT
: return ushort_types_norm
[size
-1];
179 case GL_UNSIGNED_BYTE
: return ubyte_types_norm
[size
-1];
180 case GL_FIXED
: return fixed_types
[size
-1];
181 default: assert(0); return 0;
186 case GL_DOUBLE
: return double_types
[size
-1];
187 case GL_FLOAT
: return float_types
[size
-1];
188 case GL_INT
: return int_types_scale
[size
-1];
189 case GL_SHORT
: return short_types_scale
[size
-1];
190 case GL_BYTE
: return byte_types_scale
[size
-1];
191 case GL_UNSIGNED_INT
: return uint_types_scale
[size
-1];
192 case GL_UNSIGNED_SHORT
: return ushort_types_scale
[size
-1];
193 case GL_UNSIGNED_BYTE
: return ubyte_types_scale
[size
-1];
194 case GL_FIXED
: return fixed_types
[size
-1];
195 default: assert(0); return 0;
198 return 0; /* silence compiler warning */
203 * If edge flags are needed, setup an bitvector of flags and call
204 * pipe->set_edgeflags().
205 * XXX memleak: need to free the returned pointer at some point
208 setup_edgeflags(GLcontext
*ctx
, GLenum primMode
, GLint start
, GLint count
,
209 const struct gl_client_array
*array
)
211 struct pipe_context
*pipe
= ctx
->st
->pipe
;
213 if ((primMode
== GL_TRIANGLES
||
214 primMode
== GL_QUADS
||
215 primMode
== GL_POLYGON
) &&
216 (ctx
->Polygon
.FrontMode
!= GL_FILL
||
217 ctx
->Polygon
.BackMode
!= GL_FILL
)) {
218 /* need edge flags */
221 struct st_buffer_object
*stobj
= st_buffer_object(array
->BufferObj
);
227 vec
= (unsigned *) calloc(sizeof(unsigned), (count
+ 31) / 32);
231 map
= pipe_buffer_map(pipe
, stobj
->buffer
, PIPE_BUFFER_USAGE_CPU_READ
);
232 map
= ADD_POINTERS(map
, array
->Ptr
);
234 for (i
= 0; i
< count
; i
++) {
235 if (*((float *) map
))
236 vec
[i
/32] |= 1 << (i
% 32);
238 map
+= array
->StrideB
;
241 pipe_buffer_unmap(pipe
, stobj
->buffer
);
243 pipe
->set_edgeflags(pipe
, vec
);
248 /* edge flags not needed */
249 pipe
->set_edgeflags(pipe
, NULL
);
257 * This function gets plugged into the VBO module and is called when
258 * we have something to render.
259 * Basically, translate the information into the format expected by pipe.
262 st_draw_vbo(GLcontext
*ctx
,
263 const struct gl_client_array
**arrays
,
264 const struct _mesa_prim
*prims
,
266 const struct _mesa_index_buffer
*ib
,
270 struct pipe_context
*pipe
= ctx
->st
->pipe
;
271 const struct st_vertex_program
*vp
;
272 const struct pipe_shader_state
*vs
;
273 struct pipe_vertex_buffer vbuffer
[PIPE_MAX_SHADER_INPUTS
];
275 struct pipe_vertex_element velements
[PIPE_MAX_ATTRIBS
];
277 /* sanity check for pointer arithmetic below */
278 assert(sizeof(arrays
[0]->Ptr
[0]) == 1);
280 st_validate_state(ctx
->st
);
282 /* must get these after state validation! */
284 vs
= &ctx
->st
->vp
->state
;
286 /* loop over TGSI shader inputs to determine vertex buffer
289 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
290 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
291 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
293 if (bufobj
&& bufobj
->Name
) {
294 /* Attribute data is in a VBO.
295 * Recall that for VBOs, the gl_client_array->Ptr field is
296 * really an offset from the start of the VBO, not a pointer.
298 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
299 assert(stobj
->buffer
);
301 vbuffer
[attr
].buffer
= NULL
;
302 pipe_reference_buffer(pipe
, &vbuffer
[attr
].buffer
, stobj
->buffer
);
303 vbuffer
[attr
].buffer_offset
= (unsigned) arrays
[0]->Ptr
;/* in bytes */
304 velements
[attr
].src_offset
= arrays
[mesaAttr
]->Ptr
- arrays
[0]->Ptr
;
305 assert(velements
[attr
].src_offset
<= 2048); /* 11-bit field */
308 /* attribute data is in user-space memory, not a VBO */
311 if (!arrays
[mesaAttr
]->StrideB
) {
312 bytes
= arrays
[mesaAttr
]->Size
313 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
);
315 bytes
= arrays
[mesaAttr
]->StrideB
* (max_index
+ 1);
320 = pipe_user_buffer_create(pipe
, (void *) arrays
[mesaAttr
]->Ptr
,
322 vbuffer
[attr
].buffer_offset
= 0;
323 velements
[attr
].src_offset
= 0;
326 /* common-case setup */
327 vbuffer
[attr
].pitch
= arrays
[mesaAttr
]->StrideB
; /* in bytes */
328 vbuffer
[attr
].max_index
= max_index
;
329 velements
[attr
].vertex_buffer_index
= attr
;
330 velements
[attr
].nr_components
= arrays
[mesaAttr
]->Size
;
331 velements
[attr
].src_format
332 = pipe_vertex_format(arrays
[mesaAttr
]->Type
,
333 arrays
[mesaAttr
]->Size
,
334 arrays
[mesaAttr
]->Normalized
);
335 assert(velements
[attr
].src_format
);
338 pipe
->set_vertex_buffers(pipe
, vp
->num_inputs
, vbuffer
);
339 pipe
->set_vertex_elements(pipe
, vp
->num_inputs
, velements
);
341 /* do actual drawing */
343 /* indexed primitive */
344 struct gl_buffer_object
*bufobj
= ib
->obj
;
345 struct pipe_buffer
*indexBuf
= NULL
;
346 unsigned indexSize
, indexOffset
, i
;
349 case GL_UNSIGNED_INT
:
352 case GL_UNSIGNED_SHORT
:
355 case GL_UNSIGNED_BYTE
:
363 /* get/create the index buffer object */
364 if (bufobj
&& bufobj
->Name
) {
365 /* elements/indexes are in a real VBO */
366 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
367 pipe_reference_buffer(pipe
, &indexBuf
, stobj
->buffer
);
368 indexOffset
= (unsigned) ib
->ptr
/ indexSize
;
371 /* element/indicies are in user space memory */
372 indexBuf
= pipe_user_buffer_create(pipe
, (void *) ib
->ptr
,
373 ib
->count
* indexSize
);
378 if (nr_prims
== 1 && pipe
->draw_range_elements
!= NULL
) {
381 /* XXX: exercise temporary path to pass min/max directly
382 * through to driver & draw module. These interfaces still
383 * need a bit of work...
385 setup_edgeflags(ctx
, prims
[i
].mode
,
386 prims
[i
].start
+ indexOffset
, prims
[i
].count
,
387 arrays
[VERT_ATTRIB_EDGEFLAG
]);
389 pipe
->draw_range_elements(pipe
, indexBuf
, indexSize
,
393 prims
[i
].start
+ indexOffset
, prims
[i
].count
);
396 for (i
= 0; i
< nr_prims
; i
++) {
397 setup_edgeflags(ctx
, prims
[i
].mode
,
398 prims
[i
].start
+ indexOffset
, prims
[i
].count
,
399 arrays
[VERT_ATTRIB_EDGEFLAG
]);
401 pipe
->draw_elements(pipe
, indexBuf
, indexSize
,
403 prims
[i
].start
+ indexOffset
, prims
[i
].count
);
407 pipe_reference_buffer(pipe
, &indexBuf
, NULL
);
412 for (i
= 0; i
< nr_prims
; i
++) {
413 setup_edgeflags(ctx
, prims
[i
].mode
,
414 prims
[i
].start
, prims
[i
].count
,
415 arrays
[VERT_ATTRIB_EDGEFLAG
]);
417 pipe
->draw_arrays(pipe
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
421 /* unreference buffers (frees wrapped user-space buffer objects) */
422 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
423 pipe_reference_buffer(pipe
, &vbuffer
[attr
].buffer
, NULL
);
424 assert(!vbuffer
[attr
].buffer
);
426 pipe
->set_vertex_buffers(pipe
, vp
->num_inputs
, vbuffer
);
432 * Set the (private) draw module's post-transformed vertex format when in
433 * GL_SELECT or GL_FEEDBACK mode or for glRasterPos.
436 set_feedback_vertex_format(GLcontext
*ctx
)
439 struct st_context
*st
= ctx
->st
;
440 struct vertex_info vinfo
;
443 memset(&vinfo
, 0, sizeof(vinfo
));
445 if (ctx
->RenderMode
== GL_SELECT
) {
446 assert(ctx
->RenderMode
== GL_SELECT
);
447 vinfo
.num_attribs
= 1;
448 vinfo
.format
[0] = FORMAT_4F
;
449 vinfo
.interp_mode
[0] = INTERP_LINEAR
;
452 /* GL_FEEDBACK, or glRasterPos */
453 /* emit all attribs (pos, color, texcoord) as GLfloat[4] */
454 vinfo
.num_attribs
= st
->state
.vs
->cso
->state
.num_outputs
;
455 for (i
= 0; i
< vinfo
.num_attribs
; i
++) {
456 vinfo
.format
[i
] = FORMAT_4F
;
457 vinfo
.interp_mode
[i
] = INTERP_LINEAR
;
461 draw_set_vertex_info(st
->draw
, &vinfo
);
467 * Called by VBO to draw arrays when in selection or feedback mode and
468 * to implement glRasterPos.
469 * This is very much like the normal draw_vbo() function above.
470 * Look at code refactoring some day.
471 * Might move this into the failover module some day.
474 st_feedback_draw_vbo(GLcontext
*ctx
,
475 const struct gl_client_array
**arrays
,
476 const struct _mesa_prim
*prims
,
478 const struct _mesa_index_buffer
*ib
,
482 struct st_context
*st
= ctx
->st
;
483 struct pipe_context
*pipe
= st
->pipe
;
484 struct draw_context
*draw
= st
->draw
;
485 const struct st_vertex_program
*vp
;
486 const struct pipe_shader_state
*vs
;
487 struct pipe_buffer
*index_buffer_handle
= 0;
488 struct pipe_vertex_buffer vbuffers
[PIPE_MAX_SHADER_INPUTS
];
489 struct pipe_vertex_element velements
[PIPE_MAX_ATTRIBS
];
491 ubyte
*mapped_constants
;
495 st_validate_state(ctx
->st
);
497 /* must get these after state validation! */
501 if (!st
->vp
->draw_shader
) {
502 st
->vp
->draw_shader
= draw_create_vertex_shader(draw
, vs
);
506 * Set up the draw module's state.
508 * We'd like to do this less frequently, but the normal state-update
509 * code sends state updates to the pipe, not to our private draw module.
512 draw_set_viewport_state(draw
, &st
->state
.viewport
);
513 draw_set_clip_state(draw
, &st
->state
.clip
);
514 draw_set_rasterizer_state(draw
, &st
->state
.rasterizer
);
515 draw_bind_vertex_shader(draw
, st
->vp
->draw_shader
);
516 set_feedback_vertex_format(ctx
);
518 /* loop over TGSI shader inputs to determine vertex buffer
521 for (attr
= 0; attr
< vp
->num_inputs
; attr
++) {
522 const GLuint mesaAttr
= vp
->index_to_input
[attr
];
523 struct gl_buffer_object
*bufobj
= arrays
[mesaAttr
]->BufferObj
;
526 if (bufobj
&& bufobj
->Name
) {
527 /* Attribute data is in a VBO.
528 * Recall that for VBOs, the gl_client_array->Ptr field is
529 * really an offset from the start of the VBO, not a pointer.
531 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
532 assert(stobj
->buffer
);
534 vbuffers
[attr
].buffer
= NULL
;
535 pipe_reference_buffer(pipe
, &vbuffers
[attr
].buffer
, stobj
->buffer
);
536 vbuffers
[attr
].buffer_offset
= (unsigned) arrays
[0]->Ptr
;/* in bytes */
537 velements
[attr
].src_offset
= arrays
[mesaAttr
]->Ptr
- arrays
[0]->Ptr
;
540 /* attribute data is in user-space memory, not a VBO */
541 uint bytes
= (arrays
[mesaAttr
]->Size
542 * _mesa_sizeof_type(arrays
[mesaAttr
]->Type
)
546 vbuffers
[attr
].buffer
547 = pipe_user_buffer_create(pipe
, (void *) arrays
[mesaAttr
]->Ptr
,
549 vbuffers
[attr
].buffer_offset
= 0;
550 velements
[attr
].src_offset
= 0;
553 /* common-case setup */
554 vbuffers
[attr
].pitch
= arrays
[mesaAttr
]->StrideB
; /* in bytes */
555 vbuffers
[attr
].max_index
= max_index
;
556 velements
[attr
].vertex_buffer_index
= attr
;
557 velements
[attr
].nr_components
= arrays
[mesaAttr
]->Size
;
558 velements
[attr
].src_format
= pipe_vertex_format(arrays
[mesaAttr
]->Type
,
559 arrays
[mesaAttr
]->Size
,
560 arrays
[mesaAttr
]->Normalized
);
561 assert(velements
[attr
].src_format
);
563 /* tell draw about this attribute */
565 draw_set_vertex_buffer(draw
, attr
, &vbuffer
[attr
]);
568 /* map the attrib buffer */
569 map
= pipe_buffer_map(pipe
, vbuffers
[attr
].buffer
,
570 PIPE_BUFFER_USAGE_CPU_READ
);
571 draw_set_mapped_vertex_buffer(draw
, attr
, map
);
574 draw_set_vertex_buffers(draw
, vp
->num_inputs
, vbuffers
);
575 draw_set_vertex_elements(draw
, vp
->num_inputs
, velements
);
579 struct gl_buffer_object
*bufobj
= ib
->obj
;
580 struct st_buffer_object
*stobj
= st_buffer_object(bufobj
);
583 index_buffer_handle
= stobj
->buffer
;
586 case GL_UNSIGNED_INT
:
589 case GL_UNSIGNED_SHORT
:
597 map
= pipe_buffer_map(pipe
, index_buffer_handle
,
598 PIPE_BUFFER_USAGE_CPU_READ
);
599 draw_set_mapped_element_buffer(draw
, indexSize
, map
);
602 /* no index/element buffer */
603 draw_set_mapped_element_buffer(draw
, 0, NULL
);
607 /* map constant buffers */
608 mapped_constants
= pipe_buffer_map(pipe
,
609 st
->state
.constants
[PIPE_SHADER_VERTEX
].buffer
,
610 PIPE_BUFFER_USAGE_CPU_READ
);
611 draw_set_mapped_constant_buffer(st
->draw
, mapped_constants
,
612 st
->state
.constants
[PIPE_SHADER_VERTEX
].buffer
->size
);
616 for (i
= 0; i
< nr_prims
; i
++) {
617 draw_arrays(draw
, prims
[i
].mode
, prims
[i
].start
, prims
[i
].count
);
621 /* unmap constant buffers */
622 pipe_buffer_unmap(pipe
, st
->state
.constants
[PIPE_SHADER_VERTEX
].buffer
);
625 * unmap vertex/index buffers
627 for (i
= 0; i
< PIPE_MAX_ATTRIBS
; i
++) {
628 if (draw
->pt
.vertex_buffer
[i
].buffer
) {
629 pipe_buffer_unmap(pipe
, draw
->pt
.vertex_buffer
[i
].buffer
);
630 pipe_reference_buffer(pipe
, &draw
->pt
.vertex_buffer
[i
].buffer
, NULL
);
631 draw_set_mapped_vertex_buffer(draw
, i
, NULL
);
635 pipe_buffer_unmap(pipe
, index_buffer_handle
);
636 draw_set_mapped_element_buffer(draw
, 0, NULL
);
642 void st_init_draw( struct st_context
*st
)
644 GLcontext
*ctx
= st
->ctx
;
646 vbo_set_draw_func(ctx
, st_draw_vbo
);
650 void st_destroy_draw( struct st_context
*st
)