[[["容易理解","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-07-27 (世界標準時間)。"],[],[],null,["# Migrate scripts to Vulkan\n\nFor workloads where GPU compute is ideal, migrating RenderScript scripts to\nVulkan compute gives your app more direct control over GPU hardware, potentially\nunlocking additional performance compared to other APIs.\n| **Note:** Vulkan is only accessible from within the NDK. If you want to use Kotlin or Java to write your GPU compute code, SDK bindings for OpenGL ES 3.1 compute are available.\n\nA high-level overview follows to help you use Vulkan compute\nshaders to replace RenderScript scripts.\n\nVulkan Initialization\n---------------------\n\nInstead of creating a RenderScript context object in Kotlin or Java, perform the\nfollowing steps to create a Vulkan context using the NDK.\n\n1. Create a Vulkan instance.\n\n2. Choose a Vulkan physical device that supports a compute queue.\n\n3. Create a Vulkan logical device, and get the compute queue.\n\nOptionally, you can set up the Vulkan validation layers on Android to speed up\nyour Vulkan application development.\n\nThe sample app demonstrates how to initialize the Vulkan context in [`VulkanContext.h`](https://github.com/android/renderscript-samples/blob/main/RenderScriptMigrationSample/app/src/main/cpp/VulkanContext.h).\nTo learn more, see the [Initialization](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap4.html#initialization) and [Devices and Queues](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap5.html#devsandqueues) sections of the Vulkan specification.\n\nVulkan Allocations\n------------------\n\nA RenderScript Allocation can be migrated to a [Vulkan storage image](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap14.html#descriptorsets-storageimage)\nor a [Vulkan storage buffer](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap14.html#descriptorsets-storagebuffer).\nFor better performance with read-only images, use a sampled image with fetch\noperations, either as a [combined image sampler](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap14.html#descriptorsets-combinedimagesampler),\nor with distinct [sampler](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap14.html#descriptorsets-sampler)\nand [sampled image](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap14.html#descriptorsets-sampledimage)\nbindings.\n\nThe Vulkan resources are allocated within Vulkan. To avoid memory copying\noverhead when interacting with other Android components, consider using the\n[`VK_ANDROID_external_memory_android_hardware_buffer`\nextension](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VK_ANDROID_external_memory_android_hardware_buffer.html)\nto import an Android\n[`AHardwareBuffer`](/ndk/reference/group/a-%0Ahardware-buffer#ahardwarebuffer)\ninto Vulkan. This extension is available on all Android devices supporting Vulkan\n1.1. For more information, see [`FEATURE_VULKAN_HARDWARE_VERSION`](/reference/%0Aandroid/content/pm/PackageManager#FEATURE_VULKAN_HARDWARE_VERSION).\n\nThe sample app demonstrates how to create Vulkan resources in\n[`VulkanResources.h`](https://github.com/android/renderscript-samples/blob/main/RenderScriptMigrationSample/app/src/main/cpp/VulkanResources.h).\nTo learn more, see the [resource creation](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap12.html#resources)\nand [resource descriptors](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap14.html#descriptorsets)\nsections of the Vulkan specification.\n\nConversion to Vulkan compute shaders\n------------------------------------\n\nYour RenderScript scripts must be converted to Vulkan compute shaders. You\nmay also need to adapt your code depending on the use of RenderScript globals.\n\n### Write a Vulkan compute shader\n\nA Vulkan compute shader is commonly written in [OpenGL Shading Language](https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language)\n(GLSL) and then compiled to the [Standard Portable Intermediate Representation-V](https://www.khronos.org/opengl/wiki/SPIR-V) (SPIR-V) format.\n\nFor detailed information and instructions for integrating shaders into your app,\nsee [Vulkan shader compilers on Android](/ndk/guides/graphics/shader-compilers).\n| **Note:** Unlike a RenderScript script, a GLSL shader script can't have any invokable functions, and must have only one kernel.\n\n### Adaptation of script globals\n\nBased on the characteristics of the script globals, we recommend using\nspecialization constants, push constants, or uniform buffer objects for globals\nthat are not modified within the shader:\n\n- [Specialization constants](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap10.html#pipelines-specialization-constants): Recommended for script globals that are mostly consistent across kernel invocations. Changing the value of specialization constants will need to recreate the compute pipeline.\n- [Push constants](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap14.html#descriptorsets-push-constants): Recommended for frequently-changed script globals of sizes smaller than [`maxPushConstantsSize`](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkPhysicalDeviceLimits.html) (guaranteed minimum: 128 bytes).\n- [Uniform buffer](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap14.html#descriptorsets-uniformbuffer): Recommended for frequently-changed script globals of sizes larger than the push constant limit.\n\nFor globals that are changed within the shader, you can use either the [Vulkan storage image](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap14.html#descriptorsets-storageimage)\nor the [Vulkan storage buffer](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap14.html#descriptorsets-storagebuffer).\n\nComputations\n------------\n\nYou need to create a Vulkan compute pipeline in order to have the GPU execute\nyour compute shader.\n\n### Create a Vulkan compute pipeline\n\nThe [`ComputePipeline.h`](https://github.com/android/renderscript-samples/blob/main/RenderScriptMigrationSample/app/src/main/cpp/ComputePipeline.h)\nfile in the sample app demonstrates how to create the Vulkan compute pipeline.\n\nTo use a compiled SPIR-V shader in Vulkan, construct a Vulkan compute pipeline\nas follows:\n\n1. Create a shader module with the compiled SPIR-V shader.\n2. Create a descriptor set layout specifying the resource bindings (see [Allocations](#allocations) for more details).\n3. Create a descriptor set from the descriptor set layout.\n4. Create a pipeline layout from the descriptor set layout.\n5. Create a compute pipeline with the shader module and pipeline layout.\n\nFor more information, see the [Compute Pipelines](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap10.html#pipelines-compute)\nsection in the Vulkan specification.\n\n### Start a computation\n\nTo start the computation with a compute pipeline:\n\n1. Update the descriptor set with the Vulkan resources.\n2. Create a Vulkan command buffer, and record the following commands:\n 1. Bind the pipeline and the descriptor set.\n 2. Dispatch compute workgroups.\n3. Submit the command buffer to the compute queue.\n4. Wait on the queue, or optionally return a sync fence.\n\nTo chain multiple kernels together (for example, to migrate codes using\n`ScriptGroup`), record them in a single command buffer and synchronize with\nmemory barriers.\n\nThe [sample app](https://github.com/android/renderscript-samples/tree/main/RenderScriptMigrationSample/)\ndemonstrates two compute tasks:\n\n- HUE rotation: A simple compute task with a single compute shader. See `ImageProcessor::rotateHue` for the code sample.\n- Blur: A more complex compute task that sequentially executes two compute shaders. See `ImageProcessor::blur` for the code sample.\n\nTo learn more about command buffers or memory barriers, refer to the sections in\nthe Vulkan specification called [Command Buffers](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap6.html#commandbuffers)\nand [Memory Barriers](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/chap7.html#synchronization-memory-barriers)."]]