SceneBaker.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. //
  21. #include <xmmintrin.h>
  22. #include <pmmintrin.h>
  23. #include <cmath>
  24. #include <cfloat>
  25. #include <ThirdParty/STB/stb_rect_pack.h>
  26. #include <Atomic/Core/WorkQueue.h>
  27. #include <Atomic/IO/Log.h>
  28. #include <Atomic/Resource/ResourceCache.h>
  29. #include <Atomic/Graphics/Zone.h>
  30. #include <Atomic/Graphics/Light.h>
  31. #include <Atomic/Graphics/StaticModel.h>
  32. #include "BakeModel.h"
  33. #include "BakeMesh.h"
  34. #include "BakeLight.h"
  35. #include "EmbreeScene.h"
  36. #include "LightMapPacker.h"
  37. #include "SceneBaker.h"
  38. namespace AtomicGlow
  39. {
  40. SceneBaker::SceneBaker(Context* context) : Object(context),
  41. currentLightMode_(GLOW_LIGHTMODE_UNDEFINED),
  42. currentGIPass_(0),
  43. standaloneMode_(true)
  44. {
  45. embreeScene_ = new EmbreeScene(context_);
  46. }
  47. SceneBaker::~SceneBaker()
  48. {
  49. }
  50. bool SceneBaker::SaveLitScene()
  51. {
  52. if (!standaloneMode_)
  53. {
  54. ATOMIC_LOGERROR("SceneBaker::SaveLitScene() - only supported in standalone mode");
  55. return false;
  56. }
  57. #ifdef ATOMIC_PLATFORM_WINDOWS
  58. String scenefilename = ToString("C:/Dev/atomic/AtomicExamplesPrivate/AtomicGlowTests/TestScene1/Resources/Scenes/LitScene.scene");
  59. #else
  60. String scenefilename = ToString("%s/Resources/Scenes/LitScene.scene", GlobalGlowSettings.projectPath_.CString());
  61. #endif
  62. File saveFile(context_, scenefilename, FILE_WRITE);
  63. return scene_->SaveXML(saveFile);
  64. }
  65. bool SceneBaker::WriteBakeData(VectorBuffer& buffer)
  66. {
  67. buffer.Clear();
  68. // protocol is very simple right now, can easily be expanded
  69. buffer.WriteUInt(bakeMeshes_.Size());
  70. for (unsigned i = 0; i < bakeMeshes_.Size(); i++)
  71. {
  72. BakeMesh* bakeMesh = bakeMeshes_[i];
  73. Node* node = bakeMesh->GetNode();
  74. StaticModel* staticModel = bakeMesh->GetStaticModel();
  75. buffer.WriteUInt(node->GetID());
  76. buffer.WriteUInt(staticModel->GetID());
  77. buffer.WriteUInt(staticModel->GetLightMask());
  78. buffer.WriteUInt(staticModel->GetLightmapIndex());
  79. buffer.WriteVector4(staticModel->GetLightmapTilingOffset());
  80. }
  81. return true;
  82. }
  83. bool SceneBaker::GenerateLightmaps()
  84. {
  85. ATOMIC_LOGINFO("Generating Lightmaps");
  86. for (unsigned i = 0; i < bakeMeshes_.Size(); i++)
  87. {
  88. BakeMesh* mesh = bakeMeshes_[i];
  89. mesh->GenerateRadianceMap();
  90. }
  91. SharedPtr<LightMapPacker> packer(new LightMapPacker(context_));
  92. for (unsigned i = 0; i < bakeMeshes_.Size(); i++)
  93. {
  94. BakeMesh* mesh = bakeMeshes_[i];
  95. SharedPtr<RadianceMap> radianceMap = mesh->GetRadianceMap();
  96. if (radianceMap.NotNull())
  97. {
  98. packer->AddRadianceMap(radianceMap);
  99. }
  100. }
  101. packer->Pack();
  102. packer->SaveLightmaps();
  103. return WriteBakeData(bakeData_);
  104. }
  105. void SceneBaker::TraceRay(LightRay* lightRay, const PODVector<BakeLight*>& bakeLights_)
  106. {
  107. for (unsigned i = 0; i < bakeLights_.Size(); i++)
  108. {
  109. bakeLights_[i]->Light(lightRay);
  110. }
  111. }
  112. bool SceneBaker::LightDirect()
  113. {
  114. // Direct Lighting
  115. currentLightMode_ = GLOW_LIGHTMODE_DIRECT;
  116. if (!bakeMeshes_.Size())
  117. {
  118. ATOMIC_LOGINFO("SceneBaker::LightDirect() - No bake meshes found");
  119. bakeLights_.Clear();
  120. return false;
  121. }
  122. for (unsigned i = 0; i < bakeMeshes_.Size(); i++)
  123. {
  124. BakeMesh* mesh = bakeMeshes_[i];
  125. mesh->Light(GLOW_LIGHTMODE_DIRECT);
  126. }
  127. return true;
  128. }
  129. void SceneBaker::LightDirectFinish()
  130. {
  131. bakeLights_.Clear();
  132. }
  133. bool SceneBaker::LightGI()
  134. {
  135. // Indirect Lighting
  136. currentLightMode_ = GLOW_LIGHTMODE_INDIRECT;
  137. if (!GlobalGlowSettings.giEnabled_ || currentGIPass_ >= GlobalGlowSettings.giMaxBounces_)
  138. {
  139. return false;
  140. }
  141. ATOMIC_LOGINFOF("GI Pass #%i of %i", currentGIPass_ + 1, GlobalGlowSettings.giMaxBounces_);
  142. BounceBakeLight* blight;
  143. unsigned totalBounceSamples = 0;
  144. for (unsigned i = 0; i < bakeMeshes_.Size(); i++)
  145. {
  146. BakeMesh* mesh = bakeMeshes_[i];
  147. blight = mesh->GenerateBounceBakeLight();
  148. if (blight)
  149. {
  150. bakeLights_.Push(SharedPtr<BakeLight>(blight));
  151. totalBounceSamples += blight->GetNumBounceSamples();
  152. }
  153. }
  154. if (!bakeLights_.Size())
  155. return false;
  156. ATOMIC_LOGINFOF("IMPORTANT: Optimize allocation of samples! Indirect lighting with %u bounce lights and %u samples", bakeLights_.Size(), totalBounceSamples);
  157. for (unsigned i = 0; i < bakeMeshes_.Size(); i++)
  158. {
  159. BakeMesh* mesh = bakeMeshes_[i];
  160. mesh->Light(GLOW_LIGHTMODE_INDIRECT);
  161. }
  162. return true;
  163. }
  164. void SceneBaker::LightGIFinish()
  165. {
  166. bakeLights_.Clear();
  167. currentGIPass_++;
  168. if (currentGIPass_ >= GlobalGlowSettings.giMaxBounces_)
  169. {
  170. }
  171. }
  172. bool SceneBaker::Light(const GlowLightMode lightMode)
  173. {
  174. if (lightMode == GLOW_LIGHTMODE_DIRECT)
  175. {
  176. if (!LightDirect())
  177. {
  178. currentLightMode_ = GLOW_LIGHTMODE_COMPLETE;
  179. ATOMIC_LOGINFO("Cycle: Direct Lighting - no work to be done");
  180. return false;
  181. }
  182. ATOMIC_LOGINFO("Cycle: Direct Lighting");
  183. return true;
  184. }
  185. if (lightMode == GLOW_LIGHTMODE_INDIRECT)
  186. {
  187. if (!LightGI())
  188. {
  189. currentLightMode_ = GLOW_LIGHTMODE_COMPLETE;
  190. ATOMIC_LOGINFO("Cycle: GI - no work to be done");
  191. return false;
  192. }
  193. return true;
  194. }
  195. return true;
  196. }
  197. void SceneBaker::LightFinishCycle()
  198. {
  199. if (currentLightMode_ == GLOW_LIGHTMODE_DIRECT)
  200. {
  201. LightDirectFinish();
  202. }
  203. if (currentLightMode_ == GLOW_LIGHTMODE_INDIRECT)
  204. {
  205. LightGIFinish();
  206. }
  207. }
  208. void SceneBaker::QueryLights(const BoundingBox& bbox, PODVector<BakeLight*>& lights)
  209. {
  210. lights.Clear();
  211. for (unsigned i = 0; i < bakeLights_.Size(); i++)
  212. {
  213. // TODO: filter on zone, range, groups
  214. lights.Push(bakeLights_[i]);
  215. }
  216. }
  217. bool SceneBaker::Preprocess()
  218. {
  219. Vector<SharedPtr<BakeMesh>>::Iterator itr = bakeMeshes_.Begin();
  220. while (itr != bakeMeshes_.End())
  221. {
  222. (*itr)->Preprocess();
  223. itr++;
  224. }
  225. embreeScene_->Commit();
  226. return true;
  227. }
  228. bool SceneBaker::LoadScene(const String& filename)
  229. {
  230. ResourceCache* cache = GetSubsystem<ResourceCache>();
  231. SharedPtr<File> file = cache->GetFile(filename);
  232. if (!file || !file->IsOpen())
  233. {
  234. return false;
  235. }
  236. scene_ = new Scene(context_);
  237. if (!scene_->LoadXML(*file))
  238. {
  239. scene_ = 0;
  240. return false;
  241. }
  242. // IMPORTANT!, if scene updates are enabled
  243. // the Octree component will add work queue items
  244. // and will call WorkQueue->Complete(), see Octree::HandleRenderUpdate
  245. scene_->SetUpdateEnabled(false);
  246. // Zones
  247. PODVector<Node*> zoneNodes;
  248. PODVector<Zone*> zones;
  249. scene_->GetChildrenWithComponent<Zone>(zoneNodes, true);
  250. for (unsigned i = 0; i < zoneNodes.Size(); i++)
  251. {
  252. Zone* zone = zoneNodes[i]->GetComponent<Zone>();
  253. if (!zone->GetNode()->IsEnabled() || !zone->IsEnabled())
  254. continue;
  255. zones.Push(zone);;
  256. SharedPtr<ZoneBakeLight> zlight(new ZoneBakeLight(context_, this));
  257. zlight->SetZone(zone);
  258. bakeLights_.Push(zlight);
  259. }
  260. // Lights
  261. PODVector<Node*> lightNodes;
  262. scene_->GetChildrenWithComponent<Atomic::Light>(lightNodes, true);
  263. for (unsigned i = 0; i < lightNodes.Size(); i++)
  264. {
  265. Atomic::Light* light = lightNodes[i]->GetComponent<Atomic::Light>();
  266. if (!light->GetNode()->IsEnabled()|| !light->IsEnabled())
  267. continue;
  268. if (light->GetLightType() == LIGHT_DIRECTIONAL)
  269. {
  270. SharedPtr<DirectionalBakeLight> dlight(new DirectionalBakeLight(context_, this));
  271. dlight->SetLight(light);
  272. bakeLights_.Push(dlight);
  273. }
  274. else if (light->GetLightType() == LIGHT_POINT)
  275. {
  276. SharedPtr<PointBakeLight> dlight(new PointBakeLight(context_, this));
  277. dlight->SetLight(light);
  278. bakeLights_.Push(dlight);
  279. }
  280. }
  281. // Static Models
  282. PODVector<StaticModel*> staticModels;
  283. scene_->GetComponents<StaticModel>(staticModels, true);
  284. for (unsigned i = 0; i < staticModels.Size(); i++)
  285. {
  286. StaticModel* staticModel = staticModels[i];
  287. if (!staticModel->GetNode()->IsEnabled() || !staticModel->IsEnabled())
  288. continue;
  289. Vector3 center = staticModel->GetWorldBoundingBox().Center();
  290. int bestPriority = M_MIN_INT;
  291. Zone* newZone = 0;
  292. for (PODVector<Zone*>::Iterator i = zones.Begin(); i != zones.End(); ++i)
  293. {
  294. Zone* zone = *i;
  295. int priority = zone->GetPriority();
  296. if (priority > bestPriority && (staticModel->GetZoneMask() & zone->GetZoneMask()) && zone->IsInside(center))
  297. {
  298. newZone = zone;
  299. bestPriority = priority;
  300. }
  301. }
  302. staticModel->SetZone(newZone, false);
  303. if (staticModel->GetModel() && (staticModel->GetLightmap() ||staticModel->GetCastShadows()))
  304. {
  305. SharedPtr<BakeMesh> meshMap (new BakeMesh(context_, this));
  306. meshMap->SetStaticModel(staticModel);
  307. bakeMeshes_.Push(meshMap);
  308. }
  309. }
  310. return Preprocess();
  311. }
  312. }