michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // InputLayoutCache.cpp: Defines InputLayoutCache, a class that builds and caches michael@0: // D3D11 input layouts. michael@0: michael@0: #include "libGLESv2/renderer/InputLayoutCache.h" michael@0: #include "libGLESv2/renderer/VertexBuffer11.h" michael@0: #include "libGLESv2/renderer/BufferStorage11.h" michael@0: #include "libGLESv2/renderer/ShaderExecutable11.h" michael@0: #include "libGLESv2/ProgramBinary.h" michael@0: #include "libGLESv2/Context.h" michael@0: #include "libGLESv2/renderer/VertexDataManager.h" michael@0: michael@0: #include "third_party/murmurhash/MurmurHash3.h" michael@0: michael@0: namespace rx michael@0: { michael@0: michael@0: const unsigned int InputLayoutCache::kMaxInputLayouts = 1024; michael@0: michael@0: InputLayoutCache::InputLayoutCache() : mInputLayoutMap(kMaxInputLayouts, hashInputLayout, compareInputLayouts) michael@0: { michael@0: mCounter = 0; michael@0: mDevice = NULL; michael@0: mDeviceContext = NULL; michael@0: mCurrentIL = NULL; michael@0: for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: mCurrentBuffers[i] = -1; michael@0: mCurrentVertexStrides[i] = -1; michael@0: mCurrentVertexOffsets[i] = -1; michael@0: } michael@0: } michael@0: michael@0: InputLayoutCache::~InputLayoutCache() michael@0: { michael@0: clear(); michael@0: } michael@0: michael@0: void InputLayoutCache::initialize(ID3D11Device *device, ID3D11DeviceContext *context) michael@0: { michael@0: clear(); michael@0: mDevice = device; michael@0: mDeviceContext = context; michael@0: } michael@0: michael@0: void InputLayoutCache::clear() michael@0: { michael@0: for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++) michael@0: { michael@0: i->second.inputLayout->Release(); michael@0: } michael@0: mInputLayoutMap.clear(); michael@0: markDirty(); michael@0: } michael@0: michael@0: void InputLayoutCache::markDirty() michael@0: { michael@0: mCurrentIL = NULL; michael@0: for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: mCurrentBuffers[i] = -1; michael@0: mCurrentVertexStrides[i] = -1; michael@0: mCurrentVertexOffsets[i] = -1; michael@0: } michael@0: } michael@0: michael@0: GLenum InputLayoutCache::applyVertexBuffers(TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS], michael@0: gl::ProgramBinary *programBinary) michael@0: { michael@0: int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]; michael@0: programBinary->sortAttributesByLayout(attributes, sortedSemanticIndices); michael@0: michael@0: if (!mDevice || !mDeviceContext) michael@0: { michael@0: ERR("InputLayoutCache is not initialized."); michael@0: return GL_INVALID_OPERATION; michael@0: } michael@0: michael@0: InputLayoutKey ilKey = { 0 }; michael@0: michael@0: ID3D11Buffer *vertexBuffers[gl::MAX_VERTEX_ATTRIBS] = { NULL }; michael@0: unsigned int vertexBufferSerials[gl::MAX_VERTEX_ATTRIBS] = { 0 }; michael@0: UINT vertexStrides[gl::MAX_VERTEX_ATTRIBS] = { 0 }; michael@0: UINT vertexOffsets[gl::MAX_VERTEX_ATTRIBS] = { 0 }; michael@0: michael@0: static const char* semanticName = "TEXCOORD"; michael@0: michael@0: for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: if (attributes[i].active) michael@0: { michael@0: VertexBuffer11 *vertexBuffer = VertexBuffer11::makeVertexBuffer11(attributes[i].vertexBuffer); michael@0: BufferStorage11 *bufferStorage = attributes[i].storage ? BufferStorage11::makeBufferStorage11(attributes[i].storage) : NULL; michael@0: michael@0: D3D11_INPUT_CLASSIFICATION inputClass = attributes[i].divisor > 0 ? D3D11_INPUT_PER_INSTANCE_DATA : D3D11_INPUT_PER_VERTEX_DATA; michael@0: michael@0: // Record the type of the associated vertex shader vector in our key michael@0: // This will prevent mismatched vertex shaders from using the same input layout michael@0: GLint attributeSize; michael@0: programBinary->getActiveAttribute(ilKey.elementCount, 0, NULL, &attributeSize, &ilKey.glslElementType[ilKey.elementCount], NULL); michael@0: michael@0: ilKey.elements[ilKey.elementCount].SemanticName = semanticName; michael@0: ilKey.elements[ilKey.elementCount].SemanticIndex = sortedSemanticIndices[i]; michael@0: ilKey.elements[ilKey.elementCount].Format = attributes[i].attribute->mArrayEnabled ? vertexBuffer->getDXGIFormat(*attributes[i].attribute) : DXGI_FORMAT_R32G32B32A32_FLOAT; michael@0: ilKey.elements[ilKey.elementCount].InputSlot = i; michael@0: ilKey.elements[ilKey.elementCount].AlignedByteOffset = 0; michael@0: ilKey.elements[ilKey.elementCount].InputSlotClass = inputClass; michael@0: ilKey.elements[ilKey.elementCount].InstanceDataStepRate = attributes[i].divisor; michael@0: ilKey.elementCount++; michael@0: michael@0: vertexBuffers[i] = bufferStorage ? bufferStorage->getBuffer() : vertexBuffer->getBuffer(); michael@0: vertexBufferSerials[i] = bufferStorage ? bufferStorage->getSerial() : vertexBuffer->getSerial(); michael@0: vertexStrides[i] = attributes[i].stride; michael@0: vertexOffsets[i] = attributes[i].offset; michael@0: } michael@0: } michael@0: michael@0: ID3D11InputLayout *inputLayout = NULL; michael@0: michael@0: InputLayoutMap::iterator i = mInputLayoutMap.find(ilKey); michael@0: if (i != mInputLayoutMap.end()) michael@0: { michael@0: inputLayout = i->second.inputLayout; michael@0: i->second.lastUsedTime = mCounter++; michael@0: } michael@0: else michael@0: { michael@0: ShaderExecutable11 *shader = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable()); michael@0: michael@0: HRESULT result = mDevice->CreateInputLayout(ilKey.elements, ilKey.elementCount, shader->getFunction(), shader->getLength(), &inputLayout); michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Failed to crate input layout, result: 0x%08x", result); michael@0: return GL_INVALID_OPERATION; michael@0: } michael@0: michael@0: if (mInputLayoutMap.size() >= kMaxInputLayouts) michael@0: { michael@0: TRACE("Overflowed the limit of %u input layouts, removing the least recently used " michael@0: "to make room.", kMaxInputLayouts); michael@0: michael@0: InputLayoutMap::iterator leastRecentlyUsed = mInputLayoutMap.begin(); michael@0: for (InputLayoutMap::iterator i = mInputLayoutMap.begin(); i != mInputLayoutMap.end(); i++) michael@0: { michael@0: if (i->second.lastUsedTime < leastRecentlyUsed->second.lastUsedTime) michael@0: { michael@0: leastRecentlyUsed = i; michael@0: } michael@0: } michael@0: leastRecentlyUsed->second.inputLayout->Release(); michael@0: mInputLayoutMap.erase(leastRecentlyUsed); michael@0: } michael@0: michael@0: InputLayoutCounterPair inputCounterPair; michael@0: inputCounterPair.inputLayout = inputLayout; michael@0: inputCounterPair.lastUsedTime = mCounter++; michael@0: michael@0: mInputLayoutMap.insert(std::make_pair(ilKey, inputCounterPair)); michael@0: } michael@0: michael@0: if (inputLayout != mCurrentIL) michael@0: { michael@0: mDeviceContext->IASetInputLayout(inputLayout); michael@0: mCurrentIL = inputLayout; michael@0: } michael@0: michael@0: for (unsigned int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: if (vertexBufferSerials[i] != mCurrentBuffers[i] || vertexStrides[i] != mCurrentVertexStrides[i] || michael@0: vertexOffsets[i] != mCurrentVertexOffsets[i]) michael@0: { michael@0: mDeviceContext->IASetVertexBuffers(i, 1, &vertexBuffers[i], &vertexStrides[i], &vertexOffsets[i]); michael@0: mCurrentBuffers[i] = vertexBufferSerials[i]; michael@0: mCurrentVertexStrides[i] = vertexStrides[i]; michael@0: mCurrentVertexOffsets[i] = vertexOffsets[i]; michael@0: } michael@0: } michael@0: michael@0: return GL_NO_ERROR; michael@0: } michael@0: michael@0: std::size_t InputLayoutCache::hashInputLayout(const InputLayoutKey &inputLayout) michael@0: { michael@0: static const unsigned int seed = 0xDEADBEEF; michael@0: michael@0: std::size_t hash = 0; michael@0: MurmurHash3_x86_32(&inputLayout, sizeof(InputLayoutKey), seed, &hash); michael@0: return hash; michael@0: } michael@0: michael@0: bool InputLayoutCache::compareInputLayouts(const InputLayoutKey &a, const InputLayoutKey &b) michael@0: { michael@0: return memcmp(&a, &b, sizeof(InputLayoutKey)) == 0; michael@0: } michael@0: michael@0: }