docs.unity3d.com
Search Results for

    Show / Hide Table of Contents
    Note

    Sentis is now called Inference Engine. The documentation has moved to https://docs.unity3d.com/Packages/com.unity.ai.inference@latest. Refer to the new location for the latest updates and guidance. Make sure to update your bookmarks and references accordingly.

    Use a command buffer

    You can use a command buffer to create a queue of Sentis commands, which can then be executed on the GPU at a later time.

    Follow these steps:

    1. Create or get an empty command buffer.
    2. Create a worker with the BackendType.GPUCompute backend type.
    3. In a Unity event function, for example OnRenderImage, add Sentis methods to the command buffer.
    4. Use Graphics.ExecuteCommandBuffer to execute the command buffer.

    To add commands to the command buffer, do any of the following:

    • Use a Sentis API that takes the command buffer as a parameter, for example TextureConverter.ToTensor.
    • Use the ScheduleWorker method on the command buffer to add a command that runs the model.

    For example, the following code creates and executes a command buffer queue. This queue first converts a render texture to an input tensor, then runs the model, and finally converts the output tensor back to a render texture.

    // Create a worker that uses the GPUCompute backend type
    worker = new Worker(ModelLoader.Load(modelAsset), BackendType.GPUCompute);
    
    //...
    
    // Add model execution command buffer that runs the model using the input tensor
    commandBuffer.ScheduleWorker(worker, inputs);
    

    Build the CommandBuffer once, update the input every frame and execute the CommandBuffer. This reduces the scheduling time by half.

    Tensor<float> input0 = new Tensor<float>(new TensorShape(1));
    Model model;
    CommandBuffer cb;
    Worker worker;
    
    void Start()
    {
        worker = new Worker(model, BackendType.GPUCompute);
    
        worker.SetInput("input0", input0);
    
        cb = new CommandBuffer();
        cb.ScheduleWorker(worker);
    }
    
    void Update()
    {
        // modify input0
        input0.Upload(new float[] { Time.deltaTime });
        Graphics.ExecuteCommandBuffer(cb);
    }
    

    For more information, refer to:

    • Extending the Built-in Render Pipeline with CommandBuffers
    • The Unity Discussions group for the Sentis beta, which has a full sample project that uses command buffers.
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)