BsGLRenderSystem.h 18 KB

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