// includes #include #include #include #include #include #include #include // error codes #define ERROR_GLSL -1 #define ERROR_GLEW -2 #define ERROR_TEXTURE -3 #define ERROR_BINDFBO -4 #define ERROR_FBOTEXTURE -5 #define ERROR_PARAMS -6 // prototypes bool checkFramebufferStatus(void); void checkGLErrors(const char *label); void printResults(void); void createTextures(void); void createAllTextureParameters(void); void initGLSL(void); void initFBO(void); void initGLEW(void); void initGLUT(int argc, char** argv); void performComputation(void); void printProgramInfoLog(GLuint obj); void printShaderInfoLog(GLuint obj); void printVector(const float *p, const int N); void setupTexture(const GLuint texID); void swap(void); void transferFromTexture(float* data); void transferToTexture(float* data, GLuint texID); // problem size, texture size, number of iterations (set from command line) int N; int texSize; // texture identifiers GLuint yTexID[2]; GLuint xTexID; GLuint aTexID; // ping pong management vars int writeTex = 0; int readTex = 1; GLenum attachmentpoints[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT }; // GLSL vars GLuint glslProgram; GLuint fragmentShader; GLint yParam, xParam, alphaParam; // FBO identifier GLuint fb; // timing vars double start, end; // handle to offscreen "window", only used to properly shut down the app GLuint glutWindowHandle; // struct for variable parts of GL calls (texture format, float format etc) struct struct_textureParameters { char* name; GLenum texTarget; GLenum texInternalFormat; GLenum texFormat; char* shader_source; } rect_arb_rgba_32, // texture rectangles, texture_float_ARB, RGBA, 32 bits twod_arb_rgba_32; // texture 2ds, texture_float_ARB, RGBA, 32 bits // struct actually being used (set from command line) struct_textureParameters textureParameters; // actual data float* dataX; float* dataY; /** * main, just calls things in the appropriate order */ int main(int argc, char **argv) { if(argc < 2) { printf("USAGE: glsl \n"); } else { N = atoi (argv[1]); // create variables for GL createAllTextureParameters(); textureParameters = rect_arb_rgba_32; // calc texture dimensions if (textureParameters.texFormat == GL_RGBA) texSize = (int)sqrt(N/4.0); else texSize = (int)sqrt((double)N); // create data vectors dataX = (float*)malloc(N*sizeof(float)); dataY = (float*)malloc(N*sizeof(float)); // and fill with some arbitrary values for (int i=0; i 1) { infoLog = (char *)malloc(infologLength); glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog); printf(infoLog); printf("\n"); free(infoLog); } } void printShaderInfoLog(GLuint obj) { int infologLength = 0; int charsWritten = 0; char *infoLog; glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &infologLength); if (infologLength > 1) { infoLog = (char *)malloc(infologLength); glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog); printf(infoLog); printf("\n"); free(infoLog); } } /** * Prints out given vector for debugging purposes. */ void printVector (const float *p, const int N) { for (int i=0; i