2022-04-23 08:59:50 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-06-04 15:42:19 +00:00
|
|
|
|
2020-06-05 03:09:52 +00:00
|
|
|
#include <array>
|
2020-06-04 15:42:19 +00:00
|
|
|
#include <vector>
|
2021-07-10 17:32:34 +00:00
|
|
|
#include "common/scope_exit.h"
|
|
|
|
#include "video_core/dirty_flags.h"
|
2020-06-04 15:42:19 +00:00
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
2022-01-25 18:15:45 +00:00
|
|
|
#include "video_core/macro/macro.h"
|
2020-06-04 15:42:19 +00:00
|
|
|
#include "video_core/macro/macro_hle.h"
|
|
|
|
#include "video_core/rasterizer_interface.h"
|
|
|
|
|
|
|
|
namespace Tegra {
|
2020-06-24 02:18:33 +00:00
|
|
|
namespace {
|
2022-01-25 18:15:45 +00:00
|
|
|
|
|
|
|
using HLEFunction = void (*)(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& parameters);
|
|
|
|
|
2020-06-04 15:42:19 +00:00
|
|
|
// HLE'd functions
|
2020-07-17 02:01:45 +00:00
|
|
|
void HLE_771BB18C62444DA0(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& parameters) {
|
2020-06-04 15:42:19 +00:00
|
|
|
const u32 instance_count = parameters[2] & maxwell3d.GetRegisterValue(0xD1B);
|
|
|
|
|
|
|
|
maxwell3d.regs.draw.topology.Assign(
|
2020-07-17 02:16:08 +00:00
|
|
|
static_cast<Tegra::Engines::Maxwell3D::Regs::PrimitiveTopology>(parameters[0] & 0x3ffffff));
|
2022-08-12 09:58:09 +00:00
|
|
|
maxwell3d.regs.global_base_instance_index = parameters[5];
|
|
|
|
maxwell3d.regs.global_base_vertex_index = parameters[3];
|
|
|
|
maxwell3d.regs.index_buffer.count = parameters[1];
|
|
|
|
maxwell3d.regs.index_buffer.first = parameters[4];
|
2020-06-04 15:42:19 +00:00
|
|
|
|
|
|
|
if (maxwell3d.ShouldExecute()) {
|
2022-10-21 07:38:50 +00:00
|
|
|
maxwell3d.Rasterizer().Draw(true, instance_count);
|
2020-06-04 15:42:19 +00:00
|
|
|
}
|
2022-08-12 09:58:09 +00:00
|
|
|
maxwell3d.regs.index_buffer.count = 0;
|
2020-06-04 15:42:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 02:01:45 +00:00
|
|
|
void HLE_0D61FC9FAAC9FCAD(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& parameters) {
|
2022-10-21 07:38:50 +00:00
|
|
|
const u32 instance_count = (maxwell3d.GetRegisterValue(0xD1B) & parameters[2]);
|
2020-06-04 15:42:19 +00:00
|
|
|
|
|
|
|
maxwell3d.regs.vertex_buffer.first = parameters[3];
|
|
|
|
maxwell3d.regs.vertex_buffer.count = parameters[1];
|
2022-08-12 09:58:09 +00:00
|
|
|
maxwell3d.regs.global_base_instance_index = parameters[4];
|
2020-06-04 15:42:19 +00:00
|
|
|
maxwell3d.regs.draw.topology.Assign(
|
|
|
|
static_cast<Tegra::Engines::Maxwell3D::Regs::PrimitiveTopology>(parameters[0]));
|
|
|
|
|
|
|
|
if (maxwell3d.ShouldExecute()) {
|
2022-10-21 07:38:50 +00:00
|
|
|
maxwell3d.Rasterizer().Draw(false, instance_count);
|
2020-06-04 15:42:19 +00:00
|
|
|
}
|
|
|
|
maxwell3d.regs.vertex_buffer.count = 0;
|
|
|
|
}
|
|
|
|
|
2020-07-17 02:01:45 +00:00
|
|
|
void HLE_0217920100488FF7(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& parameters) {
|
2020-06-04 15:42:19 +00:00
|
|
|
const u32 instance_count = (maxwell3d.GetRegisterValue(0xD1B) & parameters[2]);
|
|
|
|
const u32 element_base = parameters[4];
|
|
|
|
const u32 base_instance = parameters[5];
|
2022-08-12 09:58:09 +00:00
|
|
|
maxwell3d.regs.index_buffer.first = parameters[3];
|
|
|
|
maxwell3d.regs.vertex_id_base = element_base;
|
|
|
|
maxwell3d.regs.index_buffer.count = parameters[1];
|
2021-07-10 17:32:34 +00:00
|
|
|
maxwell3d.dirty.flags[VideoCommon::Dirty::IndexBuffer] = true;
|
2022-08-12 09:58:09 +00:00
|
|
|
maxwell3d.regs.global_base_vertex_index = element_base;
|
|
|
|
maxwell3d.regs.global_base_instance_index = base_instance;
|
2022-10-21 07:38:50 +00:00
|
|
|
maxwell3d.CallMethod(0x8e3, 0x640, true);
|
|
|
|
maxwell3d.CallMethod(0x8e4, element_base, true);
|
|
|
|
maxwell3d.CallMethod(0x8e5, base_instance, true);
|
2020-06-04 15:42:19 +00:00
|
|
|
maxwell3d.regs.draw.topology.Assign(
|
|
|
|
static_cast<Tegra::Engines::Maxwell3D::Regs::PrimitiveTopology>(parameters[0]));
|
|
|
|
if (maxwell3d.ShouldExecute()) {
|
2022-10-21 07:38:50 +00:00
|
|
|
maxwell3d.Rasterizer().Draw(true, instance_count);
|
2020-06-04 15:42:19 +00:00
|
|
|
}
|
2022-08-12 09:58:09 +00:00
|
|
|
maxwell3d.regs.vertex_id_base = 0x0;
|
|
|
|
maxwell3d.regs.index_buffer.count = 0;
|
|
|
|
maxwell3d.regs.global_base_vertex_index = 0x0;
|
|
|
|
maxwell3d.regs.global_base_instance_index = 0x0;
|
2022-10-21 07:38:50 +00:00
|
|
|
maxwell3d.CallMethod(0x8e3, 0x640, true);
|
|
|
|
maxwell3d.CallMethod(0x8e4, 0x0, true);
|
|
|
|
maxwell3d.CallMethod(0x8e5, 0x0, true);
|
2020-06-04 15:42:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-10 17:32:34 +00:00
|
|
|
// Multidraw Indirect
|
|
|
|
void HLE_3F5E74B9C9A50164(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& parameters) {
|
|
|
|
SCOPE_EXIT({
|
|
|
|
// Clean everything.
|
2022-08-12 09:58:09 +00:00
|
|
|
maxwell3d.regs.vertex_id_base = 0x0;
|
|
|
|
maxwell3d.regs.index_buffer.count = 0;
|
|
|
|
maxwell3d.regs.global_base_vertex_index = 0x0;
|
|
|
|
maxwell3d.regs.global_base_instance_index = 0x0;
|
2022-10-21 07:38:50 +00:00
|
|
|
maxwell3d.CallMethod(0x8e3, 0x640, true);
|
|
|
|
maxwell3d.CallMethod(0x8e4, 0x0, true);
|
|
|
|
maxwell3d.CallMethod(0x8e5, 0x0, true);
|
2021-07-10 17:32:34 +00:00
|
|
|
maxwell3d.dirty.flags[VideoCommon::Dirty::IndexBuffer] = true;
|
|
|
|
});
|
|
|
|
const u32 start_indirect = parameters[0];
|
|
|
|
const u32 end_indirect = parameters[1];
|
|
|
|
if (start_indirect >= end_indirect) {
|
|
|
|
// Nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto topology =
|
|
|
|
static_cast<Tegra::Engines::Maxwell3D::Regs::PrimitiveTopology>(parameters[2]);
|
|
|
|
maxwell3d.regs.draw.topology.Assign(topology);
|
|
|
|
const u32 padding = parameters[3];
|
|
|
|
const std::size_t max_draws = parameters[4];
|
|
|
|
|
|
|
|
const u32 indirect_words = 5 + padding;
|
|
|
|
const std::size_t first_draw = start_indirect;
|
|
|
|
const std::size_t effective_draws = end_indirect - start_indirect;
|
|
|
|
const std::size_t last_draw = start_indirect + std::min(effective_draws, max_draws);
|
|
|
|
|
|
|
|
for (std::size_t index = first_draw; index < last_draw; index++) {
|
|
|
|
const std::size_t base = index * indirect_words + 5;
|
|
|
|
const u32 num_vertices = parameters[base];
|
|
|
|
const u32 instance_count = parameters[base + 1];
|
|
|
|
const u32 first_index = parameters[base + 2];
|
|
|
|
const u32 base_vertex = parameters[base + 3];
|
|
|
|
const u32 base_instance = parameters[base + 4];
|
2022-08-12 09:58:09 +00:00
|
|
|
maxwell3d.regs.index_buffer.first = first_index;
|
|
|
|
maxwell3d.regs.vertex_id_base = base_vertex;
|
|
|
|
maxwell3d.regs.index_buffer.count = num_vertices;
|
|
|
|
maxwell3d.regs.global_base_vertex_index = base_vertex;
|
|
|
|
maxwell3d.regs.global_base_instance_index = base_instance;
|
2022-10-21 07:38:50 +00:00
|
|
|
maxwell3d.CallMethod(0x8e3, 0x640, true);
|
|
|
|
maxwell3d.CallMethod(0x8e4, base_vertex, true);
|
|
|
|
maxwell3d.CallMethod(0x8e5, base_instance, true);
|
2021-07-10 17:32:34 +00:00
|
|
|
maxwell3d.dirty.flags[VideoCommon::Dirty::IndexBuffer] = true;
|
|
|
|
if (maxwell3d.ShouldExecute()) {
|
2022-10-21 07:38:50 +00:00
|
|
|
maxwell3d.Rasterizer().Draw(true, instance_count);
|
2021-07-10 17:32:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 03:28:58 +00:00
|
|
|
// Multi-layer Clear
|
|
|
|
void HLE_EAD26C3E2109B06B(Engines::Maxwell3D& maxwell3d, const std::vector<u32>& parameters) {
|
|
|
|
ASSERT(parameters.size() == 1);
|
|
|
|
|
|
|
|
Engines::Maxwell3D::Regs::ClearSurface clear_params{parameters[0]};
|
|
|
|
|
|
|
|
const u32 rt_index = clear_params.RT;
|
|
|
|
const u32 num_layers = maxwell3d.regs.rt[rt_index].depth_volume;
|
|
|
|
|
|
|
|
for (u32 i = 0; i < num_layers; i++) {
|
|
|
|
// 0x674 = regs.clear_surface
|
|
|
|
maxwell3d.CallMethod(0x674, clear_params.raw, true);
|
|
|
|
clear_params.layer.Assign(clear_params.layer + 1);
|
|
|
|
|
|
|
|
// FIXME: remove this when amdvlk can clear multiple layers without crashing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr std::array<std::pair<u64, HLEFunction>, 5> hle_funcs{{
|
2020-07-17 01:59:23 +00:00
|
|
|
{0x771BB18C62444DA0, &HLE_771BB18C62444DA0},
|
|
|
|
{0x0D61FC9FAAC9FCAD, &HLE_0D61FC9FAAC9FCAD},
|
|
|
|
{0x0217920100488FF7, &HLE_0217920100488FF7},
|
2021-07-10 17:32:34 +00:00
|
|
|
{0x3F5E74B9C9A50164, &HLE_3F5E74B9C9A50164},
|
2022-11-17 03:28:58 +00:00
|
|
|
{0xEAD26C3E2109B06B, &HLE_EAD26C3E2109B06B},
|
2020-06-24 02:18:33 +00:00
|
|
|
}};
|
2020-06-04 15:42:19 +00:00
|
|
|
|
2022-01-25 18:15:45 +00:00
|
|
|
class HLEMacroImpl final : public CachedMacro {
|
|
|
|
public:
|
|
|
|
explicit HLEMacroImpl(Engines::Maxwell3D& maxwell3d_, HLEFunction func_)
|
|
|
|
: maxwell3d{maxwell3d_}, func{func_} {}
|
|
|
|
|
|
|
|
void Execute(const std::vector<u32>& parameters, u32 method) override {
|
|
|
|
func(maxwell3d, parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Engines::Maxwell3D& maxwell3d;
|
|
|
|
HLEFunction func;
|
|
|
|
};
|
2021-07-10 17:32:34 +00:00
|
|
|
|
2022-01-25 18:15:45 +00:00
|
|
|
} // Anonymous namespace
|
|
|
|
|
2020-12-04 19:39:12 +00:00
|
|
|
HLEMacro::HLEMacro(Engines::Maxwell3D& maxwell3d_) : maxwell3d{maxwell3d_} {}
|
2020-06-04 15:42:19 +00:00
|
|
|
HLEMacro::~HLEMacro() = default;
|
|
|
|
|
2022-01-25 18:50:10 +00:00
|
|
|
std::unique_ptr<CachedMacro> HLEMacro::GetHLEProgram(u64 hash) const {
|
2020-06-24 02:18:33 +00:00
|
|
|
const auto it = std::find_if(hle_funcs.cbegin(), hle_funcs.cend(),
|
|
|
|
[hash](const auto& pair) { return pair.first == hash; });
|
2020-06-05 03:09:52 +00:00
|
|
|
if (it == hle_funcs.end()) {
|
2022-01-25 18:50:10 +00:00
|
|
|
return nullptr;
|
2020-06-04 15:42:19 +00:00
|
|
|
}
|
2020-06-05 03:09:52 +00:00
|
|
|
return std::make_unique<HLEMacroImpl>(maxwell3d, it->second);
|
2020-06-04 15:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Tegra
|