vbo: Implement vbo_loopback_vertex_list in terms of the VAO.
[mesa.git] / src / mesa / vbo / vbo_save_draw.c
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
25 /* Author:
26 * Keith Whitwell <keithw@vmware.com>
27 */
28
29 #include <stdbool.h>
30 #include "main/arrayobj.h"
31 #include "main/glheader.h"
32 #include "main/bufferobj.h"
33 #include "main/context.h"
34 #include "main/imports.h"
35 #include "main/mtypes.h"
36 #include "main/macros.h"
37 #include "main/light.h"
38 #include "main/state.h"
39 #include "main/varray.h"
40 #include "util/bitscan.h"
41
42 #include "vbo_private.h"
43
44
45 /**
46 * After playback, copy everything but the position from the
47 * last vertex to the saved state
48 */
49 static void
50 playback_copy_to_current(struct gl_context *ctx,
51 const struct vbo_save_vertex_list *node)
52 {
53 struct vbo_context *vbo = vbo_context(ctx);
54 fi_type vertex[VBO_ATTRIB_MAX * 4];
55 fi_type *data;
56 GLbitfield64 mask;
57
58 if (node->current_size == 0)
59 return;
60
61 if (node->current_data) {
62 data = node->current_data;
63 }
64 else {
65 /* Position of last vertex */
66 const GLuint pos = node->vertex_count > 0 ? node->vertex_count - 1 : 0;
67 /* Offset to last vertex in the vertex buffer */
68 const GLuint offset = node->buffer_offset
69 + pos * node->vertex_size * sizeof(GLfloat);
70
71 data = vertex;
72
73 ctx->Driver.GetBufferSubData(ctx, offset,
74 node->vertex_size * sizeof(GLfloat),
75 data, node->vertex_store->bufferobj);
76
77 data += node->attrsz[0]; /* skip vertex position */
78 }
79
80 mask = node->enabled & (~BITFIELD64_BIT(VBO_ATTRIB_POS));
81 while (mask) {
82 const int i = u_bit_scan64(&mask);
83 fi_type *current = (fi_type *)vbo->currval[i].Ptr;
84 fi_type tmp[4];
85 assert(node->attrsz[i]);
86
87 COPY_CLEAN_4V_TYPE_AS_UNION(tmp,
88 node->attrsz[i],
89 data,
90 node->attrtype[i]);
91
92 if (node->attrtype[i] != vbo->currval[i].Type ||
93 memcmp(current, tmp, 4 * sizeof(GLfloat)) != 0) {
94 memcpy(current, tmp, 4 * sizeof(GLfloat));
95
96 vbo->currval[i].Size = node->attrsz[i];
97 vbo->currval[i]._ElementSize = vbo->currval[i].Size * sizeof(GLfloat);
98 vbo->currval[i].Type = node->attrtype[i];
99 vbo->currval[i].Integer =
100 vbo_attrtype_to_integer_flag(node->attrtype[i]);
101
102 if (i >= VBO_ATTRIB_FIRST_MATERIAL &&
103 i <= VBO_ATTRIB_LAST_MATERIAL)
104 ctx->NewState |= _NEW_LIGHT;
105
106 ctx->NewState |= _NEW_CURRENT_ATTRIB;
107 }
108
109 data += node->attrsz[i];
110 }
111
112 /* Colormaterial -- this kindof sucks.
113 */
114 if (ctx->Light.ColorMaterialEnabled) {
115 _mesa_update_color_material(ctx, ctx->Current.Attrib[VBO_ATTRIB_COLOR0]);
116 }
117
118 /* CurrentExecPrimitive
119 */
120 if (node->prim_count) {
121 const struct _mesa_prim *prim = &node->prims[node->prim_count - 1];
122 if (prim->end)
123 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
124 else
125 ctx->Driver.CurrentExecPrimitive = prim->mode;
126 }
127 }
128
129
130
131 /**
132 * Set the appropriate VAO to draw.
133 */
134 static void
135 bind_vertex_list(struct gl_context *ctx,
136 const struct vbo_save_vertex_list *node)
137 {
138 const gl_vertex_processing_mode mode = ctx->VertexProgram._VPMode;
139 _mesa_set_draw_vao(ctx, node->VAO[mode], _vbo_get_vao_filter(mode));
140 }
141
142
143 static void
144 loopback_vertex_list(struct gl_context *ctx,
145 const struct vbo_save_vertex_list *list)
146 {
147 struct gl_buffer_object *bo = list->VAO[0]->BufferBinding[0].BufferObj;
148 ctx->Driver.MapBufferRange(ctx, 0, bo->Size, GL_MAP_READ_BIT, /* ? */
149 bo, MAP_INTERNAL);
150
151 /* Note that the range of referenced vertices must be mapped already */
152 _vbo_loopback_vertex_list(ctx, list);
153
154 ctx->Driver.UnmapBuffer(ctx, bo, MAP_INTERNAL);
155 }
156
157
158 /**
159 * Execute the buffer and save copied verts.
160 * This is called from the display list code when executing
161 * a drawing command.
162 */
163 void
164 vbo_save_playback_vertex_list(struct gl_context *ctx, void *data)
165 {
166 const struct vbo_save_vertex_list *node =
167 (const struct vbo_save_vertex_list *) data;
168 struct vbo_context *vbo = vbo_context(ctx);
169 struct vbo_save_context *save = &vbo->save;
170 GLboolean remap_vertex_store = GL_FALSE;
171
172 if (save->vertex_store && save->vertex_store->buffer_map) {
173 /* The vertex store is currently mapped but we're about to replay
174 * a display list. This can happen when a nested display list is
175 * being build with GL_COMPILE_AND_EXECUTE.
176 * We never want to have mapped vertex buffers when we're drawing.
177 * Unmap the vertex store, execute the list, then remap the vertex
178 * store.
179 */
180 vbo_save_unmap_vertex_store(ctx, save->vertex_store);
181 remap_vertex_store = GL_TRUE;
182 }
183
184 FLUSH_CURRENT(ctx, 0);
185
186 if (node->prim_count > 0) {
187
188 if (_mesa_inside_begin_end(ctx) && node->prims[0].begin) {
189 /* Error: we're about to begin a new primitive but we're already
190 * inside a glBegin/End pair.
191 */
192 _mesa_error(ctx, GL_INVALID_OPERATION,
193 "draw operation inside glBegin/End");
194 goto end;
195 }
196 else if (save->replay_flags) {
197 /* Various degenerate cases: translate into immediate mode
198 * calls rather than trying to execute in place.
199 */
200 loopback_vertex_list(ctx, node);
201
202 goto end;
203 }
204
205 bind_vertex_list(ctx, node);
206
207 /* Need that at least one time. */
208 if (ctx->NewState)
209 _mesa_update_state(ctx);
210
211 /* XXX also need to check if shader enabled, but invalid */
212 if ((ctx->VertexProgram.Enabled &&
213 !_mesa_arb_vertex_program_enabled(ctx)) ||
214 (ctx->FragmentProgram.Enabled &&
215 !_mesa_arb_fragment_program_enabled(ctx))) {
216 _mesa_error(ctx, GL_INVALID_OPERATION,
217 "glBegin (invalid vertex/fragment program)");
218 return;
219 }
220
221 /* Finally update the inputs array */
222 _vbo_update_inputs(ctx, &vbo->draw_arrays);
223 _mesa_set_drawing_arrays(ctx, vbo->draw_arrays.inputs);
224
225 assert(ctx->NewState == 0);
226
227 if (node->vertex_count > 0) {
228 GLuint min_index = node->start_vertex;
229 GLuint max_index = min_index + node->vertex_count - 1;
230 vbo->draw_prims(ctx,
231 node->prims,
232 node->prim_count,
233 NULL,
234 GL_TRUE,
235 min_index, max_index,
236 NULL, 0, NULL);
237 }
238 }
239
240 /* Copy to current?
241 */
242 playback_copy_to_current(ctx, node);
243
244 end:
245 if (remap_vertex_store) {
246 save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
247 }
248 }