﻿# Synchronizing Vulkan ray queries: target the shader stage that actually reads the scene

Connect Vulkan acceleration builds to inline ray-query consumers with correct producer and consumer scopes, and separate build ordering from command recording order.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-vulkan-ray-query-consumer-stage-build-dependency/
Language: en

## Draw the resource dependency first

A renderer can build an acceleration structure and then read it from an inline query in a compute shader. The relevant relationship is a build write followed by a shader's acceleration read. Identify those operations before copying a synchronization snippet from another rendering path. Recording commands in a convenient textual order does not by itself specify every required execution and memory dependency for the resource consumed by the shader.

## Separate dependent builds explicitly

The [acceleration-build reference](https://docs.vulkan.org/refpages/latest/refpages/source/vkCmdBuildAccelerationStructuresKHR.html) states that individual builds in one multi-build call have no implied ordering or synchronization. A top-level build cannot depend on a lower-level structure being created in that same call. Schedule dependent builds with the required boundaries and preserve the lower-level resources referenced by the resulting scene. Batching several build descriptions is not equivalent to expressing a dependency graph among them.

## Target the actual inline consumer

The [ray-query extension](https://docs.vulkan.org/refpages/latest/refpages/source/VK_KHR_ray_query.html) allows queries inside compute and graphics shaders, not only a dedicated ray-tracing pipeline. For a query executed in compute, include the compute shader stage in the destination scope. A stage name mentioning ray tracing is not a universal substitute for wherever ray-query instructions happen to execute. Keep the dependency attached to the real consumer path when moving an effect between shader stages or pipeline designs.

## Match access scope as well as execution scope

The [memory-barrier reference](https://docs.vulkan.org/refpages/latest/refpages/source/VkMemoryBarrier2.html) separates stage masks from access masks. The [access-flag reference](https://docs.vulkan.org/refpages/latest/refpages/source/VkAccessFlagBits2.html) defines acceleration-structure read and write access. Specify the intended build producer and query consumer rather than treating an execution boundary alone as sufficient memory visibility. Cross-queue work may additionally require semaphore dependencies and, where applicable, queue-family ownership handling. A same-queue example should not silently be reused as a complete cross-queue protocol.

## Validate changing geometry under load

Change a diagnostic scene between frames and check that query results always match the intended completed build. Exercise the real number of in-flight jobs and enable appropriate validation for the deployment. The guides to [copy and compute overlap](/journal/gpu-async-copy-overlap/) and [GPU BVH traversal](/journal/learn-gpu-bvh-traversal/) provide adjacent context. A temporary full-device wait can help isolate a suspected dependency issue, but the final design should express the actual producer, reader and resource lifetime instead of relying on accidental serialization.
