00001
00002
00003
00004
00005
00006
00007
00008 #include "GPUGlobals.h"
00009
00010 #ifdef QMC_GPU
00011
00012 void GPUGlobals::getOpenGLError(char *msg)
00013 {
00014 static char error_txt[][32] = {
00015 "GL_INVALID_ENUM",
00016 "GL_INVALID_VALUE",
00017 "GL_INVALID_OPERATION",
00018 "GL_STACK_OVERFLOW",
00019 "GL_STACK_UNDERFLOW",
00020 "GL_OUT_OF_MEMORY" };
00021 GLenum e = glGetError();
00022 if (e != GL_NO_ERROR)
00023 {
00024 cerr << "Error: OpenGL error " << error_txt[e-GL_INVALID_ENUM] << endl;
00025 if(msg != NULL) cerr << "Message: " << msg << endl;
00026 }
00027 }
00028
00029 void GPUGlobals::PrintRGBAPixelsBoxF(float* pix, int w, int h)
00030 {
00031 int index;
00032 int maxJ = 30;
00033 for (int i = 0; i < h; i++)
00034 {
00035 for (int j = 0; j < w && j < maxJ; j++)
00036 {
00037 index = 4*(i*w + j);
00038 printf("%6.3f",pix[index +0]);
00039 printf("%6.3f",pix[index +1]);
00040 printf(" ");
00041 }
00042 printf("\n");
00043 for (int j = 0; j < w && j < maxJ; j++)
00044 {
00045 index = 4*(i*w + j);
00046 printf("%6.3f",pix[index +2]);
00047 printf("%6.3f",pix[index +3]);
00048 printf(" ");
00049 }
00050 printf("\n\n");
00051 }
00052 printf("\n");
00053 }
00054
00055 void GPUGlobals::PrintRGBAPixelsBoxE(float* pix, int w, int h,
00056 int wBorder, int hBorder,
00057 int maxJ, int maxI,
00058 bool printRC)
00059 {
00060 int index;
00061
00062
00063
00064
00065 if(maxJ < 0 || maxJ > w) maxJ = w;
00066 if(maxI < 0 || maxI > h) maxI = h;
00067
00068 if(wBorder > w) wBorder = -1;
00069 if(hBorder > h) hBorder = -1;
00070
00071 int wAdjuster = 0;
00072 if(wBorder < 0 && maxJ%2 == 1)
00073 wAdjuster = 1;
00074 int hAdjuster = 0;
00075 if(hBorder < 0 && maxI%2 == 1)
00076 hAdjuster = 1;
00077
00078 if(wBorder < 0 || wBorder*2 > maxJ) wBorder = (int)(maxJ/2.0);
00079 if(hBorder < 0 || hBorder*2 > maxI) hBorder = (int)(maxI/2.0);
00080
00081 const char * form = "%11.3e";
00082
00083
00084 if(printRC)
00085 {
00086 printf("%16i",0);
00087 for (int j=1; j < wBorder + wAdjuster; j++)
00088 printf("%25i",j);
00089 for (int j=w-wBorder; j < w; j++)
00090 printf("%25i",j);
00091 printf("\n\n");
00092 }
00093
00094
00095 for (int i = 0; i < hBorder + hAdjuster; i++)
00096 {
00097 if(printRC)
00098 {
00099 printf("%-4i",i);
00100 }
00101
00102
00103 for (int j = 0; j < wBorder + wAdjuster; j++)
00104 {
00105 index = 4*(i*w + j);
00106 printf(form,pix[index +0]);
00107 printf(form,pix[index +1]);
00108 printf(" ");
00109 }
00110
00111 for (int j = w-wBorder; j < w; j++)
00112 {
00113 index = 4*(i*w + j);
00114 printf(form,pix[index +0]);
00115 printf(form,pix[index +1]);
00116 printf(" ");
00117 }
00118
00119 printf("\n");
00120 if(printRC)
00121 {
00122 printf(" ");
00123 }
00124
00125
00126
00127 for (int j = 0; j < wBorder + wAdjuster; j++)
00128 {
00129 index = 4*(i*w + j);
00130 printf(form,pix[index +2]);
00131 printf(form,pix[index +3]);
00132 printf(" ");
00133 }
00134
00135 for (int j = w-wBorder; j < w; j++)
00136 {
00137 index = 4*(i*w + j);
00138 printf(form,pix[index +2]);
00139 printf(form,pix[index +3]);
00140 printf(" ");
00141 }
00142
00143
00144 printf("\n\n");
00145 }
00146
00147
00148 for (int i = h-hBorder; i < h; i++)
00149 {
00150 if(printRC)
00151 {
00152 printf("%-4i",i);
00153 }
00154
00155
00156 for (int j = 0; j < wBorder + wAdjuster; j++)
00157 {
00158 index = 4*(i*w + j);
00159 printf(form,pix[index +0]);
00160 printf(form,pix[index +1]);
00161 printf(" ");
00162 }
00163
00164 for (int j = w-wBorder; j < w; j++)
00165 {
00166 index = 4*(i*w + j);
00167 printf(form,pix[index +0]);
00168 printf(form,pix[index +1]);
00169 printf(" ");
00170 }
00171
00172 printf("\n");
00173 if(printRC)
00174 {
00175 printf(" ");
00176 }
00177
00178
00179
00180 for (int j = 0; j < wBorder + wAdjuster; j++)
00181 {
00182 index = 4*(i*w + j);
00183 printf(form,pix[index +2]);
00184 printf(form,pix[index +3]);
00185 printf(" ");
00186 }
00187
00188 for (int j = w-wBorder; j < w; j++)
00189 {
00190 index = 4*(i*w + j);
00191 printf(form,pix[index +2]);
00192 printf(form,pix[index +3]);
00193 printf(" ");
00194 }
00195
00196
00197 printf("\n\n");
00198 }
00199
00200 printf("\n");
00201
00202 }
00203
00204 void GPUGlobals::PrintRGBAPixelsColumn(float* pix, int w, int h)
00205 {
00206 for (int i = 0; i < 4*h; i++)
00207 {
00208 for (int j = 0; j < w && j < 26; j++)
00209 {
00210 printf("%7.3f",pix[4*((h-(i/4)-1)*w + j)+i%4]);
00211
00212
00213 }
00214 printf("\n");
00215 if(i%4==3) printf("\n");
00216 }
00217 printf("\n");
00218 }
00219
00220 void GPUGlobals::PrintRGBPixelsColumn(float* pix, int w, int h)
00221 {
00222 int maxJ = 26;
00223 for (int i = 0; i < 3*h; i++)
00224 {
00225 for (int j = 0; j < w && j < maxJ; j++)
00226 {
00227 printf("%7.3g",pix[3*(( (i/3) )*w + j)+i%3]);
00228
00229
00230 }
00231 printf("\n");
00232 if(i%3==2) printf("\n");
00233 }
00234 printf("\n");
00235 }
00236
00237 void GPUGlobals::PrintMatrix(Array2D<GLfloat> matrix)
00238 {
00239 for(int i=0; i<matrix.dim1(); i++)
00240 {
00241 for(int j=0; j<matrix.dim2() && j < 28; j++)
00242 {
00243 printf("%7.3g", (float)matrix(i,j));
00244 }
00245 printf("\n");
00246 }
00247 printf("\n");
00248 }
00249
00250 void GPUGlobals::getFactors(int num, int & fact1, int & fact2)
00251 {
00252
00253 int max = (int) sqrt( (double)num );
00254 fact1 = num;
00255 fact2 = 1;
00256
00257 for(int i=2; i<=max; i++)
00258 if( num%i == 0 )
00259 {
00260 fact1 = num/i;
00261 fact2 = i;
00262 }
00263 }
00264
00265 const char * GPUGlobals::gpubench_getDriverVersion(void)
00266 {
00267 int infosize;
00268 void *info;
00269 int status;
00270 int vsinfosize;
00271 char *desc;
00272 LPVOID version=NULL;
00273 DWORD vLen,langD;
00274 static char fileVersion[256];
00275 static char returnValue[256];
00276 char *p;
00277
00278 static char nvdriver[] = "c:\\windows\\system32\\nv4_disp.dll";
00279 static char atidriver[] = "c:\\windows\\system32\\atioglxx.dll";
00280 static char driver3dl[] = "c:\\windows\\system32\\3Dl2DD.dll";
00281
00282 char *filename;
00283
00284 if (gpu_make == ati_gpu) filename = atidriver;
00285 else if (gpu_make == nvidia_gpu) filename = nvdriver;
00286 else if (gpu_make == labs3d_gpu) filename = driver3dl;
00287 else return returnValue;
00288
00289
00290
00291 infosize = GetFileVersionInfoSize( filename, (LPDWORD) &infosize );
00292 if (!infosize) return "";
00293
00294 info = malloc (infosize);
00295
00296 status = GetFileVersionInfo(filename, 0, infosize, info);
00297 if (!status)
00298 {
00299 free(info); return "";
00300 }
00301
00302 status = VerQueryValue ( info, TEXT("\\VarFileInfo\\Translation"),
00303 (LPVOID *) &version, (UINT *)&vLen);
00304 if (!status)
00305 {
00306 free(info); return "";
00307 }
00308
00309 memcpy(&langD,version,4);
00310 sprintf(fileVersion, "\\StringFileInfo\\%02X%02X%02X%02X\\FileVersion",
00311 (langD & 0xff00)>>8,langD & 0xff,(langD & 0xff000000)>>24,
00312 (langD & 0xff0000)>>16);
00313
00314 if (VerQueryValue(info, fileVersion, (LPVOID *)&desc, (PUINT) &vsinfosize))
00315 {
00316 p = desc;
00317 if (gpu_make != labs3d_gpu)
00318 {
00319 for ( ; *p && *p != '.'; p++);
00320 for (p++; *p && *p != '.'; p++);
00321 for (p++; *p && *p != '.'; p++);
00322 p++;
00323 }
00324 else
00325 {
00326 while (*p != '\0' && *p != ' ') p++;
00327 if (*p == ' ') *p = '\0';
00328 p = desc;
00329 }
00330 }
00331 else
00332 {
00333 p = "";
00334 }
00335 strncpy(returnValue, p, sizeof returnValue);
00336 returnValue[sizeof returnValue - 1] = '\0';
00337
00338 free (info);
00339 return returnValue;
00340 }
00341
00342 void GPUGlobals::printVersions(ostream & strm)
00343 {
00344 const char* vendor = (const char *) glGetString(GL_VENDOR);
00345 const char* render = (const char *) glGetString(GL_RENDERER);
00346 printf("%s\n", render);
00347
00348 if (vendor == NULL)
00349 {
00350 cout << "NULL vendor string.\n";
00351 }
00352
00353 if (strstr(vendor, "NVIDIA") != NULL)
00354 {
00355 cout << "nVidia";
00356 gpu_make = nvidia_gpu;
00357 }
00358 else if (strstr(vendor, "ATI") != NULL)
00359 {
00360 cout << "ATI";
00361 gpu_make = ati_gpu;
00362 }
00363 else if (strstr(vendor, "3Dlabs") != NULL)
00364 {
00365 cout << "3Dlabs";
00366 gpu_make = labs3d_gpu;
00367 }
00368 else
00369 {
00370 printf("Device Unknown: '%s'\n", vendor);
00371 gpu_make = unknown_gpu;
00372 }
00373
00374 strm << " " << gpubench_getDriverVersion() << ", Cg Version: " << CG_VERSION_NUM;
00375 if(g_cgProfile == CG_PROFILE_FP40) strm << ", Using fp40\n";
00376 if(g_cgProfile == CG_PROFILE_FP30) strm << ", Using fp30\n";
00377 if(g_cgProfile == CG_PROFILE_FP20) strm << ", Using fp20\n";
00378 }
00379
00380 void GPUGlobals::writeShader(const char * shader, const char * filename)
00381 {
00382 ofstream shaderFile(filename);
00383 if(shaderFile.is_open())
00384 {
00385 shaderFile << shader;
00386 shaderFile.close();
00387 }
00388 else
00389 {
00390 cerr << "ERROR: " << filename << " failed to open\n";
00391 }
00392 }
00393
00394 void GPUGlobals::checkExtensions()
00395 {
00396 bool ok = true;
00397
00398 if(!GL_ARB_fragment_program)
00399 {
00400 cout << "GL_FRAGMENT_PROGRAM_ARB not supported" << endl;
00401 ok = false;
00402 }
00403
00404 if(!GL_NV_fragment_program)
00405 {
00406 cout << "GL_NV_fragment_program not supported" << endl;
00407 ok = false;
00408 }
00409
00410 if(!GL_NV_fragment_program2)
00411 {
00412 cout << "GL_NV_fragment_program2 not supported" << endl;
00413 ok = false;
00414 }
00415
00416 if(!GL_NV_float_buffer)
00417 {
00418 cout << "GL_NV_float_buffer not supported" << endl;
00419 ok = false;
00420 }
00421
00422 if(!GL_EXT_framebuffer_object)
00423 {
00424 cout << "GL_EXT_framebuffer_object not supported" << endl;
00425 ok = false;
00426 }
00427
00428 if(!GL_ARB_texture_rectangle)
00429 {
00430 cout << "ARB_texture_rectangle not supported" << endl;
00431 ok = false;
00432 }
00433
00434 if(!ok)
00435 {
00436 cerr << "ERROR: required extensions are not supported, exiting.\n";
00437 exit(-1);
00438 }
00439 }
00440
00441
00442 void GPUGlobals::specTest()
00443 {
00444
00445
00446 GLenum target = GL_FRAGMENT_PROGRAM_ARB;
00447 GLint num, nativeNum;
00448 glGetProgramivARB( target, GL_PROGRAM_INSTRUCTIONS_ARB, &num );
00449 glGetProgramivARB( target, GL_PROGRAM_NATIVE_INSTRUCTIONS_ARB, &nativeNum );
00450 printf("%-40s is (num, native): %2i, %2i\n", "GL_PROGRAM_INSTRUCTIONS_ARB",num,nativeNum);
00451 glGetProgramivARB( target, GL_PROGRAM_TEMPORARIES_ARB, &num );
00452 glGetProgramivARB( target, GL_PROGRAM_NATIVE_TEMPORARIES_ARB, &nativeNum );
00453 printf("%-40s is (num, native): %2i, %2i\n", "GL_PROGRAM_TEMPORARIES_ARB",num,nativeNum);
00454 glGetProgramivARB( target, GL_PROGRAM_PARAMETERS_ARB, &num );
00455 glGetProgramivARB( target, GL_PROGRAM_NATIVE_PARAMETERS_ARB, &nativeNum );
00456 printf("%-40s is (num, native): %2i, %2i\n", "GL_PROGRAM_PARAMETERS_ARB",num,nativeNum);
00457 glGetProgramivARB( target, GL_PROGRAM_ATTRIBS_ARB, &num );
00458 glGetProgramivARB( target, GL_PROGRAM_NATIVE_ATTRIBS_ARB, &nativeNum );
00459 printf("%-40s is (num, native): %2i, %2i\n", "GL_PROGRAM_ATTRIBS_ARB",num,nativeNum);
00460 glGetProgramivARB( target, GL_PROGRAM_ALU_INSTRUCTIONS_ARB, &num );
00461 glGetProgramivARB( target, GL_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, &nativeNum );
00462 printf("%-40s is (num, native): %2i, %2i\n", "GL_PROGRAM_ALU_INSTRUCTIONS_ARB",num,nativeNum);
00463 glGetProgramivARB( target, GL_PROGRAM_TEX_INSTRUCTIONS_ARB, &num );
00464 glGetProgramivARB( target, GL_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB, &nativeNum );
00465 printf("%-40s is (num, native): %2i, %2i\n", "GL_PROGRAM_TEX_INSTRUCTIONS_ARB",num,nativeNum);
00466 cout << endl;
00467
00468 glGetProgramivARB( target, GL_MAX_PROGRAM_INSTRUCTIONS_ARB, &num );
00469 glGetProgramivARB( target, GL_MAX_PROGRAM_NATIVE_INSTRUCTIONS_ARB, &nativeNum );
00470 printf("%-40s is (num, native): %2i, %2i\n", "GL_MAX_PROGRAM_INSTRUCTIONS_ARB",num,nativeNum);
00471 glGetProgramivARB( target, GL_MAX_PROGRAM_TEMPORARIES_ARB, &num );
00472 glGetProgramivARB( target, GL_MAX_PROGRAM_NATIVE_TEMPORARIES_ARB, &nativeNum );
00473 printf("%-40s is (num, native): %2i, %2i\n", "GL_MAX_PROGRAM_TEMPORARIES_ARB",num,nativeNum);
00474 glGetProgramivARB( target, GL_MAX_PROGRAM_PARAMETERS_ARB, &num );
00475 glGetProgramivARB( target, GL_MAX_PROGRAM_NATIVE_PARAMETERS_ARB, &nativeNum );
00476 printf("%-40s is (num, native): %2i, %2i\n", "GL_MAX_PROGRAM_PARAMETERS_ARB",num,nativeNum);
00477 glGetProgramivARB( target, GL_MAX_PROGRAM_ATTRIBS_ARB, &num );
00478 glGetProgramivARB( target, GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB, &nativeNum );
00479 printf("%-40s is (num, native): %2i, %2i\n", "GL_MAX_PROGRAM_ATTRIBS_ARB",num,nativeNum);
00480 glGetProgramivARB( target, GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB, &num );
00481 glGetProgramivARB( target, GL_MAX_PROGRAM_NATIVE_ALU_INSTRUCTIONS_ARB, &nativeNum );
00482 printf("%-40s is (num, native): %2i, %2i\n", "GL_MAX_PROGRAM_ALU_INSTRUCTIONS_ARB",num,nativeNum);
00483 glGetProgramivARB( target, GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB, &num );
00484 glGetProgramivARB( target, GL_MAX_PROGRAM_NATIVE_TEX_INSTRUCTIONS_ARB, &nativeNum );
00485 printf("%-40s is (num, native): %2i, %2i\n", "GL_MAX_PROGRAM_TEX_INSTRUCTIONS_ARB",num,nativeNum);
00486 glGetProgramivARB(target, GL_MAX_PROGRAM_LOOP_COUNT_NV, &num);
00487 printf("%-40s is: %2i\n", "GL_MAX_PROGRAM_LOOP_COUNT_NV",num);
00488 glGetProgramivARB(target, GL_MAX_PROGRAM_LOOP_DEPTH_NV, &num);
00489 printf("%-40s is: %2i\n", "GL_MAX_PROGRAM_LOOP_DEPTH_NV",num);
00490 glGetProgramivARB(target, GL_MAX_PROGRAM_IF_DEPTH_NV, &num);
00491 printf("%-40s is: %2i\n", "GL_MAX_PROGRAM_IF_DEPTH_NV",num);
00492
00493 getOpenGLError("Error in glGetProgramivARB");
00494 cout << endl;
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504 }
00505
00506 #endif
00507