So I am using JOGL to try and run some code written in Java faster, however the code actually takes longer while using the GPU versus just using the CPU. I arranged my code so that alot of stuff occurs only once such that when I get new data all i have to do is update the textures and then render. Anyhow, without posting too much code here is one of the shaders I’m using… If you want to see more code I can post the GLEventlistener that I’m using, but for now i’ll start with this…
As you can guess, this function peforms a dotproduct on some data…
public void calcDotProd(int index1, int index2)
{
String[] shaderSource = {"uniform sampler2D instance1;" +
"uniform sampler2D instance2;" +
"uniform sampler2D numValues;" +
"void main(void) { " +
" vec4 values1 = texture2D(instance1, gl_TexCoord[0].st);" +
" vec4 values2 = texture2D(instance2, gl_TexCoord[0].st);" +
" gl_FragColor[0] = dot(values1,values2);" +
"}" };
// create the arraylist that holds all the needed data
ArrayList<String> shaderVars = new ArrayList<String>(4);
ArrayList<SerialFloatBuffer> data = new ArrayList<SerialFloatBuffer>(4);
SerialFloatBuffer tempB = new SerialFloatBuffer(this.internalData.get(index1).capacity());
float[] tempF = new float[1];
tempF[0] = internalData.get(index1).capacity();
tempB.setBuffer(FloatBuffer.wrap(tempF));
data.add(0,internalData.get(index1));
data.add(1,internalData.get(index2));
data.add(2,tempB);
shaderVars.add(0,"");
shaderVars.add(1,"instance1");
shaderVars.add(2,"instance2");
shaderVars.add(3,"numValues");
// create the core
if ((currentCore == null) || (currentCore.coreRenderer == null) || (currentCore.coreRenderer.getRenderer().firstTime))
{
currentCore = new Core(shaderSource, data, shaderVars, true);
dotProdResult.setBuffer( currentCore.execute().getBuffer());
currentCore.coreRenderer.getRenderer().firstTime = false;
}
else
{
currentCore.addData(data, true);
dotProdResult.setBuffer( currentCore.execute().getBuffer());
}
return;
}