dragonslayer0531

Hacky Queue Sorting

Jan 18th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1.     void SceneRenderer::Receive(const gfx::ModelSubmittedEvent & ModelSubmission)
  2.     {
  3.         std::unordered_map<gfx::VertexBuffer *, std::unordered_map<gfx::GraphicsPipeline *, std::unordered_map<gfx::Material *, std::vector<ecs::ID>>>> sort_map;
  4.         StateChanges_.clear();
  5.         Entities_.clear();
  6.         RenderableIDs_.clear();
  7.         auto manager = ECSManager::GetEntityManager();
  8.  
  9.         for (auto & ent : ECSManager::GetEntityManager()->GetEntitiesWithComponents<gfx::RenderableComponent>())
  10.         {
  11.             auto id = ent.GetID();
  12.             auto & ent = manager->Get(id);
  13.             size_t renderable_id = 0;
  14.  
  15.             for (auto renderable : ent.GetComponent<gfx::RenderableComponent>()->renderables)
  16.             {
  17.                 RenderableIDs_.push_back(renderable_id++);
  18.                 auto modl = sort_map.find(renderable.GetModel()->Buffer);
  19.                 if (modl == sort_map.end())
  20.                 {
  21.                     std::unordered_map<gfx::GraphicsPipeline*, std::unordered_map<gfx::Material *, std::vector<ecs::ID>>> material_sort_map;
  22.                     std::unordered_map<gfx::Material *, std::vector<ecs::ID>> mats;
  23.                     std::vector<ecs::ID> ids = { id };
  24.                     mats.insert(std::make_pair(renderable.GetMaterial(), ids));
  25.                     material_sort_map.insert(std::make_pair(renderable.GetMaterial()->GetPipeline(), mats));
  26.                     sort_map.insert(std::make_pair(renderable.GetModel()->Buffer, material_sort_map));
  27.                 }
  28.                 else
  29.                 {
  30.                     auto pipe = modl->second.find(renderable.GetMaterial()->GetPipeline());
  31.                     if (pipe == modl->second.end())
  32.                     {
  33.                         std::unordered_map<gfx::Material *, std::vector<ecs::ID>> mats;
  34.                         std::vector<ecs::ID> ids = { id };
  35.                         mats.insert(std::make_pair(renderable.GetMaterial(), ids));
  36.                     }
  37.                     else
  38.                     {
  39.                         auto matl = pipe->second.find(renderable.GetMaterial());
  40.                         if (matl == pipe->second.end())
  41.                         {
  42.                             std::vector<ecs::ID> ids = { id };
  43.                             pipe->second.insert(std::make_pair(renderable.GetMaterial(), ids));
  44.                         }
  45.                         else
  46.                         {
  47.                             matl->second.push_back(id);
  48.                         }
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.         Entities_.clear();
  54.         size_t counter = 0;
  55.         for (auto model : sort_map)
  56.         {
  57.             if (counter == StateChanges_.size()) StateChanges_.push_back(MODL);
  58.             for (auto pipeline : model.second)
  59.             {
  60.                 if (counter == StateChanges_.size()) StateChanges_.push_back(PIPE);
  61.                 for (auto & material : pipeline.second)
  62.                 {
  63.                     if (counter == StateChanges_.size()) StateChanges_.push_back(MATL);
  64.                     for (auto & instance : material.second)
  65.                     {
  66.                         Entities_.push_back(std::move(instance));
  67.                         if (counter == StateChanges_.size()) StateChanges_.push_back(NONE);
  68.                         counter++;
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.         LUMINOS_ASSERT(Entities_.size() == StateChanges_.size())
  74.     }
Add Comment
Please, Sign In to add comment