Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/lmj01/three.js into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
lmj01 committed Jul 5, 2024
2 parents c53f65a + 673dd29 commit 86580c5
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 157 deletions.
203 changes: 128 additions & 75 deletions build/three.webgpu.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/three.webgpu.min.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions examples/jsm/nodes/accessors/TextureNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class TextureNode extends UniformNode {
this.referenceNode = null;

this._value = value;
this._matrixValue = null;

this.setUpdateMatrix( uvNode === null );

Expand Down Expand Up @@ -88,9 +89,10 @@ class TextureNode extends UniformNode {

getTransformedUV( uvNode ) {

const texture = this.value;
if ( this._matrixValue === null ) this._matrixValue = uniform( this.value.matrix );

return this._matrixValue.mul( vec3( uvNode, 1 ) ).xy;

return uniform( texture.matrix ).mul( vec3( uvNode, 1 ) ).xy;

}

Expand Down Expand Up @@ -369,6 +371,9 @@ class TextureNode extends UniformNode {
update() {

const texture = this.value;
const matrixTexture = this._matrixValue;

if ( matrixTexture !== null ) matrixTexture.value = texture.matrix;

if ( texture.matrixAutoUpdate === true ) {

Expand Down
20 changes: 16 additions & 4 deletions examples/jsm/nodes/display/PassNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ class PassTextureNode extends TextureNode {

class PassNode extends TempNode {

constructor( scope, scene, camera ) {
constructor( scope, scene, camera, options = {} ) {

super( 'vec4' );

this.scope = scope;
this.scene = scene;
this.camera = camera;
this.options = options;

this._pixelRatio = 1;
this._width = 1;
Expand All @@ -56,7 +57,7 @@ class PassNode extends TempNode {
//depthTexture.type = FloatType;
depthTexture.name = 'PostProcessingDepth';

const renderTarget = new RenderTarget( this._width * this._pixelRatio, this._height * this._pixelRatio, { type: HalfFloatType } );
const renderTarget = new RenderTarget( this._width * this._pixelRatio, this._height * this._pixelRatio, { type: HalfFloatType, ...options } );
renderTarget.texture.name = 'PostProcessing';
renderTarget.depthTexture = depthTexture;

Expand Down Expand Up @@ -124,7 +125,18 @@ class PassNode extends TempNode {

}

setup() {
setup( { renderer } ) {

this.renderTarget.samples = this.options.samples === undefined ? renderer.samples : this.options.samples;

// Disable MSAA for WebGL backend for now
if ( renderer.backend.isWebGLBackend === true ) {

this.renderTarget.samples = 0;

}

this.renderTarget.depthTexture.isMultisampleRenderTargetTexture = this.renderTarget.samples > 1;

return this.scope === PassNode.COLOR ? this.getTextureNode() : this.getDepthNode();

Expand Down Expand Up @@ -194,7 +206,7 @@ PassNode.DEPTH = 'depth';

export default PassNode;

export const pass = ( scene, camera ) => nodeObject( new PassNode( PassNode.COLOR, scene, camera ) );
export const pass = ( scene, camera, options ) => nodeObject( new PassNode( PassNode.COLOR, scene, camera, options ) );
export const texturePass = ( pass, texture ) => nodeObject( new PassTextureNode( pass, texture ) );
export const depthPass = ( scene, camera ) => nodeObject( new PassNode( PassNode.DEPTH, scene, camera ) );

Expand Down
60 changes: 43 additions & 17 deletions examples/jsm/renderers/common/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ import Background from './Background.js';
import Nodes from './nodes/Nodes.js';
import Color4 from './Color4.js';
import ClippingContext from './ClippingContext.js';
import { Scene, Frustum, Matrix4, Vector2, Vector3, Vector4, DoubleSide, BackSide, FrontSide, SRGBColorSpace, NoColorSpace, NoToneMapping, LinearFilter, LinearSRGBColorSpace, RenderTarget, HalfFloatType, RGBAFormat } from 'three';
import { NodeMaterial } from '../../nodes/Nodes.js';
import QuadMesh from '../../objects/QuadMesh.js';
import QuadMesh from './QuadMesh.js';
import RenderBundles from './RenderBundles.js';

import { NodeMaterial } from '../../nodes/Nodes.js';

import { Scene } from '../../scenes/Scene.js';
import { Frustum } from '../../math/Frustum.js';
import { Matrix4 } from '../../math/Matrix4.js';
import { Vector2 } from '../../math/Vector2.js';
import { Vector3 } from '../../math/Vector3.js';
import { Vector4 } from '../../math/Vector4.js';
import { RenderTarget } from '../../core/RenderTarget.js';
import { DoubleSide, BackSide, FrontSide, SRGBColorSpace, NoColorSpace, NoToneMapping, LinearFilter, LinearSRGBColorSpace, HalfFloatType, RGBAFormat } from '../../constants.js';

const _scene = new Scene();
const _drawingBufferSize = new Vector2();
const _screen = new Vector4();
const _frustum = new Frustum();
const _projScreenMatrix = new Matrix4();
const _vector3 = new Vector3();
const _quad = new QuadMesh( new NodeMaterial() );

class Renderer {

Expand All @@ -35,15 +43,18 @@ class Renderer {

const {
logarithmicDepthBuffer = false,
alpha = true
alpha = true,
antialias = false,
samples = 0
} = parameters;

// public

this.domElement = backend.getDomElement();

this.backend = backend;

this.samples = samples || ( antialias === true ) ? 4 : 0;

this.autoClear = true;
this.autoClearColor = true;
this.autoClearDepth = true;
Expand All @@ -67,10 +78,6 @@ class Renderer {

this.info = new Info();

// nodes

this.toneMappingNode = null;

// internals

this._pixelRatio = 1;
Expand All @@ -94,6 +101,8 @@ class Renderer {
this._textures = null;
this._background = null;

this._quad = new QuadMesh( new NodeMaterial() );

this._currentRenderContext = null;

this._opaqueSort = null;
Expand Down Expand Up @@ -430,7 +439,7 @@ class Renderer {

const { currentColorSpace } = this;

const useToneMapping = this._renderTarget === null && ( this.toneMapping !== NoToneMapping || this.toneMappingNode !== null );
const useToneMapping = this._renderTarget === null && ( this.toneMapping !== NoToneMapping );
const useColorSpace = currentColorSpace !== LinearSRGBColorSpace && currentColorSpace !== NoColorSpace;

if ( useToneMapping === false && useColorSpace === false ) return null;
Expand All @@ -451,7 +460,7 @@ class Renderer {
generateMipmaps: false,
minFilter: LinearFilter,
magFilter: LinearFilter,
samples: this.backend.parameters.antialias ? 4 : 0
samples: this.samples
} );

frameBufferTarget.isPostProcessingRenderTarget = true;
Expand Down Expand Up @@ -521,6 +530,7 @@ class Renderer {

this.info.calls ++;
this.info.render.calls ++;
this.info.render.frameCalls ++;

nodeFrame.renderId = this.info.calls;

Expand Down Expand Up @@ -673,9 +683,16 @@ class Renderer {

this.setRenderTarget( outputRenderTarget, activeCubeFace, activeMipmapLevel );

_quad.material.fragmentNode = this._nodes.getOutputNode( renderTarget.texture );
const quad = this._quad;

if ( this._nodes.hasOutputChange( renderTarget.texture ) ) {

this._renderScene( _quad, _quad.camera, false );
quad.material.fragmentNode = this._nodes.getOutputNode( renderTarget.texture );
quad.material.needsUpdate = true;

}

this._renderScene( quad, quad.camera, false );

}

Expand Down Expand Up @@ -956,8 +973,16 @@ class Renderer {
// If a color space transform or tone mapping is required,
// the clear operation clears the intermediate renderTarget texture, but does not update the screen canvas.

_quad.material.fragmentNode = this._nodes.getOutputNode( renderTarget.texture );
this._renderScene( _quad, _quad.camera, false );
const quad = this._quad;

if ( this._nodes.hasOutputChange( renderTarget.texture ) ) {

quad.material.fragmentNode = this._nodes.getOutputNode( renderTarget.texture );
quad.material.needsUpdate = true;

}

this._renderScene( quad, quad.camera, false );

}

Expand Down Expand Up @@ -1079,7 +1104,7 @@ class Renderer {

this.info.calls ++;
this.info.compute.calls ++;
this.info.compute.computeCalls ++;
this.info.compute.frameCalls ++;

nodeFrame.renderId = this.info.calls;

Expand All @@ -1089,6 +1114,7 @@ class Renderer {
const pipelines = this._pipelines;
const bindings = this._bindings;
const nodes = this._nodes;

const computeList = Array.isArray( computeNodes ) ? computeNodes : [ computeNodes ];

if ( computeList[ 0 ] === undefined || computeList[ 0 ].isComputeNode !== true ) {
Expand Down
24 changes: 5 additions & 19 deletions examples/jsm/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ class WebGPUBackend extends Backend {
// some parameters require default values other than "undefined"
this.parameters.alpha = ( parameters.alpha === undefined ) ? true : parameters.alpha;

this.parameters.antialias = ( parameters.antialias === true );

if ( this.parameters.antialias === true ) {

this.parameters.sampleCount = ( parameters.sampleCount === undefined ) ? 4 : parameters.sampleCount;

} else {

this.parameters.sampleCount = 1;

}

this.parameters.requiredLimits = ( parameters.requiredLimits === undefined ) ? {} : parameters.requiredLimits;

this.trackTimestamp = ( parameters.trackTimestamp === true );
Expand Down Expand Up @@ -153,8 +141,6 @@ class WebGPUBackend extends Backend {

let descriptor = this.defaultRenderPassdescriptor;

const antialias = this.parameters.antialias;

if ( descriptor === null ) {

const renderer = this.renderer;
Expand All @@ -170,7 +156,7 @@ class WebGPUBackend extends Backend {

const colorAttachment = descriptor.colorAttachments[ 0 ];

if ( antialias === true ) {
if ( this.renderer.samples > 0 ) {

colorAttachment.view = this.colorBuffer.createView();

Expand All @@ -186,7 +172,7 @@ class WebGPUBackend extends Backend {

const colorAttachment = descriptor.colorAttachments[ 0 ];

if ( antialias === true ) {
if ( this.renderer.samples > 0 ) {

colorAttachment.resolveTarget = this.context.getCurrentTexture().createView();

Expand Down Expand Up @@ -269,7 +255,7 @@ class WebGPUBackend extends Backend {
const depthTextureData = this.get( renderContext.depthTexture );

const depthStencilAttachment = {
view: depthTextureData.texture.createView(),
view: depthTextureData.texture.createView()
};

descriptor = {
Expand Down Expand Up @@ -959,7 +945,7 @@ class WebGPUBackend extends Backend {

const utils = this.utils;

const sampleCount = utils.getSampleCount( renderObject.context );
const sampleCount = utils.getSampleCountRenderContext( renderObject.context );
const colorSpace = utils.getCurrentColorSpace( renderObject.context );
const colorFormat = utils.getCurrentColorFormat( renderObject.context );
const depthStencilFormat = utils.getCurrentDepthStencilFormat( renderObject.context );
Expand Down Expand Up @@ -1024,7 +1010,7 @@ class WebGPUBackend extends Backend {
material.stencilFail, material.stencilZFail, material.stencilZPass,
material.stencilFuncMask, material.stencilWriteMask,
material.side,
utils.getSampleCount( renderContext ),
utils.getSampleCountRenderContext( renderContext ),
utils.getCurrentColorSpace( renderContext ), utils.getCurrentColorFormat( renderContext ), utils.getCurrentDepthStencilFormat( renderContext ),
utils.getPrimitiveTopology( object, material ),
renderObject.clippingContextVersion
Expand Down
16 changes: 12 additions & 4 deletions examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class WGSLNodeBuilder extends NodeBuilder {

this._include( 'repeatWrapping' );

const dimension = `textureDimensions( ${ textureProperty }, 0 )`;
const dimension = texture.isMultisampleRenderTargetTexture === true ? `textureDimensions( ${ textureProperty } )` : `textureDimensions( ${ textureProperty }, 0 )`;

return `textureLoad( ${ textureProperty }, threejs_repeatWrapping( ${ uvSnippet }, ${ dimension } ), i32( ${ levelSnippet } ) )`;

Expand Down Expand Up @@ -269,7 +269,7 @@ class WGSLNodeBuilder extends NodeBuilder {

isUnfilterable( texture ) {

return this.getComponentTypeFromTexture( texture ) !== 'float' || ( texture.isDataTexture === true && texture.type === FloatType );
return this.getComponentTypeFromTexture( texture ) !== 'float' || ( texture.isDataTexture === true && texture.type === FloatType ) || texture.isMultisampleRenderTargetTexture === true;

}

Expand Down Expand Up @@ -835,6 +835,14 @@ ${ flowData.code }

let textureType;

let multisampled = '';

if ( texture.isMultisampleRenderTargetTexture === true ) {

multisampled = '_multisampled';

}

if ( texture.isCubeTexture === true ) {

textureType = 'texture_cube<f32>';
Expand All @@ -845,7 +853,7 @@ ${ flowData.code }

} else if ( texture.isDepthTexture === true ) {

textureType = 'texture_depth_2d';
textureType = `texture_depth${multisampled}_2d`;

} else if ( texture.isVideoTexture === true ) {

Expand All @@ -866,7 +874,7 @@ ${ flowData.code }

const componentPrefix = this.getComponentTypeFromTexture( texture ).charAt( 0 );

textureType = `texture_2d<${ componentPrefix }32>`;
textureType = `texture${multisampled}_2d<${ componentPrefix }32>`;

}

Expand Down
6 changes: 6 additions & 0 deletions examples/jsm/renderers/webgpu/utils/WebGPUBindingUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ class WebGPUBindingUtils {

const texture = {}; // GPUTextureBindingLayout

if ( binding.texture.isMultisampleRenderTargetTexture === true ) {

texture.multisampled = true;

}

if ( binding.texture.isDepthTexture ) {

texture.sampleType = GPUTextureSampleType.Depth;
Expand Down
17 changes: 1 addition & 16 deletions examples/jsm/renderers/webgpu/utils/WebGPUPipelineUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,7 @@ class WebGPUPipelineUtils {

_getSampleCount( renderObjectContext ) {

let sampleCount = this.backend.utils.getSampleCount( renderObjectContext );

if ( sampleCount > 1 ) {

// WebGPU only supports power-of-two sample counts and 2 is not a valid value
sampleCount = Math.pow( 2, Math.floor( Math.log2( sampleCount ) ) );

if ( sampleCount === 2 ) {

sampleCount = 4;

}

}

return sampleCount;
return this.backend.utils.getSampleCountRenderContext( renderObjectContext );

}

Expand Down
Loading

0 comments on commit 86580c5

Please sign in to comment.