Skip to content

Commit

Permalink
deactivate bindings and bindgroups via defines
Browse files Browse the repository at this point in the history
  • Loading branch information
pandrr committed Feb 4, 2025
1 parent dac8a5b commit 939d8fa
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
47 changes: 42 additions & 5 deletions src/core/cgp/cgp_binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,34 @@ import GPUBuffer from "./cgp_gpubuffer.js";
import { WebGpuContext } from "./cgp_state.js";
import Uniform from "./cgp_uniform.js";
import Shader from "./cgp_shader.js";
import CgUniform from "../cg/cg_uniform.js";

export default class Binding
{
#name = "";
#options = {};

/** @type {WebGpuContext} */
#cgp = null;
#options = null;

/** @type {Array<Uniform>} */
uniforms = [];

/** @type {Array<GPUBuffer>} */
cGpuBuffers = [];

/** @type {Shader} */
shader = null;

bindingInstances = [];
stageStr = "";
bindingType = "uniform";
isValid = true;
changed = 0;
#index = -1;

define = "";

/**
* Description
* @param {WebGpuContext} cgp
Expand All @@ -33,6 +45,7 @@ export default class Binding
this.#name = name;
this.#cgp = cgp;
this.#options = options;
this.define = options.define || "";
this.stageStr = options.stage;
if (options.bindingType) this.bindingType = options.bindingType; // "uniform", "storage", "read-only-storage",
if (this.stageStr == "frag") this.stage = GPUShaderStage.FRAGMENT;
Expand Down Expand Up @@ -118,10 +131,15 @@ export default class Binding
let typeStr = "strct_" + this.#name;
let name = this.#name;

if (!this.isActive)
{
str += "// " + typeStr + " " + this.#name + ": excluded because define " + this.#options.define + "\n";
return str;
}

if (this.uniforms.length === 0) return "// no uniforms in bindinggroup...?\n";

str += "// " + this.uniforms.length + " uniforms\n";
// if (this.#options.define) str += "#ifdef " + this.#options.define + "\n";

if (this.isStruct())
{
Expand Down Expand Up @@ -162,6 +180,12 @@ export default class Binding

getBindingGroupLayoutEntry()
{
if (!this.isActive)
{
console.log("not activeeee " + this.#name);
return null;
}

let label = "layout " + this.#name + " [";
for (let i = 0; i < this.uniforms.length; i++) label += this.uniforms[i].getName() + ",";
label += "]";
Expand Down Expand Up @@ -190,8 +214,19 @@ export default class Binding
return o;
}

getBindingGroupEntry(gpuDevice, inst)
get isActive()
{
if (!this.define) return true;
if (this.define && !this.shader.hasDefine(this.define)) return false;
return true;
}

/**
* @param {number} inst
*/
getBindingGroupEntry(inst)
{
if (!this.isActive) return null;
this.isValid = false;

const o = {
Expand Down Expand Up @@ -282,8 +317,10 @@ export default class Binding
*/
update(cgp, bindingIndex)
{
if (!this.isActive) return;

let b = this.bindingInstances[bindingIndex];
if (!b) b = this.getBindingGroupEntry(cgp.device, bindingIndex);
if (!b) b = this.getBindingGroupEntry(bindingIndex);

if (this.uniforms.length == 1 && this.uniforms[0].gpuBuffer)
{
Expand All @@ -303,7 +340,7 @@ export default class Binding
if (this.uniforms[0].getValue())
if (this.uniforms[0].getValue().gpuTexture)
{
this.bindingInstances[bindingIndex] = this.getBindingGroupEntry(this.uniforms[0]._cgp.device, bindingIndex);
this.bindingInstances[bindingIndex] = this.getBindingGroupEntry(bindingIndex);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/cgp/cgp_mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Logger } from "cables-shared-client";
import Pipeline from "./cgp_pipeline.js";
import CgMesh from "../cg/cg_mesh.js";

export default class Mesh extends CgMesh
export default class CgpMesh extends CgMesh
{
#log = new Logger("cgl_mesh");
needsPipelineUpdate = false;
Expand Down
17 changes: 13 additions & 4 deletions src/core/cgp/cgp_pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Logger } from "cables-shared-client";
import { WebGpuContext } from "./cgp_state.js";
import Shader from "./cgp_shader.js";
import Mesh from "./cgp_mesh.js";
import CgpShader from "./cgp_shader.js";

export default class Pipeline
{
Expand Down Expand Up @@ -218,15 +219,17 @@ export default class Pipeline
{
if (shader.bindingsVert[i].getSizeBytes() > 0)
{
bindingGroupEntries.push(shader.bindingsVert[i].getBindingGroupEntry(this.#cgp.device, shader.bindingCounter));
const entry = shader.bindingsVert[i].getBindingGroupEntry(shader.bindingCounter);
if (entry)bindingGroupEntries.push(entry);
}
else this.#log.log("shader defaultBindingVert size 0");
}
for (let i = 0; i < shader.bindingsFrag.length; i++)
{
if (shader.bindingsFrag[i].getSizeBytes() > 0)
{
bindingGroupEntries.push(shader.bindingsFrag[i].getBindingGroupEntry(this.#cgp.device, shader.bindingCounter));
const entry = shader.bindingsFrag[i].getBindingGroupEntry(shader.bindingCounter);
if (entry)bindingGroupEntries.push(entry);
}
else this.#log.log("shader defaultBindingFrag size 0");
}
Expand Down Expand Up @@ -269,6 +272,10 @@ export default class Pipeline
this.shaderNeedsPipelineUpdate = "";
}

/**
* @param {CgpShader} shader
* @param {Mesh} mesh
*/
getPipelineObject(shader, mesh)
{
this.bindingGroupLayoutEntries = [];
Expand All @@ -283,7 +290,8 @@ export default class Pipeline
{
if (shader.bindingsVert[i].getSizeBytes() > 0)
{
this.bindingGroupLayoutEntries.push(shader.bindingsVert[i].getBindingGroupLayoutEntry());
const entry = shader.bindingsVert[i].getBindingGroupLayoutEntry();
if (entry) this.bindingGroupLayoutEntries.push(entry);
}
else this.#log.log("shader defaultBindingVert size 0");
}
Expand All @@ -292,7 +300,8 @@ export default class Pipeline
{
if (shader.bindingsFrag[i].getSizeBytes() > 0)
{
this.bindingGroupLayoutEntries.push(shader.bindingsFrag[i].getBindingGroupLayoutEntry());
const entry = shader.bindingsFrag[i].getBindingGroupLayoutEntry();
if (entry) this.bindingGroupLayoutEntries.push(entry);
}
else this.#log.log("shader defaultBindingFrag size 0");
}
Expand Down
14 changes: 13 additions & 1 deletion src/core/cgp/cgp_shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CgShader } from "../cg/cg_shader.js";
import Binding from "./cgp_binding.js";
import { WebGpuContext } from "./cgp_state.js";

export default class Shader extends CgShader
export default class CgpShader extends CgShader
{

/**
Expand Down Expand Up @@ -35,8 +35,14 @@ export default class Shader extends CgShader
this.defaultBindingVert = new Binding(_cgp, "vsUniforms", { "stage": "vert", "bindingType": "uniform", "index": this._bindingIndexCount++ });
this.defaultBindingFrag = new Binding(_cgp, "fsUniforms", { "stage": "frag", "bindingType": "uniform", "index": this._bindingIndexCount++ });
this.defaultBindingComp = new Binding(_cgp, "computeUniforms", { "bindingType": "uniform", "index": this._bindingIndexCount++ });

/** @type {Array<Binding>} */
this.bindingsFrag = [this.defaultBindingFrag];

/** @type {Array<Binding>} */
this.bindingsVert = [this.defaultBindingVert];

/** @type {Array<Binding>} */
this.bindingsComp = [this.defaultBindingComp];

if (!this.compute)
Expand Down Expand Up @@ -158,6 +164,10 @@ export default class Shader extends CgShader
// let src = this._src;
let src = preproc(this._src, defs);

let strDefs = "";
for (let i = 0; i < this._defines.length; i++)
strDefs += "// #define " + this._defines[i] + "\n";

let bindingsHeadVert = "";
for (let i = 0; i < this.bindingsFrag.length; i++)
bindingsHeadVert += this.bindingsFrag[i].getShaderHeaderCode();
Expand All @@ -169,6 +179,8 @@ export default class Shader extends CgShader
src = bindingsHeadFrag + "\n\n////////////////\n\n" + bindingsHeadVert + "\n\n////////////////\n\n" + src;
src = this._replaceMods(src);

src = strDefs + "\n" + src;

return src;
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/cgp/cgp_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Shader from "./cgp_shader.js";
import defaultShaderSrcVert from "./cgl_shader_default.wgsl";
import Texture from "./cgp_texture.js";
import CgTexture from "../cg/cg_texture.js";
import Patch from "../core_patch.js";

// https://github.com/greggman/webgpu-utils
// https://developer.chrome.com/blog/from-webgl-to-webgpu/
Expand All @@ -20,7 +21,7 @@ class WebGpuContext extends CGState
{

/**
* @param {CABLES.Patch} _patch
* @param {Patch} _patch
*/
constructor(_patch)
{
Expand Down

0 comments on commit 939d8fa

Please sign in to comment.