From 863842948bd096f59c9a0f19c99bc71a7b34d1dc Mon Sep 17 00:00:00 2001 From: Samuliak Date: Sat, 6 Apr 2024 12:39:24 +0200 Subject: [PATCH] metal: use texture cache to create textures render passes --- .../renderer_metal/mtl_command_recorder.mm | 4 +-- .../renderer_metal/mtl_rasterizer.mm | 25 ++++++++++++++++--- .../renderer_metal/mtl_swap_chain.h | 1 + .../renderer_metal/mtl_swap_chain.mm | 8 ++---- .../renderer_metal/renderer_metal.mm | 1 + 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/video_core/renderer_metal/mtl_command_recorder.mm b/src/video_core/renderer_metal/mtl_command_recorder.mm index 96d9451de9..00b508a9cf 100644 --- a/src/video_core/renderer_metal/mtl_command_recorder.mm +++ b/src/video_core/renderer_metal/mtl_command_recorder.mm @@ -40,7 +40,7 @@ void CommandRecorder::RequireBlitEncoder() { void CommandRecorder::EndEncoding() { if (encoder) { [encoder endEncoding]; - [encoder release]; + //[encoder release]; encoder = nil; } } @@ -53,7 +53,7 @@ void CommandRecorder::Submit() { if (command_buffer) { EndEncoding(); [command_buffer commit]; - [command_buffer release]; + //[command_buffer release]; command_buffer = nil; } } diff --git a/src/video_core/renderer_metal/mtl_rasterizer.mm b/src/video_core/renderer_metal/mtl_rasterizer.mm index 0d9f0cd752..e45eaee48a 100644 --- a/src/video_core/renderer_metal/mtl_rasterizer.mm +++ b/src/video_core/renderer_metal/mtl_rasterizer.mm @@ -43,7 +43,7 @@ void RasterizerMetal::Draw(bool is_indexed, u32 instance_count) { //command_recorder.CheckIfRenderPassIsActive(); //const auto& draw_state = maxwell3d->draw_manager->GetDrawState(); if (is_indexed) { - std::cout << "DrawIndexed" << std::endl; + std::cout << "Draw indexed" << std::endl; /*[command_buffer drawIndexedPrimitives:MTLPrimitiveTypeTriangle indexCount:draw_params.num_indices indexType:MTLIndexTypeUInt32 @@ -61,8 +61,24 @@ void RasterizerMetal::Draw(bool is_indexed, u32 instance_count) { // draw_params.base_vertex, draw_params.base_instance); } } -void RasterizerMetal::DrawTexture() {} -void RasterizerMetal::Clear(u32 layer_count) {} + +void RasterizerMetal::DrawTexture() { + std::cout << "Draw texture" << std::endl; +} + +void RasterizerMetal::Clear(u32 layer_count) { + std::cout << "Clear" << std::endl; + + texture_cache.UpdateRenderTargets(true); + const Framebuffer* const framebuffer = texture_cache.GetFramebuffer(); + if (!framebuffer) { + return; + } + + // Begin render pass + command_recorder.BeginRenderPass(framebuffer->GetHandle()); +} + void RasterizerMetal::DispatchCompute() {} void RasterizerMetal::ResetCounter(VideoCommon::QueryType type) {} void RasterizerMetal::Query(GPUVAddr gpu_addr, VideoCommon::QueryType type, @@ -135,12 +151,15 @@ void RasterizerMetal::LoadDiskResources(u64 title_id, std::stop_token stop_loadi const VideoCore::DiskResourceLoadCallback& callback) {} void RasterizerMetal::InitializeChannel(Tegra::Control::ChannelState& channel) { CreateChannel(channel); + texture_cache.CreateChannel(channel); } void RasterizerMetal::BindChannel(Tegra::Control::ChannelState& channel) { BindToChannel(channel.bind_id); + texture_cache.BindToChannel(channel.bind_id); } void RasterizerMetal::ReleaseChannel(s32 channel_id) { EraseChannel(channel_id); + texture_cache.EraseChannel(channel_id); } } // namespace Metal diff --git a/src/video_core/renderer_metal/mtl_swap_chain.h b/src/video_core/renderer_metal/mtl_swap_chain.h index d184df84b6..864c5820b2 100644 --- a/src/video_core/renderer_metal/mtl_swap_chain.h +++ b/src/video_core/renderer_metal/mtl_swap_chain.h @@ -20,6 +20,7 @@ public: void Present(); + // Can only be called between AcquireNextDrawable and Present MTLTexture_t GetDrawableTexture(); private: diff --git a/src/video_core/renderer_metal/mtl_swap_chain.mm b/src/video_core/renderer_metal/mtl_swap_chain.mm index 7653d929c7..e699be254d 100644 --- a/src/video_core/renderer_metal/mtl_swap_chain.mm +++ b/src/video_core/renderer_metal/mtl_swap_chain.mm @@ -15,21 +15,17 @@ SwapChain::SwapChain(const Device& device_, CommandRecorder& command_recorder_, } SwapChain::~SwapChain() { - if (drawable) { - // TODO: should drawable be released? - [drawable release]; - } [layer release]; } void SwapChain::AcquireNextDrawable() { // Get the next drawable - drawable = [layer nextDrawable]; + drawable = [[layer nextDrawable] retain]; } void SwapChain::Present() { - command_recorder.EndEncoding(); command_recorder.Present(drawable); + [drawable release]; } MTLTexture_t SwapChain::GetDrawableTexture() { diff --git a/src/video_core/renderer_metal/renderer_metal.mm b/src/video_core/renderer_metal/renderer_metal.mm index 3dc4a76fc1..575e853de4 100644 --- a/src/video_core/renderer_metal/renderer_metal.mm +++ b/src/video_core/renderer_metal/renderer_metal.mm @@ -37,6 +37,7 @@ void RendererMetal::Composite(std::span framebuf render_pass_descriptor.colorAttachments[0].texture = swap_chain.GetDrawableTexture(); command_recorder.BeginRenderPass(render_pass_descriptor); + command_recorder.EndEncoding(); swap_chain.Present(); command_recorder.Submit();