Skip to content

Commit bab57ad

Browse files
NzollnerEvergreen
authored andcommitted
Changed _REFLECTION_PROBE_ keywords to use dynamic branch
1 parent 8d139ba commit bab57ad

File tree

1 file changed

+57
-42
lines changed

1 file changed

+57
-42
lines changed

Packages/com.unity.render-pipelines.universal/ShaderLibrary/GlobalIllumination.hlsl

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ half3 SampleScreenSpaceGI(float2 pos)
3131
#define _MIXED_LIGHTING_SUBTRACTIVE
3232
#endif
3333

34+
#if !defined(_REFLECTION_PROBE_BLENDING_KEYWORD_DECLARED)
35+
#define _REFLECTION_PROBE_BLENDING 0
36+
#endif
37+
38+
#if !defined(_REFLECTION_PROBE_BOX_PROJECTION_KEYWORD_DECLARED)
39+
#define _REFLECTION_PROBE_BOX_PROJECTION 0
40+
#endif
41+
3442
// SH Vertex Evaluation. Depending on target SH sampling might be
3543
// done completely per vertex or mixed with L2 term per vertex and L0, L1
3644
// per pixel. See SampleSHPixel
@@ -300,13 +308,14 @@ half3 CalculateIrradianceFromReflectionProbes(half3 reflectVector, float3 positi
300308
weight = min(weight, 1.0f - totalWeight);
301309

302310
half3 sampleVector = reflectVector;
303-
#ifdef _REFLECTION_PROBE_BOX_PROJECTION
304-
#if defined(REFLECTION_PROBE_ROTATION)
305-
sampleVector = BoxProjectedCubemapDirection(urp_ReflProbes_Rotation[probeIndex], reflectVector, rotPosWS, urp_ReflProbes_ProbePosition[probeIndex], urp_ReflProbes_BoxMin[probeIndex], urp_ReflProbes_BoxMax[probeIndex]);
306-
#else
307-
sampleVector = BoxProjectedCubemapDirection(reflectVector, rotPosWS, urp_ReflProbes_ProbePosition[probeIndex], urp_ReflProbes_BoxMin[probeIndex], urp_ReflProbes_BoxMax[probeIndex]);
308-
#endif
309-
#endif // _REFLECTION_PROBE_BOX_PROJECTION
311+
if (_REFLECTION_PROBE_BOX_PROJECTION)
312+
{
313+
#if defined(REFLECTION_PROBE_ROTATION)
314+
sampleVector = BoxProjectedCubemapDirection(urp_ReflProbes_Rotation[probeIndex], reflectVector, rotPosWS, urp_ReflProbes_ProbePosition[probeIndex], urp_ReflProbes_BoxMin[probeIndex], urp_ReflProbes_BoxMax[probeIndex]);
315+
#else
316+
sampleVector = BoxProjectedCubemapDirection(reflectVector, rotPosWS, urp_ReflProbes_ProbePosition[probeIndex], urp_ReflProbes_BoxMin[probeIndex], urp_ReflProbes_BoxMax[probeIndex]);
317+
#endif
318+
}
310319

311320
uint maxMip = (uint)abs(urp_ReflProbes_ProbePosition[probeIndex].w) - 1;
312321
half probeMip = min(mip, maxMip);
@@ -365,13 +374,14 @@ half3 CalculateIrradianceFromReflectionProbes(half3 reflectVector, float3 positi
365374
if (weightProbe0 > 0.01f)
366375
{
367376
half3 reflectVector0 = reflectVector;
368-
#ifdef _REFLECTION_PROBE_BOX_PROJECTION
369-
#if defined(REFLECTION_PROBE_ROTATION)
370-
reflectVector0 = BoxProjectedCubemapDirection(unity_SpecCube0_Rotation, reflectVector, rotPosWS0, unity_SpecCube0_ProbePosition, unity_SpecCube0_BoxMin, unity_SpecCube0_BoxMax);
371-
#else
372-
reflectVector0 = BoxProjectedCubemapDirection(reflectVector, rotPosWS0, unity_SpecCube0_ProbePosition, unity_SpecCube0_BoxMin, unity_SpecCube0_BoxMax);
373-
#endif
374-
#endif // _REFLECTION_PROBE_BOX_PROJECTION
377+
if (_REFLECTION_PROBE_BOX_PROJECTION)
378+
{
379+
#if defined(REFLECTION_PROBE_ROTATION)
380+
reflectVector0 = BoxProjectedCubemapDirection(unity_SpecCube0_Rotation, reflectVector, rotPosWS0, unity_SpecCube0_ProbePosition, unity_SpecCube0_BoxMin, unity_SpecCube0_BoxMax);
381+
#else
382+
reflectVector0 = BoxProjectedCubemapDirection(reflectVector, rotPosWS0, unity_SpecCube0_ProbePosition, unity_SpecCube0_BoxMin, unity_SpecCube0_BoxMax);
383+
#endif
384+
}
375385

376386
half4 encodedIrradiance = half4(SAMPLE_TEXTURECUBE_LOD(unity_SpecCube0, samplerunity_SpecCube0, reflectVector0, mip));
377387

@@ -382,13 +392,14 @@ half3 CalculateIrradianceFromReflectionProbes(half3 reflectVector, float3 positi
382392
if (weightProbe1 > 0.01f)
383393
{
384394
half3 reflectVector1 = reflectVector;
385-
#ifdef _REFLECTION_PROBE_BOX_PROJECTION
386-
#if defined(REFLECTION_PROBE_ROTATION)
387-
reflectVector1 = BoxProjectedCubemapDirection(unity_SpecCube1_Rotation, reflectVector, rotPosWS1, unity_SpecCube1_ProbePosition, unity_SpecCube1_BoxMin, unity_SpecCube1_BoxMax);
388-
#else
389-
reflectVector1 = BoxProjectedCubemapDirection(reflectVector, rotPosWS1, unity_SpecCube1_ProbePosition, unity_SpecCube1_BoxMin, unity_SpecCube1_BoxMax);
390-
#endif
391-
#endif // _REFLECTION_PROBE_BOX_PROJECTION
395+
if (_REFLECTION_PROBE_BOX_PROJECTION)
396+
{
397+
#if defined(REFLECTION_PROBE_ROTATION)
398+
reflectVector1 = BoxProjectedCubemapDirection(unity_SpecCube1_Rotation, reflectVector, rotPosWS1, unity_SpecCube1_ProbePosition, unity_SpecCube1_BoxMin, unity_SpecCube1_BoxMax);
399+
#else
400+
reflectVector1 = BoxProjectedCubemapDirection(reflectVector, rotPosWS1, unity_SpecCube1_ProbePosition, unity_SpecCube1_BoxMin, unity_SpecCube1_BoxMax);
401+
#endif
402+
}
392403
half4 encodedIrradiance = half4(SAMPLE_TEXTURECUBE_LOD(unity_SpecCube1, samplerunity_SpecCube1, reflectVector1, mip));
393404

394405
irradiance += weightProbe1 * DecodeHDREnvironment(encodedIrradiance, unity_SpecCube1_HDR);
@@ -411,27 +422,31 @@ half3 GlossyEnvironmentReflection(half3 reflectVector, float3 positionWS, half p
411422
half3 irradiance;
412423

413424
#if !defined(_ENVIRONMENTREFLECTIONS_OFF)
414-
#if defined(_REFLECTION_PROBE_BLENDING)
415-
irradiance = CalculateIrradianceFromReflectionProbes(reflectVector, positionWS, perceptualRoughness, normalizedScreenSpaceUV);
416-
#else
417-
#ifdef _REFLECTION_PROBE_BOX_PROJECTION
418-
#if defined(REFLECTION_PROBE_ROTATION)
419-
float3 probeCenterPosWS0 = unity_SpecCube0_BoxMin.xyz + (unity_SpecCube0_BoxMax.xyz - unity_SpecCube0_BoxMin.xyz) / 2;
420-
float3 rotPosWS0 = RotateVectorByQuat(unity_SpecCube0_Rotation, positionWS - probeCenterPosWS0) + probeCenterPosWS0;
421-
half3 rotReflectVector0 = RotateVectorByQuat(unity_SpecCube0_Rotation, reflectVector);
422-
float4 inverseRotation0 = -unity_SpecCube0_Rotation;
423-
inverseRotation0.w = -inverseRotation0.w;
424-
reflectVector = BoxProjectedCubemapDirection(rotReflectVector0, rotPosWS0, unity_SpecCube0_ProbePosition, unity_SpecCube0_BoxMin, unity_SpecCube0_BoxMax);
425-
reflectVector = RotateVectorByQuat(inverseRotation0, reflectVector);
426-
#else
427-
reflectVector = BoxProjectedCubemapDirection(reflectVector, positionWS, unity_SpecCube0_ProbePosition, unity_SpecCube0_BoxMin, unity_SpecCube0_BoxMax);
428-
#endif
429-
#endif // _REFLECTION_PROBE_BOX_PROJECTION
430-
half mip = PerceptualRoughnessToMipmapLevel(perceptualRoughness);
431-
half4 encodedIrradiance = half4(SAMPLE_TEXTURECUBE_LOD(unity_SpecCube0, samplerunity_SpecCube0, reflectVector, mip));
432-
433-
irradiance = DecodeHDREnvironment(encodedIrradiance, unity_SpecCube0_HDR);
434-
#endif // _REFLECTION_PROBE_BLENDING
425+
if (_REFLECTION_PROBE_BLENDING)
426+
{
427+
irradiance = CalculateIrradianceFromReflectionProbes(reflectVector, positionWS, perceptualRoughness, normalizedScreenSpaceUV);
428+
}
429+
else
430+
{
431+
if (_REFLECTION_PROBE_BOX_PROJECTION)
432+
{
433+
#if defined(REFLECTION_PROBE_ROTATION)
434+
float3 probeCenterPosWS0 = unity_SpecCube0_BoxMin.xyz + (unity_SpecCube0_BoxMax.xyz - unity_SpecCube0_BoxMin.xyz) / 2;
435+
float3 rotPosWS0 = RotateVectorByQuat(unity_SpecCube0_Rotation, positionWS - probeCenterPosWS0) + probeCenterPosWS0;
436+
half3 rotReflectVector0 = RotateVectorByQuat(unity_SpecCube0_Rotation, reflectVector);
437+
float4 inverseRotation0 = -unity_SpecCube0_Rotation;
438+
inverseRotation0.w = -inverseRotation0.w;
439+
reflectVector = BoxProjectedCubemapDirection(rotReflectVector0, rotPosWS0, unity_SpecCube0_ProbePosition, unity_SpecCube0_BoxMin, unity_SpecCube0_BoxMax);
440+
reflectVector = RotateVectorByQuat(inverseRotation0, reflectVector);
441+
#else
442+
reflectVector = BoxProjectedCubemapDirection(reflectVector, positionWS, unity_SpecCube0_ProbePosition, unity_SpecCube0_BoxMin, unity_SpecCube0_BoxMax);
443+
#endif
444+
}
445+
half mip = PerceptualRoughnessToMipmapLevel(perceptualRoughness);
446+
half4 encodedIrradiance = half4(SAMPLE_TEXTURECUBE_LOD(unity_SpecCube0, samplerunity_SpecCube0, reflectVector, mip));
447+
448+
irradiance = DecodeHDREnvironment(encodedIrradiance, unity_SpecCube0_HDR);
449+
}
435450
#else // _ENVIRONMENTREFLECTIONS_OFF
436451
irradiance = _GlossyEnvironmentColor.rgb;
437452
#endif // !_ENVIRONMENTREFLECTIONS_OFF

0 commit comments

Comments
 (0)