2019-12-26 14:45:42 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
2019-02-25 09:08:54 -05:00
# include "ShaderPrint.h"
# include "ShaderPrintParameters.h"
2022-02-22 15:35:01 -05:00
# include "ShaderParameterStruct.h"
2019-02-25 09:08:54 -05:00
# include "CommonRenderResources.h"
# include "Containers/DynamicRHIResourceArray.h"
# include "Engine/Engine.h"
# include "GlobalShader.h"
# include "PipelineStateCache.h"
# include "RenderGraphBuilder.h"
# include "SceneRendering.h"
# include "SystemTextures.h"
2022-02-20 07:20:20 -05:00
# include "ScenePrivate.h"
2019-02-25 09:08:54 -05:00
namespace ShaderPrint
{
2022-02-22 15:35:01 -05:00
//////////////////////////////////////////////////////////////////////////////////////////////////
2019-02-25 09:08:54 -05:00
// Console variables
2022-02-22 15:35:01 -05:00
2020-07-06 18:58:26 -04:00
TAutoConsoleVariable < int32 > CVarEnable (
2022-02-22 15:35:01 -05:00
TEXT ( " r.ShaderPrint " ) ,
2019-02-25 09:08:54 -05:00
0 ,
TEXT ( " ShaderPrint debugging toggle. \n " ) ,
ECVF_Cheat | ECVF_RenderThreadSafe ) ;
static TAutoConsoleVariable < int32 > CVarFontSize (
2022-02-22 15:35:01 -05:00
TEXT ( " r.ShaderPrint.FontSize " ) ,
2021-09-24 12:06:31 -04:00
8 ,
2019-02-25 09:08:54 -05:00
TEXT ( " ShaderPrint font size. \n " ) ,
ECVF_Cheat | ECVF_RenderThreadSafe ) ;
static TAutoConsoleVariable < int32 > CVarFontSpacingX (
2022-02-22 15:35:01 -05:00
TEXT ( " r.ShaderPrint.FontSpacingX " ) ,
2019-02-25 09:08:54 -05:00
0 ,
TEXT ( " ShaderPrint horizontal spacing between symbols. \n " ) ,
ECVF_Cheat | ECVF_RenderThreadSafe ) ;
static TAutoConsoleVariable < int32 > CVarFontSpacingY (
2022-02-22 15:35:01 -05:00
TEXT ( " r.ShaderPrint.FontSpacingY " ) ,
2019-02-25 09:08:54 -05:00
8 ,
TEXT ( " ShaderPrint vertical spacing between symbols. \n " ) ,
ECVF_Cheat | ECVF_RenderThreadSafe ) ;
2022-02-22 15:35:01 -05:00
static TAutoConsoleVariable < int32 > CVarMaxCharacterCount (
TEXT ( " r.ShaderPrint.MaxCharacters " ) ,
2019-02-25 09:08:54 -05:00
2000 ,
TEXT ( " ShaderPrint output buffer size. \n " ) ,
ECVF_Cheat | ECVF_RenderThreadSafe ) ;
2022-02-20 07:20:20 -05:00
static TAutoConsoleVariable < int32 > CVarMaxWidgetCount (
2022-02-22 15:35:01 -05:00
TEXT ( " r.ShaderPrint.MaxWidget " ) ,
2022-02-20 07:20:20 -05:00
32 ,
TEXT ( " ShaderPrint max widget count. \n " ) ,
ECVF_Cheat | ECVF_RenderThreadSafe ) ;
2022-02-22 15:35:01 -05:00
static TAutoConsoleVariable < int32 > CVarMaxLineCount (
TEXT ( " r.ShaderPrint.MaxLine " ) ,
32 ,
TEXT ( " ShaderPrint max line count. \n " ) ,
ECVF_Cheat | ECVF_RenderThreadSafe ) ;
static TAutoConsoleVariable < int32 > CVarDrawLock (
TEXT ( " r.ShaderPrint.Lock " ) ,
0 ,
TEXT ( " Lock the line drawing. \n " ) ,
ECVF_Cheat | ECVF_RenderThreadSafe ) ;
//////////////////////////////////////////////////////////////////////////////////////////////////
// Global states
2022-02-20 07:20:20 -05:00
static uint32 GWidgetRequestCount = 0 ;
2021-09-24 12:06:31 -04:00
static uint32 GCharacterRequestCount = 0 ;
2022-02-22 15:35:01 -05:00
static uint32 GLineRequestCount = 0 ;
2022-01-29 04:04:32 -05:00
static bool GShaderPrintEnableOverride = false ;
2022-02-22 15:35:01 -05:00
static FViewInfo * GDefaultView = nullptr ;
2019-02-25 09:08:54 -05:00
2022-02-22 15:35:01 -05:00
//////////////////////////////////////////////////////////////////////////////////////////////////
// Struct & Functions
2019-02-25 09:08:54 -05:00
// Structure used by shader buffers to store values and symbols
struct ShaderPrintItem
{
FVector2D ScreenPos ;
int32 Value ;
int32 Type ;
} ;
2021-11-18 14:37:34 -05:00
// Empty buffer for binding when ShaderPrint is disabled
class FEmptyBuffer : public FBufferWithRDG
{
public :
void InitRHI ( ) override
{
if ( ! Buffer . IsValid ( ) )
{
FRHICommandList * UnusedCmdList = new FRHICommandList ( FRHIGPUMask : : All ( ) ) ;
2022-02-20 07:20:20 -05:00
GetPooledFreeBuffer ( * UnusedCmdList , FRDGBufferDesc : : CreateStructuredDesc ( sizeof ( ShaderPrintItem ) , 1 ) , Buffer , TEXT ( " ShaderPrint.EmptyValueBuffer " ) ) ;
2021-11-18 14:37:34 -05:00
delete UnusedCmdList ;
UnusedCmdList = nullptr ;
}
}
} ;
FBufferWithRDG * GEmptyBuffer = new TGlobalResource < FEmptyBuffer > ( ) ;
2019-02-25 09:08:54 -05:00
// Get value buffer size
// Note that if the ShaderPrint system is disabled we still want to bind a minimal buffer
2022-02-22 15:35:01 -05:00
static uint32 GetMaxValueCount ( )
2019-02-25 09:08:54 -05:00
{
2022-02-22 15:35:01 -05:00
uint32 MaxValueCount = FMath : : Max ( CVarMaxCharacterCount . GetValueOnRenderThread ( ) + int32 ( GCharacterRequestCount ) , 0 ) ;
2019-02-25 09:08:54 -05:00
return IsEnabled ( ) ? MaxValueCount : 0 ;
}
2022-02-22 15:35:01 -05:00
static uint32 GetMaxWidgetCount ( )
2022-02-20 07:20:20 -05:00
{
2022-02-22 15:35:01 -05:00
uint32 MaxValueCount = FMath : : Max ( CVarMaxWidgetCount . GetValueOnRenderThread ( ) + int32 ( GWidgetRequestCount ) , 0 ) ;
2022-02-20 07:20:20 -05:00
return IsEnabled ( ) ? MaxValueCount : 0 ;
}
2022-02-22 15:35:01 -05:00
static uint32 GetMaxLineCount ( )
{
uint32 MaxValueCount = FMath : : Max ( CVarMaxLineCount . GetValueOnRenderThread ( ) + int32 ( GLineRequestCount ) , 0 ) ;
return IsEnabled ( ) ? MaxValueCount : 0 ;
}
2019-02-25 09:08:54 -05:00
// Get symbol buffer size
// This is some multiple of the value buffer size to allow for maximum value->symbol expansion
2022-02-22 15:35:01 -05:00
static uint32 GetMaxSymbolCount ( )
2019-02-25 09:08:54 -05:00
{
2022-02-22 15:35:01 -05:00
return GetMaxValueCount ( ) * 12u ;
2019-02-25 09:08:54 -05:00
}
2022-02-22 15:35:01 -05:00
static bool IsDrawLocked ( )
{
return CVarDrawLock . GetValueOnRenderThread ( ) > 0 ;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Uniform buffer
2019-02-25 09:08:54 -05:00
// ShaderPrint uniform buffer
2022-02-22 07:23:17 -05:00
IMPLEMENT_GLOBAL_SHADER_PARAMETER_STRUCT ( FShaderPrintCommonParameters , " ShaderPrint " ) ;
2019-02-25 09:08:54 -05:00
// Fill the uniform buffer parameters
// Return a uniform buffer with values filled and with single frame lifetime
2022-02-22 07:23:17 -05:00
static TUniformBufferRef < ShaderPrint : : FShaderPrintCommonParameters > CreateUniformBuffer ( const FShaderPrintData & Data )
2021-09-24 03:34:49 -04:00
{
2022-02-22 07:23:17 -05:00
FShaderPrintCommonParameters Out ;
2021-09-24 03:34:49 -04:00
Out . FontSize = Data . FontSize ;
2022-02-20 07:20:20 -05:00
Out . FontSpacing = Data . FontSpacing ;
2022-01-18 06:20:07 -05:00
Out . Resolution = Data . OutputRect . Size ( ) ;
2022-02-22 15:35:01 -05:00
Out . CursorCoord = Data . CursorCoord ;
2021-09-24 03:34:49 -04:00
Out . MaxValueCount = Data . MaxValueCount ;
Out . MaxSymbolCount = Data . MaxSymbolCount ;
2022-02-20 07:20:20 -05:00
Out . MaxStateCount = Data . MaxStateCount ;
2022-02-22 15:35:01 -05:00
Out . MaxLineCount = Data . MaxLineCount ;
2022-03-04 07:43:02 -05:00
Out . TranslatedWorldOffset = FVector3f ( Data . TranslatedWorldOffset ) ;
2022-02-22 07:23:17 -05:00
return TUniformBufferRef < ShaderPrint : : FShaderPrintCommonParameters > : : CreateUniformBufferImmediate ( Out , UniformBuffer_SingleFrame ) ;
2019-02-25 09:08:54 -05:00
}
2022-02-22 15:35:01 -05:00
//////////////////////////////////////////////////////////////////////////////////////////////////
// Accessors
void SetParameters ( FRDGBuilder & GraphBuilder , FShaderParameters & OutParameters )
{
if ( GDefaultView ! = nullptr )
{
SetParameters ( GraphBuilder , GDefaultView - > ShaderPrintData , OutParameters ) ;
}
}
2019-02-25 09:08:54 -05:00
// Fill the FShaderParameters parameters
2021-09-24 03:34:49 -04:00
void SetParameters ( FRDGBuilder & GraphBuilder , const FViewInfo & View , FShaderParameters & OutParameters )
2019-02-25 09:08:54 -05:00
{
2022-02-22 07:23:17 -05:00
SetParameters ( GraphBuilder , View . ShaderPrintData , OutParameters ) ;
2021-09-24 03:34:49 -04:00
}
// Fill the FShaderParameters parameters
void SetParameters ( FRDGBuilder & GraphBuilder , const FShaderPrintData & Data , FShaderParameters & OutParameters )
{
2022-02-22 07:23:17 -05:00
OutParameters . Common = Data . UniformBuffer ;
OutParameters . ShaderPrint_StateBuffer = GraphBuilder . CreateSRV ( Data . ShaderPrintStateBuffer ) ;
OutParameters . ShaderPrint_RWValuesBuffer = GraphBuilder . CreateUAV ( Data . ShaderPrintValueBuffer ) ;
2022-02-22 15:35:01 -05:00
OutParameters . ShaderPrint_RWLinesBuffer = GraphBuilder . CreateUAV ( Data . ShaderPrintLineBuffer ) ;
2019-02-25 09:08:54 -05:00
}
2019-04-11 09:29:25 -04:00
// Supported platforms
bool IsSupported ( EShaderPlatform InShaderPlatform )
{
return RHISupportsComputeShaders ( InShaderPlatform ) & & ! IsHlslccShaderPlatform ( InShaderPlatform ) ;
}
2020-09-24 00:43:27 -04:00
void SetEnabled ( bool bInEnabled )
{
2022-01-29 04:04:32 -05:00
if ( IsInGameThread ( ) )
{
CVarEnable - > Set ( bInEnabled ) ;
}
else
{
GShaderPrintEnableOverride = bInEnabled ;
}
2020-09-24 00:43:27 -04:00
}
void SetFontSize ( int32 InFontSize )
{
CVarFontSize - > Set ( FMath : : Clamp ( InFontSize , 6 , 128 ) ) ;
}
2022-02-22 15:35:01 -05:00
void RequestSpaceForCharacters ( uint32 InCount )
2020-09-24 00:43:27 -04:00
{
2022-02-22 15:35:01 -05:00
GCharacterRequestCount + = InCount ;
}
2020-09-24 00:43:27 -04:00
2022-02-22 15:35:01 -05:00
void RequestSpaceForLines ( uint32 InCount )
2021-09-24 12:06:31 -04:00
{
2022-02-22 15:35:01 -05:00
GLineRequestCount + = InCount ;
2021-09-24 12:06:31 -04:00
}
2019-04-11 09:29:25 -04:00
bool IsEnabled ( )
{
2022-01-29 04:04:32 -05:00
return CVarEnable . GetValueOnAnyThread ( ) ! = 0 | | GShaderPrintEnableOverride ;
2019-04-11 09:29:25 -04:00
}
2021-06-18 09:22:16 -04:00
bool IsEnabled ( const FViewInfo & View )
{
return IsEnabled ( ) & & IsSupported ( View . GetShaderPlatform ( ) ) ;
}
2022-02-22 15:35:01 -05:00
// Returns true if the default view exists and has shader debug rendering enabled (this needs to be checked before using a permutation that requires the shader draw parameters)
bool IsDefaultViewEnabled ( )
{
return GDefaultView ! = nullptr & & IsEnabled ( * GDefaultView ) ;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Widget/Characters Shaders
2019-02-25 09:08:54 -05:00
// Shader to initialize the output value buffer
class FShaderInitValueBufferCS : public FGlobalShader
{
public :
2019-10-01 13:03:04 -04:00
DECLARE_GLOBAL_SHADER ( FShaderInitValueBufferCS ) ;
2019-02-25 09:08:54 -05:00
SHADER_USE_PARAMETER_STRUCT ( FShaderInitValueBufferCS , FGlobalShader ) ;
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
2020-10-27 13:40:36 -04:00
SHADER_PARAMETER_RDG_BUFFER_UAV ( RWStructuredBuffer < ShaderPrintItem > , RWValuesBuffer )
2019-02-25 09:08:54 -05:00
END_SHADER_PARAMETER_STRUCT ( )
static bool ShouldCompilePermutation ( FGlobalShaderPermutationParameters const & Parameters )
{
2019-04-11 09:29:25 -04:00
return IsSupported ( Parameters . Platform ) ;
2019-02-25 09:08:54 -05:00
}
} ;
2019-10-01 13:03:04 -04:00
IMPLEMENT_GLOBAL_SHADER ( FShaderInitValueBufferCS , " /Engine/Private/ShaderPrintDraw.usf " , " InitValueBufferCS " , SF_Compute ) ;
2019-02-25 09:08:54 -05:00
// Shader to fill the indirect parameter arguments ready for the value->symbol compute pass
class FShaderBuildIndirectDispatchArgsCS : public FGlobalShader
{
public :
2019-10-01 13:03:04 -04:00
DECLARE_GLOBAL_SHADER ( FShaderBuildIndirectDispatchArgsCS ) ;
2019-02-25 09:08:54 -05:00
SHADER_USE_PARAMETER_STRUCT ( FShaderBuildIndirectDispatchArgsCS , FGlobalShader ) ;
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
2022-02-22 07:23:17 -05:00
SHADER_PARAMETER_STRUCT_REF ( FShaderPrintCommonParameters , Common )
2020-10-27 13:40:36 -04:00
SHADER_PARAMETER_RDG_BUFFER_SRV ( StructuredBuffer < ShaderPrintItem > , ValuesBuffer )
2019-02-25 09:08:54 -05:00
SHADER_PARAMETER_RDG_BUFFER_UAV ( RWStructuredBuffer < ShaderPrintItem > , RWSymbolsBuffer )
SHADER_PARAMETER_RDG_BUFFER_UAV ( RWStructuredBuffer < uint > , RWIndirectDispatchArgsBuffer )
END_SHADER_PARAMETER_STRUCT ( )
static bool ShouldCompilePermutation ( FGlobalShaderPermutationParameters const & Parameters )
{
2019-04-11 09:29:25 -04:00
return IsSupported ( Parameters . Platform ) ;
2019-02-25 09:08:54 -05:00
}
} ;
2019-10-01 13:03:04 -04:00
IMPLEMENT_GLOBAL_SHADER ( FShaderBuildIndirectDispatchArgsCS , " /Engine/Private/ShaderPrintDraw.usf " , " BuildIndirectDispatchArgsCS " , SF_Compute ) ;
2019-02-25 09:08:54 -05:00
2022-02-20 07:20:20 -05:00
// Shader to clean & compact widget state
class FShaderCompactStateBufferCS : public FGlobalShader
{
public :
DECLARE_GLOBAL_SHADER ( FShaderCompactStateBufferCS ) ;
SHADER_USE_PARAMETER_STRUCT ( FShaderCompactStateBufferCS , FGlobalShader ) ;
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
SHADER_PARAMETER ( uint32 , FrameIndex )
SHADER_PARAMETER ( uint32 , FrameThreshold )
2022-02-22 07:23:17 -05:00
SHADER_PARAMETER_STRUCT_REF ( FShaderPrintCommonParameters , Common )
2022-02-20 07:20:20 -05:00
SHADER_PARAMETER_RDG_BUFFER_UAV ( RWStructuredBuffer < uint > , RWStateBuffer )
END_SHADER_PARAMETER_STRUCT ( )
static bool ShouldCompilePermutation ( FGlobalShaderPermutationParameters const & Parameters )
{
return IsSupported ( Parameters . Platform ) ;
}
} ;
IMPLEMENT_GLOBAL_SHADER ( FShaderCompactStateBufferCS , " /Engine/Private/ShaderPrintDraw.usf " , " CompactStateBufferCS " , SF_Compute ) ;
2019-02-25 09:08:54 -05:00
// Shader to read the values buffer and convert to the symbols buffer
class FShaderBuildSymbolBufferCS : public FGlobalShader
{
public :
2019-10-01 13:03:04 -04:00
DECLARE_GLOBAL_SHADER ( FShaderBuildSymbolBufferCS ) ;
2019-02-25 09:08:54 -05:00
SHADER_USE_PARAMETER_STRUCT ( FShaderBuildSymbolBufferCS , FGlobalShader ) ;
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
2022-02-20 07:20:20 -05:00
SHADER_PARAMETER ( uint32 , FrameIndex )
2022-02-22 07:23:17 -05:00
SHADER_PARAMETER_STRUCT_REF ( FShaderPrintCommonParameters , Common )
2020-10-27 13:40:36 -04:00
SHADER_PARAMETER_RDG_BUFFER_SRV ( StructuredBuffer < ShaderPrintItem > , ValuesBuffer )
2019-02-25 09:08:54 -05:00
SHADER_PARAMETER_RDG_BUFFER_UAV ( RWStructuredBuffer < ShaderPrintItem > , RWSymbolsBuffer )
2022-02-20 07:20:20 -05:00
SHADER_PARAMETER_RDG_BUFFER_UAV ( RWStructuredBuffer < uint > , RWStateBuffer )
2021-03-17 06:03:08 -04:00
RDG_BUFFER_ACCESS ( IndirectDispatchArgsBuffer , ERHIAccess : : IndirectArgs )
2019-02-25 09:08:54 -05:00
END_SHADER_PARAMETER_STRUCT ( )
static bool ShouldCompilePermutation ( FGlobalShaderPermutationParameters const & Parameters )
{
2019-04-11 09:29:25 -04:00
return IsSupported ( Parameters . Platform ) ;
2019-02-25 09:08:54 -05:00
}
} ;
2019-10-01 13:03:04 -04:00
IMPLEMENT_GLOBAL_SHADER ( FShaderBuildSymbolBufferCS , " /Engine/Private/ShaderPrintDraw.usf " , " BuildSymbolBufferCS " , SF_Compute ) ;
2019-02-25 09:08:54 -05:00
// Shader to fill the indirect parameter arguments ready for draw pass
class FShaderBuildIndirectDrawArgsCS : public FGlobalShader
{
public :
2019-10-01 13:03:04 -04:00
DECLARE_GLOBAL_SHADER ( FShaderBuildIndirectDrawArgsCS ) ;
2019-02-25 09:08:54 -05:00
SHADER_USE_PARAMETER_STRUCT ( FShaderBuildIndirectDrawArgsCS , FGlobalShader ) ;
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
2022-02-22 07:23:17 -05:00
SHADER_PARAMETER_STRUCT_REF ( FShaderPrintCommonParameters , Common )
2019-02-25 09:08:54 -05:00
SHADER_PARAMETER_RDG_BUFFER_SRV ( StructuredBuffer < ShaderPrintItem > , SymbolsBuffer )
SHADER_PARAMETER_RDG_BUFFER_UAV ( RWStructuredBuffer < uint > , RWIndirectDrawArgsBuffer )
END_SHADER_PARAMETER_STRUCT ( )
static bool ShouldCompilePermutation ( FGlobalShaderPermutationParameters const & Parameters )
{
2019-04-11 09:29:25 -04:00
return IsSupported ( Parameters . Platform ) ;
2019-02-25 09:08:54 -05:00
}
} ;
2019-10-01 13:03:04 -04:00
IMPLEMENT_GLOBAL_SHADER ( FShaderBuildIndirectDrawArgsCS , " /Engine/Private/ShaderPrintDraw.usf " , " BuildIndirectDrawArgsCS " , SF_Compute ) ;
2019-02-25 09:08:54 -05:00
// Shader for draw pass to render each symbol
class FShaderDrawSymbols : public FGlobalShader
{
public :
2021-12-07 07:48:20 -05:00
FShaderDrawSymbols ( )
{ }
FShaderDrawSymbols ( const ShaderMetaType : : CompiledShaderInitializerType & Initializer )
: FGlobalShader ( Initializer )
{ }
2019-02-25 09:08:54 -05:00
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
RENDER_TARGET_BINDING_SLOTS ( )
2022-02-22 07:23:17 -05:00
SHADER_PARAMETER_STRUCT_REF ( FShaderPrintCommonParameters , Common )
2019-02-25 09:08:54 -05:00
SHADER_PARAMETER_TEXTURE ( Texture2D , MiniFontTexture )
SHADER_PARAMETER_RDG_BUFFER_SRV ( StructuredBuffer < ShaderPrintItem > , SymbolsBuffer )
2021-03-17 06:03:08 -04:00
RDG_BUFFER_ACCESS ( IndirectDrawArgsBuffer , ERHIAccess : : IndirectArgs )
2019-02-25 09:08:54 -05:00
END_SHADER_PARAMETER_STRUCT ( )
static bool ShouldCompilePermutation ( FGlobalShaderPermutationParameters const & Parameters )
{
2019-04-11 09:29:25 -04:00
return IsSupported ( Parameters . Platform ) ;
2019-02-25 09:08:54 -05:00
}
} ;
class FShaderDrawSymbolsVS : public FShaderDrawSymbols
{
2019-10-01 13:03:04 -04:00
DECLARE_GLOBAL_SHADER ( FShaderDrawSymbolsVS ) ;
2021-12-07 07:48:20 -05:00
SHADER_USE_PARAMETER_STRUCT ( FShaderDrawSymbolsVS , FShaderDrawSymbols ) ;
2019-02-25 09:08:54 -05:00
} ;
2019-10-01 13:03:04 -04:00
IMPLEMENT_GLOBAL_SHADER ( FShaderDrawSymbolsVS , " /Engine/Private/ShaderPrintDraw.usf " , " DrawSymbolsVS " , SF_Vertex ) ;
2019-02-25 09:08:54 -05:00
class FShaderDrawSymbolsPS : public FShaderDrawSymbols
{
2019-10-01 13:03:04 -04:00
DECLARE_GLOBAL_SHADER ( FShaderDrawSymbolsPS ) ;
2021-12-07 07:48:20 -05:00
SHADER_USE_PARAMETER_STRUCT ( FShaderDrawSymbolsPS , FShaderDrawSymbols ) ;
2019-02-25 09:08:54 -05:00
} ;
2019-10-01 13:03:04 -04:00
IMPLEMENT_GLOBAL_SHADER ( FShaderDrawSymbolsPS , " /Engine/Private/ShaderPrintDraw.usf " , " DrawSymbolsPS " , SF_Pixel ) ;
2019-02-25 09:08:54 -05:00
2022-02-22 15:35:01 -05:00
//////////////////////////////////////////////////////////////////////////
// Line Shaders
class FShaderDrawDebugClearCS : public FGlobalShader
{
DECLARE_GLOBAL_SHADER ( FShaderDrawDebugClearCS ) ;
SHADER_USE_PARAMETER_STRUCT ( FShaderDrawDebugClearCS , FGlobalShader ) ;
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
SHADER_PARAMETER_RDG_BUFFER_UAV ( RWStructuredBuffer , RWElementBuffer )
END_SHADER_PARAMETER_STRUCT ( )
static bool ShouldCompilePermutation ( const FGlobalShaderPermutationParameters & Parameters )
{
return IsSupported ( Parameters . Platform ) ;
}
static void ModifyCompilationEnvironment ( const FGlobalShaderPermutationParameters & Parameters , FShaderCompilerEnvironment & OutEnvironment )
{
FGlobalShader : : ModifyCompilationEnvironment ( Parameters , OutEnvironment ) ;
OutEnvironment . SetDefine ( TEXT ( " GPU_DEBUG_RENDERING " ) , 1 ) ;
OutEnvironment . SetDefine ( TEXT ( " GPU_DEBUG_RENDERING_CLEAR_CS " ) , 1 ) ;
}
} ;
2022-02-22 15:49:31 -05:00
IMPLEMENT_GLOBAL_SHADER ( FShaderDrawDebugClearCS , " /Engine/Private/ShaderPrintDrawPrimitive.usf " , " ShaderDrawDebugClearCS " , SF_Compute ) ;
2022-02-22 15:35:01 -05:00
class FShaderDrawDebugCopyCS : public FGlobalShader
{
DECLARE_GLOBAL_SHADER ( FShaderDrawDebugCopyCS ) ;
SHADER_USE_PARAMETER_STRUCT ( FShaderDrawDebugCopyCS , FGlobalShader ) ;
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
SHADER_PARAMETER_RDG_BUFFER_SRV ( StructuredBuffer , ElementBuffer )
SHADER_PARAMETER_RDG_BUFFER_UAV ( RWBuffer , RWIndirectArgs )
END_SHADER_PARAMETER_STRUCT ( )
static bool ShouldCompilePermutation ( const FGlobalShaderPermutationParameters & Parameters )
{
return IsSupported ( Parameters . Platform ) ;
}
static void ModifyCompilationEnvironment ( const FGlobalShaderPermutationParameters & Parameters , FShaderCompilerEnvironment & OutEnvironment )
{
FGlobalShader : : ModifyCompilationEnvironment ( Parameters , OutEnvironment ) ;
OutEnvironment . SetDefine ( TEXT ( " GPU_DEBUG_RENDERING " ) , 1 ) ;
OutEnvironment . SetDefine ( TEXT ( " GPU_DEBUG_RENDERING_COPY_CS " ) , 1 ) ;
}
} ;
2022-02-22 15:49:31 -05:00
IMPLEMENT_GLOBAL_SHADER ( FShaderDrawDebugCopyCS , " /Engine/Private/ShaderPrintDrawPrimitive.usf " , " ShaderDrawDebugCopyCS " , SF_Compute ) ;
2022-02-22 15:35:01 -05:00
class FShaderDrawDebugVS : public FGlobalShader
{
DECLARE_GLOBAL_SHADER ( FShaderDrawDebugVS ) ;
SHADER_USE_PARAMETER_STRUCT ( FShaderDrawDebugVS , FGlobalShader ) ;
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
SHADER_PARAMETER ( FVector3f , TranslatedWorldOffsetConversion )
SHADER_PARAMETER_STRUCT_REF ( FViewUniformShaderParameters , View )
SHADER_PARAMETER_SRV ( StructuredBuffer , LockedShaderDrawDebugPrimitive )
SHADER_PARAMETER_RDG_BUFFER_SRV ( StructuredBuffer , ShaderDrawDebugPrimitive )
RDG_BUFFER_ACCESS ( IndirectBuffer , ERHIAccess : : IndirectArgs )
END_SHADER_PARAMETER_STRUCT ( )
static bool ShouldCompilePermutation ( const FGlobalShaderPermutationParameters & Parameters )
{
return IsSupported ( Parameters . Platform ) ;
}
static void ModifyCompilationEnvironment ( const FGlobalShaderPermutationParameters & Parameters , FShaderCompilerEnvironment & OutEnvironment )
{
FGlobalShader : : ModifyCompilationEnvironment ( Parameters , OutEnvironment ) ;
OutEnvironment . SetDefine ( TEXT ( " GPU_DEBUG_RENDERING " ) , 1 ) ;
OutEnvironment . SetDefine ( TEXT ( " GPU_DEBUG_RENDERING_VS " ) , 1 ) ;
OutEnvironment . SetDefine ( TEXT ( " GPU_DEBUG_RENDERING_PS " ) , 0 ) ;
}
} ;
2022-02-22 15:49:31 -05:00
IMPLEMENT_GLOBAL_SHADER ( FShaderDrawDebugVS , " /Engine/Private/ShaderPrintDrawPrimitive.usf " , " ShaderDrawDebugVS " , SF_Vertex ) ;
2022-02-22 15:35:01 -05:00
class FShaderDrawDebugPS : public FGlobalShader
{
DECLARE_GLOBAL_SHADER ( FShaderDrawDebugPS ) ;
SHADER_USE_PARAMETER_STRUCT ( FShaderDrawDebugPS , FGlobalShader ) ;
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
SHADER_PARAMETER ( FVector2f , OutputInvResolution )
SHADER_PARAMETER ( FVector2f , OriginalViewRectMin )
SHADER_PARAMETER ( FVector2f , OriginalViewSize )
SHADER_PARAMETER ( FVector2f , OriginalBufferInvSize )
SHADER_PARAMETER_RDG_TEXTURE ( Texture2D , DepthTexture )
SHADER_PARAMETER_SAMPLER ( SamplerState , DepthSampler )
RENDER_TARGET_BINDING_SLOTS ( )
END_SHADER_PARAMETER_STRUCT ( )
using FPermutationDomain = TShaderPermutationDomain < > ;
static bool ShouldCompilePermutation ( const FGlobalShaderPermutationParameters & Parameters )
{
return IsSupported ( Parameters . Platform ) ;
}
static void ModifyCompilationEnvironment ( const FGlobalShaderPermutationParameters & Parameters , FShaderCompilerEnvironment & OutEnvironment )
{
FGlobalShader : : ModifyCompilationEnvironment ( Parameters , OutEnvironment ) ;
OutEnvironment . SetDefine ( TEXT ( " GPU_DEBUG_RENDERING " ) , 1 ) ;
OutEnvironment . SetDefine ( TEXT ( " GPU_DEBUG_RENDERING_VS " ) , 0 ) ;
OutEnvironment . SetDefine ( TEXT ( " GPU_DEBUG_RENDERING_PS " ) , 1 ) ;
}
} ;
2022-02-22 15:49:31 -05:00
IMPLEMENT_GLOBAL_SHADER ( FShaderDrawDebugPS , " /Engine/Private/ShaderPrintDrawPrimitive.usf " , " ShaderDrawDebugPS " , SF_Pixel ) ;
2022-02-22 15:35:01 -05:00
BEGIN_SHADER_PARAMETER_STRUCT ( FShaderDrawVSPSParameters , )
SHADER_PARAMETER_STRUCT_INCLUDE ( FShaderDrawDebugVS : : FParameters , VS )
SHADER_PARAMETER_STRUCT_INCLUDE ( FShaderDrawDebugPS : : FParameters , PS )
END_SHADER_PARAMETER_STRUCT ( )
//////////////////////////////////////////////////////////////////////////////////////////////////
// Drawing/Rendering API
2020-10-27 13:40:36 -04:00
void BeginView ( FRDGBuilder & GraphBuilder , FViewInfo & View )
2019-02-25 09:08:54 -05:00
{
2021-01-19 06:29:15 -04:00
TRACE_CPUPROFILER_EVENT_SCOPE ( ShaderPrint : : BeginView ) ;
2021-09-24 03:34:49 -04:00
View . ShaderPrintData = FShaderPrintData ( ) ;
2021-06-18 09:22:16 -04:00
if ( ! IsSupported ( View . GetShaderPlatform ( ) ) )
2019-04-11 09:29:25 -04:00
{
return ;
}
2022-01-18 06:20:07 -05:00
const FIntPoint ViewSize ( FMath : : Max ( View . UnconstrainedViewRect . Size ( ) . X , 1 ) , FMath : : Max ( View . UnconstrainedViewRect . Size ( ) . Y , 1 ) ) ;
2021-09-24 03:34:49 -04:00
2022-01-18 06:20:07 -05:00
float FontSize = float ( FMath : : Max ( CVarFontSize . GetValueOnRenderThread ( ) , 1 ) ) * View . Family - > DebugDPIScale ;
float FontSpacingX = float ( FMath : : Max ( CVarFontSpacingX . GetValueOnRenderThread ( ) , 1 ) ) * View . Family - > DebugDPIScale ;
float FontSpacingY = float ( FMath : : Max ( CVarFontSpacingY . GetValueOnRenderThread ( ) , 1 ) ) * View . Family - > DebugDPIScale ;
const float FontWidth = FontSize / float ( ViewSize . X ) ;
const float FontHeight = FontSize / float ( ViewSize . Y ) ;
const float SpaceWidth = FontSpacingX / float ( ViewSize . X ) ;
const float SpaceHeight = FontSpacingY / float ( ViewSize . Y ) ;
2022-02-20 07:20:20 -05:00
View . ShaderPrintData . FontSize = FVector2f ( FontWidth , FontHeight ) ;
View . ShaderPrintData . FontSpacing = FVector2f ( SpaceWidth + FontWidth , SpaceHeight + FontHeight ) ;
2022-01-18 06:20:07 -05:00
View . ShaderPrintData . OutputRect = View . UnconstrainedViewRect ;
2021-09-24 03:34:49 -04:00
View . ShaderPrintData . MaxValueCount = GetMaxValueCount ( ) ;
View . ShaderPrintData . MaxSymbolCount = GetMaxSymbolCount ( ) ;
2022-02-20 07:20:20 -05:00
View . ShaderPrintData . MaxStateCount = GetMaxWidgetCount ( ) ;
2022-02-22 15:35:01 -05:00
View . ShaderPrintData . MaxLineCount = GetMaxLineCount ( ) ;
2022-02-20 07:20:20 -05:00
View . ShaderPrintData . CursorCoord = View . CursorPos ;
2022-02-22 15:35:01 -05:00
View . ShaderPrintData . TranslatedWorldOffset = View . ViewMatrices . GetPreViewTranslation ( ) ;
2021-09-24 12:06:31 -04:00
GCharacterRequestCount = 0 ;
2022-02-22 15:35:01 -05:00
GWidgetRequestCount = 0 ;
GLineRequestCount = 0 ;
2021-09-24 03:34:49 -04:00
2019-02-25 09:08:54 -05:00
// Early out if system is disabled
2021-11-18 14:37:34 -05:00
// Note that we still bind a dummy ShaderPrintValueBuffer
2019-02-25 09:08:54 -05:00
// This is in case some debug shader code is still active (we don't want an unbound buffer!)
if ( ! IsEnabled ( ) )
{
2022-02-22 07:23:17 -05:00
View . ShaderPrintData . UniformBuffer = CreateUniformBuffer ( View . ShaderPrintData ) ;
2021-11-18 14:37:34 -05:00
View . ShaderPrintData . ShaderPrintValueBuffer = GraphBuilder . RegisterExternalBuffer ( GEmptyBuffer - > Buffer ) ;
2022-02-20 07:20:20 -05:00
View . ShaderPrintData . ShaderPrintStateBuffer = GraphBuilder . RegisterExternalBuffer ( GEmptyBuffer - > Buffer ) ;
2022-02-22 15:35:01 -05:00
View . ShaderPrintData . ShaderPrintLineBuffer = GraphBuilder . CreateBuffer ( FRDGBufferDesc : : CreateStructuredDesc ( 4 , 8 ) , TEXT ( " ShaderPrint.LineBuffer(Dummy) " ) , ERDGBufferFlags : : None ) ;
2019-02-25 09:08:54 -05:00
return ;
}
2022-02-22 15:35:01 -05:00
// Invalid to call begin twice for the same view.
check ( GDefaultView ! = & View ) ;
if ( GDefaultView = = nullptr )
2022-02-20 07:20:20 -05:00
{
2022-02-22 15:35:01 -05:00
GDefaultView = & View ;
}
// Primitives/Lines
// ----------------
{
FSceneViewState * ViewState = View . ViewState ;
const bool bLockBufferThisFrame = IsDrawLocked ( ) & & ViewState & & ! ViewState - > ShaderPrintStateData . bIsLocked ;
ERDGBufferFlags Flags = bLockBufferThisFrame ? ERDGBufferFlags : : MultiFrame : ERDGBufferFlags : : None ;
View . ShaderPrintData . ShaderPrintLineBuffer = GraphBuilder . CreateBuffer ( FRDGBufferDesc : : CreateStructuredDesc ( 4 , 8 * View . ShaderPrintData . MaxLineCount ) , TEXT ( " ShaderPrint.LineBuffer " ) , Flags ) ;
// Clear buffer counter
2022-02-20 07:20:20 -05:00
{
2022-02-22 15:35:01 -05:00
FShaderDrawDebugClearCS : : FParameters * Parameters = GraphBuilder . AllocParameters < FShaderDrawDebugClearCS : : FParameters > ( ) ;
Parameters - > RWElementBuffer = GraphBuilder . CreateUAV ( View . ShaderPrintData . ShaderPrintLineBuffer ) ;
TShaderMapRef < FShaderDrawDebugClearCS > ComputeShader ( View . ShaderMap ) ;
ClearUnusedGraphResources ( ComputeShader , Parameters ) ;
GraphBuilder . AddPass (
RDG_EVENT_NAME ( " ShaderPrint::BeginView(Clear lines) " ) ,
Parameters ,
ERDGPassFlags : : Compute ,
[ Parameters , ComputeShader ] ( FRHICommandList & RHICmdList )
{
FComputeShaderUtils : : Dispatch ( RHICmdList , ComputeShader , * Parameters , FIntVector ( 1 , 1 , 1 ) ) ;
} ) ;
}
if ( IsDrawLocked ( ) & & ViewState & & ! ViewState - > ShaderPrintStateData . bIsLocked )
{
ViewState - > ShaderPrintStateData . LineBuffer = GraphBuilder . ConvertToExternalBuffer ( View . ShaderPrintData . ShaderPrintLineBuffer ) ;
ViewState - > ShaderPrintStateData . PreViewTranslation = View . ViewMatrices . GetPreViewTranslation ( ) ;
ViewState - > ShaderPrintStateData . bIsLocked = true ;
}
if ( ! IsDrawLocked ( ) & & ViewState & & ViewState - > ShaderPrintStateData . bIsLocked )
{
ViewState - > ShaderPrintStateData . LineBuffer = nullptr ;
ViewState - > ShaderPrintStateData . PreViewTranslation = FVector : : ZeroVector ;
ViewState - > ShaderPrintStateData . bIsLocked = false ;
}
}
// Characters & Widgets
// --------------------
{
// Initialize output buffer and store in the view info
// Values buffer contains Count + 1 elements. The first element is only used as a counter.
View . ShaderPrintData . ShaderPrintValueBuffer = GraphBuilder . CreateBuffer ( FRDGBufferDesc : : CreateStructuredDesc ( sizeof ( ShaderPrintItem ) , View . ShaderPrintData . MaxValueCount + 1 ) , TEXT ( " ShaderPrint.ValueBuffer " ) ) ;
// State buffer is retrieved from the view state, or created if it does not exist
View . ShaderPrintData . ShaderPrintStateBuffer = nullptr ;
if ( FSceneViewState * ViewState = View . ViewState )
{
if ( ViewState - > ShaderPrintStateData . StateBuffer )
{
View . ShaderPrintData . ShaderPrintStateBuffer = GraphBuilder . RegisterExternalBuffer ( ViewState - > ShaderPrintStateData . StateBuffer ) ;
}
else
{
View . ShaderPrintData . ShaderPrintStateBuffer = GraphBuilder . CreateBuffer ( FRDGBufferDesc : : CreateStructuredDesc ( sizeof ( uint32 ) , ( 3 * View . ShaderPrintData . MaxStateCount ) + 1 ) , TEXT ( " ShaderPrint.StateBuffer " ) ) ;
AddClearUAVPass ( GraphBuilder , GraphBuilder . CreateUAV ( View . ShaderPrintData . ShaderPrintStateBuffer , PF_R32_UINT ) , 0u ) ;
ViewState - > ShaderPrintStateData . StateBuffer = GraphBuilder . ConvertToExternalBuffer ( View . ShaderPrintData . ShaderPrintStateBuffer ) ;
}
2022-02-20 07:20:20 -05:00
}
else
{
2022-02-22 15:35:01 -05:00
View . ShaderPrintData . MaxStateCount = 0 ;
View . ShaderPrintData . ShaderPrintStateBuffer = GraphBuilder . RegisterExternalBuffer ( GEmptyBuffer - > Buffer ) ;
2022-02-20 07:20:20 -05:00
}
2022-02-22 15:35:01 -05:00
// Clear the output buffer internal counter ready for use
const ERHIFeatureLevel : : Type FeatureLevel = View . GetFeatureLevel ( ) ;
FGlobalShaderMap * GlobalShaderMap = GetGlobalShaderMap ( FeatureLevel ) ;
TShaderMapRef < FShaderInitValueBufferCS > ComputeShader ( GlobalShaderMap ) ;
auto * PassParameters = GraphBuilder . AllocParameters < FShaderInitValueBufferCS : : FParameters > ( ) ;
PassParameters - > RWValuesBuffer = GraphBuilder . CreateUAV ( View . ShaderPrintData . ShaderPrintValueBuffer ) ;
FComputeShaderUtils : : AddPass (
GraphBuilder ,
RDG_EVENT_NAME ( " ShaderPrint::BeginView(Clear characters) " ) ,
ComputeShader ,
PassParameters ,
FIntVector ( 1 , 1 , 1 ) ) ;
2022-02-20 07:20:20 -05:00
}
2022-02-22 15:35:01 -05:00
// Create common parameters uniform buffer
2022-02-22 07:23:17 -05:00
View . ShaderPrintData . UniformBuffer = CreateUniformBuffer ( View . ShaderPrintData ) ;
2019-02-25 09:08:54 -05:00
}
2022-02-22 15:35:01 -05:00
static void InternalDrawView_Characters ( FRDGBuilder & GraphBuilder , const FViewInfo & View , FScreenPassTexture OutputTexture )
2019-02-25 09:08:54 -05:00
{
2022-01-31 15:55:18 -05:00
const FIntRect Viewport = OutputTexture . ViewRect ;
2022-01-18 06:20:07 -05:00
2019-02-25 09:08:54 -05:00
// Initialize graph managed resources
// Symbols buffer contains Count + 1 elements. The first element is only used as a counter.
2022-02-20 07:20:20 -05:00
FRDGBufferRef SymbolBuffer = GraphBuilder . CreateBuffer ( FRDGBufferDesc : : CreateStructuredDesc ( sizeof ( ShaderPrintItem ) , GetMaxSymbolCount ( ) + 1 ) , TEXT ( " ShaderPrint.SymbolBuffer " ) ) ;
FRDGBufferRef IndirectDispatchArgsBuffer = GraphBuilder . CreateBuffer ( FRDGBufferDesc : : CreateIndirectDesc ( 4 ) , TEXT ( " ShaderPrint.IndirectDispatchArgs " ) ) ;
FRDGBufferRef IndirectDrawArgsBuffer = GraphBuilder . CreateBuffer ( FRDGBufferDesc : : CreateIndirectDesc ( 5 ) , TEXT ( " ShaderPrint.IndirectDrawArgs " ) ) ;
2019-02-25 09:08:54 -05:00
// Non graph managed resources
2022-03-07 15:09:45 -05:00
FRDGBufferSRVRef ValueBuffer = GraphBuilder . CreateSRV ( View . ShaderPrintData . ShaderPrintValueBuffer ) ;
2022-02-20 07:20:20 -05:00
FRDGBufferSRVRef StateBuffer = GraphBuilder . CreateSRV ( View . ShaderPrintData . ShaderPrintStateBuffer ) ;
2021-06-06 16:05:31 -04:00
FTextureRHIRef FontTexture = GSystemTextures . AsciiTexture - > GetRenderTargetItem ( ) . ShaderResourceTexture ;
2019-02-25 09:08:54 -05:00
2019-04-17 13:02:59 -04:00
const ERHIFeatureLevel : : Type FeatureLevel = View . GetFeatureLevel ( ) ;
2020-02-06 13:13:41 -05:00
FGlobalShaderMap * GlobalShaderMap = GetGlobalShaderMap ( FeatureLevel ) ;
2019-02-25 09:08:54 -05:00
// BuildIndirectDispatchArgs
{
typedef FShaderBuildIndirectDispatchArgsCS SHADER ;
TShaderMapRef < SHADER > ComputeShader ( GlobalShaderMap ) ;
SHADER : : FParameters * PassParameters = GraphBuilder . AllocParameters < SHADER : : FParameters > ( ) ;
2022-02-22 07:23:17 -05:00
PassParameters - > Common = View . ShaderPrintData . UniformBuffer ;
2022-03-07 15:09:45 -05:00
PassParameters - > ValuesBuffer = ValueBuffer ;
2019-02-25 09:08:54 -05:00
PassParameters - > RWSymbolsBuffer = GraphBuilder . CreateUAV ( SymbolBuffer , EPixelFormat : : PF_R32_UINT ) ;
PassParameters - > RWIndirectDispatchArgsBuffer = GraphBuilder . CreateUAV ( IndirectDispatchArgsBuffer , EPixelFormat : : PF_R32_UINT ) ;
FComputeShaderUtils : : AddPass (
GraphBuilder ,
2022-01-31 15:55:18 -05:00
RDG_EVENT_NAME ( " ShaderPrint::BuildIndirectDispatchArgs " ) ,
2020-02-06 13:13:41 -05:00
ComputeShader , PassParameters ,
2019-02-25 09:08:54 -05:00
FIntVector ( 1 , 1 , 1 ) ) ;
}
// BuildSymbolBuffer
{
typedef FShaderBuildSymbolBufferCS SHADER ;
TShaderMapRef < SHADER > ComputeShader ( GlobalShaderMap ) ;
SHADER : : FParameters * PassParameters = GraphBuilder . AllocParameters < SHADER : : FParameters > ( ) ;
2022-02-20 07:20:20 -05:00
PassParameters - > FrameIndex = View . Family ? View . Family - > FrameNumber : 0u ;
2022-02-22 07:23:17 -05:00
PassParameters - > Common = View . ShaderPrintData . UniformBuffer ;
2022-03-07 15:09:45 -05:00
PassParameters - > ValuesBuffer = ValueBuffer ;
2019-02-25 09:08:54 -05:00
PassParameters - > RWSymbolsBuffer = GraphBuilder . CreateUAV ( SymbolBuffer , EPixelFormat : : PF_R32_UINT ) ;
2022-02-20 07:20:20 -05:00
PassParameters - > RWStateBuffer = GraphBuilder . CreateUAV ( View . ShaderPrintData . ShaderPrintStateBuffer , EPixelFormat : : PF_R32_UINT ) ;
2019-02-25 09:08:54 -05:00
PassParameters - > IndirectDispatchArgsBuffer = IndirectDispatchArgsBuffer ;
FComputeShaderUtils : : AddPass (
GraphBuilder ,
2022-01-31 15:55:18 -05:00
RDG_EVENT_NAME ( " ShaderPrint::BuildSymbolBuffer " ) ,
2020-02-06 13:13:41 -05:00
ComputeShader , PassParameters ,
2019-02-25 09:08:54 -05:00
IndirectDispatchArgsBuffer , 0 ) ;
}
2022-02-20 07:20:20 -05:00
// CompactStateBuffer
2022-03-07 15:09:45 -05:00
#if 0
2022-02-20 07:20:20 -05:00
{
typedef FShaderCompactStateBufferCS SHADER ;
TShaderMapRef < SHADER > ComputeShader ( GlobalShaderMap ) ;
SHADER : : FParameters * PassParameters = GraphBuilder . AllocParameters < SHADER : : FParameters > ( ) ;
PassParameters - > FrameIndex = View . Family ? View . Family - > FrameNumber : 0u ;
PassParameters - > FrameThreshold = 300u ;
2022-02-22 07:23:17 -05:00
PassParameters - > Common = View . ShaderPrintData . UniformBuffer ;
2022-02-20 07:20:20 -05:00
PassParameters - > RWStateBuffer = GraphBuilder . CreateUAV ( View . ShaderPrintData . ShaderPrintStateBuffer , EPixelFormat : : PF_R32_UINT ) ;
FComputeShaderUtils : : AddPass (
GraphBuilder ,
RDG_EVENT_NAME ( " ShaderPrint::CompactStateBuffer " ) ,
ComputeShader , PassParameters , FIntVector ( 1 , 1 , 1 ) ) ;
}
2022-03-07 15:09:45 -05:00
# endif
2022-02-20 07:20:20 -05:00
2019-02-25 09:08:54 -05:00
// BuildIndirectDrawArgs
{
typedef FShaderBuildIndirectDrawArgsCS SHADER ;
TShaderMapRef < SHADER > ComputeShader ( GlobalShaderMap ) ;
SHADER : : FParameters * PassParameters = GraphBuilder . AllocParameters < SHADER : : FParameters > ( ) ;
2022-02-22 07:23:17 -05:00
PassParameters - > Common = View . ShaderPrintData . UniformBuffer ;
2019-02-25 09:08:54 -05:00
PassParameters - > SymbolsBuffer = GraphBuilder . CreateSRV ( SymbolBuffer ) ;
PassParameters - > RWIndirectDrawArgsBuffer = GraphBuilder . CreateUAV ( IndirectDrawArgsBuffer , EPixelFormat : : PF_R32_UINT ) ;
FComputeShaderUtils : : AddPass (
GraphBuilder ,
2022-01-31 15:55:18 -05:00
RDG_EVENT_NAME ( " ShaderPrint::BuildIndirectDrawArgs " ) ,
2020-02-06 13:13:41 -05:00
ComputeShader , PassParameters ,
2019-02-25 09:08:54 -05:00
FIntVector ( 1 , 1 , 1 ) ) ;
}
// DrawSymbols
{
typedef FShaderDrawSymbols SHADER ;
TShaderMapRef < FShaderDrawSymbolsVS > VertexShader ( GlobalShaderMap ) ;
TShaderMapRef < FShaderDrawSymbolsPS > PixelShader ( GlobalShaderMap ) ;
SHADER : : FParameters * PassParameters = GraphBuilder . AllocParameters < SHADER : : FParameters > ( ) ;
2022-01-18 06:20:07 -05:00
PassParameters - > RenderTargets [ 0 ] = FRenderTargetBinding ( OutputTexture . Texture , ERenderTargetLoadAction : : ELoad ) ;
2022-02-22 07:23:17 -05:00
PassParameters - > Common = View . ShaderPrintData . UniformBuffer ;
2019-02-25 09:08:54 -05:00
PassParameters - > MiniFontTexture = FontTexture ;
PassParameters - > SymbolsBuffer = GraphBuilder . CreateSRV ( SymbolBuffer ) ;
PassParameters - > IndirectDrawArgsBuffer = IndirectDrawArgsBuffer ;
GraphBuilder . AddPass (
2022-01-31 15:55:18 -05:00
RDG_EVENT_NAME ( " ShaderPrint::DrawSymbols " ) ,
2019-02-25 09:08:54 -05:00
PassParameters ,
2019-07-16 18:13:25 -04:00
ERDGPassFlags : : Raster ,
2022-01-31 15:55:18 -05:00
[ VertexShader , PixelShader , PassParameters , Viewport ] ( FRHICommandList & RHICmdList )
2019-02-25 09:08:54 -05:00
{
2022-01-18 06:20:07 -05:00
2019-02-25 09:08:54 -05:00
FGraphicsPipelineStateInitializer GraphicsPSOInit ;
2022-01-18 06:20:07 -05:00
RHICmdList . ApplyCachedRenderTargets ( GraphicsPSOInit ) ;
2019-02-25 09:08:54 -05:00
GraphicsPSOInit . DepthStencilState = TStaticDepthStencilState < false , CF_Always > : : GetRHI ( ) ;
GraphicsPSOInit . BlendState = TStaticBlendState < CW_RGBA , BO_Add , BF_One , BF_InverseSourceAlpha , BO_Add , BF_Zero , BF_One > : : GetRHI ( ) ;
GraphicsPSOInit . RasterizerState = TStaticRasterizerState < > : : GetRHI ( ) ;
GraphicsPSOInit . PrimitiveType = PT_TriangleList ;
GraphicsPSOInit . BoundShaderState . VertexDeclarationRHI = GetVertexDeclarationFVector4 ( ) ;
2020-02-06 13:13:41 -05:00
GraphicsPSOInit . BoundShaderState . VertexShaderRHI = VertexShader . GetVertexShader ( ) ;
GraphicsPSOInit . BoundShaderState . PixelShaderRHI = PixelShader . GetPixelShader ( ) ;
2022-01-18 06:20:07 -05:00
SetGraphicsPipelineState ( RHICmdList , GraphicsPSOInit , 0 ) ;
2019-02-25 09:08:54 -05:00
2022-01-31 15:55:18 -05:00
RHICmdList . SetViewport ( Viewport . Min . X , Viewport . Min . Y , 0.0f , Viewport . Max . X , Viewport . Max . Y , 1.0f ) ;
2022-01-18 06:20:07 -05:00
SetShaderParameters ( RHICmdList , VertexShader , VertexShader . GetVertexShader ( ) , * PassParameters ) ;
SetShaderParameters ( RHICmdList , PixelShader , PixelShader . GetPixelShader ( ) , * PassParameters ) ;
2019-02-25 09:08:54 -05:00
2022-01-18 06:20:07 -05:00
RHICmdList . DrawIndexedPrimitiveIndirect ( GTwoTrianglesIndexBuffer . IndexBufferRHI , PassParameters - > IndirectDrawArgsBuffer - > GetIndirectRHICallBuffer ( ) , 0 ) ;
2019-02-25 09:08:54 -05:00
} ) ;
}
}
2022-02-22 15:35:01 -05:00
static void InternalDrawView_Lines (
FRDGBuilder & GraphBuilder ,
const FViewInfo & View ,
const FVector & TranslatedWorldOffsetConversion ,
FRDGBufferRef DataBuffer ,
FRDGTextureRef OutputTexture ,
FRDGTextureRef DepthTexture )
{
FRDGBufferRef IndirectBuffer = GraphBuilder . CreateBuffer ( FRDGBufferDesc : : CreateIndirectDesc < FRHIDrawIndirectParameters > ( 1 ) , TEXT ( " ShaderDraw.IndirectBuffer " ) , ERDGBufferFlags : : None ) ;
{
FShaderDrawDebugCopyCS : : FParameters * Parameters = GraphBuilder . AllocParameters < FShaderDrawDebugCopyCS : : FParameters > ( ) ;
Parameters - > ElementBuffer = GraphBuilder . CreateSRV ( DataBuffer ) ;
Parameters - > RWIndirectArgs = GraphBuilder . CreateUAV ( IndirectBuffer , PF_R32_UINT ) ;
TShaderMapRef < FShaderDrawDebugCopyCS > ComputeShader ( View . ShaderMap ) ;
ClearUnusedGraphResources ( ComputeShader , Parameters ) ;
GraphBuilder . AddPass (
RDG_EVENT_NAME ( " ShaderPrint::CopyLineArgs " ) ,
Parameters ,
ERDGPassFlags : : Compute ,
[ Parameters , ComputeShader ] ( FRHICommandList & RHICmdList )
{
FComputeShaderUtils : : Dispatch ( RHICmdList , ComputeShader , * Parameters , FIntVector ( 1 , 1 , 1 ) ) ;
} ) ;
}
TShaderMapRef < FShaderDrawDebugVS > VertexShader ( View . ShaderMap ) ;
TShaderMapRef < FShaderDrawDebugPS > PixelShader ( View . ShaderMap ) ;
FShaderDrawVSPSParameters * PassParameters = GraphBuilder . AllocParameters < FShaderDrawVSPSParameters > ( ) ;
PassParameters - > VS . View = View . ViewUniformBuffer ;
PassParameters - > VS . TranslatedWorldOffsetConversion = FVector3f ( TranslatedWorldOffsetConversion ) ;
PassParameters - > PS . RenderTargets [ 0 ] = FRenderTargetBinding ( OutputTexture , ERenderTargetLoadAction : : ELoad ) ;
PassParameters - > PS . OutputInvResolution = FVector2f ( 1.f / View . UnscaledViewRect . Width ( ) , 1.f / View . UnscaledViewRect . Height ( ) ) ;
PassParameters - > PS . OriginalViewRectMin = FVector2f ( View . ViewRect . Min ) ;
PassParameters - > PS . OriginalViewSize = FVector2f ( View . ViewRect . Width ( ) , View . ViewRect . Height ( ) ) ;
PassParameters - > PS . OriginalBufferInvSize = FVector2f ( 1.f / DepthTexture - > Desc . Extent . X , 1.f / DepthTexture - > Desc . Extent . Y ) ;
PassParameters - > PS . DepthTexture = DepthTexture ;
PassParameters - > PS . DepthSampler = TStaticSamplerState < SF_Point , AM_Clamp , AM_Clamp , AM_Clamp > : : GetRHI ( ) ;
PassParameters - > VS . ShaderDrawDebugPrimitive = GraphBuilder . CreateSRV ( DataBuffer ) ;
PassParameters - > VS . IndirectBuffer = IndirectBuffer ;
ValidateShaderParameters ( PixelShader , PassParameters - > PS ) ;
ClearUnusedGraphResources ( PixelShader , & PassParameters - > PS , { IndirectBuffer } ) ;
ValidateShaderParameters ( VertexShader , PassParameters - > VS ) ;
ClearUnusedGraphResources ( VertexShader , & PassParameters - > VS , { IndirectBuffer } ) ;
const FIntRect Viewport = View . UnscaledViewRect ;
GraphBuilder . AddPass (
RDG_EVENT_NAME ( " ShaderPrint::DrawLines " ) ,
PassParameters ,
ERDGPassFlags : : Raster ,
[ VertexShader , PixelShader , PassParameters , IndirectBuffer , Viewport ] ( FRHICommandList & RHICmdList )
{
// Marks the indirect draw parameter as used by the pass, given it's not used directly by any of the shaders.
PassParameters - > VS . IndirectBuffer - > MarkResourceAsUsed ( ) ;
FGraphicsPipelineStateInitializer GraphicsPSOInit ;
RHICmdList . ApplyCachedRenderTargets ( GraphicsPSOInit ) ;
GraphicsPSOInit . DepthStencilState = TStaticDepthStencilState < false , CF_Always > : : GetRHI ( ) ;
GraphicsPSOInit . BlendState = TStaticBlendState < CW_RGBA , BO_Add , BF_One , BF_InverseSourceAlpha , BO_Add , BF_Zero , BF_One > : : GetRHI ( ) ; // Premultiplied-alpha composition
GraphicsPSOInit . RasterizerState = TStaticRasterizerState < FM_Solid , CM_None , true > : : GetRHI ( ) ;
GraphicsPSOInit . PrimitiveType = PT_LineList ;
GraphicsPSOInit . BoundShaderState . VertexDeclarationRHI = GEmptyVertexDeclaration . VertexDeclarationRHI ;
GraphicsPSOInit . BoundShaderState . VertexShaderRHI = VertexShader . GetVertexShader ( ) ;
GraphicsPSOInit . BoundShaderState . PixelShaderRHI = PixelShader . GetPixelShader ( ) ;
SetGraphicsPipelineState ( RHICmdList , GraphicsPSOInit , 0 ) ;
RHICmdList . SetViewport ( Viewport . Min . X , Viewport . Min . Y , 0.0f , Viewport . Max . X , Viewport . Max . Y , 1.0f ) ;
SetShaderParameters ( RHICmdList , VertexShader , VertexShader . GetVertexShader ( ) , PassParameters - > VS ) ;
SetShaderParameters ( RHICmdList , PixelShader , PixelShader . GetPixelShader ( ) , PassParameters - > PS ) ;
// Marks the indirect draw parameter as used by the pass, given it's not used directly by any of the shaders.
FRHIBuffer * IndirectBufferRHI = PassParameters - > VS . IndirectBuffer - > GetIndirectRHICallBuffer ( ) ;
check ( IndirectBufferRHI ! = nullptr ) ;
RHICmdList . DrawPrimitiveIndirect ( IndirectBufferRHI , 0 ) ;
} ) ;
}
void DrawView ( FRDGBuilder & GraphBuilder , const FViewInfo & View , const FScreenPassTexture & OutputTexture , const FScreenPassTexture & DepthTexture )
{
check ( OutputTexture . IsValid ( ) ) ;
RDG_EVENT_SCOPE ( GraphBuilder , " ShaderPrint::DrawView " ) ;
// Lines
{
FRDGBufferRef DataBuffer = View . ShaderPrintData . ShaderPrintLineBuffer ;
InternalDrawView_Lines ( GraphBuilder , View , FVector : : ZeroVector , DataBuffer , OutputTexture . Texture , DepthTexture . Texture ) ;
}
// Locked lines
if ( View . ViewState & & View . ViewState - > ShaderPrintStateData . bIsLocked )
{
const FVector LockedToCurrentTranslatedOffset = View . ViewMatrices . GetPreViewTranslation ( ) - View . ViewState - > ShaderPrintStateData . PreViewTranslation ;
FRDGBufferRef DataBuffer = GraphBuilder . RegisterExternalBuffer ( View . ViewState - > ShaderPrintStateData . LineBuffer ) ;
InternalDrawView_Lines ( GraphBuilder , View , LockedToCurrentTranslatedOffset , DataBuffer , OutputTexture . Texture , DepthTexture . Texture ) ;
}
// Characters
{
InternalDrawView_Characters ( GraphBuilder , View , OutputTexture ) ;
}
}
2019-02-25 09:08:54 -05:00
void EndView ( FViewInfo & View )
{
2021-09-24 03:34:49 -04:00
View . ShaderPrintData = FShaderPrintData ( ) ;
2022-02-22 15:35:01 -05:00
GDefaultView = nullptr ;
2019-02-25 09:08:54 -05:00
}
}