BsRenderAPI.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 BansheeEngine
  16. {
  17. void RenderAPI::setGpuParams(CoreAccessor& accessor, const SPtr<GpuParams>& gpuParams)
  18. {
  19. accessor.queueCommand(std::bind(&RenderAPICore::setGpuParams, RenderAPICore::instancePtr(), gpuParams->getCore(),
  20. nullptr));
  21. }
  22. void RenderAPI::setGraphicsPipeline(CoreAccessor& accessor, const SPtr<GpuPipelineState>& pipelineState)
  23. {
  24. accessor.queueCommand(std::bind(&RenderAPICore::setGraphicsPipeline, RenderAPICore::instancePtr(),
  25. pipelineState->getCore(), nullptr));
  26. }
  27. void RenderAPI::setComputePipeline(CoreAccessor& accessor, const SPtr<GpuProgram>& computeProgram)
  28. {
  29. accessor.queueCommand(std::bind(&RenderAPICore::setComputePipeline, RenderAPICore::instancePtr(),
  30. computeProgram->getCore(), nullptr));
  31. }
  32. void RenderAPI::setVertexBuffers(CoreAccessor& accessor, 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. accessor.queueCommand(std::bind(resizeFunc, RenderAPICore::instancePtr(), index, coreBuffers));
  43. }
  44. void RenderAPI::setIndexBuffer(CoreAccessor& accessor, const SPtr<IndexBuffer>& buffer)
  45. {
  46. accessor.queueCommand(std::bind(&RenderAPICore::setIndexBuffer, RenderAPICore::instancePtr(), buffer->getCore(),
  47. nullptr));
  48. }
  49. void RenderAPI::setVertexDeclaration(CoreAccessor& accessor, const SPtr<VertexDeclaration>& vertexDeclaration)
  50. {
  51. accessor.queueCommand(std::bind(&RenderAPICore::setVertexDeclaration, RenderAPICore::instancePtr(),
  52. vertexDeclaration->getCore(), nullptr));
  53. }
  54. void RenderAPI::setViewport(CoreAccessor& accessor, const Rect2& vp)
  55. {
  56. accessor.queueCommand(std::bind(&RenderAPICore::setViewport, RenderAPICore::instancePtr(), vp, nullptr));
  57. }
  58. void RenderAPI::setStencilRef(CoreAccessor& accessor, UINT32 value)
  59. {
  60. accessor.queueCommand(std::bind(&RenderAPICore::setStencilRef, RenderAPICore::instancePtr(), value, nullptr));
  61. }
  62. void RenderAPI::setDrawOperation(CoreAccessor& accessor, DrawOperationType op)
  63. {
  64. accessor.queueCommand(std::bind(&RenderAPICore::setDrawOperation, RenderAPICore::instancePtr(), op,
  65. nullptr));
  66. }
  67. void RenderAPI::setScissorRect(CoreAccessor& accessor, UINT32 left, UINT32 top, UINT32 right, UINT32 bottom)
  68. {
  69. accessor.queueCommand(std::bind(&RenderAPICore::setScissorRect, RenderAPICore::instancePtr(), left, top, right, bottom,
  70. nullptr));
  71. }
  72. void RenderAPI::setRenderTarget(CoreAccessor& accessor, const SPtr<RenderTarget>& target, bool readOnlyDepthStencil)
  73. {
  74. accessor.queueCommand(std::bind(&RenderAPICore::setRenderTarget,
  75. RenderAPICore::instancePtr(), target->getCore(), readOnlyDepthStencil, nullptr));
  76. }
  77. void RenderAPI::beginRender(CoreAccessor& accessor)
  78. {
  79. accessor.queueCommand(std::bind(&RenderAPICore::beginFrame, RenderAPICore::instancePtr(), nullptr));
  80. }
  81. void RenderAPI::endRender(CoreAccessor& accessor)
  82. {
  83. accessor.queueCommand(std::bind(&RenderAPICore::endFrame, RenderAPICore::instancePtr(), nullptr));
  84. }
  85. void RenderAPI::clearRenderTarget(CoreAccessor& accessor, UINT32 buffers, const Color& color, float depth,
  86. UINT16 stencil, UINT8 targetMask)
  87. {
  88. accessor.queueCommand(std::bind(&RenderAPICore::clearRenderTarget, RenderAPICore::instancePtr(), buffers, color,
  89. depth, stencil, targetMask, nullptr));
  90. }
  91. void RenderAPI::clearViewport(CoreAccessor& accessor, UINT32 buffers, const Color& color, float depth, UINT16 stencil,
  92. UINT8 targetMask)
  93. {
  94. accessor.queueCommand(std::bind(&RenderAPICore::clearViewport, RenderAPICore::instancePtr(), buffers, color, depth,
  95. stencil, targetMask, nullptr));
  96. }
  97. void RenderAPI::swapBuffers(CoreAccessor& accessor, const SPtr<RenderTarget>& target)
  98. {
  99. accessor.queueCommand(std::bind(&RenderAPICore::swapBuffers, RenderAPICore::instancePtr(), target->getCore(), 1));
  100. }
  101. void RenderAPI::draw(CoreAccessor& accessor, UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount)
  102. {
  103. accessor.queueCommand(std::bind(&RenderAPICore::draw, RenderAPICore::instancePtr(), vertexOffset,
  104. vertexCount, instanceCount, nullptr));
  105. }
  106. void RenderAPI::drawIndexed(CoreAccessor& accessor, UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset,
  107. UINT32 vertexCount, UINT32 instanceCount)
  108. {
  109. accessor.queueCommand(std::bind(&RenderAPICore::drawIndexed, RenderAPICore::instancePtr(), startIndex, indexCount,
  110. vertexOffset, vertexCount, instanceCount, nullptr));
  111. }
  112. void RenderAPI::dispatchCompute(CoreAccessor& accessor, UINT32 numGroupsX, UINT32 numGroupsY, UINT32 numGroupsZ)
  113. {
  114. accessor.queueCommand(std::bind(&RenderAPICore::dispatchCompute, RenderAPICore::instancePtr(), numGroupsX,
  115. numGroupsY, numGroupsZ, nullptr));
  116. }
  117. const VideoModeInfo& RenderAPI::getVideoModeInfo()
  118. {
  119. return RenderAPICore::instance().getVideoModeInfo();
  120. }
  121. void RenderAPI::convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest)
  122. {
  123. RenderAPICore::instance().convertProjectionMatrix(matrix, dest);
  124. }
  125. const RenderAPIInfo& RenderAPI::getAPIInfo()
  126. {
  127. return RenderAPICore::instance().getAPIInfo();
  128. }
  129. RenderAPICore::RenderAPICore()
  130. : mCurrentCapabilities(nullptr), mNumDevices(0)
  131. {
  132. }
  133. RenderAPICore::~RenderAPICore()
  134. {
  135. // Base classes need to call virtual destroy_internal method instead of a destructor
  136. bs_deleteN(mCurrentCapabilities, mNumDevices);
  137. mCurrentCapabilities = nullptr;
  138. }
  139. SPtr<RenderWindow> RenderAPICore::initialize(const RENDER_WINDOW_DESC& primaryWindowDesc)
  140. {
  141. gCoreThread().queueCommand(std::bind((void(RenderAPICore::*)())&RenderAPICore::initialize, this), true);
  142. RENDER_WINDOW_DESC windowDesc = primaryWindowDesc;
  143. SPtr<RenderWindow> renderWindow = RenderWindow::create(windowDesc, nullptr);
  144. gCoreThread().queueCommand(std::bind(&RenderAPICore::initializeWithWindow, this, renderWindow->getCore()), true);
  145. return renderWindow;
  146. }
  147. void RenderAPICore::initialize()
  148. {
  149. // Do nothing
  150. }
  151. void RenderAPICore::initializeWithWindow(const SPtr<RenderWindowCore>& primaryWindow)
  152. {
  153. THROW_IF_NOT_CORE_THREAD;
  154. }
  155. void RenderAPICore::destroy()
  156. {
  157. gCoreAccessor().queueCommand(std::bind(&RenderAPICore::destroyCore, this));
  158. gCoreThread().submitAccessors(true);
  159. }
  160. void RenderAPICore::destroyCore()
  161. {
  162. mActiveRenderTarget = nullptr;
  163. }
  164. const RenderAPICapabilities& RenderAPICore::getCapabilities(UINT32 deviceIdx) const
  165. {
  166. if(deviceIdx >= mNumDevices)
  167. {
  168. LOGWRN("Invalid device index provided: " + toString(deviceIdx) + ". Valid range is: [0, " + toString(mNumDevices) + ").");
  169. return mCurrentCapabilities[0];
  170. }
  171. return mCurrentCapabilities[deviceIdx];
  172. }
  173. UINT32 RenderAPICore::vertexCountToPrimCount(DrawOperationType type, UINT32 elementCount)
  174. {
  175. UINT32 primCount = 0;
  176. switch (type)
  177. {
  178. case DOT_POINT_LIST:
  179. primCount = elementCount;
  180. break;
  181. case DOT_LINE_LIST:
  182. primCount = elementCount / 2;
  183. break;
  184. case DOT_LINE_STRIP:
  185. primCount = elementCount - 1;
  186. break;
  187. case DOT_TRIANGLE_LIST:
  188. primCount = elementCount / 3;
  189. break;
  190. case DOT_TRIANGLE_STRIP:
  191. primCount = elementCount - 2;
  192. break;
  193. case DOT_TRIANGLE_FAN:
  194. primCount = elementCount - 2;
  195. break;
  196. }
  197. return primCount;
  198. }
  199. }