forked from StrandedKitty/streets-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResource.ts
54 lines (47 loc) · 1.63 KB
/
Resource.ts
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
import Node from "./Node";
import ResourceDescriptor from "./ResourceDescriptor";
import PhysicalResourceBuilder from "./PhysicalResourceBuilder";
import PhysicalResource from "./PhysicalResource";
import ResourcePool from "~/lib/render-graph/PhysicalResourcePool";
export default abstract class Resource<TDescriptor extends ResourceDescriptor, TBuilder extends PhysicalResourceBuilder<any>> extends Node {
public isRenderable = false;
public descriptor: TDescriptor;
public physicalResourceBuilder: TBuilder;
public isTransient: boolean;
public isUsedExternally: boolean;
public attachedPhysicalResource: TBuilder['type'] = null;
public attachedPhysicalResourceId: string = null;
protected constructor(
{
name,
descriptor,
physicalResourceBuilder,
isTransient,
isUsedExternally
}: {
name: string;
descriptor: TDescriptor;
physicalResourceBuilder: TBuilder;
isTransient: boolean;
isUsedExternally: boolean;
}
) {
super(name);
this.descriptor = descriptor;
this.physicalResourceBuilder = physicalResourceBuilder;
this.isTransient = isTransient;
this.isUsedExternally = isUsedExternally;
}
private createPhysicalResource(): TBuilder['type'] {
return this.physicalResourceBuilder.createFromResourceDescriptor(this.descriptor);
}
public attachPhysicalResource(pool: ResourcePool): void {
const id = this.descriptor.deserialize();
this.attachedPhysicalResourceId = id;
this.attachedPhysicalResource = pool.getPhysicalResource(id) ?? this.createPhysicalResource();
}
public resetAttachedPhysicalResource(): void {
this.attachedPhysicalResource = null;
this.attachedPhysicalResourceId = null;
}
}