PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/C/src/smokeParticles/framebufferObject.h

https://bitbucket.org/alex_merritt/nvidia-cuda-sdk32
C Header | 221 lines | 52 code | 24 blank | 145 comment | 0 complexity | 9daae9e9bcf625be39a32e96ea9b248b MD5 | raw file
  1. /*
  2. * Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
  3. *
  4. * Please refer to the NVIDIA end user license agreement (EULA) associated
  5. * with this source code for terms and conditions that govern your use of
  6. * this software. Any use, reproduction, disclosure, or distribution of
  7. * this software and related documentation outside the terms of the EULA
  8. * is strictly prohibited.
  9. *
  10. */
  11. /*
  12. Copyright (c) 2005,
  13. Aaron Lefohn (lefohn@cs.ucdavis.edu)
  14. Robert Strzodka (strzodka@stanford.edu)
  15. Adam Moerschell (atmoerschell@ucdavis.edu)
  16. All rights reserved.
  17. This software is licensed under the BSD open-source license. See
  18. http://www.opensource.org/licenses/bsd-license.php for more detail.
  19. *************************************************************
  20. Redistribution and use in source and binary forms, with or
  21. without modification, are permitted provided that the following
  22. conditions are met:
  23. Redistributions of source code must retain the above copyright notice,
  24. this list of conditions and the following disclaimer.
  25. Redistributions in binary form must reproduce the above copyright notice,
  26. this list of conditions and the following disclaimer in the documentation
  27. and/or other materials provided with the distribution.
  28. Neither the name of the University of Californa, Davis nor the names of
  29. the contributors may be used to endorse or promote products derived
  30. from this software without specific prior written permission.
  31. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  34. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  35. THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  36. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  37. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  38. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  40. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  41. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  42. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  43. OF SUCH DAMAGE.
  44. */
  45. #ifndef UCDAVIS_FRAMEBUFFER_OBJECT_H
  46. #define UCDAVIS_FRAMEBUFFER_OBJECT_H
  47. #include <GL/glew.h>
  48. #include <iostream>
  49. /*!
  50. FramebufferObject Class. This class encapsulates the FramebufferObject
  51. (FBO) OpenGL spec. See the official spec at:
  52. http://oss.sgi.com/projects/ogl-sample/registry/EXT/framebuffer_object.txt
  53. for details.
  54. A framebuffer object (FBO) is conceptually a structure containing pointers
  55. to GPU memory. The memory pointed to is either an OpenGL texture or an
  56. OpenGL RenderBuffer. FBOs can be used to render to one or more textures,
  57. share depth buffers between multiple sets of color buffers/textures and
  58. are a complete replacement for pbuffers.
  59. Performance Notes:
  60. 1) It is more efficient (but not required) to call Bind()
  61. on an FBO before making multiple method calls. For example:
  62. FramebufferObject fbo;
  63. fbo.Bind();
  64. fbo.AttachTexture(GL_TEXTURE_2D, texId0, GL_COLOR_ATTACHMENT0_EXT);
  65. fbo.AttachTexture(GL_TEXTURE_2D, texId1, GL_COLOR_ATTACHMENT1_EXT);
  66. fbo.IsValid();
  67. To provide a complete encapsulation, the following usage
  68. pattern works correctly but is less efficient:
  69. FramebufferObject fbo;
  70. // NOTE : No Bind() call
  71. fbo.AttachTexture(GL_TEXTURE_2D, texId0, GL_COLOR_ATTACHMENT0_EXT);
  72. fbo.AttachTexture(GL_TEXTURE_2D, texId1, GL_COLOR_ATTACHMENT1_EXT);
  73. fbo.IsValid();
  74. The first usage pattern binds the FBO only once, whereas
  75. the second usage binds/unbinds the FBO for each method call.
  76. 2) Use FramebufferObject::Disable() sparingly. We have intentionally
  77. left out an "Unbind()" method because it is largely unnecessary
  78. and encourages rendundant Bind/Unbind coding. Binding an FBO is
  79. usually much faster than enabling/disabling a pbuffer, but is
  80. still a costly operation. When switching between multiple FBOs
  81. and a visible OpenGL framebuffer, the following usage pattern
  82. is recommended:
  83. FramebufferObject fbo1, fbo2;
  84. fbo1.Bind();
  85. ... Render ...
  86. // NOTE : No Unbind/Disable here...
  87. fbo2.Bind();
  88. ... Render ...
  89. // Disable FBO rendering and return to visible window
  90. // OpenGL framebuffer.
  91. FramebufferObject::Disable();
  92. */
  93. class FramebufferObject
  94. {
  95. public:
  96. /// Ctor/Dtor
  97. FramebufferObject();
  98. virtual ~FramebufferObject();
  99. /// Bind this FBO as current render target
  100. void Bind();
  101. /// Bind a texture to the "attachment" point of this FBO
  102. virtual void AttachTexture( GLenum texTarget,
  103. GLuint texId,
  104. GLenum attachment = GL_COLOR_ATTACHMENT0_EXT,
  105. int mipLevel = 0,
  106. int zSlice = 0 );
  107. /// Bind an array of textures to multiple "attachment" points of this FBO
  108. /// - By default, the first 'numTextures' attachments are used,
  109. /// starting with GL_COLOR_ATTACHMENT0_EXT
  110. virtual void AttachTextures( int numTextures,
  111. GLenum texTarget[],
  112. GLuint texId[],
  113. GLenum attachment[] = NULL,
  114. int mipLevel[] = NULL,
  115. int zSlice[] = NULL );
  116. /// Bind a render buffer to the "attachment" point of this FBO
  117. virtual void AttachRenderBuffer( GLuint buffId,
  118. GLenum attachment = GL_COLOR_ATTACHMENT0_EXT );
  119. /// Bind an array of render buffers to corresponding "attachment" points
  120. /// of this FBO.
  121. /// - By default, the first 'numBuffers' attachments are used,
  122. /// starting with GL_COLOR_ATTACHMENT0_EXT
  123. virtual void AttachRenderBuffers( int numBuffers, GLuint buffId[],
  124. GLenum attachment[] = NULL );
  125. /// Free any resource bound to the "attachment" point of this FBO
  126. void Unattach( GLenum attachment );
  127. /// Free any resources bound to any attachment points of this FBO
  128. void UnattachAll();
  129. /// Is this FBO currently a valid render target?
  130. /// - Sends output to std::cerr by default but can
  131. /// be a user-defined C++ stream
  132. ///
  133. /// NOTE : This function works correctly in debug build
  134. /// mode but always returns "true" if NDEBUG is
  135. /// is defined (optimized builds)
  136. #ifndef NDEBUG
  137. bool IsValid( std::ostream& ostr = std::cerr );
  138. #else
  139. bool IsValid( std::ostream& ostr = std::cerr ) {
  140. return true;
  141. }
  142. #endif
  143. /// BEGIN : Accessors
  144. /// Is attached type GL_RENDERBUFFER_EXT or GL_TEXTURE?
  145. GLenum GetAttachedType( GLenum attachment );
  146. /// What is the Id of Renderbuffer/texture currently
  147. /// attached to "attachement?"
  148. GLuint GetAttachedId( GLenum attachment );
  149. /// Which mipmap level is currently attached to "attachement?"
  150. GLint GetAttachedMipLevel( GLenum attachment );
  151. /// Which cube face is currently attached to "attachment?"
  152. GLint GetAttachedCubeFace( GLenum attachment );
  153. /// Which z-slice is currently attached to "attachment?"
  154. GLint GetAttachedZSlice( GLenum attachment );
  155. /// END : Accessors
  156. /// BEGIN : Static methods global to all FBOs
  157. /// Return number of color attachments permitted
  158. static int GetMaxColorAttachments();
  159. /// Disable all FBO rendering and return to traditional,
  160. /// windowing-system controlled framebuffer
  161. /// NOTE:
  162. /// This is NOT an "unbind" for this specific FBO, but rather
  163. /// disables all FBO rendering. This call is intentionally "static"
  164. /// and named "Disable" instead of "Unbind" for this reason. The
  165. /// motivation for this strange semantic is performance. Providing
  166. /// "Unbind" would likely lead to a large number of unnecessary
  167. /// FBO enablings/disabling.
  168. static void Disable();
  169. /// END : Static methods global to all FBOs
  170. protected:
  171. void _GuardedBind();
  172. void _GuardedUnbind();
  173. void _FramebufferTextureND( GLenum attachment, GLenum texTarget,
  174. GLuint texId, int mipLevel, int zSlice );
  175. static GLuint _GenerateFboId();
  176. private:
  177. GLuint m_fboId;
  178. GLint m_savedFboId;
  179. };
  180. #endif