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