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