2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keithw@vmware.com>
30 #include "main/glheader.h"
31 #include "main/arrayobj.h"
32 #include "main/bufferobj.h"
33 #include "main/condrender.h"
34 #include "main/context.h"
35 #include "util/imports.h"
36 #include "main/mtypes.h"
37 #include "main/macros.h"
38 #include "main/enums.h"
39 #include "main/varray.h"
40 #include "util/half_float.h"
42 #include "t_context.h"
48 static GLubyte
*get_space(struct gl_context
*ctx
, GLuint bytes
)
50 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
51 GLubyte
*space
= malloc(bytes
);
53 tnl
->block
[tnl
->nr_blocks
++] = space
;
58 static void free_space(struct gl_context
*ctx
)
60 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
62 for (i
= 0; i
< tnl
->nr_blocks
; i
++)
68 /* Convert the incoming array to GLfloats. Understands the
69 * array->Normalized flag and selects the correct conversion method.
71 #define CONVERT( TYPE, MACRO ) do { \
73 if (attrib->Format.Normalized) { \
74 for (i = 0; i < count; i++) { \
75 const TYPE *in = (TYPE *)ptr; \
76 for (j = 0; j < sz; j++) { \
77 *fptr++ = MACRO(*in); \
80 ptr += binding->Stride; \
83 for (i = 0; i < count; i++) { \
84 const TYPE *in = (TYPE *)ptr; \
85 for (j = 0; j < sz; j++) { \
86 *fptr++ = (GLfloat)(*in); \
89 ptr += binding->Stride; \
96 * Convert array of BGRA/GLubyte[4] values to RGBA/float[4]
97 * \param ptr input/ubyte array
98 * \param fptr output/float array
101 convert_bgra_to_float(const struct gl_vertex_buffer_binding
*binding
,
102 const struct gl_array_attributes
*attrib
,
103 const GLubyte
*ptr
, GLfloat
*fptr
,
107 assert(attrib
->Format
.Normalized
);
108 assert(attrib
->Format
.Size
== 4);
109 for (i
= 0; i
< count
; i
++) {
110 const GLubyte
*in
= (GLubyte
*) ptr
; /* in is in BGRA order */
111 *fptr
++ = UBYTE_TO_FLOAT(in
[2]); /* red */
112 *fptr
++ = UBYTE_TO_FLOAT(in
[1]); /* green */
113 *fptr
++ = UBYTE_TO_FLOAT(in
[0]); /* blue */
114 *fptr
++ = UBYTE_TO_FLOAT(in
[3]); /* alpha */
115 ptr
+= binding
->Stride
;
120 convert_half_to_float(const struct gl_vertex_buffer_binding
*binding
,
121 const struct gl_array_attributes
*attrib
,
122 const GLubyte
*ptr
, GLfloat
*fptr
,
123 GLuint count
, GLuint sz
)
127 for (i
= 0; i
< count
; i
++) {
128 GLhalfARB
*in
= (GLhalfARB
*)ptr
;
130 for (j
= 0; j
< sz
; j
++) {
131 *fptr
++ = _mesa_half_to_float(in
[j
]);
133 ptr
+= binding
->Stride
;
138 * \brief Convert fixed-point to floating-point.
140 * In OpenGL, a fixed-point number is a "signed 2's complement 16.16 scaled
141 * integer" (Table 2.2 of the OpenGL ES 2.0 spec).
143 * If the buffer has the \c normalized flag set, the formula
144 * \code normalize(x) := (2*x + 1) / (2^16 - 1) \endcode
145 * is used to map the fixed-point numbers into the range [-1, 1].
148 convert_fixed_to_float(const struct gl_vertex_buffer_binding
*binding
,
149 const struct gl_array_attributes
*attrib
,
150 const GLubyte
*ptr
, GLfloat
*fptr
,
155 const GLint size
= attrib
->Format
.Size
;
157 if (attrib
->Format
.Normalized
) {
158 for (i
= 0; i
< count
; ++i
) {
159 const GLfixed
*in
= (GLfixed
*) ptr
;
160 for (j
= 0; j
< size
; ++j
) {
161 *fptr
++ = (GLfloat
) (2 * in
[j
] + 1) / (GLfloat
) ((1 << 16) - 1);
163 ptr
+= binding
->Stride
;
166 for (i
= 0; i
< count
; ++i
) {
167 const GLfixed
*in
= (GLfixed
*) ptr
;
168 for (j
= 0; j
< size
; ++j
) {
169 *fptr
++ = in
[j
] / (GLfloat
) (1 << 16);
171 ptr
+= binding
->Stride
;
176 /* Adjust pointer to point at first requested element, convert to
177 * floating point, populate VB->AttribPtr[].
179 static void _tnl_import_array( struct gl_context
*ctx
,
182 const struct gl_vertex_buffer_binding
*binding
,
183 const struct gl_array_attributes
*attrib
,
186 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
187 struct vertex_buffer
*VB
= &tnl
->vb
;
188 GLuint stride
= binding
->Stride
;
190 if (attrib
->Format
.Type
!= GL_FLOAT
) {
191 const GLuint sz
= attrib
->Format
.Size
;
192 GLubyte
*buf
= get_space(ctx
, count
* sz
* sizeof(GLfloat
));
193 GLfloat
*fptr
= (GLfloat
*)buf
;
195 switch (attrib
->Format
.Type
) {
197 CONVERT(GLbyte
, BYTE_TO_FLOAT
);
199 case GL_UNSIGNED_BYTE
:
200 if (attrib
->Format
.Format
== GL_BGRA
) {
201 /* See GL_EXT_vertex_array_bgra */
202 convert_bgra_to_float(binding
, attrib
, ptr
, fptr
, count
);
205 CONVERT(GLubyte
, UBYTE_TO_FLOAT
);
209 CONVERT(GLshort
, SHORT_TO_FLOAT
);
211 case GL_UNSIGNED_SHORT
:
212 CONVERT(GLushort
, USHORT_TO_FLOAT
);
215 CONVERT(GLint
, INT_TO_FLOAT
);
217 case GL_UNSIGNED_INT
:
218 CONVERT(GLuint
, UINT_TO_FLOAT
);
221 CONVERT(GLdouble
, (GLfloat
));
224 convert_half_to_float(binding
, attrib
, ptr
, fptr
, count
, sz
);
227 convert_fixed_to_float(binding
, attrib
, ptr
, fptr
, count
);
235 stride
= sz
* sizeof(GLfloat
);
238 VB
->AttribPtr
[attr
] = &tnl
->tmp_inputs
[attr
];
239 VB
->AttribPtr
[attr
]->data
= (GLfloat (*)[4])ptr
;
240 VB
->AttribPtr
[attr
]->start
= (GLfloat
*)ptr
;
241 VB
->AttribPtr
[attr
]->count
= count
;
242 VB
->AttribPtr
[attr
]->stride
= stride
;
243 VB
->AttribPtr
[attr
]->size
= attrib
->Format
.Size
;
245 /* This should die, but so should the whole GLvector4f concept:
247 VB
->AttribPtr
[attr
]->flags
= (((1<<attrib
->Format
.Size
)-1) |
249 (stride
== 4*sizeof(GLfloat
) ? 0 : VEC_BAD_STRIDE
));
251 VB
->AttribPtr
[attr
]->storage
= NULL
;
254 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
257 static GLboolean
*_tnl_import_edgeflag( struct gl_context
*ctx
,
258 const GLvector4f
*input
,
261 const GLubyte
*ptr
= (const GLubyte
*)input
->data
;
262 const GLuint stride
= input
->stride
;
263 GLboolean
*space
= (GLboolean
*)get_space(ctx
, count
+ CLIPVERTS
);
264 GLboolean
*bptr
= space
;
267 for (i
= 0; i
< count
; i
++) {
268 *bptr
++ = ((GLfloat
*)ptr
)[0] == 1.0F
;
276 static void bind_inputs( struct gl_context
*ctx
,
277 const struct tnl_vertex_array
*inputs
,
279 struct gl_buffer_object
**bo
,
282 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
283 struct vertex_buffer
*VB
= &tnl
->vb
;
288 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
289 const struct tnl_vertex_array
*array
= &inputs
[i
];
290 const struct gl_vertex_buffer_binding
*binding
= array
->BufferBinding
;
291 const struct gl_array_attributes
*attrib
= array
->VertexAttrib
;
294 if (_mesa_is_bufferobj(binding
->BufferObj
)) {
295 if (!binding
->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
) {
296 bo
[*nr_bo
] = binding
->BufferObj
;
298 ctx
->Driver
.MapBufferRange(ctx
, 0, binding
->BufferObj
->Size
,
303 assert(binding
->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
);
306 ptr
= ADD_POINTERS(binding
->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
,
307 binding
->Offset
+ attrib
->RelativeOffset
);
312 /* Just make sure the array is floating point, otherwise convert to
315 * XXX: remove the GLvector4f type at some stage and just use
318 _tnl_import_array(ctx
, i
, count
, binding
, attrib
, ptr
);
321 /* We process only the vertices between min & max index:
325 /* These should perhaps be part of _TNL_ATTRIB_* */
326 VB
->BackfaceColorPtr
= NULL
;
327 VB
->BackfaceIndexPtr
= NULL
;
328 VB
->BackfaceSecondaryColorPtr
= NULL
;
330 /* Clipping and drawing code still requires this to be a packed
331 * array of ubytes which can be written into. TODO: Fix and
334 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
335 ctx
->Polygon
.BackMode
!= GL_FILL
)
337 VB
->EdgeFlag
= _tnl_import_edgeflag( ctx
,
338 VB
->AttribPtr
[_TNL_ATTRIB_EDGEFLAG
],
342 /* the data previously pointed to by EdgeFlag may have been freed */
348 /* Translate indices to GLuints and store in VB->Elts.
350 static void bind_indices( struct gl_context
*ctx
,
351 const struct _mesa_index_buffer
*ib
,
352 struct gl_buffer_object
**bo
,
355 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
356 struct vertex_buffer
*VB
= &tnl
->vb
;
365 if (_mesa_is_bufferobj(ib
->obj
) &&
366 !_mesa_bufferobj_mapped(ib
->obj
, MAP_INTERNAL
)) {
367 /* if the buffer object isn't mapped yet, map it now */
368 bo
[*nr_bo
] = ib
->obj
;
370 ptr
= ctx
->Driver
.MapBufferRange(ctx
, (GLsizeiptr
) ib
->ptr
,
371 ib
->count
<< ib
->index_size_shift
,
372 GL_MAP_READ_BIT
, ib
->obj
,
374 assert(ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
);
376 /* user-space elements, or buffer already mapped */
377 ptr
= ADD_POINTERS(ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
, ib
->ptr
);
380 if (ib
->index_size_shift
== 2 && VB
->Primitive
[0].basevertex
== 0) {
381 VB
->Elts
= (GLuint
*) ptr
;
384 GLuint
*elts
= (GLuint
*)get_space(ctx
, ib
->count
* sizeof(GLuint
));
387 if (ib
->index_size_shift
== 2) {
388 const GLuint
*in
= (GLuint
*)ptr
;
389 for (i
= 0; i
< ib
->count
; i
++)
390 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
392 else if (ib
->index_size_shift
== 1) {
393 const GLushort
*in
= (GLushort
*)ptr
;
394 for (i
= 0; i
< ib
->count
; i
++)
395 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
398 const GLubyte
*in
= (GLubyte
*)ptr
;
399 for (i
= 0; i
< ib
->count
; i
++)
400 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
405 static void bind_prims( struct gl_context
*ctx
,
406 const struct _mesa_prim
*prim
,
409 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
410 struct vertex_buffer
*VB
= &tnl
->vb
;
412 VB
->Primitive
= prim
;
413 VB
->PrimitiveCount
= nr_prims
;
416 static void unmap_vbos( struct gl_context
*ctx
,
417 struct gl_buffer_object
**bo
,
421 for (i
= 0; i
< nr_bo
; i
++) {
422 ctx
->Driver
.UnmapBuffer(ctx
, bo
[i
], MAP_INTERNAL
);
427 /* This is the main workhorse doing all the rendering work.
429 void _tnl_draw_prims(struct gl_context
*ctx
,
430 const struct tnl_vertex_array
*arrays
,
431 const struct _mesa_prim
*prim
,
433 const struct _mesa_index_buffer
*ib
,
434 GLboolean index_bounds_valid
,
437 GLuint num_instances
,
438 GLuint base_instance
,
439 struct gl_transform_feedback_object
*tfb_vertcount
,
442 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
443 const GLuint TEST_SPLIT
= 0;
444 const GLint max
= TEST_SPLIT
? 8 : tnl
->vb
.Size
- MAX_CLIPPED_VERTICES
;
445 GLint max_basevertex
= prim
->basevertex
;
448 if (!index_bounds_valid
)
449 vbo_get_minmax_indices(ctx
, prim
, ib
, &min_index
, &max_index
, nr_prims
);
451 /* Mesa core state should have been validated already */
452 assert(ctx
->NewState
== 0x0);
454 if (!_mesa_check_conditional_render(ctx
))
455 return; /* don't draw */
457 for (i
= 1; i
< nr_prims
; i
++)
458 max_basevertex
= MAX2(max_basevertex
, prim
[i
].basevertex
);
462 printf("%s %d..%d\n", __func__
, min_index
, max_index
);
463 for (i
= 0; i
< nr_prims
; i
++)
464 printf("prim %d: %s start %d count %d\n", i
,
465 _mesa_enum_to_string(prim
[i
].mode
),
471 /* We always translate away calls with min_index != 0.
473 t_rebase_prims( ctx
, arrays
, prim
, nr_prims
, ib
,
474 min_index
, max_index
, num_instances
, base_instance
,
478 else if ((GLint
)max_index
+ max_basevertex
> max
) {
479 /* The software TNL pipeline has a fixed amount of storage for
480 * vertices and it is necessary to split incoming drawing commands
481 * if they exceed that limit.
483 struct split_limits limits
;
484 limits
.max_verts
= max
;
485 limits
.max_vb_size
= ~0;
486 limits
.max_indices
= ~0;
488 /* This will split the buffers one way or another and
489 * recursively call back into this function.
491 _tnl_split_prims( ctx
, arrays
, prim
, nr_prims
, ib
,
492 0, max_index
+ prim
->basevertex
,
493 num_instances
, base_instance
,
498 /* May need to map a vertex buffer object for every attribute plus
499 * one for the index buffer.
501 struct gl_buffer_object
*bo
[VERT_ATTRIB_MAX
+ 1];
505 assert(num_instances
> 0);
507 for (i
= 0; i
< nr_prims
;) {
508 GLuint this_nr_prims
;
510 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices
511 * will rebase the elements to the basevertex, and we'll only
512 * emit strings of prims with the same basevertex in one draw call.
514 for (this_nr_prims
= 1; i
+ this_nr_prims
< nr_prims
;
516 if (prim
[i
].basevertex
!= prim
[i
+ this_nr_prims
].basevertex
)
520 /* Binding inputs may imply mapping some vertex buffer objects.
521 * They will need to be unmapped below.
523 for (inst
= 0; inst
< num_instances
; inst
++) {
525 bind_prims(ctx
, &prim
[i
], this_nr_prims
);
526 bind_inputs(ctx
, arrays
, max_index
+ prim
[i
].basevertex
+ 1,
528 bind_indices(ctx
, ib
, bo
, &nr_bo
);
530 tnl
->CurInstance
= inst
;
531 TNL_CONTEXT(ctx
)->Driver
.RunPipeline(ctx
);
533 unmap_vbos(ctx
, bo
, nr_bo
);
544 _tnl_init_inputs(struct tnl_inputs
*inputs
)
547 inputs
->vertex_processing_mode
= VP_MODE_FF
;
552 * Update the tnl_inputs's arrays to point to the vao->_VertexArray arrays
553 * according to the 'enable' bitmask.
554 * \param enable bitfield of VERT_BIT_x flags.
557 update_vao_inputs(struct gl_context
*ctx
,
558 struct tnl_inputs
*inputs
, GLbitfield enable
)
560 const struct gl_vertex_array_object
*vao
= ctx
->Array
._DrawVAO
;
562 /* Make sure we process only arrays enabled in the VAO */
563 assert((enable
& ~_mesa_get_vao_vp_inputs(vao
)) == 0);
565 /* Fill in the client arrays from the VAO */
566 const struct gl_vertex_buffer_binding
*bindings
= &vao
->BufferBinding
[0];
568 const int attr
= u_bit_scan(&enable
);
569 struct tnl_vertex_array
*input
= &inputs
->inputs
[attr
];
570 const struct gl_array_attributes
*attrib
;
571 attrib
= _mesa_draw_array_attrib(vao
, attr
);
572 input
->VertexAttrib
= attrib
;
573 input
->BufferBinding
= &bindings
[attrib
->BufferBindingIndex
];
579 * Update the tnl_inputs's arrays to point to the vbo->currval arrays
580 * according to the 'current' bitmask.
581 * \param current bitfield of VERT_BIT_x flags.
584 update_current_inputs(struct gl_context
*ctx
,
585 struct tnl_inputs
*inputs
, GLbitfield current
)
587 gl_vertex_processing_mode mode
= ctx
->VertexProgram
._VPMode
;
589 /* All previously non current array pointers need update. */
590 GLbitfield mask
= current
& ~inputs
->current
;
591 /* On mode change, the slots aliasing with materials need update too */
592 if (mode
!= inputs
->vertex_processing_mode
)
593 mask
|= current
& VERT_BIT_MAT_ALL
;
596 const int attr
= u_bit_scan(&mask
);
597 struct tnl_vertex_array
*input
= &inputs
->inputs
[attr
];
598 input
->VertexAttrib
= _vbo_current_attrib(ctx
, attr
);
599 input
->BufferBinding
= _vbo_current_binding(ctx
);
602 inputs
->current
= current
;
603 inputs
->vertex_processing_mode
= mode
;
608 * Update the tnl_inputs's arrays to point to the vao->_VertexArray and
609 * vbo->currval arrays according to Array._DrawVAO and
610 * Array._DrawVAOEnableAttribs.
613 _tnl_update_inputs(struct gl_context
*ctx
, struct tnl_inputs
*inputs
)
615 const GLbitfield enable
= ctx
->Array
._DrawVAOEnabledAttribs
;
617 /* Update array input pointers */
618 update_vao_inputs(ctx
, inputs
, enable
);
620 /* The rest must be current inputs. */
621 update_current_inputs(ctx
, inputs
, ~enable
& VERT_BIT_ALL
);
625 const struct tnl_vertex_array
*
626 _tnl_bind_inputs( struct gl_context
*ctx
)
628 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
629 _tnl_update_inputs(ctx
, &tnl
->draw_arrays
);
630 return tnl
->draw_arrays
.inputs
;
634 /* This is the main entrypoint into the slimmed-down software tnl
635 * module. In a regular swtnl driver, this can be plugged straight
636 * into the ctx->Driver.Draw() callback.
639 _tnl_draw(struct gl_context
*ctx
,
640 const struct _mesa_prim
*prim
, GLuint nr_prims
,
641 const struct _mesa_index_buffer
*ib
,
642 GLboolean index_bounds_valid
, GLuint min_index
, GLuint max_index
,
643 GLuint num_instances
, GLuint base_instance
,
644 struct gl_transform_feedback_object
*tfb_vertcount
,
647 /* Update TNLcontext::draw_arrays and return that pointer.
649 const struct tnl_vertex_array
* arrays
= _tnl_bind_inputs(ctx
);
651 _tnl_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
,
652 index_bounds_valid
, min_index
, max_index
,
653 num_instances
, base_instance
, tfb_vertcount
, stream
);
658 _tnl_init_driver_draw_function(struct dd_function_table
*functions
)
660 functions
->Draw
= _tnl_draw
;