mesa: avoid redundant VBO updates
[mesa.git] / src / mesa / vbo / vbo_private.h
1 /*
2 * mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006 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
26 /**
27 * Types, functions, etc which are private to the VBO module.
28 */
29
30
31 #ifndef VBO_PRIVATE_H
32 #define VBO_PRIVATE_H
33
34
35 #include "vbo/vbo_attrib.h"
36 #include "vbo/vbo_exec.h"
37 #include "vbo/vbo_save.h"
38 #include "main/varray.h"
39 #include "main/macros.h"
40
41
42 struct _glapi_table;
43 struct _mesa_prim;
44
45
46 struct vbo_context {
47 struct gl_vertex_buffer_binding binding;
48 struct gl_array_attributes current[VBO_ATTRIB_MAX];
49
50 struct gl_vertex_array_object *VAO;
51
52 struct vbo_exec_context exec;
53 struct vbo_save_context save;
54 };
55
56
57 static inline struct vbo_context *
58 vbo_context(struct gl_context *ctx)
59 {
60 return ctx->vbo_context;
61 }
62
63
64 static inline const struct vbo_context *
65 vbo_context_const(const struct gl_context *ctx)
66 {
67 return ctx->vbo_context;
68 }
69
70
71 /**
72 * Array to apply the fixed function material aliasing map to
73 * an attribute value used in vbo processing inputs to an attribute
74 * as they appear in the vao.
75 */
76 extern const GLubyte
77 _vbo_attribute_alias_map[VP_MODE_MAX][VERT_ATTRIB_MAX];
78
79
80 /**
81 * Return if format is integer. The immediate mode commands only emit floats
82 * for non-integer types, thus everything else is integer.
83 */
84 static inline GLboolean
85 vbo_attrtype_to_integer_flag(GLenum format)
86 {
87 switch (format) {
88 case GL_FLOAT:
89 case GL_DOUBLE:
90 return GL_FALSE;
91 case GL_INT:
92 case GL_UNSIGNED_INT:
93 case GL_UNSIGNED_INT64_ARB:
94 return GL_TRUE;
95 default:
96 unreachable("Bad vertex attribute type");
97 return GL_FALSE;
98 }
99 }
100
101 static inline GLboolean
102 vbo_attrtype_to_double_flag(GLenum format)
103 {
104 switch (format) {
105 case GL_FLOAT:
106 case GL_INT:
107 case GL_UNSIGNED_INT:
108 return GL_FALSE;
109 case GL_UNSIGNED_INT64_ARB:
110 case GL_DOUBLE:
111 return GL_TRUE;
112 default:
113 unreachable("Bad vertex attribute type");
114 return GL_FALSE;
115 }
116 }
117
118
119 static inline void
120 vbo_set_vertex_format(struct gl_vertex_format* vertex_format,
121 GLubyte size, GLenum16 type)
122 {
123 _mesa_set_vertex_format(vertex_format, size, type, GL_RGBA, GL_FALSE,
124 vbo_attrtype_to_integer_flag(type),
125 vbo_attrtype_to_double_flag(type));
126 }
127
128
129 /**
130 * Return default component values for the given format.
131 * The return type is an array of fi_types, because that's how we declare
132 * the vertex storage : floats , integers or unsigned integers.
133 */
134 static inline const fi_type *
135 vbo_get_default_vals_as_union(GLenum format)
136 {
137 static const GLfloat default_float[4] = { 0, 0, 0, 1 };
138 static const GLint default_int[4] = { 0, 0, 0, 1 };
139 static const GLdouble default_double[4] = { 0, 0, 0, 1 };
140 static const uint64_t default_uint64[4] = { 0, 0, 0, 1 };
141
142 switch (format) {
143 case GL_FLOAT:
144 return (fi_type *)default_float;
145 case GL_INT:
146 case GL_UNSIGNED_INT:
147 return (fi_type *)default_int;
148 case GL_DOUBLE:
149 return (fi_type *)default_double;
150 case GL_UNSIGNED_INT64_ARB:
151 return (fi_type *)default_uint64;
152 default:
153 unreachable("Bad vertex format");
154 return NULL;
155 }
156 }
157
158
159 /**
160 * Compute the max number of vertices which can be stored in
161 * a vertex buffer, given the current vertex size, and the amount
162 * of space already used.
163 */
164 static inline unsigned
165 vbo_compute_max_verts(const struct vbo_exec_context *exec)
166 {
167 unsigned n = (exec->ctx->Const.glBeginEndBufferSize -
168 exec->vtx.buffer_used) /
169 (exec->vtx.vertex_size * sizeof(GLfloat));
170 if (n == 0)
171 return 0;
172 /* Subtract one so we're always sure to have room for an extra
173 * vertex for GL_LINE_LOOP -> GL_LINE_STRIP conversion.
174 */
175 n--;
176 return n;
177 }
178
179
180 void
181 vbo_try_prim_conversion(struct _mesa_prim *p);
182
183 bool
184 vbo_merge_draws(struct gl_context *ctx, bool in_dlist,
185 struct _mesa_prim *p0, const struct _mesa_prim *p1);
186
187 unsigned
188 vbo_copy_vertices(struct gl_context *ctx,
189 GLenum mode,
190 struct _mesa_prim *last_prim,
191 unsigned vertex_size,
192 bool in_dlist,
193 fi_type *dst,
194 const fi_type *src);
195
196 /**
197 * Get the filter mask for vbo draws depending on the vertex_processing_mode.
198 */
199 static inline GLbitfield
200 _vbo_get_vao_filter(gl_vertex_processing_mode vertex_processing_mode)
201 {
202 if (vertex_processing_mode == VP_MODE_FF) {
203 /* The materials mapped into the generic arrays */
204 return VERT_BIT_FF_ALL | VERT_BIT_MAT_ALL;
205 } else {
206 return VERT_BIT_ALL;
207 }
208 }
209
210
211 /**
212 * Translate the bitmask of VBO_ATTRIB_BITs to VERT_ATTRIB_BITS.
213 * Note that position/generic0 attribute aliasing is done
214 * generically in the VAO.
215 */
216 static inline GLbitfield
217 _vbo_get_vao_enabled_from_vbo(gl_vertex_processing_mode vertex_processing_mode,
218 GLbitfield64 enabled)
219 {
220 if (vertex_processing_mode == VP_MODE_FF) {
221 /* The materials mapped into the generic arrays */
222 return (((GLbitfield)enabled) & VERT_BIT_FF_ALL)
223 | (((GLbitfield)(enabled >> VBO_MATERIAL_SHIFT)) & VERT_BIT_MAT_ALL);
224 } else {
225 return ((GLbitfield)enabled) & VERT_BIT_ALL;
226 }
227 }
228
229
230 /**
231 * Set the vertex attrib for vbo draw use.
232 */
233 static inline void
234 _vbo_set_attrib_format(struct gl_context *ctx,
235 struct gl_vertex_array_object *vao,
236 gl_vert_attrib attr, GLintptr buffer_offset,
237 GLubyte size, GLenum16 type, GLuint offset)
238 {
239 const GLboolean integer = vbo_attrtype_to_integer_flag(type);
240 const GLboolean doubles = vbo_attrtype_to_double_flag(type);
241
242 if (doubles)
243 size /= 2;
244 _mesa_update_array_format(ctx, vao, attr, size, type, GL_RGBA,
245 GL_FALSE, integer, doubles, offset);
246 vao->NewArrays |= vao->Enabled & VERT_BIT(attr);
247 vao->VertexAttrib[attr].Ptr = ADD_POINTERS(buffer_offset, offset);
248 }
249
250
251 #endif /* VBO_PRIVATE_H */