Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal/2022.3/staging #8092

Merged
merged 6 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ real PenumbraSizeDirectional(real Reciever, real Blocker, real rangeScale)
return abs(Reciever - Blocker) * rangeScale;
}

bool BlockerSearch(inout real averageBlockerDepth, inout real numBlockers, real lightArea, real3 coord, real2 sampleJitter, Texture2D shadowMap, SamplerState pointSampler, int sampleCount)
bool BlockerSearch(inout real averageBlockerDepth, inout real numBlockers, real lightArea, real3 coord, real UMin, real UMax, real VMin, real VMax, real2 sampleJitter, Texture2D shadowMap, SamplerState pointSampler, int sampleCount)
{
real blockerSum = 0.0;
real sampleCountInverse = rcp((real)sampleCount);
Expand All @@ -116,32 +116,42 @@ bool BlockerSearch(inout real averageBlockerDepth, inout real numBlockers, real
offset = real2(offset.x * sampleJitter.y + offset.y * sampleJitter.x,
offset.x * -sampleJitter.x + offset.y * sampleJitter.y);

real shadowMapDepth = SAMPLE_TEXTURE2D_LOD(shadowMap, pointSampler, coord.xy + offset, 0.0).x;
real U = coord.x + offset.x;
real V = coord.y + offset.y;

if (COMPARE_DEVICE_DEPTH_CLOSER(shadowMapDepth, coord.z))
//NOTE: We must clamp the sampling within the bounds of the shadow atlas.
// Overfiltering will leak results from other shadow lights.
if (U < UMin || U > UMax || V < VMin || V > VMax)
{
blockerSum += shadowMapDepth;
numBlockers += 1.0;
// Discard the sample (it is located outside the shadow map, and it may correspond to another cube map face).
}
else
{
real shadowMapDepth = SAMPLE_TEXTURE2D_LOD(shadowMap, pointSampler, float2(U, V), 0.0).x;

if (COMPARE_DEVICE_DEPTH_CLOSER(shadowMapDepth, coord.z))
{
blockerSum += shadowMapDepth;
numBlockers += 1.0;
}
}
}
averageBlockerDepth = blockerSum / numBlockers;

return numBlockers >= 1;
// Return the depth value of the far plane if none of the samples are valid
averageBlockerDepth = (numBlockers > 0) ? (blockerSum / numBlockers) : UNITY_RAW_FAR_CLIP_VALUE;

return numBlockers > 0;
}

real PCSS(real3 coord, real filterRadius, real2 scale, real2 offset, real2 sampleJitter, Texture2D shadowMap, SamplerComparisonState compSampler, int sampleCount)
real PCSS(real3 coord, real UMin, real UMax, real VMin, real VMax, real filterRadius, real2 sampleJitter, Texture2D shadowMap, SamplerComparisonState compSampler, int sampleCount)
{
real UMin = offset.x;
real UMax = offset.x + scale.x;

real VMin = offset.y;
real VMax = offset.y + scale.y;

real sum = 0.0;
real sampleCountInverse = rcp((real)sampleCount);
real sampleCountBias = 0.5 * sampleCountInverse;
real ditherRotation = sampleJitter.x;

real numValidSamples = 0;

for (int i = 0; i < sampleCount && i < DISK_SAMPLE_COUNT; ++i)
{
real2 offset = ComputeFibonacciSpiralDiskSample(i, filterRadius, sampleCountInverse, sampleCountBias);
Expand All @@ -153,16 +163,19 @@ real PCSS(real3 coord, real filterRadius, real2 scale, real2 offset, real2 sampl

//NOTE: We must clamp the sampling within the bounds of the shadow atlas.
// Overfiltering will leak results from other shadow lights.
//TODO: Investigate moving this to blocker search.
// coord.xy = clamp(coord.xy, float2(UMin, VMin), float2(UMax, VMax));

if (U <= UMin || U >= UMax || V <= VMin || V >= VMax)
sum += SAMPLE_TEXTURE2D_SHADOW(shadowMap, compSampler, real3(coord.xy, coord.z)).r;
if (U < UMin || U > UMax || V < VMin || V > VMax)
{
// Discard the sample (it is located outside the shadow map, and it may correspond to another cube map face).
}
else
{
sum += SAMPLE_TEXTURE2D_SHADOW(shadowMap, compSampler, real3(U, V, coord.z)).r;
numValidSamples += 1.0;
}
}

return sum / sampleCount;
// Return the unoccluded (unshadowed) value if none of the samples are valid
return (numValidSamples > 0) ? (sum / numValidSamples) : 1.0;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,18 @@ float SampleShadow_PCSS(float3 tcs, float2 posSS, float2 scale, float2 offset, f
float shadowMapRes = scale.x * shadowAtlasInfo.x; // atlas is square
float resIndepenentMaxSoftness = 0.04 * (shadowMapRes / 512);

real halfTexelOffset = 0.5 * shadowAtlasInfo.y;

real UMin = offset.x + halfTexelOffset;
real UMax = offset.x + scale.x - halfTexelOffset;

real VMin = offset.y + halfTexelOffset;
real VMax = offset.y + scale.y - halfTexelOffset;

//1) Blocker Search
float averageBlockerDepth = 0.0;
float numBlockers = 0.0;
bool blockerFound = BlockerSearch(averageBlockerDepth, numBlockers, min((shadowSoftness + 0.000001), resIndepenentMaxSoftness) * atlasResFactor, tcs, sampleJitter, tex, samp, blockerSampleCount);
bool blockerFound = BlockerSearch(averageBlockerDepth, numBlockers, min((shadowSoftness + 0.000001), resIndepenentMaxSoftness) * atlasResFactor, tcs, UMin, UMax, VMin, VMax, sampleJitter, tex, samp, blockerSampleCount);

// We scale the softness also based on the distance between the occluder if we assume that the light is a sphere source.
// Also, we don't bother if the blocker has not been found.
Expand All @@ -336,7 +344,7 @@ float SampleShadow_PCSS(float3 tcs, float2 posSS, float2 scale, float2 offset, f

//3) Filter
// Note: we can't early out of the function if blockers are not found since Vulkan triggers a warning otherwise. Hence, we check for blockerFound here.
return PCSS(tcs, filterSize, scale, offset, sampleJitter, tex, compSamp, filterSampleCount);
return PCSS(tcs, UMin, UMax, VMin, VMax, filterSize, sampleJitter, tex, compSamp, filterSampleCount);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ static float2 NeighbourOffsets[8];

void SetNeighbourOffsets(float4 neighbourOffsets[4])
{
UNITY_UNROLL for (int i = 0; i < 16; ++i)
NeighbourOffsets[i/2][i%2] = neighbourOffsets[i/4][i%4];
UNITY_UNROLL for (uint i = 0; i < 16; ++i)
NeighbourOffsets[i / 2][i % 2] = neighbourOffsets[i / 4][i % 4];
}

float2 ClampAndScaleForBilinearWithCustomScale(float2 uv, float2 scale)
Expand Down Expand Up @@ -587,9 +587,9 @@ CTYPE FilterCentralColor(NeighbourhoodSamples samples, float centralWeight, floa
{
CTYPE filtered = samples.central * centralWeight;

for (int i = 0; i < NEIGHBOUR_COUNT; ++i)
for (uint i = 0; i < NEIGHBOUR_COUNT; ++i)
{
float w = weights[i/4][i%4];
float w = weights[i / 4][i % 4];
filtered += samples.neighbours[i] * w;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,11 @@ bool SetupForEmptyRendering(ref RenderingData renderingData)
/// <inheritdoc/>
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
{
// UUM-63146 - glClientWaitSync: Expected application to have kicked everything until job: 96089 (possibly by calling glFlush)" are thrown in the Android Player on some devices with PowerVR Rogue GE8320
// Resetting of target would clean up the color attachment buffers and depth attachment buffers, which inturn is preventing the leak in the said platform. This is likely a symptomatic fix, but is solving the problem for now.
if (Application.platform == RuntimePlatform.Android && PlatformAutoDetect.isRunningOnPowerVRGPU)
ResetTarget();

if (m_CreateEmptyShadowmap)
ConfigureTarget(m_EmptyAdditionalLightShadowmapTexture);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ bool SetupForEmptyRendering(ref RenderingData renderingData)
/// <inheritdoc />
public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
{
// UUM-63146 - glClientWaitSync: Expected application to have kicked everything until job: 96089 (possibly by calling glFlush)" are thrown in the Android Player on some devices with PowerVR Rogue GE8320
// Resetting of target would clean up the color attachment buffers and depth attachment buffers, which inturn is preventing the leak in the said platform. This is likely a symptomatic fix, but is solving the problem for now.
if (Application.platform == RuntimePlatform.Android && PlatformAutoDetect.isRunningOnPowerVRGPU)
ResetTarget();

if (m_CreateEmptyShadowmap)
ConfigureTarget(m_EmptyMainLightShadowmapTexture);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1879,5 +1879,7 @@ internal static ShEvalMode ShAutoDetect(ShEvalMode mode)

return mode;
}

internal static bool isRunningOnPowerVRGPU = SystemInfo.graphicsDeviceName.Contains("PowerVR");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,8 @@ half3 MixFogColor(half3 fragColor, half3 fogColor, half fogFactor)
{
#if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
half fogIntensity = ComputeFogIntensity(fogFactor);
fragColor = lerp(fogColor, fragColor, fogIntensity);
// Workaround for UUM-61728: using a manual lerp to avoid rendering artifacts on some GPUs when Vulkan is used
fragColor = fragColor * fogIntensity + fogColor * (half(1.0) - fogIntensity);
#endif
return fragColor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityGBuffer.hlsl"
#include "Packages/com.unity.shadergraph/ShaderGraphLibrary/Nature/SpeedTreeCommon.hlsl"
#include "SpeedTreeUtility.hlsl"
#if defined(LOD_FADE_CROSSFADE)
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/LODCrossFade.hlsl"
Expand Down Expand Up @@ -96,8 +97,26 @@ struct SpeedTreeFragmentInput

void InitializeData(inout SpeedTreeVertexInput input, float lodValue)
{
#if defined(LOD_FADE_PERCENTAGE) && (!defined(LOD_FADE_CROSSFADE) && !defined(EFFECT_BILLBOARD))
#if !defined(EFFECT_BILLBOARD)
#if defined(LOD_FADE_PERCENTAGE) && (!defined(LOD_FADE_CROSSFADE))
input.vertex.xyz = lerp(input.vertex.xyz, input.texcoord2.xyz, lodValue);
#endif

// geometry type
float geometryType = (int) (input.texcoord3.w + 0.25);
bool leafTwo = false;
if (geometryType > GEOM_TYPE_FACINGLEAF)
{
geometryType -= 2;
leafTwo = true;
}

// leaf facing
if (geometryType == GEOM_TYPE_FACINGLEAF)
{
float3 anchor = float3(input.texcoord1.zw, input.texcoord2.w);
input.vertex.xyz = DoLeafFacing(input.vertex.xyz, anchor);
}
#endif

// wind
Expand All @@ -117,30 +136,14 @@ void InitializeData(inout SpeedTreeVertexInput input, float lodValue)
float3 windyPosition = input.vertex.xyz;

#ifndef EFFECT_BILLBOARD
// geometry type
float geometryType = (int)(input.texcoord3.w + 0.25);
bool leafTwo = false;
if (geometryType > GEOM_TYPE_FACINGLEAF)
{
geometryType -= 2;
leafTwo = true;
}

// leaves
if (geometryType > GEOM_TYPE_FROND)
{
// remove anchor position
float3 anchor = float3(input.texcoord1.zw, input.texcoord2.w);
windyPosition -= anchor;

if (geometryType == GEOM_TYPE_FACINGLEAF)
{
// face camera-facing leaf to camera
float offsetLen = length(windyPosition);
windyPosition = mul(windyPosition.xyz, (float3x3)UNITY_MATRIX_IT_MV); // inv(MV) * windyPosition
windyPosition = normalize(windyPosition) * offsetLen; // make sure the offset vector is still scaled
}


// leaf wind
#if defined(_WINDQUALITY_FAST) || defined(_WINDQUALITY_BETTER) || defined(_WINDQUALITY_BEST)
#ifdef _WINDQUALITY_BEST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,19 @@ Shader BuildAllShaders(

ReportErrors(graph, shader, path, importErrorLog);

EditorMaterialUtility.SetShaderDefaults(
shader,
generatedShader.assignedTextures.Where(x => x.modifiable).Select(x => x.name).ToArray(),
generatedShader.assignedTextures.Where(x => x.modifiable).Select(x => EditorUtility.InstanceIDToObject(x.textureId) as Texture).ToArray());

EditorMaterialUtility.SetShaderNonModifiableDefaults(
shader,
generatedShader.assignedTextures.Where(x => !x.modifiable).Select(x => x.name).ToArray(),
generatedShader.assignedTextures.Where(x => !x.modifiable).Select(x => EditorUtility.InstanceIDToObject(x.textureId) as Texture).ToArray());

if (generatedShader.assignedTextures != null)
{
EditorMaterialUtility.SetShaderDefaults(
shader,
generatedShader.assignedTextures.Where(x => x.modifiable).Select(x => x.name).ToArray(),
generatedShader.assignedTextures.Where(x => x.modifiable).Select(x => EditorUtility.InstanceIDToObject(x.textureId) as Texture).ToArray());

EditorMaterialUtility.SetShaderNonModifiableDefaults(
shader,
generatedShader.assignedTextures.Where(x => !x.modifiable).Select(x => x.name).ToArray(),
generatedShader.assignedTextures.Where(x => !x.modifiable).Select(x => EditorUtility.InstanceIDToObject(x.textureId) as Texture).ToArray());
}

if (first)
{
// first shader is always the primary shader
Expand Down Expand Up @@ -228,7 +231,11 @@ public override void OnImportAsset(AssetImportContext ctx)
}
}
#endif

if (mainObject == null)
{
mainObject = ShaderUtil.CreateShaderAsset(ctx, k_ErrorShader, false);
}

Texture2D texture = Resources.Load<Texture2D>("Icons/sg_graph_icon");
ctx.AddObjectToAsset("MainAsset", mainObject, texture);
ctx.SetMainObject(mainObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static class Keys
internal delegate void PreferenceChangedDelegate();

internal static PreferenceChangedDelegate onVariantLimitChanged;
static int m_previewVariantLimit = 128;
static int m_previewVariantLimit = 2048;

internal static PreferenceChangedDelegate onAllowDeprecatedChanged;
internal static int previewVariantLimit
Expand Down Expand Up @@ -83,7 +83,8 @@ static void OpenGUI()
? new GUIContent("Preview Variant Limit", EditorGUIUtility.IconContent("console.infoicon").image, $"The Preview Variant Limit is higher than the Shader Variant Limit in Project Settings: {actualLimit}. The Preview Variant Limit will be ignored.")
: new GUIContent("Preview Variant Limit");

var variantLimitValue = EditorGUILayout.DelayedIntField(variantLimitLabel, previewVariantLimit);
var variantLimitValue = EditorGUILayout.DelayedIntField(variantLimitLabel, previewVariantLimit);
variantLimitValue = Mathf.Max(0, variantLimitValue);
if (EditorGUI.EndChangeCheck())
{
previewVariantLimit = variantLimitValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace UnityEditor.ShaderGraph
internal class ShaderGraphProjectSettings : ScriptableSingleton<ShaderGraphProjectSettings>
{
[SerializeField]
internal int shaderVariantLimit = 128;
internal int shaderVariantLimit = 2048;
[SerializeField]
internal int customInterpolatorErrorThreshold = 32;
[SerializeField]
Expand Down Expand Up @@ -62,10 +62,12 @@ void OnGUIHandler(string searchContext)
EditorGUI.BeginChangeCheck();

var newValue = EditorGUILayout.DelayedIntField(Styles.shaderVariantLimitLabel, m_shaderVariantLimit.intValue);
newValue = Mathf.Max(0, newValue);
if (newValue != m_shaderVariantLimit.intValue)
{
m_shaderVariantLimit.intValue = newValue;
ShaderGraphPreferences.onVariantLimitChanged();
if (ShaderGraphPreferences.onVariantLimitChanged != null)
ShaderGraphPreferences.onVariantLimitChanged();
}

EditorGUILayout.LabelField(Styles.CustomInterpLabel, EditorStyles.boldLabel);
Expand Down
Loading
Loading