summaryrefslogtreecommitdiffstats
path: root/src/mesa/vbo/vbo_save_draw.c
blob: c86efcb05eebbded07452b061194e4416ed07ff9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

/* Author:
 *    Keith Whitwell <keithw@vmware.com>
 */

#include "main/glheader.h"
#include "main/bufferobj.h"
#include "main/context.h"
#include "main/imports.h"
#include "main/mtypes.h"
#include "main/macros.h"
#include "main/light.h"
#include "main/state.h"
#include "main/varray.h"
#include "util/bitscan.h"

#include "vbo_private.h"


/**
 * After playback, copy everything but the position from the
 * last vertex to the saved state
 */
static void
playback_copy_to_current(struct gl_context *ctx,
                         const struct vbo_save_vertex_list *node)
{
   struct vbo_context *vbo = vbo_context(ctx);
   fi_type vertex[VBO_ATTRIB_MAX * 4];
   fi_type *data;
   GLbitfield64 mask;

   if (node->current_size == 0)
      return;

   if (node->current_data) {
      data = node->current_data;
   }
   else {
      /* Position of last vertex */
      const GLuint pos = node->vertex_count > 0 ? node->vertex_count - 1 : 0;
      /* Offset to last vertex in the vertex buffer */
      const GLuint offset = node->buffer_offset
         + pos * node->vertex_size * sizeof(GLfloat);

      data = vertex;

      ctx->Driver.GetBufferSubData(ctx, offset,
                                   node->vertex_size * sizeof(GLfloat),
                                   data, node->vertex_store->bufferobj);

      data += node->attrsz[0]; /* skip vertex position */
   }

   mask = node->enabled & (~BITFIELD64_BIT(VBO_ATTRIB_POS));
   while (mask) {
      const int i = u_bit_scan64(&mask);
      fi_type *current = (fi_type *)vbo->currval[i].Ptr;
      fi_type tmp[4];
      assert(node->attrsz[i]);

      COPY_CLEAN_4V_TYPE_AS_UNION(tmp,
                                  node->attrsz[i],
                                  data,
                                  node->attrtype[i]);

      if (node->attrtype[i] != vbo->currval[i].Type ||
          memcmp(current, tmp, 4 * sizeof(GLfloat)) != 0) {
         memcpy(current, tmp, 4 * sizeof(GLfloat));

         vbo->currval[i].Size = node->attrsz[i];
         vbo->currval[i]._ElementSize = vbo->currval[i].Size * sizeof(GLfloat);
         vbo->currval[i].Type = node->attrtype[i];
         vbo->currval[i].Integer =
            vbo_attrtype_to_integer_flag(node->attrtype[i]);

         if (i >= VBO_ATTRIB_FIRST_MATERIAL &&
             i <= VBO_ATTRIB_LAST_MATERIAL)
            ctx->NewState |= _NEW_LIGHT;

         ctx->NewState |= _NEW_CURRENT_ATTRIB;
      }

      data += node->attrsz[i];
   }

   /* Colormaterial -- this kindof sucks.
    */
   if (ctx->Light.ColorMaterialEnabled) {
      _mesa_update_color_material(ctx, ctx->Current.Attrib[VBO_ATTRIB_COLOR0]);
   }

   /* CurrentExecPrimitive
    */
   if (node->prim_count) {
      const struct _mesa_prim *prim = &node->prims[node->prim_count - 1];
      if (prim->end)
         ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
      else
         ctx->Driver.CurrentExecPrimitive = prim->mode;
   }
}



/**
 * Treat the vertex storage as a VBO, define vertex arrays pointing
 * into it:
 */
static void
bind_vertex_list(struct gl_context *ctx,
                 const struct vbo_save_vertex_list *node)
{
   struct vbo_context *vbo = vbo_context(ctx);
   struct vbo_save_context *save = &vbo->save;
   struct gl_vertex_array *arrays = save->arrays;
   GLuint buffer_offset = node->buffer_offset;
   const GLubyte *map;
   GLuint attr;
   GLubyte node_attrsz[VBO_ATTRIB_MAX];  /* copy of node->attrsz[] */
   GLenum node_attrtype[VBO_ATTRIB_MAX];  /* copy of node->attrtype[] */
   GLbitfield varying_inputs = 0x0;

   memcpy(node_attrsz, node->attrsz, sizeof(node->attrsz));
   memcpy(node_attrtype, node->attrtype, sizeof(node->attrtype));

   if (aligned_vertex_buffer_offset(node)) {
      /* The vertex size is an exact multiple of the buffer offset.
       * This means that we can use zero-based vertex attribute pointers
       * and specify the start of the primitive with the _mesa_prim::start
       * field.  This results in issuing several draw calls with identical
       * vertex attribute information.  This can result in fewer state
       * changes in drivers.  In particular, the Gallium CSO module will
       * filter out redundant vertex buffer changes.
       */
      buffer_offset = 0;
   }

   /* Install the default (ie Current) attributes first */
   for (attr = 0; attr < VERT_ATTRIB_FF_MAX; attr++) {
      save->inputs[attr] = &vbo->currval[VBO_ATTRIB_POS + attr];
   }

   /* Overlay other active attributes */
   switch (get_vp_mode(ctx)) {
   case VP_FF:
      /* Point the generic attributes at the legacy material values */
      for (attr = 0; attr < MAT_ATTRIB_MAX; attr++) {
         save->inputs[VERT_ATTRIB_GENERIC(attr)] =
            &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT+attr];
      }
      map = vbo->map_vp_none;
      break;
   case VP_SHADER:
      for (attr = 0; attr < VERT_ATTRIB_GENERIC_MAX; attr++) {
         save->inputs[VERT_ATTRIB_GENERIC(attr)] =
            &vbo->currval[VBO_ATTRIB_GENERIC0+attr];
      }
      map = vbo->map_vp_arb;

      /* check if VERT_ATTRIB_POS is not read but VERT_BIT_GENERIC0 is read.
       * In that case we effectively need to route the data from
       * glVertexAttrib(0, val) calls to feed into the GENERIC0 input.
       */
      const GLbitfield64 inputs_read =
         ctx->VertexProgram._Current->info.inputs_read;
      if ((inputs_read & VERT_BIT_POS) == 0 &&
          (inputs_read & VERT_BIT_GENERIC0)) {
         save->inputs[VERT_ATTRIB_GENERIC0] = save->inputs[0];
         node_attrsz[VERT_ATTRIB_GENERIC0] = node_attrsz[0];
         node_attrtype[VERT_ATTRIB_GENERIC0] = node_attrtype[0];
         node_attrsz[0] = 0;
      }
      break;
   default:
      unreachable("Bad vertex program mode");
   }

   for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
      const GLuint src = map[attr];

      if (node_attrsz[src]) {
         struct gl_vertex_array *array = &arrays[attr];

         /* override the default array set above */
         save->inputs[attr] = array;

         array->Ptr = (const GLubyte *) NULL + buffer_offset;
         array->Size = node_attrsz[src];
         array->StrideB = node->vertex_size * sizeof(GLfloat);
         array->Type = node_attrtype[src];
         array->Integer = vbo_attrtype_to_integer_flag(node_attrtype[src]);
         array->Format = GL_RGBA;
         array->_ElementSize = array->Size * sizeof(GLfloat);
         _mesa_reference_buffer_object(ctx,
                                       &array->BufferObj,
                                       node->vertex_store->bufferobj);

         assert(array->BufferObj->Name);

         buffer_offset += node_attrsz[src] * sizeof(GLfloat);
         varying_inputs |= VERT_BIT(attr);
      }
   }

   _mesa_set_varying_vp_inputs(ctx, varying_inputs);
   ctx->NewDriverState |= ctx->DriverFlags.NewArray;
}


static void
loopback_vertex_list(struct gl_context *ctx,
                     const struct vbo_save_vertex_list *list)
{
   const char *buffer =
      ctx->Driver.MapBufferRange(ctx, 0,
                                 list->vertex_store->bufferobj->Size,
                                 GL_MAP_READ_BIT, /* ? */
                                 list->vertex_store->bufferobj,
                                 MAP_INTERNAL);

   unsigned buffer_offset =
      aligned_vertex_buffer_offset(list) ? 0 : list->buffer_offset;

   vbo_loopback_vertex_list(ctx,
                            (const GLfloat *) (buffer + buffer_offset),
                            list->attrsz,
                            list->prims,
                            list->prim_count,
                            list->wrap_count,
                            list->vertex_size);

   ctx->Driver.UnmapBuffer(ctx, list->vertex_store->bufferobj,
                           MAP_INTERNAL);
}


/**
 * Execute the buffer and save copied verts.
 * This is called from the display list code when executing
 * a drawing command.
 */
void
vbo_save_playback_vertex_list(struct gl_context *ctx, void *data)
{
   const struct vbo_save_vertex_list *node =
      (const struct vbo_save_vertex_list *) data;
   struct vbo_context *vbo = vbo_context(ctx);
   struct vbo_save_context *save = &vbo->save;
   GLboolean remap_vertex_store = GL_FALSE;

   if (save->vertex_store && save->vertex_store->buffer_map) {
      /* The vertex store is currently mapped but we're about to replay
       * a display list.  This can happen when a nested display list is
       * being build with GL_COMPILE_AND_EXECUTE.
       * We never want to have mapped vertex buffers when we're drawing.
       * Unmap the vertex store, execute the list, then remap the vertex
       * store.
       */
      vbo_save_unmap_vertex_store(ctx, save->vertex_store);
      remap_vertex_store = GL_TRUE;
   }

   FLUSH_CURRENT(ctx, 0);

   if (node->prim_count > 0) {

      if (_mesa_inside_begin_end(ctx) && node->prims[0].begin) {
         /* Error: we're about to begin a new primitive but we're already
          * inside a glBegin/End pair.
          */
         _mesa_error(ctx, GL_INVALID_OPERATION,
                     "draw operation inside glBegin/End");
         goto end;
      }
      else if (save->replay_flags) {
         /* Various degenerate cases: translate into immediate mode
          * calls rather than trying to execute in place.
          */
         loopback_vertex_list(ctx, node);

         goto end;
      }

      if (ctx->NewState)
         _mesa_update_state(ctx);

      /* XXX also need to check if shader enabled, but invalid */
      if ((ctx->VertexProgram.Enabled &&
           !_mesa_arb_vertex_program_enabled(ctx)) ||
          (ctx->FragmentProgram.Enabled &&
           !_mesa_arb_fragment_program_enabled(ctx))) {
         _mesa_error(ctx, GL_INVALID_OPERATION,
                     "glBegin (invalid vertex/fragment program)");
         return;
      }

      bind_vertex_list(ctx, node);

      _mesa_set_drawing_arrays(ctx, vbo->save.inputs);

      /* Again...
       */
      if (ctx->NewState)
         _mesa_update_state(ctx);

      if (node->vertex_count > 0) {
         GLuint min_index = node->start_vertex;
         GLuint max_index = min_index + node->vertex_count - 1;
         vbo_context(ctx)->draw_prims(ctx,
                                      node->prims,
                                      node->prim_count,
                                      NULL,
                                      GL_TRUE,
                                      min_index, max_index,
                                      NULL, 0, NULL);
      }
   }

   /* Copy to current?
    */
   playback_copy_to_current(ctx, node);

end:
   if (remap_vertex_store) {
      save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
   }
}