The following figure shows the service process of frame generation in extrapolation mode using Vulkan.

A user enters a game scene suitable for frame generation.
The game app calls the HMS_FG_CreateContext_VK API to create a frame generation context instance. If the frame generation context instance fails to be created, the operations of alternately rendering and displaying real frames and predicted frames in step 6 to step 9 do not need to be performed. Instead, render and display real frames one by one.
The game app calls APIs to set properties of the frame generation context instance. For example, the game app calls HMS_FG_SetAlgorithmMode_VK to set the frame generation algorithm mode and select the extrapolation mode, calls HMS_FG_SetResolution_VK to set the resolution of the input and output images for frame generation, calls HMS_FG_SetCvvZSemantic_VK to set the Z/W range and depth test function of the homogeneous cropping space (if not called, the default setting is FG_CVV_Z_SEMANTIC_ZERO_TO_ONE_FORWARD_Z), calls HMS_FG_SetImageFormat_VK to set the image format for frame generation input and output (if not called, the image format is set to VK_FORMAT_R8G8B8A8_UNORM by default for the real frame color buffer and predicted frame buffer, and set to VK_FORMAT_D24_UNORM_S8_UINT by default for the depth stencil buffer). If the color buffer is flipped 180 degrees around the Y axis relative to the depth stencil buffer, the game app needs to call the HMS_FG_SetDepthStencilYDirectionInverted_VK API to set the flipping status (if not called, no flipping is performed by default).
The game app calls the HMS_FG_Activate_VK API to activate the frame generation context instance.
The game app calls the HMS_FG_CreateImage_VK API to create an image instance for the real frame color buffer, an image instance for the depth stencil buffer, and an image instance for the predicted frame buffer. This API is used to associate the VkImage and VkImageView image resources in the game app with the frame generation algorithm implementation.
The game app renders the game scene to obtain a real frame, and buffers the color, depth, and camera matrix information about the real frame for subsequent frame prediction.
The game app draws the UI and sends the real frame for display.
The game app calls the HMS_FG_Dispatch_VK API, passing the color, depth, and camera matrix information about historical real frames, to generate a predicted frame and update the predicted frame buffer.
The game app draws the predicted frame and its UI, and sends the predicted frame for display. Then, the game app repeats step 5 to this step until the user exits the game scene.
The user exits the game scene suitable for frame generation.
The game app calls the HMS_FG_DestroyContext_VK API to destroy the frame generation context instance and release memory resources.
The following describes an example of using the Vulkan graphics API for frame generation. For details about the code, see Graphics Development Sample (Frame Generation with Vulkan).
Include the Graphics Accelerate Kit frame generation header file frame_generation_vk.h.
// Include the frame generation header file frame_generation_vk.h. #include <graphics_game_sdk/frame_generation_vk.h>
Configure the CMakeLists.txt file.
find_library(framegeneration-lib libframegeneration.so REQUIRED)
find_library(vulkan-lib vulkan REQUIRED)
target_link_libraries(entry PUBLIC
${framegeneration-lib} ${vulkan-lib}
) Call the HMS_FG_CreateContext_VK API to create a frame generation context instance. If nullptr is returned, it indicates that the frame generation context instance fails to be created or the current device does not support the frame generation function.
// Variable declaration.
VkInstance vkInstance = VK_NULL_HANDLE; // vkInstance is created by calling vkCreateInstance.
VkPhysicalDevice vkPhysicalDevice = VK_NULL_HANDLE; // vkPhysicalDevice is enumerated by calling vkEnumeratePhysicalDevices.
VkDevice vkDevice = VK_NULL_HANDLE; // vkDevice is created by calling vkCreateDevice.
// Create a frame generation context instance.
FG_ContextDescription_VK contextDescription{};
contextDescription.vkInstance = vkInstance;
contextDescription.vkPhysicalDevice = vkPhysicalDevice;
contextDescription.vkDevice = vkDevice;
contextDescription.framesInFlight = 1;
contextDescription.fnVulkanLoaderFunction = vkGetInstanceProcAddr;
FG_Context_VK* m_context = HMS_FG_CreateContext_VK(&contextDescription);
if (m_context == nullptr) {
GOLOGE("HMS_FG_CreateContext_VK execution failed.");
return false;
} Call APIs to set attributes of the context instance. Set the frame generation algorithm mode to extrapolation.
// Initialize the error code of frame generation API calls.
FG_ErrorCode errorCode = FG_SUCCESS;
// Frame generation algorithm mode.
FG_AlgorithmModeInfo aInfo{};
aInfo.predictionMode = FG_PREDICTION_MODE_EXTRAPOLATION; // Extrapolation mode.
aInfo.meMode = FG_ME_MODE_BASIC; // Basic motion estimation mode.
errorCode = HMS_FG_SetAlgorithmMode_VK(m_context, &aInfo); // [Mandatory] Set the frame generation algorithm mode.
if (errorCode != FG_SUCCESS) {
GOLOGE("HMS_FG_SetAlgorithmMode_VK execution failed, error code: %d.", errorCode);
return false;
}
// Resolution of the real frame color buffer.
FG_Dimension2D inputColorResolution{};
inputColorResolution.width = m_swapChainExtent.width; // Image width of the real frame color buffer.
inputColorResolution.height = m_swapChainExtent.height; // Image height of the real frame color buffer.
// Resolution of the real frame depth stencil buffer.
FG_Dimension2D inputDepthStencilResolution{};
inputDepthStencilResolution.width = m_swapChainExtent.width; // Image width of the real frame depth stencil buffer.
inputDepthStencilResolution.height = m_swapChainExtent.height; // Image height of the real frame depth stencil buffer.
// Resolution of the predicted frame.
FG_Dimension2D outputColorResolution{};
outputColorResolution.width = m_swapChainExtent.width; // Image width of the predicted frame.
outputColorResolution.height = m_swapChainExtent.height; // Image height of the predicted frame.
// Input and output image resolutions.
FG_ResolutionInfo rInfo{};
rInfo.inputColorResolution = inputColorResolution;
rInfo.inputDepthStencilResolution = inputDepthStencilResolution;
rInfo.outputColorResolution = outputColorResolution;
errorCode = HMS_FG_SetResolution_VK(m_context, &rInfo); // Set the resolutions of input and output images.
if (errorCode != FG_SUCCESS) {
GOLOGE("HMS_FG_SetResolution_VK execution failed, error code: %d.", errorCode);
return false;
}
// Set the homogeneous clip space Z/W range and depth function. If this API is not called, the default value FG_CVV_Z_SEMANTIC_ZERO_TO_ONE_FORWARD_Z is used.
errorCode = HMS_FG_SetCvvZSemantic_VK(m_context, FG_CVV_Z_SEMANTIC_ZERO_TO_ONE_FORWARD_Z);
if (errorCode != FG_SUCCESS) {
GOLOGE("HMS_FG_SetCvvZSemantic_VK execution failed, error code: %d.", errorCode);
return false;
}
// Set the input and output image formats for frame generation.
FG_ImageFormat_VK imageFormat{};
imageFormat.inputColorFormat = m_swapChainImageFormat;
imageFormat.inputDepthStencilFormat = m_sceneDepthStencil.GetFormat();
imageFormat.outputColorFormat = m_swapChainImageFormat;
errorCode = HMS_FG_SetImageFormat_VK(m_context, &imageFormat);
if (errorCode != FG_SUCCESS) {
GOLOGE("HMS_FG_SetImageFormat_VK execution failed, error code: %d.", errorCode);
return false;
}
// If the color buffer is flipped 180 degrees around the Y axis relative to the depth stencil buffer, set the second parameter to true. If this API is not called, the default value false is used.
errorCode = HMS_FG_SetDepthStencilYDirectionInverted_VK(m_context, false);
if (errorCode != FG_SUCCESS) {
GOLOGE("HMS_FG_SetDepthStencilYDirectionInverted_VK execution failed, error code: %d.", errorCode);
return false;
} Call the HMS_FG_Activate_VK API to activate the frame generation context instance.
// Activate the frame generation context instance.
FG_ErrorCode errorCode = HMS_FG_Activate_VK(m_context);
if (errorCode != FG_SUCCESS) {
GOLOGE("HMS_FG_Activate_VK execution failed, error code: %d.", errorCode);
// ...
return false;
} Call the HMS_FG_CreateImage_VK API to create an image instance for the real frame color buffer, an image instance for the depth stencil buffer, and an image instance for the predicted frame buffer.
// Variable declaration.
FG_Image_VK *m_ffSceneColor = nullptr;
VulkanFG::Image m_sceneColor{};
FG_Image_VK *m_ffDepthStencil = nullptr;
VulkanFG::Image m_sceneDepthStencil{};
FG_Image_VK *m_ffPredictedColor = nullptr;
VulkanFG::Image m_predictedColor{}; // Create an image instance for the real frame color buffer.
m_ffSceneColor = HMS_FG_CreateImage_VK(m_context, m_sceneColor.GetNativeImage(), m_sceneColor.GetNativeImageView());
if (!m_ffSceneColor) {
GOLOGE("HMS_FG_CreateImage_VK m_ffSceneColor execution failed.");
return false;
}
// Create an image instance for the real frame depth stencil buffer.
m_ffDepthStencil = HMS_FG_CreateImage_VK(m_context, m_sceneDepthStencil.GetNativeImage(),
m_sceneDepthStencil.GetNativeImageView());
if (!m_ffDepthStencil) {
GOLOGE("HMS_FG_CreateImage_VK m_ffDepthStencil execution failed.");
return false;
}
// Create an image instance for the predicted frame buffer.
m_ffPredictedColor = HMS_FG_CreateImage_VK(m_context, m_predictedColor.GetNativeImage(),
m_predictedColor.GetNativeImageView());
if (!m_ffPredictedColor) {
GOLOGE("HMS_FG_CreateImage_VK m_ffPredictedColor execution failed.");
return false;
} Render and display the real frames and predicted frames alternately during game running. During real frame rendering, buffer their color, depth, and camera matrix information. During predicted frame rendering, call the HMS_FG_Dispatch_VK API, passing the properties of the previous real frame and specifying the predicted frame buffer index, to generate a predicted frame and update the memory of the predicted frame buffer.
// Variable declaration.
FG_Mat4x4 m_viewProj{};
FG_Mat4x4 m_invViewProj{};
FG_DispatchDescription_VK dispatch{}; // Real frame rendering phase.
// ...
// Render the game scene to obtain the current real frame. Buffer its color, depth, and camera matrix information for generating the next predicted frame.
// ...
// Draw the real frame and UI.
// ...
// Send the real frame for display.
// ...
bool const runPrediction = m_predictionEnabled & !m_predictionPaused;
if (runPrediction) { // Predicted frame rendering phase.
// ...
dispatch = {
// Pass the property information about the real frame color buffer.
.inputColorInfo = {
.image = m_ffSceneColor,
// Set the synchronization status of the real frame color buffer before the predicted frame is generated.
.initialSync {
.accessMask = VK_ACCESS_SHADER_READ_BIT,
.layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.stages = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
},
// Set the synchronization status of the real frame color buffer after the predicted frame is generated.
.finalSync {
.accessMask = VK_ACCESS_SHADER_READ_BIT,
.layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.stages = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
}
},
// Pass the property information about the real frame depth stencil buffer.
.inputDepthStencilInfo = {
.image = m_ffDepthStencil,
// Set the synchronization status of the depth stencil buffer before the predicted frame is generated.
.initialSync {
.accessMask = VK_ACCESS_SHADER_READ_BIT,
.layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.stages = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
},
// Set the synchronization status of the depth stencil buffer after the predicted frame is generated.
.finalSync {
.accessMask = VK_ACCESS_SHADER_READ_BIT,
.layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.stages = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
}
},
// Pass the property information about the predicted frame buffer.
.outputColorInfo = {
.image = m_ffPredictedColor,
// Set the synchronization status of the predicted frame buffer before the predicted frame is generated.
.initialSync {
.accessMask = VK_ACCESS_SHADER_READ_BIT,
.layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.stages = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
},
// Set the synchronization status of the predicted frame buffer after the predicted frame is generated.
.finalSync {
.accessMask = VK_ACCESS_SHADER_READ_BIT,
.layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
.stages = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT
}
},
// Pass the view projection matrix of the previous real frame.
.viewProj = m_viewProj,
// Pass the inverse view projection matrix of the previous real frame.
.invViewProj = m_invViewProj,
// Pass the handle of a command buffer used to record frame drawing commands.
.vkCommandBuffer = fif->commandBuffer,
// Pass the sequence number of the current frame.
.frameIdx = fifIndex
};
// ...
// Generate a predicted frame and update the memory of the predicted frame buffer.
FG_ErrorCode const errorCode = HMS_FG_Dispatch_VK(m_context, &dispatch);
if (errorCode != FG_SUCCESS) { // A predicted frame fails to be generated.
GOLOGE("HMS_FG_Dispatch_VK execution failed, error code: %d", errorCode);
m_unfinishedCommandBuffer = true;
return;
}
// Draw the predicted frame and UI.
// ...
// Send the predicted frame for display.
// ...
} Call the HMS_FG_DestroyContext_VK API to destroy the frame generation context instance and release memory resources.
// Destroy the frame generation context instance and release memory resources.
errorCode = HMS_FG_DestroyContext_VK(&m_context);
if (errorCode != FG_SUCCESS) {
GOLOGE("HMS_FG_DestroyContext_VK execution failed, error code: %d", errorCode);
return false;
}