daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2012 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 | |
| 7 | // InputLayoutCache.cpp: Defines InputLayoutCache, a class that builds and caches |
| 8 | // D3D11 input layouts. |
| 9 | |
| 10 | #include "libGLESv2/renderer/InputLayoutCache.h" |
| 11 | #include "libGLESv2/renderer/VertexBuffer11.h" |
| 12 | #include "libGLESv2/renderer/ShaderExecutable11.h" |
| 13 | #include "libGLESv2/ProgramBinary.h" |
| 14 | |
| 15 | #include "third_party/murmurhash/MurmurHash3.h" |
| 16 | |
| 17 | namespace rx |
| 18 | { |
| 19 | |
| 20 | const unsigned int InputLayoutCache::kMaxInputLayouts = 1024; |
| 21 | |
| 22 | InputLayoutCache::InputLayoutCache() : mInputLayoutMap(kMaxInputLayouts, hashInputLayout, compareInputLayouts) |
| 23 | { |
| 24 | mCounter = 0; |
| 25 | mDevice = NULL; |
| 26 | mDeviceContext = NULL; |
| 27 | } |
| 28 | |
| 29 | InputLayoutCache::~InputLayoutCache() |
| 30 | { |
| 31 | clear(); |
| 32 | } |
| 33 | |
| 34 | void InputLayoutCache::initialize(ID3D11Device *device, ID3D11DeviceContext *context) |
| 35 | { |
| 36 | clear(); |
| 37 | mDevice = device; |
| 38 | mDeviceContext = context; |
| 39 | } |
| 40 | |
| 41 | void InputLayoutCache::clear() |
| 42 | { |
| 43 | for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++) |
| 44 | { |
| 45 | i->second.inputLayout->Release(); |
| 46 | } |
| 47 | mInputLayoutMap.clear(); |
| 48 | } |
| 49 | |
| 50 | GLenum InputLayoutCache::applyVertexBuffers(TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS], |
| 51 | gl::ProgramBinary *programBinary) |
| 52 | { |
| 53 | if (!mDevice || !mDeviceContext) |
| 54 | { |
| 55 | ERR("InputLayoutCache is not initialized."); |
| 56 | return GL_INVALID_OPERATION; |
| 57 | } |
| 58 | |
| 59 | InputLayoutKey ilKey = { 0 }; |
| 60 | |
| 61 | ID3D11Buffer *vertexBuffers[gl::MAX_VERTEX_ATTRIBS] = { NULL }; |
| 62 | UINT vertexStrides[gl::MAX_VERTEX_ATTRIBS] = { 0 }; |
| 63 | UINT vertexOffsets[gl::MAX_VERTEX_ATTRIBS] = { 0 }; |
| 64 | |
| 65 | static const char* semanticName = "TEXCOORD"; |
| 66 | |
| 67 | for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 68 | { |
| 69 | if (attributes[i].active) |
| 70 | { |
| 71 | VertexBuffer11 *vertexBuffer = VertexBuffer11::makeVertexBuffer11(attributes[i].vertexBuffer); |
| 72 | |
| 73 | ilKey.elements[ilKey.elementCount].SemanticName = semanticName; |
| 74 | ilKey.elements[ilKey.elementCount].SemanticIndex = programBinary->getSemanticIndex(i); |
| 75 | ilKey.elements[ilKey.elementCount].Format = attributes[i].attribute->mArrayEnabled ? vertexBuffer->getDXGIFormat(*attributes[i].attribute) : DXGI_FORMAT_R32G32B32A32_FLOAT; |
| 76 | ilKey.elements[ilKey.elementCount].InputSlot = i; |
| 77 | ilKey.elements[ilKey.elementCount].AlignedByteOffset = 0; |
| 78 | ilKey.elements[ilKey.elementCount].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; |
| 79 | ilKey.elements[ilKey.elementCount].InstanceDataStepRate = 0; |
| 80 | ilKey.elementCount++; |
| 81 | |
| 82 | vertexBuffers[i] = vertexBuffer->getBuffer(); |
| 83 | vertexStrides[i] = attributes[i].stride; |
| 84 | vertexOffsets[i] = attributes[i].offset; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | ID3D11InputLayout *inputLayout = NULL; |
| 89 | |
| 90 | InputLayoutMap::iterator i = mInputLayoutMap.find(ilKey); |
| 91 | if (i != mInputLayoutMap.end()) |
| 92 | { |
| 93 | inputLayout = i->second.inputLayout; |
| 94 | i->second.lastUsedTime = mCounter++; |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | ShaderExecutable11 *shader = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable()); |
| 99 | |
| 100 | HRESULT result = mDevice->CreateInputLayout(ilKey.elements, ilKey.elementCount, shader->getFunction(), shader->getLength(), &inputLayout); |
| 101 | if (FAILED(result)) |
| 102 | { |
| 103 | ERR("Failed to crate input layout, result: 0x%08x", result); |
| 104 | return GL_INVALID_OPERATION; |
| 105 | } |
| 106 | |
| 107 | if (mInputLayoutMap.size() >= kMaxInputLayouts) |
| 108 | { |
| 109 | TRACE("Overflowed the limit of %u input layouts, removing the least recently used " |
| 110 | "to make room.", kMaxInputLayouts); |
| 111 | |
| 112 | InputLayoutMap::iterator leastRecentlyUsed = mInputLayoutMap.begin(); |
| 113 | for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++) |
| 114 | { |
| 115 | if (i->second.lastUsedTime < leastRecentlyUsed->second.lastUsedTime) |
| 116 | { |
| 117 | leastRecentlyUsed = i; |
| 118 | } |
| 119 | } |
| 120 | leastRecentlyUsed->second.inputLayout->Release(); |
| 121 | mInputLayoutMap.erase(leastRecentlyUsed); |
| 122 | } |
| 123 | |
| 124 | InputLayoutCounterPair inputCounterPair; |
| 125 | inputCounterPair.inputLayout = inputLayout; |
| 126 | inputCounterPair.lastUsedTime = mCounter++; |
| 127 | |
| 128 | mInputLayoutMap.insert(std::make_pair(ilKey, inputCounterPair)); |
| 129 | } |
| 130 | |
| 131 | mDeviceContext->IASetInputLayout(inputLayout); |
| 132 | mDeviceContext->IASetVertexBuffers(0, gl::MAX_VERTEX_ATTRIBS, vertexBuffers, vertexStrides, vertexOffsets); |
| 133 | |
| 134 | return GL_NO_ERROR; |
| 135 | } |
| 136 | |
| 137 | std::size_t InputLayoutCache::hashInputLayout(const InputLayoutKey &inputLayout) |
| 138 | { |
| 139 | static const unsigned int seed = 0xDEADBEEF; |
| 140 | |
| 141 | std::size_t hash = 0; |
| 142 | MurmurHash3_x86_32(&inputLayout, sizeof(InputLayoutKey), seed, &hash); |
| 143 | return hash; |
| 144 | } |
| 145 | |
| 146 | bool InputLayoutCache::compareInputLayouts(const InputLayoutKey &a, const InputLayoutKey &b) |
| 147 | { |
| 148 | return memcmp(&a, &b, sizeof(InputLayoutKey)) == 0; |
| 149 | } |
| 150 | |
| 151 | } |