Pass.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. VertexAttributeBinding* Pass::getVertexAttributeBinding() const
  48. {
  49. return _vaBinding;
  50. }
  51. void Pass::bind()
  52. {
  53. GP_ASSERT(_effect);
  54. // Bind our effect.
  55. _effect->bind();
  56. // Bind our render state
  57. RenderState::bind(this);
  58. // If we have a vertex attribute binding, bind it
  59. if (_vaBinding)
  60. {
  61. _vaBinding->bind();
  62. }
  63. }
  64. void Pass::unbind()
  65. {
  66. // If we have a vertex attribute binding, unbind it
  67. if (_vaBinding)
  68. {
  69. _vaBinding->unbind();
  70. }
  71. }
  72. Pass* Pass::clone(Technique* technique, NodeCloneContext &context) const
  73. {
  74. Effect* effect = getEffect();
  75. GP_ASSERT(effect);
  76. effect->addRef();
  77. Pass* pass = new Pass(getId(), technique, effect);
  78. RenderState::cloneInto(pass, context);
  79. return pass;
  80. }
  81. }