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