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/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"
37 static GLuint
check_size( const GLfloat
*attr
)
39 if (attr
[3] != 1.0F
) return 4;
40 if (attr
[2] != 0.0F
) return 3;
41 if (attr
[1] != 0.0F
) return 2;
47 * Helper for initializing a vertex array.
50 init_array(struct gl_context
*ctx
, struct gl_client_array
*cl
,
51 unsigned size
, const void *pointer
)
53 memset(cl
, 0, sizeof(*cl
));
60 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).
73 static void init_legacy_currval(struct gl_context
*ctx
)
75 struct vbo_context
*vbo
= vbo_context(ctx
);
78 /* Set up a constant (StrideB == 0) array for each current
81 for (i
= 0; i
< VERT_ATTRIB_FF_MAX
; i
++) {
82 struct gl_client_array
*cl
= &vbo
->currval
[VERT_ATTRIB_FF(i
)];
85 check_size(ctx
->Current
.Attrib
[i
]),
86 ctx
->Current
.Attrib
[i
]);
91 static void init_generic_currval(struct gl_context
*ctx
)
93 struct vbo_context
*vbo
= vbo_context(ctx
);
96 for (i
= 0; i
< VERT_ATTRIB_GENERIC_MAX
; i
++) {
97 struct gl_client_array
*cl
= &vbo
->currval
[VBO_ATTRIB_GENERIC0
+ i
];
99 init_array(ctx
, cl
, 1, ctx
->Current
.Attrib
[VERT_ATTRIB_GENERIC0
+ i
]);
104 static void init_mat_currval(struct gl_context
*ctx
)
106 struct vbo_context
*vbo
= vbo_context(ctx
);
109 /* Set up a constant (StrideB == 0) array for each current
112 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
113 struct gl_client_array
*cl
=
114 &vbo
->currval
[VBO_ATTRIB_MAT_FRONT_AMBIENT
+ i
];
117 /* Size is fixed for the material attributes, for others will
118 * be determined at runtime:
121 case MAT_ATTRIB_FRONT_SHININESS
:
122 case MAT_ATTRIB_BACK_SHININESS
:
125 case MAT_ATTRIB_FRONT_INDEXES
:
126 case MAT_ATTRIB_BACK_INDEXES
:
134 init_array(ctx
, cl
, size
, ctx
->Light
.Material
.Attrib
[i
]);
139 vbo_draw_indirect_prims(struct gl_context
*ctx
,
141 struct gl_buffer_object
*indirect_data
,
142 GLsizeiptr indirect_offset
,
145 struct gl_buffer_object
*indirect_params
,
146 GLsizeiptr indirect_params_offset
,
147 const struct _mesa_index_buffer
*ib
)
149 struct vbo_context
*vbo
= vbo_context(ctx
);
150 struct _mesa_prim
*prim
;
153 prim
= calloc(draw_count
, sizeof(*prim
));
155 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "gl%sDraw%sIndirect%s",
156 (draw_count
> 1) ? "Multi" : "",
157 ib
? "Elements" : "Arrays",
158 indirect_params
? "CountARB" : "");
163 prim
[draw_count
- 1].end
= 1;
164 for (i
= 0; i
< draw_count
; ++i
, indirect_offset
+= stride
) {
166 prim
[i
].indexed
= !!ib
;
167 prim
[i
].indirect_offset
= indirect_offset
;
168 prim
[i
].is_indirect
= 1;
172 vbo
->draw_prims(ctx
, prim
, draw_count
,
175 ctx
->DrawIndirectBuffer
);
181 GLboolean
_vbo_CreateContext( struct gl_context
*ctx
)
183 struct vbo_context
*vbo
= CALLOC_STRUCT(vbo_context
);
185 ctx
->vbo_context
= vbo
;
187 /* Initialize the arrayelt helper
189 if (!ctx
->aelt_context
&&
190 !_ae_create_context( ctx
)) {
194 init_legacy_currval( ctx
);
195 init_generic_currval( ctx
);
196 init_mat_currval( ctx
);
197 vbo_set_indirect_draw_func(ctx
, vbo_draw_indirect_prims
);
199 /* Build mappings from VERT_ATTRIB -> VBO_ATTRIB depending on type
200 * of vertex program active.
205 /* identity mapping */
206 for (i
= 0; i
< ARRAY_SIZE(vbo
->map_vp_none
); i
++)
207 vbo
->map_vp_none
[i
] = i
;
208 /* map material attribs to generic slots */
209 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++)
210 vbo
->map_vp_none
[VERT_ATTRIB_GENERIC(i
)]
211 = VBO_ATTRIB_MAT_FRONT_AMBIENT
+ i
;
213 for (i
= 0; i
< ARRAY_SIZE(vbo
->map_vp_arb
); i
++)
214 vbo
->map_vp_arb
[i
] = i
;
218 /* Hook our functions into exec and compile dispatch tables. These
219 * will pretty much be permanently installed, which means that the
220 * vtxfmt mechanism can be removed now.
222 vbo_exec_init( ctx
);
223 if (ctx
->API
== API_OPENGL_COMPAT
)
224 vbo_save_init( ctx
);
232 void _vbo_InvalidateState( struct gl_context
*ctx
, GLbitfield new_state
)
234 vbo_exec_invalidate_state(ctx
, new_state
);
238 void _vbo_DestroyContext( struct gl_context
*ctx
)
240 struct vbo_context
*vbo
= vbo_context(ctx
);
242 if (ctx
->aelt_context
) {
243 _ae_destroy_context( ctx
);
244 ctx
->aelt_context
= NULL
;
250 for (i
= 0; i
< VBO_ATTRIB_MAX
; i
++) {
251 _mesa_reference_buffer_object(ctx
, &vbo
->currval
[i
].BufferObj
, NULL
);
254 vbo_exec_destroy(ctx
);
255 if (ctx
->API
== API_OPENGL_COMPAT
)
256 vbo_save_destroy(ctx
);
258 ctx
->vbo_context
= NULL
;
263 void vbo_set_draw_func(struct gl_context
*ctx
, vbo_draw_func func
)
265 struct vbo_context
*vbo
= vbo_context(ctx
);
266 vbo
->draw_prims
= func
;
270 void vbo_set_indirect_draw_func(struct gl_context
*ctx
,
271 vbo_indirect_draw_func func
)
273 struct vbo_context
*vbo
= vbo_context(ctx
);
274 vbo
->draw_indirect_prims
= func
;