[shaders] further preprocessing cleanup

- move sequence of preprocessing steps out of ShaderPreprocessor module and into UE::ShaderCompilerCommon::ExecuteShaderPreprocessingSteps; the former is now explicitly just the low-level preprocessor lib
- add an implementation of PreprocessShader in FBaseShaderFormat so backends which have no custom code to execute as part of preprocessing can just automatically inherit this implementation, and fix up such backends to eliminate now-unnecessary overrides

#rb christopher.waters, Laura.Hermanns

[CL 30178136 by dan elksnitis in ue5-main branch]
This commit is contained in:
dan elksnitis
2023-12-07 08:55:41 -05:00
parent c1c26e32b3
commit 6ed653a189
11 changed files with 99 additions and 120 deletions

View File

@@ -6,7 +6,6 @@
#include "Misc/ScopeLock.h"
#include "Modules/ModuleManager.h"
#include "PreprocessorPrivate.h"
#include "ShaderCompilerCommon.h"
#include "ShaderCompilerDefinitions.h"
#include "stb_preprocess/preprocessor.h"
#include "stb_preprocess/stb_alloc.h"
@@ -76,25 +75,6 @@ static void AddStbDefines(stb_arena* MacroArena, macro_definition**& StbDefines,
class FShaderPreprocessorUtilities
{
public:
static void DumpShaderDefinesAsCommentedCode(const FShaderCompilerEnvironment& Environment, FString* OutDefines)
{
TArray<FString> DefinesLines;
DefinesLines.Reserve(Environment.Definitions->Num());
for (FShaderCompilerDefinitions::FConstIterator DefineIt(*Environment.Definitions); DefineIt; ++DefineIt)
{
DefinesLines.Add(FString::Printf(TEXT("// #define %s %s\n"), DefineIt.Key(), DefineIt.Value()));
}
DefinesLines.Sort();
FString Defines;
for (const FString& DefineLine : DefinesLines)
{
Defines += DefineLine;
}
*OutDefines += MakeInjectedShaderCodeBlock(TEXT("DumpShaderDefinesAsCommentedCode"), Defines);
}
static void PopulateDefines(const FShaderCompilerEnvironment& Environment, const FShaderCompilerDefinitions& AdditionalDefines, stb_arena* MacroArena, macro_definition**& OutDefines)
{
arrsetcap(OutDefines, Environment.Definitions->Num() + AdditionalDefines.Num());
@@ -691,15 +671,33 @@ static void AddStbDefines(stb_arena* MacroArena, macro_definition**& StbDefines,
}
}
bool InnerPreprocessShaderStb(
PRAGMA_ENABLE_DEPRECATION_WARNINGS
/**
* Preprocess a shader.
* @param OutPreprocessedShader - Upon return contains the preprocessed source code.
* @param ShaderOutput - ShaderOutput to which errors can be added.
* @param ShaderInput - The shader compiler input.
* @param AdditionalDefines - Additional defines with which to preprocess the shader.
* @param DefinesPolicy - Whether to add shader definitions as comments.
* @returns true if the shader is preprocessed without error.
*/
bool PreprocessShader(
FShaderPreprocessOutput& Output,
const FShaderCompilerInput& Input,
const FShaderCompilerEnvironment& Environment,
PRAGMA_DISABLE_DEPRECATION_WARNINGS // FShaderCompilerDefinitions will be made internal in the future, marked deprecated until then
const FShaderCompilerDefinitions& AdditionalDefines
PRAGMA_ENABLE_DEPRECATION_WARNINGS
)
{
TRACE_CPUPROFILER_EVENT_SCOPE(PreprocessShader);
stb_arena MacroArena = { 0 };
macro_definition** StbDefines = nullptr;
PRAGMA_DISABLE_DEPRECATION_WARNINGS
FShaderPreprocessorUtilities::PopulateDefines(Environment, AdditionalDefines, &MacroArena, StbDefines);
// The substitution text generated by custom macros gets run through the preprocessor afterwards, but in some cases we want to
@@ -863,64 +861,4 @@ bool PreprocessShader(
ShaderOutput.Errors.Add(MoveTemp(Error));
}
return bSucceeded;
}
/**
* Preprocess a shader.
* @param OutPreprocessedShader - Upon return contains the preprocessed source code.
* @param ShaderOutput - ShaderOutput to which errors can be added.
* @param ShaderInput - The shader compiler input.
* @param AdditionalDefines - Additional defines with which to preprocess the shader.
* @param DefinesPolicy - Whether to add shader definitions as comments.
* @returns true if the shader is preprocessed without error.
*/
bool PreprocessShader(
FShaderPreprocessOutput& Output,
const FShaderCompilerInput& Input,
const FShaderCompilerEnvironment& Environment,
PRAGMA_DISABLE_DEPRECATION_WARNINGS // FShaderCompilerDefinitions will be made internal in the future, marked deprecated until then
const FShaderCompilerDefinitions& AdditionalDefines
PRAGMA_ENABLE_DEPRECATION_WARNINGS
)
{
TRACE_CPUPROFILER_EVENT_SCOPE(PreprocessShader);
Output.EditSource().Empty();
if (EnumHasAnyFlags(Input.DebugInfoFlags, EShaderDebugInfoFlags::CompileFromDebugUSF))
{
// the "VirtualSourceFilePath" given is actually an absolute path to a dumped debug USF file; load it directly.
// this occurs when running SCW in "direct compile" mode; this file will already be preprocessed.
bool bSuccess = FFileHelper::LoadFileToString(Output.EditSource(), *Input.VirtualSourceFilePath);
if (bSuccess)
{
// const_cast for compile environment; need to populate a subset of environment parameters from parsing comments in the preprocessed code
UE::ShaderCompilerCommon::SerializeEnvironmentFromBase64(const_cast<FShaderCompilerEnvironment&>(Input.Environment), Output.GetSource());
// strip comments from source when loading from a debug USF. some backends don't handle the comments that the debug dump inserts properly.
// this (currently) incurs an extra conversion (TCHAR -> ANSI -> TCHAR), but since this is only used in the debug path the perf hit is irrelevant
TArray<ANSICHAR> Stripped;
ShaderConvertAndStripComments(Output.GetSource(), Stripped);
Output.EditSource() = Stripped.GetData();
}
return bSuccess;
}
check(CheckVirtualShaderFilePath(Input.VirtualSourceFilePath));
bool bSuccess = InnerPreprocessShaderStb(Output, Input, Environment, AdditionalDefines);
if (bSuccess)
{
CleanupUniformBufferCode(Environment, Output.EditSource());
if (Input.Environment.CompilerFlags.Contains(CFLAG_RemoveDeadCode))
{
const TArray<FStringView> RequiredSymbols(MakeArrayView(Input.RequiredSymbols));
UE::ShaderCompilerCommon::RemoveDeadCode(Output.EditSource(), Input.EntryPointName, RequiredSymbols, Output.EditErrors());
}
}
return bSuccess;
}
}

View File

@@ -10,8 +10,6 @@ public class ShaderPreprocessor : ModuleRules
new string[] {
"Core",
"RenderCore",
"ShaderCompilerCommon",
"TargetPlatform",
}
);