BsRenderAPI.cpp 14 KB

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