MaterialParameter.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. #include "Base.h"
  2. #include "MaterialParameter.h"
  3. #include "Node.h"
  4. namespace gameplay
  5. {
  6. MaterialParameter::MaterialParameter(const char* name) :
  7. _type(MaterialParameter::NONE), _count(1), _dynamic(false), _name(name ? name : ""), _uniform(NULL)
  8. {
  9. clearValue();
  10. }
  11. MaterialParameter::~MaterialParameter()
  12. {
  13. clearValue();
  14. }
  15. void MaterialParameter::clearValue()
  16. {
  17. // Release parameters
  18. switch (_type)
  19. {
  20. case MaterialParameter::SAMPLER:
  21. if (_value.samplerValue)
  22. const_cast<Texture::Sampler*>(_value.samplerValue)->release();
  23. break;
  24. case MaterialParameter::SAMPLER_ARRAY:
  25. if (_value.samplerArrayValue)
  26. {
  27. for (unsigned int i = 0; i < _count; ++i)
  28. {
  29. const_cast<Texture::Sampler*>(_value.samplerArrayValue[i])->release();
  30. }
  31. }
  32. break;
  33. default:
  34. // Ignore all other cases.
  35. break;
  36. }
  37. // Free dynamic data
  38. if (_dynamic)
  39. {
  40. switch (_type)
  41. {
  42. case MaterialParameter::FLOAT:
  43. case MaterialParameter::FLOAT_ARRAY:
  44. case MaterialParameter::VECTOR2:
  45. case MaterialParameter::VECTOR3:
  46. case MaterialParameter::VECTOR4:
  47. case MaterialParameter::MATRIX:
  48. SAFE_DELETE_ARRAY(_value.floatPtrValue);
  49. break;
  50. case MaterialParameter::INT:
  51. case MaterialParameter::INT_ARRAY:
  52. SAFE_DELETE_ARRAY(_value.intPtrValue);
  53. break;
  54. case MaterialParameter::METHOD:
  55. SAFE_RELEASE(_value.method);
  56. break;
  57. case MaterialParameter::SAMPLER_ARRAY:
  58. SAFE_DELETE_ARRAY(_value.samplerArrayValue);
  59. break;
  60. default:
  61. // Ignore all other cases.
  62. break;
  63. }
  64. _dynamic = false;
  65. _count = 1;
  66. }
  67. memset(&_value, 0, sizeof(_value));
  68. _type = MaterialParameter::NONE;
  69. }
  70. const char* MaterialParameter::getName() const
  71. {
  72. return _name.c_str();
  73. }
  74. Texture::Sampler* MaterialParameter::getSampler(unsigned int index) const
  75. {
  76. if (_type == MaterialParameter::SAMPLER)
  77. return const_cast<Texture::Sampler*>(_value.samplerValue);
  78. if (_type == MaterialParameter::SAMPLER_ARRAY && index < _count)
  79. return const_cast<Texture::Sampler*>(_value.samplerArrayValue[index]);
  80. return NULL;
  81. }
  82. void MaterialParameter::setValue(float value)
  83. {
  84. clearValue();
  85. _value.floatValue = value;
  86. _type = MaterialParameter::FLOAT;
  87. }
  88. void MaterialParameter::setValue(int value)
  89. {
  90. clearValue();
  91. _value.intValue = value;
  92. _type = MaterialParameter::INT;
  93. }
  94. void MaterialParameter::setValue(const float* values, unsigned int count)
  95. {
  96. clearValue();
  97. _value.floatPtrValue = const_cast<float*> (values);
  98. _count = count;
  99. _type = MaterialParameter::FLOAT_ARRAY;
  100. }
  101. void MaterialParameter::setValue(const int* values, unsigned int count)
  102. {
  103. clearValue();
  104. _value.intPtrValue = const_cast<int*> (values);
  105. _count = count;
  106. _type = MaterialParameter::INT_ARRAY;
  107. }
  108. void MaterialParameter::setValue(const Vector2& value)
  109. {
  110. clearValue();
  111. // Copy data by-value into a dynamic array.
  112. float* array = new float[2];
  113. memcpy(array, &value.x, sizeof(float) * 2);
  114. _value.floatPtrValue = array;
  115. _dynamic = true;
  116. _count = 1;
  117. _type = MaterialParameter::VECTOR2;
  118. }
  119. void MaterialParameter::setValue(const Vector2* values, unsigned int count)
  120. {
  121. GP_ASSERT(values);
  122. clearValue();
  123. _value.floatPtrValue = const_cast<float*> (&values[0].x);
  124. _count = count;
  125. _type = MaterialParameter::VECTOR2;
  126. }
  127. void MaterialParameter::setValue(const Vector3& value)
  128. {
  129. clearValue();
  130. // Copy data by-value into a dynamic array.
  131. float* array = new float[3];
  132. memcpy(array, &value.x, sizeof(float) * 3);
  133. _value.floatPtrValue = array;
  134. _dynamic = true;
  135. _count = 1;
  136. _type = MaterialParameter::VECTOR3;
  137. }
  138. void MaterialParameter::setValue(const Vector3* values, unsigned int count)
  139. {
  140. GP_ASSERT(values);
  141. clearValue();
  142. _value.floatPtrValue = const_cast<float*> (&values[0].x);
  143. _count = count;
  144. _type = MaterialParameter::VECTOR3;
  145. }
  146. void MaterialParameter::setValue(const Vector4& value)
  147. {
  148. clearValue();
  149. // Copy data by-value into a dynamic array.
  150. float* array = new float[4];
  151. memcpy(array, &value.x, sizeof(float) * 4);
  152. _value.floatPtrValue = array;
  153. _dynamic = true;
  154. _count = 1;
  155. _type = MaterialParameter::VECTOR4;
  156. }
  157. void MaterialParameter::setValue(const Vector4* values, unsigned int count)
  158. {
  159. GP_ASSERT(values);
  160. clearValue();
  161. _value.floatPtrValue = const_cast<float*> (&values[0].x);
  162. _count = count;
  163. _type = MaterialParameter::VECTOR4;
  164. }
  165. void MaterialParameter::setValue(const Matrix& value)
  166. {
  167. // If this parameter is already storing a single dynamic matrix, no need to clear it.
  168. if (!(_dynamic && _count == 1 && _type == MaterialParameter::MATRIX && _value.floatPtrValue != NULL))
  169. {
  170. clearValue();
  171. // Allocate a new dynamic matrix.
  172. _value.floatPtrValue = new float[16];
  173. }
  174. memcpy(_value.floatPtrValue, value.m, sizeof(float) * 16);
  175. _dynamic = true;
  176. _count = 1;
  177. _type = MaterialParameter::MATRIX;
  178. }
  179. void MaterialParameter::setValue(const Matrix* values, unsigned int count)
  180. {
  181. GP_ASSERT(values);
  182. clearValue();
  183. _value.floatPtrValue = const_cast<Matrix&> (values[0]).m;
  184. _count = count;
  185. _type = MaterialParameter::MATRIX;
  186. }
  187. void MaterialParameter::setValue(const Texture::Sampler* sampler)
  188. {
  189. clearValue();
  190. if (sampler)
  191. {
  192. const_cast<Texture::Sampler*>(sampler)->addRef();
  193. _value.samplerValue = sampler;
  194. _type = MaterialParameter::SAMPLER;
  195. }
  196. }
  197. void MaterialParameter::setValue(const Texture::Sampler** samplers, unsigned int count)
  198. {
  199. clearValue();
  200. if (samplers)
  201. {
  202. for (unsigned int i = 0; i < count; ++i)
  203. {
  204. const_cast<Texture::Sampler*>(samplers[i])->addRef();
  205. }
  206. _value.samplerArrayValue = samplers;
  207. _count = count;
  208. _type = MaterialParameter::SAMPLER_ARRAY;
  209. }
  210. }
  211. Texture::Sampler* MaterialParameter::setValue(const char* texturePath, bool generateMipmaps)
  212. {
  213. if (texturePath)
  214. {
  215. clearValue();
  216. Texture::Sampler* sampler = Texture::Sampler::create(texturePath, generateMipmaps);
  217. if (sampler)
  218. {
  219. _value.samplerValue = sampler;
  220. _type = MaterialParameter::SAMPLER;
  221. }
  222. return sampler;
  223. }
  224. return NULL;
  225. }
  226. void MaterialParameter::bind(Effect* effect)
  227. {
  228. GP_ASSERT(effect);
  229. // If we had a Uniform cached that is not from the passed in effect,
  230. // we need to update our uniform to point to the new effect's uniform.
  231. if (!_uniform || _uniform->getEffect() != effect)
  232. {
  233. _uniform = effect->getUniform(_name.c_str());
  234. if (!_uniform)
  235. {
  236. // This parameter was not found in the specified effect, so do nothing.
  237. GP_WARN("Warning: Material parameter '%s' not found in effect '%s'.", _name.c_str(), effect->getId());
  238. return;
  239. }
  240. }
  241. switch (_type)
  242. {
  243. case MaterialParameter::FLOAT:
  244. effect->setValue(_uniform, _value.floatValue);
  245. break;
  246. case MaterialParameter::FLOAT_ARRAY:
  247. effect->setValue(_uniform, _value.floatPtrValue, _count);
  248. break;
  249. case MaterialParameter::INT:
  250. effect->setValue(_uniform, _value.intValue);
  251. break;
  252. case MaterialParameter::INT_ARRAY:
  253. effect->setValue(_uniform, _value.intPtrValue, _count);
  254. break;
  255. case MaterialParameter::VECTOR2:
  256. effect->setValue(_uniform, reinterpret_cast<Vector2*>(_value.floatPtrValue), _count);
  257. break;
  258. case MaterialParameter::VECTOR3:
  259. effect->setValue(_uniform, reinterpret_cast<Vector3*>(_value.floatPtrValue), _count);
  260. break;
  261. case MaterialParameter::VECTOR4:
  262. effect->setValue(_uniform, reinterpret_cast<Vector4*>(_value.floatPtrValue), _count);
  263. break;
  264. case MaterialParameter::MATRIX:
  265. effect->setValue(_uniform, reinterpret_cast<Matrix*>(_value.floatPtrValue), _count);
  266. break;
  267. case MaterialParameter::SAMPLER:
  268. effect->setValue(_uniform, _value.samplerValue);
  269. break;
  270. case MaterialParameter::SAMPLER_ARRAY:
  271. effect->setValue(_uniform, _value.samplerArrayValue, _count);
  272. break;
  273. case MaterialParameter::METHOD:
  274. GP_ASSERT(_value.method);
  275. _value.method->setValue(effect);
  276. break;
  277. default:
  278. GP_ERROR("Unsupported material parameter type (%d).", _type);
  279. break;
  280. }
  281. }
  282. void MaterialParameter::bindValue(Node* node, const char* binding)
  283. {
  284. GP_ASSERT(binding);
  285. if (strcmp(binding, "&Node::getBackVector") == 0)
  286. {
  287. bindValue<Node, Vector3>(node, &Node::getBackVector);
  288. }
  289. else if (strcmp(binding, "&Node::getDownVector") == 0)
  290. {
  291. bindValue<Node, Vector3>(node, &Node::getDownVector);
  292. }
  293. else if (strcmp(binding, "&Node::getTranslationWorld") == 0)
  294. {
  295. bindValue<Node, Vector3>(node, &Node::getTranslationWorld);
  296. }
  297. else if (strcmp(binding, "&Node::getTranslationView") == 0)
  298. {
  299. bindValue<Node, Vector3>(node, &Node::getTranslationView);
  300. }
  301. else if (strcmp(binding, "&Node::getForwardVector") == 0)
  302. {
  303. bindValue<Node, Vector3>(node, &Node::getForwardVector);
  304. }
  305. else if (strcmp(binding, "&Node::getForwardVectorWorld") == 0)
  306. {
  307. bindValue<Node, Vector3>(node, &Node::getForwardVectorWorld);
  308. }
  309. else if (strcmp(binding, "&Node::getForwardVectorView") == 0)
  310. {
  311. bindValue<Node, Vector3>(node, &Node::getForwardVectorView);
  312. }
  313. else if (strcmp(binding, "&Node::getLeftVector") == 0)
  314. {
  315. bindValue<Node, Vector3>(node, &Node::getLeftVector);
  316. }
  317. else if (strcmp(binding, "&Node::getRightVector") == 0)
  318. {
  319. bindValue<Node, Vector3>(node, &Node::getRightVector);
  320. }
  321. else if (strcmp(binding, "&Node::getRightVectorWorld") == 0)
  322. {
  323. bindValue<Node, Vector3>(node, &Node::getRightVectorWorld);
  324. }
  325. else if (strcmp(binding, "&Node::getUpVector") == 0)
  326. {
  327. bindValue<Node, Vector3>(node, &Node::getUpVector);
  328. }
  329. else if (strcmp(binding, "&Node::getUpVectorWorld") == 0)
  330. {
  331. bindValue<Node, Vector3>(node, &Node::getUpVectorWorld);
  332. }
  333. else if (strcmp(binding, "&Node::getActiveCameraTranslationWorld") == 0)
  334. {
  335. bindValue<Node, Vector3>(node, &Node::getActiveCameraTranslationWorld);
  336. }
  337. else if (strcmp(binding, "&Node::getActiveCameraTranslationView") == 0)
  338. {
  339. bindValue<Node, Vector3>(node, &Node::getActiveCameraTranslationView);
  340. }
  341. else if (strcmp(binding, "&Node::getScaleX") == 0)
  342. {
  343. bindValue<Node, float>(node, &Node::getScaleX);
  344. }
  345. else if (strcmp(binding, "&Node::getScaleY") == 0)
  346. {
  347. bindValue<Node, float>(node, &Node::getScaleY);
  348. }
  349. else if (strcmp(binding, "&Node::getScaleZ") == 0)
  350. {
  351. bindValue<Node, float>(node, &Node::getScaleZ);
  352. }
  353. else if (strcmp(binding, "&Node::getTranslationX") == 0)
  354. {
  355. bindValue<Node, float>(node, &Node::getTranslationX);
  356. }
  357. else if (strcmp(binding, "&Node::getTranslationY") == 0)
  358. {
  359. bindValue<Node, float>(node, &Node::getTranslationY);
  360. }
  361. else if (strcmp(binding, "&Node::getTranslationZ") == 0)
  362. {
  363. bindValue<Node, float>(node, &Node::getTranslationZ);
  364. }
  365. else
  366. {
  367. GP_ERROR("Unsupported material parameter binding '%s'.", binding);
  368. }
  369. }
  370. unsigned int MaterialParameter::getAnimationPropertyComponentCount(int propertyId) const
  371. {
  372. switch (propertyId)
  373. {
  374. case ANIMATE_UNIFORM:
  375. {
  376. switch (_type)
  377. {
  378. // These types don't support animation.
  379. case NONE:
  380. case MATRIX:
  381. case SAMPLER:
  382. case SAMPLER_ARRAY:
  383. case METHOD:
  384. return 0;
  385. case FLOAT:
  386. case FLOAT_ARRAY:
  387. case INT:
  388. case INT_ARRAY:
  389. return _count;
  390. case VECTOR2:
  391. return 2 * _count;
  392. case VECTOR3:
  393. return 3 * _count;
  394. case VECTOR4:
  395. return 4 * _count;
  396. default:
  397. GP_ERROR("Unsupported material parameter type (%d).", _type);
  398. return 0;
  399. }
  400. }
  401. break;
  402. }
  403. return 0;
  404. }
  405. void MaterialParameter::getAnimationPropertyValue(int propertyId, AnimationValue* value)
  406. {
  407. GP_ASSERT(value);
  408. switch (propertyId)
  409. {
  410. case ANIMATE_UNIFORM:
  411. {
  412. switch (_type)
  413. {
  414. case FLOAT:
  415. value->setFloat(0, _value.floatValue);
  416. break;
  417. case FLOAT_ARRAY:
  418. GP_ASSERT(_value.floatPtrValue);
  419. for (unsigned int i = 0; i < _count; i++)
  420. {
  421. value->setFloat(i, _value.floatPtrValue[i]);
  422. }
  423. break;
  424. case INT:
  425. value->setFloat(0, _value.intValue);
  426. break;
  427. case INT_ARRAY:
  428. GP_ASSERT(_value.intPtrValue);
  429. for (unsigned int i = 0; i < _count; i++)
  430. {
  431. value->setFloat(i, _value.intPtrValue[i]);
  432. }
  433. break;
  434. case VECTOR2:
  435. value->setFloats(0, _value.floatPtrValue, _count * 2);
  436. break;
  437. case VECTOR3:
  438. value->setFloats(0, _value.floatPtrValue, _count * 3);
  439. break;
  440. case VECTOR4:
  441. value->setFloats(0, _value.floatPtrValue, _count * 4);
  442. break;
  443. case NONE:
  444. case MATRIX:
  445. case METHOD:
  446. case SAMPLER:
  447. case SAMPLER_ARRAY:
  448. // Unsupported material parameter types for animation.
  449. break;
  450. default:
  451. GP_ERROR("Unsupported material parameter type (%d).", _type);
  452. break;
  453. }
  454. }
  455. break;
  456. }
  457. }
  458. void MaterialParameter::setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight)
  459. {
  460. GP_ASSERT(value);
  461. GP_ASSERT(blendWeight >= 0.0f && blendWeight <= 1.0f);
  462. switch (propertyId)
  463. {
  464. case ANIMATE_UNIFORM:
  465. {
  466. switch (_type)
  467. {
  468. case FLOAT:
  469. _value.floatValue = Curve::lerp(blendWeight, _value.floatValue, value->getFloat(0));
  470. break;
  471. case FLOAT_ARRAY:
  472. applyAnimationValue(value, blendWeight, 1);
  473. break;
  474. case INT:
  475. _value.intValue = Curve::lerp(blendWeight, _value.intValue, value->getFloat(0));
  476. break;
  477. case INT_ARRAY:
  478. GP_ASSERT(_value.intPtrValue);
  479. for (unsigned int i = 0; i < _count; i++)
  480. _value.intPtrValue[i] = Curve::lerp(blendWeight, _value.intPtrValue[i], value->getFloat(i));
  481. break;
  482. case VECTOR2:
  483. applyAnimationValue(value, blendWeight, 2);
  484. break;
  485. case VECTOR3:
  486. applyAnimationValue(value, blendWeight, 3);
  487. break;
  488. case VECTOR4:
  489. applyAnimationValue(value, blendWeight, 4);
  490. break;
  491. case NONE:
  492. case MATRIX:
  493. case METHOD:
  494. case SAMPLER:
  495. case SAMPLER_ARRAY:
  496. // Unsupported material parameter types for animation.
  497. break;
  498. default:
  499. GP_ERROR("Unsupported material parameter type (%d).", _type);
  500. break;
  501. }
  502. }
  503. break;
  504. }
  505. }
  506. void MaterialParameter::applyAnimationValue(AnimationValue* value, float blendWeight, int components)
  507. {
  508. GP_ASSERT(value);
  509. GP_ASSERT(_value.floatPtrValue);
  510. unsigned int count = _count * components;
  511. for (unsigned int i = 0; i < count; i++)
  512. _value.floatPtrValue[i] = Curve::lerp(blendWeight, _value.floatPtrValue[i], value->getFloat(i));
  513. }
  514. void MaterialParameter::cloneInto(MaterialParameter* materialParameter) const
  515. {
  516. GP_ASSERT(materialParameter);
  517. materialParameter->_type = _type;
  518. materialParameter->_count = _count;
  519. materialParameter->_dynamic = _dynamic;
  520. materialParameter->_uniform = _uniform;
  521. switch (_type)
  522. {
  523. case NONE:
  524. break;
  525. case FLOAT:
  526. materialParameter->setValue(_value.floatValue);
  527. break;
  528. case FLOAT_ARRAY:
  529. materialParameter->setValue(_value.floatPtrValue, _count);
  530. break;
  531. case INT:
  532. materialParameter->setValue(_value.intValue);
  533. break;
  534. case INT_ARRAY:
  535. materialParameter->setValue(_value.intPtrValue, _count);
  536. break;
  537. case VECTOR2:
  538. {
  539. Vector2* value = reinterpret_cast<Vector2*>(_value.floatPtrValue);
  540. if (_count == 1)
  541. {
  542. GP_ASSERT(value);
  543. materialParameter->setValue(*value);
  544. }
  545. else
  546. {
  547. materialParameter->setValue(value, _count);
  548. }
  549. break;
  550. }
  551. case VECTOR3:
  552. {
  553. Vector3* value = reinterpret_cast<Vector3*>(_value.floatPtrValue);
  554. if (_count == 1)
  555. {
  556. GP_ASSERT(value);
  557. materialParameter->setValue(*value);
  558. }
  559. else
  560. {
  561. materialParameter->setValue(value, _count);
  562. }
  563. break;
  564. }
  565. case VECTOR4:
  566. {
  567. Vector4* value = reinterpret_cast<Vector4*>(_value.floatPtrValue);
  568. if (_count == 1)
  569. {
  570. GP_ASSERT(value);
  571. materialParameter->setValue(*value);
  572. }
  573. else
  574. {
  575. materialParameter->setValue(value, _count);
  576. }
  577. break;
  578. }
  579. case MATRIX:
  580. {
  581. Matrix* value = reinterpret_cast<Matrix*>(_value.floatPtrValue);
  582. if (_count == 1)
  583. {
  584. GP_ASSERT(value);
  585. materialParameter->setValue(*value);
  586. }
  587. else
  588. {
  589. materialParameter->setValue(value, _count);
  590. }
  591. break;
  592. }
  593. case SAMPLER:
  594. materialParameter->setValue(_value.samplerValue);
  595. break;
  596. case SAMPLER_ARRAY:
  597. materialParameter->setValue(_value.samplerArrayValue, _count);
  598. break;
  599. case METHOD:
  600. materialParameter->_value.method = _value.method;
  601. GP_ASSERT(materialParameter->_value.method);
  602. materialParameter->_value.method->addRef();
  603. break;
  604. default:
  605. GP_ERROR("Unsupported material parameter type(%d).", _type);
  606. break;
  607. }
  608. NodeCloneContext context;
  609. this->AnimationTarget::cloneInto(materialParameter, context);
  610. }
  611. }