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 "brw_attrib.h"
38 #include "brw_fallback.h"
40 static GLboolean
enabled( const struct gl_client_array
*array
)
42 return array
&& array
->Enabled
;
45 static void merge_enabled_arrays( const struct gl_client_array
*inputs
[],
46 const struct gl_client_array
*arrays
[],
50 for (i
= 0; i
< nr
; i
++)
51 if (!enabled(inputs
[i
]) && enabled(arrays
[i
]))
52 inputs
[i
] = arrays
[i
];
56 /* _NEW_PROGRAM, _NEW_CLIENT */
57 static void recalculate_inputs( struct brw_exec_context
*exec
)
59 if (exec
->ctx
->VertexProgram
._Enabled
) {
60 memcpy( exec
->array
.inputs
,
61 exec
->array
.attrib_arrays
,
62 sizeof(exec
->array
.inputs
) );
64 merge_enabled_arrays( exec
->array
.inputs
,
65 exec
->array
.legacy_arrays
,
69 memcpy( exec
->array
.inputs
,
70 exec
->array
.legacy_arrays
,
71 sizeof(exec
->array
.inputs
) );
74 exec
->array
.recalculate_inputs
= 0;
78 static GLuint
get_max_index( GLuint count
, GLuint type
,
79 const GLvoid
*indices
)
83 /* Compute max element. This is only needed for upload of non-VBO,
84 * non-constant data elements.
86 * XXX: Postpone this calculation until it is known that it is
87 * needed. Otherwise could scan this pointlessly in the all-vbo
91 case GL_UNSIGNED_INT
: {
92 const GLuint
*ui_indices
= (const GLuint
*)indices
;
94 for (i
= 0; i
< count
; i
++)
95 if (ui_indices
[i
] > max_ui
)
96 max_ui
= ui_indices
[i
];
99 case GL_UNSIGNED_SHORT
: {
100 const GLushort
*us_indices
= (const GLushort
*)indices
;
102 for (i
= 0; i
< count
; i
++)
103 if (us_indices
[i
] > max_us
)
104 max_us
= us_indices
[i
];
107 case GL_UNSIGNED_BYTE
: {
108 const GLubyte
*ub_indices
= (const GLubyte
*)indices
;
110 for (i
= 0; i
< count
; i
++)
111 if (ub_indices
[i
] > max_ub
)
112 max_ub
= ub_indices
[i
];
123 /***********************************************************************
127 static void GLAPIENTRY
128 brw_exec_DrawArrays(GLenum mode
, GLint start
, GLsizei count
)
130 GET_CURRENT_CONTEXT(ctx
);
131 struct brw_exec_context
*exec
= IMM_CONTEXT(ctx
)->exec
;
132 struct brw_draw_prim prim
[1];
135 if (!_mesa_validate_DrawArrays( ctx
, mode
, start
, count
))
138 FLUSH_CURRENT( ctx
, 0 );
141 _mesa_update_state( ctx
);
143 /* Bind all inputs, derive varying and size information:
145 if (exec
->array
.recalculate_inputs
)
146 recalculate_inputs( exec
);
153 if (exec
->array
.inputs
[0]->BufferObj
->Name
) {
154 /* Use vertex attribute as a hint to tell us if we expect all
155 * arrays to be in VBO's and if so, don't worry about avoiding
156 * the upload of elements < start.
159 prim
[0].start
= start
;
160 prim
[0].count
= count
;
163 ok
= brw_draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, NULL
, 0, start
+ count
, 0 );
166 /* If not using VBO's, we don't want to upload any more elements
167 * than necessary from the arrays as they will not be valid next
168 * time the application tries to draw with them.
172 prim
[0].count
= count
;
175 ok
= brw_draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, NULL
, start
, start
+ count
, 0 );
180 CALL_DrawArrays(ctx
->Exec
, ( mode
, start
, count
));
187 static void GLAPIENTRY
188 brw_exec_DrawRangeElements(GLenum mode
,
189 GLuint start
, GLuint end
,
190 GLsizei count
, GLenum type
, const GLvoid
*indices
)
192 GET_CURRENT_CONTEXT(ctx
);
193 struct brw_exec_context
*exec
= IMM_CONTEXT(ctx
)->exec
;
194 struct brw_draw_index_buffer ib
;
195 struct brw_draw_prim prim
[1];
197 if (!_mesa_validate_DrawRangeElements( ctx
, mode
, start
, end
, count
, type
, indices
))
200 FLUSH_CURRENT( ctx
, 0 );
203 _mesa_update_state( ctx
);
205 if (exec
->array
.recalculate_inputs
)
206 recalculate_inputs( exec
);
210 ib
.obj
= ctx
->Array
.ElementArrayBufferObj
;
213 if (ctx
->Array
.ElementArrayBufferObj
->Name
) {
214 /* Use the fact that indices are in a VBO as a hint that the
215 * program has put all the arrays in VBO's and we don't have to
216 * worry about performance implications of start > 0.
218 * XXX: consider passing start as min_index to draw_prims instead.
232 prim
[0].count
= count
;
235 if (!brw_draw_prims( ctx
, exec
->array
.inputs
, prim
, 1, &ib
, ib
.rebase
, end
+1, 0 )) {
237 CALL_DrawRangeElements(ctx
->Exec
, (mode
, start
, end
, count
, type
, indices
));
243 static void GLAPIENTRY
244 brw_exec_DrawElements(GLenum mode
, GLsizei count
, GLenum type
, const GLvoid
*indices
)
246 GET_CURRENT_CONTEXT(ctx
);
249 if (!_mesa_validate_DrawElements( ctx
, mode
, count
, type
, indices
))
252 if (ctx
->Array
.ElementArrayBufferObj
->Name
) {
253 const GLvoid
*map
= ctx
->Driver
.MapBuffer(ctx
,
254 GL_ELEMENT_ARRAY_BUFFER_ARB
,
256 ctx
->Array
.ElementArrayBufferObj
);
258 max_index
= get_max_index(count
, type
, ADD_POINTERS(map
, indices
));
260 ctx
->Driver
.UnmapBuffer(ctx
,
261 GL_ELEMENT_ARRAY_BUFFER_ARB
,
262 ctx
->Array
.ElementArrayBufferObj
);
265 max_index
= get_max_index(count
, type
, indices
);
268 brw_exec_DrawRangeElements(mode
, 0, max_index
, count
, type
, indices
);
272 /***********************************************************************
277 static void init_legacy_arrays( GLcontext
*ctx
,
278 const struct gl_client_array
*arrays
[] )
280 struct gl_array_object
*obj
= ctx
->Array
.ArrayObj
;
283 memset(arrays
, 0, sizeof(*arrays
) * BRW_ATTRIB_MAX
);
285 arrays
[BRW_ATTRIB_POS
] = &obj
->Vertex
;
286 arrays
[BRW_ATTRIB_NORMAL
] = &obj
->Normal
;
287 arrays
[BRW_ATTRIB_COLOR0
] = &obj
->Color
;
288 arrays
[BRW_ATTRIB_COLOR1
] = &obj
->SecondaryColor
;
289 arrays
[BRW_ATTRIB_FOG
] = &obj
->FogCoord
;
291 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++)
292 arrays
[BRW_ATTRIB_TEX0
+ i
] = &obj
->TexCoord
[i
];
294 arrays
[BRW_ATTRIB_INDEX
] = &obj
->Index
;
295 arrays
[BRW_ATTRIB_EDGEFLAG
] = &obj
->EdgeFlag
;
299 static void init_attrib_arrays( GLcontext
*ctx
,
300 const struct gl_client_array
*arrays
[] )
302 struct gl_array_object
*obj
= ctx
->Array
.ArrayObj
;
305 for (i
= 0; i
< BRW_ATTRIB_FIRST_MATERIAL
; i
++)
306 arrays
[i
] = &obj
->VertexAttrib
[i
];
312 void brw_exec_array_init( struct brw_exec_context
*exec
)
314 GLcontext
*ctx
= exec
->ctx
;
316 init_legacy_arrays(ctx
, exec
->array
.legacy_arrays
);
317 init_attrib_arrays(ctx
, exec
->array
.attrib_arrays
);
320 exec
->vtxfmt
.DrawArrays
= brw_exec_DrawArrays
;
321 exec
->vtxfmt
.DrawElements
= brw_exec_DrawElements
;
322 exec
->vtxfmt
.DrawRangeElements
= brw_exec_DrawRangeElements
;
324 exec
->vtxfmt
.DrawArrays
= _mesa_noop_DrawArrays
;
325 exec
->vtxfmt
.DrawElements
= _mesa_noop_DrawElements
;
326 exec
->vtxfmt
.DrawRangeElements
= _mesa_noop_DrawRangeElements
;
329 exec
->array
.recalculate_inputs
= 1;
331 exec
->array
.index_obj
= ctx
->Driver
.NewBufferObject(ctx
, 1, GL_ARRAY_BUFFER_ARB
);
335 void brw_exec_array_destroy( struct brw_exec_context
*exec
)
337 GLcontext
*ctx
= exec
->ctx
;
339 ctx
->Driver
.DeleteBuffer(ctx
, exec
->array
.index_obj
);