|
| 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<CopyShaderControl*>(ctl_buf_sharp.base_address); |
| 34 | + |
| 35 | + // Add list of copies |
| 36 | + boost::container::set<vk::BufferCopy> copies; |
| 37 | + for (u32 i = 0; i < regs.cs_program.dim_x; i++) { |
| 38 | + const auto& [dst_idx, src_idx, end] = ctl_buf[i]; |
| 39 | + const u32 local_dst_offset = dst_idx * buf_stride; |
| 40 | + const u32 local_src_offset = src_idx * buf_stride; |
| 41 | + const u32 local_size = (end + 1) * buf_stride; |
| 42 | + copies.emplace(local_src_offset, local_dst_offset, local_size); |
| 43 | + } |
| 44 | + |
| 45 | + scheduler.EndRendering(); |
| 46 | + |
| 47 | + static constexpr vk::MemoryBarrier READ_BARRIER{ |
| 48 | + .srcAccessMask = vk::AccessFlagBits::eMemoryWrite, |
| 49 | + .dstAccessMask = vk::AccessFlagBits::eTransferRead | vk::AccessFlagBits::eTransferWrite, |
| 50 | + }; |
| 51 | + static constexpr vk::MemoryBarrier WRITE_BARRIER{ |
| 52 | + .srcAccessMask = vk::AccessFlagBits::eTransferWrite, |
| 53 | + .dstAccessMask = vk::AccessFlagBits::eMemoryRead | vk::AccessFlagBits::eMemoryWrite, |
| 54 | + }; |
| 55 | + scheduler.CommandBuffer().pipelineBarrier( |
| 56 | + vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eTransfer, |
| 57 | + vk::DependencyFlagBits::eByRegion, READ_BARRIER, {}, {}); |
| 58 | + |
| 59 | + static constexpr vk::DeviceSize MaxDistanceForMerge = 64_MB; |
| 60 | + boost::container::small_vector<vk::BufferCopy, 32> batch_copies; |
| 61 | + while (!copies.empty()) { |
| 62 | + // Place first copy into the current batch |
| 63 | + auto it = copies.begin(); |
| 64 | + auto src_offset_min = it->srcOffset; |
| 65 | + auto src_offset_max = it->srcOffset + it->size; |
| 66 | + auto dst_offset_min = it->dstOffset; |
| 67 | + auto dst_offset_max = it->dstOffset + it->size; |
| 68 | + batch_copies.emplace_back(*it); |
| 69 | + it = copies.erase(it); |
| 70 | + |
| 71 | + while (it != copies.end()) { |
| 72 | + // Compute new src and dst bounds if we were to batch this copy |
| 73 | + auto new_src_offset_min = std::min(src_offset_min, it->srcOffset); |
| 74 | + auto new_src_offset_max = std::max(src_offset_max, it->srcOffset + it->size); |
| 75 | + if (new_src_offset_max - new_src_offset_min > MaxDistanceForMerge) { |
| 76 | + ++it; |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + auto new_dst_offset_min = std::min(dst_offset_min, it->dstOffset); |
| 81 | + auto new_dst_offset_max = std::max(dst_offset_max, it->dstOffset + it->size); |
| 82 | + if (new_dst_offset_max - new_dst_offset_min > MaxDistanceForMerge) { |
| 83 | + ++it; |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + // We can batch this copy |
| 88 | + src_offset_min = new_src_offset_min; |
| 89 | + src_offset_max = new_src_offset_max; |
| 90 | + dst_offset_min = new_dst_offset_min; |
| 91 | + dst_offset_max = new_dst_offset_max; |
| 92 | + batch_copies.emplace_back(*it); |
| 93 | + it = copies.erase(it); |
| 94 | + } |
| 95 | + |
| 96 | + // Obtain buffers for the total source and destination ranges. |
| 97 | + const auto [src_buf, src_buf_offset] = |
| 98 | + buffer_cache.ObtainBuffer(src_buf_sharp.base_address + src_offset_min, |
| 99 | + src_offset_max - src_offset_min, false, false); |
| 100 | + const auto [dst_buf, dst_buf_offset] = |
| 101 | + buffer_cache.ObtainBuffer(dst_buf_sharp.base_address + dst_offset_min, |
| 102 | + dst_offset_max - dst_offset_min, true, false); |
| 103 | + |
| 104 | + // Apply found buffer base. |
| 105 | + for (auto& vk_copy : batch_copies) { |
| 106 | + vk_copy.srcOffset = vk_copy.srcOffset - src_offset_min + src_buf_offset; |
| 107 | + vk_copy.dstOffset = vk_copy.dstOffset - dst_offset_min + dst_buf_offset; |
| 108 | + } |
| 109 | + |
| 110 | + // Execute buffer copies. |
| 111 | + LOG_TRACE(Render_Vulkan, "HLE buffer copy: src_size = {}, dst_size = {}", |
| 112 | + src_offset_max - src_offset_min, dst_offset_max - dst_offset_min); |
| 113 | + scheduler.CommandBuffer().copyBuffer(src_buf->Handle(), dst_buf->Handle(), batch_copies); |
| 114 | + batch_copies.clear(); |
| 115 | + } |
| 116 | + |
| 117 | + scheduler.CommandBuffer().pipelineBarrier( |
| 118 | + vk::PipelineStageFlagBits::eTransfer, vk::PipelineStageFlagBits::eAllCommands, |
| 119 | + vk::DependencyFlagBits::eByRegion, WRITE_BARRIER, {}, {}); |
| 120 | + |
| 121 | + return true; |
| 122 | +} |
| 123 | + |
| 124 | +bool ExecuteShaderHLE(const Shader::Info& info, const AmdGpu::Liverpool::Regs& regs, |
| 125 | + Rasterizer& rasterizer) { |
| 126 | + switch (info.pgm_hash) { |
| 127 | + case COPY_SHADER_HASH: |
| 128 | + return ExecuteCopyShaderHLE(info, regs, rasterizer); |
| 129 | + default: |
| 130 | + return false; |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace Vulkan |
0 commit comments