Skip to content

Commit

Permalink
Merge branch 'master' into fix-nativearray-leak
Browse files Browse the repository at this point in the history
  • Loading branch information
anatawa12 authored Oct 29, 2024
2 parents 3c57ca2 + 3da0b77 commit d6bdd58
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 41 deletions.
9 changes: 9 additions & 0 deletions API-Editor/ShaderInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ internal MaterialInformationCallback()
[PublicAPI]
public abstract int? GetInteger(string propertyName, bool considerAnimation = true);

/// <summary>
/// Returns the int value for the property in the material, or null if the property is not set or not found.
/// </summary>
/// <param name="propertyName">The name of the property in the material.</param>
/// <param name="considerAnimation">Whether to consider the animation of the property. If this is true, this function will never </param>
/// <returns>The int value for the property in the material, which is same as <see cref="Material.GetInt(string)"/>, or null if the property is animated.</returns>
[PublicAPI]
public abstract int? GetInt(string propertyName, bool considerAnimation = true);

/// <summary>
/// Returns the float value for the property in the material, or null if the property is not set or not found.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog].
### Fixed
- Fix non-VRChat project support `#1310`
- 'shader' doesn't have a float or range property 'prop' error `#1312`
- Integer and Int confusion `#1313`
- NativeArray leak `#1314`

### Security
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The format is based on [Keep a Changelog].
- We may relax some restriction in the future.
- Because we have to check for each condition if we use AnyState but we can check for only one (in best case) with entry/exit, this generally reduces cost for checking an parameter in a state.
- Combined with Entry / Exit to 1D BlendTree optimization, which is implemented in previous release, your AnyState layer may be optimized to 1D BlendTree.
- Optimize Texture in Trace nad Optimize `#1181` `#1184` `#1193` `#1215` `#1225` `#1235` `#1268` `#1278`
- Optimize Texture in Trace nad Optimize `#1181` `#1184` `#1193` `#1215` `#1225` `#1235` `#1268` `#1278` `#1313`
- Avatar Optimizer will pack texture and tries to reduce the VRAM usage.
- Currently liltoon is only supported.
- `Copy Enablement Animation` to Merge Skinned Mesh `#1173`
Expand Down
66 changes: 33 additions & 33 deletions Editor/APIInternal/ShaderInformation.Liltoon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
UsingUVChannels.NonMesh, null); // GradationMap UV is based on color
LIL_SAMPLE_2D_WithMat("_MainColorAdjustMask", "_MainTex", uvMain, uvMainMatrix); // simple LIL_SAMPLE_2D

if (matInfo.GetInteger("_UseMain2ndTex") != 0)
if (matInfo.GetInt("_UseMain2ndTex") != 0)
{
// caller of lilGetMain2nd will pass sampler for _MainTex as samp
SamplerStateInformation samp = "_MainTex";

UsingUVChannels uv2nd;
switch (matInfo.GetInteger("_Main2ndTex_UVMode"))
switch (matInfo.GetInt("_Main2ndTex_UVMode"))
{
case 0:
uv2nd = UsingUVChannels.UV0;
Expand Down Expand Up @@ -172,13 +172,13 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
);
}

if (matInfo.GetInteger("_UseMain3rdTex") != 0)
if (matInfo.GetInt("_UseMain3rdTex") != 0)
{
// caller of lilGetMain3rd will pass sampler for _MainTex as samp
var samp = "_MainTex";

UsingUVChannels uv3rd;
switch (matInfo.GetInteger("_Main2ndTex_UVMode"))
switch (matInfo.GetInt("_Main2ndTex_UVMode"))
{
case 0:
uv3rd = UsingUVChannels.UV0;
Expand Down Expand Up @@ -214,16 +214,16 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
}

LIL_SAMPLE_2D_ST_WithMat("_AlphaMask", "_MainTex", uvMain, uvMainMatrix);
if (matInfo.GetInteger("_UseBumpMap") != 0)
if (matInfo.GetInt("_UseBumpMap") != 0)
{
LIL_SAMPLE_2D_ST_WithMat("_BumpMap", "_MainTex", uvMain, uvMainMatrix);
}

if (matInfo.GetInteger("_UseBump2ndMap") != 0)
if (matInfo.GetInt("_UseBump2ndMap") != 0)
{
var uvBump2nd = UsingUVChannels.UV0;

switch (matInfo.GetInteger("_Bump2ndMap_UVMode"))
switch (matInfo.GetInt("_Bump2ndMap_UVMode"))
{
case 0:
uvBump2nd = UsingUVChannels.UV0;
Expand All @@ -248,7 +248,7 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
// Note: _Bump2ndScaleMask is defined as NoScaleOffset but sampled with LIL_SAMPLE_2D_ST?
}

if (matInfo.GetInteger("_UseAnisotropy") != 0)
if (matInfo.GetInt("_UseAnisotropy") != 0)
{
LIL_SAMPLE_2D_ST_WithMat("_AnisotropyTangentMap", "_MainTex", uvMain, uvMainMatrix);
LIL_SAMPLE_2D_ST_WithMat("_AnisotropyScaleMask", "_MainTex", uvMain, uvMainMatrix);
Expand All @@ -257,13 +257,13 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
LIL_SAMPLE_2D_ST_WithMat("_AnisotropyShiftNoiseMask", "_MainTex", uvMain, uvMainMatrix);
}

if (matInfo.GetInteger("_UseBacklight") != 0)
if (matInfo.GetInt("_UseBacklight") != 0)
{
var samp = "_MainTex";
LIL_SAMPLE_2D_ST_WithMat("_BacklightColorTex", samp, uvMain, uvMainMatrix);
}

if (matInfo.GetInteger("_UseShadow") != 0)
if (matInfo.GetInt("_UseShadow") != 0)
{
SamplerStateInformation samp = "_MainTex";
LIL_SAMPLE_2D_GRAD_WithMat("_ShadowStrengthMask", SamplerStateInformation.LinearRepeatSampler, uvMain,
Expand All @@ -273,7 +273,7 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
LIL_SAMPLE_2D_GRAD_WithMat("_ShadowBlurMask", SamplerStateInformation.LinearRepeatSampler, uvMain,
uvMainMatrix);
// lilSampleLUT
switch (matInfo.GetInteger("_ShadowColorType"))
switch (matInfo.GetInt("_ShadowColorType"))
{
case 1:
LIL_SAMPLE_2D_WithMat("_ShadowColorTex", SamplerStateInformation.LinearClampSampler,
Expand All @@ -297,14 +297,14 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
}
}

if (matInfo.GetInteger("_UseRimShade") != 0)
if (matInfo.GetInt("_UseRimShade") != 0)
{
var samp = "_MainTex";

LIL_SAMPLE_2D_WithMat("_RimShadeMask", samp, uvMain, uvMainMatrix);
}

if (matInfo.GetInteger("_UseReflection") != 0)
if (matInfo.GetInt("_UseReflection") != 0)
{
// TODO: research
var samp = "_MainTex"; // or SamplerStateInformation.LinearRepeatSampler in lil_pass_foreward_reblur.hlsl
Expand All @@ -315,57 +315,57 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
}

// Matcap
if (matInfo.GetInteger("_UseMatCap") != 0)
if (matInfo.GetInt("_UseMatCap") != 0)
{
var samp = "_MainTex"; // caller of lilGetMatCap

LIL_SAMPLE_2D("_MatCapTex", SamplerStateInformation.LinearRepeatSampler, UsingUVChannels.NonMesh);
LIL_SAMPLE_2D_ST_WithMat("_MatCapBlendMask", samp, uvMain, uvMainMatrix);

if (matInfo.GetInteger("_MatCapCustomNormal") != 0)
if (matInfo.GetInt("_MatCapCustomNormal") != 0)
{
LIL_SAMPLE_2D_ST_WithMat("_MatCapBumpMap", samp, uvMain, uvMainMatrix);
}
}

if (matInfo.GetInteger("_UseMatCap2nd") != 0)
if (matInfo.GetInt("_UseMatCap2nd") != 0)
{
var samp = "_MainTex"; // caller of lilGetMatCap

LIL_SAMPLE_2D("_MatCap2ndTex", SamplerStateInformation.LinearRepeatSampler, UsingUVChannels.NonMesh);
LIL_SAMPLE_2D_ST_WithMat("_MatCap2ndBlendMask", samp, uvMain, uvMainMatrix);

if (matInfo.GetInteger("_MatCap2ndCustomNormal") != 0)
if (matInfo.GetInt("_MatCap2ndCustomNormal") != 0)
{
LIL_SAMPLE_2D_ST_WithMat("_MatCap2ndBumpMap", samp, uvMain, uvMainMatrix);
}
}

// rim light
if (matInfo.GetInteger("_UseRim") != 0)
if (matInfo.GetInt("_UseRim") != 0)
{
var samp = "_MainTex"; // caller of lilGetRim
LIL_SAMPLE_2D_ST_WithMat("_RimColorTex", samp, uvMain, uvMainMatrix);
}

if (matInfo.GetInteger("_UseGlitter") != 0)
if (matInfo.GetInt("_UseGlitter") != 0)
{
var samp = "_MainTex"; // caller of lilGetGlitter

LIL_SAMPLE_2D_ST_WithMat("_GlitterColorTex", samp, uvMain, uvMainMatrix);
if (matInfo.GetInteger("_GlitterApplyShape") != 0)
if (matInfo.GetInt("_GlitterApplyShape") != 0)
{
// complex uv
LIL_SAMPLE_2D_GRAD("_GlitterShapeTex", SamplerStateInformation.LinearClampSampler,
UsingUVChannels.NonMesh);
}
}

if (matInfo.GetInteger("_UseEmission") != 0)
if (matInfo.GetInt("_UseEmission") != 0)
{
UsingUVChannels emissionUV = UsingUVChannels.UV0;

switch (matInfo.GetInteger("_EmissionMap_UVMode"))
switch (matInfo.GetInt("_EmissionMap_UVMode"))
{
case 1:
emissionUV = UsingUVChannels.UV1;
Expand Down Expand Up @@ -403,17 +403,17 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
LIL_GET_EMIMASK_WithMat("_EmissionBlendMask", uvMain, uvMainMatrix);
}

if (matInfo.GetInteger("_EmissionUseGrad") != 0)
if (matInfo.GetInt("_EmissionUseGrad") != 0)
{
LIL_SAMPLE_1D("_EmissionGradTex", SamplerStateInformation.LinearRepeatSampler, UsingUVChannels.NonMesh);
}
}

if (matInfo.GetInteger("_UseEmission2nd") != 0)
if (matInfo.GetInt("_UseEmission2nd") != 0)
{
UsingUVChannels emission2ndUV = UsingUVChannels.UV0;

switch (matInfo.GetInteger("_Emission2ndMap_UVMode"))
switch (matInfo.GetInt("_Emission2ndMap_UVMode"))
{
case 1:
emission2ndUV = UsingUVChannels.UV1;
Expand Down Expand Up @@ -453,28 +453,28 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
LIL_GET_EMIMASK_WithMat("_Emission2ndBlendMask", uvMain, uvMainMatrix);
}

if (matInfo.GetInteger("_Emission2ndUseGrad") != 0)
if (matInfo.GetInt("_Emission2ndUseGrad") != 0)
{
LIL_SAMPLE_1D("_Emission2ndGradTex", SamplerStateInformation.LinearRepeatSampler,
UsingUVChannels.NonMesh);
}
}

if (matInfo.GetInteger("_UseParallax") != 0)
if (matInfo.GetInt("_UseParallax") != 0)
{
matInfo.RegisterTextureUVUsage("_ParallaxMap", SamplerStateInformation.LinearRepeatSampler,
UsingUVChannels.UV0, null);
}

if (matInfo.GetInteger("_UseAudioLink") != 0 && matInfo.GetInteger("_AudioLink2Vertex") != 0)
if (matInfo.GetInt("_UseAudioLink") != 0 && matInfo.GetInt("_AudioLink2Vertex") != 0)
{
var _AudioLinkUVMode = matInfo.GetInteger("_AudioLinkUVMode");
var _AudioLinkUVMode = matInfo.GetInt("_AudioLinkUVMode");

if (_AudioLinkUVMode is 3 or 4 or null)
{
// TODO: _AudioLinkMask_ScrollRotate
var sampler = "_AudioLinkMask" | SamplerStateInformation.LinearRepeatSampler;
switch (matInfo.GetInteger("_AudioLinkMask_UVMode"))
switch (matInfo.GetInt("_AudioLinkMask_UVMode"))
{
case 0:
default:
Expand Down Expand Up @@ -518,7 +518,7 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
);
}

if (matInfo.GetInteger("_UseOutline") != 0)
if (matInfo.GetInt("_UseOutline") != 0)
{
// not on material side, on editor side toggle
LIL_SAMPLE_2D_WithMat("_OutlineTex", "_OutlineTex", uvMain, uvMainMatrix);
Expand All @@ -527,7 +527,7 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
// _OutlineVectorTex SamplerStateInformation.LinearRepeatSampler
// UVs _OutlineVectorUVMode main,1,2,3

switch (matInfo.GetInteger("_AudioLinkMask_UVMode"))
switch (matInfo.GetInt("_AudioLinkMask_UVMode"))
{
case 0:
LIL_SAMPLE_2D_WithMat("_OutlineVectorTex", SamplerStateInformation.LinearRepeatSampler, uvMain,
Expand Down Expand Up @@ -566,7 +566,7 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo)
"_IDMaskPrior1", "_IDMaskPrior2", "_IDMaskPrior3", "_IDMaskPrior4", "_IDMaskPrior5", "_IDMaskPrior6", "_IDMaskPrior7", "_IDMaskPrior8",
"_IDMaskIsBitmap", "_IDMaskCompile"
};
if (idMaskProperties.Any(prop => matInfo.GetInteger(prop) != 0))
if (idMaskProperties.Any(prop => matInfo.GetInt(prop) != 0))
{
// with _IDMaskFrom = 0..7, uv is used for ID Mask, but it will only use integer part
// (cast to int with normal rounding in hlsl) so it's not necessary to register UV usage.
Expand Down
18 changes: 15 additions & 3 deletions Editor/Processors/ShaderMaterialInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,21 @@ public MaterialInformationCallbackImpl(Material material, ShaderInformationKind

public Shader Shader => _material.shader;

private T? GetValue<T>(string propertyName, Func<string, T> computer, bool considerAnimation) where T : struct
private T? GetValue<T>(string propertyName, Func<string, T> computer, bool considerAnimation,
string[]? subProperties = null) where T : struct
{
// animated; return null
if (considerAnimation)
{
var animationProperty = $"material.{propertyName}";
if (_infos.Any(x => x.GetFloatNode(animationProperty).ComponentNodes.Any()))
return null;
foreach (var subProperty in subProperties ?? Array.Empty<string>())
{
var subAnimationProperty = $"material.{propertyName}.{subProperty}";
if (_infos.Any(x => x.GetFloatNode(subAnimationProperty).ComponentNodes.Any()))
return null;
}
}

return computer(propertyName);
Expand All @@ -126,11 +133,16 @@ public MaterialInformationCallbackImpl(Material material, ShaderInformationKind
public override int? GetInteger(string propertyName, bool considerAnimation = true) =>
GetValue(propertyName, _material.SafeGetInteger, considerAnimation);

public override int? GetInt(string propertyName, bool considerAnimation = true) =>
GetValue(propertyName, _material.SafeGetInt, considerAnimation);

private static readonly string[] VectorSubProperties = new[] { "r", "g", "b", "a", "x", "y", "z", "w" };

public override float? GetFloat(string propertyName, bool considerAnimation = true) =>
GetValue(propertyName, _material.SafeGetFloat, considerAnimation);
GetValue(propertyName, _material.SafeGetFloat, considerAnimation, VectorSubProperties);

public override Vector4? GetVector(string propertyName, bool considerAnimation = true) =>
GetValue(propertyName, _material.SafeGetVector, considerAnimation);
GetValue(propertyName, _material.SafeGetVector, considerAnimation, VectorSubProperties);

public override void RegisterOtherUVUsage(UsingUVChannels uvChannel)
{
Expand Down
11 changes: 7 additions & 4 deletions Internal/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,16 +375,19 @@ public static void Assert(bool condition, string message)
}

public static float SafeGetFloat(this Material material, string propertyName) =>
material.HasProperty(propertyName) ? material.GetFloat(propertyName) : 0;
material.HasFloat(propertyName) ? material.GetFloat(propertyName) : 0;

public static int SafeGetInteger(this Material material, string propertyName) =>
material.HasProperty(propertyName) ? material.GetInteger(propertyName) : 0;
material.HasInteger(propertyName) ? material.GetInteger(propertyName) : 0;

public static int SafeGetInt(this Material material, string propertyName) =>
material.HasInt(propertyName) ? material.GetInt(propertyName) : 0;

public static Vector4 SafeGetVector(this Material material, string propertyName) =>
material.HasProperty(propertyName) ? material.GetVector(propertyName) : Vector4.zero;
material.HasVector(propertyName) ? material.GetVector(propertyName) : Vector4.zero;

public static Color SafeGetColor(this Material material, string propertyName) =>
material.HasProperty(propertyName) ? material.GetColor(propertyName) : Color.clear;
material.HasColor(propertyName) ? material.GetColor(propertyName) : Color.clear;

/// <summary>
/// Fast compare of <see cref="Color32"/> array.
Expand Down

0 comments on commit d6bdd58

Please sign in to comment.