xref: /third_party/mesa3d/src/mesa/main/arrayobj.h (revision bf215546)
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 "glheader.h"
31#include "mtypes.h"
32#include "glformats.h"
33#include "vbo/vbo.h"
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39struct gl_context;
40
41/**
42 * \file arrayobj.h
43 * Functions for the GL_ARB_vertex_array_object extension.
44 *
45 * \author Ian Romanick <idr@us.ibm.com>
46 * \author Brian Paul
47 */
48
49/*
50 * Internal functions
51 */
52
53extern struct gl_vertex_array_object *
54_mesa_lookup_vao(struct gl_context *ctx, GLuint id);
55
56extern 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
60extern struct gl_vertex_array_object *
61_mesa_new_vao(struct gl_context *ctx, GLuint name);
62
63extern void
64_mesa_unbind_array_object_vbos(struct gl_context *ctx,
65                               struct gl_vertex_array_object *obj);
66
67extern void
68_mesa_delete_vao(struct gl_context *ctx, struct gl_vertex_array_object *obj);
69
70extern void
71_mesa_reference_vao_(struct gl_context *ctx,
72                     struct gl_vertex_array_object **ptr,
73                     struct gl_vertex_array_object *vao);
74
75static inline void
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
85extern void
86_mesa_initialize_vao(struct gl_context *ctx,
87                     struct gl_vertex_array_object *obj, GLuint name);
88
89
90extern void
91_mesa_update_vao_derived_arrays(struct gl_context *ctx,
92                                struct gl_vertex_array_object *vao);
93
94
95/**
96 * Mark the vao as shared and immutable, do remaining updates.
97 */
98extern void
99_mesa_set_vao_immutable(struct gl_context *ctx,
100                        struct gl_vertex_array_object *vao);
101
102
103extern void
104_mesa_vao_map_arrays(struct gl_context *ctx, struct gl_vertex_array_object *vao,
105                     GLbitfield access);
106
107extern void
108_mesa_vao_map(struct gl_context *ctx, struct gl_vertex_array_object *vao,
109              GLbitfield access);
110
111
112extern void
113_mesa_vao_unmap_arrays(struct gl_context *ctx,
114                       struct gl_vertex_array_object *vao);
115
116extern void
117_mesa_vao_unmap(struct gl_context *ctx,
118                struct gl_vertex_array_object *vao);
119
120
121/**
122 * Array to apply the position/generic0 aliasing map to
123 * an attribute value used in vertex processing inputs to an attribute
124 * as they appear in the vao.
125 */
126extern const GLubyte
127_mesa_vao_attribute_map[ATTRIBUTE_MAP_MODE_MAX][VERT_ATTRIB_MAX];
128
129
130/**
131 * Apply the position/generic0 aliasing map to a bitfield from the vao.
132 * Use for example to convert gl_vertex_array_object::Enabled
133 * or gl_vertex_buffer_binding::_VertexBinding from the vao numbering to
134 * the numbering used with vertex processing inputs.
135 */
136static inline GLbitfield
137_mesa_vao_enable_to_vp_inputs(gl_attribute_map_mode mode, GLbitfield enabled)
138{
139   switch (mode) {
140   case ATTRIBUTE_MAP_MODE_IDENTITY:
141      return enabled;
142   case ATTRIBUTE_MAP_MODE_POSITION:
143      /* Copy VERT_ATTRIB_POS enable bit into GENERIC0 position */
144      return (enabled & ~VERT_BIT_GENERIC0)
145         | ((enabled & VERT_BIT_POS) << VERT_ATTRIB_GENERIC0);
146   case ATTRIBUTE_MAP_MODE_GENERIC0:
147      /* Copy VERT_ATTRIB_GENERIC0 enable bit into POS position */
148      return (enabled & ~VERT_BIT_POS)
149         | ((enabled & VERT_BIT_GENERIC0) >> VERT_ATTRIB_GENERIC0);
150   default:
151      return 0;
152   }
153}
154
155
156/**
157 * Helper functions for consuming backends to walk the
158 * ctx->Array._DrawVAO for driver side array setup.
159 * Note that mesa provides preprocessed minimal binding information
160 * in the VAO. See _mesa_update_vao_derived_arrays for documentation.
161 */
162
163/**
164 * Return enabled vertex attribute bits for draw.
165 */
166static inline GLbitfield
167_mesa_draw_array_bits(const struct gl_context *ctx)
168{
169   return ctx->Array._DrawVAOEnabledAttribs;
170}
171
172
173/**
174 * Return enabled buffer object vertex attribute bits for draw.
175 *
176 * Needs the a fully updated VAO ready for draw.
177 */
178static inline GLbitfield
179_mesa_draw_vbo_array_bits(const struct gl_context *ctx)
180{
181   const struct gl_vertex_array_object *const vao = ctx->Array._DrawVAO;
182   assert(!vao->NewVertexBuffers && !vao->NewVertexElements);
183   return vao->_EffEnabledVBO & ctx->Array._DrawVAOEnabledAttribs;
184}
185
186
187/**
188 * Return enabled user space vertex attribute bits for draw.
189 *
190 * Needs the a fully updated VAO ready for draw.
191 */
192static inline GLbitfield
193_mesa_draw_user_array_bits(const struct gl_context *ctx)
194{
195   const struct gl_vertex_array_object *const vao = ctx->Array._DrawVAO;
196   assert(!vao->NewVertexBuffers && !vao->NewVertexElements);
197   return ~vao->_EffEnabledVBO & ctx->Array._DrawVAOEnabledAttribs;
198}
199
200
201/**
202 * Return which enabled vertex attributes have a non-zero instance divisor.
203 *
204 * Needs the a fully updated VAO ready for draw.
205 */
206static inline GLbitfield
207_mesa_draw_nonzero_divisor_bits(const struct gl_context *ctx)
208{
209   const struct gl_vertex_array_object *const vao = ctx->Array._DrawVAO;
210   assert(!vao->NewVertexBuffers && !vao->NewVertexElements);
211   return vao->_EffEnabledNonZeroDivisor & ctx->Array._DrawVAOEnabledAttribs;
212}
213
214
215/**
216 * Return enabled current values attribute bits for draw.
217 */
218static inline GLbitfield
219_mesa_draw_current_bits(const struct gl_context *ctx)
220{
221   return ~ctx->Array._DrawVAOEnabledAttribs & VERT_BIT_ALL;
222}
223
224
225/**
226 * Return vertex buffer binding provided the attribute struct.
227 *
228 * Needs the a fully updated VAO ready for draw.
229 */
230static inline const struct gl_vertex_buffer_binding*
231_mesa_draw_buffer_binding_from_attrib(const struct gl_vertex_array_object *vao,
232                                      const struct gl_array_attributes *attrib)
233{
234   assert(!vao->NewVertexBuffers && !vao->NewVertexElements);
235   return &vao->BufferBinding[attrib->_EffBufferBindingIndex];
236}
237
238
239/**
240 * Return vertex array attribute provided the attribute number.
241 */
242static inline const struct gl_array_attributes*
243_mesa_draw_array_attrib(const struct gl_vertex_array_object *vao,
244                        gl_vert_attrib attr)
245{
246   assert(!vao->NewVertexBuffers && !vao->NewVertexElements);
247   const gl_attribute_map_mode map_mode = vao->_AttributeMapMode;
248   return &vao->VertexAttrib[_mesa_vao_attribute_map[map_mode][attr]];
249}
250
251
252/**
253 * Return a vertex array vertex format provided the attribute number.
254 */
255static inline const struct gl_vertex_format *
256_mesa_draw_array_format(const struct gl_vertex_array_object *vao,
257                        gl_vert_attrib attr)
258{
259   return &_mesa_draw_array_attrib(vao, attr)->Format;
260}
261
262
263/**
264 * Return vertex buffer binding provided an attribute number.
265 */
266static inline const struct gl_vertex_buffer_binding*
267_mesa_draw_buffer_binding(const struct gl_vertex_array_object *vao,
268                          gl_vert_attrib attr)
269{
270   const struct gl_array_attributes *const attrib
271      = _mesa_draw_array_attrib(vao, attr);
272   return _mesa_draw_buffer_binding_from_attrib(vao, attrib);
273}
274
275
276/**
277 * Return vertex attribute bits bound at the provided binding.
278 *
279 * Needs the a fully updated VAO ready for draw.
280 */
281static inline GLbitfield
282_mesa_draw_bound_attrib_bits(const struct gl_vertex_buffer_binding *binding)
283{
284   return binding->_EffBoundArrays;
285}
286
287
288/**
289 * Return the vertex offset bound at the provided binding.
290 *
291 * Needs the a fully updated VAO ready for draw.
292 */
293static inline GLintptr
294_mesa_draw_binding_offset(const struct gl_vertex_buffer_binding *binding)
295{
296   return binding->_EffOffset;
297}
298
299
300/**
301 * Return the relative offset of the provided attrib.
302 *
303 * Needs the a fully updated VAO ready for draw.
304 */
305static inline GLushort
306_mesa_draw_attributes_relative_offset(const struct gl_array_attributes *attrib)
307{
308   return attrib->_EffRelativeOffset;
309}
310
311
312/**
313 * Return a current value vertex array attribute provided the attribute number.
314 */
315static inline const struct gl_array_attributes*
316_mesa_draw_current_attrib(const struct gl_context *ctx, gl_vert_attrib attr)
317{
318   return _vbo_current_attrib(ctx, attr);
319}
320
321
322/**
323 * Return a current value vertex format provided the attribute number.
324 */
325static inline const struct gl_vertex_format *
326_mesa_draw_current_format(const struct gl_context *ctx, gl_vert_attrib attr)
327{
328   return &_vbo_current_attrib(ctx, attr)->Format;
329}
330
331
332/**
333 * Return true if we have the VERT_ATTRIB_EDGEFLAG array enabled.
334 */
335static inline bool
336_mesa_draw_edge_flag_array_enabled(const struct gl_context *ctx)
337{
338   return ctx->Array._DrawVAOEnabledAttribs & VERT_BIT_EDGEFLAG;
339}
340
341
342#ifdef __cplusplus
343}
344#endif
345
346#endif /* ARRAYOBJ_H */
347