Created InputLayoutCache for D3D11 input layouts and vertex buffer application.

TRAC #22235

Author: Geoff Lang
Signed-off-by: Shannon Woods
Signed-off-by: Daniel Koch

git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1609 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/build_angle.gypi b/src/build_angle.gypi
index 7aef523..addf12d 100644
--- a/src/build_angle.gypi
+++ b/src/build_angle.gypi
@@ -266,6 +266,8 @@
             'libGLESv2/renderer/IndexBuffer11.h',
             'libGLESv2/renderer/IndexDataManager.cpp',
             'libGLESv2/renderer/IndexDataManager.h',
+            'libGLESv2/renderer/InputLayoutCache.cpp',
+            'libGLESv2/renderer/InputLayoutCache.h',
             'libGLESv2/renderer/Renderer.cpp',
             'libGLESv2/renderer/Renderer.h',
             'libGLESv2/renderer/Renderer11.cpp',
diff --git a/src/libGLESv2/libGLESv2.vcxproj b/src/libGLESv2/libGLESv2.vcxproj
index 093d454..5350bb7 100644
--- a/src/libGLESv2/libGLESv2.vcxproj
+++ b/src/libGLESv2/libGLESv2.vcxproj
@@ -253,6 +253,7 @@
     <ClCompile Include="renderer\IndexDataManager.cpp" />

     <ClCompile Include="renderer\ImageSSE2.cpp" />

     <ClCompile Include="renderer\Image11.cpp" />

+    <ClCompile Include="renderer\InputLayoutCache.cpp" />

     <ClCompile Include="renderer\Renderer.cpp" />

     <ClCompile Include="renderer\Renderer11.cpp" />

     <ClCompile Include="renderer\renderer11_utils.cpp" />

@@ -306,6 +307,7 @@
     <ClInclude Include="renderer\IndexBuffer11.h" />

     <ClInclude Include="renderer\IndexBuffer9.h" />

     <ClInclude Include="renderer\IndexDataManager.h" />

+    <ClInclude Include="renderer\InputLayoutCache.h" />

     <ClInclude Include="renderer\Renderer.h" />

     <ClInclude Include="renderer\Renderer11.h" />

     <ClInclude Include="renderer\renderer11_utils.h" />

@@ -355,5 +357,5 @@
   </ItemGroup>

   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

   <ImportGroup Label="ExtensionTargets">

-  </ImportGroup>

-</Project>

+  </ImportGroup>
+</Project>
diff --git a/src/libGLESv2/libGLESv2.vcxproj.filters b/src/libGLESv2/libGLESv2.vcxproj.filters
index bf0c4c9..0cd9b68 100644
--- a/src/libGLESv2/libGLESv2.vcxproj.filters
+++ b/src/libGLESv2/libGLESv2.vcxproj.filters
@@ -164,9 +164,12 @@
     <ClCompile Include="renderer\Image11.cpp">

       <Filter>Renderer</Filter>

     </ClCompile>

+    <ClCompile Include="renderer\InputLayoutCache.cpp">

+      <Filter>Renderer</Filter>

+    </ClCompile>

     <ClCompile Include="Uniform.cpp">

       <Filter>Source Files</Filter>

-    </ClCompile>

+    </ClCompile>
   </ItemGroup>

   <ItemGroup>

     <ClInclude Include="BinaryStream.h">

@@ -340,9 +343,12 @@
     <ClInclude Include="renderer\Image11.h">

       <Filter>Renderer</Filter>

     </ClInclude>

+    <ClInclude Include="renderer\InputLayoutCache.h">

+      <Filter>Renderer</Filter>

+    </ClInclude>

     <ClInclude Include="Uniform.h">

       <Filter>Header Files</Filter>

-    </ClInclude>

+    </ClInclude>
   </ItemGroup>

   <ItemGroup>

     <None Include="libGLESv2.def">

diff --git a/src/libGLESv2/renderer/InputLayoutCache.cpp b/src/libGLESv2/renderer/InputLayoutCache.cpp
new file mode 100644
index 0000000..0a1434b
--- /dev/null
+++ b/src/libGLESv2/renderer/InputLayoutCache.cpp
@@ -0,0 +1,151 @@
+//
+// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+
+// InputLayoutCache.cpp: Defines InputLayoutCache, a class that builds and caches
+// D3D11 input layouts.
+
+#include "libGLESv2/renderer/InputLayoutCache.h"
+#include "libGLESv2/renderer/VertexBuffer11.h"
+#include "libGLESv2/renderer/ShaderExecutable11.h"
+#include "libGLESv2/ProgramBinary.h"
+
+#include "third_party/murmurhash/MurmurHash3.h"
+
+namespace rx
+{
+
+const unsigned int InputLayoutCache::kMaxInputLayouts = 1024;
+
+InputLayoutCache::InputLayoutCache() : mInputLayoutMap(kMaxInputLayouts, hashInputLayout, compareInputLayouts)
+{
+    mCounter = 0;
+    mDevice = NULL;
+    mDeviceContext = NULL;
+}
+
+InputLayoutCache::~InputLayoutCache()
+{
+    clear();
+}
+
+void InputLayoutCache::initialize(ID3D11Device *device, ID3D11DeviceContext *context)
+{
+    clear();
+    mDevice = device;
+    mDeviceContext = context;
+}
+
+void InputLayoutCache::clear()
+{
+    for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++)
+    {
+        i->second.inputLayout->Release();
+    }
+    mInputLayoutMap.clear();
+}
+
+GLenum InputLayoutCache::applyVertexBuffers(TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
+                                            gl::ProgramBinary *programBinary)
+{
+    if (!mDevice || !mDeviceContext)
+    {
+        ERR("InputLayoutCache is not initialized.");
+        return GL_INVALID_OPERATION;
+    }
+
+    InputLayoutKey ilKey = { 0 };
+
+    ID3D11Buffer *vertexBuffers[gl::MAX_VERTEX_ATTRIBS] = { NULL };
+    UINT vertexStrides[gl::MAX_VERTEX_ATTRIBS] = { 0 };
+    UINT vertexOffsets[gl::MAX_VERTEX_ATTRIBS] = { 0 };
+
+    static const char* semanticName = "TEXCOORD";
+
+    for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
+    {
+        if (attributes[i].active)
+        {
+            VertexBuffer11 *vertexBuffer = VertexBuffer11::makeVertexBuffer11(attributes[i].vertexBuffer);
+
+            ilKey.elements[ilKey.elementCount].SemanticName = semanticName;
+            ilKey.elements[ilKey.elementCount].SemanticIndex = programBinary->getSemanticIndex(i);
+            ilKey.elements[ilKey.elementCount].Format = attributes[i].attribute->mArrayEnabled ? vertexBuffer->getDXGIFormat(*attributes[i].attribute) : DXGI_FORMAT_R32G32B32A32_FLOAT;
+            ilKey.elements[ilKey.elementCount].InputSlot = i;
+            ilKey.elements[ilKey.elementCount].AlignedByteOffset = 0;
+            ilKey.elements[ilKey.elementCount].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
+            ilKey.elements[ilKey.elementCount].InstanceDataStepRate = 0;
+            ilKey.elementCount++;
+
+            vertexBuffers[i] = vertexBuffer->getBuffer();
+            vertexStrides[i] = attributes[i].stride;
+            vertexOffsets[i] = attributes[i].offset;
+        }
+    }
+
+    ID3D11InputLayout *inputLayout = NULL;
+
+    InputLayoutMap::iterator i = mInputLayoutMap.find(ilKey);
+    if (i != mInputLayoutMap.end())
+    {
+        inputLayout = i->second.inputLayout;
+        i->second.lastUsedTime = mCounter++;
+    }
+    else
+    {
+        ShaderExecutable11 *shader = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
+
+        HRESULT result = mDevice->CreateInputLayout(ilKey.elements, ilKey.elementCount, shader->getFunction(), shader->getLength(), &inputLayout);
+        if (FAILED(result))
+        {
+            ERR("Failed to crate input layout, result: 0x%08x", result);
+            return GL_INVALID_OPERATION;
+        }
+
+        if (mInputLayoutMap.size() >= kMaxInputLayouts)
+        {
+            TRACE("Overflowed the limit of %u input layouts, removing the least recently used "
+                  "to make room.", kMaxInputLayouts);
+
+            InputLayoutMap::iterator leastRecentlyUsed = mInputLayoutMap.begin();
+            for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++)
+            {
+                if (i->second.lastUsedTime < leastRecentlyUsed->second.lastUsedTime)
+                {
+                    leastRecentlyUsed = i;
+                }
+            }
+            leastRecentlyUsed->second.inputLayout->Release();
+            mInputLayoutMap.erase(leastRecentlyUsed);
+        }
+
+        InputLayoutCounterPair inputCounterPair;
+        inputCounterPair.inputLayout = inputLayout;
+        inputCounterPair.lastUsedTime = mCounter++;
+
+        mInputLayoutMap.insert(std::make_pair(ilKey, inputCounterPair));
+    }
+
+    mDeviceContext->IASetInputLayout(inputLayout);
+    mDeviceContext->IASetVertexBuffers(0, gl::MAX_VERTEX_ATTRIBS, vertexBuffers, vertexStrides, vertexOffsets);
+
+    return GL_NO_ERROR;
+}
+
+std::size_t InputLayoutCache::hashInputLayout(const InputLayoutKey &inputLayout)
+{
+    static const unsigned int seed = 0xDEADBEEF;
+
+    std::size_t hash = 0;
+    MurmurHash3_x86_32(&inputLayout, sizeof(InputLayoutKey), seed, &hash);
+    return hash;
+}
+
+bool InputLayoutCache::compareInputLayouts(const InputLayoutKey &a, const InputLayoutKey &b)
+{
+    return memcmp(&a, &b, sizeof(InputLayoutKey)) == 0;
+}
+
+}
diff --git a/src/libGLESv2/renderer/InputLayoutCache.h b/src/libGLESv2/renderer/InputLayoutCache.h
new file mode 100644
index 0000000..209241a
--- /dev/null
+++ b/src/libGLESv2/renderer/InputLayoutCache.h
@@ -0,0 +1,71 @@
+//
+// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+
+// InputLayoutCache.h: Defines InputLayoutCache, a class that builds and caches
+// D3D11 input layouts.
+
+#ifndef LIBGLESV2_RENDERER_INPUTLAYOUTCACHE_H_
+#define LIBGLESV2_RENDERER_INPUTLAYOUTCACHE_H_
+
+#include "libGLESv2/Context.h"
+
+#include "libGLESv2/renderer/VertexDataManager.h"
+
+#include <D3D11.h>
+#include <unordered_map>
+
+namespace rx
+{
+
+class InputLayoutCache
+{
+  public:
+    InputLayoutCache();
+    virtual ~InputLayoutCache();
+
+    void initialize(ID3D11Device *device, ID3D11DeviceContext *context);
+    void clear();
+
+    GLenum applyVertexBuffers(TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
+                              gl::ProgramBinary *programBinary);
+
+  private:
+    DISALLOW_COPY_AND_ASSIGN(InputLayoutCache);
+
+    struct InputLayoutKey
+    {
+        unsigned int elementCount;
+        D3D11_INPUT_ELEMENT_DESC elements[gl::MAX_VERTEX_ATTRIBS];
+    };
+
+    struct InputLayoutCounterPair
+    {
+        ID3D11InputLayout *inputLayout;
+        unsigned long long lastUsedTime;
+    };
+
+    static std::size_t hashInputLayout(const InputLayoutKey &inputLayout);
+    static bool compareInputLayouts(const InputLayoutKey &a, const InputLayoutKey &b);
+
+    typedef std::size_t (*InputLayoutHashFunction)(const InputLayoutKey &);
+    typedef bool (*InputLayoutEqualityFunction)(const InputLayoutKey &, const InputLayoutKey &);
+    typedef std::unordered_map<InputLayoutKey,
+                               InputLayoutCounterPair,
+                               InputLayoutHashFunction,
+                               InputLayoutEqualityFunction> InputLayoutMap;
+    InputLayoutMap mInputLayoutMap;
+
+    static const unsigned int kMaxInputLayouts;
+
+    unsigned long long mCounter;
+
+    ID3D11Device *mDevice;
+    ID3D11DeviceContext *mDeviceContext;
+};
+
+}
+
+#endif // LIBGLESV2_RENDERER_INPUTLAYOUTCACHE_H_