diff --git a/03-UnrealEngine/卡通渲染相关资料/渲染功能/ShaderModel/GBuffer&Material&BasePass.md b/03-UnrealEngine/卡通渲染相关资料/渲染功能/ShaderModel/GBuffer&Material&BasePass.md index 3d80b87..61641e1 100644 --- a/03-UnrealEngine/卡通渲染相关资料/渲染功能/ShaderModel/GBuffer&Material&BasePass.md +++ b/03-UnrealEngine/卡通渲染相关资料/渲染功能/ShaderModel/GBuffer&Material&BasePass.md @@ -339,7 +339,97 @@ bool FMaterialInstanceResource::GetParameterValue(EMaterialParameterType Type, c } ``` -### 是否需要Toon + +### BasePass EncodeGBufferToMRT/DecodeGBufferDataDirect逻辑笔记 +主要逻辑位于FShaderCompileUtilities::WriteGBufferInfoAutogen(): +```c++ +void FShaderCompileUtilities::WriteGBufferInfoAutogen(EShaderPlatform TargetPlatform, ERHIFeatureLevel::Type FeatureLevel = ERHIFeatureLevel::SM5) +{ + FGBufferParams DefaultParams = FetchGBufferParamsPipeline(TargetPlatform, GBL_Default); + FScopeLock MapLock(&GCriticalSection); + + // For now, the logic always calculates the new GBuffer, and if it's the first time, write it, otherwise check it hasn't changed. We are doing this for + // debugging, and in the near future it will only calculate the GBuffer on the first time only. + FGBufferInfo DefaultBufferInfo = FetchFullGBufferInfo(DefaultParams); + FString AutoGenDirectory = GetAutoGenDirectory(TargetPlatform); + FString AutogenHeaderFilename = AutoGenDirectory / TEXT("AutogenShaderHeaders.ush"); + FString AutogenHeaderFilenameTemp = AutoGenDirectory / TEXT("AutogenShaderHeaders_temp.ush"); + + if (GLastGBufferIsValid[TargetPlatform]) + { + const bool bSame = IsGBufferInfoEqual(GLastGBufferInfo[TargetPlatform], DefaultBufferInfo);//判断GBufferInfo是否相同,不同则触发断言 + check(bSame); + } + else + { + GLastGBufferIsValid[TargetPlatform] = true; + // should cache this properly, and serialize it, but this is a temporary fix. + GLastGBufferInfo[TargetPlatform] = DefaultBufferInfo; + + FString OutputFileData; + OutputFileData += TEXT("// Copyright Epic Games, Inc. All Rights Reserved.\n"); + OutputFileData += TEXT("\n"); + OutputFileData += TEXT("#pragma once\n"); + OutputFileData += TEXT("\n"); + + OutputFileData += TEXT("#if FEATURE_LEVEL >= FEATURE_LEVEL_SM5\n"); + OutputFileData += TEXT("float SampleDeviceZFromSceneTexturesTempCopy(float2 UV)\n"); + OutputFileData += TEXT("{\n"); + OutputFileData += TEXT("\treturn SceneDepthTexture.SampleLevel(SceneDepthTextureSampler, UV, 0).r;\n"); + OutputFileData += TEXT("}\n"); + OutputFileData += TEXT("#endif\n"); + OutputFileData += TEXT("\n"); + + OutputFileData += TEXT("#ifndef GBUFFER_LAYOUT\n"); + OutputFileData += TEXT("#define GBUFFER_LAYOUT 0\n"); + OutputFileData += TEXT("#endif\n"); + OutputFileData += TEXT("\n"); + + for (uint32 Layout = 0; Layout < GBL_Num; ++Layout) + { + FGBufferParams Params = FetchGBufferParamsPipeline(TargetPlatform, (EGBufferLayout)Layout); + FGBufferInfo BufferInfo = FetchFullGBufferInfo(Params); + + OutputFileData.Appendf(TEXT("#if GBUFFER_LAYOUT == %u\n\n"), Layout); + OutputFileData += CreateGBufferEncodeFunction(BufferInfo); + + OutputFileData += TEXT("\n"); + + OutputFileData += CreateGBufferDecodeFunctionDirect(BufferInfo); + + OutputFileData += TEXT("\n"); + //OutputFileData += TEXT("#if SHADING_PATH_DEFERRED\n"); + OutputFileData += TEXT("#if FEATURE_LEVEL >= FEATURE_LEVEL_SM5\n"); + OutputFileData += TEXT("\n"); + + OutputFileData += CreateGBufferDecodeFunctionVariation(BufferInfo, EGBufferDecodeType::CoordUV, FeatureLevel); + OutputFileData += TEXT("\n"); + + OutputFileData += CreateGBufferDecodeFunctionVariation(BufferInfo, EGBufferDecodeType::CoordUInt, FeatureLevel); + + OutputFileData += TEXT("\n"); + + OutputFileData += CreateGBufferDecodeFunctionVariation(BufferInfo, EGBufferDecodeType::SceneTextures, FeatureLevel); + OutputFileData += TEXT("\n"); + + OutputFileData += CreateGBufferDecodeFunctionVariation(BufferInfo, EGBufferDecodeType::SceneTexturesLoad, FeatureLevel); + OutputFileData += TEXT("\n"); + + OutputFileData += TEXT("#endif\n"); + OutputFileData += TEXT("\n"); + + OutputFileData += TEXT("#endif\n"); + OutputFileData += TEXT("\n"); + } + ... +} +``` +写入内容与这2句获取的FGbufferInfo有关:`FGBufferParams Params = FetchGBufferParamsPipeline(TargetPlatform, (EGBufferLayout)Layout);`和`FGBufferInfo BufferInfo = FetchFullGBufferInfo(Params);` + + +![[ShaderGenerationUtil_CreateGBufferEncodeFunction.png|1200]] + +## 是否需要Toon 在材质中: ```c++ FMaterialRelevance UMaterialInterface::GetRelevance_Internal(const UMaterial* Material, ERHIFeatureLevel::Type InFeatureLevel) const diff --git a/08-Assets/Images/ImageBag/Rendering/GBuffer/ShaderGenerationUtil_CreateGBufferEncodeFunction.png b/08-Assets/Images/ImageBag/Rendering/GBuffer/ShaderGenerationUtil_CreateGBufferEncodeFunction.png new file mode 100644 index 0000000..bc72e40 Binary files /dev/null and b/08-Assets/Images/ImageBag/Rendering/GBuffer/ShaderGenerationUtil_CreateGBufferEncodeFunction.png differ