Skip to content
Closed
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
2 changes: 1 addition & 1 deletion 3rdparty/SPIRV-Tools
Submodule SPIRV-Tools updated 117 files
20 changes: 20 additions & 0 deletions core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ struct VulkanDevice::State {
bool imageInt64Atomics = false;
bool bufferInt64Atomics = false;
bool viewportIndexLayer = false;
bool fmaFloat16 = false;
bool primitiveListRestart = false;
bool depthClipControl = false;
bool graphicsPipelineLibrary = false;
Expand Down Expand Up @@ -1178,6 +1179,20 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window, bool compileOnly) :
state->float16DenormPreserve = floatControls.shaderDenormPreserveFloat16 == VK_TRUE &&
floatControls.denormBehaviorIndependence == VK_SHADER_FLOAT_CONTROLS_INDEPENDENCE_ALL;
if (state->float16DenormPreserve && std::find(state->capabilities.begin(), state->capabilities.end(), static_cast<std::uint32_t>(spv::CapabilityDenormPreserve)) == state->capabilities.end()) state->capabilities.push_back(spv::CapabilityDenormPreserve);
VkPhysicalDeviceShaderFmaFeaturesKHR fmaFeatures{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FMA_FEATURES_KHR};
if (float16Rte && hasExtension(VK_KHR_SHADER_FMA_EXTENSION_NAME)) {
VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &fmaFeatures};
state->InstanceFunction<PFN_vkGetPhysicalDeviceFeatures2>("vkGetPhysicalDeviceFeatures2")(selected, &features);
}
const bool float16Fma = float16Rte && fmaFeatures.shaderFmaFloat16 == VK_TRUE;
fmaFeatures = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FMA_FEATURES_KHR};
fmaFeatures.shaderFmaFloat16 = VK_TRUE;
state->fmaFloat16 = float16Fma;
if (float16Fma) {
deviceExtensions.push_back(VK_KHR_SHADER_FMA_EXTENSION_NAME);
state->capabilities.push_back(spv::CapabilityFMAKHR);
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);
Expand Down Expand Up @@ -1517,6 +1532,10 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window, bool compileOnly) :
float16Features.pNext = byteFeatures.pNext;
byteFeatures.pNext = &float16Features;
}
if (float16Fma) {
fmaFeatures.pNext = byteFeatures.pNext;
byteFeatures.pNext = &fmaFeatures;
}
if (bufferInt64Atomics) {
atomicInt64Features.pNext = byteFeatures.pNext;
byteFeatures.pNext = &atomicInt64Features;
Expand Down Expand Up @@ -3153,6 +3172,7 @@ Graphics::Context VulkanDevice::buildContext() const {
context.bufferInt64Atomics = state->bufferInt64Atomics;
context.placeholderImages = state->placeholderImages.get();
context.viewportIndexLayer = state->viewportIndexLayer;
context.fmaFloat16 = state->fmaFloat16;
context.primitiveListRestart = state->primitiveListRestart;
context.imageViewMinLod = state->imageViewMinLod;
context.pipelineExecutableInfo = state->pipelineExecutableInfo;
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ struct Context {
bool bufferInt64Atomics = false;
const PlaceholderImages* placeholderImages = nullptr;
bool viewportIndexLayer = false;
bool fmaFloat16 = false;
bool primitiveListRestart = false;
bool imageViewMinLod = false;
bool pipelineExecutableInfo = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ bool ValidateSampleLocations(const Context& context, const State& state);
void ValidateShaderPair(const ShaderRecompiler::RecompileResult& vertex, const ShaderRecompiler::RecompileResult& fragment);
bool ShadersWriteMemory(std::span<const CompiledShader> shaders);
// Returns the color attachment locations the pixel shader writes.
std::set<std::uint32_t> ValidateShaders(std::span<const CompiledShader> shaders, const State& state, const VkPhysicalDeviceSubgroupProperties& subgroup, bool fragmentShaderBarycentric, bool descriptorIndexing = false, bool imageInt64Atomics = false, bool geometryShader = false, bool sampleRateShading = false, bool bufferInt64Atomics = false, bool shaderClipDistance = false, bool shaderCullDistance = false, bool viewportIndexLayer = false);
std::set<std::uint32_t> ValidateShaders(std::span<const CompiledShader> shaders, const State& state, const VkPhysicalDeviceSubgroupProperties& subgroup, bool fragmentShaderBarycentric, bool descriptorIndexing = false, bool imageInt64Atomics = false, bool geometryShader = false, bool sampleRateShading = false, bool bufferInt64Atomics = false, bool shaderClipDistance = false, bool shaderCullDistance = false, bool viewportIndexLayer = false, bool fmaFloat16 = false);

}

Expand Down
2 changes: 1 addition & 1 deletion core/libs/prx/libSceAgcDriver/Graphics/src/Draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ std::set<std::uint32_t> CachedFragmentOutputs(const Context& context, std::span<
}
std::set<std::uint32_t> outputs;
try {
outputs = ValidateShaders(shaders, state, context.subgroup, context.fragmentShaderBarycentric, context.descriptorIndexing, context.imageInt64Atomics, context.geometryShader, context.sampleRateShading, context.bufferInt64Atomics, context.shaderClipDistance, context.shaderCullDistance, context.viewportIndexLayer);
outputs = ValidateShaders(shaders, state, context.subgroup, context.fragmentShaderBarycentric, context.descriptorIndexing, context.imageInt64Atomics, context.geometryShader, context.sampleRateShading, context.bufferInt64Atomics, context.shaderClipDistance, context.shaderCullDistance, context.viewportIndexLayer, context.fmaFloat16);
} catch (const std::exception& error) {
if (keyed) {
std::lock_guard lock(validationMutex());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ struct Module {
}
};

Module Inspect(const CompiledShader& compiled, const State& state, const VkPhysicalDeviceSubgroupProperties& subgroup, bool fragmentShaderBarycentric, bool descriptorIndexing, bool imageInt64Atomics, bool geometryShader, bool sampleRateShading, bool bufferInt64Atomics, bool shaderClipDistance, bool shaderCullDistance, bool viewportIndexLayer) {
Module Inspect(const CompiledShader& compiled, const State& state, const VkPhysicalDeviceSubgroupProperties& subgroup, bool fragmentShaderBarycentric, bool descriptorIndexing, bool imageInt64Atomics, bool geometryShader, bool sampleRateShading, bool bufferInt64Atomics, bool shaderClipDistance, bool shaderCullDistance, bool viewportIndexLayer, bool fmaFloat16) {
using Stage = ShaderRecompiler::ShaderStage;
Require(compiled.program != nullptr, "missing compiled shader");
const auto& shader = *compiled.program;
Expand Down Expand Up @@ -275,6 +275,7 @@ Module Inspect(const CompiledShader& compiled, const State& state, const VkPhysi
capability == spv::CapabilityFloat16 ||
capability == spv::CapabilityRoundingModeRTE ||
capability == spv::CapabilityDenormPreserve ||
(fmaFloat16 && capability == spv::CapabilityFMAKHR) ||
capability == spv::CapabilityFloat64 ||
capability == spv::CapabilityStorageBuffer8BitAccess ||
capability == spv::CapabilityPhysicalStorageBufferAddresses ||
Expand Down Expand Up @@ -328,7 +329,7 @@ Module Inspect(const CompiledShader& compiled, const State& state, const VkPhysi
Require(fragment && fragmentShaderBarycentric, "SPV_KHR_fragment_shader_barycentric requires enabled fragmentShaderBarycentric in a fragment shader");
break;
}
Require(extension == "SPV_KHR_float_controls" || (imageInt64Atomics && extension == "SPV_EXT_shader_image_int64") || (viewportIndexLayer && extension == "SPV_EXT_shader_viewport_index_layer") || (mesh && (extension == "SPV_EXT_mesh_shader" || extension == "SPV_KHR_physical_storage_buffer")) || (descriptorIndexing && extension == "SPV_EXT_descriptor_indexing") || (shader.bdaAbiVersion == ShaderRecompiler::BdaAbi::Version && (extension == "SPV_KHR_physical_storage_buffer" || extension == "SPV_KHR_8bit_storage")), "unsupported SPIR-V extension");
Require(extension == "SPV_KHR_float_controls" || (fmaFloat16 && extension == "SPV_KHR_fma") || (imageInt64Atomics && extension == "SPV_EXT_shader_image_int64") || (viewportIndexLayer && extension == "SPV_EXT_shader_viewport_index_layer") || (mesh && (extension == "SPV_EXT_mesh_shader" || extension == "SPV_KHR_physical_storage_buffer")) || (descriptorIndexing && extension == "SPV_EXT_descriptor_indexing") || (shader.bdaAbiVersion == ShaderRecompiler::BdaAbi::Version && (extension == "SPV_KHR_physical_storage_buffer" || extension == "SPV_KHR_8bit_storage")), "unsupported SPIR-V extension");
break;
}
case spv::OpDecorateId:
Expand Down Expand Up @@ -594,7 +595,7 @@ Module Inspect(const CompiledShader& compiled, const State& state, const VkPhysi

}

std::set<std::uint32_t> ValidateShaders(std::span<const CompiledShader> shaders, const State& state, const VkPhysicalDeviceSubgroupProperties& subgroup, bool fragmentShaderBarycentric, bool descriptorIndexing, bool imageInt64Atomics, bool geometryShader, bool sampleRateShading, bool bufferInt64Atomics, bool shaderClipDistance, bool shaderCullDistance, bool viewportIndexLayer) {
std::set<std::uint32_t> ValidateShaders(std::span<const CompiledShader> shaders, const State& state, const VkPhysicalDeviceSubgroupProperties& subgroup, bool fragmentShaderBarycentric, bool descriptorIndexing, bool imageInt64Atomics, bool geometryShader, bool sampleRateShading, bool bufferInt64Atomics, bool shaderClipDistance, bool shaderCullDistance, bool viewportIndexLayer, bool fmaFloat16) {
using Stage = ShaderRecompiler::ShaderStage;
const bool tessellation = state.stages.path == ShaderPath::Tessellation;
const bool mesh = state.stages.path == ShaderPath::Geometry;
Expand All @@ -615,7 +616,7 @@ std::set<std::uint32_t> ValidateShaders(std::span<const CompiledShader> shaders,
Require(shaders[i].program != nullptr, "missing compiled shader");
Require(shaders[i].stage == expected, "graphics stage order disagrees");
for (const auto& binding : shaders[i].program->bindings) Require(binding.descriptorSet == ShaderRecompiler::RuntimeAbi::DescriptorSet || (shaders[i].stage == ShaderRecompiler::ShaderStage::Fragment && binding.descriptorSet == ShaderRecompiler::RuntimeAbi::FragmentDescriptorSet), "graphics resource uses a descriptor set other than its stage's");
const auto current = Inspect(shaders[i], state, subgroup, fragmentShaderBarycentric, descriptorIndexing, imageInt64Atomics, geometryShader, sampleRateShading, bufferInt64Atomics, shaderClipDistance, shaderCullDistance, viewportIndexLayer);
const auto current = Inspect(shaders[i], state, subgroup, fragmentShaderBarycentric, descriptorIndexing, imageInt64Atomics, geometryShader, sampleRateShading, bufferInt64Atomics, shaderClipDistance, shaderCullDistance, viewportIndexLayer, fmaFloat16);
if (i != 0) {
for (const auto& [location, signature] : current.inputs) {
const auto output = previous.outputs.find(location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ struct F16x2Math {
bool add = false;
bool mul = false;
bool fma = false;
bool fusedFma = false;
};

F16x2Math FindF16x2Math(std::span<const std::uint32_t> spirv) {
Expand All @@ -124,12 +125,13 @@ F16x2Math FindF16x2Math(std::span<const std::uint32_t> spirv) {
Require(words >= 4u, "packed f16 native: malformed vector type");
if (half && spirv[offset + 2u] == *half && spirv[offset + 3u] == 2u) pair = spirv[offset + 1u];
}
if (opcode == spv::OpFAdd || opcode == spv::OpFMul || opcode == spv::OpExtInst) {
if (opcode == spv::OpFAdd || opcode == spv::OpFMul || opcode == spv::OpExtInst || opcode == spv::OpFmaKHR) {
Require(words >= 5u, "packed f16 native: malformed arithmetic instruction");
if (pair && spirv[offset + 1u] == *pair) {
math.add = math.add || opcode == spv::OpFAdd;
math.mul = math.mul || opcode == spv::OpFMul;
math.fma = math.fma || opcode == spv::OpExtInst;
math.fusedFma = math.fusedFma || opcode == spv::OpFmaKHR;
}
}
offset += words;
Expand All @@ -151,10 +153,15 @@ int main() {
std::puts("packed f16 native: skipped, the device has no native f16 with RTE rounding and kept denormals");
return VulkanTestSkipped;
}
const bool fused = supports(spv::CapabilityFMAKHR);
std::vector<std::uint32_t> withoutFloat16;
std::ranges::copy_if(target.supportedCapabilities, std::back_inserter(withoutFloat16), [](std::uint32_t capability) { return capability != spv::CapabilityFloat16; });
auto emulatedTarget = target;
emulatedTarget.supportedCapabilities = withoutFloat16;
std::vector<std::uint32_t> withoutFma;
std::ranges::copy_if(target.supportedCapabilities, std::back_inserter(withoutFma), [](std::uint32_t capability) { return capability != spv::CapabilityFMAKHR; });
auto unfusedTarget = target;
unfusedTarget.supportedCapabilities = withoutFma;

const std::span<const std::uint32_t> code(Code);
std::vector<std::uint32_t> userData(8, 0u);
Expand Down Expand Up @@ -185,28 +192,36 @@ int main() {
std::uint64_t compared = 0u;
for (const auto& mode : Modes) {
const auto native = compile(target, mode);
const auto unfused = compile(unfusedTarget, mode);
const auto emulated = compile(emulatedTarget, mode);
const F16x2Math nativeMath = FindF16x2Math(native.spirv.Words());
const F16x2Math unfusedMath = FindF16x2Math(unfused.spirv.Words());
const F16x2Math emulatedMath = FindF16x2Math(emulated.spirv.Words());
Require(nativeMath.add && nativeMath.mul, std::string("packed f16 native: ") + mode.name + " does not use f16vec2 add and mul");
Require(!nativeMath.fma, std::string("packed f16 native: ") + mode.name + " uses an f16vec2 Fma, which Vulkan does not require to be fused");
Require(!emulatedMath.add && !emulatedMath.mul && !emulatedMath.fma, std::string("packed f16 native: ") + mode.name + " uses f16vec2 math without Float16");
Require(nativeMath.add && nativeMath.mul && unfusedMath.add && unfusedMath.mul, std::string("packed f16 native: ") + mode.name + " does not use f16vec2 add and mul");
Require(!nativeMath.fma && !unfusedMath.fma, std::string("packed f16 native: ") + mode.name + " uses an f16vec2 Fma, which Vulkan does not require to be fused");
Require(nativeMath.fusedFma == fused, std::string("packed f16 native: ") + mode.name + (fused ? " does not use OpFmaKHR on f16vec2 with FMAKHR" : " uses OpFmaKHR without FMAKHR"));
Require(!unfusedMath.fusedFma, std::string("packed f16 native: ") + mode.name + " uses OpFmaKHR without FMAKHR");
Require(!emulatedMath.add && !emulatedMath.mul && !emulatedMath.fma && !emulatedMath.fusedFma, std::string("packed f16 native: ") + mode.name + " uses f16vec2 math without Float16");
for (std::uint32_t batch = 0; batch < Batches; ++batch) {
for (auto& word : Input) word = Half() | (Half() << 16u);
const auto nativeOutput = run(native);
const auto emulatedOutput = run(emulated);
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
for (std::uint32_t index = 0; index < Results; ++index) {
const std::uint32_t actual = nativeOutput[tid * Results + index];
const std::uint32_t expected = emulatedOutput[tid * Results + index];
const std::uint32_t* row = &Input[tid * Inputs];
Require(actual == expected, std::string("packed f16 native: ") + mode.name + ", " + Names[index] + " with v4=" + Hex(row[0]) + " v5=" + Hex(row[1]) + " v6=" + Hex(row[2]) + " v7=" + Hex(row[3]) + " is " + Hex(actual) + ", f32 path gives " + Hex(expected));
const auto check = [&](const auto& result, const char* path) {
const auto output = run(result);
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
for (std::uint32_t index = 0; index < Results; ++index) {
const std::uint32_t actual = output[tid * Results + index];
const std::uint32_t expected = emulatedOutput[tid * Results + index];
const std::uint32_t* row = &Input[tid * Inputs];
Require(actual == expected, std::string("packed f16 native: ") + mode.name + ", " + path + ", " + Names[index] + " with v4=" + Hex(row[0]) + " v5=" + Hex(row[1]) + " v6=" + Hex(row[2]) + " v7=" + Hex(row[3]) + " is " + Hex(actual) + ", f32 path gives " + Hex(expected));
}
}
}
compared += Threads * Results * 2u;
compared += Threads * Results * 2u;
};
check(native, fused ? "OpFmaKHR" : "native");
if (fused) check(unfused, "native without FMAKHR");
}
}
std::printf("packed f16 native tests passed (%llu halves)\n", static_cast<unsigned long long>(compared));
std::printf("packed f16 native tests passed (%llu halves, %s)\n", static_cast<unsigned long long>(compared), fused ? "fused f16 fma" : "no FMAKHR");
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ enum class IrOpcode : std::uint16_t {
PackFloat2x16Rtz,
FPAdd16x2,
FPMul16x2,
FPFma16x2,
FPAbs32,
FPNeg32,
FPSaturate32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ constexpr std::array<OpcodeMeta, static_cast<std::size_t>(IrOpcode::Count)> Meta
makeMeta("PackFloat2x16Rtz", U32, F32, F32),
makeMeta("FPAdd16x2", U32, U32, U32),
makeMeta("FPMul16x2", U32, U32, U32),
makeMeta("FPFma16x2", U32, U32, U32, U32),
makeMeta("FPAbs32", F32, F32),
makeMeta("FPNeg32", F32, F32),
makeMeta("FPSaturate32", F32, F32),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ bool isLaneLocal(IrOpcode opcode) {
case IrOpcode::PackFloat2x16Rtz:
case IrOpcode::FPAdd16x2:
case IrOpcode::FPMul16x2:
case IrOpcode::FPFma16x2:
case IrOpcode::BitFieldInsert:
case IrOpcode::BitFieldUExtract:
case IrOpcode::BitFieldSExtract:
Expand Down
1 change: 1 addition & 0 deletions core/shader/recompiler/Recompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ IrProgram PrepareResourceProgram(const RecompileRequest& request, ShaderPreparat
translateOptions.fragmentShaderBarycentricEnabled = request.target.fragmentShaderBarycentricEnabled;
const auto supports = [&](spv::Capability capability) { return std::ranges::find(request.target.supportedCapabilities, static_cast<std::uint32_t>(capability)) != request.target.supportedCapabilities.end(); };
translateOptions.nativeFloat16 = supports(spv::CapabilityFloat16) && supports(spv::CapabilityRoundingModeRTE) && supports(spv::CapabilityDenormPreserve);
translateOptions.nativeFloat16Fma = translateOptions.nativeFloat16 && supports(spv::CapabilityFMAKHR);
translateOptions.floatMode = request.context.floatMode;
translateOptions.inputInfo = inputInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ std::uint32_t EmitCompositeExtractU64(SpirvEmitterState& state, std::uint32_t ar
std::uint32_t EmitPackFloat2x16Rtz(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1);
std::uint32_t EmitFPAdd16x2(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1);
std::uint32_t EmitFPMul16x2(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1);
std::uint32_t EmitFPFma16x2(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1, std::uint32_t arg2);
std::uint32_t EmitFPSaturate32(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitIAdd64(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1);
std::uint32_t EmitISub64(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1);
Expand Down
Loading
Loading