From ae47876386703658a5b70981d80b44f986db2563 Mon Sep 17 00:00:00 2001 From: LarzVader <118484383+LarzVader@users.noreply.github.com> Date: Tue, 24 Nov 2026 22:16:26 +0100 Subject: [PATCH] fix(venus): Zink draws on Mali-G610 when the depth test is off (#3102) With depthTestEnable off, Vulkan ignores depthCompareOp. The Mali-G610 driver behind Venus (Nothing Phone (2a), Dimensity 7200 Pro) applies it anyway, even without a depth attachment. Zink leaves the op at VK_COMPARE_OP_NEVER whenever GL has the depth test off, so every fragment Zink draws is thrown away: clears and copies work, draws never show. This is the black Steam UI when it renders through Zink and the black picture of OpenGL games such as Stardew Valley. DXVK sets ALWAYS in the same case, which is why D3D games drew. The layer now hands the driver ALWAYS while the depth test is off: - vkCmdSetDepthTestEnable and vkCmdSetDepthCompareOp keep the state per command buffer, which vkBeginCommandBuffer clears. While the test is off the driver gets ALWAYS; turning it on again restores the application's op. - A pipeline with the depth test off and neither state dynamic is created with ALWAYS. It is on for every device with gaps. ALWAYS for a disabled depth test changes nothing on a driver that follows the spec. --- tools/tests/mali_compat_layer_test.c | 88 ++++++++++++++++++++++++++++ tools/venus/layer/bc.c | 6 +- tools/venus/layer/draw.c | 48 +++++++++++++++ tools/venus/layer/layer.c | 11 +++- tools/venus/layer/mali_compat.h | 12 +++- tools/venus/layer/pipeline.c | 14 +++++ 6 files changed, 175 insertions(+), 4 deletions(-) diff --git a/tools/tests/mali_compat_layer_test.c b/tools/tests/mali_compat_layer_test.c index 3584a66000..9b29e5c734 100644 --- a/tools/tests/mali_compat_layer_test.c +++ b/tools/tests/mali_compat_layer_test.c @@ -21,6 +21,8 @@ static VkPipelineColorBlendAttachmentState driver_attachments[4]; static uint32_t driver_attachment_count; static VkColorBlendEquationEXT driver_equations[4]; static uint32_t driver_equation_count; +static VkCompareOp driver_depth_op, driver_pipeline_depth_op; +static VkBool32 driver_depth_test; #define MAIN 1 #define VOID 2 @@ -305,10 +307,27 @@ static VkResult VKAPI_CALL mock_create_graphics_pipelines(VkDevice device, VkPip const VkShaderModuleCreateInfo *module = infos[0].pStages[0].pNext; assert(module && module->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO); capture_code(module->pCode, module->codeSize); + if (infos[0].pDepthStencilState) + driver_pipeline_depth_op = infos[0].pDepthStencilState->depthCompareOp; pipelines[0] = (VkPipeline)0x2000; return VK_SUCCESS; } +static VkResult VKAPI_CALL mock_begin_command_buffer(VkCommandBuffer cmd, const VkCommandBufferBeginInfo *info) +{ + return VK_SUCCESS; +} + +static void VKAPI_CALL mock_set_depth_test(VkCommandBuffer cmd, VkBool32 enable) +{ + driver_depth_test = enable; +} + +static void VKAPI_CALL mock_set_depth_op(VkCommandBuffer cmd, VkCompareOp op) +{ + driver_depth_op = op; +} + static void VKAPI_CALL mock_set_blend_equation(VkCommandBuffer cmd, uint32_t first, uint32_t count, const VkColorBlendEquationEXT *equations) { @@ -342,6 +361,9 @@ static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL mock_gdpa(VkDevice device, const MOCK("vkDestroyShaderModule", mock_destroy_shader_module); MOCK("vkCreateGraphicsPipelines", mock_create_graphics_pipelines); MOCK("vkCmdSetColorBlendEquationEXT", mock_set_blend_equation); + MOCK("vkBeginCommandBuffer", mock_begin_command_buffer); + MOCK("vkCmdSetDepthTestEnable", mock_set_depth_test); + MOCK("vkCmdSetDepthCompareOp", mock_set_depth_op); return NULL; } @@ -480,6 +502,71 @@ static void test_capable_device(void) destroy_device(device); } +static void create_depth_pipeline(VkDevice device, const VkPipelineDepthStencilStateCreateInfo *depth, + const VkDynamicState *dynamic, uint32_t dynamic_count) +{ + struct spirv plain; + fragment_shader(&plain, 0x00010000, false); + VkShaderModuleCreateInfo module = {VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, NULL, 0, + plain.n * sizeof(uint32_t), plain.w}; + VkPipelineShaderStageCreateInfo stage = {VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, &module, 0, + VK_SHADER_STAGE_FRAGMENT_BIT, VK_NULL_HANDLE, "main", NULL}; + VkPipelineColorBlendStateCreateInfo blend = {VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, NULL, 0, + VK_FALSE, VK_LOGIC_OP_COPY, 1, &plain_attachment, {0}}; + VkPipelineDynamicStateCreateInfo states = {VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, NULL, 0, + dynamic_count, dynamic}; + VkGraphicsPipelineCreateInfo info = {.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, .stageCount = 1, + .pStages = &stage, .pDepthStencilState = depth, .pColorBlendState = &blend, + .pDynamicState = dynamic_count ? &states : NULL}; + VkPipeline pipeline; + driver_pipeline_depth_op = VK_COMPARE_OP_MAX_ENUM; + PFN_vkCreateGraphicsPipelines create = + (PFN_vkCreateGraphicsPipelines)layer_gdpa(device, "vkCreateGraphicsPipelines"); + assert(create(device, VK_NULL_HANDLE, 1, &info, NULL, &pipeline) == VK_SUCCESS); +} + +static void test_depth_compare_op(void) +{ + VkDevice device; + VkPhysicalDeviceFeatures enabled = {.dualSrcBlend = VK_TRUE}; + assert(create_device((VkPhysicalDevice)&mock_lacking, NULL, &enabled, &device) == VK_SUCCESS); + PFN_vkBeginCommandBuffer begin = (PFN_vkBeginCommandBuffer)layer_gdpa(device, "vkBeginCommandBuffer"); + PFN_vkCmdSetDepthTestEnable set_test = + (PFN_vkCmdSetDepthTestEnable)layer_gdpa(device, "vkCmdSetDepthTestEnable"); + PFN_vkCmdSetDepthCompareOp set_op = (PFN_vkCmdSetDepthCompareOp)layer_gdpa(device, "vkCmdSetDepthCompareOp"); + assert((PFN_vkVoidFunction)set_test != (PFN_vkVoidFunction)mock_set_depth_test); + assert((PFN_vkVoidFunction)set_op != (PFN_vkVoidFunction)mock_set_depth_op); + + VkCommandBuffer cmd = (VkCommandBuffer)&mock_command_buffer; + VkCommandBufferBeginInfo begin_info = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; + assert(begin(cmd, &begin_info) == VK_SUCCESS); + set_op(cmd, VK_COMPARE_OP_LESS); + assert(driver_depth_op == VK_COMPARE_OP_LESS); + set_test(cmd, VK_FALSE); + assert(!driver_depth_test && driver_depth_op == VK_COMPARE_OP_ALWAYS); + set_op(cmd, VK_COMPARE_OP_NEVER); + assert(driver_depth_op == VK_COMPARE_OP_ALWAYS); + set_test(cmd, VK_TRUE); + assert(driver_depth_test && driver_depth_op == VK_COMPARE_OP_NEVER); + set_test(cmd, VK_FALSE); + assert(begin(cmd, &begin_info) == VK_SUCCESS); + set_op(cmd, VK_COMPARE_OP_GREATER); + assert(driver_depth_op == VK_COMPARE_OP_GREATER); + + VkPipelineDepthStencilStateCreateInfo depth = {.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, + .depthCompareOp = VK_COMPARE_OP_NEVER}; + create_depth_pipeline(device, &depth, NULL, 0); + assert(driver_pipeline_depth_op == VK_COMPARE_OP_ALWAYS && depth.depthCompareOp == VK_COMPARE_OP_NEVER); + const VkDynamicState dynamic[] = {VK_DYNAMIC_STATE_DEPTH_COMPARE_OP}; + create_depth_pipeline(device, &depth, dynamic, 1); + assert(driver_pipeline_depth_op == VK_COMPARE_OP_NEVER); + depth.depthTestEnable = VK_TRUE; + depth.depthCompareOp = VK_COMPARE_OP_LESS; + create_depth_pipeline(device, &depth, NULL, 0); + assert(driver_pipeline_depth_op == VK_COMPARE_OP_LESS); + destroy_device(device); +} + int main(int argc, char **argv) { test_spirv(argc > 1 ? argv[1] : NULL); @@ -502,6 +589,7 @@ int main(int argc, char **argv) test_lacking_device(); test_capable_device(); + test_depth_compare_op(); ((PFN_vkDestroyInstance)layer_gipa(instance, "vkDestroyInstance"))(instance, NULL); free(driver_code); diff --git a/tools/venus/layer/bc.c b/tools/venus/layer/bc.c index 328b68169d..3948126fd1 100644 --- a/tools/venus/layer/bc.c +++ b/tools/venus/layer/bc.c @@ -799,7 +799,11 @@ static void recycle_command_buffer(struct device *dev, VkCommandBuffer cmd) VKAPI_ATTR VkResult VKAPI_CALL BeginCommandBuffer(VkCommandBuffer cmd, const VkCommandBufferBeginInfo *info) { struct device *dev = device_from_handle(cmd); - recycle_command_buffer(dev, cmd); + if (dev->emulated & GAP_BC) + recycle_command_buffer(dev, cmd); + /* Dynamic state does not carry over from one recording to the next. */ + if (dev->emulated & GAP_DEPTH_OP) + depth_state_forget(dev, cmd); return dev->BeginCommandBuffer(cmd, info); } diff --git a/tools/venus/layer/draw.c b/tools/venus/layer/draw.c index f3621355b2..5d0dc4a13e 100644 --- a/tools/venus/layer/draw.c +++ b/tools/venus/layer/draw.c @@ -8,6 +8,8 @@ * stand-ins the layer owns. robustBufferAccess2 itself maps to robustBufferAccess (layer.c). * A null texel-buffer view gets an RGBA8 (uniform) or R32_UINT (storage) view of the stand-in * buffer: its format is not known here, but Zink leaves them unbound and never reads them. + * - depthCompareOp with the depth test off (GAP_DEPTH_OP): the driver is given ALWAYS while a + * command buffer has the test off, and the application's op again once it turns the test on. */ #include #include @@ -450,3 +452,49 @@ VKAPI_ATTR void VKAPI_CALL CmdBindVertexBuffers2(VkCommandBuffer cmd, uint32_t f } dev->CmdBindVertexBuffers2(cmd, first, count, fixed, fixed_offsets, sizes ? fixed_sizes : NULL, strides); } + +/* --- depthCompareOp while the depth test is off --- */ + +/* Per command buffer, packed into the map's value pointer so it is never NULL: bit 0 always set, + * bit 1 the test enable is known, bit 2 the test is on, bit 3 an op was set, bits 4 and up that op. */ +enum { DEPTH_VALID = 1, DEPTH_KNOWN = 2, DEPTH_ON = 4, DEPTH_OP = 8 }; + +static uintptr_t depth_state(struct device *dev, VkCommandBuffer cmd) +{ + uintptr_t state = (uintptr_t)map_get(&dev->depth_states, (uint64_t)(uintptr_t)cmd); + return state ? state : DEPTH_VALID; +} + +void depth_state_forget(struct device *dev, VkCommandBuffer cmd) +{ + pthread_mutex_lock(&dev->lock); + map_remove(&dev->depth_states, (uint64_t)(uintptr_t)cmd); + pthread_mutex_unlock(&dev->lock); +} + +VKAPI_ATTR void VKAPI_CALL CmdSetDepthTestEnable(VkCommandBuffer cmd, VkBool32 enable) +{ + struct device *dev = device_from_handle(cmd); + dev->CmdSetDepthTestEnable(cmd, enable); + pthread_mutex_lock(&dev->lock); + uintptr_t state = depth_state(dev, cmd); + uintptr_t next = (state & ~(uintptr_t)DEPTH_ON) | DEPTH_KNOWN | (enable ? DEPTH_ON : 0); + map_put(&dev->depth_states, (uint64_t)(uintptr_t)cmd, (void *)next); + pthread_mutex_unlock(&dev->lock); + if (!enable) + dev->CmdSetDepthCompareOp(cmd, VK_COMPARE_OP_ALWAYS); + else if (state & DEPTH_OP) + dev->CmdSetDepthCompareOp(cmd, (VkCompareOp)(state >> 4)); +} + +VKAPI_ATTR void VKAPI_CALL CmdSetDepthCompareOp(VkCommandBuffer cmd, VkCompareOp op) +{ + struct device *dev = device_from_handle(cmd); + pthread_mutex_lock(&dev->lock); + uintptr_t state = depth_state(dev, cmd); + uintptr_t next = (state & (uintptr_t)(DEPTH_VALID | DEPTH_KNOWN | DEPTH_ON)) | DEPTH_OP | ((uintptr_t)op << 4); + map_put(&dev->depth_states, (uint64_t)(uintptr_t)cmd, (void *)next); + pthread_mutex_unlock(&dev->lock); + bool off = (state & DEPTH_KNOWN) && !(state & DEPTH_ON); + dev->CmdSetDepthCompareOp(cmd, off ? VK_COMPARE_OP_ALWAYS : op); +} diff --git a/tools/venus/layer/layer.c b/tools/venus/layer/layer.c index 21778b859e..9aeb0bcf0e 100644 --- a/tools/venus/layer/layer.c +++ b/tools/venus/layer/layer.c @@ -606,7 +606,7 @@ static VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physical_dev dev->key = dispatch_key(*device); dev->handle = *device; dev->gdpa = gdpa; - dev->emulated = requested; + dev->emulated = requested | (gaps ? GAP_DEPTH_OP : 0); pthread_mutex_init(&dev->lock, NULL); #define LOAD(name) \ dev->name = (PFN_vk##name)gdpa(*device, "vk" #name); \ @@ -642,6 +642,8 @@ static VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physical_dev (requested & GAP_NULL_DESCRIPTOR) ? " nullDescriptor" : "", (requested & GAP_DUAL_SRC_BLEND) ? " dualSrcBlend" : "", properties.deviceName); } + if (dev->emulated & GAP_DEPTH_OP) + layer_log("depthCompareOp becomes ALWAYS while the depth test is off"); return VK_SUCCESS; } @@ -669,6 +671,7 @@ static VKAPI_ATTR void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocat } } map_free(&dev->shader_modules); + map_free(&dev->depth_states); pthread_mutex_destroy(&dev->lock); free(dev); } @@ -696,6 +699,10 @@ static const struct hook device_hooks[] = { HOOK(CmdSetScissorWithCount, GAP_MULTI_VIEWPORT), HOOK_ALIAS(CmdSetScissorWithCount, CmdSetScissorWithCountEXT, GAP_MULTI_VIEWPORT), HOOK(CmdSetColorBlendEquationEXT, GAP_DUAL_SRC_BLEND), + HOOK(CmdSetDepthTestEnable, GAP_DEPTH_OP), + HOOK_ALIAS(CmdSetDepthTestEnable, CmdSetDepthTestEnableEXT, GAP_DEPTH_OP), + HOOK(CmdSetDepthCompareOp, GAP_DEPTH_OP), + HOOK_ALIAS(CmdSetDepthCompareOp, CmdSetDepthCompareOpEXT, GAP_DEPTH_OP), HOOK(CreateImage, GAP_BC), HOOK(DestroyImage, GAP_BC), HOOK(CreateImageView, GAP_BC), @@ -727,7 +734,7 @@ static const struct hook device_hooks[] = { HOOK_ALIAS(UnmapMemory2, UnmapMemory2KHR, GAP_BC), HOOK(AllocateCommandBuffers, GAP_BC), HOOK(FreeCommandBuffers, GAP_BC), - HOOK(BeginCommandBuffer, GAP_BC), + HOOK(BeginCommandBuffer, GAP_BC | GAP_DEPTH_OP), HOOK(ResetCommandBuffer, GAP_BC), HOOK(ResetCommandPool, GAP_BC), HOOK(DestroyCommandPool, GAP_BC), diff --git a/tools/venus/layer/mali_compat.h b/tools/venus/layer/mali_compat.h index eb171bb6f9..073907f41a 100644 --- a/tools/venus/layer/mali_compat.h +++ b/tools/venus/layer/mali_compat.h @@ -30,6 +30,10 @@ enum gap { GAP_DEPTH_CLIP = 1u << 7, /* VK_EXT_depth_clip_enable: depthClipEnable off becomes depth clamp */ GAP_NULL_DESCRIPTOR = 1u << 8, /* VK_EXT_robustness2 missing entirely: nullDescriptor stand-ins */ GAP_DUAL_SRC_BLEND = 1u << 9, + /* A driver bug, not a missing feature: the Mali-G610 driver behind Venus applies depthCompareOp + * with the depth test off, so Zink's NEVER discards every fragment. On for every device with + * gaps; ALWAYS for a disabled test changes nothing on a driver that follows the spec. */ + GAP_DEPTH_OP = 1u << 10, }; #define GAPS_SHADER (GAP_CLIP_DISTANCE | GAP_CULL_DISTANCE) @@ -138,7 +142,9 @@ struct instance { X(CmdPushDescriptorSetWithTemplate) \ X(CmdBindVertexBuffers) \ X(CmdBindVertexBuffers2) \ - X(CmdSetColorBlendEquationEXT) + X(CmdSetColorBlendEquationEXT) \ + X(CmdSetDepthTestEnable) \ + X(CmdSetDepthCompareOp) struct staging_block; @@ -163,6 +169,7 @@ struct device { struct handle_map command_buffers; /* VkCommandBuffer -> struct command_buffer */ struct staging_block *free_staging; struct handle_map templates; /* VkDescriptorUpdateTemplate -> struct template_info (draw.c) */ + struct handle_map depth_states; /* VkCommandBuffer -> packed depth test state (draw.c) */ /* nullDescriptor stand-ins (draw.c): 0 not made yet, 1 ready, -1 failed. */ int null_ready; VkBuffer null_buffer; @@ -311,4 +318,7 @@ VKAPI_ATTR void VKAPI_CALL CmdPushDescriptorSetWithTemplate(VkCommandBuffer cmd, VKAPI_ATTR void VKAPI_CALL CmdBindVertexBuffers(VkCommandBuffer cmd, uint32_t first, uint32_t count, const VkBuffer *buffers, const VkDeviceSize *offsets); VKAPI_ATTR void VKAPI_CALL CmdBindVertexBuffers2(VkCommandBuffer cmd, uint32_t first, uint32_t count, const VkBuffer *buffers, const VkDeviceSize *offsets, const VkDeviceSize *sizes, const VkDeviceSize *strides); void null_resources_destroy(struct device *dev); +VKAPI_ATTR void VKAPI_CALL CmdSetDepthTestEnable(VkCommandBuffer cmd, VkBool32 enable); +VKAPI_ATTR void VKAPI_CALL CmdSetDepthCompareOp(VkCommandBuffer cmd, VkCompareOp op); +void depth_state_forget(struct device *dev, VkCommandBuffer cmd); #endif diff --git a/tools/venus/layer/pipeline.c b/tools/venus/layer/pipeline.c index 95c0ccd1ee..01236be5b7 100644 --- a/tools/venus/layer/pipeline.c +++ b/tools/venus/layer/pipeline.c @@ -143,6 +143,7 @@ struct pipeline_rewrite { /* VK_EXT_depth_clip_enable's state, taken out of the application's chain for the call. */ VkBaseOutStructure *clip_prev, *clip_node; VkPipelineViewportStateCreateInfo viewport; + VkPipelineDepthStencilStateCreateInfo depth_stencil; VkPipelineShaderStageCreateInfo stages[MAX_STAGES]; VkShaderModuleCreateInfo modules[MAX_STAGES]; struct spirv_code code[MAX_STAGES]; @@ -401,6 +402,19 @@ static void rewrite_pipeline(struct device *dev, VkGraphicsPipelineCreateInfo *i rw->viewport.scissorCount = 1; info->pViewportState = &rw->viewport; } + if ((dev->emulated & GAP_DEPTH_OP) && info->pDepthStencilState && !info->pDepthStencilState->depthTestEnable && + info->pDepthStencilState->depthCompareOp != VK_COMPARE_OP_ALWAYS) { + bool dynamic = false; + for (uint32_t i = 0; info->pDynamicState && i < info->pDynamicState->dynamicStateCount; i++) { + VkDynamicState state = info->pDynamicState->pDynamicStates[i]; + dynamic |= state == VK_DYNAMIC_STATE_DEPTH_TEST_ENABLE || state == VK_DYNAMIC_STATE_DEPTH_COMPARE_OP; + } + if (!dynamic) { + rw->depth_stencil = *info->pDepthStencilState; + rw->depth_stencil.depthCompareOp = VK_COMPARE_OP_ALWAYS; + info->pDepthStencilState = &rw->depth_stencil; + } + } if (dev->emulated & GAP_DUAL_SRC_BLEND) { rewrite_blend(info, rw); if (rw->attachments)