-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulationcache.cpp
More file actions
183 lines (151 loc) · 6.73 KB
/
simulationcache.cpp
File metadata and controls
183 lines (151 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "simulationcache.h"
#include <maya/MAnimControl.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MEventMessage.h>
#include <maya/MNodeMessage.h>
#include "globalsolver.h"
const MString SimulationCache::timeSliderDrawContextName("SimulationCacheTimeSliderContext");
SimulationCache* SimulationCache::simulationCacheInstance = nullptr;
SimulationCache::SimulationCache() {
MTimeSliderCustomDrawManager& drawManager = MTimeSliderCustomDrawManager::instance();
customDrawID = drawManager.registerCustomDrawOutside(MTimeSliderCustomDrawManager::kAbove, timeSliderDrawContextName, MString("Cubit Simulation Cache"), 0);
}
SimulationCache::~SimulationCache() {
drawPrimitives.clear();
MTimeSliderCustomDrawManager::instance().deregisterCustomDraw(customDrawID);
}
void SimulationCache::tearDown() {
if (simulationCacheInstance) {
delete simulationCacheInstance;
simulationCacheInstance = nullptr;
}
}
SimulationCache* const SimulationCache::instance() {
if (simulationCacheInstance) {
return simulationCacheInstance;
}
simulationCacheInstance = new SimulationCache();
return simulationCacheInstance;
}
SimulationCache::Registration SimulationCache::registerBuffer(ComPtr<ID3D11Buffer> buffer) {
registry.insert(buffer);
// Add its initial data to the cache start frame (special frame that persists even when clearing the cache)
MTime startTime = MAnimControl::minTime();
double startFrame = std::floor(startTime.as(MTime::uiUnit()));
std::vector<uint8_t>& bufferData = cache[startFrame][buffer];
DirectX::copyBufferToVector(buffer, bufferData);
singleFrameCacheSize += static_cast<int>(bufferData.size());
return Registration(buffer);
}
void SimulationCache::unregisterBuffer(ComPtr<ID3D11Buffer> buffer) {
registry.erase(buffer);
for (auto it = cache.begin(); it != cache.end(); ) {
it->second.erase(buffer);
if (it->second.empty()) {
removeMarkerAtFrame(it->first);
cachedFrames.erase(it->first);
it = cache.erase(it);
} else {
++it;
}
}
MTimeSliderCustomDrawManager::instance().setDrawPrimitives(customDrawID, drawPrimitives);
}
void SimulationCache::cacheData(const MTime& time) {
double currentFrame = std::floor(time.as(MTime::uiUnit()));
int maxCacheSizeMB = MPlug(GlobalSolver::getOrCreateGlobalSolver() , GlobalSolver::aMaxCacheSize).asInt();
const uint64_t maxBytes = (uint64_t)maxCacheSizeMB * 1024ull * 1024ull;
bool overLimit = (currentCacheSize + singleFrameCacheSize > (maxBytes - singleFrameCacheSize)); // subtract one frame size to account for start frame
// If we've exceeded the cache budget, reuse the data from an existing cached frame (the one furthest from the current frame)
double frameToUse = currentFrame;
if (overLimit) {
double lowestCachedFrame = cachedFrames.empty() ? DBL_MAX : *cachedFrames.begin();
double highestCachedFrame = cachedFrames.empty() ? -DBL_MAX : *cachedFrames.rbegin();
bool direction = std::abs(currentFrame - lowestCachedFrame) > std::abs(currentFrame - highestCachedFrame);
frameToUse = (direction) ? lowestCachedFrame : highestCachedFrame;
// Exclude start frame from eviction
MTime startTime = MAnimControl::minTime();
double startFrame = std::floor(startTime.as(MTime::uiUnit()));
if (frameToUse == startFrame) frameToUse = direction ? *std::next(cachedFrames.begin()) : *std::prev(std::prev(cachedFrames.end()));
}
for (const auto& buffer : registry) {
std::vector<uint8_t>& bufferData = cache[frameToUse][buffer];
DirectX::copyBufferToVector(buffer, bufferData);
}
// With the data copied, if we were over the limit we can now evict the old frame
if (overLimit) {
cache[currentFrame] = std::move(cache[frameToUse]);
cache.erase(frameToUse);
cachedFrames.erase(frameToUse);
removeMarkerAtFrame(frameToUse);
} else {
currentCacheSize += singleFrameCacheSize;
}
addMarkerToTimeline(currentFrame);
MTimeSliderCustomDrawManager::instance().setDrawPrimitives(customDrawID, drawPrimitives);
cachedFrames.insert(currentFrame);
}
bool SimulationCache::tryUseCache(const MTime& time) {
double currentFrame = std::floor(time.as(MTime::uiUnit()));
auto frameIt = cache.find(currentFrame);
if (frameIt == cache.end()) {
return false;
}
ID3D11DeviceContext* dxContext = DirectX::getContext();
for (const auto& bufferDataPair : frameIt->second) {
const ComPtr<ID3D11Buffer>& buffer = bufferDataPair.first;
const std::vector<uint8_t>& bufferData = bufferDataPair.second;
dxContext->UpdateSubresource(buffer.Get(), 0, nullptr, bufferData.data(), 0, 0);
}
return true;
}
bool SimulationCache::hasCacheData(const MTime& time) {
double currentFrame = std::floor(time.as(MTime::uiUnit()));
return cache.find(currentFrame) != cache.end();
}
void SimulationCache::resetCache() {
// This is a little outside the purview of what a cache should do, but it's very useful:
// Before resetting the cache, reset the simulation to the start frame so we never lose the initial state.
MTime startTime = MAnimControl::minTime();
double startFrame = std::floor(startTime.as(MTime::uiUnit()));
tryUseCache(startTime); // effectively resets buffer data to start state
cache.clear();
drawPrimitives.clear();
currentCacheSize = 0;
cachedFrames.clear();
// And recache the initial data for the start frame
cacheData(startTime);
currentCacheSize = 0; // zero again because cacheData already accounts for start frame and we don't want to double count it
}
void SimulationCache::addMarkerToTimeline(double frameKey) {
if (hasMarkerAtFrame(frameKey)) return;
MTime time(frameKey, MTime::uiUnit());
MTimeSliderDrawPrimitive marker(
MTimeSliderDrawPrimitive::kFilledRect,
time,
time + MTime(1.0, MTime::uiUnit()),
MColor(0.0f, 1.0f, 0.0f),
-1,
0
);
drawPrimitives.append(marker);
}
bool SimulationCache::hasMarkerAtFrame(double frameKey) {
for (size_t i = 0; i < drawPrimitives.length(); ++i) {
MTimeSliderDrawPrimitive prim = drawPrimitives[i];
if (prim.startTime().as(MTime::uiUnit()) == frameKey) {
return true;
}
}
return false;
}
void SimulationCache::removeMarkerAtFrame(double frameKey) {
MTimeSliderDrawPrimitives newPrims;
for (size_t i = 0; i < drawPrimitives.length(); ++i) {
MTimeSliderDrawPrimitive prim = drawPrimitives[i];
if (prim.startTime().as(MTime::uiUnit()) != frameKey) {
newPrims.append(prim);
}
}
drawPrimitives = std::move(newPrims);
}