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