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