2020-09-24 00:43:27 -04:00
// Copyright Epic Games, Inc. All Rights Reserved.
# pragma once
# include "RenderGraphDefinitions.h"
2020-11-12 13:39:39 -04:00
# include "ShaderParameterMacros.h"
2020-09-24 00:43:27 -04:00
/** A helper class for identifying and accessing a render graph pass parameter. */
class FRDGParameter final
{
public :
FRDGParameter ( ) = default ;
bool IsResource ( ) const
{
2021-04-06 11:45:09 -04:00
return ! IsRenderTargetBindingSlots ( ) & & ! IsResourceAccessArray ( ) ;
2020-09-24 00:43:27 -04:00
}
bool IsSRV ( ) const
{
return MemberType = = UBMT_RDG_TEXTURE_SRV | | MemberType = = UBMT_RDG_BUFFER_SRV ;
}
bool IsUAV ( ) const
{
return MemberType = = UBMT_RDG_TEXTURE_UAV | | MemberType = = UBMT_RDG_BUFFER_UAV ;
}
bool IsView ( ) const
{
return IsSRV ( ) | | IsUAV ( ) ;
}
bool IsTexture ( ) const
{
return
MemberType = = UBMT_RDG_TEXTURE | |
2021-04-06 11:45:09 -04:00
MemberType = = UBMT_RDG_TEXTURE_ACCESS ;
2020-09-24 00:43:27 -04:00
}
bool IsTextureAccess ( ) const
{
return MemberType = = UBMT_RDG_TEXTURE_ACCESS ;
}
2021-04-02 20:35:50 -04:00
bool IsTextureAccessArray ( ) const
{
return MemberType = = UBMT_RDG_TEXTURE_ACCESS_ARRAY ;
}
2020-09-24 00:43:27 -04:00
bool IsBuffer ( ) const
{
2021-04-06 11:45:09 -04:00
return MemberType = = UBMT_RDG_BUFFER_ACCESS ;
2020-09-24 00:43:27 -04:00
}
bool IsBufferAccess ( ) const
{
return MemberType = = UBMT_RDG_BUFFER_ACCESS ;
}
2021-04-02 20:35:50 -04:00
bool IsBufferAccessArray ( ) const
{
return MemberType = = UBMT_RDG_BUFFER_ACCESS_ARRAY ;
}
2021-04-06 11:45:09 -04:00
bool IsResourceAccessArray ( ) const
{
return IsBufferAccessArray ( ) | | IsTextureAccessArray ( ) ;
}
2020-09-24 00:43:27 -04:00
bool IsUniformBuffer ( ) const
{
return MemberType = = UBMT_RDG_UNIFORM_BUFFER ;
}
2022-03-25 11:19:10 -04:00
UE_DEPRECATED ( 5.1 , " Use IsViewableResource instead. " )
2020-09-24 00:43:27 -04:00
bool IsParentResource ( ) const
{
return IsTexture ( ) | | IsBuffer ( ) ;
}
2022-03-25 11:19:10 -04:00
bool IsViewableResource ( ) const
{
return IsTexture ( ) | | IsBuffer ( ) ;
}
2020-09-24 00:43:27 -04:00
bool IsRenderTargetBindingSlots ( ) const
{
return MemberType = = UBMT_RENDER_TARGET_BINDING_SLOTS ;
}
EUniformBufferBaseType GetType ( ) const
{
return MemberType ;
}
FRDGResourceRef GetAsResource ( ) const
{
check ( IsResource ( ) ) ;
return * GetAs < FRDGResourceRef > ( ) ;
}
2020-12-07 17:42:32 -04:00
FRDGUniformBufferBinding GetAsUniformBuffer ( ) const
2020-09-24 00:43:27 -04:00
{
check ( IsUniformBuffer ( ) ) ;
2020-12-07 17:42:32 -04:00
return * GetAs < FRDGUniformBufferBinding > ( ) ;
2020-09-24 00:43:27 -04:00
}
2022-03-25 11:19:10 -04:00
UE_DEPRECATED ( 5.1 , " Use IsViewableResource instead. " )
FRDGViewableResource * GetAsParentResource ( ) const
2020-09-24 00:43:27 -04:00
{
2022-03-25 11:19:10 -04:00
check ( IsViewableResource ( ) ) ;
return * GetAs < FRDGViewableResource * > ( ) ;
}
FRDGViewableResource * GetAsViewableResource ( ) const
{
check ( IsViewableResource ( ) ) ;
return * GetAs < FRDGViewableResource * > ( ) ;
2020-09-24 00:43:27 -04:00
}
FRDGViewRef GetAsView ( ) const
{
check ( IsView ( ) ) ;
return * GetAs < FRDGViewRef > ( ) ;
}
FRDGShaderResourceViewRef GetAsSRV ( ) const
{
check ( IsSRV ( ) ) ;
return * GetAs < FRDGShaderResourceViewRef > ( ) ;
}
FRDGUnorderedAccessViewRef GetAsUAV ( ) const
{
check ( IsUAV ( ) ) ;
return * GetAs < FRDGUnorderedAccessViewRef > ( ) ;
}
FRDGTextureRef GetAsTexture ( ) const
{
check ( IsTexture ( ) ) ;
return * GetAs < FRDGTextureRef > ( ) ;
}
FRDGTextureAccess GetAsTextureAccess ( ) const
{
check ( MemberType = = UBMT_RDG_TEXTURE_ACCESS ) ;
return * GetAs < FRDGTextureAccess > ( ) ;
}
2021-04-02 20:35:50 -04:00
const FRDGTextureAccessArray & GetAsTextureAccessArray ( ) const
{
check ( MemberType = = UBMT_RDG_TEXTURE_ACCESS_ARRAY ) ;
return * GetAs < FRDGTextureAccessArray > ( ) ;
}
2020-09-24 00:43:27 -04:00
FRDGBufferRef GetAsBuffer ( ) const
{
check ( IsBuffer ( ) ) ;
return * GetAs < FRDGBufferRef > ( ) ;
}
FRDGBufferAccess GetAsBufferAccess ( ) const
{
check ( MemberType = = UBMT_RDG_BUFFER_ACCESS ) ;
return * GetAs < FRDGBufferAccess > ( ) ;
}
2021-04-02 20:35:50 -04:00
const FRDGBufferAccessArray & GetAsBufferAccessArray ( ) const
{
check ( MemberType = = UBMT_RDG_BUFFER_ACCESS_ARRAY ) ;
return * GetAs < FRDGBufferAccessArray > ( ) ;
}
2020-09-24 00:43:27 -04:00
FRDGTextureSRVRef GetAsTextureSRV ( ) const
{
check ( MemberType = = UBMT_RDG_TEXTURE_SRV ) ;
return * GetAs < FRDGTextureSRVRef > ( ) ;
}
FRDGBufferSRVRef GetAsBufferSRV ( ) const
{
check ( MemberType = = UBMT_RDG_BUFFER_SRV ) ;
return * GetAs < FRDGBufferSRVRef > ( ) ;
}
FRDGTextureUAVRef GetAsTextureUAV ( ) const
{
check ( MemberType = = UBMT_RDG_TEXTURE_UAV ) ;
return * GetAs < FRDGTextureUAVRef > ( ) ;
}
FRDGBufferUAVRef GetAsBufferUAV ( ) const
{
check ( MemberType = = UBMT_RDG_BUFFER_UAV ) ;
return * GetAs < FRDGBufferUAVRef > ( ) ;
}
const FRenderTargetBindingSlots & GetAsRenderTargetBindingSlots ( ) const
{
check ( IsRenderTargetBindingSlots ( ) ) ;
return * GetAs < FRenderTargetBindingSlots > ( ) ;
}
private :
FRDGParameter ( EUniformBufferBaseType InMemberType , void * InMemberPtr )
: MemberType ( InMemberType )
, MemberPtr ( InMemberPtr )
{ }
template < typename T >
T * GetAs ( ) const
{
return reinterpret_cast < T * > ( MemberPtr ) ;
}
const EUniformBufferBaseType MemberType = UBMT_INVALID ;
void * const MemberPtr = nullptr ;
friend class FRDGParameterStruct ;
} ;
/** Wraps a pass parameter struct payload and provides helpers for traversing members. */
class RENDERCORE_API FRDGParameterStruct
{
public :
template < typename ParameterStructType >
2021-12-03 16:04:00 -05:00
explicit FRDGParameterStruct ( const ParameterStructType * Parameters , const FShaderParametersMetadata * InParameterMetadata )
: Contents ( reinterpret_cast < const uint8 * > ( Parameters ) )
, Layout ( InParameterMetadata - > GetLayoutPtr ( ) )
, Metadata ( InParameterMetadata )
2020-09-24 00:43:27 -04:00
{ }
explicit FRDGParameterStruct ( const void * InContents , const FRHIUniformBufferLayout * InLayout )
: Contents ( reinterpret_cast < const uint8 * > ( InContents ) )
, Layout ( InLayout )
{
checkf ( Contents & & Layout , TEXT ( " Pass parameter struct created with null inputs. " ) ) ;
}
/** Returns the contents of the struct. */
const uint8 * GetContents ( ) const { return Contents ; }
/** Returns the layout associated with this struct. */
const FRHIUniformBufferLayout & GetLayout ( ) const { return * Layout ; }
2021-08-19 23:56:04 -04:00
const FRHIUniformBufferLayout * GetLayoutPtr ( ) const { return Layout ; }
2020-09-24 00:43:27 -04:00
2021-12-03 16:04:00 -05:00
const FShaderParametersMetadata * GetMetadata ( ) const { return Metadata ; }
2020-09-24 00:43:27 -04:00
/** Helpful forwards from the layout. */
FORCEINLINE bool HasRenderTargets ( ) const { return Layout - > HasRenderTargets ( ) ; }
FORCEINLINE bool HasExternalOutputs ( ) const { return Layout - > HasExternalOutputs ( ) ; }
FORCEINLINE bool HasStaticSlot ( ) const { return Layout - > HasStaticSlot ( ) ; }
/** Returns the number of buffer parameters present on the layout. */
uint32 GetBufferParameterCount ( ) const { return Layout - > GraphBuffers . Num ( ) ; }
/** Returns the number of texture parameters present on the layout. */
uint32 GetTextureParameterCount ( ) const { return Layout - > GraphTextures . Num ( ) ; }
2021-05-05 11:58:15 -04:00
/** Returns the number of RDG uniform buffers present in the layout. */
uint32 GetUniformBufferParameterCount ( ) const { return Layout - > GraphUniformBuffers . Num ( ) ; }
2020-09-24 00:43:27 -04:00
/** Returns the render target binding slots. Asserts if they don't exist. */
const FRenderTargetBindingSlots & GetRenderTargets ( ) const
{
check ( HasRenderTargets ( ) ) ;
return * reinterpret_cast < const FRenderTargetBindingSlots * > ( Contents + Layout - > RenderTargetsOffset ) ;
}
/** Enumerates all graph parameters on the layout. Graph uniform buffers are traversed recursively but are
* also included in the enumeration .
* Expected function signature : void ( FRDGParameter ) .
*/
template < typename FunctionType >
void Enumerate ( FunctionType Function ) const ;
/** Same as Enumerate, but only texture parameters are included. */
template < typename FunctionType >
void EnumerateTextures ( FunctionType Function ) const ;
/** Same as Enumerate, but only buffer parameters are included. */
template < typename FunctionType >
void EnumerateBuffers ( FunctionType Function ) const ;
2020-12-07 17:42:32 -04:00
/** Enumerates all non-null uniform buffers. Expected function signature: void(FRDGUniformBufferBinding). */
2020-09-24 00:43:27 -04:00
template < typename FunctionType >
void EnumerateUniformBuffers ( FunctionType Function ) const ;
2021-01-13 16:51:48 -04:00
/** Returns a set of static uniform buffer bindings for the parameter struct. */
FUniformBufferStaticBindings GetStaticUniformBuffers ( ) const ;
2020-09-24 00:43:27 -04:00
/** Returns the render pass info generated from the render target binding slots. */
FRHIRenderPassInfo GetRenderPassInfo ( ) const ;
private :
2021-08-19 23:56:04 -04:00
FRDGParameter GetParameterInternal ( TArrayView < const FRHIUniformBufferResource > Parameters , uint32 ParameterIndex ) const
2020-09-24 00:43:27 -04:00
{
checkf ( ParameterIndex < static_cast < uint32 > ( Parameters . Num ( ) ) , TEXT ( " Attempted to access RDG pass parameter outside of index for Layout '%s' " ) , * Layout - > GetDebugName ( ) ) ;
const EUniformBufferBaseType MemberType = Parameters [ ParameterIndex ] . MemberType ;
const uint16 MemberOffset = Parameters [ ParameterIndex ] . MemberOffset ;
return FRDGParameter ( MemberType , const_cast < uint8 * > ( Contents + MemberOffset ) ) ;
}
const uint8 * Contents ;
2021-08-19 23:56:04 -04:00
FUniformBufferLayoutRHIRef Layout ;
2021-12-03 16:04:00 -05:00
const FShaderParametersMetadata * Metadata = nullptr ;
2020-09-24 00:43:27 -04:00
friend class FRDGPass ;
} ;
template < typename ParameterStructType >
class TRDGParameterStruct
: public FRDGParameterStruct
{
public :
explicit TRDGParameterStruct ( ParameterStructType * Parameters )
: FRDGParameterStruct ( Parameters , & ParameterStructType : : FTypeInfo : : GetStructMetadata ( ) - > GetLayout ( ) )
{ }
/** Returns the contents of the struct. */
const ParameterStructType * GetContents ( ) const
{
return reinterpret_cast < const ParameterStructType * > ( FRDGParameterStruct : : GetContents ( ) ) ;
}
const ParameterStructType * operator - > ( ) const
{
return GetContents ( ) ;
}
} ;
/** Helper function to get RHI render pass info from a pass parameter struct. Must be called
* within an RDG pass with the pass parameters ; otherwise , the RHI access checks will assert .
* This helper is useful when you want to control the mechanics of render passes within an
* RDG raster pass by specifying ' SkipRenderPass ' .
*/
template < typename TParameterStruct >
FORCEINLINE static FRHIRenderPassInfo GetRenderPassInfo ( TParameterStruct * Parameters )
{
2021-12-03 16:04:00 -05:00
return FRDGParameterStruct ( Parameters , TParameterStruct : : FTypeInfo : : GetStructMetadata ( ) ) . GetRenderPassInfo ( ) ;
2020-09-24 00:43:27 -04:00
}
/** Helper function to get RHI global uniform buffers out of a pass parameters struct. */
template < typename TParameterStruct >
2021-01-13 16:51:48 -04:00
FORCEINLINE static FUniformBufferStaticBindings GetStaticUniformBuffers ( TParameterStruct * Parameters )
2020-09-24 00:43:27 -04:00
{
2021-12-03 16:04:00 -05:00
return FRDGParameterStruct ( Parameters , TParameterStruct : : FTypeInfo : : GetStructMetadata ( ) ) . GetStaticUniformBuffers ( ) ;
2020-09-24 00:43:27 -04:00
}