Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node: add .construct() and some architectural changes #24110

Merged
merged 4 commits into from
May 24, 2022

Conversation

sunag
Copy link
Collaborator

@sunag sunag commented May 23, 2022

Description

Use ShaderNode with analyze() to optimization brought some other challenges. The current approach use ShaderNode directly in the .generate() stage, ignoring the previous stage analyze(). It caused some bugs and bad optimization for non-inline codes. To solve this, I worked on a process to .construct() the shader struct before .analyze() and .generate().

I'm working to make the same thing to ComputeNode.

image

"shader nodes" in this case it would be the nodes created at the time of the renderer for calculating the lighting.

NormalMapNode added on .generate() stage
-> no cache has created

// FLOW -> TransformedNormalView
TransformedNormalView = normalize( ( ( ( ( cross( dpdy( nodeVary3.xyz.xyz ), normalize( nodeVary4 ) ) * vec3<f32>( dpdx( nodeVary1.xy ).x ) ) + ( cross( normalize( nodeVary4 ), dpdx( nodeVary3.xyz.xyz ) ) * vec3<f32>( dpdy( nodeVary1.xy ).x ) ) ) * vec3<f32>( ( vec3<f32>( ( ( ( textureSample( nodeUniform8, nodeUniform8_sampler, nodeVary1 ) * vec4<f32>( vec3<f32>( 2.0 ), 1.0 ) ) - vec4<f32>( vec3<f32>( 1.0 ), 1.0 ) ).xy * NodeUniforms.nodeUniform9 ), ( ( textureSample( nodeUniform8, nodeUniform8_sampler, nodeVary1 ) * vec4<f32>( vec3<f32>( 2.0 ), 1.0 ) ) - vec4<f32>( vec3<f32>( 1.0 ), 1.0 ) ).z ).x * ( ( ( f32( isFront ) * 2.0 ) - 1.0 ) * inverseSqrt( max( dot( ( ( cross( dpdy( nodeVary3.xyz.xyz ), normalize( nodeVary4 ) ) * vec3<f32>( dpdx( nodeVary1.xy ).x ) ) + ( cross( normalize( nodeVary4 ), dpdx( nodeVary3.xyz.xyz ) ) * vec3<f32>( dpdy( nodeVary1.xy ).x ) ) ), ( ( cross( dpdy( nodeVary3.xyz.xyz ), normalize( nodeVary4 ) ) * vec3<f32>( dpdx( nodeVary1.xy ).x ) ) + ( cross( normalize( nodeVary4 ), dpdx( nodeVary3.xyz.xyz ) ) * vec3<f32>( dpdy( nodeVary1.xy ).x ) ) ) ), dot( ( ( cross( dpdy( nodeVary3.xyz.xyz ), normalize( nodeVary4 ) ) * vec3<f32>( dpdx( nodeVary1.xy ).y ) ) + ( cross( normalize( nodeVary4 ), dpdx( nodeVary3.xyz.xyz ) ) * vec3<f32>( dpdy( nodeVary1.xy ).y ) ) ), ( ( cross( dpdy( nodeVary3.xyz.xyz ), normalize( nodeVary4 ) ) * vec3<f32>( dpdx( nodeVary1.xy ).y ) ) + ( cross( normalize( nodeVary4 ), dpdx( nodeVary3.xyz.xyz ) ) * vec3<f32>( dpdy( nodeVary1.xy ).y ) ) ) ) ) ) ) ) ) ) + ( ( ( ( cross( dpdy( nodeVary3.xyz.xyz ), normalize( nodeVary4 ) ) * vec3<f32>( dpdx( nodeVary1.xy ).y ) ) + ( cross( normalize( nodeVary4 ), dpdx( nodeVary3.xyz.xyz ) ) * vec3<f32>( dpdy( nodeVary1.xy ).y ) ) ) * vec3<f32>( ( vec3<f32>( ( ( ( textureSample( nodeUniform8, nodeUniform8_sampler, nodeVary1 ) * vec4<f32>( vec3<f32>( 2.0 ), 1.0 ) ) - vec4<f32>( vec3<f32>( 1.0 ), 1.0 ) ).xy * NodeUniforms.nodeUniform9 ), ( ( textureSample( nodeUniform8, nodeUniform8_sampler, nodeVary1 ) * vec4<f32>( vec3<f32>( 2.0 ), 1.0 ) ) - vec4<f32>( vec3<f32>( 1.0 ), 1.0 ) ).z ).y * ( ( ( f32( isFront ) * 2.0 ) - 1.0 ) * inverseSqrt( max( dot( ( ( cross( dpdy( nodeVary3.xyz.xyz ), normalize( nodeVary4 ) ) * vec3<f32>( dpdx( nodeVary1.xy ).x ) ) + ( cross( normalize( nodeVary4 ), dpdx( nodeVary3.xyz.xyz ) ) * vec3<f32>( dpdy( nodeVary1.xy ).x ) ) ), ( ( cross( dpdy( nodeVary3.xyz.xyz ), normalize( nodeVary4 ) ) * vec3<f32>( dpdx( nodeVary1.xy ).x ) ) + ( cross( normalize( nodeVary4 ), dpdx( nodeVary3.xyz.xyz ) ) * vec3<f32>( dpdy( nodeVary1.xy ).x ) ) ) ), dot( ( ( cross( dpdy( nodeVary3.xyz.xyz ), normalize( nodeVary4 ) ) * vec3<f32>( dpdx( nodeVary1.xy ).y ) ) + ( cross( normalize( nodeVary4 ), dpdx( nodeVary3.xyz.xyz ) ) * vec3<f32>( dpdy( nodeVary1.xy ).y ) ) ), ( ( cross( dpdy( nodeVary3.xyz.xyz ), normalize( nodeVary4 ) ) * vec3<f32>( dpdx( nodeVary1.xy ).y ) ) + ( cross( normalize( nodeVary4 ), dpdx( nodeVary3.xyz.xyz ) ) * vec3<f32>( dpdy( nodeVary1.xy ).y ) ) ) ) ) ) ) ) ) ) + ( normalize( nodeVary4 ) * vec3<f32>( vec3<f32>( ( ( ( textureSample( nodeUniform8, nodeUniform8_sampler, nodeVary1 ) * vec4<f32>( vec3<f32>( 2.0 ), 1.0 ) ) - vec4<f32>( vec3<f32>( 1.0 ), 1.0 ) ).xy * NodeUniforms.nodeUniform9 ), ( ( textureSample( nodeUniform8, nodeUniform8_sampler, nodeVary1 ) * vec4<f32>( vec3<f32>( 2.0 ), 1.0 ) ) - vec4<f32>( vec3<f32>( 1.0 ), 1.0 ) ).z ).z ) ) ) ) );

NormalMapNode added on .construct() stage
-> cache working done

// FLOW -> TransformedNormalView
nodeVar7 = cross( dpdy( nodeVary3.xyz.xyz ), normalize( nodeVary4 ) );
nodeVar8 = dpdx( nodeVary1.xy );
nodeVar9 = cross( normalize( nodeVary4 ), dpdx( nodeVary3.xyz.xyz ) );
nodeVar10 = dpdy( nodeVary1.xy );
nodeVar11 = ( ( nodeVar7 * vec3<f32>( nodeVar8.x ) ) + ( nodeVar9 * vec3<f32>( nodeVar10.x ) ) );
nodeVar12 = ( ( textureSample( nodeUniform8, nodeUniform8_sampler, nodeVary1 ) * vec4<f32>( vec3<f32>( 2.0 ), 1.0 ) ) - vec4<f32>( vec3<f32>( 1.0 ), 1.0 ) );
nodeVar13 = ( ( nodeVar7 * vec3<f32>( nodeVar8.y ) ) + ( nodeVar9 * vec3<f32>( nodeVar10.y ) ) );
nodeVar14 = ( ( ( f32( isFront ) * 2.0 ) - 1.0 ) * inverseSqrt( max( dot( nodeVar11, nodeVar11 ), dot( nodeVar13, nodeVar13 ) ) ) );
TransformedNormalView = normalize( ( ( nodeVar11 * vec3<f32>( ( vec3<f32>( ( nodeVar12.xy * NodeUniforms.nodeUniform9 ), nodeVar12.z ).x * nodeVar14 ) ) ) + ( ( nodeVar13 * vec3<f32>( ( vec3<f32>( ( nodeVar12.xy * NodeUniforms.nodeUniform9 ), nodeVar12.z ).y * nodeVar14 ) ) ) + ( normalize( nodeVary4 ) * vec3<f32>( vec3<f32>( ( nodeVar12.xy * NodeUniforms.nodeUniform9 ), nodeVar12.z ).z ) ) ) ) );

This contribution is funded by Google via Igalia

@sunag sunag added this to the r141 milestone May 23, 2022
@sunag sunag merged commit d35bfbc into mrdoob:dev May 24, 2022
@sunag sunag deleted the dev-nm-construct branch May 24, 2022 19:50
abernier pushed a commit to abernier/three.js that referenced this pull request Sep 16, 2022
* Rename EnvironmentLightNode to EnvronmentNode

* Node: add .construct()

* cleanup

* cleanup2
@Methuselah96 Methuselah96 mentioned this pull request Jan 18, 2024
45 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant