MaterialParameter.cpp 16 KB

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