We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
GuidesGraphicsXEngine KitMaleoon APIHigh-Performance GPU Sorting

High-Performance GPU Sorting

Since version 6.0.0(20), the High-Performance GPU Sorting feature is supported.

The High Performance Sorting (HPS) feature of XEngine Kit provides the high-performance GPU sorting capability. Compared with other sorting capabilities, this capability relies on the hardware-software co-optimization of the Maleoon GPU.

Constraints

You can check whether the relevant extension is supported as follows:

For Vulkan, use the extension query API HMS_XEG_EnumerateDeviceExtensionProperties. If the query result contains XEG_HPS_RADIX_SORT_EXTENSION_NAME, the extension is supported. Otherwise, the extension is not supported.

API Description

The following APIs are required for high-performance GPU sorting. For details about these APIs, please refer to API Reference.

Expand
API Description
VKAPI_ATTR VkResult VKAPI_CALL HMS_XEG_CreateHPS (VkDevice device, const XEG_HPSCreateInfo *pCreateInfo, XEG_HPS *pHps) Creates an XEG_HPS object.
VKAPI_ATTR void VKAPI_CALL HMS_XEG_DestroyHPS (XEG_HPS hps) Destroys an XEG_HPS object.
VKAPI_ATTR VkResult VKAPI_CALL HMS_XEG_CmdRadixSortHPS (VkCommandBuffer commandBuffer, XEG_HPS hps, const XEG_HPSRadixSortDescription *pDescription) Records the HPS sorting command. Before using this API, you need to call HMS_XEG_EnumerateDeviceExtensionProperties to check whether the XEG_HPS_RADIX_SORT_EXTENSION_NAME extension is supported.

Development Procedure

This section uses Vulkan app rendering as an example to describe how to develop high-performance GPU sorting.

Configuring Your Project

During HAP compilation, the native .so files depend on related libraries and header files of XEngine Kit in the NDK.

  • Header file reference

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. #include <algorithm>
    2. #include <vector>
    3. #include <string>
    4. #include <xengine/xeg_vulkan_hps.h>
    5. #include <xengine/xeg_vulkan_extension.h>
    6. #include <xengine/xeg_extension_defs.h>
  • Adding library dependencies to CMakeLists.txt

    Add the following code to CMakeLists.txt to add the dependency on the dynamic link library of XEngine Kit:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. find_library(
    2. # Set the name of the path variable.
    3. xengine-lib
    4. # Specify the name of the NDK library that you want CMake to locate.
    5. xengine
    6. )
    7. target_link_libraries(nativerender PUBLIC
    8. // Other library files
    9. // ...
    10. ${xengine-lib})

Integrating High-Performance GPU Sorting (Vulkan)

High-performance GPU sorting of XEngine Kit can be used independently. The related code is implemented at the native layer.

Before calling extension APIs of XEngine Kit, 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 high-performance GPU sorting APIs can be used only when the XEG_HPS_RADIX_SORT_EXTENSION_NAME extension is supported.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. VkPhysicalDevice physicalDevice;
    2. std::vector<std::string> supportedExtensions;
    3. uint32_t propertyCount;
    4. HMS_XEG_EnumerateDeviceExtensionProperties(physicalDevice, &propertyCount, nullptr);
    5. if (propertyCount > 0) {
    6. std::vector<XEG_ExtensionProperties> properties(propertyCount);
    7. if (HMS_XEG_EnumerateDeviceExtensionProperties(physicalDevice, &propertyCount, &properties.front()) ==
    8. VK_SUCCESS) {
    9. for (auto ext : properties) {
    10. supportedExtensions.push_back(ext.extensionName);
    11. }
    12. }
    13. }
    14. if (std::find(supportedExtensions.begin(), supportedExtensions.end(), XEG_HPS_RADIX_SORT_EXTENSION_NAME) ==
    15. supportedExtensions.end()) {
    16. exit(1);
    17. }
  2. Prepare HPS resources.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. VkDevice device;
    2. VkCommandBuffer cmdBuffer;
    3. VkQueue queue;
    4. // Key to be sorted.
    5. VkBuffer keyBuffer;
    6. // Value corresponding to the key.
    7. VkBuffer indexBuffer;
    8. // Sorting amount.
    9. VkBuffer sortCount;
  3. Declare the instance handle.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. XEG_HPS xegHPS { VK_NULL_HANDLE };
  4. Call HMS_XEG_CreateHPS to instantiate the handle.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // Construct the input descriptor.
    2. XEG_HPSRadixSort sorInfo{
    3. XEG_STRUCTURE_TYPE_HPS_RADIX_SORT,
    4. nullptr
    5. };
    6. XEG_HPSCreateInfo info {
    7. XEG_STRUCTURE_TYPE_HPS_CREATE_INFO,
    8. &sorInfo
    9. };
    10. // Instantiate the handle.
    11. HMS_XEG_CreateHPS(device, &info, &xegHPS);
  5. Construct the sorting descriptor and call the HMS_XEG_CmdRadixSortHPS API to record the sorting command.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. VkCommandBufferBeginInfo cmdBufferBeginInfo {};
    2. cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
    3. // Record the sorting command.
    4. vkBeginCommandBuffer(cmdBuffer, &cmdBufferBeginInfo);
    5. XEG_HPSRadixSortDescription sortDescription{
    6. XEG_STRUCTURE_TYPE_HPS_RADIX_SORT_DESCRIPTION,
    7. nullptr,
    8. sortCount,
    9. keyBuffer,
    10. indexBuffer
    11. };
    12. HMS_XEG_CmdRadixSortHPS(cmdBuffer, xegHPS, &sortDescription);
    13. vkEndCommandBuffer(cmdBuffer);
  6. Submit the sorting command.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // Submit the command buffer.
    2. VkResult res;
    3. {
    4. VkSubmitInfo submitInfo{};
    5. submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
    6. submitInfo.waitSemaphoreCount = 0;
    7. submitInfo.signalSemaphoreCount = 0;
    8. submitInfo.pSignalSemaphores = nullptr;
    9. submitInfo.commandBufferCount = 1;
    10. submitInfo.pCommandBuffers = &cmdBuffer;
    11. submitInfo.pWaitSemaphores = nullptr;
    12. res = vkQueueSubmit(queue, 1, &submitInfo, nullptr);
    13. }
    14. // Wait until the operation is complete.
    15. vkDeviceWaitIdle(device);
  7. Destroy the HPS object.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. if(xegHPS){
    2. HMS_XEG_DestroyHPS(xegHPS);
    3. }
Search in Guides
Enter a keyword.