Zgragselus

CommandSignature.h

Jan 24th, 2026
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.23 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // CommandSignature.h
  4. //
  5. // Defines the command signature for indirect rendering. Indirect rendering requires command
  6. // signature to interpret what are the parameters in single record of indirect buffer.
  7. //
  8. // This is done through indirect parameters which define type of each parameter (field) in the
  9. // buffer.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////////////////////////
  12.  
  13. #ifndef __COMMAND_SIGNATURE__H__
  14. #define __COMMAND_SIGNATURE__H__
  15.  
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Header section
  18.  
  19. #include <d3d12.h>
  20.  
  21. ///////////////////////////////////////////////////////////////////////////////////////////////////
  22. // Class & Structures definition
  23.  
  24. namespace Engine
  25. {
  26.     class D3DRenderer;
  27.     class RootSignature;
  28.  
  29.     /// <summary>
  30.     /// Defines single indirect parameter in the command signature for indirect rendering.
  31.     ///
  32.     /// There is a special requirement - all indirect parameters with any root parameter index have to
  33.     /// be placed in increasing order of root parameter index.
  34.     /// </summary>
  35.     class IndirectParameter
  36.     {
  37.         friend class CommandSignature;
  38.  
  39.     protected:
  40.         /// <summary>
  41.         /// Direct3D 12 indirect argument description
  42.         /// </summary>
  43.         D3D12_INDIRECT_ARGUMENT_DESC mIndirectParam;
  44.  
  45.     public:
  46.         /// <summary>
  47.         /// Default constructor. Set type to invalid.
  48.         /// </summary>
  49.         IndirectParameter()
  50.         {
  51.             mIndirectParam.Type = (D3D12_INDIRECT_ARGUMENT_TYPE)0xFFFFFFFF;
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Draw command indirect parameter
  56.         /// </summary>
  57.         void Draw()
  58.         {
  59.             mIndirectParam.Type = D3D12_INDIRECT_ARGUMENT_TYPE_DRAW;
  60.         }
  61.  
  62.         /// <summary>
  63.         /// Draw indexed command indirect parameter
  64.         /// </summary>
  65.         void DrawIndexed()
  66.         {
  67.             mIndirectParam.Type = D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED;
  68.         }
  69.  
  70.         /// <summary>
  71.         /// Dispatch kernel command indirect parameter
  72.         /// </summary>
  73.         void Dispatch()
  74.         {
  75.             mIndirectParam.Type = D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH;
  76.         }
  77.  
  78.         /// <summary>
  79.         /// Vertex buffer view indirect parameter
  80.         /// </summary>
  81.         void VertexBufferView()
  82.         {
  83.             mIndirectParam.Type = D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW;
  84.             mIndirectParam.VertexBuffer.Slot = 0;
  85.         }
  86.  
  87.         /// <summary>
  88.         /// Index buffer view indirect parameter
  89.         /// </summary>
  90.         void IndexBufferView()
  91.         {
  92.             mIndirectParam.Type = D3D12_INDIRECT_ARGUMENT_TYPE_INDEX_BUFFER_VIEW;
  93.         }
  94.  
  95.         /// <summary>
  96.         /// Create constant indirect parameter type
  97.         /// </summary>
  98.         /// <param name="rootParameterIndex">Index of root parameter in root signature</param>
  99.         /// <param name="destOffset">Offset in 32 bit values (in root parameter)</param>
  100.         /// <param name="numValues">Number of 32 bit values set (in root parameter)</param>
  101.         void Constant(unsigned int rootParameterIndex, unsigned int destOffset, unsigned int numValues)
  102.         {
  103.             mIndirectParam.Type = D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT;
  104.             mIndirectParam.Constant.RootParameterIndex = rootParameterIndex;
  105.             mIndirectParam.Constant.DestOffsetIn32BitValues = destOffset;
  106.             mIndirectParam.Constant.Num32BitValuesToSet = numValues;
  107.         }
  108.  
  109.         /// <summary>
  110.         /// Constant buffer view indirect parameter
  111.         /// </summary>
  112.         /// <param name="rootParameterIndex">Root signature parameter index for given constant buffer view</param>
  113.         void ConstantBufferView(unsigned int rootParameterIndex)
  114.         {
  115.             mIndirectParam.Type = D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT_BUFFER_VIEW;
  116.             mIndirectParam.ConstantBufferView.RootParameterIndex = rootParameterIndex;
  117.         }
  118.  
  119.         /// <summary>
  120.         /// Shader resource view indirect parameter
  121.         /// </summary>
  122.         /// <param name="rootParameterIndex">Root signature parameter index for given shader resource view</param>
  123.         void ShaderResourceView(unsigned int rootParameterIndex)
  124.         {
  125.             mIndirectParam.Type = D3D12_INDIRECT_ARGUMENT_TYPE_SHADER_RESOURCE_VIEW;
  126.             mIndirectParam.ShaderResourceView.RootParameterIndex = rootParameterIndex;
  127.         }
  128.        
  129.         /// <summary>
  130.         /// Unordered access view indirect parameter
  131.         /// </summary>
  132.         /// <param name="rootParameterIndex">Root signature parameter index for given unordered access view</param>
  133.         void UnorderedAccessView(unsigned int rootParameterIndex)
  134.         {
  135.             mIndirectParam.Type = D3D12_INDIRECT_ARGUMENT_TYPE_UNORDERED_ACCESS_VIEW;
  136.             mIndirectParam.UnorderedAccessView.RootParameterIndex = rootParameterIndex;
  137.         }
  138.  
  139.         /// <summary>
  140.         /// Dispatch mesh command indirect parameter
  141.         /// </summary>
  142.         void DispatchMesh()
  143.         {
  144.             mIndirectParam.Type = D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH_MESH;
  145.         }
  146.  
  147.         /// <summary>
  148.         /// Get D3D12 indirect argument description for this indirect parameter
  149.         /// </summary>
  150.         /// <returns>Direct3D 12 indirect argument description</returns>
  151.         const D3D12_INDIRECT_ARGUMENT_DESC& Get() const
  152.         {
  153.             return mIndirectParam;
  154.         }
  155.     };
  156.  
  157.     /// <summary>
  158.     /// Command signature
  159.     ///
  160.     /// Defines the command signature for indirect rendering. Holds N indirect parameters defining
  161.     /// what each record of indirect buffer contains.
  162.     /// </summary>
  163.     class CommandSignature
  164.     {
  165.     protected:
  166.         /// <summary>
  167.         /// Is the command signature finalized?
  168.         /// </summary>
  169.         bool mFinalized;
  170.  
  171.         /// <summary>
  172.         /// Number of indirect parameters in the command signature
  173.         /// </summary>
  174.         unsigned int mNumParameters;
  175.  
  176.         /// <summary>
  177.         /// Pointer to buffer of indirect parameters
  178.         /// </summary>
  179.         IndirectParameter* mParameters;
  180.  
  181.         /// <summary>
  182.         /// Direct3D 12 command signature
  183.         /// </summary>
  184.         ID3D12CommandSignature* mSignature;
  185.  
  186.     public:
  187.         /// <summary>
  188.         /// Default constructor
  189.         /// </summary>
  190.         /// <param name="numParams">Number of indirect parameters in command signature</param>
  191.         CommandSignature(unsigned int numParams = 0)
  192.         {
  193.             mFinalized = false;
  194.             mNumParameters = numParams;
  195.  
  196.             Reset(mNumParameters);
  197.         }
  198.  
  199.         /// <summary>
  200.         /// Default destructor
  201.         /// </summary>
  202.         ~CommandSignature()
  203.         {
  204.             if (mSignature != nullptr)
  205.             {
  206.                 mSignature->Release();
  207.                 mSignature = nullptr;
  208.             }
  209.  
  210.             delete[] mParameters;
  211.             mParameters = nullptr;
  212.         }
  213.  
  214.         /// <summary>
  215.         /// Reset the command signature
  216.         /// </summary>
  217.         /// <param name="numParams">New number of parameters in command signature</param>
  218.         void Reset(unsigned int numParams)
  219.         {
  220.             if (mNumParameters > 0)
  221.             {
  222.                 mParameters = new IndirectParameter[mNumParameters];
  223.             }
  224.             else
  225.             {
  226.                 mParameters = nullptr;
  227.             }
  228.  
  229.             mNumParameters = numParams;
  230.         }
  231.  
  232.         /// <summary>
  233.         /// Get indirect parameter by index
  234.         /// </summary>
  235.         /// <param name="index">Index of indirect parameter</param>
  236.         /// <returns>Reference to indirect parameter at given index</returns>
  237.         IndirectParameter& operator[] (size_t index)
  238.         {
  239.             return mParameters[index];
  240.         }
  241.  
  242.         /// <summary>
  243.         /// Get indirect parameter by index (read-only)
  244.         /// </summary>
  245.         /// <param name="index">Index of indirect parameter</param>
  246.         /// <returns>Const reference to indirect parameter at given index</returns>
  247.         const IndirectParameter& operator[] (size_t index) const
  248.         {
  249.             return mParameters[index];
  250.         }
  251.  
  252.         /// <summary>
  253.         /// Initialize Direct3D 12 command signature object
  254.         /// </summary>
  255.         /// <param name="device">Direct3D 12 device</param>
  256.         /// <param name="stride">Record stride</param>
  257.         /// <param name="rootSignature">Root signature for this command signature</param>
  258.         void Init(ID3D12Device* device, size_t stride, RootSignature* rootSignature = nullptr);
  259.  
  260.         /// <summary>
  261.         /// Initialize Direct3D 12 command signature object
  262.         /// </summary>
  263.         /// <param name="renderer">Direct3D 12 renderer</param>
  264.         /// <param name="stride">Record stride</param>
  265.         /// <param name="rootSignature">Root signature for this command signature</param>
  266.         void Init(D3DRenderer* renderer, size_t stride, RootSignature* rootSignature = nullptr);
  267.  
  268.         /// <summary>
  269.         /// Get Direct3D 12 command signature
  270.         /// </summary>
  271.         /// <returns>Direct3D 12 command signature</returns>
  272.         ID3D12CommandSignature* Get()
  273.         {
  274.             return mSignature;
  275.         }
  276.     };
  277. }
  278.  
  279. ///////////////////////////////////////////////////////////////////////////////////////////////////
  280. // EOH
  281.  
  282. #endif
Advertisement
Add Comment
Please, Sign In to add comment