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"
45 static GLubyte
*get_space(struct gl_context
*ctx
, GLuint bytes
)
47 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
48 GLubyte
*space
= malloc(bytes
);
50 tnl
->block
[tnl
->nr_blocks
++] = space
;
55 static void free_space(struct gl_context
*ctx
)
57 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
59 for (i
= 0; i
< tnl
->nr_blocks
; i
++)
65 /* Convert the incoming array to GLfloats. Understands the
66 * array->Normalized flag and selects the correct conversion method.
68 #define CONVERT( TYPE, MACRO ) do { \
70 if (input->Normalized) { \
71 for (i = 0; i < count; i++) { \
72 const TYPE *in = (TYPE *)ptr; \
73 for (j = 0; j < sz; j++) { \
74 *fptr++ = MACRO(*in); \
77 ptr += input->StrideB; \
80 for (i = 0; i < count; i++) { \
81 const TYPE *in = (TYPE *)ptr; \
82 for (j = 0; j < sz; j++) { \
83 *fptr++ = (GLfloat)(*in); \
86 ptr += input->StrideB; \
93 * Convert array of BGRA/GLubyte[4] values to RGBA/float[4]
94 * \param ptr input/ubyte array
95 * \param fptr output/float array
98 convert_bgra_to_float(const struct gl_vertex_array
*input
,
99 const GLubyte
*ptr
, GLfloat
*fptr
,
103 assert(input
->Normalized
);
104 assert(input
->Size
== 4);
105 for (i
= 0; i
< count
; i
++) {
106 const GLubyte
*in
= (GLubyte
*) ptr
; /* in is in BGRA order */
107 *fptr
++ = UBYTE_TO_FLOAT(in
[2]); /* red */
108 *fptr
++ = UBYTE_TO_FLOAT(in
[1]); /* green */
109 *fptr
++ = UBYTE_TO_FLOAT(in
[0]); /* blue */
110 *fptr
++ = UBYTE_TO_FLOAT(in
[3]); /* alpha */
111 ptr
+= input
->StrideB
;
116 convert_half_to_float(const struct gl_vertex_array
*input
,
117 const GLubyte
*ptr
, GLfloat
*fptr
,
118 GLuint count
, GLuint sz
)
122 for (i
= 0; i
< count
; i
++) {
123 GLhalfARB
*in
= (GLhalfARB
*)ptr
;
125 for (j
= 0; j
< sz
; j
++) {
126 *fptr
++ = _mesa_half_to_float(in
[j
]);
128 ptr
+= input
->StrideB
;
133 * \brief Convert fixed-point to floating-point.
135 * In OpenGL, a fixed-point number is a "signed 2's complement 16.16 scaled
136 * integer" (Table 2.2 of the OpenGL ES 2.0 spec).
138 * If the buffer has the \c normalized flag set, the formula
139 * \code normalize(x) := (2*x + 1) / (2^16 - 1) \endcode
140 * is used to map the fixed-point numbers into the range [-1, 1].
143 convert_fixed_to_float(const struct gl_vertex_array
*input
,
144 const GLubyte
*ptr
, GLfloat
*fptr
,
149 const GLint size
= input
->Size
;
151 if (input
->Normalized
) {
152 for (i
= 0; i
< count
; ++i
) {
153 const GLfixed
*in
= (GLfixed
*) ptr
;
154 for (j
= 0; j
< size
; ++j
) {
155 *fptr
++ = (GLfloat
) (2 * in
[j
] + 1) / (GLfloat
) ((1 << 16) - 1);
157 ptr
+= input
->StrideB
;
160 for (i
= 0; i
< count
; ++i
) {
161 const GLfixed
*in
= (GLfixed
*) ptr
;
162 for (j
= 0; j
< size
; ++j
) {
163 *fptr
++ = in
[j
] / (GLfloat
) (1 << 16);
165 ptr
+= input
->StrideB
;
170 /* Adjust pointer to point at first requested element, convert to
171 * floating point, populate VB->AttribPtr[].
173 static void _tnl_import_array( struct gl_context
*ctx
,
176 const struct gl_vertex_array
*input
,
179 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
180 struct vertex_buffer
*VB
= &tnl
->vb
;
181 GLuint stride
= input
->StrideB
;
183 if (input
->Type
!= GL_FLOAT
) {
184 const GLuint sz
= input
->Size
;
185 GLubyte
*buf
= get_space(ctx
, count
* sz
* sizeof(GLfloat
));
186 GLfloat
*fptr
= (GLfloat
*)buf
;
188 switch (input
->Type
) {
190 CONVERT(GLbyte
, BYTE_TO_FLOAT
);
192 case GL_UNSIGNED_BYTE
:
193 if (input
->Format
== GL_BGRA
) {
194 /* See GL_EXT_vertex_array_bgra */
195 convert_bgra_to_float(input
, ptr
, fptr
, count
);
198 CONVERT(GLubyte
, UBYTE_TO_FLOAT
);
202 CONVERT(GLshort
, SHORT_TO_FLOAT
);
204 case GL_UNSIGNED_SHORT
:
205 CONVERT(GLushort
, USHORT_TO_FLOAT
);
208 CONVERT(GLint
, INT_TO_FLOAT
);
210 case GL_UNSIGNED_INT
:
211 CONVERT(GLuint
, UINT_TO_FLOAT
);
214 CONVERT(GLdouble
, (GLfloat
));
217 convert_half_to_float(input
, ptr
, fptr
, count
, sz
);
220 convert_fixed_to_float(input
, ptr
, fptr
, count
);
228 stride
= sz
* sizeof(GLfloat
);
231 VB
->AttribPtr
[attrib
] = &tnl
->tmp_inputs
[attrib
];
232 VB
->AttribPtr
[attrib
]->data
= (GLfloat (*)[4])ptr
;
233 VB
->AttribPtr
[attrib
]->start
= (GLfloat
*)ptr
;
234 VB
->AttribPtr
[attrib
]->count
= count
;
235 VB
->AttribPtr
[attrib
]->stride
= stride
;
236 VB
->AttribPtr
[attrib
]->size
= input
->Size
;
238 /* This should die, but so should the whole GLvector4f concept:
240 VB
->AttribPtr
[attrib
]->flags
= (((1<<input
->Size
)-1) |
242 (stride
== 4*sizeof(GLfloat
) ? 0 : VEC_BAD_STRIDE
));
244 VB
->AttribPtr
[attrib
]->storage
= NULL
;
247 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
250 static GLboolean
*_tnl_import_edgeflag( struct gl_context
*ctx
,
251 const GLvector4f
*input
,
254 const GLubyte
*ptr
= (const GLubyte
*)input
->data
;
255 const GLuint stride
= input
->stride
;
256 GLboolean
*space
= (GLboolean
*)get_space(ctx
, count
+ CLIPVERTS
);
257 GLboolean
*bptr
= space
;
260 for (i
= 0; i
< count
; i
++) {
261 *bptr
++ = ((GLfloat
*)ptr
)[0] == 1.0F
;
269 static void bind_inputs( struct gl_context
*ctx
,
270 const struct gl_vertex_array
*inputs
[],
272 struct gl_buffer_object
**bo
,
275 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
276 struct vertex_buffer
*VB
= &tnl
->vb
;
281 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
284 if (inputs
[i
]->BufferObj
->Name
) {
285 if (!inputs
[i
]->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
) {
286 bo
[*nr_bo
] = inputs
[i
]->BufferObj
;
288 ctx
->Driver
.MapBufferRange(ctx
, 0, inputs
[i
]->BufferObj
->Size
,
290 inputs
[i
]->BufferObj
,
293 assert(inputs
[i
]->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
);
296 ptr
= ADD_POINTERS(inputs
[i
]->BufferObj
->Mappings
[MAP_INTERNAL
].Pointer
,
300 ptr
= inputs
[i
]->Ptr
;
302 /* Just make sure the array is floating point, otherwise convert to
305 * XXX: remove the GLvector4f type at some stage and just use
308 _tnl_import_array(ctx
, i
, count
, inputs
[i
], ptr
);
311 /* We process only the vertices between min & max index:
315 /* These should perhaps be part of _TNL_ATTRIB_* */
316 VB
->BackfaceColorPtr
= NULL
;
317 VB
->BackfaceIndexPtr
= NULL
;
318 VB
->BackfaceSecondaryColorPtr
= NULL
;
320 /* Clipping and drawing code still requires this to be a packed
321 * array of ubytes which can be written into. TODO: Fix and
324 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
325 ctx
->Polygon
.BackMode
!= GL_FILL
)
327 VB
->EdgeFlag
= _tnl_import_edgeflag( ctx
,
328 VB
->AttribPtr
[_TNL_ATTRIB_EDGEFLAG
],
332 /* the data previously pointed to by EdgeFlag may have been freed */
338 /* Translate indices to GLuints and store in VB->Elts.
340 static void bind_indices( struct gl_context
*ctx
,
341 const struct _mesa_index_buffer
*ib
,
342 struct gl_buffer_object
**bo
,
345 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
346 struct vertex_buffer
*VB
= &tnl
->vb
;
355 if (_mesa_is_bufferobj(ib
->obj
) &&
356 !_mesa_bufferobj_mapped(ib
->obj
, MAP_INTERNAL
)) {
357 /* if the buffer object isn't mapped yet, map it now */
358 bo
[*nr_bo
] = ib
->obj
;
360 ptr
= ctx
->Driver
.MapBufferRange(ctx
, (GLsizeiptr
) ib
->ptr
,
361 ib
->count
* ib
->index_size
,
362 GL_MAP_READ_BIT
, ib
->obj
,
364 assert(ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
);
366 /* user-space elements, or buffer already mapped */
367 ptr
= ADD_POINTERS(ib
->obj
->Mappings
[MAP_INTERNAL
].Pointer
, ib
->ptr
);
370 if (ib
->index_size
== 4 && VB
->Primitive
[0].basevertex
== 0) {
371 VB
->Elts
= (GLuint
*) ptr
;
374 GLuint
*elts
= (GLuint
*)get_space(ctx
, ib
->count
* sizeof(GLuint
));
377 if (ib
->index_size
== 4) {
378 const GLuint
*in
= (GLuint
*)ptr
;
379 for (i
= 0; i
< ib
->count
; i
++)
380 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
382 else if (ib
->index_size
== 2) {
383 const GLushort
*in
= (GLushort
*)ptr
;
384 for (i
= 0; i
< ib
->count
; i
++)
385 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
388 const GLubyte
*in
= (GLubyte
*)ptr
;
389 for (i
= 0; i
< ib
->count
; i
++)
390 *elts
++ = (GLuint
)(*in
++) + VB
->Primitive
[0].basevertex
;
395 static void bind_prims( struct gl_context
*ctx
,
396 const struct _mesa_prim
*prim
,
399 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
400 struct vertex_buffer
*VB
= &tnl
->vb
;
402 VB
->Primitive
= prim
;
403 VB
->PrimitiveCount
= nr_prims
;
406 static void unmap_vbos( struct gl_context
*ctx
,
407 struct gl_buffer_object
**bo
,
411 for (i
= 0; i
< nr_bo
; i
++) {
412 ctx
->Driver
.UnmapBuffer(ctx
, bo
[i
], MAP_INTERNAL
);
417 /* This is the main entrypoint into the slimmed-down software tnl
418 * module. In a regular swtnl driver, this can be plugged straight
419 * into the vbo->Driver.DrawPrims() callback.
421 void _tnl_draw_prims(struct gl_context
*ctx
,
422 const struct _mesa_prim
*prim
,
424 const struct _mesa_index_buffer
*ib
,
425 GLboolean index_bounds_valid
,
428 struct gl_transform_feedback_object
*tfb_vertcount
,
430 struct gl_buffer_object
*indirect
)
432 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
433 const struct gl_vertex_array
**arrays
= ctx
->Array
._DrawArrays
;
434 const GLuint TEST_SPLIT
= 0;
435 const GLint max
= TEST_SPLIT
? 8 : tnl
->vb
.Size
- MAX_CLIPPED_VERTICES
;
436 GLint max_basevertex
= prim
->basevertex
;
439 if (!index_bounds_valid
)
440 vbo_get_minmax_indices(ctx
, prim
, ib
, &min_index
, &max_index
, nr_prims
);
442 /* Mesa core state should have been validated already */
443 assert(ctx
->NewState
== 0x0);
445 if (!_mesa_check_conditional_render(ctx
))
446 return; /* don't draw */
448 for (i
= 1; i
< nr_prims
; i
++)
449 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 vbo_rebase_prims( ctx
, arrays
, prim
, nr_prims
, ib
,
465 min_index
, max_index
,
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 vbo_split_prims( ctx
, arrays
, prim
, nr_prims
, ib
,
483 0, max_index
+ prim
->basevertex
,
488 /* May need to map a vertex buffer object for every attribute plus
489 * one for the index buffer.
491 struct gl_buffer_object
*bo
[VERT_ATTRIB_MAX
+ 1];
495 for (i
= 0; i
< nr_prims
;) {
496 GLuint this_nr_prims
;
498 /* Our SW TNL pipeline doesn't handle basevertex yet, so bind_indices
499 * will rebase the elements to the basevertex, and we'll only
500 * emit strings of prims with the same basevertex in one draw call.
502 for (this_nr_prims
= 1; i
+ this_nr_prims
< nr_prims
;
504 if (prim
[i
].basevertex
!= prim
[i
+ this_nr_prims
].basevertex
)
508 assert(prim
[i
].num_instances
> 0);
510 /* Binding inputs may imply mapping some vertex buffer objects.
511 * They will need to be unmapped below.
513 for (inst
= 0; inst
< prim
[i
].num_instances
; inst
++) {
515 bind_prims(ctx
, &prim
[i
], this_nr_prims
);
516 bind_inputs(ctx
, arrays
, max_index
+ prim
[i
].basevertex
+ 1,
518 bind_indices(ctx
, ib
, bo
, &nr_bo
);
520 tnl
->CurInstance
= inst
;
521 TNL_CONTEXT(ctx
)->Driver
.RunPipeline(ctx
);
523 unmap_vbos(ctx
, bo
, nr_bo
);