BsRenderAPI.cpp 14 KB

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