diff --git a/.clang-format b/.clang-format index 1328892f6a..0a8efd1a57 100644 --- a/.clang-format +++ b/.clang-format @@ -6,7 +6,6 @@ AlignEscapedNewlines: true AlignOperands: true AlignTrailingComments: true AllowAllParametersOfDeclarationOnNextLine: true -AllowShortFunctionsOnASingleLine: true AllowShortCaseLabelsOnASingleLine: false AllowShortFunctionsOnASingleLine: Inline AllowShortIfStatementsOnASingleLine: false diff --git a/external/Tocpp-Android-Template/app/CMakeLists.txt b/external/Tocpp-Android-Template/app/CMakeLists.txt index e1ac382af2..8eddd6a43e 100644 --- a/external/Tocpp-Android-Template/app/CMakeLists.txt +++ b/external/Tocpp-Android-Template/app/CMakeLists.txt @@ -36,6 +36,8 @@ set(GENERATED_FRAMES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/main/assets/src) # Base lib add_library(${LIBNAME}-base SHARED ${JNI_DIR}/ScreenOrientation.cpp + ${GENERATED_FRAMES_DIR}/swapchain_common.cpp + ${GENERATED_FRAMES_DIR}/swapchain_common.h ${GENERATED_FRAMES_DIR}/global_var.cpp ${GENERATED_FRAMES_DIR}/global_var.h ${GENERATED_FRAMES_DIR}/loader.cpp diff --git a/framework/decode/vulkan_cpp_consumer_base.cpp b/framework/decode/vulkan_cpp_consumer_base.cpp index 218f992f7f..1f1cc71063 100644 --- a/framework/decode/vulkan_cpp_consumer_base.cpp +++ b/framework/decode/vulkan_cpp_consumer_base.cpp @@ -45,7 +45,7 @@ const std::string GfxToCppPlatformToString(GfxToCppPlatform platform); bool GfxToCppPlatformIsValid(const GfxToCppPlatform& platform); VulkanCppConsumerBase::VulkanCppConsumerBase() : - frame_file_(nullptr), global_file_(nullptr), header_file_(nullptr), main_file_(nullptr), pfn_loader_() + frame_file_(nullptr), global_file_(nullptr), main_file_(nullptr), pfn_loader_() { counters_[VK_OBJECT_TYPE_UNKNOWN] = 0; } @@ -132,71 +132,70 @@ void VulkanCppConsumerBase::WriteMainFooter() } } -void VulkanCppConsumerBase::WriteGlobalHeaderFile() +bool VulkanCppConsumerBase::WriteGlobalHeaderFile() { - switch (platform_) + FILE* header_file = nullptr; + std::string filename = util::filepath::Join(out_dir_, src_out_dir_ + "/global_var.h"); + int32_t result = util::platform::FileOpen(&header_file, filename.c_str(), "w"); + if (result == 0) { - case GfxToCppPlatform::PLATFORM_ANDROID: - fprintf(header_file_, - "%s%s%s%s", - sAndroidOutputHeadersPlatform, - sCommonHeaderOutputHeaders, - sAndroidOutputHeader, - sCommonOutputHeaderFunctions); - break; - case GfxToCppPlatform::PLATFORM_WIN32: - fprintf(header_file_, - "%s%s%s%s", - sWin32OutputHeadersPlatform, - sCommonHeaderOutputHeaders, - sWin32OutputHeader, - sCommonOutputHeaderFunctions); - break; - case GfxToCppPlatform::PLATFORM_XCB: - fprintf(header_file_, - "%s%s%s%s", - sXcbOutputHeadersPlatform, - sCommonHeaderOutputHeaders, - sXcbOutputHeader, - sCommonOutputHeaderFunctions); - break; - case GfxToCppPlatform::PLATFORM_COUNT: - default: + switch (platform_) { - fprintf(header_file_, - "// Nothing to generate for unknown platform: %s\n", - GfxToCppPlatformToString(platform_).c_str()); - assert(false); - break; + case GfxToCppPlatform::PLATFORM_ANDROID: + fprintf(header_file, + "%s%s%s%s", + sAndroidOutputHeadersPlatform, + sCommonHeaderOutputHeaders, + sAndroidOutputHeader, + sCommonOutputHeaderFunctions); + break; + case GfxToCppPlatform::PLATFORM_WIN32: + fprintf(header_file, + "%s%s%s%s", + sWin32OutputHeadersPlatform, + sCommonHeaderOutputHeaders, + sWin32OutputHeader, + sCommonOutputHeaderFunctions); + break; + case GfxToCppPlatform::PLATFORM_XCB: + fprintf(header_file, + "%s%s%s%s", + sXcbOutputHeadersPlatform, + sCommonHeaderOutputHeaders, + sXcbOutputHeader, + sCommonOutputHeaderFunctions); + break; + case GfxToCppPlatform::PLATFORM_COUNT: + default: + { + fprintf(header_file, + "// Nothing to generate for unknown platform: %s\n", + GfxToCppPlatformToString(platform_).c_str()); + assert(false); + break; + } } - } - PrintToFile(header_file_, "extern %s;\n", GfxToCppVariable::GenerateStringVec(variable_data_)); + PrintToFile(header_file, "extern %s;\n", GfxToCppVariable::GenerateStringVec(variable_data_)); + + PrintToFile(header_file, "%s", func_data_); - PrintToFile(header_file_, "%s", func_data_); + if (needs_debug_util_callback_) + { + fprintf(header_file, "VkBool32 vulkanCppDebugUtilsCallback(\n"); + fprintf(header_file, " VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,\n"); + fprintf(header_file, " VkDebugUtilsMessageTypeFlagsEXT messageTypes,\n"); + fprintf(header_file, " const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,\n"); + fprintf(header_file, " void* pUserData);\n"); + } - if (needs_debug_util_callback_) + util::platform::FileClose(header_file); + } + else { - fprintf(header_file_, "VkBool32 vulkanCppDebugUtilsCallback(\n"); - fprintf(header_file_, " VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,\n"); - fprintf(header_file_, " VkDebugUtilsMessageTypeFlagsEXT messageTypes,\n"); - fprintf(header_file_, " const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,\n"); - fprintf(header_file_, " void* pUserData);\n"); + fprintf(stderr, "Error while opening file: %s\n", filename.c_str()); } - - fprintf(header_file_, "struct DeviceFeatures {\n"); - fprintf(header_file_, " VkPhysicalDeviceFeatures features_1_0;\n"); - fprintf(header_file_, " VkPhysicalDeviceVulkan11Features features_1_1;\n"); - fprintf(header_file_, " VkPhysicalDeviceVulkan12Features features_1_2;\n"); - fprintf(header_file_, " VkPhysicalDeviceBufferDeviceAddressFeatures features_dev_buf_addr;\n"); - fprintf(header_file_, "#ifndef VK_USE_PLATFORM_ANDROID_KHR\n"); - fprintf(header_file_, " VkPhysicalDeviceAccelerationStructureFeaturesKHR features_accel_struct;\n"); - fprintf(header_file_, " VkPhysicalDeviceRayTracingPipelineFeaturesKHR features_ray_trace_pipeline;\n"); - fprintf(header_file_, "#endif\n"); - fprintf(header_file_, "};\n"); - fprintf(header_file_, "extern std::unordered_map device_features;\n"); - - util::platform::FileClose(header_file_); + return result; } void VulkanCppConsumerBase::PrintOutCMakeFile() @@ -248,9 +247,6 @@ void VulkanCppConsumerBase::PrintOutGlobalVar() } } - fprintf(global_file, "\n"); - fprintf(global_file, "std::unordered_map device_features;\n"); - fprintf(global_file, "VkMemoryType originalMemoryTypes[%" PRId64 "][%" PRId64 "] = {\n", original_memory_types_.size(), @@ -352,6 +348,41 @@ void VulkanCppConsumerBase::PrintOutGlobalVar() } } +bool VulkanCppConsumerBase::WriteSwapchainFiles() +{ + FILE* header_file = nullptr; + FILE* source_file = nullptr; + std::string header_filename = util::filepath::Join(out_dir_, src_out_dir_ + "/swapchain_common.h"); + std::string source_filename = util::filepath::Join(out_dir_, src_out_dir_ + "/swapchain_common.cpp"); + + int32_t result = util::platform::FileOpen(&header_file, header_filename.c_str(), "w"); + if (result == 0) + { + fputs(sSwapchainHeaderCode, header_file); + + util::platform::FileClose(header_file); + + result = util::platform::FileOpen(&source_file, source_filename.c_str(), "w"); + if (result == 0) + { + FILE* global_file = GetGlobalFile(); + fputs(sCommonHeaderOutputHeaders, source_file); + fputs(sSwapchainSourceCode, source_file); + + util::platform::FileClose(source_file); + } + else + { + fprintf(stderr, "Error while opening swapchain source file: %s\n", source_filename.c_str()); + } + } + else + { + fprintf(stderr, "Error while opening swapchain header file: %s\n", header_filename.c_str()); + } + return result; +} + bool VulkanCppConsumerBase::Initialize(const std::string& filename, const GfxToCppPlatform& platform, const std::string& outputDir) @@ -406,22 +437,16 @@ void VulkanCppConsumerBase::Destroy() if (main_file_ != nullptr) { PrintOutGlobalVar(); - std::string filename = util::filepath::Join(out_dir_, src_out_dir_ + "/global_var.h"); - int32_t result = util::platform::FileOpen(&header_file_, filename.c_str(), "w"); + bool result = WriteGlobalHeaderFile(); if (result == 0) { - // Close the last frame call. - WriteGlobalHeaderFile(); WriteMainFooter(); util::platform::FileClose(main_file_); if (platform_ != GfxToCppPlatform::PLATFORM_ANDROID) { PrintOutCMakeFile(); } - } - else - { - fprintf(stderr, "Error while opening file: %s\n", filename.c_str()); + WriteSwapchainFiles(); } pfn_loader_.WriteOutLoaderGenerator(util::filepath::Join(out_dir_, src_out_dir_), platform_); } @@ -583,8 +608,6 @@ void VulkanCppConsumerBase::Generate_vkCreateSwapchainKHR( fprintf(file, "\t{\n"); - // device - // pCreateInfo std::stringstream stream_pcreate_info; VkSwapchainCreateInfoKHR* struct_info = pCreateInfo->GetPointer(); if (platform_ == GfxToCppPlatform::PLATFORM_ANDROID) @@ -595,8 +618,6 @@ void VulkanCppConsumerBase::Generate_vkCreateSwapchainKHR( std::string pcreate_info_struct = GenerateStruct_VkSwapchainCreateInfoKHR( stream_pcreate_info, struct_info, pCreateInfo->GetMetaStructPointer(), *this); fprintf(file, "%s", stream_pcreate_info.str().c_str()); - // pAllocator - // pSwapchain std::string pswapchain_name = "pSwapchain_" + std::to_string(this->GetNextId(VK_OBJECT_TYPE_SWAPCHAIN_KHR)); AddKnownVariables("VkSwapchainKHR", pswapchain_name, pSwapchain->GetPointer()); if (returnValue == VK_SUCCESS) @@ -604,36 +625,9 @@ void VulkanCppConsumerBase::Generate_vkCreateSwapchainKHR( this->AddHandles(pswapchain_name, pSwapchain->GetPointer()); } - DeviceInfo* dev_info = nullptr; - if (device_info_map_.find(device) != device_info_map_.end()) - { - dev_info = device_info_map_[device]; - - fprintf(file, "\t\tVkSurfaceCapabilitiesKHR surface_caps;\n"); - fprintf(file, - "\t\tif (VK_SUCCESS == loaded_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(%s, %s, &surface_caps)) {\n", - this->GetHandle(dev_info->parent).c_str(), - this->GetHandle(pCreateInfo->GetMetaStructPointer()->surface).c_str()); - - // VkSwapchainCreateInfoKHR modified_create_info = *create_info; - // modified_create_info.imageUsage = - // modified_create_info.imageUsage | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; - - fprintf(file, "\t\t\tif (%s.minImageCount < surface_caps.minImageCount) {\n", pcreate_info_struct.c_str()); - fprintf(file, "\t\t\t\t%s.minImageCount = surface_caps.minImageCount;\n", pcreate_info_struct.c_str()); - fprintf(file, "\t\t\t}\n"); - fprintf(file, - "\t\t\tif ((surface_caps.maxImageCount > 0) && (%s.minImageCount > surface_caps.maxImageCount)) {\n", - pcreate_info_struct.c_str()); - fprintf(file, "\t\t\t\t%s.minImageCount = surface_caps.maxImageCount;\n", pcreate_info_struct.c_str()); - fprintf(file, "\t\t\t}\n"); - - fprintf(file, "\t\t}\n"); - } - pfn_loader_.AddMethodName("vkCreateSwapchainKHR"); fprintf(file, - "\t\tVK_CALL_CHECK(loaded_vkCreateSwapchainKHR(%s, &%s, %s, &%s), %s);\n", + "\t\tVK_CALL_CHECK(toCppCreateSwapchainKHR(%s, &%s, %s, &%s), %s);\n", this->GetHandle(device).c_str(), pcreate_info_struct.c_str(), "nullptr", @@ -642,6 +636,22 @@ void VulkanCppConsumerBase::Generate_vkCreateSwapchainKHR( fprintf(file, "\t}\n"); } +void VulkanCppConsumerBase::Generate_vkDestroySwapchainKHR( + format::HandleId device, + format::HandleId swapchain, + StructPointerDecoder* pAllocator) +{ + FILE* file = GetFrameFile(); + fprintf(file, "\t{\n"); + pfn_loader_.AddMethodName("vkDestroySwapchainKHR"); + fprintf(file, + "\t\ttoCppDestroySwapchainKHR(%s, %s, %s);\n", + this->GetHandle(device).c_str(), + this->GetHandle(swapchain).c_str(), + "nullptr"); + fprintf(file, "\t}\n"); +} + void VulkanCppConsumerBase::Generate_vkGetSwapchainImagesKHR(VkResult returnValue, format::HandleId device, format::HandleId swapchain, @@ -1030,12 +1040,10 @@ void VulkanCppConsumerBase::Generate_vkAllocateMemory(VkResult fprintf(file, "\t\tbool uses_opaque_address = false;\n"); fprintf(file, "\t\tbool imports_memory = false;\n"); fprintf(file, "\n"); - fprintf( - file, "\t\tif (device_features.find(%s) != device_features.end()){\n", this->GetHandle(device).c_str()); - fprintf( - file, "\t\t\tDeviceFeatures* dev_features = device_features[%s];\n", this->GetHandle(device).c_str()); - fprintf(file, "\t\t\tif (dev_features->features_dev_buf_addr.bufferDeviceAddressCaptureReplay ||\n"); - fprintf(file, "\t\t\t dev_features->features_1_2.bufferDeviceAddressCaptureReplay) {\n"); + fprintf(file, "\t\tif (g_device_info.find(%s) != g_device_info.end()){\n", this->GetHandle(device).c_str()); + fprintf(file, "\t\t\tToCppDeviceInfo* dev_info = g_device_info[%s];\n", this->GetHandle(device).c_str()); + fprintf(file, "\t\t\tif (dev_info->features.features_dev_buf_addr.bufferDeviceAddressCaptureReplay ||\n"); + fprintf(file, "\t\t\t dev_info->features.features_1_2.bufferDeviceAddressCaptureReplay) {\n"); fprintf(file, "\t\t\t\tcan_use_opaque_address = true;\n"); fprintf(file, "\t\t\t\taddress_info.opaque_address = %" PRIu64 "ULL;\n", @@ -1129,10 +1137,10 @@ void VulkanCppConsumerBase::Generate_vkCreateBuffer(VkResult if (dev_info != nullptr) { - fprintf(file, "\t\tif (device_features.find(%s) != device_features.end()){\n", this->GetHandle(device).c_str()); - fprintf(file, "\t\t\tDeviceFeatures* dev_features = device_features[%s];\n", this->GetHandle(device).c_str()); - fprintf(file, "\t\t\tif (dev_features->features_dev_buf_addr.bufferDeviceAddressCaptureReplay ||\n"); - fprintf(file, "\t\t\t dev_features->features_1_2.bufferDeviceAddressCaptureReplay) {\n"); + fprintf(file, "\t\tif (g_device_info.find(%s) != g_device_info.end()){\n", this->GetHandle(device).c_str()); + fprintf(file, "\t\t\tToCppDeviceInfo* dev_info = g_device_info[%s];\n", this->GetHandle(device).c_str()); + fprintf(file, "\t\t\tif (dev_info->features.features_dev_buf_addr.bufferDeviceAddressCaptureReplay ||\n"); + fprintf(file, "\t\t\t dev_info->features.features_1_2.bufferDeviceAddressCaptureReplay) {\n"); fprintf(file, "\t\t\t\tbool uses_address = false;\n"); fprintf(file, "\t\t\t\tVkBufferCreateFlags address_create_flags = 0;\n"); fprintf(file, "\t\t\t\tVkBufferUsageFlags address_usage_flags = 0;\n"); @@ -1445,56 +1453,18 @@ void VulkanCppConsumerBase::Generate_vkCreateDevice(VkResult pdevice_name.c_str(), util::ToString(returnValue).c_str()); - fprintf(file, "\n"); - fprintf(file, "\t\tDeviceFeatures* dev_features = new DeviceFeatures();\n"); - fprintf(file, "\t\tassert(dev_features != nullptr);\n"); - fprintf(file, "\t\tmemset(dev_features, 0, sizeof(DeviceFeatures));\n"); - fprintf(file, "\t\tif (%s.pEnabledFeatures != nullptr) {\n", pcreate_info_struct.c_str()); - fprintf(file, "\t\t\tdev_features->features_1_0 = *(%s.pEnabledFeatures);\n", pcreate_info_struct.c_str()); - fprintf(file, "\t\t}\n"); fprintf(file, "\n"); fprintf(file, - "\t\tconst VkBaseInStructure* cur_next = reinterpret_cast(%s.pNext);\n", + "\t\tif (!toCppInitDeviceInfo(%s, %s, &%s)) {\n", + this->GetHandle(physicalDevice).c_str(), + pdevice_name.c_str(), pcreate_info_struct.c_str()); - fprintf(file, "\t\twhile (cur_next != nullptr) {\n"); - fprintf(file, "\t\t\tswitch (cur_next->sType) {\n"); - fprintf(file, "\t\t\t\tcase VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES:\n"); - fprintf(file, - "\t\t\t\t\tmemcpy(&dev_features->features_1_1, cur_next, sizeof(VkPhysicalDeviceVulkan11Features));\n"); - fprintf(file, "\t\t\t\t\tdev_features->features_1_1.pNext = nullptr;\n"); - fprintf(file, "\t\t\t\t\tbreak;\n"); - fprintf(file, "\t\t\t\tcase VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES:\n"); - fprintf(file, - "\t\t\t\t\tmemcpy(&dev_features->features_1_2, cur_next, sizeof(VkPhysicalDeviceVulkan12Features));\n"); - fprintf(file, "\t\t\t\t\tdev_features->features_1_2.pNext = nullptr;\n"); - fprintf(file, "\t\t\t\t\tbreak;\n"); - fprintf(file, "\t\t\t\tcase VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES:\n"); - fprintf(file, - "\t\t\t\t\tmemcpy(&dev_features->features_dev_buf_addr, cur_next, " - "sizeof(VkPhysicalDeviceBufferDeviceAddressFeatures));\n"); - fprintf(file, "\t\t\t\t\tdev_features->features_dev_buf_addr.pNext = nullptr;\n"); - fprintf(file, "\t\t\t\t\tbreak;\n"); - fprintf(file, "#ifndef VK_USE_PLATFORM_ANDROID_KHR\n"); - fprintf(file, "\t\t\t\tcase VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR:\n"); - fprintf(file, - "\t\t\t\t\tmemcpy(&dev_features->features_accel_struct, cur_next, " - "sizeof(VkPhysicalDeviceAccelerationStructureFeaturesKHR));\n"); - fprintf(file, "\t\t\t\t\tdev_features->features_accel_struct.pNext = nullptr;\n"); - fprintf(file, "\t\t\t\t\tbreak;\n"); - fprintf(file, "\t\t\t\tcase VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR:\n"); - fprintf(file, - "\t\t\t\t\tmemcpy(&dev_features->features_ray_trace_pipeline, cur_next, " - "sizeof(VkPhysicalDeviceRayTracingPipelineFeaturesKHR));\n"); - fprintf(file, "\t\t\t\t\tdev_features->features_ray_trace_pipeline.pNext = nullptr;\n"); - fprintf(file, "\t\t\t\t\tbreak;\n"); - fprintf(file, "#endif\n"); - fprintf(file, "\t\t\t\tdefault: break;\n"); - fprintf(file, "\t\t\t}\n"); - - fprintf(file, "\t\t\tcur_next = cur_next->pNext;\n"); + fprintf( + file, + "\t\t\tprintf(\"ERROR: Failed to generate device info for device %s created from physical device %s\\n\");\n", + pdevice_name.c_str(), + this->GetHandle(physicalDevice).c_str()); fprintf(file, "\t\t}\n"); - fprintf(file, "\t\t// Save feature information for this device\n"); - fprintf(file, "\t\tdevice_features[%s] = dev_features;\n", pdevice_name.c_str()); fprintf(file, "\t}\n"); } @@ -1512,15 +1482,8 @@ void VulkanCppConsumerBase::Generate_vkDestroyDevice(format::HandleId } fprintf(file, "\t{\n"); - fprintf(file, "\t\tif (device_features.find(%s) != device_features.end()){\n", this->GetHandle(device).c_str()); - fprintf(file, "\t\t\tDeviceFeatures* dev_features = device_features[%s];\n", this->GetHandle(device).c_str()); - fprintf(file, "\t\t\tdelete dev_features;\n"); - fprintf(file, "\t\t\tdevice_features.erase(%s);\n", this->GetHandle(device).c_str()); - fprintf(file, "\t\t}\n"); + fprintf(file, "\t\ttoCppDestroyDeviceInfo(%s);\n", this->GetHandle(device).c_str()); fprintf(file, "\n"); - - // device - // pAllocator fprintf(file, "\t\tvkDestroyDevice(%s, %s);\n", this->GetHandle(device).c_str(), "nullptr"); fprintf(file, "\t}\n"); } diff --git a/framework/decode/vulkan_cpp_consumer_base.h b/framework/decode/vulkan_cpp_consumer_base.h index a84deebf8d..f324d67d75 100644 --- a/framework/decode/vulkan_cpp_consumer_base.h +++ b/framework/decode/vulkan_cpp_consumer_base.h @@ -154,6 +154,10 @@ class VulkanCppConsumerBase : public VulkanConsumer StructPointerDecoder* pAllocator, HandlePointerDecoder* pSwapchain); + void Generate_vkDestroySwapchainKHR(format::HandleId device, + format::HandleId swapchain, + StructPointerDecoder* pAllocator); + void Generate_vkGetSwapchainImagesKHR(VkResult returnValue, format::HandleId device, format::HandleId swapchain, @@ -664,10 +668,11 @@ class VulkanCppConsumerBase : public VulkanConsumer private: bool CreateSubOutputDirectories(const std::vector& subDirs); void PrintOutCMakeFile(); - void PrintOutGlobalVar(); void WriteMainHeader(); void WriteMainFooter(); - void WriteGlobalHeaderFile(); + bool WriteGlobalHeaderFile(); + void PrintOutGlobalVar(); + bool WriteSwapchainFiles(); struct FrameTempMemory { @@ -682,7 +687,6 @@ class VulkanCppConsumerBase : public VulkanConsumer std::vector frame_split_temp_memory_; FILE* frame_file_; FILE* global_file_; - FILE* header_file_; FILE* main_file_; std::string filename_; GfxToCppPlatform platform_; diff --git a/framework/decode/vulkan_cpp_template_strings.h b/framework/decode/vulkan_cpp_template_strings.h index 25138a2e56..0cdc2c4197 100644 --- a/framework/decode/vulkan_cpp_template_strings.h +++ b/framework/decode/vulkan_cpp_template_strings.h @@ -23,14 +23,15 @@ GFXRECON_BEGIN_NAMESPACE(decode) static const char* sCommonHeaderOutputHeaders = R"( #pragma once +#include "vulkan/vulkan.h" + #include #include #include #include #include #include - -#include +#include )"; static const char* sCommonOutputHeaderFunctions = R"( @@ -42,13 +43,17 @@ extern void LogVkError(const char* function, VkResult returnValue, const char* f static const char* sCommonGlobalCppHeader = R"( #include "global_var.h" -#include "vulkan/vulkan.h" +#include "loader.h" + +#include +#include )"; static const char* sCommonFrameSourceHeader = R"( #include "global_var.h" #include "loader.h" -#include "vulkan/vulkan.h" +#include "swapchain_common.h" + #include )"; @@ -90,10 +95,10 @@ uint32_t RecalculateMemoryTypeIndex(uint32_t originalMemoryTypeIndex) { target_property_flags) { memory_type_index = originalMemoryTypeIndex; } else { - bool fallback_found = false; - uint32_t fallback_index = 0; - uint8_t fallback_important_bits = 0; - uint8_t fallback_normal_bits = 0; + bool fallback_found = false; + uint32_t fallback_index = 0; + uint8_t fallback_important_bits = 0; + uint8_t fallback_normal_bits = 0; for (uint32_t i = 0; i < s_physicalDeviceMemoryProperties.memoryTypeCount; i++) { if((s_physicalDeviceMemoryProperties.memoryTypes[i].propertyFlags & target_property_flags) == target_property_flags) { memory_type_index = i; @@ -734,6 +739,208 @@ target_link_libraries(vulkan_app vulkan) )"; // End of Win32 template strings +// Swapchain code +static const char* sSwapchainHeaderCode = R"( +#include +#include + +struct ToCppDeviceFeatures +{ + VkPhysicalDeviceFeatures features_1_0; + VkPhysicalDeviceVulkan11Features features_1_1; + VkPhysicalDeviceVulkan12Features features_1_2; + VkPhysicalDeviceBufferDeviceAddressFeatures features_dev_buf_addr; +#ifndef VK_USE_PLATFORM_ANDROID_KHR + VkPhysicalDeviceAccelerationStructureFeaturesKHR features_accel_struct; + VkPhysicalDeviceRayTracingPipelineFeaturesKHR features_ray_trace_pipeline; +#endif +}; + +struct ToCppDeviceInfo +{ + VkPhysicalDevice parent; + ToCppDeviceFeatures features; + std::unordered_map queue_family_creation_flags; + std::vector queue_family_index_enabled; +}; +extern std::unordered_map g_device_info; + +bool toCppInitDeviceInfo(VkPhysicalDevice physical_device, VkDevice device, VkDeviceCreateInfo* create_info); +void toCppDestroyDeviceInfo(VkDevice device); + +// Swapchain Info + +struct ToCppSwapchainInfo +{ + VkDevice parent; + VkSurfaceKHR surface; +}; +extern std::unordered_map g_swapchain_info; + +VkResult toCppCreateSwapchainKHR(VkDevice device, + const VkSwapchainCreateInfoKHR* create_info, + const VkAllocationCallbacks* allocator, + VkSwapchainKHR* swapchain); +void toCppDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* allocator); +)"; + +static const char* sSwapchainSourceCode = R"( +#include "global_var.h" +#include "loader.h" +#include "swapchain_common.h" + +std::unordered_map g_device_info; +std::unordered_map g_swapchain_info; +bool toCppInitDeviceInfo(VkPhysicalDevice physical_device, VkDevice device, VkDeviceCreateInfo* create_info) +{ + bool success = false; + ToCppDeviceInfo* dev_info = new ToCppDeviceInfo(); + if (dev_info == nullptr) { + printf("ERROR: Failed to create ToCppDeviceInfo for new device.\n"); + return success; + } + dev_info->parent = physical_device; + memset(&dev_info->features, 0, sizeof(ToCppDeviceFeatures)); + + if (create_info->pEnabledFeatures != nullptr) + { + dev_info->features.features_1_0 = *(create_info->pEnabledFeatures); + } + + const VkBaseInStructure* cur_next = reinterpret_cast(create_info->pNext); + while (cur_next != nullptr) + { + switch (cur_next->sType) + { + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES: + memcpy(&dev_info->features.features_1_1, cur_next, sizeof(VkPhysicalDeviceVulkan11Features)); + dev_info->features.features_1_1.pNext = nullptr; + break; + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES: + memcpy(&dev_info->features.features_1_2, cur_next, sizeof(VkPhysicalDeviceVulkan12Features)); + dev_info->features.features_1_2.pNext = nullptr; + break; + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES: + memcpy(&dev_info->features.features_dev_buf_addr, + cur_next, + sizeof(VkPhysicalDeviceBufferDeviceAddressFeatures)); + dev_info->features.features_dev_buf_addr.pNext = nullptr; + break; +#ifndef VK_USE_PLATFORM_ANDROID_KHR + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR: + memcpy(&dev_info->features.features_accel_struct, + cur_next, + sizeof(VkPhysicalDeviceAccelerationStructureFeaturesKHR)); + dev_info->features.features_accel_struct.pNext = nullptr; + break; + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR: + memcpy(&dev_info->features.features_ray_trace_pipeline, + cur_next, + sizeof(VkPhysicalDeviceRayTracingPipelineFeaturesKHR)); + dev_info->features.features_ray_trace_pipeline.pNext = nullptr; + break; +#endif + default: + break; + } // switch + cur_next = cur_next->pNext; + } // while + + // Keep track of what queue families this device is planning on using. This information is + // very important if we end up using the VulkanVirtualSwapchain path. + auto max = [](uint32_t current_max, const VkDeviceQueueCreateInfo& dqci) { + return std::max(current_max, dqci.queueFamilyIndex); + }; + uint32_t max_queue_family = std::accumulate( + create_info->pQueueCreateInfos, create_info->pQueueCreateInfos + create_info->queueCreateInfoCount, 0, max); + dev_info->queue_family_index_enabled.clear(); + dev_info->queue_family_index_enabled.resize(max_queue_family + 1, false); + + for (uint32_t q = 0; q < create_info->queueCreateInfoCount; ++q) + { + const VkDeviceQueueCreateInfo* queue_create_info = &create_info->pQueueCreateInfos[q]; + assert(dev_info->queue_family_creation_flags.find(queue_create_info->queueFamilyIndex) == + dev_info->queue_family_creation_flags.end()); + dev_info->queue_family_creation_flags[queue_create_info->queueFamilyIndex] = queue_create_info->flags; + dev_info->queue_family_index_enabled[queue_create_info->queueFamilyIndex] = true; + } + + // Save device information for this device + g_device_info[device] = dev_info; + + success = true; + return success; +} + +void toCppDestroyDeviceInfo(VkDevice device) +{ + if (g_device_info.find(device) != g_device_info.end()) + { + ToCppDeviceInfo* dev_info = g_device_info[device]; + delete dev_info; + g_device_info.erase(device); + } +} + +VkResult toCppCreateSwapchainKHR(VkDevice device, + const VkSwapchainCreateInfoKHR* create_info, + const VkAllocationCallbacks* allocator, + VkSwapchainKHR* swapchain) +{ + VkSwapchainCreateInfoKHR modified_create_info = *create_info; + ToCppDeviceInfo* dev_info = nullptr; + ToCppSwapchainInfo* swapchain_info = nullptr; + if (g_device_info.find(device) != g_device_info.end()) + { + dev_info = g_device_info[device]; +#ifdef USE_VIRTUAL_SWAPCHAIN + modified_create_info.imageUsage = + modified_create_info.imageUsage | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; +#endif // USE_VIRTUAL_SWAPCHAIN + + VkSurfaceCapabilitiesKHR surface_caps; + if (VK_SUCCESS == + loaded_vkGetPhysicalDeviceSurfaceCapabilitiesKHR(dev_info->parent, create_info->surface, &surface_caps)) + { + if (modified_create_info.minImageCount < surface_caps.minImageCount) + { + modified_create_info.minImageCount = surface_caps.minImageCount; + } + if ((surface_caps.maxImageCount > 0) && (modified_create_info.minImageCount > surface_caps.maxImageCount)) + { + modified_create_info.minImageCount = surface_caps.maxImageCount; + } + } + + swapchain_info = new ToCppSwapchainInfo(); + assert(swapchain_info != nullptr); + + memset(swapchain_info, 0, sizeof(ToCppSwapchainInfo)); + swapchain_info->parent = device; + swapchain_info->surface = create_info->surface; + } + + VkResult ret_value = loaded_vkCreateSwapchainKHR(device, &modified_create_info, allocator, swapchain); + if (ret_value == VK_SUCCESS && dev_info != nullptr) + { + // Save swapchain information + g_swapchain_info[*swapchain] = swapchain_info; + } + return ret_value; +} + +void toCppDestroySwapchainKHR(VkDevice device, VkSwapchainKHR swapchain, const VkAllocationCallbacks* allocator) +{ + if (g_swapchain_info.find(swapchain) != g_swapchain_info.end()) + { + ToCppSwapchainInfo* swapchain_info = g_swapchain_info[swapchain]; + delete swapchain_info; + g_swapchain_info.erase(swapchain); + } + loaded_vkDestroySwapchainKHR(device, swapchain, allocator); +} +)"; + GFXRECON_END_NAMESPACE(gfxrecon) GFXRECON_END_NAMESPACE(decode) diff --git a/framework/generated/generated_vulkan_cpp_consumer.cpp b/framework/generated/generated_vulkan_cpp_consumer.cpp index f9c2b5c178..40c7443c58 100644 --- a/framework/generated/generated_vulkan_cpp_consumer.cpp +++ b/framework/generated/generated_vulkan_cpp_consumer.cpp @@ -5722,18 +5722,7 @@ void VulkanCppConsumer::Process_vkDestroySwapchainKHR( format::HandleId swapchain, StructPointerDecoder* pAllocator) { - FILE* file = GetFrameFile(); - fprintf(file, "\t{\n"); -// device -// swapchain -// pAllocator - pfn_loader_.AddMethodName("vkDestroySwapchainKHR"); - fprintf(file, - "\t\tloaded_vkDestroySwapchainKHR(%s, %s, %s);\n", - this->GetHandle(device).c_str(), - this->GetHandle(swapchain).c_str(), - "nullptr"); - fprintf(file, "\t}\n"); + Generate_vkDestroySwapchainKHR(device, swapchain, pAllocator); Post_APICall(format::ApiCallId::ApiCall_vkDestroySwapchainKHR); } diff --git a/framework/generated/vulkan_generators/vulkan_cpp_consumer_body_generator.py b/framework/generated/vulkan_generators/vulkan_cpp_consumer_body_generator.py index f8f674b94b..378220a0e7 100644 --- a/framework/generated/vulkan_generators/vulkan_cpp_consumer_body_generator.py +++ b/framework/generated/vulkan_generators/vulkan_cpp_consumer_body_generator.py @@ -136,6 +136,7 @@ def getExtApiStructs(featureDictionary): 'vkCreateAndroidSurfaceKHR', 'vkCreateDescriptorUpdateTemplateKHR', 'vkCreateSwapchainKHR', + 'vkDestroySwapchainKHR', 'vkCreateWaylandSurfaceKHR', 'vkCreateWin32SurfaceKHR', 'vkCreateXcbSurfaceKHR',