Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 18 additions & 6 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ function WebGLRenderer( parameters ) {
_currentRenderTarget = null,
_currentFramebuffer = null,
_currentMaterialId = - 1,
_currentGeometryProgram = '',

// geometry and program caching

_currentGeometryProgram = {
geometryID: null,
programID: null,
wireframe: false
},

_currentCamera = null,
_currentArrayCamera = null,
Expand Down Expand Up @@ -701,13 +708,14 @@ function WebGLRenderer( parameters ) {
state.setMaterial( material, frontFaceCW );

var program = setProgram( camera, fog, material, object );
var geometryProgram = geometry.id + '_' + program.id + '_' + ( material.wireframe === true );

var updateBuffers = false;

if ( geometryProgram !== _currentGeometryProgram ) {
if ( geometry.id !== _currentGeometryProgram.geometryID || program.id !== _currentGeometryProgram.programID || material.wireframe !== _currentGeometryProgram.wireframe ) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similarly wireframe comparison should be?

 _currentGeometryProgram.wireframe !== ( material.wireframe === true )

Copy link
Owner

Choose a reason for hiding this comment

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

Are you saying that checking true !== undefined is slower than true !== false?

Copy link
Collaborator

@takahirox takahirox Jul 31, 2018

Choose a reason for hiding this comment

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

Nope. _currentGeometryProgram.wireframe is true/false now but material.wireframe can be undefined because some Materials don't have .wireframe, then undefined !== false will be false.

Copy link
Owner

Choose a reason for hiding this comment

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

Oh...

Copy link
Owner

Choose a reason for hiding this comment

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

Fixed.


_currentGeometryProgram = geometryProgram;
_currentGeometryProgram.geometryID = geometry.id;
_currentGeometryProgram.programID = program.id;
_currentGeometryProgram.wireframe = material.wireframe;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Some Materials don't have .wireframe so here can be undefined. This could be better?

_currentGeometryProgram.wireframe = material.wireframe === true;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, makes sense! Should this be address in this PR or another PR? I already removed the branch.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think another PR is fine.

Copy link
Owner

Choose a reason for hiding this comment

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

I can do the change locally 👌

Copy link
Owner

@mrdoob mrdoob Jul 30, 2018

Choose a reason for hiding this comment

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

Also, instead of this..

_currentGeometryProgram.geometryID = geometry.id;
_currentGeometryProgram.programID = program.id;

couldn't it just be...

_currentGeometryProgram.geometry = geometry;
_currentGeometryProgram.program = program;

Copy link
Collaborator

Choose a reason for hiding this comment

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

Note: If you wanna do, you should clear _currentGeometryProgram in the end of .render(). Unless doing that, _currentGeometryProgram keeps the reference to geometry and program until calling next .render(). That means, even if you no longer need the geometry/program the resources aren't released without calling next .render().

updateBuffers = true;

}
Expand Down Expand Up @@ -1107,7 +1115,9 @@ function WebGLRenderer( parameters ) {

// reset caching for this frame

_currentGeometryProgram = '';
_currentGeometryProgram.geometryID = null;
_currentGeometryProgram.programID = null;
_currentGeometryProgram.wireframe = false;
_currentMaterialId = - 1;
_currentCamera = null;

Expand Down Expand Up @@ -1455,7 +1465,9 @@ function WebGLRenderer( parameters ) {

var program = setProgram( camera, scene.fog, material, object );

_currentGeometryProgram = '';
_currentGeometryProgram.geometryID = null;
_currentGeometryProgram.programID = null;
_currentGeometryProgram.wireframe = false;

renderObjectImmediate( object, program, material );

Expand Down
22 changes: 21 additions & 1 deletion src/renderers/webgl/WebGLRenderStates.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,30 @@ function WebGLRenderState() {
function WebGLRenderStates() {

var renderStates = {};
var hashCache = {};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we add hashCache = {}; in dispose()?


function get( scene, camera ) {

var hash = scene.id + ',' + camera.id;
var hash;

if ( hashCache[ scene.id ] ) {

hash = hashCache[ scene.id ][ camera.id ];

if ( hash === undefined ) {

hash = scene.id + ',' + camera.id;
hashCache[ scene.id ][ camera.id ] = hash;

}

} else {

hash = scene.id + ',' + camera.id;
hashCache[ scene.id ] = {};
hashCache[ scene.id ][ camera.id ] = hash;

}
Copy link
Collaborator

@takahirox takahirox Jul 28, 2018

Choose a reason for hiding this comment

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

I think we don't need to use hash.

function get( scene, camera ) {

    if ( renderStates[ scene.id ] === undefined ) {

        renderStates[ scene.id ] = {};

    }

    if ( renderStates[ scene.id ][ camera.id ] === undefined ) {

        renderStates[ scene.id ][ camera.id ] = new WebGLRenderState();

    }

    return renderStates[ scene.id ][ camera.id ];

}

If we use Map, we don't even need .id like renderStates.get( scene ).get( camera );.

Copy link
Contributor

Choose a reason for hiding this comment

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

Map is now widely supported? I'm in favor of using it here.


var renderState = renderStates[ hash ];

Expand Down