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"
38 static void vbo_exec_debug_verts( struct vbo_exec_context
*exec
)
40 GLuint count
= exec
->vtx
.vert_count
;
43 _mesa_printf("%s: %u vertices %d primitives, %d vertsize\n",
47 exec
->vtx
.vertex_size
);
49 for (i
= 0 ; i
< exec
->vtx
.prim_count
; i
++) {
50 struct _mesa_prim
*prim
= &exec
->vtx
.prim
[i
];
51 _mesa_printf(" prim %d: %s%s %d..%d %s %s\n",
53 _mesa_lookup_enum_by_nr(prim
->mode
),
54 prim
->weak
? " (weak)" : "",
56 prim
->start
+ prim
->count
,
57 prim
->begin
? "BEGIN" : "(wrap)",
58 prim
->end
? "END" : "(wrap)");
64 * NOTE: Need to have calculated primitives by this point -- do it on the fly.
65 * NOTE: Old 'parity' issue is gone.
67 static GLuint
vbo_copy_vertices( struct vbo_exec_context
*exec
)
69 GLuint nr
= exec
->vtx
.prim
[exec
->vtx
.prim_count
-1].count
;
71 GLuint sz
= exec
->vtx
.vertex_size
;
72 GLfloat
*dst
= exec
->vtx
.copied
.buffer
;
73 GLfloat
*src
= (exec
->vtx
.buffer_map
+
74 exec
->vtx
.prim
[exec
->vtx
.prim_count
-1].start
*
75 exec
->vtx
.vertex_size
);
78 switch( exec
->ctx
->Driver
.CurrentExecPrimitive
)
84 for (i
= 0 ; i
< ovf
; i
++)
85 _mesa_memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
* sizeof(GLfloat
) );
89 for (i
= 0 ; i
< ovf
; i
++)
90 _mesa_memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
* sizeof(GLfloat
) );
94 for (i
= 0 ; i
< ovf
; i
++)
95 _mesa_memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
* sizeof(GLfloat
) );
101 _mesa_memcpy( dst
, src
+(nr
-1)*sz
, sz
* sizeof(GLfloat
) );
105 case GL_TRIANGLE_FAN
:
110 _mesa_memcpy( dst
, src
+0, sz
* sizeof(GLfloat
) );
113 _mesa_memcpy( dst
, src
+0, sz
* sizeof(GLfloat
) );
114 _mesa_memcpy( dst
+sz
, src
+(nr
-1)*sz
, sz
* sizeof(GLfloat
) );
117 case GL_TRIANGLE_STRIP
:
118 /* no parity issue, but need to make sure the tri is not drawn twice */
120 exec
->vtx
.prim
[exec
->vtx
.prim_count
-1].count
--;
125 case 0: ovf
= 0; break;
126 case 1: ovf
= 1; break;
127 default: ovf
= 2 + (nr
&1); break;
129 for (i
= 0 ; i
< ovf
; i
++)
130 _mesa_memcpy( dst
+i
*sz
, src
+(nr
-ovf
+i
)*sz
, sz
* sizeof(GLfloat
) );
132 case PRIM_OUTSIDE_BEGIN_END
:
142 /* TODO: populate these as the vertex is defined:
144 static void vbo_exec_bind_arrays( GLcontext
*ctx
)
146 struct vbo_context
*vbo
= vbo_context(ctx
);
147 struct vbo_exec_context
*exec
= &vbo
->exec
;
148 struct gl_client_array
*arrays
= exec
->vtx
.arrays
;
149 GLuint count
= exec
->vtx
.vert_count
;
150 GLubyte
*data
= (GLubyte
*)exec
->vtx
.buffer_map
;
153 GLbitfield varying_inputs
= 0x0;
155 /* Install the default (ie Current) attributes first, then overlay
158 switch (get_program_mode(exec
->ctx
)) {
160 for (attr
= 0; attr
< 16; attr
++) {
161 exec
->vtx
.inputs
[attr
] = &vbo
->legacy_currval
[attr
];
163 for (attr
= 0; attr
< MAT_ATTRIB_MAX
; attr
++) {
164 exec
->vtx
.inputs
[attr
+ 16] = &vbo
->mat_currval
[attr
];
166 map
= vbo
->map_vp_none
;
170 /* The aliasing of attributes for NV vertex programs has already
171 * occurred. NV vertex programs cannot access material values,
172 * nor attributes greater than VERT_ATTRIB_TEX7.
174 for (attr
= 0; attr
< 16; attr
++) {
175 exec
->vtx
.inputs
[attr
] = &vbo
->legacy_currval
[attr
];
176 exec
->vtx
.inputs
[attr
+ 16] = &vbo
->generic_currval
[attr
];
178 map
= vbo
->map_vp_arb
;
180 /* check if VERT_ATTRIB_POS is not read but VERT_BIT_GENERIC0 is read.
181 * In that case we effectively need to route the data from
182 * glVertexAttrib(0, val) calls to feed into the GENERIC0 input.
184 if ((ctx
->VertexProgram
._Current
->Base
.InputsRead
& VERT_BIT_POS
) == 0 &&
185 (ctx
->VertexProgram
._Current
->Base
.InputsRead
& VERT_BIT_GENERIC0
)) {
186 exec
->vtx
.inputs
[16] = exec
->vtx
.inputs
[0];
187 exec
->vtx
.attrsz
[16] = exec
->vtx
.attrsz
[0];
188 exec
->vtx
.attrsz
[0] = 0;
195 /* Make all active attributes (including edgeflag) available as
198 for (attr
= 0; attr
< VERT_ATTRIB_MAX
; attr
++) {
199 const GLuint src
= map
[attr
];
201 if (exec
->vtx
.attrsz
[src
]) {
202 /* override the default array set above */
203 exec
->vtx
.inputs
[attr
] = &arrays
[attr
];
205 if (exec
->vtx
.bufferobj
->Name
) {
206 /* a real buffer obj: Ptr is an offset, not a pointer*/
208 assert(exec
->vtx
.bufferobj
->Pointer
); /* buf should be mapped */
209 offset
= (GLbyte
*) data
- (GLbyte
*) exec
->vtx
.bufferobj
->Pointer
;
211 arrays
[attr
].Ptr
= (void *) offset
;
214 /* Ptr into ordinary app memory */
215 arrays
[attr
].Ptr
= (void *) data
;
217 arrays
[attr
].Size
= exec
->vtx
.attrsz
[src
];
218 arrays
[attr
].StrideB
= exec
->vtx
.vertex_size
* sizeof(GLfloat
);
219 arrays
[attr
].Stride
= exec
->vtx
.vertex_size
* sizeof(GLfloat
);
220 arrays
[attr
].Type
= GL_FLOAT
;
221 arrays
[attr
].Format
= GL_RGBA
;
222 arrays
[attr
].Enabled
= 1;
223 _mesa_reference_buffer_object(ctx
,
224 &arrays
[attr
].BufferObj
,
225 exec
->vtx
.bufferobj
);
226 arrays
[attr
]._MaxElement
= count
; /* ??? */
228 data
+= exec
->vtx
.attrsz
[src
] * sizeof(GLfloat
);
229 varying_inputs
|= 1<<attr
;
233 _mesa_set_varying_vp_inputs( ctx
, varying_inputs
);
237 static void vbo_exec_vtx_unmap( struct vbo_exec_context
*exec
)
239 GLenum target
= GL_ARRAY_BUFFER_ARB
;
241 if (exec
->vtx
.bufferobj
->Name
) {
242 GLcontext
*ctx
= exec
->ctx
;
244 exec
->vtx
.buffer_used
+= (exec
->vtx
.buffer_ptr
-
245 exec
->vtx
.buffer_map
) * sizeof(float);
247 ctx
->Driver
.UnmapBuffer(ctx
, target
, exec
->vtx
.bufferobj
);
248 exec
->vtx
.buffer_map
= NULL
;
249 exec
->vtx
.buffer_ptr
= NULL
;
250 exec
->vtx
.max_vert
= 0;
254 void vbo_exec_vtx_map( struct vbo_exec_context
*exec
)
256 GLcontext
*ctx
= exec
->ctx
;
257 GLenum target
= GL_ARRAY_BUFFER_ARB
;
258 GLenum access
= GL_READ_WRITE_ARB
;
259 GLenum usage
= GL_STREAM_DRAW_ARB
;
261 if (exec
->vtx
.bufferobj
->Name
== 0)
264 if (exec
->vtx
.buffer_map
!= NULL
) {
266 exec
->vtx
.buffer_map
= NULL
;
269 if (VBO_VERT_BUFFER_SIZE
> exec
->vtx
.buffer_used
+ 1024 &&
270 ctx
->Driver
.MapBufferRange
)
272 exec
->vtx
.buffer_map
=
273 (GLfloat
*)ctx
->Driver
.MapBufferRange(ctx
,
275 exec
->vtx
.buffer_used
,
276 (VBO_VERT_BUFFER_SIZE
-
277 exec
->vtx
.buffer_used
),
279 GL_MAP_INVALIDATE_RANGE_BIT
|
280 GL_MAP_UNSYNCHRONIZED_BIT
|
281 MESA_MAP_NOWAIT_BIT
),
282 exec
->vtx
.bufferobj
);
285 if (exec
->vtx
.buffer_map
) {
286 exec
->vtx
.buffer_map
+= exec
->vtx
.buffer_used
/ sizeof(float);
289 exec
->vtx
.buffer_used
= 0;
291 ctx
->Driver
.BufferData(ctx
, target
,
292 VBO_VERT_BUFFER_SIZE
,
293 NULL
, usage
, exec
->vtx
.bufferobj
);
295 exec
->vtx
.buffer_map
=
296 (GLfloat
*)ctx
->Driver
.MapBuffer(ctx
, target
, access
, exec
->vtx
.bufferobj
);
299 if (0) _mesa_printf("map %d..\n", exec
->vtx
.buffer_used
);
305 * Execute the buffer and save copied verts.
307 void vbo_exec_vtx_flush( struct vbo_exec_context
*exec
,
311 vbo_exec_debug_verts( exec
);
314 if (exec
->vtx
.prim_count
&&
315 exec
->vtx
.vert_count
) {
317 exec
->vtx
.copied
.nr
= vbo_copy_vertices( exec
);
319 if (exec
->vtx
.copied
.nr
!= exec
->vtx
.vert_count
) {
320 GLcontext
*ctx
= exec
->ctx
;
322 /* Before the update_state() as this may raise _NEW_ARRAY
323 * from _mesa_set_varying_vp_inputs().
325 vbo_exec_bind_arrays( ctx
);
328 _mesa_update_state( ctx
);
330 if (exec
->vtx
.bufferobj
->Name
) {
331 vbo_exec_vtx_unmap( exec
);
334 if (0) _mesa_printf("%s %d %d\n", __FUNCTION__
, exec
->vtx
.prim_count
,
335 exec
->vtx
.vert_count
);
337 vbo_context(ctx
)->draw_prims( ctx
,
340 exec
->vtx
.prim_count
,
343 exec
->vtx
.vert_count
- 1);
345 /* If using a real VBO, get new storage -- unless asked not to.
347 if (exec
->vtx
.bufferobj
->Name
&& !unmap
) {
348 vbo_exec_vtx_map( exec
);
353 /* May have to unmap explicitly if we didn't draw:
356 exec
->vtx
.bufferobj
->Name
&&
357 exec
->vtx
.buffer_map
) {
358 vbo_exec_vtx_unmap( exec
);
363 exec
->vtx
.max_vert
= 0;
365 exec
->vtx
.max_vert
= ((VBO_VERT_BUFFER_SIZE
- exec
->vtx
.buffer_used
) /
366 (exec
->vtx
.vertex_size
* sizeof(GLfloat
)));
369 exec
->vtx
.buffer_ptr
= exec
->vtx
.buffer_map
;
370 exec
->vtx
.prim_count
= 0;
371 exec
->vtx
.vert_count
= 0;