OgreGLFBORenderTexture.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #ifndef __OgreGLFBORTT_H__
  25. #define __OgreGLFBORTT_H__
  26. #include "OgreGLRenderTexture.h"
  27. #include "OgreGLContext.h"
  28. #include "OgreGLFrameBufferObject.h"
  29. /// Extra GL constants
  30. #define GL_DEPTH24_STENCIL8_EXT 0x88F0
  31. namespace Ogre {
  32. class GLFBOManager;
  33. /** RenderTexture for GL FBO
  34. */
  35. class _OgreGLExport GLFBORenderTexture: public GLRenderTexture
  36. {
  37. public:
  38. GLFBORenderTexture(GLFBOManager *manager, const String &name, const GLSurfaceDesc &target, bool writeGamma, uint fsaa);
  39. virtual void getCustomAttribute(const String& name, void* pData);
  40. /// Override needed to deal with multisample buffers
  41. virtual void swapBuffers(bool waitForVSync = true);
  42. protected:
  43. GLFrameBufferObject mFB;
  44. };
  45. /** Factory for GL Frame Buffer Objects, and related things.
  46. */
  47. class _OgreGLExport GLFBOManager: public GLRTTManager
  48. {
  49. public:
  50. GLFBOManager(bool atimode);
  51. ~GLFBOManager();
  52. /** Bind a certain render target if it is a FBO. If it is not a FBO, bind the
  53. main frame buffer.
  54. */
  55. void bind(RenderTarget *target);
  56. /** Unbind a certain render target. No-op for FBOs.
  57. */
  58. void unbind(RenderTarget *target) {};
  59. /** Get best depth and stencil supported for given internalFormat
  60. */
  61. void getBestDepthStencil(GLenum internalFormat, GLenum *depthFormat, GLenum *stencilFormat);
  62. /** Create a texture rendertarget object
  63. */
  64. virtual GLFBORenderTexture *createRenderTexture(const String &name,
  65. const GLSurfaceDesc &target, bool writeGamma, uint fsaa);
  66. /** Create a multi render target
  67. */
  68. virtual MultiRenderTarget* createMultiRenderTarget(const String & name);
  69. /** Request a render buffer. If format is GL_NONE, return a zero buffer.
  70. */
  71. GLSurfaceDesc requestRenderBuffer(GLenum format, size_t width, size_t height, uint fsaa);
  72. /** Request the specify render buffer in case shared somewhere. Ignore
  73. silently if surface.buffer is 0.
  74. */
  75. void requestRenderBuffer(const GLSurfaceDesc &surface);
  76. /** Release a render buffer. Ignore silently if surface.buffer is 0.
  77. */
  78. void releaseRenderBuffer(const GLSurfaceDesc &surface);
  79. /** Check if a certain format is usable as FBO rendertarget format
  80. */
  81. bool checkFormat(PixelFormat format) { return mProps[format].valid; }
  82. /** Get a FBO without depth/stencil for temporary use, like blitting between textures.
  83. */
  84. GLuint getTemporaryFBO() { return mTempFBO; }
  85. private:
  86. /** Frame Buffer Object properties for a certain texture format.
  87. */
  88. struct FormatProperties
  89. {
  90. bool valid; // This format can be used as RTT (FBO)
  91. /** Allowed modes/properties for this pixel format
  92. */
  93. struct Mode
  94. {
  95. size_t depth; // Depth format (0=no depth)
  96. size_t stencil; // Stencil format (0=no stencil)
  97. };
  98. vector<Mode>::type modes;
  99. };
  100. /** Properties for all internal formats defined by OGRE
  101. */
  102. FormatProperties mProps[PF_COUNT];
  103. /** Stencil and depth renderbuffers of the same format are re-used between surfaces of the
  104. same size and format. This can save a lot of memory when a large amount of rendertargets
  105. are used.
  106. */
  107. struct RBFormat
  108. {
  109. RBFormat(GLenum inFormat, size_t inWidth, size_t inHeight, uint fsaa):
  110. format(inFormat), width(inWidth), height(inHeight), samples(fsaa)
  111. {}
  112. GLenum format;
  113. size_t width;
  114. size_t height;
  115. uint samples;
  116. // Overloaded comparison operator for usage in map
  117. bool operator < (const RBFormat &other) const
  118. {
  119. if(format < other.format)
  120. {
  121. return true;
  122. }
  123. else if(format == other.format)
  124. {
  125. if(width < other.width)
  126. {
  127. return true;
  128. }
  129. else if(width == other.width)
  130. {
  131. if(height < other.height)
  132. return true;
  133. else if (height == other.height)
  134. {
  135. if (samples < other.samples)
  136. return true;
  137. }
  138. }
  139. }
  140. return false;
  141. }
  142. };
  143. struct RBRef
  144. {
  145. RBRef(){}
  146. RBRef(GLRenderBuffer *inBuffer):
  147. buffer(inBuffer), refcount(1)
  148. { }
  149. GLRenderBuffer *buffer;
  150. size_t refcount;
  151. };
  152. typedef map<RBFormat, RBRef>::type RenderBufferMap;
  153. RenderBufferMap mRenderBufferMap;
  154. // map(format, sizex, sizey) -> [GLSurface*,refcount]
  155. /** Temporary FBO identifier
  156. */
  157. GLuint mTempFBO;
  158. /// Buggy ATI driver?
  159. bool mATIMode;
  160. /** Detect allowed FBO formats */
  161. void detectFBOFormats();
  162. GLuint _tryFormat(GLenum depthFormat, GLenum stencilFormat);
  163. bool _tryPackedFormat(GLenum packedFormat);
  164. };
  165. }
  166. #endif