~thumper/nux/next-changes

« back to all changes in this revision

Viewing changes to NuxGraphics/IOpenGLFrameBufferObject.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 21:15:42 UTC
  • Revision ID: neil.patel@canonical.com-20100901211542-cw2ce3ak28unouwb
Add NuxGraphics with licensing

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2010 Inalogic Inc.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it 
 
5
 * under the terms of the GNU Lesser General Public License version 3, as
 
6
 * published by the  Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but 
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of 
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 
 
11
 * PURPOSE.  See the applicable version of the GNU Lesser General Public 
 
12
 * License for more details.
 
13
 * 
 
14
 * You should have received a copy of both the GNU Lesser General Public 
 
15
 * License version 3 along with this program.  If not, see 
 
16
 * <http://www.gnu.org/licenses/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
 
19
 *
 
20
 */
 
21
 
 
22
 
 
23
#include "GLResource.h"
 
24
#include "GLDeviceFactory.h"
 
25
#include "GLDeviceObjects.h"
 
26
#include "IOpenGLFrameBufferObject.h"
 
27
#include "OpenGLEngine.h"
 
28
 
 
29
NAMESPACE_BEGIN_OGL
 
30
 
 
31
IMPLEMENT_OBJECT_TYPE(IOpenGLFrameBufferObject);
 
32
 
 
33
IOpenGLFrameBufferObject::IOpenGLFrameBufferObject(unsigned int Width, unsigned int Height, BitmapFormat PixelFormat)
 
34
:   _Width(Width)
 
35
,   _Height(Height)
 
36
,   _PixelFormat(PixelFormat)
 
37
,   _IsActive(false)
 
38
,   _Depth_Attachment(0)
 
39
,   _Stencil_Attachment(0)
 
40
,   IOpenGLResource(RTFRAMEBUFFEROBJECT)
 
41
{
 
42
    for (int i = 0; i < GetThreadGLDeviceFactory()->GetOpenGLMaxFrameBufferAttachment(); i++)
 
43
    {
 
44
        _Color_AttachmentArray.push_back(TRefGL<IOpenGLSurface>(0));
 
45
    }
 
46
 
 
47
    FormatFrameBufferObject(Width, Height, PixelFormat);
 
48
    GRunTimeStats.Register(this);
 
49
}
 
50
 
 
51
IOpenGLFrameBufferObject::~IOpenGLFrameBufferObject()
 
52
{
 
53
    // IOpenGLFrameBufferObject is an abstraction. Is does not have an opengl id.
 
54
    // _Fbo has an opengl id that is destroyed when the destructor is called.
 
55
    _OpenGLID = 0;
 
56
    GRunTimeStats.UnRegister(this);
 
57
}
 
58
 
 
59
int IOpenGLFrameBufferObject::FormatFrameBufferObject(unsigned int Width, unsigned int Height, BitmapFormat PixelFormat)
 
60
{
 
61
    Deactivate();
 
62
    for(int i=0; i< GetThreadGLDeviceFactory()->GetOpenGLMaxFrameBufferAttachment(); i++)
 
63
    {
 
64
        _Color_AttachmentArray[i] = NULL;
 
65
    }
 
66
    _Depth_Attachment = NULL;
 
67
    _Stencil_Attachment = NULL;
 
68
 
 
69
    if((_Width == Width) && (_Height == Height) && (_PixelFormat == PixelFormat))
 
70
        return 1;
 
71
 
 
72
    _Rbo.Set (GL_DEPTH_COMPONENT, Width, Height);
 
73
 
 
74
    // Clear clipping region stack
 
75
    _Width  = Width;
 
76
    _Height = Height;
 
77
    EmptyClippingRegion();
 
78
    _PixelFormat = PixelFormat;
 
79
    return 1;
 
80
}
 
81
 
 
82
int IOpenGLFrameBufferObject::SetRenderTarget(unsigned int ColorAttachmentIndex, TRefGL<IOpenGLSurface> pRenderTargetSurface)
 
83
{
 
84
    nuxAssert(ColorAttachmentIndex < GetThreadGLDeviceFactory()->GetOpenGLMaxFrameBufferAttachment());
 
85
    if(pRenderTargetSurface.IsNull())
 
86
    {
 
87
        _Color_AttachmentArray[ColorAttachmentIndex] = 0;
 
88
        return 1;
 
89
    }
 
90
 
 
91
    if(_Color_AttachmentArray[ColorAttachmentIndex] == pRenderTargetSurface)
 
92
    {
 
93
        return 1;
 
94
    }
 
95
 
 
96
    BitmapFormat PixelFormat = pRenderTargetSurface->GetPixelFormat();
 
97
    if(!(_Width == pRenderTargetSurface->GetWidth() && _Height == pRenderTargetSurface->GetHeight()))
 
98
    {
 
99
        nuxAssertMsg(0, TEXT("[IOpenGLFrameBufferObject::SetRenderTarget] Invalid surface size.") );
 
100
        return 0;
 
101
    }
 
102
 
 
103
    _Color_AttachmentArray[ColorAttachmentIndex] = pRenderTargetSurface;
 
104
    if(_IsActive)
 
105
    {
 
106
        Activate();
 
107
    }
 
108
    return 1;
 
109
}
 
110
 
 
111
int IOpenGLFrameBufferObject::SetDepthSurface(TRefGL<IOpenGLSurface> pDepthSurface)
 
112
{
 
113
    //nuxAssert(pDepthSurface.IsValid());
 
114
 
 
115
    if(pDepthSurface.IsNull())
 
116
    {
 
117
        _Depth_Attachment = 0;
 
118
        _Stencil_Attachment = 0;
 
119
        return 1;
 
120
    }
 
121
 
 
122
    BitmapFormat PixelFormat = pDepthSurface->GetPixelFormat();
 
123
 
 
124
    if(!(_Width == pDepthSurface->GetWidth() && _Height == pDepthSurface->GetHeight()))
 
125
    {
 
126
        nuxAssertMsg(0, TEXT("The depth surface size is not compatible with the frame buffer size.") );
 
127
        return 0;
 
128
    }
 
129
 
 
130
    if(_Depth_Attachment == pDepthSurface)
 
131
        return 1;
 
132
 
 
133
    // We rely on the fact that the depth texture is actually a D24_S8 texture.
 
134
    // That is, the surface for the depth and stencil attachment is the same. When we bound, the surface,
 
135
    // we explicitly bind the depth attachment and the stencil attachment with the same surface.
 
136
    _Depth_Attachment = pDepthSurface;
 
137
    _Stencil_Attachment = pDepthSurface;
 
138
 
 
139
    if(_IsActive)
 
140
    {
 
141
        Activate();
 
142
    }
 
143
    return 1;
 
144
}
 
145
 
 
146
TRefGL<IOpenGLSurface> IOpenGLFrameBufferObject::GetRenderTarget(unsigned int ColorAttachmentIndex)
 
147
{
 
148
    nuxAssert(ColorAttachmentIndex < GetThreadGLDeviceFactory()->GetOpenGLMaxFrameBufferAttachment());
 
149
    return _Color_AttachmentArray[ColorAttachmentIndex];
 
150
}
 
151
 
 
152
TRefGL<IOpenGLSurface> IOpenGLFrameBufferObject::GetDepthRenderTarget()
 
153
{
 
154
    return _Depth_Attachment;
 
155
}
 
156
 
 
157
int IOpenGLFrameBufferObject::Activate(bool WithClippingStack)
 
158
{
 
159
    GLuint NumBuffers = 0;
 
160
    _Fbo.Bind();
 
161
 
 
162
    if(GetThreadGLDeviceFactory())
 
163
        GetThreadGLDeviceFactory()->SetCurrentFrameBufferObject(this);
 
164
 
 
165
    for (int i = 0; i < GetThreadGLDeviceFactory()->GetOpenGLMaxFrameBufferAttachment(); i++)
 
166
    {
 
167
        if(_Color_AttachmentArray[i].IsValid())
 
168
        {
 
169
            GLenum target   = _Color_AttachmentArray[i]->GetSurfaceTarget();
 
170
            GLenum glID     = _Color_AttachmentArray[i]->GetOpenGLID();
 
171
            GLint level     = _Color_AttachmentArray[i]->GetMipLevel();
 
172
            CHECKGL( glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + i, target, glID, level) );
 
173
            CHECKGL( glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT + i) );
 
174
 
 
175
            NumBuffers++;
 
176
        }
 
177
        else
 
178
        {
 
179
            CHECKGL( glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + i, GL_TEXTURE_2D, 0, 0) );
 
180
        }
 
181
    }
 
182
 
 
183
//     if(_Depth_Attachment.IsValid())
 
184
//     {
 
185
//         GLenum target   = _Depth_Attachment->GetSurfaceTarget();
 
186
//         GLenum glID     = _Depth_Attachment->GetOpenGLID();
 
187
//         GLint level     = _Depth_Attachment->GetMipLevel();
 
188
//         CHECKGL( glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, target, glID, level) );
 
189
//     }
 
190
//     else
 
191
//     {
 
192
//         CHECKGL( glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0) );
 
193
//         // On the PC you need to bing the same D24S8 surface to the depth and the stencil attachment.
 
194
//     }
 
195
 
 
196
    //_Rbo.Set (GL_DEPTH_COMPONENT, _Width, _Height);
 
197
    CHECKGL( glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,
 
198
        GL_DEPTH_ATTACHMENT_EXT,
 
199
        GL_RENDERBUFFER_EXT,
 
200
        _Rbo.GetId()) );
 
201
 
 
202
    nuxAssert( _Fbo.IsValid() == true );
 
203
    if(GetThreadGraphicsContext())
 
204
        GetThreadGraphicsContext()->SetViewport(0, 0, _Width, _Height);
 
205
 
 
206
    if(WithClippingStack)
 
207
        ApplyClippingRegion();
 
208
 
 
209
    _IsActive = true;
 
210
    return 1;
 
211
}
 
212
 
 
213
// Restore the original opengl back buffer as defined when creating the opengl context(color + depth + stencil).
 
214
int IOpenGLFrameBufferObject::Deactivate()
 
215
{
 
216
    CHECKGL( glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 ) );
 
217
    CHECKGL( glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0) );
 
218
 
 
219
    if(GetThreadGLDeviceFactory())
 
220
        GetThreadGLDeviceFactory()->SetCurrentFrameBufferObject(0);
 
221
 
 
222
    if(GetThreadGraphicsContext())
 
223
        GetThreadGraphicsContext()->SetScissor(0, 0, _Width, _Height);
 
224
    _IsActive = false;
 
225
    return 1;
 
226
}
 
227
 
 
228
void IOpenGLFrameBufferObject::PushClippingRegion(Rect clip_rect)
 
229
{
 
230
    clip_rect.OffsetPosition(0, 0);
 
231
 
 
232
    Rect current_clip_rect;
 
233
    unsigned int stacksize = (unsigned int)_ClippingRegionStack.size();
 
234
 
 
235
    if(stacksize == 0)
 
236
    {
 
237
        current_clip_rect = Rect(0, 0, _Width, _Height);
 
238
    }
 
239
    else
 
240
    {
 
241
        current_clip_rect = _ClippingRegionStack[stacksize-1];
 
242
    }
 
243
 
 
244
    Rect rect;
 
245
    if(GetThreadGraphicsContext())
 
246
        rect = GetThreadGraphicsContext()->GetViewportRect();
 
247
    clip_rect.OffsetPosition(rect.x, _Height - (rect.y + rect.GetHeight()));
 
248
 
 
249
    Rect Intersection = current_clip_rect.Intersect(clip_rect);
 
250
    if(!Intersection.IsNull())
 
251
    {
 
252
        _ClippingRegionStack.push_back(Intersection);
 
253
 
 
254
        SetClippingRegion(Intersection.x + GetThreadGraphicsContext()->GetContextX(),
 
255
            _Height - Intersection.y - Intersection.GetHeight() - GetThreadGraphicsContext()->GetContextY(),
 
256
            Intersection.GetWidth(), Intersection.GetHeight());
 
257
    }
 
258
    else
 
259
    {
 
260
        _ClippingRegionStack.push_back(Rect(0, 0, 0, 0));
 
261
        SetClippingRegion(0, 0, 0, 0);
 
262
    }
 
263
}
 
264
 
 
265
void IOpenGLFrameBufferObject::PopClippingRegion()
 
266
{
 
267
    _ClippingRegionStack.pop_back();
 
268
    int stacksize = (int)_ClippingRegionStack.size();
 
269
    if(stacksize == 0)
 
270
    {
 
271
        SetClippingRegion(0, 0, _Width, _Height);
 
272
    }
 
273
    else
 
274
    {
 
275
        Rect current_clip_rect = _ClippingRegionStack[stacksize-1];
 
276
        SetClippingRegion(current_clip_rect.x, _Height - current_clip_rect.y - current_clip_rect.GetHeight(), current_clip_rect.GetWidth(), current_clip_rect.GetHeight());
 
277
    }
 
278
}
 
279
 
 
280
void IOpenGLFrameBufferObject::EmptyClippingRegion()
 
281
{
 
282
    _ClippingRegionStack.clear();
 
283
    {
 
284
        SetClippingRegion(0, 0, _Width, _Height);
 
285
    }
 
286
}
 
287
 
 
288
void IOpenGLFrameBufferObject::ApplyClippingRegion()
 
289
{
 
290
    int stacksize = (int)_ClippingRegionStack.size();
 
291
    if(stacksize == 0)
 
292
    {
 
293
        SetClippingRegion(0, 0, _Width, _Height);
 
294
    }
 
295
    else
 
296
    {
 
297
        Rect current_clip_rect = _ClippingRegionStack[stacksize-1];
 
298
        SetClippingRegion(current_clip_rect.x, _Height - current_clip_rect.y - current_clip_rect.GetHeight(), current_clip_rect.GetWidth(), current_clip_rect.GetHeight());
 
299
    }
 
300
}
 
301
 
 
302
void IOpenGLFrameBufferObject::SetClippingRegion(int x, int y, unsigned int width, unsigned int height)
 
303
{
 
304
    if(GetThreadGraphicsContext())
 
305
        GetThreadGraphicsContext()->SetScissor(x, y, width, height);
 
306
}
 
307
 
 
308
Rect IOpenGLFrameBufferObject::GetClippingRegion()
 
309
{
 
310
    unsigned int stacksize = (unsigned int)_ClippingRegionStack.size();
 
311
    if(stacksize == 0)
 
312
    {
 
313
        return Rect(0, 0, _Width, _Height);
 
314
    }
 
315
    else
 
316
    {
 
317
        Rect r = _ClippingRegionStack[stacksize-1];
 
318
        return r;
 
319
    }
 
320
}
 
321
NAMESPACE_END_OGL