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: // VertexDeclarationCache.cpp: Implements a helper class to construct and cache vertex declarations. michael@0: michael@0: #include "libGLESv2/ProgramBinary.h" michael@0: #include "libGLESv2/Context.h" michael@0: #include "libGLESv2/renderer/VertexBuffer9.h" michael@0: #include "libGLESv2/renderer/VertexDeclarationCache.h" michael@0: michael@0: namespace rx michael@0: { michael@0: michael@0: VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0) michael@0: { michael@0: for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) michael@0: { michael@0: mVertexDeclCache[i].vertexDeclaration = NULL; michael@0: mVertexDeclCache[i].lruCount = 0; michael@0: } michael@0: michael@0: for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: mAppliedVBs[i].serial = 0; michael@0: } michael@0: michael@0: mLastSetVDecl = NULL; michael@0: mInstancingEnabled = true; michael@0: } michael@0: michael@0: VertexDeclarationCache::~VertexDeclarationCache() michael@0: { michael@0: for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) michael@0: { michael@0: if (mVertexDeclCache[i].vertexDeclaration) michael@0: { michael@0: mVertexDeclCache[i].vertexDeclaration->Release(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], gl::ProgramBinary *programBinary, GLsizei instances, GLsizei *repeatDraw) michael@0: { michael@0: *repeatDraw = 1; michael@0: michael@0: int indexedAttribute = gl::MAX_VERTEX_ATTRIBS; michael@0: int instancedAttribute = gl::MAX_VERTEX_ATTRIBS; michael@0: michael@0: if (instances > 0) michael@0: { michael@0: // Find an indexed attribute to be mapped to D3D stream 0 michael@0: for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: if (attributes[i].active) michael@0: { michael@0: if (indexedAttribute == gl::MAX_VERTEX_ATTRIBS && attributes[i].divisor == 0) michael@0: { michael@0: indexedAttribute = i; michael@0: } michael@0: else if (instancedAttribute == gl::MAX_VERTEX_ATTRIBS && attributes[i].divisor != 0) michael@0: { michael@0: instancedAttribute = i; michael@0: } michael@0: if (indexedAttribute != gl::MAX_VERTEX_ATTRIBS && instancedAttribute != gl::MAX_VERTEX_ATTRIBS) michael@0: break; // Found both an indexed and instanced attribute michael@0: } michael@0: } michael@0: michael@0: if (indexedAttribute == gl::MAX_VERTEX_ATTRIBS) michael@0: { michael@0: return GL_INVALID_OPERATION; michael@0: } michael@0: } michael@0: michael@0: D3DVERTEXELEMENT9 elements[gl::MAX_VERTEX_ATTRIBS + 1]; michael@0: D3DVERTEXELEMENT9 *element = &elements[0]; michael@0: michael@0: for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: if (attributes[i].active) michael@0: { michael@0: // Directly binding the storage buffer is not supported for d3d9 michael@0: ASSERT(attributes[i].storage == NULL); michael@0: michael@0: int stream = i; michael@0: michael@0: if (instances > 0) michael@0: { michael@0: // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced. michael@0: if (instancedAttribute == gl::MAX_VERTEX_ATTRIBS) michael@0: { michael@0: *repeatDraw = instances; michael@0: } michael@0: else michael@0: { michael@0: if (i == indexedAttribute) michael@0: { michael@0: stream = 0; michael@0: } michael@0: else if (i == 0) michael@0: { michael@0: stream = indexedAttribute; michael@0: } michael@0: michael@0: UINT frequency = 1; michael@0: michael@0: if (attributes[i].divisor == 0) michael@0: { michael@0: frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances; michael@0: } michael@0: else michael@0: { michael@0: frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor; michael@0: } michael@0: michael@0: device->SetStreamSourceFreq(stream, frequency); michael@0: mInstancingEnabled = true; michael@0: } michael@0: } michael@0: michael@0: VertexBuffer9 *vertexBuffer = VertexBuffer9::makeVertexBuffer9(attributes[i].vertexBuffer); michael@0: michael@0: if (mAppliedVBs[stream].serial != attributes[i].serial || michael@0: mAppliedVBs[stream].stride != attributes[i].stride || michael@0: mAppliedVBs[stream].offset != attributes[i].offset) michael@0: { michael@0: device->SetStreamSource(stream, vertexBuffer->getBuffer(), attributes[i].offset, attributes[i].stride); michael@0: mAppliedVBs[stream].serial = attributes[i].serial; michael@0: mAppliedVBs[stream].stride = attributes[i].stride; michael@0: mAppliedVBs[stream].offset = attributes[i].offset; michael@0: } michael@0: michael@0: element->Stream = stream; michael@0: element->Offset = 0; michael@0: element->Type = attributes[i].attribute->mArrayEnabled ? vertexBuffer->getDeclType(*attributes[i].attribute) : D3DDECLTYPE_FLOAT4; michael@0: element->Method = D3DDECLMETHOD_DEFAULT; michael@0: element->Usage = D3DDECLUSAGE_TEXCOORD; michael@0: element->UsageIndex = programBinary->getSemanticIndex(i); michael@0: element++; michael@0: } michael@0: } michael@0: michael@0: if (instances == 0 || instancedAttribute == gl::MAX_VERTEX_ATTRIBS) michael@0: { michael@0: if (mInstancingEnabled) michael@0: { michael@0: for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: device->SetStreamSourceFreq(i, 1); michael@0: } michael@0: michael@0: mInstancingEnabled = false; michael@0: } michael@0: } michael@0: michael@0: static const D3DVERTEXELEMENT9 end = D3DDECL_END(); michael@0: *(element++) = end; michael@0: michael@0: for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) michael@0: { michael@0: VertexDeclCacheEntry *entry = &mVertexDeclCache[i]; michael@0: if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration) michael@0: { michael@0: entry->lruCount = ++mMaxLru; michael@0: if(entry->vertexDeclaration != mLastSetVDecl) michael@0: { michael@0: device->SetVertexDeclaration(entry->vertexDeclaration); michael@0: mLastSetVDecl = entry->vertexDeclaration; michael@0: } michael@0: michael@0: return GL_NO_ERROR; michael@0: } michael@0: } michael@0: michael@0: VertexDeclCacheEntry *lastCache = mVertexDeclCache; michael@0: michael@0: for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) michael@0: { michael@0: if (mVertexDeclCache[i].lruCount < lastCache->lruCount) michael@0: { michael@0: lastCache = &mVertexDeclCache[i]; michael@0: } michael@0: } michael@0: michael@0: if (lastCache->vertexDeclaration != NULL) michael@0: { michael@0: lastCache->vertexDeclaration->Release(); michael@0: lastCache->vertexDeclaration = NULL; michael@0: // mLastSetVDecl is set to the replacement, so we don't have to worry michael@0: // about it. michael@0: } michael@0: michael@0: memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)); michael@0: device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration); michael@0: device->SetVertexDeclaration(lastCache->vertexDeclaration); michael@0: mLastSetVDecl = lastCache->vertexDeclaration; michael@0: lastCache->lruCount = ++mMaxLru; michael@0: michael@0: return GL_NO_ERROR; michael@0: } michael@0: michael@0: void VertexDeclarationCache::markStateDirty() michael@0: { michael@0: for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) michael@0: { michael@0: mAppliedVBs[i].serial = 0; michael@0: } michael@0: michael@0: mLastSetVDecl = NULL; michael@0: mInstancingEnabled = true; // Forces it to be disabled when not used michael@0: } michael@0: michael@0: }