Use tiles and queue of them for rendering.
This commit is contained in:
parent
415a2122b4
commit
d2ce61e208
81
wtracer.cpp
81
wtracer.cpp
@ -1,11 +1,14 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
#include <atomic>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
#define LODEPNG_NO_COMPILE_DECODER
|
#define LODEPNG_NO_COMPILE_DECODER
|
||||||
#include "lodepng.h"
|
#include "lodepng.h"
|
||||||
|
|
||||||
|
#include <boost/lockfree/queue.hpp>
|
||||||
|
|
||||||
#ifdef _OPENMP
|
#ifdef _OPENMP
|
||||||
#include <omp.h>
|
#include <omp.h>
|
||||||
#endif
|
#endif
|
||||||
@ -36,11 +39,9 @@ Color ray_color(const Ray& r, const Hittable& world, int depth) {
|
|||||||
return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0);
|
return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Hittable_list setup_random_scene() {
|
Hittable_list setup_random_scene(const int sph_i) {
|
||||||
Hittable_list world;
|
Hittable_list world;
|
||||||
|
|
||||||
const int sph_i = 3;
|
|
||||||
|
|
||||||
auto ground_material = std::make_shared<Lambertian>(Color(0.5, 0.5, 0.5));
|
auto ground_material = std::make_shared<Lambertian>(Color(0.5, 0.5, 0.5));
|
||||||
world.add(std::make_shared<Sphere>(Point3(0, -1000, 0), 1000, ground_material));
|
world.add(std::make_shared<Sphere>(Point3(0, -1000, 0), 1000, ground_material));
|
||||||
for (int a = -sph_i; a<sph_i; a++) {
|
for (int a = -sph_i; a<sph_i; a++) {
|
||||||
@ -50,7 +51,7 @@ Hittable_list setup_random_scene() {
|
|||||||
|
|
||||||
if ((center - Point3(4, 0.2, 0)).length() > 0.9) {
|
if ((center - Point3(4, 0.2, 0)).length() > 0.9) {
|
||||||
std::shared_ptr<Material> sphere_material;
|
std::shared_ptr<Material> sphere_material;
|
||||||
if (choose_mat < 0.8) {
|
if (choose_mat < 0.7) {
|
||||||
// diffuse
|
// diffuse
|
||||||
auto albedo = Color::random() * Color::random();
|
auto albedo = Color::random() * Color::random();
|
||||||
sphere_material = std::make_shared<Lambertian>(albedo);
|
sphere_material = std::make_shared<Lambertian>(albedo);
|
||||||
@ -92,6 +93,13 @@ struct render_params {
|
|||||||
Camera& cam;
|
Camera& cam;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct render_tile {
|
||||||
|
int start_x;
|
||||||
|
int start_y;
|
||||||
|
int end_x;
|
||||||
|
int end_y;
|
||||||
|
};
|
||||||
|
|
||||||
void split_line(const int& line_length, int& start, int& end) {
|
void split_line(const int& line_length, int& start, int& end) {
|
||||||
#ifdef _OPENMP
|
#ifdef _OPENMP
|
||||||
const int line_length_thread = static_cast<int>(line_length/omp_get_num_threads());
|
const int line_length_thread = static_cast<int>(line_length/omp_get_num_threads());
|
||||||
@ -103,38 +111,51 @@ void split_line(const int& line_length, int& start, int& end) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void render(const render_params& params, std::vector<Color>& image) {
|
void render(boost::lockfree::queue<render_tile>& queue,
|
||||||
int line_start, line_end;
|
std::atomic<int>& queue_counter,
|
||||||
split_line(params.image_width, line_start, line_end);
|
const render_params& params, std::vector<Color>& image) {
|
||||||
for (int i = params.image_height-1; i>=0; --i) {
|
render_tile rt;
|
||||||
#ifdef _OPENMP
|
while (queue.pop(rt)) {
|
||||||
if (omp_get_thread_num() == 0)
|
--queue_counter;
|
||||||
#endif
|
int qc = queue_counter;
|
||||||
std::cerr << "\rScanline remaining: " << std::setw(4) << i << std::flush;
|
printf("\rTiles remaining: %4d", qc);
|
||||||
for (int j = line_start; j<line_end; ++j) {
|
std::cout << std::flush;
|
||||||
Color pixel_color(0, 0, 0);
|
for (int i = rt.start_x; i<rt.end_x; ++i) {
|
||||||
for (int s = 0; s<params.samples_per_pixel; ++s) {
|
for (int j = rt.start_y; j<rt.end_y; ++j) {
|
||||||
auto u = double(j + random_double(-0.5, 0.5)) / (params.image_width - 1);
|
Color pixel_color(0, 0, 0);
|
||||||
auto v = double(i + random_double(-0.5, 0.5)) / (params.image_height - 1);
|
for (int s = 0; s<params.samples_per_pixel; ++s) {
|
||||||
Ray r = params.cam.get_ray(u, v);
|
auto u = double(j + random_double(-0.5, 0.5)) / (params.image_width - 1);
|
||||||
pixel_color += ray_color(r, params.world, params.max_depth);
|
auto v = double(i + random_double(-0.5, 0.5)) / (params.image_height - 1);
|
||||||
|
Ray r = params.cam.get_ray(u, v);
|
||||||
|
pixel_color += ray_color(r, params.world, params.max_depth);
|
||||||
|
}
|
||||||
|
image[i*params.image_width+j] = pixel_color;
|
||||||
}
|
}
|
||||||
image[i*params.image_width+j] = pixel_color;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
const auto aspect_ratio = 16.0 / 9.0;
|
const auto aspect_ratio = 16.0 / 9.0;
|
||||||
|
//const int image_width = 1920;
|
||||||
//const int image_width = 1280;
|
//const int image_width = 1280;
|
||||||
//const int image_width = 768;
|
//const int image_width = 768;
|
||||||
const int image_width = 384;
|
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 = 2000;
|
||||||
//const int samples_per_pixel = 1000;
|
//const int samples_per_pixel = 1000;
|
||||||
const int samples_per_pixel = 400;
|
const int samples_per_pixel = 400;
|
||||||
const int max_depth = 50;
|
const int max_depth = 50;
|
||||||
|
|
||||||
auto world = setup_random_scene();
|
//const int tile_size = 128;
|
||||||
|
const int tile_size = 64;
|
||||||
|
|
||||||
|
const int sph_i = 3;
|
||||||
|
//const int sph_i = 5;
|
||||||
|
//const int sph_i = 7;
|
||||||
|
//const int sph_i = 11;
|
||||||
|
|
||||||
|
auto world = setup_random_scene(sph_i);
|
||||||
|
|
||||||
Point3 lookfrom(13, 2, 3);
|
Point3 lookfrom(13, 2, 3);
|
||||||
Point3 lookat(0, 0, 0);
|
Point3 lookat(0, 0, 0);
|
||||||
@ -153,11 +174,27 @@ int main() {
|
|||||||
cam,
|
cam,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::atomic<int> queue_counter;
|
||||||
|
boost::lockfree::queue<render_tile> queue(0);
|
||||||
|
|
||||||
|
for (int i = 0; i<std::ceil(double(image_height)/double(tile_size)); i++) {
|
||||||
|
for (int j = 0; j<std::ceil(double(image_width)/double(tile_size)); j++) {
|
||||||
|
render_tile rt = {
|
||||||
|
i*tile_size,
|
||||||
|
j*tile_size,
|
||||||
|
std::min((i+1)*tile_size, image_height),
|
||||||
|
std::min((j+1)*tile_size, image_width)
|
||||||
|
};
|
||||||
|
queue.push(rt);
|
||||||
|
++queue_counter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<Color> image(image_width * image_height);
|
std::vector<Color> image(image_width * image_height);
|
||||||
|
|
||||||
#pragma omp parallel
|
#pragma omp parallel
|
||||||
{
|
{
|
||||||
render(params, image);
|
render(queue, queue_counter, params, image);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cerr << "\nAssembling image.\n";
|
std::cerr << "\nAssembling image.\n";
|
||||||
|
Loading…
Reference in New Issue
Block a user