www.pudn.com > GPUVision_5-13-05-2.zip > ConjGrad_back.cpp
#include "RenderTexture.h" #include#include #include #include #include #include #include #define USE_CG mud #ifdef USE_CG #include #endif #define FILE_MODE 1 #define RANDOM_MODE !FILE_MODE #define RAND_RANGE 10 #define MUL_MODE true #define ADD_MODE !MUL_MODE // ping pong macros const GLenum glsurf[2] = {GL_FRONT_LEFT, GL_BACK_LEFT}; const GLenum wglsurf[2] = {WGL_FRONT_LEFT_ARB, WGL_BACK_LEFT_ARB}; int SOURCE_BUFFER = 0; #define DESTINATION_BUFFER !SOURCE_BUFFER #define SWAP() SOURCE_BUFFER = DESTINATION_BUFFER; // Global variables: hold the Cg context that we're storing our programs // in as well as handles to the vertex and fragment program used in this // demo. GLfloat* data_x, * data_a_nd, *data_a_d, * data_r, *data_c, *data_b; RenderTexture *rt = NULL; int texHeight=500, texWidth=1000; bool calMode; int m =160000, n =160000; //400x400 image int density=5; //Max No of elements in a row //const char *modeString ="rgba=32f tex2D"; const char *modeString ="rgb=32f doublebuffer texRECT"; CGprofile cgProfile; CGcontext cgContext; CGprogram fragmentMulMtxVecProgram ,fragmentDotVecVecProgram ,fragmentMinusVecVecProgram ,fragmentPlusVecVecProgram ,fragmentMulScalarVecProgram ,fragmentDivScalarScalarProgram; CGparameter textureXParam, textureARCParam, textureWidthParam ,textureDVVAParam, textureDVVBParam ,textureMVVAParam, textureMVVBParam ,texturePVVAParam, texturePVVBParam ,textureMSVAParam, textureMSVBParam ,textureDSSAParam, textureDSSBParam; int texIDx, texIDarc, texIDb; void Reshape(int w, int h); void printOutMtx(float* mtx, int m, int n); bool checkValidFile(int n_temp); void uploadTexture (); unsigned int createTexture(float *data, int width, int height, unsigned int inputDataFormat); unsigned int createTexture(float *data); void preProcessing(); void loadCGFragmentProgram(); RenderTexture* createFloatBuffer(); void InitCG(); //--------------------------------------------------------------------------- // Function : PrintGLerror // Description : //--------------------------------------------------------------------------- void PrintGLerror( char *msg ) { GLenum errCode; const GLubyte *errStr; if ((errCode = glGetError()) != GL_NO_ERROR) { errStr = gluErrorString(errCode); fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg); } } //--------------------------------------------------------------------------- // Function : cgErrorCallback // Description : //--------------------------------------------------------------------------- void cgErrorCallback() { CGerror lastError = cgGetError(); if(lastError) { printf("%s\n\n", cgGetErrorString(lastError)); printf("%s\n", cgGetLastListing(cgContext)); printf("Cg error, exiting...\n"); exit(0); } } //--------------------------------------------------------------------------- // Function : Keyboard // Description : //--------------------------------------------------------------------------- void Keyboard(unsigned char key, int x, int y) { if (key == 'q' || key == 'Q' || key == 27 /* escape */) { cgDestroyContext(cgContext); exit(0); } } void DestroyRenderTexture(RenderTexture *rt2) { if (rt2->IsFloatTexture()) { cgDestroyProgram(fragmentMulMtxVecProgram); cgDestroyProgram(fragmentDotVecVecProgram); cgDestroyProgram(fragmentMinusVecVecProgram); cgDestroyProgram(fragmentPlusVecVecProgram); cgDestroyProgram(fragmentMulScalarVecProgram); cgDestroyProgram(fragmentDivScalarScalarProgram); cgDestroyContext(cgContext); } delete rt2; } //--------------------------------------------------------------------------- // Function : Idle // Description : //--------------------------------------------------------------------------- void Idle() { // make sure we don't try to display nonexistent textures } //--------------------------------------------------------------------------- // Function : Reshape // Description : //--------------------------------------------------------------------------- void Reshape(int w, int h) { if (h==0) h=1; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1,1,-1,1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } GLfloat* interpreteResult (GLfloat *colBuffer) { // check if N ist multiple of 4 (yeah, wasting a lot of memory here) // allocate memory GLfloat* ret = (GLfloat*)malloc(m*sizeof(GLfloat)); // and fill it with some arbitrary nonsense values int i; for (i=0; i BeginCapture(); glReadBuffer(target); glReadPixels(0, 0, texWidth, texHeight,GL_RGB,GL_FLOAT,data); rt->EndCapture(); return data; } void doPostprocessing (void) { GLfloat *data; // get GPU results data = interpreteResult( downloadTexture (glsurf[SOURCE_BUFFER])); // and print them both out for visual comparison :-) printf("Result Matrix\n"); //printOutMtx(data, m, n); printOutMtx(data, 10, 10); delete data; // ok done, kill yourself (but let the user read the output first) system("pause"); exit(0); } void printTiming (const double start, const double end, const char* label) { double total = (end-start) / CLOCKS_PER_SEC*1000; printf("%s: %5.5f msecs\n", label, total); } //--------------------------------------------------------------------------- // Function : Display // Description : //--------------------------------------------------------------------------- void display() { double start, end; start = clock(); // get RT rt->BeginCapture(); // bind Cg program cgGLBindProgram(fragmentMulMtxVecProgram); cgGLEnableProfile(cgProfile); // tell the shader to use the right texture cgGLSetTextureParameter(textureXParam, texIDx); cgGLSetTextureParameter(textureARCParam, texIDarc); cgGLSetParameter1f(textureWidthParam, texWidth); cgGLEnableTextureParameter(textureXParam); cgGLEnableTextureParameter(textureARCParam); glDrawBuffer(glsurf[DESTINATION_BUFFER]); rt->BindBuffer(wglsurf[SOURCE_BUFFER]); glBegin(GL_QUADS); glTexCoord2f(-texWidth,-texHeight); glVertex2f(-texWidth, -texHeight); glTexCoord2f(texWidth, -texHeight); glVertex2f(texWidth, -texHeight); glTexCoord2f(texWidth, texHeight); glVertex2f(texWidth, texHeight); glTexCoord2f(-texWidth, texHeight); glVertex2f(-texWidth, texHeight); glEnd(); SWAP(); /**MINUS**/ cgGLBindProgram(fragmentMinusVecVecProgram); cgGLEnableProfile(cgProfile); // tell the shader to use the right texture cgGLSetTextureParameter(textureMVVAParam, texIDb); cgGLSetTextureParameter(textureMVVBParam, rt->GetTextureID()); cgGLEnableTextureParameter(textureMVVAParam); cgGLEnableTextureParameter(textureMVVBParam); glDrawBuffer(glsurf[DESTINATION_BUFFER]); rt->BindBuffer(wglsurf[SOURCE_BUFFER]); glBegin(GL_QUADS); glTexCoord2f(-texWidth,-texHeight); glVertex2f(-texWidth, -texHeight); glTexCoord2f(texWidth, -texHeight); glVertex2f(texWidth, -texHeight); glTexCoord2f(texWidth, texHeight); glVertex2f(texWidth, texHeight); glTexCoord2f(-texWidth, texHeight); glVertex2f(-texWidth, texHeight); glEnd(); SWAP(); /**/ rt->EndCapture(); cgGLDisableProfile(cgProfile); end = clock(); printTiming(start, end, "GPU time"); doPostprocessing(); //glPostResdisplay(); } void initGLUT() { glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); glutInitWindowPosition(50,50); glutInitWindowSize(512, 512); glutCreateWindow("MulSparseMtx"); glutDisplayFunc(display); glutIdleFunc(Idle); glutReshapeFunc(Reshape); glutKeyboardFunc(Keyboard); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glEnable(GL_TEXTURE_RECTANGLE_NV); } /* * utility function called by createFloatBuffer() */ RenderTexture* createFloatBuffer() { // create new RT RenderTexture *rt = new RenderTexture(); // set it up to suit our needs rt->Reset(modeString); // and initialize it to preferred size if (!rt->Initialize(texWidth,texHeight)) { printf("ERROR::when create RT\n"); exit(1); } // setup RT rt->BeginCapture(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, texWidth, 0.0, texHeight); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0, 0, texWidth, texHeight); glBindTexture(rt->GetTextureTarget(), rt->GetTextureID()); glTexParameteri(rt->GetTextureTarget(), GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(rt->GetTextureTarget(), GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(rt->GetTextureTarget(), GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(rt->GetTextureTarget(), GL_TEXTURE_WRAP_T, GL_CLAMP); rt->EndCapture(); return rt; } unsigned int createTexture(float *data) { return createTexture(data,texWidth, texHeight, GL_RGBA); } unsigned int createTexture(float *data, int width, int height, unsigned int inputDataFormat) { unsigned int textureID; glGenTextures(1,&textureID); glDisable(GL_TEXTURE_2D); glPixelStorei(GL_UNPACK_ALIGNMENT, 4); glBindTexture(GL_TEXTURE_RECTANGLE_NV, textureID); glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_RECTANGLE_NV, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); //if(_mipmapped) { if(0) { gluBuild2DMipmaps(GL_TEXTURE_RECTANGLE_NV, GL_FLOAT_RGBA32_NV, width, height, inputDataFormat, GL_FLOAT, data); } else { glTexImage2D(GL_TEXTURE_RECTANGLE_NV,0, GL_FLOAT_RGBA32_NV, width, height, 0, inputDataFormat, GL_FLOAT, data); } return textureID; } void initGLEW (void) { int err = glewInit(); if (GLEW_OK != err) { exit(1); } } /* * creates y,x and a vectors and stores them in the RGB channels * of a newly created texture. Does not do any openGL stuff! */ void createTextureArray (GLfloat *tex_x, GLfloat *tex_b, GLfloat *tex_arc) { int idx; for (int i=0; i