vbo: Try to reuse the same VAO more often for successive dlists.
[mesa.git] / src / mesa / vbo / vbo_context.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 *
24 * Authors:
25 * Keith Whitwell <keithw@vmware.com>
26 */
27
28 #include "main/mtypes.h"
29 #include "main/bufferobj.h"
30 #include "math/m_eval.h"
31 #include "main/vtxfmt.h"
32 #include "main/api_arrayelt.h"
33 #include "main/arrayobj.h"
34 #include "main/varray.h"
35 #include "vbo.h"
36 #include "vbo_private.h"
37
38
39 static GLuint
40 check_size(const GLfloat *attr)
41 {
42 if (attr[3] != 1.0F)
43 return 4;
44 if (attr[2] != 0.0F)
45 return 3;
46 if (attr[1] != 0.0F)
47 return 2;
48 return 1;
49 }
50
51
52 /**
53 * Helper for initializing a vertex array.
54 */
55 static void
56 init_array(struct gl_context *ctx, struct gl_vertex_array *array,
57 unsigned size, const void *pointer)
58 {
59 memset(array, 0, sizeof(*array));
60
61 array->Size = size;
62 array->Type = GL_FLOAT;
63 array->Format = GL_RGBA;
64 array->StrideB = 0;
65 array->_ElementSize = array->Size * sizeof(GLfloat);
66 array->Ptr = pointer;
67
68 _mesa_reference_buffer_object(ctx, &array->BufferObj,
69 ctx->Shared->NullBufferObj);
70 }
71
72
73 /**
74 * Set up the vbo->currval arrays to point at the context's current
75 * vertex attributes (with strides = 0).
76 */
77 static void
78 init_legacy_currval(struct gl_context *ctx)
79 {
80 struct vbo_context *vbo = vbo_context(ctx);
81 GLuint i;
82
83 /* Set up a constant (StrideB == 0) array for each current
84 * attribute:
85 */
86 for (i = 0; i < VERT_ATTRIB_FF_MAX; i++) {
87 struct gl_vertex_array *array = &vbo->currval[VERT_ATTRIB_FF(i)];
88
89 init_array(ctx, array,
90 check_size(ctx->Current.Attrib[i]),
91 ctx->Current.Attrib[i]);
92 }
93 }
94
95
96 static void
97 init_generic_currval(struct gl_context *ctx)
98 {
99 struct vbo_context *vbo = vbo_context(ctx);
100 GLuint i;
101
102 for (i = 0; i < VERT_ATTRIB_GENERIC_MAX; i++) {
103 struct gl_vertex_array *array = &vbo->currval[VBO_ATTRIB_GENERIC0 + i];
104
105 init_array(ctx, array, 1, ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + i]);
106 }
107 }
108
109
110 static void
111 init_mat_currval(struct gl_context *ctx)
112 {
113 struct vbo_context *vbo = vbo_context(ctx);
114 GLuint i;
115
116 /* Set up a constant (StrideB == 0) array for each current
117 * attribute:
118 */
119 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
120 struct gl_vertex_array *array =
121 &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT + i];
122 unsigned size;
123
124 /* Size is fixed for the material attributes, for others will
125 * be determined at runtime:
126 */
127 switch (i) {
128 case MAT_ATTRIB_FRONT_SHININESS:
129 case MAT_ATTRIB_BACK_SHININESS:
130 size = 1;
131 break;
132 case MAT_ATTRIB_FRONT_INDEXES:
133 case MAT_ATTRIB_BACK_INDEXES:
134 size = 3;
135 break;
136 default:
137 size = 4;
138 break;
139 }
140
141 init_array(ctx, array, size, ctx->Light.Material.Attrib[i]);
142 }
143 }
144
145
146 /**
147 * Fallback for when a driver does not call vbo_set_indirect_draw_func().
148 */
149 static void
150 vbo_draw_indirect_prims(struct gl_context *ctx,
151 GLuint mode,
152 struct gl_buffer_object *indirect_buffer,
153 GLsizeiptr indirect_offset,
154 unsigned draw_count,
155 unsigned stride,
156 struct gl_buffer_object *indirect_draw_count_buffer,
157 GLsizeiptr indirect_draw_count_offset,
158 const struct _mesa_index_buffer *ib)
159 {
160 struct vbo_context *vbo = vbo_context(ctx);
161 struct _mesa_prim *prim;
162 GLsizei i;
163
164 prim = calloc(draw_count, sizeof(*prim));
165 if (prim == NULL) {
166 _mesa_error(ctx, GL_OUT_OF_MEMORY, "gl%sDraw%sIndirect%s",
167 (draw_count > 1) ? "Multi" : "",
168 ib ? "Elements" : "Arrays",
169 indirect_buffer ? "CountARB" : "");
170 return;
171 }
172
173 prim[0].begin = 1;
174 prim[draw_count - 1].end = 1;
175 for (i = 0; i < draw_count; ++i, indirect_offset += stride) {
176 prim[i].mode = mode;
177 prim[i].indexed = !!ib;
178 prim[i].indirect_offset = indirect_offset;
179 prim[i].is_indirect = 1;
180 prim[i].draw_id = i;
181 }
182
183 /* This should always be true at this time */
184 assert(indirect_buffer == ctx->DrawIndirectBuffer);
185
186 vbo->draw_prims(ctx, prim, draw_count,
187 ib, false, 0, ~0,
188 NULL, 0,
189 indirect_buffer);
190
191 free(prim);
192 }
193
194
195 void
196 _vbo_install_exec_vtxfmt(struct gl_context *ctx)
197 {
198 struct vbo_context *vbo = vbo_context(ctx);
199
200 _mesa_install_exec_vtxfmt(ctx, &vbo->exec.vtxfmt);
201 }
202
203
204 void
205 vbo_exec_invalidate_state(struct gl_context *ctx)
206 {
207 struct vbo_context *vbo = vbo_context(ctx);
208 struct vbo_exec_context *exec = &vbo->exec;
209
210 if (ctx->NewState & (_NEW_PROGRAM | _NEW_ARRAY)) {
211 exec->array.recalculate_inputs = GL_TRUE;
212
213 _ae_invalidate_state(ctx);
214 }
215 /* If _mesa_update_state is called in a non draw code path,
216 * changes in the VAO need to be captured.
217 */
218 if (ctx->Array.VAO->NewArrays)
219 exec->array.recalculate_inputs = GL_TRUE;
220
221 if (ctx->NewState & _NEW_EVAL)
222 exec->eval.recalculate_maps = GL_TRUE;
223 }
224
225
226 GLboolean
227 _vbo_CreateContext(struct gl_context *ctx)
228 {
229 struct vbo_context *vbo = CALLOC_STRUCT(vbo_context);
230
231 ctx->vbo_context = vbo;
232
233 /* Initialize the arrayelt helper
234 */
235 if (!ctx->aelt_context &&
236 !_ae_create_context(ctx)) {
237 return GL_FALSE;
238 }
239
240 init_legacy_currval(ctx);
241 init_generic_currval(ctx);
242 init_mat_currval(ctx);
243 _vbo_init_inputs(&vbo->draw_arrays);
244 vbo_set_indirect_draw_func(ctx, vbo_draw_indirect_prims);
245
246 /* make sure all VBO_ATTRIB_ values can fit in an unsigned byte */
247 STATIC_ASSERT(VBO_ATTRIB_MAX <= 255);
248
249 /* Hook our functions into exec and compile dispatch tables. These
250 * will pretty much be permanently installed, which means that the
251 * vtxfmt mechanism can be removed now.
252 */
253 vbo_exec_init(ctx);
254 if (ctx->API == API_OPENGL_COMPAT)
255 vbo_save_init(ctx);
256
257 vbo->VAO = _mesa_new_vao(ctx, ~((GLuint)0));
258 /* The exec VAO assumes to have all arributes bound to binding 0 */
259 for (unsigned i = 0; i < VERT_ATTRIB_MAX; ++i)
260 _mesa_vertex_attrib_binding(ctx, vbo->VAO, i, 0, false);
261
262 _math_init_eval();
263
264 return GL_TRUE;
265 }
266
267
268 void
269 _vbo_DestroyContext(struct gl_context *ctx)
270 {
271 struct vbo_context *vbo = vbo_context(ctx);
272
273 if (ctx->aelt_context) {
274 _ae_destroy_context(ctx);
275 ctx->aelt_context = NULL;
276 }
277
278 if (vbo) {
279 GLuint i;
280
281 for (i = 0; i < VBO_ATTRIB_MAX; i++) {
282 _mesa_reference_buffer_object(ctx, &vbo->currval[i].BufferObj, NULL);
283 }
284
285 vbo_exec_destroy(ctx);
286 if (ctx->API == API_OPENGL_COMPAT)
287 vbo_save_destroy(ctx);
288 _mesa_reference_vao(ctx, &vbo->VAO, NULL);
289 free(vbo);
290 ctx->vbo_context = NULL;
291 }
292 }
293
294
295 void
296 vbo_set_draw_func(struct gl_context *ctx, vbo_draw_func func)
297 {
298 struct vbo_context *vbo = vbo_context(ctx);
299 vbo->draw_prims = func;
300 }
301
302
303 void
304 vbo_set_indirect_draw_func(struct gl_context *ctx,
305 vbo_indirect_draw_func func)
306 {
307 struct vbo_context *vbo = vbo_context(ctx);
308 vbo->draw_indirect_prims = func;
309 }