diff --git a/core/kyty/graphics/host_gpu/graphicContext.h b/core/kyty/graphics/host_gpu/graphicContext.h index a59c2ac1ff..07b7a566ef 100644 --- a/core/kyty/graphics/host_gpu/graphicContext.h +++ b/core/kyty/graphics/host_gpu/graphicContext.h @@ -36,6 +36,7 @@ struct GraphicContext { bool attachment_feedback_loop_enabled = false; bool provoking_vertex_last_enabled = false; bool maintenance8_enabled = false; + bool shader_resource_min_lod_enabled = false; bool robust_buffer_access_enabled = false; bool ray_query_enabled = false; bool compute_derivative_quads_enabled = false; diff --git a/core/kyty/graphics/host_gpu/renderer/pipeline/pipelineShaderModule.cpp b/core/kyty/graphics/host_gpu/renderer/pipeline/pipelineShaderModule.cpp index 14988a915f..e427eeae75 100644 --- a/core/kyty/graphics/host_gpu/renderer/pipeline/pipelineShaderModule.cpp +++ b/core/kyty/graphics/host_gpu/renderer/pipeline/pipelineShaderModule.cpp @@ -152,7 +152,8 @@ ShaderRecompiler::Spirv::Profile SpirvProfile(const GraphicContext& graphics) { .subgroup_ballot = bool(operations & vk::SubgroupFeatureFlagBits::eBallot), .subgroup_shuffle = bool(operations & vk::SubgroupFeatureFlagBits::eShuffle), .subgroup_quad = bool(operations & vk::SubgroupFeatureFlagBits::eQuad), - .subgroup_stages = SubgroupStages(graphics.subgroup_supported_stages)}; + .subgroup_stages = SubgroupStages(graphics.subgroup_supported_stages), + .shader_resource_min_lod = graphics.shader_resource_min_lod_enabled}; } bool PipelineCache::ResolveTessellationLayout(const HW::ShaderRegisters& sh, const ShaderParams& ls, diff --git a/core/kyty/graphics/presentation/window/vulkanWindow.cpp b/core/kyty/graphics/presentation/window/vulkanWindow.cpp index 45d6f1522f..4fa4212c7a 100644 --- a/core/kyty/graphics/presentation/window/vulkanWindow.cpp +++ b/core/kyty/graphics/presentation/window/vulkanWindow.cpp @@ -307,7 +307,9 @@ static void VulkanFindPhysicalDevice(vk::Instance instance, vk::SurfaceKHR surfa check_feature(device_features2.features.shaderImageGatherExtended, "shaderImageGatherExtended"); - check_feature(device_features2.features.shaderResourceMinLod, "shaderResourceMinLod"); + if (device_features2.features.shaderResourceMinLod != VK_TRUE) { + LOGF("shaderResourceMinLod is not supported; LOD clamps are emulated in shaders\n"); + } check_feature(device_features2.features.independentBlend, "independentBlend"); check_feature(device_features2.features.tessellationShader, "tessellationShader"); @@ -625,6 +627,7 @@ static vk::Device VulkanCreateDevice(GraphicContext& graphics, graphics.attachment_feedback_loop_enabled = feedback_layout.attachmentFeedbackLoopLayout && feedback_dynamic.attachmentFeedbackLoopDynamicState; graphics.maintenance8_enabled = maintenance8.maintenance8; + graphics.shader_resource_min_lod_enabled = supported2.features.shaderResourceMinLod == VK_TRUE; graphics.ray_query_enabled = ray_query_enabled; graphics.robust_buffer_access_enabled = robustness2.robustBufferAccess2 && @@ -643,6 +646,8 @@ static vk::Device VulkanCreateDevice(GraphicContext& graphics, graphics.conditional_rendering_enabled = conditional_rendering.conditionalRendering; LOGF("Vulkan maintenance8 support: %s\n", graphics.maintenance8_enabled ? "true" : "false"); + LOGF("Vulkan shaderResourceMinLod support: %s\n", + graphics.shader_resource_min_lod_enabled ? "true" : "false"); LOGF("Vulkan depth feedback support: %s\n", graphics.attachment_feedback_loop_enabled ? "true" : "false"); if (graphics.mesh_shader_enabled) { @@ -673,7 +678,7 @@ static vk::Device VulkanCreateDevice(GraphicContext& graphics, "tables will not work\n"); } device_features.shaderImageGatherExtended = VK_TRUE; - device_features.shaderResourceMinLod = VK_TRUE; + device_features.shaderResourceMinLod = supported2.features.shaderResourceMinLod; device_features.independentBlend = VK_TRUE; device_features.dualSrcBlend = VK_TRUE; device_features.tessellationShader = VK_TRUE; diff --git a/core/kyty/graphics/shader/recompiler/backend/spirv/SpirvEmitter.h b/core/kyty/graphics/shader/recompiler/backend/spirv/SpirvEmitter.h index 429f0da06c..b338197ba1 100644 --- a/core/kyty/graphics/shader/recompiler/backend/spirv/SpirvEmitter.h +++ b/core/kyty/graphics/shader/recompiler/backend/spirv/SpirvEmitter.h @@ -28,6 +28,7 @@ struct Profile { bool subgroup_shuffle = true; bool subgroup_quad = true; uint32_t subgroup_stages = ~0u; + bool shader_resource_min_lod = true; }; inline constexpr Profile DefaultProfile {}; diff --git a/core/kyty/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cpp b/core/kyty/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cpp index 411040cf7a..06a6c0f49a 100644 --- a/core/kyty/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cpp +++ b/core/kyty/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cpp @@ -1258,16 +1258,46 @@ void EmitImage(ValueEmitContext& ctx, const IR::Inst& inst) { const auto sampler = samplers[candidate.numeric_class != Prospero::TextureNumericClass::Float]; auto sample_operands = operands; auto sample_operand_count = operand_count; + auto sample_opcode = opcode; + auto sample_min_lod = min_lod; + if (sample_min_lod != 0 && !state.profile.shader_resource_min_lod) { + if (!explicit_lod) { + state.builder.RequireCapability(spv::CapabilityImageQuery); + const auto query = state.builder.AllocateId(); + state.builder.AddFunction( + spv::OpImageQueryLod, TypeF32Vector(state, 2), query, + MakeSampledImage(state, resource, sampler.id, 0u, array_index, + sampler.dynamic), + CoordF32(ctx, mem, *address, layout.coord, + ImageDimensionInfoFor(candidate.dimension).spatial_components, + candidate.cube, RowCenter(state, candidate.dimension))); + auto lod = state.builder.AllocateId(); + state.builder.AddFunction(spv::OpCompositeExtract, TypeF32(state), lod, query, + 1u); + if (layout.bias != NoImageComponent) { + lod = Binary(state, spv::OpFAdd, TypeF32(state), lod, + AddressF32(ctx, mem, *address, layout.bias)); + } + lod = EmitGlsl(state, lod, sample_min_lod); + sample_operands = {}; + sample_operands[0] = spv::ImageOperandsLodMask; + sample_operands[1] = lod; + sample_operand_count = 2; + sample_opcode = dref ? spv::OpImageSampleDrefExplicitLod + : spv::OpImageSampleExplicitLod; + } + sample_min_lod = 0; + } if (layout.offset != NoImageComponent) { sample_operand_count = std::max(size_t{1}, sample_operand_count); sample_operands[sample_operand_count++] = PackedOffset( ctx, offset_value, candidate.dimension, sample_operands[0]); } - if (min_lod != 0) { + if (sample_min_lod != 0) { state.builder.RequireCapability(spv::CapabilityMinLod); sample_operand_count = std::max(size_t{1}, sample_operand_count); sample_operands[0] |= spv::ImageOperandsMinLodMask; - sample_operands[sample_operand_count++] = min_lod; + sample_operands[sample_operand_count++] = sample_min_lod; } const auto coord = CoordF32(ctx, mem, *address, layout.coord, @@ -1276,7 +1306,7 @@ void EmitImage(ValueEmitContext& ctx, const IR::Inst& inst) { const auto sampled = MakeSampledImage(state, resource, sampler.id, 0u, array_index, sampler.dynamic); const auto sample = state.builder.AllocateId(); - state.builder.AddFunction(opcode, result_type, sample, sampled, coord, + state.builder.AddFunction(sample_opcode, result_type, sample, sampled, coord, std::span(&dref_value, dref ? 1u : 0u), std::span(sample_operands).first(sample_operand_count)); if (state.mip_stats_variable != 0 && state.mip_stats_records && array_index == 0u && diff --git a/core/kyty/graphics/shader/recompiler/ir/Serialize.h b/core/kyty/graphics/shader/recompiler/ir/Serialize.h index d9324f3369..85cecb5142 100644 --- a/core/kyty/graphics/shader/recompiler/ir/Serialize.h +++ b/core/kyty/graphics/shader/recompiler/ir/Serialize.h @@ -220,7 +220,7 @@ KYTY_IO_FIELDS(BdaReadWord, a, b) KYTY_IO_FIELDS(BdaReadSpan, a, b, c, d, e) KYTY_IO_FIELDS(BdaReadPlan, a, b) KYTY_IO_FIELDS(CompiledShaderInfo, a, b, c, d, e, f, g, h, i, j, k) -KYTY_IO_FIELDS(Spirv::Profile, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r) +KYTY_IO_FIELDS(Spirv::Profile, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s) #if defined(ANYPS5_KYTY) KYTY_IO_FIELDS(ShaderTessellationInputInfo, a, b, c, d, e, f, g, h, i, j) #else diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt index 4d5bdc20d1..3cc2863ce7 100644 --- a/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt @@ -464,6 +464,9 @@ if(NOT ANYPS5_KYTY_DLL) add_test(NAME kyty_shim_recompiler_flat_offsets COMMAND kyty_shim_recompiler_robustness_tests flat-offsets) set_tests_properties(kyty_shim_recompiler_flat_offsets PROPERTIES TIMEOUT 60 PASS_REGULAR_EXPRESSION "kyty_shim_recompiler_robustness: ok" FAIL_REGULAR_EXPRESSION "terminate called|FAILED") + add_test(NAME kyty_shim_recompiler_min_lod_fallback COMMAND kyty_shim_recompiler_robustness_tests min-lod-fallback) + set_tests_properties(kyty_shim_recompiler_min_lod_fallback PROPERTIES TIMEOUT 60 PASS_REGULAR_EXPRESSION "kyty_shim_recompiler_robustness: ok" + FAIL_REGULAR_EXPRESSION "terminate called|FAILED") add_test(NAME kyty_shim_recompiler_valid_spirv COMMAND kyty_shim_recompiler_robustness_tests) set_tests_properties(kyty_shim_recompiler_valid_spirv PROPERTIES TIMEOUT 60 ENVIRONMENT "KYTY_FUZZ_SPIRV=abort" PASS_REGULAR_EXPRESSION "kyty_shim_recompiler_robustness: ok" FAIL_REGULAR_EXPRESSION "terminate called|FAILED|spirv-val refuses") diff --git a/core/libs/prx/libSceAgcDriver/KytyShim/tests/RecompilerRobustness.cpp b/core/libs/prx/libSceAgcDriver/KytyShim/tests/RecompilerRobustness.cpp index 388b8f6ad9..33a525397f 100644 --- a/core/libs/prx/libSceAgcDriver/KytyShim/tests/RecompilerRobustness.cpp +++ b/core/libs/prx/libSceAgcDriver/KytyShim/tests/RecompilerRobustness.cpp @@ -105,6 +105,90 @@ const std::vector Refusals = { "image_get_lod requires a pixel or compute shader"}, }; +struct MinLodCase { + const char* name; + std::vector words; + bool gradients; +}; + +const std::vector MinLodCases = { + {"image-sample-cl", {0xf0840108u, 0x00400400u, 0xf8000801u, 0x00000004u, 0xbf810000u}, false}, + {"image-sample-b-cl", {0xf0980108u, 0x00400400u, 0xf8000801u, 0x00000004u, 0xbf810000u}, false}, + {"image-sample-d-cl", {0xf08c0108u, 0x00400400u, 0xf8000801u, 0x00000004u, 0xbf810000u}, true}, +}; + +struct MinLodScan { + bool capability = false; + bool operand = false; + bool query = false; + bool explicit_lod = false; + bool gradients = false; +}; + +MinLodScan ScanMinLod(const std::vector& words) { + MinLodScan scan; + for (size_t offset = 5; offset < words.size();) { + const uint32_t count = words[offset] >> 16u; + if (count == 0u || count > words.size() - offset) break; + const auto opcode = static_cast(words[offset] & 0xffffu); + scan.capability |= opcode == spv::OpCapability && words[offset + 1u] == spv::CapabilityMinLod; + scan.query |= opcode == spv::OpImageQueryLod; + const bool sample = opcode == spv::OpImageSampleImplicitLod || opcode == spv::OpImageSampleExplicitLod; + const bool dref = opcode == spv::OpImageSampleDrefImplicitLod || opcode == spv::OpImageSampleDrefExplicitLod; + const uint32_t mask_at = dref ? 6u : 5u; + if ((sample || dref) && count > mask_at) { + const uint32_t mask = words[offset + mask_at]; + scan.operand |= (mask & spv::ImageOperandsMinLodMask) != 0u; + scan.gradients |= (mask & spv::ImageOperandsGradMask) != 0u; + scan.explicit_lod |= (opcode == spv::OpImageSampleExplicitLod || opcode == spv::OpImageSampleDrefExplicitLod) && + (mask & spv::ImageOperandsLodMask) != 0u; + } + offset += count; + } + return scan; +} + +bool MinLodFallback() { + bool ok = true; + for (const auto& entry: MinLodCases) { + for (const bool native: {true, false}) { + const std::string what = std::string(entry.name) + (native ? " with" : " without") + " shaderResourceMinLod"; + auto profile = KytyFuzz::Profile(); + profile.shader_resource_min_lod = native; + std::vector spirv; + std::string refusal; + bool emitted = false; + KytyFuzz::Run(entry.words, 0x04, 0x00, &refusal, &emitted, &profile, &spirv); + if (!emitted) { + std::printf("min-lod-fallback: FAILED, %s did not emit SPIR-V: %s\n", what.c_str(), refusal.c_str()); + ok = false; + continue; + } + std::string message; + if (!KytyFuzz::Valid(spirv, message)) { + std::printf("min-lod-fallback: FAILED, %s is not valid SPIR-V: %s\n", what.c_str(), message.c_str()); + ok = false; + continue; + } + const auto scan = ScanMinLod(spirv); + bool expected = false; + if (native) { + expected = scan.capability && scan.operand; + } else if (entry.gradients) { + expected = !scan.capability && !scan.operand && scan.gradients; + } else { + expected = !scan.capability && !scan.operand && scan.query && scan.explicit_lod; + } + if (!expected) { + std::printf("min-lod-fallback: FAILED, %s: capability=%d operand=%d query=%d explicit_lod=%d gradients=%d\n", + what.c_str(), scan.capability, scan.operand, scan.query, scan.explicit_lod, scan.gradients); + ok = false; + } + } + } + return ok; +} + struct ModeWrite { uint32_t word; uint32_t bits; @@ -197,6 +281,11 @@ int main(int argc, char** argv) { if (!FlatOffsets()) return 1; std::printf("flat-offsets: ok\n"); } + if (argc < 2 || std::string_view(argv[1]) == "min-lod-fallback") { + found = true; + if (!MinLodFallback()) return 1; + std::printf("min-lod-fallback: ok\n"); + } for (const auto& entry: Cases) { if (argc >= 2 && std::string_view(argv[1]) != entry.name) continue; found = true;