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/bufferobj.h"
32 #include "main/condrender.h"
33 #include "main/context.h"
34 #include "main/imports.h"
35 #include "main/mtypes.h"
36 #include "main/macros.h"
37 #include "main/enums.h"
38 #include "main/varray.h"
39 #include "util/half_float.h"
41 #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 (i
= 0; i
< tnl
->nr_blocks
; i
++)
67 /* Convert the incoming array to GLfloats. Understands the
68 * array->Normalized flag and selects the correct conversion method.
70 #define CONVERT( TYPE, MACRO ) do { \
72 if (attrib->Normalized) { \
73 for (i = 0; i < count; i++) { \
74 const TYPE *in = (TYPE *)ptr; \
75 for (j = 0; j < sz; j++) { \
76 *fptr++ = MACRO(*in); \
79 ptr += binding->Stride; \
82 for (i = 0; i < count; i++) { \
83 const TYPE *in = (TYPE *)ptr; \
84 for (j = 0; j < sz; j++) { \
85 *fptr++ = (GLfloat)(*in); \
88 ptr += binding->Stride; \
95 * Convert array of BGRA/GLubyte[4] values to RGBA/float[4]
96 * \param ptr input/ubyte array
97 * \param fptr output/float array
100 convert_bgra_to_float(const struct gl_vertex_buffer_binding
*binding
,
101 const struct gl_array_attributes
*attrib
,
102 const GLubyte
*ptr
, GLfloat
*fptr
,
106 assert(attrib
->Normalized
);
107 assert(attrib
->Size
== 4);
108 for (i
= 0; i
< count
; i
++) {
109 const GLubyte
*in
= (GLubyte
*) ptr
; /* in is in BGRA order */
110 *fptr
++ = UBYTE_TO_FLOAT(in
[2]); /* red */
111 *fptr
++ = UBYTE_TO_FLOAT(in
[1]); /* green */
112 *fptr
++ = UBYTE_TO_FLOAT(in
[0]); /* blue */
113 *fptr
++ = UBYTE_TO_FLOAT(in
[3]); /* alpha */
114 ptr
+= binding
->Stride
;
119 convert_half_to_float(const struct gl_vertex_buffer_binding
*binding
,
120 const struct gl_array_attributes
*attrib
,
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
->Size
;
156 if (attrib
->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
->Type
!= GL_FLOAT
) {
190 const GLuint sz
= attrib
->Size
;
191 GLubyte
*buf
= get_space(ctx
, count
* sz
* sizeof(GLfloat
));
192 GLfloat
*fptr
= (GLfloat
*)buf
;
194 switch (attrib
->Type
) {
196 CONVERT(GLbyte
, BYTE_TO_FLOAT
);
198 case GL_UNSIGNED_BYTE
:
199 if (attrib
->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
, attrib
, ptr
, fptr
, count
, sz
);
226 convert_fixed_to_float(binding
, attrib
, ptr
, fptr
, count
);
234 stride
= sz
* sizeof(GLfloat
);
237 VB
->AttribPtr
[attr
] = &tnl
->tmp_inputs
[attr
];
238 VB
->AttribPtr
[attr
]->data
= (GLfloat (*)[4])ptr
;
239 VB
->AttribPtr
[attr
]->start
= (GLfloat
*)ptr
;
240 VB
->AttribPtr
[attr
]->count
= count
;
241 VB
->AttribPtr
[attr
]->stride
= stride
;
242 VB
->AttribPtr
[attr
]->size
= attrib
->Size
;
244 /* This should die, but so should the whole GLvector4f concept:
246 VB
->AttribPtr
[attr
]->flags
= (((1<<attrib
->Size
)-1) |
248 (stride
== 4*sizeof(GLfloat
) ? 0 : VEC_BAD_STRIDE
));
250 VB
->AttribPtr
[attr
]->storage
= NULL
;
253 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
256 static GLboolean
*_tnl_import_edgeflag( struct gl_context
*ctx
,
257 const GLvector4f
*input
,
260 const GLubyte
*ptr
= (const GLubyte
*)input
->data
;
261 const GLuint stride
= input
->stride
;
262 GLboolean
*space
= (GLboolean
*)get_space(ctx
, count
+ CLIPVERTS
);
263 GLboolean
*bptr
= space
;
266 for (i
= 0; i
< count
; i
++) {
267 *bptr
++ = ((GLfloat
*)ptr
)[0] == 1.0F
;
275 static void bind_inputs( struct gl_context
*ctx
,
276 const struct gl_vertex_array
*inputs
,
278 struct gl_buffer_object
**bo
,
281 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
282 struct vertex_buffer
*VB
= &tnl
->vb
;
287 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
288 const struct gl_vertex_array
*array
= &inputs
[i
];
289 const struct gl_vertex_buffer_binding
*binding
= array
->BufferBinding
;
290 const struct gl_array_attributes
*attrib
= array
->VertexAttrib
;
293 if (_mesa_is_bufferobj(binding
->BufferObj
)) {
294 if (!binding
->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
) {
295 bo
[*nr_bo
] = binding
->BufferObj
;
297 ctx
->Driver
.MapBufferRange(ctx
, 0, binding
->BufferObj
->Size
,
302 assert(binding
->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
);
305 ptr
= ADD_POINTERS(binding
->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
,
306 binding
->Offset
+ attrib
->RelativeOffset
);
311 /* Just make sure the array is floating point, otherwise convert to
314 * XXX: remove the GLvector4f type at some stage and just use
317 _tnl_import_array(ctx
, i
, count
, binding
, attrib
, ptr
);
320 /* We process only the vertices between min & max index:
324 /* These should perhaps be part of _TNL_ATTRIB_* */
325 VB
->BackfaceColorPtr
= NULL
;
326 VB
->BackfaceIndexPtr
= NULL
;
327 VB
->BackfaceSecondaryColorPtr
= NULL
;
329 /* Clipping and drawing code still requires this to be a packed
330 * array of ubytes which can be written into. TODO: Fix and
333 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
334 ctx
->Polygon
.BackMode
!= GL_FILL
)
336 VB
->EdgeFlag
= _tnl_import_edgeflag( ctx
,
337 VB
->AttribPtr
[_TNL_ATTRIB_EDGEFLAG
],
341 /* the data previously pointed to by EdgeFlag may have been freed */
347 /* Translate indices to GLuints and store in VB->Elts.
349 static void bind_indices( struct gl_context
*ctx
,
350 const struct _mesa_index_buffer
*ib
,
351 struct gl_buffer_object
**bo
,
354 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
355 struct vertex_buffer
*VB
= &tnl
->vb
;
364 if (_mesa_is_bufferobj(ib
->obj
) &&
365 !_mesa_bufferobj_mapped(ib
->obj
, MAP_INTERNAL
)) {
366 /* if the buffer object isn't mapped yet, map it now */
367 bo
[*nr_bo
] = ib
->obj
;
369 ptr
= ctx
->Driver
.MapBufferRange(ctx
, (GLsizeiptr
) ib
->ptr
,
370 ib
->count
* ib
->index_size
,
371 GL_MAP_READ_BIT
, ib
->obj
,
373 assert(ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
);
375 /* user-space elements, or buffer already mapped */
376 ptr
= ADD_POINTERS(ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
, ib
->ptr
);
379 if (ib
->index_size
== 4 && VB
->Primitive
[0].basevertex
== 0) {
380 VB
->Elts
= (GLuint
*) ptr
;
383 GLuint
*elts
= (GLuint
*)get_space(ctx
, ib
->count
* sizeof(GLuint
));
386 if (ib
->index_size
== 4) {
387 const GLuint
*in
= (GLuint
*)ptr
;
388 for (i
= 0; i
< ib
->count
; i
++)
389 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
391 else if (ib
->index_size
== 2) {
392 const GLushort
*in
= (GLushort
*)ptr
;
393 for (i
= 0; i
< ib
->count
; i
++)
394 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
397 const GLubyte
*in
= (GLubyte
*)ptr
;
398 for (i
= 0; i
< ib
->count
; i
++)
399 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
404 static void bind_prims( struct gl_context
*ctx
,
405 const struct _mesa_prim
*prim
,
408 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
409 struct vertex_buffer
*VB
= &tnl
->vb
;
411 VB
->Primitive
= prim
;
412 VB
->PrimitiveCount
= nr_prims
;
415 static void unmap_vbos( struct gl_context
*ctx
,
416 struct gl_buffer_object
**bo
,
420 for (i
= 0; i
< nr_bo
; i
++) {
421 ctx
->Driver
.UnmapBuffer(ctx
, bo
[i
], MAP_INTERNAL
);
426 /* This is the main workhorse doing all the rendering work.
428 void _tnl_draw_prims(struct gl_context
*ctx
,
429 const struct _mesa_prim
*prim
,
431 const struct _mesa_index_buffer
*ib
,
432 GLboolean index_bounds_valid
,
435 struct gl_transform_feedback_object
*tfb_vertcount
,
437 struct gl_buffer_object
*indirect
)
439 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
440 const struct gl_vertex_array
*arrays
= ctx
->Array
._DrawArrays
;
441 const GLuint TEST_SPLIT
= 0;
442 const GLint max
= TEST_SPLIT
? 8 : tnl
->vb
.Size
- MAX_CLIPPED_VERTICES
;
443 GLint max_basevertex
= prim
->basevertex
;
446 if (!index_bounds_valid
)
447 vbo_get_minmax_indices(ctx
, prim
, ib
, &min_index
, &max_index
, nr_prims
);
449 /* Mesa core state should have been validated already */
450 assert(ctx
->NewState
== 0x0);
452 if (!_mesa_check_conditional_render(ctx
))
453 return; /* don't draw */
455 for (i
= 1; i
< nr_prims
; i
++)
456 max_basevertex
= MAX2(max_basevertex
, prim
[i
].basevertex
);
460 printf("%s %d..%d\n", __func__
, min_index
, max_index
);
461 for (i
= 0; i
< nr_prims
; i
++)
462 printf("prim %d: %s start %d count %d\n", i
,
463 _mesa_enum_to_string(prim
[i
].mode
),
469 /* We always translate away calls with min_index != 0.
471 t_rebase_prims( ctx
, arrays
, prim
, nr_prims
, ib
,
472 min_index
, max_index
,
476 else if ((GLint
)max_index
+ max_basevertex
> max
) {
477 /* The software TNL pipeline has a fixed amount of storage for
478 * vertices and it is necessary to split incoming drawing commands
479 * if they exceed that limit.
481 struct split_limits limits
;
482 limits
.max_verts
= max
;
483 limits
.max_vb_size
= ~0;
484 limits
.max_indices
= ~0;
486 /* This will split the buffers one way or another and
487 * recursively call back into this function.
489 vbo_split_prims( ctx
, arrays
, prim
, nr_prims
, ib
,
490 0, max_index
+ prim
->basevertex
,
495 /* May need to map a vertex buffer object for every attribute plus
496 * one for the index buffer.
498 struct gl_buffer_object
*bo
[VERT_ATTRIB_MAX
+ 1];
502 for (i
= 0; i
< nr_prims
;) {
503 GLuint this_nr_prims
;
505 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices
506 * will rebase the elements to the basevertex, and we'll only
507 * emit strings of prims with the same basevertex in one draw call.
509 for (this_nr_prims
= 1; i
+ this_nr_prims
< nr_prims
;
511 if (prim
[i
].basevertex
!= prim
[i
+ this_nr_prims
].basevertex
)
515 assert(prim
[i
].num_instances
> 0);
517 /* Binding inputs may imply mapping some vertex buffer objects.
518 * They will need to be unmapped below.
520 for (inst
= 0; inst
< prim
[i
].num_instances
; inst
++) {
522 bind_prims(ctx
, &prim
[i
], this_nr_prims
);
523 bind_inputs(ctx
, arrays
, max_index
+ prim
[i
].basevertex
+ 1,
525 bind_indices(ctx
, ib
, bo
, &nr_bo
);
527 tnl
->CurInstance
= inst
;
528 TNL_CONTEXT(ctx
)->Driver
.RunPipeline(ctx
);
530 unmap_vbos(ctx
, bo
, nr_bo
);
541 _tnl_bind_inputs( struct gl_context
*ctx
)
543 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
544 _mesa_set_drawing_arrays(ctx
, tnl
->draw_arrays
.inputs
);
545 _vbo_update_inputs(ctx
, &tnl
->draw_arrays
);
549 /* This is the main entrypoint into the slimmed-down software tnl
550 * module. In a regular swtnl driver, this can be plugged straight
551 * into the ctx->Driver.Draw() callback.
554 _tnl_draw(struct gl_context
*ctx
,
555 const struct _mesa_prim
*prim
, GLuint nr_prims
,
556 const struct _mesa_index_buffer
*ib
,
557 GLboolean index_bounds_valid
, GLuint min_index
, GLuint max_index
,
558 struct gl_transform_feedback_object
*tfb_vertcount
,
559 unsigned stream
, struct gl_buffer_object
*indirect
)
561 /* Update TNLcontext::draw_arrays and set that pointer
562 * into Array._DrawArrays.
564 _tnl_bind_inputs(ctx
);
566 _tnl_draw_prims(ctx
, prim
, nr_prims
, ib
, index_bounds_valid
,
567 min_index
, max_index
, tfb_vertcount
, stream
, indirect
);
572 _tnl_init_driver_draw_function(struct dd_function_table
*functions
)
574 functions
->Draw
= _tnl_draw
;