blob: 24179c0c471fb0428f7ee934280993bbc9e4ce91 [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
11#include "libGLESv2/renderer/InputLayoutCache.h"
12#include "libGLESv2/renderer/VertexBuffer11.h"
shannon.woods@transgaming.comdb1899c2013-02-28 23:08:33 +000013#include "libGLESv2/renderer/BufferStorage11.h"
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +000014#include "libGLESv2/renderer/ShaderExecutable11.h"
15#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;
109 programBinary->getActiveAttribute(ilKey.elementCount, 0, NULL, &attributeSize, &ilKey.glslElementType[ilKey.elementCount], NULL);
110
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +0000111 ilKey.elements[ilKey.elementCount].SemanticName = semanticName;
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +0000112 ilKey.elements[ilKey.elementCount].SemanticIndex = sortedSemanticIndices[i];
Jamie Madilla857c362013-07-02 11:57:02 -0400113 ilKey.elements[ilKey.elementCount].Format = dxgiFormat;
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +0000114 ilKey.elements[ilKey.elementCount].InputSlot = i;
115 ilKey.elements[ilKey.elementCount].AlignedByteOffset = 0;
shannon.woods@transgaming.com00032cb2013-01-25 21:56:30 +0000116 ilKey.elements[ilKey.elementCount].InputSlotClass = inputClass;
117 ilKey.elements[ilKey.elementCount].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
139 HRESULT result = mDevice->CreateInputLayout(ilKey.elements, ilKey.elementCount, shader->getFunction(), shader->getLength(), &inputLayout);
140 if (FAILED(result))
141 {
142 ERR("Failed to crate input layout, result: 0x%08x", result);
143 return GL_INVALID_OPERATION;
144 }
145
146 if (mInputLayoutMap.size() >= kMaxInputLayouts)
147 {
148 TRACE("Overflowed the limit of %u input layouts, removing the least recently used "
149 "to make room.", kMaxInputLayouts);
150
151 InputLayoutMap::iterator leastRecentlyUsed = mInputLayoutMap.begin();
152 for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++)
153 {
154 if (i->second.lastUsedTime < leastRecentlyUsed->second.lastUsedTime)
155 {
156 leastRecentlyUsed = i;
157 }
158 }
Geoff Langea228632013-07-30 15:17:12 -0400159 SafeRelease(leastRecentlyUsed->second.inputLayout);
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +0000160 mInputLayoutMap.erase(leastRecentlyUsed);
161 }
162
163 InputLayoutCounterPair inputCounterPair;
164 inputCounterPair.inputLayout = inputLayout;
165 inputCounterPair.lastUsedTime = mCounter++;
166
167 mInputLayoutMap.insert(std::make_pair(ilKey, inputCounterPair));
168 }
169
Geoff Lang1f53cab2013-07-22 10:37:22 -0400170 if (inputLayout != mCurrentIL)
171 {
172 mDeviceContext->IASetInputLayout(inputLayout);
173 mCurrentIL = inputLayout;
174 }
175
176 for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
177 {
178 if (vertexBufferSerials[i] != mCurrentBuffers[i] || vertexStrides[i] != mCurrentVertexStrides[i] ||
179 vertexOffsets[i] != mCurrentVertexOffsets[i])
180 {
181 mDeviceContext->IASetVertexBuffers(i, 1, &vertexBuffers[i], &vertexStrides[i], &vertexOffsets[i]);
182 mCurrentBuffers[i] = vertexBufferSerials[i];
183 mCurrentVertexStrides[i] = vertexStrides[i];
184 mCurrentVertexOffsets[i] = vertexOffsets[i];
185 }
186 }
daniel@transgaming.comcd9458d2012-12-20 21:10:09 +0000187
188 return GL_NO_ERROR;
189}
190
191std::size_t InputLayoutCache::hashInputLayout(const InputLayoutKey &inputLayout)
192{
193 static const unsigned int seed = 0xDEADBEEF;
194
195 std::size_t hash = 0;
196 MurmurHash3_x86_32(&inputLayout, sizeof(InputLayoutKey), seed, &hash);
197 return hash;
198}
199
200bool InputLayoutCache::compareInputLayouts(const InputLayoutKey &a, const InputLayoutKey &b)
201{
202 return memcmp(&a, &b, sizeof(InputLayoutKey)) == 0;
203}
204
205}