diff --git a/cocos/core/pipeline/deferred/deferred-pipeline.ts b/cocos/core/pipeline/deferred/deferred-pipeline.ts index 20c8e7543c..aa802a07e5 100644 --- a/cocos/core/pipeline/deferred/deferred-pipeline.ts +++ b/cocos/core/pipeline/deferred/deferred-pipeline.ts @@ -274,7 +274,7 @@ export class DeferredPipeline extends RenderPipeline { if (!this._lightingRenderPass) { const colorAttachment = new ColorAttachment(); - colorAttachment.format = Format.RGBA16F; + colorAttachment.format = Format.RGBA8; colorAttachment.loadOp = LoadOp.CLEAR; // should clear color attachment colorAttachment.storeOp = StoreOp.STORE; colorAttachment.endAccesses = [AccessType.COLOR_ATTACHMENT_WRITE]; @@ -296,15 +296,6 @@ export class DeferredPipeline extends RenderPipeline { this._height = device.height; this._generateDeferredRenderData(); - if (device.surfaceTransform === SurfaceTransform.IDENTITY - || device.surfaceTransform === SurfaceTransform.ROTATE_180) { - this._width = device.width; - this._height = device.height; - } else { - this._width = device.height; - this._height = device.width; - } - return true; } @@ -575,7 +566,7 @@ export class DeferredPipeline extends RenderPipeline { data.lightingRenderTargets.push(device.createTexture(new TextureInfo( TextureType.TEX2D, TextureUsageBit.COLOR_ATTACHMENT | TextureUsageBit.SAMPLED, - Format.RGBA16F, + Format.RGBA8, this._width, this._height, ))); diff --git a/cocos/core/pipeline/define.ts b/cocos/core/pipeline/define.ts index 32e20d8d39..d36723c9aa 100644 --- a/cocos/core/pipeline/define.ts +++ b/cocos/core/pipeline/define.ts @@ -34,7 +34,7 @@ import { SubModel } from '../renderer/scene/submodel'; import { Layers } from '../scene-graph/layers'; import { legacyCC } from '../global-exports'; import { BindingMappingInfo, DescriptorType, Type, ShaderStageFlagBit, - DescriptorSetLayoutBinding, Uniform, UniformBlock, UniformSamplerTexture, Device, Feature } from '../gfx'; + DescriptorSetLayoutBinding, Uniform, UniformBlock, UniformSamplerTexture, UniformStorageImage, Device, Feature } from '../gfx'; export const PIPELINE_FLOW_GBUFFER = 'GbufferFlow'; export const PIPELINE_FLOW_LIGHTING = 'LightingFlow'; @@ -104,7 +104,7 @@ export interface IRenderQueueDesc { export interface IDescriptorSetLayoutInfo { bindings: DescriptorSetLayoutBinding[]; - layouts: Record; + layouts: Record; } export const globalDescriptorSetLayout: IDescriptorSetLayoutInfo = { bindings: [], layouts: {} }; @@ -146,6 +146,8 @@ export enum ModelLocalBindings { SAMPLER_MORPH_TANGENT, SAMPLER_LIGHTMAP, SAMPLER_SPRITE, + SAMPLER_REFLECTION, + STORAGE_REFLECTION, COUNT, } @@ -344,7 +346,7 @@ export class UBOLocal { public static readonly NAME = 'CCLocal'; public static readonly BINDING = ModelLocalBindings.UBO_LOCAL; - public static readonly DESCRIPTOR = new DescriptorSetLayoutBinding(UBOLocal.BINDING, DescriptorType.UNIFORM_BUFFER, 1, ShaderStageFlagBit.VERTEX); + public static readonly DESCRIPTOR = new DescriptorSetLayoutBinding(UBOLocal.BINDING, DescriptorType.UNIFORM_BUFFER, 1, ShaderStageFlagBit.VERTEX | ShaderStageFlagBit.COMPUTE); public static readonly LAYOUT = new UniformBlock(SetIndex.LOCAL, UBOLocal.BINDING, UBOLocal.NAME, [ new Uniform('cc_matWorld', Type.MAT4, 1), new Uniform('cc_matWorldIT', Type.MAT4, 1), @@ -364,7 +366,7 @@ export class UBOLocalBatched { public static readonly NAME = 'CCLocalBatched'; public static readonly BINDING = ModelLocalBindings.UBO_LOCAL; - public static readonly DESCRIPTOR = new DescriptorSetLayoutBinding(UBOLocalBatched.BINDING, DescriptorType.UNIFORM_BUFFER, 1, ShaderStageFlagBit.VERTEX); + public static readonly DESCRIPTOR = new DescriptorSetLayoutBinding(UBOLocalBatched.BINDING, DescriptorType.UNIFORM_BUFFER, 1, ShaderStageFlagBit.VERTEX | ShaderStageFlagBit.COMPUTE); public static readonly LAYOUT = new UniformBlock(SetIndex.LOCAL, UBOLocalBatched.BINDING, UBOLocalBatched.NAME, [ new Uniform('cc_matWorlds', Type.MAT4, UBOLocalBatched.BATCHING_COUNT), ], 1); @@ -548,6 +550,28 @@ const UNIFORM_SPRITE_TEXTURE_LAYOUT = new UniformSamplerTexture(SetIndex.LOCAL, localDescriptorSetLayout.layouts[UNIFORM_SPRITE_TEXTURE_NAME] = UNIFORM_SPRITE_TEXTURE_LAYOUT; localDescriptorSetLayout.bindings[UNIFORM_SPRITE_TEXTURE_BINDING] = UNIFORM_SPRITE_TEXTURE_DESCRIPTOR; +/** + * @en The sampler for reflection + * @zh 反射纹理采样器。 + */ +const UNIFORM_REFLECTION_TEXTURE_NAME = 'cc_reflectionTexture'; +export const UNIFORM_REFLECTION_TEXTURE_BINDING = ModelLocalBindings.SAMPLER_REFLECTION; +const UNIFORM_REFLECTION_TEXTURE_DESCRIPTOR = new DescriptorSetLayoutBinding(UNIFORM_REFLECTION_TEXTURE_BINDING, DescriptorType.SAMPLER_TEXTURE, 1, ShaderStageFlagBit.FRAGMENT); +const UNIFORM_REFLECTION_TEXTURE_LAYOUT = new UniformSamplerTexture(SetIndex.LOCAL, UNIFORM_REFLECTION_TEXTURE_BINDING, UNIFORM_REFLECTION_TEXTURE_NAME, Type.SAMPLER2D, 1); +localDescriptorSetLayout.layouts[UNIFORM_REFLECTION_TEXTURE_NAME] = UNIFORM_REFLECTION_TEXTURE_LAYOUT; +localDescriptorSetLayout.bindings[UNIFORM_REFLECTION_TEXTURE_BINDING] = UNIFORM_REFLECTION_TEXTURE_DESCRIPTOR; + +/** + * @en The storage image for reflection + * @zh 反射纹理存储。 + */ +const UNIFORM_REFLECTION_STORAGE_NAME = 'cc_reflectionStorage'; +export const UNIFORM_REFLECTION_STORAGE_BINDING = ModelLocalBindings.STORAGE_REFLECTION; +const UNIFORM_REFLECTION_STORAGE_DESCRIPTOR = new DescriptorSetLayoutBinding(UNIFORM_REFLECTION_STORAGE_BINDING, DescriptorType.STORAGE_IMAGE, 1, ShaderStageFlagBit.COMPUTE); +const UNIFORM_REFLECTION_STORAGE_LAYOUT = new UniformStorageImage(SetIndex.LOCAL, UNIFORM_REFLECTION_STORAGE_BINDING, UNIFORM_REFLECTION_STORAGE_NAME, Type.IMAGE2D, 1); +localDescriptorSetLayout.layouts[UNIFORM_REFLECTION_STORAGE_NAME] = UNIFORM_REFLECTION_STORAGE_LAYOUT; +localDescriptorSetLayout.bindings[UNIFORM_REFLECTION_STORAGE_BINDING] = UNIFORM_REFLECTION_STORAGE_DESCRIPTOR; + export const CAMERA_DEFAULT_MASK = Layers.makeMaskExclude([Layers.BitMask.UI_2D, Layers.BitMask.GIZMOS, Layers.BitMask.EDITOR, Layers.BitMask.SCENE_GIZMO, Layers.BitMask.PROFILER]); diff --git a/cocos/core/renderer/scene/submodel.ts b/cocos/core/renderer/scene/submodel.ts index e770c2aa4f..0b439efe6b 100644 --- a/cocos/core/renderer/scene/submodel.ts +++ b/cocos/core/renderer/scene/submodel.ts @@ -25,14 +25,17 @@ import { JSB } from 'internal:constants'; import { RenderingSubMesh } from '../../assets/rendering-sub-mesh'; -import { RenderPriority } from '../../pipeline/define'; +import { RenderPriority, UNIFORM_REFLECTION_TEXTURE_BINDING, UNIFORM_REFLECTION_STORAGE_BINDING } from '../../pipeline/define'; import { BatchingSchemes, IMacroPatch, Pass } from '../core/pass'; -import { DescriptorSet, DescriptorSetInfo, Device, InputAssembler, InputAssemblerInfo, Shader } from '../../gfx'; +import { DescriptorSet, DescriptorSetInfo, Device, InputAssembler, InputAssemblerInfo, Texture, TextureType, TextureUsageBit, TextureInfo, + Format, Sampler, Filter, Address, TextureFlagBit, Shader } from '../../gfx'; import { legacyCC } from '../../global-exports'; import { ForwardPipeline } from '../../pipeline'; import { errorID } from '../../platform/debug'; import { Shadows } from './shadows'; import { NativePass, NativeSubModel } from './native-scene'; +import { getPhaseID } from '../../pipeline/pass-phase'; +import { genSamplerHash, samplerLib } from '../core/sampler-lib'; const _dsInfo = new DescriptorSetInfo(null!); const MAX_PASS_COUNT = 8; @@ -46,6 +49,8 @@ export class SubModel { protected _descriptorSet: DescriptorSet | null = null; protected _planarInstanceShader: Shader | null = null; protected _planarShader: Shader | null = null; + protected _reflectionTex: Texture | null = null; + protected _reflectionSampler: Sampler | null = null; protected declare _nativeObj: NativeSubModel | null; private _destroyDescriptorSet () { @@ -172,6 +177,46 @@ export class SubModel { if (passes[0].batchingScheme === BatchingSchemes.VB_MERGING) { this.subMesh.genFlatBuffers(); } this.priority = RenderPriority.DEFAULT; + + // initialize resources for reflection material + if (passes[0].phase === getPhaseID('reflection')) { + let texWidth = this._device.width; + let texHeight = this._device.height; + const minSize = 512; + + if (texHeight < texWidth) { + texWidth = minSize * texWidth / texHeight; + texHeight = minSize; + } else { + texWidth = minSize; + texHeight = minSize * texHeight / texWidth; + } + + this._reflectionTex = this._device.createTexture(new TextureInfo( + TextureType.TEX2D, + TextureUsageBit.STORAGE | TextureUsageBit.TRANSFER_SRC | TextureUsageBit.SAMPLED, + Format.RGBA8, + texWidth, + texHeight, + TextureFlagBit.IMMUTABLE, + )); + + this.descriptorSet.bindTexture(UNIFORM_REFLECTION_TEXTURE_BINDING, this._reflectionTex); + + const samplerInfo = [ + Filter.LINEAR, + Filter.LINEAR, + Filter.NONE, + Address.CLAMP, + Address.CLAMP, + Address.CLAMP, + ]; + + const samplerHash = genSamplerHash(samplerInfo); + this._reflectionSampler = samplerLib.getSampler(this._device, samplerHash); + this.descriptorSet.bindSampler(UNIFORM_REFLECTION_TEXTURE_BINDING, this._reflectionSampler); + this.descriptorSet.bindTexture(UNIFORM_REFLECTION_STORAGE_BINDING, this._reflectionTex); + } } private _initNativePlanarShadowShader (shadowInfo: Shadows) { @@ -219,6 +264,11 @@ export class SubModel { this._subMesh = null; this._passes = null; + if (this._reflectionTex) this._reflectionTex.destroy(); + this._reflectionTex = null; + if (this._reflectionSampler) this._reflectionSampler.destroy(); + this._reflectionSampler = null; + this._destroy(); } diff --git a/editor/assets/effects/builtin-reflection-deferred.effect b/editor/assets/effects/builtin-reflection-deferred.effect new file mode 100644 index 0000000000..f60f42b049 --- /dev/null +++ b/editor/assets/effects/builtin-reflection-deferred.effect @@ -0,0 +1,248 @@ +// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd. + +CCEffect %{ + techniques: + - passes: + - vert: standard-vs + frag: standard-fs + phase: reflection + depthStencilState: + depthTest: true + depthWrite: false + blendState: + targets: + - blend: true + blendSrc: src_alpha + blendDst: one_minus_src_alpha + blendDstAlpha: one_minus_src_alpha + properties: &props + 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 } } + occlusion: { value: 1.0, target: pbrParams.x } + roughness: { value: 0.8, target: pbrParams.y } + metallic: { value: 0.6, target: pbrParams.z } + normalStrenth: { value: 1.0, target: pbrParams.w, editor: { parent: USE_NORMAL_MAP } } + emissive: { value: [0.0, 0.0, 0.0, 1.0], editor: { type: color } } + emissiveScale: { value: [1.0, 1.0, 1.0], target: emissiveScaleParam.xyz } + mainTexture: { value: grey, target: albedoMap, editor: { displayName: AlbedoMap } } + normalMap: { value: normal } + pbrMap: { value: grey } + metallicRoughnessMap: { value: grey } + occlusionMap: { value: white } + emissiveMap: { value: grey } +}% + +CCProgram shared-ubos %{ + uniform Constants { + vec4 tilingOffset; + vec4 albedo; + vec4 albedoScaleAndCutoff; + vec4 pbrParams; + vec4 emissive; + vec4 emissiveScaleParam; + }; +}% + +CCProgram standard-vs %{ + precision highp float; + #include + #include + #include + #include + #include + #include + + #if USE_VERTEX_COLOR + in vec4 a_color; + out vec4 v_color; + #endif + + out vec3 v_position; + out vec3 v_normal; + out vec2 v_uv; + out vec2 v_uv1; + + #if USE_NORMAL_MAP + out vec3 v_tangent; + out vec3 v_bitangent; + #endif + + #if HAS_SECOND_UV || USE_LIGHTMAP + in vec2 a_texCoord1; + #endif + + #if USE_LIGHTMAP && !USE_BATCHING && !CC_FORWARD_ADD + #include + #endif + + void main () { + StandardVertInput In; + CCVertInput(In); + + mat4 matWorld, matWorldIT; + CCGetWorldMatrixFull(matWorld, matWorldIT); + + vec4 pos = matWorld * In.position; + + v_position = pos.xyz; + v_normal = normalize((matWorldIT * vec4(In.normal, 0.0)).xyz); + #if USE_NORMAL_MAP + v_tangent = normalize((matWorld * vec4(In.tangent.xyz, 0.0)).xyz); + v_bitangent = cross(v_normal, v_tangent) * In.tangent.w; // note the cross order + #endif + + v_uv = a_texCoord * tilingOffset.xy + tilingOffset.zw; + #if HAS_SECOND_UV + v_uv1 = a_texCoord1 * tilingOffset.xy + tilingOffset.zw; + #endif + + #if USE_VERTEX_COLOR + v_color = a_color; + #endif + + CC_TRANSFER_FOG(pos); + CC_TRANSFER_SHADOW(pos); + + #if USE_LIGHTMAP && !USE_BATCHING && !CC_FORWARD_ADD + CCLightingMapCaclUV(); + #endif + + + gl_Position = cc_matProj * (cc_matView * matWorld) * In.position; + } +}% + +CCProgram standard-fs %{ + precision highp float; + #include + #include + #include + #include + #if USE_LIGHTMAP && !USE_BATCHING && !CC_FORWARD_ADD + #include + #endif + + in vec3 v_position; + in vec2 v_uv; + in vec2 v_uv1; + in vec3 v_normal; + + #if USE_VERTEX_COLOR + in vec4 v_color; + #endif + + #if USE_ALBEDO_MAP + uniform sampler2D albedoMap; + #pragma define ALBEDO_UV options([v_uv, v_uv1]) + #endif + #if USE_NORMAL_MAP + in vec3 v_tangent; + in vec3 v_bitangent; + uniform sampler2D normalMap; + #pragma define NORMAL_UV options([v_uv, v_uv1]) + #endif + #pragma define PBR_UV options([v_uv, v_uv1]) + #if USE_PBR_MAP + uniform sampler2D pbrMap; + #endif + #if USE_METALLIC_ROUGHNESS_MAP + uniform sampler2D metallicRoughnessMap; + #endif + #if USE_OCCLUSION_MAP + uniform sampler2D occlusionMap; + #endif + #if USE_EMISSIVE_MAP + uniform sampler2D emissiveMap; + #pragma define EMISSIVE_UV options([v_uv, v_uv1]) + #endif + + #define OCCLUSION_CHANNEL r + #define ROUGHNESS_CHANNEL g + #define METALLIC_CHANNEL b + + #if USE_ALPHA_TEST + #pragma define ALPHA_TEST_CHANNEL options([a, r]) + #endif + + void surf (out StandardSurface s) { + vec4 baseColor = albedo; + #if USE_VERTEX_COLOR + baseColor *= v_color; + #endif + #if USE_ALBEDO_MAP + vec4 texColor = texture(albedoMap, ALBEDO_UV); + texColor.rgb = SRGBToLinear(texColor.rgb); + baseColor *= texColor; + #endif + s.albedo = baseColor; + s.albedo.rgb *= albedoScaleAndCutoff.xyz; + + #if USE_ALPHA_TEST + if (s.albedo.ALPHA_TEST_CHANNEL < albedoScaleAndCutoff.w) discard; + #endif + + #if USE_LIGHTMAP && !USE_BATCHING && !CC_FORWARD_ADD + s.lightmap = texture(cc_lightingMap, v_luv); + #endif + + s.normal = v_normal; + #if USE_NORMAL_MAP + vec3 nmmp = texture(normalMap, NORMAL_UV).xyz - vec3(0.5); + s.normal = + (nmmp.x * pbrParams.w) * normalize(v_tangent) + + (nmmp.y * pbrParams.w) * normalize(v_bitangent) + + nmmp.z * normalize(s.normal); + #endif + + s.position = v_position; + + vec4 pbr = pbrParams; + #if USE_PBR_MAP + vec4 res = texture(pbrMap, PBR_UV); + pbr.x *= res.OCCLUSION_CHANNEL; + pbr.y *= res.ROUGHNESS_CHANNEL; + pbr.z *= res.METALLIC_CHANNEL; + #endif + #if USE_METALLIC_ROUGHNESS_MAP + vec4 metallicRoughness = texture(metallicRoughnessMap, PBR_UV); + pbr.z *= metallicRoughness.METALLIC_CHANNEL; + pbr.y *= metallicRoughness.ROUGHNESS_CHANNEL; + #endif + #if USE_OCCLUSION_MAP + pbr.x *= texture(occlusionMap, PBR_UV).OCCLUSION_CHANNEL; + #endif + s.occlusion = clamp(pbr.x, 0.0, 0.96); + s.roughness = clamp(pbr.y, 0.04, 1.0); + s.metallic = pbr.z; + + s.emissive = emissive.rgb * emissiveScaleParam.xyz; + #if USE_EMISSIVE_MAP + s.emissive *= SRGBToLinear(texture(emissiveMap, EMISSIVE_UV).rgb); + #endif + } + + #pragma builtin(local) + layout(set = 2, binding = 11) uniform sampler2D cc_reflectionTexture; + + layout(location = 0) out vec4 fragColorX; + + void main () { + StandardSurface s; surf(s); + vec4 color = CCStandardShadingBase(s, CC_SHADOW_POSITION); + CC_APPLY_FOG(color); + vec4 baseColor = CCFragOutput(color); + + vec2 noise = v_normal.xy * 0.02; + #if USE_NORMAL_MAP + noise = texture(normalMap, ALBEDO_UV).xy * 0.02; + #endif + + vec2 resolution = cc_screenSize.xy; + vec2 uv = (gl_FragCoord.xy / resolution) + noise; + vec4 reflectionColor = texture(cc_reflectionTexture, uv); + fragColorX = mix(baseColor, reflectionColor, 0.5); + fragColorX.w = 1.0; + } +}% \ No newline at end of file diff --git a/editor/assets/effects/builtin-reflection-deferred.effect.meta b/editor/assets/effects/builtin-reflection-deferred.effect.meta new file mode 100644 index 0000000000..6acebc99e4 --- /dev/null +++ b/editor/assets/effects/builtin-reflection-deferred.effect.meta @@ -0,0 +1,11 @@ +{ + "ver": "1.4.6", + "importer": "effect", + "imported": true, + "uuid": "99498f84-efe6-43a6-a9a7-e6e93eb845c1", + "files": [ + ".json" + ], + "subMetas": {}, + "userData": {} +}