shannon.woods@transgaming.com | bdf2d80 | 2013-02-28 23:16:20 +0000 | [diff] [blame] | 1 | #include "precompiled.h" |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. |
| 4 | // Use of this source code is governed by a BSD-style license that can be |
| 5 | // found in the LICENSE file. |
| 6 | // |
| 7 | |
| 8 | // InputLayoutCache.cpp: Defines InputLayoutCache, a class that builds and caches |
| 9 | // D3D11 input layouts. |
| 10 | |
| 11 | #include "libGLESv2/renderer/InputLayoutCache.h" |
| 12 | #include "libGLESv2/renderer/VertexBuffer11.h" |
shannon.woods@transgaming.com | db1899c | 2013-02-28 23:08:33 +0000 | [diff] [blame] | 13 | #include "libGLESv2/renderer/BufferStorage11.h" |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 14 | #include "libGLESv2/renderer/ShaderExecutable11.h" |
| 15 | #include "libGLESv2/ProgramBinary.h" |
shannon.woods@transgaming.com | 486d9e9 | 2013-02-28 23:15:41 +0000 | [diff] [blame] | 16 | #include "libGLESv2/Context.h" |
| 17 | #include "libGLESv2/renderer/VertexDataManager.h" |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 18 | |
| 19 | #include "third_party/murmurhash/MurmurHash3.h" |
| 20 | |
| 21 | namespace rx |
| 22 | { |
| 23 | |
| 24 | const unsigned int InputLayoutCache::kMaxInputLayouts = 1024; |
| 25 | |
| 26 | InputLayoutCache::InputLayoutCache() : mInputLayoutMap(kMaxInputLayouts, hashInputLayout, compareInputLayouts) |
| 27 | { |
| 28 | mCounter = 0; |
| 29 | mDevice = NULL; |
| 30 | mDeviceContext = NULL; |
| 31 | } |
| 32 | |
| 33 | InputLayoutCache::~InputLayoutCache() |
| 34 | { |
| 35 | clear(); |
| 36 | } |
| 37 | |
| 38 | void InputLayoutCache::initialize(ID3D11Device *device, ID3D11DeviceContext *context) |
| 39 | { |
| 40 | clear(); |
| 41 | mDevice = device; |
| 42 | mDeviceContext = context; |
| 43 | } |
| 44 | |
| 45 | void InputLayoutCache::clear() |
| 46 | { |
| 47 | for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++) |
| 48 | { |
| 49 | i->second.inputLayout->Release(); |
| 50 | } |
| 51 | mInputLayoutMap.clear(); |
| 52 | } |
| 53 | |
| 54 | GLenum InputLayoutCache::applyVertexBuffers(TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS], |
| 55 | gl::ProgramBinary *programBinary) |
| 56 | { |
shannon.woods@transgaming.com | 0b60014 | 2013-02-28 23:15:04 +0000 | [diff] [blame] | 57 | int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]; |
| 58 | programBinary->sortAttributesByLayout(attributes, sortedSemanticIndices); |
| 59 | |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 60 | if (!mDevice || !mDeviceContext) |
| 61 | { |
| 62 | ERR("InputLayoutCache is not initialized."); |
| 63 | return GL_INVALID_OPERATION; |
| 64 | } |
| 65 | |
| 66 | InputLayoutKey ilKey = { 0 }; |
| 67 | |
| 68 | ID3D11Buffer *vertexBuffers[gl::MAX_VERTEX_ATTRIBS] = { NULL }; |
| 69 | UINT vertexStrides[gl::MAX_VERTEX_ATTRIBS] = { 0 }; |
| 70 | UINT vertexOffsets[gl::MAX_VERTEX_ATTRIBS] = { 0 }; |
| 71 | |
| 72 | static const char* semanticName = "TEXCOORD"; |
| 73 | |
| 74 | for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 75 | { |
| 76 | if (attributes[i].active) |
| 77 | { |
| 78 | VertexBuffer11 *vertexBuffer = VertexBuffer11::makeVertexBuffer11(attributes[i].vertexBuffer); |
shannon.woods@transgaming.com | db1899c | 2013-02-28 23:08:33 +0000 | [diff] [blame] | 79 | BufferStorage11 *bufferStorage = attributes[i].storage ? BufferStorage11::makeBufferStorage11(attributes[i].storage) : NULL; |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 80 | |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 81 | D3D11_INPUT_CLASSIFICATION inputClass = attributes[i].divisor > 0 ? D3D11_INPUT_PER_INSTANCE_DATA : D3D11_INPUT_PER_VERTEX_DATA; |
| 82 | |
shannon.woods@transgaming.com | 0a71ecf | 2013-02-28 23:15:10 +0000 | [diff] [blame] | 83 | // Record the type of the associated vertex shader vector in our key |
| 84 | // This will prevent mismatched vertex shaders from using the same input layout |
| 85 | GLint attributeSize; |
| 86 | programBinary->getActiveAttribute(ilKey.elementCount, 0, NULL, &attributeSize, &ilKey.glslElementType[ilKey.elementCount], NULL); |
| 87 | |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 88 | ilKey.elements[ilKey.elementCount].SemanticName = semanticName; |
shannon.woods@transgaming.com | 0b60014 | 2013-02-28 23:15:04 +0000 | [diff] [blame] | 89 | ilKey.elements[ilKey.elementCount].SemanticIndex = sortedSemanticIndices[i]; |
shannon.woods%transgaming.com@gtempaccount.com | cf8d2f8 | 2013-04-13 03:37:34 +0000 | [diff] [blame] | 90 | ilKey.elements[ilKey.elementCount].Format = vertexBuffer->getDXGIFormat(*attributes[i].attribute); |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 91 | ilKey.elements[ilKey.elementCount].InputSlot = i; |
| 92 | ilKey.elements[ilKey.elementCount].AlignedByteOffset = 0; |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 93 | ilKey.elements[ilKey.elementCount].InputSlotClass = inputClass; |
| 94 | ilKey.elements[ilKey.elementCount].InstanceDataStepRate = attributes[i].divisor; |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 95 | ilKey.elementCount++; |
| 96 | |
shannonwoods@chromium.org | 675526e | 2013-05-30 00:04:49 +0000 | [diff] [blame^] | 97 | vertexBuffers[i] = bufferStorage ? bufferStorage->getBuffer(GL_ARRAY_BUFFER) : vertexBuffer->getBuffer(); |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 98 | vertexStrides[i] = attributes[i].stride; |
| 99 | vertexOffsets[i] = attributes[i].offset; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | ID3D11InputLayout *inputLayout = NULL; |
| 104 | |
| 105 | InputLayoutMap::iterator i = mInputLayoutMap.find(ilKey); |
| 106 | if (i != mInputLayoutMap.end()) |
| 107 | { |
| 108 | inputLayout = i->second.inputLayout; |
| 109 | i->second.lastUsedTime = mCounter++; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | ShaderExecutable11 *shader = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable()); |
| 114 | |
| 115 | HRESULT result = mDevice->CreateInputLayout(ilKey.elements, ilKey.elementCount, shader->getFunction(), shader->getLength(), &inputLayout); |
| 116 | if (FAILED(result)) |
| 117 | { |
| 118 | ERR("Failed to crate input layout, result: 0x%08x", result); |
| 119 | return GL_INVALID_OPERATION; |
| 120 | } |
| 121 | |
| 122 | if (mInputLayoutMap.size() >= kMaxInputLayouts) |
| 123 | { |
| 124 | TRACE("Overflowed the limit of %u input layouts, removing the least recently used " |
| 125 | "to make room.", kMaxInputLayouts); |
| 126 | |
| 127 | InputLayoutMap::iterator leastRecentlyUsed = mInputLayoutMap.begin(); |
| 128 | for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++) |
| 129 | { |
| 130 | if (i->second.lastUsedTime < leastRecentlyUsed->second.lastUsedTime) |
| 131 | { |
| 132 | leastRecentlyUsed = i; |
| 133 | } |
| 134 | } |
| 135 | leastRecentlyUsed->second.inputLayout->Release(); |
| 136 | mInputLayoutMap.erase(leastRecentlyUsed); |
| 137 | } |
| 138 | |
| 139 | InputLayoutCounterPair inputCounterPair; |
| 140 | inputCounterPair.inputLayout = inputLayout; |
| 141 | inputCounterPair.lastUsedTime = mCounter++; |
| 142 | |
| 143 | mInputLayoutMap.insert(std::make_pair(ilKey, inputCounterPair)); |
| 144 | } |
| 145 | |
| 146 | mDeviceContext->IASetInputLayout(inputLayout); |
| 147 | mDeviceContext->IASetVertexBuffers(0, gl::MAX_VERTEX_ATTRIBS, vertexBuffers, vertexStrides, vertexOffsets); |
| 148 | |
| 149 | return GL_NO_ERROR; |
| 150 | } |
| 151 | |
| 152 | std::size_t InputLayoutCache::hashInputLayout(const InputLayoutKey &inputLayout) |
| 153 | { |
| 154 | static const unsigned int seed = 0xDEADBEEF; |
| 155 | |
| 156 | std::size_t hash = 0; |
| 157 | MurmurHash3_x86_32(&inputLayout, sizeof(InputLayoutKey), seed, &hash); |
| 158 | return hash; |
| 159 | } |
| 160 | |
| 161 | bool InputLayoutCache::compareInputLayouts(const InputLayoutKey &a, const InputLayoutKey &b) |
| 162 | { |
| 163 | return memcmp(&a, &b, sizeof(InputLayoutKey)) == 0; |
| 164 | } |
| 165 | |
| 166 | } |