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"
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"
47 static GLubyte
*get_space(struct gl_context
*ctx
, GLuint bytes
)
49 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
50 GLubyte
*space
= malloc(bytes
);
52 tnl
->block
[tnl
->nr_blocks
++] = space
;
57 static void free_space(struct gl_context
*ctx
)
59 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
61 for (GLuint 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 GLubyte
*ptr
, GLfloat
*fptr
,
122 GLuint count
, GLuint sz
)
126 for (i
= 0; i
< count
; i
++) {
127 GLhalfARB
*in
= (GLhalfARB
*)ptr
;
129 for (j
= 0; j
< sz
; j
++)
130 *fptr
++ = _mesa_half_to_float(in
[j
]);
132 ptr
+= binding
->Stride
;
137 * \brief Convert fixed-point to floating-point.
139 * In OpenGL, a fixed-point number is a "signed 2's complement 16.16 scaled
140 * integer" (Table 2.2 of the OpenGL ES 2.0 spec).
142 * If the buffer has the \c normalized flag set, the formula
143 * \code normalize(x) := (2*x + 1) / (2^16 - 1) \endcode
144 * is used to map the fixed-point numbers into the range [-1, 1].
147 convert_fixed_to_float(const struct gl_vertex_buffer_binding
*binding
,
148 const struct gl_array_attributes
*attrib
,
149 const GLubyte
*ptr
, GLfloat
*fptr
,
154 const GLint size
= attrib
->Format
.Size
;
156 if (attrib
->Format
.Normalized
) {
157 for (i
= 0; i
< count
; ++i
) {
158 const GLfixed
*in
= (GLfixed
*) ptr
;
159 for (j
= 0; j
< size
; ++j
) {
160 *fptr
++ = (GLfloat
) (2 * in
[j
] + 1) / (GLfloat
) ((1 << 16) - 1);
162 ptr
+= binding
->Stride
;
165 for (i
= 0; i
< count
; ++i
) {
166 const GLfixed
*in
= (GLfixed
*) ptr
;
167 for (j
= 0; j
< size
; ++j
) {
168 *fptr
++ = in
[j
] / (GLfloat
) (1 << 16);
170 ptr
+= binding
->Stride
;
175 /* Adjust pointer to point at first requested element, convert to
176 * floating point, populate VB->AttribPtr[].
178 static void _tnl_import_array(struct gl_context
*ctx
,
181 const struct gl_vertex_buffer_binding
*binding
,
182 const struct gl_array_attributes
*attrib
,
185 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
186 struct vertex_buffer
*VB
= &tnl
->vb
;
187 GLuint stride
= binding
->Stride
;
189 if (attrib
->Format
.Type
!= GL_FLOAT
) {
190 const GLuint sz
= attrib
->Format
.Size
;
191 GLubyte
*buf
= get_space(ctx
, count
* sz
* sizeof(GLfloat
));
192 GLfloat
*fptr
= (GLfloat
*)buf
;
194 switch (attrib
->Format
.Type
) {
196 CONVERT(GLbyte
, BYTE_TO_FLOAT
);
198 case GL_UNSIGNED_BYTE
:
199 if (attrib
->Format
.Format
== GL_BGRA
) {
200 /* See GL_EXT_vertex_array_bgra */
201 convert_bgra_to_float(binding
, attrib
, ptr
, fptr
, count
);
204 CONVERT(GLubyte
, UBYTE_TO_FLOAT
);
208 CONVERT(GLshort
, SHORT_TO_FLOAT
);
210 case GL_UNSIGNED_SHORT
:
211 CONVERT(GLushort
, USHORT_TO_FLOAT
);
214 CONVERT(GLint
, INT_TO_FLOAT
);
216 case GL_UNSIGNED_INT
:
217 CONVERT(GLuint
, UINT_TO_FLOAT
);
220 CONVERT(GLdouble
, (GLfloat
));
223 convert_half_to_float(binding
, ptr
, fptr
, count
, sz
);
226 convert_fixed_to_float(binding
, attrib
, ptr
, fptr
, count
);
229 unreachable("Invalid type.");
233 stride
= sz
* sizeof(GLfloat
);
236 VB
->AttribPtr
[attr
] = &tnl
->tmp_inputs
[attr
];
237 VB
->AttribPtr
[attr
]->data
= (GLfloat (*)[4])ptr
;
238 VB
->AttribPtr
[attr
]->start
= (GLfloat
*)ptr
;
239 VB
->AttribPtr
[attr
]->count
= count
;
240 VB
->AttribPtr
[attr
]->stride
= stride
;
241 VB
->AttribPtr
[attr
]->size
= attrib
->Format
.Size
;
243 /* This should die, but so should the whole GLvector4f concept:
245 VB
->AttribPtr
[attr
]->flags
= (((1<<attrib
->Format
.Size
)-1) |
247 (stride
== 4*sizeof(GLfloat
) ? 0 : VEC_BAD_STRIDE
));
249 VB
->AttribPtr
[attr
]->storage
= NULL
;
252 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
255 static GLboolean
*_tnl_import_edgeflag(struct gl_context
*ctx
,
256 const GLvector4f
*input
,
259 const GLubyte
*ptr
= (const GLubyte
*)input
->data
;
260 const GLuint stride
= input
->stride
;
261 GLboolean
*space
= (GLboolean
*)get_space(ctx
, count
+ CLIPVERTS
);
262 GLboolean
*bptr
= space
;
264 for (GLuint i
= 0; i
< count
; i
++) {
265 *bptr
++ = ((GLfloat
*)ptr
)[0] == 1.0F
;
273 static void bind_inputs(struct gl_context
*ctx
,
274 const struct tnl_vertex_array
*inputs
,
276 struct gl_buffer_object
**bo
,
279 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
280 struct vertex_buffer
*VB
= &tnl
->vb
;
284 for (unsigned i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
285 const struct tnl_vertex_array
*array
= &inputs
[i
];
286 const struct gl_vertex_buffer_binding
*binding
= array
->BufferBinding
;
287 const struct gl_array_attributes
*attrib
= array
->VertexAttrib
;
290 if (binding
->BufferObj
) {
291 if (!binding
->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
) {
292 bo
[*nr_bo
] = binding
->BufferObj
;
294 ctx
->Driver
.MapBufferRange(ctx
, 0, binding
->BufferObj
->Size
,
299 assert(binding
->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
);
302 ptr
= ADD_POINTERS(binding
->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
,
303 binding
->Offset
+ attrib
->RelativeOffset
);
307 /* Just make sure the array is floating point, otherwise convert to
310 * XXX: remove the GLvector4f type at some stage and just use
313 _tnl_import_array(ctx
, i
, count
, binding
, attrib
, ptr
);
316 /* We process only the vertices between min & max index:
320 /* These should perhaps be part of _TNL_ATTRIB_* */
321 VB
->BackfaceColorPtr
= NULL
;
322 VB
->BackfaceIndexPtr
= NULL
;
323 VB
->BackfaceSecondaryColorPtr
= NULL
;
325 /* Clipping and drawing code still requires this to be a packed
326 * array of ubytes which can be written into. TODO: Fix and
329 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
330 ctx
->Polygon
.BackMode
!= GL_FILL
) {
331 VB
->EdgeFlag
= _tnl_import_edgeflag(ctx
,
332 VB
->AttribPtr
[_TNL_ATTRIB_EDGEFLAG
],
335 /* the data previously pointed to by EdgeFlag may have been freed */
341 /* Translate indices to GLuints and store in VB->Elts.
343 static void bind_indices(struct gl_context
*ctx
,
344 const struct _mesa_index_buffer
*ib
,
345 struct gl_buffer_object
**bo
,
348 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
349 struct vertex_buffer
*VB
= &tnl
->vb
;
359 if (!_mesa_bufferobj_mapped(ib
->obj
, MAP_INTERNAL
)) {
360 /* if the buffer object isn't mapped yet, map it now */
361 bo
[*nr_bo
] = ib
->obj
;
363 ptr
= ctx
->Driver
.MapBufferRange(ctx
, (GLsizeiptr
) ib
->ptr
,
364 ib
->count
<< ib
->index_size_shift
,
365 GL_MAP_READ_BIT
, ib
->obj
,
367 assert(ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
);
369 /* user-space elements, or buffer already mapped */
370 ptr
= ADD_POINTERS(ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
, ib
->ptr
);
375 if (ib
->index_size_shift
== 2 && VB
->Primitive
[0].basevertex
== 0) {
376 VB
->Elts
= (GLuint
*) ptr
;
379 GLuint
*elts
= (GLuint
*)get_space(ctx
, ib
->count
* sizeof(GLuint
));
382 if (ib
->index_size_shift
== 2) {
383 const GLuint
*in
= (GLuint
*)ptr
;
384 for (i
= 0; i
< ib
->count
; i
++)
385 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
387 else if (ib
->index_size_shift
== 1) {
388 const GLushort
*in
= (GLushort
*)ptr
;
389 for (i
= 0; i
< ib
->count
; i
++)
390 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
393 const GLubyte
*in
= (GLubyte
*)ptr
;
394 for (i
= 0; i
< ib
->count
; i
++)
395 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
400 static void bind_prims(struct gl_context
*ctx
,
401 const struct _mesa_prim
*prim
,
404 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
405 struct vertex_buffer
*VB
= &tnl
->vb
;
407 VB
->Primitive
= prim
;
408 VB
->PrimitiveCount
= nr_prims
;
411 static void unmap_vbos(struct gl_context
*ctx
,
412 struct gl_buffer_object
**bo
,
415 for (GLuint i
= 0; i
< nr_bo
; i
++) {
416 ctx
->Driver
.UnmapBuffer(ctx
, bo
[i
], MAP_INTERNAL
);
421 /* This is the main workhorse doing all the rendering work.
423 void _tnl_draw_prims(struct gl_context
*ctx
,
424 const struct tnl_vertex_array
*arrays
,
425 const struct _mesa_prim
*prim
,
427 const struct _mesa_index_buffer
*ib
,
428 GLboolean index_bounds_valid
,
431 GLuint num_instances
,
432 GLuint base_instance
)
434 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
435 const GLuint TEST_SPLIT
= 0;
436 const GLint max
= TEST_SPLIT
? 8 : tnl
->vb
.Size
- MAX_CLIPPED_VERTICES
;
437 GLint max_basevertex
= prim
->basevertex
;
440 if (!index_bounds_valid
)
441 vbo_get_minmax_indices(ctx
, prim
, ib
, &min_index
, &max_index
, nr_prims
);
443 /* Mesa core state should have been validated already */
444 assert(ctx
->NewState
== 0x0);
446 if (!_mesa_check_conditional_render(ctx
))
447 return; /* don't draw */
449 for (i
= 1; i
< nr_prims
; i
++)
450 max_basevertex
= MAX2(max_basevertex
, prim
[i
].basevertex
);
453 printf("%s %d..%d\n", __func__
, min_index
, max_index
);
454 for (i
= 0; i
< nr_prims
; i
++)
455 printf("prim %d: %s start %d count %d\n", i
,
456 _mesa_enum_to_string(prim
[i
].mode
),
462 /* We always translate away calls with min_index != 0.
464 t_rebase_prims(ctx
, arrays
, prim
, nr_prims
, ib
,
465 min_index
, max_index
, num_instances
, base_instance
,
469 else if ((GLint
)max_index
+ max_basevertex
> max
) {
470 /* The software TNL pipeline has a fixed amount of storage for
471 * vertices and it is necessary to split incoming drawing commands
472 * if they exceed that limit.
474 struct split_limits limits
;
475 limits
.max_verts
= max
;
476 limits
.max_vb_size
= ~0;
477 limits
.max_indices
= ~0;
479 /* This will split the buffers one way or another and
480 * recursively call back into this function.
482 _tnl_split_prims(ctx
, arrays
, prim
, nr_prims
, ib
,
483 0, max_index
+ prim
->basevertex
,
484 num_instances
, base_instance
,
489 /* May need to map a vertex buffer object for every attribute plus
490 * one for the index buffer.
492 struct gl_buffer_object
*bo
[VERT_ATTRIB_MAX
+ 1];
496 assert(num_instances
> 0);
498 for (i
= 0; i
< nr_prims
;) {
499 GLuint this_nr_prims
;
501 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices
502 * will rebase the elements to the basevertex, and we'll only
503 * emit strings of prims with the same basevertex in one draw call.
505 for (this_nr_prims
= 1; i
+ this_nr_prims
< nr_prims
;
507 if (prim
[i
].basevertex
!= prim
[i
+ this_nr_prims
].basevertex
)
511 /* Binding inputs may imply mapping some vertex buffer objects.
512 * They will need to be unmapped below.
514 for (inst
= 0; inst
< num_instances
; inst
++) {
516 bind_prims(ctx
, &prim
[i
], this_nr_prims
);
517 bind_inputs(ctx
, arrays
, max_index
+ prim
[i
].basevertex
+ 1,
519 bind_indices(ctx
, ib
, bo
, &nr_bo
);
521 tnl
->CurInstance
= inst
;
522 TNL_CONTEXT(ctx
)->Driver
.RunPipeline(ctx
);
524 unmap_vbos(ctx
, bo
, nr_bo
);
535 _tnl_init_inputs(struct tnl_inputs
*inputs
)
538 inputs
->vertex_processing_mode
= VP_MODE_FF
;
543 * Update the tnl_inputs's arrays to point to the vao->_VertexArray arrays
544 * according to the 'enable' bitmask.
545 * \param enable bitfield of VERT_BIT_x flags.
548 update_vao_inputs(struct gl_context
*ctx
,
549 struct tnl_inputs
*inputs
, GLbitfield enable
)
551 const struct gl_vertex_array_object
*vao
= ctx
->Array
._DrawVAO
;
553 /* Make sure we process only arrays enabled in the VAO */
554 assert((enable
& ~_mesa_get_vao_vp_inputs(vao
)) == 0);
556 /* Fill in the client arrays from the VAO */
557 const struct gl_vertex_buffer_binding
*bindings
= &vao
->BufferBinding
[0];
559 const int attr
= u_bit_scan(&enable
);
560 struct tnl_vertex_array
*input
= &inputs
->inputs
[attr
];
561 const struct gl_array_attributes
*attrib
;
562 attrib
= _mesa_draw_array_attrib(vao
, attr
);
563 input
->VertexAttrib
= attrib
;
564 input
->BufferBinding
= &bindings
[attrib
->BufferBindingIndex
];
570 * Update the tnl_inputs's arrays to point to the vbo->currval arrays
571 * according to the 'current' bitmask.
572 * \param current bitfield of VERT_BIT_x flags.
575 update_current_inputs(struct gl_context
*ctx
,
576 struct tnl_inputs
*inputs
, GLbitfield current
)
578 gl_vertex_processing_mode mode
= ctx
->VertexProgram
._VPMode
;
580 /* All previously non current array pointers need update. */
581 GLbitfield mask
= current
& ~inputs
->current
;
582 /* On mode change, the slots aliasing with materials need update too */
583 if (mode
!= inputs
->vertex_processing_mode
)
584 mask
|= current
& VERT_BIT_MAT_ALL
;
587 const int attr
= u_bit_scan(&mask
);
588 struct tnl_vertex_array
*input
= &inputs
->inputs
[attr
];
589 input
->VertexAttrib
= _vbo_current_attrib(ctx
, attr
);
590 input
->BufferBinding
= _vbo_current_binding(ctx
);
593 inputs
->current
= current
;
594 inputs
->vertex_processing_mode
= mode
;
599 * Update the tnl_inputs's arrays to point to the vao->_VertexArray and
600 * vbo->currval arrays according to Array._DrawVAO and
601 * Array._DrawVAOEnableAttribs.
604 _tnl_update_inputs(struct gl_context
*ctx
, struct tnl_inputs
*inputs
)
606 const GLbitfield enable
= ctx
->Array
._DrawVAOEnabledAttribs
;
608 /* Update array input pointers */
609 update_vao_inputs(ctx
, inputs
, enable
);
611 /* The rest must be current inputs. */
612 update_current_inputs(ctx
, inputs
, ~enable
& VERT_BIT_ALL
);
616 const struct tnl_vertex_array
*
617 _tnl_bind_inputs(struct gl_context
*ctx
)
619 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
620 _tnl_update_inputs(ctx
, &tnl
->draw_arrays
);
621 return tnl
->draw_arrays
.inputs
;
625 /* This is the main entrypoint into the slimmed-down software tnl
626 * module. In a regular swtnl driver, this can be plugged straight
627 * into the ctx->Driver.Draw() callback.
630 _tnl_draw(struct gl_context
*ctx
,
631 const struct _mesa_prim
*prim
, GLuint nr_prims
,
632 const struct _mesa_index_buffer
*ib
,
633 GLboolean index_bounds_valid
, GLuint min_index
, GLuint max_index
,
634 GLuint num_instances
, GLuint base_instance
,
635 UNUSED
struct gl_transform_feedback_object
*tfb_vertcount
,
636 UNUSED
unsigned stream
)
638 /* Update TNLcontext::draw_arrays and return that pointer.
640 const struct tnl_vertex_array
* arrays
= _tnl_bind_inputs(ctx
);
642 _tnl_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
,
643 index_bounds_valid
, min_index
, max_index
,
644 num_instances
, base_instance
);
649 _tnl_init_driver_draw_function(struct dd_function_table
*functions
)
651 functions
->Draw
= _tnl_draw
;