| 1 |
// Copyright 2022 Dolphin Emulator Project |
| 2 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 |
|
| 4 |
#include "VideoBackends/Metal/MTLTexture.h" |
| 5 |
|
| 6 |
#include "Common/Align.h" |
| 7 |
#include "Common/Assert.h" |
| 8 |
|
| 9 |
#include "VideoBackends/Metal/MTLGfx.h" |
| 10 |
#include "VideoBackends/Metal/MTLStateTracker.h" |
| 11 |
|
| 12 |
Metal::Texture::Texture(MRCOwned<id<MTLTexture>> tex, const TextureConfig& config) |
| 13 |
: AbstractTexture(config), m_tex(std::move(tex)) |
| 14 |
{ |
| 15 |
} |
| 16 |
|
| 17 |
Metal::Texture::~Texture() |
| 18 |
{ |
| 19 |
if (g_state_tracker) |
| 20 |
g_state_tracker->UnbindTexture(m_tex); |
| 21 |
} |
| 22 |
|
| 23 |
void Metal::Texture::CopyRectangleFromTexture(const AbstractTexture* src, |
| 24 |
const MathUtil::Rectangle<int>& src_rect, |
| 25 |
u32 src_layer, u32 src_level, |
| 26 |
const MathUtil::Rectangle<int>& dst_rect, |
| 27 |
u32 dst_layer, u32 dst_level) |
| 28 |
{ |
| 29 |
g_state_tracker->EndRenderPass(); |
| 30 |
id<MTLTexture> msrc = static_cast<const Texture*>(src)->GetMTLTexture(); |
| 31 |
id<MTLBlitCommandEncoder> blit = [g_state_tracker->GetRenderCmdBuf() blitCommandEncoder]; |
| 32 |
MTLSize size = MTLSizeMake(src_rect.right - src_rect.left, src_rect.bottom - src_rect.top, 1); |
| 33 |
[blit setLabel:@"Texture Copy"]; |
| 34 |
[blit copyFromTexture:msrc |
| 35 |
sourceSlice:src_layer |
| 36 |
sourceLevel:src_level |
| 37 |
sourceOrigin:MTLOriginMake(src_rect.left, src_rect.top, 0) |
| 38 |
sourceSize:size |
| 39 |
toTexture:m_tex |
| 40 |
destinationSlice:dst_layer |
| 41 |
destinationLevel:dst_level |
| 42 |
destinationOrigin:MTLOriginMake(dst_rect.left, dst_rect.top, 0)]; |
| 43 |
[blit endEncoding]; |
| 44 |
} |
| 45 |
|
| 46 |
void Metal::Texture::ResolveFromTexture(const AbstractTexture* src, |
| 47 |
const MathUtil::Rectangle<int>& rect, u32 layer, u32 level) |
| 48 |
{ |
| 49 |
ASSERT(rect == MathUtil::Rectangle<int>(0, 0, src->GetWidth(), src->GetHeight())); |
| 50 |
id<MTLTexture> src_tex = static_cast<const Texture*>(src)->GetMTLTexture(); |
| 51 |
g_state_tracker->ResolveTexture(src_tex, m_tex, layer, level); |
| 52 |
} |
| 53 |
|
| 54 |
// Use a temporary texture for large texture loads |
| 55 |
// (Since the main upload buffer doesn't shrink after it grows) |
| 56 |
static constexpr u32 STAGING_TEXTURE_UPLOAD_THRESHOLD = 1024 * 1024 * 4; |
| 57 |
|
| 58 |
void Metal::Texture::Load(u32 level, u32 width, u32 height, u32 row_length, // |
| 59 |
const u8* buffer, size_t buffer_size, u32 layer) |
| 60 |
{ |
| 61 |
@autoreleasepool |
| 62 |
{ |
| 63 |
const u32 block_size = GetBlockSizeForFormat(GetFormat()); |
| 64 |
const u32 num_rows = Common::AlignUp(height, block_size) / block_size; |
| 65 |
const u32 source_pitch = CalculateStrideForFormat(m_config.format, row_length); |
| 66 |
const u32 upload_size = source_pitch * num_rows; |
| 67 |
MRCOwned<id<MTLBuffer>> tmp_buffer; |
| 68 |
StateTracker::Map map; |
| 69 |
if (upload_size > STAGING_TEXTURE_UPLOAD_THRESHOLD) |
| 70 |
{ |
| 71 |
tmp_buffer = MRCTransfer([g_device |
| 72 |
newBufferWithLength:upload_size |
| 73 |
options:MTLResourceStorageModeShared | MTLResourceCPUCacheModeWriteCombined]); |
| 74 |
[tmp_buffer setLabel:@"Temp Texture Upload"]; |
| 75 |
map.gpu_buffer = tmp_buffer; |
| 76 |
map.gpu_offset = 0; |
| 77 |
map.cpu_buffer = [tmp_buffer contents]; |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
map = g_state_tracker->AllocateForTextureUpload(upload_size); |
| 82 |
} |
| 83 |
|
| 84 |
memcpy(map.cpu_buffer, buffer, upload_size); |
| 85 |
id<MTLBlitCommandEncoder> encoder = g_state_tracker->GetTextureUploadEncoder(); |
| 86 |
[encoder copyFromBuffer:map.gpu_buffer |
| 87 |
sourceOffset:map.gpu_offset |
| 88 |
sourceBytesPerRow:source_pitch |
| 89 |
sourceBytesPerImage:upload_size |
| 90 |
sourceSize:MTLSizeMake(width, height, 1) |
| 91 |
toTexture:m_tex |
| 92 |
destinationSlice:layer |
| 93 |
destinationLevel:level |
| 94 |
destinationOrigin:MTLOriginMake(0, 0, 0)]; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
Metal::StagingTexture::StagingTexture(MRCOwned<id<MTLBuffer>> buffer, StagingTextureType type, |
| 99 |
const TextureConfig& config) |
| 100 |
: AbstractStagingTexture(type, config), m_buffer(std::move(buffer)) |
| 101 |
{ |
| 102 |
m_map_pointer = static_cast<char*>([m_buffer contents]); |
| 103 |
m_map_stride = config.GetStride(); |
| 104 |
} |
| 105 |
|
| 106 |
Metal::StagingTexture::~StagingTexture() = default; |
| 107 |
|
| 108 |
void Metal::StagingTexture::CopyFromTexture(const AbstractTexture* src, |
| 109 |
const MathUtil::Rectangle<int>& src_rect, // |
| 110 |
u32 src_layer, u32 src_level, |
| 111 |
const MathUtil::Rectangle<int>& dst_rect) |
| 112 |
{ |
| 113 |
@autoreleasepool |
| 114 |
{ |
| 115 |
const size_t stride = m_config.GetStride(); |
| 116 |
const u32 offset = dst_rect.top * stride + dst_rect.left * m_texel_size; |
| 117 |
const MTLSize size = |
| 118 |
MTLSizeMake(src_rect.right - src_rect.left, src_rect.bottom - src_rect.top, 1); |
| 119 |
g_state_tracker->EndRenderPass(); |
| 120 |
m_wait_buffer = MRCRetain(g_state_tracker->GetRenderCmdBuf()); |
| 121 |
id<MTLBlitCommandEncoder> download_encoder = [m_wait_buffer blitCommandEncoder]; |
| 122 |
[download_encoder setLabel:@"Texture Download"]; |
| 123 |
[download_encoder copyFromTexture:static_cast<const Texture*>(src)->GetMTLTexture() |
| 124 |
sourceSlice:src_layer |
| 125 |
sourceLevel:src_level |
| 126 |
sourceOrigin:MTLOriginMake(src_rect.left, src_rect.top, 0) |
| 127 |
sourceSize:size |
| 128 |
toBuffer:m_buffer |
| 129 |
destinationOffset:offset |
| 130 |
destinationBytesPerRow:stride |
| 131 |
destinationBytesPerImage:stride * size.height]; |
| 132 |
[download_encoder endEncoding]; |
| 133 |
m_needs_flush = true; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void Metal::StagingTexture::CopyToTexture(const MathUtil::Rectangle<int>& src_rect, // |
| 138 |
AbstractTexture* dst, |
| 139 |
const MathUtil::Rectangle<int>& dst_rect, // |
| 140 |
u32 dst_layer, u32 dst_level) |
| 141 |
{ |
| 142 |
@autoreleasepool |
| 143 |
{ |
| 144 |
const size_t stride = m_config.GetStride(); |
| 145 |
const u32 offset = src_rect.top * stride + src_rect.left * m_texel_size; |
| 146 |
const MTLSize size = |
| 147 |
MTLSizeMake(src_rect.right - src_rect.left, src_rect.bottom - src_rect.top, 1); |
| 148 |
g_state_tracker->EndRenderPass(); |
| 149 |
m_wait_buffer = MRCRetain(g_state_tracker->GetRenderCmdBuf()); |
| 150 |
id<MTLBlitCommandEncoder> upload_encoder = [m_wait_buffer blitCommandEncoder]; |
| 151 |
[upload_encoder setLabel:@"Texture Upload"]; |
| 152 |
[upload_encoder copyFromBuffer:m_buffer |
| 153 |
sourceOffset:offset |
| 154 |
sourceBytesPerRow:stride |
| 155 |
sourceBytesPerImage:stride * size.height |
| 156 |
sourceSize:size |
| 157 |
toTexture:static_cast<Texture*>(dst)->GetMTLTexture() |
| 158 |
destinationSlice:dst_layer |
| 159 |
destinationLevel:dst_level |
| 160 |
destinationOrigin:MTLOriginMake(dst_rect.left, dst_rect.top, 0)]; |
| 161 |
[upload_encoder endEncoding]; |
| 162 |
m_needs_flush = true; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
bool Metal::StagingTexture::Map() |
| 167 |
{ |
| 168 |
// Always mapped |
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
void Metal::StagingTexture::Unmap() |
| 173 |
{ |
| 174 |
// Always mapped |
| 175 |
} |
| 176 |
|
| 177 |
void Metal::StagingTexture::Flush() |
| 178 |
{ |
| 179 |
m_needs_flush = false; |
| 180 |
if (!m_wait_buffer) |
| 181 |
return; |
| 182 |
if ([m_wait_buffer status] != MTLCommandBufferStatusCompleted) |
| 183 |
{ |
| 184 |
// Flush while we wait, since who knows how long we'll be sitting here |
| 185 |
g_state_tracker->FlushEncoders(); |
| 186 |
g_state_tracker->NotifyOfCPUGPUSync(); |
| 187 |
[m_wait_buffer waitUntilCompleted]; |
| 188 |
} |
| 189 |
m_wait_buffer = nullptr; |
| 190 |
} |
| 191 |
|
| 192 |
static void InitDesc(id desc, AbstractTexture* tex) |
| 193 |
{ |
| 194 |
[desc setTexture:static_cast<Metal::Texture*>(tex)->GetMTLTexture()]; |
| 195 |
[desc setLoadAction:MTLLoadActionLoad]; |
| 196 |
[desc setStoreAction:MTLStoreActionStore]; |
| 197 |
} |
| 198 |
|
| 199 |
static void InitStencilDesc(MTLRenderPassStencilAttachmentDescriptor* desc, AbstractTexture* tex) |
| 200 |
{ |
| 201 |
InitDesc(desc, tex); |
| 202 |
[desc setClearStencil:0]; |
| 203 |
} |
| 204 |
|
| 205 |
Metal::Framebuffer::Framebuffer(AbstractTexture* color, AbstractTexture* depth, |
| 206 |
std::vector<AbstractTexture*> additonal_color_textures, // |
| 207 |
u32 width, u32 height, u32 layers, u32 samples) |
| 208 |
: AbstractFramebuffer(color, depth, {}, |
| 209 |
color ? color->GetFormat() : AbstractTextureFormat::Undefined, // |
| 210 |
depth ? depth->GetFormat() : AbstractTextureFormat::Undefined, // |
| 211 |
width, height, layers, samples), |
| 212 |
m_additional_color_textures(std::move(additonal_color_textures)) |
| 213 |
{ |
| 214 |
m_pass_descriptor = MRCTransfer([MTLRenderPassDescriptor new]); |
| 215 |
MTLRenderPassDescriptor* desc = m_pass_descriptor; |
| 216 |
if (color) |
| 217 |
InitDesc(desc.colorAttachments[0], color); |
| 218 |
if (depth) |
| 219 |
{ |
| 220 |
InitDesc(desc.depthAttachment, depth); |
| 221 |
if (Util::HasStencil(depth->GetFormat())) |
| 222 |
InitStencilDesc(desc.stencilAttachment, depth); |
| 223 |
} |
| 224 |
for (size_t i = 0; i < m_additional_color_textures.size(); i++) |
| 225 |
InitDesc(desc.colorAttachments[i + 1], m_additional_color_textures[i]); |
| 226 |
} |
| 227 |
|
| 228 |
Metal::Framebuffer::~Framebuffer() = default; |
| 229 |
|
| 230 |
void Metal::Framebuffer::ActualSetLoadAction(MTLLoadAction action) |
| 231 |
{ |
| 232 |
m_current_load_action = action; |
| 233 |
AbstractTextureFormat depth_fmt = GetDepthFormat(); |
| 234 |
MTLRenderPassDescriptor* desc = m_pass_descriptor; |
| 235 |
desc.colorAttachments[0].loadAction = action; |
| 236 |
if (depth_fmt != AbstractTextureFormat::Undefined) |
| 237 |
{ |
| 238 |
desc.depthAttachment.loadAction = action; |
| 239 |
if (Util::HasStencil(depth_fmt)) |
| 240 |
desc.stencilAttachment.loadAction = action; |
| 241 |
} |
| 242 |
for (size_t i = 0; i < NumAdditionalColorTextures(); i++) |
| 243 |
desc.colorAttachments[i + 1].loadAction = action; |
| 244 |
} |