You've already forked UnrealEngineUWP
mirror of
https://github.com/izzy2lost/UnrealEngineUWP.git
synced 2026-03-26 18:15:20 -07:00
- never append the environment defines as commented code to the source used for further preprocessing/compilation; instead only append it to the debug USF - strip comments after loading the debug usf in direct compile mode as some backends expect comments to have already been removed and the extra ones we add to the debug dump cause them to barf - change all #if 0s in the debug usf to block comments instead so the above can strip them (said backends also don't like preprocessor directives left in the file) #rb Jason.Nadro, rob.krajcarski [CL 30161438 by dan elksnitis in ue5-main branch]
170 lines
5.4 KiB
C++
170 lines
5.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);
|
|
return PreprocessShader(Output, Input, Environment);
|
|
}
|
|
|
|
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);
|
|
}
|