13
13
14
14
#include " hittable.h"
15
15
#include " material.h"
16
-
16
+ #include < sys/mman.h>
17
+ #include < unistd.h>
18
+ #include < stdio.h>
19
+ #include < sys/types.h>
20
+ #include < sys/wait.h>
17
21
18
22
class camera {
19
23
public:
@@ -30,26 +34,62 @@ class camera {
30
34
double defocus_angle = 0 ; // Variation angle of rays through each pixel
31
35
double focus_dist = 10 ; // Distance from camera lookfrom point to plane of perfect focus
32
36
33
- void render (const hittable& world) {
37
+ void render (const hittable& world, int processes ) {
34
38
initialize ();
35
39
36
40
std::cout << " P3\n " << image_width << ' ' << image_height << " \n 255\n " ;
37
41
38
- for (int j = 0 ; j < image_height; j++) {
39
- std::clog << " \r Scanlines remaining: " << (image_height - j) << ' ' << std::flush;
40
- for (int i = 0 ; i < image_width; i++) {
41
- color pixel_color (0 ,0 ,0 );
42
- for (int sample = 0 ; sample < samples_per_pixel; sample++) {
43
- ray r = get_ray (i, j);
44
- pixel_color += ray_color (r, max_depth, world);
42
+ int image_size_in_bytes = sizeof (color) * image_width * image_height;
43
+ color *rendered_image = (color *) mmap (nullptr , image_size_in_bytes,
44
+ PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1 , 0 );
45
+
46
+ int lines_per_child = image_height / processes;
47
+ int remainder = image_height % processes;
48
+
49
+ std::clog << " Rendering " << image_height << " lines with " << processes << " processes.\n " ;
50
+ std::clog << lines_per_child << " lines per child, " << remainder << " remainder.\n " ;
51
+
52
+ pid_t pid;
53
+ for (int i = 0 ; i < processes; i++) {
54
+ pid = fork ();
55
+ if (pid == 0 ) {
56
+ for (int j = i * lines_per_child; j < (i + 1 ) * lines_per_child; j++) {
57
+ renderLine (j, world, rendered_image);
45
58
}
46
- write_color (std::cout, pixel_samples_scale * pixel_color);
59
+ std::clog << " Child process " << i << " done. \n " ;
60
+ exit (0 );
61
+ }
62
+ }
63
+
64
+ for (int i = 0 ; i < remainder; i++) {
65
+ renderLine (i, world, rendered_image);
66
+ }
67
+ std::clog << " Remainder done. \n " ;
68
+
69
+ for (int i = 0 ; i < processes; i++) {
70
+ wait (NULL );
71
+ }
72
+
73
+ for (int i = 0 ; i < image_height; i++) {
74
+ for (int j = 0 ; j < image_width; j++) {
75
+ write_color (std::cout, rendered_image[i * image_width + j]);
47
76
}
48
77
}
49
78
50
79
std::clog << " \r Done. \n " ;
51
80
}
52
81
82
+ void renderLine (int line, const hittable &world, color *rendered_image) {
83
+ for (int i = 0 ; i < image_width; i++) {
84
+ color pixel_color (0 ,0 ,0 );
85
+ for (int sample = 0 ; sample < samples_per_pixel; sample++) {
86
+ ray r = get_ray (i, line);
87
+ pixel_color += ray_color (r, max_depth, world);
88
+ }
89
+ rendered_image[line * image_width + i] = pixel_samples_scale * pixel_color;
90
+ }
91
+ }
92
+
53
93
private:
54
94
int image_height; // Rendered image height
55
95
double pixel_samples_scale; // Color scale factor for a sum of pixel samples
0 commit comments