Initialize lightmap texture in SetLevelMesh

Create VkLightmap class on VulkanRenderDevice
This commit is contained in:
Magnus Norddahl 2023-09-01 00:03:18 +02:00
commit cff50b8087
15 changed files with 125 additions and 200 deletions

View file

@ -183,6 +183,7 @@ void VulkanRenderDevice::InitializeState()
mDescriptorSetManager.reset(new VkDescriptorSetManager(this));
mRenderPassManager.reset(new VkRenderPassManager(this));
mRaytrace.reset(new VkRaytrace(this));
mLightmap.reset(new VkLightmap(this));
mBufferManager->Init();
@ -475,61 +476,6 @@ void VulkanRenderDevice::BeginFrame()
mDescriptorSetManager->BeginFrame();
}
void VulkanRenderDevice::GenerateLightmap(TArray<uint16_t>& LMTextureData, int LMTextureSize, hwrenderer::LevelMesh& mesh)
{
if (false && mesh.surfaces.size() > 0)
{
Printf("Running VkLightmap.\n");
//VkLightmap lightmap(this);
//lightmap.Raytrace(&mesh);
Printf("Copying data.\n");
// TODO refactor
auto clamp = [](float a, float min, float max) -> float { return a < min ? min : a > max ? max : a; };
std::sort(mesh.surfaces.begin(), mesh.surfaces.end(), [](const std::unique_ptr<hwrenderer::Surface>& a, const std::unique_ptr<hwrenderer::Surface>& b) { return a->texHeight != b->texHeight ? a->texHeight > b->texHeight : a->texWidth > b->texWidth; });
RectPacker packer(LMTextureSize, LMTextureSize, RectPacker::Spacing(0));
for (auto& surface : mesh.surfaces)
{
mesh.FinishSurface(LMTextureSize, LMTextureSize, packer, *surface.get());
uint16_t* currentTexture = LMTextureData.Data() + (LMTextureSize * LMTextureSize * 3) * surface->atlasPageIndex;
FVector3* colorSamples = surface->texPixels.data();
// store results to lightmap texture
for (int i = 0; i < surface->texHeight; i++)
{
for (int j = 0; j < surface->texWidth; j++)
{
// get texture offset
int offs = ((surface->texWidth * (i + surface->atlasY)) + surface->atlasX) * 3;
// convert RGB to bytes
currentTexture[offs + j * 3 + 0] = floatToHalf(clamp(colorSamples[i * surface->texWidth + j].X, 0.0f, 65000.0f));
currentTexture[offs + j * 3 + 1] = floatToHalf(clamp(colorSamples[i * surface->texWidth + j].Y, 0.0f, 65000.0f));
currentTexture[offs + j * 3 + 2] = floatToHalf(clamp(colorSamples[i * surface->texWidth + j].Z, 0.0f, 65000.0f));
}
}
}
}
}
void VulkanRenderDevice::InitLightmap(int LMTextureSize, int LMTextureCount, TArray<uint16_t>& LMTextureData)
{
if (LMTextureData.Size() > 0)
{
GetTextureManager()->SetLightmap(LMTextureSize, LMTextureCount, LMTextureData);
LMTextureData.Reset(); // We no longer need this, release the memory
}
}
void VulkanRenderDevice::Draw2D()
{
::Draw2D(twod, *RenderState(0));
@ -587,6 +533,61 @@ void VulkanRenderDevice::PrintStartupLog()
void VulkanRenderDevice::SetLevelMesh(hwrenderer::LevelMesh* mesh)
{
mRaytrace->SetLevelMesh(mesh);
if (mesh->Surfaces.Size() > 0)
{
#if 0 // To do: GetLightmap()->Raytrace should output directly to the lightmap texture instead of forcing us to download it to the CPU first
GetTextureManager()->CreateLightmap(mesh->LMTextureSize, mesh->LMTextureCount);
GetCommands()->WaitForCommands(false);
GetLightmap()->Raytrace(mesh);
#else // Temporary version using the old zdray Raytracer code as-is
Printf("Running VkLightmap.\n");
GetLightmap()->Raytrace(mesh);
Printf("Copying data.\n");
// TODO refactor
auto clamp = [](float a, float min, float max) -> float { return a < min ? min : a > max ? max : a; };
// BUG: This is a destructive action as it reorders the surfaces array that is indexed by MeshSurfaces
std::sort(mesh->Surfaces.begin(), mesh->Surfaces.end(), [](const hwrenderer::Surface& a, const hwrenderer::Surface& b) { return a.texHeight != b.texHeight ? a.texHeight > b.texHeight : a.texWidth > b.texWidth; });
RectPacker packer(mesh->LMTextureSize, mesh->LMTextureSize, RectPacker::Spacing(0));
TArray<uint16_t> LMTextureData;
LMTextureData.Resize(mesh->LMTextureSize * mesh->LMTextureSize * mesh->LMTextureCount * 3);
for (auto& surface : mesh->Surfaces)
{
mesh->FinishSurface(mesh->LMTextureSize, mesh->LMTextureSize, packer, surface);
uint16_t* currentTexture = LMTextureData.Data() + (mesh->LMTextureSize * mesh->LMTextureSize * 3) * surface.atlasPageIndex;
FVector3* colorSamples = surface.texPixels.data();
// store results to lightmap texture
for (int i = 0; i < surface.texHeight; i++)
{
for (int j = 0; j < surface.texWidth; j++)
{
// get texture offset
int offs = ((surface.texWidth * (i + surface.atlasY)) + surface.atlasX) * 3;
// convert RGB to bytes
currentTexture[offs + j * 3 + 0] = floatToHalf(clamp(colorSamples[i * surface.texWidth + j].X, 0.0f, 65000.0f));
currentTexture[offs + j * 3 + 1] = floatToHalf(clamp(colorSamples[i * surface.texWidth + j].Y, 0.0f, 65000.0f));
currentTexture[offs + j * 3 + 2] = floatToHalf(clamp(colorSamples[i * surface.texWidth + j].Z, 0.0f, 65000.0f));
}
}
}
GetTextureManager()->SetLightmap(mesh->LMTextureSize, mesh->LMTextureCount, LMTextureData);
#endif
}
}
void VulkanRenderDevice::SetShadowMaps(const TArray<float>& lights, hwrenderer::LevelAABBTree* tree, bool newTree)