2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2005 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>
28 #include "main/mtypes.h"
29 #include "main/bufferobj.h"
30 #include "math/m_eval.h"
32 #include "vbo_context.h"
36 check_size(const GLfloat
*attr
)
49 * Helper for initializing a vertex array.
52 init_array(struct gl_context
*ctx
, struct gl_vertex_array
*cl
,
53 unsigned size
, const void *pointer
)
55 memset(cl
, 0, sizeof(*cl
));
61 cl
->_ElementSize
= cl
->Size
* sizeof(GLfloat
);
64 _mesa_reference_buffer_object(ctx
, &cl
->BufferObj
,
65 ctx
->Shared
->NullBufferObj
);
70 * Set up the vbo->currval arrays to point at the context's current
71 * vertex attributes (with strides = 0).
74 init_legacy_currval(struct gl_context
*ctx
)
76 struct vbo_context
*vbo
= vbo_context(ctx
);
79 /* Set up a constant (StrideB == 0) array for each current
82 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
83 struct gl_vertex_array
*cl
= &vbo
->currval
[VERT_ATTRIB_FF(i
)];
86 check_size(ctx
->Current
.Attrib
[i
]),
87 ctx
->Current
.Attrib
[i
]);
93 init_generic_currval(struct gl_context
*ctx
)
95 struct vbo_context
*vbo
= vbo_context(ctx
);
98 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
99 struct gl_vertex_array
*cl
= &vbo
->currval
[VBO_ATTRIB_GENERIC0
+ i
];
101 init_array(ctx
, cl
, 1, ctx
->Current
.Attrib
[VERT_ATTRIB_GENERIC0
+ i
]);
107 init_mat_currval(struct gl_context
*ctx
)
109 struct vbo_context
*vbo
= vbo_context(ctx
);
112 /* Set up a constant (StrideB == 0) array for each current
115 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
116 struct gl_vertex_array
*cl
=
117 &vbo
->currval
[VBO_ATTRIB_MAT_FRONT_AMBIENT
+ i
];
120 /* Size is fixed for the material attributes, for others will
121 * be determined at runtime:
124 case MAT_ATTRIB_FRONT_SHININESS
:
125 case MAT_ATTRIB_BACK_SHININESS
:
128 case MAT_ATTRIB_FRONT_INDEXES
:
129 case MAT_ATTRIB_BACK_INDEXES
:
137 init_array(ctx
, cl
, size
, ctx
->Light
.Material
.Attrib
[i
]);
142 vbo_draw_indirect_prims(struct gl_context
*ctx
,
144 struct gl_buffer_object
*indirect_data
,
145 GLsizeiptr indirect_offset
,
148 struct gl_buffer_object
*indirect_params
,
149 GLsizeiptr indirect_params_offset
,
150 const struct _mesa_index_buffer
*ib
)
152 struct vbo_context
*vbo
= vbo_context(ctx
);
153 struct _mesa_prim
*prim
;
156 prim
= calloc(draw_count
, sizeof(*prim
));
158 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "gl%sDraw%sIndirect%s",
159 (draw_count
> 1) ? "Multi" : "",
160 ib
? "Elements" : "Arrays",
161 indirect_params
? "CountARB" : "");
166 prim
[draw_count
- 1].end
= 1;
167 for (i
= 0; i
< draw_count
; ++i
, indirect_offset
+= stride
) {
169 prim
[i
].indexed
= !!ib
;
170 prim
[i
].indirect_offset
= indirect_offset
;
171 prim
[i
].is_indirect
= 1;
175 vbo
->draw_prims(ctx
, prim
, draw_count
,
178 ctx
->DrawIndirectBuffer
);
185 _vbo_CreateContext(struct gl_context
*ctx
)
187 struct vbo_context
*vbo
= CALLOC_STRUCT(vbo_context
);
189 ctx
->vbo_context
= vbo
;
191 /* Initialize the arrayelt helper
193 if (!ctx
->aelt_context
&&
194 !_ae_create_context(ctx
)) {
198 init_legacy_currval(ctx
);
199 init_generic_currval(ctx
);
200 init_mat_currval(ctx
);
201 vbo_set_indirect_draw_func(ctx
, vbo_draw_indirect_prims
);
203 /* Build mappings from VERT_ATTRIB -> VBO_ATTRIB depending on type
204 * of vertex program active.
209 /* make sure all VBO_ATTRIB_ values can fit in an unsigned byte */
210 STATIC_ASSERT(VBO_ATTRIB_MAX
<= 255);
212 /* identity mapping */
213 for (i
= 0; i
< ARRAY_SIZE(vbo
->map_vp_none
); i
++)
214 vbo
->map_vp_none
[i
] = i
;
215 /* map material attribs to generic slots */
216 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++)
217 vbo
->map_vp_none
[VERT_ATTRIB_GENERIC(i
)]
218 = VBO_ATTRIB_MAT_FRONT_AMBIENT
+ i
;
220 for (i
= 0; i
< ARRAY_SIZE(vbo
->map_vp_arb
); i
++)
221 vbo
->map_vp_arb
[i
] = i
;
225 /* Hook our functions into exec and compile dispatch tables. These
226 * will pretty much be permanently installed, which means that the
227 * vtxfmt mechanism can be removed now.
230 if (ctx
->API
== API_OPENGL_COMPAT
)
240 _vbo_DestroyContext(struct gl_context
*ctx
)
242 struct vbo_context
*vbo
= vbo_context(ctx
);
244 if (ctx
->aelt_context
) {
245 _ae_destroy_context(ctx
);
246 ctx
->aelt_context
= NULL
;
252 for (i
= 0; i
< VBO_ATTRIB_MAX
; i
++) {
253 _mesa_reference_buffer_object(ctx
, &vbo
->currval
[i
].BufferObj
, NULL
);
256 vbo_exec_destroy(ctx
);
257 if (ctx
->API
== API_OPENGL_COMPAT
)
258 vbo_save_destroy(ctx
);
260 ctx
->vbo_context
= NULL
;
266 vbo_set_draw_func(struct gl_context
*ctx
, vbo_draw_func func
)
268 struct vbo_context
*vbo
= vbo_context(ctx
);
269 vbo
->draw_prims
= func
;
274 vbo_set_indirect_draw_func(struct gl_context
*ctx
,
275 vbo_indirect_draw_func func
)
277 struct vbo_context
*vbo
= vbo_context(ctx
);
278 vbo
->draw_indirect_prims
= func
;