BatchRendererGroup (BRG) doesn’t automatically provide any instance data. Instance data includes many properties which are normally built in for GameObjectsThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary, such as transform matrices, light probeLight probes store information about how light passes through space in your scene. A collection of light probes arranged within a given space can improve lighting on moving objects and static LOD scenery within that space. More info
See in Glossary coefficients, and lightmapA pre-rendered texture that contains the effects of light sources on static objects in the scene. Lightmaps are overlaid on top of scene geometry to create the effect of lighting. More info
See in Glossary texture coordinates. This means features like ambient lighting only work if you provide instance data yourself. To do this, you add and configure batches. A batch is a collection of instances, where each instance corresponds to a single thing to render. What the instance actually represents depends on what you want to render. For example, in a prop object renderer, an instance could represent a single prop, and in a terrainThe landscape in your scene. A Terrain GameObject adds a large flat plane to your scene and you can use the Terrain’s Inspector window to create a detailed landscape. More info
See in Glossary renderer, it could represent a single terrain patch.
Each batch has a set of metadata values and a single GraphicsBuffer, which every instance in the batch shares. To load data for an instance, the typical process is to use the metadata values to load from the correct location in the GraphicsBuffer. The UNITY_ACCESS_DOTS_INSTANCED_PROP
family of shaderA program that runs on the GPU. More info
See in Glossary macros work with this scheme (see Accessing DOTS Instanced properties). However, you don’t need to use this per-instance data loading scheme, and you are free to implement your own scheme if you want.
To create a batch, use BatchRendererGroup.AddBatch. The method receives an array of metadata values as well as a handle to a GraphicsBuffer. Unity passes the metadata values to the shader when it renders instances from the batch, and binds the GraphicsBuffer as unity_DOTSInstanceData
. For metadata values that the shader uses but you don’t pass in when you create a batch, Unity sets them to zero.
You can’t modify batch metadata values after you create them, so any metadata values you pass to the batch are final. If you need to change any metadata values, create a new batch and remove the old one. You can modify the batch’s GraphicsBuffer at any time. To do this, use SetBatchBuffer. This can be useful to resize buffers and allocate a larger buffer if the existing one runs out of space.
Note: You don’t need to specify the size of a batch when you create one. Instead, you have to make sure that the shader can correctly handle the instance indices you pass to it. What this means depends on the shader. For Unity-provided SRP shaders, this means that there must be valid instance data in the buffer at the index you pass.
See the following code sample for an example of how to create a batch with metadata values and a GraphicsBuffer of instance data. This code sample builds on the one in Registering meshes and materials.
using System; using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; using Unity.Jobs; using UnityEngine; using UnityEngine.Rendering; public class SimpleBRGExample : MonoBehaviour { public Mesh mesh; public Material material; private BatchRendererGroup m_BRG; private GraphicsBuffer m_InstanceData; private BatchID m_BatchID; private BatchMeshID m_MeshID; private BatchMaterialID m_MaterialID; // Some helper constants to make calculations more convenient. private const int kSizeOfMatrix = sizeof(float) * 4 * 4; private const int kSizeOfPackedMatrix = sizeof(float) * 4 * 3; private const int kSizeOfFloat4 = sizeof(float) * 4; private const int kBytesPerInstance = (kSizeOfPackedMatrix * 2) + kSizeOfFloat4; private const int kExtraBytes = kSizeOfMatrix * 2; private const int kNumInstances = 3; // The PackedMatrix is a convenience type that converts matrices into // the format that Unity-provided SRP shaders expect. struct PackedMatrix { public float c0x; public float c0y; public float c0z; public float c1x; public float c1y; public float c1z; public float c2x; public float c2y; public float c2z; public float c3x; public float c3y; public float c3z; public PackedMatrix(Matrix4x4 m) { c0x = m.m00; c0y = m.m10; c0z = m.m20; c1x = m.m01; c1y = m.m11; c1z = m.m21; c2x = m.m02; c2y = m.m12; c2z = m.m22; c3x = m.m03; c3y = m.m13; c3z = m.m23; } } private void Start() { m_BRG = new BatchRendererGroup(this.OnPerformCulling, IntPtr.Zero); m_MeshID = m_BRG.RegisterMesh(mesh); m_MaterialID = m_BRG.RegisterMaterial(material); AllocateInstanceDataBuffer(); PopulateInstanceDataBuffer(); } private void AllocateInstanceDataBuffer() { m_InstanceData = new GraphicsBuffer(GraphicsBuffer.Target.Raw, BufferCountForInstances(kBytesPerInstance, kNumInstances, kExtraBytes), sizeof(int)); } private void PopulateInstanceDataBuffer() { // Place a zero matrix at the start of the instance data buffer, so loads from address 0 return zero. var zero = new Matrix4x4[1] { Matrix4x4.zero }; // Create transform matrices for three example instances. var matrices = new Matrix4x4[kNumInstances] { Matrix4x4.Translate(new Vector3(-2, 0, 0)), Matrix4x4.Translate(new Vector3(0, 0, 0)), Matrix4x4.Translate(new Vector3(2, 0, 0)), }; // Convert the transform matrices into the packed format that the shader expects. var objectToWorld = new PackedMatrix[kNumInstances] { new PackedMatrix(matrices[0]), new PackedMatrix(matrices[1]), new PackedMatrix(matrices[2]), }; // Also create packed inverse matrices. var worldToObject = new PackedMatrix[kNumInstances] { new PackedMatrix(matrices[0].inverse), new PackedMatrix(matrices[1].inverse), new PackedMatrix(matrices[2].inverse), }; // Make all instances have unique colors. var colors = new Vector4[kNumInstances] { new Vector4(1, 0, 0, 1), new Vector4(0, 1, 0, 1), new Vector4(0, 0, 1, 1), }; // In this simple example, the instance data is placed into the buffer like this: // Offset | Description // 0 | 64 bytes of zeroes, so loads from address 0 return zeroes // 64 | 32 uninitialized bytes to make working with SetData easier, otherwise unnecessary // 96 | unity_ObjectToWorld, three packed float3x4 matrices // 240 | unity_WorldToObject, three packed float3x4 matrices // 384 | _BaseColor, three float4s // Calculates start addresses for the different instanced properties. unity_ObjectToWorld starts // at address 96 instead of 64, because the computeBufferStartIndex parameter of SetData // is expressed as source array elements, so it is easier to work in multiples of sizeof(PackedMatrix). uint byteAddressObjectToWorld = kSizeOfPackedMatrix * 2; uint byteAddressWorldToObject = byteAddressObjectToWorld + kSizeOfPackedMatrix * kNumInstances; uint byteAddressColor = byteAddressWorldToObject + kSizeOfPackedMatrix * kNumInstances; // Upload the instance data to the GraphicsBuffer so the shader can load them. m_InstanceData.SetData(zero, 0, 0, 1); m_InstanceData.SetData(objectToWorld, 0, (int)(byteAddressObjectToWorld / kSizeOfPackedMatrix), objectToWorld.Length); m_InstanceData.SetData(worldToObject, 0, (int)(byteAddressWorldToObject / kSizeOfPackedMatrix), worldToObject.Length); m_InstanceData.SetData(colors, 0, (int)(byteAddressColor / kSizeOfFloat4), colors.Length); // Set up metadata values to point to the instance data. Set the most significant bit 0x80000000 in each // which instructs the shader that the data is an array with one value per instance, indexed by the instance index. // Any metadata values that the shader uses that are not set here will be 0. When a value of 0 is used with // UNITY_ACCESS_DOTS_INSTANCED_PROP (i.e. without a default), the shader interprets the // 0x00000000 metadata value and loads from the start of the buffer. The start of the buffer is // a zero matrix so this sort of load is guaranteed to return zero, which is a reasonable default value. var metadata = new NativeArray<MetadataValue>(3, Allocator.Temp); metadata[0] = new MetadataValue { NameID = Shader.PropertyToID("unity_ObjectToWorld"), Value = 0x80000000 | byteAddressObjectToWorld, }; metadata[1] = new MetadataValue { NameID = Shader.PropertyToID("unity_WorldToObject"), Value = 0x80000000 | byteAddressWorldToObject, }; metadata[2] = new MetadataValue { NameID = Shader.PropertyToID("_BaseColor"), Value = 0x80000000 | byteAddressColor, }; // Finally, create a batch for the instances and make the batch use the GraphicsBuffer with the // instance data as well as the metadata values that specify where the properties are. m_BatchID = m_BRG.AddBatch(metadata, m_InstanceData.bufferHandle); } // Raw buffers are allocated in ints. This is a utility method that calculates // the required number of ints for the data. int BufferCountForInstances(int bytesPerInstance, int numInstances, int extraBytes = 0) { // Round byte counts to int multiples bytesPerInstance = (bytesPerInstance + sizeof(int) - 1) / sizeof(int) * sizeof(int); extraBytes = (extraBytes + sizeof(int) - 1) / sizeof(int) * sizeof(int); int totalBytes = bytesPerInstance * numInstances + extraBytes; return totalBytes / sizeof(int); } private void OnDisable() { m_BRG.Dispose(); } public unsafe JobHandle OnPerformCulling( BatchRendererGroup rendererGroup, BatchCullingContext cullingContext, BatchCullingOutput cullingOutput, IntPtr userContext) { // This simple example doesn't use jobs, so it can just return an empty JobHandle. // Performance-sensitive applications should use Burst jobs to implement // culling and draw command output. In this case, this function would return a // handle here that completes when the Burst jobs finish. return new JobHandle(); } }
Now that you have registered all the required resources with a BatchRendererGroup object, you can create draw commands. For more information, see the next topic, Creating draw commands.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.