Skip to content

Commit

Permalink
Merge pull request #7978 from Unity-Technologies/internal/2022.3/staging
Browse files Browse the repository at this point in the history
Internal/2022.3/staging
  • Loading branch information
UnityAljosha authored Oct 16, 2023
2 parents b9efb1d + ff70467 commit 9a3412a
Show file tree
Hide file tree
Showing 47 changed files with 734 additions and 251 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using UnityEngine;

namespace UnityEditor.Rendering
{
/// <summary> Set of helpers for AssetDatabase operations. </summary>
public static class AssetDatabaseHelper
{
/// <summary>
/// Finds all assets of type T in the project.
/// </summary>
/// <param name="extension">Asset type extension i.e ".mat" for materials</param>
/// <typeparam name="T">The type of material you are looking for</typeparam>
/// <returns>A IEnumerable object</returns>
public static IEnumerable<T> FindAssets<T>(string extension = null)
{
string typeName = typeof(T).ToString();
int i = typeName.LastIndexOf('.');
if (i != -1)
{
typeName = typeName.Substring(i+1, typeName.Length - i-1);
}

string query = !string.IsNullOrEmpty(extension) ? $"t:{typeName} glob:\"**/*{extension}\"" : $"t:{typeName}";

foreach (var guid in AssetDatabase.FindAssets(query))
{
var asset = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(guid));
if (asset is T castAsset)
yield return castAsset;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 40 additions & 34 deletions Packages/com.unity.render-pipelines.core/Editor/MaterialUpgrader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public class MaterialUpgrader
string m_OldShader;
string m_NewShader;

private static string[] s_PathsWhiteList = new[]
{
"Hidden/",
"HDRP/",
"Shader Graphs/"
};

/// <summary>
/// Retrieves path to new shader.
/// </summary>
Expand Down Expand Up @@ -309,20 +316,6 @@ public void RenameKeywordToFloat(string oldName, string newName, float setVal, f
m_KeywordFloatRename.Add(new KeywordFloatRename { keyword = oldName, property = newName, setVal = setVal, unsetVal = unsetVal });
}

/// <summary>
/// Checking if the passed in value is a path to a Material.
/// </summary>
/// <param name="path">Path to test.</param>
/// <return>Returns true if the passed in value is a path to a material.</return>
static bool IsMaterialPath(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(nameof(path));
}
return path.EndsWith(".mat", StringComparison.OrdinalIgnoreCase);
}

static MaterialUpgrader GetUpgrader(List<MaterialUpgrader> upgraders, Material material)
{
if (material == null || material.shader == null)
Expand Down Expand Up @@ -364,6 +357,29 @@ static bool ShouldUpgradeShader(Material material, HashSet<string> shaderNamesTo
return !shaderNamesToIgnore.Contains(material.shader.name);
}


private static bool IsNotAutomaticallyUpgradable(List<MaterialUpgrader> upgraders, Material material)
{
return GetUpgrader(upgraders, material) == null && !material.shader.name.ContainsAny(s_PathsWhiteList);
}


/// <summary>
/// Checking if project folder contains any materials that are not using built-in shaders.
/// </summary>
/// <param name="upgraders">List if MaterialUpgraders</param>
/// <returns>Returns true if at least one material uses a non-built-in shader (ignores Hidden, HDRP and Shader Graph Shaders)</returns>
public static bool ProjectFolderContainsNonBuiltinMaterials(List<MaterialUpgrader> upgraders)
{
foreach (var material in AssetDatabaseHelper.FindAssets<Material>(".mat"))
{
if(IsNotAutomaticallyUpgradable(upgraders, material))
return true;
}

return false;
}

/// <summary>
/// Upgrade the project folder.
/// </summary>
Expand All @@ -388,31 +404,21 @@ public static void UpgradeProjectFolder(List<MaterialUpgrader> upgraders, HashSe
if ((!Application.isBatchMode) && (!EditorUtility.DisplayDialog(DialogText.title, "The upgrade will overwrite materials in your project. " + DialogText.projectBackMessage, DialogText.proceed, DialogText.cancel)))
return;

int totalMaterialCount = 0;
foreach (string s in UnityEditor.AssetDatabase.GetAllAssetPaths())
{
if (IsMaterialPath(s))
totalMaterialCount++;
}

var materialAssets = AssetDatabase.FindAssets($"t:{nameof(Material)} glob:\"**/*.mat\"");
int materialIndex = 0;
foreach (string path in UnityEditor.AssetDatabase.GetAllAssetPaths())
{
if (IsMaterialPath(path))
{
materialIndex++;
if (UnityEditor.EditorUtility.DisplayCancelableProgressBar(progressBarName, string.Format("({0} of {1}) {2}", materialIndex, totalMaterialCount, path), (float)materialIndex / (float)totalMaterialCount))
break;

Material m = UnityEditor.AssetDatabase.LoadMainAssetAtPath(path) as Material;
foreach (var guid in materialAssets)
{
Material material = AssetDatabase.LoadAssetAtPath<Material>(AssetDatabase.GUIDToAssetPath(guid));
materialIndex++;
if (UnityEditor.EditorUtility.DisplayCancelableProgressBar(progressBarName, string.Format("({0} of {1}) {2}", materialIndex, materialAssets.Length, material), (float)materialIndex / (float)materialAssets.Length))
break;

if (!ShouldUpgradeShader(m, shaderNamesToIgnore))
continue;
if (!ShouldUpgradeShader(material, shaderNamesToIgnore))
continue;

Upgrade(m, upgraders, flags);
Upgrade(material, upgraders, flags);

//SaveAssetsAndFreeMemory();
}
}

// Upgrade terrain specifically since it is a builtin material
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,29 @@ public static class StringExtensions
/// <param name="replacement">The replacement</param>
/// <returns>The string with the invalid characters replaced</returns>
public static string ReplaceInvalidFileNameCharacters(this string input, string replacement = "_") => k_InvalidRegEx.Replace(input, replacement);


/// <summary>
/// Checks if a string contains any of the strings given in strings to check and early out if it does
/// </summary>
/// <param name="input">The input string</param>
/// <param name="stringsToCheck">List of strings to check</param>
/// <returns>True if a string contains any of the strings given in strings </returns>
public static bool ContainsAny(this string input, params string[] stringsToCheck)
{
if(string.IsNullOrEmpty(input))
return false;

foreach (var value in stringsToCheck)
{
if(string.IsNullOrEmpty(value))
continue;

if (input.Contains(value))
return true;
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,20 @@ public override object Clone()
{
return new AnimationCurveParameter(new AnimationCurve(GetValue<AnimationCurve>().keys), overrideState);
}

/// <summary>
/// Returns a hash code for the animationCurve.
/// </summary>
/// <returns>A hash code for the animationCurve.</returns>
public override int GetHashCode()
{
unchecked
{
var hash = overrideState.GetHashCode();

return hash * 23 + value.GetHashCode();
}
}
}

/// <summary>
Expand Down
46 changes: 44 additions & 2 deletions Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,48 @@
// The function of the shader library are stateless, no uniform declare in it.
// Any function that require an explicit precision, use float or half qualifier, when the function can support both, it use real (see below)
// If a function require to have both a half and a float version, then both need to be explicitly define

///
/// Hardware Support for Wave Operations
///

// Support for wave operations is intentionally limited to the compute shader stage in order to make this functionality available to a wider range of hardware.
#if defined(SHADER_STAGE_COMPUTE)
//
// Platform Support
//
#if (defined(UNITY_PLATFORM_SUPPORTS_WAVE_32) || defined(UNITY_PLATFORM_SUPPORTS_WAVE_64))
#if defined(UNITY_PLATFORM_SUPPORTS_WAVE_32)
#define UNITY_HW_WAVE_SIZE 32
#elif defined(UNITY_PLATFORM_SUPPORTS_WAVE_64)
#define UNITY_HW_WAVE_SIZE 64
#endif

#define UNITY_PLATFORM_SUPPORTS_WAVE 1
//
// Device Support
//
#elif (defined(UNITY_DEVICE_SUPPORTS_WAVE_ANY) || defined(UNITY_DEVICE_SUPPORTS_WAVE_8) || defined(UNITY_DEVICE_SUPPORTS_WAVE_16) || defined(UNITY_DEVICE_SUPPORTS_WAVE_32) || defined(UNITY_DEVICE_SUPPORTS_WAVE_64) || defined(UNITY_DEVICE_SUPPORTS_WAVE_128))
#if defined(UNITY_DEVICE_SUPPORTS_WAVE_8)
#define UNITY_HW_WAVE_SIZE 8
#elif defined(UNITY_DEVICE_SUPPORTS_WAVE_16)
#define UNITY_HW_WAVE_SIZE 16
#elif defined(UNITY_DEVICE_SUPPORTS_WAVE_32)
#define UNITY_HW_WAVE_SIZE 32
#elif defined(UNITY_DEVICE_SUPPORTS_WAVE_64)
#define UNITY_HW_WAVE_SIZE 64
#elif defined(UNITY_DEVICE_SUPPORTS_WAVE_128)
#define UNITY_HW_WAVE_SIZE 128
#endif

#define UNITY_DEVICE_SUPPORTS_WAVE 1
#endif

#if (defined(UNITY_PLATFORM_SUPPORTS_WAVE) || defined(UNITY_DEVICE_SUPPORTS_WAVE))
#define UNITY_HW_SUPPORTS_WAVE 1
#endif
#endif

#ifndef real

// The including shader should define whether half
Expand Down Expand Up @@ -368,8 +410,8 @@
#define LODDitheringTransition ERROR_ON_UNSUPPORTED_FUNCTION(LODDitheringTransition)
#endif

// On everything but GCN consoles or DXC compiled shaders we error on cross-lane operations
#if !defined(PLATFORM_SUPPORTS_WAVE_INTRINSICS) && !defined(UNITY_COMPILER_DXC)
#if !defined(PLATFORM_SUPPORTS_WAVE_INTRINSICS) && !defined(UNITY_COMPILER_DXC) && !defined(UNITY_HW_SUPPORTS_WAVE)
// Intercept wave functions when they aren't supported to provide better error messages
#define WaveActiveAllTrue ERROR_ON_UNSUPPORTED_FUNCTION(WaveActiveAllTrue)
#define WaveActiveAnyTrue ERROR_ON_UNSUPPORTED_FUNCTION(WaveActiveAnyTrue)
#define WaveGetLaneIndex ERROR_ON_UNSUPPORTED_FUNCTION(WaveGetLaneIndex)
Expand Down
20 changes: 10 additions & 10 deletions Packages/com.unity.render-pipelines.core/ShaderLibrary/Hashes.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ void Hash_Tchou_2_1_float(float2 i, out float o)
uint r;
uint2 v = (uint2) (int2) round(i);
Hash_Tchou_2_1_uint(v, r);
o = r * (1.0 / float(0xffffffff));
o = (r >> 8) * (1.0 / float(0x00ffffff));
}

void Hash_Tchou_2_1_half(half2 i, out half o)
{
uint r;
uint2 v = (uint2) (int2) round(i);
Hash_Tchou_2_1_uint(v, r);
o = r * (1.0 / float(0xffffffff));
o = (r >> 8) * (1.0 / float(0x00ffffff));
}

void Hash_Tchou_2_3_uint(uint2 q, out uint3 o)
Expand All @@ -45,15 +45,15 @@ void Hash_Tchou_2_3_float(float2 i, out float3 o)
uint3 r;
uint2 v = (uint2) (int2) round(i);
Hash_Tchou_2_3_uint(v, r);
o = r * (1.0 / float(0xffffffff));
o = (r >> 8) * (1.0 / float(0x00ffffff));
}

void Hash_Tchou_2_3_half(half2 i, out half3 o)
{
uint3 r;
uint2 v = (uint2) (int2) round(i);
Hash_Tchou_2_3_uint(v, r);
o = r * (1.0 / float(0xffffffff));
o = (r >> 8) * (1.0 / float(0x00ffffff));
}

void Hash_Tchou_2_2_uint(uint2 v, out uint2 o)
Expand All @@ -73,15 +73,15 @@ void Hash_Tchou_2_2_float(float2 i, out float2 o)
uint2 r;
uint2 v = (uint2) (int2) round(i);
Hash_Tchou_2_2_uint(v, r);
o = r * (1.0 / float(0xffffffff));
o = (r >> 8) * (1.0 / float(0x00ffffff));
}

void Hash_Tchou_2_2_half(half2 i, out half2 o)
{
uint2 r;
uint2 v = (uint2) (int2) round(i);
Hash_Tchou_2_2_uint(v, r);
o = r * (1.0 / float(0xffffffff));
o = (r >> 8) * (1.0 / float(0x00ffffff));
}

void Hash_Tchou_3_1_uint(uint3 v, out uint o)
Expand All @@ -102,15 +102,15 @@ void Hash_Tchou_3_1_float(float3 i, out float o)
uint r;
uint3 v = (uint3) (int3) round(i);
Hash_Tchou_3_1_uint(v, r);
o = r * (1.0 / float(0xffffffff));
o = (r >> 8) * (1.0 / float(0x00ffffff));
}

void Hash_Tchou_3_1_half(half3 i, out half o)
{
uint r;
uint3 v = (uint3) (int3) round(i);
Hash_Tchou_3_1_uint(v, r);
o = r * (1.0 / float(0xffffffff));
o = (r >> 8) * (1.0 / float(0x00ffffff));
}

void Hash_Tchou_3_3_uint(uint3 v, out uint3 o)
Expand All @@ -132,14 +132,14 @@ void Hash_Tchou_3_3_float(float3 i, out float3 o)
{
uint3 r, v = (uint3) (int3) round(i);
Hash_Tchou_3_3_uint(v, r);
o = r * (1.0 / float(0xffffffff));
o = (r >> 8) * (1.0 / float(0x00ffffff));
}

void Hash_Tchou_3_3_half(half3 i, out half3 o)
{
uint3 r, v = (uint3) (int3) round(i);
Hash_Tchou_3_3_uint(v, r);
o = r * (1.0 / float(0xffffffff));
o = (r >> 8) * (1.0 / float(0x00ffffff));
}

void Hash_LegacySine_2_1_float(float2 i, out float o)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ internal enum FPTLMaxLightSizes
Ultra = 255
}

internal enum PathTracingLightListSizes
{
Low = 8,
Medium = 16,
High = 32,
Ultra = 64
}


/// <summary>
/// Project-wide shader configuration options.
/// </summary>
Expand Down Expand Up @@ -53,7 +62,18 @@ public enum ShaderOptions
/// Lower count will mean some memory savings.
/// Note: For any rendering bigger than 4k (in native) it is recommended to use Low count per tile, to avoid possible artifacts.
/// </summary>
FPTLMaxLightCount = FPTLMaxLightSizes.High
FPTLMaxLightCount = FPTLMaxLightSizes.High,

/// <summary>
/// The upper limit for the maximum amount of elements per cell in the light cluster. The maximum can be set in the project settings. This value caps the maximum.
/// </summary>
LightClusterMaxCellElementCount = 24,

/// <summary>
/// Maximum number of lights used in the path tracer light list. This number can be one of the prespecified possibilities in PathTracingLightListSizes, or can be chosen manually.
/// Lower count will mean some memory savings.
/// </summary>
PathTracingMaxLightCount = PathTracingLightListSizes.Medium
};

// Note: #define can't be use in include file in C# so we chose this way to configure both C# and hlsl
Expand Down Expand Up @@ -94,6 +114,12 @@ public class ShaderConfig
/// <summary>Indicates the maximum number of lights available for Fine Prunning Tile Lighting.</summary>
/// <seealso cref="ShaderOptions.FPTLMaxLightCount"/>
public static int FPTLMaxLightCount = (int)ShaderOptions.FPTLMaxLightCount;
/// <summary>Indicates the cap on the maximum number of elements per cell in the light cluster.</summary>
/// <seealso cref="ShaderOptions.LightClusterMaxCellElementCount"/>
public const int LightClusterMaxCellElementCount = (int)ShaderOptions.LightClusterMaxCellElementCount;
/// <summary>Indicates the maximum number of lights in the path tracing light list.</summary>
/// <seealso cref="ShaderOptions.PathTracingMaxLightCount"/>
public static int PathTracingMaxLightCount = (int)ShaderOptions.PathTracingMaxLightCount;
}

/// <summary>
Expand Down
Loading

0 comments on commit 9a3412a

Please sign in to comment.