Skip to content

Commit

Permalink
feat: #98 Update bake lightmap for sponza mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Jul 28, 2020
1 parent f31ec26 commit 874e79c
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 124 deletions.
22 changes: 11 additions & 11 deletions Assets/BuiltIn/Shader/Compute/HLSL/IrradianceSH.hlsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Number of thread
#define MAX_NUM_THREAD 120
#define MAX_NUM_THREAD 128

// MAX_NUM_THREAD * 6 faces
#define TANGENT_COUNT 720
#define TANGENT_COUNT 768

// NUM FACE
#define NUM_FACE 6
Expand Down Expand Up @@ -164,14 +164,14 @@ void main(
uint id = (threadID * NUM_FACE + faceID) * 9;

// Write result
OutputBuffer[id + 0] = float4(ResultSH[0][0], 0.0);
OutputBuffer[id + 1] = float4(ResultSH[0][1], 0.0);
OutputBuffer[id + 2] = float4(ResultSH[0][2], 0.0);
OutputBuffer[id + 3] = float4(ResultSH[0][3], 0.0);
OutputBuffer[id + 4] = float4(ResultSH[0][4], 0.0);
OutputBuffer[id + 5] = float4(ResultSH[0][5], 0.0);
OutputBuffer[id + 6] = float4(ResultSH[0][6], 0.0);
OutputBuffer[id + 7] = float4(ResultSH[0][7], 0.0);
OutputBuffer[id + 8] = float4(ResultSH[0][8], 0.0);
OutputBuffer[id + 0] += float4(ResultSH[0][0], 0.0);
OutputBuffer[id + 1] += float4(ResultSH[0][1], 0.0);
OutputBuffer[id + 2] += float4(ResultSH[0][2], 0.0);
OutputBuffer[id + 3] += float4(ResultSH[0][3], 0.0);
OutputBuffer[id + 4] += float4(ResultSH[0][4], 0.0);
OutputBuffer[id + 5] += float4(ResultSH[0][5], 0.0);
OutputBuffer[id + 6] += float4(ResultSH[0][6], 0.0);
OutputBuffer[id + 7] += float4(ResultSH[0][7], 0.0);
OutputBuffer[id + 8] += float4(ResultSH[0][8], 0.0);
}
}
2 changes: 1 addition & 1 deletion Assets/Sponza/Sponza.smesh
Git LFS file not shown
Binary file removed Assets/Sponza/mesh_charts00.png
Binary file not shown.
Binary file removed Assets/Sponza/mesh_charts01.png
Binary file not shown.
Binary file removed Assets/Sponza/mesh_charts02.png
Binary file not shown.
Binary file removed Assets/Sponza/mesh_charts03.png
Binary file not shown.
Binary file removed Assets/Sponza/mesh_charts04.png
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Skylicht
{
namespace Lightmapper
{
int CLightmapper::s_numThread = 120;
int CLightmapper::s_numThread = 128;
int CLightmapper::s_hemisphereBakeSize = 128;

CLightmapper::CLightmapper() :
Expand All @@ -54,6 +54,13 @@ namespace Skylicht

void CLightmapper::initBaker(u32 hemisphereBakeSize)
{
// adjust size
u32 size = 16;
while (size < hemisphereBakeSize && size <= 128)
{
size = size * 2;
}

if (m_singleBaker != NULL)
delete m_singleBaker;

Expand All @@ -63,7 +70,7 @@ namespace Skylicht
if (m_gpuBaker != NULL)
delete m_gpuBaker;

s_hemisphereBakeSize = hemisphereBakeSize;
s_hemisphereBakeSize = size;

m_singleBaker = new CBaker();
m_multiBaker = new CMTBaker();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ This file is part of the "Skylicht Engine".

#include "CBaker.h"

#define MAX_NUM_THREAD 120
#define MAX_NUM_THREAD 128

namespace Skylicht
{
Expand Down
17 changes: 10 additions & 7 deletions Samples/LightmapUV/Source/SampleLightmapUV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ void SampleLightmapUV::onInitApp()
core::vector3df direction = core::vector3df(-2.0f, -7.0f, -1.5f);
lightTransform->setOrientation(direction, CTransform::s_oy);

// 3D model
// 3D model
#ifdef LIGHTMAP_SPONZA
m_model = CMeshManager::getInstance()->loadModel("Sponza/Sponza.dae", "Sponza/Textures");
#else
m_model = CMeshManager::getInstance()->loadModel("SampleModels/Gazebo/gazebo.obj", "");
// m_model = CMeshManager::getInstance()->loadModel("Sponza/Sponza.dae", "Sponza/Textures");
#endif

if (m_model != NULL)
{
Expand Down Expand Up @@ -163,7 +166,7 @@ void SampleLightmapUV::onInitApp()
void SampleLightmapUV::runThread()
{
// generate uv in thread
m_unwrap.generate(2048, 5.0f);
m_unwrap.generate(2048, 0.5f);
m_unwrap.generateUVImage();

// write to bin folder output layout uv
Expand Down Expand Up @@ -227,18 +230,18 @@ void SampleLightmapUV::updateMeshUV()
}
}

// test exporter
/*
// Exporter result
#ifdef LIGHTMAP_SPONZA
CMeshManager::getInstance()->exportModel(
m_renderMesh->getEntities().pointer(),
m_renderMesh->getEntities().size(),
"../Assets/Sponza/Sponza.smesh");
*/

#else
CMeshManager::getInstance()->exportModel(
m_renderMesh->getEntities().pointer(),
m_renderMesh->getEntities().size(),
"../Assets/SampleModels/Gazebo/gazebo.smesh");
#endif

// Update material
core::array<IImage*> arrayTexture;
Expand Down
2 changes: 2 additions & 0 deletions Samples/LightmapUV/Source/SampleLightmapUV.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "UnwrapUV/CUnwrapUV.h"

#define LIGHTMAP_SPONZA

class SampleLightmapUV :
public IApplicationEventReceiver,
public SkylichtSystem::IThreadCallback
Expand Down
50 changes: 42 additions & 8 deletions Samples/Lightmapping/Source/CViewBakeLightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ CViewBakeLightmap::CViewBakeLightmap() :
m_timeSpentFromLastSave(0),
m_numRenderers(0),
m_numIndices(0),
m_numVertices(0)
m_numVertices(0),
m_guiObject(NULL),
m_font(NULL)
{
for (int i = 0; i < MAX_LIGHTMAP_ATLAS; i++)
m_lmRasterize[i] = NULL;
}

CViewBakeLightmap::~CViewBakeLightmap()
{
m_guiObject->remove();
if (m_guiObject != NULL)
m_guiObject->remove();

delete m_font;
if (m_font != NULL)
delete m_font;

for (int i = 0; i < MAX_LIGHTMAP_ATLAS; i++)
{
Expand Down Expand Up @@ -68,6 +72,14 @@ int CViewBakeLightmap::getRasterisationIndex(Lightmapper::CRasterisation *raster

void CViewBakeLightmap::onInit()
{
/*
// enable render indirect
CDeferredRP::enableRenderIndirect(false);
// switch to demo view
CViewManager::getInstance()->getLayer(0)->changeView<CViewDemo>();
return;
*/

CContext *context = CContext::getInstance();
CZone *zone = context->getActiveZone();
CEntityManager *entityMgr = zone->getEntityManager();
Expand Down Expand Up @@ -231,11 +243,18 @@ void CViewBakeLightmap::onUpdate()
}

int lmIndex = (int)vertices[v1].Lightmap.Z;
m_currentRasterisation = createGetLightmapRasterisation(lmIndex);

m_pixel = m_currentRasterisation->setTriangle(positions, uvs, normals, tangents, pass);

m_lastTris = m_currentTris;
if (lmIndex >= 0)
{
m_currentRasterisation = createGetLightmapRasterisation(lmIndex);
m_pixel = m_currentRasterisation->setTriangle(positions, uvs, normals, tangents, pass);
m_lastTris = m_currentTris;
}
else
{
// skip this triangle
m_currentTris++;
continue;
}
}

core::vector3df outPos;
Expand Down Expand Up @@ -459,6 +478,21 @@ void CViewBakeLightmap::saveProgress()
io::IWriteFile *file = getIrrlichtDevice()->getFileSystem()->createAndWriteFile("LightmapProgress.dat");
file->write(stream->getData(), stream->getSize());
file->drop();

// write current output to review
core::dimension2du size(m_lightmapSize, m_lightmapSize);
char outFileName[512];
IVideoDriver *driver = getVideoDriver();

for (int i = 0; i < m_numberRasterize; i++)
{
unsigned char *data = m_lmRasterize[i]->getLightmapData();

IImage *img = driver->createImageFromData(video::ECF_R8G8B8, size, data);
sprintf(outFileName, "LightMapRasterize_review_bounce_%d_%d.png", m_lightBounce, i);
driver->writeImageToFile(img, outFileName);
img->drop();
}
}

void CViewBakeLightmap::loadProgress()
Expand Down
2 changes: 2 additions & 0 deletions Samples/Lightmapping/Source/CViewBakeLightmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#define MAX_LIGHTMAP_ATLAS 40

#define LIGHTMAP_SPONZA

class CViewBakeLightmap : public CView
{
protected:
Expand Down
Loading

0 comments on commit 874e79c

Please sign in to comment.