RavEngine
Loading...
Searching...
No Matches
sampling_job.h
1//----------------------------------------------------------------------------//
2// //
3// ozz-animation is hosted at http://github.com/guillaumeblanc/ozz-animation //
4// and distributed under the MIT License (MIT). //
5// //
6// Copyright (c) Guillaume Blanc //
7// //
8// Permission is hereby granted, free of charge, to any person obtaining a //
9// copy of this software and associated documentation files (the "Software"), //
10// to deal in the Software without restriction, including without limitation //
11// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
12// and/or sell copies of the Software, and to permit persons to whom the //
13// Software is furnished to do so, subject to the following conditions: //
14// //
15// The above copyright notice and this permission notice shall be included in //
16// all copies or substantial portions of the Software. //
17// //
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
21// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
24// DEALINGS IN THE SOFTWARE. //
25// //
26//----------------------------------------------------------------------------//
27
28#ifndef OZZ_OZZ_ANIMATION_RUNTIME_SAMPLING_JOB_H_
29#define OZZ_OZZ_ANIMATION_RUNTIME_SAMPLING_JOB_H_
30
31#include "ozz/animation/runtime/export.h"
32#include "ozz/base/platform.h"
33#include "ozz/base/span.h"
34
35namespace ozz {
36
37// Forward declaration of math structures.
38namespace math {
39struct SoaTransform;
40}
41
42namespace animation {
43
44// Forward declares the animation type to sample.
45class Animation;
46
47// Samples an animation at a given time ratio in the unit interval [0,1] (where
48// 0 is the beginning of the animation, 1 is the end), to output the
49// corresponding posture in local-space.
50// SamplingJob uses a context (aka SamplingJob::Context) to store intermediate
51// values (decompressed animation keyframes...) while sampling. This context
52// also stores pre-computed values that allows drastic optimization while
53// playing/sampling the animation forward. Backward sampling works, but isn't
54// optimized through the context. The job does not owned the buffers (in/output)
55// and will thus not delete them during job's destruction.
56struct OZZ_ANIMATION_DLL SamplingJob {
57 // Default constructor, initializes default values.
59
60 // Validates job parameters. Returns true for a valid job, or false otherwise:
61 // -if any input pointer is nullptr
62 // -if output range is invalid.
63 bool Validate() const;
64
65 // Runs job's sampling task.
66 // The job is validated before any operation is performed, see Validate() for
67 // more details.
68 // Returns false if *this job is not valid.
69 bool Run() const;
70
71 // Time ratio in the unit interval [0,1] used to sample animation (where 0 is
72 // the beginning of the animation, 1 is the end). It should be computed as the
73 // current time in the animation , divided by animation duration.
74 // This ratio is clamped before job execution in order to resolves any
75 // approximation issue on range bounds.
76 float ratio;
77
78 // The animation to sample.
79 const Animation* animation;
80
81 // Forward declares the context object used by the SamplingJob.
82 class Context;
83
84 // A context object that must be big enough to sample *this animation.
85 Context* context;
86
87 // Job output.
88 // The output range to be filled with sampled joints during job execution.
89 // If there are less joints in the animation compared to the output range,
90 // then remaining SoaTransform are left unchanged.
91 // If there are more joints in the animation, then the last joints are not
92 // sampled.
94};
95
96namespace internal {
97// Soa hot data to interpolate.
98struct InterpSoaFloat3;
99struct InterpSoaQuaternion;
100} // namespace internal
101
102// Declares the context object used by the workload to take advantage of the
103// frame coherency of animation sampling.
104class OZZ_ANIMATION_DLL SamplingJob::Context {
105 public:
106 // Constructs an empty context. The context needs to be resized with the
107 // appropriate number of tracks before it can be used with a SamplingJob.
108 Context();
109
110 // Constructs a context that can be used to sample any animation with at most
111 // _max_tracks tracks. _num_tracks is internally aligned to a multiple of
112 // soa size, which means max_tracks() can return a different (but bigger)
113 // value than _max_tracks.
114 explicit Context(int _max_tracks);
115
116 // Disables copy and assignation.
117 Context(Context const&) = delete;
118 Context& operator=(Context const&) = delete;
119
120 // Deallocates context.
121 ~Context();
122
123 // Resize the number of joints that the context can support.
124 // This also implicitly invalidate the context.
125 void Resize(int _max_tracks);
126
127 // Invalidate the context.
128 // The SamplingJob automatically invalidates a context when required
129 // during sampling. This automatic mechanism is based on the animation
130 // address and sampling time ratio. The weak point is that it can result in a
131 // crash if ever the address of an animation is used again with another
132 // animation (could be the result of successive call to delete / new).
133 // Therefore it is recommended to manually invalidate a context when it is
134 // known that this context will not be used for with an animation again.
135 void Invalidate();
136
137 // The maximum number of tracks that the context can handle.
138 int max_tracks() const { return max_soa_tracks_ * 4; }
139 int max_soa_tracks() const { return max_soa_tracks_; }
140
141 private:
142 friend struct SamplingJob;
143
144 // Steps the context in order to use it for a potentially new animation and
145 // ratio. If the _animation is different from the animation currently cached,
146 // or if the _ratio shows that the animation is played backward, then the
147 // context is invalidated and reset for the new _animation and _ratio.
148 void Step(const Animation& _animation, float _ratio);
149
150 // The animation this context refers to. nullptr means that the context is
151 // invalid.
152 const Animation* animation_;
153
154 // The current time ratio in the animation.
155 float ratio_;
156
157 // The number of soa tracks that can store this context.
158 int max_soa_tracks_;
159
160 // Soa hot data to interpolate.
161 internal::InterpSoaFloat3* soa_translations_;
162 internal::InterpSoaQuaternion* soa_rotations_;
163 internal::InterpSoaFloat3* soa_scales_;
164
165 // Points to the keys in the animation that are valid for the current time
166 // ratio.
167 int* translation_keys_;
168 int* rotation_keys_;
169 int* scale_keys_;
170
171 // Current cursors in the animation. 0 means that the context is invalid.
172 int translation_cursor_;
173 int rotation_cursor_;
174 int scale_cursor_;
175
176 // Outdated soa entries. One bit per soa entry (32 joints per byte).
177 uint8_t* outdated_translations_;
178 uint8_t* outdated_rotations_;
179 uint8_t* outdated_scales_;
180};
181} // namespace animation
182} // namespace ozz
183#endif // OZZ_OZZ_ANIMATION_RUNTIME_SAMPLING_JOB_H_
Definition base.h:1940
Definition animation.h:62
uint8 uint8_t
Definition fwd.hpp:103
Definition sampling_job.h:56
Definition span.h:37