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

d3d11_texture.h (5163B)


      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 "d3d11_stream_buffer.h"
      7 #include "gpu_device.h"
      8 
      9 #include "common/windows_headers.h"
     10 
     11 #include <d3d11_1.h>
     12 #include <wrl/client.h>
     13 
     14 class D3D11Device;
     15 
     16 class D3D11Sampler final : public GPUSampler
     17 {
     18   friend D3D11Device;
     19 
     20   template<typename T>
     21   using ComPtr = Microsoft::WRL::ComPtr<T>;
     22 
     23 public:
     24   ~D3D11Sampler() override;
     25 
     26   ALWAYS_INLINE ID3D11SamplerState* GetSamplerState() const { return m_ss.Get(); }
     27   ALWAYS_INLINE ID3D11SamplerState* const* GetSamplerStateArray() const { return m_ss.GetAddressOf(); }
     28 
     29   void SetDebugName(std::string_view name) override;
     30 
     31 private:
     32   D3D11Sampler(ComPtr<ID3D11SamplerState> ss);
     33 
     34   ComPtr<ID3D11SamplerState> m_ss;
     35 };
     36 
     37 class D3D11Texture final : public GPUTexture
     38 {
     39   friend D3D11Device;
     40 
     41 public:
     42   template<typename T>
     43   using ComPtr = Microsoft::WRL::ComPtr<T>;
     44 
     45   ~D3D11Texture();
     46 
     47   ALWAYS_INLINE ID3D11Texture2D* GetD3DTexture() const { return m_texture.Get(); }
     48   ALWAYS_INLINE ID3D11ShaderResourceView* GetD3DSRV() const { return m_srv.Get(); }
     49   ALWAYS_INLINE ID3D11View* GetRTVOrDSV() const { return m_rtv_dsv.Get(); }
     50   ALWAYS_INLINE ID3D11RenderTargetView* GetD3DRTV() const
     51   {
     52     return static_cast<ID3D11RenderTargetView*>(m_rtv_dsv.Get());
     53   }
     54   ALWAYS_INLINE ID3D11DepthStencilView* GetD3DDSV() const
     55   {
     56     return static_cast<ID3D11DepthStencilView*>(m_rtv_dsv.Get());
     57   }
     58   ALWAYS_INLINE ID3D11ShaderResourceView* const* GetD3DSRVArray() const { return m_srv.GetAddressOf(); }
     59   ALWAYS_INLINE ID3D11RenderTargetView* const* GetD3DRTVArray() const
     60   {
     61     return reinterpret_cast<ID3D11RenderTargetView* const*>(m_rtv_dsv.GetAddressOf());
     62   }
     63   ALWAYS_INLINE ID3D11UnorderedAccessView* GetD3DUAV() const { return m_uav.Get(); }
     64   DXGI_FORMAT GetDXGIFormat() const;
     65 
     66   ALWAYS_INLINE operator ID3D11Texture2D*() const { return m_texture.Get(); }
     67   ALWAYS_INLINE operator ID3D11ShaderResourceView*() const { return m_srv.Get(); }
     68   ALWAYS_INLINE operator ID3D11RenderTargetView*() const
     69   {
     70     return static_cast<ID3D11RenderTargetView*>(m_rtv_dsv.Get());
     71   }
     72   ALWAYS_INLINE operator ID3D11DepthStencilView*() const
     73   {
     74     return static_cast<ID3D11DepthStencilView*>(m_rtv_dsv.Get());
     75   }
     76   ALWAYS_INLINE operator ID3D11UnorderedAccessView*() const { return m_uav.Get(); }
     77   ALWAYS_INLINE operator bool() const { return static_cast<bool>(m_texture); }
     78 
     79   static std::unique_ptr<D3D11Texture> Create(ID3D11Device* device, u32 width, u32 height, u32 layers, u32 levels,
     80                                               u32 samples, Type type, Format format, const void* initial_data = nullptr,
     81                                               u32 initial_data_stride = 0);
     82 
     83   D3D11_TEXTURE2D_DESC GetDesc() const;
     84   void CommitClear(ID3D11DeviceContext1* context);
     85 
     86   bool Update(u32 x, u32 y, u32 width, u32 height, const void* data, u32 pitch, u32 layer = 0, u32 level = 0) override;
     87   bool Map(void** map, u32* map_stride, u32 x, u32 y, u32 width, u32 height, u32 layer = 0, u32 level = 0) override;
     88   void Unmap() override;
     89 
     90   void SetDebugName(std::string_view name) override;
     91 
     92 private:
     93   D3D11Texture(u32 width, u32 height, u32 layers, u32 levels, u32 samples, Type type, Format format,
     94                ComPtr<ID3D11Texture2D> texture, ComPtr<ID3D11ShaderResourceView> srv, ComPtr<ID3D11View> rtv_dsv,
     95                ComPtr<ID3D11UnorderedAccessView> uav);
     96 
     97   ComPtr<ID3D11Texture2D> m_texture;
     98   ComPtr<ID3D11ShaderResourceView> m_srv;
     99   ComPtr<ID3D11View> m_rtv_dsv;
    100   ComPtr<ID3D11UnorderedAccessView> m_uav;
    101   u32 m_mapped_subresource = 0;
    102 };
    103 
    104 class D3D11TextureBuffer final : public GPUTextureBuffer
    105 {
    106 public:
    107   D3D11TextureBuffer(Format format, u32 size_in_elements);
    108   ~D3D11TextureBuffer() override;
    109 
    110   ALWAYS_INLINE ID3D11Buffer* GetBuffer() const { return m_buffer.GetD3DBuffer(); }
    111   ALWAYS_INLINE ID3D11ShaderResourceView* GetSRV() const { return m_srv.Get(); }
    112   ALWAYS_INLINE ID3D11ShaderResourceView* const* GetSRVArray() const { return m_srv.GetAddressOf(); }
    113 
    114   bool CreateBuffer();
    115 
    116   // Inherited via GPUTextureBuffer
    117   void* Map(u32 required_elements) override;
    118   void Unmap(u32 used_elements) override;
    119 
    120   void SetDebugName(std::string_view name) override;
    121 
    122 private:
    123   D3D11StreamBuffer m_buffer;
    124   Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_srv;
    125 };
    126 
    127 class D3D11DownloadTexture final : public GPUDownloadTexture
    128 {
    129 public:
    130   ~D3D11DownloadTexture() override;
    131 
    132   static std::unique_ptr<D3D11DownloadTexture> Create(u32 width, u32 height, GPUTexture::Format format);
    133 
    134   void CopyFromTexture(u32 dst_x, u32 dst_y, GPUTexture* src, u32 src_x, u32 src_y, u32 width, u32 height,
    135                        u32 src_layer, u32 src_level, bool use_transfer_pitch) override;
    136 
    137   bool Map(u32 x, u32 y, u32 width, u32 height) override;
    138   void Unmap() override;
    139 
    140   void Flush() override;
    141 
    142   void SetDebugName(std::string_view name) override;
    143 
    144 private:
    145   D3D11DownloadTexture(Microsoft::WRL::ComPtr<ID3D11Texture2D> tex, u32 width, u32 height, GPUTexture::Format format);
    146 
    147   Microsoft::WRL::ComPtr<ID3D11Texture2D> m_texture;
    148 };