forked from ShuriZma/suyu
1
0
Fork 0

metal: use metal cpp

This commit is contained in:
Samuliak 2024-04-06 20:28:54 +02:00
parent cc0cde9c3f
commit 897e0dfdc0
20 changed files with 189 additions and 225 deletions

View File

@ -320,4 +320,3 @@ if (ARCHITECTURE_arm64 AND NOT TARGET sse2neon)
add_library(sse2neon INTERFACE)
target_include_directories(sse2neon INTERFACE sse2neon)
endif()

View File

@ -184,6 +184,11 @@ else()
endif()
endif()
if (APPLE)
# TODO: use target_link_libraries
include_directories(${CMAKE_SOURCE_DIR}/externals/metal-cpp)
endif()
add_subdirectory(common)
add_subdirectory(core)
add_subdirectory(audio_core)

View File

@ -374,16 +374,17 @@ if (APPLE)
)
list(APPEND sources
renderer_metal/mtl_buffer_cache.mm
renderer_metal/metal_cpp_impl.cpp
renderer_metal/mtl_buffer_cache.cpp
renderer_metal/mtl_buffer_cache_base.cpp
renderer_metal/mtl_command_recorder.mm
renderer_metal/mtl_device.mm
renderer_metal/mtl_rasterizer.mm
renderer_metal/mtl_staging_buffer_pool.mm
renderer_metal/mtl_swap_chain.mm
renderer_metal/mtl_texture_cache.mm
renderer_metal/mtl_command_recorder.cpp
renderer_metal/mtl_device.cpp
renderer_metal/mtl_rasterizer.cpp
renderer_metal/mtl_staging_buffer_pool.cpp
renderer_metal/mtl_swap_chain.cpp
renderer_metal/mtl_texture_cache.cpp
renderer_metal/mtl_texture_cache_base.cpp
renderer_metal/renderer_metal.mm
renderer_metal/renderer_metal.cpp
)
endif()
@ -404,10 +405,6 @@ add_dependencies(video_core host_shaders)
target_include_directories(video_core PRIVATE ${HOST_SHADERS_INCLUDE})
target_link_libraries(video_core PRIVATE sirit Vulkan::Headers Vulkan::UtilityHeaders GPUOpen::VulkanMemoryAllocator)
if (APPLE)
target_include_directories(video_core PRIVATE ${CMAKE_SOURCE_DIR}/externals/metal-cpp)
endif()
if (ENABLE_NSIGHT_AFTERMATH)
if (NOT DEFINED ENV{NSIGHT_AFTERMATH_SDK})
message(FATAL_ERROR "Environment variable NSIGHT_AFTERMATH_SDK has to be provided")

View File

@ -0,0 +1,6 @@
#define NS_PRIVATE_IMPLEMENTATION
#define CA_PRIVATE_IMPLEMENTATION
#define MTL_PRIVATE_IMPLEMENTATION
#include <Foundation/Foundation.hpp>
#include <Metal/Metal.hpp>
#include <QuartzCore/QuartzCore.hpp>

View File

@ -15,37 +15,36 @@ namespace Metal {
namespace {
MTLBuffer_t CreatePrivateBuffer(const Device& device, size_t size) {
return [device.GetDevice() newBufferWithLength:size options:MTLResourceStorageModePrivate];
MTL::Buffer* CreatePrivateBuffer(const Device& device, size_t size) {
return device.GetDevice()->newBuffer(size, MTL::ResourceStorageModePrivate);
}
} // Anonymous namespace
BoundBuffer::BoundBuffer(MTLBuffer_t buffer_, size_t offset_, size_t size_)
: buffer{[buffer_ retain]}, offset{offset_}, size{size_} {}
BoundBuffer::BoundBuffer(MTL::Buffer* buffer_, size_t offset_, size_t size_)
: buffer{buffer_->retain()}, offset{offset_}, size{size_} {}
BoundBuffer::~BoundBuffer() {
if (buffer) {
[buffer release];
buffer->release();
}
}
BufferView::BufferView(MTLBuffer_t buffer_, size_t offset_, size_t size_,
BufferView::BufferView(MTL::Buffer* buffer_, size_t offset_, size_t size_,
VideoCore::Surface::PixelFormat format_)
: buffer{[buffer_ retain]}, offset{offset_}, size{size_}, format{format_} {}
: buffer{buffer_->retain()}, offset{offset_}, size{size_}, format{format_} {}
BufferView::~BufferView() {
[buffer release];
buffer->release();
}
Buffer::Buffer(BufferCacheRuntime& runtime, VideoCommon::NullBufferParams null_params)
: VideoCommon::BufferBase(null_params), buffer{runtime.CreateNullBuffer()},
is_null{true}, view(buffer, 0, BufferCacheRuntime::NULL_BUFFER_SIZE) {}
: VideoCommon::BufferBase(null_params), buffer{runtime.CreateNullBuffer()}, is_null{true},
view(buffer, 0, BufferCacheRuntime::NULL_BUFFER_SIZE) {}
Buffer::Buffer(BufferCacheRuntime& runtime, DAddr cpu_addr_, u64 size_bytes_)
: VideoCommon::BufferBase(cpu_addr_, size_bytes_),
buffer{CreatePrivateBuffer(runtime.device, size_bytes_)},
view(buffer, 0, size_bytes_) {}
buffer{CreatePrivateBuffer(runtime.device, size_bytes_)}, view(buffer, 0, size_bytes_) {}
BufferView Buffer::View(u32 offset, u32 size, VideoCore::Surface::PixelFormat format) {
return BufferView(buffer, offset, size, format);
@ -78,18 +77,18 @@ void BufferCacheRuntime::TickFrame(Common::SlotVector<Buffer>& slot_buffers) noe
void BufferCacheRuntime::Finish() {}
void BufferCacheRuntime::CopyBuffer(MTLBuffer_t dst_buffer, MTLBuffer_t src_buffer,
void BufferCacheRuntime::CopyBuffer(MTL::Buffer* dst_buffer, MTL::Buffer* src_buffer,
std::span<const VideoCommon::BufferCopy> copies, bool barrier,
bool can_reorder_upload) {
// TODO: copy buffer
}
void BufferCacheRuntime::ClearBuffer(MTLBuffer_t dest_buffer, u32 offset, size_t size, u32 value) {
void BufferCacheRuntime::ClearBuffer(MTL::Buffer* dest_buffer, u32 offset, size_t size, u32 value) {
// TODO: clear buffer
}
void BufferCacheRuntime::BindIndexBuffer(PrimitiveTopology topology, IndexFormat index_format,
u32 base_vertex, u32 num_indices, MTLBuffer_t buffer,
u32 base_vertex, u32 num_indices, MTL::Buffer* buffer,
u32 offset, [[maybe_unused]] u32 size) {
// TODO: convert parameters to Metal enums
bound_index_buffer = {BoundBuffer(buffer, offset, size)};
@ -99,7 +98,7 @@ void BufferCacheRuntime::BindQuadIndexBuffer(PrimitiveTopology topology, u32 fir
// TODO: bind quad index buffer
}
void BufferCacheRuntime::BindVertexBuffer(u32 index, MTLBuffer_t buffer, u32 offset, u32 size,
void BufferCacheRuntime::BindVertexBuffer(u32 index, MTL::Buffer* buffer, u32 offset, u32 size,
u32 stride) {
// TODO: use stride
bound_vertex_buffers[MAX_METAL_BUFFERS - index - 1] = {BoundBuffer(buffer, offset, size)};
@ -115,9 +114,8 @@ void BufferCacheRuntime::ReserveNullBuffer() {
}
}
MTLBuffer_t BufferCacheRuntime::CreateNullBuffer() {
return [device.GetDevice() newBufferWithLength:NULL_BUFFER_SIZE
options:MTLResourceStorageModePrivate];
MTL::Buffer* BufferCacheRuntime::CreateNullBuffer() {
return CreatePrivateBuffer(device, NULL_BUFFER_SIZE);
}
} // namespace Vulkan
} // namespace Metal

View File

@ -19,21 +19,21 @@ class BufferCacheRuntime;
struct BoundBuffer {
BoundBuffer() = default;
BoundBuffer(MTLBuffer_t buffer_, size_t offset_, size_t size_);
BoundBuffer(MTL::Buffer* buffer_, size_t offset_, size_t size_);
~BoundBuffer();
MTLBuffer_t buffer = nil;
MTL::Buffer* buffer = nil;
size_t offset{};
size_t size{};
};
struct BufferView {
BufferView(MTLBuffer_t buffer_, size_t offset_, size_t size_,
BufferView(MTL::Buffer* buffer_, size_t offset_, size_t size_,
VideoCore::Surface::PixelFormat format_ = VideoCore::Surface::PixelFormat::Invalid);
~BufferView();
MTLBuffer_t buffer = nil;
MTL::Buffer* buffer = nil;
size_t offset{};
size_t size{};
VideoCore::Surface::PixelFormat format{};
@ -50,16 +50,16 @@ public:
// TODO: track usage
}
[[nodiscard]] MTLBuffer_t Handle() const noexcept {
[[nodiscard]] MTL::Buffer* Handle() const noexcept {
return buffer;
}
operator MTLBuffer_t() const noexcept {
operator MTL::Buffer*() const noexcept {
return buffer;
}
private:
MTLBuffer_t buffer = nil;
MTL::Buffer* buffer = nil;
bool is_null{};
BufferView view;
@ -108,25 +108,25 @@ public:
void PreCopyBarrier() {}
void CopyBuffer(MTLBuffer_t src_buffer, MTLBuffer_t dst_buffer,
void CopyBuffer(MTL::Buffer* src_buffer, MTL::Buffer* dst_buffer,
std::span<const VideoCommon::BufferCopy> copies, bool barrier,
bool can_reorder_upload = false);
void PostCopyBarrier() {}
void ClearBuffer(MTLBuffer_t dest_buffer, u32 offset, size_t size, u32 value);
void ClearBuffer(MTL::Buffer* dest_buffer, u32 offset, size_t size, u32 value);
void BindIndexBuffer(PrimitiveTopology topology, IndexFormat index_format, u32 num_indices,
u32 base_vertex, MTLBuffer_t buffer, u32 offset, u32 size);
u32 base_vertex, MTL::Buffer* buffer, u32 offset, u32 size);
void BindQuadIndexBuffer(PrimitiveTopology topology, u32 first, u32 count);
void BindVertexBuffer(u32 index, MTLBuffer_t buffer, u32 offset, u32 size, u32 stride);
void BindVertexBuffer(u32 index, MTL::Buffer* buffer, u32 offset, u32 size, u32 stride);
void BindVertexBuffers(VideoCommon::HostBindings<Buffer>& bindings);
// TODO: implement
void BindTransformFeedbackBuffer(u32 index, MTLBuffer_t buffer, u32 offset, u32 size) {}
void BindTransformFeedbackBuffer(u32 index, MTL::Buffer* buffer, u32 offset, u32 size) {}
// TODO: implement
void BindTransformFeedbackBuffers(VideoCommon::HostBindings<Buffer>& bindings) {}
@ -138,11 +138,11 @@ public:
return ref.mapped_span;
}
void BindUniformBuffer(MTLBuffer_t buffer, u32 offset, u32 size) {
void BindUniformBuffer(MTL::Buffer* buffer, u32 offset, u32 size) {
BindBuffer(buffer, offset, size);
}
void BindStorageBuffer(MTLBuffer_t buffer, u32 offset, u32 size,
void BindStorageBuffer(MTL::Buffer* buffer, u32 offset, u32 size,
[[maybe_unused]] bool is_written) {
BindBuffer(buffer, offset, size);
}
@ -152,21 +152,21 @@ public:
VideoCore::Surface::PixelFormat format) {}
private:
void BindBuffer(MTLBuffer_t buffer, u32 offset, u32 size) {
void BindBuffer(MTL::Buffer* buffer, u32 offset, u32 size) {
// FIXME: what should be the index?
bound_buffers[0] = BoundBuffer(buffer, offset, size);
}
void ReserveNullBuffer();
MTLBuffer_t CreateNullBuffer();
MTL::Buffer* CreateNullBuffer();
const Device& device;
CommandRecorder& command_recorder;
StagingBufferPool& staging_pool;
// Common buffers
MTLBuffer_t null_buffer = nil;
MTLBuffer_t quad_index_buffer = nil;
MTL::Buffer* null_buffer = nil;
MTL::Buffer* quad_index_buffer = nil;
// TODO: probably move this into a separate class
// Bound state

View File

@ -12,10 +12,10 @@ CommandRecorder::CommandRecorder(const Device& device_) : device(device_) {}
CommandRecorder::~CommandRecorder() = default;
void CommandRecorder::BeginRenderPass(MTLRenderPassDescriptor* render_pass_descriptor) {
void CommandRecorder::BeginRenderPass(MTL::RenderPassDescriptor* render_pass_descriptor) {
RequireCommandBuffer();
EndEncoding();
encoder = [command_buffer renderCommandEncoderWithDescriptor:render_pass_descriptor];
encoder = command_buffer->renderCommandEncoder(render_pass_descriptor);
encoder_type = EncoderType::Render;
}
@ -23,7 +23,7 @@ void CommandRecorder::RequireComputeEncoder() {
RequireCommandBuffer();
if (!encoder || encoder_type != EncoderType::Compute) {
EndEncoding();
encoder = [command_buffer computeCommandEncoder];
encoder = command_buffer->computeCommandEncoder();
encoder_type = EncoderType::Compute;
}
}
@ -32,28 +32,28 @@ void CommandRecorder::RequireBlitEncoder() {
RequireCommandBuffer();
if (!encoder || encoder_type != EncoderType::Blit) {
EndEncoding();
encoder = [command_buffer blitCommandEncoder];
encoder = command_buffer->blitCommandEncoder();
encoder_type = EncoderType::Blit;
}
}
void CommandRecorder::EndEncoding() {
if (encoder) {
[encoder endEncoding];
encoder->endEncoding();
//[encoder release];
encoder = nil;
}
}
void CommandRecorder::Present(CAMetalDrawable_t drawable) {
void CommandRecorder::Present(CA::MetalDrawable* drawable) {
EndEncoding();
[command_buffer presentDrawable:drawable];
command_buffer->presentDrawable(drawable);
}
void CommandRecorder::Submit() {
if (command_buffer) {
EndEncoding();
[command_buffer commit];
command_buffer->commit();
//[command_buffer release];
command_buffer = nil;
}
@ -61,7 +61,7 @@ void CommandRecorder::Submit() {
void CommandRecorder::RequireCommandBuffer() {
if (!command_buffer) {
command_buffer = [device.GetCommandQueue() commandBuffer];
command_buffer = device.GetCommandQueue()->commandBuffer();
}
}

View File

@ -3,8 +3,8 @@
#pragma once
#include "objc_bridge.h"
#include "video_core/renderer_metal/objc_bridge.h"
#include <Metal/Metal.hpp>
#include <QuartzCore/QuartzCore.hpp>
namespace Metal {
@ -17,7 +17,7 @@ public:
CommandRecorder(const Device& device_);
~CommandRecorder();
void BeginRenderPass(MTLRenderPassDescriptor* render_pass_descriptor);
void BeginRenderPass(MTL::RenderPassDescriptor* render_pass_descriptor);
void CheckIfRenderPassIsActive() {
if (!encoder || encoder_type != EncoderType::Render) {
@ -32,15 +32,15 @@ public:
void EndEncoding();
void Present(CAMetalDrawable_t drawable);
void Present(CA::MetalDrawable* drawable);
void Submit();
MTLCommandBuffer_t GetCommandBuffer() {
MTL::CommandBuffer* GetCommandBuffer() {
return command_buffer;
}
MTLCommandEncoder_t GetCommandEncoder() {
MTL::CommandEncoder* GetCommandEncoder() {
return encoder;
}
@ -49,8 +49,8 @@ private:
// HACK: Command buffers and encoders currently aren't released every frame due to Xcode
// crashing in Debug mode. This leads to memory leaks
MTLCommandBuffer_t command_buffer = nil;
MTLCommandEncoder_t encoder = nil;
MTL::CommandBuffer* command_buffer = nil;
MTL::CommandEncoder* encoder = nil;
EncoderType encoder_type;

View File

@ -6,19 +6,19 @@
namespace Metal {
Device::Device() {
device = MTLCreateSystemDefaultDevice();
device = MTL::CreateSystemDefaultDevice();
if (!device) {
throw std::runtime_error("Failed to create Metal device");
}
command_queue = [device newCommandQueue];
command_queue = device->newCommandQueue();
if (!command_queue) {
throw std::runtime_error("Failed to create Metal command queue");
}
}
Device::~Device() {
[command_queue release];
[device release];
command_queue->release();
device->release();
}
} // namespace Metal

View File

@ -3,7 +3,9 @@
#pragma once
#include "video_core/renderer_metal/objc_bridge.h"
#include <Foundation/Foundation.hpp>
#include <Metal/Metal.hpp>
#include <QuartzCore/QuartzCore.hpp>
namespace Metal {
@ -12,17 +14,17 @@ public:
explicit Device();
~Device();
MTLDevice_t GetDevice() const {
MTL::Device* GetDevice() const {
return device;
}
MTLCommandQueue_t GetCommandQueue() const {
MTL::CommandQueue* GetCommandQueue() const {
return command_queue;
}
private:
MTLDevice_t device;
MTLCommandQueue_t command_queue;
MTL::Device* device;
MTL::CommandQueue* command_queue;
};
} // namespace Metal

View File

@ -18,18 +18,18 @@
namespace Metal {
StagingBufferRef::StagingBufferRef(MTLBuffer_t buffer_, size_t offset_, std::span<u8> mapped_span_)
: buffer{[buffer_ retain]}, offset{offset_}, mapped_span{mapped_span_} {}
StagingBufferRef::StagingBufferRef(MTL::Buffer* buffer_, size_t offset_, std::span<u8> mapped_span_)
: buffer{buffer_->retain()}, offset{offset_}, mapped_span{mapped_span_} {}
StagingBufferRef::~StagingBufferRef() {
[buffer release];
buffer->release();
}
StagingBuffer::StagingBuffer(MTLBuffer_t buffer_, std::span<u8> mapped_span_)
: buffer{[buffer_ retain]}, mapped_span{mapped_span_} {}
StagingBuffer::StagingBuffer(MTL::Buffer* buffer_, std::span<u8> mapped_span_)
: buffer{buffer_->retain()}, mapped_span{mapped_span_} {}
StagingBuffer::~StagingBuffer() {
[buffer release];
buffer->release();
}
StagingBufferRef StagingBuffer::Ref() const noexcept {
@ -37,13 +37,13 @@ StagingBufferRef StagingBuffer::Ref() const noexcept {
}
// TODO: use the _MiB suffix
constexpr size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024;//128_MiB;
constexpr size_t STREAM_BUFFER_SIZE = 128 * 1024 * 1024; // 128_MiB;
constexpr size_t REGION_SIZE = STREAM_BUFFER_SIZE / StagingBufferPool::NUM_SYNCS;
StagingBufferPool::StagingBufferPool(const Device& device_, CommandRecorder& command_recorder_)
: device{device_}, command_recorder{command_recorder_} {
stream_buffer = [device.GetDevice() newBufferWithLength:STREAM_BUFFER_SIZE
options:MTLResourceStorageModeShared];
stream_buffer =
device.GetDevice()->newBuffer(STREAM_BUFFER_SIZE, MTL::ResourceStorageModeShared);
}
StagingBufferPool::~StagingBufferPool() = default;
@ -57,7 +57,7 @@ StagingBufferRef StagingBufferPool::Request(size_t size, MemoryUsage usage, bool
}
void StagingBufferPool::FreeDeferred(StagingBufferRef& ref) {
// TODO: implement this
// TODO: implement this
}
void StagingBufferPool::TickFrame() {
@ -83,10 +83,9 @@ StagingBufferRef StagingBufferPool::GetStagingBuffer(size_t size, MemoryUsage us
StagingBufferRef StagingBufferPool::CreateStagingBuffer(size_t size, MemoryUsage usage,
bool deferred) {
const u32 log2 = Common::Log2Ceil64(size);
MTLBuffer_t buffer = [device.GetDevice() newBufferWithLength:size
options:MTLResourceStorageModeShared];
MTL::Buffer* buffer = device.GetDevice()->newBuffer(size, MTL::ResourceStorageModeShared);
// TODO: check if the mapped span is correct
std::span<u8> mapped_span(static_cast<u8*>([buffer contents]), size);
std::span<u8> mapped_span(static_cast<u8*>(buffer->contents()), size);
auto& entry = GetCache(usage)[log2].entries.emplace_back(buffer, mapped_span);
return entry.Ref();

View File

@ -7,10 +7,9 @@
#include <span>
#include <vector>
#include "common/common_types.h"
#include <Metal/Metal.hpp>
#include "objc_bridge.h"
#include "video_core/renderer_metal/objc_bridge.h"
#include "common/common_types.h"
namespace Metal {
@ -24,19 +23,19 @@ enum class MemoryUsage {
};
struct StagingBufferRef {
StagingBufferRef(MTLBuffer_t buffer_, size_t offset_, std::span<u8> mapped_span_);
StagingBufferRef(MTL::Buffer* buffer_, size_t offset_, std::span<u8> mapped_span_);
~StagingBufferRef();
MTLBuffer_t buffer;
MTL::Buffer* buffer;
size_t offset;
std::span<u8> mapped_span;
};
struct StagingBuffer {
StagingBuffer(MTLBuffer_t buffer_, std::span<u8> mapped_span_);
StagingBuffer(MTL::Buffer* buffer_, std::span<u8> mapped_span_);
~StagingBuffer();
MTLBuffer_t buffer;
MTL::Buffer* buffer;
std::span<u8> mapped_span;
StagingBufferRef Ref() const noexcept;
@ -52,7 +51,7 @@ public:
StagingBufferRef Request(size_t size, MemoryUsage usage, bool deferred = false);
void FreeDeferred(StagingBufferRef& ref);
[[nodiscard]] MTLBuffer_t GetSTreamBufferHandle() const noexcept {
[[nodiscard]] MTL::Buffer* GetSTreamBufferHandle() const noexcept {
return stream_buffer;
}
@ -83,7 +82,7 @@ private:
const Device& device;
CommandRecorder& command_recorder;
MTLBuffer_t stream_buffer{};
MTL::Buffer* stream_buffer{};
size_t iterator = 0;
size_t used_iterator = 0;

View File

@ -8,28 +8,28 @@
namespace Metal {
SwapChain::SwapChain(const Device& device_, CommandRecorder& command_recorder_,
const CAMetalLayer* layer_)
: device(device_), command_recorder(command_recorder_), layer([layer_ retain]) {
CA::MetalLayer* layer_)
: device(device_), command_recorder(command_recorder_), layer(layer_->retain()) {
// Give the layer our device
layer.device = device.GetDevice();
layer->setDevice(device.GetDevice());
}
SwapChain::~SwapChain() {
[layer release];
layer->release();
}
void SwapChain::AcquireNextDrawable() {
// Get the next drawable
drawable = [[layer nextDrawable] retain];
drawable = layer->nextDrawable()->retain();
}
void SwapChain::Present() {
command_recorder.Present(drawable);
[drawable release];
drawable->release();
}
MTLTexture_t SwapChain::GetDrawableTexture() {
return drawable.texture;
MTL::Texture* SwapChain::GetDrawableTexture() {
return drawable->texture();
}
} // namespace Metal

View File

@ -3,7 +3,8 @@
#pragma once
#include "video_core/renderer_metal/objc_bridge.h"
#include <Metal/Metal.hpp>
#include <QuartzCore/QuartzCore.hpp>
namespace Metal {
@ -12,8 +13,7 @@ class CommandRecorder;
class SwapChain {
public:
SwapChain(const Device& device_, CommandRecorder& command_recorder_,
const CAMetalLayer* layer_);
SwapChain(const Device& device_, CommandRecorder& command_recorder_, CA::MetalLayer* layer_);
~SwapChain();
void AcquireNextDrawable();
@ -21,14 +21,14 @@ public:
void Present();
// Can only be called between AcquireNextDrawable and Present
MTLTexture_t GetDrawableTexture();
MTL::Texture* GetDrawableTexture();
private:
const Device& device;
CommandRecorder& command_recorder;
const CAMetalLayer* layer;
CA::MetalLayer* layer;
CAMetalDrawable_t drawable = nil;
CA::MetalDrawable* drawable = nil;
};
} // namespace Metal

View File

@ -3,9 +3,9 @@
#include <algorithm>
#include <array>
#include <boost/container/small_vector.hpp>
#include <span>
#include <vector>
#include <boost/container/small_vector.hpp>
#include "common/bit_cast.h"
#include "common/bit_util.h"
@ -37,8 +37,7 @@ using VideoCore::Surface::SurfaceType;
TextureCacheRuntime::TextureCacheRuntime(const Device& device_, CommandRecorder& command_recorder_,
StagingBufferPool& staging_buffer_pool_)
: device{device_}, command_recorder{command_recorder_},
staging_buffer_pool{staging_buffer_pool_},
resolution{Settings::values.resolution_info} {}
staging_buffer_pool{staging_buffer_pool_}, resolution{Settings::values.resolution_info} {}
void TextureCacheRuntime::TickFrame() {}
@ -54,30 +53,28 @@ void TextureCacheRuntime::FreeDeferredStagingBuffer(StagingBufferRef& ref) {
staging_buffer_pool.FreeDeferred(ref);
}
Image::Image(TextureCacheRuntime& runtime, const ImageInfo& info,
GPUVAddr gpu_addr_, VAddr cpu_addr_)
Image::Image(TextureCacheRuntime& runtime, const ImageInfo& info, GPUVAddr gpu_addr_,
VAddr cpu_addr_)
: VideoCommon::ImageBase(info, gpu_addr_, cpu_addr_) {
MTLTextureDescriptor *texture_descriptor =
[[MTLTextureDescriptor alloc] init];
MTL::TextureDescriptor* texture_descriptor = MTL::TextureDescriptor::alloc()->init();
// TODO: don't hardcode the format
texture_descriptor.pixelFormat = MTLPixelFormatRGBA8Unorm;
texture_descriptor.width = info.size.width;
texture_descriptor.height = info.size.height;
texture_descriptor->setPixelFormat(MTL::PixelFormatRGBA8Unorm);
texture_descriptor->setWidth(info.size.width);
texture_descriptor->setHeight(info.size.height);
texture =
[runtime.device.GetDevice() newTextureWithDescriptor:texture_descriptor];
texture = runtime.device.GetDevice()->newTexture(texture_descriptor);
}
Image::Image(const VideoCommon::NullImageParams& params) : VideoCommon::ImageBase{params} {}
Image::~Image() {
if (texture) {
[texture release];
texture->release();
}
}
// TODO: implement these
void Image::UploadMemory(MTLBuffer_t buffer, size_t offset,
void Image::UploadMemory(MTL::Buffer* buffer, size_t offset,
std::span<const VideoCommon::BufferImageCopy> copies) {
;
}
@ -87,38 +84,33 @@ void Image::UploadMemory(const StagingBufferRef& map,
;
}
void Image::DownloadMemory(MTLBuffer_t buffer, size_t offset,
void Image::DownloadMemory(MTL::Buffer* buffer, size_t offset,
std::span<const VideoCommon::BufferImageCopy> copies) {
;
}
// TODO: uncomment
/*
void Image::DownloadMemory(std::span<MTLBuffer_t> buffers, std::span<size_t> offsets,
void Image::DownloadMemory(std::span<MTL::Buffer*> buffers, std::span<size_t> offsets,
std::span<const VideoCommon::BufferImageCopy> copies) {
;
}
*/
void Image::DownloadMemory(const StagingBufferRef& map,
std::span<const VideoCommon::BufferImageCopy> copies) {
;
}
ImageView::ImageView(TextureCacheRuntime& runtime,
const VideoCommon::ImageViewInfo& info, ImageId image_id_,
Image& image)
ImageView::ImageView(TextureCacheRuntime& runtime, const VideoCommon::ImageViewInfo& info,
ImageId image_id_, Image& image)
: VideoCommon::ImageViewBase{info, image.info, image_id_, image.gpu_addr} {
using Shader::TextureType;
texture = [image.GetHandle() retain];
texture = image.GetHandle()->retain();
// TODO: create texture view
}
ImageView::ImageView(TextureCacheRuntime& runtime,
const VideoCommon::ImageViewInfo& info, ImageId image_id_,
Image& image, const SlotVector<Image>& slot_imgs)
ImageView::ImageView(TextureCacheRuntime& runtime, const VideoCommon::ImageViewInfo& info,
ImageId image_id_, Image& image, const SlotVector<Image>& slot_imgs)
: ImageView(runtime, info, image_id_, image) {
// TODO: save slot images
}
@ -134,52 +126,51 @@ ImageView::ImageView(TextureCacheRuntime& runtime, const VideoCommon::NullImageV
// TODO: implement
}
ImageView::~ImageView() { [texture release]; }
ImageView::~ImageView() {
texture->release();
}
Sampler::Sampler(TextureCacheRuntime& runtime,
const Tegra::Texture::TSCEntry& tsc) {
MTLSamplerDescriptor* sampler_descriptor =
[[MTLSamplerDescriptor alloc] init];
Sampler::Sampler(TextureCacheRuntime& runtime, const Tegra::Texture::TSCEntry& tsc) {
MTL::SamplerDescriptor* sampler_descriptor = MTL::SamplerDescriptor::alloc()->init();
// TODO: configure the descriptor
sampler_state = [runtime.device.GetDevice()
newSamplerStateWithDescriptor:sampler_descriptor];
sampler_state = runtime.device.GetDevice()->newSamplerState(sampler_descriptor);
}
Framebuffer::Framebuffer(TextureCacheRuntime& runtime,
std::span<ImageView*, NUM_RT> color_buffers,
ImageView* depth_buffer,
const VideoCommon::RenderTargets& key) {
CreateRenderPassDescriptor(runtime, color_buffers, depth_buffer,
key.is_rescaled, key.size.width, key.size.height);
Framebuffer::Framebuffer(TextureCacheRuntime& runtime, std::span<ImageView*, NUM_RT> color_buffers,
ImageView* depth_buffer, const VideoCommon::RenderTargets& key) {
CreateRenderPassDescriptor(runtime, color_buffers, depth_buffer, key.is_rescaled,
key.size.width, key.size.height);
}
Framebuffer::~Framebuffer() = default;
void Framebuffer::CreateRenderPassDescriptor(
TextureCacheRuntime& runtime, std::span<ImageView*, NUM_RT> color_buffers,
ImageView* depth_buffer, bool is_rescaled, size_t width, size_t height) {
render_pass = [MTLRenderPassDescriptor renderPassDescriptor];
void Framebuffer::CreateRenderPassDescriptor(TextureCacheRuntime& runtime,
std::span<ImageView*, NUM_RT> color_buffers,
ImageView* depth_buffer, bool is_rescaled,
size_t width, size_t height) {
render_pass = MTL::RenderPassDescriptor::alloc()->init();
for (size_t index = 0; index < NUM_RT; ++index) {
const ImageView* const color_buffer = color_buffers[index];
if (!color_buffer) {
continue;
continue;
}
// TODO: don't use index as attachment index
render_pass.colorAttachments[index].clearColor =
MTLClearColorMake(0.5, 1.0, 0.0, 1.0);
render_pass.colorAttachments[index].loadAction = MTLLoadActionClear;
render_pass.colorAttachments[index].storeAction = MTLStoreActionStore;
render_pass.colorAttachments[index].texture = color_buffer->GetHandle();
auto color_attachment = render_pass->colorAttachments()->object(index);
color_attachment->setClearColor(MTL::ClearColor::Make(0.5, 1.0, 0.0, 1.0));
color_attachment->setLoadAction(MTL::LoadActionClear);
color_attachment->setStoreAction(MTL::StoreActionStore);
color_attachment->setTexture(color_buffer->GetHandle());
}
if (depth_buffer) {
render_pass.depthAttachment.clearDepth = 1.0;
render_pass.depthAttachment.loadAction = MTLLoadActionClear;
render_pass.depthAttachment.storeAction = MTLStoreActionStore;
render_pass.depthAttachment.texture = depth_buffer->GetHandle();
auto depth_attachment = render_pass->depthAttachment();
depth_attachment->setClearDepth(1.0);
depth_attachment->setLoadAction(MTL::LoadActionClear);
depth_attachment->setStoreAction(MTL::StoreActionStore);
depth_attachment->setTexture(depth_buffer->GetHandle());
}
}
} // namespace Vulkan
} // namespace Metal

View File

@ -5,12 +5,13 @@
#include <span>
#include <Metal/Metal.hpp>
#include "mtl_staging_buffer_pool.h"
#include "video_core/texture_cache/texture_cache_base.h"
#include "shader_recompiler/shader_info.h"
#include "video_core/renderer_metal/mtl_staging_buffer_pool.h"
#include "video_core/renderer_metal/objc_bridge.h"
#include "video_core/texture_cache/image_view_base.h"
namespace Settings {
@ -127,21 +128,17 @@ public:
Image(Image&&) = default;
Image& operator=(Image&&) = default;
void UploadMemory(MTLBuffer_t buffer, size_t offset,
void UploadMemory(MTL::Buffer* buffer, size_t offset,
std::span<const VideoCommon::BufferImageCopy> copies);
void UploadMemory(const StagingBufferRef& map,
std::span<const VideoCommon::BufferImageCopy> copies);
void DownloadMemory(MTLBuffer_t buffer, size_t offset,
void DownloadMemory(MTL::Buffer* buffer, size_t offset,
std::span<const VideoCommon::BufferImageCopy> copies);
// For some reason, this function cannot be defined in the .mm file since it would report
// undefined symbols
void DownloadMemory(std::span<MTLBuffer_t> buffers, std::span<size_t> offsets,
std::span<const VideoCommon::BufferImageCopy> copies) {
// TODO: implement
}
void DownloadMemory(std::span<MTL::Buffer*> buffers, std::span<size_t> offsets,
std::span<const VideoCommon::BufferImageCopy> copies);
void DownloadMemory(const StagingBufferRef& map,
std::span<const VideoCommon::BufferImageCopy> copies);
@ -160,12 +157,12 @@ public:
return true;
}
MTLTexture_t GetHandle() const noexcept {
MTL::Texture* GetHandle() const noexcept {
return texture;
}
private:
MTLTexture_t texture = nil;
MTL::Texture* texture = nil;
bool initialized = false;
bool rescaled = false;
@ -188,12 +185,12 @@ public:
ImageView(ImageView&&) = default;
ImageView& operator=(ImageView&&) = default;
MTLTexture_t GetHandle() const noexcept {
MTL::Texture* GetHandle() const noexcept {
return texture;
}
private:
MTLTexture_t texture;
MTL::Texture* texture;
};
class ImageAlloc : public VideoCommon::ImageAllocBase {};
@ -202,12 +199,12 @@ class Sampler {
public:
explicit Sampler(TextureCacheRuntime&, const Tegra::Texture::TSCEntry&);
MTLSamplerState_t GetHandle() const noexcept {
MTL::SamplerState* GetHandle() const noexcept {
return sampler_state;
}
private:
MTLSamplerState_t sampler_state;
MTL::SamplerState* sampler_state;
};
class Framebuffer {
@ -227,12 +224,12 @@ public:
ImageView* depth_buffer, bool is_rescaled, size_t width,
size_t height);
MTLRenderPassDescriptor* GetHandle() const noexcept {
MTL::RenderPassDescriptor* GetHandle() const noexcept {
return render_pass;
}
private:
MTLRenderPassDescriptor* render_pass{};
MTL::RenderPassDescriptor* render_pass{};
};
struct TextureCacheParams {
@ -249,7 +246,7 @@ struct TextureCacheParams {
using Sampler = Metal::Sampler;
using Framebuffer = Metal::Framebuffer;
using AsyncBuffer = Metal::StagingBufferRef;
using BufferType = MTLBuffer_t;
using BufferType = MTL::Buffer*;
};
using TextureCache = VideoCommon::TextureCache<TextureCacheParams>;

View File

@ -1,29 +0,0 @@
// SPDX-FileCopyrightText: Copyright 2024 suyu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#ifdef __OBJC__
#import <Metal/Metal.h>
#import <QuartzCore/QuartzCore.h>
typedef id<MTLDevice> MTLDevice_t;
typedef id<MTLCommandQueue> MTLCommandQueue_t;
typedef id<MTLCommandBuffer> MTLCommandBuffer_t;
typedef id<MTLCommandEncoder> MTLCommandEncoder_t;
typedef id<MTLBuffer> MTLBuffer_t;
typedef id<MTLTexture> MTLTexture_t;
typedef id<MTLSamplerState> MTLSamplerState_t;
typedef id<CAMetalDrawable> CAMetalDrawable_t;
#else
typedef void* MTLDevice_t;
typedef void* MTLCommandQueue_t;
typedef void* MTLCommandBuffer_t;
typedef void* MTLCommandEncoder_t;
typedef void* MTLBuffer_t;
typedef void* MTLTexture_t;
typedef void* MTLSamplerState_t;
typedef void MTLRenderPassDescriptor;
typedef void CAMetalLayer;
typedef void* CAMetalDrawable_t;
#define nil NULL
#endif

View File

@ -4,19 +4,18 @@
#include "core/frontend/emu_window.h"
#include "core/frontend/graphics_context.h"
#include "video_core/capture.h"
#include "video_core/renderer_metal/renderer_metal.h"
#include "video_core/renderer_metal/mtl_device.h"
#include "video_core/renderer_metal/renderer_metal.h"
namespace Metal {
RendererMetal::RendererMetal(Core::Frontend::EmuWindow& emu_window,
Tegra::MaxwellDeviceMemoryManager& device_memory_, Tegra::GPU& gpu_,
std::unique_ptr<Core::Frontend::GraphicsContext> context_)
: RendererBase(emu_window, std::move(context_)), device_memory{device_memory_},
gpu{gpu_}, device{},
command_recorder(device),
: RendererBase(emu_window, std::move(context_)), device_memory{device_memory_}, gpu{gpu_},
device{}, command_recorder(device),
swap_chain(device, command_recorder,
static_cast<const CAMetalLayer*>(render_window.GetWindowInfo().render_surface)),
static_cast<CA::MetalLayer*>(render_window.GetWindowInfo().render_surface)),
rasterizer(gpu_, device_memory, device, command_recorder, swap_chain) {}
RendererMetal::~RendererMetal() = default;
@ -30,11 +29,13 @@ void RendererMetal::Composite(std::span<const Tegra::FramebufferConfig> framebuf
swap_chain.AcquireNextDrawable();
// TODO: copy the framebuffer to the drawable texture instead of this dummy render pass
MTLRenderPassDescriptor* render_pass_descriptor = [MTLRenderPassDescriptor renderPassDescriptor];
render_pass_descriptor.colorAttachments[0].clearColor = MTLClearColorMake(1.0, 0.5, 0.0, 1.0);
render_pass_descriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
render_pass_descriptor.colorAttachments[0].storeAction = MTLStoreActionStore;
render_pass_descriptor.colorAttachments[0].texture = swap_chain.GetDrawableTexture();
MTL::RenderPassDescriptor* render_pass_descriptor = MTL::RenderPassDescriptor::alloc()->init();
render_pass_descriptor->colorAttachments()->object(0)->setClearColor(
MTL::ClearColor::Make(1.0, 0.5, 0.0, 1.0));
render_pass_descriptor->colorAttachments()->object(0)->setLoadAction(MTL::LoadActionClear);
render_pass_descriptor->colorAttachments()->object(0)->setStoreAction(MTL::StoreActionStore);
render_pass_descriptor->colorAttachments()->object(0)->setTexture(
swap_chain.GetDrawableTexture());
command_recorder.BeginRenderPass(render_pass_descriptor);

View File

@ -6,7 +6,6 @@
#include <memory>
#include <string>
#include "objc_bridge.h"
#include "video_core/host1x/gpu_device_memory_manager.h"
#include "video_core/renderer_base.h"
#include "video_core/renderer_metal/mtl_command_recorder.h"