1 #include "precompiled.h"
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.
8 // VertexDeclarationCache.cpp: Implements a helper class to construct and cache vertex declarations.
10 #include "libGLESv2/ProgramBinary.h"
11 #include "libGLESv2/VertexAttribute.h"
12 #include "libGLESv2/renderer/d3d9/VertexBuffer9.h"
13 #include "libGLESv2/renderer/d3d9/VertexDeclarationCache.h"
14 #include "libGLESv2/renderer/d3d9/formatutils9.h"
19 VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
21 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
23 mVertexDeclCache[i].vertexDeclaration = NULL;
24 mVertexDeclCache[i].lruCount = 0;
27 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
29 mAppliedVBs[i].serial = 0;
33 mInstancingEnabled = true;
36 VertexDeclarationCache::~VertexDeclarationCache()
38 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
40 SafeRelease(mVertexDeclCache[i].vertexDeclaration);
44 GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], gl::ProgramBinary *programBinary, GLsizei instances, GLsizei *repeatDraw)
48 int indexedAttribute = gl::MAX_VERTEX_ATTRIBS;
49 int instancedAttribute = gl::MAX_VERTEX_ATTRIBS;
53 // Find an indexed attribute to be mapped to D3D stream 0
54 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
56 if (attributes[i].active)
58 if (indexedAttribute == gl::MAX_VERTEX_ATTRIBS && attributes[i].divisor == 0)
62 else if (instancedAttribute == gl::MAX_VERTEX_ATTRIBS && attributes[i].divisor != 0)
64 instancedAttribute = i;
66 if (indexedAttribute != gl::MAX_VERTEX_ATTRIBS && instancedAttribute != gl::MAX_VERTEX_ATTRIBS)
67 break; // Found both an indexed and instanced attribute
71 if (indexedAttribute == gl::MAX_VERTEX_ATTRIBS)
73 return GL_INVALID_OPERATION;
77 D3DVERTEXELEMENT9 elements[gl::MAX_VERTEX_ATTRIBS + 1];
78 D3DVERTEXELEMENT9 *element = &elements[0];
80 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
82 if (attributes[i].active)
84 // Directly binding the storage buffer is not supported for d3d9
85 ASSERT(attributes[i].storage == NULL);
91 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
92 if (instancedAttribute == gl::MAX_VERTEX_ATTRIBS)
94 *repeatDraw = instances;
98 if (i == indexedAttribute)
104 stream = indexedAttribute;
109 if (attributes[i].divisor == 0)
111 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
115 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
118 device->SetStreamSourceFreq(stream, frequency);
119 mInstancingEnabled = true;
123 VertexBuffer9 *vertexBuffer = VertexBuffer9::makeVertexBuffer9(attributes[i].vertexBuffer);
125 if (mAppliedVBs[stream].serial != attributes[i].serial ||
126 mAppliedVBs[stream].stride != attributes[i].stride ||
127 mAppliedVBs[stream].offset != attributes[i].offset)
129 device->SetStreamSource(stream, vertexBuffer->getBuffer(), attributes[i].offset, attributes[i].stride);
130 mAppliedVBs[stream].serial = attributes[i].serial;
131 mAppliedVBs[stream].stride = attributes[i].stride;
132 mAppliedVBs[stream].offset = attributes[i].offset;
135 gl::VertexFormat vertexFormat(*attributes[i].attribute, GL_FLOAT);
137 element->Stream = stream;
139 element->Type = d3d9::GetNativeVertexFormat(vertexFormat);
140 element->Method = D3DDECLMETHOD_DEFAULT;
141 element->Usage = D3DDECLUSAGE_TEXCOORD;
142 element->UsageIndex = programBinary->getSemanticIndex(i);
147 if (instances == 0 || instancedAttribute == gl::MAX_VERTEX_ATTRIBS)
149 if (mInstancingEnabled)
151 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
153 device->SetStreamSourceFreq(i, 1);
156 mInstancingEnabled = false;
160 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
163 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
165 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
166 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
168 entry->lruCount = ++mMaxLru;
169 if(entry->vertexDeclaration != mLastSetVDecl)
171 device->SetVertexDeclaration(entry->vertexDeclaration);
172 mLastSetVDecl = entry->vertexDeclaration;
179 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
181 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
183 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
185 lastCache = &mVertexDeclCache[i];
189 if (lastCache->vertexDeclaration != NULL)
191 SafeRelease(lastCache->vertexDeclaration);
192 // mLastSetVDecl is set to the replacement, so we don't have to worry
196 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
197 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
198 device->SetVertexDeclaration(lastCache->vertexDeclaration);
199 mLastSetVDecl = lastCache->vertexDeclaration;
200 lastCache->lruCount = ++mMaxLru;
205 void VertexDeclarationCache::markStateDirty()
207 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++)
209 mAppliedVBs[i].serial = 0;
212 mLastSetVDecl = NULL;
213 mInstancingEnabled = true; // Forces it to be disabled when not used