blob: 6bef6120f78b2a98ca10be1e9fd3fac1454b0c9a [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{
54 if (!mDevice || !mDeviceContext)
55 {
56 ERR("InputLayoutCache is not initialized.");
57 return GL_INVALID_OPERATION;
58 }
59
60 InputLayoutKey ilKey = { 0 };
61
62 ID3D11Buffer *vertexBuffers[gl::MAX_VERTEX_ATTRIBS] = { NULL };
63 UINT vertexStrides[gl::MAX_VERTEX_ATTRIBS] = { 0 };
64 UINT vertexOffsets[gl::MAX_VERTEX_ATTRIBS] = { 0 };
65
66 static const char* semanticName = "TEXCOORD";
67
68 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
69 {
70 if (attributes[i].active)
71 {
72 VertexBuffer11 *vertexBuffer = VertexBuffer11::makeVertexBuffer11(attributes[i].vertexBuffer);
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +000073 BufferStorage11 *bufferStorage = attributes[i].storage ? BufferStorage11::makeBufferStorage11(attributes[i].storage) : NULL;
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000074
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +000075 D3D11_INPUT_CLASSIFICATION inputClass = attributes[i].divisor > 0 ? D3D11_INPUT_PER_INSTANCE_DATA : D3D11_INPUT_PER_VERTEX_DATA;
76
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000077 ilKey.elements[ilKey.elementCount].SemanticName = semanticName;
78 ilKey.elements[ilKey.elementCount].SemanticIndex = programBinary->getSemanticIndex(i);
79 ilKey.elements[ilKey.elementCount].Format = attributes[i].attribute->mArrayEnabled ? vertexBuffer->getDXGIFormat(*attributes[i].attribute) : DXGI_FORMAT_R32G32B32A32_FLOAT;
80 ilKey.elements[ilKey.elementCount].InputSlot = i;
81 ilKey.elements[ilKey.elementCount].AlignedByteOffset = 0;
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +000082 ilKey.elements[ilKey.elementCount].InputSlotClass = inputClass;
83 ilKey.elements[ilKey.elementCount].InstanceDataStepRate = attributes[i].divisor;
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000084 ilKey.elementCount++;
85
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +000086 vertexBuffers[i] = bufferStorage ? bufferStorage->getBuffer() : vertexBuffer->getBuffer();
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000087 vertexStrides[i] = attributes[i].stride;
88 vertexOffsets[i] = attributes[i].offset;
89 }
90 }
91
92 ID3D11InputLayout *inputLayout = NULL;
93
94 InputLayoutMap::iterator i = mInputLayoutMap.find(ilKey);
95 if (i != mInputLayoutMap.end())
96 {
97 inputLayout = i->second.inputLayout;
98 i->second.lastUsedTime = mCounter++;
99 }
100 else
101 {
102 ShaderExecutable11 *shader = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
103
104 HRESULT result = mDevice->CreateInputLayout(ilKey.elements, ilKey.elementCount, shader->getFunction(), shader->getLength(), &inputLayout);
105 if (FAILED(result))
106 {
107 ERR("Failed to crate input layout, result: 0x%08x", result);
108 return GL_INVALID_OPERATION;
109 }
110
111 if (mInputLayoutMap.size() >= kMaxInputLayouts)
112 {
113 TRACE("Overflowed the limit of %u input layouts, removing the least recently used "
114 "to make room.", kMaxInputLayouts);
115
116 InputLayoutMap::iterator leastRecentlyUsed = mInputLayoutMap.begin();
117 for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++)
118 {
119 if (i->second.lastUsedTime < leastRecentlyUsed->second.lastUsedTime)
120 {
121 leastRecentlyUsed = i;
122 }
123 }
124 leastRecentlyUsed->second.inputLayout->Release();
125 mInputLayoutMap.erase(leastRecentlyUsed);
126 }
127
128 InputLayoutCounterPair inputCounterPair;
129 inputCounterPair.inputLayout = inputLayout;
130 inputCounterPair.lastUsedTime = mCounter++;
131
132 mInputLayoutMap.insert(std::make_pair(ilKey, inputCounterPair));
133 }
134
135 mDeviceContext->IASetInputLayout(inputLayout);
136 mDeviceContext->IASetVertexBuffers(0, gl::MAX_VERTEX_ATTRIBS, vertexBuffers, vertexStrides, vertexOffsets);
137
138 return GL_NO_ERROR;
139}
140
141std::size_t InputLayoutCache::hashInputLayout(const InputLayoutKey &inputLayout)
142{
143 static const unsigned int seed = 0xDEADBEEF;
144
145 std::size_t hash = 0;
146 MurmurHash3_x86_32(&inputLayout, sizeof(InputLayoutKey), seed, &hash);
147 return hash;
148}
149
150bool InputLayoutCache::compareInputLayouts(const InputLayoutKey &a, const InputLayoutKey &b)
151{
152 return memcmp(&a, &b, sizeof(InputLayoutKey)) == 0;
153}
154
155}