2019-12-26 14:45:42 -05:00
// Copyright Epic Games, Inc. All Rights Reserved.
Merging //UE4/Dev-Rendering-Graph@4492585 to Dev-Rendering (//UE4/Dev-Rendering)
This implements the framework to write the high level rendering code into passes organized in direct acyclic graph. It is also unifying the uniform buffer, shader parameters, and pass parameters to same single API: structures with run time meta data. This allow high level user code be extremely seamless, user code debugging, and render graph ease of implementation and debugging.
Issue of collaborative work of Arne Schnober, Brian Karis, Daniel Wright, Marcus Wassmer and Guillaume Abadie.
Names of the graph managed resources are not final.
#rb Arne.Schnober, Brian.Karis, Daniel.Wright, Marcus.Wassmer
[CL 4492694 by Guillaume Abadie in Dev-Rendering branch]
2018-10-19 17:36:35 -04:00
# include "RenderGraphUtils.h"
2019-09-14 09:45:25 -04:00
# include "ClearQuad.h"
# include "ClearReplacementShaders.h"
2019-10-11 15:33:31 -04:00
# include "ShaderParameterUtils.h"
2019-01-18 19:20:21 -05:00
# include <initializer_list>
2020-01-24 18:07:01 -05:00
# include "GlobalShader.h"
# include "PixelShaderUtils.h"
Merging //UE4/Dev-Rendering-Graph@4492585 to Dev-Rendering (//UE4/Dev-Rendering)
This implements the framework to write the high level rendering code into passes organized in direct acyclic graph. It is also unifying the uniform buffer, shader parameters, and pass parameters to same single API: structures with run time meta data. This allow high level user code be extremely seamless, user code debugging, and render graph ease of implementation and debugging.
Issue of collaborative work of Arne Schnober, Brian Karis, Daniel Wright, Marcus Wassmer and Guillaume Abadie.
Names of the graph managed resources are not final.
#rb Arne.Schnober, Brian.Karis, Daniel.Wright, Marcus.Wassmer
[CL 4492694 by Guillaume Abadie in Dev-Rendering branch]
2018-10-19 17:36:35 -04:00
2019-09-14 09:45:25 -04:00
/** Adds a render graph tracked buffer suitable for use as a copy destination. */
# define RDG_BUFFER_COPY_DEST(MemberName) \
INTERNAL_SHADER_PARAMETER_EXPLICIT ( UBMT_RDG_BUFFER_COPY_DEST , TShaderResourceParameterTypeInfo < FRDGBufferRef > , FRDGBufferRef , MemberName , , = nullptr , EShaderPrecisionModifier : : Float , TEXT ( " " ) , false )
/** Adds a render graph tracked texture suitable for use as a copy destination. */
# define RDG_TEXTURE_COPY_DEST(MemberName) \
INTERNAL_SHADER_PARAMETER_EXPLICIT ( UBMT_RDG_TEXTURE_COPY_DEST , TShaderResourceParameterTypeInfo < FRDGTextureRef > , FRDGTextureRef , MemberName , , = nullptr , EShaderPrecisionModifier : : Float , TEXT ( " " ) , false )
2019-01-18 19:20:21 -05:00
void ClearUnusedGraphResourcesImpl (
const FShaderParameterBindings & ShaderBindings ,
const FShaderParametersMetadata * ParametersMetadata ,
void * InoutParameters ,
2020-01-24 18:07:01 -05:00
std : : initializer_list < FRDGResourceRef > ExcludeList )
Merging //UE4/Dev-Rendering-Graph@4492585 to Dev-Rendering (//UE4/Dev-Rendering)
This implements the framework to write the high level rendering code into passes organized in direct acyclic graph. It is also unifying the uniform buffer, shader parameters, and pass parameters to same single API: structures with run time meta data. This allow high level user code be extremely seamless, user code debugging, and render graph ease of implementation and debugging.
Issue of collaborative work of Arne Schnober, Brian Karis, Daniel Wright, Marcus Wassmer and Guillaume Abadie.
Names of the graph managed resources are not final.
#rb Arne.Schnober, Brian.Karis, Daniel.Wright, Marcus.Wassmer
[CL 4492694 by Guillaume Abadie in Dev-Rendering branch]
2018-10-19 17:36:35 -04:00
{
2020-03-10 08:04:05 -04:00
int32 UsedResourceIdx = 0 ;
Merging //UE4/Dev-Rendering-Graph@4492585 to Dev-Rendering (//UE4/Dev-Rendering)
This implements the framework to write the high level rendering code into passes organized in direct acyclic graph. It is also unifying the uniform buffer, shader parameters, and pass parameters to same single API: structures with run time meta data. This allow high level user code be extremely seamless, user code debugging, and render graph ease of implementation and debugging.
Issue of collaborative work of Arne Schnober, Brian Karis, Daniel Wright, Marcus Wassmer and Guillaume Abadie.
Names of the graph managed resources are not final.
#rb Arne.Schnober, Brian.Karis, Daniel.Wright, Marcus.Wassmer
[CL 4492694 by Guillaume Abadie in Dev-Rendering branch]
2018-10-19 17:36:35 -04:00
uint8 * Base = reinterpret_cast < uint8 * > ( InoutParameters ) ;
for ( int32 ResourceIndex = 0 , Num = ParametersMetadata - > GetLayout ( ) . Resources . Num ( ) ; ResourceIndex < Num ; ResourceIndex + + )
{
2018-12-18 21:41:17 -05:00
EUniformBufferBaseType Type = ParametersMetadata - > GetLayout ( ) . Resources [ ResourceIndex ] . MemberType ;
uint16 ByteOffset = ParametersMetadata - > GetLayout ( ) . Resources [ ResourceIndex ] . MemberOffset ;
2020-03-10 08:04:05 -04:00
bool bRDGResource = Type = = UBMT_RDG_TEXTURE | |
Type = = UBMT_RDG_TEXTURE_SRV | |
Type = = UBMT_RDG_BUFFER_SRV | |
Type = = UBMT_RDG_TEXTURE_UAV | |
Type = = UBMT_RDG_BUFFER_UAV ;
Merging //UE4/Dev-Rendering-Graph@4492585 to Dev-Rendering (//UE4/Dev-Rendering)
This implements the framework to write the high level rendering code into passes organized in direct acyclic graph. It is also unifying the uniform buffer, shader parameters, and pass parameters to same single API: structures with run time meta data. This allow high level user code be extremely seamless, user code debugging, and render graph ease of implementation and debugging.
Issue of collaborative work of Arne Schnober, Brian Karis, Daniel Wright, Marcus Wassmer and Guillaume Abadie.
Names of the graph managed resources are not final.
#rb Arne.Schnober, Brian.Karis, Daniel.Wright, Marcus.Wassmer
[CL 4492694 by Guillaume Abadie in Dev-Rendering branch]
2018-10-19 17:36:35 -04:00
2020-03-10 08:04:05 -04:00
if ( UsedResourceIdx < ShaderBindings . ResourceParameters . Num ( ) )
Merging //UE4/Dev-Rendering-Graph@4492585 to Dev-Rendering (//UE4/Dev-Rendering)
This implements the framework to write the high level rendering code into passes organized in direct acyclic graph. It is also unifying the uniform buffer, shader parameters, and pass parameters to same single API: structures with run time meta data. This allow high level user code be extremely seamless, user code debugging, and render graph ease of implementation and debugging.
Issue of collaborative work of Arne Schnober, Brian Karis, Daniel Wright, Marcus Wassmer and Guillaume Abadie.
Names of the graph managed resources are not final.
#rb Arne.Schnober, Brian.Karis, Daniel.Wright, Marcus.Wassmer
[CL 4492694 by Guillaume Abadie in Dev-Rendering branch]
2018-10-19 17:36:35 -04:00
{
2020-03-10 08:04:05 -04:00
const FShaderParameterBindings : : FResourceParameter & ResourceParameter = ShaderBindings . ResourceParameters [ UsedResourceIdx ] ;
if ( ByteOffset = = ResourceParameter . ByteOffset )
Merging //UE4/Dev-Rendering-Graph@4492585 to Dev-Rendering (//UE4/Dev-Rendering)
This implements the framework to write the high level rendering code into passes organized in direct acyclic graph. It is also unifying the uniform buffer, shader parameters, and pass parameters to same single API: structures with run time meta data. This allow high level user code be extremely seamless, user code debugging, and render graph ease of implementation and debugging.
Issue of collaborative work of Arne Schnober, Brian Karis, Daniel Wright, Marcus Wassmer and Guillaume Abadie.
Names of the graph managed resources are not final.
#rb Arne.Schnober, Brian.Karis, Daniel.Wright, Marcus.Wassmer
[CL 4492694 by Guillaume Abadie in Dev-Rendering branch]
2018-10-19 17:36:35 -04:00
{
2020-03-10 08:04:05 -04:00
// check next one
UsedResourceIdx + + ;
Merging //UE4/Dev-Rendering-Graph@4492585 to Dev-Rendering (//UE4/Dev-Rendering)
This implements the framework to write the high level rendering code into passes organized in direct acyclic graph. It is also unifying the uniform buffer, shader parameters, and pass parameters to same single API: structures with run time meta data. This allow high level user code be extremely seamless, user code debugging, and render graph ease of implementation and debugging.
Issue of collaborative work of Arne Schnober, Brian Karis, Daniel Wright, Marcus Wassmer and Guillaume Abadie.
Names of the graph managed resources are not final.
#rb Arne.Schnober, Brian.Karis, Daniel.Wright, Marcus.Wassmer
[CL 4492694 by Guillaume Abadie in Dev-Rendering branch]
2018-10-19 17:36:35 -04:00
continue ;
}
2020-03-10 08:04:05 -04:00
// make sure we dont jump over used resources
check ( ByteOffset < = ResourceParameter . ByteOffset ) ;
Merging //UE4/Dev-Rendering-Graph@4492585 to Dev-Rendering (//UE4/Dev-Rendering)
This implements the framework to write the high level rendering code into passes organized in direct acyclic graph. It is also unifying the uniform buffer, shader parameters, and pass parameters to same single API: structures with run time meta data. This allow high level user code be extremely seamless, user code debugging, and render graph ease of implementation and debugging.
Issue of collaborative work of Arne Schnober, Brian Karis, Daniel Wright, Marcus Wassmer and Guillaume Abadie.
Names of the graph managed resources are not final.
#rb Arne.Schnober, Brian.Karis, Daniel.Wright, Marcus.Wassmer
[CL 4492694 by Guillaume Abadie in Dev-Rendering branch]
2018-10-19 17:36:35 -04:00
}
2020-03-10 08:04:05 -04:00
if ( bRDGResource )
2019-01-18 19:20:21 -05:00
{
2020-03-10 08:04:05 -04:00
for ( FRDGResourceRef ExcludeResource : ExcludeList )
2020-01-24 18:07:01 -05:00
{
2020-03-10 08:04:05 -04:00
auto Resource = * reinterpret_cast < const FRDGResource * const * > ( Base + ByteOffset ) ;
if ( Resource = = ExcludeResource )
{
continue ;
}
2020-01-24 18:07:01 -05:00
}
2020-03-10 08:04:05 -04:00
void * * ResourcePointerAddress = reinterpret_cast < void * * > ( Base + ByteOffset ) ;
* ResourcePointerAddress = nullptr ;
}
2020-01-24 18:07:01 -05:00
}
}
void ClearUnusedGraphResourcesImpl (
TArrayView < const FShaderParameterBindings * > ShaderBindingsList ,
const FShaderParametersMetadata * ParametersMetadata ,
void * InoutParameters ,
std : : initializer_list < FRDGResourceRef > ExcludeList )
{
2020-03-10 08:04:05 -04:00
TArray < int32 , TInlineAllocator < SF_NumFrequencies > > GraphResourceIds ;
GraphResourceIds . SetNumZeroed ( ShaderBindingsList . Num ( ) ) ;
2020-01-24 18:07:01 -05:00
uint8 * Base = reinterpret_cast < uint8 * > ( InoutParameters ) ;
for ( int32 ResourceIndex = 0 , Num = ParametersMetadata - > GetLayout ( ) . Resources . Num ( ) ; ResourceIndex < Num ; ResourceIndex + + )
{
EUniformBufferBaseType Type = ParametersMetadata - > GetLayout ( ) . Resources [ ResourceIndex ] . MemberType ;
uint16 ByteOffset = ParametersMetadata - > GetLayout ( ) . Resources [ ResourceIndex ] . MemberOffset ;
2020-03-10 08:04:05 -04:00
bool bRDGResource = Type = = UBMT_RDG_TEXTURE | |
Type = = UBMT_RDG_TEXTURE_SRV | |
Type = = UBMT_RDG_BUFFER_SRV | |
Type = = UBMT_RDG_TEXTURE_UAV | |
Type = = UBMT_RDG_BUFFER_UAV ;
2020-01-24 18:07:01 -05:00
bool bResourceIsUsed = false ;
2020-03-10 08:04:05 -04:00
for ( int32 Index = 0 ; Index < ShaderBindingsList . Num ( ) ; + + Index )
2020-01-24 18:07:01 -05:00
{
2020-03-10 08:04:05 -04:00
const FShaderParameterBindings & ShaderBindings = * ShaderBindingsList [ Index ] ;
int32 & GraphResourceId = GraphResourceIds [ Index ] ;
2020-03-16 23:59:33 -04:00
for ( ; GraphResourceId < ShaderBindings . ResourceParameters . Num ( ) & & ShaderBindings . ResourceParameters [ GraphResourceId ] . ByteOffset < ByteOffset ; + + GraphResourceId )
2020-01-24 18:07:01 -05:00
{
2020-03-10 08:04:05 -04:00
}
bResourceIsUsed | = GraphResourceId < ShaderBindings . ResourceParameters . Num ( ) & & ByteOffset = = ShaderBindings . ResourceParameters [ GraphResourceId ] . ByteOffset ; // check the resouce type as well?
}
if ( bRDGResource & & ! bResourceIsUsed )
{
for ( FRDGResourceRef ExcludeResource : ExcludeList )
{
auto Resource = * reinterpret_cast < const FRDGResource * const * > ( Base + ByteOffset ) ;
if ( Resource = = ExcludeResource )
2020-01-24 18:07:01 -05:00
{
2020-03-10 08:04:05 -04:00
continue ;
2020-01-24 18:07:01 -05:00
}
}
2020-03-10 08:04:05 -04:00
void * * ResourcePointerAddress = reinterpret_cast < void * * > ( Base + ByteOffset ) ;
* ResourcePointerAddress = nullptr ;
2020-01-24 18:07:01 -05:00
}
Merging //UE4/Dev-Rendering-Graph@4492585 to Dev-Rendering (//UE4/Dev-Rendering)
This implements the framework to write the high level rendering code into passes organized in direct acyclic graph. It is also unifying the uniform buffer, shader parameters, and pass parameters to same single API: structures with run time meta data. This allow high level user code be extremely seamless, user code debugging, and render graph ease of implementation and debugging.
Issue of collaborative work of Arne Schnober, Brian Karis, Daniel Wright, Marcus Wassmer and Guillaume Abadie.
Names of the graph managed resources are not final.
#rb Arne.Schnober, Brian.Karis, Daniel.Wright, Marcus.Wassmer
[CL 4492694 by Guillaume Abadie in Dev-Rendering branch]
2018-10-19 17:36:35 -04:00
}
}
2018-12-18 21:41:17 -05:00
FRDGTextureRef RegisterExternalTextureWithFallback (
FRDGBuilder & GraphBuilder ,
const TRefCountPtr < IPooledRenderTarget > & ExternalPooledTexture ,
const TRefCountPtr < IPooledRenderTarget > & FallbackPooledTexture ,
const TCHAR * ExternalPooledTextureName )
{
ensureMsgf ( FallbackPooledTexture . IsValid ( ) , TEXT ( " RegisterExternalTextureWithDummyFallback() requires a valid fallback pooled texture. " ) ) ;
if ( ExternalPooledTexture . IsValid ( ) )
{
return GraphBuilder . RegisterExternalTexture ( ExternalPooledTexture , ExternalPooledTextureName ) ;
}
else
{
return GraphBuilder . RegisterExternalTexture ( FallbackPooledTexture ) ;
}
}
2019-09-14 09:45:25 -04:00
BEGIN_SHADER_PARAMETER_STRUCT ( FCopyTextureParameters , )
SHADER_PARAMETER_RDG_TEXTURE ( , Input )
RDG_TEXTURE_COPY_DEST ( Output )
END_SHADER_PARAMETER_STRUCT ( )
void AddCopyTexturePass (
FRDGBuilder & GraphBuilder ,
FRDGTextureRef InputTexture ,
FRDGTextureRef OutputTexture ,
const FRHICopyTextureInfo & CopyInfo )
{
const FRDGTextureDesc & InputDesc = InputTexture - > Desc ;
const FRDGTextureDesc & OutputDesc = OutputTexture - > Desc ;
checkf ( InputDesc . Format = = OutputDesc . Format , TEXT ( " This method does not support format conversion. " ) ) ;
FCopyTextureParameters * Parameters = GraphBuilder . AllocParameters < FCopyTextureParameters > ( ) ;
Parameters - > Input = InputTexture ;
Parameters - > Output = OutputTexture ;
2020-01-24 18:07:01 -05:00
GraphBuilder . AddPass (
RDG_EVENT_NAME ( " CopyTexture(%s -> %s) " , InputTexture - > Name , OutputTexture - > Name ) ,
Parameters ,
ERDGPassFlags : : Copy ,
2019-09-14 09:45:25 -04:00
[ InputTexture , OutputTexture , CopyInfo ] ( FRHICommandList & RHICmdList )
{
// Manually mark as used since we aren't invoking any shaders.
InputTexture - > MarkResourceAsUsed ( ) ;
OutputTexture - > MarkResourceAsUsed ( ) ;
RHICmdList . CopyTexture ( InputTexture - > GetRHI ( ) , OutputTexture - > GetRHI ( ) , CopyInfo ) ;
} ) ;
}
2019-10-17 04:24:15 -04:00
BEGIN_SHADER_PARAMETER_STRUCT ( FCopyToResolveTargetParameters , )
SHADER_PARAMETER_RDG_TEXTURE ( , Input )
RDG_TEXTURE_COPY_DEST ( Output )
END_SHADER_PARAMETER_STRUCT ( )
void AddCopyToResolveTargetPass (
FRDGBuilder & GraphBuilder ,
FRDGTextureRef InputTexture ,
FRDGTextureRef OutputTexture ,
const FResolveParams & ResolveParams )
{
FCopyToResolveTargetParameters * Parameters = GraphBuilder . AllocParameters < FCopyToResolveTargetParameters > ( ) ;
Parameters - > Input = InputTexture ;
Parameters - > Output = OutputTexture ;
GraphBuilder . AddPass ( RDG_EVENT_NAME ( " CopyTexture " ) , Parameters , ERDGPassFlags : : Copy ,
[ InputTexture , OutputTexture , ResolveParams ] ( FRHICommandList & RHICmdList )
{
// Manually mark as used since we aren't invoking any shaders.
InputTexture - > MarkResourceAsUsed ( ) ;
OutputTexture - > MarkResourceAsUsed ( ) ;
RHICmdList . CopyToResolveTarget ( InputTexture - > GetRHI ( ) , OutputTexture - > GetRHI ( ) , ResolveParams ) ;
} ) ;
}
2019-09-14 09:45:25 -04:00
BEGIN_SHADER_PARAMETER_STRUCT ( FClearBufferUAVParameters , )
SHADER_PARAMETER_RDG_BUFFER_UAV ( RWBuffer < uint > , BufferUAV )
END_SHADER_PARAMETER_STRUCT ( )
void AddClearUAVPass ( FRDGBuilder & GraphBuilder , FRDGBufferUAVRef BufferUAV , uint32 Value )
{
FClearBufferUAVParameters * Parameters = GraphBuilder . AllocParameters < FClearBufferUAVParameters > ( ) ;
Parameters - > BufferUAV = BufferUAV ;
GraphBuilder . AddPass (
2020-01-24 18:07:01 -05:00
RDG_EVENT_NAME ( " ClearBuffer(%s Size=%ubytes) " , BufferUAV - > GetParent ( ) - > Name , BufferUAV - > GetParent ( ) - > Desc . GetTotalNumBytes ( ) ) ,
2019-09-14 09:45:25 -04:00
Parameters ,
ERDGPassFlags : : Compute ,
[ & Parameters , BufferUAV , Value ] ( FRHICommandList & RHICmdList )
{
// This might be called if using ClearTinyUAV.
BufferUAV - > MarkResourceAsUsed ( ) ;
2020-01-24 18:07:01 -05:00
RHICmdList . ClearUAVUint ( BufferUAV - > GetRHI ( ) , FUintVector4 ( Value , Value , Value , Value ) ) ;
2019-09-14 09:45:25 -04:00
} ) ;
}
BEGIN_SHADER_PARAMETER_STRUCT ( FClearTextureUAVParameters , )
SHADER_PARAMETER_RDG_TEXTURE_UAV ( RWTexture2D , TextureUAV )
END_SHADER_PARAMETER_STRUCT ( )
2020-01-24 18:07:01 -05:00
void AddClearUAVPass ( FRDGBuilder & GraphBuilder , FRDGTextureUAVRef TextureUAV , const FUintVector4 & ClearValues )
{
check ( TextureUAV ) ;
FClearTextureUAVParameters * Parameters = GraphBuilder . AllocParameters < FClearTextureUAVParameters > ( ) ;
Parameters - > TextureUAV = TextureUAV ;
FRDGTextureRef Texture = TextureUAV - > GetParent ( ) ;
GraphBuilder . AddPass (
RDG_EVENT_NAME ( " ClearTextureUint(%s %s %dx%d Mip=%d) " ,
Texture - > Name ,
GPixelFormats [ Texture - > Desc . Format ] . Name ,
Texture - > Desc . Extent . X , Texture - > Desc . Extent . Y ,
int32 ( TextureUAV - > Desc . MipLevel ) ) ,
Parameters ,
ERDGPassFlags : : Compute ,
[ & Parameters , TextureUAV , ClearValues ] ( FRHICommandList & RHICmdList )
{
const FRDGTextureDesc & LocalTextureDesc = TextureUAV - > GetParent ( ) - > Desc ;
FRHIUnorderedAccessView * RHITextureUAV = TextureUAV - > GetRHI ( ) ;
RHICmdList . ClearUAVUint ( RHITextureUAV , ClearValues ) ;
} ) ;
}
void AddClearUAVPass ( FRDGBuilder & GraphBuilder , FRDGTextureUAVRef TextureUAV , const FVector4 & ClearValues )
2019-09-14 09:45:25 -04:00
{
check ( TextureUAV ) ;
FClearTextureUAVParameters * Parameters = GraphBuilder . AllocParameters < FClearTextureUAVParameters > ( ) ;
Parameters - > TextureUAV = TextureUAV ;
const FRDGTextureDesc & TextureDesc = TextureUAV - > GetParent ( ) - > Desc ;
GraphBuilder . AddPass (
2020-01-24 18:07:01 -05:00
RDG_EVENT_NAME ( " ClearTextureFloat(%s) %dx%d " , TextureUAV - > GetParent ( ) - > Name , TextureDesc . Extent . X , TextureDesc . Extent . Y ) ,
2019-09-14 09:45:25 -04:00
Parameters ,
ERDGPassFlags : : Compute ,
[ & Parameters , TextureUAV , ClearValues ] ( FRHICommandList & RHICmdList )
{
const FRDGTextureDesc & LocalTextureDesc = TextureUAV - > GetParent ( ) - > Desc ;
FRHIUnorderedAccessView * RHITextureUAV = TextureUAV - > GetRHI ( ) ;
2020-01-24 18:07:01 -05:00
RHICmdList . ClearUAVFloat ( RHITextureUAV , ClearValues ) ;
2019-09-14 09:45:25 -04:00
} ) ;
}
void AddClearUAVPass ( FRDGBuilder & GraphBuilder , FRDGTextureUAVRef TextureUAV , const uint32 ( & ClearValues ) [ 4 ] )
{
2020-01-24 18:07:01 -05:00
AddClearUAVPass ( GraphBuilder , TextureUAV , FUintVector4 ( ClearValues [ 0 ] , ClearValues [ 1 ] , ClearValues [ 2 ] , ClearValues [ 3 ] ) ) ;
}
void AddClearUAVPass ( FRDGBuilder & GraphBuilder , FRDGTextureUAVRef TextureUAV , const float ( & ClearValues ) [ 4 ] )
{
AddClearUAVPass ( GraphBuilder , TextureUAV , FVector4 ( ClearValues [ 0 ] , ClearValues [ 1 ] , ClearValues [ 2 ] , ClearValues [ 3 ] ) ) ;
2019-09-14 09:45:25 -04:00
}
void AddClearUAVPass ( FRDGBuilder & GraphBuilder , FRDGTextureUAVRef TextureUAV , const FLinearColor & ClearColor )
{
2020-01-24 18:07:01 -05:00
AddClearUAVPass ( GraphBuilder , TextureUAV , FVector4 ( ClearColor . R , ClearColor . G , ClearColor . B , ClearColor . A ) ) ;
}
class FClearUAVRectsPS : public FGlobalShader
{
DECLARE_GLOBAL_SHADER ( FClearUAVRectsPS ) ;
SHADER_USE_PARAMETER_STRUCT ( FClearUAVRectsPS , FGlobalShader ) ;
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
SHADER_PARAMETER ( FUintVector4 , ClearValue )
SHADER_PARAMETER_RDG_TEXTURE_UAV ( RWTexture2D , ClearResource )
END_SHADER_PARAMETER_STRUCT ( )
using FPermutationDomain = TShaderPermutationDomain < > ;
static bool ShouldCompilePermutation ( const FGlobalShaderPermutationParameters & Parameters )
{
return IsFeatureLevelSupported ( Parameters . Platform , ERHIFeatureLevel : : SM5 ) ;
}
static void ModifyCompilationEnvironment ( const FGlobalShaderPermutationParameters & Parameters , FShaderCompilerEnvironment & OutEnvironment )
{
FGlobalShader : : ModifyCompilationEnvironment ( Parameters , OutEnvironment ) ;
OutEnvironment . SetDefine ( TEXT ( " ENABLE_CLEAR_VALUE " ) , 1 ) ;
OutEnvironment . SetDefine ( TEXT ( " RESOURCE_TYPE " ) , 1 ) ;
OutEnvironment . SetDefine ( TEXT ( " VALUE_TYPE " ) , TEXT ( " uint4 " ) ) ;
}
} ;
IMPLEMENT_GLOBAL_SHADER ( FClearUAVRectsPS , " /Engine/Private/ClearReplacementShaders.usf " , " ClearTextureRWPS " , SF_Pixel ) ;
BEGIN_SHADER_PARAMETER_STRUCT ( FClearUAVRectsParameters , )
SHADER_PARAMETER_STRUCT_INCLUDE ( FPixelShaderUtils : : FRasterizeToRectsVS : : FParameters , VS )
SHADER_PARAMETER_STRUCT_INCLUDE ( FClearUAVRectsPS : : FParameters , PS )
RENDER_TARGET_BINDING_SLOTS ( )
END_SHADER_PARAMETER_STRUCT ( )
void AddClearUAVPass ( FRDGBuilder & GraphBuilder , FRDGTextureUAVRef TextureUAV , const uint32 ( & ClearValues ) [ 4 ] , FRDGBufferSRVRef RectMinMaxBufferSRV , uint32 NumRects )
{
if ( NumRects = = 0 )
{
AddClearUAVPass ( GraphBuilder , TextureUAV , ClearValues ) ;
return ;
}
check ( TextureUAV & & RectMinMaxBufferSRV ) ;
FClearUAVRectsParameters * PassParameters = GraphBuilder . AllocParameters < FClearUAVRectsParameters > ( ) ;
PassParameters - > PS . ClearValue . X = ClearValues [ 0 ] ;
PassParameters - > PS . ClearValue . Y = ClearValues [ 1 ] ;
PassParameters - > PS . ClearValue . Z = ClearValues [ 2 ] ;
PassParameters - > PS . ClearValue . W = ClearValues [ 3 ] ;
PassParameters - > PS . ClearResource = TextureUAV ;
auto * ShaderMap = GetGlobalShaderMap ( GMaxRHIFeatureLevel ) ;
auto PixelShader = ShaderMap - > GetShader < FClearUAVRectsPS > ( ) ;
const FRDGTextureRef Texture = TextureUAV - > GetParent ( ) ;
const FIntPoint TextureSize = Texture - > Desc . Extent ;
FPixelShaderUtils : : AddRasterizeToRectsPass < FClearUAVRectsPS > ( GraphBuilder ,
ShaderMap ,
RDG_EVENT_NAME ( " ClearTextureRects(%s %s %dx%d Mip=%d) " ,
Texture - > Name ,
GPixelFormats [ Texture - > Desc . Format ] . Name ,
Texture - > Desc . Extent . X , Texture - > Desc . Extent . Y ,
int32 ( TextureUAV - > Desc . MipLevel ) ) ,
PixelShader ,
PassParameters ,
TextureSize ,
RectMinMaxBufferSRV ,
NumRects ,
TStaticBlendState < > : : GetRHI ( ) ,
TStaticRasterizerState < > : : GetRHI ( ) ,
TStaticDepthStencilState < false , CF_Always > : : GetRHI ( ) ) ;
2019-09-14 09:45:25 -04:00
}
void AddClearRenderTargetPass ( FRDGBuilder & GraphBuilder , FRDGTextureRef Texture , const FLinearColor & ClearColor )
{
check ( Texture ) ;
FRenderTargetParameters * Parameters = GraphBuilder . AllocParameters < FRenderTargetParameters > ( ) ;
Parameters - > RenderTargets [ 0 ] = FRenderTargetBinding ( Texture , ERenderTargetLoadAction : : ENoAction ) ;
GraphBuilder . AddPass (
RDG_EVENT_NAME ( " ClearRenderTarget(%s) %dx%d " , Texture - > Name , Texture - > Desc . Extent . X , Texture - > Desc . Extent . Y ) ,
Parameters ,
ERDGPassFlags : : Raster ,
[ Parameters , ClearColor ] ( FRHICommandList & RHICmdList )
{
DrawClearQuad ( RHICmdList , ClearColor ) ;
} ) ;
}
void AddClearDepthStencilPass (
FRDGBuilder & GraphBuilder ,
FRDGTextureRef Texture ,
bool bClearDepth ,
float Depth ,
bool bClearStencil ,
uint8 Stencil )
{
check ( Texture ) ;
FExclusiveDepthStencil ExclusiveDepthStencil ;
ERenderTargetLoadAction DepthLoadAction = ERenderTargetLoadAction : : ELoad ;
ERenderTargetLoadAction StencilLoadAction = ERenderTargetLoadAction : : ENoAction ;
const bool bHasStencil = Texture - > Desc . Format = = PF_DepthStencil ;
// We can't clear stencil if we don't have it.
bClearStencil & = bHasStencil ;
if ( bClearDepth )
{
ExclusiveDepthStencil . SetDepthWrite ( ) ;
DepthLoadAction = ERenderTargetLoadAction : : ENoAction ;
}
if ( bHasStencil )
{
if ( bClearStencil )
{
ExclusiveDepthStencil . SetStencilWrite ( ) ;
StencilLoadAction = ERenderTargetLoadAction : : ENoAction ;
}
else
{
// Preserve stencil contents.
StencilLoadAction = ERenderTargetLoadAction : : ELoad ;
}
}
FRenderTargetParameters * Parameters = GraphBuilder . AllocParameters < FRenderTargetParameters > ( ) ;
Parameters - > RenderTargets . DepthStencil = FDepthStencilBinding ( Texture , DepthLoadAction , StencilLoadAction , ExclusiveDepthStencil ) ;
GraphBuilder . AddPass (
RDG_EVENT_NAME ( " ClearDepthStencil(%s) %dx%d " , Texture - > Name , Texture - > Desc . Extent . X , Texture - > Desc . Extent . Y ) ,
Parameters ,
ERDGPassFlags : : Raster ,
[ Parameters , bClearDepth , Depth , bClearStencil , Stencil ] ( FRHICommandList & RHICmdList )
{
DrawClearQuad ( RHICmdList , false , FLinearColor ( ) , bClearDepth , Depth , bClearStencil , Stencil ) ;
} ) ;
2020-01-24 18:07:01 -05:00
}
class FClearUAVUIntCS : public FGlobalShader
{
DECLARE_GLOBAL_SHADER ( FClearUAVUIntCS )
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
SHADER_PARAMETER_RDG_BUFFER_UAV ( RWBuffer < uint > , UAV )
SHADER_PARAMETER ( uint32 , ClearValue )
SHADER_PARAMETER ( uint32 , NumEntries )
END_SHADER_PARAMETER_STRUCT ( )
using FPermutationDomain = TShaderPermutationDomain < > ;
public :
static bool ShouldCompilePermutation ( const FGlobalShaderPermutationParameters & Parameters )
{
return GetMaxSupportedFeatureLevel ( Parameters . Platform ) > = ERHIFeatureLevel : : SM5 ;
}
FClearUAVUIntCS ( const ShaderMetaType : : CompiledShaderInitializerType & Initializer )
: FGlobalShader ( Initializer )
{
2020-04-06 21:12:18 -04:00
Bindings . BindForLegacyShaderParameters ( this , Initializer . PermutationId , Initializer . ParameterMap , * FParameters : : FTypeInfo : : GetStructMetadata ( ) ) ;
2020-01-24 18:07:01 -05:00
}
FClearUAVUIntCS ( )
{
}
} ;
IMPLEMENT_GLOBAL_SHADER ( FClearUAVUIntCS , " /Engine/Private/Tools/ClearUAV.usf " , " ClearUAVUIntCS " , SF_Compute ) ;
2020-02-06 13:13:41 -05:00
void FComputeShaderUtils : : ClearUAV ( FRDGBuilder & GraphBuilder , FGlobalShaderMap * ShaderMap , FRDGBufferUAVRef UAV , uint32 ClearValue )
2020-01-24 18:07:01 -05:00
{
FClearUAVUIntCS : : FParameters * PassParameters = GraphBuilder . AllocParameters < FClearUAVUIntCS : : FParameters > ( ) ;
PassParameters - > UAV = UAV ;
PassParameters - > ClearValue = ClearValue ;
check ( UAV - > Desc . Format = = PF_R32_UINT ) ;
PassParameters - > NumEntries = UAV - > Desc . Buffer - > Desc . NumElements ;
check ( PassParameters - > NumEntries > 0 ) ;
auto ComputeShader = ShaderMap - > GetShader < FClearUAVUIntCS > ( ) ;
FComputeShaderUtils : : AddPass (
GraphBuilder ,
RDG_EVENT_NAME ( " ClearUAV " ) ,
ComputeShader ,
PassParameters ,
FIntVector ( FMath : : DivideAndRoundUp < int32 > ( PassParameters - > NumEntries , 64 ) , 1 , 1 ) ) ;
}
class FClearUAVFloatCS : public FGlobalShader
{
DECLARE_GLOBAL_SHADER ( FClearUAVFloatCS )
BEGIN_SHADER_PARAMETER_STRUCT ( FParameters , )
SHADER_PARAMETER_RDG_BUFFER_UAV ( RWBuffer < float4 > , UAVFloat )
SHADER_PARAMETER ( FVector4 , ClearValueFloat )
SHADER_PARAMETER ( uint32 , NumEntries )
END_SHADER_PARAMETER_STRUCT ( )
using FPermutationDomain = TShaderPermutationDomain < > ;
public :
static bool ShouldCompilePermutation ( const FGlobalShaderPermutationParameters & Parameters )
{
return GetMaxSupportedFeatureLevel ( Parameters . Platform ) > = ERHIFeatureLevel : : SM5 ;
}
FClearUAVFloatCS ( const ShaderMetaType : : CompiledShaderInitializerType & Initializer )
: FGlobalShader ( Initializer )
{
2020-04-06 21:12:18 -04:00
Bindings . BindForLegacyShaderParameters ( this , Initializer . PermutationId , Initializer . ParameterMap , * FParameters : : FTypeInfo : : GetStructMetadata ( ) ) ;
2020-01-24 18:07:01 -05:00
}
FClearUAVFloatCS ( )
{
}
} ;
IMPLEMENT_GLOBAL_SHADER ( FClearUAVFloatCS , " /Engine/Private/Tools/ClearUAV.usf " , " ClearUAVFloatCS " , SF_Compute ) ;
2020-02-06 13:13:41 -05:00
void FComputeShaderUtils : : ClearUAV ( FRDGBuilder & GraphBuilder , FGlobalShaderMap * ShaderMap , FRDGBufferUAVRef UAV , FVector4 ClearValue )
2020-01-24 18:07:01 -05:00
{
FClearUAVFloatCS : : FParameters * PassParameters = GraphBuilder . AllocParameters < FClearUAVFloatCS : : FParameters > ( ) ;
PassParameters - > UAVFloat = UAV ;
PassParameters - > ClearValueFloat = ClearValue ;
check ( UAV - > Desc . Format = = PF_A32B32G32R32F | | UAV - > Desc . Format = = PF_FloatRGBA ) ;
PassParameters - > NumEntries = UAV - > Desc . Buffer - > Desc . NumElements ;
check ( PassParameters - > NumEntries > 0 ) ;
auto ComputeShader = ShaderMap - > GetShader < FClearUAVFloatCS > ( ) ;
FComputeShaderUtils : : AddPass (
GraphBuilder ,
RDG_EVENT_NAME ( " ClearUAV " ) ,
ComputeShader ,
PassParameters ,
FIntVector ( FMath : : DivideAndRoundUp < int32 > ( PassParameters - > NumEntries , 64 ) , 1 , 1 ) ) ;
}
BEGIN_SHADER_PARAMETER_STRUCT ( FCopyBufferParameters , )
RDG_BUFFER_COPY_DEST ( Buffer )
END_SHADER_PARAMETER_STRUCT ( )
const void * GetInitialData ( FRDGBuilder & GraphBuilder , const void * InitialData , uint64 InitialDataSize , ERDGInitialDataFlags InitialDataFlags )
{
if ( ( InitialDataFlags & ERDGInitialDataFlags : : NoCopy ) ! = ERDGInitialDataFlags : : NoCopy )
{
// Allocates memory for the lifetime of the pass, since execution is deferred.
void * InitialDataCopy = GraphBuilder . MemStack . Alloc ( InitialDataSize , 16 ) ;
FMemory : : Memcpy ( InitialDataCopy , InitialData , InitialDataSize ) ;
return InitialDataCopy ;
}
return InitialData ;
}
FRDGBufferRef CreateStructuredBuffer (
FRDGBuilder & GraphBuilder ,
const TCHAR * Name ,
uint32 BytesPerElement ,
uint32 NumElements ,
const void * InitialData ,
uint64 InitialDataSize ,
ERDGInitialDataFlags InitialDataFlags )
{
const void * SourcePtr = GetInitialData ( GraphBuilder , InitialData , InitialDataSize , InitialDataFlags ) ;
FRDGBufferRef Buffer = GraphBuilder . CreateBuffer ( FRDGBufferDesc : : CreateStructuredDesc ( BytesPerElement , NumElements ) , Name ) ;
FCopyBufferParameters * PassParameters = GraphBuilder . AllocParameters < FCopyBufferParameters > ( ) ;
PassParameters - > Buffer = Buffer ;
GraphBuilder . AddPass (
RDG_EVENT_NAME ( " StructuredBufferUpload(%s) " , Buffer - > Name ) ,
PassParameters ,
ERDGPassFlags : : Copy ,
[ Buffer , SourcePtr , InitialDataSize ] ( FRHICommandListImmediate & RHICmdList )
{
FRHIStructuredBuffer * StructuredBuffer = Buffer - > GetRHIStructuredBuffer ( ) ;
void * DestPtr = RHICmdList . LockStructuredBuffer ( StructuredBuffer , 0 , InitialDataSize , RLM_WriteOnly ) ;
FMemory : : Memcpy ( DestPtr , SourcePtr , InitialDataSize ) ;
RHICmdList . UnlockStructuredBuffer ( StructuredBuffer ) ;
} ) ;
return Buffer ;
}
FRDGBufferRef CreateVertexBuffer (
FRDGBuilder & GraphBuilder ,
const TCHAR * Name ,
const FRDGBufferDesc & Desc ,
const void * InitialData ,
uint64 InitialDataSize ,
ERDGInitialDataFlags InitialDataFlags )
{
checkf ( Name , TEXT ( " Buffer must have a name. " ) ) ;
checkf ( Desc . UnderlyingType = = FRDGBufferDesc : : EUnderlyingType : : VertexBuffer , TEXT ( " CreateVertexBuffer called with an FRDGBufferDesc underlying type that is not 'VertexBuffer'. Buffer: %s " ) , Name ) ;
const void * SourcePtr = GetInitialData ( GraphBuilder , InitialData , InitialDataSize , InitialDataFlags ) ;
FRDGBufferRef Buffer = GraphBuilder . CreateBuffer ( Desc , Name ) ;
FCopyBufferParameters * PassParameters = GraphBuilder . AllocParameters < FCopyBufferParameters > ( ) ;
PassParameters - > Buffer = Buffer ;
GraphBuilder . AddPass (
RDG_EVENT_NAME ( " VertexBufferUpload(%s) " , Buffer - > Name ) ,
PassParameters ,
ERDGPassFlags : : Copy ,
[ Buffer , SourcePtr , InitialDataSize ] ( FRHICommandListImmediate & RHICmdList )
{
FRHIVertexBuffer * VertexBuffer = Buffer - > GetRHIVertexBuffer ( ) ;
void * DestPtr = RHICmdList . LockVertexBuffer ( VertexBuffer , 0 , InitialDataSize , RLM_WriteOnly ) ;
FMemory : : Memcpy ( DestPtr , SourcePtr , InitialDataSize ) ;
RHICmdList . UnlockVertexBuffer ( VertexBuffer ) ;
} ) ;
return Buffer ;
2019-09-14 09:45:25 -04:00
}