CmPass.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "CmPass.h"
  2. #include "CmRasterizerState.h"
  3. #include "CmBlendState.h"
  4. #include "CmDepthStencilState.h"
  5. #include "CmPassRTTI.h"
  6. #include "CmMaterial.h"
  7. #include "CmGpuParams.h"
  8. #include "CmException.h"
  9. namespace CamelotFramework
  10. {
  11. Pass::Pass()
  12. :mStencilRefValue(0)
  13. { }
  14. Pass::Pass(const Pass& oth)
  15. {
  16. *this = oth;
  17. }
  18. Pass::~Pass()
  19. {
  20. }
  21. Pass& Pass::operator=(const Pass& oth)
  22. {
  23. // Default blending (overwrite)
  24. mBlendState = oth.mBlendState;
  25. mRasterizerState = oth.mRasterizerState;
  26. mDepthStencilState = oth.mDepthStencilState;
  27. mStencilRefValue = oth.mStencilRefValue;
  28. mVertexProgram = oth.mVertexProgram;
  29. mFragmentProgram = oth.mFragmentProgram;
  30. mGeometryProgram = oth.mGeometryProgram;
  31. mHullProgram = oth.mHullProgram;
  32. mDomainProgram = oth.mDomainProgram;
  33. mComputeProgram = oth.mComputeProgram;
  34. return *this;
  35. }
  36. bool Pass::isTransparent(void) const
  37. {
  38. bool transparent = false;
  39. if(mBlendState != nullptr)
  40. {
  41. for(UINT32 i = 0; i < CM_MAX_MULTIPLE_RENDER_TARGETS; i++)
  42. {
  43. // Transparent if destination color is taken into account
  44. if (mBlendState->getDstBlend(i) != BF_ZERO ||
  45. mBlendState->getSrcBlend(i) == BF_DEST_COLOR ||
  46. mBlendState->getSrcBlend(i) == BF_INV_DEST_COLOR ||
  47. mBlendState->getSrcBlend(i) == BF_DEST_ALPHA ||
  48. mBlendState->getSrcBlend(i) == BF_INV_DEST_ALPHA)
  49. {
  50. transparent = true;
  51. }
  52. }
  53. }
  54. return transparent;
  55. }
  56. void Pass::setBlendState(HBlendState& blendState)
  57. {
  58. mBlendState = blendState;
  59. }
  60. HBlendState Pass::getBlendState() const
  61. {
  62. return mBlendState;
  63. }
  64. void Pass::setRasterizerState(HRasterizerState& rasterizerState)
  65. {
  66. mRasterizerState = rasterizerState;
  67. }
  68. HRasterizerState Pass::getRasterizerState() const
  69. {
  70. return mRasterizerState;
  71. }
  72. void Pass::setDepthStencilState(HDepthStencilState& depthStencilState)
  73. {
  74. mDepthStencilState = depthStencilState;
  75. }
  76. HDepthStencilState Pass::getDepthStencilState() const
  77. {
  78. return mDepthStencilState;
  79. }
  80. void Pass::setStencilRefValue(UINT32 refValue)
  81. {
  82. mStencilRefValue = refValue;
  83. }
  84. UINT32 Pass::getStencilRefValue() const
  85. {
  86. return mStencilRefValue;
  87. }
  88. void Pass::activate(CoreAccessor& coreAccessor) const
  89. {
  90. HGpuProgram vertProgram = getVertexProgram();
  91. if(vertProgram)
  92. coreAccessor.bindGpuProgram(vertProgram);
  93. else
  94. coreAccessor.unbindGpuProgram(GPT_VERTEX_PROGRAM);
  95. HGpuProgram fragProgram = getFragmentProgram();
  96. if(fragProgram)
  97. coreAccessor.bindGpuProgram(fragProgram);
  98. else
  99. coreAccessor.unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
  100. HGpuProgram geomProgram = getGeometryProgram();
  101. if(geomProgram)
  102. coreAccessor.bindGpuProgram(geomProgram);
  103. else
  104. coreAccessor.unbindGpuProgram(GPT_GEOMETRY_PROGRAM);
  105. HGpuProgram hullProgram = getHullProgram();
  106. if(hullProgram)
  107. coreAccessor.bindGpuProgram(hullProgram);
  108. else
  109. coreAccessor.unbindGpuProgram(GPT_HULL_PROGRAM);
  110. HGpuProgram domainProgram = getDomainProgram();
  111. if(domainProgram)
  112. coreAccessor.bindGpuProgram(domainProgram);
  113. else
  114. coreAccessor.unbindGpuProgram(GPT_DOMAIN_PROGRAM);
  115. // TODO - Try to limit amount of state changes, if previous state is already the same (especially with textures)
  116. // TODO: Disable remaining texture units
  117. //renderSystem->_disableTextureUnitsFrom(pass->getNumTextures());
  118. // Set up non-texture related pass settings
  119. HBlendState blendState = getBlendState();
  120. if(blendState != nullptr)
  121. coreAccessor.setBlendState(blendState.getInternalPtr());
  122. else
  123. coreAccessor.setBlendState(BlendState::getDefault());
  124. HDepthStencilState depthStancilState = getDepthStencilState();
  125. if(depthStancilState != nullptr)
  126. coreAccessor.setDepthStencilState(depthStancilState.getInternalPtr(), getStencilRefValue());
  127. else
  128. coreAccessor.setDepthStencilState(DepthStencilState::getDefault(), getStencilRefValue());
  129. HRasterizerState rasterizerState = getRasterizerState();
  130. if(rasterizerState != nullptr)
  131. coreAccessor.setRasterizerState(rasterizerState.getInternalPtr());
  132. else
  133. coreAccessor.setRasterizerState(RasterizerState::getDefault());
  134. }
  135. void Pass::bindParameters(CoreAccessor& coreAccessor, const PassParametersPtr& params) const
  136. {
  137. HGpuProgram vertProgram = getVertexProgram();
  138. if(vertProgram)
  139. coreAccessor.bindGpuParams(GPT_VERTEX_PROGRAM, params->mVertParams);
  140. HGpuProgram fragProgram = getFragmentProgram();
  141. if(fragProgram)
  142. coreAccessor.bindGpuParams(GPT_FRAGMENT_PROGRAM, params->mFragParams);
  143. HGpuProgram geomProgram = getGeometryProgram();
  144. if(geomProgram)
  145. coreAccessor.bindGpuParams(GPT_GEOMETRY_PROGRAM, params->mGeomParams);
  146. HGpuProgram hullProgram = getHullProgram();
  147. if(hullProgram)
  148. coreAccessor.bindGpuParams(GPT_HULL_PROGRAM, params->mHullParams);
  149. HGpuProgram domainProgram = getDomainProgram();
  150. if(domainProgram)
  151. coreAccessor.bindGpuParams(GPT_DOMAIN_PROGRAM, params->mDomainParams);
  152. HGpuProgram computeProgram = getComputeProgram();
  153. if(computeProgram)
  154. coreAccessor.bindGpuParams(GPT_COMPUTE_PROGRAM, params->mComputeParams);
  155. }
  156. RTTITypeBase* Pass::getRTTIStatic()
  157. {
  158. return PassRTTI::instance();
  159. }
  160. RTTITypeBase* Pass::getRTTI() const
  161. {
  162. return Pass::getRTTIStatic();
  163. }
  164. }