blob: ea850a810037d052f587a4166613a26c041792e5 [file] [log] [blame]
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +00001//
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"
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +000012#include "libGLESv2/renderer/BufferStorage11.h"
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000013#include "libGLESv2/renderer/ShaderExecutable11.h"
14#include "libGLESv2/ProgramBinary.h"
15
16#include "third_party/murmurhash/MurmurHash3.h"
17
18namespace rx
19{
20
21const unsigned int InputLayoutCache::kMaxInputLayouts = 1024;
22
23InputLayoutCache::InputLayoutCache() : mInputLayoutMap(kMaxInputLayouts, hashInputLayout, compareInputLayouts)
24{
25 mCounter = 0;
26 mDevice = NULL;
27 mDeviceContext = NULL;
28}
29
30InputLayoutCache::~InputLayoutCache()
31{
32 clear();
33}
34
35void InputLayoutCache::initialize(ID3D11Device *device, ID3D11DeviceContext *context)
36{
37 clear();
38 mDevice = device;
39 mDeviceContext = context;
40}
41
42void InputLayoutCache::clear()
43{
44 for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++)
45 {
46 i->second.inputLayout->Release();
47 }
48 mInputLayoutMap.clear();
49}
50
51GLenum InputLayoutCache::applyVertexBuffers(TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
52 gl::ProgramBinary *programBinary)
53{
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +000054 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS];
55 programBinary->sortAttributesByLayout(attributes, sortedSemanticIndices);
56
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000057 if (!mDevice || !mDeviceContext)
58 {
59 ERR("InputLayoutCache is not initialized.");
60 return GL_INVALID_OPERATION;
61 }
62
63 InputLayoutKey ilKey = { 0 };
64
65 ID3D11Buffer *vertexBuffers[gl::MAX_VERTEX_ATTRIBS] = { NULL };
66 UINT vertexStrides[gl::MAX_VERTEX_ATTRIBS] = { 0 };
67 UINT vertexOffsets[gl::MAX_VERTEX_ATTRIBS] = { 0 };
68
69 static const char* semanticName = "TEXCOORD";
70
71 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
72 {
73 if (attributes[i].active)
74 {
75 VertexBuffer11 *vertexBuffer = VertexBuffer11::makeVertexBuffer11(attributes[i].vertexBuffer);
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +000076 BufferStorage11 *bufferStorage = attributes[i].storage ? BufferStorage11::makeBufferStorage11(attributes[i].storage) : NULL;
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000077
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +000078 D3D11_INPUT_CLASSIFICATION inputClass = attributes[i].divisor > 0 ? D3D11_INPUT_PER_INSTANCE_DATA : D3D11_INPUT_PER_VERTEX_DATA;
79
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000080 ilKey.elements[ilKey.elementCount].SemanticName = semanticName;
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +000081 ilKey.elements[ilKey.elementCount].SemanticIndex = sortedSemanticIndices[i];
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000082 ilKey.elements[ilKey.elementCount].Format = attributes[i].attribute->mArrayEnabled ? vertexBuffer->getDXGIFormat(*attributes[i].attribute) : DXGI_FORMAT_R32G32B32A32_FLOAT;
83 ilKey.elements[ilKey.elementCount].InputSlot = i;
84 ilKey.elements[ilKey.elementCount].AlignedByteOffset = 0;
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +000085 ilKey.elements[ilKey.elementCount].InputSlotClass = inputClass;
86 ilKey.elements[ilKey.elementCount].InstanceDataStepRate = attributes[i].divisor;
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000087 ilKey.elementCount++;
88
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +000089 vertexBuffers[i] = bufferStorage ? bufferStorage->getBuffer() : vertexBuffer->getBuffer();
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000090 vertexStrides[i] = attributes[i].stride;
91 vertexOffsets[i] = attributes[i].offset;
92 }
93 }
94
95 ID3D11InputLayout *inputLayout = NULL;
96
97 InputLayoutMap::iterator i = mInputLayoutMap.find(ilKey);
98 if (i != mInputLayoutMap.end())
99 {
100 inputLayout = i->second.inputLayout;
101 i->second.lastUsedTime = mCounter++;
102 }
103 else
104 {
105 ShaderExecutable11 *shader = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
106
107 HRESULT result = mDevice->CreateInputLayout(ilKey.elements, ilKey.elementCount, shader->getFunction(), shader->getLength(), &inputLayout);
108 if (FAILED(result))
109 {
110 ERR("Failed to crate input layout, result: 0x%08x", result);
111 return GL_INVALID_OPERATION;
112 }
113
114 if (mInputLayoutMap.size() >= kMaxInputLayouts)
115 {
116 TRACE("Overflowed the limit of %u input layouts, removing the least recently used "
117 "to make room.", kMaxInputLayouts);
118
119 InputLayoutMap::iterator leastRecentlyUsed = mInputLayoutMap.begin();
120 for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++)
121 {
122 if (i->second.lastUsedTime < leastRecentlyUsed->second.lastUsedTime)
123 {
124 leastRecentlyUsed = i;
125 }
126 }
127 leastRecentlyUsed->second.inputLayout->Release();
128 mInputLayoutMap.erase(leastRecentlyUsed);
129 }
130
131 InputLayoutCounterPair inputCounterPair;
132 inputCounterPair.inputLayout = inputLayout;
133 inputCounterPair.lastUsedTime = mCounter++;
134
135 mInputLayoutMap.insert(std::make_pair(ilKey, inputCounterPair));
136 }
137
138 mDeviceContext->IASetInputLayout(inputLayout);
139 mDeviceContext->IASetVertexBuffers(0, gl::MAX_VERTEX_ATTRIBS, vertexBuffers, vertexStrides, vertexOffsets);
140
141 return GL_NO_ERROR;
142}
143
144std::size_t InputLayoutCache::hashInputLayout(const InputLayoutKey &inputLayout)
145{
146 static const unsigned int seed = 0xDEADBEEF;
147
148 std::size_t hash = 0;
149 MurmurHash3_x86_32(&inputLayout, sizeof(InputLayoutKey), seed, &hash);
150 return hash;
151}
152
153bool InputLayoutCache::compareInputLayouts(const InputLayoutKey &a, const InputLayoutKey &b)
154{
155 return memcmp(&a, &b, sizeof(InputLayoutKey)) == 0;
156}
157
158}