CmRenderSystem.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. // RenderSystem implementation
  25. // Note that most of this class is abstract since
  26. // we cannot know how to implement the behaviour without
  27. // being aware of the 3D API. However there are a few
  28. // simple functions which can have a base implementation
  29. #include "CmRenderSystem.h"
  30. #include "CmCoreThread.h"
  31. #include "CmViewport.h"
  32. #include "CmException.h"
  33. #include "CmRenderTarget.h"
  34. #include "CmRenderWindow.h"
  35. #include "CmPixelBuffer.h"
  36. #include "CmOcclusionQuery.h"
  37. #include "CmGpuResource.h"
  38. #include "CmCoreThread.h"
  39. #include "CmMesh.h"
  40. #include "CmProfiler.h"
  41. using namespace std::placeholders;
  42. namespace BansheeEngine {
  43. static const TexturePtr sNullTexPtr;
  44. RenderSystem::RenderSystem()
  45. : mCullingMode(CULL_COUNTERCLOCKWISE)
  46. , mDisabledTexUnitsFrom(0)
  47. , mVertexProgramBound(false)
  48. , mGeometryProgramBound(false)
  49. , mFragmentProgramBound(false)
  50. , mClipPlanesDirty(true)
  51. , mCurrentCapabilities(nullptr)
  52. {
  53. }
  54. RenderSystem::~RenderSystem()
  55. {
  56. // Base classes need to call virtual destroy_internal method (queue it on core thread)
  57. cm_delete(mCurrentCapabilities);
  58. mCurrentCapabilities = nullptr;
  59. }
  60. RenderWindowPtr RenderSystem::initialize(const RENDER_WINDOW_DESC& primaryWindowDesc)
  61. {
  62. mPrimaryWindowDesc = primaryWindowDesc;
  63. AsyncOp op = gCoreThread().queueReturnCommand(std::bind(&RenderSystem::initialize_internal, this, _1), true);
  64. return op.getReturnValue<RenderWindowPtr>();
  65. }
  66. void RenderSystem::initialize_internal(AsyncOp& asyncOp)
  67. {
  68. THROW_IF_NOT_CORE_THREAD;
  69. mVertexProgramBound = false;
  70. mGeometryProgramBound = false;
  71. mFragmentProgramBound = false;
  72. }
  73. void RenderSystem::destroy()
  74. {
  75. gCoreAccessor().queueCommand(std::bind(&RenderSystem::destroy_internal, this));
  76. gCoreThread().submitAccessors(true);
  77. }
  78. void RenderSystem::destroy_internal()
  79. {
  80. mActiveRenderTarget = nullptr;
  81. }
  82. const RenderSystemCapabilities* RenderSystem::getCapabilities(void) const
  83. {
  84. return mCurrentCapabilities;
  85. }
  86. const DriverVersion& RenderSystem::getDriverVersion(void) const
  87. {
  88. THROW_IF_NOT_CORE_THREAD;
  89. return mDriverVersion;
  90. }
  91. void RenderSystem::disableTextureUnit(GpuProgramType gptype, UINT16 texUnit)
  92. {
  93. THROW_IF_NOT_CORE_THREAD;
  94. setTexture(gptype, texUnit, false, sNullTexPtr);
  95. }
  96. void RenderSystem::addClipPlane (const Plane &p)
  97. {
  98. THROW_IF_NOT_CORE_THREAD;
  99. mClipPlanes.push_back(p);
  100. mClipPlanesDirty = true;
  101. }
  102. void RenderSystem::setClipPlanes(const PlaneList& clipPlanes)
  103. {
  104. THROW_IF_NOT_CORE_THREAD;
  105. if (clipPlanes != mClipPlanes)
  106. {
  107. mClipPlanes = clipPlanes;
  108. mClipPlanesDirty = true;
  109. }
  110. }
  111. void RenderSystem::resetClipPlanes()
  112. {
  113. THROW_IF_NOT_CORE_THREAD;
  114. if (!mClipPlanes.empty())
  115. {
  116. mClipPlanes.clear();
  117. mClipPlanesDirty = true;
  118. }
  119. }
  120. void RenderSystem::bindGpuProgram(HGpuProgram prg)
  121. {
  122. THROW_IF_NOT_CORE_THREAD;
  123. switch(prg->getBindingDelegate()->getType())
  124. {
  125. case GPT_VERTEX_PROGRAM:
  126. // mark clip planes dirty if changed (programmable can change space)
  127. if (!mVertexProgramBound && !mClipPlanes.empty())
  128. mClipPlanesDirty = true;
  129. mVertexProgramBound = true;
  130. break;
  131. case GPT_GEOMETRY_PROGRAM:
  132. mGeometryProgramBound = true;
  133. break;
  134. case GPT_FRAGMENT_PROGRAM:
  135. mFragmentProgramBound = true;
  136. break;
  137. }
  138. }
  139. void RenderSystem::unbindGpuProgram(GpuProgramType gptype)
  140. {
  141. THROW_IF_NOT_CORE_THREAD;
  142. switch(gptype)
  143. {
  144. case GPT_VERTEX_PROGRAM:
  145. // mark clip planes dirty if changed (programmable can change space)
  146. if (mVertexProgramBound && !mClipPlanes.empty())
  147. mClipPlanesDirty = true;
  148. mVertexProgramBound = false;
  149. break;
  150. case GPT_GEOMETRY_PROGRAM:
  151. mGeometryProgramBound = false;
  152. break;
  153. case GPT_FRAGMENT_PROGRAM:
  154. mFragmentProgramBound = false;
  155. break;
  156. }
  157. }
  158. bool RenderSystem::isGpuProgramBound(GpuProgramType gptype)
  159. {
  160. THROW_IF_NOT_CORE_THREAD;
  161. switch(gptype)
  162. {
  163. case GPT_VERTEX_PROGRAM:
  164. return mVertexProgramBound;
  165. case GPT_GEOMETRY_PROGRAM:
  166. return mGeometryProgramBound;
  167. case GPT_FRAGMENT_PROGRAM:
  168. return mFragmentProgramBound;
  169. }
  170. return false;
  171. }
  172. void RenderSystem::render(const MeshBasePtr& mesh, UINT32 indexOffset, UINT32 indexCount, bool useIndices, DrawOperationType drawOp)
  173. {
  174. THROW_IF_NOT_CORE_THREAD;
  175. gProfiler().beginSample("render");
  176. // sort out clip planes
  177. // have to do it here in case of matrix issues
  178. if (mClipPlanesDirty)
  179. {
  180. setClipPlanesImpl(mClipPlanes);
  181. mClipPlanesDirty = false;
  182. }
  183. std::shared_ptr<VertexData> vertexData = mesh->getVertexData();
  184. setVertexDeclaration(vertexData->vertexDeclaration);
  185. auto vertexBuffers = vertexData->getBuffers();
  186. if(vertexBuffers.size() > 0)
  187. {
  188. VertexBufferPtr buffers[MAX_BOUND_VERTEX_BUFFERS];
  189. UINT32 endSlot = 0;
  190. UINT32 startSlot = MAX_BOUND_VERTEX_BUFFERS;
  191. for(auto iter = vertexBuffers.begin(); iter != vertexBuffers.end() ; ++iter)
  192. {
  193. if(iter->first >= MAX_BOUND_VERTEX_BUFFERS)
  194. CM_EXCEPT(InvalidParametersException, "Buffer index out of range");
  195. startSlot = std::min(iter->first, startSlot);
  196. endSlot = std::max(iter->first, endSlot);
  197. }
  198. for(auto iter = vertexBuffers.begin(); iter != vertexBuffers.end() ; ++iter)
  199. {
  200. buffers[iter->first - startSlot] = iter->second;
  201. }
  202. setVertexBuffers(startSlot, buffers, endSlot - startSlot + 1);
  203. }
  204. setDrawOperation(drawOp);
  205. if (useIndices)
  206. {
  207. std::shared_ptr<IndexData> indexData = mesh->getIndexData();
  208. if(indexCount == 0)
  209. indexCount = indexData->indexCount;
  210. setIndexBuffer(indexData->indexBuffer);
  211. drawIndexed(indexOffset + mesh->getIndexOffset(), indexCount, mesh->getVertexOffset(), vertexData->vertexCount);
  212. }
  213. else
  214. draw(mesh->getVertexOffset(), vertexData->vertexCount);
  215. mesh->notifyUsedOnGPU();
  216. gProfiler().endSample("render");
  217. }
  218. void RenderSystem::swapBuffers(RenderTargetPtr target)
  219. {
  220. THROW_IF_NOT_CORE_THREAD;
  221. if(target->isInitialized())
  222. target->swapBuffers();
  223. }
  224. void RenderSystem::writeSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, const GpuResourceDataPtr& data, bool discardEntireBuffer, AsyncOp& asyncOp)
  225. {
  226. THROW_IF_NOT_CORE_THREAD;
  227. gProfiler().beginSample("writeSubresource");
  228. resource->writeSubresource(subresourceIdx, *data, discardEntireBuffer);
  229. gProfiler().endSample("writeSubresource");
  230. gProfiler().beginSample("writeSubresourceB");
  231. data->unlock();
  232. asyncOp._completeOperation();
  233. gProfiler().endSample("writeSubresourceB");
  234. }
  235. void RenderSystem::readSubresource(GpuResourcePtr resource, UINT32 subresourceIdx, GpuResourceDataPtr& data, AsyncOp& asyncOp)
  236. {
  237. THROW_IF_NOT_CORE_THREAD;
  238. gProfiler().beginSample("readSubresource");
  239. resource->readSubresource(subresourceIdx, *data);
  240. data->unlock();
  241. asyncOp._completeOperation();
  242. gProfiler().endSample("readSubresource");
  243. }
  244. }