目录

Adaptive VRS

XEngine Kit offers adaptive VRS, which optimizes resource allocation to reduce the rendering frequency without visual loss. This allows for varying rendering rates for different images, effectively improving rendering performance.

Constraints

API Description

The following lists APIs for adaptive VRS setting. For details about more setting and query APIs, please refer to API Reference.

API Description
const GLubyte * HMS_XEG_GetString (GLenum name) Queries OpenGL ES extensions of XEngine Kit.
GL_APICALL void GL_APIENTRY HMS_XEG_AdaptiveVRSParameter (GLenum pname, GLvoid * param) Sets adaptive VRS parameters.
GL_APICALL void GL_APIENTRY HMS_XEG_DispatchAdaptiveVRS (GLfloat * reprojectionMatrix, GLuint inputColorImage, GLuint inputDepthImage, GLuint shadingRateImage) Computes the shading rate image.
GL_APICALL void GL_APIENTRY HMS_XEG_ApplyAdaptiveVRS (GLuint shadingRateImage) Apply the shading rate image to the rendering target.
VKAPI_ATTR VkResult VKAPI_CALL HMS_XEG_EnumerateDeviceExtensionProperties (VkPhysicalDevice physicalDevice, uint32_t * pPropertyCount, XEG_ExtensionProperties * pProperties) Queries Vulkan extensions of XEngine Kit.
VKAPI_ATTR VkResult VKAPI_CALL HMS_XEG_CreateAdaptiveVRS (VkDevice device, XEG_AdaptiveVRSCreateInfo * pXegAdaptiveVRSCreateInfo, XEG_AdaptiveVRS * pXegAdaptiveVRS) Creates an XEG_AdaptiveVRS object.
VKAPI_ATTR void VKAPI_CALL HMS_XEG_CmdDispatchAdaptiveVRS (VkCommandBuffer commandBuffer, XEG_AdaptiveVRS xegAdaptiveVRS, XEG_AdaptiveVRSDescription * pXegAdaptiveVRSDescription) Executes the command for computing the adaptive VRS.
VKAPI_ATTR void VKAPI_CALL HMS_XEG_DestroyAdaptiveVRS (XEG_AdaptiveVRS xegAdaptiveVRS) Destroys an XEG_AdaptiveVRS object.

Service Process

  1. When a user enters the game's initialization scene, call the HMS_XEG_GetString API to query the list of features supported by XEngine Kit. Check whether the returned list contains XEG_ADAPTIVE_VRS_EXTENSION_NAME. If not, the current device does not support this feature, and the process ends.
  2. Call HMS_XEG_AdaptiveVRSParameter to set adaptive VRS parameters.
  3. When the game is running, it renders the texture of the current frame.
  4. Before using adaptive VRS, call the HMS_XEG_DispatchAdaptiveVRS API to compute the shading rate image.
  5. Call HMS_XEG_ApplyAdaptiveVRS to apply the shading rate image to the rendering target.
  6. The game renders the remaining textures, such as the UI. Perform the final presentation operation after the frame rendering is completed.
  7. When the game exits, the adaptive VRS resources are automatically released.
  1. When a user enters the game's initialization scene, call the HMS_XEG_EnumerateDeviceExtensionProperties API to query the list of features supported by XEngine Kit. Check whether the returned list contains XEG_ADAPTIVE_VRS_EXTENSION_NAME. If not, the current device does not support this feature, and the process ends.
  2. Call the HMS_XEG_CreateAdaptiveVRS API to create an adaptive VRS instance.
  3. When the adaptive VRS extension is used, Vulkan resources that support VRS need to be created.
  4. When the game is running, it renders the texture of the current frame.
  5. Call HMS_XEG_CmdDispatchAdaptiveVRS to compute the shading rate image.
  6. Apply the shading rate image to the rendering target.
  7. Render the remaining game textures, such as the UI. Perform the final presentation operation after the frame rendering is completed.
  8. When the game exits, call the HMS_XEG_DestroyAdaptiveVRS API to destroy the adaptive VRS instance.

Development Procedure

This section uses the OpenGL ES and Vulkan graphics APIs as examples to describe the XEngine Kit integration process.

[h2]Configuring Your Project

During HAP compilation, the native layer's .so files must be built with a dependency on libxengine.so from the NDK.

[h2]Integrating Adaptive VRS (OpenGL ES)

The creation and binding of shading rate textures in the adaptive VRS feature (OpenGL ES) are implemented through the APIs provided by the feature.

Before calling XEngine Kit capabilities, you need to use SysCap to check whether your target device supports the SystemCapability.Graphic.XEngine system capability.

  1. Call HMS_XEG_GetString to obtain the extensions supported by XEngine Kit. The adaptive VRS APIs can be used only when the XEG_ADAPTIVE_VRS_EXTENSION_NAME extension is supported.

    // Query GLES extensions supported by XEngine Kit.
    std::string extensionStr = (const char*)HMS_XEG_GetString(XEG_EXTENSIONS);
    std::vector<std::string> extensions;
    std::istringstream istringstream(extensionStr);
    std::string word;
    while (istringstream >> word) {
        extensions.push_back(word);
    }
        
    // ...
    // Check whether adaptive VRS is supported.
    if (std::find(extensions.begin(), extensions.end(), XEG_ADAPTIVE_VRS_EXTENSION_NAME) != extensions.end()) {
        // Normal processing logic.
        // ...
    } else {
        // Handle the error.
        // ...
    }
  2. Call HMS_XEG_AdaptiveVRSParameter to set adaptive VRS parameters.

    // inputWidth and inputHeight indicate the custom rendering width and height, respectively.
    // inputSize indicates the image size finally rendered by the rendering pipeline of the previous frame. You can customize this parameter as required.
    GLsizei inputSize[2] = {inputWidth, inputHeight};
    HMS_XEG_AdaptiveVRSParameter(XEG_ADAPTIVE_VRS_INPUT_SIZE, inputSize);
    // inputRegion indicates the image region finally rendered by the rendering pipeline of the previous frame. You can customize this parameter as required.
    GLuint inputRegion[4] = {0, 0, static_cast<GLuint>(inputWidth), static_cast<GLuint>(inputHeight)};
    HMS_XEG_AdaptiveVRSParameter(XEG_ADAPTIVE_VRS_INPUT_REGION, inputRegion);
    // flip indicates whether to flip an image up or down (true: flip; false: not flip). Here, the value false is used as an example.
    GLboolean flip = false;
    HMS_XEG_AdaptiveVRSParameter(XEG_ADAPTIVE_VRS_FLIP, &flip);
    // texelSize indicates the tile size for rendering. You can customize this parameter as required. The options are [8, 8] and [16, 16].
    GLsizei texelSize[2] = {ADAPTIVE_VRS_TEXEL_SIZE, ADAPTIVE_VRS_TEXEL_SIZE};
    HMS_XEG_AdaptiveVRSParameter(XEG_ADAPTIVE_VRS_TEXEL_SIZE, texelSize);
    // sensitivity indicates the threshold for controlling the generation of the shading rate image. You can customize this parameter as required. The recommended value range is [0.0, 1.0].
    GLfloat sensitivity = 0.3;
    HMS_XEG_AdaptiveVRSParameter(XEG_ADAPTIVE_VRS_ERROR_SENSITIVITY, &sensitivity);
  3. Call HMS_XEG_DispatchAdaptiveVRS to compute the shading rate image.

    // reprojectionM indicates the reprojection matrix computed based on the projection matrix and view matrix.
    // light indicates the texture of the color attachment containing the final rendering result of the rendering pipeline of the previous frame. You can customize this parameter as required.
    // depth indicates the texture of the depth attachment containing the final rendering result of the rendering pipeline of the current frame. You can customize this parameter as required.
    // sri indicates the custom texture used to generate the shading rate image information.
    HMS_XEG_DispatchAdaptiveVRS(reprojectionM, light, depth, sri);
  4. Call HMS_XEG_ApplyAdaptiveVRS to apply the shading rate image to the rendering target.

    HMS_XEG_ApplyAdaptiveVRS(sri);

[h2]Integrating Adaptive VRS (Vulkan)

Before calling XEngine Kit capabilities, you need to use SysCap to check whether your target device supports the SystemCapability.Graphic.XEngine system capability.

  1. Call HMS_XEG_EnumerateDeviceExtensionProperties to obtain the extensions supported by XEngine Kit. The adaptive VRS APIs can be used only when the XEG_ADAPTIVE_VRS_EXTENSION_NAME extension is supported.

    // Query the Vulkan extensions supported by XEngine Kit.
    std::vector<std::string> supportedExtensions;
    uint32_t pPropertyCount;
    // physicalDevice indicates a Vulkan physical device. You need to initialize it.
    HMS_XEG_EnumerateDeviceExtensionProperties(physicalDevice, &pPropertyCount, nullptr);
    if (pPropertyCount > 0) {
        std::vector<XEG_ExtensionProperties> pProperties(pPropertyCount);
        if (HMS_XEG_EnumerateDeviceExtensionProperties(physicalDevice, &pPropertyCount,
            &pProperties.front()) == VK_SUCCESS) {
            for (auto ext : pProperties) {
                supportedExtensions.push_back(ext.extensionName);
            }
        }
    }
    // ...
    // Check whether adaptive VRS is supported.
    if (std::find(supportedExtensions.begin(), supportedExtensions.end(), XEG_ADAPTIVE_VRS_EXTENSION_NAME) ==
        supportedExtensions.end()) {
        // Handle the error.
        // ...
    }
  2. Declare the instance handle.

    XEG_AdaptiveVRS xegAdaptiveVRS = nullptr;
  3. Call HMS_XEG_CreateAdaptiveVRS to define and create an instance.

    // VkExtent2D inputSize indicates the image size finally rendered by the rendering pipeline of the previous frame.
    // VkRect2D indicates the 2D area structure specified by Vulkan.
    // inputRegion indicates the input texture area for adaptive VRS. You can customize this parameter as required.
    VkExtent2D inputSize;
    VkRect2D inputRegion{};
    // highResWidth and highResHeight indicate the custom rendering width and height, respectively.
    // inputSize.width and inputSize.height indicate the width and height of the final rendered image of the previous frame, respectively.
    inputSize.width = highResWidth;
    inputSize.height = highResHeight;
    // inputRegion.extent.width and inputRegion.extent.height indicate the texture sampling width and height, respectively.
    inputRegion.extent.width = highResWidth;
    inputRegion.extent.height = highResHeight;
    // inputRegion.offset.x and inputRegion.offset.y indicate the offset from the origin.
    inputRegion.offset.x = 0;
    inputRegion.offset.y = 0;
    // xegCreateInfo.inputSize indicates the image size finally rendered by the rendering pipeline of the previous frame.
    xegCreateInfo.inputSize = inputSize;
    // xegCreateInfo.inputRegion indicates the image region finally rendered by the rendering pipeline of the previous frame.
    xegCreateInfo.inputRegion = inputRegion;
    // xegCreateInfo.adaptiveTileSize indicates the tile size for adaptive VRS rendering.
    // VRS_TILE_SIZE indicates the tile size for adaptive VRS rendering.
    xegCreateInfo.adaptiveTileSize = VRS_TILE_SIZE;
    // xegCreateInfo.errorSensitivity indicates the threshold for generating the final shading rate texture result.
    xegCreateInfo.errorSensitivity = SENSITIVITY;
    // xegCreateInfo.flip indicates whether to flip an image up or down (true: flip; false: not flip). Here, the value false is used as an example.
    xegCreateInfo.flip = false;
    // device indicates the logical device. You need to initialize it.
    // xegCreateInfo indicates the parameter information of the adaptive VRS instance handle object.
    // xeg_adaptiveVRS indicates the parameter information required for sending the command for drawing the shading rate texture.
    VkResult res = HMS_XEG_CreateAdaptiveVRS(device, &xegCreateInfo, &xegAdaptiveVRS);
  4. Call HMS_XEG_CmdDispatchAdaptiveVRS to deliver adaptive VRS commands and generate the perImage shading rate texture.

    XEG_AdaptiveVRSDescription adaptiveVRSDescription;
    // inputColorImageView indicates the custom texture of the color attachment containing the final rendering result of the rendering pipeline of the previous frame.
    adaptiveVRSDescription.inputColorImage = inputColorImageView;
    // inputDepthImageView indicates the custom texture of the depth attachment containing the final rendering result of the rendering pipeline of the current frame.
    adaptiveVRSDescription.inputDepthImage = inputDepthImageView;
    // outputShadingRateImage indicates the custom texture used to generate the shading rate image information.
    adaptiveVRSDescription.outputShadingRateImage = outputShadingRateImage;
    // adaptiveVRSDescription.reprojectionMatrix indicates the reprojection matrix computed based on the projection matrix and view matrix.
    adaptiveVRSDescription.reprojectionMatrix = nullptr;
    // ...
    if (xegAdaptiveVRS) {
        HMS_XEG_CmdDispatchAdaptiveVRS(commandBuffer, xegAdaptiveVRS, &adaptiveVRSDescription);
        // ...
    }
  5. Call HMS_XEG_DestroyAdaptiveVRS to destroy the VRS instance and clear VRS-related resources.

    HMS_XEG_DestroyAdaptiveVRS(xegAdaptiveVRS);