gfxGLVertexDecl.cpp
Engine/source/gfx/gl/gfxGLVertexDecl.cpp
Detailed Description
1 2#include "gfx/gl/gfxGLDevice.h" 3#include "gfx/gl/gfxGLStateCache.h" 4#include "gfx/gl/gfxGLVertexAttribLocation.h" 5#include "gfx/gl/gfxGLVertexDecl.h" 6 7void GFXGLVertexDecl::init(const GFXVertexFormat *format) 8{ 9 AssertFatal(!mFormat, ""); 10 mFormat = format; 11 12 for(int i = 0; i < GFXGL->getNumVertexStreams(); ++i) 13 _initVerticesFormat(i); 14} 15 16void GFXGLVertexDecl::prepareVertexFormat() const 17{ 18 AssertFatal(mFormat, "GFXGLVertexDecl - Not inited"); 19 if( GFXGL->mCapabilities.vertexAttributeBinding ) 20 { 21 for ( U32 i=0; i < glVerticesFormat.size(); i++ ) 22 { 23 const glVertexAttribData &glElement = glVerticesFormat[i]; 24 25 glVertexAttribFormat( glElement.attrIndex, glElement.elementCount, glElement.type, glElement.normalized, (uintptr_t)glElement.pointerFirst ); 26 glVertexAttribBinding( glElement.attrIndex, glElement.stream ); 27 } 28 29 updateActiveVertexAttrib( GFXGL->getOpenglCache()->getCacheVertexAttribActive() ); 30 31 return; 32 } 33} 34 35void GFXGLVertexDecl::prepareBuffer_old(U32 stream, GLint mBuffer, GLint mDivisor) const 36{ 37 PROFILE_SCOPE(GFXGLVertexDecl_prepare); 38 AssertFatal(mFormat, "GFXGLVertexDecl - Not inited"); 39 40 if( GFXGL->mCapabilities.vertexAttributeBinding ) 41 return; 42 43 // Bind the buffer... 44 glBindBuffer(GL_ARRAY_BUFFER, mBuffer); 45 GFXGL->getOpenglCache()->setCacheBinded(GL_ARRAY_BUFFER, mBuffer); 46 47 // Loop thru the vertex format elements adding the array state... 48 for ( U32 i=0; i < glVerticesFormat.size(); i++ ) 49 { 50 // glEnableVertexAttribArray are called and cache in GFXGLDevice::preDrawPrimitive 51 52 const glVertexAttribData &e = glVerticesFormat[i]; 53 if(e.stream != stream) 54 continue; 55 56 glVertexAttribPointer( 57 e.attrIndex, // attribute 58 e.elementCount, // number of elements per vertex, here (r,g,b) 59 e.type, // the type of each element 60 e.normalized, // take our values as-is 61 e.stride, // stride between each position 62 e.pointerFirst // offset of first element 63 ); 64 glVertexAttribDivisor( e.attrIndex, mDivisor ); 65 } 66} 67 68void GFXGLVertexDecl::updateActiveVertexAttrib(U32 lastActiveMask) const 69{ 70 AssertFatal(mVertexAttribActiveMask, "GFXGLVertexDecl::updateActiveVertexAttrib - No vertex attribute are active"); 71 72 U32 lastActiveVerxtexAttrib = GFXGL->getOpenglCache()->getCacheVertexAttribActive(); 73 if(mVertexAttribActiveMask == lastActiveVerxtexAttrib) 74 return; 75 76 U32 forActiveMask = mVertexAttribActiveMask & ~lastActiveVerxtexAttrib; 77 U32 forDeactiveMask = ~<a href="/coding/class/classgfxglvertexdecl/#classgfxglvertexdecl_1ae9fe2a73b34972b4e90e477a910469ef">mVertexAttribActiveMask</a> & lastActiveVerxtexAttrib; 78 for(int i = 0; i < Torque::GL_VertexAttrib_COUNT; ++i) 79 { 80 if( BIT(i) & forActiveMask ) //if is active but not in last mask 81 glEnableVertexAttribArray(i); 82 else if( BIT(i) & forDeactiveMask ) // if not active but in last mask 83 glDisableVertexAttribArray(i); 84 } 85 86 GFXGL->getOpenglCache()->setCacheVertexAttribActive(mVertexAttribActiveMask); 87} 88 89void GFXGLVertexDecl::_initVerticesFormat2() 90{ 91 for( U32 i=0; i < GFXGL->getNumVertexStreams(); ++i ) 92 { 93 _initVerticesFormat(i); 94 } 95} 96 97void GFXGLVertexDecl::_initVerticesFormat(U32 stream) 98{ 99 U32 buffer = 0; 100 U32 vertexSize = 0; 101 102 for ( U32 i=0; i < mFormat->getElementCount(); i++ ) 103 { 104 const GFXVertexElement &element = mFormat->getElement( i ); 105 106 if(element.getStreamIndex() != stream) 107 continue; 108 109 AssertFatal(!mFormat->hasBlendIndices() || !element.isSemantic(GFXSemantic::TEXCOORD) || (mFormat->hasBlendIndices() && element.isSemantic(GFXSemantic::TEXCOORD) && element.getSemanticIndex() < 2), "skinning with more than 2 used texcoords!"); 110 111 vertexSize += element.getSizeInBytes(); 112 } 113 114 // Loop thru the vertex format elements adding the array state... 115 U32 texCoordIndex = 0; 116 for ( U32 i=0; i < mFormat->getElementCount(); i++ ) 117 { 118 const GFXVertexElement &element = mFormat->getElement( i ); 119 120 if(element.getStreamIndex() != stream) 121 continue; 122 123 glVerticesFormat.increment(); 124 glVertexAttribData &glElement = glVerticesFormat.last(); 125 glElement.stream = element.getStreamIndex(); 126 127 if ( element.isSemantic( GFXSemantic::POSITION ) ) 128 { 129 glElement.attrIndex = Torque::GL_VertexAttrib_Position; 130 glElement.elementCount = element.getSizeInBytes() / 4; 131 glElement.normalized = false; 132 glElement.type = GL_FLOAT; 133 glElement.stride = vertexSize; 134 glElement.pointerFirst = (void*)(uintptr_t)buffer; 135 136 buffer += element.getSizeInBytes(); 137 } 138 else if ( element.isSemantic( GFXSemantic::NORMAL ) ) 139 { 140 glElement.attrIndex = Torque::GL_VertexAttrib_Normal; 141 glElement.elementCount = 3; 142 glElement.normalized = false; 143 glElement.type = GL_FLOAT; 144 glElement.stride = vertexSize; 145 glElement.pointerFirst = (void*)(uintptr_t)buffer; 146 147 buffer += element.getSizeInBytes(); 148 } 149 else if ( element.isSemantic( GFXSemantic::TANGENT ) ) 150 { 151 glElement.attrIndex = Torque::GL_VertexAttrib_Tangent; 152 glElement.elementCount = 3; 153 glElement.normalized = false; 154 glElement.type = GL_FLOAT; 155 glElement.stride = vertexSize; 156 glElement.pointerFirst = (void*)(uintptr_t)buffer; 157 158 buffer += element.getSizeInBytes(); 159 } 160 else if ( element.isSemantic( GFXSemantic::TANGENTW ) ) 161 { 162 glElement.attrIndex = Torque::GL_VertexAttrib_TangentW; 163 glElement.elementCount = element.getSizeInBytes()/4; 164 glElement.normalized = false; 165 glElement.type = GL_FLOAT; 166 glElement.stride = vertexSize; 167 glElement.pointerFirst = (void*)(uintptr_t)buffer; 168 169 buffer += element.getSizeInBytes(); 170 } 171 else if ( element.isSemantic( GFXSemantic::BINORMAL ) ) 172 { 173 glElement.attrIndex = Torque::GL_VertexAttrib_Binormal; 174 glElement.elementCount = 3; 175 glElement.normalized = false; 176 glElement.type = GL_FLOAT; 177 glElement.stride = vertexSize; 178 glElement.pointerFirst = (void*)(uintptr_t)buffer; 179 180 buffer += element.getSizeInBytes(); 181 } 182 else if ( element.isSemantic( GFXSemantic::COLOR ) ) 183 { 184 glElement.attrIndex = Torque::GL_VertexAttrib_Color; 185 glElement.elementCount = element.getSizeInBytes(); 186 glElement.normalized = true; 187 glElement.type = GL_UNSIGNED_BYTE; 188 glElement.stride = vertexSize; 189 glElement.pointerFirst = (void*)(uintptr_t)buffer; 190 191 buffer += element.getSizeInBytes(); 192 } 193 else if ( element.isSemantic( GFXSemantic::BLENDWEIGHT ) ) 194 { 195 glElement.attrIndex = Torque::GL_VertexAttrib_BlendWeight0 + element.getSemanticIndex(); 196 glElement.elementCount = 4; 197 glElement.normalized = false; 198 glElement.type = GL_FLOAT; 199 glElement.stride = vertexSize; 200 glElement.pointerFirst = (void*)(uintptr_t)buffer; 201 202 buffer += element.getSizeInBytes(); 203 } 204 else if ( element.isSemantic( GFXSemantic::BLENDINDICES ) ) 205 { 206 glElement.attrIndex = Torque::GL_VertexAttrib_BlendIndex0 + element.getSemanticIndex(); 207 glElement.elementCount = 4; 208 glElement.normalized = false; 209 glElement.type = GL_UNSIGNED_BYTE; 210 glElement.stride = vertexSize; 211 glElement.pointerFirst = (void*)(uintptr_t)buffer; 212 213 buffer += element.getSizeInBytes(); 214 } 215 else // Everything else is a texture coordinate. 216 { 217 String name = element.getSemantic(); 218 glElement.elementCount = element.getSizeInBytes() / 4; 219 texCoordIndex = getMax(texCoordIndex, element.getSemanticIndex()); 220 glElement.attrIndex = Torque::GL_VertexAttrib_TexCoord0 + texCoordIndex; 221 222 glElement.normalized = false; 223 glElement.type = GL_FLOAT; 224 glElement.stride = vertexSize; 225 glElement.pointerFirst = (void*)(uintptr_t)buffer; 226 227 buffer += element.getSizeInBytes(); 228 ++texCoordIndex; 229 } 230 231 AssertFatal(!( mVertexAttribActiveMask & BIT(glElement.attrIndex) ), "GFXGLVertexBuffer::_initVerticesFormat - Duplicate vertex attrib index"); 232 mVertexAttribActiveMask |= BIT(glElement.attrIndex); 233 } 234 235 mVertexSize[stream] = vertexSize; 236 AssertFatal(vertexSize == buffer, ""); 237} 238