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 "util/half_float.h"
40 #include "t_context.h"
46 static GLubyte
*get_space(struct gl_context
*ctx
, GLuint bytes
)
48 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
49 GLubyte
*space
= malloc(bytes
);
51 tnl
->block
[tnl
->nr_blocks
++] = space
;
56 static void free_space(struct gl_context
*ctx
)
58 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
60 for (i
= 0; i
< tnl
->nr_blocks
; i
++)
66 /* Convert the incoming array to GLfloats. Understands the
67 * array->Normalized flag and selects the correct conversion method.
69 #define CONVERT( TYPE, MACRO ) do { \
71 if (input->Normalized) { \
72 for (i = 0; i < count; i++) { \
73 const TYPE *in = (TYPE *)ptr; \
74 for (j = 0; j < sz; j++) { \
75 *fptr++ = MACRO(*in); \
78 ptr += input->StrideB; \
81 for (i = 0; i < count; i++) { \
82 const TYPE *in = (TYPE *)ptr; \
83 for (j = 0; j < sz; j++) { \
84 *fptr++ = (GLfloat)(*in); \
87 ptr += input->StrideB; \
94 * Convert array of BGRA/GLubyte[4] values to RGBA/float[4]
95 * \param ptr input/ubyte array
96 * \param fptr output/float array
99 convert_bgra_to_float(const struct gl_vertex_array
*input
,
100 const GLubyte
*ptr
, GLfloat
*fptr
,
104 assert(input
->Normalized
);
105 assert(input
->Size
== 4);
106 for (i
= 0; i
< count
; i
++) {
107 const GLubyte
*in
= (GLubyte
*) ptr
; /* in is in BGRA order */
108 *fptr
++ = UBYTE_TO_FLOAT(in
[2]); /* red */
109 *fptr
++ = UBYTE_TO_FLOAT(in
[1]); /* green */
110 *fptr
++ = UBYTE_TO_FLOAT(in
[0]); /* blue */
111 *fptr
++ = UBYTE_TO_FLOAT(in
[3]); /* alpha */
112 ptr
+= input
->StrideB
;
117 convert_half_to_float(const struct gl_vertex_array
*input
,
118 const GLubyte
*ptr
, GLfloat
*fptr
,
119 GLuint count
, GLuint sz
)
123 for (i
= 0; i
< count
; i
++) {
124 GLhalfARB
*in
= (GLhalfARB
*)ptr
;
126 for (j
= 0; j
< sz
; j
++) {
127 *fptr
++ = _mesa_half_to_float(in
[j
]);
129 ptr
+= input
->StrideB
;
134 * \brief Convert fixed-point to floating-point.
136 * In OpenGL, a fixed-point number is a "signed 2's complement 16.16 scaled
137 * integer" (Table 2.2 of the OpenGL ES 2.0 spec).
139 * If the buffer has the \c normalized flag set, the formula
140 * \code normalize(x) := (2*x + 1) / (2^16 - 1) \endcode
141 * is used to map the fixed-point numbers into the range [-1, 1].
144 convert_fixed_to_float(const struct gl_vertex_array
*input
,
145 const GLubyte
*ptr
, GLfloat
*fptr
,
150 const GLint size
= input
->Size
;
152 if (input
->Normalized
) {
153 for (i
= 0; i
< count
; ++i
) {
154 const GLfixed
*in
= (GLfixed
*) ptr
;
155 for (j
= 0; j
< size
; ++j
) {
156 *fptr
++ = (GLfloat
) (2 * in
[j
] + 1) / (GLfloat
) ((1 << 16) - 1);
158 ptr
+= input
->StrideB
;
161 for (i
= 0; i
< count
; ++i
) {
162 const GLfixed
*in
= (GLfixed
*) ptr
;
163 for (j
= 0; j
< size
; ++j
) {
164 *fptr
++ = in
[j
] / (GLfloat
) (1 << 16);
166 ptr
+= input
->StrideB
;
171 /* Adjust pointer to point at first requested element, convert to
172 * floating point, populate VB->AttribPtr[].
174 static void _tnl_import_array( struct gl_context
*ctx
,
177 const struct gl_vertex_array
*input
,
180 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
181 struct vertex_buffer
*VB
= &tnl
->vb
;
182 GLuint stride
= input
->StrideB
;
184 if (input
->Type
!= GL_FLOAT
) {
185 const GLuint sz
= input
->Size
;
186 GLubyte
*buf
= get_space(ctx
, count
* sz
* sizeof(GLfloat
));
187 GLfloat
*fptr
= (GLfloat
*)buf
;
189 switch (input
->Type
) {
191 CONVERT(GLbyte
, BYTE_TO_FLOAT
);
193 case GL_UNSIGNED_BYTE
:
194 if (input
->Format
== GL_BGRA
) {
195 /* See GL_EXT_vertex_array_bgra */
196 convert_bgra_to_float(input
, ptr
, fptr
, count
);
199 CONVERT(GLubyte
, UBYTE_TO_FLOAT
);
203 CONVERT(GLshort
, SHORT_TO_FLOAT
);
205 case GL_UNSIGNED_SHORT
:
206 CONVERT(GLushort
, USHORT_TO_FLOAT
);
209 CONVERT(GLint
, INT_TO_FLOAT
);
211 case GL_UNSIGNED_INT
:
212 CONVERT(GLuint
, UINT_TO_FLOAT
);
215 CONVERT(GLdouble
, (GLfloat
));
218 convert_half_to_float(input
, ptr
, fptr
, count
, sz
);
221 convert_fixed_to_float(input
, ptr
, fptr
, count
);
229 stride
= sz
* sizeof(GLfloat
);
232 VB
->AttribPtr
[attrib
] = &tnl
->tmp_inputs
[attrib
];
233 VB
->AttribPtr
[attrib
]->data
= (GLfloat (*)[4])ptr
;
234 VB
->AttribPtr
[attrib
]->start
= (GLfloat
*)ptr
;
235 VB
->AttribPtr
[attrib
]->count
= count
;
236 VB
->AttribPtr
[attrib
]->stride
= stride
;
237 VB
->AttribPtr
[attrib
]->size
= input
->Size
;
239 /* This should die, but so should the whole GLvector4f concept:
241 VB
->AttribPtr
[attrib
]->flags
= (((1<<input
->Size
)-1) |
243 (stride
== 4*sizeof(GLfloat
) ? 0 : VEC_BAD_STRIDE
));
245 VB
->AttribPtr
[attrib
]->storage
= NULL
;
248 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
251 static GLboolean
*_tnl_import_edgeflag( struct gl_context
*ctx
,
252 const GLvector4f
*input
,
255 const GLubyte
*ptr
= (const GLubyte
*)input
->data
;
256 const GLuint stride
= input
->stride
;
257 GLboolean
*space
= (GLboolean
*)get_space(ctx
, count
+ CLIPVERTS
);
258 GLboolean
*bptr
= space
;
261 for (i
= 0; i
< count
; i
++) {
262 *bptr
++ = ((GLfloat
*)ptr
)[0] == 1.0F
;
270 static void bind_inputs( struct gl_context
*ctx
,
271 const struct gl_vertex_array
*inputs
[],
273 struct gl_buffer_object
**bo
,
276 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
277 struct vertex_buffer
*VB
= &tnl
->vb
;
282 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
285 if (inputs
[i
]->BufferObj
->Name
) {
286 if (!inputs
[i
]->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
) {
287 bo
[*nr_bo
] = inputs
[i
]->BufferObj
;
289 ctx
->Driver
.MapBufferRange(ctx
, 0, inputs
[i
]->BufferObj
->Size
,
291 inputs
[i
]->BufferObj
,
294 assert(inputs
[i
]->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
);
297 ptr
= ADD_POINTERS(inputs
[i
]->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
,
301 ptr
= inputs
[i
]->Ptr
;
303 /* Just make sure the array is floating point, otherwise convert to
306 * XXX: remove the GLvector4f type at some stage and just use
309 _tnl_import_array(ctx
, i
, count
, inputs
[i
], ptr
);
312 /* We process only the vertices between min & max index:
316 /* These should perhaps be part of _TNL_ATTRIB_* */
317 VB
->BackfaceColorPtr
= NULL
;
318 VB
->BackfaceIndexPtr
= NULL
;
319 VB
->BackfaceSecondaryColorPtr
= NULL
;
321 /* Clipping and drawing code still requires this to be a packed
322 * array of ubytes which can be written into. TODO: Fix and
325 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
326 ctx
->Polygon
.BackMode
!= GL_FILL
)
328 VB
->EdgeFlag
= _tnl_import_edgeflag( ctx
,
329 VB
->AttribPtr
[_TNL_ATTRIB_EDGEFLAG
],
333 /* the data previously pointed to by EdgeFlag may have been freed */
339 /* Translate indices to GLuints and store in VB->Elts.
341 static void bind_indices( struct gl_context
*ctx
,
342 const struct _mesa_index_buffer
*ib
,
343 struct gl_buffer_object
**bo
,
346 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
347 struct vertex_buffer
*VB
= &tnl
->vb
;
356 if (_mesa_is_bufferobj(ib
->obj
) &&
357 !_mesa_bufferobj_mapped(ib
->obj
, MAP_INTERNAL
)) {
358 /* if the buffer object isn't mapped yet, map it now */
359 bo
[*nr_bo
] = ib
->obj
;
361 ptr
= ctx
->Driver
.MapBufferRange(ctx
, (GLsizeiptr
) ib
->ptr
,
362 ib
->count
* ib
->index_size
,
363 GL_MAP_READ_BIT
, ib
->obj
,
365 assert(ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
);
367 /* user-space elements, or buffer already mapped */
368 ptr
= ADD_POINTERS(ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
, ib
->ptr
);
371 if (ib
->index_size
== 4 && VB
->Primitive
[0].basevertex
== 0) {
372 VB
->Elts
= (GLuint
*) ptr
;
375 GLuint
*elts
= (GLuint
*)get_space(ctx
, ib
->count
* sizeof(GLuint
));
378 if (ib
->index_size
== 4) {
379 const GLuint
*in
= (GLuint
*)ptr
;
380 for (i
= 0; i
< ib
->count
; i
++)
381 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
383 else if (ib
->index_size
== 2) {
384 const GLushort
*in
= (GLushort
*)ptr
;
385 for (i
= 0; i
< ib
->count
; i
++)
386 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
389 const GLubyte
*in
= (GLubyte
*)ptr
;
390 for (i
= 0; i
< ib
->count
; i
++)
391 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
396 static void bind_prims( struct gl_context
*ctx
,
397 const struct _mesa_prim
*prim
,
400 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
401 struct vertex_buffer
*VB
= &tnl
->vb
;
403 VB
->Primitive
= prim
;
404 VB
->PrimitiveCount
= nr_prims
;
407 static void unmap_vbos( struct gl_context
*ctx
,
408 struct gl_buffer_object
**bo
,
412 for (i
= 0; i
< nr_bo
; i
++) {
413 ctx
->Driver
.UnmapBuffer(ctx
, bo
[i
], MAP_INTERNAL
);
418 /* This is the main entrypoint into the slimmed-down software tnl
419 * module. In a regular swtnl driver, this can be plugged straight
420 * into the vbo->Driver.DrawPrims() callback.
422 void _tnl_draw_prims(struct gl_context
*ctx
,
423 const struct _mesa_prim
*prim
,
425 const struct _mesa_index_buffer
*ib
,
426 GLboolean index_bounds_valid
,
429 struct gl_transform_feedback_object
*tfb_vertcount
,
431 struct gl_buffer_object
*indirect
)
433 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
434 const struct gl_vertex_array
**arrays
= ctx
->Array
._DrawArrays
;
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
);
454 printf("%s %d..%d\n", __func__
, min_index
, max_index
);
455 for (i
= 0; i
< nr_prims
; i
++)
456 printf("prim %d: %s start %d count %d\n", i
,
457 _mesa_enum_to_string(prim
[i
].mode
),
463 /* We always translate away calls with min_index != 0.
465 t_rebase_prims( ctx
, arrays
, prim
, nr_prims
, ib
,
466 min_index
, max_index
,
470 else if ((GLint
)max_index
+ max_basevertex
> max
) {
471 /* The software TNL pipeline has a fixed amount of storage for
472 * vertices and it is necessary to split incoming drawing commands
473 * if they exceed that limit.
475 struct split_limits limits
;
476 limits
.max_verts
= max
;
477 limits
.max_vb_size
= ~0;
478 limits
.max_indices
= ~0;
480 /* This will split the buffers one way or another and
481 * recursively call back into this function.
483 vbo_split_prims( ctx
, arrays
, prim
, nr_prims
, ib
,
484 0, max_index
+ prim
->basevertex
,
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 for (i
= 0; i
< nr_prims
;) {
497 GLuint this_nr_prims
;
499 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices
500 * will rebase the elements to the basevertex, and we'll only
501 * emit strings of prims with the same basevertex in one draw call.
503 for (this_nr_prims
= 1; i
+ this_nr_prims
< nr_prims
;
505 if (prim
[i
].basevertex
!= prim
[i
+ this_nr_prims
].basevertex
)
509 assert(prim
[i
].num_instances
> 0);
511 /* Binding inputs may imply mapping some vertex buffer objects.
512 * They will need to be unmapped below.
514 for (inst
= 0; inst
< prim
[i
].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
);