VkPhysicalDevicephysicalDevice;uint32_tavailableExtensionCount;VkExtensionProperties*pAvailableExtensions;uint32_trequiredExtensionCount;char**pRequiredExtensions;// Determine the number of extensions available for this device.vkEnumerateDeviceExtensionProperties(physicalDevice,layerName,&availableExtensionCount,pAvailableExtensions);// Determine the number of required extensions.SwappyVk_determineDeviceExtensions(physicalDevice,availableExtensionCount,pAvailableExtensions,&requiredExtensionCount,nullptr);// Determine the required extensions.pRequiredExtensions=(char**)malloc(requiredExtensionCount*sizeof(char*));pRequiredExtensionsData=(char*)malloc(requiredExtensionCount*(VK_MAX_EXTENSION_NAME_SIZE+1));for(uint32_ti=0;i<requiredExtensionCount;i++){pRequiredExtensions[i]=&pRequiredExtensionsData[i*(VK_MAX_EXTENSION_NAME_SIZE+1)];}SwappyVk_determineDeviceExtensions(physicalDevice,availableExtensionCount,pAvailableExtensions,&requiredExtensionCount,pRequiredExtensions);
// Reusing local variables from previous snippets:// VkPhysicalDevice physicalDevice;constVkDeviceCreateInfocreateInfo;constVkAllocationCallbacksallocator;VkDevicedevice;uint32_tqueueFamilyIndex;uint32_tqueueIndex;VkQueuedeviceQueue;// Values of "device" and "deviceQueue" set in the 1st and 2nd function// calls, respectively.vkCreateDevice(physicalDevice,&createInfo,&allocator,&device);vkGetDeviceQueue(device,queueFamilyIndex,queueIndex,&deviceQueue);SwappyVk_setQueueFamilyIndex(device,deviceQueue,queueFamilyIndex);
// Reusing local variables from previous snippets:// VkPhysicalDevice physicalDevice;// VkDevice device;// Assume that the JNI environment is available in:// JNIEnv *env;// jobject jactivity;// Assume that swapchain is already known.VkSwapchainKHRswapchain;uint64_trefreshDuration;// in nanoseconds// Determine duration between vertical-blanking periods.// Example: 60 FPS sets "refreshDuration" to 16,666,666.SwappyVk_initAndGetRefreshCycleDuration(env,jactivity,physicalDevice,device,swapchain,&refreshDuration);
// Declare the periods in nanoseconds that should elapse before refreshing one// image with the next image. There are helper macros defined in swappy_common.h// for common swap durations.// This example shows what to do when you want to render your game at 30 FPS.SwappyVk_setSwapIntervalNS(device,swapchain,SWAPPY_SWAP_30FPS);
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-26 (世界標準時間)。"],[],[],null,["Use the following functions to use Android Frame Pacing with a rendering engine\nbased on the Vulkan API.\n\nIdentify required extensions for creation\n\nTo gather the set of extensions necessary to create an instance of Android Frame\nPacing when using Vulkan, complete the steps shown in the following code\nsnippet: \n\n```c++\nVkPhysicalDevice physicalDevice;\nuint32_t availableExtensionCount;\nVkExtensionProperties* pAvailableExtensions;\nuint32_t requiredExtensionCount;\nchar** pRequiredExtensions;\n\n// Determine the number of extensions available for this device.\nvkEnumerateDeviceExtensionProperties(physicalDevice, layerName, &availableExtensionCount,\n pAvailableExtensions);\n\n// Determine the number of required extensions.\nSwappyVk_determineDeviceExtensions(physicalDevice, availableExtensionCount,\n pAvailableExtensions, &requiredExtensionCount, nullptr);\n\n// Determine the required extensions.\npRequiredExtensions = (char**)malloc(requiredExtensionCount * sizeof(char*));\npRequiredExtensionsData = (char*)malloc(requiredExtensionCount * (VK_MAX_EXTENSION_NAME_SIZE + 1));\nfor (uint32_t i = 0; i \u003c requiredExtensionCount; i++) {\n pRequiredExtensions[i] = &pRequiredExtensionsData[i * (VK_MAX_EXTENSION_NAME_SIZE + 1)];\n}\nSwappyVk_determineDeviceExtensions(physicalDevice, availableExtensionCount,\n pAvailableExtensions, &requiredExtensionCount, pRequiredExtensions);\n```\n\nYou can then start Android Frame Pacing by calling `vkCreateDevice()`. The 2nd\nargument, a struct of type `VkDeviceCreateInfo*`, should have its\n`enabledExtensionCount` member set to the number of required extensions.\n\nIdentify queue family\n\nTo present the correct display queue, Android Frame Pacing needs to know which\nqueue family Vulkan is using. To determine the correct family, complete the\nsteps shown in the following code snippet: \n\n```c++\n// Reusing local variables from previous snippets:\n// VkPhysicalDevice physicalDevice;\n\nconst VkDeviceCreateInfo createInfo;\nconst VkAllocationCallbacks allocator;\nVkDevice device;\nuint32_t queueFamilyIndex;\nuint32_t queueIndex;\nVkQueue deviceQueue;\n\n// Values of \"device\" and \"deviceQueue\" set in the 1st and 2nd function\n// calls, respectively.\nvkCreateDevice(physicalDevice, &createInfo, &allocator, &device);\nvkGetDeviceQueue(device, queueFamilyIndex, queueIndex, &deviceQueue);\nSwappyVk_setQueueFamilyIndex(device, deviceQueue, queueFamilyIndex);\n```\n\nDefine framerate for swapchain\n\nTo initialize Android Frame Pacing for a given physical device and swapchain,\ncomplete the steps shown in the following code snippet: \n\n```c++\n// Reusing local variables from previous snippets:\n// VkPhysicalDevice physicalDevice;\n// VkDevice device;\n\n// Assume that the JNI environment is available in:\n// JNIEnv *env;\n// jobject jactivity;\n\n// Assume that swapchain is already known.\nVkSwapchainKHR swapchain;\nuint64_t refreshDuration; // in nanoseconds\n\n// Determine duration between vertical-blanking periods.\n// Example: 60 FPS sets \"refreshDuration\" to 16,666,666.\nSwappyVk_initAndGetRefreshCycleDuration(env, jactivity, physicalDevice,\n device, swapchain, &refreshDuration);\n```\n\nThis determines the swap duration in nanoseconds. There are helper macros\ndefined in `swappy_common.h` for common swap durations (for example,\n`SWAPPY_SWAP_60FPS`).\n\nNext, you need to supply the swap duration in nanoseconds. \n\n```c++\n// Declare the periods in nanoseconds that should elapse before refreshing one\n// image with the next image. There are helper macros defined in swappy_common.h\n// for common swap durations.\n// This example shows what to do when you want to render your game at 30 FPS.\n\nSwappyVk_setSwapIntervalNS(device, swapchain, SWAPPY_SWAP_30FPS);\n```\n\nSetting the ANativeWindow\n\nSwappy needs the handle of `ANativeWindow` in order to perform\n`ANativeWindow`-specific operation, such as calling\n[`ANativeWindow_setFrameRate()`](/ndk/reference/group/a-native-window#anativewindow_setframerate).\nCall\n[`SwappyVk_setWindow()`](/games/sdk/reference/frame-pacing/group/swappy-vk#swappyvk_setwindow)\nwhen your Android display surface has changed and you have a new `ANativeWindow`\nhandle (see the [Cube sample](https://android.googlesource.com/platform/frameworks/opt/gamesdk/+/refs/heads/master/samples/cube/) for an example).\n\nAuto Modes\n\nAndroid Frame Pacing adjusts the swap duration and pipeline mode based on the\naverage duration of previous frames. You can control this behavior with the\nfollowing functions:\n\n- [`void SwappyVk_setAutoSwapInterval(bool enabled);`](/games/sdk/reference/frame-pacing/group/swappy-vk#swappyvk_setautoswapinterval)\n- [`void SwappyVk_setMaxAutoSwapIntervalNS(uint64_t max_swap_ns);`](/games/sdk/reference/frame-pacing/group/swappy-vk#group__swappy_vk_1ga250d3120ca0446f36be6a94975169013)\n- [`void SwappyVk_setAutoPipelineMode(bool enabled);`](/games/sdk/reference/frame-pacing/group/swappy-vk#swappyvk_setautopipelinemode)\n\nPresent a frame\n\nTo present a frame of your game to Android Frame Pacing, call\n[`SwappyVk_queuePresent()`](/games/sdk/reference/frame-pacing/group/swappy-vk#swappyvk_queuepresent).\nThis function calls `vkQueuePresentKHR()` on behalf of your game.\n| **Note:** To achieve proper frame pacing, the Android Frame Pacing API can insert Vulkan commands into the presentation queue.\n\nDestroy the swapchain\n\nTo destroy the `SwappyVk` data associated with a given swapchain, complete the\nsteps shown in the following code snippet: \n\n```c++\n// Reusing local variables from previous snippets:\n// VkDevice device;\n// VkSwapchainKHR swapchain;\n// const VkAllocationCallbacks allocator;\n\nSwappyVk_destroySwapchain(device, swapchain);\nvkDestroySwapchainKHR(device, swapchain, &allocator);\n```"]]