2019-09-23 14:02:02 -04:00
|
|
|
// Copyright 2019 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2019-09-25 18:19:41 -04:00
|
|
|
#include <algorithm>
|
2019-11-27 05:53:40 -05:00
|
|
|
#include <tuple>
|
2019-11-27 05:51:13 -05:00
|
|
|
|
2019-09-23 14:02:02 -04:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "video_core/engines/maxwell_3d.h"
|
2019-11-18 16:35:21 -05:00
|
|
|
#include "video_core/engines/shader_type.h"
|
2020-02-28 18:53:10 -05:00
|
|
|
#include "video_core/shader/registry.h"
|
2019-09-23 14:02:02 -04:00
|
|
|
|
|
|
|
namespace VideoCommon::Shader {
|
|
|
|
|
2019-09-25 18:19:41 -04:00
|
|
|
using Tegra::Engines::SamplerDescriptor;
|
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
Registry::Registry(Tegra::Engines::ShaderType shader_stage,
|
|
|
|
VideoCore::GuestDriverProfile stored_guest_driver_profile)
|
2020-02-26 14:13:47 -05:00
|
|
|
: stage{shader_stage}, stored_guest_driver_profile{stored_guest_driver_profile} {}
|
2019-09-23 14:02:02 -04:00
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
Registry::Registry(Tegra::Engines::ShaderType shader_stage,
|
|
|
|
Tegra::Engines::ConstBufferEngineInterface& engine)
|
2019-09-25 18:19:41 -04:00
|
|
|
: stage{shader_stage}, engine{&engine} {}
|
2019-09-23 14:02:02 -04:00
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
Registry::~Registry() = default;
|
2019-10-17 10:35:16 -04:00
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
std::optional<u32> Registry::ObtainKey(u32 buffer, u32 offset) {
|
2019-09-23 14:02:02 -04:00
|
|
|
const std::pair<u32, u32> key = {buffer, offset};
|
2019-09-25 18:19:41 -04:00
|
|
|
const auto iter = keys.find(key);
|
|
|
|
if (iter != keys.end()) {
|
|
|
|
return iter->second;
|
2019-09-23 14:02:02 -04:00
|
|
|
}
|
2019-09-25 18:19:41 -04:00
|
|
|
if (!engine) {
|
2019-10-17 10:35:16 -04:00
|
|
|
return std::nullopt;
|
2019-09-23 14:02:02 -04:00
|
|
|
}
|
2019-09-25 18:19:41 -04:00
|
|
|
const u32 value = engine->AccessConstBuffer32(stage, buffer, offset);
|
|
|
|
keys.emplace(key, value);
|
|
|
|
return value;
|
2019-09-25 09:53:18 -04:00
|
|
|
}
|
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
std::optional<SamplerDescriptor> Registry::ObtainBoundSampler(u32 offset) {
|
2019-09-25 09:53:18 -04:00
|
|
|
const u32 key = offset;
|
2019-09-25 18:19:41 -04:00
|
|
|
const auto iter = bound_samplers.find(key);
|
|
|
|
if (iter != bound_samplers.end()) {
|
|
|
|
return iter->second;
|
2019-09-25 09:53:18 -04:00
|
|
|
}
|
2019-09-25 18:19:41 -04:00
|
|
|
if (!engine) {
|
2019-10-17 10:35:16 -04:00
|
|
|
return std::nullopt;
|
2019-09-25 09:53:18 -04:00
|
|
|
}
|
2019-09-25 18:19:41 -04:00
|
|
|
const SamplerDescriptor value = engine->AccessBoundSampler(stage, offset);
|
|
|
|
bound_samplers.emplace(key, value);
|
|
|
|
return value;
|
2019-09-25 09:53:18 -04:00
|
|
|
}
|
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
std::optional<Tegra::Engines::SamplerDescriptor> Registry::ObtainBindlessSampler(u32 buffer,
|
|
|
|
u32 offset) {
|
2019-09-25 18:19:41 -04:00
|
|
|
const std::pair key = {buffer, offset};
|
|
|
|
const auto iter = bindless_samplers.find(key);
|
|
|
|
if (iter != bindless_samplers.end()) {
|
|
|
|
return iter->second;
|
2019-09-25 09:53:18 -04:00
|
|
|
}
|
2019-09-25 18:19:41 -04:00
|
|
|
if (!engine) {
|
2019-10-17 10:35:16 -04:00
|
|
|
return std::nullopt;
|
2019-09-25 09:53:18 -04:00
|
|
|
}
|
2019-09-25 18:19:41 -04:00
|
|
|
const SamplerDescriptor value = engine->AccessBindlessSampler(stage, buffer, offset);
|
|
|
|
bindless_samplers.emplace(key, value);
|
|
|
|
return value;
|
2019-09-23 14:02:02 -04:00
|
|
|
}
|
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
std::optional<u32> Registry::ObtainBoundBuffer() {
|
2020-01-03 17:15:24 -05:00
|
|
|
if (bound_buffer_saved) {
|
|
|
|
return bound_buffer;
|
|
|
|
}
|
|
|
|
if (!engine) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
bound_buffer_saved = true;
|
|
|
|
bound_buffer = engine->GetBoundBuffer();
|
|
|
|
return bound_buffer;
|
|
|
|
}
|
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
void Registry::InsertKey(u32 buffer, u32 offset, u32 value) {
|
2019-09-25 18:19:41 -04:00
|
|
|
keys.insert_or_assign({buffer, offset}, value);
|
2019-09-23 14:02:02 -04:00
|
|
|
}
|
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
void Registry::InsertBoundSampler(u32 offset, SamplerDescriptor sampler) {
|
2019-09-25 18:19:41 -04:00
|
|
|
bound_samplers.insert_or_assign(offset, sampler);
|
2019-09-23 14:02:02 -04:00
|
|
|
}
|
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
void Registry::InsertBindlessSampler(u32 buffer, u32 offset, SamplerDescriptor sampler) {
|
2019-09-25 18:19:41 -04:00
|
|
|
bindless_samplers.insert_or_assign({buffer, offset}, sampler);
|
2019-09-23 14:02:02 -04:00
|
|
|
}
|
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
void Registry::SetBoundBuffer(u32 buffer) {
|
2020-01-03 17:15:24 -05:00
|
|
|
bound_buffer_saved = true;
|
|
|
|
bound_buffer = buffer;
|
|
|
|
}
|
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
bool Registry::IsConsistent() const {
|
2019-09-25 18:19:41 -04:00
|
|
|
if (!engine) {
|
2020-02-26 14:13:47 -05:00
|
|
|
return true;
|
2019-09-23 14:02:02 -04:00
|
|
|
}
|
2019-09-25 18:19:41 -04:00
|
|
|
return std::all_of(keys.begin(), keys.end(),
|
2019-09-25 23:23:08 -04:00
|
|
|
[this](const auto& pair) {
|
|
|
|
const auto [cbuf, offset] = pair.first;
|
|
|
|
const auto value = pair.second;
|
|
|
|
return value == engine->AccessConstBuffer32(stage, cbuf, offset);
|
2019-09-25 18:19:41 -04:00
|
|
|
}) &&
|
|
|
|
std::all_of(bound_samplers.begin(), bound_samplers.end(),
|
|
|
|
[this](const auto& sampler) {
|
|
|
|
const auto [key, value] = sampler;
|
2019-09-25 23:23:08 -04:00
|
|
|
return value == engine->AccessBoundSampler(stage, key);
|
2019-09-25 18:19:41 -04:00
|
|
|
}) &&
|
2019-09-25 23:23:08 -04:00
|
|
|
std::all_of(bindless_samplers.begin(), bindless_samplers.end(),
|
|
|
|
[this](const auto& sampler) {
|
|
|
|
const auto [cbuf, offset] = sampler.first;
|
|
|
|
const auto value = sampler.second;
|
|
|
|
return value == engine->AccessBindlessSampler(stage, cbuf, offset);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-28 18:53:10 -05:00
|
|
|
bool Registry::HasEqualKeys(const Registry& rhs) const {
|
2019-11-27 05:53:40 -05:00
|
|
|
return std::tie(keys, bound_samplers, bindless_samplers) ==
|
|
|
|
std::tie(rhs.keys, rhs.bound_samplers, rhs.bindless_samplers);
|
2019-09-23 14:02:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace VideoCommon::Shader
|