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:
- Create or get an empty command buffer.
- Create a worker with the
BackendType.GPUCommandBuffer
back end type. - In a Unity event function, for example
OnRenderImage
, add Sentis methods to the command buffer. - 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.