CmPass.cpp 6.9 KB

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