~ubuntu-branches/ubuntu/oneiric/nux/oneiric

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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * 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>
 *
 */


#include "GLResource.h"
#include "GpuDevice.h"
#include "GLDeviceObjects.h"
#include "GLResourceManager.h"

#include "GLTextureResourceManager.h"
#include "GLVertexResourceManager.h"
#include "GLDeviceFrameBufferObject.h"
#include "GLTemplatePrimitiveBuffer.h"
#include "GraphicsEngine.h"

namespace nux
{

  GLenum AttachmentBuffer[] =
  {
    GL_COLOR_ATTACHMENT0_EXT
    , GL_COLOR_ATTACHMENT1_EXT
    , GL_COLOR_ATTACHMENT2_EXT
    , GL_COLOR_ATTACHMENT3_EXT
  };

//////////////////////////////////////////////////////////////////////////
// GLFramebufferObject
//////////////////////////////////////////////////////////////////////////

  GLFramebufferObject::GLFramebufferObject()
    :   m_fboId (0)
    ,   m_savedFboId (0)
  {
    m_fboId = _GenerateFboId();
    // Bind this FBO so that it actually gets created now
    _GuardedBind();
    _GuardedUnbind();
  }

  GLFramebufferObject::~GLFramebufferObject()
  {
    CHECKGL ( glDeleteFramebuffersEXT (1, (const GLuint *) &m_fboId) );
  }

  void GLFramebufferObject::Bind()
  {
    CHECKGL ( glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, m_fboId) );
  }

  void GLFramebufferObject::Disable()
  {
    CHECKGL ( glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0) );
  }

  void
  GLFramebufferObject::AttachTexture ( GLenum attachment, GLenum texType,
                                       GLuint texId, int mipLevel, int zSlice)
  {
    _GuardedBind();

    if ( GetAttachedId (attachment) != texId )
    {
      _FramebufferTextureND ( attachment, texType,
                              texId, mipLevel, zSlice );
    }
    else
    {
//        nuxError(TEXT("GLFramebufferObject::AttachTexture PERFORMANCE WARNING:\n
//            \tRedundant bind of texture (id = %d).\n"), texId);
    }

    _GuardedUnbind();
  }
  void
  GLFramebufferObject::AttachTextures ( int numTextures, GLenum texTarget[], GLuint texId[],
                                        GLenum attachment[], int mipLevel[], int zSlice[] )
  {
    for (int i = 0; i < numTextures; ++i)
    {
      AttachTexture ( texTarget[i], texId[i],
                      attachment ? attachment[i] : (GL_COLOR_ATTACHMENT0_EXT + i),
                      mipLevel ? mipLevel[i] : 0,
                      zSlice ? zSlice[i] : 0 );
    }
  }


  void
  GLFramebufferObject::AttachRenderBuffer ( GLenum attachment, GLuint buffId )
  {
    _GuardedBind();

    if ( GetAttachedId (attachment) != buffId )
    {
      CHECKGL ( glFramebufferRenderbufferEXT ( GL_FRAMEBUFFER_EXT, attachment,
                GL_RENDERBUFFER_EXT, buffId) );
    }
    else
    {
//        nuxError(TEXT("GLFramebufferObject::AttachRenderBuffer PERFORMANCE WARNING:\n
//            \tRedundant bind of GLRenderbuffer (id = %d).\n"), buffId);
    }

    _GuardedUnbind();
  }

  void
  GLFramebufferObject::Unattach ( GLenum attachment )
  {
    _GuardedBind();
    GLenum type = GetAttachedType (attachment);

    switch (type)
    {
      case GL_NONE:
        break;
      case GL_RENDERBUFFER_EXT:
        AttachRenderBuffer ( attachment, 0 );
        break;
      case GL_TEXTURE:
        AttachTexture ( attachment, GL_TEXTURE_2D, 0 );
        break;
      default:
        std::cout << "GLFramebufferObject::unbind_attachment ERROR: Unknown attached resource type\n";
    }

    _GuardedUnbind();
  }

  GLint GLFramebufferObject::GetMaxColorAttachments()
  {
    GLint maxAttach = 0;
    CHECKGL ( glGetIntegerv ( GL_MAX_COLOR_ATTACHMENTS_EXT, &maxAttach ) );
    return maxAttach;
  }

  GLuint GLFramebufferObject::_GenerateFboId()
  {
    GLuint id = 0;
    CHECKGL ( glGenFramebuffersEXT (1, &id) );
    return id;
  }

  void GLFramebufferObject::_GuardedBind()
  {
#ifndef NUX_OPENGLES_20
    // Only binds if m_fboId is different than the currently bound FBO
    CHECKGL ( glGetIntegerv ( GL_FRAMEBUFFER_BINDING_EXT, &m_savedFboId ) );

    if (m_fboId != m_savedFboId)
    {
      CHECKGL ( glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, m_fboId) );
    }
#else
    CHECKGL ( glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, m_fboId) );
#endif
  }

  void GLFramebufferObject::_GuardedUnbind()
  {
    // Returns FBO binding to the previously enabled FBO
    if (m_savedFboId != m_fboId)
    {
      CHECKGL ( glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, (GLuint) m_savedFboId) );
    }
  }

  void
  GLFramebufferObject::_FramebufferTextureND ( GLenum attachment, GLenum texType,
      GLuint texId, int mipLevel,
      int zSlice )
  {
    if (texType == GL_TEXTURE_2D)
    {
      // Default is GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE_ARB, or cube faces
      CHECKGL ( glFramebufferTexture2DEXT ( GL_FRAMEBUFFER_EXT, attachment,
                                            texType, texId, mipLevel ) );
    }
    else if (texType == GL_TEXTURE_1D)
    {
      CHECKGL ( glFramebufferTexture1DEXT ( GL_FRAMEBUFFER_EXT, attachment,
                                            GL_TEXTURE_1D, texId, mipLevel ) );
    }
    else if (texType == GL_TEXTURE_3D)
    {
      CHECKGL ( glFramebufferTexture3DEXT ( GL_FRAMEBUFFER_EXT, attachment,
                                            GL_TEXTURE_3D, texId, mipLevel, zSlice ) );
    }
  }

  bool GLFramebufferObject::IsValid()
  {
    _GuardedBind();

    bool isOK = false;

    GLenum status;
    status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
    CHECKGL_MSG (glCheckFramebufferStatusEXT);

    switch (status)
    {
      case GL_FRAMEBUFFER_COMPLETE_EXT: // Everything's OK
        isOK = true;
        break;
      case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
        nuxError (TEXT ("[GLFramebufferObject::IsValid] GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT") );
        isOK = false;
        break;
      case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
        nuxError (TEXT ("[GLFramebufferObject::IsValid] GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT") );
        isOK = false;
        break;
// See issue (87) of http://www.opengl.org/registry/specs/EXT/framebuffer_object.txt
//  case GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT:
//      nuxError(TEXT("[GLFramebufferObject::IsValid] GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT"));
//      isOK = false;
//      break;
      case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
        nuxError (TEXT ("[GLFramebufferObject::IsValid] GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT") );
        isOK = false;
        break;
      case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
        nuxError (TEXT ("[GLFramebufferObject::IsValid] GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT") );
        isOK = false;
        break;
      case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
        nuxError (TEXT ("[GLFramebufferObject::IsValid] GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT") );
        isOK = false;
        break;
      case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
        nuxError (TEXT ("[GLFramebufferObject::IsValid] GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT") );
        isOK = false;
        break;
//  case GL_FRAMEBUFFER_STATUS_ERROR_EXT:
//      nuxError(TEXT("[GLFramebufferObject::IsValid] GL_FRAMEBUFFER_STATUS_ERROR_EXT"));
//      isOK = false;
//      break;
      case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
        nuxError (TEXT ("[GLFramebufferObject::IsValid] GL_FRAMEBUFFER_UNSUPPORTED_EXT") );
        isOK = false;
        break;
      default:
        nuxError (TEXT ("[GLFramebufferObject::IsValid] Unknown ERROR") );
        isOK = false;
    }

    _GuardedUnbind();
    return isOK;
  }

/// Accessors
  GLenum GLFramebufferObject::GetAttachedType ( GLenum attachment )
  {
    // Returns GL_RENDERBUFFER_EXT or GL_TEXTURE
    _GuardedBind();
    GLint type = 0;
    CHECKGL ( glGetFramebufferAttachmentParameterivEXT (GL_FRAMEBUFFER_EXT, attachment,
              GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT,
              &type) );
    _GuardedUnbind();
    return GLenum (type);
  }

  GLuint GLFramebufferObject::GetAttachedId ( GLenum attachment )
  {
    _GuardedBind();
    GLint id = 0;
    CHECKGL ( glGetFramebufferAttachmentParameterivEXT (GL_FRAMEBUFFER_EXT, attachment,
              GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT,
              &id) );
    _GuardedUnbind();
    return GLuint (id);
  }

  GLint GLFramebufferObject::GetAttachedMipLevel ( GLenum attachment )
  {
    _GuardedBind();
    GLint level = 0;
    CHECKGL ( glGetFramebufferAttachmentParameterivEXT (GL_FRAMEBUFFER_EXT, attachment,
              GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT,
              &level) );
    _GuardedUnbind();
    return level;
  }

  GLint GLFramebufferObject::GetAttachedCubeFace ( GLenum attachment )
  {
    _GuardedBind();
    GLint level = 0;
    CHECKGL ( glGetFramebufferAttachmentParameterivEXT (GL_FRAMEBUFFER_EXT, attachment,
              GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT,
              &level) );
    _GuardedUnbind();
    return level;
  }

  GLint GLFramebufferObject::GetAttachedZSlice ( GLenum attachment )
  {
    _GuardedBind();
    GLint slice = 0;
    CHECKGL ( glGetFramebufferAttachmentParameterivEXT (GL_FRAMEBUFFER_EXT, attachment,
              GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT,
              &slice) );
    _GuardedUnbind();
    return slice;
  }

//////////////////////////////////////////////////////////////////////////
// GLRenderbuffer
//////////////////////////////////////////////////////////////////////////


  GLRenderbuffer::GLRenderbuffer()
    :   m_bufId (0)
  {
    m_bufId = _CreateBufferId();
  }

  GLRenderbuffer::GLRenderbuffer (GLenum internalFormat, int width, int height)
    : m_bufId (_CreateBufferId() )
  {
    Set (internalFormat, width, height);
  }

  GLRenderbuffer::~GLRenderbuffer()
  {
    CHECKGL ( glDeleteRenderbuffersEXT (1, &m_bufId) );
  }

  void GLRenderbuffer::Bind()
  {
    CHECKGL ( glBindRenderbufferEXT (GL_RENDERBUFFER_EXT, m_bufId) );
  }

  void GLRenderbuffer::Unbind()
  {
    CHECKGL ( glBindRenderbufferEXT (GL_RENDERBUFFER_EXT, 0) );
  }

  void GLRenderbuffer::Set (GLenum internalFormat, int width, int height)
  {
    int maxSize = GLRenderbuffer::GetMaxSize();

    if (width > maxSize || height > maxSize )
    {
      std::cout << "GLRenderbuffer::GLRenderbuffer() ERROR: Size too big width=" << width << "height=" << height << "\n";
      return;
    }

    // Guarded bind
    GLint savedId = 0;
    CHECKGL ( glGetIntegerv ( GL_RENDERBUFFER_BINDING_EXT, &savedId ) );

    if (savedId != (GLint) m_bufId)
    {
      Bind();
    }

    // Allocate memory for renderBuffer
    CHECKGL ( glRenderbufferStorageEXT (GL_RENDERBUFFER_EXT, internalFormat, width, height ) );

    // Guarded unbind
    if (savedId != (GLint) m_bufId)
    {
      Unbind();
    }
  }

  GLuint GLRenderbuffer::GetId() const
  {
    return m_bufId;
  }

  GLint GLRenderbuffer::GetMaxSize()
  {
    GLint maxAttach = 0;
    CHECKGL ( glGetIntegerv ( GL_MAX_RENDERBUFFER_SIZE_EXT, &maxAttach ) );
    return maxAttach;
  }

  GLuint GLRenderbuffer::_CreateBufferId()
  {
    GLuint id = 0;
    CHECKGL ( glGenRenderbuffersEXT (1, &id) );
    return id;
  }

}