Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 60 additions & 9 deletions layersvt/api_dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,10 @@ class ApiDumpInstance {

// Called from vkQueuePresentKHR, from vkQueueSubmit/vkQueueSubmit2 when a chained
// VkFrameBoundaryEXT marks the end of a frame (see checkFrameBoundaryInSubmit), and from
// vkFrameBoundaryANDROID. Always advances frame_count and the frame's own JSON markup,
// vkFrameBoundaryANDROID. Each dumps the call that triggers the boundary before calling this, so
// that call lands in the frame it closes. That does not make it the only thing a frame can end on:
// a vkQueueSubmit2 with several end-flagged submits calls this once per submit, and the extra
// frames it closes are empty. Always advances frame_count and the frame's own JSON markup,
// regardless of capture_trigger_boundary - a frame boundary is a frame boundary independent of
// what output_range/capture_trigger is currently counting. See notifyQueueSubmit for the
// independent counter used for range/trigger decisions when the boundary is queue_submits.
Expand Down Expand Up @@ -1446,13 +1449,17 @@ class ApiDumpInstance {
// (GetCommandNumberAPIDUMP via GetInstanceProcAddr).
void setFrameBoundaryAndroidFunction(PFN_vkFrameBoundaryANDROID fp) { next_frame_boundary_android = fp; }

// Hand-written interception for vkFrameBoundaryANDROID. Forwards to the resolved pointer so the
// app's call still reaches the driver - it is void, so there is nothing to fail even if no
// driver below actually implements it - then advances the real frame counter the same way any
// other genuine frame-boundary signal does.
void frameBoundaryAndroid(VkDevice device, VkSemaphore semaphore, VkImage image) {
// Forwards a vkFrameBoundaryANDROID to the resolved pointer so the app's call still reaches the
// driver - it is void, so there is nothing to fail even if no driver below actually implements it.
//
// Deliberately does not advance the frame counter: the interception (layer_vkFrameBoundaryANDROID)
// has to write the call into the dump first, so that it lands in the frame it ends, and only then
// call nextFrame() - the same order vkQueuePresentKHR uses. Without the entry in the dump, a frame
// that ends here has no visible reason to have ended, which is how an app whose UI renderer calls
// this on one thread while another thread presents ends up with frames that contain neither a
// present nor a submit.
void forwardFrameBoundaryAndroid(VkDevice device, VkSemaphore semaphore, VkImage image) {
Comment thread
wpaul-samsung marked this conversation as resolved.
Resolved
if (next_frame_boundary_android) next_frame_boundary_android(device, semaphore, image);
nextFrame();
}

// The number assigned to the command currently being dumped. Only advanced in dump_function_head
Expand Down Expand Up @@ -1701,8 +1708,8 @@ class ApiDumpInstance {
uint64_t command_count;

// The next layer/driver's real vkFrameBoundaryANDROID, resolved once at CreateDevice - see
// setFrameBoundaryAndroidFunction/frameBoundaryAndroid. Null until then, and whenever nothing
// below actually implements it.
// setFrameBoundaryAndroidFunction/forwardFrameBoundaryAndroid. Null until then, and whenever
// nothing below actually implements it.
PFN_vkFrameBoundaryANDROID next_frame_boundary_android = nullptr;

std::mutex thread_mutex;
Expand Down Expand Up @@ -2743,6 +2750,50 @@ inline void dump_function_head(ApiDumpInstance &dump_inst, const char *funcName,
}
}

// Everything the layer does for an app's vkFrameBoundaryANDROID, in order: dump the call, forward it
// to whatever implements it below, then close the frame - so the call is the last entry of the frame
// it ends, the same order vkQueuePresentKHR uses. Must be called with the output mutex held.
//
// Lives here, apart from the layer entry point, so the order and the frame advance can be tested
// without a device. The one part that cannot live here is writing the parameters, which need the
// generated dump functions this header cannot see: `params_dumper` is called as
// params_dumper.template operator()<Format>(dump_inst) and has to dump just the parameters.
template <typename ParamsDumper>
inline void handle_frame_boundary_android(ApiDumpInstance &dump_inst, VkDevice device, VkSemaphore semaphore, VkImage image,
ParamsDumper &&params_dumper) {
dump_function_head(dump_inst, "vkFrameBoundaryANDROID", "device, semaphore, image", "void");
if (dump_inst.settings().shouldPreDump() && dump_inst.settings().format() == ApiDumpFormat::Text &&
dump_inst.shouldDumpOutput()) {
dump_before_pre_dump_formatting<ApiDumpFormat::Text>(dump_inst.settings());
params_dumper.template operator()<ApiDumpFormat::Text>(dump_inst);
}

dump_inst.forwardFrameBoundaryAndroid(device, semaphore, image);

if (dump_inst.shouldDumpOutput()) {
switch (dump_inst.settings().format()) {
case ApiDumpFormat::Text:
dump_pre_function_formatting<ApiDumpFormat::Text>(dump_inst.settings());
params_dumper.template operator()<ApiDumpFormat::Text>(dump_inst);
dump_post_function_formatting<ApiDumpFormat::Text>(dump_inst.settings());
break;
case ApiDumpFormat::Html:
dump_pre_function_formatting<ApiDumpFormat::Html>(dump_inst.settings());
params_dumper.template operator()<ApiDumpFormat::Html>(dump_inst);
dump_post_function_formatting<ApiDumpFormat::Html>(dump_inst.settings());
break;
case ApiDumpFormat::Json:
dump_pre_function_formatting<ApiDumpFormat::Json>(dump_inst.settings());
params_dumper.template operator()<ApiDumpFormat::Json>(dump_inst);
dump_post_function_formatting<ApiDumpFormat::Json>(dump_inst.settings());
break;
}
flush(dump_inst.settings());
}

dump_inst.nextFrame();
}

//==================================== Exposed Query Functions ======================================//

// Lets other layers poll the commandNumber of the last command api_dump wrote, to correlate their
Expand Down
39 changes: 37 additions & 2 deletions layersvt/api_dump_handwritten_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,36 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice physicalDevice, c
return result;
}

// vkFrameBoundaryANDROID is not in the generated dump code (see VK_ANDROID_frame_boundary.h), so its dump
// is written by hand, in the shape the generator gives every other void command. These are templates,
// so they have to sit outside the extern "C" block below that holds layer_vkFrameBoundaryANDROID.
template <ApiDumpFormat Format>
void dump_params_vkFrameBoundaryANDROID(ApiDumpInstance& dump_inst, VkDevice device, VkSemaphore semaphore, VkImage image) {
const ApiDumpSettings& settings(dump_inst.settings());
if (settings.showParams()) {
dump_pre_params_formatting<Format>(settings);
dump_VkDevice<Format>(device, settings, "VkDevice", "device", (Format == ApiDumpFormat::Json ? 4 : 1));
dump_separate_members<Format>(settings);
dump_VkSemaphore<Format>(semaphore, settings, "VkSemaphore", "semaphore", (Format == ApiDumpFormat::Json ? 4 : 1));
dump_separate_members<Format>(settings);
dump_VkImage<Format>(image, settings, "VkImage", "image", (Format == ApiDumpFormat::Json ? 4 : 1));
dump_post_params_formatting<Format>(settings);
flush(settings);
}
}

// What handle_frame_boundary_android (api_dump.h) calls to write the parameters, once per format.
struct FrameBoundaryAndroidParamsDumper {
VkDevice device;
VkSemaphore semaphore;
VkImage image;

template <ApiDumpFormat Format>
void operator()(ApiDumpInstance& dump_inst) const {
dump_params_vkFrameBoundaryANDROID<Format>(dump_inst, device, semaphore, image);
}
};

extern "C" {

VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_layerGetPhysicalDeviceProcAddr(VkInstance instance, const char* funcName) {
Expand Down Expand Up @@ -226,9 +256,14 @@ EXPORT_FUNCTION VKAPI_ATTR VkResult VKAPI_CALL layer_vkEnumerateDeviceExtensionP
// vkFrameBoundaryANDROID has no dispatch-table slot (see VK_ANDROID_frame_boundary.h), so unlike
// every generated entry point it cannot be reached through device_dispatch_table(device)->X - it is
// caught by name instead, in layer_vkGetInstanceProcAddr/layer_vkGetDeviceProcAddr below.
//
// The call is dumped like any other, and before the frame counter advances, so it is the last entry of
// the frame it ends - what lets a consumer of the dump see why the frame ended. The sequence itself is
// handle_frame_boundary_android (api_dump.h), where it can be tested without a device.
VKAPI_ATTR void VKAPI_CALL layer_vkFrameBoundaryANDROID(VkDevice device, VkSemaphore semaphore, VkImage image) {
std::lock_guard<std::mutex> lg(ApiDumpInstance::current().outputMutex());
ApiDumpInstance::current().frameBoundaryAndroid(device, semaphore, image);
ApiDumpInstance& dump_inst = ApiDumpInstance::current();
std::lock_guard<std::mutex> lg(dump_inst.outputMutex());
handle_frame_boundary_android(dump_inst, device, semaphore, image, FrameBoundaryAndroidParamsDumper{device, semaphore, image});
}

EXPORT_FUNCTION VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t* pPropertyCount,
Expand Down
151 changes: 147 additions & 4 deletions layersvt/test/test_api_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,82 @@ TEST_F(ApiDumpTests, init_layer) {
EXPECT_STREQ(file_start_content_read.c_str(), file_start_content_expected);
}

// vkFrameBoundaryANDROID is not in the registry, so the generator never emitted a dump for it. The
// layer has to write the call itself, as the last entry of the frame it ends - otherwise a frame can
// end with nothing in the dump to say why.
TEST_F(ApiDumpTests, FrameBoundaryAndroidIsDumpedAsTheLastCallOfTheFrameItEnds) {
const char* filename_string = "api_dump_frame_boundary_android.json";
std::remove(filename_string);

VkBool32 use_file = VK_TRUE;
const char* output_format = "json";
const std::vector<VkLayerSettingEXT> settings = {
{kLayerName, "file", VK_LAYER_SETTING_TYPE_BOOL32_EXT, 1, &use_file},
{kLayerName, "log_filename", VK_LAYER_SETTING_TYPE_STRING_EXT, 1, &filename_string},
{kLayerName, "output_format", VK_LAYER_SETTING_TYPE_STRING_EXT, 1, &output_format}};

{
layer_test::VulkanInstanceBuilder inst_builder;
ASSERT_EQ(inst_builder.Init(settings), VK_SUCCESS);

VkPhysicalDevice physical_device = VK_NULL_HANDLE;
if (inst_builder.GetPhysicalDevice(&physical_device) != VK_SUCCESS || physical_device == VK_NULL_HANDLE) {
GTEST_SKIP() << "no physical device to create a device on";
}
// The call below passes null semaphore and image, which the layer forwards to whatever it
// resolved below itself at vkCreateDevice. Harmless where nothing implements the function, but
// where a driver or lower layer does (Android), that would be an invalid call into real code.
// The test only needs the layer to answer by name, so it does not run there.
if (layer_test::IsExtensionSupported(physical_device, "VK_ANDROID_frame_boundary")) {
GTEST_SKIP() << "something below the layer implements VK_ANDROID_frame_boundary; "
"not calling it with null handles";
}

float priority = 1.0f;
VkDeviceQueueCreateInfo queue_info{VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO};
queue_info.queueFamilyIndex = 0;
queue_info.queueCount = 1;
queue_info.pQueuePriorities = &priority;
VkDeviceCreateInfo device_info{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO};
device_info.queueCreateInfoCount = 1;
device_info.pQueueCreateInfos = &queue_info;

VkDevice device = VK_NULL_HANDLE;
ASSERT_EQ(vkCreateDevice(physical_device, &device_info, nullptr, &device), VK_SUCCESS);

// Resolved by name, like an app does: the layer answers for it whether or not the driver
// below implements the extension.
auto fp = reinterpret_cast<void(VKAPI_PTR*)(VkDevice, VkSemaphore, VkImage)>(
vkGetDeviceProcAddr(device, "vkFrameBoundaryANDROID"));
ASSERT_NE(fp, nullptr);
fp(device, VK_NULL_HANDLE, VK_NULL_HANDLE);
Comment thread
wpaul-samsung marked this conversation as resolved.
Resolved

vkDestroyDevice(device, nullptr);
}

std::ifstream stream(filename_string);
ASSERT_TRUE(stream.good());
const std::string dump((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
stream.close();
std::remove(filename_string);

const size_t boundary_call = dump.find("\"name\" : \"vkFrameBoundaryANDROID\"");
ASSERT_NE(boundary_call, std::string::npos) << dump;
const size_t frame_one = dump.find("\"frameNumber\" : \"1\"");
ASSERT_NE(frame_one, std::string::npos) << dump;

// Written into the frame it ends (frame 0), not the one that opens after it.
EXPECT_LT(boundary_call, frame_one);
const size_t frame_zero = dump.find("\"frameNumber\" : \"0\"");
ASSERT_NE(frame_zero, std::string::npos);
EXPECT_LT(frame_zero, boundary_call);

// And the vkDestroyDevice after it belongs to frame 1.
const size_t destroy_call = dump.find("\"name\" : \"vkDestroyDevice\"");
ASSERT_NE(destroy_call, std::string::npos);
EXPECT_GT(destroy_call, frame_one);
}

// ConditionalFrameOutput parses output_range/output_range_queue_submits strings and answers
// isFrameInRange for them. Neither needs a Vulkan device, so these test it directly rather than
// through a full capture.
Expand Down Expand Up @@ -346,16 +422,83 @@ TEST_F(ApiDumpInstanceTests, QueueSubmitIsNeverSetupContentInFramesBoundary) {
EXPECT_FALSE(instance.currentCommandIsSetup());
}

TEST_F(ApiDumpInstanceTests, FrameBoundaryAndroidAdvancesFrameCountWithNoDriverResolved) {
static int g_frame_boundary_android_calls = 0;
static void VKAPI_CALL CountingFrameBoundaryAndroid(VkDevice, VkSemaphore, VkImage) { ++g_frame_boundary_android_calls; }

TEST_F(ApiDumpInstanceTests, ForwardFrameBoundaryAndroidWithNoDriverResolvedDoesNotCrash) {
Comment thread
wpaul-samsung marked this conversation as resolved.
Resolved
// The driver-forwarding pointer is resolved separately at CreateDevice time (see
// api_dump_handwritten_functions.h); this only exercises the interception's own behavior, which
// must not crash and must still advance the real frame counter when nothing is resolved yet.
// api_dump_handwritten_functions.h); with nothing resolved yet the forward must be a no-op.
// It must not advance the frame counter either: layer_vkFrameBoundaryANDROID dumps the call first
// and only then calls nextFrame(), so the call is the last entry of the frame it ends.
ApiDumpInstance instance;
InitWithSettings(instance, {});

instance.frameBoundaryAndroid(VK_NULL_HANDLE, VK_NULL_HANDLE, VK_NULL_HANDLE);
instance.forwardFrameBoundaryAndroid(VK_NULL_HANDLE, VK_NULL_HANDLE, VK_NULL_HANDLE);

EXPECT_EQ(instance.frameCount(), 0u);
}

TEST_F(ApiDumpInstanceTests, ForwardFrameBoundaryAndroidReachesTheResolvedDriverFunction) {
ApiDumpInstance instance;
InitWithSettings(instance, {});
g_frame_boundary_android_calls = 0;
instance.setFrameBoundaryAndroidFunction(CountingFrameBoundaryAndroid);

instance.forwardFrameBoundaryAndroid(VK_NULL_HANDLE, VK_NULL_HANDLE, VK_NULL_HANDLE);

EXPECT_EQ(g_frame_boundary_android_calls, 1);
EXPECT_EQ(instance.frameCount(), 0u);
}

// The frame count as each step of handle_frame_boundary_android saw it. The helper is what
// layer_vkFrameBoundaryANDROID runs, and unlike the layer entry point it needs no device - so this is
// the device-free check that a vkFrameBoundaryANDROID dumps the call, forwards it, and only then
// advances the frame. (The parameter dump itself is generated code the test cannot include; it is
// stood in for here, and covered end to end by ApiDumpTests.FrameBoundaryAndroid...)
static ApiDumpInstance* g_boundary_instance = nullptr;
static std::vector<uint64_t> g_frame_count_at_dump;
static std::vector<uint64_t> g_frame_count_at_forward;

static void VKAPI_CALL RecordingFrameBoundaryAndroid(VkDevice, VkSemaphore, VkImage) {
g_frame_count_at_forward.push_back(g_boundary_instance->frameCount());
}

struct RecordingParamsDumper {
template <ApiDumpFormat Format>
void operator()(ApiDumpInstance& dump_inst) const {
g_frame_count_at_dump.push_back(dump_inst.frameCount());
}
};

TEST_F(ApiDumpInstanceTests, FrameBoundaryAndroidIsDumpedAndForwardedBeforeTheFrameAdvances) {
ApiDumpInstance instance;
InitWithSettings(instance, {});
g_boundary_instance = &instance;
g_frame_count_at_dump.clear();
g_frame_count_at_forward.clear();
instance.setFrameBoundaryAndroidFunction(RecordingFrameBoundaryAndroid);

handle_frame_boundary_android(instance, VK_NULL_HANDLE, VK_NULL_HANDLE, VK_NULL_HANDLE, RecordingParamsDumper{});

// Both happen while the call's own frame (0) is still the current one, and exactly once each...
EXPECT_EQ(g_frame_count_at_forward, std::vector<uint64_t>{0});
EXPECT_EQ(g_frame_count_at_dump, std::vector<uint64_t>{0});
// ...and then the frame advances, which only this helper does for vkFrameBoundaryANDROID.
EXPECT_EQ(instance.frameCount(), 1u);
g_boundary_instance = nullptr;
}

TEST_F(ApiDumpInstanceTests, FrameBoundaryAndroidStillAdvancesTheFrameWithNoDriverResolved) {
ApiDumpInstance instance;
InitWithSettings(instance, {});
g_frame_count_at_dump.clear();

handle_frame_boundary_android(instance, VK_NULL_HANDLE, VK_NULL_HANDLE, VK_NULL_HANDLE, RecordingParamsDumper{});
handle_frame_boundary_android(instance, VK_NULL_HANDLE, VK_NULL_HANDLE, VK_NULL_HANDLE, RecordingParamsDumper{});

// Each call is dumped into the frame it ends: 0, then 1.
EXPECT_EQ(g_frame_count_at_dump, (std::vector<uint64_t>{0, 1}));
EXPECT_EQ(instance.frameCount(), 2u);
}

// ---------------------------------------------------------------------------------------------------
Expand Down
Loading