DXTexture.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2017 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <d3d11_4.h>
  5. #include <memory>
  6. #include <string>
  7. #include <string_view>
  8. #include <vector>
  9. #include "Common/CommonTypes.h"
  10. #include "VideoBackends/D3D/D3DBase.h"
  11. #include "VideoCommon/AbstractFramebuffer.h"
  12. #include "VideoCommon/AbstractGfx.h"
  13. #include "VideoCommon/AbstractStagingTexture.h"
  14. #include "VideoCommon/AbstractTexture.h"
  15. namespace DX11
  16. {
  17. class DXTexture final : public AbstractTexture
  18. {
  19. public:
  20. ~DXTexture();
  21. static std::unique_ptr<DXTexture> Create(const TextureConfig& config, std::string_view name);
  22. static std::unique_ptr<DXTexture> CreateAdopted(ComPtr<ID3D11Texture2D> texture);
  23. void CopyRectangleFromTexture(const AbstractTexture* src,
  24. const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
  25. u32 src_level, const MathUtil::Rectangle<int>& dst_rect,
  26. u32 dst_layer, u32 dst_level) override;
  27. void ResolveFromTexture(const AbstractTexture* src, const MathUtil::Rectangle<int>& rect,
  28. u32 layer, u32 level) override;
  29. void Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer, size_t buffer_size,
  30. u32 layer) override;
  31. ID3D11Texture2D* GetD3DTexture() const { return m_texture.Get(); }
  32. ID3D11ShaderResourceView* GetD3DSRV() const { return m_srv.Get(); }
  33. ID3D11UnorderedAccessView* GetD3DUAV() const { return m_uav.Get(); }
  34. private:
  35. DXTexture(const TextureConfig& config, ComPtr<ID3D11Texture2D> texture, std::string_view name);
  36. bool CreateSRV();
  37. bool CreateUAV();
  38. ComPtr<ID3D11Texture2D> m_texture;
  39. ComPtr<ID3D11ShaderResourceView> m_srv;
  40. ComPtr<ID3D11UnorderedAccessView> m_uav;
  41. std::string m_name;
  42. };
  43. class DXStagingTexture final : public AbstractStagingTexture
  44. {
  45. public:
  46. DXStagingTexture() = delete;
  47. ~DXStagingTexture();
  48. void CopyFromTexture(const AbstractTexture* src, const MathUtil::Rectangle<int>& src_rect,
  49. u32 src_layer, u32 src_level,
  50. const MathUtil::Rectangle<int>& dst_rect) override;
  51. void CopyToTexture(const MathUtil::Rectangle<int>& src_rect, AbstractTexture* dst,
  52. const MathUtil::Rectangle<int>& dst_rect, u32 dst_layer,
  53. u32 dst_level) override;
  54. bool Map() override;
  55. void Unmap() override;
  56. void Flush() override;
  57. static std::unique_ptr<DXStagingTexture> Create(StagingTextureType type,
  58. const TextureConfig& config);
  59. private:
  60. DXStagingTexture(StagingTextureType type, const TextureConfig& config,
  61. ComPtr<ID3D11Texture2D> tex);
  62. ComPtr<ID3D11Texture2D> m_tex = nullptr;
  63. };
  64. class DXFramebuffer final : public AbstractFramebuffer
  65. {
  66. public:
  67. DXFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
  68. std::vector<AbstractTexture*> additional_color_attachments,
  69. AbstractTextureFormat color_format, AbstractTextureFormat depth_format, u32 width,
  70. u32 height, u32 layers, u32 samples, ComPtr<ID3D11RenderTargetView> rtv,
  71. ComPtr<ID3D11RenderTargetView> integer_rtv, ComPtr<ID3D11DepthStencilView> dsv,
  72. std::vector<ComPtr<ID3D11RenderTargetView>> additional_rtvs);
  73. ~DXFramebuffer() override;
  74. ID3D11RenderTargetView* const* GetRTVArray() const { return m_render_targets_raw.data(); }
  75. ID3D11RenderTargetView* const* GetIntegerRTVArray() const { return m_integer_rtv.GetAddressOf(); }
  76. UINT GetNumRTVs() const { return static_cast<UINT>(m_render_targets_raw.size()); }
  77. ID3D11DepthStencilView* GetDSV() const { return m_dsv.Get(); }
  78. void Unbind();
  79. void Clear(const ClearColor& color_value, float depth_value);
  80. static std::unique_ptr<DXFramebuffer>
  81. Create(DXTexture* color_attachment, DXTexture* depth_attachment,
  82. std::vector<AbstractTexture*> additional_color_attachments);
  83. protected:
  84. std::vector<ComPtr<ID3D11RenderTargetView>> m_render_targets;
  85. std::vector<ID3D11RenderTargetView*> m_render_targets_raw;
  86. ComPtr<ID3D11RenderTargetView> m_integer_rtv;
  87. ComPtr<ID3D11DepthStencilView> m_dsv;
  88. };
  89. } // namespace DX11