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"
34 #include "vbo_context.h"
36 /* Compute min and max elements for drawelements calls.
38 static void get_minmax_index( GLuint count
, GLuint type
,
39 const GLvoid
*indices
,
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
];
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
];
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
];
89 /* Just translate the arrayobj into a sane layout.
91 static void bind_array_obj( GLcontext
*ctx
)
93 struct vbo_context
*vbo
= vbo_context(ctx
);
94 struct vbo_exec_context
*exec
= &vbo
->exec
;
97 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
98 * rather than as individual named arrays. Then this function can
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
;
112 exec
->array
.legacy_array
[VERT_ATTRIB_EDGEFLAG
] = &ctx
->Array
.ArrayObj
->EdgeFlag
;
114 for (i
= 0; i
< 8; i
++)
115 exec
->array
.legacy_array
[VERT_ATTRIB_TEX0
+ i
] = &ctx
->Array
.ArrayObj
->TexCoord
[i
];
117 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
118 exec
->array
.generic_array
[i
] = &ctx
->Array
.ArrayObj
->VertexAttrib
[i
];
120 exec
->array
.array_obj
= ctx
->Array
.ArrayObj
->Name
;
123 static void recalculate_input_bindings( GLcontext
*ctx
)
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];
130 exec
->array
.program_mode
= get_program_mode(ctx
);
131 exec
->array
.enabled_flags
= ctx
->Array
.ArrayObj
->_Enabled
;
133 switch (exec
->array
.program_mode
) {
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.
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
];
143 inputs
[i
] = &vbo
->legacy_currval
[i
];
146 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
147 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->mat_currval
[i
];
150 /* Could use just about anything, just to fill in the empty
153 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_MAX
- VERT_ATTRIB_GENERIC0
; i
++)
154 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
158 /* NV_vertex_program - attribute arrays alias and override
159 * conventional, legacy arrays. No materials, and the generic
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
];
168 inputs
[i
] = &vbo
->legacy_currval
[i
];
171 /* Could use just about anything, just to fill in the empty
174 for (i
= VERT_ATTRIB_GENERIC0
; i
< VERT_ATTRIB_MAX
; i
++)
175 inputs
[i
] = &vbo
->generic_currval
[i
- VERT_ATTRIB_GENERIC0
];
179 /* ARB_vertex_program - Only the attribute zero (position) array
180 * aliases and overrides the legacy position array.
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.
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];
191 inputs
[0] = &vbo
->legacy_currval
[0];
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
];
198 inputs
[i
] = &vbo
->legacy_currval
[i
];
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
];
205 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
211 static void bind_arrays( GLcontext
*ctx
)
214 if (ctx
->Array
.ArrayObj
.Name
!= exec
->array
.array_obj
) {
216 recalculate_input_bindings(ctx
);
218 else if (exec
->array
.program_mode
!= get_program_mode(ctx
) ||
219 exec
->array
.enabled_flags
!= ctx
->Array
.ArrayObj
->_Enabled
) {
221 recalculate_input_bindings(ctx
);
225 recalculate_input_bindings(ctx
);
231 /***********************************************************************
235 static void GLAPIENTRY
236 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
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];
243 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
246 FLUSH_CURRENT( ctx
, 0 );
249 _mesa_update_state( ctx
);
251 if (!vbo_validate_shaders(ctx
)) {
252 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawArrays(bad shader)");
263 prim
[0].start
= start
;
264 prim
[0].count
= count
;
267 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, NULL
, start
, start
+ count
- 1 );
272 static void GLAPIENTRY
273 vbo_exec_DrawRangeElements(GLenum mode
,
274 GLuint start
, GLuint end
,
275 GLsizei count
, GLenum type
, const GLvoid
*indices
)
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];
283 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
, type
, indices
))
286 FLUSH_CURRENT( ctx
, 0 );
289 _mesa_update_state( ctx
);
291 if (!vbo_validate_shaders(ctx
)) {
292 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawRangeElements(bad shader)");
300 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
309 prim
[0].count
= count
;
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.
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.
326 * If we just upload or transform the vertices in start..end,
327 * however, the indices will be incorrect.
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
333 * 1) Do nothing, everything is in a VBO and is processed once
336 * 2) Adjust the indices and vertex arrays so that start becomes
339 * Rather than doing anything here, I'll provide a helper function
340 * for the latter case elsewhere.
343 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
, start
, end
);
346 static void GLAPIENTRY
347 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
, const GLvoid
*indices
)
349 GET_CURRENT_CONTEXT(ctx
);
350 GLuint min_index
= 0;
351 GLuint max_index
= 0;
353 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
))
356 if (!vbo_validate_shaders(ctx
)) {
357 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glDrawElements(bad shader)");
361 if (ctx
->Array
.ElementArrayBufferObj
->Name
) {
362 const GLvoid
*map
= ctx
->Driver
.MapBuffer(ctx
,
363 GL_ELEMENT_ARRAY_BUFFER_ARB
,
365 ctx
->Array
.ElementArrayBufferObj
);
367 get_minmax_index(count
, type
, ADD_POINTERS(map
, indices
), &min_index
, &max_index
);
369 ctx
->Driver
.UnmapBuffer(ctx
,
370 GL_ELEMENT_ARRAY_BUFFER_ARB
,
371 ctx
->Array
.ElementArrayBufferObj
);
374 get_minmax_index(count
, type
, indices
, &min_index
, &max_index
);
377 vbo_exec_DrawRangeElements(mode
, min_index
, max_index
, count
, type
, indices
);
381 /***********************************************************************
388 void vbo_exec_array_init( struct vbo_exec_context
*exec
)
391 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
392 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
393 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
395 exec
->vtxfmt
.DrawArrays
= _mesa_noop_DrawArrays
;
396 exec
->vtxfmt
.DrawElements
= _mesa_noop_DrawElements
;
397 exec
->vtxfmt
.DrawRangeElements
= _mesa_noop_DrawRangeElements
;
402 void vbo_exec_array_destroy( struct vbo_exec_context
*exec
)