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