BsHandleDrawManager.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "BsHandleDrawManager.h"
  2. #include "BsDrawHelper.h"
  3. #include "BsMaterial.h"
  4. #include "BsBuiltinEditorResources.h"
  5. #include "BsCoreThread.h"
  6. #include "BsRendererManager.h"
  7. #include "BsCoreRenderer.h"
  8. #include "BsTransientMesh.h"
  9. #include "BsCamera.h"
  10. #include "BsRendererUtility.h"
  11. #include "BsSceneObject.h"
  12. #include "BsTime.h"
  13. using namespace std::placeholders;
  14. namespace BansheeEngine
  15. {
  16. const UINT32 HandleDrawManager::SPHERE_QUALITY = 1;
  17. const UINT32 HandleDrawManager::WIRE_SPHERE_QUALITY = 10;
  18. const UINT32 HandleDrawManager::ARC_QUALITY = 10;
  19. HandleDrawManager::HandleDrawManager()
  20. :mCore(nullptr), mLastFrameIdx((UINT64)-1)
  21. {
  22. mTransform = Matrix4::IDENTITY;
  23. mDrawHelper = bs_new<DrawHelper>();
  24. HMaterial solidMaterial = BuiltinEditorResources::instance().createSolidHandleMat();
  25. HMaterial wireMaterial = BuiltinEditorResources::instance().createWireHandleMat();
  26. SPtr<MaterialCore> solidMaterialProxy = solidMaterial->getCore();
  27. SPtr<MaterialCore> wireMaterialProxy = wireMaterial->getCore();
  28. mCore.store(bs_new<HandleDrawManagerCore>(HandleDrawManagerCore::PrivatelyConstruct()), std::memory_order_release);
  29. gCoreAccessor().queueCommand(std::bind(&HandleDrawManager::initializeCore, this, wireMaterialProxy, solidMaterialProxy));
  30. }
  31. HandleDrawManager::~HandleDrawManager()
  32. {
  33. clearMeshes();
  34. bs_delete(mDrawHelper);
  35. gCoreAccessor().queueCommand(std::bind(&HandleDrawManager::destroyCore, this, mCore.load(std::memory_order_relaxed)));
  36. }
  37. void HandleDrawManager::initializeCore(const SPtr<MaterialCore>& wireMat, const SPtr<MaterialCore>& solidMat)
  38. {
  39. THROW_IF_NOT_CORE_THREAD;
  40. mCore.load(std::memory_order_acquire)->initialize(wireMat, solidMat);
  41. }
  42. void HandleDrawManager::destroyCore(HandleDrawManagerCore* core)
  43. {
  44. THROW_IF_NOT_CORE_THREAD;
  45. bs_delete(core);
  46. }
  47. void HandleDrawManager::setColor(const Color& color)
  48. {
  49. mDrawHelper->setColor(color);
  50. }
  51. void HandleDrawManager::setTransform(const Matrix4& transform)
  52. {
  53. mTransform = transform;
  54. }
  55. void HandleDrawManager::setLayer(UINT64 layer)
  56. {
  57. mDrawHelper->setLayer(layer);
  58. }
  59. void HandleDrawManager::drawCube(const Vector3& position, const Vector3& extents, float size)
  60. {
  61. Matrix4 scale = Matrix4::scaling(size);
  62. mDrawHelper->setTransform(mTransform * scale);
  63. mDrawHelper->cube(position, extents);
  64. }
  65. void HandleDrawManager::drawSphere(const Vector3& position, float radius, float size)
  66. {
  67. Matrix4 scale = Matrix4::scaling(size);
  68. mDrawHelper->setTransform(mTransform * scale);
  69. mDrawHelper->sphere(position, radius);
  70. }
  71. void HandleDrawManager::drawWireCube(const Vector3& position, const Vector3& extents, float size)
  72. {
  73. Matrix4 scale = Matrix4::scaling(size);
  74. mDrawHelper->setTransform(mTransform * scale);
  75. mDrawHelper->wireCube(position, extents);
  76. }
  77. void HandleDrawManager::drawWireSphere(const Vector3& position, float radius, float size)
  78. {
  79. Matrix4 scale = Matrix4::scaling(size);
  80. mDrawHelper->setTransform(mTransform * scale);
  81. mDrawHelper->wireSphere(position, radius);
  82. }
  83. void HandleDrawManager::drawCone(const Vector3& base, const Vector3& normal, float height, float radius, float size)
  84. {
  85. Matrix4 scale = Matrix4::scaling(size);
  86. mDrawHelper->setTransform(mTransform * scale);
  87. mDrawHelper->cone(base, normal, height, radius);
  88. }
  89. void HandleDrawManager::drawLine(const Vector3& start, const Vector3& end, float size)
  90. {
  91. Matrix4 scale = Matrix4::scaling(size);
  92. mDrawHelper->setTransform(mTransform * scale);
  93. mDrawHelper->line(start, end);
  94. }
  95. void HandleDrawManager::drawDisc(const Vector3& position, const Vector3& normal, float radius, float size)
  96. {
  97. Matrix4 scale = Matrix4::scaling(size);
  98. mDrawHelper->setTransform(mTransform * scale);
  99. mDrawHelper->disc(position, normal, radius);
  100. }
  101. void HandleDrawManager::drawWireDisc(const Vector3& position, const Vector3& normal, float radius, float size)
  102. {
  103. Matrix4 scale = Matrix4::scaling(size);
  104. mDrawHelper->setTransform(mTransform * scale);
  105. mDrawHelper->wireDisc(position, normal, radius);
  106. }
  107. void HandleDrawManager::drawArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle, float size)
  108. {
  109. Matrix4 scale = Matrix4::scaling(size);
  110. mDrawHelper->setTransform(mTransform * scale);
  111. mDrawHelper->arc(position, normal, radius, startAngle, amountAngle);
  112. }
  113. void HandleDrawManager::drawWireArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle, float size)
  114. {
  115. Matrix4 scale = Matrix4::scaling(size);
  116. mDrawHelper->setTransform(mTransform * scale);
  117. mDrawHelper->wireArc(position, normal, radius, startAngle, amountAngle);
  118. }
  119. void HandleDrawManager::drawRect(const Rect3& area, float size)
  120. {
  121. Matrix4 scale = Matrix4::scaling(size);
  122. mDrawHelper->setTransform(mTransform * scale);
  123. mDrawHelper->rectangle(area);
  124. }
  125. void HandleDrawManager::draw(const CameraPtr& camera)
  126. {
  127. HandleDrawManagerCore* core = mCore.load(std::memory_order_relaxed);
  128. // Clear meshes from previous frame
  129. UINT64 frameIdx = gTime().getFrameIdx();
  130. if(frameIdx != mLastFrameIdx)
  131. {
  132. gCoreAccessor().queueCommand(std::bind(&HandleDrawManagerCore::clearQueued, core));
  133. clearMeshes();
  134. mLastFrameIdx = frameIdx;
  135. }
  136. mDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera->getPosition(), camera->getLayers());
  137. const Vector<DrawHelper::ShapeMeshData>& meshes = mDrawHelper->getMeshes();
  138. mActiveMeshes.push_back(meshes);
  139. Vector<HandleDrawManagerCore::MeshData> proxyData;
  140. for (auto& meshData : meshes)
  141. {
  142. if (meshData.type == DrawHelper::MeshType::Solid)
  143. {
  144. proxyData.push_back(HandleDrawManagerCore::MeshData(
  145. meshData.mesh->getCore(), HandleDrawManagerCore::MeshType::Solid));
  146. }
  147. else // Wire
  148. {
  149. proxyData.push_back(HandleDrawManagerCore::MeshData(
  150. meshData.mesh->getCore(), HandleDrawManagerCore::MeshType::Wire));
  151. }
  152. }
  153. gCoreAccessor().queueCommand(std::bind(&HandleDrawManagerCore::queueForDraw, core, camera->getCore(), proxyData));
  154. }
  155. void HandleDrawManager::clear()
  156. {
  157. mDrawHelper->clear();
  158. }
  159. void HandleDrawManager::clearMeshes()
  160. {
  161. for (auto entry : mActiveMeshes)
  162. mDrawHelper->clearMeshes(entry);
  163. mActiveMeshes.clear();
  164. }
  165. HandleDrawManagerCore::~HandleDrawManagerCore()
  166. {
  167. clearQueued();
  168. }
  169. void HandleDrawManagerCore::initialize(const SPtr<MaterialCore>& wireMat, const SPtr<MaterialCore>& solidMat)
  170. {
  171. {
  172. mWireMaterial.mat = wireMat;
  173. SPtr<GpuParamsCore> vertParams = wireMat->getPassParameters(0)->mVertParams;
  174. vertParams->getParam("matViewProj", mWireMaterial.mViewProj);
  175. }
  176. {
  177. mSolidMaterial.mat = solidMat;
  178. SPtr<GpuParamsCore> vertParams = solidMat->getPassParameters(0)->mVertParams;
  179. SPtr<GpuParamsCore> fragParams = solidMat->getPassParameters(0)->mFragParams;
  180. vertParams->getParam("matViewProj", mSolidMaterial.mViewProj);
  181. fragParams->getParam("viewDir", mSolidMaterial.mViewDir);
  182. }
  183. }
  184. void HandleDrawManagerCore::queueForDraw(const SPtr<CameraCore>& camera, const Vector<MeshData>& meshes)
  185. {
  186. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  187. if (camera != nullptr)
  188. {
  189. UINT32 idx = (UINT32)mQueuedData.size();
  190. mQueuedData.push_back({ camera, meshes });
  191. activeRenderer->_registerRenderCallback(camera.get(), 20, std::bind(&HandleDrawManagerCore::render, this, idx));
  192. }
  193. }
  194. void HandleDrawManagerCore::clearQueued()
  195. {
  196. CoreRendererPtr activeRenderer = RendererManager::instance().getActive();
  197. for (auto& entry : mQueuedData)
  198. activeRenderer->_unregisterRenderCallback(entry.camera.get(), 20);
  199. mQueuedData.clear();
  200. }
  201. void HandleDrawManagerCore::render(UINT32 queuedDataIdx)
  202. {
  203. THROW_IF_NOT_CORE_THREAD;
  204. const QueuedData& queueData = mQueuedData[queuedDataIdx];
  205. SPtr<CameraCore> camera = queueData.camera;
  206. const Vector<MeshData>& meshes = queueData.meshes;
  207. SPtr<RenderTargetCore> renderTarget = camera->getViewport()->getTarget();
  208. float width = (float)renderTarget->getProperties().getWidth();
  209. float height = (float)renderTarget->getProperties().getHeight();
  210. Rect2 normArea = camera->getViewport()->getNormArea();
  211. Rect2I screenArea;
  212. screenArea.x = (int)(normArea.x * width);
  213. screenArea.y = (int)(normArea.y * height);
  214. screenArea.width = (int)(normArea.width * width);
  215. screenArea.height = (int)(normArea.height * height);
  216. Matrix4 viewProjMat = camera->getProjectionMatrixRS() * camera->getViewMatrix();
  217. mSolidMaterial.mViewProj.set(viewProjMat);
  218. mSolidMaterial.mViewDir.set((Vector4)camera->getForward());
  219. mWireMaterial.mViewProj.set(viewProjMat);
  220. MeshType currentType = MeshType::Solid;
  221. if (meshes.size() > 0)
  222. {
  223. currentType = meshes[0].type;
  224. if (currentType == MeshType::Solid)
  225. gRendererUtility().setPass(mSolidMaterial.mat, 0);
  226. else
  227. gRendererUtility().setPass(mWireMaterial.mat, 0);
  228. }
  229. for (auto& meshData : meshes)
  230. {
  231. if (currentType != meshData.type)
  232. {
  233. if (meshData.type == MeshType::Solid)
  234. gRendererUtility().setPass(mSolidMaterial.mat, 0);
  235. else
  236. gRendererUtility().setPass(mWireMaterial.mat, 0);
  237. currentType = meshData.type;
  238. }
  239. gRendererUtility().draw(meshData.mesh, meshData.mesh->getProperties().getSubMesh(0));
  240. }
  241. }
  242. }