blob: 8bb8b7ee80f4649eba58fcee37b2ca66c6cdc3f6 [file] [log] [blame]
shannon.woods@transgaming.combdf2d802013-02-28 23:16:20 +00001#include "precompiled.h"
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +00002//
3// Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// InputLayoutCache.cpp: Defines InputLayoutCache, a class that builds and caches
9// D3D11 input layouts.
10
Geoff Langd47e0fc2013-08-29 11:40:43 -040011#include "libGLESv2/renderer/d3d11/InputLayoutCache.h"
12#include "libGLESv2/renderer/d3d11/VertexBuffer11.h"
13#include "libGLESv2/renderer/d3d11/BufferStorage11.h"
14#include "libGLESv2/renderer/d3d11/ShaderExecutable11.h"
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000015#include "libGLESv2/ProgramBinary.h"
Jamie Madill87939712013-07-02 11:57:01 -040016#include "libGLESv2/VertexAttribute.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000017#include "libGLESv2/renderer/VertexDataManager.h"
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000018
19#include "third_party/murmurhash/MurmurHash3.h"
20
21namespace rx
22{
23
24const unsigned int InputLayoutCache::kMaxInputLayouts = 1024;
25
26InputLayoutCache::InputLayoutCache() : mInputLayoutMap(kMaxInputLayouts, hashInputLayout, compareInputLayouts)
27{
28 mCounter = 0;
29 mDevice = NULL;
30 mDeviceContext = NULL;
Geoff Lang1f53cab2013-07-22 10:37:22 -040031 mCurrentIL = NULL;
32 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
33 {
34 mCurrentBuffers[i] = -1;
35 mCurrentVertexStrides[i] = -1;
36 mCurrentVertexOffsets[i] = -1;
37 }
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000038}
39
40InputLayoutCache::~InputLayoutCache()
41{
42 clear();
43}
44
45void InputLayoutCache::initialize(ID3D11Device *device, ID3D11DeviceContext *context)
46{
47 clear();
48 mDevice = device;
49 mDeviceContext = context;
50}
51
52void InputLayoutCache::clear()
53{
54 for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++)
55 {
Geoff Langea228632013-07-30 15:17:12 -040056 SafeRelease(i->second.inputLayout);
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000057 }
58 mInputLayoutMap.clear();
Geoff Lang1f53cab2013-07-22 10:37:22 -040059 markDirty();
60}
61
62void InputLayoutCache::markDirty()
63{
64 mCurrentIL = NULL;
65 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
66 {
67 mCurrentBuffers[i] = -1;
68 mCurrentVertexStrides[i] = -1;
69 mCurrentVertexOffsets[i] = -1;
70 }
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000071}
72
73GLenum InputLayoutCache::applyVertexBuffers(TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
74 gl::ProgramBinary *programBinary)
75{
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +000076 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS];
77 programBinary->sortAttributesByLayout(attributes, sortedSemanticIndices);
78
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000079 if (!mDevice || !mDeviceContext)
80 {
81 ERR("InputLayoutCache is not initialized.");
82 return GL_INVALID_OPERATION;
83 }
84
85 InputLayoutKey ilKey = { 0 };
86
87 ID3D11Buffer *vertexBuffers[gl::MAX_VERTEX_ATTRIBS] = { NULL };
Geoff Lang1f53cab2013-07-22 10:37:22 -040088 unsigned int vertexBufferSerials[gl::MAX_VERTEX_ATTRIBS] = { 0 };
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000089 UINT vertexStrides[gl::MAX_VERTEX_ATTRIBS] = { 0 };
90 UINT vertexOffsets[gl::MAX_VERTEX_ATTRIBS] = { 0 };
91
92 static const char* semanticName = "TEXCOORD";
93
94 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
95 {
96 if (attributes[i].active)
97 {
98 VertexBuffer11 *vertexBuffer = VertexBuffer11::makeVertexBuffer11(attributes[i].vertexBuffer);
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +000099 BufferStorage11 *bufferStorage = attributes[i].storage ? BufferStorage11::makeBufferStorage11(attributes[i].storage) : NULL;
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +0000100
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +0000101 D3D11_INPUT_CLASSIFICATION inputClass = attributes[i].divisor > 0 ? D3D11_INPUT_PER_INSTANCE_DATA : D3D11_INPUT_PER_VERTEX_DATA;
Jamie Madilla857c362013-07-02 11:57:02 -0400102 DXGI_FORMAT dxgiFormat = attributes[i].attribute->mArrayEnabled ?
103 VertexBuffer11::getAttributeDXGIFormat(*attributes[i].attribute) :
104 VertexBuffer11::getCurrentValueDXGIFormat(attributes[i].currentValueType);
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +0000105
shannon.woods@transgaming.com0a71ecf2013-02-28 23:15:10 +0000106 // Record the type of the associated vertex shader vector in our key
107 // This will prevent mismatched vertex shaders from using the same input layout
108 GLint attributeSize;
Al Patrick978911c2013-08-15 12:43:29 -0700109 programBinary->getActiveAttribute(ilKey.elementCount, 0, NULL, &attributeSize, &ilKey.elements[ilKey.elementCount].glslElementType, NULL);
shannon.woods@transgaming.com0a71ecf2013-02-28 23:15:10 +0000110
Al Patrick978911c2013-08-15 12:43:29 -0700111 ilKey.elements[ilKey.elementCount].desc.SemanticName = semanticName;
112 ilKey.elements[ilKey.elementCount].desc.SemanticIndex = sortedSemanticIndices[i];
113 ilKey.elements[ilKey.elementCount].desc.Format = dxgiFormat;
114 ilKey.elements[ilKey.elementCount].desc.InputSlot = i;
115 ilKey.elements[ilKey.elementCount].desc.AlignedByteOffset = 0;
116 ilKey.elements[ilKey.elementCount].desc.InputSlotClass = inputClass;
117 ilKey.elements[ilKey.elementCount].desc.InstanceDataStepRate = attributes[i].divisor;
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +0000118 ilKey.elementCount++;
119
shannonwoods@chromium.org675526e2013-05-30 00:04:49 +0000120 vertexBuffers[i] = bufferStorage ? bufferStorage->getBuffer(GL_ARRAY_BUFFER) : vertexBuffer->getBuffer();
Geoff Lang1f53cab2013-07-22 10:37:22 -0400121 vertexBufferSerials[i] = bufferStorage ? bufferStorage->getSerial() : vertexBuffer->getSerial();
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +0000122 vertexStrides[i] = attributes[i].stride;
123 vertexOffsets[i] = attributes[i].offset;
124 }
125 }
126
127 ID3D11InputLayout *inputLayout = NULL;
128
129 InputLayoutMap::iterator i = mInputLayoutMap.find(ilKey);
130 if (i != mInputLayoutMap.end())
131 {
132 inputLayout = i->second.inputLayout;
133 i->second.lastUsedTime = mCounter++;
134 }
135 else
136 {
137 ShaderExecutable11 *shader = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable());
138
Al Patrick978911c2013-08-15 12:43:29 -0700139 D3D11_INPUT_ELEMENT_DESC descs[gl::MAX_VERTEX_ATTRIBS];
140 for (unsigned int j = 0; j < ilKey.elementCount; ++j)
141 {
142 descs[j] = ilKey.elements[j].desc;
143 }
144
145 HRESULT result = mDevice->CreateInputLayout(descs, ilKey.elementCount, shader->getFunction(), shader->getLength(), &inputLayout);
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +0000146 if (FAILED(result))
147 {
148 ERR("Failed to crate input layout, result: 0x%08x", result);
149 return GL_INVALID_OPERATION;
150 }
151
152 if (mInputLayoutMap.size() >= kMaxInputLayouts)
153 {
154 TRACE("Overflowed the limit of %u input layouts, removing the least recently used "
155 "to make room.", kMaxInputLayouts);
156
157 InputLayoutMap::iterator leastRecentlyUsed = mInputLayoutMap.begin();
158 for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++)
159 {
160 if (i->second.lastUsedTime < leastRecentlyUsed->second.lastUsedTime)
161 {
162 leastRecentlyUsed = i;
163 }
164 }
Geoff Langea228632013-07-30 15:17:12 -0400165 SafeRelease(leastRecentlyUsed->second.inputLayout);
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +0000166 mInputLayoutMap.erase(leastRecentlyUsed);
167 }
168
169 InputLayoutCounterPair inputCounterPair;
170 inputCounterPair.inputLayout = inputLayout;
171 inputCounterPair.lastUsedTime = mCounter++;
172
173 mInputLayoutMap.insert(std::make_pair(ilKey, inputCounterPair));
174 }
175
Geoff Lang1f53cab2013-07-22 10:37:22 -0400176 if (inputLayout != mCurrentIL)
177 {
178 mDeviceContext->IASetInputLayout(inputLayout);
179 mCurrentIL = inputLayout;
180 }
181
182 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
183 {
184 if (vertexBufferSerials[i] != mCurrentBuffers[i] || vertexStrides[i] != mCurrentVertexStrides[i] ||
185 vertexOffsets[i] != mCurrentVertexOffsets[i])
186 {
187 mDeviceContext->IASetVertexBuffers(i, 1, &vertexBuffers[i], &vertexStrides[i], &vertexOffsets[i]);
188 mCurrentBuffers[i] = vertexBufferSerials[i];
189 mCurrentVertexStrides[i] = vertexStrides[i];
190 mCurrentVertexOffsets[i] = vertexOffsets[i];
191 }
192 }
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +0000193
194 return GL_NO_ERROR;
195}
196
197std::size_t InputLayoutCache::hashInputLayout(const InputLayoutKey &inputLayout)
198{
199 static const unsigned int seed = 0xDEADBEEF;
200
201 std::size_t hash = 0;
Al Patrick978911c2013-08-15 12:43:29 -0700202 MurmurHash3_x86_32(inputLayout.begin(), inputLayout.end() - inputLayout.begin(), seed, &hash);
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +0000203 return hash;
204}
205
206bool InputLayoutCache::compareInputLayouts(const InputLayoutKey &a, const InputLayoutKey &b)
207{
Al Patrick978911c2013-08-15 12:43:29 -0700208 if (a.elementCount != b.elementCount)
209 {
210 return false;
211 }
212
213 return std::equal(a.begin(), a.end(), b.begin());
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +0000214}
215
216}