diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/BdaFeatures.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/BdaFeatures.cpp index 18f35209d0f1..fafc0ef33edc 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/BdaFeatures.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/BdaFeatures.cpp @@ -6,12 +6,11 @@ namespace AgcDriver { VkPhysicalDevice8BitStorageFeatures QueryBdaByteFeatures(VkPhysicalDevice physical, PFN_vkGetPhysicalDeviceFeatures2 query, std::span extensions) { Graphics::Require(query != nullptr, "missing Vulkan feature query"); - Graphics::Require(std::any_of(extensions.begin(), extensions.end(), [](const auto& extension) { return std::strcmp(extension.extensionName, VK_KHR_8BIT_STORAGE_EXTENSION_NAME) == 0; }), "VK_KHR_8bit_storage is unavailable"); VkPhysicalDevice8BitStorageFeatures bytes{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES}; + if (std::none_of(extensions.begin(), extensions.end(), [](const auto& extension) { return std::strcmp(extension.extensionName, VK_KHR_8BIT_STORAGE_EXTENSION_NAME) == 0; })) return bytes; VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &bytes}; query(physical, &features); - Graphics::Require(bytes.storageBuffer8BitAccess == VK_TRUE, "BDA requires exact byte access through storageBuffer8BitAccess"); - return {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES, nullptr, VK_TRUE, VK_FALSE, VK_FALSE}; + return {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES, nullptr, bytes.storageBuffer8BitAccess, VK_FALSE, VK_FALSE}; } VkPhysicalDeviceBufferDeviceAddressFeatures QueryBdaFeatures(VkPhysicalDevice physical, PFN_vkGetPhysicalDeviceFeatures2 query, std::span extensions) { diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/HostReportVulkan.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/HostReportVulkan.cpp index b26ae078989d..c95d486ddff6 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/HostReportVulkan.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/HostReportVulkan.cpp @@ -85,26 +85,17 @@ HostGpuFacts CollectHostGpuFacts(const HostGpuQuery& query) { if ((memory.memoryHeaps[index].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0) largest = std::max(largest, memory.memoryHeaps[index].size); gpu.deviceLocalMiB = largest / (1024u * 1024u); - VkPhysicalDevice8BitStorageFeatures bytes{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES}; VkPhysicalDeviceBufferDeviceAddressFeatures address{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES}; VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2}; - const bool hasBytes = has(VK_KHR_8BIT_STORAGE_EXTENSION_NAME); const bool hasAddress = has(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME); - void** featureNext = &features.pNext; - if (hasBytes) { - *featureNext = &bytes; - featureNext = &bytes.pNext; - } - if (hasAddress) *featureNext = &address; + if (hasAddress) features.pNext = &address; load(query, "vkGetPhysicalDeviceFeatures2")(query.physical, &features); const auto& core = features.features; const auto required = [&](const char* name, const char* kind, bool present) { gpu.requirements.push_back({name, kind, true, present}); }; const auto optional = [&](const char* name) { gpu.requirements.push_back({name, "extension", false, has(name)}); }; if (query.presentation) required(VK_KHR_SWAPCHAIN_EXTENSION_NAME, "extension", has(VK_KHR_SWAPCHAIN_EXTENSION_NAME)); - required(VK_KHR_8BIT_STORAGE_EXTENSION_NAME, "extension", hasBytes); required(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME, "extension", hasAddress); required(VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME, "extension", hasFloatControls); - required("storageBuffer8BitAccess", "feature", hasBytes && bytes.storageBuffer8BitAccess == VK_TRUE); required("bufferDeviceAddress", "feature", hasAddress && address.bufferDeviceAddress == VK_TRUE); required("shaderInt64", "feature", core.shaderInt64 == VK_TRUE); required("shaderSignedZeroInfNanPreserveFloat32", "feature", hasFloatControls && floatControls.shaderSignedZeroInfNanPreserveFloat32 == VK_TRUE); @@ -118,7 +109,7 @@ HostGpuFacts CollectHostGpuFacts(const HostGpuQuery& query) { VK_KHR_MAINTENANCE_5_EXTENSION_NAME, VK_EXT_HDR_METADATA_EXTENSION_NAME, VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME, VK_EXT_SHADER_IMAGE_ATOMIC_INT64_EXTENSION_NAME, VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME, VK_KHR_DRAW_INDIRECT_COUNT_EXTENSION_NAME, VK_EXT_SAMPLE_LOCATIONS_EXTENSION_NAME, VK_EXT_SAMPLER_FILTER_MINMAX_EXTENSION_NAME, VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME, - VK_KHR_PRESENT_MODE_FIFO_LATEST_READY_EXTENSION_NAME, "VK_EXT_present_mode_fifo_latest_ready"}) + VK_KHR_PRESENT_MODE_FIFO_LATEST_READY_EXTENSION_NAME, "VK_EXT_present_mode_fifo_latest_ready", VK_KHR_8BIT_STORAGE_EXTENSION_NAME}) optional(name); if (query.surface != VK_NULL_HANDLE) { diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/ShaderDeviceProfile.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/ShaderDeviceProfile.cpp index c51adce86c77..1ffee220cd54 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/ShaderDeviceProfile.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/ShaderDeviceProfile.cpp @@ -36,17 +36,17 @@ ShaderDeviceProfile::ShaderDeviceProfile(const ShaderRecompiler::SpirvTarget& ta const auto* maintenance8 = findFeatures(deviceInfo.pNext, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_8_FEATURES_KHR); Require(target.bdaAbiVersion == ShaderRecompiler::BdaAbi::Version, "shader device profile has an incompatible BDA ABI"); Require(bda != nullptr && bda->bufferDeviceAddress == VK_TRUE && core.shaderInt64 == VK_TRUE, "shader runtime requires enabled bufferDeviceAddress and shaderInt64"); - Require(bytes != nullptr && bytes->storageBuffer8BitAccess == VK_TRUE, "shader runtime requires enabled storageBuffer8BitAccess"); Require(core.vertexPipelineStoresAndAtomics == VK_TRUE && core.fragmentStoresAndAtomics == VK_TRUE, "shader runtime requires enabled graphics stores and atomics"); Require(limits.maxPushConstantsSize >= ShaderRecompiler::RuntimeAbi::PushConstantDwords * sizeof(std::uint32_t) && limits.maxBoundDescriptorSets > ShaderRecompiler::RuntimeAbi::DescriptorSet, "shader runtime ABI exceeds device limits"); Require(limits.maxStorageBufferRange >= sizeof(ShaderRecompiler::RuntimeAbi::ShaderData), "shader runtime ShaderData exceeds device limits"); std::ranges::sort(capabilities); capabilities.erase(std::unique(capabilities.begin(), capabilities.end()), capabilities.end()); const auto hasCapability = [&](spv::Capability capability) { return std::ranges::binary_search(capabilities, static_cast(capability)); }; - Require(hasCapability(spv::CapabilityShader) && hasCapability(spv::CapabilityInt64) && hasCapability(spv::CapabilityPhysicalStorageBufferAddresses) && hasCapability(spv::CapabilityStorageBuffer8BitAccess), "shader device profile is missing runtime capabilities"); + Require(hasCapability(spv::CapabilityShader) && hasCapability(spv::CapabilityInt64) && hasCapability(spv::CapabilityPhysicalStorageBufferAddresses), "shader device profile is missing runtime capabilities"); const auto checkFeature = [&](spv::Capability capability, bool enabled, const char* name) { Require(!hasCapability(capability) || enabled, std::string("shader device profile advertises a disabled feature: ") + name); }; + checkFeature(spv::CapabilityStorageBuffer8BitAccess, bytes != nullptr && bytes->storageBuffer8BitAccess == VK_TRUE, "storageBuffer8BitAccess"); checkFeature(spv::CapabilityFloat64, core.shaderFloat64 == VK_TRUE, "shaderFloat64"); checkFeature(spv::CapabilityInt16, core.shaderInt16 == VK_TRUE, "shaderInt16"); checkFeature(spv::CapabilityGeometry, core.geometryShader == VK_TRUE, "geometryShader"); @@ -82,7 +82,8 @@ ShaderDeviceProfile::ShaderDeviceProfile(const ShaderRecompiler::SpirvTarget& ta std::ranges::sort(extensions); extensions.erase(std::unique(extensions.begin(), extensions.end()), extensions.end()); for (const auto& extension : extensions) extensionViews.emplace_back(extension); - Require(std::ranges::find(extensionViews, "SPV_KHR_physical_storage_buffer") != extensionViews.end() && std::ranges::find(extensionViews, "SPV_KHR_8bit_storage") != extensionViews.end(), "shader device profile is missing runtime extensions"); + Require(std::ranges::find(extensionViews, "SPV_KHR_physical_storage_buffer") != extensionViews.end(), "shader device profile is missing runtime extensions"); + Require(!hasCapability(spv::CapabilityStorageBuffer8BitAccess) || std::ranges::find(extensionViews, "SPV_KHR_8bit_storage") != extensionViews.end(), "shader device profile is missing runtime extensions"); Require(std::ranges::find(extensionViews, "SPV_KHR_terminate_invocation") == extensionViews.end() || (terminate != nullptr && terminate->shaderTerminateInvocation == VK_TRUE), "shader device profile advertises a disabled feature: shaderTerminateInvocation"); this->target.supportedCapabilities = capabilities; this->target.supportedExtensions = extensionViews; diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp index c5a332eba2e3..0ae857a27922 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp @@ -1858,9 +1858,11 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window, bool compileOnly) : state->spirvExtensions.push_back("SPV_KHR_fma"); } deviceExtensions.push_back(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME); - deviceExtensions.push_back(VK_KHR_8BIT_STORAGE_EXTENSION_NAME); - state->capabilities.push_back(4448); - state->spirvExtensions.push_back("SPV_KHR_8bit_storage"); + if (byteFeatures.storageBuffer8BitAccess == VK_TRUE) { + deviceExtensions.push_back(VK_KHR_8BIT_STORAGE_EXTENSION_NAME); + state->capabilities.push_back(4448); + state->spirvExtensions.push_back("SPV_KHR_8bit_storage"); + } state->capabilities.push_back(11); state->capabilities.push_back(5347); state->spirvExtensions.push_back("SPV_KHR_physical_storage_buffer"); @@ -2519,7 +2521,7 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window, bool compileOnly) : std::fprintf(stderr, "[gpu] APS5_NV_DIAGNOSTICS: the device does not support VK_NV_device_diagnostics_config\n"); } const bool diagnosticCheckpoints = diagnostics.config && diagnostics.checkpoints; - bdaFeatures.pNext = &byteFeatures; + bdaFeatures.pNext = byteFeatures.storageBuffer8BitAccess == VK_TRUE ? &byteFeatures : byteFeatures.pNext; VkPhysicalDeviceRobustness2FeaturesEXT robustness2{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT}; if (hasExtension(VK_EXT_ROBUSTNESS_2_EXTENSION_NAME)) { VkPhysicalDeviceFeatures2 queried{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &robustness2}; diff --git a/core/libs/prx/libSceAgcDriver/tests/BdaContracts.cpp b/core/libs/prx/libSceAgcDriver/tests/BdaContracts.cpp index ccf33f592a8e..1acf3261ca44 100644 --- a/core/libs/prx/libSceAgcDriver/tests/BdaContracts.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/BdaContracts.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -257,6 +258,26 @@ void RunBdaContractTests() { program.Resources().stage = IrShaderStage::Compute; program.Info().usesDma = true; ValidateBdaTarget(program, target); + const std::array wordCapabilities{spv::CapabilityInt64, spv::CapabilityPhysicalStorageBufferAddresses}; + const std::array wordExtensions{"SPV_KHR_physical_storage_buffer"}; + ValidateBdaTarget(program, SpirvTargetOptions{0x00401000u, 0x00010300u, 32, BdaAbi::Version, wordCapabilities, wordExtensions}); + const auto declaresBytes = [](const std::vector& words) { + for (std::size_t offset = 5; offset < words.size() && (words[offset] >> 16u) != 0u; offset += words[offset] >> 16u) { + const auto opcode = words[offset] & 0xffffu; + if (opcode == spv::OpCapability && words[offset + 1] == spv::CapabilityStorageBuffer8BitAccess) return true; + if (opcode == spv::OpTypeInt && words[offset + 2] == 8u) return true; + } + return false; + }; + for (const bool bytePointers : {true, false}) { + SetBdaTestBytePointers(bytePointers); + const std::array, bool>, 4> shaders{{{MakeBdaTestShader(0x1001, 8), false}, {MakeBdaTestShader(0x1001, 16), false}, {MakeBdaStoreTestShader(0x1001, 7, 8, false), true}, {MakeBdaWriteTestShader(0x1001, 7, 32, true), true}}}; + for (const auto& [shader, stores] : shaders) { + AgcDriver::Graphics::Require(declaresBytes(shader) == bytePointers, bytePointers ? "a BDA byte access did not use 8-bit pointers on a target with StorageBuffer8BitAccess" : "a BDA byte access declared 8-bit storage on a target without StorageBuffer8BitAccess"); + AgcDriver::Graphics::Require(SpirvStoresThroughAddresses(shader) == stores, "a BDA byte access was misread by the address store scan"); + } + } + SetBdaTestBytePointers(true); target.bdaAbiVersion = 0; reject([&] { ValidateBdaTarget(program, target); }, "ABI version"); target.bdaAbiVersion = BdaAbi::Version; diff --git a/core/libs/prx/libSceAgcDriver/tests/BdaDevice.cpp b/core/libs/prx/libSceAgcDriver/tests/BdaDevice.cpp index 9e9dd1263aa4..17acb2f0fdbf 100644 --- a/core/libs/prx/libSceAgcDriver/tests/BdaDevice.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/BdaDevice.cpp @@ -260,13 +260,14 @@ int main(int argc, char** argv) { Require(buffer.DeviceAddress() != 0 && buffer.Bytes().size() == 256, "invalid real BDA buffer"); buffer.Bytes()[255] = std::byte{0x5a}; Require(buffer.Bytes()[255] == std::byte{0x5a}, "real BDA buffer mapping failed"); - if (device->RunsOnCpu()) { - std::cout << "CPU Vulkan device: BDA read and multi-dword write execution not tested\n"; - } else { - RunBdaExecutionTests(device->GetContext()); + if (device->RunsOnCpu()) std::cout << "CPU Vulkan device: BDA read and multi-dword write execution not tested\n"; + for (const bool bytePointers : {true, false}) { + SetBdaTestBytePointers(bytePointers); + if (!device->RunsOnCpu()) RunBdaExecutionTests(device->GetContext()); + RunBdaStoreExecutionTests(device->GetContext()); + std::cout << (bytePointers ? "BDA store boundary tests passed with 8-bit pointers\n" : "BDA store boundary tests passed with 32-bit word byte access\n"); } - RunBdaStoreExecutionTests(device->GetContext()); - std::cout << "BDA store boundary tests passed\n"; + SetBdaTestBytePointers(true); RunBdaFaultSlotExecutionTests(device->GetContext()); std::cout << "BDA report-only fault slot tests passed\n"; RunColorTransferTests(device->GetContext()); diff --git a/core/libs/prx/libSceAgcDriver/tests/BdaShader.cpp b/core/libs/prx/libSceAgcDriver/tests/BdaShader.cpp index 924122965ce8..cd97a1ee9b5f 100644 --- a/core/libs/prx/libSceAgcDriver/tests/BdaShader.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/BdaShader.cpp @@ -3,12 +3,20 @@ #include "SpirvBackend/SpirvMemory/SpirvTypes.hpp" #include "SpirvBackend/SpirvMemory/SpirvConstants.hpp" #include "IntermediateRepresentation/IrBuilder.hpp" +#include #if ANYPS5_ENABLE_SPIRV_TOOLS #include "SpirvBackend/SpirvOptimizer.hpp" #endif namespace { +constexpr std::array BytePointerCapabilities{spv::CapabilityStorageBuffer8BitAccess}; +bool bytePointers = true; + +void UseTestTarget(ShaderRecompiler::SpirvEmitterState& state) { + if (bytePointers) state.supportedCapabilities = BytePointerCapabilities; +} + template std::vector MakeShader(std::uint64_t address, bool coherent, bool stops, const TEmit& emit, std::uint32_t extracted = 0, bool writes = false) { using namespace ShaderRecompiler; @@ -22,6 +30,7 @@ std::vector MakeShader(std::uint64_t address, bool coherent, bool program.Resources().memoryInfo = {memory}; } SpirvEmitterState state(program, {}); + UseTestTarget(state); EmitBaseHeader(state.module, program); const auto define = [&](std::uint32_t binding) { const auto variable = state.module.DefineGlobalVariable(TypeStorageBufferPointer(state), spv::StorageClassStorageBuffer); @@ -71,6 +80,10 @@ std::vector MakeShader(std::uint64_t address, bool coherent, bool } +void SetBdaTestBytePointers(bool enabled) { + bytePointers = enabled; +} + std::vector MakeBdaTestShader(std::uint64_t address, std::uint32_t bits, std::int64_t offset) { using namespace ShaderRecompiler; return MakeShader(address, false, true, [&](SpirvValueEmitContext& ctx, const IrValue& instruction, std::uint32_t base) { @@ -153,6 +166,7 @@ std::vector MakeBdaAnchoredReadTestShader(const BdaAnchoredReadPr program.Resources().memoryInfo = {memory}; } SpirvEmitterState state(program, {}); + UseTestTarget(state); EmitBaseHeader(state.module, program); if (reads.elect) { state.module.EmitCapability(spv::CapabilityGroupNonUniform); diff --git a/core/libs/prx/libSceAgcDriver/tests/BdaShader.hpp b/core/libs/prx/libSceAgcDriver/tests/BdaShader.hpp index 87c236c922d2..1255773e2095 100644 --- a/core/libs/prx/libSceAgcDriver/tests/BdaShader.hpp +++ b/core/libs/prx/libSceAgcDriver/tests/BdaShader.hpp @@ -7,6 +7,7 @@ #include #include +void SetBdaTestBytePointers(bool enabled); std::vector MakeBdaTestShader(std::uint64_t address, std::uint32_t bits, std::int64_t offset = 0); std::vector MakeBdaDwordReadTestShader(std::uint64_t address, std::uint32_t dwords, bool coherent, bool stops); std::vector MakeBdaSpanReadTestShader(std::uint64_t address, std::uint32_t offset, std::uint32_t extracted, bool coherent, bool stops); diff --git a/core/libs/prx/libSceAgcDriver/tests/HostReportVulkan.cpp b/core/libs/prx/libSceAgcDriver/tests/HostReportVulkan.cpp index 6108f55f0d98..f8d505b2311e 100644 --- a/core/libs/prx/libSceAgcDriver/tests/HostReportVulkan.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/HostReportVulkan.cpp @@ -175,10 +175,12 @@ void MissingExtensionsAreFound() { const auto missing = AgcDriver::MissingRequirements(gpu); std::vector names; for (const auto& item : missing) names.push_back(item.name); - const std::vector expected{VK_KHR_8BIT_STORAGE_EXTENSION_NAME, VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME, "bufferDeviceAddress", "storageBuffer8BitAccess"}; + const std::vector expected{VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME, "bufferDeviceAddress"}; Check(names == expected, "the missing extensions and the features that depend on them were not found"); const auto message = AgcDriver::DescribeMissingRequirements(gpu); - Check(message.find("Fake GPU 9000") != std::string::npos && message.find("VK_KHR_8bit_storage (extension)") != std::string::npos, "the message does not name the GPU and the missing extension"); + Check(message.find("Fake GPU 9000") != std::string::npos && message.find("VK_KHR_buffer_device_address (extension)") != std::string::npos, "the message does not name the GPU and the missing extension"); + const auto* bytes = Find(gpu, VK_KHR_8BIT_STORAGE_EXTENSION_NAME); + Check(bytes != nullptr && !bytes->required && !bytes->present, "the absent 8-bit storage extension is not listed as optional and missing"); } void MissingFeaturesAreFound() { @@ -199,7 +201,7 @@ void MissingFeaturesAreFound() { device.address = false; names.clear(); for (const auto& item : AgcDriver::MissingRequirements(AgcDriver::CollectHostGpuFacts(Query(extensions)))) names.push_back(item.name); - const std::vector second{"bufferDeviceAddress", "fragmentStoresAndAtomics", "storageBuffer8BitAccess", "textureCompressionBC", "vertexPipelineStoresAndAtomics"}; + const std::vector second{"bufferDeviceAddress", "fragmentStoresAndAtomics", "textureCompressionBC", "vertexPipelineStoresAndAtomics"}; Check(names == second, "the second set of unsupported features was not found"); } diff --git a/core/libs/prx/libSceAgcDriver/tests/ShaderDeviceProfile.cpp b/core/libs/prx/libSceAgcDriver/tests/ShaderDeviceProfile.cpp index ca7b65b6fbb5..8481f7fcf5f2 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ShaderDeviceProfile.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ShaderDeviceProfile.cpp @@ -84,6 +84,13 @@ void CheckProfile() { invalid([](auto& value) { value.core.shaderInt64 = VK_FALSE; }, "shaderInt64"); invalid([](auto& value) { value.capabilities.push_back(spv::CapabilityGeometry); }, "geometryShader"); invalid([](auto& value) { value.bytes.storageBuffer8BitAccess = VK_FALSE; }, "storageBuffer8BitAccess"); + invalid([](auto& value) { value.extensions.pop_back(); }, "missing runtime extensions"); + ProfileInput withoutBytes; + withoutBytes.capabilities.pop_back(); + withoutBytes.extensions.pop_back(); + withoutBytes.bda.pNext = &withoutBytes.indexing; + const AgcDriver::ShaderDeviceProfile wordAccess(withoutBytes.Target(), withoutBytes.device, withoutBytes.limits); + Require(std::ranges::find(wordAccess.Target().supportedCapabilities, spv::CapabilityStorageBuffer8BitAccess) == wordAccess.Target().supportedCapabilities.end(), "a device without storageBuffer8BitAccess was profiled with 8-bit storage"); ProfileInput withoutNull; withoutNull.robustness.nullDescriptor = VK_FALSE; Require(!AgcDriver::ShaderDeviceProfile(withoutNull.Target(), withoutNull.device, withoutNull.limits).NullDescriptors(), "a device without nullDescriptor was profiled with it"); diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp index c332862a1e3d..0c21d1df1fc7 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp @@ -36,18 +36,46 @@ std::uint32_t BdaAccessMask(const SpirvEmitterState& state, const IrValue& inst) return BdaAccessMask(BdaCoherent(state, inst)); } +bool BdaBytePointers(const SpirvEmitterState& state) { + return std::ranges::find(state.supportedCapabilities, static_cast(spv::CapabilityStorageBuffer8BitAccess)) != state.supportedCapabilities.end(); +} + +std::uint32_t TypePhysicalBytePointer(SpirvEmitterState& state) { + state.module.EmitCapability(spv::CapabilityStorageBuffer8BitAccess); + state.module.EmitExtension("SPV_KHR_8bit_storage"); + return TypePointer(state, spv::StorageClassPhysicalStorageBuffer, TypeU8(state)); +} + +std::uint32_t BdaWordAddress(SpirvEmitterState& state, std::uint32_t physical) { + return Binary(state, spv::OpBitwiseAnd, TypeScalarU64(state), physical, BdaConstant(state, ~std::uint64_t {3u})); +} + +std::uint32_t BdaByteShift(SpirvEmitterState& state, std::uint32_t physical) { + const auto u32 = TypeU32(state); + const auto lane = Unary(state, spv::OpUConvert, u32, Binary(state, spv::OpBitwiseAnd, TypeScalarU64(state), physical, BdaConstant(state, 3u))); + return Binary(state, spv::OpShiftLeftLogical, u32, lane, ConstantU32(state, 3u)); +} + +std::uint32_t LoadPhysicalByte(SpirvEmitterState& state, std::uint32_t physical, std::uint32_t accessMask) { + const auto u32 = TypeU32(state); + const auto pointer = state.module.AllocateId(); + const auto loaded = state.module.AllocateId(); + if (BdaBytePointers(state)) { + state.module.AddFunction(spv::OpConvertUToPtr, TypePhysicalBytePointer(state), pointer, physical); + state.module.AddFunction(spv::OpLoad, TypeU8(state), loaded, pointer, accessMask, 1u); + return Unary(state, spv::OpUConvert, u32, loaded); + } + state.module.AddFunction(spv::OpConvertUToPtr, TypePhysicalU32Pointer(state), pointer, BdaWordAddress(state, physical)); + state.module.AddFunction(spv::OpLoad, u32, loaded, pointer, accessMask, 4u); + return Binary(state, spv::OpBitwiseAnd, u32, Binary(state, spv::OpShiftRightLogical, u32, loaded, BdaByteShift(state, physical)), ConstantU32(state, 0xffu)); +} + std::uint32_t EmitBdaByte(SpirvEmitterState& state, std::uint32_t guest, std::uint32_t instruction, std::uint32_t accessMask) { - const auto byteType = state.module.Type(spv::OpTypeInt, 8u, 0u); - const auto bytePointer = TypePointer(state, spv::StorageClassPhysicalStorageBuffer, byteType); const auto physical = state.module.AllocateId(); state.module.AddFunction(spv::OpFunctionCall, TypeScalarU64(state), physical, state.bdaPointerFunction, guest, ConstantU32(state, 1u), instruction); if (state.bdaStopsInvocations) { StopBdaInvocationIf(state, Binary(state, spv::OpIEqual, TypeBool(state), physical, BdaConstant(state, 0u))); - const auto pointer = state.module.AllocateId(); - state.module.AddFunction(spv::OpConvertUToPtr, bytePointer, pointer, physical); - const auto loaded = state.module.AllocateId(); - state.module.AddFunction(spv::OpLoad, byteType, loaded, pointer, accessMask, 1u); - return Unary(state, spv::OpUConvert, TypeU32(state), loaded); + return LoadPhysicalByte(state, physical, accessMask); } // Unmapped bytes read as zero; the fault is recorded and every invocation reaches the // program's barriers. @@ -58,11 +86,7 @@ std::uint32_t EmitBdaByte(SpirvEmitterState& state, std::uint32_t guest, std::ui state.module.AddFunction(spv::OpSelectionMerge, merge, spv::SelectionControlMaskNone); state.module.AddFunction(spv::OpBranchConditional, mapped, loadLabel, merge); EmitLabel(state, loadLabel); - const auto pointer = state.module.AllocateId(); - state.module.AddFunction(spv::OpConvertUToPtr, bytePointer, pointer, physical); - const auto loaded = state.module.AllocateId(); - state.module.AddFunction(spv::OpLoad, byteType, loaded, pointer, accessMask, 1u); - const auto widened = Unary(state, spv::OpUConvert, TypeU32(state), loaded); + const auto widened = LoadPhysicalByte(state, physical, accessMask); state.module.AddFunction(spv::OpBranch, merge); EmitLabel(state, merge); const auto value = state.module.AllocateId(); @@ -105,14 +129,37 @@ std::uint32_t EmitBdaBytes(SpirvEmitterState& state, std::uint32_t address, std: return EmitBdaByteLoop(state, address, ConstantU32(state, bytes), instruction, accessMask); } -void EmitBdaStoreAt(SpirvEmitterState& state, std::uint32_t address, std::uint32_t bytes, std::uint32_t pointerType, std::uint32_t value, std::uint32_t instruction, std::uint32_t accessMask) { +template +void EmitBdaWriteAt(SpirvEmitterState& state, std::uint32_t address, std::uint32_t bytes, std::uint32_t instruction, TStore&& store) { const auto physical = state.module.AllocateId(); state.module.AddFunction(spv::OpFunctionCall, TypeScalarU64(state), physical, state.bdaWritePointerFunction, address, ConstantU32(state, bytes), instruction); EmitIfCondition(state, Binary(state, spv::OpINotEqual, TypeBool(state), physical, BdaConstant(state, 0u)), [&] { + store(physical); + state.module.AddFunction(spv::OpFunctionCall, state.module.Type(spv::OpTypeVoid), state.module.AllocateId(), state.bdaNoteWriteFunction, address); + }); +} + +void EmitBdaStoreAt(SpirvEmitterState& state, std::uint32_t address, std::uint32_t bytes, std::uint32_t pointerType, std::uint32_t value, std::uint32_t instruction, std::uint32_t accessMask) { + EmitBdaWriteAt(state, address, bytes, instruction, [&](std::uint32_t physical) { const auto pointer = state.module.AllocateId(); state.module.AddFunction(spv::OpConvertUToPtr, pointerType, pointer, physical); state.module.AddFunction(spv::OpStore, pointer, value, accessMask, bytes); - state.module.AddFunction(spv::OpFunctionCall, state.module.Type(spv::OpTypeVoid), state.module.AllocateId(), state.bdaNoteWriteFunction, address); + }); +} + +void EmitBdaByteStoreAt(SpirvEmitterState& state, std::uint32_t address, std::uint32_t value, std::uint32_t instruction, std::uint32_t accessMask) { + if (BdaBytePointers(state)) { + EmitBdaStoreAt(state, address, 1u, TypePhysicalBytePointer(state), Unary(state, spv::OpUConvert, TypeU8(state), value), instruction, accessMask); + return; + } + EmitBdaWriteAt(state, address, 1u, instruction, [&](std::uint32_t physical) { + const auto u32 = TypeU32(state); + const auto pointer = state.module.AllocateId(); + state.module.AddFunction(spv::OpConvertUToPtr, TypePhysicalU32Pointer(state), pointer, BdaWordAddress(state, physical)); + const auto shift = BdaByteShift(state, physical); + const auto placed = Binary(state, spv::OpShiftLeftLogical, u32, Binary(state, spv::OpBitwiseAnd, u32, value, ConstantU32(state, 0xffu)), shift); + const auto kept = Unary(state, spv::OpNot, u32, Binary(state, spv::OpShiftLeftLogical, u32, ConstantU32(state, 0xffu), shift)); + AtomicUpdate(state, pointer, ResourceKind::Buffer, [&](std::uint32_t current) { return Binary(state, spv::OpBitwiseOr, u32, Binary(state, spv::OpBitwiseAnd, u32, current, kept), placed); }); }); } @@ -405,12 +452,10 @@ void ValidateBdaTarget(const IrProgram& program, const SpirvTargetOptions& targe if (program.Info().usesFaultBuffer && target.bdaAbiVersion != BdaAbi::Version) throw std::runtime_error("the fault buffer needs the BDA fault ABI, which the target lacks"); if (!program.Info().usesDma) return; if (target.bdaAbiVersion != BdaAbi::Version) throw std::runtime_error("unsupported BDA ABI version"); - for (const auto capability : {spv::CapabilityInt64, spv::CapabilityPhysicalStorageBufferAddresses, spv::CapabilityStorageBuffer8BitAccess}) { + for (const auto capability : {spv::CapabilityInt64, spv::CapabilityPhysicalStorageBufferAddresses}) { if (std::find(target.supportedCapabilities.begin(), target.supportedCapabilities.end(), static_cast(capability)) == target.supportedCapabilities.end()) throw std::runtime_error("BDA requires unsupported SPIR-V capability " + std::to_string(capability)); } - for (const auto extension : {"SPV_KHR_physical_storage_buffer", "SPV_KHR_8bit_storage"}) { - if (std::find(target.supportedExtensions.begin(), target.supportedExtensions.end(), extension) == target.supportedExtensions.end()) throw std::runtime_error(std::string("BDA requires unsupported extension ") + extension); - } + if (std::find(target.supportedExtensions.begin(), target.supportedExtensions.end(), "SPV_KHR_physical_storage_buffer") == target.supportedExtensions.end()) throw std::runtime_error("BDA requires unsupported extension SPV_KHR_physical_storage_buffer"); } bool BdaInvocationsMayStop(const IrProgram& program) { @@ -602,16 +647,11 @@ std::array EmitAnchoredBdaRead(SpirvEmitterState& state, const else state.module.AddFunction(spv::OpLoad, u32, directValues[dword], pointer, BdaAccessMask(false), 4u); } } else { - const auto byteType = state.module.Type(spv::OpTypeInt, 8u, 0u); - const auto bytePointer = TypePointer(state, spv::StorageClassPhysicalStorageBuffer, byteType); auto value = ConstantU32(state, 0u); for (std::uint32_t byte = 0; byte < bytes; ++byte) { const auto element = byte == 0u ? translated : Binary(state, spv::OpIAdd, u64, translated, BdaConstant(state, byte)); - const auto pointer = state.module.AllocateId(); - state.module.AddFunction(spv::OpConvertUToPtr, bytePointer, pointer, element); - const auto loaded = state.module.AllocateId(); - state.module.AddFunction(spv::OpLoad, byteType, loaded, pointer, BdaAccessMask(coherent || bits == 16u), 1u); - value = Binary(state, spv::OpBitwiseOr, u32, value, Binary(state, spv::OpShiftLeftLogical, u32, Unary(state, spv::OpUConvert, u32, loaded), ConstantU32(state, byte * 8u))); + const auto loaded = LoadPhysicalByte(state, element, BdaAccessMask(coherent || bits == 16u)); + value = Binary(state, spv::OpBitwiseOr, u32, value, Binary(state, spv::OpShiftLeftLogical, u32, loaded, ConstantU32(state, byte * 8u))); } directValues[0] = value; } @@ -681,8 +721,6 @@ void DefineBdaByteWriteFunctions(SpirvEmitterState& state) { const auto u32 = TypeU32(state); const auto u64 = TypeScalarU64(state); const auto voidType = state.module.Type(spv::OpTypeVoid); - const auto byteType = state.module.Type(spv::OpTypeInt, 8u, 0u); - const auto bytePointer = TypePointer(state, spv::StorageClassPhysicalStorageBuffer, byteType); const auto type = state.module.Type(spv::OpTypeFunction, voidType, u64, u32, u32, u32); const auto& memory = state.program.Resources().memoryInfo; const bool coherentAccesses = std::any_of(memory.begin(), memory.end(), [](const MemoryInfo& info) { return info.coherent; }); @@ -717,7 +755,7 @@ void DefineBdaByteWriteFunctions(SpirvEmitterState& state) { EmitLabel(state, body); const auto guest = Binary(state, spv::OpIAdd, u64, address, Unary(state, spv::OpUConvert, u64, byte)); const auto shifted = Binary(state, spv::OpShiftRightLogical, u32, value, Binary(state, spv::OpShiftLeftLogical, u32, byte, ConstantU32(state, 3u))); - EmitBdaStoreAt(state, guest, 1u, bytePointer, Unary(state, spv::OpUConvert, byteType, shifted), instruction, BdaAccessMask(coherent)); + EmitBdaByteStoreAt(state, guest, shifted, instruction, BdaAccessMask(coherent)); state.module.AddFunction(spv::OpBranch, continuation); EmitLabel(state, continuation); state.module.AddFunction(spv::OpStore, counter, Binary(state, spv::OpIAdd, u32, byte, ConstantU32(state, 1u))); diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp index 63e75e3a2951..6e8710d387c4 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp @@ -174,10 +174,6 @@ void EmitBaseHeader(SpirvModule& module, const IrProgram& program, bool atomicLo module.EmitCapability(spv::CapabilityPhysicalStorageBufferAddresses); module.EmitExtension("SPV_KHR_physical_storage_buffer"); } - if (program.Info().usesDma) { - module.EmitCapability(spv::CapabilityStorageBuffer8BitAccess); - module.EmitExtension("SPV_KHR_8bit_storage"); - } module.AddMemoryModel(physical ? spv::AddressingModelPhysicalStorageBuffer64 : spv::AddressingModelLogical, spv::MemoryModelGLSL450); } diff --git a/docs/dev/debt/shader.md b/docs/dev/debt/shader.md index a8d9ffb4b89a..05eba683aeac 100644 --- a/docs/dev/debt/shader.md +++ b/docs/dev/debt/shader.md @@ -44,6 +44,7 @@ ### Functional +- [BDA byte access without storageBuffer8BitAccess](../../../core/shader/recompiler/SpirvBackend/src/SpirvBdaRead.cpp) - on a device without `storageBuffer8BitAccess` (Adreno 6xx under Turnip), a BDA byte load reads the aligned 32-bit word that holds the byte, and a byte store is a 32-bit compare-and-swap on that word. Both touch the three neighbouring bytes of the word, so they rely on every host range the BDA table maps being 4-byte aligned and a multiple of 4 bytes long, as guest buffer chunks are; a range that ends inside a word would be read and rewritten past its end. A neighbouring byte written by a plain store between the swap's load and exchange makes the swap retry, so no byte is lost, but every byte store on these devices is an atomic loop. Speed on such a device has not been measured. - [`buffer_atomic_csub` without glc](../../../core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp) (shader recompiler) - `buffer_atomic_csub` without `glc` is translated as a non-returning atomic subtract; console hardware behaviour is unmeasured on a PS5 (GFX10.3), where LLVM defines csub atomics only in their returning form (`FeatureAtomicCSubNoRtnInsts` disabled in llvm `AMDGPU.td`) and the assembler requires `glc`. RDNA2 (gfx1031) behaves that way: with `glc` clear it applies the clamped subtract and leaves the data register unwritten, in wave32 and wave64 with all or half of the lanes active (64 rows, coarse-grained memory). No compiler target is known to emit it. Closes when: measured on a PS5 or an unhandled title shader is captured. - [Captured shader target robustness](../../../core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp) - request format 53 does not preserve `SpirvTarget::robustBufferAccess2` or `exactRobustLoads`. Deserializing a native capture therefore restores their false defaults even when the device enabled them. `agc_shader_replay --robust2` overrides the former only; prepared/materialized SPIR-V output reflects the replay target and is not proof of the native pipeline's buffer checks. Preserve the flags in a versioned request before using offline bounds/branch counts as native performance evidence. - [Vector select IR lowering](../../../core/shader/recompiler/IntermediateRepresentation/src/IrBuilder/IrBuilderLogic.cpp) (shader recompiler) - multi-word and vector selects (`U64`, `U32x2`, `U32x3`, `U32x4`) decompose into scalar 32-bit selects and composite constructors because the IR has no vector select opcodes; unaligned multi-word operands or vector selections with per-component condition vectors remain unsupported and throw. diff --git a/docs/user/USAGE.md b/docs/user/USAGE.md index 2449f2f875b9..c8417e62e74f 100644 --- a/docs/user/USAGE.md +++ b/docs/user/USAGE.md @@ -558,7 +558,7 @@ The report has fixed sections in a fixed order, so two runs on one machine print The report holds no user names, host names, serial numbers or MAC addresses. Any word that looks like a path (starting with `/` or `~`, containing a backslash or starting with a drive letter) prints as ``, URLs print as ``, MAC addresses as ``, and the account name, host name and home directory name are replaced wherever they appear as a whole word. Check the text before posting it anyway, since the values of the variables it lists are printed as set. -When the chosen GPU lacks an extension or a feature the game requires (`VK_KHR_8bit_storage`, `VK_KHR_buffer_device_address`, `VK_KHR_shader_float_controls`, `storageBuffer8BitAccess`, `bufferDeviceAddress`, `shaderInt64`, `shaderSignedZeroInfNanPreserveFloat32`, `vertexPipelineStoresAndAtomics`, `fragmentStoresAndAtomics`, `samplerAnisotropy`, `textureCompressionBC`, and `VK_KHR_swapchain` for a window), the start fails with one message that names the GPU, its driver and Vulkan version, lists every missing item, and points to the other Vulkan devices that `ANYPS5_GPU` can select. With `ANYPS5_HOST_REPORT=1` the report is printed first, so a failed start still has it. +When the chosen GPU lacks an extension or a feature the game requires (`VK_KHR_buffer_device_address`, `VK_KHR_shader_float_controls`, `bufferDeviceAddress`, `shaderInt64`, `shaderSignedZeroInfNanPreserveFloat32`, `vertexPipelineStoresAndAtomics`, `fragmentStoresAndAtomics`, `samplerAnisotropy`, `textureCompressionBC`, and `VK_KHR_swapchain` for a window), the start fails with one message that names the GPU, its driver and Vulkan version, lists every missing item, and points to the other Vulkan devices that `ANYPS5_GPU` can select. With `ANYPS5_HOST_REPORT=1` the report is printed first, so a failed start still has it. ### HDR output