| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/AbstractFramebuffer.h" |
| 5 | |
| 6 | #include "VideoCommon/AbstractTexture.h" |
| 7 | |
| 8 | AbstractFramebuffer::AbstractFramebuffer(AbstractTexture* color_attachment, |
| 9 | AbstractTexture* depth_attachment, |
| 10 | std::vector<AbstractTexture*> additional_color_attachments, |
| 11 | AbstractTextureFormat color_format, |
| 12 | AbstractTextureFormat depth_format, u32 width, u32 height, |
| 13 | u32 layers, u32 samples) |
| 14 | : m_color_attachment(color_attachment), m_depth_attachment(depth_attachment), |
| 15 | m_additional_color_attachments(std::move(additional_color_attachments)), |
| 16 | m_color_format(color_format), m_depth_format(depth_format), m_width(width), m_height(height), |
| 17 | m_layers(layers), m_samples(samples) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | AbstractFramebuffer::~AbstractFramebuffer() = default; |
| 22 | |
| 23 | bool AbstractFramebuffer::ValidateConfig( |
| 24 | const AbstractTexture* color_attachment, const AbstractTexture* depth_attachment, |
| 25 | const std::vector<AbstractTexture*>& additional_color_attachments) |
| 26 | { |
| 27 | // Must have at least a color or depth attachment. |
| 28 | if (!color_attachment && !depth_attachment) |
| 29 | return false; |
| 30 | |
| 31 | // Must have a color attachment if additional color attachments are defined |
| 32 | if (!color_attachment && !additional_color_attachments.empty()) |
| 33 | return false; |
| 34 | |
| 35 | // Currently we only expose a single mip level for render target textures. |
| 36 | // MSAA textures are not supported with mip levels on most backends, and it simplifies our |
| 37 | // handling of framebuffers. |
| 38 | auto CheckAttachment = [](const AbstractTexture* tex) { |
| 39 | return tex->GetConfig().IsRenderTarget() && tex->GetConfig().levels == 1; |
| 40 | }; |
| 41 | if ((color_attachment && !CheckAttachment(color_attachment)) || |
| 42 | (depth_attachment && !CheckAttachment(depth_attachment))) |
| 43 | { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | for (auto* attachment : additional_color_attachments) |
| 48 | { |
| 49 | if (!CheckAttachment(attachment)) |
| 50 | { |
| 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // If both color and depth are present, their attributes must match. |
| 56 | if (color_attachment && depth_attachment) |
| 57 | { |
| 58 | if (color_attachment->GetConfig().width != depth_attachment->GetConfig().width || |
| 59 | color_attachment->GetConfig().height != depth_attachment->GetConfig().height || |
| 60 | color_attachment->GetConfig().layers != depth_attachment->GetConfig().layers || |
| 61 | color_attachment->GetConfig().samples != depth_attachment->GetConfig().samples) |
| 62 | { |
| 63 | return false; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | for (auto* attachment : additional_color_attachments) |
| 68 | { |
| 69 | if (color_attachment->GetConfig().width != attachment->GetConfig().width || |
| 70 | color_attachment->GetConfig().height != attachment->GetConfig().height || |
| 71 | color_attachment->GetConfig().samples != attachment->GetConfig().samples) |
| 72 | { |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | MathUtil::Rectangle<int> AbstractFramebuffer::GetRect() const |
| 81 | { |
| 82 | return MathUtil::Rectangle<int>(0, 0, static_cast<int>(m_width), static_cast<int>(m_height)); |
| 83 | } |