vbo: Make use of _DrawVAO from immediate mode draw
[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/mtypes.h"
39 #include "main/varray.h"
40
41
42 struct _glapi_table;
43 struct _mesa_prim;
44
45
46 struct vbo_context {
47 struct gl_vertex_array currval[VBO_ATTRIB_MAX];
48 /* The array of inputs used for _DrawVAO draws. */
49 struct vbo_inputs draw_arrays;
50
51 struct gl_vertex_array_object *VAO;
52
53 struct vbo_exec_context exec;
54 struct vbo_save_context save;
55
56 /* Callback into the driver. This must always succeed, the driver
57 * is responsible for initiating any fallback actions required:
58 */
59 vbo_draw_func draw_prims;
60
61 /* Optional callback for indirect draws. This allows multidraws to not be
62 * broken up, as well as for the actual count to be passed in as a separate
63 * indirect parameter.
64 */
65 vbo_indirect_draw_func draw_indirect_prims;
66 };
67
68
69 static inline struct vbo_context *
70 vbo_context(struct gl_context *ctx)
71 {
72 return ctx->vbo_context;
73 }
74
75
76 /**
77 * Array to apply the fixed function material aliasing map to
78 * an attribute value used in vbo processing inputs to an attribute
79 * as they appear in the vao.
80 */
81 extern const GLubyte
82 _vbo_attribute_alias_map[VP_MODE_MAX][VERT_ATTRIB_MAX];
83
84
85 /**
86 * Return if format is integer. The immediate mode commands only emit floats
87 * for non-integer types, thus everything else is integer.
88 */
89 static inline GLboolean
90 vbo_attrtype_to_integer_flag(GLenum format)
91 {
92 switch (format) {
93 case GL_FLOAT:
94 case GL_DOUBLE:
95 return GL_FALSE;
96 case GL_INT:
97 case GL_UNSIGNED_INT:
98 case GL_UNSIGNED_INT64_ARB:
99 return GL_TRUE;
100 default:
101 unreachable("Bad vertex attribute type");
102 return GL_FALSE;
103 }
104 }
105
106 static inline GLboolean
107 vbo_attrtype_to_double_flag(GLenum format)
108 {
109 switch (format) {
110 case GL_FLOAT:
111 case GL_INT:
112 case GL_UNSIGNED_INT:
113 case GL_UNSIGNED_INT64_ARB:
114 return GL_FALSE;
115 case GL_DOUBLE:
116 return GL_TRUE;
117 default:
118 unreachable("Bad vertex attribute type");
119 return GL_FALSE;
120 }
121 }
122
123
124 /**
125 * Return default component values for the given format.
126 * The return type is an array of fi_types, because that's how we declare
127 * the vertex storage : floats , integers or unsigned integers.
128 */
129 static inline const fi_type *
130 vbo_get_default_vals_as_union(GLenum format)
131 {
132 static const GLfloat default_float[4] = { 0, 0, 0, 1 };
133 static const GLint default_int[4] = { 0, 0, 0, 1 };
134
135 switch (format) {
136 case GL_FLOAT:
137 return (fi_type *)default_float;
138 case GL_INT:
139 case GL_UNSIGNED_INT:
140 return (fi_type *)default_int;
141 default:
142 unreachable("Bad vertex format");
143 return NULL;
144 }
145 }
146
147
148 /**
149 * Compute the max number of vertices which can be stored in
150 * a vertex buffer, given the current vertex size, and the amount
151 * of space already used.
152 */
153 static inline unsigned
154 vbo_compute_max_verts(const struct vbo_exec_context *exec)
155 {
156 unsigned n = (VBO_VERT_BUFFER_SIZE - exec->vtx.buffer_used) /
157 (exec->vtx.vertex_size * sizeof(GLfloat));
158 if (n == 0)
159 return 0;
160 /* Subtract one so we're always sure to have room for an extra
161 * vertex for GL_LINE_LOOP -> GL_LINE_STRIP conversion.
162 */
163 n--;
164 return n;
165 }
166
167
168 void
169 vbo_try_prim_conversion(struct _mesa_prim *p);
170
171 bool
172 vbo_can_merge_prims(const struct _mesa_prim *p0, const struct _mesa_prim *p1);
173
174 void
175 vbo_merge_prims(struct _mesa_prim *p0, const struct _mesa_prim *p1);
176
177
178 /**
179 * Get the filter mask for vbo draws depending on the vertex_processing_mode.
180 */
181 static inline GLbitfield
182 _vbo_get_vao_filter(gl_vertex_processing_mode vertex_processing_mode)
183 {
184 if (vertex_processing_mode == VP_MODE_FF) {
185 /* The materials mapped into the generic arrays */
186 return VERT_BIT_FF_ALL | VERT_BIT_MAT_ALL;
187 } else {
188 return VERT_BIT_ALL;
189 }
190 }
191
192
193 /**
194 * Translate the bitmask of VBO_ATTRIB_BITs to VERT_ATTRIB_BITS.
195 * Note that position/generic0 attribute aliasing is done
196 * generically in the VAO.
197 */
198 static inline GLbitfield
199 _vbo_get_vao_enabled_from_vbo(gl_vertex_processing_mode vertex_processing_mode,
200 GLbitfield64 enabled)
201 {
202 if (vertex_processing_mode == VP_MODE_FF) {
203 /* The materials mapped into the generic arrays */
204 return (((GLbitfield)enabled) & VERT_BIT_FF_ALL)
205 | (((GLbitfield)(enabled >> VBO_MATERIAL_SHIFT)) & VERT_BIT_MAT_ALL);
206 } else {
207 return ((GLbitfield)enabled) & VERT_BIT_ALL;
208 }
209 }
210
211
212 /**
213 * Set the vertex attrib for vbo draw use.
214 */
215 static inline void
216 _vbo_set_attrib_format(struct gl_context *ctx,
217 struct gl_vertex_array_object *vao,
218 gl_vert_attrib attr, GLintptr buffer_offset,
219 GLubyte size, GLenum16 type, GLuint offset)
220 {
221 const GLboolean integer = vbo_attrtype_to_integer_flag(type);
222 const GLboolean doubles = vbo_attrtype_to_double_flag(type);
223 _mesa_update_array_format(ctx, vao, attr, size, type, GL_RGBA,
224 GL_FALSE, integer, doubles, offset);
225 /* Ptr for userspace arrays */
226 vao->VertexAttrib[attr].Ptr = ADD_POINTERS(buffer_offset, offset);
227 }
228
229
230 #endif /* VBO_PRIVATE_H */