vbo: Test for VBO_SAVE_PRIM_WEAK in _mesa_prim::mode is false.
[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/macros.h"
36 #include "main/light.h"
37 #include "main/state.h"
38 #include "main/varray.h"
39 #include "util/bitscan.h"
40
41 #include "vbo_private.h"
42
43
44 static void
45 copy_vao(struct gl_context *ctx, const struct gl_vertex_array_object *vao,
46 GLbitfield mask, GLbitfield state, int shift, fi_type **data)
47 {
48 struct vbo_context *vbo = vbo_context(ctx);
49
50 mask &= vao->_Enabled;
51 while (mask) {
52 const int i = u_bit_scan(&mask);
53 const struct gl_array_attributes *attrib = &vao->VertexAttrib[i];
54 struct gl_array_attributes *currval = &vbo->current[shift + i];
55 const GLubyte size = attrib->Size;
56 const GLenum16 type = attrib->Type;
57 fi_type tmp[8];
58 int dmul = 1;
59
60 if (type == GL_DOUBLE ||
61 type == GL_UNSIGNED_INT64_ARB)
62 dmul = 2;
63
64 if (dmul == 2)
65 memcpy(tmp, *data, size * dmul * sizeof(GLfloat));
66 else
67 COPY_CLEAN_4V_TYPE_AS_UNION(tmp, size, *data, type);
68
69 if (type != currval->Type ||
70 memcmp(currval->Ptr, tmp, 4 * sizeof(GLfloat) * dmul) != 0) {
71 memcpy((fi_type*)currval->Ptr, tmp, 4 * sizeof(GLfloat) * dmul);
72
73 currval->Size = size;
74 currval->_ElementSize = size * sizeof(GLfloat) * dmul;
75 currval->Type = type;
76 currval->Integer = vbo_attrtype_to_integer_flag(type);
77 currval->Doubles = vbo_attrtype_to_double_flag(type);
78 currval->Normalized = GL_FALSE;
79 currval->Format = GL_RGBA;
80
81 ctx->NewState |= state;
82 }
83
84 *data += size;
85 }
86 }
87
88 /**
89 * After playback, copy everything but the position from the
90 * last vertex to the saved state
91 */
92 static void
93 playback_copy_to_current(struct gl_context *ctx,
94 const struct vbo_save_vertex_list *node)
95 {
96 if (!node->current_data)
97 return;
98
99 fi_type *data = node->current_data;
100 /* Copy conventional attribs and generics except pos */
101 copy_vao(ctx, node->VAO[VP_MODE_SHADER], ~VERT_BIT_POS & VERT_BIT_ALL,
102 _NEW_CURRENT_ATTRIB, 0, &data);
103 /* Copy materials */
104 copy_vao(ctx, node->VAO[VP_MODE_FF], VERT_BIT_MAT_ALL,
105 _NEW_CURRENT_ATTRIB | _NEW_LIGHT, VBO_MATERIAL_SHIFT, &data);
106
107 /* Colormaterial -- this kindof sucks.
108 */
109 if (ctx->Light.ColorMaterialEnabled) {
110 _mesa_update_color_material(ctx, ctx->Current.Attrib[VBO_ATTRIB_COLOR0]);
111 }
112
113 /* CurrentExecPrimitive
114 */
115 if (node->prim_count) {
116 const struct _mesa_prim *prim = &node->prims[node->prim_count - 1];
117 if (prim->end)
118 ctx->Driver.CurrentExecPrimitive = PRIM_OUTSIDE_BEGIN_END;
119 else
120 ctx->Driver.CurrentExecPrimitive = prim->mode;
121 }
122 }
123
124
125
126 /**
127 * Set the appropriate VAO to draw.
128 */
129 static void
130 bind_vertex_list(struct gl_context *ctx,
131 const struct vbo_save_vertex_list *node)
132 {
133 const gl_vertex_processing_mode mode = ctx->VertexProgram._VPMode;
134 _mesa_set_draw_vao(ctx, node->VAO[mode], _vbo_get_vao_filter(mode));
135 }
136
137
138 static void
139 loopback_vertex_list(struct gl_context *ctx,
140 const struct vbo_save_vertex_list *list)
141 {
142 struct gl_buffer_object *bo = list->VAO[0]->BufferBinding[0].BufferObj;
143 ctx->Driver.MapBufferRange(ctx, 0, bo->Size, GL_MAP_READ_BIT, /* ? */
144 bo, MAP_INTERNAL);
145
146 /* Note that the range of referenced vertices must be mapped already */
147 _vbo_loopback_vertex_list(ctx, list);
148
149 ctx->Driver.UnmapBuffer(ctx, bo, MAP_INTERNAL);
150 }
151
152
153 /**
154 * Execute the buffer and save copied verts.
155 * This is called from the display list code when executing
156 * a drawing command.
157 */
158 void
159 vbo_save_playback_vertex_list(struct gl_context *ctx, void *data)
160 {
161 const struct vbo_save_vertex_list *node =
162 (const struct vbo_save_vertex_list *) data;
163 struct vbo_context *vbo = vbo_context(ctx);
164 struct vbo_save_context *save = &vbo->save;
165 GLboolean remap_vertex_store = GL_FALSE;
166
167 if (save->vertex_store && save->vertex_store->buffer_map) {
168 /* The vertex store is currently mapped but we're about to replay
169 * a display list. This can happen when a nested display list is
170 * being build with GL_COMPILE_AND_EXECUTE.
171 * We never want to have mapped vertex buffers when we're drawing.
172 * Unmap the vertex store, execute the list, then remap the vertex
173 * store.
174 */
175 vbo_save_unmap_vertex_store(ctx, save->vertex_store);
176 remap_vertex_store = GL_TRUE;
177 }
178
179 FLUSH_FOR_DRAW(ctx);
180
181 if (node->prim_count > 0) {
182
183 if (_mesa_inside_begin_end(ctx) && node->prims[0].begin) {
184 /* Error: we're about to begin a new primitive but we're already
185 * inside a glBegin/End pair.
186 */
187 _mesa_error(ctx, GL_INVALID_OPERATION,
188 "draw operation inside glBegin/End");
189 goto end;
190 }
191 else if (save->replay_flags) {
192 /* Various degenerate cases: translate into immediate mode
193 * calls rather than trying to execute in place.
194 */
195 loopback_vertex_list(ctx, node);
196
197 goto end;
198 }
199
200 bind_vertex_list(ctx, node);
201
202 /* Need that at least one time. */
203 if (ctx->NewState)
204 _mesa_update_state(ctx);
205
206 /* XXX also need to check if shader enabled, but invalid */
207 if ((ctx->VertexProgram.Enabled &&
208 !_mesa_arb_vertex_program_enabled(ctx)) ||
209 (ctx->FragmentProgram.Enabled &&
210 !_mesa_arb_fragment_program_enabled(ctx))) {
211 _mesa_error(ctx, GL_INVALID_OPERATION,
212 "glBegin (invalid vertex/fragment program)");
213 return;
214 }
215
216 assert(ctx->NewState == 0);
217
218 if (node->vertex_count > 0) {
219 GLuint min_index = _vbo_save_get_min_index(node);
220 GLuint max_index = _vbo_save_get_max_index(node);
221 ctx->Driver.Draw(ctx, node->prims, node->prim_count, NULL, GL_TRUE,
222 min_index, max_index, NULL, 0, NULL);
223 }
224 }
225
226 /* Copy to current?
227 */
228 playback_copy_to_current(ctx, node);
229
230 end:
231 if (remap_vertex_store) {
232 save->buffer_ptr = vbo_save_map_vertex_store(ctx, save->vertex_store);
233 }
234 }