OGLGraphics.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  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. #pragma once
  24. #include "ArrayPtr.h"
  25. #include "Color.h"
  26. #include "GraphicsDefs.h"
  27. #include "HashMap.h"
  28. #include "Matrix3x4.h"
  29. #include "Object.h"
  30. #include "Rect.h"
  31. class Image;
  32. class IndexBuffer;
  33. class Matrix3;
  34. class Matrix4;
  35. class Matrix3x4;
  36. class GPUObject;
  37. class GraphicsImpl;
  38. class RenderSurface;
  39. class ShaderProgram;
  40. class ShaderVariation;
  41. class Texture;
  42. class Texture2D;
  43. class TextureCube;
  44. class Vector3;
  45. class Vector4;
  46. class VertexBuffer;
  47. typedef Map<Pair<ShaderVariation*, ShaderVariation*>, SharedPtr<ShaderProgram> > ShaderProgramMap;
  48. static const unsigned NUM_SCREEN_BUFFERS = 2;
  49. /// CPU-side buffer for VBO discard locking.
  50. struct DiscardLockBuffer
  51. {
  52. DiscardLockBuffer() :
  53. size_(0),
  54. reserved_(false)
  55. {
  56. }
  57. /// Buffer data.
  58. SharedArrayPtr<unsigned char> data_;
  59. /// Data size.
  60. unsigned size_;
  61. /// Reserved flag.
  62. bool reserved_;
  63. };
  64. /// %Graphics subsystem. Manages the application window, rendering state and GPU resources.
  65. class Graphics : public Object
  66. {
  67. OBJECT(Graphics);
  68. public:
  69. /// Construct.
  70. Graphics(Context* context_);
  71. /// Destruct. Release the OpenGL context and close the window.
  72. virtual ~Graphics();
  73. /// Set window title.
  74. void SetWindowTitle(const String& windowTitle);
  75. /// Set screen mode.
  76. bool SetMode(int width, int height, bool fullscreen, bool vsync, bool tripleBuffer, int multiSample);
  77. /// Set screen resolution only.
  78. bool SetMode(int width, int height);
  79. /// Toggle between full screen and windowed mode.
  80. bool ToggleFullscreen();
  81. /// Close the window.
  82. void Close();
  83. /// Take a screenshot.
  84. bool TakeScreenShot(Image& destImage);
  85. /// Set whether to flush GPU command queue at the end of each frame. Default true.
  86. void SetFlushGPU(bool enable);
  87. /// Begin frame rendering.
  88. bool BeginFrame();
  89. /// End frame rendering and swap buffers.
  90. void EndFrame();
  91. /// Clear any or all of render target, depth buffer and stencil buffer.
  92. void Clear(unsigned flags, const Color& color = Color(0.0f, 0.0f, 0.0f, 0.0f), float depth = 1.0f, unsigned stencil = 0);
  93. /// Draw non-indexed geometry.
  94. void Draw(PrimitiveType type, unsigned vertexStart, unsigned vertexCount);
  95. /// Draw indexed geometry.
  96. void Draw(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount);
  97. /// Draw indexed, instanced geometry. No-op on OpenGL.
  98. void DrawInstanced(PrimitiveType type, unsigned indexStart, unsigned indexCount, unsigned minVertex, unsigned vertexCount, unsigned instanceCount);
  99. /// Set vertex buffer.
  100. void SetVertexBuffer(VertexBuffer* buffer);
  101. /// Set multiple vertex buffers.
  102. bool SetVertexBuffers(const Vector<VertexBuffer*>& buffers, const PODVector<unsigned>& elementMasks, unsigned instanceOffset = 0);
  103. /// Set multiple vertex buffers.
  104. bool SetVertexBuffers(const Vector<SharedPtr<VertexBuffer> >& buffers, const PODVector<unsigned>& elementMasks, unsigned instanceOffset = 0);
  105. /// Set index buffer.
  106. void SetIndexBuffer(IndexBuffer* buffer);
  107. /// Set shaders.
  108. void SetShaders(ShaderVariation* vs, ShaderVariation* ps);
  109. /// Set shader float constants.
  110. void SetShaderParameter(StringHash param, const float* data, unsigned count);
  111. /// Set shader float constant.
  112. void SetShaderParameter(StringHash param, float value);
  113. /// Set shader color constant.
  114. void SetShaderParameter(StringHash param, const Color& color);
  115. /// Set shader 3x3 matrix constant.
  116. void SetShaderParameter(StringHash param, const Matrix3& matrix);
  117. /// Set shader 3D vector constant.
  118. void SetShaderParameter(StringHash param, const Vector3& vector);
  119. /// Set shader 4x4 matrix constant.
  120. void SetShaderParameter(StringHash param, const Matrix4& matrix);
  121. /// Set shader 4D vector constant.
  122. void SetShaderParameter(StringHash param, const Vector4& vector);
  123. /// Set shader 4x3 matrix constant.
  124. void SetShaderParameter(StringHash param, const Matrix3x4& matrix);
  125. /// Check whether a shader parameter in the currently set shaders needs update.
  126. bool NeedParameterUpdate(StringHash param, const void* source);
  127. /// Check whether the current pixel shader uses a texture unit.
  128. bool NeedTextureUnit(TextureUnit unit);
  129. /// Clear remembered shader parameter sources.
  130. void ClearParameterSources();
  131. /// Clear remembered transform shader parameter sources.
  132. void ClearTransformSources();
  133. /// Clean up unused shader programs.
  134. void CleanupShaderPrograms();
  135. /// Set texture.
  136. void SetTexture(unsigned index, Texture* texture);
  137. /// Bind texture unit 0 for update. Called by Texture.
  138. void SetTextureForUpdate(Texture* texture);
  139. /// Set default texture filtering mode.
  140. void SetDefaultTextureFilterMode(TextureFilterMode mode);
  141. /// Set texture anisotropy.
  142. void SetTextureAnisotropy(unsigned level);
  143. /// Dirty texture parameters of all textures (when global settings change.)
  144. void SetTextureParametersDirty();
  145. /// Reset all render targets and depth stencil buffer (render to back buffer.)
  146. void ResetRenderTargets();
  147. /// Reset specific render target.
  148. void ResetRenderTarget(unsigned index);
  149. /// Reset depth stencil buffer.
  150. void ResetDepthStencil();
  151. /// Set render target.
  152. void SetRenderTarget(unsigned index, RenderSurface* renderTarget);
  153. /// Set render target.
  154. void SetRenderTarget(unsigned index, Texture2D* renderTexture);
  155. /// Set depth stencil buffer.
  156. void SetDepthStencil(RenderSurface* depthStencil);
  157. /// Set depth stencil buffer.
  158. void SetDepthStencil(Texture2D* depthTexture);
  159. /// Set viewport.
  160. void SetViewport(const IntRect& rect);
  161. /// Set alpha test.
  162. void SetAlphaTest(bool enable, CompareMode mode = CMP_ALWAYS, float alphaRef = 0.5f);
  163. /// Set blending mode.
  164. void SetBlendMode(BlendMode mode);
  165. /// Set color write on/off.
  166. void SetColorWrite(bool enable);
  167. /// Set hardware culling mode.
  168. void SetCullMode(CullMode mode);
  169. /// Set depth bias.
  170. void SetDepthBias(float constantBias, float slopeScaledBias);
  171. /// Set depth compare.
  172. void SetDepthTest(CompareMode mode);
  173. /// Set depth write on/off.
  174. void SetDepthWrite(bool enable);
  175. /// Set polygon fill mode.
  176. void SetFillMode(FillMode mode);
  177. /// Set scissor test.
  178. void SetScissorTest(bool enable, const Rect& rect = Rect::FULL, bool borderInclusive = true);
  179. /// Set scissor test.
  180. void SetScissorTest(bool enable, const IntRect& rect);
  181. /// Set stencil test.
  182. void SetStencilTest(bool enable, CompareMode mode = CMP_ALWAYS, StencilOp pass = OP_KEEP, StencilOp fail = OP_KEEP, StencilOp zFail = OP_KEEP, unsigned stencilRef = 0, unsigned stencilMask = M_MAX_UNSIGNED);
  183. /// Set vertex buffer stream frequency. No-op on OpenGL.
  184. void SetStreamFrequency(unsigned index, unsigned frequency);
  185. /// Reset stream frequencies. No-op on OpenGL.
  186. void ResetStreamFrequencies();
  187. /// Set force Shader Model 2 flag. No effect on OpenGL.
  188. void SetForceSM2(bool enable);
  189. /// %Set force fallback shaders flag. No effect on OpenGL.
  190. void SetForceFallback(bool enable);
  191. /// Return whether rendering initialized.
  192. bool IsInitialized() const;
  193. /// Return graphics implementation, which holds the actual API-specific resources.
  194. GraphicsImpl* GetImpl() const { return impl_; }
  195. /// Return window title.
  196. const String& GetWindowTitle() const { return windowTitle_; }
  197. /// Return window width.
  198. int GetWidth() const { return width_; }
  199. /// Return window height.
  200. int GetHeight() const { return height_; }
  201. /// Return multisample mode (1 = no multisampling.)
  202. int GetMultiSample() const { return multiSample_; }
  203. /// Return whether window is fullscreen.
  204. bool GetFullscreen() const { return fullscreen_; }
  205. /// Return whether vertical sync is on.
  206. bool GetVSync() const { return vsync_; }
  207. /// Return whether triple buffering is enabled.
  208. bool GetTripleBuffer() const { return tripleBuffer_; }
  209. /// Return whether GPU command queue is flushed at the end of each frame.
  210. bool GetFlushGPU() const { return flushGPU_; }
  211. /// Return whether device is lost, and can not yet render. Always false on OpenGL.
  212. bool IsDeviceLost() const { return false; }
  213. /// Return window handle.
  214. void* GetWindowHandle() const;
  215. /// Return number of primitives drawn this frame.
  216. unsigned GetNumPrimitives() const { return numPrimitives_; }
  217. /// Return number of batches drawn this frame.
  218. unsigned GetNumBatches() const { return numBatches_; }
  219. /// Return dummy color texture format for shadow maps. Is always 0 on OpenGL.
  220. unsigned GetDummyColorFormat() const { return 0; }
  221. /// Return shadow map depth texture format, or 0 if not supported.
  222. unsigned GetShadowMapFormat() const { return shadowMapFormat_; }
  223. /// Return 24-bit shadow map depth texture format, or 0 if not supported.
  224. unsigned GetHiresShadowMapFormat() const { return hiresShadowMapFormat_; }
  225. /// Return whether fallback shaders are required. Always false on OpenGL.
  226. bool GetFallback() const { return false; }
  227. /// Return whether Shader Model 3 is supported. Always false on OpenGL.
  228. bool GetSM3Support() const { return false; }
  229. /// Return whether hardware depth texture is supported.
  230. bool GetHardwareDepthSupport() const { return false; }
  231. /// Return whether shadow map depth compare is done in hardware. Always true on OpenGL.
  232. bool GetHardwareShadowSupport() const { return true; }
  233. /// Return whether 24-bit shadow maps are supported. Assume true on OpenGL.
  234. bool GetHiresShadowSupport() const { return true; }
  235. /// Return whether stream offset is supported. Always false on OpenGL.
  236. bool GetStreamOffsetSupport() const { return false; }
  237. /// Return supported fullscreen resolutions.
  238. PODVector<IntVector2> GetResolutions() const;
  239. /// Return supported multisampling levels.
  240. PODVector<int> GetMultiSampleLevels() const;
  241. /// Return vertex buffer by index.
  242. VertexBuffer* GetVertexBuffer(unsigned index) const;
  243. /// Return index buffer.
  244. IndexBuffer* GetIndexBuffer() const { return indexBuffer_; }
  245. /// Return vertex shader.
  246. ShaderVariation* GetVertexShader() const { return vertexShader_; }
  247. /// Return pixel shader.
  248. ShaderVariation* GetPixelShader() const { return pixelShader_; }
  249. /// Return shader program.
  250. ShaderProgram* GetShaderProgram() const { return shaderProgram_; }
  251. /// Return texture unit index by name.
  252. TextureUnit GetTextureUnit(const String& name);
  253. /// Return texture unit name by index.
  254. const String& GetTextureUnitName(TextureUnit unit);
  255. /// Return texture by texture unit index.
  256. Texture* GetTexture(unsigned index) const;
  257. /// Return default texture filtering mode.
  258. TextureFilterMode GetDefaultTextureFilterMode() const { return defaultTextureFilterMode_; }
  259. /// Return render target by index.
  260. RenderSurface* GetRenderTarget(unsigned index) const;
  261. /// Return depth stencil buffer.
  262. RenderSurface* GetDepthStencil() const { return depthStencil_; }
  263. /// Return backbuffer depth stencil texture, created if available. Never created on OpenGL.
  264. Texture2D* GetDepthTexture() const { return 0; }
  265. /// Return the viewport coordinates.
  266. IntRect GetViewport() const { return viewport_; }
  267. /// Return whether alpha testing is enabled.
  268. bool GetAlphaTest() const { return alphaTest_; }
  269. /// Return alpha test compare mode.
  270. CompareMode GetAlphaTestMode() const { return alphaTestMode_; }
  271. /// Return texture anisotropy.
  272. unsigned GetTextureAnisotropy() const { return textureAnisotropy_; }
  273. /// Return alpha test reference value.
  274. float GetAlphaRef() const { return alphaRef_; }
  275. /// Return blending mode.
  276. BlendMode GetBlendMode() const { return blendMode_; }
  277. /// Return whether color write is enabled.
  278. bool GetColorWrite() const { return colorWrite_; }
  279. /// Return hardware culling mode.
  280. CullMode GetCullMode() const { return cullMode_; }
  281. /// Return depth constant bias.
  282. float GetDepthConstantBias() const { return constantDepthBias_; }
  283. /// Return depth slope scaled bias.
  284. float GetDepthSlopeScaledBias() const { return slopeScaledDepthBias_; }
  285. /// Return depth compare mode.
  286. CompareMode GetDepthTest() const { return depthTestMode_; }
  287. /// Return whether depth write is enabled.
  288. bool GetDepthWrite() const { return depthWrite_; }
  289. /// Return polygon fill mode.
  290. FillMode GetFillMode() const { return fillMode_; }
  291. /// Return whether stencil test is enabled.
  292. bool GetStencilTest() const { return stencilTest_; }
  293. /// Return whether scissor test is enabled.
  294. bool GetScissorTest() const { return scissorTest_; }
  295. /// Return scissor rectangle coordinates.
  296. const IntRect& GetScissorRect() const { return scissorRect_; }
  297. /// Return stencil compare mode.
  298. CompareMode GetStencilTestMode() const { return stencilTestMode_; }
  299. /// Return stencil operation to do if stencil test passes.
  300. StencilOp GetStencilPass() const { return stencilPass_; }
  301. /// Return stencil operation to do if stencil test fails.
  302. StencilOp GetStencilFail() const { return stencilFail_; }
  303. /// Return stencil operation to do if depth compare fails.
  304. StencilOp GetStencilZFail() const { return stencilZFail_; }
  305. /// Return stencil reference value.
  306. unsigned GetStencilRef() const { return stencilRef_; }
  307. /// Return stencil compare bitmask.
  308. unsigned GetStencilMask() const { return stencilMask_; }
  309. /// Return stream frequency by vertex buffer index. Always returns 0 on OpenGL.
  310. unsigned GetStreamFrequency(unsigned index) const { return 0; }
  311. /// Return render target width and height.
  312. IntVector2 GetRenderTargetDimensions() const;
  313. /// Return force Shader Model 2 flag. Always false on OpenGL.
  314. bool GetForceSM2() const { return false; }
  315. /// Return force fallback mode flag. Always false on OpenGL.
  316. bool GetForceFallback() const { return false; }
  317. /// Add a GPU object to keep track of. Called by GPUObject.
  318. void AddGPUObject(GPUObject* object);
  319. /// Remove a GPU object. Called by GPUObject.
  320. void RemoveGPUObject(GPUObject* object);
  321. /// Reserve a CPU side discard locking buffer.
  322. void* ReserveDiscardLockBuffer(unsigned size);
  323. /// Free a CPU side discard locking buffer.
  324. void FreeDiscardLockBuffer(void* buffer);
  325. /// Return the API-specific alpha texture format.
  326. static unsigned GetAlphaFormat();
  327. /// Return the API-specific luminance texture format.
  328. static unsigned GetLuminanceFormat();
  329. /// Return the API-specific luminance alpha texture format.
  330. static unsigned GetLuminanceAlphaFormat();
  331. /// Return the API-specific RGB texture format.
  332. static unsigned GetRGBFormat();
  333. /// Return the API-specific RGBA texture format.
  334. static unsigned GetRGBAFormat();
  335. /// Return the API-specific depth texture format.
  336. static unsigned GetDepthFormat();
  337. /// Return the API-specific depth stencil texture format.
  338. static unsigned GetDepthStencilFormat();
  339. private:
  340. /// Reset cached rendering state.
  341. void ResetCachedState();
  342. /// Set draw buffer(s) on the FBO.
  343. void SetDrawBuffers();
  344. /// Release GPU objects and close the window.
  345. void Release();
  346. /// Initialize texture unit mappings.
  347. void SetTextureUnitMappings();
  348. /// Implementation.
  349. GraphicsImpl* impl_;
  350. /// Window title.
  351. String windowTitle_;
  352. /// Window width.
  353. int width_;
  354. /// Window height.
  355. int height_;
  356. /// Multisampling mode.
  357. int multiSample_;
  358. /// Fullscreen flag.
  359. bool fullscreen_;
  360. /// Vertical sync flag.
  361. bool vsync_;
  362. /// Triple buffering flag.
  363. bool tripleBuffer_;
  364. /// Flush GPU command queue flag.
  365. bool flushGPU_;
  366. /// Number of primitives this frame.
  367. unsigned numPrimitives_;
  368. /// Number of batches this frame.
  369. unsigned numBatches_;
  370. /// GPU objects.
  371. Vector<GPUObject*> gpuObjects_;
  372. /// Discard lock buffers.
  373. Vector<DiscardLockBuffer> discardLockBuffers_;
  374. /// Shadow map depth texture format.
  375. unsigned shadowMapFormat_;
  376. /// Shadow map 24-bit depth texture format.
  377. unsigned hiresShadowMapFormat_;
  378. /// Vertex buffers in use.
  379. VertexBuffer* vertexBuffers_[MAX_VERTEX_STREAMS];
  380. /// Element mask in use.
  381. unsigned elementMasks_[MAX_VERTEX_STREAMS];
  382. /// Index buffer in use.
  383. IndexBuffer* indexBuffer_;
  384. /// Vertex shader in use.
  385. ShaderVariation* vertexShader_;
  386. /// Pixel shader in use.
  387. ShaderVariation* pixelShader_;
  388. /// Shader program in use.
  389. ShaderProgram* shaderProgram_;
  390. /// Linked shader programs.
  391. ShaderProgramMap shaderPrograms_;
  392. /// Shader parameters global frame number.
  393. unsigned shaderParameterFrame_;
  394. /// Textures in use.
  395. Texture* textures_[MAX_TEXTURE_UNITS];
  396. /// OpenGL texture types in use.
  397. unsigned textureTypes_[MAX_TEXTURE_UNITS];
  398. /// Texture unit mappings.
  399. Map<String, TextureUnit> textureUnits_;
  400. /// Render targets in use.
  401. RenderSurface* renderTargets_[MAX_RENDERTARGETS];
  402. /// Depth stencil buffer in use.
  403. RenderSurface* depthStencil_;
  404. /// Viewport coordinates.
  405. IntRect viewport_;
  406. /// Alpha test enable flag.
  407. bool alphaTest_;
  408. /// Alpha test compare mode.
  409. CompareMode alphaTestMode_;
  410. /// Alpha test reference value.
  411. float alphaRef_;
  412. /// Texture anisotropy level.
  413. unsigned textureAnisotropy_;
  414. /// Blending mode.
  415. BlendMode blendMode_;
  416. /// Color write enable.
  417. bool colorWrite_;
  418. /// Hardware culling mode.
  419. CullMode cullMode_;
  420. /// Depth constant bias.
  421. float constantDepthBias_;
  422. /// Depth slope scaled bias.
  423. float slopeScaledDepthBias_;
  424. /// Depth compare mode.
  425. CompareMode depthTestMode_;
  426. /// Depth write enable flag.
  427. bool depthWrite_;
  428. /// Polygon fill mode.
  429. FillMode fillMode_;
  430. /// Scissor test rectangle.
  431. IntRect scissorRect_;
  432. /// Scissor test enable flag.
  433. bool scissorTest_;
  434. /// Stencil test compare mode.
  435. CompareMode stencilTestMode_;
  436. /// Stencil operation on pass.
  437. StencilOp stencilPass_;
  438. /// Stencil operation on fail.
  439. StencilOp stencilFail_;
  440. /// Stencil operation on depth fail.
  441. StencilOp stencilZFail_;
  442. /// Stencil test enable flag.
  443. bool stencilTest_;
  444. /// Stencil test reference value.
  445. unsigned stencilRef_;
  446. /// Stencil compare bitmask.
  447. unsigned stencilMask_;
  448. /// Default texture filtering mode.
  449. TextureFilterMode defaultTextureFilterMode_;
  450. /// Map for additional depth textures, to emulate Direct3D9 ability to mix render texture and backbuffer rendering.
  451. HashMap<int, SharedPtr<Texture2D> > depthTextures_;
  452. };
  453. /// Register Graphics library objects.
  454. void RegisterGraphicsLibrary(Context* context_);