BsRenderAPI.cpp 14 KB

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