2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keith@tungstengraphics.com>
30 static GLubyte
*get_space(struct gl_context
*ctx
, GLuint bytes
)
32 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
33 GLubyte
*space
= malloc(bytes
);
35 tnl
->block
[tnl
->nr_blocks
++] = space
;
40 static void free_space(struct gl_context
*ctx
)
42 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
44 for (i
= 0; i
< tnl
->nr_blocks
; i
++)
50 /* Convert the incoming array to GLfloats. Understands the
51 * array->Normalized flag and selects the correct conversion method.
53 #define CONVERT( TYPE, MACRO ) do { \
55 if (input->Normalized) { \
56 for (i = 0; i < count; i++) { \
57 const TYPE *in = (TYPE *)ptr; \
58 for (j = 0; j < sz; j++) { \
59 *fptr++ = MACRO(*in); \
62 ptr += input->StrideB; \
65 for (i = 0; i < count; i++) { \
66 const TYPE *in = (TYPE *)ptr; \
67 for (j = 0; j < sz; j++) { \
68 *fptr++ = (GLfloat)(*in); \
71 ptr += input->StrideB; \
77 * \brief Convert fixed-point to floating-point.
79 * In OpenGL, a fixed-point number is a "signed 2's complement 16.16 scaled
80 * integer" (Table 2.2 of the OpenGL ES 2.0 spec).
82 * If the buffer has the \c normalized flag set, the formula
83 * \code normalize(x) := (2*x + 1) / (2^16 - 1) \endcode
84 * is used to map the fixed-point numbers into the range [-1, 1].
87 convert_fixed_to_float(const struct gl_client_array
*input
,
88 const GLubyte
*ptr
, GLfloat
*fptr
,
92 const GLint size
= input
->Size
;
94 if (input
->Normalized
) {
95 for (i
= 0; i
< count
; ++i
) {
96 const GLfixed
*in
= (GLfixed
*) ptr
;
97 for (j
= 0; j
< size
; ++j
) {
98 *fptr
++ = (GLfloat
) (2 * in
[j
] + 1) / (GLfloat
) ((1 << 16) - 1);
100 ptr
+= input
->StrideB
;
103 for (i
= 0; i
< count
; ++i
) {
104 const GLfixed
*in
= (GLfixed
*) ptr
;
105 for (j
= 0; j
< size
; ++j
) {
106 *fptr
++ = in
[j
] / (GLfloat
) (1 << 16);
108 ptr
+= input
->StrideB
;
113 /* Adjust pointer to point at first requested element, convert to
114 * floating point, populate VB->AttribPtr[].
116 static void _tnl_import_array( struct gl_context
*ctx
,
119 const struct gl_client_array
*input
,
122 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
123 struct vertex_buffer
*VB
= &tnl
->vb
;
124 GLuint stride
= input
->StrideB
;
126 if (input
->Type
!= GL_FLOAT
) {
127 const GLuint sz
= input
->Size
;
128 GLubyte
*buf
= get_space(ctx
, count
* sz
* sizeof(GLfloat
));
129 GLfloat
*fptr
= (GLfloat
*)buf
;
131 switch (input
->Type
) {
133 CONVERT(GLbyte
, BYTE_TO_FLOAT
);
135 case GL_UNSIGNED_BYTE
:
136 CONVERT(GLubyte
, UBYTE_TO_FLOAT
);
139 CONVERT(GLshort
, SHORT_TO_FLOAT
);
141 case GL_UNSIGNED_SHORT
:
142 CONVERT(GLushort
, USHORT_TO_FLOAT
);
145 CONVERT(GLint
, INT_TO_FLOAT
);
147 case GL_UNSIGNED_INT
:
148 CONVERT(GLuint
, UINT_TO_FLOAT
);
151 CONVERT(GLdouble
, (GLfloat
));
154 convert_fixed_to_float(input
, ptr
, fptr
, count
);
162 stride
= sz
* sizeof(GLfloat
);
165 VB
->AttribPtr
[attrib
] = &tnl
->tmp_inputs
[attrib
];
166 VB
->AttribPtr
[attrib
]->data
= (GLfloat (*)[4])ptr
;
167 VB
->AttribPtr
[attrib
]->start
= (GLfloat
*)ptr
;
168 VB
->AttribPtr
[attrib
]->count
= count
;
169 VB
->AttribPtr
[attrib
]->stride
= stride
;
170 VB
->AttribPtr
[attrib
]->size
= input
->Size
;
172 /* This should die, but so should the whole GLvector4f concept:
174 VB
->AttribPtr
[attrib
]->flags
= (((1<<input
->Size
)-1) |
176 (stride
== 4*sizeof(GLfloat
) ? 0 : VEC_BAD_STRIDE
));
178 VB
->AttribPtr
[attrib
]->storage
= NULL
;
181 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
184 static GLboolean
*_tnl_import_edgeflag( struct gl_context
*ctx
,
185 const GLvector4f
*input
,
188 const GLubyte
*ptr
= (const GLubyte
*)input
->data
;
189 const GLuint stride
= input
->stride
;
190 GLboolean
*space
= (GLboolean
*)get_space(ctx
, count
+ CLIPVERTS
);
191 GLboolean
*bptr
= space
;
194 for (i
= 0; i
< count
; i
++) {
195 *bptr
++ = ((GLfloat
*)ptr
)[0] == 1.0;
202 static void bind_inputs( struct gl_context
*ctx
,
203 const struct gl_client_array
*inputs
[],
205 struct gl_buffer_object
**bo
,
208 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
209 struct vertex_buffer
*VB
= &tnl
->vb
;
214 for (i
= 0; i
< _TNL_ATTRIB_MAX
; i
++) {
217 if (inputs
[i
]->BufferObj
->Name
) {
218 if (!inputs
[i
]->BufferObj
->Pointer
) {
219 bo
[*nr_bo
] = inputs
[i
]->BufferObj
;
221 ctx
->Driver
.MapBufferRange(ctx
, 0, inputs
[i
]->BufferObj
->Size
,
223 inputs
[i
]->BufferObj
);
225 assert(inputs
[i
]->BufferObj
->Pointer
);
228 ptr
= ADD_POINTERS(inputs
[i
]->BufferObj
->Pointer
,
232 ptr
= inputs
[i
]->Ptr
;
234 /* Just make sure the array is floating point, otherwise convert to
237 * XXX: remove the GLvector4f type at some stage and just use
240 _tnl_import_array(ctx
, i
, count
, inputs
[i
], ptr
);
243 /* We process only the vertices between min & max index:
247 /* These should perhaps be part of _TNL_ATTRIB_* */
248 VB
->BackfaceColorPtr
= NULL
;
249 VB
->BackfaceIndexPtr
= NULL
;
251 /* Clipping and drawing code still requires this to be a packed
252 * array of ubytes which can be written into. TODO: Fix and
255 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
256 ctx
->Polygon
.BackMode
!= GL_FILL
)
258 VB
->EdgeFlag
= _tnl_import_edgeflag( ctx
,
259 VB
->AttribPtr
[_TNL_ATTRIB_EDGEFLAG
],
263 /* the data previously pointed to by EdgeFlag may have been freed */
269 /* Translate indices to GLuints and store in VB->Elts.
271 static void bind_indices( struct gl_context
*ctx
,
272 const struct _mesa_index_buffer
*ib
,
273 struct gl_buffer_object
**bo
,
276 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
277 struct vertex_buffer
*VB
= &tnl
->vb
;
286 if (_mesa_is_bufferobj(ib
->obj
) && !_mesa_bufferobj_mapped(ib
->obj
)) {
287 /* if the buffer object isn't mapped yet, map it now */
288 bo
[*nr_bo
] = ib
->obj
;
290 ptr
= ctx
->Driver
.MapBufferRange(ctx
, (GLsizeiptr
) ib
->ptr
,
291 ib
->count
* vbo_sizeof_ib_type(ib
->type
),
292 GL_MAP_READ_BIT
, ib
->obj
);
293 assert(ib
->obj
->Pointer
);
295 /* user-space elements, or buffer already mapped */
296 ptr
= ADD_POINTERS(ib
->obj
->Pointer
, ib
->ptr
);
299 if (ib
->type
== GL_UNSIGNED_INT
) {
300 VB
->Elts
= (GLuint
*) ptr
;
303 GLuint
*elts
= (GLuint
*)get_space(ctx
, ib
->count
* sizeof(GLuint
));
306 if (ib
->type
== GL_UNSIGNED_SHORT
) {
307 const GLushort
*in
= (GLushort
*)ptr
;
308 for (i
= 0; i
< ib
->count
; i
++)
309 *elts
++ = (GLuint
)(*in
++);
312 const GLubyte
*in
= (GLubyte
*)ptr
;
313 for (i
= 0; i
< ib
->count
; i
++)
314 *elts
++ = (GLuint
)(*in
++);
319 static void bind_prims( struct gl_context
*ctx
,
320 const struct _mesa_prim
*prim
,
323 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
324 struct vertex_buffer
*VB
= &tnl
->vb
;
326 VB
->Primitive
= prim
;
327 VB
->PrimitiveCount
= nr_prims
;
330 static void unmap_vbos( struct gl_context
*ctx
,
331 struct gl_buffer_object
**bo
,
335 for (i
= 0; i
< nr_bo
; i
++) {
336 ctx
->Driver
.UnmapBuffer(ctx
, bo
[i
]);
341 void _tnl_vbo_draw_prims(struct gl_context
*ctx
,
342 const struct gl_client_array
*arrays
[],
343 const struct _mesa_prim
*prim
,
345 const struct _mesa_index_buffer
*ib
,
346 GLboolean index_bounds_valid
,
350 if (!index_bounds_valid
)
351 vbo_get_minmax_index(ctx
, prim
, ib
, &min_index
, &max_index
);
353 _tnl_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
356 /* This is the main entrypoint into the slimmed-down software tnl
357 * module. In a regular swtnl driver, this can be plugged straight
358 * into the vbo->Driver.DrawPrims() callback.
360 void _tnl_draw_prims( struct gl_context
*ctx
,
361 const struct gl_client_array
*arrays
[],
362 const struct _mesa_prim
*prim
,
364 const struct _mesa_index_buffer
*ib
,
368 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
369 const GLuint TEST_SPLIT
= 0;
370 const GLint max
= TEST_SPLIT
? 8 : tnl
->vb
.Size
- MAX_CLIPPED_VERTICES
;
372 /* Mesa core state should have been validated already */
373 assert(ctx
->NewState
== 0x0);
378 printf("%s %d..%d\n", __FUNCTION__
, min_index
, max_index
);
379 for (i
= 0; i
< nr_prims
; i
++)
380 printf("prim %d: %s start %d count %d\n", i
,
381 _mesa_lookup_enum_by_nr(prim
[i
].mode
),
387 /* We always translate away calls with min_index != 0.
389 vbo_rebase_prims( ctx
, arrays
, prim
, nr_prims
, ib
,
390 min_index
, max_index
,
391 _tnl_vbo_draw_prims
);
394 else if (max_index
> max
) {
395 /* The software TNL pipeline has a fixed amount of storage for
396 * vertices and it is necessary to split incoming drawing commands
397 * if they exceed that limit.
399 struct split_limits limits
;
400 limits
.max_verts
= max
;
401 limits
.max_vb_size
= ~0;
402 limits
.max_indices
= ~0;
404 /* This will split the buffers one way or another and
405 * recursively call back into this function.
407 vbo_split_prims( ctx
, arrays
, prim
, nr_prims
, ib
,
413 /* May need to map a vertex buffer object for every attribute plus
414 * one for the index buffer.
416 struct gl_buffer_object
*bo
[_TNL_ATTRIB_MAX
];
420 /* Binding inputs may imply mapping some vertex buffer objects.
421 * They will need to be unmapped below.
423 for (inst
= 0; inst
< prim
[0].num_instances
; inst
++) {
425 bind_prims(ctx
, prim
, nr_prims
);
426 bind_inputs(ctx
, arrays
, max_index
+ 1,
428 bind_indices(ctx
, ib
, bo
, &nr_bo
);
430 tnl
->CurInstance
= inst
;
431 TNL_CONTEXT(ctx
)->Driver
.RunPipeline(ctx
);
433 unmap_vbos(ctx
, bo
, nr_bo
);