docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    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.GPUCommandBuffer back end 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 ExecuteWorker 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.

    using UnityEngine;
    using Unity.Sentis;
    using UnityEngine.Rendering;
    
    public class UseCommandBuffer : MonoBehaviour
    {
        public ModelAsset modelAsset;
        IWorker worker;
    
        void Start()
        {
            // Create a worker that uses the GPUCommandBuffer back end type
            worker = WorkerFactory.CreateWorker(BackendType.GPUCommandBuffer, ModelLoader.Load(modelAsset));
        }
    
        void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            // Create a new command buffer
            CommandBuffer commandBuffer = new CommandBuffer();
    
            // Add a command to the command buffer that creates an input tensor from the source texture data
            // you can also call TextureConverter.ToTensor(commandBuffer, source, inputTensor) if you allready have the tensor allocated
            Tensor inputTensor = TextureConverter.ToTensor(commandBuffer, source);
    
            // Add a command to the command buffer that runs the model using the input tensor
            commandBuffer.ExecuteWorker(worker, inputTensor);
    
            // Add a command to the command buffer that blits the output to the destination render texture
            TextureConverter.RenderToTexture(commandBuffer, worker.PeekOutput() as TensorFloat, destination);
    
            // Execute all the commands in the command buffer
            Graphics.ExecuteCommandBuffer(commandBuffer);
    
            // Clear the input tensor to free its memory.
            inputTensor.Dispose();
        }
    
        void OnDisable()
        {
            worker.Dispose();
        }
    }
    

    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 © 2024 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)