BsRenderAPI.cpp 14 KB

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