r.Mobile.MeshSortingMethod

r.Mobile.MeshSortingMethod

#Overview

name: r.Mobile.MeshSortingMethod

This variable is created as a Console Variable (cvar).

It is referenced in 3 C++ source files.

#Summary

#Usage in the C++ source code

The purpose of r.Mobile.MeshSortingMethod is to control how mesh commands are sorted on mobile platforms in Unreal Engine 5’s rendering system.

This setting variable is primarily used in the Renderer module of Unreal Engine 5, specifically in the mesh drawing and sorting subsystem. Based on the callsites, it’s clear that this variable is crucial for optimizing rendering performance on mobile devices.

The value of this variable is set through a console variable (CVar) system, which allows it to be changed at runtime. It’s defined with a default value of 0, meaning it will sort by state and roughly front to back by default.

The associated variable CVarMobileMeshSortingMethod directly interacts with r.Mobile.MeshSortingMethod. They share the same value and purpose, with CVarMobileMeshSortingMethod being the actual C++ variable used in the code to access the setting.

Developers must be aware that this variable has two possible values: 0: Sort by state, roughly front to back (Default) 1: Strict front to back sorting

The choice between these two methods can significantly impact rendering performance and visual quality on mobile devices.

Best practices when using this variable include:

  1. Carefully consider the trade-offs between performance and visual quality when choosing a sorting method.
  2. Test thoroughly on target mobile devices to ensure the chosen method provides the best balance of performance and visual quality for your specific game or application.
  3. Be aware that changing this value at runtime may have performance implications, so it’s generally best to set it once at the start of the application unless dynamic adjustment is necessary.

Regarding CVarMobileMeshSortingMethod, it’s the C++ representation of the r.Mobile.MeshSortingMethod console variable. It’s used in the actual implementation to retrieve the current sorting method value. Developers should use this variable when they need to access the sorting method value in C++ code, as seen in the UpdateMobilePassMeshSortKeys function.

When working with CVarMobileMeshSortingMethod, developers should:

  1. Use GetValueOnAnyThread() to safely retrieve the current value in a thread-safe manner.
  2. Be aware that changes to this variable will affect all mobile rendering that relies on mesh sorting.
  3. Consider performance implications when frequently accessing or changing this value, especially in performance-critical code paths.

#References in C++ code

#Callsites

This variable is referenced in the following C++ source code:

#Loc: <Workspace>/Engine/Source/Runtime/Renderer/Private/MeshDrawCommands.cpp:23

Scope: file

Source code excerpt:


static TAutoConsoleVariable<int32> CVarMobileMeshSortingMethod(
	TEXT("r.Mobile.MeshSortingMethod"),
	0,
	TEXT("How to sort mesh commands on mobile:\n")
	TEXT("\t0: Sort by state, roughly front to back (Default).\n")
	TEXT("\t1: Strict front to back sorting.\n"),
	ECVF_RenderThreadSafe);

#Associated Variable and Callsites

This variable is associated with another variable named CVarMobileMeshSortingMethod. They share the same value. See the following C++ source code.

#Loc: <Workspace>/Engine/Source/Runtime/Renderer/Private/MeshDrawCommands.cpp:22

Scope: file

Source code excerpt:

	ECVF_RenderThreadSafe);

static TAutoConsoleVariable<int32> CVarMobileMeshSortingMethod(
	TEXT("r.Mobile.MeshSortingMethod"),
	0,
	TEXT("How to sort mesh commands on mobile:\n")
	TEXT("\t0: Sort by state, roughly front to back (Default).\n")
	TEXT("\t1: Strict front to back sorting.\n"),
	ECVF_RenderThreadSafe);

#Loc: <Workspace>/Engine/Source/Runtime/Renderer/Private/MeshDrawCommands.cpp:338

Scope (from outer to inner):

file
function     void UpdateMobilePassMeshSortKeys

Source code excerpt:


	int32 NumCmds = VisibleMeshCommands.Num();
	int32 MeshSortingMethod = CVarMobileMeshSortingMethod.GetValueOnAnyThread();
	
	if (MeshSortingMethod == 1) //strict front to back sorting
	{
		// compute sort key for each mesh command
		for (int32 CmdIdx = 0; CmdIdx < NumCmds; ++CmdIdx)
		{