|
|
@@ -1,6 +1,6 @@
|
|
|
//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
|
|
|
//**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
|
|
|
-#include "BsReflectionCubemapCache.h"
|
|
|
+#include "BsLightProbeCache.h"
|
|
|
#include "BsFileSystem.h"
|
|
|
#include "BsIReflectable.h"
|
|
|
#include "BsRTTIType.h"
|
|
|
@@ -65,11 +65,11 @@ namespace bs { namespace ct
|
|
|
return getRTTIStatic();
|
|
|
}
|
|
|
|
|
|
- ReflectionCubemapCache::~ReflectionCubemapCache()
|
|
|
+ LightProbeCache::~LightProbeCache()
|
|
|
{
|
|
|
- for (auto& entry : mTextureInfos)
|
|
|
+ for (auto& entry : mProbeInfos)
|
|
|
{
|
|
|
- if(entry.second.needsSaving)
|
|
|
+ if(entry.second.radiance.needsSaving || entry.second.irradiance.needsSaving)
|
|
|
{
|
|
|
LOGWRN("Shutting down reflection cubemap cache manager but not all textures were saved on disk before "
|
|
|
"shutdown.");
|
|
|
@@ -78,18 +78,14 @@ namespace bs { namespace ct
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- void ReflectionCubemapCache::initCache(const Path& path)
|
|
|
+ void LightProbeCache::initCache(const Path& path)
|
|
|
{
|
|
|
mDataPath = path;
|
|
|
|
|
|
auto visitFile = [&](const Path& filePath) -> bool
|
|
|
{
|
|
|
String uuid = filePath.getFilename(false);
|
|
|
-
|
|
|
- TextureInfo& texInfo = mTextureInfos[uuid];
|
|
|
- texInfo.texture = nullptr;
|
|
|
- texInfo.needsSaving = false;
|
|
|
- texInfo.dirty = false;
|
|
|
+ mProbeInfos.insert(std::make_pair(uuid, ProbeInfo()));
|
|
|
|
|
|
return true;
|
|
|
};
|
|
|
@@ -97,45 +93,120 @@ namespace bs { namespace ct
|
|
|
FileSystem::iterate(path, visitFile, nullptr, false);
|
|
|
}
|
|
|
|
|
|
- void ReflectionCubemapCache::notifyDirty(const String& uuid)
|
|
|
+ void LightProbeCache::notifyDirty(const String& uuid)
|
|
|
{
|
|
|
- auto iterFind = mTextureInfos.find(uuid);
|
|
|
- if (iterFind != mTextureInfos.end())
|
|
|
- iterFind->second.dirty = true;
|
|
|
+ auto iterFind = mProbeInfos.find(uuid);
|
|
|
+ if (iterFind != mProbeInfos.end())
|
|
|
+ {
|
|
|
+ iterFind->second.radiance.dirty = true;
|
|
|
+ iterFind->second.irradiance.dirty = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ bool LightProbeCache::isRadianceDirty(const String& uuid) const
|
|
|
+ {
|
|
|
+ auto iterFind = mProbeInfos.find(uuid);
|
|
|
+ if (iterFind != mProbeInfos.end())
|
|
|
+ return iterFind->second.radiance.dirty;
|
|
|
+
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
- bool ReflectionCubemapCache::isDirty(const String& uuid) const
|
|
|
+ bool LightProbeCache::isIrradianceDirty(const String& uuid) const
|
|
|
{
|
|
|
- auto iterFind = mTextureInfos.find(uuid);
|
|
|
- if (iterFind != mTextureInfos.end())
|
|
|
- return iterFind->second.dirty;
|
|
|
+ auto iterFind = mProbeInfos.find(uuid);
|
|
|
+ if (iterFind != mProbeInfos.end())
|
|
|
+ return iterFind->second.irradiance.dirty;
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- void ReflectionCubemapCache::setCachedTexture(const String& uuid, const SPtr<Texture>& texture)
|
|
|
+ void LightProbeCache::setCachedRadianceTexture(const String& uuid, const SPtr<Texture>& texture)
|
|
|
+ {
|
|
|
+ ProbeInfo& probeInfo = mProbeInfos[uuid];
|
|
|
+ TextureInfo& texInfo = probeInfo.radiance;
|
|
|
+ texInfo.texture = texture;
|
|
|
+ texInfo.needsSaving = true;
|
|
|
+ texInfo.dirty = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ void LightProbeCache::setCachedIrradianceTexture(const String& uuid, const SPtr<Texture>& texture)
|
|
|
{
|
|
|
- TextureInfo& texInfo = mTextureInfos[uuid];
|
|
|
+ ProbeInfo& probeInfo = mProbeInfos[uuid];
|
|
|
+ TextureInfo& texInfo = probeInfo.irradiance;
|
|
|
texInfo.texture = texture;
|
|
|
texInfo.needsSaving = true;
|
|
|
texInfo.dirty = false;
|
|
|
}
|
|
|
|
|
|
- SPtr<Texture> ReflectionCubemapCache::getCachedTexture(const String& uuid) const
|
|
|
+ SPtr<Texture> LightProbeCache::getCachedRadianceTexture(const String& uuid) const
|
|
|
{
|
|
|
- auto iterFind = mTextureInfos.find(uuid);
|
|
|
- if (iterFind == mTextureInfos.end())
|
|
|
+ auto iterFind = mProbeInfos.find(uuid);
|
|
|
+ if (iterFind == mProbeInfos.end())
|
|
|
return nullptr;
|
|
|
|
|
|
- TextureInfo& texInfo = iterFind->second;
|
|
|
+ TextureInfo& texInfo = iterFind->second.radiance;
|
|
|
if (texInfo.texture != nullptr)
|
|
|
return texInfo.texture;
|
|
|
|
|
|
- Path filePath = mDataPath + uuid + ".asset";
|
|
|
- if (!FileSystem::exists(filePath))
|
|
|
+ Path filePath = mDataPath + "R_" + uuid + ".asset";
|
|
|
+ texInfo.texture = loadCachedTexture(filePath);
|
|
|
+
|
|
|
+ return texInfo.texture;
|
|
|
+ }
|
|
|
+
|
|
|
+ SPtr<Texture> LightProbeCache::getCachedIrradianceTexture(const String& uuid) const
|
|
|
+ {
|
|
|
+ auto iterFind = mProbeInfos.find(uuid);
|
|
|
+ if (iterFind == mProbeInfos.end())
|
|
|
return nullptr;
|
|
|
|
|
|
- FileDecoder fd(filePath);
|
|
|
+ TextureInfo& texInfo = iterFind->second.irradiance;
|
|
|
+ if (texInfo.texture != nullptr)
|
|
|
+ return texInfo.texture;
|
|
|
+
|
|
|
+ Path filePath = mDataPath + "IRR_" + uuid + ".asset";
|
|
|
+ texInfo.texture = loadCachedTexture(filePath);
|
|
|
+
|
|
|
+ return texInfo.texture;
|
|
|
+ }
|
|
|
+
|
|
|
+ void LightProbeCache::unloadCachedTexture(const String& uuid)
|
|
|
+ {
|
|
|
+ auto iterFind = mProbeInfos.find(uuid);
|
|
|
+ if (iterFind != mProbeInfos.end())
|
|
|
+ {
|
|
|
+ // Not allowed to unload if it requires saving (should only happen during development time)
|
|
|
+ if (!iterFind->second.radiance.needsSaving)
|
|
|
+ iterFind->second.radiance.texture = nullptr;
|
|
|
+
|
|
|
+ if (!iterFind->second.irradiance.needsSaving)
|
|
|
+ iterFind->second.irradiance.texture = nullptr;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ void LightProbeCache::saveCache(const Path& path)
|
|
|
+ {
|
|
|
+ for(auto& entry : mProbeInfos)
|
|
|
+ {
|
|
|
+ ProbeInfo& probeInfo = entry.second;
|
|
|
+
|
|
|
+ Path radiancePath = path + "R_" + entry.first + ".asset";
|
|
|
+ saveCachedTexture(probeInfo.radiance, radiancePath);
|
|
|
+ probeInfo.radiance.needsSaving = false;
|
|
|
+
|
|
|
+ Path irradiancePath = path + "IRRR_" + entry.first + ".asset";
|
|
|
+ saveCachedTexture(probeInfo.irradiance, irradiancePath);
|
|
|
+ probeInfo.irradiance.needsSaving = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ SPtr<Texture> LightProbeCache::loadCachedTexture(const Path& path) const
|
|
|
+ {
|
|
|
+ if (!FileSystem::exists(path))
|
|
|
+ return nullptr;
|
|
|
+
|
|
|
+ FileDecoder fd(path);
|
|
|
SPtr<CachedTextureData> textureData = std::static_pointer_cast<CachedTextureData>(fd.decode());
|
|
|
|
|
|
if (textureData == nullptr || textureData->pixelData.size() == 0 || textureData->pixelData[0] == nullptr)
|
|
|
@@ -154,11 +225,11 @@ namespace bs { namespace ct
|
|
|
desc.numArraySlices = textureData->numFaces;
|
|
|
|
|
|
desc.numMips = textureData->numMips;
|
|
|
-
|
|
|
+
|
|
|
SPtr<Texture> texture = Texture::create(desc);
|
|
|
- for(UINT32 face = 0; face < textureData->numFaces; face++)
|
|
|
+ for (UINT32 face = 0; face < textureData->numFaces; face++)
|
|
|
{
|
|
|
- for(UINT32 mip = 0; mip < textureData->numMips; mip++)
|
|
|
+ for (UINT32 mip = 0; mip < textureData->numMips; mip++)
|
|
|
{
|
|
|
UINT32 srcIdx = face * textureData->numMips + mip;
|
|
|
if (srcIdx >= textureData->pixelData.size())
|
|
|
@@ -168,52 +239,33 @@ namespace bs { namespace ct
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- texInfo.texture = texture;
|
|
|
return texture;
|
|
|
}
|
|
|
|
|
|
- void ReflectionCubemapCache::unloadCachedTexture(const String& uuid)
|
|
|
+ void LightProbeCache::saveCachedTexture(const TextureInfo& texInfo, const Path& path)
|
|
|
{
|
|
|
- auto iterFind = mTextureInfos.find(uuid);
|
|
|
- if (iterFind != mTextureInfos.end())
|
|
|
- {
|
|
|
- // Not allowed to unload if it requires saving (should only happen during development time)
|
|
|
- if (!iterFind->second.needsSaving)
|
|
|
- iterFind->second.texture = nullptr;
|
|
|
- }
|
|
|
- }
|
|
|
+ if (!texInfo.needsSaving)
|
|
|
+ return;
|
|
|
|
|
|
- void ReflectionCubemapCache::saveCache(const Path& path)
|
|
|
- {
|
|
|
- for(auto& entry : mTextureInfos)
|
|
|
+ auto& texProps = texInfo.texture->getProperties();
|
|
|
+
|
|
|
+ SPtr<CachedTextureData> textureData = bs_shared_ptr_new<CachedTextureData>();
|
|
|
+ textureData->type = texProps.getTextureType();
|
|
|
+ textureData->numFaces = texProps.getNumFaces();
|
|
|
+ textureData->numMips = texProps.getNumMipmaps() + 1;
|
|
|
+
|
|
|
+ for (UINT32 face = 0; face < textureData->numFaces; face++)
|
|
|
{
|
|
|
- TextureInfo& texInfo = entry.second;
|
|
|
- if (!texInfo.needsSaving)
|
|
|
- continue;
|
|
|
-
|
|
|
- auto& texProps = texInfo.texture->getProperties();
|
|
|
-
|
|
|
- SPtr<CachedTextureData> textureData = bs_shared_ptr_new<CachedTextureData>();
|
|
|
- textureData->type = texProps.getTextureType();
|
|
|
- textureData->numFaces = texProps.getNumFaces();
|
|
|
- textureData->numMips = texProps.getNumMipmaps() + 1;
|
|
|
-
|
|
|
- for (UINT32 face = 0; face < textureData->numFaces; face++)
|
|
|
+ for (UINT32 mip = 0; mip < textureData->numMips; mip++)
|
|
|
{
|
|
|
- for (UINT32 mip = 0; mip < textureData->numMips; mip++)
|
|
|
- {
|
|
|
- SPtr<PixelData> pixelData = texProps.allocBuffer(face, mip);
|
|
|
- texInfo.texture->readData(*pixelData, mip, face);
|
|
|
+ SPtr<PixelData> pixelData = texProps.allocBuffer(face, mip);
|
|
|
+ texInfo.texture->readData(*pixelData, mip, face);
|
|
|
|
|
|
- textureData->pixelData.push_back(pixelData);
|
|
|
- }
|
|
|
+ textureData->pixelData.push_back(pixelData);
|
|
|
}
|
|
|
-
|
|
|
- Path filePath = path + entry.first + ".asset";
|
|
|
- FileEncoder fe(filePath);
|
|
|
- fe.encode(textureData.get());
|
|
|
-
|
|
|
- texInfo.needsSaving = false;
|
|
|
}
|
|
|
+;
|
|
|
+ FileEncoder fe(path);
|
|
|
+ fe.encode(textureData.get());
|
|
|
}
|
|
|
}}
|