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