/src/ogre/OgreMain/src/OgreInstanceBatchHW_VTF.cpp
Line | Count | Source |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #include "OgreStableHeaders.h" |
29 | | #include "OgreInstanceBatchHW_VTF.h" |
30 | | #include "OgreHardwarePixelBuffer.h" |
31 | | #include "OgreInstancedEntity.h" |
32 | | |
33 | | namespace Ogre |
34 | | { |
35 | | static const uint16 c_maxTexWidthHW = 4096; |
36 | | static const uint16 c_maxTexHeightHW = 4096; |
37 | | |
38 | | InstanceBatchHW_VTF::InstanceBatchHW_VTF( |
39 | | InstanceManager *creator, MeshPtr &meshReference, |
40 | | const MaterialPtr &material, size_t instancesPerBatch, |
41 | | const Mesh::IndexMap *indexToBoneMap, const String &batchName ) |
42 | 0 | : BaseInstanceBatchVTF( creator, meshReference, material, |
43 | 0 | instancesPerBatch, indexToBoneMap, batchName), |
44 | 0 | mKeepStatic( false ) |
45 | 0 | { |
46 | 0 | } |
47 | | //----------------------------------------------------------------------- |
48 | | InstanceBatchHW_VTF::~InstanceBatchHW_VTF() |
49 | 0 | { |
50 | 0 | } |
51 | | //----------------------------------------------------------------------- |
52 | | void InstanceBatchHW_VTF::setupVertices( const SubMesh* baseSubMesh ) |
53 | 0 | { |
54 | 0 | mRenderOperation.vertexData = OGRE_NEW VertexData(); |
55 | 0 | mRemoveOwnVertexData = true; //Raise flag to remove our own vertex data in the end (not always needed) |
56 | |
|
57 | 0 | VertexData *thisVertexData = mRenderOperation.vertexData; |
58 | 0 | VertexData *baseVertexData = baseSubMesh->vertexData; |
59 | |
|
60 | 0 | thisVertexData->vertexStart = 0; |
61 | 0 | thisVertexData->vertexCount = baseVertexData->vertexCount; |
62 | 0 | mRenderOperation.numberOfInstances = mInstancesPerBatch; |
63 | |
|
64 | 0 | HardwareBufferManager::getSingleton().destroyVertexDeclaration(thisVertexData->vertexDeclaration ); |
65 | 0 | thisVertexData->vertexDeclaration = baseVertexData->vertexDeclaration->clone(); |
66 | | |
67 | | //Reuse all vertex buffers |
68 | 0 | for (auto& v : baseVertexData->vertexBufferBinding->getBindings()) |
69 | 0 | { |
70 | 0 | const unsigned short bufferIdx = v.first; |
71 | 0 | const HardwareVertexBufferSharedPtr vBuf = v.second; |
72 | 0 | thisVertexData->vertexBufferBinding->setBinding( bufferIdx, vBuf ); |
73 | 0 | } |
74 | | |
75 | | //Remove the blend weights & indices |
76 | 0 | HWBoneIdxVec hwBoneIdx; |
77 | 0 | HWBoneWgtVec hwBoneWgt; |
78 | | |
79 | | //Blend weights may not be present because HW_VTF does not require to be skeletally animated |
80 | 0 | const VertexElement *veWeights = baseVertexData->vertexDeclaration->findElementBySemantic( VES_BLEND_WEIGHTS ); |
81 | 0 | if( veWeights ) |
82 | 0 | mWeightCount = forceOneWeight() ? 1 : veWeights->getSize() / sizeof(float); |
83 | 0 | else |
84 | 0 | mWeightCount = 1; |
85 | |
|
86 | 0 | hwBoneIdx.resize( baseVertexData->vertexCount * mWeightCount, 0 ); |
87 | |
|
88 | 0 | if( mMeshReference->hasSkeleton() && mMeshReference->getSkeleton() ) |
89 | 0 | { |
90 | 0 | if(mWeightCount > 1) |
91 | 0 | { |
92 | 0 | hwBoneWgt.resize( baseVertexData->vertexCount * mWeightCount, 0 ); |
93 | 0 | retrieveBoneIdxWithWeights(baseVertexData, hwBoneIdx, hwBoneWgt); |
94 | 0 | } |
95 | 0 | else |
96 | 0 | { |
97 | 0 | retrieveBoneIdx( baseVertexData, hwBoneIdx ); |
98 | 0 | } |
99 | |
|
100 | 0 | const VertexElement* pElement = thisVertexData->vertexDeclaration->findElementBySemantic |
101 | 0 | (VES_BLEND_INDICES); |
102 | 0 | if (pElement) |
103 | 0 | { |
104 | 0 | unsigned short skelDataSource = pElement->getSource(); |
105 | 0 | thisVertexData->vertexDeclaration->removeElement( VES_BLEND_INDICES ); |
106 | 0 | thisVertexData->vertexDeclaration->removeElement( VES_BLEND_WEIGHTS ); |
107 | 0 | if (thisVertexData->vertexDeclaration->findElementsBySource(skelDataSource).empty()) |
108 | 0 | { |
109 | 0 | thisVertexData->vertexDeclaration->closeGapsInSource(); |
110 | 0 | thisVertexData->vertexBufferBinding->unsetBinding(skelDataSource); |
111 | 0 | VertexBufferBinding::BindingIndexMap tmpMap; |
112 | 0 | thisVertexData->vertexBufferBinding->closeGaps(tmpMap); |
113 | 0 | } |
114 | 0 | } |
115 | 0 | } |
116 | |
|
117 | 0 | createVertexTexture( baseSubMesh ); |
118 | 0 | createVertexSemantics( thisVertexData, baseVertexData, hwBoneIdx, hwBoneWgt); |
119 | 0 | } |
120 | | //----------------------------------------------------------------------- |
121 | | void InstanceBatchHW_VTF::setupIndices( const SubMesh* baseSubMesh ) |
122 | 0 | { |
123 | | //We could use just a reference, but the InstanceManager will in the end attampt to delete |
124 | | //the pointer, and we can't give it something that doesn't belong to us. |
125 | 0 | mRenderOperation.indexData = baseSubMesh->indexData->clone( true ); |
126 | 0 | mRemoveOwnIndexData = true; //Raise flag to remove our own index data in the end (not always needed) |
127 | 0 | } |
128 | | //----------------------------------------------------------------------- |
129 | | void InstanceBatchHW_VTF::createVertexSemantics( VertexData *thisVertexData, |
130 | | VertexData *baseVertexData, |
131 | | const HWBoneIdxVec &hwBoneIdx, |
132 | | const HWBoneWgtVec& hwBoneWgt) |
133 | 0 | { |
134 | 0 | const float texWidth = static_cast<float>(mMatrixTexture->getWidth()); |
135 | | |
136 | | //Only one weight per vertex is supported. It would not only be complex, but prohibitively slow. |
137 | | //Put them in a new buffer, since it's 16 bytes aligned :-) |
138 | 0 | unsigned short newSource = thisVertexData->vertexDeclaration->getMaxSource() + 1; |
139 | |
|
140 | 0 | size_t offset = 0; |
141 | |
|
142 | 0 | size_t maxFloatsPerVector = 4; |
143 | | |
144 | | //Can fit two dual quaternions in every float4, but only one 3x4 matrix |
145 | 0 | for(size_t i = 0; i < mWeightCount; i += maxFloatsPerVector / mRowLength) |
146 | 0 | { |
147 | 0 | offset += thisVertexData->vertexDeclaration->addElement( newSource, offset, VET_FLOAT4, VES_TEXTURE_COORDINATES, |
148 | 0 | thisVertexData->vertexDeclaration->getNextFreeTextureCoordinate() ).getSize(); |
149 | 0 | } |
150 | | |
151 | | //Add the weights (supports up to four, which is Ogre's limit) |
152 | 0 | if(mWeightCount > 1) |
153 | 0 | { |
154 | 0 | thisVertexData->vertexDeclaration->addElement(newSource, offset, VET_FLOAT4, VES_BLEND_WEIGHTS, |
155 | 0 | 0 ).getSize(); |
156 | 0 | } |
157 | | |
158 | | //Create our own vertex buffer |
159 | 0 | HardwareVertexBufferSharedPtr vertexBuffer = |
160 | 0 | HardwareBufferManager::getSingleton().createVertexBuffer( |
161 | 0 | thisVertexData->vertexDeclaration->getVertexSize(newSource), |
162 | 0 | thisVertexData->vertexCount, |
163 | 0 | HardwareBuffer::HBU_STATIC_WRITE_ONLY ); |
164 | 0 | thisVertexData->vertexBufferBinding->setBinding( newSource, vertexBuffer ); |
165 | |
|
166 | 0 | HardwareBufferLockGuard vertexLock(vertexBuffer, HardwareBuffer::HBL_DISCARD); |
167 | 0 | float *thisFloat = static_cast<float*>(vertexLock.pData); |
168 | | |
169 | | //Create the UVs to sample from the right bone/matrix |
170 | 0 | for( size_t j=0; j < baseVertexData->vertexCount * mWeightCount; j += mWeightCount) |
171 | 0 | { |
172 | 0 | size_t numberOfMatricesInLine = 0; |
173 | | |
174 | | //Write the matrices, adding padding as needed |
175 | 0 | for(size_t i = 0; i < mWeightCount; ++i) |
176 | 0 | { |
177 | | //Write the matrix |
178 | 0 | for( size_t k=0; k < mRowLength; ++k) |
179 | 0 | { |
180 | | //Only calculate U (not V) since all matrices are in the same row. We use the instanced |
181 | | //(repeated) buffer to tell how much U & V we need to offset |
182 | 0 | size_t instanceIdx = hwBoneIdx[j+i] * mRowLength + k; |
183 | 0 | *thisFloat++ = instanceIdx / texWidth; |
184 | 0 | } |
185 | |
|
186 | 0 | ++numberOfMatricesInLine; |
187 | | |
188 | | //If another matrix can't be fit, we're on another line, or if this is the last weight |
189 | 0 | if((numberOfMatricesInLine + 1) * mRowLength > maxFloatsPerVector || (i+1) == mWeightCount) |
190 | 0 | { |
191 | | //Place zeroes in the remaining coordinates |
192 | 0 | for ( size_t k=mRowLength * numberOfMatricesInLine; k < maxFloatsPerVector; ++k) |
193 | 0 | { |
194 | 0 | *thisFloat++ = 0.0f; |
195 | 0 | } |
196 | |
|
197 | 0 | numberOfMatricesInLine = 0; |
198 | 0 | } |
199 | 0 | } |
200 | | |
201 | | //Don't need to write weights if there is only one |
202 | 0 | if(mWeightCount > 1) |
203 | 0 | { |
204 | | //Write the weights |
205 | 0 | for(size_t i = 0; i < mWeightCount; ++i) |
206 | 0 | { |
207 | 0 | *thisFloat++ = hwBoneWgt[j+i]; |
208 | 0 | } |
209 | | |
210 | | //Write the empty space |
211 | 0 | for(size_t i = mWeightCount; i < maxFloatsPerVector; ++i) |
212 | 0 | { |
213 | 0 | *thisFloat++ = 0.0f; |
214 | 0 | } |
215 | 0 | } |
216 | 0 | } |
217 | |
|
218 | 0 | vertexLock.unlock(); |
219 | | |
220 | | //Now create the instance buffer that will be incremented per instance, contains UV offsets |
221 | 0 | newSource = thisVertexData->vertexDeclaration->getMaxSource() + 1; |
222 | 0 | offset = thisVertexData->vertexDeclaration->addElement( newSource, 0, VET_FLOAT2, VES_TEXTURE_COORDINATES,thisVertexData->vertexDeclaration->getNextFreeTextureCoordinate() ).getSize(); |
223 | 0 | if (useBoneMatrixLookup()) |
224 | 0 | { |
225 | | //if using bone matrix lookup we will need to add 3 more float4 to contain the matrix. containing |
226 | | //the personal world transform of each entity. |
227 | 0 | offset += thisVertexData->vertexDeclaration->addElement( newSource, offset, VET_FLOAT4, VES_TEXTURE_COORDINATES, |
228 | 0 | thisVertexData->vertexDeclaration->getNextFreeTextureCoordinate() ).getSize(); |
229 | 0 | offset += thisVertexData->vertexDeclaration->addElement( newSource, offset, VET_FLOAT4, VES_TEXTURE_COORDINATES, |
230 | 0 | thisVertexData->vertexDeclaration->getNextFreeTextureCoordinate() ).getSize(); |
231 | 0 | thisVertexData->vertexDeclaration->addElement( newSource, offset, VET_FLOAT4, VES_TEXTURE_COORDINATES, |
232 | 0 | thisVertexData->vertexDeclaration->getNextFreeTextureCoordinate() ).getSize(); |
233 | | //Add two floats of padding here? or earlier? |
234 | | //If not using bone matrix lookup, is it ok that it is 8 bytes since divides evenly into 16 |
235 | |
|
236 | 0 | } |
237 | | |
238 | | //Create our own vertex buffer |
239 | 0 | mInstanceVertexBuffer = HardwareBufferManager::getSingleton().createVertexBuffer( |
240 | 0 | thisVertexData->vertexDeclaration->getVertexSize(newSource), |
241 | 0 | mInstancesPerBatch, |
242 | 0 | HardwareBuffer::HBU_STATIC_WRITE_ONLY ); |
243 | 0 | thisVertexData->vertexBufferBinding->setBinding( newSource, mInstanceVertexBuffer ); |
244 | | |
245 | | //Mark this buffer as instanced |
246 | 0 | mInstanceVertexBuffer->setIsInstanceData( true ); |
247 | 0 | mInstanceVertexBuffer->setInstanceDataStepRate( 1 ); |
248 | |
|
249 | 0 | updateInstanceDataBuffer(true, NULL); |
250 | 0 | } |
251 | | |
252 | | //updates the vertex buffer containing the per instance data |
253 | | size_t InstanceBatchHW_VTF::updateInstanceDataBuffer(bool isFirstTime, Camera* currentCamera) |
254 | 0 | { |
255 | 0 | size_t visibleEntityCount = 0; |
256 | 0 | bool useMatrixLookup = useBoneMatrixLookup(); |
257 | 0 | if (isFirstTime ^ useMatrixLookup) |
258 | 0 | { |
259 | | //update the mTransformLookupNumber value in the entities if needed |
260 | 0 | updateSharedLookupIndexes(); |
261 | |
|
262 | 0 | const float texWidth = static_cast<float>(mMatrixTexture->getWidth()); |
263 | 0 | const float texHeight = static_cast<float>(mMatrixTexture->getHeight()); |
264 | | |
265 | | //Calculate the texel offsets to correct them offline |
266 | | //Awkwardly enough, the offset is needed in OpenGL too |
267 | 0 | Vector2 texelOffsets; |
268 | | //RenderSystem *renderSystem = Root::getSingleton().getRenderSystem(); |
269 | 0 | texelOffsets.x = /*renderSystem->getHorizontalTexelOffset()*/ -0.5f / texWidth; |
270 | 0 | texelOffsets.y = /*renderSystem->getHorizontalTexelOffset()*/ -0.5f / texHeight; |
271 | |
|
272 | 0 | HardwareBufferLockGuard instanceVertexLock(mInstanceVertexBuffer, HardwareBuffer::HBL_DISCARD); |
273 | 0 | float *thisVec = static_cast<float*>(instanceVertexLock.pData); |
274 | |
|
275 | 0 | const size_t maxPixelsPerLine = std::min( static_cast<size_t>(mMatrixTexture->getWidth()), mMaxFloatsPerLine >> 2 ); |
276 | | |
277 | | //Calculate UV offsets, which change per instance |
278 | 0 | for( size_t i=0; i<mInstancesPerBatch; ++i ) |
279 | 0 | { |
280 | 0 | InstancedEntity* entity = useMatrixLookup ? mInstancedEntities[i] : NULL; |
281 | 0 | if //Update if we are not using a lookup bone matrix method. In this case the function will |
282 | | //be called only once |
283 | 0 | (!useMatrixLookup || |
284 | | //Update if we are in the visible range of the camera (for look up bone matrix method |
285 | | //and static mode). |
286 | 0 | (entity->findVisible(currentCamera))) |
287 | 0 | { |
288 | 0 | size_t matrixIndex = useMatrixLookup ? entity->mTransformLookupNumber : i; |
289 | 0 | size_t instanceIdx = matrixIndex * mMatricesPerInstance * mRowLength; |
290 | 0 | *thisVec = ((instanceIdx % maxPixelsPerLine) / texWidth) - (float)(texelOffsets.x); |
291 | 0 | *(thisVec + 1) = ((instanceIdx / maxPixelsPerLine) / texHeight) - (float)(texelOffsets.y); |
292 | 0 | thisVec += 2; |
293 | |
|
294 | 0 | if (useMatrixLookup) |
295 | 0 | { |
296 | 0 | const Affine3& mat = entity->_getParentNodeFullTransform(); |
297 | 0 | *(thisVec) = static_cast<float>( mat[0][0] ); |
298 | 0 | *(thisVec + 1) = static_cast<float>( mat[0][1] ); |
299 | 0 | *(thisVec + 2) = static_cast<float>( mat[0][2] ); |
300 | 0 | *(thisVec + 3) = static_cast<float>( mat[0][3] ); |
301 | 0 | *(thisVec + 4) = static_cast<float>( mat[1][0] ); |
302 | 0 | *(thisVec + 5) = static_cast<float>( mat[1][1] ); |
303 | 0 | *(thisVec + 6) = static_cast<float>( mat[1][2] ); |
304 | 0 | *(thisVec + 7) = static_cast<float>( mat[1][3] ); |
305 | 0 | *(thisVec + 8) = static_cast<float>( mat[2][0] ); |
306 | 0 | *(thisVec + 9) = static_cast<float>( mat[2][1] ); |
307 | 0 | *(thisVec + 10)= static_cast<float>( mat[2][2] ); |
308 | 0 | *(thisVec + 11)= static_cast<float>( mat[2][3] ); |
309 | 0 | if(currentCamera && mManager->getCameraRelativeRendering()) // && useMatrixLookup |
310 | 0 | { |
311 | 0 | const Vector3 &cameraRelativePosition = currentCamera->getDerivedPosition(); |
312 | 0 | *(thisVec + 3) -= static_cast<float>( cameraRelativePosition.x ); |
313 | 0 | *(thisVec + 7) -= static_cast<float>( cameraRelativePosition.y ); |
314 | 0 | *(thisVec + 11) -= static_cast<float>( cameraRelativePosition.z ); |
315 | 0 | } |
316 | 0 | thisVec += 12; |
317 | 0 | } |
318 | 0 | ++visibleEntityCount; |
319 | 0 | } |
320 | 0 | } |
321 | 0 | } |
322 | 0 | else |
323 | 0 | { |
324 | 0 | visibleEntityCount = mInstancedEntities.size(); |
325 | 0 | } |
326 | 0 | return visibleEntityCount; |
327 | 0 | } |
328 | | |
329 | | //----------------------------------------------------------------------- |
330 | | bool InstanceBatchHW_VTF::checkSubMeshCompatibility( const SubMesh* baseSubMesh ) |
331 | 0 | { |
332 | | //Max number of texture coordinates is _usually_ 8, we need at least 2 available |
333 | 0 | unsigned short neededTextureCoord = 2; |
334 | 0 | if (useBoneMatrixLookup()) |
335 | 0 | { |
336 | | //we need another 3 for the unique world transform of each instanced entity |
337 | 0 | neededTextureCoord += 3; |
338 | 0 | } |
339 | 0 | if( baseSubMesh->vertexData->vertexDeclaration->getNextFreeTextureCoordinate() > 8 - neededTextureCoord ) |
340 | 0 | { |
341 | 0 | OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, |
342 | 0 | String("Given mesh must have at least ") + |
343 | 0 | StringConverter::toString(neededTextureCoord) + "free TEXCOORDs", |
344 | 0 | "InstanceBatchHW_VTF::checkSubMeshCompatibility"); |
345 | 0 | } |
346 | | |
347 | 0 | return InstanceBatch::checkSubMeshCompatibility( baseSubMesh ); |
348 | 0 | } |
349 | | //----------------------------------------------------------------------- |
350 | | size_t InstanceBatchHW_VTF::calculateMaxNumInstances( |
351 | | const SubMesh *baseSubMesh, uint16 flags ) const |
352 | 0 | { |
353 | 0 | size_t retVal = 0; |
354 | |
|
355 | 0 | RenderSystem *renderSystem = Root::getSingleton().getRenderSystem(); |
356 | 0 | const RenderSystemCapabilities *capabilities = renderSystem->getCapabilities(); |
357 | | |
358 | | //VTF & HW Instancing must be supported |
359 | 0 | if( capabilities->hasCapability( RSC_VERTEX_BUFFER_INSTANCE_DATA ) && |
360 | 0 | capabilities->getNumVertexTextureUnits() ) |
361 | 0 | { |
362 | | //TODO: Check PF_FLOAT32_RGBA is supported (should be, since it was the 1st one) |
363 | 0 | const size_t numBones = std::max<size_t>( 1, baseSubMesh->blendIndexToBoneIndexMap.size() ); |
364 | | |
365 | | // mRowLength can only be 2 or 3 |
366 | 0 | static_assert(3 * OGRE_MAX_NUM_BONES < c_maxTexWidthHW, "Too many bones for VTF"); |
367 | 0 | const size_t maxUsableWidth = c_maxTexWidthHW - (c_maxTexWidthHW % (numBones * mRowLength)); |
368 | | |
369 | | //See InstanceBatchHW::calculateMaxNumInstances for the 65535 |
370 | 0 | retVal = std::min<size_t>( 65535, maxUsableWidth * c_maxTexHeightHW / mRowLength / numBones ); |
371 | |
|
372 | 0 | if( flags & IM_VTFBESTFIT ) |
373 | 0 | { |
374 | 0 | size_t numUsedSkeletons = mInstancesPerBatch; |
375 | 0 | if (flags & IM_VTFBONEMATRIXLOOKUP) |
376 | 0 | numUsedSkeletons = std::min<size_t>(getMaxLookupTableInstances(), numUsedSkeletons); |
377 | 0 | const size_t instancesPerBatch = std::min( retVal, numUsedSkeletons ); |
378 | | //Do the same as in createVertexTexture(), but changing c_maxTexWidthHW for maxUsableWidth |
379 | 0 | const size_t numWorldMatrices = instancesPerBatch * numBones; |
380 | |
|
381 | 0 | size_t texWidth = std::min<size_t>( numWorldMatrices * mRowLength, maxUsableWidth ); |
382 | 0 | size_t texHeight = numWorldMatrices * mRowLength / maxUsableWidth; |
383 | |
|
384 | 0 | const size_t remainder = (numWorldMatrices * mRowLength) % maxUsableWidth; |
385 | |
|
386 | 0 | if( remainder && texHeight > 0 ) |
387 | 0 | retVal = static_cast<size_t>(texWidth * texHeight / (float)mRowLength / (float)(numBones)); |
388 | 0 | } |
389 | 0 | } |
390 | |
|
391 | 0 | return retVal; |
392 | 0 | } |
393 | | //----------------------------------------------------------------------- |
394 | | size_t InstanceBatchHW_VTF::updateVertexTexture( Camera *currentCamera ) |
395 | 0 | { |
396 | 0 | size_t renderedInstances = 0; |
397 | 0 | bool useMatrixLookup = useBoneMatrixLookup(); |
398 | 0 | if (useMatrixLookup) |
399 | 0 | { |
400 | | //if we are using bone matrix look up we have to update the instance buffer for the |
401 | | //vertex texture to be relevant |
402 | | |
403 | | //also note that in this case the number of instances to render comes directly from the |
404 | | //updateInstanceDataBuffer() function, not from this function. |
405 | 0 | renderedInstances = updateInstanceDataBuffer(false, currentCamera); |
406 | 0 | } |
407 | | |
408 | | |
409 | 0 | mDirtyAnimation = false; |
410 | | |
411 | | //Now lock the texture and copy the 4x3 matrices! |
412 | 0 | HardwareBufferLockGuard matTexLock(mMatrixTexture->getBuffer(), HardwareBuffer::HBL_DISCARD); |
413 | 0 | const PixelBox &pixelBox = mMatrixTexture->getBuffer()->getCurrentLock(); |
414 | |
|
415 | 0 | float *pSource = reinterpret_cast<float*>(pixelBox.data); |
416 | | |
417 | 0 | InstancedEntityVec::const_iterator itor = mInstancedEntities.begin(); |
418 | | |
419 | 0 | std::vector<bool> writtenPositions(getMaxLookupTableInstances(), false); |
420 | |
|
421 | 0 | size_t floatPerEntity = mMatricesPerInstance * mRowLength * 4; |
422 | 0 | size_t entitiesPerPadding = (size_t)(mMaxFloatsPerLine / floatPerEntity); |
423 | | |
424 | 0 | size_t instanceCount = mInstancedEntities.size(); |
425 | 0 | size_t updatedInstances = 0; |
426 | |
|
427 | 0 | Matrix3x4f* transforms = NULL; |
428 | | //If using dual quaternions, write 3x4 matrices to a temporary buffer, then convert to dual quaternions |
429 | 0 | if(mUseBoneDualQuaternions) |
430 | 0 | { |
431 | 0 | transforms = (Matrix3x4f*)mTempTransformsArray3x4; |
432 | 0 | } |
433 | | |
434 | 0 | for(size_t i = 0 ; i < instanceCount ; ++i) |
435 | 0 | { |
436 | 0 | InstancedEntity* entity = mInstancedEntities[i]; |
437 | 0 | size_t textureLookupPosition = updatedInstances; |
438 | 0 | if (useMatrixLookup) |
439 | 0 | { |
440 | 0 | textureLookupPosition = entity->mTransformLookupNumber; |
441 | 0 | } |
442 | | //Check that we are not using a lookup matrix or that we have not already written |
443 | | //The bone data |
444 | 0 | if (((!useMatrixLookup) || !writtenPositions[entity->mTransformLookupNumber]) && |
445 | | //Cull on an individual basis, the less entities are visible, the less instances we draw. |
446 | | //No need to use null matrices at all! |
447 | 0 | (entity->findVisible( currentCamera ))) |
448 | 0 | { |
449 | 0 | float* pDest = pSource + floatPerEntity * textureLookupPosition + |
450 | 0 | (size_t)(textureLookupPosition / entitiesPerPadding) * mWidthFloatsPadding; |
451 | |
|
452 | 0 | if(!mUseBoneDualQuaternions) |
453 | 0 | { |
454 | 0 | transforms = (Matrix3x4f*)pDest; |
455 | 0 | } |
456 | | |
457 | 0 | if( mMeshReference->hasSkeleton() ) |
458 | 0 | mDirtyAnimation |= entity->_updateAnimation(); |
459 | |
|
460 | 0 | size_t floatsWritten = entity->getTransforms3x4( transforms ); |
461 | |
|
462 | 0 | if( !useMatrixLookup && mManager->getCameraRelativeRendering() ) |
463 | 0 | makeMatrixCameraRelative3x4( transforms, floatsWritten / 12 ); |
464 | |
|
465 | 0 | if(mUseBoneDualQuaternions) |
466 | 0 | { |
467 | 0 | convert3x4MatricesToDualQuaternions(transforms, floatsWritten / 12, pDest); |
468 | 0 | } |
469 | |
|
470 | 0 | if (useMatrixLookup) |
471 | 0 | { |
472 | 0 | writtenPositions[entity->mTransformLookupNumber] = true; |
473 | 0 | } |
474 | 0 | else |
475 | 0 | { |
476 | 0 | ++updatedInstances; |
477 | 0 | } |
478 | 0 | } |
479 | |
|
480 | 0 | ++itor; |
481 | 0 | } |
482 | |
|
483 | 0 | if (!useMatrixLookup) |
484 | 0 | { |
485 | 0 | renderedInstances = updatedInstances; |
486 | 0 | } |
487 | |
|
488 | 0 | return renderedInstances; |
489 | 0 | } |
490 | | //----------------------------------------------------------------------- |
491 | | void InstanceBatchHW_VTF::_boundsDirty(void) |
492 | 0 | { |
493 | | //Don't update if we're static, but still mark we're dirty |
494 | 0 | if( !mBoundsDirty && !mKeepStatic && mCreator) |
495 | 0 | mCreator->_addDirtyBatch( this ); |
496 | 0 | mBoundsDirty = true; |
497 | 0 | } |
498 | | //----------------------------------------------------------------------- |
499 | | void InstanceBatchHW_VTF::setStaticAndUpdate( bool bStatic ) |
500 | 0 | { |
501 | | //We were dirty but didn't update bounds. Do it now. |
502 | 0 | if( mKeepStatic && mBoundsDirty ) |
503 | 0 | mCreator->_addDirtyBatch( this ); |
504 | |
|
505 | 0 | mKeepStatic = bStatic; |
506 | 0 | if( mKeepStatic ) |
507 | 0 | { |
508 | | //One final update, since there will be none from now on |
509 | | //(except further calls to this function). Pass NULL because |
510 | | //we want to include only those who were added to the scene |
511 | | //but we don't want to perform culling |
512 | 0 | mRenderOperation.numberOfInstances = updateVertexTexture( 0 ); |
513 | 0 | } |
514 | 0 | } |
515 | | //----------------------------------------------------------------------- |
516 | | void InstanceBatchHW_VTF::_updateRenderQueue( RenderQueue* queue ) |
517 | 0 | { |
518 | 0 | if( !mKeepStatic ) |
519 | 0 | { |
520 | | //Completely override base functionality, since we don't cull on an "all-or-nothing" basis |
521 | 0 | if( (mRenderOperation.numberOfInstances = updateVertexTexture( mCurrentCamera )) ) |
522 | 0 | queue->addRenderable( this, mRenderQueueID, mRenderQueuePriority ); |
523 | 0 | } |
524 | 0 | else |
525 | 0 | { |
526 | 0 | OgreAssert(!mManager->getCameraRelativeRendering(), |
527 | 0 | "Camera-relative rendering is incompatible with Instancing's static batches. " |
528 | 0 | "Disable at least one of them"); |
529 | | |
530 | | //Don't update when we're static |
531 | 0 | if( mRenderOperation.numberOfInstances ) |
532 | 0 | queue->addRenderable( this, mRenderQueueID, mRenderQueuePriority ); |
533 | 0 | } |
534 | 0 | } |
535 | | } |