2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-04-26 08:53:26 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <iosfwd>
|
|
|
|
#include <limits>
|
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
|
|
|
#include <span>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2022-11-21 17:31:18 +01:00
|
|
|
#include "common/polyfill_thread.h"
|
2021-04-26 08:53:26 +02:00
|
|
|
#include "common/unique_function.h"
|
|
|
|
#include "shader_recompiler/environment.h"
|
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
|
|
|
|
|
|
|
namespace Tegra {
|
|
|
|
class Memorymanager;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace VideoCommon {
|
|
|
|
|
|
|
|
class GenericEnvironment : public Shader::Environment {
|
|
|
|
public:
|
|
|
|
explicit GenericEnvironment() = default;
|
|
|
|
explicit GenericEnvironment(Tegra::MemoryManager& gpu_memory_, GPUVAddr program_base_,
|
|
|
|
u32 start_address_);
|
|
|
|
|
|
|
|
~GenericEnvironment() override;
|
|
|
|
|
|
|
|
[[nodiscard]] u32 TextureBoundBuffer() const final;
|
|
|
|
|
|
|
|
[[nodiscard]] u32 LocalMemorySize() const final;
|
|
|
|
|
|
|
|
[[nodiscard]] u32 SharedMemorySize() const final;
|
|
|
|
|
|
|
|
[[nodiscard]] std::array<u32, 3> WorkgroupSize() const final;
|
|
|
|
|
|
|
|
[[nodiscard]] u64 ReadInstruction(u32 address) final;
|
|
|
|
|
|
|
|
[[nodiscard]] std::optional<u64> Analyze();
|
|
|
|
|
|
|
|
void SetCachedSize(size_t size_bytes);
|
|
|
|
|
|
|
|
[[nodiscard]] size_t CachedSize() const noexcept;
|
|
|
|
|
|
|
|
[[nodiscard]] size_t ReadSize() const noexcept;
|
|
|
|
|
|
|
|
[[nodiscard]] bool CanBeSerialized() const noexcept;
|
|
|
|
|
|
|
|
[[nodiscard]] u64 CalculateHash() const;
|
|
|
|
|
2021-11-17 04:19:29 +01:00
|
|
|
void Dump(u64 hash) override;
|
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
void Serialize(std::ofstream& file) const;
|
|
|
|
|
2022-11-09 17:58:10 +01:00
|
|
|
bool HasHLEMacroState() const override {
|
|
|
|
return has_hle_engine_state;
|
|
|
|
}
|
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
protected:
|
|
|
|
std::optional<u64> TryFindSize();
|
|
|
|
|
2022-11-04 07:39:42 +01:00
|
|
|
Tegra::Texture::TICEntry ReadTextureInfo(GPUVAddr tic_addr, u32 tic_limit,
|
|
|
|
bool via_header_index, u32 raw);
|
2021-04-26 08:53:26 +02:00
|
|
|
|
|
|
|
Tegra::MemoryManager* gpu_memory{};
|
|
|
|
GPUVAddr program_base{};
|
|
|
|
|
|
|
|
std::vector<u64> code;
|
|
|
|
std::unordered_map<u32, Shader::TextureType> texture_types;
|
2022-11-04 07:39:42 +01:00
|
|
|
std::unordered_map<u32, Shader::TexturePixelFormat> texture_pixel_formats;
|
2021-04-26 08:53:26 +02:00
|
|
|
std::unordered_map<u64, u32> cbuf_values;
|
2022-11-09 17:58:10 +01:00
|
|
|
std::unordered_map<u64, Shader::ReplaceConstant> cbuf_replacements;
|
2021-04-26 08:53:26 +02:00
|
|
|
|
|
|
|
u32 local_memory_size{};
|
|
|
|
u32 texture_bound{};
|
|
|
|
u32 shared_memory_size{};
|
|
|
|
std::array<u32, 3> workgroup_size{};
|
|
|
|
|
|
|
|
u32 read_lowest = std::numeric_limits<u32>::max();
|
|
|
|
u32 read_highest = 0;
|
|
|
|
|
|
|
|
u32 cached_lowest = std::numeric_limits<u32>::max();
|
|
|
|
u32 cached_highest = 0;
|
2021-11-17 04:19:29 +01:00
|
|
|
u32 initial_offset = 0;
|
2021-04-26 08:53:26 +02:00
|
|
|
|
2022-09-01 16:05:11 +02:00
|
|
|
u32 viewport_transform_state = 1;
|
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
bool has_unbound_instructions = false;
|
2022-11-09 17:58:10 +01:00
|
|
|
bool has_hle_engine_state = false;
|
2021-04-26 08:53:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class GraphicsEnvironment final : public GenericEnvironment {
|
|
|
|
public:
|
|
|
|
explicit GraphicsEnvironment() = default;
|
|
|
|
explicit GraphicsEnvironment(Tegra::Engines::Maxwell3D& maxwell3d_,
|
|
|
|
Tegra::MemoryManager& gpu_memory_,
|
2022-08-12 11:58:09 +02:00
|
|
|
Tegra::Engines::Maxwell3D::Regs::ShaderType program,
|
2021-04-26 08:53:26 +02:00
|
|
|
GPUVAddr program_base_, u32 start_address_);
|
|
|
|
|
|
|
|
~GraphicsEnvironment() override = default;
|
|
|
|
|
|
|
|
u32 ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) override;
|
|
|
|
|
|
|
|
Shader::TextureType ReadTextureType(u32 handle) override;
|
|
|
|
|
2022-11-04 07:39:42 +01:00
|
|
|
Shader::TexturePixelFormat ReadTexturePixelFormat(u32 handle) override;
|
|
|
|
|
2022-09-01 16:05:11 +02:00
|
|
|
u32 ReadViewportTransformState() override;
|
|
|
|
|
2022-11-09 17:58:10 +01:00
|
|
|
std::optional<Shader::ReplaceConstant> GetReplaceConstBuffer(u32 bank, u32 offset) override;
|
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
private:
|
|
|
|
Tegra::Engines::Maxwell3D* maxwell3d{};
|
|
|
|
size_t stage_index{};
|
|
|
|
};
|
|
|
|
|
|
|
|
class ComputeEnvironment final : public GenericEnvironment {
|
|
|
|
public:
|
|
|
|
explicit ComputeEnvironment() = default;
|
|
|
|
explicit ComputeEnvironment(Tegra::Engines::KeplerCompute& kepler_compute_,
|
|
|
|
Tegra::MemoryManager& gpu_memory_, GPUVAddr program_base_,
|
|
|
|
u32 start_address_);
|
|
|
|
|
|
|
|
~ComputeEnvironment() override = default;
|
|
|
|
|
|
|
|
u32 ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) override;
|
|
|
|
|
|
|
|
Shader::TextureType ReadTextureType(u32 handle) override;
|
|
|
|
|
2022-11-04 07:39:42 +01:00
|
|
|
Shader::TexturePixelFormat ReadTexturePixelFormat(u32 handle) override;
|
|
|
|
|
2022-09-01 16:05:11 +02:00
|
|
|
u32 ReadViewportTransformState() override;
|
|
|
|
|
2022-11-09 17:58:10 +01:00
|
|
|
std::optional<Shader::ReplaceConstant> GetReplaceConstBuffer(
|
|
|
|
[[maybe_unused]] u32 bank, [[maybe_unused]] u32 offset) override {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
private:
|
|
|
|
Tegra::Engines::KeplerCompute* kepler_compute{};
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileEnvironment final : public Shader::Environment {
|
|
|
|
public:
|
|
|
|
FileEnvironment() = default;
|
|
|
|
~FileEnvironment() override = default;
|
|
|
|
|
|
|
|
FileEnvironment& operator=(FileEnvironment&&) noexcept = default;
|
|
|
|
FileEnvironment(FileEnvironment&&) noexcept = default;
|
|
|
|
|
|
|
|
FileEnvironment& operator=(const FileEnvironment&) = delete;
|
|
|
|
FileEnvironment(const FileEnvironment&) = delete;
|
|
|
|
|
|
|
|
void Deserialize(std::ifstream& file);
|
|
|
|
|
|
|
|
[[nodiscard]] u64 ReadInstruction(u32 address) override;
|
|
|
|
|
|
|
|
[[nodiscard]] u32 ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) override;
|
|
|
|
|
|
|
|
[[nodiscard]] Shader::TextureType ReadTextureType(u32 handle) override;
|
|
|
|
|
2022-11-04 07:39:42 +01:00
|
|
|
[[nodiscard]] Shader::TexturePixelFormat ReadTexturePixelFormat(u32 handle) override;
|
|
|
|
|
2022-09-01 16:05:11 +02:00
|
|
|
[[nodiscard]] u32 ReadViewportTransformState() override;
|
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
[[nodiscard]] u32 LocalMemorySize() const override;
|
|
|
|
|
|
|
|
[[nodiscard]] u32 SharedMemorySize() const override;
|
|
|
|
|
|
|
|
[[nodiscard]] u32 TextureBoundBuffer() const override;
|
|
|
|
|
|
|
|
[[nodiscard]] std::array<u32, 3> WorkgroupSize() const override;
|
|
|
|
|
2022-11-09 17:58:10 +01:00
|
|
|
[[nodiscard]] std::optional<Shader::ReplaceConstant> GetReplaceConstBuffer(u32 bank,
|
|
|
|
u32 offset) override;
|
|
|
|
|
|
|
|
[[nodiscard]] bool HasHLEMacroState() const override {
|
|
|
|
return cbuf_replacements.size() != 0;
|
|
|
|
}
|
|
|
|
|
2021-11-17 04:19:29 +01:00
|
|
|
void Dump(u64 hash) override;
|
|
|
|
|
2021-04-26 08:53:26 +02:00
|
|
|
private:
|
|
|
|
std::unique_ptr<u64[]> code;
|
|
|
|
std::unordered_map<u32, Shader::TextureType> texture_types;
|
2022-11-04 07:39:42 +01:00
|
|
|
std::unordered_map<u32, Shader::TexturePixelFormat> texture_pixel_formats;
|
2021-04-26 08:53:26 +02:00
|
|
|
std::unordered_map<u64, u32> cbuf_values;
|
2022-11-09 17:58:10 +01:00
|
|
|
std::unordered_map<u64, Shader::ReplaceConstant> cbuf_replacements;
|
2021-04-26 08:53:26 +02:00
|
|
|
std::array<u32, 3> workgroup_size{};
|
|
|
|
u32 local_memory_size{};
|
|
|
|
u32 shared_memory_size{};
|
|
|
|
u32 texture_bound{};
|
|
|
|
u32 read_lowest{};
|
|
|
|
u32 read_highest{};
|
2021-11-17 04:19:29 +01:00
|
|
|
u32 initial_offset{};
|
2022-09-01 16:05:11 +02:00
|
|
|
u32 viewport_transform_state = 1;
|
2021-04-26 08:53:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void SerializePipeline(std::span<const char> key, std::span<const GenericEnvironment* const> envs,
|
2021-07-19 02:07:12 +02:00
|
|
|
const std::filesystem::path& filename, u32 cache_version);
|
2021-04-26 08:53:26 +02:00
|
|
|
|
|
|
|
template <typename Key, typename Envs>
|
2021-07-19 02:07:12 +02:00
|
|
|
void SerializePipeline(const Key& key, const Envs& envs, const std::filesystem::path& filename,
|
|
|
|
u32 cache_version) {
|
2021-04-26 08:53:26 +02:00
|
|
|
static_assert(std::is_trivially_copyable_v<Key>);
|
|
|
|
static_assert(std::has_unique_object_representations_v<Key>);
|
|
|
|
SerializePipeline(std::span(reinterpret_cast<const char*>(&key), sizeof(key)),
|
2021-07-19 02:07:12 +02:00
|
|
|
std::span(envs.data(), envs.size()), filename, cache_version);
|
2021-04-26 08:53:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoadPipelines(
|
2021-07-19 02:07:12 +02:00
|
|
|
std::stop_token stop_loading, const std::filesystem::path& filename, u32 expected_cache_version,
|
2021-04-26 08:53:26 +02:00
|
|
|
Common::UniqueFunction<void, std::ifstream&, FileEnvironment> load_compute,
|
|
|
|
Common::UniqueFunction<void, std::ifstream&, std::vector<FileEnvironment>> load_graphics);
|
|
|
|
|
|
|
|
} // namespace VideoCommon
|