-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAssetManager.js
303 lines (245 loc) · 9.62 KB
/
AssetManager.js
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
292
293
294
295
296
297
298
299
300
301
import { CleanShaderSource,InsertMacrosToShader,RefactorFragShader,RefactorVertShader} from './OpenglShaders.js'
import {GetUniqueHash} from './Hash.js'
import {ExtractShaderUniforms,ExtractShaderAttributes} from './Shaders.js'
import * as Pop from './PopEngine.js'
// todo: turn asset manager into a class so client has to instance it,
// and can have multiple asset managers if required
const PopAssetManager = {};
export default PopAssetManager;
// gr:this import is crashing native
// need to fix this. Shaders or any type-specific asset stuff shouldn't really be in here anyway
//import * as Opengl from './PopWebOpenglApi.js'
// AssetCacheContexts[ContextHash][AssetName] = CachedAsset
PopAssetManager.AssetCacheContexts = {};
PopAssetManager.AssetPendingContexts = {}; // promises of pending loads
// put this somewhere else
// AssetFetchFunctions[Name] = function(Context)
// where the function takes a context (eg. render context) and returns the apporiate asset or throws if not loaded
PopAssetManager.AssetFetchFunctions = {};
PopAssetManager.AssetFetchAsyncFunctions = {};
// for shaders (later more files?) multiple-filenames => asset name need to be identifiable/split/joined but we
// need to distinguish them from valid filename chars. Not much in unix/osx is invalid...
PopAssetManager.AssetFilenameJoinString = ':';
// bindings if using as module
PopAssetManager.GetAsset = GetAsset;
PopAssetManager.RegisterShaderAssetFilename = RegisterShaderAssetFilename;
PopAssetManager.RegisterAssetFetchFunction = RegisterAssetFetchFunction;
PopAssetManager.RegisterAssetAsyncFetchFunction = RegisterAssetAsyncFetchFunction;
export function RegisterAssetFetchFunction(Filename,FetchFunction)
{
PopAssetManager.AssetFetchFunctions[Filename] = FetchFunction;
// auto invalidate the old asset, we're assuming there's a change
InvalidateAsset(Filename);
return Filename;
}
export function RegisterAssetAsyncFetchFunction(Filename,FetchFunction)
{
PopAssetManager.AssetFetchAsyncFunctions[Filename] = FetchFunction;
/*
if ( PopAssetManager.AssetFetchAsyncPending.hasOwnProperty(Filename) )
{
Pop.Warning(`Async asset function registered, and a load is pending, we need to handle this`);
}
*/
// auto invalidate the old asset, we're assuming there's a change
InvalidateAsset(Filename);
return Filename;
}
function OnAssetChanged()
{
}
export function HasFetchFunction(Name)
{
if ( PopAssetManager.AssetFetchAsyncFunctions.hasOwnProperty(Name) )
return true;
if ( PopAssetManager.AssetFetchFunctions.hasOwnProperty(Name) )
return true;
return false;
}
export function GetAsset(Name,RenderContext)
{
const Assets = PopAssetManager.AssetCacheContexts;
const Pendings = PopAssetManager.AssetPendingContexts;
const AssetFetchFunctions = PopAssetManager.AssetFetchFunctions;
const AssetFetchAsyncFunctions = PopAssetManager.AssetFetchAsyncFunctions;
const ContextKey = GetUniqueHash( RenderContext );
if ( !Assets.hasOwnProperty(ContextKey) )
Assets[ContextKey] = {};
if ( !Pendings.hasOwnProperty(ContextKey) )
Pendings[ContextKey] = {};
const ContextAssets = Assets[ContextKey];
const ContextPendings = Pendings[ContextKey];
// already loaded, return cached asset
if ( ContextAssets.hasOwnProperty(Name) )
return ContextAssets[Name];
if ( !HasFetchFunction(Name) )
throw `No known asset named ${Name} registered`;
Pop.Debug(`Generating asset ${Name} on context ${ContextKey}...`);
const Timer_Start = Pop.GetTimeNowMs();
// check if an async load is pending
if ( ContextPendings[Name] )
throw `Asset ${Name} on context ${ContextKey} is async-loading still...`;
function OnLoadedAsset(Asset)
{
// set cache
ContextAssets[Name] = Asset;
// delete pending
ContextPendings[Name] = null;
if ( Asset === undefined )
throw `Asset created for ${Name} on context ${ContextKey} is undefined`;
const Timer_Duration = Math.floor(Pop.GetTimeNowMs() - Timer_Start);
Pop.Debug(`Generating asset ${Name}(${typeof Asset}) on context ${ContextKey} took ${Timer_Duration}ms`);
Pop.Debug(`Completed asset=${ContextAssets[Name]}`);
OnAssetChanged( Name );
}
function OnFailedToLoadAsset(Error)
{
Pop.Warning(`todo: handle failed async loading of ${Name}! ${Error}`);
//delete ContextPendings[Name];
}
// start async load
if ( AssetFetchAsyncFunctions.hasOwnProperty(Name) )
{
const LoadFunc = AssetFetchAsyncFunctions[Name];
ContextPendings[Name] = LoadFunc( RenderContext );
ContextPendings[Name].then( OnLoadedAsset ).catch( OnFailedToLoadAsset );
throw `Asset ${Name} on context ${ContextKey} is now async loading...`;
}
else
{
// immediate load
const Asset = AssetFetchFunctions[Name]( RenderContext );
OnLoadedAsset(Asset);
return ContextAssets[Name];
}
}
function ShaderMacrosToHash(Macros)
{
if ( !Macros )
return ``;
let Hash = `_`;
for ( let [Macro,Value] of Object.entries(Macros) )
{
Hash += `${Macro}=${Value}_`;
}
return Hash;
}
export async function CompileShader(RenderContext,Name,VertSource,FragSource,Macros)
{
//FragSource = RefactorFragShader(FragSource);
//VertSource = RefactorVertShader(VertSource);
FragSource = InsertMacrosToShader(FragSource,Macros);
VertSource = InsertMacrosToShader(VertSource,Macros);
const ShaderUniforms = ExtractShaderUniforms( FragSource, VertSource );
const ShaderAttributes = ExtractShaderAttributes( FragSource, VertSource );
Pop.Debug(`LoadAndCompileShader ${Name}. Uniforms; ${JSON.stringify(ShaderUniforms)}`);
const Shader = await RenderContext.CreateShader( VertSource, FragSource, ShaderUniforms, ShaderAttributes );
return Shader;
}
// this returns the "asset name"
// gr: should this be somewhere else, not in the core asset manager?
export function RegisterShaderAssetFilename(FragFilename,VertFilename,ShaderMacros,/*ShaderUniforms,*/ShaderAttribs)
{
// we now extract these with regex
// gr: 3rd arg is now defines/macros
//if ( ShaderUniforms )
// Pop.Debug(`RegisterShaderAssetFilename(${FragFilename}): ShaderUniforms no longer need to be supplied`);
if ( ShaderAttribs )
Pop.Debug(`RegisterShaderAssetFilename(${FragFilename}): ShaderAttribs no longer need to be supplied`);
// we use / as its not a valid filename char
let AssetName = FragFilename+PopAssetManager.AssetFilenameJoinString+VertFilename;
AssetName += ShaderMacrosToHash(ShaderMacros);
async function LoadAndCompileShader(RenderContext)
{
const ShaderName = AssetName;
let FragSource = await Pop.LoadFileAsStringAsync(FragFilename);
let VertSource = await Pop.LoadFileAsStringAsync(VertFilename);
return await CompileShader( RenderContext, AssetName, VertSource, FragSource, ShaderMacros );
}
RegisterAssetAsyncFetchFunction(AssetName,LoadAndCompileShader);
return AssetName;
}
// modify object, but don't store a reference to it! otherwise it wont garbage collect
const ContextUniqueHashCounter = [1000];
function GetContextUniqueHash(Object)
{
let HashPrefix = 'object_hash#';
// the string is the hash
if ( typeof Object == 'string' )
return Object;
if ( typeof Object != 'object' )
throw "Need to work out how to store unique hash on a " + (typeof Object);
// objects are passed by reference, so we can add a hash
if ( Object._UniqueHash !== undefined )
return Object._UniqueHash;
ContextUniqueHashCounter[0]++;
Object._UniqueHash = HashPrefix + ContextUniqueHashCounter[0];
// Pop.Debug("Created new hash for object: " + Object._UniqueHash );
return Object._UniqueHash;
}
export function InvalidateAsset(Filename,ForceInvalidation=false,NewFileMeta=undefined)
{
const Assets = PopAssetManager.AssetCacheContexts;
if ( !Filename )
throw `InvalidateAsset(${Filename}) invalid filename`;
Pop.Debug(`InvalidateAsset ${Filename}`);
function InvalidateAssetInContext(Context)
{
const ContextKey = GetContextUniqueHash( Context );
const ContextAssets = Assets[ContextKey];
// gr: cope with assetnames containing multiple filenames
function ShouldInvalidateKey(AssetName)
{
const Filenames = AssetName.split(PopAssetManager.AssetFilenameJoinString);
const AnyMatches = Filenames.some( f => f == Filename );
return AnyMatches;
}
const InvalidateKeys = Object.keys( ContextAssets ).filter( ShouldInvalidateKey );
if ( !InvalidateKeys.length )
{
// Pop.Debug("Context",Context," has no matching assets for ",Filename,Object.keys(ContextAssets));
return;
}
function InvalidateKey(AssetName)
{
// if we've reached this point, we have an asset
const Asset = ContextAssets[AssetName];
// for streaming assets, we dont want to just destroy & reload the asset
// if we dont need to (ie, we already have enough data, like with audio, avoid re-seeking and clicking)
if ( Asset.ShouldInvalidateWithNewFile )
{
if ( !ForceInvalidation && NewFileMeta )
{
if ( !Asset.ShouldInvalidateWithNewFile(Context,NewFileMeta.Contents,NewFileMeta) )
{
Pop.Debug(`Skipped asset invalidation ${Filename}`);
return;
}
}
}
// delete existing asset
// if it has a cleanup func, call it
if ( Asset.Free )
{
Pop.Debug(`Freeing asset ${AssetName}...`);
try
{
Asset.Free();
}
catch(e)
{
Pop.Debug(`Erroring freeing asset ${AssetName}: ${e}`);
}
}
// delete from context cache (note: must use array accessor!)
delete ContextAssets[AssetName];
// Pop.Debug(`Invalidated ${AssetName} on ${Context}`,Context);
}
InvalidateKeys.forEach( InvalidateKey );
}
const AssetContexts = Object.keys(Assets);
AssetContexts.forEach( InvalidateAssetInContext );
// todo; this should be OnAssetChanged(AssetName), not just filename (eg. shaders)
// so code above should accumulate unique asset name and then call here after
OnAssetChanged(Filename);
}