Skip to content

Commit

Permalink
support Nodes buffer()
Browse files Browse the repository at this point in the history
  • Loading branch information
aardgoose committed Sep 22, 2023
1 parent ac901f1 commit 3ddeadc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/jsm/nodes/core/IndexNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class IndexNode extends Node {

constructor( scope ) {

super( 'uint' );
super( 'int' );

this.scope = scope;

Expand Down
8 changes: 4 additions & 4 deletions examples/jsm/renderers/webgl/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class WebGLBackend extends Backend {
const bindingData = this.get( binding );
const index = bindingData.index;

if ( binding.isUniformsGroup ) {
if ( binding.isUniformsGroup || binding.isUniformBuffer ) {

gl.bindBufferBase( gl.UNIFORM_BUFFER, index, bindingData.bufferGPU );

Expand Down Expand Up @@ -463,7 +463,7 @@ class WebGLBackend extends Backend {
const bindingData = this.get( binding );
const index = bindingData.index;

if ( binding.isUniformsGroup ) {
if ( binding.isUniformsGroup || binding.isUniformBuffer ) {

const location = gl.getUniformBlockIndex( programGPU, binding.name );
gl.uniformBlockBinding( programGPU, location, index );
Expand Down Expand Up @@ -537,7 +537,7 @@ class WebGLBackend extends Backend {

for ( const binding of bindings ) {

if ( binding.isUniformsGroup ) {
if ( binding.isUniformsGroup || binding.isUniformBuffer ) {

const bufferGPU = gl.createBuffer();
const data = binding.buffer;
Expand Down Expand Up @@ -571,7 +571,7 @@ class WebGLBackend extends Backend {

const gl = this.gl;

if ( binding.isUniformsGroup ) {
if ( binding.isUniformsGroup || binding.isUniformBuffer ) {

const bindingData = this.get( binding );
const bufferGPU = bindingData.bufferGPU;
Expand Down
44 changes: 42 additions & 2 deletions examples/jsm/renderers/webgl/nodes/GLSLNodeBuilder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MathNode, GLSLNodeParser, NodeBuilder, NodeMaterial } from '../../../nodes/Nodes.js';

import UniformBuffer from '../../common/UniformBuffer.js';
import UniformsGroup from '../../common/UniformsGroup.js';
import { NodeSampledTexture, NodeSampledCubeTexture } from '../../common/nodes/NodeSampledTexture.js';

Expand All @@ -21,6 +22,7 @@ class GLSLNodeBuilder extends NodeBuilder {
super( object, renderer, new GLSLNodeParser(), scene );

this.uniformsGroup = {};
this.bufferId = 0;

}

Expand Down Expand Up @@ -100,6 +102,15 @@ class GLSLNodeBuilder extends NodeBuilder {

snippet = `samplerCube ${uniform.name};`;

} else if ( uniform.type === 'buffer' ) {

const bufferNode = uniform.node;
const bufferType = this.getType( bufferNode.bufferType );
const bufferCount = bufferNode.bufferCount;

const bufferCountSnippet = bufferCount > 0 ? bufferCount : '';
snippet = `${bufferNode.name} {\n\t${bufferType} ${uniform.name}[${bufferCountSnippet}];\n};\n`;

} else {

const vectorType = this.getVectorType( uniform.type );
Expand Down Expand Up @@ -223,7 +234,10 @@ class GLSLNodeBuilder extends NodeBuilder {

for ( const varying of varyings ) {

snippet += `${varying.needsInterpolation ? 'out' : '/*out*/'} ${varying.type} ${varying.name};\n`;
const type = varying.type;
const flat = type === 'int' || type === 'uint' ? 'flat ' : '';

snippet += `${flat}${varying.needsInterpolation ? 'out' : '/*out*/'} ${type} ${varying.name};\n`;

}

Expand All @@ -233,7 +247,10 @@ class GLSLNodeBuilder extends NodeBuilder {

if ( varying.needsInterpolation ) {

snippet += `in ${varying.type} ${varying.name};\n`;
const type = varying.type;
const flat = type === 'int' || type === 'uint' ? 'flat ' : '';

snippet += `${flat}in ${type} ${varying.name};\n`;

}

Expand All @@ -251,6 +268,12 @@ class GLSLNodeBuilder extends NodeBuilder {

}

getInstanceIndex() {

return 'gl_InstanceID';

}

getFrontFacing() {

return 'gl_FrontFacing';
Expand Down Expand Up @@ -450,6 +473,23 @@ void main() {

this.bindings[ shaderStage ].push( uniformGPU );

} else if ( type === 'buffer' ) {

node.name = `NodeBuffer_${node.id}`;

const bindings = this.bindings[ shaderStage ];
const buffer = new UniformBuffer( node.name, node.value );

uniformNode.name = `buffer${node.id}`;

// add first textures in sequence and group for last
const lastBinding = bindings[ bindings.length - 1 ];
const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;

bindings.splice( index, 0, buffer );

uniformGPU = buffer;

} else {

let uniformsGroup = this.uniformsGroup[ shaderStage ];
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/renderers/webgpu/WebGPURenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WebGPURenderer extends Renderer {

let BackendClass;

if ( WebGPU.isAvailable() ) {
if ( ! WebGPU.isAvailable() ) {

BackendClass = WebGPUBackend;

Expand Down

0 comments on commit 3ddeadc

Please sign in to comment.