BsRenderAPI.cpp 7.7 KB

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