载入中...
搜索中...
未找到
AnimBatch.cpp
浏览该文件的文档.
2
6#include "common/Exception.h"
7
8#include <algorithm>
9#include <atomic>
10#include <thread>
11
12namespace eve::animation {
13
14void AnimBatch::add(AnimClip* clip, AnimSkeleton* skeleton, AnimPose* pose, float time, int lodLevel) {
15 if (!clip || !skeleton || !pose) throw Exception("AnimBatch.add: clip, skeleton and pose are required");
16 if (lodLevel < 0) throw Exception("AnimBatch.add: lodLevel must be >= 0");
17 for (const Job& job : jobs_) if (job.pose == pose) throw Exception("AnimBatch.add: pose is already queued");
18 jobs_.push_back({clip, skeleton, pose, time, lodLevel});
19}
20
21void AnimBatch::evaluate(int workerCount) {
22 if (workerCount < 0) throw Exception("AnimBatch.evaluate: workerCount must be >= 0");
23 if (jobs_.empty()) { lastWorkerCount_ = 0; return; }
24 const unsigned hardware = std::max(1u, std::thread::hardware_concurrency());
25 const size_t count = std::min(jobs_.size(), static_cast<size_t>(workerCount > 0 ? workerCount : hardware));
26 lastWorkerCount_ = static_cast<int>(count);
27 if (count == 1) {
28 for (Job& job : jobs_) job.clip->sampleLod(job.time, job.pose, job.skeleton, job.lodLevel);
29 return;
30 }
31 std::atomic<size_t> next{0};
32 std::vector<std::thread> workers;
33 workers.reserve(count);
34 for (size_t worker = 0; worker < count; ++worker) {
35 workers.emplace_back([&] {
36 for (;;) {
37 const size_t index = next.fetch_add(1, std::memory_order_relaxed);
38 if (index >= jobs_.size()) break;
39 Job& job = jobs_[index];
40 job.clip->sampleLod(job.time, job.pose, job.skeleton, job.lodLevel);
41 }
42 });
43 }
44 for (std::thread& worker : workers) worker.join();
45}
46
47} // namespace eve::animation
eve::EntitySpatialPose pose
glm::vec4 clip
std::uint32_t count
uint32_t index
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
void evaluate(int workerCount=0)
Evaluate queued poses, using hardware concurrency when workerCount is zero.
Definition AnimBatch.cpp:21
void add(AnimClip *clip, AnimSkeleton *skeleton, AnimPose *pose, float time, int lodLevel=0)
Queue one clip sample. The clip, skeleton and pose must outlive evaluate().
Definition AnimBatch.cpp:14
Keyframed skeletal animation clip (local TRS tracks per bone). Script type: AnimClip.
Definition AnimClip.h:145
Evaluated local (and optional world) pose for an AnimSkeleton. Script type: AnimPose.
Definition AnimPose.h:17
3D bone hierarchy + bind-pose local TRS for skeletal animation. Independent of ik::Skeleton3D (FABRIK...