From 18480106646aac17ffac097761469c3ccdb68bdc Mon Sep 17 00:00:00 2001 From: troublemaker52025 Date: Fri, 20 Nov 2020 16:18:10 +0800 Subject: [PATCH] Fix customize descriptor not bingding shadow map (#7836) * fix Multiple light sources not bingding globle CC_SHADOW_MAP * update --- .../pipeline/render-additive-light-queue.ts | 38 +++++++++---------- cocos/core/pipeline/shadow/shadow-flow.ts | 18 +++++++-- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/cocos/core/pipeline/render-additive-light-queue.ts b/cocos/core/pipeline/render-additive-light-queue.ts index b512adc1a8..31f1b1e5c9 100644 --- a/cocos/core/pipeline/render-additive-light-queue.ts +++ b/cocos/core/pipeline/render-additive-light-queue.ts @@ -48,9 +48,11 @@ import { SpotLight } from '../renderer/scene/spot-light'; import { SubModel } from '../renderer/scene/submodel'; import { getPhaseID } from './pass-phase'; import { Light, LightType } from '../renderer/scene/light'; -import { IRenderObject, SetIndex, UBOForwardLight, UBOGlobal, UBOShadow, UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING } from './define'; +import { IRenderObject, SetIndex, UBOForwardLight, UBOGlobal, UBOShadow, UNIFORM_SHADOWMAP_BINDING, UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING } from './define'; import { legacyCC } from '../global-exports'; import { genSamplerHash, samplerLib } from '../renderer/core/sampler-lib'; +import { builtinResMgr } from '../3d/builtin/init'; +import { Texture2D } from '../assets/texture-2d'; const _samplerInfo = [ Filter.LINEAR, @@ -333,14 +335,14 @@ export class RenderAdditiveLightQueue { const shadowInfo = this._pipeline.shadows; for (let i = 0; i < this._validLights.length; i++) { - const light = this._validLights[i]; - let texture: Texture|null = null; - if (this._pipeline.shadowFrameBufferMap.has(light)) { - texture = this._pipeline.shadowFrameBufferMap.get(light)!.colorTextures[0]!; - } - + const light = this._validLights[i]; const descriptorSet = this._getOrCreateDescriptorSet(light); if(!descriptorSet) { return; } + + descriptorSet.bindSampler(UNIFORM_SHADOWMAP_BINDING, this._sampler!); + descriptorSet.bindSampler(UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING, this._sampler!); + // Main light sampler binding + descriptorSet.bindTexture(UNIFORM_SHADOWMAP_BINDING, builtinResMgr.get('default-texture').getGFXTexture()!); descriptorSet.update(); this._updateGlobalDescriptorSet(view, cmdBuff); @@ -353,6 +355,8 @@ export class RenderAdditiveLightQueue { this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 1] = shadowInfo.size.y; this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 2] = shadowInfo.pcf; this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 3] = shadowInfo.bias; + // Spot light sampler binding + descriptorSet.bindTexture(UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING, builtinResMgr.get('default-texture').getGFXTexture()!); break; case LightType.SPOT: const spotLight = light as SpotLight; @@ -374,23 +378,15 @@ export class RenderAdditiveLightQueue { this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 1] = shadowInfo.size.y; this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 2] = shadowInfo.pcf; this._shadowUBO[UBOShadow.SHADOW_INFO_OFFSET + 3] = shadowInfo.bias; + // Spot light sampler binding + if (this._pipeline.shadowFrameBufferMap.has(light)) { + descriptorSet.bindTexture(UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING, this._pipeline.shadowFrameBufferMap.get(light)?.colorTextures[0]!); + } else { + descriptorSet.bindTexture(UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING, builtinResMgr.get('default-texture').getGFXTexture()!); + } break; default: } - - // must binding a sampler - if (texture === null) { - let framebufferArray = Array.from(this._pipeline.shadowFrameBufferMap.values()); - for (let i = 0; i < framebufferArray.length; i++) { - if (framebufferArray[i].colorTextures[0]) { - texture = framebufferArray[i].colorTextures[0]!; - break; - } - } - } - - descriptorSet.bindSampler(UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING, this._sampler!); - descriptorSet.bindTexture(UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING, texture!); descriptorSet.update(); cmdBuff.updateBuffer(descriptorSet.getBuffer(UBOGlobal.BINDING)!, this._globalUBO); diff --git a/cocos/core/pipeline/shadow/shadow-flow.ts b/cocos/core/pipeline/shadow/shadow-flow.ts index a4701d7598..9d24ff0dde 100644 --- a/cocos/core/pipeline/shadow/shadow-flow.ts +++ b/cocos/core/pipeline/shadow/shadow-flow.ts @@ -44,6 +44,8 @@ import { genSamplerHash, samplerLib } from '../../renderer/core/sampler-lib'; import { Light, LightType } from '../../renderer/scene/light'; import { lightCollecting, shadowCollecting } from '../forward/scene-culling'; import { Vec2 } from '../../math'; +import { builtinResMgr } from '../../3d/builtin/init'; +import { Texture2D } from '../../assets/texture-2d'; const _samplerInfo = [ Filter.LINEAR, @@ -73,6 +75,7 @@ export class ShadowFlow extends RenderFlow { }; private _shadowRenderPass: RenderPass|null = null; + private _globalBinding: boolean = false; public initialize (info: IRenderFlowInfo): boolean { super.initialize(info); @@ -133,6 +136,8 @@ export class ShadowFlow extends RenderFlow { (this._pipeline as ForwardPipeline).shadowFrameBufferMap.clear(); if(this._shadowRenderPass) { this._shadowRenderPass.destroy() }; + + this._globalBinding = false; } public _initShadowFrameBuffer (pipeline: ForwardPipeline, light: Light) { @@ -188,9 +193,16 @@ export class ShadowFlow extends RenderFlow { // Cache frameBuffer pipeline.shadowFrameBufferMap.set(light, shadowFrameBuffer); - const shadowMapSamplerHash = genSamplerHash(_samplerInfo); - const shadowMapSampler = samplerLib.getSampler(device, shadowMapSamplerHash); - pipeline.descriptorSet.bindSampler(UNIFORM_SHADOWMAP_BINDING, shadowMapSampler); + + if (!this._globalBinding) { + const shadowMapSamplerHash = genSamplerHash(_samplerInfo); + const shadowMapSampler = samplerLib.getSampler(device, shadowMapSamplerHash); + pipeline.descriptorSet.bindSampler(UNIFORM_SHADOWMAP_BINDING, shadowMapSampler); + pipeline.descriptorSet.bindTexture(UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING, builtinResMgr.get('default-texture').getGFXTexture()!); + pipeline.descriptorSet.bindSampler(UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING, shadowMapSampler); + pipeline.descriptorSet.bindTexture(UNIFORM_SPOT_LIGHTING_MAP_TEXTURE_BINDING, builtinResMgr.get('default-texture').getGFXTexture()!); + this._globalBinding = true; + } if (light && light.type === LightType.DIRECTIONAL) { pipeline.descriptorSet.bindTexture(UNIFORM_SHADOWMAP_BINDING, shadowFrameBuffer.colorTextures[0]!);