From 3da86aed53fdcd1e5a083f5d75fc8b78ed7eadc3 Mon Sep 17 00:00:00 2001 From: jk20012001 Date: Mon, 18 Jul 2022 11:32:50 +0800 Subject: [PATCH] [optimize] shadow pass texture sampling without alpha test (#12005) --- .../render-to-shadowmap/fs.chunk | 5 +-- .../default-functions/standard-fs.chunk | 7 ++++ editor/assets/effects/builtin-standard.effect | 7 ++-- editor/assets/effects/builtin-toon.effect | 6 ++-- .../dcc/imported-metallic-roughness.effect | 34 ++++++++++++++----- .../dcc/imported-specular-glossiness.effect | 32 ++++++++--------- ...urfaces-imported-metallic-roughness.effect | 29 ++++++++++++++++ ...rfaces-imported-specular-glossiness.effect | 26 ++++++++++---- .../assets/effects/surfaces/standard.effect | 24 ++++++++++--- editor/assets/effects/surfaces/toon.effect | 23 +++++-------- 10 files changed, 133 insertions(+), 60 deletions(-) diff --git a/editor/assets/chunks/shading-entries/main-functions/render-to-shadowmap/fs.chunk b/editor/assets/chunks/shading-entries/main-functions/render-to-shadowmap/fs.chunk index 33614ecd79..e1824dc45d 100644 --- a/editor/assets/chunks/shading-entries/main-functions/render-to-shadowmap/fs.chunk +++ b/editor/assets/chunks/shading-entries/main-functions/render-to-shadowmap/fs.chunk @@ -3,8 +3,9 @@ in highp vec2 v_clip_depth; layout(location = 0) out vec4 fragColorX; void main () { - // for alpha clip - SurfacesFragmentModifyBaseColorAndTransparency(); + // better performance than SurfacesFragmentModifyBaseColorAndTransparency + SurfacesFragmentAlphaClipOnly(); + highp float clipDepth = v_clip_depth.x / v_clip_depth.y * 0.5 + 0.5; // spot use linear diff --git a/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk b/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk index 16ba4c5344..10234ffc44 100644 --- a/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk +++ b/editor/assets/chunks/surfaces/default-functions/standard-fs.chunk @@ -5,6 +5,13 @@ vec4 SurfacesFragmentModifyBaseColorAndTransparency() } #endif +#ifndef CC_SURFACES_FRAGMENT_ALPHA_CLIP_ONLY +// for base shape without color usage, such as render-to-shadow +void SurfacesFragmentAlphaClipOnly() +{ +} +#endif + #ifndef CC_SURFACES_FRAGMENT_MODIFY_WORLD_NORMAL vec3 SurfacesFragmentModifyWorldNormal() diff --git a/editor/assets/effects/builtin-standard.effect b/editor/assets/effects/builtin-standard.effect index 69fa82c50b..bf63e71f8a 100644 --- a/editor/assets/effects/builtin-standard.effect +++ b/editor/assets/effects/builtin-standard.effect @@ -368,11 +368,10 @@ CCProgram shadow-caster-fs %{ vec4 frag () { vec4 baseColor = albedo; - #if USE_ALBEDO_MAP - baseColor *= texture(albedoMap, ALBEDO_UV); - #endif - #if USE_ALPHA_TEST + #if USE_ALBEDO_MAP + baseColor *= texture(albedoMap, ALBEDO_UV); + #endif if (baseColor.ALPHA_TEST_CHANNEL < albedoScaleAndCutoff.w) discard; #endif diff --git a/editor/assets/effects/builtin-toon.effect b/editor/assets/effects/builtin-toon.effect index 7c9a6badec..a36b279efc 100644 --- a/editor/assets/effects/builtin-toon.effect +++ b/editor/assets/effects/builtin-toon.effect @@ -309,11 +309,11 @@ CCProgram shadow-caster-fs %{ vec4 frag () { vec4 baseColor = baseColor; - #if USE_BASE_COLOR_MAP - baseColor *= texture(baseColorMap, v_uv); - #endif #if USE_ALPHA_TEST + #if USE_BASE_COLOR_MAP + baseColor *= texture(baseColorMap, v_uv); + #endif if (baseColor.ALPHA_TEST_CHANNEL < colorScaleAndCutoff.w) discard; #endif diff --git a/editor/assets/effects/dcc/imported-metallic-roughness.effect b/editor/assets/effects/dcc/imported-metallic-roughness.effect index cc26af5ded..57d9153dc4 100644 --- a/editor/assets/effects/dcc/imported-metallic-roughness.effect +++ b/editor/assets/effects/dcc/imported-metallic-roughness.effect @@ -57,6 +57,8 @@ CCEffect %{ albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } + alphaSource: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } } + alphaSourceMap: { value: grey, editor: { parent: USE_OPACITY_MAP } } - name: transparent passes: - vert: standard-vs @@ -435,19 +437,33 @@ CCProgram shadow-caster-fs %{ #pragma define-meta ALBEDO_UV options([v_uv, v_uv1]) #endif - #if USE_ALPHA_TEST - #pragma define-meta ALPHA_TEST_CHANNEL options([a, r]) + #pragma define-meta TEXTURE_UV options([v_uv, v_uv1]) + + #if USE_OPACITY_MAP + #pragma define-meta ALPHA_SOURCE_CHANNEL options([r, g, b, a]) + uniform sampler2D alphaSourceMap; #endif vec4 frag () { - vec4 baseColor = albedo; - - #if USE_ALBEDO_MAP - baseColor *= texture(albedoMap, ALBEDO_UV); - #endif - #if USE_ALPHA_TEST - if (baseColor.ALPHA_TEST_CHANNEL < albedoScaleAndCutoff.w) discard; + float alpha = albedo.a; + #if USE_ALBEDO_MAP + alpha = texture(albedoMap, TEXTURE_UV).a; + #endif + #if ALPHA_SOURCE_IS_OPACITY + #if USE_OPACITY_MAP + alpha = 1.0 - texture(alphaSourceMap, TEXTURE_UV).ALPHA_SOURCE_CHANNEL; + #else + alpha = 1.0 - alphaSource; + #endif + #else + #if USE_OPACITY_MAP + alpha = texture(alphaSourceMap, TEXTURE_UV).ALPHA_SOURCE_CHANNEL; + #else + alpha = alphaSource; + #endif + #endif + if (alpha < albedoScaleAndCutoff.w) discard; #endif highp float clipDepth = v_clip_depth.x / v_clip_depth.y * 0.5 + 0.5; diff --git a/editor/assets/effects/dcc/imported-specular-glossiness.effect b/editor/assets/effects/dcc/imported-specular-glossiness.effect index e7bbd5f9ed..92b87d1eb8 100644 --- a/editor/assets/effects/dcc/imported-specular-glossiness.effect +++ b/editor/assets/effects/dcc/imported-specular-glossiness.effect @@ -52,11 +52,13 @@ CCEffect %{ rasterizerState: cullMode: front properties: - tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } - mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } } - albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } - alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } - mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } + tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } + mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } } + albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } + alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } + mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } + transparencyMap: { value: grey, editor: { displayName: TransparencyMap } } + transparencyFactor: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } } - name: transparent passes: - vert: standard-vs @@ -446,19 +448,13 @@ CCProgram shadow-caster-fs %{ #endif vec4 frag () { - vec4 baseColor = diffuseColor; - #if USE_ALBEDO_MAP - vec4 texColor = texture(albedoMap, TEXTURE_UV); - baseColor *= texColor; - #endif - - #if USE_TRANSPARENCY_MAP - baseColor.a = texture(transparencyMap, TEXTURE_UV).a * transparencyFactor; - #endif - - #if USE_ALPHA_TEST - if (baseColor.a < albedoScaleAndCutoff.w) discard; - #endif + #if USE_ALPHA_TEST + float alpha = 1.0; + #if USE_TRANSPARENCY_MAP + alpha = texture(transparencyMap, TEXTURE_UV).a * transparencyFactor; + #endif + if (alpha < albedoScaleAndCutoff.w) discard; + #endif highp float clipDepth = v_clip_depth.x / v_clip_depth.y * 0.5 + 0.5; // spot use linear diff --git a/editor/assets/effects/dcc/surfaces-imported-metallic-roughness.effect b/editor/assets/effects/dcc/surfaces-imported-metallic-roughness.effect index a7e51a0614..f0e0515a49 100644 --- a/editor/assets/effects/dcc/surfaces-imported-metallic-roughness.effect +++ b/editor/assets/effects/dcc/surfaces-imported-metallic-roughness.effect @@ -57,6 +57,8 @@ CCEffect %{ albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } + alphaSource: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } } + alphaSourceMap: { value: grey, editor: { parent: USE_OPACITY_MAP } } - name: transparent passes: - vert: standard-vs @@ -225,6 +227,33 @@ CCProgram surface-fragment %{ return baseColor; } + #define CC_SURFACES_FRAGMENT_ALPHA_CLIP_ONLY + void SurfacesFragmentAlphaClipOnly() + { + #if USE_ALPHA_TEST + float alpha = albedo.a; + #if USE_ALBEDO_MAP + alpha = texture(albedoMap, TEXTURE_UV).a; + #endif + + #if ALPHA_SOURCE_IS_OPACITY + #if USE_OPACITY_MAP + alpha = 1.0 - texture(alphaSourceMap, TEXTURE_UV).ALPHA_SOURCE_CHANNEL; + #else + alpha = 1.0 - alphaSource; + #endif + #else + #if USE_OPACITY_MAP + alpha = texture(alphaSourceMap, TEXTURE_UV).ALPHA_SOURCE_CHANNEL; + #else + alpha = alphaSource; + #endif + #endif + + if (alpha < albedoScaleAndCutoff.w) discard; + #endif + } + #define CC_SURFACES_FRAGMENT_MODIFY_WORLD_NORMAL vec3 SurfacesFragmentModifyWorldNormal() { diff --git a/editor/assets/effects/dcc/surfaces-imported-specular-glossiness.effect b/editor/assets/effects/dcc/surfaces-imported-specular-glossiness.effect index fff8749109..3fc592b0c0 100644 --- a/editor/assets/effects/dcc/surfaces-imported-specular-glossiness.effect +++ b/editor/assets/effects/dcc/surfaces-imported-specular-glossiness.effect @@ -19,7 +19,7 @@ CCEffect %{ specularMap: { value: grey, editor: { parent: '!HAS_EXPORTED_METALLIC' } } specularFactor: { value: 1.0, editor: { parent: '!HAS_EXPORTED_METALLIC' } } transparencyMap: { value: grey, editor: { displayName: TransparencyMap } } - transparencyFactor: { value: 0.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } } + transparencyFactor: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } } tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } normalStrength: { value: 1.0, target: normalScale, editor: { displayName: bumpFactor, parent: USE_NORMAL_MAP, slide: true, range: [0, 5.0], step: 0.001 } } normalMap: { value: normal } @@ -52,11 +52,13 @@ CCEffect %{ rasterizerState: cullMode: front properties: - tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } - mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } } - albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } - alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } - mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } + tilingOffset: { value: [1.0, 1.0, 0.0, 0.0] } + mainColor: { value: [1.0, 1.0, 1.0, 1.0], target: albedo, editor: { displayName: Albedo, type: color } } + albedoScale: { value: [1.0, 1.0, 1.0], target: albedoScaleAndCutoff.xyz } + alphaThreshold: { value: 0.5, target: albedoScaleAndCutoff.w, editor: { parent: USE_ALPHA_TEST } } + mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } + transparencyMap: { value: grey, editor: { displayName: TransparencyMap } } + transparencyFactor: { value: 1.0, editor: { slide: true, range: [0, 1.0], step: 0.001 } } - name: transparent passes: - vert: standard-vs @@ -238,6 +240,18 @@ CCProgram surface-fragment %{ return baseColor; } + #define CC_SURFACES_FRAGMENT_ALPHA_CLIP_ONLY + void SurfacesFragmentAlphaClipOnly() + { + #if USE_ALPHA_TEST + float alpha = 1.0; + #if USE_TRANSPARENCY_MAP + alpha = texture(transparencyMap, TEXTURE_UV).a * transparencyFactor; + #endif + if (alpha < albedoScaleAndCutoff.w) discard; + #endif + } + #define CC_SURFACES_FRAGMENT_MODIFY_WORLD_NORMAL vec3 SurfacesFragmentModifyWorldNormal() { diff --git a/editor/assets/effects/surfaces/standard.effect b/editor/assets/effects/surfaces/standard.effect index 54a73ad84b..3aa5030fe6 100644 --- a/editor/assets/effects/surfaces/standard.effect +++ b/editor/assets/effects/surfaces/standard.effect @@ -193,7 +193,7 @@ CCProgram surface-fragment %{ baseColor.rgb *= SRGBToLinear(FSInput_vertexColor.rgb); // use linear baseColor.a *= FSInput_vertexColor.a; #endif - + #if USE_ALBEDO_MAP vec4 texColor = texture(albedoMap, ALBEDO_UV); texColor.rgb = SRGBToLinear(texColor.rgb); @@ -208,10 +208,26 @@ CCProgram surface-fragment %{ return baseColor; } + #define CC_SURFACES_FRAGMENT_ALPHA_CLIP_ONLY + void SurfacesFragmentAlphaClipOnly() + { + #if USE_ALPHA_TEST + float alpha = albedo.ALPHA_TEST_CHANNEL; + #if USE_VERTEX_COLOR + alpha *= FSInput_vertexColor.a; + #endif + #if USE_ALBEDO_MAP + alpha = texture(albedoMap, ALBEDO_UV).ALPHA_TEST_CHANNEL; + #endif + + if (alpha < albedoScaleAndCutoff.w) discard; + #endif + } + #define CC_SURFACES_FRAGMENT_MODIFY_WORLD_NORMAL vec3 SurfacesFragmentModifyWorldNormal() { - vec3 normal = FSInput_worldNormal; + vec3 normal = FSInput_worldNormal; #if USE_NORMAL_MAP vec3 nmmp = texture(normalMap, NORMAL_UV).xyz - vec3(0.5); normal = CalculateNormalFromTangentSpace(nmmp, emissiveScaleParam.w, normal.xyz, FSInput_worldTangent, FSInput_mirrorNormal); @@ -223,8 +239,8 @@ CCProgram surface-fragment %{ #define CC_SURFACES_FRAGMENT_MODIFY_ANISOTROPY_PARAMS vec4 SurfacesFragmentModifyAnisotropyParams() { - float anisotropyRotation = anisotropyParam.y; - float anisotropyShape = anisotropyParam.x; + float anisotropyRotation = anisotropyParam.y; + float anisotropyShape = anisotropyParam.x; #if IS_ANISOTROPY && USE_ANISOTROPY_ROTATION_MAP anisotropyRotation = fract(anisotropyRotation + texture(anisotropyRotationMap, DEFAULT_UV).x); #endif diff --git a/editor/assets/effects/surfaces/toon.effect b/editor/assets/effects/surfaces/toon.effect index 380a66dbe8..5460bca9dd 100644 --- a/editor/assets/effects/surfaces/toon.effect +++ b/editor/assets/effects/surfaces/toon.effect @@ -171,28 +171,23 @@ CCProgram surface-fragment %{ #endif } - // only for render-to-shadow-map or misc usage - #define CC_SURFACES_FRAGMENT_MODIFY_BASECOLOR_AND_TRANSPARENCY - vec4 SurfacesFragmentModifyBaseColorAndTransparency() + #define CC_SURFACES_FRAGMENT_ALPHA_CLIP_ONLY + void SurfacesFragmentAlphaClipOnly() { - vec4 color = baseColor; - #if USE_BASE_COLOR_MAP - vec4 texColor = texture(baseColorMap, FSInput_texcoord); - texColor.rgb = SRGBToLinear(texColor.rgb); - color *= texColor; - #endif - #if USE_ALPHA_TEST - if (color.ALPHA_TEST_CHANNEL < colorScaleAndCutoff.w) discard; + float alpha = baseColor.ALPHA_TEST_CHANNEL; + #if USE_BASE_COLOR_MAP + alpha = texture(baseColorMap, FSInput_texcoord).ALPHA_TEST_CHANNEL; + #endif + + if (alpha < colorScaleAndCutoff.w) discard; #endif - - return color; } #define CC_SURFACES_FRAGMENT_MODIFY_WORLD_NORMAL vec3 SurfacesFragmentModifyWorldNormal() { - vec3 normal = FSInput_worldNormal; + vec3 normal = FSInput_worldNormal; #if USE_NORMAL_MAP vec3 nmmp = texture(normalMap, FSInput_texcoord).xyz - vec3(0.5); normal = CalculateNormalFromTangentSpace(nmmp, emissiveScaleAndStrenth.w, normal.xyz, FSInput_worldTangent, FSInput_mirrorNormal);