BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Avatar / Scripts / OvrAvatarTextureCopyManager.cs

OvrAvatarTextureCopyManager.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OvrAvatarTextureCopyManager.cs 9.6 KiB Download Raw
1using System.Collections;
2using System.Collections.Generic;
3using Oculus.Avatar;
4using UnityEngine;
5
6public class OvrAvatarTextureCopyManager : MonoBehaviour
7{
8 [System.Serializable]
9 public struct FallbackTextureSet
10 {
11 public bool Initialized;
12 public Texture2D DiffuseRoughness;
13 public Texture2D Normal;
14 }
15 // Fallback texture sets are indexed with ovrAvatarAssetLevelOfDetail.
16 // We currently only use 1, 3 (mobile default), 5 (PC default).
17 public FallbackTextureSet[] FallbackTextureSets = new FallbackTextureSet[(int)ovrAvatarAssetLevelOfDetail.Highest + 1];
18
19 struct CopyTextureParams
20 {
21 public Texture Src;
22 public Texture Dst;
23 public int Mip;
24 public int SrcSize;
25 public int DstElement;
26
27 public CopyTextureParams(
28 Texture src,
29 Texture dst,
30 int mip,
31 int srcSize,
32 int dstElement)
33 {
34 Src = src;
35 Dst = dst;
36 Mip = mip;
37 SrcSize = srcSize;
38 DstElement = dstElement;
39 }
40 }
41 private Queue<CopyTextureParams> texturesToCopy;
42
43 public struct TextureSet
44 {
45 // Contains all texture asset IDs that are part of an avatar spec.
46 // Used by DeleteTextureSet().
47 // Textures that are part of combined mesh avatars can be safely deleted once they have been
48 // uploaded to the texture arrays.
49 // Textures that are part of single component meshes will remain in memory.
50 public Dictionary<ulong, bool> TextureIDSingleMeshPair;
51 public bool IsProcessed;
52
53 public TextureSet(
54 Dictionary<ulong, bool> textureIDSingleMeshPair,
55 bool isProcessed)
56 {
57 TextureIDSingleMeshPair = textureIDSingleMeshPair;
58 IsProcessed = isProcessed;
59 }
60 }
61 private Dictionary<int, TextureSet> textureSets;
62
63 private const int TEXTURES_TO_COPY_QUEUE_CAPACITY = 256;
64 private const int COPIES_PER_FRAME = 8;
65
66 // Fallback texture paths are indexed with ovrAvatarAssetLevelOfDetail
67 // We currently only use 1, 3 (mobile default), 5 (PC default)
68 private readonly string[] FALLBACK_TEXTURE_PATHS_DIFFUSE_ROUGHNESS = new string[]
69 {
70 "null",
71 PATH_LOWEST_DIFFUSE_ROUGHNESS,
72 "null",
73 PATH_MEDIUM_DIFFUSE_ROUGHNESS,
74 "null",
75 PATH_HIGHEST_DIFFUSE_ROUGHNESS,
76 };
77 private readonly string[] FALLBACK_TEXTURE_PATHS_NORMAL = new string[]
78 {
79 "null",
80 PATH_LOWEST_NORMAL,
81 "null",
82 PATH_MEDIUM_NORMAL,
83 "null",
84 PATH_HIGHEST_NORMAL,
85 };
86
87 private const string PATH_HIGHEST_DIFFUSE_ROUGHNESS = "FallbackTextures/fallback_diffuse_roughness_2048";
88 private const string PATH_MEDIUM_DIFFUSE_ROUGHNESS = "FallbackTextures/fallback_diffuse_roughness_1024";
89 private const string PATH_LOWEST_DIFFUSE_ROUGHNESS = "FallbackTextures/fallback_diffuse_roughness_256";
90 private const string PATH_HIGHEST_NORMAL = "FallbackTextures/fallback_normal_2048";
91 private const string PATH_MEDIUM_NORMAL = "FallbackTextures/fallback_normal_1024";
92 private const string PATH_LOWEST_NORMAL = "FallbackTextures/fallback_normal_256";
93
94 private const int GPU_TEXTURE_COPY_WAIT_TIME = 10;
95
96 public OvrAvatarTextureCopyManager()
97 {
98 texturesToCopy = new Queue<CopyTextureParams>(TEXTURES_TO_COPY_QUEUE_CAPACITY);
99 textureSets = new Dictionary<int, TextureSet>();
100 }
101
102 public void Update()
103 {
104 if (texturesToCopy.Count == 0)
105 {
106 return;
107 }
108
109 lock (texturesToCopy)
110 {
111 for (int i = 0; i < Mathf.Min(COPIES_PER_FRAME, texturesToCopy.Count); ++i)
112 {
113 CopyTexture(texturesToCopy.Dequeue());
114 }
115 }
116 }
117
118 public int GetTextureCount()
119 {
120 return texturesToCopy.Count;
121 }
122
123 public void CopyTexture(
124 Texture src,
125 Texture dst,
126 int mipLevel,
127 int mipSize,
128 int dstElement,
129 bool useQueue = true)
130 {
131 var copyTextureParams = new CopyTextureParams(src, dst, mipLevel, mipSize, dstElement);
132
133 if (useQueue)
134 {
135 lock (texturesToCopy)
136 {
137 if (texturesToCopy.Count < TEXTURES_TO_COPY_QUEUE_CAPACITY)
138 {
139 texturesToCopy.Enqueue(copyTextureParams);
140 }
141 else
142 {
143 // Queue is full so copy texture immediately
144 CopyTexture(copyTextureParams);
145 }
146 }
147 }
148 else
149 {
150 CopyTexture(copyTextureParams);
151 }
152 }
153
154 private void CopyTexture(CopyTextureParams copyTextureParams)
155 {
156 Graphics.CopyTexture(
157 copyTextureParams.Src,
158 0,
159 copyTextureParams.Mip,
160 copyTextureParams.Dst,
161 copyTextureParams.DstElement,
162 copyTextureParams.Mip);
163 }
164
165 public void AddTextureIDToTextureSet(int gameobjectID, ulong textureID, bool isSingleMesh)
166 {
167 if (!textureSets.ContainsKey(gameobjectID))
168 {
169 TextureSet newTextureSet = new TextureSet(new Dictionary<ulong, bool>(), false);
170 newTextureSet.TextureIDSingleMeshPair.Add(textureID, isSingleMesh);
171 textureSets.Add(gameobjectID, newTextureSet);
172 }
173 else
174 {
175 bool TexIDSingleMesh;
176 if (textureSets[gameobjectID].TextureIDSingleMeshPair.TryGetValue(textureID, out TexIDSingleMesh))
177 {
178 if (!TexIDSingleMesh && isSingleMesh)
179 {
180 textureSets[gameobjectID].TextureIDSingleMeshPair[textureID] = true;
181 }
182 }
183 else
184 {
185 textureSets[gameobjectID].TextureIDSingleMeshPair.Add(textureID, isSingleMesh);
186 }
187 }
188 }
189
190 // This is called by a fully loaded avatar using combined mesh to safely delete unused textures.
191 public void DeleteTextureSet(int gameobjectID)
192 {
193 TextureSet textureSetToDelete;
194 if (!textureSets.TryGetValue(gameobjectID, out textureSetToDelete))
195 {
196 return;
197 };
198
199 if (textureSetToDelete.IsProcessed)
200 {
201 return;
202 }
203
204 StartCoroutine(DeleteTextureSetCoroutine(textureSetToDelete, gameobjectID));
205 }
206
207 private IEnumerator DeleteTextureSetCoroutine(TextureSet textureSetToDelete, int gameobjectID)
208 {
209 // Wait a conservative amount of time for gpu upload to finish. Unity 2017 doesn't support async GPU calls,
210 // so this 10 second time is a very conservative delay for this process to occur, which should be <1 sec.
211 yield return new WaitForSeconds(GPU_TEXTURE_COPY_WAIT_TIME);
212
213 // Spin if an avatar is loading
214 while (OvrAvatarSDKManager.Instance.IsAvatarLoading())
215 {
216 yield return null;
217 }
218
219 // The avatar's texture set is compared against all other loaded or loading avatar texture sets.
220 foreach (var textureIdAndSingleMeshFlag in textureSetToDelete.TextureIDSingleMeshPair)
221 {
222 bool triggerDelete = !textureIdAndSingleMeshFlag.Value;
223 if (triggerDelete)
224 {
225 foreach (KeyValuePair<int, TextureSet> textureSet in textureSets)
226 {
227 if (textureSet.Key == gameobjectID)
228 {
229 continue;
230 }
231
232 foreach (var comparisonTextureIDSingleMeshPair in textureSet.Value.TextureIDSingleMeshPair)
233 {
234 // Mark the texture as not deletable if it's present in another set and that set hasn't been processed
235 // or that texture ID is marked as part of a single mesh component.
236 if (comparisonTextureIDSingleMeshPair.Key == textureIdAndSingleMeshFlag.Key &&
237 (!textureSet.Value.IsProcessed || comparisonTextureIDSingleMeshPair.Value))
238 {
239 triggerDelete = false;
240 break;
241 }
242 }
243
244 if (!triggerDelete)
245 {
246 break;
247 }
248 }
249 }
250
251 if (triggerDelete)
252 {
253 Texture2D textureToDelete = OvrAvatarComponent.GetLoadedTexture(textureIdAndSingleMeshFlag.Key);
254 if (textureToDelete != null)
255 {
256 AvatarLogger.Log("Deleting texture " + textureIdAndSingleMeshFlag.Key);
257 OvrAvatarSDKManager.Instance.DeleteAssetFromCache(textureIdAndSingleMeshFlag.Key);
258 Destroy(textureToDelete);
259 }
260 }
261 }
262 textureSetToDelete.IsProcessed = true;
263 textureSets.Remove(gameobjectID);
264 }
265
266 public void CheckFallbackTextureSet(ovrAvatarAssetLevelOfDetail lod)
267 {
268 if (FallbackTextureSets[(int)lod].Initialized)
269 {
270 return;
271 }
272
273 InitFallbackTextureSet(lod);
274 }
275
276 private void InitFallbackTextureSet(ovrAvatarAssetLevelOfDetail lod)
277 {
278 FallbackTextureSets[(int)lod].DiffuseRoughness = FallbackTextureSets[(int)lod].DiffuseRoughness =
279 Resources.Load<Texture2D>(FALLBACK_TEXTURE_PATHS_DIFFUSE_ROUGHNESS[(int)lod]);
280 FallbackTextureSets[(int)lod].Normal = FallbackTextureSets[(int)lod].Normal =
281 Resources.Load<Texture2D>(FALLBACK_TEXTURE_PATHS_NORMAL[(int)lod]);
282 FallbackTextureSets[(int)lod].Initialized = true;
283 }
284}