~thumper/nux/next-changes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright 2010 Inalogic® Inc.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License, as
 * published by the  Free Software Foundation; either version 2.1 or 3.0
 * of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 *
 */


#ifndef GLDEVICEFRAMEBUFFEROBJECT_H
#define GLDEVICEFRAMEBUFFEROBJECT_H

/*!
FramebufferObject Class. This class encapsulates the FramebufferObject
(FBO) OpenGL spec. See the official spec at:
http://oss.sgi.com/projects/ogl-sample/registry/EXT/framebuffer_object.txt

for details.

A framebuffer object (FBO) is conceptually a structure containing pointers
to GPU memory. The memory pointed to is either an OpenGL texture or an
OpenGL RenderBuffer. FBOs can be used to render to one or more textures,
share depth buffers between multiple sets of color buffers/textures and
are a complete replacement for pbuffers.

Performance Notes:
1) It is more efficient (but not required) to call Bind()
on an FBO before making multiple method calls. For example:

FramebufferObject fbo;
fbo.Bind();
fbo.AttachTexture(GL_COLOR_ATTACHMENT0_EXT, texId0);
fbo.AttachTexture(GL_COLOR_ATTACHMENT1_EXT, texId1);
fbo.IsValid();

To provide a complete encapsulation, the following usage
pattern works correctly but is less efficient:

FramebufferObject fbo;
// NOTE : No Bind() call
fbo.AttachTexture(GL_COLOR_ATTACHMENT0_EXT, texId0);
fbo.AttachTexture(GL_COLOR_ATTACHMENT1_EXT, texId1);
fbo.IsValid();

The first usage pattern binds the FBO only once, whereas
the second usage binds/unbinds the FBO for each method call.

2) Use FramebufferObject::Disable() sparingly. We have intentionally
left out an "Unbind()" method because it is largely unnecessary
and encourages rendundant Bind/Unbind coding. Binding an FBO is
usually much faster than enabling/disabling a pbuffer, but is
still a costly operation. When switching between multiple FBOs
and a visible OpenGL framebuffer, the following usage pattern
is recommended:

FramebufferObject fbo1, fbo2;
fbo1.Bind();
... Render ...
// NOTE : No Unbind/Disable here...

fbo2.Bind();
... Render ...

// Disable FBO rendering and return to visible window
// OpenGL framebuffer.
FramebufferObject::Disable();
*/

namespace nux
{

  class GLFramebufferObject
  {
  public:
    /// Ctor/Dtor
    GLFramebufferObject();
    virtual ~GLFramebufferObject();

    /// Bind this FBO as current render target
    void Bind();

    /// Bind a texture to the "attachment" point of this FBO
    virtual void AttachTexture ( GLenum attachment, GLenum texType, GLuint texId,
                                 int mipLevel = 0, int zSlice = 0);

    /// Bind an array of textures to multiple "attachment" points of this FBO
    ///  - By default, the first 'numTextures' attachments are used,
    ///    starting with GL_COLOR_ATTACHMENT0_EXT
    virtual void AttachTextures ( int numTextures,
                                  GLenum texTarget[],
                                  GLuint texId[],
                                  GLenum attachment[] = NULL,
                                  int mipLevel[]      = NULL,
                                  int zSlice[]        = NULL );

    /// Bind a render buffer to the "attachment" point of this FBO
    virtual void AttachRenderBuffer ( GLenum attachment, GLuint buffId );

    /// Free any resource bound to the "attachment" point of this FBO
    void Unattach ( GLenum attachment );

    /// Is this FBO currently a valid render target?
    ///  - Sends output to std::cerr by default but can
    ///    be a user-defined C++ stream
    ///
    /// NOTE : This function works correctly in debug build
    ///        mode but always returns "true" if NDEBUG is
    ///        is defined (optimized builds)
    bool IsValid();


    /// BEGIN : Accessors
    /// Is attached type GL_RENDERBUFFER_EXT or GL_TEXTURE?
    GLenum GetAttachedType ( GLenum attachment );

    /// What is the Id of Renderbuffer/texture currently
    /// attached to "attachement?"
    GLuint GetAttachedId ( GLenum attachment );

    /// Which mipmap level is currently attached to "attachement?"
    GLint  GetAttachedMipLevel ( GLenum attachment );

    /// Which cube face is currently attached to "attachment?"
    GLint  GetAttachedCubeFace ( GLenum attachment );

    /// Which z-slice is currently attached to "attachment?"
    GLint  GetAttachedZSlice ( GLenum attachment );
    /// END : Accessors


    /// BEGIN : Static methods global to all FBOs
    /// Return number of color attachments permitted
    static int GetMaxColorAttachments();

    /// Disable all FBO rendering and return to traditional,
    /// windowing-system controlled framebuffer
    ///  NOTE:
    ///     This is NOT an "unbind" for this specific FBO, but rather
    ///     disables all FBO rendering. This call is intentionally "static"
    ///     and named "Disable" instead of "Unbind" for this reason. The
    ///     motivation for this strange semantic is performance. Providing "Unbind"
    ///     would likely lead to a large number of unnecessary FBO enablings/disabling.
    static void Disable();
    /// END : Static methods global to all FBOs

  private:
    GLint m_fboId;
    GLint  m_savedFboId;

    void  _GuardedBind();
    void  _GuardedUnbind();
    void  _FramebufferTextureND ( GLenum attachment, GLenum texType,
                                  GLuint texId, int mipLevel, int zSlice );
    static GLuint _GenerateFboId();
  };

  /*!
  Renderbuffer Class. This class encapsulates the Renderbuffer OpenGL
  object described in the FramebufferObject (FBO) OpenGL spec.
  See the official spec at:
  http://oss.sgi.com/projects/ogl-sample/registry/EXT/framebuffer_object.txt
  for complete details.

  A "Renderbuffer" is a chunk of GPU memory used by FramebufferObjects to
  represent "traditional" framebuffer memory (depth, stencil, and color buffers).
  By "traditional," we mean that the memory cannot be bound as a texture.
  With respect to GPU shaders, Renderbuffer memory is "write-only." Framebuffer
  operations such as alpha blending, depth test, alpha test, stencil test, etc.
  read from this memory in post-fragement-shader (ROP) operations.

  The most common use of Renderbuffers is to create depth and stencil buffers.
  Note that as of 7/1/05, NVIDIA drivers to do not support stencil Renderbuffers.

  Usage Notes:
  1) "internalFormat" can be any of the following:
  Valid OpenGL internal formats beginning with:
  RGB, RGBA, DEPTH_COMPONENT

  or a stencil buffer format (not currently supported
  in NVIDIA drivers as of 7/1/05).
  STENCIL_INDEX1_EXT
  STENCIL_INDEX4_EXT
  STENCIL_INDEX8_EXT
  STENCIL_INDEX16_EXT

  */
  class GLRenderbuffer
  {
  public:
    /// Ctors/Dtors
    GLRenderbuffer();
    GLRenderbuffer (GLenum internalFormat, int width, int height);
    ~GLRenderbuffer();

    void   Bind();
    void   Unbind();
    void   Set (GLenum internalFormat, int width, int height);
    GLuint GetId() const;

    static GLint GetMaxSize();

  private:
    GLuint m_bufId;

    static GLuint _CreateBufferId();
  };

}

#endif // GLDEVICEFRAMEBUFFEROBJECT_H