BsRenderAPI.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 TexturePtr &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 TexturePtr& 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 SamplerStatePtr& samplerState)
  34. {
  35. accessor.queueCommand(std::bind(&RenderAPICore::setSamplerState, RenderAPICore::instancePtr(), gptype, texUnit, samplerState->getCore()));
  36. }
  37. void RenderAPI::setBlendState(CoreAccessor& accessor, const BlendStatePtr& blendState)
  38. {
  39. accessor.queueCommand(std::bind(&RenderAPICore::setBlendState, RenderAPICore::instancePtr(), blendState->getCore()));
  40. }
  41. void RenderAPI::setRasterizerState(CoreAccessor& accessor, const RasterizerStatePtr& rasterizerState)
  42. {
  43. accessor.queueCommand(std::bind(&RenderAPICore::setRasterizerState, RenderAPICore::instancePtr(), rasterizerState->getCore()));
  44. }
  45. void RenderAPI::setDepthStencilState(CoreAccessor& accessor, const DepthStencilStatePtr& 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<VertexBufferPtr>& 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 IndexBufferPtr& buffer)
  62. {
  63. accessor.queueCommand(std::bind(&RenderAPICore::setIndexBuffer, RenderAPICore::instancePtr(), buffer->getCore()));
  64. }
  65. void RenderAPI::setVertexDeclaration(CoreAccessor& accessor, const VertexDeclarationPtr& 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::setScissorTest(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 RenderTargetPtr& 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 GpuProgramPtr& 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 GpuParamsPtr& params)
  108. {
  109. accessor.queueCommand(std::bind(&RenderAPICore::setConstantBuffers, RenderAPICore::instancePtr(), gptype, params->getCore()));
  110. }
  111. void RenderAPI::setGpuParams(CoreAccessor& accessor, GpuProgramType gptype, const GpuParamsPtr& 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 RenderTargetPtr& 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. VertexElementType RenderAPI::getColorVertexElementType()
  156. {
  157. return RenderAPICore::instance().getColorVertexElementType();
  158. }
  159. void RenderAPI::convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest)
  160. {
  161. RenderAPICore::instance().convertProjectionMatrix(matrix, dest);
  162. }
  163. float RenderAPI::getHorizontalTexelOffset()
  164. {
  165. return RenderAPICore::instance().getHorizontalTexelOffset();
  166. }
  167. float RenderAPI::getVerticalTexelOffset()
  168. {
  169. return RenderAPICore::instance().getVerticalTexelOffset();
  170. }
  171. float RenderAPI::getMinimumDepthInputValue()
  172. {
  173. return RenderAPICore::instance().getMinimumDepthInputValue();
  174. }
  175. float RenderAPI::getMaximumDepthInputValue()
  176. {
  177. return RenderAPICore::instance().getMaximumDepthInputValue();
  178. }
  179. bool RenderAPI::getVertexColorFlipRequired()
  180. {
  181. return RenderAPICore::instance().getVertexColorFlipRequired();
  182. }
  183. RenderAPICore::RenderAPICore()
  184. : mCullingMode(CULL_COUNTERCLOCKWISE)
  185. , mDisabledTexUnitsFrom(0)
  186. , mVertexProgramBound(false)
  187. , mGeometryProgramBound(false)
  188. , mFragmentProgramBound(false)
  189. , mDomainProgramBound(false)
  190. , mHullProgramBound(false)
  191. , mComputeProgramBound(false)
  192. , mClipPlanesDirty(true)
  193. , mCurrentCapabilities(nullptr)
  194. {
  195. }
  196. RenderAPICore::~RenderAPICore()
  197. {
  198. // Base classes need to call virtual destroy_internal method instead of a destructor
  199. bs_delete(mCurrentCapabilities);
  200. mCurrentCapabilities = nullptr;
  201. }
  202. RenderWindowPtr RenderAPICore::initialize(const RENDER_WINDOW_DESC& primaryWindowDesc)
  203. {
  204. gCoreThread().queueCommand(std::bind(&RenderAPICore::initializePrepare, this), true);
  205. RENDER_WINDOW_DESC windowDesc = primaryWindowDesc;
  206. RenderWindowPtr renderWindow = RenderWindow::create(windowDesc, nullptr);
  207. gCoreThread().queueCommand(std::bind(&RenderAPICore::initializeFinalize, this, renderWindow->getCore()), true);
  208. return renderWindow;
  209. }
  210. void RenderAPICore::initializePrepare()
  211. {
  212. // Do nothing
  213. }
  214. void RenderAPICore::initializeFinalize(const SPtr<RenderWindowCore>& primaryWindow)
  215. {
  216. THROW_IF_NOT_CORE_THREAD;
  217. mVertexProgramBound = false;
  218. mGeometryProgramBound = false;
  219. mFragmentProgramBound = false;
  220. mDomainProgramBound = false;
  221. mHullProgramBound = false;
  222. mComputeProgramBound = false;
  223. }
  224. void RenderAPICore::destroy()
  225. {
  226. gCoreAccessor().queueCommand(std::bind(&RenderAPICore::destroyCore, this));
  227. gCoreThread().submitAccessors(true);
  228. }
  229. void RenderAPICore::destroyCore()
  230. {
  231. mActiveRenderTarget = nullptr;
  232. }
  233. const RenderAPICapabilities* RenderAPICore::getCapabilities(void) const
  234. {
  235. return mCurrentCapabilities;
  236. }
  237. const DriverVersion& RenderAPICore::getDriverVersion(void) const
  238. {
  239. THROW_IF_NOT_CORE_THREAD;
  240. return mDriverVersion;
  241. }
  242. void RenderAPICore::disableTextureUnit(GpuProgramType gptype, UINT16 texUnit)
  243. {
  244. THROW_IF_NOT_CORE_THREAD;
  245. setTexture(gptype, texUnit, false, SPtr<TextureCore>());
  246. }
  247. void RenderAPICore::addClipPlane(const Plane &p)
  248. {
  249. THROW_IF_NOT_CORE_THREAD;
  250. mClipPlanes.push_back(p);
  251. mClipPlanesDirty = true;
  252. }
  253. void RenderAPICore::setClipPlanes(const PlaneList& clipPlanes)
  254. {
  255. THROW_IF_NOT_CORE_THREAD;
  256. if (clipPlanes != mClipPlanes)
  257. {
  258. mClipPlanes = clipPlanes;
  259. mClipPlanesDirty = true;
  260. }
  261. }
  262. void RenderAPICore::resetClipPlanes()
  263. {
  264. THROW_IF_NOT_CORE_THREAD;
  265. if (!mClipPlanes.empty())
  266. {
  267. mClipPlanes.clear();
  268. mClipPlanesDirty = true;
  269. }
  270. }
  271. void RenderAPICore::bindGpuProgram(const SPtr<GpuProgramCore>& prg)
  272. {
  273. THROW_IF_NOT_CORE_THREAD;
  274. switch(prg->getProperties().getType())
  275. {
  276. case GPT_VERTEX_PROGRAM:
  277. if (!mVertexProgramBound && !mClipPlanes.empty())
  278. mClipPlanesDirty = true;
  279. mVertexProgramBound = true;
  280. break;
  281. case GPT_GEOMETRY_PROGRAM:
  282. mGeometryProgramBound = true;
  283. break;
  284. case GPT_FRAGMENT_PROGRAM:
  285. mFragmentProgramBound = true;
  286. break;
  287. case GPT_DOMAIN_PROGRAM:
  288. mDomainProgramBound = true;
  289. break;
  290. case GPT_HULL_PROGRAM:
  291. mHullProgramBound = true;
  292. break;
  293. case GPT_COMPUTE_PROGRAM:
  294. mComputeProgramBound = true;
  295. break;
  296. }
  297. }
  298. void RenderAPICore::unbindGpuProgram(GpuProgramType gptype)
  299. {
  300. THROW_IF_NOT_CORE_THREAD;
  301. switch(gptype)
  302. {
  303. case GPT_VERTEX_PROGRAM:
  304. if (mVertexProgramBound && !mClipPlanes.empty())
  305. mClipPlanesDirty = true;
  306. mVertexProgramBound = false;
  307. break;
  308. case GPT_GEOMETRY_PROGRAM:
  309. mGeometryProgramBound = false;
  310. break;
  311. case GPT_FRAGMENT_PROGRAM:
  312. mFragmentProgramBound = false;
  313. break;
  314. case GPT_DOMAIN_PROGRAM:
  315. mDomainProgramBound = false;
  316. break;
  317. case GPT_HULL_PROGRAM:
  318. mHullProgramBound = false;
  319. break;
  320. case GPT_COMPUTE_PROGRAM:
  321. mComputeProgramBound = false;
  322. break;
  323. }
  324. }
  325. bool RenderAPICore::isGpuProgramBound(GpuProgramType gptype)
  326. {
  327. THROW_IF_NOT_CORE_THREAD;
  328. switch(gptype)
  329. {
  330. case GPT_VERTEX_PROGRAM:
  331. return mVertexProgramBound;
  332. case GPT_GEOMETRY_PROGRAM:
  333. return mGeometryProgramBound;
  334. case GPT_FRAGMENT_PROGRAM:
  335. return mFragmentProgramBound;
  336. case GPT_DOMAIN_PROGRAM:
  337. return mDomainProgramBound;
  338. case GPT_HULL_PROGRAM:
  339. return mHullProgramBound;
  340. case GPT_COMPUTE_PROGRAM:
  341. return mComputeProgramBound;
  342. }
  343. return false;
  344. }
  345. void RenderAPICore::setGpuParams(GpuProgramType gptype, const SPtr<GpuParamsCore>& params)
  346. {
  347. const GpuParamDesc& paramDesc = params->getParamDesc();
  348. for (auto iter = paramDesc.samplers.begin(); iter != paramDesc.samplers.end(); ++iter)
  349. {
  350. SPtr<SamplerStateCore> samplerState = params->getSamplerState(iter->second.slot);
  351. if (samplerState == nullptr)
  352. setSamplerState(gptype, iter->second.slot, SamplerStateCore::getDefault());
  353. else
  354. setSamplerState(gptype, iter->second.slot, samplerState);
  355. }
  356. for (auto iter = paramDesc.textures.begin(); iter != paramDesc.textures.end(); ++iter)
  357. {
  358. SPtr<TextureCore> texture = params->getTexture(iter->second.slot);
  359. if (!params->isLoadStoreTexture(iter->second.slot))
  360. {
  361. if (texture == nullptr)
  362. setTexture(gptype, iter->second.slot, false, nullptr);
  363. else
  364. setTexture(gptype, iter->second.slot, true, texture);
  365. }
  366. else
  367. {
  368. const TextureSurface& surface = params->getLoadStoreSurface(iter->second.slot);
  369. if (texture == nullptr)
  370. setLoadStoreTexture(gptype, iter->second.slot, false, nullptr, surface);
  371. else
  372. setLoadStoreTexture(gptype, iter->second.slot, true, texture, surface);
  373. }
  374. }
  375. setConstantBuffers(gptype, params);
  376. }
  377. void RenderAPICore::swapBuffers(const SPtr<RenderTargetCore>& target)
  378. {
  379. THROW_IF_NOT_CORE_THREAD;
  380. target->swapBuffers();
  381. BS_INC_RENDER_STAT(NumPresents);
  382. }
  383. }