From 3fa4d41e9cac75df7514c2ff8dd27842aaafc4a7 Mon Sep 17 00:00:00 2001 From: Keuin Date: Wed, 20 Apr 2022 00:22:02 +0800 Subject: Code refactor: make basic_viewport and aa_viewport no longer a child class. Move data into classes. Code refactor: make threading.h more specific, reducing redundant data copies in memory. Code refactor: make camera parameters more clear and well-defined in viewport.h, ready to extend camera flexibility. --- aa.h | 85 ++++++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 45 insertions(+), 40 deletions(-) (limited to 'aa.h') diff --git a/aa.h b/aa.h index 988a2f7..3a4def4 100644 --- a/aa.h +++ b/aa.h @@ -14,64 +14,71 @@ #include // Antialiasing viewport -template -class aa_viewport : public viewport { +// U: color depth, V: pos +template +class aa_viewport { + vec3 cxyz; + vec3 screen_center; + uint32_t image_width; // how many pixels every row has + uint32_t image_height; // how many pixels every column has + V screen_hw; // determined if screen_height is known + V screen_hh; // determined if screen_width is known + hitlist &world; unsigned samples; - std::vector> *subviews; int threads; public: - aa_viewport(double width, double height, vec3d viewport_center, unsigned samples, int threads = -1) - : samples(samples), threads{(threads > 0) ? threads : (int)std::thread::hardware_concurrency()} { - assert(samples >= 1); - subviews = new std::vector>{samples, {width, height, viewport_center}}; - } - ~aa_viewport() { - delete subviews; + aa_viewport(const vec3 &cxyz, + const vec3 &screen_center, + uint16_t image_width, + uint16_t image_height, + V screen_hw, + V screen_hh, + hitlist &world, + unsigned samples, + int threads = -1) : + cxyz(cxyz), + screen_center(screen_center), + image_width(image_width), + image_height(image_height), + screen_hw(screen_hw), + screen_hh(screen_hh), + world(world), + samples(samples), + threads((threads > 0) ? threads : (int) std::thread::hardware_concurrency()) { + assert(samples >= 1); } - virtual bitmap render(const hitlist &world, vec3d viewpoint, uint16_t image_width, uint16_t image_height) { + bitmap render() { static constexpr auto seed = 123456789012345678ULL; - const unsigned thread_count = std::min((unsigned)threads, samples); + const unsigned thread_count = std::min((unsigned) threads, samples); std::cerr << "Preparing tasks..." << std::endl; - std::vector> images{samples, {0, 0}}; + std::vector> images{samples, {0, 0}}; std::mt19937_64 seedgen{seed}; // generates seeds for workers - const struct s_render_shared { - std::vector> &subs; - vec3d viewpoint; - uint16_t image_width; - uint16_t image_height; - const hitlist &world; - std::vector> &images; - } s_{.subs=*subviews, .viewpoint = viewpoint, - .image_width=image_width, .image_height=image_height, - .world=world, .images=images - }; - struct s_render_task { - uint32_t task_id; - uint64_t seed; + uint64_t bias_seed; uint64_t diffuse_seed; - const s_render_shared &shared; }; - thread_pool pool{thread_count}; + thread_pool pool{thread_count, *this, images}; timer tim{true}; std::cerr << "Seeding tasks..." << std::endl; tim.start_measure(); for (typeof(samples) i = 0; i < samples; ++i) { - pool.submit_task([](s_render_task &task) { - bias_ctx bc{task.seed}; - auto image = task.shared.subs[task.task_id].render( - task.shared.world, task.shared.viewpoint, - task.shared.image_width, task.shared.image_height, - bc, task.diffuse_seed); - task.shared.images[task.task_id] = image; + pool.submit_task([](size_t tid, s_render_task &task, const aa_viewport &ctx, typeof(images) &images) { + basic_viewport vp{ + ctx.cxyz, ctx.screen_center, + ctx.image_width, ctx.image_height, + ctx.screen_hw, ctx.screen_hh, + ctx.world + }; + bias_ctx bc{task.bias_seed}; + images[tid] = vp.render(task.diffuse_seed, bc); }, s_render_task{ - .task_id = i, .seed=seedgen(), .diffuse_seed=seedgen(), .shared=s_ + .bias_seed=seedgen(), .diffuse_seed=seedgen() }); } tim.stop_measure(); @@ -84,12 +91,10 @@ public: tim.stop_measure(); std::cerr << "Finish rendering sub-pixels. Speed: " << 1.0 * tim.duration().count() / samples << "sec/image, " << 1.0 * samples * image_width * image_height / tim.duration().count() << " pixel/sec" << std::endl; - return bitmap::average(images); + return bitmap::average(images); } }; -using aa_viewport8b = aa_viewport; - #endif //RT_AA_H -- cgit v1.2.3