2
* Copyright 2010 Inalogic Inc.
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.
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.
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/>
18
* Authored by: Jay Taoko <jay.taoko_AT_gmail_DOT_com>
23
#include "GLResource.h"
24
#include "GLDeviceFactory.h"
25
#include "GLDeviceObjects.h"
26
#include "GLResourceManager.h"
28
#include "GLTextureResourceManager.h"
29
#include "GLVertexResourceManager.h"
30
#include "GLDeviceFrameBufferObject.h"
31
#include "GLTemplatePrimitiveBuffer.h"
32
#include "OpenGLEngine.h"
36
#define MANAGEDEVICERESOURCE 0
38
extern void cgErrorCallback(void);
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.
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
48
#define INL_MISSING_GL_EXTENSION_MESSAGE_BOX(message) {MessageBox(NULL, TEXT("Missing extension: " #message), TEXT("ERROR"), MB_OK|MB_ICONERROR); exit(-1);}
50
// float Log2(float f)
52
// return logf(f) / logf(2);
55
extern PixelFormatInfo GPixelFormats[];
59
const char* extension0;
60
const char* extension1;
61
const char* extension2;
67
struct ReqExtension ReqNVidiaExtension[] =
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},
82
struct ReqExtension ReqNVidiaWGLExtension[] =
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},
90
static void InitializeExtension()
93
int NumReqExtension = sizeof(ReqNVidiaExtension) / sizeof(ReqNVidiaExtension[0]);
95
for(int index = 0; index < NumReqExtension; index++)
97
ReqNVidiaExtension[index].supported0 = 0;
98
ReqNVidiaExtension[index].supported1 = 0;
99
ReqNVidiaExtension[index].supported2 = 0;
101
if(ReqNVidiaExtension[index].extension0)
103
ReqNVidiaExtension[index].supported0 = glewIsSupported(ReqNVidiaExtension[index].extension0);
105
if(ReqNVidiaExtension[index].extension1)
107
ReqNVidiaExtension[index].supported1 = glewIsSupported(ReqNVidiaExtension[index].extension1);
109
if(ReqNVidiaExtension[index].extension2)
111
ReqNVidiaExtension[index].supported2 = glewIsSupported(ReqNVidiaExtension[index].extension2);
114
if((!ReqNVidiaExtension[index].supported0) &&
115
(!ReqNVidiaExtension[index].supported1) &&
116
(!ReqNVidiaExtension[index].supported2))
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);
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);
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"
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());
159
// if (!wglewIsSupported(
160
// "WGL_ARB_pbuffer "
161
// "WGL_ARB_pixel_format "
162
// "WGL_ARB_render_texture "
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());
174
static void InitTextureFormats()
176
GPixelFormats[ BITFMT_UNKNOWN ].PlatformFormat = GL_NONE; // Not supported for rendering.
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;
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;
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;
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;
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;
203
GPixelFormats[ BITFMT_B8G8R8 ].PlatformFormat = GL_RGB8;
204
GPixelFormats[ BITFMT_B8G8R8 ].Format = GL_BGR;
205
GPixelFormats[ BITFMT_B8G8R8 ].type = GL_UNSIGNED_BYTE;
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;
211
GPixelFormats[ BITFMT_RGBA16F ].PlatformFormat = GL_RGBA16F_ARB;
212
GPixelFormats[ BITFMT_RGBA16F ].Format = GL_RGBA;
213
GPixelFormats[ BITFMT_RGBA16F ].type = GL_HALF_FLOAT_ARB;
215
GPixelFormats[ BITFMT_RGB32F ].PlatformFormat = GL_RGB;
216
GPixelFormats[ BITFMT_RGB32F ].Format = GL_RGB;
217
GPixelFormats[ BITFMT_RGB32F ].type = GL_FLOAT;
219
GPixelFormats[ BITFMT_RGBA32F ].PlatformFormat = GL_RGBA32F_ARB;
220
GPixelFormats[ BITFMT_RGBA32F ].Format = GL_RGBA;
221
GPixelFormats[ BITFMT_RGBA32F ].type = GL_FLOAT;
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;
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;
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;
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;
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;
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;
250
GPixelFormats[ BITFMT_A8 ].PlatformFormat = GL_RGBA8;
251
GPixelFormats[ BITFMT_A8 ].Format = GL_LUMINANCE;
252
GPixelFormats[ BITFMT_A8 ].type = GL_UNSIGNED_BYTE;
255
TRefGL<IOpenGLTexture2D> GLDeviceFactory::pDefaultRenderTargetTexture = 0;
256
TRefGL<IOpenGLSurface> GLDeviceFactory::pDefaultRenderTargetSurface = 0;
259
TRefGL<IOpenGLTexture2D> GLDeviceFactory::CreateTexture(
263
, BitmapFormat PixelFormat)
265
IOpenGLTexture2D* ptr;
266
CreateTexture(Width, Height, Levels, PixelFormat, (IOpenGLTexture2D**)&ptr);
267
TRefGL<IOpenGLTexture2D> h = ptr;
271
TRefGL<IOpenGLRectangleTexture> GLDeviceFactory::CreateRectangleTexture(
275
, BitmapFormat PixelFormat)
277
IOpenGLRectangleTexture* ptr;
278
CreateRectangleTexture(Width, Height, Levels, PixelFormat, (IOpenGLRectangleTexture**)&ptr);
279
TRefGL<IOpenGLRectangleTexture> h = ptr;
283
TRefGL<IOpenGLCubeTexture> GLDeviceFactory::CreateCubeTexture(
286
, BitmapFormat PixelFormat)
288
IOpenGLCubeTexture* ptr;
289
CreateCubeTexture(EdgeLength, Levels, PixelFormat, (IOpenGLCubeTexture**)&ptr);
290
TRefGL<IOpenGLCubeTexture> h = ptr;
294
TRefGL<IOpenGLVolumeTexture> GLDeviceFactory::CreateVolumeTexture(
299
, BitmapFormat PixelFormat)
301
IOpenGLVolumeTexture* ptr;
302
CreateVolumeTexture(Width, Height, Depth, Levels, PixelFormat, (IOpenGLVolumeTexture**)&ptr);
303
TRefGL<IOpenGLVolumeTexture> h = ptr;
307
TRefGL<IOpenGLAnimatedTexture> GLDeviceFactory::CreateAnimatedTexture(
311
, BitmapFormat PixelFormat)
313
IOpenGLAnimatedTexture* prt;
314
CreateAnimatedTexture(Width, Height, Depth, PixelFormat, (IOpenGLAnimatedTexture**)&prt);
315
TRefGL<IOpenGLAnimatedTexture> h = prt;
320
TRefGL<IOpenGLQuery> GLDeviceFactory::CreateQuery(QUERY_TYPE Type)
323
CreateQuery(Type, (IOpenGLQuery**)&prt);
324
TRefGL<IOpenGLQuery> h = prt;
328
TRefGL<IOpenGLFrameBufferObject> GLDeviceFactory::CreateFrameBufferObject()
330
IOpenGLFrameBufferObject* ptr;
331
CreateFrameBufferObject((IOpenGLFrameBufferObject**)&ptr);
332
TRefGL<IOpenGLFrameBufferObject> h = ptr;
336
TRefGL<IOpenGLShaderProgram> GLDeviceFactory::CreateShaderProgram()
338
IOpenGLShaderProgram* ptr;
339
CreateShaderProgram((IOpenGLShaderProgram**)&ptr);
340
TRefGL<IOpenGLShaderProgram> h = ptr;
344
TRefGL<IOpenGLVertexShader> GLDeviceFactory::CreateVertexShader()
346
IOpenGLVertexShader* ptr;
347
CreateVertexShader((IOpenGLVertexShader**)&ptr);
348
TRefGL<IOpenGLVertexShader> h = ptr;
352
TRefGL<IOpenGLPixelShader> GLDeviceFactory::CreatePixelShader()
354
IOpenGLPixelShader* ptr;
355
CreatePixelShader((IOpenGLPixelShader**)&ptr);
356
TRefGL<IOpenGLPixelShader> h = ptr;
360
TRefGL<IOpenGLAsmShaderProgram> GLDeviceFactory::CreateAsmShaderProgram()
362
IOpenGLAsmShaderProgram* ptr;
363
CreateAsmShaderProgram((IOpenGLAsmShaderProgram**)&ptr);
364
TRefGL<IOpenGLAsmShaderProgram> h = ptr;
368
TRefGL<IOpenGLAsmVertexShader> GLDeviceFactory::CreateAsmVertexShader()
370
IOpenGLAsmVertexShader* ptr;
371
CreateAsmVertexShader((IOpenGLAsmVertexShader**)&ptr);
372
TRefGL<IOpenGLAsmVertexShader> h = ptr;
376
TRefGL<IOpenGLAsmPixelShader> GLDeviceFactory::CreateAsmPixelShader()
378
IOpenGLAsmPixelShader* ptr;
379
CreateAsmPixelShader((IOpenGLAsmPixelShader**)&ptr);
380
TRefGL<IOpenGLAsmPixelShader> h = ptr;
384
#if (NUX_ENABLE_CG_SHADERS)
385
TRefGL<ICgVertexShader> GLDeviceFactory::CreateCGVertexShader()
387
ICgVertexShader* ptr;
388
CreateCGVertexShader((ICgVertexShader**)&ptr);
389
TRefGL<ICgVertexShader> h = ptr;
393
TRefGL<ICgPixelShader> GLDeviceFactory::CreateCGPixelShader()
396
CreateCGPixelShader((ICgPixelShader**)&ptr);
397
TRefGL<ICgPixelShader> h = ptr;
402
void GLDeviceFactory::Initialize()
404
GLenum Glew_Ok = glewInit();
405
nuxAssert(Glew_Ok == GLEW_OK);
406
InitializeExtension();
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;
414
//void TestARBShaders()
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;
443
// // BEWARE glGetProgramiv != glGetProgramivARB (ARB fragment vertex program)
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) );
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)
502
inlSetThreadLocalStorage(ThreadLocal_GLDeviceFactory, this);
506
Glew_Ok = glewContextInit(glewGetContext());
507
nuxAssertMsg(Glew_Ok == GLEW_OK, TEXT("[GLDeviceFactory::GLDeviceFactory] GL Extensions failed to initialize."));
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());
516
nuxAssertMsg(Glew_Ok == GLEW_OK, TEXT("[GLDeviceFactory::GLDeviceFactory] WGL Extensions failed to initialize."));
518
Glew_Ok = glewInit();
521
InitializeExtension();
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));
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());
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);
545
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &OPENGL_MAX_VERTEX_ATTRIBUTES);
548
// int OPENGL_MAX_VERTEX_ATTRIBS_ARB;
549
// int OPENGL_MAX_PROGRAM_MATRIX_STACK_DEPTH;
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));
556
OPENGL_MAX_FB_ATTACHMENT = 4;
558
NString TempStr = (const TCHAR*)TCharToUpperCase(m_BoardVendorString.GetTCharPtr());
559
if(TempStr.FindFirstOccurence(TEXT("NVIDIA")) != -1)
561
m_isNVIDIABoard = true;
562
m_GraphicsBoardVendor = BOARD_NVIDIA;
564
else if(TempStr.FindFirstOccurence(TEXT("ATI")) != -1)
567
m_GraphicsBoardVendor = BOARD_ATI;
569
else if(TempStr.FindFirstOccurence(TEXT("TUNGSTEN")) != -1)
571
m_isINTELBoard = true;
572
m_GraphicsBoardVendor = BOARD_INTEL;
578
m_UsePixelBufferObject = false;
580
m_UsePixelBufferObject = true;
584
m_UsePixelBufferObject = false;
587
m_RenderStates = new GLRenderStates(m_GraphicsBoardVendor);
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;
599
if(OPENGL_VERSION_2_1 == false)
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."));
607
if(GLEW_EXT_framebuffer_object == false)
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."));
615
if((GLEW_ARB_texture_rectangle == false) && (GLEW_EXT_texture_rectangle == false))
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."));
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;
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;
645
InitTextureFormats();
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.
652
glPixelStorei(GL_UNPACK_ALIGNMENT, _PixelStoreAlignment);
653
glPixelStorei(GL_PACK_ALIGNMENT, _PixelStoreAlignment);
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);
659
_DeviceWidth = DeviceWidth;
660
_DeviceHeight = DeviceHeight;
664
_ViewportWidth = DeviceWidth;
665
_ViewportHeight = DeviceHeight;
667
for(int i = 0; i < MAX_NUM_STREAM; i++)
669
_StreamSource[i].ResetStreamSource();
672
// Configure NVidia CG
673
#if (NUX_ENABLE_CG_SHADERS)
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 ) );
685
if(GL_EXT_FRAMEBUFFER_OBJECT)
686
_FrameBufferObject = CreateFrameBufferObject();
689
GLDeviceFactory::~GLDeviceFactory()
691
pDefaultRenderTargetSurface = 0;
692
pDefaultRenderTargetTexture = 0;
694
INL_SAFE_DELETE(m_RenderStates);
696
_FrameBufferObject = 0;
697
_CurrentFrameBufferObject = 0;
698
CollectDeviceResource();
701
#if (NUX_ENABLE_CG_SHADERS)
702
cgDestroyContext(m_Cgcontext);
703
inlSetThreadLocalStorage(ThreadLocal_GLDeviceFactory, 0);
707
int GLDeviceFactory::CreateTexture(
711
//, DWORD Usage // no use
712
, BitmapFormat PixelFormat
713
//, D3DPOOL Pool // no use
714
, IOpenGLTexture2D** ppTexture
715
//, HANDLE* pSharedHandle // no use
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)));
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;
732
NumMipLevel = NumTotalMipLevel;
734
else if(Levels > NumTotalMipLevel)
736
NumMipLevel = NumTotalMipLevel;
740
NumMipLevel = Levels;
744
// The "floor" convention can be evaluated incrementally with the
745
// following recursion:
747
// nextLODdim = max(1, currentLODdim >> 1)
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.
753
*ppTexture = new IOpenGLTexture2D(Width, Height, NumMipLevel, PixelFormat);
755
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLTexture2D> >(*ppTexture, &_CachedTextureList);
759
int GLDeviceFactory::CreateRectangleTexture(
763
//, DWORD Usage // no use
764
, BitmapFormat PixelFormat
765
//, D3DPOOL Pool // no use
766
, IOpenGLRectangleTexture** ppTexture
767
//, HANDLE* pSharedHandle // no use
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)));
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;
785
NumMipLevel = NumTotalMipLevel;
787
else if(Levels > NumTotalMipLevel)
789
NumMipLevel = NumTotalMipLevel;
793
NumMipLevel = Levels;
797
// The "floor" convention can be evaluated incrementally with the
798
// following recursion:
800
// nextLODdim = max(1, currentLODdim >> 1)
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.
806
*ppTexture = new IOpenGLRectangleTexture(Width, Height, NumMipLevel, PixelFormat);
808
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLRectangleTexture> >(*ppTexture, &_CachedTextureRectangleList);
813
int GLDeviceFactory::CreateCubeTexture(
816
//, DWORD Usage // no use
817
, BitmapFormat PixelFormat
818
//, D3DPOOL Pool // no use
819
, IOpenGLCubeTexture ** ppCubeTexture
820
//, HANDLE* pSharedHandle // no use
823
t_u32 NumTotalMipLevel = 1 + floorf(Log2(EdgeLength));
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;
831
NumMipLevel = NumTotalMipLevel;
833
else if(Levels > NumTotalMipLevel)
835
NumMipLevel = NumTotalMipLevel;
839
NumMipLevel = Levels;
842
*ppCubeTexture = new IOpenGLCubeTexture(EdgeLength, NumMipLevel, PixelFormat);
844
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLCubeTexture> >(*ppCubeTexture, &_CachedCubeTextureList);
848
int GLDeviceFactory::CreateVolumeTexture(
853
//, DWORD Usage // no use
854
, BitmapFormat PixelFormat
855
//, D3DPOOL Pool // no use
856
, IOpenGLVolumeTexture** ppVolumeTexture
857
//, HANDLE* pSharedHandle // no use
860
t_u32 NumTotalMipLevel = 1 + floorf(Log2(Max(Max(Width, Height), Depth)));
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;
868
NumMipLevel = NumTotalMipLevel;
870
else if(Levels > NumTotalMipLevel)
872
NumMipLevel = NumTotalMipLevel;
876
NumMipLevel = Levels;
879
*ppVolumeTexture = new IOpenGLVolumeTexture(Width, Height, Depth, NumMipLevel, PixelFormat);
881
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLVolumeTexture> >(*ppVolumeTexture, &_CachedVolumeTextureList);
885
int GLDeviceFactory::CreateAnimatedTexture(t_u32 Width,
888
BitmapFormat PixelFormat,
889
IOpenGLAnimatedTexture** ppAnimatedTexture)
891
*ppAnimatedTexture = new IOpenGLAnimatedTexture(Width, Height, Depth, PixelFormat);
892
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLAnimatedTexture> >(*ppAnimatedTexture, &_CachedAnimatedTextureList);
896
int GLDeviceFactory::CreateQuery(QUERY_TYPE Type, IOpenGLQuery** ppQuery)
898
*ppQuery = new IOpenGLQuery(Type);
899
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLQuery> >(*ppQuery, &_CachedQueryList);
904
int GLDeviceFactory::CreateFrameBufferObject(IOpenGLFrameBufferObject** ppFrameBufferObject)
906
*ppFrameBufferObject = new IOpenGLFrameBufferObject();
907
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLFrameBufferObject> >(*ppFrameBufferObject, &_CachedFrameBufferList);
911
int GLDeviceFactory::CreateShaderProgram(IOpenGLShaderProgram** ppShaderProgram)
913
*ppShaderProgram = new IOpenGLShaderProgram();
914
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLShaderProgram> >(*ppShaderProgram, &_CachedShaderProgramList);
918
int GLDeviceFactory::CreateVertexShader(IOpenGLVertexShader** ppVertexShader)
920
*ppVertexShader = new IOpenGLVertexShader();
921
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLVertexShader> >(*ppVertexShader, &_CachedVertexShaderList);
925
int GLDeviceFactory::CreatePixelShader(IOpenGLPixelShader** ppPixelShader)
927
*ppPixelShader = new IOpenGLPixelShader();
928
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLPixelShader> >(*ppPixelShader, &_CachedPixelShaderList);
932
int GLDeviceFactory::CreateAsmShaderProgram(IOpenGLAsmShaderProgram** ppAsmShaderProgram)
934
*ppAsmShaderProgram = new IOpenGLAsmShaderProgram();
935
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLAsmShaderProgram> >(*ppAsmShaderProgram, &_CachedAsmShaderProgramList);
939
int GLDeviceFactory::CreateAsmVertexShader(IOpenGLAsmVertexShader** ppAsmVertexShader)
941
*ppAsmVertexShader = new IOpenGLAsmVertexShader();
942
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLAsmVertexShader> >(*ppAsmVertexShader, &_CachedAsmVertexShaderList);
946
int GLDeviceFactory::CreateAsmPixelShader(IOpenGLAsmPixelShader** ppAsmPixelShader)
948
*ppAsmPixelShader = new IOpenGLAsmPixelShader();
949
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<IOpenGLAsmPixelShader> >(*ppAsmPixelShader, &_CachedAsmPixelShaderList);
953
#if (NUX_ENABLE_CG_SHADERS)
954
int GLDeviceFactory::CreateCGVertexShader(ICgVertexShader** ppCgVertexShader)
956
*ppCgVertexShader = new ICgVertexShader();
957
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<ICgVertexShader> >(*ppCgVertexShader, &_CachedCGVertexShaderList);
961
int GLDeviceFactory::CreateCGPixelShader(ICgPixelShader** ppCgPixelShader)
963
*ppCgPixelShader = new ICgPixelShader();
964
if(MANAGEDEVICERESOURCE) ManageDeviceResource< TRefGL<ICgPixelShader> >(*ppCgPixelShader, &_CachedCGPixelShaderList);
969
void GLDeviceFactory::CollectDeviceResource()
971
TDeviceResourceList< TRefGL<IOpenGLVertexBuffer> >* pTempVertexBuffer = _CachedVertexBufferList;
972
while(pTempVertexBuffer)
974
if(pTempVertexBuffer->_DeviceResource->RefCount() == 1)
976
pTempVertexBuffer->_DeviceResource = 0;
977
pTempVertexBuffer = pTempVertexBuffer->_next;
980
pTempVertexBuffer = pTempVertexBuffer->_next;
983
TDeviceResourceList< TRefGL<IOpenGLIndexBuffer> >* pTempIndexBuffer = _CachedIndexBufferList;
984
while(pTempIndexBuffer)
986
if(pTempIndexBuffer->_DeviceResource->RefCount() == 1)
988
pTempIndexBuffer->_DeviceResource = 0;
989
pTempIndexBuffer = pTempIndexBuffer->_next;
992
pTempIndexBuffer = pTempIndexBuffer->_next;
995
TDeviceResourceList< TRefGL<IOpenGLVertexDeclaration> >* pTempVertexDeclaration = _CachedVertexDeclarationList;
996
while(pTempVertexDeclaration)
998
if(pTempVertexDeclaration->_DeviceResource->RefCount() == 1)
1000
pTempVertexDeclaration->_DeviceResource = 0;
1001
pTempVertexDeclaration = pTempVertexDeclaration->_next;
1004
pTempVertexDeclaration = pTempVertexDeclaration->_next;
1007
TDeviceResourceList< TRefGL<IOpenGLTexture2D> >* pTempTexture2D = _CachedTextureList;
1008
while(pTempTexture2D)
1010
if(pTempTexture2D->_DeviceResource->RefCount() == 1)
1012
pTempTexture2D->_DeviceResource = 0;
1013
pTempTexture2D = pTempTexture2D->_next;
1016
pTempTexture2D = pTempTexture2D->_next;
1019
TDeviceResourceList< TRefGL<IOpenGLCubeTexture> >* pTempCubeTexture = _CachedCubeTextureList;
1020
while(pTempCubeTexture)
1022
if(pTempCubeTexture->_DeviceResource->RefCount() == 1)
1024
pTempCubeTexture->_DeviceResource = 0;
1025
pTempCubeTexture = pTempCubeTexture->_next;
1028
pTempCubeTexture = pTempCubeTexture->_next;
1031
TDeviceResourceList< TRefGL<IOpenGLVolumeTexture> >* pTempVolumeTexture = _CachedVolumeTextureList;
1032
while(pTempVolumeTexture)
1034
if(pTempVolumeTexture->_DeviceResource->RefCount() == 1)
1036
pTempVolumeTexture->_DeviceResource = 0;
1037
pTempVolumeTexture = pTempVolumeTexture->_next;
1040
pTempVolumeTexture = pTempVolumeTexture->_next;
1043
TDeviceResourceList< TRefGL<IOpenGLAnimatedTexture> >* pTempAnimatedTexture = _CachedAnimatedTextureList;
1044
while(pTempAnimatedTexture)
1046
if(pTempAnimatedTexture->_DeviceResource->RefCount() == 1)
1048
pTempAnimatedTexture->_DeviceResource = 0;
1049
pTempAnimatedTexture = pTempAnimatedTexture->_next;
1052
pTempAnimatedTexture = pTempAnimatedTexture->_next;
1055
TDeviceResourceList< TRefGL<IOpenGLFrameBufferObject> >* pTempFrameBufferObject = _CachedFrameBufferList;
1056
while(pTempFrameBufferObject)
1058
if(pTempFrameBufferObject->_DeviceResource->RefCount() == 1)
1060
pTempFrameBufferObject->_DeviceResource = 0;
1061
pTempFrameBufferObject = pTempFrameBufferObject->_next;
1064
pTempFrameBufferObject = pTempFrameBufferObject->_next;
1067
TDeviceResourceList< TRefGL<IOpenGLQuery> >* pTempQuery = _CachedQueryList;
1070
if(pTempQuery->_DeviceResource->RefCount() == 1)
1072
pTempQuery->_DeviceResource = 0;
1073
pTempQuery = pTempQuery->_next;
1076
pTempQuery = pTempQuery->_next;
1079
TDeviceResourceList< TRefGL<IOpenGLVertexShader> >* pTempVertexShader = _CachedVertexShaderList;
1080
while(pTempVertexShader)
1082
if(pTempVertexShader->_DeviceResource->RefCount() == 1)
1084
pTempVertexShader->_DeviceResource = 0;
1085
pTempVertexShader = pTempVertexShader->_next;
1088
pTempVertexShader = pTempVertexShader->_next;
1091
TDeviceResourceList< TRefGL<IOpenGLPixelShader> >* pTempPixelShader = _CachedPixelShaderList;
1092
while(pTempPixelShader)
1094
if(pTempPixelShader->_DeviceResource->RefCount() == 1)
1096
pTempPixelShader->_DeviceResource = 0;
1097
pTempPixelShader = pTempPixelShader->_next;
1100
pTempPixelShader = pTempPixelShader->_next;
1103
#if (NUX_ENABLE_CG_SHADERS)
1104
TDeviceResourceList< TRefGL<ICgVertexShader> >* pTempCgVertexShader = _CachedCGVertexShaderList;
1105
while(pTempCgVertexShader)
1107
if(pTempCgVertexShader->_DeviceResource->RefCount() == 1)
1109
pTempCgVertexShader->_DeviceResource = 0;
1110
pTempCgVertexShader = pTempCgVertexShader->_next;
1113
pTempCgVertexShader = pTempCgVertexShader->_next;
1116
TDeviceResourceList< TRefGL<ICgPixelShader> >* pTempCgPixelShader = _CachedCGPixelShaderList;
1117
while(pTempCgPixelShader)
1119
if(pTempCgPixelShader->_DeviceResource->RefCount() == 1)
1121
pTempCgPixelShader->_DeviceResource = 0;
1122
pTempCgPixelShader = pTempCgPixelShader->_next;
1125
pTempCgPixelShader = pTempCgPixelShader->_next;
1130
void GLDeviceFactory::InvalidateTextureUnit(int TextureUnitIndex)
1132
CHECKGL(glActiveTextureARB(TextureUnitIndex) );
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));
1140
// From lowest priority to highest priority:
1143
// GL_TEXTURE_RECTANGLE_ARB,
1145
// GL_TEXTURE_CUBE_MAP.
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) );
1154
int GLDeviceFactory::AllocateUnpackPixelBufferIndex(int* index)
1156
t_u32 num = (t_u32)_PixelBufferArray.size();
1157
for(t_u32 i = 0; i< num; i++)
1159
if(_PixelBufferArray[i].IsReserved == FALSE)
1161
_PixelBufferArray[i].IsReserved = TRUE;
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;
1176
int GLDeviceFactory::FreeUnpackPixelBufferIndex(const int index)
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))
1184
_PixelBufferArray[index].IsReserved = false;
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) );
1195
void* GLDeviceFactory::LockUnpackPixelBufferIndex(const int index, int Size)
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) );
1205
void* GLDeviceFactory::LockPackPixelBufferIndex(const int index, int Size)
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) );
1215
void GLDeviceFactory::UnlockUnpackPixelBufferIndex(const int index)
1217
GetThreadGLDeviceFactory()->BindUnpackPixelBufferIndex(index);
1218
CHECKGL( glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB) );
1219
CHECKGL( glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0) );
1222
void GLDeviceFactory::UnlockPackPixelBufferIndex(const int index)
1224
GetThreadGLDeviceFactory()->BindPackPixelBufferIndex(index);
1225
CHECKGL( glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB) );
1226
CHECKGL( glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0) );
1229
int GLDeviceFactory::BindUnpackPixelBufferIndex(const int index)
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))
1238
nuxAssertMsg(_PixelBufferArray[index].IsReserved == true, TEXT("[GLDeviceFactory::BindUnpackPixelBufferIndex] Trying to reserved pixel buffer index."));
1239
if(_PixelBufferArray[index].IsReserved == false)
1243
_PixelBufferArray[index].PBO->BindUnpackPixelBufferObject();
1247
int GLDeviceFactory::BindPackPixelBufferIndex(const int index)
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))
1256
nuxAssertMsg(_PixelBufferArray[index].IsReserved == true, TEXT("[GLDeviceFactory::BindPackPixelBufferIndex] Trying to reserved pixel buffer index."));
1257
if(_PixelBufferArray[index].IsReserved == false)
1261
_PixelBufferArray[index].PBO->BindPackPixelBufferObject();
1265
int GLDeviceFactory::FormatFrameBufferObject(t_u32 Width, t_u32 Height, BitmapFormat PixelFormat)
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);
1272
int GLDeviceFactory::SetColorRenderTargetSurface(t_u32 ColorAttachmentIndex, TRefGL<IOpenGLSurface> pRenderTargetSurface)
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);
1279
int GLDeviceFactory::SetDepthRenderTargetSurface(TRefGL<IOpenGLSurface> pDepthSurface)
1281
if(!GL_EXT_FRAMEBUFFER_OBJECT)
1282
{nuxDebugMsg(TEXT("[GLDeviceFactory::SetDepthRenderTargetSurface] No support for OpenGL framebuffer extension.")); return 0;}
1283
return _FrameBufferObject->SetDepthSurface(pDepthSurface);
1286
TRefGL<IOpenGLSurface> GLDeviceFactory::GetColorRenderTargetSurface(t_u32 ColorAttachmentIndex)
1288
if(!GL_EXT_FRAMEBUFFER_OBJECT)
1289
{nuxDebugMsg(TEXT("[GLDeviceFactory::GetColorRenderTargetSurface] No support for OpenGL framebuffer extension.")); return 0;}
1290
return _FrameBufferObject->GetRenderTarget(ColorAttachmentIndex);
1293
TRefGL<IOpenGLSurface> GLDeviceFactory::GetDepthRenderTargetSurface()
1295
if(!GL_EXT_FRAMEBUFFER_OBJECT)
1296
{nuxDebugMsg(TEXT("[GLDeviceFactory::GetDepthRenderTargetSurface] No support for OpenGL framebuffer extension.")); return 0;}
1297
return _FrameBufferObject->GetDepthRenderTarget();
1300
void GLDeviceFactory::ActivateFrameBuffer()
1302
if(!GL_EXT_FRAMEBUFFER_OBJECT)
1303
{nuxDebugMsg(TEXT("[GLDeviceFactory::ActivateFrameBuffer] No support for OpenGL framebuffer extension.")); return;}
1304
_FrameBufferObject->Activate();
1307
void GLDeviceFactory::DeactivateFrameBuffer()
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) );
1315
void GLDeviceFactory::Clear(FLOAT red, FLOAT green, FLOAT blue, FLOAT alpha, FLOAT depth, int stencil)
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) );
1323
void GLDeviceFactory::ClearColorRT(FLOAT red, FLOAT green, FLOAT blue, FLOAT alpha)
1325
CHECKGL( glClearColor(red, green, blue, alpha) );
1326
CHECKGL( glClear(GL_COLOR_BUFFER_BIT) );
1329
void GLDeviceFactory::ClearDepthRT(FLOAT depth)
1331
CHECKGL( glClearDepth(depth) );
1332
CHECKGL( glClear(GL_DEPTH_BUFFER_BIT) );
1334
void GLDeviceFactory::ClearStencilRT(int stencil)
1336
CHECKGL( glClearStencil(stencil) );
1337
CHECKGL( glClear(GL_STENCIL_BUFFER_BIT) );
1340
void GLDeviceFactory::ClearFloatingPointColorRT(int x, int y, int width, int height,
1341
FLOAT red, FLOAT green, FLOAT blue, FLOAT alpha) // use a Quad.
1343
DrawQuad_FixPipe(x, y, width, height, red, green, blue, alpha);
1346
void GLDeviceFactory::ClearSurfaceWithColor(TRefGL<IOpenGLSurface> s_, const SURFACE_RECT *rect_, float r, float g, float b, float a)
1348
if(!GL_EXT_FRAMEBUFFER_OBJECT)
1349
{nuxDebugMsg(TEXT("[GLDeviceFactory::ClearSurfaceWithColor] No support for OpenGL framebuffer extension."));}
1351
FormatFrameBufferObject(s_->GetWidth(), s_->GetHeight(), s_->GetPixelFormat());
1352
SetColorRenderTargetSurface(0, s_);
1353
SetDepthRenderTargetSurface(0);
1354
ActivateFrameBuffer();
1355
ClearFloatingPointColorRT(rect_->left,
1357
rect_->right - rect_->left,
1358
rect_->bottom - rect_->top,
1362
void GLDeviceFactory::SetCurrentFrameBufferObject(TRefGL<IOpenGLFrameBufferObject> fbo)
1364
_CurrentFrameBufferObject = fbo;
1367
TRefGL<IOpenGLFrameBufferObject> GLDeviceFactory::GetCurrentFrameBufferObject()
1369
return _CurrentFrameBufferObject;