1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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 "main/bufferobj.h"
35 #include "glapi/dispatch.h"
37 #include "vbo_context.h"
40 * Compute min and max elements for glDraw[Range]Elements() calls.
42 static void get_minmax_index( GLuint count
, GLuint type
,
43 const GLvoid
*indices
,
50 case GL_UNSIGNED_INT
: {
51 const GLuint
*ui_indices
= (const GLuint
*)indices
;
52 GLuint max_ui
= ui_indices
[count
-1];
53 GLuint min_ui
= ui_indices
[0];
54 for (i
= 0; i
< count
; i
++) {
55 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
56 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
62 case GL_UNSIGNED_SHORT
: {
63 const GLushort
*us_indices
= (const GLushort
*)indices
;
64 GLuint max_us
= us_indices
[count
-1];
65 GLuint min_us
= us_indices
[0];
66 for (i
= 0; i
< count
; i
++) {
67 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
68 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
74 case GL_UNSIGNED_BYTE
: {
75 const GLubyte
*ub_indices
= (const GLubyte
*)indices
;
76 GLuint max_ub
= ub_indices
[count
-1];
77 GLuint min_ub
= ub_indices
[0];
78 for (i
= 0; i
< count
; i
++) {
79 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
80 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
94 * Just translate the arrayobj into a sane layout.
96 static void bind_array_obj( GLcontext
*ctx
)
98 struct vbo_context
*vbo
= vbo_context(ctx
);
99 struct vbo_exec_context
*exec
= &vbo
->exec
;
100 struct gl_array_object
*arrayObj
= ctx
->Array
.ArrayObj
;
103 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
104 * rather than as individual named arrays. Then this function can
107 exec
->array
.legacy_array
[VERT_ATTRIB_POS
] = &arrayObj
->Vertex
;
108 exec
->array
.legacy_array
[VERT_ATTRIB_WEIGHT
] = &arrayObj
->Weight
;
109 exec
->array
.legacy_array
[VERT_ATTRIB_NORMAL
] = &arrayObj
->Normal
;
110 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR0
] = &arrayObj
->Color
;
111 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR1
] = &arrayObj
->SecondaryColor
;
112 exec
->array
.legacy_array
[VERT_ATTRIB_FOG
] = &arrayObj
->FogCoord
;
113 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR_INDEX
] = &arrayObj
->Index
;
114 if (arrayObj
->PointSize
.Enabled
) {
115 /* this aliases COLOR_INDEX */
116 exec
->array
.legacy_array
[VERT_ATTRIB_POINT_SIZE
] = &arrayObj
->PointSize
;
118 exec
->array
.legacy_array
[VERT_ATTRIB_EDGEFLAG
] = &arrayObj
->EdgeFlag
;
120 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++)
121 exec
->array
.legacy_array
[VERT_ATTRIB_TEX0
+ i
] = &arrayObj
->TexCoord
[i
];
123 for (i
= 0; i
< MAX_VERTEX_GENERIC_ATTRIBS
; i
++) {
124 assert(i
< Elements(arrayObj
->VertexAttrib
));
125 assert(i
< Elements(exec
->array
.generic_array
));
126 exec
->array
.generic_array
[i
] = &arrayObj
->VertexAttrib
[i
];
129 exec
->array
.array_obj
= arrayObj
->Name
;
133 static void recalculate_input_bindings( GLcontext
*ctx
)
135 struct vbo_context
*vbo
= vbo_context(ctx
);
136 struct vbo_exec_context
*exec
= &vbo
->exec
;
137 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
138 GLbitfield const_inputs
= 0x0;
141 exec
->array
.program_mode
= get_program_mode(ctx
);
142 exec
->array
.enabled_flags
= ctx
->Array
.ArrayObj
->_Enabled
;
144 switch (exec
->array
.program_mode
) {
146 /* When no vertex program is active (or the vertex program is generated
147 * from fixed-function state). We put the material values into the
148 * generic slots. This is the only situation where material values
149 * are available as per-vertex attributes.
151 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
152 if (exec
->array
.legacy_array
[i
]->Enabled
)
153 inputs
[i
] = exec
->array
.legacy_array
[i
];
155 inputs
[i
] = &vbo
->legacy_currval
[i
];
156 const_inputs
|= 1 << i
;
160 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
161 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->mat_currval
[i
];
162 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
165 /* Could use just about anything, just to fill in the empty
168 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_MAX
- VERT_ATTRIB_GENERIC0
; i
++) {
169 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
170 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
175 /* NV_vertex_program - attribute arrays alias and override
176 * conventional, legacy arrays. No materials, and the generic
179 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
180 if (exec
->array
.generic_array
[i
]->Enabled
)
181 inputs
[i
] = exec
->array
.generic_array
[i
];
182 else if (exec
->array
.legacy_array
[i
]->Enabled
)
183 inputs
[i
] = exec
->array
.legacy_array
[i
];
185 inputs
[i
] = &vbo
->legacy_currval
[i
];
186 const_inputs
|= 1 << i
;
190 /* Could use just about anything, just to fill in the empty
193 for (i
= VERT_ATTRIB_GENERIC0
; i
< VERT_ATTRIB_MAX
; i
++) {
194 inputs
[i
] = &vbo
->generic_currval
[i
- VERT_ATTRIB_GENERIC0
];
195 const_inputs
|= 1 << i
;
200 /* GL_ARB_vertex_program or GLSL vertex shader - Only the generic[0]
201 * attribute array aliases and overrides the legacy position array.
203 * Otherwise, legacy attributes available in the legacy slots,
204 * generic attributes in the generic slots and materials are not
205 * available as per-vertex attributes.
207 if (exec
->array
.generic_array
[0]->Enabled
)
208 inputs
[0] = exec
->array
.generic_array
[0];
209 else if (exec
->array
.legacy_array
[0]->Enabled
)
210 inputs
[0] = exec
->array
.legacy_array
[0];
212 inputs
[0] = &vbo
->legacy_currval
[0];
213 const_inputs
|= 1 << 0;
216 for (i
= 1; i
<= VERT_ATTRIB_TEX7
; i
++) {
217 if (exec
->array
.legacy_array
[i
]->Enabled
)
218 inputs
[i
] = exec
->array
.legacy_array
[i
];
220 inputs
[i
] = &vbo
->legacy_currval
[i
];
221 const_inputs
|= 1 << i
;
225 for (i
= 0; i
< MAX_VERTEX_GENERIC_ATTRIBS
; i
++) {
226 if (exec
->array
.generic_array
[i
]->Enabled
)
227 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = exec
->array
.generic_array
[i
];
229 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
230 const_inputs
|= 1 << (VERT_ATTRIB_GENERIC0
+ i
);
237 _mesa_set_varying_vp_inputs( ctx
, ~const_inputs
);
241 static void bind_arrays( GLcontext
*ctx
)
244 if (ctx
->Array
.ArrayObj
.Name
!= exec
->array
.array_obj
) {
246 recalculate_input_bindings(ctx
);
248 else if (exec
->array
.program_mode
!= get_program_mode(ctx
) ||
249 exec
->array
.enabled_flags
!= ctx
->Array
.ArrayObj
->_Enabled
) {
251 recalculate_input_bindings(ctx
);
255 recalculate_input_bindings(ctx
);
261 /***********************************************************************
265 static void GLAPIENTRY
266 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
268 GET_CURRENT_CONTEXT(ctx
);
269 struct vbo_context
*vbo
= vbo_context(ctx
);
270 struct vbo_exec_context
*exec
= &vbo
->exec
;
271 struct _mesa_prim prim
[1];
273 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
276 FLUSH_CURRENT( ctx
, 0 );
279 _mesa_update_state( ctx
);
281 if (!vbo_validate_shaders(ctx
)) {
282 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawArrays(bad shader)");
288 /* Again... because we may have changed the bitmask of per-vertex varying
289 * attributes. If we regenerate the fixed-function vertex program now
290 * we may be able to prune down the number of vertex attributes which we
291 * need in the shader.
294 _mesa_update_state( ctx
);
301 prim
[0].start
= start
;
302 prim
[0].count
= count
;
305 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, NULL
,
306 start
, start
+ count
- 1 );
312 _mesa_printf("vbo_exec_DrawArrays(mode 0x%x, start %d, count %d):\n",
315 for (i
= 0; i
< 32; i
++) {
316 GLuint bufName
= exec
->array
.inputs
[i
]->BufferObj
->Name
;
317 GLint stride
= exec
->array
.inputs
[i
]->Stride
;
318 _mesa_printf("attr %2d: size %d stride %d enabled %d "
319 "ptr %p Bufobj %u\n",
321 exec
->array
.inputs
[i
]->Size
,
323 /*exec->array.inputs[i]->Enabled,*/
324 exec
->array
.legacy_array
[i
]->Enabled
,
325 exec
->array
.inputs
[i
]->Ptr
,
329 struct gl_buffer_object
*buf
= _mesa_lookup_bufferobj(ctx
, bufName
);
330 GLubyte
*p
= ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
,
331 GL_READ_ONLY_ARB
, buf
);
332 int offset
= (int) exec
->array
.inputs
[i
]->Ptr
;
333 float *f
= (float *) (p
+ offset
);
336 int n
= (count
* stride
) / 4;
339 _mesa_printf(" Data at offset %d:\n", offset
);
340 for (i
= 0; i
< n
; i
++) {
341 _mesa_printf(" float[%d] = 0x%08x %f\n", i
, k
[i
], f
[i
]);
343 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
, buf
);
351 static void GLAPIENTRY
352 vbo_exec_DrawRangeElements(GLenum mode
,
353 GLuint start
, GLuint end
,
354 GLsizei count
, GLenum type
, const GLvoid
*indices
)
356 GET_CURRENT_CONTEXT(ctx
);
357 struct vbo_context
*vbo
= vbo_context(ctx
);
358 struct vbo_exec_context
*exec
= &vbo
->exec
;
359 struct _mesa_index_buffer ib
;
360 struct _mesa_prim prim
[1];
362 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
, type
, indices
))
365 if (end
>= ctx
->Array
.ArrayObj
->_MaxElement
) {
366 /* the max element is out of bounds of one or more enabled arrays */
367 _mesa_warning(ctx
, "glDraw[Range]Elements(start %u, end %u, count %d, "
368 "type 0x%x) index=%u is out of bounds (max=%u)",
369 start
, end
, count
, type
, end
);
371 _mesa_print_arrays(ctx
);
375 FLUSH_CURRENT( ctx
, 0 );
378 _mesa_update_state( ctx
);
380 if (!vbo_validate_shaders(ctx
)) {
381 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawRangeElements(bad shader)");
388 _mesa_update_state( ctx
);
392 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
401 prim
[0].count
= count
;
404 /* Need to give special consideration to rendering a range of
405 * indices starting somewhere above zero. Typically the
406 * application is issuing multiple DrawRangeElements() to draw
407 * successive primitives layed out linearly in the vertex arrays.
408 * Unless the vertex arrays are all in a VBO (or locked as with
409 * CVA), the OpenGL semantics imply that we need to re-read or
410 * re-upload the vertex data on each draw call.
412 * In the case of hardware tnl, we want to avoid starting the
413 * upload at zero, as it will mean every draw call uploads an
414 * increasing amount of not-used vertex data. Worse - in the
415 * software tnl module, all those vertices might be transformed and
416 * lit but never rendered.
418 * If we just upload or transform the vertices in start..end,
419 * however, the indices will be incorrect.
421 * At this level, we don't know exactly what the requirements of
422 * the backend are going to be, though it will likely boil down to
425 * 1) Do nothing, everything is in a VBO and is processed once
428 * 2) Adjust the indices and vertex arrays so that start becomes
431 * Rather than doing anything here, I'll provide a helper function
432 * for the latter case elsewhere.
435 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
, start
, end
);
439 static void GLAPIENTRY
440 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
, const GLvoid
*indices
)
442 GET_CURRENT_CONTEXT(ctx
);
443 GLuint min_index
= 0;
444 GLuint max_index
= 0;
446 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
))
449 if (!vbo_validate_shaders(ctx
)) {
450 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawElements(bad shader)");
454 if (ctx
->Array
.ElementArrayBufferObj
->Name
) {
455 const GLvoid
*map
= ctx
->Driver
.MapBuffer(ctx
,
456 GL_ELEMENT_ARRAY_BUFFER_ARB
,
458 ctx
->Array
.ElementArrayBufferObj
);
460 get_minmax_index(count
, type
, ADD_POINTERS(map
, indices
), &min_index
, &max_index
);
462 ctx
->Driver
.UnmapBuffer(ctx
,
463 GL_ELEMENT_ARRAY_BUFFER_ARB
,
464 ctx
->Array
.ElementArrayBufferObj
);
467 get_minmax_index(count
, type
, indices
, &min_index
, &max_index
);
470 vbo_exec_DrawRangeElements(mode
, min_index
, max_index
, count
, type
, indices
);
474 /***********************************************************************
478 void vbo_exec_array_init( struct vbo_exec_context
*exec
)
481 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
482 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
483 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
485 exec
->vtxfmt
.DrawArrays
= _mesa_noop_DrawArrays
;
486 exec
->vtxfmt
.DrawElements
= _mesa_noop_DrawElements
;
487 exec
->vtxfmt
.DrawRangeElements
= _mesa_noop_DrawRangeElements
;
492 void vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
498 /* This API entrypoint is not ordinarily used */
500 _mesa_DrawArrays(GLenum mode
, GLint first
, GLsizei count
)
502 vbo_exec_DrawArrays(mode
, first
, count
);
506 /* This API entrypoint is not ordinarily used */
508 _mesa_DrawElements(GLenum mode
, GLsizei count
, GLenum type
,
509 const GLvoid
*indices
)
511 vbo_exec_DrawElements(mode
, count
, type
, indices
);
515 /* This API entrypoint is not ordinarily used */
517 _mesa_DrawRangeElements(GLenum mode
, GLuint start
, GLuint end
, GLsizei count
,
518 GLenum type
, const GLvoid
*indices
)
520 vbo_exec_DrawRangeElements(mode
, start
, end
, count
, type
, indices
);