~thumper/nux/next-changes

« back to all changes in this revision

Viewing changes to NuxGraphics/GLDeviceFactory.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 "GLResourceManager.h"
 
27
 
 
28
#include "GLTextureResourceManager.h"
 
29
#include "GLVertexResourceManager.h"
 
30
#include "GLDeviceFrameBufferObject.h"
 
31
#include "GLTemplatePrimitiveBuffer.h"
 
32
#include "OpenGLEngine.h"
 
33
 
 
34
NAMESPACE_BEGIN_OGL
 
35
 
 
36
#define MANAGEDEVICERESOURCE    0
 
37
 
 
38
extern void cgErrorCallback(void);
 
39
 
 
40
// Pixel buffer object seems to corrupt textures. This can be seen when the window is moved
 
41
// to the second screen in a multi-display configuration... 
 
42
// Happens on geforce 6600. Does not happens on geforce 6800.
 
43
 
 
44
// ATI Radeon 4670 has problems loading textures from pixel buffer object. PBO should be deactivated if the 
 
45
// graphics card is made by AMD/ATI
 
46
#define INL_USE_PBO     1
 
47
 
 
48
#define INL_MISSING_GL_EXTENSION_MESSAGE_BOX(message) {MessageBox(NULL, TEXT("Missing extension: " #message), TEXT("ERROR"), MB_OK|MB_ICONERROR); exit(-1);}
 
49
 
 
50
// float Log2(float f)
 
51
// {
 
52
//     return logf(f) / logf(2);
 
53
// }
 
54
 
 
55
extern PixelFormatInfo GPixelFormats[];
 
56
 
 
57
struct ReqExtension
 
58
{
 
59
    const char* extension0;
 
60
    const char* extension1;
 
61
    const char* extension2;
 
62
    int supported0;
 
63
    int supported1;
 
64
    int supported2;
 
65
};
 
66
 
 
67
struct ReqExtension ReqNVidiaExtension[] = 
 
68
{
 
69
    {"GL_VERSION_1_5",              "",                         "",                         0,  0,  0},
 
70
    {"GL_ARB_texture_rectangle",    "GL_NV_texture_rectangle",  "GL_EXT_texture_rectangle", 0,  0,  0},
 
71
    {"GL_ARB_vertex_program",       "",                         "",                         0,  0,  0},
 
72
    {"GL_ARB_fragment_program",     "",                         "",                         0,  0,  0},
 
73
    {"GL_ARB_shader_objects",       "",                         "",                         0,  0,  0},
 
74
    {"GL_ARB_vertex_shader",        "",                         "",                         0,  0,  0},
 
75
    {"GL_ARB_fragment_shader",      "",                         "",                         0,  0,  0},
 
76
    {"GL_ARB_vertex_buffer_object", "",                         "",                         0,  0,  0},
 
77
    {"GL_EXT_framebuffer_object",   "",                         "",                         0,  0,  0},
 
78
    {"GL_EXT_draw_range_elements",  "",                         "",                         0,  0,  0},
 
79
    {"GL_EXT_stencil_two_side",     "",                         "",                         0,  0,  0},
 
80
};
 
81
 
 
82
struct ReqExtension ReqNVidiaWGLExtension[] = 
 
83
{
 
84
    {"WGL_ARB_pbuffer",             "",                         "",                         0,  0,  0},
 
85
    {"WGL_ARB_pixel_format",        "",                         "",                         0,  0,  0},
 
86
    {"WGL_ARB_render_texture",      "",                         "",                         0,  0,  0},
 
87
    {"WGL_ARB_render_texture",      "",                         "",                         0,  0,  0},
 
88
};
 
89
 
 
90
static void InitializeExtension()
 
91
{
 
92
 
 
93
    int NumReqExtension = sizeof(ReqNVidiaExtension) / sizeof(ReqNVidiaExtension[0]);
 
94
 
 
95
    for(int index = 0; index < NumReqExtension; index++)
 
96
    {
 
97
        ReqNVidiaExtension[index].supported0 = 0;
 
98
        ReqNVidiaExtension[index].supported1 = 0;
 
99
        ReqNVidiaExtension[index].supported2 = 0;
 
100
 
 
101
        if(ReqNVidiaExtension[index].extension0)
 
102
        {
 
103
            ReqNVidiaExtension[index].supported0 = glewIsSupported(ReqNVidiaExtension[index].extension0);
 
104
        }
 
105
        if(ReqNVidiaExtension[index].extension1)
 
106
        {
 
107
            ReqNVidiaExtension[index].supported1 = glewIsSupported(ReqNVidiaExtension[index].extension1);
 
108
        }
 
109
        if(ReqNVidiaExtension[index].extension2)
 
110
        {
 
111
            ReqNVidiaExtension[index].supported2 = glewIsSupported(ReqNVidiaExtension[index].extension2);
 
112
        }
 
113
 
 
114
        if((!ReqNVidiaExtension[index].supported0) &&
 
115
            (!ReqNVidiaExtension[index].supported1) &&
 
116
            (!ReqNVidiaExtension[index].supported2))
 
117
        {
 
118
//            char error[512];
 
119
//            sprintf(error, "Error - required extensions were not supported: \n %s \n %s \n %s \n"
 
120
//                ,ReqNVidiaExtension[index].extension0
 
121
//                ,ReqNVidiaExtension[index].extension1
 
122
//                ,ReqNVidiaExtension[index].extension2);
 
123
//            MessageBox(NULL,error,"ERROR",MB_OK|MB_ICONERROR);
 
124
//            exit(-1);
 
125
        }
 
126
//        else
 
127
//        {
 
128
//            if(ReqNVidiaExtension[index].supported0)
 
129
//                glh_init_extensions(ReqNVidiaExtension[index].extension0);
 
130
//            if(ReqNVidiaExtension[index].supported1)
 
131
//                glh_init_extensions(ReqNVidiaExtension[index].extension1);
 
132
//            if(ReqNVidiaExtension[index].supported2)
 
133
//                glh_init_extensions(ReqNVidiaExtension[index].extension2);
 
134
//        }
 
135
    }
 
136
// 
 
137
//     if (!glewIsSupported("GL_ARB_multitexture "
 
138
//         "GL_ARB_vertex_program "
 
139
//         "GL_ARB_fragment_program "
 
140
//         "GL_ARB_shader_objects "
 
141
//         "GL_ARB_vertex_shader "
 
142
//         "GL_ARB_fragment_shader "
 
143
//         //"GL_NV_float_buffer "
 
144
//         "GL_ARB_vertex_buffer_object "
 
145
//         //"GL_EXT_pixel_buffer_object "
 
146
//         //"GL_ARB_texture_non_power_of_two "
 
147
//         "GL_EXT_framebuffer_object "
 
148
//         "GL_ARB_texture_rectangle "
 
149
//         "GL_EXT_pixel_buffer_object"
 
150
//         )) 
 
151
//     {
 
152
// //        char error[512];
 
153
// //        sprintf(error, "Error - required extensions were not supported: %s", "XXXX");
 
154
// //        MessageBox(NULL,error,"ERROR",MB_OK|MB_ICONERROR);
 
155
// //        //fprintf(stderr, "Error - required extensions were not supported: %s", glh_get_unsupported_extensions());
 
156
// //        exit(-1);
 
157
//     }
 
158
// 
 
159
//     if (!wglewIsSupported(
 
160
//         "WGL_ARB_pbuffer "
 
161
//         "WGL_ARB_pixel_format "
 
162
//         "WGL_ARB_render_texture "
 
163
//         )) 
 
164
//     {
 
165
// //        char error[512];
 
166
// //        sprintf(error, "Error - required extensions were not supported: %s", "XXXX");
 
167
// //        MessageBox(NULL,error,"ERROR",MB_OK|MB_ICONERROR);
 
168
// //        //fprintf(stderr, "Error - required extensions were not supported: %s", glh_get_unsupported_extensions());
 
169
// //        exit(-1);
 
170
//     }
 
171
 
 
172
}
 
173
 
 
174
static void InitTextureFormats()
 
175
{
 
176
    GPixelFormats[ BITFMT_UNKNOWN               ].PlatformFormat        = GL_NONE;                                                      // Not supported for rendering.
 
177
    
 
178
    // Data in PC system memory: R(LSB) G B A(MSB) ---> GL Format:GL_RGBA - GL Type:GL_UNSIGNED_INT_8_8_8_8_REV
 
179
    GPixelFormats[ BITFMT_R8G8B8A8     ].PlatformFormat = GL_RGBA8;
 
180
    GPixelFormats[ BITFMT_R8G8B8A8     ].Format         = GL_RGBA;
 
181
    GPixelFormats[ BITFMT_R8G8B8A8     ].type               = GL_UNSIGNED_INT_8_8_8_8_REV;
 
182
    
 
183
    // Data in PC system memory: A(LSB) B G R(MSB) ---> GL Format:GL_RGBA - GL Type:GL_UNSIGNED_INT_8_8_8_8
 
184
    GPixelFormats[ BITFMT_A8B8G8R8     ].PlatformFormat = GL_RGBA8;
 
185
    GPixelFormats[ BITFMT_A8B8G8R8     ].Format         = GL_RGBA;
 
186
    GPixelFormats[ BITFMT_A8B8G8R8     ].type               = GL_UNSIGNED_INT_8_8_8_8;
 
187
 
 
188
    // Data in PC system memory: B(LSB) G R A(MSB) ---> GL Format:GL_BGRA - GL Type:GL_UNSIGNED_INT_8_8_8_8_REV
 
189
    GPixelFormats[ BITFMT_B8G8R8A8     ].PlatformFormat = GL_RGBA8;
 
190
    GPixelFormats[ BITFMT_B8G8R8A8     ].Format         = GL_BGRA;
 
191
    GPixelFormats[ BITFMT_B8G8R8A8     ].type               = GL_UNSIGNED_INT_8_8_8_8_REV;
 
192
 
 
193
    // Data in PC system memory: A(LSB) R G B(MSB) ---> GL Format:GL_BGRA - GL Type:GL_UNSIGNED_INT_8_8_8_8
 
194
    GPixelFormats[ BITFMT_A8R8G8B8     ].PlatformFormat = GL_RGBA8;
 
195
    GPixelFormats[ BITFMT_A8R8G8B8     ].Format         = GL_BGRA;
 
196
    GPixelFormats[ BITFMT_A8R8G8B8     ].type               = GL_UNSIGNED_INT_8_8_8_8;
 
197
 
 
198
    // Data in PC system memory: R(LSB) G B(MSB) ---> GL Format:GL_RGB - GL Type:GL_UNSIGNED
 
199
    GPixelFormats[ BITFMT_R8G8B8                ].PlatformFormat        = GL_RGB8;
 
200
    GPixelFormats[ BITFMT_R8G8B8                ].Format                = GL_RGB;
 
201
    GPixelFormats[ BITFMT_R8G8B8                ].type              = GL_UNSIGNED_BYTE;
 
202
 
 
203
    GPixelFormats[ BITFMT_B8G8R8                ].PlatformFormat        = GL_RGB8;
 
204
    GPixelFormats[ BITFMT_B8G8R8                ].Format                = GL_BGR;
 
205
    GPixelFormats[ BITFMT_B8G8R8                ].type              = GL_UNSIGNED_BYTE;
 
206
 
 
207
    GPixelFormats[ BITFMT_R5G6B5       ].PlatformFormat = GL_RGB5;
 
208
    GPixelFormats[ BITFMT_R5G6B5       ].Format         = GL_RGB;
 
209
    GPixelFormats[ BITFMT_R5G6B5       ].type               = GL_UNSIGNED_SHORT_5_6_5;
 
210
    
 
211
    GPixelFormats[ BITFMT_RGBA16F               ].PlatformFormat        = GL_RGBA16F_ARB;
 
212
    GPixelFormats[ BITFMT_RGBA16F               ].Format                = GL_RGBA;
 
213
    GPixelFormats[ BITFMT_RGBA16F               ].type              = GL_HALF_FLOAT_ARB;
 
214
 
 
215
    GPixelFormats[ BITFMT_RGB32F                ].PlatformFormat        = GL_RGB;
 
216
    GPixelFormats[ BITFMT_RGB32F                ].Format                = GL_RGB;
 
217
    GPixelFormats[ BITFMT_RGB32F                ].type              = GL_FLOAT;
 
218
 
 
219
    GPixelFormats[ BITFMT_RGBA32F               ].PlatformFormat        = GL_RGBA32F_ARB;
 
220
    GPixelFormats[ BITFMT_RGBA32F               ].Format                = GL_RGBA;
 
221
    GPixelFormats[ BITFMT_RGBA32F               ].type              = GL_FLOAT;
 
222
 
 
223
    // Note: Using GL_DEPTH_COMPONENT24 or GL_DEPTH_COMPONENT for PlatformFormat generate error GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT.
 
224
    GPixelFormats[ BITFMT_D24S8         ].PlatformFormat        = GL_DEPTH24_STENCIL8_EXT;
 
225
    GPixelFormats[ BITFMT_D24S8         ].Format                = GL_DEPTH_STENCIL_EXT;     // or GL_DEPTH_STENCIL_NV;
 
226
    GPixelFormats[ BITFMT_D24S8         ].type              = GL_UNSIGNED_INT_24_8_EXT; // or GL_UNSIGNED_INT_24_8_NV;
 
227
 
 
228
    GPixelFormats[ BITFMT_DXT1                  ].PlatformFormat        = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
 
229
    GPixelFormats[ BITFMT_DXT2                  ].PlatformFormat        = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
 
230
    GPixelFormats[ BITFMT_DXT3                  ].PlatformFormat        = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
 
231
    GPixelFormats[ BITFMT_DXT4                  ].PlatformFormat        = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
 
232
    GPixelFormats[ BITFMT_DXT5                  ].PlatformFormat        = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
 
233
 
 
234
    GPixelFormats[ BITFMT_R10G10B10A2  ].PlatformFormat = GL_RGB10_A2;
 
235
    GPixelFormats[ BITFMT_R10G10B10A2  ].Format         = GL_RGBA;
 
236
    GPixelFormats[ BITFMT_R10G10B10A2  ].type               = GL_UNSIGNED_INT_10_10_10_2;
 
237
 
 
238
    GPixelFormats[ BITFMT_A2B10G10R10  ].PlatformFormat = GL_RGB10_A2;
 
239
    GPixelFormats[ BITFMT_A2B10G10R10  ].Format         = GL_RGBA;
 
240
    GPixelFormats[ BITFMT_A2B10G10R10  ].type               = GL_UNSIGNED_INT_2_10_10_10_REV;
 
241
 
 
242
    GPixelFormats[ BITFMT_B10G10R10A2  ].PlatformFormat = GL_RGB10_A2;
 
243
    GPixelFormats[ BITFMT_B10G10R10A2  ].Format         = GL_BGRA;
 
244
    GPixelFormats[ BITFMT_B10G10R10A2  ].type               = GL_UNSIGNED_INT_10_10_10_2;
 
245
 
 
246
    GPixelFormats[ BITFMT_A2R10G10B10  ].PlatformFormat = GL_RGB10_A2;
 
247
    GPixelFormats[ BITFMT_A2R10G10B10  ].Format         = GL_BGRA;
 
248
    GPixelFormats[ BITFMT_A2R10G10B10  ].type               = GL_UNSIGNED_INT_2_10_10_10_REV;
 
249
 
 
250
    GPixelFormats[ BITFMT_A8           ].PlatformFormat = GL_RGBA8;
 
251
    GPixelFormats[ BITFMT_A8           ].Format         = GL_LUMINANCE;
 
252
    GPixelFormats[ BITFMT_A8           ].type               = GL_UNSIGNED_BYTE;
 
253
}
 
254
 
 
255
TRefGL<IOpenGLTexture2D>   GLDeviceFactory::pDefaultRenderTargetTexture = 0;
 
256
TRefGL<IOpenGLSurface>     GLDeviceFactory::pDefaultRenderTargetSurface = 0;
 
257
 
 
258
 
 
259
TRefGL<IOpenGLTexture2D> GLDeviceFactory::CreateTexture(
 
260
    int Width
 
261
    , int Height
 
262
    , int Levels
 
263
    , BitmapFormat PixelFormat)
 
264
{
 
265
    IOpenGLTexture2D* ptr;
 
266
    CreateTexture(Width, Height, Levels, PixelFormat, (IOpenGLTexture2D**)&ptr);
 
267
    TRefGL<IOpenGLTexture2D> h = ptr;
 
268
    return h;
 
269
}
 
270
 
 
271
TRefGL<IOpenGLRectangleTexture> GLDeviceFactory::CreateRectangleTexture(
 
272
    int Width
 
273
    , int Height
 
274
    , int Levels
 
275
    , BitmapFormat PixelFormat)
 
276
{
 
277
    IOpenGLRectangleTexture* ptr;
 
278
    CreateRectangleTexture(Width, Height, Levels, PixelFormat, (IOpenGLRectangleTexture**)&ptr);
 
279
    TRefGL<IOpenGLRectangleTexture> h = ptr;
 
280
    return h;
 
281
}
 
282
 
 
283
TRefGL<IOpenGLCubeTexture> GLDeviceFactory::CreateCubeTexture(
 
284
    int EdgeLength
 
285
    , int Levels
 
286
    , BitmapFormat PixelFormat)
 
287
{
 
288
    IOpenGLCubeTexture* ptr;
 
289
    CreateCubeTexture(EdgeLength, Levels, PixelFormat, (IOpenGLCubeTexture**)&ptr);
 
290
    TRefGL<IOpenGLCubeTexture> h = ptr;
 
291
    return h;
 
292
}
 
293
 
 
294
TRefGL<IOpenGLVolumeTexture> GLDeviceFactory::CreateVolumeTexture(
 
295
    int Width
 
296
    , int Height
 
297
    , int Depth
 
298
    , int Levels
 
299
    , BitmapFormat PixelFormat)
 
300
{
 
301
    IOpenGLVolumeTexture* ptr;
 
302
    CreateVolumeTexture(Width, Height, Depth, Levels, PixelFormat, (IOpenGLVolumeTexture**)&ptr);
 
303
    TRefGL<IOpenGLVolumeTexture> h = ptr;
 
304
    return h;
 
305
}
 
306
 
 
307
TRefGL<IOpenGLAnimatedTexture> GLDeviceFactory::CreateAnimatedTexture(
 
308
    int Width
 
309
    , int Height
 
310
    , int Depth
 
311
    , BitmapFormat PixelFormat)
 
312
{
 
313
    IOpenGLAnimatedTexture* prt;
 
314
    CreateAnimatedTexture(Width, Height, Depth, PixelFormat, (IOpenGLAnimatedTexture**)&prt);
 
315
    TRefGL<IOpenGLAnimatedTexture> h = prt;
 
316
    return h;
 
317
}
 
318
 
 
319
 
 
320
TRefGL<IOpenGLQuery> GLDeviceFactory::CreateQuery(QUERY_TYPE Type)
 
321
{
 
322
    IOpenGLQuery* prt;
 
323
    CreateQuery(Type, (IOpenGLQuery**)&prt);
 
324
    TRefGL<IOpenGLQuery> h = prt;
 
325
    return h;
 
326
}
 
327
 
 
328
TRefGL<IOpenGLFrameBufferObject> GLDeviceFactory::CreateFrameBufferObject()
 
329
{
 
330
    IOpenGLFrameBufferObject* ptr;
 
331
    CreateFrameBufferObject((IOpenGLFrameBufferObject**)&ptr);
 
332
    TRefGL<IOpenGLFrameBufferObject> h = ptr;
 
333
    return h;
 
334
}
 
335
 
 
336
TRefGL<IOpenGLShaderProgram> GLDeviceFactory::CreateShaderProgram()
 
337
{
 
338
    IOpenGLShaderProgram* ptr;
 
339
    CreateShaderProgram((IOpenGLShaderProgram**)&ptr);
 
340
    TRefGL<IOpenGLShaderProgram> h = ptr;
 
341
    return h;
 
342
}
 
343
 
 
344
TRefGL<IOpenGLVertexShader> GLDeviceFactory::CreateVertexShader()
 
345
{
 
346
    IOpenGLVertexShader* ptr;
 
347
    CreateVertexShader((IOpenGLVertexShader**)&ptr);
 
348
    TRefGL<IOpenGLVertexShader> h = ptr;
 
349
    return h;
 
350
}
 
351
 
 
352
TRefGL<IOpenGLPixelShader> GLDeviceFactory::CreatePixelShader()
 
353
{
 
354
    IOpenGLPixelShader* ptr;
 
355
    CreatePixelShader((IOpenGLPixelShader**)&ptr);
 
356
    TRefGL<IOpenGLPixelShader> h = ptr;
 
357
    return h;
 
358
}
 
359
 
 
360
TRefGL<IOpenGLAsmShaderProgram> GLDeviceFactory::CreateAsmShaderProgram()
 
361
{
 
362
    IOpenGLAsmShaderProgram* ptr;
 
363
    CreateAsmShaderProgram((IOpenGLAsmShaderProgram**)&ptr);
 
364
    TRefGL<IOpenGLAsmShaderProgram> h = ptr;
 
365
    return h;
 
366
}
 
367
 
 
368
TRefGL<IOpenGLAsmVertexShader> GLDeviceFactory::CreateAsmVertexShader()
 
369
{
 
370
    IOpenGLAsmVertexShader* ptr;
 
371
    CreateAsmVertexShader((IOpenGLAsmVertexShader**)&ptr);
 
372
    TRefGL<IOpenGLAsmVertexShader> h = ptr;
 
373
    return h;
 
374
}
 
375
 
 
376
TRefGL<IOpenGLAsmPixelShader> GLDeviceFactory::CreateAsmPixelShader()
 
377
{
 
378
    IOpenGLAsmPixelShader* ptr;
 
379
    CreateAsmPixelShader((IOpenGLAsmPixelShader**)&ptr);
 
380
    TRefGL<IOpenGLAsmPixelShader> h = ptr;
 
381
    return h;
 
382
}
 
383
 
 
384
#if (NUX_ENABLE_CG_SHADERS)
 
385
TRefGL<ICgVertexShader> GLDeviceFactory::CreateCGVertexShader()
 
386
{
 
387
    ICgVertexShader* ptr;
 
388
    CreateCGVertexShader((ICgVertexShader**)&ptr);
 
389
    TRefGL<ICgVertexShader> h = ptr;
 
390
    return h;
 
391
}
 
392
 
 
393
TRefGL<ICgPixelShader> GLDeviceFactory::CreateCGPixelShader()
 
394
{
 
395
    ICgPixelShader* ptr;
 
396
    CreateCGPixelShader((ICgPixelShader**)&ptr);
 
397
    TRefGL<ICgPixelShader> h = ptr;
 
398
    return h;
 
399
}
 
400
#endif 
 
401
 
 
402
void GLDeviceFactory::Initialize()
 
403
{
 
404
    GLenum Glew_Ok = glewInit();
 
405
    nuxAssert(Glew_Ok == GLEW_OK);
 
406
    InitializeExtension();
 
407
}
 
408
 
 
409
STREAMSOURCE GLDeviceFactory::_StreamSource[MAX_NUM_STREAM];
 
410
// TRefGL<IOpenGLIndexBuffer> GLDeviceFactory::_CurrentIndexBuffer = 0;
 
411
// TRefGL<IOpenGLVertexBuffer> GLDeviceFactory::_CurrentVertexBuffer = 0;
 
412
// TRefGL<IOpenGLVertexDeclaration> GLDeviceFactory::_CurrentVertexDeclaration = 0;
 
413
 
 
414
//void TestARBShaders()
 
415
//{
 
416
//    int OPENGL_PROGRAM_LENGTH_ARB;
 
417
//    int OPENGL_PROGRAM_FORMAT_ARB;
 
418
//    int OPENGL_PROGRAM_BINDING_ARB;
 
419
//    int OPENGL_PROGRAM_INSTRUCTIONS_ARB;
 
420
//    int OPENGL_MAX_PROGRAM_INSTRUCTIONS_ARB;
 
421
//    int OPENGL_PROGRAM_NATIVE_INSTRUCTIONS_ARB;
 
422
//    int OPENGL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB;
 
423
//    int OPENGL_PROGRAM_TEMPORARIES_ARB;
 
424
//    int OPENGL_MAX_PROGRAM_TEMPORARIES_ARB;
 
425
//    int OPENGL_PROGRAM_NATIVE_TEMPORARIES_ARB;
 
426
//    int OPENGL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB;
 
427
//    int OPENGL_PROGRAM_PARAMETERS_ARB;
 
428
//    int OPENGL_MAX_PROGRAM_PARAMETERS_ARB;
 
429
//    int OPENGL_PROGRAM_NATIVE_PARAMETERS_ARB;
 
430
//    int OPENGL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB;
 
431
//    int OPENGL_PROGRAM_ATTRIBS_ARB;
 
432
//    int OPENGL_MAX_PROGRAM_ATTRIBS_ARB;
 
433
//    int OPENGL_PROGRAM_NATIVE_ATTRIBS_ARB;
 
434
//    int OPENGL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB;
 
435
//    int OPENGL_PROGRAM_ADDRESS_REGISTERS_ARB;
 
436
//    int OPENGL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB;
 
437
//    int OPENGL_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB;
 
438
//    int OPENGL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB;
 
439
//    int OPENGL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB;
 
440
//    int OPENGL_MAX_PROGRAM_ENV_PARAMETERS_ARB;
 
441
//    int OPENGL_PROGRAM_UNDER_NATIVE_LIMITS_ARB;
 
442
//
 
443
//    // BEWARE glGetProgramiv != glGetProgramivARB (ARB fragment vertex program)
 
444
//
 
445
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_LENGTH_ARB,                          &OPENGL_PROGRAM_LENGTH_ARB) );                      
 
446
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ARB,                          &OPENGL_PROGRAM_FORMAT_ARB) );
 
447
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_BINDING_ARB,                         &OPENGL_PROGRAM_BINDING_ARB) );
 
448
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_INSTRUCTIONS_ARB,                    &OPENGL_PROGRAM_INSTRUCTIONS_ARB) );
 
449
//    CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_INSTRUCTIONS_ARB,                &OPENGL_MAX_PROGRAM_INSTRUCTIONS_ARB) );
 
450
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_NATIVE_INSTRUCTIONS_ARB,             &OPENGL_PROGRAM_NATIVE_INSTRUCTIONS_ARB) );
 
451
//    CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB,         &OPENGL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB) );
 
452
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_TEMPORARIES_ARB,                     &OPENGL_PROGRAM_TEMPORARIES_ARB) );
 
453
//    CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_TEMPORARIES_ARB,                 &OPENGL_MAX_PROGRAM_TEMPORARIES_ARB) );
 
454
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_NATIVE_TEMPORARIES_ARB,              &OPENGL_PROGRAM_NATIVE_TEMPORARIES_ARB) );
 
455
//    CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB,          &OPENGL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB) );
 
456
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_PARAMETERS_ARB,                      &OPENGL_PROGRAM_PARAMETERS_ARB) );
 
457
//    CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_PARAMETERS_ARB,                  &OPENGL_MAX_PROGRAM_PARAMETERS_ARB) );
 
458
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_NATIVE_PARAMETERS_ARB,               &OPENGL_PROGRAM_NATIVE_PARAMETERS_ARB) );
 
459
//    CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB,           &OPENGL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB) );
 
460
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_ATTRIBS_ARB,                         &OPENGL_PROGRAM_ATTRIBS_ARB) );
 
461
//    CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_ATTRIBS_ARB,                     &OPENGL_MAX_PROGRAM_ATTRIBS_ARB) );
 
462
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_NATIVE_ATTRIBS_ARB,                  &OPENGL_PROGRAM_NATIVE_ATTRIBS_ARB) );
 
463
//    CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB,              &OPENGL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB) );
 
464
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_ADDRESS_REGISTERS_ARB,               &OPENGL_PROGRAM_ADDRESS_REGISTERS_ARB) );
 
465
//    CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB,           &OPENGL_MAX_PROGRAM_ADDRESS_REGISTERS_ARB) );
 
466
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB,        &OPENGL_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB) );
 
467
//    CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB,    &OPENGL_MAX_PROGRAM_NATIVE_ADDRESS_REGISTERS_ARB) );
 
468
//    CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB,            &OPENGL_MAX_PROGRAM_LOCAL_PARAMETERS_ARB) );
 
469
//    CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_MAX_PROGRAM_ENV_PARAMETERS_ARB,              &OPENGL_MAX_PROGRAM_ENV_PARAMETERS_ARB) );
 
470
//    //CHECKGL (glGetProgramivARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_UNDER_NATIVE_LIMITS_ARB,             &OPENGL_PROGRAM_UNDER_NATIVE_LIMITS_ARB) );
 
471
//}
 
472
//
 
473
 
 
474
GLDeviceFactory::GLDeviceFactory(t_u32 DeviceWidth, t_u32 DeviceHeight, BitmapFormat DeviceFormat)
 
475
:   _PixelStoreAlignment(4)
 
476
,   _CachedVertexBufferList(0)
 
477
,   _CachedIndexBufferList(0)
 
478
,   _CachedTextureList(0)
 
479
,   _CachedTextureRectangleList(0)
 
480
,   _CachedCubeTextureList(0)
 
481
,   _CachedVolumeTextureList(0)
 
482
,   _CachedAnimatedTextureList(0)
 
483
,   _CachedVertexDeclarationList(0)
 
484
,   _CachedQueryList(0)
 
485
,   _CachedFrameBufferList(0)
 
486
,   _CachedVertexShaderList(0)
 
487
,   _CachedPixelShaderList(0)
 
488
,   _CachedShaderProgramList(0)
 
489
,   _CachedPixelBufferObjectList(0)
 
490
,   _FrameBufferObject(0)
 
491
,   m_isATIBoard(false)
 
492
,   m_isNVIDIABoard(false)
 
493
,   m_isINTELBoard(false)
 
494
,   m_UsePixelBufferObject(false)
 
495
,   m_GraphicsBoardVendor(BOARD_UNKNOWN)
 
496
#if (NUX_ENABLE_CG_SHADERS)
 
497
,   _CachedCGVertexShaderList(0)
 
498
,   _CachedCGPixelShaderList(0)
 
499
,   m_Cgcontext(0)
 
500
#endif
 
501
{
 
502
    inlSetThreadLocalStorage(ThreadLocal_GLDeviceFactory, this);
 
503
 
 
504
    GLenum Glew_Ok = 0;
 
505
#ifdef GLEW_MX
 
506
    Glew_Ok = glewContextInit(glewGetContext());
 
507
    nuxAssertMsg(Glew_Ok == GLEW_OK, TEXT("[GLDeviceFactory::GLDeviceFactory] GL Extensions failed to initialize."));
 
508
 
 
509
#if defined(INL_OS_WINDOWS)
 
510
    Glew_Ok = wglewContextInit(wglewGetContext());
 
511
#elif defined(INL_OS_LINUX)
 
512
    Glew_Ok = glxewContextInit(glxewGetContext());
 
513
#elif defined(INL_OS_MACOSX)
 
514
    Glew_Ok = glxewContextInit(glxewGetContext());
 
515
#endif
 
516
    nuxAssertMsg(Glew_Ok == GLEW_OK, TEXT("[GLDeviceFactory::GLDeviceFactory] WGL Extensions failed to initialize."));
 
517
#else
 
518
    Glew_Ok = glewInit();
 
519
#endif
 
520
 
 
521
    InitializeExtension();
 
522
 
 
523
    //m_BoardVendorString = "aaaa";
 
524
    //std::string str = (const char*) glGetString(GL_VENDOR);
 
525
    m_BoardVendorString = ANSI_TO_TCHAR(INL_REINTERPRET_CAST(const char*, glGetString(GL_VENDOR)));
 
526
    CHECKGL_MSG(glGetString(GL_VENDOR));
 
527
    m_BoardRendererString = ANSI_TO_TCHAR(INL_REINTERPRET_CAST(const char*, glGetString(GL_RENDERER)));
 
528
    CHECKGL_MSG(glGetString(GL_RENDERER));
 
529
    m_OpenGLVersionString = ANSI_TO_TCHAR(INL_REINTERPRET_CAST(const char*, glGetString(GL_VERSION)));
 
530
    CHECKGL_MSG(glGetString(GL_VERSION));
 
531
    m_GLSLVersionString = ANSI_TO_TCHAR(INL_REINTERPRET_CAST(const char*, glGetString(GL_SHADING_LANGUAGE_VERSION)));
 
532
    CHECKGL_MSG(glGetString(GL_SHADING_LANGUAGE_VERSION));
 
533
 
 
534
    nuxDebugMsg(TEXT("Board Vendor: %s"), m_BoardVendorString.GetTCharPtr());
 
535
    nuxDebugMsg(TEXT("Board Renderer: %s"), m_BoardRendererString.GetTCharPtr());
 
536
    nuxDebugMsg(TEXT("Board OpenGL Version: %s"), m_OpenGLVersionString.GetTCharPtr());
 
537
    nuxDebugMsg(TEXT("Board GLSL Version: %s"), m_GLSLVersionString.GetTCharPtr());
 
538
 
 
539
    // See: http://developer.nvidia.com/object/General_FAQ.html
 
540
    // The value of GL_MAX_TEXTURE_UNITS is 4 for GeForce FX and GeForce 6 Series GPUs. Why is that, since those GPUs have 16 texture units?
 
541
    glGetIntegerv(GL_MAX_TEXTURE_UNITS, &OPENGL_MAX_TEXTURE_UNITS);
 
542
    glGetIntegerv(GL_MAX_TEXTURE_COORDS, &OPENGL_MAX_TEXTURE_COORDS);
 
543
    glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &OPENGL_MAX_TEXTURE_IMAGE_UNITS);
 
544
 
 
545
    glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &OPENGL_MAX_VERTEX_ATTRIBUTES);
 
546
 
 
547
//     // ARB shaders
 
548
//     int OPENGL_MAX_VERTEX_ATTRIBS_ARB;
 
549
//     int OPENGL_MAX_PROGRAM_MATRIX_STACK_DEPTH;
 
550
// 
 
551
//     CHECKGL(glGetIntegerv(GL_MAX_VERTEX_ATTRIBS_ARB, &OPENGL_MAX_VERTEX_ATTRIBS_ARB));
 
552
//     CHECKGL(glGetIntegerv(GL_MAX_PROGRAM_MATRIX_STACK_DEPTH_ARB, &OPENGL_MAX_PROGRAM_MATRIX_STACK_DEPTH));
 
553
 
 
554
    //TestARBShaders();
 
555
 
 
556
    OPENGL_MAX_FB_ATTACHMENT = 4;
 
557
 
 
558
    NString TempStr = (const TCHAR*)TCharToUpperCase(m_BoardVendorString.GetTCharPtr());
 
559
    if(TempStr.FindFirstOccurence(TEXT("NVIDIA")) != -1)
 
560
    {
 
561
        m_isNVIDIABoard = true;
 
562
        m_GraphicsBoardVendor = BOARD_NVIDIA;
 
563
    }
 
564
    else if(TempStr.FindFirstOccurence(TEXT("ATI")) != -1)
 
565
    {
 
566
        m_isATIBoard = true;
 
567
        m_GraphicsBoardVendor = BOARD_ATI;
 
568
    }
 
569
    else if(TempStr.FindFirstOccurence(TEXT("TUNGSTEN")) != -1)
 
570
    {
 
571
        m_isINTELBoard = true;
 
572
        m_GraphicsBoardVendor = BOARD_INTEL;
 
573
    }
 
574
 
 
575
    if(INL_USE_PBO)
 
576
    {
 
577
        if(isATIBoard())
 
578
            m_UsePixelBufferObject = false;
 
579
        else
 
580
            m_UsePixelBufferObject = true;
 
581
    }
 
582
    else
 
583
    {
 
584
        m_UsePixelBufferObject = false;
 
585
    }
 
586
 
 
587
    m_RenderStates = new GLRenderStates(m_GraphicsBoardVendor);
 
588
 
 
589
    OPENGL_VERSION_1_1 = GLEW_VERSION_1_1 ? true : false;
 
590
    OPENGL_VERSION_1_2 = GLEW_VERSION_1_2 ? true : false;
 
591
    OPENGL_VERSION_1_3 = GLEW_VERSION_1_3 ? true : false;
 
592
    OPENGL_VERSION_1_4 = GLEW_VERSION_1_4 ? true : false;
 
593
    OPENGL_VERSION_1_5 = GLEW_VERSION_1_5 ? true : false;
 
594
    OPENGL_VERSION_2_0 = GLEW_VERSION_2_0 ? true : false;
 
595
    OPENGL_VERSION_2_1 = GLEW_VERSION_2_1 ? true : false;
 
596
    GLSL_VERSION_1_0 = 1;
 
597
    GLSL_VERSION_1_1 = 1;
 
598
 
 
599
    if(OPENGL_VERSION_2_1 == false)
 
600
    {
 
601
        //nuxDebugMsg(TEXT("[GLDeviceFactory::GLDeviceFactory] No support for OpenGL 2.1"));
 
602
//         inlWin32MessageBox(NULL, TEXT("Error"), MBTYPE_Ok, MBICON_Error, MBMODAL_ApplicationModal, 
 
603
//             TEXT("No Support for OpenGL 2.1.\nThe program will exit."));
 
604
//         exit(-1);
 
605
    }
 
606
 
 
607
    if(GLEW_EXT_framebuffer_object == false)
 
608
    {
 
609
        nuxDebugMsg(TEXT("[GLDeviceFactory::GLDeviceFactory] No support for Framebuffer Objects."));
 
610
//         inlWin32MessageBox(NULL, TEXT("Error"), MBTYPE_Ok, MBICON_Error, MBMODAL_ApplicationModal, 
 
611
//             TEXT("No support for Framebuffer Objects.\nThe program will exit."));
 
612
//         exit(-1);
 
613
    }
 
614
 
 
615
    if((GLEW_ARB_texture_rectangle == false) && (GLEW_EXT_texture_rectangle == false))
 
616
    {
 
617
        nuxDebugMsg(TEXT("[GLDeviceFactory::GLDeviceFactory] No support for rectangle textures."));
 
618
//         inlWin32MessageBox(NULL, TEXT("Error"), MBTYPE_Ok, MBICON_Error, MBMODAL_ApplicationModal, 
 
619
//             TEXT("No support for rectangle textures.\nThe program will exit."));
 
620
//         exit(-1);
 
621
    }
 
622
 
 
623
#if defined(INL_OS_WINDOWS)
 
624
    OGL_EXT_SWAP_CONTROL                = WGLEW_EXT_swap_control ? true : false;
 
625
#elif defined(INL_OS_LINUX)
 
626
    OGL_EXT_SWAP_CONTROL                = GLXEW_SGI_swap_control ? true : false;
 
627
#elif defined(INL_OS_LINUX)
 
628
    OGL_EXT_SWAP_CONTROL                = GLXEW_SGI_swap_control ? true : false;
 
629
#endif
 
630
 
 
631
    GL_ARB_TEXTURE_RECTANGLE            = GLEW_ARB_texture_rectangle ? true : false;
 
632
    GL_ARB_VERTEX_PROGRAM               = GLEW_ARB_vertex_program ? true : false;
 
633
    GL_ARB_FRAGMENT_PROGRAM             = GLEW_ARB_fragment_program ? true : false;
 
634
    GL_ARB_SHADER_OBJECTS               = GLEW_ARB_shader_objects ? true : false;
 
635
    GL_ARB_VERTEX_SHADER                = GLEW_ARB_vertex_shader ? true : false;
 
636
    GL_ARB_FRAGMENT_SHADER              = GLEW_ARB_fragment_shader ? true : false;
 
637
    GL_ARB_VERTEX_BUFFER_OBJECT         = GLEW_ARB_vertex_buffer_object ? true : false;
 
638
    GL_ARB_TEXTURE_NON_POWER_OF_TWO     = GLEW_ARB_texture_non_power_of_two ? true : false;
 
639
    GL_EXT_FRAMEBUFFER_OBJECT           = GLEW_EXT_framebuffer_object ? true : false;
 
640
    GL_EXT_DRAW_RANGE_ELEMENTS          = GLEW_EXT_draw_range_elements ? true : false;
 
641
    GL_EXT_STENCIL_TWO_SIDE             = GLEW_EXT_stencil_two_side ? true : false;
 
642
    GL_EXT_TEXTURE_RECTANGLE            = GLEW_EXT_texture_rectangle ? true : false;
 
643
    GL_NV_TEXTURE_RECTANGLE             = GLEW_NV_texture_rectangle ? true : false;
 
644
    
 
645
    InitTextureFormats();
 
646
 
 
647
    // See Avoiding 16 Common OpenGL Pitfalls
 
648
    // 7. Watch Your Pixel Store Alignment
 
649
    // http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/
 
650
    // We use a pack /unpack alignment to 1 so we don't have any padding at the end of row.
 
651
 
 
652
    glPixelStorei(GL_UNPACK_ALIGNMENT, _PixelStoreAlignment);
 
653
    glPixelStorei(GL_PACK_ALIGNMENT, _PixelStoreAlignment);
 
654
 
 
655
    // Create dummy texture and surface for the default render target.
 
656
    pDefaultRenderTargetTexture = new IOpenGLTexture2D(DeviceWidth, DeviceHeight, 1, DeviceFormat, TRUE);
 
657
    pDefaultRenderTargetSurface = pDefaultRenderTargetTexture->GetSurfaceLevel(0);
 
658
 
 
659
    _DeviceWidth = DeviceWidth;
 
660
    _DeviceHeight = DeviceHeight;
 
661
 
 
662
    _ViewportX = 0;
 
663
    _ViewportY = 0;
 
664
    _ViewportWidth = DeviceWidth;
 
665
    _ViewportHeight = DeviceHeight;
 
666
 
 
667
    for(int i = 0; i < MAX_NUM_STREAM; i++)
 
668
    {
 
669
        _StreamSource[i].ResetStreamSource();
 
670
    }
 
671
 
 
672
    // Configure NVidia CG
 
673
    #if (NUX_ENABLE_CG_SHADERS)
 
674
    {
 
675
        // Create Cg context and set profile.
 
676
        CHECKGL( cgSetErrorCallback( cgErrorCallback ) );
 
677
        m_Cgcontext = cgCreateContext();
 
678
        nuxAssert(m_Cgcontext);
 
679
        //CHECKGL( cgGLEnableProfile( CG_PROFILE_VP40 ) );
 
680
        //CHECKGL( cgGLEnableProfile( CG_PROFILE_FP40 ) );
 
681
        CHECKGL( cgGLSetManageTextureParameters( m_Cgcontext, CG_FALSE ) );
 
682
    }
 
683
    #endif
 
684
 
 
685
    if(GL_EXT_FRAMEBUFFER_OBJECT)
 
686
        _FrameBufferObject = CreateFrameBufferObject();
 
687
}
 
688
 
 
689
GLDeviceFactory::~GLDeviceFactory()
 
690
{
 
691
    pDefaultRenderTargetSurface = 0;
 
692
    pDefaultRenderTargetTexture = 0;
 
693
    
 
694
    INL_SAFE_DELETE(m_RenderStates);
 
695
    
 
696
    _FrameBufferObject = 0;
 
697
    _CurrentFrameBufferObject = 0;
 
698
    CollectDeviceResource();
 
699
 
 
700
    // NVidia CG
 
701
    #if (NUX_ENABLE_CG_SHADERS)
 
702
    cgDestroyContext(m_Cgcontext);
 
703
    inlSetThreadLocalStorage(ThreadLocal_GLDeviceFactory, 0);
 
704
    #endif
 
705
}
 
706
 
 
707
int GLDeviceFactory::CreateTexture(
 
708
    t_u32 Width
 
709
    , t_u32 Height
 
710
    , t_u32 Levels
 
711
    //, DWORD Usage    // no use
 
712
    , BitmapFormat PixelFormat
 
713
    //, D3DPOOL Pool       // no use
 
714
    , IOpenGLTexture2D** ppTexture
 
715
    //, HANDLE* pSharedHandle       // no use
 
716
    )
 
717
{
 
718
// From : http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_non_power_of_two.txt
 
719
//    The "floor" convention has a relatively straightforward way to
 
720
//        evaluate (with integer math) means to determine how many mipmap
 
721
//        levels are required for a complete pyramid:
 
722
//    numLevels = 1 + floor(log2(max(w, h, d)))
 
723
    t_u32 NumTotalMipLevel    = 1 + floorf(Log2(Max(Width, Height)));
 
724
 
 
725
//    Levels
 
726
//        [in] Number of levels in the texture. If this is zero, Direct3D will generate all texture sublevels 
 
727
//        down to 1 by 1 pixels for hardware that supports mipmapped textures. Call GetNumMipLevel to see the 
 
728
//        number of levels generated. 
 
729
    t_u32 NumMipLevel = 0;
 
730
    if(Levels == 0)
 
731
    {
 
732
        NumMipLevel = NumTotalMipLevel;
 
733
    }
 
734
    else if(Levels > NumTotalMipLevel)
 
735
    {
 
736
        NumMipLevel = NumTotalMipLevel;
 
737
    }
 
738
    else
 
739
    {
 
740
        NumMipLevel = Levels;
 
741
    }
 
742
 
 
743
 
 
744
//    The "floor" convention can be evaluated incrementally with the
 
745
//        following recursion:
 
746
//
 
747
//    nextLODdim = max(1, currentLODdim >> 1)
 
748
//
 
749
//        where currentLODdim is the dimension of a level N and nextLODdim
 
750
//        is the dimension of level N+1.  The recursion stops when level
 
751
//        numLevels-1 is reached.
 
752
 
 
753
    *ppTexture = new IOpenGLTexture2D(Width, Height, NumMipLevel, PixelFormat);
 
754
 
 
755
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLTexture2D> >(*ppTexture, &_CachedTextureList);
 
756
    return 1;
 
757
}
 
758
 
 
759
int GLDeviceFactory::CreateRectangleTexture(
 
760
                                   t_u32 Width
 
761
                                   , t_u32 Height
 
762
                                   , t_u32 Levels
 
763
                                   //, DWORD Usage    // no use
 
764
                                   , BitmapFormat PixelFormat
 
765
                                   //, D3DPOOL Pool       // no use
 
766
                                   , IOpenGLRectangleTexture** ppTexture
 
767
                                   //, HANDLE* pSharedHandle       // no use
 
768
                                   )
 
769
{
 
770
 
 
771
    // From : http://oss.sgi.com/projects/ogl-sample/registry/ARB/texture_non_power_of_two.txt
 
772
    //    The "floor" convention has a relatively straightforward way to
 
773
    //        evaluate (with integer math) means to determine how many mipmap
 
774
    //        levels are required for a complete pyramid:
 
775
    //    numLevels = 1 + floor(log2(max(w, h, d)))
 
776
    t_u32 NumTotalMipLevel    = 1 + floorf(Log2(Max(Width, Height)));
 
777
 
 
778
    //    Levels
 
779
    //        [in] Number of levels in the texture. If this is zero, Direct3D will generate all texture sublevels 
 
780
    //        down to 1 by 1 pixels for hardware that supports mipmapped textures. Call GetNumMipLevel to see the 
 
781
    //        number of levels generated. 
 
782
    t_u32 NumMipLevel = 0;
 
783
    if(Levels == 0)
 
784
    {
 
785
        NumMipLevel = NumTotalMipLevel;
 
786
    }
 
787
    else if(Levels > NumTotalMipLevel)
 
788
    {
 
789
        NumMipLevel = NumTotalMipLevel;
 
790
    }
 
791
    else
 
792
    {
 
793
        NumMipLevel = Levels;
 
794
    }
 
795
 
 
796
 
 
797
    //    The "floor" convention can be evaluated incrementally with the
 
798
    //        following recursion:
 
799
    //
 
800
    //    nextLODdim = max(1, currentLODdim >> 1)
 
801
    //
 
802
    //        where currentLODdim is the dimension of a level N and nextLODdim
 
803
    //        is the dimension of level N+1.  The recursion stops when level
 
804
    //        numLevels-1 is reached.
 
805
 
 
806
    *ppTexture = new IOpenGLRectangleTexture(Width, Height, NumMipLevel, PixelFormat);
 
807
 
 
808
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLRectangleTexture> >(*ppTexture, &_CachedTextureRectangleList);
 
809
    return 1;
 
810
}
 
811
 
 
812
 
 
813
int GLDeviceFactory::CreateCubeTexture(
 
814
    t_u32 EdgeLength
 
815
    , t_u32 Levels
 
816
    //, DWORD Usage    // no use
 
817
    , BitmapFormat PixelFormat
 
818
    //, D3DPOOL Pool    // no use
 
819
    , IOpenGLCubeTexture ** ppCubeTexture
 
820
    //, HANDLE* pSharedHandle    // no use
 
821
    )
 
822
{
 
823
    t_u32 NumTotalMipLevel    = 1 + floorf(Log2(EdgeLength));
 
824
    //    Levels
 
825
    //        [in] Number of levels in the texture. If this is zero, Direct3D will generate all texture sublevels 
 
826
    //        down to 1 by 1 pixels for hardware that supports mipmapped textures. Call GetNumMipLevel to see the 
 
827
    //        number of levels generated. 
 
828
    t_u32 NumMipLevel = 0;
 
829
    if(Levels == 0)
 
830
    {
 
831
        NumMipLevel = NumTotalMipLevel;
 
832
    }
 
833
    else if(Levels > NumTotalMipLevel)
 
834
    {
 
835
        NumMipLevel = NumTotalMipLevel;
 
836
    }
 
837
    else
 
838
    {
 
839
        NumMipLevel = Levels;
 
840
    }
 
841
 
 
842
    *ppCubeTexture = new IOpenGLCubeTexture(EdgeLength, NumMipLevel, PixelFormat);
 
843
 
 
844
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLCubeTexture> >(*ppCubeTexture, &_CachedCubeTextureList);
 
845
    return 1;
 
846
}
 
847
 
 
848
int GLDeviceFactory::CreateVolumeTexture(
 
849
    t_u32 Width
 
850
    , t_u32 Height
 
851
    , t_u32 Depth
 
852
    , t_u32 Levels
 
853
    //, DWORD Usage        // no use
 
854
    , BitmapFormat PixelFormat
 
855
    //, D3DPOOL Pool       // no use
 
856
    , IOpenGLVolumeTexture** ppVolumeTexture
 
857
    //, HANDLE* pSharedHandle       // no use
 
858
    )
 
859
{
 
860
    t_u32 NumTotalMipLevel = 1 + floorf(Log2(Max(Max(Width, Height), Depth)));
 
861
    //    Levels
 
862
    //        [in] Number of levels in the texture. If this is zero, Direct3D will generate all texture sublevels 
 
863
    //        down to 1 by 1 pixels for hardware that supports mipmapped textures. Call GetNumMipLevel to see the 
 
864
    //        number of levels generated. 
 
865
    t_u32 NumMipLevel = 0;
 
866
    if(Levels == 0)
 
867
    {
 
868
        NumMipLevel = NumTotalMipLevel;
 
869
    }
 
870
    else if(Levels > NumTotalMipLevel)
 
871
    {
 
872
        NumMipLevel = NumTotalMipLevel;
 
873
    }
 
874
    else
 
875
    {
 
876
        NumMipLevel = Levels;
 
877
    }
 
878
 
 
879
    *ppVolumeTexture = new IOpenGLVolumeTexture(Width, Height, Depth, NumMipLevel, PixelFormat);
 
880
 
 
881
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLVolumeTexture> >(*ppVolumeTexture, &_CachedVolumeTextureList);
 
882
    return OGL_OK;
 
883
}
 
884
 
 
885
int GLDeviceFactory::CreateAnimatedTexture(t_u32 Width,
 
886
                                           t_u32 Height,
 
887
                                           t_u32 Depth,
 
888
                                           BitmapFormat PixelFormat,
 
889
                                           IOpenGLAnimatedTexture** ppAnimatedTexture)
 
890
{
 
891
    *ppAnimatedTexture = new IOpenGLAnimatedTexture(Width, Height, Depth, PixelFormat);
 
892
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLAnimatedTexture> >(*ppAnimatedTexture, &_CachedAnimatedTextureList);
 
893
    return OGL_OK;
 
894
}
 
895
 
 
896
int GLDeviceFactory::CreateQuery(QUERY_TYPE Type, IOpenGLQuery** ppQuery)
 
897
{
 
898
    *ppQuery = new IOpenGLQuery(Type);
 
899
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLQuery> >(*ppQuery, &_CachedQueryList);
 
900
 
 
901
    return OGL_OK;
 
902
}
 
903
 
 
904
int GLDeviceFactory::CreateFrameBufferObject(IOpenGLFrameBufferObject** ppFrameBufferObject)
 
905
{
 
906
    *ppFrameBufferObject = new IOpenGLFrameBufferObject();
 
907
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLFrameBufferObject> >(*ppFrameBufferObject, &_CachedFrameBufferList);
 
908
    return OGL_OK;
 
909
}
 
910
 
 
911
int GLDeviceFactory::CreateShaderProgram(IOpenGLShaderProgram** ppShaderProgram)
 
912
{
 
913
    *ppShaderProgram = new IOpenGLShaderProgram();
 
914
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLShaderProgram> >(*ppShaderProgram, &_CachedShaderProgramList);
 
915
    return OGL_OK;
 
916
}
 
917
 
 
918
int GLDeviceFactory::CreateVertexShader(IOpenGLVertexShader** ppVertexShader)
 
919
{
 
920
    *ppVertexShader = new IOpenGLVertexShader();
 
921
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLVertexShader> >(*ppVertexShader, &_CachedVertexShaderList);
 
922
    return OGL_OK;
 
923
}
 
924
 
 
925
int GLDeviceFactory::CreatePixelShader(IOpenGLPixelShader** ppPixelShader)
 
926
{
 
927
    *ppPixelShader = new IOpenGLPixelShader();
 
928
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLPixelShader> >(*ppPixelShader, &_CachedPixelShaderList);
 
929
    return OGL_OK;
 
930
}
 
931
 
 
932
int GLDeviceFactory::CreateAsmShaderProgram(IOpenGLAsmShaderProgram** ppAsmShaderProgram)
 
933
{
 
934
    *ppAsmShaderProgram = new IOpenGLAsmShaderProgram();
 
935
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLAsmShaderProgram> >(*ppAsmShaderProgram, &_CachedAsmShaderProgramList);
 
936
    return OGL_OK;
 
937
}
 
938
 
 
939
int GLDeviceFactory::CreateAsmVertexShader(IOpenGLAsmVertexShader** ppAsmVertexShader)
 
940
{
 
941
    *ppAsmVertexShader = new IOpenGLAsmVertexShader();
 
942
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLAsmVertexShader> >(*ppAsmVertexShader, &_CachedAsmVertexShaderList);
 
943
    return OGL_OK;
 
944
}
 
945
 
 
946
int GLDeviceFactory::CreateAsmPixelShader(IOpenGLAsmPixelShader** ppAsmPixelShader)
 
947
{
 
948
    *ppAsmPixelShader = new IOpenGLAsmPixelShader();
 
949
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLAsmPixelShader> >(*ppAsmPixelShader, &_CachedAsmPixelShaderList);
 
950
    return OGL_OK;
 
951
}
 
952
 
 
953
#if (NUX_ENABLE_CG_SHADERS)
 
954
int GLDeviceFactory::CreateCGVertexShader(ICgVertexShader** ppCgVertexShader)
 
955
{
 
956
    *ppCgVertexShader = new ICgVertexShader();
 
957
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<ICgVertexShader> >(*ppCgVertexShader, &_CachedCGVertexShaderList);
 
958
    return OGL_OK;
 
959
}
 
960
 
 
961
int GLDeviceFactory::CreateCGPixelShader(ICgPixelShader** ppCgPixelShader)
 
962
{
 
963
    *ppCgPixelShader = new ICgPixelShader();
 
964
    if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<ICgPixelShader> >(*ppCgPixelShader, &_CachedCGPixelShaderList);
 
965
    return OGL_OK;
 
966
}
 
967
#endif
 
968
 
 
969
void GLDeviceFactory::CollectDeviceResource()
 
970
{
 
971
    TDeviceResourceList< TRefGL<IOpenGLVertexBuffer> >* pTempVertexBuffer = _CachedVertexBufferList;
 
972
    while(pTempVertexBuffer)
 
973
    {
 
974
        if(pTempVertexBuffer->_DeviceResource->RefCount() == 1)
 
975
        {
 
976
            pTempVertexBuffer->_DeviceResource = 0;
 
977
            pTempVertexBuffer = pTempVertexBuffer->_next;
 
978
        }
 
979
        else
 
980
            pTempVertexBuffer = pTempVertexBuffer->_next;
 
981
    }
 
982
 
 
983
    TDeviceResourceList< TRefGL<IOpenGLIndexBuffer> >* pTempIndexBuffer = _CachedIndexBufferList;
 
984
    while(pTempIndexBuffer)
 
985
    {
 
986
        if(pTempIndexBuffer->_DeviceResource->RefCount() == 1)
 
987
        {
 
988
            pTempIndexBuffer->_DeviceResource = 0;
 
989
            pTempIndexBuffer = pTempIndexBuffer->_next;
 
990
        }
 
991
        else
 
992
            pTempIndexBuffer = pTempIndexBuffer->_next;
 
993
    }
 
994
 
 
995
    TDeviceResourceList< TRefGL<IOpenGLVertexDeclaration> >* pTempVertexDeclaration = _CachedVertexDeclarationList;
 
996
    while(pTempVertexDeclaration)
 
997
    {
 
998
        if(pTempVertexDeclaration->_DeviceResource->RefCount() == 1)
 
999
        {
 
1000
            pTempVertexDeclaration->_DeviceResource = 0;
 
1001
            pTempVertexDeclaration = pTempVertexDeclaration->_next;
 
1002
        }
 
1003
        else
 
1004
            pTempVertexDeclaration = pTempVertexDeclaration->_next;
 
1005
    }
 
1006
 
 
1007
    TDeviceResourceList< TRefGL<IOpenGLTexture2D> >* pTempTexture2D = _CachedTextureList;
 
1008
    while(pTempTexture2D)
 
1009
    {
 
1010
        if(pTempTexture2D->_DeviceResource->RefCount() == 1)
 
1011
        {
 
1012
            pTempTexture2D->_DeviceResource = 0;
 
1013
            pTempTexture2D = pTempTexture2D->_next;
 
1014
        }
 
1015
        else
 
1016
            pTempTexture2D = pTempTexture2D->_next;
 
1017
    }
 
1018
 
 
1019
    TDeviceResourceList< TRefGL<IOpenGLCubeTexture> >* pTempCubeTexture = _CachedCubeTextureList;
 
1020
    while(pTempCubeTexture)
 
1021
    {
 
1022
        if(pTempCubeTexture->_DeviceResource->RefCount() == 1)
 
1023
        {
 
1024
            pTempCubeTexture->_DeviceResource = 0;
 
1025
            pTempCubeTexture = pTempCubeTexture->_next;
 
1026
        }
 
1027
        else
 
1028
            pTempCubeTexture = pTempCubeTexture->_next;
 
1029
    }
 
1030
 
 
1031
    TDeviceResourceList< TRefGL<IOpenGLVolumeTexture> >* pTempVolumeTexture = _CachedVolumeTextureList;
 
1032
    while(pTempVolumeTexture)
 
1033
    {
 
1034
        if(pTempVolumeTexture->_DeviceResource->RefCount() == 1)
 
1035
        {
 
1036
            pTempVolumeTexture->_DeviceResource = 0;
 
1037
            pTempVolumeTexture = pTempVolumeTexture->_next;
 
1038
        }
 
1039
        else
 
1040
            pTempVolumeTexture = pTempVolumeTexture->_next;
 
1041
    }
 
1042
 
 
1043
    TDeviceResourceList< TRefGL<IOpenGLAnimatedTexture> >* pTempAnimatedTexture = _CachedAnimatedTextureList;
 
1044
    while(pTempAnimatedTexture)
 
1045
    {
 
1046
        if(pTempAnimatedTexture->_DeviceResource->RefCount() == 1)
 
1047
        {
 
1048
            pTempAnimatedTexture->_DeviceResource = 0;
 
1049
            pTempAnimatedTexture = pTempAnimatedTexture->_next;
 
1050
        }
 
1051
        else
 
1052
            pTempAnimatedTexture = pTempAnimatedTexture->_next;
 
1053
    }
 
1054
 
 
1055
    TDeviceResourceList< TRefGL<IOpenGLFrameBufferObject> >* pTempFrameBufferObject = _CachedFrameBufferList;
 
1056
    while(pTempFrameBufferObject)
 
1057
    {
 
1058
        if(pTempFrameBufferObject->_DeviceResource->RefCount() == 1)
 
1059
        {
 
1060
            pTempFrameBufferObject->_DeviceResource = 0;
 
1061
            pTempFrameBufferObject = pTempFrameBufferObject->_next;
 
1062
        }
 
1063
        else
 
1064
            pTempFrameBufferObject = pTempFrameBufferObject->_next;
 
1065
    }
 
1066
 
 
1067
    TDeviceResourceList< TRefGL<IOpenGLQuery> >* pTempQuery = _CachedQueryList;
 
1068
    while(pTempQuery)
 
1069
    {
 
1070
        if(pTempQuery->_DeviceResource->RefCount() == 1)
 
1071
        {
 
1072
            pTempQuery->_DeviceResource = 0;
 
1073
            pTempQuery = pTempQuery->_next;
 
1074
        }
 
1075
        else
 
1076
            pTempQuery = pTempQuery->_next;
 
1077
    }
 
1078
 
 
1079
    TDeviceResourceList< TRefGL<IOpenGLVertexShader> >* pTempVertexShader = _CachedVertexShaderList;
 
1080
    while(pTempVertexShader)
 
1081
    {
 
1082
        if(pTempVertexShader->_DeviceResource->RefCount() == 1)
 
1083
        {
 
1084
            pTempVertexShader->_DeviceResource = 0;
 
1085
            pTempVertexShader = pTempVertexShader->_next;
 
1086
        }
 
1087
        else
 
1088
            pTempVertexShader = pTempVertexShader->_next;
 
1089
    }
 
1090
 
 
1091
    TDeviceResourceList< TRefGL<IOpenGLPixelShader> >* pTempPixelShader = _CachedPixelShaderList;
 
1092
    while(pTempPixelShader)
 
1093
    {
 
1094
        if(pTempPixelShader->_DeviceResource->RefCount() == 1)
 
1095
        {
 
1096
            pTempPixelShader->_DeviceResource = 0;
 
1097
            pTempPixelShader = pTempPixelShader->_next;
 
1098
        }
 
1099
        else
 
1100
            pTempPixelShader = pTempPixelShader->_next;
 
1101
    }
 
1102
 
 
1103
    #if (NUX_ENABLE_CG_SHADERS)
 
1104
    TDeviceResourceList< TRefGL<ICgVertexShader> >* pTempCgVertexShader = _CachedCGVertexShaderList;
 
1105
    while(pTempCgVertexShader)
 
1106
    {
 
1107
        if(pTempCgVertexShader->_DeviceResource->RefCount() == 1)
 
1108
        {
 
1109
            pTempCgVertexShader->_DeviceResource = 0;
 
1110
            pTempCgVertexShader = pTempCgVertexShader->_next;
 
1111
        }
 
1112
        else
 
1113
            pTempCgVertexShader = pTempCgVertexShader->_next;
 
1114
    }
 
1115
 
 
1116
    TDeviceResourceList< TRefGL<ICgPixelShader> >* pTempCgPixelShader = _CachedCGPixelShaderList;
 
1117
    while(pTempCgPixelShader)
 
1118
    {
 
1119
        if(pTempCgPixelShader->_DeviceResource->RefCount() == 1)
 
1120
        {
 
1121
            pTempCgPixelShader->_DeviceResource = 0;
 
1122
            pTempCgPixelShader = pTempCgPixelShader->_next;
 
1123
        }
 
1124
        else
 
1125
            pTempCgPixelShader = pTempCgPixelShader->_next;
 
1126
    }
 
1127
    #endif
 
1128
}
 
1129
 
 
1130
void GLDeviceFactory::InvalidateTextureUnit(int TextureUnitIndex)
 
1131
{
 
1132
    CHECKGL(glActiveTextureARB(TextureUnitIndex) );
 
1133
    
 
1134
    CHECKGL(glBindTexture(GL_TEXTURE_1D, 0));
 
1135
    CHECKGL(glBindTexture(GL_TEXTURE_2D, 0));
 
1136
    CHECKGL(glBindTexture(GL_TEXTURE_CUBE_MAP, 0));
 
1137
    CHECKGL(glBindTexture(GL_TEXTURE_3D, 0));
 
1138
    CHECKGL(glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0));
 
1139
    
 
1140
    // From lowest priority to highest priority:
 
1141
    //      GL_TEXTURE_1D,
 
1142
    //      GL_TEXTURE_2D,
 
1143
    //      GL_TEXTURE_RECTANGLE_ARB,
 
1144
    //      GL_TEXTURE_3D,
 
1145
    //      GL_TEXTURE_CUBE_MAP.
 
1146
 
 
1147
    CHECKGL(glDisable(GL_TEXTURE_1D) );
 
1148
    CHECKGL(glDisable(GL_TEXTURE_2D) );
 
1149
    CHECKGL(glDisable(GL_TEXTURE_RECTANGLE_ARB) );
 
1150
    CHECKGL(glDisable(GL_TEXTURE_3D) );
 
1151
    CHECKGL(glDisable(GL_TEXTURE_CUBE_MAP) );
 
1152
}
 
1153
 
 
1154
int GLDeviceFactory::AllocateUnpackPixelBufferIndex(int* index)
 
1155
{
 
1156
    t_u32 num = (t_u32)_PixelBufferArray.size();
 
1157
    for(t_u32 i = 0; i< num; i++)
 
1158
    {
 
1159
        if(_PixelBufferArray[i].IsReserved == FALSE)
 
1160
        {
 
1161
            _PixelBufferArray[i].IsReserved = TRUE;
 
1162
            *index = i;
 
1163
            return OGL_OK;
 
1164
        }
 
1165
    }
 
1166
    // Not enough free pbo
 
1167
    PixelBufferObject pbo;
 
1168
    GLuint OpenGLID = 0;
 
1169
    pbo.PBO = CreatePixelBufferObject(4, (VBO_USAGE)GL_STATIC_DRAW);
 
1170
    pbo.IsReserved = TRUE;
 
1171
    _PixelBufferArray.push_back(pbo);
 
1172
    *index = (int)_PixelBufferArray.size() - 1;
 
1173
    return OGL_OK;
 
1174
}
 
1175
 
 
1176
int GLDeviceFactory::FreeUnpackPixelBufferIndex(const int index)
 
1177
{
 
1178
    t_u32 num = (t_u32)_PixelBufferArray.size();
 
1179
    nuxAssertMsg((index >= 0) && (index < num), TEXT("[GLDeviceFactory::FreeUnpackPixelBufferIndex] Trying to Free a pixel buffer index that does not exist."));
 
1180
    if((index < 0) || (index >= num))
 
1181
    {
 
1182
        return OGL_ERROR;
 
1183
    }
 
1184
    _PixelBufferArray[index].IsReserved = false;
 
1185
 
 
1186
// //     if(0)
 
1187
// //     {
 
1188
// //         // Can we realloc the memory used by the buffer with much less memory (4x4bytes)???
 
1189
// //         CHECKGL( glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, OpenGLID) );
 
1190
// //         CHECKGL( glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 4*4, NULL, GL_STREAM_DRAW_ARB) );
 
1191
// //     }
 
1192
    return OGL_OK;
 
1193
}
 
1194
 
 
1195
void* GLDeviceFactory::LockUnpackPixelBufferIndex(const int index, int Size)
 
1196
{
 
1197
    GetThreadGLDeviceFactory()->BindUnpackPixelBufferIndex(index);
 
1198
    CHECKGL( glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, Size, NULL, GL_STREAM_DRAW) );
 
1199
    void* pBits = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB);
 
1200
    CHECKGL_MSG(glMapBufferARB );
 
1201
    CHECKGL( glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0) );
 
1202
    return pBits;
 
1203
}
 
1204
 
 
1205
void* GLDeviceFactory::LockPackPixelBufferIndex(const int index, int Size)
 
1206
{
 
1207
    GetThreadGLDeviceFactory()->BindPackPixelBufferIndex(index);
 
1208
    CHECKGL( glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, Size, NULL, GL_STREAM_DRAW) );
 
1209
    void* pBits = glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_WRITE_ONLY_ARB);
 
1210
    CHECKGL_MSG(glMapBufferARB );
 
1211
    CHECKGL( glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0) );
 
1212
    return pBits;
 
1213
}
 
1214
 
 
1215
void GLDeviceFactory::UnlockUnpackPixelBufferIndex(const int index)
 
1216
{
 
1217
    GetThreadGLDeviceFactory()->BindUnpackPixelBufferIndex(index);
 
1218
    CHECKGL( glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB) );
 
1219
    CHECKGL( glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0) );
 
1220
}
 
1221
 
 
1222
void GLDeviceFactory::UnlockPackPixelBufferIndex(const int index)
 
1223
{
 
1224
    GetThreadGLDeviceFactory()->BindPackPixelBufferIndex(index);
 
1225
    CHECKGL( glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB) );
 
1226
    CHECKGL( glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0) );
 
1227
}
 
1228
 
 
1229
int GLDeviceFactory::BindUnpackPixelBufferIndex(const int index)
 
1230
{
 
1231
    t_u32 num = (t_u32)_PixelBufferArray.size();
 
1232
    nuxAssertMsg((index >= 0) && (index < num), TEXT("[GLDeviceFactory::BindUnpackPixelBufferIndex] Trying to bind an invalid pixel buffer index."));
 
1233
    if((index < 0) || (index >= num))
 
1234
    {
 
1235
        return OGL_ERROR;
 
1236
    }
 
1237
 
 
1238
    nuxAssertMsg(_PixelBufferArray[index].IsReserved == true, TEXT("[GLDeviceFactory::BindUnpackPixelBufferIndex] Trying to reserved pixel buffer index."));
 
1239
    if(_PixelBufferArray[index].IsReserved == false)
 
1240
    {
 
1241
        return OGL_ERROR;
 
1242
    }
 
1243
    _PixelBufferArray[index].PBO->BindUnpackPixelBufferObject();
 
1244
    return OGL_OK;
 
1245
}
 
1246
 
 
1247
int GLDeviceFactory::BindPackPixelBufferIndex(const int index)
 
1248
{
 
1249
    t_u32 num = (t_u32)_PixelBufferArray.size();
 
1250
    nuxAssertMsg((index >= 0) && (index < num), TEXT("[GLDeviceFactory::BindPackPixelBufferIndex] Trying to bind an invalid pixel buffer index."));
 
1251
    if((index < 0) || (index >= num))
 
1252
    {
 
1253
        return OGL_ERROR;
 
1254
    }
 
1255
 
 
1256
    nuxAssertMsg(_PixelBufferArray[index].IsReserved == true, TEXT("[GLDeviceFactory::BindPackPixelBufferIndex] Trying to reserved pixel buffer index."));
 
1257
    if(_PixelBufferArray[index].IsReserved == false)
 
1258
    {
 
1259
        return OGL_ERROR;
 
1260
    }
 
1261
    _PixelBufferArray[index].PBO->BindPackPixelBufferObject();
 
1262
    return OGL_OK;
 
1263
}
 
1264
 
 
1265
int GLDeviceFactory::FormatFrameBufferObject(t_u32 Width, t_u32 Height, BitmapFormat PixelFormat)
 
1266
{
 
1267
    if(!GL_EXT_FRAMEBUFFER_OBJECT)
 
1268
    {nuxDebugMsg(TEXT("[GLDeviceFactory::FormatFrameBufferObject] No support for OpenGL framebuffer extension.")); return 0;}
 
1269
    return _FrameBufferObject->FormatFrameBufferObject(Width, Height, PixelFormat);
 
1270
}
 
1271
 
 
1272
int GLDeviceFactory::SetColorRenderTargetSurface(t_u32 ColorAttachmentIndex, TRefGL<IOpenGLSurface> pRenderTargetSurface)
 
1273
{
 
1274
    if(!GL_EXT_FRAMEBUFFER_OBJECT)
 
1275
    {nuxDebugMsg(TEXT("[GLDeviceFactory::SetColorRenderTargetSurface] No support for OpenGL framebuffer extension.")); return 0;}
 
1276
    return _FrameBufferObject->SetRenderTarget(ColorAttachmentIndex, pRenderTargetSurface);
 
1277
}
 
1278
 
 
1279
int GLDeviceFactory::SetDepthRenderTargetSurface(TRefGL<IOpenGLSurface> pDepthSurface)
 
1280
{
 
1281
    if(!GL_EXT_FRAMEBUFFER_OBJECT)
 
1282
    {nuxDebugMsg(TEXT("[GLDeviceFactory::SetDepthRenderTargetSurface] No support for OpenGL framebuffer extension.")); return 0;}
 
1283
    return _FrameBufferObject->SetDepthSurface(pDepthSurface);
 
1284
}
 
1285
 
 
1286
TRefGL<IOpenGLSurface> GLDeviceFactory::GetColorRenderTargetSurface(t_u32 ColorAttachmentIndex)
 
1287
{
 
1288
    if(!GL_EXT_FRAMEBUFFER_OBJECT)
 
1289
    {nuxDebugMsg(TEXT("[GLDeviceFactory::GetColorRenderTargetSurface] No support for OpenGL framebuffer extension.")); return 0;}
 
1290
    return _FrameBufferObject->GetRenderTarget(ColorAttachmentIndex);
 
1291
}
 
1292
 
 
1293
TRefGL<IOpenGLSurface> GLDeviceFactory::GetDepthRenderTargetSurface()
 
1294
{
 
1295
    if(!GL_EXT_FRAMEBUFFER_OBJECT)
 
1296
    {nuxDebugMsg(TEXT("[GLDeviceFactory::GetDepthRenderTargetSurface] No support for OpenGL framebuffer extension.")); return 0;}
 
1297
    return _FrameBufferObject->GetDepthRenderTarget();
 
1298
}
 
1299
 
 
1300
void GLDeviceFactory::ActivateFrameBuffer()
 
1301
{
 
1302
    if(!GL_EXT_FRAMEBUFFER_OBJECT)
 
1303
    {nuxDebugMsg(TEXT("[GLDeviceFactory::ActivateFrameBuffer] No support for OpenGL framebuffer extension.")); return;}
 
1304
    _FrameBufferObject->Activate();
 
1305
}
 
1306
 
 
1307
void GLDeviceFactory::DeactivateFrameBuffer()
 
1308
{
 
1309
    if(!GL_EXT_FRAMEBUFFER_OBJECT)
 
1310
    {nuxDebugMsg(TEXT("[GLDeviceFactory::DeactivateFrameBuffer] No support for OpenGL framebuffer extension.")); return;}
 
1311
    CHECKGL( glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, 0 ) );
 
1312
    CHECKGL( glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0) );
 
1313
}
 
1314
 
 
1315
void GLDeviceFactory::Clear(FLOAT red, FLOAT green, FLOAT blue, FLOAT alpha, FLOAT depth, int stencil)
 
1316
{
 
1317
    CHECKGL( glClearColor(red, green, blue, alpha) );
 
1318
    CHECKGL( glClearDepth(depth) );
 
1319
    CHECKGL( glClearStencil(stencil) );
 
1320
    CHECKGL( glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT) );
 
1321
}
 
1322
 
 
1323
void GLDeviceFactory::ClearColorRT(FLOAT red, FLOAT green, FLOAT blue, FLOAT alpha)
 
1324
{
 
1325
    CHECKGL( glClearColor(red, green, blue, alpha) );
 
1326
    CHECKGL( glClear(GL_COLOR_BUFFER_BIT) );
 
1327
}
 
1328
 
 
1329
void GLDeviceFactory::ClearDepthRT(FLOAT depth)
 
1330
{
 
1331
    CHECKGL( glClearDepth(depth) );
 
1332
    CHECKGL( glClear(GL_DEPTH_BUFFER_BIT) );
 
1333
}
 
1334
void GLDeviceFactory::ClearStencilRT(int stencil)
 
1335
{
 
1336
    CHECKGL( glClearStencil(stencil) );
 
1337
    CHECKGL( glClear(GL_STENCIL_BUFFER_BIT) );
 
1338
}
 
1339
 
 
1340
void GLDeviceFactory::ClearFloatingPointColorRT(int x, int y, int width, int height,
 
1341
                                                FLOAT red, FLOAT green, FLOAT blue, FLOAT alpha) // use a Quad.
 
1342
{
 
1343
    DrawQuad_FixPipe(x, y, width, height, red, green, blue, alpha);
 
1344
}
 
1345
 
 
1346
void GLDeviceFactory::ClearSurfaceWithColor(TRefGL<IOpenGLSurface> s_, const SURFACE_RECT *rect_, float r, float g, float b, float a)
 
1347
{
 
1348
    if(!GL_EXT_FRAMEBUFFER_OBJECT)
 
1349
    {nuxDebugMsg(TEXT("[GLDeviceFactory::ClearSurfaceWithColor] No support for OpenGL framebuffer extension."));}
 
1350
 
 
1351
    FormatFrameBufferObject(s_->GetWidth(), s_->GetHeight(), s_->GetPixelFormat());
 
1352
    SetColorRenderTargetSurface(0, s_);
 
1353
    SetDepthRenderTargetSurface(0);
 
1354
    ActivateFrameBuffer();
 
1355
    ClearFloatingPointColorRT(rect_->left,
 
1356
        rect_->top,
 
1357
        rect_->right - rect_->left,
 
1358
        rect_->bottom - rect_->top,
 
1359
        r, g, b, a);
 
1360
}
 
1361
 
 
1362
void GLDeviceFactory::SetCurrentFrameBufferObject(TRefGL<IOpenGLFrameBufferObject> fbo)
 
1363
{
 
1364
    _CurrentFrameBufferObject = fbo;
 
1365
}
 
1366
 
 
1367
TRefGL<IOpenGLFrameBufferObject> GLDeviceFactory::GetCurrentFrameBufferObject()
 
1368
{
 
1369
    return _CurrentFrameBufferObject;
 
1370
}
 
1371
 
 
1372
NAMESPACE_END_OGL