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