Skip to content

Commit dbf6963

Browse files
committed
Parallel rendering improved
1 parent 073d947 commit dbf6963

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

final_test.png

1.51 MB
Loading

src/RayTracing/Main.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ public class Main {
44
public static void main(String[] args) {
55
Scene chosen = Scene.finalScene();
66
Renderer render = new Renderer(chosen.camera);
7-
render.imageRenderParallel(chosen.world, 500, "final_500.ppm");
7+
8+
// imageRender(50spp, 1280x720) for final image = 206.791s
9+
// imageRenderParallel(50spp, 1280x720) for final image = 46.492s
10+
render.imageRenderParallel(chosen.world, 50, "final_test.png");
11+
812
//render.windowRender(chosen.world, 50);
913

1014
}

src/RayTracing/RayResult.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package RayTracing;
2+
3+
public class RayResult {
4+
int index;
5+
Color color;
6+
7+
public RayResult() {
8+
this.index = 0;
9+
this.color = new Color(0);
10+
}
11+
}

src/RayTracing/Renderer.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ Color rayColor(Ray r, HitTable world, int depth){
101101
return skyColor(r);
102102
}
103103

104+
// Old version of imageRender, kept for benchmarking purposes and for futures references. Do not use.
104105
void imageRender(HitTable scene, int spp, String filename) {
105106
try(FileOutputStream file = new FileOutputStream(filename);
106107
PrintStream filePrint = new PrintStream(file) ) {
@@ -141,18 +142,24 @@ void imageRenderParallel(HitTable scene, int spp, String filename) {
141142

142143
for (long y=0; y < image_height; ++y) {
143144
System.out.print("\r" + "Scanlines remaining: "+(image_height-y));
144-
for (long x=0; x < image_width; ++x) {
145+
long finalY = y;
146+
IntStream.range(0, image_width).parallel().forEach(x -> {
145147
Color pixelColor = new Color(0);
146-
147-
long finalX = x;
148-
long finalY = y;
149-
IntStream.range(0, spp).parallel().forEach(s -> {
150-
double u = (finalX + randomDouble()) / (image_width-1);
151-
double v = (finalY + randomDouble()) / (image_height-1);
148+
for (int s = 0; s < spp; ++s) {
149+
double u = (x + randomDouble()) / (image_width - 1);
150+
double v = (finalY + randomDouble()) / (image_height - 1);
152151
pixelColor.equalAdd(rayColor(camera.get_ray(u, v), scene, depth));
153-
});
154-
image.setRGB((int)x, (int)y, writeAwtColor(pixelColor, spp).getRGB());
155-
}
152+
}
153+
// pixelColor = pixelColor.multiply(1.0 / spp);
154+
155+
RayResult result = new RayResult();
156+
int index = (int) (finalY * image_width + x);
157+
result.color = pixelColor;
158+
result.index = index;
159+
synchronized (image) {
160+
image.setRGB(index % image_width, index / image_width, writeAwtColor(result.color, spp).getRGB());
161+
}
162+
});
156163
}
157164
try {
158165
ImageIO.write(image, "png", new java.io.File(filename));

0 commit comments

Comments
 (0)