xref: /aosp_15_r20/external/mesa3d/src/mesa/main/arrayobj.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
5  * (C) Copyright IBM Corporation 2006
6  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions 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 MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25  */
26 
27 #ifndef ARRAYOBJ_H
28 #define ARRAYOBJ_H
29 
30 #include "util/glheader.h"
31 #include "mtypes.h"
32 #include "glformats.h"
33 #include "vbo/vbo.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 struct gl_context;
40 
41 /**
42  * \file arrayobj.h
43  * Functions for the GL_ARB_vertex_array_object extension.
44  *
45  * \author Ian Romanick <[email protected]>
46  * \author Brian Paul
47  */
48 
49 /*
50  * Internal functions
51  */
52 
53 extern struct gl_vertex_array_object *
54 _mesa_lookup_vao(struct gl_context *ctx, GLuint id);
55 
56 extern struct gl_vertex_array_object *
57 _mesa_lookup_vao_err(struct gl_context *ctx, GLuint id,
58                      bool is_ext_dsa, const char *caller);
59 
60 extern struct gl_vertex_array_object *
61 _mesa_new_vao(struct gl_context *ctx, GLuint name);
62 
63 extern void
64 _mesa_unbind_array_object_vbos(struct gl_context *ctx,
65                                struct gl_vertex_array_object *obj);
66 
67 extern void
68 _mesa_delete_vao(struct gl_context *ctx, struct gl_vertex_array_object *obj);
69 
70 extern void
71 _mesa_reference_vao_(struct gl_context *ctx,
72                      struct gl_vertex_array_object **ptr,
73                      struct gl_vertex_array_object *vao);
74 
75 static inline void
_mesa_reference_vao(struct gl_context * ctx,struct gl_vertex_array_object ** ptr,struct gl_vertex_array_object * vao)76 _mesa_reference_vao(struct gl_context *ctx,
77                     struct gl_vertex_array_object **ptr,
78                     struct gl_vertex_array_object *vao)
79 {
80    if (*ptr != vao)
81       _mesa_reference_vao_(ctx, ptr, vao);
82 }
83 
84 
85 extern void
86 _mesa_initialize_vao(struct gl_context *ctx,
87                      struct gl_vertex_array_object *obj, GLuint name);
88 
89 
90 extern void
91 _mesa_update_vao_derived_arrays(struct gl_context *ctx,
92                                 struct gl_vertex_array_object *vao,
93                                 bool display_list);
94 
95 extern void
96 _mesa_vao_map_arrays(struct gl_context *ctx, struct gl_vertex_array_object *vao,
97                      GLbitfield access);
98 
99 extern void
100 _mesa_vao_map(struct gl_context *ctx, struct gl_vertex_array_object *vao,
101               GLbitfield access);
102 
103 
104 extern void
105 _mesa_vao_unmap_arrays(struct gl_context *ctx,
106                        struct gl_vertex_array_object *vao);
107 
108 extern void
109 _mesa_vao_unmap(struct gl_context *ctx,
110                 struct gl_vertex_array_object *vao);
111 
112 
113 /**
114  * Array to apply the position/generic0 aliasing map to
115  * an attribute value used in vertex processing inputs to an attribute
116  * as they appear in the vao.
117  */
118 extern const GLubyte
119 _mesa_vao_attribute_map[ATTRIBUTE_MAP_MODE_MAX][VERT_ATTRIB_MAX];
120 
121 
122 /**
123  * Apply the position/generic0 aliasing map to a bitfield from the vao.
124  * Use for example to convert gl_vertex_array_object::Enabled
125  * or gl_vertex_buffer_binding::_VertexBinding from the vao numbering to
126  * the numbering used with vertex processing inputs.
127  */
128 static inline GLbitfield
_mesa_vao_enable_to_vp_inputs(gl_attribute_map_mode mode,GLbitfield enabled)129 _mesa_vao_enable_to_vp_inputs(gl_attribute_map_mode mode, GLbitfield enabled)
130 {
131    switch (mode) {
132    case ATTRIBUTE_MAP_MODE_IDENTITY:
133       return enabled;
134    case ATTRIBUTE_MAP_MODE_POSITION:
135       /* Copy VERT_ATTRIB_POS enable bit into GENERIC0 position */
136       return (enabled & ~VERT_BIT_GENERIC0)
137          | ((enabled & VERT_BIT_POS) << VERT_ATTRIB_GENERIC0);
138    case ATTRIBUTE_MAP_MODE_GENERIC0:
139       /* Copy VERT_ATTRIB_GENERIC0 enable bit into POS position */
140       return (enabled & ~VERT_BIT_POS)
141          | ((enabled & VERT_BIT_GENERIC0) >> VERT_ATTRIB_GENERIC0);
142    default:
143       return 0;
144    }
145 }
146 
147 /**
148  * Return enabled vertex arrays. The bitmask is trimmed based on POS/GENERIC0
149  * remapping, and generic varyings are masked out for fixed-func shaders.
150  */
151 static inline GLbitfield
_mesa_get_enabled_vertex_arrays(const struct gl_context * ctx)152 _mesa_get_enabled_vertex_arrays(const struct gl_context *ctx)
153 {
154    return ctx->VertexProgram._VPModeInputFilter &
155           ctx->Array._DrawVAO->_EnabledWithMapMode;
156 }
157 
158 
159 /**
160  * Return the enabled user (= non-VBO) attrib mask and the non-zero divisor
161  * attrib mask for the draw.
162  *
163  * Needs a fully updated VAO ready for draw.
164  */
165 static inline void
_mesa_get_derived_vao_masks(const struct gl_context * ctx,const GLbitfield enabled_attribs,GLbitfield * enabled_user_attribs,GLbitfield * nonzero_divisor_attribs)166 _mesa_get_derived_vao_masks(const struct gl_context *ctx,
167                             const GLbitfield enabled_attribs,
168                             GLbitfield *enabled_user_attribs,
169                             GLbitfield *nonzero_divisor_attribs)
170 {
171    const struct gl_vertex_array_object *const vao = ctx->Array._DrawVAO;
172    const GLbitfield enabled = vao->Enabled;
173    const GLbitfield enabled_nonuser = enabled & vao->VertexAttribBufferMask;
174    const GLbitfield enabled_nonzero_divisor = enabled & vao->NonZeroDivisorMask;
175 
176    *enabled_user_attribs = ~enabled_nonuser & enabled_attribs;
177    *nonzero_divisor_attribs = enabled_nonzero_divisor & enabled_attribs;
178 
179    switch (vao->_AttributeMapMode) {
180    case ATTRIBUTE_MAP_MODE_POSITION:
181       /* Copy VERT_ATTRIB_POS enable bit into GENERIC0 position */
182       *enabled_user_attribs =
183          (*enabled_user_attribs & ~VERT_BIT_GENERIC0) |
184          ((*enabled_user_attribs & VERT_BIT_POS) << VERT_ATTRIB_GENERIC0);
185       *nonzero_divisor_attribs =
186          (*nonzero_divisor_attribs & ~VERT_BIT_GENERIC0) |
187          ((*nonzero_divisor_attribs & VERT_BIT_POS) << VERT_ATTRIB_GENERIC0);
188       break;
189 
190    case ATTRIBUTE_MAP_MODE_GENERIC0:
191       /* Copy VERT_ATTRIB_GENERIC0 enable bit into POS position */
192       *enabled_user_attribs =
193          (*enabled_user_attribs & ~VERT_BIT_POS) |
194          ((*enabled_user_attribs & VERT_BIT_GENERIC0) >> VERT_ATTRIB_GENERIC0);
195       *nonzero_divisor_attribs =
196          (*nonzero_divisor_attribs & ~VERT_BIT_POS) |
197          ((*nonzero_divisor_attribs & VERT_BIT_GENERIC0) >> VERT_ATTRIB_GENERIC0);
198       break;
199    default:
200       break;
201    }
202 }
203 
204 
205 /**
206  * Return vertex buffer binding provided the attribute struct.
207  *
208  * Needs the a fully updated VAO ready for draw.
209  */
210 static inline const struct gl_vertex_buffer_binding*
_mesa_draw_buffer_binding_from_attrib(const struct gl_vertex_array_object * vao,const struct gl_array_attributes * attrib)211 _mesa_draw_buffer_binding_from_attrib(const struct gl_vertex_array_object *vao,
212                                       const struct gl_array_attributes *attrib)
213 {
214    return &vao->BufferBinding[attrib->_EffBufferBindingIndex];
215 }
216 
217 
218 /**
219  * Return vertex array attribute provided the attribute number.
220  */
221 static inline const struct gl_array_attributes*
_mesa_draw_array_attrib(const struct gl_vertex_array_object * vao,gl_vert_attrib attr)222 _mesa_draw_array_attrib(const struct gl_vertex_array_object *vao,
223                         gl_vert_attrib attr)
224 {
225    const gl_attribute_map_mode map_mode = vao->_AttributeMapMode;
226    return &vao->VertexAttrib[_mesa_vao_attribute_map[map_mode][attr]];
227 }
228 
229 
230 /**
231  * Return vertex buffer binding provided an attribute number.
232  */
233 static inline const struct gl_vertex_buffer_binding*
_mesa_draw_buffer_binding(const struct gl_vertex_array_object * vao,gl_vert_attrib attr)234 _mesa_draw_buffer_binding(const struct gl_vertex_array_object *vao,
235                           gl_vert_attrib attr)
236 {
237    const struct gl_array_attributes *const attrib
238       = _mesa_draw_array_attrib(vao, attr);
239    return _mesa_draw_buffer_binding_from_attrib(vao, attrib);
240 }
241 
242 
243 /**
244  * Return vertex attribute bits bound at the provided binding.
245  *
246  * Needs the a fully updated VAO ready for draw.
247  */
248 static inline GLbitfield
_mesa_draw_bound_attrib_bits(const struct gl_vertex_buffer_binding * binding)249 _mesa_draw_bound_attrib_bits(const struct gl_vertex_buffer_binding *binding)
250 {
251    return binding->_EffBoundArrays;
252 }
253 
254 
255 /**
256  * Return the vertex offset bound at the provided binding.
257  *
258  * Needs the a fully updated VAO ready for draw.
259  */
260 static inline GLintptr
_mesa_draw_binding_offset(const struct gl_vertex_buffer_binding * binding)261 _mesa_draw_binding_offset(const struct gl_vertex_buffer_binding *binding)
262 {
263    return binding->_EffOffset;
264 }
265 
266 
267 /**
268  * Return the relative offset of the provided attrib.
269  *
270  * Needs the a fully updated VAO ready for draw.
271  */
272 static inline GLushort
_mesa_draw_attributes_relative_offset(const struct gl_array_attributes * attrib)273 _mesa_draw_attributes_relative_offset(const struct gl_array_attributes *attrib)
274 {
275    return attrib->_EffRelativeOffset;
276 }
277 
278 
279 /**
280  * Return a current value vertex array attribute provided the attribute number.
281  */
282 static inline const struct gl_array_attributes*
_mesa_draw_current_attrib(const struct gl_context * ctx,gl_vert_attrib attr)283 _mesa_draw_current_attrib(const struct gl_context *ctx, gl_vert_attrib attr)
284 {
285    return _vbo_current_attrib(ctx, attr);
286 }
287 
288 
289 #ifdef __cplusplus
290 }
291 #endif
292 
293 #endif /* ARRAYOBJ_H */
294