const_buffer_locker: Minor style changes
This commit is contained in:
parent
7b81ba4d8a
commit
fa2c297f3e
2 changed files with 81 additions and 157 deletions
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <memory>
|
||||||
#include "common/assert.h"
|
#include "common/assert.h"
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
#include "video_core/engines/maxwell_3d.h"
|
#include "video_core/engines/maxwell_3d.h"
|
||||||
|
@ -11,140 +13,92 @@
|
||||||
|
|
||||||
namespace VideoCommon::Shader {
|
namespace VideoCommon::Shader {
|
||||||
|
|
||||||
|
using Tegra::Engines::SamplerDescriptor;
|
||||||
|
|
||||||
ConstBufferLocker::ConstBufferLocker(Tegra::Engines::ShaderType shader_stage)
|
ConstBufferLocker::ConstBufferLocker(Tegra::Engines::ShaderType shader_stage)
|
||||||
: engine{nullptr}, shader_stage{shader_stage} {}
|
: stage{shader_stage} {}
|
||||||
|
|
||||||
ConstBufferLocker::ConstBufferLocker(Tegra::Engines::ShaderType shader_stage,
|
ConstBufferLocker::ConstBufferLocker(Tegra::Engines::ShaderType shader_stage,
|
||||||
Tegra::Engines::ConstBufferEngineInterface& engine)
|
Tegra::Engines::ConstBufferEngineInterface& engine)
|
||||||
: engine{&engine}, shader_stage{shader_stage} {}
|
: stage{shader_stage}, engine{&engine} {}
|
||||||
|
|
||||||
bool ConstBufferLocker::IsEngineSet() const {
|
|
||||||
return engine != nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ConstBufferLocker::SetEngine(Tegra::Engines::ConstBufferEngineInterface& engine_) {
|
|
||||||
engine = &engine_;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<u32> ConstBufferLocker::ObtainKey(u32 buffer, u32 offset) {
|
std::optional<u32> ConstBufferLocker::ObtainKey(u32 buffer, u32 offset) {
|
||||||
if (!keys) {
|
|
||||||
keys = std::make_shared<KeyMap>();
|
|
||||||
}
|
|
||||||
auto& key_map = *keys;
|
|
||||||
const std::pair<u32, u32> key = {buffer, offset};
|
const std::pair<u32, u32> key = {buffer, offset};
|
||||||
const auto iter = key_map.find(key);
|
const auto iter = keys.find(key);
|
||||||
if (iter != key_map.end()) {
|
if (iter != keys.end()) {
|
||||||
return {iter->second};
|
return iter->second;
|
||||||
}
|
}
|
||||||
if (!IsEngineSet()) {
|
if (!engine) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
const u32 value = engine->AccessConstBuffer32(shader_stage, buffer, offset);
|
const u32 value = engine->AccessConstBuffer32(stage, buffer, offset);
|
||||||
key_map.emplace(key, value);
|
keys.emplace(key, value);
|
||||||
return {value};
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<Tegra::Engines::SamplerDescriptor> ConstBufferLocker::ObtainBoundSampler(u32 offset) {
|
std::optional<SamplerDescriptor> ConstBufferLocker::ObtainBoundSampler(u32 offset) {
|
||||||
if (!bound_samplers) {
|
|
||||||
bound_samplers = std::make_shared<BoundSamplerMap>();
|
|
||||||
}
|
|
||||||
auto& key_map = *bound_samplers;
|
|
||||||
const u32 key = offset;
|
const u32 key = offset;
|
||||||
const auto iter = key_map.find(key);
|
const auto iter = bound_samplers.find(key);
|
||||||
if (iter != key_map.end()) {
|
if (iter != bound_samplers.end()) {
|
||||||
return {iter->second};
|
return iter->second;
|
||||||
}
|
}
|
||||||
if (!IsEngineSet()) {
|
if (!engine) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
const Tegra::Engines::SamplerDescriptor value =
|
const SamplerDescriptor value = engine->AccessBoundSampler(stage, offset);
|
||||||
engine->AccessBoundSampler(shader_stage, offset);
|
bound_samplers.emplace(key, value);
|
||||||
key_map.emplace(key, value);
|
return value;
|
||||||
return {value};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<Tegra::Engines::SamplerDescriptor> ConstBufferLocker::ObtainBindlessSampler(
|
std::optional<Tegra::Engines::SamplerDescriptor> ConstBufferLocker::ObtainBindlessSampler(
|
||||||
u32 buffer, u32 offset) {
|
u32 buffer, u32 offset) {
|
||||||
if (!bindless_samplers) {
|
const std::pair key = {buffer, offset};
|
||||||
bindless_samplers = std::make_shared<BindlessSamplerMap>();
|
const auto iter = bindless_samplers.find(key);
|
||||||
|
if (iter != bindless_samplers.end()) {
|
||||||
|
return iter->second;
|
||||||
}
|
}
|
||||||
auto& key_map = *bindless_samplers;
|
if (!engine) {
|
||||||
const std::pair<u32, u32> key = {buffer, offset};
|
|
||||||
const auto iter = key_map.find(key);
|
|
||||||
if (iter != key_map.end()) {
|
|
||||||
return {iter->second};
|
|
||||||
}
|
|
||||||
if (!IsEngineSet()) {
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
const Tegra::Engines::SamplerDescriptor value =
|
const SamplerDescriptor value = engine->AccessBindlessSampler(stage, buffer, offset);
|
||||||
engine->AccessBindlessSampler(shader_stage, buffer, offset);
|
bindless_samplers.emplace(key, value);
|
||||||
key_map.emplace(key, value);
|
return value;
|
||||||
return {value};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConstBufferLocker::InsertKey(u32 buffer, u32 offset, u32 value) {
|
void ConstBufferLocker::InsertKey(u32 buffer, u32 offset, u32 value) {
|
||||||
if (!keys) {
|
keys.insert_or_assign({buffer, offset}, value);
|
||||||
keys = std::make_shared<KeyMap>();
|
|
||||||
}
|
|
||||||
const std::pair<u32, u32> key = {buffer, offset};
|
|
||||||
(*keys)[key] = value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConstBufferLocker::InsertBoundSampler(u32 offset, Tegra::Engines::SamplerDescriptor sampler) {
|
void ConstBufferLocker::InsertBoundSampler(u32 offset, SamplerDescriptor sampler) {
|
||||||
if (!bound_samplers) {
|
bound_samplers.insert_or_assign(offset, sampler);
|
||||||
bound_samplers = std::make_shared<BoundSamplerMap>();
|
|
||||||
}
|
|
||||||
(*bound_samplers)[offset] = sampler;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConstBufferLocker::InsertBindlessSampler(u32 buffer, u32 offset,
|
void ConstBufferLocker::InsertBindlessSampler(u32 buffer, u32 offset, SamplerDescriptor sampler) {
|
||||||
Tegra::Engines::SamplerDescriptor sampler) {
|
bindless_samplers.insert_or_assign({buffer, offset}, sampler);
|
||||||
if (!bindless_samplers) {
|
|
||||||
bindless_samplers = std::make_shared<BindlessSamplerMap>();
|
|
||||||
}
|
|
||||||
const std::pair<u32, u32> key = {buffer, offset};
|
|
||||||
(*bindless_samplers)[key] = sampler;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ConstBufferLocker::IsConsistent() const {
|
bool ConstBufferLocker::IsConsistent() const {
|
||||||
if (!IsEngineSet()) {
|
if (!engine) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (keys) {
|
return std::all_of(keys.begin(), keys.end(),
|
||||||
for (const auto& key_val : *keys) {
|
[](const auto& key) {
|
||||||
const std::pair<u32, u32> key = key_val.first;
|
const auto [value, other_value] = key.first;
|
||||||
const u32 value = key_val.second;
|
return value == other_value;
|
||||||
const u32 other_value =
|
}) &&
|
||||||
engine->AccessConstBuffer32(shader_stage, key.first, key.second);
|
std::all_of(bound_samplers.begin(), bound_samplers.end(),
|
||||||
if (other_value != value) {
|
[this](const auto& sampler) {
|
||||||
return false;
|
const auto [key, value] = sampler;
|
||||||
}
|
const auto other_value = engine->AccessBoundSampler(stage, key);
|
||||||
}
|
return value.raw == other_value.raw;
|
||||||
}
|
}) &&
|
||||||
if (bound_samplers) {
|
std::all_of(
|
||||||
for (const auto& sampler_val : *bound_samplers) {
|
bindless_samplers.begin(), bindless_samplers.end(), [this](const auto& sampler) {
|
||||||
const u32 key = sampler_val.first;
|
const auto [cbuf, offset] = sampler.first;
|
||||||
const Tegra::Engines::SamplerDescriptor value = sampler_val.second;
|
const auto value = sampler.second;
|
||||||
const Tegra::Engines::SamplerDescriptor other_value =
|
const auto other_value = engine->AccessBindlessSampler(stage, cbuf, offset);
|
||||||
engine->AccessBoundSampler(shader_stage, key);
|
return value.raw == other_value.raw;
|
||||||
if (other_value.raw != value.raw) {
|
});
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (bindless_samplers) {
|
|
||||||
for (const auto& sampler_val : *bindless_samplers) {
|
|
||||||
const std::pair<u32, u32> key = sampler_val.first;
|
|
||||||
const Tegra::Engines::SamplerDescriptor value = sampler_val.second;
|
|
||||||
const Tegra::Engines::SamplerDescriptor other_value =
|
|
||||||
engine->AccessBindlessSampler(shader_stage, key.first, key.second);
|
|
||||||
if (other_value.raw != value.raw) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace VideoCommon::Shader
|
} // namespace VideoCommon::Shader
|
||||||
|
|
|
@ -23,78 +23,48 @@ public:
|
||||||
explicit ConstBufferLocker(Tegra::Engines::ShaderType shader_stage,
|
explicit ConstBufferLocker(Tegra::Engines::ShaderType shader_stage,
|
||||||
Tegra::Engines::ConstBufferEngineInterface& engine);
|
Tegra::Engines::ConstBufferEngineInterface& engine);
|
||||||
|
|
||||||
// Checks if an engine is setup, it may be possible that during disk shader
|
/// Retrieves a key from the locker, if it's registered, it will give the registered value, if
|
||||||
// cache run, the engines have not been created yet.
|
/// not it will obtain it from maxwell3d and register it.
|
||||||
bool IsEngineSet() const;
|
|
||||||
|
|
||||||
// Use this to set/change the engine used for this shader.
|
|
||||||
void SetEngine(Tegra::Engines::ConstBufferEngineInterface& engine);
|
|
||||||
|
|
||||||
// Retrieves a key from the locker, if it's registered, it will give the
|
|
||||||
// registered value, if not it will obtain it from maxwell3d and register it.
|
|
||||||
std::optional<u32> ObtainKey(u32 buffer, u32 offset);
|
std::optional<u32> ObtainKey(u32 buffer, u32 offset);
|
||||||
|
|
||||||
std::optional<Tegra::Engines::SamplerDescriptor> ObtainBoundSampler(u32 offset);
|
std::optional<Tegra::Engines::SamplerDescriptor> ObtainBoundSampler(u32 offset);
|
||||||
|
|
||||||
std::optional<Tegra::Engines::SamplerDescriptor> ObtainBindlessSampler(u32 buffer, u32 offset);
|
std::optional<Tegra::Engines::SamplerDescriptor> ObtainBindlessSampler(u32 buffer, u32 offset);
|
||||||
|
|
||||||
// Manually inserts a key.
|
/// Inserts a key.
|
||||||
void InsertKey(u32 buffer, u32 offset, u32 value);
|
void InsertKey(u32 buffer, u32 offset, u32 value);
|
||||||
|
|
||||||
|
/// Inserts a bound sampler key.
|
||||||
void InsertBoundSampler(u32 offset, Tegra::Engines::SamplerDescriptor sampler);
|
void InsertBoundSampler(u32 offset, Tegra::Engines::SamplerDescriptor sampler);
|
||||||
|
|
||||||
|
/// Inserts a bindless sampler key.
|
||||||
void InsertBindlessSampler(u32 buffer, u32 offset, Tegra::Engines::SamplerDescriptor sampler);
|
void InsertBindlessSampler(u32 buffer, u32 offset, Tegra::Engines::SamplerDescriptor sampler);
|
||||||
|
|
||||||
// Retrieves the number of keys registered.
|
/// Checks keys and samplers against engine's current const buffers. Returns true if they are
|
||||||
std::size_t NumKeys() const {
|
/// the same value, false otherwise;
|
||||||
if (!keys) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return keys->size();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t NumBoundSamplers() const {
|
|
||||||
if (!bound_samplers) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return bound_samplers->size();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t NumBindlessSamplers() const {
|
|
||||||
if (!bindless_samplers) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return bindless_samplers->size();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gives an accessor to the key's database.
|
|
||||||
// Pre: NumKeys > 0
|
|
||||||
const KeyMap& AccessKeys() const {
|
|
||||||
return *keys;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gives an accessor to the sampler's database.
|
|
||||||
// Pre: NumBindlessSamplers > 0
|
|
||||||
const BoundSamplerMap& AccessBoundSamplers() const {
|
|
||||||
return *bound_samplers;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gives an accessor to the sampler's database.
|
|
||||||
// Pre: NumBindlessSamplers > 0
|
|
||||||
const BindlessSamplerMap& AccessBindlessSamplers() const {
|
|
||||||
return *bindless_samplers;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Checks keys & samplers against engine's current const buffers. Returns true if they
|
|
||||||
// are the same value, false otherwise;
|
|
||||||
bool IsConsistent() const;
|
bool IsConsistent() const;
|
||||||
|
|
||||||
|
/// Gives an getter to the const buffer keys in the database.
|
||||||
|
const KeyMap& GetKeys() const {
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets samplers database.
|
||||||
|
const BoundSamplerMap& GetBoundSamplers() const {
|
||||||
|
return bound_samplers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets bindless samplers database.
|
||||||
|
const BindlessSamplerMap& GetBindlessSamplers() const {
|
||||||
|
return bindless_samplers;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Tegra::Engines::ConstBufferEngineInterface* engine;
|
const Tegra::Engines::ShaderType stage;
|
||||||
Tegra::Engines::ShaderType shader_stage;
|
Tegra::Engines::ConstBufferEngineInterface* engine = nullptr;
|
||||||
// All containers are lazy initialized as most shaders don't use them.
|
KeyMap keys;
|
||||||
std::shared_ptr<KeyMap> keys{};
|
BoundSamplerMap bound_samplers;
|
||||||
std::shared_ptr<BoundSamplerMap> bound_samplers{};
|
BindlessSamplerMap bindless_samplers;
|
||||||
std::shared_ptr<BindlessSamplerMap> bindless_samplers{};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace VideoCommon::Shader
|
} // namespace VideoCommon::Shader
|
||||||
|
|
Loading…
Reference in a new issue