vbo: whitespace, formatting fixes in vbo_context.c
[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/imports.h"
29 #include "main/mtypes.h"
30 #include "main/api_arrayelt.h"
31 #include "main/bufferobj.h"
32 #include "math/m_eval.h"
33 #include "vbo.h"
34 #include "vbo_context.h"
35
36
37 static GLuint
38 check_size(const GLfloat *attr)
39 {
40 if (attr[3] != 1.0F)
41 return 4;
42 if (attr[2] != 0.0F)
43 return 3;
44 if (attr[1] != 0.0F)
45 return 2;
46 return 1;
47 }
48
49
50 /**
51 * Helper for initializing a vertex array.
52 */
53 static void
54 init_array(struct gl_context *ctx, struct gl_vertex_array *cl,
55 unsigned size, const void *pointer)
56 {
57 memset(cl, 0, sizeof(*cl));
58
59 cl->Size = size;
60 cl->Type = GL_FLOAT;
61 cl->Format = GL_RGBA;
62 cl->StrideB = 0;
63 cl->_ElementSize = cl->Size * sizeof(GLfloat);
64 cl->Ptr = pointer;
65
66 _mesa_reference_buffer_object(ctx, &cl->BufferObj,
67 ctx->Shared->NullBufferObj);
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 (StrideB == 0) array for each current
82 * attribute:
83 */
84 for (i = 0; i < VERT_ATTRIB_FF_MAX; i++) {
85 struct gl_vertex_array *cl = &vbo->currval[VERT_ATTRIB_FF(i)];
86
87 init_array(ctx, cl,
88 check_size(ctx->Current.Attrib[i]),
89 ctx->Current.Attrib[i]);
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 struct gl_vertex_array *cl = &vbo->currval[VBO_ATTRIB_GENERIC0 + i];
102
103 init_array(ctx, cl, 1, ctx->Current.Attrib[VERT_ATTRIB_GENERIC0 + i]);
104 }
105 }
106
107
108 static void
109 init_mat_currval(struct gl_context *ctx)
110 {
111 struct vbo_context *vbo = vbo_context(ctx);
112 GLuint i;
113
114 /* Set up a constant (StrideB == 0) array for each current
115 * attribute:
116 */
117 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
118 struct gl_vertex_array *cl =
119 &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT + i];
120 unsigned size;
121
122 /* Size is fixed for the material attributes, for others will
123 * be determined at runtime:
124 */
125 switch (i) {
126 case MAT_ATTRIB_FRONT_SHININESS:
127 case MAT_ATTRIB_BACK_SHININESS:
128 size = 1;
129 break;
130 case MAT_ATTRIB_FRONT_INDEXES:
131 case MAT_ATTRIB_BACK_INDEXES:
132 size = 3;
133 break;
134 default:
135 size = 4;
136 break;
137 }
138
139 init_array(ctx, cl, size, ctx->Light.Material.Attrib[i]);
140 }
141 }
142
143 static void
144 vbo_draw_indirect_prims(struct gl_context *ctx,
145 GLuint mode,
146 struct gl_buffer_object *indirect_data,
147 GLsizeiptr indirect_offset,
148 unsigned draw_count,
149 unsigned stride,
150 struct gl_buffer_object *indirect_params,
151 GLsizeiptr indirect_params_offset,
152 const struct _mesa_index_buffer *ib)
153 {
154 struct vbo_context *vbo = vbo_context(ctx);
155 struct _mesa_prim *prim;
156 GLsizei i;
157
158 prim = calloc(draw_count, sizeof(*prim));
159 if (prim == NULL) {
160 _mesa_error(ctx, GL_OUT_OF_MEMORY, "gl%sDraw%sIndirect%s",
161 (draw_count > 1) ? "Multi" : "",
162 ib ? "Elements" : "Arrays",
163 indirect_params ? "CountARB" : "");
164 return;
165 }
166
167 prim[0].begin = 1;
168 prim[draw_count - 1].end = 1;
169 for (i = 0; i < draw_count; ++i, indirect_offset += stride) {
170 prim[i].mode = mode;
171 prim[i].indexed = !!ib;
172 prim[i].indirect_offset = indirect_offset;
173 prim[i].is_indirect = 1;
174 prim[i].draw_id = i;
175 }
176
177 vbo->draw_prims(ctx, prim, draw_count,
178 ib, false, 0, ~0,
179 NULL, 0,
180 ctx->DrawIndirectBuffer);
181
182 free(prim);
183 }
184
185
186 GLboolean
187 _vbo_CreateContext(struct gl_context *ctx)
188 {
189 struct vbo_context *vbo = CALLOC_STRUCT(vbo_context);
190
191 ctx->vbo_context = vbo;
192
193 /* Initialize the arrayelt helper
194 */
195 if (!ctx->aelt_context &&
196 !_ae_create_context(ctx)) {
197 return GL_FALSE;
198 }
199
200 init_legacy_currval(ctx);
201 init_generic_currval(ctx);
202 init_mat_currval(ctx);
203 vbo_set_indirect_draw_func(ctx, vbo_draw_indirect_prims);
204
205 /* Build mappings from VERT_ATTRIB -> VBO_ATTRIB depending on type
206 * of vertex program active.
207 */
208 {
209 GLuint i;
210
211 /* make sure all VBO_ATTRIB_ values can fit in an unsigned byte */
212 STATIC_ASSERT(VBO_ATTRIB_MAX <= 255);
213
214 /* identity mapping */
215 for (i = 0; i < ARRAY_SIZE(vbo->map_vp_none); i++)
216 vbo->map_vp_none[i] = i;
217 /* map material attribs to generic slots */
218 for (i = 0; i < MAT_ATTRIB_MAX; i++)
219 vbo->map_vp_none[VERT_ATTRIB_GENERIC(i)]
220 = VBO_ATTRIB_MAT_FRONT_AMBIENT + i;
221
222 for (i = 0; i < ARRAY_SIZE(vbo->map_vp_arb); i++)
223 vbo->map_vp_arb[i] = i;
224 }
225
226
227 /* Hook our functions into exec and compile dispatch tables. These
228 * will pretty much be permanently installed, which means that the
229 * vtxfmt mechanism can be removed now.
230 */
231 vbo_exec_init(ctx);
232 if (ctx->API == API_OPENGL_COMPAT)
233 vbo_save_init(ctx);
234
235 _math_init_eval();
236
237 return GL_TRUE;
238 }
239
240
241 void
242 _vbo_DestroyContext(struct gl_context *ctx)
243 {
244 struct vbo_context *vbo = vbo_context(ctx);
245
246 if (ctx->aelt_context) {
247 _ae_destroy_context(ctx);
248 ctx->aelt_context = NULL;
249 }
250
251 if (vbo) {
252 GLuint i;
253
254 for (i = 0; i < VBO_ATTRIB_MAX; i++) {
255 _mesa_reference_buffer_object(ctx, &vbo->currval[i].BufferObj, NULL);
256 }
257
258 vbo_exec_destroy(ctx);
259 if (ctx->API == API_OPENGL_COMPAT)
260 vbo_save_destroy(ctx);
261 free(vbo);
262 ctx->vbo_context = NULL;
263 }
264 }
265
266
267 void
268 vbo_set_draw_func(struct gl_context *ctx, vbo_draw_func func)
269 {
270 struct vbo_context *vbo = vbo_context(ctx);
271 vbo->draw_prims = func;
272 }
273
274
275 void
276 vbo_set_indirect_draw_func(struct gl_context *ctx,
277 vbo_indirect_draw_func func)
278 {
279 struct vbo_context *vbo = vbo_context(ctx);
280 vbo->draw_indirect_prims = func;
281 }