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