SWGfx.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2023 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoBackends/Software/SWGfx.h"
  4. #include "Common/GL/GLContext.h"
  5. #include "VideoBackends/Software/EfbCopy.h"
  6. #include "VideoBackends/Software/Rasterizer.h"
  7. #include "VideoBackends/Software/SWOGLWindow.h"
  8. #include "VideoBackends/Software/SWTexture.h"
  9. #include "VideoCommon/AbstractPipeline.h"
  10. #include "VideoCommon/AbstractShader.h"
  11. #include "VideoCommon/AbstractTexture.h"
  12. #include "VideoCommon/NativeVertexFormat.h"
  13. #include "VideoCommon/Present.h"
  14. namespace SW
  15. {
  16. SWGfx::SWGfx(std::unique_ptr<SWOGLWindow> window) : m_window(std::move(window))
  17. {
  18. }
  19. SWGfx::~SWGfx() = default;
  20. bool SWGfx::IsHeadless() const
  21. {
  22. return m_window->IsHeadless();
  23. }
  24. bool SWGfx::SupportsUtilityDrawing() const
  25. {
  26. return false;
  27. }
  28. std::unique_ptr<AbstractTexture> SWGfx::CreateTexture(const TextureConfig& config,
  29. [[maybe_unused]] std::string_view name)
  30. {
  31. return std::make_unique<SWTexture>(config);
  32. }
  33. std::unique_ptr<AbstractStagingTexture> SWGfx::CreateStagingTexture(StagingTextureType type,
  34. const TextureConfig& config)
  35. {
  36. return std::make_unique<SWStagingTexture>(type, config);
  37. }
  38. std::unique_ptr<AbstractFramebuffer>
  39. SWGfx::CreateFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
  40. std::vector<AbstractTexture*> additional_color_attachments)
  41. {
  42. return SWFramebuffer::Create(static_cast<SWTexture*>(color_attachment),
  43. static_cast<SWTexture*>(depth_attachment),
  44. std::move(additional_color_attachments));
  45. }
  46. bool SWGfx::BindBackbuffer(const ClearColor& clear_color)
  47. {
  48. // Look for framebuffer resizes
  49. if (!g_presenter->SurfaceResizedTestAndClear())
  50. return true;
  51. GLContext* context = m_window->GetContext();
  52. context->Update();
  53. g_presenter->SetBackbuffer(context->GetBackBufferWidth(), context->GetBackBufferHeight());
  54. return true;
  55. }
  56. class SWShader final : public AbstractShader
  57. {
  58. public:
  59. explicit SWShader(ShaderStage stage) : AbstractShader(stage) {}
  60. ~SWShader() = default;
  61. BinaryData GetBinary() const override { return {}; }
  62. };
  63. std::unique_ptr<AbstractShader>
  64. SWGfx::CreateShaderFromSource(ShaderStage stage, [[maybe_unused]] std::string_view source,
  65. [[maybe_unused]] std::string_view name)
  66. {
  67. return std::make_unique<SWShader>(stage);
  68. }
  69. std::unique_ptr<AbstractShader>
  70. SWGfx::CreateShaderFromBinary(ShaderStage stage, const void* data, size_t length,
  71. [[maybe_unused]] std::string_view name)
  72. {
  73. return std::make_unique<SWShader>(stage);
  74. }
  75. class SWPipeline final : public AbstractPipeline
  76. {
  77. public:
  78. SWPipeline() = default;
  79. ~SWPipeline() override = default;
  80. };
  81. std::unique_ptr<AbstractPipeline> SWGfx::CreatePipeline(const AbstractPipelineConfig& config,
  82. const void* cache_data,
  83. size_t cache_data_length)
  84. {
  85. return std::make_unique<SWPipeline>();
  86. }
  87. // Called on the GPU thread
  88. void SWGfx::ShowImage(const AbstractTexture* source_texture,
  89. const MathUtil::Rectangle<int>& source_rc)
  90. {
  91. if (!IsHeadless())
  92. m_window->ShowImage(source_texture, source_rc);
  93. }
  94. void SWGfx::ClearRegion(const MathUtil::Rectangle<int>& target_rc, bool colorEnable,
  95. bool alphaEnable, bool zEnable, u32 color, u32 z)
  96. {
  97. EfbCopy::ClearEfb();
  98. }
  99. std::unique_ptr<NativeVertexFormat>
  100. SWGfx::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl)
  101. {
  102. return std::make_unique<NativeVertexFormat>(vtx_decl);
  103. }
  104. void SWGfx::SetScissorRect(const MathUtil::Rectangle<int>& rc)
  105. {
  106. // BPFunctions calls SetScissorRect with the "best" scissor rect whenever the viewport or scissor
  107. // changes. However, the software renderer is actually able to use multiple scissor rects (which
  108. // is necessary in a few renderering edge cases, such as with Major Minor's Majestic March).
  109. // Thus, we use this as a signal to update the list of scissor rects, but ignore the parameter.
  110. Rasterizer::ScissorChanged();
  111. }
  112. SurfaceInfo SWGfx::GetSurfaceInfo() const
  113. {
  114. GLContext* context = m_window->GetContext();
  115. return {std::max(context->GetBackBufferWidth(), 1u), std::max(context->GetBackBufferHeight(), 1u),
  116. 1.0f, AbstractTextureFormat::RGBA8};
  117. }
  118. } // namespace SW