CmPass.cpp 6.8 KB

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