CmGLRenderTexture.h 7.1 KB

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