forked from QianMo/GPU-Gems-Book-Source-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolkit.cpp
282 lines (221 loc) · 6.55 KB
/
toolkit.cpp
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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include "GL/glew.h"
#include "GL/glut.h"
#include "GL/wglew.h"
#include "timer.h"
#include "fragprog.h"
#include "window.h"
#include "defines.h"
void Keyboard( unsigned char key, int x, int y );
void Mouse( int button, int state, int x, int y );
void Motion( int x, int y );
void Idle( void );
void Init( void );
void Display( void );
void DoBinarySearch( void );
Window floatbuffer;
Window colorbuffer;
FragProg binarysearchfp;
GLuint sortedtextureid;
float sortedtexture[(int)(W*H)*4];
FragProg bitonicsortfp;
GLuint randomtextureid;
float randomtexture[(int)(W*H)*4];
FragProg reducefp;
float data[(int)(W*H)*4];
Timer frametime;
void CheckErrors() {
GLenum error = glGetError();
if( error != GL_NO_ERROR ) {
fprintf( stderr, "\nGL Error: %s\n", gluErrorString( error ) );
assert(0);
}
}
void Init( void ){
int i;
glutInitWindowSize( W, H );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA );
glutCreateWindow( "NVToolkit" );
int err = glewInit();
if (GLEW_OK != err){
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
}
glutDisplayFunc( Display );
glutMotionFunc( Motion );
glutKeyboardFunc( Keyboard );
glutMouseFunc( Mouse );
glutIdleFunc( Idle );
colorbuffer.SetContext();
glDrawBuffer(GL_FRONT);
glClear(GL_COLOR_BUFFER_BIT);
floatbuffer.CreateContext(W, H, "floatbuffer");
binarysearchfp.Load("binarysearch.fp");
glGenTextures(1, &sortedtextureid);
for(i=0; i<W*H; i++){
sortedtexture[4*i+0] = i;
sortedtexture[4*i+1] = i;
sortedtexture[4*i+2] = i;
sortedtexture[4*i+3] = i;
}
glActiveTextureARB( GL_TEXTURE1_ARB );
glBindTexture(GL_TEXTURE_RECTANGLE_NV, sortedtextureid);
glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, W, H, 0, GL_RGBA, GL_FLOAT, sortedtexture);
bitonicsortfp.Load("bitonicsort.fp");
glGenTextures(1, &randomtextureid);
for(i=0; i<W*H; i++){
randomtexture[4*i+0] = (rand() * (W*H-1)) / RAND_MAX;
randomtexture[4*i+1] = randomtexture[4*i+0];
randomtexture[4*i+2] = randomtexture[4*i+0];
randomtexture[4*i+3] = randomtexture[4*i+0];
}
glActiveTextureARB( GL_TEXTURE2_ARB );
glBindTexture(GL_TEXTURE_RECTANGLE_NV, randomtextureid);
glTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, W, H, 0, GL_RGBA, GL_FLOAT, randomtexture);
reducefp.Load("reduce.fp");
printf ("Press 1 for Binary Search\n");
printf ("Press 2 for Bitonic Sort\n");
printf ("Press 3 for Reduce\n");
printf ("Press q to quit\n");
frametime.init();
}
int main(int argc, char **argv)
{
glutInit( &argc, argv );
Init();
glutMainLoop();
return 0;
}
void Display( void ){
char title[128];
sprintf(title, "%3.3f fps NVToolkit", frametime.fps());
glutSetWindowTitle(title);
frametime.reset();
}
void DoBinarySearch( void ){
floatbuffer.MakeCurrent();
binarysearchfp.Bind();
binarysearchfp.SetParameter1f("stride", N/2);
binarysearchfp.SetParameter1f("pbufinfo", W);
binarysearchfp.SetParameter1f("sortbufinfo", W);
glActiveTextureARB( GL_TEXTURE1_ARB );
glBindTexture(GL_TEXTURE_RECTANGLE_NV, sortedtextureid);
binarysearchfp.Run(0,0,W,H);
glFinish();
char title[128];
if (frametime.elapsed_ms() < 1000)
sprintf(title, "%3.3f ms SEARCH NVToolkit", frametime.elapsed_ms());
else
sprintf(title, "%3.3f seconds SEARCH NVToolkit", frametime.elapsed_ms()/1000);
glutSetWindowTitle(title);
frametime.reset();
#if 0
glReadPixels(0, 0, W, H, GL_RGBA, GL_FLOAT, data);
for(int i=0; i<W*H-1; i++){
//fprintf(stderr, "%i : %f %f %f %f\n", i, data[4*i+0], data[4*i+1], data[4*i+2], data[4*i+3]);
assert (data[4*i] <= data[4*(i+1)]);
}
#endif
colorbuffer.MakeCurrent();
}
void DoReduce( void ){
float rval[4];
floatbuffer.MakeCurrent();
reducefp.Bind();
glActiveTextureARB( GL_TEXTURE1_ARB );
glBindTexture(GL_TEXTURE_RECTANGLE_NV, randomtextureid);
CheckErrors();
for (unsigned int i=W/2; i>0; i = i/2) {
// Note that we have to subtract -1 from
// the texture coordinates since we
// specify the texture coordinates at the
// edge of the pixel but OpenGL samples at
// the pixel centers.
reducefp.Run1( 0, 0, i, i,
-1, -1, i*2-1, i*2-1);
glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_NV, 0,
0, 0, 0, 0, i, i);
}
glFinish();
char title[128];
if (frametime.elapsed_ms() < 1000)
sprintf(title, "%3.3f ms REDUCE NVToolkit", frametime.elapsed_ms());
else
sprintf(title, "%3.3f seconds REDUCE NVToolkit", frametime.elapsed_ms()/1000);
glutSetWindowTitle(title);
frametime.reset();
CheckErrors();
colorbuffer.MakeCurrent();
}
void DoBitonicSort( void ){
int stepno, offset, stage;
int i,j;
floatbuffer.MakeCurrent();
CheckErrors();
for(i=0; i<LOGN; i++){
stepno = 1 << (i+1);//(int)pow(2,i+1);
for(j=i; j>=0; j--){
offset = 1 << j;//(int)pow(2,j);
stage = 2*offset;
//fprintf(stderr, ".");
bitonicsortfp.Bind();
bitonicsortfp.SetParameter3f("pbufinfo", W, 1.0/W, 1.0);
bitonicsortfp.SetParameter1f("offset", offset);
bitonicsortfp.SetParameter1f("stage", stage);
bitonicsortfp.SetParameter1f("stepno", stepno);
glActiveTextureARB( GL_TEXTURE2_ARB );
glBindTexture(GL_TEXTURE_RECTANGLE_NV, randomtextureid);
bitonicsortfp.Run(0,0,W,H);
glActiveTextureARB( GL_TEXTURE2_ARB );
glBindTexture(GL_TEXTURE_RECTANGLE_NV, randomtextureid);
glCopyTexImage2D(GL_TEXTURE_RECTANGLE_NV, 0, GL_FLOAT_RGBA32_NV, 0, 0, W, H, 0);
}
}
glFinish();
char title[128];
if (frametime.elapsed_ms() < 1000)
sprintf(title, "%3.3f ms SORT NVToolkit", frametime.elapsed_ms());
else
sprintf(title, "%3.3f seconds SORT NVToolkit", frametime.elapsed_ms()/1000);
glutSetWindowTitle(title);
frametime.reset();
#if 0
glReadPixels(0, 0, W, H, GL_RGBA, GL_FLOAT, data);
for(i=0; i<W*H-1; i++){
//fprintf(stderr, "%i : %f %f %f %f\n", i, data[4*i+0], data[4*i+1], data[4*i+2], data[4*i+3]);
assert (data[4*i] <= data[4*(i+1)]);
}
#endif
CheckErrors();
colorbuffer.MakeCurrent();
}
void Idle(void){
glutPostRedisplay();
}
void Keyboard( unsigned char key, int x, int y ){
switch (key) {
case '0':
glutDisplayFunc( Display );
break;
case '1':
glutDisplayFunc( DoBinarySearch );
break;
case '2':
glutDisplayFunc( DoBitonicSort );
break;
case '3':
glutDisplayFunc( DoReduce );
break;
case 'Q':
case 'q':
case 27:
exit(0);
break;
}
}
void Mouse( int button, int state, int x, int y ){
}
void Motion( int x, int y ){
}