blob: 965f2b9ced2c5d7c99d5c6470bb7b0c31c46e510 [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
shannon.woods@transgaming.com0a71ecf2013-02-28 23:15:10 +000080 // Record the type of the associated vertex shader vector in our key
81 // This will prevent mismatched vertex shaders from using the same input layout
82 GLint attributeSize;
83 programBinary->getActiveAttribute(ilKey.elementCount, 0, NULL, &attributeSize, &ilKey.glslElementType[ilKey.elementCount], NULL);
84
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000085 ilKey.elements[ilKey.elementCount].SemanticName = semanticName;
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +000086 ilKey.elements[ilKey.elementCount].SemanticIndex = sortedSemanticIndices[i];
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000087 ilKey.elements[ilKey.elementCount].Format = attributes[i].attribute->mArrayEnabled ? vertexBuffer->getDXGIFormat(*attributes[i].attribute) : DXGI_FORMAT_R32G32B32A32_FLOAT;
88 ilKey.elements[ilKey.elementCount].InputSlot = i;
89 ilKey.elements[ilKey.elementCount].AlignedByteOffset = 0;
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +000090 ilKey.elements[ilKey.elementCount].InputSlotClass = inputClass;
91 ilKey.elements[ilKey.elementCount].InstanceDataStepRate = attributes[i].divisor;
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000092 ilKey.elementCount++;
93
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +000094 vertexBuffers[i] = bufferStorage ? bufferStorage->getBuffer() : vertexBuffer->getBuffer();
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000095 vertexStrides[i] = attributes[i].stride;
96 vertexOffsets[i] = attributes[i].offset;
97 }
98 }
99
100 ID3D11InputLayout *inputLayout = NULL;
101
102 InputLayoutMap::iterator i = mInputLayoutMap.find(ilKey);
103 if (i != mInputLayoutMap.end())
104 {
105 inputLayout = i->second.inputLayout;
106 i->second.lastUsedTime = mCounter++;
107 }
108 else
109 {
110 ShaderExecutable11 *shader = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
111
112 HRESULT result = mDevice->CreateInputLayout(ilKey.elements, ilKey.elementCount, shader->getFunction(), shader->getLength(), &inputLayout);
113 if (FAILED(result))
114 {
115 ERR("Failed to crate input layout, result: 0x%08x", result);
116 return GL_INVALID_OPERATION;
117 }
118
119 if (mInputLayoutMap.size() >= kMaxInputLayouts)
120 {
121 TRACE("Overflowed the limit of %u input layouts, removing the least recently used "
122 "to make room.", kMaxInputLayouts);
123
124 InputLayoutMap::iterator leastRecentlyUsed = mInputLayoutMap.begin();
125 for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++)
126 {
127 if (i->second.lastUsedTime < leastRecentlyUsed->second.lastUsedTime)
128 {
129 leastRecentlyUsed = i;
130 }
131 }
132 leastRecentlyUsed->second.inputLayout->Release();
133 mInputLayoutMap.erase(leastRecentlyUsed);
134 }
135
136 InputLayoutCounterPair inputCounterPair;
137 inputCounterPair.inputLayout = inputLayout;
138 inputCounterPair.lastUsedTime = mCounter++;
139
140 mInputLayoutMap.insert(std::make_pair(ilKey, inputCounterPair));
141 }
142
143 mDeviceContext->IASetInputLayout(inputLayout);
144 mDeviceContext->IASetVertexBuffers(0, gl::MAX_VERTEX_ATTRIBS, vertexBuffers, vertexStrides, vertexOffsets);
145
146 return GL_NO_ERROR;
147}
148
149std::size_t InputLayoutCache::hashInputLayout(const InputLayoutKey &inputLayout)
150{
151 static const unsigned int seed = 0xDEADBEEF;
152
153 std::size_t hash = 0;
154 MurmurHash3_x86_32(&inputLayout, sizeof(InputLayoutKey), seed, &hash);
155 return hash;
156}
157
158bool InputLayoutCache::compareInputLayouts(const InputLayoutKey &a, const InputLayoutKey &b)
159{
160 return memcmp(&a, &b, sizeof(InputLayoutKey)) == 0;
161}
162
163}