From c8a717651c339be002fd3e107f8f5d72abe668d6 Mon Sep 17 00:00:00 2001 From: Samuliak Date: Sun, 7 Apr 2024 11:58:35 +0200 Subject: [PATCH] metal: create basic graphics pipeline --- src/video_core/CMakeLists.txt | 1 + .../renderer_metal/mtl_graphics_pipeline.cpp | 98 +++++++++++++ .../renderer_metal/mtl_graphics_pipeline.h | 135 ++++++++++++++++++ 3 files changed, 234 insertions(+) create mode 100644 src/video_core/renderer_metal/mtl_graphics_pipeline.cpp create mode 100644 src/video_core/renderer_metal/mtl_graphics_pipeline.h diff --git a/src/video_core/CMakeLists.txt b/src/video_core/CMakeLists.txt index 1c3ab968f7..c29bf6fc3d 100644 --- a/src/video_core/CMakeLists.txt +++ b/src/video_core/CMakeLists.txt @@ -379,6 +379,7 @@ if (APPLE) renderer_metal/mtl_buffer_cache_base.cpp renderer_metal/mtl_command_recorder.cpp renderer_metal/mtl_device.cpp + renderer_metal/mtl_graphics_pipeline.cpp renderer_metal/mtl_rasterizer.cpp renderer_metal/mtl_staging_buffer_pool.cpp renderer_metal/mtl_swap_chain.cpp diff --git a/src/video_core/renderer_metal/mtl_graphics_pipeline.cpp b/src/video_core/renderer_metal/mtl_graphics_pipeline.cpp new file mode 100644 index 0000000000..735d455bff --- /dev/null +++ b/src/video_core/renderer_metal/mtl_graphics_pipeline.cpp @@ -0,0 +1,98 @@ +// SPDX-FileCopyrightText: Copyright 2024 suyu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include + +#include +#include + +#include "video_core/renderer_metal/mtl_graphics_pipeline.h" + +#include "common/bit_field.h" +#include "video_core/buffer_cache/buffer_cache_base.h" +#include "video_core/engines/maxwell_3d.h" +#include "video_core/renderer_metal/mtl_command_recorder.h" +#include "video_core/renderer_metal/mtl_device.h" +#include "video_core/shader_notify.h" +#include "video_core/texture_cache/texture_cache_base.h" + +namespace Metal { +namespace { +using Tegra::Texture::TexturePair; +using VideoCommon::NUM_RT; +using VideoCore::Surface::PixelFormat; +using VideoCore::Surface::PixelFormatFromDepthFormat; +using VideoCore::Surface::PixelFormatFromRenderTargetFormat; +using Maxwell = Tegra::Engines::Maxwell3D::Regs; +} // Anonymous namespace + +GraphicsPipeline::GraphicsPipeline(const Device& device_, CommandRecorder& command_recorder_, + const GraphicsPipelineCacheKey& key_, BufferCache& buffer_cache_, + TextureCache& texture_cache_, + VideoCore::ShaderNotify* shader_notify, + std::array functions_, + const std::array& infos) + : device{device_}, command_recorder{command_recorder_}, key{key_}, buffer_cache{buffer_cache_}, + texture_cache{texture_cache_}, functions{functions_} { + if (shader_notify) { + shader_notify->MarkShaderBuilding(); + } + for (size_t stage = 0; stage < NUM_STAGES; ++stage) { + const Shader::Info* const info{infos[stage]}; + if (!info) { + continue; + } + stage_infos[stage] = *info; + } + Validate(); + // TODO: is the framebuffer available by this time? + Framebuffer* framebuffer = texture_cache.GetFramebuffer(); + if (!framebuffer) { + LOG_DEBUG(Render_Metal, "framebuffer not available"); + return; + } + MakePipeline(framebuffer->GetHandle()); + // configure_func = ConfigureFunc(functions, stage_infos); +} + +template +void GraphicsPipeline::ConfigureImpl(bool is_indexed) { + // TODO: implement +} + +void GraphicsPipeline::ConfigureDraw() { + Framebuffer* framebuffer = texture_cache.GetFramebuffer(); + if (!framebuffer) { + return; + } + // MTL::RenderPassDescriptor* render_pass = framebuffer->GetHandle(); + + // TODO: bind resources +} + +void GraphicsPipeline::MakePipeline(MTL::RenderPassDescriptor* render_pass) { + MTL::RenderPipelineDescriptor* pipeline_descriptor = + MTL::RenderPipelineDescriptor::alloc()->init(); + pipeline_descriptor->setVertexFunction(functions[0]); + pipeline_descriptor->setFragmentFunction(functions[1]); + // pipeline_descriptor->setVertexDescriptor(vertex_descriptor); + // TODO: get the attachment count from render pass descriptor + for (u32 index = 0; index < NUM_RT; index++) { + auto* render_pass_attachment = render_pass->colorAttachments()->object(index); + // TODO: is this the correct way to check if the attachment is valid? + if (!render_pass_attachment->texture()) { + continue; + } + + auto* color_attachment = pipeline_descriptor->colorAttachments()->object(index); + color_attachment->setPixelFormat(render_pass_attachment->texture()->pixelFormat()); + // TODO: provide blend information + } +} + +void GraphicsPipeline::Validate() { + // TODO: validate pipeline +} + +} // namespace Metal diff --git a/src/video_core/renderer_metal/mtl_graphics_pipeline.h b/src/video_core/renderer_metal/mtl_graphics_pipeline.h new file mode 100644 index 0000000000..d614995fea --- /dev/null +++ b/src/video_core/renderer_metal/mtl_graphics_pipeline.h @@ -0,0 +1,135 @@ +// SPDX-FileCopyrightText: Copyright 2024 suyu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include +#include +#include +#include +#include + +#include + +#include "common/thread_worker.h" +#include "shader_recompiler/shader_info.h" +#include "video_core/engines/maxwell_3d.h" +#include "video_core/renderer_metal/mtl_buffer_cache.h" +#include "video_core/renderer_metal/mtl_texture_cache.h" + +namespace VideoCore { +class ShaderNotify; +} + +namespace Metal { + +struct GraphicsPipelineCacheKey { + std::array unique_hashes; + // TODO: include fixed state + + size_t Hash() const noexcept; + + bool operator==(const GraphicsPipelineCacheKey& rhs) const noexcept; + + bool operator!=(const GraphicsPipelineCacheKey& rhs) const noexcept { + return !operator==(rhs); + } + + size_t Size() const noexcept { + return sizeof(unique_hashes); + } +}; +static_assert(std::has_unique_object_representations_v); +static_assert(std::is_trivially_copyable_v); +static_assert(std::is_trivially_constructible_v); + +} // namespace Metal + +namespace std { +template <> +struct hash { + size_t operator()(const Metal::GraphicsPipelineCacheKey& k) const noexcept { + return k.Hash(); + } +}; +} // namespace std + +namespace Metal { + +class Device; +class CommandRecorder; + +class GraphicsPipeline { + static constexpr size_t NUM_STAGES = Tegra::Engines::Maxwell3D::Regs::MaxShaderStage; + +public: + // TODO: accept render pass cache as an argument to provide info on color and depth attachments + explicit GraphicsPipeline(const Device& device_, CommandRecorder& command_recorder_, + const GraphicsPipelineCacheKey& key_, BufferCache& buffer_cache_, + TextureCache& texture_cache_, VideoCore::ShaderNotify* shader_notify, + std::array functions_, + const std::array& infos); + + GraphicsPipeline& operator=(GraphicsPipeline&&) noexcept = delete; + GraphicsPipeline(GraphicsPipeline&&) noexcept = delete; + + GraphicsPipeline& operator=(const GraphicsPipeline&) = delete; + GraphicsPipeline(const GraphicsPipeline&) = delete; + + // TODO: implement + void AddTransition(GraphicsPipeline* transition) {} + + void Configure(bool is_indexed) { + // configure_func(this, is_indexed); + } + + [[nodiscard]] GraphicsPipeline* Next(const GraphicsPipelineCacheKey& current_key) noexcept { + // TODO: implement + return nullptr; + } + + [[nodiscard]] bool IsBuilt() const noexcept { + return true; + } + + template + static auto MakeConfigureSpecFunc() { + return [](GraphicsPipeline* pl, bool is_indexed) { pl->ConfigureImpl(is_indexed); }; + } + + void SetEngine(Tegra::Engines::Maxwell3D* maxwell3d_, Tegra::MemoryManager* gpu_memory_) { + maxwell3d = maxwell3d_; + gpu_memory = gpu_memory_; + } + +private: + template + void ConfigureImpl(bool is_indexed); + + void ConfigureDraw(); + + void MakePipeline(MTL::RenderPassDescriptor* render_pass); + + void Validate(); + + const Device& device; + CommandRecorder& command_recorder; + const GraphicsPipelineCacheKey key; + Tegra::Engines::Maxwell3D* maxwell3d; + Tegra::MemoryManager* gpu_memory; + BufferCache& buffer_cache; + TextureCache& texture_cache; + + void (*configure_func)(GraphicsPipeline*, bool){}; + + std::array functions; + + std::array stage_infos; + // VideoCommon::UniformBufferSizes uniform_buffer_sizes{}; + // u32 num_textures{}; + + MTL::RenderPipelineState* pipeline_state{nullptr}; +}; + +} // namespace Metal