Move the vertex data manager and vertex declaration cache from Context to Renderer9.

TRAC #22016
Signed-off-by: Daniel Koch
Signed-off-by: Shannon Woods
Author: Nicolas Capens

git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1474 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/Context.cpp b/src/libGLESv2/Context.cpp
index 5100fce..aac149b 100644
--- a/src/libGLESv2/Context.cpp
+++ b/src/libGLESv2/Context.cpp
@@ -153,7 +153,6 @@
     mState.unpackAlignment = 4;
     mState.packReverseRowOrder = false;
 
-    mVertexDataManager = NULL;
     mIndexDataManager = NULL;
     mLineLoopIB = NULL;
 
@@ -237,7 +236,6 @@
     mTexture2DZero.set(NULL);
     mTextureCubeMapZero.set(NULL);
 
-    delete mVertexDataManager;
     delete mIndexDataManager;
     delete mLineLoopIB;
 
@@ -255,7 +253,6 @@
 
     if (!mHasBeenCurrent)
     {
-        mVertexDataManager = new VertexDataManager(mRenderer);
         mIndexDataManager = new IndexDataManager(mRenderer);
 
         mSupportsShaderModel3 = mRenderer->getShaderModel3Support();
@@ -349,8 +346,6 @@
     mAppliedProgramBinarySerial = 0;
     mAppliedIBSerial = 0;
 
-    mVertexDeclarationCache.markStateDirty();
-
     mDxUniformsDirty = true;
 }
 
@@ -1854,18 +1849,6 @@
                                     mState.rasterizer.frontFace == GL_CCW, stencilSize);
 }
 
-GLenum Context::applyVertexBuffer(ProgramBinary *programBinary, VertexAttributeArray &vertexAttributes, GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
-{
-    TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
-    GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances);
-    if (err != GL_NO_ERROR)
-    {
-        return err;
-    }
-    
-    return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, repeatDraw);
-}
-
 // Applies the indices and element array bindings to the Direct3D 9 device
 GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
 {
@@ -2267,7 +2250,7 @@
     ProgramBinary *programBinary = getCurrentProgramBinary();
 
     GLsizei repeatDraw = 1;
-    GLenum err = applyVertexBuffer(programBinary, mState.vertexAttribute, first, count, instances, &repeatDraw);
+    GLenum err = mRenderer->applyVertexBuffer(programBinary, mState.vertexAttribute, first, count, instances, &repeatDraw);
     if (err != GL_NO_ERROR)
     {
         return error(err);
@@ -2359,7 +2342,7 @@
 
     GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
     GLsizei repeatDraw = 1;
-    err = applyVertexBuffer(programBinary, mState.vertexAttribute, indexInfo.minIndex, vertexCount, instances, &repeatDraw);
+    err = mRenderer->applyVertexBuffer(programBinary, mState.vertexAttribute, indexInfo.minIndex, vertexCount, instances, &repeatDraw);
     if (err != GL_NO_ERROR)
     {
         return error(err);
@@ -3012,8 +2995,6 @@
     mState.vertexAttribute[index].mCurrentValue[1] = values[1];
     mState.vertexAttribute[index].mCurrentValue[2] = values[2];
     mState.vertexAttribute[index].mCurrentValue[3] = values[3];
-
-    mVertexDataManager->dirtyCurrentValue(index);
 }
 
 void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
@@ -3406,197 +3387,6 @@
     }
 }
 
-VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
-{
-    for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
-    {
-        mVertexDeclCache[i].vertexDeclaration = NULL;
-        mVertexDeclCache[i].lruCount = 0;
-    }
-}
-
-VertexDeclarationCache::~VertexDeclarationCache()
-{
-    for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
-    {
-        if (mVertexDeclCache[i].vertexDeclaration)
-        {
-            mVertexDeclCache[i].vertexDeclaration->Release();
-        }
-    }
-}
-
-GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], ProgramBinary *programBinary, GLsizei instances, GLsizei *repeatDraw)
-{
-    *repeatDraw = 1;
-
-    int indexedAttribute = MAX_VERTEX_ATTRIBS;
-    int instancedAttribute = MAX_VERTEX_ATTRIBS;
-
-    if (instances > 0)
-    {
-        // Find an indexed attribute to be mapped to D3D stream 0
-        for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
-        {
-            if (attributes[i].active)
-            {
-                if (indexedAttribute == MAX_VERTEX_ATTRIBS)
-                {
-                    if (attributes[i].divisor == 0)
-                    {
-                        indexedAttribute = i;
-                    }
-                }
-                else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
-                {
-                    if (attributes[i].divisor != 0)
-                    {
-                        instancedAttribute = i;
-                    }
-                }
-                else break;   // Found both an indexed and instanced attribute
-            }
-        }
-
-        if (indexedAttribute == MAX_VERTEX_ATTRIBS)
-        {
-            return GL_INVALID_OPERATION;
-        }
-    }
-
-    D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
-    D3DVERTEXELEMENT9 *element = &elements[0];
-
-    for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
-    {
-        if (attributes[i].active)
-        {
-            int stream = i;
-
-            if (instances > 0)
-            {
-                // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
-                if (instancedAttribute == MAX_VERTEX_ATTRIBS)
-                {
-                    *repeatDraw = instances;
-                }
-                else
-                {
-                    if (i == indexedAttribute)
-                    {
-                        stream = 0;
-                    }
-                    else if (i == 0)
-                    {
-                        stream = indexedAttribute;
-                    }
-
-                    UINT frequency = 1;
-                    
-                    if (attributes[i].divisor == 0)
-                    {
-                        frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
-                    }
-                    else
-                    {
-                        frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
-                    }
-                    
-                    device->SetStreamSourceFreq(stream, frequency);
-                    mInstancingEnabled = true;
-                }
-            }
-
-            if (mAppliedVBs[stream].serial != attributes[i].serial ||
-                mAppliedVBs[stream].stride != attributes[i].stride ||
-                mAppliedVBs[stream].offset != attributes[i].offset)
-            {
-                device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
-                mAppliedVBs[stream].serial = attributes[i].serial;
-                mAppliedVBs[stream].stride = attributes[i].stride;
-                mAppliedVBs[stream].offset = attributes[i].offset;
-            }
-
-            element->Stream = stream;
-            element->Offset = 0;
-            element->Type = attributes[i].type;
-            element->Method = D3DDECLMETHOD_DEFAULT;
-            element->Usage = D3DDECLUSAGE_TEXCOORD;
-            element->UsageIndex = programBinary->getSemanticIndex(i);
-            element++;
-        }
-    }
-
-    if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
-    {
-        if (mInstancingEnabled)
-        {
-            for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
-            {
-                device->SetStreamSourceFreq(i, 1);
-            }
-
-            mInstancingEnabled = false;
-        }
-    }
-
-    static const D3DVERTEXELEMENT9 end = D3DDECL_END();
-    *(element++) = end;
-
-    for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
-    {
-        VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
-        if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
-        {
-            entry->lruCount = ++mMaxLru;
-            if(entry->vertexDeclaration != mLastSetVDecl)
-            {
-                device->SetVertexDeclaration(entry->vertexDeclaration);
-                mLastSetVDecl = entry->vertexDeclaration;
-            }
-
-            return GL_NO_ERROR;
-        }
-    }
-
-    VertexDeclCacheEntry *lastCache = mVertexDeclCache;
-
-    for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
-    {
-        if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
-        {
-            lastCache = &mVertexDeclCache[i];
-        }
-    }
-
-    if (lastCache->vertexDeclaration != NULL)
-    {
-        lastCache->vertexDeclaration->Release();
-        lastCache->vertexDeclaration = NULL;
-        // mLastSetVDecl is set to the replacement, so we don't have to worry
-        // about it.
-    }
-
-    memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
-    device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
-    device->SetVertexDeclaration(lastCache->vertexDeclaration);
-    mLastSetVDecl = lastCache->vertexDeclaration;
-    lastCache->lruCount = ++mMaxLru;
-
-    return GL_NO_ERROR;
-}
-
-void VertexDeclarationCache::markStateDirty()
-{
-    for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
-    {
-        mAppliedVBs[i].serial = 0;
-    }
-
-    mLastSetVDecl = NULL;
-    mInstancingEnabled = true;   // Forces it to be disabled when not used
-}
-
 }
 
 extern "C"