Make lighting use all cores

This commit is contained in:
Jarrod Doyle 2024-09-26 13:10:42 +01:00
parent dca1b1e5ed
commit cb526c634c
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 5 additions and 11 deletions

View File

@ -52,7 +52,7 @@ class Program
if (!mis.Chunks.TryGetValue("RENDPARAMS", out var rendParamsRaw)) if (!mis.Chunks.TryGetValue("RENDPARAMS", out var rendParamsRaw))
return; return;
var ambient = ((RendParams)rendParamsRaw).ambientLight * 255; var ambient = ((RendParams)rendParamsRaw).ambientLight * 255;
Timing.TimeStage("Light", () => CastScene(scene, worldRep, [.. lights], ambient)); Timing.TimeStage("Light", () => CastSceneParallel(scene, worldRep, [.. lights], ambient));
var dir = Path.GetDirectoryName(misPath); var dir = Path.GetDirectoryName(misPath);
var filename = Path.GetFileNameWithoutExtension(misPath); var filename = Path.GetFileNameWithoutExtension(misPath);
@ -214,16 +214,12 @@ class Program
return new TriangleMesh([.. vertices], [.. indices]); return new TriangleMesh([.. vertices], [.. indices]);
} }
private static void CastScene(Raytracer scene, WorldRep wr, Light[] lights, Vector3 ambientLight) private static void CastSceneParallel(Raytracer scene, WorldRep wr, Light[] lights, Vector3 ambientLight)
{ {
var hdr = wr.DataHeader.LightmapFormat == 2; var hdr = wr.DataHeader.LightmapFormat == 2;
var cells = wr.Cells; Parallel.ForEach(wr.Cells, cell =>
for (var cellIdx = 0; cellIdx < cells.Length; cellIdx++)
{ {
Console.Write($"\rLighting cell... {cellIdx + 1}/{cells.Length}");
var cell = cells[cellIdx];
var numPolys = cell.PolyCount; var numPolys = cell.PolyCount;
var numRenderPolys = cell.RenderPolyCount; var numRenderPolys = cell.RenderPolyCount;
var numPortalPolys = cell.PortalPolyCount; var numPortalPolys = cell.PortalPolyCount;
@ -232,7 +228,7 @@ class Program
// Portal polys can be render polys (e.g. water) but we're ignoring them for now // Portal polys can be render polys (e.g. water) but we're ignoring them for now
if (numRenderPolys == 0 || numPortalPolys >= numPolys) if (numRenderPolys == 0 || numPortalPolys >= numPolys)
{ {
continue; return;
} }
var maxPolyIdx = Math.Min(numRenderPolys, numPolys - numPortalPolys); var maxPolyIdx = Math.Min(numRenderPolys, numPolys - numPortalPolys);
@ -348,9 +344,7 @@ class Program
cellIdxOffset += poly.VertexCount; cellIdxOffset += poly.VertexCount;
} }
} });
Console.Write("\n");
} }
private static float CalculateLightStrengthAtPoint(Light light, Vector3 point, Plane plane) private static float CalculateLightStrengthAtPoint(Light light, Vector3 point, Plane plane)