| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/AbstractTexture.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | |
| 8 | #include <imgui.h> |
| 9 | |
| 10 | #include "Common/Assert.h" |
| 11 | #include "Common/Image.h" |
| 12 | #include "Common/MsgHandler.h" |
| 13 | #include "VideoCommon/AbstractGfx.h" |
| 14 | #include "VideoCommon/AbstractStagingTexture.h" |
| 15 | |
| 16 | static_assert(std::is_same_v<AbstractTexture::imgui_texture_id, ImTextureID>, |
| 17 | "The typedef should match ImTextureID in imgui"); |
| 18 | |
| 19 | AbstractTexture::AbstractTexture(const TextureConfig& c) : m_config(c) |
| 20 | { |
| 21 | } |
| 22 | |
| 23 | AbstractTexture::operator ImTextureRef() const |
| 24 | { |
| 25 | return ImTextureRef(reinterpret_cast<AbstractTexture::imgui_texture_id>(this)); |
| 26 | } |
| 27 | |
| 28 | void AbstractTexture::FinishedRendering() |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | bool AbstractTexture::Save(const std::string& filename, unsigned int level, int compression) const |
| 33 | { |
| 34 | // We can't dump compressed textures currently (it would mean drawing them to a RGBA8 |
| 35 | // framebuffer, and saving that). TextureCache does not call Save for custom textures |
| 36 | // anyway, so this is fine for now. |
| 37 | ASSERT(!IsCompressedFormat(m_config.format)); |
| 38 | ASSERT(level < m_config.levels); |
| 39 | // We can't copy from float (HDR) textures to RGBA8 |
| 40 | // (most other formats would probably fail as well) |
| 41 | ASSERT(m_config.format != AbstractTextureFormat::RGBA16F); |
| 42 | |
| 43 | // Determine dimensions of image we want to save. |
| 44 | u32 level_width = std::max(1u, m_config.width >> level); |
| 45 | u32 level_height = std::max(1u, m_config.height >> level); |
| 46 | |
| 47 | // Use a temporary staging texture for the download. Certainly not optimal, |
| 48 | // but this is not a frequently-executed code path.. |
| 49 | TextureConfig readback_texture_config(level_width, level_height, 1, 1, 1, |
| 50 | AbstractTextureFormat::RGBA8, 0, |
| 51 | AbstractTextureType::Texture_2DArray); |
| 52 | auto readback_texture = |
| 53 | g_gfx->CreateStagingTexture(StagingTextureType::Readback, readback_texture_config); |
| 54 | if (!readback_texture) |
| 55 | return false; |
| 56 | |
| 57 | // Copy to the readback texture's buffer. |
| 58 | readback_texture->CopyFromTexture(this, 0, level); |
| 59 | readback_texture->Flush(); |
| 60 | |
| 61 | // Map it so we can encode it to the file. |
| 62 | if (!readback_texture->Map()) |
| 63 | return false; |
| 64 | |
| 65 | return Common::SavePNG(filename, |
| 66 | reinterpret_cast<const u8*>(readback_texture->GetMappedPointer()), |
| 67 | Common::ImageByteFormat::RGBA, level_width, level_height, |
| 68 | static_cast<int>(readback_texture->GetMappedStride()), compression); |
| 69 | } |
| 70 | |
| 71 | bool AbstractTexture::IsCompressedFormat(AbstractTextureFormat format) |
| 72 | { |
| 73 | switch (format) |
| 74 | { |
| 75 | case AbstractTextureFormat::DXT1: |
| 76 | case AbstractTextureFormat::DXT3: |
| 77 | case AbstractTextureFormat::DXT5: |
| 78 | case AbstractTextureFormat::BPTC: |
| 79 | return true; |
| 80 | |
| 81 | default: |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | bool AbstractTexture::IsDepthFormat(AbstractTextureFormat format) |
| 87 | { |
| 88 | switch (format) |
| 89 | { |
| 90 | case AbstractTextureFormat::D16: |
| 91 | case AbstractTextureFormat::D24_S8: |
| 92 | case AbstractTextureFormat::D32F: |
| 93 | case AbstractTextureFormat::D32F_S8: |
| 94 | return true; |
| 95 | |
| 96 | default: |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | bool AbstractTexture::IsStencilFormat(AbstractTextureFormat format) |
| 102 | { |
| 103 | return format == AbstractTextureFormat::D24_S8 || format == AbstractTextureFormat::D32F_S8; |
| 104 | } |
| 105 | |
| 106 | bool AbstractTexture::IsCompatibleDepthAndColorFormats(AbstractTextureFormat depth_format, |
| 107 | AbstractTextureFormat color_format) |
| 108 | { |
| 109 | switch (depth_format) |
| 110 | { |
| 111 | case AbstractTextureFormat::D16: |
| 112 | return color_format == AbstractTextureFormat::R16; |
| 113 | |
| 114 | case AbstractTextureFormat::D32F: |
| 115 | return color_format == AbstractTextureFormat::R32F; |
| 116 | |
| 117 | default: |
| 118 | return false; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | u32 AbstractTexture::CalculateStrideForFormat(AbstractTextureFormat format, u32 row_length) |
| 123 | { |
| 124 | switch (format) |
| 125 | { |
| 126 | case AbstractTextureFormat::DXT1: |
| 127 | return static_cast<size_t>(std::max(1u, row_length / 4)) * 8; |
| 128 | case AbstractTextureFormat::DXT3: |
| 129 | case AbstractTextureFormat::DXT5: |
| 130 | case AbstractTextureFormat::BPTC: |
| 131 | return static_cast<size_t>(std::max(1u, row_length / 4)) * 16; |
| 132 | case AbstractTextureFormat::R16: |
| 133 | case AbstractTextureFormat::D16: |
| 134 | return static_cast<size_t>(row_length) * 2; |
| 135 | case AbstractTextureFormat::RGBA8: |
| 136 | case AbstractTextureFormat::BGRA8: |
| 137 | case AbstractTextureFormat::RGB10_A2: |
| 138 | case AbstractTextureFormat::R32F: |
| 139 | case AbstractTextureFormat::D32F: |
| 140 | case AbstractTextureFormat::D24_S8: |
| 141 | return static_cast<size_t>(row_length) * 4; |
| 142 | case AbstractTextureFormat::D32F_S8: |
| 143 | case AbstractTextureFormat::RGBA16F: |
| 144 | return static_cast<size_t>(row_length) * 8; |
| 145 | default: |
| 146 | PanicAlertFmt("Unhandled texture format."); |
| 147 | return 0; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | u32 AbstractTexture::GetTexelSizeForFormat(AbstractTextureFormat format) |
| 152 | { |
| 153 | switch (format) |
| 154 | { |
| 155 | case AbstractTextureFormat::DXT1: |
| 156 | return 8; |
| 157 | case AbstractTextureFormat::DXT3: |
| 158 | case AbstractTextureFormat::DXT5: |
| 159 | case AbstractTextureFormat::BPTC: |
| 160 | return 16; |
| 161 | case AbstractTextureFormat::R16: |
| 162 | case AbstractTextureFormat::D16: |
| 163 | return 2; |
| 164 | case AbstractTextureFormat::RGBA8: |
| 165 | case AbstractTextureFormat::BGRA8: |
| 166 | case AbstractTextureFormat::RGB10_A2: |
| 167 | case AbstractTextureFormat::D24_S8: |
| 168 | case AbstractTextureFormat::R32F: |
| 169 | case AbstractTextureFormat::D32F: |
| 170 | return 4; |
| 171 | case AbstractTextureFormat::D32F_S8: |
| 172 | case AbstractTextureFormat::RGBA16F: |
| 173 | return 8; |
| 174 | default: |
| 175 | PanicAlertFmt("Unhandled texture format."); |
| 176 | return 0; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | u32 AbstractTexture::GetBlockSizeForFormat(AbstractTextureFormat format) |
| 181 | { |
| 182 | switch (format) |
| 183 | { |
| 184 | case AbstractTextureFormat::DXT1: |
| 185 | case AbstractTextureFormat::DXT3: |
| 186 | case AbstractTextureFormat::DXT5: |
| 187 | case AbstractTextureFormat::BPTC: |
| 188 | return 4; |
| 189 | |
| 190 | default: |
| 191 | return 1; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | const TextureConfig& AbstractTexture::GetConfig() const |
| 196 | { |
| 197 | return m_config; |
| 198 | } |