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