CmShader.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "CmShader.h"
  2. #include "CmTechnique.h"
  3. #include "CmException.h"
  4. #include "CmDebug.h"
  5. namespace CamelotEngine
  6. {
  7. Shader::Shader(const String& name)
  8. :mName(name)
  9. {
  10. }
  11. void Shader::addTechnique(TechniquePtr technique)
  12. {
  13. auto iterFind = std::find(mTechniques.begin(), mTechniques.end(), technique);
  14. if(iterFind != mTechniques.end())
  15. CM_EXCEPT(InvalidParametersException, "Identical technique already exists in this shader.");
  16. mTechniques.push_back(technique);
  17. }
  18. void Shader::removeTechnique(UINT32 idx)
  19. {
  20. if(idx < 0 || idx >= mTechniques.size())
  21. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx));
  22. int count = 0;
  23. auto iter = mTechniques.begin();
  24. while(count != idx)
  25. {
  26. ++count;
  27. ++iter;
  28. }
  29. mTechniques.erase(iter);
  30. }
  31. void Shader::removeTechnique(TechniquePtr technique)
  32. {
  33. auto iterFind = std::find(mTechniques.begin(), mTechniques.end(), technique);
  34. if(iterFind == mTechniques.end())
  35. CM_EXCEPT(InvalidParametersException, "Cannot remove specified technique because it wasn't found in this shader.");
  36. mTechniques.erase(iterFind);
  37. }
  38. TechniquePtr Shader::getBestTechnique() const
  39. {
  40. for(auto iter = mTechniques.begin(); iter != mTechniques.end(); ++iter)
  41. {
  42. if((*iter)->isSupported())
  43. {
  44. return *iter;
  45. }
  46. }
  47. CM_EXCEPT(InternalErrorException, "No techniques are supported!");
  48. // TODO - Low priority. Instead of throwing an exception use an extremely simple technique that will be supported almost everywhere as a fallback.
  49. }
  50. }