mesa: point size arrays
[mesa.git] / src / mesa / vbo / vbo_exec_array.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * 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
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/glheader.h"
29 #include "main/context.h"
30 #include "main/state.h"
31 #include "main/api_validate.h"
32 #include "main/api_noop.h"
33 #include "main/varray.h"
34 #include "glapi/dispatch.h"
35
36 #include "vbo_context.h"
37
38 /* Compute min and max elements for drawelements calls.
39 */
40 static void get_minmax_index( GLuint count, GLuint type,
41 const GLvoid *indices,
42 GLuint *min_index,
43 GLuint *max_index)
44 {
45 GLuint i;
46
47 switch(type) {
48 case GL_UNSIGNED_INT: {
49 const GLuint *ui_indices = (const GLuint *)indices;
50 GLuint max_ui = ui_indices[0];
51 GLuint min_ui = ui_indices[0];
52 for (i = 1; i < count; i++) {
53 if (ui_indices[i] > max_ui) max_ui = ui_indices[i];
54 if (ui_indices[i] < min_ui) min_ui = ui_indices[i];
55 }
56 *min_index = min_ui;
57 *max_index = max_ui;
58 break;
59 }
60 case GL_UNSIGNED_SHORT: {
61 const GLushort *us_indices = (const GLushort *)indices;
62 GLuint max_us = us_indices[0];
63 GLuint min_us = us_indices[0];
64 for (i = 1; i < count; i++) {
65 if (us_indices[i] > max_us) max_us = us_indices[i];
66 if (us_indices[i] < min_us) min_us = us_indices[i];
67 }
68 *min_index = min_us;
69 *max_index = max_us;
70 break;
71 }
72 case GL_UNSIGNED_BYTE: {
73 const GLubyte *ub_indices = (const GLubyte *)indices;
74 GLuint max_ub = ub_indices[0];
75 GLuint min_ub = ub_indices[0];
76 for (i = 1; i < count; i++) {
77 if (ub_indices[i] > max_ub) max_ub = ub_indices[i];
78 if (ub_indices[i] < min_ub) min_ub = ub_indices[i];
79 }
80 *min_index = min_ub;
81 *max_index = max_ub;
82 break;
83 }
84 default:
85 assert(0);
86 break;
87 }
88 }
89
90
91 /* Just translate the arrayobj into a sane layout.
92 */
93 static void bind_array_obj( GLcontext *ctx )
94 {
95 struct vbo_context *vbo = vbo_context(ctx);
96 struct vbo_exec_context *exec = &vbo->exec;
97 GLuint i;
98
99 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
100 * rather than as individual named arrays. Then this function can
101 * go away.
102 */
103 exec->array.legacy_array[VERT_ATTRIB_POS] = &ctx->Array.ArrayObj->Vertex;
104 exec->array.legacy_array[VERT_ATTRIB_WEIGHT] = &vbo->legacy_currval[VERT_ATTRIB_WEIGHT];
105 exec->array.legacy_array[VERT_ATTRIB_NORMAL] = &ctx->Array.ArrayObj->Normal;
106 exec->array.legacy_array[VERT_ATTRIB_COLOR0] = &ctx->Array.ArrayObj->Color;
107 exec->array.legacy_array[VERT_ATTRIB_COLOR1] = &ctx->Array.ArrayObj->SecondaryColor;
108 exec->array.legacy_array[VERT_ATTRIB_FOG] = &ctx->Array.ArrayObj->FogCoord;
109 exec->array.legacy_array[VERT_ATTRIB_COLOR_INDEX] = &ctx->Array.ArrayObj->Index;
110 if (ctx->Array.ArrayObj->PointSize.Enabled) {
111 /* this aliases COLOR_INDEX */
112 exec->array.legacy_array[VERT_ATTRIB_POINT_SIZE] = &ctx->Array.ArrayObj->PointSize;
113 }
114 exec->array.legacy_array[VERT_ATTRIB_EDGEFLAG] = &ctx->Array.ArrayObj->EdgeFlag;
115
116 for (i = 0; i < 8; i++)
117 exec->array.legacy_array[VERT_ATTRIB_TEX0 + i] = &ctx->Array.ArrayObj->TexCoord[i];
118
119 for (i = 0; i < VERT_ATTRIB_MAX; i++)
120 exec->array.generic_array[i] = &ctx->Array.ArrayObj->VertexAttrib[i];
121
122 exec->array.array_obj = ctx->Array.ArrayObj->Name;
123 }
124
125 static void recalculate_input_bindings( GLcontext *ctx )
126 {
127 struct vbo_context *vbo = vbo_context(ctx);
128 struct vbo_exec_context *exec = &vbo->exec;
129 const struct gl_client_array **inputs = &exec->array.inputs[0];
130 GLuint i;
131
132 exec->array.program_mode = get_program_mode(ctx);
133 exec->array.enabled_flags = ctx->Array.ArrayObj->_Enabled;
134
135 switch (exec->array.program_mode) {
136 case VP_NONE:
137 /* When no vertex program is active, we put the material values
138 * into the generic slots. This is the only situation where
139 * material values are available as per-vertex attributes.
140 */
141 for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
142 if (exec->array.legacy_array[i]->Enabled)
143 inputs[i] = exec->array.legacy_array[i];
144 else
145 inputs[i] = &vbo->legacy_currval[i];
146 }
147
148 for (i = 0; i < MAT_ATTRIB_MAX; i++) {
149 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->mat_currval[i];
150 }
151
152 /* Could use just about anything, just to fill in the empty
153 * slots:
154 */
155 for (i = MAT_ATTRIB_MAX; i < VERT_ATTRIB_MAX - VERT_ATTRIB_GENERIC0; i++)
156 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i];
157
158 break;
159 case VP_NV:
160 /* NV_vertex_program - attribute arrays alias and override
161 * conventional, legacy arrays. No materials, and the generic
162 * slots are vacant.
163 */
164 for (i = 0; i <= VERT_ATTRIB_TEX7; i++) {
165 if (exec->array.generic_array[i]->Enabled)
166 inputs[i] = exec->array.generic_array[i];
167 else if (exec->array.legacy_array[i]->Enabled)
168 inputs[i] = exec->array.legacy_array[i];
169 else
170 inputs[i] = &vbo->legacy_currval[i];
171 }
172
173 /* Could use just about anything, just to fill in the empty
174 * slots:
175 */
176 for (i = VERT_ATTRIB_GENERIC0; i < VERT_ATTRIB_MAX; i++)
177 inputs[i] = &vbo->generic_currval[i - VERT_ATTRIB_GENERIC0];
178
179 break;
180 case VP_ARB:
181 /* ARB_vertex_program - Only the attribute zero (position) array
182 * aliases and overrides the legacy position array.
183 *
184 * Otherwise, legacy attributes available in the legacy slots,
185 * generic attributes in the generic slots and materials are not
186 * available as per-vertex attributes.
187 */
188 if (exec->array.generic_array[0]->Enabled)
189 inputs[0] = exec->array.generic_array[0];
190 else if (exec->array.legacy_array[0]->Enabled)
191 inputs[0] = exec->array.legacy_array[0];
192 else
193 inputs[0] = &vbo->legacy_currval[0];
194
195
196 for (i = 1; i <= VERT_ATTRIB_TEX7; i++) {
197 if (exec->array.legacy_array[i]->Enabled)
198 inputs[i] = exec->array.legacy_array[i];
199 else
200 inputs[i] = &vbo->legacy_currval[i];
201 }
202
203 for (i = 0; i < 16; i++) {
204 if (exec->array.generic_array[i]->Enabled)
205 inputs[VERT_ATTRIB_GENERIC0 + i] = exec->array.generic_array[i];
206 else
207 inputs[VERT_ATTRIB_GENERIC0 + i] = &vbo->generic_currval[i];
208 }
209 break;
210 }
211 }
212
213 static void bind_arrays( GLcontext *ctx )
214 {
215 #if 0
216 if (ctx->Array.ArrayObj.Name != exec->array.array_obj) {
217 bind_array_obj(ctx);
218 recalculate_input_bindings(ctx);
219 }
220 else if (exec->array.program_mode != get_program_mode(ctx) ||
221 exec->array.enabled_flags != ctx->Array.ArrayObj->_Enabled) {
222
223 recalculate_input_bindings(ctx);
224 }
225 #else
226 bind_array_obj(ctx);
227 recalculate_input_bindings(ctx);
228 #endif
229 }
230
231
232
233 /***********************************************************************
234 * API functions.
235 */
236
237 static void GLAPIENTRY
238 vbo_exec_DrawArrays(GLenum mode, GLint start, GLsizei count)
239 {
240 GET_CURRENT_CONTEXT(ctx);
241 struct vbo_context *vbo = vbo_context(ctx);
242 struct vbo_exec_context *exec = &vbo->exec;
243 struct _mesa_prim prim[1];
244
245 if (!_mesa_validate_DrawArrays( ctx, mode, start, count ))
246 return;
247
248 FLUSH_CURRENT( ctx, 0 );
249
250 if (ctx->NewState)
251 _mesa_update_state( ctx );
252
253 if (!vbo_validate_shaders(ctx)) {
254 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawArrays(bad shader)");
255 return;
256 }
257
258 bind_arrays( ctx );
259
260 prim[0].begin = 1;
261 prim[0].end = 1;
262 prim[0].weak = 0;
263 prim[0].pad = 0;
264 prim[0].mode = mode;
265 prim[0].start = start;
266 prim[0].count = count;
267 prim[0].indexed = 0;
268
269 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, NULL, start, start + count - 1 );
270 }
271
272
273
274 static void GLAPIENTRY
275 vbo_exec_DrawRangeElements(GLenum mode,
276 GLuint start, GLuint end,
277 GLsizei count, GLenum type, const GLvoid *indices)
278 {
279 GET_CURRENT_CONTEXT(ctx);
280 struct vbo_context *vbo = vbo_context(ctx);
281 struct vbo_exec_context *exec = &vbo->exec;
282 struct _mesa_index_buffer ib;
283 struct _mesa_prim prim[1];
284
285 if (!_mesa_validate_DrawRangeElements( ctx, mode, start, end, count, type, indices ))
286 return;
287
288 FLUSH_CURRENT( ctx, 0 );
289
290 if (ctx->NewState)
291 _mesa_update_state( ctx );
292
293 if (!vbo_validate_shaders(ctx)) {
294 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawRangeElements(bad shader)");
295 return;
296 }
297
298 bind_arrays( ctx );
299
300 ib.count = count;
301 ib.type = type;
302 ib.obj = ctx->Array.ElementArrayBufferObj;
303 ib.ptr = indices;
304
305 prim[0].begin = 1;
306 prim[0].end = 1;
307 prim[0].weak = 0;
308 prim[0].pad = 0;
309 prim[0].mode = mode;
310 prim[0].start = 0;
311 prim[0].count = count;
312 prim[0].indexed = 1;
313
314 /* Need to give special consideration to rendering a range of
315 * indices starting somewhere above zero. Typically the
316 * application is issuing multiple DrawRangeElements() to draw
317 * successive primitives layed out linearly in the vertex arrays.
318 * Unless the vertex arrays are all in a VBO (or locked as with
319 * CVA), the OpenGL semantics imply that we need to re-read or
320 * re-upload the vertex data on each draw call.
321 *
322 * In the case of hardware tnl, we want to avoid starting the
323 * upload at zero, as it will mean every draw call uploads an
324 * increasing amount of not-used vertex data. Worse - in the
325 * software tnl module, all those vertices might be transformed and
326 * lit but never rendered.
327 *
328 * If we just upload or transform the vertices in start..end,
329 * however, the indices will be incorrect.
330 *
331 * At this level, we don't know exactly what the requirements of
332 * the backend are going to be, though it will likely boil down to
333 * either:
334 *
335 * 1) Do nothing, everything is in a VBO and is processed once
336 * only.
337 *
338 * 2) Adjust the indices and vertex arrays so that start becomes
339 * zero.
340 *
341 * Rather than doing anything here, I'll provide a helper function
342 * for the latter case elsewhere.
343 */
344
345 vbo->draw_prims( ctx, exec->array.inputs, prim, 1, &ib, start, end );
346 }
347
348 static void GLAPIENTRY
349 vbo_exec_DrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
350 {
351 GET_CURRENT_CONTEXT(ctx);
352 GLuint min_index = 0;
353 GLuint max_index = 0;
354
355 if (!_mesa_validate_DrawElements( ctx, mode, count, type, indices ))
356 return;
357
358 if (!vbo_validate_shaders(ctx)) {
359 _mesa_error(ctx, GL_INVALID_OPERATION, "glDrawElements(bad shader)");
360 return;
361 }
362
363 if (ctx->Array.ElementArrayBufferObj->Name) {
364 const GLvoid *map = ctx->Driver.MapBuffer(ctx,
365 GL_ELEMENT_ARRAY_BUFFER_ARB,
366 GL_READ_ONLY,
367 ctx->Array.ElementArrayBufferObj);
368
369 get_minmax_index(count, type, ADD_POINTERS(map, indices), &min_index, &max_index);
370
371 ctx->Driver.UnmapBuffer(ctx,
372 GL_ELEMENT_ARRAY_BUFFER_ARB,
373 ctx->Array.ElementArrayBufferObj);
374 }
375 else {
376 get_minmax_index(count, type, indices, &min_index, &max_index);
377 }
378
379 vbo_exec_DrawRangeElements(mode, min_index, max_index, count, type, indices);
380 }
381
382
383 /***********************************************************************
384 * Initialization
385 */
386
387
388
389
390 void vbo_exec_array_init( struct vbo_exec_context *exec )
391 {
392 #if 1
393 exec->vtxfmt.DrawArrays = vbo_exec_DrawArrays;
394 exec->vtxfmt.DrawElements = vbo_exec_DrawElements;
395 exec->vtxfmt.DrawRangeElements = vbo_exec_DrawRangeElements;
396 #else
397 exec->vtxfmt.DrawArrays = _mesa_noop_DrawArrays;
398 exec->vtxfmt.DrawElements = _mesa_noop_DrawElements;
399 exec->vtxfmt.DrawRangeElements = _mesa_noop_DrawRangeElements;
400 #endif
401 }
402
403
404 void vbo_exec_array_destroy( struct vbo_exec_context *exec )
405 {
406 /* nothing to do */
407 }
408
409
410 /* This API entrypoint is not ordinarily used */
411 void GLAPIENTRY
412 _mesa_DrawArrays(GLenum mode, GLint first, GLsizei count)
413 {
414 vbo_exec_DrawArrays(mode, first, count);
415 }
416
417
418 /* This API entrypoint is not ordinarily used */
419 void GLAPIENTRY
420 _mesa_DrawElements(GLenum mode, GLsizei count, GLenum type,
421 const GLvoid *indices)
422 {
423 vbo_exec_DrawElements(mode, count, type, indices);
424 }
425
426
427 /* This API entrypoint is not ordinarily used */
428 void GLAPIENTRY
429 _mesa_DrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count,
430 GLenum type, const GLvoid *indices)
431 {
432 vbo_exec_DrawRangeElements(mode, start, end, count, type, indices);
433 }