vbo: move VBO-private types, prototypes, etc. into new vbo_private.h header
[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 "main/glheader.h"
30 #include "main/bufferobj.h"
31 #include "main/context.h"
32 #include "main/imports.h"
33 #include "main/mtypes.h"
34 #include "main/macros.h"
35 #include "main/light.h"
36 #include "main/state.h"
37 #include "util/bitscan.h"
38
39 #include "vbo_context.h"
40 #include "vbo_private.h"
41
42
43 /**
44 * After playback, copy everything but the position from the
45 * last vertex to the saved state
46 */
47 static void
48 playback_copy_to_current(struct gl_context *ctx,
49 const struct vbo_save_vertex_list *node)
50 {
51 struct vbo_context *vbo = vbo_context(ctx);
52 fi_type vertex[VBO_ATTRIB_MAX * 4];
53 fi_type *data;
54 GLbitfield64 mask;
55
56 if (node->current_size == 0)
57 return;
58
59 if (node->current_data) {
60 data = node->current_data;
61 }
62 else {
63 /* Position of last vertex */
64 const GLuint pos = node->vertex_count > 0 ? node->vertex_count - 1 : 0;
65 /* Offset to last vertex in the vertex buffer */
66 const GLuint offset = node->buffer_offset
67 + pos * node->vertex_size * sizeof(GLfloat);
68
69 data = vertex;
70
71 ctx->Driver.GetBufferSubData(ctx, offset,
72 node->vertex_size * sizeof(GLfloat),
73 data, node->vertex_store->bufferobj);
74
75 data += node->attrsz[0]; /* skip vertex position */
76 }
77
78 mask = node->enabled & (~BITFIELD64_BIT(VBO_ATTRIB_POS));
79 while (mask) {
80 const int i = u_bit_scan64(&mask);
81 fi_type *current = (fi_type *)vbo->currval[i].Ptr;
82 fi_type tmp[4];
83 assert(node->attrsz[i]);
84
85 COPY_CLEAN_4V_TYPE_AS_UNION(tmp,
86 node->attrsz[i],
87 data,
88 node->attrtype[i]);
89
90 if (node->attrtype[i] != vbo->currval[i].Type ||
91 memcmp(current, tmp, 4 * sizeof(GLfloat)) != 0) {
92 memcpy(current, tmp, 4 * sizeof(GLfloat));
93
94 vbo->currval[i].Size = node->attrsz[i];
95 vbo->currval[i]._ElementSize = vbo->currval[i].Size * sizeof(GLfloat);
96 vbo->currval[i].Type = node->attrtype[i];
97 vbo->currval[i].Integer =
98 vbo_attrtype_to_integer_flag(node->attrtype[i]);
99
100 if (i >= VBO_ATTRIB_FIRST_MATERIAL &&
101 i <= VBO_ATTRIB_LAST_MATERIAL)
102 ctx->NewState |= _NEW_LIGHT;
103
104 ctx->NewState |= _NEW_CURRENT_ATTRIB;
105 }
106
107 data += node->attrsz[i];
108 }
109
110 /* Colormaterial -- this kindof sucks.
111 */
112 if (ctx->Light.ColorMaterialEnabled) {
113 _mesa_update_color_material(ctx, ctx->Current.Attrib[VBO_ATTRIB_COLOR0]);
114 }
115
116 /* CurrentExecPrimitive
117 */
118 if (node->prim_count) {
119 const struct _mesa_prim *prim = &node->prims[node->prim_count - 1];
120 if (prim->end)
121 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
122 else
123 ctx->Driver.CurrentExecPrimitive = prim->mode;
124 }
125 }
126
127
128
129 /**
130 * Treat the vertex storage as a VBO, define vertex arrays pointing
131 * into it:
132 */
133 static void
134 bind_vertex_list(struct gl_context *ctx,
135 const struct vbo_save_vertex_list *node)
136 {
137 struct vbo_context *vbo = vbo_context(ctx);
138 struct vbo_save_context *save = &vbo->save;
139 struct gl_vertex_array *arrays = save->arrays;
140 GLuint buffer_offset = node->buffer_offset;
141 const GLubyte *map;
142 GLuint attr;
143 GLubyte node_attrsz[VBO_ATTRIB_MAX]; /* copy of node->attrsz[] */
144 GLenum node_attrtype[VBO_ATTRIB_MAX]; /* copy of node->attrtype[] */
145 GLbitfield varying_inputs = 0x0;
146
147 memcpy(node_attrsz, node->attrsz, sizeof(node->attrsz));
148 memcpy(node_attrtype, node->attrtype, sizeof(node->attrtype));
149
150 if (aligned_vertex_buffer_offset(node)) {
151 /* The vertex size is an exact multiple of the buffer offset.
152 * This means that we can use zero-based vertex attribute pointers
153 * and specify the start of the primitive with the _mesa_prim::start
154 * field. This results in issuing several draw calls with identical
155 * vertex attribute information. This can result in fewer state
156 * changes in drivers. In particular, the Gallium CSO module will
157 * filter out redundant vertex buffer changes.
158 */
159 buffer_offset = 0;
160 }
161
162 /* Install the default (ie Current) attributes first */
163 for (attr = 0; attr < VERT_ATTRIB_FF_MAX; attr++) {
164 save->inputs[attr] = &vbo->currval[VBO_ATTRIB_POS + attr];
165 }
166
167 /* Overlay other active attributes */
168 switch (get_program_mode(ctx)) {
169 case VP_NONE:
170 for (attr = 0; attr < MAT_ATTRIB_MAX; attr++) {
171 save->inputs[VERT_ATTRIB_GENERIC(attr)] =
172 &vbo->currval[VBO_ATTRIB_MAT_FRONT_AMBIENT+attr];
173 }
174 map = vbo->map_vp_none;
175 break;
176 case VP_ARB:
177 for (attr = 0; attr < VERT_ATTRIB_GENERIC_MAX; attr++) {
178 save->inputs[VERT_ATTRIB_GENERIC(attr)] =
179 &vbo->currval[VBO_ATTRIB_GENERIC0+attr];
180 }
181 map = vbo->map_vp_arb;
182
183 /* check if VERT_ATTRIB_POS is not read but VERT_BIT_GENERIC0 is read.
184 * In that case we effectively need to route the data from
185 * glVertexAttrib(0, val) calls to feed into the GENERIC0 input.
186 */
187 const GLbitfield64 inputs_read =
188 ctx->VertexProgram._Current->info.inputs_read;
189 if ((inputs_read & VERT_BIT_POS) == 0 &&
190 (inputs_read & VERT_BIT_GENERIC0)) {
191 save->inputs[VERT_ATTRIB_GENERIC0] = save->inputs[0];
192 node_attrsz[VERT_ATTRIB_GENERIC0] = node_attrsz[0];
193 node_attrtype[VERT_ATTRIB_GENERIC0] = node_attrtype[0];
194 node_attrsz[0] = 0;
195 }
196 break;
197 default:
198 unreachable("Bad vertex program mode");
199 }
200
201 for (attr = 0; attr < VERT_ATTRIB_MAX; attr++) {
202 const GLuint src = map[attr];
203
204 if (node_attrsz[src]) {
205 struct gl_vertex_array *array = &arrays[attr];
206
207 /* override the default array set above */
208 save->inputs[attr] = array;
209
210 array->Ptr = (const GLubyte *) NULL + buffer_offset;
211 array->Size = node_attrsz[src];
212 array->StrideB = node->vertex_size * sizeof(GLfloat);
213 array->Type = node_attrtype[src];
214 array->Integer = vbo_attrtype_to_integer_flag(node_attrtype[src]);
215 array->Format = GL_RGBA;
216 array->_ElementSize = array->Size * sizeof(GLfloat);
217 _mesa_reference_buffer_object(ctx,
218 &array->BufferObj,
219 node->vertex_store->bufferobj);
220
221 assert(array->BufferObj->Name);
222
223 buffer_offset += node_attrsz[src] * sizeof(GLfloat);
224 varying_inputs |= VERT_BIT(attr);
225 }
226 }
227
228 _mesa_set_varying_vp_inputs(ctx, varying_inputs);
229 ctx->NewDriverState |= ctx->DriverFlags.NewArray;
230 }
231
232
233 static void
234 loopback_vertex_list(struct gl_context *ctx,
235 const struct vbo_save_vertex_list *list)
236 {
237 const char *buffer =
238 ctx->Driver.MapBufferRange(ctx, 0,
239 list->vertex_store->bufferobj->Size,
240 GL_MAP_READ_BIT, /* ? */
241 list->vertex_store->bufferobj,
242 MAP_INTERNAL);
243
244 unsigned buffer_offset =
245 aligned_vertex_buffer_offset(list) ? 0 : list->buffer_offset;
246
247 vbo_loopback_vertex_list(ctx,
248 (const GLfloat *) (buffer + buffer_offset),
249 list->attrsz,
250 list->prims,
251 list->prim_count,
252 list->wrap_count,
253 list->vertex_size);
254
255 ctx->Driver.UnmapBuffer(ctx, list->vertex_store->bufferobj,
256 MAP_INTERNAL);
257 }
258
259
260 /**
261 * Execute the buffer and save copied verts.
262 * This is called from the display list code when executing
263 * a drawing command.
264 */
265 void
266 vbo_save_playback_vertex_list(struct gl_context *ctx, void *data)
267 {
268 const struct vbo_save_vertex_list *node =
269 (const struct vbo_save_vertex_list *) data;
270 struct vbo_save_context *save = &vbo_context(ctx)->save;
271 GLboolean remap_vertex_store = GL_FALSE;
272
273 if (save->vertex_store && save->vertex_store->buffer_map) {
274 /* The vertex store is currently mapped but we're about to replay
275 * a display list. This can happen when a nested display list is
276 * being build with GL_COMPILE_AND_EXECUTE.
277 * We never want to have mapped vertex buffers when we're drawing.
278 * Unmap the vertex store, execute the list, then remap the vertex
279 * store.
280 */
281 vbo_save_unmap_vertex_store(ctx, save->vertex_store);
282 remap_vertex_store = GL_TRUE;
283 }
284
285 FLUSH_CURRENT(ctx, 0);
286
287 if (node->prim_count > 0) {
288
289 if (_mesa_inside_begin_end(ctx) && node->prims[0].begin) {
290 /* Error: we're about to begin a new primitive but we're already
291 * inside a glBegin/End pair.
292 */
293 _mesa_error(ctx, GL_INVALID_OPERATION,
294 "draw operation inside glBegin/End");
295 goto end;
296 }
297 else if (save->replay_flags) {
298 /* Various degenerate cases: translate into immediate mode
299 * calls rather than trying to execute in place.
300 */
301 loopback_vertex_list(ctx, node);
302
303 goto end;
304 }
305
306 if (ctx->NewState)
307 _mesa_update_state(ctx);
308
309 /* XXX also need to check if shader enabled, but invalid */
310 if ((ctx->VertexProgram.Enabled &&
311 !_mesa_arb_vertex_program_enabled(ctx)) ||
312 (ctx->FragmentProgram.Enabled &&
313 !_mesa_arb_fragment_program_enabled(ctx))) {
314 _mesa_error(ctx, GL_INVALID_OPERATION,
315 "glBegin (invalid vertex/fragment program)");
316 return;
317 }
318
319 bind_vertex_list(ctx, node);
320
321 vbo_draw_method(vbo_context(ctx), DRAW_DISPLAY_LIST);
322
323 /* Again...
324 */
325 if (ctx->NewState)
326 _mesa_update_state(ctx);
327
328 if (node->vertex_count > 0) {
329 vbo_context(ctx)->draw_prims(ctx,
330 node->prims,
331 node->prim_count,
332 NULL,
333 GL_TRUE,
334 0, /* Node is a VBO, so this is ok */
335 node->vertex_count - 1,
336 NULL, 0, NULL);
337 }
338 }
339
340 /* Copy to current?
341 */
342 playback_copy_to_current(ctx, node);
343
344 end:
345 if (remap_vertex_store) {
346 save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
347 }
348 }