3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Keith Whitwell <keith@tungstengraphics.com>
37 #include "t_context.h"
38 #include "t_pipeline.h"
39 #include "t_vp_build.h"
45 static GLubyte
*get_space(GLcontext
*ctx
, GLuint bytes
)
47 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
48 GLubyte
*space
= _mesa_malloc(bytes
);
50 tnl
->block
[tnl
->nr_blocks
++] = space
;
55 static void free_space(GLcontext
*ctx
)
57 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
59 for (i
= 0; i
< tnl
->nr_blocks
; i
++)
60 _mesa_free(tnl
->block
[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 /* Adjust pointer to point at first requested element, convert to
94 * floating point, populate VB->AttribPtr[].
96 static void _tnl_import_array( GLcontext
*ctx
,
99 const struct gl_client_array
*input
,
102 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
103 struct vertex_buffer
*VB
= &tnl
->vb
;
104 GLuint stride
= input
->StrideB
;
106 if (input
->Type
!= GL_FLOAT
) {
107 const GLuint sz
= input
->Size
;
108 GLubyte
*buf
= get_space(ctx
, count
* sz
* sizeof(GLfloat
));
109 GLfloat
*fptr
= (GLfloat
*)buf
;
111 switch (input
->Type
) {
113 CONVERT(GLbyte
, BYTE_TO_FLOAT
);
115 case GL_UNSIGNED_BYTE
:
116 CONVERT(GLubyte
, UBYTE_TO_FLOAT
);
119 CONVERT(GLshort
, SHORT_TO_FLOAT
);
121 case GL_UNSIGNED_SHORT
:
122 CONVERT(GLushort
, USHORT_TO_FLOAT
);
125 CONVERT(GLint
, INT_TO_FLOAT
);
127 case GL_UNSIGNED_INT
:
128 CONVERT(GLuint
, UINT_TO_FLOAT
);
131 CONVERT(GLdouble
, (GLfloat
));
139 stride
= sz
* sizeof(GLfloat
);
142 VB
->AttribPtr
[attrib
] = &tnl
->tmp_inputs
[attrib
];
143 VB
->AttribPtr
[attrib
]->data
= (GLfloat (*)[4])ptr
;
144 VB
->AttribPtr
[attrib
]->start
= (GLfloat
*)ptr
;
145 VB
->AttribPtr
[attrib
]->count
= count
;
146 VB
->AttribPtr
[attrib
]->stride
= stride
;
147 VB
->AttribPtr
[attrib
]->size
= input
->Size
;
149 /* This should die, but so should the whole GLvector4f concept:
151 VB
->AttribPtr
[attrib
]->flags
= (((1<<input
->Size
)-1) |
153 (stride
== 4*sizeof(GLfloat
) ? 0 : VEC_BAD_STRIDE
));
155 VB
->AttribPtr
[attrib
]->storage
= NULL
;
158 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
161 static GLboolean
*_tnl_import_edgeflag( GLcontext
*ctx
,
162 const GLvector4f
*input
,
165 const GLubyte
*ptr
= (const GLubyte
*)input
->data
;
166 const GLuint stride
= input
->stride
;
167 GLboolean
*space
= (GLboolean
*)get_space(ctx
, count
+ CLIPVERTS
);
168 GLboolean
*bptr
= space
;
171 for (i
= 0; i
< count
; i
++) {
172 *bptr
++ = ((GLfloat
*)ptr
)[0] == 1.0;
180 static void bind_inputs( GLcontext
*ctx
,
181 const struct gl_client_array
*inputs
[],
183 struct gl_buffer_object
**bo
,
186 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
187 struct vertex_buffer
*VB
= &tnl
->vb
;
192 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
195 if (inputs
[i
]->BufferObj
->Name
) {
196 if (!inputs
[i
]->BufferObj
->Pointer
) {
197 bo
[*nr_bo
] = inputs
[i
]->BufferObj
;
199 ctx
->Driver
.MapBuffer(ctx
,
202 inputs
[i
]->BufferObj
);
204 assert(inputs
[i
]->BufferObj
->Pointer
);
207 ptr
= ADD_POINTERS(inputs
[i
]->BufferObj
->Pointer
,
211 ptr
= inputs
[i
]->Ptr
;
213 /* Just make sure the array is floating point, otherwise convert to
216 * XXX: remove the GLvector4f type at some stage and just use
219 _tnl_import_array(ctx
, i
, count
, inputs
[i
], ptr
);
222 /* We process only the vertices between min & max index:
227 /* Legacy pointers -- remove one day.
229 VB
->ObjPtr
= VB
->AttribPtr
[_TNL_ATTRIB_POS
];
230 VB
->NormalPtr
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
];
231 VB
->ColorPtr
[0] = VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
];
232 VB
->ColorPtr
[1] = NULL
;
233 VB
->IndexPtr
[0] = VB
->AttribPtr
[_TNL_ATTRIB_COLOR_INDEX
];
234 VB
->IndexPtr
[1] = NULL
;
235 VB
->SecondaryColorPtr
[0] = VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
];
236 VB
->SecondaryColorPtr
[1] = NULL
;
237 VB
->FogCoordPtr
= VB
->AttribPtr
[_TNL_ATTRIB_FOG
];
239 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
240 VB
->TexCoordPtr
[i
] = VB
->AttribPtr
[_TNL_ATTRIB_TEX0
+ i
];
243 /* Clipping and drawing code still requires this to be a packed
244 * array of ubytes which can be written into. TODO: Fix and
247 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
248 ctx
->Polygon
.BackMode
!= GL_FILL
)
250 VB
->EdgeFlag
= _tnl_import_edgeflag( ctx
,
251 VB
->AttribPtr
[_TNL_ATTRIB_EDGEFLAG
],
255 /* the data previously pointed to by EdgeFlag may have been freed */
261 /* Translate indices to GLuints and store in VB->Elts.
263 static void bind_indices( GLcontext
*ctx
,
264 const struct _mesa_index_buffer
*ib
,
265 struct gl_buffer_object
**bo
,
268 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
269 struct vertex_buffer
*VB
= &tnl
->vb
;
278 if (ib
->obj
->Name
&& !ib
->obj
->Pointer
) {
279 bo
[*nr_bo
] = ib
->obj
;
281 ctx
->Driver
.MapBuffer(ctx
,
282 GL_ELEMENT_ARRAY_BUFFER
,
286 assert(ib
->obj
->Pointer
);
289 ptr
= ADD_POINTERS(ib
->obj
->Pointer
, ib
->ptr
);
291 if (ib
->type
== GL_UNSIGNED_INT
) {
292 VB
->Elts
= (GLuint
*) ptr
;
295 GLuint
*elts
= (GLuint
*)get_space(ctx
, ib
->count
* sizeof(GLuint
));
298 if (ib
->type
== GL_UNSIGNED_SHORT
) {
299 const GLushort
*in
= (GLushort
*)ptr
;
300 for (i
= 0; i
< ib
->count
; i
++)
301 *elts
++ = (GLuint
)(*in
++);
304 const GLubyte
*in
= (GLubyte
*)ptr
;
305 for (i
= 0; i
< ib
->count
; i
++)
306 *elts
++ = (GLuint
)(*in
++);
311 static void bind_prims( GLcontext
*ctx
,
312 const struct _mesa_prim
*prim
,
315 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
316 struct vertex_buffer
*VB
= &tnl
->vb
;
318 VB
->Primitive
= prim
;
319 VB
->PrimitiveCount
= nr_prims
;
322 static void unmap_vbos( GLcontext
*ctx
,
323 struct gl_buffer_object
**bo
,
327 for (i
= 0; i
< nr_bo
; i
++) {
328 ctx
->Driver
.UnmapBuffer(ctx
,
329 0, /* target -- I don't see why this would be needed */
336 /* This is the main entrypoint into the slimmed-down software tnl
337 * module. In a regular swtnl driver, this can be plugged straight
338 * into the vbo->Driver.DrawPrims() callback.
340 void _tnl_draw_prims( GLcontext
*ctx
,
341 const struct gl_client_array
*arrays
[],
342 const struct _mesa_prim
*prim
,
344 const struct _mesa_index_buffer
*ib
,
348 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
349 const GLuint TEST_SPLIT
= 0;
350 const GLint max
= TEST_SPLIT
? 8 : tnl
->vb
.Size
- MAX_CLIPPED_VERTICES
;
355 _mesa_printf("%s %d..%d\n", __FUNCTION__
, min_index
, max_index
);
356 for (i
= 0; i
< nr_prims
; i
++)
357 _mesa_printf("prim %d: %s start %d count %d\n", i
,
358 _mesa_lookup_enum_by_nr(prim
[i
].mode
),
364 /* We always translate away calls with min_index != 0.
366 vbo_rebase_prims( ctx
, arrays
, prim
, nr_prims
, ib
,
367 min_index
, max_index
,
371 else if (max_index
>= max
) {
372 /* The software TNL pipeline has a fixed amount of storage for
373 * vertices and it is necessary to split incoming drawing commands
374 * if they exceed that limit.
376 struct split_limits limits
;
377 limits
.max_verts
= max
;
378 limits
.max_vb_size
= ~0;
379 limits
.max_indices
= ~0;
381 /* This will split the buffers one way or another and
382 * recursively call back into this function.
384 vbo_split_prims( ctx
, arrays
, prim
, nr_prims
, ib
,
390 /* May need to map a vertex buffer object for every attribute plus
391 * one for the index buffer.
393 struct gl_buffer_object
*bo
[VERT_ATTRIB_MAX
+ 1];
396 /* Binding inputs may imply mapping some vertex buffer objects.
397 * They will need to be unmapped below.
399 bind_inputs(ctx
, arrays
, max_index
+1, bo
, &nr_bo
);
400 bind_indices(ctx
, ib
, bo
, &nr_bo
);
401 bind_prims(ctx
, prim
, nr_prims
);
403 TNL_CONTEXT(ctx
)->Driver
.RunPipeline(ctx
);
405 unmap_vbos(ctx
, bo
, nr_bo
);