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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
#include "UnityPrefix.h"
#include "GfxDeviceRecreate.h"
#include "Runtime/Camera/RenderSettings.h"
#include "Runtime/Filters/Deformation/SkinnedMeshFilter.h"
#include "Runtime/Filters/Mesh/LodMesh.h"
#include "Runtime/Dynamics/ClothRenderer.h"
#include "Runtime/Filters/Misc/Font.h"
#include "Runtime/Filters/Misc/TextMesh.h"
#include "Runtime/GfxDevice/GfxDevice.h"
#include "Runtime/GfxDevice/GfxDeviceSetup.h"
#include "Runtime/Graphics/GeneratedTextures.h"
#include "Runtime/Graphics/RenderTexture.h"
#include "Runtime/Graphics/Texture.h"
#include "Runtime/IMGUI/TextMeshGenerator2.h"
#include "Runtime/Shaders/Shader.h"
#include "Runtime/Shaders/ComputeShader.h"
#if ENABLE_PROFILER
#include "Runtime/Profiler/ProfilerImpl.h"
#endif
#if UNITY_EDITOR
#include "Editor/Platform/Interface/EditorWindows.h"
#include "Editor/Src/Application.h"
#endif
#if ENABLE_TERRAIN
#include "Runtime/Interfaces/ITerrainManager.h"
#endif
#if ENABLE_WEBCAM
#include "Runtime/Video/VideoTexture.h"
#endif
static void UnloadAllTextures()
{
#if ENABLE_WEBCAM
BaseVideoTexture::SuspendVideoTextures ();
#endif
Texture::ReloadAll (true, false, true);
}
static void LoadAllTextures()
{
Texture::ReloadAll (false, true);
#if ENABLE_WEBCAM
BaseVideoTexture::ResumeVideoTextures ();
#endif
}
static void UnloadAllShaders()
{
std::vector<SInt32> allShaders;
Shader::DeleteAllShaders(allShaders);
Shader::UnloadDefaultShaderLabShader();
// Now, some shaders might have been unsupported, which means they were pointing to default
// ShaderLab shader. Go over all of them and make sure ShaderLab shaders are NULL everywhere.
for (std::vector<SInt32>::iterator i = allShaders.begin(); i != allShaders.end(); ++i)
{
Shader *s = PPtr<Shader> (*i);
if (s)
s->ResetInternalPointersToNull();
}
}
static void LoadAllShaders()
{
// Default pink shader is handled specially in shader load/unload because it's shared
// between multiple shader objects. We have to recreate it here.
Shader::LoadDefaultShaderLabShader();
std::vector<SInt32> allShaders;
Object::FindAllDerivedObjects (ClassID (Shader), &allShaders);
Shader::RecreateAllShaders(allShaders);
}
static void UnloadAllComputeShaders()
{
vector<SInt32> objs;
Object::FindAllDerivedObjects (ClassID (ComputeShader), &objs);
for (size_t i = 0, n = objs.size(); i != n; ++i)
{
ComputeShader* obj = PPtr<ComputeShader> (objs[i]);
obj->UnloadFromGfxDevice();
}
}
static void LoadAllComputeShaders()
{
vector<SInt32> objs;
Object::FindAllDerivedObjects (ClassID (ComputeShader), &objs);
for (size_t i = 0, n = objs.size(); i != n; ++i)
{
ComputeShader* obj = PPtr<ComputeShader> (objs[i]);
obj->ReloadToGfxDevice();
}
}
static void UnloadAllMeshes()
{
vector<SInt32> meshes;
Object::FindAllDerivedObjects (ClassID (Mesh), &meshes, true);
for (size_t i = 0, n = meshes.size(); i != n; ++i)
{
Mesh* mesh = PPtr<Mesh> (meshes[i]);
mesh->UnloadVBOFromGfxDevice();
}
}
static void LoadAllMeshes()
{
vector<SInt32> meshes;
Object::FindAllDerivedObjects (ClassID (Mesh), &meshes, true);
for (size_t i = 0, n = meshes.size(); i != n; ++i)
{
Mesh* mesh = PPtr<Mesh> (meshes[i]);
mesh->ReloadVBOToGfxDevice();
}
}
// Note: nothing needs to be done to "unload" fonts
static void LoadAllFonts()
{
vector<SInt32> fonts;
Object::FindAllDerivedObjects (ClassID (Font), &fonts, true);
for (size_t i = 0, n = fonts.size(); i != n; ++i)
{
Font* font = PPtr<Font> (fonts[i]);
font->ResetCachedTexture();
}
}
static void UnloadAllSkins()
{
vector<SInt32> skins;
Object::FindAllDerivedObjects (ClassID (SkinnedMeshRenderer), &skins, true);
for (size_t i = 0, n = skins.size(); i != n; ++i)
{
SkinnedMeshRenderer* skin = PPtr<SkinnedMeshRenderer> (skins[i]);
skin->UnloadVBOFromGfxDevice();
}
}
static void LoadAllSkins()
{
vector<SInt32> skins;
Object::FindAllDerivedObjects (ClassID (SkinnedMeshRenderer), &skins, true);
for (size_t i = 0, n = skins.size(); i != n; ++i)
{
SkinnedMeshRenderer* skin = PPtr<SkinnedMeshRenderer> (skins[i]);
skin->ReloadVBOToGfxDevice();
}
}
static void UnloadAllCloths()
{
# if ENABLE_CLOTH
vector<SInt32> cloths;
Object::FindAllDerivedObjects (ClassID (ClothRenderer), &cloths, true);
for (size_t i = 0, n = cloths.size(); i != n; ++i)
{
ClothRenderer* cloth = PPtr<ClothRenderer> (cloths[i]);
cloth->UnloadVBOFromGfxDevice();
}
# endif // #if ENABLE_CLOTH
}
static void LoadAllCloths()
{
# if ENABLE_CLOTH
vector<SInt32> cloths;
Object::FindAllDerivedObjects (ClassID (ClothRenderer), &cloths, true);
for (size_t i = 0, n = cloths.size(); i != n; ++i)
{
ClothRenderer* cloth = PPtr<ClothRenderer> (cloths[i]);
cloth->ReloadVBOToGfxDevice();
}
# endif // #if ENABLE_CLOTH
}
#if ENABLE_TERRAIN
static void UnloadAllTerrains()
{
GetITerrainManager()->UnloadTerrainsFromGfxDevice();
}
static void LoadAllTerrains()
{
GetITerrainManager()->ReloadTerrainsToGfxDevice();
}
#endif
static void AwakeFromLoadTextMeshes()
{
vector<SInt32> textMeshes;
Object::FindAllDerivedObjects (ClassID (TextMesh), &textMeshes, true);
for (size_t i = 0, n = textMeshes.size(); i != n; ++i)
{
TextMesh* textMesh = PPtr<TextMesh> (textMeshes[i]);
textMesh->AwakeFromLoad(kDefaultAwakeFromLoad);
}
}
void CleanupAllGfxDeviceResources()
{
#if UNITY_EDITOR && UNITY_WIN // GfxWindow thing only exists on Windows right now
ReleaseGfxWindowOnAllGUIViews();
#endif
TextMeshGenerator2::Flush();
RenderTexture::ReleaseAll();
ComputeBuffer::UnloadAllFromGfxDevice();
UnloadAllComputeShaders();
UnloadAllCloths();
UnloadAllSkins();
UnloadAllShaders();
UnloadAllTextures();
UnloadAllMeshes();
#if ENABLE_TERRAIN
UnloadAllTerrains();
#endif
#if ENABLE_PROFILER
UnityProfiler::CleanupGfx();
#endif
GetGfxDevice().FinishRendering();
DestroyGfxDevice();
}
void RecreateAllGfxDeviceResources()
{
// Reinitialize graphics caps
gGraphicsCaps = GraphicsCaps();
#if UNITY_EDITOR
gGraphicsCaps.ResetOriginalEmulationCaps();
#endif
InitializeGfxDevice();
LoadAllMeshes();
LoadAllTextures();
LoadAllShaders();
LoadAllFonts();
LoadAllSkins();
LoadAllCloths();
LoadAllComputeShaders();
ComputeBuffer::ReloadAllToGfxDevice();
#if ENABLE_TERRAIN
LoadAllTerrains();
#endif
// Neet to re-setup buit-in texture props on the new gfx device
builtintex::ReinitBuiltinTextures();
// Need to re-setup fog values etc.
GetRenderSettings().AwakeFromLoad(kDefaultAwakeFromLoad);
// Resetup text meshes, because dynamic font textures were reset
AwakeFromLoadTextMeshes();
#if UNITY_EDITOR && UNITY_WIN // GfxWindow thing only exists on Windows right now
GetApplication().UpdateMainWindowTitle();
CreateGfxWindowOnAllGUIViews();
GUIView::RepaintAll(true);
#endif
// Reload current scene in the editor, so that any scripts that depend on the gfx device
// capabilities are reinitialized.
#if UNITY_EDITOR
GetApplication().OpenScene(GetApplication().GetCurrentScene());
#endif
}
void RecreateGfxDevice()
{
CleanupAllGfxDeviceResources();
RecreateAllGfxDeviceResources();
}
void RecreateSkinnedMeshResources()
{
UnloadAllSkins();
LoadAllSkins();
}
|