@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
90 lines (74 loc) • 2.08 kB
JavaScript
import { assert } from "../../../../core/assert.js";
export class RenderGraphBuilder {
/**
*
* @type {RenderGraph}
* @private
*/
#graph = null;
/**
*
* @type {RenderPassNode}
* @private
*/
#node = null;
/**
*
* @param {RenderGraph} graph
* @param {RenderPassNode} node
*/
init(graph, node) {
this.#graph = graph;
this.#node = node;
}
/**
* Create a new resource
* @param {string} name
* @param {ResourceDescriptor} descriptor
* @returns {number} resource ID
*/
create(name, descriptor) {
const node = this.#graph.create_resource(name, descriptor);
// remember resource
this.#node.resource_creates.push(node);
// creation implies writing as well
this.#node.resource_writes.push(node);
return node;
}
/**
* Read an existing resource
* @param {number} resource
* @returns {number}
*/
read(resource) {
assert.isNonNegativeInteger(resource, 'resource');
return this.#node.read(resource);
}
/**
* Write a resource
* @param {number} resource
* @returns {number}
*/
write(resource) {
assert.isNonNegativeInteger(resource, 'resource');
const graph = this.#graph;
const node = this.#node;
if (graph.getResourceEntry(resource).isImported()) {
// comes from outside the graph, and we write to it. That's a side effect
node.has_side_effects = true;
}
if (node.creates(resource)) {
return node.write(resource);
} else {
// writing to a resource produces a renamed handle
node.read(resource);
return node.write(graph.clone_resource(resource));
}
}
/**
* Will force the node to be executed
*/
make_side_effect() {
this.#node.has_side_effects = true;
}
}