1 // 2 // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 // Helper structure describing a single vertex attribute 7 // 8 9 #ifndef LIBGLESV2_VERTEXATTRIBUTE_H_ 10 #define LIBGLESV2_VERTEXATTRIBUTE_H_ 11 12 #include "libGLESv2/Buffer.h" 13 14 namespace gl 15 { 16 17 struct VertexAttribute 18 { 19 bool enabled; // From glEnable/DisableVertexAttribArray 20 21 GLenum type; 22 GLuint size; 23 bool normalized; 24 bool pureInteger; 25 GLuint stride; // 0 means natural stride 26 27 union 28 { 29 const GLvoid *pointer; 30 GLintptr offset; 31 }; 32 BindingPointer<Buffer> buffer; // Captured when glVertexAttribPointer is called. 33 34 GLuint divisor; 35 36 VertexAttribute(); 37 }; 38 39 template <typename T> QuerySingleVertexAttributeParameter(const VertexAttribute & attrib,GLenum pname)40T QuerySingleVertexAttributeParameter(const VertexAttribute& attrib, GLenum pname) 41 { 42 switch (pname) 43 { 44 case GL_VERTEX_ATTRIB_ARRAY_ENABLED: 45 return static_cast<T>(attrib.enabled ? GL_TRUE : GL_FALSE); 46 case GL_VERTEX_ATTRIB_ARRAY_SIZE: 47 return static_cast<T>(attrib.size); 48 case GL_VERTEX_ATTRIB_ARRAY_STRIDE: 49 return static_cast<T>(attrib.stride); 50 case GL_VERTEX_ATTRIB_ARRAY_TYPE: 51 return static_cast<T>(attrib.type); 52 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED: 53 return static_cast<T>(attrib.normalized ? GL_TRUE : GL_FALSE); 54 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING: 55 return static_cast<T>(attrib.buffer.id()); 56 case GL_VERTEX_ATTRIB_ARRAY_DIVISOR: 57 return static_cast<T>(attrib.divisor); 58 case GL_VERTEX_ATTRIB_ARRAY_INTEGER: 59 return static_cast<T>(attrib.pureInteger ? GL_TRUE : GL_FALSE); 60 default: 61 UNREACHABLE(); 62 return static_cast<T>(0); 63 } 64 } 65 66 size_t ComputeVertexAttributeTypeSize(const VertexAttribute& attrib); 67 size_t ComputeVertexAttributeStride(const VertexAttribute& attrib); 68 69 struct VertexAttribCurrentValueData 70 { 71 union 72 { 73 GLfloat FloatValues[4]; 74 GLint IntValues[4]; 75 GLuint UnsignedIntValues[4]; 76 }; 77 GLenum Type; 78 setFloatValuesVertexAttribCurrentValueData79 void setFloatValues(const GLfloat floatValues[4]) 80 { 81 for (unsigned int valueIndex = 0; valueIndex < 4; valueIndex++) 82 { 83 FloatValues[valueIndex] = floatValues[valueIndex]; 84 } 85 Type = GL_FLOAT; 86 } 87 setIntValuesVertexAttribCurrentValueData88 void setIntValues(const GLint intValues[4]) 89 { 90 for (unsigned int valueIndex = 0; valueIndex < 4; valueIndex++) 91 { 92 IntValues[valueIndex] = intValues[valueIndex]; 93 } 94 Type = GL_INT; 95 } 96 setUnsignedIntValuesVertexAttribCurrentValueData97 void setUnsignedIntValues(const GLuint unsignedIntValues[4]) 98 { 99 for (unsigned int valueIndex = 0; valueIndex < 4; valueIndex++) 100 { 101 UnsignedIntValues[valueIndex] = unsignedIntValues[valueIndex]; 102 } 103 Type = GL_UNSIGNED_INT; 104 } 105 106 bool operator==(const VertexAttribCurrentValueData &other) 107 { 108 return (Type == other.Type && memcmp(FloatValues, other.FloatValues, sizeof(float) * 4) == 0); 109 } 110 111 bool operator!=(const VertexAttribCurrentValueData &other) 112 { 113 return !(*this == other); 114 } 115 }; 116 117 } 118 119 #endif // LIBGLESV2_VERTEXATTRIBUTE_H_ 120