BsGLRenderSystem.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsGLPrerequisites.h"
  6. #include "BsRenderSystem.h"
  7. #include "BsGLHardwareBufferManager.h"
  8. #include "BsGLSLProgramFactory.h"
  9. #include "BsVector4.h"
  10. namespace BansheeEngine
  11. {
  12. /**
  13. * @brief Implementation of a render system using OpenGL. Provides abstracted
  14. * access to various low level OpenGL methods.
  15. */
  16. class BS_RSGL_EXPORT GLRenderSystem : public RenderSystem
  17. {
  18. public:
  19. GLRenderSystem();
  20. ~GLRenderSystem();
  21. /**
  22. * @copydoc RenderSystem::getName()
  23. */
  24. const String& getName() const;
  25. /**
  26. * @copydoc RenderSystem::getShadingLanguageName()
  27. */
  28. const String& getShadingLanguageName() const;
  29. /**
  30. * @copydoc RenderSystem::setRenderTarget()
  31. */
  32. void setRenderTarget(RenderTargetPtr target);
  33. /**
  34. * @copydoc RenderSystem::setVertexBuffers()
  35. */
  36. void setVertexBuffers(UINT32 index, VertexBufferPtr* buffers, UINT32 numBuffers);
  37. /**
  38. * @copydoc RenderSystem::setIndexBuffer()
  39. */
  40. void setIndexBuffer(const IndexBufferPtr& buffer);
  41. /**
  42. * @copydoc RenderSystem::setVertexDeclaration()
  43. */
  44. void setVertexDeclaration(VertexDeclarationPtr vertexDeclaration);
  45. /**
  46. * @copydoc RenderSystem::setDrawOperation()
  47. */
  48. void setDrawOperation(DrawOperationType op);
  49. /**
  50. * @copydoc RenderSystem::setScissorRect()
  51. */
  52. void setScissorRect(UINT32 left, UINT32 top, UINT32 right, UINT32 bottom);
  53. /**
  54. * @copydoc RenderSystem::setTexture()
  55. */
  56. void setTexture(GpuProgramType gptype, UINT16 unit, bool enabled, const TexturePtr &tex);
  57. /**
  58. * @copydoc RenderSystem::setSamplerState()
  59. */
  60. void setSamplerState(GpuProgramType gptype, UINT16 unit, const SamplerStatePtr& state);
  61. /**
  62. * @copydoc RenderSystem::setBlendState()
  63. */
  64. void setBlendState(const BlendStatePtr& blendState);
  65. /**
  66. * @copydoc RenderSystem::setRasterizerState()
  67. */
  68. void setRasterizerState(const RasterizerStatePtr& rasterizerState);
  69. /**
  70. * @copydoc RenderSystem::setDepthStencilState()
  71. */
  72. void setDepthStencilState(const DepthStencilStatePtr& depthStencilState, UINT32 stencilRefValue);
  73. /**
  74. * @copydoc RenderSystem::setViewport()
  75. */
  76. void setViewport(Viewport vp);
  77. /**
  78. * @copydoc RenderSystem::bindGpuProgram()
  79. */
  80. void bindGpuProgram(HGpuProgram prg);
  81. /**
  82. * @copydoc RenderSystem::unbindGpuProgram()
  83. */
  84. void unbindGpuProgram(GpuProgramType gptype);
  85. /**
  86. * @copydoc RenderSystem::bindGpuParams()
  87. */
  88. void bindGpuParams(GpuProgramType gptype, GpuParamsPtr params);
  89. /**
  90. * @copydoc RenderSystem::beginFrame()
  91. */
  92. void beginFrame(void);
  93. /**
  94. * @copydoc RenderSystem::endFrame()
  95. */
  96. void endFrame(void);
  97. /**
  98. * @copydoc RenderSystem::draw()
  99. */
  100. void draw(UINT32 vertexOffset, UINT32 vertexCount);
  101. /**
  102. * @copydoc RenderSystem::drawIndexed()
  103. */
  104. void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 vertexCount);
  105. /**
  106. * @copydoc RenderSystem::clearRenderTarget()
  107. */
  108. void clearRenderTarget(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0);
  109. /**
  110. * @copydoc RenderSystem::clearViewport()
  111. */
  112. void clearViewport(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0);
  113. /**
  114. * @copydoc RenderSystem::getColorVertexElementType()
  115. */
  116. VertexElementType getColorVertexElementType() const;
  117. /**
  118. * @copydoc RenderSystem::getHorizontalTexelOffset()
  119. */
  120. float getHorizontalTexelOffset();
  121. /**
  122. * @copydoc RenderSystem::getVerticalTexelOffset()
  123. */
  124. float getVerticalTexelOffset();
  125. /**
  126. * @copydoc RenderSystem::getMinimumDepthInputValue()
  127. */
  128. float getMinimumDepthInputValue();
  129. /**
  130. * @copydoc RenderSystem::getMaximumDepthInputValue()
  131. */
  132. float getMaximumDepthInputValue();
  133. /**
  134. * @copydoc RenderSystem::convertProjectionMatrix()
  135. */
  136. void convertProjectionMatrix(const Matrix4& matrix,
  137. Matrix4& dest, bool forGpuProgram = false);
  138. /************************************************************************/
  139. /* Internal use by OpenGL RenderSystem only */
  140. /************************************************************************/
  141. /**
  142. * @brief Query has the main context been initialized.
  143. */
  144. bool _isContextInitialized() const { return mGLInitialised; }
  145. /**
  146. * @brief Returns main context. Caller must ensure the context has been initialized.
  147. */
  148. GLContext* getMainContext() const { return mMainContext; }
  149. /**
  150. * @brief Returns a support object you may use for creating
  151. */
  152. GLSupport* getGLSupport() const { return mGLSupport; }
  153. protected:
  154. /**
  155. * @copydoc RenderSystem::initialize_internal().
  156. */
  157. void initialize_internal(AsyncOp& asyncOp);
  158. /**
  159. * @copydoc RenderSystem::destroy_internal().
  160. */
  161. void destroy_internal(void);
  162. /**
  163. * @brief Call before doing a draw operation, this method sets everything up.
  164. */
  165. void beginDraw();
  166. /**
  167. * @brief Needs to accompany every beginDraw after you are done with a single draw operation.
  168. */
  169. void endDraw();
  170. /**
  171. * @brief Clear a part of a render target.
  172. */
  173. void clearArea(UINT32 buffers, const Color& color = Color::Black, float depth = 1.0f, UINT16 stencil = 0, const RectI& clearArea = RectI::EMPTY);
  174. /**
  175. * @brief Set up clip planes against which all geometry will get clipped.
  176. */
  177. void setClipPlanesImpl(const PlaneList& clipPlanes);
  178. /**
  179. * @brief Set up a clip plane at a specific clip plane index. If enabled,
  180. * geometry will be clipped against the positive side of the plane.
  181. *
  182. * @note Valid index range is [0, 5].
  183. */
  184. void setClipPlane(UINT16 index, float A, float B, float C, float D);
  185. /**
  186. * @brief Enable or disable clipping against a clip plane at the specified index.
  187. *
  188. * @note Valid index range is [0, 5].
  189. */
  190. void enableClipPlane (UINT16 index, bool enable);
  191. /**
  192. * @brief Changes the currently active texture unit. Any texture related operations
  193. * will then be performed on this unit.
  194. */
  195. bool activateGLTextureUnit(UINT16 unit);
  196. /**
  197. * @brief Changes the active GPU program.
  198. */
  199. void setActiveProgram(GpuProgramType gptype, GLSLGpuProgramPtr program);
  200. /**
  201. * @brief Retrieves the active GPU program of the specified type.
  202. */
  203. GLSLGpuProgramPtr getActiveProgram(GpuProgramType gptype) const;
  204. /**
  205. * @brief Converts Banshee blend mode to OpenGL blend mode.
  206. */
  207. GLint getBlendMode(BlendFactor blendMode) const;
  208. /**
  209. * @brief Converts Banshee texture addressing mode to OpenGL texture addressing mode.
  210. */
  211. GLint getTextureAddressingMode(TextureAddressingMode tam) const;
  212. /**
  213. * @brief Gets a combined min/mip filter value usable by OpenGL from the currently
  214. * set min and mip filters.
  215. */
  216. GLuint getCombinedMinMipFilter() const;
  217. /**
  218. * @brief OpenGL shares all texture slots, but the engine prefers to keep textures
  219. * separate per-stage. This will convert texture unit that is set per stage
  220. * into a global texture unit usable by OpenGL.
  221. */
  222. UINT32 getGLTextureUnit(GpuProgramType gptype, UINT32 unit);
  223. /**
  224. * @brief OpenGL shares all buffer bindings, but the engine prefers to keep buffers
  225. * separate per-stage. This will convert block buffer binding that is set per stage
  226. * into a global block buffer binding usable by OpenGL.
  227. */
  228. UINT32 getGLUniformBlockBinding(GpuProgramType gptype, UINT32 binding);
  229. /**
  230. * @brief Returns the OpenGL specific mode used for drawing, depending on the
  231. * currently set draw operation;
  232. */
  233. GLint getGLDrawMode() const;
  234. /**
  235. * @brief Creates render system capabilities that specify which features are
  236. * or aren't supported.
  237. */
  238. RenderSystemCapabilities* createRenderSystemCapabilities() const;
  239. /**
  240. * @brief Finish initialization by setting up any systems dependant on render system
  241. * capabilities.
  242. */
  243. void initFromCaps(RenderSystemCapabilities* caps);
  244. /**
  245. * @brief Switch the currently used OpenGL context. You will need to re-bind
  246. * any previously bound values manually. (e.g. textures, gpu programs and such)
  247. */
  248. void switchContext(GLContext* context);
  249. /************************************************************************/
  250. /* Sampler states */
  251. /************************************************************************/
  252. /**
  253. * @brief Sets the texture addressing mode for a texture unit. This determines
  254. * how are UV address values outside of [0, 1] range handled when sampling
  255. * from texture.
  256. */
  257. void setTextureAddressingMode(UINT16 stage, const UVWAddressingMode& uvw);
  258. /**
  259. * @brief Sets the texture border color for a texture unit. Border color
  260. * determines color returned by the texture sampler when border addressing mode
  261. * is used and texture address is outside of [0, 1] range.
  262. */
  263. void setTextureBorderColor(UINT16 stage, const Color& color);
  264. /**
  265. * @brief Sets the mipmap bias value for a given texture unit. Bias allows
  266. * you to adjust the mipmap selection calculation. Negative values force a
  267. * larger mipmap to be used, and positive values smaller. Units are in values
  268. * of mip levels, so -1 means use a mipmap one level higher than default.
  269. */
  270. void setTextureMipmapBias(UINT16 unit, float bias);
  271. /**
  272. * @brief Allows you to specify how is the texture bound to the specified texture unit filtered.
  273. * Different filter types are used for different situations like magnifying or minifying a texture.
  274. */
  275. void setTextureFiltering(UINT16 unit, FilterType ftype, FilterOptions filter);
  276. /**
  277. * @brief Sets anisotropy value for the specified texture unit.
  278. */
  279. void setTextureAnisotropy(UINT16 unit, UINT32 maxAnisotropy);
  280. /**
  281. * @brief Gets anisotropy value for the specified texture unit.
  282. */
  283. GLfloat getCurrentAnisotropy(UINT16 unit);
  284. /************************************************************************/
  285. /* Blend states */
  286. /************************************************************************/
  287. /**
  288. * @brief Sets up blending mode that allows you to combine new pixels with pixels already in the render target.
  289. * Final pixel value = (renderTargetPixel * sourceFactor) op (pixel * destFactor).
  290. */
  291. void setSceneBlending(BlendFactor sourceFactor, BlendFactor destFactor, BlendOperation op);
  292. /**
  293. * @brief Sets up blending mode that allows you to combine new pixels with pixels already in the render target.
  294. * Allows you to set up separate blend operations for alpha values.
  295. *
  296. * Final pixel value = (renderTargetPixel * sourceFactor) op (pixel * destFactor). (And the same for alpha)
  297. */
  298. void setSceneBlending(BlendFactor sourceFactor, BlendFactor destFactor, BlendFactor sourceFactorAlpha,
  299. BlendFactor destFactorAlpha, BlendOperation op, BlendOperation alphaOp);
  300. /**
  301. * @brief Sets alpha test that allows you to reject pixels that fail the comparison function
  302. * versus the provided reference value.
  303. */
  304. void setAlphaTest(CompareFunction func, unsigned char value);
  305. /**
  306. * @brief Enable alpha to coverage. Alpha to coverage allows you to perform blending without needing
  307. * to worry about order of rendering like regular blending does. It requires multi-sampling to
  308. * be active in order to work, and you need to supply an alpha texture that determines object transparency.
  309. */
  310. void setAlphaToCoverage(bool enabled);
  311. /**
  312. * @brief Enables or disables writing to certain color channels of the render target.
  313. */
  314. void setColorBufferWriteEnabled(bool red, bool green, bool blue, bool alpha);
  315. /************************************************************************/
  316. /* Rasterizer states */
  317. /************************************************************************/
  318. /**
  319. * @brief Sets vertex winding order. Normally you would use this to cull back facing
  320. * polygons.
  321. */
  322. void setCullingMode(CullingMode mode);
  323. /**
  324. * @brief Sets the polygon rasterization mode. Determines how are polygons interpreted.
  325. */
  326. void setPolygonMode(PolygonMode level);
  327. /**
  328. * @brief Sets a depth bias that will offset the depth values of new pixels by the specified amount.
  329. * Final depth bias value is a combination of the constant depth bias and slope depth bias.
  330. * Slope depth bias has more effect the higher the slope of the rendered polygon.
  331. *
  332. * @note This is useful if you want to avoid z fighting for objects at the same or similar depth.
  333. */
  334. void setDepthBias(float constantBias, float slopeScaleBias);
  335. /**
  336. * @brief Scissor test allows you to mask off rendering in all but a given rectangular area
  337. * identified by the rectangle set by setScissorRect().
  338. */
  339. void setScissorTestEnable(bool enable);
  340. /************************************************************************/
  341. /* Depth stencil state */
  342. /************************************************************************/
  343. /**
  344. * @brief Should new pixels perform depth testing using the set depth comparison function before
  345. * being written.
  346. */
  347. void setDepthBufferCheckEnabled(bool enabled = true);
  348. /**
  349. * @brief Should new pixels write to the depth buffer.
  350. */
  351. void setDepthBufferWriteEnabled(bool enabled = true);
  352. /**
  353. * @brief Sets comparison function used for depth testing. Determines how are new and existing
  354. * pixel values compared - if comparison function returns true the new pixel is written.
  355. */
  356. void setDepthBufferFunction(CompareFunction func = CMPF_LESS_EQUAL);
  357. /**
  358. * @brief Turns stencil tests on or off. By default this is disabled.
  359. * Stencil testing allow you to mask out a part of the rendered image by using
  360. * various stencil operations provided.
  361. */
  362. void setStencilCheckEnabled(bool enabled);
  363. /**
  364. * @brief Allows you to set stencil operations that are performed when stencil test passes or fails.
  365. *
  366. * @param stencilFailOp Operation executed when stencil test fails.
  367. * @param depthFailOp Operation executed when stencil test succeeds but depth test fails.
  368. * @param passOp Operation executed when stencil test succeeds and depth test succeeds.
  369. * @param front Should the stencil operations be applied to front or back facing polygons.
  370. */
  371. void setStencilBufferOperations(StencilOperation stencilFailOp = SOP_KEEP,
  372. StencilOperation depthFailOp = SOP_KEEP, StencilOperation passOp = SOP_KEEP,
  373. bool front = true);
  374. /**
  375. * @brief Sets a stencil buffer comparison function. The result of this will cause one of 3 actions
  376. * depending on whether the test fails, succeeds but with the depth buffer check still failing,
  377. * or succeeds with the depth buffer check passing too.
  378. *
  379. * @param func Comparison function that determines whether a stencil test fails or passes. Reference value
  380. * gets compared to the value already in the buffer using this function.
  381. * @param mask The bitmask applied to both the stencil value and the reference value
  382. * before comparison
  383. * @param ccw If set to true, the stencil operations will be applied to counterclockwise
  384. * faces. Otherwise they will be applied to clockwise faces.
  385. */
  386. void setStencilBufferFunc(CompareFunction func = CMPF_ALWAYS_PASS, UINT32 mask = 0xFFFFFFFF, bool front = true);
  387. /**
  388. * @brief The bitmask applied to the stencil value before writing it to the stencil buffer.
  389. */
  390. void setStencilBufferWriteMask(UINT32 mask = 0xFFFFFFFF);
  391. /**
  392. * @brief Sets a reference values used for stencil buffer comparisons.
  393. * Actual comparison function and stencil operations are set by setting the DepthStencilState.
  394. */
  395. void setStencilRefValue(UINT32 refValue);
  396. /************************************************************************/
  397. /* UTILITY METHODS */
  398. /************************************************************************/
  399. /**
  400. * @brief Converts the provided matrix m into a representation usable by OpenGL.
  401. */
  402. void makeGLMatrix(GLfloat gl_matrix[16], const Matrix4& m);
  403. /**
  404. * @brief Converts the engine depth/stencil compare function into OpenGL representation.
  405. */
  406. GLint convertCompareFunction(CompareFunction func) const;
  407. /**
  408. * @brief Convers the engine stencil operation in OpenGL representation. Optionally inverts
  409. * the operation (increment becomes decrement, etc.).
  410. */
  411. GLint convertStencilOp(StencilOperation op, bool invert = false) const;
  412. /**
  413. * @brief Checks if there are any OpenGL errors and prints them to the log.
  414. */
  415. bool checkForErrors() const;
  416. private:
  417. UINT32 mScissorTop, mScissorBottom, mScissorLeft, mScissorRight;
  418. UINT32 mViewportLeft, mViewportTop, mViewportWidth, mViewportHeight;
  419. UINT32 mStencilReadMask;
  420. UINT32 mStencilWriteMask;
  421. UINT32 mStencilRefValue;
  422. CompareFunction mStencilCompareFront;
  423. CompareFunction mStencilCompareBack;
  424. // View matrix to set world against
  425. Matrix4 mViewMatrix;
  426. // Last min & mip filtering options, so we can combine them
  427. FilterOptions mMinFilter;
  428. FilterOptions mMipFilter;
  429. // Holds texture type settings for every stage
  430. UINT32 mNumTextureTypes;
  431. GLenum* mTextureTypes;
  432. bool mDepthWrite;
  433. bool mColorWrite[4];
  434. GLSupport* mGLSupport;
  435. bool mGLInitialised;
  436. GLSLProgramFactory* mGLSLProgramFactory;
  437. GLSLProgramPipelineManager* mProgramPipelineManager;
  438. GLSLGpuProgramPtr mCurrentVertexProgram;
  439. GLSLGpuProgramPtr mCurrentFragmentProgram;
  440. GLSLGpuProgramPtr mCurrentGeometryProgram;
  441. GLSLGpuProgramPtr mCurrentHullProgram;
  442. GLSLGpuProgramPtr mCurrentDomainProgram;
  443. const GLSLProgramPipeline* mActivePipeline;
  444. UINT32 mFragmentTexOffset;
  445. UINT32 mVertexTexOffset;
  446. UINT32 mGeometryTexOffset;
  447. UINT32 mFragmentUBOffset;
  448. UINT32 mVertexUBOffset;
  449. UINT32 mGeometryUBOffset;
  450. UINT32 mHullUBOffset;
  451. UINT32 mDomainUBOffset;
  452. UINT32 mComputeUBOffset;
  453. Vector<VertexBufferPtr> mBoundVertexBuffers;
  454. VertexDeclarationPtr mBoundVertexDeclaration;
  455. IndexBufferPtr mBoundIndexBuffer;
  456. DrawOperationType mCurrentDrawOperation;
  457. GLContext *mMainContext;
  458. GLContext *mCurrentContext;
  459. bool mDrawCallInProgress;
  460. UINT16 mActiveTextureUnit;
  461. };
  462. }