This commit is contained in:
parent
05c8a39bd8
commit
c40c5d97db
74 changed files with 863 additions and 669 deletions
|
|
@ -1,48 +1,38 @@
|
|||
export const createBindGroupCache = <K1 extends object, K2 extends object>(
|
||||
factory: (key1: K1, key2: K2) => GPUBindGroup
|
||||
): ((key1: K1, key2: K2) => GPUBindGroup) => {
|
||||
const outer = new WeakMap<K1, WeakMap<K2, GPUBindGroup>>();
|
||||
return (key1, key2) => {
|
||||
let inner = outer.get(key1);
|
||||
if (!inner) {
|
||||
inner = new WeakMap();
|
||||
outer.set(key1, inner);
|
||||
}
|
||||
const cached = inner.get(key2);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
const bindGroup = factory(key1, key2);
|
||||
inner.set(key2, bindGroup);
|
||||
return bindGroup;
|
||||
};
|
||||
type BindGroupCacheKeys = readonly [object, ...object[]];
|
||||
|
||||
interface BindGroupCacheNode {
|
||||
bindGroup?: GPUBindGroup;
|
||||
children: WeakMap<object, BindGroupCacheNode>;
|
||||
}
|
||||
|
||||
const createNode = (): BindGroupCacheNode => ({
|
||||
children: new WeakMap(),
|
||||
});
|
||||
|
||||
const getOrCreateNode = (
|
||||
children: WeakMap<object, BindGroupCacheNode>,
|
||||
key: object
|
||||
): BindGroupCacheNode => {
|
||||
let node = children.get(key);
|
||||
if (!node) {
|
||||
node = createNode();
|
||||
children.set(key, node);
|
||||
}
|
||||
return node;
|
||||
};
|
||||
|
||||
export const createBindGroupCache3 = <
|
||||
K1 extends object,
|
||||
K2 extends object,
|
||||
K3 extends object,
|
||||
>(
|
||||
factory: (key1: K1, key2: K2, key3: K3) => GPUBindGroup
|
||||
): ((key1: K1, key2: K2, key3: K3) => GPUBindGroup) => {
|
||||
const outer = new WeakMap<K1, WeakMap<K2, WeakMap<K3, GPUBindGroup>>>();
|
||||
return (key1, key2, key3) => {
|
||||
let mid = outer.get(key1);
|
||||
if (!mid) {
|
||||
mid = new WeakMap();
|
||||
outer.set(key1, mid);
|
||||
export const createBindGroupCache = <Keys extends BindGroupCacheKeys>(
|
||||
factory: (...keys: Keys) => GPUBindGroup
|
||||
): ((...keys: Keys) => GPUBindGroup) => {
|
||||
const root = new WeakMap<object, BindGroupCacheNode>();
|
||||
|
||||
return (...keys) => {
|
||||
let node = getOrCreateNode(root, keys[0]);
|
||||
for (const key of keys.slice(1)) {
|
||||
node = getOrCreateNode(node.children, key);
|
||||
}
|
||||
let inner = mid.get(key2);
|
||||
if (!inner) {
|
||||
inner = new WeakMap();
|
||||
mid.set(key2, inner);
|
||||
}
|
||||
const cached = inner.get(key3);
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
const bindGroup = factory(key1, key2, key3);
|
||||
inner.set(key3, bindGroup);
|
||||
return bindGroup;
|
||||
|
||||
node.bindGroup ??= factory(...keys);
|
||||
return node.bindGroup;
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue