forked from NVIDIA/cuda-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
1051 lines (880 loc) · 32.3 KB
/
main.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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
This example demonstrates the use of CUDA/OpenGL interoperability
to post-process an image of a 3D scene generated in OpenGL.
The basic steps are:
1 - render the scene to the framebuffer
2 - map the color texture so that its memory is accessible from CUDA
4 - run CUDA to process the image, writing to memory
a- either mapped from a second PBO
b- or allocated through CUDA
6 - copy result
a- from the PBO to a texture with glTexSubImage2D()
b- or map the target texture and do a cuda memory copy
7 - display the texture with a fullscreen quad
The example also provides two solutions for the format of the image:
- RGBA16F : more bytes involved but easier to handle because
compatible with regular fragment shader
- RGBA8UI : 32bytes, but the teapot color must be scaled by 255 (so we
needed GLSL code)
How about RGBA8? The CUDA driver does not have consistent interoperability
with this format.
Older GPUs may not store data the same way compared with newer GPUs,
resulting in a swap of R and B components
On older HW, this will need workarounds.
Press space to toggle the CUDA processing on/off.
Press 'a' to toggle animation.
Press '+' and '-' to increment and decrement blur radius
*/
// this mode is "old fashion" : use glTexSubImage2D() to update the final result
// commenting it will make the sample use the other way :
// map a texture in CUDA and blit the result into it
#define USE_TEXSUBIMAGE2D
#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
#define WINDOWS_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#pragma warning(disable : 4996)
#endif
// OpenGL Graphics includes
#include <helper_gl.h>
#if defined(__APPLE__) || defined(MACOSX)
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#include <GLUT/glut.h>
// Sorry for Apple : unsigned int sampler is not available to you, yet...
// Let's switch to the use of PBO and glTexSubImage
#define USE_TEXSUBIMAGE2D
#else
#include <GL/freeglut.h>
#endif
// CUDA includes
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
// CUDA utilities and system includes
#include <helper_cuda.h>
#include <helper_functions.h>
#include <rendercheck_gl.h>
// Shared Library Test Functions
#define MAX_EPSILON 10
#define REFRESH_DELAY 10 // ms
const char *sSDKname = "postProcessGL";
unsigned int g_TotalErrors = 0;
// CheckFBO/BackBuffer class objects
CheckRender *g_CheckRender = NULL;
////////////////////////////////////////////////////////////////////////////////
// constants / global variables
unsigned int window_width = 512;
unsigned int window_height = 512;
unsigned int image_width = 512;
unsigned int image_height = 512;
int iGLUTWindowHandle = 0; // handle to the GLUT window
// pbo and fbo variables
#ifdef USE_TEXSUBIMAGE2D
GLuint pbo_dest;
struct cudaGraphicsResource *cuda_pbo_dest_resource;
#else
unsigned int *cuda_dest_resource;
GLuint shDrawTex; // draws a texture
struct cudaGraphicsResource *cuda_tex_result_resource;
#endif
extern cudaTextureObject_t inTexObject;
GLuint fbo_source;
struct cudaGraphicsResource *cuda_tex_screen_resource;
unsigned int size_tex_data;
unsigned int num_texels;
unsigned int num_values;
// (offscreen) render target fbo variables
GLuint framebuffer; // to bind the proper targets
GLuint depth_buffer; // for proper depth test while rendering the scene
GLuint tex_screen; // where we render the image
GLuint tex_cudaResult; // where we will copy the CUDA result
float rotate[3];
char *ref_file = NULL;
bool enable_cuda = true;
bool animate = true;
int blur_radius = 8;
int max_blur_radius = 16;
int *pArgc = NULL;
char **pArgv = NULL;
// Timer
static int fpsCount = 0;
static int fpsLimit = 1;
StopWatchInterface *timer = NULL;
#ifndef USE_TEXTURE_RGBA8UI
#pragma message("Note: Using Texture fmt GL_RGBA16F_ARB")
#else
// NOTE: the current issue with regular RGBA8 internal format of textures
// is that HW stores them as BGRA8. Therefore CUDA will see BGRA where users
// expected RGBA8. To prevent this issue, the driver team decided to prevent
// this to happen
// instead, use RGBA8UI which required the additional work of scaling the
// fragment shader
// output from 0-1 to 0-255. This is why we have some GLSL code, in this case
#pragma message("Note: Using Texture RGBA8UI + GLSL for teapot rendering")
#endif
GLuint shDrawPot; // colors the teapot
////////////////////////////////////////////////////////////////////////////////
extern "C" void launch_cudaProcess(dim3 grid, dim3 block, int sbytes,
cudaArray *g_data, unsigned int *g_odata,
int imgw, int imgh, int tilew, int radius,
float threshold, float highlight);
// Forward declarations
void runStdProgram(int argc, char **argv);
void FreeResource();
void Cleanup(int iExitCode);
// GL functionality
bool initGL(int *argc, char **argv);
#ifdef USE_TEXSUBIMAGE2D
void createPBO(GLuint *pbo, struct cudaGraphicsResource **pbo_resource);
void deletePBO(GLuint *pbo);
#endif
void createTextureDst(GLuint *tex_cudaResult, unsigned int size_x,
unsigned int size_y);
void createTextureSrc(GLuint *tex_screen, unsigned int size_x,
unsigned int size_y);
void deleteTexture(GLuint *tex);
void createDepthBuffer(GLuint *depth, unsigned int size_x, unsigned int size_y);
void deleteDepthBuffer(GLuint *depth);
void createFramebuffer(GLuint *fbo, GLuint color, GLuint depth);
void deleteFramebuffer(GLuint *fbo);
// rendering callbacks
void display();
void idle();
void keyboard(unsigned char key, int x, int y);
void reshape(int w, int h);
void mainMenu(int i);
////////////////////////////////////////////////////////////////////////////////
//! Run the Cuda part of the computation
////////////////////////////////////////////////////////////////////////////////
void process(int width, int height, int radius) {
cudaArray *in_array;
unsigned int *out_data;
#ifdef USE_TEXSUBIMAGE2D
checkCudaErrors(cudaGraphicsMapResources(1, &cuda_pbo_dest_resource, 0));
size_t num_bytes;
checkCudaErrors(cudaGraphicsResourceGetMappedPointer(
(void **)&out_data, &num_bytes, cuda_pbo_dest_resource));
// printf("CUDA mapped pointer of pbo_out: May access %ld bytes, expected %d\n",
// num_bytes, size_tex_data);
#else
out_data = cuda_dest_resource;
#endif
// map buffer objects to get CUDA device pointers
checkCudaErrors(cudaGraphicsMapResources(1, &cuda_tex_screen_resource, 0));
// printf("Mapping tex_in\n");
checkCudaErrors(cudaGraphicsSubResourceGetMappedArray(
&in_array, cuda_tex_screen_resource, 0, 0));
// calculate grid size
dim3 block(16, 16, 1);
// dim3 block(16, 16, 1);
dim3 grid(width / block.x, height / block.y, 1);
int sbytes = (block.x + (2 * radius)) * (block.y + (2 * radius)) *
sizeof(unsigned int);
// execute CUDA kernel
launch_cudaProcess(grid, block, sbytes, in_array, out_data, width, height,
block.x + (2 * radius), radius, 0.8f, 4.0f);
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_tex_screen_resource, 0));
#ifdef USE_TEXSUBIMAGE2D
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_pbo_dest_resource, 0));
#endif
checkCudaErrors(cudaDestroyTextureObject(inTexObject));
}
#ifdef USE_TEXSUBIMAGE2D
////////////////////////////////////////////////////////////////////////////////
//! Create PBO
////////////////////////////////////////////////////////////////////////////////
void createPBO(GLuint *pbo, struct cudaGraphicsResource **pbo_resource) {
// set up vertex data parameter
num_texels = image_width * image_height;
num_values = num_texels * 4;
size_tex_data = sizeof(GLubyte) * num_values;
void *data = malloc(size_tex_data);
// create buffer object
glGenBuffers(1, pbo);
glBindBuffer(GL_ARRAY_BUFFER, *pbo);
glBufferData(GL_ARRAY_BUFFER, size_tex_data, data, GL_DYNAMIC_DRAW);
free(data);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// register this buffer object with CUDA
checkCudaErrors(cudaGraphicsGLRegisterBuffer(pbo_resource, *pbo,
cudaGraphicsMapFlagsNone));
SDK_CHECK_ERROR_GL();
}
void deletePBO(GLuint *pbo) {
glDeleteBuffers(1, pbo);
SDK_CHECK_ERROR_GL();
*pbo = 0;
}
#endif
const GLenum fbo_targets[] = {
GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT,
GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
#ifndef USE_TEXSUBIMAGE2D
static const char *glsl_drawtex_vertshader_src =
"void main(void)\n"
"{\n"
" gl_Position = gl_Vertex;\n"
" gl_TexCoord[0].xy = gl_MultiTexCoord0.xy;\n"
"}\n";
static const char *glsl_drawtex_fragshader_src =
"#version 130\n"
"uniform usampler2D texImage;\n"
"void main()\n"
"{\n"
" vec4 c = texture(texImage, gl_TexCoord[0].xy);\n"
" gl_FragColor = c / 255.0;\n"
"}\n";
#endif
static const char *glsl_drawpot_fragshader_src =
// WARNING: seems like the gl_FragColor doesn't want to output >1 colors...
// you need version 1.3 so you can define a uvec4 output...
// but MacOSX complains about not supporting 1.3 !!
// for now, the mode where we use RGBA8UI may not work properly for Apple : only
// RGBA16F works (default)
#if defined(__APPLE__) || defined(MACOSX)
"void main()\n"
"{"
" gl_FragColor = vec4(gl_Color * 255.0);\n"
"}\n";
#else
"#version 130\n"
"in vec4 inColor;\n"
"out uvec4 FragColor;\n"
"void main()\n"
"{"
" FragColor = uvec4(inColor.xyz * 255.0, 255.0);\n"
"}\n";
#endif
////////////////////////////////////////////////////////////////////////////////
//! render a simple 3D scene
////////////////////////////////////////////////////////////////////////////////
void renderScene(bool colorScale) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (colorScale) {
glUseProgram(shDrawPot);
glBindFragDataLocationEXT(shDrawPot, 0, "FragColor");
SDK_CHECK_ERROR_GL();
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.0);
glRotatef(rotate[0], 1.0, 0.0, 0.0);
glRotatef(rotate[1], 0.0, 1.0, 0.0);
glRotatef(rotate[2], 0.0, 0.0, 1.0);
glViewport(0, 0, 512, 512);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glutSolidTeapot(1.0);
if (colorScale) {
glUseProgram(0);
}
SDK_CHECK_ERROR_GL();
}
// copy image and process using CUDA
void processImage() {
// run the Cuda kernel
process(image_width, image_height, blur_radius);
// CUDA generated data in cuda memory or in a mapped PBO made of BGRA 8 bits
// 2 solutions, here :
// - use glTexSubImage2D(), there is the potential to loose performance in
// possible hidden conversion
// - map the texture and blit the result thanks to CUDA API
#ifdef USE_TEXSUBIMAGE2D
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo_dest);
glBindTexture(GL_TEXTURE_2D, tex_cudaResult);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image_width, image_height, GL_RGBA,
GL_UNSIGNED_BYTE, NULL);
SDK_CHECK_ERROR_GL();
glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, 0);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
#else
// We want to copy cuda_dest_resource data to the texture
// map buffer objects to get CUDA device pointers
cudaArray *texture_ptr;
checkCudaErrors(cudaGraphicsMapResources(1, &cuda_tex_result_resource, 0));
checkCudaErrors(cudaGraphicsSubResourceGetMappedArray(
&texture_ptr, cuda_tex_result_resource, 0, 0));
int num_texels = image_width * image_height;
int num_values = num_texels * 4;
int size_tex_data = sizeof(GLubyte) * num_values;
checkCudaErrors(cudaMemcpyToArray(texture_ptr, 0, 0, cuda_dest_resource,
size_tex_data, cudaMemcpyDeviceToDevice));
checkCudaErrors(cudaGraphicsUnmapResources(1, &cuda_tex_result_resource, 0));
#endif
}
// display image to the screen as textured quad
void displayImage(GLuint texture) {
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, window_width, window_height);
// if the texture is a 8 bits UI, scale the fetch with a GLSL shader
#ifndef USE_TEXSUBIMAGE2D
glUseProgram(shDrawTex);
GLint id = glGetUniformLocation(shDrawTex, "texImage");
glUniform1i(id, 0); // texture unit 0 to "texImage"
SDK_CHECK_ERROR_GL();
#endif
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(-1.0, -1.0, 0.5);
glTexCoord2f(1.0, 0.0);
glVertex3f(1.0, -1.0, 0.5);
glTexCoord2f(1.0, 1.0);
glVertex3f(1.0, 1.0, 0.5);
glTexCoord2f(0.0, 1.0);
glVertex3f(-1.0, 1.0, 0.5);
glEnd();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glDisable(GL_TEXTURE_2D);
#ifndef USE_TEXSUBIMAGE2D
glUseProgram(0);
#endif
SDK_CHECK_ERROR_GL();
}
////////////////////////////////////////////////////////////////////////////////
//! Display callback
////////////////////////////////////////////////////////////////////////////////
void display() {
sdkStartTimer(&timer);
if (enable_cuda) {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer);
#ifndef USE_TEXTURE_RGBA8UI
renderScene(false);
#else
renderScene(true); // output of fragment * by 255 (for RGBA8UI texture)
#endif
processImage();
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
displayImage(tex_cudaResult);
} else {
renderScene(false);
}
// NOTE: I needed to add this call so the timing is consistent.
// Need to investigate why
cudaDeviceSynchronize();
sdkStopTimer(&timer);
// flip backbuffer
glutSwapBuffers();
// If specified, Check rendering against reference,
if (ref_file && g_CheckRender && g_CheckRender->IsQAReadback()) {
static int pass = 0;
if (pass > 0) {
g_CheckRender->readback(window_width, window_height);
char currentOutputPPM[256];
sprintf(currentOutputPPM, "teapot_%d.ppm", blur_radius);
g_CheckRender->savePPM(currentOutputPPM, true, NULL);
if (!g_CheckRender->PPMvsPPM(currentOutputPPM,
sdkFindFilePath(ref_file, pArgv[0]),
MAX_EPSILON, 0.30f)) {
g_TotalErrors++;
}
Cleanup((g_TotalErrors == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
}
pass++;
}
// Update fps counter, fps/title display and log
if (++fpsCount == fpsLimit) {
char cTitle[256];
float fps = 1000.0f / sdkGetAverageTimerValue(&timer);
sprintf(cTitle, "CUDA GL Post Processing (%d x %d): %.1f fps", window_width,
window_height, fps);
glutSetWindowTitle(cTitle);
// printf("%s\n", cTitle);
fpsCount = 0;
fpsLimit = (int)((fps > 1.0f) ? fps : 1.0f);
sdkResetTimer(&timer);
}
}
void timerEvent(int value) {
if (animate) {
rotate[0] += 0.2f;
if (rotate[0] > 360.0f) {
rotate[0] -= 360.0f;
}
rotate[1] += 0.6f;
if (rotate[1] > 360.0f) {
rotate[1] -= 360.0f;
}
rotate[2] += 1.0f;
if (rotate[2] > 360.0f) {
rotate[2] -= 360.0f;
}
}
glutPostRedisplay();
glutTimerFunc(REFRESH_DELAY, timerEvent, 0);
}
////////////////////////////////////////////////////////////////////////////////
//! Keyboard events handler
////////////////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int /*x*/, int /*y*/) {
switch (key) {
case (27):
Cleanup(EXIT_SUCCESS);
break;
case ' ':
enable_cuda ^= 1;
#ifdef USE_TEXTURE_RGBA8UI
if (enable_cuda) {
glClearColorIuiEXT(128, 128, 128, 255);
} else {
glClearColor(0.5, 0.5, 0.5, 1.0);
}
#endif
break;
case 'a':
animate ^= 1;
break;
case '=':
case '+':
if (blur_radius < 16) {
blur_radius++;
}
printf("radius = %d\n", blur_radius);
break;
case '-':
if (blur_radius > 1) {
blur_radius--;
}
printf("radius = %d\n", blur_radius);
break;
}
}
void reshape(int w, int h) {
window_width = w;
window_height = h;
}
void mainMenu(int i) { keyboard((unsigned char)i, 0, 0); }
////////////////////////////////////////////////////////////////////////////////
//!
////////////////////////////////////////////////////////////////////////////////
void createTextureSrc(GLuint *tex_screen, unsigned int size_x,
unsigned int size_y) {
// create a texture
glGenTextures(1, tex_screen);
glBindTexture(GL_TEXTURE_2D, *tex_screen);
// set basic parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// buffer data
#ifndef USE_TEXTURE_RGBA8UI
printf("Creating a Texture render target GL_RGBA16F_ARB\n");
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, size_x, size_y, 0, GL_RGBA,
GL_UNSIGNED_BYTE, NULL);
#else
printf("Creating a Texture render target GL_RGBA8UI_EXT\n");
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8UI_EXT, size_x, size_y, 0,
GL_RGBA_INTEGER_EXT, GL_UNSIGNED_BYTE, NULL);
#endif
SDK_CHECK_ERROR_GL();
// register this texture with CUDA
checkCudaErrors(cudaGraphicsGLRegisterImage(&cuda_tex_screen_resource,
*tex_screen, GL_TEXTURE_2D,
cudaGraphicsMapFlagsReadOnly));
}
////////////////////////////////////////////////////////////////////////////////
//!
////////////////////////////////////////////////////////////////////////////////
void createTextureDst(GLuint *tex_cudaResult, unsigned int size_x,
unsigned int size_y) {
// create a texture
glGenTextures(1, tex_cudaResult);
glBindTexture(GL_TEXTURE_2D, *tex_cudaResult);
// set basic parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
#ifdef USE_TEXSUBIMAGE2D
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, size_x, size_y, 0, GL_RGBA,
GL_UNSIGNED_BYTE, NULL);
SDK_CHECK_ERROR_GL();
#else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8UI_EXT, size_x, size_y, 0,
GL_RGBA_INTEGER_EXT, GL_UNSIGNED_BYTE, NULL);
SDK_CHECK_ERROR_GL();
// register this texture with CUDA
checkCudaErrors(cudaGraphicsGLRegisterImage(
&cuda_tex_result_resource, *tex_cudaResult, GL_TEXTURE_2D,
cudaGraphicsMapFlagsWriteDiscard));
#endif
}
////////////////////////////////////////////////////////////////////////////////
//!
////////////////////////////////////////////////////////////////////////////////
void deleteTexture(GLuint *tex) {
glDeleteTextures(1, tex);
SDK_CHECK_ERROR_GL();
*tex = 0;
}
////////////////////////////////////////////////////////////////////////////////
//!
////////////////////////////////////////////////////////////////////////////////
void createDepthBuffer(GLuint *depth, unsigned int size_x,
unsigned int size_y) {
// create a renderbuffer
glGenRenderbuffersEXT(1, depth);
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, *depth);
// allocate storage
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, size_x,
size_y);
// clean up
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
SDK_CHECK_ERROR_GL();
}
////////////////////////////////////////////////////////////////////////////////
//!
////////////////////////////////////////////////////////////////////////////////
// void
// createRenderBuffer(GLuint* render, unsigned int size_x, unsigned int size_y)
//{
// // create a renderbuffer
// glGenRenderbuffersEXT(1, render);
// glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, *render);
//
// // allocate storage
// glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, size_x, size_y);
//
// // clean up
// glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
//
// SDK_CHECK_ERROR_GL();
//
// checkCudaErrors(cudaGraphicsGLRegisterImage(&cuda_tex_screen_resource,
// *render,
// GL_RENDERBUFFER_EXT, cudaGraphicsMapFlagsReadOnly));
//}
////////////////////////////////////////////////////////////////////////////////
//!
////////////////////////////////////////////////////////////////////////////////
void deleteDepthBuffer(GLuint *depth) {
glDeleteRenderbuffersEXT(1, depth);
SDK_CHECK_ERROR_GL();
*depth = 0;
}
////////////////////////////////////////////////////////////////////////////////
//!
////////////////////////////////////////////////////////////////////////////////
void createFramebuffer(GLuint *fbo, GLuint color, GLuint depth) {
// create and bind a framebuffer
glGenFramebuffersEXT(1, fbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, *fbo);
// attach images
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
GL_TEXTURE_2D, color, 0);
// glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
// GL_RENDERBUFFER_EXT, color);
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
GL_RENDERBUFFER_EXT, depth);
// clean up
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
SDK_CHECK_ERROR_GL();
}
////////////////////////////////////////////////////////////////////////////////
//!
////////////////////////////////////////////////////////////////////////////////
void deleteFramebuffer(GLuint *fbo) {
glDeleteFramebuffersEXT(1, fbo);
SDK_CHECK_ERROR_GL();
*fbo = 0;
}
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv) {
#if defined(__linux__)
char *Xstatus = getenv("DISPLAY");
if (Xstatus == NULL) {
printf("Waiving execution as X server is not running\n");
exit(EXIT_WAIVED);
}
setenv("DISPLAY", ":0", 0);
#endif
printf("%s Starting...\n\n", argv[0]);
if (checkCmdLineFlag(argc, (const char **)argv, "radius") &&
checkCmdLineFlag(argc, (const char **)argv, "file")) {
getCmdLineArgumentString(argc, (const char **)argv, "file", &ref_file);
blur_radius = getCmdLineArgumentInt(argc, (const char **)argv, "radius");
}
pArgc = &argc;
pArgv = argv;
// use command-line specified CUDA device, otherwise use device with highest
// Gflops/s
if (checkCmdLineFlag(argc, (const char **)argv, "device")) {
printf("[%s]\n", argv[0]);
printf(" Does not explicitly support -device=n\n");
printf(
" This sample requires OpenGL. Only -file=<reference> -radius=<n> "
"are supported\n");
printf("exiting...\n");
exit(EXIT_WAIVED);
}
if (ref_file) {
printf("(Test with OpenGL verification)\n");
animate = false;
runStdProgram(argc, argv);
} else {
printf("(Interactive OpenGL Demo)\n");
animate = true;
runStdProgram(argc, argv);
}
exit(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
//!
////////////////////////////////////////////////////////////////////////////////
void FreeResource() {
sdkDeleteTimer(&timer);
// unregister this buffer object with CUDA
checkCudaErrors(cudaGraphicsUnregisterResource(cuda_tex_screen_resource));
#ifdef USE_TEXSUBIMAGE2D
checkCudaErrors(cudaGraphicsUnregisterResource(cuda_pbo_dest_resource));
deletePBO(&pbo_dest);
#else
cudaFree(cuda_dest_resource);
#endif
deleteTexture(&tex_screen);
deleteTexture(&tex_cudaResult);
deleteDepthBuffer(&depth_buffer);
deleteFramebuffer(&framebuffer);
if (iGLUTWindowHandle) {
glutDestroyWindow(iGLUTWindowHandle);
}
// finalize logs and leave
printf("postProcessGL.exe Exiting...\n");
}
void Cleanup(int iExitCode) {
FreeResource();
printf("Images are %s\n",
(iExitCode == EXIT_SUCCESS) ? "Matching" : "Not Matching");
exit(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////////////
//!
////////////////////////////////////////////////////////////////////////////////
GLuint compileGLSLprogram(const char *vertex_shader_src,
const char *fragment_shader_src) {
GLuint v, f, p = 0;
p = glCreateProgram();
if (vertex_shader_src) {
v = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(v, 1, &vertex_shader_src, NULL);
glCompileShader(v);
// check if shader compiled
GLint compiled = 0;
glGetShaderiv(v, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
//#ifdef NV_REPORT_COMPILE_ERRORS
char temp[256] = "";
glGetShaderInfoLog(v, 256, NULL, temp);
printf("Vtx Compile failed:\n%s\n", temp);
//#endif
glDeleteShader(v);
return 0;
} else {
glAttachShader(p, v);
}
}
if (fragment_shader_src) {
f = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(f, 1, &fragment_shader_src, NULL);
glCompileShader(f);
// check if shader compiled
GLint compiled = 0;
glGetShaderiv(f, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
//#ifdef NV_REPORT_COMPILE_ERRORS
char temp[256] = "";
glGetShaderInfoLog(f, 256, NULL, temp);
printf("frag Compile failed:\n%s\n", temp);
//#endif
glDeleteShader(f);
return 0;
} else {
glAttachShader(p, f);
}
}
glLinkProgram(p);
int infologLength = 0;
int charsWritten = 0;
GLint linked = 0;
glGetProgramiv(p, GL_LINK_STATUS, &linked);
if (linked == 0) {
glGetProgramiv(p, GL_INFO_LOG_LENGTH, (GLint *)&infologLength);
if (infologLength > 0) {
char *infoLog = (char *)malloc(infologLength);
glGetProgramInfoLog(p, infologLength, (GLsizei *)&charsWritten, infoLog);
printf("Shader compilation error: %s\n", infoLog);
free(infoLog);
}
}
return p;
}
////////////////////////////////////////////////////////////////////////////////
//! Allocate the "render target" of CUDA
////////////////////////////////////////////////////////////////////////////////
#ifndef USE_TEXSUBIMAGE2D
void initCUDABuffers() {
// set up vertex data parameter
num_texels = image_width * image_height;
num_values = num_texels * 4;
size_tex_data = sizeof(GLubyte) * num_values;
checkCudaErrors(cudaMalloc((void **)&cuda_dest_resource, size_tex_data));
// checkCudaErrors(cudaHostAlloc((void**)&cuda_dest_resource, size_tex_data,
// ));
}
#endif
////////////////////////////////////////////////////////////////////////////////
//!
////////////////////////////////////////////////////////////////////////////////
void initGLBuffers() {
// create pbo
#ifdef USE_TEXSUBIMAGE2D
createPBO(&pbo_dest, &cuda_pbo_dest_resource);
#endif
// create texture that will receive the result of CUDA
createTextureDst(&tex_cudaResult, image_width, image_height);
// create texture for blitting onto the screen
createTextureSrc(&tex_screen, image_width, image_height);
// createRenderBuffer(&tex_screen, image_width, image_height); // Doesn't work
// create a depth buffer for offscreen rendering
createDepthBuffer(&depth_buffer, image_width, image_height);
// create a framebuffer for offscreen rendering
createFramebuffer(&framebuffer, tex_screen, depth_buffer);
// load shader programs
shDrawPot = compileGLSLprogram(NULL, glsl_drawpot_fragshader_src);
#ifndef USE_TEXSUBIMAGE2D
shDrawTex = compileGLSLprogram(glsl_drawtex_vertshader_src,
glsl_drawtex_fragshader_src);
#endif
SDK_CHECK_ERROR_GL();
}
////////////////////////////////////////////////////////////////////////////////
//! Run standard demo loop with or without GL verification
////////////////////////////////////////////////////////////////////////////////
void runStdProgram(int argc, char **argv) {
// First initialize OpenGL context, so we can properly set the GL for CUDA.
// This is necessary in order to achieve optimal performance with OpenGL/CUDA
// interop.
if (false == initGL(&argc, argv)) {
return;
}
// Now initialize CUDA context
findCudaDevice(argc, (const char **)argv);
sdkCreateTimer(&timer);
sdkResetTimer(&timer);
// register callbacks
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutTimerFunc(REFRESH_DELAY, timerEvent, 0);
// create menu
glutCreateMenu(mainMenu);
glutAddMenuEntry("Toggle CUDA Post Processing (on/off) [ ]", ' ');
glutAddMenuEntry("Toggle Animation (on/off) [a]", 'a');
glutAddMenuEntry("Increase Blur Radius [=]", '=');
glutAddMenuEntry("Decrease Blur Radius [-]", '-');
glutAddMenuEntry("Quit (esc)", '\033');
glutAttachMenu(GLUT_RIGHT_BUTTON);
initGLBuffers();
#ifndef USE_TEXSUBIMAGE2D
initCUDABuffers();
#endif
// Creating the Auto-Validation Code
if (ref_file) {
g_CheckRender = new CheckBackBuffer(window_width, window_height, 4);
g_CheckRender->setPixelFormat(GL_RGBA);
g_CheckRender->setExecPath(argv[0]);
g_CheckRender->EnableQAReadback(true);
}
printf(
"\n"
"\tControls\n"
"\t(right click mouse button for Menu)\n"
"\t[ ] : Toggle CUDA Post Processing (on/off)\n"
"\t[a] : Toggle Animation (on/off)\n"
"\t[=] : Increase Blur Radius\n"
"\t[-] : Decrease Blur Radius\n"
"\t[esc] - Quit\n\n");
// start rendering mainloop
glutMainLoop();
// Normally unused return path
Cleanup(EXIT_SUCCESS);