CmShader.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. TechniquePtr Shader::addTechnique(const String& renderSystem, const String& renderer)
  12. {
  13. TechniquePtr technique = TechniquePtr(new Technique(renderSystem, renderer));
  14. mTechniques.push_back(technique);
  15. return technique;
  16. }
  17. void Shader::removeTechnique(UINT32 idx)
  18. {
  19. if(idx < 0 || idx >= mTechniques.size())
  20. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx));
  21. int count = 0;
  22. auto iter = mTechniques.begin();
  23. while(count != idx)
  24. {
  25. ++count;
  26. ++iter;
  27. }
  28. mTechniques.erase(iter);
  29. }
  30. void Shader::removeTechnique(TechniquePtr technique)
  31. {
  32. auto iterFind = std::find(mTechniques.begin(), mTechniques.end(), technique);
  33. if(iterFind == mTechniques.end())
  34. CM_EXCEPT(InvalidParametersException, "Cannot remove specified technique because it wasn't found in this shader.");
  35. mTechniques.erase(iterFind);
  36. }
  37. TechniquePtr Shader::getBestTechnique() const
  38. {
  39. for(auto iter = mTechniques.begin(); iter != mTechniques.end(); ++iter)
  40. {
  41. if((*iter)->isSupported())
  42. {
  43. return *iter;
  44. }
  45. }
  46. CM_EXCEPT(InternalErrorException, "No techniques are supported!");
  47. // TODO - Low priority. Instead of throwing an exception use an extremely simple technique that will be supported almost everywhere as a fallback.
  48. }
  49. }