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 987f5fa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
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
37 changes: 35 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 Down Expand Up @@ -100,6 +101,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 +233,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 +246,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 Down Expand Up @@ -450,6 +466,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

0 comments on commit 987f5fa

Please sign in to comment.