Skip to content

Commit

Permalink
Add initial opaque memory support
Browse files Browse the repository at this point in the history
Initial pass at attempting to implement opaque memory support.
Looking for example to test against, but for now it compiles and
doesn't interfere with the other tests that are run.
  • Loading branch information
MarkY-LunarG committed Dec 28, 2023
1 parent bba7aa9 commit 92612b3
Show file tree
Hide file tree
Showing 17 changed files with 1,357 additions and 502 deletions.
403 changes: 366 additions & 37 deletions framework/decode/vulkan_cpp_consumer_base.cpp

Large diffs are not rendered by default.

78 changes: 46 additions & 32 deletions framework/decode/vulkan_cpp_consumer_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "vulkan/vulkan.h"

#include <cstdio>
#include <map>
#include <string>
#include <sstream>
#include <type_traits>
Expand All @@ -51,6 +50,13 @@ struct DescriptorUpdateTemplateEntries
std::vector<VkDescriptorUpdateTemplateEntry> accelerations;
};

// Track items that are specific to a given device
struct DeviceInfo
{
format::HandleId parent{ 0 };
std::unordered_map<format::HandleId, uint64_t> opaque_addresses;
};

class VulkanCppConsumerBase : public VulkanConsumer
{
public:
Expand Down Expand Up @@ -142,6 +148,12 @@ class VulkanCppConsumerBase : public VulkanConsumer
PointerDecoder<uint32_t>* pPhysicalDeviceCount,
HandlePointerDecoder<VkPhysicalDevice>* pPhysicalDevices);

void Generate_vkCreateSwapchainKHR(VkResult returnValue,
format::HandleId device,
StructPointerDecoder<Decoded_VkSwapchainCreateInfoKHR>* pCreateInfo,
StructPointerDecoder<Decoded_VkAllocationCallbacks>* pAllocator,
HandlePointerDecoder<VkSwapchainKHR>* pSwapchain);

void Generate_vkGetSwapchainImagesKHR(VkResult returnValue,
format::HandleId device,
format::HandleId swapchain,
Expand Down Expand Up @@ -171,6 +183,13 @@ class VulkanCppConsumerBase : public VulkanConsumer
uint32_t queueFamilyIndex,
uint64_t connection,
uint32_t visual_id);
void Generate_vkCreateDevice(VkResult returnValue,
format::HandleId physicalDevice,
StructPointerDecoder<Decoded_VkDeviceCreateInfo>* pCreateInfo,
StructPointerDecoder<Decoded_VkAllocationCallbacks>* pAllocator,
HandlePointerDecoder<VkDevice>* pDevice);
void Generate_vkDestroyDevice(format::HandleId device,
StructPointerDecoder<Decoded_VkAllocationCallbacks>* pAllocator);

void Generate_vkGetImageMemoryRequirements(format::HandleId device,
format::HandleId image,
Expand Down Expand Up @@ -222,6 +241,11 @@ class VulkanCppConsumerBase : public VulkanConsumer
StructPointerDecoder<Decoded_VkMemoryAllocateInfo>* pAllocateInfo,
StructPointerDecoder<Decoded_VkAllocationCallbacks>* pAllocator,
HandlePointerDecoder<VkDeviceMemory>* pMemory);
void Generate_vkCreateBuffer(VkResult returnValue,
format::HandleId device,
StructPointerDecoder<Decoded_VkBufferCreateInfo>* pCreateInfo,
StructPointerDecoder<Decoded_VkAllocationCallbacks>* pAllocator,
HandlePointerDecoder<VkBuffer>* pBuffer);

void Generate_vkCreateInstance(VkResult returnValue,
StructPointerDecoder<Decoded_VkInstanceCreateInfo>* pCreateInfo,
Expand Down Expand Up @@ -444,18 +468,6 @@ class VulkanCppConsumerBase : public VulkanConsumer
StructPointerDecoder<Decoded_VkPresentInfoKHR>* pPresentInfo);

// Intercept commands that perform additional work prior to the standard code generation
void Intercept_vkCreateDevice(VkResult returnValue,
format::HandleId physicalDevice,
StructPointerDecoder<Decoded_VkDeviceCreateInfo>* pCreateInfo,
StructPointerDecoder<Decoded_VkAllocationCallbacks>* pAllocator,
HandlePointerDecoder<VkDevice>* pDevice);

void Intercept_vkCreateSwapchainKHR(VkResult returnValue,
format::HandleId device,
StructPointerDecoder<Decoded_VkSwapchainCreateInfoKHR>* pCreateInfo,
StructPointerDecoder<Decoded_VkAllocationCallbacks>* pAllocator,
HandlePointerDecoder<VkSwapchainKHR>* pSwapchain);

void Intercept_vkCreateFramebuffer(VkResult returnValue,
format::HandleId device,
StructPointerDecoder<Decoded_VkFramebufferCreateInfo>* pCreateInfo,
Expand Down Expand Up @@ -591,10 +603,11 @@ class VulkanCppConsumerBase : public VulkanConsumer
uint32_t layers,
const std::vector<format::HardwareBufferPlaneInfo>& plane_info) override;
virtual void ProcessDestroyHardwareBufferCommand(uint64_t buffer_id) override;
virtual void
ProcessSetOpaqueAddressCommand(format::HandleId device_id, format::HandleId object_id, uint64_t address) override;

protected:
FILE* GetFrameFile();
FILE* GetHeaderFile() const { return header_file_; };
FILE* GetGlobalFile() const { return global_file_; };

std::string GenFrameName(uint32_t frameNumber, uint32_t frameSplitNumber, uint32_t fillLength);
Expand Down Expand Up @@ -625,25 +638,26 @@ class VulkanCppConsumerBase : public VulkanConsumer
std::string buffer_name;
};

std::unordered_map<VkObjectType, uint32_t> counters_;
VulkanCppResourceTracker* resource_tracker_;
VulkanCppLoaderGenerator pfn_loader_;
std::map<format::HandleId, std::string> handle_id_map_;
std::vector<std::string> func_data_;
std::map<uint64_t, std::string> memory_id_map_;
std::map<uint64_t, VulkanCppAndroidBufferInfo> android_buffer_id_map_;
std::map<uint64_t, VulkanCppAndroidMemoryInfo> android_memory_id_map_;
std::map<format::HandleId, std::queue<std::string>> next_image_map_;
std::map<void*, std::string> ptr_map_;
std::map<uint64_t, std::string> struct_map_; // hash -> name
uint32_t window_width_;
uint32_t window_height_;
uint32_t max_command_limit_{ 1000 };
std::vector<GfxToCppVariable> variable_data_;
std::vector<format::HandleId> imported_semaphores_;
std::map<format::HandleId, DescriptorUpdateTemplateEntries> descriptor_update_template_entry_map_;
std::unordered_map<VkObjectType, uint32_t> counters_;
VulkanCppResourceTracker* resource_tracker_;
VulkanCppLoaderGenerator pfn_loader_;
std::unordered_map<format::HandleId, std::string> handle_id_map_;
std::unordered_map<format::HandleId, DeviceInfo*> device_info_map_;
std::vector<std::string> func_data_;
std::unordered_map<uint64_t, std::string> memory_id_map_;
std::unordered_map<uint64_t, VulkanCppAndroidBufferInfo> android_buffer_id_map_;
std::unordered_map<uint64_t, VulkanCppAndroidMemoryInfo> android_memory_id_map_;
std::unordered_map<format::HandleId, std::queue<std::string>> next_image_map_;
std::unordered_map<void*, std::string> ptr_map_;
std::unordered_map<uint64_t, std::string> struct_map_; // hash -> name
uint32_t window_width_;
uint32_t window_height_;
uint32_t max_command_limit_{ 1000 };
std::vector<GfxToCppVariable> variable_data_;
std::vector<format::HandleId> imported_semaphores_;
std::unordered_map<format::HandleId, DescriptorUpdateTemplateEntries> descriptor_update_template_entry_map_;
std::map<format::HandleId, std::queue<std::pair<format::HandleId, VkDeviceSize>>> memory_resource_map_;
std::map<format::HandleId, std::string> resource_memory_req_map_;
std::unordered_map<format::HandleId, std::string> resource_memory_req_map_;

bool needs_debug_util_callback_ = false;

Expand Down
154 changes: 154 additions & 0 deletions framework/decode/vulkan_cpp_structs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,87 @@ std::string GenerateStruct_VkBindSparseInfo(std::ostream&
return variable_name;
}

std::string GenerateStruct_VkSwapchainCreateInfoKHR(std::ostream& out,
const VkSwapchainCreateInfoKHR* structInfo,
Decoded_VkSwapchainCreateInfoKHR* metaInfo,
VulkanCppConsumerBase& consumer)
{
std::stringstream struct_body;
std::string pnext_name = GenerateExtension(out, structInfo->pNext, metaInfo->pNext, consumer);
std::string image_extent_info_var =
GenerateStruct_VkExtent2D(out, &structInfo->imageExtent, metaInfo->imageExtent, consumer);
std::string pqueue_family_indices_array = "NULL";
if (structInfo->pQueueFamilyIndices != NULL)
{
pqueue_family_indices_array = "pQueueFamilyIndices_" + std::to_string(consumer.GetNextId());
out << "\t\t"
<< "uint32_t " << pqueue_family_indices_array << "[] = "
<< VulkanCppConsumerBase::BuildValue(structInfo->pQueueFamilyIndices, structInfo->queueFamilyIndexCount)
<< ";" << std::endl;
}
// sType
struct_body << "\t"
<< "VkStructureType(" << structInfo->sType << ")"
<< "," << std::endl;
// pNext
struct_body << "\t\t\t" << pnext_name << "," << std::endl;
// flags
struct_body << "\t\t\t"
<< "VkSwapchainCreateFlagsKHR(" << structInfo->flags << ")"
<< "," << std::endl;
// surface
struct_body << "\t\t\t" << consumer.GetHandle(metaInfo->surface) << "," << std::endl;
// minImageCount
struct_body << "\t\t\t3,\n"; // structInfo->minImageCount << "," << std::endl;
// imageFormat
struct_body << "\t\t\t"
<< "VkFormat(" << structInfo->imageFormat << ")"
<< "," << std::endl;
// imageColorSpace
struct_body << "\t\t\t"
<< "VkColorSpaceKHR(" << structInfo->imageColorSpace << ")"
<< "," << std::endl;
// imageExtent
struct_body << "\t\t\t" << image_extent_info_var << "," << std::endl;
// imageArrayLayers
struct_body << "\t\t\t" << structInfo->imageArrayLayers << "," << std::endl;
// imageUsage
struct_body << "\t\t\t"
<< "VkImageUsageFlags(" << structInfo->imageUsage << ")"
<< "," << std::endl;
// imageSharingMode
struct_body << "\t\t\t"
<< "VkSharingMode(" << structInfo->imageSharingMode << ")"
<< "," << std::endl;
// queueFamilyIndexCount
struct_body << "\t\t\t" << structInfo->queueFamilyIndexCount << "," << std::endl;
// pQueueFamilyIndices
struct_body << "\t\t\t" << pqueue_family_indices_array << "," << std::endl;
// preTransform
struct_body << "\t\t\t"
<< "VkSurfaceTransformFlagBitsKHR(" << structInfo->preTransform << ")"
<< "," << std::endl;
// compositeAlpha
struct_body << "\t\t\t"
<< "VkCompositeAlphaFlagBitsKHR(" << structInfo->compositeAlpha << ")"
<< "," << std::endl;
// presentMode
struct_body << "\t\t\t"
<< "VkPresentModeKHR(" << structInfo->presentMode << ")"
<< "," << std::endl;
// clipped
struct_body << "\t\t\t" << structInfo->clipped << "," << std::endl;
// oldSwapchain
struct_body << "\t\t\t" << consumer.GetHandle(metaInfo->oldSwapchain) << ",";
std::string variable_name = consumer.AddStruct(struct_body, "swapchainCreateInfoKHR");
out << "\t\t"
<< "VkSwapchainCreateInfoKHR " << variable_name << " {" << std::endl;
out << "\t\t" << struct_body.str() << std::endl;
out << "\t\t"
<< "};" << std::endl;
return variable_name;
}

std::string GenerateStruct_VkPresentInfoKHR(std::ostream& out,
const VkPresentInfoKHR* structInfo,
Decoded_VkPresentInfoKHR* metaInfo,
Expand Down Expand Up @@ -1056,5 +1137,78 @@ GenerateStruct_VkImportAndroidHardwareBufferInfoANDROID(std::ostream&
return variable_name;
}

std::string GenerateStruct_VkMemoryAllocateFlagsInfo(std::ostream& out,
const VkMemoryAllocateFlagsInfo* structInfo,
Decoded_VkMemoryAllocateFlagsInfo* metaInfo,
VulkanCppConsumerBase& consumer)
{
std::stringstream struct_body;
std::string pnext_name = GenerateExtension(out, structInfo->pNext, metaInfo->pNext, consumer);
// sType
struct_body << "\t"
<< "VkStructureType(" << structInfo->sType << ")"
<< "," << std::endl;
// pNext
struct_body << "\t\t\t" << pnext_name << "," << std::endl;
// flags
struct_body << "\t\t\t"
<< "VkMemoryAllocateFlags(" << structInfo->flags << ")"
<< "," << std::endl;
// deviceMask
struct_body << "\t\t\t" << structInfo->deviceMask << ",";
std::string variable_name = consumer.AddStruct(struct_body, "memoryAllocateFlagsInfo");
out << "\t\t"
<< "VkMemoryAllocateFlagsInfo " << variable_name << " {" << std::endl;
out << "\t\t" << struct_body.str() << std::endl;
out << "\t\t"
<< "};" << std::endl;

// If we're using opaque addresses and this memory allocation we're on right now is
// using an opaque address, we need to add a flag to indicate we're using one.
out << "\t\tif (can_use_opaque_address) {" << std::endl;
out << "\t\t\tuses_opaque_address = true;" << std::endl;
out << "\t\t\t" << variable_name << ".flags |= VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_CAPTURE_REPLAY_BIT;" << std::endl;
out << "\t\t}" << std::endl;

return variable_name;
}

std::string GenerateStruct_VkImportMemoryHostPointerInfoEXT(std::ostream& out,
const VkImportMemoryHostPointerInfoEXT* structInfo,
Decoded_VkImportMemoryHostPointerInfoEXT* metaInfo,
VulkanCppConsumerBase& consumer)
{
std::stringstream struct_body;
std::string pnext_name = GenerateExtension(out, structInfo->pNext, metaInfo->pNext, consumer);
// sType
struct_body << "\t"
<< "VkStructureType(" << structInfo->sType << ")"
<< "," << std::endl;
// pNext
struct_body << "\t\t\t" << pnext_name << "," << std::endl;
// handleType
struct_body << "\t\t\t"
<< "VkExternalMemoryHandleTypeFlagBits(" << structInfo->handleType << ")"
<< "," << std::endl;
// pHostPointer
out << "\t\t"
<< "// TODO: Support pHostPointer (output?) argument." << std::endl;
std::string variable_name = consumer.AddStruct(struct_body, "importMemoryHostPointerInfoEXT");
out << "\t\t"
<< "VkImportMemoryHostPointerInfoEXT " << variable_name << " {" << std::endl;
out << "\t\t" << struct_body.str() << std::endl;
out << "\t\t"
<< "};" << std::endl;

// If we're using opaque addresses and this memory allocation we're on right now is
// using an opaque address, we need to flag this as importing memory as well so we
// set the proper address.
out << "\t\tif (can_use_opaque_address) {" << std::endl;
out << "\t\t\timports_memory = true;" << std::endl;
out << "\t\t}" << std::endl;

return variable_name;
}

GFXRECON_END_NAMESPACE(gfxrecon)
GFXRECON_END_NAMESPACE(decode)
15 changes: 15 additions & 0 deletions framework/decode/vulkan_cpp_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ std::string GenerateStruct_VkBindSparseInfo(std::ostream&
const std::vector<format::HandleId>& imported_semaphores,
VulkanCppConsumerBase& consumer);

std::string GenerateStruct_VkSwapchainCreateInfoKHR(std::ostream& out,
const VkSwapchainCreateInfoKHR* structInfo,
Decoded_VkSwapchainCreateInfoKHR* metaInfo,
VulkanCppConsumerBase& consumer);

std::string GenerateStruct_VkPresentInfoKHR(std::ostream& out,
const VkPresentInfoKHR* structInfo,
Decoded_VkPresentInfoKHR* metainfo,
Expand Down Expand Up @@ -112,6 +117,16 @@ GenerateStruct_VkImportAndroidHardwareBufferInfoANDROID(std::ostream&
Decoded_VkImportAndroidHardwareBufferInfoANDROID* metaInfo,
VulkanCppConsumerBase& consumer);

std::string GenerateStruct_VkMemoryAllocateFlagsInfo(std::ostream& out,
const VkMemoryAllocateFlagsInfo* structInfo,
Decoded_VkMemoryAllocateFlagsInfo* metaInfo,
VulkanCppConsumerBase& consumer);

std::string GenerateStruct_VkImportMemoryHostPointerInfoEXT(std::ostream& out,
const VkImportMemoryHostPointerInfoEXT* structInfo,
Decoded_VkImportMemoryHostPointerInfoEXT* metaInfo,
VulkanCppConsumerBase& consumer);

GFXRECON_END_NAMESPACE(gfxrecon)
GFXRECON_END_NAMESPACE(decode)

Expand Down
Loading

0 comments on commit 92612b3

Please sign in to comment.