Adding basic multi threading via OpenMP and using more aggressive optimizations.

This commit is contained in:
Faerbit 2020-06-06 15:55:24 +02:00
parent a0de4cc8f1
commit b86ad047a5
2 changed files with 15 additions and 4 deletions

View File

@ -1,6 +1,6 @@
CXX = g++ CXX = g++
CXXFLAGS = -Wall -Wextra -O2 -std=c++14 CXXFLAGS = -std=c++14 -Wall -Wextra -march=native -Ofast -fopenmp -flto
DEPS = util.h vec3.h color.h ray.h camera.h hittable.h hittable_list.h sphere.h DEPS = util.h vec3.h color.h ray.h camera.h hittable.h hittable_list.h sphere.h
OBJ = wtracer.o OBJ = wtracer.o

View File

@ -27,7 +27,8 @@ Color ray_color(const Ray& r, const Hittable& world, int depth) {
int main() { int main() {
const auto aspect_ratio = 16.0 / 9.0; const auto aspect_ratio = 16.0 / 9.0;
//const int image_width = 1280; //const int image_width = 1280;
const int image_width = 384; const int image_width = 768;
//const int image_width = 384;
const int image_height = static_cast<int>(image_width / aspect_ratio); const int image_height = static_cast<int>(image_width / aspect_ratio);
const int samples_per_pixel = 250; const int samples_per_pixel = 250;
const int max_depth = 50; const int max_depth = 50;
@ -40,8 +41,11 @@ int main() {
Camera cam; Camera cam;
Color* image = new Color[image_height*image_width];
for (int j = image_height - 1; j >= 0; --j) { for (int j = image_height - 1; j >= 0; --j) {
std::cerr << "\rScanlines remaining: " << j << " " << std::flush; std::cerr << "\rScanlines remaining: " << j << " " << std::flush;
#pragma omp parallel for
for (int i = 0; i < image_width; ++i) { for (int i = 0; i < image_width; ++i) {
Color pixel_color(0, 0, 0); Color pixel_color(0, 0, 0);
for (int s = 0; s<samples_per_pixel; ++s) { for (int s = 0; s<samples_per_pixel; ++s) {
@ -50,8 +54,15 @@ int main() {
Ray r = cam.get_ray(u, v); Ray r = cam.get_ray(u, v);
pixel_color += ray_color(r, world, max_depth); pixel_color += ray_color(r, world, max_depth);
} }
write_color(std::cout, pixel_color, samples_per_pixel); image[i*image_height+j] = pixel_color;
} }
} }
std::cerr << "\nDone.\n"; std::cerr << "\nWriting file.\n";
for (int j = image_height - 1; j >= 0; --j) {
for (int i = 0; i < image_width; ++i) {
write_color(std::cout, image[i*image_height+j], samples_per_pixel);
}
}
delete[] image;
std::cerr << "Done.\n";
} }