2
0

BsRenderAPI.cpp 14 KB

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