daniel@transgaming.com | 70a219b | 2012-11-28 21:00:08 +0000 | [diff] [blame^] | 1 | // |
| 2 | // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // VertexDeclarationCache.cpp: Implements a helper class to construct and cache vertex declarations. |
| 8 | |
| 9 | #include "libGLESv2/ProgramBinary.h" |
| 10 | #include "libGLESv2/VertexDataManager.h" |
| 11 | #include "libGLESv2/renderer/VertexDeclarationCache.h" |
| 12 | |
| 13 | namespace rx |
| 14 | { |
| 15 | |
| 16 | VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0) |
| 17 | { |
| 18 | for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) |
| 19 | { |
| 20 | mVertexDeclCache[i].vertexDeclaration = NULL; |
| 21 | mVertexDeclCache[i].lruCount = 0; |
| 22 | } |
| 23 | |
| 24 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 25 | { |
| 26 | mAppliedVBs[i].serial = 0; |
| 27 | } |
| 28 | |
| 29 | mLastSetVDecl = NULL; |
| 30 | mInstancingEnabled = true; |
| 31 | } |
| 32 | |
| 33 | VertexDeclarationCache::~VertexDeclarationCache() |
| 34 | { |
| 35 | for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) |
| 36 | { |
| 37 | if (mVertexDeclCache[i].vertexDeclaration) |
| 38 | { |
| 39 | mVertexDeclCache[i].vertexDeclaration->Release(); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, gl::TranslatedAttribute attributes[], gl::ProgramBinary *programBinary, GLsizei instances, GLsizei *repeatDraw) |
| 45 | { |
| 46 | *repeatDraw = 1; |
| 47 | |
| 48 | int indexedAttribute = gl::MAX_VERTEX_ATTRIBS; |
| 49 | int instancedAttribute = gl::MAX_VERTEX_ATTRIBS; |
| 50 | |
| 51 | if (instances > 0) |
| 52 | { |
| 53 | // Find an indexed attribute to be mapped to D3D stream 0 |
| 54 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 55 | { |
| 56 | if (attributes[i].active) |
| 57 | { |
| 58 | if (indexedAttribute == gl::MAX_VERTEX_ATTRIBS) |
| 59 | { |
| 60 | if (attributes[i].divisor == 0) |
| 61 | { |
| 62 | indexedAttribute = i; |
| 63 | } |
| 64 | } |
| 65 | else if (instancedAttribute == gl::MAX_VERTEX_ATTRIBS) |
| 66 | { |
| 67 | if (attributes[i].divisor != 0) |
| 68 | { |
| 69 | instancedAttribute = i; |
| 70 | } |
| 71 | } |
| 72 | else break; // Found both an indexed and instanced attribute |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (indexedAttribute == gl::MAX_VERTEX_ATTRIBS) |
| 77 | { |
| 78 | return GL_INVALID_OPERATION; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | D3DVERTEXELEMENT9 elements[gl::MAX_VERTEX_ATTRIBS + 1]; |
| 83 | D3DVERTEXELEMENT9 *element = &elements[0]; |
| 84 | |
| 85 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 86 | { |
| 87 | if (attributes[i].active) |
| 88 | { |
| 89 | int stream = i; |
| 90 | |
| 91 | if (instances > 0) |
| 92 | { |
| 93 | // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced. |
| 94 | if (instancedAttribute == gl::MAX_VERTEX_ATTRIBS) |
| 95 | { |
| 96 | *repeatDraw = instances; |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | if (i == indexedAttribute) |
| 101 | { |
| 102 | stream = 0; |
| 103 | } |
| 104 | else if (i == 0) |
| 105 | { |
| 106 | stream = indexedAttribute; |
| 107 | } |
| 108 | |
| 109 | UINT frequency = 1; |
| 110 | |
| 111 | if (attributes[i].divisor == 0) |
| 112 | { |
| 113 | frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances; |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor; |
| 118 | } |
| 119 | |
| 120 | device->SetStreamSourceFreq(stream, frequency); |
| 121 | mInstancingEnabled = true; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (mAppliedVBs[stream].serial != attributes[i].serial || |
| 126 | mAppliedVBs[stream].stride != attributes[i].stride || |
| 127 | mAppliedVBs[stream].offset != attributes[i].offset) |
| 128 | { |
| 129 | device->SetStreamSource(stream, attributes[i].vertexBuffer, 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; |
| 133 | } |
| 134 | |
| 135 | element->Stream = stream; |
| 136 | element->Offset = 0; |
| 137 | element->Type = attributes[i].type; |
| 138 | element->Method = D3DDECLMETHOD_DEFAULT; |
| 139 | element->Usage = D3DDECLUSAGE_TEXCOORD; |
| 140 | element->UsageIndex = programBinary->getSemanticIndex(i); |
| 141 | element++; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (instances == 0 || instancedAttribute == gl::MAX_VERTEX_ATTRIBS) |
| 146 | { |
| 147 | if (mInstancingEnabled) |
| 148 | { |
| 149 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 150 | { |
| 151 | device->SetStreamSourceFreq(i, 1); |
| 152 | } |
| 153 | |
| 154 | mInstancingEnabled = false; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static const D3DVERTEXELEMENT9 end = D3DDECL_END(); |
| 159 | *(element++) = end; |
| 160 | |
| 161 | for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) |
| 162 | { |
| 163 | VertexDeclCacheEntry *entry = &mVertexDeclCache[i]; |
| 164 | if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration) |
| 165 | { |
| 166 | entry->lruCount = ++mMaxLru; |
| 167 | if(entry->vertexDeclaration != mLastSetVDecl) |
| 168 | { |
| 169 | device->SetVertexDeclaration(entry->vertexDeclaration); |
| 170 | mLastSetVDecl = entry->vertexDeclaration; |
| 171 | } |
| 172 | |
| 173 | return GL_NO_ERROR; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | VertexDeclCacheEntry *lastCache = mVertexDeclCache; |
| 178 | |
| 179 | for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) |
| 180 | { |
| 181 | if (mVertexDeclCache[i].lruCount < lastCache->lruCount) |
| 182 | { |
| 183 | lastCache = &mVertexDeclCache[i]; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | if (lastCache->vertexDeclaration != NULL) |
| 188 | { |
| 189 | lastCache->vertexDeclaration->Release(); |
| 190 | lastCache->vertexDeclaration = NULL; |
| 191 | // mLastSetVDecl is set to the replacement, so we don't have to worry |
| 192 | // about it. |
| 193 | } |
| 194 | |
| 195 | memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)); |
| 196 | device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration); |
| 197 | device->SetVertexDeclaration(lastCache->vertexDeclaration); |
| 198 | mLastSetVDecl = lastCache->vertexDeclaration; |
| 199 | lastCache->lruCount = ++mMaxLru; |
| 200 | |
| 201 | return GL_NO_ERROR; |
| 202 | } |
| 203 | |
| 204 | void VertexDeclarationCache::markStateDirty() |
| 205 | { |
| 206 | for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) |
| 207 | { |
| 208 | mAppliedVBs[i].serial = 0; |
| 209 | } |
| 210 | |
| 211 | mLastSetVDecl = NULL; |
| 212 | mInstancingEnabled = true; // Forces it to be disabled when not used |
| 213 | } |
| 214 | |
| 215 | } |