Pass.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "Base.h"
  2. #include "Pass.h"
  3. #include "Technique.h"
  4. #include "Material.h"
  5. #include "Node.h"
  6. namespace gameplay
  7. {
  8. Pass::Pass(const char* id, Technique* technique, Effect* effect) :
  9. _id(id ? id : ""), _technique(technique), _effect(effect), _vaBinding(NULL)
  10. {
  11. RenderState::_parent = _technique;
  12. }
  13. Pass::~Pass()
  14. {
  15. SAFE_RELEASE(_effect);
  16. SAFE_RELEASE(_vaBinding);
  17. }
  18. Pass* Pass::create(const char* id, Technique* technique, const char* vshPath, const char* fshPath, const char* defines)
  19. {
  20. // Attempt to create/load the effect.
  21. Effect* effect = Effect::createFromFile(vshPath, fshPath, defines);
  22. if (effect == NULL)
  23. {
  24. GP_ERROR("Failed to create effect for pass.");
  25. return NULL;
  26. }
  27. // Return the new pass.
  28. return new Pass(id, technique, effect);
  29. }
  30. const char* Pass::getId() const
  31. {
  32. return _id.c_str();
  33. }
  34. Effect* Pass::getEffect() const
  35. {
  36. return _effect;
  37. }
  38. void Pass::setVertexAttributeBinding(VertexAttributeBinding* binding)
  39. {
  40. SAFE_RELEASE(_vaBinding);
  41. if (binding)
  42. {
  43. _vaBinding = binding;
  44. binding->addRef();
  45. }
  46. }
  47. void Pass::bind()
  48. {
  49. GP_ASSERT(_effect);
  50. // Bind our effect.
  51. _effect->bind();
  52. // Bind our render state
  53. RenderState::bind(this);
  54. // If we have a vertex attribute binding, bind it
  55. if (_vaBinding)
  56. {
  57. _vaBinding->bind();
  58. }
  59. }
  60. void Pass::unbind()
  61. {
  62. // If we have a vertex attribute binding, unbind it
  63. if (_vaBinding)
  64. {
  65. _vaBinding->unbind();
  66. }
  67. }
  68. Pass* Pass::clone(Technique* technique, NodeCloneContext &context) const
  69. {
  70. Effect* effect = getEffect();
  71. GP_ASSERT(effect);
  72. effect->addRef();
  73. Pass* pass = new Pass(getId(), technique, effect);
  74. RenderState::cloneInto(pass, context);
  75. return pass;
  76. }
  77. }