From 2be78b6965eedf7b70d2b9d4e8f6c885b8f6cabc Mon Sep 17 00:00:00 2001 From: Dean Galvin Date: Tue, 29 Sep 2026 07:51:12 +1300 Subject: [PATCH] feat(agc): apply a texture descriptor's MIN_LOD clamp through the image view T# word1 MIN_LOD (unsigned 4.8, in levels of the whole surface) was rejected. Sampled views now chain VkImageViewMinLodCreateInfoEXT (VK_EXT_image_view_min_lod, enabled when the device offers the minLod feature). Its minLod counts levels of the whole image exactly as MIN_LOD counts levels of the whole surface, because the image holds every mip and the view starts at BASE_LEVEL. - A clamp at or below BASE_LEVEL cannot change which level a sample reads and is left off; one past the view's last level is bounded by it (the view selects that level anyway, and Vulkan requires the bound). - A clamp that binds on a device without the feature throws. - The texture cache already keys by the eight descriptor words, so views with different clamps are not shared. - Storage views address one level: a clamp at or below that level is accepted, one above it still throws. --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 16 ++ .../Execution/src/VulkanDevice.cpp | 15 ++ .../Graphics/include/Context.hpp | 1 + .../Graphics/include/GuestTextureResource.hpp | 3 + .../Graphics/src/GuestTextureResource.cpp | 7 +- .../Graphics/src/ShaderResources.cpp | 1 + .../libSceAgcDriver/Graphics/src/Texture.cpp | 17 ++ .../tests/GuestTextureResource.cpp | 24 ++- .../prx/libSceAgcDriver/tests/Recorder.cpp | 167 ++++++++++++++++++ .../tests/shaders/SampleLod.comp | 10 ++ 10 files changed, 257 insertions(+), 4 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/shaders/SampleLod.comp diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index fdb83b071..d57eaec50 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -197,12 +197,28 @@ target_link_libraries(agc_driver_flip_tests PRIVATE set_target_properties(agc_driver_flip_tests PROPERTIES CXX_EXTENSIONS OFF) configure_windows_unwind(agc_driver_flip_tests) +set(agcSampleLodShaderSource ${CMAKE_CURRENT_SOURCE_DIR}/tests/shaders/SampleLod.comp) +set(agcTestShaderGeneratedRoot ${CMAKE_CURRENT_BINARY_DIR}/generated-tests) +set(agcSampleLodShaderSpv ${agcTestShaderGeneratedRoot}/SampleLod.spv) +set(agcSampleLodShaderHeader ${agcTestShaderGeneratedRoot}/SampleLod_spv.h) +add_custom_command( + OUTPUT "${agcSampleLodShaderHeader}" + COMMAND ${CMAKE_COMMAND} -E make_directory "${agcTestShaderGeneratedRoot}" + COMMAND "$" -V --target-env vulkan1.1 -o "${agcSampleLodShaderSpv}" "${agcSampleLodShaderSource}" + COMMAND ${CMAKE_COMMAND} -DINPUT=${agcSampleLodShaderSpv} -DOUTPUT=${agcSampleLodShaderHeader} + -DSYMBOL=SAMPLE_LOD_SPV -P "${CMAKE_CURRENT_SOURCE_DIR}/embed_spirv.cmake" + DEPENDS "${agcSampleLodShaderSource}" glslang-standalone + VERBATIM +) + add_executable(agc_driver_recorder_tests EXCLUDE_FROM_ALL tests/Recorder.cpp + ${agcSampleLodShaderHeader} ) target_include_directories(agc_driver_recorder_tests PRIVATE ${LIBS_INCLUDE_DIR} + ${agcTestShaderGeneratedRoot} ${CMAKE_SOURCE_DIR}/core/shader/recompiler ${CMAKE_SOURCE_DIR}/3rdparty/Vulkan-Headers/include ) diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp index 5db14ed8d..483b52a03 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp @@ -188,6 +188,7 @@ struct VulkanDevice::State { bool descriptorIndexing = false; bool primitiveListRestart = false; bool depthClipControl = false; + bool imageViewMinLod = false; bool depthClamp = false; bool occlusionQueryPrecise = false; VkDeviceSize hostImportAlignment = 0; @@ -743,6 +744,15 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window) : state(std::make_u } listRestartFeatures = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIMITIVE_TOPOLOGY_LIST_RESTART_FEATURES_EXT}; listRestartFeatures.primitiveTopologyListRestart = state->primitiveListRestart ? VK_TRUE : VK_FALSE; + VkPhysicalDeviceImageViewMinLodFeaturesEXT minLodFeatures{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_VIEW_MIN_LOD_FEATURES_EXT}; + if (hasExtension(VK_EXT_IMAGE_VIEW_MIN_LOD_EXTENSION_NAME)) { + VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &minLodFeatures}; + state->InstanceFunction("vkGetPhysicalDeviceFeatures2")(selected, &features); + state->imageViewMinLod = minLodFeatures.minLod == VK_TRUE; + if (state->imageViewMinLod) deviceExtensions.push_back(VK_EXT_IMAGE_VIEW_MIN_LOD_EXTENSION_NAME); + } + minLodFeatures = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_VIEW_MIN_LOD_FEATURES_EXT}; + minLodFeatures.minLod = VK_TRUE; VkPhysicalDeviceDepthClipControlFeaturesEXT depthClipFeatures{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_CONTROL_FEATURES_EXT}; if (hasExtension(VK_EXT_DEPTH_CLIP_CONTROL_EXTENSION_NAME)) { VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &depthClipFeatures}; @@ -838,6 +848,10 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window) : state(std::make_u listRestartFeatures.pNext = const_cast(deviceInfo.pNext); deviceInfo.pNext = &listRestartFeatures; } + if (state->imageViewMinLod) { + minLodFeatures.pNext = const_cast(deviceInfo.pNext); + deviceInfo.pNext = &minLodFeatures; + } byteFeatures.pNext = const_cast(deviceInfo.pNext); if (state->fragmentShaderBarycentric) { barycentricFeatures.pNext = byteFeatures.pNext; @@ -2189,6 +2203,7 @@ Graphics::Context VulkanDevice::buildContext() const { context.functions = state->functionsReady ? &state->deviceFunctions : nullptr; context.descriptorIndexing = state->descriptorIndexing; context.primitiveListRestart = state->primitiveListRestart; + context.imageViewMinLod = state->imageViewMinLod; return context; } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp index 4060d8976..9dd6429be 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/Context.hpp @@ -130,6 +130,7 @@ struct Context { // (bindless image tables in graphics stages). bool descriptorIndexing = false; bool primitiveListRestart = false; + bool imageViewMinLod = false; template TFunction Function(const char* name) const { diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp index 7d5393043..b09d3168c 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/GuestTextureResource.hpp @@ -60,8 +60,11 @@ struct GuestTextureResource { // DCC metadata of a compressed surface, or 0 (see DccMetadata.hpp). std::uint64_t dccAddress = 0; bool dccAlphaOnMsb = false; + std::uint32_t minLod = 0; }; +float EffectiveMinLod(const GuestTextureResource& resource); + GuestTextureResource DecodeTextureResource(std::span words); bool MatchesGuestDimension(ShaderRecompiler::DescriptorImageShape shape, TextureDimension dimension); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestTextureResource.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestTextureResource.cpp index a18b2f49a..fc8dfba9a 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/GuestTextureResource.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/GuestTextureResource.cpp @@ -88,7 +88,6 @@ GuestTextureResource DecodeTextureResource(std::span words) requireValidDstSel(dstSelZ); requireValidDstSel(dstSelW); - Require(minLod == 0, "guest texture descriptor uses a nonzero minimum LOD clamp which is not implemented"); // The LOD warning threshold and mip statistics counters are texture-streaming feedback: the GPU // reports which mips were wanted. Nothing is reported back here, which only affects what the title // chooses to stream, not what this access returns. @@ -176,9 +175,15 @@ GuestTextureResource DecodeTextureResource(std::span words) result.dstSelW = static_cast(dstSelW); result.dccAddress = metaCompress ? metaAddr << 8u : 0u; result.dccAlphaOnMsb = dccAlphaPos; + result.minLod = minLod; return result; } +float EffectiveMinLod(const GuestTextureResource& resource) { + if (resource.minLod <= resource.baseLevel * 256u) return 0.0f; + return std::min(static_cast(resource.minLod) / 256.0f, static_cast(resource.lastLevel)); +} + bool MatchesGuestDimension(ShaderRecompiler::DescriptorImageShape shape, TextureDimension dimension) { switch (shape) { case ShaderRecompiler::DescriptorImageShape::Image1D: return dimension == TextureDimension::k1D; diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp index c4b2930f9..5d096a597 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp @@ -2420,6 +2420,7 @@ void ShaderResources::resolveImageBinding(const ShaderRecompiler::DescriptorBind const auto resource = record != nullptr && record->decoded ? record->resource : DecodeTextureResource(words); if (binding.imageShape.has_value()) Require(MatchesGuestDimension(*binding.imageShape, resource.dimension), "guest storage texture dimension disagrees with the shader's declared image shape"); const auto mip = std::min(resource.baseLevel + mipOffset, resource.mipCount - 1u); + Require(resource.minLod <= mip * 256u, "guest storage texture descriptor clamps its minimum LOD above the level it addresses, which is not implemented"); const auto guestBytes = record != nullptr && record->decoded ? record->guestBytes : DescribeSurface(resource).guestBytes; // The same surface as the previous element: its image was just looked up and refreshed. if (sameAsPrevious && StorageDedupeEnabled()) storageTextures.push_back(storageTextures.back()); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp index ce85f2fe7..923f8ab0e 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/Texture.cpp @@ -210,6 +210,19 @@ VkImageViewType ViewTypeFor(TextureDimension dimension, [[maybe_unused]] std::ui } +namespace { + +void ChainMinLod(const Context& context, const GuestTextureResource& descriptor, VkImageViewCreateInfo& viewInfo, VkImageViewMinLodCreateInfoEXT& minLod) { + const auto clamp = EffectiveMinLod(descriptor); + if (clamp == 0.0f) return; + Require(context.imageViewMinLod, "guest texture descriptor uses a minimum LOD clamp, which needs VK_EXT_image_view_min_lod"); + minLod.minLod = clamp; + minLod.pNext = viewInfo.pNext; + viewInfo.pNext = &minLod; +} + +} + Texture::Texture(const Context& context, TextureDetiler& detiler, const GuestTextureResource& descriptor, VkComponentMapping components, std::span snapshot) : context(context) { static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr; PhaseTimer timer; @@ -403,6 +416,8 @@ Texture::Texture(const Context& context, TextureDetiler& detiler, const GuestTex viewInfo.format = vkFormat; viewInfo.components = components; viewInfo.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, descriptor.baseLevel, viewLevelCount, descriptor.baseArray, viewLayerCount}; + VkImageViewMinLodCreateInfoEXT minLod{VK_STRUCTURE_TYPE_IMAGE_VIEW_MIN_LOD_CREATE_INFO_EXT}; + ChainMinLod(context, descriptor, viewInfo, minLod); Check(context.Function("vkCreateImageView")(context.device, &viewInfo, nullptr, &view), "vkCreateImageView"); if (profile) { auto& totals = Profile(); @@ -444,6 +459,8 @@ Texture::Texture(const Context& context, const std::shared_ptr& viewInfo.format = vkFormat; viewInfo.components = components; viewInfo.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, descriptor.baseLevel, viewLevelCount, descriptor.baseArray, viewLayerCount}; + VkImageViewMinLodCreateInfoEXT minLod{VK_STRUCTURE_TYPE_IMAGE_VIEW_MIN_LOD_CREATE_INFO_EXT}; + ChainMinLod(context, descriptor, viewInfo, minLod); Check(context.Function("vkCreateImageView")(context.device, &viewInfo, nullptr, &view), "vkCreateImageView storage view"); if (profile) { auto& totals = Profile(); diff --git a/core/libs/prx/libSceAgcDriver/tests/GuestTextureResource.cpp b/core/libs/prx/libSceAgcDriver/tests/GuestTextureResource.cpp index 64116f4a8..092a548c0 100644 --- a/core/libs/prx/libSceAgcDriver/tests/GuestTextureResource.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/GuestTextureResource.cpp @@ -178,9 +178,27 @@ void RunGuestTextureResourceTests() { zeroAddress.base40 = 0; rejectFields(zeroAddress, "null base address"); - Fields badMinLod = base; - badMinLod.minLod = 1; - rejectFields(badMinLod, "nonzero minimum LOD clamp"); + Fields minLod = base; + minLod.maxMip = 4; + minLod.baseLevel = 1; + minLod.lastLevel = 3; + minLod.minLod = 0x180; + auto clamped = DecodeTextureResource(pack(minLod)); + Require(clamped.minLod == 0x180 && EffectiveMinLod(clamped) == 1.5f, "MIN_LOD 1.5 over levels 1..3 did not clamp at 1.5"); + minLod.minLod = 0x100; + clamped = DecodeTextureResource(pack(minLod)); + Require(EffectiveMinLod(clamped) == 0.0f, "MIN_LOD at BASE_LEVEL cannot bind but was applied"); + minLod.minLod = 0x0c0; + clamped = DecodeTextureResource(pack(minLod)); + Require(EffectiveMinLod(clamped) == 0.0f, "MIN_LOD below BASE_LEVEL cannot bind but was applied"); + minLod.minLod = 0xfff; + clamped = DecodeTextureResource(pack(minLod)); + Require(EffectiveMinLod(clamped) == 3.0f, "MIN_LOD past the view's last level was not bounded by it"); + minLod.baseLevel = 0; + minLod.minLod = 0x001; + clamped = DecodeTextureResource(pack(minLod)); + Require(EffectiveMinLod(clamped) == 1.0f / 256.0f, "the smallest MIN_LOD step above level 0 was lost"); + Require(DecodeTextureResource(pack(base)).minLod == 0 && EffectiveMinLod(DecodeTextureResource(pack(base))) == 0.0f, "an unclamped descriptor gained a MIN_LOD"); // Streaming feedback fields decode; nothing is reported back. Fields feedback = base; diff --git a/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp b/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp index e55e6fdc6..6655fd732 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Recorder.cpp @@ -13,6 +13,7 @@ #include "prx/libc/include/GuestAllocations.hpp" #include "prx/libc/include/GuestArena.hpp" #include "prx/libc/include/GuestWriteWatch.hpp" +#include "SampleLod_spv.h" #include #ifdef _WIN32 #ifndef NOMINMAX @@ -25,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -130,6 +132,19 @@ public: function("vkGetPhysicalDeviceProperties2")(context.physical, &properties); context.hostImportAlignment = hostProperties.minImportedHostPointerAlignment; } + VkPhysicalDeviceImageViewMinLodFeaturesEXT minLod{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_VIEW_MIN_LOD_FEATURES_EXT}; + if (hasExtension(VK_EXT_IMAGE_VIEW_MIN_LOD_EXTENSION_NAME)) { + VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &minLod}; + function("vkGetPhysicalDeviceFeatures2")(context.physical, &features); + context.imageViewMinLod = minLod.minLod == VK_TRUE; + } + minLod = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_VIEW_MIN_LOD_FEATURES_EXT}; + minLod.minLod = VK_TRUE; + if (context.imageViewMinLod) { + extensionsEnabled.push_back(VK_EXT_IMAGE_VIEW_MIN_LOD_EXTENSION_NAME); + minLod.pNext = address.pNext; + address.pNext = &minLod; + } VkDeviceCreateInfo device{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, &address}; device.queueCreateInfoCount = 1; device.pQueueCreateInfos = &queue; @@ -1555,6 +1570,157 @@ void dataRefreshTests(const Device& device, Recorder& recorder) { } +class SampleProgram { +public: + SampleProgram(const Context& context, Recorder& recorder) : context(context), recorder(recorder), result(context, sizeof(float) * 4, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) { + VkDescriptorSetLayoutBinding bindings[2]{}; + bindings[0] = {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; + bindings[1] = {1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; + VkDescriptorSetLayoutCreateInfo layoutInfo{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO}; + layoutInfo.bindingCount = 2; + layoutInfo.pBindings = bindings; + Check(context.Function("vkCreateDescriptorSetLayout")(context.device, &layoutInfo, nullptr, &setLayout), "vkCreateDescriptorSetLayout"); + const VkPushConstantRange push{VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(float)}; + VkPipelineLayoutCreateInfo pipelineLayoutInfo{VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; + pipelineLayoutInfo.setLayoutCount = 1; + pipelineLayoutInfo.pSetLayouts = &setLayout; + pipelineLayoutInfo.pushConstantRangeCount = 1; + pipelineLayoutInfo.pPushConstantRanges = &push; + Check(context.Function("vkCreatePipelineLayout")(context.device, &pipelineLayoutInfo, nullptr, &pipelineLayout), "vkCreatePipelineLayout"); + VkShaderModuleCreateInfo moduleInfo{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO}; + moduleInfo.codeSize = sizeof(SAMPLE_LOD_SPV); + moduleInfo.pCode = SAMPLE_LOD_SPV; + Check(context.Function("vkCreateShaderModule")(context.device, &moduleInfo, nullptr, &module), "vkCreateShaderModule"); + VkComputePipelineCreateInfo pipelineInfo{VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; + pipelineInfo.stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, nullptr, 0, VK_SHADER_STAGE_COMPUTE_BIT, module, "main", nullptr}; + pipelineInfo.layout = pipelineLayout; + Check(context.Function("vkCreateComputePipelines")(context.device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &pipeline), "vkCreateComputePipelines"); + VkSamplerCreateInfo samplerInfo{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO}; + samplerInfo.magFilter = VK_FILTER_NEAREST; + samplerInfo.minFilter = VK_FILTER_NEAREST; + samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; + samplerInfo.addressModeU = samplerInfo.addressModeV = samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; + samplerInfo.maxLod = VK_LOD_CLAMP_NONE; + Check(context.Function("vkCreateSampler")(context.device, &samplerInfo, nullptr, &sampler), "vkCreateSampler"); + const VkDescriptorPoolSize sizes[2]{{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1}, {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1}}; + VkDescriptorPoolCreateInfo poolInfo{VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO}; + poolInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; + poolInfo.maxSets = 1; + poolInfo.poolSizeCount = 2; + poolInfo.pPoolSizes = sizes; + Check(context.Function("vkCreateDescriptorPool")(context.device, &poolInfo, nullptr, &pool), "vkCreateDescriptorPool"); + } + ~SampleProgram() { + context.Function("vkDestroyDescriptorPool")(context.device, pool, nullptr); + context.Function("vkDestroySampler")(context.device, sampler, nullptr); + context.Function("vkDestroyPipeline")(context.device, pipeline, nullptr); + context.Function("vkDestroyShaderModule")(context.device, module, nullptr); + context.Function("vkDestroyPipelineLayout")(context.device, pipelineLayout, nullptr); + context.Function("vkDestroyDescriptorSetLayout")(context.device, setLayout, nullptr); + } + SampleProgram(const SampleProgram&) = delete; + SampleProgram& operator=(const SampleProgram&) = delete; + + float Red(VkImageView view, VkImageLayout layout, float lod) { + VkDescriptorSetAllocateInfo allocateInfo{VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO}; + allocateInfo.descriptorPool = pool; + allocateInfo.descriptorSetCount = 1; + allocateInfo.pSetLayouts = &setLayout; + VkDescriptorSet set = VK_NULL_HANDLE; + Check(context.Function("vkAllocateDescriptorSets")(context.device, &allocateInfo, &set), "vkAllocateDescriptorSets"); + const VkDescriptorImageInfo image{sampler, view, layout}; + const VkDescriptorBufferInfo buffer{result.Handle(), 0, VK_WHOLE_SIZE}; + VkWriteDescriptorSet writes[2]{{VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}, {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET}}; + writes[0].dstSet = set; + writes[0].dstBinding = 0; + writes[0].descriptorCount = 1; + writes[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; + writes[0].pImageInfo = ℑ + writes[1].dstSet = set; + writes[1].dstBinding = 1; + writes[1].descriptorCount = 1; + writes[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; + writes[1].pBufferInfo = &buffer; + context.Function("vkUpdateDescriptorSets")(context.device, 2, writes, 0, nullptr); + const auto commands = recorder.Commands(); + RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_SHADER_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT); + context.Function("vkCmdBindPipeline")(commands, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline); + context.Function("vkCmdBindDescriptorSets")(commands, VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout, 0, 1, &set, 0, nullptr); + context.Function("vkCmdPushConstants")(commands, pipelineLayout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(lod), &lod); + context.Function("vkCmdDispatch")(commands, 1, 1, 1); + RecordMemoryBarrier(context, commands, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_HOST_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT); + recorder.Submit(); + recorder.Sync(); + Check(context.Function("vkFreeDescriptorSets")(context.device, pool, 1, &set), "vkFreeDescriptorSets"); + float texel[4]; + std::memcpy(texel, result.Bytes().data(), sizeof(texel)); + return texel[0]; + } + +private: + const Context& context; + Recorder& recorder; + Buffer result; + VkDescriptorSetLayout setLayout = VK_NULL_HANDLE; + VkPipelineLayout pipelineLayout = VK_NULL_HANDLE; + VkShaderModule module = VK_NULL_HANDLE; + VkPipeline pipeline = VK_NULL_HANDLE; + VkSampler sampler = VK_NULL_HANDLE; + VkDescriptorPool pool = VK_NULL_HANDLE; +}; + +void expectRed(float got, float want, const char* what) { + if (std::abs(got - want) > 1.5f / 255.0f) throw std::runtime_error(std::string(what) + ": read " + std::to_string(got) + ", expected " + std::to_string(want)); +} + +void minLodTests(const Device& device, Recorder& recorder) { + const auto& context = device.GetContext(); + if (!context.imageViewMinLod) { + std::cout << "VK_EXT_image_view_min_lod unavailable: minimum LOD clamp not tested\n"; + return; + } + TextureDetiler detiler(context); + GuestTextureResource resource{}; + resource.baseAddress = 0x100000; + resource.width = 8; + resource.height = 8; + resource.mipCount = 4; + resource.baseLevel = 0; + resource.lastLevel = 3; + resource.tileMode = TextureTileMode::kLinear; + resource.dimension = TextureDimension::k2D; + resource.format = 56; + resource.dstSelX = 4; + resource.dstSelY = 5; + resource.dstSelZ = 6; + resource.dstSelW = 7; + const auto geometry = DescribeSurface(resource); + Require(geometry.mips.size() == 4, "the min LOD test surface has an unexpected mip count"); + const std::array levels{0x00, 0x40, 0x80, 0xff}; + std::vector snapshot(static_cast(geometry.guestBytes)); + for (std::size_t level = 0; level < levels.size(); ++level) { + const auto& mip = geometry.mips[level]; + std::fill_n(snapshot.begin() + static_cast(mip.tiledOffset), static_cast(mip.tiledSize), std::byte{levels[level]}); + } + SampleProgram program(context, recorder); + const VkComponentMapping identity{VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY}; + const auto sample = [&](std::uint32_t minLod, float lod, std::uint32_t baseLevel = 0) { + auto described = resource; + described.minLod = minLod; + described.baseLevel = baseLevel; + Texture texture(context, detiler, described, identity, snapshot); + return program.Red(texture.View(), texture.Layout(), lod); + }; + const auto unorm = [&](std::size_t level) { return levels[level] / 255.0f; }; + expectRed(sample(0, 0.0f), unorm(0), "minimum LOD clamp: no clamp reads level 0"); + expectRed(sample(0x100, 0.0f), unorm(1), "minimum LOD clamp: MIN_LOD 1 reads level 1 at LOD 0"); + expectRed(sample(0x180, 0.0f), (unorm(1) + unorm(2)) / 2.0f, "minimum LOD clamp: MIN_LOD 1.5 blends levels 1 and 2"); + expectRed(sample(0x100, 2.0f), unorm(2), "minimum LOD clamp: MIN_LOD 1 lowered LOD 2"); + expectRed(sample(0xfff, 0.0f), unorm(3), "minimum LOD clamp: MIN_LOD past the last level reads the last level"); + expectRed(sample(0x200, 0.0f, 1), unorm(2), "minimum LOD clamp: MIN_LOD 2 over a view from level 1 reads level 2"); + expectRed(sample(0x100, 0.0f, 1), unorm(1), "minimum LOD clamp: MIN_LOD at the view's base level reads its base level"); +} + int main() { try { Device device; @@ -1581,6 +1747,7 @@ int main() { importWindowTests(device, recorder); dataWordPositionsTests(); dataRefreshTests(device, recorder); + minLodTests(device, recorder); std::cout << "Recorder read tracking and label tests passed\n"; return 0; } catch (const std::exception& error) { diff --git a/core/libs/prx/libSceAgcDriver/tests/shaders/SampleLod.comp b/core/libs/prx/libSceAgcDriver/tests/shaders/SampleLod.comp new file mode 100644 index 000000000..507f295c8 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/shaders/SampleLod.comp @@ -0,0 +1,10 @@ +#version 450 + +layout(local_size_x = 1) in; +layout(set = 0, binding = 0) uniform sampler2D source; +layout(set = 0, binding = 1, std430) buffer Result { vec4 texel; }; +layout(push_constant) uniform Parameters { float lod; } parameters; + +void main() { + texel = textureLod(source, vec2(0.5), parameters.lod); +}