So I somewhat ported Dominik’s c++ code over for use with java using jogl. The thing is I’m not getting the results of 0.0. The code is very easy to follow, I know it seems like alot, however I’m hoping that others may use or build upon what I was able to do. If you look at the textureParameters variable, I have it set to the twod_arb_rgba_32 object. The shader program for it is quite simple. It is listed in its source. Thanks for anyone who can help!
Sincerely ~Bolt
PS: Some functions were omitted for some resemblance of brevity
this is just the class that is call when display is called, basically just follow the flow in the renderMain function
public class CompleteTestRenderer implements GLEventListener
{
private GLU glu = new GLU();
private GL gl;
// mode: 0=test (POT), 1=bench (set from command line)
int mode = 1;
// problem size, texture size, number of iterations (set from command line)
int N;
int texSize;
int numIterations;
// flags to fine-tune application and to ease debugging
boolean mode_showResults = true;
boolean mode_compareResults = true;
// texture identifiers
IntBuffer yTexID;
IntBuffer xTexID;
int aTexID;
// ping pong management vars
int writeTex = 0;
int readTex = 1;
int attachmentpoints[] = { GL.GL_COLOR_ATTACHMENT0_EXT, GL.GL_COLOR_ATTACHMENT1_EXT };
// GLSL vars
int glslProgram;
int fragmentShader;
int yParam, xParam, alphaParam;
// FBO identifier
int[] fb = new int[1];
// timing vars
double start, end;
// handle to offscreen "window", only used to properly shut down the app
int glutWindowHandle;
// struct for variable parts of GL calls (texture format, float format etc)
textParam rect_arb_rgba_32 = new textParam();
textParam textureParameters = new textParam();
// actual data
FloatBuffer dataX;
FloatBuffer dataY;
FloatBuffer dataY2;
float alpha;
/**
* renderMain, just calls things in the appropriate order
*/
public void renderMain(){
// create variables for GL
N = 16;
dataX = FloatBuffer.allocate(255);
dataY = FloatBuffer.allocate(255);
dataY2 = FloatBuffer.allocate(255);
createAllTextureParameters();
textureParameters = twod_arb_rgba_32;
// calc texture dimensions
if (textureParameters.texFormat == GL.GL_RGBA) texSize = (int)Math.sqrt(N/4.0);
else texSize = (int)Math.sqrt((double)N);
// fill with some arbitrary values
for (int i=0; i<N; i++) {
dataX.put(i, 1.0f);
dataY.put(i, i+1.0f);
dataY2.put(i, 0.0f);
}
alpha = 1.0f;
xTexID = IntBuffer.allocate(1);
yTexID = IntBuffer.allocate(2);
// init offscreen framebuffer
initFBO();
// create textures for vectors
createTextures();
// init shader runtime
initGLSL();
// and start computation
performComputation();
// compare results
compareResults();
// exit
return;
}
/**
* Performs and times saxpy on the CPU, compares results
*/
public void compareResults(){
// get GPU results
FloatBuffer data = null;
data = FloatBuffer.allocate(N);
transferFromTexture (data);
if (mode_compareResults) {
// calc on CPU
start = System.nanoTime();
for (int n=0; n<numIterations; n++)
for (int i=0; i<N; i++)
dataY.put(i, (dataY.get(i) + alpha*dataX.get(i)));
end = System.nanoTime();
double total = (end-start)/1000;
double mflops = (2.0*N*numIterations) / (total * 1000000.0);
System.out.println("CPU TIME:"+mflops);
// and compare results
double maxError = -1000.0;
double avgError = 0.0;
for (int i=0; i<N; i++) {
double diff = Math.abs((data.get(i)-dataY.get(i)));
if (diff > maxError)
maxError = diff;
avgError += diff;
}
avgError /= (double)N;
System.out.println("Max Error: "+maxError);
System.out.println("Avg Error: "+avgError);
if (mode_showResults) {
System.out.println("CPU RESULTS:\n");
printVector(dataY,N);
}
}
if (mode_showResults) {
// print out results
System.out.println("GPU RESULTS:\n");
printVector (data,N);
}
}
/**
* creates textures, sets proper viewport etc.
*/
public void createTextures(){
// create textures
// y gets two textures, alternatingly read-only and write-only,
// x is just read-only
gl.glGenTextures (2, yTexID);
gl.glGenTextures (1, xTexID);
// set up textures
setupTexture (yTexID.get(readTex), dataY);
transferToTexture(dataY,yTexID.get(readTex));
setupTexture (yTexID.get(writeTex), dataY2);
transferToTexture(dataY2,yTexID.get(writeTex));
setupTexture (xTexID.get(0), dataX);
transferToTexture(dataX,xTexID.get(0));
// set texenv mode from modulate (the default) to replace
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
// check if something went completely wrong
checkGLErrors ("createFBOandTextures()");
}