|
@@ -1,36 +1,10 @@
|
|
|
-import WebGPUUniformsGroup from './WebGPUUniformsGroup.js';
|
|
|
-import { FloatUniform, Vector3Uniform } from './WebGPUUniform.js';
|
|
|
-import WebGPUSampler from './WebGPUSampler.js';
|
|
|
-import { WebGPUSampledTexture } from './WebGPUSampledTexture.js';
|
|
|
+import WebGPUNodeUniformsGroup from './WebGPUNodeUniformsGroup.js';
|
|
|
+import { FloatNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform } from './WebGPUNodeUniform.js';
|
|
|
+import WebGPUSampler from '../WebGPUSampler.js';
|
|
|
+import { WebGPUSampledTexture } from '../WebGPUSampledTexture.js';
|
|
|
|
|
|
-import NodeSlot from '../nodes/core/NodeSlot.js';
|
|
|
-import NodeBuilder from '../nodes/core/NodeBuilder.js';
|
|
|
-
|
|
|
-class WebGPUNodeUniformsGroup extends WebGPUUniformsGroup {
|
|
|
-
|
|
|
- constructor( shaderStage ) {
|
|
|
-
|
|
|
- super( 'nodeUniforms' );
|
|
|
-
|
|
|
- let shaderStageVisibility;
|
|
|
-
|
|
|
- if ( shaderStage === 'vertex' ) shaderStageVisibility = GPUShaderStage.VERTEX;
|
|
|
- else if ( shaderStage === 'fragment' ) shaderStageVisibility = GPUShaderStage.FRAGMENT;
|
|
|
-
|
|
|
- this.setVisibility( shaderStageVisibility );
|
|
|
-
|
|
|
- //this.setOnBeforeUpdate( this._onBeforeUpdate );
|
|
|
-
|
|
|
- }
|
|
|
- /*
|
|
|
- _onBeforeUpdate( object, camera ) {
|
|
|
-
|
|
|
- const material = object.material;
|
|
|
-
|
|
|
- }
|
|
|
- */
|
|
|
-
|
|
|
-}
|
|
|
+import NodeSlot from '../../nodes/core/NodeSlot.js';
|
|
|
+import NodeBuilder from '../../nodes/core/NodeBuilder.js';
|
|
|
|
|
|
class WebGPUNodeBuilder extends NodeBuilder {
|
|
|
|
|
@@ -41,8 +15,11 @@ class WebGPUNodeBuilder extends NodeBuilder {
|
|
|
this.bindingIndex = 2;
|
|
|
this.bindings = { vertex: [], fragment: [] };
|
|
|
|
|
|
- this.uniformsGroup = {};
|
|
|
+ this.attributeIndex = 1;
|
|
|
+ this.varyIndex = 0;
|
|
|
|
|
|
+ this.uniformsGroup = {};
|
|
|
+
|
|
|
this._parseMaterial();
|
|
|
|
|
|
}
|
|
@@ -129,11 +106,19 @@ class WebGPUNodeBuilder extends NodeBuilder {
|
|
|
|
|
|
if ( type === 'float' ) {
|
|
|
|
|
|
- uniform = new FloatUniform( uniformNode );
|
|
|
+ uniform = new FloatNodeUniform( uniformNode );
|
|
|
+
|
|
|
+ } else if ( type === 'vec2' ) {
|
|
|
+
|
|
|
+ uniform = new Vector2NodeUniform( uniformNode );
|
|
|
|
|
|
} else if ( type === 'vec3' ) {
|
|
|
|
|
|
- uniform = new Vector3Uniform( uniformNode.name, uniformNode.value );
|
|
|
+ uniform = new Vector3NodeUniform( uniformNode );
|
|
|
+
|
|
|
+ } else if ( type === 'vec4' ) {
|
|
|
+
|
|
|
+ uniform = new Vector4NodeUniform( uniformNode );
|
|
|
|
|
|
} else {
|
|
|
|
|
@@ -153,12 +138,66 @@ class WebGPUNodeBuilder extends NodeBuilder {
|
|
|
|
|
|
}
|
|
|
|
|
|
- getUniformsOutput( shaderStage ) {
|
|
|
+ getAttributesHeaderSnippet( shaderStage ) {
|
|
|
+
|
|
|
+ let snippet = '';
|
|
|
+
|
|
|
+ const attributes = this.attributes;
|
|
|
+
|
|
|
+ let attributeIndex = this.attributeIndex;
|
|
|
+ let varyIndex = this.varyIndex;
|
|
|
+
|
|
|
+ for ( let name in attributes ) {
|
|
|
+
|
|
|
+ let attribute = attributes[ name ];
|
|
|
+
|
|
|
+ let type = attribute.type;
|
|
|
+ let property = attribute.property;
|
|
|
+
|
|
|
+ if ( shaderStage === 'vertex' ) {
|
|
|
+
|
|
|
+ snippet += `layout(location = ${attributeIndex++}) in ${type} ${name};`;
|
|
|
+ snippet += `layout(location = ${varyIndex++}) out ${type} ${property};`;
|
|
|
+
|
|
|
+ } else if ( shaderStage === 'fragment' ) {
|
|
|
+
|
|
|
+ snippet += `layout(location = ${varyIndex++}) in ${type} ${property};`;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return snippet;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ getAttributesBodySnippet( shaderStage ) {
|
|
|
+
|
|
|
+ let snippet = '';
|
|
|
+
|
|
|
+ const attributes = this.attributes;
|
|
|
+
|
|
|
+ for ( let name in attributes ) {
|
|
|
+
|
|
|
+ let attribute = attributes[ name ];
|
|
|
+
|
|
|
+ let property = attribute.property;
|
|
|
+
|
|
|
+ snippet += `${property} = ${name};`;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return snippet;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ getUniformsHeaderSnippet( shaderStage ) {
|
|
|
|
|
|
const uniforms = this.uniforms[ shaderStage ];
|
|
|
|
|
|
- let uniformsCode = '';
|
|
|
- let uniformGroupCode = '';
|
|
|
+ let snippet = '';
|
|
|
+ let groupSnippet = '';
|
|
|
|
|
|
let bindingIndex = this.bindingIndex;
|
|
|
|
|
@@ -166,47 +205,43 @@ class WebGPUNodeBuilder extends NodeBuilder {
|
|
|
|
|
|
if (uniform.type === 'texture') {
|
|
|
|
|
|
- uniformsCode += `layout(set = 0, binding = ${bindingIndex++}) uniform sampler ${uniform.name}_sampler;`;
|
|
|
- uniformsCode += `layout(set = 0, binding = ${bindingIndex++}) uniform texture2D ${uniform.name};`;
|
|
|
+ snippet += `layout(set = 0, binding = ${bindingIndex++}) uniform sampler ${uniform.name}_sampler;`;
|
|
|
+ snippet += `layout(set = 0, binding = ${bindingIndex++}) uniform texture2D ${uniform.name};`;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
- if (!uniformGroupCode) {
|
|
|
+ if (!groupSnippet) {
|
|
|
|
|
|
- uniformGroupCode = `layout(set = 0, binding = ${bindingIndex++}) uniform NodeUniforms {`;
|
|
|
+ groupSnippet = `layout(set = 0, binding = ${bindingIndex++}) uniform NodeUniforms {`;
|
|
|
|
|
|
}
|
|
|
|
|
|
- uniformGroupCode += `uniform ${uniform.type} ${uniform.name};`;
|
|
|
+ groupSnippet += `uniform ${uniform.type} ${uniform.name};`;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- if (uniformGroupCode) {
|
|
|
+ if (groupSnippet) {
|
|
|
|
|
|
- uniformGroupCode += `} nodeUniforms;`;
|
|
|
+ groupSnippet += `} nodeUniforms;`;
|
|
|
|
|
|
- uniformsCode += uniformGroupCode;
|
|
|
+ snippet += groupSnippet;
|
|
|
|
|
|
}
|
|
|
|
|
|
- console.log( uniformsCode );
|
|
|
-
|
|
|
- return uniformsCode;
|
|
|
+ return snippet;
|
|
|
|
|
|
}
|
|
|
|
|
|
- buildShader( shaderStage, code ) {
|
|
|
+ buildShader( code, snippet ) {
|
|
|
|
|
|
// use regex maybe for security?
|
|
|
const versionStrIndex = code.indexOf( "\n" );
|
|
|
|
|
|
let finalCode = code.substr( 0, versionStrIndex ) + "\n\n";
|
|
|
|
|
|
- const shaderCodes = this.build( shaderStage );
|
|
|
-
|
|
|
- finalCode += shaderCodes.defines;
|
|
|
+ finalCode += snippet;
|
|
|
|
|
|
finalCode += code.substr( versionStrIndex );
|
|
|
|
|
@@ -215,9 +250,11 @@ class WebGPUNodeBuilder extends NodeBuilder {
|
|
|
}
|
|
|
|
|
|
parse( vertexShader, fragmentShader ) {
|
|
|
+
|
|
|
+ const shader = this.build();
|
|
|
|
|
|
- vertexShader = this.buildShader( 'vertex', vertexShader );
|
|
|
- fragmentShader = this.buildShader( 'fragment', fragmentShader );
|
|
|
+ vertexShader = this.buildShader( vertexShader, shader.vertex );
|
|
|
+ fragmentShader = this.buildShader( fragmentShader, shader.fragment );
|
|
|
|
|
|
return {
|
|
|
vertexShader,
|