vbo: Test for VBO_SAVE_PRIM_WEAK in _mesa_prim::mode is false.
[mesa.git] / src / mesa / vbo / vbo_save_loopback.c
1 /**************************************************************************
2 *
3 * Copyright 2005 VMware, Inc.
4 * 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
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdio.h>
29 #include "main/context.h"
30 #include "main/glheader.h"
31 #include "main/enums.h"
32 #include "main/imports.h"
33 #include "main/dispatch.h"
34 #include "glapi/glapi.h"
35
36 #include "vbo_private.h"
37
38
39 typedef void (*attr_func)(struct gl_context *ctx, GLint index, const GLfloat *);
40
41
42 /* This file makes heavy use of the aliasing of NV vertex attributes
43 * with the legacy attributes, and also with ARB and Material
44 * attributes as currently implemented.
45 */
46 static void
47 VertexAttrib1fvNV(struct gl_context *ctx, GLint index, const GLfloat *v)
48 {
49 CALL_VertexAttrib1fvNV(ctx->Exec, (index, v));
50 }
51
52
53 static void
54 VertexAttrib2fvNV(struct gl_context *ctx, GLint index, const GLfloat *v)
55 {
56 CALL_VertexAttrib2fvNV(ctx->Exec, (index, v));
57 }
58
59
60 static void
61 VertexAttrib3fvNV(struct gl_context *ctx, GLint index, const GLfloat *v)
62 {
63 CALL_VertexAttrib3fvNV(ctx->Exec, (index, v));
64 }
65
66
67 static void
68 VertexAttrib4fvNV(struct gl_context *ctx, GLint index, const GLfloat *v)
69 {
70 CALL_VertexAttrib4fvNV(ctx->Exec, (index, v));
71 }
72
73
74 static attr_func vert_attrfunc[4] = {
75 VertexAttrib1fvNV,
76 VertexAttrib2fvNV,
77 VertexAttrib3fvNV,
78 VertexAttrib4fvNV
79 };
80
81
82 struct loopback_attr {
83 enum vbo_attrib index;
84 GLuint offset;
85 attr_func func;
86 };
87
88
89 /**
90 * Don't emit ends and begins on wrapped primitives. Don't replay
91 * wrapped vertices. If we get here, it's probably because the
92 * precalculated wrapping is wrong.
93 */
94 static void
95 loopback_prim(struct gl_context *ctx,
96 const GLubyte *buffer,
97 const struct _mesa_prim *prim,
98 GLuint wrap_count,
99 GLuint stride,
100 const struct loopback_attr *la, GLuint nr)
101 {
102 GLuint start = prim->start;
103 const GLuint end = start + prim->count;
104 const GLubyte *data;
105
106 if (0)
107 printf("loopback prim %s(%s,%s) verts %d..%d vsize %d\n",
108 _mesa_lookup_prim_by_nr(prim->mode),
109 prim->begin ? "begin" : "..",
110 prim->end ? "end" : "..",
111 start, end,
112 stride);
113
114 if (prim->begin) {
115 CALL_Begin(GET_DISPATCH(), (prim->mode));
116 }
117 else {
118 start += wrap_count;
119 }
120
121 data = buffer + start * stride;
122
123 for (GLuint j = start; j < end; j++) {
124 for (GLuint k = 0; k < nr; k++)
125 la[k].func(ctx, la[k].index, (const GLfloat *)(data + la[k].offset));
126
127 data += stride;
128 }
129
130 if (prim->end) {
131 CALL_End(GET_DISPATCH(), ());
132 }
133 }
134
135
136 /**
137 * Primitives generated by DrawArrays/DrawElements/Rectf may be
138 * caught here. If there is no primitive in progress, execute them
139 * normally, otherwise need to track and discard the generated
140 * primitives.
141 */
142 static void
143 loopback_weak_prim(struct gl_context *ctx,
144 const struct _mesa_prim *prim)
145 {
146 /* Use the prim_weak flag to ensure that if this primitive
147 * wraps, we don't mistake future vertex_lists for part of the
148 * surrounding primitive.
149 *
150 * While this flag is set, we are simply disposing of data
151 * generated by an operation now known to be a noop.
152 */
153 if (prim->begin)
154 ctx->Driver.CurrentExecPrimitive |= VBO_SAVE_PRIM_WEAK;
155 if (prim->end)
156 ctx->Driver.CurrentExecPrimitive &= ~VBO_SAVE_PRIM_WEAK;
157 }
158
159
160 static inline void
161 append_attr(GLuint *nr, struct loopback_attr la[], int i, int shift,
162 const struct gl_vertex_array_object *vao)
163 {
164 la[*nr].index = shift + i;
165 la[*nr].offset = vao->VertexAttrib[i].RelativeOffset;
166 la[*nr].func = vert_attrfunc[vao->VertexAttrib[i].Size - 1];
167 (*nr)++;
168 }
169
170
171 void
172 _vbo_loopback_vertex_list(struct gl_context *ctx,
173 const struct vbo_save_vertex_list* node)
174 {
175 struct loopback_attr la[VBO_ATTRIB_MAX];
176 GLuint nr = 0;
177
178 /* All Legacy, NV, ARB and Material attributes are routed through
179 * the NV attributes entrypoints:
180 */
181 const struct gl_vertex_array_object *vao = node->VAO[VP_MODE_FF];
182 GLbitfield mask = vao->_Enabled & VERT_BIT_MAT_ALL;
183 while (mask) {
184 const int i = u_bit_scan(&mask);
185 append_attr(&nr, la, i, VBO_MATERIAL_SHIFT, vao);
186 }
187
188 vao = node->VAO[VP_MODE_SHADER];
189 mask = vao->_Enabled & ~(VERT_BIT_POS | VERT_BIT_GENERIC0);
190 while (mask) {
191 const int i = u_bit_scan(&mask);
192 append_attr(&nr, la, i, 0, vao);
193 }
194
195 /* The last in the list should be the vertex provoking attribute */
196 if (vao->_Enabled & VERT_BIT_GENERIC0) {
197 append_attr(&nr, la, VERT_ATTRIB_GENERIC0, 0, vao);
198 } else if (vao->_Enabled & VERT_BIT_POS) {
199 append_attr(&nr, la, VERT_ATTRIB_POS, 0, vao);
200 }
201
202 const GLuint wrap_count = node->wrap_count;
203 const GLuint stride = _vbo_save_get_stride(node);
204 const GLubyte *buffer = NULL;
205 if (0 < nr) {
206 /* Compute the minimal offset into the vertex buffer object */
207 GLuint offset = ~0u;
208 for (GLuint i = 0; i < nr; ++i)
209 offset = MIN2(offset, la[i].offset);
210 for (GLuint i = 0; i < nr; ++i)
211 la[i].offset -= offset;
212
213 /* Get the mapped base pointer, assert sufficient mapping */
214 struct gl_buffer_object *bufferobj = vao->BufferBinding[0].BufferObj;
215 assert(bufferobj && bufferobj->Mappings[MAP_INTERNAL].Pointer);
216 buffer = bufferobj->Mappings[MAP_INTERNAL].Pointer;
217 assert(bufferobj->Mappings[MAP_INTERNAL].Offset
218 <= vao->BufferBinding[0].Offset + offset
219 + stride*(_vbo_save_get_min_index(node) + wrap_count));
220 buffer += vao->BufferBinding[0].Offset + offset
221 - bufferobj->Mappings[MAP_INTERNAL].Offset;
222 assert(stride*(_vbo_save_get_vertex_count(node) - wrap_count)
223 <= bufferobj->Mappings[MAP_INTERNAL].Length);
224 }
225
226 /* Replay the primitives */
227 const struct _mesa_prim *prims = node->prims;
228 const GLuint prim_count = node->prim_count;
229 for (GLuint i = 0; i < prim_count; i++) {
230 if ((0) && _mesa_inside_begin_end(ctx)) {
231 loopback_weak_prim(ctx, &prims[i]);
232 } else {
233 loopback_prim(ctx, buffer, &prims[i], wrap_count, stride, la, nr);
234 }
235 }
236 }