1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
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.
23  *
24  * Authors:
25  *    Keith Whitwell <keithw@vmware.com>
26  */
27 
28 #include <stdbool.h>
29 #include <stdio.h>
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"
38 
39 #include "vbo_noop.h"
40 #include "vbo_private.h"
41 
42 
43 static void
vbo_exec_debug_verts(struct vbo_exec_context * exec)44 vbo_exec_debug_verts(struct vbo_exec_context *exec)
45 {
46    GLuint count = exec->vtx.vert_count;
47    GLuint i;
48 
49    printf("%s: %u vertices %d primitives, %d vertsize\n",
50           __func__,
51           count,
52           exec->vtx.prim_count,
53           exec->vtx.vertex_size);
54 
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",
58              i,
59              _mesa_lookup_prim_by_nr(prim->mode),
60              prim->start,
61              prim->start + prim->count,
62              prim->begin ? "BEGIN" : "(wrap)",
63              prim->end ? "END" : "(wrap)");
64    }
65 }
66 
67 
68 static GLuint
vbo_exec_copy_vertices(struct vbo_exec_context * exec)69 vbo_exec_copy_vertices(struct vbo_exec_context *exec)
70 {
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;
75 
76    return vbo_copy_vertices(exec->ctx, exec->ctx->Driver.CurrentExecPrimitive,
77                             last_prim, sz, false, dst, src);
78 }
79 
80 
81 
82 /* TODO: populate these as the vertex is defined:
83  */
84 static void
vbo_exec_bind_arrays(struct gl_context * ctx)85 vbo_exec_bind_arrays(struct gl_context *ctx)
86 {
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;
90 
91    GLintptr buffer_offset;
92    if (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;
96    } else {
97       /* Ptr into ordinary app memory */
98       buffer_offset = (GLbyte *)exec->vtx.buffer_map - (GLbyte *)NULL;
99    }
100 
101    const gl_vertex_processing_mode mode = ctx->VertexProgram._VPMode;
102 
103    /* Compute the bitmasks of vao_enabled arrays */
104    GLbitfield vao_enabled = _vbo_get_vao_enabled_from_vbo(mode, exec->vtx.enabled);
105 
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);
109 
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,
113                             stride, false, false);
114 
115    /* Retrieve the mapping from VBO_ATTRIB to VERT_ATTRIB space
116     * Note that the position/generic0 aliasing is done in the VAO.
117     */
118    const GLubyte *const vao_to_vbo_map = _vbo_attribute_alias_map[mode];
119    /* Now set the enabled arrays */
120    GLbitfield mask = vao_enabled;
121    while (mask) {
122       const int vao_attr = u_bit_scan(&mask);
123       const GLubyte vbo_attr = vao_to_vbo_map[vao_attr];
124 
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);
130 
131       /* Set and enable */
132       _vbo_set_attrib_format(ctx, vao, vao_attr, buffer_offset,
133                              size, type, offset);
134 
135       /* The vao is initially created with all bindings set to 0. */
136       assert(vao->VertexAttrib[vao_attr].BufferBindingIndex == 0);
137    }
138    _mesa_enable_vertex_array_attribs(ctx, vao, vao_enabled);
139    assert(vao_enabled == vao->Enabled);
140    assert(!exec->vtx.bufferobj ||
141           (vao_enabled & ~vao->VertexAttribBufferMask) == 0);
142 
143    _mesa_set_draw_vao(ctx, vao, _vbo_get_vao_filter(mode));
144 }
145 
146 
147 /**
148  * Unmap the VBO.  This is called before drawing.
149  */
150 static void
vbo_exec_vtx_unmap(struct vbo_exec_context * exec)151 vbo_exec_vtx_unmap(struct vbo_exec_context *exec)
152 {
153    if (exec->vtx.bufferobj) {
154       struct gl_context *ctx = exec->ctx;
155 
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) *
161                              sizeof(float);
162 
163          if (length)
164             ctx->Driver.FlushMappedBufferRange(ctx, offset, length,
165                                                exec->vtx.bufferobj,
166                                                MAP_INTERNAL);
167       }
168 
169       exec->vtx.buffer_used += (exec->vtx.buffer_ptr -
170                                 exec->vtx.buffer_map) * sizeof(float);
171 
172       assert(exec->vtx.buffer_used <= exec->ctx->Const.glBeginEndBufferSize);
173       assert(exec->vtx.buffer_ptr != NULL);
174 
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;
179    }
180 }
181 
182 static bool
vbo_exec_buffer_has_space(struct vbo_exec_context * exec)183 vbo_exec_buffer_has_space(struct vbo_exec_context *exec)
184 {
185    return exec->ctx->Const.glBeginEndBufferSize > exec->vtx.buffer_used + 1024;
186 }
187 
188 
189 /**
190  * Map the vertex buffer to begin storing glVertex, glColor, etc data.
191  */
192 void
vbo_exec_vtx_map(struct vbo_exec_context * exec)193 vbo_exec_vtx_map(struct vbo_exec_context *exec)
194 {
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;
199 
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.
204        */
205       accessRange |= GL_MAP_PERSISTENT_BIT |
206                      GL_MAP_COHERENT_BIT |
207                      GL_MAP_READ_BIT;
208    } else {
209       accessRange |= GL_MAP_INVALIDATE_RANGE_BIT |
210                      GL_MAP_FLUSH_EXPLICIT_BIT |
211                      MESA_MAP_NOWAIT_BIT;
212    }
213 
214    if (!exec->vtx.bufferobj)
215       return;
216 
217    assert(!exec->vtx.buffer_map);
218    assert(!exec->vtx.buffer_ptr);
219 
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,
226                                        ctx->Const.glBeginEndBufferSize
227                                        - exec->vtx.buffer_used,
228                                        accessRange,
229                                        exec->vtx.bufferobj,
230                                        MAP_INTERNAL);
231          exec->vtx.buffer_ptr = exec->vtx.buffer_map;
232       }
233       else {
234          exec->vtx.buffer_ptr = exec->vtx.buffer_map = NULL;
235       }
236    }
237 
238    if (!exec->vtx.buffer_map) {
239       /* Need to allocate a new VBO */
240       exec->vtx.buffer_used = 0;
241 
242       if (ctx->Driver.BufferData(ctx, GL_ARRAY_BUFFER_ARB,
243                                  ctx->Const.glBeginEndBufferSize,
244                                  NULL, usage,
245                                  GL_MAP_WRITE_BIT |
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, ctx->Const.glBeginEndBufferSize,
257                                                   accessRange,
258                                                   exec->vtx.bufferobj,
259                                                   MAP_INTERNAL);
260       }
261       else {
262          _mesa_error(ctx, GL_OUT_OF_MEMORY, "VBO allocation");
263          exec->vtx.buffer_map = NULL;
264       }
265    }
266 
267    exec->vtx.buffer_ptr = exec->vtx.buffer_map;
268    exec->vtx.buffer_offset = 0;
269 
270    if (!exec->vtx.buffer_map) {
271       /* out of memory */
272       _mesa_install_exec_vtxfmt(ctx, &exec->vtxfmt_noop);
273    }
274    else {
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().
279           */
280          _mesa_install_exec_vtxfmt(ctx, &exec->vtxfmt);
281       }
282    }
283 
284    if (0)
285       printf("map %d..\n", exec->vtx.buffer_used);
286 }
287 
288 
289 
290 /**
291  * Execute the buffer and save copied verts.
292  */
293 void
vbo_exec_vtx_flush(struct vbo_exec_context * exec)294 vbo_exec_vtx_flush(struct vbo_exec_context *exec)
295 {
296    /* Only unmap if persistent mappings are unsupported. */
297    bool persistent_mapping = exec->ctx->Extensions.ARB_buffer_storage &&
298                              exec->vtx.bufferobj &&
299                              exec->vtx.buffer_map;
300 
301    if (0)
302       vbo_exec_debug_verts(exec);
303 
304    if (exec->vtx.prim_count &&
305        exec->vtx.vert_count) {
306 
307       exec->vtx.copied.nr = vbo_exec_copy_vertices(exec);
308 
309       if (exec->vtx.copied.nr != exec->vtx.vert_count) {
310          struct gl_context *ctx = exec->ctx;
311 
312          /* Prepare and set the exec draws internal VAO for drawing. */
313          vbo_exec_bind_arrays(ctx);
314 
315          if (ctx->NewState)
316             _mesa_update_state(ctx);
317 
318          if (!persistent_mapping)
319             vbo_exec_vtx_unmap(exec);
320 
321          assert(ctx->NewState == 0);
322 
323          if (0)
324             printf("%s %d %d\n", __func__, exec->vtx.prim_count,
325                    exec->vtx.vert_count);
326 
327          ctx->Driver.Draw(ctx, exec->vtx.prim, exec->vtx.prim_count,
328                           NULL, GL_TRUE, 0, exec->vtx.vert_count - 1, 1, 0,
329                           NULL, 0);
330 
331          /* Get new storage -- unless asked not to. */
332          if (!persistent_mapping)
333             vbo_exec_vtx_map(exec);
334       }
335    }
336 
337    if (persistent_mapping) {
338       exec->vtx.buffer_used += (exec->vtx.buffer_ptr - exec->vtx.buffer_map) *
339                                sizeof(float);
340       exec->vtx.buffer_map = exec->vtx.buffer_ptr;
341 
342       /* Set the buffer offset for the next draw. */
343       exec->vtx.buffer_offset = exec->vtx.buffer_used;
344 
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);
349       }
350    }
351 
352    if (exec->vtx.vertex_size == 0)
353       exec->vtx.max_vert = 0;
354    else
355       exec->vtx.max_vert = vbo_compute_max_verts(exec);
356 
357    exec->vtx.buffer_ptr = exec->vtx.buffer_map;
358    exec->vtx.prim_count = 0;
359    exec->vtx.vert_count = 0;
360 }
361