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 | |
Geoff Lang | d47e0fc | 2013-08-29 11:40:43 -0400 | [diff] [blame^] | 11 | #include "libGLESv2/renderer/d3d11/InputLayoutCache.h" |
| 12 | #include "libGLESv2/renderer/d3d11/VertexBuffer11.h" |
| 13 | #include "libGLESv2/renderer/d3d11/BufferStorage11.h" |
| 14 | #include "libGLESv2/renderer/d3d11/ShaderExecutable11.h" |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 15 | #include "libGLESv2/ProgramBinary.h" |
Jamie Madill | 8793971 | 2013-07-02 11:57:01 -0400 | [diff] [blame] | 16 | #include "libGLESv2/VertexAttribute.h" |
shannon.woods@transgaming.com | 486d9e9 | 2013-02-28 23:15:41 +0000 | [diff] [blame] | 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; |
Geoff Lang | 1f53cab | 2013-07-22 10:37:22 -0400 | [diff] [blame] | 31 | mCurrentIL = NULL; |
| 32 | for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 33 | { |
| 34 | mCurrentBuffers[i] = -1; |
| 35 | mCurrentVertexStrides[i] = -1; |
| 36 | mCurrentVertexOffsets[i] = -1; |
| 37 | } |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | InputLayoutCache::~InputLayoutCache() |
| 41 | { |
| 42 | clear(); |
| 43 | } |
| 44 | |
| 45 | void InputLayoutCache::initialize(ID3D11Device *device, ID3D11DeviceContext *context) |
| 46 | { |
| 47 | clear(); |
| 48 | mDevice = device; |
| 49 | mDeviceContext = context; |
| 50 | } |
| 51 | |
| 52 | void InputLayoutCache::clear() |
| 53 | { |
| 54 | for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++) |
| 55 | { |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 56 | SafeRelease(i->second.inputLayout); |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 57 | } |
| 58 | mInputLayoutMap.clear(); |
Geoff Lang | 1f53cab | 2013-07-22 10:37:22 -0400 | [diff] [blame] | 59 | markDirty(); |
| 60 | } |
| 61 | |
| 62 | void InputLayoutCache::markDirty() |
| 63 | { |
| 64 | mCurrentIL = NULL; |
| 65 | for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 66 | { |
| 67 | mCurrentBuffers[i] = -1; |
| 68 | mCurrentVertexStrides[i] = -1; |
| 69 | mCurrentVertexOffsets[i] = -1; |
| 70 | } |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | GLenum InputLayoutCache::applyVertexBuffers(TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS], |
| 74 | gl::ProgramBinary *programBinary) |
| 75 | { |
shannon.woods@transgaming.com | 0b60014 | 2013-02-28 23:15:04 +0000 | [diff] [blame] | 76 | int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]; |
| 77 | programBinary->sortAttributesByLayout(attributes, sortedSemanticIndices); |
| 78 | |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 79 | if (!mDevice || !mDeviceContext) |
| 80 | { |
| 81 | ERR("InputLayoutCache is not initialized."); |
| 82 | return GL_INVALID_OPERATION; |
| 83 | } |
| 84 | |
| 85 | InputLayoutKey ilKey = { 0 }; |
| 86 | |
| 87 | ID3D11Buffer *vertexBuffers[gl::MAX_VERTEX_ATTRIBS] = { NULL }; |
Geoff Lang | 1f53cab | 2013-07-22 10:37:22 -0400 | [diff] [blame] | 88 | unsigned int vertexBufferSerials[gl::MAX_VERTEX_ATTRIBS] = { 0 }; |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 89 | UINT vertexStrides[gl::MAX_VERTEX_ATTRIBS] = { 0 }; |
| 90 | UINT vertexOffsets[gl::MAX_VERTEX_ATTRIBS] = { 0 }; |
| 91 | |
| 92 | static const char* semanticName = "TEXCOORD"; |
| 93 | |
| 94 | for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 95 | { |
| 96 | if (attributes[i].active) |
| 97 | { |
| 98 | VertexBuffer11 *vertexBuffer = VertexBuffer11::makeVertexBuffer11(attributes[i].vertexBuffer); |
shannon.woods@transgaming.com | db1899c | 2013-02-28 23:08:33 +0000 | [diff] [blame] | 99 | BufferStorage11 *bufferStorage = attributes[i].storage ? BufferStorage11::makeBufferStorage11(attributes[i].storage) : NULL; |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 100 | |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 101 | D3D11_INPUT_CLASSIFICATION inputClass = attributes[i].divisor > 0 ? D3D11_INPUT_PER_INSTANCE_DATA : D3D11_INPUT_PER_VERTEX_DATA; |
Jamie Madill | a857c36 | 2013-07-02 11:57:02 -0400 | [diff] [blame] | 102 | DXGI_FORMAT dxgiFormat = attributes[i].attribute->mArrayEnabled ? |
| 103 | VertexBuffer11::getAttributeDXGIFormat(*attributes[i].attribute) : |
| 104 | VertexBuffer11::getCurrentValueDXGIFormat(attributes[i].currentValueType); |
shannon.woods@transgaming.com | 00032cb | 2013-01-25 21:56:30 +0000 | [diff] [blame] | 105 | |
shannon.woods@transgaming.com | 0a71ecf | 2013-02-28 23:15:10 +0000 | [diff] [blame] | 106 | // Record the type of the associated vertex shader vector in our key |
| 107 | // This will prevent mismatched vertex shaders from using the same input layout |
| 108 | GLint attributeSize; |
Al Patrick | 978911c | 2013-08-15 12:43:29 -0700 | [diff] [blame] | 109 | programBinary->getActiveAttribute(ilKey.elementCount, 0, NULL, &attributeSize, &ilKey.elements[ilKey.elementCount].glslElementType, NULL); |
shannon.woods@transgaming.com | 0a71ecf | 2013-02-28 23:15:10 +0000 | [diff] [blame] | 110 | |
Al Patrick | 978911c | 2013-08-15 12:43:29 -0700 | [diff] [blame] | 111 | ilKey.elements[ilKey.elementCount].desc.SemanticName = semanticName; |
| 112 | ilKey.elements[ilKey.elementCount].desc.SemanticIndex = sortedSemanticIndices[i]; |
| 113 | ilKey.elements[ilKey.elementCount].desc.Format = dxgiFormat; |
| 114 | ilKey.elements[ilKey.elementCount].desc.InputSlot = i; |
| 115 | ilKey.elements[ilKey.elementCount].desc.AlignedByteOffset = 0; |
| 116 | ilKey.elements[ilKey.elementCount].desc.InputSlotClass = inputClass; |
| 117 | ilKey.elements[ilKey.elementCount].desc.InstanceDataStepRate = attributes[i].divisor; |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 118 | ilKey.elementCount++; |
| 119 | |
shannonwoods@chromium.org | 675526e | 2013-05-30 00:04:49 +0000 | [diff] [blame] | 120 | vertexBuffers[i] = bufferStorage ? bufferStorage->getBuffer(GL_ARRAY_BUFFER) : vertexBuffer->getBuffer(); |
Geoff Lang | 1f53cab | 2013-07-22 10:37:22 -0400 | [diff] [blame] | 121 | vertexBufferSerials[i] = bufferStorage ? bufferStorage->getSerial() : vertexBuffer->getSerial(); |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 122 | vertexStrides[i] = attributes[i].stride; |
| 123 | vertexOffsets[i] = attributes[i].offset; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | ID3D11InputLayout *inputLayout = NULL; |
| 128 | |
| 129 | InputLayoutMap::iterator i = mInputLayoutMap.find(ilKey); |
| 130 | if (i != mInputLayoutMap.end()) |
| 131 | { |
| 132 | inputLayout = i->second.inputLayout; |
| 133 | i->second.lastUsedTime = mCounter++; |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | ShaderExecutable11 *shader = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable()); |
| 138 | |
Al Patrick | 978911c | 2013-08-15 12:43:29 -0700 | [diff] [blame] | 139 | D3D11_INPUT_ELEMENT_DESC descs[gl::MAX_VERTEX_ATTRIBS]; |
| 140 | for (unsigned int j = 0; j < ilKey.elementCount; ++j) |
| 141 | { |
| 142 | descs[j] = ilKey.elements[j].desc; |
| 143 | } |
| 144 | |
| 145 | HRESULT result = mDevice->CreateInputLayout(descs, ilKey.elementCount, shader->getFunction(), shader->getLength(), &inputLayout); |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 146 | if (FAILED(result)) |
| 147 | { |
| 148 | ERR("Failed to crate input layout, result: 0x%08x", result); |
| 149 | return GL_INVALID_OPERATION; |
| 150 | } |
| 151 | |
| 152 | if (mInputLayoutMap.size() >= kMaxInputLayouts) |
| 153 | { |
| 154 | TRACE("Overflowed the limit of %u input layouts, removing the least recently used " |
| 155 | "to make room.", kMaxInputLayouts); |
| 156 | |
| 157 | InputLayoutMap::iterator leastRecentlyUsed = mInputLayoutMap.begin(); |
| 158 | for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++) |
| 159 | { |
| 160 | if (i->second.lastUsedTime < leastRecentlyUsed->second.lastUsedTime) |
| 161 | { |
| 162 | leastRecentlyUsed = i; |
| 163 | } |
| 164 | } |
Geoff Lang | ea22863 | 2013-07-30 15:17:12 -0400 | [diff] [blame] | 165 | SafeRelease(leastRecentlyUsed->second.inputLayout); |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 166 | mInputLayoutMap.erase(leastRecentlyUsed); |
| 167 | } |
| 168 | |
| 169 | InputLayoutCounterPair inputCounterPair; |
| 170 | inputCounterPair.inputLayout = inputLayout; |
| 171 | inputCounterPair.lastUsedTime = mCounter++; |
| 172 | |
| 173 | mInputLayoutMap.insert(std::make_pair(ilKey, inputCounterPair)); |
| 174 | } |
| 175 | |
Geoff Lang | 1f53cab | 2013-07-22 10:37:22 -0400 | [diff] [blame] | 176 | if (inputLayout != mCurrentIL) |
| 177 | { |
| 178 | mDeviceContext->IASetInputLayout(inputLayout); |
| 179 | mCurrentIL = inputLayout; |
| 180 | } |
| 181 | |
| 182 | for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 183 | { |
| 184 | if (vertexBufferSerials[i] != mCurrentBuffers[i] || vertexStrides[i] != mCurrentVertexStrides[i] || |
| 185 | vertexOffsets[i] != mCurrentVertexOffsets[i]) |
| 186 | { |
| 187 | mDeviceContext->IASetVertexBuffers(i, 1, &vertexBuffers[i], &vertexStrides[i], &vertexOffsets[i]); |
| 188 | mCurrentBuffers[i] = vertexBufferSerials[i]; |
| 189 | mCurrentVertexStrides[i] = vertexStrides[i]; |
| 190 | mCurrentVertexOffsets[i] = vertexOffsets[i]; |
| 191 | } |
| 192 | } |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 193 | |
| 194 | return GL_NO_ERROR; |
| 195 | } |
| 196 | |
| 197 | std::size_t InputLayoutCache::hashInputLayout(const InputLayoutKey &inputLayout) |
| 198 | { |
| 199 | static const unsigned int seed = 0xDEADBEEF; |
| 200 | |
| 201 | std::size_t hash = 0; |
Al Patrick | 978911c | 2013-08-15 12:43:29 -0700 | [diff] [blame] | 202 | MurmurHash3_x86_32(inputLayout.begin(), inputLayout.end() - inputLayout.begin(), seed, &hash); |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 203 | return hash; |
| 204 | } |
| 205 | |
| 206 | bool InputLayoutCache::compareInputLayouts(const InputLayoutKey &a, const InputLayoutKey &b) |
| 207 | { |
Al Patrick | 978911c | 2013-08-15 12:43:29 -0700 | [diff] [blame] | 208 | if (a.elementCount != b.elementCount) |
| 209 | { |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | return std::equal(a.begin(), a.end(), b.begin()); |
daniel@transgaming.com | cd9458d | 2012-12-20 21:10:09 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | } |