Example #1
0
void shade::Component::compile(Formatter& fmt, const FileAccumulator& acc, ShaderEnvironment env, formatter::Constants::Primitive primitive_type)
{
  // Setup shader environment
  fmt.setup(m_objects.get_next_index(), primitive_type);
  fmt.insert_sources(m_sources);

  // Output class methods and properties
  IterateAttributes instance_var_output(bind(&Component::output_attribute, boost::cref(*this), boost::ref(fmt), _1),
      bind(&Component::get_type_state, boost::ref(*this), _1)
      );
  instance_var_output(get_pointer(m_shader));
  std::for_each(m_classbins.begin(), m_classbins.end(), SelfArgumentOutput(fmt, env, m_objects));
  std::for_each(m_classbins.begin(), m_classbins.end(), MethodOutput(fmt, env, m_objects));
  std::for_each(m_classbins.begin(), m_classbins.end(),
      PropertyOutput(bind(&Component::output_property_dispatcher, boost::cref(*this), boost::ref(fmt), _1),
        fmt, m_objects));

  // Write initializer in (if possible) correct order
  fmt.begin_initialize("shade_initialize");
  InitializerOutput initializer_output(fmt, env, m_objects, bind(&Component::get_type_state, this, _1));
  initializer_output(&*m_shader);
  fmt.end_initialize();
  if (env == geometry_shader)
  {
    fmt.begin_initialize("shade_initialize_post", false);
    InitializerOutput initializer_output(fmt, post_geometry_shader, m_objects, bind(&Component::get_type_state, this, _1));
    initializer_output(&*m_shader);
    fmt.end_initialize();
  }

  // Finally write main entry function
  shade::shaders::Enterable* entry = dynamic_cast<shade::shaders::Enterable*>(get_pointer(m_shader));
  if (entry)
  {
    std::string class_name(entry->shade::shaders::Enterable::get_class_name());
    std::string method_name(entry->get_entry_name(env));
    fmt.invoke(class_name, method_name);
  }

  m_requires_compilation = false;
}