Skip to content

Commit

Permalink
webgopu
Browse files Browse the repository at this point in the history
  • Loading branch information
pandrr committed Feb 3, 2025
1 parent 4c824ef commit 4943bcd
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 44 deletions.
9 changes: 9 additions & 0 deletions src/core/cg/cg_mesh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default class CgMesh
{
_name = "unknown";

constructor()
{
}

}
2 changes: 1 addition & 1 deletion src/core/cg/cg_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class CGState extends Events
* @param {number} h
* @param {boolean} ignorestyle
*/
setSize(w, h, ignorestyle)
setSize(w, h, ignorestyle = false)
{
this.cgCanvas.setSize(w, h, ignorestyle);
}
Expand Down
5 changes: 3 additions & 2 deletions src/core/cgl/cgl_mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Uniform } from "./cgl_shader_uniform.js";
import { CONSTANTS } from "./constants.js";
import { Geometry } from "../cg/cg_geom.js";
import { Context } from "./cgl_state.js";
import CgMesh from "../cg/cg_mesh.js";

const MESH = {};
MESH.lastMesh = null;
Expand All @@ -22,7 +23,7 @@ MESH.lastMesh = null;
* }
*
*/
class Mesh
class Mesh extends CgMesh
{

/** @type {Geometry} */
Expand All @@ -35,6 +36,7 @@ class Mesh
*/
constructor(_cgl, __geom, _options)
{
super();
this._cgl = _cgl;

let options = _options || {};
Expand All @@ -53,7 +55,6 @@ class Mesh
this.opId = options.opId || "";
this._preWireframeGeom = null;
this.addVertexNumbers = false;
this._name = "unknown";

this.feedBackAttributes = [];
this.setGeom(__geom);
Expand Down
17 changes: 16 additions & 1 deletion src/core/cgp/cgp_binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,19 @@ export default class Binding
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())
{
str += "struct " + typeStr + "\n";
str += "{\n";
for (let i = 0; i < this.uniforms.length; i++)
{

str += " " + this.uniforms[i].name + ": " + this.uniforms[i].getWgslTypeStr();
if (i != this.uniforms.length - 1)str += ",";
str += "\n";

}
str += "};\n";
}
Expand All @@ -152,6 +155,8 @@ export default class Binding

str += name + ": " + typeStr + ";\n";

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

return str;
}

Expand Down Expand Up @@ -206,6 +211,7 @@ export default class Binding
{
if (this.uniforms[0].getValue() && this.uniforms[0].getValue().gpuTexture) o.resource = this.uniforms[0].getValue().gpuTexture.createView();
else o.resource = this.#cgp.getEmptyTexture().createView();// CABLES.emptyCglTexture.createView();

}
else if (this.uniforms.length == 1 && this.uniforms[0].getType() == "sampler")
{
Expand All @@ -218,6 +224,7 @@ export default class Binding
};

if (this.uniforms[0].getValue())
{
if (!this.uniforms[0].getValue().getSampler)
{
this._log.error("uniform texture does not have function getSampler... not a WebGpu Texture?");
Expand All @@ -226,8 +233,11 @@ export default class Binding
{
smplDesc = this.uniforms[0].getValue().getSampler();
const sampler = this.uniforms[0]._cgp.device.createSampler(smplDesc);
o.resource = sampler;
if (sampler)o.resource = sampler;

}

}
}
else
{
Expand All @@ -243,6 +253,11 @@ export default class Binding
this.isValid = true;
this.bindingInstances[inst] = o;

if (o.hasOwnProperty("resource"))
{
console.log("rrrrrr ", o.label, o.resource);
}

return o;
}

Expand Down
18 changes: 8 additions & 10 deletions src/core/cgp/cgp_mesh.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { Logger } from "cables-shared-client";
import Pipeline from "./cgp_pipeline.js";
import CgMesh from "../cg/cg_mesh.js";

export default class Mesh
export default class Mesh extends CgMesh
{
#log = new Logger("cgl_mesh");
#needsPipelineUpdate = false;
needsPipelineUpdate = false;

constructor(_cgp, __geom)
{
super();

this._cgp = _cgp;
this._geom = null;
this.numIndex = 0;
this.instances = 1;

this._pipe = new Pipeline(this._cgp, "new mesh");
this._pipe = new Pipeline(this._cgp, "new mesh " + __geom.name);
this._numNonIndexed = 0;
this._positionBuffer = null;
this._bufVerticesIndizes = null;
Expand All @@ -22,11 +25,6 @@ export default class Mesh
if (__geom) this.setGeom(__geom);
}

get needsPipelineUpdate()
{
return this.#needsPipelineUpdate;
}

_createBuffer(device, data, usage)
{
let bo = {
Expand All @@ -51,7 +49,7 @@ export default class Mesh
*/
setGeom(geom, removeRef)
{
this.#needsPipelineUpdate = true;
this.needsPipelineUpdate = true;
this._geom = geom;
this._disposeAttributes();

Expand All @@ -70,7 +68,7 @@ export default class Mesh

_disposeAttributes()
{
this.#needsPipelineUpdate = true;
this.needsPipelineUpdate = true;
for (let i = 0; i < this._attributes.length; i++)
{
this._attributes[i].buffer.destroy();
Expand Down
39 changes: 30 additions & 9 deletions src/core/cgp/cgp_pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default class Pipeline
#bindGroups = [];
#shaderListeners = [];
#old = {};
#errorCount = 0;

shaderNeedsPipelineUpdate = "";

Expand Down Expand Up @@ -111,10 +112,14 @@ export default class Pipeline

if (shader.needsPipelineUpdate)
{
needsRebuildReason = "mesh needs update: " + shader.needsPipelineUpdate;
needsRebuildReason = "shader needs update: " + shader.needsPipelineUpdate;
shader.needsPipelineUpdate = "";
}
if (mesh.needsPipelineUpdate)needsRebuildReason = "mesh needs update";
if (mesh.needsPipelineUpdate)
{
needsRebuildReason = "mesh needs update";
mesh.needsPipelineUpdate = false;
}
if (this.shaderNeedsPipelineUpdate)needsRebuildReason = "shader was recompiled: " + this.shaderNeedsPipelineUpdate;

if (this.#pipeCfg)
Expand Down Expand Up @@ -167,27 +172,27 @@ export default class Pipeline
"cfg": this.#pipeCfg,
"bindingGroupLayoutEntries": this.bindingGroupLayoutEntries
};

if (needsRebuildReason != "")
{
this.#log.log("rebuild pipe", needsRebuildReason);
// console.log("needsRebuildReason");
console.log("rebuild pipe", needsRebuildReason);
this.#cgp.pushErrorScope("createPipeline", { "logger": this.#log });

this.#bindGroups = [];

const pipeObj = this.getPipelineObject(shader, mesh);
this.#pipeCfg = this.getPipelineObject(shader, mesh);

this.#old.device = this.#cgp.device;
this.#old.shader = shader;
this.#old.mesh = mesh;

try
{
this.#renderPipeline = this.#cgp.device.createRenderPipeline(pipeObj);
this.#renderPipeline = this.#cgp.device.createRenderPipeline(this.#pipeCfg);
}
catch (e)
{
console.error("catc", e.message, pipeObj);
console.error("catc", e.message, this.#pipeCfg);
this.#isValid = false;
}

Expand Down Expand Up @@ -227,12 +232,28 @@ export default class Pipeline
}

const bg = {
"label": "label2",
"label": this.#name,
"layout": this.bindGroupLayout,
"entries": bindingGroupEntries
};

this.#bindGroups[shader.bindingCounter] = this.#cgp.device.createBindGroup(bg);
try
{
this.#bindGroups[shader.bindingCounter] = this.#cgp.device.createBindGroup(bg);
}
catch (e)
{
this.#errorCount++;
if (this.#errorCount == 3) console.log("stopping error logging for cgp pipeline");
if (this.#errorCount >= 3) return;

console.log(bg);
console.error(e);
// console.log(shader);
console.log("error mesh:", this.#name);

}

}

this._bindUniforms(shader, shader.bindingCounter);
Expand Down
1 change: 1 addition & 0 deletions src/core/cgp/cgp_shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export default class Shader extends CgShader
for (let i = 0; i < this._defines.length; i++)
defs[this._defines[i][0]] = this._defines[i][1] || true;

// let src = this._src;
let src = preproc(this._src, defs);

let bindingsHeadVert = "";
Expand Down
8 changes: 6 additions & 2 deletions src/core/cgp/cgp_uniform.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ export default class Uniform extends CgUniform
super(__shader, __type, __name, _value, _port2, _port3, _port4, _structUniformName, _structName, _propertyName);
this._cgp = __shader._cgp;

if (!_value)
if (!_value || (_value.get && !_value.get()))
{
// if (this.getType() == "m4") this._value = mat4.create();
if (this.getType() == "t") this._value = this._cgp.getEmptyTexture();
if (this.getType() == "t")
{
this._value = this._cgp.getEmptyTexture();
console.log("TEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", this._value);
}
// else if (this.getType() == "2f") this._value = [0, 0];
// else if (this.getType() == "4f") this._value = [0, 1, 0, 1];
// else if (this.getType() == "3f") this._value = [0, 1, 0];
Expand Down
24 changes: 12 additions & 12 deletions src/ops/base/Ops.Trigger.Repeat_v2/Ops.Trigger.Repeat_v2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
const
exe=op.inTrigger("Execute"),
num=op.inValueInt("Repeats",5),
dir=op.inSwitch("Direction",['Forward','Backward'],'Forward'),
next=op.outTrigger("Next"),
idx=op.addOutPort(new CABLES.Port(op,"index"));
exe = op.inTrigger("Execute"),
num = op.inValueInt("Repeats", 5),
dir = op.inSwitch("Direction", ["Forward", "Backward"], "Forward"),
next = op.outTrigger("Next"),
idx = op.addOutPort(new CABLES.Port(op, "index"));

dir.onChange=updateDir;
dir.onChange = updateDir;
updateDir();

function updateDir()
{
if(dir.get()=="Forward") exe.onTriggered=forward;
else exe.onTriggered=backward;
if (dir.get() == "Forward") exe.onTriggered = forward;
else exe.onTriggered = backward;
}

function forward()
{
const max=Math.floor(num.get());
const max = Math.floor(num.get());

for(var i=0;i<max;i++)
for (let i = 0; i < max; i++)
{
idx.set(i);
next.trigger();
Expand All @@ -27,8 +27,8 @@ function forward()

function backward()
{
const numi=Math.floor(num.get());
for(var i=numi-1;i>-1;i--)
const numi = Math.floor(num.get());
for (let i = numi - 1; i > -1; i--)
{
idx.set(i);
next.trigger();
Expand Down
18 changes: 11 additions & 7 deletions src/ops/base/Ops.Trigger.Repeat_v2/Ops.Trigger.Repeat_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,38 @@
"layout": {
"portsIn": [
{
"type": "1",
"type": 1,
"name": "Execute"
},
{
"type": "0",
"type": 0,
"name": "Repeats",
"subType": "integer"
},
{
"type": "0",
"type": 0,
"name": "Direction index",
"values": [
"Forward",
"Backward"
],
"subType": "integer"
}
],
"portsOut": [
{
"type": "1",
"type": 1,
"name": "Next"
},
{
"type": "0",
"type": 0,
"name": "index",
"subType": "number"
}
]
},
"summary": "Triggers all ops below x times (for loop / while)",
"issues": "",

"youtubeids": [
"jiOLZaMUH78",
"DW9U5tv1GHM"
Expand All @@ -53,5 +56,6 @@
}
]
},
"exampleProjectId": "VFAfgg"
"exampleProjectId": "VFAfgg",
"license": "MIT"
}

0 comments on commit 4943bcd

Please sign in to comment.