From 0a2227d1f8e634faf5e08436cba73c85fdf42afc Mon Sep 17 00:00:00 2001 From: Samuliak Date: Tue, 9 Apr 2024 15:23:44 +0200 Subject: [PATCH] metal: bind and sample from texture --- .../renderer_metal/mtl_graphics_pipeline.cpp | 51 +++++++++++++++++++ .../renderer_metal/mtl_pipeline_cache.cpp | 6 ++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/video_core/renderer_metal/mtl_graphics_pipeline.cpp b/src/video_core/renderer_metal/mtl_graphics_pipeline.cpp index 626b0221a6..819e327897 100644 --- a/src/video_core/renderer_metal/mtl_graphics_pipeline.cpp +++ b/src/video_core/renderer_metal/mtl_graphics_pipeline.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include +#include #include #include @@ -70,6 +71,56 @@ void GraphicsPipeline::Configure(bool is_indexed) { command_recorder.BeginOrContinueRenderPass(framebuffer->GetHandle()); command_recorder.GetRenderCommandEncoder()->setRenderPipelineState(pipeline_state); + + // Bind resources + + // HACK: try to find a texture that we can bind + size_t stage = 4; + // const auto& cbufs{maxwell3d->state.shader_stages[stage].const_buffers}; + const auto read_handle{[&](const auto& desc, u32 index) { + /* + ASSERT(cbufs[desc.cbuf_index].enabled); + const u32 index_offset{index << desc.size_shift}; + const u32 offset{desc.cbuf_offset + index_offset}; + const GPUVAddr addr{cbufs[desc.cbuf_index].address + offset}; + if constexpr (std::is_same_v || + std::is_same_v) { + if (desc.has_secondary) { + ASSERT(cbufs[desc.secondary_cbuf_index].enabled); + const u32 second_offset{desc.secondary_cbuf_offset + index_offset}; + const GPUVAddr separate_addr{cbufs[desc.secondary_cbuf_index].address + + second_offset}; + const u32 lhs_raw{gpu_memory->Read(addr) << desc.shift_left}; + const u32 rhs_raw{gpu_memory->Read(separate_addr) + << desc.secondary_shift_left}; + const u32 raw{lhs_raw | rhs_raw}; + return TexturePair(raw, false); + } + } + */ + // HACK: hardcode the texture address + return TexturePair(/*gpu_memory->Read(addr)*/ 310378932, false); + }}; + + const Shader::Info& info{stage_infos[stage]}; + std::array views; + size_t view_index{}; + for (const auto& desc : info.texture_descriptors) { + for (u32 index = 0; index < desc.count; ++index) { + const auto handle{read_handle(desc, index)}; + views[view_index++] = { + .index = handle.first, + .blacklist = false, + .id = {}, + }; + } + } + texture_cache.FillGraphicsImageViews(std::span(views.data(), view_index)); + VideoCommon::ImageViewInOut* texture_buffer_it{views.data()}; + + ImageView& image_view{texture_cache.GetImageView(texture_buffer_it->id)}; + + command_recorder.GetRenderCommandEncoder()->setFragmentTexture(image_view.GetHandle(), 0); } void GraphicsPipeline::MakePipeline(MTL::RenderPassDescriptor* render_pass) { diff --git a/src/video_core/renderer_metal/mtl_pipeline_cache.cpp b/src/video_core/renderer_metal/mtl_pipeline_cache.cpp index 116d44698f..2681277e12 100644 --- a/src/video_core/renderer_metal/mtl_pipeline_cache.cpp +++ b/src/video_core/renderer_metal/mtl_pipeline_cache.cpp @@ -298,8 +298,10 @@ std::unique_ptr PipelineCache::CreateGraphicsPipeline( return out; } - fragment float4 fragmentMain(VertexOut in [[stage_in]]) { - return float4(in.texCoord, 0.0, 1.0); + fragment float4 fragmentMain(VertexOut in [[stage_in]], texture2d tex [[texture(0)]]) { + constexpr sampler samplr(mip_filter::linear, mag_filter::linear, min_filter::linear); + + return tex.sample(samplr, in.texCoord); } )", NS::ASCIIStringEncoding),