BsRenderAPI.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "RenderAPI/BsRenderAPI.h"
  4. #include "CoreThread/BsCoreThread.h"
  5. #include "RenderAPI/BsViewport.h"
  6. #include "RenderAPI/BsRenderTarget.h"
  7. #include "RenderAPI/BsRenderWindow.h"
  8. #include "Mesh/BsMesh.h"
  9. #include "RenderAPI/BsGpuParams.h"
  10. #include "RenderAPI/BsDepthStencilState.h"
  11. #include "RenderAPI/BsRasterizerState.h"
  12. #include "RenderAPI/BsGpuBuffer.h"
  13. #include "RenderAPI/BsGpuPipelineState.h"
  14. using namespace std::placeholders;
  15. namespace bs
  16. {
  17. void RenderAPI::setGpuParams(const SPtr<GpuParams>& gpuParams)
  18. {
  19. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::setGpuParams, ct::RenderAPI::instancePtr(), gpuParams->getCore(),
  20. nullptr));
  21. }
  22. void RenderAPI::setGraphicsPipeline(const SPtr<GraphicsPipelineState>& pipelineState)
  23. {
  24. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::setGraphicsPipeline, ct::RenderAPI::instancePtr(),
  25. pipelineState->getCore(), nullptr));
  26. }
  27. void RenderAPI::setComputePipeline(const SPtr<ComputePipelineState>& pipelineState)
  28. {
  29. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::setComputePipeline, ct::RenderAPI::instancePtr(),
  30. pipelineState->getCore(), nullptr));
  31. }
  32. void RenderAPI::setVertexBuffers(UINT32 index, const Vector<SPtr<VertexBuffer>>& buffers)
  33. {
  34. Vector<SPtr<ct::VertexBuffer>> coreBuffers(buffers.size());
  35. for (UINT32 i = 0; i < (UINT32)buffers.size(); i++)
  36. coreBuffers[i] = buffers[i] != nullptr ? buffers[i]->getCore() : nullptr;
  37. std::function<void(ct::RenderAPI*, UINT32, const Vector<SPtr<ct::VertexBuffer>>&)> resizeFunc =
  38. [](ct::RenderAPI* rs, UINT32 idx, const Vector<SPtr<ct::VertexBuffer>>& _buffers)
  39. {
  40. rs->setVertexBuffers(idx, (SPtr<ct::VertexBuffer>*)_buffers.data(), (UINT32)_buffers.size());
  41. };
  42. gCoreThread().queueCommand(std::bind(resizeFunc, ct::RenderAPI::instancePtr(), index, coreBuffers));
  43. }
  44. void RenderAPI::setIndexBuffer(const SPtr<IndexBuffer>& buffer)
  45. {
  46. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::setIndexBuffer, ct::RenderAPI::instancePtr(), buffer->getCore(),
  47. nullptr));
  48. }
  49. void RenderAPI::setVertexDeclaration(const SPtr<VertexDeclaration>& vertexDeclaration)
  50. {
  51. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::setVertexDeclaration, ct::RenderAPI::instancePtr(),
  52. vertexDeclaration->getCore(), nullptr));
  53. }
  54. void RenderAPI::setViewport(const Rect2& vp)
  55. {
  56. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::setViewport, ct::RenderAPI::instancePtr(), vp, nullptr));
  57. }
  58. void RenderAPI::setStencilRef(UINT32 value)
  59. {
  60. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::setStencilRef, ct::RenderAPI::instancePtr(), value, nullptr));
  61. }
  62. void RenderAPI::setDrawOperation(DrawOperationType op)
  63. {
  64. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::setDrawOperation, ct::RenderAPI::instancePtr(), op,
  65. nullptr));
  66. }
  67. void RenderAPI::setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom)
  68. {
  69. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::setScissorRect, ct::RenderAPI::instancePtr(), left, top, right, bottom,
  70. nullptr));
  71. }
  72. void RenderAPI::setRenderTarget(const SPtr<RenderTarget>& target, UINT32 readOnlyFlags,
  73. RenderSurfaceMask loadMask)
  74. {
  75. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::setRenderTarget,
  76. ct::RenderAPI::instancePtr(), target->getCore(), readOnlyFlags, loadMask, nullptr));
  77. }
  78. void RenderAPI::clearRenderTarget(UINT32 buffers, const Color& color, float depth,
  79. UINT16 stencil, UINT8 targetMask)
  80. {
  81. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::clearRenderTarget, ct::RenderAPI::instancePtr(), buffers, color,
  82. depth, stencil, targetMask, nullptr));
  83. }
  84. void RenderAPI::clearViewport(UINT32 buffers, const Color& color, float depth, UINT16 stencil,
  85. UINT8 targetMask)
  86. {
  87. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::clearViewport, ct::RenderAPI::instancePtr(), buffers, color, depth,
  88. stencil, targetMask, nullptr));
  89. }
  90. void RenderAPI::swapBuffers(const SPtr<RenderTarget>& target)
  91. {
  92. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::swapBuffers, ct::RenderAPI::instancePtr(), target->getCore(), 1));
  93. }
  94. void RenderAPI::draw(UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount)
  95. {
  96. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::draw, ct::RenderAPI::instancePtr(), vertexOffset,
  97. vertexCount, instanceCount, nullptr));
  98. }
  99. void RenderAPI::drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset,
  100. UINT32 vertexCount, UINT32 instanceCount)
  101. {
  102. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::drawIndexed, ct::RenderAPI::instancePtr(), startIndex, indexCount,
  103. vertexOffset, vertexCount, instanceCount, nullptr));
  104. }
  105. void RenderAPI::dispatchCompute(UINT32 numGroupsX, UINT32 numGroupsY, UINT32 numGroupsZ)
  106. {
  107. gCoreThread().queueCommand(std::bind(&ct::RenderAPI::dispatchCompute, ct::RenderAPI::instancePtr(), numGroupsX,
  108. numGroupsY, numGroupsZ, nullptr));
  109. }
  110. const VideoModeInfo& RenderAPI::getVideoModeInfo()
  111. {
  112. return ct::RenderAPI::instance().getVideoModeInfo();
  113. }
  114. void RenderAPI::convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest)
  115. {
  116. ct::RenderAPI::instance().convertProjectionMatrix(matrix, dest);
  117. }
  118. const RenderAPIInfo& RenderAPI::getAPIInfo()
  119. {
  120. return ct::RenderAPI::instance().getAPIInfo();
  121. }
  122. namespace ct
  123. {
  124. RenderAPI::RenderAPI()
  125. : mCurrentCapabilities(nullptr), mNumDevices(0)
  126. {
  127. }
  128. RenderAPI::~RenderAPI()
  129. {
  130. // Base classes need to call virtual destroy_internal method instead of a destructor
  131. bs_deleteN(mCurrentCapabilities, mNumDevices);
  132. mCurrentCapabilities = nullptr;
  133. }
  134. SPtr<bs::RenderWindow> RenderAPI::initialize(const RENDER_WINDOW_DESC& primaryWindowDesc)
  135. {
  136. gCoreThread().queueCommand(std::bind((void(RenderAPI::*)())&RenderAPI::initialize, this),
  137. CTQF_InternalQueue | CTQF_BlockUntilComplete);
  138. RENDER_WINDOW_DESC windowDesc = primaryWindowDesc;
  139. SPtr<bs::RenderWindow> renderWindow = bs::RenderWindow::create(windowDesc, nullptr);
  140. gCoreThread().queueCommand(std::bind(&RenderAPI::initializeWithWindow, this, renderWindow->getCore()),
  141. CTQF_InternalQueue | CTQF_BlockUntilComplete);
  142. return renderWindow;
  143. }
  144. void RenderAPI::initialize()
  145. {
  146. // Do nothing
  147. }
  148. void RenderAPI::initializeWithWindow(const SPtr<RenderWindow>& primaryWindow)
  149. {
  150. THROW_IF_NOT_CORE_THREAD;
  151. }
  152. void RenderAPI::destroy()
  153. {
  154. gCoreThread().queueCommand(std::bind(&RenderAPI::destroyCore, this));
  155. gCoreThread().submitAll(true);
  156. }
  157. void RenderAPI::destroyCore()
  158. {
  159. mActiveRenderTarget = nullptr;
  160. }
  161. const RenderAPICapabilities& RenderAPI::getCapabilities(UINT32 deviceIdx) const
  162. {
  163. if(deviceIdx >= mNumDevices)
  164. {
  165. LOGWRN("Invalid device index provided: " + toString(deviceIdx) + ". Valid range is: [0, " + toString(mNumDevices) + ").");
  166. return mCurrentCapabilities[0];
  167. }
  168. return mCurrentCapabilities[deviceIdx];
  169. }
  170. UINT32 RenderAPI::vertexCountToPrimCount(DrawOperationType type, UINT32 elementCount)
  171. {
  172. UINT32 primCount = 0;
  173. switch (type)
  174. {
  175. case DOT_POINT_LIST:
  176. primCount = elementCount;
  177. break;
  178. case DOT_LINE_LIST:
  179. primCount = elementCount / 2;
  180. break;
  181. case DOT_LINE_STRIP:
  182. primCount = elementCount - 1;
  183. break;
  184. case DOT_TRIANGLE_LIST:
  185. primCount = elementCount / 3;
  186. break;
  187. case DOT_TRIANGLE_STRIP:
  188. primCount = elementCount - 2;
  189. break;
  190. case DOT_TRIANGLE_FAN:
  191. primCount = elementCount - 2;
  192. break;
  193. }
  194. return primCount;
  195. }
  196. }
  197. }