Debian Codegraph @

src/scene/castleviewport_warmup_cache.inc

raw
1 {%MainUnit castleviewport.pas}
2 {
3 Copyright 2020-2022 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 {$ifdef read_interface}
18
19 {$endif read_interface}
20
21 {$ifdef read_implementation}
22
23 { TSceneFromCache ------------------------------------------------------------ }
24
25 type
26 { Like TCastleScene but loads/unloads model in a special way, from X3DCache.
27 This allows to utilize this cache when loading, in TCastleScene.Load. }
28 TSceneFromCache = class(TCastleScene)
29 strict private
30 FRootNodeFromCache: TX3DRootNode;
31 public
32 procedure LoadFromCache(const AURL: String);
33 destructor Destroy; override;
34 end;
35
36 procedure TSceneFromCache.LoadFromCache(const AURL: String);
37 var
38 TimeStart: TCastleProfilerTime;
39 begin
40 { Do not load node again, if it is already in the cache.
41 It would cause warnings
42 Warning: X3D node xxx is already part of another TCastleScene instance
43 if user uses the same URL twice in warmup_cache specification. }
44 if X3DCache.IsCached(AURL) then Exit;
45
46 TimeStart := Profiler.Start('Loading "' + UriDisplay(AURL) + '" (warming up cache)');
47 try
48 FRootNodeFromCache := X3DCache.LoadNode(AURL);
49 finally Profiler.Stop(TimeStart) end;
50 Load(FRootNodeFromCache, false);
51 end;
52
53 destructor TSceneFromCache.Destroy;
54 begin
55 RootNode := nil; // unassign RootNode, as X3DCache.FreeNode may free it
56 X3DCache.FreeNode(FRootNodeFromCache);
57 inherited;
58 end;
59
60 { TSceneCache -------------------------------------------------------------- }
61
62 var
63 SceneCacheId: QWord;
64
65 type
66 TSceneCache = class
67 class procedure Event(const Cache: TWarmupCache;
68 const Element: TDOMElement; const ElementBaseUrl: String);
69 end;
70
71 class procedure TSceneCache.Event(const Cache: TWarmupCache;
72 const Element: TDOMElement; const ElementBaseUrl: String);
73 var
74 URL: String;
75 Scene: TSceneFromCache;
76 TempViewport: TCastleViewport;
77 begin
78 URL := Element.AttributeURL('url', ElementBaseUrl);
79 Scene := TSceneFromCache.Create(nil);
80 Cache.OwnedObjects.Add(Scene);
81 Inc(SceneCacheId);
82 Cache.Name := 'SceneCache' + IntToStr(SceneCacheId);
83
84 // Scene.URL := URL; // loads the scene
85 // loads the scene using cache, this way other scenes may reload it by DeepCopy
86 Scene.LoadFromCache(URL);
87
88 TempViewport := TCastleViewport.Create(nil);
89 try
90 // loads the textures inside scene
91 TempViewport.PrepareResources(Scene, TCastleViewport.DefaultPrepareOptions - [prRenderSelf]);
92 finally FreeAndNil(TempViewport) end;
93 end;
94
95 procedure InitializeWarmupCache;
96 begin
97 WarmupCacheFormats.RegisterFormat('scene', {$ifdef FPC}@{$endif}TSceneCache{$ifdef FPC}(nil){$endif}.Event);
98 end;
99
100 {$endif read_implementation}