Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "trenderresourcemanager.h"
Toshihiro Shimizu 890ddd
#include "trenderer.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
//    Resource managers builder for instance-scoped resource managers
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
This manager class is used to create and destroy instance-scope managers through
Shinya Kitaoka 120a6e
the
Shinya Kitaoka 120a6e
renderStart/renderEnd notifications. Observe that this involves maintenance of a
Shinya Kitaoka 120a6e
container
Toshihiro Shimizu 890ddd
structure of the active renderIds against resource managers.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class RenderInstanceManagersBuilder final : public TRenderResourceManager {
Shinya Kitaoka 120a6e
  T_RENDER_RESOURCE_MANAGER
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  typedef std::vector<trenderresourcemanager *=""> ManagersVector;</trenderresourcemanager>
Shinya Kitaoka 120a6e
  std::map<unsigned long,="" managersvector=""> m_managersMap;</unsigned>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  RenderInstanceManagersBuilder() {}
Shinya Kitaoka 120a6e
  ~RenderInstanceManagersBuilder() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  static RenderInstanceManagersBuilder *instance();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRenderResourceManager *getManager(unsigned long renderId,
Shinya Kitaoka 120a6e
                                     unsigned int idx) const;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void onRenderInstanceStart(unsigned long id) override;
Shinya Kitaoka 473e70
  void onRenderInstanceEnd(unsigned long id) override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  bool renderHasOwnership() override { return false; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===============================================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class RenderInstanceManagersBuilderGenerator final
Shinya Kitaoka 120a6e
    : public TRenderResourceManagerGenerator {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 473e70
  TRenderResourceManager *operator()(void) override {
Shinya Kitaoka 120a6e
    return RenderInstanceManagersBuilder::instance();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
MANAGER_FILESCOPE_DECLARATION(RenderInstanceManagersBuilder,
Shinya Kitaoka 120a6e
                              RenderInstanceManagersBuilderGenerator);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
//    Stub managers and generators
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka 120a6e
These manager-generator stubs are substitutes used to maintain dependency order
Shinya Kitaoka 120a6e
about managers
Shinya Kitaoka 120a6e
which have render instance scope. They retrieve ordered event calls that are
Shinya Kitaoka 120a6e
passed to the
Toshihiro Shimizu 890ddd
dedicated instanceScope handler.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class InstanceResourceManagerStub final : public TRenderResourceManager {
Shinya Kitaoka 120a6e
  TRenderResourceManagerGenerator *m_generator;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  InstanceResourceManagerStub(TRenderResourceManagerGenerator *generator)
Shinya Kitaoka 120a6e
      : m_generator(generator) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void onRenderInstanceStart(unsigned long id) override;
Shinya Kitaoka 473e70
  void onRenderInstanceEnd(unsigned long id) override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void onRenderFrameStart(double f) override;
Shinya Kitaoka 473e70
  void onRenderFrameEnd(double f) override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void onRenderStatusStart(int renderStatus) override;
Shinya Kitaoka 473e70
  void onRenderStatusEnd(int renderStatus) override;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===============================================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class StubGenerator final : public TRenderResourceManagerGenerator {
Shinya Kitaoka 120a6e
  TRenderResourceManagerGenerator *m_generator;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  StubGenerator(TRenderResourceManagerGenerator *generator)
Shinya Kitaoka 120a6e
      : m_generator(generator) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  TRenderResourceManager *operator()() override {
Shinya Kitaoka 120a6e
    return new InstanceResourceManagerStub(m_generator);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
//    TRenderResourceManagerGenerator methods
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::vector<trenderresourcemanagergenerator *=""></trenderresourcemanagergenerator>
Shinya Kitaoka 120a6e
    &TRenderResourceManagerGenerator::generators() {
Shinya Kitaoka 120a6e
  static std::vector<trenderresourcemanagergenerator *=""> generatorsInstance;</trenderresourcemanagergenerator>
Shinya Kitaoka 120a6e
  return generatorsInstance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::vector<trenderresourcemanagergenerator *=""></trenderresourcemanagergenerator>
Shinya Kitaoka 120a6e
    &TRenderResourceManagerGenerator::generators(bool instanceScope) {
Shinya Kitaoka 120a6e
  static std::vector<trenderresourcemanagergenerator *=""> generatorsInstance;</trenderresourcemanagergenerator>
Shinya Kitaoka 120a6e
  static std::vector<trenderresourcemanagergenerator *=""> generatorsRenderer;</trenderresourcemanagergenerator>
Shinya Kitaoka 120a6e
  return instanceScope ? generatorsInstance : generatorsRenderer;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===============================================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TRenderResourceManagerGenerator::TRenderResourceManagerGenerator(
Shinya Kitaoka 120a6e
    bool renderInstanceScope)
Shinya Kitaoka 120a6e
    : m_instanceScope(renderInstanceScope) {
Shinya Kitaoka 120a6e
  // In case this has a renderInstanceScope, build a stub generator
Shinya Kitaoka 120a6e
  if (renderInstanceScope) {
Shinya Kitaoka 120a6e
    RenderInstanceManagersBuilder::gen();  // Stubs depend on this manager
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    static std::vector<trenderresourcemanagergenerator *=""> stubGenerators;</trenderresourcemanagergenerator>
Shinya Kitaoka 120a6e
    stubGenerators.push_back(new StubGenerator(this));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  generators().push_back(this);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::vector<trenderresourcemanagergenerator *=""> &scopeGenerators =</trenderresourcemanagergenerator>
Shinya Kitaoka 120a6e
      generators(renderInstanceScope);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  scopeGenerators.push_back(this);
Shinya Kitaoka 120a6e
  m_managerIndex = scopeGenerators.size() - 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TRenderResourceManager *TRenderResourceManagerGenerator::getManager(
Shinya Kitaoka 120a6e
    const TRenderer &renderer) const {
Shinya Kitaoka 120a6e
  return m_instanceScope ? 0 : renderer.getManager(m_managerIndex);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TRenderResourceManager *TRenderResourceManagerGenerator::getManager(
Shinya Kitaoka 120a6e
    unsigned long renderId) const {
Shinya Kitaoka 120a6e
  return m_instanceScope
Shinya Kitaoka 120a6e
             ? RenderInstanceManagersBuilder::instance()->getManager(
Shinya Kitaoka 120a6e
                   renderId, m_managerIndex)
Shinya Kitaoka 120a6e
             : 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
//    "Instance-scoped Managers" - Management methods
Toshihiro Shimizu 890ddd
//***********************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
RenderInstanceManagersBuilder *RenderInstanceManagersBuilder::instance() {
Shinya Kitaoka 120a6e
  static RenderInstanceManagersBuilder theInstance;
Shinya Kitaoka 120a6e
  return &theInstance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
inline TRenderResourceManager *RenderInstanceManagersBuilder::getManager(
Shinya Kitaoka 120a6e
    unsigned long renderId, unsigned int idx) const {
Shinya Kitaoka 120a6e
  std::map<unsigned long,="" managersvector="">::const_iterator it =</unsigned>
Shinya Kitaoka 120a6e
      m_managersMap.find(renderId);
Shinya Kitaoka 120a6e
  return it == m_managersMap.end() ? 0 : it->second[idx];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void RenderInstanceManagersBuilder::onRenderInstanceStart(unsigned long id) {
Shinya Kitaoka 120a6e
  // Build the instance managers
Shinya Kitaoka 120a6e
  std::map<unsigned long,="" managersvector="">::iterator it =</unsigned>
Shinya Kitaoka 120a6e
      m_managersMap.insert(std::make_pair(id, ManagersVector())).first;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::vector<trenderresourcemanagergenerator *=""> &instanceScopeGenerators =</trenderresourcemanagergenerator>
Shinya Kitaoka 120a6e
      TRenderResourceManagerGenerator::generators(true);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  unsigned int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < instanceScopeGenerators.size(); ++i)
Shinya Kitaoka 120a6e
    it->second.push_back((*instanceScopeGenerators[i])());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void RenderInstanceManagersBuilder::onRenderInstanceEnd(unsigned long id) {
Shinya Kitaoka 120a6e
  // Delete the instance managers
Shinya Kitaoka 120a6e
  std::map<unsigned long,="" managersvector="">::iterator it = m_managersMap.find(id);</unsigned>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(it != m_managersMap.end());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  unsigned int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < it->second.size(); ++i) {
Shinya Kitaoka 120a6e
    if (it->second[i]->renderHasOwnership()) delete it->second[i];
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_managersMap.erase(it);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===============================================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void InstanceResourceManagerStub::onRenderInstanceStart(unsigned long id) {
Shinya Kitaoka 120a6e
  RenderInstanceManagersBuilder::instance()
Shinya Kitaoka 120a6e
      ->getManager(id, m_generator->getGeneratorIndex())
Shinya Kitaoka 120a6e
      ->onRenderInstanceStart(id);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void InstanceResourceManagerStub::onRenderInstanceEnd(unsigned long id) {
Shinya Kitaoka 120a6e
  RenderInstanceManagersBuilder::instance()
Shinya Kitaoka 120a6e
      ->getManager(id, m_generator->getGeneratorIndex())
Shinya Kitaoka 120a6e
      ->onRenderInstanceEnd(id);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void InstanceResourceManagerStub::onRenderFrameStart(double f) {
Shinya Kitaoka 120a6e
  RenderInstanceManagersBuilder::instance()
Shinya Kitaoka 120a6e
      ->getManager(TRenderer::renderId(), m_generator->getGeneratorIndex())
Shinya Kitaoka 120a6e
      ->onRenderFrameStart(f);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void InstanceResourceManagerStub::onRenderFrameEnd(double f) {
Shinya Kitaoka 120a6e
  RenderInstanceManagersBuilder::instance()
Shinya Kitaoka 120a6e
      ->getManager(TRenderer::renderId(), m_generator->getGeneratorIndex())
Shinya Kitaoka 120a6e
      ->onRenderFrameEnd(f);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void InstanceResourceManagerStub::onRenderStatusStart(int renderStatus) {
Shinya Kitaoka 120a6e
  RenderInstanceManagersBuilder::instance()
Shinya Kitaoka 120a6e
      ->getManager(TRenderer::renderId(), m_generator->getGeneratorIndex())
Shinya Kitaoka 120a6e
      ->onRenderStatusStart(renderStatus);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void InstanceResourceManagerStub::onRenderStatusEnd(int renderStatus) {
Shinya Kitaoka 120a6e
  RenderInstanceManagersBuilder::instance()
Shinya Kitaoka 120a6e
      ->getManager(TRenderer::renderId(), m_generator->getGeneratorIndex())
Shinya Kitaoka 120a6e
      ->onRenderStatusEnd(renderStatus);
Toshihiro Shimizu 890ddd
}