GPU Instancing
GPU instancing is an optimization where multiple instances of the same model with the same material can be drawn in a single draw call. This is a big optimization when rendering things that appear many times in a scene such as foliage.
GPU instancing is an optimization where multiple instances of the same model with the same material can be drawn in a single draw call. This is a big optimization when rendering things that appear many times in a scene such as foliage.
Standard Instancing
Everything is automatically instanced by default, if the renderer can batch your models into 1 draw call it will.
To get the ObjectโWorld matrix in your vertex shader you use the following function:
float3x4 CalculateInstancingObjectToWorldMatrix( const VS_INPUT i );
Extra per instance data can be grabbed by using:
ExtraShaderData_t GetExtraPerInstanceShaderData( const VS_INPUT i );
ExtraShaderData allows you to still have instances even though some data is different between them, currently only tint color is changeable within instances:
struct ExtraShaderData_t
{
float4 vTint;
uint nBlendWeightCount; // if D_SKINNING, blend weight count
};
Instances can be drawn manually in C# with: Graphics.DrawModelInstanced( Model, Span<Transform> ) this allows you to draw 1000s of instances without burdening the scene system.
Procedural Instancing
Procedural instancing is useful for programmatic shaders that are to be invoked from C# or indirectly. No transforms are passed only the count of instance ids, you would derive some form of transform yourself from them within the shader.
You can get the instance id from SV_InstanceID in your vertex function.
This is also useful for indirect draw calls.
PixelInput MainVs( VertexInput i, uint instanceID : SV_InstanceID )
{
float3 offsetPosition = float3( 0, 0, 64 * instanceID );
}
See:
Referenced API
Canonical API pages mentioned in this guide.
A struct containing a position, rotation and scale. This is commonly used in engine to describe entity position, bone position and scene object position.
Represents a 4x4 matrix.
A GPU data buffer intended for use with a `Sandbox.ComputeShader`. You can read and write arbitrary data to and from the CPU and GPU. This allows for efficient parallel data processing on the GPU. Different GPU buffer types can be used depending on the provided `Sandbox.GpuBuffer.UsageFlags`. Using the default `Sandbox.GpuBuffer.UsageFlags.Structured` type buffers map to StructuredBuffer and RWStructuredBuffer in HLSL.
Used to render to the screen using your Graphics Card, or whatever you kids are using in your crazy future computers. Whatever it is I'm sure it isn't fungible and everyone has free money and no-one has to ever work.
A model.
No summary available.
Draws multiple instances of a model using GPU instancing with the number of instances being provided by indirect draw arguments. Use `SV_InstanceID` semantic in shaders to access the rendered instance.
Draws multiple instances of a model using GPU instancing. This is similar to `Sandbox.Graphics.DrawModelInstancedIndirect(Sandbox.Model,Sandbox.GpuBuffer,System.Int32,Sandbox.RenderAttributes)`, except the count is provided from the CPU rather than via a GPU buffer. Use `SV_InstanceID` semantic in shaders to access the rendered instance.