llrendertarget.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @file llrendertarget.h
  3. * @brief LLRenderTarget declaration
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLRENDERTARGET_H
  33. #define LL_LLRENDERTARGET_H
  34. #include "llgl.h"
  35. #include "llrender.h"
  36. // Wrapper around OpenGL frame buffer objects for use in render-to-texture.
  37. // SAMPLE USAGE:
  38. //
  39. // LLRenderTarget target;
  40. //
  41. // .../..
  42. //
  43. // // Allocate a 256x256 RGBA render target with depth buffer
  44. // target.allocate(256, 256, GL_RGBA, true);
  45. //
  46. // // Render to contents of offscreen buffer
  47. // target.bindTarget();
  48. // target.clear();
  49. // ... <issue drawing commands> ...
  50. // target.flush();
  51. // .../...
  52. //
  53. // // Use target as a texture
  54. // gGL.getTexUnit(INDEX)->bind(&target);
  55. // ... <issue drawing commands> ...
  56. class LLRenderTarget
  57. {
  58. protected:
  59. LOG_CLASS(LLRenderTarget);
  60. public:
  61. LLRenderTarget();
  62. ~LLRenderTarget();
  63. // Allocates resources for rendering; must be called before use.
  64. // Multiple calls will release previously allocated resources.
  65. // Legacy method for EE rendering only.
  66. bool allocate(U32 resx, U32 resy, U32 color_fmt, bool depth, bool stencil,
  67. LLTexUnit::eTextureType usage = LLTexUnit::TT_TEXTURE);
  68. // New method for PBR rendering only.
  69. bool allocate(U32 resx, U32 resy, U32 color_fmt, bool depth = false,
  70. LLTexUnit::eTextureType usage = LLTexUnit::TT_TEXTURE,
  71. LLTexUnit::eMipGeneration generation = LLTexUnit::TMG_NONE);
  72. // Resizes existing attachments to use new resolution and color format.
  73. // CAUTION: if GL runs out of memory attempting to resize, this render
  74. // target will be undefined. DO NOT use for screen space buffers or for
  75. // scratch space for an image that might be uploaded. DO use for render
  76. // targets that resize often and aren't likely to ruin someone's day if
  77. // they break
  78. void resize(U32 resx, U32 resy);
  79. // Points this render target at a particular LLImageGL.
  80. // Intended usage:
  81. // LLRenderTarget target;
  82. // target.setColorAttachment(attachment, use_name);
  83. // target.bindTarget();
  84. // <Issue GL calls>
  85. // target.flush();
  86. // target.releaseColorAttachment();
  87. // With: 'attachment' the LLImageGL to render into, 'use_name' an optional
  88. // texture name to target instead of attachment->getTexName().
  89. // Note: setColorAttachment() and releaseColorAttachment() cannot be used
  90. // in conjuction with addColorAttachment(), allocateDepth(), resize(), etc.
  91. void setColorAttachment(LLImageGL* attachmentp, U32 use_name = 0);
  92. // Adds a color buffer attachment with a limit of 4 color attachments per
  93. // render target.
  94. bool addColorAttachment(U32 color_fmt);
  95. // Detaches from current color attachment
  96. void releaseColorAttachment();
  97. // Allocates a depth texture
  98. bool allocateDepth();
  99. // Shares depth buffer with provided render target
  100. void shareDepthBuffer(LLRenderTarget& target);
  101. // Frees any allocated resources; safe to call redundantly.
  102. void release();
  103. // Binds target for rendering; applies appropriate viewport.
  104. void bindTarget();
  105. // Clears render targer, clears depth buffer if present, uses scissor rect
  106. // if in copy-to-texture mode
  107. void clear(U32 mask = 0xFFFFFFFF);
  108. // Gets the applied viewport
  109. void getViewport(S32* viewportp);
  110. // Returns X resolution
  111. LL_INLINE U32 getWidth() const { return mResX; }
  112. // Returns Y resolution
  113. LL_INLINE U32 getHeight() const { return mResY; }
  114. LL_INLINE LLTexUnit::eTextureType getUsage() const { return mUsage; }
  115. U32 getTexture(U32 attachment = 0) const;
  116. LL_INLINE U32 getNumTextures() const { return mTex.size(); }
  117. LL_INLINE U32 getDepth() const { return mDepth; }
  118. // For EE rendering only
  119. LL_INLINE bool hasStencil() const { return mStencil; }
  120. LL_INLINE U32 getFBO() const { return mFBO; }
  121. void bindTexture(U32 index, S32 channel,
  122. LLTexUnit::eTextureFilterOptions filter_options =
  123. LLTexUnit::TFO_BILINEAR);
  124. // Flushes rendering operations. Must be called when rendering is complete.
  125. // Should be used 1:1 with bindTarget call bindTarget once, do all your
  126. // rendering, call flush once. If fetch_depth is true, every effort will be
  127. // made to copy the depth buffer into the current depth texture. A depth
  128. // texture will be allocated if needed. Note: 'fetch_depth' is ignored by
  129. // the PBR renderer.
  130. void flush(bool fetch_depth = false);
  131. // Returns true if target is ready to be rendered into, that is if the
  132. // target has been allocated with at least one renderable attachment (i.e.
  133. // color buffer, depth buffer).
  134. bool isComplete() const;
  135. // The following copyContents*() methods are only used by the EE renderer.
  136. // HB
  137. void copyContents(LLRenderTarget& source, S32 src_x0, S32 src_y0,
  138. S32 src_x1, S32 src_y1, S32 dst_x0, S32 dst_y0,
  139. S32 dst_x1, S32 dst_y1, U32 mask, U32 filter);
  140. // *HACK: for PBR material preview only.
  141. void swapFBORefs(LLRenderTarget& other);
  142. static void copyContentsToFramebuffer(LLRenderTarget& source,
  143. S32 src_x0, S32 src_y0,
  144. S32 src_x1, S32 src_y1,
  145. S32 dst_x0, S32 dst_y0,
  146. S32 dst_x1, S32 dst_y1,
  147. U32 mask, U32 filter);
  148. // To call when toggling between EE and PBR rendering. HB
  149. static void reset();
  150. protected:
  151. std::vector<U32> mTex;
  152. std::vector<U32> mInternalFormat;
  153. LLRenderTarget* mPreviousRT; // For PBR rendering only
  154. U32 mResX;
  155. U32 mResY;
  156. U32 mFBO;
  157. U32 mPreviousFBO; // For EE rendering only
  158. U32 mPreviousResX; // For EE rendering only
  159. U32 mPreviousResY; // For EE rendering only
  160. U32 mDepth;
  161. LLTexUnit::eTextureType mUsage;
  162. LLTexUnit::eMipGeneration mGenerateMipMaps; // For PBR rendering only
  163. U32 mMipLevels; // For PBR rendering only
  164. bool mUseDepth;
  165. bool mStencil; // For EE rendering only
  166. public:
  167. // Whether or not to use FBO implementation
  168. static bool sUseFBO;
  169. static U32 sBytesAllocated;
  170. static U32 sCurFBO;
  171. static U32 sCurResX;
  172. static U32 sCurResY;
  173. };
  174. #endif