2022-04-23 09:59:50 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-05-23 00:53:27 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-05-23 08:28:34 +01:00
|
|
|
#include <array>
|
2020-05-23 00:53:27 +01:00
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2021-05-23 08:28:34 +01:00
|
|
|
#include <span>
|
2020-05-23 00:53:27 +01:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "common/common_types.h"
|
2022-11-21 16:31:18 +00:00
|
|
|
#include "common/polyfill_ranges.h"
|
2021-11-05 14:52:31 +00:00
|
|
|
#include "video_core/control/channel_state_cache.h"
|
2020-05-23 00:53:27 +01:00
|
|
|
#include "video_core/rasterizer_interface.h"
|
2021-05-23 08:28:34 +01:00
|
|
|
#include "video_core/shader_environment.h"
|
2020-05-23 00:53:27 +01:00
|
|
|
|
2021-04-26 07:53:26 +01:00
|
|
|
namespace Tegra {
|
|
|
|
class MemoryManager;
|
|
|
|
}
|
|
|
|
|
2021-11-05 14:52:31 +00:00
|
|
|
namespace Tegra::Control {
|
|
|
|
struct ChannelState;
|
|
|
|
}
|
|
|
|
|
2020-05-23 00:53:27 +01:00
|
|
|
namespace VideoCommon {
|
|
|
|
|
2021-04-26 07:53:26 +01:00
|
|
|
class GenericEnvironment;
|
|
|
|
|
|
|
|
struct ShaderInfo {
|
|
|
|
u64 unique_hash{};
|
|
|
|
size_t size_bytes{};
|
|
|
|
};
|
|
|
|
|
2021-11-05 14:52:31 +00:00
|
|
|
class ShaderCache : public VideoCommon::ChannelSetupCaches<VideoCommon::ChannelInfo> {
|
2022-08-19 00:28:55 +01:00
|
|
|
static constexpr u64 YUZU_PAGEBITS = 14;
|
|
|
|
static constexpr u64 YUZU_PAGESIZE = u64(1) << YUZU_PAGEBITS;
|
2020-05-23 00:53:27 +01:00
|
|
|
|
2021-05-23 08:28:34 +01:00
|
|
|
static constexpr size_t NUM_PROGRAMS = 6;
|
|
|
|
|
2020-05-23 00:53:27 +01:00
|
|
|
struct Entry {
|
|
|
|
VAddr addr_start;
|
|
|
|
VAddr addr_end;
|
2021-04-26 07:53:26 +01:00
|
|
|
ShaderInfo* data;
|
2020-05-23 00:53:27 +01:00
|
|
|
|
|
|
|
bool is_memory_marked = true;
|
|
|
|
|
2021-04-26 07:53:26 +01:00
|
|
|
bool Overlaps(VAddr start, VAddr end) const noexcept {
|
2020-05-23 00:53:27 +01:00
|
|
|
return start < addr_end && addr_start < end;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// @brief Removes shaders inside a given region
|
|
|
|
/// @note Checks for ranges
|
|
|
|
/// @param addr Start address of the invalidation
|
|
|
|
/// @param size Number of bytes of the invalidation
|
2021-04-26 07:53:26 +01:00
|
|
|
void InvalidateRegion(VAddr addr, size_t size);
|
2020-05-23 00:53:27 +01:00
|
|
|
|
|
|
|
/// @brief Unmarks a memory region as cached and marks it for removal
|
|
|
|
/// @param addr Start address of the CPU write operation
|
|
|
|
/// @param size Number of bytes of the CPU write operation
|
2023-06-28 18:32:50 +01:00
|
|
|
void OnCacheInvalidation(VAddr addr, size_t size);
|
2020-05-23 00:53:27 +01:00
|
|
|
|
|
|
|
/// @brief Flushes delayed removal operations
|
2021-04-26 07:53:26 +01:00
|
|
|
void SyncGuestHost();
|
|
|
|
|
|
|
|
protected:
|
2021-05-23 08:28:34 +01:00
|
|
|
struct GraphicsEnvironments {
|
|
|
|
std::array<GraphicsEnvironment, NUM_PROGRAMS> envs;
|
2023-08-03 12:18:35 +01:00
|
|
|
std::array<Shader::Environment*, NUM_PROGRAMS> env_ptrs{};
|
2021-05-23 08:28:34 +01:00
|
|
|
|
|
|
|
std::span<Shader::Environment* const> Span() const noexcept {
|
|
|
|
return std::span(env_ptrs.begin(), std::ranges::find(env_ptrs, nullptr));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-05 14:52:31 +00:00
|
|
|
explicit ShaderCache(VideoCore::RasterizerInterface& rasterizer_);
|
2021-04-26 07:53:26 +01:00
|
|
|
|
|
|
|
/// @brief Update the hashes and information of shader stages
|
|
|
|
/// @param unique_hashes Shader hashes to store into when a stage is enabled
|
|
|
|
/// @return True no success, false on error
|
2021-05-23 08:28:34 +01:00
|
|
|
bool RefreshStages(std::array<u64, NUM_PROGRAMS>& unique_hashes);
|
2021-04-26 07:53:26 +01:00
|
|
|
|
|
|
|
/// @brief Returns information about the current compute shader
|
|
|
|
/// @return Pointer to a valid shader, nullptr on error
|
|
|
|
const ShaderInfo* ComputeShader();
|
|
|
|
|
2021-05-23 08:28:34 +01:00
|
|
|
/// @brief Collect the current graphics environments
|
|
|
|
void GetGraphicsEnvironments(GraphicsEnvironments& result,
|
|
|
|
const std::array<u64, NUM_PROGRAMS>& unique_hashes);
|
|
|
|
|
|
|
|
std::array<const ShaderInfo*, NUM_PROGRAMS> shader_infos{};
|
2021-04-26 07:53:26 +01:00
|
|
|
bool last_shaders_valid = false;
|
|
|
|
|
|
|
|
private:
|
2020-05-23 00:53:27 +01:00
|
|
|
/// @brief Tries to obtain a cached shader starting in a given address
|
|
|
|
/// @note Doesn't check for ranges, the given address has to be the start of the shader
|
|
|
|
/// @param addr Start address of the shader, this doesn't cache for region
|
|
|
|
/// @return Pointer to a valid shader, nullptr when nothing is found
|
2021-04-26 07:53:26 +01:00
|
|
|
ShaderInfo* TryGet(VAddr addr) const;
|
2020-05-23 00:53:27 +01:00
|
|
|
|
|
|
|
/// @brief Register in the cache a given entry
|
|
|
|
/// @param data Shader to store in the cache
|
|
|
|
/// @param addr Start address of the shader that will be registered
|
|
|
|
/// @param size Size in bytes of the shader
|
2021-04-26 07:53:26 +01:00
|
|
|
void Register(std::unique_ptr<ShaderInfo> data, VAddr addr, size_t size);
|
2020-05-23 00:53:27 +01:00
|
|
|
|
|
|
|
/// @brief Invalidate pages in a given region
|
|
|
|
/// @pre invalidation_mutex is locked
|
2021-04-26 07:53:26 +01:00
|
|
|
void InvalidatePagesInRegion(VAddr addr, size_t size);
|
2020-05-23 00:53:27 +01:00
|
|
|
|
|
|
|
/// @brief Remove shaders marked for deletion
|
|
|
|
/// @pre invalidation_mutex is locked
|
2021-04-26 07:53:26 +01:00
|
|
|
void RemovePendingShaders();
|
2020-05-23 00:53:27 +01:00
|
|
|
|
|
|
|
/// @brief Invalidates entries in a given range for the passed page
|
|
|
|
/// @param entries Vector of entries in the page, it will be modified on overlaps
|
|
|
|
/// @param addr Start address of the invalidation
|
|
|
|
/// @param addr_end Non-inclusive end address of the invalidation
|
|
|
|
/// @pre invalidation_mutex is locked
|
2021-04-26 07:53:26 +01:00
|
|
|
void InvalidatePageEntries(std::vector<Entry*>& entries, VAddr addr, VAddr addr_end);
|
2020-05-23 00:53:27 +01:00
|
|
|
|
2020-06-28 08:58:58 +01:00
|
|
|
/// @brief Removes all references to an entry in the invalidation cache
|
|
|
|
/// @param entry Entry to remove from the invalidation cache
|
|
|
|
/// @pre invalidation_mutex is locked
|
2021-04-26 07:53:26 +01:00
|
|
|
void RemoveEntryFromInvalidationCache(const Entry* entry);
|
2020-05-23 00:53:27 +01:00
|
|
|
|
|
|
|
/// @brief Unmarks an entry from the rasterizer cache
|
|
|
|
/// @param entry Entry to unmark from memory
|
2021-04-26 07:53:26 +01:00
|
|
|
void UnmarkMemory(Entry* entry);
|
2020-05-23 00:53:27 +01:00
|
|
|
|
|
|
|
/// @brief Removes a vector of shaders from a list
|
2020-06-28 08:58:58 +01:00
|
|
|
/// @param removed_shaders Shaders to be removed from the storage
|
2020-05-23 00:53:27 +01:00
|
|
|
/// @pre invalidation_mutex is locked
|
|
|
|
/// @pre lookup_mutex is locked
|
2022-02-01 06:39:40 +00:00
|
|
|
void RemoveShadersFromStorage(std::span<ShaderInfo*> removed_shaders);
|
2020-05-23 00:53:27 +01:00
|
|
|
|
|
|
|
/// @brief Creates a new entry in the lookup cache and returns its pointer
|
|
|
|
/// @pre lookup_mutex is locked
|
2021-04-26 07:53:26 +01:00
|
|
|
Entry* NewEntry(VAddr addr, VAddr addr_end, ShaderInfo* data);
|
2020-05-23 00:53:27 +01:00
|
|
|
|
2021-04-26 07:53:26 +01:00
|
|
|
/// @brief Create a new shader entry and register it
|
|
|
|
const ShaderInfo* MakeShaderInfo(GenericEnvironment& env, VAddr cpu_addr);
|
2020-05-23 00:53:27 +01:00
|
|
|
|
|
|
|
VideoCore::RasterizerInterface& rasterizer;
|
|
|
|
|
|
|
|
mutable std::mutex lookup_mutex;
|
|
|
|
std::mutex invalidation_mutex;
|
|
|
|
|
|
|
|
std::unordered_map<u64, std::unique_ptr<Entry>> lookup_cache;
|
|
|
|
std::unordered_map<u64, std::vector<Entry*>> invalidation_cache;
|
2021-04-26 07:53:26 +01:00
|
|
|
std::vector<std::unique_ptr<ShaderInfo>> storage;
|
2020-05-23 00:53:27 +01:00
|
|
|
std::vector<Entry*> marked_for_removal;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace VideoCommon
|