summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeuin <[email protected]>2022-04-20 00:48:44 +0800
committerKeuin <[email protected]>2022-04-20 00:48:44 +0800
commitba97aeb0a6ff52b79f43f883c639a55c8e3cf78d (patch)
tree8f8707eba7095a75c6d65d7265d1b8b8040c2194
parent3fa4d41e9cac75df7514c2ff8dd27842aaafc4a7 (diff)
Reserve vector thread_pool::tasks to speed up task initializing.
-rw-r--r--aa.h22
-rw-r--r--threading.h5
2 files changed, 25 insertions, 2 deletions
diff --git a/aa.h b/aa.h
index 3a4def4..9622696 100644
--- a/aa.h
+++ b/aa.h
@@ -50,6 +50,26 @@ public:
assert(samples >= 1);
}
+ aa_viewport(const vec3<V> &cxyz,
+ const vec3<V> &screen_center,
+ uint16_t image_width,
+ uint16_t image_height,
+ double fov_h,
+ hitlist &world,
+ unsigned samples,
+ int threads = -1) :
+ cxyz(cxyz),
+ screen_center(screen_center),
+ image_width(image_width),
+ image_height(image_height),
+ screen_hw{(cxyz - screen_center).norm() * tan((double) fov_h / 2.0)},
+ screen_hh{screen_hw * ((double) image_height / image_width)},
+ world(world),
+ samples(samples),
+ threads((threads > 0) ? threads : (int) std::thread::hardware_concurrency()) {
+ assert(samples >= 1);
+ }
+
bitmap<U> render() {
static constexpr auto seed = 123456789012345678ULL;
const unsigned thread_count = std::min((unsigned) threads, samples);
@@ -62,7 +82,7 @@ public:
uint64_t diffuse_seed;
};
- thread_pool<s_render_task, typeof(*this), typeof(images)> pool{thread_count, *this, images};
+ thread_pool<s_render_task, typeof(*this), typeof(images)> pool{thread_count, *this, images, samples};
timer tim{true};
std::cerr << "Seeding tasks..." << std::endl;
diff --git a/threading.h b/threading.h
index 6d79494..249a115 100644
--- a/threading.h
+++ b/threading.h
@@ -41,9 +41,12 @@ class thread_pool {
void worker_main();
public:
- explicit thread_pool(unsigned thread_count, const U &shared_ctx, V &mut_shared_ctx) :
+ thread_pool(unsigned thread_count, const U &shared_ctx, V &mut_shared_ctx, size_t reserve_tasks = -1) :
thread_count{thread_count}, shared_ctx{shared_ctx}, mut_shared_ctx{mut_shared_ctx} {
std::cerr << "Using " << (counter.is_lock_free() ? "lock-free" : "locking") << " dispatcher." << std::endl;
+ if (reserve_tasks > 0) {
+ tasks.reserve(reserve_tasks);
+ }
}
// Thread unsafe!