Guest User

Untitled

a guest
Feb 21st, 2018
162
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. #include "GlobalShader.h"
  4. #include "RHICommandList.h"
  5.  
  6. class FBitonicMergeSortBuffers
  7. {
  8. public:
  9.     int32 Count;
  10.     FRWBufferStructured KeysBuffer;
  11.     FRWBufferStructured ValuesBuffer;
  12.    
  13.     void Initialize()
  14.     {
  15.         if(Count <= 0)
  16.             return;
  17.        
  18.         KeysBuffer.Initialize(sizeof(uint32), Count, BUF_Static, TEXT("FBitonicMergeSortShaderParameters::Keys"));
  19.         ValuesBuffer.Initialize(sizeof(float), Count, BUF_Static, TEXT("FBitonicMergeSortShaderParameters::Values"));
  20.     }
  21.    
  22.     void AcquireTransientResource()
  23.     {
  24.         KeysBuffer.AcquireTransientResource();
  25.         ValuesBuffer.AcquireTransientResource();
  26.     }
  27.    
  28.     void DiscardTransientResource()
  29.     {
  30.         KeysBuffer.DiscardTransientResource();
  31.         ValuesBuffer.DiscardTransientResource();
  32.     }
  33.    
  34.     void Release()
  35.     {
  36.         KeysBuffer.Release();
  37.         ValuesBuffer.Release();
  38.     }
  39.    
  40.     size_t GetSizeBytes() const
  41.     {
  42.         return KeysBuffer.NumBytes + ValuesBuffer.NumBytes;
  43.     }
  44. };
  45.  
  46. class FBitonicMergeSortBufferResource
  47.     : public FRenderResource
  48. {
  49. public:
  50.     FBitonicMergeSortBuffers Buffers;
  51.    
  52.     virtual void InitDynamicRHI()  override
  53.     {
  54.         Buffers.Initialize();
  55.     }
  56.  
  57.     virtual void ReleaseDynamicRHI() override
  58.     {
  59.         Buffers.Release();
  60.     }
  61. };
  62.  
  63. class FBitonicMergeSortBufferParameters
  64. {
  65. public:
  66.     void Bind(const FShaderParameterMap& InParameterMap)
  67.     {
  68.         KeysBuffer.Bind(InParameterMap, TEXT("Keys"));
  69.         ValuesBuffer.Bind(InParameterMap, TEXT("Values"));
  70.     }
  71.    
  72.     template<typename TParamRef, typename TRHICommandList>
  73.     void Set(TRHICommandList& InRHICommandList, const TParamRef& InShaderRHI, const FBitonicMergeSortBuffers& InBuffers)
  74.     {
  75.         KeysBuffer.SetBuffer(InRHICommandList, InShaderRHI, InBuffers.KeysBuffer);
  76.        
  77.         SetSRVParameter(InRHICommandList, InShaderRHI, ValuesBuffer, InBuffers.ValuesBuffer);
  78.     }
  79.    
  80.     template<typename TParamRef, typename TRHICommandList>
  81.     void UnsetParameters(TRHICommandList& InRHICommandList, const TParamRef& InShaderRHI)
  82.     {
  83.         KeysBuffer.UnsetUAV(InRHICommandList, InShaderRHI);
  84.     }
  85.    
  86.     void GetUAVs(const FBitonicMergeSortBuffers& InBuffers, TArray<FUnorderedAccessViewRHIParamRef>& OutUAVs)
  87.     {
  88.         uint32 MaxIndex = 0;
  89.         MaxIndex = FMath::Max(MaxIndex, KeysBuffer.GetUAVIndex());
  90.  
  91.         OutUAVs.AddZeroed(MaxIndex + 1);
  92.  
  93.         if (KeysBuffer.IsUAVBound())
  94.             OutUAVs[KeysBuffer.GetUAVIndex()] = InBuffers.KeysBuffer.UAV;
  95.        
  96.         check(OutUAVs.Num() > 0);
  97.     }
  98.  
  99.     friend FArchive& operator<<(FArchive& InArchive, FBitonicMergeSortBufferParameters& InParameters)
  100.     {
  101.         InArchive << InParameters.KeysBuffer << InParameters.ValuesBuffer;
  102.         return InArchive;
  103.     }
  104.    
  105. private:
  106.     FRWShaderParameter KeysBuffer;
  107.     FShaderResourceParameter ValuesBuffer;
  108. };
  109.  
  110. class FBitonicMergeSortShaderDeclaration
  111.     : public FGlobalShader
  112. {
  113.     DECLARE_SHADER_TYPE(FBitonicMergeSortShaderDeclaration, Global);
  114.    
  115. public:
  116.     FBitonicMergeSortShaderDeclaration() {}
  117.     FBitonicMergeSortShaderDeclaration(const ShaderMetaType::CompiledShaderInitializerType& InInitializer);
  118.    
  119.     static bool ShouldCache(EShaderPlatform InPlatform) { return IsFeatureLevelSupported(InPlatform, ERHIFeatureLevel::SM5); }
  120.     static void ModifyCompilationEnvironment(EShaderPlatform InPlatform, FShaderCompilerEnvironment& OutEnvironment);
  121.    
  122.     void SetParameters(FRHICommandList& InRHICommandList, int32 InCount, TArray<uint32>& InKeys, TArray<float>& InValues);
  123.     void UnsetParameters(FRHICommandList& InRHICommandList);
  124.  
  125. private:
  126.     FBitonicMergeSortBufferParameters Parameters;
  127. };
RAW Paste Data