/src/ogre/Components/RTShaderSystem/src/OgreShaderExHardwareSkinning.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | | of this software and associated documentation files (the "Software"), to deal |
10 | | in the Software without restriction, including without limitation the rights |
11 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12 | | copies of the Software, and to permit persons to whom the Software is |
13 | | furnished to do so, subject to the following conditions: |
14 | | |
15 | | The above copyright notice and this permission notice shall be included in |
16 | | all copies or substantial portions of the Software. |
17 | | |
18 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24 | | THE SOFTWARE. |
25 | | ----------------------------------------------------------------------------- |
26 | | */ |
27 | | #include "OgreShaderPrecompiledHeaders.h" |
28 | | |
29 | | #ifdef RTSHADER_SYSTEM_BUILD_EXT_SHADERS |
30 | | |
31 | 0 | #define HS_DATA_BIND_NAME "HS_SRS_DATA" |
32 | | |
33 | | |
34 | | namespace Ogre { |
35 | | template<> RTShader::HardwareSkinningFactory* Singleton<RTShader::HardwareSkinningFactory>::msSingleton = 0; |
36 | | |
37 | | namespace RTShader { |
38 | | |
39 | | HardwareSkinningFactory* HardwareSkinningFactory::getSingletonPtr(void) |
40 | 0 | { |
41 | 0 | return msSingleton; |
42 | 0 | } |
43 | | HardwareSkinningFactory& HardwareSkinningFactory::getSingleton(void) |
44 | 0 | { |
45 | 0 | assert( msSingleton ); return ( *msSingleton ); |
46 | 0 | } |
47 | | |
48 | | String HardwareSkinning::Type = "SGX_HardwareSkinning"; |
49 | | const String SRS_HARDWARE_SKINNING = "SGX_HardwareSkinning"; |
50 | | |
51 | | ushort HardwareSkinningFactory::mMaxCalculableBoneCount = 70; |
52 | | |
53 | | #define HS_MAX_WEIGHT_COUNT 4 |
54 | | |
55 | | /// A set of custom shadow caster materials |
56 | | static MaterialPtr mCustomShadowCasterMaterialsLinear[HS_MAX_WEIGHT_COUNT]; |
57 | | static MaterialPtr mCustomShadowCasterMaterialsDualQuaternion[HS_MAX_WEIGHT_COUNT]; |
58 | | |
59 | | /// A set of custom shadow receiver materials |
60 | | static MaterialPtr mCustomShadowReceiverMaterialsLinear[HS_MAX_WEIGHT_COUNT]; |
61 | | static MaterialPtr mCustomShadowReceiverMaterialsDualQuaternion[HS_MAX_WEIGHT_COUNT]; |
62 | | |
63 | | /************************************************************************/ |
64 | | /* */ |
65 | | /************************************************************************/ |
66 | | HardwareSkinning::HardwareSkinning() : |
67 | | mCreator(NULL), |
68 | 0 | mSkinningType(ST_LINEAR) |
69 | 0 | { |
70 | 0 | } |
71 | | |
72 | | //----------------------------------------------------------------------- |
73 | | const String& HardwareSkinning::getType() const |
74 | 0 | { |
75 | 0 | return SRS_HARDWARE_SKINNING; |
76 | 0 | } |
77 | | |
78 | | //----------------------------------------------------------------------- |
79 | | int HardwareSkinning::getExecutionOrder() const |
80 | 0 | { |
81 | 0 | return FFP_TRANSFORM; |
82 | 0 | } |
83 | | |
84 | | //----------------------------------------------------------------------- |
85 | | bool HardwareSkinning::setParameter(const String& name, const String& value) |
86 | 0 | { |
87 | 0 | if (name == "type") |
88 | 0 | { |
89 | 0 | if (value == "dual_quaternion") |
90 | 0 | { |
91 | 0 | mSkinningType = ST_DUAL_QUATERNION; |
92 | 0 | if (!mDualQuat) |
93 | 0 | { |
94 | 0 | mDualQuat.reset(OGRE_NEW DualQuaternionSkinning); |
95 | 0 | } |
96 | |
|
97 | 0 | mActiveTechnique = mDualQuat; |
98 | 0 | return true; |
99 | 0 | } |
100 | 0 | else if(value == "linear") |
101 | 0 | { |
102 | 0 | mSkinningType = ST_LINEAR; |
103 | 0 | if (!mLinear) |
104 | 0 | { |
105 | 0 | mLinear.reset(OGRE_NEW LinearSkinning); |
106 | 0 | } |
107 | |
|
108 | 0 | mActiveTechnique = mLinear; |
109 | 0 | return true; |
110 | 0 | } |
111 | 0 | } |
112 | 0 | else if(mActiveTechnique) |
113 | 0 | { |
114 | 0 | return mActiveTechnique->setParameter(name, value); |
115 | 0 | } |
116 | 0 | return false; |
117 | 0 | } |
118 | | //----------------------------------------------------------------------- |
119 | | ushort HardwareSkinning::getBoneCount() |
120 | 0 | { |
121 | 0 | assert(mActiveTechnique); |
122 | 0 | return mActiveTechnique->getBoneCount(); |
123 | 0 | } |
124 | | |
125 | | //----------------------------------------------------------------------- |
126 | | ushort HardwareSkinning::getWeightCount() |
127 | 0 | { |
128 | 0 | assert(mActiveTechnique); |
129 | 0 | return mActiveTechnique->getWeightCount(); |
130 | 0 | } |
131 | | |
132 | | //----------------------------------------------------------------------- |
133 | | SkinningType HardwareSkinning::getSkinningType() |
134 | 0 | { |
135 | 0 | assert(mActiveTechnique); |
136 | 0 | return mSkinningType; |
137 | 0 | } |
138 | | |
139 | | //----------------------------------------------------------------------- |
140 | | bool HardwareSkinning::hasCorrectAntipodalityHandling() |
141 | 0 | { |
142 | 0 | assert(mActiveTechnique); |
143 | 0 | return mActiveTechnique->hasCorrectAntipodalityHandling(); |
144 | 0 | } |
145 | | |
146 | | //----------------------------------------------------------------------- |
147 | | bool HardwareSkinning::hasScalingShearingSupport() |
148 | 0 | { |
149 | 0 | assert(mActiveTechnique); |
150 | 0 | return mActiveTechnique->hasScalingShearingSupport(); |
151 | 0 | } |
152 | | |
153 | | //----------------------------------------------------------------------- |
154 | | void HardwareSkinning::copyFrom(const SubRenderState& rhs) |
155 | 0 | { |
156 | 0 | const HardwareSkinning& hardSkin = static_cast<const HardwareSkinning&>(rhs); |
157 | |
|
158 | 0 | mDualQuat = hardSkin.mDualQuat; |
159 | 0 | mLinear = hardSkin.mLinear; |
160 | 0 | mActiveTechnique = hardSkin.mActiveTechnique; |
161 | | |
162 | 0 | mCreator = hardSkin.mCreator; |
163 | 0 | mSkinningType = hardSkin.mSkinningType; |
164 | 0 | } |
165 | | |
166 | | //----------------------------------------------------------------------- |
167 | | bool HardwareSkinning::preAddToRenderState(const RenderState* renderState, Pass* srcPass, Pass* dstPass) |
168 | 0 | { |
169 | 0 | bool isValid = true; |
170 | 0 | Technique* pFirstTech = srcPass->getParent()->getParent()->getTechnique(0); |
171 | 0 | const Any& hsAny = pFirstTech->getUserObjectBindings().getUserAny(HS_DATA_BIND_NAME); |
172 | |
|
173 | 0 | if (hsAny.has_value()) |
174 | 0 | { |
175 | 0 | HardwareSkinning::SkinningData pData = any_cast<HardwareSkinning::SkinningData>(hsAny); |
176 | 0 | isValid = pData.isValid; |
177 | | |
178 | | //If the skinning data is being passed through the material, we need to create an instance of the appropriate |
179 | | //skinning type and set its parameters here |
180 | 0 | setParameter("type", pData.skinningType == ST_LINEAR ? "linear" : "dual_quaternion"); |
181 | 0 | mActiveTechnique->setHardwareSkinningParam(pData.maxBoneCount, pData.maxWeightCount, |
182 | 0 | pData.correctAntipodalityHandling, pData.scalingShearingSupport); |
183 | 0 | } |
184 | | |
185 | | //If there is no associated technique, default to linear skinning as a pass-through |
186 | 0 | if(!mActiveTechnique) |
187 | 0 | { |
188 | 0 | setParameter("type", "linear"); |
189 | 0 | } |
190 | |
|
191 | 0 | int boneCount = mActiveTechnique->getBoneCount(); |
192 | 0 | int weightCount = mActiveTechnique->getWeightCount(); |
193 | |
|
194 | 0 | bool doBoneCalculations = isValid && |
195 | 0 | (boneCount != 0) && (boneCount <= 256) && |
196 | 0 | (weightCount != 0) && (weightCount <= 4) && |
197 | 0 | ((mCreator == NULL) || (boneCount <= mCreator->getMaxCalculableBoneCount())); |
198 | | |
199 | | // This requires GLES3.0 |
200 | 0 | if (ShaderGenerator::getSingleton().getTargetLanguage() == "glsles" && |
201 | 0 | !GpuProgramManager::getSingleton().isSyntaxSupported("glsl300es")) |
202 | 0 | doBoneCalculations = false; |
203 | |
|
204 | 0 | mActiveTechnique->setDoBoneCalculations(doBoneCalculations); |
205 | 0 | mActiveTechnique->setDoLightCalculations(srcPass->getLightingEnabled()); |
206 | |
|
207 | 0 | if ((doBoneCalculations) && (mCreator)) |
208 | 0 | { |
209 | | //update the receiver and caster materials |
210 | 0 | if (!dstPass->getParent()->getShadowCasterMaterial()) |
211 | 0 | { |
212 | 0 | auto casterMat = mCreator->getCustomShadowCasterMaterial(mSkinningType, weightCount - 1); |
213 | | |
214 | | // if the caster material itsefl uses RTSS hardware skinning |
215 | 0 | if(casterMat.get() != dstPass->getParent()->getParent()) |
216 | 0 | dstPass->getParent()->setShadowCasterMaterial(casterMat); |
217 | 0 | } |
218 | |
|
219 | 0 | if (!dstPass->getParent()->getShadowReceiverMaterial()) |
220 | 0 | { |
221 | 0 | dstPass->getParent()->setShadowReceiverMaterial( |
222 | 0 | mCreator->getCustomShadowReceiverMaterial(mSkinningType, weightCount - 1)); |
223 | 0 | } |
224 | 0 | } |
225 | |
|
226 | 0 | return true; |
227 | 0 | } |
228 | | |
229 | | //----------------------------------------------------------------------- |
230 | | bool HardwareSkinning::resolveParameters(ProgramSet* programSet) |
231 | 0 | { |
232 | 0 | assert(mActiveTechnique); |
233 | 0 | return mActiveTechnique->resolveParameters(programSet); |
234 | 0 | } |
235 | | |
236 | | //----------------------------------------------------------------------- |
237 | | bool HardwareSkinning::resolveDependencies(ProgramSet* programSet) |
238 | 0 | { |
239 | 0 | assert(mActiveTechnique); |
240 | 0 | return mActiveTechnique->resolveDependencies(programSet); |
241 | 0 | } |
242 | | |
243 | | //----------------------------------------------------------------------- |
244 | | bool HardwareSkinning::addFunctionInvocations(ProgramSet* programSet) |
245 | 0 | { |
246 | 0 | assert(mActiveTechnique); |
247 | 0 | return mActiveTechnique->addFunctionInvocations(programSet); |
248 | 0 | } |
249 | | |
250 | | //----------------------------------------------------------------------- |
251 | | HardwareSkinningFactory::HardwareSkinningFactory() |
252 | 0 | { |
253 | |
|
254 | 0 | } |
255 | | |
256 | 0 | HardwareSkinningFactory::~HardwareSkinningFactory() {} |
257 | | |
258 | | //----------------------------------------------------------------------- |
259 | | const String& HardwareSkinningFactory::getType() const |
260 | 0 | { |
261 | 0 | return SRS_HARDWARE_SKINNING; |
262 | 0 | } |
263 | | |
264 | | //----------------------------------------------------------------------- |
265 | | SubRenderState* HardwareSkinningFactory::createInstance(const ScriptProperty& prop, Pass* pass, SGScriptTranslator* translator) |
266 | 0 | { |
267 | 0 | if (prop.name == "hardware_skinning") |
268 | 0 | { |
269 | 0 | String skinningType = "linear"; |
270 | | |
271 | 0 | if(prop.values.size() < 2) |
272 | 0 | return NULL; |
273 | | |
274 | 0 | std::map<String, String> params; |
275 | 0 | params["max_bone_count"] = prop.values[0]; |
276 | 0 | params["weight_count"] = prop.values[1]; |
277 | |
|
278 | 0 | if(prop.values.size() >= 3) |
279 | 0 | { |
280 | 0 | skinningType = prop.values[2]; |
281 | 0 | } |
282 | |
|
283 | 0 | if(skinningType != "dual_quaternion" && skinningType != "linear") |
284 | 0 | return NULL; |
285 | | |
286 | 0 | if(prop.values.size() >= 5) |
287 | 0 | { |
288 | 0 | params["correct_antipodality"] = prop.values[3]; |
289 | 0 | params["scale_shearing"] = prop.values[4]; |
290 | 0 | } |
291 | | |
292 | | //create and update the hardware skinning sub render state |
293 | 0 | SubRenderState* subRenderState = createOrRetrieveInstance(translator); |
294 | 0 | subRenderState->setParameter("type", skinningType); |
295 | |
|
296 | 0 | for(const auto& p : params) |
297 | 0 | { |
298 | 0 | if(!subRenderState->setParameter(p.first, p.second)) |
299 | 0 | { |
300 | 0 | translator->emitError(p.second); |
301 | 0 | } |
302 | 0 | } |
303 | |
|
304 | 0 | return subRenderState; |
305 | 0 | } |
306 | | |
307 | 0 | return NULL; |
308 | 0 | } |
309 | | |
310 | | //----------------------------------------------------------------------- |
311 | | void HardwareSkinningFactory::writeInstance(MaterialSerializer* ser, SubRenderState* subRenderState, |
312 | | Pass* srcPass, Pass* dstPass) |
313 | 0 | { |
314 | 0 | ser->writeAttribute(4, "hardware_skinning"); |
315 | | |
316 | 0 | HardwareSkinning* hardSkinSrs = static_cast<HardwareSkinning*>(subRenderState); |
317 | 0 | ser->writeValue(StringConverter::toString(hardSkinSrs->getBoneCount())); |
318 | 0 | ser->writeValue(StringConverter::toString(hardSkinSrs->getWeightCount())); |
319 | | |
320 | | //Correct antipodality handling and scaling and shearing support are only really valid for dual quaternion skinning |
321 | 0 | if(hardSkinSrs->getSkinningType() == ST_DUAL_QUATERNION) |
322 | 0 | { |
323 | 0 | ser->writeValue("dual_quaternion"); |
324 | 0 | ser->writeValue(StringConverter::toString(hardSkinSrs->hasCorrectAntipodalityHandling())); |
325 | 0 | ser->writeValue(StringConverter::toString(hardSkinSrs->hasScalingShearingSupport())); |
326 | 0 | } |
327 | 0 | } |
328 | | |
329 | | //----------------------------------------------------------------------- |
330 | | SubRenderState* HardwareSkinningFactory::createInstanceImpl() |
331 | 0 | { |
332 | 0 | HardwareSkinning* pSkin = OGRE_NEW HardwareSkinning; |
333 | | |
334 | 0 | pSkin->_setCreator(this); |
335 | 0 | return pSkin; |
336 | 0 | } |
337 | | |
338 | | //----------------------------------------------------------------------- |
339 | | void HardwareSkinningFactory::setCustomShadowCasterMaterials(const SkinningType skinningType, const MaterialPtr& caster1Weight, const MaterialPtr& caster2Weight, |
340 | | const MaterialPtr& caster3Weight, const MaterialPtr& caster4Weight) |
341 | 0 | { |
342 | 0 | if(skinningType == ST_DUAL_QUATERNION) |
343 | 0 | { |
344 | 0 | mCustomShadowCasterMaterialsDualQuaternion[0] = caster1Weight; |
345 | 0 | mCustomShadowCasterMaterialsDualQuaternion[1] = caster2Weight; |
346 | 0 | mCustomShadowCasterMaterialsDualQuaternion[2] = caster3Weight; |
347 | 0 | mCustomShadowCasterMaterialsDualQuaternion[3] = caster4Weight; |
348 | 0 | } |
349 | 0 | else //if(skinningType == ST_LINEAR) |
350 | 0 | { |
351 | 0 | mCustomShadowCasterMaterialsLinear[0] = caster1Weight; |
352 | 0 | mCustomShadowCasterMaterialsLinear[1] = caster2Weight; |
353 | 0 | mCustomShadowCasterMaterialsLinear[2] = caster3Weight; |
354 | 0 | mCustomShadowCasterMaterialsLinear[3] = caster4Weight; |
355 | 0 | } |
356 | 0 | } |
357 | | |
358 | | //----------------------------------------------------------------------- |
359 | | void HardwareSkinningFactory::setCustomShadowReceiverMaterials(const SkinningType skinningType, const MaterialPtr& receiver1Weight, const MaterialPtr& receiver2Weight, |
360 | | const MaterialPtr& receiver3Weight, const MaterialPtr& receiver4Weight) |
361 | 0 | { |
362 | 0 | if(skinningType == ST_DUAL_QUATERNION) |
363 | 0 | { |
364 | 0 | mCustomShadowReceiverMaterialsDualQuaternion[0] = receiver1Weight; |
365 | 0 | mCustomShadowReceiverMaterialsDualQuaternion[1] = receiver2Weight; |
366 | 0 | mCustomShadowReceiverMaterialsDualQuaternion[2] = receiver3Weight; |
367 | 0 | mCustomShadowReceiverMaterialsDualQuaternion[3] = receiver4Weight; |
368 | 0 | } |
369 | 0 | else //if(skinningType == ST_LINEAR) |
370 | 0 | { |
371 | 0 | mCustomShadowReceiverMaterialsLinear[0] = receiver1Weight; |
372 | 0 | mCustomShadowReceiverMaterialsLinear[1] = receiver2Weight; |
373 | 0 | mCustomShadowReceiverMaterialsLinear[2] = receiver3Weight; |
374 | 0 | mCustomShadowReceiverMaterialsLinear[3] = receiver4Weight; |
375 | 0 | } |
376 | 0 | } |
377 | | |
378 | | //----------------------------------------------------------------------- |
379 | | const MaterialPtr& HardwareSkinningFactory::getCustomShadowCasterMaterial(const SkinningType skinningType, ushort index) |
380 | 0 | { |
381 | 0 | assert(index < HS_MAX_WEIGHT_COUNT); |
382 | |
|
383 | 0 | if(skinningType == ST_DUAL_QUATERNION) |
384 | 0 | { |
385 | 0 | return mCustomShadowCasterMaterialsDualQuaternion[index]; |
386 | 0 | } |
387 | 0 | else //if(skinningType = ST_LINEAR) |
388 | 0 | { |
389 | 0 | return mCustomShadowCasterMaterialsLinear[index]; |
390 | 0 | } |
391 | 0 | } |
392 | | |
393 | | //----------------------------------------------------------------------- |
394 | | const MaterialPtr& HardwareSkinningFactory::getCustomShadowReceiverMaterial(const SkinningType skinningType, ushort index) |
395 | 0 | { |
396 | 0 | assert(index < HS_MAX_WEIGHT_COUNT); |
397 | |
|
398 | 0 | if(skinningType == ST_DUAL_QUATERNION) |
399 | 0 | { |
400 | 0 | return mCustomShadowReceiverMaterialsDualQuaternion[index]; |
401 | 0 | } |
402 | 0 | else //if(skinningType == ST_LINEAR) |
403 | 0 | { |
404 | 0 | return mCustomShadowReceiverMaterialsLinear[index]; |
405 | 0 | } |
406 | 0 | } |
407 | | |
408 | | //---------------------------------------------------------------------- |
409 | | /** |
410 | | @brief |
411 | | Extracts the maximum amount of bones and weights used in an specific subentity of given entity. |
412 | | |
413 | | @param pEntity The entity from which the information needs to be extracted. |
414 | | @param subEntityIndex The index of subentity from which the information needs to be extracted. |
415 | | @param boneCount The maximum number of bones used by the entity. |
416 | | @param weightCount The maximum number of weights used by the entity. |
417 | | @return Returns true if the entity can use HS. False if not. |
418 | | */ |
419 | | static bool extractSkeletonData(const Entity* pEntity, size_t subEntityIndex, ushort& boneCount, ushort& weightCount) |
420 | 0 | { |
421 | 0 | bool isValidData = false; |
422 | 0 | boneCount = 0; |
423 | 0 | weightCount = 0; |
424 | | |
425 | | //Check if we have pose animation which the HS sub render state does not |
426 | | //know how to handle |
427 | 0 | bool hasVertexAnim = pEntity->getMesh()->hasVertexAnimation(); |
428 | | |
429 | | //gather data on the skeleton |
430 | 0 | if (!hasVertexAnim && pEntity->hasSkeleton()) |
431 | 0 | { |
432 | | //get weights count |
433 | 0 | const MeshPtr& pMesh = pEntity->getMesh(); |
434 | |
|
435 | 0 | RenderOperation ro; |
436 | 0 | SubMesh* pSubMesh = pMesh->getSubMesh(subEntityIndex); |
437 | 0 | pSubMesh->_getRenderOperation(ro,0); |
438 | | |
439 | | //get the largest bone assignment |
440 | 0 | boneCount = ushort(std::max(pMesh->sharedBlendIndexToBoneIndexMap.size(), pSubMesh->blendIndexToBoneIndexMap.size())); |
441 | | |
442 | | //go over vertex deceleration |
443 | | //check that they have blend indices and blend weights |
444 | 0 | const VertexElement* pDeclWeights = ro.vertexData->vertexDeclaration->findElementBySemantic(VES_BLEND_WEIGHTS,0); |
445 | 0 | const VertexElement* pDeclIndexes = ro.vertexData->vertexDeclaration->findElementBySemantic(VES_BLEND_INDICES,0); |
446 | 0 | if ((pDeclWeights != NULL) && (pDeclIndexes != NULL)) |
447 | 0 | { |
448 | 0 | isValidData = true; |
449 | 0 | switch (pDeclWeights->getType()) |
450 | 0 | { |
451 | 0 | case VET_FLOAT1: |
452 | 0 | weightCount = 1; |
453 | 0 | break; |
454 | 0 | case VET_USHORT2_NORM: |
455 | 0 | case VET_FLOAT2: |
456 | 0 | weightCount = 2; |
457 | 0 | break; |
458 | 0 | case VET_FLOAT3: |
459 | 0 | weightCount = 3; |
460 | 0 | break; |
461 | 0 | case VET_USHORT4_NORM: |
462 | 0 | case VET_UBYTE4_NORM: |
463 | 0 | case VET_FLOAT4: |
464 | 0 | weightCount = 4; |
465 | 0 | break; |
466 | 0 | default: |
467 | 0 | isValidData = false; |
468 | 0 | break; |
469 | 0 | } |
470 | 0 | } |
471 | 0 | } |
472 | 0 | return isValidData; |
473 | 0 | } |
474 | | |
475 | | /** |
476 | | @brief |
477 | | Updates an entity's the skeleton data onto one of it's materials. |
478 | | |
479 | | @param pMaterial The material to update with the information. |
480 | | @param isValid Tells if the material can be used with HS. |
481 | | @param boneCount The maximum number of bones used by the entity. |
482 | | @param weightCount The maximum number of weights used by the entity. |
483 | | @return Returns true if the data was updated on the material. False if not. |
484 | | */ |
485 | | static bool imprintSkeletonData(const MaterialPtr& pMaterial, bool isVaild, |
486 | | ushort boneCount, ushort weightCount, SkinningType skinningType, bool correctAntidpodalityHandling, bool scalingShearingSupport) |
487 | 0 | { |
488 | 0 | bool isUpdated = false; |
489 | 0 | if (pMaterial->getNumTechniques() > 0) |
490 | 0 | { |
491 | 0 | HardwareSkinning::SkinningData data; |
492 | | |
493 | | //get the previous skinning data if available |
494 | 0 | UserObjectBindings& binding = pMaterial->getTechnique(0)->getUserObjectBindings(); |
495 | 0 | const Any& hsAny = binding.getUserAny(HS_DATA_BIND_NAME); |
496 | 0 | if (hsAny.has_value()) |
497 | 0 | { |
498 | 0 | data = any_cast<HardwareSkinning::SkinningData>(hsAny); |
499 | 0 | } |
500 | | |
501 | | //check if we need to update the data |
502 | 0 | if (((data.isValid == true) && (isVaild == false)) || |
503 | 0 | (data.maxBoneCount < boneCount) || |
504 | 0 | (data.maxWeightCount < weightCount)) |
505 | 0 | { |
506 | | //update the data |
507 | 0 | isUpdated = true; |
508 | 0 | data.isValid &= isVaild; |
509 | 0 | data.maxBoneCount = std::max<ushort>(data.maxBoneCount, boneCount); |
510 | 0 | data.maxWeightCount = std::max<ushort>(data.maxWeightCount, weightCount); |
511 | 0 | data.skinningType = skinningType; |
512 | 0 | data.correctAntipodalityHandling = correctAntidpodalityHandling; |
513 | 0 | data.scalingShearingSupport = scalingShearingSupport; |
514 | | |
515 | | //update the data in the material and invalidate it in the RTShader system |
516 | | //do it will be regenerated |
517 | 0 | binding.setUserAny(HS_DATA_BIND_NAME, data); |
518 | |
|
519 | 0 | size_t schemeCount = ShaderGenerator::getSingleton().getRTShaderSchemeCount(); |
520 | 0 | for(size_t i = 0 ; i < schemeCount ; ++i) |
521 | 0 | { |
522 | | //invalidate the material so it will be recreated with the correct |
523 | | //amount of bones and weights |
524 | 0 | const String& schemeName = ShaderGenerator::getSingleton().getRTShaderScheme(i); |
525 | 0 | ShaderGenerator::getSingleton().invalidateMaterial(schemeName, *pMaterial); |
526 | 0 | } |
527 | |
|
528 | 0 | } |
529 | 0 | } |
530 | 0 | return isUpdated; |
531 | |
|
532 | 0 | } |
533 | | |
534 | | void HardwareSkinningFactory::prepareEntityForSkinning(const Entity* pEntity, SkinningType skinningType, |
535 | | bool correctAntidpodalityHandling, bool shearScale) |
536 | 0 | { |
537 | | // This requires GLES3.0 |
538 | 0 | if (ShaderGenerator::getSingleton().getTargetLanguage() == "glsles" && |
539 | 0 | !GpuProgramManager::getSingleton().isSyntaxSupported("glsl300es")) |
540 | 0 | return; |
541 | | |
542 | 0 | if (pEntity != NULL) |
543 | 0 | { |
544 | 0 | size_t lodLevels = pEntity->getNumManualLodLevels() + 1; |
545 | 0 | for(size_t indexLod = 0 ; indexLod < lodLevels ; ++indexLod) |
546 | 0 | { |
547 | 0 | const Entity* pCurEntity = pEntity; |
548 | 0 | if (indexLod > 0) pCurEntity = pEntity->getManualLodLevel(indexLod - 1); |
549 | |
|
550 | 0 | size_t numSubEntities = pCurEntity->getNumSubEntities(); |
551 | 0 | for(size_t indexSub = 0 ; indexSub < numSubEntities ; ++indexSub) |
552 | 0 | { |
553 | 0 | ushort boneCount = 0,weightCount = 0; |
554 | 0 | bool isValid = extractSkeletonData(pCurEntity, indexSub, boneCount, weightCount); |
555 | |
|
556 | 0 | SubEntity* pSubEntity = pCurEntity->getSubEntity(indexSub); |
557 | 0 | const MaterialPtr& pMat = pSubEntity->getMaterial(); |
558 | 0 | imprintSkeletonData(pMat, isValid, boneCount, weightCount, skinningType, correctAntidpodalityHandling, shearScale); |
559 | 0 | } |
560 | 0 | } |
561 | 0 | } |
562 | 0 | } |
563 | | |
564 | | } |
565 | | } |
566 | | |
567 | | #endif |
568 | | |
569 | | |