3

I'm working with Aparapi and I cant return a piece of data that I want. I have discovered that the Aparapi works similar to that of a thread.

    private int calculateBlockLightLevel(final int x,final int y,final int z) {
    int blockType = world.getBlockTypeAt(x, z, y);

    //TODO Work out how to edit this, aka not Final!
    final int found[] = new int[]{0};
    Kernel kernel = new Kernel(){
        @Override public void run() {
            int blockLightLevel = getBlockLightLevelAt(x, y + 1, z);
            int highestSurroundingBlockLight = blockLightLevel;

            found[0]=highestSurroundingBlockLight;

            blockLightLevel = getBlockLightLevelAt(x - 1, y, z);
            if (blockLightLevel > highestSurroundingBlockLight) {
                highestSurroundingBlockLight = blockLightLevel;
                found[0]=highestSurroundingBlockLight;
            }
            blockLightLevel = getBlockLightLevelAt(x + 1, y, z);
            if (blockLightLevel > highestSurroundingBlockLight) {
                highestSurroundingBlockLight = blockLightLevel;
                found[0]=highestSurroundingBlockLight;
            }
            blockLightLevel = getBlockLightLevelAt(x, y, z - 1);
            if (blockLightLevel > highestSurroundingBlockLight) {
                highestSurroundingBlockLight = blockLightLevel;
                found[0]=highestSurroundingBlockLight;
            }
            blockLightLevel = getBlockLightLevelAt(x, y, z + 1);
            if (blockLightLevel > highestSurroundingBlockLight) {
                highestSurroundingBlockLight = blockLightLevel;
                found[0]=highestSurroundingBlockLight;
            }
            blockLightLevel = getBlockLightLevelAt(x, y - 1, z);
            if (blockLightLevel > highestSurroundingBlockLight) {
                highestSurroundingBlockLight = blockLightLevel;
                found[0]=highestSurroundingBlockLight;
            }

        }
     };
     kernel.execute(1);

     kernel.dispose();

    return Math.max(found[0] - Math.max(BLOCK_TRANSPARENCY.get(blockType), 1), 0);

}

From this code I am trying to return the found to my return.

Kai
  • 38,985
  • 14
  • 88
  • 103
Haydn Trigg
  • 105
  • 1
  • 6
  • Once you have executed your kernel, the result will be in found. The found[] array will be passed to the GPU and will be copied back once execution is complete. Think of the Kernel just like another anonymous inner class that can mutate array content from the enclosed scope. Aparapi follows the same model. – gfrost Apr 25 '12 at 18:32
  • I forgot to add. Just because the array is final, does not mean that it's contents can't be changed. The found[] array has to be final for the anonymous class to 'capture' it in the run() method. We cannot change found as an array reference (can't have found = new int[]{...}!) but we can assign to the elements of found[] from within the kernel's run method. – gfrost Apr 25 '12 at 20:27

0 Answers0