BsRenderAPI.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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::bindGpuParams(CoreAccessor& accessor, GpuProgramType gptype, const GpuParamsPtr& params)
  110. {
  111. accessor.queueCommand(std::bind(&RenderAPICore::bindGpuParams, 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, sNullTexPtr);
  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::swapBuffers(const SPtr<RenderTargetCore>& target)
  336. {
  337. THROW_IF_NOT_CORE_THREAD;
  338. target->swapBuffers();
  339. BS_INC_RENDER_STAT(NumPresents);
  340. }
  341. }