MaterialRuntime.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "anki/scene/MaterialRuntime.h"
  2. #include "anki/resource/Material.h"
  3. #include "anki/resource/Texture.h"
  4. #include "anki/resource/Resource.h"
  5. #include "anki/resource/ShaderProgram.h"
  6. #include <boost/foreach.hpp>
  7. namespace anki {
  8. //==============================================================================
  9. // MaterialRuntimeVariable =
  10. //==============================================================================
  11. //==============================================================================
  12. template <>
  13. void MaterialRuntimeVariable::ConstructVisitor::
  14. operator()<TextureResourcePointer >(const TextureResourcePointer& x) const
  15. {
  16. var.data = &x;
  17. }
  18. //==============================================================================
  19. MaterialRuntimeVariable::MaterialRuntimeVariable(
  20. const MaterialVariable& mvar_)
  21. : mvar(mvar_), buildinId(-1)
  22. {
  23. // Initialize the data using a visitor
  24. boost::apply_visitor(ConstructVisitor(*this), mvar.getVariant());
  25. }
  26. //==============================================================================
  27. MaterialRuntimeVariable::~MaterialRuntimeVariable()
  28. {}
  29. //==============================================================================
  30. template<>
  31. MaterialRuntimeVariable::ConstPtrRsrcPtrTexture&
  32. MaterialRuntimeVariable::getValue<
  33. MaterialRuntimeVariable::ConstPtrRsrcPtrTexture>()
  34. {
  35. throw ANKI_EXCEPTION("You shouldn't call this getter");
  36. return boost::get<ConstPtrRsrcPtrTexture>(data);
  37. }
  38. //==============================================================================
  39. template<>
  40. void MaterialRuntimeVariable::setValue<
  41. MaterialRuntimeVariable::ConstPtrRsrcPtrTexture>(
  42. const ConstPtrRsrcPtrTexture& v)
  43. {
  44. throw ANKI_EXCEPTION("You shouldn't call this setter");
  45. boost::get<ConstPtrRsrcPtrTexture>(data) = v;
  46. }
  47. //==============================================================================
  48. template<typename Type>
  49. void MaterialRuntimeVariable::SetUniformVisitor::operator()(
  50. const Type& x) const
  51. {
  52. uni.set(x);
  53. }
  54. //==============================================================================
  55. template<>
  56. void MaterialRuntimeVariable::SetUniformVisitor::
  57. operator()<MaterialRuntimeVariable::ConstPtrRsrcPtrTexture>(
  58. const ConstPtrRsrcPtrTexture& x) const
  59. {
  60. uni.set(*(x->get()), texUnit);
  61. ++texUnit;
  62. }
  63. //==============================================================================
  64. void MaterialRuntimeVariable::setUniformVariable(const PassLevelKey& k,
  65. uint& texUnit)
  66. {
  67. const ShaderProgramUniformVariable& uni =
  68. mvar.getShaderProgramUniformVariable(k);
  69. boost::apply_visitor(SetUniformVisitor(uni, texUnit), data);
  70. }
  71. //==============================================================================
  72. // MaterialRuntime =
  73. //==============================================================================
  74. //==============================================================================
  75. MaterialRuntime::MaterialRuntime(const Material& mtl_)
  76. : mtl(mtl_)
  77. {
  78. // Copy props
  79. MaterialProperties& me = *this;
  80. const MaterialProperties& he = mtl.getBaseClass();
  81. me = he;
  82. // Create vars
  83. BOOST_FOREACH(const MaterialVariable& var, mtl.getVariables())
  84. {
  85. MaterialRuntimeVariable* varr = new MaterialRuntimeVariable(var);
  86. vars.push_back(varr);
  87. varNameToVar[varr->getMaterialVariable().getName().c_str()] = varr;
  88. }
  89. }
  90. //==============================================================================
  91. MaterialRuntime::~MaterialRuntime()
  92. {}
  93. //==============================================================================
  94. MaterialRuntimeVariable& MaterialRuntime::findVariableByName(
  95. const char* name)
  96. {
  97. VariablesHashMap::iterator it = varNameToVar.find(name);
  98. if(it == varNameToVar.end())
  99. {
  100. throw ANKI_EXCEPTION("Cannot get material runtime variable "
  101. "with name \"" + name + '\"');
  102. }
  103. return *(it->second);
  104. }
  105. //==============================================================================
  106. const MaterialRuntimeVariable& MaterialRuntime::findVariableByName(
  107. const char* name) const
  108. {
  109. VariablesHashMap::const_iterator it = varNameToVar.find(name);
  110. if(it == varNameToVar.end())
  111. {
  112. throw ANKI_EXCEPTION("Cannot get material runtime variable "
  113. "with name \"" + name + '\"');
  114. }
  115. return *(it->second);
  116. }
  117. } // end namespace