Created
July 21, 2014 15:09
-
-
Save ssube/6e22cd9f54d571c8a2de to your computer and use it in GitHub Desktop.
Java software shader, with int optimizations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.stackoverflow.questions; | |
public class LayerImpl implements LayerProcessor { | |
@Override | |
public int apply(final int[][] data, final int x, final int y) { | |
int[] col = data[x]; | |
int accumulator = col[y] * 2 | |
+ data[x-1][y] | |
+ data[x+1][y] | |
+ col[y-1] | |
+ col[y+1]; | |
return (1000 * (accumulator + 500)) / 6000; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.stackoverflow.questions; | |
public interface LayerProcessor { | |
int apply(int[][] data, int x, int y); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.stackoverflow.questions; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Measurement; | |
import org.openjdk.jmh.infra.Blackhole; | |
import java.util.Random; | |
import java.util.concurrent.TimeUnit; | |
@Fork(2) | |
public class ShaderFunc { | |
public static final LayerProcessor proc = new LayerImpl(); | |
public static int width = 4000; | |
public static int height = 2000; | |
public static int[][] blurData = randomMatrix(width, height); | |
public int blur(final int[][] data, final int x, final int y) { | |
int[] col = data[x]; | |
int accumulator = col[y] * 2 | |
+ data[x-1][y] | |
+ data[x+1][y] | |
+ col[y-1] | |
+ col[y+1]; | |
return (1000 * (accumulator + 500)) / 6000; | |
} | |
@Benchmark | |
@Measurement(timeUnit = TimeUnit.MILLISECONDS) | |
public void testProcess(Blackhole bh) { | |
int width = 4000; | |
int height = 2000; | |
int[][] nextData = new int[width][height]; | |
for (int i = 1; i < width + 1; ++i) { | |
for (int j = 1; j < height + 1; ++j) { | |
nextData[i][j] = blur(blurData, i, j); | |
} | |
} | |
bh.consume(nextData); | |
} | |
@Benchmark | |
@Measurement(timeUnit = TimeUnit.MILLISECONDS) | |
public void testProcessProc(Blackhole bh) { | |
int width = 4000; | |
int height = 2000; | |
int[][] nextData = new int[width][height]; | |
for (int i = 1; i < width + 1; ++i) { | |
for (int j = 1; j < height + 1; ++j) { | |
nextData[i][j] = proc.apply(blurData, i, j); | |
} | |
} | |
bh.consume(nextData); | |
} | |
@Benchmark | |
@Measurement(timeUnit = TimeUnit.MILLISECONDS) | |
public void testProcessInline(Blackhole bh) { | |
int width = 4000; | |
int height = 2000; | |
int[][] nextData = new int[width][height]; | |
for (int i = 1; i < width + 1; ++i) { | |
for (int j = 1; j < height + 1; ++j) { | |
int[] col = blurData[i]; | |
int accumulator = col[j] * 2 | |
+ blurData[i-1][j] | |
+ blurData[i+1][j] | |
+ col[j-1] | |
+ col[j+1]; | |
nextData[i][j] = (1000 * (accumulator + 500)) / 6000; | |
} | |
} | |
bh.consume(nextData); | |
} | |
public static int[][] randomMatrix(final int x, final int y) { | |
final Random random = new Random(); | |
final int[][] data = new int[x + 2][y + 2]; | |
for (int i = 1; i < x - 1; ++i) { | |
for (int j = 1; j < y - 1; ++j) { | |
data[i][j] = random.nextInt(); | |
} | |
} | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment