Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions src/materials/nodes/NodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,15 +592,7 @@ class NodeMaterial extends Material {

} else {

let fragmentNode = this.fragmentNode;

if ( fragmentNode.isOutputStructNode !== true ) {

fragmentNode = vec4( fragmentNode );

}

resultNode = this.setupOutput( builder, fragmentNode );
resultNode = this.setupOutput( builder, this.fragmentNode );

}

Expand Down
64 changes: 58 additions & 6 deletions src/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import CubeRenderTarget from '../../renderers/common/CubeRenderTarget.js';

import BindGroup from '../../renderers/common/BindGroup.js';

import { REVISION, IntType, UnsignedIntType, LinearFilter, LinearMipmapNearestFilter, NearestMipmapLinearFilter, LinearMipmapLinearFilter, NormalBlending } from '../../constants.js';
import { REVISION, IntType, UnsignedIntType, LinearFilter, LinearMipmapNearestFilter, NearestMipmapLinearFilter, LinearMipmapLinearFilter, NormalBlending, RedFormat, RGFormat, RGBFormat, RedIntegerFormat, RGIntegerFormat, RGBIntegerFormat } from '../../constants.js';
import { RenderTarget } from '../../core/RenderTarget.js';
import { Color } from '../../math/Color.js';
import { Vector2 } from '../../math/Vector2.js';
Expand Down Expand Up @@ -534,6 +534,60 @@ class NodeBuilder {

}

/**
* Returns the type of the color output based on the renderer's render target.
*
* @return {string} The type.
*/
getOutputType() {

let type = 'vec4';

const renderTarget = this.renderer.getRenderTarget();

if ( renderTarget !== null ) {

const renderTargetType = renderTarget.texture.type;
const renderTargetFormat = renderTarget.texture.format;

let typeStr = 'vec';

if ( renderTargetType === IntType ) {

typeStr = 'ivec';

} else if ( renderTargetType === UnsignedIntType ) {

typeStr = 'uvec';

}

if ( renderTargetFormat === RedFormat || renderTargetFormat === RedIntegerFormat ) {

if ( renderTargetType === IntType ) type = 'int';
else if ( renderTargetType === UnsignedIntType ) type = 'uint';
else type = 'float';

} else if ( renderTargetFormat === RGFormat || renderTargetFormat === RGIntegerFormat ) {

type = `${ typeStr }2`;

} else if ( renderTargetFormat === RGBFormat || renderTargetFormat === RGBIntegerFormat ) {

type = `${ typeStr }3`;

} else {

type = `${ typeStr }4`;

}

}

return type;

}

/**
* Returns the output struct name which is required by
* {@link OutputStructNode}.
Expand Down Expand Up @@ -1468,12 +1522,10 @@ class NodeBuilder {

const type = texture.type;

if ( texture.isDataTexture ) {
if ( texture.isDepthTexture === true ) return 'float';

if ( type === IntType ) return 'int';
if ( type === UnsignedIntType ) return 'uint';

}
if ( type === IntType ) return 'int';
if ( type === UnsignedIntType ) return 'uint';

return 'float';

Expand Down
6 changes: 3 additions & 3 deletions src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ ${ flowData.code }

if ( outputSnippet.length === 0 ) {

outputSnippet.push( 'layout( location = 0 ) out vec4 fragColor;' );
outputSnippet.push( `layout( location = 0 ) out ${ this.getOutputType() } fragColor;` );

}

Expand Down Expand Up @@ -1529,14 +1529,14 @@ void main() {
if ( shaderStage === 'vertex' ) {

flow += 'gl_Position = ';
flow += `${ flowSlotData.result };`;
flow += `${ this.format( flowSlotData.result, mainNode.getNodeType( this ), 'vec4' ) };`;

} else if ( shaderStage === 'fragment' ) {

if ( ! node.outputNode.isOutputStructNode ) {

flow += 'fragColor = ';
flow += `${ flowSlotData.result };`;
flow += `${ this.format( flowSlotData.result, mainNode.getNodeType( this ), this.getOutputType() ) };`;

}

Expand Down
16 changes: 14 additions & 2 deletions src/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2126,7 +2126,7 @@ ${ flowData.code }

} else {

let structSnippet = '\t@location( 0 ) color: vec4<f32>';
let structSnippet = `\t@location( 0 ) color: ${ this.getOutputType() }`;

const builtins = this.getBuiltins( 'output' );

Expand All @@ -2136,7 +2136,7 @@ ${ flowData.code }
stageData.structs += this._getWGSLStruct( 'OutputStruct', structSnippet );
stageData.structs += '\nvar<private> output : OutputStruct;';

flow += `output.color = ${ flowSlotData.result };\n\n\treturn output;`;
flow += `output.color = ${ this.format( flowSlotData.result, mainNode.getNodeType( this ), this.getOutputType() ) };\n\n\treturn output;`;

}

Expand Down Expand Up @@ -2260,6 +2260,18 @@ ${ flowData.code }

}

/**
* Returns the type of the color output based on the renderer's render target.
*
* @return {string} The WGSL type.
*/
getOutputType() {

return this.getType( super.getOutputType() );

}


/**
* Whether the requested feature is available or not.
*
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/webgpu/utils/WebGPUBindingUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ class WebGPUBindingUtils {

}

} else if ( binding.texture.isDataTexture || binding.texture.isDataArrayTexture || binding.texture.isData3DTexture || binding.texture.isStorageTexture ) {
} else {

const type = binding.texture.type;

Expand Down
Loading