2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keith@tungstengraphics.com>
34 vbo_exec_debug_verts( struct vbo_exec_context
*exec
)
36 GLuint count
= exec
->vtx
.vert_count
;
39 printf("%s: %u vertices %d primitives, %d vertsize\n",
43 exec
->vtx
.vertex_size
);
45 for (i
= 0 ; i
< exec
->vtx
.prim_count
; i
++) {
46 struct _mesa_prim
*prim
= &exec
->vtx
.prim
[i
];
47 printf(" prim %d: %s%s %d..%d %s %s\n",
49 _mesa_lookup_prim_by_nr(prim
->mode
),
50 prim
->weak
? " (weak)" : "",
52 prim
->start
+ prim
->count
,
53 prim
->begin
? "BEGIN" : "(wrap)",
54 prim
->end
? "END" : "(wrap)");
60 * NOTE: Need to have calculated primitives by this point -- do it on the fly.
61 * NOTE: Old 'parity' issue is gone.
64 vbo_copy_vertices( struct vbo_exec_context
*exec
)
66 GLuint nr
= exec
->vtx
.prim
[exec
->vtx
.prim_count
-1].count
;
68 GLuint sz
= exec
->vtx
.vertex_size
;
69 GLfloat
*dst
= exec
->vtx
.copied
.buffer
;
70 const GLfloat
*src
= (exec
->vtx
.buffer_map
+
71 exec
->vtx
.prim
[exec
->vtx
.prim_count
-1].start
*
72 exec
->vtx
.vertex_size
);
75 switch (exec
->ctx
->Driver
.CurrentExecPrimitive
) {
80 for (i
= 0 ; i
< ovf
; i
++)
81 memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
* sizeof(GLfloat
) );
85 for (i
= 0 ; i
< ovf
; i
++)
86 memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
* sizeof(GLfloat
) );
90 for (i
= 0 ; i
< ovf
; i
++)
91 memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
* sizeof(GLfloat
) );
98 memcpy( dst
, src
+(nr
-1)*sz
, sz
* sizeof(GLfloat
) );
102 case GL_TRIANGLE_FAN
:
108 memcpy( dst
, src
+0, sz
* sizeof(GLfloat
) );
112 memcpy( dst
, src
+0, sz
* sizeof(GLfloat
) );
113 memcpy( dst
+sz
, src
+(nr
-1)*sz
, sz
* sizeof(GLfloat
) );
116 case GL_TRIANGLE_STRIP
:
117 /* no parity issue, but need to make sure the tri is not drawn twice */
119 exec
->vtx
.prim
[exec
->vtx
.prim_count
-1].count
--;
134 for (i
= 0 ; i
< ovf
; i
++)
135 memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
* sizeof(GLfloat
) );
137 case PRIM_OUTSIDE_BEGIN_END
:
147 /* TODO: populate these as the vertex is defined:
150 vbo_exec_bind_arrays( struct gl_context
*ctx
)
152 struct vbo_context
*vbo
= vbo_context(ctx
);
153 struct vbo_exec_context
*exec
= &vbo
->exec
;
154 struct gl_client_array
*arrays
= exec
->vtx
.arrays
;
155 const GLuint count
= exec
->vtx
.vert_count
;
157 GLbitfield64 varying_inputs
= 0x0;
159 /* Install the default (ie Current) attributes first, then overlay
162 for (attr
= 0; attr
< VBO_ATTRIB_MAX
; attr
++) {
163 exec
->vtx
.inputs
[attr
] = &vbo
->legacy_currval
[attr
];
166 /* Make all active attributes (including edgeflag) available as
169 for (attr
= 0; attr
< VBO_ATTRIB_MAX
; attr
++) {
171 if (exec
->vtx
.attrsz
[attr
]) {
172 GLsizeiptr offset
= (GLbyte
*)exec
->vtx
.attrptr
[attr
] -
173 (GLbyte
*)exec
->vtx
.vertex
;
175 /* override the default array set above */
176 ASSERT(attr
< Elements(exec
->vtx
.inputs
));
177 ASSERT(attr
< Elements(exec
->vtx
.arrays
)); /* arrays[] */
178 exec
->vtx
.inputs
[attr
] = &arrays
[attr
];
180 if (_mesa_is_bufferobj(exec
->vtx
.bufferobj
)) {
181 /* a real buffer obj: Ptr is an offset, not a pointer*/
182 assert(exec
->vtx
.bufferobj
->Pointer
); /* buf should be mapped */
184 arrays
[attr
].Ptr
= (GLubyte
*)exec
->vtx
.bufferobj
->Offset
+ offset
;
187 /* Ptr into ordinary app memory */
188 arrays
[attr
].Ptr
= (GLubyte
*)exec
->vtx
.buffer_map
+ offset
;
190 arrays
[attr
].Size
= exec
->vtx
.attrsz
[attr
];
191 arrays
[attr
].StrideB
= exec
->vtx
.vertex_size
* sizeof(GLfloat
);
192 arrays
[attr
].Stride
= exec
->vtx
.vertex_size
* sizeof(GLfloat
);
193 arrays
[attr
].Type
= GL_FLOAT
;
194 arrays
[attr
].Enabled
= 1;
195 arrays
[attr
]._ElementSize
= arrays
[attr
].Size
* sizeof(GLfloat
);
196 _mesa_reference_buffer_object(ctx
,
197 &arrays
[attr
].BufferObj
,
198 exec
->vtx
.bufferobj
);
199 arrays
[attr
]._MaxElement
= count
; /* ??? */
201 varying_inputs
|= VERT_BIT(attr
);
202 ctx
->NewState
|= _NEW_ARRAY
;
209 * Unmap the VBO. This is called before drawing.
212 vbo_exec_vtx_unmap( struct vbo_exec_context
*exec
)
214 if (_mesa_is_bufferobj(exec
->vtx
.bufferobj
)) {
215 struct gl_context
*ctx
= exec
->ctx
;
217 if (ctx
->Driver
.FlushMappedBufferRange
) {
218 GLintptr offset
= exec
->vtx
.buffer_used
- exec
->vtx
.bufferobj
->Offset
;
219 GLsizeiptr length
= (exec
->vtx
.buffer_ptr
- exec
->vtx
.buffer_map
) * sizeof(float);
222 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
,
223 exec
->vtx
.bufferobj
);
226 exec
->vtx
.buffer_used
+= (exec
->vtx
.buffer_ptr
-
227 exec
->vtx
.buffer_map
) * sizeof(float);
229 assert(exec
->vtx
.buffer_used
<= VBO_VERT_BUFFER_SIZE
);
230 assert(exec
->vtx
.buffer_ptr
!= NULL
);
232 ctx
->Driver
.UnmapBuffer(ctx
, exec
->vtx
.bufferobj
);
233 exec
->vtx
.buffer_map
= NULL
;
234 exec
->vtx
.buffer_ptr
= NULL
;
235 exec
->vtx
.max_vert
= 0;
241 * Map the vertex buffer to begin storing glVertex, glColor, etc data.
244 vbo_exec_vtx_map( struct vbo_exec_context
*exec
)
246 struct gl_context
*ctx
= exec
->ctx
;
247 const GLenum accessRange
= GL_MAP_WRITE_BIT
| /* for MapBufferRange */
248 GL_MAP_INVALIDATE_RANGE_BIT
|
249 GL_MAP_UNSYNCHRONIZED_BIT
|
250 GL_MAP_FLUSH_EXPLICIT_BIT
|
252 const GLenum usage
= GL_STREAM_DRAW_ARB
;
254 if (!_mesa_is_bufferobj(exec
->vtx
.bufferobj
))
257 assert(!exec
->vtx
.buffer_map
);
258 assert(!exec
->vtx
.buffer_ptr
);
260 if (VBO_VERT_BUFFER_SIZE
> exec
->vtx
.buffer_used
+ 1024) {
261 /* The VBO exists and there's room for more */
262 if (exec
->vtx
.bufferobj
->Size
> 0) {
263 exec
->vtx
.buffer_map
=
264 (GLfloat
*)ctx
->Driver
.MapBufferRange(ctx
,
265 exec
->vtx
.buffer_used
,
266 (VBO_VERT_BUFFER_SIZE
-
267 exec
->vtx
.buffer_used
),
269 exec
->vtx
.bufferobj
);
270 exec
->vtx
.buffer_ptr
= exec
->vtx
.buffer_map
;
273 exec
->vtx
.buffer_ptr
= exec
->vtx
.buffer_map
= NULL
;
277 if (!exec
->vtx
.buffer_map
) {
278 /* Need to allocate a new VBO */
279 exec
->vtx
.buffer_used
= 0;
281 if (ctx
->Driver
.BufferData(ctx
, GL_ARRAY_BUFFER_ARB
,
282 VBO_VERT_BUFFER_SIZE
,
283 NULL
, usage
, exec
->vtx
.bufferobj
)) {
284 /* buffer allocation worked, now map the buffer */
285 exec
->vtx
.buffer_map
=
286 (GLfloat
*)ctx
->Driver
.MapBufferRange(ctx
,
287 0, VBO_VERT_BUFFER_SIZE
,
289 exec
->vtx
.bufferobj
);
292 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "VBO allocation");
293 exec
->vtx
.buffer_map
= NULL
;
297 exec
->vtx
.buffer_ptr
= exec
->vtx
.buffer_map
;
299 if (!exec
->vtx
.buffer_map
) {
301 _mesa_install_exec_vtxfmt( ctx
, &exec
->vtxfmt_noop
);
304 if (_mesa_using_noop_vtxfmt(ctx
->Exec
)) {
305 /* The no-op functions are installed so switch back to regular
306 * functions. We do this test just to avoid frequent and needless
307 * calls to _mesa_install_exec_vtxfmt().
309 _mesa_install_exec_vtxfmt(ctx
, &exec
->vtxfmt
);
314 printf("map %d..\n", exec
->vtx
.buffer_used
);
320 * Execute the buffer and save copied verts.
321 * \param keep_unmapped if true, leave the VBO unmapped when we're done.
324 vbo_exec_vtx_flush(struct vbo_exec_context
*exec
, GLboolean keepUnmapped
)
327 vbo_exec_debug_verts( exec
);
329 if (exec
->vtx
.prim_count
&&
330 exec
->vtx
.vert_count
) {
332 exec
->vtx
.copied
.nr
= vbo_copy_vertices( exec
);
334 if (exec
->vtx
.copied
.nr
!= exec
->vtx
.vert_count
) {
335 struct gl_context
*ctx
= exec
->ctx
;
337 vbo_exec_bind_arrays( ctx
);
340 _mesa_update_state( ctx
);
342 if (_mesa_is_bufferobj(exec
->vtx
.bufferobj
)) {
343 vbo_exec_vtx_unmap( exec
);
347 printf("%s %d %d\n", __FUNCTION__
, exec
->vtx
.prim_count
,
348 exec
->vtx
.vert_count
);
350 vbo_context(ctx
)->draw_prims( ctx
,
353 exec
->vtx
.prim_count
,
357 exec
->vtx
.vert_count
- 1);
359 /* If using a real VBO, get new storage -- unless asked not to.
361 if (_mesa_is_bufferobj(exec
->vtx
.bufferobj
) && !keepUnmapped
) {
362 vbo_exec_vtx_map( exec
);
367 /* May have to unmap explicitly if we didn't draw:
370 _mesa_is_bufferobj(exec
->vtx
.bufferobj
) &&
371 exec
->vtx
.buffer_map
) {
372 vbo_exec_vtx_unmap( exec
);
375 if (keepUnmapped
|| exec
->vtx
.vertex_size
== 0)
376 exec
->vtx
.max_vert
= 0;
378 exec
->vtx
.max_vert
= ((VBO_VERT_BUFFER_SIZE
- exec
->vtx
.buffer_used
) /
379 (exec
->vtx
.vertex_size
* sizeof(GLfloat
)));
381 exec
->vtx
.buffer_ptr
= exec
->vtx
.buffer_map
;
382 exec
->vtx
.prim_count
= 0;
383 exec
->vtx
.vert_count
= 0;
387 #endif /* FEATURE_beginend */