30 Oct 2009
 

glCopyTexSubImage2D is faster than FBO 8\ (on GeForce Go 7300)

 
 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#include <stdio.h>
#include <math.h>
#include <SDL.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#ifdef _WIN32
#include <GL/wglew.h>
#else
#include <GL/glxew.h>
#endif
#define BUFSIZE 1024
static const char* progname;
typedef struct _Vertex
{
float tx, ty;
float nx, ny, nz;
float x, y, z;
} Vertex;
typedef struct _Mesh
{
uint32_t _vertexCount;
Vertex* _verts;
} Mesh;
int mesh_load(Mesh* mesh, const char* filename)
{
FILE* f = fopen(filename, "rb");
if(!f)
return 0;
size_t size = 0;
char* buf = (char*)malloc(BUFSIZE);
while(1)
{
size += fread(buf + size, 1, BUFSIZE, f);
if(feof(f))
break;
if(ferror(f))
{
fclose(f);
return 0;
break;
}
buf = (char*)realloc(buf, size + BUFSIZE);
}
mesh->_vertexCount = size/sizeof(Vertex);
mesh->_verts = (Vertex*)buf;
fclose(f);
return 1;
}
Mesh mesh;
GLuint vb;
void init_vbo(Mesh* mesh)
{
glGenBuffersARB(1, &vb);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vb);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, mesh->_vertexCount*sizeof(Vertex), mesh->_verts, GL_STATIC_DRAW_ARB);
glBindBufferARB(GL_VERTEX_ARRAY, 0);
}
Uint32 rot_timer = 0;
Uint32 timer = 0;
float angle = -165;
int counter = 0;
GLuint vp_id, fp_id;
char vp_code[] = "!!ARBvp1.0\n"
"OPTION ARB_position_invariant;\n"
"ATTRIB iN = vertex.normal;\n"
"PARAM mvit[4] = {state.matrix.modelview.invtrans};\n"
"OUTPUT oN = result.texcoord[7];\n"
"DP4 oN.x, mvit[0], iN;\n"
"DP4 oN.y, mvit[1], iN;\n"
"DP4 oN.z, mvit[2], iN;\n"
"MOV result.color, vertex.color;\n"
"END";
char fp_code[] = "!!ARBfp1.0\n"
"ATTRIB iP = fragment.position;\n"
"ATTRIB iC = fragment.color;\n"
"ATTRIB iN = fragment.texcoord[7];\n"
"PARAM amb = {0.5, 0.3, 0.036, 1.0};\n"
"PARAM l0pos = state.light[0].position;\n"
"TEMP toL, tmpC, n1;\n"
"OUTPUT oC = result.color;\n"
"ADD toL, l0pos, -iP;\n"
"DP3 toL.w, toL, toL;\n"
"RSQ toL.w, toL.w;\n"
"DP3 n1.w, iN, iN;\n"
"RSQ n1.w, n1.w;"
"MUL n1.xyz, n1.w, iN;\n"
"MUL toL.xyz, toL.w, toL;\n"
"DP3_SAT tmpC, toL, n1;\n"
"MAD tmpC, tmpC, iC, amb;\n"
"MOV tmpC.w, 1;\n"
"MOV oC, tmpC;\n"
"END";
GLuint init_vp(const char* code)
{
GLuint vp;
glGenProgramsARB(1, &vp);
glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vp);
glProgramStringARB(GL_VERTEX_PROGRAM_ARB,
GL_PROGRAM_FORMAT_ASCII_ARB,
strlen(code), code);
if(glGetError() == GL_INVALID_OPERATION)
{
fprintf(stderr, "\n");
GLint error_pos;
const GLubyte* error_str;
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos);
error_str = glGetString(GL_PROGRAM_ERROR_STRING_ARB);
fprintf(stderr, "Can't create vertex program. Error at position %d. Line: \"%s\".\n",
error_pos, error_str);
return 0;
}
return vp;
}
GLuint init_fp(const char* code)
{
GLuint fp;
glGenProgramsARB(1, &fp);
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fp);
glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
GL_PROGRAM_FORMAT_ASCII_ARB,
strlen(code), code);
if(glGetError() == GL_INVALID_OPERATION)
{
fprintf(stderr, "\n");
GLint error_pos;
const GLubyte* error_str;
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos);
error_str = glGetString(GL_PROGRAM_ERROR_STRING_ARB);
fprintf(stderr, "Can't create fragment program. Error at position %d. Line: \"%s\".\n",
error_pos, error_str);
return 0;
}
return fp;
}
GLuint tex;
GLuint fb;
GLuint rb;
GLuint fp_post_id;
void init_tex(int w, int h)
{
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
}
void init_fbo(int w, int h)
{
glGenFramebuffersEXT(1, &fb);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, tex, 0);
glGenRenderbuffersEXT(1, &rb);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rb);
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, w, h);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, rb);
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
switch(status)
{
case GL_FRAMEBUFFER_COMPLETE_EXT:
printf("FBO created successfully.\n");
break;
default:
fprintf(stderr, "Can't create FBO.\n");
exit(-1);
}
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
void draw(int w, int h)
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, w/(float)h, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, -10, 0, 0, 0, 0, 1, 0);
glTranslatef(0, 0, -8);
glRotatef(10, 1, 0, 0);
glRotatef(angle, 0, 1, 0);
glRotatef(15, 0, 0, 1);
glEnable(GL_VERTEX_PROGRAM_ARB);
glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vp_id);
glEnable(GL_FRAGMENT_PROGRAM_ARB);
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fp_id);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vb);
glInterleavedArrays(GL_T2F_N3F_V3F, sizeof(Vertex), 0);
glDrawArrays(GL_TRIANGLES, 0, mesh._vertexCount);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
glBindProgramARB(GL_VERTEX_PROGRAM_ARB, 0);
glDisable(GL_VERTEX_PROGRAM_ARB);
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
glDisable(GL_FRAGMENT_PROGRAM_ARB);
Uint32 now = SDL_GetTicks();
Uint32 elapsed = now - rot_timer;
rot_timer = now;
angle += 30.0 * (elapsed/1000.0);
angle = fmod(angle, 360);
++counter;
Uint32 delta = now - timer;
if(delta > 1000)
{
printf("FPS: %4.2f (on %d verts)\n", counter / (delta/(float)1000), mesh._vertexCount);
counter = 0;
timer = now;
}
}
void draw_to_fbo(int w, int h)
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb);
draw(w, h);
glFlush();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
void draw_and_copy(int w, int h)
{
draw(w, h);
glBindTexture(GL_TEXTURE_2D, tex);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, w, h);
}
char fp_post_code[] = "!!ARBfp1.0\n"
"ATTRIB tc0 = fragment.texcoord[0];\n"
"TEMP C, c0, c1, c2, tc1, tc2;\n"
"OUTPUT oC = result.color;"
"ADD tc1, tc0, {0.01, 0.01, 0, 0};\n"
"ADD tc2, tc0, {-0.016, 0.005, 0, 0};\n"
"TEX c0, tc0, texture[0], 2D;\n"
"TEX c1, tc1, texture[0], 2D;\n"
"TEX c2, tc2, texture[0], 2D;\n"
"MAD_SAT C, c0, c1.bgra, c2;\n"
"MUL c0.x, tc0.x, 15;\n"
"SIN c0, c0.x;\n"
"ADD_SAT oC, C, c0;\n"
"END";
void post(int w, int h)
{
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, w, h);
glBindTexture(GL_TEXTURE_2D, tex);
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fp_post_id);
glEnable(GL_FRAGMENT_PROGRAM_ARB);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(w, 0);
glTexCoord2f(1, 1);
glVertex2f(w, h);
glTexCoord2f(0, 1);
glVertex2f(0, h);
glEnd();
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
glDisable(GL_FRAGMENT_PROGRAM_ARB);
}
void print_usage(FILE* f, int exit_code)
{
fprintf(f, "Usage: %s some.mesh [fbo(default)|copy]\n", progname);
exit(exit_code);
}
typedef enum
{
RENDER_STRATEGY_FBO,
RENDER_STRATEGY_COPY,
} RenderStrategy;
int main(int argc, char *argv[])
{
progname = argv[0];
if(argc < 2 || argc > 3)
print_usage(stderr, -1);
RenderStrategy rs = RENDER_STRATEGY_FBO;
if(3 == argc)
{
if(strcmp(argv[2], "fbo") == 0)
rs = RENDER_STRATEGY_FBO;
else if(strcmp(argv[2], "copy") == 0)
rs = RENDER_STRATEGY_COPY;
else
print_usage(stderr, -1);
}
switch(rs)
{
case RENDER_STRATEGY_FBO:
printf("Using FBO.\n");
break;
case RENDER_STRATEGY_COPY:
printf("Using glCopyTexSubImage2D.\n");
break;
}
int width = 800;
int height = 600;
const char* meshfile = argv[1];
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
char title[] = "glCopyTexSubImage2D vs EXT_framebuffer_object";
SDL_WM_SetCaption(title, title);
int flags = SDL_OPENGL;
int w = width, h = height;
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if(!SDL_SetVideoMode(w, h, 0, flags))
{
fprintf(stderr, "Can't initialize video mode: %s\n", SDL_GetError());
exit(-1);
}
if(glewInit() != GLEW_OK)
{
fprintf(stderr, "Can't initialize GLEW.\n");
exit(-1);
}
if(!mesh_load(&mesh, meshfile))
{
fprintf(stderr, "Can't load mesh from '%s'.\n", meshfile);
exit(-1);
}
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
rot_timer = timer = SDL_GetTicks();
init_vbo(&mesh);
vp_id = init_vp(vp_code);
fp_id = init_fp(fp_code);
fp_post_id = init_fp(fp_post_code);
init_tex(width, height);
if(rs == RENDER_STRATEGY_FBO)
init_fbo(width, height);
int done = 0;
while(!done)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
done = 1;
break;
case SDL_KEYDOWN:
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
{
done = 1;
}
default:
break;
}
}
default:
break;
}
}
if(rs == RENDER_STRATEGY_FBO)
draw_to_fbo(w, h);
else
draw_and_copy(w, h);
post(w, h);
glFlush();
SDL_GL_SwapBuffers();
// SDL_Delay(4);
}
return 0;
}