Skip to content

Commit af804c3

Browse files
pigselatedEvergreen
authored andcommitted
Fix Unified Baking
1 parent 20c3530 commit af804c3

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

Packages/com.unity.render-pipelines.core/Editor/PathTracing/BakeInputSerialization.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -708,11 +708,11 @@ public static UInt64 TilingModeToLightmapExpandedBufferSize(TilingMode tilingMod
708708
// TODO: We need to change the naming of the entries in the enum see: https://jira.unity3d.com/browse/GFXFEAT-728
709709
switch (tilingMode)
710710
{
711-
case TilingMode.None: bufferSize = 524288; break; // UI: Highest Performance
712-
case TilingMode.Quarter: bufferSize = 262144; break; // UI: High Performance
713-
case TilingMode.Sixteenth: bufferSize = 131072; break; // UI: Automatic (but it is not automatic)
714-
case TilingMode.Sixtyfourth: bufferSize = 65536; break; // UI: Low Memory Usage
715-
case TilingMode.TwoHundredFiftySixth: bufferSize = 32768; break; // UI: Lowest Memory Usage
711+
case TilingMode.None: bufferSize = 1048576; break; // UI: Highest Performance
712+
case TilingMode.Quarter: bufferSize = 524288; break; // UI: High Performance
713+
case TilingMode.Sixteenth: bufferSize = 262144; break; // UI: Automatic (but it is not automatic)
714+
case TilingMode.Sixtyfourth: bufferSize = 131072; break; // UI: Low Memory Usage
715+
case TilingMode.TwoHundredFiftySixth: bufferSize = 65536; break; // UI: Lowest Memory Usage
716716
default: Debug.Assert(false, "Unknown tiling mode."); break;
717717
}
718718
return math.max(bufferSize, kMinBufferSize);

Packages/com.unity.render-pipelines.core/Editor/PathTracing/LightBakerStrangler.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ static SetLightmappingUnifiedBaker()
8989
try
9090
{
9191
var lightmappingType = typeof(UnityEditor.Lightmapping);
92-
var unifiedBakerProperty = lightmappingType.GetProperty("UnifiedBaker",
92+
var unifiedBakerProperty = lightmappingType.GetProperty("UnifiedBaker",
9393
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
94-
94+
9595
if (unifiedBakerProperty != null && unifiedBakerProperty.CanWrite)
9696
{
9797
#if UNIFIED_BAKER
@@ -274,6 +274,7 @@ private static bool LightmapRequestOutputTypeToIntegratedOutputType(LightmapRequ
274274
case LightmapRequestOutputType.Normal:
275275
case LightmapRequestOutputType.ChartIndex:
276276
case LightmapRequestOutputType.OverlapPixelIndex:
277+
case LightmapRequestOutputType.IrradianceEnvironment:
277278
return false;
278279
}
279280
Debug.Assert(false, $"Error unknown LightmapRequestOutputType {type}.");
@@ -1386,6 +1387,10 @@ internal static Result ExecuteLightmapRequests(
13861387
byte[] compressedBlack = blackOutput.EncodeToR2D();
13871388
try
13881389
{
1390+
// Environment is part of indirect irradiance.
1391+
if (request.outputTypeMask.HasFlag(LightmapRequestOutputType.IrradianceEnvironment))
1392+
File.WriteAllBytes(request.outputFolderPath + $"/irradianceEnvironment{lightmapIndex}.r2d", compressedBlack);
1393+
13891394
// occupiedTexels is needed for analytics, don't fail the bake if we cannot write it.
13901395
UInt64 occupiedTexels = (UInt64)lightmapRequestData.atlassing.m_EstimatedTexelCount;
13911396
if (request.outputTypeMask.HasFlag(LightmapRequestOutputType.Occupancy))

Packages/com.unity.render-pipelines.core/Runtime/PathTracing/LightmapIntegrationHelpers.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ public static void ReferenceBoxFilterBlueChannelRenderTexture(CommandBuffer cmd,
508508
// these regions can overlap for multiple instances, when the instance's UV layout doesn't fully
509509
// cover the [0; 1] range. We pack based on tight bounding boxes of the instance's UVs,
510510
// but lightmap STs are with respect to the instance's entire UV layout.
511+
// It is assumed that the lightmap offset is aligned to either half or full texel.
511512
internal static void ComputeOccupiedTexelRegionForInstance(
512513
uint lightmapWidth,
513514
uint lightmapHeight,
@@ -540,9 +541,35 @@ internal static void ComputeOccupiedTexelRegionForInstance(
540541
// Clamp the occupied region to the lightmap bounds.
541542
float occupiedTexelOffsetXFloat = Mathf.Max(occupiedOffsetX, 0f);
542543
float occupiedTexelOffsetYFloat = Mathf.Max(occupiedOffsetY, 0f);
543-
Debug.Assert(Mathf.Abs(Mathf.Round(occupiedTexelOffsetXFloat) - occupiedTexelOffsetXFloat) < 0.001f, $"Expected offset (X) to be very close to zero. Was {occupiedTexelOffsetXFloat % 1}. Was the scene baked with the same resolution as it was atlassed for?");
544-
Debug.Assert(Mathf.Abs(Mathf.Round(occupiedTexelOffsetYFloat) - occupiedTexelOffsetYFloat) < 0.001f, $"Expected offset (Y) to be very close to zero. Was {occupiedTexelOffsetYFloat % 1}. Was the scene baked with the same resolution as it was atlassed for?");
545-
occupiedTexelOffset = new((int)Mathf.Round(occupiedTexelOffsetXFloat), (int)Mathf.Round(occupiedTexelOffsetYFloat));
544+
545+
// Get the fractional value
546+
float offsetFracX = Mathf.Abs(occupiedTexelOffsetXFloat - Mathf.Floor(occupiedTexelOffsetXFloat));
547+
float offsetFracY = Mathf.Abs(occupiedTexelOffsetYFloat - Mathf.Floor(occupiedTexelOffsetYFloat));
548+
549+
// Check that we are either aligned to half or full texel
550+
float distanceToHalfX = Mathf.Abs(offsetFracX - 0.5f);
551+
float distanceToHalfY = Mathf.Abs(offsetFracY - 0.5f);
552+
float distanceToWholeX = Mathf.Abs(Mathf.Round(offsetFracX) - offsetFracX);
553+
float distanceToWholeY = Mathf.Abs(Mathf.Round(offsetFracY) - offsetFracY);
554+
555+
Debug.Assert(Mathf.Min(distanceToHalfX, distanceToWholeX) < 0.001f, $"Expected offset (X) to align with an offset of 0.5 (half texel) or 0.0 (whole texel). Was {occupiedTexelOffsetXFloat}. Was the scene baked with the same resolution as it was atlassed for?");
556+
Debug.Assert(Mathf.Min(distanceToHalfY, distanceToWholeY) < 0.001f, $"Expected offset (Y) to align with an offset of 0.5 (half texel) or 0.0 (whole texel). Was {occupiedTexelOffsetYFloat}. Was the scene baked with the same resolution as it was atlassed for?");
557+
Debug.Assert((distanceToHalfX < distanceToWholeX && distanceToHalfY < distanceToWholeY) || (distanceToHalfX > distanceToWholeX && distanceToHalfY > distanceToWholeY), "Texel alignment with an offset of 0.5 (half texel) or 0.0 (whole texel) must be the same for X and Y");
558+
559+
int occupiedTexelOffsetX = 0;
560+
int occupiedTexelOffsetY = 0;
561+
562+
if (distanceToHalfX < distanceToWholeX)
563+
occupiedTexelOffsetX = (int)Mathf.Floor(occupiedTexelOffsetXFloat);
564+
else
565+
occupiedTexelOffsetX = (int)Mathf.Round(occupiedTexelOffsetXFloat);
566+
567+
if (distanceToHalfY < distanceToWholeY)
568+
occupiedTexelOffsetY = (int)Mathf.Floor(occupiedTexelOffsetYFloat);
569+
else
570+
occupiedTexelOffsetY = (int)Mathf.Round(occupiedTexelOffsetYFloat);
571+
572+
occupiedTexelOffset = new(occupiedTexelOffsetX, occupiedTexelOffsetY);
546573
}
547574

548575
// Computes the bounding box of a set of UV coordinates.

0 commit comments

Comments
 (0)