Skip to content
Open
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 @@ -573,7 +573,7 @@ class ShaderResources {
// `written` is the element's DescriptorBinding::bufferWritten: a read-only element binds the
// same way but is left out of the write set (no write-back, no pending-write note). `atomic`
// is its bufferAtomic (see GuestBufferMemory::AddWritable).
std::size_t addGuestBuffer(std::span<const std::uint32_t> words, const ColorTarget* target, std::uint64_t indexAddress, std::size_t indexBytes, bool written, bool atomic, bool read, std::uint32_t extent);
std::size_t addGuestBuffer(std::span<const std::uint32_t> words, const ColorTarget* target, std::uint64_t indexAddress, std::size_t indexBytes, bool written, bool atomic, bool read, std::uint32_t extent, bool deferTypeFault);
std::uint64_t guestBufferBytes(std::span<const std::uint32_t> words) const;
std::size_t addDataBuffer(std::span<const std::uint32_t> words);
void requireGuestBuffers(std::span<const MovedBuffer> moved = {}) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,14 @@ void ShaderResources::prepareAddressBindings(std::span<const CompiledShader> sha
if (tables != 0 && storesThroughAddressesCached(shader.program->spirv)) bdaReadOnly = false;
usesFaultBuffer = usesFaultBuffer || faults != 0;
const bool tableFaultsOnly = std::any_of(shader.program->bindings.begin(), shader.program->bindings.end(), [](const ShaderRecompiler::DescriptorBinding& binding) { return binding.role == ShaderRecompiler::DescriptorRole::ImageTableMap; });
faultChecks = faultChecks || tables != 0 || (faults != 0 && (!tableFaultsOnly || shader.program->imageTableFaults != 0 || shader.program->poisonedSrtReads != 0 || shader.program->windowLaneGuards != 0 || shader.program->descriptorFaults != 0 || shader.program->splitStateLanes != 0 || loopGuarded));
const bool deferredBufferTypeFaults = std::any_of(shader.program->bindings.begin(), shader.program->bindings.end(), [](const ShaderRecompiler::DescriptorBinding& binding) {
if (binding.role != ShaderRecompiler::DescriptorRole::GuestBuffers || binding.guestDescriptor.size() % 4u != 0u) return false;
for (std::size_t element = 0; element < binding.bufferDeferredTypeFault.size() && element * 4u + 3u < binding.guestDescriptor.size(); ++element) {
if (binding.bufferDeferredTypeFault[element] && (binding.guestDescriptor[element * 4u + 3u] >> 30u) != 0u) return true;
}
return false;
});
faultChecks = faultChecks || tables != 0 || deferredBufferTypeFaults || (faults != 0 && (!tableFaultsOnly || shader.program->imageTableFaults != 0 || shader.program->poisonedSrtReads != 0 || shader.program->windowLaneGuards != 0 || shader.program->descriptorFaults != 0 || shader.program->splitStateLanes != 0 || loopGuarded));
}
if (usesBda) {
Require(context.bufferDeviceAddress, "buffer device address is not enabled");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2966,7 +2966,8 @@ void ShaderResources::buildPrepare(std::span<const CompiledShader> shaders, cons
const bool atomic = element < binding.bufferAtomic.size() && binding.bufferAtomic[element];
const bool read = element < binding.bufferRead.size() && binding.bufferRead[element];
const std::uint32_t extent = element < binding.bufferExtent.size() ? binding.bufferExtent[element] : 0u;
const auto index = addGuestBuffer(std::span<const std::uint32_t>(binding.guestDescriptor).subspan(static_cast<std::size_t>(element) * 4, 4), target, indexAddress, indexBytes, written, atomic, read, extent);
const bool deferTypeFault = element < binding.bufferDeferredTypeFault.size() && binding.bufferDeferredTypeFault[element];
const auto index = addGuestBuffer(std::span<const std::uint32_t>(binding.guestDescriptor).subspan(static_cast<std::size_t>(element) * 4, 4), target, indexAddress, indexBytes, written, atomic, read, extent, deferTypeFault);
const bool classified = element < binding.bufferWritten.size() && element < binding.bufferRead.size() && element < binding.bufferAtomic.size();
allocations[index].writeOnly = classified && allocations[index].guest && allocations[index].written && binding.bufferWritten[element] && !read && !atomic;
const auto& push = shader.program->pushConstants;
Expand Down Expand Up @@ -6086,9 +6087,13 @@ std::uint64_t ShaderResources::BytesUpToTarget(std::uint64_t address, std::uint6
return target->MemoryAddress() - address;
}

std::size_t ShaderResources::addGuestBuffer(std::span<const std::uint32_t> words, const ColorTarget* target, std::uint64_t indexAddress, std::size_t indexBytes, bool written, bool atomic, bool read, std::uint32_t extent) {
std::size_t ShaderResources::addGuestBuffer(std::span<const std::uint32_t> words, const ColorTarget* target, std::uint64_t indexAddress, std::size_t indexBytes, bool written, bool atomic, bool read, std::uint32_t extent, bool deferTypeFault) {
Require(words.size() == 4, "buffer descriptor must contain four DWORDs");
const ShaderRecompiler::ShaderBufferResource descriptor{{words[0], words[1] & ~0x40000000u, words[2], words[3]}};
if (descriptor.Type() != 0u && deferTypeFault) {
allocations.push_back({0, EmptyBufferBytes, false, nullptr, ShaderRecompiler::DescriptorRole::GuestBuffers, false});
return allocations.size() - 1;
}
Require(descriptor.Type() == 0u, "buffer descriptor uses an unsupported type");
const auto address = descriptor.Base48();
const auto byteSize = guestBufferBytes(words);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@ if(BUILD_TESTING)
set_tests_properties(agc_driver_draw_unsupported_buffer_type_skip_refused agc_driver_draw_unsupported_buffer_type_skip_refused_finisher agc_driver_draw_unsupported_buffer_type_skip_refused_draw_pipeline PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
add_test(NAME agc_driver_draw_unsupported_buffer_type_deferred_fault COMMAND agc_driver_draw_unsupported_buffer_type_tests --deferred-fault)
set_tests_properties(agc_driver_draw_unsupported_buffer_type_deferred_fault PROPERTIES
ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
if(WIN32)
set_tests_properties(agc_driver_draw_unsupported_buffer_type_deferred_fault PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
agc_add_draw_pipeline_test(agc_driver_draw_unsupported_buffer_type)
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,32 @@ alignas(256) constexpr std::array<std::uint32_t, 16> VertexCode{
alignas(256) constexpr std::array<std::uint32_t, 16> PixelCode{
0x7e020280u, 0xe0301000u, 0x80000201u, 0xbf8c3f70u, 0x7e000280u, 0x7e0602f2u, 0xf800180fu, 0x03010002u, 0xbf810000u,
};
alignas(256) constexpr std::array<std::uint32_t, 9> InactivePixelCode{
0xbefe0404u, 0xe0301000u, 0x80000201u, 0xbf8c3f70u, 0x7e000280u, 0x7e0602f2u, 0xf800180fu, 0x03010002u, 0xbf810000u,
};
alignas(256) constexpr std::array<std::uint32_t, 16> WritingPixelCode{
0x7e020280u, 0xe0301000u, 0x80000201u, 0xbf8c3f70u, 0xe0701000u, 0x80000201u, 0x7e000280u, 0x7e0602f2u, 0xf800180fu, 0x03010002u, 0xbf810000u,
};

void Append(std::vector<std::uint32_t>& commands, std::uint32_t opcode, std::initializer_list<std::uint32_t> payload) {
commands.push_back(0xc0000000u | (static_cast<std::uint32_t>(payload.size() - 1) << 16u) | (opcode << 8u));
commands.insert(commands.end(), payload);
}

void Draw(const std::array<std::uint32_t, 4>& descriptor, std::uint32_t* label = nullptr) {
enum class Pixel { Load, Inactive, Writing };

const void* PixelAddress(Pixel kind) {
switch (kind) {
case Pixel::Inactive: return InactivePixelCode.data();
case Pixel::Writing: return WritingPixelCode.data();
case Pixel::Load: break;
}
return PixelCode.data();
}

void Draw(const std::array<std::uint32_t, 4>& descriptor, Pixel kind = Pixel::Load, std::uint32_t* label = nullptr) {
const auto vertex = reinterpret_cast<std::uintptr_t>(VertexCode.data());
const auto pixel = reinterpret_cast<std::uintptr_t>(PixelCode.data());
const auto pixel = reinterpret_cast<std::uintptr_t>(PixelAddress(kind));
const std::vector<std::pair<std::uint32_t, std::uint32_t>> context{
{0x2d5, 0x2000},
{0x1b6, 0}, {0x207, 0}, {0x200, 0}, {0x203, 0x800},
Expand All @@ -59,8 +76,9 @@ void Draw(const std::array<std::uint32_t, 4>& descriptor, std::uint32_t* label =
};
const std::vector<std::pair<std::uint32_t, std::uint32_t>> shader{
{0xc8, static_cast<std::uint32_t>(vertex >> 8u)}, {0xc9, static_cast<std::uint32_t>(vertex >> 40u)}, {0x8b, 0u},
{0x008, static_cast<std::uint32_t>(pixel >> 8u)}, {0x009, static_cast<std::uint32_t>(pixel >> 40u)}, {0x00b, 4u << 1u},
{0x008, static_cast<std::uint32_t>(pixel >> 8u)}, {0x009, static_cast<std::uint32_t>(pixel >> 40u)}, {0x00b, 6u << 1u},
{0x00c, descriptor[0]}, {0x00d, descriptor[1]}, {0x00e, descriptor[2]}, {0x00f, descriptor[3]},
{0x010, 0}, {0x011, 0},
};
std::vector<std::uint32_t> commands;
Append(commands, 0x79, {0x242, 4});
Expand All @@ -79,15 +97,29 @@ void Draw(const std::array<std::uint32_t, 4>& descriptor, std::uint32_t* label =
AgcDriverWaitIdle_nid_postfix();
}

void NamesShaderAndDescriptor() {
void ReportsDeferredTypeFault() {
const auto address = reinterpret_cast<std::uintptr_t>(Source.data());
const std::array<std::uint32_t, 4> descriptor{static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((address >> 32u) & 0xffffu), sizeof(Source), 0xa1b00facu};
try {
Draw(descriptor);
} catch (const std::exception& error) {
const std::string message = error.what();
Require(message.find("BDA access failed") != std::string::npos, "draw failed for another reason: " + message);
Require(message.find("stage=2 reason=8") != std::string::npos, "draw did not report an invalid fragment descriptor: " + message);
return;
}
throw std::runtime_error("a pixel shader read a buffer through a descriptor of type 2 without reporting a draw-completion fault");
}

void NamesShaderAndDescriptor() {
const auto address = reinterpret_cast<std::uintptr_t>(Source.data());
const std::array<std::uint32_t, 4> descriptor{static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((address >> 32u) & 0xffffu), sizeof(Source), 0xa1b00facu};
try {
Draw(descriptor, Pixel::Writing);
} catch (const std::exception& error) {
const std::string message = error.what();
char shader[64];
std::snprintf(shader, sizeof(shader), "shader 0x%llx stage %u", static_cast<unsigned long long>(reinterpret_cast<std::uintptr_t>(PixelCode.data())), static_cast<unsigned>(ShaderRecompiler::ShaderStage::Fragment));
std::snprintf(shader, sizeof(shader), "shader 0x%llx stage %u", static_cast<unsigned long long>(reinterpret_cast<std::uintptr_t>(WritingPixelCode.data())), static_cast<unsigned>(ShaderRecompiler::ShaderStage::Fragment));
char words[64];
std::snprintf(words, sizeof(words), "(buffer 0 V# %08x %08x %08x %08x)", descriptor[0], descriptor[1], descriptor[2], descriptor[3]);
Require(message.find("buffer descriptor uses an unsupported type") != std::string::npos, "draw failed for another reason: " + message);
Expand All @@ -96,7 +128,13 @@ void NamesShaderAndDescriptor() {
if (std::getenv("APS5_DRAW_PIPELINE") != nullptr) Require(message.find("draw at submission ") != std::string::npos && message.find("(finished on the draw finisher)") != std::string::npos, "a draw finished on the draw finisher does not name its submission and offset: " + message);
return;
}
throw std::runtime_error("a pixel shader read a buffer through a descriptor of type 2");
throw std::runtime_error("a pixel shader that writes a buffer through a descriptor of type 2 was not refused");
}

void InactiveLaneDoesNotFault() {
const auto address = reinterpret_cast<std::uintptr_t>(Source.data());
const std::array<std::uint32_t, 4> descriptor{static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((address >> 32u) & 0xffffu), sizeof(Source), 0xa1b00facu};
Draw(descriptor, Pixel::Inactive);
}

void SkipsRefusedDraw() {
Expand All @@ -105,7 +143,7 @@ void SkipsRefusedDraw() {
alignas(8) static std::uint32_t label = 0;
for (int submission = 0; submission < 2; ++submission) {
label = 0;
Draw(descriptor, &label);
Draw(descriptor, Pixel::Writing, &label);
Require(label == 7u, "the packet after a skipped refused draw was not executed");
}
for (const auto byte : Pixels) Require(byte == std::byte{0}, "a skipped refused draw wrote its color target");
Expand All @@ -115,13 +153,19 @@ void SkipsRefusedDraw() {

int main(int argc, char** argv) {
try {
Require(argc == 1 || (argc == 2 && std::string_view(argv[1]) == "--skip-refused"), "invalid test arguments");
Require(argc == 1 || (argc == 2 && (std::string_view(argv[1]) == "--skip-refused" || std::string_view(argv[1]) == "--deferred-fault")), "invalid test arguments");
{
const auto device = OpenVulkanTestDevice();
if (!device) return VulkanTestSkipped;
}
if (argc == 2) SkipsRefusedDraw();
else NamesShaderAndDescriptor();
if (argc == 2 && std::string_view(argv[1]) == "--skip-refused") {
SkipsRefusedDraw();
} else if (argc == 2) {
ReportsDeferredTypeFault();
} else {
InactiveLaneDoesNotFault();
NamesShaderAndDescriptor();
}
try { AgcDriverShutdown_nid_postfix(); } catch (const std::exception&) {}
std::puts("draw unsupported buffer type tests passed");
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct ShaderInfo {
bool usesDma = false;
bool bdaWrites = false;
bool usesFaultBuffer = false;
std::vector<bool> deferInvalidBufferTypeFaults;
bool dispatchThreadLimit = false;
bool rasterPrimitive = false;
bool layerExported = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ DescriptorBindingPlan DescriptorBindingBuilder::Prepare(const IrBindingLayout& l
physical.bufferWritten.push_back(buffer.written || buffer.atomic);
physical.bufferRead.push_back(buffer.read);
physical.bufferExtent.push_back(buffer.maxByteExtent);
physical.bufferDeferredTypeFault.push_back(resource < info.deferInvalidBufferTypeFaults.size() && info.deferInvalidBufferTypeFaults[resource]);
if (buffer.written || buffer.atomic) ++entry.writtenBuffers;
else ++entry.readOnlyBuffers;
}
Expand Down
35 changes: 33 additions & 2 deletions core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,31 @@ bool disabledBufferAccesses(const IrResourcePlan& plan, std::uint32_t index, con
return accessed;
}

std::vector<bool> DeferredBufferTypeFaults(const IrProgram& program) {
const auto& resources = program.Resources();
std::vector<bool> deferred(resources.info.buffers.size(), resources.stage == IrShaderStage::Pixel);
std::vector<bool> bufferUsed(resources.info.buffers.size());
for (const auto& block : program.Blocks()) {
for (const auto* inst : block->Instructions()) {
if (BufferAccessOf(inst->Opcode()) == BufferAccess::None) continue;
const auto& memory = resources.memoryInfo.at(inst->Flags<MemoryFlags>().index);
if (memory.resource >= bufferUsed.size()) continue;
bufferUsed[memory.resource] = true;
if (memory.kind != ResourceKind::Buffer || memory.gpuDescriptor || BufferAccessOf(inst->Opcode()) != BufferAccess::Read
|| memory.formatted || memory.typed || memory.dataBits != 32u || memory.dataDwords == 0u || memory.dataDwords > 4u
|| (inst->Opcode() != IrOpcode::LoadBufferU32 && inst->Opcode() != IrOpcode::LoadBufferU32x2 && inst->Opcode() != IrOpcode::LoadBufferU32x3 && inst->Opcode() != IrOpcode::LoadBufferU32x4)) {
deferred[memory.resource] = false;
}
}
}
for (std::uint32_t i = 0; i < resources.info.buffers.size(); i++) {
const auto& buffer = resources.info.buffers[i];
deferred[i] = deferred[i] && bufferUsed[i] && buffer.read && !buffer.written && !buffer.atomic && !buffer.formatted && !buffer.scalar
&& buffer.imageAlias == BufferResource::NoImageAlias;
}
return deferred;
}

void materializeTables(const IrResourcePlan& plan, ResourceSnapshot& snapshot) {
for (std::uint32_t i = 0u; i < plan.info.buffers.size(); ++i) {
const auto& buffer = plan.info.buffers[i];
Expand All @@ -1930,13 +1955,14 @@ void materializeTables(const IrResourcePlan& plan, ResourceSnapshot& snapshot) {
snapshot.buffers.at(i).dwords[3] = 0u;
}
const auto decoded = decodeBufferDescriptor(snapshot.buffers.at(i));
if (decoded.Type() != 0u) {
if (decoded.Type() == 0u && plan.stage != IrShaderStage::Compute && decoded.AddTid()) throw std::runtime_error("buffer ADD_TID is only valid for compute shaders");
const bool deferInvalidType = i < plan.info.deferInvalidBufferTypeFaults.size() && plan.info.deferInvalidBufferTypeFaults[i] && plan.stage == IrShaderStage::Pixel;
if (decoded.Type() != 0u && !deferInvalidType) {
const auto& words = snapshot.buffers.at(i).dwords;
char detail[96];
std::snprintf(detail, sizeof(detail), " (buffer %u V# %08x %08x %08x %08x) first used at pc 0x%x", i, words[0], words[1], words[2], words[3], buffer.firstUsePc);
throw std::runtime_error(std::string("buffer descriptor uses an unsupported type") + detail);
}
if (plan.stage != IrShaderStage::Compute && decoded.AddTid()) throw std::runtime_error("buffer ADD_TID is only valid for compute shaders");
}
if (snapshot.flattenedSrt.size() != plan.srtReads.size()) throw std::runtime_error("runtime SRT size differs from the static interface");
for (std::uint32_t i = 0u; i < plan.info.images.size(); ++i) {
Expand Down Expand Up @@ -2807,6 +2833,10 @@ std::uint32_t ResourceMaterializer::EmulatedCompareState(const ShaderInfo& info,
void ResourceMaterializer::ApplyStaticInterface(IrProgram& program, bool nativeSampleOffsets) const {
auto& resources = program.Resources();
if (!resources.resourceTrackingComplete || !resources.srtPlanComplete) throw std::runtime_error("static resource interface requires a completed resource plan");
const auto deferInvalidBufferTypeFaults = DeferredBufferTypeFaults(program);
const bool usesDeferredBufferFaults = std::ranges::any_of(deferInvalidBufferTypeFaults, [](bool deferred) { return deferred; });
resources.info.deferInvalidBufferTypeFaults = deferInvalidBufferTypeFaults;
if (usesDeferredBufferFaults) resources.info.usesFaultBuffer = true;
auto images = resources.info.images;
if (!nativeSampleOffsets) {
for (const auto& block : program.Blocks()) {
Expand Down Expand Up @@ -3071,6 +3101,7 @@ IrResourcePlan ResourceMaterializer::ExtractPlan(const IrProgram& program) const
plan.srtPlanComplete = source.srtPlanComplete;
plan.resourceTrackingComplete = source.resourceTrackingComplete;
plan.info = source.info;
plan.info.deferInvalidBufferTypeFaults = DeferredBufferTypeFaults(program);
PrepareImageModes(plan.info);
plan.uniformFill = source.uniformFill;
std::unordered_map<const IrValue*, bool> decided;
Expand Down
1 change: 1 addition & 0 deletions core/shader/recompiler/Recompiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ struct DescriptorBinding {
ElementFlags bufferRead;
std::vector<std::uint32_t> bufferExtent;
std::vector<ImageOverwrite> imageOverwrites;
std::vector<bool> bufferDeferredTypeFault;
};

struct ImageTableEntryPoison {
Expand Down
Loading
Loading