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