BsRenderAPI.cpp 15 KB

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