From b5678d5f86ed09af73268b6f32bd214d47fec52e Mon Sep 17 00:00:00 2001 From: RaveYard <29225776+MrRaveYard@users.noreply.github.com> Date: Fri, 1 Sep 2023 12:02:03 +0200 Subject: [PATCH] Use lightmapper output --- .../rendering/hwrenderer/data/hw_levelmesh.h | 28 ++++--------------- .../vulkan/accelstructs/vk_lightmap.cpp | 25 ++++++++++------- 2 files changed, 20 insertions(+), 33 deletions(-) diff --git a/src/common/rendering/hwrenderer/data/hw_levelmesh.h b/src/common/rendering/hwrenderer/data/hw_levelmesh.h index b7dcde0f2a..e8e745f518 100644 --- a/src/common/rendering/hwrenderer/data/hw_levelmesh.h +++ b/src/common/rendering/hwrenderer/data/hw_levelmesh.h @@ -106,6 +106,11 @@ struct Surface // Output lightmap for the surface std::vector texPixels; + + // Lightmapper has a lot of additional padding around the borders + int lightmapperAtlasPage = -1; + int lightmapperAtlasX = -1; + int lightmapperAtlasY = -1; }; @@ -223,29 +228,6 @@ public: FVector3 end = start + direction * std::max(maxDist - 10.0f, 0.0f); return !TriangleMeshShape::find_any_hit(Collision.get(), start, end); } - - inline void FinishSurface(int lightmapTextureWidth, int lightmapTextureHeight, RectPacker& packer, Surface& surface) - { - int sampleWidth = surface.texWidth; - int sampleHeight = surface.texHeight; - - auto result = packer.insert(sampleWidth, sampleHeight); - int x = result.pos.x, y = result.pos.y; - surface.atlasPageIndex = (int)result.pageIndex; - - // calculate final texture coordinates - for (unsigned i = 0; i < surface.uvs.Size(); i++) - { - auto& u = surface.uvs[i].X; - auto& v = surface.uvs[i].Y; - u = (u + x) / (float)lightmapTextureWidth; - v = (v + y) / (float)lightmapTextureHeight; - } - - surface.atlasX = x; - surface.atlasY = y; - - } }; } // namespace diff --git a/src/common/rendering/vulkan/accelstructs/vk_lightmap.cpp b/src/common/rendering/vulkan/accelstructs/vk_lightmap.cpp index 044a27e983..ba87bd42bb 100644 --- a/src/common/rendering/vulkan/accelstructs/vk_lightmap.cpp +++ b/src/common/rendering/vulkan/accelstructs/vk_lightmap.cpp @@ -50,6 +50,11 @@ void VkLightmap::Raytrace(hwrenderer::LevelMesh* level) } FinishCommands(); + + for (size_t pageIndex = 0; pageIndex < atlasImages.size(); pageIndex++) + { + DownloadAtlasImage(pageIndex); + } } void VkLightmap::BeginCommands() @@ -97,13 +102,13 @@ void VkLightmap::RenderAtlasImage(size_t pageIndex) for (unsigned int i = 0; i < mesh->Surfaces.Size(); i++) { hwrenderer::Surface* targetSurface = &mesh->Surfaces[i]; - if (targetSurface->atlasPageIndex != pageIndex) + if (targetSurface->lightmapperAtlasPage != pageIndex) continue; VkViewport viewport = {}; viewport.maxDepth = 1; - viewport.x = (float)targetSurface->atlasX - 1; - viewport.y = (float)targetSurface->atlasY - 1; + viewport.x = (float)targetSurface->lightmapperAtlasX - 1; + viewport.y = (float)targetSurface->lightmapperAtlasY - 1; viewport.width = (float)(targetSurface->texWidth + 2); viewport.height = (float)(targetSurface->texHeight + 2); cmdbuffer->setViewport(0, 1, &viewport); @@ -200,9 +205,9 @@ void VkLightmap::CreateAtlasImages() hwrenderer::Surface* surface = &mesh->Surfaces[i]; auto result = packer.insert(surface->texWidth + 2, surface->texHeight + 2); - surface->atlasX = result.pos.x + 1; - surface->atlasY = result.pos.y + 1; - surface->atlasPageIndex = (int)result.pageIndex; + surface->lightmapperAtlasX = result.pos.x + 1; + surface->lightmapperAtlasY = result.pos.y + 1; + surface->lightmapperAtlasPage = (int)result.pageIndex; } for (size_t pageIndex = 0; pageIndex < packer.getNumPages(); pageIndex++) @@ -307,18 +312,18 @@ void VkLightmap::DownloadAtlasImage(size_t pageIndex) for (unsigned int i = 0; i < mesh->Surfaces.Size(); i++) { hwrenderer::Surface* surface = &mesh->Surfaces[i]; - if (surface->atlasPageIndex != pageIndex) + if (surface->lightmapperAtlasPage != pageIndex) continue; - int atlasX = surface->atlasX; - int atlasY = surface->atlasY; + int lightmapperAtlasX = surface->lightmapperAtlasX; + int lightmapperAtlasY = surface->lightmapperAtlasY; int sampleWidth = surface->texWidth; int sampleHeight = surface->texHeight; for (int y = 0; y < sampleHeight; y++) { FVector3* dest = &surface->texPixels[y * sampleWidth]; - hvec4* src = &pixels[atlasX + (atlasY + y) * atlasImageSize]; + hvec4* src = &pixels[lightmapperAtlasX + (lightmapperAtlasY + y) * atlasImageSize]; for (int x = 0; x < sampleWidth; x++) { dest[x] = src[x].xyz();