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>
28 #include "main/glheader.h"
29 #include "main/bufferobj.h"
30 #include "main/context.h"
31 #include "main/enums.h"
32 #include "main/state.h"
33 #include "main/macros.h"
35 #include "vbo_context.h"
39 vbo_exec_debug_verts( struct vbo_exec_context
*exec
)
41 GLuint count
= exec
->vtx
.vert_count
;
44 _mesa_printf("%s: %u vertices %d primitives, %d vertsize\n",
48 exec
->vtx
.vertex_size
);
50 for (i
= 0 ; i
< exec
->vtx
.prim_count
; i
++) {
51 struct _mesa_prim
*prim
= &exec
->vtx
.prim
[i
];
52 _mesa_printf(" prim %d: %s%s %d..%d %s %s\n",
54 _mesa_lookup_prim_by_nr(prim
->mode
),
55 prim
->weak
? " (weak)" : "",
57 prim
->start
+ prim
->count
,
58 prim
->begin
? "BEGIN" : "(wrap)",
59 prim
->end
? "END" : "(wrap)");
65 * NOTE: Need to have calculated primitives by this point -- do it on the fly.
66 * NOTE: Old 'parity' issue is gone.
69 vbo_copy_vertices( struct vbo_exec_context
*exec
)
71 GLuint nr
= exec
->vtx
.prim
[exec
->vtx
.prim_count
-1].count
;
73 GLuint sz
= exec
->vtx
.vertex_size
;
74 GLfloat
*dst
= exec
->vtx
.copied
.buffer
;
75 const GLfloat
*src
= (exec
->vtx
.buffer_map
+
76 exec
->vtx
.prim
[exec
->vtx
.prim_count
-1].start
*
77 exec
->vtx
.vertex_size
);
80 switch (exec
->ctx
->Driver
.CurrentExecPrimitive
) {
85 for (i
= 0 ; i
< ovf
; i
++)
86 _mesa_memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
* sizeof(GLfloat
) );
90 for (i
= 0 ; i
< ovf
; i
++)
91 _mesa_memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
* sizeof(GLfloat
) );
95 for (i
= 0 ; i
< ovf
; i
++)
96 _mesa_memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
* sizeof(GLfloat
) );
103 _mesa_memcpy( dst
, src
+(nr
-1)*sz
, sz
* sizeof(GLfloat
) );
107 case GL_TRIANGLE_FAN
:
113 _mesa_memcpy( dst
, src
+0, sz
* sizeof(GLfloat
) );
117 _mesa_memcpy( dst
, src
+0, sz
* sizeof(GLfloat
) );
118 _mesa_memcpy( dst
+sz
, src
+(nr
-1)*sz
, sz
* sizeof(GLfloat
) );
121 case GL_TRIANGLE_STRIP
:
122 /* no parity issue, but need to make sure the tri is not drawn twice */
124 exec
->vtx
.prim
[exec
->vtx
.prim_count
-1].count
--;
139 for (i
= 0 ; i
< ovf
; i
++)
140 _mesa_memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
* sizeof(GLfloat
) );
142 case PRIM_OUTSIDE_BEGIN_END
:
152 /* TODO: populate these as the vertex is defined:
155 vbo_exec_bind_arrays( GLcontext
*ctx
)
157 struct vbo_context
*vbo
= vbo_context(ctx
);
158 struct vbo_exec_context
*exec
= &vbo
->exec
;
159 struct gl_client_array
*arrays
= exec
->vtx
.arrays
;
160 const GLuint count
= exec
->vtx
.vert_count
;
161 const GLubyte
*data
= (GLubyte
*) exec
->vtx
.buffer_map
;
164 GLbitfield varying_inputs
= 0x0;
166 /* Install the default (ie Current) attributes first, then overlay
169 switch (get_program_mode(exec
->ctx
)) {
171 for (attr
= 0; attr
< 16; attr
++) {
172 exec
->vtx
.inputs
[attr
] = &vbo
->legacy_currval
[attr
];
174 for (attr
= 0; attr
< MAT_ATTRIB_MAX
; attr
++) {
175 exec
->vtx
.inputs
[attr
+ 16] = &vbo
->mat_currval
[attr
];
177 map
= vbo
->map_vp_none
;
181 /* The aliasing of attributes for NV vertex programs has already
182 * occurred. NV vertex programs cannot access material values,
183 * nor attributes greater than VERT_ATTRIB_TEX7.
185 for (attr
= 0; attr
< 16; attr
++) {
186 exec
->vtx
.inputs
[attr
] = &vbo
->legacy_currval
[attr
];
187 exec
->vtx
.inputs
[attr
+ 16] = &vbo
->generic_currval
[attr
];
189 map
= vbo
->map_vp_arb
;
191 /* check if VERT_ATTRIB_POS is not read but VERT_BIT_GENERIC0 is read.
192 * In that case we effectively need to route the data from
193 * glVertexAttrib(0, val) calls to feed into the GENERIC0 input.
195 if ((ctx
->VertexProgram
._Current
->Base
.InputsRead
& VERT_BIT_POS
) == 0 &&
196 (ctx
->VertexProgram
._Current
->Base
.InputsRead
& VERT_BIT_GENERIC0
)) {
197 exec
->vtx
.inputs
[16] = exec
->vtx
.inputs
[0];
198 exec
->vtx
.attrsz
[16] = exec
->vtx
.attrsz
[0];
199 exec
->vtx
.attrptr
[16] = exec
->vtx
.attrptr
[0];
200 exec
->vtx
.attrsz
[0] = 0;
207 /* Make all active attributes (including edgeflag) available as
210 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
211 const GLuint src
= map
[attr
];
213 if (exec
->vtx
.attrsz
[src
]) {
214 /* override the default array set above */
215 exec
->vtx
.inputs
[attr
] = &arrays
[attr
];
217 if (_mesa_is_bufferobj(exec
->vtx
.bufferobj
)) {
218 /* a real buffer obj: Ptr is an offset, not a pointer*/
220 assert(exec
->vtx
.bufferobj
->Pointer
); /* buf should be mapped */
221 offset
= (GLbyte
*) data
-
222 (GLbyte
*) exec
->vtx
.bufferobj
->Pointer
+
223 exec
->vtx
.bufferobj
->Offset
;
225 arrays
[attr
].Ptr
= (void *) offset
;
228 /* Ptr into ordinary app memory */
229 arrays
[attr
].Ptr
= (void *) data
;
231 arrays
[attr
].Size
= exec
->vtx
.attrsz
[src
];
232 arrays
[attr
].StrideB
= exec
->vtx
.vertex_size
* sizeof(GLfloat
);
233 arrays
[attr
].Stride
= exec
->vtx
.vertex_size
* sizeof(GLfloat
);
234 arrays
[attr
].Type
= GL_FLOAT
;
235 arrays
[attr
].Format
= GL_RGBA
;
236 arrays
[attr
].Enabled
= 1;
237 _mesa_reference_buffer_object(ctx
,
238 &arrays
[attr
].BufferObj
,
239 exec
->vtx
.bufferobj
);
240 arrays
[attr
]._MaxElement
= count
; /* ??? */
242 data
+= exec
->vtx
.attrsz
[src
] * sizeof(GLfloat
);
243 varying_inputs
|= 1 << attr
;
247 _mesa_set_varying_vp_inputs( ctx
, varying_inputs
);
252 vbo_exec_vtx_unmap( struct vbo_exec_context
*exec
)
254 GLenum target
= GL_ARRAY_BUFFER_ARB
;
256 if (_mesa_is_bufferobj(exec
->vtx
.bufferobj
)) {
257 GLcontext
*ctx
= exec
->ctx
;
259 if (ctx
->Driver
.FlushMappedBufferRange
) {
260 GLintptr offset
= exec
->vtx
.buffer_used
- exec
->vtx
.bufferobj
->Offset
;
261 GLsizeiptr length
= (exec
->vtx
.buffer_ptr
- exec
->vtx
.buffer_map
) * sizeof(float);
264 ctx
->Driver
.FlushMappedBufferRange(ctx
, target
,
266 exec
->vtx
.bufferobj
);
269 exec
->vtx
.buffer_used
+= (exec
->vtx
.buffer_ptr
-
270 exec
->vtx
.buffer_map
) * sizeof(float);
272 assert(exec
->vtx
.buffer_used
<= VBO_VERT_BUFFER_SIZE
);
273 assert(exec
->vtx
.buffer_ptr
!= NULL
);
275 ctx
->Driver
.UnmapBuffer(ctx
, target
, exec
->vtx
.bufferobj
);
276 exec
->vtx
.buffer_map
= NULL
;
277 exec
->vtx
.buffer_ptr
= NULL
;
278 exec
->vtx
.max_vert
= 0;
284 vbo_exec_vtx_map( struct vbo_exec_context
*exec
)
286 GLcontext
*ctx
= exec
->ctx
;
287 const GLenum target
= GL_ARRAY_BUFFER_ARB
;
288 const GLenum access
= GL_READ_WRITE_ARB
; /* for MapBuffer */
289 const GLenum accessRange
= GL_MAP_WRITE_BIT
| /* for MapBufferRange */
290 GL_MAP_INVALIDATE_RANGE_BIT
|
291 GL_MAP_UNSYNCHRONIZED_BIT
|
292 GL_MAP_FLUSH_EXPLICIT_BIT
|
294 const GLenum usage
= GL_STREAM_DRAW_ARB
;
296 if (!_mesa_is_bufferobj(exec
->vtx
.bufferobj
))
299 if (exec
->vtx
.buffer_map
!= NULL
) {
301 exec
->vtx
.buffer_map
= NULL
;
302 exec
->vtx
.buffer_ptr
= NULL
;
305 if (VBO_VERT_BUFFER_SIZE
> exec
->vtx
.buffer_used
+ 1024 &&
306 ctx
->Driver
.MapBufferRange
) {
307 exec
->vtx
.buffer_map
=
308 (GLfloat
*)ctx
->Driver
.MapBufferRange(ctx
,
310 exec
->vtx
.buffer_used
,
311 (VBO_VERT_BUFFER_SIZE
-
312 exec
->vtx
.buffer_used
),
314 exec
->vtx
.bufferobj
);
315 exec
->vtx
.buffer_ptr
= exec
->vtx
.buffer_map
;
318 if (!exec
->vtx
.buffer_map
) {
319 exec
->vtx
.buffer_used
= 0;
321 ctx
->Driver
.BufferData(ctx
, target
,
322 VBO_VERT_BUFFER_SIZE
,
323 NULL
, usage
, exec
->vtx
.bufferobj
);
326 if (ctx
->Driver
.MapBufferRange
)
327 exec
->vtx
.buffer_map
=
328 (GLfloat
*)ctx
->Driver
.MapBufferRange(ctx
, target
,
329 0, VBO_VERT_BUFFER_SIZE
,
331 exec
->vtx
.bufferobj
);
332 if (!exec
->vtx
.buffer_map
)
333 exec
->vtx
.buffer_map
=
334 (GLfloat
*)ctx
->Driver
.MapBuffer(ctx
, target
, access
, exec
->vtx
.bufferobj
);
335 assert(exec
->vtx
.buffer_map
);
336 exec
->vtx
.buffer_ptr
= exec
->vtx
.buffer_map
;
340 _mesa_printf("map %d..\n", exec
->vtx
.buffer_used
);
346 * Execute the buffer and save copied verts.
349 vbo_exec_vtx_flush( struct vbo_exec_context
*exec
, GLboolean unmap
)
352 vbo_exec_debug_verts( exec
);
354 if (exec
->vtx
.prim_count
&&
355 exec
->vtx
.vert_count
) {
357 exec
->vtx
.copied
.nr
= vbo_copy_vertices( exec
);
359 if (exec
->vtx
.copied
.nr
!= exec
->vtx
.vert_count
) {
360 GLcontext
*ctx
= exec
->ctx
;
362 /* Before the update_state() as this may raise _NEW_ARRAY
363 * from _mesa_set_varying_vp_inputs().
365 vbo_exec_bind_arrays( ctx
);
368 _mesa_update_state( ctx
);
370 if (_mesa_is_bufferobj(exec
->vtx
.bufferobj
)) {
371 vbo_exec_vtx_unmap( exec
);
375 _mesa_printf("%s %d %d\n", __FUNCTION__
, exec
->vtx
.prim_count
,
376 exec
->vtx
.vert_count
);
378 vbo_context(ctx
)->draw_prims( ctx
,
381 exec
->vtx
.prim_count
,
385 exec
->vtx
.vert_count
- 1);
387 /* If using a real VBO, get new storage -- unless asked not to.
389 if (_mesa_is_bufferobj(exec
->vtx
.bufferobj
) && !unmap
) {
390 vbo_exec_vtx_map( exec
);
395 /* May have to unmap explicitly if we didn't draw:
398 _mesa_is_bufferobj(exec
->vtx
.bufferobj
) &&
399 exec
->vtx
.buffer_map
) {
400 vbo_exec_vtx_unmap( exec
);
404 if (unmap
|| exec
->vtx
.vertex_size
== 0)
405 exec
->vtx
.max_vert
= 0;
407 exec
->vtx
.max_vert
= ((VBO_VERT_BUFFER_SIZE
- exec
->vtx
.buffer_used
) /
408 (exec
->vtx
.vertex_size
* sizeof(GLfloat
)));
410 exec
->vtx
.buffer_ptr
= exec
->vtx
.buffer_map
;
411 exec
->vtx
.prim_count
= 0;
412 exec
->vtx
.vert_count
= 0;