Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ShaderPreparationTransaction {
ShaderPreparationTransaction& operator=(const ShaderPreparationTransaction&) = delete;
PreparedShaderState& Edit(const ShaderSnapshot& snapshot);
const PreparedShaderState& Read(const ShaderSnapshot& snapshot) const;
bool Tracks(const ShaderSnapshot& snapshot) const;
void Hold();
void Commit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,24 @@ bool CommittedAbi(const ShaderSnapshot& snapshot, std::span<const std::uint64_t>
return std::ranges::any_of(snapshot.prepared->registeredAbis, [&](const auto& abi) { return std::ranges::equal(abi, key); });
}

std::optional<std::vector<std::weak_ptr<const ShaderSnapshot>>> CommittedGraphicsAbi(const ShaderSnapshot& owner, std::span<const std::uint64_t> key, const std::shared_ptr<const ShaderRegistry>& registry) {
std::unique_lock lock(owner.prepared->mutex);
AwaitRegisteredPreparation(*owner.prepared, lock);
const auto& abis = owner.prepared->graphicsAbis;
const auto found = std::ranges::find_if(abis, [&](const auto& abi) { return std::ranges::equal(abi.key, key) && abi.registry.lock() == registry; });
if (found == abis.end()) return std::nullopt;
return found->stages;
}

bool CommittedLink(const ShaderSnapshot& front, const std::shared_ptr<const ShaderSnapshot>& fragment, std::uint32_t primitiveType) {
if (primitiveType == 7 || primitiveType == 17) return false;
std::unique_lock lock(front.prepared->mutex);
AwaitRegisteredPreparation(*front.prepared, lock);
const auto& fragments = front.prepared->fragments;
if (front.prepared->rectangleRequested || std::ranges::any_of(fragments, [](const auto& entry) { return entry.expired(); })) return false;
return fragment == nullptr || std::ranges::any_of(fragments, [&](const auto& entry) { return entry.lock() == fragment; });
}

}

bool ProgramUsesWorkgroup(const ShaderSnapshot& snapshot, std::size_t codeOffset) {
Expand Down Expand Up @@ -351,6 +369,10 @@ const PreparedShaderState& ShaderPreparationTransaction::Read(const ShaderSnapsh
return changes.emplace(snapshot.prepared.get(), State::Change{snapshot.prepared, *snapshot.prepared, false, snapshot.prepared->entries.size(), snapshot.prepared->registeredAbis.size()}).first->second.prepared;
}

bool ShaderPreparationTransaction::Tracks(const ShaderSnapshot& snapshot) const {
return root->changes.contains(snapshot.prepared.get());
}

void ShaderPreparationTransaction::Hold() {
require(!committed, "shader preparation transaction is already committed");
root->Hold();
Expand Down Expand Up @@ -1531,14 +1553,23 @@ void Driver::ResolveGraphicsStagesAbi(std::span<const Shader* const> stages, std
auto& key = HostThreadLocal<std::vector<std::uint64_t>, GraphicsAbiKeyStorage>();
BuildRegisteredAbiKey(state, *localDevice, key);
const auto primitiveType = state.userConfig.find(0x242u);
for (const auto& abi : transaction.Read(*owner).graphicsAbis) {
if (abi.key != key || abi.registry.lock() != registry) continue;
for (const auto& stage : abi.stages) {
const auto resolveStages = [&](const std::vector<std::weak_ptr<const ShaderSnapshot>>& stages) {
for (const auto& stage : stages) {
const auto snapshot = stage.lock();
require(snapshot != nullptr, "prepared graphics ABI lost a registered stage");
if (snapshot->type != 1) ResolvePreparedGraphics(*snapshot, {}, primitiveType == state.userConfig.end() ? 0u : primitiveType->second, localDevice->Target(), true);
}
transaction.Commit();
};
if (!transaction.Tracks(*owner)) {
if (const auto committed = CommittedGraphicsAbi(*owner, key, registry)) {
resolveStages(*committed);
return;
}
}
for (const auto& abi : transaction.Read(*owner).graphicsAbis) {
if (abi.key != key || abi.registry.lock() != registry) continue;
resolveStages(abi.stages);
return;
}
DrawDecode decoded{};
Expand Down Expand Up @@ -1624,6 +1655,10 @@ void Driver::ResolveShaderAbi(const Shader* shader, std::span<const ShaderRegist
}
};
const bool committedAbi = CommittedAbi(*snapshot, key);
if (committedAbi && !transaction.Tracks(*snapshot)) {
transaction.Commit();
return;
}
auto entries = committedAbi ? std::vector<PreparedShaders::Entry>{} : prepare();
const auto& registeredAbis = transaction.Read(*snapshot).registeredAbis;
if (std::ranges::find(registeredAbis, key) != registeredAbis.end()) {
Expand Down Expand Up @@ -1709,6 +1744,10 @@ void ResolvePreparedGraphics(const ShaderSnapshot& front, const std::shared_ptr<
return;
}
}
if (!transaction.Tracks(front) && CommittedLink(front, fragment, primitiveType)) {
transaction.Commit();
return;
}
std::vector<std::shared_ptr<const ShaderSnapshot>> fragments;
{
const auto& current = transaction.Read(front);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,34 @@ void TransactionLock() {
Require(excluded, "a transaction ran beside one that had read prepared state");
}

void CommittedLinks() {
ShaderSnapshot front{};
auto pixel = std::make_shared<ShaderSnapshot>();
ResolvePreparedGraphics(front, pixel, 0, {});
Require(front.prepared->fragments.size() == 1 && front.prepared->fragments.front().lock() == pixel, "a resolved link was not published");
ShaderSnapshot unrelated{};
auto other = std::make_shared<ShaderSnapshot>();
std::optional<ShaderPreparationTransaction> open(std::in_place);
static_cast<void>(open->Read(unrelated));
auto repeated = std::async(std::launch::async, [&] { ResolvePreparedGraphics(front, pixel, 0, {}); });
const bool unblocked = repeated.wait_for(std::chrono::seconds(10)) == std::future_status::ready;
auto linking = std::async(std::launch::async, [&] { ResolvePreparedGraphics(front, other, 0, {}); });
const bool excluded = linking.wait_for(std::chrono::milliseconds(200)) == std::future_status::timeout;
open->Commit();
open.reset();
repeated.get();
linking.get();
Require(unblocked, "resolving a link the shader already had waited for another thread's transaction");
Require(excluded && front.prepared->fragments.size() == 2, "a new link was published beside a transaction that had read prepared state");
{
ShaderPreparationTransaction transaction;
transaction.Edit(front).fragments.clear();
ResolvePreparedGraphics(front, pixel, 0, {});
transaction.Commit();
}
Require(front.prepared->fragments.size() == 1 && front.prepared->fragments.front().lock() == pixel, "a link resolved in a transaction that had changed the shader was checked against the published links");
}

void PreparationPriority(bool beforeSubmit) {
const auto workers = std::max(2u, std::thread::hardware_concurrency() / 2u);
std::barrier started(static_cast<std::ptrdiff_t>(workers + 1u));
Expand Down Expand Up @@ -415,6 +443,7 @@ int main() {
RegistryVersions();
FusedHullHeaderSelection();
TransactionLock();
CommittedLinks();
PreparationPriority(false);
PreparationPriority(true);
auto device = OpenVulkanTestDevice();
Expand Down
58 changes: 58 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/execution/PreparedShaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,63 @@ void AbiDeferredRectanglePairs() {
ExpectFailure([] { AgcDriverShutdown_nid_postfix(); }, "custom per-vertex interpolation is unsupported");
}

void CommittedAbis() {
static AbiHeader front;
static AbiHeader pixel;
DescribeFront(front);
DescribeAbi(pixel, PassthroughPixelCode, 1, {{0x8, ProgramLow(PassthroughPixelCode.data())}, {0x9, ProgramHigh(PassthroughPixelCode.data())}, {0xa, 0}, {0xb, 0}}, {{0x1b6, 1}, {0x1b3, 2}, {0x1b4, 2}, {0x191, 0}, {0x1c5, 4}});
for (auto* header : {&front, &pixel}) AgcDriverRegisterShader_nid_postfix(&header->shader);
const std::array<const Shader*, 2> stages{&front.shader, &pixel.shader};
const std::array<ShaderRegister, 1> primitive{{{0x242, 4}}};
const std::array<ShaderRegister, 1> interpolant{{{0x191, 0x20}}};
const auto resolve = [&] {
AgcDriver::ShaderPreparationScope scope({&front.shader, &pixel.shader});
AgcDriverResolveShaderAbi_nid_postfix(&pixel.shader, {}, {});
AgcDriverResolveGraphicsStagesAbi_nid_postfix(stages, {}, primitive);
AgcDriverResolveGraphicsAbi_nid_postfix(&front.shader, &pixel.shader, 0);
scope.Commit();
};
const auto resolveOther = [&] {
AgcDriver::ShaderPreparationScope scope({&pixel.shader});
AgcDriverResolveShaderAbi_nid_postfix(&pixel.shader, interpolant, {});
scope.Commit();
};
resolve();
const auto registry = AgcDriver::DriverDetail::Driver::Get().RegisteredShaders();
Require(registry != nullptr, "committed ABI resolution published no shader registry");
const auto pixelSnapshot = AgcDriver::DriverDetail::RegisteredHeader(*registry, &pixel.shader);
Require(pixelSnapshot != nullptr, "committed ABI resolution published no pixel snapshot");
const auto committedAbis = [&] {
std::lock_guard lock(pixelSnapshot->prepared->mutex);
return pixelSnapshot->prepared->registeredAbis.size();
};
AgcDriver::DriverDetail::AwaitRegistrationPreparation();
const auto before = ShaderRecompiler::FrontEndBuilds();
const auto abisBefore = committedAbis();
bool unblocked = false;
bool excluded = false;
std::uint64_t repeatedBuilds = 0;
std::future<void> repeated;
std::future<void> other;
{
AgcDriver::DriverDetail::ShaderSnapshot unrelated{};
AgcDriver::DriverDetail::ShaderPreparationTransaction open;
static_cast<void>(open.Read(unrelated));
repeated = std::async(std::launch::async, resolve);
unblocked = repeated.wait_for(std::chrono::seconds(10)) == std::future_status::ready;
repeatedBuilds = ShaderRecompiler::FrontEndBuilds() - before;
other = std::async(std::launch::async, resolveOther);
const auto started = std::chrono::steady_clock::now();
while (ShaderRecompiler::FrontEndBuilds() == before + repeatedBuilds && std::chrono::steady_clock::now() - started < std::chrono::seconds(10)) std::this_thread::yield();
excluded = other.wait_for(std::chrono::milliseconds(500)) == std::future_status::timeout && committedAbis() == abisBefore;
open.Commit();
}
repeated.get();
other.get();
Require(unblocked && repeatedBuilds == 0, "resolving ABIs the shaders already had waited for another thread's shader preparation, or prepared them again");
Require(excluded, "a new ABI was published beside a shader preparation that had read prepared state");
}

void RelocatedHeaders() {
alignas(256) static const std::array<std::uint32_t, 11> code{0x7e0002ffu, 0, 0x7e0202ffu, 0, 0x7e0402ffu, 0, 0x7e0602ffu, 0x3f800000u, 0xf80008cfu, 0x03020100u, 0xbf810000u};
struct Header {
Expand Down Expand Up @@ -1318,6 +1375,7 @@ int main(int argc, char** argv) {
device.reset();
SharedCodeHeaders();
RelocatedHeaders();
CommittedAbis();
RegistrationWithoutSpecials();
Registration(argc == 2);
std::cout << "prepared shader and transactional registration tests passed\n";
Expand Down
Loading