BsRenderAPI.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 "BsShader.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, texPtr->getCore()));
  25. }
  26. void RenderAPI::setLoadStoreTexture(CoreAccessor& accessor, GpuProgramType gptype, UINT16 unit, bool enabled, const SPtr<Texture>& texPtr,
  27. const TextureSurface& surface)
  28. {
  29. accessor.queueCommand(std::bind(&RenderAPICore::setLoadStoreTexture, RenderAPICore::instancePtr(), gptype, unit, enabled, texPtr->getCore(),
  30. surface));
  31. }
  32. void RenderAPI::setBuffer(CoreAccessor& accessor, GpuProgramType gptype, UINT16 unit, const SPtr<GpuBuffer>& buffer,
  33. bool loadStore)
  34. {
  35. accessor.queueCommand(std::bind(&RenderAPICore::setBuffer, RenderAPICore::instancePtr(), gptype, unit,
  36. buffer->getCore(), loadStore));
  37. }
  38. void RenderAPI::setSamplerState(CoreAccessor& accessor, GpuProgramType gptype, UINT16 texUnit, const SPtr<SamplerState>& samplerState)
  39. {
  40. accessor.queueCommand(std::bind(&RenderAPICore::setSamplerState, RenderAPICore::instancePtr(), gptype, texUnit, samplerState->getCore()));
  41. }
  42. void RenderAPI::setBlendState(CoreAccessor& accessor, const SPtr<BlendState>& blendState)
  43. {
  44. accessor.queueCommand(std::bind(&RenderAPICore::setBlendState, RenderAPICore::instancePtr(), blendState->getCore()));
  45. }
  46. void RenderAPI::setRasterizerState(CoreAccessor& accessor, const SPtr<RasterizerState>& rasterizerState)
  47. {
  48. accessor.queueCommand(std::bind(&RenderAPICore::setRasterizerState, RenderAPICore::instancePtr(), rasterizerState->getCore()));
  49. }
  50. void RenderAPI::setDepthStencilState(CoreAccessor& accessor, const SPtr<DepthStencilState>& depthStencilState, UINT32 stencilRefValue)
  51. {
  52. accessor.queueCommand(std::bind(&RenderAPICore::setDepthStencilState, RenderAPICore::instancePtr(), depthStencilState->getCore(), stencilRefValue));
  53. }
  54. void RenderAPI::setVertexBuffers(CoreAccessor& accessor, UINT32 index, const Vector<SPtr<VertexBuffer>>& buffers)
  55. {
  56. Vector<SPtr<VertexBufferCore>> coreBuffers(buffers.size());
  57. for (UINT32 i = 0; i < (UINT32)buffers.size(); i++)
  58. coreBuffers[i] = buffers[i] != nullptr ? buffers[i]->getCore() : nullptr;
  59. std::function<void(RenderAPICore*, UINT32, const Vector<SPtr<VertexBufferCore>>&)> resizeFunc =
  60. [](RenderAPICore* rs, UINT32 idx, const Vector<SPtr<VertexBufferCore>>& _buffers)
  61. {
  62. rs->setVertexBuffers(idx, (SPtr<VertexBufferCore>*)_buffers.data(), (UINT32)_buffers.size());
  63. };
  64. accessor.queueCommand(std::bind(resizeFunc, RenderAPICore::instancePtr(), index, coreBuffers));
  65. }
  66. void RenderAPI::setIndexBuffer(CoreAccessor& accessor, const SPtr<IndexBuffer>& buffer)
  67. {
  68. accessor.queueCommand(std::bind(&RenderAPICore::setIndexBuffer, RenderAPICore::instancePtr(), buffer->getCore()));
  69. }
  70. void RenderAPI::setVertexDeclaration(CoreAccessor& accessor, const SPtr<VertexDeclaration>& vertexDeclaration)
  71. {
  72. accessor.queueCommand(std::bind(&RenderAPICore::setVertexDeclaration, RenderAPICore::instancePtr(), vertexDeclaration->getCore()));
  73. }
  74. void RenderAPI::setViewport(CoreAccessor& accessor, const Rect2& vp)
  75. {
  76. accessor.queueCommand(std::bind(&RenderAPICore::setViewport, RenderAPICore::instancePtr(), vp));
  77. }
  78. void RenderAPI::setDrawOperation(CoreAccessor& accessor, DrawOperationType op)
  79. {
  80. accessor.queueCommand(std::bind(&RenderAPICore::setDrawOperation, RenderAPICore::instancePtr(), op));
  81. }
  82. void RenderAPI::setClipPlanes(CoreAccessor& accessor, const PlaneList& clipPlanes)
  83. {
  84. accessor.queueCommand(std::bind(&RenderAPICore::setClipPlanes, RenderAPICore::instancePtr(), clipPlanes));
  85. }
  86. void RenderAPI::addClipPlane(CoreAccessor& accessor, const Plane& p)
  87. {
  88. accessor.queueCommand(std::bind(&RenderAPICore::addClipPlane, RenderAPICore::instancePtr(), p));
  89. }
  90. void RenderAPI::resetClipPlanes(CoreAccessor& accessor)
  91. {
  92. accessor.queueCommand(std::bind(&RenderAPICore::resetClipPlanes, RenderAPICore::instancePtr()));
  93. }
  94. void RenderAPI::setScissorRect(CoreAccessor& accessor, UINT32 left, UINT32 top, UINT32 right, UINT32 bottom)
  95. {
  96. accessor.queueCommand(std::bind(&RenderAPICore::setScissorRect, RenderAPICore::instancePtr(), left, top, right, bottom));
  97. }
  98. void RenderAPI::setRenderTarget(CoreAccessor& accessor, const SPtr<RenderTarget>& target, bool readOnlyDepthStencil)
  99. {
  100. accessor.queueCommand(std::bind(&RenderAPICore::setRenderTarget,
  101. RenderAPICore::instancePtr(), target->getCore(), readOnlyDepthStencil));
  102. }
  103. void RenderAPI::bindGpuProgram(CoreAccessor& accessor, const SPtr<GpuProgram>& prg)
  104. {
  105. prg->syncToCore(accessor);
  106. accessor.queueCommand(std::bind(&RenderAPICore::bindGpuProgram, RenderAPICore::instancePtr(), prg->getCore()));
  107. }
  108. void RenderAPI::unbindGpuProgram(CoreAccessor& accessor, GpuProgramType gptype)
  109. {
  110. accessor.queueCommand(std::bind(&RenderAPICore::unbindGpuProgram, RenderAPICore::instancePtr(), gptype));
  111. }
  112. void RenderAPI::beginRender(CoreAccessor& accessor)
  113. {
  114. accessor.queueCommand(std::bind(&RenderAPICore::beginFrame, RenderAPICore::instancePtr()));
  115. }
  116. void RenderAPI::endRender(CoreAccessor& accessor)
  117. {
  118. accessor.queueCommand(std::bind(&RenderAPICore::endFrame, RenderAPICore::instancePtr()));
  119. }
  120. void RenderAPI::clearRenderTarget(CoreAccessor& accessor, UINT32 buffers, const Color& color, float depth, UINT16 stencil, UINT8 targetMask)
  121. {
  122. accessor.queueCommand(std::bind(&RenderAPICore::clearRenderTarget, RenderAPICore::instancePtr(), buffers, color,
  123. depth, stencil, targetMask));
  124. }
  125. void RenderAPI::clearViewport(CoreAccessor& accessor, UINT32 buffers, const Color& color, float depth, UINT16 stencil, UINT8 targetMask)
  126. {
  127. accessor.queueCommand(std::bind(&RenderAPICore::clearViewport, RenderAPICore::instancePtr(), buffers, color, depth,
  128. stencil, targetMask));
  129. }
  130. void RenderAPI::swapBuffers(CoreAccessor& accessor, const SPtr<RenderTarget>& target)
  131. {
  132. accessor.queueCommand(std::bind(&RenderAPICore::swapBuffers, RenderAPICore::instancePtr(), target->getCore()));
  133. }
  134. void RenderAPI::draw(CoreAccessor& accessor, UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount)
  135. {
  136. accessor.queueCommand(std::bind(&RenderAPICore::draw, RenderAPICore::instancePtr(), vertexOffset,
  137. vertexCount, instanceCount));
  138. }
  139. void RenderAPI::drawIndexed(CoreAccessor& accessor, UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset,
  140. UINT32 vertexCount, UINT32 instanceCount)
  141. {
  142. accessor.queueCommand(std::bind(&RenderAPICore::drawIndexed, RenderAPICore::instancePtr(), startIndex, indexCount,
  143. vertexOffset, vertexCount, instanceCount));
  144. }
  145. void RenderAPI::dispatchCompute(CoreAccessor& accessor, UINT32 numGroupsX, UINT32 numGroupsY, UINT32 numGroupsZ)
  146. {
  147. accessor.queueCommand(std::bind(&RenderAPICore::dispatchCompute, RenderAPICore::instancePtr(), numGroupsX,
  148. numGroupsY, numGroupsZ));
  149. }
  150. const VideoModeInfo& RenderAPI::getVideoModeInfo()
  151. {
  152. return RenderAPICore::instance().getVideoModeInfo();
  153. }
  154. void RenderAPI::convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest)
  155. {
  156. RenderAPICore::instance().convertProjectionMatrix(matrix, dest);
  157. }
  158. const RenderAPIInfo& RenderAPI::getAPIInfo()
  159. {
  160. return RenderAPICore::instance().getAPIInfo();
  161. }
  162. RenderAPICore::RenderAPICore()
  163. : mCullingMode(CULL_COUNTERCLOCKWISE)
  164. , mDisabledTexUnitsFrom(0)
  165. , mVertexProgramBound(false)
  166. , mGeometryProgramBound(false)
  167. , mFragmentProgramBound(false)
  168. , mDomainProgramBound(false)
  169. , mHullProgramBound(false)
  170. , mComputeProgramBound(false)
  171. , mClipPlanesDirty(true)
  172. , mCurrentCapabilities(nullptr)
  173. {
  174. }
  175. RenderAPICore::~RenderAPICore()
  176. {
  177. // Base classes need to call virtual destroy_internal method instead of a destructor
  178. bs_delete(mCurrentCapabilities);
  179. mCurrentCapabilities = nullptr;
  180. }
  181. SPtr<RenderWindow> RenderAPICore::initialize(const RENDER_WINDOW_DESC& primaryWindowDesc)
  182. {
  183. gCoreThread().queueCommand(std::bind(&RenderAPICore::initializePrepare, this), true);
  184. RENDER_WINDOW_DESC windowDesc = primaryWindowDesc;
  185. SPtr<RenderWindow> renderWindow = RenderWindow::create(windowDesc, nullptr);
  186. gCoreThread().queueCommand(std::bind(&RenderAPICore::initializeFinalize, this, renderWindow->getCore()), true);
  187. return renderWindow;
  188. }
  189. void RenderAPICore::initializePrepare()
  190. {
  191. // Do nothing
  192. }
  193. void RenderAPICore::initializeFinalize(const SPtr<RenderWindowCore>& primaryWindow)
  194. {
  195. THROW_IF_NOT_CORE_THREAD;
  196. mVertexProgramBound = false;
  197. mGeometryProgramBound = false;
  198. mFragmentProgramBound = false;
  199. mDomainProgramBound = false;
  200. mHullProgramBound = false;
  201. mComputeProgramBound = false;
  202. }
  203. void RenderAPICore::destroy()
  204. {
  205. gCoreAccessor().queueCommand(std::bind(&RenderAPICore::destroyCore, this));
  206. gCoreThread().submitAccessors(true);
  207. }
  208. void RenderAPICore::destroyCore()
  209. {
  210. mActiveRenderTarget = nullptr;
  211. }
  212. const RenderAPICapabilities* RenderAPICore::getCapabilities(void) const
  213. {
  214. return mCurrentCapabilities;
  215. }
  216. const DriverVersion& RenderAPICore::getDriverVersion(void) const
  217. {
  218. THROW_IF_NOT_CORE_THREAD;
  219. return mDriverVersion;
  220. }
  221. void RenderAPICore::addClipPlane(const Plane &p)
  222. {
  223. THROW_IF_NOT_CORE_THREAD;
  224. mClipPlanes.push_back(p);
  225. mClipPlanesDirty = true;
  226. }
  227. void RenderAPICore::setClipPlanes(const PlaneList& clipPlanes)
  228. {
  229. THROW_IF_NOT_CORE_THREAD;
  230. if (clipPlanes != mClipPlanes)
  231. {
  232. mClipPlanes = clipPlanes;
  233. mClipPlanesDirty = true;
  234. }
  235. }
  236. void RenderAPICore::resetClipPlanes()
  237. {
  238. THROW_IF_NOT_CORE_THREAD;
  239. if (!mClipPlanes.empty())
  240. {
  241. mClipPlanes.clear();
  242. mClipPlanesDirty = true;
  243. }
  244. }
  245. void RenderAPICore::bindGpuProgram(const SPtr<GpuProgramCore>& prg)
  246. {
  247. THROW_IF_NOT_CORE_THREAD;
  248. switch(prg->getProperties().getType())
  249. {
  250. case GPT_VERTEX_PROGRAM:
  251. if (!mVertexProgramBound && !mClipPlanes.empty())
  252. mClipPlanesDirty = true;
  253. mVertexProgramBound = true;
  254. break;
  255. case GPT_GEOMETRY_PROGRAM:
  256. mGeometryProgramBound = true;
  257. break;
  258. case GPT_FRAGMENT_PROGRAM:
  259. mFragmentProgramBound = true;
  260. break;
  261. case GPT_DOMAIN_PROGRAM:
  262. mDomainProgramBound = true;
  263. break;
  264. case GPT_HULL_PROGRAM:
  265. mHullProgramBound = true;
  266. break;
  267. case GPT_COMPUTE_PROGRAM:
  268. mComputeProgramBound = true;
  269. break;
  270. }
  271. }
  272. void RenderAPICore::unbindGpuProgram(GpuProgramType gptype)
  273. {
  274. THROW_IF_NOT_CORE_THREAD;
  275. switch(gptype)
  276. {
  277. case GPT_VERTEX_PROGRAM:
  278. if (mVertexProgramBound && !mClipPlanes.empty())
  279. mClipPlanesDirty = true;
  280. mVertexProgramBound = false;
  281. break;
  282. case GPT_GEOMETRY_PROGRAM:
  283. mGeometryProgramBound = false;
  284. break;
  285. case GPT_FRAGMENT_PROGRAM:
  286. mFragmentProgramBound = false;
  287. break;
  288. case GPT_DOMAIN_PROGRAM:
  289. mDomainProgramBound = false;
  290. break;
  291. case GPT_HULL_PROGRAM:
  292. mHullProgramBound = false;
  293. break;
  294. case GPT_COMPUTE_PROGRAM:
  295. mComputeProgramBound = false;
  296. break;
  297. }
  298. }
  299. bool RenderAPICore::isGpuProgramBound(GpuProgramType gptype)
  300. {
  301. THROW_IF_NOT_CORE_THREAD;
  302. switch(gptype)
  303. {
  304. case GPT_VERTEX_PROGRAM:
  305. return mVertexProgramBound;
  306. case GPT_GEOMETRY_PROGRAM:
  307. return mGeometryProgramBound;
  308. case GPT_FRAGMENT_PROGRAM:
  309. return mFragmentProgramBound;
  310. case GPT_DOMAIN_PROGRAM:
  311. return mDomainProgramBound;
  312. case GPT_HULL_PROGRAM:
  313. return mHullProgramBound;
  314. case GPT_COMPUTE_PROGRAM:
  315. return mComputeProgramBound;
  316. }
  317. return false;
  318. }
  319. void RenderAPICore::swapBuffers(const SPtr<RenderTargetCore>& target)
  320. {
  321. THROW_IF_NOT_CORE_THREAD;
  322. target->swapBuffers();
  323. BS_INC_RENDER_STAT(NumPresents);
  324. }
  325. }