Skip to content

Commit

Permalink
Cleanup/Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-lunarg committed Feb 24, 2025
1 parent c608c6e commit c6e75ed
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 124 deletions.
85 changes: 24 additions & 61 deletions framework/decode/vulkan_replay_consumer_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3548,7 +3548,7 @@ VkResult VulkanReplayConsumerBase::OverrideQueueSubmit(PFN_vkQueueSubmit fu
auto* device_info = GetObjectInfoTable().GetVkDeviceInfo(queue_info->parent_id);
GFXRECON_ASSERT(device_info != nullptr);

if (UseExtraDescriptorInfo(device_info) && submit_info_data != nullptr)
if (UseAddressReplacement(device_info) && submit_info_data != nullptr)
{
std::vector<VkDeviceAddress> addresses_to_replace;

Expand Down Expand Up @@ -3758,7 +3758,7 @@ VkResult VulkanReplayConsumerBase::OverrideQueueSubmit2(PFN_vkQueueSubmit2 f
auto* device_info = GetObjectInfoTable().GetVkDeviceInfo(queue_info->parent_id);
GFXRECON_ASSERT(device_info != nullptr);

if (UseExtraDescriptorInfo(device_info) && submit_info_data != nullptr)
if (UseAddressReplacement(device_info) && submit_info_data != nullptr)
{
std::vector<VkDeviceAddress> addresses_to_replace;

Expand Down Expand Up @@ -4358,7 +4358,7 @@ void VulkanReplayConsumerBase::OverrideCmdBindDescriptorSets(PFN_vkCmdBindDescri
VkPipelineLayout pipeline_layout = in_layout->handle;
const VkDescriptorSet* descriptor_sets = pDescriptorSets->GetHandlePointer();

if (!device_info->allocator->SupportsOpaqueDeviceAddresses())
if (UseAddressReplacement(device_info))
{
auto& address_replacer = GetDeviceAddressReplacer(device_info);
address_replacer.ProcessCmdBindDescriptorSets(in_commandBuffer,
Expand Down Expand Up @@ -4392,7 +4392,7 @@ void VulkanReplayConsumerBase::OverrideCmdBindDescriptorSets2(
VkCommandBuffer command_buffer = in_commandBuffer->handle;
VkBindDescriptorSetsInfo* bind_descriptor_sets_info = pBindDescriptorSetsInfo->GetPointer();

if (bind_descriptor_sets_info != nullptr && UseExtraDescriptorInfo(device_info))
if (bind_descriptor_sets_info != nullptr && UseAddressReplacement(device_info))
{
auto* bind_descriptor_sets_info_meta = pBindDescriptorSetsInfo->GetMetaStructPointer();
VkPipelineBindPoint pipelineBindPoint = in_commandBuffer->bound_pipelines.begin()->first;
Expand Down Expand Up @@ -5105,9 +5105,11 @@ VulkanReplayConsumerBase::OverrideCreateBuffer(PFN_vkCreateBuffer

// Check for a buffer device address.
bool uses_address = false;

// when using opaque addresses (-m rebind), force support for VkBufferDeviceAddress to allow sanitizing
bool force_address =
UseExtraDescriptorInfo(device_info) && (replay_create_info->usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT ||
replay_create_info->usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
UseAddressReplacement(device_info) && (replay_create_info->usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT ||
replay_create_info->usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
VkBufferCreateFlags address_create_flags = 0;
VkBufferUsageFlags address_usage_flags = 0;

Expand Down Expand Up @@ -8069,7 +8071,7 @@ void VulkanReplayConsumerBase::OverrideCmdBuildAccelerationStructuresKHR(
VkAccelerationStructureBuildGeometryInfoKHR* build_geometry_infos = pInfos->GetPointer();
VkAccelerationStructureBuildRangeInfoKHR** build_range_infos = ppBuildRangeInfos->GetPointer();

if (!device_info->allocator->SupportsOpaqueDeviceAddresses())
if (UseAddressReplacement(device_info))
{
auto& address_tracker = GetDeviceAddressTracker(device_info);
auto& address_replacer = GetDeviceAddressReplacer(device_info);
Expand Down Expand Up @@ -8788,7 +8790,7 @@ void VulkanReplayConsumerBase::OverrideCmdPushConstants(PFN_vkCmdPushConstants
auto* device_info = GetObjectInfoTable().GetVkDeviceInfo(command_buffer_info->parent_id);
GFXRECON_ASSERT(device_info != nullptr);

if (UseExtraDescriptorInfo(device_info))
if (UseAddressReplacement(device_info))
{
const auto& address_tracker = GetDeviceAddressTracker(device_info);
auto& address_replacer = GetDeviceAddressReplacer(device_info);
Expand Down Expand Up @@ -8920,7 +8922,7 @@ void VulkanReplayConsumerBase::OverrideCmdTraceRaysKHR(
VkStridedDeviceAddressRegionKHR* in_pHitShaderBindingTable = pHitShaderBindingTable->GetPointer();
VkStridedDeviceAddressRegionKHR* in_pCallableShaderBindingTable = pCallableShaderBindingTable->GetPointer();

if (!device_info->allocator->SupportsOpaqueDeviceAddresses())
if (UseAddressReplacement(device_info))
{
// identify buffer(s) by their device-address
const auto& address_tracker = GetDeviceAddressTracker(device_info);
Expand Down Expand Up @@ -9750,7 +9752,12 @@ VulkanAddressReplacer& VulkanReplayConsumerBase::GetDeviceAddressReplacer(const

bool VulkanReplayConsumerBase::UseExtraDescriptorInfo(const VulkanDeviceInfo* device_info) const
{
return options_.dumping_resources || !device_info->allocator->SupportsOpaqueDeviceAddresses();
return options_.dumping_resources || UseAddressReplacement(device_info);
}

bool VulkanReplayConsumerBase::UseAddressReplacement(const VulkanDeviceInfo* device_info) const
{
return !device_info->allocator->SupportsOpaqueDeviceAddresses();
}

void VulkanReplayConsumerBase::Process_vkUpdateDescriptorSetWithTemplate(const ApiCallInfo& call_info,
Expand Down Expand Up @@ -9948,7 +9955,7 @@ void VulkanReplayConsumerBase::ProcessCopyVulkanAccelerationStructuresMetaComman
VulkanDeviceInfo* device_info = GetObjectInfoTable().GetVkDeviceInfo(device);
GFXRECON_ASSERT(device_info != nullptr);

if (!device_info->allocator->SupportsOpaqueDeviceAddresses())
if (UseAddressReplacement(device_info))
{
MapStructArrayHandles(copy_infos->GetMetaStructPointer(), copy_infos->GetLength(), GetObjectInfoTable());

Expand All @@ -9971,7 +9978,7 @@ void VulkanReplayConsumerBase::ProcessBuildVulkanAccelerationStructuresMetaComma
VulkanDeviceInfo* device_info = GetObjectInfoTable().GetVkDeviceInfo(device);
GFXRECON_ASSERT(device_info != nullptr);

if (!device_info->allocator->SupportsOpaqueDeviceAddresses())
if (UseAddressReplacement(device_info))
{
MapStructArrayHandles(pInfos->GetMetaStructPointer(), pInfos->GetLength(), GetObjectInfoTable());

Expand All @@ -9993,7 +10000,7 @@ void VulkanReplayConsumerBase::ProcessVulkanAccelerationStructuresWritePropertie
VulkanDeviceInfo* device_info = GetObjectInfoTable().GetVkDeviceInfo(device_id);
GFXRECON_ASSERT(device_info != nullptr);

if (!device_info->allocator->SupportsOpaqueDeviceAddresses())
if (UseAddressReplacement(device_info))
{
VkAccelerationStructureKHR acceleration_structure = MapHandle<VulkanAccelerationStructureKHRInfo>(
acceleration_structure_id, &VulkanObjectInfoTable::GetVkAccelerationStructureKHRInfo);
Expand Down Expand Up @@ -10258,22 +10265,13 @@ VkResult VulkanReplayConsumerBase::OverrideCreateGraphicsPipelines(
// Information is stored in the created PipelineInfos only when the dumping resources feature is in use
if (replay_result == VK_SUCCESS)
{
// populate all VulkanPipelineInfo structs with information related to shader-modules
graphics::populate_shader_stages(pCreateInfos, pPipelines, GetObjectInfoTable());

if (options_.dumping_resources)
{
resource_dumper_->DumpGraphicsPipelineInfos(pCreateInfos, create_info_count, pPipelines);
}

std::vector<VulkanPipelineInfo*> pipeline_infos(pPipelines->GetLength());
for (uint32_t i = 0; i < pPipelines->GetLength(); ++i)
{
pipeline_infos[i] = reinterpret_cast<VulkanPipelineInfo*>(pPipelines->GetConsumerData(i));
}

// check potentially inlined spirv
graphics::vulkan_check_buffer_references(maybe_replaced_create_infos, create_info_count, pipeline_infos.data());
// populate all VulkanPipelineInfo structs with information related to shader-modules
graphics::populate_shader_stages(pCreateInfos, pPipelines, GetObjectInfoTable());
}
return replay_result;
}
Expand Down Expand Up @@ -10328,15 +10326,6 @@ VkResult VulkanReplayConsumerBase::OverrideCreateComputePipelines(
{
// populate all VulkanPipelineInfo structs with information related to shader-modules
graphics::populate_shader_stages(pCreateInfos, pPipelines, GetObjectInfoTable());

std::vector<VulkanPipelineInfo*> pipeline_infos(pPipelines->GetLength());
for (uint32_t i = 0; i < pPipelines->GetLength(); ++i)
{
pipeline_infos[i] = reinterpret_cast<VulkanPipelineInfo*>(pPipelines->GetConsumerData(i));
}

// check potentially inlined spirv
graphics::vulkan_check_buffer_references(in_p_create_infos, create_info_count, pipeline_infos.data());
}
return replay_result;
}
Expand Down Expand Up @@ -10592,13 +10581,6 @@ std::function<decode::handle_create_result_t<VkPipeline>()> VulkanReplayConsumer
}
TrackAsyncHandles(handle_deps, sync_fn);

// assemble array of info-structs
std::vector<VulkanPipelineInfo*> pipeline_infos(pPipelines->GetLength());
for (uint32_t i = 0; i < pPipelines->GetLength(); ++i)
{
pipeline_infos[i] = reinterpret_cast<VulkanPipelineInfo*>(pPipelines->GetConsumerData(i));
}

// define pipeline-creation task, assert object-lifetimes by copying/moving into closure
auto task = [this,
device_handle,
Expand All @@ -10610,8 +10592,7 @@ std::function<decode::handle_create_result_t<VkPipeline>()> VulkanReplayConsumer
createInfoCount,
create_info_data = std::move(create_info_data),
handle_deps = std::move(handle_deps),
pipelines = std::move(pipelines),
pipeline_infos = std::move(pipeline_infos)]() mutable -> handle_create_result_t<VkPipeline> {
pipelines = std::move(pipelines)]() mutable -> handle_create_result_t<VkPipeline> {
std::vector<VkPipeline> out_pipelines(createInfoCount);
auto create_infos = reinterpret_cast<VkGraphicsPipelineCreateInfo*>(create_info_data.data());

Expand All @@ -10625,11 +10606,6 @@ std::function<decode::handle_create_result_t<VkPipeline>()> VulkanReplayConsumer
device_handle, pipeline_cache_handle, createInfoCount, create_infos, in_pAllocator, out_pipelines.data());
CheckResult("vkCreateGraphicsPipelines", returnValue, replay_result, call_info);

if (replay_result == VK_SUCCESS)
{
// check potentially inlined spirv
graphics::vulkan_check_buffer_references(create_infos, createInfoCount, pipeline_infos.data());
}
// schedule dependency-clear on main-thread
MainThreadQueue().post([this, handle_deps = std::move(handle_deps)] { ClearAsyncHandles(handle_deps); });
return { replay_result, std::move(out_pipelines) };
Expand Down Expand Up @@ -10679,13 +10655,6 @@ std::function<handle_create_result_t<VkPipeline>()> VulkanReplayConsumerBase::As
}
TrackAsyncHandles(handle_deps, sync_fn);

// assemble array of info-structs
std::vector<VulkanPipelineInfo*> pipeline_infos(pPipelines->GetLength());
for (uint32_t i = 0; i < pPipelines->GetLength(); ++i)
{
pipeline_infos[i] = reinterpret_cast<VulkanPipelineInfo*>(pPipelines->GetConsumerData(i));
}

// define pipeline-creation task, assert object-lifetimes by copying/moving into closure
auto task = [this,
device_handle,
Expand All @@ -10696,19 +10665,13 @@ std::function<handle_create_result_t<VkPipeline>()> VulkanReplayConsumerBase::As
in_pAllocator,
createInfoCount,
create_info_data = std::move(create_info_data),
handle_deps = std::move(handle_deps),
pipeline_infos = std::move(pipeline_infos)]() mutable -> handle_create_result_t<VkPipeline> {
handle_deps = std::move(handle_deps)]() mutable -> handle_create_result_t<VkPipeline> {
std::vector<VkPipeline> out_pipelines(createInfoCount);
auto create_infos = reinterpret_cast<const VkComputePipelineCreateInfo*>(create_info_data.data());
VkResult replay_result = func(
device_handle, pipeline_cache_handle, createInfoCount, create_infos, in_pAllocator, out_pipelines.data());
CheckResult("vkCreateComputePipelines", returnValue, replay_result, call_info);

if (replay_result == VK_SUCCESS)
{
// check potentially inlined spirv
graphics::vulkan_check_buffer_references(create_infos, createInfoCount, pipeline_infos.data());
}
// schedule dependency-clear on main-thread
MainThreadQueue().post([this, handle_deps = std::move(handle_deps)] { ClearAsyncHandles(handle_deps); });
return { replay_result, std::move(out_pipelines) };
Expand Down
11 changes: 11 additions & 0 deletions framework/decode/vulkan_replay_consumer_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,17 @@ class VulkanReplayConsumerBase : public VulkanConsumer
*/
bool UseExtraDescriptorInfo(const VulkanDeviceInfo* device_info) const;

/**
* @brief UseAddressReplacement returns true if address-sanitizing for various resources
* like buffer-device-addresses, shader-binding-tables, acceleration-structures, etc. is used.
*
* This is generally the case when opaque addresses cannot be used or when replaying on another device.
*
* @param device_info a device info struct
* @return true if address-replacement features will be used.
*/
bool UseAddressReplacement(const VulkanDeviceInfo* device_info) const;

[[nodiscard]] std::vector<std::unique_ptr<char[]>> ReplaceShaders(uint32_t create_info_count,
VkGraphicsPipelineCreateInfo* create_infos,
const format::HandleId* pipelines) const;
Expand Down
46 changes: 11 additions & 35 deletions framework/graphics/vulkan_check_buffer_references.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ void populate_shader_stages(const decode::StructPointerDecoder<T>* pCreateInf
module_info->buffer_reference_infos.begin(),
module_info->buffer_reference_infos.end());
}

// check potentially inlined spirv
if (auto module_create_info =
vulkan_struct_get_pnext<VkShaderModuleCreateInfo>(pCreateInfos->GetPointer()->pStages + s))
{
graphics::vulkan_check_buffer_references(
module_create_info->pCode, module_create_info->codeSize, pipeline_info);
}
}
}
}
Expand Down Expand Up @@ -110,49 +118,17 @@ void populate_shader_stages(
pipeline_info->buffer_reference_infos.insert(pipeline_info->buffer_reference_infos.end(),
module_info->buffer_reference_infos.begin(),
module_info->buffer_reference_infos.end());
}
}
}

template <>
void vulkan_check_buffer_references(const VkGraphicsPipelineCreateInfo* create_infos,
uint32_t create_info_count,
decode::VulkanPipelineInfo** out_info_structs)
{
GFXRECON_ASSERT(out_info_structs != nullptr);
gfxrecon::util::SpirVParsingUtil spirv_util;

for (uint32_t i = 0; i < create_info_count; ++i)
{
for (uint32_t j = 0; j < create_infos[i].stageCount; ++j)
{
// check potentially inlined spirv
if (auto module_create_info =
vulkan_struct_get_pnext<VkShaderModuleCreateInfo>(create_infos[i].pStages + j))
vulkan_struct_get_pnext<VkShaderModuleCreateInfo>(&pCreateInfos->GetPointer()->stage))
{
graphics::vulkan_check_buffer_references(
module_create_info->pCode, module_create_info->codeSize, out_info_structs[i]);
module_create_info->pCode, module_create_info->codeSize, pipeline_info);
}
}
}
}

template <>
void vulkan_check_buffer_references(const VkComputePipelineCreateInfo* create_infos,
uint32_t create_info_count,
decode::VulkanPipelineInfo** out_info_structs)
{
GFXRECON_ASSERT(out_info_structs != nullptr);
gfxrecon::util::SpirVParsingUtil spirv_util;

for (uint32_t i = 0; i < create_info_count; ++i)
{
if (auto module_create_info = vulkan_struct_get_pnext<VkShaderModuleCreateInfo>(&create_infos[i].stage))
{
graphics::vulkan_check_buffer_references(
module_create_info->pCode, module_create_info->codeSize, out_info_structs[i]);
}
}
}

GFXRECON_END_NAMESPACE(graphics)
GFXRECON_END_NAMESPACE(gfxrecon)
28 changes: 0 additions & 28 deletions framework/graphics/vulkan_check_buffer_references.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,34 +75,6 @@ static void vulkan_check_buffer_references(const uint32_t* const spirv_code, siz
}
}

/**
* @brief vulkan_check_buffer_references is a helper-function to search and check inlined SPIRV-bytecode
* in an array of create-infos.
*
* Inlined SPIRV: VkPipelineShaderStageCreateInfo 'can' provide a VkShaderModuleCreateInfo
* This function will iterate all contained 'VkPipelineShaderStageCreateInfo' and descend their pNext-chains.
* If any 'VkShaderModuleCreateInfo' are contained in the pNext-chains, the contained spirv-code will be checked using:
* vulkan_check_buffer_references(spirv_code, num_bytes).
*
* @tparam T structure-type
* @param create_infos an array of pipeline/shader create-info structures.
* @param create_info_count create-infos' array-count
*/
template <typename T>
void vulkan_check_buffer_references(const T* create_infos,
uint32_t create_info_count,
decode::VulkanPipelineInfo** out_info_structs) = delete;

template <>
void vulkan_check_buffer_references(const VkGraphicsPipelineCreateInfo* create_infos,
uint32_t create_info_count,
decode::VulkanPipelineInfo** out_info_structs);

template <>
void vulkan_check_buffer_references(const VkComputePipelineCreateInfo* create_infos,
uint32_t create_info_count,
decode::VulkanPipelineInfo** out_info_structs);

GFXRECON_END_NAMESPACE(graphics)
GFXRECON_END_NAMESPACE(gfxrecon)

Expand Down

0 comments on commit c6e75ed

Please sign in to comment.