2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 * Keith Whitwell <keithw@vmware.com>
30 #include "main/arrayobj.h"
31 #include "main/glheader.h"
32 #include "main/bufferobj.h"
33 #include "main/context.h"
34 #include "main/enums.h"
35 #include "main/state.h"
36 #include "main/varray.h"
37 #include "main/vtxfmt.h"
40 #include "vbo_private.h"
44 vbo_exec_debug_verts(struct vbo_exec_context
*exec
)
46 GLuint count
= exec
->vtx
.vert_count
;
49 printf("%s: %u vertices %d primitives, %d vertsize\n",
53 exec
->vtx
.vertex_size
);
55 for (i
= 0 ; i
< exec
->vtx
.prim_count
; i
++) {
56 struct _mesa_prim
*prim
= &exec
->vtx
.prim
[i
];
57 printf(" prim %d: %s %d..%d %s %s\n",
59 _mesa_lookup_prim_by_nr(prim
->mode
),
61 prim
->start
+ prim
->count
,
62 prim
->begin
? "BEGIN" : "(wrap)",
63 prim
->end
? "END" : "(wrap)");
69 vbo_exec_copy_vertices(struct vbo_exec_context
*exec
)
71 struct _mesa_prim
*last_prim
= &exec
->vtx
.prim
[exec
->vtx
.prim_count
- 1];
72 const GLuint sz
= exec
->vtx
.vertex_size
;
73 fi_type
*dst
= exec
->vtx
.copied
.buffer
;
74 const fi_type
*src
= exec
->vtx
.buffer_map
+ last_prim
->start
* sz
;
76 return vbo_copy_vertices(exec
->ctx
, exec
->ctx
->Driver
.CurrentExecPrimitive
,
77 last_prim
, sz
, false, dst
, src
);
82 /* TODO: populate these as the vertex is defined:
85 vbo_exec_bind_arrays(struct gl_context
*ctx
)
87 struct vbo_context
*vbo
= vbo_context(ctx
);
88 struct gl_vertex_array_object
*vao
= vbo
->VAO
;
89 struct vbo_exec_context
*exec
= &vbo
->exec
;
91 GLintptr buffer_offset
;
92 if (_mesa_is_bufferobj(exec
->vtx
.bufferobj
)) {
93 assert(exec
->vtx
.bufferobj
->Mappings
[MAP_INTERNAL
].Pointer
);
94 buffer_offset
= exec
->vtx
.bufferobj
->Mappings
[MAP_INTERNAL
].Offset
+
95 exec
->vtx
.buffer_offset
;
97 /* Ptr into ordinary app memory */
98 buffer_offset
= (GLbyte
*)exec
->vtx
.buffer_map
- (GLbyte
*)NULL
;
101 const gl_vertex_processing_mode mode
= ctx
->VertexProgram
._VPMode
;
103 /* Compute the bitmasks of vao_enabled arrays */
104 GLbitfield vao_enabled
= _vbo_get_vao_enabled_from_vbo(mode
, exec
->vtx
.enabled
);
106 /* At first disable arrays no longer needed */
107 _mesa_disable_vertex_array_attribs(ctx
, vao
, VERT_BIT_ALL
& ~vao_enabled
);
108 assert((~vao_enabled
& vao
->Enabled
) == 0);
110 /* Bind the buffer object */
111 const GLuint stride
= exec
->vtx
.vertex_size
*sizeof(GLfloat
);
112 _mesa_bind_vertex_buffer(ctx
, vao
, 0, exec
->vtx
.bufferobj
, buffer_offset
,
115 /* Retrieve the mapping from VBO_ATTRIB to VERT_ATTRIB space
116 * Note that the position/generic0 aliasing is done in the VAO.
118 const GLubyte
*const vao_to_vbo_map
= _vbo_attribute_alias_map
[mode
];
119 /* Now set the enabled arrays */
120 GLbitfield mask
= vao_enabled
;
122 const int vao_attr
= u_bit_scan(&mask
);
123 const GLubyte vbo_attr
= vao_to_vbo_map
[vao_attr
];
125 const GLubyte size
= exec
->vtx
.attr
[vbo_attr
].size
;
126 const GLenum16 type
= exec
->vtx
.attr
[vbo_attr
].type
;
127 const GLuint offset
= (GLuint
)((GLbyte
*)exec
->vtx
.attrptr
[vbo_attr
] -
128 (GLbyte
*)exec
->vtx
.vertex
);
129 assert(offset
<= ctx
->Const
.MaxVertexAttribRelativeOffset
);
132 _vbo_set_attrib_format(ctx
, vao
, vao_attr
, buffer_offset
,
135 /* The vao is initially created with all bindings set to 0. */
136 assert(vao
->VertexAttrib
[vao_attr
].BufferBindingIndex
== 0);
138 _mesa_enable_vertex_array_attribs(ctx
, vao
, vao_enabled
);
139 assert(vao_enabled
== vao
->Enabled
);
140 assert(!_mesa_is_bufferobj(exec
->vtx
.bufferobj
) ||
141 (vao_enabled
& ~vao
->VertexAttribBufferMask
) == 0);
143 _mesa_set_draw_vao(ctx
, vao
, _vbo_get_vao_filter(mode
));
148 * Unmap the VBO. This is called before drawing.
151 vbo_exec_vtx_unmap(struct vbo_exec_context
*exec
)
153 if (_mesa_is_bufferobj(exec
->vtx
.bufferobj
)) {
154 struct gl_context
*ctx
= exec
->ctx
;
156 if (ctx
->Driver
.FlushMappedBufferRange
&&
157 !ctx
->Extensions
.ARB_buffer_storage
) {
158 GLintptr offset
= exec
->vtx
.buffer_used
-
159 exec
->vtx
.bufferobj
->Mappings
[MAP_INTERNAL
].Offset
;
160 GLsizeiptr length
= (exec
->vtx
.buffer_ptr
- exec
->vtx
.buffer_map
) *
164 ctx
->Driver
.FlushMappedBufferRange(ctx
, offset
, length
,
169 exec
->vtx
.buffer_used
+= (exec
->vtx
.buffer_ptr
-
170 exec
->vtx
.buffer_map
) * sizeof(float);
172 assert(exec
->vtx
.buffer_used
<= VBO_VERT_BUFFER_SIZE
);
173 assert(exec
->vtx
.buffer_ptr
!= NULL
);
175 ctx
->Driver
.UnmapBuffer(ctx
, exec
->vtx
.bufferobj
, MAP_INTERNAL
);
176 exec
->vtx
.buffer_map
= NULL
;
177 exec
->vtx
.buffer_ptr
= NULL
;
178 exec
->vtx
.max_vert
= 0;
183 vbo_exec_buffer_has_space(struct vbo_exec_context
*exec
)
185 return VBO_VERT_BUFFER_SIZE
> exec
->vtx
.buffer_used
+ 1024;
190 * Map the vertex buffer to begin storing glVertex, glColor, etc data.
193 vbo_exec_vtx_map(struct vbo_exec_context
*exec
)
195 struct gl_context
*ctx
= exec
->ctx
;
196 const GLenum usage
= GL_STREAM_DRAW_ARB
;
197 GLenum accessRange
= GL_MAP_WRITE_BIT
| /* for MapBufferRange */
198 GL_MAP_UNSYNCHRONIZED_BIT
;
200 if (ctx
->Extensions
.ARB_buffer_storage
) {
201 /* We sometimes read from the buffer, so map it for read too.
202 * Only the persistent mapping can do that, because the non-persistent
203 * mapping uses flags that are incompatible with GL_MAP_READ_BIT.
205 accessRange
|= GL_MAP_PERSISTENT_BIT
|
206 GL_MAP_COHERENT_BIT
|
209 accessRange
|= GL_MAP_INVALIDATE_RANGE_BIT
|
210 GL_MAP_FLUSH_EXPLICIT_BIT
|
214 if (!_mesa_is_bufferobj(exec
->vtx
.bufferobj
))
217 assert(!exec
->vtx
.buffer_map
);
218 assert(!exec
->vtx
.buffer_ptr
);
220 if (vbo_exec_buffer_has_space(exec
)) {
221 /* The VBO exists and there's room for more */
222 if (exec
->vtx
.bufferobj
->Size
> 0) {
223 exec
->vtx
.buffer_map
= (fi_type
*)
224 ctx
->Driver
.MapBufferRange(ctx
,
225 exec
->vtx
.buffer_used
,
227 - exec
->vtx
.buffer_used
,
231 exec
->vtx
.buffer_ptr
= exec
->vtx
.buffer_map
;
234 exec
->vtx
.buffer_ptr
= exec
->vtx
.buffer_map
= NULL
;
238 if (!exec
->vtx
.buffer_map
) {
239 /* Need to allocate a new VBO */
240 exec
->vtx
.buffer_used
= 0;
242 if (ctx
->Driver
.BufferData(ctx
, GL_ARRAY_BUFFER_ARB
,
243 VBO_VERT_BUFFER_SIZE
,
246 (ctx
->Extensions
.ARB_buffer_storage
?
247 GL_MAP_PERSISTENT_BIT
|
248 GL_MAP_COHERENT_BIT
|
249 GL_MAP_READ_BIT
: 0) |
250 GL_DYNAMIC_STORAGE_BIT
|
251 GL_CLIENT_STORAGE_BIT
,
252 exec
->vtx
.bufferobj
)) {
253 /* buffer allocation worked, now map the buffer */
254 exec
->vtx
.buffer_map
=
255 (fi_type
*)ctx
->Driver
.MapBufferRange(ctx
,
256 0, VBO_VERT_BUFFER_SIZE
,
262 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "VBO allocation");
263 exec
->vtx
.buffer_map
= NULL
;
267 exec
->vtx
.buffer_ptr
= exec
->vtx
.buffer_map
;
268 exec
->vtx
.buffer_offset
= 0;
270 if (!exec
->vtx
.buffer_map
) {
272 _mesa_install_exec_vtxfmt(ctx
, &exec
->vtxfmt_noop
);
275 if (_mesa_using_noop_vtxfmt(ctx
->Exec
)) {
276 /* The no-op functions are installed so switch back to regular
277 * functions. We do this test just to avoid frequent and needless
278 * calls to _mesa_install_exec_vtxfmt().
280 _mesa_install_exec_vtxfmt(ctx
, &exec
->vtxfmt
);
285 printf("map %d..\n", exec
->vtx
.buffer_used
);
291 * Execute the buffer and save copied verts.
294 vbo_exec_vtx_flush(struct vbo_exec_context
*exec
)
296 /* Only unmap if persistent mappings are unsupported. */
297 bool persistent_mapping
= exec
->ctx
->Extensions
.ARB_buffer_storage
&&
298 _mesa_is_bufferobj(exec
->vtx
.bufferobj
) &&
299 exec
->vtx
.buffer_map
;
302 vbo_exec_debug_verts(exec
);
304 if (exec
->vtx
.prim_count
&&
305 exec
->vtx
.vert_count
) {
307 exec
->vtx
.copied
.nr
= vbo_exec_copy_vertices(exec
);
309 if (exec
->vtx
.copied
.nr
!= exec
->vtx
.vert_count
) {
310 struct gl_context
*ctx
= exec
->ctx
;
312 /* Prepare and set the exec draws internal VAO for drawing. */
313 vbo_exec_bind_arrays(ctx
);
316 _mesa_update_state(ctx
);
318 if (!persistent_mapping
)
319 vbo_exec_vtx_unmap(exec
);
321 assert(ctx
->NewState
== 0);
324 printf("%s %d %d\n", __func__
, exec
->vtx
.prim_count
,
325 exec
->vtx
.vert_count
);
327 ctx
->Driver
.Draw(ctx
, exec
->vtx
.prim
, exec
->vtx
.prim_count
,
328 NULL
, GL_TRUE
, 0, exec
->vtx
.vert_count
- 1, 1, 0,
331 /* Get new storage -- unless asked not to. */
332 if (!persistent_mapping
)
333 vbo_exec_vtx_map(exec
);
337 if (persistent_mapping
) {
338 exec
->vtx
.buffer_used
+= (exec
->vtx
.buffer_ptr
- exec
->vtx
.buffer_map
) *
340 exec
->vtx
.buffer_map
= exec
->vtx
.buffer_ptr
;
342 /* Set the buffer offset for the next draw. */
343 exec
->vtx
.buffer_offset
= exec
->vtx
.buffer_used
;
345 if (!vbo_exec_buffer_has_space(exec
)) {
346 /* This will allocate a new buffer. */
347 vbo_exec_vtx_unmap(exec
);
348 vbo_exec_vtx_map(exec
);
352 if (exec
->vtx
.vertex_size
== 0)
353 exec
->vtx
.max_vert
= 0;
355 exec
->vtx
.max_vert
= vbo_compute_max_verts(exec
);
357 exec
->vtx
.buffer_ptr
= exec
->vtx
.buffer_map
;
358 exec
->vtx
.prim_count
= 0;
359 exec
->vtx
.vert_count
= 0;