BsRenderAPI.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "BsResource.h"
  9. #include "BsMesh.h"
  10. #include "BsRenderStats.h"
  11. #include "BsGpuParams.h"
  12. #include "BsBlendState.h"
  13. #include "BsDepthStencilState.h"
  14. #include "BsRasterizerState.h"
  15. #include "BsGpuParamDesc.h"
  16. #include "BsGpuBuffer.h"
  17. #include "BsGpuParamBlockBuffer.h"
  18. #include "BsCommandBuffer.h"
  19. using namespace std::placeholders;
  20. namespace BansheeEngine
  21. {
  22. void RenderAPI::setTexture(CoreAccessor& accessor, GpuProgramType gptype, UINT16 unit, const SPtr<Texture> &texPtr)
  23. {
  24. accessor.queueCommand(std::bind(&RenderAPICore::setTexture, RenderAPICore::instancePtr(), gptype, unit,
  25. texPtr->getCore(), nullptr));
  26. }
  27. void RenderAPI::setLoadStoreTexture(CoreAccessor& accessor, GpuProgramType gptype, UINT16 unit,
  28. const SPtr<Texture>& texPtr, const TextureSurface& surface)
  29. {
  30. accessor.queueCommand(std::bind(&RenderAPICore::setLoadStoreTexture, RenderAPICore::instancePtr(), gptype, unit,
  31. texPtr->getCore(), surface, nullptr));
  32. }
  33. void RenderAPI::setBuffer(CoreAccessor& accessor, GpuProgramType gptype, UINT16 unit, const SPtr<GpuBuffer>& buffer,
  34. bool loadStore)
  35. {
  36. accessor.queueCommand(std::bind(&RenderAPICore::setBuffer, RenderAPICore::instancePtr(), gptype, unit,
  37. buffer->getCore(), loadStore, nullptr));
  38. }
  39. void RenderAPI::setSamplerState(CoreAccessor& accessor, GpuProgramType gptype, UINT16 texUnit,
  40. const SPtr<SamplerState>& samplerState)
  41. {
  42. accessor.queueCommand(std::bind(&RenderAPICore::setSamplerState, RenderAPICore::instancePtr(), gptype, texUnit,
  43. samplerState->getCore(), nullptr));
  44. }
  45. void RenderAPI::setBlendState(CoreAccessor& accessor, const SPtr<BlendState>& blendState)
  46. {
  47. accessor.queueCommand(std::bind(&RenderAPICore::setBlendState, RenderAPICore::instancePtr(), blendState->getCore(),
  48. nullptr));
  49. }
  50. void RenderAPI::setRasterizerState(CoreAccessor& accessor, const SPtr<RasterizerState>& rasterizerState)
  51. {
  52. accessor.queueCommand(std::bind(&RenderAPICore::setRasterizerState, RenderAPICore::instancePtr(),
  53. rasterizerState->getCore(), nullptr));
  54. }
  55. void RenderAPI::setDepthStencilState(CoreAccessor& accessor, const SPtr<DepthStencilState>& depthStencilState, UINT32 stencilRefValue)
  56. {
  57. accessor.queueCommand(std::bind(&RenderAPICore::setDepthStencilState, RenderAPICore::instancePtr(),
  58. depthStencilState->getCore(), stencilRefValue, nullptr));
  59. }
  60. void RenderAPI::setVertexBuffers(CoreAccessor& accessor, UINT32 index, const Vector<SPtr<VertexBuffer>>& buffers)
  61. {
  62. Vector<SPtr<VertexBufferCore>> coreBuffers(buffers.size());
  63. for (UINT32 i = 0; i < (UINT32)buffers.size(); i++)
  64. coreBuffers[i] = buffers[i] != nullptr ? buffers[i]->getCore() : nullptr;
  65. std::function<void(RenderAPICore*, UINT32, const Vector<SPtr<VertexBufferCore>>&)> resizeFunc =
  66. [](RenderAPICore* rs, UINT32 idx, const Vector<SPtr<VertexBufferCore>>& _buffers)
  67. {
  68. rs->setVertexBuffers(idx, (SPtr<VertexBufferCore>*)_buffers.data(), (UINT32)_buffers.size());
  69. };
  70. accessor.queueCommand(std::bind(resizeFunc, RenderAPICore::instancePtr(), index, coreBuffers));
  71. }
  72. void RenderAPI::setIndexBuffer(CoreAccessor& accessor, const SPtr<IndexBuffer>& buffer)
  73. {
  74. accessor.queueCommand(std::bind(&RenderAPICore::setIndexBuffer, RenderAPICore::instancePtr(), buffer->getCore(),
  75. nullptr));
  76. }
  77. void RenderAPI::setVertexDeclaration(CoreAccessor& accessor, const SPtr<VertexDeclaration>& vertexDeclaration)
  78. {
  79. accessor.queueCommand(std::bind(&RenderAPICore::setVertexDeclaration, RenderAPICore::instancePtr(),
  80. vertexDeclaration->getCore(), nullptr));
  81. }
  82. void RenderAPI::setViewport(CoreAccessor& accessor, const Rect2& vp)
  83. {
  84. accessor.queueCommand(std::bind(&RenderAPICore::setViewport, RenderAPICore::instancePtr(), vp, nullptr));
  85. }
  86. void RenderAPI::setDrawOperation(CoreAccessor& accessor, DrawOperationType op)
  87. {
  88. accessor.queueCommand(std::bind(&RenderAPICore::setDrawOperation, RenderAPICore::instancePtr(), op,
  89. nullptr));
  90. }
  91. void RenderAPI::setScissorRect(CoreAccessor& accessor, UINT32 left, UINT32 top, UINT32 right, UINT32 bottom)
  92. {
  93. accessor.queueCommand(std::bind(&RenderAPICore::setScissorRect, RenderAPICore::instancePtr(), left, top, right, bottom,
  94. nullptr));
  95. }
  96. void RenderAPI::setRenderTarget(CoreAccessor& accessor, const SPtr<RenderTarget>& target, bool readOnlyDepthStencil)
  97. {
  98. accessor.queueCommand(std::bind(&RenderAPICore::setRenderTarget,
  99. RenderAPICore::instancePtr(), target->getCore(), readOnlyDepthStencil, nullptr));
  100. }
  101. void RenderAPI::bindGpuProgram(CoreAccessor& accessor, const SPtr<GpuProgram>& prg)
  102. {
  103. prg->syncToCore(accessor);
  104. accessor.queueCommand(std::bind(&RenderAPICore::bindGpuProgram, RenderAPICore::instancePtr(), prg->getCore(),
  105. nullptr));
  106. }
  107. void RenderAPI::unbindGpuProgram(CoreAccessor& accessor, GpuProgramType gptype)
  108. {
  109. accessor.queueCommand(std::bind(&RenderAPICore::unbindGpuProgram, RenderAPICore::instancePtr(), gptype,
  110. nullptr));
  111. }
  112. void RenderAPI::beginRender(CoreAccessor& accessor)
  113. {
  114. accessor.queueCommand(std::bind(&RenderAPICore::beginFrame, RenderAPICore::instancePtr(), nullptr));
  115. }
  116. void RenderAPI::endRender(CoreAccessor& accessor)
  117. {
  118. accessor.queueCommand(std::bind(&RenderAPICore::endFrame, RenderAPICore::instancePtr(), nullptr));
  119. }
  120. void RenderAPI::clearRenderTarget(CoreAccessor& accessor, UINT32 buffers, const Color& color, float depth,
  121. UINT16 stencil, UINT8 targetMask)
  122. {
  123. accessor.queueCommand(std::bind(&RenderAPICore::clearRenderTarget, RenderAPICore::instancePtr(), buffers, color,
  124. depth, stencil, targetMask, nullptr));
  125. }
  126. void RenderAPI::clearViewport(CoreAccessor& accessor, UINT32 buffers, const Color& color, float depth, UINT16 stencil,
  127. UINT8 targetMask)
  128. {
  129. accessor.queueCommand(std::bind(&RenderAPICore::clearViewport, RenderAPICore::instancePtr(), buffers, color, depth,
  130. stencil, targetMask, nullptr));
  131. }
  132. void RenderAPI::swapBuffers(CoreAccessor& accessor, const SPtr<RenderTarget>& target)
  133. {
  134. accessor.queueCommand(std::bind(&RenderAPICore::swapBuffers, RenderAPICore::instancePtr(), target->getCore(),
  135. nullptr));
  136. }
  137. void RenderAPI::draw(CoreAccessor& accessor, UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount)
  138. {
  139. accessor.queueCommand(std::bind(&RenderAPICore::draw, RenderAPICore::instancePtr(), vertexOffset,
  140. vertexCount, instanceCount, nullptr));
  141. }
  142. void RenderAPI::drawIndexed(CoreAccessor& accessor, UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset,
  143. UINT32 vertexCount, UINT32 instanceCount)
  144. {
  145. accessor.queueCommand(std::bind(&RenderAPICore::drawIndexed, RenderAPICore::instancePtr(), startIndex, indexCount,
  146. vertexOffset, vertexCount, instanceCount, nullptr));
  147. }
  148. void RenderAPI::dispatchCompute(CoreAccessor& accessor, UINT32 numGroupsX, UINT32 numGroupsY, UINT32 numGroupsZ)
  149. {
  150. accessor.queueCommand(std::bind(&RenderAPICore::dispatchCompute, RenderAPICore::instancePtr(), numGroupsX,
  151. numGroupsY, numGroupsZ, nullptr));
  152. }
  153. const VideoModeInfo& RenderAPI::getVideoModeInfo()
  154. {
  155. return RenderAPICore::instance().getVideoModeInfo();
  156. }
  157. void RenderAPI::convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest)
  158. {
  159. RenderAPICore::instance().convertProjectionMatrix(matrix, dest);
  160. }
  161. const RenderAPIInfo& RenderAPI::getAPIInfo()
  162. {
  163. return RenderAPICore::instance().getAPIInfo();
  164. }
  165. RenderAPICore::RenderAPICore()
  166. : mCurrentCapabilities(nullptr)
  167. {
  168. }
  169. RenderAPICore::~RenderAPICore()
  170. {
  171. // Base classes need to call virtual destroy_internal method instead of a destructor
  172. bs_delete(mCurrentCapabilities);
  173. mCurrentCapabilities = nullptr;
  174. }
  175. SPtr<RenderWindow> RenderAPICore::initialize(const RENDER_WINDOW_DESC& primaryWindowDesc)
  176. {
  177. gCoreThread().queueCommand(std::bind(&RenderAPICore::initializePrepare, this), true);
  178. RENDER_WINDOW_DESC windowDesc = primaryWindowDesc;
  179. SPtr<RenderWindow> renderWindow = RenderWindow::create(windowDesc, nullptr);
  180. gCoreThread().queueCommand(std::bind(&RenderAPICore::initializeFinalize, this, renderWindow->getCore()), true);
  181. return renderWindow;
  182. }
  183. void RenderAPICore::initializePrepare()
  184. {
  185. // Do nothing
  186. }
  187. void RenderAPICore::initializeFinalize(const SPtr<RenderWindowCore>& primaryWindow)
  188. {
  189. THROW_IF_NOT_CORE_THREAD;
  190. }
  191. void RenderAPICore::destroy()
  192. {
  193. gCoreAccessor().queueCommand(std::bind(&RenderAPICore::destroyCore, this));
  194. gCoreThread().submitAccessors(true);
  195. }
  196. void RenderAPICore::destroyCore()
  197. {
  198. mActiveRenderTarget = nullptr;
  199. }
  200. const DriverVersion& RenderAPICore::getDriverVersion(void) const
  201. {
  202. THROW_IF_NOT_CORE_THREAD;
  203. return mDriverVersion;
  204. }
  205. }