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>
36 #include "t_context.h"
37 #include "t_pipeline.h"
38 #include "t_vp_build.h"
44 static GLfloat
*get_space(GLcontext
*ctx
, GLuint bytes
)
46 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
47 GLubyte
*space
= _mesa_malloc(bytes
);
49 tnl
->block
[tnl
->nr_blocks
++] = space
;
50 return (GLfloat
*)space
;
54 static void free_space(GLcontext
*ctx
)
56 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
58 for (i
= 0; i
< tnl
->nr_blocks
; i
++)
59 _mesa_free(tnl
->block
[i
]);
64 /* Convert the incoming array to GLfloats. Understands the
65 * array->Normalized flag and selects the correct conversion method.
67 #define CONVERT( TYPE, MACRO ) do { \
69 if (input->Normalized) { \
70 for (i = 0; i < count; i++) { \
71 const TYPE *in = (TYPE *)ptr; \
72 for (j = 0; j < sz; j++) { \
73 *fptr++ = MACRO(*in); \
76 ptr += input->StrideB; \
79 for (i = 0; i < count; i++) { \
80 const TYPE *in = (TYPE *)ptr; \
81 for (j = 0; j < sz; j++) { \
82 *fptr++ = (GLfloat)(*in); \
85 ptr += input->StrideB; \
92 /* Adjust pointer to point at first requested element, convert to
93 * floating point, populate VB->AttribPtr[].
95 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 const GLuint count
= end
- start
;
105 GLuint stride
= input
->StrideB
;
107 ptr
+= start
* stride
;
109 if (input
->Type
!= GL_FLOAT
) {
110 const GLuint sz
= input
->Size
;
111 GLfloat
*fptr
= get_space(ctx
, count
* sz
* sizeof(GLfloat
));
113 switch (input
->Type
) {
115 CONVERT(GLbyte
, BYTE_TO_FLOAT
);
117 case GL_UNSIGNED_BYTE
:
118 CONVERT(GLubyte
, UBYTE_TO_FLOAT
);
121 CONVERT(GLshort
, SHORT_TO_FLOAT
);
123 case GL_UNSIGNED_SHORT
:
124 CONVERT(GLushort
, USHORT_TO_FLOAT
);
127 CONVERT(GLint
, INT_TO_FLOAT
);
129 case GL_UNSIGNED_INT
:
130 CONVERT(GLuint
, UINT_TO_FLOAT
);
133 CONVERT(GLdouble
, (GLfloat
));
140 ptr
= (const char *)fptr
;
141 stride
= sz
* sizeof(GLfloat
);
144 VB
->AttribPtr
[attrib
] = &tnl
->tmp_inputs
[attrib
];
145 VB
->AttribPtr
[attrib
]->data
= (GLfloat (*)[4])ptr
;
146 VB
->AttribPtr
[attrib
]->start
= (GLfloat
*)ptr
;
147 VB
->AttribPtr
[attrib
]->count
= count
;
148 VB
->AttribPtr
[attrib
]->stride
= stride
;
149 VB
->AttribPtr
[attrib
]->size
= input
->Size
;
151 /* This should die, but so should the whole GLvector4f concept:
153 VB
->AttribPtr
[attrib
]->flags
= (((1<<input
->Size
)-1) |
155 (stride
== 4*sizeof(GLfloat
) ? 0 : VEC_BAD_STRIDE
));
157 VB
->AttribPtr
[attrib
]->storage
= NULL
;
160 #define CLIPVERTS ((6 + MAX_CLIP_PLANES) * 2)
163 static GLboolean
*_tnl_import_edgeflag( GLcontext
*ctx
,
164 const GLvector4f
*input
,
167 const GLubyte
*ptr
= (const GLubyte
*)input
->data
;
168 const GLuint stride
= input
->stride
;
169 GLboolean
*space
= (GLboolean
*)get_space(ctx
, count
+ CLIPVERTS
);
170 GLboolean
*bptr
= space
;
173 for (i
= 0; i
< count
; i
++) {
174 *bptr
++ = ((GLfloat
*)ptr
)[0] == 1.0;
182 static void bind_inputs( GLcontext
*ctx
,
183 const struct gl_client_array
*inputs
[],
184 GLint start
, GLint end
,
185 struct gl_buffer_object
**bo
,
188 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
189 struct vertex_buffer
*VB
= &tnl
->vb
;
194 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
197 if (inputs
[i
]->BufferObj
->Name
) {
198 if (!inputs
[i
]->BufferObj
->Pointer
) {
199 bo
[*nr_bo
] = inputs
[i
]->BufferObj
;
201 ctx
->Driver
.MapBuffer(ctx
,
204 inputs
[i
]->BufferObj
);
206 assert(inputs
[i
]->BufferObj
->Pointer
);
209 ptr
= ADD_POINTERS(inputs
[i
]->BufferObj
->Pointer
,
213 ptr
= inputs
[i
]->Ptr
;
215 /* Just make sure the array is floating point, otherwise convert to
216 * temporary storage. Rebase arrays so that 'start' becomes
219 * XXX: remove the GLvector4f type at some stage and just use
222 _tnl_import_array(ctx
, i
, start
, end
, inputs
[i
], ptr
);
225 /* Legacy pointers -- remove one day.
227 VB
->ObjPtr
= VB
->AttribPtr
[_TNL_ATTRIB_POS
];
228 VB
->NormalPtr
= VB
->AttribPtr
[_TNL_ATTRIB_NORMAL
];
229 VB
->ColorPtr
[0] = VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
];
230 VB
->ColorPtr
[1] = NULL
;
231 VB
->IndexPtr
[0] = VB
->AttribPtr
[_TNL_ATTRIB_COLOR_INDEX
];
232 VB
->IndexPtr
[1] = NULL
;
233 VB
->SecondaryColorPtr
[0] = VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
];
234 VB
->SecondaryColorPtr
[1] = NULL
;
235 VB
->FogCoordPtr
= VB
->AttribPtr
[_TNL_ATTRIB_FOG
];
237 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
238 VB
->TexCoordPtr
[i
] = VB
->AttribPtr
[_TNL_ATTRIB_TEX0
+ i
];
241 /* Clipping and drawing code still requires this to be a packed
242 * array of ubytes which can be written into. TODO: Fix and
245 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
246 ctx
->Polygon
.BackMode
!= GL_FILL
)
248 VB
->EdgeFlag
= _tnl_import_edgeflag( ctx
,
249 VB
->AttribPtr
[_TNL_ATTRIB_EDGEFLAG
],
256 /* Translate indices to GLuints and store in VB->Elts.
258 static void bind_indicies( GLcontext
*ctx
,
259 const struct _mesa_index_buffer
*ib
,
260 struct gl_buffer_object
**bo
,
263 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
264 struct vertex_buffer
*VB
= &tnl
->vb
;
269 if (ib
->obj
->Name
&& !ib
->obj
->Pointer
) {
270 bo
[*nr_bo
] = ib
->obj
;
272 ctx
->Driver
.MapBuffer(ctx
,
273 GL_ELEMENT_ARRAY_BUFFER
,
277 assert(ib
->obj
->Pointer
);
280 VB
->Elts
= (GLuint
*)ADD_POINTERS(ib
->obj
->Pointer
,
283 VB
->Elts
+= ib
->rebase
;
286 case GL_UNSIGNED_INT
:
288 case GL_UNSIGNED_SHORT
:
290 case GL_UNSIGNED_BYTE
:
295 static void unmap_vbos( GLcontext
*ctx
,
296 struct gl_buffer_object
**bo
,
300 for (i
= 0; i
< nr_bo
; i
++) {
301 ctx
->Driver
.UnmapBuffer(ctx
,
302 0, /* target -- I don't see why this would be needed */
309 /* This is the main entrypoint into the slimmed-down software tnl
310 * module. In a regular swtnl driver, this can be plugged straight
311 * into the vbo->Driver.DrawPrims() callback.
313 void _tnl_draw_prims( GLcontext
*ctx
,
314 const struct gl_client_array
*arrays
[],
315 const struct _mesa_prim
*prim
,
317 const struct _mesa_index_buffer
*ib
,
321 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
322 struct vertex_buffer
*VB
= &tnl
->vb
;
324 /* May need to map a vertex buffer object for every attribute plus
325 * one for the index buffer.
327 struct gl_buffer_object
*bo
[VERT_ATTRIB_MAX
+ 1];
330 /* Binding inputs may imply mapping some vertex buffer objects.
331 * They will need to be unmapped below.
333 bind_inputs(ctx
, arrays
, min_index
, max_index
, bo
, &nr_bo
);
334 bind_indicies(ctx
, ib
, bo
, &nr_bo
);
336 VB
->Primitive
= prim
;
337 VB
->PrimitiveCount
= nr_prims
;
338 VB
->Count
= max_index
- min_index
;
340 TNL_CONTEXT(ctx
)->Driver
.RunPipeline(ctx
);
342 unmap_vbos(ctx
, bo
, nr_bo
);