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