|
| 1 | +// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project |
| 2 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + |
| 4 | +#include "shader_recompiler/info.h" |
| 5 | +#include "video_core/renderer_vulkan/vk_scheduler.h" |
| 6 | +#include "video_core/renderer_vulkan/vk_shader_hle.h" |
| 7 | + |
| 8 | +#include "vk_rasterizer.h" |
| 9 | + |
| 10 | +namespace Vulkan { |
| 11 | + |
| 12 | +static constexpr u64 COPY_SHADER_HASH = 0xfefebf9f; |
| 13 | + |
| 14 | +bool ExecuteCopyShaderHLE(const Shader::Info& info, const AmdGpu::Liverpool::Regs& regs, |
| 15 | + Rasterizer& rasterizer) { |
| 16 | + auto& scheduler = rasterizer.GetScheduler(); |
| 17 | + auto& buffer_cache = rasterizer.GetBufferCache(); |
| 18 | + |
| 19 | + // Copy shader defines three formatted buffers as inputs: control, source, and destination. |
| 20 | + const auto ctl_buf_sharp = info.texture_buffers[0].GetSharp(info); |
| 21 | + const auto src_buf_sharp = info.texture_buffers[1].GetSharp(info); |
| 22 | + const auto dst_buf_sharp = info.texture_buffers[2].GetSharp(info); |
| 23 | + const auto buf_stride = src_buf_sharp.GetStride(); |
| 24 | + ASSERT(buf_stride == dst_buf_sharp.GetStride()); |
| 25 | + |
| 26 | + struct CopyShaderControl { |
| 27 | + u32 dst_idx; |
| 28 | + u32 src_idx; |
| 29 | + u32 end; |
| 30 | + }; |
| 31 | + static_assert(sizeof(CopyShaderControl) == 12); |
| 32 | + ASSERT(ctl_buf_sharp.GetStride() == sizeof(CopyShaderControl)); |
| 33 | + const auto ctl_buf = reinterpret_cast<const CopyShaderControl*>(ctl_buf_sharp.base_address); |
| 34 | + |
| 35 | + static std::vector<vk::BufferCopy> copies; |
| 36 | + copies.clear(); |
| 37 | + copies.reserve(regs.cs_program.dim_x); |
| 38 | + |
| 39 | + for (u32 i = 0; i < regs.cs_program.dim_x; i++) { |
| 40 | + const auto& [dst_idx, src_idx, end] = ctl_buf[i]; |
| 41 | + const u32 local_dst_offset = dst_idx * buf_stride; |
| 42 | + const u32 local_src_offset = src_idx * buf_stride; |
| 43 | + const u32 local_size = (end + 1) * buf_stride; |
| 44 | + copies.emplace_back(local_src_offset, local_dst_offset, local_size); |
| 45 | + } |
| 46 | + |
| 47 | + scheduler.EndRendering(); |
| 48 | + |
| 49 | + static constexpr vk::MemoryBarrier READ_BARRIER{ |
| 50 | + .srcAccessMask = vk::AccessFlagBits::eMemoryWrite, |
| 51 | + .dstAccessMask = vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite, |
| 52 | + }; |
| 53 | + static constexpr vk::MemoryBarrier WRITE_BARRIER{ |
| 54 | + .srcAccessMask = vk::AccessFlagBits::eTransferWrite, |
| 55 | + .dstAccessMask = vk::AccessFlagBits::eMemoryRead | vk::AccessFlagBits::eMemoryWrite, |
| 56 | + }; |
| 57 | + scheduler.CommandBuffer().pipelineBarrier( |
| 58 | + vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eTransfer, |
| 59 | + vk::DependencyFlagBits::eByRegion, READ_BARRIER, {}, {}); |
| 60 | + |
| 61 | + static constexpr vk::DeviceSize MaxDistanceForMerge = 64_MB; |
| 62 | + u32 batch_start = 0; |
| 63 | + u32 batch_end = 1; |
| 64 | + |
| 65 | + while (batch_end < copies.size()) { |
| 66 | + // Place first copy into the current batch |
| 67 | + const auto& copy = copies[batch_start]; |
| 68 | + auto src_offset_min = copy.srcOffset; |
| 69 | + auto src_offset_max = copy.srcOffset + copy.size; |
| 70 | + auto dst_offset_min = copy.dstOffset; |
| 71 | + auto dst_offset_max = copy.dstOffset + copy.size; |
| 72 | + |
| 73 | + for (int i = batch_start + 1; i < copies.size(); i++) { |
| 74 | + // Compute new src and dst bounds if we were to batch this copy |
| 75 | + const auto [src_offset, dst_offset, size] = copies[i]; |
| 76 | + auto new_src_offset_min = std::min(src_offset_min, src_offset); |
| 77 | + auto new_src_offset_max = std::max(src_offset_max, src_offset + size); |
| 78 | + if (new_src_offset_max - new_src_offset_min > MaxDistanceForMerge) { |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + auto new_dst_offset_min = std::min(dst_offset_min, dst_offset); |
| 83 | + auto new_dst_offset_max = std::max(dst_offset_max, dst_offset + size); |
| 84 | + if (new_dst_offset_max - new_dst_offset_min > MaxDistanceForMerge) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + // We can batch this copy |
| 89 | + src_offset_min = new_src_offset_min; |
| 90 | + src_offset_max = new_src_offset_max; |
| 91 | + dst_offset_min = new_dst_offset_min; |
| 92 | + dst_offset_max = new_dst_offset_max; |
| 93 | + if (i != batch_end) { |
| 94 | + std::swap(copies[i], copies[batch_end]); |
| 95 | + } |
| 96 | + ++batch_end; |
| 97 | + } |
| 98 | + |
| 99 | + // Obtain buffers for the total source and destination ranges. |
| 100 | + const auto [src_buf, src_buf_offset] = |
| 101 | + buffer_cache.ObtainBuffer(src_buf_sharp.base_address + src_offset_min, |
| 102 | + src_offset_max - src_offset_min, false, false); |
| 103 | + const auto [dst_buf, dst_buf_offset] = |
| 104 | + buffer_cache.ObtainBuffer(dst_buf_sharp.base_address + dst_offset_min, |
| 105 | + dst_offset_max - dst_offset_min, true, false); |
| 106 | + |
| 107 | + // Apply found buffer base. |
| 108 | + const auto vk_copies = std::span{copies}.subspan(batch_start, batch_end - batch_start); |
| 109 | + for (auto& copy : vk_copies) { |
| 110 | + copy.srcOffset = copy.srcOffset - src_offset_min + src_buf_offset; |
| 111 | + copy.dstOffset = copy.dstOffset - dst_offset_min + dst_buf_offset; |
| 112 | + } |
| 113 | + |
| 114 | + // Execute buffer copies. |
| 115 | + LOG_TRACE(Render_Vulkan, "HLE buffer copy: src_size = {}, dst_size = {}", |
| 116 | + src_offset_max - src_offset_min, dst_offset_max - dst_offset_min); |
| 117 | + scheduler.CommandBuffer().copyBuffer(src_buf->Handle(), dst_buf->Handle(), vk_copies); |
| 118 | + batch_start = batch_end; |
| 119 | + ++batch_end; |
| 120 | + } |
| 121 | + |
| 122 | + scheduler.CommandBuffer().pipelineBarrier( |
| 123 | + vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eAllCommands, |
| 124 | + vk::DependencyFlagBits::eByRegion, WRITE_BARRIER, {}, {}); |
| 125 | + |
| 126 | + return true; |
| 127 | +} |
| 128 | + |
| 129 | +bool ExecuteShaderHLE(const Shader::Info& info, const AmdGpu::Liverpool::Regs& regs, |
| 130 | + Rasterizer& rasterizer) { |
| 131 | + switch (info.pgm_hash) { |
| 132 | + case COPY_SHADER_HASH: |
| 133 | + return ExecuteCopyShaderHLE(info, regs, rasterizer); |
| 134 | + default: |
| 135 | + return false; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +} // namespace Vulkan |
0 commit comments