Files
UnrealEngineUWP/Engine/Source/Developer/ShaderFormatVectorVM/Private/VectorVMShaderCompiler.cpp
dan elksnitis 9064172949 [shaders] migration of VectorVM shader format to independent preprocessing API (along with some cleanup)
#rb Rob.Krajcarski
#rb Laura.Hermanns

[CL 27106719 by dan elksnitis in ue5-main branch]
2023-08-15 12:28:03 -04:00

198 lines
6.4 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
// .
#include "CoreMinimal.h"
#include "CrossCompiler.h"
#include "CrossCompilerCommon.h"
#include "HAL/CriticalSection.h"
#include "Misc/FileHelper.h"
#include "Serialization/MemoryWriter.h"
#include "ShaderCompilerCommon.h"
#include "ShaderCore.h"
#include "ShaderFormatVectorVM.h"
#include "ShaderPreprocessor.h"
#include "ShaderPreprocessTypes.h"
#include "ShaderCompilerDefinitions.h"
#include "VectorVM.h"
#include "VectorVMBackend.h"
#include "VectorVMTestCompile.h"
DEFINE_LOG_CATEGORY_STATIC(LogVectorVMShaderCompiler, Log, All);
DECLARE_STATS_GROUP(TEXT("VectorVM"), STATGROUP_VectorVM, STATCAT_Advanced);
DECLARE_CYCLE_STAT(TEXT("VectorVM - Compiler - CompileShader_VectorVM"), STAT_VectorVM_Compiler_CompileShader_VectorVM, STATGROUP_VectorVM);
DECLARE_CYCLE_STAT(TEXT("VectorVM - Compiler - PreprocessShader"), STAT_VectorVM_Compiler_CompileShader_VectorVMPreprocessShader, STATGROUP_VectorVM);
DECLARE_CYCLE_STAT(TEXT("VectorVM - Compiler - CrossCompilerContextRun"), STAT_VectorVM_Compiler_CompileShader_CrossCompilerContextRun, STATGROUP_VectorVM);
bool PreprocessVectorVMShader(const FShaderCompilerInput& Input, const FShaderCompilerEnvironment& Environment, FShaderPreprocessOutput& Output)
{
SCOPE_CYCLE_COUNTER(STAT_VectorVM_Compiler_CompileShader_VectorVMPreprocessShader);
PRAGMA_DISABLE_DEPRECATION_WARNINGS // FShaderCompilerDefinitions will be made internal in the future, marked deprecated until then
FShaderCompilerDefinitions AdditionalDefines;
AdditionalDefines.SetDefine(TEXT("COMPILER_HLSLCC"), 1);
AdditionalDefines.SetDefine(TEXT("COMPILER_VECTORVM"), 1);
AdditionalDefines.SetDefine(TEXT("VECTORVM_PROFILE"), 1);
AdditionalDefines.SetDefine(TEXT("FORCE_FLOATS"), (uint32)1);
PRAGMA_ENABLE_DEPRECATION_WARNINGS
if (Input.bSkipPreprocessedCache)
{
if (!FFileHelper::LoadFileToString(Output.EditSource(), *Input.VirtualSourceFilePath))
{
return false;
}
// Remove const as we are on debug-only mode
CrossCompiler::CreateEnvironmentFromResourceTable(Output.GetSource(), (FShaderCompilerEnvironment&)Input.Environment);
}
else
{
// Don't include shader definitions since it creates shader compilation errors.
if (!PreprocessShader(Output, Input, Environment, AdditionalDefines, EDumpShaderDefines::DontIncludeDefines))
{
// The preprocessing stage will add any relevant errors.
return false;
}
}
return true;
}
bool CompileVectorVMShader(
const FShaderCompilerInput& Input,
const FShaderPreprocessOutput& PreprocessOutput,
FVectorVMCompilationOutput& VMCompilationOutput,
const FString& WorkingDirectory,
bool bSkipBackendOptimizations)
{
SCOPE_CYCLE_COUNTER(STAT_VectorVM_Compiler_CompileShader_VectorVM);
EHlslCompileTarget HlslCompilerTarget = HCT_FeatureLevelSM5;
ECompilerFlags PlatformFlowControl = CFLAG_AvoidFlowControl;
char* ShaderSource = NULL;
char* ErrorLog = NULL;
const EHlslShaderFrequency Frequency = HSF_VertexShader;
uint32 CCFlags = HLSLCC_NoPreprocess;
if (bSkipBackendOptimizations)
{
CCFlags |= HLSLCC_DisableBackendOptimizations;
}
FVectorVMCodeBackend VVMBackEnd(CCFlags, HlslCompilerTarget, VMCompilationOutput);
FVectorVMLanguageSpec VVMLanguageSpec;
bool bResult = false;
{
FScopeLock HlslCcLock(CrossCompiler::GetCrossCompilerLock());
FHlslCrossCompilerContext CrossCompilerContext(CCFlags, Frequency, HlslCompilerTarget);
if (CrossCompilerContext.Init(TCHAR_TO_ANSI(*Input.VirtualSourceFilePath), &VVMLanguageSpec))
{
SCOPE_CYCLE_COUNTER(STAT_VectorVM_Compiler_CompileShader_CrossCompilerContextRun);
bResult = CrossCompilerContext.Run(
TCHAR_TO_ANSI(*PreprocessOutput.GetSource()),
TCHAR_TO_ANSI(*Input.EntryPointName),
&VVMBackEnd,
&ShaderSource,
&ErrorLog);
}
}
if (ErrorLog)
{
int32 SrcLen = FPlatformString::Strlen(ErrorLog);
int32 DestLen = FPlatformString::ConvertedLength<TCHAR, ANSICHAR>(ErrorLog, SrcLen);
TArray<TCHAR> Converted;
Converted.AddUninitialized(DestLen);
FPlatformString::Convert<ANSICHAR, TCHAR>(Converted.GetData(), DestLen, ErrorLog, SrcLen);
Converted.Add(0);
VMCompilationOutput.Errors = Converted.GetData();
TArray<FString> OutputByLines;
PreprocessOutput.GetSource().ParseIntoArrayLines(OutputByLines, false);
UE_LOG(LogVectorVMShaderCompiler, Warning, TEXT("Warnings while processing %s"), *Input.DebugGroupName);
FString OutputHlsl;
for (int32 i = 0; i < OutputByLines.Num(); i++)
{
UE_LOG(LogVectorVMShaderCompiler, Display, TEXT("/*%d*/%s"), i, *OutputByLines[i]);
}
}
else
{
VMCompilationOutput.Errors.Empty();
}
if (ShaderSource)
{
UE_LOG(LogVectorVMShaderCompiler, Warning, TEXT("%hs"), (const char*)ShaderSource);
free(ShaderSource);
}
if (VMCompilationOutput.Errors.Len() != 0)
{
UE_LOG(LogVectorVMShaderCompiler, Warning, TEXT("%s"), *VMCompilationOutput.Errors);
free(ErrorLog);
}
return bResult;
}
bool CompileVectorVMShader(const FShaderCompilerInput& Input, const FShaderPreprocessOutput& PreprocessOutput, FShaderCompilerOutput& Output, const FString& WorkingDirectory)
{
FVectorVMCompilationOutput CompilationOutput;
bool bResult = CompileVectorVMShader(Input, PreprocessOutput, CompilationOutput, WorkingDirectory, Input.Environment.CompilerFlags.Contains(CFLAG_SkipOptimizations));
if (bResult)
{
FMemoryWriter Ar(Output.ShaderCode.GetWriteAccess(), true);
Ar << CompilationOutput;
Output.bSucceeded = true;
}
else if (CompilationOutput.Errors.Len() > 0)
{
Output.Errors.Add(FShaderCompilerError(*CompilationOutput.Errors));
}
return bResult;
}
void OutputVectorVMDebugData(
const FShaderCompilerInput& Input,
const FShaderPreprocessOutput& PreprocessOutput,
const FShaderCompilerOutput& Output)
{
UE::ShaderCompilerCommon::DumpExtendedDebugShaderData(Input, PreprocessOutput, Output);
}
bool TestCompileVectorVMShader(
const FShaderCompilerInput& Input,
const FString& WorkingDirectory,
FVectorVMCompilationOutput& VMCompilationOutput,
bool bSkipBackendOptimizations)
{
FShaderPreprocessOutput PreprocessOutput;
if (!PreprocessVectorVMShader(Input, Input.Environment, PreprocessOutput))
{
if (PreprocessOutput.GetErrors().Num() != 0)
{
FString Errors;
for (const FShaderCompilerError& Error : PreprocessOutput.GetErrors())
{
Errors += Error.GetErrorString() + TEXT("\r\n");
}
VMCompilationOutput.Errors = Errors;
}
return false;
}
return CompileVectorVMShader(Input, PreprocessOutput, VMCompilationOutput, WorkingDirectory, bSkipBackendOptimizations);
}