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