duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

d3d12_builders.h (3985B)


      1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
      2 // SPDX-License-Identifier: (GPL-3.0 OR PolyForm-Strict-1.0.0)
      3 
      4 #pragma once
      5 
      6 #include "common/types.h"
      7 #include "common/windows_headers.h"
      8 
      9 #include <array>
     10 #include <d3d12.h>
     11 #include <string_view>
     12 #include <wrl/client.h>
     13 
     14 class Error;
     15 
     16 namespace D3D12 {
     17 class RootSignatureBuilder
     18 {
     19 public:
     20   enum : u32
     21   {
     22     MAX_PARAMETERS = 16,
     23     MAX_DESCRIPTOR_RANGES = 16
     24   };
     25 
     26   RootSignatureBuilder();
     27 
     28   void Clear();
     29 
     30   Microsoft::WRL::ComPtr<ID3D12RootSignature> Create(Error* error, bool clear);
     31 
     32   void SetInputAssemblerFlag();
     33 
     34   u32 Add32BitConstants(u32 shader_reg, u32 num_values, D3D12_SHADER_VISIBILITY visibility);
     35   u32 AddCBVParameter(u32 shader_reg, D3D12_SHADER_VISIBILITY visibility);
     36   u32 AddSRVParameter(u32 shader_reg, D3D12_SHADER_VISIBILITY visibility);
     37   u32 AddDescriptorTable(D3D12_DESCRIPTOR_RANGE_TYPE rt, u32 start_shader_reg, u32 num_shader_regs,
     38                          D3D12_SHADER_VISIBILITY visibility);
     39 
     40 private:
     41   D3D12_ROOT_SIGNATURE_DESC m_desc{};
     42   std::array<D3D12_ROOT_PARAMETER, MAX_PARAMETERS> m_params{};
     43   std::array<D3D12_DESCRIPTOR_RANGE, MAX_DESCRIPTOR_RANGES> m_descriptor_ranges{};
     44   u32 m_num_descriptor_ranges = 0;
     45 };
     46 
     47 class GraphicsPipelineBuilder
     48 {
     49 public:
     50   enum : u32
     51   {
     52     MAX_VERTEX_ATTRIBUTES = 16,
     53   };
     54 
     55   GraphicsPipelineBuilder();
     56 
     57   ~GraphicsPipelineBuilder() = default;
     58 
     59   ALWAYS_INLINE const D3D12_GRAPHICS_PIPELINE_STATE_DESC* GetDesc() const { return &m_desc; }
     60 
     61   void Clear();
     62 
     63   Microsoft::WRL::ComPtr<ID3D12PipelineState> Create(ID3D12Device* device, Error* error, bool clear);
     64 
     65   void SetRootSignature(ID3D12RootSignature* rs);
     66 
     67   void SetVertexShader(const void* data, u32 data_size);
     68   void SetGeometryShader(const void* data, u32 data_size);
     69   void SetPixelShader(const void* data, u32 data_size);
     70 
     71   void SetVertexShader(const ID3DBlob* blob);
     72   void SetGeometryShader(const ID3DBlob* blob);
     73   void SetPixelShader(const ID3DBlob* blob);
     74 
     75   void AddVertexAttribute(const char* semantic_name, u32 semantic_index, DXGI_FORMAT format, u32 buffer, u32 offset);
     76 
     77   void SetPrimitiveTopologyType(D3D12_PRIMITIVE_TOPOLOGY_TYPE type);
     78 
     79   void SetRasterizationState(D3D12_FILL_MODE polygon_mode, D3D12_CULL_MODE cull_mode, bool front_face_ccw);
     80 
     81   void SetMultisamples(u32 multisamples);
     82 
     83   void SetNoCullRasterizationState();
     84 
     85   void SetDepthState(bool depth_test, bool depth_write, D3D12_COMPARISON_FUNC compare_op);
     86   void SetStencilState(bool stencil_test, u8 read_mask, u8 write_mask, const D3D12_DEPTH_STENCILOP_DESC& front,
     87                        const D3D12_DEPTH_STENCILOP_DESC& back);
     88 
     89   void SetNoDepthTestState();
     90   void SetNoStencilState();
     91 
     92   void SetBlendState(u32 rt, bool blend_enable, D3D12_BLEND src_factor, D3D12_BLEND dst_factor, D3D12_BLEND_OP op,
     93                      D3D12_BLEND alpha_src_factor, D3D12_BLEND alpha_dst_factor, D3D12_BLEND_OP alpha_op,
     94                      u8 write_mask = D3D12_COLOR_WRITE_ENABLE_ALL);
     95   void SetColorWriteMask(u32 rt, u8 write_mask = D3D12_COLOR_WRITE_ENABLE_ALL);
     96 
     97   void SetNoBlendingState();
     98 
     99   void ClearRenderTargets();
    100 
    101   void SetRenderTarget(u32 rt, DXGI_FORMAT format);
    102 
    103   void ClearDepthStencilFormat();
    104 
    105   void SetDepthStencilFormat(DXGI_FORMAT format);
    106 
    107 private:
    108   D3D12_GRAPHICS_PIPELINE_STATE_DESC m_desc;
    109   std::array<D3D12_INPUT_ELEMENT_DESC, MAX_VERTEX_ATTRIBUTES> m_input_elements;
    110 };
    111 
    112 class ComputePipelineBuilder
    113 {
    114 public:
    115   ComputePipelineBuilder();
    116   ~ComputePipelineBuilder() = default;
    117 
    118   void Clear();
    119 
    120   Microsoft::WRL::ComPtr<ID3D12PipelineState> Create(ID3D12Device* device, Error* error, bool clear);
    121 
    122   void SetRootSignature(ID3D12RootSignature* rs);
    123 
    124   void SetShader(const void* data, u32 data_size);
    125 
    126 private:
    127   D3D12_COMPUTE_PIPELINE_STATE_DESC m_desc;
    128 };
    129 
    130 #ifdef _DEBUG
    131 void SetObjectName(ID3D12Object* object, std::string_view name);
    132 #else
    133 static inline void SetObjectName(ID3D12Object* object, std::string_view name)
    134 {
    135 }
    136 #endif
    137 } // namespace D3D12