Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions examples/jsm/renderers/common/Attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class Attributes extends DataMap {

}

getUUID( attribute ) {

return this._getBufferAttribute( attribute ).uuid;

}

_getBufferAttribute( attribute ) {

if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data;
Expand Down
15 changes: 14 additions & 1 deletion examples/jsm/renderers/common/RenderObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ let id = 0;

export default class RenderObject {

constructor( nodes, geometries, renderer, object, material, scene, camera, lightsNode ) {
constructor( nodes, attributes, geometries, renderer, object, material, scene, camera, lightsNode ) {

this._nodes = nodes;
this._attributes = attributes;
this._geometries = geometries;

this.id = id ++;
Expand Down Expand Up @@ -72,6 +73,18 @@ export default class RenderObject {

}

getAttributeHash( index ) {

return this._attributes.getUUID( this.getAttributes()[ index ] );

}

getIndexHash() {

return this._attributes.getUUID( this.getIndex() );

}

getCacheKey() {

const { material, scene, lightsNode } = this;
Expand Down
5 changes: 3 additions & 2 deletions examples/jsm/renderers/common/RenderObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import RenderObject from './RenderObject.js';

class RenderObjects extends ChainMap {

constructor( renderer, nodes, geometries, pipelines, info ) {
constructor( renderer, nodes, attributes, geometries, pipelines, info ) {

super();

this.renderer = renderer;
this.nodes = nodes;
this.attributes = attributes;
this.geometries = geometries;
this.pipelines = pipelines;
this.info = info;
Expand All @@ -26,7 +27,7 @@ class RenderObjects extends ChainMap {

if ( renderObject === undefined ) {

renderObject = new RenderObject( this.nodes, this.geometries, this.renderer, object, material, scene, camera, lightsNode );
renderObject = new RenderObject( this.nodes, this.attributes, this.geometries, this.renderer, object, material, scene, camera, lightsNode );

this._initRenderObject( renderObject );

Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/renderers/common/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class Renderer {
this._textures = new Textures( backend, this._info );
this._pipelines = new Pipelines( backend, this._nodes );
this._bindings = new Bindings( backend, this._nodes, this._textures, this._attributes, this._pipelines, this._info );
this._objects = new RenderObjects( this, this._nodes, this._geometries, this._pipelines, this._info );
this._objects = new RenderObjects( this, this._nodes, this._attributes, this._geometries, this._pipelines, this._info );
this._renderLists = new RenderLists();
this._renderContexts = new RenderContexts();

Expand Down
38 changes: 32 additions & 6 deletions examples/jsm/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*// debugger tools
import 'https://greggman.github.io/webgpu-avoid-redundant-state-setting/webgpu-check-redundant-state-setting.js';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greggman I've added its lib here, so it can inspire other developers to build good tools for it :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add some debug toggle (for example, global variable like window.DEBUG), which will dynamic-import the lib if enabled?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe THREE.Renderer( { debug: true } )

Copy link
Contributor

@LeviPesin LeviPesin Jun 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... And maybe place renderer.info also under that toggle? Like, it isn't updating (and there is no such property) when it is disabled.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... And maybe place renderer.info also under that toggle? Like, it isn't updating (and there is no such property) when it is disabled.

I think debug would be a property for development, info can still be useful for inspection.

//*/

import { GPUFeatureName, GPUTextureFormat, GPULoadOp, GPUStoreOp, GPUIndexFormat, GPUTextureViewDimension } from './utils/WebGPUConstants.js';

import WebGPUNodeBuilder from './nodes/WGSLNodeBuilder.js';
Expand Down Expand Up @@ -243,6 +247,7 @@ class WebGPUBackend extends Backend {
renderContextData.descriptor = descriptor;
renderContextData.encoder = encoder;
renderContextData.currentPass = currentPass;
renderContextData.currentAttributesSet = {};

//

Expand Down Expand Up @@ -395,6 +400,7 @@ class WebGPUBackend extends Backend {
const bindingsData = this.get( renderObject.getBindings() );
const contextData = this.get( context );
const pipelineGPU = this.get( pipeline ).pipeline;
const attributesSet = contextData.currentAttributesSet;

// pipeline

Expand All @@ -406,18 +412,28 @@ class WebGPUBackend extends Backend {
const bindGroupGPU = bindingsData.group;
passEncoderGPU.setBindGroup( 0, bindGroupGPU );

// index
// attributes

const index = renderObject.getIndex();

const hasIndex = ( index !== null );

// index

if ( hasIndex === true ) {

const buffer = this.get( index ).buffer;
const indexFormat = ( index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
const indexHash = renderObject.getIndexHash();

passEncoderGPU.setIndexBuffer( buffer, indexFormat );
if ( attributesSet.index !== indexHash ) {

const buffer = this.get( index ).buffer;
const indexFormat = ( index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;

passEncoderGPU.setIndexBuffer( buffer, indexFormat );

attributesSet.index = indexHash;

}

}

Expand All @@ -427,8 +443,17 @@ class WebGPUBackend extends Backend {

for ( let i = 0, l = attributes.length; i < l; i ++ ) {

const buffer = this.get( attributes[ i ] ).buffer;
passEncoderGPU.setVertexBuffer( i, buffer );
const attribute = attributes[ i ];
const attributeHash = renderObject.getAttributeHash( i );

if ( attributesSet[ i ] !== attributeHash ) {

const buffer = this.get( attribute ).buffer;
passEncoderGPU.setVertexBuffer( i, buffer );

attributesSet[ i ] = attributeHash;

}

}

Expand Down Expand Up @@ -703,6 +728,7 @@ class WebGPUBackend extends Backend {
if ( renderContext.stencil ) descriptor.depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;

renderContextData.currentPass = encoder.beginRenderPass( descriptor );
renderContextData.currentAttributesSet = {};

}

Expand Down
3 changes: 3 additions & 0 deletions src/core/BufferAttribute.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as MathUtils from '../math/MathUtils.js';
import { Vector3 } from '../math/Vector3.js';
import { Vector2 } from '../math/Vector2.js';
import { denormalize, normalize } from '../math/MathUtils.js';
Expand Down Expand Up @@ -32,6 +33,8 @@ class BufferAttribute {

this.version = 0;

this.uuid = MathUtils.generateUUID();

}

onUploadCallback() {}
Expand Down