BsRenderAPI.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. 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::bindGpuParams(CoreAccessor& accessor, GpuProgramType gptype, const GpuParamsPtr& params)
  109. {
  110. accessor.queueCommand(std::bind(&RenderAPICore::bindGpuParams, RenderAPICore::instancePtr(), gptype, params->getCore()));
  111. }
  112. void RenderAPI::beginRender(CoreAccessor& accessor)
  113. {
  114. accessor.queueCommand(std::bind(&RenderAPICore::beginFrame, RenderAPICore::instancePtr()));
  115. }
  116. void RenderAPI::endRender(CoreAccessor& accessor)
  117. {
  118. accessor.queueCommand(std::bind(&RenderAPICore::endFrame, RenderAPICore::instancePtr()));
  119. }
  120. void RenderAPI::clearRenderTarget(CoreAccessor& accessor, UINT32 buffers, const Color& color, float depth, UINT16 stencil)
  121. {
  122. accessor.queueCommand(std::bind(&RenderAPICore::clearRenderTarget, RenderAPICore::instancePtr(), buffers, color, depth, stencil));
  123. }
  124. void RenderAPI::clearViewport(CoreAccessor& accessor, UINT32 buffers, const Color& color, float depth, UINT16 stencil)
  125. {
  126. accessor.queueCommand(std::bind(&RenderAPICore::clearViewport, RenderAPICore::instancePtr(), buffers, color, depth, stencil));
  127. }
  128. void RenderAPI::swapBuffers(CoreAccessor& accessor, const RenderTargetPtr& target)
  129. {
  130. accessor.queueCommand(std::bind(&RenderAPICore::swapBuffers, RenderAPICore::instancePtr(), target->getCore()));
  131. }
  132. void RenderAPI::draw(CoreAccessor& accessor, UINT32 vertexOffset, UINT32 vertexCount)
  133. {
  134. accessor.queueCommand(std::bind(&RenderAPICore::draw, RenderAPICore::instancePtr(), vertexOffset, vertexCount));
  135. }
  136. void RenderAPI::drawIndexed(CoreAccessor& accessor, UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount)
  137. {
  138. accessor.queueCommand(std::bind(&RenderAPICore::drawIndexed, RenderAPICore::instancePtr(), startIndex, indexCount, vertexOffset, vertexCount));
  139. }
  140. const VideoModeInfo& RenderAPI::getVideoModeInfo()
  141. {
  142. return RenderAPICore::instance().getVideoModeInfo();
  143. }
  144. VertexElementType RenderAPI::getColorVertexElementType()
  145. {
  146. return RenderAPICore::instance().getColorVertexElementType();
  147. }
  148. void RenderAPI::convertProjectionMatrix(const Matrix4& matrix, Matrix4& dest)
  149. {
  150. RenderAPICore::instance().convertProjectionMatrix(matrix, dest);
  151. }
  152. float RenderAPI::getHorizontalTexelOffset()
  153. {
  154. return RenderAPICore::instance().getHorizontalTexelOffset();
  155. }
  156. float RenderAPI::getVerticalTexelOffset()
  157. {
  158. return RenderAPICore::instance().getVerticalTexelOffset();
  159. }
  160. float RenderAPI::getMinimumDepthInputValue()
  161. {
  162. return RenderAPICore::instance().getMinimumDepthInputValue();
  163. }
  164. float RenderAPI::getMaximumDepthInputValue()
  165. {
  166. return RenderAPICore::instance().getMaximumDepthInputValue();
  167. }
  168. bool RenderAPI::getVertexColorFlipRequired()
  169. {
  170. return RenderAPICore::instance().getVertexColorFlipRequired();
  171. }
  172. RenderAPICore::RenderAPICore()
  173. : mCullingMode(CULL_COUNTERCLOCKWISE)
  174. , mDisabledTexUnitsFrom(0)
  175. , mVertexProgramBound(false)
  176. , mGeometryProgramBound(false)
  177. , mFragmentProgramBound(false)
  178. , mDomainProgramBound(false)
  179. , mHullProgramBound(false)
  180. , mComputeProgramBound(false)
  181. , mClipPlanesDirty(true)
  182. , mCurrentCapabilities(nullptr)
  183. {
  184. }
  185. RenderAPICore::~RenderAPICore()
  186. {
  187. // Base classes need to call virtual destroy_internal method instead of a destructor
  188. bs_delete(mCurrentCapabilities);
  189. mCurrentCapabilities = nullptr;
  190. }
  191. RenderWindowPtr RenderAPICore::initialize(const RENDER_WINDOW_DESC& primaryWindowDesc)
  192. {
  193. gCoreThread().queueCommand(std::bind(&RenderAPICore::initializePrepare, this), true);
  194. RENDER_WINDOW_DESC windowDesc = primaryWindowDesc;
  195. RenderWindowPtr renderWindow = RenderWindow::create(windowDesc, nullptr);
  196. gCoreThread().queueCommand(std::bind(&RenderAPICore::initializeFinalize, this, renderWindow->getCore()), true);
  197. return renderWindow;
  198. }
  199. void RenderAPICore::initializePrepare()
  200. {
  201. // Do nothing
  202. }
  203. void RenderAPICore::initializeFinalize(const SPtr<RenderWindowCore>& primaryWindow)
  204. {
  205. THROW_IF_NOT_CORE_THREAD;
  206. mVertexProgramBound = false;
  207. mGeometryProgramBound = false;
  208. mFragmentProgramBound = false;
  209. mDomainProgramBound = false;
  210. mHullProgramBound = false;
  211. mComputeProgramBound = false;
  212. }
  213. void RenderAPICore::destroy()
  214. {
  215. gCoreAccessor().queueCommand(std::bind(&RenderAPICore::destroyCore, this));
  216. gCoreThread().submitAccessors(true);
  217. }
  218. void RenderAPICore::destroyCore()
  219. {
  220. mActiveRenderTarget = nullptr;
  221. }
  222. const RenderAPICapabilities* RenderAPICore::getCapabilities(void) const
  223. {
  224. return mCurrentCapabilities;
  225. }
  226. const DriverVersion& RenderAPICore::getDriverVersion(void) const
  227. {
  228. THROW_IF_NOT_CORE_THREAD;
  229. return mDriverVersion;
  230. }
  231. void RenderAPICore::disableTextureUnit(GpuProgramType gptype, UINT16 texUnit)
  232. {
  233. THROW_IF_NOT_CORE_THREAD;
  234. setTexture(gptype, texUnit, false, sNullTexPtr);
  235. }
  236. void RenderAPICore::addClipPlane (const Plane &p)
  237. {
  238. THROW_IF_NOT_CORE_THREAD;
  239. mClipPlanes.push_back(p);
  240. mClipPlanesDirty = true;
  241. }
  242. void RenderAPICore::setClipPlanes(const PlaneList& clipPlanes)
  243. {
  244. THROW_IF_NOT_CORE_THREAD;
  245. if (clipPlanes != mClipPlanes)
  246. {
  247. mClipPlanes = clipPlanes;
  248. mClipPlanesDirty = true;
  249. }
  250. }
  251. void RenderAPICore::resetClipPlanes()
  252. {
  253. THROW_IF_NOT_CORE_THREAD;
  254. if (!mClipPlanes.empty())
  255. {
  256. mClipPlanes.clear();
  257. mClipPlanesDirty = true;
  258. }
  259. }
  260. void RenderAPICore::bindGpuProgram(const SPtr<GpuProgramCore>& prg)
  261. {
  262. THROW_IF_NOT_CORE_THREAD;
  263. switch(prg->getProperties().getType())
  264. {
  265. case GPT_VERTEX_PROGRAM:
  266. if (!mVertexProgramBound && !mClipPlanes.empty())
  267. mClipPlanesDirty = true;
  268. mVertexProgramBound = true;
  269. break;
  270. case GPT_GEOMETRY_PROGRAM:
  271. mGeometryProgramBound = true;
  272. break;
  273. case GPT_FRAGMENT_PROGRAM:
  274. mFragmentProgramBound = true;
  275. break;
  276. case GPT_DOMAIN_PROGRAM:
  277. mDomainProgramBound = true;
  278. break;
  279. case GPT_HULL_PROGRAM:
  280. mHullProgramBound = true;
  281. break;
  282. case GPT_COMPUTE_PROGRAM:
  283. mComputeProgramBound = true;
  284. break;
  285. }
  286. }
  287. void RenderAPICore::unbindGpuProgram(GpuProgramType gptype)
  288. {
  289. THROW_IF_NOT_CORE_THREAD;
  290. switch(gptype)
  291. {
  292. case GPT_VERTEX_PROGRAM:
  293. if (mVertexProgramBound && !mClipPlanes.empty())
  294. mClipPlanesDirty = true;
  295. mVertexProgramBound = false;
  296. break;
  297. case GPT_GEOMETRY_PROGRAM:
  298. mGeometryProgramBound = false;
  299. break;
  300. case GPT_FRAGMENT_PROGRAM:
  301. mFragmentProgramBound = false;
  302. break;
  303. case GPT_DOMAIN_PROGRAM:
  304. mDomainProgramBound = false;
  305. break;
  306. case GPT_HULL_PROGRAM:
  307. mHullProgramBound = false;
  308. break;
  309. case GPT_COMPUTE_PROGRAM:
  310. mComputeProgramBound = false;
  311. break;
  312. }
  313. }
  314. bool RenderAPICore::isGpuProgramBound(GpuProgramType gptype)
  315. {
  316. THROW_IF_NOT_CORE_THREAD;
  317. switch(gptype)
  318. {
  319. case GPT_VERTEX_PROGRAM:
  320. return mVertexProgramBound;
  321. case GPT_GEOMETRY_PROGRAM:
  322. return mGeometryProgramBound;
  323. case GPT_FRAGMENT_PROGRAM:
  324. return mFragmentProgramBound;
  325. case GPT_DOMAIN_PROGRAM:
  326. return mDomainProgramBound;
  327. case GPT_HULL_PROGRAM:
  328. return mHullProgramBound;
  329. case GPT_COMPUTE_PROGRAM:
  330. return mComputeProgramBound;
  331. }
  332. return false;
  333. }
  334. void RenderAPICore::swapBuffers(const SPtr<RenderTargetCore>& target)
  335. {
  336. THROW_IF_NOT_CORE_THREAD;
  337. target->swapBuffers();
  338. BS_INC_RENDER_STAT(NumPresents);
  339. }
  340. }