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 **************************************************************************/
31 #include "api_validate.h"
35 #include "vbo_context.h"
37 static GLuint
get_max_index( GLuint count
, GLuint type
,
38 const GLvoid
*indices
)
42 /* Compute max element. This is only needed for upload of non-VBO,
43 * non-constant data elements.
45 * XXX: Postpone this calculation until it is known that it is
46 * needed. Otherwise could scan this pointlessly in the all-vbo
50 case GL_UNSIGNED_INT
: {
51 const GLuint
*ui_indices
= (const GLuint
*)indices
;
53 for (i
= 0; i
< count
; i
++)
54 if (ui_indices
[i
] > max_ui
)
55 max_ui
= ui_indices
[i
];
58 case GL_UNSIGNED_SHORT
: {
59 const GLushort
*us_indices
= (const GLushort
*)indices
;
61 for (i
= 0; i
< count
; i
++)
62 if (us_indices
[i
] > max_us
)
63 max_us
= us_indices
[i
];
66 case GL_UNSIGNED_BYTE
: {
67 const GLubyte
*ub_indices
= (const GLubyte
*)indices
;
69 for (i
= 0; i
< count
; i
++)
70 if (ub_indices
[i
] > max_ub
)
71 max_ub
= ub_indices
[i
];
80 /* Just translate the arrayobj into a sane layout.
82 static void bind_array_obj( GLcontext
*ctx
)
84 struct vbo_context
*vbo
= vbo_context(ctx
);
85 struct vbo_exec_context
*exec
= &vbo
->exec
;
88 /* TODO: Fix the ArrayObj struct to keep legacy arrays in an array
89 * rather than as individual named arrays. Then this function can
92 exec
->array
.legacy_array
[VERT_ATTRIB_POS
] = &ctx
->Array
.ArrayObj
->Vertex
;
93 exec
->array
.legacy_array
[VERT_ATTRIB_WEIGHT
] = &vbo
->legacy_currval
[VERT_ATTRIB_WEIGHT
];
94 exec
->array
.legacy_array
[VERT_ATTRIB_NORMAL
] = &ctx
->Array
.ArrayObj
->Normal
;
95 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR0
] = &ctx
->Array
.ArrayObj
->Color
;
96 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR1
] = &ctx
->Array
.ArrayObj
->SecondaryColor
;
97 exec
->array
.legacy_array
[VERT_ATTRIB_FOG
] = &ctx
->Array
.ArrayObj
->FogCoord
;
98 exec
->array
.legacy_array
[VERT_ATTRIB_COLOR_INDEX
] = &ctx
->Array
.ArrayObj
->Index
;
99 exec
->array
.legacy_array
[VBO_ATTRIB_EDGEFLAG
] = &ctx
->Array
.ArrayObj
->EdgeFlag
;
101 for (i
= 0; i
< 8; i
++)
102 exec
->array
.legacy_array
[VBO_ATTRIB_TEX0
+ i
] = &ctx
->Array
.ArrayObj
->TexCoord
[i
];
104 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
105 exec
->array
.generic_array
[i
] = &ctx
->Array
.ArrayObj
->VertexAttrib
[i
];
107 exec
->array
.array_obj
= ctx
->Array
.ArrayObj
->Name
;
110 static void recalculate_input_bindings( GLcontext
*ctx
)
112 struct vbo_context
*vbo
= vbo_context(ctx
);
113 struct vbo_exec_context
*exec
= &vbo
->exec
;
114 const struct gl_client_array
**inputs
= &exec
->array
.inputs
[0];
117 exec
->array
.program_mode
= get_program_mode(ctx
);
118 exec
->array
.enabled_flags
= ctx
->Array
.ArrayObj
->_Enabled
;
120 /* TODO: Get rid of NV_program (please!).
122 switch (exec
->array
.program_mode
) {
124 /* When no vertex program is active, we put the material values
125 * into the generic slots. This is the only situation where
126 * material values are available as per-vertex attributes.
128 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
129 if (exec
->array
.legacy_array
[i
]->Enabled
)
130 inputs
[i
] = exec
->array
.legacy_array
[i
];
132 inputs
[i
] = &vbo
->legacy_currval
[i
];
135 for (i
= 0; i
< MAT_ATTRIB_MAX
; i
++) {
136 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->mat_currval
[i
];
139 /* Could use just about anything, just to fill in the empty
142 for (i
= MAT_ATTRIB_MAX
; i
< VERT_ATTRIB_MAX
; i
++)
143 inputs
[i
] = &vbo
->generic_currval
[i
- VERT_ATTRIB_GENERIC0
];
147 /* NV_vertex_program - attribute arrays alias and override
148 * conventional, legacy arrays. No materials, and the generic
151 for (i
= 0; i
<= VERT_ATTRIB_TEX7
; i
++) {
152 if (exec
->array
.generic_array
[i
]->Enabled
)
153 inputs
[i
] = exec
->array
.generic_array
[i
];
154 else if (exec
->array
.legacy_array
[i
]->Enabled
)
155 inputs
[i
] = exec
->array
.legacy_array
[i
];
157 inputs
[i
] = &vbo
->legacy_currval
[i
];
160 /* Could use just about anything, just to fill in the empty
163 for (i
= VERT_ATTRIB_GENERIC0
; i
< VERT_ATTRIB_MAX
; i
++)
164 inputs
[i
] = &vbo
->generic_currval
[i
- VERT_ATTRIB_GENERIC0
];
168 /* ARB_vertex_program - Only the attribute zero (position) array
169 * aliases and overrides the legacy position array.
171 * Otherwise, legacy attributes available in the legacy slots,
172 * generic attributes in the generic slots and materials are not
173 * available as per-vertex attributes.
175 if (exec
->array
.generic_array
[0]->Enabled
)
176 inputs
[0] = exec
->array
.generic_array
[0];
177 else if (exec
->array
.legacy_array
[0]->Enabled
)
178 inputs
[0] = exec
->array
.legacy_array
[0];
180 inputs
[0] = &vbo
->legacy_currval
[0];
183 for (i
= 1; i
<= VERT_ATTRIB_TEX7
; i
++) {
184 if (exec
->array
.legacy_array
[i
]->Enabled
)
185 inputs
[i
] = exec
->array
.legacy_array
[i
];
187 inputs
[i
] = &vbo
->legacy_currval
[i
];
190 for (i
= 0; i
< 16; i
++) {
191 if (exec
->array
.generic_array
[0]->Enabled
)
192 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = exec
->array
.generic_array
[i
];
194 inputs
[VERT_ATTRIB_GENERIC0
+ i
] = &vbo
->generic_currval
[i
];
200 static void bind_arrays( GLcontext
*ctx
)
203 if (ctx
->Array
.ArrayObj
.Name
!= exec
->array
.array_obj
) {
205 recalculate_input_bindings(ctx
);
207 else if (exec
->array
.program_mode
!= get_program_mode(ctx
) ||
208 exec
->array
.enabled_flags
!= ctx
->Array
.ArrayObj
->_Enabled
) {
210 recalculate_input_bindings(ctx
);
214 recalculate_input_bindings(ctx
);
220 /***********************************************************************
224 static void GLAPIENTRY
225 vbo_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
227 GET_CURRENT_CONTEXT(ctx
);
228 struct vbo_context
*vbo
= vbo_context(ctx
);
229 struct vbo_exec_context
*exec
= &vbo
->exec
;
230 struct _mesa_prim prim
[1];
232 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
235 FLUSH_CURRENT( ctx
, 0 );
238 _mesa_update_state( ctx
);
247 if (exec
->array
.inputs
[0]->BufferObj
->Name
) {
248 /* Use vertex attribute as a hint to tell us if we expect all
249 * arrays to be in VBO's and if so, don't worry about avoiding
250 * the upload of elements < start.
253 prim
[0].start
= start
;
254 prim
[0].count
= count
;
257 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, NULL
, 0, start
+ count
);
260 /* If not using VBO's, we don't want to upload any more elements
261 * than necessary from the arrays as they will not be valid next
262 * time the application tries to draw with them.
266 prim
[0].count
= count
;
269 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, NULL
, start
, start
+ count
);
275 static void GLAPIENTRY
276 vbo_exec_DrawRangeElements(GLenum mode
,
277 GLuint start
, GLuint end
,
278 GLsizei count
, GLenum type
, const GLvoid
*indices
)
280 GET_CURRENT_CONTEXT(ctx
);
281 struct vbo_context
*vbo
= vbo_context(ctx
);
282 struct vbo_exec_context
*exec
= &vbo
->exec
;
283 struct _mesa_index_buffer ib
;
284 struct _mesa_prim prim
[1];
286 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
, type
, indices
))
289 FLUSH_CURRENT( ctx
, 0 );
292 _mesa_update_state( ctx
);
296 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
299 if (ctx
->Array
.ElementArrayBufferObj
->Name
) {
300 /* Use the fact that indices are in a VBO as a hint that the
301 * program has put all the arrays in VBO's and we don't have to
302 * worry about performance implications of start > 0.
304 * XXX: consider passing start as min_index to draw_prims instead.
318 prim
[0].count
= count
;
321 vbo
->draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
, ib
.rebase
, end
+1 );
325 static void GLAPIENTRY
326 vbo_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
, const GLvoid
*indices
)
328 GET_CURRENT_CONTEXT(ctx
);
331 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
))
334 if (ctx
->Array
.ElementArrayBufferObj
->Name
) {
335 const GLvoid
*map
= ctx
->Driver
.MapBuffer(ctx
,
336 GL_ELEMENT_ARRAY_BUFFER_ARB
,
338 ctx
->Array
.ElementArrayBufferObj
);
340 max_index
= get_max_index(count
, type
, ADD_POINTERS(map
, indices
));
342 ctx
->Driver
.UnmapBuffer(ctx
,
343 GL_ELEMENT_ARRAY_BUFFER_ARB
,
344 ctx
->Array
.ElementArrayBufferObj
);
347 max_index
= get_max_index(count
, type
, indices
);
350 vbo_exec_DrawRangeElements(mode
, 0, max_index
, count
, type
, indices
);
354 /***********************************************************************
361 void vbo_exec_array_init( struct vbo_exec_context
*exec
)
363 GLcontext
*ctx
= exec
->ctx
;
366 exec
->vtxfmt
.DrawArrays
= vbo_exec_DrawArrays
;
367 exec
->vtxfmt
.DrawElements
= vbo_exec_DrawElements
;
368 exec
->vtxfmt
.DrawRangeElements
= vbo_exec_DrawRangeElements
;
370 exec
->vtxfmt
.DrawArrays
= _mesa_noop_DrawArrays
;
371 exec
->vtxfmt
.DrawElements
= _mesa_noop_DrawElements
;
372 exec
->vtxfmt
.DrawRangeElements
= _mesa_noop_DrawRangeElements
;
375 exec
->array
.index_obj
= ctx
->Driver
.NewBufferObject(ctx
, 1, GL_ARRAY_BUFFER_ARB
);
379 void vbo_exec_array_destroy( struct vbo_exec_context
*exec
)
381 GLcontext
*ctx
= exec
->ctx
;
383 ctx
->Driver
.DeleteBuffer(ctx
, exec
->array
.index_obj
);