From 28d7bb586f50c59ffb269d4f9171d4542d01ef1e Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Mon, 31 Dec 2018 17:37:19 +0100 Subject: [PATCH] - remove some code duplication --- src/lightmap/lightmap.cpp | 153 ++++++++++++++++---------------------- src/lightmap/lightmap.h | 5 +- 2 files changed, 70 insertions(+), 88 deletions(-) diff --git a/src/lightmap/lightmap.cpp b/src/lightmap/lightmap.cpp index 2103091..2ab262d 100644 --- a/src/lightmap/lightmap.cpp +++ b/src/lightmap/lightmap.cpp @@ -54,6 +54,50 @@ LightmapBuilder::~LightmapBuilder() { } +void LightmapBuilder::CreateLightmaps(FLevel &doomMap, int sampleDistance, int textureSize) +{ + map = &doomMap; + samples = sampleDistance; + textureWidth = textureSize; + textureHeight = textureSize; + + mesh = std::make_unique(doomMap); + + CreateSurfaceLights(); + CreateTraceTasks(); + + SetupLightCellGrid(); + + SetupTaskProcessed("Tracing cells", grid.blocks.size()); + Worker::RunJob(grid.blocks.size(), [=](int id) { + LightBlock(id); + PrintTaskProcessed(); + }); + printf("Cells traced: %i \n\n", tracedTexels); + + SetupTaskProcessed("Tracing surfaces", traceTasks.size()); + Worker::RunJob(traceTasks.size(), [=](int id) { + LightSurface(id); + PrintTaskProcessed(); + }); + printf("Texels traced: %i \n\n", tracedTexels); + + if (LightBounce > 0) + { + SetupTaskProcessed("Tracing indirect", traceTasks.size()); + Worker::RunJob(traceTasks.size(), [=](int id) { + LightIndirect(id); + PrintTaskProcessed(); + }); + printf("Texels traced: %i \n\n", tracedTexels); + } + + for (auto &surf : mesh->surfaces) + { + FinishSurface(surf.get()); + } +} + BBox LightmapBuilder::GetBoundsFromSurface(const Surface *surface) { Vec3 low(M_INFINITY, M_INFINITY, M_INFINITY); @@ -597,34 +641,12 @@ void LightmapBuilder::LightSurface(const int taskid) { const TraceTask &task = traceTasks[taskid]; TraceSurface(mesh->surfaces[task.surface].get(), task.offset); - - std::unique_lock lock(mutex); - - int numtasks = traceTasks.size(); - int lastproc = processed * 100 / numtasks; - processed++; - int curproc = processed * 100 / numtasks; - if (lastproc != curproc || processed == 1) - { - float remaining = (float)processed / (float)numtasks; - printf("%i%c done\r", (int)(remaining * 100.0f), '%'); - } } void LightmapBuilder::LightIndirect(const int taskid) { const TraceTask &task = traceTasks[taskid]; TraceIndirectLight(mesh->surfaces[task.surface].get(), task.offset); - - int numtasks = traceTasks.size(); - int lastproc = processed * 100 / numtasks; - processed++; - int curproc = processed * 100 / numtasks; - if (lastproc != curproc || processed == 1) - { - float remaining = (float)processed / (float)numtasks; - printf("%i%c done\r", (int)(remaining * 100.0f), '%'); - } } void LightmapBuilder::CreateSurfaceLights() @@ -667,59 +689,6 @@ void LightmapBuilder::CreateSurfaceLights() } } -void LightmapBuilder::CreateLightmaps(FLevel &doomMap, int sampleDistance, int textureSize) -{ - map = &doomMap; - samples = sampleDistance; - textureWidth = textureSize; - textureHeight = textureSize; - - mesh = std::make_unique(doomMap); - - CreateSurfaceLights(); - CreateTraceTasks(); - - printf("-------------- Tracing cells ---------------\n"); - - SetupLightCellGrid(); - - processed = 0; - tracedTexels = 0; - Worker::RunJob(grid.blocks.size(), [=](int id) { - LightBlock(id); - }); - - printf("Cells traced: %i \n\n", tracedTexels); - - printf("------------- Tracing surfaces -------------\n"); - - tracedTexels = 0; - processed = 0; - Worker::RunJob(traceTasks.size(), [=](int id) { - LightSurface(id); - }); - - printf("Texels traced: %i \n\n", tracedTexels); - - if (LightBounce > 0) - { - printf("------------- Tracing indirect -------------\n"); - - tracedTexels = 0; - processed = 0; - Worker::RunJob(traceTasks.size(), [=](int id) { - LightIndirect(id); - }); - - printf("Texels traced: %i \n\n", tracedTexels); - } - - for (auto &surf : mesh->surfaces) - { - FinishSurface(surf.get()); - } -} - void LightmapBuilder::SetupLightCellGrid() { BBox worldBBox = mesh->CollisionMesh->get_bbox(); @@ -819,18 +788,6 @@ void LightmapBuilder::LightBlock(int id) block.z = 0; block.layers = 0; } - - std::unique_lock lock(mutex); - - int numblocks = grid.blocks.size(); - int lastproc = processed * 100 / numblocks; - processed++; - int curproc = processed * 100 / numblocks; - if (lastproc != curproc || processed == 1) - { - float remaining = (float)processed / (float)numblocks; - printf("%i%c cells done\r", (int)(remaining * 100.0f), '%'); - } } void LightmapBuilder::AddLightmapLump(FWadWriter &wadFile) @@ -969,6 +926,28 @@ void LightmapBuilder::AddLightmapLump(FWadWriter &wadFile) zout.Write(buffer.data(), lumpFile.BufferAt() - lumpFile.Buffer()); } +void LightmapBuilder::SetupTaskProcessed(const char *name, int total) +{ + printf("-------------- %s ---------------\n", name); + + processed = 0; + progresstotal = total; +} + +void LightmapBuilder::PrintTaskProcessed() +{ + std::unique_lock lock(mutex); + + int lastproc = processed * 100 / progresstotal; + processed++; + int curproc = processed * 100 / progresstotal; + if (lastproc != curproc || processed == 1) + { + float remaining = (float)processed / (float)progresstotal; + printf("%i%c done\r", (int)(remaining * 100.0f), '%'); + } +} + ///////////////////////////////////////////////////////////////////////////// LightmapTexture::LightmapTexture(int width, int height) : textureWidth(width), textureHeight(height) diff --git a/src/lightmap/lightmap.h b/src/lightmap/lightmap.h index b5a9c18..5d1a4f9 100644 --- a/src/lightmap/lightmap.h +++ b/src/lightmap/lightmap.h @@ -107,6 +107,9 @@ private: void CreateSurfaceLights(); + void SetupTaskProcessed(const char *name, int total); + void PrintTaskProcessed(); + uint16_t *AllocTextureRoom(Surface *surface, int *x, int *y); FLevel *map; @@ -118,11 +121,11 @@ private: std::vector> surfaceLights; std::vector> textures; std::vector traceTasks; - int extraSamples = 2; int tracedTexels = 0; LightCellGrid grid; std::mutex mutex; int processed = 0; + int progresstotal = 0; };