OvrAvatarTextureCopyManager.cs
9.58 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
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
using System.Collections;
using System.Collections.Generic;
using Oculus.Avatar;
using UnityEngine;
public class OvrAvatarTextureCopyManager : MonoBehaviour
{
[System.Serializable]
public struct FallbackTextureSet
{
public bool Initialized;
public Texture2D DiffuseRoughness;
public Texture2D Normal;
}
// Fallback texture sets are indexed with ovrAvatarAssetLevelOfDetail.
// We currently only use 1, 3 (mobile default), 5 (PC default).
public FallbackTextureSet[] FallbackTextureSets = new FallbackTextureSet[(int)ovrAvatarAssetLevelOfDetail.Highest + 1];
struct CopyTextureParams
{
public Texture Src;
public Texture Dst;
public int Mip;
public int SrcSize;
public int DstElement;
public CopyTextureParams(
Texture src,
Texture dst,
int mip,
int srcSize,
int dstElement)
{
Src = src;
Dst = dst;
Mip = mip;
SrcSize = srcSize;
DstElement = dstElement;
}
}
private Queue<CopyTextureParams> texturesToCopy;
public struct TextureSet
{
// Contains all texture asset IDs that are part of an avatar spec.
// Used by DeleteTextureSet().
// Textures that are part of combined mesh avatars can be safely deleted once they have been
// uploaded to the texture arrays.
// Textures that are part of single component meshes will remain in memory.
public Dictionary<ulong, bool> TextureIDSingleMeshPair;
public bool IsProcessed;
public TextureSet(
Dictionary<ulong, bool> textureIDSingleMeshPair,
bool isProcessed)
{
TextureIDSingleMeshPair = textureIDSingleMeshPair;
IsProcessed = isProcessed;
}
}
private Dictionary<int, TextureSet> textureSets;
private const int TEXTURES_TO_COPY_QUEUE_CAPACITY = 256;
private const int COPIES_PER_FRAME = 8;
// Fallback texture paths are indexed with ovrAvatarAssetLevelOfDetail
// We currently only use 1, 3 (mobile default), 5 (PC default)
private readonly string[] FALLBACK_TEXTURE_PATHS_DIFFUSE_ROUGHNESS = new string[]
{
"null",
PATH_LOWEST_DIFFUSE_ROUGHNESS,
"null",
PATH_MEDIUM_DIFFUSE_ROUGHNESS,
"null",
PATH_HIGHEST_DIFFUSE_ROUGHNESS,
};
private readonly string[] FALLBACK_TEXTURE_PATHS_NORMAL = new string[]
{
"null",
PATH_LOWEST_NORMAL,
"null",
PATH_MEDIUM_NORMAL,
"null",
PATH_HIGHEST_NORMAL,
};
private const string PATH_HIGHEST_DIFFUSE_ROUGHNESS = "FallbackTextures/fallback_diffuse_roughness_2048";
private const string PATH_MEDIUM_DIFFUSE_ROUGHNESS = "FallbackTextures/fallback_diffuse_roughness_1024";
private const string PATH_LOWEST_DIFFUSE_ROUGHNESS = "FallbackTextures/fallback_diffuse_roughness_256";
private const string PATH_HIGHEST_NORMAL = "FallbackTextures/fallback_normal_2048";
private const string PATH_MEDIUM_NORMAL = "FallbackTextures/fallback_normal_1024";
private const string PATH_LOWEST_NORMAL = "FallbackTextures/fallback_normal_256";
private const int GPU_TEXTURE_COPY_WAIT_TIME = 10;
public OvrAvatarTextureCopyManager()
{
texturesToCopy = new Queue<CopyTextureParams>(TEXTURES_TO_COPY_QUEUE_CAPACITY);
textureSets = new Dictionary<int, TextureSet>();
}
public void Update()
{
if (texturesToCopy.Count == 0)
{
return;
}
lock (texturesToCopy)
{
for (int i = 0; i < Mathf.Min(COPIES_PER_FRAME, texturesToCopy.Count); ++i)
{
CopyTexture(texturesToCopy.Dequeue());
}
}
}
public int GetTextureCount()
{
return texturesToCopy.Count;
}
public void CopyTexture(
Texture src,
Texture dst,
int mipLevel,
int mipSize,
int dstElement,
bool useQueue = true)
{
var copyTextureParams = new CopyTextureParams(src, dst, mipLevel, mipSize, dstElement);
if (useQueue)
{
lock (texturesToCopy)
{
if (texturesToCopy.Count < TEXTURES_TO_COPY_QUEUE_CAPACITY)
{
texturesToCopy.Enqueue(copyTextureParams);
}
else
{
// Queue is full so copy texture immediately
CopyTexture(copyTextureParams);
}
}
}
else
{
CopyTexture(copyTextureParams);
}
}
private void CopyTexture(CopyTextureParams copyTextureParams)
{
Graphics.CopyTexture(
copyTextureParams.Src,
0,
copyTextureParams.Mip,
copyTextureParams.Dst,
copyTextureParams.DstElement,
copyTextureParams.Mip);
}
public void AddTextureIDToTextureSet(int gameobjectID, ulong textureID, bool isSingleMesh)
{
if (!textureSets.ContainsKey(gameobjectID))
{
TextureSet newTextureSet = new TextureSet(new Dictionary<ulong, bool>(), false);
newTextureSet.TextureIDSingleMeshPair.Add(textureID, isSingleMesh);
textureSets.Add(gameobjectID, newTextureSet);
}
else
{
bool TexIDSingleMesh;
if (textureSets[gameobjectID].TextureIDSingleMeshPair.TryGetValue(textureID, out TexIDSingleMesh))
{
if (!TexIDSingleMesh && isSingleMesh)
{
textureSets[gameobjectID].TextureIDSingleMeshPair[textureID] = true;
}
}
else
{
textureSets[gameobjectID].TextureIDSingleMeshPair.Add(textureID, isSingleMesh);
}
}
}
// This is called by a fully loaded avatar using combined mesh to safely delete unused textures.
public void DeleteTextureSet(int gameobjectID)
{
TextureSet textureSetToDelete;
if (!textureSets.TryGetValue(gameobjectID, out textureSetToDelete))
{
return;
};
if (textureSetToDelete.IsProcessed)
{
return;
}
StartCoroutine(DeleteTextureSetCoroutine(textureSetToDelete, gameobjectID));
}
private IEnumerator DeleteTextureSetCoroutine(TextureSet textureSetToDelete, int gameobjectID)
{
// Wait a conservative amount of time for gpu upload to finish. Unity 2017 doesn't support async GPU calls,
// so this 10 second time is a very conservative delay for this process to occur, which should be <1 sec.
yield return new WaitForSeconds(GPU_TEXTURE_COPY_WAIT_TIME);
// Spin if an avatar is loading
while (OvrAvatarSDKManager.Instance.IsAvatarLoading())
{
yield return null;
}
// The avatar's texture set is compared against all other loaded or loading avatar texture sets.
foreach (var textureIdAndSingleMeshFlag in textureSetToDelete.TextureIDSingleMeshPair)
{
bool triggerDelete = !textureIdAndSingleMeshFlag.Value;
if (triggerDelete)
{
foreach (KeyValuePair<int, TextureSet> textureSet in textureSets)
{
if (textureSet.Key == gameobjectID)
{
continue;
}
foreach (var comparisonTextureIDSingleMeshPair in textureSet.Value.TextureIDSingleMeshPair)
{
// Mark the texture as not deletable if it's present in another set and that set hasn't been processed
// or that texture ID is marked as part of a single mesh component.
if (comparisonTextureIDSingleMeshPair.Key == textureIdAndSingleMeshFlag.Key &&
(!textureSet.Value.IsProcessed || comparisonTextureIDSingleMeshPair.Value))
{
triggerDelete = false;
break;
}
}
if (!triggerDelete)
{
break;
}
}
}
if (triggerDelete)
{
Texture2D textureToDelete = OvrAvatarComponent.GetLoadedTexture(textureIdAndSingleMeshFlag.Key);
if (textureToDelete != null)
{
AvatarLogger.Log("Deleting texture " + textureIdAndSingleMeshFlag.Key);
OvrAvatarSDKManager.Instance.DeleteAssetFromCache(textureIdAndSingleMeshFlag.Key);
Destroy(textureToDelete);
}
}
}
textureSetToDelete.IsProcessed = true;
textureSets.Remove(gameobjectID);
}
public void CheckFallbackTextureSet(ovrAvatarAssetLevelOfDetail lod)
{
if (FallbackTextureSets[(int)lod].Initialized)
{
return;
}
InitFallbackTextureSet(lod);
}
private void InitFallbackTextureSet(ovrAvatarAssetLevelOfDetail lod)
{
FallbackTextureSets[(int)lod].DiffuseRoughness = FallbackTextureSets[(int)lod].DiffuseRoughness =
Resources.Load<Texture2D>(FALLBACK_TEXTURE_PATHS_DIFFUSE_ROUGHNESS[(int)lod]);
FallbackTextureSets[(int)lod].Normal = FallbackTextureSets[(int)lod].Normal =
Resources.Load<Texture2D>(FALLBACK_TEXTURE_PATHS_NORMAL[(int)lod]);
FallbackTextureSets[(int)lod].Initialized = true;
}
}