2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 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>
28 #include "main/imports.h"
29 #include "main/mtypes.h"
30 #include "main/api_arrayelt.h"
31 #include "main/bufferobj.h"
32 #include "math/m_eval.h"
34 #include "vbo_context.h"
38 #define NR_LEGACY_ATTRIBS 16
39 #define NR_GENERIC_ATTRIBS 16
40 #define NR_MAT_ATTRIBS 12
43 static GLuint
check_size( const GLfloat
*attr
)
45 if (attr
[3] != 1.0) return 4;
46 if (attr
[2] != 0.0) return 3;
47 if (attr
[1] != 0.0) return 2;
52 static void init_legacy_currval(struct gl_context
*ctx
)
54 struct vbo_context
*vbo
= vbo_context(ctx
);
55 struct gl_client_array
*arrays
= vbo
->legacy_currval
;
58 memset(arrays
, 0, sizeof(*arrays
) * NR_LEGACY_ATTRIBS
);
60 /* Set up a constant (StrideB == 0) array for each current
63 for (i
= 0; i
< NR_LEGACY_ATTRIBS
; i
++) {
64 struct gl_client_array
*cl
= &arrays
[i
];
66 /* Size will have to be determined at runtime:
68 cl
->Size
= check_size(ctx
->Current
.Attrib
[i
]);
74 cl
->Ptr
= (const void *)ctx
->Current
.Attrib
[i
];
75 cl
->_ElementSize
= cl
->Size
* sizeof(GLfloat
);
76 _mesa_reference_buffer_object(ctx
, &cl
->BufferObj
,
77 ctx
->Shared
->NullBufferObj
);
82 static void init_generic_currval(struct gl_context
*ctx
)
84 struct vbo_context
*vbo
= vbo_context(ctx
);
85 struct gl_client_array
*arrays
= vbo
->generic_currval
;
88 memset(arrays
, 0, sizeof(*arrays
) * NR_GENERIC_ATTRIBS
);
90 for (i
= 0; i
< NR_GENERIC_ATTRIBS
; i
++) {
91 struct gl_client_array
*cl
= &arrays
[i
];
93 /* This will have to be determined at runtime:
98 cl
->Ptr
= (const void *)ctx
->Current
.Attrib
[VERT_ATTRIB_GENERIC0
+ i
];
102 cl
->_ElementSize
= cl
->Size
* sizeof(GLfloat
);
103 _mesa_reference_buffer_object(ctx
, &cl
->BufferObj
,
104 ctx
->Shared
->NullBufferObj
);
109 static void init_mat_currval(struct gl_context
*ctx
)
111 struct vbo_context
*vbo
= vbo_context(ctx
);
112 struct gl_client_array
*arrays
= vbo
->mat_currval
;
115 ASSERT(NR_MAT_ATTRIBS
== MAT_ATTRIB_MAX
);
117 memset(arrays
, 0, sizeof(*arrays
) * NR_MAT_ATTRIBS
);
119 /* Set up a constant (StrideB == 0) array for each current
122 for (i
= 0; i
< NR_MAT_ATTRIBS
; i
++) {
123 struct gl_client_array
*cl
= &arrays
[i
];
125 /* Size is fixed for the material attributes, for others will
126 * be determined at runtime:
128 switch (i
- VERT_ATTRIB_GENERIC0
) {
129 case MAT_ATTRIB_FRONT_SHININESS
:
130 case MAT_ATTRIB_BACK_SHININESS
:
133 case MAT_ATTRIB_FRONT_INDEXES
:
134 case MAT_ATTRIB_BACK_INDEXES
:
142 cl
->Ptr
= (const void *)ctx
->Light
.Material
.Attrib
[i
];
144 cl
->Format
= GL_RGBA
;
148 cl
->_ElementSize
= cl
->Size
* sizeof(GLfloat
);
149 _mesa_reference_buffer_object(ctx
, &cl
->BufferObj
,
150 ctx
->Shared
->NullBufferObj
);
155 GLboolean
_vbo_CreateContext( struct gl_context
*ctx
)
157 struct vbo_context
*vbo
= CALLOC_STRUCT(vbo_context
);
159 ctx
->swtnl_im
= (void *)vbo
;
161 /* Initialize the arrayelt helper
163 if (!ctx
->aelt_context
&&
164 !_ae_create_context( ctx
)) {
168 /* TODO: remove these pointers.
170 vbo
->legacy_currval
= &vbo
->currval
[VBO_ATTRIB_POS
];
171 vbo
->generic_currval
= &vbo
->currval
[VBO_ATTRIB_GENERIC0
];
172 vbo
->mat_currval
= &vbo
->currval
[VBO_ATTRIB_MAT_FRONT_AMBIENT
];
174 init_legacy_currval( ctx
);
175 init_generic_currval( ctx
);
176 init_mat_currval( ctx
);
178 /* Build mappings from VERT_ATTRIB -> VBO_ATTRIB depending on type
179 * of vertex program active.
184 /* When no vertex program, pull in the material attributes in
185 * the 16..32 generic range.
187 for (i
= 0; i
< 16; i
++)
188 vbo
->map_vp_none
[i
] = i
;
189 for (i
= 0; i
< 12; i
++)
190 vbo
->map_vp_none
[16+i
] = VBO_ATTRIB_MAT_FRONT_AMBIENT
+ i
;
191 for (i
= 0; i
< 4; i
++)
192 vbo
->map_vp_none
[28+i
] = i
;
194 for (i
= 0; i
< Elements(vbo
->map_vp_arb
); i
++)
195 vbo
->map_vp_arb
[i
] = i
;
199 /* Hook our functions into exec and compile dispatch tables. These
200 * will pretty much be permanently installed, which means that the
201 * vtxfmt mechanism can be removed now.
203 vbo_exec_init( ctx
);
204 if (ctx
->API
== API_OPENGL
)
205 vbo_save_init( ctx
);
213 void _vbo_InvalidateState( struct gl_context
*ctx
, GLuint new_state
)
215 vbo_exec_invalidate_state(ctx
, new_state
);
219 void _vbo_DestroyContext( struct gl_context
*ctx
)
221 struct vbo_context
*vbo
= vbo_context(ctx
);
223 if (ctx
->aelt_context
) {
224 _ae_destroy_context( ctx
);
225 ctx
->aelt_context
= NULL
;
231 for (i
= 0; i
< VBO_ATTRIB_MAX
; i
++) {
232 _mesa_reference_buffer_object(ctx
, &vbo
->currval
[i
].BufferObj
, NULL
);
235 vbo_exec_destroy(ctx
);
236 if (ctx
->API
== API_OPENGL
)
237 vbo_save_destroy(ctx
);
239 ctx
->swtnl_im
= NULL
;
244 void vbo_set_draw_func(struct gl_context
*ctx
, vbo_draw_func func
)
246 struct vbo_context
*vbo
= vbo_context(ctx
);
247 vbo
->draw_prims
= func
;