BsRenderSystem.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "BsRenderSystem.h"
  2. #include "BsCoreThread.h"
  3. #include "BsViewport.h"
  4. #include "BsException.h"
  5. #include "BsRenderTarget.h"
  6. #include "BsRenderWindow.h"
  7. #include "BsPixelBuffer.h"
  8. #include "BsOcclusionQuery.h"
  9. #include "BsResource.h"
  10. #include "BsCoreThread.h"
  11. #include "BsMesh.h"
  12. #include "BsProfilerCPU.h"
  13. #include "BsRenderStats.h"
  14. using namespace std::placeholders;
  15. namespace BansheeEngine
  16. {
  17. static const SPtr<TextureCore> sNullTexPtr;
  18. RenderSystem::RenderSystem()
  19. : mCullingMode(CULL_COUNTERCLOCKWISE)
  20. , mDisabledTexUnitsFrom(0)
  21. , mVertexProgramBound(false)
  22. , mGeometryProgramBound(false)
  23. , mFragmentProgramBound(false)
  24. , mDomainProgramBound(false)
  25. , mHullProgramBound(false)
  26. , mComputeProgramBound(false)
  27. , mClipPlanesDirty(true)
  28. , mCurrentCapabilities(nullptr)
  29. {
  30. }
  31. RenderSystem::~RenderSystem()
  32. {
  33. // Base classes need to call virtual destroy_internal method instead of a destructor
  34. bs_delete(mCurrentCapabilities);
  35. mCurrentCapabilities = nullptr;
  36. }
  37. RenderWindowPtr RenderSystem::initialize(const RENDER_WINDOW_DESC& primaryWindowDesc)
  38. {
  39. gCoreThread().queueCommand(std::bind(&RenderSystem::initializePrepare, this), true);
  40. RENDER_WINDOW_DESC windowDesc = primaryWindowDesc;
  41. RenderWindowPtr renderWindow = RenderWindow::create(windowDesc, nullptr);
  42. gCoreThread().queueCommand(std::bind(&RenderSystem::initializeFinalize, this, renderWindow->getCore()), true);
  43. return renderWindow;
  44. }
  45. void RenderSystem::initializePrepare()
  46. {
  47. // Do nothing
  48. }
  49. void RenderSystem::initializeFinalize(const SPtr<RenderWindowCore>& primaryWindow)
  50. {
  51. THROW_IF_NOT_CORE_THREAD;
  52. mVertexProgramBound = false;
  53. mGeometryProgramBound = false;
  54. mFragmentProgramBound = false;
  55. mDomainProgramBound = false;
  56. mHullProgramBound = false;
  57. mComputeProgramBound = false;
  58. }
  59. void RenderSystem::destroy()
  60. {
  61. gCoreAccessor().queueCommand(std::bind(&RenderSystem::destroy_internal, this));
  62. gCoreThread().submitAccessors(true);
  63. }
  64. void RenderSystem::destroy_internal()
  65. {
  66. mActiveRenderTarget = nullptr;
  67. }
  68. const RenderSystemCapabilities* RenderSystem::getCapabilities(void) const
  69. {
  70. return mCurrentCapabilities;
  71. }
  72. const DriverVersion& RenderSystem::getDriverVersion(void) const
  73. {
  74. THROW_IF_NOT_CORE_THREAD;
  75. return mDriverVersion;
  76. }
  77. void RenderSystem::disableTextureUnit(GpuProgramType gptype, UINT16 texUnit)
  78. {
  79. THROW_IF_NOT_CORE_THREAD;
  80. setTexture(gptype, texUnit, false, sNullTexPtr);
  81. }
  82. void RenderSystem::addClipPlane (const Plane &p)
  83. {
  84. THROW_IF_NOT_CORE_THREAD;
  85. mClipPlanes.push_back(p);
  86. mClipPlanesDirty = true;
  87. }
  88. void RenderSystem::setClipPlanes(const PlaneList& clipPlanes)
  89. {
  90. THROW_IF_NOT_CORE_THREAD;
  91. if (clipPlanes != mClipPlanes)
  92. {
  93. mClipPlanes = clipPlanes;
  94. mClipPlanesDirty = true;
  95. }
  96. }
  97. void RenderSystem::resetClipPlanes()
  98. {
  99. THROW_IF_NOT_CORE_THREAD;
  100. if (!mClipPlanes.empty())
  101. {
  102. mClipPlanes.clear();
  103. mClipPlanesDirty = true;
  104. }
  105. }
  106. void RenderSystem::bindGpuProgram(const SPtr<GpuProgramCore>& prg)
  107. {
  108. THROW_IF_NOT_CORE_THREAD;
  109. switch(prg->getProperties().getType())
  110. {
  111. case GPT_VERTEX_PROGRAM:
  112. if (!mVertexProgramBound && !mClipPlanes.empty())
  113. mClipPlanesDirty = true;
  114. mVertexProgramBound = true;
  115. break;
  116. case GPT_GEOMETRY_PROGRAM:
  117. mGeometryProgramBound = true;
  118. break;
  119. case GPT_FRAGMENT_PROGRAM:
  120. mFragmentProgramBound = true;
  121. break;
  122. case GPT_DOMAIN_PROGRAM:
  123. mDomainProgramBound = true;
  124. break;
  125. case GPT_HULL_PROGRAM:
  126. mHullProgramBound = true;
  127. break;
  128. case GPT_COMPUTE_PROGRAM:
  129. mComputeProgramBound = true;
  130. break;
  131. }
  132. }
  133. void RenderSystem::unbindGpuProgram(GpuProgramType gptype)
  134. {
  135. THROW_IF_NOT_CORE_THREAD;
  136. switch(gptype)
  137. {
  138. case GPT_VERTEX_PROGRAM:
  139. if (mVertexProgramBound && !mClipPlanes.empty())
  140. mClipPlanesDirty = true;
  141. mVertexProgramBound = false;
  142. break;
  143. case GPT_GEOMETRY_PROGRAM:
  144. mGeometryProgramBound = false;
  145. break;
  146. case GPT_FRAGMENT_PROGRAM:
  147. mFragmentProgramBound = false;
  148. break;
  149. case GPT_DOMAIN_PROGRAM:
  150. mDomainProgramBound = false;
  151. break;
  152. case GPT_HULL_PROGRAM:
  153. mHullProgramBound = false;
  154. break;
  155. case GPT_COMPUTE_PROGRAM:
  156. mComputeProgramBound = false;
  157. break;
  158. }
  159. }
  160. bool RenderSystem::isGpuProgramBound(GpuProgramType gptype)
  161. {
  162. THROW_IF_NOT_CORE_THREAD;
  163. switch(gptype)
  164. {
  165. case GPT_VERTEX_PROGRAM:
  166. return mVertexProgramBound;
  167. case GPT_GEOMETRY_PROGRAM:
  168. return mGeometryProgramBound;
  169. case GPT_FRAGMENT_PROGRAM:
  170. return mFragmentProgramBound;
  171. case GPT_DOMAIN_PROGRAM:
  172. return mDomainProgramBound;
  173. case GPT_HULL_PROGRAM:
  174. return mHullProgramBound;
  175. case GPT_COMPUTE_PROGRAM:
  176. return mComputeProgramBound;
  177. }
  178. return false;
  179. }
  180. void RenderSystem::swapBuffers(const SPtr<RenderTargetCore>& target)
  181. {
  182. THROW_IF_NOT_CORE_THREAD;
  183. target->swapBuffers();
  184. BS_INC_RENDER_STAT(NumPresents);
  185. }
  186. }