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